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; |
eaa6bcb7 HL |
241 | u32 btf_id; |
242 | u32 ret_btf_id; | |
33ff9823 DB |
243 | }; |
244 | ||
8580ac94 AS |
245 | struct btf *btf_vmlinux; |
246 | ||
cbd35700 AS |
247 | static DEFINE_MUTEX(bpf_verifier_lock); |
248 | ||
d9762e84 MKL |
249 | static const struct bpf_line_info * |
250 | find_linfo(const struct bpf_verifier_env *env, u32 insn_off) | |
251 | { | |
252 | const struct bpf_line_info *linfo; | |
253 | const struct bpf_prog *prog; | |
254 | u32 i, nr_linfo; | |
255 | ||
256 | prog = env->prog; | |
257 | nr_linfo = prog->aux->nr_linfo; | |
258 | ||
259 | if (!nr_linfo || insn_off >= prog->len) | |
260 | return NULL; | |
261 | ||
262 | linfo = prog->aux->linfo; | |
263 | for (i = 1; i < nr_linfo; i++) | |
264 | if (insn_off < linfo[i].insn_off) | |
265 | break; | |
266 | ||
267 | return &linfo[i - 1]; | |
268 | } | |
269 | ||
77d2e05a MKL |
270 | void bpf_verifier_vlog(struct bpf_verifier_log *log, const char *fmt, |
271 | va_list args) | |
cbd35700 | 272 | { |
a2a7d570 | 273 | unsigned int n; |
cbd35700 | 274 | |
a2a7d570 | 275 | n = vscnprintf(log->kbuf, BPF_VERIFIER_TMP_LOG_SIZE, fmt, args); |
a2a7d570 JK |
276 | |
277 | WARN_ONCE(n >= BPF_VERIFIER_TMP_LOG_SIZE - 1, | |
278 | "verifier log line truncated - local buffer too short\n"); | |
279 | ||
280 | n = min(log->len_total - log->len_used - 1, n); | |
281 | log->kbuf[n] = '\0'; | |
282 | ||
8580ac94 AS |
283 | if (log->level == BPF_LOG_KERNEL) { |
284 | pr_err("BPF:%s\n", log->kbuf); | |
285 | return; | |
286 | } | |
a2a7d570 JK |
287 | if (!copy_to_user(log->ubuf + log->len_used, log->kbuf, n + 1)) |
288 | log->len_used += n; | |
289 | else | |
290 | log->ubuf = NULL; | |
cbd35700 | 291 | } |
abe08840 | 292 | |
6f8a57cc AN |
293 | static void bpf_vlog_reset(struct bpf_verifier_log *log, u32 new_pos) |
294 | { | |
295 | char zero = 0; | |
296 | ||
297 | if (!bpf_verifier_log_needed(log)) | |
298 | return; | |
299 | ||
300 | log->len_used = new_pos; | |
301 | if (put_user(zero, log->ubuf + new_pos)) | |
302 | log->ubuf = NULL; | |
303 | } | |
304 | ||
abe08840 JO |
305 | /* log_level controls verbosity level of eBPF verifier. |
306 | * bpf_verifier_log_write() is used to dump the verification trace to the log, | |
307 | * so the user can figure out what's wrong with the program | |
430e68d1 | 308 | */ |
abe08840 JO |
309 | __printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env, |
310 | const char *fmt, ...) | |
311 | { | |
312 | va_list args; | |
313 | ||
77d2e05a MKL |
314 | if (!bpf_verifier_log_needed(&env->log)) |
315 | return; | |
316 | ||
abe08840 | 317 | va_start(args, fmt); |
77d2e05a | 318 | bpf_verifier_vlog(&env->log, fmt, args); |
abe08840 JO |
319 | va_end(args); |
320 | } | |
321 | EXPORT_SYMBOL_GPL(bpf_verifier_log_write); | |
322 | ||
323 | __printf(2, 3) static void verbose(void *private_data, const char *fmt, ...) | |
324 | { | |
77d2e05a | 325 | struct bpf_verifier_env *env = private_data; |
abe08840 JO |
326 | va_list args; |
327 | ||
77d2e05a MKL |
328 | if (!bpf_verifier_log_needed(&env->log)) |
329 | return; | |
330 | ||
abe08840 | 331 | va_start(args, fmt); |
77d2e05a | 332 | bpf_verifier_vlog(&env->log, fmt, args); |
abe08840 JO |
333 | va_end(args); |
334 | } | |
cbd35700 | 335 | |
9e15db66 AS |
336 | __printf(2, 3) void bpf_log(struct bpf_verifier_log *log, |
337 | const char *fmt, ...) | |
338 | { | |
339 | va_list args; | |
340 | ||
341 | if (!bpf_verifier_log_needed(log)) | |
342 | return; | |
343 | ||
344 | va_start(args, fmt); | |
345 | bpf_verifier_vlog(log, fmt, args); | |
346 | va_end(args); | |
347 | } | |
348 | ||
d9762e84 MKL |
349 | static const char *ltrim(const char *s) |
350 | { | |
351 | while (isspace(*s)) | |
352 | s++; | |
353 | ||
354 | return s; | |
355 | } | |
356 | ||
357 | __printf(3, 4) static void verbose_linfo(struct bpf_verifier_env *env, | |
358 | u32 insn_off, | |
359 | const char *prefix_fmt, ...) | |
360 | { | |
361 | const struct bpf_line_info *linfo; | |
362 | ||
363 | if (!bpf_verifier_log_needed(&env->log)) | |
364 | return; | |
365 | ||
366 | linfo = find_linfo(env, insn_off); | |
367 | if (!linfo || linfo == env->prev_linfo) | |
368 | return; | |
369 | ||
370 | if (prefix_fmt) { | |
371 | va_list args; | |
372 | ||
373 | va_start(args, prefix_fmt); | |
374 | bpf_verifier_vlog(&env->log, prefix_fmt, args); | |
375 | va_end(args); | |
376 | } | |
377 | ||
378 | verbose(env, "%s\n", | |
379 | ltrim(btf_name_by_offset(env->prog->aux->btf, | |
380 | linfo->line_off))); | |
381 | ||
382 | env->prev_linfo = linfo; | |
383 | } | |
384 | ||
de8f3a83 DB |
385 | static bool type_is_pkt_pointer(enum bpf_reg_type type) |
386 | { | |
387 | return type == PTR_TO_PACKET || | |
388 | type == PTR_TO_PACKET_META; | |
389 | } | |
390 | ||
46f8bc92 MKL |
391 | static bool type_is_sk_pointer(enum bpf_reg_type type) |
392 | { | |
393 | return type == PTR_TO_SOCKET || | |
655a51e5 | 394 | type == PTR_TO_SOCK_COMMON || |
fada7fdc JL |
395 | type == PTR_TO_TCP_SOCK || |
396 | type == PTR_TO_XDP_SOCK; | |
46f8bc92 MKL |
397 | } |
398 | ||
cac616db JF |
399 | static bool reg_type_not_null(enum bpf_reg_type type) |
400 | { | |
401 | return type == PTR_TO_SOCKET || | |
402 | type == PTR_TO_TCP_SOCK || | |
403 | type == PTR_TO_MAP_VALUE || | |
01c66c48 | 404 | type == PTR_TO_SOCK_COMMON; |
cac616db JF |
405 | } |
406 | ||
840b9615 JS |
407 | static bool reg_type_may_be_null(enum bpf_reg_type type) |
408 | { | |
fd978bf7 | 409 | return type == PTR_TO_MAP_VALUE_OR_NULL || |
46f8bc92 | 410 | type == PTR_TO_SOCKET_OR_NULL || |
655a51e5 | 411 | type == PTR_TO_SOCK_COMMON_OR_NULL || |
b121b341 | 412 | type == PTR_TO_TCP_SOCK_OR_NULL || |
457f4436 | 413 | type == PTR_TO_BTF_ID_OR_NULL || |
afbf21dc YS |
414 | type == PTR_TO_MEM_OR_NULL || |
415 | type == PTR_TO_RDONLY_BUF_OR_NULL || | |
416 | type == PTR_TO_RDWR_BUF_OR_NULL; | |
fd978bf7 JS |
417 | } |
418 | ||
d83525ca AS |
419 | static bool reg_may_point_to_spin_lock(const struct bpf_reg_state *reg) |
420 | { | |
421 | return reg->type == PTR_TO_MAP_VALUE && | |
422 | map_value_has_spin_lock(reg->map_ptr); | |
423 | } | |
424 | ||
cba368c1 MKL |
425 | static bool reg_type_may_be_refcounted_or_null(enum bpf_reg_type type) |
426 | { | |
427 | return type == PTR_TO_SOCKET || | |
428 | type == PTR_TO_SOCKET_OR_NULL || | |
429 | type == PTR_TO_TCP_SOCK || | |
457f4436 AN |
430 | type == PTR_TO_TCP_SOCK_OR_NULL || |
431 | type == PTR_TO_MEM || | |
432 | type == PTR_TO_MEM_OR_NULL; | |
cba368c1 MKL |
433 | } |
434 | ||
1b986589 | 435 | static bool arg_type_may_be_refcounted(enum bpf_arg_type type) |
fd978bf7 | 436 | { |
1b986589 | 437 | return type == ARG_PTR_TO_SOCK_COMMON; |
fd978bf7 JS |
438 | } |
439 | ||
fd1b0d60 LB |
440 | static bool arg_type_may_be_null(enum bpf_arg_type type) |
441 | { | |
442 | return type == ARG_PTR_TO_MAP_VALUE_OR_NULL || | |
443 | type == ARG_PTR_TO_MEM_OR_NULL || | |
444 | type == ARG_PTR_TO_CTX_OR_NULL || | |
445 | type == ARG_PTR_TO_SOCKET_OR_NULL || | |
446 | type == ARG_PTR_TO_ALLOC_MEM_OR_NULL; | |
447 | } | |
448 | ||
fd978bf7 JS |
449 | /* Determine whether the function releases some resources allocated by another |
450 | * function call. The first reference type argument will be assumed to be | |
451 | * released by release_reference(). | |
452 | */ | |
453 | static bool is_release_function(enum bpf_func_id func_id) | |
454 | { | |
457f4436 AN |
455 | return func_id == BPF_FUNC_sk_release || |
456 | func_id == BPF_FUNC_ringbuf_submit || | |
457 | func_id == BPF_FUNC_ringbuf_discard; | |
840b9615 JS |
458 | } |
459 | ||
64d85290 | 460 | static bool may_be_acquire_function(enum bpf_func_id func_id) |
46f8bc92 MKL |
461 | { |
462 | return func_id == BPF_FUNC_sk_lookup_tcp || | |
edbf8c01 | 463 | func_id == BPF_FUNC_sk_lookup_udp || |
64d85290 | 464 | func_id == BPF_FUNC_skc_lookup_tcp || |
457f4436 AN |
465 | func_id == BPF_FUNC_map_lookup_elem || |
466 | func_id == BPF_FUNC_ringbuf_reserve; | |
64d85290 JS |
467 | } |
468 | ||
469 | static bool is_acquire_function(enum bpf_func_id func_id, | |
470 | const struct bpf_map *map) | |
471 | { | |
472 | enum bpf_map_type map_type = map ? map->map_type : BPF_MAP_TYPE_UNSPEC; | |
473 | ||
474 | if (func_id == BPF_FUNC_sk_lookup_tcp || | |
475 | func_id == BPF_FUNC_sk_lookup_udp || | |
457f4436 AN |
476 | func_id == BPF_FUNC_skc_lookup_tcp || |
477 | func_id == BPF_FUNC_ringbuf_reserve) | |
64d85290 JS |
478 | return true; |
479 | ||
480 | if (func_id == BPF_FUNC_map_lookup_elem && | |
481 | (map_type == BPF_MAP_TYPE_SOCKMAP || | |
482 | map_type == BPF_MAP_TYPE_SOCKHASH)) | |
483 | return true; | |
484 | ||
485 | return false; | |
46f8bc92 MKL |
486 | } |
487 | ||
1b986589 MKL |
488 | static bool is_ptr_cast_function(enum bpf_func_id func_id) |
489 | { | |
490 | return func_id == BPF_FUNC_tcp_sock || | |
1df8f55a MKL |
491 | func_id == BPF_FUNC_sk_fullsock || |
492 | func_id == BPF_FUNC_skc_to_tcp_sock || | |
493 | func_id == BPF_FUNC_skc_to_tcp6_sock || | |
494 | func_id == BPF_FUNC_skc_to_udp6_sock || | |
495 | func_id == BPF_FUNC_skc_to_tcp_timewait_sock || | |
496 | func_id == BPF_FUNC_skc_to_tcp_request_sock; | |
1b986589 MKL |
497 | } |
498 | ||
17a52670 AS |
499 | /* string representation of 'enum bpf_reg_type' */ |
500 | static const char * const reg_type_str[] = { | |
501 | [NOT_INIT] = "?", | |
f1174f77 | 502 | [SCALAR_VALUE] = "inv", |
17a52670 AS |
503 | [PTR_TO_CTX] = "ctx", |
504 | [CONST_PTR_TO_MAP] = "map_ptr", | |
505 | [PTR_TO_MAP_VALUE] = "map_value", | |
506 | [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null", | |
17a52670 | 507 | [PTR_TO_STACK] = "fp", |
969bf05e | 508 | [PTR_TO_PACKET] = "pkt", |
de8f3a83 | 509 | [PTR_TO_PACKET_META] = "pkt_meta", |
969bf05e | 510 | [PTR_TO_PACKET_END] = "pkt_end", |
d58e468b | 511 | [PTR_TO_FLOW_KEYS] = "flow_keys", |
c64b7983 JS |
512 | [PTR_TO_SOCKET] = "sock", |
513 | [PTR_TO_SOCKET_OR_NULL] = "sock_or_null", | |
46f8bc92 MKL |
514 | [PTR_TO_SOCK_COMMON] = "sock_common", |
515 | [PTR_TO_SOCK_COMMON_OR_NULL] = "sock_common_or_null", | |
655a51e5 MKL |
516 | [PTR_TO_TCP_SOCK] = "tcp_sock", |
517 | [PTR_TO_TCP_SOCK_OR_NULL] = "tcp_sock_or_null", | |
9df1c28b | 518 | [PTR_TO_TP_BUFFER] = "tp_buffer", |
fada7fdc | 519 | [PTR_TO_XDP_SOCK] = "xdp_sock", |
9e15db66 | 520 | [PTR_TO_BTF_ID] = "ptr_", |
b121b341 | 521 | [PTR_TO_BTF_ID_OR_NULL] = "ptr_or_null_", |
eaa6bcb7 | 522 | [PTR_TO_PERCPU_BTF_ID] = "percpu_ptr_", |
457f4436 AN |
523 | [PTR_TO_MEM] = "mem", |
524 | [PTR_TO_MEM_OR_NULL] = "mem_or_null", | |
afbf21dc YS |
525 | [PTR_TO_RDONLY_BUF] = "rdonly_buf", |
526 | [PTR_TO_RDONLY_BUF_OR_NULL] = "rdonly_buf_or_null", | |
527 | [PTR_TO_RDWR_BUF] = "rdwr_buf", | |
528 | [PTR_TO_RDWR_BUF_OR_NULL] = "rdwr_buf_or_null", | |
17a52670 AS |
529 | }; |
530 | ||
8efea21d EC |
531 | static char slot_type_char[] = { |
532 | [STACK_INVALID] = '?', | |
533 | [STACK_SPILL] = 'r', | |
534 | [STACK_MISC] = 'm', | |
535 | [STACK_ZERO] = '0', | |
536 | }; | |
537 | ||
4e92024a AS |
538 | static void print_liveness(struct bpf_verifier_env *env, |
539 | enum bpf_reg_liveness live) | |
540 | { | |
9242b5f5 | 541 | if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN | REG_LIVE_DONE)) |
4e92024a AS |
542 | verbose(env, "_"); |
543 | if (live & REG_LIVE_READ) | |
544 | verbose(env, "r"); | |
545 | if (live & REG_LIVE_WRITTEN) | |
546 | verbose(env, "w"); | |
9242b5f5 AS |
547 | if (live & REG_LIVE_DONE) |
548 | verbose(env, "D"); | |
4e92024a AS |
549 | } |
550 | ||
f4d7e40a AS |
551 | static struct bpf_func_state *func(struct bpf_verifier_env *env, |
552 | const struct bpf_reg_state *reg) | |
553 | { | |
554 | struct bpf_verifier_state *cur = env->cur_state; | |
555 | ||
556 | return cur->frame[reg->frameno]; | |
557 | } | |
558 | ||
9e15db66 AS |
559 | const char *kernel_type_name(u32 id) |
560 | { | |
561 | return btf_name_by_offset(btf_vmlinux, | |
562 | btf_type_by_id(btf_vmlinux, id)->name_off); | |
563 | } | |
564 | ||
61bd5218 | 565 | static void print_verifier_state(struct bpf_verifier_env *env, |
f4d7e40a | 566 | const struct bpf_func_state *state) |
17a52670 | 567 | { |
f4d7e40a | 568 | const struct bpf_reg_state *reg; |
17a52670 AS |
569 | enum bpf_reg_type t; |
570 | int i; | |
571 | ||
f4d7e40a AS |
572 | if (state->frameno) |
573 | verbose(env, " frame%d:", state->frameno); | |
17a52670 | 574 | for (i = 0; i < MAX_BPF_REG; i++) { |
1a0dc1ac AS |
575 | reg = &state->regs[i]; |
576 | t = reg->type; | |
17a52670 AS |
577 | if (t == NOT_INIT) |
578 | continue; | |
4e92024a AS |
579 | verbose(env, " R%d", i); |
580 | print_liveness(env, reg->live); | |
581 | verbose(env, "=%s", reg_type_str[t]); | |
b5dc0163 AS |
582 | if (t == SCALAR_VALUE && reg->precise) |
583 | verbose(env, "P"); | |
f1174f77 EC |
584 | if ((t == SCALAR_VALUE || t == PTR_TO_STACK) && |
585 | tnum_is_const(reg->var_off)) { | |
586 | /* reg->off should be 0 for SCALAR_VALUE */ | |
61bd5218 | 587 | verbose(env, "%lld", reg->var_off.value + reg->off); |
f1174f77 | 588 | } else { |
eaa6bcb7 HL |
589 | if (t == PTR_TO_BTF_ID || |
590 | t == PTR_TO_BTF_ID_OR_NULL || | |
591 | t == PTR_TO_PERCPU_BTF_ID) | |
9e15db66 | 592 | verbose(env, "%s", kernel_type_name(reg->btf_id)); |
cba368c1 MKL |
593 | verbose(env, "(id=%d", reg->id); |
594 | if (reg_type_may_be_refcounted_or_null(t)) | |
595 | verbose(env, ",ref_obj_id=%d", reg->ref_obj_id); | |
f1174f77 | 596 | if (t != SCALAR_VALUE) |
61bd5218 | 597 | verbose(env, ",off=%d", reg->off); |
de8f3a83 | 598 | if (type_is_pkt_pointer(t)) |
61bd5218 | 599 | verbose(env, ",r=%d", reg->range); |
f1174f77 EC |
600 | else if (t == CONST_PTR_TO_MAP || |
601 | t == PTR_TO_MAP_VALUE || | |
602 | t == PTR_TO_MAP_VALUE_OR_NULL) | |
61bd5218 | 603 | verbose(env, ",ks=%d,vs=%d", |
f1174f77 EC |
604 | reg->map_ptr->key_size, |
605 | reg->map_ptr->value_size); | |
7d1238f2 EC |
606 | if (tnum_is_const(reg->var_off)) { |
607 | /* Typically an immediate SCALAR_VALUE, but | |
608 | * could be a pointer whose offset is too big | |
609 | * for reg->off | |
610 | */ | |
61bd5218 | 611 | verbose(env, ",imm=%llx", reg->var_off.value); |
7d1238f2 EC |
612 | } else { |
613 | if (reg->smin_value != reg->umin_value && | |
614 | reg->smin_value != S64_MIN) | |
61bd5218 | 615 | verbose(env, ",smin_value=%lld", |
7d1238f2 EC |
616 | (long long)reg->smin_value); |
617 | if (reg->smax_value != reg->umax_value && | |
618 | reg->smax_value != S64_MAX) | |
61bd5218 | 619 | verbose(env, ",smax_value=%lld", |
7d1238f2 EC |
620 | (long long)reg->smax_value); |
621 | if (reg->umin_value != 0) | |
61bd5218 | 622 | verbose(env, ",umin_value=%llu", |
7d1238f2 EC |
623 | (unsigned long long)reg->umin_value); |
624 | if (reg->umax_value != U64_MAX) | |
61bd5218 | 625 | verbose(env, ",umax_value=%llu", |
7d1238f2 EC |
626 | (unsigned long long)reg->umax_value); |
627 | if (!tnum_is_unknown(reg->var_off)) { | |
628 | char tn_buf[48]; | |
f1174f77 | 629 | |
7d1238f2 | 630 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
61bd5218 | 631 | verbose(env, ",var_off=%s", tn_buf); |
7d1238f2 | 632 | } |
3f50f132 JF |
633 | if (reg->s32_min_value != reg->smin_value && |
634 | reg->s32_min_value != S32_MIN) | |
635 | verbose(env, ",s32_min_value=%d", | |
636 | (int)(reg->s32_min_value)); | |
637 | if (reg->s32_max_value != reg->smax_value && | |
638 | reg->s32_max_value != S32_MAX) | |
639 | verbose(env, ",s32_max_value=%d", | |
640 | (int)(reg->s32_max_value)); | |
641 | if (reg->u32_min_value != reg->umin_value && | |
642 | reg->u32_min_value != U32_MIN) | |
643 | verbose(env, ",u32_min_value=%d", | |
644 | (int)(reg->u32_min_value)); | |
645 | if (reg->u32_max_value != reg->umax_value && | |
646 | reg->u32_max_value != U32_MAX) | |
647 | verbose(env, ",u32_max_value=%d", | |
648 | (int)(reg->u32_max_value)); | |
f1174f77 | 649 | } |
61bd5218 | 650 | verbose(env, ")"); |
f1174f77 | 651 | } |
17a52670 | 652 | } |
638f5b90 | 653 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
8efea21d EC |
654 | char types_buf[BPF_REG_SIZE + 1]; |
655 | bool valid = false; | |
656 | int j; | |
657 | ||
658 | for (j = 0; j < BPF_REG_SIZE; j++) { | |
659 | if (state->stack[i].slot_type[j] != STACK_INVALID) | |
660 | valid = true; | |
661 | types_buf[j] = slot_type_char[ | |
662 | state->stack[i].slot_type[j]]; | |
663 | } | |
664 | types_buf[BPF_REG_SIZE] = 0; | |
665 | if (!valid) | |
666 | continue; | |
667 | verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE); | |
668 | print_liveness(env, state->stack[i].spilled_ptr.live); | |
b5dc0163 AS |
669 | if (state->stack[i].slot_type[0] == STACK_SPILL) { |
670 | reg = &state->stack[i].spilled_ptr; | |
671 | t = reg->type; | |
672 | verbose(env, "=%s", reg_type_str[t]); | |
673 | if (t == SCALAR_VALUE && reg->precise) | |
674 | verbose(env, "P"); | |
675 | if (t == SCALAR_VALUE && tnum_is_const(reg->var_off)) | |
676 | verbose(env, "%lld", reg->var_off.value + reg->off); | |
677 | } else { | |
8efea21d | 678 | verbose(env, "=%s", types_buf); |
b5dc0163 | 679 | } |
17a52670 | 680 | } |
fd978bf7 JS |
681 | if (state->acquired_refs && state->refs[0].id) { |
682 | verbose(env, " refs=%d", state->refs[0].id); | |
683 | for (i = 1; i < state->acquired_refs; i++) | |
684 | if (state->refs[i].id) | |
685 | verbose(env, ",%d", state->refs[i].id); | |
686 | } | |
61bd5218 | 687 | verbose(env, "\n"); |
17a52670 AS |
688 | } |
689 | ||
84dbf350 JS |
690 | #define COPY_STATE_FN(NAME, COUNT, FIELD, SIZE) \ |
691 | static int copy_##NAME##_state(struct bpf_func_state *dst, \ | |
692 | const struct bpf_func_state *src) \ | |
693 | { \ | |
694 | if (!src->FIELD) \ | |
695 | return 0; \ | |
696 | if (WARN_ON_ONCE(dst->COUNT < src->COUNT)) { \ | |
697 | /* internal bug, make state invalid to reject the program */ \ | |
698 | memset(dst, 0, sizeof(*dst)); \ | |
699 | return -EFAULT; \ | |
700 | } \ | |
701 | memcpy(dst->FIELD, src->FIELD, \ | |
702 | sizeof(*src->FIELD) * (src->COUNT / SIZE)); \ | |
703 | return 0; \ | |
638f5b90 | 704 | } |
fd978bf7 JS |
705 | /* copy_reference_state() */ |
706 | COPY_STATE_FN(reference, acquired_refs, refs, 1) | |
84dbf350 JS |
707 | /* copy_stack_state() */ |
708 | COPY_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE) | |
709 | #undef COPY_STATE_FN | |
710 | ||
711 | #define REALLOC_STATE_FN(NAME, COUNT, FIELD, SIZE) \ | |
712 | static int realloc_##NAME##_state(struct bpf_func_state *state, int size, \ | |
713 | bool copy_old) \ | |
714 | { \ | |
715 | u32 old_size = state->COUNT; \ | |
716 | struct bpf_##NAME##_state *new_##FIELD; \ | |
717 | int slot = size / SIZE; \ | |
718 | \ | |
719 | if (size <= old_size || !size) { \ | |
720 | if (copy_old) \ | |
721 | return 0; \ | |
722 | state->COUNT = slot * SIZE; \ | |
723 | if (!size && old_size) { \ | |
724 | kfree(state->FIELD); \ | |
725 | state->FIELD = NULL; \ | |
726 | } \ | |
727 | return 0; \ | |
728 | } \ | |
729 | new_##FIELD = kmalloc_array(slot, sizeof(struct bpf_##NAME##_state), \ | |
730 | GFP_KERNEL); \ | |
731 | if (!new_##FIELD) \ | |
732 | return -ENOMEM; \ | |
733 | if (copy_old) { \ | |
734 | if (state->FIELD) \ | |
735 | memcpy(new_##FIELD, state->FIELD, \ | |
736 | sizeof(*new_##FIELD) * (old_size / SIZE)); \ | |
737 | memset(new_##FIELD + old_size / SIZE, 0, \ | |
738 | sizeof(*new_##FIELD) * (size - old_size) / SIZE); \ | |
739 | } \ | |
740 | state->COUNT = slot * SIZE; \ | |
741 | kfree(state->FIELD); \ | |
742 | state->FIELD = new_##FIELD; \ | |
743 | return 0; \ | |
744 | } | |
fd978bf7 JS |
745 | /* realloc_reference_state() */ |
746 | REALLOC_STATE_FN(reference, acquired_refs, refs, 1) | |
84dbf350 JS |
747 | /* realloc_stack_state() */ |
748 | REALLOC_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE) | |
749 | #undef REALLOC_STATE_FN | |
638f5b90 AS |
750 | |
751 | /* do_check() starts with zero-sized stack in struct bpf_verifier_state to | |
752 | * make it consume minimal amount of memory. check_stack_write() access from | |
f4d7e40a | 753 | * the program calls into realloc_func_state() to grow the stack size. |
84dbf350 JS |
754 | * Note there is a non-zero 'parent' pointer inside bpf_verifier_state |
755 | * which realloc_stack_state() copies over. It points to previous | |
756 | * bpf_verifier_state which is never reallocated. | |
638f5b90 | 757 | */ |
fd978bf7 JS |
758 | static int realloc_func_state(struct bpf_func_state *state, int stack_size, |
759 | int refs_size, bool copy_old) | |
638f5b90 | 760 | { |
fd978bf7 JS |
761 | int err = realloc_reference_state(state, refs_size, copy_old); |
762 | if (err) | |
763 | return err; | |
764 | return realloc_stack_state(state, stack_size, copy_old); | |
765 | } | |
766 | ||
767 | /* Acquire a pointer id from the env and update the state->refs to include | |
768 | * this new pointer reference. | |
769 | * On success, returns a valid pointer id to associate with the register | |
770 | * On failure, returns a negative errno. | |
638f5b90 | 771 | */ |
fd978bf7 | 772 | static int acquire_reference_state(struct bpf_verifier_env *env, int insn_idx) |
638f5b90 | 773 | { |
fd978bf7 JS |
774 | struct bpf_func_state *state = cur_func(env); |
775 | int new_ofs = state->acquired_refs; | |
776 | int id, err; | |
777 | ||
778 | err = realloc_reference_state(state, state->acquired_refs + 1, true); | |
779 | if (err) | |
780 | return err; | |
781 | id = ++env->id_gen; | |
782 | state->refs[new_ofs].id = id; | |
783 | state->refs[new_ofs].insn_idx = insn_idx; | |
638f5b90 | 784 | |
fd978bf7 JS |
785 | return id; |
786 | } | |
787 | ||
788 | /* release function corresponding to acquire_reference_state(). Idempotent. */ | |
46f8bc92 | 789 | static int release_reference_state(struct bpf_func_state *state, int ptr_id) |
fd978bf7 JS |
790 | { |
791 | int i, last_idx; | |
792 | ||
fd978bf7 JS |
793 | last_idx = state->acquired_refs - 1; |
794 | for (i = 0; i < state->acquired_refs; i++) { | |
795 | if (state->refs[i].id == ptr_id) { | |
796 | if (last_idx && i != last_idx) | |
797 | memcpy(&state->refs[i], &state->refs[last_idx], | |
798 | sizeof(*state->refs)); | |
799 | memset(&state->refs[last_idx], 0, sizeof(*state->refs)); | |
800 | state->acquired_refs--; | |
638f5b90 | 801 | return 0; |
638f5b90 | 802 | } |
638f5b90 | 803 | } |
46f8bc92 | 804 | return -EINVAL; |
fd978bf7 JS |
805 | } |
806 | ||
807 | static int transfer_reference_state(struct bpf_func_state *dst, | |
808 | struct bpf_func_state *src) | |
809 | { | |
810 | int err = realloc_reference_state(dst, src->acquired_refs, false); | |
811 | if (err) | |
812 | return err; | |
813 | err = copy_reference_state(dst, src); | |
814 | if (err) | |
815 | return err; | |
638f5b90 AS |
816 | return 0; |
817 | } | |
818 | ||
f4d7e40a AS |
819 | static void free_func_state(struct bpf_func_state *state) |
820 | { | |
5896351e AS |
821 | if (!state) |
822 | return; | |
fd978bf7 | 823 | kfree(state->refs); |
f4d7e40a AS |
824 | kfree(state->stack); |
825 | kfree(state); | |
826 | } | |
827 | ||
b5dc0163 AS |
828 | static void clear_jmp_history(struct bpf_verifier_state *state) |
829 | { | |
830 | kfree(state->jmp_history); | |
831 | state->jmp_history = NULL; | |
832 | state->jmp_history_cnt = 0; | |
833 | } | |
834 | ||
1969db47 AS |
835 | static void free_verifier_state(struct bpf_verifier_state *state, |
836 | bool free_self) | |
638f5b90 | 837 | { |
f4d7e40a AS |
838 | int i; |
839 | ||
840 | for (i = 0; i <= state->curframe; i++) { | |
841 | free_func_state(state->frame[i]); | |
842 | state->frame[i] = NULL; | |
843 | } | |
b5dc0163 | 844 | clear_jmp_history(state); |
1969db47 AS |
845 | if (free_self) |
846 | kfree(state); | |
638f5b90 AS |
847 | } |
848 | ||
849 | /* copy verifier state from src to dst growing dst stack space | |
850 | * when necessary to accommodate larger src stack | |
851 | */ | |
f4d7e40a AS |
852 | static int copy_func_state(struct bpf_func_state *dst, |
853 | const struct bpf_func_state *src) | |
638f5b90 AS |
854 | { |
855 | int err; | |
856 | ||
fd978bf7 JS |
857 | err = realloc_func_state(dst, src->allocated_stack, src->acquired_refs, |
858 | false); | |
859 | if (err) | |
860 | return err; | |
861 | memcpy(dst, src, offsetof(struct bpf_func_state, acquired_refs)); | |
862 | err = copy_reference_state(dst, src); | |
638f5b90 AS |
863 | if (err) |
864 | return err; | |
638f5b90 AS |
865 | return copy_stack_state(dst, src); |
866 | } | |
867 | ||
f4d7e40a AS |
868 | static int copy_verifier_state(struct bpf_verifier_state *dst_state, |
869 | const struct bpf_verifier_state *src) | |
870 | { | |
871 | struct bpf_func_state *dst; | |
b5dc0163 | 872 | u32 jmp_sz = sizeof(struct bpf_idx_pair) * src->jmp_history_cnt; |
f4d7e40a AS |
873 | int i, err; |
874 | ||
b5dc0163 AS |
875 | if (dst_state->jmp_history_cnt < src->jmp_history_cnt) { |
876 | kfree(dst_state->jmp_history); | |
877 | dst_state->jmp_history = kmalloc(jmp_sz, GFP_USER); | |
878 | if (!dst_state->jmp_history) | |
879 | return -ENOMEM; | |
880 | } | |
881 | memcpy(dst_state->jmp_history, src->jmp_history, jmp_sz); | |
882 | dst_state->jmp_history_cnt = src->jmp_history_cnt; | |
883 | ||
f4d7e40a AS |
884 | /* if dst has more stack frames then src frame, free them */ |
885 | for (i = src->curframe + 1; i <= dst_state->curframe; i++) { | |
886 | free_func_state(dst_state->frame[i]); | |
887 | dst_state->frame[i] = NULL; | |
888 | } | |
979d63d5 | 889 | dst_state->speculative = src->speculative; |
f4d7e40a | 890 | dst_state->curframe = src->curframe; |
d83525ca | 891 | dst_state->active_spin_lock = src->active_spin_lock; |
2589726d AS |
892 | dst_state->branches = src->branches; |
893 | dst_state->parent = src->parent; | |
b5dc0163 AS |
894 | dst_state->first_insn_idx = src->first_insn_idx; |
895 | dst_state->last_insn_idx = src->last_insn_idx; | |
f4d7e40a AS |
896 | for (i = 0; i <= src->curframe; i++) { |
897 | dst = dst_state->frame[i]; | |
898 | if (!dst) { | |
899 | dst = kzalloc(sizeof(*dst), GFP_KERNEL); | |
900 | if (!dst) | |
901 | return -ENOMEM; | |
902 | dst_state->frame[i] = dst; | |
903 | } | |
904 | err = copy_func_state(dst, src->frame[i]); | |
905 | if (err) | |
906 | return err; | |
907 | } | |
908 | return 0; | |
909 | } | |
910 | ||
2589726d AS |
911 | static void update_branch_counts(struct bpf_verifier_env *env, struct bpf_verifier_state *st) |
912 | { | |
913 | while (st) { | |
914 | u32 br = --st->branches; | |
915 | ||
916 | /* WARN_ON(br > 1) technically makes sense here, | |
917 | * but see comment in push_stack(), hence: | |
918 | */ | |
919 | WARN_ONCE((int)br < 0, | |
920 | "BUG update_branch_counts:branches_to_explore=%d\n", | |
921 | br); | |
922 | if (br) | |
923 | break; | |
924 | st = st->parent; | |
925 | } | |
926 | } | |
927 | ||
638f5b90 | 928 | static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx, |
6f8a57cc | 929 | int *insn_idx, bool pop_log) |
638f5b90 AS |
930 | { |
931 | struct bpf_verifier_state *cur = env->cur_state; | |
932 | struct bpf_verifier_stack_elem *elem, *head = env->head; | |
933 | int err; | |
17a52670 AS |
934 | |
935 | if (env->head == NULL) | |
638f5b90 | 936 | return -ENOENT; |
17a52670 | 937 | |
638f5b90 AS |
938 | if (cur) { |
939 | err = copy_verifier_state(cur, &head->st); | |
940 | if (err) | |
941 | return err; | |
942 | } | |
6f8a57cc AN |
943 | if (pop_log) |
944 | bpf_vlog_reset(&env->log, head->log_pos); | |
638f5b90 AS |
945 | if (insn_idx) |
946 | *insn_idx = head->insn_idx; | |
17a52670 | 947 | if (prev_insn_idx) |
638f5b90 AS |
948 | *prev_insn_idx = head->prev_insn_idx; |
949 | elem = head->next; | |
1969db47 | 950 | free_verifier_state(&head->st, false); |
638f5b90 | 951 | kfree(head); |
17a52670 AS |
952 | env->head = elem; |
953 | env->stack_size--; | |
638f5b90 | 954 | return 0; |
17a52670 AS |
955 | } |
956 | ||
58e2af8b | 957 | static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, |
979d63d5 DB |
958 | int insn_idx, int prev_insn_idx, |
959 | bool speculative) | |
17a52670 | 960 | { |
638f5b90 | 961 | struct bpf_verifier_state *cur = env->cur_state; |
58e2af8b | 962 | struct bpf_verifier_stack_elem *elem; |
638f5b90 | 963 | int err; |
17a52670 | 964 | |
638f5b90 | 965 | elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL); |
17a52670 AS |
966 | if (!elem) |
967 | goto err; | |
968 | ||
17a52670 AS |
969 | elem->insn_idx = insn_idx; |
970 | elem->prev_insn_idx = prev_insn_idx; | |
971 | elem->next = env->head; | |
6f8a57cc | 972 | elem->log_pos = env->log.len_used; |
17a52670 AS |
973 | env->head = elem; |
974 | env->stack_size++; | |
1969db47 AS |
975 | err = copy_verifier_state(&elem->st, cur); |
976 | if (err) | |
977 | goto err; | |
979d63d5 | 978 | elem->st.speculative |= speculative; |
b285fcb7 AS |
979 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_JMP_SEQ) { |
980 | verbose(env, "The sequence of %d jumps is too complex.\n", | |
981 | env->stack_size); | |
17a52670 AS |
982 | goto err; |
983 | } | |
2589726d AS |
984 | if (elem->st.parent) { |
985 | ++elem->st.parent->branches; | |
986 | /* WARN_ON(branches > 2) technically makes sense here, | |
987 | * but | |
988 | * 1. speculative states will bump 'branches' for non-branch | |
989 | * instructions | |
990 | * 2. is_state_visited() heuristics may decide not to create | |
991 | * a new state for a sequence of branches and all such current | |
992 | * and cloned states will be pointing to a single parent state | |
993 | * which might have large 'branches' count. | |
994 | */ | |
995 | } | |
17a52670 AS |
996 | return &elem->st; |
997 | err: | |
5896351e AS |
998 | free_verifier_state(env->cur_state, true); |
999 | env->cur_state = NULL; | |
17a52670 | 1000 | /* pop all elements and return */ |
6f8a57cc | 1001 | while (!pop_stack(env, NULL, NULL, false)); |
17a52670 AS |
1002 | return NULL; |
1003 | } | |
1004 | ||
1005 | #define CALLER_SAVED_REGS 6 | |
1006 | static const int caller_saved[CALLER_SAVED_REGS] = { | |
1007 | BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5 | |
1008 | }; | |
1009 | ||
f54c7898 DB |
1010 | static void __mark_reg_not_init(const struct bpf_verifier_env *env, |
1011 | struct bpf_reg_state *reg); | |
f1174f77 | 1012 | |
b03c9f9f EC |
1013 | /* Mark the unknown part of a register (variable offset or scalar value) as |
1014 | * known to have the value @imm. | |
1015 | */ | |
1016 | static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm) | |
1017 | { | |
a9c676bc AS |
1018 | /* Clear id, off, and union(map_ptr, range) */ |
1019 | memset(((u8 *)reg) + sizeof(reg->type), 0, | |
1020 | offsetof(struct bpf_reg_state, var_off) - sizeof(reg->type)); | |
b03c9f9f EC |
1021 | reg->var_off = tnum_const(imm); |
1022 | reg->smin_value = (s64)imm; | |
1023 | reg->smax_value = (s64)imm; | |
1024 | reg->umin_value = imm; | |
1025 | reg->umax_value = imm; | |
3f50f132 JF |
1026 | |
1027 | reg->s32_min_value = (s32)imm; | |
1028 | reg->s32_max_value = (s32)imm; | |
1029 | reg->u32_min_value = (u32)imm; | |
1030 | reg->u32_max_value = (u32)imm; | |
1031 | } | |
1032 | ||
1033 | static void __mark_reg32_known(struct bpf_reg_state *reg, u64 imm) | |
1034 | { | |
1035 | reg->var_off = tnum_const_subreg(reg->var_off, imm); | |
1036 | reg->s32_min_value = (s32)imm; | |
1037 | reg->s32_max_value = (s32)imm; | |
1038 | reg->u32_min_value = (u32)imm; | |
1039 | reg->u32_max_value = (u32)imm; | |
b03c9f9f EC |
1040 | } |
1041 | ||
f1174f77 EC |
1042 | /* Mark the 'variable offset' part of a register as zero. This should be |
1043 | * used only on registers holding a pointer type. | |
1044 | */ | |
1045 | static void __mark_reg_known_zero(struct bpf_reg_state *reg) | |
a9789ef9 | 1046 | { |
b03c9f9f | 1047 | __mark_reg_known(reg, 0); |
f1174f77 | 1048 | } |
a9789ef9 | 1049 | |
cc2b14d5 AS |
1050 | static void __mark_reg_const_zero(struct bpf_reg_state *reg) |
1051 | { | |
1052 | __mark_reg_known(reg, 0); | |
cc2b14d5 AS |
1053 | reg->type = SCALAR_VALUE; |
1054 | } | |
1055 | ||
61bd5218 JK |
1056 | static void mark_reg_known_zero(struct bpf_verifier_env *env, |
1057 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
1058 | { |
1059 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 1060 | verbose(env, "mark_reg_known_zero(regs, %u)\n", regno); |
f1174f77 EC |
1061 | /* Something bad happened, let's kill all regs */ |
1062 | for (regno = 0; regno < MAX_BPF_REG; regno++) | |
f54c7898 | 1063 | __mark_reg_not_init(env, regs + regno); |
f1174f77 EC |
1064 | return; |
1065 | } | |
1066 | __mark_reg_known_zero(regs + regno); | |
1067 | } | |
1068 | ||
de8f3a83 DB |
1069 | static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg) |
1070 | { | |
1071 | return type_is_pkt_pointer(reg->type); | |
1072 | } | |
1073 | ||
1074 | static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg) | |
1075 | { | |
1076 | return reg_is_pkt_pointer(reg) || | |
1077 | reg->type == PTR_TO_PACKET_END; | |
1078 | } | |
1079 | ||
1080 | /* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */ | |
1081 | static bool reg_is_init_pkt_pointer(const struct bpf_reg_state *reg, | |
1082 | enum bpf_reg_type which) | |
1083 | { | |
1084 | /* The register can already have a range from prior markings. | |
1085 | * This is fine as long as it hasn't been advanced from its | |
1086 | * origin. | |
1087 | */ | |
1088 | return reg->type == which && | |
1089 | reg->id == 0 && | |
1090 | reg->off == 0 && | |
1091 | tnum_equals_const(reg->var_off, 0); | |
1092 | } | |
1093 | ||
3f50f132 JF |
1094 | /* Reset the min/max bounds of a register */ |
1095 | static void __mark_reg_unbounded(struct bpf_reg_state *reg) | |
1096 | { | |
1097 | reg->smin_value = S64_MIN; | |
1098 | reg->smax_value = S64_MAX; | |
1099 | reg->umin_value = 0; | |
1100 | reg->umax_value = U64_MAX; | |
1101 | ||
1102 | reg->s32_min_value = S32_MIN; | |
1103 | reg->s32_max_value = S32_MAX; | |
1104 | reg->u32_min_value = 0; | |
1105 | reg->u32_max_value = U32_MAX; | |
1106 | } | |
1107 | ||
1108 | static void __mark_reg64_unbounded(struct bpf_reg_state *reg) | |
1109 | { | |
1110 | reg->smin_value = S64_MIN; | |
1111 | reg->smax_value = S64_MAX; | |
1112 | reg->umin_value = 0; | |
1113 | reg->umax_value = U64_MAX; | |
1114 | } | |
1115 | ||
1116 | static void __mark_reg32_unbounded(struct bpf_reg_state *reg) | |
1117 | { | |
1118 | reg->s32_min_value = S32_MIN; | |
1119 | reg->s32_max_value = S32_MAX; | |
1120 | reg->u32_min_value = 0; | |
1121 | reg->u32_max_value = U32_MAX; | |
1122 | } | |
1123 | ||
1124 | static void __update_reg32_bounds(struct bpf_reg_state *reg) | |
1125 | { | |
1126 | struct tnum var32_off = tnum_subreg(reg->var_off); | |
1127 | ||
1128 | /* min signed is max(sign bit) | min(other bits) */ | |
1129 | reg->s32_min_value = max_t(s32, reg->s32_min_value, | |
1130 | var32_off.value | (var32_off.mask & S32_MIN)); | |
1131 | /* max signed is min(sign bit) | max(other bits) */ | |
1132 | reg->s32_max_value = min_t(s32, reg->s32_max_value, | |
1133 | var32_off.value | (var32_off.mask & S32_MAX)); | |
1134 | reg->u32_min_value = max_t(u32, reg->u32_min_value, (u32)var32_off.value); | |
1135 | reg->u32_max_value = min(reg->u32_max_value, | |
1136 | (u32)(var32_off.value | var32_off.mask)); | |
1137 | } | |
1138 | ||
1139 | static void __update_reg64_bounds(struct bpf_reg_state *reg) | |
b03c9f9f EC |
1140 | { |
1141 | /* min signed is max(sign bit) | min(other bits) */ | |
1142 | reg->smin_value = max_t(s64, reg->smin_value, | |
1143 | reg->var_off.value | (reg->var_off.mask & S64_MIN)); | |
1144 | /* max signed is min(sign bit) | max(other bits) */ | |
1145 | reg->smax_value = min_t(s64, reg->smax_value, | |
1146 | reg->var_off.value | (reg->var_off.mask & S64_MAX)); | |
1147 | reg->umin_value = max(reg->umin_value, reg->var_off.value); | |
1148 | reg->umax_value = min(reg->umax_value, | |
1149 | reg->var_off.value | reg->var_off.mask); | |
1150 | } | |
1151 | ||
3f50f132 JF |
1152 | static void __update_reg_bounds(struct bpf_reg_state *reg) |
1153 | { | |
1154 | __update_reg32_bounds(reg); | |
1155 | __update_reg64_bounds(reg); | |
1156 | } | |
1157 | ||
b03c9f9f | 1158 | /* Uses signed min/max values to inform unsigned, and vice-versa */ |
3f50f132 JF |
1159 | static void __reg32_deduce_bounds(struct bpf_reg_state *reg) |
1160 | { | |
1161 | /* Learn sign from signed bounds. | |
1162 | * If we cannot cross the sign boundary, then signed and unsigned bounds | |
1163 | * are the same, so combine. This works even in the negative case, e.g. | |
1164 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. | |
1165 | */ | |
1166 | if (reg->s32_min_value >= 0 || reg->s32_max_value < 0) { | |
1167 | reg->s32_min_value = reg->u32_min_value = | |
1168 | max_t(u32, reg->s32_min_value, reg->u32_min_value); | |
1169 | reg->s32_max_value = reg->u32_max_value = | |
1170 | min_t(u32, reg->s32_max_value, reg->u32_max_value); | |
1171 | return; | |
1172 | } | |
1173 | /* Learn sign from unsigned bounds. Signed bounds cross the sign | |
1174 | * boundary, so we must be careful. | |
1175 | */ | |
1176 | if ((s32)reg->u32_max_value >= 0) { | |
1177 | /* Positive. We can't learn anything from the smin, but smax | |
1178 | * is positive, hence safe. | |
1179 | */ | |
1180 | reg->s32_min_value = reg->u32_min_value; | |
1181 | reg->s32_max_value = reg->u32_max_value = | |
1182 | min_t(u32, reg->s32_max_value, reg->u32_max_value); | |
1183 | } else if ((s32)reg->u32_min_value < 0) { | |
1184 | /* Negative. We can't learn anything from the smax, but smin | |
1185 | * is negative, hence safe. | |
1186 | */ | |
1187 | reg->s32_min_value = reg->u32_min_value = | |
1188 | max_t(u32, reg->s32_min_value, reg->u32_min_value); | |
1189 | reg->s32_max_value = reg->u32_max_value; | |
1190 | } | |
1191 | } | |
1192 | ||
1193 | static void __reg64_deduce_bounds(struct bpf_reg_state *reg) | |
b03c9f9f EC |
1194 | { |
1195 | /* Learn sign from signed bounds. | |
1196 | * If we cannot cross the sign boundary, then signed and unsigned bounds | |
1197 | * are the same, so combine. This works even in the negative case, e.g. | |
1198 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. | |
1199 | */ | |
1200 | if (reg->smin_value >= 0 || reg->smax_value < 0) { | |
1201 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, | |
1202 | reg->umin_value); | |
1203 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, | |
1204 | reg->umax_value); | |
1205 | return; | |
1206 | } | |
1207 | /* Learn sign from unsigned bounds. Signed bounds cross the sign | |
1208 | * boundary, so we must be careful. | |
1209 | */ | |
1210 | if ((s64)reg->umax_value >= 0) { | |
1211 | /* Positive. We can't learn anything from the smin, but smax | |
1212 | * is positive, hence safe. | |
1213 | */ | |
1214 | reg->smin_value = reg->umin_value; | |
1215 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, | |
1216 | reg->umax_value); | |
1217 | } else if ((s64)reg->umin_value < 0) { | |
1218 | /* Negative. We can't learn anything from the smax, but smin | |
1219 | * is negative, hence safe. | |
1220 | */ | |
1221 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, | |
1222 | reg->umin_value); | |
1223 | reg->smax_value = reg->umax_value; | |
1224 | } | |
1225 | } | |
1226 | ||
3f50f132 JF |
1227 | static void __reg_deduce_bounds(struct bpf_reg_state *reg) |
1228 | { | |
1229 | __reg32_deduce_bounds(reg); | |
1230 | __reg64_deduce_bounds(reg); | |
1231 | } | |
1232 | ||
b03c9f9f EC |
1233 | /* Attempts to improve var_off based on unsigned min/max information */ |
1234 | static void __reg_bound_offset(struct bpf_reg_state *reg) | |
1235 | { | |
3f50f132 JF |
1236 | struct tnum var64_off = tnum_intersect(reg->var_off, |
1237 | tnum_range(reg->umin_value, | |
1238 | reg->umax_value)); | |
1239 | struct tnum var32_off = tnum_intersect(tnum_subreg(reg->var_off), | |
1240 | tnum_range(reg->u32_min_value, | |
1241 | reg->u32_max_value)); | |
1242 | ||
1243 | reg->var_off = tnum_or(tnum_clear_subreg(var64_off), var32_off); | |
b03c9f9f EC |
1244 | } |
1245 | ||
3f50f132 | 1246 | static void __reg_assign_32_into_64(struct bpf_reg_state *reg) |
b03c9f9f | 1247 | { |
3f50f132 JF |
1248 | reg->umin_value = reg->u32_min_value; |
1249 | reg->umax_value = reg->u32_max_value; | |
1250 | /* Attempt to pull 32-bit signed bounds into 64-bit bounds | |
1251 | * but must be positive otherwise set to worse case bounds | |
1252 | * and refine later from tnum. | |
1253 | */ | |
3a71dc36 | 1254 | if (reg->s32_min_value >= 0 && reg->s32_max_value >= 0) |
3f50f132 JF |
1255 | reg->smax_value = reg->s32_max_value; |
1256 | else | |
1257 | reg->smax_value = U32_MAX; | |
3a71dc36 JF |
1258 | if (reg->s32_min_value >= 0) |
1259 | reg->smin_value = reg->s32_min_value; | |
1260 | else | |
1261 | reg->smin_value = 0; | |
3f50f132 JF |
1262 | } |
1263 | ||
1264 | static void __reg_combine_32_into_64(struct bpf_reg_state *reg) | |
1265 | { | |
1266 | /* special case when 64-bit register has upper 32-bit register | |
1267 | * zeroed. Typically happens after zext or <<32, >>32 sequence | |
1268 | * allowing us to use 32-bit bounds directly, | |
1269 | */ | |
1270 | if (tnum_equals_const(tnum_clear_subreg(reg->var_off), 0)) { | |
1271 | __reg_assign_32_into_64(reg); | |
1272 | } else { | |
1273 | /* Otherwise the best we can do is push lower 32bit known and | |
1274 | * unknown bits into register (var_off set from jmp logic) | |
1275 | * then learn as much as possible from the 64-bit tnum | |
1276 | * known and unknown bits. The previous smin/smax bounds are | |
1277 | * invalid here because of jmp32 compare so mark them unknown | |
1278 | * so they do not impact tnum bounds calculation. | |
1279 | */ | |
1280 | __mark_reg64_unbounded(reg); | |
1281 | __update_reg_bounds(reg); | |
1282 | } | |
1283 | ||
1284 | /* Intersecting with the old var_off might have improved our bounds | |
1285 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), | |
1286 | * then new var_off is (0; 0x7f...fc) which improves our umax. | |
1287 | */ | |
1288 | __reg_deduce_bounds(reg); | |
1289 | __reg_bound_offset(reg); | |
1290 | __update_reg_bounds(reg); | |
1291 | } | |
1292 | ||
1293 | static bool __reg64_bound_s32(s64 a) | |
1294 | { | |
1295 | if (a > S32_MIN && a < S32_MAX) | |
1296 | return true; | |
1297 | return false; | |
1298 | } | |
1299 | ||
1300 | static bool __reg64_bound_u32(u64 a) | |
1301 | { | |
1302 | if (a > U32_MIN && a < U32_MAX) | |
1303 | return true; | |
1304 | return false; | |
1305 | } | |
1306 | ||
1307 | static void __reg_combine_64_into_32(struct bpf_reg_state *reg) | |
1308 | { | |
1309 | __mark_reg32_unbounded(reg); | |
1310 | ||
1311 | if (__reg64_bound_s32(reg->smin_value)) | |
1312 | reg->s32_min_value = (s32)reg->smin_value; | |
1313 | if (__reg64_bound_s32(reg->smax_value)) | |
1314 | reg->s32_max_value = (s32)reg->smax_value; | |
1315 | if (__reg64_bound_u32(reg->umin_value)) | |
1316 | reg->u32_min_value = (u32)reg->umin_value; | |
1317 | if (__reg64_bound_u32(reg->umax_value)) | |
1318 | reg->u32_max_value = (u32)reg->umax_value; | |
1319 | ||
1320 | /* Intersecting with the old var_off might have improved our bounds | |
1321 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), | |
1322 | * then new var_off is (0; 0x7f...fc) which improves our umax. | |
1323 | */ | |
1324 | __reg_deduce_bounds(reg); | |
1325 | __reg_bound_offset(reg); | |
1326 | __update_reg_bounds(reg); | |
b03c9f9f EC |
1327 | } |
1328 | ||
f1174f77 | 1329 | /* Mark a register as having a completely unknown (scalar) value. */ |
f54c7898 DB |
1330 | static void __mark_reg_unknown(const struct bpf_verifier_env *env, |
1331 | struct bpf_reg_state *reg) | |
f1174f77 | 1332 | { |
a9c676bc AS |
1333 | /* |
1334 | * Clear type, id, off, and union(map_ptr, range) and | |
1335 | * padding between 'type' and union | |
1336 | */ | |
1337 | memset(reg, 0, offsetof(struct bpf_reg_state, var_off)); | |
f1174f77 | 1338 | reg->type = SCALAR_VALUE; |
f1174f77 | 1339 | reg->var_off = tnum_unknown; |
f4d7e40a | 1340 | reg->frameno = 0; |
2c78ee89 | 1341 | reg->precise = env->subprog_cnt > 1 || !env->bpf_capable; |
b03c9f9f | 1342 | __mark_reg_unbounded(reg); |
f1174f77 EC |
1343 | } |
1344 | ||
61bd5218 JK |
1345 | static void mark_reg_unknown(struct bpf_verifier_env *env, |
1346 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
1347 | { |
1348 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 1349 | verbose(env, "mark_reg_unknown(regs, %u)\n", regno); |
19ceb417 AS |
1350 | /* Something bad happened, let's kill all regs except FP */ |
1351 | for (regno = 0; regno < BPF_REG_FP; regno++) | |
f54c7898 | 1352 | __mark_reg_not_init(env, regs + regno); |
f1174f77 EC |
1353 | return; |
1354 | } | |
f54c7898 | 1355 | __mark_reg_unknown(env, regs + regno); |
f1174f77 EC |
1356 | } |
1357 | ||
f54c7898 DB |
1358 | static void __mark_reg_not_init(const struct bpf_verifier_env *env, |
1359 | struct bpf_reg_state *reg) | |
f1174f77 | 1360 | { |
f54c7898 | 1361 | __mark_reg_unknown(env, reg); |
f1174f77 EC |
1362 | reg->type = NOT_INIT; |
1363 | } | |
1364 | ||
61bd5218 JK |
1365 | static void mark_reg_not_init(struct bpf_verifier_env *env, |
1366 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
1367 | { |
1368 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 1369 | verbose(env, "mark_reg_not_init(regs, %u)\n", regno); |
19ceb417 AS |
1370 | /* Something bad happened, let's kill all regs except FP */ |
1371 | for (regno = 0; regno < BPF_REG_FP; regno++) | |
f54c7898 | 1372 | __mark_reg_not_init(env, regs + regno); |
f1174f77 EC |
1373 | return; |
1374 | } | |
f54c7898 | 1375 | __mark_reg_not_init(env, regs + regno); |
a9789ef9 DB |
1376 | } |
1377 | ||
41c48f3a AI |
1378 | static void mark_btf_ld_reg(struct bpf_verifier_env *env, |
1379 | struct bpf_reg_state *regs, u32 regno, | |
1380 | enum bpf_reg_type reg_type, u32 btf_id) | |
1381 | { | |
1382 | if (reg_type == SCALAR_VALUE) { | |
1383 | mark_reg_unknown(env, regs, regno); | |
1384 | return; | |
1385 | } | |
1386 | mark_reg_known_zero(env, regs, regno); | |
1387 | regs[regno].type = PTR_TO_BTF_ID; | |
1388 | regs[regno].btf_id = btf_id; | |
1389 | } | |
1390 | ||
5327ed3d | 1391 | #define DEF_NOT_SUBREG (0) |
61bd5218 | 1392 | static void init_reg_state(struct bpf_verifier_env *env, |
f4d7e40a | 1393 | struct bpf_func_state *state) |
17a52670 | 1394 | { |
f4d7e40a | 1395 | struct bpf_reg_state *regs = state->regs; |
17a52670 AS |
1396 | int i; |
1397 | ||
dc503a8a | 1398 | for (i = 0; i < MAX_BPF_REG; i++) { |
61bd5218 | 1399 | mark_reg_not_init(env, regs, i); |
dc503a8a | 1400 | regs[i].live = REG_LIVE_NONE; |
679c782d | 1401 | regs[i].parent = NULL; |
5327ed3d | 1402 | regs[i].subreg_def = DEF_NOT_SUBREG; |
dc503a8a | 1403 | } |
17a52670 AS |
1404 | |
1405 | /* frame pointer */ | |
f1174f77 | 1406 | regs[BPF_REG_FP].type = PTR_TO_STACK; |
61bd5218 | 1407 | mark_reg_known_zero(env, regs, BPF_REG_FP); |
f4d7e40a | 1408 | regs[BPF_REG_FP].frameno = state->frameno; |
6760bf2d DB |
1409 | } |
1410 | ||
f4d7e40a AS |
1411 | #define BPF_MAIN_FUNC (-1) |
1412 | static void init_func_state(struct bpf_verifier_env *env, | |
1413 | struct bpf_func_state *state, | |
1414 | int callsite, int frameno, int subprogno) | |
1415 | { | |
1416 | state->callsite = callsite; | |
1417 | state->frameno = frameno; | |
1418 | state->subprogno = subprogno; | |
1419 | init_reg_state(env, state); | |
1420 | } | |
1421 | ||
17a52670 AS |
1422 | enum reg_arg_type { |
1423 | SRC_OP, /* register is used as source operand */ | |
1424 | DST_OP, /* register is used as destination operand */ | |
1425 | DST_OP_NO_MARK /* same as above, check only, don't mark */ | |
1426 | }; | |
1427 | ||
cc8b0b92 AS |
1428 | static int cmp_subprogs(const void *a, const void *b) |
1429 | { | |
9c8105bd JW |
1430 | return ((struct bpf_subprog_info *)a)->start - |
1431 | ((struct bpf_subprog_info *)b)->start; | |
cc8b0b92 AS |
1432 | } |
1433 | ||
1434 | static int find_subprog(struct bpf_verifier_env *env, int off) | |
1435 | { | |
9c8105bd | 1436 | struct bpf_subprog_info *p; |
cc8b0b92 | 1437 | |
9c8105bd JW |
1438 | p = bsearch(&off, env->subprog_info, env->subprog_cnt, |
1439 | sizeof(env->subprog_info[0]), cmp_subprogs); | |
cc8b0b92 AS |
1440 | if (!p) |
1441 | return -ENOENT; | |
9c8105bd | 1442 | return p - env->subprog_info; |
cc8b0b92 AS |
1443 | |
1444 | } | |
1445 | ||
1446 | static int add_subprog(struct bpf_verifier_env *env, int off) | |
1447 | { | |
1448 | int insn_cnt = env->prog->len; | |
1449 | int ret; | |
1450 | ||
1451 | if (off >= insn_cnt || off < 0) { | |
1452 | verbose(env, "call to invalid destination\n"); | |
1453 | return -EINVAL; | |
1454 | } | |
1455 | ret = find_subprog(env, off); | |
1456 | if (ret >= 0) | |
1457 | return 0; | |
4cb3d99c | 1458 | if (env->subprog_cnt >= BPF_MAX_SUBPROGS) { |
cc8b0b92 AS |
1459 | verbose(env, "too many subprograms\n"); |
1460 | return -E2BIG; | |
1461 | } | |
9c8105bd JW |
1462 | env->subprog_info[env->subprog_cnt++].start = off; |
1463 | sort(env->subprog_info, env->subprog_cnt, | |
1464 | sizeof(env->subprog_info[0]), cmp_subprogs, NULL); | |
cc8b0b92 AS |
1465 | return 0; |
1466 | } | |
1467 | ||
1468 | static int check_subprogs(struct bpf_verifier_env *env) | |
1469 | { | |
1470 | int i, ret, subprog_start, subprog_end, off, cur_subprog = 0; | |
9c8105bd | 1471 | struct bpf_subprog_info *subprog = env->subprog_info; |
cc8b0b92 AS |
1472 | struct bpf_insn *insn = env->prog->insnsi; |
1473 | int insn_cnt = env->prog->len; | |
1474 | ||
f910cefa JW |
1475 | /* Add entry function. */ |
1476 | ret = add_subprog(env, 0); | |
1477 | if (ret < 0) | |
1478 | return ret; | |
1479 | ||
cc8b0b92 AS |
1480 | /* determine subprog starts. The end is one before the next starts */ |
1481 | for (i = 0; i < insn_cnt; i++) { | |
1482 | if (insn[i].code != (BPF_JMP | BPF_CALL)) | |
1483 | continue; | |
1484 | if (insn[i].src_reg != BPF_PSEUDO_CALL) | |
1485 | continue; | |
2c78ee89 AS |
1486 | if (!env->bpf_capable) { |
1487 | verbose(env, | |
1488 | "function calls to other bpf functions are allowed for CAP_BPF and CAP_SYS_ADMIN\n"); | |
cc8b0b92 AS |
1489 | return -EPERM; |
1490 | } | |
cc8b0b92 AS |
1491 | ret = add_subprog(env, i + insn[i].imm + 1); |
1492 | if (ret < 0) | |
1493 | return ret; | |
1494 | } | |
1495 | ||
4cb3d99c JW |
1496 | /* Add a fake 'exit' subprog which could simplify subprog iteration |
1497 | * logic. 'subprog_cnt' should not be increased. | |
1498 | */ | |
1499 | subprog[env->subprog_cnt].start = insn_cnt; | |
1500 | ||
06ee7115 | 1501 | if (env->log.level & BPF_LOG_LEVEL2) |
cc8b0b92 | 1502 | for (i = 0; i < env->subprog_cnt; i++) |
9c8105bd | 1503 | verbose(env, "func#%d @%d\n", i, subprog[i].start); |
cc8b0b92 AS |
1504 | |
1505 | /* now check that all jumps are within the same subprog */ | |
4cb3d99c JW |
1506 | subprog_start = subprog[cur_subprog].start; |
1507 | subprog_end = subprog[cur_subprog + 1].start; | |
cc8b0b92 AS |
1508 | for (i = 0; i < insn_cnt; i++) { |
1509 | u8 code = insn[i].code; | |
1510 | ||
7f6e4312 MF |
1511 | if (code == (BPF_JMP | BPF_CALL) && |
1512 | insn[i].imm == BPF_FUNC_tail_call && | |
1513 | insn[i].src_reg != BPF_PSEUDO_CALL) | |
1514 | subprog[cur_subprog].has_tail_call = true; | |
09b28d76 AS |
1515 | if (BPF_CLASS(code) == BPF_LD && |
1516 | (BPF_MODE(code) == BPF_ABS || BPF_MODE(code) == BPF_IND)) | |
1517 | subprog[cur_subprog].has_ld_abs = true; | |
092ed096 | 1518 | if (BPF_CLASS(code) != BPF_JMP && BPF_CLASS(code) != BPF_JMP32) |
cc8b0b92 AS |
1519 | goto next; |
1520 | if (BPF_OP(code) == BPF_EXIT || BPF_OP(code) == BPF_CALL) | |
1521 | goto next; | |
1522 | off = i + insn[i].off + 1; | |
1523 | if (off < subprog_start || off >= subprog_end) { | |
1524 | verbose(env, "jump out of range from insn %d to %d\n", i, off); | |
1525 | return -EINVAL; | |
1526 | } | |
1527 | next: | |
1528 | if (i == subprog_end - 1) { | |
1529 | /* to avoid fall-through from one subprog into another | |
1530 | * the last insn of the subprog should be either exit | |
1531 | * or unconditional jump back | |
1532 | */ | |
1533 | if (code != (BPF_JMP | BPF_EXIT) && | |
1534 | code != (BPF_JMP | BPF_JA)) { | |
1535 | verbose(env, "last insn is not an exit or jmp\n"); | |
1536 | return -EINVAL; | |
1537 | } | |
1538 | subprog_start = subprog_end; | |
4cb3d99c JW |
1539 | cur_subprog++; |
1540 | if (cur_subprog < env->subprog_cnt) | |
9c8105bd | 1541 | subprog_end = subprog[cur_subprog + 1].start; |
cc8b0b92 AS |
1542 | } |
1543 | } | |
1544 | return 0; | |
1545 | } | |
1546 | ||
679c782d EC |
1547 | /* Parentage chain of this register (or stack slot) should take care of all |
1548 | * issues like callee-saved registers, stack slot allocation time, etc. | |
1549 | */ | |
f4d7e40a | 1550 | static int mark_reg_read(struct bpf_verifier_env *env, |
679c782d | 1551 | const struct bpf_reg_state *state, |
5327ed3d | 1552 | struct bpf_reg_state *parent, u8 flag) |
f4d7e40a AS |
1553 | { |
1554 | bool writes = parent == state->parent; /* Observe write marks */ | |
06ee7115 | 1555 | int cnt = 0; |
dc503a8a EC |
1556 | |
1557 | while (parent) { | |
1558 | /* if read wasn't screened by an earlier write ... */ | |
679c782d | 1559 | if (writes && state->live & REG_LIVE_WRITTEN) |
dc503a8a | 1560 | break; |
9242b5f5 AS |
1561 | if (parent->live & REG_LIVE_DONE) { |
1562 | verbose(env, "verifier BUG type %s var_off %lld off %d\n", | |
1563 | reg_type_str[parent->type], | |
1564 | parent->var_off.value, parent->off); | |
1565 | return -EFAULT; | |
1566 | } | |
5327ed3d JW |
1567 | /* The first condition is more likely to be true than the |
1568 | * second, checked it first. | |
1569 | */ | |
1570 | if ((parent->live & REG_LIVE_READ) == flag || | |
1571 | parent->live & REG_LIVE_READ64) | |
25af32da AS |
1572 | /* The parentage chain never changes and |
1573 | * this parent was already marked as LIVE_READ. | |
1574 | * There is no need to keep walking the chain again and | |
1575 | * keep re-marking all parents as LIVE_READ. | |
1576 | * This case happens when the same register is read | |
1577 | * multiple times without writes into it in-between. | |
5327ed3d JW |
1578 | * Also, if parent has the stronger REG_LIVE_READ64 set, |
1579 | * then no need to set the weak REG_LIVE_READ32. | |
25af32da AS |
1580 | */ |
1581 | break; | |
dc503a8a | 1582 | /* ... then we depend on parent's value */ |
5327ed3d JW |
1583 | parent->live |= flag; |
1584 | /* REG_LIVE_READ64 overrides REG_LIVE_READ32. */ | |
1585 | if (flag == REG_LIVE_READ64) | |
1586 | parent->live &= ~REG_LIVE_READ32; | |
dc503a8a EC |
1587 | state = parent; |
1588 | parent = state->parent; | |
f4d7e40a | 1589 | writes = true; |
06ee7115 | 1590 | cnt++; |
dc503a8a | 1591 | } |
06ee7115 AS |
1592 | |
1593 | if (env->longest_mark_read_walk < cnt) | |
1594 | env->longest_mark_read_walk = cnt; | |
f4d7e40a | 1595 | return 0; |
dc503a8a EC |
1596 | } |
1597 | ||
5327ed3d JW |
1598 | /* This function is supposed to be used by the following 32-bit optimization |
1599 | * code only. It returns TRUE if the source or destination register operates | |
1600 | * on 64-bit, otherwise return FALSE. | |
1601 | */ | |
1602 | static bool is_reg64(struct bpf_verifier_env *env, struct bpf_insn *insn, | |
1603 | u32 regno, struct bpf_reg_state *reg, enum reg_arg_type t) | |
1604 | { | |
1605 | u8 code, class, op; | |
1606 | ||
1607 | code = insn->code; | |
1608 | class = BPF_CLASS(code); | |
1609 | op = BPF_OP(code); | |
1610 | if (class == BPF_JMP) { | |
1611 | /* BPF_EXIT for "main" will reach here. Return TRUE | |
1612 | * conservatively. | |
1613 | */ | |
1614 | if (op == BPF_EXIT) | |
1615 | return true; | |
1616 | if (op == BPF_CALL) { | |
1617 | /* BPF to BPF call will reach here because of marking | |
1618 | * caller saved clobber with DST_OP_NO_MARK for which we | |
1619 | * don't care the register def because they are anyway | |
1620 | * marked as NOT_INIT already. | |
1621 | */ | |
1622 | if (insn->src_reg == BPF_PSEUDO_CALL) | |
1623 | return false; | |
1624 | /* Helper call will reach here because of arg type | |
1625 | * check, conservatively return TRUE. | |
1626 | */ | |
1627 | if (t == SRC_OP) | |
1628 | return true; | |
1629 | ||
1630 | return false; | |
1631 | } | |
1632 | } | |
1633 | ||
1634 | if (class == BPF_ALU64 || class == BPF_JMP || | |
1635 | /* BPF_END always use BPF_ALU class. */ | |
1636 | (class == BPF_ALU && op == BPF_END && insn->imm == 64)) | |
1637 | return true; | |
1638 | ||
1639 | if (class == BPF_ALU || class == BPF_JMP32) | |
1640 | return false; | |
1641 | ||
1642 | if (class == BPF_LDX) { | |
1643 | if (t != SRC_OP) | |
1644 | return BPF_SIZE(code) == BPF_DW; | |
1645 | /* LDX source must be ptr. */ | |
1646 | return true; | |
1647 | } | |
1648 | ||
1649 | if (class == BPF_STX) { | |
1650 | if (reg->type != SCALAR_VALUE) | |
1651 | return true; | |
1652 | return BPF_SIZE(code) == BPF_DW; | |
1653 | } | |
1654 | ||
1655 | if (class == BPF_LD) { | |
1656 | u8 mode = BPF_MODE(code); | |
1657 | ||
1658 | /* LD_IMM64 */ | |
1659 | if (mode == BPF_IMM) | |
1660 | return true; | |
1661 | ||
1662 | /* Both LD_IND and LD_ABS return 32-bit data. */ | |
1663 | if (t != SRC_OP) | |
1664 | return false; | |
1665 | ||
1666 | /* Implicit ctx ptr. */ | |
1667 | if (regno == BPF_REG_6) | |
1668 | return true; | |
1669 | ||
1670 | /* Explicit source could be any width. */ | |
1671 | return true; | |
1672 | } | |
1673 | ||
1674 | if (class == BPF_ST) | |
1675 | /* The only source register for BPF_ST is a ptr. */ | |
1676 | return true; | |
1677 | ||
1678 | /* Conservatively return true at default. */ | |
1679 | return true; | |
1680 | } | |
1681 | ||
b325fbca JW |
1682 | /* Return TRUE if INSN doesn't have explicit value define. */ |
1683 | static bool insn_no_def(struct bpf_insn *insn) | |
1684 | { | |
1685 | u8 class = BPF_CLASS(insn->code); | |
1686 | ||
1687 | return (class == BPF_JMP || class == BPF_JMP32 || | |
1688 | class == BPF_STX || class == BPF_ST); | |
1689 | } | |
1690 | ||
1691 | /* Return TRUE if INSN has defined any 32-bit value explicitly. */ | |
1692 | static bool insn_has_def32(struct bpf_verifier_env *env, struct bpf_insn *insn) | |
1693 | { | |
1694 | if (insn_no_def(insn)) | |
1695 | return false; | |
1696 | ||
1697 | return !is_reg64(env, insn, insn->dst_reg, NULL, DST_OP); | |
1698 | } | |
1699 | ||
5327ed3d JW |
1700 | static void mark_insn_zext(struct bpf_verifier_env *env, |
1701 | struct bpf_reg_state *reg) | |
1702 | { | |
1703 | s32 def_idx = reg->subreg_def; | |
1704 | ||
1705 | if (def_idx == DEF_NOT_SUBREG) | |
1706 | return; | |
1707 | ||
1708 | env->insn_aux_data[def_idx - 1].zext_dst = true; | |
1709 | /* The dst will be zero extended, so won't be sub-register anymore. */ | |
1710 | reg->subreg_def = DEF_NOT_SUBREG; | |
1711 | } | |
1712 | ||
dc503a8a | 1713 | static int check_reg_arg(struct bpf_verifier_env *env, u32 regno, |
17a52670 AS |
1714 | enum reg_arg_type t) |
1715 | { | |
f4d7e40a AS |
1716 | struct bpf_verifier_state *vstate = env->cur_state; |
1717 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
5327ed3d | 1718 | struct bpf_insn *insn = env->prog->insnsi + env->insn_idx; |
c342dc10 | 1719 | struct bpf_reg_state *reg, *regs = state->regs; |
5327ed3d | 1720 | bool rw64; |
dc503a8a | 1721 | |
17a52670 | 1722 | if (regno >= MAX_BPF_REG) { |
61bd5218 | 1723 | verbose(env, "R%d is invalid\n", regno); |
17a52670 AS |
1724 | return -EINVAL; |
1725 | } | |
1726 | ||
c342dc10 | 1727 | reg = ®s[regno]; |
5327ed3d | 1728 | rw64 = is_reg64(env, insn, regno, reg, t); |
17a52670 AS |
1729 | if (t == SRC_OP) { |
1730 | /* check whether register used as source operand can be read */ | |
c342dc10 | 1731 | if (reg->type == NOT_INIT) { |
61bd5218 | 1732 | verbose(env, "R%d !read_ok\n", regno); |
17a52670 AS |
1733 | return -EACCES; |
1734 | } | |
679c782d | 1735 | /* We don't need to worry about FP liveness because it's read-only */ |
c342dc10 JW |
1736 | if (regno == BPF_REG_FP) |
1737 | return 0; | |
1738 | ||
5327ed3d JW |
1739 | if (rw64) |
1740 | mark_insn_zext(env, reg); | |
1741 | ||
1742 | return mark_reg_read(env, reg, reg->parent, | |
1743 | rw64 ? REG_LIVE_READ64 : REG_LIVE_READ32); | |
17a52670 AS |
1744 | } else { |
1745 | /* check whether register used as dest operand can be written to */ | |
1746 | if (regno == BPF_REG_FP) { | |
61bd5218 | 1747 | verbose(env, "frame pointer is read only\n"); |
17a52670 AS |
1748 | return -EACCES; |
1749 | } | |
c342dc10 | 1750 | reg->live |= REG_LIVE_WRITTEN; |
5327ed3d | 1751 | reg->subreg_def = rw64 ? DEF_NOT_SUBREG : env->insn_idx + 1; |
17a52670 | 1752 | if (t == DST_OP) |
61bd5218 | 1753 | mark_reg_unknown(env, regs, regno); |
17a52670 AS |
1754 | } |
1755 | return 0; | |
1756 | } | |
1757 | ||
b5dc0163 AS |
1758 | /* for any branch, call, exit record the history of jmps in the given state */ |
1759 | static int push_jmp_history(struct bpf_verifier_env *env, | |
1760 | struct bpf_verifier_state *cur) | |
1761 | { | |
1762 | u32 cnt = cur->jmp_history_cnt; | |
1763 | struct bpf_idx_pair *p; | |
1764 | ||
1765 | cnt++; | |
1766 | p = krealloc(cur->jmp_history, cnt * sizeof(*p), GFP_USER); | |
1767 | if (!p) | |
1768 | return -ENOMEM; | |
1769 | p[cnt - 1].idx = env->insn_idx; | |
1770 | p[cnt - 1].prev_idx = env->prev_insn_idx; | |
1771 | cur->jmp_history = p; | |
1772 | cur->jmp_history_cnt = cnt; | |
1773 | return 0; | |
1774 | } | |
1775 | ||
1776 | /* Backtrack one insn at a time. If idx is not at the top of recorded | |
1777 | * history then previous instruction came from straight line execution. | |
1778 | */ | |
1779 | static int get_prev_insn_idx(struct bpf_verifier_state *st, int i, | |
1780 | u32 *history) | |
1781 | { | |
1782 | u32 cnt = *history; | |
1783 | ||
1784 | if (cnt && st->jmp_history[cnt - 1].idx == i) { | |
1785 | i = st->jmp_history[cnt - 1].prev_idx; | |
1786 | (*history)--; | |
1787 | } else { | |
1788 | i--; | |
1789 | } | |
1790 | return i; | |
1791 | } | |
1792 | ||
1793 | /* For given verifier state backtrack_insn() is called from the last insn to | |
1794 | * the first insn. Its purpose is to compute a bitmask of registers and | |
1795 | * stack slots that needs precision in the parent verifier state. | |
1796 | */ | |
1797 | static int backtrack_insn(struct bpf_verifier_env *env, int idx, | |
1798 | u32 *reg_mask, u64 *stack_mask) | |
1799 | { | |
1800 | const struct bpf_insn_cbs cbs = { | |
1801 | .cb_print = verbose, | |
1802 | .private_data = env, | |
1803 | }; | |
1804 | struct bpf_insn *insn = env->prog->insnsi + idx; | |
1805 | u8 class = BPF_CLASS(insn->code); | |
1806 | u8 opcode = BPF_OP(insn->code); | |
1807 | u8 mode = BPF_MODE(insn->code); | |
1808 | u32 dreg = 1u << insn->dst_reg; | |
1809 | u32 sreg = 1u << insn->src_reg; | |
1810 | u32 spi; | |
1811 | ||
1812 | if (insn->code == 0) | |
1813 | return 0; | |
1814 | if (env->log.level & BPF_LOG_LEVEL) { | |
1815 | verbose(env, "regs=%x stack=%llx before ", *reg_mask, *stack_mask); | |
1816 | verbose(env, "%d: ", idx); | |
1817 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); | |
1818 | } | |
1819 | ||
1820 | if (class == BPF_ALU || class == BPF_ALU64) { | |
1821 | if (!(*reg_mask & dreg)) | |
1822 | return 0; | |
1823 | if (opcode == BPF_MOV) { | |
1824 | if (BPF_SRC(insn->code) == BPF_X) { | |
1825 | /* dreg = sreg | |
1826 | * dreg needs precision after this insn | |
1827 | * sreg needs precision before this insn | |
1828 | */ | |
1829 | *reg_mask &= ~dreg; | |
1830 | *reg_mask |= sreg; | |
1831 | } else { | |
1832 | /* dreg = K | |
1833 | * dreg needs precision after this insn. | |
1834 | * Corresponding register is already marked | |
1835 | * as precise=true in this verifier state. | |
1836 | * No further markings in parent are necessary | |
1837 | */ | |
1838 | *reg_mask &= ~dreg; | |
1839 | } | |
1840 | } else { | |
1841 | if (BPF_SRC(insn->code) == BPF_X) { | |
1842 | /* dreg += sreg | |
1843 | * both dreg and sreg need precision | |
1844 | * before this insn | |
1845 | */ | |
1846 | *reg_mask |= sreg; | |
1847 | } /* else dreg += K | |
1848 | * dreg still needs precision before this insn | |
1849 | */ | |
1850 | } | |
1851 | } else if (class == BPF_LDX) { | |
1852 | if (!(*reg_mask & dreg)) | |
1853 | return 0; | |
1854 | *reg_mask &= ~dreg; | |
1855 | ||
1856 | /* scalars can only be spilled into stack w/o losing precision. | |
1857 | * Load from any other memory can be zero extended. | |
1858 | * The desire to keep that precision is already indicated | |
1859 | * by 'precise' mark in corresponding register of this state. | |
1860 | * No further tracking necessary. | |
1861 | */ | |
1862 | if (insn->src_reg != BPF_REG_FP) | |
1863 | return 0; | |
1864 | if (BPF_SIZE(insn->code) != BPF_DW) | |
1865 | return 0; | |
1866 | ||
1867 | /* dreg = *(u64 *)[fp - off] was a fill from the stack. | |
1868 | * that [fp - off] slot contains scalar that needs to be | |
1869 | * tracked with precision | |
1870 | */ | |
1871 | spi = (-insn->off - 1) / BPF_REG_SIZE; | |
1872 | if (spi >= 64) { | |
1873 | verbose(env, "BUG spi %d\n", spi); | |
1874 | WARN_ONCE(1, "verifier backtracking bug"); | |
1875 | return -EFAULT; | |
1876 | } | |
1877 | *stack_mask |= 1ull << spi; | |
b3b50f05 | 1878 | } else if (class == BPF_STX || class == BPF_ST) { |
b5dc0163 | 1879 | if (*reg_mask & dreg) |
b3b50f05 | 1880 | /* stx & st shouldn't be using _scalar_ dst_reg |
b5dc0163 AS |
1881 | * to access memory. It means backtracking |
1882 | * encountered a case of pointer subtraction. | |
1883 | */ | |
1884 | return -ENOTSUPP; | |
1885 | /* scalars can only be spilled into stack */ | |
1886 | if (insn->dst_reg != BPF_REG_FP) | |
1887 | return 0; | |
1888 | if (BPF_SIZE(insn->code) != BPF_DW) | |
1889 | return 0; | |
1890 | spi = (-insn->off - 1) / BPF_REG_SIZE; | |
1891 | if (spi >= 64) { | |
1892 | verbose(env, "BUG spi %d\n", spi); | |
1893 | WARN_ONCE(1, "verifier backtracking bug"); | |
1894 | return -EFAULT; | |
1895 | } | |
1896 | if (!(*stack_mask & (1ull << spi))) | |
1897 | return 0; | |
1898 | *stack_mask &= ~(1ull << spi); | |
b3b50f05 AN |
1899 | if (class == BPF_STX) |
1900 | *reg_mask |= sreg; | |
b5dc0163 AS |
1901 | } else if (class == BPF_JMP || class == BPF_JMP32) { |
1902 | if (opcode == BPF_CALL) { | |
1903 | if (insn->src_reg == BPF_PSEUDO_CALL) | |
1904 | return -ENOTSUPP; | |
1905 | /* regular helper call sets R0 */ | |
1906 | *reg_mask &= ~1; | |
1907 | if (*reg_mask & 0x3f) { | |
1908 | /* if backtracing was looking for registers R1-R5 | |
1909 | * they should have been found already. | |
1910 | */ | |
1911 | verbose(env, "BUG regs %x\n", *reg_mask); | |
1912 | WARN_ONCE(1, "verifier backtracking bug"); | |
1913 | return -EFAULT; | |
1914 | } | |
1915 | } else if (opcode == BPF_EXIT) { | |
1916 | return -ENOTSUPP; | |
1917 | } | |
1918 | } else if (class == BPF_LD) { | |
1919 | if (!(*reg_mask & dreg)) | |
1920 | return 0; | |
1921 | *reg_mask &= ~dreg; | |
1922 | /* It's ld_imm64 or ld_abs or ld_ind. | |
1923 | * For ld_imm64 no further tracking of precision | |
1924 | * into parent is necessary | |
1925 | */ | |
1926 | if (mode == BPF_IND || mode == BPF_ABS) | |
1927 | /* to be analyzed */ | |
1928 | return -ENOTSUPP; | |
b5dc0163 AS |
1929 | } |
1930 | return 0; | |
1931 | } | |
1932 | ||
1933 | /* the scalar precision tracking algorithm: | |
1934 | * . at the start all registers have precise=false. | |
1935 | * . scalar ranges are tracked as normal through alu and jmp insns. | |
1936 | * . once precise value of the scalar register is used in: | |
1937 | * . ptr + scalar alu | |
1938 | * . if (scalar cond K|scalar) | |
1939 | * . helper_call(.., scalar, ...) where ARG_CONST is expected | |
1940 | * backtrack through the verifier states and mark all registers and | |
1941 | * stack slots with spilled constants that these scalar regisers | |
1942 | * should be precise. | |
1943 | * . during state pruning two registers (or spilled stack slots) | |
1944 | * are equivalent if both are not precise. | |
1945 | * | |
1946 | * Note the verifier cannot simply walk register parentage chain, | |
1947 | * since many different registers and stack slots could have been | |
1948 | * used to compute single precise scalar. | |
1949 | * | |
1950 | * The approach of starting with precise=true for all registers and then | |
1951 | * backtrack to mark a register as not precise when the verifier detects | |
1952 | * that program doesn't care about specific value (e.g., when helper | |
1953 | * takes register as ARG_ANYTHING parameter) is not safe. | |
1954 | * | |
1955 | * It's ok to walk single parentage chain of the verifier states. | |
1956 | * It's possible that this backtracking will go all the way till 1st insn. | |
1957 | * All other branches will be explored for needing precision later. | |
1958 | * | |
1959 | * The backtracking needs to deal with cases like: | |
1960 | * 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) | |
1961 | * r9 -= r8 | |
1962 | * r5 = r9 | |
1963 | * if r5 > 0x79f goto pc+7 | |
1964 | * R5_w=inv(id=0,umax_value=1951,var_off=(0x0; 0x7ff)) | |
1965 | * r5 += 1 | |
1966 | * ... | |
1967 | * call bpf_perf_event_output#25 | |
1968 | * where .arg5_type = ARG_CONST_SIZE_OR_ZERO | |
1969 | * | |
1970 | * and this case: | |
1971 | * r6 = 1 | |
1972 | * call foo // uses callee's r6 inside to compute r0 | |
1973 | * r0 += r6 | |
1974 | * if r0 == 0 goto | |
1975 | * | |
1976 | * to track above reg_mask/stack_mask needs to be independent for each frame. | |
1977 | * | |
1978 | * Also if parent's curframe > frame where backtracking started, | |
1979 | * the verifier need to mark registers in both frames, otherwise callees | |
1980 | * may incorrectly prune callers. This is similar to | |
1981 | * commit 7640ead93924 ("bpf: verifier: make sure callees don't prune with caller differences") | |
1982 | * | |
1983 | * For now backtracking falls back into conservative marking. | |
1984 | */ | |
1985 | static void mark_all_scalars_precise(struct bpf_verifier_env *env, | |
1986 | struct bpf_verifier_state *st) | |
1987 | { | |
1988 | struct bpf_func_state *func; | |
1989 | struct bpf_reg_state *reg; | |
1990 | int i, j; | |
1991 | ||
1992 | /* big hammer: mark all scalars precise in this path. | |
1993 | * pop_stack may still get !precise scalars. | |
1994 | */ | |
1995 | for (; st; st = st->parent) | |
1996 | for (i = 0; i <= st->curframe; i++) { | |
1997 | func = st->frame[i]; | |
1998 | for (j = 0; j < BPF_REG_FP; j++) { | |
1999 | reg = &func->regs[j]; | |
2000 | if (reg->type != SCALAR_VALUE) | |
2001 | continue; | |
2002 | reg->precise = true; | |
2003 | } | |
2004 | for (j = 0; j < func->allocated_stack / BPF_REG_SIZE; j++) { | |
2005 | if (func->stack[j].slot_type[0] != STACK_SPILL) | |
2006 | continue; | |
2007 | reg = &func->stack[j].spilled_ptr; | |
2008 | if (reg->type != SCALAR_VALUE) | |
2009 | continue; | |
2010 | reg->precise = true; | |
2011 | } | |
2012 | } | |
2013 | } | |
2014 | ||
a3ce685d AS |
2015 | static int __mark_chain_precision(struct bpf_verifier_env *env, int regno, |
2016 | int spi) | |
b5dc0163 AS |
2017 | { |
2018 | struct bpf_verifier_state *st = env->cur_state; | |
2019 | int first_idx = st->first_insn_idx; | |
2020 | int last_idx = env->insn_idx; | |
2021 | struct bpf_func_state *func; | |
2022 | struct bpf_reg_state *reg; | |
a3ce685d AS |
2023 | u32 reg_mask = regno >= 0 ? 1u << regno : 0; |
2024 | u64 stack_mask = spi >= 0 ? 1ull << spi : 0; | |
b5dc0163 | 2025 | bool skip_first = true; |
a3ce685d | 2026 | bool new_marks = false; |
b5dc0163 AS |
2027 | int i, err; |
2028 | ||
2c78ee89 | 2029 | if (!env->bpf_capable) |
b5dc0163 AS |
2030 | return 0; |
2031 | ||
2032 | func = st->frame[st->curframe]; | |
a3ce685d AS |
2033 | if (regno >= 0) { |
2034 | reg = &func->regs[regno]; | |
2035 | if (reg->type != SCALAR_VALUE) { | |
2036 | WARN_ONCE(1, "backtracing misuse"); | |
2037 | return -EFAULT; | |
2038 | } | |
2039 | if (!reg->precise) | |
2040 | new_marks = true; | |
2041 | else | |
2042 | reg_mask = 0; | |
2043 | reg->precise = true; | |
b5dc0163 | 2044 | } |
b5dc0163 | 2045 | |
a3ce685d AS |
2046 | while (spi >= 0) { |
2047 | if (func->stack[spi].slot_type[0] != STACK_SPILL) { | |
2048 | stack_mask = 0; | |
2049 | break; | |
2050 | } | |
2051 | reg = &func->stack[spi].spilled_ptr; | |
2052 | if (reg->type != SCALAR_VALUE) { | |
2053 | stack_mask = 0; | |
2054 | break; | |
2055 | } | |
2056 | if (!reg->precise) | |
2057 | new_marks = true; | |
2058 | else | |
2059 | stack_mask = 0; | |
2060 | reg->precise = true; | |
2061 | break; | |
2062 | } | |
2063 | ||
2064 | if (!new_marks) | |
2065 | return 0; | |
2066 | if (!reg_mask && !stack_mask) | |
2067 | return 0; | |
b5dc0163 AS |
2068 | for (;;) { |
2069 | DECLARE_BITMAP(mask, 64); | |
b5dc0163 AS |
2070 | u32 history = st->jmp_history_cnt; |
2071 | ||
2072 | if (env->log.level & BPF_LOG_LEVEL) | |
2073 | verbose(env, "last_idx %d first_idx %d\n", last_idx, first_idx); | |
2074 | for (i = last_idx;;) { | |
2075 | if (skip_first) { | |
2076 | err = 0; | |
2077 | skip_first = false; | |
2078 | } else { | |
2079 | err = backtrack_insn(env, i, ®_mask, &stack_mask); | |
2080 | } | |
2081 | if (err == -ENOTSUPP) { | |
2082 | mark_all_scalars_precise(env, st); | |
2083 | return 0; | |
2084 | } else if (err) { | |
2085 | return err; | |
2086 | } | |
2087 | if (!reg_mask && !stack_mask) | |
2088 | /* Found assignment(s) into tracked register in this state. | |
2089 | * Since this state is already marked, just return. | |
2090 | * Nothing to be tracked further in the parent state. | |
2091 | */ | |
2092 | return 0; | |
2093 | if (i == first_idx) | |
2094 | break; | |
2095 | i = get_prev_insn_idx(st, i, &history); | |
2096 | if (i >= env->prog->len) { | |
2097 | /* This can happen if backtracking reached insn 0 | |
2098 | * and there are still reg_mask or stack_mask | |
2099 | * to backtrack. | |
2100 | * It means the backtracking missed the spot where | |
2101 | * particular register was initialized with a constant. | |
2102 | */ | |
2103 | verbose(env, "BUG backtracking idx %d\n", i); | |
2104 | WARN_ONCE(1, "verifier backtracking bug"); | |
2105 | return -EFAULT; | |
2106 | } | |
2107 | } | |
2108 | st = st->parent; | |
2109 | if (!st) | |
2110 | break; | |
2111 | ||
a3ce685d | 2112 | new_marks = false; |
b5dc0163 AS |
2113 | func = st->frame[st->curframe]; |
2114 | bitmap_from_u64(mask, reg_mask); | |
2115 | for_each_set_bit(i, mask, 32) { | |
2116 | reg = &func->regs[i]; | |
a3ce685d AS |
2117 | if (reg->type != SCALAR_VALUE) { |
2118 | reg_mask &= ~(1u << i); | |
b5dc0163 | 2119 | continue; |
a3ce685d | 2120 | } |
b5dc0163 AS |
2121 | if (!reg->precise) |
2122 | new_marks = true; | |
2123 | reg->precise = true; | |
2124 | } | |
2125 | ||
2126 | bitmap_from_u64(mask, stack_mask); | |
2127 | for_each_set_bit(i, mask, 64) { | |
2128 | if (i >= func->allocated_stack / BPF_REG_SIZE) { | |
2339cd6c AS |
2129 | /* the sequence of instructions: |
2130 | * 2: (bf) r3 = r10 | |
2131 | * 3: (7b) *(u64 *)(r3 -8) = r0 | |
2132 | * 4: (79) r4 = *(u64 *)(r10 -8) | |
2133 | * doesn't contain jmps. It's backtracked | |
2134 | * as a single block. | |
2135 | * During backtracking insn 3 is not recognized as | |
2136 | * stack access, so at the end of backtracking | |
2137 | * stack slot fp-8 is still marked in stack_mask. | |
2138 | * However the parent state may not have accessed | |
2139 | * fp-8 and it's "unallocated" stack space. | |
2140 | * In such case fallback to conservative. | |
b5dc0163 | 2141 | */ |
2339cd6c AS |
2142 | mark_all_scalars_precise(env, st); |
2143 | return 0; | |
b5dc0163 AS |
2144 | } |
2145 | ||
a3ce685d AS |
2146 | if (func->stack[i].slot_type[0] != STACK_SPILL) { |
2147 | stack_mask &= ~(1ull << i); | |
b5dc0163 | 2148 | continue; |
a3ce685d | 2149 | } |
b5dc0163 | 2150 | reg = &func->stack[i].spilled_ptr; |
a3ce685d AS |
2151 | if (reg->type != SCALAR_VALUE) { |
2152 | stack_mask &= ~(1ull << i); | |
b5dc0163 | 2153 | continue; |
a3ce685d | 2154 | } |
b5dc0163 AS |
2155 | if (!reg->precise) |
2156 | new_marks = true; | |
2157 | reg->precise = true; | |
2158 | } | |
2159 | if (env->log.level & BPF_LOG_LEVEL) { | |
2160 | print_verifier_state(env, func); | |
2161 | verbose(env, "parent %s regs=%x stack=%llx marks\n", | |
2162 | new_marks ? "didn't have" : "already had", | |
2163 | reg_mask, stack_mask); | |
2164 | } | |
2165 | ||
a3ce685d AS |
2166 | if (!reg_mask && !stack_mask) |
2167 | break; | |
b5dc0163 AS |
2168 | if (!new_marks) |
2169 | break; | |
2170 | ||
2171 | last_idx = st->last_insn_idx; | |
2172 | first_idx = st->first_insn_idx; | |
2173 | } | |
2174 | return 0; | |
2175 | } | |
2176 | ||
a3ce685d AS |
2177 | static int mark_chain_precision(struct bpf_verifier_env *env, int regno) |
2178 | { | |
2179 | return __mark_chain_precision(env, regno, -1); | |
2180 | } | |
2181 | ||
2182 | static int mark_chain_precision_stack(struct bpf_verifier_env *env, int spi) | |
2183 | { | |
2184 | return __mark_chain_precision(env, -1, spi); | |
2185 | } | |
b5dc0163 | 2186 | |
1be7f75d AS |
2187 | static bool is_spillable_regtype(enum bpf_reg_type type) |
2188 | { | |
2189 | switch (type) { | |
2190 | case PTR_TO_MAP_VALUE: | |
2191 | case PTR_TO_MAP_VALUE_OR_NULL: | |
2192 | case PTR_TO_STACK: | |
2193 | case PTR_TO_CTX: | |
969bf05e | 2194 | case PTR_TO_PACKET: |
de8f3a83 | 2195 | case PTR_TO_PACKET_META: |
969bf05e | 2196 | case PTR_TO_PACKET_END: |
d58e468b | 2197 | case PTR_TO_FLOW_KEYS: |
1be7f75d | 2198 | case CONST_PTR_TO_MAP: |
c64b7983 JS |
2199 | case PTR_TO_SOCKET: |
2200 | case PTR_TO_SOCKET_OR_NULL: | |
46f8bc92 MKL |
2201 | case PTR_TO_SOCK_COMMON: |
2202 | case PTR_TO_SOCK_COMMON_OR_NULL: | |
655a51e5 MKL |
2203 | case PTR_TO_TCP_SOCK: |
2204 | case PTR_TO_TCP_SOCK_OR_NULL: | |
fada7fdc | 2205 | case PTR_TO_XDP_SOCK: |
65726b5b | 2206 | case PTR_TO_BTF_ID: |
b121b341 | 2207 | case PTR_TO_BTF_ID_OR_NULL: |
afbf21dc YS |
2208 | case PTR_TO_RDONLY_BUF: |
2209 | case PTR_TO_RDONLY_BUF_OR_NULL: | |
2210 | case PTR_TO_RDWR_BUF: | |
2211 | case PTR_TO_RDWR_BUF_OR_NULL: | |
eaa6bcb7 | 2212 | case PTR_TO_PERCPU_BTF_ID: |
1be7f75d AS |
2213 | return true; |
2214 | default: | |
2215 | return false; | |
2216 | } | |
2217 | } | |
2218 | ||
cc2b14d5 AS |
2219 | /* Does this register contain a constant zero? */ |
2220 | static bool register_is_null(struct bpf_reg_state *reg) | |
2221 | { | |
2222 | return reg->type == SCALAR_VALUE && tnum_equals_const(reg->var_off, 0); | |
2223 | } | |
2224 | ||
f7cf25b2 AS |
2225 | static bool register_is_const(struct bpf_reg_state *reg) |
2226 | { | |
2227 | return reg->type == SCALAR_VALUE && tnum_is_const(reg->var_off); | |
2228 | } | |
2229 | ||
6e7e63cb JH |
2230 | static bool __is_pointer_value(bool allow_ptr_leaks, |
2231 | const struct bpf_reg_state *reg) | |
2232 | { | |
2233 | if (allow_ptr_leaks) | |
2234 | return false; | |
2235 | ||
2236 | return reg->type != SCALAR_VALUE; | |
2237 | } | |
2238 | ||
f7cf25b2 AS |
2239 | static void save_register_state(struct bpf_func_state *state, |
2240 | int spi, struct bpf_reg_state *reg) | |
2241 | { | |
2242 | int i; | |
2243 | ||
2244 | state->stack[spi].spilled_ptr = *reg; | |
2245 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; | |
2246 | ||
2247 | for (i = 0; i < BPF_REG_SIZE; i++) | |
2248 | state->stack[spi].slot_type[i] = STACK_SPILL; | |
2249 | } | |
2250 | ||
17a52670 AS |
2251 | /* check_stack_read/write functions track spill/fill of registers, |
2252 | * stack boundary and alignment are checked in check_mem_access() | |
2253 | */ | |
61bd5218 | 2254 | static int check_stack_write(struct bpf_verifier_env *env, |
f4d7e40a | 2255 | struct bpf_func_state *state, /* func where register points to */ |
af86ca4e | 2256 | int off, int size, int value_regno, int insn_idx) |
17a52670 | 2257 | { |
f4d7e40a | 2258 | struct bpf_func_state *cur; /* state of the current function */ |
638f5b90 | 2259 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err; |
b5dc0163 | 2260 | u32 dst_reg = env->prog->insnsi[insn_idx].dst_reg; |
f7cf25b2 | 2261 | struct bpf_reg_state *reg = NULL; |
638f5b90 | 2262 | |
f4d7e40a | 2263 | err = realloc_func_state(state, round_up(slot + 1, BPF_REG_SIZE), |
fd978bf7 | 2264 | state->acquired_refs, true); |
638f5b90 AS |
2265 | if (err) |
2266 | return err; | |
9c399760 AS |
2267 | /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0, |
2268 | * so it's aligned access and [off, off + size) are within stack limits | |
2269 | */ | |
638f5b90 AS |
2270 | if (!env->allow_ptr_leaks && |
2271 | state->stack[spi].slot_type[0] == STACK_SPILL && | |
2272 | size != BPF_REG_SIZE) { | |
2273 | verbose(env, "attempt to corrupt spilled pointer on stack\n"); | |
2274 | return -EACCES; | |
2275 | } | |
17a52670 | 2276 | |
f4d7e40a | 2277 | cur = env->cur_state->frame[env->cur_state->curframe]; |
f7cf25b2 AS |
2278 | if (value_regno >= 0) |
2279 | reg = &cur->regs[value_regno]; | |
17a52670 | 2280 | |
f7cf25b2 | 2281 | if (reg && size == BPF_REG_SIZE && register_is_const(reg) && |
2c78ee89 | 2282 | !register_is_null(reg) && env->bpf_capable) { |
b5dc0163 AS |
2283 | if (dst_reg != BPF_REG_FP) { |
2284 | /* The backtracking logic can only recognize explicit | |
2285 | * stack slot address like [fp - 8]. Other spill of | |
2286 | * scalar via different register has to be conervative. | |
2287 | * Backtrack from here and mark all registers as precise | |
2288 | * that contributed into 'reg' being a constant. | |
2289 | */ | |
2290 | err = mark_chain_precision(env, value_regno); | |
2291 | if (err) | |
2292 | return err; | |
2293 | } | |
f7cf25b2 AS |
2294 | save_register_state(state, spi, reg); |
2295 | } else if (reg && is_spillable_regtype(reg->type)) { | |
17a52670 | 2296 | /* register containing pointer is being spilled into stack */ |
9c399760 | 2297 | if (size != BPF_REG_SIZE) { |
f7cf25b2 | 2298 | verbose_linfo(env, insn_idx, "; "); |
61bd5218 | 2299 | verbose(env, "invalid size of register spill\n"); |
17a52670 AS |
2300 | return -EACCES; |
2301 | } | |
2302 | ||
f7cf25b2 | 2303 | if (state != cur && reg->type == PTR_TO_STACK) { |
f4d7e40a AS |
2304 | verbose(env, "cannot spill pointers to stack into stack frame of the caller\n"); |
2305 | return -EINVAL; | |
2306 | } | |
2307 | ||
2c78ee89 | 2308 | if (!env->bypass_spec_v4) { |
f7cf25b2 | 2309 | bool sanitize = false; |
17a52670 | 2310 | |
f7cf25b2 AS |
2311 | if (state->stack[spi].slot_type[0] == STACK_SPILL && |
2312 | register_is_const(&state->stack[spi].spilled_ptr)) | |
2313 | sanitize = true; | |
2314 | for (i = 0; i < BPF_REG_SIZE; i++) | |
2315 | if (state->stack[spi].slot_type[i] == STACK_MISC) { | |
2316 | sanitize = true; | |
2317 | break; | |
2318 | } | |
2319 | if (sanitize) { | |
af86ca4e AS |
2320 | int *poff = &env->insn_aux_data[insn_idx].sanitize_stack_off; |
2321 | int soff = (-spi - 1) * BPF_REG_SIZE; | |
2322 | ||
2323 | /* detected reuse of integer stack slot with a pointer | |
2324 | * which means either llvm is reusing stack slot or | |
2325 | * an attacker is trying to exploit CVE-2018-3639 | |
2326 | * (speculative store bypass) | |
2327 | * Have to sanitize that slot with preemptive | |
2328 | * store of zero. | |
2329 | */ | |
2330 | if (*poff && *poff != soff) { | |
2331 | /* disallow programs where single insn stores | |
2332 | * into two different stack slots, since verifier | |
2333 | * cannot sanitize them | |
2334 | */ | |
2335 | verbose(env, | |
2336 | "insn %d cannot access two stack slots fp%d and fp%d", | |
2337 | insn_idx, *poff, soff); | |
2338 | return -EINVAL; | |
2339 | } | |
2340 | *poff = soff; | |
2341 | } | |
af86ca4e | 2342 | } |
f7cf25b2 | 2343 | save_register_state(state, spi, reg); |
9c399760 | 2344 | } else { |
cc2b14d5 AS |
2345 | u8 type = STACK_MISC; |
2346 | ||
679c782d EC |
2347 | /* regular write of data into stack destroys any spilled ptr */ |
2348 | state->stack[spi].spilled_ptr.type = NOT_INIT; | |
0bae2d4d JW |
2349 | /* Mark slots as STACK_MISC if they belonged to spilled ptr. */ |
2350 | if (state->stack[spi].slot_type[0] == STACK_SPILL) | |
2351 | for (i = 0; i < BPF_REG_SIZE; i++) | |
2352 | state->stack[spi].slot_type[i] = STACK_MISC; | |
9c399760 | 2353 | |
cc2b14d5 AS |
2354 | /* only mark the slot as written if all 8 bytes were written |
2355 | * otherwise read propagation may incorrectly stop too soon | |
2356 | * when stack slots are partially written. | |
2357 | * This heuristic means that read propagation will be | |
2358 | * conservative, since it will add reg_live_read marks | |
2359 | * to stack slots all the way to first state when programs | |
2360 | * writes+reads less than 8 bytes | |
2361 | */ | |
2362 | if (size == BPF_REG_SIZE) | |
2363 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; | |
2364 | ||
2365 | /* when we zero initialize stack slots mark them as such */ | |
b5dc0163 AS |
2366 | if (reg && register_is_null(reg)) { |
2367 | /* backtracking doesn't work for STACK_ZERO yet. */ | |
2368 | err = mark_chain_precision(env, value_regno); | |
2369 | if (err) | |
2370 | return err; | |
cc2b14d5 | 2371 | type = STACK_ZERO; |
b5dc0163 | 2372 | } |
cc2b14d5 | 2373 | |
0bae2d4d | 2374 | /* Mark slots affected by this stack write. */ |
9c399760 | 2375 | for (i = 0; i < size; i++) |
638f5b90 | 2376 | state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] = |
cc2b14d5 | 2377 | type; |
17a52670 AS |
2378 | } |
2379 | return 0; | |
2380 | } | |
2381 | ||
61bd5218 | 2382 | static int check_stack_read(struct bpf_verifier_env *env, |
f4d7e40a AS |
2383 | struct bpf_func_state *reg_state /* func where register points to */, |
2384 | int off, int size, int value_regno) | |
17a52670 | 2385 | { |
f4d7e40a AS |
2386 | struct bpf_verifier_state *vstate = env->cur_state; |
2387 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
638f5b90 | 2388 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE; |
f7cf25b2 | 2389 | struct bpf_reg_state *reg; |
638f5b90 | 2390 | u8 *stype; |
17a52670 | 2391 | |
f4d7e40a | 2392 | if (reg_state->allocated_stack <= slot) { |
638f5b90 AS |
2393 | verbose(env, "invalid read from stack off %d+0 size %d\n", |
2394 | off, size); | |
2395 | return -EACCES; | |
2396 | } | |
f4d7e40a | 2397 | stype = reg_state->stack[spi].slot_type; |
f7cf25b2 | 2398 | reg = ®_state->stack[spi].spilled_ptr; |
17a52670 | 2399 | |
638f5b90 | 2400 | if (stype[0] == STACK_SPILL) { |
9c399760 | 2401 | if (size != BPF_REG_SIZE) { |
f7cf25b2 AS |
2402 | if (reg->type != SCALAR_VALUE) { |
2403 | verbose_linfo(env, env->insn_idx, "; "); | |
2404 | verbose(env, "invalid size of register fill\n"); | |
2405 | return -EACCES; | |
2406 | } | |
2407 | if (value_regno >= 0) { | |
2408 | mark_reg_unknown(env, state->regs, value_regno); | |
2409 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; | |
2410 | } | |
2411 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); | |
2412 | return 0; | |
17a52670 | 2413 | } |
9c399760 | 2414 | for (i = 1; i < BPF_REG_SIZE; i++) { |
638f5b90 | 2415 | if (stype[(slot - i) % BPF_REG_SIZE] != STACK_SPILL) { |
61bd5218 | 2416 | verbose(env, "corrupted spill memory\n"); |
17a52670 AS |
2417 | return -EACCES; |
2418 | } | |
2419 | } | |
2420 | ||
dc503a8a | 2421 | if (value_regno >= 0) { |
17a52670 | 2422 | /* restore register state from stack */ |
f7cf25b2 | 2423 | state->regs[value_regno] = *reg; |
2f18f62e AS |
2424 | /* mark reg as written since spilled pointer state likely |
2425 | * has its liveness marks cleared by is_state_visited() | |
2426 | * which resets stack/reg liveness for state transitions | |
2427 | */ | |
2428 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; | |
6e7e63cb JH |
2429 | } else if (__is_pointer_value(env->allow_ptr_leaks, reg)) { |
2430 | /* If value_regno==-1, the caller is asking us whether | |
2431 | * it is acceptable to use this value as a SCALAR_VALUE | |
2432 | * (e.g. for XADD). | |
2433 | * We must not allow unprivileged callers to do that | |
2434 | * with spilled pointers. | |
2435 | */ | |
2436 | verbose(env, "leaking pointer from stack off %d\n", | |
2437 | off); | |
2438 | return -EACCES; | |
dc503a8a | 2439 | } |
f7cf25b2 | 2440 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
17a52670 | 2441 | } else { |
cc2b14d5 AS |
2442 | int zeros = 0; |
2443 | ||
17a52670 | 2444 | for (i = 0; i < size; i++) { |
cc2b14d5 AS |
2445 | if (stype[(slot - i) % BPF_REG_SIZE] == STACK_MISC) |
2446 | continue; | |
2447 | if (stype[(slot - i) % BPF_REG_SIZE] == STACK_ZERO) { | |
2448 | zeros++; | |
2449 | continue; | |
17a52670 | 2450 | } |
cc2b14d5 AS |
2451 | verbose(env, "invalid read from stack off %d+%d size %d\n", |
2452 | off, i, size); | |
2453 | return -EACCES; | |
2454 | } | |
f7cf25b2 | 2455 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
cc2b14d5 AS |
2456 | if (value_regno >= 0) { |
2457 | if (zeros == size) { | |
2458 | /* any size read into register is zero extended, | |
2459 | * so the whole register == const_zero | |
2460 | */ | |
2461 | __mark_reg_const_zero(&state->regs[value_regno]); | |
b5dc0163 AS |
2462 | /* backtracking doesn't support STACK_ZERO yet, |
2463 | * so mark it precise here, so that later | |
2464 | * backtracking can stop here. | |
2465 | * Backtracking may not need this if this register | |
2466 | * doesn't participate in pointer adjustment. | |
2467 | * Forward propagation of precise flag is not | |
2468 | * necessary either. This mark is only to stop | |
2469 | * backtracking. Any register that contributed | |
2470 | * to const 0 was marked precise before spill. | |
2471 | */ | |
2472 | state->regs[value_regno].precise = true; | |
cc2b14d5 AS |
2473 | } else { |
2474 | /* have read misc data from the stack */ | |
2475 | mark_reg_unknown(env, state->regs, value_regno); | |
2476 | } | |
2477 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; | |
17a52670 | 2478 | } |
17a52670 | 2479 | } |
f7cf25b2 | 2480 | return 0; |
17a52670 AS |
2481 | } |
2482 | ||
e4298d25 DB |
2483 | static int check_stack_access(struct bpf_verifier_env *env, |
2484 | const struct bpf_reg_state *reg, | |
2485 | int off, int size) | |
2486 | { | |
2487 | /* Stack accesses must be at a fixed offset, so that we | |
2488 | * can determine what type of data were returned. See | |
2489 | * check_stack_read(). | |
2490 | */ | |
2491 | if (!tnum_is_const(reg->var_off)) { | |
2492 | char tn_buf[48]; | |
2493 | ||
2494 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
1fbd20f8 | 2495 | verbose(env, "variable stack access var_off=%s off=%d size=%d\n", |
e4298d25 DB |
2496 | tn_buf, off, size); |
2497 | return -EACCES; | |
2498 | } | |
2499 | ||
2500 | if (off >= 0 || off < -MAX_BPF_STACK) { | |
2501 | verbose(env, "invalid stack off=%d size=%d\n", off, size); | |
2502 | return -EACCES; | |
2503 | } | |
2504 | ||
2505 | return 0; | |
2506 | } | |
2507 | ||
591fe988 DB |
2508 | static int check_map_access_type(struct bpf_verifier_env *env, u32 regno, |
2509 | int off, int size, enum bpf_access_type type) | |
2510 | { | |
2511 | struct bpf_reg_state *regs = cur_regs(env); | |
2512 | struct bpf_map *map = regs[regno].map_ptr; | |
2513 | u32 cap = bpf_map_flags_to_cap(map); | |
2514 | ||
2515 | if (type == BPF_WRITE && !(cap & BPF_MAP_CAN_WRITE)) { | |
2516 | verbose(env, "write into map forbidden, value_size=%d off=%d size=%d\n", | |
2517 | map->value_size, off, size); | |
2518 | return -EACCES; | |
2519 | } | |
2520 | ||
2521 | if (type == BPF_READ && !(cap & BPF_MAP_CAN_READ)) { | |
2522 | verbose(env, "read from map forbidden, value_size=%d off=%d size=%d\n", | |
2523 | map->value_size, off, size); | |
2524 | return -EACCES; | |
2525 | } | |
2526 | ||
2527 | return 0; | |
2528 | } | |
2529 | ||
457f4436 AN |
2530 | /* check read/write into memory region (e.g., map value, ringbuf sample, etc) */ |
2531 | static int __check_mem_access(struct bpf_verifier_env *env, int regno, | |
2532 | int off, int size, u32 mem_size, | |
2533 | bool zero_size_allowed) | |
17a52670 | 2534 | { |
457f4436 AN |
2535 | bool size_ok = size > 0 || (size == 0 && zero_size_allowed); |
2536 | struct bpf_reg_state *reg; | |
2537 | ||
2538 | if (off >= 0 && size_ok && (u64)off + size <= mem_size) | |
2539 | return 0; | |
17a52670 | 2540 | |
457f4436 AN |
2541 | reg = &cur_regs(env)[regno]; |
2542 | switch (reg->type) { | |
2543 | case PTR_TO_MAP_VALUE: | |
61bd5218 | 2544 | verbose(env, "invalid access to map value, value_size=%d off=%d size=%d\n", |
457f4436 AN |
2545 | mem_size, off, size); |
2546 | break; | |
2547 | case PTR_TO_PACKET: | |
2548 | case PTR_TO_PACKET_META: | |
2549 | case PTR_TO_PACKET_END: | |
2550 | verbose(env, "invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n", | |
2551 | off, size, regno, reg->id, off, mem_size); | |
2552 | break; | |
2553 | case PTR_TO_MEM: | |
2554 | default: | |
2555 | verbose(env, "invalid access to memory, mem_size=%u off=%d size=%d\n", | |
2556 | mem_size, off, size); | |
17a52670 | 2557 | } |
457f4436 AN |
2558 | |
2559 | return -EACCES; | |
17a52670 AS |
2560 | } |
2561 | ||
457f4436 AN |
2562 | /* check read/write into a memory region with possible variable offset */ |
2563 | static int check_mem_region_access(struct bpf_verifier_env *env, u32 regno, | |
2564 | int off, int size, u32 mem_size, | |
2565 | bool zero_size_allowed) | |
dbcfe5f7 | 2566 | { |
f4d7e40a AS |
2567 | struct bpf_verifier_state *vstate = env->cur_state; |
2568 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
dbcfe5f7 GB |
2569 | struct bpf_reg_state *reg = &state->regs[regno]; |
2570 | int err; | |
2571 | ||
457f4436 | 2572 | /* We may have adjusted the register pointing to memory region, so we |
f1174f77 EC |
2573 | * need to try adding each of min_value and max_value to off |
2574 | * to make sure our theoretical access will be safe. | |
dbcfe5f7 | 2575 | */ |
06ee7115 | 2576 | if (env->log.level & BPF_LOG_LEVEL) |
61bd5218 | 2577 | print_verifier_state(env, state); |
b7137c4e | 2578 | |
dbcfe5f7 GB |
2579 | /* The minimum value is only important with signed |
2580 | * comparisons where we can't assume the floor of a | |
2581 | * value is 0. If we are using signed variables for our | |
2582 | * index'es we need to make sure that whatever we use | |
2583 | * will have a set floor within our range. | |
2584 | */ | |
b7137c4e DB |
2585 | if (reg->smin_value < 0 && |
2586 | (reg->smin_value == S64_MIN || | |
2587 | (off + reg->smin_value != (s64)(s32)(off + reg->smin_value)) || | |
2588 | reg->smin_value + off < 0)) { | |
61bd5218 | 2589 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
dbcfe5f7 GB |
2590 | regno); |
2591 | return -EACCES; | |
2592 | } | |
457f4436 AN |
2593 | err = __check_mem_access(env, regno, reg->smin_value + off, size, |
2594 | mem_size, zero_size_allowed); | |
dbcfe5f7 | 2595 | if (err) { |
457f4436 | 2596 | verbose(env, "R%d min value is outside of the allowed memory range\n", |
61bd5218 | 2597 | regno); |
dbcfe5f7 GB |
2598 | return err; |
2599 | } | |
2600 | ||
b03c9f9f EC |
2601 | /* If we haven't set a max value then we need to bail since we can't be |
2602 | * sure we won't do bad things. | |
2603 | * If reg->umax_value + off could overflow, treat that as unbounded too. | |
dbcfe5f7 | 2604 | */ |
b03c9f9f | 2605 | if (reg->umax_value >= BPF_MAX_VAR_OFF) { |
457f4436 | 2606 | verbose(env, "R%d unbounded memory access, make sure to bounds check any such access\n", |
dbcfe5f7 GB |
2607 | regno); |
2608 | return -EACCES; | |
2609 | } | |
457f4436 AN |
2610 | err = __check_mem_access(env, regno, reg->umax_value + off, size, |
2611 | mem_size, zero_size_allowed); | |
2612 | if (err) { | |
2613 | verbose(env, "R%d max value is outside of the allowed memory range\n", | |
61bd5218 | 2614 | regno); |
457f4436 AN |
2615 | return err; |
2616 | } | |
2617 | ||
2618 | return 0; | |
2619 | } | |
d83525ca | 2620 | |
457f4436 AN |
2621 | /* check read/write into a map element with possible variable offset */ |
2622 | static int check_map_access(struct bpf_verifier_env *env, u32 regno, | |
2623 | int off, int size, bool zero_size_allowed) | |
2624 | { | |
2625 | struct bpf_verifier_state *vstate = env->cur_state; | |
2626 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
2627 | struct bpf_reg_state *reg = &state->regs[regno]; | |
2628 | struct bpf_map *map = reg->map_ptr; | |
2629 | int err; | |
2630 | ||
2631 | err = check_mem_region_access(env, regno, off, size, map->value_size, | |
2632 | zero_size_allowed); | |
2633 | if (err) | |
2634 | return err; | |
2635 | ||
2636 | if (map_value_has_spin_lock(map)) { | |
2637 | u32 lock = map->spin_lock_off; | |
d83525ca AS |
2638 | |
2639 | /* if any part of struct bpf_spin_lock can be touched by | |
2640 | * load/store reject this program. | |
2641 | * To check that [x1, x2) overlaps with [y1, y2) | |
2642 | * it is sufficient to check x1 < y2 && y1 < x2. | |
2643 | */ | |
2644 | if (reg->smin_value + off < lock + sizeof(struct bpf_spin_lock) && | |
2645 | lock < reg->umax_value + off + size) { | |
2646 | verbose(env, "bpf_spin_lock cannot be accessed directly by load/store\n"); | |
2647 | return -EACCES; | |
2648 | } | |
2649 | } | |
f1174f77 | 2650 | return err; |
dbcfe5f7 GB |
2651 | } |
2652 | ||
969bf05e AS |
2653 | #define MAX_PACKET_OFF 0xffff |
2654 | ||
7e40781c UP |
2655 | static enum bpf_prog_type resolve_prog_type(struct bpf_prog *prog) |
2656 | { | |
3aac1ead | 2657 | return prog->aux->dst_prog ? prog->aux->dst_prog->type : prog->type; |
7e40781c UP |
2658 | } |
2659 | ||
58e2af8b | 2660 | static bool may_access_direct_pkt_data(struct bpf_verifier_env *env, |
3a0af8fd TG |
2661 | const struct bpf_call_arg_meta *meta, |
2662 | enum bpf_access_type t) | |
4acf6c0b | 2663 | { |
7e40781c UP |
2664 | enum bpf_prog_type prog_type = resolve_prog_type(env->prog); |
2665 | ||
2666 | switch (prog_type) { | |
5d66fa7d | 2667 | /* Program types only with direct read access go here! */ |
3a0af8fd TG |
2668 | case BPF_PROG_TYPE_LWT_IN: |
2669 | case BPF_PROG_TYPE_LWT_OUT: | |
004d4b27 | 2670 | case BPF_PROG_TYPE_LWT_SEG6LOCAL: |
2dbb9b9e | 2671 | case BPF_PROG_TYPE_SK_REUSEPORT: |
5d66fa7d | 2672 | case BPF_PROG_TYPE_FLOW_DISSECTOR: |
d5563d36 | 2673 | case BPF_PROG_TYPE_CGROUP_SKB: |
3a0af8fd TG |
2674 | if (t == BPF_WRITE) |
2675 | return false; | |
8731745e | 2676 | fallthrough; |
5d66fa7d DB |
2677 | |
2678 | /* Program types with direct read + write access go here! */ | |
36bbef52 DB |
2679 | case BPF_PROG_TYPE_SCHED_CLS: |
2680 | case BPF_PROG_TYPE_SCHED_ACT: | |
4acf6c0b | 2681 | case BPF_PROG_TYPE_XDP: |
3a0af8fd | 2682 | case BPF_PROG_TYPE_LWT_XMIT: |
8a31db56 | 2683 | case BPF_PROG_TYPE_SK_SKB: |
4f738adb | 2684 | case BPF_PROG_TYPE_SK_MSG: |
36bbef52 DB |
2685 | if (meta) |
2686 | return meta->pkt_access; | |
2687 | ||
2688 | env->seen_direct_write = true; | |
4acf6c0b | 2689 | return true; |
0d01da6a SF |
2690 | |
2691 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: | |
2692 | if (t == BPF_WRITE) | |
2693 | env->seen_direct_write = true; | |
2694 | ||
2695 | return true; | |
2696 | ||
4acf6c0b BB |
2697 | default: |
2698 | return false; | |
2699 | } | |
2700 | } | |
2701 | ||
f1174f77 | 2702 | static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off, |
9fd29c08 | 2703 | int size, bool zero_size_allowed) |
f1174f77 | 2704 | { |
638f5b90 | 2705 | struct bpf_reg_state *regs = cur_regs(env); |
f1174f77 EC |
2706 | struct bpf_reg_state *reg = ®s[regno]; |
2707 | int err; | |
2708 | ||
2709 | /* We may have added a variable offset to the packet pointer; but any | |
2710 | * reg->range we have comes after that. We are only checking the fixed | |
2711 | * offset. | |
2712 | */ | |
2713 | ||
2714 | /* We don't allow negative numbers, because we aren't tracking enough | |
2715 | * detail to prove they're safe. | |
2716 | */ | |
b03c9f9f | 2717 | if (reg->smin_value < 0) { |
61bd5218 | 2718 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
f1174f77 EC |
2719 | regno); |
2720 | return -EACCES; | |
2721 | } | |
457f4436 AN |
2722 | err = __check_mem_access(env, regno, off, size, reg->range, |
2723 | zero_size_allowed); | |
f1174f77 | 2724 | if (err) { |
61bd5218 | 2725 | verbose(env, "R%d offset is outside of the packet\n", regno); |
f1174f77 EC |
2726 | return err; |
2727 | } | |
e647815a | 2728 | |
457f4436 | 2729 | /* __check_mem_access has made sure "off + size - 1" is within u16. |
e647815a JW |
2730 | * reg->umax_value can't be bigger than MAX_PACKET_OFF which is 0xffff, |
2731 | * otherwise find_good_pkt_pointers would have refused to set range info | |
457f4436 | 2732 | * that __check_mem_access would have rejected this pkt access. |
e647815a JW |
2733 | * Therefore, "off + reg->umax_value + size - 1" won't overflow u32. |
2734 | */ | |
2735 | env->prog->aux->max_pkt_offset = | |
2736 | max_t(u32, env->prog->aux->max_pkt_offset, | |
2737 | off + reg->umax_value + size - 1); | |
2738 | ||
f1174f77 EC |
2739 | return err; |
2740 | } | |
2741 | ||
2742 | /* check access to 'struct bpf_context' fields. Supports fixed offsets only */ | |
31fd8581 | 2743 | static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off, int size, |
9e15db66 AS |
2744 | enum bpf_access_type t, enum bpf_reg_type *reg_type, |
2745 | u32 *btf_id) | |
17a52670 | 2746 | { |
f96da094 DB |
2747 | struct bpf_insn_access_aux info = { |
2748 | .reg_type = *reg_type, | |
9e15db66 | 2749 | .log = &env->log, |
f96da094 | 2750 | }; |
31fd8581 | 2751 | |
4f9218aa | 2752 | if (env->ops->is_valid_access && |
5e43f899 | 2753 | env->ops->is_valid_access(off, size, t, env->prog, &info)) { |
f96da094 DB |
2754 | /* A non zero info.ctx_field_size indicates that this field is a |
2755 | * candidate for later verifier transformation to load the whole | |
2756 | * field and then apply a mask when accessed with a narrower | |
2757 | * access than actual ctx access size. A zero info.ctx_field_size | |
2758 | * will only allow for whole field access and rejects any other | |
2759 | * type of narrower access. | |
31fd8581 | 2760 | */ |
23994631 | 2761 | *reg_type = info.reg_type; |
31fd8581 | 2762 | |
b121b341 | 2763 | if (*reg_type == PTR_TO_BTF_ID || *reg_type == PTR_TO_BTF_ID_OR_NULL) |
9e15db66 AS |
2764 | *btf_id = info.btf_id; |
2765 | else | |
2766 | env->insn_aux_data[insn_idx].ctx_field_size = info.ctx_field_size; | |
32bbe007 AS |
2767 | /* remember the offset of last byte accessed in ctx */ |
2768 | if (env->prog->aux->max_ctx_offset < off + size) | |
2769 | env->prog->aux->max_ctx_offset = off + size; | |
17a52670 | 2770 | return 0; |
32bbe007 | 2771 | } |
17a52670 | 2772 | |
61bd5218 | 2773 | verbose(env, "invalid bpf_context access off=%d size=%d\n", off, size); |
17a52670 AS |
2774 | return -EACCES; |
2775 | } | |
2776 | ||
d58e468b PP |
2777 | static int check_flow_keys_access(struct bpf_verifier_env *env, int off, |
2778 | int size) | |
2779 | { | |
2780 | if (size < 0 || off < 0 || | |
2781 | (u64)off + size > sizeof(struct bpf_flow_keys)) { | |
2782 | verbose(env, "invalid access to flow keys off=%d size=%d\n", | |
2783 | off, size); | |
2784 | return -EACCES; | |
2785 | } | |
2786 | return 0; | |
2787 | } | |
2788 | ||
5f456649 MKL |
2789 | static int check_sock_access(struct bpf_verifier_env *env, int insn_idx, |
2790 | u32 regno, int off, int size, | |
2791 | enum bpf_access_type t) | |
c64b7983 JS |
2792 | { |
2793 | struct bpf_reg_state *regs = cur_regs(env); | |
2794 | struct bpf_reg_state *reg = ®s[regno]; | |
5f456649 | 2795 | struct bpf_insn_access_aux info = {}; |
46f8bc92 | 2796 | bool valid; |
c64b7983 JS |
2797 | |
2798 | if (reg->smin_value < 0) { | |
2799 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", | |
2800 | regno); | |
2801 | return -EACCES; | |
2802 | } | |
2803 | ||
46f8bc92 MKL |
2804 | switch (reg->type) { |
2805 | case PTR_TO_SOCK_COMMON: | |
2806 | valid = bpf_sock_common_is_valid_access(off, size, t, &info); | |
2807 | break; | |
2808 | case PTR_TO_SOCKET: | |
2809 | valid = bpf_sock_is_valid_access(off, size, t, &info); | |
2810 | break; | |
655a51e5 MKL |
2811 | case PTR_TO_TCP_SOCK: |
2812 | valid = bpf_tcp_sock_is_valid_access(off, size, t, &info); | |
2813 | break; | |
fada7fdc JL |
2814 | case PTR_TO_XDP_SOCK: |
2815 | valid = bpf_xdp_sock_is_valid_access(off, size, t, &info); | |
2816 | break; | |
46f8bc92 MKL |
2817 | default: |
2818 | valid = false; | |
c64b7983 JS |
2819 | } |
2820 | ||
5f456649 | 2821 | |
46f8bc92 MKL |
2822 | if (valid) { |
2823 | env->insn_aux_data[insn_idx].ctx_field_size = | |
2824 | info.ctx_field_size; | |
2825 | return 0; | |
2826 | } | |
2827 | ||
2828 | verbose(env, "R%d invalid %s access off=%d size=%d\n", | |
2829 | regno, reg_type_str[reg->type], off, size); | |
2830 | ||
2831 | return -EACCES; | |
c64b7983 JS |
2832 | } |
2833 | ||
2a159c6f DB |
2834 | static struct bpf_reg_state *reg_state(struct bpf_verifier_env *env, int regno) |
2835 | { | |
2836 | return cur_regs(env) + regno; | |
2837 | } | |
2838 | ||
4cabc5b1 DB |
2839 | static bool is_pointer_value(struct bpf_verifier_env *env, int regno) |
2840 | { | |
2a159c6f | 2841 | return __is_pointer_value(env->allow_ptr_leaks, reg_state(env, regno)); |
4cabc5b1 DB |
2842 | } |
2843 | ||
f37a8cb8 DB |
2844 | static bool is_ctx_reg(struct bpf_verifier_env *env, int regno) |
2845 | { | |
2a159c6f | 2846 | const struct bpf_reg_state *reg = reg_state(env, regno); |
f37a8cb8 | 2847 | |
46f8bc92 MKL |
2848 | return reg->type == PTR_TO_CTX; |
2849 | } | |
2850 | ||
2851 | static bool is_sk_reg(struct bpf_verifier_env *env, int regno) | |
2852 | { | |
2853 | const struct bpf_reg_state *reg = reg_state(env, regno); | |
2854 | ||
2855 | return type_is_sk_pointer(reg->type); | |
f37a8cb8 DB |
2856 | } |
2857 | ||
ca369602 DB |
2858 | static bool is_pkt_reg(struct bpf_verifier_env *env, int regno) |
2859 | { | |
2a159c6f | 2860 | const struct bpf_reg_state *reg = reg_state(env, regno); |
ca369602 DB |
2861 | |
2862 | return type_is_pkt_pointer(reg->type); | |
2863 | } | |
2864 | ||
4b5defde DB |
2865 | static bool is_flow_key_reg(struct bpf_verifier_env *env, int regno) |
2866 | { | |
2867 | const struct bpf_reg_state *reg = reg_state(env, regno); | |
2868 | ||
2869 | /* Separate to is_ctx_reg() since we still want to allow BPF_ST here. */ | |
2870 | return reg->type == PTR_TO_FLOW_KEYS; | |
2871 | } | |
2872 | ||
61bd5218 JK |
2873 | static int check_pkt_ptr_alignment(struct bpf_verifier_env *env, |
2874 | const struct bpf_reg_state *reg, | |
d1174416 | 2875 | int off, int size, bool strict) |
969bf05e | 2876 | { |
f1174f77 | 2877 | struct tnum reg_off; |
e07b98d9 | 2878 | int ip_align; |
d1174416 DM |
2879 | |
2880 | /* Byte size accesses are always allowed. */ | |
2881 | if (!strict || size == 1) | |
2882 | return 0; | |
2883 | ||
e4eda884 DM |
2884 | /* For platforms that do not have a Kconfig enabling |
2885 | * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of | |
2886 | * NET_IP_ALIGN is universally set to '2'. And on platforms | |
2887 | * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get | |
2888 | * to this code only in strict mode where we want to emulate | |
2889 | * the NET_IP_ALIGN==2 checking. Therefore use an | |
2890 | * unconditional IP align value of '2'. | |
e07b98d9 | 2891 | */ |
e4eda884 | 2892 | ip_align = 2; |
f1174f77 EC |
2893 | |
2894 | reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off)); | |
2895 | if (!tnum_is_aligned(reg_off, size)) { | |
2896 | char tn_buf[48]; | |
2897 | ||
2898 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
61bd5218 JK |
2899 | verbose(env, |
2900 | "misaligned packet access off %d+%s+%d+%d size %d\n", | |
f1174f77 | 2901 | ip_align, tn_buf, reg->off, off, size); |
969bf05e AS |
2902 | return -EACCES; |
2903 | } | |
79adffcd | 2904 | |
969bf05e AS |
2905 | return 0; |
2906 | } | |
2907 | ||
61bd5218 JK |
2908 | static int check_generic_ptr_alignment(struct bpf_verifier_env *env, |
2909 | const struct bpf_reg_state *reg, | |
f1174f77 EC |
2910 | const char *pointer_desc, |
2911 | int off, int size, bool strict) | |
79adffcd | 2912 | { |
f1174f77 EC |
2913 | struct tnum reg_off; |
2914 | ||
2915 | /* Byte size accesses are always allowed. */ | |
2916 | if (!strict || size == 1) | |
2917 | return 0; | |
2918 | ||
2919 | reg_off = tnum_add(reg->var_off, tnum_const(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 | 2924 | verbose(env, "misaligned %saccess off %s+%d+%d size %d\n", |
f1174f77 | 2925 | pointer_desc, tn_buf, reg->off, off, size); |
79adffcd DB |
2926 | return -EACCES; |
2927 | } | |
2928 | ||
969bf05e AS |
2929 | return 0; |
2930 | } | |
2931 | ||
e07b98d9 | 2932 | static int check_ptr_alignment(struct bpf_verifier_env *env, |
ca369602 DB |
2933 | const struct bpf_reg_state *reg, int off, |
2934 | int size, bool strict_alignment_once) | |
79adffcd | 2935 | { |
ca369602 | 2936 | bool strict = env->strict_alignment || strict_alignment_once; |
f1174f77 | 2937 | const char *pointer_desc = ""; |
d1174416 | 2938 | |
79adffcd DB |
2939 | switch (reg->type) { |
2940 | case PTR_TO_PACKET: | |
de8f3a83 DB |
2941 | case PTR_TO_PACKET_META: |
2942 | /* Special case, because of NET_IP_ALIGN. Given metadata sits | |
2943 | * right in front, treat it the very same way. | |
2944 | */ | |
61bd5218 | 2945 | return check_pkt_ptr_alignment(env, reg, off, size, strict); |
d58e468b PP |
2946 | case PTR_TO_FLOW_KEYS: |
2947 | pointer_desc = "flow keys "; | |
2948 | break; | |
f1174f77 EC |
2949 | case PTR_TO_MAP_VALUE: |
2950 | pointer_desc = "value "; | |
2951 | break; | |
2952 | case PTR_TO_CTX: | |
2953 | pointer_desc = "context "; | |
2954 | break; | |
2955 | case PTR_TO_STACK: | |
2956 | pointer_desc = "stack "; | |
a5ec6ae1 JH |
2957 | /* The stack spill tracking logic in check_stack_write() |
2958 | * and check_stack_read() relies on stack accesses being | |
2959 | * aligned. | |
2960 | */ | |
2961 | strict = true; | |
f1174f77 | 2962 | break; |
c64b7983 JS |
2963 | case PTR_TO_SOCKET: |
2964 | pointer_desc = "sock "; | |
2965 | break; | |
46f8bc92 MKL |
2966 | case PTR_TO_SOCK_COMMON: |
2967 | pointer_desc = "sock_common "; | |
2968 | break; | |
655a51e5 MKL |
2969 | case PTR_TO_TCP_SOCK: |
2970 | pointer_desc = "tcp_sock "; | |
2971 | break; | |
fada7fdc JL |
2972 | case PTR_TO_XDP_SOCK: |
2973 | pointer_desc = "xdp_sock "; | |
2974 | break; | |
79adffcd | 2975 | default: |
f1174f77 | 2976 | break; |
79adffcd | 2977 | } |
61bd5218 JK |
2978 | return check_generic_ptr_alignment(env, reg, pointer_desc, off, size, |
2979 | strict); | |
79adffcd DB |
2980 | } |
2981 | ||
f4d7e40a AS |
2982 | static int update_stack_depth(struct bpf_verifier_env *env, |
2983 | const struct bpf_func_state *func, | |
2984 | int off) | |
2985 | { | |
9c8105bd | 2986 | u16 stack = env->subprog_info[func->subprogno].stack_depth; |
f4d7e40a AS |
2987 | |
2988 | if (stack >= -off) | |
2989 | return 0; | |
2990 | ||
2991 | /* update known max for given subprogram */ | |
9c8105bd | 2992 | env->subprog_info[func->subprogno].stack_depth = -off; |
70a87ffe AS |
2993 | return 0; |
2994 | } | |
f4d7e40a | 2995 | |
70a87ffe AS |
2996 | /* starting from main bpf function walk all instructions of the function |
2997 | * and recursively walk all callees that given function can call. | |
2998 | * Ignore jump and exit insns. | |
2999 | * Since recursion is prevented by check_cfg() this algorithm | |
3000 | * only needs a local stack of MAX_CALL_FRAMES to remember callsites | |
3001 | */ | |
3002 | static int check_max_stack_depth(struct bpf_verifier_env *env) | |
3003 | { | |
9c8105bd JW |
3004 | int depth = 0, frame = 0, idx = 0, i = 0, subprog_end; |
3005 | struct bpf_subprog_info *subprog = env->subprog_info; | |
70a87ffe | 3006 | struct bpf_insn *insn = env->prog->insnsi; |
ebf7d1f5 | 3007 | bool tail_call_reachable = false; |
70a87ffe AS |
3008 | int ret_insn[MAX_CALL_FRAMES]; |
3009 | int ret_prog[MAX_CALL_FRAMES]; | |
ebf7d1f5 | 3010 | int j; |
f4d7e40a | 3011 | |
70a87ffe | 3012 | process_func: |
7f6e4312 MF |
3013 | /* protect against potential stack overflow that might happen when |
3014 | * bpf2bpf calls get combined with tailcalls. Limit the caller's stack | |
3015 | * depth for such case down to 256 so that the worst case scenario | |
3016 | * would result in 8k stack size (32 which is tailcall limit * 256 = | |
3017 | * 8k). | |
3018 | * | |
3019 | * To get the idea what might happen, see an example: | |
3020 | * func1 -> sub rsp, 128 | |
3021 | * subfunc1 -> sub rsp, 256 | |
3022 | * tailcall1 -> add rsp, 256 | |
3023 | * func2 -> sub rsp, 192 (total stack size = 128 + 192 = 320) | |
3024 | * subfunc2 -> sub rsp, 64 | |
3025 | * subfunc22 -> sub rsp, 128 | |
3026 | * tailcall2 -> add rsp, 128 | |
3027 | * func3 -> sub rsp, 32 (total stack size 128 + 192 + 64 + 32 = 416) | |
3028 | * | |
3029 | * tailcall will unwind the current stack frame but it will not get rid | |
3030 | * of caller's stack as shown on the example above. | |
3031 | */ | |
3032 | if (idx && subprog[idx].has_tail_call && depth >= 256) { | |
3033 | verbose(env, | |
3034 | "tail_calls are not allowed when call stack of previous frames is %d bytes. Too large\n", | |
3035 | depth); | |
3036 | return -EACCES; | |
3037 | } | |
70a87ffe AS |
3038 | /* round up to 32-bytes, since this is granularity |
3039 | * of interpreter stack size | |
3040 | */ | |
9c8105bd | 3041 | depth += round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
70a87ffe | 3042 | if (depth > MAX_BPF_STACK) { |
f4d7e40a | 3043 | verbose(env, "combined stack size of %d calls is %d. Too large\n", |
70a87ffe | 3044 | frame + 1, depth); |
f4d7e40a AS |
3045 | return -EACCES; |
3046 | } | |
70a87ffe | 3047 | continue_func: |
4cb3d99c | 3048 | subprog_end = subprog[idx + 1].start; |
70a87ffe AS |
3049 | for (; i < subprog_end; i++) { |
3050 | if (insn[i].code != (BPF_JMP | BPF_CALL)) | |
3051 | continue; | |
3052 | if (insn[i].src_reg != BPF_PSEUDO_CALL) | |
3053 | continue; | |
3054 | /* remember insn and function to return to */ | |
3055 | ret_insn[frame] = i + 1; | |
9c8105bd | 3056 | ret_prog[frame] = idx; |
70a87ffe AS |
3057 | |
3058 | /* find the callee */ | |
3059 | i = i + insn[i].imm + 1; | |
9c8105bd JW |
3060 | idx = find_subprog(env, i); |
3061 | if (idx < 0) { | |
70a87ffe AS |
3062 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
3063 | i); | |
3064 | return -EFAULT; | |
3065 | } | |
ebf7d1f5 MF |
3066 | |
3067 | if (subprog[idx].has_tail_call) | |
3068 | tail_call_reachable = true; | |
3069 | ||
70a87ffe AS |
3070 | frame++; |
3071 | if (frame >= MAX_CALL_FRAMES) { | |
927cb781 PC |
3072 | verbose(env, "the call stack of %d frames is too deep !\n", |
3073 | frame); | |
3074 | return -E2BIG; | |
70a87ffe AS |
3075 | } |
3076 | goto process_func; | |
3077 | } | |
ebf7d1f5 MF |
3078 | /* if tail call got detected across bpf2bpf calls then mark each of the |
3079 | * currently present subprog frames as tail call reachable subprogs; | |
3080 | * this info will be utilized by JIT so that we will be preserving the | |
3081 | * tail call counter throughout bpf2bpf calls combined with tailcalls | |
3082 | */ | |
3083 | if (tail_call_reachable) | |
3084 | for (j = 0; j < frame; j++) | |
3085 | subprog[ret_prog[j]].tail_call_reachable = true; | |
3086 | ||
70a87ffe AS |
3087 | /* end of for() loop means the last insn of the 'subprog' |
3088 | * was reached. Doesn't matter whether it was JA or EXIT | |
3089 | */ | |
3090 | if (frame == 0) | |
3091 | return 0; | |
9c8105bd | 3092 | depth -= round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
70a87ffe AS |
3093 | frame--; |
3094 | i = ret_insn[frame]; | |
9c8105bd | 3095 | idx = ret_prog[frame]; |
70a87ffe | 3096 | goto continue_func; |
f4d7e40a AS |
3097 | } |
3098 | ||
19d28fbd | 3099 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
1ea47e01 AS |
3100 | static int get_callee_stack_depth(struct bpf_verifier_env *env, |
3101 | const struct bpf_insn *insn, int idx) | |
3102 | { | |
3103 | int start = idx + insn->imm + 1, subprog; | |
3104 | ||
3105 | subprog = find_subprog(env, start); | |
3106 | if (subprog < 0) { | |
3107 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", | |
3108 | start); | |
3109 | return -EFAULT; | |
3110 | } | |
9c8105bd | 3111 | return env->subprog_info[subprog].stack_depth; |
1ea47e01 | 3112 | } |
19d28fbd | 3113 | #endif |
1ea47e01 | 3114 | |
51c39bb1 AS |
3115 | int check_ctx_reg(struct bpf_verifier_env *env, |
3116 | const struct bpf_reg_state *reg, int regno) | |
58990d1f DB |
3117 | { |
3118 | /* Access to ctx or passing it to a helper is only allowed in | |
3119 | * its original, unmodified form. | |
3120 | */ | |
3121 | ||
3122 | if (reg->off) { | |
3123 | verbose(env, "dereference of modified ctx ptr R%d off=%d disallowed\n", | |
3124 | regno, reg->off); | |
3125 | return -EACCES; | |
3126 | } | |
3127 | ||
3128 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
3129 | char tn_buf[48]; | |
3130 | ||
3131 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
3132 | verbose(env, "variable ctx access var_off=%s disallowed\n", tn_buf); | |
3133 | return -EACCES; | |
3134 | } | |
3135 | ||
3136 | return 0; | |
3137 | } | |
3138 | ||
afbf21dc YS |
3139 | static int __check_buffer_access(struct bpf_verifier_env *env, |
3140 | const char *buf_info, | |
3141 | const struct bpf_reg_state *reg, | |
3142 | int regno, int off, int size) | |
9df1c28b MM |
3143 | { |
3144 | if (off < 0) { | |
3145 | verbose(env, | |
4fc00b79 | 3146 | "R%d invalid %s buffer access: off=%d, size=%d\n", |
afbf21dc | 3147 | regno, buf_info, off, size); |
9df1c28b MM |
3148 | return -EACCES; |
3149 | } | |
3150 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
3151 | char tn_buf[48]; | |
3152 | ||
3153 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
3154 | verbose(env, | |
4fc00b79 | 3155 | "R%d invalid variable buffer offset: off=%d, var_off=%s\n", |
9df1c28b MM |
3156 | regno, off, tn_buf); |
3157 | return -EACCES; | |
3158 | } | |
afbf21dc YS |
3159 | |
3160 | return 0; | |
3161 | } | |
3162 | ||
3163 | static int check_tp_buffer_access(struct bpf_verifier_env *env, | |
3164 | const struct bpf_reg_state *reg, | |
3165 | int regno, int off, int size) | |
3166 | { | |
3167 | int err; | |
3168 | ||
3169 | err = __check_buffer_access(env, "tracepoint", reg, regno, off, size); | |
3170 | if (err) | |
3171 | return err; | |
3172 | ||
9df1c28b MM |
3173 | if (off + size > env->prog->aux->max_tp_access) |
3174 | env->prog->aux->max_tp_access = off + size; | |
3175 | ||
3176 | return 0; | |
3177 | } | |
3178 | ||
afbf21dc YS |
3179 | static int check_buffer_access(struct bpf_verifier_env *env, |
3180 | const struct bpf_reg_state *reg, | |
3181 | int regno, int off, int size, | |
3182 | bool zero_size_allowed, | |
3183 | const char *buf_info, | |
3184 | u32 *max_access) | |
3185 | { | |
3186 | int err; | |
3187 | ||
3188 | err = __check_buffer_access(env, buf_info, reg, regno, off, size); | |
3189 | if (err) | |
3190 | return err; | |
3191 | ||
3192 | if (off + size > *max_access) | |
3193 | *max_access = off + size; | |
3194 | ||
3195 | return 0; | |
3196 | } | |
3197 | ||
3f50f132 JF |
3198 | /* BPF architecture zero extends alu32 ops into 64-bit registesr */ |
3199 | static void zext_32_to_64(struct bpf_reg_state *reg) | |
3200 | { | |
3201 | reg->var_off = tnum_subreg(reg->var_off); | |
3202 | __reg_assign_32_into_64(reg); | |
3203 | } | |
9df1c28b | 3204 | |
0c17d1d2 JH |
3205 | /* truncate register to smaller size (in bytes) |
3206 | * must be called with size < BPF_REG_SIZE | |
3207 | */ | |
3208 | static void coerce_reg_to_size(struct bpf_reg_state *reg, int size) | |
3209 | { | |
3210 | u64 mask; | |
3211 | ||
3212 | /* clear high bits in bit representation */ | |
3213 | reg->var_off = tnum_cast(reg->var_off, size); | |
3214 | ||
3215 | /* fix arithmetic bounds */ | |
3216 | mask = ((u64)1 << (size * 8)) - 1; | |
3217 | if ((reg->umin_value & ~mask) == (reg->umax_value & ~mask)) { | |
3218 | reg->umin_value &= mask; | |
3219 | reg->umax_value &= mask; | |
3220 | } else { | |
3221 | reg->umin_value = 0; | |
3222 | reg->umax_value = mask; | |
3223 | } | |
3224 | reg->smin_value = reg->umin_value; | |
3225 | reg->smax_value = reg->umax_value; | |
3f50f132 JF |
3226 | |
3227 | /* If size is smaller than 32bit register the 32bit register | |
3228 | * values are also truncated so we push 64-bit bounds into | |
3229 | * 32-bit bounds. Above were truncated < 32-bits already. | |
3230 | */ | |
3231 | if (size >= 4) | |
3232 | return; | |
3233 | __reg_combine_64_into_32(reg); | |
0c17d1d2 JH |
3234 | } |
3235 | ||
a23740ec AN |
3236 | static bool bpf_map_is_rdonly(const struct bpf_map *map) |
3237 | { | |
3238 | return (map->map_flags & BPF_F_RDONLY_PROG) && map->frozen; | |
3239 | } | |
3240 | ||
3241 | static int bpf_map_direct_read(struct bpf_map *map, int off, int size, u64 *val) | |
3242 | { | |
3243 | void *ptr; | |
3244 | u64 addr; | |
3245 | int err; | |
3246 | ||
3247 | err = map->ops->map_direct_value_addr(map, &addr, off); | |
3248 | if (err) | |
3249 | return err; | |
2dedd7d2 | 3250 | ptr = (void *)(long)addr + off; |
a23740ec AN |
3251 | |
3252 | switch (size) { | |
3253 | case sizeof(u8): | |
3254 | *val = (u64)*(u8 *)ptr; | |
3255 | break; | |
3256 | case sizeof(u16): | |
3257 | *val = (u64)*(u16 *)ptr; | |
3258 | break; | |
3259 | case sizeof(u32): | |
3260 | *val = (u64)*(u32 *)ptr; | |
3261 | break; | |
3262 | case sizeof(u64): | |
3263 | *val = *(u64 *)ptr; | |
3264 | break; | |
3265 | default: | |
3266 | return -EINVAL; | |
3267 | } | |
3268 | return 0; | |
3269 | } | |
3270 | ||
9e15db66 AS |
3271 | static int check_ptr_to_btf_access(struct bpf_verifier_env *env, |
3272 | struct bpf_reg_state *regs, | |
3273 | int regno, int off, int size, | |
3274 | enum bpf_access_type atype, | |
3275 | int value_regno) | |
3276 | { | |
3277 | struct bpf_reg_state *reg = regs + regno; | |
3278 | const struct btf_type *t = btf_type_by_id(btf_vmlinux, reg->btf_id); | |
3279 | const char *tname = btf_name_by_offset(btf_vmlinux, t->name_off); | |
3280 | u32 btf_id; | |
3281 | int ret; | |
3282 | ||
9e15db66 AS |
3283 | if (off < 0) { |
3284 | verbose(env, | |
3285 | "R%d is ptr_%s invalid negative access: off=%d\n", | |
3286 | regno, tname, off); | |
3287 | return -EACCES; | |
3288 | } | |
3289 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
3290 | char tn_buf[48]; | |
3291 | ||
3292 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
3293 | verbose(env, | |
3294 | "R%d is ptr_%s invalid variable offset: off=%d, var_off=%s\n", | |
3295 | regno, tname, off, tn_buf); | |
3296 | return -EACCES; | |
3297 | } | |
3298 | ||
27ae7997 MKL |
3299 | if (env->ops->btf_struct_access) { |
3300 | ret = env->ops->btf_struct_access(&env->log, t, off, size, | |
3301 | atype, &btf_id); | |
3302 | } else { | |
3303 | if (atype != BPF_READ) { | |
3304 | verbose(env, "only read is supported\n"); | |
3305 | return -EACCES; | |
3306 | } | |
3307 | ||
3308 | ret = btf_struct_access(&env->log, t, off, size, atype, | |
3309 | &btf_id); | |
3310 | } | |
3311 | ||
9e15db66 AS |
3312 | if (ret < 0) |
3313 | return ret; | |
3314 | ||
41c48f3a AI |
3315 | if (atype == BPF_READ && value_regno >= 0) |
3316 | mark_btf_ld_reg(env, regs, value_regno, ret, btf_id); | |
3317 | ||
3318 | return 0; | |
3319 | } | |
3320 | ||
3321 | static int check_ptr_to_map_access(struct bpf_verifier_env *env, | |
3322 | struct bpf_reg_state *regs, | |
3323 | int regno, int off, int size, | |
3324 | enum bpf_access_type atype, | |
3325 | int value_regno) | |
3326 | { | |
3327 | struct bpf_reg_state *reg = regs + regno; | |
3328 | struct bpf_map *map = reg->map_ptr; | |
3329 | const struct btf_type *t; | |
3330 | const char *tname; | |
3331 | u32 btf_id; | |
3332 | int ret; | |
3333 | ||
3334 | if (!btf_vmlinux) { | |
3335 | verbose(env, "map_ptr access not supported without CONFIG_DEBUG_INFO_BTF\n"); | |
3336 | return -ENOTSUPP; | |
3337 | } | |
3338 | ||
3339 | if (!map->ops->map_btf_id || !*map->ops->map_btf_id) { | |
3340 | verbose(env, "map_ptr access not supported for map type %d\n", | |
3341 | map->map_type); | |
3342 | return -ENOTSUPP; | |
3343 | } | |
3344 | ||
3345 | t = btf_type_by_id(btf_vmlinux, *map->ops->map_btf_id); | |
3346 | tname = btf_name_by_offset(btf_vmlinux, t->name_off); | |
3347 | ||
3348 | if (!env->allow_ptr_to_map_access) { | |
3349 | verbose(env, | |
3350 | "%s access is allowed only to CAP_PERFMON and CAP_SYS_ADMIN\n", | |
3351 | tname); | |
3352 | return -EPERM; | |
9e15db66 | 3353 | } |
27ae7997 | 3354 | |
41c48f3a AI |
3355 | if (off < 0) { |
3356 | verbose(env, "R%d is %s invalid negative access: off=%d\n", | |
3357 | regno, tname, off); | |
3358 | return -EACCES; | |
3359 | } | |
3360 | ||
3361 | if (atype != BPF_READ) { | |
3362 | verbose(env, "only read from %s is supported\n", tname); | |
3363 | return -EACCES; | |
3364 | } | |
3365 | ||
3366 | ret = btf_struct_access(&env->log, t, off, size, atype, &btf_id); | |
3367 | if (ret < 0) | |
3368 | return ret; | |
3369 | ||
3370 | if (value_regno >= 0) | |
3371 | mark_btf_ld_reg(env, regs, value_regno, ret, btf_id); | |
3372 | ||
9e15db66 AS |
3373 | return 0; |
3374 | } | |
3375 | ||
41c48f3a | 3376 | |
17a52670 AS |
3377 | /* check whether memory at (regno + off) is accessible for t = (read | write) |
3378 | * if t==write, value_regno is a register which value is stored into memory | |
3379 | * if t==read, value_regno is a register which will receive the value from memory | |
3380 | * if t==write && value_regno==-1, some unknown value is stored into memory | |
3381 | * if t==read && value_regno==-1, don't care what we read from memory | |
3382 | */ | |
ca369602 DB |
3383 | static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno, |
3384 | int off, int bpf_size, enum bpf_access_type t, | |
3385 | int value_regno, bool strict_alignment_once) | |
17a52670 | 3386 | { |
638f5b90 AS |
3387 | struct bpf_reg_state *regs = cur_regs(env); |
3388 | struct bpf_reg_state *reg = regs + regno; | |
f4d7e40a | 3389 | struct bpf_func_state *state; |
17a52670 AS |
3390 | int size, err = 0; |
3391 | ||
3392 | size = bpf_size_to_bytes(bpf_size); | |
3393 | if (size < 0) | |
3394 | return size; | |
3395 | ||
f1174f77 | 3396 | /* alignment checks will add in reg->off themselves */ |
ca369602 | 3397 | err = check_ptr_alignment(env, reg, off, size, strict_alignment_once); |
969bf05e AS |
3398 | if (err) |
3399 | return err; | |
17a52670 | 3400 | |
f1174f77 EC |
3401 | /* for access checks, reg->off is just part of off */ |
3402 | off += reg->off; | |
3403 | ||
3404 | if (reg->type == PTR_TO_MAP_VALUE) { | |
1be7f75d AS |
3405 | if (t == BPF_WRITE && value_regno >= 0 && |
3406 | is_pointer_value(env, value_regno)) { | |
61bd5218 | 3407 | verbose(env, "R%d leaks addr into map\n", value_regno); |
1be7f75d AS |
3408 | return -EACCES; |
3409 | } | |
591fe988 DB |
3410 | err = check_map_access_type(env, regno, off, size, t); |
3411 | if (err) | |
3412 | return err; | |
9fd29c08 | 3413 | err = check_map_access(env, regno, off, size, false); |
a23740ec AN |
3414 | if (!err && t == BPF_READ && value_regno >= 0) { |
3415 | struct bpf_map *map = reg->map_ptr; | |
3416 | ||
3417 | /* if map is read-only, track its contents as scalars */ | |
3418 | if (tnum_is_const(reg->var_off) && | |
3419 | bpf_map_is_rdonly(map) && | |
3420 | map->ops->map_direct_value_addr) { | |
3421 | int map_off = off + reg->var_off.value; | |
3422 | u64 val = 0; | |
3423 | ||
3424 | err = bpf_map_direct_read(map, map_off, size, | |
3425 | &val); | |
3426 | if (err) | |
3427 | return err; | |
3428 | ||
3429 | regs[value_regno].type = SCALAR_VALUE; | |
3430 | __mark_reg_known(®s[value_regno], val); | |
3431 | } else { | |
3432 | mark_reg_unknown(env, regs, value_regno); | |
3433 | } | |
3434 | } | |
457f4436 AN |
3435 | } else if (reg->type == PTR_TO_MEM) { |
3436 | if (t == BPF_WRITE && value_regno >= 0 && | |
3437 | is_pointer_value(env, value_regno)) { | |
3438 | verbose(env, "R%d leaks addr into mem\n", value_regno); | |
3439 | return -EACCES; | |
3440 | } | |
3441 | err = check_mem_region_access(env, regno, off, size, | |
3442 | reg->mem_size, false); | |
3443 | if (!err && t == BPF_READ && value_regno >= 0) | |
3444 | mark_reg_unknown(env, regs, value_regno); | |
1a0dc1ac | 3445 | } else if (reg->type == PTR_TO_CTX) { |
f1174f77 | 3446 | enum bpf_reg_type reg_type = SCALAR_VALUE; |
9e15db66 | 3447 | u32 btf_id = 0; |
19de99f7 | 3448 | |
1be7f75d AS |
3449 | if (t == BPF_WRITE && value_regno >= 0 && |
3450 | is_pointer_value(env, value_regno)) { | |
61bd5218 | 3451 | verbose(env, "R%d leaks addr into ctx\n", value_regno); |
1be7f75d AS |
3452 | return -EACCES; |
3453 | } | |
f1174f77 | 3454 | |
58990d1f DB |
3455 | err = check_ctx_reg(env, reg, regno); |
3456 | if (err < 0) | |
3457 | return err; | |
3458 | ||
9e15db66 AS |
3459 | err = check_ctx_access(env, insn_idx, off, size, t, ®_type, &btf_id); |
3460 | if (err) | |
3461 | verbose_linfo(env, insn_idx, "; "); | |
969bf05e | 3462 | if (!err && t == BPF_READ && value_regno >= 0) { |
f1174f77 | 3463 | /* ctx access returns either a scalar, or a |
de8f3a83 DB |
3464 | * PTR_TO_PACKET[_META,_END]. In the latter |
3465 | * case, we know the offset is zero. | |
f1174f77 | 3466 | */ |
46f8bc92 | 3467 | if (reg_type == SCALAR_VALUE) { |
638f5b90 | 3468 | mark_reg_unknown(env, regs, value_regno); |
46f8bc92 | 3469 | } else { |
638f5b90 | 3470 | mark_reg_known_zero(env, regs, |
61bd5218 | 3471 | value_regno); |
46f8bc92 MKL |
3472 | if (reg_type_may_be_null(reg_type)) |
3473 | regs[value_regno].id = ++env->id_gen; | |
5327ed3d JW |
3474 | /* A load of ctx field could have different |
3475 | * actual load size with the one encoded in the | |
3476 | * insn. When the dst is PTR, it is for sure not | |
3477 | * a sub-register. | |
3478 | */ | |
3479 | regs[value_regno].subreg_def = DEF_NOT_SUBREG; | |
b121b341 YS |
3480 | if (reg_type == PTR_TO_BTF_ID || |
3481 | reg_type == PTR_TO_BTF_ID_OR_NULL) | |
9e15db66 | 3482 | regs[value_regno].btf_id = btf_id; |
46f8bc92 | 3483 | } |
638f5b90 | 3484 | regs[value_regno].type = reg_type; |
969bf05e | 3485 | } |
17a52670 | 3486 | |
f1174f77 | 3487 | } else if (reg->type == PTR_TO_STACK) { |
f1174f77 | 3488 | off += reg->var_off.value; |
e4298d25 DB |
3489 | err = check_stack_access(env, reg, off, size); |
3490 | if (err) | |
3491 | return err; | |
8726679a | 3492 | |
f4d7e40a AS |
3493 | state = func(env, reg); |
3494 | err = update_stack_depth(env, state, off); | |
3495 | if (err) | |
3496 | return err; | |
8726679a | 3497 | |
638f5b90 | 3498 | if (t == BPF_WRITE) |
61bd5218 | 3499 | err = check_stack_write(env, state, off, size, |
af86ca4e | 3500 | value_regno, insn_idx); |
638f5b90 | 3501 | else |
61bd5218 JK |
3502 | err = check_stack_read(env, state, off, size, |
3503 | value_regno); | |
de8f3a83 | 3504 | } else if (reg_is_pkt_pointer(reg)) { |
3a0af8fd | 3505 | if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) { |
61bd5218 | 3506 | verbose(env, "cannot write into packet\n"); |
969bf05e AS |
3507 | return -EACCES; |
3508 | } | |
4acf6c0b BB |
3509 | if (t == BPF_WRITE && value_regno >= 0 && |
3510 | is_pointer_value(env, value_regno)) { | |
61bd5218 JK |
3511 | verbose(env, "R%d leaks addr into packet\n", |
3512 | value_regno); | |
4acf6c0b BB |
3513 | return -EACCES; |
3514 | } | |
9fd29c08 | 3515 | err = check_packet_access(env, regno, off, size, false); |
969bf05e | 3516 | if (!err && t == BPF_READ && value_regno >= 0) |
638f5b90 | 3517 | mark_reg_unknown(env, regs, value_regno); |
d58e468b PP |
3518 | } else if (reg->type == PTR_TO_FLOW_KEYS) { |
3519 | if (t == BPF_WRITE && value_regno >= 0 && | |
3520 | is_pointer_value(env, value_regno)) { | |
3521 | verbose(env, "R%d leaks addr into flow keys\n", | |
3522 | value_regno); | |
3523 | return -EACCES; | |
3524 | } | |
3525 | ||
3526 | err = check_flow_keys_access(env, off, size); | |
3527 | if (!err && t == BPF_READ && value_regno >= 0) | |
3528 | mark_reg_unknown(env, regs, value_regno); | |
46f8bc92 | 3529 | } else if (type_is_sk_pointer(reg->type)) { |
c64b7983 | 3530 | if (t == BPF_WRITE) { |
46f8bc92 MKL |
3531 | verbose(env, "R%d cannot write into %s\n", |
3532 | regno, reg_type_str[reg->type]); | |
c64b7983 JS |
3533 | return -EACCES; |
3534 | } | |
5f456649 | 3535 | err = check_sock_access(env, insn_idx, regno, off, size, t); |
c64b7983 JS |
3536 | if (!err && value_regno >= 0) |
3537 | mark_reg_unknown(env, regs, value_regno); | |
9df1c28b MM |
3538 | } else if (reg->type == PTR_TO_TP_BUFFER) { |
3539 | err = check_tp_buffer_access(env, reg, regno, off, size); | |
3540 | if (!err && t == BPF_READ && value_regno >= 0) | |
3541 | mark_reg_unknown(env, regs, value_regno); | |
9e15db66 AS |
3542 | } else if (reg->type == PTR_TO_BTF_ID) { |
3543 | err = check_ptr_to_btf_access(env, regs, regno, off, size, t, | |
3544 | value_regno); | |
41c48f3a AI |
3545 | } else if (reg->type == CONST_PTR_TO_MAP) { |
3546 | err = check_ptr_to_map_access(env, regs, regno, off, size, t, | |
3547 | value_regno); | |
afbf21dc YS |
3548 | } else if (reg->type == PTR_TO_RDONLY_BUF) { |
3549 | if (t == BPF_WRITE) { | |
3550 | verbose(env, "R%d cannot write into %s\n", | |
3551 | regno, reg_type_str[reg->type]); | |
3552 | return -EACCES; | |
3553 | } | |
f6dfbe31 CIK |
3554 | err = check_buffer_access(env, reg, regno, off, size, false, |
3555 | "rdonly", | |
afbf21dc YS |
3556 | &env->prog->aux->max_rdonly_access); |
3557 | if (!err && value_regno >= 0) | |
3558 | mark_reg_unknown(env, regs, value_regno); | |
3559 | } else if (reg->type == PTR_TO_RDWR_BUF) { | |
f6dfbe31 CIK |
3560 | err = check_buffer_access(env, reg, regno, off, size, false, |
3561 | "rdwr", | |
afbf21dc YS |
3562 | &env->prog->aux->max_rdwr_access); |
3563 | if (!err && t == BPF_READ && value_regno >= 0) | |
3564 | mark_reg_unknown(env, regs, value_regno); | |
17a52670 | 3565 | } else { |
61bd5218 JK |
3566 | verbose(env, "R%d invalid mem access '%s'\n", regno, |
3567 | reg_type_str[reg->type]); | |
17a52670 AS |
3568 | return -EACCES; |
3569 | } | |
969bf05e | 3570 | |
f1174f77 | 3571 | if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ && |
638f5b90 | 3572 | regs[value_regno].type == SCALAR_VALUE) { |
f1174f77 | 3573 | /* b/h/w load zero-extends, mark upper bits as known 0 */ |
0c17d1d2 | 3574 | coerce_reg_to_size(®s[value_regno], size); |
969bf05e | 3575 | } |
17a52670 AS |
3576 | return err; |
3577 | } | |
3578 | ||
31fd8581 | 3579 | static int check_xadd(struct bpf_verifier_env *env, int insn_idx, struct bpf_insn *insn) |
17a52670 | 3580 | { |
17a52670 AS |
3581 | int err; |
3582 | ||
3583 | if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) || | |
3584 | insn->imm != 0) { | |
61bd5218 | 3585 | verbose(env, "BPF_XADD uses reserved fields\n"); |
17a52670 AS |
3586 | return -EINVAL; |
3587 | } | |
3588 | ||
3589 | /* check src1 operand */ | |
dc503a8a | 3590 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
3591 | if (err) |
3592 | return err; | |
3593 | ||
3594 | /* check src2 operand */ | |
dc503a8a | 3595 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
3596 | if (err) |
3597 | return err; | |
3598 | ||
6bdf6abc | 3599 | if (is_pointer_value(env, insn->src_reg)) { |
61bd5218 | 3600 | verbose(env, "R%d leaks addr into mem\n", insn->src_reg); |
6bdf6abc DB |
3601 | return -EACCES; |
3602 | } | |
3603 | ||
ca369602 | 3604 | if (is_ctx_reg(env, insn->dst_reg) || |
4b5defde | 3605 | is_pkt_reg(env, insn->dst_reg) || |
46f8bc92 MKL |
3606 | is_flow_key_reg(env, insn->dst_reg) || |
3607 | is_sk_reg(env, insn->dst_reg)) { | |
ca369602 | 3608 | verbose(env, "BPF_XADD stores into R%d %s is not allowed\n", |
2a159c6f DB |
3609 | insn->dst_reg, |
3610 | reg_type_str[reg_state(env, insn->dst_reg)->type]); | |
f37a8cb8 DB |
3611 | return -EACCES; |
3612 | } | |
3613 | ||
17a52670 | 3614 | /* check whether atomic_add can read the memory */ |
31fd8581 | 3615 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
ca369602 | 3616 | BPF_SIZE(insn->code), BPF_READ, -1, true); |
17a52670 AS |
3617 | if (err) |
3618 | return err; | |
3619 | ||
3620 | /* check whether atomic_add can write into the same memory */ | |
31fd8581 | 3621 | return check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
ca369602 | 3622 | BPF_SIZE(insn->code), BPF_WRITE, -1, true); |
17a52670 AS |
3623 | } |
3624 | ||
2011fccf AI |
3625 | static int __check_stack_boundary(struct bpf_verifier_env *env, u32 regno, |
3626 | int off, int access_size, | |
3627 | bool zero_size_allowed) | |
3628 | { | |
3629 | struct bpf_reg_state *reg = reg_state(env, regno); | |
3630 | ||
3631 | if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 || | |
3632 | access_size < 0 || (access_size == 0 && !zero_size_allowed)) { | |
3633 | if (tnum_is_const(reg->var_off)) { | |
3634 | verbose(env, "invalid stack type R%d off=%d access_size=%d\n", | |
3635 | regno, off, access_size); | |
3636 | } else { | |
3637 | char tn_buf[48]; | |
3638 | ||
3639 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
3640 | verbose(env, "invalid stack type R%d var_off=%s access_size=%d\n", | |
3641 | regno, tn_buf, access_size); | |
3642 | } | |
3643 | return -EACCES; | |
3644 | } | |
3645 | return 0; | |
3646 | } | |
3647 | ||
17a52670 AS |
3648 | /* when register 'regno' is passed into function that will read 'access_size' |
3649 | * bytes from that pointer, make sure that it's within stack boundary | |
f1174f77 EC |
3650 | * and all elements of stack are initialized. |
3651 | * Unlike most pointer bounds-checking functions, this one doesn't take an | |
3652 | * 'off' argument, so it has to add in reg->off itself. | |
17a52670 | 3653 | */ |
58e2af8b | 3654 | static int check_stack_boundary(struct bpf_verifier_env *env, int regno, |
435faee1 DB |
3655 | int access_size, bool zero_size_allowed, |
3656 | struct bpf_call_arg_meta *meta) | |
17a52670 | 3657 | { |
2a159c6f | 3658 | struct bpf_reg_state *reg = reg_state(env, regno); |
f4d7e40a | 3659 | struct bpf_func_state *state = func(env, reg); |
f7cf25b2 | 3660 | int err, min_off, max_off, i, j, slot, spi; |
17a52670 | 3661 | |
2011fccf AI |
3662 | if (tnum_is_const(reg->var_off)) { |
3663 | min_off = max_off = reg->var_off.value + reg->off; | |
3664 | err = __check_stack_boundary(env, regno, min_off, access_size, | |
3665 | zero_size_allowed); | |
3666 | if (err) | |
3667 | return err; | |
3668 | } else { | |
088ec26d AI |
3669 | /* Variable offset is prohibited for unprivileged mode for |
3670 | * simplicity since it requires corresponding support in | |
3671 | * Spectre masking for stack ALU. | |
3672 | * See also retrieve_ptr_limit(). | |
3673 | */ | |
2c78ee89 | 3674 | if (!env->bypass_spec_v1) { |
088ec26d | 3675 | char tn_buf[48]; |
f1174f77 | 3676 | |
088ec26d AI |
3677 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
3678 | verbose(env, "R%d indirect variable offset stack access prohibited for !root, var_off=%s\n", | |
3679 | regno, tn_buf); | |
3680 | return -EACCES; | |
3681 | } | |
f2bcd05e AI |
3682 | /* Only initialized buffer on stack is allowed to be accessed |
3683 | * with variable offset. With uninitialized buffer it's hard to | |
3684 | * guarantee that whole memory is marked as initialized on | |
3685 | * helper return since specific bounds are unknown what may | |
3686 | * cause uninitialized stack leaking. | |
3687 | */ | |
3688 | if (meta && meta->raw_mode) | |
3689 | meta = NULL; | |
3690 | ||
107c26a7 AI |
3691 | if (reg->smax_value >= BPF_MAX_VAR_OFF || |
3692 | reg->smax_value <= -BPF_MAX_VAR_OFF) { | |
3693 | verbose(env, "R%d unbounded indirect variable offset stack access\n", | |
3694 | regno); | |
3695 | return -EACCES; | |
3696 | } | |
2011fccf | 3697 | min_off = reg->smin_value + reg->off; |
107c26a7 | 3698 | max_off = reg->smax_value + reg->off; |
2011fccf AI |
3699 | err = __check_stack_boundary(env, regno, min_off, access_size, |
3700 | zero_size_allowed); | |
107c26a7 AI |
3701 | if (err) { |
3702 | verbose(env, "R%d min value is outside of stack bound\n", | |
3703 | regno); | |
2011fccf | 3704 | return err; |
107c26a7 | 3705 | } |
2011fccf AI |
3706 | err = __check_stack_boundary(env, regno, max_off, access_size, |
3707 | zero_size_allowed); | |
107c26a7 AI |
3708 | if (err) { |
3709 | verbose(env, "R%d max value is outside of stack bound\n", | |
3710 | regno); | |
2011fccf | 3711 | return err; |
107c26a7 | 3712 | } |
17a52670 AS |
3713 | } |
3714 | ||
435faee1 DB |
3715 | if (meta && meta->raw_mode) { |
3716 | meta->access_size = access_size; | |
3717 | meta->regno = regno; | |
3718 | return 0; | |
3719 | } | |
3720 | ||
2011fccf | 3721 | for (i = min_off; i < max_off + access_size; i++) { |
cc2b14d5 AS |
3722 | u8 *stype; |
3723 | ||
2011fccf | 3724 | slot = -i - 1; |
638f5b90 | 3725 | spi = slot / BPF_REG_SIZE; |
cc2b14d5 AS |
3726 | if (state->allocated_stack <= slot) |
3727 | goto err; | |
3728 | stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE]; | |
3729 | if (*stype == STACK_MISC) | |
3730 | goto mark; | |
3731 | if (*stype == STACK_ZERO) { | |
3732 | /* helper can write anything into the stack */ | |
3733 | *stype = STACK_MISC; | |
3734 | goto mark; | |
17a52670 | 3735 | } |
1d68f22b YS |
3736 | |
3737 | if (state->stack[spi].slot_type[0] == STACK_SPILL && | |
3738 | state->stack[spi].spilled_ptr.type == PTR_TO_BTF_ID) | |
3739 | goto mark; | |
3740 | ||
f7cf25b2 AS |
3741 | if (state->stack[spi].slot_type[0] == STACK_SPILL && |
3742 | state->stack[spi].spilled_ptr.type == SCALAR_VALUE) { | |
f54c7898 | 3743 | __mark_reg_unknown(env, &state->stack[spi].spilled_ptr); |
f7cf25b2 AS |
3744 | for (j = 0; j < BPF_REG_SIZE; j++) |
3745 | state->stack[spi].slot_type[j] = STACK_MISC; | |
3746 | goto mark; | |
3747 | } | |
3748 | ||
cc2b14d5 | 3749 | err: |
2011fccf AI |
3750 | if (tnum_is_const(reg->var_off)) { |
3751 | verbose(env, "invalid indirect read from stack off %d+%d size %d\n", | |
3752 | min_off, i - min_off, access_size); | |
3753 | } else { | |
3754 | char tn_buf[48]; | |
3755 | ||
3756 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
3757 | verbose(env, "invalid indirect read from stack var_off %s+%d size %d\n", | |
3758 | tn_buf, i - min_off, access_size); | |
3759 | } | |
cc2b14d5 AS |
3760 | return -EACCES; |
3761 | mark: | |
3762 | /* reading any byte out of 8-byte 'spill_slot' will cause | |
3763 | * the whole slot to be marked as 'read' | |
3764 | */ | |
679c782d | 3765 | mark_reg_read(env, &state->stack[spi].spilled_ptr, |
5327ed3d JW |
3766 | state->stack[spi].spilled_ptr.parent, |
3767 | REG_LIVE_READ64); | |
17a52670 | 3768 | } |
2011fccf | 3769 | return update_stack_depth(env, state, min_off); |
17a52670 AS |
3770 | } |
3771 | ||
06c1c049 GB |
3772 | static int check_helper_mem_access(struct bpf_verifier_env *env, int regno, |
3773 | int access_size, bool zero_size_allowed, | |
3774 | struct bpf_call_arg_meta *meta) | |
3775 | { | |
638f5b90 | 3776 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
06c1c049 | 3777 | |
f1174f77 | 3778 | switch (reg->type) { |
06c1c049 | 3779 | case PTR_TO_PACKET: |
de8f3a83 | 3780 | case PTR_TO_PACKET_META: |
9fd29c08 YS |
3781 | return check_packet_access(env, regno, reg->off, access_size, |
3782 | zero_size_allowed); | |
06c1c049 | 3783 | case PTR_TO_MAP_VALUE: |
591fe988 DB |
3784 | if (check_map_access_type(env, regno, reg->off, access_size, |
3785 | meta && meta->raw_mode ? BPF_WRITE : | |
3786 | BPF_READ)) | |
3787 | return -EACCES; | |
9fd29c08 YS |
3788 | return check_map_access(env, regno, reg->off, access_size, |
3789 | zero_size_allowed); | |
457f4436 AN |
3790 | case PTR_TO_MEM: |
3791 | return check_mem_region_access(env, regno, reg->off, | |
3792 | access_size, reg->mem_size, | |
3793 | zero_size_allowed); | |
afbf21dc YS |
3794 | case PTR_TO_RDONLY_BUF: |
3795 | if (meta && meta->raw_mode) | |
3796 | return -EACCES; | |
3797 | return check_buffer_access(env, reg, regno, reg->off, | |
3798 | access_size, zero_size_allowed, | |
3799 | "rdonly", | |
3800 | &env->prog->aux->max_rdonly_access); | |
3801 | case PTR_TO_RDWR_BUF: | |
3802 | return check_buffer_access(env, reg, regno, reg->off, | |
3803 | access_size, zero_size_allowed, | |
3804 | "rdwr", | |
3805 | &env->prog->aux->max_rdwr_access); | |
0d004c02 | 3806 | case PTR_TO_STACK: |
06c1c049 GB |
3807 | return check_stack_boundary(env, regno, access_size, |
3808 | zero_size_allowed, meta); | |
0d004c02 LB |
3809 | default: /* scalar_value or invalid ptr */ |
3810 | /* Allow zero-byte read from NULL, regardless of pointer type */ | |
3811 | if (zero_size_allowed && access_size == 0 && | |
3812 | register_is_null(reg)) | |
3813 | return 0; | |
3814 | ||
3815 | verbose(env, "R%d type=%s expected=%s\n", regno, | |
3816 | reg_type_str[reg->type], | |
3817 | reg_type_str[PTR_TO_STACK]); | |
3818 | return -EACCES; | |
06c1c049 GB |
3819 | } |
3820 | } | |
3821 | ||
d83525ca AS |
3822 | /* Implementation details: |
3823 | * bpf_map_lookup returns PTR_TO_MAP_VALUE_OR_NULL | |
3824 | * Two bpf_map_lookups (even with the same key) will have different reg->id. | |
3825 | * For traditional PTR_TO_MAP_VALUE the verifier clears reg->id after | |
3826 | * value_or_null->value transition, since the verifier only cares about | |
3827 | * the range of access to valid map value pointer and doesn't care about actual | |
3828 | * address of the map element. | |
3829 | * For maps with 'struct bpf_spin_lock' inside map value the verifier keeps | |
3830 | * reg->id > 0 after value_or_null->value transition. By doing so | |
3831 | * two bpf_map_lookups will be considered two different pointers that | |
3832 | * point to different bpf_spin_locks. | |
3833 | * The verifier allows taking only one bpf_spin_lock at a time to avoid | |
3834 | * dead-locks. | |
3835 | * Since only one bpf_spin_lock is allowed the checks are simpler than | |
3836 | * reg_is_refcounted() logic. The verifier needs to remember only | |
3837 | * one spin_lock instead of array of acquired_refs. | |
3838 | * cur_state->active_spin_lock remembers which map value element got locked | |
3839 | * and clears it after bpf_spin_unlock. | |
3840 | */ | |
3841 | static int process_spin_lock(struct bpf_verifier_env *env, int regno, | |
3842 | bool is_lock) | |
3843 | { | |
3844 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; | |
3845 | struct bpf_verifier_state *cur = env->cur_state; | |
3846 | bool is_const = tnum_is_const(reg->var_off); | |
3847 | struct bpf_map *map = reg->map_ptr; | |
3848 | u64 val = reg->var_off.value; | |
3849 | ||
d83525ca AS |
3850 | if (!is_const) { |
3851 | verbose(env, | |
3852 | "R%d doesn't have constant offset. bpf_spin_lock has to be at the constant offset\n", | |
3853 | regno); | |
3854 | return -EINVAL; | |
3855 | } | |
3856 | if (!map->btf) { | |
3857 | verbose(env, | |
3858 | "map '%s' has to have BTF in order to use bpf_spin_lock\n", | |
3859 | map->name); | |
3860 | return -EINVAL; | |
3861 | } | |
3862 | if (!map_value_has_spin_lock(map)) { | |
3863 | if (map->spin_lock_off == -E2BIG) | |
3864 | verbose(env, | |
3865 | "map '%s' has more than one 'struct bpf_spin_lock'\n", | |
3866 | map->name); | |
3867 | else if (map->spin_lock_off == -ENOENT) | |
3868 | verbose(env, | |
3869 | "map '%s' doesn't have 'struct bpf_spin_lock'\n", | |
3870 | map->name); | |
3871 | else | |
3872 | verbose(env, | |
3873 | "map '%s' is not a struct type or bpf_spin_lock is mangled\n", | |
3874 | map->name); | |
3875 | return -EINVAL; | |
3876 | } | |
3877 | if (map->spin_lock_off != val + reg->off) { | |
3878 | verbose(env, "off %lld doesn't point to 'struct bpf_spin_lock'\n", | |
3879 | val + reg->off); | |
3880 | return -EINVAL; | |
3881 | } | |
3882 | if (is_lock) { | |
3883 | if (cur->active_spin_lock) { | |
3884 | verbose(env, | |
3885 | "Locking two bpf_spin_locks are not allowed\n"); | |
3886 | return -EINVAL; | |
3887 | } | |
3888 | cur->active_spin_lock = reg->id; | |
3889 | } else { | |
3890 | if (!cur->active_spin_lock) { | |
3891 | verbose(env, "bpf_spin_unlock without taking a lock\n"); | |
3892 | return -EINVAL; | |
3893 | } | |
3894 | if (cur->active_spin_lock != reg->id) { | |
3895 | verbose(env, "bpf_spin_unlock of different lock\n"); | |
3896 | return -EINVAL; | |
3897 | } | |
3898 | cur->active_spin_lock = 0; | |
3899 | } | |
3900 | return 0; | |
3901 | } | |
3902 | ||
90133415 DB |
3903 | static bool arg_type_is_mem_ptr(enum bpf_arg_type type) |
3904 | { | |
3905 | return type == ARG_PTR_TO_MEM || | |
3906 | type == ARG_PTR_TO_MEM_OR_NULL || | |
3907 | type == ARG_PTR_TO_UNINIT_MEM; | |
3908 | } | |
3909 | ||
3910 | static bool arg_type_is_mem_size(enum bpf_arg_type type) | |
3911 | { | |
3912 | return type == ARG_CONST_SIZE || | |
3913 | type == ARG_CONST_SIZE_OR_ZERO; | |
3914 | } | |
3915 | ||
457f4436 AN |
3916 | static bool arg_type_is_alloc_size(enum bpf_arg_type type) |
3917 | { | |
3918 | return type == ARG_CONST_ALLOC_SIZE_OR_ZERO; | |
3919 | } | |
3920 | ||
57c3bb72 AI |
3921 | static bool arg_type_is_int_ptr(enum bpf_arg_type type) |
3922 | { | |
3923 | return type == ARG_PTR_TO_INT || | |
3924 | type == ARG_PTR_TO_LONG; | |
3925 | } | |
3926 | ||
3927 | static int int_ptr_type_to_size(enum bpf_arg_type type) | |
3928 | { | |
3929 | if (type == ARG_PTR_TO_INT) | |
3930 | return sizeof(u32); | |
3931 | else if (type == ARG_PTR_TO_LONG) | |
3932 | return sizeof(u64); | |
3933 | ||
3934 | return -EINVAL; | |
3935 | } | |
3936 | ||
912f442c LB |
3937 | static int resolve_map_arg_type(struct bpf_verifier_env *env, |
3938 | const struct bpf_call_arg_meta *meta, | |
3939 | enum bpf_arg_type *arg_type) | |
3940 | { | |
3941 | if (!meta->map_ptr) { | |
3942 | /* kernel subsystem misconfigured verifier */ | |
3943 | verbose(env, "invalid map_ptr to access map->type\n"); | |
3944 | return -EACCES; | |
3945 | } | |
3946 | ||
3947 | switch (meta->map_ptr->map_type) { | |
3948 | case BPF_MAP_TYPE_SOCKMAP: | |
3949 | case BPF_MAP_TYPE_SOCKHASH: | |
3950 | if (*arg_type == ARG_PTR_TO_MAP_VALUE) { | |
6550f2dd | 3951 | *arg_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON; |
912f442c LB |
3952 | } else { |
3953 | verbose(env, "invalid arg_type for sockmap/sockhash\n"); | |
3954 | return -EINVAL; | |
3955 | } | |
3956 | break; | |
3957 | ||
3958 | default: | |
3959 | break; | |
3960 | } | |
3961 | return 0; | |
3962 | } | |
3963 | ||
f79e7ea5 LB |
3964 | struct bpf_reg_types { |
3965 | const enum bpf_reg_type types[10]; | |
1df8f55a | 3966 | u32 *btf_id; |
f79e7ea5 LB |
3967 | }; |
3968 | ||
3969 | static const struct bpf_reg_types map_key_value_types = { | |
3970 | .types = { | |
3971 | PTR_TO_STACK, | |
3972 | PTR_TO_PACKET, | |
3973 | PTR_TO_PACKET_META, | |
3974 | PTR_TO_MAP_VALUE, | |
3975 | }, | |
3976 | }; | |
3977 | ||
3978 | static const struct bpf_reg_types sock_types = { | |
3979 | .types = { | |
3980 | PTR_TO_SOCK_COMMON, | |
3981 | PTR_TO_SOCKET, | |
3982 | PTR_TO_TCP_SOCK, | |
3983 | PTR_TO_XDP_SOCK, | |
3984 | }, | |
3985 | }; | |
3986 | ||
49a2a4d4 | 3987 | #ifdef CONFIG_NET |
1df8f55a MKL |
3988 | static const struct bpf_reg_types btf_id_sock_common_types = { |
3989 | .types = { | |
3990 | PTR_TO_SOCK_COMMON, | |
3991 | PTR_TO_SOCKET, | |
3992 | PTR_TO_TCP_SOCK, | |
3993 | PTR_TO_XDP_SOCK, | |
3994 | PTR_TO_BTF_ID, | |
3995 | }, | |
3996 | .btf_id = &btf_sock_ids[BTF_SOCK_TYPE_SOCK_COMMON], | |
3997 | }; | |
49a2a4d4 | 3998 | #endif |
1df8f55a | 3999 | |
f79e7ea5 LB |
4000 | static const struct bpf_reg_types mem_types = { |
4001 | .types = { | |
4002 | PTR_TO_STACK, | |
4003 | PTR_TO_PACKET, | |
4004 | PTR_TO_PACKET_META, | |
4005 | PTR_TO_MAP_VALUE, | |
4006 | PTR_TO_MEM, | |
4007 | PTR_TO_RDONLY_BUF, | |
4008 | PTR_TO_RDWR_BUF, | |
4009 | }, | |
4010 | }; | |
4011 | ||
4012 | static const struct bpf_reg_types int_ptr_types = { | |
4013 | .types = { | |
4014 | PTR_TO_STACK, | |
4015 | PTR_TO_PACKET, | |
4016 | PTR_TO_PACKET_META, | |
4017 | PTR_TO_MAP_VALUE, | |
4018 | }, | |
4019 | }; | |
4020 | ||
4021 | static const struct bpf_reg_types fullsock_types = { .types = { PTR_TO_SOCKET } }; | |
4022 | static const struct bpf_reg_types scalar_types = { .types = { SCALAR_VALUE } }; | |
4023 | static const struct bpf_reg_types context_types = { .types = { PTR_TO_CTX } }; | |
4024 | static const struct bpf_reg_types alloc_mem_types = { .types = { PTR_TO_MEM } }; | |
4025 | static const struct bpf_reg_types const_map_ptr_types = { .types = { CONST_PTR_TO_MAP } }; | |
4026 | static const struct bpf_reg_types btf_ptr_types = { .types = { PTR_TO_BTF_ID } }; | |
4027 | static const struct bpf_reg_types spin_lock_types = { .types = { PTR_TO_MAP_VALUE } }; | |
eaa6bcb7 | 4028 | static const struct bpf_reg_types percpu_btf_ptr_types = { .types = { PTR_TO_PERCPU_BTF_ID } }; |
f79e7ea5 | 4029 | |
0789e13b | 4030 | static const struct bpf_reg_types *compatible_reg_types[__BPF_ARG_TYPE_MAX] = { |
f79e7ea5 LB |
4031 | [ARG_PTR_TO_MAP_KEY] = &map_key_value_types, |
4032 | [ARG_PTR_TO_MAP_VALUE] = &map_key_value_types, | |
4033 | [ARG_PTR_TO_UNINIT_MAP_VALUE] = &map_key_value_types, | |
4034 | [ARG_PTR_TO_MAP_VALUE_OR_NULL] = &map_key_value_types, | |
4035 | [ARG_CONST_SIZE] = &scalar_types, | |
4036 | [ARG_CONST_SIZE_OR_ZERO] = &scalar_types, | |
4037 | [ARG_CONST_ALLOC_SIZE_OR_ZERO] = &scalar_types, | |
4038 | [ARG_CONST_MAP_PTR] = &const_map_ptr_types, | |
4039 | [ARG_PTR_TO_CTX] = &context_types, | |
4040 | [ARG_PTR_TO_CTX_OR_NULL] = &context_types, | |
4041 | [ARG_PTR_TO_SOCK_COMMON] = &sock_types, | |
49a2a4d4 | 4042 | #ifdef CONFIG_NET |
1df8f55a | 4043 | [ARG_PTR_TO_BTF_ID_SOCK_COMMON] = &btf_id_sock_common_types, |
49a2a4d4 | 4044 | #endif |
f79e7ea5 LB |
4045 | [ARG_PTR_TO_SOCKET] = &fullsock_types, |
4046 | [ARG_PTR_TO_SOCKET_OR_NULL] = &fullsock_types, | |
4047 | [ARG_PTR_TO_BTF_ID] = &btf_ptr_types, | |
4048 | [ARG_PTR_TO_SPIN_LOCK] = &spin_lock_types, | |
4049 | [ARG_PTR_TO_MEM] = &mem_types, | |
4050 | [ARG_PTR_TO_MEM_OR_NULL] = &mem_types, | |
4051 | [ARG_PTR_TO_UNINIT_MEM] = &mem_types, | |
4052 | [ARG_PTR_TO_ALLOC_MEM] = &alloc_mem_types, | |
4053 | [ARG_PTR_TO_ALLOC_MEM_OR_NULL] = &alloc_mem_types, | |
4054 | [ARG_PTR_TO_INT] = &int_ptr_types, | |
4055 | [ARG_PTR_TO_LONG] = &int_ptr_types, | |
eaa6bcb7 | 4056 | [ARG_PTR_TO_PERCPU_BTF_ID] = &percpu_btf_ptr_types, |
f79e7ea5 LB |
4057 | }; |
4058 | ||
4059 | static int check_reg_type(struct bpf_verifier_env *env, u32 regno, | |
a968d5e2 MKL |
4060 | enum bpf_arg_type arg_type, |
4061 | const u32 *arg_btf_id) | |
f79e7ea5 LB |
4062 | { |
4063 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; | |
4064 | enum bpf_reg_type expected, type = reg->type; | |
a968d5e2 | 4065 | const struct bpf_reg_types *compatible; |
f79e7ea5 LB |
4066 | int i, j; |
4067 | ||
a968d5e2 MKL |
4068 | compatible = compatible_reg_types[arg_type]; |
4069 | if (!compatible) { | |
4070 | verbose(env, "verifier internal error: unsupported arg type %d\n", arg_type); | |
4071 | return -EFAULT; | |
4072 | } | |
4073 | ||
f79e7ea5 LB |
4074 | for (i = 0; i < ARRAY_SIZE(compatible->types); i++) { |
4075 | expected = compatible->types[i]; | |
4076 | if (expected == NOT_INIT) | |
4077 | break; | |
4078 | ||
4079 | if (type == expected) | |
a968d5e2 | 4080 | goto found; |
f79e7ea5 LB |
4081 | } |
4082 | ||
4083 | verbose(env, "R%d type=%s expected=", regno, reg_type_str[type]); | |
4084 | for (j = 0; j + 1 < i; j++) | |
4085 | verbose(env, "%s, ", reg_type_str[compatible->types[j]]); | |
4086 | verbose(env, "%s\n", reg_type_str[compatible->types[j]]); | |
4087 | return -EACCES; | |
a968d5e2 MKL |
4088 | |
4089 | found: | |
4090 | if (type == PTR_TO_BTF_ID) { | |
1df8f55a MKL |
4091 | if (!arg_btf_id) { |
4092 | if (!compatible->btf_id) { | |
4093 | verbose(env, "verifier internal error: missing arg compatible BTF ID\n"); | |
4094 | return -EFAULT; | |
4095 | } | |
4096 | arg_btf_id = compatible->btf_id; | |
4097 | } | |
4098 | ||
a968d5e2 MKL |
4099 | if (!btf_struct_ids_match(&env->log, reg->off, reg->btf_id, |
4100 | *arg_btf_id)) { | |
4101 | verbose(env, "R%d is of type %s but %s is expected\n", | |
4102 | regno, kernel_type_name(reg->btf_id), | |
4103 | kernel_type_name(*arg_btf_id)); | |
4104 | return -EACCES; | |
4105 | } | |
4106 | ||
4107 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
4108 | verbose(env, "R%d is a pointer to in-kernel struct with non-zero offset\n", | |
4109 | regno); | |
4110 | return -EACCES; | |
4111 | } | |
4112 | } | |
4113 | ||
4114 | return 0; | |
f79e7ea5 LB |
4115 | } |
4116 | ||
af7ec138 YS |
4117 | static int check_func_arg(struct bpf_verifier_env *env, u32 arg, |
4118 | struct bpf_call_arg_meta *meta, | |
4119 | const struct bpf_func_proto *fn) | |
17a52670 | 4120 | { |
af7ec138 | 4121 | u32 regno = BPF_REG_1 + arg; |
638f5b90 | 4122 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
af7ec138 | 4123 | enum bpf_arg_type arg_type = fn->arg_type[arg]; |
f79e7ea5 | 4124 | enum bpf_reg_type type = reg->type; |
17a52670 AS |
4125 | int err = 0; |
4126 | ||
80f1d68c | 4127 | if (arg_type == ARG_DONTCARE) |
17a52670 AS |
4128 | return 0; |
4129 | ||
dc503a8a EC |
4130 | err = check_reg_arg(env, regno, SRC_OP); |
4131 | if (err) | |
4132 | return err; | |
17a52670 | 4133 | |
1be7f75d AS |
4134 | if (arg_type == ARG_ANYTHING) { |
4135 | if (is_pointer_value(env, regno)) { | |
61bd5218 JK |
4136 | verbose(env, "R%d leaks addr into helper function\n", |
4137 | regno); | |
1be7f75d AS |
4138 | return -EACCES; |
4139 | } | |
80f1d68c | 4140 | return 0; |
1be7f75d | 4141 | } |
80f1d68c | 4142 | |
de8f3a83 | 4143 | if (type_is_pkt_pointer(type) && |
3a0af8fd | 4144 | !may_access_direct_pkt_data(env, meta, BPF_READ)) { |
61bd5218 | 4145 | verbose(env, "helper access to the packet is not allowed\n"); |
6841de8b AS |
4146 | return -EACCES; |
4147 | } | |
4148 | ||
912f442c LB |
4149 | if (arg_type == ARG_PTR_TO_MAP_VALUE || |
4150 | arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE || | |
4151 | arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL) { | |
4152 | err = resolve_map_arg_type(env, meta, &arg_type); | |
4153 | if (err) | |
4154 | return err; | |
4155 | } | |
4156 | ||
fd1b0d60 LB |
4157 | if (register_is_null(reg) && arg_type_may_be_null(arg_type)) |
4158 | /* A NULL register has a SCALAR_VALUE type, so skip | |
4159 | * type checking. | |
4160 | */ | |
4161 | goto skip_type_check; | |
4162 | ||
a968d5e2 | 4163 | err = check_reg_type(env, regno, arg_type, fn->arg_btf_id[arg]); |
f79e7ea5 LB |
4164 | if (err) |
4165 | return err; | |
4166 | ||
a968d5e2 | 4167 | if (type == PTR_TO_CTX) { |
feec7040 LB |
4168 | err = check_ctx_reg(env, reg, regno); |
4169 | if (err < 0) | |
4170 | return err; | |
d7b9454a LB |
4171 | } |
4172 | ||
fd1b0d60 | 4173 | skip_type_check: |
02f7c958 | 4174 | if (reg->ref_obj_id) { |
457f4436 AN |
4175 | if (meta->ref_obj_id) { |
4176 | verbose(env, "verifier internal error: more than one arg with ref_obj_id R%d %u %u\n", | |
4177 | regno, reg->ref_obj_id, | |
4178 | meta->ref_obj_id); | |
4179 | return -EFAULT; | |
4180 | } | |
4181 | meta->ref_obj_id = reg->ref_obj_id; | |
17a52670 AS |
4182 | } |
4183 | ||
17a52670 AS |
4184 | if (arg_type == ARG_CONST_MAP_PTR) { |
4185 | /* bpf_map_xxx(map_ptr) call: remember that map_ptr */ | |
33ff9823 | 4186 | meta->map_ptr = reg->map_ptr; |
17a52670 AS |
4187 | } else if (arg_type == ARG_PTR_TO_MAP_KEY) { |
4188 | /* bpf_map_xxx(..., map_ptr, ..., key) call: | |
4189 | * check that [key, key + map->key_size) are within | |
4190 | * stack limits and initialized | |
4191 | */ | |
33ff9823 | 4192 | if (!meta->map_ptr) { |
17a52670 AS |
4193 | /* in function declaration map_ptr must come before |
4194 | * map_key, so that it's verified and known before | |
4195 | * we have to check map_key here. Otherwise it means | |
4196 | * that kernel subsystem misconfigured verifier | |
4197 | */ | |
61bd5218 | 4198 | verbose(env, "invalid map_ptr to access map->key\n"); |
17a52670 AS |
4199 | return -EACCES; |
4200 | } | |
d71962f3 PC |
4201 | err = check_helper_mem_access(env, regno, |
4202 | meta->map_ptr->key_size, false, | |
4203 | NULL); | |
2ea864c5 | 4204 | } else if (arg_type == ARG_PTR_TO_MAP_VALUE || |
6ac99e8f MKL |
4205 | (arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL && |
4206 | !register_is_null(reg)) || | |
2ea864c5 | 4207 | arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE) { |
17a52670 AS |
4208 | /* bpf_map_xxx(..., map_ptr, ..., value) call: |
4209 | * check [value, value + map->value_size) validity | |
4210 | */ | |
33ff9823 | 4211 | if (!meta->map_ptr) { |
17a52670 | 4212 | /* kernel subsystem misconfigured verifier */ |
61bd5218 | 4213 | verbose(env, "invalid map_ptr to access map->value\n"); |
17a52670 AS |
4214 | return -EACCES; |
4215 | } | |
2ea864c5 | 4216 | meta->raw_mode = (arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE); |
d71962f3 PC |
4217 | err = check_helper_mem_access(env, regno, |
4218 | meta->map_ptr->value_size, false, | |
2ea864c5 | 4219 | meta); |
eaa6bcb7 HL |
4220 | } else if (arg_type == ARG_PTR_TO_PERCPU_BTF_ID) { |
4221 | if (!reg->btf_id) { | |
4222 | verbose(env, "Helper has invalid btf_id in R%d\n", regno); | |
4223 | return -EACCES; | |
4224 | } | |
4225 | meta->ret_btf_id = reg->btf_id; | |
c18f0b6a LB |
4226 | } else if (arg_type == ARG_PTR_TO_SPIN_LOCK) { |
4227 | if (meta->func_id == BPF_FUNC_spin_lock) { | |
4228 | if (process_spin_lock(env, regno, true)) | |
4229 | return -EACCES; | |
4230 | } else if (meta->func_id == BPF_FUNC_spin_unlock) { | |
4231 | if (process_spin_lock(env, regno, false)) | |
4232 | return -EACCES; | |
4233 | } else { | |
4234 | verbose(env, "verifier internal error\n"); | |
4235 | return -EFAULT; | |
4236 | } | |
a2bbe7cc LB |
4237 | } else if (arg_type_is_mem_ptr(arg_type)) { |
4238 | /* The access to this pointer is only checked when we hit the | |
4239 | * next is_mem_size argument below. | |
4240 | */ | |
4241 | meta->raw_mode = (arg_type == ARG_PTR_TO_UNINIT_MEM); | |
90133415 | 4242 | } else if (arg_type_is_mem_size(arg_type)) { |
39f19ebb | 4243 | bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO); |
17a52670 | 4244 | |
10060503 JF |
4245 | /* This is used to refine r0 return value bounds for helpers |
4246 | * that enforce this value as an upper bound on return values. | |
4247 | * See do_refine_retval_range() for helpers that can refine | |
4248 | * the return value. C type of helper is u32 so we pull register | |
4249 | * bound from umax_value however, if negative verifier errors | |
4250 | * out. Only upper bounds can be learned because retval is an | |
4251 | * int type and negative retvals are allowed. | |
849fa506 | 4252 | */ |
10060503 | 4253 | meta->msize_max_value = reg->umax_value; |
849fa506 | 4254 | |
f1174f77 EC |
4255 | /* The register is SCALAR_VALUE; the access check |
4256 | * happens using its boundaries. | |
06c1c049 | 4257 | */ |
f1174f77 | 4258 | if (!tnum_is_const(reg->var_off)) |
06c1c049 GB |
4259 | /* For unprivileged variable accesses, disable raw |
4260 | * mode so that the program is required to | |
4261 | * initialize all the memory that the helper could | |
4262 | * just partially fill up. | |
4263 | */ | |
4264 | meta = NULL; | |
4265 | ||
b03c9f9f | 4266 | if (reg->smin_value < 0) { |
61bd5218 | 4267 | verbose(env, "R%d min value is negative, either use unsigned or 'var &= const'\n", |
f1174f77 EC |
4268 | regno); |
4269 | return -EACCES; | |
4270 | } | |
06c1c049 | 4271 | |
b03c9f9f | 4272 | if (reg->umin_value == 0) { |
f1174f77 EC |
4273 | err = check_helper_mem_access(env, regno - 1, 0, |
4274 | zero_size_allowed, | |
4275 | meta); | |
06c1c049 GB |
4276 | if (err) |
4277 | return err; | |
06c1c049 | 4278 | } |
f1174f77 | 4279 | |
b03c9f9f | 4280 | if (reg->umax_value >= BPF_MAX_VAR_SIZ) { |
61bd5218 | 4281 | verbose(env, "R%d unbounded memory access, use 'var &= const' or 'if (var < const)'\n", |
f1174f77 EC |
4282 | regno); |
4283 | return -EACCES; | |
4284 | } | |
4285 | err = check_helper_mem_access(env, regno - 1, | |
b03c9f9f | 4286 | reg->umax_value, |
f1174f77 | 4287 | zero_size_allowed, meta); |
b5dc0163 AS |
4288 | if (!err) |
4289 | err = mark_chain_precision(env, regno); | |
457f4436 AN |
4290 | } else if (arg_type_is_alloc_size(arg_type)) { |
4291 | if (!tnum_is_const(reg->var_off)) { | |
4292 | verbose(env, "R%d unbounded size, use 'var &= const' or 'if (var < const)'\n", | |
4293 | regno); | |
4294 | return -EACCES; | |
4295 | } | |
4296 | meta->mem_size = reg->var_off.value; | |
57c3bb72 AI |
4297 | } else if (arg_type_is_int_ptr(arg_type)) { |
4298 | int size = int_ptr_type_to_size(arg_type); | |
4299 | ||
4300 | err = check_helper_mem_access(env, regno, size, false, meta); | |
4301 | if (err) | |
4302 | return err; | |
4303 | err = check_ptr_alignment(env, reg, 0, size, true); | |
17a52670 AS |
4304 | } |
4305 | ||
4306 | return err; | |
4307 | } | |
4308 | ||
0126240f LB |
4309 | static bool may_update_sockmap(struct bpf_verifier_env *env, int func_id) |
4310 | { | |
4311 | enum bpf_attach_type eatype = env->prog->expected_attach_type; | |
7e40781c | 4312 | enum bpf_prog_type type = resolve_prog_type(env->prog); |
0126240f LB |
4313 | |
4314 | if (func_id != BPF_FUNC_map_update_elem) | |
4315 | return false; | |
4316 | ||
4317 | /* It's not possible to get access to a locked struct sock in these | |
4318 | * contexts, so updating is safe. | |
4319 | */ | |
4320 | switch (type) { | |
4321 | case BPF_PROG_TYPE_TRACING: | |
4322 | if (eatype == BPF_TRACE_ITER) | |
4323 | return true; | |
4324 | break; | |
4325 | case BPF_PROG_TYPE_SOCKET_FILTER: | |
4326 | case BPF_PROG_TYPE_SCHED_CLS: | |
4327 | case BPF_PROG_TYPE_SCHED_ACT: | |
4328 | case BPF_PROG_TYPE_XDP: | |
4329 | case BPF_PROG_TYPE_SK_REUSEPORT: | |
4330 | case BPF_PROG_TYPE_FLOW_DISSECTOR: | |
4331 | case BPF_PROG_TYPE_SK_LOOKUP: | |
4332 | return true; | |
4333 | default: | |
4334 | break; | |
4335 | } | |
4336 | ||
4337 | verbose(env, "cannot update sockmap in this context\n"); | |
4338 | return false; | |
4339 | } | |
4340 | ||
e411901c MF |
4341 | static bool allow_tail_call_in_subprogs(struct bpf_verifier_env *env) |
4342 | { | |
4343 | return env->prog->jit_requested && IS_ENABLED(CONFIG_X86_64); | |
4344 | } | |
4345 | ||
61bd5218 JK |
4346 | static int check_map_func_compatibility(struct bpf_verifier_env *env, |
4347 | struct bpf_map *map, int func_id) | |
35578d79 | 4348 | { |
35578d79 KX |
4349 | if (!map) |
4350 | return 0; | |
4351 | ||
6aff67c8 AS |
4352 | /* We need a two way check, first is from map perspective ... */ |
4353 | switch (map->map_type) { | |
4354 | case BPF_MAP_TYPE_PROG_ARRAY: | |
4355 | if (func_id != BPF_FUNC_tail_call) | |
4356 | goto error; | |
4357 | break; | |
4358 | case BPF_MAP_TYPE_PERF_EVENT_ARRAY: | |
4359 | if (func_id != BPF_FUNC_perf_event_read && | |
908432ca | 4360 | func_id != BPF_FUNC_perf_event_output && |
a7658e1a | 4361 | func_id != BPF_FUNC_skb_output && |
d831ee84 EC |
4362 | func_id != BPF_FUNC_perf_event_read_value && |
4363 | func_id != BPF_FUNC_xdp_output) | |
6aff67c8 AS |
4364 | goto error; |
4365 | break; | |
457f4436 AN |
4366 | case BPF_MAP_TYPE_RINGBUF: |
4367 | if (func_id != BPF_FUNC_ringbuf_output && | |
4368 | func_id != BPF_FUNC_ringbuf_reserve && | |
4369 | func_id != BPF_FUNC_ringbuf_submit && | |
4370 | func_id != BPF_FUNC_ringbuf_discard && | |
4371 | func_id != BPF_FUNC_ringbuf_query) | |
4372 | goto error; | |
4373 | break; | |
6aff67c8 AS |
4374 | case BPF_MAP_TYPE_STACK_TRACE: |
4375 | if (func_id != BPF_FUNC_get_stackid) | |
4376 | goto error; | |
4377 | break; | |
4ed8ec52 | 4378 | case BPF_MAP_TYPE_CGROUP_ARRAY: |
60747ef4 | 4379 | if (func_id != BPF_FUNC_skb_under_cgroup && |
60d20f91 | 4380 | func_id != BPF_FUNC_current_task_under_cgroup) |
4a482f34 MKL |
4381 | goto error; |
4382 | break; | |
cd339431 | 4383 | case BPF_MAP_TYPE_CGROUP_STORAGE: |
b741f163 | 4384 | case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE: |
cd339431 RG |
4385 | if (func_id != BPF_FUNC_get_local_storage) |
4386 | goto error; | |
4387 | break; | |
546ac1ff | 4388 | case BPF_MAP_TYPE_DEVMAP: |
6f9d451a | 4389 | case BPF_MAP_TYPE_DEVMAP_HASH: |
0cdbb4b0 THJ |
4390 | if (func_id != BPF_FUNC_redirect_map && |
4391 | func_id != BPF_FUNC_map_lookup_elem) | |
546ac1ff JF |
4392 | goto error; |
4393 | break; | |
fbfc504a BT |
4394 | /* Restrict bpf side of cpumap and xskmap, open when use-cases |
4395 | * appear. | |
4396 | */ | |
6710e112 JDB |
4397 | case BPF_MAP_TYPE_CPUMAP: |
4398 | if (func_id != BPF_FUNC_redirect_map) | |
4399 | goto error; | |
4400 | break; | |
fada7fdc JL |
4401 | case BPF_MAP_TYPE_XSKMAP: |
4402 | if (func_id != BPF_FUNC_redirect_map && | |
4403 | func_id != BPF_FUNC_map_lookup_elem) | |
4404 | goto error; | |
4405 | break; | |
56f668df | 4406 | case BPF_MAP_TYPE_ARRAY_OF_MAPS: |
bcc6b1b7 | 4407 | case BPF_MAP_TYPE_HASH_OF_MAPS: |
56f668df MKL |
4408 | if (func_id != BPF_FUNC_map_lookup_elem) |
4409 | goto error; | |
16a43625 | 4410 | break; |
174a79ff JF |
4411 | case BPF_MAP_TYPE_SOCKMAP: |
4412 | if (func_id != BPF_FUNC_sk_redirect_map && | |
4413 | func_id != BPF_FUNC_sock_map_update && | |
4f738adb | 4414 | func_id != BPF_FUNC_map_delete_elem && |
9fed9000 | 4415 | func_id != BPF_FUNC_msg_redirect_map && |
64d85290 | 4416 | func_id != BPF_FUNC_sk_select_reuseport && |
0126240f LB |
4417 | func_id != BPF_FUNC_map_lookup_elem && |
4418 | !may_update_sockmap(env, func_id)) | |
174a79ff JF |
4419 | goto error; |
4420 | break; | |
81110384 JF |
4421 | case BPF_MAP_TYPE_SOCKHASH: |
4422 | if (func_id != BPF_FUNC_sk_redirect_hash && | |
4423 | func_id != BPF_FUNC_sock_hash_update && | |
4424 | func_id != BPF_FUNC_map_delete_elem && | |
9fed9000 | 4425 | func_id != BPF_FUNC_msg_redirect_hash && |
64d85290 | 4426 | func_id != BPF_FUNC_sk_select_reuseport && |
0126240f LB |
4427 | func_id != BPF_FUNC_map_lookup_elem && |
4428 | !may_update_sockmap(env, func_id)) | |
81110384 JF |
4429 | goto error; |
4430 | break; | |
2dbb9b9e MKL |
4431 | case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY: |
4432 | if (func_id != BPF_FUNC_sk_select_reuseport) | |
4433 | goto error; | |
4434 | break; | |
f1a2e44a MV |
4435 | case BPF_MAP_TYPE_QUEUE: |
4436 | case BPF_MAP_TYPE_STACK: | |
4437 | if (func_id != BPF_FUNC_map_peek_elem && | |
4438 | func_id != BPF_FUNC_map_pop_elem && | |
4439 | func_id != BPF_FUNC_map_push_elem) | |
4440 | goto error; | |
4441 | break; | |
6ac99e8f MKL |
4442 | case BPF_MAP_TYPE_SK_STORAGE: |
4443 | if (func_id != BPF_FUNC_sk_storage_get && | |
4444 | func_id != BPF_FUNC_sk_storage_delete) | |
4445 | goto error; | |
4446 | break; | |
8ea63684 KS |
4447 | case BPF_MAP_TYPE_INODE_STORAGE: |
4448 | if (func_id != BPF_FUNC_inode_storage_get && | |
4449 | func_id != BPF_FUNC_inode_storage_delete) | |
4450 | goto error; | |
4451 | break; | |
6aff67c8 AS |
4452 | default: |
4453 | break; | |
4454 | } | |
4455 | ||
4456 | /* ... and second from the function itself. */ | |
4457 | switch (func_id) { | |
4458 | case BPF_FUNC_tail_call: | |
4459 | if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY) | |
4460 | goto error; | |
e411901c MF |
4461 | if (env->subprog_cnt > 1 && !allow_tail_call_in_subprogs(env)) { |
4462 | verbose(env, "tail_calls are not allowed in non-JITed programs with bpf-to-bpf calls\n"); | |
f4d7e40a AS |
4463 | return -EINVAL; |
4464 | } | |
6aff67c8 AS |
4465 | break; |
4466 | case BPF_FUNC_perf_event_read: | |
4467 | case BPF_FUNC_perf_event_output: | |
908432ca | 4468 | case BPF_FUNC_perf_event_read_value: |
a7658e1a | 4469 | case BPF_FUNC_skb_output: |
d831ee84 | 4470 | case BPF_FUNC_xdp_output: |
6aff67c8 AS |
4471 | if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) |
4472 | goto error; | |
4473 | break; | |
4474 | case BPF_FUNC_get_stackid: | |
4475 | if (map->map_type != BPF_MAP_TYPE_STACK_TRACE) | |
4476 | goto error; | |
4477 | break; | |
60d20f91 | 4478 | case BPF_FUNC_current_task_under_cgroup: |
747ea55e | 4479 | case BPF_FUNC_skb_under_cgroup: |
4a482f34 MKL |
4480 | if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY) |
4481 | goto error; | |
4482 | break; | |
97f91a7c | 4483 | case BPF_FUNC_redirect_map: |
9c270af3 | 4484 | if (map->map_type != BPF_MAP_TYPE_DEVMAP && |
6f9d451a | 4485 | map->map_type != BPF_MAP_TYPE_DEVMAP_HASH && |
fbfc504a BT |
4486 | map->map_type != BPF_MAP_TYPE_CPUMAP && |
4487 | map->map_type != BPF_MAP_TYPE_XSKMAP) | |
97f91a7c JF |
4488 | goto error; |
4489 | break; | |
174a79ff | 4490 | case BPF_FUNC_sk_redirect_map: |
4f738adb | 4491 | case BPF_FUNC_msg_redirect_map: |
81110384 | 4492 | case BPF_FUNC_sock_map_update: |
174a79ff JF |
4493 | if (map->map_type != BPF_MAP_TYPE_SOCKMAP) |
4494 | goto error; | |
4495 | break; | |
81110384 JF |
4496 | case BPF_FUNC_sk_redirect_hash: |
4497 | case BPF_FUNC_msg_redirect_hash: | |
4498 | case BPF_FUNC_sock_hash_update: | |
4499 | if (map->map_type != BPF_MAP_TYPE_SOCKHASH) | |
174a79ff JF |
4500 | goto error; |
4501 | break; | |
cd339431 | 4502 | case BPF_FUNC_get_local_storage: |
b741f163 RG |
4503 | if (map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE && |
4504 | map->map_type != BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) | |
cd339431 RG |
4505 | goto error; |
4506 | break; | |
2dbb9b9e | 4507 | case BPF_FUNC_sk_select_reuseport: |
9fed9000 JS |
4508 | if (map->map_type != BPF_MAP_TYPE_REUSEPORT_SOCKARRAY && |
4509 | map->map_type != BPF_MAP_TYPE_SOCKMAP && | |
4510 | map->map_type != BPF_MAP_TYPE_SOCKHASH) | |
2dbb9b9e MKL |
4511 | goto error; |
4512 | break; | |
f1a2e44a MV |
4513 | case BPF_FUNC_map_peek_elem: |
4514 | case BPF_FUNC_map_pop_elem: | |
4515 | case BPF_FUNC_map_push_elem: | |
4516 | if (map->map_type != BPF_MAP_TYPE_QUEUE && | |
4517 | map->map_type != BPF_MAP_TYPE_STACK) | |
4518 | goto error; | |
4519 | break; | |
6ac99e8f MKL |
4520 | case BPF_FUNC_sk_storage_get: |
4521 | case BPF_FUNC_sk_storage_delete: | |
4522 | if (map->map_type != BPF_MAP_TYPE_SK_STORAGE) | |
4523 | goto error; | |
4524 | break; | |
8ea63684 KS |
4525 | case BPF_FUNC_inode_storage_get: |
4526 | case BPF_FUNC_inode_storage_delete: | |
4527 | if (map->map_type != BPF_MAP_TYPE_INODE_STORAGE) | |
4528 | goto error; | |
4529 | break; | |
6aff67c8 AS |
4530 | default: |
4531 | break; | |
35578d79 KX |
4532 | } |
4533 | ||
4534 | return 0; | |
6aff67c8 | 4535 | error: |
61bd5218 | 4536 | verbose(env, "cannot pass map_type %d into func %s#%d\n", |
ebb676da | 4537 | map->map_type, func_id_name(func_id), func_id); |
6aff67c8 | 4538 | return -EINVAL; |
35578d79 KX |
4539 | } |
4540 | ||
90133415 | 4541 | static bool check_raw_mode_ok(const struct bpf_func_proto *fn) |
435faee1 DB |
4542 | { |
4543 | int count = 0; | |
4544 | ||
39f19ebb | 4545 | if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 4546 | count++; |
39f19ebb | 4547 | if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 4548 | count++; |
39f19ebb | 4549 | if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 4550 | count++; |
39f19ebb | 4551 | if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 4552 | count++; |
39f19ebb | 4553 | if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 DB |
4554 | count++; |
4555 | ||
90133415 DB |
4556 | /* We only support one arg being in raw mode at the moment, |
4557 | * which is sufficient for the helper functions we have | |
4558 | * right now. | |
4559 | */ | |
4560 | return count <= 1; | |
4561 | } | |
4562 | ||
4563 | static bool check_args_pair_invalid(enum bpf_arg_type arg_curr, | |
4564 | enum bpf_arg_type arg_next) | |
4565 | { | |
4566 | return (arg_type_is_mem_ptr(arg_curr) && | |
4567 | !arg_type_is_mem_size(arg_next)) || | |
4568 | (!arg_type_is_mem_ptr(arg_curr) && | |
4569 | arg_type_is_mem_size(arg_next)); | |
4570 | } | |
4571 | ||
4572 | static bool check_arg_pair_ok(const struct bpf_func_proto *fn) | |
4573 | { | |
4574 | /* bpf_xxx(..., buf, len) call will access 'len' | |
4575 | * bytes from memory 'buf'. Both arg types need | |
4576 | * to be paired, so make sure there's no buggy | |
4577 | * helper function specification. | |
4578 | */ | |
4579 | if (arg_type_is_mem_size(fn->arg1_type) || | |
4580 | arg_type_is_mem_ptr(fn->arg5_type) || | |
4581 | check_args_pair_invalid(fn->arg1_type, fn->arg2_type) || | |
4582 | check_args_pair_invalid(fn->arg2_type, fn->arg3_type) || | |
4583 | check_args_pair_invalid(fn->arg3_type, fn->arg4_type) || | |
4584 | check_args_pair_invalid(fn->arg4_type, fn->arg5_type)) | |
4585 | return false; | |
4586 | ||
4587 | return true; | |
4588 | } | |
4589 | ||
1b986589 | 4590 | static bool check_refcount_ok(const struct bpf_func_proto *fn, int func_id) |
fd978bf7 JS |
4591 | { |
4592 | int count = 0; | |
4593 | ||
1b986589 | 4594 | if (arg_type_may_be_refcounted(fn->arg1_type)) |
fd978bf7 | 4595 | count++; |
1b986589 | 4596 | if (arg_type_may_be_refcounted(fn->arg2_type)) |
fd978bf7 | 4597 | count++; |
1b986589 | 4598 | if (arg_type_may_be_refcounted(fn->arg3_type)) |
fd978bf7 | 4599 | count++; |
1b986589 | 4600 | if (arg_type_may_be_refcounted(fn->arg4_type)) |
fd978bf7 | 4601 | count++; |
1b986589 | 4602 | if (arg_type_may_be_refcounted(fn->arg5_type)) |
fd978bf7 JS |
4603 | count++; |
4604 | ||
1b986589 MKL |
4605 | /* A reference acquiring function cannot acquire |
4606 | * another refcounted ptr. | |
4607 | */ | |
64d85290 | 4608 | if (may_be_acquire_function(func_id) && count) |
1b986589 MKL |
4609 | return false; |
4610 | ||
fd978bf7 JS |
4611 | /* We only support one arg being unreferenced at the moment, |
4612 | * which is sufficient for the helper functions we have right now. | |
4613 | */ | |
4614 | return count <= 1; | |
4615 | } | |
4616 | ||
9436ef6e LB |
4617 | static bool check_btf_id_ok(const struct bpf_func_proto *fn) |
4618 | { | |
4619 | int i; | |
4620 | ||
1df8f55a | 4621 | for (i = 0; i < ARRAY_SIZE(fn->arg_type); i++) { |
9436ef6e LB |
4622 | if (fn->arg_type[i] == ARG_PTR_TO_BTF_ID && !fn->arg_btf_id[i]) |
4623 | return false; | |
4624 | ||
1df8f55a MKL |
4625 | if (fn->arg_type[i] != ARG_PTR_TO_BTF_ID && fn->arg_btf_id[i]) |
4626 | return false; | |
4627 | } | |
4628 | ||
9436ef6e LB |
4629 | return true; |
4630 | } | |
4631 | ||
1b986589 | 4632 | static int check_func_proto(const struct bpf_func_proto *fn, int func_id) |
90133415 DB |
4633 | { |
4634 | return check_raw_mode_ok(fn) && | |
fd978bf7 | 4635 | check_arg_pair_ok(fn) && |
9436ef6e | 4636 | check_btf_id_ok(fn) && |
1b986589 | 4637 | check_refcount_ok(fn, func_id) ? 0 : -EINVAL; |
435faee1 DB |
4638 | } |
4639 | ||
de8f3a83 DB |
4640 | /* Packet data might have moved, any old PTR_TO_PACKET[_META,_END] |
4641 | * are now invalid, so turn them into unknown SCALAR_VALUE. | |
f1174f77 | 4642 | */ |
f4d7e40a AS |
4643 | static void __clear_all_pkt_pointers(struct bpf_verifier_env *env, |
4644 | struct bpf_func_state *state) | |
969bf05e | 4645 | { |
58e2af8b | 4646 | struct bpf_reg_state *regs = state->regs, *reg; |
969bf05e AS |
4647 | int i; |
4648 | ||
4649 | for (i = 0; i < MAX_BPF_REG; i++) | |
de8f3a83 | 4650 | if (reg_is_pkt_pointer_any(®s[i])) |
61bd5218 | 4651 | mark_reg_unknown(env, regs, i); |
969bf05e | 4652 | |
f3709f69 JS |
4653 | bpf_for_each_spilled_reg(i, state, reg) { |
4654 | if (!reg) | |
969bf05e | 4655 | continue; |
de8f3a83 | 4656 | if (reg_is_pkt_pointer_any(reg)) |
f54c7898 | 4657 | __mark_reg_unknown(env, reg); |
969bf05e AS |
4658 | } |
4659 | } | |
4660 | ||
f4d7e40a AS |
4661 | static void clear_all_pkt_pointers(struct bpf_verifier_env *env) |
4662 | { | |
4663 | struct bpf_verifier_state *vstate = env->cur_state; | |
4664 | int i; | |
4665 | ||
4666 | for (i = 0; i <= vstate->curframe; i++) | |
4667 | __clear_all_pkt_pointers(env, vstate->frame[i]); | |
4668 | } | |
4669 | ||
fd978bf7 | 4670 | static void release_reg_references(struct bpf_verifier_env *env, |
1b986589 MKL |
4671 | struct bpf_func_state *state, |
4672 | int ref_obj_id) | |
fd978bf7 JS |
4673 | { |
4674 | struct bpf_reg_state *regs = state->regs, *reg; | |
4675 | int i; | |
4676 | ||
4677 | for (i = 0; i < MAX_BPF_REG; i++) | |
1b986589 | 4678 | if (regs[i].ref_obj_id == ref_obj_id) |
fd978bf7 JS |
4679 | mark_reg_unknown(env, regs, i); |
4680 | ||
4681 | bpf_for_each_spilled_reg(i, state, reg) { | |
4682 | if (!reg) | |
4683 | continue; | |
1b986589 | 4684 | if (reg->ref_obj_id == ref_obj_id) |
f54c7898 | 4685 | __mark_reg_unknown(env, reg); |
fd978bf7 JS |
4686 | } |
4687 | } | |
4688 | ||
4689 | /* The pointer with the specified id has released its reference to kernel | |
4690 | * resources. Identify all copies of the same pointer and clear the reference. | |
4691 | */ | |
4692 | static int release_reference(struct bpf_verifier_env *env, | |
1b986589 | 4693 | int ref_obj_id) |
fd978bf7 JS |
4694 | { |
4695 | struct bpf_verifier_state *vstate = env->cur_state; | |
1b986589 | 4696 | int err; |
fd978bf7 JS |
4697 | int i; |
4698 | ||
1b986589 MKL |
4699 | err = release_reference_state(cur_func(env), ref_obj_id); |
4700 | if (err) | |
4701 | return err; | |
4702 | ||
fd978bf7 | 4703 | for (i = 0; i <= vstate->curframe; i++) |
1b986589 | 4704 | release_reg_references(env, vstate->frame[i], ref_obj_id); |
fd978bf7 | 4705 | |
1b986589 | 4706 | return 0; |
fd978bf7 JS |
4707 | } |
4708 | ||
51c39bb1 AS |
4709 | static void clear_caller_saved_regs(struct bpf_verifier_env *env, |
4710 | struct bpf_reg_state *regs) | |
4711 | { | |
4712 | int i; | |
4713 | ||
4714 | /* after the call registers r0 - r5 were scratched */ | |
4715 | for (i = 0; i < CALLER_SAVED_REGS; i++) { | |
4716 | mark_reg_not_init(env, regs, caller_saved[i]); | |
4717 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); | |
4718 | } | |
4719 | } | |
4720 | ||
f4d7e40a AS |
4721 | static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, |
4722 | int *insn_idx) | |
4723 | { | |
4724 | struct bpf_verifier_state *state = env->cur_state; | |
51c39bb1 | 4725 | struct bpf_func_info_aux *func_info_aux; |
f4d7e40a | 4726 | struct bpf_func_state *caller, *callee; |
fd978bf7 | 4727 | int i, err, subprog, target_insn; |
51c39bb1 | 4728 | bool is_global = false; |
f4d7e40a | 4729 | |
aada9ce6 | 4730 | if (state->curframe + 1 >= MAX_CALL_FRAMES) { |
f4d7e40a | 4731 | verbose(env, "the call stack of %d frames is too deep\n", |
aada9ce6 | 4732 | state->curframe + 2); |
f4d7e40a AS |
4733 | return -E2BIG; |
4734 | } | |
4735 | ||
4736 | target_insn = *insn_idx + insn->imm; | |
4737 | subprog = find_subprog(env, target_insn + 1); | |
4738 | if (subprog < 0) { | |
4739 | verbose(env, "verifier bug. No program starts at insn %d\n", | |
4740 | target_insn + 1); | |
4741 | return -EFAULT; | |
4742 | } | |
4743 | ||
4744 | caller = state->frame[state->curframe]; | |
4745 | if (state->frame[state->curframe + 1]) { | |
4746 | verbose(env, "verifier bug. Frame %d already allocated\n", | |
4747 | state->curframe + 1); | |
4748 | return -EFAULT; | |
4749 | } | |
4750 | ||
51c39bb1 AS |
4751 | func_info_aux = env->prog->aux->func_info_aux; |
4752 | if (func_info_aux) | |
4753 | is_global = func_info_aux[subprog].linkage == BTF_FUNC_GLOBAL; | |
4754 | err = btf_check_func_arg_match(env, subprog, caller->regs); | |
4755 | if (err == -EFAULT) | |
4756 | return err; | |
4757 | if (is_global) { | |
4758 | if (err) { | |
4759 | verbose(env, "Caller passes invalid args into func#%d\n", | |
4760 | subprog); | |
4761 | return err; | |
4762 | } else { | |
4763 | if (env->log.level & BPF_LOG_LEVEL) | |
4764 | verbose(env, | |
4765 | "Func#%d is global and valid. Skipping.\n", | |
4766 | subprog); | |
4767 | clear_caller_saved_regs(env, caller->regs); | |
4768 | ||
4769 | /* All global functions return SCALAR_VALUE */ | |
4770 | mark_reg_unknown(env, caller->regs, BPF_REG_0); | |
4771 | ||
4772 | /* continue with next insn after call */ | |
4773 | return 0; | |
4774 | } | |
4775 | } | |
4776 | ||
f4d7e40a AS |
4777 | callee = kzalloc(sizeof(*callee), GFP_KERNEL); |
4778 | if (!callee) | |
4779 | return -ENOMEM; | |
4780 | state->frame[state->curframe + 1] = callee; | |
4781 | ||
4782 | /* callee cannot access r0, r6 - r9 for reading and has to write | |
4783 | * into its own stack before reading from it. | |
4784 | * callee can read/write into caller's stack | |
4785 | */ | |
4786 | init_func_state(env, callee, | |
4787 | /* remember the callsite, it will be used by bpf_exit */ | |
4788 | *insn_idx /* callsite */, | |
4789 | state->curframe + 1 /* frameno within this callchain */, | |
f910cefa | 4790 | subprog /* subprog number within this prog */); |
f4d7e40a | 4791 | |
fd978bf7 JS |
4792 | /* Transfer references to the callee */ |
4793 | err = transfer_reference_state(callee, caller); | |
4794 | if (err) | |
4795 | return err; | |
4796 | ||
679c782d EC |
4797 | /* copy r1 - r5 args that callee can access. The copy includes parent |
4798 | * pointers, which connects us up to the liveness chain | |
4799 | */ | |
f4d7e40a AS |
4800 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) |
4801 | callee->regs[i] = caller->regs[i]; | |
4802 | ||
51c39bb1 | 4803 | clear_caller_saved_regs(env, caller->regs); |
f4d7e40a AS |
4804 | |
4805 | /* only increment it after check_reg_arg() finished */ | |
4806 | state->curframe++; | |
4807 | ||
4808 | /* and go analyze first insn of the callee */ | |
4809 | *insn_idx = target_insn; | |
4810 | ||
06ee7115 | 4811 | if (env->log.level & BPF_LOG_LEVEL) { |
f4d7e40a AS |
4812 | verbose(env, "caller:\n"); |
4813 | print_verifier_state(env, caller); | |
4814 | verbose(env, "callee:\n"); | |
4815 | print_verifier_state(env, callee); | |
4816 | } | |
4817 | return 0; | |
4818 | } | |
4819 | ||
4820 | static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx) | |
4821 | { | |
4822 | struct bpf_verifier_state *state = env->cur_state; | |
4823 | struct bpf_func_state *caller, *callee; | |
4824 | struct bpf_reg_state *r0; | |
fd978bf7 | 4825 | int err; |
f4d7e40a AS |
4826 | |
4827 | callee = state->frame[state->curframe]; | |
4828 | r0 = &callee->regs[BPF_REG_0]; | |
4829 | if (r0->type == PTR_TO_STACK) { | |
4830 | /* technically it's ok to return caller's stack pointer | |
4831 | * (or caller's caller's pointer) back to the caller, | |
4832 | * since these pointers are valid. Only current stack | |
4833 | * pointer will be invalid as soon as function exits, | |
4834 | * but let's be conservative | |
4835 | */ | |
4836 | verbose(env, "cannot return stack pointer to the caller\n"); | |
4837 | return -EINVAL; | |
4838 | } | |
4839 | ||
4840 | state->curframe--; | |
4841 | caller = state->frame[state->curframe]; | |
4842 | /* return to the caller whatever r0 had in the callee */ | |
4843 | caller->regs[BPF_REG_0] = *r0; | |
4844 | ||
fd978bf7 JS |
4845 | /* Transfer references to the caller */ |
4846 | err = transfer_reference_state(caller, callee); | |
4847 | if (err) | |
4848 | return err; | |
4849 | ||
f4d7e40a | 4850 | *insn_idx = callee->callsite + 1; |
06ee7115 | 4851 | if (env->log.level & BPF_LOG_LEVEL) { |
f4d7e40a AS |
4852 | verbose(env, "returning from callee:\n"); |
4853 | print_verifier_state(env, callee); | |
4854 | verbose(env, "to caller at %d:\n", *insn_idx); | |
4855 | print_verifier_state(env, caller); | |
4856 | } | |
4857 | /* clear everything in the callee */ | |
4858 | free_func_state(callee); | |
4859 | state->frame[state->curframe + 1] = NULL; | |
4860 | return 0; | |
4861 | } | |
4862 | ||
849fa506 YS |
4863 | static void do_refine_retval_range(struct bpf_reg_state *regs, int ret_type, |
4864 | int func_id, | |
4865 | struct bpf_call_arg_meta *meta) | |
4866 | { | |
4867 | struct bpf_reg_state *ret_reg = ®s[BPF_REG_0]; | |
4868 | ||
4869 | if (ret_type != RET_INTEGER || | |
4870 | (func_id != BPF_FUNC_get_stack && | |
47cc0ed5 DB |
4871 | func_id != BPF_FUNC_probe_read_str && |
4872 | func_id != BPF_FUNC_probe_read_kernel_str && | |
4873 | func_id != BPF_FUNC_probe_read_user_str)) | |
849fa506 YS |
4874 | return; |
4875 | ||
10060503 | 4876 | ret_reg->smax_value = meta->msize_max_value; |
fa123ac0 | 4877 | ret_reg->s32_max_value = meta->msize_max_value; |
849fa506 YS |
4878 | __reg_deduce_bounds(ret_reg); |
4879 | __reg_bound_offset(ret_reg); | |
10060503 | 4880 | __update_reg_bounds(ret_reg); |
849fa506 YS |
4881 | } |
4882 | ||
c93552c4 DB |
4883 | static int |
4884 | record_func_map(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, | |
4885 | int func_id, int insn_idx) | |
4886 | { | |
4887 | struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; | |
591fe988 | 4888 | struct bpf_map *map = meta->map_ptr; |
c93552c4 DB |
4889 | |
4890 | if (func_id != BPF_FUNC_tail_call && | |
09772d92 DB |
4891 | func_id != BPF_FUNC_map_lookup_elem && |
4892 | func_id != BPF_FUNC_map_update_elem && | |
f1a2e44a MV |
4893 | func_id != BPF_FUNC_map_delete_elem && |
4894 | func_id != BPF_FUNC_map_push_elem && | |
4895 | func_id != BPF_FUNC_map_pop_elem && | |
4896 | func_id != BPF_FUNC_map_peek_elem) | |
c93552c4 | 4897 | return 0; |
09772d92 | 4898 | |
591fe988 | 4899 | if (map == NULL) { |
c93552c4 DB |
4900 | verbose(env, "kernel subsystem misconfigured verifier\n"); |
4901 | return -EINVAL; | |
4902 | } | |
4903 | ||
591fe988 DB |
4904 | /* In case of read-only, some additional restrictions |
4905 | * need to be applied in order to prevent altering the | |
4906 | * state of the map from program side. | |
4907 | */ | |
4908 | if ((map->map_flags & BPF_F_RDONLY_PROG) && | |
4909 | (func_id == BPF_FUNC_map_delete_elem || | |
4910 | func_id == BPF_FUNC_map_update_elem || | |
4911 | func_id == BPF_FUNC_map_push_elem || | |
4912 | func_id == BPF_FUNC_map_pop_elem)) { | |
4913 | verbose(env, "write into map forbidden\n"); | |
4914 | return -EACCES; | |
4915 | } | |
4916 | ||
d2e4c1e6 | 4917 | if (!BPF_MAP_PTR(aux->map_ptr_state)) |
c93552c4 | 4918 | bpf_map_ptr_store(aux, meta->map_ptr, |
2c78ee89 | 4919 | !meta->map_ptr->bypass_spec_v1); |
d2e4c1e6 | 4920 | else if (BPF_MAP_PTR(aux->map_ptr_state) != meta->map_ptr) |
c93552c4 | 4921 | bpf_map_ptr_store(aux, BPF_MAP_PTR_POISON, |
2c78ee89 | 4922 | !meta->map_ptr->bypass_spec_v1); |
c93552c4 DB |
4923 | return 0; |
4924 | } | |
4925 | ||
d2e4c1e6 DB |
4926 | static int |
4927 | record_func_key(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, | |
4928 | int func_id, int insn_idx) | |
4929 | { | |
4930 | struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; | |
4931 | struct bpf_reg_state *regs = cur_regs(env), *reg; | |
4932 | struct bpf_map *map = meta->map_ptr; | |
4933 | struct tnum range; | |
4934 | u64 val; | |
cc52d914 | 4935 | int err; |
d2e4c1e6 DB |
4936 | |
4937 | if (func_id != BPF_FUNC_tail_call) | |
4938 | return 0; | |
4939 | if (!map || map->map_type != BPF_MAP_TYPE_PROG_ARRAY) { | |
4940 | verbose(env, "kernel subsystem misconfigured verifier\n"); | |
4941 | return -EINVAL; | |
4942 | } | |
4943 | ||
4944 | range = tnum_range(0, map->max_entries - 1); | |
4945 | reg = ®s[BPF_REG_3]; | |
4946 | ||
4947 | if (!register_is_const(reg) || !tnum_in(range, reg->var_off)) { | |
4948 | bpf_map_key_store(aux, BPF_MAP_KEY_POISON); | |
4949 | return 0; | |
4950 | } | |
4951 | ||
cc52d914 DB |
4952 | err = mark_chain_precision(env, BPF_REG_3); |
4953 | if (err) | |
4954 | return err; | |
4955 | ||
d2e4c1e6 DB |
4956 | val = reg->var_off.value; |
4957 | if (bpf_map_key_unseen(aux)) | |
4958 | bpf_map_key_store(aux, val); | |
4959 | else if (!bpf_map_key_poisoned(aux) && | |
4960 | bpf_map_key_immediate(aux) != val) | |
4961 | bpf_map_key_store(aux, BPF_MAP_KEY_POISON); | |
4962 | return 0; | |
4963 | } | |
4964 | ||
fd978bf7 JS |
4965 | static int check_reference_leak(struct bpf_verifier_env *env) |
4966 | { | |
4967 | struct bpf_func_state *state = cur_func(env); | |
4968 | int i; | |
4969 | ||
4970 | for (i = 0; i < state->acquired_refs; i++) { | |
4971 | verbose(env, "Unreleased reference id=%d alloc_insn=%d\n", | |
4972 | state->refs[i].id, state->refs[i].insn_idx); | |
4973 | } | |
4974 | return state->acquired_refs ? -EINVAL : 0; | |
4975 | } | |
4976 | ||
f4d7e40a | 4977 | static int check_helper_call(struct bpf_verifier_env *env, int func_id, int insn_idx) |
17a52670 | 4978 | { |
17a52670 | 4979 | const struct bpf_func_proto *fn = NULL; |
638f5b90 | 4980 | struct bpf_reg_state *regs; |
33ff9823 | 4981 | struct bpf_call_arg_meta meta; |
969bf05e | 4982 | bool changes_data; |
17a52670 AS |
4983 | int i, err; |
4984 | ||
4985 | /* find function prototype */ | |
4986 | if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) { | |
61bd5218 JK |
4987 | verbose(env, "invalid func %s#%d\n", func_id_name(func_id), |
4988 | func_id); | |
17a52670 AS |
4989 | return -EINVAL; |
4990 | } | |
4991 | ||
00176a34 | 4992 | if (env->ops->get_func_proto) |
5e43f899 | 4993 | fn = env->ops->get_func_proto(func_id, env->prog); |
17a52670 | 4994 | if (!fn) { |
61bd5218 JK |
4995 | verbose(env, "unknown func %s#%d\n", func_id_name(func_id), |
4996 | func_id); | |
17a52670 AS |
4997 | return -EINVAL; |
4998 | } | |
4999 | ||
5000 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ | |
24701ece | 5001 | if (!env->prog->gpl_compatible && fn->gpl_only) { |
3fe2867c | 5002 | verbose(env, "cannot call GPL-restricted function from non-GPL compatible program\n"); |
17a52670 AS |
5003 | return -EINVAL; |
5004 | } | |
5005 | ||
eae2e83e JO |
5006 | if (fn->allowed && !fn->allowed(env->prog)) { |
5007 | verbose(env, "helper call is not allowed in probe\n"); | |
5008 | return -EINVAL; | |
5009 | } | |
5010 | ||
04514d13 | 5011 | /* With LD_ABS/IND some JITs save/restore skb from r1. */ |
17bedab2 | 5012 | changes_data = bpf_helper_changes_pkt_data(fn->func); |
04514d13 DB |
5013 | if (changes_data && fn->arg1_type != ARG_PTR_TO_CTX) { |
5014 | verbose(env, "kernel subsystem misconfigured func %s#%d: r1 != ctx\n", | |
5015 | func_id_name(func_id), func_id); | |
5016 | return -EINVAL; | |
5017 | } | |
969bf05e | 5018 | |
33ff9823 | 5019 | memset(&meta, 0, sizeof(meta)); |
36bbef52 | 5020 | meta.pkt_access = fn->pkt_access; |
33ff9823 | 5021 | |
1b986589 | 5022 | err = check_func_proto(fn, func_id); |
435faee1 | 5023 | if (err) { |
61bd5218 | 5024 | verbose(env, "kernel subsystem misconfigured func %s#%d\n", |
ebb676da | 5025 | func_id_name(func_id), func_id); |
435faee1 DB |
5026 | return err; |
5027 | } | |
5028 | ||
d83525ca | 5029 | meta.func_id = func_id; |
17a52670 | 5030 | /* check args */ |
a7658e1a | 5031 | for (i = 0; i < 5; i++) { |
af7ec138 | 5032 | err = check_func_arg(env, i, &meta, fn); |
a7658e1a AS |
5033 | if (err) |
5034 | return err; | |
5035 | } | |
17a52670 | 5036 | |
c93552c4 DB |
5037 | err = record_func_map(env, &meta, func_id, insn_idx); |
5038 | if (err) | |
5039 | return err; | |
5040 | ||
d2e4c1e6 DB |
5041 | err = record_func_key(env, &meta, func_id, insn_idx); |
5042 | if (err) | |
5043 | return err; | |
5044 | ||
435faee1 DB |
5045 | /* Mark slots with STACK_MISC in case of raw mode, stack offset |
5046 | * is inferred from register state. | |
5047 | */ | |
5048 | for (i = 0; i < meta.access_size; i++) { | |
ca369602 DB |
5049 | err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B, |
5050 | BPF_WRITE, -1, false); | |
435faee1 DB |
5051 | if (err) |
5052 | return err; | |
5053 | } | |
5054 | ||
fd978bf7 JS |
5055 | if (func_id == BPF_FUNC_tail_call) { |
5056 | err = check_reference_leak(env); | |
5057 | if (err) { | |
5058 | verbose(env, "tail_call would lead to reference leak\n"); | |
5059 | return err; | |
5060 | } | |
5061 | } else if (is_release_function(func_id)) { | |
1b986589 | 5062 | err = release_reference(env, meta.ref_obj_id); |
46f8bc92 MKL |
5063 | if (err) { |
5064 | verbose(env, "func %s#%d reference has not been acquired before\n", | |
5065 | func_id_name(func_id), func_id); | |
fd978bf7 | 5066 | return err; |
46f8bc92 | 5067 | } |
fd978bf7 JS |
5068 | } |
5069 | ||
638f5b90 | 5070 | regs = cur_regs(env); |
cd339431 RG |
5071 | |
5072 | /* check that flags argument in get_local_storage(map, flags) is 0, | |
5073 | * this is required because get_local_storage() can't return an error. | |
5074 | */ | |
5075 | if (func_id == BPF_FUNC_get_local_storage && | |
5076 | !register_is_null(®s[BPF_REG_2])) { | |
5077 | verbose(env, "get_local_storage() doesn't support non-zero flags\n"); | |
5078 | return -EINVAL; | |
5079 | } | |
5080 | ||
17a52670 | 5081 | /* reset caller saved regs */ |
dc503a8a | 5082 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
61bd5218 | 5083 | mark_reg_not_init(env, regs, caller_saved[i]); |
dc503a8a EC |
5084 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
5085 | } | |
17a52670 | 5086 | |
5327ed3d JW |
5087 | /* helper call returns 64-bit value. */ |
5088 | regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG; | |
5089 | ||
dc503a8a | 5090 | /* update return register (already marked as written above) */ |
17a52670 | 5091 | if (fn->ret_type == RET_INTEGER) { |
f1174f77 | 5092 | /* sets type to SCALAR_VALUE */ |
61bd5218 | 5093 | mark_reg_unknown(env, regs, BPF_REG_0); |
17a52670 AS |
5094 | } else if (fn->ret_type == RET_VOID) { |
5095 | regs[BPF_REG_0].type = NOT_INIT; | |
3e6a4b3e RG |
5096 | } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL || |
5097 | fn->ret_type == RET_PTR_TO_MAP_VALUE) { | |
f1174f77 | 5098 | /* There is no offset yet applied, variable or fixed */ |
61bd5218 | 5099 | mark_reg_known_zero(env, regs, BPF_REG_0); |
17a52670 AS |
5100 | /* remember map_ptr, so that check_map_access() |
5101 | * can check 'value_size' boundary of memory access | |
5102 | * to map element returned from bpf_map_lookup_elem() | |
5103 | */ | |
33ff9823 | 5104 | if (meta.map_ptr == NULL) { |
61bd5218 JK |
5105 | verbose(env, |
5106 | "kernel subsystem misconfigured verifier\n"); | |
17a52670 AS |
5107 | return -EINVAL; |
5108 | } | |
33ff9823 | 5109 | regs[BPF_REG_0].map_ptr = meta.map_ptr; |
4d31f301 DB |
5110 | if (fn->ret_type == RET_PTR_TO_MAP_VALUE) { |
5111 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE; | |
e16d2f1a AS |
5112 | if (map_value_has_spin_lock(meta.map_ptr)) |
5113 | regs[BPF_REG_0].id = ++env->id_gen; | |
4d31f301 DB |
5114 | } else { |
5115 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL; | |
5116 | regs[BPF_REG_0].id = ++env->id_gen; | |
5117 | } | |
c64b7983 JS |
5118 | } else if (fn->ret_type == RET_PTR_TO_SOCKET_OR_NULL) { |
5119 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
5120 | regs[BPF_REG_0].type = PTR_TO_SOCKET_OR_NULL; | |
0f3adc28 | 5121 | regs[BPF_REG_0].id = ++env->id_gen; |
85a51f8c LB |
5122 | } else if (fn->ret_type == RET_PTR_TO_SOCK_COMMON_OR_NULL) { |
5123 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
5124 | regs[BPF_REG_0].type = PTR_TO_SOCK_COMMON_OR_NULL; | |
5125 | regs[BPF_REG_0].id = ++env->id_gen; | |
655a51e5 MKL |
5126 | } else if (fn->ret_type == RET_PTR_TO_TCP_SOCK_OR_NULL) { |
5127 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
5128 | regs[BPF_REG_0].type = PTR_TO_TCP_SOCK_OR_NULL; | |
5129 | regs[BPF_REG_0].id = ++env->id_gen; | |
457f4436 AN |
5130 | } else if (fn->ret_type == RET_PTR_TO_ALLOC_MEM_OR_NULL) { |
5131 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
5132 | regs[BPF_REG_0].type = PTR_TO_MEM_OR_NULL; | |
5133 | regs[BPF_REG_0].id = ++env->id_gen; | |
5134 | regs[BPF_REG_0].mem_size = meta.mem_size; | |
63d9b80d HL |
5135 | } else if (fn->ret_type == RET_PTR_TO_MEM_OR_BTF_ID_OR_NULL || |
5136 | fn->ret_type == RET_PTR_TO_MEM_OR_BTF_ID) { | |
eaa6bcb7 HL |
5137 | const struct btf_type *t; |
5138 | ||
5139 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
5140 | t = btf_type_skip_modifiers(btf_vmlinux, meta.ret_btf_id, NULL); | |
5141 | if (!btf_type_is_struct(t)) { | |
5142 | u32 tsize; | |
5143 | const struct btf_type *ret; | |
5144 | const char *tname; | |
5145 | ||
5146 | /* resolve the type size of ksym. */ | |
5147 | ret = btf_resolve_size(btf_vmlinux, t, &tsize); | |
5148 | if (IS_ERR(ret)) { | |
5149 | tname = btf_name_by_offset(btf_vmlinux, t->name_off); | |
5150 | verbose(env, "unable to resolve the size of type '%s': %ld\n", | |
5151 | tname, PTR_ERR(ret)); | |
5152 | return -EINVAL; | |
5153 | } | |
63d9b80d HL |
5154 | regs[BPF_REG_0].type = |
5155 | fn->ret_type == RET_PTR_TO_MEM_OR_BTF_ID ? | |
5156 | PTR_TO_MEM : PTR_TO_MEM_OR_NULL; | |
eaa6bcb7 HL |
5157 | regs[BPF_REG_0].mem_size = tsize; |
5158 | } else { | |
63d9b80d HL |
5159 | regs[BPF_REG_0].type = |
5160 | fn->ret_type == RET_PTR_TO_MEM_OR_BTF_ID ? | |
5161 | PTR_TO_BTF_ID : PTR_TO_BTF_ID_OR_NULL; | |
eaa6bcb7 HL |
5162 | regs[BPF_REG_0].btf_id = meta.ret_btf_id; |
5163 | } | |
af7ec138 YS |
5164 | } else if (fn->ret_type == RET_PTR_TO_BTF_ID_OR_NULL) { |
5165 | int ret_btf_id; | |
5166 | ||
5167 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
5168 | regs[BPF_REG_0].type = PTR_TO_BTF_ID_OR_NULL; | |
5169 | ret_btf_id = *fn->ret_btf_id; | |
5170 | if (ret_btf_id == 0) { | |
5171 | verbose(env, "invalid return type %d of func %s#%d\n", | |
5172 | fn->ret_type, func_id_name(func_id), func_id); | |
5173 | return -EINVAL; | |
5174 | } | |
5175 | regs[BPF_REG_0].btf_id = ret_btf_id; | |
17a52670 | 5176 | } else { |
61bd5218 | 5177 | verbose(env, "unknown return type %d of func %s#%d\n", |
ebb676da | 5178 | fn->ret_type, func_id_name(func_id), func_id); |
17a52670 AS |
5179 | return -EINVAL; |
5180 | } | |
04fd61ab | 5181 | |
0f3adc28 | 5182 | if (is_ptr_cast_function(func_id)) { |
1b986589 MKL |
5183 | /* For release_reference() */ |
5184 | regs[BPF_REG_0].ref_obj_id = meta.ref_obj_id; | |
64d85290 | 5185 | } else if (is_acquire_function(func_id, meta.map_ptr)) { |
0f3adc28 LB |
5186 | int id = acquire_reference_state(env, insn_idx); |
5187 | ||
5188 | if (id < 0) | |
5189 | return id; | |
5190 | /* For mark_ptr_or_null_reg() */ | |
5191 | regs[BPF_REG_0].id = id; | |
5192 | /* For release_reference() */ | |
5193 | regs[BPF_REG_0].ref_obj_id = id; | |
5194 | } | |
1b986589 | 5195 | |
849fa506 YS |
5196 | do_refine_retval_range(regs, fn->ret_type, func_id, &meta); |
5197 | ||
61bd5218 | 5198 | err = check_map_func_compatibility(env, meta.map_ptr, func_id); |
35578d79 KX |
5199 | if (err) |
5200 | return err; | |
04fd61ab | 5201 | |
fa28dcb8 SL |
5202 | if ((func_id == BPF_FUNC_get_stack || |
5203 | func_id == BPF_FUNC_get_task_stack) && | |
5204 | !env->prog->has_callchain_buf) { | |
c195651e YS |
5205 | const char *err_str; |
5206 | ||
5207 | #ifdef CONFIG_PERF_EVENTS | |
5208 | err = get_callchain_buffers(sysctl_perf_event_max_stack); | |
5209 | err_str = "cannot get callchain buffer for func %s#%d\n"; | |
5210 | #else | |
5211 | err = -ENOTSUPP; | |
5212 | err_str = "func %s#%d not supported without CONFIG_PERF_EVENTS\n"; | |
5213 | #endif | |
5214 | if (err) { | |
5215 | verbose(env, err_str, func_id_name(func_id), func_id); | |
5216 | return err; | |
5217 | } | |
5218 | ||
5219 | env->prog->has_callchain_buf = true; | |
5220 | } | |
5221 | ||
5d99cb2c SL |
5222 | if (func_id == BPF_FUNC_get_stackid || func_id == BPF_FUNC_get_stack) |
5223 | env->prog->call_get_stack = true; | |
5224 | ||
969bf05e AS |
5225 | if (changes_data) |
5226 | clear_all_pkt_pointers(env); | |
5227 | return 0; | |
5228 | } | |
5229 | ||
b03c9f9f EC |
5230 | static bool signed_add_overflows(s64 a, s64 b) |
5231 | { | |
5232 | /* Do the add in u64, where overflow is well-defined */ | |
5233 | s64 res = (s64)((u64)a + (u64)b); | |
5234 | ||
5235 | if (b < 0) | |
5236 | return res > a; | |
5237 | return res < a; | |
5238 | } | |
5239 | ||
3f50f132 JF |
5240 | static bool signed_add32_overflows(s64 a, s64 b) |
5241 | { | |
5242 | /* Do the add in u32, where overflow is well-defined */ | |
5243 | s32 res = (s32)((u32)a + (u32)b); | |
5244 | ||
5245 | if (b < 0) | |
5246 | return res > a; | |
5247 | return res < a; | |
5248 | } | |
5249 | ||
5250 | static bool signed_sub_overflows(s32 a, s32 b) | |
b03c9f9f EC |
5251 | { |
5252 | /* Do the sub in u64, where overflow is well-defined */ | |
5253 | s64 res = (s64)((u64)a - (u64)b); | |
5254 | ||
5255 | if (b < 0) | |
5256 | return res < a; | |
5257 | return res > a; | |
969bf05e AS |
5258 | } |
5259 | ||
3f50f132 JF |
5260 | static bool signed_sub32_overflows(s32 a, s32 b) |
5261 | { | |
5262 | /* Do the sub in u64, where overflow is well-defined */ | |
5263 | s32 res = (s32)((u32)a - (u32)b); | |
5264 | ||
5265 | if (b < 0) | |
5266 | return res < a; | |
5267 | return res > a; | |
5268 | } | |
5269 | ||
bb7f0f98 AS |
5270 | static bool check_reg_sane_offset(struct bpf_verifier_env *env, |
5271 | const struct bpf_reg_state *reg, | |
5272 | enum bpf_reg_type type) | |
5273 | { | |
5274 | bool known = tnum_is_const(reg->var_off); | |
5275 | s64 val = reg->var_off.value; | |
5276 | s64 smin = reg->smin_value; | |
5277 | ||
5278 | if (known && (val >= BPF_MAX_VAR_OFF || val <= -BPF_MAX_VAR_OFF)) { | |
5279 | verbose(env, "math between %s pointer and %lld is not allowed\n", | |
5280 | reg_type_str[type], val); | |
5281 | return false; | |
5282 | } | |
5283 | ||
5284 | if (reg->off >= BPF_MAX_VAR_OFF || reg->off <= -BPF_MAX_VAR_OFF) { | |
5285 | verbose(env, "%s pointer offset %d is not allowed\n", | |
5286 | reg_type_str[type], reg->off); | |
5287 | return false; | |
5288 | } | |
5289 | ||
5290 | if (smin == S64_MIN) { | |
5291 | verbose(env, "math between %s pointer and register with unbounded min value is not allowed\n", | |
5292 | reg_type_str[type]); | |
5293 | return false; | |
5294 | } | |
5295 | ||
5296 | if (smin >= BPF_MAX_VAR_OFF || smin <= -BPF_MAX_VAR_OFF) { | |
5297 | verbose(env, "value %lld makes %s pointer be out of bounds\n", | |
5298 | smin, reg_type_str[type]); | |
5299 | return false; | |
5300 | } | |
5301 | ||
5302 | return true; | |
5303 | } | |
5304 | ||
979d63d5 DB |
5305 | static struct bpf_insn_aux_data *cur_aux(struct bpf_verifier_env *env) |
5306 | { | |
5307 | return &env->insn_aux_data[env->insn_idx]; | |
5308 | } | |
5309 | ||
5310 | static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg, | |
5311 | u32 *ptr_limit, u8 opcode, bool off_is_neg) | |
5312 | { | |
5313 | bool mask_to_left = (opcode == BPF_ADD && off_is_neg) || | |
5314 | (opcode == BPF_SUB && !off_is_neg); | |
5315 | u32 off; | |
5316 | ||
5317 | switch (ptr_reg->type) { | |
5318 | case PTR_TO_STACK: | |
088ec26d AI |
5319 | /* Indirect variable offset stack access is prohibited in |
5320 | * unprivileged mode so it's not handled here. | |
5321 | */ | |
979d63d5 DB |
5322 | off = ptr_reg->off + ptr_reg->var_off.value; |
5323 | if (mask_to_left) | |
5324 | *ptr_limit = MAX_BPF_STACK + off; | |
5325 | else | |
5326 | *ptr_limit = -off; | |
5327 | return 0; | |
5328 | case PTR_TO_MAP_VALUE: | |
5329 | if (mask_to_left) { | |
5330 | *ptr_limit = ptr_reg->umax_value + ptr_reg->off; | |
5331 | } else { | |
5332 | off = ptr_reg->smin_value + ptr_reg->off; | |
5333 | *ptr_limit = ptr_reg->map_ptr->value_size - off; | |
5334 | } | |
5335 | return 0; | |
5336 | default: | |
5337 | return -EINVAL; | |
5338 | } | |
5339 | } | |
5340 | ||
d3bd7413 DB |
5341 | static bool can_skip_alu_sanitation(const struct bpf_verifier_env *env, |
5342 | const struct bpf_insn *insn) | |
5343 | { | |
2c78ee89 | 5344 | return env->bypass_spec_v1 || BPF_SRC(insn->code) == BPF_K; |
d3bd7413 DB |
5345 | } |
5346 | ||
5347 | static int update_alu_sanitation_state(struct bpf_insn_aux_data *aux, | |
5348 | u32 alu_state, u32 alu_limit) | |
5349 | { | |
5350 | /* If we arrived here from different branches with different | |
5351 | * state or limits to sanitize, then this won't work. | |
5352 | */ | |
5353 | if (aux->alu_state && | |
5354 | (aux->alu_state != alu_state || | |
5355 | aux->alu_limit != alu_limit)) | |
5356 | return -EACCES; | |
5357 | ||
5358 | /* Corresponding fixup done in fixup_bpf_calls(). */ | |
5359 | aux->alu_state = alu_state; | |
5360 | aux->alu_limit = alu_limit; | |
5361 | return 0; | |
5362 | } | |
5363 | ||
5364 | static int sanitize_val_alu(struct bpf_verifier_env *env, | |
5365 | struct bpf_insn *insn) | |
5366 | { | |
5367 | struct bpf_insn_aux_data *aux = cur_aux(env); | |
5368 | ||
5369 | if (can_skip_alu_sanitation(env, insn)) | |
5370 | return 0; | |
5371 | ||
5372 | return update_alu_sanitation_state(aux, BPF_ALU_NON_POINTER, 0); | |
5373 | } | |
5374 | ||
979d63d5 DB |
5375 | static int sanitize_ptr_alu(struct bpf_verifier_env *env, |
5376 | struct bpf_insn *insn, | |
5377 | const struct bpf_reg_state *ptr_reg, | |
5378 | struct bpf_reg_state *dst_reg, | |
5379 | bool off_is_neg) | |
5380 | { | |
5381 | struct bpf_verifier_state *vstate = env->cur_state; | |
5382 | struct bpf_insn_aux_data *aux = cur_aux(env); | |
5383 | bool ptr_is_dst_reg = ptr_reg == dst_reg; | |
5384 | u8 opcode = BPF_OP(insn->code); | |
5385 | u32 alu_state, alu_limit; | |
5386 | struct bpf_reg_state tmp; | |
5387 | bool ret; | |
5388 | ||
d3bd7413 | 5389 | if (can_skip_alu_sanitation(env, insn)) |
979d63d5 DB |
5390 | return 0; |
5391 | ||
5392 | /* We already marked aux for masking from non-speculative | |
5393 | * paths, thus we got here in the first place. We only care | |
5394 | * to explore bad access from here. | |
5395 | */ | |
5396 | if (vstate->speculative) | |
5397 | goto do_sim; | |
5398 | ||
5399 | alu_state = off_is_neg ? BPF_ALU_NEG_VALUE : 0; | |
5400 | alu_state |= ptr_is_dst_reg ? | |
5401 | BPF_ALU_SANITIZE_SRC : BPF_ALU_SANITIZE_DST; | |
5402 | ||
5403 | if (retrieve_ptr_limit(ptr_reg, &alu_limit, opcode, off_is_neg)) | |
5404 | return 0; | |
d3bd7413 | 5405 | if (update_alu_sanitation_state(aux, alu_state, alu_limit)) |
979d63d5 | 5406 | return -EACCES; |
979d63d5 DB |
5407 | do_sim: |
5408 | /* Simulate and find potential out-of-bounds access under | |
5409 | * speculative execution from truncation as a result of | |
5410 | * masking when off was not within expected range. If off | |
5411 | * sits in dst, then we temporarily need to move ptr there | |
5412 | * to simulate dst (== 0) +/-= ptr. Needed, for example, | |
5413 | * for cases where we use K-based arithmetic in one direction | |
5414 | * and truncated reg-based in the other in order to explore | |
5415 | * bad access. | |
5416 | */ | |
5417 | if (!ptr_is_dst_reg) { | |
5418 | tmp = *dst_reg; | |
5419 | *dst_reg = *ptr_reg; | |
5420 | } | |
5421 | ret = push_stack(env, env->insn_idx + 1, env->insn_idx, true); | |
0803278b | 5422 | if (!ptr_is_dst_reg && ret) |
979d63d5 DB |
5423 | *dst_reg = tmp; |
5424 | return !ret ? -EFAULT : 0; | |
5425 | } | |
5426 | ||
f1174f77 | 5427 | /* Handles arithmetic on a pointer and a scalar: computes new min/max and var_off. |
f1174f77 EC |
5428 | * Caller should also handle BPF_MOV case separately. |
5429 | * If we return -EACCES, caller may want to try again treating pointer as a | |
5430 | * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks. | |
5431 | */ | |
5432 | static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, | |
5433 | struct bpf_insn *insn, | |
5434 | const struct bpf_reg_state *ptr_reg, | |
5435 | const struct bpf_reg_state *off_reg) | |
969bf05e | 5436 | { |
f4d7e40a AS |
5437 | struct bpf_verifier_state *vstate = env->cur_state; |
5438 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
5439 | struct bpf_reg_state *regs = state->regs, *dst_reg; | |
f1174f77 | 5440 | bool known = tnum_is_const(off_reg->var_off); |
b03c9f9f EC |
5441 | s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value, |
5442 | smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value; | |
5443 | u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value, | |
5444 | umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value; | |
9d7eceed | 5445 | u32 dst = insn->dst_reg, src = insn->src_reg; |
969bf05e | 5446 | u8 opcode = BPF_OP(insn->code); |
979d63d5 | 5447 | int ret; |
969bf05e | 5448 | |
f1174f77 | 5449 | dst_reg = ®s[dst]; |
969bf05e | 5450 | |
6f16101e DB |
5451 | if ((known && (smin_val != smax_val || umin_val != umax_val)) || |
5452 | smin_val > smax_val || umin_val > umax_val) { | |
5453 | /* Taint dst register if offset had invalid bounds derived from | |
5454 | * e.g. dead branches. | |
5455 | */ | |
f54c7898 | 5456 | __mark_reg_unknown(env, dst_reg); |
6f16101e | 5457 | return 0; |
f1174f77 EC |
5458 | } |
5459 | ||
5460 | if (BPF_CLASS(insn->code) != BPF_ALU64) { | |
5461 | /* 32-bit ALU ops on pointers produce (meaningless) scalars */ | |
6c693541 YS |
5462 | if (opcode == BPF_SUB && env->allow_ptr_leaks) { |
5463 | __mark_reg_unknown(env, dst_reg); | |
5464 | return 0; | |
5465 | } | |
5466 | ||
82abbf8d AS |
5467 | verbose(env, |
5468 | "R%d 32-bit pointer arithmetic prohibited\n", | |
5469 | dst); | |
f1174f77 | 5470 | return -EACCES; |
969bf05e AS |
5471 | } |
5472 | ||
aad2eeaf JS |
5473 | switch (ptr_reg->type) { |
5474 | case PTR_TO_MAP_VALUE_OR_NULL: | |
5475 | verbose(env, "R%d pointer arithmetic on %s prohibited, null-check it first\n", | |
5476 | dst, reg_type_str[ptr_reg->type]); | |
f1174f77 | 5477 | return -EACCES; |
aad2eeaf | 5478 | case CONST_PTR_TO_MAP: |
7c696732 YS |
5479 | /* smin_val represents the known value */ |
5480 | if (known && smin_val == 0 && opcode == BPF_ADD) | |
5481 | break; | |
8731745e | 5482 | fallthrough; |
aad2eeaf | 5483 | case PTR_TO_PACKET_END: |
c64b7983 JS |
5484 | case PTR_TO_SOCKET: |
5485 | case PTR_TO_SOCKET_OR_NULL: | |
46f8bc92 MKL |
5486 | case PTR_TO_SOCK_COMMON: |
5487 | case PTR_TO_SOCK_COMMON_OR_NULL: | |
655a51e5 MKL |
5488 | case PTR_TO_TCP_SOCK: |
5489 | case PTR_TO_TCP_SOCK_OR_NULL: | |
fada7fdc | 5490 | case PTR_TO_XDP_SOCK: |
aad2eeaf JS |
5491 | verbose(env, "R%d pointer arithmetic on %s prohibited\n", |
5492 | dst, reg_type_str[ptr_reg->type]); | |
f1174f77 | 5493 | return -EACCES; |
9d7eceed DB |
5494 | case PTR_TO_MAP_VALUE: |
5495 | if (!env->allow_ptr_leaks && !known && (smin_val < 0) != (smax_val < 0)) { | |
5496 | verbose(env, "R%d has unknown scalar with mixed signed bounds, pointer arithmetic with it prohibited for !root\n", | |
5497 | off_reg == dst_reg ? dst : src); | |
5498 | return -EACCES; | |
5499 | } | |
df561f66 | 5500 | fallthrough; |
aad2eeaf JS |
5501 | default: |
5502 | break; | |
f1174f77 EC |
5503 | } |
5504 | ||
5505 | /* In case of 'scalar += pointer', dst_reg inherits pointer type and id. | |
5506 | * The id may be overwritten later if we create a new variable offset. | |
969bf05e | 5507 | */ |
f1174f77 EC |
5508 | dst_reg->type = ptr_reg->type; |
5509 | dst_reg->id = ptr_reg->id; | |
969bf05e | 5510 | |
bb7f0f98 AS |
5511 | if (!check_reg_sane_offset(env, off_reg, ptr_reg->type) || |
5512 | !check_reg_sane_offset(env, ptr_reg, ptr_reg->type)) | |
5513 | return -EINVAL; | |
5514 | ||
3f50f132 JF |
5515 | /* pointer types do not carry 32-bit bounds at the moment. */ |
5516 | __mark_reg32_unbounded(dst_reg); | |
5517 | ||
f1174f77 EC |
5518 | switch (opcode) { |
5519 | case BPF_ADD: | |
979d63d5 DB |
5520 | ret = sanitize_ptr_alu(env, insn, ptr_reg, dst_reg, smin_val < 0); |
5521 | if (ret < 0) { | |
5522 | verbose(env, "R%d tried to add from different maps or paths\n", dst); | |
5523 | return ret; | |
5524 | } | |
f1174f77 EC |
5525 | /* We can take a fixed offset as long as it doesn't overflow |
5526 | * the s32 'off' field | |
969bf05e | 5527 | */ |
b03c9f9f EC |
5528 | if (known && (ptr_reg->off + smin_val == |
5529 | (s64)(s32)(ptr_reg->off + smin_val))) { | |
f1174f77 | 5530 | /* pointer += K. Accumulate it into fixed offset */ |
b03c9f9f EC |
5531 | dst_reg->smin_value = smin_ptr; |
5532 | dst_reg->smax_value = smax_ptr; | |
5533 | dst_reg->umin_value = umin_ptr; | |
5534 | dst_reg->umax_value = umax_ptr; | |
f1174f77 | 5535 | dst_reg->var_off = ptr_reg->var_off; |
b03c9f9f | 5536 | dst_reg->off = ptr_reg->off + smin_val; |
0962590e | 5537 | dst_reg->raw = ptr_reg->raw; |
f1174f77 EC |
5538 | break; |
5539 | } | |
f1174f77 EC |
5540 | /* A new variable offset is created. Note that off_reg->off |
5541 | * == 0, since it's a scalar. | |
5542 | * dst_reg gets the pointer type and since some positive | |
5543 | * integer value was added to the pointer, give it a new 'id' | |
5544 | * if it's a PTR_TO_PACKET. | |
5545 | * this creates a new 'base' pointer, off_reg (variable) gets | |
5546 | * added into the variable offset, and we copy the fixed offset | |
5547 | * from ptr_reg. | |
969bf05e | 5548 | */ |
b03c9f9f EC |
5549 | if (signed_add_overflows(smin_ptr, smin_val) || |
5550 | signed_add_overflows(smax_ptr, smax_val)) { | |
5551 | dst_reg->smin_value = S64_MIN; | |
5552 | dst_reg->smax_value = S64_MAX; | |
5553 | } else { | |
5554 | dst_reg->smin_value = smin_ptr + smin_val; | |
5555 | dst_reg->smax_value = smax_ptr + smax_val; | |
5556 | } | |
5557 | if (umin_ptr + umin_val < umin_ptr || | |
5558 | umax_ptr + umax_val < umax_ptr) { | |
5559 | dst_reg->umin_value = 0; | |
5560 | dst_reg->umax_value = U64_MAX; | |
5561 | } else { | |
5562 | dst_reg->umin_value = umin_ptr + umin_val; | |
5563 | dst_reg->umax_value = umax_ptr + umax_val; | |
5564 | } | |
f1174f77 EC |
5565 | dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off); |
5566 | dst_reg->off = ptr_reg->off; | |
0962590e | 5567 | dst_reg->raw = ptr_reg->raw; |
de8f3a83 | 5568 | if (reg_is_pkt_pointer(ptr_reg)) { |
f1174f77 EC |
5569 | dst_reg->id = ++env->id_gen; |
5570 | /* something was added to pkt_ptr, set range to zero */ | |
0962590e | 5571 | dst_reg->raw = 0; |
f1174f77 EC |
5572 | } |
5573 | break; | |
5574 | case BPF_SUB: | |
979d63d5 DB |
5575 | ret = sanitize_ptr_alu(env, insn, ptr_reg, dst_reg, smin_val < 0); |
5576 | if (ret < 0) { | |
5577 | verbose(env, "R%d tried to sub from different maps or paths\n", dst); | |
5578 | return ret; | |
5579 | } | |
f1174f77 EC |
5580 | if (dst_reg == off_reg) { |
5581 | /* scalar -= pointer. Creates an unknown scalar */ | |
82abbf8d AS |
5582 | verbose(env, "R%d tried to subtract pointer from scalar\n", |
5583 | dst); | |
f1174f77 EC |
5584 | return -EACCES; |
5585 | } | |
5586 | /* We don't allow subtraction from FP, because (according to | |
5587 | * test_verifier.c test "invalid fp arithmetic", JITs might not | |
5588 | * be able to deal with it. | |
969bf05e | 5589 | */ |
f1174f77 | 5590 | if (ptr_reg->type == PTR_TO_STACK) { |
82abbf8d AS |
5591 | verbose(env, "R%d subtraction from stack pointer prohibited\n", |
5592 | dst); | |
f1174f77 EC |
5593 | return -EACCES; |
5594 | } | |
b03c9f9f EC |
5595 | if (known && (ptr_reg->off - smin_val == |
5596 | (s64)(s32)(ptr_reg->off - smin_val))) { | |
f1174f77 | 5597 | /* pointer -= K. Subtract it from fixed offset */ |
b03c9f9f EC |
5598 | dst_reg->smin_value = smin_ptr; |
5599 | dst_reg->smax_value = smax_ptr; | |
5600 | dst_reg->umin_value = umin_ptr; | |
5601 | dst_reg->umax_value = umax_ptr; | |
f1174f77 EC |
5602 | dst_reg->var_off = ptr_reg->var_off; |
5603 | dst_reg->id = ptr_reg->id; | |
b03c9f9f | 5604 | dst_reg->off = ptr_reg->off - smin_val; |
0962590e | 5605 | dst_reg->raw = ptr_reg->raw; |
f1174f77 EC |
5606 | break; |
5607 | } | |
f1174f77 EC |
5608 | /* A new variable offset is created. If the subtrahend is known |
5609 | * nonnegative, then any reg->range we had before is still good. | |
969bf05e | 5610 | */ |
b03c9f9f EC |
5611 | if (signed_sub_overflows(smin_ptr, smax_val) || |
5612 | signed_sub_overflows(smax_ptr, smin_val)) { | |
5613 | /* Overflow possible, we know nothing */ | |
5614 | dst_reg->smin_value = S64_MIN; | |
5615 | dst_reg->smax_value = S64_MAX; | |
5616 | } else { | |
5617 | dst_reg->smin_value = smin_ptr - smax_val; | |
5618 | dst_reg->smax_value = smax_ptr - smin_val; | |
5619 | } | |
5620 | if (umin_ptr < umax_val) { | |
5621 | /* Overflow possible, we know nothing */ | |
5622 | dst_reg->umin_value = 0; | |
5623 | dst_reg->umax_value = U64_MAX; | |
5624 | } else { | |
5625 | /* Cannot overflow (as long as bounds are consistent) */ | |
5626 | dst_reg->umin_value = umin_ptr - umax_val; | |
5627 | dst_reg->umax_value = umax_ptr - umin_val; | |
5628 | } | |
f1174f77 EC |
5629 | dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off); |
5630 | dst_reg->off = ptr_reg->off; | |
0962590e | 5631 | dst_reg->raw = ptr_reg->raw; |
de8f3a83 | 5632 | if (reg_is_pkt_pointer(ptr_reg)) { |
f1174f77 EC |
5633 | dst_reg->id = ++env->id_gen; |
5634 | /* something was added to pkt_ptr, set range to zero */ | |
b03c9f9f | 5635 | if (smin_val < 0) |
0962590e | 5636 | dst_reg->raw = 0; |
43188702 | 5637 | } |
f1174f77 EC |
5638 | break; |
5639 | case BPF_AND: | |
5640 | case BPF_OR: | |
5641 | case BPF_XOR: | |
82abbf8d AS |
5642 | /* bitwise ops on pointers are troublesome, prohibit. */ |
5643 | verbose(env, "R%d bitwise operator %s on pointer prohibited\n", | |
5644 | dst, bpf_alu_string[opcode >> 4]); | |
f1174f77 EC |
5645 | return -EACCES; |
5646 | default: | |
5647 | /* other operators (e.g. MUL,LSH) produce non-pointer results */ | |
82abbf8d AS |
5648 | verbose(env, "R%d pointer arithmetic with %s operator prohibited\n", |
5649 | dst, bpf_alu_string[opcode >> 4]); | |
f1174f77 | 5650 | return -EACCES; |
43188702 JF |
5651 | } |
5652 | ||
bb7f0f98 AS |
5653 | if (!check_reg_sane_offset(env, dst_reg, ptr_reg->type)) |
5654 | return -EINVAL; | |
5655 | ||
b03c9f9f EC |
5656 | __update_reg_bounds(dst_reg); |
5657 | __reg_deduce_bounds(dst_reg); | |
5658 | __reg_bound_offset(dst_reg); | |
0d6303db DB |
5659 | |
5660 | /* For unprivileged we require that resulting offset must be in bounds | |
5661 | * in order to be able to sanitize access later on. | |
5662 | */ | |
2c78ee89 | 5663 | if (!env->bypass_spec_v1) { |
e4298d25 DB |
5664 | if (dst_reg->type == PTR_TO_MAP_VALUE && |
5665 | check_map_access(env, dst, dst_reg->off, 1, false)) { | |
5666 | verbose(env, "R%d pointer arithmetic of map value goes out of range, " | |
5667 | "prohibited for !root\n", dst); | |
5668 | return -EACCES; | |
5669 | } else if (dst_reg->type == PTR_TO_STACK && | |
5670 | check_stack_access(env, dst_reg, dst_reg->off + | |
5671 | dst_reg->var_off.value, 1)) { | |
5672 | verbose(env, "R%d stack pointer arithmetic goes out of range, " | |
5673 | "prohibited for !root\n", dst); | |
5674 | return -EACCES; | |
5675 | } | |
0d6303db DB |
5676 | } |
5677 | ||
43188702 JF |
5678 | return 0; |
5679 | } | |
5680 | ||
3f50f132 JF |
5681 | static void scalar32_min_max_add(struct bpf_reg_state *dst_reg, |
5682 | struct bpf_reg_state *src_reg) | |
5683 | { | |
5684 | s32 smin_val = src_reg->s32_min_value; | |
5685 | s32 smax_val = src_reg->s32_max_value; | |
5686 | u32 umin_val = src_reg->u32_min_value; | |
5687 | u32 umax_val = src_reg->u32_max_value; | |
5688 | ||
5689 | if (signed_add32_overflows(dst_reg->s32_min_value, smin_val) || | |
5690 | signed_add32_overflows(dst_reg->s32_max_value, smax_val)) { | |
5691 | dst_reg->s32_min_value = S32_MIN; | |
5692 | dst_reg->s32_max_value = S32_MAX; | |
5693 | } else { | |
5694 | dst_reg->s32_min_value += smin_val; | |
5695 | dst_reg->s32_max_value += smax_val; | |
5696 | } | |
5697 | if (dst_reg->u32_min_value + umin_val < umin_val || | |
5698 | dst_reg->u32_max_value + umax_val < umax_val) { | |
5699 | dst_reg->u32_min_value = 0; | |
5700 | dst_reg->u32_max_value = U32_MAX; | |
5701 | } else { | |
5702 | dst_reg->u32_min_value += umin_val; | |
5703 | dst_reg->u32_max_value += umax_val; | |
5704 | } | |
5705 | } | |
5706 | ||
07cd2631 JF |
5707 | static void scalar_min_max_add(struct bpf_reg_state *dst_reg, |
5708 | struct bpf_reg_state *src_reg) | |
5709 | { | |
5710 | s64 smin_val = src_reg->smin_value; | |
5711 | s64 smax_val = src_reg->smax_value; | |
5712 | u64 umin_val = src_reg->umin_value; | |
5713 | u64 umax_val = src_reg->umax_value; | |
5714 | ||
5715 | if (signed_add_overflows(dst_reg->smin_value, smin_val) || | |
5716 | signed_add_overflows(dst_reg->smax_value, smax_val)) { | |
5717 | dst_reg->smin_value = S64_MIN; | |
5718 | dst_reg->smax_value = S64_MAX; | |
5719 | } else { | |
5720 | dst_reg->smin_value += smin_val; | |
5721 | dst_reg->smax_value += smax_val; | |
5722 | } | |
5723 | if (dst_reg->umin_value + umin_val < umin_val || | |
5724 | dst_reg->umax_value + umax_val < umax_val) { | |
5725 | dst_reg->umin_value = 0; | |
5726 | dst_reg->umax_value = U64_MAX; | |
5727 | } else { | |
5728 | dst_reg->umin_value += umin_val; | |
5729 | dst_reg->umax_value += umax_val; | |
5730 | } | |
3f50f132 JF |
5731 | } |
5732 | ||
5733 | static void scalar32_min_max_sub(struct bpf_reg_state *dst_reg, | |
5734 | struct bpf_reg_state *src_reg) | |
5735 | { | |
5736 | s32 smin_val = src_reg->s32_min_value; | |
5737 | s32 smax_val = src_reg->s32_max_value; | |
5738 | u32 umin_val = src_reg->u32_min_value; | |
5739 | u32 umax_val = src_reg->u32_max_value; | |
5740 | ||
5741 | if (signed_sub32_overflows(dst_reg->s32_min_value, smax_val) || | |
5742 | signed_sub32_overflows(dst_reg->s32_max_value, smin_val)) { | |
5743 | /* Overflow possible, we know nothing */ | |
5744 | dst_reg->s32_min_value = S32_MIN; | |
5745 | dst_reg->s32_max_value = S32_MAX; | |
5746 | } else { | |
5747 | dst_reg->s32_min_value -= smax_val; | |
5748 | dst_reg->s32_max_value -= smin_val; | |
5749 | } | |
5750 | if (dst_reg->u32_min_value < umax_val) { | |
5751 | /* Overflow possible, we know nothing */ | |
5752 | dst_reg->u32_min_value = 0; | |
5753 | dst_reg->u32_max_value = U32_MAX; | |
5754 | } else { | |
5755 | /* Cannot overflow (as long as bounds are consistent) */ | |
5756 | dst_reg->u32_min_value -= umax_val; | |
5757 | dst_reg->u32_max_value -= umin_val; | |
5758 | } | |
07cd2631 JF |
5759 | } |
5760 | ||
5761 | static void scalar_min_max_sub(struct bpf_reg_state *dst_reg, | |
5762 | struct bpf_reg_state *src_reg) | |
5763 | { | |
5764 | s64 smin_val = src_reg->smin_value; | |
5765 | s64 smax_val = src_reg->smax_value; | |
5766 | u64 umin_val = src_reg->umin_value; | |
5767 | u64 umax_val = src_reg->umax_value; | |
5768 | ||
5769 | if (signed_sub_overflows(dst_reg->smin_value, smax_val) || | |
5770 | signed_sub_overflows(dst_reg->smax_value, smin_val)) { | |
5771 | /* Overflow possible, we know nothing */ | |
5772 | dst_reg->smin_value = S64_MIN; | |
5773 | dst_reg->smax_value = S64_MAX; | |
5774 | } else { | |
5775 | dst_reg->smin_value -= smax_val; | |
5776 | dst_reg->smax_value -= smin_val; | |
5777 | } | |
5778 | if (dst_reg->umin_value < umax_val) { | |
5779 | /* Overflow possible, we know nothing */ | |
5780 | dst_reg->umin_value = 0; | |
5781 | dst_reg->umax_value = U64_MAX; | |
5782 | } else { | |
5783 | /* Cannot overflow (as long as bounds are consistent) */ | |
5784 | dst_reg->umin_value -= umax_val; | |
5785 | dst_reg->umax_value -= umin_val; | |
5786 | } | |
3f50f132 JF |
5787 | } |
5788 | ||
5789 | static void scalar32_min_max_mul(struct bpf_reg_state *dst_reg, | |
5790 | struct bpf_reg_state *src_reg) | |
5791 | { | |
5792 | s32 smin_val = src_reg->s32_min_value; | |
5793 | u32 umin_val = src_reg->u32_min_value; | |
5794 | u32 umax_val = src_reg->u32_max_value; | |
5795 | ||
5796 | if (smin_val < 0 || dst_reg->s32_min_value < 0) { | |
5797 | /* Ain't nobody got time to multiply that sign */ | |
5798 | __mark_reg32_unbounded(dst_reg); | |
5799 | return; | |
5800 | } | |
5801 | /* Both values are positive, so we can work with unsigned and | |
5802 | * copy the result to signed (unless it exceeds S32_MAX). | |
5803 | */ | |
5804 | if (umax_val > U16_MAX || dst_reg->u32_max_value > U16_MAX) { | |
5805 | /* Potential overflow, we know nothing */ | |
5806 | __mark_reg32_unbounded(dst_reg); | |
5807 | return; | |
5808 | } | |
5809 | dst_reg->u32_min_value *= umin_val; | |
5810 | dst_reg->u32_max_value *= umax_val; | |
5811 | if (dst_reg->u32_max_value > S32_MAX) { | |
5812 | /* Overflow possible, we know nothing */ | |
5813 | dst_reg->s32_min_value = S32_MIN; | |
5814 | dst_reg->s32_max_value = S32_MAX; | |
5815 | } else { | |
5816 | dst_reg->s32_min_value = dst_reg->u32_min_value; | |
5817 | dst_reg->s32_max_value = dst_reg->u32_max_value; | |
5818 | } | |
07cd2631 JF |
5819 | } |
5820 | ||
5821 | static void scalar_min_max_mul(struct bpf_reg_state *dst_reg, | |
5822 | struct bpf_reg_state *src_reg) | |
5823 | { | |
5824 | s64 smin_val = src_reg->smin_value; | |
5825 | u64 umin_val = src_reg->umin_value; | |
5826 | u64 umax_val = src_reg->umax_value; | |
5827 | ||
07cd2631 JF |
5828 | if (smin_val < 0 || dst_reg->smin_value < 0) { |
5829 | /* Ain't nobody got time to multiply that sign */ | |
3f50f132 | 5830 | __mark_reg64_unbounded(dst_reg); |
07cd2631 JF |
5831 | return; |
5832 | } | |
5833 | /* Both values are positive, so we can work with unsigned and | |
5834 | * copy the result to signed (unless it exceeds S64_MAX). | |
5835 | */ | |
5836 | if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) { | |
5837 | /* Potential overflow, we know nothing */ | |
3f50f132 | 5838 | __mark_reg64_unbounded(dst_reg); |
07cd2631 JF |
5839 | return; |
5840 | } | |
5841 | dst_reg->umin_value *= umin_val; | |
5842 | dst_reg->umax_value *= umax_val; | |
5843 | if (dst_reg->umax_value > S64_MAX) { | |
5844 | /* Overflow possible, we know nothing */ | |
5845 | dst_reg->smin_value = S64_MIN; | |
5846 | dst_reg->smax_value = S64_MAX; | |
5847 | } else { | |
5848 | dst_reg->smin_value = dst_reg->umin_value; | |
5849 | dst_reg->smax_value = dst_reg->umax_value; | |
5850 | } | |
5851 | } | |
5852 | ||
3f50f132 JF |
5853 | static void scalar32_min_max_and(struct bpf_reg_state *dst_reg, |
5854 | struct bpf_reg_state *src_reg) | |
5855 | { | |
5856 | bool src_known = tnum_subreg_is_const(src_reg->var_off); | |
5857 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); | |
5858 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); | |
5859 | s32 smin_val = src_reg->s32_min_value; | |
5860 | u32 umax_val = src_reg->u32_max_value; | |
5861 | ||
5862 | /* Assuming scalar64_min_max_and will be called so its safe | |
5863 | * to skip updating register for known 32-bit case. | |
5864 | */ | |
5865 | if (src_known && dst_known) | |
5866 | return; | |
5867 | ||
5868 | /* We get our minimum from the var_off, since that's inherently | |
5869 | * bitwise. Our maximum is the minimum of the operands' maxima. | |
5870 | */ | |
5871 | dst_reg->u32_min_value = var32_off.value; | |
5872 | dst_reg->u32_max_value = min(dst_reg->u32_max_value, umax_val); | |
5873 | if (dst_reg->s32_min_value < 0 || smin_val < 0) { | |
5874 | /* Lose signed bounds when ANDing negative numbers, | |
5875 | * ain't nobody got time for that. | |
5876 | */ | |
5877 | dst_reg->s32_min_value = S32_MIN; | |
5878 | dst_reg->s32_max_value = S32_MAX; | |
5879 | } else { | |
5880 | /* ANDing two positives gives a positive, so safe to | |
5881 | * cast result into s64. | |
5882 | */ | |
5883 | dst_reg->s32_min_value = dst_reg->u32_min_value; | |
5884 | dst_reg->s32_max_value = dst_reg->u32_max_value; | |
5885 | } | |
5886 | ||
5887 | } | |
5888 | ||
07cd2631 JF |
5889 | static void scalar_min_max_and(struct bpf_reg_state *dst_reg, |
5890 | struct bpf_reg_state *src_reg) | |
5891 | { | |
3f50f132 JF |
5892 | bool src_known = tnum_is_const(src_reg->var_off); |
5893 | bool dst_known = tnum_is_const(dst_reg->var_off); | |
07cd2631 JF |
5894 | s64 smin_val = src_reg->smin_value; |
5895 | u64 umax_val = src_reg->umax_value; | |
5896 | ||
3f50f132 | 5897 | if (src_known && dst_known) { |
4fbb38a3 | 5898 | __mark_reg_known(dst_reg, dst_reg->var_off.value); |
3f50f132 JF |
5899 | return; |
5900 | } | |
5901 | ||
07cd2631 JF |
5902 | /* We get our minimum from the var_off, since that's inherently |
5903 | * bitwise. Our maximum is the minimum of the operands' maxima. | |
5904 | */ | |
07cd2631 JF |
5905 | dst_reg->umin_value = dst_reg->var_off.value; |
5906 | dst_reg->umax_value = min(dst_reg->umax_value, umax_val); | |
5907 | if (dst_reg->smin_value < 0 || smin_val < 0) { | |
5908 | /* Lose signed bounds when ANDing negative numbers, | |
5909 | * ain't nobody got time for that. | |
5910 | */ | |
5911 | dst_reg->smin_value = S64_MIN; | |
5912 | dst_reg->smax_value = S64_MAX; | |
5913 | } else { | |
5914 | /* ANDing two positives gives a positive, so safe to | |
5915 | * cast result into s64. | |
5916 | */ | |
5917 | dst_reg->smin_value = dst_reg->umin_value; | |
5918 | dst_reg->smax_value = dst_reg->umax_value; | |
5919 | } | |
5920 | /* We may learn something more from the var_off */ | |
5921 | __update_reg_bounds(dst_reg); | |
5922 | } | |
5923 | ||
3f50f132 JF |
5924 | static void scalar32_min_max_or(struct bpf_reg_state *dst_reg, |
5925 | struct bpf_reg_state *src_reg) | |
5926 | { | |
5927 | bool src_known = tnum_subreg_is_const(src_reg->var_off); | |
5928 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); | |
5929 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); | |
5930 | s32 smin_val = src_reg->smin_value; | |
5931 | u32 umin_val = src_reg->umin_value; | |
5932 | ||
5933 | /* Assuming scalar64_min_max_or will be called so it is safe | |
5934 | * to skip updating register for known case. | |
5935 | */ | |
5936 | if (src_known && dst_known) | |
5937 | return; | |
5938 | ||
5939 | /* We get our maximum from the var_off, and our minimum is the | |
5940 | * maximum of the operands' minima | |
5941 | */ | |
5942 | dst_reg->u32_min_value = max(dst_reg->u32_min_value, umin_val); | |
5943 | dst_reg->u32_max_value = var32_off.value | var32_off.mask; | |
5944 | if (dst_reg->s32_min_value < 0 || smin_val < 0) { | |
5945 | /* Lose signed bounds when ORing negative numbers, | |
5946 | * ain't nobody got time for that. | |
5947 | */ | |
5948 | dst_reg->s32_min_value = S32_MIN; | |
5949 | dst_reg->s32_max_value = S32_MAX; | |
5950 | } else { | |
5951 | /* ORing two positives gives a positive, so safe to | |
5952 | * cast result into s64. | |
5953 | */ | |
5954 | dst_reg->s32_min_value = dst_reg->umin_value; | |
5955 | dst_reg->s32_max_value = dst_reg->umax_value; | |
5956 | } | |
5957 | } | |
5958 | ||
07cd2631 JF |
5959 | static void scalar_min_max_or(struct bpf_reg_state *dst_reg, |
5960 | struct bpf_reg_state *src_reg) | |
5961 | { | |
3f50f132 JF |
5962 | bool src_known = tnum_is_const(src_reg->var_off); |
5963 | bool dst_known = tnum_is_const(dst_reg->var_off); | |
07cd2631 JF |
5964 | s64 smin_val = src_reg->smin_value; |
5965 | u64 umin_val = src_reg->umin_value; | |
5966 | ||
3f50f132 | 5967 | if (src_known && dst_known) { |
4fbb38a3 | 5968 | __mark_reg_known(dst_reg, dst_reg->var_off.value); |
3f50f132 JF |
5969 | return; |
5970 | } | |
5971 | ||
07cd2631 JF |
5972 | /* We get our maximum from the var_off, and our minimum is the |
5973 | * maximum of the operands' minima | |
5974 | */ | |
07cd2631 JF |
5975 | dst_reg->umin_value = max(dst_reg->umin_value, umin_val); |
5976 | dst_reg->umax_value = dst_reg->var_off.value | dst_reg->var_off.mask; | |
5977 | if (dst_reg->smin_value < 0 || smin_val < 0) { | |
5978 | /* Lose signed bounds when ORing negative numbers, | |
5979 | * ain't nobody got time for that. | |
5980 | */ | |
5981 | dst_reg->smin_value = S64_MIN; | |
5982 | dst_reg->smax_value = S64_MAX; | |
5983 | } else { | |
5984 | /* ORing two positives gives a positive, so safe to | |
5985 | * cast result into s64. | |
5986 | */ | |
5987 | dst_reg->smin_value = dst_reg->umin_value; | |
5988 | dst_reg->smax_value = dst_reg->umax_value; | |
5989 | } | |
5990 | /* We may learn something more from the var_off */ | |
5991 | __update_reg_bounds(dst_reg); | |
5992 | } | |
5993 | ||
2921c90d YS |
5994 | static void scalar32_min_max_xor(struct bpf_reg_state *dst_reg, |
5995 | struct bpf_reg_state *src_reg) | |
5996 | { | |
5997 | bool src_known = tnum_subreg_is_const(src_reg->var_off); | |
5998 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); | |
5999 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); | |
6000 | s32 smin_val = src_reg->s32_min_value; | |
6001 | ||
6002 | /* Assuming scalar64_min_max_xor will be called so it is safe | |
6003 | * to skip updating register for known case. | |
6004 | */ | |
6005 | if (src_known && dst_known) | |
6006 | return; | |
6007 | ||
6008 | /* We get both minimum and maximum from the var32_off. */ | |
6009 | dst_reg->u32_min_value = var32_off.value; | |
6010 | dst_reg->u32_max_value = var32_off.value | var32_off.mask; | |
6011 | ||
6012 | if (dst_reg->s32_min_value >= 0 && smin_val >= 0) { | |
6013 | /* XORing two positive sign numbers gives a positive, | |
6014 | * so safe to cast u32 result into s32. | |
6015 | */ | |
6016 | dst_reg->s32_min_value = dst_reg->u32_min_value; | |
6017 | dst_reg->s32_max_value = dst_reg->u32_max_value; | |
6018 | } else { | |
6019 | dst_reg->s32_min_value = S32_MIN; | |
6020 | dst_reg->s32_max_value = S32_MAX; | |
6021 | } | |
6022 | } | |
6023 | ||
6024 | static void scalar_min_max_xor(struct bpf_reg_state *dst_reg, | |
6025 | struct bpf_reg_state *src_reg) | |
6026 | { | |
6027 | bool src_known = tnum_is_const(src_reg->var_off); | |
6028 | bool dst_known = tnum_is_const(dst_reg->var_off); | |
6029 | s64 smin_val = src_reg->smin_value; | |
6030 | ||
6031 | if (src_known && dst_known) { | |
6032 | /* dst_reg->var_off.value has been updated earlier */ | |
6033 | __mark_reg_known(dst_reg, dst_reg->var_off.value); | |
6034 | return; | |
6035 | } | |
6036 | ||
6037 | /* We get both minimum and maximum from the var_off. */ | |
6038 | dst_reg->umin_value = dst_reg->var_off.value; | |
6039 | dst_reg->umax_value = dst_reg->var_off.value | dst_reg->var_off.mask; | |
6040 | ||
6041 | if (dst_reg->smin_value >= 0 && smin_val >= 0) { | |
6042 | /* XORing two positive sign numbers gives a positive, | |
6043 | * so safe to cast u64 result into s64. | |
6044 | */ | |
6045 | dst_reg->smin_value = dst_reg->umin_value; | |
6046 | dst_reg->smax_value = dst_reg->umax_value; | |
6047 | } else { | |
6048 | dst_reg->smin_value = S64_MIN; | |
6049 | dst_reg->smax_value = S64_MAX; | |
6050 | } | |
6051 | ||
6052 | __update_reg_bounds(dst_reg); | |
6053 | } | |
6054 | ||
3f50f132 JF |
6055 | static void __scalar32_min_max_lsh(struct bpf_reg_state *dst_reg, |
6056 | u64 umin_val, u64 umax_val) | |
07cd2631 | 6057 | { |
07cd2631 JF |
6058 | /* We lose all sign bit information (except what we can pick |
6059 | * up from var_off) | |
6060 | */ | |
3f50f132 JF |
6061 | dst_reg->s32_min_value = S32_MIN; |
6062 | dst_reg->s32_max_value = S32_MAX; | |
6063 | /* If we might shift our top bit out, then we know nothing */ | |
6064 | if (umax_val > 31 || dst_reg->u32_max_value > 1ULL << (31 - umax_val)) { | |
6065 | dst_reg->u32_min_value = 0; | |
6066 | dst_reg->u32_max_value = U32_MAX; | |
6067 | } else { | |
6068 | dst_reg->u32_min_value <<= umin_val; | |
6069 | dst_reg->u32_max_value <<= umax_val; | |
6070 | } | |
6071 | } | |
6072 | ||
6073 | static void scalar32_min_max_lsh(struct bpf_reg_state *dst_reg, | |
6074 | struct bpf_reg_state *src_reg) | |
6075 | { | |
6076 | u32 umax_val = src_reg->u32_max_value; | |
6077 | u32 umin_val = src_reg->u32_min_value; | |
6078 | /* u32 alu operation will zext upper bits */ | |
6079 | struct tnum subreg = tnum_subreg(dst_reg->var_off); | |
6080 | ||
6081 | __scalar32_min_max_lsh(dst_reg, umin_val, umax_val); | |
6082 | dst_reg->var_off = tnum_subreg(tnum_lshift(subreg, umin_val)); | |
6083 | /* Not required but being careful mark reg64 bounds as unknown so | |
6084 | * that we are forced to pick them up from tnum and zext later and | |
6085 | * if some path skips this step we are still safe. | |
6086 | */ | |
6087 | __mark_reg64_unbounded(dst_reg); | |
6088 | __update_reg32_bounds(dst_reg); | |
6089 | } | |
6090 | ||
6091 | static void __scalar64_min_max_lsh(struct bpf_reg_state *dst_reg, | |
6092 | u64 umin_val, u64 umax_val) | |
6093 | { | |
6094 | /* Special case <<32 because it is a common compiler pattern to sign | |
6095 | * extend subreg by doing <<32 s>>32. In this case if 32bit bounds are | |
6096 | * positive we know this shift will also be positive so we can track | |
6097 | * bounds correctly. Otherwise we lose all sign bit information except | |
6098 | * what we can pick up from var_off. Perhaps we can generalize this | |
6099 | * later to shifts of any length. | |
6100 | */ | |
6101 | if (umin_val == 32 && umax_val == 32 && dst_reg->s32_max_value >= 0) | |
6102 | dst_reg->smax_value = (s64)dst_reg->s32_max_value << 32; | |
6103 | else | |
6104 | dst_reg->smax_value = S64_MAX; | |
6105 | ||
6106 | if (umin_val == 32 && umax_val == 32 && dst_reg->s32_min_value >= 0) | |
6107 | dst_reg->smin_value = (s64)dst_reg->s32_min_value << 32; | |
6108 | else | |
6109 | dst_reg->smin_value = S64_MIN; | |
6110 | ||
07cd2631 JF |
6111 | /* If we might shift our top bit out, then we know nothing */ |
6112 | if (dst_reg->umax_value > 1ULL << (63 - umax_val)) { | |
6113 | dst_reg->umin_value = 0; | |
6114 | dst_reg->umax_value = U64_MAX; | |
6115 | } else { | |
6116 | dst_reg->umin_value <<= umin_val; | |
6117 | dst_reg->umax_value <<= umax_val; | |
6118 | } | |
3f50f132 JF |
6119 | } |
6120 | ||
6121 | static void scalar_min_max_lsh(struct bpf_reg_state *dst_reg, | |
6122 | struct bpf_reg_state *src_reg) | |
6123 | { | |
6124 | u64 umax_val = src_reg->umax_value; | |
6125 | u64 umin_val = src_reg->umin_value; | |
6126 | ||
6127 | /* scalar64 calc uses 32bit unshifted bounds so must be called first */ | |
6128 | __scalar64_min_max_lsh(dst_reg, umin_val, umax_val); | |
6129 | __scalar32_min_max_lsh(dst_reg, umin_val, umax_val); | |
6130 | ||
07cd2631 JF |
6131 | dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val); |
6132 | /* We may learn something more from the var_off */ | |
6133 | __update_reg_bounds(dst_reg); | |
6134 | } | |
6135 | ||
3f50f132 JF |
6136 | static void scalar32_min_max_rsh(struct bpf_reg_state *dst_reg, |
6137 | struct bpf_reg_state *src_reg) | |
6138 | { | |
6139 | struct tnum subreg = tnum_subreg(dst_reg->var_off); | |
6140 | u32 umax_val = src_reg->u32_max_value; | |
6141 | u32 umin_val = src_reg->u32_min_value; | |
6142 | ||
6143 | /* BPF_RSH is an unsigned shift. If the value in dst_reg might | |
6144 | * be negative, then either: | |
6145 | * 1) src_reg might be zero, so the sign bit of the result is | |
6146 | * unknown, so we lose our signed bounds | |
6147 | * 2) it's known negative, thus the unsigned bounds capture the | |
6148 | * signed bounds | |
6149 | * 3) the signed bounds cross zero, so they tell us nothing | |
6150 | * about the result | |
6151 | * If the value in dst_reg is known nonnegative, then again the | |
6152 | * unsigned bounts capture the signed bounds. | |
6153 | * Thus, in all cases it suffices to blow away our signed bounds | |
6154 | * and rely on inferring new ones from the unsigned bounds and | |
6155 | * var_off of the result. | |
6156 | */ | |
6157 | dst_reg->s32_min_value = S32_MIN; | |
6158 | dst_reg->s32_max_value = S32_MAX; | |
6159 | ||
6160 | dst_reg->var_off = tnum_rshift(subreg, umin_val); | |
6161 | dst_reg->u32_min_value >>= umax_val; | |
6162 | dst_reg->u32_max_value >>= umin_val; | |
6163 | ||
6164 | __mark_reg64_unbounded(dst_reg); | |
6165 | __update_reg32_bounds(dst_reg); | |
6166 | } | |
6167 | ||
07cd2631 JF |
6168 | static void scalar_min_max_rsh(struct bpf_reg_state *dst_reg, |
6169 | struct bpf_reg_state *src_reg) | |
6170 | { | |
6171 | u64 umax_val = src_reg->umax_value; | |
6172 | u64 umin_val = src_reg->umin_value; | |
6173 | ||
6174 | /* BPF_RSH is an unsigned shift. If the value in dst_reg might | |
6175 | * be negative, then either: | |
6176 | * 1) src_reg might be zero, so the sign bit of the result is | |
6177 | * unknown, so we lose our signed bounds | |
6178 | * 2) it's known negative, thus the unsigned bounds capture the | |
6179 | * signed bounds | |
6180 | * 3) the signed bounds cross zero, so they tell us nothing | |
6181 | * about the result | |
6182 | * If the value in dst_reg is known nonnegative, then again the | |
6183 | * unsigned bounts capture the signed bounds. | |
6184 | * Thus, in all cases it suffices to blow away our signed bounds | |
6185 | * and rely on inferring new ones from the unsigned bounds and | |
6186 | * var_off of the result. | |
6187 | */ | |
6188 | dst_reg->smin_value = S64_MIN; | |
6189 | dst_reg->smax_value = S64_MAX; | |
6190 | dst_reg->var_off = tnum_rshift(dst_reg->var_off, umin_val); | |
6191 | dst_reg->umin_value >>= umax_val; | |
6192 | dst_reg->umax_value >>= umin_val; | |
3f50f132 JF |
6193 | |
6194 | /* Its not easy to operate on alu32 bounds here because it depends | |
6195 | * on bits being shifted in. Take easy way out and mark unbounded | |
6196 | * so we can recalculate later from tnum. | |
6197 | */ | |
6198 | __mark_reg32_unbounded(dst_reg); | |
07cd2631 JF |
6199 | __update_reg_bounds(dst_reg); |
6200 | } | |
6201 | ||
3f50f132 JF |
6202 | static void scalar32_min_max_arsh(struct bpf_reg_state *dst_reg, |
6203 | struct bpf_reg_state *src_reg) | |
07cd2631 | 6204 | { |
3f50f132 | 6205 | u64 umin_val = src_reg->u32_min_value; |
07cd2631 JF |
6206 | |
6207 | /* Upon reaching here, src_known is true and | |
6208 | * umax_val is equal to umin_val. | |
6209 | */ | |
3f50f132 JF |
6210 | dst_reg->s32_min_value = (u32)(((s32)dst_reg->s32_min_value) >> umin_val); |
6211 | dst_reg->s32_max_value = (u32)(((s32)dst_reg->s32_max_value) >> umin_val); | |
07cd2631 | 6212 | |
3f50f132 JF |
6213 | dst_reg->var_off = tnum_arshift(tnum_subreg(dst_reg->var_off), umin_val, 32); |
6214 | ||
6215 | /* blow away the dst_reg umin_value/umax_value and rely on | |
6216 | * dst_reg var_off to refine the result. | |
6217 | */ | |
6218 | dst_reg->u32_min_value = 0; | |
6219 | dst_reg->u32_max_value = U32_MAX; | |
6220 | ||
6221 | __mark_reg64_unbounded(dst_reg); | |
6222 | __update_reg32_bounds(dst_reg); | |
6223 | } | |
6224 | ||
6225 | static void scalar_min_max_arsh(struct bpf_reg_state *dst_reg, | |
6226 | struct bpf_reg_state *src_reg) | |
6227 | { | |
6228 | u64 umin_val = src_reg->umin_value; | |
6229 | ||
6230 | /* Upon reaching here, src_known is true and umax_val is equal | |
6231 | * to umin_val. | |
6232 | */ | |
6233 | dst_reg->smin_value >>= umin_val; | |
6234 | dst_reg->smax_value >>= umin_val; | |
6235 | ||
6236 | dst_reg->var_off = tnum_arshift(dst_reg->var_off, umin_val, 64); | |
07cd2631 JF |
6237 | |
6238 | /* blow away the dst_reg umin_value/umax_value and rely on | |
6239 | * dst_reg var_off to refine the result. | |
6240 | */ | |
6241 | dst_reg->umin_value = 0; | |
6242 | dst_reg->umax_value = U64_MAX; | |
3f50f132 JF |
6243 | |
6244 | /* Its not easy to operate on alu32 bounds here because it depends | |
6245 | * on bits being shifted in from upper 32-bits. Take easy way out | |
6246 | * and mark unbounded so we can recalculate later from tnum. | |
6247 | */ | |
6248 | __mark_reg32_unbounded(dst_reg); | |
07cd2631 JF |
6249 | __update_reg_bounds(dst_reg); |
6250 | } | |
6251 | ||
468f6eaf JH |
6252 | /* WARNING: This function does calculations on 64-bit values, but the actual |
6253 | * execution may occur on 32-bit values. Therefore, things like bitshifts | |
6254 | * need extra checks in the 32-bit case. | |
6255 | */ | |
f1174f77 EC |
6256 | static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env, |
6257 | struct bpf_insn *insn, | |
6258 | struct bpf_reg_state *dst_reg, | |
6259 | struct bpf_reg_state src_reg) | |
969bf05e | 6260 | { |
638f5b90 | 6261 | struct bpf_reg_state *regs = cur_regs(env); |
48461135 | 6262 | u8 opcode = BPF_OP(insn->code); |
b0b3fb67 | 6263 | bool src_known; |
b03c9f9f EC |
6264 | s64 smin_val, smax_val; |
6265 | u64 umin_val, umax_val; | |
3f50f132 JF |
6266 | s32 s32_min_val, s32_max_val; |
6267 | u32 u32_min_val, u32_max_val; | |
468f6eaf | 6268 | u64 insn_bitness = (BPF_CLASS(insn->code) == BPF_ALU64) ? 64 : 32; |
d3bd7413 DB |
6269 | u32 dst = insn->dst_reg; |
6270 | int ret; | |
3f50f132 | 6271 | bool alu32 = (BPF_CLASS(insn->code) != BPF_ALU64); |
b799207e | 6272 | |
b03c9f9f EC |
6273 | smin_val = src_reg.smin_value; |
6274 | smax_val = src_reg.smax_value; | |
6275 | umin_val = src_reg.umin_value; | |
6276 | umax_val = src_reg.umax_value; | |
f23cc643 | 6277 | |
3f50f132 JF |
6278 | s32_min_val = src_reg.s32_min_value; |
6279 | s32_max_val = src_reg.s32_max_value; | |
6280 | u32_min_val = src_reg.u32_min_value; | |
6281 | u32_max_val = src_reg.u32_max_value; | |
6282 | ||
6283 | if (alu32) { | |
6284 | src_known = tnum_subreg_is_const(src_reg.var_off); | |
3f50f132 JF |
6285 | if ((src_known && |
6286 | (s32_min_val != s32_max_val || u32_min_val != u32_max_val)) || | |
6287 | s32_min_val > s32_max_val || u32_min_val > u32_max_val) { | |
6288 | /* Taint dst register if offset had invalid bounds | |
6289 | * derived from e.g. dead branches. | |
6290 | */ | |
6291 | __mark_reg_unknown(env, dst_reg); | |
6292 | return 0; | |
6293 | } | |
6294 | } else { | |
6295 | src_known = tnum_is_const(src_reg.var_off); | |
3f50f132 JF |
6296 | if ((src_known && |
6297 | (smin_val != smax_val || umin_val != umax_val)) || | |
6298 | smin_val > smax_val || umin_val > umax_val) { | |
6299 | /* Taint dst register if offset had invalid bounds | |
6300 | * derived from e.g. dead branches. | |
6301 | */ | |
6302 | __mark_reg_unknown(env, dst_reg); | |
6303 | return 0; | |
6304 | } | |
6f16101e DB |
6305 | } |
6306 | ||
bb7f0f98 AS |
6307 | if (!src_known && |
6308 | opcode != BPF_ADD && opcode != BPF_SUB && opcode != BPF_AND) { | |
f54c7898 | 6309 | __mark_reg_unknown(env, dst_reg); |
bb7f0f98 AS |
6310 | return 0; |
6311 | } | |
6312 | ||
3f50f132 JF |
6313 | /* Calculate sign/unsigned bounds and tnum for alu32 and alu64 bit ops. |
6314 | * There are two classes of instructions: The first class we track both | |
6315 | * alu32 and alu64 sign/unsigned bounds independently this provides the | |
6316 | * greatest amount of precision when alu operations are mixed with jmp32 | |
6317 | * operations. These operations are BPF_ADD, BPF_SUB, BPF_MUL, BPF_ADD, | |
6318 | * and BPF_OR. This is possible because these ops have fairly easy to | |
6319 | * understand and calculate behavior in both 32-bit and 64-bit alu ops. | |
6320 | * See alu32 verifier tests for examples. The second class of | |
6321 | * operations, BPF_LSH, BPF_RSH, and BPF_ARSH, however are not so easy | |
6322 | * with regards to tracking sign/unsigned bounds because the bits may | |
6323 | * cross subreg boundaries in the alu64 case. When this happens we mark | |
6324 | * the reg unbounded in the subreg bound space and use the resulting | |
6325 | * tnum to calculate an approximation of the sign/unsigned bounds. | |
6326 | */ | |
48461135 JB |
6327 | switch (opcode) { |
6328 | case BPF_ADD: | |
d3bd7413 DB |
6329 | ret = sanitize_val_alu(env, insn); |
6330 | if (ret < 0) { | |
6331 | verbose(env, "R%d tried to add from different pointers or scalars\n", dst); | |
6332 | return ret; | |
6333 | } | |
3f50f132 | 6334 | scalar32_min_max_add(dst_reg, &src_reg); |
07cd2631 | 6335 | scalar_min_max_add(dst_reg, &src_reg); |
3f50f132 | 6336 | dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off); |
48461135 JB |
6337 | break; |
6338 | case BPF_SUB: | |
d3bd7413 DB |
6339 | ret = sanitize_val_alu(env, insn); |
6340 | if (ret < 0) { | |
6341 | verbose(env, "R%d tried to sub from different pointers or scalars\n", dst); | |
6342 | return ret; | |
6343 | } | |
3f50f132 | 6344 | scalar32_min_max_sub(dst_reg, &src_reg); |
07cd2631 | 6345 | scalar_min_max_sub(dst_reg, &src_reg); |
3f50f132 | 6346 | dst_reg->var_off = tnum_sub(dst_reg->var_off, src_reg.var_off); |
48461135 JB |
6347 | break; |
6348 | case BPF_MUL: | |
3f50f132 JF |
6349 | dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off); |
6350 | scalar32_min_max_mul(dst_reg, &src_reg); | |
07cd2631 | 6351 | scalar_min_max_mul(dst_reg, &src_reg); |
48461135 JB |
6352 | break; |
6353 | case BPF_AND: | |
3f50f132 JF |
6354 | dst_reg->var_off = tnum_and(dst_reg->var_off, src_reg.var_off); |
6355 | scalar32_min_max_and(dst_reg, &src_reg); | |
07cd2631 | 6356 | scalar_min_max_and(dst_reg, &src_reg); |
f1174f77 EC |
6357 | break; |
6358 | case BPF_OR: | |
3f50f132 JF |
6359 | dst_reg->var_off = tnum_or(dst_reg->var_off, src_reg.var_off); |
6360 | scalar32_min_max_or(dst_reg, &src_reg); | |
07cd2631 | 6361 | scalar_min_max_or(dst_reg, &src_reg); |
48461135 | 6362 | break; |
2921c90d YS |
6363 | case BPF_XOR: |
6364 | dst_reg->var_off = tnum_xor(dst_reg->var_off, src_reg.var_off); | |
6365 | scalar32_min_max_xor(dst_reg, &src_reg); | |
6366 | scalar_min_max_xor(dst_reg, &src_reg); | |
6367 | break; | |
48461135 | 6368 | case BPF_LSH: |
468f6eaf JH |
6369 | if (umax_val >= insn_bitness) { |
6370 | /* Shifts greater than 31 or 63 are undefined. | |
6371 | * This includes shifts by a negative number. | |
b03c9f9f | 6372 | */ |
61bd5218 | 6373 | mark_reg_unknown(env, regs, insn->dst_reg); |
f1174f77 EC |
6374 | break; |
6375 | } | |
3f50f132 JF |
6376 | if (alu32) |
6377 | scalar32_min_max_lsh(dst_reg, &src_reg); | |
6378 | else | |
6379 | scalar_min_max_lsh(dst_reg, &src_reg); | |
48461135 JB |
6380 | break; |
6381 | case BPF_RSH: | |
468f6eaf JH |
6382 | if (umax_val >= insn_bitness) { |
6383 | /* Shifts greater than 31 or 63 are undefined. | |
6384 | * This includes shifts by a negative number. | |
b03c9f9f | 6385 | */ |
61bd5218 | 6386 | mark_reg_unknown(env, regs, insn->dst_reg); |
f1174f77 EC |
6387 | break; |
6388 | } | |
3f50f132 JF |
6389 | if (alu32) |
6390 | scalar32_min_max_rsh(dst_reg, &src_reg); | |
6391 | else | |
6392 | scalar_min_max_rsh(dst_reg, &src_reg); | |
48461135 | 6393 | break; |
9cbe1f5a YS |
6394 | case BPF_ARSH: |
6395 | if (umax_val >= insn_bitness) { | |
6396 | /* Shifts greater than 31 or 63 are undefined. | |
6397 | * This includes shifts by a negative number. | |
6398 | */ | |
6399 | mark_reg_unknown(env, regs, insn->dst_reg); | |
6400 | break; | |
6401 | } | |
3f50f132 JF |
6402 | if (alu32) |
6403 | scalar32_min_max_arsh(dst_reg, &src_reg); | |
6404 | else | |
6405 | scalar_min_max_arsh(dst_reg, &src_reg); | |
9cbe1f5a | 6406 | break; |
48461135 | 6407 | default: |
61bd5218 | 6408 | mark_reg_unknown(env, regs, insn->dst_reg); |
48461135 JB |
6409 | break; |
6410 | } | |
6411 | ||
3f50f132 JF |
6412 | /* ALU32 ops are zero extended into 64bit register */ |
6413 | if (alu32) | |
6414 | zext_32_to_64(dst_reg); | |
468f6eaf | 6415 | |
294f2fc6 | 6416 | __update_reg_bounds(dst_reg); |
b03c9f9f EC |
6417 | __reg_deduce_bounds(dst_reg); |
6418 | __reg_bound_offset(dst_reg); | |
f1174f77 EC |
6419 | return 0; |
6420 | } | |
6421 | ||
6422 | /* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max | |
6423 | * and var_off. | |
6424 | */ | |
6425 | static int adjust_reg_min_max_vals(struct bpf_verifier_env *env, | |
6426 | struct bpf_insn *insn) | |
6427 | { | |
f4d7e40a AS |
6428 | struct bpf_verifier_state *vstate = env->cur_state; |
6429 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
6430 | struct bpf_reg_state *regs = state->regs, *dst_reg, *src_reg; | |
f1174f77 EC |
6431 | struct bpf_reg_state *ptr_reg = NULL, off_reg = {0}; |
6432 | u8 opcode = BPF_OP(insn->code); | |
b5dc0163 | 6433 | int err; |
f1174f77 EC |
6434 | |
6435 | dst_reg = ®s[insn->dst_reg]; | |
f1174f77 EC |
6436 | src_reg = NULL; |
6437 | if (dst_reg->type != SCALAR_VALUE) | |
6438 | ptr_reg = dst_reg; | |
6439 | if (BPF_SRC(insn->code) == BPF_X) { | |
6440 | src_reg = ®s[insn->src_reg]; | |
f1174f77 EC |
6441 | if (src_reg->type != SCALAR_VALUE) { |
6442 | if (dst_reg->type != SCALAR_VALUE) { | |
6443 | /* Combining two pointers by any ALU op yields | |
82abbf8d AS |
6444 | * an arbitrary scalar. Disallow all math except |
6445 | * pointer subtraction | |
f1174f77 | 6446 | */ |
dd066823 | 6447 | if (opcode == BPF_SUB && env->allow_ptr_leaks) { |
82abbf8d AS |
6448 | mark_reg_unknown(env, regs, insn->dst_reg); |
6449 | return 0; | |
f1174f77 | 6450 | } |
82abbf8d AS |
6451 | verbose(env, "R%d pointer %s pointer prohibited\n", |
6452 | insn->dst_reg, | |
6453 | bpf_alu_string[opcode >> 4]); | |
6454 | return -EACCES; | |
f1174f77 EC |
6455 | } else { |
6456 | /* scalar += pointer | |
6457 | * This is legal, but we have to reverse our | |
6458 | * src/dest handling in computing the range | |
6459 | */ | |
b5dc0163 AS |
6460 | err = mark_chain_precision(env, insn->dst_reg); |
6461 | if (err) | |
6462 | return err; | |
82abbf8d AS |
6463 | return adjust_ptr_min_max_vals(env, insn, |
6464 | src_reg, dst_reg); | |
f1174f77 EC |
6465 | } |
6466 | } else if (ptr_reg) { | |
6467 | /* pointer += scalar */ | |
b5dc0163 AS |
6468 | err = mark_chain_precision(env, insn->src_reg); |
6469 | if (err) | |
6470 | return err; | |
82abbf8d AS |
6471 | return adjust_ptr_min_max_vals(env, insn, |
6472 | dst_reg, src_reg); | |
f1174f77 EC |
6473 | } |
6474 | } else { | |
6475 | /* Pretend the src is a reg with a known value, since we only | |
6476 | * need to be able to read from this state. | |
6477 | */ | |
6478 | off_reg.type = SCALAR_VALUE; | |
b03c9f9f | 6479 | __mark_reg_known(&off_reg, insn->imm); |
f1174f77 | 6480 | src_reg = &off_reg; |
82abbf8d AS |
6481 | if (ptr_reg) /* pointer += K */ |
6482 | return adjust_ptr_min_max_vals(env, insn, | |
6483 | ptr_reg, src_reg); | |
f1174f77 EC |
6484 | } |
6485 | ||
6486 | /* Got here implies adding two SCALAR_VALUEs */ | |
6487 | if (WARN_ON_ONCE(ptr_reg)) { | |
f4d7e40a | 6488 | print_verifier_state(env, state); |
61bd5218 | 6489 | verbose(env, "verifier internal error: unexpected ptr_reg\n"); |
f1174f77 EC |
6490 | return -EINVAL; |
6491 | } | |
6492 | if (WARN_ON(!src_reg)) { | |
f4d7e40a | 6493 | print_verifier_state(env, state); |
61bd5218 | 6494 | verbose(env, "verifier internal error: no src_reg\n"); |
f1174f77 EC |
6495 | return -EINVAL; |
6496 | } | |
6497 | return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg); | |
48461135 JB |
6498 | } |
6499 | ||
17a52670 | 6500 | /* check validity of 32-bit and 64-bit arithmetic operations */ |
58e2af8b | 6501 | static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn) |
17a52670 | 6502 | { |
638f5b90 | 6503 | struct bpf_reg_state *regs = cur_regs(env); |
17a52670 AS |
6504 | u8 opcode = BPF_OP(insn->code); |
6505 | int err; | |
6506 | ||
6507 | if (opcode == BPF_END || opcode == BPF_NEG) { | |
6508 | if (opcode == BPF_NEG) { | |
6509 | if (BPF_SRC(insn->code) != 0 || | |
6510 | insn->src_reg != BPF_REG_0 || | |
6511 | insn->off != 0 || insn->imm != 0) { | |
61bd5218 | 6512 | verbose(env, "BPF_NEG uses reserved fields\n"); |
17a52670 AS |
6513 | return -EINVAL; |
6514 | } | |
6515 | } else { | |
6516 | if (insn->src_reg != BPF_REG_0 || insn->off != 0 || | |
e67b8a68 EC |
6517 | (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) || |
6518 | BPF_CLASS(insn->code) == BPF_ALU64) { | |
61bd5218 | 6519 | verbose(env, "BPF_END uses reserved fields\n"); |
17a52670 AS |
6520 | return -EINVAL; |
6521 | } | |
6522 | } | |
6523 | ||
6524 | /* check src operand */ | |
dc503a8a | 6525 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
6526 | if (err) |
6527 | return err; | |
6528 | ||
1be7f75d | 6529 | if (is_pointer_value(env, insn->dst_reg)) { |
61bd5218 | 6530 | verbose(env, "R%d pointer arithmetic prohibited\n", |
1be7f75d AS |
6531 | insn->dst_reg); |
6532 | return -EACCES; | |
6533 | } | |
6534 | ||
17a52670 | 6535 | /* check dest operand */ |
dc503a8a | 6536 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
17a52670 AS |
6537 | if (err) |
6538 | return err; | |
6539 | ||
6540 | } else if (opcode == BPF_MOV) { | |
6541 | ||
6542 | if (BPF_SRC(insn->code) == BPF_X) { | |
6543 | if (insn->imm != 0 || insn->off != 0) { | |
61bd5218 | 6544 | verbose(env, "BPF_MOV uses reserved fields\n"); |
17a52670 AS |
6545 | return -EINVAL; |
6546 | } | |
6547 | ||
6548 | /* check src operand */ | |
dc503a8a | 6549 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
6550 | if (err) |
6551 | return err; | |
6552 | } else { | |
6553 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { | |
61bd5218 | 6554 | verbose(env, "BPF_MOV uses reserved fields\n"); |
17a52670 AS |
6555 | return -EINVAL; |
6556 | } | |
6557 | } | |
6558 | ||
fbeb1603 AF |
6559 | /* check dest operand, mark as required later */ |
6560 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); | |
17a52670 AS |
6561 | if (err) |
6562 | return err; | |
6563 | ||
6564 | if (BPF_SRC(insn->code) == BPF_X) { | |
e434b8cd JW |
6565 | struct bpf_reg_state *src_reg = regs + insn->src_reg; |
6566 | struct bpf_reg_state *dst_reg = regs + insn->dst_reg; | |
6567 | ||
17a52670 AS |
6568 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
6569 | /* case: R1 = R2 | |
6570 | * copy register state to dest reg | |
6571 | */ | |
e434b8cd JW |
6572 | *dst_reg = *src_reg; |
6573 | dst_reg->live |= REG_LIVE_WRITTEN; | |
5327ed3d | 6574 | dst_reg->subreg_def = DEF_NOT_SUBREG; |
17a52670 | 6575 | } else { |
f1174f77 | 6576 | /* R1 = (u32) R2 */ |
1be7f75d | 6577 | if (is_pointer_value(env, insn->src_reg)) { |
61bd5218 JK |
6578 | verbose(env, |
6579 | "R%d partial copy of pointer\n", | |
1be7f75d AS |
6580 | insn->src_reg); |
6581 | return -EACCES; | |
e434b8cd JW |
6582 | } else if (src_reg->type == SCALAR_VALUE) { |
6583 | *dst_reg = *src_reg; | |
6584 | dst_reg->live |= REG_LIVE_WRITTEN; | |
5327ed3d | 6585 | dst_reg->subreg_def = env->insn_idx + 1; |
e434b8cd JW |
6586 | } else { |
6587 | mark_reg_unknown(env, regs, | |
6588 | insn->dst_reg); | |
1be7f75d | 6589 | } |
3f50f132 | 6590 | zext_32_to_64(dst_reg); |
17a52670 AS |
6591 | } |
6592 | } else { | |
6593 | /* case: R = imm | |
6594 | * remember the value we stored into this reg | |
6595 | */ | |
fbeb1603 AF |
6596 | /* clear any state __mark_reg_known doesn't set */ |
6597 | mark_reg_unknown(env, regs, insn->dst_reg); | |
f1174f77 | 6598 | regs[insn->dst_reg].type = SCALAR_VALUE; |
95a762e2 JH |
6599 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
6600 | __mark_reg_known(regs + insn->dst_reg, | |
6601 | insn->imm); | |
6602 | } else { | |
6603 | __mark_reg_known(regs + insn->dst_reg, | |
6604 | (u32)insn->imm); | |
6605 | } | |
17a52670 AS |
6606 | } |
6607 | ||
6608 | } else if (opcode > BPF_END) { | |
61bd5218 | 6609 | verbose(env, "invalid BPF_ALU opcode %x\n", opcode); |
17a52670 AS |
6610 | return -EINVAL; |
6611 | ||
6612 | } else { /* all other ALU ops: and, sub, xor, add, ... */ | |
6613 | ||
17a52670 AS |
6614 | if (BPF_SRC(insn->code) == BPF_X) { |
6615 | if (insn->imm != 0 || insn->off != 0) { | |
61bd5218 | 6616 | verbose(env, "BPF_ALU uses reserved fields\n"); |
17a52670 AS |
6617 | return -EINVAL; |
6618 | } | |
6619 | /* check src1 operand */ | |
dc503a8a | 6620 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
6621 | if (err) |
6622 | return err; | |
6623 | } else { | |
6624 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { | |
61bd5218 | 6625 | verbose(env, "BPF_ALU uses reserved fields\n"); |
17a52670 AS |
6626 | return -EINVAL; |
6627 | } | |
6628 | } | |
6629 | ||
6630 | /* check src2 operand */ | |
dc503a8a | 6631 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
6632 | if (err) |
6633 | return err; | |
6634 | ||
6635 | if ((opcode == BPF_MOD || opcode == BPF_DIV) && | |
6636 | BPF_SRC(insn->code) == BPF_K && insn->imm == 0) { | |
61bd5218 | 6637 | verbose(env, "div by zero\n"); |
17a52670 AS |
6638 | return -EINVAL; |
6639 | } | |
6640 | ||
229394e8 RV |
6641 | if ((opcode == BPF_LSH || opcode == BPF_RSH || |
6642 | opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) { | |
6643 | int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32; | |
6644 | ||
6645 | if (insn->imm < 0 || insn->imm >= size) { | |
61bd5218 | 6646 | verbose(env, "invalid shift %d\n", insn->imm); |
229394e8 RV |
6647 | return -EINVAL; |
6648 | } | |
6649 | } | |
6650 | ||
1a0dc1ac | 6651 | /* check dest operand */ |
dc503a8a | 6652 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
1a0dc1ac AS |
6653 | if (err) |
6654 | return err; | |
6655 | ||
f1174f77 | 6656 | return adjust_reg_min_max_vals(env, insn); |
17a52670 AS |
6657 | } |
6658 | ||
6659 | return 0; | |
6660 | } | |
6661 | ||
c6a9efa1 PC |
6662 | static void __find_good_pkt_pointers(struct bpf_func_state *state, |
6663 | struct bpf_reg_state *dst_reg, | |
6664 | enum bpf_reg_type type, u16 new_range) | |
6665 | { | |
6666 | struct bpf_reg_state *reg; | |
6667 | int i; | |
6668 | ||
6669 | for (i = 0; i < MAX_BPF_REG; i++) { | |
6670 | reg = &state->regs[i]; | |
6671 | if (reg->type == type && reg->id == dst_reg->id) | |
6672 | /* keep the maximum range already checked */ | |
6673 | reg->range = max(reg->range, new_range); | |
6674 | } | |
6675 | ||
6676 | bpf_for_each_spilled_reg(i, state, reg) { | |
6677 | if (!reg) | |
6678 | continue; | |
6679 | if (reg->type == type && reg->id == dst_reg->id) | |
6680 | reg->range = max(reg->range, new_range); | |
6681 | } | |
6682 | } | |
6683 | ||
f4d7e40a | 6684 | static void find_good_pkt_pointers(struct bpf_verifier_state *vstate, |
de8f3a83 | 6685 | struct bpf_reg_state *dst_reg, |
f8ddadc4 | 6686 | enum bpf_reg_type type, |
fb2a311a | 6687 | bool range_right_open) |
969bf05e | 6688 | { |
fb2a311a | 6689 | u16 new_range; |
c6a9efa1 | 6690 | int i; |
2d2be8ca | 6691 | |
fb2a311a DB |
6692 | if (dst_reg->off < 0 || |
6693 | (dst_reg->off == 0 && range_right_open)) | |
f1174f77 EC |
6694 | /* This doesn't give us any range */ |
6695 | return; | |
6696 | ||
b03c9f9f EC |
6697 | if (dst_reg->umax_value > MAX_PACKET_OFF || |
6698 | dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF) | |
f1174f77 EC |
6699 | /* Risk of overflow. For instance, ptr + (1<<63) may be less |
6700 | * than pkt_end, but that's because it's also less than pkt. | |
6701 | */ | |
6702 | return; | |
6703 | ||
fb2a311a DB |
6704 | new_range = dst_reg->off; |
6705 | if (range_right_open) | |
6706 | new_range--; | |
6707 | ||
6708 | /* Examples for register markings: | |
2d2be8ca | 6709 | * |
fb2a311a | 6710 | * pkt_data in dst register: |
2d2be8ca DB |
6711 | * |
6712 | * r2 = r3; | |
6713 | * r2 += 8; | |
6714 | * if (r2 > pkt_end) goto <handle exception> | |
6715 | * <access okay> | |
6716 | * | |
b4e432f1 DB |
6717 | * r2 = r3; |
6718 | * r2 += 8; | |
6719 | * if (r2 < pkt_end) goto <access okay> | |
6720 | * <handle exception> | |
6721 | * | |
2d2be8ca DB |
6722 | * Where: |
6723 | * r2 == dst_reg, pkt_end == src_reg | |
6724 | * r2=pkt(id=n,off=8,r=0) | |
6725 | * r3=pkt(id=n,off=0,r=0) | |
6726 | * | |
fb2a311a | 6727 | * pkt_data in src register: |
2d2be8ca DB |
6728 | * |
6729 | * r2 = r3; | |
6730 | * r2 += 8; | |
6731 | * if (pkt_end >= r2) goto <access okay> | |
6732 | * <handle exception> | |
6733 | * | |
b4e432f1 DB |
6734 | * r2 = r3; |
6735 | * r2 += 8; | |
6736 | * if (pkt_end <= r2) goto <handle exception> | |
6737 | * <access okay> | |
6738 | * | |
2d2be8ca DB |
6739 | * Where: |
6740 | * pkt_end == dst_reg, r2 == src_reg | |
6741 | * r2=pkt(id=n,off=8,r=0) | |
6742 | * r3=pkt(id=n,off=0,r=0) | |
6743 | * | |
6744 | * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8) | |
fb2a311a DB |
6745 | * or r3=pkt(id=n,off=0,r=8-1), so that range of bytes [r3, r3 + 8) |
6746 | * and [r3, r3 + 8-1) respectively is safe to access depending on | |
6747 | * the check. | |
969bf05e | 6748 | */ |
2d2be8ca | 6749 | |
f1174f77 EC |
6750 | /* If our ids match, then we must have the same max_value. And we |
6751 | * don't care about the other reg's fixed offset, since if it's too big | |
6752 | * the range won't allow anything. | |
6753 | * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16. | |
6754 | */ | |
c6a9efa1 PC |
6755 | for (i = 0; i <= vstate->curframe; i++) |
6756 | __find_good_pkt_pointers(vstate->frame[i], dst_reg, type, | |
6757 | new_range); | |
969bf05e AS |
6758 | } |
6759 | ||
3f50f132 | 6760 | static int is_branch32_taken(struct bpf_reg_state *reg, u32 val, u8 opcode) |
4f7b3e82 | 6761 | { |
3f50f132 JF |
6762 | struct tnum subreg = tnum_subreg(reg->var_off); |
6763 | s32 sval = (s32)val; | |
a72dafaf | 6764 | |
3f50f132 JF |
6765 | switch (opcode) { |
6766 | case BPF_JEQ: | |
6767 | if (tnum_is_const(subreg)) | |
6768 | return !!tnum_equals_const(subreg, val); | |
6769 | break; | |
6770 | case BPF_JNE: | |
6771 | if (tnum_is_const(subreg)) | |
6772 | return !tnum_equals_const(subreg, val); | |
6773 | break; | |
6774 | case BPF_JSET: | |
6775 | if ((~subreg.mask & subreg.value) & val) | |
6776 | return 1; | |
6777 | if (!((subreg.mask | subreg.value) & val)) | |
6778 | return 0; | |
6779 | break; | |
6780 | case BPF_JGT: | |
6781 | if (reg->u32_min_value > val) | |
6782 | return 1; | |
6783 | else if (reg->u32_max_value <= val) | |
6784 | return 0; | |
6785 | break; | |
6786 | case BPF_JSGT: | |
6787 | if (reg->s32_min_value > sval) | |
6788 | return 1; | |
6789 | else if (reg->s32_max_value < sval) | |
6790 | return 0; | |
6791 | break; | |
6792 | case BPF_JLT: | |
6793 | if (reg->u32_max_value < val) | |
6794 | return 1; | |
6795 | else if (reg->u32_min_value >= val) | |
6796 | return 0; | |
6797 | break; | |
6798 | case BPF_JSLT: | |
6799 | if (reg->s32_max_value < sval) | |
6800 | return 1; | |
6801 | else if (reg->s32_min_value >= sval) | |
6802 | return 0; | |
6803 | break; | |
6804 | case BPF_JGE: | |
6805 | if (reg->u32_min_value >= val) | |
6806 | return 1; | |
6807 | else if (reg->u32_max_value < val) | |
6808 | return 0; | |
6809 | break; | |
6810 | case BPF_JSGE: | |
6811 | if (reg->s32_min_value >= sval) | |
6812 | return 1; | |
6813 | else if (reg->s32_max_value < sval) | |
6814 | return 0; | |
6815 | break; | |
6816 | case BPF_JLE: | |
6817 | if (reg->u32_max_value <= val) | |
6818 | return 1; | |
6819 | else if (reg->u32_min_value > val) | |
6820 | return 0; | |
6821 | break; | |
6822 | case BPF_JSLE: | |
6823 | if (reg->s32_max_value <= sval) | |
6824 | return 1; | |
6825 | else if (reg->s32_min_value > sval) | |
6826 | return 0; | |
6827 | break; | |
6828 | } | |
4f7b3e82 | 6829 | |
3f50f132 JF |
6830 | return -1; |
6831 | } | |
092ed096 | 6832 | |
3f50f132 JF |
6833 | |
6834 | static int is_branch64_taken(struct bpf_reg_state *reg, u64 val, u8 opcode) | |
6835 | { | |
6836 | s64 sval = (s64)val; | |
a72dafaf | 6837 | |
4f7b3e82 AS |
6838 | switch (opcode) { |
6839 | case BPF_JEQ: | |
6840 | if (tnum_is_const(reg->var_off)) | |
6841 | return !!tnum_equals_const(reg->var_off, val); | |
6842 | break; | |
6843 | case BPF_JNE: | |
6844 | if (tnum_is_const(reg->var_off)) | |
6845 | return !tnum_equals_const(reg->var_off, val); | |
6846 | break; | |
960ea056 JK |
6847 | case BPF_JSET: |
6848 | if ((~reg->var_off.mask & reg->var_off.value) & val) | |
6849 | return 1; | |
6850 | if (!((reg->var_off.mask | reg->var_off.value) & val)) | |
6851 | return 0; | |
6852 | break; | |
4f7b3e82 AS |
6853 | case BPF_JGT: |
6854 | if (reg->umin_value > val) | |
6855 | return 1; | |
6856 | else if (reg->umax_value <= val) | |
6857 | return 0; | |
6858 | break; | |
6859 | case BPF_JSGT: | |
a72dafaf | 6860 | if (reg->smin_value > sval) |
4f7b3e82 | 6861 | return 1; |
a72dafaf | 6862 | else if (reg->smax_value < sval) |
4f7b3e82 AS |
6863 | return 0; |
6864 | break; | |
6865 | case BPF_JLT: | |
6866 | if (reg->umax_value < val) | |
6867 | return 1; | |
6868 | else if (reg->umin_value >= val) | |
6869 | return 0; | |
6870 | break; | |
6871 | case BPF_JSLT: | |
a72dafaf | 6872 | if (reg->smax_value < sval) |
4f7b3e82 | 6873 | return 1; |
a72dafaf | 6874 | else if (reg->smin_value >= sval) |
4f7b3e82 AS |
6875 | return 0; |
6876 | break; | |
6877 | case BPF_JGE: | |
6878 | if (reg->umin_value >= val) | |
6879 | return 1; | |
6880 | else if (reg->umax_value < val) | |
6881 | return 0; | |
6882 | break; | |
6883 | case BPF_JSGE: | |
a72dafaf | 6884 | if (reg->smin_value >= sval) |
4f7b3e82 | 6885 | return 1; |
a72dafaf | 6886 | else if (reg->smax_value < sval) |
4f7b3e82 AS |
6887 | return 0; |
6888 | break; | |
6889 | case BPF_JLE: | |
6890 | if (reg->umax_value <= val) | |
6891 | return 1; | |
6892 | else if (reg->umin_value > val) | |
6893 | return 0; | |
6894 | break; | |
6895 | case BPF_JSLE: | |
a72dafaf | 6896 | if (reg->smax_value <= sval) |
4f7b3e82 | 6897 | return 1; |
a72dafaf | 6898 | else if (reg->smin_value > sval) |
4f7b3e82 AS |
6899 | return 0; |
6900 | break; | |
6901 | } | |
6902 | ||
6903 | return -1; | |
6904 | } | |
6905 | ||
3f50f132 JF |
6906 | /* compute branch direction of the expression "if (reg opcode val) goto target;" |
6907 | * and return: | |
6908 | * 1 - branch will be taken and "goto target" will be executed | |
6909 | * 0 - branch will not be taken and fall-through to next insn | |
6910 | * -1 - unknown. Example: "if (reg < 5)" is unknown when register value | |
6911 | * range [0,10] | |
604dca5e | 6912 | */ |
3f50f132 JF |
6913 | static int is_branch_taken(struct bpf_reg_state *reg, u64 val, u8 opcode, |
6914 | bool is_jmp32) | |
604dca5e | 6915 | { |
cac616db JF |
6916 | if (__is_pointer_value(false, reg)) { |
6917 | if (!reg_type_not_null(reg->type)) | |
6918 | return -1; | |
6919 | ||
6920 | /* If pointer is valid tests against zero will fail so we can | |
6921 | * use this to direct branch taken. | |
6922 | */ | |
6923 | if (val != 0) | |
6924 | return -1; | |
6925 | ||
6926 | switch (opcode) { | |
6927 | case BPF_JEQ: | |
6928 | return 0; | |
6929 | case BPF_JNE: | |
6930 | return 1; | |
6931 | default: | |
6932 | return -1; | |
6933 | } | |
6934 | } | |
604dca5e | 6935 | |
3f50f132 JF |
6936 | if (is_jmp32) |
6937 | return is_branch32_taken(reg, val, opcode); | |
6938 | return is_branch64_taken(reg, val, opcode); | |
604dca5e JH |
6939 | } |
6940 | ||
48461135 JB |
6941 | /* Adjusts the register min/max values in the case that the dst_reg is the |
6942 | * variable register that we are working on, and src_reg is a constant or we're | |
6943 | * simply doing a BPF_K check. | |
f1174f77 | 6944 | * In JEQ/JNE cases we also adjust the var_off values. |
48461135 JB |
6945 | */ |
6946 | static void reg_set_min_max(struct bpf_reg_state *true_reg, | |
3f50f132 JF |
6947 | struct bpf_reg_state *false_reg, |
6948 | u64 val, u32 val32, | |
092ed096 | 6949 | u8 opcode, bool is_jmp32) |
48461135 | 6950 | { |
3f50f132 JF |
6951 | struct tnum false_32off = tnum_subreg(false_reg->var_off); |
6952 | struct tnum false_64off = false_reg->var_off; | |
6953 | struct tnum true_32off = tnum_subreg(true_reg->var_off); | |
6954 | struct tnum true_64off = true_reg->var_off; | |
6955 | s64 sval = (s64)val; | |
6956 | s32 sval32 = (s32)val32; | |
a72dafaf | 6957 | |
f1174f77 EC |
6958 | /* If the dst_reg is a pointer, we can't learn anything about its |
6959 | * variable offset from the compare (unless src_reg were a pointer into | |
6960 | * the same object, but we don't bother with that. | |
6961 | * Since false_reg and true_reg have the same type by construction, we | |
6962 | * only need to check one of them for pointerness. | |
6963 | */ | |
6964 | if (__is_pointer_value(false, false_reg)) | |
6965 | return; | |
4cabc5b1 | 6966 | |
48461135 JB |
6967 | switch (opcode) { |
6968 | case BPF_JEQ: | |
48461135 | 6969 | case BPF_JNE: |
a72dafaf JW |
6970 | { |
6971 | struct bpf_reg_state *reg = | |
6972 | opcode == BPF_JEQ ? true_reg : false_reg; | |
6973 | ||
6974 | /* For BPF_JEQ, if this is false we know nothing Jon Snow, but | |
6975 | * if it is true we know the value for sure. Likewise for | |
6976 | * BPF_JNE. | |
48461135 | 6977 | */ |
3f50f132 JF |
6978 | if (is_jmp32) |
6979 | __mark_reg32_known(reg, val32); | |
6980 | else | |
092ed096 | 6981 | __mark_reg_known(reg, val); |
48461135 | 6982 | break; |
a72dafaf | 6983 | } |
960ea056 | 6984 | case BPF_JSET: |
3f50f132 JF |
6985 | if (is_jmp32) { |
6986 | false_32off = tnum_and(false_32off, tnum_const(~val32)); | |
6987 | if (is_power_of_2(val32)) | |
6988 | true_32off = tnum_or(true_32off, | |
6989 | tnum_const(val32)); | |
6990 | } else { | |
6991 | false_64off = tnum_and(false_64off, tnum_const(~val)); | |
6992 | if (is_power_of_2(val)) | |
6993 | true_64off = tnum_or(true_64off, | |
6994 | tnum_const(val)); | |
6995 | } | |
960ea056 | 6996 | break; |
48461135 | 6997 | case BPF_JGE: |
a72dafaf JW |
6998 | case BPF_JGT: |
6999 | { | |
3f50f132 JF |
7000 | if (is_jmp32) { |
7001 | u32 false_umax = opcode == BPF_JGT ? val32 : val32 - 1; | |
7002 | u32 true_umin = opcode == BPF_JGT ? val32 + 1 : val32; | |
7003 | ||
7004 | false_reg->u32_max_value = min(false_reg->u32_max_value, | |
7005 | false_umax); | |
7006 | true_reg->u32_min_value = max(true_reg->u32_min_value, | |
7007 | true_umin); | |
7008 | } else { | |
7009 | u64 false_umax = opcode == BPF_JGT ? val : val - 1; | |
7010 | u64 true_umin = opcode == BPF_JGT ? val + 1 : val; | |
7011 | ||
7012 | false_reg->umax_value = min(false_reg->umax_value, false_umax); | |
7013 | true_reg->umin_value = max(true_reg->umin_value, true_umin); | |
7014 | } | |
b03c9f9f | 7015 | break; |
a72dafaf | 7016 | } |
48461135 | 7017 | case BPF_JSGE: |
a72dafaf JW |
7018 | case BPF_JSGT: |
7019 | { | |
3f50f132 JF |
7020 | if (is_jmp32) { |
7021 | s32 false_smax = opcode == BPF_JSGT ? sval32 : sval32 - 1; | |
7022 | s32 true_smin = opcode == BPF_JSGT ? sval32 + 1 : sval32; | |
a72dafaf | 7023 | |
3f50f132 JF |
7024 | false_reg->s32_max_value = min(false_reg->s32_max_value, false_smax); |
7025 | true_reg->s32_min_value = max(true_reg->s32_min_value, true_smin); | |
7026 | } else { | |
7027 | s64 false_smax = opcode == BPF_JSGT ? sval : sval - 1; | |
7028 | s64 true_smin = opcode == BPF_JSGT ? sval + 1 : sval; | |
7029 | ||
7030 | false_reg->smax_value = min(false_reg->smax_value, false_smax); | |
7031 | true_reg->smin_value = max(true_reg->smin_value, true_smin); | |
7032 | } | |
48461135 | 7033 | break; |
a72dafaf | 7034 | } |
b4e432f1 | 7035 | case BPF_JLE: |
a72dafaf JW |
7036 | case BPF_JLT: |
7037 | { | |
3f50f132 JF |
7038 | if (is_jmp32) { |
7039 | u32 false_umin = opcode == BPF_JLT ? val32 : val32 + 1; | |
7040 | u32 true_umax = opcode == BPF_JLT ? val32 - 1 : val32; | |
7041 | ||
7042 | false_reg->u32_min_value = max(false_reg->u32_min_value, | |
7043 | false_umin); | |
7044 | true_reg->u32_max_value = min(true_reg->u32_max_value, | |
7045 | true_umax); | |
7046 | } else { | |
7047 | u64 false_umin = opcode == BPF_JLT ? val : val + 1; | |
7048 | u64 true_umax = opcode == BPF_JLT ? val - 1 : val; | |
7049 | ||
7050 | false_reg->umin_value = max(false_reg->umin_value, false_umin); | |
7051 | true_reg->umax_value = min(true_reg->umax_value, true_umax); | |
7052 | } | |
b4e432f1 | 7053 | break; |
a72dafaf | 7054 | } |
b4e432f1 | 7055 | case BPF_JSLE: |
a72dafaf JW |
7056 | case BPF_JSLT: |
7057 | { | |
3f50f132 JF |
7058 | if (is_jmp32) { |
7059 | s32 false_smin = opcode == BPF_JSLT ? sval32 : sval32 + 1; | |
7060 | s32 true_smax = opcode == BPF_JSLT ? sval32 - 1 : sval32; | |
a72dafaf | 7061 | |
3f50f132 JF |
7062 | false_reg->s32_min_value = max(false_reg->s32_min_value, false_smin); |
7063 | true_reg->s32_max_value = min(true_reg->s32_max_value, true_smax); | |
7064 | } else { | |
7065 | s64 false_smin = opcode == BPF_JSLT ? sval : sval + 1; | |
7066 | s64 true_smax = opcode == BPF_JSLT ? sval - 1 : sval; | |
7067 | ||
7068 | false_reg->smin_value = max(false_reg->smin_value, false_smin); | |
7069 | true_reg->smax_value = min(true_reg->smax_value, true_smax); | |
7070 | } | |
b4e432f1 | 7071 | break; |
a72dafaf | 7072 | } |
48461135 | 7073 | default: |
0fc31b10 | 7074 | return; |
48461135 JB |
7075 | } |
7076 | ||
3f50f132 JF |
7077 | if (is_jmp32) { |
7078 | false_reg->var_off = tnum_or(tnum_clear_subreg(false_64off), | |
7079 | tnum_subreg(false_32off)); | |
7080 | true_reg->var_off = tnum_or(tnum_clear_subreg(true_64off), | |
7081 | tnum_subreg(true_32off)); | |
7082 | __reg_combine_32_into_64(false_reg); | |
7083 | __reg_combine_32_into_64(true_reg); | |
7084 | } else { | |
7085 | false_reg->var_off = false_64off; | |
7086 | true_reg->var_off = true_64off; | |
7087 | __reg_combine_64_into_32(false_reg); | |
7088 | __reg_combine_64_into_32(true_reg); | |
7089 | } | |
48461135 JB |
7090 | } |
7091 | ||
f1174f77 EC |
7092 | /* Same as above, but for the case that dst_reg holds a constant and src_reg is |
7093 | * the variable reg. | |
48461135 JB |
7094 | */ |
7095 | static void reg_set_min_max_inv(struct bpf_reg_state *true_reg, | |
3f50f132 JF |
7096 | struct bpf_reg_state *false_reg, |
7097 | u64 val, u32 val32, | |
092ed096 | 7098 | u8 opcode, bool is_jmp32) |
48461135 | 7099 | { |
0fc31b10 JH |
7100 | /* How can we transform "a <op> b" into "b <op> a"? */ |
7101 | static const u8 opcode_flip[16] = { | |
7102 | /* these stay the same */ | |
7103 | [BPF_JEQ >> 4] = BPF_JEQ, | |
7104 | [BPF_JNE >> 4] = BPF_JNE, | |
7105 | [BPF_JSET >> 4] = BPF_JSET, | |
7106 | /* these swap "lesser" and "greater" (L and G in the opcodes) */ | |
7107 | [BPF_JGE >> 4] = BPF_JLE, | |
7108 | [BPF_JGT >> 4] = BPF_JLT, | |
7109 | [BPF_JLE >> 4] = BPF_JGE, | |
7110 | [BPF_JLT >> 4] = BPF_JGT, | |
7111 | [BPF_JSGE >> 4] = BPF_JSLE, | |
7112 | [BPF_JSGT >> 4] = BPF_JSLT, | |
7113 | [BPF_JSLE >> 4] = BPF_JSGE, | |
7114 | [BPF_JSLT >> 4] = BPF_JSGT | |
7115 | }; | |
7116 | opcode = opcode_flip[opcode >> 4]; | |
7117 | /* This uses zero as "not present in table"; luckily the zero opcode, | |
7118 | * BPF_JA, can't get here. | |
b03c9f9f | 7119 | */ |
0fc31b10 | 7120 | if (opcode) |
3f50f132 | 7121 | reg_set_min_max(true_reg, false_reg, val, val32, opcode, is_jmp32); |
f1174f77 EC |
7122 | } |
7123 | ||
7124 | /* Regs are known to be equal, so intersect their min/max/var_off */ | |
7125 | static void __reg_combine_min_max(struct bpf_reg_state *src_reg, | |
7126 | struct bpf_reg_state *dst_reg) | |
7127 | { | |
b03c9f9f EC |
7128 | src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value, |
7129 | dst_reg->umin_value); | |
7130 | src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value, | |
7131 | dst_reg->umax_value); | |
7132 | src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value, | |
7133 | dst_reg->smin_value); | |
7134 | src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value, | |
7135 | dst_reg->smax_value); | |
f1174f77 EC |
7136 | src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off, |
7137 | dst_reg->var_off); | |
b03c9f9f EC |
7138 | /* We might have learned new bounds from the var_off. */ |
7139 | __update_reg_bounds(src_reg); | |
7140 | __update_reg_bounds(dst_reg); | |
7141 | /* We might have learned something about the sign bit. */ | |
7142 | __reg_deduce_bounds(src_reg); | |
7143 | __reg_deduce_bounds(dst_reg); | |
7144 | /* We might have learned some bits from the bounds. */ | |
7145 | __reg_bound_offset(src_reg); | |
7146 | __reg_bound_offset(dst_reg); | |
7147 | /* Intersecting with the old var_off might have improved our bounds | |
7148 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), | |
7149 | * then new var_off is (0; 0x7f...fc) which improves our umax. | |
7150 | */ | |
7151 | __update_reg_bounds(src_reg); | |
7152 | __update_reg_bounds(dst_reg); | |
f1174f77 EC |
7153 | } |
7154 | ||
7155 | static void reg_combine_min_max(struct bpf_reg_state *true_src, | |
7156 | struct bpf_reg_state *true_dst, | |
7157 | struct bpf_reg_state *false_src, | |
7158 | struct bpf_reg_state *false_dst, | |
7159 | u8 opcode) | |
7160 | { | |
7161 | switch (opcode) { | |
7162 | case BPF_JEQ: | |
7163 | __reg_combine_min_max(true_src, true_dst); | |
7164 | break; | |
7165 | case BPF_JNE: | |
7166 | __reg_combine_min_max(false_src, false_dst); | |
b03c9f9f | 7167 | break; |
4cabc5b1 | 7168 | } |
48461135 JB |
7169 | } |
7170 | ||
fd978bf7 JS |
7171 | static void mark_ptr_or_null_reg(struct bpf_func_state *state, |
7172 | struct bpf_reg_state *reg, u32 id, | |
840b9615 | 7173 | bool is_null) |
57a09bf0 | 7174 | { |
840b9615 | 7175 | if (reg_type_may_be_null(reg->type) && reg->id == id) { |
f1174f77 EC |
7176 | /* Old offset (both fixed and variable parts) should |
7177 | * have been known-zero, because we don't allow pointer | |
7178 | * arithmetic on pointers that might be NULL. | |
7179 | */ | |
b03c9f9f EC |
7180 | if (WARN_ON_ONCE(reg->smin_value || reg->smax_value || |
7181 | !tnum_equals_const(reg->var_off, 0) || | |
f1174f77 | 7182 | reg->off)) { |
b03c9f9f EC |
7183 | __mark_reg_known_zero(reg); |
7184 | reg->off = 0; | |
f1174f77 EC |
7185 | } |
7186 | if (is_null) { | |
7187 | reg->type = SCALAR_VALUE; | |
840b9615 | 7188 | } else if (reg->type == PTR_TO_MAP_VALUE_OR_NULL) { |
64d85290 JS |
7189 | const struct bpf_map *map = reg->map_ptr; |
7190 | ||
7191 | if (map->inner_map_meta) { | |
840b9615 | 7192 | reg->type = CONST_PTR_TO_MAP; |
64d85290 JS |
7193 | reg->map_ptr = map->inner_map_meta; |
7194 | } else if (map->map_type == BPF_MAP_TYPE_XSKMAP) { | |
fada7fdc | 7195 | reg->type = PTR_TO_XDP_SOCK; |
64d85290 JS |
7196 | } else if (map->map_type == BPF_MAP_TYPE_SOCKMAP || |
7197 | map->map_type == BPF_MAP_TYPE_SOCKHASH) { | |
7198 | reg->type = PTR_TO_SOCKET; | |
840b9615 JS |
7199 | } else { |
7200 | reg->type = PTR_TO_MAP_VALUE; | |
7201 | } | |
c64b7983 JS |
7202 | } else if (reg->type == PTR_TO_SOCKET_OR_NULL) { |
7203 | reg->type = PTR_TO_SOCKET; | |
46f8bc92 MKL |
7204 | } else if (reg->type == PTR_TO_SOCK_COMMON_OR_NULL) { |
7205 | reg->type = PTR_TO_SOCK_COMMON; | |
655a51e5 MKL |
7206 | } else if (reg->type == PTR_TO_TCP_SOCK_OR_NULL) { |
7207 | reg->type = PTR_TO_TCP_SOCK; | |
b121b341 YS |
7208 | } else if (reg->type == PTR_TO_BTF_ID_OR_NULL) { |
7209 | reg->type = PTR_TO_BTF_ID; | |
457f4436 AN |
7210 | } else if (reg->type == PTR_TO_MEM_OR_NULL) { |
7211 | reg->type = PTR_TO_MEM; | |
afbf21dc YS |
7212 | } else if (reg->type == PTR_TO_RDONLY_BUF_OR_NULL) { |
7213 | reg->type = PTR_TO_RDONLY_BUF; | |
7214 | } else if (reg->type == PTR_TO_RDWR_BUF_OR_NULL) { | |
7215 | reg->type = PTR_TO_RDWR_BUF; | |
56f668df | 7216 | } |
1b986589 MKL |
7217 | if (is_null) { |
7218 | /* We don't need id and ref_obj_id from this point | |
7219 | * onwards anymore, thus we should better reset it, | |
7220 | * so that state pruning has chances to take effect. | |
7221 | */ | |
7222 | reg->id = 0; | |
7223 | reg->ref_obj_id = 0; | |
7224 | } else if (!reg_may_point_to_spin_lock(reg)) { | |
7225 | /* For not-NULL ptr, reg->ref_obj_id will be reset | |
7226 | * in release_reg_references(). | |
7227 | * | |
7228 | * reg->id is still used by spin_lock ptr. Other | |
7229 | * than spin_lock ptr type, reg->id can be reset. | |
fd978bf7 JS |
7230 | */ |
7231 | reg->id = 0; | |
56f668df | 7232 | } |
57a09bf0 TG |
7233 | } |
7234 | } | |
7235 | ||
c6a9efa1 PC |
7236 | static void __mark_ptr_or_null_regs(struct bpf_func_state *state, u32 id, |
7237 | bool is_null) | |
7238 | { | |
7239 | struct bpf_reg_state *reg; | |
7240 | int i; | |
7241 | ||
7242 | for (i = 0; i < MAX_BPF_REG; i++) | |
7243 | mark_ptr_or_null_reg(state, &state->regs[i], id, is_null); | |
7244 | ||
7245 | bpf_for_each_spilled_reg(i, state, reg) { | |
7246 | if (!reg) | |
7247 | continue; | |
7248 | mark_ptr_or_null_reg(state, reg, id, is_null); | |
7249 | } | |
7250 | } | |
7251 | ||
57a09bf0 TG |
7252 | /* The logic is similar to find_good_pkt_pointers(), both could eventually |
7253 | * be folded together at some point. | |
7254 | */ | |
840b9615 JS |
7255 | static void mark_ptr_or_null_regs(struct bpf_verifier_state *vstate, u32 regno, |
7256 | bool is_null) | |
57a09bf0 | 7257 | { |
f4d7e40a | 7258 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
c6a9efa1 | 7259 | struct bpf_reg_state *regs = state->regs; |
1b986589 | 7260 | u32 ref_obj_id = regs[regno].ref_obj_id; |
a08dd0da | 7261 | u32 id = regs[regno].id; |
c6a9efa1 | 7262 | int i; |
57a09bf0 | 7263 | |
1b986589 MKL |
7264 | if (ref_obj_id && ref_obj_id == id && is_null) |
7265 | /* regs[regno] is in the " == NULL" branch. | |
7266 | * No one could have freed the reference state before | |
7267 | * doing the NULL check. | |
7268 | */ | |
7269 | WARN_ON_ONCE(release_reference_state(state, id)); | |
fd978bf7 | 7270 | |
c6a9efa1 PC |
7271 | for (i = 0; i <= vstate->curframe; i++) |
7272 | __mark_ptr_or_null_regs(vstate->frame[i], id, is_null); | |
57a09bf0 TG |
7273 | } |
7274 | ||
5beca081 DB |
7275 | static bool try_match_pkt_pointers(const struct bpf_insn *insn, |
7276 | struct bpf_reg_state *dst_reg, | |
7277 | struct bpf_reg_state *src_reg, | |
7278 | struct bpf_verifier_state *this_branch, | |
7279 | struct bpf_verifier_state *other_branch) | |
7280 | { | |
7281 | if (BPF_SRC(insn->code) != BPF_X) | |
7282 | return false; | |
7283 | ||
092ed096 JW |
7284 | /* Pointers are always 64-bit. */ |
7285 | if (BPF_CLASS(insn->code) == BPF_JMP32) | |
7286 | return false; | |
7287 | ||
5beca081 DB |
7288 | switch (BPF_OP(insn->code)) { |
7289 | case BPF_JGT: | |
7290 | if ((dst_reg->type == PTR_TO_PACKET && | |
7291 | src_reg->type == PTR_TO_PACKET_END) || | |
7292 | (dst_reg->type == PTR_TO_PACKET_META && | |
7293 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
7294 | /* pkt_data' > pkt_end, pkt_meta' > pkt_data */ | |
7295 | find_good_pkt_pointers(this_branch, dst_reg, | |
7296 | dst_reg->type, false); | |
7297 | } else if ((dst_reg->type == PTR_TO_PACKET_END && | |
7298 | src_reg->type == PTR_TO_PACKET) || | |
7299 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
7300 | src_reg->type == PTR_TO_PACKET_META)) { | |
7301 | /* pkt_end > pkt_data', pkt_data > pkt_meta' */ | |
7302 | find_good_pkt_pointers(other_branch, src_reg, | |
7303 | src_reg->type, true); | |
7304 | } else { | |
7305 | return false; | |
7306 | } | |
7307 | break; | |
7308 | case BPF_JLT: | |
7309 | if ((dst_reg->type == PTR_TO_PACKET && | |
7310 | src_reg->type == PTR_TO_PACKET_END) || | |
7311 | (dst_reg->type == PTR_TO_PACKET_META && | |
7312 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
7313 | /* pkt_data' < pkt_end, pkt_meta' < pkt_data */ | |
7314 | find_good_pkt_pointers(other_branch, dst_reg, | |
7315 | dst_reg->type, true); | |
7316 | } else if ((dst_reg->type == PTR_TO_PACKET_END && | |
7317 | src_reg->type == PTR_TO_PACKET) || | |
7318 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
7319 | src_reg->type == PTR_TO_PACKET_META)) { | |
7320 | /* pkt_end < pkt_data', pkt_data > pkt_meta' */ | |
7321 | find_good_pkt_pointers(this_branch, src_reg, | |
7322 | src_reg->type, false); | |
7323 | } else { | |
7324 | return false; | |
7325 | } | |
7326 | break; | |
7327 | case BPF_JGE: | |
7328 | if ((dst_reg->type == PTR_TO_PACKET && | |
7329 | src_reg->type == PTR_TO_PACKET_END) || | |
7330 | (dst_reg->type == PTR_TO_PACKET_META && | |
7331 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
7332 | /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */ | |
7333 | find_good_pkt_pointers(this_branch, dst_reg, | |
7334 | dst_reg->type, true); | |
7335 | } else if ((dst_reg->type == PTR_TO_PACKET_END && | |
7336 | src_reg->type == PTR_TO_PACKET) || | |
7337 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
7338 | src_reg->type == PTR_TO_PACKET_META)) { | |
7339 | /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */ | |
7340 | find_good_pkt_pointers(other_branch, src_reg, | |
7341 | src_reg->type, false); | |
7342 | } else { | |
7343 | return false; | |
7344 | } | |
7345 | break; | |
7346 | case BPF_JLE: | |
7347 | if ((dst_reg->type == PTR_TO_PACKET && | |
7348 | src_reg->type == PTR_TO_PACKET_END) || | |
7349 | (dst_reg->type == PTR_TO_PACKET_META && | |
7350 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
7351 | /* pkt_data' <= pkt_end, pkt_meta' <= pkt_data */ | |
7352 | find_good_pkt_pointers(other_branch, dst_reg, | |
7353 | dst_reg->type, false); | |
7354 | } else if ((dst_reg->type == PTR_TO_PACKET_END && | |
7355 | src_reg->type == PTR_TO_PACKET) || | |
7356 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
7357 | src_reg->type == PTR_TO_PACKET_META)) { | |
7358 | /* pkt_end <= pkt_data', pkt_data <= pkt_meta' */ | |
7359 | find_good_pkt_pointers(this_branch, src_reg, | |
7360 | src_reg->type, true); | |
7361 | } else { | |
7362 | return false; | |
7363 | } | |
7364 | break; | |
7365 | default: | |
7366 | return false; | |
7367 | } | |
7368 | ||
7369 | return true; | |
7370 | } | |
7371 | ||
58e2af8b | 7372 | static int check_cond_jmp_op(struct bpf_verifier_env *env, |
17a52670 AS |
7373 | struct bpf_insn *insn, int *insn_idx) |
7374 | { | |
f4d7e40a AS |
7375 | struct bpf_verifier_state *this_branch = env->cur_state; |
7376 | struct bpf_verifier_state *other_branch; | |
7377 | struct bpf_reg_state *regs = this_branch->frame[this_branch->curframe]->regs; | |
fb8d251e | 7378 | struct bpf_reg_state *dst_reg, *other_branch_regs, *src_reg = NULL; |
17a52670 | 7379 | u8 opcode = BPF_OP(insn->code); |
092ed096 | 7380 | bool is_jmp32; |
fb8d251e | 7381 | int pred = -1; |
17a52670 AS |
7382 | int err; |
7383 | ||
092ed096 JW |
7384 | /* Only conditional jumps are expected to reach here. */ |
7385 | if (opcode == BPF_JA || opcode > BPF_JSLE) { | |
7386 | verbose(env, "invalid BPF_JMP/JMP32 opcode %x\n", opcode); | |
17a52670 AS |
7387 | return -EINVAL; |
7388 | } | |
7389 | ||
7390 | if (BPF_SRC(insn->code) == BPF_X) { | |
7391 | if (insn->imm != 0) { | |
092ed096 | 7392 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
17a52670 AS |
7393 | return -EINVAL; |
7394 | } | |
7395 | ||
7396 | /* check src1 operand */ | |
dc503a8a | 7397 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
7398 | if (err) |
7399 | return err; | |
1be7f75d AS |
7400 | |
7401 | if (is_pointer_value(env, insn->src_reg)) { | |
61bd5218 | 7402 | verbose(env, "R%d pointer comparison prohibited\n", |
1be7f75d AS |
7403 | insn->src_reg); |
7404 | return -EACCES; | |
7405 | } | |
fb8d251e | 7406 | src_reg = ®s[insn->src_reg]; |
17a52670 AS |
7407 | } else { |
7408 | if (insn->src_reg != BPF_REG_0) { | |
092ed096 | 7409 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
17a52670 AS |
7410 | return -EINVAL; |
7411 | } | |
7412 | } | |
7413 | ||
7414 | /* check src2 operand */ | |
dc503a8a | 7415 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
7416 | if (err) |
7417 | return err; | |
7418 | ||
1a0dc1ac | 7419 | dst_reg = ®s[insn->dst_reg]; |
092ed096 | 7420 | is_jmp32 = BPF_CLASS(insn->code) == BPF_JMP32; |
1a0dc1ac | 7421 | |
3f50f132 JF |
7422 | if (BPF_SRC(insn->code) == BPF_K) { |
7423 | pred = is_branch_taken(dst_reg, insn->imm, opcode, is_jmp32); | |
7424 | } else if (src_reg->type == SCALAR_VALUE && | |
7425 | is_jmp32 && tnum_is_const(tnum_subreg(src_reg->var_off))) { | |
7426 | pred = is_branch_taken(dst_reg, | |
7427 | tnum_subreg(src_reg->var_off).value, | |
7428 | opcode, | |
7429 | is_jmp32); | |
7430 | } else if (src_reg->type == SCALAR_VALUE && | |
7431 | !is_jmp32 && tnum_is_const(src_reg->var_off)) { | |
7432 | pred = is_branch_taken(dst_reg, | |
7433 | src_reg->var_off.value, | |
7434 | opcode, | |
7435 | is_jmp32); | |
7436 | } | |
7437 | ||
b5dc0163 | 7438 | if (pred >= 0) { |
cac616db JF |
7439 | /* If we get here with a dst_reg pointer type it is because |
7440 | * above is_branch_taken() special cased the 0 comparison. | |
7441 | */ | |
7442 | if (!__is_pointer_value(false, dst_reg)) | |
7443 | err = mark_chain_precision(env, insn->dst_reg); | |
b5dc0163 AS |
7444 | if (BPF_SRC(insn->code) == BPF_X && !err) |
7445 | err = mark_chain_precision(env, insn->src_reg); | |
7446 | if (err) | |
7447 | return err; | |
7448 | } | |
fb8d251e AS |
7449 | if (pred == 1) { |
7450 | /* only follow the goto, ignore fall-through */ | |
7451 | *insn_idx += insn->off; | |
7452 | return 0; | |
7453 | } else if (pred == 0) { | |
7454 | /* only follow fall-through branch, since | |
7455 | * that's where the program will go | |
7456 | */ | |
7457 | return 0; | |
17a52670 AS |
7458 | } |
7459 | ||
979d63d5 DB |
7460 | other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx, |
7461 | false); | |
17a52670 AS |
7462 | if (!other_branch) |
7463 | return -EFAULT; | |
f4d7e40a | 7464 | other_branch_regs = other_branch->frame[other_branch->curframe]->regs; |
17a52670 | 7465 | |
48461135 JB |
7466 | /* detect if we are comparing against a constant value so we can adjust |
7467 | * our min/max values for our dst register. | |
f1174f77 EC |
7468 | * this is only legit if both are scalars (or pointers to the same |
7469 | * object, I suppose, but we don't support that right now), because | |
7470 | * otherwise the different base pointers mean the offsets aren't | |
7471 | * comparable. | |
48461135 JB |
7472 | */ |
7473 | if (BPF_SRC(insn->code) == BPF_X) { | |
092ed096 | 7474 | struct bpf_reg_state *src_reg = ®s[insn->src_reg]; |
092ed096 | 7475 | |
f1174f77 | 7476 | if (dst_reg->type == SCALAR_VALUE && |
092ed096 JW |
7477 | src_reg->type == SCALAR_VALUE) { |
7478 | if (tnum_is_const(src_reg->var_off) || | |
3f50f132 JF |
7479 | (is_jmp32 && |
7480 | tnum_is_const(tnum_subreg(src_reg->var_off)))) | |
f4d7e40a | 7481 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
092ed096 | 7482 | dst_reg, |
3f50f132 JF |
7483 | src_reg->var_off.value, |
7484 | tnum_subreg(src_reg->var_off).value, | |
092ed096 JW |
7485 | opcode, is_jmp32); |
7486 | else if (tnum_is_const(dst_reg->var_off) || | |
3f50f132 JF |
7487 | (is_jmp32 && |
7488 | tnum_is_const(tnum_subreg(dst_reg->var_off)))) | |
f4d7e40a | 7489 | reg_set_min_max_inv(&other_branch_regs[insn->src_reg], |
092ed096 | 7490 | src_reg, |
3f50f132 JF |
7491 | dst_reg->var_off.value, |
7492 | tnum_subreg(dst_reg->var_off).value, | |
092ed096 JW |
7493 | opcode, is_jmp32); |
7494 | else if (!is_jmp32 && | |
7495 | (opcode == BPF_JEQ || opcode == BPF_JNE)) | |
f1174f77 | 7496 | /* Comparing for equality, we can combine knowledge */ |
f4d7e40a AS |
7497 | reg_combine_min_max(&other_branch_regs[insn->src_reg], |
7498 | &other_branch_regs[insn->dst_reg], | |
092ed096 | 7499 | src_reg, dst_reg, opcode); |
f1174f77 EC |
7500 | } |
7501 | } else if (dst_reg->type == SCALAR_VALUE) { | |
f4d7e40a | 7502 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
3f50f132 JF |
7503 | dst_reg, insn->imm, (u32)insn->imm, |
7504 | opcode, is_jmp32); | |
48461135 JB |
7505 | } |
7506 | ||
092ed096 JW |
7507 | /* detect if R == 0 where R is returned from bpf_map_lookup_elem(). |
7508 | * NOTE: these optimizations below are related with pointer comparison | |
7509 | * which will never be JMP32. | |
7510 | */ | |
7511 | if (!is_jmp32 && BPF_SRC(insn->code) == BPF_K && | |
1a0dc1ac | 7512 | insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) && |
840b9615 JS |
7513 | reg_type_may_be_null(dst_reg->type)) { |
7514 | /* Mark all identical registers in each branch as either | |
57a09bf0 TG |
7515 | * safe or unknown depending R == 0 or R != 0 conditional. |
7516 | */ | |
840b9615 JS |
7517 | mark_ptr_or_null_regs(this_branch, insn->dst_reg, |
7518 | opcode == BPF_JNE); | |
7519 | mark_ptr_or_null_regs(other_branch, insn->dst_reg, | |
7520 | opcode == BPF_JEQ); | |
5beca081 DB |
7521 | } else if (!try_match_pkt_pointers(insn, dst_reg, ®s[insn->src_reg], |
7522 | this_branch, other_branch) && | |
7523 | is_pointer_value(env, insn->dst_reg)) { | |
61bd5218 JK |
7524 | verbose(env, "R%d pointer comparison prohibited\n", |
7525 | insn->dst_reg); | |
1be7f75d | 7526 | return -EACCES; |
17a52670 | 7527 | } |
06ee7115 | 7528 | if (env->log.level & BPF_LOG_LEVEL) |
f4d7e40a | 7529 | print_verifier_state(env, this_branch->frame[this_branch->curframe]); |
17a52670 AS |
7530 | return 0; |
7531 | } | |
7532 | ||
17a52670 | 7533 | /* verify BPF_LD_IMM64 instruction */ |
58e2af8b | 7534 | static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn) |
17a52670 | 7535 | { |
d8eca5bb | 7536 | struct bpf_insn_aux_data *aux = cur_aux(env); |
638f5b90 | 7537 | struct bpf_reg_state *regs = cur_regs(env); |
4976b718 | 7538 | struct bpf_reg_state *dst_reg; |
d8eca5bb | 7539 | struct bpf_map *map; |
17a52670 AS |
7540 | int err; |
7541 | ||
7542 | if (BPF_SIZE(insn->code) != BPF_DW) { | |
61bd5218 | 7543 | verbose(env, "invalid BPF_LD_IMM insn\n"); |
17a52670 AS |
7544 | return -EINVAL; |
7545 | } | |
7546 | if (insn->off != 0) { | |
61bd5218 | 7547 | verbose(env, "BPF_LD_IMM64 uses reserved fields\n"); |
17a52670 AS |
7548 | return -EINVAL; |
7549 | } | |
7550 | ||
dc503a8a | 7551 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
17a52670 AS |
7552 | if (err) |
7553 | return err; | |
7554 | ||
4976b718 | 7555 | dst_reg = ®s[insn->dst_reg]; |
6b173873 | 7556 | if (insn->src_reg == 0) { |
6b173873 JK |
7557 | u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm; |
7558 | ||
4976b718 | 7559 | dst_reg->type = SCALAR_VALUE; |
b03c9f9f | 7560 | __mark_reg_known(®s[insn->dst_reg], imm); |
17a52670 | 7561 | return 0; |
6b173873 | 7562 | } |
17a52670 | 7563 | |
4976b718 HL |
7564 | if (insn->src_reg == BPF_PSEUDO_BTF_ID) { |
7565 | mark_reg_known_zero(env, regs, insn->dst_reg); | |
7566 | ||
7567 | dst_reg->type = aux->btf_var.reg_type; | |
7568 | switch (dst_reg->type) { | |
7569 | case PTR_TO_MEM: | |
7570 | dst_reg->mem_size = aux->btf_var.mem_size; | |
7571 | break; | |
7572 | case PTR_TO_BTF_ID: | |
eaa6bcb7 | 7573 | case PTR_TO_PERCPU_BTF_ID: |
4976b718 HL |
7574 | dst_reg->btf_id = aux->btf_var.btf_id; |
7575 | break; | |
7576 | default: | |
7577 | verbose(env, "bpf verifier is misconfigured\n"); | |
7578 | return -EFAULT; | |
7579 | } | |
7580 | return 0; | |
7581 | } | |
7582 | ||
d8eca5bb DB |
7583 | map = env->used_maps[aux->map_index]; |
7584 | mark_reg_known_zero(env, regs, insn->dst_reg); | |
4976b718 | 7585 | dst_reg->map_ptr = map; |
d8eca5bb DB |
7586 | |
7587 | if (insn->src_reg == BPF_PSEUDO_MAP_VALUE) { | |
4976b718 HL |
7588 | dst_reg->type = PTR_TO_MAP_VALUE; |
7589 | dst_reg->off = aux->map_off; | |
d8eca5bb | 7590 | if (map_value_has_spin_lock(map)) |
4976b718 | 7591 | dst_reg->id = ++env->id_gen; |
d8eca5bb | 7592 | } else if (insn->src_reg == BPF_PSEUDO_MAP_FD) { |
4976b718 | 7593 | dst_reg->type = CONST_PTR_TO_MAP; |
d8eca5bb DB |
7594 | } else { |
7595 | verbose(env, "bpf verifier is misconfigured\n"); | |
7596 | return -EINVAL; | |
7597 | } | |
17a52670 | 7598 | |
17a52670 AS |
7599 | return 0; |
7600 | } | |
7601 | ||
96be4325 DB |
7602 | static bool may_access_skb(enum bpf_prog_type type) |
7603 | { | |
7604 | switch (type) { | |
7605 | case BPF_PROG_TYPE_SOCKET_FILTER: | |
7606 | case BPF_PROG_TYPE_SCHED_CLS: | |
94caee8c | 7607 | case BPF_PROG_TYPE_SCHED_ACT: |
96be4325 DB |
7608 | return true; |
7609 | default: | |
7610 | return false; | |
7611 | } | |
7612 | } | |
7613 | ||
ddd872bc AS |
7614 | /* verify safety of LD_ABS|LD_IND instructions: |
7615 | * - they can only appear in the programs where ctx == skb | |
7616 | * - since they are wrappers of function calls, they scratch R1-R5 registers, | |
7617 | * preserve R6-R9, and store return value into R0 | |
7618 | * | |
7619 | * Implicit input: | |
7620 | * ctx == skb == R6 == CTX | |
7621 | * | |
7622 | * Explicit input: | |
7623 | * SRC == any register | |
7624 | * IMM == 32-bit immediate | |
7625 | * | |
7626 | * Output: | |
7627 | * R0 - 8/16/32-bit skb data converted to cpu endianness | |
7628 | */ | |
58e2af8b | 7629 | static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn) |
ddd872bc | 7630 | { |
638f5b90 | 7631 | struct bpf_reg_state *regs = cur_regs(env); |
6d4f151a | 7632 | static const int ctx_reg = BPF_REG_6; |
ddd872bc | 7633 | u8 mode = BPF_MODE(insn->code); |
ddd872bc AS |
7634 | int i, err; |
7635 | ||
7e40781c | 7636 | if (!may_access_skb(resolve_prog_type(env->prog))) { |
61bd5218 | 7637 | verbose(env, "BPF_LD_[ABS|IND] instructions not allowed for this program type\n"); |
ddd872bc AS |
7638 | return -EINVAL; |
7639 | } | |
7640 | ||
e0cea7ce DB |
7641 | if (!env->ops->gen_ld_abs) { |
7642 | verbose(env, "bpf verifier is misconfigured\n"); | |
7643 | return -EINVAL; | |
7644 | } | |
7645 | ||
ddd872bc | 7646 | if (insn->dst_reg != BPF_REG_0 || insn->off != 0 || |
d82bccc6 | 7647 | BPF_SIZE(insn->code) == BPF_DW || |
ddd872bc | 7648 | (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) { |
61bd5218 | 7649 | verbose(env, "BPF_LD_[ABS|IND] uses reserved fields\n"); |
ddd872bc AS |
7650 | return -EINVAL; |
7651 | } | |
7652 | ||
7653 | /* check whether implicit source operand (register R6) is readable */ | |
6d4f151a | 7654 | err = check_reg_arg(env, ctx_reg, SRC_OP); |
ddd872bc AS |
7655 | if (err) |
7656 | return err; | |
7657 | ||
fd978bf7 JS |
7658 | /* Disallow usage of BPF_LD_[ABS|IND] with reference tracking, as |
7659 | * gen_ld_abs() may terminate the program at runtime, leading to | |
7660 | * reference leak. | |
7661 | */ | |
7662 | err = check_reference_leak(env); | |
7663 | if (err) { | |
7664 | verbose(env, "BPF_LD_[ABS|IND] cannot be mixed with socket references\n"); | |
7665 | return err; | |
7666 | } | |
7667 | ||
d83525ca AS |
7668 | if (env->cur_state->active_spin_lock) { |
7669 | verbose(env, "BPF_LD_[ABS|IND] cannot be used inside bpf_spin_lock-ed region\n"); | |
7670 | return -EINVAL; | |
7671 | } | |
7672 | ||
6d4f151a | 7673 | if (regs[ctx_reg].type != PTR_TO_CTX) { |
61bd5218 JK |
7674 | verbose(env, |
7675 | "at the time of BPF_LD_ABS|IND R6 != pointer to skb\n"); | |
ddd872bc AS |
7676 | return -EINVAL; |
7677 | } | |
7678 | ||
7679 | if (mode == BPF_IND) { | |
7680 | /* check explicit source operand */ | |
dc503a8a | 7681 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
ddd872bc AS |
7682 | if (err) |
7683 | return err; | |
7684 | } | |
7685 | ||
6d4f151a DB |
7686 | err = check_ctx_reg(env, ®s[ctx_reg], ctx_reg); |
7687 | if (err < 0) | |
7688 | return err; | |
7689 | ||
ddd872bc | 7690 | /* reset caller saved regs to unreadable */ |
dc503a8a | 7691 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
61bd5218 | 7692 | mark_reg_not_init(env, regs, caller_saved[i]); |
dc503a8a EC |
7693 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
7694 | } | |
ddd872bc AS |
7695 | |
7696 | /* mark destination R0 register as readable, since it contains | |
dc503a8a EC |
7697 | * the value fetched from the packet. |
7698 | * Already marked as written above. | |
ddd872bc | 7699 | */ |
61bd5218 | 7700 | mark_reg_unknown(env, regs, BPF_REG_0); |
5327ed3d JW |
7701 | /* ld_abs load up to 32-bit skb data. */ |
7702 | regs[BPF_REG_0].subreg_def = env->insn_idx + 1; | |
ddd872bc AS |
7703 | return 0; |
7704 | } | |
7705 | ||
390ee7e2 AS |
7706 | static int check_return_code(struct bpf_verifier_env *env) |
7707 | { | |
5cf1e914 | 7708 | struct tnum enforce_attach_type_range = tnum_unknown; |
27ae7997 | 7709 | const struct bpf_prog *prog = env->prog; |
390ee7e2 AS |
7710 | struct bpf_reg_state *reg; |
7711 | struct tnum range = tnum_range(0, 1); | |
7e40781c | 7712 | enum bpf_prog_type prog_type = resolve_prog_type(env->prog); |
27ae7997 MKL |
7713 | int err; |
7714 | ||
9e4e01df | 7715 | /* LSM and struct_ops func-ptr's return type could be "void" */ |
7e40781c UP |
7716 | if ((prog_type == BPF_PROG_TYPE_STRUCT_OPS || |
7717 | prog_type == BPF_PROG_TYPE_LSM) && | |
27ae7997 MKL |
7718 | !prog->aux->attach_func_proto->type) |
7719 | return 0; | |
7720 | ||
7721 | /* eBPF calling convetion is such that R0 is used | |
7722 | * to return the value from eBPF program. | |
7723 | * Make sure that it's readable at this time | |
7724 | * of bpf_exit, which means that program wrote | |
7725 | * something into it earlier | |
7726 | */ | |
7727 | err = check_reg_arg(env, BPF_REG_0, SRC_OP); | |
7728 | if (err) | |
7729 | return err; | |
7730 | ||
7731 | if (is_pointer_value(env, BPF_REG_0)) { | |
7732 | verbose(env, "R0 leaks addr as return value\n"); | |
7733 | return -EACCES; | |
7734 | } | |
390ee7e2 | 7735 | |
7e40781c | 7736 | switch (prog_type) { |
983695fa DB |
7737 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
7738 | if (env->prog->expected_attach_type == BPF_CGROUP_UDP4_RECVMSG || | |
1b66d253 DB |
7739 | env->prog->expected_attach_type == BPF_CGROUP_UDP6_RECVMSG || |
7740 | env->prog->expected_attach_type == BPF_CGROUP_INET4_GETPEERNAME || | |
7741 | env->prog->expected_attach_type == BPF_CGROUP_INET6_GETPEERNAME || | |
7742 | env->prog->expected_attach_type == BPF_CGROUP_INET4_GETSOCKNAME || | |
7743 | env->prog->expected_attach_type == BPF_CGROUP_INET6_GETSOCKNAME) | |
983695fa | 7744 | range = tnum_range(1, 1); |
ed4ed404 | 7745 | break; |
390ee7e2 | 7746 | case BPF_PROG_TYPE_CGROUP_SKB: |
5cf1e914 | 7747 | if (env->prog->expected_attach_type == BPF_CGROUP_INET_EGRESS) { |
7748 | range = tnum_range(0, 3); | |
7749 | enforce_attach_type_range = tnum_range(2, 3); | |
7750 | } | |
ed4ed404 | 7751 | break; |
390ee7e2 AS |
7752 | case BPF_PROG_TYPE_CGROUP_SOCK: |
7753 | case BPF_PROG_TYPE_SOCK_OPS: | |
ebc614f6 | 7754 | case BPF_PROG_TYPE_CGROUP_DEVICE: |
7b146ceb | 7755 | case BPF_PROG_TYPE_CGROUP_SYSCTL: |
0d01da6a | 7756 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: |
390ee7e2 | 7757 | break; |
15ab09bd AS |
7758 | case BPF_PROG_TYPE_RAW_TRACEPOINT: |
7759 | if (!env->prog->aux->attach_btf_id) | |
7760 | return 0; | |
7761 | range = tnum_const(0); | |
7762 | break; | |
15d83c4d | 7763 | case BPF_PROG_TYPE_TRACING: |
e92888c7 YS |
7764 | switch (env->prog->expected_attach_type) { |
7765 | case BPF_TRACE_FENTRY: | |
7766 | case BPF_TRACE_FEXIT: | |
7767 | range = tnum_const(0); | |
7768 | break; | |
7769 | case BPF_TRACE_RAW_TP: | |
7770 | case BPF_MODIFY_RETURN: | |
15d83c4d | 7771 | return 0; |
2ec0616e DB |
7772 | case BPF_TRACE_ITER: |
7773 | break; | |
e92888c7 YS |
7774 | default: |
7775 | return -ENOTSUPP; | |
7776 | } | |
15d83c4d | 7777 | break; |
e9ddbb77 JS |
7778 | case BPF_PROG_TYPE_SK_LOOKUP: |
7779 | range = tnum_range(SK_DROP, SK_PASS); | |
7780 | break; | |
e92888c7 YS |
7781 | case BPF_PROG_TYPE_EXT: |
7782 | /* freplace program can return anything as its return value | |
7783 | * depends on the to-be-replaced kernel func or bpf program. | |
7784 | */ | |
390ee7e2 AS |
7785 | default: |
7786 | return 0; | |
7787 | } | |
7788 | ||
638f5b90 | 7789 | reg = cur_regs(env) + BPF_REG_0; |
390ee7e2 | 7790 | if (reg->type != SCALAR_VALUE) { |
61bd5218 | 7791 | verbose(env, "At program exit the register R0 is not a known value (%s)\n", |
390ee7e2 AS |
7792 | reg_type_str[reg->type]); |
7793 | return -EINVAL; | |
7794 | } | |
7795 | ||
7796 | if (!tnum_in(range, reg->var_off)) { | |
5cf1e914 | 7797 | char tn_buf[48]; |
7798 | ||
61bd5218 | 7799 | verbose(env, "At program exit the register R0 "); |
390ee7e2 | 7800 | if (!tnum_is_unknown(reg->var_off)) { |
390ee7e2 | 7801 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
61bd5218 | 7802 | verbose(env, "has value %s", tn_buf); |
390ee7e2 | 7803 | } else { |
61bd5218 | 7804 | verbose(env, "has unknown scalar value"); |
390ee7e2 | 7805 | } |
5cf1e914 | 7806 | tnum_strn(tn_buf, sizeof(tn_buf), range); |
983695fa | 7807 | verbose(env, " should have been in %s\n", tn_buf); |
390ee7e2 AS |
7808 | return -EINVAL; |
7809 | } | |
5cf1e914 | 7810 | |
7811 | if (!tnum_is_unknown(enforce_attach_type_range) && | |
7812 | tnum_in(enforce_attach_type_range, reg->var_off)) | |
7813 | env->prog->enforce_expected_attach_type = 1; | |
390ee7e2 AS |
7814 | return 0; |
7815 | } | |
7816 | ||
475fb78f AS |
7817 | /* non-recursive DFS pseudo code |
7818 | * 1 procedure DFS-iterative(G,v): | |
7819 | * 2 label v as discovered | |
7820 | * 3 let S be a stack | |
7821 | * 4 S.push(v) | |
7822 | * 5 while S is not empty | |
7823 | * 6 t <- S.pop() | |
7824 | * 7 if t is what we're looking for: | |
7825 | * 8 return t | |
7826 | * 9 for all edges e in G.adjacentEdges(t) do | |
7827 | * 10 if edge e is already labelled | |
7828 | * 11 continue with the next edge | |
7829 | * 12 w <- G.adjacentVertex(t,e) | |
7830 | * 13 if vertex w is not discovered and not explored | |
7831 | * 14 label e as tree-edge | |
7832 | * 15 label w as discovered | |
7833 | * 16 S.push(w) | |
7834 | * 17 continue at 5 | |
7835 | * 18 else if vertex w is discovered | |
7836 | * 19 label e as back-edge | |
7837 | * 20 else | |
7838 | * 21 // vertex w is explored | |
7839 | * 22 label e as forward- or cross-edge | |
7840 | * 23 label t as explored | |
7841 | * 24 S.pop() | |
7842 | * | |
7843 | * convention: | |
7844 | * 0x10 - discovered | |
7845 | * 0x11 - discovered and fall-through edge labelled | |
7846 | * 0x12 - discovered and fall-through and branch edges labelled | |
7847 | * 0x20 - explored | |
7848 | */ | |
7849 | ||
7850 | enum { | |
7851 | DISCOVERED = 0x10, | |
7852 | EXPLORED = 0x20, | |
7853 | FALLTHROUGH = 1, | |
7854 | BRANCH = 2, | |
7855 | }; | |
7856 | ||
dc2a4ebc AS |
7857 | static u32 state_htab_size(struct bpf_verifier_env *env) |
7858 | { | |
7859 | return env->prog->len; | |
7860 | } | |
7861 | ||
5d839021 AS |
7862 | static struct bpf_verifier_state_list **explored_state( |
7863 | struct bpf_verifier_env *env, | |
7864 | int idx) | |
7865 | { | |
dc2a4ebc AS |
7866 | struct bpf_verifier_state *cur = env->cur_state; |
7867 | struct bpf_func_state *state = cur->frame[cur->curframe]; | |
7868 | ||
7869 | return &env->explored_states[(idx ^ state->callsite) % state_htab_size(env)]; | |
5d839021 AS |
7870 | } |
7871 | ||
7872 | static void init_explored_state(struct bpf_verifier_env *env, int idx) | |
7873 | { | |
a8f500af | 7874 | env->insn_aux_data[idx].prune_point = true; |
5d839021 | 7875 | } |
f1bca824 | 7876 | |
475fb78f AS |
7877 | /* t, w, e - match pseudo-code above: |
7878 | * t - index of current instruction | |
7879 | * w - next instruction | |
7880 | * e - edge | |
7881 | */ | |
2589726d AS |
7882 | static int push_insn(int t, int w, int e, struct bpf_verifier_env *env, |
7883 | bool loop_ok) | |
475fb78f | 7884 | { |
7df737e9 AS |
7885 | int *insn_stack = env->cfg.insn_stack; |
7886 | int *insn_state = env->cfg.insn_state; | |
7887 | ||
475fb78f AS |
7888 | if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH)) |
7889 | return 0; | |
7890 | ||
7891 | if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH)) | |
7892 | return 0; | |
7893 | ||
7894 | if (w < 0 || w >= env->prog->len) { | |
d9762e84 | 7895 | verbose_linfo(env, t, "%d: ", t); |
61bd5218 | 7896 | verbose(env, "jump out of range from insn %d to %d\n", t, w); |
475fb78f AS |
7897 | return -EINVAL; |
7898 | } | |
7899 | ||
f1bca824 AS |
7900 | if (e == BRANCH) |
7901 | /* mark branch target for state pruning */ | |
5d839021 | 7902 | init_explored_state(env, w); |
f1bca824 | 7903 | |
475fb78f AS |
7904 | if (insn_state[w] == 0) { |
7905 | /* tree-edge */ | |
7906 | insn_state[t] = DISCOVERED | e; | |
7907 | insn_state[w] = DISCOVERED; | |
7df737e9 | 7908 | if (env->cfg.cur_stack >= env->prog->len) |
475fb78f | 7909 | return -E2BIG; |
7df737e9 | 7910 | insn_stack[env->cfg.cur_stack++] = w; |
475fb78f AS |
7911 | return 1; |
7912 | } else if ((insn_state[w] & 0xF0) == DISCOVERED) { | |
2c78ee89 | 7913 | if (loop_ok && env->bpf_capable) |
2589726d | 7914 | return 0; |
d9762e84 MKL |
7915 | verbose_linfo(env, t, "%d: ", t); |
7916 | verbose_linfo(env, w, "%d: ", w); | |
61bd5218 | 7917 | verbose(env, "back-edge from insn %d to %d\n", t, w); |
475fb78f AS |
7918 | return -EINVAL; |
7919 | } else if (insn_state[w] == EXPLORED) { | |
7920 | /* forward- or cross-edge */ | |
7921 | insn_state[t] = DISCOVERED | e; | |
7922 | } else { | |
61bd5218 | 7923 | verbose(env, "insn state internal bug\n"); |
475fb78f AS |
7924 | return -EFAULT; |
7925 | } | |
7926 | return 0; | |
7927 | } | |
7928 | ||
7929 | /* non-recursive depth-first-search to detect loops in BPF program | |
7930 | * loop == back-edge in directed graph | |
7931 | */ | |
58e2af8b | 7932 | static int check_cfg(struct bpf_verifier_env *env) |
475fb78f AS |
7933 | { |
7934 | struct bpf_insn *insns = env->prog->insnsi; | |
7935 | int insn_cnt = env->prog->len; | |
7df737e9 | 7936 | int *insn_stack, *insn_state; |
475fb78f AS |
7937 | int ret = 0; |
7938 | int i, t; | |
7939 | ||
7df737e9 | 7940 | insn_state = env->cfg.insn_state = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
475fb78f AS |
7941 | if (!insn_state) |
7942 | return -ENOMEM; | |
7943 | ||
7df737e9 | 7944 | insn_stack = env->cfg.insn_stack = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
475fb78f | 7945 | if (!insn_stack) { |
71dde681 | 7946 | kvfree(insn_state); |
475fb78f AS |
7947 | return -ENOMEM; |
7948 | } | |
7949 | ||
7950 | insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */ | |
7951 | insn_stack[0] = 0; /* 0 is the first instruction */ | |
7df737e9 | 7952 | env->cfg.cur_stack = 1; |
475fb78f AS |
7953 | |
7954 | peek_stack: | |
7df737e9 | 7955 | if (env->cfg.cur_stack == 0) |
475fb78f | 7956 | goto check_state; |
7df737e9 | 7957 | t = insn_stack[env->cfg.cur_stack - 1]; |
475fb78f | 7958 | |
092ed096 JW |
7959 | if (BPF_CLASS(insns[t].code) == BPF_JMP || |
7960 | BPF_CLASS(insns[t].code) == BPF_JMP32) { | |
475fb78f AS |
7961 | u8 opcode = BPF_OP(insns[t].code); |
7962 | ||
7963 | if (opcode == BPF_EXIT) { | |
7964 | goto mark_explored; | |
7965 | } else if (opcode == BPF_CALL) { | |
2589726d | 7966 | ret = push_insn(t, t + 1, FALLTHROUGH, env, false); |
475fb78f AS |
7967 | if (ret == 1) |
7968 | goto peek_stack; | |
7969 | else if (ret < 0) | |
7970 | goto err_free; | |
07016151 | 7971 | if (t + 1 < insn_cnt) |
5d839021 | 7972 | init_explored_state(env, t + 1); |
cc8b0b92 | 7973 | if (insns[t].src_reg == BPF_PSEUDO_CALL) { |
5d839021 | 7974 | init_explored_state(env, t); |
2589726d AS |
7975 | ret = push_insn(t, t + insns[t].imm + 1, BRANCH, |
7976 | env, false); | |
cc8b0b92 AS |
7977 | if (ret == 1) |
7978 | goto peek_stack; | |
7979 | else if (ret < 0) | |
7980 | goto err_free; | |
7981 | } | |
475fb78f AS |
7982 | } else if (opcode == BPF_JA) { |
7983 | if (BPF_SRC(insns[t].code) != BPF_K) { | |
7984 | ret = -EINVAL; | |
7985 | goto err_free; | |
7986 | } | |
7987 | /* unconditional jump with single edge */ | |
7988 | ret = push_insn(t, t + insns[t].off + 1, | |
2589726d | 7989 | FALLTHROUGH, env, true); |
475fb78f AS |
7990 | if (ret == 1) |
7991 | goto peek_stack; | |
7992 | else if (ret < 0) | |
7993 | goto err_free; | |
b5dc0163 AS |
7994 | /* unconditional jmp is not a good pruning point, |
7995 | * but it's marked, since backtracking needs | |
7996 | * to record jmp history in is_state_visited(). | |
7997 | */ | |
7998 | init_explored_state(env, t + insns[t].off + 1); | |
f1bca824 AS |
7999 | /* tell verifier to check for equivalent states |
8000 | * after every call and jump | |
8001 | */ | |
c3de6317 | 8002 | if (t + 1 < insn_cnt) |
5d839021 | 8003 | init_explored_state(env, t + 1); |
475fb78f AS |
8004 | } else { |
8005 | /* conditional jump with two edges */ | |
5d839021 | 8006 | init_explored_state(env, t); |
2589726d | 8007 | ret = push_insn(t, t + 1, FALLTHROUGH, env, true); |
475fb78f AS |
8008 | if (ret == 1) |
8009 | goto peek_stack; | |
8010 | else if (ret < 0) | |
8011 | goto err_free; | |
8012 | ||
2589726d | 8013 | ret = push_insn(t, t + insns[t].off + 1, BRANCH, env, true); |
475fb78f AS |
8014 | if (ret == 1) |
8015 | goto peek_stack; | |
8016 | else if (ret < 0) | |
8017 | goto err_free; | |
8018 | } | |
8019 | } else { | |
8020 | /* all other non-branch instructions with single | |
8021 | * fall-through edge | |
8022 | */ | |
2589726d | 8023 | ret = push_insn(t, t + 1, FALLTHROUGH, env, false); |
475fb78f AS |
8024 | if (ret == 1) |
8025 | goto peek_stack; | |
8026 | else if (ret < 0) | |
8027 | goto err_free; | |
8028 | } | |
8029 | ||
8030 | mark_explored: | |
8031 | insn_state[t] = EXPLORED; | |
7df737e9 | 8032 | if (env->cfg.cur_stack-- <= 0) { |
61bd5218 | 8033 | verbose(env, "pop stack internal bug\n"); |
475fb78f AS |
8034 | ret = -EFAULT; |
8035 | goto err_free; | |
8036 | } | |
8037 | goto peek_stack; | |
8038 | ||
8039 | check_state: | |
8040 | for (i = 0; i < insn_cnt; i++) { | |
8041 | if (insn_state[i] != EXPLORED) { | |
61bd5218 | 8042 | verbose(env, "unreachable insn %d\n", i); |
475fb78f AS |
8043 | ret = -EINVAL; |
8044 | goto err_free; | |
8045 | } | |
8046 | } | |
8047 | ret = 0; /* cfg looks good */ | |
8048 | ||
8049 | err_free: | |
71dde681 AS |
8050 | kvfree(insn_state); |
8051 | kvfree(insn_stack); | |
7df737e9 | 8052 | env->cfg.insn_state = env->cfg.insn_stack = NULL; |
475fb78f AS |
8053 | return ret; |
8054 | } | |
8055 | ||
09b28d76 AS |
8056 | static int check_abnormal_return(struct bpf_verifier_env *env) |
8057 | { | |
8058 | int i; | |
8059 | ||
8060 | for (i = 1; i < env->subprog_cnt; i++) { | |
8061 | if (env->subprog_info[i].has_ld_abs) { | |
8062 | verbose(env, "LD_ABS is not allowed in subprogs without BTF\n"); | |
8063 | return -EINVAL; | |
8064 | } | |
8065 | if (env->subprog_info[i].has_tail_call) { | |
8066 | verbose(env, "tail_call is not allowed in subprogs without BTF\n"); | |
8067 | return -EINVAL; | |
8068 | } | |
8069 | } | |
8070 | return 0; | |
8071 | } | |
8072 | ||
838e9690 YS |
8073 | /* The minimum supported BTF func info size */ |
8074 | #define MIN_BPF_FUNCINFO_SIZE 8 | |
8075 | #define MAX_FUNCINFO_REC_SIZE 252 | |
8076 | ||
c454a46b MKL |
8077 | static int check_btf_func(struct bpf_verifier_env *env, |
8078 | const union bpf_attr *attr, | |
8079 | union bpf_attr __user *uattr) | |
838e9690 | 8080 | { |
09b28d76 | 8081 | const struct btf_type *type, *func_proto, *ret_type; |
d0b2818e | 8082 | u32 i, nfuncs, urec_size, min_size; |
838e9690 | 8083 | u32 krec_size = sizeof(struct bpf_func_info); |
c454a46b | 8084 | struct bpf_func_info *krecord; |
8c1b6e69 | 8085 | struct bpf_func_info_aux *info_aux = NULL; |
c454a46b MKL |
8086 | struct bpf_prog *prog; |
8087 | const struct btf *btf; | |
838e9690 | 8088 | void __user *urecord; |
d0b2818e | 8089 | u32 prev_offset = 0; |
09b28d76 | 8090 | bool scalar_return; |
e7ed83d6 | 8091 | int ret = -ENOMEM; |
838e9690 YS |
8092 | |
8093 | nfuncs = attr->func_info_cnt; | |
09b28d76 AS |
8094 | if (!nfuncs) { |
8095 | if (check_abnormal_return(env)) | |
8096 | return -EINVAL; | |
838e9690 | 8097 | return 0; |
09b28d76 | 8098 | } |
838e9690 YS |
8099 | |
8100 | if (nfuncs != env->subprog_cnt) { | |
8101 | verbose(env, "number of funcs in func_info doesn't match number of subprogs\n"); | |
8102 | return -EINVAL; | |
8103 | } | |
8104 | ||
8105 | urec_size = attr->func_info_rec_size; | |
8106 | if (urec_size < MIN_BPF_FUNCINFO_SIZE || | |
8107 | urec_size > MAX_FUNCINFO_REC_SIZE || | |
8108 | urec_size % sizeof(u32)) { | |
8109 | verbose(env, "invalid func info rec size %u\n", urec_size); | |
8110 | return -EINVAL; | |
8111 | } | |
8112 | ||
c454a46b MKL |
8113 | prog = env->prog; |
8114 | btf = prog->aux->btf; | |
838e9690 YS |
8115 | |
8116 | urecord = u64_to_user_ptr(attr->func_info); | |
8117 | min_size = min_t(u32, krec_size, urec_size); | |
8118 | ||
ba64e7d8 | 8119 | krecord = kvcalloc(nfuncs, krec_size, GFP_KERNEL | __GFP_NOWARN); |
c454a46b MKL |
8120 | if (!krecord) |
8121 | return -ENOMEM; | |
8c1b6e69 AS |
8122 | info_aux = kcalloc(nfuncs, sizeof(*info_aux), GFP_KERNEL | __GFP_NOWARN); |
8123 | if (!info_aux) | |
8124 | goto err_free; | |
ba64e7d8 | 8125 | |
838e9690 YS |
8126 | for (i = 0; i < nfuncs; i++) { |
8127 | ret = bpf_check_uarg_tail_zero(urecord, krec_size, urec_size); | |
8128 | if (ret) { | |
8129 | if (ret == -E2BIG) { | |
8130 | verbose(env, "nonzero tailing record in func info"); | |
8131 | /* set the size kernel expects so loader can zero | |
8132 | * out the rest of the record. | |
8133 | */ | |
8134 | if (put_user(min_size, &uattr->func_info_rec_size)) | |
8135 | ret = -EFAULT; | |
8136 | } | |
c454a46b | 8137 | goto err_free; |
838e9690 YS |
8138 | } |
8139 | ||
ba64e7d8 | 8140 | if (copy_from_user(&krecord[i], urecord, min_size)) { |
838e9690 | 8141 | ret = -EFAULT; |
c454a46b | 8142 | goto err_free; |
838e9690 YS |
8143 | } |
8144 | ||
d30d42e0 | 8145 | /* check insn_off */ |
09b28d76 | 8146 | ret = -EINVAL; |
838e9690 | 8147 | if (i == 0) { |
d30d42e0 | 8148 | if (krecord[i].insn_off) { |
838e9690 | 8149 | verbose(env, |
d30d42e0 MKL |
8150 | "nonzero insn_off %u for the first func info record", |
8151 | krecord[i].insn_off); | |
c454a46b | 8152 | goto err_free; |
838e9690 | 8153 | } |
d30d42e0 | 8154 | } else if (krecord[i].insn_off <= prev_offset) { |
838e9690 YS |
8155 | verbose(env, |
8156 | "same or smaller insn offset (%u) than previous func info record (%u)", | |
d30d42e0 | 8157 | krecord[i].insn_off, prev_offset); |
c454a46b | 8158 | goto err_free; |
838e9690 YS |
8159 | } |
8160 | ||
d30d42e0 | 8161 | if (env->subprog_info[i].start != krecord[i].insn_off) { |
838e9690 | 8162 | verbose(env, "func_info BTF section doesn't match subprog layout in BPF program\n"); |
c454a46b | 8163 | goto err_free; |
838e9690 YS |
8164 | } |
8165 | ||
8166 | /* check type_id */ | |
ba64e7d8 | 8167 | type = btf_type_by_id(btf, krecord[i].type_id); |
51c39bb1 | 8168 | if (!type || !btf_type_is_func(type)) { |
838e9690 | 8169 | verbose(env, "invalid type id %d in func info", |
ba64e7d8 | 8170 | krecord[i].type_id); |
c454a46b | 8171 | goto err_free; |
838e9690 | 8172 | } |
51c39bb1 | 8173 | info_aux[i].linkage = BTF_INFO_VLEN(type->info); |
09b28d76 AS |
8174 | |
8175 | func_proto = btf_type_by_id(btf, type->type); | |
8176 | if (unlikely(!func_proto || !btf_type_is_func_proto(func_proto))) | |
8177 | /* btf_func_check() already verified it during BTF load */ | |
8178 | goto err_free; | |
8179 | ret_type = btf_type_skip_modifiers(btf, func_proto->type, NULL); | |
8180 | scalar_return = | |
8181 | btf_type_is_small_int(ret_type) || btf_type_is_enum(ret_type); | |
8182 | if (i && !scalar_return && env->subprog_info[i].has_ld_abs) { | |
8183 | verbose(env, "LD_ABS is only allowed in functions that return 'int'.\n"); | |
8184 | goto err_free; | |
8185 | } | |
8186 | if (i && !scalar_return && env->subprog_info[i].has_tail_call) { | |
8187 | verbose(env, "tail_call is only allowed in functions that return 'int'.\n"); | |
8188 | goto err_free; | |
8189 | } | |
8190 | ||
d30d42e0 | 8191 | prev_offset = krecord[i].insn_off; |
838e9690 YS |
8192 | urecord += urec_size; |
8193 | } | |
8194 | ||
ba64e7d8 YS |
8195 | prog->aux->func_info = krecord; |
8196 | prog->aux->func_info_cnt = nfuncs; | |
8c1b6e69 | 8197 | prog->aux->func_info_aux = info_aux; |
838e9690 YS |
8198 | return 0; |
8199 | ||
c454a46b | 8200 | err_free: |
ba64e7d8 | 8201 | kvfree(krecord); |
8c1b6e69 | 8202 | kfree(info_aux); |
838e9690 YS |
8203 | return ret; |
8204 | } | |
8205 | ||
ba64e7d8 YS |
8206 | static void adjust_btf_func(struct bpf_verifier_env *env) |
8207 | { | |
8c1b6e69 | 8208 | struct bpf_prog_aux *aux = env->prog->aux; |
ba64e7d8 YS |
8209 | int i; |
8210 | ||
8c1b6e69 | 8211 | if (!aux->func_info) |
ba64e7d8 YS |
8212 | return; |
8213 | ||
8214 | for (i = 0; i < env->subprog_cnt; i++) | |
8c1b6e69 | 8215 | aux->func_info[i].insn_off = env->subprog_info[i].start; |
ba64e7d8 YS |
8216 | } |
8217 | ||
c454a46b MKL |
8218 | #define MIN_BPF_LINEINFO_SIZE (offsetof(struct bpf_line_info, line_col) + \ |
8219 | sizeof(((struct bpf_line_info *)(0))->line_col)) | |
8220 | #define MAX_LINEINFO_REC_SIZE MAX_FUNCINFO_REC_SIZE | |
8221 | ||
8222 | static int check_btf_line(struct bpf_verifier_env *env, | |
8223 | const union bpf_attr *attr, | |
8224 | union bpf_attr __user *uattr) | |
8225 | { | |
8226 | u32 i, s, nr_linfo, ncopy, expected_size, rec_size, prev_offset = 0; | |
8227 | struct bpf_subprog_info *sub; | |
8228 | struct bpf_line_info *linfo; | |
8229 | struct bpf_prog *prog; | |
8230 | const struct btf *btf; | |
8231 | void __user *ulinfo; | |
8232 | int err; | |
8233 | ||
8234 | nr_linfo = attr->line_info_cnt; | |
8235 | if (!nr_linfo) | |
8236 | return 0; | |
8237 | ||
8238 | rec_size = attr->line_info_rec_size; | |
8239 | if (rec_size < MIN_BPF_LINEINFO_SIZE || | |
8240 | rec_size > MAX_LINEINFO_REC_SIZE || | |
8241 | rec_size & (sizeof(u32) - 1)) | |
8242 | return -EINVAL; | |
8243 | ||
8244 | /* Need to zero it in case the userspace may | |
8245 | * pass in a smaller bpf_line_info object. | |
8246 | */ | |
8247 | linfo = kvcalloc(nr_linfo, sizeof(struct bpf_line_info), | |
8248 | GFP_KERNEL | __GFP_NOWARN); | |
8249 | if (!linfo) | |
8250 | return -ENOMEM; | |
8251 | ||
8252 | prog = env->prog; | |
8253 | btf = prog->aux->btf; | |
8254 | ||
8255 | s = 0; | |
8256 | sub = env->subprog_info; | |
8257 | ulinfo = u64_to_user_ptr(attr->line_info); | |
8258 | expected_size = sizeof(struct bpf_line_info); | |
8259 | ncopy = min_t(u32, expected_size, rec_size); | |
8260 | for (i = 0; i < nr_linfo; i++) { | |
8261 | err = bpf_check_uarg_tail_zero(ulinfo, expected_size, rec_size); | |
8262 | if (err) { | |
8263 | if (err == -E2BIG) { | |
8264 | verbose(env, "nonzero tailing record in line_info"); | |
8265 | if (put_user(expected_size, | |
8266 | &uattr->line_info_rec_size)) | |
8267 | err = -EFAULT; | |
8268 | } | |
8269 | goto err_free; | |
8270 | } | |
8271 | ||
8272 | if (copy_from_user(&linfo[i], ulinfo, ncopy)) { | |
8273 | err = -EFAULT; | |
8274 | goto err_free; | |
8275 | } | |
8276 | ||
8277 | /* | |
8278 | * Check insn_off to ensure | |
8279 | * 1) strictly increasing AND | |
8280 | * 2) bounded by prog->len | |
8281 | * | |
8282 | * The linfo[0].insn_off == 0 check logically falls into | |
8283 | * the later "missing bpf_line_info for func..." case | |
8284 | * because the first linfo[0].insn_off must be the | |
8285 | * first sub also and the first sub must have | |
8286 | * subprog_info[0].start == 0. | |
8287 | */ | |
8288 | if ((i && linfo[i].insn_off <= prev_offset) || | |
8289 | linfo[i].insn_off >= prog->len) { | |
8290 | verbose(env, "Invalid line_info[%u].insn_off:%u (prev_offset:%u prog->len:%u)\n", | |
8291 | i, linfo[i].insn_off, prev_offset, | |
8292 | prog->len); | |
8293 | err = -EINVAL; | |
8294 | goto err_free; | |
8295 | } | |
8296 | ||
fdbaa0be MKL |
8297 | if (!prog->insnsi[linfo[i].insn_off].code) { |
8298 | verbose(env, | |
8299 | "Invalid insn code at line_info[%u].insn_off\n", | |
8300 | i); | |
8301 | err = -EINVAL; | |
8302 | goto err_free; | |
8303 | } | |
8304 | ||
23127b33 MKL |
8305 | if (!btf_name_by_offset(btf, linfo[i].line_off) || |
8306 | !btf_name_by_offset(btf, linfo[i].file_name_off)) { | |
c454a46b MKL |
8307 | verbose(env, "Invalid line_info[%u].line_off or .file_name_off\n", i); |
8308 | err = -EINVAL; | |
8309 | goto err_free; | |
8310 | } | |
8311 | ||
8312 | if (s != env->subprog_cnt) { | |
8313 | if (linfo[i].insn_off == sub[s].start) { | |
8314 | sub[s].linfo_idx = i; | |
8315 | s++; | |
8316 | } else if (sub[s].start < linfo[i].insn_off) { | |
8317 | verbose(env, "missing bpf_line_info for func#%u\n", s); | |
8318 | err = -EINVAL; | |
8319 | goto err_free; | |
8320 | } | |
8321 | } | |
8322 | ||
8323 | prev_offset = linfo[i].insn_off; | |
8324 | ulinfo += rec_size; | |
8325 | } | |
8326 | ||
8327 | if (s != env->subprog_cnt) { | |
8328 | verbose(env, "missing bpf_line_info for %u funcs starting from func#%u\n", | |
8329 | env->subprog_cnt - s, s); | |
8330 | err = -EINVAL; | |
8331 | goto err_free; | |
8332 | } | |
8333 | ||
8334 | prog->aux->linfo = linfo; | |
8335 | prog->aux->nr_linfo = nr_linfo; | |
8336 | ||
8337 | return 0; | |
8338 | ||
8339 | err_free: | |
8340 | kvfree(linfo); | |
8341 | return err; | |
8342 | } | |
8343 | ||
8344 | static int check_btf_info(struct bpf_verifier_env *env, | |
8345 | const union bpf_attr *attr, | |
8346 | union bpf_attr __user *uattr) | |
8347 | { | |
8348 | struct btf *btf; | |
8349 | int err; | |
8350 | ||
09b28d76 AS |
8351 | if (!attr->func_info_cnt && !attr->line_info_cnt) { |
8352 | if (check_abnormal_return(env)) | |
8353 | return -EINVAL; | |
c454a46b | 8354 | return 0; |
09b28d76 | 8355 | } |
c454a46b MKL |
8356 | |
8357 | btf = btf_get_by_fd(attr->prog_btf_fd); | |
8358 | if (IS_ERR(btf)) | |
8359 | return PTR_ERR(btf); | |
8360 | env->prog->aux->btf = btf; | |
8361 | ||
8362 | err = check_btf_func(env, attr, uattr); | |
8363 | if (err) | |
8364 | return err; | |
8365 | ||
8366 | err = check_btf_line(env, attr, uattr); | |
8367 | if (err) | |
8368 | return err; | |
8369 | ||
8370 | return 0; | |
ba64e7d8 YS |
8371 | } |
8372 | ||
f1174f77 EC |
8373 | /* check %cur's range satisfies %old's */ |
8374 | static bool range_within(struct bpf_reg_state *old, | |
8375 | struct bpf_reg_state *cur) | |
8376 | { | |
b03c9f9f EC |
8377 | return old->umin_value <= cur->umin_value && |
8378 | old->umax_value >= cur->umax_value && | |
8379 | old->smin_value <= cur->smin_value && | |
8380 | old->smax_value >= cur->smax_value; | |
f1174f77 EC |
8381 | } |
8382 | ||
8383 | /* Maximum number of register states that can exist at once */ | |
8384 | #define ID_MAP_SIZE (MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE) | |
8385 | struct idpair { | |
8386 | u32 old; | |
8387 | u32 cur; | |
8388 | }; | |
8389 | ||
8390 | /* If in the old state two registers had the same id, then they need to have | |
8391 | * the same id in the new state as well. But that id could be different from | |
8392 | * the old state, so we need to track the mapping from old to new ids. | |
8393 | * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent | |
8394 | * regs with old id 5 must also have new id 9 for the new state to be safe. But | |
8395 | * regs with a different old id could still have new id 9, we don't care about | |
8396 | * that. | |
8397 | * So we look through our idmap to see if this old id has been seen before. If | |
8398 | * so, we require the new id to match; otherwise, we add the id pair to the map. | |
969bf05e | 8399 | */ |
f1174f77 | 8400 | static bool check_ids(u32 old_id, u32 cur_id, struct idpair *idmap) |
969bf05e | 8401 | { |
f1174f77 | 8402 | unsigned int i; |
969bf05e | 8403 | |
f1174f77 EC |
8404 | for (i = 0; i < ID_MAP_SIZE; i++) { |
8405 | if (!idmap[i].old) { | |
8406 | /* Reached an empty slot; haven't seen this id before */ | |
8407 | idmap[i].old = old_id; | |
8408 | idmap[i].cur = cur_id; | |
8409 | return true; | |
8410 | } | |
8411 | if (idmap[i].old == old_id) | |
8412 | return idmap[i].cur == cur_id; | |
8413 | } | |
8414 | /* We ran out of idmap slots, which should be impossible */ | |
8415 | WARN_ON_ONCE(1); | |
8416 | return false; | |
8417 | } | |
8418 | ||
9242b5f5 AS |
8419 | static void clean_func_state(struct bpf_verifier_env *env, |
8420 | struct bpf_func_state *st) | |
8421 | { | |
8422 | enum bpf_reg_liveness live; | |
8423 | int i, j; | |
8424 | ||
8425 | for (i = 0; i < BPF_REG_FP; i++) { | |
8426 | live = st->regs[i].live; | |
8427 | /* liveness must not touch this register anymore */ | |
8428 | st->regs[i].live |= REG_LIVE_DONE; | |
8429 | if (!(live & REG_LIVE_READ)) | |
8430 | /* since the register is unused, clear its state | |
8431 | * to make further comparison simpler | |
8432 | */ | |
f54c7898 | 8433 | __mark_reg_not_init(env, &st->regs[i]); |
9242b5f5 AS |
8434 | } |
8435 | ||
8436 | for (i = 0; i < st->allocated_stack / BPF_REG_SIZE; i++) { | |
8437 | live = st->stack[i].spilled_ptr.live; | |
8438 | /* liveness must not touch this stack slot anymore */ | |
8439 | st->stack[i].spilled_ptr.live |= REG_LIVE_DONE; | |
8440 | if (!(live & REG_LIVE_READ)) { | |
f54c7898 | 8441 | __mark_reg_not_init(env, &st->stack[i].spilled_ptr); |
9242b5f5 AS |
8442 | for (j = 0; j < BPF_REG_SIZE; j++) |
8443 | st->stack[i].slot_type[j] = STACK_INVALID; | |
8444 | } | |
8445 | } | |
8446 | } | |
8447 | ||
8448 | static void clean_verifier_state(struct bpf_verifier_env *env, | |
8449 | struct bpf_verifier_state *st) | |
8450 | { | |
8451 | int i; | |
8452 | ||
8453 | if (st->frame[0]->regs[0].live & REG_LIVE_DONE) | |
8454 | /* all regs in this state in all frames were already marked */ | |
8455 | return; | |
8456 | ||
8457 | for (i = 0; i <= st->curframe; i++) | |
8458 | clean_func_state(env, st->frame[i]); | |
8459 | } | |
8460 | ||
8461 | /* the parentage chains form a tree. | |
8462 | * the verifier states are added to state lists at given insn and | |
8463 | * pushed into state stack for future exploration. | |
8464 | * when the verifier reaches bpf_exit insn some of the verifer states | |
8465 | * stored in the state lists have their final liveness state already, | |
8466 | * but a lot of states will get revised from liveness point of view when | |
8467 | * the verifier explores other branches. | |
8468 | * Example: | |
8469 | * 1: r0 = 1 | |
8470 | * 2: if r1 == 100 goto pc+1 | |
8471 | * 3: r0 = 2 | |
8472 | * 4: exit | |
8473 | * when the verifier reaches exit insn the register r0 in the state list of | |
8474 | * insn 2 will be seen as !REG_LIVE_READ. Then the verifier pops the other_branch | |
8475 | * of insn 2 and goes exploring further. At the insn 4 it will walk the | |
8476 | * parentage chain from insn 4 into insn 2 and will mark r0 as REG_LIVE_READ. | |
8477 | * | |
8478 | * Since the verifier pushes the branch states as it sees them while exploring | |
8479 | * the program the condition of walking the branch instruction for the second | |
8480 | * time means that all states below this branch were already explored and | |
8481 | * their final liveness markes are already propagated. | |
8482 | * Hence when the verifier completes the search of state list in is_state_visited() | |
8483 | * we can call this clean_live_states() function to mark all liveness states | |
8484 | * as REG_LIVE_DONE to indicate that 'parent' pointers of 'struct bpf_reg_state' | |
8485 | * will not be used. | |
8486 | * This function also clears the registers and stack for states that !READ | |
8487 | * to simplify state merging. | |
8488 | * | |
8489 | * Important note here that walking the same branch instruction in the callee | |
8490 | * doesn't meant that the states are DONE. The verifier has to compare | |
8491 | * the callsites | |
8492 | */ | |
8493 | static void clean_live_states(struct bpf_verifier_env *env, int insn, | |
8494 | struct bpf_verifier_state *cur) | |
8495 | { | |
8496 | struct bpf_verifier_state_list *sl; | |
8497 | int i; | |
8498 | ||
5d839021 | 8499 | sl = *explored_state(env, insn); |
a8f500af | 8500 | while (sl) { |
2589726d AS |
8501 | if (sl->state.branches) |
8502 | goto next; | |
dc2a4ebc AS |
8503 | if (sl->state.insn_idx != insn || |
8504 | sl->state.curframe != cur->curframe) | |
9242b5f5 AS |
8505 | goto next; |
8506 | for (i = 0; i <= cur->curframe; i++) | |
8507 | if (sl->state.frame[i]->callsite != cur->frame[i]->callsite) | |
8508 | goto next; | |
8509 | clean_verifier_state(env, &sl->state); | |
8510 | next: | |
8511 | sl = sl->next; | |
8512 | } | |
8513 | } | |
8514 | ||
f1174f77 | 8515 | /* Returns true if (rold safe implies rcur safe) */ |
1b688a19 EC |
8516 | static bool regsafe(struct bpf_reg_state *rold, struct bpf_reg_state *rcur, |
8517 | struct idpair *idmap) | |
f1174f77 | 8518 | { |
f4d7e40a AS |
8519 | bool equal; |
8520 | ||
dc503a8a EC |
8521 | if (!(rold->live & REG_LIVE_READ)) |
8522 | /* explored state didn't use this */ | |
8523 | return true; | |
8524 | ||
679c782d | 8525 | equal = memcmp(rold, rcur, offsetof(struct bpf_reg_state, parent)) == 0; |
f4d7e40a AS |
8526 | |
8527 | if (rold->type == PTR_TO_STACK) | |
8528 | /* two stack pointers are equal only if they're pointing to | |
8529 | * the same stack frame, since fp-8 in foo != fp-8 in bar | |
8530 | */ | |
8531 | return equal && rold->frameno == rcur->frameno; | |
8532 | ||
8533 | if (equal) | |
969bf05e AS |
8534 | return true; |
8535 | ||
f1174f77 EC |
8536 | if (rold->type == NOT_INIT) |
8537 | /* explored state can't have used this */ | |
969bf05e | 8538 | return true; |
f1174f77 EC |
8539 | if (rcur->type == NOT_INIT) |
8540 | return false; | |
8541 | switch (rold->type) { | |
8542 | case SCALAR_VALUE: | |
8543 | if (rcur->type == SCALAR_VALUE) { | |
b5dc0163 AS |
8544 | if (!rold->precise && !rcur->precise) |
8545 | return true; | |
f1174f77 EC |
8546 | /* new val must satisfy old val knowledge */ |
8547 | return range_within(rold, rcur) && | |
8548 | tnum_in(rold->var_off, rcur->var_off); | |
8549 | } else { | |
179d1c56 JH |
8550 | /* We're trying to use a pointer in place of a scalar. |
8551 | * Even if the scalar was unbounded, this could lead to | |
8552 | * pointer leaks because scalars are allowed to leak | |
8553 | * while pointers are not. We could make this safe in | |
8554 | * special cases if root is calling us, but it's | |
8555 | * probably not worth the hassle. | |
f1174f77 | 8556 | */ |
179d1c56 | 8557 | return false; |
f1174f77 EC |
8558 | } |
8559 | case PTR_TO_MAP_VALUE: | |
1b688a19 EC |
8560 | /* If the new min/max/var_off satisfy the old ones and |
8561 | * everything else matches, we are OK. | |
d83525ca AS |
8562 | * 'id' is not compared, since it's only used for maps with |
8563 | * bpf_spin_lock inside map element and in such cases if | |
8564 | * the rest of the prog is valid for one map element then | |
8565 | * it's valid for all map elements regardless of the key | |
8566 | * used in bpf_map_lookup() | |
1b688a19 EC |
8567 | */ |
8568 | return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 && | |
8569 | range_within(rold, rcur) && | |
8570 | tnum_in(rold->var_off, rcur->var_off); | |
f1174f77 EC |
8571 | case PTR_TO_MAP_VALUE_OR_NULL: |
8572 | /* a PTR_TO_MAP_VALUE could be safe to use as a | |
8573 | * PTR_TO_MAP_VALUE_OR_NULL into the same map. | |
8574 | * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL- | |
8575 | * checked, doing so could have affected others with the same | |
8576 | * id, and we can't check for that because we lost the id when | |
8577 | * we converted to a PTR_TO_MAP_VALUE. | |
8578 | */ | |
8579 | if (rcur->type != PTR_TO_MAP_VALUE_OR_NULL) | |
8580 | return false; | |
8581 | if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, id))) | |
8582 | return false; | |
8583 | /* Check our ids match any regs they're supposed to */ | |
8584 | return check_ids(rold->id, rcur->id, idmap); | |
de8f3a83 | 8585 | case PTR_TO_PACKET_META: |
f1174f77 | 8586 | case PTR_TO_PACKET: |
de8f3a83 | 8587 | if (rcur->type != rold->type) |
f1174f77 EC |
8588 | return false; |
8589 | /* We must have at least as much range as the old ptr | |
8590 | * did, so that any accesses which were safe before are | |
8591 | * still safe. This is true even if old range < old off, | |
8592 | * since someone could have accessed through (ptr - k), or | |
8593 | * even done ptr -= k in a register, to get a safe access. | |
8594 | */ | |
8595 | if (rold->range > rcur->range) | |
8596 | return false; | |
8597 | /* If the offsets don't match, we can't trust our alignment; | |
8598 | * nor can we be sure that we won't fall out of range. | |
8599 | */ | |
8600 | if (rold->off != rcur->off) | |
8601 | return false; | |
8602 | /* id relations must be preserved */ | |
8603 | if (rold->id && !check_ids(rold->id, rcur->id, idmap)) | |
8604 | return false; | |
8605 | /* new val must satisfy old val knowledge */ | |
8606 | return range_within(rold, rcur) && | |
8607 | tnum_in(rold->var_off, rcur->var_off); | |
8608 | case PTR_TO_CTX: | |
8609 | case CONST_PTR_TO_MAP: | |
f1174f77 | 8610 | case PTR_TO_PACKET_END: |
d58e468b | 8611 | case PTR_TO_FLOW_KEYS: |
c64b7983 JS |
8612 | case PTR_TO_SOCKET: |
8613 | case PTR_TO_SOCKET_OR_NULL: | |
46f8bc92 MKL |
8614 | case PTR_TO_SOCK_COMMON: |
8615 | case PTR_TO_SOCK_COMMON_OR_NULL: | |
655a51e5 MKL |
8616 | case PTR_TO_TCP_SOCK: |
8617 | case PTR_TO_TCP_SOCK_OR_NULL: | |
fada7fdc | 8618 | case PTR_TO_XDP_SOCK: |
f1174f77 EC |
8619 | /* Only valid matches are exact, which memcmp() above |
8620 | * would have accepted | |
8621 | */ | |
8622 | default: | |
8623 | /* Don't know what's going on, just say it's not safe */ | |
8624 | return false; | |
8625 | } | |
969bf05e | 8626 | |
f1174f77 EC |
8627 | /* Shouldn't get here; if we do, say it's not safe */ |
8628 | WARN_ON_ONCE(1); | |
969bf05e AS |
8629 | return false; |
8630 | } | |
8631 | ||
f4d7e40a AS |
8632 | static bool stacksafe(struct bpf_func_state *old, |
8633 | struct bpf_func_state *cur, | |
638f5b90 AS |
8634 | struct idpair *idmap) |
8635 | { | |
8636 | int i, spi; | |
8637 | ||
638f5b90 AS |
8638 | /* walk slots of the explored stack and ignore any additional |
8639 | * slots in the current stack, since explored(safe) state | |
8640 | * didn't use them | |
8641 | */ | |
8642 | for (i = 0; i < old->allocated_stack; i++) { | |
8643 | spi = i / BPF_REG_SIZE; | |
8644 | ||
b233920c AS |
8645 | if (!(old->stack[spi].spilled_ptr.live & REG_LIVE_READ)) { |
8646 | i += BPF_REG_SIZE - 1; | |
cc2b14d5 | 8647 | /* explored state didn't use this */ |
fd05e57b | 8648 | continue; |
b233920c | 8649 | } |
cc2b14d5 | 8650 | |
638f5b90 AS |
8651 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID) |
8652 | continue; | |
19e2dbb7 AS |
8653 | |
8654 | /* explored stack has more populated slots than current stack | |
8655 | * and these slots were used | |
8656 | */ | |
8657 | if (i >= cur->allocated_stack) | |
8658 | return false; | |
8659 | ||
cc2b14d5 AS |
8660 | /* if old state was safe with misc data in the stack |
8661 | * it will be safe with zero-initialized stack. | |
8662 | * The opposite is not true | |
8663 | */ | |
8664 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC && | |
8665 | cur->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_ZERO) | |
8666 | continue; | |
638f5b90 AS |
8667 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] != |
8668 | cur->stack[spi].slot_type[i % BPF_REG_SIZE]) | |
8669 | /* Ex: old explored (safe) state has STACK_SPILL in | |
b8c1a309 | 8670 | * this stack slot, but current has STACK_MISC -> |
638f5b90 AS |
8671 | * this verifier states are not equivalent, |
8672 | * return false to continue verification of this path | |
8673 | */ | |
8674 | return false; | |
8675 | if (i % BPF_REG_SIZE) | |
8676 | continue; | |
8677 | if (old->stack[spi].slot_type[0] != STACK_SPILL) | |
8678 | continue; | |
8679 | if (!regsafe(&old->stack[spi].spilled_ptr, | |
8680 | &cur->stack[spi].spilled_ptr, | |
8681 | idmap)) | |
8682 | /* when explored and current stack slot are both storing | |
8683 | * spilled registers, check that stored pointers types | |
8684 | * are the same as well. | |
8685 | * Ex: explored safe path could have stored | |
8686 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8} | |
8687 | * but current path has stored: | |
8688 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16} | |
8689 | * such verifier states are not equivalent. | |
8690 | * return false to continue verification of this path | |
8691 | */ | |
8692 | return false; | |
8693 | } | |
8694 | return true; | |
8695 | } | |
8696 | ||
fd978bf7 JS |
8697 | static bool refsafe(struct bpf_func_state *old, struct bpf_func_state *cur) |
8698 | { | |
8699 | if (old->acquired_refs != cur->acquired_refs) | |
8700 | return false; | |
8701 | return !memcmp(old->refs, cur->refs, | |
8702 | sizeof(*old->refs) * old->acquired_refs); | |
8703 | } | |
8704 | ||
f1bca824 AS |
8705 | /* compare two verifier states |
8706 | * | |
8707 | * all states stored in state_list are known to be valid, since | |
8708 | * verifier reached 'bpf_exit' instruction through them | |
8709 | * | |
8710 | * this function is called when verifier exploring different branches of | |
8711 | * execution popped from the state stack. If it sees an old state that has | |
8712 | * more strict register state and more strict stack state then this execution | |
8713 | * branch doesn't need to be explored further, since verifier already | |
8714 | * concluded that more strict state leads to valid finish. | |
8715 | * | |
8716 | * Therefore two states are equivalent if register state is more conservative | |
8717 | * and explored stack state is more conservative than the current one. | |
8718 | * Example: | |
8719 | * explored current | |
8720 | * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC) | |
8721 | * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC) | |
8722 | * | |
8723 | * In other words if current stack state (one being explored) has more | |
8724 | * valid slots than old one that already passed validation, it means | |
8725 | * the verifier can stop exploring and conclude that current state is valid too | |
8726 | * | |
8727 | * Similarly with registers. If explored state has register type as invalid | |
8728 | * whereas register type in current state is meaningful, it means that | |
8729 | * the current state will reach 'bpf_exit' instruction safely | |
8730 | */ | |
f4d7e40a AS |
8731 | static bool func_states_equal(struct bpf_func_state *old, |
8732 | struct bpf_func_state *cur) | |
f1bca824 | 8733 | { |
f1174f77 EC |
8734 | struct idpair *idmap; |
8735 | bool ret = false; | |
f1bca824 AS |
8736 | int i; |
8737 | ||
f1174f77 EC |
8738 | idmap = kcalloc(ID_MAP_SIZE, sizeof(struct idpair), GFP_KERNEL); |
8739 | /* If we failed to allocate the idmap, just say it's not safe */ | |
8740 | if (!idmap) | |
1a0dc1ac | 8741 | return false; |
f1174f77 EC |
8742 | |
8743 | for (i = 0; i < MAX_BPF_REG; i++) { | |
1b688a19 | 8744 | if (!regsafe(&old->regs[i], &cur->regs[i], idmap)) |
f1174f77 | 8745 | goto out_free; |
f1bca824 AS |
8746 | } |
8747 | ||
638f5b90 AS |
8748 | if (!stacksafe(old, cur, idmap)) |
8749 | goto out_free; | |
fd978bf7 JS |
8750 | |
8751 | if (!refsafe(old, cur)) | |
8752 | goto out_free; | |
f1174f77 EC |
8753 | ret = true; |
8754 | out_free: | |
8755 | kfree(idmap); | |
8756 | return ret; | |
f1bca824 AS |
8757 | } |
8758 | ||
f4d7e40a AS |
8759 | static bool states_equal(struct bpf_verifier_env *env, |
8760 | struct bpf_verifier_state *old, | |
8761 | struct bpf_verifier_state *cur) | |
8762 | { | |
8763 | int i; | |
8764 | ||
8765 | if (old->curframe != cur->curframe) | |
8766 | return false; | |
8767 | ||
979d63d5 DB |
8768 | /* Verification state from speculative execution simulation |
8769 | * must never prune a non-speculative execution one. | |
8770 | */ | |
8771 | if (old->speculative && !cur->speculative) | |
8772 | return false; | |
8773 | ||
d83525ca AS |
8774 | if (old->active_spin_lock != cur->active_spin_lock) |
8775 | return false; | |
8776 | ||
f4d7e40a AS |
8777 | /* for states to be equal callsites have to be the same |
8778 | * and all frame states need to be equivalent | |
8779 | */ | |
8780 | for (i = 0; i <= old->curframe; i++) { | |
8781 | if (old->frame[i]->callsite != cur->frame[i]->callsite) | |
8782 | return false; | |
8783 | if (!func_states_equal(old->frame[i], cur->frame[i])) | |
8784 | return false; | |
8785 | } | |
8786 | return true; | |
8787 | } | |
8788 | ||
5327ed3d JW |
8789 | /* Return 0 if no propagation happened. Return negative error code if error |
8790 | * happened. Otherwise, return the propagated bit. | |
8791 | */ | |
55e7f3b5 JW |
8792 | static int propagate_liveness_reg(struct bpf_verifier_env *env, |
8793 | struct bpf_reg_state *reg, | |
8794 | struct bpf_reg_state *parent_reg) | |
8795 | { | |
5327ed3d JW |
8796 | u8 parent_flag = parent_reg->live & REG_LIVE_READ; |
8797 | u8 flag = reg->live & REG_LIVE_READ; | |
55e7f3b5 JW |
8798 | int err; |
8799 | ||
5327ed3d JW |
8800 | /* When comes here, read flags of PARENT_REG or REG could be any of |
8801 | * REG_LIVE_READ64, REG_LIVE_READ32, REG_LIVE_NONE. There is no need | |
8802 | * of propagation if PARENT_REG has strongest REG_LIVE_READ64. | |
8803 | */ | |
8804 | if (parent_flag == REG_LIVE_READ64 || | |
8805 | /* Or if there is no read flag from REG. */ | |
8806 | !flag || | |
8807 | /* Or if the read flag from REG is the same as PARENT_REG. */ | |
8808 | parent_flag == flag) | |
55e7f3b5 JW |
8809 | return 0; |
8810 | ||
5327ed3d | 8811 | err = mark_reg_read(env, reg, parent_reg, flag); |
55e7f3b5 JW |
8812 | if (err) |
8813 | return err; | |
8814 | ||
5327ed3d | 8815 | return flag; |
55e7f3b5 JW |
8816 | } |
8817 | ||
8e9cd9ce | 8818 | /* A write screens off any subsequent reads; but write marks come from the |
f4d7e40a AS |
8819 | * straight-line code between a state and its parent. When we arrive at an |
8820 | * equivalent state (jump target or such) we didn't arrive by the straight-line | |
8821 | * code, so read marks in the state must propagate to the parent regardless | |
8822 | * of the state's write marks. That's what 'parent == state->parent' comparison | |
679c782d | 8823 | * in mark_reg_read() is for. |
8e9cd9ce | 8824 | */ |
f4d7e40a AS |
8825 | static int propagate_liveness(struct bpf_verifier_env *env, |
8826 | const struct bpf_verifier_state *vstate, | |
8827 | struct bpf_verifier_state *vparent) | |
dc503a8a | 8828 | { |
3f8cafa4 | 8829 | struct bpf_reg_state *state_reg, *parent_reg; |
f4d7e40a | 8830 | struct bpf_func_state *state, *parent; |
3f8cafa4 | 8831 | int i, frame, err = 0; |
dc503a8a | 8832 | |
f4d7e40a AS |
8833 | if (vparent->curframe != vstate->curframe) { |
8834 | WARN(1, "propagate_live: parent frame %d current frame %d\n", | |
8835 | vparent->curframe, vstate->curframe); | |
8836 | return -EFAULT; | |
8837 | } | |
dc503a8a EC |
8838 | /* Propagate read liveness of registers... */ |
8839 | BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG); | |
83d16312 | 8840 | for (frame = 0; frame <= vstate->curframe; frame++) { |
3f8cafa4 JW |
8841 | parent = vparent->frame[frame]; |
8842 | state = vstate->frame[frame]; | |
8843 | parent_reg = parent->regs; | |
8844 | state_reg = state->regs; | |
83d16312 JK |
8845 | /* We don't need to worry about FP liveness, it's read-only */ |
8846 | for (i = frame < vstate->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) { | |
55e7f3b5 JW |
8847 | err = propagate_liveness_reg(env, &state_reg[i], |
8848 | &parent_reg[i]); | |
5327ed3d | 8849 | if (err < 0) |
3f8cafa4 | 8850 | return err; |
5327ed3d JW |
8851 | if (err == REG_LIVE_READ64) |
8852 | mark_insn_zext(env, &parent_reg[i]); | |
dc503a8a | 8853 | } |
f4d7e40a | 8854 | |
1b04aee7 | 8855 | /* Propagate stack slots. */ |
f4d7e40a AS |
8856 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE && |
8857 | i < parent->allocated_stack / BPF_REG_SIZE; i++) { | |
3f8cafa4 JW |
8858 | parent_reg = &parent->stack[i].spilled_ptr; |
8859 | state_reg = &state->stack[i].spilled_ptr; | |
55e7f3b5 JW |
8860 | err = propagate_liveness_reg(env, state_reg, |
8861 | parent_reg); | |
5327ed3d | 8862 | if (err < 0) |
3f8cafa4 | 8863 | return err; |
dc503a8a EC |
8864 | } |
8865 | } | |
5327ed3d | 8866 | return 0; |
dc503a8a EC |
8867 | } |
8868 | ||
a3ce685d AS |
8869 | /* find precise scalars in the previous equivalent state and |
8870 | * propagate them into the current state | |
8871 | */ | |
8872 | static int propagate_precision(struct bpf_verifier_env *env, | |
8873 | const struct bpf_verifier_state *old) | |
8874 | { | |
8875 | struct bpf_reg_state *state_reg; | |
8876 | struct bpf_func_state *state; | |
8877 | int i, err = 0; | |
8878 | ||
8879 | state = old->frame[old->curframe]; | |
8880 | state_reg = state->regs; | |
8881 | for (i = 0; i < BPF_REG_FP; i++, state_reg++) { | |
8882 | if (state_reg->type != SCALAR_VALUE || | |
8883 | !state_reg->precise) | |
8884 | continue; | |
8885 | if (env->log.level & BPF_LOG_LEVEL2) | |
8886 | verbose(env, "propagating r%d\n", i); | |
8887 | err = mark_chain_precision(env, i); | |
8888 | if (err < 0) | |
8889 | return err; | |
8890 | } | |
8891 | ||
8892 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { | |
8893 | if (state->stack[i].slot_type[0] != STACK_SPILL) | |
8894 | continue; | |
8895 | state_reg = &state->stack[i].spilled_ptr; | |
8896 | if (state_reg->type != SCALAR_VALUE || | |
8897 | !state_reg->precise) | |
8898 | continue; | |
8899 | if (env->log.level & BPF_LOG_LEVEL2) | |
8900 | verbose(env, "propagating fp%d\n", | |
8901 | (-i - 1) * BPF_REG_SIZE); | |
8902 | err = mark_chain_precision_stack(env, i); | |
8903 | if (err < 0) | |
8904 | return err; | |
8905 | } | |
8906 | return 0; | |
8907 | } | |
8908 | ||
2589726d AS |
8909 | static bool states_maybe_looping(struct bpf_verifier_state *old, |
8910 | struct bpf_verifier_state *cur) | |
8911 | { | |
8912 | struct bpf_func_state *fold, *fcur; | |
8913 | int i, fr = cur->curframe; | |
8914 | ||
8915 | if (old->curframe != fr) | |
8916 | return false; | |
8917 | ||
8918 | fold = old->frame[fr]; | |
8919 | fcur = cur->frame[fr]; | |
8920 | for (i = 0; i < MAX_BPF_REG; i++) | |
8921 | if (memcmp(&fold->regs[i], &fcur->regs[i], | |
8922 | offsetof(struct bpf_reg_state, parent))) | |
8923 | return false; | |
8924 | return true; | |
8925 | } | |
8926 | ||
8927 | ||
58e2af8b | 8928 | static int is_state_visited(struct bpf_verifier_env *env, int insn_idx) |
f1bca824 | 8929 | { |
58e2af8b | 8930 | struct bpf_verifier_state_list *new_sl; |
9f4686c4 | 8931 | struct bpf_verifier_state_list *sl, **pprev; |
679c782d | 8932 | struct bpf_verifier_state *cur = env->cur_state, *new; |
ceefbc96 | 8933 | int i, j, err, states_cnt = 0; |
10d274e8 | 8934 | bool add_new_state = env->test_state_freq ? true : false; |
f1bca824 | 8935 | |
b5dc0163 | 8936 | cur->last_insn_idx = env->prev_insn_idx; |
a8f500af | 8937 | if (!env->insn_aux_data[insn_idx].prune_point) |
f1bca824 AS |
8938 | /* this 'insn_idx' instruction wasn't marked, so we will not |
8939 | * be doing state search here | |
8940 | */ | |
8941 | return 0; | |
8942 | ||
2589726d AS |
8943 | /* bpf progs typically have pruning point every 4 instructions |
8944 | * http://vger.kernel.org/bpfconf2019.html#session-1 | |
8945 | * Do not add new state for future pruning if the verifier hasn't seen | |
8946 | * at least 2 jumps and at least 8 instructions. | |
8947 | * This heuristics helps decrease 'total_states' and 'peak_states' metric. | |
8948 | * In tests that amounts to up to 50% reduction into total verifier | |
8949 | * memory consumption and 20% verifier time speedup. | |
8950 | */ | |
8951 | if (env->jmps_processed - env->prev_jmps_processed >= 2 && | |
8952 | env->insn_processed - env->prev_insn_processed >= 8) | |
8953 | add_new_state = true; | |
8954 | ||
a8f500af AS |
8955 | pprev = explored_state(env, insn_idx); |
8956 | sl = *pprev; | |
8957 | ||
9242b5f5 AS |
8958 | clean_live_states(env, insn_idx, cur); |
8959 | ||
a8f500af | 8960 | while (sl) { |
dc2a4ebc AS |
8961 | states_cnt++; |
8962 | if (sl->state.insn_idx != insn_idx) | |
8963 | goto next; | |
2589726d AS |
8964 | if (sl->state.branches) { |
8965 | if (states_maybe_looping(&sl->state, cur) && | |
8966 | states_equal(env, &sl->state, cur)) { | |
8967 | verbose_linfo(env, insn_idx, "; "); | |
8968 | verbose(env, "infinite loop detected at insn %d\n", insn_idx); | |
8969 | return -EINVAL; | |
8970 | } | |
8971 | /* if the verifier is processing a loop, avoid adding new state | |
8972 | * too often, since different loop iterations have distinct | |
8973 | * states and may not help future pruning. | |
8974 | * This threshold shouldn't be too low to make sure that | |
8975 | * a loop with large bound will be rejected quickly. | |
8976 | * The most abusive loop will be: | |
8977 | * r1 += 1 | |
8978 | * if r1 < 1000000 goto pc-2 | |
8979 | * 1M insn_procssed limit / 100 == 10k peak states. | |
8980 | * This threshold shouldn't be too high either, since states | |
8981 | * at the end of the loop are likely to be useful in pruning. | |
8982 | */ | |
8983 | if (env->jmps_processed - env->prev_jmps_processed < 20 && | |
8984 | env->insn_processed - env->prev_insn_processed < 100) | |
8985 | add_new_state = false; | |
8986 | goto miss; | |
8987 | } | |
638f5b90 | 8988 | if (states_equal(env, &sl->state, cur)) { |
9f4686c4 | 8989 | sl->hit_cnt++; |
f1bca824 | 8990 | /* reached equivalent register/stack state, |
dc503a8a EC |
8991 | * prune the search. |
8992 | * Registers read by the continuation are read by us. | |
8e9cd9ce EC |
8993 | * If we have any write marks in env->cur_state, they |
8994 | * will prevent corresponding reads in the continuation | |
8995 | * from reaching our parent (an explored_state). Our | |
8996 | * own state will get the read marks recorded, but | |
8997 | * they'll be immediately forgotten as we're pruning | |
8998 | * this state and will pop a new one. | |
f1bca824 | 8999 | */ |
f4d7e40a | 9000 | err = propagate_liveness(env, &sl->state, cur); |
a3ce685d AS |
9001 | |
9002 | /* if previous state reached the exit with precision and | |
9003 | * current state is equivalent to it (except precsion marks) | |
9004 | * the precision needs to be propagated back in | |
9005 | * the current state. | |
9006 | */ | |
9007 | err = err ? : push_jmp_history(env, cur); | |
9008 | err = err ? : propagate_precision(env, &sl->state); | |
f4d7e40a AS |
9009 | if (err) |
9010 | return err; | |
f1bca824 | 9011 | return 1; |
dc503a8a | 9012 | } |
2589726d AS |
9013 | miss: |
9014 | /* when new state is not going to be added do not increase miss count. | |
9015 | * Otherwise several loop iterations will remove the state | |
9016 | * recorded earlier. The goal of these heuristics is to have | |
9017 | * states from some iterations of the loop (some in the beginning | |
9018 | * and some at the end) to help pruning. | |
9019 | */ | |
9020 | if (add_new_state) | |
9021 | sl->miss_cnt++; | |
9f4686c4 AS |
9022 | /* heuristic to determine whether this state is beneficial |
9023 | * to keep checking from state equivalence point of view. | |
9024 | * Higher numbers increase max_states_per_insn and verification time, | |
9025 | * but do not meaningfully decrease insn_processed. | |
9026 | */ | |
9027 | if (sl->miss_cnt > sl->hit_cnt * 3 + 3) { | |
9028 | /* the state is unlikely to be useful. Remove it to | |
9029 | * speed up verification | |
9030 | */ | |
9031 | *pprev = sl->next; | |
9032 | if (sl->state.frame[0]->regs[0].live & REG_LIVE_DONE) { | |
2589726d AS |
9033 | u32 br = sl->state.branches; |
9034 | ||
9035 | WARN_ONCE(br, | |
9036 | "BUG live_done but branches_to_explore %d\n", | |
9037 | br); | |
9f4686c4 AS |
9038 | free_verifier_state(&sl->state, false); |
9039 | kfree(sl); | |
9040 | env->peak_states--; | |
9041 | } else { | |
9042 | /* cannot free this state, since parentage chain may | |
9043 | * walk it later. Add it for free_list instead to | |
9044 | * be freed at the end of verification | |
9045 | */ | |
9046 | sl->next = env->free_list; | |
9047 | env->free_list = sl; | |
9048 | } | |
9049 | sl = *pprev; | |
9050 | continue; | |
9051 | } | |
dc2a4ebc | 9052 | next: |
9f4686c4 AS |
9053 | pprev = &sl->next; |
9054 | sl = *pprev; | |
f1bca824 AS |
9055 | } |
9056 | ||
06ee7115 AS |
9057 | if (env->max_states_per_insn < states_cnt) |
9058 | env->max_states_per_insn = states_cnt; | |
9059 | ||
2c78ee89 | 9060 | if (!env->bpf_capable && states_cnt > BPF_COMPLEXITY_LIMIT_STATES) |
b5dc0163 | 9061 | return push_jmp_history(env, cur); |
ceefbc96 | 9062 | |
2589726d | 9063 | if (!add_new_state) |
b5dc0163 | 9064 | return push_jmp_history(env, cur); |
ceefbc96 | 9065 | |
2589726d AS |
9066 | /* There were no equivalent states, remember the current one. |
9067 | * Technically the current state is not proven to be safe yet, | |
f4d7e40a | 9068 | * but it will either reach outer most bpf_exit (which means it's safe) |
2589726d | 9069 | * or it will be rejected. When there are no loops the verifier won't be |
f4d7e40a | 9070 | * seeing this tuple (frame[0].callsite, frame[1].callsite, .. insn_idx) |
2589726d AS |
9071 | * again on the way to bpf_exit. |
9072 | * When looping the sl->state.branches will be > 0 and this state | |
9073 | * will not be considered for equivalence until branches == 0. | |
f1bca824 | 9074 | */ |
638f5b90 | 9075 | new_sl = kzalloc(sizeof(struct bpf_verifier_state_list), GFP_KERNEL); |
f1bca824 AS |
9076 | if (!new_sl) |
9077 | return -ENOMEM; | |
06ee7115 AS |
9078 | env->total_states++; |
9079 | env->peak_states++; | |
2589726d AS |
9080 | env->prev_jmps_processed = env->jmps_processed; |
9081 | env->prev_insn_processed = env->insn_processed; | |
f1bca824 AS |
9082 | |
9083 | /* add new state to the head of linked list */ | |
679c782d EC |
9084 | new = &new_sl->state; |
9085 | err = copy_verifier_state(new, cur); | |
1969db47 | 9086 | if (err) { |
679c782d | 9087 | free_verifier_state(new, false); |
1969db47 AS |
9088 | kfree(new_sl); |
9089 | return err; | |
9090 | } | |
dc2a4ebc | 9091 | new->insn_idx = insn_idx; |
2589726d AS |
9092 | WARN_ONCE(new->branches != 1, |
9093 | "BUG is_state_visited:branches_to_explore=%d insn %d\n", new->branches, insn_idx); | |
b5dc0163 | 9094 | |
2589726d | 9095 | cur->parent = new; |
b5dc0163 AS |
9096 | cur->first_insn_idx = insn_idx; |
9097 | clear_jmp_history(cur); | |
5d839021 AS |
9098 | new_sl->next = *explored_state(env, insn_idx); |
9099 | *explored_state(env, insn_idx) = new_sl; | |
7640ead9 JK |
9100 | /* connect new state to parentage chain. Current frame needs all |
9101 | * registers connected. Only r6 - r9 of the callers are alive (pushed | |
9102 | * to the stack implicitly by JITs) so in callers' frames connect just | |
9103 | * r6 - r9 as an optimization. Callers will have r1 - r5 connected to | |
9104 | * the state of the call instruction (with WRITTEN set), and r0 comes | |
9105 | * from callee with its full parentage chain, anyway. | |
9106 | */ | |
8e9cd9ce EC |
9107 | /* clear write marks in current state: the writes we did are not writes |
9108 | * our child did, so they don't screen off its reads from us. | |
9109 | * (There are no read marks in current state, because reads always mark | |
9110 | * their parent and current state never has children yet. Only | |
9111 | * explored_states can get read marks.) | |
9112 | */ | |
eea1c227 AS |
9113 | for (j = 0; j <= cur->curframe; j++) { |
9114 | for (i = j < cur->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) | |
9115 | cur->frame[j]->regs[i].parent = &new->frame[j]->regs[i]; | |
9116 | for (i = 0; i < BPF_REG_FP; i++) | |
9117 | cur->frame[j]->regs[i].live = REG_LIVE_NONE; | |
9118 | } | |
f4d7e40a AS |
9119 | |
9120 | /* all stack frames are accessible from callee, clear them all */ | |
9121 | for (j = 0; j <= cur->curframe; j++) { | |
9122 | struct bpf_func_state *frame = cur->frame[j]; | |
679c782d | 9123 | struct bpf_func_state *newframe = new->frame[j]; |
f4d7e40a | 9124 | |
679c782d | 9125 | for (i = 0; i < frame->allocated_stack / BPF_REG_SIZE; i++) { |
cc2b14d5 | 9126 | frame->stack[i].spilled_ptr.live = REG_LIVE_NONE; |
679c782d EC |
9127 | frame->stack[i].spilled_ptr.parent = |
9128 | &newframe->stack[i].spilled_ptr; | |
9129 | } | |
f4d7e40a | 9130 | } |
f1bca824 AS |
9131 | return 0; |
9132 | } | |
9133 | ||
c64b7983 JS |
9134 | /* Return true if it's OK to have the same insn return a different type. */ |
9135 | static bool reg_type_mismatch_ok(enum bpf_reg_type type) | |
9136 | { | |
9137 | switch (type) { | |
9138 | case PTR_TO_CTX: | |
9139 | case PTR_TO_SOCKET: | |
9140 | case PTR_TO_SOCKET_OR_NULL: | |
46f8bc92 MKL |
9141 | case PTR_TO_SOCK_COMMON: |
9142 | case PTR_TO_SOCK_COMMON_OR_NULL: | |
655a51e5 MKL |
9143 | case PTR_TO_TCP_SOCK: |
9144 | case PTR_TO_TCP_SOCK_OR_NULL: | |
fada7fdc | 9145 | case PTR_TO_XDP_SOCK: |
2a02759e | 9146 | case PTR_TO_BTF_ID: |
b121b341 | 9147 | case PTR_TO_BTF_ID_OR_NULL: |
c64b7983 JS |
9148 | return false; |
9149 | default: | |
9150 | return true; | |
9151 | } | |
9152 | } | |
9153 | ||
9154 | /* If an instruction was previously used with particular pointer types, then we | |
9155 | * need to be careful to avoid cases such as the below, where it may be ok | |
9156 | * for one branch accessing the pointer, but not ok for the other branch: | |
9157 | * | |
9158 | * R1 = sock_ptr | |
9159 | * goto X; | |
9160 | * ... | |
9161 | * R1 = some_other_valid_ptr; | |
9162 | * goto X; | |
9163 | * ... | |
9164 | * R2 = *(u32 *)(R1 + 0); | |
9165 | */ | |
9166 | static bool reg_type_mismatch(enum bpf_reg_type src, enum bpf_reg_type prev) | |
9167 | { | |
9168 | return src != prev && (!reg_type_mismatch_ok(src) || | |
9169 | !reg_type_mismatch_ok(prev)); | |
9170 | } | |
9171 | ||
58e2af8b | 9172 | static int do_check(struct bpf_verifier_env *env) |
17a52670 | 9173 | { |
6f8a57cc | 9174 | bool pop_log = !(env->log.level & BPF_LOG_LEVEL2); |
51c39bb1 | 9175 | struct bpf_verifier_state *state = env->cur_state; |
17a52670 | 9176 | struct bpf_insn *insns = env->prog->insnsi; |
638f5b90 | 9177 | struct bpf_reg_state *regs; |
06ee7115 | 9178 | int insn_cnt = env->prog->len; |
17a52670 | 9179 | bool do_print_state = false; |
b5dc0163 | 9180 | int prev_insn_idx = -1; |
17a52670 | 9181 | |
17a52670 AS |
9182 | for (;;) { |
9183 | struct bpf_insn *insn; | |
9184 | u8 class; | |
9185 | int err; | |
9186 | ||
b5dc0163 | 9187 | env->prev_insn_idx = prev_insn_idx; |
c08435ec | 9188 | if (env->insn_idx >= insn_cnt) { |
61bd5218 | 9189 | verbose(env, "invalid insn idx %d insn_cnt %d\n", |
c08435ec | 9190 | env->insn_idx, insn_cnt); |
17a52670 AS |
9191 | return -EFAULT; |
9192 | } | |
9193 | ||
c08435ec | 9194 | insn = &insns[env->insn_idx]; |
17a52670 AS |
9195 | class = BPF_CLASS(insn->code); |
9196 | ||
06ee7115 | 9197 | if (++env->insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) { |
61bd5218 JK |
9198 | verbose(env, |
9199 | "BPF program is too large. Processed %d insn\n", | |
06ee7115 | 9200 | env->insn_processed); |
17a52670 AS |
9201 | return -E2BIG; |
9202 | } | |
9203 | ||
c08435ec | 9204 | err = is_state_visited(env, env->insn_idx); |
f1bca824 AS |
9205 | if (err < 0) |
9206 | return err; | |
9207 | if (err == 1) { | |
9208 | /* found equivalent state, can prune the search */ | |
06ee7115 | 9209 | if (env->log.level & BPF_LOG_LEVEL) { |
f1bca824 | 9210 | if (do_print_state) |
979d63d5 DB |
9211 | verbose(env, "\nfrom %d to %d%s: safe\n", |
9212 | env->prev_insn_idx, env->insn_idx, | |
9213 | env->cur_state->speculative ? | |
9214 | " (speculative execution)" : ""); | |
f1bca824 | 9215 | else |
c08435ec | 9216 | verbose(env, "%d: safe\n", env->insn_idx); |
f1bca824 AS |
9217 | } |
9218 | goto process_bpf_exit; | |
9219 | } | |
9220 | ||
c3494801 AS |
9221 | if (signal_pending(current)) |
9222 | return -EAGAIN; | |
9223 | ||
3c2ce60b DB |
9224 | if (need_resched()) |
9225 | cond_resched(); | |
9226 | ||
06ee7115 AS |
9227 | if (env->log.level & BPF_LOG_LEVEL2 || |
9228 | (env->log.level & BPF_LOG_LEVEL && do_print_state)) { | |
9229 | if (env->log.level & BPF_LOG_LEVEL2) | |
c08435ec | 9230 | verbose(env, "%d:", env->insn_idx); |
c5fc9692 | 9231 | else |
979d63d5 DB |
9232 | verbose(env, "\nfrom %d to %d%s:", |
9233 | env->prev_insn_idx, env->insn_idx, | |
9234 | env->cur_state->speculative ? | |
9235 | " (speculative execution)" : ""); | |
f4d7e40a | 9236 | print_verifier_state(env, state->frame[state->curframe]); |
17a52670 AS |
9237 | do_print_state = false; |
9238 | } | |
9239 | ||
06ee7115 | 9240 | if (env->log.level & BPF_LOG_LEVEL) { |
7105e828 DB |
9241 | const struct bpf_insn_cbs cbs = { |
9242 | .cb_print = verbose, | |
abe08840 | 9243 | .private_data = env, |
7105e828 DB |
9244 | }; |
9245 | ||
c08435ec DB |
9246 | verbose_linfo(env, env->insn_idx, "; "); |
9247 | verbose(env, "%d: ", env->insn_idx); | |
abe08840 | 9248 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); |
17a52670 AS |
9249 | } |
9250 | ||
cae1927c | 9251 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
c08435ec DB |
9252 | err = bpf_prog_offload_verify_insn(env, env->insn_idx, |
9253 | env->prev_insn_idx); | |
cae1927c JK |
9254 | if (err) |
9255 | return err; | |
9256 | } | |
13a27dfc | 9257 | |
638f5b90 | 9258 | regs = cur_regs(env); |
51c39bb1 | 9259 | env->insn_aux_data[env->insn_idx].seen = env->pass_cnt; |
b5dc0163 | 9260 | prev_insn_idx = env->insn_idx; |
fd978bf7 | 9261 | |
17a52670 | 9262 | if (class == BPF_ALU || class == BPF_ALU64) { |
1be7f75d | 9263 | err = check_alu_op(env, insn); |
17a52670 AS |
9264 | if (err) |
9265 | return err; | |
9266 | ||
9267 | } else if (class == BPF_LDX) { | |
3df126f3 | 9268 | enum bpf_reg_type *prev_src_type, src_reg_type; |
9bac3d6d AS |
9269 | |
9270 | /* check for reserved fields is already done */ | |
9271 | ||
17a52670 | 9272 | /* check src operand */ |
dc503a8a | 9273 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
9274 | if (err) |
9275 | return err; | |
9276 | ||
dc503a8a | 9277 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
17a52670 AS |
9278 | if (err) |
9279 | return err; | |
9280 | ||
725f9dcd AS |
9281 | src_reg_type = regs[insn->src_reg].type; |
9282 | ||
17a52670 AS |
9283 | /* check that memory (src_reg + off) is readable, |
9284 | * the state of dst_reg will be updated by this func | |
9285 | */ | |
c08435ec DB |
9286 | err = check_mem_access(env, env->insn_idx, insn->src_reg, |
9287 | insn->off, BPF_SIZE(insn->code), | |
9288 | BPF_READ, insn->dst_reg, false); | |
17a52670 AS |
9289 | if (err) |
9290 | return err; | |
9291 | ||
c08435ec | 9292 | prev_src_type = &env->insn_aux_data[env->insn_idx].ptr_type; |
3df126f3 JK |
9293 | |
9294 | if (*prev_src_type == NOT_INIT) { | |
9bac3d6d AS |
9295 | /* saw a valid insn |
9296 | * dst_reg = *(u32 *)(src_reg + off) | |
3df126f3 | 9297 | * save type to validate intersecting paths |
9bac3d6d | 9298 | */ |
3df126f3 | 9299 | *prev_src_type = src_reg_type; |
9bac3d6d | 9300 | |
c64b7983 | 9301 | } else if (reg_type_mismatch(src_reg_type, *prev_src_type)) { |
9bac3d6d AS |
9302 | /* ABuser program is trying to use the same insn |
9303 | * dst_reg = *(u32*) (src_reg + off) | |
9304 | * with different pointer types: | |
9305 | * src_reg == ctx in one branch and | |
9306 | * src_reg == stack|map in some other branch. | |
9307 | * Reject it. | |
9308 | */ | |
61bd5218 | 9309 | verbose(env, "same insn cannot be used with different pointers\n"); |
9bac3d6d AS |
9310 | return -EINVAL; |
9311 | } | |
9312 | ||
17a52670 | 9313 | } else if (class == BPF_STX) { |
3df126f3 | 9314 | enum bpf_reg_type *prev_dst_type, dst_reg_type; |
d691f9e8 | 9315 | |
17a52670 | 9316 | if (BPF_MODE(insn->code) == BPF_XADD) { |
c08435ec | 9317 | err = check_xadd(env, env->insn_idx, insn); |
17a52670 AS |
9318 | if (err) |
9319 | return err; | |
c08435ec | 9320 | env->insn_idx++; |
17a52670 AS |
9321 | continue; |
9322 | } | |
9323 | ||
17a52670 | 9324 | /* check src1 operand */ |
dc503a8a | 9325 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
9326 | if (err) |
9327 | return err; | |
9328 | /* check src2 operand */ | |
dc503a8a | 9329 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
9330 | if (err) |
9331 | return err; | |
9332 | ||
d691f9e8 AS |
9333 | dst_reg_type = regs[insn->dst_reg].type; |
9334 | ||
17a52670 | 9335 | /* check that memory (dst_reg + off) is writeable */ |
c08435ec DB |
9336 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
9337 | insn->off, BPF_SIZE(insn->code), | |
9338 | BPF_WRITE, insn->src_reg, false); | |
17a52670 AS |
9339 | if (err) |
9340 | return err; | |
9341 | ||
c08435ec | 9342 | prev_dst_type = &env->insn_aux_data[env->insn_idx].ptr_type; |
3df126f3 JK |
9343 | |
9344 | if (*prev_dst_type == NOT_INIT) { | |
9345 | *prev_dst_type = dst_reg_type; | |
c64b7983 | 9346 | } else if (reg_type_mismatch(dst_reg_type, *prev_dst_type)) { |
61bd5218 | 9347 | verbose(env, "same insn cannot be used with different pointers\n"); |
d691f9e8 AS |
9348 | return -EINVAL; |
9349 | } | |
9350 | ||
17a52670 AS |
9351 | } else if (class == BPF_ST) { |
9352 | if (BPF_MODE(insn->code) != BPF_MEM || | |
9353 | insn->src_reg != BPF_REG_0) { | |
61bd5218 | 9354 | verbose(env, "BPF_ST uses reserved fields\n"); |
17a52670 AS |
9355 | return -EINVAL; |
9356 | } | |
9357 | /* check src operand */ | |
dc503a8a | 9358 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
9359 | if (err) |
9360 | return err; | |
9361 | ||
f37a8cb8 | 9362 | if (is_ctx_reg(env, insn->dst_reg)) { |
9d2be44a | 9363 | verbose(env, "BPF_ST stores into R%d %s is not allowed\n", |
2a159c6f DB |
9364 | insn->dst_reg, |
9365 | reg_type_str[reg_state(env, insn->dst_reg)->type]); | |
f37a8cb8 DB |
9366 | return -EACCES; |
9367 | } | |
9368 | ||
17a52670 | 9369 | /* check that memory (dst_reg + off) is writeable */ |
c08435ec DB |
9370 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
9371 | insn->off, BPF_SIZE(insn->code), | |
9372 | BPF_WRITE, -1, false); | |
17a52670 AS |
9373 | if (err) |
9374 | return err; | |
9375 | ||
092ed096 | 9376 | } else if (class == BPF_JMP || class == BPF_JMP32) { |
17a52670 AS |
9377 | u8 opcode = BPF_OP(insn->code); |
9378 | ||
2589726d | 9379 | env->jmps_processed++; |
17a52670 AS |
9380 | if (opcode == BPF_CALL) { |
9381 | if (BPF_SRC(insn->code) != BPF_K || | |
9382 | insn->off != 0 || | |
f4d7e40a AS |
9383 | (insn->src_reg != BPF_REG_0 && |
9384 | insn->src_reg != BPF_PSEUDO_CALL) || | |
092ed096 JW |
9385 | insn->dst_reg != BPF_REG_0 || |
9386 | class == BPF_JMP32) { | |
61bd5218 | 9387 | verbose(env, "BPF_CALL uses reserved fields\n"); |
17a52670 AS |
9388 | return -EINVAL; |
9389 | } | |
9390 | ||
d83525ca AS |
9391 | if (env->cur_state->active_spin_lock && |
9392 | (insn->src_reg == BPF_PSEUDO_CALL || | |
9393 | insn->imm != BPF_FUNC_spin_unlock)) { | |
9394 | verbose(env, "function calls are not allowed while holding a lock\n"); | |
9395 | return -EINVAL; | |
9396 | } | |
f4d7e40a | 9397 | if (insn->src_reg == BPF_PSEUDO_CALL) |
c08435ec | 9398 | err = check_func_call(env, insn, &env->insn_idx); |
f4d7e40a | 9399 | else |
c08435ec | 9400 | err = check_helper_call(env, insn->imm, env->insn_idx); |
17a52670 AS |
9401 | if (err) |
9402 | return err; | |
9403 | ||
9404 | } else if (opcode == BPF_JA) { | |
9405 | if (BPF_SRC(insn->code) != BPF_K || | |
9406 | insn->imm != 0 || | |
9407 | insn->src_reg != BPF_REG_0 || | |
092ed096 JW |
9408 | insn->dst_reg != BPF_REG_0 || |
9409 | class == BPF_JMP32) { | |
61bd5218 | 9410 | verbose(env, "BPF_JA uses reserved fields\n"); |
17a52670 AS |
9411 | return -EINVAL; |
9412 | } | |
9413 | ||
c08435ec | 9414 | env->insn_idx += insn->off + 1; |
17a52670 AS |
9415 | continue; |
9416 | ||
9417 | } else if (opcode == BPF_EXIT) { | |
9418 | if (BPF_SRC(insn->code) != BPF_K || | |
9419 | insn->imm != 0 || | |
9420 | insn->src_reg != BPF_REG_0 || | |
092ed096 JW |
9421 | insn->dst_reg != BPF_REG_0 || |
9422 | class == BPF_JMP32) { | |
61bd5218 | 9423 | verbose(env, "BPF_EXIT uses reserved fields\n"); |
17a52670 AS |
9424 | return -EINVAL; |
9425 | } | |
9426 | ||
d83525ca AS |
9427 | if (env->cur_state->active_spin_lock) { |
9428 | verbose(env, "bpf_spin_unlock is missing\n"); | |
9429 | return -EINVAL; | |
9430 | } | |
9431 | ||
f4d7e40a AS |
9432 | if (state->curframe) { |
9433 | /* exit from nested function */ | |
c08435ec | 9434 | err = prepare_func_exit(env, &env->insn_idx); |
f4d7e40a AS |
9435 | if (err) |
9436 | return err; | |
9437 | do_print_state = true; | |
9438 | continue; | |
9439 | } | |
9440 | ||
fd978bf7 JS |
9441 | err = check_reference_leak(env); |
9442 | if (err) | |
9443 | return err; | |
9444 | ||
390ee7e2 AS |
9445 | err = check_return_code(env); |
9446 | if (err) | |
9447 | return err; | |
f1bca824 | 9448 | process_bpf_exit: |
2589726d | 9449 | update_branch_counts(env, env->cur_state); |
b5dc0163 | 9450 | err = pop_stack(env, &prev_insn_idx, |
6f8a57cc | 9451 | &env->insn_idx, pop_log); |
638f5b90 AS |
9452 | if (err < 0) { |
9453 | if (err != -ENOENT) | |
9454 | return err; | |
17a52670 AS |
9455 | break; |
9456 | } else { | |
9457 | do_print_state = true; | |
9458 | continue; | |
9459 | } | |
9460 | } else { | |
c08435ec | 9461 | err = check_cond_jmp_op(env, insn, &env->insn_idx); |
17a52670 AS |
9462 | if (err) |
9463 | return err; | |
9464 | } | |
9465 | } else if (class == BPF_LD) { | |
9466 | u8 mode = BPF_MODE(insn->code); | |
9467 | ||
9468 | if (mode == BPF_ABS || mode == BPF_IND) { | |
ddd872bc AS |
9469 | err = check_ld_abs(env, insn); |
9470 | if (err) | |
9471 | return err; | |
9472 | ||
17a52670 AS |
9473 | } else if (mode == BPF_IMM) { |
9474 | err = check_ld_imm(env, insn); | |
9475 | if (err) | |
9476 | return err; | |
9477 | ||
c08435ec | 9478 | env->insn_idx++; |
51c39bb1 | 9479 | env->insn_aux_data[env->insn_idx].seen = env->pass_cnt; |
17a52670 | 9480 | } else { |
61bd5218 | 9481 | verbose(env, "invalid BPF_LD mode\n"); |
17a52670 AS |
9482 | return -EINVAL; |
9483 | } | |
9484 | } else { | |
61bd5218 | 9485 | verbose(env, "unknown insn class %d\n", class); |
17a52670 AS |
9486 | return -EINVAL; |
9487 | } | |
9488 | ||
c08435ec | 9489 | env->insn_idx++; |
17a52670 AS |
9490 | } |
9491 | ||
9492 | return 0; | |
9493 | } | |
9494 | ||
4976b718 HL |
9495 | /* replace pseudo btf_id with kernel symbol address */ |
9496 | static int check_pseudo_btf_id(struct bpf_verifier_env *env, | |
9497 | struct bpf_insn *insn, | |
9498 | struct bpf_insn_aux_data *aux) | |
9499 | { | |
eaa6bcb7 HL |
9500 | u32 datasec_id, type, id = insn->imm; |
9501 | const struct btf_var_secinfo *vsi; | |
9502 | const struct btf_type *datasec; | |
4976b718 HL |
9503 | const struct btf_type *t; |
9504 | const char *sym_name; | |
eaa6bcb7 | 9505 | bool percpu = false; |
4976b718 | 9506 | u64 addr; |
eaa6bcb7 | 9507 | int i; |
4976b718 HL |
9508 | |
9509 | if (!btf_vmlinux) { | |
9510 | verbose(env, "kernel is missing BTF, make sure CONFIG_DEBUG_INFO_BTF=y is specified in Kconfig.\n"); | |
9511 | return -EINVAL; | |
9512 | } | |
9513 | ||
9514 | if (insn[1].imm != 0) { | |
9515 | verbose(env, "reserved field (insn[1].imm) is used in pseudo_btf_id ldimm64 insn.\n"); | |
9516 | return -EINVAL; | |
9517 | } | |
9518 | ||
9519 | t = btf_type_by_id(btf_vmlinux, id); | |
9520 | if (!t) { | |
9521 | verbose(env, "ldimm64 insn specifies invalid btf_id %d.\n", id); | |
9522 | return -ENOENT; | |
9523 | } | |
9524 | ||
9525 | if (!btf_type_is_var(t)) { | |
9526 | verbose(env, "pseudo btf_id %d in ldimm64 isn't KIND_VAR.\n", | |
9527 | id); | |
9528 | return -EINVAL; | |
9529 | } | |
9530 | ||
9531 | sym_name = btf_name_by_offset(btf_vmlinux, t->name_off); | |
9532 | addr = kallsyms_lookup_name(sym_name); | |
9533 | if (!addr) { | |
9534 | verbose(env, "ldimm64 failed to find the address for kernel symbol '%s'.\n", | |
9535 | sym_name); | |
9536 | return -ENOENT; | |
9537 | } | |
9538 | ||
eaa6bcb7 HL |
9539 | datasec_id = btf_find_by_name_kind(btf_vmlinux, ".data..percpu", |
9540 | BTF_KIND_DATASEC); | |
9541 | if (datasec_id > 0) { | |
9542 | datasec = btf_type_by_id(btf_vmlinux, datasec_id); | |
9543 | for_each_vsi(i, datasec, vsi) { | |
9544 | if (vsi->type == id) { | |
9545 | percpu = true; | |
9546 | break; | |
9547 | } | |
9548 | } | |
9549 | } | |
9550 | ||
4976b718 HL |
9551 | insn[0].imm = (u32)addr; |
9552 | insn[1].imm = addr >> 32; | |
9553 | ||
9554 | type = t->type; | |
9555 | t = btf_type_skip_modifiers(btf_vmlinux, type, NULL); | |
eaa6bcb7 HL |
9556 | if (percpu) { |
9557 | aux->btf_var.reg_type = PTR_TO_PERCPU_BTF_ID; | |
9558 | aux->btf_var.btf_id = type; | |
9559 | } else if (!btf_type_is_struct(t)) { | |
4976b718 HL |
9560 | const struct btf_type *ret; |
9561 | const char *tname; | |
9562 | u32 tsize; | |
9563 | ||
9564 | /* resolve the type size of ksym. */ | |
9565 | ret = btf_resolve_size(btf_vmlinux, t, &tsize); | |
9566 | if (IS_ERR(ret)) { | |
9567 | tname = btf_name_by_offset(btf_vmlinux, t->name_off); | |
9568 | verbose(env, "ldimm64 unable to resolve the size of type '%s': %ld\n", | |
9569 | tname, PTR_ERR(ret)); | |
9570 | return -EINVAL; | |
9571 | } | |
9572 | aux->btf_var.reg_type = PTR_TO_MEM; | |
9573 | aux->btf_var.mem_size = tsize; | |
9574 | } else { | |
9575 | aux->btf_var.reg_type = PTR_TO_BTF_ID; | |
9576 | aux->btf_var.btf_id = type; | |
9577 | } | |
9578 | return 0; | |
9579 | } | |
9580 | ||
56f668df MKL |
9581 | static int check_map_prealloc(struct bpf_map *map) |
9582 | { | |
9583 | return (map->map_type != BPF_MAP_TYPE_HASH && | |
bcc6b1b7 MKL |
9584 | map->map_type != BPF_MAP_TYPE_PERCPU_HASH && |
9585 | map->map_type != BPF_MAP_TYPE_HASH_OF_MAPS) || | |
56f668df MKL |
9586 | !(map->map_flags & BPF_F_NO_PREALLOC); |
9587 | } | |
9588 | ||
d83525ca AS |
9589 | static bool is_tracing_prog_type(enum bpf_prog_type type) |
9590 | { | |
9591 | switch (type) { | |
9592 | case BPF_PROG_TYPE_KPROBE: | |
9593 | case BPF_PROG_TYPE_TRACEPOINT: | |
9594 | case BPF_PROG_TYPE_PERF_EVENT: | |
9595 | case BPF_PROG_TYPE_RAW_TRACEPOINT: | |
9596 | return true; | |
9597 | default: | |
9598 | return false; | |
9599 | } | |
9600 | } | |
9601 | ||
94dacdbd TG |
9602 | static bool is_preallocated_map(struct bpf_map *map) |
9603 | { | |
9604 | if (!check_map_prealloc(map)) | |
9605 | return false; | |
9606 | if (map->inner_map_meta && !check_map_prealloc(map->inner_map_meta)) | |
9607 | return false; | |
9608 | return true; | |
9609 | } | |
9610 | ||
61bd5218 JK |
9611 | static int check_map_prog_compatibility(struct bpf_verifier_env *env, |
9612 | struct bpf_map *map, | |
fdc15d38 AS |
9613 | struct bpf_prog *prog) |
9614 | ||
9615 | { | |
7e40781c | 9616 | enum bpf_prog_type prog_type = resolve_prog_type(prog); |
94dacdbd TG |
9617 | /* |
9618 | * Validate that trace type programs use preallocated hash maps. | |
9619 | * | |
9620 | * For programs attached to PERF events this is mandatory as the | |
9621 | * perf NMI can hit any arbitrary code sequence. | |
9622 | * | |
9623 | * All other trace types using preallocated hash maps are unsafe as | |
9624 | * well because tracepoint or kprobes can be inside locked regions | |
9625 | * of the memory allocator or at a place where a recursion into the | |
9626 | * memory allocator would see inconsistent state. | |
9627 | * | |
2ed905c5 TG |
9628 | * On RT enabled kernels run-time allocation of all trace type |
9629 | * programs is strictly prohibited due to lock type constraints. On | |
9630 | * !RT kernels it is allowed for backwards compatibility reasons for | |
9631 | * now, but warnings are emitted so developers are made aware of | |
9632 | * the unsafety and can fix their programs before this is enforced. | |
56f668df | 9633 | */ |
7e40781c UP |
9634 | if (is_tracing_prog_type(prog_type) && !is_preallocated_map(map)) { |
9635 | if (prog_type == BPF_PROG_TYPE_PERF_EVENT) { | |
61bd5218 | 9636 | verbose(env, "perf_event programs can only use preallocated hash map\n"); |
56f668df MKL |
9637 | return -EINVAL; |
9638 | } | |
2ed905c5 TG |
9639 | if (IS_ENABLED(CONFIG_PREEMPT_RT)) { |
9640 | verbose(env, "trace type programs can only use preallocated hash map\n"); | |
9641 | return -EINVAL; | |
9642 | } | |
94dacdbd TG |
9643 | WARN_ONCE(1, "trace type BPF program uses run-time allocation\n"); |
9644 | verbose(env, "trace type programs with run-time allocated hash maps are unsafe. Switch to preallocated hash maps.\n"); | |
fdc15d38 | 9645 | } |
a3884572 | 9646 | |
7e40781c UP |
9647 | if ((is_tracing_prog_type(prog_type) || |
9648 | prog_type == BPF_PROG_TYPE_SOCKET_FILTER) && | |
d83525ca AS |
9649 | map_value_has_spin_lock(map)) { |
9650 | verbose(env, "tracing progs cannot use bpf_spin_lock yet\n"); | |
9651 | return -EINVAL; | |
9652 | } | |
9653 | ||
a3884572 | 9654 | if ((bpf_prog_is_dev_bound(prog->aux) || bpf_map_is_dev_bound(map)) && |
09728266 | 9655 | !bpf_offload_prog_map_match(prog, map)) { |
a3884572 JK |
9656 | verbose(env, "offload device mismatch between prog and map\n"); |
9657 | return -EINVAL; | |
9658 | } | |
9659 | ||
85d33df3 MKL |
9660 | if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) { |
9661 | verbose(env, "bpf_struct_ops map cannot be used in prog\n"); | |
9662 | return -EINVAL; | |
9663 | } | |
9664 | ||
1e6c62a8 AS |
9665 | if (prog->aux->sleepable) |
9666 | switch (map->map_type) { | |
9667 | case BPF_MAP_TYPE_HASH: | |
9668 | case BPF_MAP_TYPE_LRU_HASH: | |
9669 | case BPF_MAP_TYPE_ARRAY: | |
9670 | if (!is_preallocated_map(map)) { | |
9671 | verbose(env, | |
9672 | "Sleepable programs can only use preallocated hash maps\n"); | |
9673 | return -EINVAL; | |
9674 | } | |
9675 | break; | |
9676 | default: | |
9677 | verbose(env, | |
9678 | "Sleepable programs can only use array and hash maps\n"); | |
9679 | return -EINVAL; | |
9680 | } | |
9681 | ||
fdc15d38 AS |
9682 | return 0; |
9683 | } | |
9684 | ||
b741f163 RG |
9685 | static bool bpf_map_is_cgroup_storage(struct bpf_map *map) |
9686 | { | |
9687 | return (map->map_type == BPF_MAP_TYPE_CGROUP_STORAGE || | |
9688 | map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE); | |
9689 | } | |
9690 | ||
4976b718 HL |
9691 | /* find and rewrite pseudo imm in ld_imm64 instructions: |
9692 | * | |
9693 | * 1. if it accesses map FD, replace it with actual map pointer. | |
9694 | * 2. if it accesses btf_id of a VAR, replace it with pointer to the var. | |
9695 | * | |
9696 | * NOTE: btf_vmlinux is required for converting pseudo btf_id. | |
0246e64d | 9697 | */ |
4976b718 | 9698 | static int resolve_pseudo_ldimm64(struct bpf_verifier_env *env) |
0246e64d AS |
9699 | { |
9700 | struct bpf_insn *insn = env->prog->insnsi; | |
9701 | int insn_cnt = env->prog->len; | |
fdc15d38 | 9702 | int i, j, err; |
0246e64d | 9703 | |
f1f7714e | 9704 | err = bpf_prog_calc_tag(env->prog); |
aafe6ae9 DB |
9705 | if (err) |
9706 | return err; | |
9707 | ||
0246e64d | 9708 | for (i = 0; i < insn_cnt; i++, insn++) { |
9bac3d6d | 9709 | if (BPF_CLASS(insn->code) == BPF_LDX && |
d691f9e8 | 9710 | (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) { |
61bd5218 | 9711 | verbose(env, "BPF_LDX uses reserved fields\n"); |
9bac3d6d AS |
9712 | return -EINVAL; |
9713 | } | |
9714 | ||
d691f9e8 AS |
9715 | if (BPF_CLASS(insn->code) == BPF_STX && |
9716 | ((BPF_MODE(insn->code) != BPF_MEM && | |
9717 | BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) { | |
61bd5218 | 9718 | verbose(env, "BPF_STX uses reserved fields\n"); |
d691f9e8 AS |
9719 | return -EINVAL; |
9720 | } | |
9721 | ||
0246e64d | 9722 | if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) { |
d8eca5bb | 9723 | struct bpf_insn_aux_data *aux; |
0246e64d AS |
9724 | struct bpf_map *map; |
9725 | struct fd f; | |
d8eca5bb | 9726 | u64 addr; |
0246e64d AS |
9727 | |
9728 | if (i == insn_cnt - 1 || insn[1].code != 0 || | |
9729 | insn[1].dst_reg != 0 || insn[1].src_reg != 0 || | |
9730 | insn[1].off != 0) { | |
61bd5218 | 9731 | verbose(env, "invalid bpf_ld_imm64 insn\n"); |
0246e64d AS |
9732 | return -EINVAL; |
9733 | } | |
9734 | ||
d8eca5bb | 9735 | if (insn[0].src_reg == 0) |
0246e64d AS |
9736 | /* valid generic load 64-bit imm */ |
9737 | goto next_insn; | |
9738 | ||
4976b718 HL |
9739 | if (insn[0].src_reg == BPF_PSEUDO_BTF_ID) { |
9740 | aux = &env->insn_aux_data[i]; | |
9741 | err = check_pseudo_btf_id(env, insn, aux); | |
9742 | if (err) | |
9743 | return err; | |
9744 | goto next_insn; | |
9745 | } | |
9746 | ||
d8eca5bb DB |
9747 | /* In final convert_pseudo_ld_imm64() step, this is |
9748 | * converted into regular 64-bit imm load insn. | |
9749 | */ | |
9750 | if ((insn[0].src_reg != BPF_PSEUDO_MAP_FD && | |
9751 | insn[0].src_reg != BPF_PSEUDO_MAP_VALUE) || | |
9752 | (insn[0].src_reg == BPF_PSEUDO_MAP_FD && | |
9753 | insn[1].imm != 0)) { | |
9754 | verbose(env, | |
9755 | "unrecognized bpf_ld_imm64 insn\n"); | |
0246e64d AS |
9756 | return -EINVAL; |
9757 | } | |
9758 | ||
20182390 | 9759 | f = fdget(insn[0].imm); |
c2101297 | 9760 | map = __bpf_map_get(f); |
0246e64d | 9761 | if (IS_ERR(map)) { |
61bd5218 | 9762 | verbose(env, "fd %d is not pointing to valid bpf_map\n", |
20182390 | 9763 | insn[0].imm); |
0246e64d AS |
9764 | return PTR_ERR(map); |
9765 | } | |
9766 | ||
61bd5218 | 9767 | err = check_map_prog_compatibility(env, map, env->prog); |
fdc15d38 AS |
9768 | if (err) { |
9769 | fdput(f); | |
9770 | return err; | |
9771 | } | |
9772 | ||
d8eca5bb DB |
9773 | aux = &env->insn_aux_data[i]; |
9774 | if (insn->src_reg == BPF_PSEUDO_MAP_FD) { | |
9775 | addr = (unsigned long)map; | |
9776 | } else { | |
9777 | u32 off = insn[1].imm; | |
9778 | ||
9779 | if (off >= BPF_MAX_VAR_OFF) { | |
9780 | verbose(env, "direct value offset of %u is not allowed\n", off); | |
9781 | fdput(f); | |
9782 | return -EINVAL; | |
9783 | } | |
9784 | ||
9785 | if (!map->ops->map_direct_value_addr) { | |
9786 | verbose(env, "no direct value access support for this map type\n"); | |
9787 | fdput(f); | |
9788 | return -EINVAL; | |
9789 | } | |
9790 | ||
9791 | err = map->ops->map_direct_value_addr(map, &addr, off); | |
9792 | if (err) { | |
9793 | verbose(env, "invalid access to map value pointer, value_size=%u off=%u\n", | |
9794 | map->value_size, off); | |
9795 | fdput(f); | |
9796 | return err; | |
9797 | } | |
9798 | ||
9799 | aux->map_off = off; | |
9800 | addr += off; | |
9801 | } | |
9802 | ||
9803 | insn[0].imm = (u32)addr; | |
9804 | insn[1].imm = addr >> 32; | |
0246e64d AS |
9805 | |
9806 | /* check whether we recorded this map already */ | |
d8eca5bb | 9807 | for (j = 0; j < env->used_map_cnt; j++) { |
0246e64d | 9808 | if (env->used_maps[j] == map) { |
d8eca5bb | 9809 | aux->map_index = j; |
0246e64d AS |
9810 | fdput(f); |
9811 | goto next_insn; | |
9812 | } | |
d8eca5bb | 9813 | } |
0246e64d AS |
9814 | |
9815 | if (env->used_map_cnt >= MAX_USED_MAPS) { | |
9816 | fdput(f); | |
9817 | return -E2BIG; | |
9818 | } | |
9819 | ||
0246e64d AS |
9820 | /* hold the map. If the program is rejected by verifier, |
9821 | * the map will be released by release_maps() or it | |
9822 | * will be used by the valid program until it's unloaded | |
ab7f5bf0 | 9823 | * and all maps are released in free_used_maps() |
0246e64d | 9824 | */ |
1e0bd5a0 | 9825 | bpf_map_inc(map); |
d8eca5bb DB |
9826 | |
9827 | aux->map_index = env->used_map_cnt; | |
92117d84 AS |
9828 | env->used_maps[env->used_map_cnt++] = map; |
9829 | ||
b741f163 | 9830 | if (bpf_map_is_cgroup_storage(map) && |
e4730423 | 9831 | bpf_cgroup_storage_assign(env->prog->aux, map)) { |
b741f163 | 9832 | verbose(env, "only one cgroup storage of each type is allowed\n"); |
de9cbbaa RG |
9833 | fdput(f); |
9834 | return -EBUSY; | |
9835 | } | |
9836 | ||
0246e64d AS |
9837 | fdput(f); |
9838 | next_insn: | |
9839 | insn++; | |
9840 | i++; | |
5e581dad DB |
9841 | continue; |
9842 | } | |
9843 | ||
9844 | /* Basic sanity check before we invest more work here. */ | |
9845 | if (!bpf_opcode_in_insntable(insn->code)) { | |
9846 | verbose(env, "unknown opcode %02x\n", insn->code); | |
9847 | return -EINVAL; | |
0246e64d AS |
9848 | } |
9849 | } | |
9850 | ||
9851 | /* now all pseudo BPF_LD_IMM64 instructions load valid | |
9852 | * 'struct bpf_map *' into a register instead of user map_fd. | |
9853 | * These pointers will be used later by verifier to validate map access. | |
9854 | */ | |
9855 | return 0; | |
9856 | } | |
9857 | ||
9858 | /* drop refcnt of maps used by the rejected program */ | |
58e2af8b | 9859 | static void release_maps(struct bpf_verifier_env *env) |
0246e64d | 9860 | { |
a2ea0746 DB |
9861 | __bpf_free_used_maps(env->prog->aux, env->used_maps, |
9862 | env->used_map_cnt); | |
0246e64d AS |
9863 | } |
9864 | ||
9865 | /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */ | |
58e2af8b | 9866 | static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env) |
0246e64d AS |
9867 | { |
9868 | struct bpf_insn *insn = env->prog->insnsi; | |
9869 | int insn_cnt = env->prog->len; | |
9870 | int i; | |
9871 | ||
9872 | for (i = 0; i < insn_cnt; i++, insn++) | |
9873 | if (insn->code == (BPF_LD | BPF_IMM | BPF_DW)) | |
9874 | insn->src_reg = 0; | |
9875 | } | |
9876 | ||
8041902d AS |
9877 | /* single env->prog->insni[off] instruction was replaced with the range |
9878 | * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying | |
9879 | * [0, off) and [off, end) to new locations, so the patched range stays zero | |
9880 | */ | |
b325fbca JW |
9881 | static int adjust_insn_aux_data(struct bpf_verifier_env *env, |
9882 | struct bpf_prog *new_prog, u32 off, u32 cnt) | |
8041902d AS |
9883 | { |
9884 | struct bpf_insn_aux_data *new_data, *old_data = env->insn_aux_data; | |
b325fbca JW |
9885 | struct bpf_insn *insn = new_prog->insnsi; |
9886 | u32 prog_len; | |
c131187d | 9887 | int i; |
8041902d | 9888 | |
b325fbca JW |
9889 | /* aux info at OFF always needs adjustment, no matter fast path |
9890 | * (cnt == 1) is taken or not. There is no guarantee INSN at OFF is the | |
9891 | * original insn at old prog. | |
9892 | */ | |
9893 | old_data[off].zext_dst = insn_has_def32(env, insn + off + cnt - 1); | |
9894 | ||
8041902d AS |
9895 | if (cnt == 1) |
9896 | return 0; | |
b325fbca | 9897 | prog_len = new_prog->len; |
fad953ce KC |
9898 | new_data = vzalloc(array_size(prog_len, |
9899 | sizeof(struct bpf_insn_aux_data))); | |
8041902d AS |
9900 | if (!new_data) |
9901 | return -ENOMEM; | |
9902 | memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off); | |
9903 | memcpy(new_data + off + cnt - 1, old_data + off, | |
9904 | sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1)); | |
b325fbca | 9905 | for (i = off; i < off + cnt - 1; i++) { |
51c39bb1 | 9906 | new_data[i].seen = env->pass_cnt; |
b325fbca JW |
9907 | new_data[i].zext_dst = insn_has_def32(env, insn + i); |
9908 | } | |
8041902d AS |
9909 | env->insn_aux_data = new_data; |
9910 | vfree(old_data); | |
9911 | return 0; | |
9912 | } | |
9913 | ||
cc8b0b92 AS |
9914 | static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len) |
9915 | { | |
9916 | int i; | |
9917 | ||
9918 | if (len == 1) | |
9919 | return; | |
4cb3d99c JW |
9920 | /* NOTE: fake 'exit' subprog should be updated as well. */ |
9921 | for (i = 0; i <= env->subprog_cnt; i++) { | |
afd59424 | 9922 | if (env->subprog_info[i].start <= off) |
cc8b0b92 | 9923 | continue; |
9c8105bd | 9924 | env->subprog_info[i].start += len - 1; |
cc8b0b92 AS |
9925 | } |
9926 | } | |
9927 | ||
a748c697 MF |
9928 | static void adjust_poke_descs(struct bpf_prog *prog, u32 len) |
9929 | { | |
9930 | struct bpf_jit_poke_descriptor *tab = prog->aux->poke_tab; | |
9931 | int i, sz = prog->aux->size_poke_tab; | |
9932 | struct bpf_jit_poke_descriptor *desc; | |
9933 | ||
9934 | for (i = 0; i < sz; i++) { | |
9935 | desc = &tab[i]; | |
9936 | desc->insn_idx += len - 1; | |
9937 | } | |
9938 | } | |
9939 | ||
8041902d AS |
9940 | static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off, |
9941 | const struct bpf_insn *patch, u32 len) | |
9942 | { | |
9943 | struct bpf_prog *new_prog; | |
9944 | ||
9945 | new_prog = bpf_patch_insn_single(env->prog, off, patch, len); | |
4f73379e AS |
9946 | if (IS_ERR(new_prog)) { |
9947 | if (PTR_ERR(new_prog) == -ERANGE) | |
9948 | verbose(env, | |
9949 | "insn %d cannot be patched due to 16-bit range\n", | |
9950 | env->insn_aux_data[off].orig_idx); | |
8041902d | 9951 | return NULL; |
4f73379e | 9952 | } |
b325fbca | 9953 | if (adjust_insn_aux_data(env, new_prog, off, len)) |
8041902d | 9954 | return NULL; |
cc8b0b92 | 9955 | adjust_subprog_starts(env, off, len); |
a748c697 | 9956 | adjust_poke_descs(new_prog, len); |
8041902d AS |
9957 | return new_prog; |
9958 | } | |
9959 | ||
52875a04 JK |
9960 | static int adjust_subprog_starts_after_remove(struct bpf_verifier_env *env, |
9961 | u32 off, u32 cnt) | |
9962 | { | |
9963 | int i, j; | |
9964 | ||
9965 | /* find first prog starting at or after off (first to remove) */ | |
9966 | for (i = 0; i < env->subprog_cnt; i++) | |
9967 | if (env->subprog_info[i].start >= off) | |
9968 | break; | |
9969 | /* find first prog starting at or after off + cnt (first to stay) */ | |
9970 | for (j = i; j < env->subprog_cnt; j++) | |
9971 | if (env->subprog_info[j].start >= off + cnt) | |
9972 | break; | |
9973 | /* if j doesn't start exactly at off + cnt, we are just removing | |
9974 | * the front of previous prog | |
9975 | */ | |
9976 | if (env->subprog_info[j].start != off + cnt) | |
9977 | j--; | |
9978 | ||
9979 | if (j > i) { | |
9980 | struct bpf_prog_aux *aux = env->prog->aux; | |
9981 | int move; | |
9982 | ||
9983 | /* move fake 'exit' subprog as well */ | |
9984 | move = env->subprog_cnt + 1 - j; | |
9985 | ||
9986 | memmove(env->subprog_info + i, | |
9987 | env->subprog_info + j, | |
9988 | sizeof(*env->subprog_info) * move); | |
9989 | env->subprog_cnt -= j - i; | |
9990 | ||
9991 | /* remove func_info */ | |
9992 | if (aux->func_info) { | |
9993 | move = aux->func_info_cnt - j; | |
9994 | ||
9995 | memmove(aux->func_info + i, | |
9996 | aux->func_info + j, | |
9997 | sizeof(*aux->func_info) * move); | |
9998 | aux->func_info_cnt -= j - i; | |
9999 | /* func_info->insn_off is set after all code rewrites, | |
10000 | * in adjust_btf_func() - no need to adjust | |
10001 | */ | |
10002 | } | |
10003 | } else { | |
10004 | /* convert i from "first prog to remove" to "first to adjust" */ | |
10005 | if (env->subprog_info[i].start == off) | |
10006 | i++; | |
10007 | } | |
10008 | ||
10009 | /* update fake 'exit' subprog as well */ | |
10010 | for (; i <= env->subprog_cnt; i++) | |
10011 | env->subprog_info[i].start -= cnt; | |
10012 | ||
10013 | return 0; | |
10014 | } | |
10015 | ||
10016 | static int bpf_adj_linfo_after_remove(struct bpf_verifier_env *env, u32 off, | |
10017 | u32 cnt) | |
10018 | { | |
10019 | struct bpf_prog *prog = env->prog; | |
10020 | u32 i, l_off, l_cnt, nr_linfo; | |
10021 | struct bpf_line_info *linfo; | |
10022 | ||
10023 | nr_linfo = prog->aux->nr_linfo; | |
10024 | if (!nr_linfo) | |
10025 | return 0; | |
10026 | ||
10027 | linfo = prog->aux->linfo; | |
10028 | ||
10029 | /* find first line info to remove, count lines to be removed */ | |
10030 | for (i = 0; i < nr_linfo; i++) | |
10031 | if (linfo[i].insn_off >= off) | |
10032 | break; | |
10033 | ||
10034 | l_off = i; | |
10035 | l_cnt = 0; | |
10036 | for (; i < nr_linfo; i++) | |
10037 | if (linfo[i].insn_off < off + cnt) | |
10038 | l_cnt++; | |
10039 | else | |
10040 | break; | |
10041 | ||
10042 | /* First live insn doesn't match first live linfo, it needs to "inherit" | |
10043 | * last removed linfo. prog is already modified, so prog->len == off | |
10044 | * means no live instructions after (tail of the program was removed). | |
10045 | */ | |
10046 | if (prog->len != off && l_cnt && | |
10047 | (i == nr_linfo || linfo[i].insn_off != off + cnt)) { | |
10048 | l_cnt--; | |
10049 | linfo[--i].insn_off = off + cnt; | |
10050 | } | |
10051 | ||
10052 | /* remove the line info which refer to the removed instructions */ | |
10053 | if (l_cnt) { | |
10054 | memmove(linfo + l_off, linfo + i, | |
10055 | sizeof(*linfo) * (nr_linfo - i)); | |
10056 | ||
10057 | prog->aux->nr_linfo -= l_cnt; | |
10058 | nr_linfo = prog->aux->nr_linfo; | |
10059 | } | |
10060 | ||
10061 | /* pull all linfo[i].insn_off >= off + cnt in by cnt */ | |
10062 | for (i = l_off; i < nr_linfo; i++) | |
10063 | linfo[i].insn_off -= cnt; | |
10064 | ||
10065 | /* fix up all subprogs (incl. 'exit') which start >= off */ | |
10066 | for (i = 0; i <= env->subprog_cnt; i++) | |
10067 | if (env->subprog_info[i].linfo_idx > l_off) { | |
10068 | /* program may have started in the removed region but | |
10069 | * may not be fully removed | |
10070 | */ | |
10071 | if (env->subprog_info[i].linfo_idx >= l_off + l_cnt) | |
10072 | env->subprog_info[i].linfo_idx -= l_cnt; | |
10073 | else | |
10074 | env->subprog_info[i].linfo_idx = l_off; | |
10075 | } | |
10076 | ||
10077 | return 0; | |
10078 | } | |
10079 | ||
10080 | static int verifier_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt) | |
10081 | { | |
10082 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
10083 | unsigned int orig_prog_len = env->prog->len; | |
10084 | int err; | |
10085 | ||
08ca90af JK |
10086 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
10087 | bpf_prog_offload_remove_insns(env, off, cnt); | |
10088 | ||
52875a04 JK |
10089 | err = bpf_remove_insns(env->prog, off, cnt); |
10090 | if (err) | |
10091 | return err; | |
10092 | ||
10093 | err = adjust_subprog_starts_after_remove(env, off, cnt); | |
10094 | if (err) | |
10095 | return err; | |
10096 | ||
10097 | err = bpf_adj_linfo_after_remove(env, off, cnt); | |
10098 | if (err) | |
10099 | return err; | |
10100 | ||
10101 | memmove(aux_data + off, aux_data + off + cnt, | |
10102 | sizeof(*aux_data) * (orig_prog_len - off - cnt)); | |
10103 | ||
10104 | return 0; | |
10105 | } | |
10106 | ||
2a5418a1 DB |
10107 | /* The verifier does more data flow analysis than llvm and will not |
10108 | * explore branches that are dead at run time. Malicious programs can | |
10109 | * have dead code too. Therefore replace all dead at-run-time code | |
10110 | * with 'ja -1'. | |
10111 | * | |
10112 | * Just nops are not optimal, e.g. if they would sit at the end of the | |
10113 | * program and through another bug we would manage to jump there, then | |
10114 | * we'd execute beyond program memory otherwise. Returning exception | |
10115 | * code also wouldn't work since we can have subprogs where the dead | |
10116 | * code could be located. | |
c131187d AS |
10117 | */ |
10118 | static void sanitize_dead_code(struct bpf_verifier_env *env) | |
10119 | { | |
10120 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
2a5418a1 | 10121 | struct bpf_insn trap = BPF_JMP_IMM(BPF_JA, 0, 0, -1); |
c131187d AS |
10122 | struct bpf_insn *insn = env->prog->insnsi; |
10123 | const int insn_cnt = env->prog->len; | |
10124 | int i; | |
10125 | ||
10126 | for (i = 0; i < insn_cnt; i++) { | |
10127 | if (aux_data[i].seen) | |
10128 | continue; | |
2a5418a1 | 10129 | memcpy(insn + i, &trap, sizeof(trap)); |
c131187d AS |
10130 | } |
10131 | } | |
10132 | ||
e2ae4ca2 JK |
10133 | static bool insn_is_cond_jump(u8 code) |
10134 | { | |
10135 | u8 op; | |
10136 | ||
092ed096 JW |
10137 | if (BPF_CLASS(code) == BPF_JMP32) |
10138 | return true; | |
10139 | ||
e2ae4ca2 JK |
10140 | if (BPF_CLASS(code) != BPF_JMP) |
10141 | return false; | |
10142 | ||
10143 | op = BPF_OP(code); | |
10144 | return op != BPF_JA && op != BPF_EXIT && op != BPF_CALL; | |
10145 | } | |
10146 | ||
10147 | static void opt_hard_wire_dead_code_branches(struct bpf_verifier_env *env) | |
10148 | { | |
10149 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
10150 | struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); | |
10151 | struct bpf_insn *insn = env->prog->insnsi; | |
10152 | const int insn_cnt = env->prog->len; | |
10153 | int i; | |
10154 | ||
10155 | for (i = 0; i < insn_cnt; i++, insn++) { | |
10156 | if (!insn_is_cond_jump(insn->code)) | |
10157 | continue; | |
10158 | ||
10159 | if (!aux_data[i + 1].seen) | |
10160 | ja.off = insn->off; | |
10161 | else if (!aux_data[i + 1 + insn->off].seen) | |
10162 | ja.off = 0; | |
10163 | else | |
10164 | continue; | |
10165 | ||
08ca90af JK |
10166 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
10167 | bpf_prog_offload_replace_insn(env, i, &ja); | |
10168 | ||
e2ae4ca2 JK |
10169 | memcpy(insn, &ja, sizeof(ja)); |
10170 | } | |
10171 | } | |
10172 | ||
52875a04 JK |
10173 | static int opt_remove_dead_code(struct bpf_verifier_env *env) |
10174 | { | |
10175 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
10176 | int insn_cnt = env->prog->len; | |
10177 | int i, err; | |
10178 | ||
10179 | for (i = 0; i < insn_cnt; i++) { | |
10180 | int j; | |
10181 | ||
10182 | j = 0; | |
10183 | while (i + j < insn_cnt && !aux_data[i + j].seen) | |
10184 | j++; | |
10185 | if (!j) | |
10186 | continue; | |
10187 | ||
10188 | err = verifier_remove_insns(env, i, j); | |
10189 | if (err) | |
10190 | return err; | |
10191 | insn_cnt = env->prog->len; | |
10192 | } | |
10193 | ||
10194 | return 0; | |
10195 | } | |
10196 | ||
a1b14abc JK |
10197 | static int opt_remove_nops(struct bpf_verifier_env *env) |
10198 | { | |
10199 | const struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); | |
10200 | struct bpf_insn *insn = env->prog->insnsi; | |
10201 | int insn_cnt = env->prog->len; | |
10202 | int i, err; | |
10203 | ||
10204 | for (i = 0; i < insn_cnt; i++) { | |
10205 | if (memcmp(&insn[i], &ja, sizeof(ja))) | |
10206 | continue; | |
10207 | ||
10208 | err = verifier_remove_insns(env, i, 1); | |
10209 | if (err) | |
10210 | return err; | |
10211 | insn_cnt--; | |
10212 | i--; | |
10213 | } | |
10214 | ||
10215 | return 0; | |
10216 | } | |
10217 | ||
d6c2308c JW |
10218 | static int opt_subreg_zext_lo32_rnd_hi32(struct bpf_verifier_env *env, |
10219 | const union bpf_attr *attr) | |
a4b1d3c1 | 10220 | { |
d6c2308c | 10221 | struct bpf_insn *patch, zext_patch[2], rnd_hi32_patch[4]; |
a4b1d3c1 | 10222 | struct bpf_insn_aux_data *aux = env->insn_aux_data; |
d6c2308c | 10223 | int i, patch_len, delta = 0, len = env->prog->len; |
a4b1d3c1 | 10224 | struct bpf_insn *insns = env->prog->insnsi; |
a4b1d3c1 | 10225 | struct bpf_prog *new_prog; |
d6c2308c | 10226 | bool rnd_hi32; |
a4b1d3c1 | 10227 | |
d6c2308c | 10228 | rnd_hi32 = attr->prog_flags & BPF_F_TEST_RND_HI32; |
a4b1d3c1 | 10229 | zext_patch[1] = BPF_ZEXT_REG(0); |
d6c2308c JW |
10230 | rnd_hi32_patch[1] = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, 0); |
10231 | rnd_hi32_patch[2] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32); | |
10232 | rnd_hi32_patch[3] = BPF_ALU64_REG(BPF_OR, 0, BPF_REG_AX); | |
a4b1d3c1 JW |
10233 | for (i = 0; i < len; i++) { |
10234 | int adj_idx = i + delta; | |
10235 | struct bpf_insn insn; | |
10236 | ||
d6c2308c JW |
10237 | insn = insns[adj_idx]; |
10238 | if (!aux[adj_idx].zext_dst) { | |
10239 | u8 code, class; | |
10240 | u32 imm_rnd; | |
10241 | ||
10242 | if (!rnd_hi32) | |
10243 | continue; | |
10244 | ||
10245 | code = insn.code; | |
10246 | class = BPF_CLASS(code); | |
10247 | if (insn_no_def(&insn)) | |
10248 | continue; | |
10249 | ||
10250 | /* NOTE: arg "reg" (the fourth one) is only used for | |
10251 | * BPF_STX which has been ruled out in above | |
10252 | * check, it is safe to pass NULL here. | |
10253 | */ | |
10254 | if (is_reg64(env, &insn, insn.dst_reg, NULL, DST_OP)) { | |
10255 | if (class == BPF_LD && | |
10256 | BPF_MODE(code) == BPF_IMM) | |
10257 | i++; | |
10258 | continue; | |
10259 | } | |
10260 | ||
10261 | /* ctx load could be transformed into wider load. */ | |
10262 | if (class == BPF_LDX && | |
10263 | aux[adj_idx].ptr_type == PTR_TO_CTX) | |
10264 | continue; | |
10265 | ||
10266 | imm_rnd = get_random_int(); | |
10267 | rnd_hi32_patch[0] = insn; | |
10268 | rnd_hi32_patch[1].imm = imm_rnd; | |
10269 | rnd_hi32_patch[3].dst_reg = insn.dst_reg; | |
10270 | patch = rnd_hi32_patch; | |
10271 | patch_len = 4; | |
10272 | goto apply_patch_buffer; | |
10273 | } | |
10274 | ||
10275 | if (!bpf_jit_needs_zext()) | |
a4b1d3c1 JW |
10276 | continue; |
10277 | ||
a4b1d3c1 JW |
10278 | zext_patch[0] = insn; |
10279 | zext_patch[1].dst_reg = insn.dst_reg; | |
10280 | zext_patch[1].src_reg = insn.dst_reg; | |
d6c2308c JW |
10281 | patch = zext_patch; |
10282 | patch_len = 2; | |
10283 | apply_patch_buffer: | |
10284 | new_prog = bpf_patch_insn_data(env, adj_idx, patch, patch_len); | |
a4b1d3c1 JW |
10285 | if (!new_prog) |
10286 | return -ENOMEM; | |
10287 | env->prog = new_prog; | |
10288 | insns = new_prog->insnsi; | |
10289 | aux = env->insn_aux_data; | |
d6c2308c | 10290 | delta += patch_len - 1; |
a4b1d3c1 JW |
10291 | } |
10292 | ||
10293 | return 0; | |
10294 | } | |
10295 | ||
c64b7983 JS |
10296 | /* convert load instructions that access fields of a context type into a |
10297 | * sequence of instructions that access fields of the underlying structure: | |
10298 | * struct __sk_buff -> struct sk_buff | |
10299 | * struct bpf_sock_ops -> struct sock | |
9bac3d6d | 10300 | */ |
58e2af8b | 10301 | static int convert_ctx_accesses(struct bpf_verifier_env *env) |
9bac3d6d | 10302 | { |
00176a34 | 10303 | const struct bpf_verifier_ops *ops = env->ops; |
f96da094 | 10304 | int i, cnt, size, ctx_field_size, delta = 0; |
3df126f3 | 10305 | const int insn_cnt = env->prog->len; |
36bbef52 | 10306 | struct bpf_insn insn_buf[16], *insn; |
46f53a65 | 10307 | u32 target_size, size_default, off; |
9bac3d6d | 10308 | struct bpf_prog *new_prog; |
d691f9e8 | 10309 | enum bpf_access_type type; |
f96da094 | 10310 | bool is_narrower_load; |
9bac3d6d | 10311 | |
b09928b9 DB |
10312 | if (ops->gen_prologue || env->seen_direct_write) { |
10313 | if (!ops->gen_prologue) { | |
10314 | verbose(env, "bpf verifier is misconfigured\n"); | |
10315 | return -EINVAL; | |
10316 | } | |
36bbef52 DB |
10317 | cnt = ops->gen_prologue(insn_buf, env->seen_direct_write, |
10318 | env->prog); | |
10319 | if (cnt >= ARRAY_SIZE(insn_buf)) { | |
61bd5218 | 10320 | verbose(env, "bpf verifier is misconfigured\n"); |
36bbef52 DB |
10321 | return -EINVAL; |
10322 | } else if (cnt) { | |
8041902d | 10323 | new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt); |
36bbef52 DB |
10324 | if (!new_prog) |
10325 | return -ENOMEM; | |
8041902d | 10326 | |
36bbef52 | 10327 | env->prog = new_prog; |
3df126f3 | 10328 | delta += cnt - 1; |
36bbef52 DB |
10329 | } |
10330 | } | |
10331 | ||
c64b7983 | 10332 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
9bac3d6d AS |
10333 | return 0; |
10334 | ||
3df126f3 | 10335 | insn = env->prog->insnsi + delta; |
36bbef52 | 10336 | |
9bac3d6d | 10337 | for (i = 0; i < insn_cnt; i++, insn++) { |
c64b7983 JS |
10338 | bpf_convert_ctx_access_t convert_ctx_access; |
10339 | ||
62c7989b DB |
10340 | if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) || |
10341 | insn->code == (BPF_LDX | BPF_MEM | BPF_H) || | |
10342 | insn->code == (BPF_LDX | BPF_MEM | BPF_W) || | |
ea2e7ce5 | 10343 | insn->code == (BPF_LDX | BPF_MEM | BPF_DW)) |
d691f9e8 | 10344 | type = BPF_READ; |
62c7989b DB |
10345 | else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) || |
10346 | insn->code == (BPF_STX | BPF_MEM | BPF_H) || | |
10347 | insn->code == (BPF_STX | BPF_MEM | BPF_W) || | |
ea2e7ce5 | 10348 | insn->code == (BPF_STX | BPF_MEM | BPF_DW)) |
d691f9e8 AS |
10349 | type = BPF_WRITE; |
10350 | else | |
9bac3d6d AS |
10351 | continue; |
10352 | ||
af86ca4e AS |
10353 | if (type == BPF_WRITE && |
10354 | env->insn_aux_data[i + delta].sanitize_stack_off) { | |
10355 | struct bpf_insn patch[] = { | |
10356 | /* Sanitize suspicious stack slot with zero. | |
10357 | * There are no memory dependencies for this store, | |
10358 | * since it's only using frame pointer and immediate | |
10359 | * constant of zero | |
10360 | */ | |
10361 | BPF_ST_MEM(BPF_DW, BPF_REG_FP, | |
10362 | env->insn_aux_data[i + delta].sanitize_stack_off, | |
10363 | 0), | |
10364 | /* the original STX instruction will immediately | |
10365 | * overwrite the same stack slot with appropriate value | |
10366 | */ | |
10367 | *insn, | |
10368 | }; | |
10369 | ||
10370 | cnt = ARRAY_SIZE(patch); | |
10371 | new_prog = bpf_patch_insn_data(env, i + delta, patch, cnt); | |
10372 | if (!new_prog) | |
10373 | return -ENOMEM; | |
10374 | ||
10375 | delta += cnt - 1; | |
10376 | env->prog = new_prog; | |
10377 | insn = new_prog->insnsi + i + delta; | |
10378 | continue; | |
10379 | } | |
10380 | ||
c64b7983 JS |
10381 | switch (env->insn_aux_data[i + delta].ptr_type) { |
10382 | case PTR_TO_CTX: | |
10383 | if (!ops->convert_ctx_access) | |
10384 | continue; | |
10385 | convert_ctx_access = ops->convert_ctx_access; | |
10386 | break; | |
10387 | case PTR_TO_SOCKET: | |
46f8bc92 | 10388 | case PTR_TO_SOCK_COMMON: |
c64b7983 JS |
10389 | convert_ctx_access = bpf_sock_convert_ctx_access; |
10390 | break; | |
655a51e5 MKL |
10391 | case PTR_TO_TCP_SOCK: |
10392 | convert_ctx_access = bpf_tcp_sock_convert_ctx_access; | |
10393 | break; | |
fada7fdc JL |
10394 | case PTR_TO_XDP_SOCK: |
10395 | convert_ctx_access = bpf_xdp_sock_convert_ctx_access; | |
10396 | break; | |
2a02759e | 10397 | case PTR_TO_BTF_ID: |
27ae7997 MKL |
10398 | if (type == BPF_READ) { |
10399 | insn->code = BPF_LDX | BPF_PROBE_MEM | | |
10400 | BPF_SIZE((insn)->code); | |
10401 | env->prog->aux->num_exentries++; | |
7e40781c | 10402 | } else if (resolve_prog_type(env->prog) != BPF_PROG_TYPE_STRUCT_OPS) { |
2a02759e AS |
10403 | verbose(env, "Writes through BTF pointers are not allowed\n"); |
10404 | return -EINVAL; | |
10405 | } | |
2a02759e | 10406 | continue; |
c64b7983 | 10407 | default: |
9bac3d6d | 10408 | continue; |
c64b7983 | 10409 | } |
9bac3d6d | 10410 | |
31fd8581 | 10411 | ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size; |
f96da094 | 10412 | size = BPF_LDST_BYTES(insn); |
31fd8581 YS |
10413 | |
10414 | /* If the read access is a narrower load of the field, | |
10415 | * convert to a 4/8-byte load, to minimum program type specific | |
10416 | * convert_ctx_access changes. If conversion is successful, | |
10417 | * we will apply proper mask to the result. | |
10418 | */ | |
f96da094 | 10419 | is_narrower_load = size < ctx_field_size; |
46f53a65 AI |
10420 | size_default = bpf_ctx_off_adjust_machine(ctx_field_size); |
10421 | off = insn->off; | |
31fd8581 | 10422 | if (is_narrower_load) { |
f96da094 DB |
10423 | u8 size_code; |
10424 | ||
10425 | if (type == BPF_WRITE) { | |
61bd5218 | 10426 | verbose(env, "bpf verifier narrow ctx access misconfigured\n"); |
f96da094 DB |
10427 | return -EINVAL; |
10428 | } | |
31fd8581 | 10429 | |
f96da094 | 10430 | size_code = BPF_H; |
31fd8581 YS |
10431 | if (ctx_field_size == 4) |
10432 | size_code = BPF_W; | |
10433 | else if (ctx_field_size == 8) | |
10434 | size_code = BPF_DW; | |
f96da094 | 10435 | |
bc23105c | 10436 | insn->off = off & ~(size_default - 1); |
31fd8581 YS |
10437 | insn->code = BPF_LDX | BPF_MEM | size_code; |
10438 | } | |
f96da094 DB |
10439 | |
10440 | target_size = 0; | |
c64b7983 JS |
10441 | cnt = convert_ctx_access(type, insn, insn_buf, env->prog, |
10442 | &target_size); | |
f96da094 DB |
10443 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) || |
10444 | (ctx_field_size && !target_size)) { | |
61bd5218 | 10445 | verbose(env, "bpf verifier is misconfigured\n"); |
9bac3d6d AS |
10446 | return -EINVAL; |
10447 | } | |
f96da094 DB |
10448 | |
10449 | if (is_narrower_load && size < target_size) { | |
d895a0f1 IL |
10450 | u8 shift = bpf_ctx_narrow_access_offset( |
10451 | off, size, size_default) * 8; | |
46f53a65 AI |
10452 | if (ctx_field_size <= 4) { |
10453 | if (shift) | |
10454 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_RSH, | |
10455 | insn->dst_reg, | |
10456 | shift); | |
31fd8581 | 10457 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg, |
f96da094 | 10458 | (1 << size * 8) - 1); |
46f53a65 AI |
10459 | } else { |
10460 | if (shift) | |
10461 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_RSH, | |
10462 | insn->dst_reg, | |
10463 | shift); | |
31fd8581 | 10464 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg, |
e2f7fc0a | 10465 | (1ULL << size * 8) - 1); |
46f53a65 | 10466 | } |
31fd8581 | 10467 | } |
9bac3d6d | 10468 | |
8041902d | 10469 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
9bac3d6d AS |
10470 | if (!new_prog) |
10471 | return -ENOMEM; | |
10472 | ||
3df126f3 | 10473 | delta += cnt - 1; |
9bac3d6d AS |
10474 | |
10475 | /* keep walking new program and skip insns we just inserted */ | |
10476 | env->prog = new_prog; | |
3df126f3 | 10477 | insn = new_prog->insnsi + i + delta; |
9bac3d6d AS |
10478 | } |
10479 | ||
10480 | return 0; | |
10481 | } | |
10482 | ||
1c2a088a AS |
10483 | static int jit_subprogs(struct bpf_verifier_env *env) |
10484 | { | |
10485 | struct bpf_prog *prog = env->prog, **func, *tmp; | |
10486 | int i, j, subprog_start, subprog_end = 0, len, subprog; | |
a748c697 | 10487 | struct bpf_map *map_ptr; |
7105e828 | 10488 | struct bpf_insn *insn; |
1c2a088a | 10489 | void *old_bpf_func; |
c4c0bdc0 | 10490 | int err, num_exentries; |
1c2a088a | 10491 | |
f910cefa | 10492 | if (env->subprog_cnt <= 1) |
1c2a088a AS |
10493 | return 0; |
10494 | ||
7105e828 | 10495 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
1c2a088a AS |
10496 | if (insn->code != (BPF_JMP | BPF_CALL) || |
10497 | insn->src_reg != BPF_PSEUDO_CALL) | |
10498 | continue; | |
c7a89784 DB |
10499 | /* Upon error here we cannot fall back to interpreter but |
10500 | * need a hard reject of the program. Thus -EFAULT is | |
10501 | * propagated in any case. | |
10502 | */ | |
1c2a088a AS |
10503 | subprog = find_subprog(env, i + insn->imm + 1); |
10504 | if (subprog < 0) { | |
10505 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", | |
10506 | i + insn->imm + 1); | |
10507 | return -EFAULT; | |
10508 | } | |
10509 | /* temporarily remember subprog id inside insn instead of | |
10510 | * aux_data, since next loop will split up all insns into funcs | |
10511 | */ | |
f910cefa | 10512 | insn->off = subprog; |
1c2a088a AS |
10513 | /* remember original imm in case JIT fails and fallback |
10514 | * to interpreter will be needed | |
10515 | */ | |
10516 | env->insn_aux_data[i].call_imm = insn->imm; | |
10517 | /* point imm to __bpf_call_base+1 from JITs point of view */ | |
10518 | insn->imm = 1; | |
10519 | } | |
10520 | ||
c454a46b MKL |
10521 | err = bpf_prog_alloc_jited_linfo(prog); |
10522 | if (err) | |
10523 | goto out_undo_insn; | |
10524 | ||
10525 | err = -ENOMEM; | |
6396bb22 | 10526 | func = kcalloc(env->subprog_cnt, sizeof(prog), GFP_KERNEL); |
1c2a088a | 10527 | if (!func) |
c7a89784 | 10528 | goto out_undo_insn; |
1c2a088a | 10529 | |
f910cefa | 10530 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a | 10531 | subprog_start = subprog_end; |
4cb3d99c | 10532 | subprog_end = env->subprog_info[i + 1].start; |
1c2a088a AS |
10533 | |
10534 | len = subprog_end - subprog_start; | |
492ecee8 AS |
10535 | /* BPF_PROG_RUN doesn't call subprogs directly, |
10536 | * hence main prog stats include the runtime of subprogs. | |
10537 | * subprogs don't have IDs and not reachable via prog_get_next_id | |
10538 | * func[i]->aux->stats will never be accessed and stays NULL | |
10539 | */ | |
10540 | func[i] = bpf_prog_alloc_no_stats(bpf_prog_size(len), GFP_USER); | |
1c2a088a AS |
10541 | if (!func[i]) |
10542 | goto out_free; | |
10543 | memcpy(func[i]->insnsi, &prog->insnsi[subprog_start], | |
10544 | len * sizeof(struct bpf_insn)); | |
4f74d809 | 10545 | func[i]->type = prog->type; |
1c2a088a | 10546 | func[i]->len = len; |
4f74d809 DB |
10547 | if (bpf_prog_calc_tag(func[i])) |
10548 | goto out_free; | |
1c2a088a | 10549 | func[i]->is_func = 1; |
ba64e7d8 YS |
10550 | func[i]->aux->func_idx = i; |
10551 | /* the btf and func_info will be freed only at prog->aux */ | |
10552 | func[i]->aux->btf = prog->aux->btf; | |
10553 | func[i]->aux->func_info = prog->aux->func_info; | |
10554 | ||
a748c697 MF |
10555 | for (j = 0; j < prog->aux->size_poke_tab; j++) { |
10556 | u32 insn_idx = prog->aux->poke_tab[j].insn_idx; | |
10557 | int ret; | |
10558 | ||
10559 | if (!(insn_idx >= subprog_start && | |
10560 | insn_idx <= subprog_end)) | |
10561 | continue; | |
10562 | ||
10563 | ret = bpf_jit_add_poke_descriptor(func[i], | |
10564 | &prog->aux->poke_tab[j]); | |
10565 | if (ret < 0) { | |
10566 | verbose(env, "adding tail call poke descriptor failed\n"); | |
10567 | goto out_free; | |
10568 | } | |
10569 | ||
10570 | func[i]->insnsi[insn_idx - subprog_start].imm = ret + 1; | |
10571 | ||
10572 | map_ptr = func[i]->aux->poke_tab[ret].tail_call.map; | |
10573 | ret = map_ptr->ops->map_poke_track(map_ptr, func[i]->aux); | |
10574 | if (ret < 0) { | |
10575 | verbose(env, "tracking tail call prog failed\n"); | |
10576 | goto out_free; | |
10577 | } | |
10578 | } | |
10579 | ||
1c2a088a AS |
10580 | /* Use bpf_prog_F_tag to indicate functions in stack traces. |
10581 | * Long term would need debug info to populate names | |
10582 | */ | |
10583 | func[i]->aux->name[0] = 'F'; | |
9c8105bd | 10584 | func[i]->aux->stack_depth = env->subprog_info[i].stack_depth; |
1c2a088a | 10585 | func[i]->jit_requested = 1; |
c454a46b MKL |
10586 | func[i]->aux->linfo = prog->aux->linfo; |
10587 | func[i]->aux->nr_linfo = prog->aux->nr_linfo; | |
10588 | func[i]->aux->jited_linfo = prog->aux->jited_linfo; | |
10589 | func[i]->aux->linfo_idx = env->subprog_info[i].linfo_idx; | |
c4c0bdc0 YS |
10590 | num_exentries = 0; |
10591 | insn = func[i]->insnsi; | |
10592 | for (j = 0; j < func[i]->len; j++, insn++) { | |
10593 | if (BPF_CLASS(insn->code) == BPF_LDX && | |
10594 | BPF_MODE(insn->code) == BPF_PROBE_MEM) | |
10595 | num_exentries++; | |
10596 | } | |
10597 | func[i]->aux->num_exentries = num_exentries; | |
ebf7d1f5 | 10598 | func[i]->aux->tail_call_reachable = env->subprog_info[i].tail_call_reachable; |
1c2a088a AS |
10599 | func[i] = bpf_int_jit_compile(func[i]); |
10600 | if (!func[i]->jited) { | |
10601 | err = -ENOTSUPP; | |
10602 | goto out_free; | |
10603 | } | |
10604 | cond_resched(); | |
10605 | } | |
a748c697 MF |
10606 | |
10607 | /* Untrack main program's aux structs so that during map_poke_run() | |
10608 | * we will not stumble upon the unfilled poke descriptors; each | |
10609 | * of the main program's poke descs got distributed across subprogs | |
10610 | * and got tracked onto map, so we are sure that none of them will | |
10611 | * be missed after the operation below | |
10612 | */ | |
10613 | for (i = 0; i < prog->aux->size_poke_tab; i++) { | |
10614 | map_ptr = prog->aux->poke_tab[i].tail_call.map; | |
10615 | ||
10616 | map_ptr->ops->map_poke_untrack(map_ptr, prog->aux); | |
10617 | } | |
10618 | ||
1c2a088a AS |
10619 | /* at this point all bpf functions were successfully JITed |
10620 | * now populate all bpf_calls with correct addresses and | |
10621 | * run last pass of JIT | |
10622 | */ | |
f910cefa | 10623 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a AS |
10624 | insn = func[i]->insnsi; |
10625 | for (j = 0; j < func[i]->len; j++, insn++) { | |
10626 | if (insn->code != (BPF_JMP | BPF_CALL) || | |
10627 | insn->src_reg != BPF_PSEUDO_CALL) | |
10628 | continue; | |
10629 | subprog = insn->off; | |
0d306c31 PB |
10630 | insn->imm = BPF_CAST_CALL(func[subprog]->bpf_func) - |
10631 | __bpf_call_base; | |
1c2a088a | 10632 | } |
2162fed4 SD |
10633 | |
10634 | /* we use the aux data to keep a list of the start addresses | |
10635 | * of the JITed images for each function in the program | |
10636 | * | |
10637 | * for some architectures, such as powerpc64, the imm field | |
10638 | * might not be large enough to hold the offset of the start | |
10639 | * address of the callee's JITed image from __bpf_call_base | |
10640 | * | |
10641 | * in such cases, we can lookup the start address of a callee | |
10642 | * by using its subprog id, available from the off field of | |
10643 | * the call instruction, as an index for this list | |
10644 | */ | |
10645 | func[i]->aux->func = func; | |
10646 | func[i]->aux->func_cnt = env->subprog_cnt; | |
1c2a088a | 10647 | } |
f910cefa | 10648 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a AS |
10649 | old_bpf_func = func[i]->bpf_func; |
10650 | tmp = bpf_int_jit_compile(func[i]); | |
10651 | if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) { | |
10652 | verbose(env, "JIT doesn't support bpf-to-bpf calls\n"); | |
c7a89784 | 10653 | err = -ENOTSUPP; |
1c2a088a AS |
10654 | goto out_free; |
10655 | } | |
10656 | cond_resched(); | |
10657 | } | |
10658 | ||
10659 | /* finally lock prog and jit images for all functions and | |
10660 | * populate kallsysm | |
10661 | */ | |
f910cefa | 10662 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a AS |
10663 | bpf_prog_lock_ro(func[i]); |
10664 | bpf_prog_kallsyms_add(func[i]); | |
10665 | } | |
7105e828 DB |
10666 | |
10667 | /* Last step: make now unused interpreter insns from main | |
10668 | * prog consistent for later dump requests, so they can | |
10669 | * later look the same as if they were interpreted only. | |
10670 | */ | |
10671 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { | |
7105e828 DB |
10672 | if (insn->code != (BPF_JMP | BPF_CALL) || |
10673 | insn->src_reg != BPF_PSEUDO_CALL) | |
10674 | continue; | |
10675 | insn->off = env->insn_aux_data[i].call_imm; | |
10676 | subprog = find_subprog(env, i + insn->off + 1); | |
dbecd738 | 10677 | insn->imm = subprog; |
7105e828 DB |
10678 | } |
10679 | ||
1c2a088a AS |
10680 | prog->jited = 1; |
10681 | prog->bpf_func = func[0]->bpf_func; | |
10682 | prog->aux->func = func; | |
f910cefa | 10683 | prog->aux->func_cnt = env->subprog_cnt; |
c454a46b | 10684 | bpf_prog_free_unused_jited_linfo(prog); |
1c2a088a AS |
10685 | return 0; |
10686 | out_free: | |
a748c697 MF |
10687 | for (i = 0; i < env->subprog_cnt; i++) { |
10688 | if (!func[i]) | |
10689 | continue; | |
10690 | ||
10691 | for (j = 0; j < func[i]->aux->size_poke_tab; j++) { | |
10692 | map_ptr = func[i]->aux->poke_tab[j].tail_call.map; | |
10693 | map_ptr->ops->map_poke_untrack(map_ptr, func[i]->aux); | |
10694 | } | |
10695 | bpf_jit_free(func[i]); | |
10696 | } | |
1c2a088a | 10697 | kfree(func); |
c7a89784 | 10698 | out_undo_insn: |
1c2a088a AS |
10699 | /* cleanup main prog to be interpreted */ |
10700 | prog->jit_requested = 0; | |
10701 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { | |
10702 | if (insn->code != (BPF_JMP | BPF_CALL) || | |
10703 | insn->src_reg != BPF_PSEUDO_CALL) | |
10704 | continue; | |
10705 | insn->off = 0; | |
10706 | insn->imm = env->insn_aux_data[i].call_imm; | |
10707 | } | |
c454a46b | 10708 | bpf_prog_free_jited_linfo(prog); |
1c2a088a AS |
10709 | return err; |
10710 | } | |
10711 | ||
1ea47e01 AS |
10712 | static int fixup_call_args(struct bpf_verifier_env *env) |
10713 | { | |
19d28fbd | 10714 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
1ea47e01 AS |
10715 | struct bpf_prog *prog = env->prog; |
10716 | struct bpf_insn *insn = prog->insnsi; | |
10717 | int i, depth; | |
19d28fbd | 10718 | #endif |
e4052d06 | 10719 | int err = 0; |
1ea47e01 | 10720 | |
e4052d06 QM |
10721 | if (env->prog->jit_requested && |
10722 | !bpf_prog_is_dev_bound(env->prog->aux)) { | |
19d28fbd DM |
10723 | err = jit_subprogs(env); |
10724 | if (err == 0) | |
1c2a088a | 10725 | return 0; |
c7a89784 DB |
10726 | if (err == -EFAULT) |
10727 | return err; | |
19d28fbd DM |
10728 | } |
10729 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON | |
e411901c MF |
10730 | if (env->subprog_cnt > 1 && env->prog->aux->tail_call_reachable) { |
10731 | /* When JIT fails the progs with bpf2bpf calls and tail_calls | |
10732 | * have to be rejected, since interpreter doesn't support them yet. | |
10733 | */ | |
10734 | verbose(env, "tail_calls are not allowed in non-JITed programs with bpf-to-bpf calls\n"); | |
10735 | return -EINVAL; | |
10736 | } | |
1ea47e01 AS |
10737 | for (i = 0; i < prog->len; i++, insn++) { |
10738 | if (insn->code != (BPF_JMP | BPF_CALL) || | |
10739 | insn->src_reg != BPF_PSEUDO_CALL) | |
10740 | continue; | |
10741 | depth = get_callee_stack_depth(env, insn, i); | |
10742 | if (depth < 0) | |
10743 | return depth; | |
10744 | bpf_patch_call_args(insn, depth); | |
10745 | } | |
19d28fbd DM |
10746 | err = 0; |
10747 | #endif | |
10748 | return err; | |
1ea47e01 AS |
10749 | } |
10750 | ||
79741b3b | 10751 | /* fixup insn->imm field of bpf_call instructions |
81ed18ab | 10752 | * and inline eligible helpers as explicit sequence of BPF instructions |
e245c5c6 AS |
10753 | * |
10754 | * this function is called after eBPF program passed verification | |
10755 | */ | |
79741b3b | 10756 | static int fixup_bpf_calls(struct bpf_verifier_env *env) |
e245c5c6 | 10757 | { |
79741b3b | 10758 | struct bpf_prog *prog = env->prog; |
d2e4c1e6 | 10759 | bool expect_blinding = bpf_jit_blinding_enabled(prog); |
79741b3b | 10760 | struct bpf_insn *insn = prog->insnsi; |
e245c5c6 | 10761 | const struct bpf_func_proto *fn; |
79741b3b | 10762 | const int insn_cnt = prog->len; |
09772d92 | 10763 | const struct bpf_map_ops *ops; |
c93552c4 | 10764 | struct bpf_insn_aux_data *aux; |
81ed18ab AS |
10765 | struct bpf_insn insn_buf[16]; |
10766 | struct bpf_prog *new_prog; | |
10767 | struct bpf_map *map_ptr; | |
d2e4c1e6 | 10768 | int i, ret, cnt, delta = 0; |
e245c5c6 | 10769 | |
79741b3b | 10770 | for (i = 0; i < insn_cnt; i++, insn++) { |
f6b1b3bf DB |
10771 | if (insn->code == (BPF_ALU64 | BPF_MOD | BPF_X) || |
10772 | insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) || | |
10773 | insn->code == (BPF_ALU | BPF_MOD | BPF_X) || | |
68fda450 | 10774 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { |
f6b1b3bf DB |
10775 | bool is64 = BPF_CLASS(insn->code) == BPF_ALU64; |
10776 | struct bpf_insn mask_and_div[] = { | |
10777 | BPF_MOV32_REG(insn->src_reg, insn->src_reg), | |
10778 | /* Rx div 0 -> 0 */ | |
10779 | BPF_JMP_IMM(BPF_JNE, insn->src_reg, 0, 2), | |
10780 | BPF_ALU32_REG(BPF_XOR, insn->dst_reg, insn->dst_reg), | |
10781 | BPF_JMP_IMM(BPF_JA, 0, 0, 1), | |
10782 | *insn, | |
10783 | }; | |
10784 | struct bpf_insn mask_and_mod[] = { | |
10785 | BPF_MOV32_REG(insn->src_reg, insn->src_reg), | |
10786 | /* Rx mod 0 -> Rx */ | |
10787 | BPF_JMP_IMM(BPF_JEQ, insn->src_reg, 0, 1), | |
10788 | *insn, | |
10789 | }; | |
10790 | struct bpf_insn *patchlet; | |
10791 | ||
10792 | if (insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) || | |
10793 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { | |
10794 | patchlet = mask_and_div + (is64 ? 1 : 0); | |
10795 | cnt = ARRAY_SIZE(mask_and_div) - (is64 ? 1 : 0); | |
10796 | } else { | |
10797 | patchlet = mask_and_mod + (is64 ? 1 : 0); | |
10798 | cnt = ARRAY_SIZE(mask_and_mod) - (is64 ? 1 : 0); | |
10799 | } | |
10800 | ||
10801 | new_prog = bpf_patch_insn_data(env, i + delta, patchlet, cnt); | |
68fda450 AS |
10802 | if (!new_prog) |
10803 | return -ENOMEM; | |
10804 | ||
10805 | delta += cnt - 1; | |
10806 | env->prog = prog = new_prog; | |
10807 | insn = new_prog->insnsi + i + delta; | |
10808 | continue; | |
10809 | } | |
10810 | ||
e0cea7ce DB |
10811 | if (BPF_CLASS(insn->code) == BPF_LD && |
10812 | (BPF_MODE(insn->code) == BPF_ABS || | |
10813 | BPF_MODE(insn->code) == BPF_IND)) { | |
10814 | cnt = env->ops->gen_ld_abs(insn, insn_buf); | |
10815 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { | |
10816 | verbose(env, "bpf verifier is misconfigured\n"); | |
10817 | return -EINVAL; | |
10818 | } | |
10819 | ||
10820 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
10821 | if (!new_prog) | |
10822 | return -ENOMEM; | |
10823 | ||
10824 | delta += cnt - 1; | |
10825 | env->prog = prog = new_prog; | |
10826 | insn = new_prog->insnsi + i + delta; | |
10827 | continue; | |
10828 | } | |
10829 | ||
979d63d5 DB |
10830 | if (insn->code == (BPF_ALU64 | BPF_ADD | BPF_X) || |
10831 | insn->code == (BPF_ALU64 | BPF_SUB | BPF_X)) { | |
10832 | const u8 code_add = BPF_ALU64 | BPF_ADD | BPF_X; | |
10833 | const u8 code_sub = BPF_ALU64 | BPF_SUB | BPF_X; | |
10834 | struct bpf_insn insn_buf[16]; | |
10835 | struct bpf_insn *patch = &insn_buf[0]; | |
10836 | bool issrc, isneg; | |
10837 | u32 off_reg; | |
10838 | ||
10839 | aux = &env->insn_aux_data[i + delta]; | |
3612af78 DB |
10840 | if (!aux->alu_state || |
10841 | aux->alu_state == BPF_ALU_NON_POINTER) | |
979d63d5 DB |
10842 | continue; |
10843 | ||
10844 | isneg = aux->alu_state & BPF_ALU_NEG_VALUE; | |
10845 | issrc = (aux->alu_state & BPF_ALU_SANITIZE) == | |
10846 | BPF_ALU_SANITIZE_SRC; | |
10847 | ||
10848 | off_reg = issrc ? insn->src_reg : insn->dst_reg; | |
10849 | if (isneg) | |
10850 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); | |
10851 | *patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit - 1); | |
10852 | *patch++ = BPF_ALU64_REG(BPF_SUB, BPF_REG_AX, off_reg); | |
10853 | *patch++ = BPF_ALU64_REG(BPF_OR, BPF_REG_AX, off_reg); | |
10854 | *patch++ = BPF_ALU64_IMM(BPF_NEG, BPF_REG_AX, 0); | |
10855 | *patch++ = BPF_ALU64_IMM(BPF_ARSH, BPF_REG_AX, 63); | |
10856 | if (issrc) { | |
10857 | *patch++ = BPF_ALU64_REG(BPF_AND, BPF_REG_AX, | |
10858 | off_reg); | |
10859 | insn->src_reg = BPF_REG_AX; | |
10860 | } else { | |
10861 | *patch++ = BPF_ALU64_REG(BPF_AND, off_reg, | |
10862 | BPF_REG_AX); | |
10863 | } | |
10864 | if (isneg) | |
10865 | insn->code = insn->code == code_add ? | |
10866 | code_sub : code_add; | |
10867 | *patch++ = *insn; | |
10868 | if (issrc && isneg) | |
10869 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); | |
10870 | cnt = patch - insn_buf; | |
10871 | ||
10872 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
10873 | if (!new_prog) | |
10874 | return -ENOMEM; | |
10875 | ||
10876 | delta += cnt - 1; | |
10877 | env->prog = prog = new_prog; | |
10878 | insn = new_prog->insnsi + i + delta; | |
10879 | continue; | |
10880 | } | |
10881 | ||
79741b3b AS |
10882 | if (insn->code != (BPF_JMP | BPF_CALL)) |
10883 | continue; | |
cc8b0b92 AS |
10884 | if (insn->src_reg == BPF_PSEUDO_CALL) |
10885 | continue; | |
e245c5c6 | 10886 | |
79741b3b AS |
10887 | if (insn->imm == BPF_FUNC_get_route_realm) |
10888 | prog->dst_needed = 1; | |
10889 | if (insn->imm == BPF_FUNC_get_prandom_u32) | |
10890 | bpf_user_rnd_init_once(); | |
9802d865 JB |
10891 | if (insn->imm == BPF_FUNC_override_return) |
10892 | prog->kprobe_override = 1; | |
79741b3b | 10893 | if (insn->imm == BPF_FUNC_tail_call) { |
7b9f6da1 DM |
10894 | /* If we tail call into other programs, we |
10895 | * cannot make any assumptions since they can | |
10896 | * be replaced dynamically during runtime in | |
10897 | * the program array. | |
10898 | */ | |
10899 | prog->cb_access = 1; | |
e411901c MF |
10900 | if (!allow_tail_call_in_subprogs(env)) |
10901 | prog->aux->stack_depth = MAX_BPF_STACK; | |
10902 | prog->aux->max_pkt_offset = MAX_PACKET_OFF; | |
7b9f6da1 | 10903 | |
79741b3b AS |
10904 | /* mark bpf_tail_call as different opcode to avoid |
10905 | * conditional branch in the interpeter for every normal | |
10906 | * call and to prevent accidental JITing by JIT compiler | |
10907 | * that doesn't support bpf_tail_call yet | |
e245c5c6 | 10908 | */ |
79741b3b | 10909 | insn->imm = 0; |
71189fa9 | 10910 | insn->code = BPF_JMP | BPF_TAIL_CALL; |
b2157399 | 10911 | |
c93552c4 | 10912 | aux = &env->insn_aux_data[i + delta]; |
2c78ee89 | 10913 | if (env->bpf_capable && !expect_blinding && |
cc52d914 | 10914 | prog->jit_requested && |
d2e4c1e6 DB |
10915 | !bpf_map_key_poisoned(aux) && |
10916 | !bpf_map_ptr_poisoned(aux) && | |
10917 | !bpf_map_ptr_unpriv(aux)) { | |
10918 | struct bpf_jit_poke_descriptor desc = { | |
10919 | .reason = BPF_POKE_REASON_TAIL_CALL, | |
10920 | .tail_call.map = BPF_MAP_PTR(aux->map_ptr_state), | |
10921 | .tail_call.key = bpf_map_key_immediate(aux), | |
a748c697 | 10922 | .insn_idx = i + delta, |
d2e4c1e6 DB |
10923 | }; |
10924 | ||
10925 | ret = bpf_jit_add_poke_descriptor(prog, &desc); | |
10926 | if (ret < 0) { | |
10927 | verbose(env, "adding tail call poke descriptor failed\n"); | |
10928 | return ret; | |
10929 | } | |
10930 | ||
10931 | insn->imm = ret + 1; | |
10932 | continue; | |
10933 | } | |
10934 | ||
c93552c4 DB |
10935 | if (!bpf_map_ptr_unpriv(aux)) |
10936 | continue; | |
10937 | ||
b2157399 AS |
10938 | /* instead of changing every JIT dealing with tail_call |
10939 | * emit two extra insns: | |
10940 | * if (index >= max_entries) goto out; | |
10941 | * index &= array->index_mask; | |
10942 | * to avoid out-of-bounds cpu speculation | |
10943 | */ | |
c93552c4 | 10944 | if (bpf_map_ptr_poisoned(aux)) { |
40950343 | 10945 | verbose(env, "tail_call abusing map_ptr\n"); |
b2157399 AS |
10946 | return -EINVAL; |
10947 | } | |
c93552c4 | 10948 | |
d2e4c1e6 | 10949 | map_ptr = BPF_MAP_PTR(aux->map_ptr_state); |
b2157399 AS |
10950 | insn_buf[0] = BPF_JMP_IMM(BPF_JGE, BPF_REG_3, |
10951 | map_ptr->max_entries, 2); | |
10952 | insn_buf[1] = BPF_ALU32_IMM(BPF_AND, BPF_REG_3, | |
10953 | container_of(map_ptr, | |
10954 | struct bpf_array, | |
10955 | map)->index_mask); | |
10956 | insn_buf[2] = *insn; | |
10957 | cnt = 3; | |
10958 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
10959 | if (!new_prog) | |
10960 | return -ENOMEM; | |
10961 | ||
10962 | delta += cnt - 1; | |
10963 | env->prog = prog = new_prog; | |
10964 | insn = new_prog->insnsi + i + delta; | |
79741b3b AS |
10965 | continue; |
10966 | } | |
e245c5c6 | 10967 | |
89c63074 | 10968 | /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup |
09772d92 DB |
10969 | * and other inlining handlers are currently limited to 64 bit |
10970 | * only. | |
89c63074 | 10971 | */ |
60b58afc | 10972 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
09772d92 DB |
10973 | (insn->imm == BPF_FUNC_map_lookup_elem || |
10974 | insn->imm == BPF_FUNC_map_update_elem || | |
84430d42 DB |
10975 | insn->imm == BPF_FUNC_map_delete_elem || |
10976 | insn->imm == BPF_FUNC_map_push_elem || | |
10977 | insn->imm == BPF_FUNC_map_pop_elem || | |
10978 | insn->imm == BPF_FUNC_map_peek_elem)) { | |
c93552c4 DB |
10979 | aux = &env->insn_aux_data[i + delta]; |
10980 | if (bpf_map_ptr_poisoned(aux)) | |
10981 | goto patch_call_imm; | |
10982 | ||
d2e4c1e6 | 10983 | map_ptr = BPF_MAP_PTR(aux->map_ptr_state); |
09772d92 DB |
10984 | ops = map_ptr->ops; |
10985 | if (insn->imm == BPF_FUNC_map_lookup_elem && | |
10986 | ops->map_gen_lookup) { | |
10987 | cnt = ops->map_gen_lookup(map_ptr, insn_buf); | |
10988 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { | |
10989 | verbose(env, "bpf verifier is misconfigured\n"); | |
10990 | return -EINVAL; | |
10991 | } | |
81ed18ab | 10992 | |
09772d92 DB |
10993 | new_prog = bpf_patch_insn_data(env, i + delta, |
10994 | insn_buf, cnt); | |
10995 | if (!new_prog) | |
10996 | return -ENOMEM; | |
81ed18ab | 10997 | |
09772d92 DB |
10998 | delta += cnt - 1; |
10999 | env->prog = prog = new_prog; | |
11000 | insn = new_prog->insnsi + i + delta; | |
11001 | continue; | |
11002 | } | |
81ed18ab | 11003 | |
09772d92 DB |
11004 | BUILD_BUG_ON(!__same_type(ops->map_lookup_elem, |
11005 | (void *(*)(struct bpf_map *map, void *key))NULL)); | |
11006 | BUILD_BUG_ON(!__same_type(ops->map_delete_elem, | |
11007 | (int (*)(struct bpf_map *map, void *key))NULL)); | |
11008 | BUILD_BUG_ON(!__same_type(ops->map_update_elem, | |
11009 | (int (*)(struct bpf_map *map, void *key, void *value, | |
11010 | u64 flags))NULL)); | |
84430d42 DB |
11011 | BUILD_BUG_ON(!__same_type(ops->map_push_elem, |
11012 | (int (*)(struct bpf_map *map, void *value, | |
11013 | u64 flags))NULL)); | |
11014 | BUILD_BUG_ON(!__same_type(ops->map_pop_elem, | |
11015 | (int (*)(struct bpf_map *map, void *value))NULL)); | |
11016 | BUILD_BUG_ON(!__same_type(ops->map_peek_elem, | |
11017 | (int (*)(struct bpf_map *map, void *value))NULL)); | |
11018 | ||
09772d92 DB |
11019 | switch (insn->imm) { |
11020 | case BPF_FUNC_map_lookup_elem: | |
11021 | insn->imm = BPF_CAST_CALL(ops->map_lookup_elem) - | |
11022 | __bpf_call_base; | |
11023 | continue; | |
11024 | case BPF_FUNC_map_update_elem: | |
11025 | insn->imm = BPF_CAST_CALL(ops->map_update_elem) - | |
11026 | __bpf_call_base; | |
11027 | continue; | |
11028 | case BPF_FUNC_map_delete_elem: | |
11029 | insn->imm = BPF_CAST_CALL(ops->map_delete_elem) - | |
11030 | __bpf_call_base; | |
11031 | continue; | |
84430d42 DB |
11032 | case BPF_FUNC_map_push_elem: |
11033 | insn->imm = BPF_CAST_CALL(ops->map_push_elem) - | |
11034 | __bpf_call_base; | |
11035 | continue; | |
11036 | case BPF_FUNC_map_pop_elem: | |
11037 | insn->imm = BPF_CAST_CALL(ops->map_pop_elem) - | |
11038 | __bpf_call_base; | |
11039 | continue; | |
11040 | case BPF_FUNC_map_peek_elem: | |
11041 | insn->imm = BPF_CAST_CALL(ops->map_peek_elem) - | |
11042 | __bpf_call_base; | |
11043 | continue; | |
09772d92 | 11044 | } |
81ed18ab | 11045 | |
09772d92 | 11046 | goto patch_call_imm; |
81ed18ab AS |
11047 | } |
11048 | ||
5576b991 MKL |
11049 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
11050 | insn->imm == BPF_FUNC_jiffies64) { | |
11051 | struct bpf_insn ld_jiffies_addr[2] = { | |
11052 | BPF_LD_IMM64(BPF_REG_0, | |
11053 | (unsigned long)&jiffies), | |
11054 | }; | |
11055 | ||
11056 | insn_buf[0] = ld_jiffies_addr[0]; | |
11057 | insn_buf[1] = ld_jiffies_addr[1]; | |
11058 | insn_buf[2] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, | |
11059 | BPF_REG_0, 0); | |
11060 | cnt = 3; | |
11061 | ||
11062 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, | |
11063 | cnt); | |
11064 | if (!new_prog) | |
11065 | return -ENOMEM; | |
11066 | ||
11067 | delta += cnt - 1; | |
11068 | env->prog = prog = new_prog; | |
11069 | insn = new_prog->insnsi + i + delta; | |
11070 | continue; | |
11071 | } | |
11072 | ||
81ed18ab | 11073 | patch_call_imm: |
5e43f899 | 11074 | fn = env->ops->get_func_proto(insn->imm, env->prog); |
79741b3b AS |
11075 | /* all functions that have prototype and verifier allowed |
11076 | * programs to call them, must be real in-kernel functions | |
11077 | */ | |
11078 | if (!fn->func) { | |
61bd5218 JK |
11079 | verbose(env, |
11080 | "kernel subsystem misconfigured func %s#%d\n", | |
79741b3b AS |
11081 | func_id_name(insn->imm), insn->imm); |
11082 | return -EFAULT; | |
e245c5c6 | 11083 | } |
79741b3b | 11084 | insn->imm = fn->func - __bpf_call_base; |
e245c5c6 | 11085 | } |
e245c5c6 | 11086 | |
d2e4c1e6 DB |
11087 | /* Since poke tab is now finalized, publish aux to tracker. */ |
11088 | for (i = 0; i < prog->aux->size_poke_tab; i++) { | |
11089 | map_ptr = prog->aux->poke_tab[i].tail_call.map; | |
11090 | if (!map_ptr->ops->map_poke_track || | |
11091 | !map_ptr->ops->map_poke_untrack || | |
11092 | !map_ptr->ops->map_poke_run) { | |
11093 | verbose(env, "bpf verifier is misconfigured\n"); | |
11094 | return -EINVAL; | |
11095 | } | |
11096 | ||
11097 | ret = map_ptr->ops->map_poke_track(map_ptr, prog->aux); | |
11098 | if (ret < 0) { | |
11099 | verbose(env, "tracking tail call prog failed\n"); | |
11100 | return ret; | |
11101 | } | |
11102 | } | |
11103 | ||
79741b3b AS |
11104 | return 0; |
11105 | } | |
e245c5c6 | 11106 | |
58e2af8b | 11107 | static void free_states(struct bpf_verifier_env *env) |
f1bca824 | 11108 | { |
58e2af8b | 11109 | struct bpf_verifier_state_list *sl, *sln; |
f1bca824 AS |
11110 | int i; |
11111 | ||
9f4686c4 AS |
11112 | sl = env->free_list; |
11113 | while (sl) { | |
11114 | sln = sl->next; | |
11115 | free_verifier_state(&sl->state, false); | |
11116 | kfree(sl); | |
11117 | sl = sln; | |
11118 | } | |
51c39bb1 | 11119 | env->free_list = NULL; |
9f4686c4 | 11120 | |
f1bca824 AS |
11121 | if (!env->explored_states) |
11122 | return; | |
11123 | ||
dc2a4ebc | 11124 | for (i = 0; i < state_htab_size(env); i++) { |
f1bca824 AS |
11125 | sl = env->explored_states[i]; |
11126 | ||
a8f500af AS |
11127 | while (sl) { |
11128 | sln = sl->next; | |
11129 | free_verifier_state(&sl->state, false); | |
11130 | kfree(sl); | |
11131 | sl = sln; | |
11132 | } | |
51c39bb1 | 11133 | env->explored_states[i] = NULL; |
f1bca824 | 11134 | } |
51c39bb1 | 11135 | } |
f1bca824 | 11136 | |
51c39bb1 AS |
11137 | /* The verifier is using insn_aux_data[] to store temporary data during |
11138 | * verification and to store information for passes that run after the | |
11139 | * verification like dead code sanitization. do_check_common() for subprogram N | |
11140 | * may analyze many other subprograms. sanitize_insn_aux_data() clears all | |
11141 | * temporary data after do_check_common() finds that subprogram N cannot be | |
11142 | * verified independently. pass_cnt counts the number of times | |
11143 | * do_check_common() was run and insn->aux->seen tells the pass number | |
11144 | * insn_aux_data was touched. These variables are compared to clear temporary | |
11145 | * data from failed pass. For testing and experiments do_check_common() can be | |
11146 | * run multiple times even when prior attempt to verify is unsuccessful. | |
11147 | */ | |
11148 | static void sanitize_insn_aux_data(struct bpf_verifier_env *env) | |
11149 | { | |
11150 | struct bpf_insn *insn = env->prog->insnsi; | |
11151 | struct bpf_insn_aux_data *aux; | |
11152 | int i, class; | |
11153 | ||
11154 | for (i = 0; i < env->prog->len; i++) { | |
11155 | class = BPF_CLASS(insn[i].code); | |
11156 | if (class != BPF_LDX && class != BPF_STX) | |
11157 | continue; | |
11158 | aux = &env->insn_aux_data[i]; | |
11159 | if (aux->seen != env->pass_cnt) | |
11160 | continue; | |
11161 | memset(aux, 0, offsetof(typeof(*aux), orig_idx)); | |
11162 | } | |
f1bca824 AS |
11163 | } |
11164 | ||
51c39bb1 AS |
11165 | static int do_check_common(struct bpf_verifier_env *env, int subprog) |
11166 | { | |
6f8a57cc | 11167 | bool pop_log = !(env->log.level & BPF_LOG_LEVEL2); |
51c39bb1 AS |
11168 | struct bpf_verifier_state *state; |
11169 | struct bpf_reg_state *regs; | |
11170 | int ret, i; | |
11171 | ||
11172 | env->prev_linfo = NULL; | |
11173 | env->pass_cnt++; | |
11174 | ||
11175 | state = kzalloc(sizeof(struct bpf_verifier_state), GFP_KERNEL); | |
11176 | if (!state) | |
11177 | return -ENOMEM; | |
11178 | state->curframe = 0; | |
11179 | state->speculative = false; | |
11180 | state->branches = 1; | |
11181 | state->frame[0] = kzalloc(sizeof(struct bpf_func_state), GFP_KERNEL); | |
11182 | if (!state->frame[0]) { | |
11183 | kfree(state); | |
11184 | return -ENOMEM; | |
11185 | } | |
11186 | env->cur_state = state; | |
11187 | init_func_state(env, state->frame[0], | |
11188 | BPF_MAIN_FUNC /* callsite */, | |
11189 | 0 /* frameno */, | |
11190 | subprog); | |
11191 | ||
11192 | regs = state->frame[state->curframe]->regs; | |
be8704ff | 11193 | if (subprog || env->prog->type == BPF_PROG_TYPE_EXT) { |
51c39bb1 AS |
11194 | ret = btf_prepare_func_args(env, subprog, regs); |
11195 | if (ret) | |
11196 | goto out; | |
11197 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) { | |
11198 | if (regs[i].type == PTR_TO_CTX) | |
11199 | mark_reg_known_zero(env, regs, i); | |
11200 | else if (regs[i].type == SCALAR_VALUE) | |
11201 | mark_reg_unknown(env, regs, i); | |
11202 | } | |
11203 | } else { | |
11204 | /* 1st arg to a function */ | |
11205 | regs[BPF_REG_1].type = PTR_TO_CTX; | |
11206 | mark_reg_known_zero(env, regs, BPF_REG_1); | |
11207 | ret = btf_check_func_arg_match(env, subprog, regs); | |
11208 | if (ret == -EFAULT) | |
11209 | /* unlikely verifier bug. abort. | |
11210 | * ret == 0 and ret < 0 are sadly acceptable for | |
11211 | * main() function due to backward compatibility. | |
11212 | * Like socket filter program may be written as: | |
11213 | * int bpf_prog(struct pt_regs *ctx) | |
11214 | * and never dereference that ctx in the program. | |
11215 | * 'struct pt_regs' is a type mismatch for socket | |
11216 | * filter that should be using 'struct __sk_buff'. | |
11217 | */ | |
11218 | goto out; | |
11219 | } | |
11220 | ||
11221 | ret = do_check(env); | |
11222 | out: | |
f59bbfc2 AS |
11223 | /* check for NULL is necessary, since cur_state can be freed inside |
11224 | * do_check() under memory pressure. | |
11225 | */ | |
11226 | if (env->cur_state) { | |
11227 | free_verifier_state(env->cur_state, true); | |
11228 | env->cur_state = NULL; | |
11229 | } | |
6f8a57cc AN |
11230 | while (!pop_stack(env, NULL, NULL, false)); |
11231 | if (!ret && pop_log) | |
11232 | bpf_vlog_reset(&env->log, 0); | |
51c39bb1 AS |
11233 | free_states(env); |
11234 | if (ret) | |
11235 | /* clean aux data in case subprog was rejected */ | |
11236 | sanitize_insn_aux_data(env); | |
11237 | return ret; | |
11238 | } | |
11239 | ||
11240 | /* Verify all global functions in a BPF program one by one based on their BTF. | |
11241 | * All global functions must pass verification. Otherwise the whole program is rejected. | |
11242 | * Consider: | |
11243 | * int bar(int); | |
11244 | * int foo(int f) | |
11245 | * { | |
11246 | * return bar(f); | |
11247 | * } | |
11248 | * int bar(int b) | |
11249 | * { | |
11250 | * ... | |
11251 | * } | |
11252 | * foo() will be verified first for R1=any_scalar_value. During verification it | |
11253 | * will be assumed that bar() already verified successfully and call to bar() | |
11254 | * from foo() will be checked for type match only. Later bar() will be verified | |
11255 | * independently to check that it's safe for R1=any_scalar_value. | |
11256 | */ | |
11257 | static int do_check_subprogs(struct bpf_verifier_env *env) | |
11258 | { | |
11259 | struct bpf_prog_aux *aux = env->prog->aux; | |
11260 | int i, ret; | |
11261 | ||
11262 | if (!aux->func_info) | |
11263 | return 0; | |
11264 | ||
11265 | for (i = 1; i < env->subprog_cnt; i++) { | |
11266 | if (aux->func_info_aux[i].linkage != BTF_FUNC_GLOBAL) | |
11267 | continue; | |
11268 | env->insn_idx = env->subprog_info[i].start; | |
11269 | WARN_ON_ONCE(env->insn_idx == 0); | |
11270 | ret = do_check_common(env, i); | |
11271 | if (ret) { | |
11272 | return ret; | |
11273 | } else if (env->log.level & BPF_LOG_LEVEL) { | |
11274 | verbose(env, | |
11275 | "Func#%d is safe for any args that match its prototype\n", | |
11276 | i); | |
11277 | } | |
11278 | } | |
11279 | return 0; | |
11280 | } | |
11281 | ||
11282 | static int do_check_main(struct bpf_verifier_env *env) | |
11283 | { | |
11284 | int ret; | |
11285 | ||
11286 | env->insn_idx = 0; | |
11287 | ret = do_check_common(env, 0); | |
11288 | if (!ret) | |
11289 | env->prog->aux->stack_depth = env->subprog_info[0].stack_depth; | |
11290 | return ret; | |
11291 | } | |
11292 | ||
11293 | ||
06ee7115 AS |
11294 | static void print_verification_stats(struct bpf_verifier_env *env) |
11295 | { | |
11296 | int i; | |
11297 | ||
11298 | if (env->log.level & BPF_LOG_STATS) { | |
11299 | verbose(env, "verification time %lld usec\n", | |
11300 | div_u64(env->verification_time, 1000)); | |
11301 | verbose(env, "stack depth "); | |
11302 | for (i = 0; i < env->subprog_cnt; i++) { | |
11303 | u32 depth = env->subprog_info[i].stack_depth; | |
11304 | ||
11305 | verbose(env, "%d", depth); | |
11306 | if (i + 1 < env->subprog_cnt) | |
11307 | verbose(env, "+"); | |
11308 | } | |
11309 | verbose(env, "\n"); | |
11310 | } | |
11311 | verbose(env, "processed %d insns (limit %d) max_states_per_insn %d " | |
11312 | "total_states %d peak_states %d mark_read %d\n", | |
11313 | env->insn_processed, BPF_COMPLEXITY_LIMIT_INSNS, | |
11314 | env->max_states_per_insn, env->total_states, | |
11315 | env->peak_states, env->longest_mark_read_walk); | |
f1bca824 AS |
11316 | } |
11317 | ||
27ae7997 MKL |
11318 | static int check_struct_ops_btf_id(struct bpf_verifier_env *env) |
11319 | { | |
11320 | const struct btf_type *t, *func_proto; | |
11321 | const struct bpf_struct_ops *st_ops; | |
11322 | const struct btf_member *member; | |
11323 | struct bpf_prog *prog = env->prog; | |
11324 | u32 btf_id, member_idx; | |
11325 | const char *mname; | |
11326 | ||
11327 | btf_id = prog->aux->attach_btf_id; | |
11328 | st_ops = bpf_struct_ops_find(btf_id); | |
11329 | if (!st_ops) { | |
11330 | verbose(env, "attach_btf_id %u is not a supported struct\n", | |
11331 | btf_id); | |
11332 | return -ENOTSUPP; | |
11333 | } | |
11334 | ||
11335 | t = st_ops->type; | |
11336 | member_idx = prog->expected_attach_type; | |
11337 | if (member_idx >= btf_type_vlen(t)) { | |
11338 | verbose(env, "attach to invalid member idx %u of struct %s\n", | |
11339 | member_idx, st_ops->name); | |
11340 | return -EINVAL; | |
11341 | } | |
11342 | ||
11343 | member = &btf_type_member(t)[member_idx]; | |
11344 | mname = btf_name_by_offset(btf_vmlinux, member->name_off); | |
11345 | func_proto = btf_type_resolve_func_ptr(btf_vmlinux, member->type, | |
11346 | NULL); | |
11347 | if (!func_proto) { | |
11348 | verbose(env, "attach to invalid member %s(@idx %u) of struct %s\n", | |
11349 | mname, member_idx, st_ops->name); | |
11350 | return -EINVAL; | |
11351 | } | |
11352 | ||
11353 | if (st_ops->check_member) { | |
11354 | int err = st_ops->check_member(t, member); | |
11355 | ||
11356 | if (err) { | |
11357 | verbose(env, "attach to unsupported member %s of struct %s\n", | |
11358 | mname, st_ops->name); | |
11359 | return err; | |
11360 | } | |
11361 | } | |
11362 | ||
11363 | prog->aux->attach_func_proto = func_proto; | |
11364 | prog->aux->attach_func_name = mname; | |
11365 | env->ops = st_ops->verifier_ops; | |
11366 | ||
11367 | return 0; | |
11368 | } | |
6ba43b76 KS |
11369 | #define SECURITY_PREFIX "security_" |
11370 | ||
f7b12b6f | 11371 | static int check_attach_modify_return(unsigned long addr, const char *func_name) |
6ba43b76 | 11372 | { |
69191754 | 11373 | if (within_error_injection_list(addr) || |
f7b12b6f | 11374 | !strncmp(SECURITY_PREFIX, func_name, sizeof(SECURITY_PREFIX) - 1)) |
6ba43b76 | 11375 | return 0; |
6ba43b76 | 11376 | |
6ba43b76 KS |
11377 | return -EINVAL; |
11378 | } | |
27ae7997 | 11379 | |
1e6c62a8 AS |
11380 | /* non exhaustive list of sleepable bpf_lsm_*() functions */ |
11381 | BTF_SET_START(btf_sleepable_lsm_hooks) | |
11382 | #ifdef CONFIG_BPF_LSM | |
1e6c62a8 | 11383 | BTF_ID(func, bpf_lsm_bprm_committed_creds) |
29523c5e AS |
11384 | #else |
11385 | BTF_ID_UNUSED | |
1e6c62a8 AS |
11386 | #endif |
11387 | BTF_SET_END(btf_sleepable_lsm_hooks) | |
11388 | ||
11389 | static int check_sleepable_lsm_hook(u32 btf_id) | |
11390 | { | |
11391 | return btf_id_set_contains(&btf_sleepable_lsm_hooks, btf_id); | |
11392 | } | |
11393 | ||
11394 | /* list of non-sleepable functions that are otherwise on | |
11395 | * ALLOW_ERROR_INJECTION list | |
11396 | */ | |
11397 | BTF_SET_START(btf_non_sleepable_error_inject) | |
11398 | /* Three functions below can be called from sleepable and non-sleepable context. | |
11399 | * Assume non-sleepable from bpf safety point of view. | |
11400 | */ | |
11401 | BTF_ID(func, __add_to_page_cache_locked) | |
11402 | BTF_ID(func, should_fail_alloc_page) | |
11403 | BTF_ID(func, should_failslab) | |
11404 | BTF_SET_END(btf_non_sleepable_error_inject) | |
11405 | ||
11406 | static int check_non_sleepable_error_inject(u32 btf_id) | |
11407 | { | |
11408 | return btf_id_set_contains(&btf_non_sleepable_error_inject, btf_id); | |
11409 | } | |
11410 | ||
f7b12b6f THJ |
11411 | int bpf_check_attach_target(struct bpf_verifier_log *log, |
11412 | const struct bpf_prog *prog, | |
11413 | const struct bpf_prog *tgt_prog, | |
11414 | u32 btf_id, | |
11415 | struct bpf_attach_target_info *tgt_info) | |
38207291 | 11416 | { |
be8704ff | 11417 | bool prog_extension = prog->type == BPF_PROG_TYPE_EXT; |
f1b9509c | 11418 | const char prefix[] = "btf_trace_"; |
5b92a28a | 11419 | int ret = 0, subprog = -1, i; |
38207291 | 11420 | const struct btf_type *t; |
5b92a28a | 11421 | bool conservative = true; |
38207291 | 11422 | const char *tname; |
5b92a28a | 11423 | struct btf *btf; |
f7b12b6f | 11424 | long addr = 0; |
38207291 | 11425 | |
f1b9509c | 11426 | if (!btf_id) { |
efc68158 | 11427 | bpf_log(log, "Tracing programs must provide btf_id\n"); |
f1b9509c AS |
11428 | return -EINVAL; |
11429 | } | |
f7b12b6f | 11430 | btf = tgt_prog ? tgt_prog->aux->btf : btf_vmlinux; |
5b92a28a | 11431 | if (!btf) { |
efc68158 | 11432 | bpf_log(log, |
5b92a28a AS |
11433 | "FENTRY/FEXIT program can only be attached to another program annotated with BTF\n"); |
11434 | return -EINVAL; | |
11435 | } | |
11436 | t = btf_type_by_id(btf, btf_id); | |
f1b9509c | 11437 | if (!t) { |
efc68158 | 11438 | bpf_log(log, "attach_btf_id %u is invalid\n", btf_id); |
f1b9509c AS |
11439 | return -EINVAL; |
11440 | } | |
5b92a28a | 11441 | tname = btf_name_by_offset(btf, t->name_off); |
f1b9509c | 11442 | if (!tname) { |
efc68158 | 11443 | bpf_log(log, "attach_btf_id %u doesn't have a name\n", btf_id); |
f1b9509c AS |
11444 | return -EINVAL; |
11445 | } | |
5b92a28a AS |
11446 | if (tgt_prog) { |
11447 | struct bpf_prog_aux *aux = tgt_prog->aux; | |
11448 | ||
11449 | for (i = 0; i < aux->func_info_cnt; i++) | |
11450 | if (aux->func_info[i].type_id == btf_id) { | |
11451 | subprog = i; | |
11452 | break; | |
11453 | } | |
11454 | if (subprog == -1) { | |
efc68158 | 11455 | bpf_log(log, "Subprog %s doesn't exist\n", tname); |
5b92a28a AS |
11456 | return -EINVAL; |
11457 | } | |
11458 | conservative = aux->func_info_aux[subprog].unreliable; | |
be8704ff AS |
11459 | if (prog_extension) { |
11460 | if (conservative) { | |
efc68158 | 11461 | bpf_log(log, |
be8704ff AS |
11462 | "Cannot replace static functions\n"); |
11463 | return -EINVAL; | |
11464 | } | |
11465 | if (!prog->jit_requested) { | |
efc68158 | 11466 | bpf_log(log, |
be8704ff AS |
11467 | "Extension programs should be JITed\n"); |
11468 | return -EINVAL; | |
11469 | } | |
be8704ff AS |
11470 | } |
11471 | if (!tgt_prog->jited) { | |
efc68158 | 11472 | bpf_log(log, "Can attach to only JITed progs\n"); |
be8704ff AS |
11473 | return -EINVAL; |
11474 | } | |
11475 | if (tgt_prog->type == prog->type) { | |
11476 | /* Cannot fentry/fexit another fentry/fexit program. | |
11477 | * Cannot attach program extension to another extension. | |
11478 | * It's ok to attach fentry/fexit to extension program. | |
11479 | */ | |
efc68158 | 11480 | bpf_log(log, "Cannot recursively attach\n"); |
be8704ff AS |
11481 | return -EINVAL; |
11482 | } | |
11483 | if (tgt_prog->type == BPF_PROG_TYPE_TRACING && | |
11484 | prog_extension && | |
11485 | (tgt_prog->expected_attach_type == BPF_TRACE_FENTRY || | |
11486 | tgt_prog->expected_attach_type == BPF_TRACE_FEXIT)) { | |
11487 | /* Program extensions can extend all program types | |
11488 | * except fentry/fexit. The reason is the following. | |
11489 | * The fentry/fexit programs are used for performance | |
11490 | * analysis, stats and can be attached to any program | |
11491 | * type except themselves. When extension program is | |
11492 | * replacing XDP function it is necessary to allow | |
11493 | * performance analysis of all functions. Both original | |
11494 | * XDP program and its program extension. Hence | |
11495 | * attaching fentry/fexit to BPF_PROG_TYPE_EXT is | |
11496 | * allowed. If extending of fentry/fexit was allowed it | |
11497 | * would be possible to create long call chain | |
11498 | * fentry->extension->fentry->extension beyond | |
11499 | * reasonable stack size. Hence extending fentry is not | |
11500 | * allowed. | |
11501 | */ | |
efc68158 | 11502 | bpf_log(log, "Cannot extend fentry/fexit\n"); |
be8704ff AS |
11503 | return -EINVAL; |
11504 | } | |
5b92a28a | 11505 | } else { |
be8704ff | 11506 | if (prog_extension) { |
efc68158 | 11507 | bpf_log(log, "Cannot replace kernel functions\n"); |
be8704ff AS |
11508 | return -EINVAL; |
11509 | } | |
5b92a28a | 11510 | } |
f1b9509c AS |
11511 | |
11512 | switch (prog->expected_attach_type) { | |
11513 | case BPF_TRACE_RAW_TP: | |
5b92a28a | 11514 | if (tgt_prog) { |
efc68158 | 11515 | bpf_log(log, |
5b92a28a AS |
11516 | "Only FENTRY/FEXIT progs are attachable to another BPF prog\n"); |
11517 | return -EINVAL; | |
11518 | } | |
38207291 | 11519 | if (!btf_type_is_typedef(t)) { |
efc68158 | 11520 | bpf_log(log, "attach_btf_id %u is not a typedef\n", |
38207291 MKL |
11521 | btf_id); |
11522 | return -EINVAL; | |
11523 | } | |
f1b9509c | 11524 | if (strncmp(prefix, tname, sizeof(prefix) - 1)) { |
efc68158 | 11525 | bpf_log(log, "attach_btf_id %u points to wrong type name %s\n", |
38207291 MKL |
11526 | btf_id, tname); |
11527 | return -EINVAL; | |
11528 | } | |
11529 | tname += sizeof(prefix) - 1; | |
5b92a28a | 11530 | t = btf_type_by_id(btf, t->type); |
38207291 MKL |
11531 | if (!btf_type_is_ptr(t)) |
11532 | /* should never happen in valid vmlinux build */ | |
11533 | return -EINVAL; | |
5b92a28a | 11534 | t = btf_type_by_id(btf, t->type); |
38207291 MKL |
11535 | if (!btf_type_is_func_proto(t)) |
11536 | /* should never happen in valid vmlinux build */ | |
11537 | return -EINVAL; | |
11538 | ||
f7b12b6f | 11539 | break; |
15d83c4d YS |
11540 | case BPF_TRACE_ITER: |
11541 | if (!btf_type_is_func(t)) { | |
efc68158 | 11542 | bpf_log(log, "attach_btf_id %u is not a function\n", |
15d83c4d YS |
11543 | btf_id); |
11544 | return -EINVAL; | |
11545 | } | |
11546 | t = btf_type_by_id(btf, t->type); | |
11547 | if (!btf_type_is_func_proto(t)) | |
11548 | return -EINVAL; | |
f7b12b6f THJ |
11549 | ret = btf_distill_func_proto(log, btf, t, tname, &tgt_info->fmodel); |
11550 | if (ret) | |
11551 | return ret; | |
11552 | break; | |
be8704ff AS |
11553 | default: |
11554 | if (!prog_extension) | |
11555 | return -EINVAL; | |
df561f66 | 11556 | fallthrough; |
ae240823 | 11557 | case BPF_MODIFY_RETURN: |
9e4e01df | 11558 | case BPF_LSM_MAC: |
fec56f58 AS |
11559 | case BPF_TRACE_FENTRY: |
11560 | case BPF_TRACE_FEXIT: | |
11561 | if (!btf_type_is_func(t)) { | |
efc68158 | 11562 | bpf_log(log, "attach_btf_id %u is not a function\n", |
fec56f58 AS |
11563 | btf_id); |
11564 | return -EINVAL; | |
11565 | } | |
be8704ff | 11566 | if (prog_extension && |
efc68158 | 11567 | btf_check_type_match(log, prog, btf, t)) |
be8704ff | 11568 | return -EINVAL; |
5b92a28a | 11569 | t = btf_type_by_id(btf, t->type); |
fec56f58 AS |
11570 | if (!btf_type_is_func_proto(t)) |
11571 | return -EINVAL; | |
f7b12b6f | 11572 | |
4a1e7c0c THJ |
11573 | if ((prog->aux->saved_dst_prog_type || prog->aux->saved_dst_attach_type) && |
11574 | (!tgt_prog || prog->aux->saved_dst_prog_type != tgt_prog->type || | |
11575 | prog->aux->saved_dst_attach_type != tgt_prog->expected_attach_type)) | |
11576 | return -EINVAL; | |
11577 | ||
f7b12b6f | 11578 | if (tgt_prog && conservative) |
5b92a28a | 11579 | t = NULL; |
f7b12b6f THJ |
11580 | |
11581 | ret = btf_distill_func_proto(log, btf, t, tname, &tgt_info->fmodel); | |
fec56f58 | 11582 | if (ret < 0) |
f7b12b6f THJ |
11583 | return ret; |
11584 | ||
5b92a28a | 11585 | if (tgt_prog) { |
e9eeec58 YS |
11586 | if (subprog == 0) |
11587 | addr = (long) tgt_prog->bpf_func; | |
11588 | else | |
11589 | addr = (long) tgt_prog->aux->func[subprog]->bpf_func; | |
5b92a28a AS |
11590 | } else { |
11591 | addr = kallsyms_lookup_name(tname); | |
11592 | if (!addr) { | |
efc68158 | 11593 | bpf_log(log, |
5b92a28a AS |
11594 | "The address of function %s cannot be found\n", |
11595 | tname); | |
f7b12b6f | 11596 | return -ENOENT; |
5b92a28a | 11597 | } |
fec56f58 | 11598 | } |
18644cec | 11599 | |
1e6c62a8 AS |
11600 | if (prog->aux->sleepable) { |
11601 | ret = -EINVAL; | |
11602 | switch (prog->type) { | |
11603 | case BPF_PROG_TYPE_TRACING: | |
11604 | /* fentry/fexit/fmod_ret progs can be sleepable only if they are | |
11605 | * attached to ALLOW_ERROR_INJECTION and are not in denylist. | |
11606 | */ | |
11607 | if (!check_non_sleepable_error_inject(btf_id) && | |
11608 | within_error_injection_list(addr)) | |
11609 | ret = 0; | |
11610 | break; | |
11611 | case BPF_PROG_TYPE_LSM: | |
11612 | /* LSM progs check that they are attached to bpf_lsm_*() funcs. | |
11613 | * Only some of them are sleepable. | |
11614 | */ | |
11615 | if (check_sleepable_lsm_hook(btf_id)) | |
11616 | ret = 0; | |
11617 | break; | |
11618 | default: | |
11619 | break; | |
11620 | } | |
f7b12b6f THJ |
11621 | if (ret) { |
11622 | bpf_log(log, "%s is not sleepable\n", tname); | |
11623 | return ret; | |
11624 | } | |
1e6c62a8 | 11625 | } else if (prog->expected_attach_type == BPF_MODIFY_RETURN) { |
1af9270e | 11626 | if (tgt_prog) { |
efc68158 | 11627 | bpf_log(log, "can't modify return codes of BPF programs\n"); |
f7b12b6f THJ |
11628 | return -EINVAL; |
11629 | } | |
11630 | ret = check_attach_modify_return(addr, tname); | |
11631 | if (ret) { | |
11632 | bpf_log(log, "%s() is not modifiable\n", tname); | |
11633 | return ret; | |
1af9270e | 11634 | } |
18644cec | 11635 | } |
f7b12b6f THJ |
11636 | |
11637 | break; | |
11638 | } | |
11639 | tgt_info->tgt_addr = addr; | |
11640 | tgt_info->tgt_name = tname; | |
11641 | tgt_info->tgt_type = t; | |
11642 | return 0; | |
11643 | } | |
11644 | ||
11645 | static int check_attach_btf_id(struct bpf_verifier_env *env) | |
11646 | { | |
11647 | struct bpf_prog *prog = env->prog; | |
3aac1ead | 11648 | struct bpf_prog *tgt_prog = prog->aux->dst_prog; |
f7b12b6f THJ |
11649 | struct bpf_attach_target_info tgt_info = {}; |
11650 | u32 btf_id = prog->aux->attach_btf_id; | |
11651 | struct bpf_trampoline *tr; | |
11652 | int ret; | |
11653 | u64 key; | |
11654 | ||
11655 | if (prog->aux->sleepable && prog->type != BPF_PROG_TYPE_TRACING && | |
11656 | prog->type != BPF_PROG_TYPE_LSM) { | |
11657 | verbose(env, "Only fentry/fexit/fmod_ret and lsm programs can be sleepable\n"); | |
11658 | return -EINVAL; | |
11659 | } | |
11660 | ||
11661 | if (prog->type == BPF_PROG_TYPE_STRUCT_OPS) | |
11662 | return check_struct_ops_btf_id(env); | |
11663 | ||
11664 | if (prog->type != BPF_PROG_TYPE_TRACING && | |
11665 | prog->type != BPF_PROG_TYPE_LSM && | |
11666 | prog->type != BPF_PROG_TYPE_EXT) | |
11667 | return 0; | |
11668 | ||
11669 | ret = bpf_check_attach_target(&env->log, prog, tgt_prog, btf_id, &tgt_info); | |
11670 | if (ret) | |
fec56f58 | 11671 | return ret; |
f7b12b6f THJ |
11672 | |
11673 | if (tgt_prog && prog->type == BPF_PROG_TYPE_EXT) { | |
3aac1ead THJ |
11674 | /* to make freplace equivalent to their targets, they need to |
11675 | * inherit env->ops and expected_attach_type for the rest of the | |
11676 | * verification | |
11677 | */ | |
f7b12b6f THJ |
11678 | env->ops = bpf_verifier_ops[tgt_prog->type]; |
11679 | prog->expected_attach_type = tgt_prog->expected_attach_type; | |
11680 | } | |
11681 | ||
11682 | /* store info about the attachment target that will be used later */ | |
11683 | prog->aux->attach_func_proto = tgt_info.tgt_type; | |
11684 | prog->aux->attach_func_name = tgt_info.tgt_name; | |
11685 | ||
4a1e7c0c THJ |
11686 | if (tgt_prog) { |
11687 | prog->aux->saved_dst_prog_type = tgt_prog->type; | |
11688 | prog->aux->saved_dst_attach_type = tgt_prog->expected_attach_type; | |
11689 | } | |
11690 | ||
f7b12b6f THJ |
11691 | if (prog->expected_attach_type == BPF_TRACE_RAW_TP) { |
11692 | prog->aux->attach_btf_trace = true; | |
11693 | return 0; | |
11694 | } else if (prog->expected_attach_type == BPF_TRACE_ITER) { | |
11695 | if (!bpf_iter_prog_supported(prog)) | |
11696 | return -EINVAL; | |
11697 | return 0; | |
11698 | } | |
11699 | ||
11700 | if (prog->type == BPF_PROG_TYPE_LSM) { | |
11701 | ret = bpf_lsm_verify_prog(&env->log, prog); | |
11702 | if (ret < 0) | |
11703 | return ret; | |
38207291 | 11704 | } |
f7b12b6f THJ |
11705 | |
11706 | key = bpf_trampoline_compute_key(tgt_prog, btf_id); | |
11707 | tr = bpf_trampoline_get(key, &tgt_info); | |
11708 | if (!tr) | |
11709 | return -ENOMEM; | |
11710 | ||
3aac1ead | 11711 | prog->aux->dst_trampoline = tr; |
f7b12b6f | 11712 | return 0; |
38207291 MKL |
11713 | } |
11714 | ||
76654e67 AM |
11715 | struct btf *bpf_get_btf_vmlinux(void) |
11716 | { | |
11717 | if (!btf_vmlinux && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) { | |
11718 | mutex_lock(&bpf_verifier_lock); | |
11719 | if (!btf_vmlinux) | |
11720 | btf_vmlinux = btf_parse_vmlinux(); | |
11721 | mutex_unlock(&bpf_verifier_lock); | |
11722 | } | |
11723 | return btf_vmlinux; | |
11724 | } | |
11725 | ||
838e9690 YS |
11726 | int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, |
11727 | union bpf_attr __user *uattr) | |
51580e79 | 11728 | { |
06ee7115 | 11729 | u64 start_time = ktime_get_ns(); |
58e2af8b | 11730 | struct bpf_verifier_env *env; |
b9193c1b | 11731 | struct bpf_verifier_log *log; |
9e4c24e7 | 11732 | int i, len, ret = -EINVAL; |
e2ae4ca2 | 11733 | bool is_priv; |
51580e79 | 11734 | |
eba0c929 AB |
11735 | /* no program is valid */ |
11736 | if (ARRAY_SIZE(bpf_verifier_ops) == 0) | |
11737 | return -EINVAL; | |
11738 | ||
58e2af8b | 11739 | /* 'struct bpf_verifier_env' can be global, but since it's not small, |
cbd35700 AS |
11740 | * allocate/free it every time bpf_check() is called |
11741 | */ | |
58e2af8b | 11742 | env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL); |
cbd35700 AS |
11743 | if (!env) |
11744 | return -ENOMEM; | |
61bd5218 | 11745 | log = &env->log; |
cbd35700 | 11746 | |
9e4c24e7 | 11747 | len = (*prog)->len; |
fad953ce | 11748 | env->insn_aux_data = |
9e4c24e7 | 11749 | vzalloc(array_size(sizeof(struct bpf_insn_aux_data), len)); |
3df126f3 JK |
11750 | ret = -ENOMEM; |
11751 | if (!env->insn_aux_data) | |
11752 | goto err_free_env; | |
9e4c24e7 JK |
11753 | for (i = 0; i < len; i++) |
11754 | env->insn_aux_data[i].orig_idx = i; | |
9bac3d6d | 11755 | env->prog = *prog; |
00176a34 | 11756 | env->ops = bpf_verifier_ops[env->prog->type]; |
2c78ee89 | 11757 | is_priv = bpf_capable(); |
0246e64d | 11758 | |
76654e67 | 11759 | bpf_get_btf_vmlinux(); |
8580ac94 | 11760 | |
cbd35700 | 11761 | /* grab the mutex to protect few globals used by verifier */ |
45a73c17 AS |
11762 | if (!is_priv) |
11763 | mutex_lock(&bpf_verifier_lock); | |
cbd35700 AS |
11764 | |
11765 | if (attr->log_level || attr->log_buf || attr->log_size) { | |
11766 | /* user requested verbose verifier output | |
11767 | * and supplied buffer to store the verification trace | |
11768 | */ | |
e7bf8249 JK |
11769 | log->level = attr->log_level; |
11770 | log->ubuf = (char __user *) (unsigned long) attr->log_buf; | |
11771 | log->len_total = attr->log_size; | |
cbd35700 AS |
11772 | |
11773 | ret = -EINVAL; | |
e7bf8249 | 11774 | /* log attributes have to be sane */ |
7a9f5c65 | 11775 | if (log->len_total < 128 || log->len_total > UINT_MAX >> 2 || |
06ee7115 | 11776 | !log->level || !log->ubuf || log->level & ~BPF_LOG_MASK) |
3df126f3 | 11777 | goto err_unlock; |
cbd35700 | 11778 | } |
1ad2f583 | 11779 | |
8580ac94 AS |
11780 | if (IS_ERR(btf_vmlinux)) { |
11781 | /* Either gcc or pahole or kernel are broken. */ | |
11782 | verbose(env, "in-kernel BTF is malformed\n"); | |
11783 | ret = PTR_ERR(btf_vmlinux); | |
38207291 | 11784 | goto skip_full_check; |
8580ac94 AS |
11785 | } |
11786 | ||
1ad2f583 DB |
11787 | env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT); |
11788 | if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) | |
e07b98d9 | 11789 | env->strict_alignment = true; |
e9ee9efc DM |
11790 | if (attr->prog_flags & BPF_F_ANY_ALIGNMENT) |
11791 | env->strict_alignment = false; | |
cbd35700 | 11792 | |
2c78ee89 | 11793 | env->allow_ptr_leaks = bpf_allow_ptr_leaks(); |
41c48f3a | 11794 | env->allow_ptr_to_map_access = bpf_allow_ptr_to_map_access(); |
2c78ee89 AS |
11795 | env->bypass_spec_v1 = bpf_bypass_spec_v1(); |
11796 | env->bypass_spec_v4 = bpf_bypass_spec_v4(); | |
11797 | env->bpf_capable = bpf_capable(); | |
e2ae4ca2 | 11798 | |
10d274e8 AS |
11799 | if (is_priv) |
11800 | env->test_state_freq = attr->prog_flags & BPF_F_TEST_STATE_FREQ; | |
11801 | ||
cae1927c | 11802 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
a40a2632 | 11803 | ret = bpf_prog_offload_verifier_prep(env->prog); |
ab3f0063 | 11804 | if (ret) |
f4e3ec0d | 11805 | goto skip_full_check; |
ab3f0063 JK |
11806 | } |
11807 | ||
dc2a4ebc | 11808 | env->explored_states = kvcalloc(state_htab_size(env), |
58e2af8b | 11809 | sizeof(struct bpf_verifier_state_list *), |
f1bca824 AS |
11810 | GFP_USER); |
11811 | ret = -ENOMEM; | |
11812 | if (!env->explored_states) | |
11813 | goto skip_full_check; | |
11814 | ||
d9762e84 | 11815 | ret = check_subprogs(env); |
475fb78f AS |
11816 | if (ret < 0) |
11817 | goto skip_full_check; | |
11818 | ||
c454a46b | 11819 | ret = check_btf_info(env, attr, uattr); |
838e9690 YS |
11820 | if (ret < 0) |
11821 | goto skip_full_check; | |
11822 | ||
be8704ff AS |
11823 | ret = check_attach_btf_id(env); |
11824 | if (ret) | |
11825 | goto skip_full_check; | |
11826 | ||
4976b718 HL |
11827 | ret = resolve_pseudo_ldimm64(env); |
11828 | if (ret < 0) | |
11829 | goto skip_full_check; | |
11830 | ||
d9762e84 MKL |
11831 | ret = check_cfg(env); |
11832 | if (ret < 0) | |
11833 | goto skip_full_check; | |
11834 | ||
51c39bb1 AS |
11835 | ret = do_check_subprogs(env); |
11836 | ret = ret ?: do_check_main(env); | |
cbd35700 | 11837 | |
c941ce9c QM |
11838 | if (ret == 0 && bpf_prog_is_dev_bound(env->prog->aux)) |
11839 | ret = bpf_prog_offload_finalize(env); | |
11840 | ||
0246e64d | 11841 | skip_full_check: |
51c39bb1 | 11842 | kvfree(env->explored_states); |
0246e64d | 11843 | |
c131187d | 11844 | if (ret == 0) |
9b38c405 | 11845 | ret = check_max_stack_depth(env); |
c131187d | 11846 | |
9b38c405 | 11847 | /* instruction rewrites happen after this point */ |
e2ae4ca2 JK |
11848 | if (is_priv) { |
11849 | if (ret == 0) | |
11850 | opt_hard_wire_dead_code_branches(env); | |
52875a04 JK |
11851 | if (ret == 0) |
11852 | ret = opt_remove_dead_code(env); | |
a1b14abc JK |
11853 | if (ret == 0) |
11854 | ret = opt_remove_nops(env); | |
52875a04 JK |
11855 | } else { |
11856 | if (ret == 0) | |
11857 | sanitize_dead_code(env); | |
e2ae4ca2 JK |
11858 | } |
11859 | ||
9bac3d6d AS |
11860 | if (ret == 0) |
11861 | /* program is valid, convert *(u32*)(ctx + off) accesses */ | |
11862 | ret = convert_ctx_accesses(env); | |
11863 | ||
e245c5c6 | 11864 | if (ret == 0) |
79741b3b | 11865 | ret = fixup_bpf_calls(env); |
e245c5c6 | 11866 | |
a4b1d3c1 JW |
11867 | /* do 32-bit optimization after insn patching has done so those patched |
11868 | * insns could be handled correctly. | |
11869 | */ | |
d6c2308c JW |
11870 | if (ret == 0 && !bpf_prog_is_dev_bound(env->prog->aux)) { |
11871 | ret = opt_subreg_zext_lo32_rnd_hi32(env, attr); | |
11872 | env->prog->aux->verifier_zext = bpf_jit_needs_zext() ? !ret | |
11873 | : false; | |
a4b1d3c1 JW |
11874 | } |
11875 | ||
1ea47e01 AS |
11876 | if (ret == 0) |
11877 | ret = fixup_call_args(env); | |
11878 | ||
06ee7115 AS |
11879 | env->verification_time = ktime_get_ns() - start_time; |
11880 | print_verification_stats(env); | |
11881 | ||
a2a7d570 | 11882 | if (log->level && bpf_verifier_log_full(log)) |
cbd35700 | 11883 | ret = -ENOSPC; |
a2a7d570 | 11884 | if (log->level && !log->ubuf) { |
cbd35700 | 11885 | ret = -EFAULT; |
a2a7d570 | 11886 | goto err_release_maps; |
cbd35700 AS |
11887 | } |
11888 | ||
0246e64d AS |
11889 | if (ret == 0 && env->used_map_cnt) { |
11890 | /* if program passed verifier, update used_maps in bpf_prog_info */ | |
9bac3d6d AS |
11891 | env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt, |
11892 | sizeof(env->used_maps[0]), | |
11893 | GFP_KERNEL); | |
0246e64d | 11894 | |
9bac3d6d | 11895 | if (!env->prog->aux->used_maps) { |
0246e64d | 11896 | ret = -ENOMEM; |
a2a7d570 | 11897 | goto err_release_maps; |
0246e64d AS |
11898 | } |
11899 | ||
9bac3d6d | 11900 | memcpy(env->prog->aux->used_maps, env->used_maps, |
0246e64d | 11901 | sizeof(env->used_maps[0]) * env->used_map_cnt); |
9bac3d6d | 11902 | env->prog->aux->used_map_cnt = env->used_map_cnt; |
0246e64d AS |
11903 | |
11904 | /* program is valid. Convert pseudo bpf_ld_imm64 into generic | |
11905 | * bpf_ld_imm64 instructions | |
11906 | */ | |
11907 | convert_pseudo_ld_imm64(env); | |
11908 | } | |
cbd35700 | 11909 | |
ba64e7d8 YS |
11910 | if (ret == 0) |
11911 | adjust_btf_func(env); | |
11912 | ||
a2a7d570 | 11913 | err_release_maps: |
9bac3d6d | 11914 | if (!env->prog->aux->used_maps) |
0246e64d | 11915 | /* if we didn't copy map pointers into bpf_prog_info, release |
ab7f5bf0 | 11916 | * them now. Otherwise free_used_maps() will release them. |
0246e64d AS |
11917 | */ |
11918 | release_maps(env); | |
03f87c0b THJ |
11919 | |
11920 | /* extension progs temporarily inherit the attach_type of their targets | |
11921 | for verification purposes, so set it back to zero before returning | |
11922 | */ | |
11923 | if (env->prog->type == BPF_PROG_TYPE_EXT) | |
11924 | env->prog->expected_attach_type = 0; | |
11925 | ||
9bac3d6d | 11926 | *prog = env->prog; |
3df126f3 | 11927 | err_unlock: |
45a73c17 AS |
11928 | if (!is_priv) |
11929 | mutex_unlock(&bpf_verifier_lock); | |
3df126f3 JK |
11930 | vfree(env->insn_aux_data); |
11931 | err_free_env: | |
11932 | kfree(env); | |
51580e79 AS |
11933 | return ret; |
11934 | } |