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; |
33ff9823 DB |
241 | }; |
242 | ||
8580ac94 AS |
243 | struct btf *btf_vmlinux; |
244 | ||
cbd35700 AS |
245 | static DEFINE_MUTEX(bpf_verifier_lock); |
246 | ||
d9762e84 MKL |
247 | static const struct bpf_line_info * |
248 | find_linfo(const struct bpf_verifier_env *env, u32 insn_off) | |
249 | { | |
250 | const struct bpf_line_info *linfo; | |
251 | const struct bpf_prog *prog; | |
252 | u32 i, nr_linfo; | |
253 | ||
254 | prog = env->prog; | |
255 | nr_linfo = prog->aux->nr_linfo; | |
256 | ||
257 | if (!nr_linfo || insn_off >= prog->len) | |
258 | return NULL; | |
259 | ||
260 | linfo = prog->aux->linfo; | |
261 | for (i = 1; i < nr_linfo; i++) | |
262 | if (insn_off < linfo[i].insn_off) | |
263 | break; | |
264 | ||
265 | return &linfo[i - 1]; | |
266 | } | |
267 | ||
77d2e05a MKL |
268 | void bpf_verifier_vlog(struct bpf_verifier_log *log, const char *fmt, |
269 | va_list args) | |
cbd35700 | 270 | { |
a2a7d570 | 271 | unsigned int n; |
cbd35700 | 272 | |
a2a7d570 | 273 | n = vscnprintf(log->kbuf, BPF_VERIFIER_TMP_LOG_SIZE, fmt, args); |
a2a7d570 JK |
274 | |
275 | WARN_ONCE(n >= BPF_VERIFIER_TMP_LOG_SIZE - 1, | |
276 | "verifier log line truncated - local buffer too short\n"); | |
277 | ||
278 | n = min(log->len_total - log->len_used - 1, n); | |
279 | log->kbuf[n] = '\0'; | |
280 | ||
8580ac94 AS |
281 | if (log->level == BPF_LOG_KERNEL) { |
282 | pr_err("BPF:%s\n", log->kbuf); | |
283 | return; | |
284 | } | |
a2a7d570 JK |
285 | if (!copy_to_user(log->ubuf + log->len_used, log->kbuf, n + 1)) |
286 | log->len_used += n; | |
287 | else | |
288 | log->ubuf = NULL; | |
cbd35700 | 289 | } |
abe08840 | 290 | |
6f8a57cc AN |
291 | static void bpf_vlog_reset(struct bpf_verifier_log *log, u32 new_pos) |
292 | { | |
293 | char zero = 0; | |
294 | ||
295 | if (!bpf_verifier_log_needed(log)) | |
296 | return; | |
297 | ||
298 | log->len_used = new_pos; | |
299 | if (put_user(zero, log->ubuf + new_pos)) | |
300 | log->ubuf = NULL; | |
301 | } | |
302 | ||
abe08840 JO |
303 | /* log_level controls verbosity level of eBPF verifier. |
304 | * bpf_verifier_log_write() is used to dump the verification trace to the log, | |
305 | * so the user can figure out what's wrong with the program | |
430e68d1 | 306 | */ |
abe08840 JO |
307 | __printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env, |
308 | const char *fmt, ...) | |
309 | { | |
310 | va_list args; | |
311 | ||
77d2e05a MKL |
312 | if (!bpf_verifier_log_needed(&env->log)) |
313 | return; | |
314 | ||
abe08840 | 315 | va_start(args, fmt); |
77d2e05a | 316 | bpf_verifier_vlog(&env->log, fmt, args); |
abe08840 JO |
317 | va_end(args); |
318 | } | |
319 | EXPORT_SYMBOL_GPL(bpf_verifier_log_write); | |
320 | ||
321 | __printf(2, 3) static void verbose(void *private_data, const char *fmt, ...) | |
322 | { | |
77d2e05a | 323 | struct bpf_verifier_env *env = private_data; |
abe08840 JO |
324 | va_list args; |
325 | ||
77d2e05a MKL |
326 | if (!bpf_verifier_log_needed(&env->log)) |
327 | return; | |
328 | ||
abe08840 | 329 | va_start(args, fmt); |
77d2e05a | 330 | bpf_verifier_vlog(&env->log, fmt, args); |
abe08840 JO |
331 | va_end(args); |
332 | } | |
cbd35700 | 333 | |
9e15db66 AS |
334 | __printf(2, 3) void bpf_log(struct bpf_verifier_log *log, |
335 | const char *fmt, ...) | |
336 | { | |
337 | va_list args; | |
338 | ||
339 | if (!bpf_verifier_log_needed(log)) | |
340 | return; | |
341 | ||
342 | va_start(args, fmt); | |
343 | bpf_verifier_vlog(log, fmt, args); | |
344 | va_end(args); | |
345 | } | |
346 | ||
d9762e84 MKL |
347 | static const char *ltrim(const char *s) |
348 | { | |
349 | while (isspace(*s)) | |
350 | s++; | |
351 | ||
352 | return s; | |
353 | } | |
354 | ||
355 | __printf(3, 4) static void verbose_linfo(struct bpf_verifier_env *env, | |
356 | u32 insn_off, | |
357 | const char *prefix_fmt, ...) | |
358 | { | |
359 | const struct bpf_line_info *linfo; | |
360 | ||
361 | if (!bpf_verifier_log_needed(&env->log)) | |
362 | return; | |
363 | ||
364 | linfo = find_linfo(env, insn_off); | |
365 | if (!linfo || linfo == env->prev_linfo) | |
366 | return; | |
367 | ||
368 | if (prefix_fmt) { | |
369 | va_list args; | |
370 | ||
371 | va_start(args, prefix_fmt); | |
372 | bpf_verifier_vlog(&env->log, prefix_fmt, args); | |
373 | va_end(args); | |
374 | } | |
375 | ||
376 | verbose(env, "%s\n", | |
377 | ltrim(btf_name_by_offset(env->prog->aux->btf, | |
378 | linfo->line_off))); | |
379 | ||
380 | env->prev_linfo = linfo; | |
381 | } | |
382 | ||
de8f3a83 DB |
383 | static bool type_is_pkt_pointer(enum bpf_reg_type type) |
384 | { | |
385 | return type == PTR_TO_PACKET || | |
386 | type == PTR_TO_PACKET_META; | |
387 | } | |
388 | ||
46f8bc92 MKL |
389 | static bool type_is_sk_pointer(enum bpf_reg_type type) |
390 | { | |
391 | return type == PTR_TO_SOCKET || | |
655a51e5 | 392 | type == PTR_TO_SOCK_COMMON || |
fada7fdc JL |
393 | type == PTR_TO_TCP_SOCK || |
394 | type == PTR_TO_XDP_SOCK; | |
46f8bc92 MKL |
395 | } |
396 | ||
cac616db JF |
397 | static bool reg_type_not_null(enum bpf_reg_type type) |
398 | { | |
399 | return type == PTR_TO_SOCKET || | |
400 | type == PTR_TO_TCP_SOCK || | |
401 | type == PTR_TO_MAP_VALUE || | |
01c66c48 | 402 | type == PTR_TO_SOCK_COMMON; |
cac616db JF |
403 | } |
404 | ||
840b9615 JS |
405 | static bool reg_type_may_be_null(enum bpf_reg_type type) |
406 | { | |
fd978bf7 | 407 | return type == PTR_TO_MAP_VALUE_OR_NULL || |
46f8bc92 | 408 | type == PTR_TO_SOCKET_OR_NULL || |
655a51e5 | 409 | type == PTR_TO_SOCK_COMMON_OR_NULL || |
b121b341 | 410 | type == PTR_TO_TCP_SOCK_OR_NULL || |
457f4436 | 411 | type == PTR_TO_BTF_ID_OR_NULL || |
afbf21dc YS |
412 | type == PTR_TO_MEM_OR_NULL || |
413 | type == PTR_TO_RDONLY_BUF_OR_NULL || | |
414 | type == PTR_TO_RDWR_BUF_OR_NULL; | |
fd978bf7 JS |
415 | } |
416 | ||
d83525ca AS |
417 | static bool reg_may_point_to_spin_lock(const struct bpf_reg_state *reg) |
418 | { | |
419 | return reg->type == PTR_TO_MAP_VALUE && | |
420 | map_value_has_spin_lock(reg->map_ptr); | |
421 | } | |
422 | ||
cba368c1 MKL |
423 | static bool reg_type_may_be_refcounted_or_null(enum bpf_reg_type type) |
424 | { | |
425 | return type == PTR_TO_SOCKET || | |
426 | type == PTR_TO_SOCKET_OR_NULL || | |
427 | type == PTR_TO_TCP_SOCK || | |
457f4436 AN |
428 | type == PTR_TO_TCP_SOCK_OR_NULL || |
429 | type == PTR_TO_MEM || | |
430 | type == PTR_TO_MEM_OR_NULL; | |
cba368c1 MKL |
431 | } |
432 | ||
1b986589 | 433 | static bool arg_type_may_be_refcounted(enum bpf_arg_type type) |
fd978bf7 | 434 | { |
1b986589 | 435 | return type == ARG_PTR_TO_SOCK_COMMON; |
fd978bf7 JS |
436 | } |
437 | ||
fd1b0d60 LB |
438 | static bool arg_type_may_be_null(enum bpf_arg_type type) |
439 | { | |
440 | return type == ARG_PTR_TO_MAP_VALUE_OR_NULL || | |
441 | type == ARG_PTR_TO_MEM_OR_NULL || | |
442 | type == ARG_PTR_TO_CTX_OR_NULL || | |
443 | type == ARG_PTR_TO_SOCKET_OR_NULL || | |
444 | type == ARG_PTR_TO_ALLOC_MEM_OR_NULL; | |
445 | } | |
446 | ||
fd978bf7 JS |
447 | /* Determine whether the function releases some resources allocated by another |
448 | * function call. The first reference type argument will be assumed to be | |
449 | * released by release_reference(). | |
450 | */ | |
451 | static bool is_release_function(enum bpf_func_id func_id) | |
452 | { | |
457f4436 AN |
453 | return func_id == BPF_FUNC_sk_release || |
454 | func_id == BPF_FUNC_ringbuf_submit || | |
455 | func_id == BPF_FUNC_ringbuf_discard; | |
840b9615 JS |
456 | } |
457 | ||
64d85290 | 458 | static bool may_be_acquire_function(enum bpf_func_id func_id) |
46f8bc92 MKL |
459 | { |
460 | return func_id == BPF_FUNC_sk_lookup_tcp || | |
edbf8c01 | 461 | func_id == BPF_FUNC_sk_lookup_udp || |
64d85290 | 462 | func_id == BPF_FUNC_skc_lookup_tcp || |
457f4436 AN |
463 | func_id == BPF_FUNC_map_lookup_elem || |
464 | func_id == BPF_FUNC_ringbuf_reserve; | |
64d85290 JS |
465 | } |
466 | ||
467 | static bool is_acquire_function(enum bpf_func_id func_id, | |
468 | const struct bpf_map *map) | |
469 | { | |
470 | enum bpf_map_type map_type = map ? map->map_type : BPF_MAP_TYPE_UNSPEC; | |
471 | ||
472 | if (func_id == BPF_FUNC_sk_lookup_tcp || | |
473 | func_id == BPF_FUNC_sk_lookup_udp || | |
457f4436 AN |
474 | func_id == BPF_FUNC_skc_lookup_tcp || |
475 | func_id == BPF_FUNC_ringbuf_reserve) | |
64d85290 JS |
476 | return true; |
477 | ||
478 | if (func_id == BPF_FUNC_map_lookup_elem && | |
479 | (map_type == BPF_MAP_TYPE_SOCKMAP || | |
480 | map_type == BPF_MAP_TYPE_SOCKHASH)) | |
481 | return true; | |
482 | ||
483 | return false; | |
46f8bc92 MKL |
484 | } |
485 | ||
1b986589 MKL |
486 | static bool is_ptr_cast_function(enum bpf_func_id func_id) |
487 | { | |
488 | return func_id == BPF_FUNC_tcp_sock || | |
1df8f55a MKL |
489 | func_id == BPF_FUNC_sk_fullsock || |
490 | func_id == BPF_FUNC_skc_to_tcp_sock || | |
491 | func_id == BPF_FUNC_skc_to_tcp6_sock || | |
492 | func_id == BPF_FUNC_skc_to_udp6_sock || | |
493 | func_id == BPF_FUNC_skc_to_tcp_timewait_sock || | |
494 | func_id == BPF_FUNC_skc_to_tcp_request_sock; | |
1b986589 MKL |
495 | } |
496 | ||
17a52670 AS |
497 | /* string representation of 'enum bpf_reg_type' */ |
498 | static const char * const reg_type_str[] = { | |
499 | [NOT_INIT] = "?", | |
f1174f77 | 500 | [SCALAR_VALUE] = "inv", |
17a52670 AS |
501 | [PTR_TO_CTX] = "ctx", |
502 | [CONST_PTR_TO_MAP] = "map_ptr", | |
503 | [PTR_TO_MAP_VALUE] = "map_value", | |
504 | [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null", | |
17a52670 | 505 | [PTR_TO_STACK] = "fp", |
969bf05e | 506 | [PTR_TO_PACKET] = "pkt", |
de8f3a83 | 507 | [PTR_TO_PACKET_META] = "pkt_meta", |
969bf05e | 508 | [PTR_TO_PACKET_END] = "pkt_end", |
d58e468b | 509 | [PTR_TO_FLOW_KEYS] = "flow_keys", |
c64b7983 JS |
510 | [PTR_TO_SOCKET] = "sock", |
511 | [PTR_TO_SOCKET_OR_NULL] = "sock_or_null", | |
46f8bc92 MKL |
512 | [PTR_TO_SOCK_COMMON] = "sock_common", |
513 | [PTR_TO_SOCK_COMMON_OR_NULL] = "sock_common_or_null", | |
655a51e5 MKL |
514 | [PTR_TO_TCP_SOCK] = "tcp_sock", |
515 | [PTR_TO_TCP_SOCK_OR_NULL] = "tcp_sock_or_null", | |
9df1c28b | 516 | [PTR_TO_TP_BUFFER] = "tp_buffer", |
fada7fdc | 517 | [PTR_TO_XDP_SOCK] = "xdp_sock", |
9e15db66 | 518 | [PTR_TO_BTF_ID] = "ptr_", |
b121b341 | 519 | [PTR_TO_BTF_ID_OR_NULL] = "ptr_or_null_", |
457f4436 AN |
520 | [PTR_TO_MEM] = "mem", |
521 | [PTR_TO_MEM_OR_NULL] = "mem_or_null", | |
afbf21dc YS |
522 | [PTR_TO_RDONLY_BUF] = "rdonly_buf", |
523 | [PTR_TO_RDONLY_BUF_OR_NULL] = "rdonly_buf_or_null", | |
524 | [PTR_TO_RDWR_BUF] = "rdwr_buf", | |
525 | [PTR_TO_RDWR_BUF_OR_NULL] = "rdwr_buf_or_null", | |
17a52670 AS |
526 | }; |
527 | ||
8efea21d EC |
528 | static char slot_type_char[] = { |
529 | [STACK_INVALID] = '?', | |
530 | [STACK_SPILL] = 'r', | |
531 | [STACK_MISC] = 'm', | |
532 | [STACK_ZERO] = '0', | |
533 | }; | |
534 | ||
4e92024a AS |
535 | static void print_liveness(struct bpf_verifier_env *env, |
536 | enum bpf_reg_liveness live) | |
537 | { | |
9242b5f5 | 538 | if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN | REG_LIVE_DONE)) |
4e92024a AS |
539 | verbose(env, "_"); |
540 | if (live & REG_LIVE_READ) | |
541 | verbose(env, "r"); | |
542 | if (live & REG_LIVE_WRITTEN) | |
543 | verbose(env, "w"); | |
9242b5f5 AS |
544 | if (live & REG_LIVE_DONE) |
545 | verbose(env, "D"); | |
4e92024a AS |
546 | } |
547 | ||
f4d7e40a AS |
548 | static struct bpf_func_state *func(struct bpf_verifier_env *env, |
549 | const struct bpf_reg_state *reg) | |
550 | { | |
551 | struct bpf_verifier_state *cur = env->cur_state; | |
552 | ||
553 | return cur->frame[reg->frameno]; | |
554 | } | |
555 | ||
9e15db66 AS |
556 | const char *kernel_type_name(u32 id) |
557 | { | |
558 | return btf_name_by_offset(btf_vmlinux, | |
559 | btf_type_by_id(btf_vmlinux, id)->name_off); | |
560 | } | |
561 | ||
61bd5218 | 562 | static void print_verifier_state(struct bpf_verifier_env *env, |
f4d7e40a | 563 | const struct bpf_func_state *state) |
17a52670 | 564 | { |
f4d7e40a | 565 | const struct bpf_reg_state *reg; |
17a52670 AS |
566 | enum bpf_reg_type t; |
567 | int i; | |
568 | ||
f4d7e40a AS |
569 | if (state->frameno) |
570 | verbose(env, " frame%d:", state->frameno); | |
17a52670 | 571 | for (i = 0; i < MAX_BPF_REG; i++) { |
1a0dc1ac AS |
572 | reg = &state->regs[i]; |
573 | t = reg->type; | |
17a52670 AS |
574 | if (t == NOT_INIT) |
575 | continue; | |
4e92024a AS |
576 | verbose(env, " R%d", i); |
577 | print_liveness(env, reg->live); | |
578 | verbose(env, "=%s", reg_type_str[t]); | |
b5dc0163 AS |
579 | if (t == SCALAR_VALUE && reg->precise) |
580 | verbose(env, "P"); | |
f1174f77 EC |
581 | if ((t == SCALAR_VALUE || t == PTR_TO_STACK) && |
582 | tnum_is_const(reg->var_off)) { | |
583 | /* reg->off should be 0 for SCALAR_VALUE */ | |
61bd5218 | 584 | verbose(env, "%lld", reg->var_off.value + reg->off); |
f1174f77 | 585 | } else { |
b121b341 | 586 | if (t == PTR_TO_BTF_ID || t == PTR_TO_BTF_ID_OR_NULL) |
9e15db66 | 587 | verbose(env, "%s", kernel_type_name(reg->btf_id)); |
cba368c1 MKL |
588 | verbose(env, "(id=%d", reg->id); |
589 | if (reg_type_may_be_refcounted_or_null(t)) | |
590 | verbose(env, ",ref_obj_id=%d", reg->ref_obj_id); | |
f1174f77 | 591 | if (t != SCALAR_VALUE) |
61bd5218 | 592 | verbose(env, ",off=%d", reg->off); |
de8f3a83 | 593 | if (type_is_pkt_pointer(t)) |
61bd5218 | 594 | verbose(env, ",r=%d", reg->range); |
f1174f77 EC |
595 | else if (t == CONST_PTR_TO_MAP || |
596 | t == PTR_TO_MAP_VALUE || | |
597 | t == PTR_TO_MAP_VALUE_OR_NULL) | |
61bd5218 | 598 | verbose(env, ",ks=%d,vs=%d", |
f1174f77 EC |
599 | reg->map_ptr->key_size, |
600 | reg->map_ptr->value_size); | |
7d1238f2 EC |
601 | if (tnum_is_const(reg->var_off)) { |
602 | /* Typically an immediate SCALAR_VALUE, but | |
603 | * could be a pointer whose offset is too big | |
604 | * for reg->off | |
605 | */ | |
61bd5218 | 606 | verbose(env, ",imm=%llx", reg->var_off.value); |
7d1238f2 EC |
607 | } else { |
608 | if (reg->smin_value != reg->umin_value && | |
609 | reg->smin_value != S64_MIN) | |
61bd5218 | 610 | verbose(env, ",smin_value=%lld", |
7d1238f2 EC |
611 | (long long)reg->smin_value); |
612 | if (reg->smax_value != reg->umax_value && | |
613 | reg->smax_value != S64_MAX) | |
61bd5218 | 614 | verbose(env, ",smax_value=%lld", |
7d1238f2 EC |
615 | (long long)reg->smax_value); |
616 | if (reg->umin_value != 0) | |
61bd5218 | 617 | verbose(env, ",umin_value=%llu", |
7d1238f2 EC |
618 | (unsigned long long)reg->umin_value); |
619 | if (reg->umax_value != U64_MAX) | |
61bd5218 | 620 | verbose(env, ",umax_value=%llu", |
7d1238f2 EC |
621 | (unsigned long long)reg->umax_value); |
622 | if (!tnum_is_unknown(reg->var_off)) { | |
623 | char tn_buf[48]; | |
f1174f77 | 624 | |
7d1238f2 | 625 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
61bd5218 | 626 | verbose(env, ",var_off=%s", tn_buf); |
7d1238f2 | 627 | } |
3f50f132 JF |
628 | if (reg->s32_min_value != reg->smin_value && |
629 | reg->s32_min_value != S32_MIN) | |
630 | verbose(env, ",s32_min_value=%d", | |
631 | (int)(reg->s32_min_value)); | |
632 | if (reg->s32_max_value != reg->smax_value && | |
633 | reg->s32_max_value != S32_MAX) | |
634 | verbose(env, ",s32_max_value=%d", | |
635 | (int)(reg->s32_max_value)); | |
636 | if (reg->u32_min_value != reg->umin_value && | |
637 | reg->u32_min_value != U32_MIN) | |
638 | verbose(env, ",u32_min_value=%d", | |
639 | (int)(reg->u32_min_value)); | |
640 | if (reg->u32_max_value != reg->umax_value && | |
641 | reg->u32_max_value != U32_MAX) | |
642 | verbose(env, ",u32_max_value=%d", | |
643 | (int)(reg->u32_max_value)); | |
f1174f77 | 644 | } |
61bd5218 | 645 | verbose(env, ")"); |
f1174f77 | 646 | } |
17a52670 | 647 | } |
638f5b90 | 648 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
8efea21d EC |
649 | char types_buf[BPF_REG_SIZE + 1]; |
650 | bool valid = false; | |
651 | int j; | |
652 | ||
653 | for (j = 0; j < BPF_REG_SIZE; j++) { | |
654 | if (state->stack[i].slot_type[j] != STACK_INVALID) | |
655 | valid = true; | |
656 | types_buf[j] = slot_type_char[ | |
657 | state->stack[i].slot_type[j]]; | |
658 | } | |
659 | types_buf[BPF_REG_SIZE] = 0; | |
660 | if (!valid) | |
661 | continue; | |
662 | verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE); | |
663 | print_liveness(env, state->stack[i].spilled_ptr.live); | |
b5dc0163 AS |
664 | if (state->stack[i].slot_type[0] == STACK_SPILL) { |
665 | reg = &state->stack[i].spilled_ptr; | |
666 | t = reg->type; | |
667 | verbose(env, "=%s", reg_type_str[t]); | |
668 | if (t == SCALAR_VALUE && reg->precise) | |
669 | verbose(env, "P"); | |
670 | if (t == SCALAR_VALUE && tnum_is_const(reg->var_off)) | |
671 | verbose(env, "%lld", reg->var_off.value + reg->off); | |
672 | } else { | |
8efea21d | 673 | verbose(env, "=%s", types_buf); |
b5dc0163 | 674 | } |
17a52670 | 675 | } |
fd978bf7 JS |
676 | if (state->acquired_refs && state->refs[0].id) { |
677 | verbose(env, " refs=%d", state->refs[0].id); | |
678 | for (i = 1; i < state->acquired_refs; i++) | |
679 | if (state->refs[i].id) | |
680 | verbose(env, ",%d", state->refs[i].id); | |
681 | } | |
61bd5218 | 682 | verbose(env, "\n"); |
17a52670 AS |
683 | } |
684 | ||
84dbf350 JS |
685 | #define COPY_STATE_FN(NAME, COUNT, FIELD, SIZE) \ |
686 | static int copy_##NAME##_state(struct bpf_func_state *dst, \ | |
687 | const struct bpf_func_state *src) \ | |
688 | { \ | |
689 | if (!src->FIELD) \ | |
690 | return 0; \ | |
691 | if (WARN_ON_ONCE(dst->COUNT < src->COUNT)) { \ | |
692 | /* internal bug, make state invalid to reject the program */ \ | |
693 | memset(dst, 0, sizeof(*dst)); \ | |
694 | return -EFAULT; \ | |
695 | } \ | |
696 | memcpy(dst->FIELD, src->FIELD, \ | |
697 | sizeof(*src->FIELD) * (src->COUNT / SIZE)); \ | |
698 | return 0; \ | |
638f5b90 | 699 | } |
fd978bf7 JS |
700 | /* copy_reference_state() */ |
701 | COPY_STATE_FN(reference, acquired_refs, refs, 1) | |
84dbf350 JS |
702 | /* copy_stack_state() */ |
703 | COPY_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE) | |
704 | #undef COPY_STATE_FN | |
705 | ||
706 | #define REALLOC_STATE_FN(NAME, COUNT, FIELD, SIZE) \ | |
707 | static int realloc_##NAME##_state(struct bpf_func_state *state, int size, \ | |
708 | bool copy_old) \ | |
709 | { \ | |
710 | u32 old_size = state->COUNT; \ | |
711 | struct bpf_##NAME##_state *new_##FIELD; \ | |
712 | int slot = size / SIZE; \ | |
713 | \ | |
714 | if (size <= old_size || !size) { \ | |
715 | if (copy_old) \ | |
716 | return 0; \ | |
717 | state->COUNT = slot * SIZE; \ | |
718 | if (!size && old_size) { \ | |
719 | kfree(state->FIELD); \ | |
720 | state->FIELD = NULL; \ | |
721 | } \ | |
722 | return 0; \ | |
723 | } \ | |
724 | new_##FIELD = kmalloc_array(slot, sizeof(struct bpf_##NAME##_state), \ | |
725 | GFP_KERNEL); \ | |
726 | if (!new_##FIELD) \ | |
727 | return -ENOMEM; \ | |
728 | if (copy_old) { \ | |
729 | if (state->FIELD) \ | |
730 | memcpy(new_##FIELD, state->FIELD, \ | |
731 | sizeof(*new_##FIELD) * (old_size / SIZE)); \ | |
732 | memset(new_##FIELD + old_size / SIZE, 0, \ | |
733 | sizeof(*new_##FIELD) * (size - old_size) / SIZE); \ | |
734 | } \ | |
735 | state->COUNT = slot * SIZE; \ | |
736 | kfree(state->FIELD); \ | |
737 | state->FIELD = new_##FIELD; \ | |
738 | return 0; \ | |
739 | } | |
fd978bf7 JS |
740 | /* realloc_reference_state() */ |
741 | REALLOC_STATE_FN(reference, acquired_refs, refs, 1) | |
84dbf350 JS |
742 | /* realloc_stack_state() */ |
743 | REALLOC_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE) | |
744 | #undef REALLOC_STATE_FN | |
638f5b90 AS |
745 | |
746 | /* do_check() starts with zero-sized stack in struct bpf_verifier_state to | |
747 | * make it consume minimal amount of memory. check_stack_write() access from | |
f4d7e40a | 748 | * the program calls into realloc_func_state() to grow the stack size. |
84dbf350 JS |
749 | * Note there is a non-zero 'parent' pointer inside bpf_verifier_state |
750 | * which realloc_stack_state() copies over. It points to previous | |
751 | * bpf_verifier_state which is never reallocated. | |
638f5b90 | 752 | */ |
fd978bf7 JS |
753 | static int realloc_func_state(struct bpf_func_state *state, int stack_size, |
754 | int refs_size, bool copy_old) | |
638f5b90 | 755 | { |
fd978bf7 JS |
756 | int err = realloc_reference_state(state, refs_size, copy_old); |
757 | if (err) | |
758 | return err; | |
759 | return realloc_stack_state(state, stack_size, copy_old); | |
760 | } | |
761 | ||
762 | /* Acquire a pointer id from the env and update the state->refs to include | |
763 | * this new pointer reference. | |
764 | * On success, returns a valid pointer id to associate with the register | |
765 | * On failure, returns a negative errno. | |
638f5b90 | 766 | */ |
fd978bf7 | 767 | static int acquire_reference_state(struct bpf_verifier_env *env, int insn_idx) |
638f5b90 | 768 | { |
fd978bf7 JS |
769 | struct bpf_func_state *state = cur_func(env); |
770 | int new_ofs = state->acquired_refs; | |
771 | int id, err; | |
772 | ||
773 | err = realloc_reference_state(state, state->acquired_refs + 1, true); | |
774 | if (err) | |
775 | return err; | |
776 | id = ++env->id_gen; | |
777 | state->refs[new_ofs].id = id; | |
778 | state->refs[new_ofs].insn_idx = insn_idx; | |
638f5b90 | 779 | |
fd978bf7 JS |
780 | return id; |
781 | } | |
782 | ||
783 | /* release function corresponding to acquire_reference_state(). Idempotent. */ | |
46f8bc92 | 784 | static int release_reference_state(struct bpf_func_state *state, int ptr_id) |
fd978bf7 JS |
785 | { |
786 | int i, last_idx; | |
787 | ||
fd978bf7 JS |
788 | last_idx = state->acquired_refs - 1; |
789 | for (i = 0; i < state->acquired_refs; i++) { | |
790 | if (state->refs[i].id == ptr_id) { | |
791 | if (last_idx && i != last_idx) | |
792 | memcpy(&state->refs[i], &state->refs[last_idx], | |
793 | sizeof(*state->refs)); | |
794 | memset(&state->refs[last_idx], 0, sizeof(*state->refs)); | |
795 | state->acquired_refs--; | |
638f5b90 | 796 | return 0; |
638f5b90 | 797 | } |
638f5b90 | 798 | } |
46f8bc92 | 799 | return -EINVAL; |
fd978bf7 JS |
800 | } |
801 | ||
802 | static int transfer_reference_state(struct bpf_func_state *dst, | |
803 | struct bpf_func_state *src) | |
804 | { | |
805 | int err = realloc_reference_state(dst, src->acquired_refs, false); | |
806 | if (err) | |
807 | return err; | |
808 | err = copy_reference_state(dst, src); | |
809 | if (err) | |
810 | return err; | |
638f5b90 AS |
811 | return 0; |
812 | } | |
813 | ||
f4d7e40a AS |
814 | static void free_func_state(struct bpf_func_state *state) |
815 | { | |
5896351e AS |
816 | if (!state) |
817 | return; | |
fd978bf7 | 818 | kfree(state->refs); |
f4d7e40a AS |
819 | kfree(state->stack); |
820 | kfree(state); | |
821 | } | |
822 | ||
b5dc0163 AS |
823 | static void clear_jmp_history(struct bpf_verifier_state *state) |
824 | { | |
825 | kfree(state->jmp_history); | |
826 | state->jmp_history = NULL; | |
827 | state->jmp_history_cnt = 0; | |
828 | } | |
829 | ||
1969db47 AS |
830 | static void free_verifier_state(struct bpf_verifier_state *state, |
831 | bool free_self) | |
638f5b90 | 832 | { |
f4d7e40a AS |
833 | int i; |
834 | ||
835 | for (i = 0; i <= state->curframe; i++) { | |
836 | free_func_state(state->frame[i]); | |
837 | state->frame[i] = NULL; | |
838 | } | |
b5dc0163 | 839 | clear_jmp_history(state); |
1969db47 AS |
840 | if (free_self) |
841 | kfree(state); | |
638f5b90 AS |
842 | } |
843 | ||
844 | /* copy verifier state from src to dst growing dst stack space | |
845 | * when necessary to accommodate larger src stack | |
846 | */ | |
f4d7e40a AS |
847 | static int copy_func_state(struct bpf_func_state *dst, |
848 | const struct bpf_func_state *src) | |
638f5b90 AS |
849 | { |
850 | int err; | |
851 | ||
fd978bf7 JS |
852 | err = realloc_func_state(dst, src->allocated_stack, src->acquired_refs, |
853 | false); | |
854 | if (err) | |
855 | return err; | |
856 | memcpy(dst, src, offsetof(struct bpf_func_state, acquired_refs)); | |
857 | err = copy_reference_state(dst, src); | |
638f5b90 AS |
858 | if (err) |
859 | return err; | |
638f5b90 AS |
860 | return copy_stack_state(dst, src); |
861 | } | |
862 | ||
f4d7e40a AS |
863 | static int copy_verifier_state(struct bpf_verifier_state *dst_state, |
864 | const struct bpf_verifier_state *src) | |
865 | { | |
866 | struct bpf_func_state *dst; | |
b5dc0163 | 867 | u32 jmp_sz = sizeof(struct bpf_idx_pair) * src->jmp_history_cnt; |
f4d7e40a AS |
868 | int i, err; |
869 | ||
b5dc0163 AS |
870 | if (dst_state->jmp_history_cnt < src->jmp_history_cnt) { |
871 | kfree(dst_state->jmp_history); | |
872 | dst_state->jmp_history = kmalloc(jmp_sz, GFP_USER); | |
873 | if (!dst_state->jmp_history) | |
874 | return -ENOMEM; | |
875 | } | |
876 | memcpy(dst_state->jmp_history, src->jmp_history, jmp_sz); | |
877 | dst_state->jmp_history_cnt = src->jmp_history_cnt; | |
878 | ||
f4d7e40a AS |
879 | /* if dst has more stack frames then src frame, free them */ |
880 | for (i = src->curframe + 1; i <= dst_state->curframe; i++) { | |
881 | free_func_state(dst_state->frame[i]); | |
882 | dst_state->frame[i] = NULL; | |
883 | } | |
979d63d5 | 884 | dst_state->speculative = src->speculative; |
f4d7e40a | 885 | dst_state->curframe = src->curframe; |
d83525ca | 886 | dst_state->active_spin_lock = src->active_spin_lock; |
2589726d AS |
887 | dst_state->branches = src->branches; |
888 | dst_state->parent = src->parent; | |
b5dc0163 AS |
889 | dst_state->first_insn_idx = src->first_insn_idx; |
890 | dst_state->last_insn_idx = src->last_insn_idx; | |
f4d7e40a AS |
891 | for (i = 0; i <= src->curframe; i++) { |
892 | dst = dst_state->frame[i]; | |
893 | if (!dst) { | |
894 | dst = kzalloc(sizeof(*dst), GFP_KERNEL); | |
895 | if (!dst) | |
896 | return -ENOMEM; | |
897 | dst_state->frame[i] = dst; | |
898 | } | |
899 | err = copy_func_state(dst, src->frame[i]); | |
900 | if (err) | |
901 | return err; | |
902 | } | |
903 | return 0; | |
904 | } | |
905 | ||
2589726d AS |
906 | static void update_branch_counts(struct bpf_verifier_env *env, struct bpf_verifier_state *st) |
907 | { | |
908 | while (st) { | |
909 | u32 br = --st->branches; | |
910 | ||
911 | /* WARN_ON(br > 1) technically makes sense here, | |
912 | * but see comment in push_stack(), hence: | |
913 | */ | |
914 | WARN_ONCE((int)br < 0, | |
915 | "BUG update_branch_counts:branches_to_explore=%d\n", | |
916 | br); | |
917 | if (br) | |
918 | break; | |
919 | st = st->parent; | |
920 | } | |
921 | } | |
922 | ||
638f5b90 | 923 | static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx, |
6f8a57cc | 924 | int *insn_idx, bool pop_log) |
638f5b90 AS |
925 | { |
926 | struct bpf_verifier_state *cur = env->cur_state; | |
927 | struct bpf_verifier_stack_elem *elem, *head = env->head; | |
928 | int err; | |
17a52670 AS |
929 | |
930 | if (env->head == NULL) | |
638f5b90 | 931 | return -ENOENT; |
17a52670 | 932 | |
638f5b90 AS |
933 | if (cur) { |
934 | err = copy_verifier_state(cur, &head->st); | |
935 | if (err) | |
936 | return err; | |
937 | } | |
6f8a57cc AN |
938 | if (pop_log) |
939 | bpf_vlog_reset(&env->log, head->log_pos); | |
638f5b90 AS |
940 | if (insn_idx) |
941 | *insn_idx = head->insn_idx; | |
17a52670 | 942 | if (prev_insn_idx) |
638f5b90 AS |
943 | *prev_insn_idx = head->prev_insn_idx; |
944 | elem = head->next; | |
1969db47 | 945 | free_verifier_state(&head->st, false); |
638f5b90 | 946 | kfree(head); |
17a52670 AS |
947 | env->head = elem; |
948 | env->stack_size--; | |
638f5b90 | 949 | return 0; |
17a52670 AS |
950 | } |
951 | ||
58e2af8b | 952 | static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, |
979d63d5 DB |
953 | int insn_idx, int prev_insn_idx, |
954 | bool speculative) | |
17a52670 | 955 | { |
638f5b90 | 956 | struct bpf_verifier_state *cur = env->cur_state; |
58e2af8b | 957 | struct bpf_verifier_stack_elem *elem; |
638f5b90 | 958 | int err; |
17a52670 | 959 | |
638f5b90 | 960 | elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL); |
17a52670 AS |
961 | if (!elem) |
962 | goto err; | |
963 | ||
17a52670 AS |
964 | elem->insn_idx = insn_idx; |
965 | elem->prev_insn_idx = prev_insn_idx; | |
966 | elem->next = env->head; | |
6f8a57cc | 967 | elem->log_pos = env->log.len_used; |
17a52670 AS |
968 | env->head = elem; |
969 | env->stack_size++; | |
1969db47 AS |
970 | err = copy_verifier_state(&elem->st, cur); |
971 | if (err) | |
972 | goto err; | |
979d63d5 | 973 | elem->st.speculative |= speculative; |
b285fcb7 AS |
974 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_JMP_SEQ) { |
975 | verbose(env, "The sequence of %d jumps is too complex.\n", | |
976 | env->stack_size); | |
17a52670 AS |
977 | goto err; |
978 | } | |
2589726d AS |
979 | if (elem->st.parent) { |
980 | ++elem->st.parent->branches; | |
981 | /* WARN_ON(branches > 2) technically makes sense here, | |
982 | * but | |
983 | * 1. speculative states will bump 'branches' for non-branch | |
984 | * instructions | |
985 | * 2. is_state_visited() heuristics may decide not to create | |
986 | * a new state for a sequence of branches and all such current | |
987 | * and cloned states will be pointing to a single parent state | |
988 | * which might have large 'branches' count. | |
989 | */ | |
990 | } | |
17a52670 AS |
991 | return &elem->st; |
992 | err: | |
5896351e AS |
993 | free_verifier_state(env->cur_state, true); |
994 | env->cur_state = NULL; | |
17a52670 | 995 | /* pop all elements and return */ |
6f8a57cc | 996 | while (!pop_stack(env, NULL, NULL, false)); |
17a52670 AS |
997 | return NULL; |
998 | } | |
999 | ||
1000 | #define CALLER_SAVED_REGS 6 | |
1001 | static const int caller_saved[CALLER_SAVED_REGS] = { | |
1002 | BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5 | |
1003 | }; | |
1004 | ||
f54c7898 DB |
1005 | static void __mark_reg_not_init(const struct bpf_verifier_env *env, |
1006 | struct bpf_reg_state *reg); | |
f1174f77 | 1007 | |
b03c9f9f EC |
1008 | /* Mark the unknown part of a register (variable offset or scalar value) as |
1009 | * known to have the value @imm. | |
1010 | */ | |
1011 | static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm) | |
1012 | { | |
a9c676bc AS |
1013 | /* Clear id, off, and union(map_ptr, range) */ |
1014 | memset(((u8 *)reg) + sizeof(reg->type), 0, | |
1015 | offsetof(struct bpf_reg_state, var_off) - sizeof(reg->type)); | |
b03c9f9f EC |
1016 | reg->var_off = tnum_const(imm); |
1017 | reg->smin_value = (s64)imm; | |
1018 | reg->smax_value = (s64)imm; | |
1019 | reg->umin_value = imm; | |
1020 | reg->umax_value = imm; | |
3f50f132 JF |
1021 | |
1022 | reg->s32_min_value = (s32)imm; | |
1023 | reg->s32_max_value = (s32)imm; | |
1024 | reg->u32_min_value = (u32)imm; | |
1025 | reg->u32_max_value = (u32)imm; | |
1026 | } | |
1027 | ||
1028 | static void __mark_reg32_known(struct bpf_reg_state *reg, u64 imm) | |
1029 | { | |
1030 | reg->var_off = tnum_const_subreg(reg->var_off, imm); | |
1031 | reg->s32_min_value = (s32)imm; | |
1032 | reg->s32_max_value = (s32)imm; | |
1033 | reg->u32_min_value = (u32)imm; | |
1034 | reg->u32_max_value = (u32)imm; | |
b03c9f9f EC |
1035 | } |
1036 | ||
f1174f77 EC |
1037 | /* Mark the 'variable offset' part of a register as zero. This should be |
1038 | * used only on registers holding a pointer type. | |
1039 | */ | |
1040 | static void __mark_reg_known_zero(struct bpf_reg_state *reg) | |
a9789ef9 | 1041 | { |
b03c9f9f | 1042 | __mark_reg_known(reg, 0); |
f1174f77 | 1043 | } |
a9789ef9 | 1044 | |
cc2b14d5 AS |
1045 | static void __mark_reg_const_zero(struct bpf_reg_state *reg) |
1046 | { | |
1047 | __mark_reg_known(reg, 0); | |
cc2b14d5 AS |
1048 | reg->type = SCALAR_VALUE; |
1049 | } | |
1050 | ||
61bd5218 JK |
1051 | static void mark_reg_known_zero(struct bpf_verifier_env *env, |
1052 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
1053 | { |
1054 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 1055 | verbose(env, "mark_reg_known_zero(regs, %u)\n", regno); |
f1174f77 EC |
1056 | /* Something bad happened, let's kill all regs */ |
1057 | for (regno = 0; regno < MAX_BPF_REG; regno++) | |
f54c7898 | 1058 | __mark_reg_not_init(env, regs + regno); |
f1174f77 EC |
1059 | return; |
1060 | } | |
1061 | __mark_reg_known_zero(regs + regno); | |
1062 | } | |
1063 | ||
de8f3a83 DB |
1064 | static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg) |
1065 | { | |
1066 | return type_is_pkt_pointer(reg->type); | |
1067 | } | |
1068 | ||
1069 | static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg) | |
1070 | { | |
1071 | return reg_is_pkt_pointer(reg) || | |
1072 | reg->type == PTR_TO_PACKET_END; | |
1073 | } | |
1074 | ||
1075 | /* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */ | |
1076 | static bool reg_is_init_pkt_pointer(const struct bpf_reg_state *reg, | |
1077 | enum bpf_reg_type which) | |
1078 | { | |
1079 | /* The register can already have a range from prior markings. | |
1080 | * This is fine as long as it hasn't been advanced from its | |
1081 | * origin. | |
1082 | */ | |
1083 | return reg->type == which && | |
1084 | reg->id == 0 && | |
1085 | reg->off == 0 && | |
1086 | tnum_equals_const(reg->var_off, 0); | |
1087 | } | |
1088 | ||
3f50f132 JF |
1089 | /* Reset the min/max bounds of a register */ |
1090 | static void __mark_reg_unbounded(struct bpf_reg_state *reg) | |
1091 | { | |
1092 | reg->smin_value = S64_MIN; | |
1093 | reg->smax_value = S64_MAX; | |
1094 | reg->umin_value = 0; | |
1095 | reg->umax_value = U64_MAX; | |
1096 | ||
1097 | reg->s32_min_value = S32_MIN; | |
1098 | reg->s32_max_value = S32_MAX; | |
1099 | reg->u32_min_value = 0; | |
1100 | reg->u32_max_value = U32_MAX; | |
1101 | } | |
1102 | ||
1103 | static void __mark_reg64_unbounded(struct bpf_reg_state *reg) | |
1104 | { | |
1105 | reg->smin_value = S64_MIN; | |
1106 | reg->smax_value = S64_MAX; | |
1107 | reg->umin_value = 0; | |
1108 | reg->umax_value = U64_MAX; | |
1109 | } | |
1110 | ||
1111 | static void __mark_reg32_unbounded(struct bpf_reg_state *reg) | |
1112 | { | |
1113 | reg->s32_min_value = S32_MIN; | |
1114 | reg->s32_max_value = S32_MAX; | |
1115 | reg->u32_min_value = 0; | |
1116 | reg->u32_max_value = U32_MAX; | |
1117 | } | |
1118 | ||
1119 | static void __update_reg32_bounds(struct bpf_reg_state *reg) | |
1120 | { | |
1121 | struct tnum var32_off = tnum_subreg(reg->var_off); | |
1122 | ||
1123 | /* min signed is max(sign bit) | min(other bits) */ | |
1124 | reg->s32_min_value = max_t(s32, reg->s32_min_value, | |
1125 | var32_off.value | (var32_off.mask & S32_MIN)); | |
1126 | /* max signed is min(sign bit) | max(other bits) */ | |
1127 | reg->s32_max_value = min_t(s32, reg->s32_max_value, | |
1128 | var32_off.value | (var32_off.mask & S32_MAX)); | |
1129 | reg->u32_min_value = max_t(u32, reg->u32_min_value, (u32)var32_off.value); | |
1130 | reg->u32_max_value = min(reg->u32_max_value, | |
1131 | (u32)(var32_off.value | var32_off.mask)); | |
1132 | } | |
1133 | ||
1134 | static void __update_reg64_bounds(struct bpf_reg_state *reg) | |
b03c9f9f EC |
1135 | { |
1136 | /* min signed is max(sign bit) | min(other bits) */ | |
1137 | reg->smin_value = max_t(s64, reg->smin_value, | |
1138 | reg->var_off.value | (reg->var_off.mask & S64_MIN)); | |
1139 | /* max signed is min(sign bit) | max(other bits) */ | |
1140 | reg->smax_value = min_t(s64, reg->smax_value, | |
1141 | reg->var_off.value | (reg->var_off.mask & S64_MAX)); | |
1142 | reg->umin_value = max(reg->umin_value, reg->var_off.value); | |
1143 | reg->umax_value = min(reg->umax_value, | |
1144 | reg->var_off.value | reg->var_off.mask); | |
1145 | } | |
1146 | ||
3f50f132 JF |
1147 | static void __update_reg_bounds(struct bpf_reg_state *reg) |
1148 | { | |
1149 | __update_reg32_bounds(reg); | |
1150 | __update_reg64_bounds(reg); | |
1151 | } | |
1152 | ||
b03c9f9f | 1153 | /* Uses signed min/max values to inform unsigned, and vice-versa */ |
3f50f132 JF |
1154 | static void __reg32_deduce_bounds(struct bpf_reg_state *reg) |
1155 | { | |
1156 | /* Learn sign from signed bounds. | |
1157 | * If we cannot cross the sign boundary, then signed and unsigned bounds | |
1158 | * are the same, so combine. This works even in the negative case, e.g. | |
1159 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. | |
1160 | */ | |
1161 | if (reg->s32_min_value >= 0 || reg->s32_max_value < 0) { | |
1162 | reg->s32_min_value = reg->u32_min_value = | |
1163 | max_t(u32, reg->s32_min_value, reg->u32_min_value); | |
1164 | reg->s32_max_value = reg->u32_max_value = | |
1165 | min_t(u32, reg->s32_max_value, reg->u32_max_value); | |
1166 | return; | |
1167 | } | |
1168 | /* Learn sign from unsigned bounds. Signed bounds cross the sign | |
1169 | * boundary, so we must be careful. | |
1170 | */ | |
1171 | if ((s32)reg->u32_max_value >= 0) { | |
1172 | /* Positive. We can't learn anything from the smin, but smax | |
1173 | * is positive, hence safe. | |
1174 | */ | |
1175 | reg->s32_min_value = reg->u32_min_value; | |
1176 | reg->s32_max_value = reg->u32_max_value = | |
1177 | min_t(u32, reg->s32_max_value, reg->u32_max_value); | |
1178 | } else if ((s32)reg->u32_min_value < 0) { | |
1179 | /* Negative. We can't learn anything from the smax, but smin | |
1180 | * is negative, hence safe. | |
1181 | */ | |
1182 | reg->s32_min_value = reg->u32_min_value = | |
1183 | max_t(u32, reg->s32_min_value, reg->u32_min_value); | |
1184 | reg->s32_max_value = reg->u32_max_value; | |
1185 | } | |
1186 | } | |
1187 | ||
1188 | static void __reg64_deduce_bounds(struct bpf_reg_state *reg) | |
b03c9f9f EC |
1189 | { |
1190 | /* Learn sign from signed bounds. | |
1191 | * If we cannot cross the sign boundary, then signed and unsigned bounds | |
1192 | * are the same, so combine. This works even in the negative case, e.g. | |
1193 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. | |
1194 | */ | |
1195 | if (reg->smin_value >= 0 || reg->smax_value < 0) { | |
1196 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, | |
1197 | reg->umin_value); | |
1198 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, | |
1199 | reg->umax_value); | |
1200 | return; | |
1201 | } | |
1202 | /* Learn sign from unsigned bounds. Signed bounds cross the sign | |
1203 | * boundary, so we must be careful. | |
1204 | */ | |
1205 | if ((s64)reg->umax_value >= 0) { | |
1206 | /* Positive. We can't learn anything from the smin, but smax | |
1207 | * is positive, hence safe. | |
1208 | */ | |
1209 | reg->smin_value = reg->umin_value; | |
1210 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, | |
1211 | reg->umax_value); | |
1212 | } else if ((s64)reg->umin_value < 0) { | |
1213 | /* Negative. We can't learn anything from the smax, but smin | |
1214 | * is negative, hence safe. | |
1215 | */ | |
1216 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, | |
1217 | reg->umin_value); | |
1218 | reg->smax_value = reg->umax_value; | |
1219 | } | |
1220 | } | |
1221 | ||
3f50f132 JF |
1222 | static void __reg_deduce_bounds(struct bpf_reg_state *reg) |
1223 | { | |
1224 | __reg32_deduce_bounds(reg); | |
1225 | __reg64_deduce_bounds(reg); | |
1226 | } | |
1227 | ||
b03c9f9f EC |
1228 | /* Attempts to improve var_off based on unsigned min/max information */ |
1229 | static void __reg_bound_offset(struct bpf_reg_state *reg) | |
1230 | { | |
3f50f132 JF |
1231 | struct tnum var64_off = tnum_intersect(reg->var_off, |
1232 | tnum_range(reg->umin_value, | |
1233 | reg->umax_value)); | |
1234 | struct tnum var32_off = tnum_intersect(tnum_subreg(reg->var_off), | |
1235 | tnum_range(reg->u32_min_value, | |
1236 | reg->u32_max_value)); | |
1237 | ||
1238 | reg->var_off = tnum_or(tnum_clear_subreg(var64_off), var32_off); | |
b03c9f9f EC |
1239 | } |
1240 | ||
3f50f132 | 1241 | static void __reg_assign_32_into_64(struct bpf_reg_state *reg) |
b03c9f9f | 1242 | { |
3f50f132 JF |
1243 | reg->umin_value = reg->u32_min_value; |
1244 | reg->umax_value = reg->u32_max_value; | |
1245 | /* Attempt to pull 32-bit signed bounds into 64-bit bounds | |
1246 | * but must be positive otherwise set to worse case bounds | |
1247 | * and refine later from tnum. | |
1248 | */ | |
3a71dc36 | 1249 | if (reg->s32_min_value >= 0 && reg->s32_max_value >= 0) |
3f50f132 JF |
1250 | reg->smax_value = reg->s32_max_value; |
1251 | else | |
1252 | reg->smax_value = U32_MAX; | |
3a71dc36 JF |
1253 | if (reg->s32_min_value >= 0) |
1254 | reg->smin_value = reg->s32_min_value; | |
1255 | else | |
1256 | reg->smin_value = 0; | |
3f50f132 JF |
1257 | } |
1258 | ||
1259 | static void __reg_combine_32_into_64(struct bpf_reg_state *reg) | |
1260 | { | |
1261 | /* special case when 64-bit register has upper 32-bit register | |
1262 | * zeroed. Typically happens after zext or <<32, >>32 sequence | |
1263 | * allowing us to use 32-bit bounds directly, | |
1264 | */ | |
1265 | if (tnum_equals_const(tnum_clear_subreg(reg->var_off), 0)) { | |
1266 | __reg_assign_32_into_64(reg); | |
1267 | } else { | |
1268 | /* Otherwise the best we can do is push lower 32bit known and | |
1269 | * unknown bits into register (var_off set from jmp logic) | |
1270 | * then learn as much as possible from the 64-bit tnum | |
1271 | * known and unknown bits. The previous smin/smax bounds are | |
1272 | * invalid here because of jmp32 compare so mark them unknown | |
1273 | * so they do not impact tnum bounds calculation. | |
1274 | */ | |
1275 | __mark_reg64_unbounded(reg); | |
1276 | __update_reg_bounds(reg); | |
1277 | } | |
1278 | ||
1279 | /* Intersecting with the old var_off might have improved our bounds | |
1280 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), | |
1281 | * then new var_off is (0; 0x7f...fc) which improves our umax. | |
1282 | */ | |
1283 | __reg_deduce_bounds(reg); | |
1284 | __reg_bound_offset(reg); | |
1285 | __update_reg_bounds(reg); | |
1286 | } | |
1287 | ||
1288 | static bool __reg64_bound_s32(s64 a) | |
1289 | { | |
1290 | if (a > S32_MIN && a < S32_MAX) | |
1291 | return true; | |
1292 | return false; | |
1293 | } | |
1294 | ||
1295 | static bool __reg64_bound_u32(u64 a) | |
1296 | { | |
1297 | if (a > U32_MIN && a < U32_MAX) | |
1298 | return true; | |
1299 | return false; | |
1300 | } | |
1301 | ||
1302 | static void __reg_combine_64_into_32(struct bpf_reg_state *reg) | |
1303 | { | |
1304 | __mark_reg32_unbounded(reg); | |
1305 | ||
1306 | if (__reg64_bound_s32(reg->smin_value)) | |
1307 | reg->s32_min_value = (s32)reg->smin_value; | |
1308 | if (__reg64_bound_s32(reg->smax_value)) | |
1309 | reg->s32_max_value = (s32)reg->smax_value; | |
1310 | if (__reg64_bound_u32(reg->umin_value)) | |
1311 | reg->u32_min_value = (u32)reg->umin_value; | |
1312 | if (__reg64_bound_u32(reg->umax_value)) | |
1313 | reg->u32_max_value = (u32)reg->umax_value; | |
1314 | ||
1315 | /* Intersecting with the old var_off might have improved our bounds | |
1316 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), | |
1317 | * then new var_off is (0; 0x7f...fc) which improves our umax. | |
1318 | */ | |
1319 | __reg_deduce_bounds(reg); | |
1320 | __reg_bound_offset(reg); | |
1321 | __update_reg_bounds(reg); | |
b03c9f9f EC |
1322 | } |
1323 | ||
f1174f77 | 1324 | /* Mark a register as having a completely unknown (scalar) value. */ |
f54c7898 DB |
1325 | static void __mark_reg_unknown(const struct bpf_verifier_env *env, |
1326 | struct bpf_reg_state *reg) | |
f1174f77 | 1327 | { |
a9c676bc AS |
1328 | /* |
1329 | * Clear type, id, off, and union(map_ptr, range) and | |
1330 | * padding between 'type' and union | |
1331 | */ | |
1332 | memset(reg, 0, offsetof(struct bpf_reg_state, var_off)); | |
f1174f77 | 1333 | reg->type = SCALAR_VALUE; |
f1174f77 | 1334 | reg->var_off = tnum_unknown; |
f4d7e40a | 1335 | reg->frameno = 0; |
2c78ee89 | 1336 | reg->precise = env->subprog_cnt > 1 || !env->bpf_capable; |
b03c9f9f | 1337 | __mark_reg_unbounded(reg); |
f1174f77 EC |
1338 | } |
1339 | ||
61bd5218 JK |
1340 | static void mark_reg_unknown(struct bpf_verifier_env *env, |
1341 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
1342 | { |
1343 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 1344 | verbose(env, "mark_reg_unknown(regs, %u)\n", regno); |
19ceb417 AS |
1345 | /* Something bad happened, let's kill all regs except FP */ |
1346 | for (regno = 0; regno < BPF_REG_FP; regno++) | |
f54c7898 | 1347 | __mark_reg_not_init(env, regs + regno); |
f1174f77 EC |
1348 | return; |
1349 | } | |
f54c7898 | 1350 | __mark_reg_unknown(env, regs + regno); |
f1174f77 EC |
1351 | } |
1352 | ||
f54c7898 DB |
1353 | static void __mark_reg_not_init(const struct bpf_verifier_env *env, |
1354 | struct bpf_reg_state *reg) | |
f1174f77 | 1355 | { |
f54c7898 | 1356 | __mark_reg_unknown(env, reg); |
f1174f77 EC |
1357 | reg->type = NOT_INIT; |
1358 | } | |
1359 | ||
61bd5218 JK |
1360 | static void mark_reg_not_init(struct bpf_verifier_env *env, |
1361 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
1362 | { |
1363 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 1364 | verbose(env, "mark_reg_not_init(regs, %u)\n", regno); |
19ceb417 AS |
1365 | /* Something bad happened, let's kill all regs except FP */ |
1366 | for (regno = 0; regno < BPF_REG_FP; regno++) | |
f54c7898 | 1367 | __mark_reg_not_init(env, regs + regno); |
f1174f77 EC |
1368 | return; |
1369 | } | |
f54c7898 | 1370 | __mark_reg_not_init(env, regs + regno); |
a9789ef9 DB |
1371 | } |
1372 | ||
41c48f3a AI |
1373 | static void mark_btf_ld_reg(struct bpf_verifier_env *env, |
1374 | struct bpf_reg_state *regs, u32 regno, | |
1375 | enum bpf_reg_type reg_type, u32 btf_id) | |
1376 | { | |
1377 | if (reg_type == SCALAR_VALUE) { | |
1378 | mark_reg_unknown(env, regs, regno); | |
1379 | return; | |
1380 | } | |
1381 | mark_reg_known_zero(env, regs, regno); | |
1382 | regs[regno].type = PTR_TO_BTF_ID; | |
1383 | regs[regno].btf_id = btf_id; | |
1384 | } | |
1385 | ||
5327ed3d | 1386 | #define DEF_NOT_SUBREG (0) |
61bd5218 | 1387 | static void init_reg_state(struct bpf_verifier_env *env, |
f4d7e40a | 1388 | struct bpf_func_state *state) |
17a52670 | 1389 | { |
f4d7e40a | 1390 | struct bpf_reg_state *regs = state->regs; |
17a52670 AS |
1391 | int i; |
1392 | ||
dc503a8a | 1393 | for (i = 0; i < MAX_BPF_REG; i++) { |
61bd5218 | 1394 | mark_reg_not_init(env, regs, i); |
dc503a8a | 1395 | regs[i].live = REG_LIVE_NONE; |
679c782d | 1396 | regs[i].parent = NULL; |
5327ed3d | 1397 | regs[i].subreg_def = DEF_NOT_SUBREG; |
dc503a8a | 1398 | } |
17a52670 AS |
1399 | |
1400 | /* frame pointer */ | |
f1174f77 | 1401 | regs[BPF_REG_FP].type = PTR_TO_STACK; |
61bd5218 | 1402 | mark_reg_known_zero(env, regs, BPF_REG_FP); |
f4d7e40a | 1403 | regs[BPF_REG_FP].frameno = state->frameno; |
6760bf2d DB |
1404 | } |
1405 | ||
f4d7e40a AS |
1406 | #define BPF_MAIN_FUNC (-1) |
1407 | static void init_func_state(struct bpf_verifier_env *env, | |
1408 | struct bpf_func_state *state, | |
1409 | int callsite, int frameno, int subprogno) | |
1410 | { | |
1411 | state->callsite = callsite; | |
1412 | state->frameno = frameno; | |
1413 | state->subprogno = subprogno; | |
1414 | init_reg_state(env, state); | |
1415 | } | |
1416 | ||
17a52670 AS |
1417 | enum reg_arg_type { |
1418 | SRC_OP, /* register is used as source operand */ | |
1419 | DST_OP, /* register is used as destination operand */ | |
1420 | DST_OP_NO_MARK /* same as above, check only, don't mark */ | |
1421 | }; | |
1422 | ||
cc8b0b92 AS |
1423 | static int cmp_subprogs(const void *a, const void *b) |
1424 | { | |
9c8105bd JW |
1425 | return ((struct bpf_subprog_info *)a)->start - |
1426 | ((struct bpf_subprog_info *)b)->start; | |
cc8b0b92 AS |
1427 | } |
1428 | ||
1429 | static int find_subprog(struct bpf_verifier_env *env, int off) | |
1430 | { | |
9c8105bd | 1431 | struct bpf_subprog_info *p; |
cc8b0b92 | 1432 | |
9c8105bd JW |
1433 | p = bsearch(&off, env->subprog_info, env->subprog_cnt, |
1434 | sizeof(env->subprog_info[0]), cmp_subprogs); | |
cc8b0b92 AS |
1435 | if (!p) |
1436 | return -ENOENT; | |
9c8105bd | 1437 | return p - env->subprog_info; |
cc8b0b92 AS |
1438 | |
1439 | } | |
1440 | ||
1441 | static int add_subprog(struct bpf_verifier_env *env, int off) | |
1442 | { | |
1443 | int insn_cnt = env->prog->len; | |
1444 | int ret; | |
1445 | ||
1446 | if (off >= insn_cnt || off < 0) { | |
1447 | verbose(env, "call to invalid destination\n"); | |
1448 | return -EINVAL; | |
1449 | } | |
1450 | ret = find_subprog(env, off); | |
1451 | if (ret >= 0) | |
1452 | return 0; | |
4cb3d99c | 1453 | if (env->subprog_cnt >= BPF_MAX_SUBPROGS) { |
cc8b0b92 AS |
1454 | verbose(env, "too many subprograms\n"); |
1455 | return -E2BIG; | |
1456 | } | |
9c8105bd JW |
1457 | env->subprog_info[env->subprog_cnt++].start = off; |
1458 | sort(env->subprog_info, env->subprog_cnt, | |
1459 | sizeof(env->subprog_info[0]), cmp_subprogs, NULL); | |
cc8b0b92 AS |
1460 | return 0; |
1461 | } | |
1462 | ||
1463 | static int check_subprogs(struct bpf_verifier_env *env) | |
1464 | { | |
1465 | int i, ret, subprog_start, subprog_end, off, cur_subprog = 0; | |
9c8105bd | 1466 | struct bpf_subprog_info *subprog = env->subprog_info; |
cc8b0b92 AS |
1467 | struct bpf_insn *insn = env->prog->insnsi; |
1468 | int insn_cnt = env->prog->len; | |
1469 | ||
f910cefa JW |
1470 | /* Add entry function. */ |
1471 | ret = add_subprog(env, 0); | |
1472 | if (ret < 0) | |
1473 | return ret; | |
1474 | ||
cc8b0b92 AS |
1475 | /* determine subprog starts. The end is one before the next starts */ |
1476 | for (i = 0; i < insn_cnt; i++) { | |
1477 | if (insn[i].code != (BPF_JMP | BPF_CALL)) | |
1478 | continue; | |
1479 | if (insn[i].src_reg != BPF_PSEUDO_CALL) | |
1480 | continue; | |
2c78ee89 AS |
1481 | if (!env->bpf_capable) { |
1482 | verbose(env, | |
1483 | "function calls to other bpf functions are allowed for CAP_BPF and CAP_SYS_ADMIN\n"); | |
cc8b0b92 AS |
1484 | return -EPERM; |
1485 | } | |
cc8b0b92 AS |
1486 | ret = add_subprog(env, i + insn[i].imm + 1); |
1487 | if (ret < 0) | |
1488 | return ret; | |
1489 | } | |
1490 | ||
4cb3d99c JW |
1491 | /* Add a fake 'exit' subprog which could simplify subprog iteration |
1492 | * logic. 'subprog_cnt' should not be increased. | |
1493 | */ | |
1494 | subprog[env->subprog_cnt].start = insn_cnt; | |
1495 | ||
06ee7115 | 1496 | if (env->log.level & BPF_LOG_LEVEL2) |
cc8b0b92 | 1497 | for (i = 0; i < env->subprog_cnt; i++) |
9c8105bd | 1498 | verbose(env, "func#%d @%d\n", i, subprog[i].start); |
cc8b0b92 AS |
1499 | |
1500 | /* now check that all jumps are within the same subprog */ | |
4cb3d99c JW |
1501 | subprog_start = subprog[cur_subprog].start; |
1502 | subprog_end = subprog[cur_subprog + 1].start; | |
cc8b0b92 AS |
1503 | for (i = 0; i < insn_cnt; i++) { |
1504 | u8 code = insn[i].code; | |
1505 | ||
7f6e4312 MF |
1506 | if (code == (BPF_JMP | BPF_CALL) && |
1507 | insn[i].imm == BPF_FUNC_tail_call && | |
1508 | insn[i].src_reg != BPF_PSEUDO_CALL) | |
1509 | subprog[cur_subprog].has_tail_call = true; | |
09b28d76 AS |
1510 | if (BPF_CLASS(code) == BPF_LD && |
1511 | (BPF_MODE(code) == BPF_ABS || BPF_MODE(code) == BPF_IND)) | |
1512 | subprog[cur_subprog].has_ld_abs = true; | |
092ed096 | 1513 | if (BPF_CLASS(code) != BPF_JMP && BPF_CLASS(code) != BPF_JMP32) |
cc8b0b92 AS |
1514 | goto next; |
1515 | if (BPF_OP(code) == BPF_EXIT || BPF_OP(code) == BPF_CALL) | |
1516 | goto next; | |
1517 | off = i + insn[i].off + 1; | |
1518 | if (off < subprog_start || off >= subprog_end) { | |
1519 | verbose(env, "jump out of range from insn %d to %d\n", i, off); | |
1520 | return -EINVAL; | |
1521 | } | |
1522 | next: | |
1523 | if (i == subprog_end - 1) { | |
1524 | /* to avoid fall-through from one subprog into another | |
1525 | * the last insn of the subprog should be either exit | |
1526 | * or unconditional jump back | |
1527 | */ | |
1528 | if (code != (BPF_JMP | BPF_EXIT) && | |
1529 | code != (BPF_JMP | BPF_JA)) { | |
1530 | verbose(env, "last insn is not an exit or jmp\n"); | |
1531 | return -EINVAL; | |
1532 | } | |
1533 | subprog_start = subprog_end; | |
4cb3d99c JW |
1534 | cur_subprog++; |
1535 | if (cur_subprog < env->subprog_cnt) | |
9c8105bd | 1536 | subprog_end = subprog[cur_subprog + 1].start; |
cc8b0b92 AS |
1537 | } |
1538 | } | |
1539 | return 0; | |
1540 | } | |
1541 | ||
679c782d EC |
1542 | /* Parentage chain of this register (or stack slot) should take care of all |
1543 | * issues like callee-saved registers, stack slot allocation time, etc. | |
1544 | */ | |
f4d7e40a | 1545 | static int mark_reg_read(struct bpf_verifier_env *env, |
679c782d | 1546 | const struct bpf_reg_state *state, |
5327ed3d | 1547 | struct bpf_reg_state *parent, u8 flag) |
f4d7e40a AS |
1548 | { |
1549 | bool writes = parent == state->parent; /* Observe write marks */ | |
06ee7115 | 1550 | int cnt = 0; |
dc503a8a EC |
1551 | |
1552 | while (parent) { | |
1553 | /* if read wasn't screened by an earlier write ... */ | |
679c782d | 1554 | if (writes && state->live & REG_LIVE_WRITTEN) |
dc503a8a | 1555 | break; |
9242b5f5 AS |
1556 | if (parent->live & REG_LIVE_DONE) { |
1557 | verbose(env, "verifier BUG type %s var_off %lld off %d\n", | |
1558 | reg_type_str[parent->type], | |
1559 | parent->var_off.value, parent->off); | |
1560 | return -EFAULT; | |
1561 | } | |
5327ed3d JW |
1562 | /* The first condition is more likely to be true than the |
1563 | * second, checked it first. | |
1564 | */ | |
1565 | if ((parent->live & REG_LIVE_READ) == flag || | |
1566 | parent->live & REG_LIVE_READ64) | |
25af32da AS |
1567 | /* The parentage chain never changes and |
1568 | * this parent was already marked as LIVE_READ. | |
1569 | * There is no need to keep walking the chain again and | |
1570 | * keep re-marking all parents as LIVE_READ. | |
1571 | * This case happens when the same register is read | |
1572 | * multiple times without writes into it in-between. | |
5327ed3d JW |
1573 | * Also, if parent has the stronger REG_LIVE_READ64 set, |
1574 | * then no need to set the weak REG_LIVE_READ32. | |
25af32da AS |
1575 | */ |
1576 | break; | |
dc503a8a | 1577 | /* ... then we depend on parent's value */ |
5327ed3d JW |
1578 | parent->live |= flag; |
1579 | /* REG_LIVE_READ64 overrides REG_LIVE_READ32. */ | |
1580 | if (flag == REG_LIVE_READ64) | |
1581 | parent->live &= ~REG_LIVE_READ32; | |
dc503a8a EC |
1582 | state = parent; |
1583 | parent = state->parent; | |
f4d7e40a | 1584 | writes = true; |
06ee7115 | 1585 | cnt++; |
dc503a8a | 1586 | } |
06ee7115 AS |
1587 | |
1588 | if (env->longest_mark_read_walk < cnt) | |
1589 | env->longest_mark_read_walk = cnt; | |
f4d7e40a | 1590 | return 0; |
dc503a8a EC |
1591 | } |
1592 | ||
5327ed3d JW |
1593 | /* This function is supposed to be used by the following 32-bit optimization |
1594 | * code only. It returns TRUE if the source or destination register operates | |
1595 | * on 64-bit, otherwise return FALSE. | |
1596 | */ | |
1597 | static bool is_reg64(struct bpf_verifier_env *env, struct bpf_insn *insn, | |
1598 | u32 regno, struct bpf_reg_state *reg, enum reg_arg_type t) | |
1599 | { | |
1600 | u8 code, class, op; | |
1601 | ||
1602 | code = insn->code; | |
1603 | class = BPF_CLASS(code); | |
1604 | op = BPF_OP(code); | |
1605 | if (class == BPF_JMP) { | |
1606 | /* BPF_EXIT for "main" will reach here. Return TRUE | |
1607 | * conservatively. | |
1608 | */ | |
1609 | if (op == BPF_EXIT) | |
1610 | return true; | |
1611 | if (op == BPF_CALL) { | |
1612 | /* BPF to BPF call will reach here because of marking | |
1613 | * caller saved clobber with DST_OP_NO_MARK for which we | |
1614 | * don't care the register def because they are anyway | |
1615 | * marked as NOT_INIT already. | |
1616 | */ | |
1617 | if (insn->src_reg == BPF_PSEUDO_CALL) | |
1618 | return false; | |
1619 | /* Helper call will reach here because of arg type | |
1620 | * check, conservatively return TRUE. | |
1621 | */ | |
1622 | if (t == SRC_OP) | |
1623 | return true; | |
1624 | ||
1625 | return false; | |
1626 | } | |
1627 | } | |
1628 | ||
1629 | if (class == BPF_ALU64 || class == BPF_JMP || | |
1630 | /* BPF_END always use BPF_ALU class. */ | |
1631 | (class == BPF_ALU && op == BPF_END && insn->imm == 64)) | |
1632 | return true; | |
1633 | ||
1634 | if (class == BPF_ALU || class == BPF_JMP32) | |
1635 | return false; | |
1636 | ||
1637 | if (class == BPF_LDX) { | |
1638 | if (t != SRC_OP) | |
1639 | return BPF_SIZE(code) == BPF_DW; | |
1640 | /* LDX source must be ptr. */ | |
1641 | return true; | |
1642 | } | |
1643 | ||
1644 | if (class == BPF_STX) { | |
1645 | if (reg->type != SCALAR_VALUE) | |
1646 | return true; | |
1647 | return BPF_SIZE(code) == BPF_DW; | |
1648 | } | |
1649 | ||
1650 | if (class == BPF_LD) { | |
1651 | u8 mode = BPF_MODE(code); | |
1652 | ||
1653 | /* LD_IMM64 */ | |
1654 | if (mode == BPF_IMM) | |
1655 | return true; | |
1656 | ||
1657 | /* Both LD_IND and LD_ABS return 32-bit data. */ | |
1658 | if (t != SRC_OP) | |
1659 | return false; | |
1660 | ||
1661 | /* Implicit ctx ptr. */ | |
1662 | if (regno == BPF_REG_6) | |
1663 | return true; | |
1664 | ||
1665 | /* Explicit source could be any width. */ | |
1666 | return true; | |
1667 | } | |
1668 | ||
1669 | if (class == BPF_ST) | |
1670 | /* The only source register for BPF_ST is a ptr. */ | |
1671 | return true; | |
1672 | ||
1673 | /* Conservatively return true at default. */ | |
1674 | return true; | |
1675 | } | |
1676 | ||
b325fbca JW |
1677 | /* Return TRUE if INSN doesn't have explicit value define. */ |
1678 | static bool insn_no_def(struct bpf_insn *insn) | |
1679 | { | |
1680 | u8 class = BPF_CLASS(insn->code); | |
1681 | ||
1682 | return (class == BPF_JMP || class == BPF_JMP32 || | |
1683 | class == BPF_STX || class == BPF_ST); | |
1684 | } | |
1685 | ||
1686 | /* Return TRUE if INSN has defined any 32-bit value explicitly. */ | |
1687 | static bool insn_has_def32(struct bpf_verifier_env *env, struct bpf_insn *insn) | |
1688 | { | |
1689 | if (insn_no_def(insn)) | |
1690 | return false; | |
1691 | ||
1692 | return !is_reg64(env, insn, insn->dst_reg, NULL, DST_OP); | |
1693 | } | |
1694 | ||
5327ed3d JW |
1695 | static void mark_insn_zext(struct bpf_verifier_env *env, |
1696 | struct bpf_reg_state *reg) | |
1697 | { | |
1698 | s32 def_idx = reg->subreg_def; | |
1699 | ||
1700 | if (def_idx == DEF_NOT_SUBREG) | |
1701 | return; | |
1702 | ||
1703 | env->insn_aux_data[def_idx - 1].zext_dst = true; | |
1704 | /* The dst will be zero extended, so won't be sub-register anymore. */ | |
1705 | reg->subreg_def = DEF_NOT_SUBREG; | |
1706 | } | |
1707 | ||
dc503a8a | 1708 | static int check_reg_arg(struct bpf_verifier_env *env, u32 regno, |
17a52670 AS |
1709 | enum reg_arg_type t) |
1710 | { | |
f4d7e40a AS |
1711 | struct bpf_verifier_state *vstate = env->cur_state; |
1712 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
5327ed3d | 1713 | struct bpf_insn *insn = env->prog->insnsi + env->insn_idx; |
c342dc10 | 1714 | struct bpf_reg_state *reg, *regs = state->regs; |
5327ed3d | 1715 | bool rw64; |
dc503a8a | 1716 | |
17a52670 | 1717 | if (regno >= MAX_BPF_REG) { |
61bd5218 | 1718 | verbose(env, "R%d is invalid\n", regno); |
17a52670 AS |
1719 | return -EINVAL; |
1720 | } | |
1721 | ||
c342dc10 | 1722 | reg = ®s[regno]; |
5327ed3d | 1723 | rw64 = is_reg64(env, insn, regno, reg, t); |
17a52670 AS |
1724 | if (t == SRC_OP) { |
1725 | /* check whether register used as source operand can be read */ | |
c342dc10 | 1726 | if (reg->type == NOT_INIT) { |
61bd5218 | 1727 | verbose(env, "R%d !read_ok\n", regno); |
17a52670 AS |
1728 | return -EACCES; |
1729 | } | |
679c782d | 1730 | /* We don't need to worry about FP liveness because it's read-only */ |
c342dc10 JW |
1731 | if (regno == BPF_REG_FP) |
1732 | return 0; | |
1733 | ||
5327ed3d JW |
1734 | if (rw64) |
1735 | mark_insn_zext(env, reg); | |
1736 | ||
1737 | return mark_reg_read(env, reg, reg->parent, | |
1738 | rw64 ? REG_LIVE_READ64 : REG_LIVE_READ32); | |
17a52670 AS |
1739 | } else { |
1740 | /* check whether register used as dest operand can be written to */ | |
1741 | if (regno == BPF_REG_FP) { | |
61bd5218 | 1742 | verbose(env, "frame pointer is read only\n"); |
17a52670 AS |
1743 | return -EACCES; |
1744 | } | |
c342dc10 | 1745 | reg->live |= REG_LIVE_WRITTEN; |
5327ed3d | 1746 | reg->subreg_def = rw64 ? DEF_NOT_SUBREG : env->insn_idx + 1; |
17a52670 | 1747 | if (t == DST_OP) |
61bd5218 | 1748 | mark_reg_unknown(env, regs, regno); |
17a52670 AS |
1749 | } |
1750 | return 0; | |
1751 | } | |
1752 | ||
b5dc0163 AS |
1753 | /* for any branch, call, exit record the history of jmps in the given state */ |
1754 | static int push_jmp_history(struct bpf_verifier_env *env, | |
1755 | struct bpf_verifier_state *cur) | |
1756 | { | |
1757 | u32 cnt = cur->jmp_history_cnt; | |
1758 | struct bpf_idx_pair *p; | |
1759 | ||
1760 | cnt++; | |
1761 | p = krealloc(cur->jmp_history, cnt * sizeof(*p), GFP_USER); | |
1762 | if (!p) | |
1763 | return -ENOMEM; | |
1764 | p[cnt - 1].idx = env->insn_idx; | |
1765 | p[cnt - 1].prev_idx = env->prev_insn_idx; | |
1766 | cur->jmp_history = p; | |
1767 | cur->jmp_history_cnt = cnt; | |
1768 | return 0; | |
1769 | } | |
1770 | ||
1771 | /* Backtrack one insn at a time. If idx is not at the top of recorded | |
1772 | * history then previous instruction came from straight line execution. | |
1773 | */ | |
1774 | static int get_prev_insn_idx(struct bpf_verifier_state *st, int i, | |
1775 | u32 *history) | |
1776 | { | |
1777 | u32 cnt = *history; | |
1778 | ||
1779 | if (cnt && st->jmp_history[cnt - 1].idx == i) { | |
1780 | i = st->jmp_history[cnt - 1].prev_idx; | |
1781 | (*history)--; | |
1782 | } else { | |
1783 | i--; | |
1784 | } | |
1785 | return i; | |
1786 | } | |
1787 | ||
1788 | /* For given verifier state backtrack_insn() is called from the last insn to | |
1789 | * the first insn. Its purpose is to compute a bitmask of registers and | |
1790 | * stack slots that needs precision in the parent verifier state. | |
1791 | */ | |
1792 | static int backtrack_insn(struct bpf_verifier_env *env, int idx, | |
1793 | u32 *reg_mask, u64 *stack_mask) | |
1794 | { | |
1795 | const struct bpf_insn_cbs cbs = { | |
1796 | .cb_print = verbose, | |
1797 | .private_data = env, | |
1798 | }; | |
1799 | struct bpf_insn *insn = env->prog->insnsi + idx; | |
1800 | u8 class = BPF_CLASS(insn->code); | |
1801 | u8 opcode = BPF_OP(insn->code); | |
1802 | u8 mode = BPF_MODE(insn->code); | |
1803 | u32 dreg = 1u << insn->dst_reg; | |
1804 | u32 sreg = 1u << insn->src_reg; | |
1805 | u32 spi; | |
1806 | ||
1807 | if (insn->code == 0) | |
1808 | return 0; | |
1809 | if (env->log.level & BPF_LOG_LEVEL) { | |
1810 | verbose(env, "regs=%x stack=%llx before ", *reg_mask, *stack_mask); | |
1811 | verbose(env, "%d: ", idx); | |
1812 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); | |
1813 | } | |
1814 | ||
1815 | if (class == BPF_ALU || class == BPF_ALU64) { | |
1816 | if (!(*reg_mask & dreg)) | |
1817 | return 0; | |
1818 | if (opcode == BPF_MOV) { | |
1819 | if (BPF_SRC(insn->code) == BPF_X) { | |
1820 | /* dreg = sreg | |
1821 | * dreg needs precision after this insn | |
1822 | * sreg needs precision before this insn | |
1823 | */ | |
1824 | *reg_mask &= ~dreg; | |
1825 | *reg_mask |= sreg; | |
1826 | } else { | |
1827 | /* dreg = K | |
1828 | * dreg needs precision after this insn. | |
1829 | * Corresponding register is already marked | |
1830 | * as precise=true in this verifier state. | |
1831 | * No further markings in parent are necessary | |
1832 | */ | |
1833 | *reg_mask &= ~dreg; | |
1834 | } | |
1835 | } else { | |
1836 | if (BPF_SRC(insn->code) == BPF_X) { | |
1837 | /* dreg += sreg | |
1838 | * both dreg and sreg need precision | |
1839 | * before this insn | |
1840 | */ | |
1841 | *reg_mask |= sreg; | |
1842 | } /* else dreg += K | |
1843 | * dreg still needs precision before this insn | |
1844 | */ | |
1845 | } | |
1846 | } else if (class == BPF_LDX) { | |
1847 | if (!(*reg_mask & dreg)) | |
1848 | return 0; | |
1849 | *reg_mask &= ~dreg; | |
1850 | ||
1851 | /* scalars can only be spilled into stack w/o losing precision. | |
1852 | * Load from any other memory can be zero extended. | |
1853 | * The desire to keep that precision is already indicated | |
1854 | * by 'precise' mark in corresponding register of this state. | |
1855 | * No further tracking necessary. | |
1856 | */ | |
1857 | if (insn->src_reg != BPF_REG_FP) | |
1858 | return 0; | |
1859 | if (BPF_SIZE(insn->code) != BPF_DW) | |
1860 | return 0; | |
1861 | ||
1862 | /* dreg = *(u64 *)[fp - off] was a fill from the stack. | |
1863 | * that [fp - off] slot contains scalar that needs to be | |
1864 | * tracked with precision | |
1865 | */ | |
1866 | spi = (-insn->off - 1) / BPF_REG_SIZE; | |
1867 | if (spi >= 64) { | |
1868 | verbose(env, "BUG spi %d\n", spi); | |
1869 | WARN_ONCE(1, "verifier backtracking bug"); | |
1870 | return -EFAULT; | |
1871 | } | |
1872 | *stack_mask |= 1ull << spi; | |
b3b50f05 | 1873 | } else if (class == BPF_STX || class == BPF_ST) { |
b5dc0163 | 1874 | if (*reg_mask & dreg) |
b3b50f05 | 1875 | /* stx & st shouldn't be using _scalar_ dst_reg |
b5dc0163 AS |
1876 | * to access memory. It means backtracking |
1877 | * encountered a case of pointer subtraction. | |
1878 | */ | |
1879 | return -ENOTSUPP; | |
1880 | /* scalars can only be spilled into stack */ | |
1881 | if (insn->dst_reg != BPF_REG_FP) | |
1882 | return 0; | |
1883 | if (BPF_SIZE(insn->code) != BPF_DW) | |
1884 | return 0; | |
1885 | spi = (-insn->off - 1) / BPF_REG_SIZE; | |
1886 | if (spi >= 64) { | |
1887 | verbose(env, "BUG spi %d\n", spi); | |
1888 | WARN_ONCE(1, "verifier backtracking bug"); | |
1889 | return -EFAULT; | |
1890 | } | |
1891 | if (!(*stack_mask & (1ull << spi))) | |
1892 | return 0; | |
1893 | *stack_mask &= ~(1ull << spi); | |
b3b50f05 AN |
1894 | if (class == BPF_STX) |
1895 | *reg_mask |= sreg; | |
b5dc0163 AS |
1896 | } else if (class == BPF_JMP || class == BPF_JMP32) { |
1897 | if (opcode == BPF_CALL) { | |
1898 | if (insn->src_reg == BPF_PSEUDO_CALL) | |
1899 | return -ENOTSUPP; | |
1900 | /* regular helper call sets R0 */ | |
1901 | *reg_mask &= ~1; | |
1902 | if (*reg_mask & 0x3f) { | |
1903 | /* if backtracing was looking for registers R1-R5 | |
1904 | * they should have been found already. | |
1905 | */ | |
1906 | verbose(env, "BUG regs %x\n", *reg_mask); | |
1907 | WARN_ONCE(1, "verifier backtracking bug"); | |
1908 | return -EFAULT; | |
1909 | } | |
1910 | } else if (opcode == BPF_EXIT) { | |
1911 | return -ENOTSUPP; | |
1912 | } | |
1913 | } else if (class == BPF_LD) { | |
1914 | if (!(*reg_mask & dreg)) | |
1915 | return 0; | |
1916 | *reg_mask &= ~dreg; | |
1917 | /* It's ld_imm64 or ld_abs or ld_ind. | |
1918 | * For ld_imm64 no further tracking of precision | |
1919 | * into parent is necessary | |
1920 | */ | |
1921 | if (mode == BPF_IND || mode == BPF_ABS) | |
1922 | /* to be analyzed */ | |
1923 | return -ENOTSUPP; | |
b5dc0163 AS |
1924 | } |
1925 | return 0; | |
1926 | } | |
1927 | ||
1928 | /* the scalar precision tracking algorithm: | |
1929 | * . at the start all registers have precise=false. | |
1930 | * . scalar ranges are tracked as normal through alu and jmp insns. | |
1931 | * . once precise value of the scalar register is used in: | |
1932 | * . ptr + scalar alu | |
1933 | * . if (scalar cond K|scalar) | |
1934 | * . helper_call(.., scalar, ...) where ARG_CONST is expected | |
1935 | * backtrack through the verifier states and mark all registers and | |
1936 | * stack slots with spilled constants that these scalar regisers | |
1937 | * should be precise. | |
1938 | * . during state pruning two registers (or spilled stack slots) | |
1939 | * are equivalent if both are not precise. | |
1940 | * | |
1941 | * Note the verifier cannot simply walk register parentage chain, | |
1942 | * since many different registers and stack slots could have been | |
1943 | * used to compute single precise scalar. | |
1944 | * | |
1945 | * The approach of starting with precise=true for all registers and then | |
1946 | * backtrack to mark a register as not precise when the verifier detects | |
1947 | * that program doesn't care about specific value (e.g., when helper | |
1948 | * takes register as ARG_ANYTHING parameter) is not safe. | |
1949 | * | |
1950 | * It's ok to walk single parentage chain of the verifier states. | |
1951 | * It's possible that this backtracking will go all the way till 1st insn. | |
1952 | * All other branches will be explored for needing precision later. | |
1953 | * | |
1954 | * The backtracking needs to deal with cases like: | |
1955 | * 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) | |
1956 | * r9 -= r8 | |
1957 | * r5 = r9 | |
1958 | * if r5 > 0x79f goto pc+7 | |
1959 | * R5_w=inv(id=0,umax_value=1951,var_off=(0x0; 0x7ff)) | |
1960 | * r5 += 1 | |
1961 | * ... | |
1962 | * call bpf_perf_event_output#25 | |
1963 | * where .arg5_type = ARG_CONST_SIZE_OR_ZERO | |
1964 | * | |
1965 | * and this case: | |
1966 | * r6 = 1 | |
1967 | * call foo // uses callee's r6 inside to compute r0 | |
1968 | * r0 += r6 | |
1969 | * if r0 == 0 goto | |
1970 | * | |
1971 | * to track above reg_mask/stack_mask needs to be independent for each frame. | |
1972 | * | |
1973 | * Also if parent's curframe > frame where backtracking started, | |
1974 | * the verifier need to mark registers in both frames, otherwise callees | |
1975 | * may incorrectly prune callers. This is similar to | |
1976 | * commit 7640ead93924 ("bpf: verifier: make sure callees don't prune with caller differences") | |
1977 | * | |
1978 | * For now backtracking falls back into conservative marking. | |
1979 | */ | |
1980 | static void mark_all_scalars_precise(struct bpf_verifier_env *env, | |
1981 | struct bpf_verifier_state *st) | |
1982 | { | |
1983 | struct bpf_func_state *func; | |
1984 | struct bpf_reg_state *reg; | |
1985 | int i, j; | |
1986 | ||
1987 | /* big hammer: mark all scalars precise in this path. | |
1988 | * pop_stack may still get !precise scalars. | |
1989 | */ | |
1990 | for (; st; st = st->parent) | |
1991 | for (i = 0; i <= st->curframe; i++) { | |
1992 | func = st->frame[i]; | |
1993 | for (j = 0; j < BPF_REG_FP; j++) { | |
1994 | reg = &func->regs[j]; | |
1995 | if (reg->type != SCALAR_VALUE) | |
1996 | continue; | |
1997 | reg->precise = true; | |
1998 | } | |
1999 | for (j = 0; j < func->allocated_stack / BPF_REG_SIZE; j++) { | |
2000 | if (func->stack[j].slot_type[0] != STACK_SPILL) | |
2001 | continue; | |
2002 | reg = &func->stack[j].spilled_ptr; | |
2003 | if (reg->type != SCALAR_VALUE) | |
2004 | continue; | |
2005 | reg->precise = true; | |
2006 | } | |
2007 | } | |
2008 | } | |
2009 | ||
a3ce685d AS |
2010 | static int __mark_chain_precision(struct bpf_verifier_env *env, int regno, |
2011 | int spi) | |
b5dc0163 AS |
2012 | { |
2013 | struct bpf_verifier_state *st = env->cur_state; | |
2014 | int first_idx = st->first_insn_idx; | |
2015 | int last_idx = env->insn_idx; | |
2016 | struct bpf_func_state *func; | |
2017 | struct bpf_reg_state *reg; | |
a3ce685d AS |
2018 | u32 reg_mask = regno >= 0 ? 1u << regno : 0; |
2019 | u64 stack_mask = spi >= 0 ? 1ull << spi : 0; | |
b5dc0163 | 2020 | bool skip_first = true; |
a3ce685d | 2021 | bool new_marks = false; |
b5dc0163 AS |
2022 | int i, err; |
2023 | ||
2c78ee89 | 2024 | if (!env->bpf_capable) |
b5dc0163 AS |
2025 | return 0; |
2026 | ||
2027 | func = st->frame[st->curframe]; | |
a3ce685d AS |
2028 | if (regno >= 0) { |
2029 | reg = &func->regs[regno]; | |
2030 | if (reg->type != SCALAR_VALUE) { | |
2031 | WARN_ONCE(1, "backtracing misuse"); | |
2032 | return -EFAULT; | |
2033 | } | |
2034 | if (!reg->precise) | |
2035 | new_marks = true; | |
2036 | else | |
2037 | reg_mask = 0; | |
2038 | reg->precise = true; | |
b5dc0163 | 2039 | } |
b5dc0163 | 2040 | |
a3ce685d AS |
2041 | while (spi >= 0) { |
2042 | if (func->stack[spi].slot_type[0] != STACK_SPILL) { | |
2043 | stack_mask = 0; | |
2044 | break; | |
2045 | } | |
2046 | reg = &func->stack[spi].spilled_ptr; | |
2047 | if (reg->type != SCALAR_VALUE) { | |
2048 | stack_mask = 0; | |
2049 | break; | |
2050 | } | |
2051 | if (!reg->precise) | |
2052 | new_marks = true; | |
2053 | else | |
2054 | stack_mask = 0; | |
2055 | reg->precise = true; | |
2056 | break; | |
2057 | } | |
2058 | ||
2059 | if (!new_marks) | |
2060 | return 0; | |
2061 | if (!reg_mask && !stack_mask) | |
2062 | return 0; | |
b5dc0163 AS |
2063 | for (;;) { |
2064 | DECLARE_BITMAP(mask, 64); | |
b5dc0163 AS |
2065 | u32 history = st->jmp_history_cnt; |
2066 | ||
2067 | if (env->log.level & BPF_LOG_LEVEL) | |
2068 | verbose(env, "last_idx %d first_idx %d\n", last_idx, first_idx); | |
2069 | for (i = last_idx;;) { | |
2070 | if (skip_first) { | |
2071 | err = 0; | |
2072 | skip_first = false; | |
2073 | } else { | |
2074 | err = backtrack_insn(env, i, ®_mask, &stack_mask); | |
2075 | } | |
2076 | if (err == -ENOTSUPP) { | |
2077 | mark_all_scalars_precise(env, st); | |
2078 | return 0; | |
2079 | } else if (err) { | |
2080 | return err; | |
2081 | } | |
2082 | if (!reg_mask && !stack_mask) | |
2083 | /* Found assignment(s) into tracked register in this state. | |
2084 | * Since this state is already marked, just return. | |
2085 | * Nothing to be tracked further in the parent state. | |
2086 | */ | |
2087 | return 0; | |
2088 | if (i == first_idx) | |
2089 | break; | |
2090 | i = get_prev_insn_idx(st, i, &history); | |
2091 | if (i >= env->prog->len) { | |
2092 | /* This can happen if backtracking reached insn 0 | |
2093 | * and there are still reg_mask or stack_mask | |
2094 | * to backtrack. | |
2095 | * It means the backtracking missed the spot where | |
2096 | * particular register was initialized with a constant. | |
2097 | */ | |
2098 | verbose(env, "BUG backtracking idx %d\n", i); | |
2099 | WARN_ONCE(1, "verifier backtracking bug"); | |
2100 | return -EFAULT; | |
2101 | } | |
2102 | } | |
2103 | st = st->parent; | |
2104 | if (!st) | |
2105 | break; | |
2106 | ||
a3ce685d | 2107 | new_marks = false; |
b5dc0163 AS |
2108 | func = st->frame[st->curframe]; |
2109 | bitmap_from_u64(mask, reg_mask); | |
2110 | for_each_set_bit(i, mask, 32) { | |
2111 | reg = &func->regs[i]; | |
a3ce685d AS |
2112 | if (reg->type != SCALAR_VALUE) { |
2113 | reg_mask &= ~(1u << i); | |
b5dc0163 | 2114 | continue; |
a3ce685d | 2115 | } |
b5dc0163 AS |
2116 | if (!reg->precise) |
2117 | new_marks = true; | |
2118 | reg->precise = true; | |
2119 | } | |
2120 | ||
2121 | bitmap_from_u64(mask, stack_mask); | |
2122 | for_each_set_bit(i, mask, 64) { | |
2123 | if (i >= func->allocated_stack / BPF_REG_SIZE) { | |
2339cd6c AS |
2124 | /* the sequence of instructions: |
2125 | * 2: (bf) r3 = r10 | |
2126 | * 3: (7b) *(u64 *)(r3 -8) = r0 | |
2127 | * 4: (79) r4 = *(u64 *)(r10 -8) | |
2128 | * doesn't contain jmps. It's backtracked | |
2129 | * as a single block. | |
2130 | * During backtracking insn 3 is not recognized as | |
2131 | * stack access, so at the end of backtracking | |
2132 | * stack slot fp-8 is still marked in stack_mask. | |
2133 | * However the parent state may not have accessed | |
2134 | * fp-8 and it's "unallocated" stack space. | |
2135 | * In such case fallback to conservative. | |
b5dc0163 | 2136 | */ |
2339cd6c AS |
2137 | mark_all_scalars_precise(env, st); |
2138 | return 0; | |
b5dc0163 AS |
2139 | } |
2140 | ||
a3ce685d AS |
2141 | if (func->stack[i].slot_type[0] != STACK_SPILL) { |
2142 | stack_mask &= ~(1ull << i); | |
b5dc0163 | 2143 | continue; |
a3ce685d | 2144 | } |
b5dc0163 | 2145 | reg = &func->stack[i].spilled_ptr; |
a3ce685d AS |
2146 | if (reg->type != SCALAR_VALUE) { |
2147 | stack_mask &= ~(1ull << i); | |
b5dc0163 | 2148 | continue; |
a3ce685d | 2149 | } |
b5dc0163 AS |
2150 | if (!reg->precise) |
2151 | new_marks = true; | |
2152 | reg->precise = true; | |
2153 | } | |
2154 | if (env->log.level & BPF_LOG_LEVEL) { | |
2155 | print_verifier_state(env, func); | |
2156 | verbose(env, "parent %s regs=%x stack=%llx marks\n", | |
2157 | new_marks ? "didn't have" : "already had", | |
2158 | reg_mask, stack_mask); | |
2159 | } | |
2160 | ||
a3ce685d AS |
2161 | if (!reg_mask && !stack_mask) |
2162 | break; | |
b5dc0163 AS |
2163 | if (!new_marks) |
2164 | break; | |
2165 | ||
2166 | last_idx = st->last_insn_idx; | |
2167 | first_idx = st->first_insn_idx; | |
2168 | } | |
2169 | return 0; | |
2170 | } | |
2171 | ||
a3ce685d AS |
2172 | static int mark_chain_precision(struct bpf_verifier_env *env, int regno) |
2173 | { | |
2174 | return __mark_chain_precision(env, regno, -1); | |
2175 | } | |
2176 | ||
2177 | static int mark_chain_precision_stack(struct bpf_verifier_env *env, int spi) | |
2178 | { | |
2179 | return __mark_chain_precision(env, -1, spi); | |
2180 | } | |
b5dc0163 | 2181 | |
1be7f75d AS |
2182 | static bool is_spillable_regtype(enum bpf_reg_type type) |
2183 | { | |
2184 | switch (type) { | |
2185 | case PTR_TO_MAP_VALUE: | |
2186 | case PTR_TO_MAP_VALUE_OR_NULL: | |
2187 | case PTR_TO_STACK: | |
2188 | case PTR_TO_CTX: | |
969bf05e | 2189 | case PTR_TO_PACKET: |
de8f3a83 | 2190 | case PTR_TO_PACKET_META: |
969bf05e | 2191 | case PTR_TO_PACKET_END: |
d58e468b | 2192 | case PTR_TO_FLOW_KEYS: |
1be7f75d | 2193 | case CONST_PTR_TO_MAP: |
c64b7983 JS |
2194 | case PTR_TO_SOCKET: |
2195 | case PTR_TO_SOCKET_OR_NULL: | |
46f8bc92 MKL |
2196 | case PTR_TO_SOCK_COMMON: |
2197 | case PTR_TO_SOCK_COMMON_OR_NULL: | |
655a51e5 MKL |
2198 | case PTR_TO_TCP_SOCK: |
2199 | case PTR_TO_TCP_SOCK_OR_NULL: | |
fada7fdc | 2200 | case PTR_TO_XDP_SOCK: |
65726b5b | 2201 | case PTR_TO_BTF_ID: |
b121b341 | 2202 | case PTR_TO_BTF_ID_OR_NULL: |
afbf21dc YS |
2203 | case PTR_TO_RDONLY_BUF: |
2204 | case PTR_TO_RDONLY_BUF_OR_NULL: | |
2205 | case PTR_TO_RDWR_BUF: | |
2206 | case PTR_TO_RDWR_BUF_OR_NULL: | |
1be7f75d AS |
2207 | return true; |
2208 | default: | |
2209 | return false; | |
2210 | } | |
2211 | } | |
2212 | ||
cc2b14d5 AS |
2213 | /* Does this register contain a constant zero? */ |
2214 | static bool register_is_null(struct bpf_reg_state *reg) | |
2215 | { | |
2216 | return reg->type == SCALAR_VALUE && tnum_equals_const(reg->var_off, 0); | |
2217 | } | |
2218 | ||
f7cf25b2 AS |
2219 | static bool register_is_const(struct bpf_reg_state *reg) |
2220 | { | |
2221 | return reg->type == SCALAR_VALUE && tnum_is_const(reg->var_off); | |
2222 | } | |
2223 | ||
6e7e63cb JH |
2224 | static bool __is_pointer_value(bool allow_ptr_leaks, |
2225 | const struct bpf_reg_state *reg) | |
2226 | { | |
2227 | if (allow_ptr_leaks) | |
2228 | return false; | |
2229 | ||
2230 | return reg->type != SCALAR_VALUE; | |
2231 | } | |
2232 | ||
f7cf25b2 AS |
2233 | static void save_register_state(struct bpf_func_state *state, |
2234 | int spi, struct bpf_reg_state *reg) | |
2235 | { | |
2236 | int i; | |
2237 | ||
2238 | state->stack[spi].spilled_ptr = *reg; | |
2239 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; | |
2240 | ||
2241 | for (i = 0; i < BPF_REG_SIZE; i++) | |
2242 | state->stack[spi].slot_type[i] = STACK_SPILL; | |
2243 | } | |
2244 | ||
17a52670 AS |
2245 | /* check_stack_read/write functions track spill/fill of registers, |
2246 | * stack boundary and alignment are checked in check_mem_access() | |
2247 | */ | |
61bd5218 | 2248 | static int check_stack_write(struct bpf_verifier_env *env, |
f4d7e40a | 2249 | struct bpf_func_state *state, /* func where register points to */ |
af86ca4e | 2250 | int off, int size, int value_regno, int insn_idx) |
17a52670 | 2251 | { |
f4d7e40a | 2252 | struct bpf_func_state *cur; /* state of the current function */ |
638f5b90 | 2253 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err; |
b5dc0163 | 2254 | u32 dst_reg = env->prog->insnsi[insn_idx].dst_reg; |
f7cf25b2 | 2255 | struct bpf_reg_state *reg = NULL; |
638f5b90 | 2256 | |
f4d7e40a | 2257 | err = realloc_func_state(state, round_up(slot + 1, BPF_REG_SIZE), |
fd978bf7 | 2258 | state->acquired_refs, true); |
638f5b90 AS |
2259 | if (err) |
2260 | return err; | |
9c399760 AS |
2261 | /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0, |
2262 | * so it's aligned access and [off, off + size) are within stack limits | |
2263 | */ | |
638f5b90 AS |
2264 | if (!env->allow_ptr_leaks && |
2265 | state->stack[spi].slot_type[0] == STACK_SPILL && | |
2266 | size != BPF_REG_SIZE) { | |
2267 | verbose(env, "attempt to corrupt spilled pointer on stack\n"); | |
2268 | return -EACCES; | |
2269 | } | |
17a52670 | 2270 | |
f4d7e40a | 2271 | cur = env->cur_state->frame[env->cur_state->curframe]; |
f7cf25b2 AS |
2272 | if (value_regno >= 0) |
2273 | reg = &cur->regs[value_regno]; | |
17a52670 | 2274 | |
f7cf25b2 | 2275 | if (reg && size == BPF_REG_SIZE && register_is_const(reg) && |
2c78ee89 | 2276 | !register_is_null(reg) && env->bpf_capable) { |
b5dc0163 AS |
2277 | if (dst_reg != BPF_REG_FP) { |
2278 | /* The backtracking logic can only recognize explicit | |
2279 | * stack slot address like [fp - 8]. Other spill of | |
2280 | * scalar via different register has to be conervative. | |
2281 | * Backtrack from here and mark all registers as precise | |
2282 | * that contributed into 'reg' being a constant. | |
2283 | */ | |
2284 | err = mark_chain_precision(env, value_regno); | |
2285 | if (err) | |
2286 | return err; | |
2287 | } | |
f7cf25b2 AS |
2288 | save_register_state(state, spi, reg); |
2289 | } else if (reg && is_spillable_regtype(reg->type)) { | |
17a52670 | 2290 | /* register containing pointer is being spilled into stack */ |
9c399760 | 2291 | if (size != BPF_REG_SIZE) { |
f7cf25b2 | 2292 | verbose_linfo(env, insn_idx, "; "); |
61bd5218 | 2293 | verbose(env, "invalid size of register spill\n"); |
17a52670 AS |
2294 | return -EACCES; |
2295 | } | |
2296 | ||
f7cf25b2 | 2297 | if (state != cur && reg->type == PTR_TO_STACK) { |
f4d7e40a AS |
2298 | verbose(env, "cannot spill pointers to stack into stack frame of the caller\n"); |
2299 | return -EINVAL; | |
2300 | } | |
2301 | ||
2c78ee89 | 2302 | if (!env->bypass_spec_v4) { |
f7cf25b2 | 2303 | bool sanitize = false; |
17a52670 | 2304 | |
f7cf25b2 AS |
2305 | if (state->stack[spi].slot_type[0] == STACK_SPILL && |
2306 | register_is_const(&state->stack[spi].spilled_ptr)) | |
2307 | sanitize = true; | |
2308 | for (i = 0; i < BPF_REG_SIZE; i++) | |
2309 | if (state->stack[spi].slot_type[i] == STACK_MISC) { | |
2310 | sanitize = true; | |
2311 | break; | |
2312 | } | |
2313 | if (sanitize) { | |
af86ca4e AS |
2314 | int *poff = &env->insn_aux_data[insn_idx].sanitize_stack_off; |
2315 | int soff = (-spi - 1) * BPF_REG_SIZE; | |
2316 | ||
2317 | /* detected reuse of integer stack slot with a pointer | |
2318 | * which means either llvm is reusing stack slot or | |
2319 | * an attacker is trying to exploit CVE-2018-3639 | |
2320 | * (speculative store bypass) | |
2321 | * Have to sanitize that slot with preemptive | |
2322 | * store of zero. | |
2323 | */ | |
2324 | if (*poff && *poff != soff) { | |
2325 | /* disallow programs where single insn stores | |
2326 | * into two different stack slots, since verifier | |
2327 | * cannot sanitize them | |
2328 | */ | |
2329 | verbose(env, | |
2330 | "insn %d cannot access two stack slots fp%d and fp%d", | |
2331 | insn_idx, *poff, soff); | |
2332 | return -EINVAL; | |
2333 | } | |
2334 | *poff = soff; | |
2335 | } | |
af86ca4e | 2336 | } |
f7cf25b2 | 2337 | save_register_state(state, spi, reg); |
9c399760 | 2338 | } else { |
cc2b14d5 AS |
2339 | u8 type = STACK_MISC; |
2340 | ||
679c782d EC |
2341 | /* regular write of data into stack destroys any spilled ptr */ |
2342 | state->stack[spi].spilled_ptr.type = NOT_INIT; | |
0bae2d4d JW |
2343 | /* Mark slots as STACK_MISC if they belonged to spilled ptr. */ |
2344 | if (state->stack[spi].slot_type[0] == STACK_SPILL) | |
2345 | for (i = 0; i < BPF_REG_SIZE; i++) | |
2346 | state->stack[spi].slot_type[i] = STACK_MISC; | |
9c399760 | 2347 | |
cc2b14d5 AS |
2348 | /* only mark the slot as written if all 8 bytes were written |
2349 | * otherwise read propagation may incorrectly stop too soon | |
2350 | * when stack slots are partially written. | |
2351 | * This heuristic means that read propagation will be | |
2352 | * conservative, since it will add reg_live_read marks | |
2353 | * to stack slots all the way to first state when programs | |
2354 | * writes+reads less than 8 bytes | |
2355 | */ | |
2356 | if (size == BPF_REG_SIZE) | |
2357 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; | |
2358 | ||
2359 | /* when we zero initialize stack slots mark them as such */ | |
b5dc0163 AS |
2360 | if (reg && register_is_null(reg)) { |
2361 | /* backtracking doesn't work for STACK_ZERO yet. */ | |
2362 | err = mark_chain_precision(env, value_regno); | |
2363 | if (err) | |
2364 | return err; | |
cc2b14d5 | 2365 | type = STACK_ZERO; |
b5dc0163 | 2366 | } |
cc2b14d5 | 2367 | |
0bae2d4d | 2368 | /* Mark slots affected by this stack write. */ |
9c399760 | 2369 | for (i = 0; i < size; i++) |
638f5b90 | 2370 | state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] = |
cc2b14d5 | 2371 | type; |
17a52670 AS |
2372 | } |
2373 | return 0; | |
2374 | } | |
2375 | ||
61bd5218 | 2376 | static int check_stack_read(struct bpf_verifier_env *env, |
f4d7e40a AS |
2377 | struct bpf_func_state *reg_state /* func where register points to */, |
2378 | int off, int size, int value_regno) | |
17a52670 | 2379 | { |
f4d7e40a AS |
2380 | struct bpf_verifier_state *vstate = env->cur_state; |
2381 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
638f5b90 | 2382 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE; |
f7cf25b2 | 2383 | struct bpf_reg_state *reg; |
638f5b90 | 2384 | u8 *stype; |
17a52670 | 2385 | |
f4d7e40a | 2386 | if (reg_state->allocated_stack <= slot) { |
638f5b90 AS |
2387 | verbose(env, "invalid read from stack off %d+0 size %d\n", |
2388 | off, size); | |
2389 | return -EACCES; | |
2390 | } | |
f4d7e40a | 2391 | stype = reg_state->stack[spi].slot_type; |
f7cf25b2 | 2392 | reg = ®_state->stack[spi].spilled_ptr; |
17a52670 | 2393 | |
638f5b90 | 2394 | if (stype[0] == STACK_SPILL) { |
9c399760 | 2395 | if (size != BPF_REG_SIZE) { |
f7cf25b2 AS |
2396 | if (reg->type != SCALAR_VALUE) { |
2397 | verbose_linfo(env, env->insn_idx, "; "); | |
2398 | verbose(env, "invalid size of register fill\n"); | |
2399 | return -EACCES; | |
2400 | } | |
2401 | if (value_regno >= 0) { | |
2402 | mark_reg_unknown(env, state->regs, value_regno); | |
2403 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; | |
2404 | } | |
2405 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); | |
2406 | return 0; | |
17a52670 | 2407 | } |
9c399760 | 2408 | for (i = 1; i < BPF_REG_SIZE; i++) { |
638f5b90 | 2409 | if (stype[(slot - i) % BPF_REG_SIZE] != STACK_SPILL) { |
61bd5218 | 2410 | verbose(env, "corrupted spill memory\n"); |
17a52670 AS |
2411 | return -EACCES; |
2412 | } | |
2413 | } | |
2414 | ||
dc503a8a | 2415 | if (value_regno >= 0) { |
17a52670 | 2416 | /* restore register state from stack */ |
f7cf25b2 | 2417 | state->regs[value_regno] = *reg; |
2f18f62e AS |
2418 | /* mark reg as written since spilled pointer state likely |
2419 | * has its liveness marks cleared by is_state_visited() | |
2420 | * which resets stack/reg liveness for state transitions | |
2421 | */ | |
2422 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; | |
6e7e63cb JH |
2423 | } else if (__is_pointer_value(env->allow_ptr_leaks, reg)) { |
2424 | /* If value_regno==-1, the caller is asking us whether | |
2425 | * it is acceptable to use this value as a SCALAR_VALUE | |
2426 | * (e.g. for XADD). | |
2427 | * We must not allow unprivileged callers to do that | |
2428 | * with spilled pointers. | |
2429 | */ | |
2430 | verbose(env, "leaking pointer from stack off %d\n", | |
2431 | off); | |
2432 | return -EACCES; | |
dc503a8a | 2433 | } |
f7cf25b2 | 2434 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
17a52670 | 2435 | } else { |
cc2b14d5 AS |
2436 | int zeros = 0; |
2437 | ||
17a52670 | 2438 | for (i = 0; i < size; i++) { |
cc2b14d5 AS |
2439 | if (stype[(slot - i) % BPF_REG_SIZE] == STACK_MISC) |
2440 | continue; | |
2441 | if (stype[(slot - i) % BPF_REG_SIZE] == STACK_ZERO) { | |
2442 | zeros++; | |
2443 | continue; | |
17a52670 | 2444 | } |
cc2b14d5 AS |
2445 | verbose(env, "invalid read from stack off %d+%d size %d\n", |
2446 | off, i, size); | |
2447 | return -EACCES; | |
2448 | } | |
f7cf25b2 | 2449 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
cc2b14d5 AS |
2450 | if (value_regno >= 0) { |
2451 | if (zeros == size) { | |
2452 | /* any size read into register is zero extended, | |
2453 | * so the whole register == const_zero | |
2454 | */ | |
2455 | __mark_reg_const_zero(&state->regs[value_regno]); | |
b5dc0163 AS |
2456 | /* backtracking doesn't support STACK_ZERO yet, |
2457 | * so mark it precise here, so that later | |
2458 | * backtracking can stop here. | |
2459 | * Backtracking may not need this if this register | |
2460 | * doesn't participate in pointer adjustment. | |
2461 | * Forward propagation of precise flag is not | |
2462 | * necessary either. This mark is only to stop | |
2463 | * backtracking. Any register that contributed | |
2464 | * to const 0 was marked precise before spill. | |
2465 | */ | |
2466 | state->regs[value_regno].precise = true; | |
cc2b14d5 AS |
2467 | } else { |
2468 | /* have read misc data from the stack */ | |
2469 | mark_reg_unknown(env, state->regs, value_regno); | |
2470 | } | |
2471 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; | |
17a52670 | 2472 | } |
17a52670 | 2473 | } |
f7cf25b2 | 2474 | return 0; |
17a52670 AS |
2475 | } |
2476 | ||
e4298d25 DB |
2477 | static int check_stack_access(struct bpf_verifier_env *env, |
2478 | const struct bpf_reg_state *reg, | |
2479 | int off, int size) | |
2480 | { | |
2481 | /* Stack accesses must be at a fixed offset, so that we | |
2482 | * can determine what type of data were returned. See | |
2483 | * check_stack_read(). | |
2484 | */ | |
2485 | if (!tnum_is_const(reg->var_off)) { | |
2486 | char tn_buf[48]; | |
2487 | ||
2488 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
1fbd20f8 | 2489 | verbose(env, "variable stack access var_off=%s off=%d size=%d\n", |
e4298d25 DB |
2490 | tn_buf, off, size); |
2491 | return -EACCES; | |
2492 | } | |
2493 | ||
2494 | if (off >= 0 || off < -MAX_BPF_STACK) { | |
2495 | verbose(env, "invalid stack off=%d size=%d\n", off, size); | |
2496 | return -EACCES; | |
2497 | } | |
2498 | ||
2499 | return 0; | |
2500 | } | |
2501 | ||
591fe988 DB |
2502 | static int check_map_access_type(struct bpf_verifier_env *env, u32 regno, |
2503 | int off, int size, enum bpf_access_type type) | |
2504 | { | |
2505 | struct bpf_reg_state *regs = cur_regs(env); | |
2506 | struct bpf_map *map = regs[regno].map_ptr; | |
2507 | u32 cap = bpf_map_flags_to_cap(map); | |
2508 | ||
2509 | if (type == BPF_WRITE && !(cap & BPF_MAP_CAN_WRITE)) { | |
2510 | verbose(env, "write into map forbidden, value_size=%d off=%d size=%d\n", | |
2511 | map->value_size, off, size); | |
2512 | return -EACCES; | |
2513 | } | |
2514 | ||
2515 | if (type == BPF_READ && !(cap & BPF_MAP_CAN_READ)) { | |
2516 | verbose(env, "read from map forbidden, value_size=%d off=%d size=%d\n", | |
2517 | map->value_size, off, size); | |
2518 | return -EACCES; | |
2519 | } | |
2520 | ||
2521 | return 0; | |
2522 | } | |
2523 | ||
457f4436 AN |
2524 | /* check read/write into memory region (e.g., map value, ringbuf sample, etc) */ |
2525 | static int __check_mem_access(struct bpf_verifier_env *env, int regno, | |
2526 | int off, int size, u32 mem_size, | |
2527 | bool zero_size_allowed) | |
17a52670 | 2528 | { |
457f4436 AN |
2529 | bool size_ok = size > 0 || (size == 0 && zero_size_allowed); |
2530 | struct bpf_reg_state *reg; | |
2531 | ||
2532 | if (off >= 0 && size_ok && (u64)off + size <= mem_size) | |
2533 | return 0; | |
17a52670 | 2534 | |
457f4436 AN |
2535 | reg = &cur_regs(env)[regno]; |
2536 | switch (reg->type) { | |
2537 | case PTR_TO_MAP_VALUE: | |
61bd5218 | 2538 | verbose(env, "invalid access to map value, value_size=%d off=%d size=%d\n", |
457f4436 AN |
2539 | mem_size, off, size); |
2540 | break; | |
2541 | case PTR_TO_PACKET: | |
2542 | case PTR_TO_PACKET_META: | |
2543 | case PTR_TO_PACKET_END: | |
2544 | verbose(env, "invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n", | |
2545 | off, size, regno, reg->id, off, mem_size); | |
2546 | break; | |
2547 | case PTR_TO_MEM: | |
2548 | default: | |
2549 | verbose(env, "invalid access to memory, mem_size=%u off=%d size=%d\n", | |
2550 | mem_size, off, size); | |
17a52670 | 2551 | } |
457f4436 AN |
2552 | |
2553 | return -EACCES; | |
17a52670 AS |
2554 | } |
2555 | ||
457f4436 AN |
2556 | /* check read/write into a memory region with possible variable offset */ |
2557 | static int check_mem_region_access(struct bpf_verifier_env *env, u32 regno, | |
2558 | int off, int size, u32 mem_size, | |
2559 | bool zero_size_allowed) | |
dbcfe5f7 | 2560 | { |
f4d7e40a AS |
2561 | struct bpf_verifier_state *vstate = env->cur_state; |
2562 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
dbcfe5f7 GB |
2563 | struct bpf_reg_state *reg = &state->regs[regno]; |
2564 | int err; | |
2565 | ||
457f4436 | 2566 | /* We may have adjusted the register pointing to memory region, so we |
f1174f77 EC |
2567 | * need to try adding each of min_value and max_value to off |
2568 | * to make sure our theoretical access will be safe. | |
dbcfe5f7 | 2569 | */ |
06ee7115 | 2570 | if (env->log.level & BPF_LOG_LEVEL) |
61bd5218 | 2571 | print_verifier_state(env, state); |
b7137c4e | 2572 | |
dbcfe5f7 GB |
2573 | /* The minimum value is only important with signed |
2574 | * comparisons where we can't assume the floor of a | |
2575 | * value is 0. If we are using signed variables for our | |
2576 | * index'es we need to make sure that whatever we use | |
2577 | * will have a set floor within our range. | |
2578 | */ | |
b7137c4e DB |
2579 | if (reg->smin_value < 0 && |
2580 | (reg->smin_value == S64_MIN || | |
2581 | (off + reg->smin_value != (s64)(s32)(off + reg->smin_value)) || | |
2582 | reg->smin_value + off < 0)) { | |
61bd5218 | 2583 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
dbcfe5f7 GB |
2584 | regno); |
2585 | return -EACCES; | |
2586 | } | |
457f4436 AN |
2587 | err = __check_mem_access(env, regno, reg->smin_value + off, size, |
2588 | mem_size, zero_size_allowed); | |
dbcfe5f7 | 2589 | if (err) { |
457f4436 | 2590 | verbose(env, "R%d min value is outside of the allowed memory range\n", |
61bd5218 | 2591 | regno); |
dbcfe5f7 GB |
2592 | return err; |
2593 | } | |
2594 | ||
b03c9f9f EC |
2595 | /* If we haven't set a max value then we need to bail since we can't be |
2596 | * sure we won't do bad things. | |
2597 | * If reg->umax_value + off could overflow, treat that as unbounded too. | |
dbcfe5f7 | 2598 | */ |
b03c9f9f | 2599 | if (reg->umax_value >= BPF_MAX_VAR_OFF) { |
457f4436 | 2600 | verbose(env, "R%d unbounded memory access, make sure to bounds check any such access\n", |
dbcfe5f7 GB |
2601 | regno); |
2602 | return -EACCES; | |
2603 | } | |
457f4436 AN |
2604 | err = __check_mem_access(env, regno, reg->umax_value + off, size, |
2605 | mem_size, zero_size_allowed); | |
2606 | if (err) { | |
2607 | verbose(env, "R%d max value is outside of the allowed memory range\n", | |
61bd5218 | 2608 | regno); |
457f4436 AN |
2609 | return err; |
2610 | } | |
2611 | ||
2612 | return 0; | |
2613 | } | |
d83525ca | 2614 | |
457f4436 AN |
2615 | /* check read/write into a map element with possible variable offset */ |
2616 | static int check_map_access(struct bpf_verifier_env *env, u32 regno, | |
2617 | int off, int size, bool zero_size_allowed) | |
2618 | { | |
2619 | struct bpf_verifier_state *vstate = env->cur_state; | |
2620 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
2621 | struct bpf_reg_state *reg = &state->regs[regno]; | |
2622 | struct bpf_map *map = reg->map_ptr; | |
2623 | int err; | |
2624 | ||
2625 | err = check_mem_region_access(env, regno, off, size, map->value_size, | |
2626 | zero_size_allowed); | |
2627 | if (err) | |
2628 | return err; | |
2629 | ||
2630 | if (map_value_has_spin_lock(map)) { | |
2631 | u32 lock = map->spin_lock_off; | |
d83525ca AS |
2632 | |
2633 | /* if any part of struct bpf_spin_lock can be touched by | |
2634 | * load/store reject this program. | |
2635 | * To check that [x1, x2) overlaps with [y1, y2) | |
2636 | * it is sufficient to check x1 < y2 && y1 < x2. | |
2637 | */ | |
2638 | if (reg->smin_value + off < lock + sizeof(struct bpf_spin_lock) && | |
2639 | lock < reg->umax_value + off + size) { | |
2640 | verbose(env, "bpf_spin_lock cannot be accessed directly by load/store\n"); | |
2641 | return -EACCES; | |
2642 | } | |
2643 | } | |
f1174f77 | 2644 | return err; |
dbcfe5f7 GB |
2645 | } |
2646 | ||
969bf05e AS |
2647 | #define MAX_PACKET_OFF 0xffff |
2648 | ||
7e40781c UP |
2649 | static enum bpf_prog_type resolve_prog_type(struct bpf_prog *prog) |
2650 | { | |
3aac1ead | 2651 | return prog->aux->dst_prog ? prog->aux->dst_prog->type : prog->type; |
7e40781c UP |
2652 | } |
2653 | ||
58e2af8b | 2654 | static bool may_access_direct_pkt_data(struct bpf_verifier_env *env, |
3a0af8fd TG |
2655 | const struct bpf_call_arg_meta *meta, |
2656 | enum bpf_access_type t) | |
4acf6c0b | 2657 | { |
7e40781c UP |
2658 | enum bpf_prog_type prog_type = resolve_prog_type(env->prog); |
2659 | ||
2660 | switch (prog_type) { | |
5d66fa7d | 2661 | /* Program types only with direct read access go here! */ |
3a0af8fd TG |
2662 | case BPF_PROG_TYPE_LWT_IN: |
2663 | case BPF_PROG_TYPE_LWT_OUT: | |
004d4b27 | 2664 | case BPF_PROG_TYPE_LWT_SEG6LOCAL: |
2dbb9b9e | 2665 | case BPF_PROG_TYPE_SK_REUSEPORT: |
5d66fa7d | 2666 | case BPF_PROG_TYPE_FLOW_DISSECTOR: |
d5563d36 | 2667 | case BPF_PROG_TYPE_CGROUP_SKB: |
3a0af8fd TG |
2668 | if (t == BPF_WRITE) |
2669 | return false; | |
7e57fbb2 | 2670 | /* fallthrough */ |
5d66fa7d DB |
2671 | |
2672 | /* Program types with direct read + write access go here! */ | |
36bbef52 DB |
2673 | case BPF_PROG_TYPE_SCHED_CLS: |
2674 | case BPF_PROG_TYPE_SCHED_ACT: | |
4acf6c0b | 2675 | case BPF_PROG_TYPE_XDP: |
3a0af8fd | 2676 | case BPF_PROG_TYPE_LWT_XMIT: |
8a31db56 | 2677 | case BPF_PROG_TYPE_SK_SKB: |
4f738adb | 2678 | case BPF_PROG_TYPE_SK_MSG: |
36bbef52 DB |
2679 | if (meta) |
2680 | return meta->pkt_access; | |
2681 | ||
2682 | env->seen_direct_write = true; | |
4acf6c0b | 2683 | return true; |
0d01da6a SF |
2684 | |
2685 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: | |
2686 | if (t == BPF_WRITE) | |
2687 | env->seen_direct_write = true; | |
2688 | ||
2689 | return true; | |
2690 | ||
4acf6c0b BB |
2691 | default: |
2692 | return false; | |
2693 | } | |
2694 | } | |
2695 | ||
f1174f77 | 2696 | static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off, |
9fd29c08 | 2697 | int size, bool zero_size_allowed) |
f1174f77 | 2698 | { |
638f5b90 | 2699 | struct bpf_reg_state *regs = cur_regs(env); |
f1174f77 EC |
2700 | struct bpf_reg_state *reg = ®s[regno]; |
2701 | int err; | |
2702 | ||
2703 | /* We may have added a variable offset to the packet pointer; but any | |
2704 | * reg->range we have comes after that. We are only checking the fixed | |
2705 | * offset. | |
2706 | */ | |
2707 | ||
2708 | /* We don't allow negative numbers, because we aren't tracking enough | |
2709 | * detail to prove they're safe. | |
2710 | */ | |
b03c9f9f | 2711 | if (reg->smin_value < 0) { |
61bd5218 | 2712 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
f1174f77 EC |
2713 | regno); |
2714 | return -EACCES; | |
2715 | } | |
457f4436 AN |
2716 | err = __check_mem_access(env, regno, off, size, reg->range, |
2717 | zero_size_allowed); | |
f1174f77 | 2718 | if (err) { |
61bd5218 | 2719 | verbose(env, "R%d offset is outside of the packet\n", regno); |
f1174f77 EC |
2720 | return err; |
2721 | } | |
e647815a | 2722 | |
457f4436 | 2723 | /* __check_mem_access has made sure "off + size - 1" is within u16. |
e647815a JW |
2724 | * reg->umax_value can't be bigger than MAX_PACKET_OFF which is 0xffff, |
2725 | * otherwise find_good_pkt_pointers would have refused to set range info | |
457f4436 | 2726 | * that __check_mem_access would have rejected this pkt access. |
e647815a JW |
2727 | * Therefore, "off + reg->umax_value + size - 1" won't overflow u32. |
2728 | */ | |
2729 | env->prog->aux->max_pkt_offset = | |
2730 | max_t(u32, env->prog->aux->max_pkt_offset, | |
2731 | off + reg->umax_value + size - 1); | |
2732 | ||
f1174f77 EC |
2733 | return err; |
2734 | } | |
2735 | ||
2736 | /* check access to 'struct bpf_context' fields. Supports fixed offsets only */ | |
31fd8581 | 2737 | static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off, int size, |
9e15db66 AS |
2738 | enum bpf_access_type t, enum bpf_reg_type *reg_type, |
2739 | u32 *btf_id) | |
17a52670 | 2740 | { |
f96da094 DB |
2741 | struct bpf_insn_access_aux info = { |
2742 | .reg_type = *reg_type, | |
9e15db66 | 2743 | .log = &env->log, |
f96da094 | 2744 | }; |
31fd8581 | 2745 | |
4f9218aa | 2746 | if (env->ops->is_valid_access && |
5e43f899 | 2747 | env->ops->is_valid_access(off, size, t, env->prog, &info)) { |
f96da094 DB |
2748 | /* A non zero info.ctx_field_size indicates that this field is a |
2749 | * candidate for later verifier transformation to load the whole | |
2750 | * field and then apply a mask when accessed with a narrower | |
2751 | * access than actual ctx access size. A zero info.ctx_field_size | |
2752 | * will only allow for whole field access and rejects any other | |
2753 | * type of narrower access. | |
31fd8581 | 2754 | */ |
23994631 | 2755 | *reg_type = info.reg_type; |
31fd8581 | 2756 | |
b121b341 | 2757 | if (*reg_type == PTR_TO_BTF_ID || *reg_type == PTR_TO_BTF_ID_OR_NULL) |
9e15db66 AS |
2758 | *btf_id = info.btf_id; |
2759 | else | |
2760 | env->insn_aux_data[insn_idx].ctx_field_size = info.ctx_field_size; | |
32bbe007 AS |
2761 | /* remember the offset of last byte accessed in ctx */ |
2762 | if (env->prog->aux->max_ctx_offset < off + size) | |
2763 | env->prog->aux->max_ctx_offset = off + size; | |
17a52670 | 2764 | return 0; |
32bbe007 | 2765 | } |
17a52670 | 2766 | |
61bd5218 | 2767 | verbose(env, "invalid bpf_context access off=%d size=%d\n", off, size); |
17a52670 AS |
2768 | return -EACCES; |
2769 | } | |
2770 | ||
d58e468b PP |
2771 | static int check_flow_keys_access(struct bpf_verifier_env *env, int off, |
2772 | int size) | |
2773 | { | |
2774 | if (size < 0 || off < 0 || | |
2775 | (u64)off + size > sizeof(struct bpf_flow_keys)) { | |
2776 | verbose(env, "invalid access to flow keys off=%d size=%d\n", | |
2777 | off, size); | |
2778 | return -EACCES; | |
2779 | } | |
2780 | return 0; | |
2781 | } | |
2782 | ||
5f456649 MKL |
2783 | static int check_sock_access(struct bpf_verifier_env *env, int insn_idx, |
2784 | u32 regno, int off, int size, | |
2785 | enum bpf_access_type t) | |
c64b7983 JS |
2786 | { |
2787 | struct bpf_reg_state *regs = cur_regs(env); | |
2788 | struct bpf_reg_state *reg = ®s[regno]; | |
5f456649 | 2789 | struct bpf_insn_access_aux info = {}; |
46f8bc92 | 2790 | bool valid; |
c64b7983 JS |
2791 | |
2792 | if (reg->smin_value < 0) { | |
2793 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", | |
2794 | regno); | |
2795 | return -EACCES; | |
2796 | } | |
2797 | ||
46f8bc92 MKL |
2798 | switch (reg->type) { |
2799 | case PTR_TO_SOCK_COMMON: | |
2800 | valid = bpf_sock_common_is_valid_access(off, size, t, &info); | |
2801 | break; | |
2802 | case PTR_TO_SOCKET: | |
2803 | valid = bpf_sock_is_valid_access(off, size, t, &info); | |
2804 | break; | |
655a51e5 MKL |
2805 | case PTR_TO_TCP_SOCK: |
2806 | valid = bpf_tcp_sock_is_valid_access(off, size, t, &info); | |
2807 | break; | |
fada7fdc JL |
2808 | case PTR_TO_XDP_SOCK: |
2809 | valid = bpf_xdp_sock_is_valid_access(off, size, t, &info); | |
2810 | break; | |
46f8bc92 MKL |
2811 | default: |
2812 | valid = false; | |
c64b7983 JS |
2813 | } |
2814 | ||
5f456649 | 2815 | |
46f8bc92 MKL |
2816 | if (valid) { |
2817 | env->insn_aux_data[insn_idx].ctx_field_size = | |
2818 | info.ctx_field_size; | |
2819 | return 0; | |
2820 | } | |
2821 | ||
2822 | verbose(env, "R%d invalid %s access off=%d size=%d\n", | |
2823 | regno, reg_type_str[reg->type], off, size); | |
2824 | ||
2825 | return -EACCES; | |
c64b7983 JS |
2826 | } |
2827 | ||
2a159c6f DB |
2828 | static struct bpf_reg_state *reg_state(struct bpf_verifier_env *env, int regno) |
2829 | { | |
2830 | return cur_regs(env) + regno; | |
2831 | } | |
2832 | ||
4cabc5b1 DB |
2833 | static bool is_pointer_value(struct bpf_verifier_env *env, int regno) |
2834 | { | |
2a159c6f | 2835 | return __is_pointer_value(env->allow_ptr_leaks, reg_state(env, regno)); |
4cabc5b1 DB |
2836 | } |
2837 | ||
f37a8cb8 DB |
2838 | static bool is_ctx_reg(struct bpf_verifier_env *env, int regno) |
2839 | { | |
2a159c6f | 2840 | const struct bpf_reg_state *reg = reg_state(env, regno); |
f37a8cb8 | 2841 | |
46f8bc92 MKL |
2842 | return reg->type == PTR_TO_CTX; |
2843 | } | |
2844 | ||
2845 | static bool is_sk_reg(struct bpf_verifier_env *env, int regno) | |
2846 | { | |
2847 | const struct bpf_reg_state *reg = reg_state(env, regno); | |
2848 | ||
2849 | return type_is_sk_pointer(reg->type); | |
f37a8cb8 DB |
2850 | } |
2851 | ||
ca369602 DB |
2852 | static bool is_pkt_reg(struct bpf_verifier_env *env, int regno) |
2853 | { | |
2a159c6f | 2854 | const struct bpf_reg_state *reg = reg_state(env, regno); |
ca369602 DB |
2855 | |
2856 | return type_is_pkt_pointer(reg->type); | |
2857 | } | |
2858 | ||
4b5defde DB |
2859 | static bool is_flow_key_reg(struct bpf_verifier_env *env, int regno) |
2860 | { | |
2861 | const struct bpf_reg_state *reg = reg_state(env, regno); | |
2862 | ||
2863 | /* Separate to is_ctx_reg() since we still want to allow BPF_ST here. */ | |
2864 | return reg->type == PTR_TO_FLOW_KEYS; | |
2865 | } | |
2866 | ||
61bd5218 JK |
2867 | static int check_pkt_ptr_alignment(struct bpf_verifier_env *env, |
2868 | const struct bpf_reg_state *reg, | |
d1174416 | 2869 | int off, int size, bool strict) |
969bf05e | 2870 | { |
f1174f77 | 2871 | struct tnum reg_off; |
e07b98d9 | 2872 | int ip_align; |
d1174416 DM |
2873 | |
2874 | /* Byte size accesses are always allowed. */ | |
2875 | if (!strict || size == 1) | |
2876 | return 0; | |
2877 | ||
e4eda884 DM |
2878 | /* For platforms that do not have a Kconfig enabling |
2879 | * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of | |
2880 | * NET_IP_ALIGN is universally set to '2'. And on platforms | |
2881 | * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get | |
2882 | * to this code only in strict mode where we want to emulate | |
2883 | * the NET_IP_ALIGN==2 checking. Therefore use an | |
2884 | * unconditional IP align value of '2'. | |
e07b98d9 | 2885 | */ |
e4eda884 | 2886 | ip_align = 2; |
f1174f77 EC |
2887 | |
2888 | reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off)); | |
2889 | if (!tnum_is_aligned(reg_off, size)) { | |
2890 | char tn_buf[48]; | |
2891 | ||
2892 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
61bd5218 JK |
2893 | verbose(env, |
2894 | "misaligned packet access off %d+%s+%d+%d size %d\n", | |
f1174f77 | 2895 | ip_align, tn_buf, reg->off, off, size); |
969bf05e AS |
2896 | return -EACCES; |
2897 | } | |
79adffcd | 2898 | |
969bf05e AS |
2899 | return 0; |
2900 | } | |
2901 | ||
61bd5218 JK |
2902 | static int check_generic_ptr_alignment(struct bpf_verifier_env *env, |
2903 | const struct bpf_reg_state *reg, | |
f1174f77 EC |
2904 | const char *pointer_desc, |
2905 | int off, int size, bool strict) | |
79adffcd | 2906 | { |
f1174f77 EC |
2907 | struct tnum reg_off; |
2908 | ||
2909 | /* Byte size accesses are always allowed. */ | |
2910 | if (!strict || size == 1) | |
2911 | return 0; | |
2912 | ||
2913 | reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off)); | |
2914 | if (!tnum_is_aligned(reg_off, size)) { | |
2915 | char tn_buf[48]; | |
2916 | ||
2917 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
61bd5218 | 2918 | verbose(env, "misaligned %saccess off %s+%d+%d size %d\n", |
f1174f77 | 2919 | pointer_desc, tn_buf, reg->off, off, size); |
79adffcd DB |
2920 | return -EACCES; |
2921 | } | |
2922 | ||
969bf05e AS |
2923 | return 0; |
2924 | } | |
2925 | ||
e07b98d9 | 2926 | static int check_ptr_alignment(struct bpf_verifier_env *env, |
ca369602 DB |
2927 | const struct bpf_reg_state *reg, int off, |
2928 | int size, bool strict_alignment_once) | |
79adffcd | 2929 | { |
ca369602 | 2930 | bool strict = env->strict_alignment || strict_alignment_once; |
f1174f77 | 2931 | const char *pointer_desc = ""; |
d1174416 | 2932 | |
79adffcd DB |
2933 | switch (reg->type) { |
2934 | case PTR_TO_PACKET: | |
de8f3a83 DB |
2935 | case PTR_TO_PACKET_META: |
2936 | /* Special case, because of NET_IP_ALIGN. Given metadata sits | |
2937 | * right in front, treat it the very same way. | |
2938 | */ | |
61bd5218 | 2939 | return check_pkt_ptr_alignment(env, reg, off, size, strict); |
d58e468b PP |
2940 | case PTR_TO_FLOW_KEYS: |
2941 | pointer_desc = "flow keys "; | |
2942 | break; | |
f1174f77 EC |
2943 | case PTR_TO_MAP_VALUE: |
2944 | pointer_desc = "value "; | |
2945 | break; | |
2946 | case PTR_TO_CTX: | |
2947 | pointer_desc = "context "; | |
2948 | break; | |
2949 | case PTR_TO_STACK: | |
2950 | pointer_desc = "stack "; | |
a5ec6ae1 JH |
2951 | /* The stack spill tracking logic in check_stack_write() |
2952 | * and check_stack_read() relies on stack accesses being | |
2953 | * aligned. | |
2954 | */ | |
2955 | strict = true; | |
f1174f77 | 2956 | break; |
c64b7983 JS |
2957 | case PTR_TO_SOCKET: |
2958 | pointer_desc = "sock "; | |
2959 | break; | |
46f8bc92 MKL |
2960 | case PTR_TO_SOCK_COMMON: |
2961 | pointer_desc = "sock_common "; | |
2962 | break; | |
655a51e5 MKL |
2963 | case PTR_TO_TCP_SOCK: |
2964 | pointer_desc = "tcp_sock "; | |
2965 | break; | |
fada7fdc JL |
2966 | case PTR_TO_XDP_SOCK: |
2967 | pointer_desc = "xdp_sock "; | |
2968 | break; | |
79adffcd | 2969 | default: |
f1174f77 | 2970 | break; |
79adffcd | 2971 | } |
61bd5218 JK |
2972 | return check_generic_ptr_alignment(env, reg, pointer_desc, off, size, |
2973 | strict); | |
79adffcd DB |
2974 | } |
2975 | ||
f4d7e40a AS |
2976 | static int update_stack_depth(struct bpf_verifier_env *env, |
2977 | const struct bpf_func_state *func, | |
2978 | int off) | |
2979 | { | |
9c8105bd | 2980 | u16 stack = env->subprog_info[func->subprogno].stack_depth; |
f4d7e40a AS |
2981 | |
2982 | if (stack >= -off) | |
2983 | return 0; | |
2984 | ||
2985 | /* update known max for given subprogram */ | |
9c8105bd | 2986 | env->subprog_info[func->subprogno].stack_depth = -off; |
70a87ffe AS |
2987 | return 0; |
2988 | } | |
f4d7e40a | 2989 | |
70a87ffe AS |
2990 | /* starting from main bpf function walk all instructions of the function |
2991 | * and recursively walk all callees that given function can call. | |
2992 | * Ignore jump and exit insns. | |
2993 | * Since recursion is prevented by check_cfg() this algorithm | |
2994 | * only needs a local stack of MAX_CALL_FRAMES to remember callsites | |
2995 | */ | |
2996 | static int check_max_stack_depth(struct bpf_verifier_env *env) | |
2997 | { | |
9c8105bd JW |
2998 | int depth = 0, frame = 0, idx = 0, i = 0, subprog_end; |
2999 | struct bpf_subprog_info *subprog = env->subprog_info; | |
70a87ffe | 3000 | struct bpf_insn *insn = env->prog->insnsi; |
ebf7d1f5 | 3001 | bool tail_call_reachable = false; |
70a87ffe AS |
3002 | int ret_insn[MAX_CALL_FRAMES]; |
3003 | int ret_prog[MAX_CALL_FRAMES]; | |
ebf7d1f5 | 3004 | int j; |
f4d7e40a | 3005 | |
70a87ffe | 3006 | process_func: |
7f6e4312 MF |
3007 | /* protect against potential stack overflow that might happen when |
3008 | * bpf2bpf calls get combined with tailcalls. Limit the caller's stack | |
3009 | * depth for such case down to 256 so that the worst case scenario | |
3010 | * would result in 8k stack size (32 which is tailcall limit * 256 = | |
3011 | * 8k). | |
3012 | * | |
3013 | * To get the idea what might happen, see an example: | |
3014 | * func1 -> sub rsp, 128 | |
3015 | * subfunc1 -> sub rsp, 256 | |
3016 | * tailcall1 -> add rsp, 256 | |
3017 | * func2 -> sub rsp, 192 (total stack size = 128 + 192 = 320) | |
3018 | * subfunc2 -> sub rsp, 64 | |
3019 | * subfunc22 -> sub rsp, 128 | |
3020 | * tailcall2 -> add rsp, 128 | |
3021 | * func3 -> sub rsp, 32 (total stack size 128 + 192 + 64 + 32 = 416) | |
3022 | * | |
3023 | * tailcall will unwind the current stack frame but it will not get rid | |
3024 | * of caller's stack as shown on the example above. | |
3025 | */ | |
3026 | if (idx && subprog[idx].has_tail_call && depth >= 256) { | |
3027 | verbose(env, | |
3028 | "tail_calls are not allowed when call stack of previous frames is %d bytes. Too large\n", | |
3029 | depth); | |
3030 | return -EACCES; | |
3031 | } | |
70a87ffe AS |
3032 | /* round up to 32-bytes, since this is granularity |
3033 | * of interpreter stack size | |
3034 | */ | |
9c8105bd | 3035 | depth += round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
70a87ffe | 3036 | if (depth > MAX_BPF_STACK) { |
f4d7e40a | 3037 | verbose(env, "combined stack size of %d calls is %d. Too large\n", |
70a87ffe | 3038 | frame + 1, depth); |
f4d7e40a AS |
3039 | return -EACCES; |
3040 | } | |
70a87ffe | 3041 | continue_func: |
4cb3d99c | 3042 | subprog_end = subprog[idx + 1].start; |
70a87ffe AS |
3043 | for (; i < subprog_end; i++) { |
3044 | if (insn[i].code != (BPF_JMP | BPF_CALL)) | |
3045 | continue; | |
3046 | if (insn[i].src_reg != BPF_PSEUDO_CALL) | |
3047 | continue; | |
3048 | /* remember insn and function to return to */ | |
3049 | ret_insn[frame] = i + 1; | |
9c8105bd | 3050 | ret_prog[frame] = idx; |
70a87ffe AS |
3051 | |
3052 | /* find the callee */ | |
3053 | i = i + insn[i].imm + 1; | |
9c8105bd JW |
3054 | idx = find_subprog(env, i); |
3055 | if (idx < 0) { | |
70a87ffe AS |
3056 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
3057 | i); | |
3058 | return -EFAULT; | |
3059 | } | |
ebf7d1f5 MF |
3060 | |
3061 | if (subprog[idx].has_tail_call) | |
3062 | tail_call_reachable = true; | |
3063 | ||
70a87ffe AS |
3064 | frame++; |
3065 | if (frame >= MAX_CALL_FRAMES) { | |
927cb781 PC |
3066 | verbose(env, "the call stack of %d frames is too deep !\n", |
3067 | frame); | |
3068 | return -E2BIG; | |
70a87ffe AS |
3069 | } |
3070 | goto process_func; | |
3071 | } | |
ebf7d1f5 MF |
3072 | /* if tail call got detected across bpf2bpf calls then mark each of the |
3073 | * currently present subprog frames as tail call reachable subprogs; | |
3074 | * this info will be utilized by JIT so that we will be preserving the | |
3075 | * tail call counter throughout bpf2bpf calls combined with tailcalls | |
3076 | */ | |
3077 | if (tail_call_reachable) | |
3078 | for (j = 0; j < frame; j++) | |
3079 | subprog[ret_prog[j]].tail_call_reachable = true; | |
3080 | ||
70a87ffe AS |
3081 | /* end of for() loop means the last insn of the 'subprog' |
3082 | * was reached. Doesn't matter whether it was JA or EXIT | |
3083 | */ | |
3084 | if (frame == 0) | |
3085 | return 0; | |
9c8105bd | 3086 | depth -= round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
70a87ffe AS |
3087 | frame--; |
3088 | i = ret_insn[frame]; | |
9c8105bd | 3089 | idx = ret_prog[frame]; |
70a87ffe | 3090 | goto continue_func; |
f4d7e40a AS |
3091 | } |
3092 | ||
19d28fbd | 3093 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
1ea47e01 AS |
3094 | static int get_callee_stack_depth(struct bpf_verifier_env *env, |
3095 | const struct bpf_insn *insn, int idx) | |
3096 | { | |
3097 | int start = idx + insn->imm + 1, subprog; | |
3098 | ||
3099 | subprog = find_subprog(env, start); | |
3100 | if (subprog < 0) { | |
3101 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", | |
3102 | start); | |
3103 | return -EFAULT; | |
3104 | } | |
9c8105bd | 3105 | return env->subprog_info[subprog].stack_depth; |
1ea47e01 | 3106 | } |
19d28fbd | 3107 | #endif |
1ea47e01 | 3108 | |
51c39bb1 AS |
3109 | int check_ctx_reg(struct bpf_verifier_env *env, |
3110 | const struct bpf_reg_state *reg, int regno) | |
58990d1f DB |
3111 | { |
3112 | /* Access to ctx or passing it to a helper is only allowed in | |
3113 | * its original, unmodified form. | |
3114 | */ | |
3115 | ||
3116 | if (reg->off) { | |
3117 | verbose(env, "dereference of modified ctx ptr R%d off=%d disallowed\n", | |
3118 | regno, reg->off); | |
3119 | return -EACCES; | |
3120 | } | |
3121 | ||
3122 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
3123 | char tn_buf[48]; | |
3124 | ||
3125 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
3126 | verbose(env, "variable ctx access var_off=%s disallowed\n", tn_buf); | |
3127 | return -EACCES; | |
3128 | } | |
3129 | ||
3130 | return 0; | |
3131 | } | |
3132 | ||
afbf21dc YS |
3133 | static int __check_buffer_access(struct bpf_verifier_env *env, |
3134 | const char *buf_info, | |
3135 | const struct bpf_reg_state *reg, | |
3136 | int regno, int off, int size) | |
9df1c28b MM |
3137 | { |
3138 | if (off < 0) { | |
3139 | verbose(env, | |
4fc00b79 | 3140 | "R%d invalid %s buffer access: off=%d, size=%d\n", |
afbf21dc | 3141 | regno, buf_info, off, size); |
9df1c28b MM |
3142 | return -EACCES; |
3143 | } | |
3144 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
3145 | char tn_buf[48]; | |
3146 | ||
3147 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
3148 | verbose(env, | |
4fc00b79 | 3149 | "R%d invalid variable buffer offset: off=%d, var_off=%s\n", |
9df1c28b MM |
3150 | regno, off, tn_buf); |
3151 | return -EACCES; | |
3152 | } | |
afbf21dc YS |
3153 | |
3154 | return 0; | |
3155 | } | |
3156 | ||
3157 | static int check_tp_buffer_access(struct bpf_verifier_env *env, | |
3158 | const struct bpf_reg_state *reg, | |
3159 | int regno, int off, int size) | |
3160 | { | |
3161 | int err; | |
3162 | ||
3163 | err = __check_buffer_access(env, "tracepoint", reg, regno, off, size); | |
3164 | if (err) | |
3165 | return err; | |
3166 | ||
9df1c28b MM |
3167 | if (off + size > env->prog->aux->max_tp_access) |
3168 | env->prog->aux->max_tp_access = off + size; | |
3169 | ||
3170 | return 0; | |
3171 | } | |
3172 | ||
afbf21dc YS |
3173 | static int check_buffer_access(struct bpf_verifier_env *env, |
3174 | const struct bpf_reg_state *reg, | |
3175 | int regno, int off, int size, | |
3176 | bool zero_size_allowed, | |
3177 | const char *buf_info, | |
3178 | u32 *max_access) | |
3179 | { | |
3180 | int err; | |
3181 | ||
3182 | err = __check_buffer_access(env, buf_info, reg, regno, off, size); | |
3183 | if (err) | |
3184 | return err; | |
3185 | ||
3186 | if (off + size > *max_access) | |
3187 | *max_access = off + size; | |
3188 | ||
3189 | return 0; | |
3190 | } | |
3191 | ||
3f50f132 JF |
3192 | /* BPF architecture zero extends alu32 ops into 64-bit registesr */ |
3193 | static void zext_32_to_64(struct bpf_reg_state *reg) | |
3194 | { | |
3195 | reg->var_off = tnum_subreg(reg->var_off); | |
3196 | __reg_assign_32_into_64(reg); | |
3197 | } | |
9df1c28b | 3198 | |
0c17d1d2 JH |
3199 | /* truncate register to smaller size (in bytes) |
3200 | * must be called with size < BPF_REG_SIZE | |
3201 | */ | |
3202 | static void coerce_reg_to_size(struct bpf_reg_state *reg, int size) | |
3203 | { | |
3204 | u64 mask; | |
3205 | ||
3206 | /* clear high bits in bit representation */ | |
3207 | reg->var_off = tnum_cast(reg->var_off, size); | |
3208 | ||
3209 | /* fix arithmetic bounds */ | |
3210 | mask = ((u64)1 << (size * 8)) - 1; | |
3211 | if ((reg->umin_value & ~mask) == (reg->umax_value & ~mask)) { | |
3212 | reg->umin_value &= mask; | |
3213 | reg->umax_value &= mask; | |
3214 | } else { | |
3215 | reg->umin_value = 0; | |
3216 | reg->umax_value = mask; | |
3217 | } | |
3218 | reg->smin_value = reg->umin_value; | |
3219 | reg->smax_value = reg->umax_value; | |
3f50f132 JF |
3220 | |
3221 | /* If size is smaller than 32bit register the 32bit register | |
3222 | * values are also truncated so we push 64-bit bounds into | |
3223 | * 32-bit bounds. Above were truncated < 32-bits already. | |
3224 | */ | |
3225 | if (size >= 4) | |
3226 | return; | |
3227 | __reg_combine_64_into_32(reg); | |
0c17d1d2 JH |
3228 | } |
3229 | ||
a23740ec AN |
3230 | static bool bpf_map_is_rdonly(const struct bpf_map *map) |
3231 | { | |
3232 | return (map->map_flags & BPF_F_RDONLY_PROG) && map->frozen; | |
3233 | } | |
3234 | ||
3235 | static int bpf_map_direct_read(struct bpf_map *map, int off, int size, u64 *val) | |
3236 | { | |
3237 | void *ptr; | |
3238 | u64 addr; | |
3239 | int err; | |
3240 | ||
3241 | err = map->ops->map_direct_value_addr(map, &addr, off); | |
3242 | if (err) | |
3243 | return err; | |
2dedd7d2 | 3244 | ptr = (void *)(long)addr + off; |
a23740ec AN |
3245 | |
3246 | switch (size) { | |
3247 | case sizeof(u8): | |
3248 | *val = (u64)*(u8 *)ptr; | |
3249 | break; | |
3250 | case sizeof(u16): | |
3251 | *val = (u64)*(u16 *)ptr; | |
3252 | break; | |
3253 | case sizeof(u32): | |
3254 | *val = (u64)*(u32 *)ptr; | |
3255 | break; | |
3256 | case sizeof(u64): | |
3257 | *val = *(u64 *)ptr; | |
3258 | break; | |
3259 | default: | |
3260 | return -EINVAL; | |
3261 | } | |
3262 | return 0; | |
3263 | } | |
3264 | ||
9e15db66 AS |
3265 | static int check_ptr_to_btf_access(struct bpf_verifier_env *env, |
3266 | struct bpf_reg_state *regs, | |
3267 | int regno, int off, int size, | |
3268 | enum bpf_access_type atype, | |
3269 | int value_regno) | |
3270 | { | |
3271 | struct bpf_reg_state *reg = regs + regno; | |
3272 | const struct btf_type *t = btf_type_by_id(btf_vmlinux, reg->btf_id); | |
3273 | const char *tname = btf_name_by_offset(btf_vmlinux, t->name_off); | |
3274 | u32 btf_id; | |
3275 | int ret; | |
3276 | ||
9e15db66 AS |
3277 | if (off < 0) { |
3278 | verbose(env, | |
3279 | "R%d is ptr_%s invalid negative access: off=%d\n", | |
3280 | regno, tname, off); | |
3281 | return -EACCES; | |
3282 | } | |
3283 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
3284 | char tn_buf[48]; | |
3285 | ||
3286 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
3287 | verbose(env, | |
3288 | "R%d is ptr_%s invalid variable offset: off=%d, var_off=%s\n", | |
3289 | regno, tname, off, tn_buf); | |
3290 | return -EACCES; | |
3291 | } | |
3292 | ||
27ae7997 MKL |
3293 | if (env->ops->btf_struct_access) { |
3294 | ret = env->ops->btf_struct_access(&env->log, t, off, size, | |
3295 | atype, &btf_id); | |
3296 | } else { | |
3297 | if (atype != BPF_READ) { | |
3298 | verbose(env, "only read is supported\n"); | |
3299 | return -EACCES; | |
3300 | } | |
3301 | ||
3302 | ret = btf_struct_access(&env->log, t, off, size, atype, | |
3303 | &btf_id); | |
3304 | } | |
3305 | ||
9e15db66 AS |
3306 | if (ret < 0) |
3307 | return ret; | |
3308 | ||
41c48f3a AI |
3309 | if (atype == BPF_READ && value_regno >= 0) |
3310 | mark_btf_ld_reg(env, regs, value_regno, ret, btf_id); | |
3311 | ||
3312 | return 0; | |
3313 | } | |
3314 | ||
3315 | static int check_ptr_to_map_access(struct bpf_verifier_env *env, | |
3316 | struct bpf_reg_state *regs, | |
3317 | int regno, int off, int size, | |
3318 | enum bpf_access_type atype, | |
3319 | int value_regno) | |
3320 | { | |
3321 | struct bpf_reg_state *reg = regs + regno; | |
3322 | struct bpf_map *map = reg->map_ptr; | |
3323 | const struct btf_type *t; | |
3324 | const char *tname; | |
3325 | u32 btf_id; | |
3326 | int ret; | |
3327 | ||
3328 | if (!btf_vmlinux) { | |
3329 | verbose(env, "map_ptr access not supported without CONFIG_DEBUG_INFO_BTF\n"); | |
3330 | return -ENOTSUPP; | |
3331 | } | |
3332 | ||
3333 | if (!map->ops->map_btf_id || !*map->ops->map_btf_id) { | |
3334 | verbose(env, "map_ptr access not supported for map type %d\n", | |
3335 | map->map_type); | |
3336 | return -ENOTSUPP; | |
3337 | } | |
3338 | ||
3339 | t = btf_type_by_id(btf_vmlinux, *map->ops->map_btf_id); | |
3340 | tname = btf_name_by_offset(btf_vmlinux, t->name_off); | |
3341 | ||
3342 | if (!env->allow_ptr_to_map_access) { | |
3343 | verbose(env, | |
3344 | "%s access is allowed only to CAP_PERFMON and CAP_SYS_ADMIN\n", | |
3345 | tname); | |
3346 | return -EPERM; | |
9e15db66 | 3347 | } |
27ae7997 | 3348 | |
41c48f3a AI |
3349 | if (off < 0) { |
3350 | verbose(env, "R%d is %s invalid negative access: off=%d\n", | |
3351 | regno, tname, off); | |
3352 | return -EACCES; | |
3353 | } | |
3354 | ||
3355 | if (atype != BPF_READ) { | |
3356 | verbose(env, "only read from %s is supported\n", tname); | |
3357 | return -EACCES; | |
3358 | } | |
3359 | ||
3360 | ret = btf_struct_access(&env->log, t, off, size, atype, &btf_id); | |
3361 | if (ret < 0) | |
3362 | return ret; | |
3363 | ||
3364 | if (value_regno >= 0) | |
3365 | mark_btf_ld_reg(env, regs, value_regno, ret, btf_id); | |
3366 | ||
9e15db66 AS |
3367 | return 0; |
3368 | } | |
3369 | ||
41c48f3a | 3370 | |
17a52670 AS |
3371 | /* check whether memory at (regno + off) is accessible for t = (read | write) |
3372 | * if t==write, value_regno is a register which value is stored into memory | |
3373 | * if t==read, value_regno is a register which will receive the value from memory | |
3374 | * if t==write && value_regno==-1, some unknown value is stored into memory | |
3375 | * if t==read && value_regno==-1, don't care what we read from memory | |
3376 | */ | |
ca369602 DB |
3377 | static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno, |
3378 | int off, int bpf_size, enum bpf_access_type t, | |
3379 | int value_regno, bool strict_alignment_once) | |
17a52670 | 3380 | { |
638f5b90 AS |
3381 | struct bpf_reg_state *regs = cur_regs(env); |
3382 | struct bpf_reg_state *reg = regs + regno; | |
f4d7e40a | 3383 | struct bpf_func_state *state; |
17a52670 AS |
3384 | int size, err = 0; |
3385 | ||
3386 | size = bpf_size_to_bytes(bpf_size); | |
3387 | if (size < 0) | |
3388 | return size; | |
3389 | ||
f1174f77 | 3390 | /* alignment checks will add in reg->off themselves */ |
ca369602 | 3391 | err = check_ptr_alignment(env, reg, off, size, strict_alignment_once); |
969bf05e AS |
3392 | if (err) |
3393 | return err; | |
17a52670 | 3394 | |
f1174f77 EC |
3395 | /* for access checks, reg->off is just part of off */ |
3396 | off += reg->off; | |
3397 | ||
3398 | if (reg->type == PTR_TO_MAP_VALUE) { | |
1be7f75d AS |
3399 | if (t == BPF_WRITE && value_regno >= 0 && |
3400 | is_pointer_value(env, value_regno)) { | |
61bd5218 | 3401 | verbose(env, "R%d leaks addr into map\n", value_regno); |
1be7f75d AS |
3402 | return -EACCES; |
3403 | } | |
591fe988 DB |
3404 | err = check_map_access_type(env, regno, off, size, t); |
3405 | if (err) | |
3406 | return err; | |
9fd29c08 | 3407 | err = check_map_access(env, regno, off, size, false); |
a23740ec AN |
3408 | if (!err && t == BPF_READ && value_regno >= 0) { |
3409 | struct bpf_map *map = reg->map_ptr; | |
3410 | ||
3411 | /* if map is read-only, track its contents as scalars */ | |
3412 | if (tnum_is_const(reg->var_off) && | |
3413 | bpf_map_is_rdonly(map) && | |
3414 | map->ops->map_direct_value_addr) { | |
3415 | int map_off = off + reg->var_off.value; | |
3416 | u64 val = 0; | |
3417 | ||
3418 | err = bpf_map_direct_read(map, map_off, size, | |
3419 | &val); | |
3420 | if (err) | |
3421 | return err; | |
3422 | ||
3423 | regs[value_regno].type = SCALAR_VALUE; | |
3424 | __mark_reg_known(®s[value_regno], val); | |
3425 | } else { | |
3426 | mark_reg_unknown(env, regs, value_regno); | |
3427 | } | |
3428 | } | |
457f4436 AN |
3429 | } else if (reg->type == PTR_TO_MEM) { |
3430 | if (t == BPF_WRITE && value_regno >= 0 && | |
3431 | is_pointer_value(env, value_regno)) { | |
3432 | verbose(env, "R%d leaks addr into mem\n", value_regno); | |
3433 | return -EACCES; | |
3434 | } | |
3435 | err = check_mem_region_access(env, regno, off, size, | |
3436 | reg->mem_size, false); | |
3437 | if (!err && t == BPF_READ && value_regno >= 0) | |
3438 | mark_reg_unknown(env, regs, value_regno); | |
1a0dc1ac | 3439 | } else if (reg->type == PTR_TO_CTX) { |
f1174f77 | 3440 | enum bpf_reg_type reg_type = SCALAR_VALUE; |
9e15db66 | 3441 | u32 btf_id = 0; |
19de99f7 | 3442 | |
1be7f75d AS |
3443 | if (t == BPF_WRITE && value_regno >= 0 && |
3444 | is_pointer_value(env, value_regno)) { | |
61bd5218 | 3445 | verbose(env, "R%d leaks addr into ctx\n", value_regno); |
1be7f75d AS |
3446 | return -EACCES; |
3447 | } | |
f1174f77 | 3448 | |
58990d1f DB |
3449 | err = check_ctx_reg(env, reg, regno); |
3450 | if (err < 0) | |
3451 | return err; | |
3452 | ||
9e15db66 AS |
3453 | err = check_ctx_access(env, insn_idx, off, size, t, ®_type, &btf_id); |
3454 | if (err) | |
3455 | verbose_linfo(env, insn_idx, "; "); | |
969bf05e | 3456 | if (!err && t == BPF_READ && value_regno >= 0) { |
f1174f77 | 3457 | /* ctx access returns either a scalar, or a |
de8f3a83 DB |
3458 | * PTR_TO_PACKET[_META,_END]. In the latter |
3459 | * case, we know the offset is zero. | |
f1174f77 | 3460 | */ |
46f8bc92 | 3461 | if (reg_type == SCALAR_VALUE) { |
638f5b90 | 3462 | mark_reg_unknown(env, regs, value_regno); |
46f8bc92 | 3463 | } else { |
638f5b90 | 3464 | mark_reg_known_zero(env, regs, |
61bd5218 | 3465 | value_regno); |
46f8bc92 MKL |
3466 | if (reg_type_may_be_null(reg_type)) |
3467 | regs[value_regno].id = ++env->id_gen; | |
5327ed3d JW |
3468 | /* A load of ctx field could have different |
3469 | * actual load size with the one encoded in the | |
3470 | * insn. When the dst is PTR, it is for sure not | |
3471 | * a sub-register. | |
3472 | */ | |
3473 | regs[value_regno].subreg_def = DEF_NOT_SUBREG; | |
b121b341 YS |
3474 | if (reg_type == PTR_TO_BTF_ID || |
3475 | reg_type == PTR_TO_BTF_ID_OR_NULL) | |
9e15db66 | 3476 | regs[value_regno].btf_id = btf_id; |
46f8bc92 | 3477 | } |
638f5b90 | 3478 | regs[value_regno].type = reg_type; |
969bf05e | 3479 | } |
17a52670 | 3480 | |
f1174f77 | 3481 | } else if (reg->type == PTR_TO_STACK) { |
f1174f77 | 3482 | off += reg->var_off.value; |
e4298d25 DB |
3483 | err = check_stack_access(env, reg, off, size); |
3484 | if (err) | |
3485 | return err; | |
8726679a | 3486 | |
f4d7e40a AS |
3487 | state = func(env, reg); |
3488 | err = update_stack_depth(env, state, off); | |
3489 | if (err) | |
3490 | return err; | |
8726679a | 3491 | |
638f5b90 | 3492 | if (t == BPF_WRITE) |
61bd5218 | 3493 | err = check_stack_write(env, state, off, size, |
af86ca4e | 3494 | value_regno, insn_idx); |
638f5b90 | 3495 | else |
61bd5218 JK |
3496 | err = check_stack_read(env, state, off, size, |
3497 | value_regno); | |
de8f3a83 | 3498 | } else if (reg_is_pkt_pointer(reg)) { |
3a0af8fd | 3499 | if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) { |
61bd5218 | 3500 | verbose(env, "cannot write into packet\n"); |
969bf05e AS |
3501 | return -EACCES; |
3502 | } | |
4acf6c0b BB |
3503 | if (t == BPF_WRITE && value_regno >= 0 && |
3504 | is_pointer_value(env, value_regno)) { | |
61bd5218 JK |
3505 | verbose(env, "R%d leaks addr into packet\n", |
3506 | value_regno); | |
4acf6c0b BB |
3507 | return -EACCES; |
3508 | } | |
9fd29c08 | 3509 | err = check_packet_access(env, regno, off, size, false); |
969bf05e | 3510 | if (!err && t == BPF_READ && value_regno >= 0) |
638f5b90 | 3511 | mark_reg_unknown(env, regs, value_regno); |
d58e468b PP |
3512 | } else if (reg->type == PTR_TO_FLOW_KEYS) { |
3513 | if (t == BPF_WRITE && value_regno >= 0 && | |
3514 | is_pointer_value(env, value_regno)) { | |
3515 | verbose(env, "R%d leaks addr into flow keys\n", | |
3516 | value_regno); | |
3517 | return -EACCES; | |
3518 | } | |
3519 | ||
3520 | err = check_flow_keys_access(env, off, size); | |
3521 | if (!err && t == BPF_READ && value_regno >= 0) | |
3522 | mark_reg_unknown(env, regs, value_regno); | |
46f8bc92 | 3523 | } else if (type_is_sk_pointer(reg->type)) { |
c64b7983 | 3524 | if (t == BPF_WRITE) { |
46f8bc92 MKL |
3525 | verbose(env, "R%d cannot write into %s\n", |
3526 | regno, reg_type_str[reg->type]); | |
c64b7983 JS |
3527 | return -EACCES; |
3528 | } | |
5f456649 | 3529 | err = check_sock_access(env, insn_idx, regno, off, size, t); |
c64b7983 JS |
3530 | if (!err && value_regno >= 0) |
3531 | mark_reg_unknown(env, regs, value_regno); | |
9df1c28b MM |
3532 | } else if (reg->type == PTR_TO_TP_BUFFER) { |
3533 | err = check_tp_buffer_access(env, reg, regno, off, size); | |
3534 | if (!err && t == BPF_READ && value_regno >= 0) | |
3535 | mark_reg_unknown(env, regs, value_regno); | |
9e15db66 AS |
3536 | } else if (reg->type == PTR_TO_BTF_ID) { |
3537 | err = check_ptr_to_btf_access(env, regs, regno, off, size, t, | |
3538 | value_regno); | |
41c48f3a AI |
3539 | } else if (reg->type == CONST_PTR_TO_MAP) { |
3540 | err = check_ptr_to_map_access(env, regs, regno, off, size, t, | |
3541 | value_regno); | |
afbf21dc YS |
3542 | } else if (reg->type == PTR_TO_RDONLY_BUF) { |
3543 | if (t == BPF_WRITE) { | |
3544 | verbose(env, "R%d cannot write into %s\n", | |
3545 | regno, reg_type_str[reg->type]); | |
3546 | return -EACCES; | |
3547 | } | |
f6dfbe31 CIK |
3548 | err = check_buffer_access(env, reg, regno, off, size, false, |
3549 | "rdonly", | |
afbf21dc YS |
3550 | &env->prog->aux->max_rdonly_access); |
3551 | if (!err && value_regno >= 0) | |
3552 | mark_reg_unknown(env, regs, value_regno); | |
3553 | } else if (reg->type == PTR_TO_RDWR_BUF) { | |
f6dfbe31 CIK |
3554 | err = check_buffer_access(env, reg, regno, off, size, false, |
3555 | "rdwr", | |
afbf21dc YS |
3556 | &env->prog->aux->max_rdwr_access); |
3557 | if (!err && t == BPF_READ && value_regno >= 0) | |
3558 | mark_reg_unknown(env, regs, value_regno); | |
17a52670 | 3559 | } else { |
61bd5218 JK |
3560 | verbose(env, "R%d invalid mem access '%s'\n", regno, |
3561 | reg_type_str[reg->type]); | |
17a52670 AS |
3562 | return -EACCES; |
3563 | } | |
969bf05e | 3564 | |
f1174f77 | 3565 | if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ && |
638f5b90 | 3566 | regs[value_regno].type == SCALAR_VALUE) { |
f1174f77 | 3567 | /* b/h/w load zero-extends, mark upper bits as known 0 */ |
0c17d1d2 | 3568 | coerce_reg_to_size(®s[value_regno], size); |
969bf05e | 3569 | } |
17a52670 AS |
3570 | return err; |
3571 | } | |
3572 | ||
31fd8581 | 3573 | static int check_xadd(struct bpf_verifier_env *env, int insn_idx, struct bpf_insn *insn) |
17a52670 | 3574 | { |
17a52670 AS |
3575 | int err; |
3576 | ||
3577 | if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) || | |
3578 | insn->imm != 0) { | |
61bd5218 | 3579 | verbose(env, "BPF_XADD uses reserved fields\n"); |
17a52670 AS |
3580 | return -EINVAL; |
3581 | } | |
3582 | ||
3583 | /* check src1 operand */ | |
dc503a8a | 3584 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
3585 | if (err) |
3586 | return err; | |
3587 | ||
3588 | /* check src2 operand */ | |
dc503a8a | 3589 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
3590 | if (err) |
3591 | return err; | |
3592 | ||
6bdf6abc | 3593 | if (is_pointer_value(env, insn->src_reg)) { |
61bd5218 | 3594 | verbose(env, "R%d leaks addr into mem\n", insn->src_reg); |
6bdf6abc DB |
3595 | return -EACCES; |
3596 | } | |
3597 | ||
ca369602 | 3598 | if (is_ctx_reg(env, insn->dst_reg) || |
4b5defde | 3599 | is_pkt_reg(env, insn->dst_reg) || |
46f8bc92 MKL |
3600 | is_flow_key_reg(env, insn->dst_reg) || |
3601 | is_sk_reg(env, insn->dst_reg)) { | |
ca369602 | 3602 | verbose(env, "BPF_XADD stores into R%d %s is not allowed\n", |
2a159c6f DB |
3603 | insn->dst_reg, |
3604 | reg_type_str[reg_state(env, insn->dst_reg)->type]); | |
f37a8cb8 DB |
3605 | return -EACCES; |
3606 | } | |
3607 | ||
17a52670 | 3608 | /* check whether atomic_add can read the memory */ |
31fd8581 | 3609 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
ca369602 | 3610 | BPF_SIZE(insn->code), BPF_READ, -1, true); |
17a52670 AS |
3611 | if (err) |
3612 | return err; | |
3613 | ||
3614 | /* check whether atomic_add can write into the same memory */ | |
31fd8581 | 3615 | return check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
ca369602 | 3616 | BPF_SIZE(insn->code), BPF_WRITE, -1, true); |
17a52670 AS |
3617 | } |
3618 | ||
2011fccf AI |
3619 | static int __check_stack_boundary(struct bpf_verifier_env *env, u32 regno, |
3620 | int off, int access_size, | |
3621 | bool zero_size_allowed) | |
3622 | { | |
3623 | struct bpf_reg_state *reg = reg_state(env, regno); | |
3624 | ||
3625 | if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 || | |
3626 | access_size < 0 || (access_size == 0 && !zero_size_allowed)) { | |
3627 | if (tnum_is_const(reg->var_off)) { | |
3628 | verbose(env, "invalid stack type R%d off=%d access_size=%d\n", | |
3629 | regno, off, access_size); | |
3630 | } else { | |
3631 | char tn_buf[48]; | |
3632 | ||
3633 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
3634 | verbose(env, "invalid stack type R%d var_off=%s access_size=%d\n", | |
3635 | regno, tn_buf, access_size); | |
3636 | } | |
3637 | return -EACCES; | |
3638 | } | |
3639 | return 0; | |
3640 | } | |
3641 | ||
17a52670 AS |
3642 | /* when register 'regno' is passed into function that will read 'access_size' |
3643 | * bytes from that pointer, make sure that it's within stack boundary | |
f1174f77 EC |
3644 | * and all elements of stack are initialized. |
3645 | * Unlike most pointer bounds-checking functions, this one doesn't take an | |
3646 | * 'off' argument, so it has to add in reg->off itself. | |
17a52670 | 3647 | */ |
58e2af8b | 3648 | static int check_stack_boundary(struct bpf_verifier_env *env, int regno, |
435faee1 DB |
3649 | int access_size, bool zero_size_allowed, |
3650 | struct bpf_call_arg_meta *meta) | |
17a52670 | 3651 | { |
2a159c6f | 3652 | struct bpf_reg_state *reg = reg_state(env, regno); |
f4d7e40a | 3653 | struct bpf_func_state *state = func(env, reg); |
f7cf25b2 | 3654 | int err, min_off, max_off, i, j, slot, spi; |
17a52670 | 3655 | |
2011fccf AI |
3656 | if (tnum_is_const(reg->var_off)) { |
3657 | min_off = max_off = reg->var_off.value + reg->off; | |
3658 | err = __check_stack_boundary(env, regno, min_off, access_size, | |
3659 | zero_size_allowed); | |
3660 | if (err) | |
3661 | return err; | |
3662 | } else { | |
088ec26d AI |
3663 | /* Variable offset is prohibited for unprivileged mode for |
3664 | * simplicity since it requires corresponding support in | |
3665 | * Spectre masking for stack ALU. | |
3666 | * See also retrieve_ptr_limit(). | |
3667 | */ | |
2c78ee89 | 3668 | if (!env->bypass_spec_v1) { |
088ec26d | 3669 | char tn_buf[48]; |
f1174f77 | 3670 | |
088ec26d AI |
3671 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
3672 | verbose(env, "R%d indirect variable offset stack access prohibited for !root, var_off=%s\n", | |
3673 | regno, tn_buf); | |
3674 | return -EACCES; | |
3675 | } | |
f2bcd05e AI |
3676 | /* Only initialized buffer on stack is allowed to be accessed |
3677 | * with variable offset. With uninitialized buffer it's hard to | |
3678 | * guarantee that whole memory is marked as initialized on | |
3679 | * helper return since specific bounds are unknown what may | |
3680 | * cause uninitialized stack leaking. | |
3681 | */ | |
3682 | if (meta && meta->raw_mode) | |
3683 | meta = NULL; | |
3684 | ||
107c26a7 AI |
3685 | if (reg->smax_value >= BPF_MAX_VAR_OFF || |
3686 | reg->smax_value <= -BPF_MAX_VAR_OFF) { | |
3687 | verbose(env, "R%d unbounded indirect variable offset stack access\n", | |
3688 | regno); | |
3689 | return -EACCES; | |
3690 | } | |
2011fccf | 3691 | min_off = reg->smin_value + reg->off; |
107c26a7 | 3692 | max_off = reg->smax_value + reg->off; |
2011fccf AI |
3693 | err = __check_stack_boundary(env, regno, min_off, access_size, |
3694 | zero_size_allowed); | |
107c26a7 AI |
3695 | if (err) { |
3696 | verbose(env, "R%d min value is outside of stack bound\n", | |
3697 | regno); | |
2011fccf | 3698 | return err; |
107c26a7 | 3699 | } |
2011fccf AI |
3700 | err = __check_stack_boundary(env, regno, max_off, access_size, |
3701 | zero_size_allowed); | |
107c26a7 AI |
3702 | if (err) { |
3703 | verbose(env, "R%d max value is outside of stack bound\n", | |
3704 | regno); | |
2011fccf | 3705 | return err; |
107c26a7 | 3706 | } |
17a52670 AS |
3707 | } |
3708 | ||
435faee1 DB |
3709 | if (meta && meta->raw_mode) { |
3710 | meta->access_size = access_size; | |
3711 | meta->regno = regno; | |
3712 | return 0; | |
3713 | } | |
3714 | ||
2011fccf | 3715 | for (i = min_off; i < max_off + access_size; i++) { |
cc2b14d5 AS |
3716 | u8 *stype; |
3717 | ||
2011fccf | 3718 | slot = -i - 1; |
638f5b90 | 3719 | spi = slot / BPF_REG_SIZE; |
cc2b14d5 AS |
3720 | if (state->allocated_stack <= slot) |
3721 | goto err; | |
3722 | stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE]; | |
3723 | if (*stype == STACK_MISC) | |
3724 | goto mark; | |
3725 | if (*stype == STACK_ZERO) { | |
3726 | /* helper can write anything into the stack */ | |
3727 | *stype = STACK_MISC; | |
3728 | goto mark; | |
17a52670 | 3729 | } |
1d68f22b YS |
3730 | |
3731 | if (state->stack[spi].slot_type[0] == STACK_SPILL && | |
3732 | state->stack[spi].spilled_ptr.type == PTR_TO_BTF_ID) | |
3733 | goto mark; | |
3734 | ||
f7cf25b2 AS |
3735 | if (state->stack[spi].slot_type[0] == STACK_SPILL && |
3736 | state->stack[spi].spilled_ptr.type == SCALAR_VALUE) { | |
f54c7898 | 3737 | __mark_reg_unknown(env, &state->stack[spi].spilled_ptr); |
f7cf25b2 AS |
3738 | for (j = 0; j < BPF_REG_SIZE; j++) |
3739 | state->stack[spi].slot_type[j] = STACK_MISC; | |
3740 | goto mark; | |
3741 | } | |
3742 | ||
cc2b14d5 | 3743 | err: |
2011fccf AI |
3744 | if (tnum_is_const(reg->var_off)) { |
3745 | verbose(env, "invalid indirect read from stack off %d+%d size %d\n", | |
3746 | min_off, i - min_off, access_size); | |
3747 | } else { | |
3748 | char tn_buf[48]; | |
3749 | ||
3750 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
3751 | verbose(env, "invalid indirect read from stack var_off %s+%d size %d\n", | |
3752 | tn_buf, i - min_off, access_size); | |
3753 | } | |
cc2b14d5 AS |
3754 | return -EACCES; |
3755 | mark: | |
3756 | /* reading any byte out of 8-byte 'spill_slot' will cause | |
3757 | * the whole slot to be marked as 'read' | |
3758 | */ | |
679c782d | 3759 | mark_reg_read(env, &state->stack[spi].spilled_ptr, |
5327ed3d JW |
3760 | state->stack[spi].spilled_ptr.parent, |
3761 | REG_LIVE_READ64); | |
17a52670 | 3762 | } |
2011fccf | 3763 | return update_stack_depth(env, state, min_off); |
17a52670 AS |
3764 | } |
3765 | ||
06c1c049 GB |
3766 | static int check_helper_mem_access(struct bpf_verifier_env *env, int regno, |
3767 | int access_size, bool zero_size_allowed, | |
3768 | struct bpf_call_arg_meta *meta) | |
3769 | { | |
638f5b90 | 3770 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
06c1c049 | 3771 | |
f1174f77 | 3772 | switch (reg->type) { |
06c1c049 | 3773 | case PTR_TO_PACKET: |
de8f3a83 | 3774 | case PTR_TO_PACKET_META: |
9fd29c08 YS |
3775 | return check_packet_access(env, regno, reg->off, access_size, |
3776 | zero_size_allowed); | |
06c1c049 | 3777 | case PTR_TO_MAP_VALUE: |
591fe988 DB |
3778 | if (check_map_access_type(env, regno, reg->off, access_size, |
3779 | meta && meta->raw_mode ? BPF_WRITE : | |
3780 | BPF_READ)) | |
3781 | return -EACCES; | |
9fd29c08 YS |
3782 | return check_map_access(env, regno, reg->off, access_size, |
3783 | zero_size_allowed); | |
457f4436 AN |
3784 | case PTR_TO_MEM: |
3785 | return check_mem_region_access(env, regno, reg->off, | |
3786 | access_size, reg->mem_size, | |
3787 | zero_size_allowed); | |
afbf21dc YS |
3788 | case PTR_TO_RDONLY_BUF: |
3789 | if (meta && meta->raw_mode) | |
3790 | return -EACCES; | |
3791 | return check_buffer_access(env, reg, regno, reg->off, | |
3792 | access_size, zero_size_allowed, | |
3793 | "rdonly", | |
3794 | &env->prog->aux->max_rdonly_access); | |
3795 | case PTR_TO_RDWR_BUF: | |
3796 | return check_buffer_access(env, reg, regno, reg->off, | |
3797 | access_size, zero_size_allowed, | |
3798 | "rdwr", | |
3799 | &env->prog->aux->max_rdwr_access); | |
0d004c02 | 3800 | case PTR_TO_STACK: |
06c1c049 GB |
3801 | return check_stack_boundary(env, regno, access_size, |
3802 | zero_size_allowed, meta); | |
0d004c02 LB |
3803 | default: /* scalar_value or invalid ptr */ |
3804 | /* Allow zero-byte read from NULL, regardless of pointer type */ | |
3805 | if (zero_size_allowed && access_size == 0 && | |
3806 | register_is_null(reg)) | |
3807 | return 0; | |
3808 | ||
3809 | verbose(env, "R%d type=%s expected=%s\n", regno, | |
3810 | reg_type_str[reg->type], | |
3811 | reg_type_str[PTR_TO_STACK]); | |
3812 | return -EACCES; | |
06c1c049 GB |
3813 | } |
3814 | } | |
3815 | ||
d83525ca AS |
3816 | /* Implementation details: |
3817 | * bpf_map_lookup returns PTR_TO_MAP_VALUE_OR_NULL | |
3818 | * Two bpf_map_lookups (even with the same key) will have different reg->id. | |
3819 | * For traditional PTR_TO_MAP_VALUE the verifier clears reg->id after | |
3820 | * value_or_null->value transition, since the verifier only cares about | |
3821 | * the range of access to valid map value pointer and doesn't care about actual | |
3822 | * address of the map element. | |
3823 | * For maps with 'struct bpf_spin_lock' inside map value the verifier keeps | |
3824 | * reg->id > 0 after value_or_null->value transition. By doing so | |
3825 | * two bpf_map_lookups will be considered two different pointers that | |
3826 | * point to different bpf_spin_locks. | |
3827 | * The verifier allows taking only one bpf_spin_lock at a time to avoid | |
3828 | * dead-locks. | |
3829 | * Since only one bpf_spin_lock is allowed the checks are simpler than | |
3830 | * reg_is_refcounted() logic. The verifier needs to remember only | |
3831 | * one spin_lock instead of array of acquired_refs. | |
3832 | * cur_state->active_spin_lock remembers which map value element got locked | |
3833 | * and clears it after bpf_spin_unlock. | |
3834 | */ | |
3835 | static int process_spin_lock(struct bpf_verifier_env *env, int regno, | |
3836 | bool is_lock) | |
3837 | { | |
3838 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; | |
3839 | struct bpf_verifier_state *cur = env->cur_state; | |
3840 | bool is_const = tnum_is_const(reg->var_off); | |
3841 | struct bpf_map *map = reg->map_ptr; | |
3842 | u64 val = reg->var_off.value; | |
3843 | ||
d83525ca AS |
3844 | if (!is_const) { |
3845 | verbose(env, | |
3846 | "R%d doesn't have constant offset. bpf_spin_lock has to be at the constant offset\n", | |
3847 | regno); | |
3848 | return -EINVAL; | |
3849 | } | |
3850 | if (!map->btf) { | |
3851 | verbose(env, | |
3852 | "map '%s' has to have BTF in order to use bpf_spin_lock\n", | |
3853 | map->name); | |
3854 | return -EINVAL; | |
3855 | } | |
3856 | if (!map_value_has_spin_lock(map)) { | |
3857 | if (map->spin_lock_off == -E2BIG) | |
3858 | verbose(env, | |
3859 | "map '%s' has more than one 'struct bpf_spin_lock'\n", | |
3860 | map->name); | |
3861 | else if (map->spin_lock_off == -ENOENT) | |
3862 | verbose(env, | |
3863 | "map '%s' doesn't have 'struct bpf_spin_lock'\n", | |
3864 | map->name); | |
3865 | else | |
3866 | verbose(env, | |
3867 | "map '%s' is not a struct type or bpf_spin_lock is mangled\n", | |
3868 | map->name); | |
3869 | return -EINVAL; | |
3870 | } | |
3871 | if (map->spin_lock_off != val + reg->off) { | |
3872 | verbose(env, "off %lld doesn't point to 'struct bpf_spin_lock'\n", | |
3873 | val + reg->off); | |
3874 | return -EINVAL; | |
3875 | } | |
3876 | if (is_lock) { | |
3877 | if (cur->active_spin_lock) { | |
3878 | verbose(env, | |
3879 | "Locking two bpf_spin_locks are not allowed\n"); | |
3880 | return -EINVAL; | |
3881 | } | |
3882 | cur->active_spin_lock = reg->id; | |
3883 | } else { | |
3884 | if (!cur->active_spin_lock) { | |
3885 | verbose(env, "bpf_spin_unlock without taking a lock\n"); | |
3886 | return -EINVAL; | |
3887 | } | |
3888 | if (cur->active_spin_lock != reg->id) { | |
3889 | verbose(env, "bpf_spin_unlock of different lock\n"); | |
3890 | return -EINVAL; | |
3891 | } | |
3892 | cur->active_spin_lock = 0; | |
3893 | } | |
3894 | return 0; | |
3895 | } | |
3896 | ||
90133415 DB |
3897 | static bool arg_type_is_mem_ptr(enum bpf_arg_type type) |
3898 | { | |
3899 | return type == ARG_PTR_TO_MEM || | |
3900 | type == ARG_PTR_TO_MEM_OR_NULL || | |
3901 | type == ARG_PTR_TO_UNINIT_MEM; | |
3902 | } | |
3903 | ||
3904 | static bool arg_type_is_mem_size(enum bpf_arg_type type) | |
3905 | { | |
3906 | return type == ARG_CONST_SIZE || | |
3907 | type == ARG_CONST_SIZE_OR_ZERO; | |
3908 | } | |
3909 | ||
457f4436 AN |
3910 | static bool arg_type_is_alloc_size(enum bpf_arg_type type) |
3911 | { | |
3912 | return type == ARG_CONST_ALLOC_SIZE_OR_ZERO; | |
3913 | } | |
3914 | ||
57c3bb72 AI |
3915 | static bool arg_type_is_int_ptr(enum bpf_arg_type type) |
3916 | { | |
3917 | return type == ARG_PTR_TO_INT || | |
3918 | type == ARG_PTR_TO_LONG; | |
3919 | } | |
3920 | ||
3921 | static int int_ptr_type_to_size(enum bpf_arg_type type) | |
3922 | { | |
3923 | if (type == ARG_PTR_TO_INT) | |
3924 | return sizeof(u32); | |
3925 | else if (type == ARG_PTR_TO_LONG) | |
3926 | return sizeof(u64); | |
3927 | ||
3928 | return -EINVAL; | |
3929 | } | |
3930 | ||
912f442c LB |
3931 | static int resolve_map_arg_type(struct bpf_verifier_env *env, |
3932 | const struct bpf_call_arg_meta *meta, | |
3933 | enum bpf_arg_type *arg_type) | |
3934 | { | |
3935 | if (!meta->map_ptr) { | |
3936 | /* kernel subsystem misconfigured verifier */ | |
3937 | verbose(env, "invalid map_ptr to access map->type\n"); | |
3938 | return -EACCES; | |
3939 | } | |
3940 | ||
3941 | switch (meta->map_ptr->map_type) { | |
3942 | case BPF_MAP_TYPE_SOCKMAP: | |
3943 | case BPF_MAP_TYPE_SOCKHASH: | |
3944 | if (*arg_type == ARG_PTR_TO_MAP_VALUE) { | |
6550f2dd | 3945 | *arg_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON; |
912f442c LB |
3946 | } else { |
3947 | verbose(env, "invalid arg_type for sockmap/sockhash\n"); | |
3948 | return -EINVAL; | |
3949 | } | |
3950 | break; | |
3951 | ||
3952 | default: | |
3953 | break; | |
3954 | } | |
3955 | return 0; | |
3956 | } | |
3957 | ||
f79e7ea5 LB |
3958 | struct bpf_reg_types { |
3959 | const enum bpf_reg_type types[10]; | |
1df8f55a | 3960 | u32 *btf_id; |
f79e7ea5 LB |
3961 | }; |
3962 | ||
3963 | static const struct bpf_reg_types map_key_value_types = { | |
3964 | .types = { | |
3965 | PTR_TO_STACK, | |
3966 | PTR_TO_PACKET, | |
3967 | PTR_TO_PACKET_META, | |
3968 | PTR_TO_MAP_VALUE, | |
3969 | }, | |
3970 | }; | |
3971 | ||
3972 | static const struct bpf_reg_types sock_types = { | |
3973 | .types = { | |
3974 | PTR_TO_SOCK_COMMON, | |
3975 | PTR_TO_SOCKET, | |
3976 | PTR_TO_TCP_SOCK, | |
3977 | PTR_TO_XDP_SOCK, | |
3978 | }, | |
3979 | }; | |
3980 | ||
1df8f55a MKL |
3981 | static const struct bpf_reg_types btf_id_sock_common_types = { |
3982 | .types = { | |
3983 | PTR_TO_SOCK_COMMON, | |
3984 | PTR_TO_SOCKET, | |
3985 | PTR_TO_TCP_SOCK, | |
3986 | PTR_TO_XDP_SOCK, | |
3987 | PTR_TO_BTF_ID, | |
3988 | }, | |
3989 | .btf_id = &btf_sock_ids[BTF_SOCK_TYPE_SOCK_COMMON], | |
3990 | }; | |
3991 | ||
f79e7ea5 LB |
3992 | static const struct bpf_reg_types mem_types = { |
3993 | .types = { | |
3994 | PTR_TO_STACK, | |
3995 | PTR_TO_PACKET, | |
3996 | PTR_TO_PACKET_META, | |
3997 | PTR_TO_MAP_VALUE, | |
3998 | PTR_TO_MEM, | |
3999 | PTR_TO_RDONLY_BUF, | |
4000 | PTR_TO_RDWR_BUF, | |
4001 | }, | |
4002 | }; | |
4003 | ||
4004 | static const struct bpf_reg_types int_ptr_types = { | |
4005 | .types = { | |
4006 | PTR_TO_STACK, | |
4007 | PTR_TO_PACKET, | |
4008 | PTR_TO_PACKET_META, | |
4009 | PTR_TO_MAP_VALUE, | |
4010 | }, | |
4011 | }; | |
4012 | ||
4013 | static const struct bpf_reg_types fullsock_types = { .types = { PTR_TO_SOCKET } }; | |
4014 | static const struct bpf_reg_types scalar_types = { .types = { SCALAR_VALUE } }; | |
4015 | static const struct bpf_reg_types context_types = { .types = { PTR_TO_CTX } }; | |
4016 | static const struct bpf_reg_types alloc_mem_types = { .types = { PTR_TO_MEM } }; | |
4017 | static const struct bpf_reg_types const_map_ptr_types = { .types = { CONST_PTR_TO_MAP } }; | |
4018 | static const struct bpf_reg_types btf_ptr_types = { .types = { PTR_TO_BTF_ID } }; | |
4019 | static const struct bpf_reg_types spin_lock_types = { .types = { PTR_TO_MAP_VALUE } }; | |
4020 | ||
0789e13b | 4021 | static const struct bpf_reg_types *compatible_reg_types[__BPF_ARG_TYPE_MAX] = { |
f79e7ea5 LB |
4022 | [ARG_PTR_TO_MAP_KEY] = &map_key_value_types, |
4023 | [ARG_PTR_TO_MAP_VALUE] = &map_key_value_types, | |
4024 | [ARG_PTR_TO_UNINIT_MAP_VALUE] = &map_key_value_types, | |
4025 | [ARG_PTR_TO_MAP_VALUE_OR_NULL] = &map_key_value_types, | |
4026 | [ARG_CONST_SIZE] = &scalar_types, | |
4027 | [ARG_CONST_SIZE_OR_ZERO] = &scalar_types, | |
4028 | [ARG_CONST_ALLOC_SIZE_OR_ZERO] = &scalar_types, | |
4029 | [ARG_CONST_MAP_PTR] = &const_map_ptr_types, | |
4030 | [ARG_PTR_TO_CTX] = &context_types, | |
4031 | [ARG_PTR_TO_CTX_OR_NULL] = &context_types, | |
4032 | [ARG_PTR_TO_SOCK_COMMON] = &sock_types, | |
1df8f55a | 4033 | [ARG_PTR_TO_BTF_ID_SOCK_COMMON] = &btf_id_sock_common_types, |
f79e7ea5 LB |
4034 | [ARG_PTR_TO_SOCKET] = &fullsock_types, |
4035 | [ARG_PTR_TO_SOCKET_OR_NULL] = &fullsock_types, | |
4036 | [ARG_PTR_TO_BTF_ID] = &btf_ptr_types, | |
4037 | [ARG_PTR_TO_SPIN_LOCK] = &spin_lock_types, | |
4038 | [ARG_PTR_TO_MEM] = &mem_types, | |
4039 | [ARG_PTR_TO_MEM_OR_NULL] = &mem_types, | |
4040 | [ARG_PTR_TO_UNINIT_MEM] = &mem_types, | |
4041 | [ARG_PTR_TO_ALLOC_MEM] = &alloc_mem_types, | |
4042 | [ARG_PTR_TO_ALLOC_MEM_OR_NULL] = &alloc_mem_types, | |
4043 | [ARG_PTR_TO_INT] = &int_ptr_types, | |
4044 | [ARG_PTR_TO_LONG] = &int_ptr_types, | |
f79e7ea5 LB |
4045 | }; |
4046 | ||
4047 | static int check_reg_type(struct bpf_verifier_env *env, u32 regno, | |
a968d5e2 MKL |
4048 | enum bpf_arg_type arg_type, |
4049 | const u32 *arg_btf_id) | |
f79e7ea5 LB |
4050 | { |
4051 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; | |
4052 | enum bpf_reg_type expected, type = reg->type; | |
a968d5e2 | 4053 | const struct bpf_reg_types *compatible; |
f79e7ea5 LB |
4054 | int i, j; |
4055 | ||
a968d5e2 MKL |
4056 | compatible = compatible_reg_types[arg_type]; |
4057 | if (!compatible) { | |
4058 | verbose(env, "verifier internal error: unsupported arg type %d\n", arg_type); | |
4059 | return -EFAULT; | |
4060 | } | |
4061 | ||
f79e7ea5 LB |
4062 | for (i = 0; i < ARRAY_SIZE(compatible->types); i++) { |
4063 | expected = compatible->types[i]; | |
4064 | if (expected == NOT_INIT) | |
4065 | break; | |
4066 | ||
4067 | if (type == expected) | |
a968d5e2 | 4068 | goto found; |
f79e7ea5 LB |
4069 | } |
4070 | ||
4071 | verbose(env, "R%d type=%s expected=", regno, reg_type_str[type]); | |
4072 | for (j = 0; j + 1 < i; j++) | |
4073 | verbose(env, "%s, ", reg_type_str[compatible->types[j]]); | |
4074 | verbose(env, "%s\n", reg_type_str[compatible->types[j]]); | |
4075 | return -EACCES; | |
a968d5e2 MKL |
4076 | |
4077 | found: | |
4078 | if (type == PTR_TO_BTF_ID) { | |
1df8f55a MKL |
4079 | if (!arg_btf_id) { |
4080 | if (!compatible->btf_id) { | |
4081 | verbose(env, "verifier internal error: missing arg compatible BTF ID\n"); | |
4082 | return -EFAULT; | |
4083 | } | |
4084 | arg_btf_id = compatible->btf_id; | |
4085 | } | |
4086 | ||
a968d5e2 MKL |
4087 | if (!btf_struct_ids_match(&env->log, reg->off, reg->btf_id, |
4088 | *arg_btf_id)) { | |
4089 | verbose(env, "R%d is of type %s but %s is expected\n", | |
4090 | regno, kernel_type_name(reg->btf_id), | |
4091 | kernel_type_name(*arg_btf_id)); | |
4092 | return -EACCES; | |
4093 | } | |
4094 | ||
4095 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
4096 | verbose(env, "R%d is a pointer to in-kernel struct with non-zero offset\n", | |
4097 | regno); | |
4098 | return -EACCES; | |
4099 | } | |
4100 | } | |
4101 | ||
4102 | return 0; | |
f79e7ea5 LB |
4103 | } |
4104 | ||
af7ec138 YS |
4105 | static int check_func_arg(struct bpf_verifier_env *env, u32 arg, |
4106 | struct bpf_call_arg_meta *meta, | |
4107 | const struct bpf_func_proto *fn) | |
17a52670 | 4108 | { |
af7ec138 | 4109 | u32 regno = BPF_REG_1 + arg; |
638f5b90 | 4110 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
af7ec138 | 4111 | enum bpf_arg_type arg_type = fn->arg_type[arg]; |
f79e7ea5 | 4112 | enum bpf_reg_type type = reg->type; |
17a52670 AS |
4113 | int err = 0; |
4114 | ||
80f1d68c | 4115 | if (arg_type == ARG_DONTCARE) |
17a52670 AS |
4116 | return 0; |
4117 | ||
dc503a8a EC |
4118 | err = check_reg_arg(env, regno, SRC_OP); |
4119 | if (err) | |
4120 | return err; | |
17a52670 | 4121 | |
1be7f75d AS |
4122 | if (arg_type == ARG_ANYTHING) { |
4123 | if (is_pointer_value(env, regno)) { | |
61bd5218 JK |
4124 | verbose(env, "R%d leaks addr into helper function\n", |
4125 | regno); | |
1be7f75d AS |
4126 | return -EACCES; |
4127 | } | |
80f1d68c | 4128 | return 0; |
1be7f75d | 4129 | } |
80f1d68c | 4130 | |
de8f3a83 | 4131 | if (type_is_pkt_pointer(type) && |
3a0af8fd | 4132 | !may_access_direct_pkt_data(env, meta, BPF_READ)) { |
61bd5218 | 4133 | verbose(env, "helper access to the packet is not allowed\n"); |
6841de8b AS |
4134 | return -EACCES; |
4135 | } | |
4136 | ||
912f442c LB |
4137 | if (arg_type == ARG_PTR_TO_MAP_VALUE || |
4138 | arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE || | |
4139 | arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL) { | |
4140 | err = resolve_map_arg_type(env, meta, &arg_type); | |
4141 | if (err) | |
4142 | return err; | |
4143 | } | |
4144 | ||
fd1b0d60 LB |
4145 | if (register_is_null(reg) && arg_type_may_be_null(arg_type)) |
4146 | /* A NULL register has a SCALAR_VALUE type, so skip | |
4147 | * type checking. | |
4148 | */ | |
4149 | goto skip_type_check; | |
4150 | ||
a968d5e2 | 4151 | err = check_reg_type(env, regno, arg_type, fn->arg_btf_id[arg]); |
f79e7ea5 LB |
4152 | if (err) |
4153 | return err; | |
4154 | ||
a968d5e2 | 4155 | if (type == PTR_TO_CTX) { |
feec7040 LB |
4156 | err = check_ctx_reg(env, reg, regno); |
4157 | if (err < 0) | |
4158 | return err; | |
d7b9454a LB |
4159 | } |
4160 | ||
fd1b0d60 | 4161 | skip_type_check: |
02f7c958 | 4162 | if (reg->ref_obj_id) { |
457f4436 AN |
4163 | if (meta->ref_obj_id) { |
4164 | verbose(env, "verifier internal error: more than one arg with ref_obj_id R%d %u %u\n", | |
4165 | regno, reg->ref_obj_id, | |
4166 | meta->ref_obj_id); | |
4167 | return -EFAULT; | |
4168 | } | |
4169 | meta->ref_obj_id = reg->ref_obj_id; | |
17a52670 AS |
4170 | } |
4171 | ||
17a52670 AS |
4172 | if (arg_type == ARG_CONST_MAP_PTR) { |
4173 | /* bpf_map_xxx(map_ptr) call: remember that map_ptr */ | |
33ff9823 | 4174 | meta->map_ptr = reg->map_ptr; |
17a52670 AS |
4175 | } else if (arg_type == ARG_PTR_TO_MAP_KEY) { |
4176 | /* bpf_map_xxx(..., map_ptr, ..., key) call: | |
4177 | * check that [key, key + map->key_size) are within | |
4178 | * stack limits and initialized | |
4179 | */ | |
33ff9823 | 4180 | if (!meta->map_ptr) { |
17a52670 AS |
4181 | /* in function declaration map_ptr must come before |
4182 | * map_key, so that it's verified and known before | |
4183 | * we have to check map_key here. Otherwise it means | |
4184 | * that kernel subsystem misconfigured verifier | |
4185 | */ | |
61bd5218 | 4186 | verbose(env, "invalid map_ptr to access map->key\n"); |
17a52670 AS |
4187 | return -EACCES; |
4188 | } | |
d71962f3 PC |
4189 | err = check_helper_mem_access(env, regno, |
4190 | meta->map_ptr->key_size, false, | |
4191 | NULL); | |
2ea864c5 | 4192 | } else if (arg_type == ARG_PTR_TO_MAP_VALUE || |
6ac99e8f MKL |
4193 | (arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL && |
4194 | !register_is_null(reg)) || | |
2ea864c5 | 4195 | arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE) { |
17a52670 AS |
4196 | /* bpf_map_xxx(..., map_ptr, ..., value) call: |
4197 | * check [value, value + map->value_size) validity | |
4198 | */ | |
33ff9823 | 4199 | if (!meta->map_ptr) { |
17a52670 | 4200 | /* kernel subsystem misconfigured verifier */ |
61bd5218 | 4201 | verbose(env, "invalid map_ptr to access map->value\n"); |
17a52670 AS |
4202 | return -EACCES; |
4203 | } | |
2ea864c5 | 4204 | meta->raw_mode = (arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE); |
d71962f3 PC |
4205 | err = check_helper_mem_access(env, regno, |
4206 | meta->map_ptr->value_size, false, | |
2ea864c5 | 4207 | meta); |
c18f0b6a LB |
4208 | } else if (arg_type == ARG_PTR_TO_SPIN_LOCK) { |
4209 | if (meta->func_id == BPF_FUNC_spin_lock) { | |
4210 | if (process_spin_lock(env, regno, true)) | |
4211 | return -EACCES; | |
4212 | } else if (meta->func_id == BPF_FUNC_spin_unlock) { | |
4213 | if (process_spin_lock(env, regno, false)) | |
4214 | return -EACCES; | |
4215 | } else { | |
4216 | verbose(env, "verifier internal error\n"); | |
4217 | return -EFAULT; | |
4218 | } | |
a2bbe7cc LB |
4219 | } else if (arg_type_is_mem_ptr(arg_type)) { |
4220 | /* The access to this pointer is only checked when we hit the | |
4221 | * next is_mem_size argument below. | |
4222 | */ | |
4223 | meta->raw_mode = (arg_type == ARG_PTR_TO_UNINIT_MEM); | |
90133415 | 4224 | } else if (arg_type_is_mem_size(arg_type)) { |
39f19ebb | 4225 | bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO); |
17a52670 | 4226 | |
10060503 JF |
4227 | /* This is used to refine r0 return value bounds for helpers |
4228 | * that enforce this value as an upper bound on return values. | |
4229 | * See do_refine_retval_range() for helpers that can refine | |
4230 | * the return value. C type of helper is u32 so we pull register | |
4231 | * bound from umax_value however, if negative verifier errors | |
4232 | * out. Only upper bounds can be learned because retval is an | |
4233 | * int type and negative retvals are allowed. | |
849fa506 | 4234 | */ |
10060503 | 4235 | meta->msize_max_value = reg->umax_value; |
849fa506 | 4236 | |
f1174f77 EC |
4237 | /* The register is SCALAR_VALUE; the access check |
4238 | * happens using its boundaries. | |
06c1c049 | 4239 | */ |
f1174f77 | 4240 | if (!tnum_is_const(reg->var_off)) |
06c1c049 GB |
4241 | /* For unprivileged variable accesses, disable raw |
4242 | * mode so that the program is required to | |
4243 | * initialize all the memory that the helper could | |
4244 | * just partially fill up. | |
4245 | */ | |
4246 | meta = NULL; | |
4247 | ||
b03c9f9f | 4248 | if (reg->smin_value < 0) { |
61bd5218 | 4249 | verbose(env, "R%d min value is negative, either use unsigned or 'var &= const'\n", |
f1174f77 EC |
4250 | regno); |
4251 | return -EACCES; | |
4252 | } | |
06c1c049 | 4253 | |
b03c9f9f | 4254 | if (reg->umin_value == 0) { |
f1174f77 EC |
4255 | err = check_helper_mem_access(env, regno - 1, 0, |
4256 | zero_size_allowed, | |
4257 | meta); | |
06c1c049 GB |
4258 | if (err) |
4259 | return err; | |
06c1c049 | 4260 | } |
f1174f77 | 4261 | |
b03c9f9f | 4262 | if (reg->umax_value >= BPF_MAX_VAR_SIZ) { |
61bd5218 | 4263 | verbose(env, "R%d unbounded memory access, use 'var &= const' or 'if (var < const)'\n", |
f1174f77 EC |
4264 | regno); |
4265 | return -EACCES; | |
4266 | } | |
4267 | err = check_helper_mem_access(env, regno - 1, | |
b03c9f9f | 4268 | reg->umax_value, |
f1174f77 | 4269 | zero_size_allowed, meta); |
b5dc0163 AS |
4270 | if (!err) |
4271 | err = mark_chain_precision(env, regno); | |
457f4436 AN |
4272 | } else if (arg_type_is_alloc_size(arg_type)) { |
4273 | if (!tnum_is_const(reg->var_off)) { | |
4274 | verbose(env, "R%d unbounded size, use 'var &= const' or 'if (var < const)'\n", | |
4275 | regno); | |
4276 | return -EACCES; | |
4277 | } | |
4278 | meta->mem_size = reg->var_off.value; | |
57c3bb72 AI |
4279 | } else if (arg_type_is_int_ptr(arg_type)) { |
4280 | int size = int_ptr_type_to_size(arg_type); | |
4281 | ||
4282 | err = check_helper_mem_access(env, regno, size, false, meta); | |
4283 | if (err) | |
4284 | return err; | |
4285 | err = check_ptr_alignment(env, reg, 0, size, true); | |
17a52670 AS |
4286 | } |
4287 | ||
4288 | return err; | |
4289 | } | |
4290 | ||
0126240f LB |
4291 | static bool may_update_sockmap(struct bpf_verifier_env *env, int func_id) |
4292 | { | |
4293 | enum bpf_attach_type eatype = env->prog->expected_attach_type; | |
7e40781c | 4294 | enum bpf_prog_type type = resolve_prog_type(env->prog); |
0126240f LB |
4295 | |
4296 | if (func_id != BPF_FUNC_map_update_elem) | |
4297 | return false; | |
4298 | ||
4299 | /* It's not possible to get access to a locked struct sock in these | |
4300 | * contexts, so updating is safe. | |
4301 | */ | |
4302 | switch (type) { | |
4303 | case BPF_PROG_TYPE_TRACING: | |
4304 | if (eatype == BPF_TRACE_ITER) | |
4305 | return true; | |
4306 | break; | |
4307 | case BPF_PROG_TYPE_SOCKET_FILTER: | |
4308 | case BPF_PROG_TYPE_SCHED_CLS: | |
4309 | case BPF_PROG_TYPE_SCHED_ACT: | |
4310 | case BPF_PROG_TYPE_XDP: | |
4311 | case BPF_PROG_TYPE_SK_REUSEPORT: | |
4312 | case BPF_PROG_TYPE_FLOW_DISSECTOR: | |
4313 | case BPF_PROG_TYPE_SK_LOOKUP: | |
4314 | return true; | |
4315 | default: | |
4316 | break; | |
4317 | } | |
4318 | ||
4319 | verbose(env, "cannot update sockmap in this context\n"); | |
4320 | return false; | |
4321 | } | |
4322 | ||
e411901c MF |
4323 | static bool allow_tail_call_in_subprogs(struct bpf_verifier_env *env) |
4324 | { | |
4325 | return env->prog->jit_requested && IS_ENABLED(CONFIG_X86_64); | |
4326 | } | |
4327 | ||
61bd5218 JK |
4328 | static int check_map_func_compatibility(struct bpf_verifier_env *env, |
4329 | struct bpf_map *map, int func_id) | |
35578d79 | 4330 | { |
35578d79 KX |
4331 | if (!map) |
4332 | return 0; | |
4333 | ||
6aff67c8 AS |
4334 | /* We need a two way check, first is from map perspective ... */ |
4335 | switch (map->map_type) { | |
4336 | case BPF_MAP_TYPE_PROG_ARRAY: | |
4337 | if (func_id != BPF_FUNC_tail_call) | |
4338 | goto error; | |
4339 | break; | |
4340 | case BPF_MAP_TYPE_PERF_EVENT_ARRAY: | |
4341 | if (func_id != BPF_FUNC_perf_event_read && | |
908432ca | 4342 | func_id != BPF_FUNC_perf_event_output && |
a7658e1a | 4343 | func_id != BPF_FUNC_skb_output && |
d831ee84 EC |
4344 | func_id != BPF_FUNC_perf_event_read_value && |
4345 | func_id != BPF_FUNC_xdp_output) | |
6aff67c8 AS |
4346 | goto error; |
4347 | break; | |
457f4436 AN |
4348 | case BPF_MAP_TYPE_RINGBUF: |
4349 | if (func_id != BPF_FUNC_ringbuf_output && | |
4350 | func_id != BPF_FUNC_ringbuf_reserve && | |
4351 | func_id != BPF_FUNC_ringbuf_submit && | |
4352 | func_id != BPF_FUNC_ringbuf_discard && | |
4353 | func_id != BPF_FUNC_ringbuf_query) | |
4354 | goto error; | |
4355 | break; | |
6aff67c8 AS |
4356 | case BPF_MAP_TYPE_STACK_TRACE: |
4357 | if (func_id != BPF_FUNC_get_stackid) | |
4358 | goto error; | |
4359 | break; | |
4ed8ec52 | 4360 | case BPF_MAP_TYPE_CGROUP_ARRAY: |
60747ef4 | 4361 | if (func_id != BPF_FUNC_skb_under_cgroup && |
60d20f91 | 4362 | func_id != BPF_FUNC_current_task_under_cgroup) |
4a482f34 MKL |
4363 | goto error; |
4364 | break; | |
cd339431 | 4365 | case BPF_MAP_TYPE_CGROUP_STORAGE: |
b741f163 | 4366 | case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE: |
cd339431 RG |
4367 | if (func_id != BPF_FUNC_get_local_storage) |
4368 | goto error; | |
4369 | break; | |
546ac1ff | 4370 | case BPF_MAP_TYPE_DEVMAP: |
6f9d451a | 4371 | case BPF_MAP_TYPE_DEVMAP_HASH: |
0cdbb4b0 THJ |
4372 | if (func_id != BPF_FUNC_redirect_map && |
4373 | func_id != BPF_FUNC_map_lookup_elem) | |
546ac1ff JF |
4374 | goto error; |
4375 | break; | |
fbfc504a BT |
4376 | /* Restrict bpf side of cpumap and xskmap, open when use-cases |
4377 | * appear. | |
4378 | */ | |
6710e112 JDB |
4379 | case BPF_MAP_TYPE_CPUMAP: |
4380 | if (func_id != BPF_FUNC_redirect_map) | |
4381 | goto error; | |
4382 | break; | |
fada7fdc JL |
4383 | case BPF_MAP_TYPE_XSKMAP: |
4384 | if (func_id != BPF_FUNC_redirect_map && | |
4385 | func_id != BPF_FUNC_map_lookup_elem) | |
4386 | goto error; | |
4387 | break; | |
56f668df | 4388 | case BPF_MAP_TYPE_ARRAY_OF_MAPS: |
bcc6b1b7 | 4389 | case BPF_MAP_TYPE_HASH_OF_MAPS: |
56f668df MKL |
4390 | if (func_id != BPF_FUNC_map_lookup_elem) |
4391 | goto error; | |
16a43625 | 4392 | break; |
174a79ff JF |
4393 | case BPF_MAP_TYPE_SOCKMAP: |
4394 | if (func_id != BPF_FUNC_sk_redirect_map && | |
4395 | func_id != BPF_FUNC_sock_map_update && | |
4f738adb | 4396 | func_id != BPF_FUNC_map_delete_elem && |
9fed9000 | 4397 | func_id != BPF_FUNC_msg_redirect_map && |
64d85290 | 4398 | func_id != BPF_FUNC_sk_select_reuseport && |
0126240f LB |
4399 | func_id != BPF_FUNC_map_lookup_elem && |
4400 | !may_update_sockmap(env, func_id)) | |
174a79ff JF |
4401 | goto error; |
4402 | break; | |
81110384 JF |
4403 | case BPF_MAP_TYPE_SOCKHASH: |
4404 | if (func_id != BPF_FUNC_sk_redirect_hash && | |
4405 | func_id != BPF_FUNC_sock_hash_update && | |
4406 | func_id != BPF_FUNC_map_delete_elem && | |
9fed9000 | 4407 | func_id != BPF_FUNC_msg_redirect_hash && |
64d85290 | 4408 | func_id != BPF_FUNC_sk_select_reuseport && |
0126240f LB |
4409 | func_id != BPF_FUNC_map_lookup_elem && |
4410 | !may_update_sockmap(env, func_id)) | |
81110384 JF |
4411 | goto error; |
4412 | break; | |
2dbb9b9e MKL |
4413 | case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY: |
4414 | if (func_id != BPF_FUNC_sk_select_reuseport) | |
4415 | goto error; | |
4416 | break; | |
f1a2e44a MV |
4417 | case BPF_MAP_TYPE_QUEUE: |
4418 | case BPF_MAP_TYPE_STACK: | |
4419 | if (func_id != BPF_FUNC_map_peek_elem && | |
4420 | func_id != BPF_FUNC_map_pop_elem && | |
4421 | func_id != BPF_FUNC_map_push_elem) | |
4422 | goto error; | |
4423 | break; | |
6ac99e8f MKL |
4424 | case BPF_MAP_TYPE_SK_STORAGE: |
4425 | if (func_id != BPF_FUNC_sk_storage_get && | |
4426 | func_id != BPF_FUNC_sk_storage_delete) | |
4427 | goto error; | |
4428 | break; | |
8ea63684 KS |
4429 | case BPF_MAP_TYPE_INODE_STORAGE: |
4430 | if (func_id != BPF_FUNC_inode_storage_get && | |
4431 | func_id != BPF_FUNC_inode_storage_delete) | |
4432 | goto error; | |
4433 | break; | |
6aff67c8 AS |
4434 | default: |
4435 | break; | |
4436 | } | |
4437 | ||
4438 | /* ... and second from the function itself. */ | |
4439 | switch (func_id) { | |
4440 | case BPF_FUNC_tail_call: | |
4441 | if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY) | |
4442 | goto error; | |
e411901c MF |
4443 | if (env->subprog_cnt > 1 && !allow_tail_call_in_subprogs(env)) { |
4444 | verbose(env, "tail_calls are not allowed in non-JITed programs with bpf-to-bpf calls\n"); | |
f4d7e40a AS |
4445 | return -EINVAL; |
4446 | } | |
6aff67c8 AS |
4447 | break; |
4448 | case BPF_FUNC_perf_event_read: | |
4449 | case BPF_FUNC_perf_event_output: | |
908432ca | 4450 | case BPF_FUNC_perf_event_read_value: |
a7658e1a | 4451 | case BPF_FUNC_skb_output: |
d831ee84 | 4452 | case BPF_FUNC_xdp_output: |
6aff67c8 AS |
4453 | if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) |
4454 | goto error; | |
4455 | break; | |
4456 | case BPF_FUNC_get_stackid: | |
4457 | if (map->map_type != BPF_MAP_TYPE_STACK_TRACE) | |
4458 | goto error; | |
4459 | break; | |
60d20f91 | 4460 | case BPF_FUNC_current_task_under_cgroup: |
747ea55e | 4461 | case BPF_FUNC_skb_under_cgroup: |
4a482f34 MKL |
4462 | if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY) |
4463 | goto error; | |
4464 | break; | |
97f91a7c | 4465 | case BPF_FUNC_redirect_map: |
9c270af3 | 4466 | if (map->map_type != BPF_MAP_TYPE_DEVMAP && |
6f9d451a | 4467 | map->map_type != BPF_MAP_TYPE_DEVMAP_HASH && |
fbfc504a BT |
4468 | map->map_type != BPF_MAP_TYPE_CPUMAP && |
4469 | map->map_type != BPF_MAP_TYPE_XSKMAP) | |
97f91a7c JF |
4470 | goto error; |
4471 | break; | |
174a79ff | 4472 | case BPF_FUNC_sk_redirect_map: |
4f738adb | 4473 | case BPF_FUNC_msg_redirect_map: |
81110384 | 4474 | case BPF_FUNC_sock_map_update: |
174a79ff JF |
4475 | if (map->map_type != BPF_MAP_TYPE_SOCKMAP) |
4476 | goto error; | |
4477 | break; | |
81110384 JF |
4478 | case BPF_FUNC_sk_redirect_hash: |
4479 | case BPF_FUNC_msg_redirect_hash: | |
4480 | case BPF_FUNC_sock_hash_update: | |
4481 | if (map->map_type != BPF_MAP_TYPE_SOCKHASH) | |
174a79ff JF |
4482 | goto error; |
4483 | break; | |
cd339431 | 4484 | case BPF_FUNC_get_local_storage: |
b741f163 RG |
4485 | if (map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE && |
4486 | map->map_type != BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) | |
cd339431 RG |
4487 | goto error; |
4488 | break; | |
2dbb9b9e | 4489 | case BPF_FUNC_sk_select_reuseport: |
9fed9000 JS |
4490 | if (map->map_type != BPF_MAP_TYPE_REUSEPORT_SOCKARRAY && |
4491 | map->map_type != BPF_MAP_TYPE_SOCKMAP && | |
4492 | map->map_type != BPF_MAP_TYPE_SOCKHASH) | |
2dbb9b9e MKL |
4493 | goto error; |
4494 | break; | |
f1a2e44a MV |
4495 | case BPF_FUNC_map_peek_elem: |
4496 | case BPF_FUNC_map_pop_elem: | |
4497 | case BPF_FUNC_map_push_elem: | |
4498 | if (map->map_type != BPF_MAP_TYPE_QUEUE && | |
4499 | map->map_type != BPF_MAP_TYPE_STACK) | |
4500 | goto error; | |
4501 | break; | |
6ac99e8f MKL |
4502 | case BPF_FUNC_sk_storage_get: |
4503 | case BPF_FUNC_sk_storage_delete: | |
4504 | if (map->map_type != BPF_MAP_TYPE_SK_STORAGE) | |
4505 | goto error; | |
4506 | break; | |
8ea63684 KS |
4507 | case BPF_FUNC_inode_storage_get: |
4508 | case BPF_FUNC_inode_storage_delete: | |
4509 | if (map->map_type != BPF_MAP_TYPE_INODE_STORAGE) | |
4510 | goto error; | |
4511 | break; | |
6aff67c8 AS |
4512 | default: |
4513 | break; | |
35578d79 KX |
4514 | } |
4515 | ||
4516 | return 0; | |
6aff67c8 | 4517 | error: |
61bd5218 | 4518 | verbose(env, "cannot pass map_type %d into func %s#%d\n", |
ebb676da | 4519 | map->map_type, func_id_name(func_id), func_id); |
6aff67c8 | 4520 | return -EINVAL; |
35578d79 KX |
4521 | } |
4522 | ||
90133415 | 4523 | static bool check_raw_mode_ok(const struct bpf_func_proto *fn) |
435faee1 DB |
4524 | { |
4525 | int count = 0; | |
4526 | ||
39f19ebb | 4527 | if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 4528 | count++; |
39f19ebb | 4529 | if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 4530 | count++; |
39f19ebb | 4531 | if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 4532 | count++; |
39f19ebb | 4533 | if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 4534 | count++; |
39f19ebb | 4535 | if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 DB |
4536 | count++; |
4537 | ||
90133415 DB |
4538 | /* We only support one arg being in raw mode at the moment, |
4539 | * which is sufficient for the helper functions we have | |
4540 | * right now. | |
4541 | */ | |
4542 | return count <= 1; | |
4543 | } | |
4544 | ||
4545 | static bool check_args_pair_invalid(enum bpf_arg_type arg_curr, | |
4546 | enum bpf_arg_type arg_next) | |
4547 | { | |
4548 | return (arg_type_is_mem_ptr(arg_curr) && | |
4549 | !arg_type_is_mem_size(arg_next)) || | |
4550 | (!arg_type_is_mem_ptr(arg_curr) && | |
4551 | arg_type_is_mem_size(arg_next)); | |
4552 | } | |
4553 | ||
4554 | static bool check_arg_pair_ok(const struct bpf_func_proto *fn) | |
4555 | { | |
4556 | /* bpf_xxx(..., buf, len) call will access 'len' | |
4557 | * bytes from memory 'buf'. Both arg types need | |
4558 | * to be paired, so make sure there's no buggy | |
4559 | * helper function specification. | |
4560 | */ | |
4561 | if (arg_type_is_mem_size(fn->arg1_type) || | |
4562 | arg_type_is_mem_ptr(fn->arg5_type) || | |
4563 | check_args_pair_invalid(fn->arg1_type, fn->arg2_type) || | |
4564 | check_args_pair_invalid(fn->arg2_type, fn->arg3_type) || | |
4565 | check_args_pair_invalid(fn->arg3_type, fn->arg4_type) || | |
4566 | check_args_pair_invalid(fn->arg4_type, fn->arg5_type)) | |
4567 | return false; | |
4568 | ||
4569 | return true; | |
4570 | } | |
4571 | ||
1b986589 | 4572 | static bool check_refcount_ok(const struct bpf_func_proto *fn, int func_id) |
fd978bf7 JS |
4573 | { |
4574 | int count = 0; | |
4575 | ||
1b986589 | 4576 | if (arg_type_may_be_refcounted(fn->arg1_type)) |
fd978bf7 | 4577 | count++; |
1b986589 | 4578 | if (arg_type_may_be_refcounted(fn->arg2_type)) |
fd978bf7 | 4579 | count++; |
1b986589 | 4580 | if (arg_type_may_be_refcounted(fn->arg3_type)) |
fd978bf7 | 4581 | count++; |
1b986589 | 4582 | if (arg_type_may_be_refcounted(fn->arg4_type)) |
fd978bf7 | 4583 | count++; |
1b986589 | 4584 | if (arg_type_may_be_refcounted(fn->arg5_type)) |
fd978bf7 JS |
4585 | count++; |
4586 | ||
1b986589 MKL |
4587 | /* A reference acquiring function cannot acquire |
4588 | * another refcounted ptr. | |
4589 | */ | |
64d85290 | 4590 | if (may_be_acquire_function(func_id) && count) |
1b986589 MKL |
4591 | return false; |
4592 | ||
fd978bf7 JS |
4593 | /* We only support one arg being unreferenced at the moment, |
4594 | * which is sufficient for the helper functions we have right now. | |
4595 | */ | |
4596 | return count <= 1; | |
4597 | } | |
4598 | ||
9436ef6e LB |
4599 | static bool check_btf_id_ok(const struct bpf_func_proto *fn) |
4600 | { | |
4601 | int i; | |
4602 | ||
1df8f55a | 4603 | for (i = 0; i < ARRAY_SIZE(fn->arg_type); i++) { |
9436ef6e LB |
4604 | if (fn->arg_type[i] == ARG_PTR_TO_BTF_ID && !fn->arg_btf_id[i]) |
4605 | return false; | |
4606 | ||
1df8f55a MKL |
4607 | if (fn->arg_type[i] != ARG_PTR_TO_BTF_ID && fn->arg_btf_id[i]) |
4608 | return false; | |
4609 | } | |
4610 | ||
9436ef6e LB |
4611 | return true; |
4612 | } | |
4613 | ||
1b986589 | 4614 | static int check_func_proto(const struct bpf_func_proto *fn, int func_id) |
90133415 DB |
4615 | { |
4616 | return check_raw_mode_ok(fn) && | |
fd978bf7 | 4617 | check_arg_pair_ok(fn) && |
9436ef6e | 4618 | check_btf_id_ok(fn) && |
1b986589 | 4619 | check_refcount_ok(fn, func_id) ? 0 : -EINVAL; |
435faee1 DB |
4620 | } |
4621 | ||
de8f3a83 DB |
4622 | /* Packet data might have moved, any old PTR_TO_PACKET[_META,_END] |
4623 | * are now invalid, so turn them into unknown SCALAR_VALUE. | |
f1174f77 | 4624 | */ |
f4d7e40a AS |
4625 | static void __clear_all_pkt_pointers(struct bpf_verifier_env *env, |
4626 | struct bpf_func_state *state) | |
969bf05e | 4627 | { |
58e2af8b | 4628 | struct bpf_reg_state *regs = state->regs, *reg; |
969bf05e AS |
4629 | int i; |
4630 | ||
4631 | for (i = 0; i < MAX_BPF_REG; i++) | |
de8f3a83 | 4632 | if (reg_is_pkt_pointer_any(®s[i])) |
61bd5218 | 4633 | mark_reg_unknown(env, regs, i); |
969bf05e | 4634 | |
f3709f69 JS |
4635 | bpf_for_each_spilled_reg(i, state, reg) { |
4636 | if (!reg) | |
969bf05e | 4637 | continue; |
de8f3a83 | 4638 | if (reg_is_pkt_pointer_any(reg)) |
f54c7898 | 4639 | __mark_reg_unknown(env, reg); |
969bf05e AS |
4640 | } |
4641 | } | |
4642 | ||
f4d7e40a AS |
4643 | static void clear_all_pkt_pointers(struct bpf_verifier_env *env) |
4644 | { | |
4645 | struct bpf_verifier_state *vstate = env->cur_state; | |
4646 | int i; | |
4647 | ||
4648 | for (i = 0; i <= vstate->curframe; i++) | |
4649 | __clear_all_pkt_pointers(env, vstate->frame[i]); | |
4650 | } | |
4651 | ||
fd978bf7 | 4652 | static void release_reg_references(struct bpf_verifier_env *env, |
1b986589 MKL |
4653 | struct bpf_func_state *state, |
4654 | int ref_obj_id) | |
fd978bf7 JS |
4655 | { |
4656 | struct bpf_reg_state *regs = state->regs, *reg; | |
4657 | int i; | |
4658 | ||
4659 | for (i = 0; i < MAX_BPF_REG; i++) | |
1b986589 | 4660 | if (regs[i].ref_obj_id == ref_obj_id) |
fd978bf7 JS |
4661 | mark_reg_unknown(env, regs, i); |
4662 | ||
4663 | bpf_for_each_spilled_reg(i, state, reg) { | |
4664 | if (!reg) | |
4665 | continue; | |
1b986589 | 4666 | if (reg->ref_obj_id == ref_obj_id) |
f54c7898 | 4667 | __mark_reg_unknown(env, reg); |
fd978bf7 JS |
4668 | } |
4669 | } | |
4670 | ||
4671 | /* The pointer with the specified id has released its reference to kernel | |
4672 | * resources. Identify all copies of the same pointer and clear the reference. | |
4673 | */ | |
4674 | static int release_reference(struct bpf_verifier_env *env, | |
1b986589 | 4675 | int ref_obj_id) |
fd978bf7 JS |
4676 | { |
4677 | struct bpf_verifier_state *vstate = env->cur_state; | |
1b986589 | 4678 | int err; |
fd978bf7 JS |
4679 | int i; |
4680 | ||
1b986589 MKL |
4681 | err = release_reference_state(cur_func(env), ref_obj_id); |
4682 | if (err) | |
4683 | return err; | |
4684 | ||
fd978bf7 | 4685 | for (i = 0; i <= vstate->curframe; i++) |
1b986589 | 4686 | release_reg_references(env, vstate->frame[i], ref_obj_id); |
fd978bf7 | 4687 | |
1b986589 | 4688 | return 0; |
fd978bf7 JS |
4689 | } |
4690 | ||
51c39bb1 AS |
4691 | static void clear_caller_saved_regs(struct bpf_verifier_env *env, |
4692 | struct bpf_reg_state *regs) | |
4693 | { | |
4694 | int i; | |
4695 | ||
4696 | /* after the call registers r0 - r5 were scratched */ | |
4697 | for (i = 0; i < CALLER_SAVED_REGS; i++) { | |
4698 | mark_reg_not_init(env, regs, caller_saved[i]); | |
4699 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); | |
4700 | } | |
4701 | } | |
4702 | ||
f4d7e40a AS |
4703 | static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, |
4704 | int *insn_idx) | |
4705 | { | |
4706 | struct bpf_verifier_state *state = env->cur_state; | |
51c39bb1 | 4707 | struct bpf_func_info_aux *func_info_aux; |
f4d7e40a | 4708 | struct bpf_func_state *caller, *callee; |
fd978bf7 | 4709 | int i, err, subprog, target_insn; |
51c39bb1 | 4710 | bool is_global = false; |
f4d7e40a | 4711 | |
aada9ce6 | 4712 | if (state->curframe + 1 >= MAX_CALL_FRAMES) { |
f4d7e40a | 4713 | verbose(env, "the call stack of %d frames is too deep\n", |
aada9ce6 | 4714 | state->curframe + 2); |
f4d7e40a AS |
4715 | return -E2BIG; |
4716 | } | |
4717 | ||
4718 | target_insn = *insn_idx + insn->imm; | |
4719 | subprog = find_subprog(env, target_insn + 1); | |
4720 | if (subprog < 0) { | |
4721 | verbose(env, "verifier bug. No program starts at insn %d\n", | |
4722 | target_insn + 1); | |
4723 | return -EFAULT; | |
4724 | } | |
4725 | ||
4726 | caller = state->frame[state->curframe]; | |
4727 | if (state->frame[state->curframe + 1]) { | |
4728 | verbose(env, "verifier bug. Frame %d already allocated\n", | |
4729 | state->curframe + 1); | |
4730 | return -EFAULT; | |
4731 | } | |
4732 | ||
51c39bb1 AS |
4733 | func_info_aux = env->prog->aux->func_info_aux; |
4734 | if (func_info_aux) | |
4735 | is_global = func_info_aux[subprog].linkage == BTF_FUNC_GLOBAL; | |
4736 | err = btf_check_func_arg_match(env, subprog, caller->regs); | |
4737 | if (err == -EFAULT) | |
4738 | return err; | |
4739 | if (is_global) { | |
4740 | if (err) { | |
4741 | verbose(env, "Caller passes invalid args into func#%d\n", | |
4742 | subprog); | |
4743 | return err; | |
4744 | } else { | |
4745 | if (env->log.level & BPF_LOG_LEVEL) | |
4746 | verbose(env, | |
4747 | "Func#%d is global and valid. Skipping.\n", | |
4748 | subprog); | |
4749 | clear_caller_saved_regs(env, caller->regs); | |
4750 | ||
4751 | /* All global functions return SCALAR_VALUE */ | |
4752 | mark_reg_unknown(env, caller->regs, BPF_REG_0); | |
4753 | ||
4754 | /* continue with next insn after call */ | |
4755 | return 0; | |
4756 | } | |
4757 | } | |
4758 | ||
f4d7e40a AS |
4759 | callee = kzalloc(sizeof(*callee), GFP_KERNEL); |
4760 | if (!callee) | |
4761 | return -ENOMEM; | |
4762 | state->frame[state->curframe + 1] = callee; | |
4763 | ||
4764 | /* callee cannot access r0, r6 - r9 for reading and has to write | |
4765 | * into its own stack before reading from it. | |
4766 | * callee can read/write into caller's stack | |
4767 | */ | |
4768 | init_func_state(env, callee, | |
4769 | /* remember the callsite, it will be used by bpf_exit */ | |
4770 | *insn_idx /* callsite */, | |
4771 | state->curframe + 1 /* frameno within this callchain */, | |
f910cefa | 4772 | subprog /* subprog number within this prog */); |
f4d7e40a | 4773 | |
fd978bf7 JS |
4774 | /* Transfer references to the callee */ |
4775 | err = transfer_reference_state(callee, caller); | |
4776 | if (err) | |
4777 | return err; | |
4778 | ||
679c782d EC |
4779 | /* copy r1 - r5 args that callee can access. The copy includes parent |
4780 | * pointers, which connects us up to the liveness chain | |
4781 | */ | |
f4d7e40a AS |
4782 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) |
4783 | callee->regs[i] = caller->regs[i]; | |
4784 | ||
51c39bb1 | 4785 | clear_caller_saved_regs(env, caller->regs); |
f4d7e40a AS |
4786 | |
4787 | /* only increment it after check_reg_arg() finished */ | |
4788 | state->curframe++; | |
4789 | ||
4790 | /* and go analyze first insn of the callee */ | |
4791 | *insn_idx = target_insn; | |
4792 | ||
06ee7115 | 4793 | if (env->log.level & BPF_LOG_LEVEL) { |
f4d7e40a AS |
4794 | verbose(env, "caller:\n"); |
4795 | print_verifier_state(env, caller); | |
4796 | verbose(env, "callee:\n"); | |
4797 | print_verifier_state(env, callee); | |
4798 | } | |
4799 | return 0; | |
4800 | } | |
4801 | ||
4802 | static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx) | |
4803 | { | |
4804 | struct bpf_verifier_state *state = env->cur_state; | |
4805 | struct bpf_func_state *caller, *callee; | |
4806 | struct bpf_reg_state *r0; | |
fd978bf7 | 4807 | int err; |
f4d7e40a AS |
4808 | |
4809 | callee = state->frame[state->curframe]; | |
4810 | r0 = &callee->regs[BPF_REG_0]; | |
4811 | if (r0->type == PTR_TO_STACK) { | |
4812 | /* technically it's ok to return caller's stack pointer | |
4813 | * (or caller's caller's pointer) back to the caller, | |
4814 | * since these pointers are valid. Only current stack | |
4815 | * pointer will be invalid as soon as function exits, | |
4816 | * but let's be conservative | |
4817 | */ | |
4818 | verbose(env, "cannot return stack pointer to the caller\n"); | |
4819 | return -EINVAL; | |
4820 | } | |
4821 | ||
4822 | state->curframe--; | |
4823 | caller = state->frame[state->curframe]; | |
4824 | /* return to the caller whatever r0 had in the callee */ | |
4825 | caller->regs[BPF_REG_0] = *r0; | |
4826 | ||
fd978bf7 JS |
4827 | /* Transfer references to the caller */ |
4828 | err = transfer_reference_state(caller, callee); | |
4829 | if (err) | |
4830 | return err; | |
4831 | ||
f4d7e40a | 4832 | *insn_idx = callee->callsite + 1; |
06ee7115 | 4833 | if (env->log.level & BPF_LOG_LEVEL) { |
f4d7e40a AS |
4834 | verbose(env, "returning from callee:\n"); |
4835 | print_verifier_state(env, callee); | |
4836 | verbose(env, "to caller at %d:\n", *insn_idx); | |
4837 | print_verifier_state(env, caller); | |
4838 | } | |
4839 | /* clear everything in the callee */ | |
4840 | free_func_state(callee); | |
4841 | state->frame[state->curframe + 1] = NULL; | |
4842 | return 0; | |
4843 | } | |
4844 | ||
849fa506 YS |
4845 | static void do_refine_retval_range(struct bpf_reg_state *regs, int ret_type, |
4846 | int func_id, | |
4847 | struct bpf_call_arg_meta *meta) | |
4848 | { | |
4849 | struct bpf_reg_state *ret_reg = ®s[BPF_REG_0]; | |
4850 | ||
4851 | if (ret_type != RET_INTEGER || | |
4852 | (func_id != BPF_FUNC_get_stack && | |
47cc0ed5 DB |
4853 | func_id != BPF_FUNC_probe_read_str && |
4854 | func_id != BPF_FUNC_probe_read_kernel_str && | |
4855 | func_id != BPF_FUNC_probe_read_user_str)) | |
849fa506 YS |
4856 | return; |
4857 | ||
10060503 | 4858 | ret_reg->smax_value = meta->msize_max_value; |
fa123ac0 | 4859 | ret_reg->s32_max_value = meta->msize_max_value; |
849fa506 YS |
4860 | __reg_deduce_bounds(ret_reg); |
4861 | __reg_bound_offset(ret_reg); | |
10060503 | 4862 | __update_reg_bounds(ret_reg); |
849fa506 YS |
4863 | } |
4864 | ||
c93552c4 DB |
4865 | static int |
4866 | record_func_map(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, | |
4867 | int func_id, int insn_idx) | |
4868 | { | |
4869 | struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; | |
591fe988 | 4870 | struct bpf_map *map = meta->map_ptr; |
c93552c4 DB |
4871 | |
4872 | if (func_id != BPF_FUNC_tail_call && | |
09772d92 DB |
4873 | func_id != BPF_FUNC_map_lookup_elem && |
4874 | func_id != BPF_FUNC_map_update_elem && | |
f1a2e44a MV |
4875 | func_id != BPF_FUNC_map_delete_elem && |
4876 | func_id != BPF_FUNC_map_push_elem && | |
4877 | func_id != BPF_FUNC_map_pop_elem && | |
4878 | func_id != BPF_FUNC_map_peek_elem) | |
c93552c4 | 4879 | return 0; |
09772d92 | 4880 | |
591fe988 | 4881 | if (map == NULL) { |
c93552c4 DB |
4882 | verbose(env, "kernel subsystem misconfigured verifier\n"); |
4883 | return -EINVAL; | |
4884 | } | |
4885 | ||
591fe988 DB |
4886 | /* In case of read-only, some additional restrictions |
4887 | * need to be applied in order to prevent altering the | |
4888 | * state of the map from program side. | |
4889 | */ | |
4890 | if ((map->map_flags & BPF_F_RDONLY_PROG) && | |
4891 | (func_id == BPF_FUNC_map_delete_elem || | |
4892 | func_id == BPF_FUNC_map_update_elem || | |
4893 | func_id == BPF_FUNC_map_push_elem || | |
4894 | func_id == BPF_FUNC_map_pop_elem)) { | |
4895 | verbose(env, "write into map forbidden\n"); | |
4896 | return -EACCES; | |
4897 | } | |
4898 | ||
d2e4c1e6 | 4899 | if (!BPF_MAP_PTR(aux->map_ptr_state)) |
c93552c4 | 4900 | bpf_map_ptr_store(aux, meta->map_ptr, |
2c78ee89 | 4901 | !meta->map_ptr->bypass_spec_v1); |
d2e4c1e6 | 4902 | else if (BPF_MAP_PTR(aux->map_ptr_state) != meta->map_ptr) |
c93552c4 | 4903 | bpf_map_ptr_store(aux, BPF_MAP_PTR_POISON, |
2c78ee89 | 4904 | !meta->map_ptr->bypass_spec_v1); |
c93552c4 DB |
4905 | return 0; |
4906 | } | |
4907 | ||
d2e4c1e6 DB |
4908 | static int |
4909 | record_func_key(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, | |
4910 | int func_id, int insn_idx) | |
4911 | { | |
4912 | struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; | |
4913 | struct bpf_reg_state *regs = cur_regs(env), *reg; | |
4914 | struct bpf_map *map = meta->map_ptr; | |
4915 | struct tnum range; | |
4916 | u64 val; | |
cc52d914 | 4917 | int err; |
d2e4c1e6 DB |
4918 | |
4919 | if (func_id != BPF_FUNC_tail_call) | |
4920 | return 0; | |
4921 | if (!map || map->map_type != BPF_MAP_TYPE_PROG_ARRAY) { | |
4922 | verbose(env, "kernel subsystem misconfigured verifier\n"); | |
4923 | return -EINVAL; | |
4924 | } | |
4925 | ||
4926 | range = tnum_range(0, map->max_entries - 1); | |
4927 | reg = ®s[BPF_REG_3]; | |
4928 | ||
4929 | if (!register_is_const(reg) || !tnum_in(range, reg->var_off)) { | |
4930 | bpf_map_key_store(aux, BPF_MAP_KEY_POISON); | |
4931 | return 0; | |
4932 | } | |
4933 | ||
cc52d914 DB |
4934 | err = mark_chain_precision(env, BPF_REG_3); |
4935 | if (err) | |
4936 | return err; | |
4937 | ||
d2e4c1e6 DB |
4938 | val = reg->var_off.value; |
4939 | if (bpf_map_key_unseen(aux)) | |
4940 | bpf_map_key_store(aux, val); | |
4941 | else if (!bpf_map_key_poisoned(aux) && | |
4942 | bpf_map_key_immediate(aux) != val) | |
4943 | bpf_map_key_store(aux, BPF_MAP_KEY_POISON); | |
4944 | return 0; | |
4945 | } | |
4946 | ||
fd978bf7 JS |
4947 | static int check_reference_leak(struct bpf_verifier_env *env) |
4948 | { | |
4949 | struct bpf_func_state *state = cur_func(env); | |
4950 | int i; | |
4951 | ||
4952 | for (i = 0; i < state->acquired_refs; i++) { | |
4953 | verbose(env, "Unreleased reference id=%d alloc_insn=%d\n", | |
4954 | state->refs[i].id, state->refs[i].insn_idx); | |
4955 | } | |
4956 | return state->acquired_refs ? -EINVAL : 0; | |
4957 | } | |
4958 | ||
f4d7e40a | 4959 | static int check_helper_call(struct bpf_verifier_env *env, int func_id, int insn_idx) |
17a52670 | 4960 | { |
17a52670 | 4961 | const struct bpf_func_proto *fn = NULL; |
638f5b90 | 4962 | struct bpf_reg_state *regs; |
33ff9823 | 4963 | struct bpf_call_arg_meta meta; |
969bf05e | 4964 | bool changes_data; |
17a52670 AS |
4965 | int i, err; |
4966 | ||
4967 | /* find function prototype */ | |
4968 | if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) { | |
61bd5218 JK |
4969 | verbose(env, "invalid func %s#%d\n", func_id_name(func_id), |
4970 | func_id); | |
17a52670 AS |
4971 | return -EINVAL; |
4972 | } | |
4973 | ||
00176a34 | 4974 | if (env->ops->get_func_proto) |
5e43f899 | 4975 | fn = env->ops->get_func_proto(func_id, env->prog); |
17a52670 | 4976 | if (!fn) { |
61bd5218 JK |
4977 | verbose(env, "unknown func %s#%d\n", func_id_name(func_id), |
4978 | func_id); | |
17a52670 AS |
4979 | return -EINVAL; |
4980 | } | |
4981 | ||
4982 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ | |
24701ece | 4983 | if (!env->prog->gpl_compatible && fn->gpl_only) { |
3fe2867c | 4984 | verbose(env, "cannot call GPL-restricted function from non-GPL compatible program\n"); |
17a52670 AS |
4985 | return -EINVAL; |
4986 | } | |
4987 | ||
eae2e83e JO |
4988 | if (fn->allowed && !fn->allowed(env->prog)) { |
4989 | verbose(env, "helper call is not allowed in probe\n"); | |
4990 | return -EINVAL; | |
4991 | } | |
4992 | ||
04514d13 | 4993 | /* With LD_ABS/IND some JITs save/restore skb from r1. */ |
17bedab2 | 4994 | changes_data = bpf_helper_changes_pkt_data(fn->func); |
04514d13 DB |
4995 | if (changes_data && fn->arg1_type != ARG_PTR_TO_CTX) { |
4996 | verbose(env, "kernel subsystem misconfigured func %s#%d: r1 != ctx\n", | |
4997 | func_id_name(func_id), func_id); | |
4998 | return -EINVAL; | |
4999 | } | |
969bf05e | 5000 | |
33ff9823 | 5001 | memset(&meta, 0, sizeof(meta)); |
36bbef52 | 5002 | meta.pkt_access = fn->pkt_access; |
33ff9823 | 5003 | |
1b986589 | 5004 | err = check_func_proto(fn, func_id); |
435faee1 | 5005 | if (err) { |
61bd5218 | 5006 | verbose(env, "kernel subsystem misconfigured func %s#%d\n", |
ebb676da | 5007 | func_id_name(func_id), func_id); |
435faee1 DB |
5008 | return err; |
5009 | } | |
5010 | ||
d83525ca | 5011 | meta.func_id = func_id; |
17a52670 | 5012 | /* check args */ |
a7658e1a | 5013 | for (i = 0; i < 5; i++) { |
af7ec138 | 5014 | err = check_func_arg(env, i, &meta, fn); |
a7658e1a AS |
5015 | if (err) |
5016 | return err; | |
5017 | } | |
17a52670 | 5018 | |
c93552c4 DB |
5019 | err = record_func_map(env, &meta, func_id, insn_idx); |
5020 | if (err) | |
5021 | return err; | |
5022 | ||
d2e4c1e6 DB |
5023 | err = record_func_key(env, &meta, func_id, insn_idx); |
5024 | if (err) | |
5025 | return err; | |
5026 | ||
435faee1 DB |
5027 | /* Mark slots with STACK_MISC in case of raw mode, stack offset |
5028 | * is inferred from register state. | |
5029 | */ | |
5030 | for (i = 0; i < meta.access_size; i++) { | |
ca369602 DB |
5031 | err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B, |
5032 | BPF_WRITE, -1, false); | |
435faee1 DB |
5033 | if (err) |
5034 | return err; | |
5035 | } | |
5036 | ||
fd978bf7 JS |
5037 | if (func_id == BPF_FUNC_tail_call) { |
5038 | err = check_reference_leak(env); | |
5039 | if (err) { | |
5040 | verbose(env, "tail_call would lead to reference leak\n"); | |
5041 | return err; | |
5042 | } | |
5043 | } else if (is_release_function(func_id)) { | |
1b986589 | 5044 | err = release_reference(env, meta.ref_obj_id); |
46f8bc92 MKL |
5045 | if (err) { |
5046 | verbose(env, "func %s#%d reference has not been acquired before\n", | |
5047 | func_id_name(func_id), func_id); | |
fd978bf7 | 5048 | return err; |
46f8bc92 | 5049 | } |
fd978bf7 JS |
5050 | } |
5051 | ||
638f5b90 | 5052 | regs = cur_regs(env); |
cd339431 RG |
5053 | |
5054 | /* check that flags argument in get_local_storage(map, flags) is 0, | |
5055 | * this is required because get_local_storage() can't return an error. | |
5056 | */ | |
5057 | if (func_id == BPF_FUNC_get_local_storage && | |
5058 | !register_is_null(®s[BPF_REG_2])) { | |
5059 | verbose(env, "get_local_storage() doesn't support non-zero flags\n"); | |
5060 | return -EINVAL; | |
5061 | } | |
5062 | ||
17a52670 | 5063 | /* reset caller saved regs */ |
dc503a8a | 5064 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
61bd5218 | 5065 | mark_reg_not_init(env, regs, caller_saved[i]); |
dc503a8a EC |
5066 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
5067 | } | |
17a52670 | 5068 | |
5327ed3d JW |
5069 | /* helper call returns 64-bit value. */ |
5070 | regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG; | |
5071 | ||
dc503a8a | 5072 | /* update return register (already marked as written above) */ |
17a52670 | 5073 | if (fn->ret_type == RET_INTEGER) { |
f1174f77 | 5074 | /* sets type to SCALAR_VALUE */ |
61bd5218 | 5075 | mark_reg_unknown(env, regs, BPF_REG_0); |
17a52670 AS |
5076 | } else if (fn->ret_type == RET_VOID) { |
5077 | regs[BPF_REG_0].type = NOT_INIT; | |
3e6a4b3e RG |
5078 | } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL || |
5079 | fn->ret_type == RET_PTR_TO_MAP_VALUE) { | |
f1174f77 | 5080 | /* There is no offset yet applied, variable or fixed */ |
61bd5218 | 5081 | mark_reg_known_zero(env, regs, BPF_REG_0); |
17a52670 AS |
5082 | /* remember map_ptr, so that check_map_access() |
5083 | * can check 'value_size' boundary of memory access | |
5084 | * to map element returned from bpf_map_lookup_elem() | |
5085 | */ | |
33ff9823 | 5086 | if (meta.map_ptr == NULL) { |
61bd5218 JK |
5087 | verbose(env, |
5088 | "kernel subsystem misconfigured verifier\n"); | |
17a52670 AS |
5089 | return -EINVAL; |
5090 | } | |
33ff9823 | 5091 | regs[BPF_REG_0].map_ptr = meta.map_ptr; |
4d31f301 DB |
5092 | if (fn->ret_type == RET_PTR_TO_MAP_VALUE) { |
5093 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE; | |
e16d2f1a AS |
5094 | if (map_value_has_spin_lock(meta.map_ptr)) |
5095 | regs[BPF_REG_0].id = ++env->id_gen; | |
4d31f301 DB |
5096 | } else { |
5097 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL; | |
5098 | regs[BPF_REG_0].id = ++env->id_gen; | |
5099 | } | |
c64b7983 JS |
5100 | } else if (fn->ret_type == RET_PTR_TO_SOCKET_OR_NULL) { |
5101 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
5102 | regs[BPF_REG_0].type = PTR_TO_SOCKET_OR_NULL; | |
0f3adc28 | 5103 | regs[BPF_REG_0].id = ++env->id_gen; |
85a51f8c LB |
5104 | } else if (fn->ret_type == RET_PTR_TO_SOCK_COMMON_OR_NULL) { |
5105 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
5106 | regs[BPF_REG_0].type = PTR_TO_SOCK_COMMON_OR_NULL; | |
5107 | regs[BPF_REG_0].id = ++env->id_gen; | |
655a51e5 MKL |
5108 | } else if (fn->ret_type == RET_PTR_TO_TCP_SOCK_OR_NULL) { |
5109 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
5110 | regs[BPF_REG_0].type = PTR_TO_TCP_SOCK_OR_NULL; | |
5111 | regs[BPF_REG_0].id = ++env->id_gen; | |
457f4436 AN |
5112 | } else if (fn->ret_type == RET_PTR_TO_ALLOC_MEM_OR_NULL) { |
5113 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
5114 | regs[BPF_REG_0].type = PTR_TO_MEM_OR_NULL; | |
5115 | regs[BPF_REG_0].id = ++env->id_gen; | |
5116 | regs[BPF_REG_0].mem_size = meta.mem_size; | |
af7ec138 YS |
5117 | } else if (fn->ret_type == RET_PTR_TO_BTF_ID_OR_NULL) { |
5118 | int ret_btf_id; | |
5119 | ||
5120 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
5121 | regs[BPF_REG_0].type = PTR_TO_BTF_ID_OR_NULL; | |
5122 | ret_btf_id = *fn->ret_btf_id; | |
5123 | if (ret_btf_id == 0) { | |
5124 | verbose(env, "invalid return type %d of func %s#%d\n", | |
5125 | fn->ret_type, func_id_name(func_id), func_id); | |
5126 | return -EINVAL; | |
5127 | } | |
5128 | regs[BPF_REG_0].btf_id = ret_btf_id; | |
17a52670 | 5129 | } else { |
61bd5218 | 5130 | verbose(env, "unknown return type %d of func %s#%d\n", |
ebb676da | 5131 | fn->ret_type, func_id_name(func_id), func_id); |
17a52670 AS |
5132 | return -EINVAL; |
5133 | } | |
04fd61ab | 5134 | |
0f3adc28 | 5135 | if (is_ptr_cast_function(func_id)) { |
1b986589 MKL |
5136 | /* For release_reference() */ |
5137 | regs[BPF_REG_0].ref_obj_id = meta.ref_obj_id; | |
64d85290 | 5138 | } else if (is_acquire_function(func_id, meta.map_ptr)) { |
0f3adc28 LB |
5139 | int id = acquire_reference_state(env, insn_idx); |
5140 | ||
5141 | if (id < 0) | |
5142 | return id; | |
5143 | /* For mark_ptr_or_null_reg() */ | |
5144 | regs[BPF_REG_0].id = id; | |
5145 | /* For release_reference() */ | |
5146 | regs[BPF_REG_0].ref_obj_id = id; | |
5147 | } | |
1b986589 | 5148 | |
849fa506 YS |
5149 | do_refine_retval_range(regs, fn->ret_type, func_id, &meta); |
5150 | ||
61bd5218 | 5151 | err = check_map_func_compatibility(env, meta.map_ptr, func_id); |
35578d79 KX |
5152 | if (err) |
5153 | return err; | |
04fd61ab | 5154 | |
fa28dcb8 SL |
5155 | if ((func_id == BPF_FUNC_get_stack || |
5156 | func_id == BPF_FUNC_get_task_stack) && | |
5157 | !env->prog->has_callchain_buf) { | |
c195651e YS |
5158 | const char *err_str; |
5159 | ||
5160 | #ifdef CONFIG_PERF_EVENTS | |
5161 | err = get_callchain_buffers(sysctl_perf_event_max_stack); | |
5162 | err_str = "cannot get callchain buffer for func %s#%d\n"; | |
5163 | #else | |
5164 | err = -ENOTSUPP; | |
5165 | err_str = "func %s#%d not supported without CONFIG_PERF_EVENTS\n"; | |
5166 | #endif | |
5167 | if (err) { | |
5168 | verbose(env, err_str, func_id_name(func_id), func_id); | |
5169 | return err; | |
5170 | } | |
5171 | ||
5172 | env->prog->has_callchain_buf = true; | |
5173 | } | |
5174 | ||
5d99cb2c SL |
5175 | if (func_id == BPF_FUNC_get_stackid || func_id == BPF_FUNC_get_stack) |
5176 | env->prog->call_get_stack = true; | |
5177 | ||
969bf05e AS |
5178 | if (changes_data) |
5179 | clear_all_pkt_pointers(env); | |
5180 | return 0; | |
5181 | } | |
5182 | ||
b03c9f9f EC |
5183 | static bool signed_add_overflows(s64 a, s64 b) |
5184 | { | |
5185 | /* Do the add in u64, where overflow is well-defined */ | |
5186 | s64 res = (s64)((u64)a + (u64)b); | |
5187 | ||
5188 | if (b < 0) | |
5189 | return res > a; | |
5190 | return res < a; | |
5191 | } | |
5192 | ||
3f50f132 JF |
5193 | static bool signed_add32_overflows(s64 a, s64 b) |
5194 | { | |
5195 | /* Do the add in u32, where overflow is well-defined */ | |
5196 | s32 res = (s32)((u32)a + (u32)b); | |
5197 | ||
5198 | if (b < 0) | |
5199 | return res > a; | |
5200 | return res < a; | |
5201 | } | |
5202 | ||
5203 | static bool signed_sub_overflows(s32 a, s32 b) | |
b03c9f9f EC |
5204 | { |
5205 | /* Do the sub in u64, where overflow is well-defined */ | |
5206 | s64 res = (s64)((u64)a - (u64)b); | |
5207 | ||
5208 | if (b < 0) | |
5209 | return res < a; | |
5210 | return res > a; | |
969bf05e AS |
5211 | } |
5212 | ||
3f50f132 JF |
5213 | static bool signed_sub32_overflows(s32 a, s32 b) |
5214 | { | |
5215 | /* Do the sub in u64, where overflow is well-defined */ | |
5216 | s32 res = (s32)((u32)a - (u32)b); | |
5217 | ||
5218 | if (b < 0) | |
5219 | return res < a; | |
5220 | return res > a; | |
5221 | } | |
5222 | ||
bb7f0f98 AS |
5223 | static bool check_reg_sane_offset(struct bpf_verifier_env *env, |
5224 | const struct bpf_reg_state *reg, | |
5225 | enum bpf_reg_type type) | |
5226 | { | |
5227 | bool known = tnum_is_const(reg->var_off); | |
5228 | s64 val = reg->var_off.value; | |
5229 | s64 smin = reg->smin_value; | |
5230 | ||
5231 | if (known && (val >= BPF_MAX_VAR_OFF || val <= -BPF_MAX_VAR_OFF)) { | |
5232 | verbose(env, "math between %s pointer and %lld is not allowed\n", | |
5233 | reg_type_str[type], val); | |
5234 | return false; | |
5235 | } | |
5236 | ||
5237 | if (reg->off >= BPF_MAX_VAR_OFF || reg->off <= -BPF_MAX_VAR_OFF) { | |
5238 | verbose(env, "%s pointer offset %d is not allowed\n", | |
5239 | reg_type_str[type], reg->off); | |
5240 | return false; | |
5241 | } | |
5242 | ||
5243 | if (smin == S64_MIN) { | |
5244 | verbose(env, "math between %s pointer and register with unbounded min value is not allowed\n", | |
5245 | reg_type_str[type]); | |
5246 | return false; | |
5247 | } | |
5248 | ||
5249 | if (smin >= BPF_MAX_VAR_OFF || smin <= -BPF_MAX_VAR_OFF) { | |
5250 | verbose(env, "value %lld makes %s pointer be out of bounds\n", | |
5251 | smin, reg_type_str[type]); | |
5252 | return false; | |
5253 | } | |
5254 | ||
5255 | return true; | |
5256 | } | |
5257 | ||
979d63d5 DB |
5258 | static struct bpf_insn_aux_data *cur_aux(struct bpf_verifier_env *env) |
5259 | { | |
5260 | return &env->insn_aux_data[env->insn_idx]; | |
5261 | } | |
5262 | ||
5263 | static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg, | |
5264 | u32 *ptr_limit, u8 opcode, bool off_is_neg) | |
5265 | { | |
5266 | bool mask_to_left = (opcode == BPF_ADD && off_is_neg) || | |
5267 | (opcode == BPF_SUB && !off_is_neg); | |
5268 | u32 off; | |
5269 | ||
5270 | switch (ptr_reg->type) { | |
5271 | case PTR_TO_STACK: | |
088ec26d AI |
5272 | /* Indirect variable offset stack access is prohibited in |
5273 | * unprivileged mode so it's not handled here. | |
5274 | */ | |
979d63d5 DB |
5275 | off = ptr_reg->off + ptr_reg->var_off.value; |
5276 | if (mask_to_left) | |
5277 | *ptr_limit = MAX_BPF_STACK + off; | |
5278 | else | |
5279 | *ptr_limit = -off; | |
5280 | return 0; | |
5281 | case PTR_TO_MAP_VALUE: | |
5282 | if (mask_to_left) { | |
5283 | *ptr_limit = ptr_reg->umax_value + ptr_reg->off; | |
5284 | } else { | |
5285 | off = ptr_reg->smin_value + ptr_reg->off; | |
5286 | *ptr_limit = ptr_reg->map_ptr->value_size - off; | |
5287 | } | |
5288 | return 0; | |
5289 | default: | |
5290 | return -EINVAL; | |
5291 | } | |
5292 | } | |
5293 | ||
d3bd7413 DB |
5294 | static bool can_skip_alu_sanitation(const struct bpf_verifier_env *env, |
5295 | const struct bpf_insn *insn) | |
5296 | { | |
2c78ee89 | 5297 | return env->bypass_spec_v1 || BPF_SRC(insn->code) == BPF_K; |
d3bd7413 DB |
5298 | } |
5299 | ||
5300 | static int update_alu_sanitation_state(struct bpf_insn_aux_data *aux, | |
5301 | u32 alu_state, u32 alu_limit) | |
5302 | { | |
5303 | /* If we arrived here from different branches with different | |
5304 | * state or limits to sanitize, then this won't work. | |
5305 | */ | |
5306 | if (aux->alu_state && | |
5307 | (aux->alu_state != alu_state || | |
5308 | aux->alu_limit != alu_limit)) | |
5309 | return -EACCES; | |
5310 | ||
5311 | /* Corresponding fixup done in fixup_bpf_calls(). */ | |
5312 | aux->alu_state = alu_state; | |
5313 | aux->alu_limit = alu_limit; | |
5314 | return 0; | |
5315 | } | |
5316 | ||
5317 | static int sanitize_val_alu(struct bpf_verifier_env *env, | |
5318 | struct bpf_insn *insn) | |
5319 | { | |
5320 | struct bpf_insn_aux_data *aux = cur_aux(env); | |
5321 | ||
5322 | if (can_skip_alu_sanitation(env, insn)) | |
5323 | return 0; | |
5324 | ||
5325 | return update_alu_sanitation_state(aux, BPF_ALU_NON_POINTER, 0); | |
5326 | } | |
5327 | ||
979d63d5 DB |
5328 | static int sanitize_ptr_alu(struct bpf_verifier_env *env, |
5329 | struct bpf_insn *insn, | |
5330 | const struct bpf_reg_state *ptr_reg, | |
5331 | struct bpf_reg_state *dst_reg, | |
5332 | bool off_is_neg) | |
5333 | { | |
5334 | struct bpf_verifier_state *vstate = env->cur_state; | |
5335 | struct bpf_insn_aux_data *aux = cur_aux(env); | |
5336 | bool ptr_is_dst_reg = ptr_reg == dst_reg; | |
5337 | u8 opcode = BPF_OP(insn->code); | |
5338 | u32 alu_state, alu_limit; | |
5339 | struct bpf_reg_state tmp; | |
5340 | bool ret; | |
5341 | ||
d3bd7413 | 5342 | if (can_skip_alu_sanitation(env, insn)) |
979d63d5 DB |
5343 | return 0; |
5344 | ||
5345 | /* We already marked aux for masking from non-speculative | |
5346 | * paths, thus we got here in the first place. We only care | |
5347 | * to explore bad access from here. | |
5348 | */ | |
5349 | if (vstate->speculative) | |
5350 | goto do_sim; | |
5351 | ||
5352 | alu_state = off_is_neg ? BPF_ALU_NEG_VALUE : 0; | |
5353 | alu_state |= ptr_is_dst_reg ? | |
5354 | BPF_ALU_SANITIZE_SRC : BPF_ALU_SANITIZE_DST; | |
5355 | ||
5356 | if (retrieve_ptr_limit(ptr_reg, &alu_limit, opcode, off_is_neg)) | |
5357 | return 0; | |
d3bd7413 | 5358 | if (update_alu_sanitation_state(aux, alu_state, alu_limit)) |
979d63d5 | 5359 | return -EACCES; |
979d63d5 DB |
5360 | do_sim: |
5361 | /* Simulate and find potential out-of-bounds access under | |
5362 | * speculative execution from truncation as a result of | |
5363 | * masking when off was not within expected range. If off | |
5364 | * sits in dst, then we temporarily need to move ptr there | |
5365 | * to simulate dst (== 0) +/-= ptr. Needed, for example, | |
5366 | * for cases where we use K-based arithmetic in one direction | |
5367 | * and truncated reg-based in the other in order to explore | |
5368 | * bad access. | |
5369 | */ | |
5370 | if (!ptr_is_dst_reg) { | |
5371 | tmp = *dst_reg; | |
5372 | *dst_reg = *ptr_reg; | |
5373 | } | |
5374 | ret = push_stack(env, env->insn_idx + 1, env->insn_idx, true); | |
0803278b | 5375 | if (!ptr_is_dst_reg && ret) |
979d63d5 DB |
5376 | *dst_reg = tmp; |
5377 | return !ret ? -EFAULT : 0; | |
5378 | } | |
5379 | ||
f1174f77 | 5380 | /* Handles arithmetic on a pointer and a scalar: computes new min/max and var_off. |
f1174f77 EC |
5381 | * Caller should also handle BPF_MOV case separately. |
5382 | * If we return -EACCES, caller may want to try again treating pointer as a | |
5383 | * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks. | |
5384 | */ | |
5385 | static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, | |
5386 | struct bpf_insn *insn, | |
5387 | const struct bpf_reg_state *ptr_reg, | |
5388 | const struct bpf_reg_state *off_reg) | |
969bf05e | 5389 | { |
f4d7e40a AS |
5390 | struct bpf_verifier_state *vstate = env->cur_state; |
5391 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
5392 | struct bpf_reg_state *regs = state->regs, *dst_reg; | |
f1174f77 | 5393 | bool known = tnum_is_const(off_reg->var_off); |
b03c9f9f EC |
5394 | s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value, |
5395 | smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value; | |
5396 | u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value, | |
5397 | umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value; | |
9d7eceed | 5398 | u32 dst = insn->dst_reg, src = insn->src_reg; |
969bf05e | 5399 | u8 opcode = BPF_OP(insn->code); |
979d63d5 | 5400 | int ret; |
969bf05e | 5401 | |
f1174f77 | 5402 | dst_reg = ®s[dst]; |
969bf05e | 5403 | |
6f16101e DB |
5404 | if ((known && (smin_val != smax_val || umin_val != umax_val)) || |
5405 | smin_val > smax_val || umin_val > umax_val) { | |
5406 | /* Taint dst register if offset had invalid bounds derived from | |
5407 | * e.g. dead branches. | |
5408 | */ | |
f54c7898 | 5409 | __mark_reg_unknown(env, dst_reg); |
6f16101e | 5410 | return 0; |
f1174f77 EC |
5411 | } |
5412 | ||
5413 | if (BPF_CLASS(insn->code) != BPF_ALU64) { | |
5414 | /* 32-bit ALU ops on pointers produce (meaningless) scalars */ | |
6c693541 YS |
5415 | if (opcode == BPF_SUB && env->allow_ptr_leaks) { |
5416 | __mark_reg_unknown(env, dst_reg); | |
5417 | return 0; | |
5418 | } | |
5419 | ||
82abbf8d AS |
5420 | verbose(env, |
5421 | "R%d 32-bit pointer arithmetic prohibited\n", | |
5422 | dst); | |
f1174f77 | 5423 | return -EACCES; |
969bf05e AS |
5424 | } |
5425 | ||
aad2eeaf JS |
5426 | switch (ptr_reg->type) { |
5427 | case PTR_TO_MAP_VALUE_OR_NULL: | |
5428 | verbose(env, "R%d pointer arithmetic on %s prohibited, null-check it first\n", | |
5429 | dst, reg_type_str[ptr_reg->type]); | |
f1174f77 | 5430 | return -EACCES; |
aad2eeaf | 5431 | case CONST_PTR_TO_MAP: |
7c696732 YS |
5432 | /* smin_val represents the known value */ |
5433 | if (known && smin_val == 0 && opcode == BPF_ADD) | |
5434 | break; | |
5435 | /* fall-through */ | |
aad2eeaf | 5436 | case PTR_TO_PACKET_END: |
c64b7983 JS |
5437 | case PTR_TO_SOCKET: |
5438 | case PTR_TO_SOCKET_OR_NULL: | |
46f8bc92 MKL |
5439 | case PTR_TO_SOCK_COMMON: |
5440 | case PTR_TO_SOCK_COMMON_OR_NULL: | |
655a51e5 MKL |
5441 | case PTR_TO_TCP_SOCK: |
5442 | case PTR_TO_TCP_SOCK_OR_NULL: | |
fada7fdc | 5443 | case PTR_TO_XDP_SOCK: |
aad2eeaf JS |
5444 | verbose(env, "R%d pointer arithmetic on %s prohibited\n", |
5445 | dst, reg_type_str[ptr_reg->type]); | |
f1174f77 | 5446 | return -EACCES; |
9d7eceed DB |
5447 | case PTR_TO_MAP_VALUE: |
5448 | if (!env->allow_ptr_leaks && !known && (smin_val < 0) != (smax_val < 0)) { | |
5449 | verbose(env, "R%d has unknown scalar with mixed signed bounds, pointer arithmetic with it prohibited for !root\n", | |
5450 | off_reg == dst_reg ? dst : src); | |
5451 | return -EACCES; | |
5452 | } | |
df561f66 | 5453 | fallthrough; |
aad2eeaf JS |
5454 | default: |
5455 | break; | |
f1174f77 EC |
5456 | } |
5457 | ||
5458 | /* In case of 'scalar += pointer', dst_reg inherits pointer type and id. | |
5459 | * The id may be overwritten later if we create a new variable offset. | |
969bf05e | 5460 | */ |
f1174f77 EC |
5461 | dst_reg->type = ptr_reg->type; |
5462 | dst_reg->id = ptr_reg->id; | |
969bf05e | 5463 | |
bb7f0f98 AS |
5464 | if (!check_reg_sane_offset(env, off_reg, ptr_reg->type) || |
5465 | !check_reg_sane_offset(env, ptr_reg, ptr_reg->type)) | |
5466 | return -EINVAL; | |
5467 | ||
3f50f132 JF |
5468 | /* pointer types do not carry 32-bit bounds at the moment. */ |
5469 | __mark_reg32_unbounded(dst_reg); | |
5470 | ||
f1174f77 EC |
5471 | switch (opcode) { |
5472 | case BPF_ADD: | |
979d63d5 DB |
5473 | ret = sanitize_ptr_alu(env, insn, ptr_reg, dst_reg, smin_val < 0); |
5474 | if (ret < 0) { | |
5475 | verbose(env, "R%d tried to add from different maps or paths\n", dst); | |
5476 | return ret; | |
5477 | } | |
f1174f77 EC |
5478 | /* We can take a fixed offset as long as it doesn't overflow |
5479 | * the s32 'off' field | |
969bf05e | 5480 | */ |
b03c9f9f EC |
5481 | if (known && (ptr_reg->off + smin_val == |
5482 | (s64)(s32)(ptr_reg->off + smin_val))) { | |
f1174f77 | 5483 | /* pointer += K. Accumulate it into fixed offset */ |
b03c9f9f EC |
5484 | dst_reg->smin_value = smin_ptr; |
5485 | dst_reg->smax_value = smax_ptr; | |
5486 | dst_reg->umin_value = umin_ptr; | |
5487 | dst_reg->umax_value = umax_ptr; | |
f1174f77 | 5488 | dst_reg->var_off = ptr_reg->var_off; |
b03c9f9f | 5489 | dst_reg->off = ptr_reg->off + smin_val; |
0962590e | 5490 | dst_reg->raw = ptr_reg->raw; |
f1174f77 EC |
5491 | break; |
5492 | } | |
f1174f77 EC |
5493 | /* A new variable offset is created. Note that off_reg->off |
5494 | * == 0, since it's a scalar. | |
5495 | * dst_reg gets the pointer type and since some positive | |
5496 | * integer value was added to the pointer, give it a new 'id' | |
5497 | * if it's a PTR_TO_PACKET. | |
5498 | * this creates a new 'base' pointer, off_reg (variable) gets | |
5499 | * added into the variable offset, and we copy the fixed offset | |
5500 | * from ptr_reg. | |
969bf05e | 5501 | */ |
b03c9f9f EC |
5502 | if (signed_add_overflows(smin_ptr, smin_val) || |
5503 | signed_add_overflows(smax_ptr, smax_val)) { | |
5504 | dst_reg->smin_value = S64_MIN; | |
5505 | dst_reg->smax_value = S64_MAX; | |
5506 | } else { | |
5507 | dst_reg->smin_value = smin_ptr + smin_val; | |
5508 | dst_reg->smax_value = smax_ptr + smax_val; | |
5509 | } | |
5510 | if (umin_ptr + umin_val < umin_ptr || | |
5511 | umax_ptr + umax_val < umax_ptr) { | |
5512 | dst_reg->umin_value = 0; | |
5513 | dst_reg->umax_value = U64_MAX; | |
5514 | } else { | |
5515 | dst_reg->umin_value = umin_ptr + umin_val; | |
5516 | dst_reg->umax_value = umax_ptr + umax_val; | |
5517 | } | |
f1174f77 EC |
5518 | dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off); |
5519 | dst_reg->off = ptr_reg->off; | |
0962590e | 5520 | dst_reg->raw = ptr_reg->raw; |
de8f3a83 | 5521 | if (reg_is_pkt_pointer(ptr_reg)) { |
f1174f77 EC |
5522 | dst_reg->id = ++env->id_gen; |
5523 | /* something was added to pkt_ptr, set range to zero */ | |
0962590e | 5524 | dst_reg->raw = 0; |
f1174f77 EC |
5525 | } |
5526 | break; | |
5527 | case BPF_SUB: | |
979d63d5 DB |
5528 | ret = sanitize_ptr_alu(env, insn, ptr_reg, dst_reg, smin_val < 0); |
5529 | if (ret < 0) { | |
5530 | verbose(env, "R%d tried to sub from different maps or paths\n", dst); | |
5531 | return ret; | |
5532 | } | |
f1174f77 EC |
5533 | if (dst_reg == off_reg) { |
5534 | /* scalar -= pointer. Creates an unknown scalar */ | |
82abbf8d AS |
5535 | verbose(env, "R%d tried to subtract pointer from scalar\n", |
5536 | dst); | |
f1174f77 EC |
5537 | return -EACCES; |
5538 | } | |
5539 | /* We don't allow subtraction from FP, because (according to | |
5540 | * test_verifier.c test "invalid fp arithmetic", JITs might not | |
5541 | * be able to deal with it. | |
969bf05e | 5542 | */ |
f1174f77 | 5543 | if (ptr_reg->type == PTR_TO_STACK) { |
82abbf8d AS |
5544 | verbose(env, "R%d subtraction from stack pointer prohibited\n", |
5545 | dst); | |
f1174f77 EC |
5546 | return -EACCES; |
5547 | } | |
b03c9f9f EC |
5548 | if (known && (ptr_reg->off - smin_val == |
5549 | (s64)(s32)(ptr_reg->off - smin_val))) { | |
f1174f77 | 5550 | /* pointer -= K. Subtract it from fixed offset */ |
b03c9f9f EC |
5551 | dst_reg->smin_value = smin_ptr; |
5552 | dst_reg->smax_value = smax_ptr; | |
5553 | dst_reg->umin_value = umin_ptr; | |
5554 | dst_reg->umax_value = umax_ptr; | |
f1174f77 EC |
5555 | dst_reg->var_off = ptr_reg->var_off; |
5556 | dst_reg->id = ptr_reg->id; | |
b03c9f9f | 5557 | dst_reg->off = ptr_reg->off - smin_val; |
0962590e | 5558 | dst_reg->raw = ptr_reg->raw; |
f1174f77 EC |
5559 | break; |
5560 | } | |
f1174f77 EC |
5561 | /* A new variable offset is created. If the subtrahend is known |
5562 | * nonnegative, then any reg->range we had before is still good. | |
969bf05e | 5563 | */ |
b03c9f9f EC |
5564 | if (signed_sub_overflows(smin_ptr, smax_val) || |
5565 | signed_sub_overflows(smax_ptr, smin_val)) { | |
5566 | /* Overflow possible, we know nothing */ | |
5567 | dst_reg->smin_value = S64_MIN; | |
5568 | dst_reg->smax_value = S64_MAX; | |
5569 | } else { | |
5570 | dst_reg->smin_value = smin_ptr - smax_val; | |
5571 | dst_reg->smax_value = smax_ptr - smin_val; | |
5572 | } | |
5573 | if (umin_ptr < umax_val) { | |
5574 | /* Overflow possible, we know nothing */ | |
5575 | dst_reg->umin_value = 0; | |
5576 | dst_reg->umax_value = U64_MAX; | |
5577 | } else { | |
5578 | /* Cannot overflow (as long as bounds are consistent) */ | |
5579 | dst_reg->umin_value = umin_ptr - umax_val; | |
5580 | dst_reg->umax_value = umax_ptr - umin_val; | |
5581 | } | |
f1174f77 EC |
5582 | dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off); |
5583 | dst_reg->off = ptr_reg->off; | |
0962590e | 5584 | dst_reg->raw = ptr_reg->raw; |
de8f3a83 | 5585 | if (reg_is_pkt_pointer(ptr_reg)) { |
f1174f77 EC |
5586 | dst_reg->id = ++env->id_gen; |
5587 | /* something was added to pkt_ptr, set range to zero */ | |
b03c9f9f | 5588 | if (smin_val < 0) |
0962590e | 5589 | dst_reg->raw = 0; |
43188702 | 5590 | } |
f1174f77 EC |
5591 | break; |
5592 | case BPF_AND: | |
5593 | case BPF_OR: | |
5594 | case BPF_XOR: | |
82abbf8d AS |
5595 | /* bitwise ops on pointers are troublesome, prohibit. */ |
5596 | verbose(env, "R%d bitwise operator %s on pointer prohibited\n", | |
5597 | dst, bpf_alu_string[opcode >> 4]); | |
f1174f77 EC |
5598 | return -EACCES; |
5599 | default: | |
5600 | /* other operators (e.g. MUL,LSH) produce non-pointer results */ | |
82abbf8d AS |
5601 | verbose(env, "R%d pointer arithmetic with %s operator prohibited\n", |
5602 | dst, bpf_alu_string[opcode >> 4]); | |
f1174f77 | 5603 | return -EACCES; |
43188702 JF |
5604 | } |
5605 | ||
bb7f0f98 AS |
5606 | if (!check_reg_sane_offset(env, dst_reg, ptr_reg->type)) |
5607 | return -EINVAL; | |
5608 | ||
b03c9f9f EC |
5609 | __update_reg_bounds(dst_reg); |
5610 | __reg_deduce_bounds(dst_reg); | |
5611 | __reg_bound_offset(dst_reg); | |
0d6303db DB |
5612 | |
5613 | /* For unprivileged we require that resulting offset must be in bounds | |
5614 | * in order to be able to sanitize access later on. | |
5615 | */ | |
2c78ee89 | 5616 | if (!env->bypass_spec_v1) { |
e4298d25 DB |
5617 | if (dst_reg->type == PTR_TO_MAP_VALUE && |
5618 | check_map_access(env, dst, dst_reg->off, 1, false)) { | |
5619 | verbose(env, "R%d pointer arithmetic of map value goes out of range, " | |
5620 | "prohibited for !root\n", dst); | |
5621 | return -EACCES; | |
5622 | } else if (dst_reg->type == PTR_TO_STACK && | |
5623 | check_stack_access(env, dst_reg, dst_reg->off + | |
5624 | dst_reg->var_off.value, 1)) { | |
5625 | verbose(env, "R%d stack pointer arithmetic goes out of range, " | |
5626 | "prohibited for !root\n", dst); | |
5627 | return -EACCES; | |
5628 | } | |
0d6303db DB |
5629 | } |
5630 | ||
43188702 JF |
5631 | return 0; |
5632 | } | |
5633 | ||
3f50f132 JF |
5634 | static void scalar32_min_max_add(struct bpf_reg_state *dst_reg, |
5635 | struct bpf_reg_state *src_reg) | |
5636 | { | |
5637 | s32 smin_val = src_reg->s32_min_value; | |
5638 | s32 smax_val = src_reg->s32_max_value; | |
5639 | u32 umin_val = src_reg->u32_min_value; | |
5640 | u32 umax_val = src_reg->u32_max_value; | |
5641 | ||
5642 | if (signed_add32_overflows(dst_reg->s32_min_value, smin_val) || | |
5643 | signed_add32_overflows(dst_reg->s32_max_value, smax_val)) { | |
5644 | dst_reg->s32_min_value = S32_MIN; | |
5645 | dst_reg->s32_max_value = S32_MAX; | |
5646 | } else { | |
5647 | dst_reg->s32_min_value += smin_val; | |
5648 | dst_reg->s32_max_value += smax_val; | |
5649 | } | |
5650 | if (dst_reg->u32_min_value + umin_val < umin_val || | |
5651 | dst_reg->u32_max_value + umax_val < umax_val) { | |
5652 | dst_reg->u32_min_value = 0; | |
5653 | dst_reg->u32_max_value = U32_MAX; | |
5654 | } else { | |
5655 | dst_reg->u32_min_value += umin_val; | |
5656 | dst_reg->u32_max_value += umax_val; | |
5657 | } | |
5658 | } | |
5659 | ||
07cd2631 JF |
5660 | static void scalar_min_max_add(struct bpf_reg_state *dst_reg, |
5661 | struct bpf_reg_state *src_reg) | |
5662 | { | |
5663 | s64 smin_val = src_reg->smin_value; | |
5664 | s64 smax_val = src_reg->smax_value; | |
5665 | u64 umin_val = src_reg->umin_value; | |
5666 | u64 umax_val = src_reg->umax_value; | |
5667 | ||
5668 | if (signed_add_overflows(dst_reg->smin_value, smin_val) || | |
5669 | signed_add_overflows(dst_reg->smax_value, smax_val)) { | |
5670 | dst_reg->smin_value = S64_MIN; | |
5671 | dst_reg->smax_value = S64_MAX; | |
5672 | } else { | |
5673 | dst_reg->smin_value += smin_val; | |
5674 | dst_reg->smax_value += smax_val; | |
5675 | } | |
5676 | if (dst_reg->umin_value + umin_val < umin_val || | |
5677 | dst_reg->umax_value + umax_val < umax_val) { | |
5678 | dst_reg->umin_value = 0; | |
5679 | dst_reg->umax_value = U64_MAX; | |
5680 | } else { | |
5681 | dst_reg->umin_value += umin_val; | |
5682 | dst_reg->umax_value += umax_val; | |
5683 | } | |
3f50f132 JF |
5684 | } |
5685 | ||
5686 | static void scalar32_min_max_sub(struct bpf_reg_state *dst_reg, | |
5687 | struct bpf_reg_state *src_reg) | |
5688 | { | |
5689 | s32 smin_val = src_reg->s32_min_value; | |
5690 | s32 smax_val = src_reg->s32_max_value; | |
5691 | u32 umin_val = src_reg->u32_min_value; | |
5692 | u32 umax_val = src_reg->u32_max_value; | |
5693 | ||
5694 | if (signed_sub32_overflows(dst_reg->s32_min_value, smax_val) || | |
5695 | signed_sub32_overflows(dst_reg->s32_max_value, smin_val)) { | |
5696 | /* Overflow possible, we know nothing */ | |
5697 | dst_reg->s32_min_value = S32_MIN; | |
5698 | dst_reg->s32_max_value = S32_MAX; | |
5699 | } else { | |
5700 | dst_reg->s32_min_value -= smax_val; | |
5701 | dst_reg->s32_max_value -= smin_val; | |
5702 | } | |
5703 | if (dst_reg->u32_min_value < umax_val) { | |
5704 | /* Overflow possible, we know nothing */ | |
5705 | dst_reg->u32_min_value = 0; | |
5706 | dst_reg->u32_max_value = U32_MAX; | |
5707 | } else { | |
5708 | /* Cannot overflow (as long as bounds are consistent) */ | |
5709 | dst_reg->u32_min_value -= umax_val; | |
5710 | dst_reg->u32_max_value -= umin_val; | |
5711 | } | |
07cd2631 JF |
5712 | } |
5713 | ||
5714 | static void scalar_min_max_sub(struct bpf_reg_state *dst_reg, | |
5715 | struct bpf_reg_state *src_reg) | |
5716 | { | |
5717 | s64 smin_val = src_reg->smin_value; | |
5718 | s64 smax_val = src_reg->smax_value; | |
5719 | u64 umin_val = src_reg->umin_value; | |
5720 | u64 umax_val = src_reg->umax_value; | |
5721 | ||
5722 | if (signed_sub_overflows(dst_reg->smin_value, smax_val) || | |
5723 | signed_sub_overflows(dst_reg->smax_value, smin_val)) { | |
5724 | /* Overflow possible, we know nothing */ | |
5725 | dst_reg->smin_value = S64_MIN; | |
5726 | dst_reg->smax_value = S64_MAX; | |
5727 | } else { | |
5728 | dst_reg->smin_value -= smax_val; | |
5729 | dst_reg->smax_value -= smin_val; | |
5730 | } | |
5731 | if (dst_reg->umin_value < umax_val) { | |
5732 | /* Overflow possible, we know nothing */ | |
5733 | dst_reg->umin_value = 0; | |
5734 | dst_reg->umax_value = U64_MAX; | |
5735 | } else { | |
5736 | /* Cannot overflow (as long as bounds are consistent) */ | |
5737 | dst_reg->umin_value -= umax_val; | |
5738 | dst_reg->umax_value -= umin_val; | |
5739 | } | |
3f50f132 JF |
5740 | } |
5741 | ||
5742 | static void scalar32_min_max_mul(struct bpf_reg_state *dst_reg, | |
5743 | struct bpf_reg_state *src_reg) | |
5744 | { | |
5745 | s32 smin_val = src_reg->s32_min_value; | |
5746 | u32 umin_val = src_reg->u32_min_value; | |
5747 | u32 umax_val = src_reg->u32_max_value; | |
5748 | ||
5749 | if (smin_val < 0 || dst_reg->s32_min_value < 0) { | |
5750 | /* Ain't nobody got time to multiply that sign */ | |
5751 | __mark_reg32_unbounded(dst_reg); | |
5752 | return; | |
5753 | } | |
5754 | /* Both values are positive, so we can work with unsigned and | |
5755 | * copy the result to signed (unless it exceeds S32_MAX). | |
5756 | */ | |
5757 | if (umax_val > U16_MAX || dst_reg->u32_max_value > U16_MAX) { | |
5758 | /* Potential overflow, we know nothing */ | |
5759 | __mark_reg32_unbounded(dst_reg); | |
5760 | return; | |
5761 | } | |
5762 | dst_reg->u32_min_value *= umin_val; | |
5763 | dst_reg->u32_max_value *= umax_val; | |
5764 | if (dst_reg->u32_max_value > S32_MAX) { | |
5765 | /* Overflow possible, we know nothing */ | |
5766 | dst_reg->s32_min_value = S32_MIN; | |
5767 | dst_reg->s32_max_value = S32_MAX; | |
5768 | } else { | |
5769 | dst_reg->s32_min_value = dst_reg->u32_min_value; | |
5770 | dst_reg->s32_max_value = dst_reg->u32_max_value; | |
5771 | } | |
07cd2631 JF |
5772 | } |
5773 | ||
5774 | static void scalar_min_max_mul(struct bpf_reg_state *dst_reg, | |
5775 | struct bpf_reg_state *src_reg) | |
5776 | { | |
5777 | s64 smin_val = src_reg->smin_value; | |
5778 | u64 umin_val = src_reg->umin_value; | |
5779 | u64 umax_val = src_reg->umax_value; | |
5780 | ||
07cd2631 JF |
5781 | if (smin_val < 0 || dst_reg->smin_value < 0) { |
5782 | /* Ain't nobody got time to multiply that sign */ | |
3f50f132 | 5783 | __mark_reg64_unbounded(dst_reg); |
07cd2631 JF |
5784 | return; |
5785 | } | |
5786 | /* Both values are positive, so we can work with unsigned and | |
5787 | * copy the result to signed (unless it exceeds S64_MAX). | |
5788 | */ | |
5789 | if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) { | |
5790 | /* Potential overflow, we know nothing */ | |
3f50f132 | 5791 | __mark_reg64_unbounded(dst_reg); |
07cd2631 JF |
5792 | return; |
5793 | } | |
5794 | dst_reg->umin_value *= umin_val; | |
5795 | dst_reg->umax_value *= umax_val; | |
5796 | if (dst_reg->umax_value > S64_MAX) { | |
5797 | /* Overflow possible, we know nothing */ | |
5798 | dst_reg->smin_value = S64_MIN; | |
5799 | dst_reg->smax_value = S64_MAX; | |
5800 | } else { | |
5801 | dst_reg->smin_value = dst_reg->umin_value; | |
5802 | dst_reg->smax_value = dst_reg->umax_value; | |
5803 | } | |
5804 | } | |
5805 | ||
3f50f132 JF |
5806 | static void scalar32_min_max_and(struct bpf_reg_state *dst_reg, |
5807 | struct bpf_reg_state *src_reg) | |
5808 | { | |
5809 | bool src_known = tnum_subreg_is_const(src_reg->var_off); | |
5810 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); | |
5811 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); | |
5812 | s32 smin_val = src_reg->s32_min_value; | |
5813 | u32 umax_val = src_reg->u32_max_value; | |
5814 | ||
5815 | /* Assuming scalar64_min_max_and will be called so its safe | |
5816 | * to skip updating register for known 32-bit case. | |
5817 | */ | |
5818 | if (src_known && dst_known) | |
5819 | return; | |
5820 | ||
5821 | /* We get our minimum from the var_off, since that's inherently | |
5822 | * bitwise. Our maximum is the minimum of the operands' maxima. | |
5823 | */ | |
5824 | dst_reg->u32_min_value = var32_off.value; | |
5825 | dst_reg->u32_max_value = min(dst_reg->u32_max_value, umax_val); | |
5826 | if (dst_reg->s32_min_value < 0 || smin_val < 0) { | |
5827 | /* Lose signed bounds when ANDing negative numbers, | |
5828 | * ain't nobody got time for that. | |
5829 | */ | |
5830 | dst_reg->s32_min_value = S32_MIN; | |
5831 | dst_reg->s32_max_value = S32_MAX; | |
5832 | } else { | |
5833 | /* ANDing two positives gives a positive, so safe to | |
5834 | * cast result into s64. | |
5835 | */ | |
5836 | dst_reg->s32_min_value = dst_reg->u32_min_value; | |
5837 | dst_reg->s32_max_value = dst_reg->u32_max_value; | |
5838 | } | |
5839 | ||
5840 | } | |
5841 | ||
07cd2631 JF |
5842 | static void scalar_min_max_and(struct bpf_reg_state *dst_reg, |
5843 | struct bpf_reg_state *src_reg) | |
5844 | { | |
3f50f132 JF |
5845 | bool src_known = tnum_is_const(src_reg->var_off); |
5846 | bool dst_known = tnum_is_const(dst_reg->var_off); | |
07cd2631 JF |
5847 | s64 smin_val = src_reg->smin_value; |
5848 | u64 umax_val = src_reg->umax_value; | |
5849 | ||
3f50f132 | 5850 | if (src_known && dst_known) { |
4fbb38a3 | 5851 | __mark_reg_known(dst_reg, dst_reg->var_off.value); |
3f50f132 JF |
5852 | return; |
5853 | } | |
5854 | ||
07cd2631 JF |
5855 | /* We get our minimum from the var_off, since that's inherently |
5856 | * bitwise. Our maximum is the minimum of the operands' maxima. | |
5857 | */ | |
07cd2631 JF |
5858 | dst_reg->umin_value = dst_reg->var_off.value; |
5859 | dst_reg->umax_value = min(dst_reg->umax_value, umax_val); | |
5860 | if (dst_reg->smin_value < 0 || smin_val < 0) { | |
5861 | /* Lose signed bounds when ANDing negative numbers, | |
5862 | * ain't nobody got time for that. | |
5863 | */ | |
5864 | dst_reg->smin_value = S64_MIN; | |
5865 | dst_reg->smax_value = S64_MAX; | |
5866 | } else { | |
5867 | /* ANDing two positives gives a positive, so safe to | |
5868 | * cast result into s64. | |
5869 | */ | |
5870 | dst_reg->smin_value = dst_reg->umin_value; | |
5871 | dst_reg->smax_value = dst_reg->umax_value; | |
5872 | } | |
5873 | /* We may learn something more from the var_off */ | |
5874 | __update_reg_bounds(dst_reg); | |
5875 | } | |
5876 | ||
3f50f132 JF |
5877 | static void scalar32_min_max_or(struct bpf_reg_state *dst_reg, |
5878 | struct bpf_reg_state *src_reg) | |
5879 | { | |
5880 | bool src_known = tnum_subreg_is_const(src_reg->var_off); | |
5881 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); | |
5882 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); | |
5883 | s32 smin_val = src_reg->smin_value; | |
5884 | u32 umin_val = src_reg->umin_value; | |
5885 | ||
5886 | /* Assuming scalar64_min_max_or will be called so it is safe | |
5887 | * to skip updating register for known case. | |
5888 | */ | |
5889 | if (src_known && dst_known) | |
5890 | return; | |
5891 | ||
5892 | /* We get our maximum from the var_off, and our minimum is the | |
5893 | * maximum of the operands' minima | |
5894 | */ | |
5895 | dst_reg->u32_min_value = max(dst_reg->u32_min_value, umin_val); | |
5896 | dst_reg->u32_max_value = var32_off.value | var32_off.mask; | |
5897 | if (dst_reg->s32_min_value < 0 || smin_val < 0) { | |
5898 | /* Lose signed bounds when ORing negative numbers, | |
5899 | * ain't nobody got time for that. | |
5900 | */ | |
5901 | dst_reg->s32_min_value = S32_MIN; | |
5902 | dst_reg->s32_max_value = S32_MAX; | |
5903 | } else { | |
5904 | /* ORing two positives gives a positive, so safe to | |
5905 | * cast result into s64. | |
5906 | */ | |
5907 | dst_reg->s32_min_value = dst_reg->umin_value; | |
5908 | dst_reg->s32_max_value = dst_reg->umax_value; | |
5909 | } | |
5910 | } | |
5911 | ||
07cd2631 JF |
5912 | static void scalar_min_max_or(struct bpf_reg_state *dst_reg, |
5913 | struct bpf_reg_state *src_reg) | |
5914 | { | |
3f50f132 JF |
5915 | bool src_known = tnum_is_const(src_reg->var_off); |
5916 | bool dst_known = tnum_is_const(dst_reg->var_off); | |
07cd2631 JF |
5917 | s64 smin_val = src_reg->smin_value; |
5918 | u64 umin_val = src_reg->umin_value; | |
5919 | ||
3f50f132 | 5920 | if (src_known && dst_known) { |
4fbb38a3 | 5921 | __mark_reg_known(dst_reg, dst_reg->var_off.value); |
3f50f132 JF |
5922 | return; |
5923 | } | |
5924 | ||
07cd2631 JF |
5925 | /* We get our maximum from the var_off, and our minimum is the |
5926 | * maximum of the operands' minima | |
5927 | */ | |
07cd2631 JF |
5928 | dst_reg->umin_value = max(dst_reg->umin_value, umin_val); |
5929 | dst_reg->umax_value = dst_reg->var_off.value | dst_reg->var_off.mask; | |
5930 | if (dst_reg->smin_value < 0 || smin_val < 0) { | |
5931 | /* Lose signed bounds when ORing negative numbers, | |
5932 | * ain't nobody got time for that. | |
5933 | */ | |
5934 | dst_reg->smin_value = S64_MIN; | |
5935 | dst_reg->smax_value = S64_MAX; | |
5936 | } else { | |
5937 | /* ORing two positives gives a positive, so safe to | |
5938 | * cast result into s64. | |
5939 | */ | |
5940 | dst_reg->smin_value = dst_reg->umin_value; | |
5941 | dst_reg->smax_value = dst_reg->umax_value; | |
5942 | } | |
5943 | /* We may learn something more from the var_off */ | |
5944 | __update_reg_bounds(dst_reg); | |
5945 | } | |
5946 | ||
2921c90d YS |
5947 | static void scalar32_min_max_xor(struct bpf_reg_state *dst_reg, |
5948 | struct bpf_reg_state *src_reg) | |
5949 | { | |
5950 | bool src_known = tnum_subreg_is_const(src_reg->var_off); | |
5951 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); | |
5952 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); | |
5953 | s32 smin_val = src_reg->s32_min_value; | |
5954 | ||
5955 | /* Assuming scalar64_min_max_xor will be called so it is safe | |
5956 | * to skip updating register for known case. | |
5957 | */ | |
5958 | if (src_known && dst_known) | |
5959 | return; | |
5960 | ||
5961 | /* We get both minimum and maximum from the var32_off. */ | |
5962 | dst_reg->u32_min_value = var32_off.value; | |
5963 | dst_reg->u32_max_value = var32_off.value | var32_off.mask; | |
5964 | ||
5965 | if (dst_reg->s32_min_value >= 0 && smin_val >= 0) { | |
5966 | /* XORing two positive sign numbers gives a positive, | |
5967 | * so safe to cast u32 result into s32. | |
5968 | */ | |
5969 | dst_reg->s32_min_value = dst_reg->u32_min_value; | |
5970 | dst_reg->s32_max_value = dst_reg->u32_max_value; | |
5971 | } else { | |
5972 | dst_reg->s32_min_value = S32_MIN; | |
5973 | dst_reg->s32_max_value = S32_MAX; | |
5974 | } | |
5975 | } | |
5976 | ||
5977 | static void scalar_min_max_xor(struct bpf_reg_state *dst_reg, | |
5978 | struct bpf_reg_state *src_reg) | |
5979 | { | |
5980 | bool src_known = tnum_is_const(src_reg->var_off); | |
5981 | bool dst_known = tnum_is_const(dst_reg->var_off); | |
5982 | s64 smin_val = src_reg->smin_value; | |
5983 | ||
5984 | if (src_known && dst_known) { | |
5985 | /* dst_reg->var_off.value has been updated earlier */ | |
5986 | __mark_reg_known(dst_reg, dst_reg->var_off.value); | |
5987 | return; | |
5988 | } | |
5989 | ||
5990 | /* We get both minimum and maximum from the var_off. */ | |
5991 | dst_reg->umin_value = dst_reg->var_off.value; | |
5992 | dst_reg->umax_value = dst_reg->var_off.value | dst_reg->var_off.mask; | |
5993 | ||
5994 | if (dst_reg->smin_value >= 0 && smin_val >= 0) { | |
5995 | /* XORing two positive sign numbers gives a positive, | |
5996 | * so safe to cast u64 result into s64. | |
5997 | */ | |
5998 | dst_reg->smin_value = dst_reg->umin_value; | |
5999 | dst_reg->smax_value = dst_reg->umax_value; | |
6000 | } else { | |
6001 | dst_reg->smin_value = S64_MIN; | |
6002 | dst_reg->smax_value = S64_MAX; | |
6003 | } | |
6004 | ||
6005 | __update_reg_bounds(dst_reg); | |
6006 | } | |
6007 | ||
3f50f132 JF |
6008 | static void __scalar32_min_max_lsh(struct bpf_reg_state *dst_reg, |
6009 | u64 umin_val, u64 umax_val) | |
07cd2631 | 6010 | { |
07cd2631 JF |
6011 | /* We lose all sign bit information (except what we can pick |
6012 | * up from var_off) | |
6013 | */ | |
3f50f132 JF |
6014 | dst_reg->s32_min_value = S32_MIN; |
6015 | dst_reg->s32_max_value = S32_MAX; | |
6016 | /* If we might shift our top bit out, then we know nothing */ | |
6017 | if (umax_val > 31 || dst_reg->u32_max_value > 1ULL << (31 - umax_val)) { | |
6018 | dst_reg->u32_min_value = 0; | |
6019 | dst_reg->u32_max_value = U32_MAX; | |
6020 | } else { | |
6021 | dst_reg->u32_min_value <<= umin_val; | |
6022 | dst_reg->u32_max_value <<= umax_val; | |
6023 | } | |
6024 | } | |
6025 | ||
6026 | static void scalar32_min_max_lsh(struct bpf_reg_state *dst_reg, | |
6027 | struct bpf_reg_state *src_reg) | |
6028 | { | |
6029 | u32 umax_val = src_reg->u32_max_value; | |
6030 | u32 umin_val = src_reg->u32_min_value; | |
6031 | /* u32 alu operation will zext upper bits */ | |
6032 | struct tnum subreg = tnum_subreg(dst_reg->var_off); | |
6033 | ||
6034 | __scalar32_min_max_lsh(dst_reg, umin_val, umax_val); | |
6035 | dst_reg->var_off = tnum_subreg(tnum_lshift(subreg, umin_val)); | |
6036 | /* Not required but being careful mark reg64 bounds as unknown so | |
6037 | * that we are forced to pick them up from tnum and zext later and | |
6038 | * if some path skips this step we are still safe. | |
6039 | */ | |
6040 | __mark_reg64_unbounded(dst_reg); | |
6041 | __update_reg32_bounds(dst_reg); | |
6042 | } | |
6043 | ||
6044 | static void __scalar64_min_max_lsh(struct bpf_reg_state *dst_reg, | |
6045 | u64 umin_val, u64 umax_val) | |
6046 | { | |
6047 | /* Special case <<32 because it is a common compiler pattern to sign | |
6048 | * extend subreg by doing <<32 s>>32. In this case if 32bit bounds are | |
6049 | * positive we know this shift will also be positive so we can track | |
6050 | * bounds correctly. Otherwise we lose all sign bit information except | |
6051 | * what we can pick up from var_off. Perhaps we can generalize this | |
6052 | * later to shifts of any length. | |
6053 | */ | |
6054 | if (umin_val == 32 && umax_val == 32 && dst_reg->s32_max_value >= 0) | |
6055 | dst_reg->smax_value = (s64)dst_reg->s32_max_value << 32; | |
6056 | else | |
6057 | dst_reg->smax_value = S64_MAX; | |
6058 | ||
6059 | if (umin_val == 32 && umax_val == 32 && dst_reg->s32_min_value >= 0) | |
6060 | dst_reg->smin_value = (s64)dst_reg->s32_min_value << 32; | |
6061 | else | |
6062 | dst_reg->smin_value = S64_MIN; | |
6063 | ||
07cd2631 JF |
6064 | /* If we might shift our top bit out, then we know nothing */ |
6065 | if (dst_reg->umax_value > 1ULL << (63 - umax_val)) { | |
6066 | dst_reg->umin_value = 0; | |
6067 | dst_reg->umax_value = U64_MAX; | |
6068 | } else { | |
6069 | dst_reg->umin_value <<= umin_val; | |
6070 | dst_reg->umax_value <<= umax_val; | |
6071 | } | |
3f50f132 JF |
6072 | } |
6073 | ||
6074 | static void scalar_min_max_lsh(struct bpf_reg_state *dst_reg, | |
6075 | struct bpf_reg_state *src_reg) | |
6076 | { | |
6077 | u64 umax_val = src_reg->umax_value; | |
6078 | u64 umin_val = src_reg->umin_value; | |
6079 | ||
6080 | /* scalar64 calc uses 32bit unshifted bounds so must be called first */ | |
6081 | __scalar64_min_max_lsh(dst_reg, umin_val, umax_val); | |
6082 | __scalar32_min_max_lsh(dst_reg, umin_val, umax_val); | |
6083 | ||
07cd2631 JF |
6084 | dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val); |
6085 | /* We may learn something more from the var_off */ | |
6086 | __update_reg_bounds(dst_reg); | |
6087 | } | |
6088 | ||
3f50f132 JF |
6089 | static void scalar32_min_max_rsh(struct bpf_reg_state *dst_reg, |
6090 | struct bpf_reg_state *src_reg) | |
6091 | { | |
6092 | struct tnum subreg = tnum_subreg(dst_reg->var_off); | |
6093 | u32 umax_val = src_reg->u32_max_value; | |
6094 | u32 umin_val = src_reg->u32_min_value; | |
6095 | ||
6096 | /* BPF_RSH is an unsigned shift. If the value in dst_reg might | |
6097 | * be negative, then either: | |
6098 | * 1) src_reg might be zero, so the sign bit of the result is | |
6099 | * unknown, so we lose our signed bounds | |
6100 | * 2) it's known negative, thus the unsigned bounds capture the | |
6101 | * signed bounds | |
6102 | * 3) the signed bounds cross zero, so they tell us nothing | |
6103 | * about the result | |
6104 | * If the value in dst_reg is known nonnegative, then again the | |
6105 | * unsigned bounts capture the signed bounds. | |
6106 | * Thus, in all cases it suffices to blow away our signed bounds | |
6107 | * and rely on inferring new ones from the unsigned bounds and | |
6108 | * var_off of the result. | |
6109 | */ | |
6110 | dst_reg->s32_min_value = S32_MIN; | |
6111 | dst_reg->s32_max_value = S32_MAX; | |
6112 | ||
6113 | dst_reg->var_off = tnum_rshift(subreg, umin_val); | |
6114 | dst_reg->u32_min_value >>= umax_val; | |
6115 | dst_reg->u32_max_value >>= umin_val; | |
6116 | ||
6117 | __mark_reg64_unbounded(dst_reg); | |
6118 | __update_reg32_bounds(dst_reg); | |
6119 | } | |
6120 | ||
07cd2631 JF |
6121 | static void scalar_min_max_rsh(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 | /* BPF_RSH is an unsigned shift. If the value in dst_reg might | |
6128 | * be negative, then either: | |
6129 | * 1) src_reg might be zero, so the sign bit of the result is | |
6130 | * unknown, so we lose our signed bounds | |
6131 | * 2) it's known negative, thus the unsigned bounds capture the | |
6132 | * signed bounds | |
6133 | * 3) the signed bounds cross zero, so they tell us nothing | |
6134 | * about the result | |
6135 | * If the value in dst_reg is known nonnegative, then again the | |
6136 | * unsigned bounts capture the signed bounds. | |
6137 | * Thus, in all cases it suffices to blow away our signed bounds | |
6138 | * and rely on inferring new ones from the unsigned bounds and | |
6139 | * var_off of the result. | |
6140 | */ | |
6141 | dst_reg->smin_value = S64_MIN; | |
6142 | dst_reg->smax_value = S64_MAX; | |
6143 | dst_reg->var_off = tnum_rshift(dst_reg->var_off, umin_val); | |
6144 | dst_reg->umin_value >>= umax_val; | |
6145 | dst_reg->umax_value >>= umin_val; | |
3f50f132 JF |
6146 | |
6147 | /* Its not easy to operate on alu32 bounds here because it depends | |
6148 | * on bits being shifted in. Take easy way out and mark unbounded | |
6149 | * so we can recalculate later from tnum. | |
6150 | */ | |
6151 | __mark_reg32_unbounded(dst_reg); | |
07cd2631 JF |
6152 | __update_reg_bounds(dst_reg); |
6153 | } | |
6154 | ||
3f50f132 JF |
6155 | static void scalar32_min_max_arsh(struct bpf_reg_state *dst_reg, |
6156 | struct bpf_reg_state *src_reg) | |
07cd2631 | 6157 | { |
3f50f132 | 6158 | u64 umin_val = src_reg->u32_min_value; |
07cd2631 JF |
6159 | |
6160 | /* Upon reaching here, src_known is true and | |
6161 | * umax_val is equal to umin_val. | |
6162 | */ | |
3f50f132 JF |
6163 | dst_reg->s32_min_value = (u32)(((s32)dst_reg->s32_min_value) >> umin_val); |
6164 | dst_reg->s32_max_value = (u32)(((s32)dst_reg->s32_max_value) >> umin_val); | |
07cd2631 | 6165 | |
3f50f132 JF |
6166 | dst_reg->var_off = tnum_arshift(tnum_subreg(dst_reg->var_off), umin_val, 32); |
6167 | ||
6168 | /* blow away the dst_reg umin_value/umax_value and rely on | |
6169 | * dst_reg var_off to refine the result. | |
6170 | */ | |
6171 | dst_reg->u32_min_value = 0; | |
6172 | dst_reg->u32_max_value = U32_MAX; | |
6173 | ||
6174 | __mark_reg64_unbounded(dst_reg); | |
6175 | __update_reg32_bounds(dst_reg); | |
6176 | } | |
6177 | ||
6178 | static void scalar_min_max_arsh(struct bpf_reg_state *dst_reg, | |
6179 | struct bpf_reg_state *src_reg) | |
6180 | { | |
6181 | u64 umin_val = src_reg->umin_value; | |
6182 | ||
6183 | /* Upon reaching here, src_known is true and umax_val is equal | |
6184 | * to umin_val. | |
6185 | */ | |
6186 | dst_reg->smin_value >>= umin_val; | |
6187 | dst_reg->smax_value >>= umin_val; | |
6188 | ||
6189 | dst_reg->var_off = tnum_arshift(dst_reg->var_off, umin_val, 64); | |
07cd2631 JF |
6190 | |
6191 | /* blow away the dst_reg umin_value/umax_value and rely on | |
6192 | * dst_reg var_off to refine the result. | |
6193 | */ | |
6194 | dst_reg->umin_value = 0; | |
6195 | dst_reg->umax_value = U64_MAX; | |
3f50f132 JF |
6196 | |
6197 | /* Its not easy to operate on alu32 bounds here because it depends | |
6198 | * on bits being shifted in from upper 32-bits. Take easy way out | |
6199 | * and mark unbounded so we can recalculate later from tnum. | |
6200 | */ | |
6201 | __mark_reg32_unbounded(dst_reg); | |
07cd2631 JF |
6202 | __update_reg_bounds(dst_reg); |
6203 | } | |
6204 | ||
468f6eaf JH |
6205 | /* WARNING: This function does calculations on 64-bit values, but the actual |
6206 | * execution may occur on 32-bit values. Therefore, things like bitshifts | |
6207 | * need extra checks in the 32-bit case. | |
6208 | */ | |
f1174f77 EC |
6209 | static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env, |
6210 | struct bpf_insn *insn, | |
6211 | struct bpf_reg_state *dst_reg, | |
6212 | struct bpf_reg_state src_reg) | |
969bf05e | 6213 | { |
638f5b90 | 6214 | struct bpf_reg_state *regs = cur_regs(env); |
48461135 | 6215 | u8 opcode = BPF_OP(insn->code); |
b0b3fb67 | 6216 | bool src_known; |
b03c9f9f EC |
6217 | s64 smin_val, smax_val; |
6218 | u64 umin_val, umax_val; | |
3f50f132 JF |
6219 | s32 s32_min_val, s32_max_val; |
6220 | u32 u32_min_val, u32_max_val; | |
468f6eaf | 6221 | u64 insn_bitness = (BPF_CLASS(insn->code) == BPF_ALU64) ? 64 : 32; |
d3bd7413 DB |
6222 | u32 dst = insn->dst_reg; |
6223 | int ret; | |
3f50f132 | 6224 | bool alu32 = (BPF_CLASS(insn->code) != BPF_ALU64); |
b799207e | 6225 | |
b03c9f9f EC |
6226 | smin_val = src_reg.smin_value; |
6227 | smax_val = src_reg.smax_value; | |
6228 | umin_val = src_reg.umin_value; | |
6229 | umax_val = src_reg.umax_value; | |
f23cc643 | 6230 | |
3f50f132 JF |
6231 | s32_min_val = src_reg.s32_min_value; |
6232 | s32_max_val = src_reg.s32_max_value; | |
6233 | u32_min_val = src_reg.u32_min_value; | |
6234 | u32_max_val = src_reg.u32_max_value; | |
6235 | ||
6236 | if (alu32) { | |
6237 | src_known = tnum_subreg_is_const(src_reg.var_off); | |
3f50f132 JF |
6238 | if ((src_known && |
6239 | (s32_min_val != s32_max_val || u32_min_val != u32_max_val)) || | |
6240 | s32_min_val > s32_max_val || u32_min_val > u32_max_val) { | |
6241 | /* Taint dst register if offset had invalid bounds | |
6242 | * derived from e.g. dead branches. | |
6243 | */ | |
6244 | __mark_reg_unknown(env, dst_reg); | |
6245 | return 0; | |
6246 | } | |
6247 | } else { | |
6248 | src_known = tnum_is_const(src_reg.var_off); | |
3f50f132 JF |
6249 | if ((src_known && |
6250 | (smin_val != smax_val || umin_val != umax_val)) || | |
6251 | smin_val > smax_val || umin_val > umax_val) { | |
6252 | /* Taint dst register if offset had invalid bounds | |
6253 | * derived from e.g. dead branches. | |
6254 | */ | |
6255 | __mark_reg_unknown(env, dst_reg); | |
6256 | return 0; | |
6257 | } | |
6f16101e DB |
6258 | } |
6259 | ||
bb7f0f98 AS |
6260 | if (!src_known && |
6261 | opcode != BPF_ADD && opcode != BPF_SUB && opcode != BPF_AND) { | |
f54c7898 | 6262 | __mark_reg_unknown(env, dst_reg); |
bb7f0f98 AS |
6263 | return 0; |
6264 | } | |
6265 | ||
3f50f132 JF |
6266 | /* Calculate sign/unsigned bounds and tnum for alu32 and alu64 bit ops. |
6267 | * There are two classes of instructions: The first class we track both | |
6268 | * alu32 and alu64 sign/unsigned bounds independently this provides the | |
6269 | * greatest amount of precision when alu operations are mixed with jmp32 | |
6270 | * operations. These operations are BPF_ADD, BPF_SUB, BPF_MUL, BPF_ADD, | |
6271 | * and BPF_OR. This is possible because these ops have fairly easy to | |
6272 | * understand and calculate behavior in both 32-bit and 64-bit alu ops. | |
6273 | * See alu32 verifier tests for examples. The second class of | |
6274 | * operations, BPF_LSH, BPF_RSH, and BPF_ARSH, however are not so easy | |
6275 | * with regards to tracking sign/unsigned bounds because the bits may | |
6276 | * cross subreg boundaries in the alu64 case. When this happens we mark | |
6277 | * the reg unbounded in the subreg bound space and use the resulting | |
6278 | * tnum to calculate an approximation of the sign/unsigned bounds. | |
6279 | */ | |
48461135 JB |
6280 | switch (opcode) { |
6281 | case BPF_ADD: | |
d3bd7413 DB |
6282 | ret = sanitize_val_alu(env, insn); |
6283 | if (ret < 0) { | |
6284 | verbose(env, "R%d tried to add from different pointers or scalars\n", dst); | |
6285 | return ret; | |
6286 | } | |
3f50f132 | 6287 | scalar32_min_max_add(dst_reg, &src_reg); |
07cd2631 | 6288 | scalar_min_max_add(dst_reg, &src_reg); |
3f50f132 | 6289 | dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off); |
48461135 JB |
6290 | break; |
6291 | case BPF_SUB: | |
d3bd7413 DB |
6292 | ret = sanitize_val_alu(env, insn); |
6293 | if (ret < 0) { | |
6294 | verbose(env, "R%d tried to sub from different pointers or scalars\n", dst); | |
6295 | return ret; | |
6296 | } | |
3f50f132 | 6297 | scalar32_min_max_sub(dst_reg, &src_reg); |
07cd2631 | 6298 | scalar_min_max_sub(dst_reg, &src_reg); |
3f50f132 | 6299 | dst_reg->var_off = tnum_sub(dst_reg->var_off, src_reg.var_off); |
48461135 JB |
6300 | break; |
6301 | case BPF_MUL: | |
3f50f132 JF |
6302 | dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off); |
6303 | scalar32_min_max_mul(dst_reg, &src_reg); | |
07cd2631 | 6304 | scalar_min_max_mul(dst_reg, &src_reg); |
48461135 JB |
6305 | break; |
6306 | case BPF_AND: | |
3f50f132 JF |
6307 | dst_reg->var_off = tnum_and(dst_reg->var_off, src_reg.var_off); |
6308 | scalar32_min_max_and(dst_reg, &src_reg); | |
07cd2631 | 6309 | scalar_min_max_and(dst_reg, &src_reg); |
f1174f77 EC |
6310 | break; |
6311 | case BPF_OR: | |
3f50f132 JF |
6312 | dst_reg->var_off = tnum_or(dst_reg->var_off, src_reg.var_off); |
6313 | scalar32_min_max_or(dst_reg, &src_reg); | |
07cd2631 | 6314 | scalar_min_max_or(dst_reg, &src_reg); |
48461135 | 6315 | break; |
2921c90d YS |
6316 | case BPF_XOR: |
6317 | dst_reg->var_off = tnum_xor(dst_reg->var_off, src_reg.var_off); | |
6318 | scalar32_min_max_xor(dst_reg, &src_reg); | |
6319 | scalar_min_max_xor(dst_reg, &src_reg); | |
6320 | break; | |
48461135 | 6321 | case BPF_LSH: |
468f6eaf JH |
6322 | if (umax_val >= insn_bitness) { |
6323 | /* Shifts greater than 31 or 63 are undefined. | |
6324 | * This includes shifts by a negative number. | |
b03c9f9f | 6325 | */ |
61bd5218 | 6326 | mark_reg_unknown(env, regs, insn->dst_reg); |
f1174f77 EC |
6327 | break; |
6328 | } | |
3f50f132 JF |
6329 | if (alu32) |
6330 | scalar32_min_max_lsh(dst_reg, &src_reg); | |
6331 | else | |
6332 | scalar_min_max_lsh(dst_reg, &src_reg); | |
48461135 JB |
6333 | break; |
6334 | case BPF_RSH: | |
468f6eaf JH |
6335 | if (umax_val >= insn_bitness) { |
6336 | /* Shifts greater than 31 or 63 are undefined. | |
6337 | * This includes shifts by a negative number. | |
b03c9f9f | 6338 | */ |
61bd5218 | 6339 | mark_reg_unknown(env, regs, insn->dst_reg); |
f1174f77 EC |
6340 | break; |
6341 | } | |
3f50f132 JF |
6342 | if (alu32) |
6343 | scalar32_min_max_rsh(dst_reg, &src_reg); | |
6344 | else | |
6345 | scalar_min_max_rsh(dst_reg, &src_reg); | |
48461135 | 6346 | break; |
9cbe1f5a YS |
6347 | case BPF_ARSH: |
6348 | if (umax_val >= insn_bitness) { | |
6349 | /* Shifts greater than 31 or 63 are undefined. | |
6350 | * This includes shifts by a negative number. | |
6351 | */ | |
6352 | mark_reg_unknown(env, regs, insn->dst_reg); | |
6353 | break; | |
6354 | } | |
3f50f132 JF |
6355 | if (alu32) |
6356 | scalar32_min_max_arsh(dst_reg, &src_reg); | |
6357 | else | |
6358 | scalar_min_max_arsh(dst_reg, &src_reg); | |
9cbe1f5a | 6359 | break; |
48461135 | 6360 | default: |
61bd5218 | 6361 | mark_reg_unknown(env, regs, insn->dst_reg); |
48461135 JB |
6362 | break; |
6363 | } | |
6364 | ||
3f50f132 JF |
6365 | /* ALU32 ops are zero extended into 64bit register */ |
6366 | if (alu32) | |
6367 | zext_32_to_64(dst_reg); | |
468f6eaf | 6368 | |
294f2fc6 | 6369 | __update_reg_bounds(dst_reg); |
b03c9f9f EC |
6370 | __reg_deduce_bounds(dst_reg); |
6371 | __reg_bound_offset(dst_reg); | |
f1174f77 EC |
6372 | return 0; |
6373 | } | |
6374 | ||
6375 | /* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max | |
6376 | * and var_off. | |
6377 | */ | |
6378 | static int adjust_reg_min_max_vals(struct bpf_verifier_env *env, | |
6379 | struct bpf_insn *insn) | |
6380 | { | |
f4d7e40a AS |
6381 | struct bpf_verifier_state *vstate = env->cur_state; |
6382 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
6383 | struct bpf_reg_state *regs = state->regs, *dst_reg, *src_reg; | |
f1174f77 EC |
6384 | struct bpf_reg_state *ptr_reg = NULL, off_reg = {0}; |
6385 | u8 opcode = BPF_OP(insn->code); | |
b5dc0163 | 6386 | int err; |
f1174f77 EC |
6387 | |
6388 | dst_reg = ®s[insn->dst_reg]; | |
f1174f77 EC |
6389 | src_reg = NULL; |
6390 | if (dst_reg->type != SCALAR_VALUE) | |
6391 | ptr_reg = dst_reg; | |
6392 | if (BPF_SRC(insn->code) == BPF_X) { | |
6393 | src_reg = ®s[insn->src_reg]; | |
f1174f77 EC |
6394 | if (src_reg->type != SCALAR_VALUE) { |
6395 | if (dst_reg->type != SCALAR_VALUE) { | |
6396 | /* Combining two pointers by any ALU op yields | |
82abbf8d AS |
6397 | * an arbitrary scalar. Disallow all math except |
6398 | * pointer subtraction | |
f1174f77 | 6399 | */ |
dd066823 | 6400 | if (opcode == BPF_SUB && env->allow_ptr_leaks) { |
82abbf8d AS |
6401 | mark_reg_unknown(env, regs, insn->dst_reg); |
6402 | return 0; | |
f1174f77 | 6403 | } |
82abbf8d AS |
6404 | verbose(env, "R%d pointer %s pointer prohibited\n", |
6405 | insn->dst_reg, | |
6406 | bpf_alu_string[opcode >> 4]); | |
6407 | return -EACCES; | |
f1174f77 EC |
6408 | } else { |
6409 | /* scalar += pointer | |
6410 | * This is legal, but we have to reverse our | |
6411 | * src/dest handling in computing the range | |
6412 | */ | |
b5dc0163 AS |
6413 | err = mark_chain_precision(env, insn->dst_reg); |
6414 | if (err) | |
6415 | return err; | |
82abbf8d AS |
6416 | return adjust_ptr_min_max_vals(env, insn, |
6417 | src_reg, dst_reg); | |
f1174f77 EC |
6418 | } |
6419 | } else if (ptr_reg) { | |
6420 | /* pointer += scalar */ | |
b5dc0163 AS |
6421 | err = mark_chain_precision(env, insn->src_reg); |
6422 | if (err) | |
6423 | return err; | |
82abbf8d AS |
6424 | return adjust_ptr_min_max_vals(env, insn, |
6425 | dst_reg, src_reg); | |
f1174f77 EC |
6426 | } |
6427 | } else { | |
6428 | /* Pretend the src is a reg with a known value, since we only | |
6429 | * need to be able to read from this state. | |
6430 | */ | |
6431 | off_reg.type = SCALAR_VALUE; | |
b03c9f9f | 6432 | __mark_reg_known(&off_reg, insn->imm); |
f1174f77 | 6433 | src_reg = &off_reg; |
82abbf8d AS |
6434 | if (ptr_reg) /* pointer += K */ |
6435 | return adjust_ptr_min_max_vals(env, insn, | |
6436 | ptr_reg, src_reg); | |
f1174f77 EC |
6437 | } |
6438 | ||
6439 | /* Got here implies adding two SCALAR_VALUEs */ | |
6440 | if (WARN_ON_ONCE(ptr_reg)) { | |
f4d7e40a | 6441 | print_verifier_state(env, state); |
61bd5218 | 6442 | verbose(env, "verifier internal error: unexpected ptr_reg\n"); |
f1174f77 EC |
6443 | return -EINVAL; |
6444 | } | |
6445 | if (WARN_ON(!src_reg)) { | |
f4d7e40a | 6446 | print_verifier_state(env, state); |
61bd5218 | 6447 | verbose(env, "verifier internal error: no src_reg\n"); |
f1174f77 EC |
6448 | return -EINVAL; |
6449 | } | |
6450 | return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg); | |
48461135 JB |
6451 | } |
6452 | ||
17a52670 | 6453 | /* check validity of 32-bit and 64-bit arithmetic operations */ |
58e2af8b | 6454 | static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn) |
17a52670 | 6455 | { |
638f5b90 | 6456 | struct bpf_reg_state *regs = cur_regs(env); |
17a52670 AS |
6457 | u8 opcode = BPF_OP(insn->code); |
6458 | int err; | |
6459 | ||
6460 | if (opcode == BPF_END || opcode == BPF_NEG) { | |
6461 | if (opcode == BPF_NEG) { | |
6462 | if (BPF_SRC(insn->code) != 0 || | |
6463 | insn->src_reg != BPF_REG_0 || | |
6464 | insn->off != 0 || insn->imm != 0) { | |
61bd5218 | 6465 | verbose(env, "BPF_NEG uses reserved fields\n"); |
17a52670 AS |
6466 | return -EINVAL; |
6467 | } | |
6468 | } else { | |
6469 | if (insn->src_reg != BPF_REG_0 || insn->off != 0 || | |
e67b8a68 EC |
6470 | (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) || |
6471 | BPF_CLASS(insn->code) == BPF_ALU64) { | |
61bd5218 | 6472 | verbose(env, "BPF_END uses reserved fields\n"); |
17a52670 AS |
6473 | return -EINVAL; |
6474 | } | |
6475 | } | |
6476 | ||
6477 | /* check src operand */ | |
dc503a8a | 6478 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
6479 | if (err) |
6480 | return err; | |
6481 | ||
1be7f75d | 6482 | if (is_pointer_value(env, insn->dst_reg)) { |
61bd5218 | 6483 | verbose(env, "R%d pointer arithmetic prohibited\n", |
1be7f75d AS |
6484 | insn->dst_reg); |
6485 | return -EACCES; | |
6486 | } | |
6487 | ||
17a52670 | 6488 | /* check dest operand */ |
dc503a8a | 6489 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
17a52670 AS |
6490 | if (err) |
6491 | return err; | |
6492 | ||
6493 | } else if (opcode == BPF_MOV) { | |
6494 | ||
6495 | if (BPF_SRC(insn->code) == BPF_X) { | |
6496 | if (insn->imm != 0 || insn->off != 0) { | |
61bd5218 | 6497 | verbose(env, "BPF_MOV uses reserved fields\n"); |
17a52670 AS |
6498 | return -EINVAL; |
6499 | } | |
6500 | ||
6501 | /* check src operand */ | |
dc503a8a | 6502 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
6503 | if (err) |
6504 | return err; | |
6505 | } else { | |
6506 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { | |
61bd5218 | 6507 | verbose(env, "BPF_MOV uses reserved fields\n"); |
17a52670 AS |
6508 | return -EINVAL; |
6509 | } | |
6510 | } | |
6511 | ||
fbeb1603 AF |
6512 | /* check dest operand, mark as required later */ |
6513 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); | |
17a52670 AS |
6514 | if (err) |
6515 | return err; | |
6516 | ||
6517 | if (BPF_SRC(insn->code) == BPF_X) { | |
e434b8cd JW |
6518 | struct bpf_reg_state *src_reg = regs + insn->src_reg; |
6519 | struct bpf_reg_state *dst_reg = regs + insn->dst_reg; | |
6520 | ||
17a52670 AS |
6521 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
6522 | /* case: R1 = R2 | |
6523 | * copy register state to dest reg | |
6524 | */ | |
e434b8cd JW |
6525 | *dst_reg = *src_reg; |
6526 | dst_reg->live |= REG_LIVE_WRITTEN; | |
5327ed3d | 6527 | dst_reg->subreg_def = DEF_NOT_SUBREG; |
17a52670 | 6528 | } else { |
f1174f77 | 6529 | /* R1 = (u32) R2 */ |
1be7f75d | 6530 | if (is_pointer_value(env, insn->src_reg)) { |
61bd5218 JK |
6531 | verbose(env, |
6532 | "R%d partial copy of pointer\n", | |
1be7f75d AS |
6533 | insn->src_reg); |
6534 | return -EACCES; | |
e434b8cd JW |
6535 | } else if (src_reg->type == SCALAR_VALUE) { |
6536 | *dst_reg = *src_reg; | |
6537 | dst_reg->live |= REG_LIVE_WRITTEN; | |
5327ed3d | 6538 | dst_reg->subreg_def = env->insn_idx + 1; |
e434b8cd JW |
6539 | } else { |
6540 | mark_reg_unknown(env, regs, | |
6541 | insn->dst_reg); | |
1be7f75d | 6542 | } |
3f50f132 | 6543 | zext_32_to_64(dst_reg); |
17a52670 AS |
6544 | } |
6545 | } else { | |
6546 | /* case: R = imm | |
6547 | * remember the value we stored into this reg | |
6548 | */ | |
fbeb1603 AF |
6549 | /* clear any state __mark_reg_known doesn't set */ |
6550 | mark_reg_unknown(env, regs, insn->dst_reg); | |
f1174f77 | 6551 | regs[insn->dst_reg].type = SCALAR_VALUE; |
95a762e2 JH |
6552 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
6553 | __mark_reg_known(regs + insn->dst_reg, | |
6554 | insn->imm); | |
6555 | } else { | |
6556 | __mark_reg_known(regs + insn->dst_reg, | |
6557 | (u32)insn->imm); | |
6558 | } | |
17a52670 AS |
6559 | } |
6560 | ||
6561 | } else if (opcode > BPF_END) { | |
61bd5218 | 6562 | verbose(env, "invalid BPF_ALU opcode %x\n", opcode); |
17a52670 AS |
6563 | return -EINVAL; |
6564 | ||
6565 | } else { /* all other ALU ops: and, sub, xor, add, ... */ | |
6566 | ||
17a52670 AS |
6567 | if (BPF_SRC(insn->code) == BPF_X) { |
6568 | if (insn->imm != 0 || insn->off != 0) { | |
61bd5218 | 6569 | verbose(env, "BPF_ALU uses reserved fields\n"); |
17a52670 AS |
6570 | return -EINVAL; |
6571 | } | |
6572 | /* check src1 operand */ | |
dc503a8a | 6573 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
6574 | if (err) |
6575 | return err; | |
6576 | } else { | |
6577 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { | |
61bd5218 | 6578 | verbose(env, "BPF_ALU uses reserved fields\n"); |
17a52670 AS |
6579 | return -EINVAL; |
6580 | } | |
6581 | } | |
6582 | ||
6583 | /* check src2 operand */ | |
dc503a8a | 6584 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
6585 | if (err) |
6586 | return err; | |
6587 | ||
6588 | if ((opcode == BPF_MOD || opcode == BPF_DIV) && | |
6589 | BPF_SRC(insn->code) == BPF_K && insn->imm == 0) { | |
61bd5218 | 6590 | verbose(env, "div by zero\n"); |
17a52670 AS |
6591 | return -EINVAL; |
6592 | } | |
6593 | ||
229394e8 RV |
6594 | if ((opcode == BPF_LSH || opcode == BPF_RSH || |
6595 | opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) { | |
6596 | int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32; | |
6597 | ||
6598 | if (insn->imm < 0 || insn->imm >= size) { | |
61bd5218 | 6599 | verbose(env, "invalid shift %d\n", insn->imm); |
229394e8 RV |
6600 | return -EINVAL; |
6601 | } | |
6602 | } | |
6603 | ||
1a0dc1ac | 6604 | /* check dest operand */ |
dc503a8a | 6605 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
1a0dc1ac AS |
6606 | if (err) |
6607 | return err; | |
6608 | ||
f1174f77 | 6609 | return adjust_reg_min_max_vals(env, insn); |
17a52670 AS |
6610 | } |
6611 | ||
6612 | return 0; | |
6613 | } | |
6614 | ||
c6a9efa1 PC |
6615 | static void __find_good_pkt_pointers(struct bpf_func_state *state, |
6616 | struct bpf_reg_state *dst_reg, | |
6617 | enum bpf_reg_type type, u16 new_range) | |
6618 | { | |
6619 | struct bpf_reg_state *reg; | |
6620 | int i; | |
6621 | ||
6622 | for (i = 0; i < MAX_BPF_REG; i++) { | |
6623 | reg = &state->regs[i]; | |
6624 | if (reg->type == type && reg->id == dst_reg->id) | |
6625 | /* keep the maximum range already checked */ | |
6626 | reg->range = max(reg->range, new_range); | |
6627 | } | |
6628 | ||
6629 | bpf_for_each_spilled_reg(i, state, reg) { | |
6630 | if (!reg) | |
6631 | continue; | |
6632 | if (reg->type == type && reg->id == dst_reg->id) | |
6633 | reg->range = max(reg->range, new_range); | |
6634 | } | |
6635 | } | |
6636 | ||
f4d7e40a | 6637 | static void find_good_pkt_pointers(struct bpf_verifier_state *vstate, |
de8f3a83 | 6638 | struct bpf_reg_state *dst_reg, |
f8ddadc4 | 6639 | enum bpf_reg_type type, |
fb2a311a | 6640 | bool range_right_open) |
969bf05e | 6641 | { |
fb2a311a | 6642 | u16 new_range; |
c6a9efa1 | 6643 | int i; |
2d2be8ca | 6644 | |
fb2a311a DB |
6645 | if (dst_reg->off < 0 || |
6646 | (dst_reg->off == 0 && range_right_open)) | |
f1174f77 EC |
6647 | /* This doesn't give us any range */ |
6648 | return; | |
6649 | ||
b03c9f9f EC |
6650 | if (dst_reg->umax_value > MAX_PACKET_OFF || |
6651 | dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF) | |
f1174f77 EC |
6652 | /* Risk of overflow. For instance, ptr + (1<<63) may be less |
6653 | * than pkt_end, but that's because it's also less than pkt. | |
6654 | */ | |
6655 | return; | |
6656 | ||
fb2a311a DB |
6657 | new_range = dst_reg->off; |
6658 | if (range_right_open) | |
6659 | new_range--; | |
6660 | ||
6661 | /* Examples for register markings: | |
2d2be8ca | 6662 | * |
fb2a311a | 6663 | * pkt_data in dst register: |
2d2be8ca DB |
6664 | * |
6665 | * r2 = r3; | |
6666 | * r2 += 8; | |
6667 | * if (r2 > pkt_end) goto <handle exception> | |
6668 | * <access okay> | |
6669 | * | |
b4e432f1 DB |
6670 | * r2 = r3; |
6671 | * r2 += 8; | |
6672 | * if (r2 < pkt_end) goto <access okay> | |
6673 | * <handle exception> | |
6674 | * | |
2d2be8ca DB |
6675 | * Where: |
6676 | * r2 == dst_reg, pkt_end == src_reg | |
6677 | * r2=pkt(id=n,off=8,r=0) | |
6678 | * r3=pkt(id=n,off=0,r=0) | |
6679 | * | |
fb2a311a | 6680 | * pkt_data in src register: |
2d2be8ca DB |
6681 | * |
6682 | * r2 = r3; | |
6683 | * r2 += 8; | |
6684 | * if (pkt_end >= r2) goto <access okay> | |
6685 | * <handle exception> | |
6686 | * | |
b4e432f1 DB |
6687 | * r2 = r3; |
6688 | * r2 += 8; | |
6689 | * if (pkt_end <= r2) goto <handle exception> | |
6690 | * <access okay> | |
6691 | * | |
2d2be8ca DB |
6692 | * Where: |
6693 | * pkt_end == dst_reg, r2 == src_reg | |
6694 | * r2=pkt(id=n,off=8,r=0) | |
6695 | * r3=pkt(id=n,off=0,r=0) | |
6696 | * | |
6697 | * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8) | |
fb2a311a DB |
6698 | * or r3=pkt(id=n,off=0,r=8-1), so that range of bytes [r3, r3 + 8) |
6699 | * and [r3, r3 + 8-1) respectively is safe to access depending on | |
6700 | * the check. | |
969bf05e | 6701 | */ |
2d2be8ca | 6702 | |
f1174f77 EC |
6703 | /* If our ids match, then we must have the same max_value. And we |
6704 | * don't care about the other reg's fixed offset, since if it's too big | |
6705 | * the range won't allow anything. | |
6706 | * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16. | |
6707 | */ | |
c6a9efa1 PC |
6708 | for (i = 0; i <= vstate->curframe; i++) |
6709 | __find_good_pkt_pointers(vstate->frame[i], dst_reg, type, | |
6710 | new_range); | |
969bf05e AS |
6711 | } |
6712 | ||
3f50f132 | 6713 | static int is_branch32_taken(struct bpf_reg_state *reg, u32 val, u8 opcode) |
4f7b3e82 | 6714 | { |
3f50f132 JF |
6715 | struct tnum subreg = tnum_subreg(reg->var_off); |
6716 | s32 sval = (s32)val; | |
a72dafaf | 6717 | |
3f50f132 JF |
6718 | switch (opcode) { |
6719 | case BPF_JEQ: | |
6720 | if (tnum_is_const(subreg)) | |
6721 | return !!tnum_equals_const(subreg, val); | |
6722 | break; | |
6723 | case BPF_JNE: | |
6724 | if (tnum_is_const(subreg)) | |
6725 | return !tnum_equals_const(subreg, val); | |
6726 | break; | |
6727 | case BPF_JSET: | |
6728 | if ((~subreg.mask & subreg.value) & val) | |
6729 | return 1; | |
6730 | if (!((subreg.mask | subreg.value) & val)) | |
6731 | return 0; | |
6732 | break; | |
6733 | case BPF_JGT: | |
6734 | if (reg->u32_min_value > val) | |
6735 | return 1; | |
6736 | else if (reg->u32_max_value <= val) | |
6737 | return 0; | |
6738 | break; | |
6739 | case BPF_JSGT: | |
6740 | if (reg->s32_min_value > sval) | |
6741 | return 1; | |
6742 | else if (reg->s32_max_value < sval) | |
6743 | return 0; | |
6744 | break; | |
6745 | case BPF_JLT: | |
6746 | if (reg->u32_max_value < val) | |
6747 | return 1; | |
6748 | else if (reg->u32_min_value >= val) | |
6749 | return 0; | |
6750 | break; | |
6751 | case BPF_JSLT: | |
6752 | if (reg->s32_max_value < sval) | |
6753 | return 1; | |
6754 | else if (reg->s32_min_value >= sval) | |
6755 | return 0; | |
6756 | break; | |
6757 | case BPF_JGE: | |
6758 | if (reg->u32_min_value >= val) | |
6759 | return 1; | |
6760 | else if (reg->u32_max_value < val) | |
6761 | return 0; | |
6762 | break; | |
6763 | case BPF_JSGE: | |
6764 | if (reg->s32_min_value >= sval) | |
6765 | return 1; | |
6766 | else if (reg->s32_max_value < sval) | |
6767 | return 0; | |
6768 | break; | |
6769 | case BPF_JLE: | |
6770 | if (reg->u32_max_value <= val) | |
6771 | return 1; | |
6772 | else if (reg->u32_min_value > val) | |
6773 | return 0; | |
6774 | break; | |
6775 | case BPF_JSLE: | |
6776 | if (reg->s32_max_value <= sval) | |
6777 | return 1; | |
6778 | else if (reg->s32_min_value > sval) | |
6779 | return 0; | |
6780 | break; | |
6781 | } | |
4f7b3e82 | 6782 | |
3f50f132 JF |
6783 | return -1; |
6784 | } | |
092ed096 | 6785 | |
3f50f132 JF |
6786 | |
6787 | static int is_branch64_taken(struct bpf_reg_state *reg, u64 val, u8 opcode) | |
6788 | { | |
6789 | s64 sval = (s64)val; | |
a72dafaf | 6790 | |
4f7b3e82 AS |
6791 | switch (opcode) { |
6792 | case BPF_JEQ: | |
6793 | if (tnum_is_const(reg->var_off)) | |
6794 | return !!tnum_equals_const(reg->var_off, val); | |
6795 | break; | |
6796 | case BPF_JNE: | |
6797 | if (tnum_is_const(reg->var_off)) | |
6798 | return !tnum_equals_const(reg->var_off, val); | |
6799 | break; | |
960ea056 JK |
6800 | case BPF_JSET: |
6801 | if ((~reg->var_off.mask & reg->var_off.value) & val) | |
6802 | return 1; | |
6803 | if (!((reg->var_off.mask | reg->var_off.value) & val)) | |
6804 | return 0; | |
6805 | break; | |
4f7b3e82 AS |
6806 | case BPF_JGT: |
6807 | if (reg->umin_value > val) | |
6808 | return 1; | |
6809 | else if (reg->umax_value <= val) | |
6810 | return 0; | |
6811 | break; | |
6812 | case BPF_JSGT: | |
a72dafaf | 6813 | if (reg->smin_value > sval) |
4f7b3e82 | 6814 | return 1; |
a72dafaf | 6815 | else if (reg->smax_value < sval) |
4f7b3e82 AS |
6816 | return 0; |
6817 | break; | |
6818 | case BPF_JLT: | |
6819 | if (reg->umax_value < val) | |
6820 | return 1; | |
6821 | else if (reg->umin_value >= val) | |
6822 | return 0; | |
6823 | break; | |
6824 | case BPF_JSLT: | |
a72dafaf | 6825 | if (reg->smax_value < sval) |
4f7b3e82 | 6826 | return 1; |
a72dafaf | 6827 | else if (reg->smin_value >= sval) |
4f7b3e82 AS |
6828 | return 0; |
6829 | break; | |
6830 | case BPF_JGE: | |
6831 | if (reg->umin_value >= val) | |
6832 | return 1; | |
6833 | else if (reg->umax_value < val) | |
6834 | return 0; | |
6835 | break; | |
6836 | case BPF_JSGE: | |
a72dafaf | 6837 | if (reg->smin_value >= sval) |
4f7b3e82 | 6838 | return 1; |
a72dafaf | 6839 | else if (reg->smax_value < sval) |
4f7b3e82 AS |
6840 | return 0; |
6841 | break; | |
6842 | case BPF_JLE: | |
6843 | if (reg->umax_value <= val) | |
6844 | return 1; | |
6845 | else if (reg->umin_value > val) | |
6846 | return 0; | |
6847 | break; | |
6848 | case BPF_JSLE: | |
a72dafaf | 6849 | if (reg->smax_value <= sval) |
4f7b3e82 | 6850 | return 1; |
a72dafaf | 6851 | else if (reg->smin_value > sval) |
4f7b3e82 AS |
6852 | return 0; |
6853 | break; | |
6854 | } | |
6855 | ||
6856 | return -1; | |
6857 | } | |
6858 | ||
3f50f132 JF |
6859 | /* compute branch direction of the expression "if (reg opcode val) goto target;" |
6860 | * and return: | |
6861 | * 1 - branch will be taken and "goto target" will be executed | |
6862 | * 0 - branch will not be taken and fall-through to next insn | |
6863 | * -1 - unknown. Example: "if (reg < 5)" is unknown when register value | |
6864 | * range [0,10] | |
604dca5e | 6865 | */ |
3f50f132 JF |
6866 | static int is_branch_taken(struct bpf_reg_state *reg, u64 val, u8 opcode, |
6867 | bool is_jmp32) | |
604dca5e | 6868 | { |
cac616db JF |
6869 | if (__is_pointer_value(false, reg)) { |
6870 | if (!reg_type_not_null(reg->type)) | |
6871 | return -1; | |
6872 | ||
6873 | /* If pointer is valid tests against zero will fail so we can | |
6874 | * use this to direct branch taken. | |
6875 | */ | |
6876 | if (val != 0) | |
6877 | return -1; | |
6878 | ||
6879 | switch (opcode) { | |
6880 | case BPF_JEQ: | |
6881 | return 0; | |
6882 | case BPF_JNE: | |
6883 | return 1; | |
6884 | default: | |
6885 | return -1; | |
6886 | } | |
6887 | } | |
604dca5e | 6888 | |
3f50f132 JF |
6889 | if (is_jmp32) |
6890 | return is_branch32_taken(reg, val, opcode); | |
6891 | return is_branch64_taken(reg, val, opcode); | |
604dca5e JH |
6892 | } |
6893 | ||
48461135 JB |
6894 | /* Adjusts the register min/max values in the case that the dst_reg is the |
6895 | * variable register that we are working on, and src_reg is a constant or we're | |
6896 | * simply doing a BPF_K check. | |
f1174f77 | 6897 | * In JEQ/JNE cases we also adjust the var_off values. |
48461135 JB |
6898 | */ |
6899 | static void reg_set_min_max(struct bpf_reg_state *true_reg, | |
3f50f132 JF |
6900 | struct bpf_reg_state *false_reg, |
6901 | u64 val, u32 val32, | |
092ed096 | 6902 | u8 opcode, bool is_jmp32) |
48461135 | 6903 | { |
3f50f132 JF |
6904 | struct tnum false_32off = tnum_subreg(false_reg->var_off); |
6905 | struct tnum false_64off = false_reg->var_off; | |
6906 | struct tnum true_32off = tnum_subreg(true_reg->var_off); | |
6907 | struct tnum true_64off = true_reg->var_off; | |
6908 | s64 sval = (s64)val; | |
6909 | s32 sval32 = (s32)val32; | |
a72dafaf | 6910 | |
f1174f77 EC |
6911 | /* If the dst_reg is a pointer, we can't learn anything about its |
6912 | * variable offset from the compare (unless src_reg were a pointer into | |
6913 | * the same object, but we don't bother with that. | |
6914 | * Since false_reg and true_reg have the same type by construction, we | |
6915 | * only need to check one of them for pointerness. | |
6916 | */ | |
6917 | if (__is_pointer_value(false, false_reg)) | |
6918 | return; | |
4cabc5b1 | 6919 | |
48461135 JB |
6920 | switch (opcode) { |
6921 | case BPF_JEQ: | |
48461135 | 6922 | case BPF_JNE: |
a72dafaf JW |
6923 | { |
6924 | struct bpf_reg_state *reg = | |
6925 | opcode == BPF_JEQ ? true_reg : false_reg; | |
6926 | ||
6927 | /* For BPF_JEQ, if this is false we know nothing Jon Snow, but | |
6928 | * if it is true we know the value for sure. Likewise for | |
6929 | * BPF_JNE. | |
48461135 | 6930 | */ |
3f50f132 JF |
6931 | if (is_jmp32) |
6932 | __mark_reg32_known(reg, val32); | |
6933 | else | |
092ed096 | 6934 | __mark_reg_known(reg, val); |
48461135 | 6935 | break; |
a72dafaf | 6936 | } |
960ea056 | 6937 | case BPF_JSET: |
3f50f132 JF |
6938 | if (is_jmp32) { |
6939 | false_32off = tnum_and(false_32off, tnum_const(~val32)); | |
6940 | if (is_power_of_2(val32)) | |
6941 | true_32off = tnum_or(true_32off, | |
6942 | tnum_const(val32)); | |
6943 | } else { | |
6944 | false_64off = tnum_and(false_64off, tnum_const(~val)); | |
6945 | if (is_power_of_2(val)) | |
6946 | true_64off = tnum_or(true_64off, | |
6947 | tnum_const(val)); | |
6948 | } | |
960ea056 | 6949 | break; |
48461135 | 6950 | case BPF_JGE: |
a72dafaf JW |
6951 | case BPF_JGT: |
6952 | { | |
3f50f132 JF |
6953 | if (is_jmp32) { |
6954 | u32 false_umax = opcode == BPF_JGT ? val32 : val32 - 1; | |
6955 | u32 true_umin = opcode == BPF_JGT ? val32 + 1 : val32; | |
6956 | ||
6957 | false_reg->u32_max_value = min(false_reg->u32_max_value, | |
6958 | false_umax); | |
6959 | true_reg->u32_min_value = max(true_reg->u32_min_value, | |
6960 | true_umin); | |
6961 | } else { | |
6962 | u64 false_umax = opcode == BPF_JGT ? val : val - 1; | |
6963 | u64 true_umin = opcode == BPF_JGT ? val + 1 : val; | |
6964 | ||
6965 | false_reg->umax_value = min(false_reg->umax_value, false_umax); | |
6966 | true_reg->umin_value = max(true_reg->umin_value, true_umin); | |
6967 | } | |
b03c9f9f | 6968 | break; |
a72dafaf | 6969 | } |
48461135 | 6970 | case BPF_JSGE: |
a72dafaf JW |
6971 | case BPF_JSGT: |
6972 | { | |
3f50f132 JF |
6973 | if (is_jmp32) { |
6974 | s32 false_smax = opcode == BPF_JSGT ? sval32 : sval32 - 1; | |
6975 | s32 true_smin = opcode == BPF_JSGT ? sval32 + 1 : sval32; | |
a72dafaf | 6976 | |
3f50f132 JF |
6977 | false_reg->s32_max_value = min(false_reg->s32_max_value, false_smax); |
6978 | true_reg->s32_min_value = max(true_reg->s32_min_value, true_smin); | |
6979 | } else { | |
6980 | s64 false_smax = opcode == BPF_JSGT ? sval : sval - 1; | |
6981 | s64 true_smin = opcode == BPF_JSGT ? sval + 1 : sval; | |
6982 | ||
6983 | false_reg->smax_value = min(false_reg->smax_value, false_smax); | |
6984 | true_reg->smin_value = max(true_reg->smin_value, true_smin); | |
6985 | } | |
48461135 | 6986 | break; |
a72dafaf | 6987 | } |
b4e432f1 | 6988 | case BPF_JLE: |
a72dafaf JW |
6989 | case BPF_JLT: |
6990 | { | |
3f50f132 JF |
6991 | if (is_jmp32) { |
6992 | u32 false_umin = opcode == BPF_JLT ? val32 : val32 + 1; | |
6993 | u32 true_umax = opcode == BPF_JLT ? val32 - 1 : val32; | |
6994 | ||
6995 | false_reg->u32_min_value = max(false_reg->u32_min_value, | |
6996 | false_umin); | |
6997 | true_reg->u32_max_value = min(true_reg->u32_max_value, | |
6998 | true_umax); | |
6999 | } else { | |
7000 | u64 false_umin = opcode == BPF_JLT ? val : val + 1; | |
7001 | u64 true_umax = opcode == BPF_JLT ? val - 1 : val; | |
7002 | ||
7003 | false_reg->umin_value = max(false_reg->umin_value, false_umin); | |
7004 | true_reg->umax_value = min(true_reg->umax_value, true_umax); | |
7005 | } | |
b4e432f1 | 7006 | break; |
a72dafaf | 7007 | } |
b4e432f1 | 7008 | case BPF_JSLE: |
a72dafaf JW |
7009 | case BPF_JSLT: |
7010 | { | |
3f50f132 JF |
7011 | if (is_jmp32) { |
7012 | s32 false_smin = opcode == BPF_JSLT ? sval32 : sval32 + 1; | |
7013 | s32 true_smax = opcode == BPF_JSLT ? sval32 - 1 : sval32; | |
a72dafaf | 7014 | |
3f50f132 JF |
7015 | false_reg->s32_min_value = max(false_reg->s32_min_value, false_smin); |
7016 | true_reg->s32_max_value = min(true_reg->s32_max_value, true_smax); | |
7017 | } else { | |
7018 | s64 false_smin = opcode == BPF_JSLT ? sval : sval + 1; | |
7019 | s64 true_smax = opcode == BPF_JSLT ? sval - 1 : sval; | |
7020 | ||
7021 | false_reg->smin_value = max(false_reg->smin_value, false_smin); | |
7022 | true_reg->smax_value = min(true_reg->smax_value, true_smax); | |
7023 | } | |
b4e432f1 | 7024 | break; |
a72dafaf | 7025 | } |
48461135 | 7026 | default: |
0fc31b10 | 7027 | return; |
48461135 JB |
7028 | } |
7029 | ||
3f50f132 JF |
7030 | if (is_jmp32) { |
7031 | false_reg->var_off = tnum_or(tnum_clear_subreg(false_64off), | |
7032 | tnum_subreg(false_32off)); | |
7033 | true_reg->var_off = tnum_or(tnum_clear_subreg(true_64off), | |
7034 | tnum_subreg(true_32off)); | |
7035 | __reg_combine_32_into_64(false_reg); | |
7036 | __reg_combine_32_into_64(true_reg); | |
7037 | } else { | |
7038 | false_reg->var_off = false_64off; | |
7039 | true_reg->var_off = true_64off; | |
7040 | __reg_combine_64_into_32(false_reg); | |
7041 | __reg_combine_64_into_32(true_reg); | |
7042 | } | |
48461135 JB |
7043 | } |
7044 | ||
f1174f77 EC |
7045 | /* Same as above, but for the case that dst_reg holds a constant and src_reg is |
7046 | * the variable reg. | |
48461135 JB |
7047 | */ |
7048 | static void reg_set_min_max_inv(struct bpf_reg_state *true_reg, | |
3f50f132 JF |
7049 | struct bpf_reg_state *false_reg, |
7050 | u64 val, u32 val32, | |
092ed096 | 7051 | u8 opcode, bool is_jmp32) |
48461135 | 7052 | { |
0fc31b10 JH |
7053 | /* How can we transform "a <op> b" into "b <op> a"? */ |
7054 | static const u8 opcode_flip[16] = { | |
7055 | /* these stay the same */ | |
7056 | [BPF_JEQ >> 4] = BPF_JEQ, | |
7057 | [BPF_JNE >> 4] = BPF_JNE, | |
7058 | [BPF_JSET >> 4] = BPF_JSET, | |
7059 | /* these swap "lesser" and "greater" (L and G in the opcodes) */ | |
7060 | [BPF_JGE >> 4] = BPF_JLE, | |
7061 | [BPF_JGT >> 4] = BPF_JLT, | |
7062 | [BPF_JLE >> 4] = BPF_JGE, | |
7063 | [BPF_JLT >> 4] = BPF_JGT, | |
7064 | [BPF_JSGE >> 4] = BPF_JSLE, | |
7065 | [BPF_JSGT >> 4] = BPF_JSLT, | |
7066 | [BPF_JSLE >> 4] = BPF_JSGE, | |
7067 | [BPF_JSLT >> 4] = BPF_JSGT | |
7068 | }; | |
7069 | opcode = opcode_flip[opcode >> 4]; | |
7070 | /* This uses zero as "not present in table"; luckily the zero opcode, | |
7071 | * BPF_JA, can't get here. | |
b03c9f9f | 7072 | */ |
0fc31b10 | 7073 | if (opcode) |
3f50f132 | 7074 | reg_set_min_max(true_reg, false_reg, val, val32, opcode, is_jmp32); |
f1174f77 EC |
7075 | } |
7076 | ||
7077 | /* Regs are known to be equal, so intersect their min/max/var_off */ | |
7078 | static void __reg_combine_min_max(struct bpf_reg_state *src_reg, | |
7079 | struct bpf_reg_state *dst_reg) | |
7080 | { | |
b03c9f9f EC |
7081 | src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value, |
7082 | dst_reg->umin_value); | |
7083 | src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value, | |
7084 | dst_reg->umax_value); | |
7085 | src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value, | |
7086 | dst_reg->smin_value); | |
7087 | src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value, | |
7088 | dst_reg->smax_value); | |
f1174f77 EC |
7089 | src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off, |
7090 | dst_reg->var_off); | |
b03c9f9f EC |
7091 | /* We might have learned new bounds from the var_off. */ |
7092 | __update_reg_bounds(src_reg); | |
7093 | __update_reg_bounds(dst_reg); | |
7094 | /* We might have learned something about the sign bit. */ | |
7095 | __reg_deduce_bounds(src_reg); | |
7096 | __reg_deduce_bounds(dst_reg); | |
7097 | /* We might have learned some bits from the bounds. */ | |
7098 | __reg_bound_offset(src_reg); | |
7099 | __reg_bound_offset(dst_reg); | |
7100 | /* Intersecting with the old var_off might have improved our bounds | |
7101 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), | |
7102 | * then new var_off is (0; 0x7f...fc) which improves our umax. | |
7103 | */ | |
7104 | __update_reg_bounds(src_reg); | |
7105 | __update_reg_bounds(dst_reg); | |
f1174f77 EC |
7106 | } |
7107 | ||
7108 | static void reg_combine_min_max(struct bpf_reg_state *true_src, | |
7109 | struct bpf_reg_state *true_dst, | |
7110 | struct bpf_reg_state *false_src, | |
7111 | struct bpf_reg_state *false_dst, | |
7112 | u8 opcode) | |
7113 | { | |
7114 | switch (opcode) { | |
7115 | case BPF_JEQ: | |
7116 | __reg_combine_min_max(true_src, true_dst); | |
7117 | break; | |
7118 | case BPF_JNE: | |
7119 | __reg_combine_min_max(false_src, false_dst); | |
b03c9f9f | 7120 | break; |
4cabc5b1 | 7121 | } |
48461135 JB |
7122 | } |
7123 | ||
fd978bf7 JS |
7124 | static void mark_ptr_or_null_reg(struct bpf_func_state *state, |
7125 | struct bpf_reg_state *reg, u32 id, | |
840b9615 | 7126 | bool is_null) |
57a09bf0 | 7127 | { |
840b9615 | 7128 | if (reg_type_may_be_null(reg->type) && reg->id == id) { |
f1174f77 EC |
7129 | /* Old offset (both fixed and variable parts) should |
7130 | * have been known-zero, because we don't allow pointer | |
7131 | * arithmetic on pointers that might be NULL. | |
7132 | */ | |
b03c9f9f EC |
7133 | if (WARN_ON_ONCE(reg->smin_value || reg->smax_value || |
7134 | !tnum_equals_const(reg->var_off, 0) || | |
f1174f77 | 7135 | reg->off)) { |
b03c9f9f EC |
7136 | __mark_reg_known_zero(reg); |
7137 | reg->off = 0; | |
f1174f77 EC |
7138 | } |
7139 | if (is_null) { | |
7140 | reg->type = SCALAR_VALUE; | |
840b9615 | 7141 | } else if (reg->type == PTR_TO_MAP_VALUE_OR_NULL) { |
64d85290 JS |
7142 | const struct bpf_map *map = reg->map_ptr; |
7143 | ||
7144 | if (map->inner_map_meta) { | |
840b9615 | 7145 | reg->type = CONST_PTR_TO_MAP; |
64d85290 JS |
7146 | reg->map_ptr = map->inner_map_meta; |
7147 | } else if (map->map_type == BPF_MAP_TYPE_XSKMAP) { | |
fada7fdc | 7148 | reg->type = PTR_TO_XDP_SOCK; |
64d85290 JS |
7149 | } else if (map->map_type == BPF_MAP_TYPE_SOCKMAP || |
7150 | map->map_type == BPF_MAP_TYPE_SOCKHASH) { | |
7151 | reg->type = PTR_TO_SOCKET; | |
840b9615 JS |
7152 | } else { |
7153 | reg->type = PTR_TO_MAP_VALUE; | |
7154 | } | |
c64b7983 JS |
7155 | } else if (reg->type == PTR_TO_SOCKET_OR_NULL) { |
7156 | reg->type = PTR_TO_SOCKET; | |
46f8bc92 MKL |
7157 | } else if (reg->type == PTR_TO_SOCK_COMMON_OR_NULL) { |
7158 | reg->type = PTR_TO_SOCK_COMMON; | |
655a51e5 MKL |
7159 | } else if (reg->type == PTR_TO_TCP_SOCK_OR_NULL) { |
7160 | reg->type = PTR_TO_TCP_SOCK; | |
b121b341 YS |
7161 | } else if (reg->type == PTR_TO_BTF_ID_OR_NULL) { |
7162 | reg->type = PTR_TO_BTF_ID; | |
457f4436 AN |
7163 | } else if (reg->type == PTR_TO_MEM_OR_NULL) { |
7164 | reg->type = PTR_TO_MEM; | |
afbf21dc YS |
7165 | } else if (reg->type == PTR_TO_RDONLY_BUF_OR_NULL) { |
7166 | reg->type = PTR_TO_RDONLY_BUF; | |
7167 | } else if (reg->type == PTR_TO_RDWR_BUF_OR_NULL) { | |
7168 | reg->type = PTR_TO_RDWR_BUF; | |
56f668df | 7169 | } |
1b986589 MKL |
7170 | if (is_null) { |
7171 | /* We don't need id and ref_obj_id from this point | |
7172 | * onwards anymore, thus we should better reset it, | |
7173 | * so that state pruning has chances to take effect. | |
7174 | */ | |
7175 | reg->id = 0; | |
7176 | reg->ref_obj_id = 0; | |
7177 | } else if (!reg_may_point_to_spin_lock(reg)) { | |
7178 | /* For not-NULL ptr, reg->ref_obj_id will be reset | |
7179 | * in release_reg_references(). | |
7180 | * | |
7181 | * reg->id is still used by spin_lock ptr. Other | |
7182 | * than spin_lock ptr type, reg->id can be reset. | |
fd978bf7 JS |
7183 | */ |
7184 | reg->id = 0; | |
56f668df | 7185 | } |
57a09bf0 TG |
7186 | } |
7187 | } | |
7188 | ||
c6a9efa1 PC |
7189 | static void __mark_ptr_or_null_regs(struct bpf_func_state *state, u32 id, |
7190 | bool is_null) | |
7191 | { | |
7192 | struct bpf_reg_state *reg; | |
7193 | int i; | |
7194 | ||
7195 | for (i = 0; i < MAX_BPF_REG; i++) | |
7196 | mark_ptr_or_null_reg(state, &state->regs[i], id, is_null); | |
7197 | ||
7198 | bpf_for_each_spilled_reg(i, state, reg) { | |
7199 | if (!reg) | |
7200 | continue; | |
7201 | mark_ptr_or_null_reg(state, reg, id, is_null); | |
7202 | } | |
7203 | } | |
7204 | ||
57a09bf0 TG |
7205 | /* The logic is similar to find_good_pkt_pointers(), both could eventually |
7206 | * be folded together at some point. | |
7207 | */ | |
840b9615 JS |
7208 | static void mark_ptr_or_null_regs(struct bpf_verifier_state *vstate, u32 regno, |
7209 | bool is_null) | |
57a09bf0 | 7210 | { |
f4d7e40a | 7211 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
c6a9efa1 | 7212 | struct bpf_reg_state *regs = state->regs; |
1b986589 | 7213 | u32 ref_obj_id = regs[regno].ref_obj_id; |
a08dd0da | 7214 | u32 id = regs[regno].id; |
c6a9efa1 | 7215 | int i; |
57a09bf0 | 7216 | |
1b986589 MKL |
7217 | if (ref_obj_id && ref_obj_id == id && is_null) |
7218 | /* regs[regno] is in the " == NULL" branch. | |
7219 | * No one could have freed the reference state before | |
7220 | * doing the NULL check. | |
7221 | */ | |
7222 | WARN_ON_ONCE(release_reference_state(state, id)); | |
fd978bf7 | 7223 | |
c6a9efa1 PC |
7224 | for (i = 0; i <= vstate->curframe; i++) |
7225 | __mark_ptr_or_null_regs(vstate->frame[i], id, is_null); | |
57a09bf0 TG |
7226 | } |
7227 | ||
5beca081 DB |
7228 | static bool try_match_pkt_pointers(const struct bpf_insn *insn, |
7229 | struct bpf_reg_state *dst_reg, | |
7230 | struct bpf_reg_state *src_reg, | |
7231 | struct bpf_verifier_state *this_branch, | |
7232 | struct bpf_verifier_state *other_branch) | |
7233 | { | |
7234 | if (BPF_SRC(insn->code) != BPF_X) | |
7235 | return false; | |
7236 | ||
092ed096 JW |
7237 | /* Pointers are always 64-bit. */ |
7238 | if (BPF_CLASS(insn->code) == BPF_JMP32) | |
7239 | return false; | |
7240 | ||
5beca081 DB |
7241 | switch (BPF_OP(insn->code)) { |
7242 | case BPF_JGT: | |
7243 | if ((dst_reg->type == PTR_TO_PACKET && | |
7244 | src_reg->type == PTR_TO_PACKET_END) || | |
7245 | (dst_reg->type == PTR_TO_PACKET_META && | |
7246 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
7247 | /* pkt_data' > pkt_end, pkt_meta' > pkt_data */ | |
7248 | find_good_pkt_pointers(this_branch, dst_reg, | |
7249 | dst_reg->type, false); | |
7250 | } else if ((dst_reg->type == PTR_TO_PACKET_END && | |
7251 | src_reg->type == PTR_TO_PACKET) || | |
7252 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
7253 | src_reg->type == PTR_TO_PACKET_META)) { | |
7254 | /* pkt_end > pkt_data', pkt_data > pkt_meta' */ | |
7255 | find_good_pkt_pointers(other_branch, src_reg, | |
7256 | src_reg->type, true); | |
7257 | } else { | |
7258 | return false; | |
7259 | } | |
7260 | break; | |
7261 | case BPF_JLT: | |
7262 | if ((dst_reg->type == PTR_TO_PACKET && | |
7263 | src_reg->type == PTR_TO_PACKET_END) || | |
7264 | (dst_reg->type == PTR_TO_PACKET_META && | |
7265 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
7266 | /* pkt_data' < pkt_end, pkt_meta' < pkt_data */ | |
7267 | find_good_pkt_pointers(other_branch, dst_reg, | |
7268 | dst_reg->type, true); | |
7269 | } else if ((dst_reg->type == PTR_TO_PACKET_END && | |
7270 | src_reg->type == PTR_TO_PACKET) || | |
7271 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
7272 | src_reg->type == PTR_TO_PACKET_META)) { | |
7273 | /* pkt_end < pkt_data', pkt_data > pkt_meta' */ | |
7274 | find_good_pkt_pointers(this_branch, src_reg, | |
7275 | src_reg->type, false); | |
7276 | } else { | |
7277 | return false; | |
7278 | } | |
7279 | break; | |
7280 | case BPF_JGE: | |
7281 | if ((dst_reg->type == PTR_TO_PACKET && | |
7282 | src_reg->type == PTR_TO_PACKET_END) || | |
7283 | (dst_reg->type == PTR_TO_PACKET_META && | |
7284 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
7285 | /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */ | |
7286 | find_good_pkt_pointers(this_branch, dst_reg, | |
7287 | dst_reg->type, true); | |
7288 | } else if ((dst_reg->type == PTR_TO_PACKET_END && | |
7289 | src_reg->type == PTR_TO_PACKET) || | |
7290 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
7291 | src_reg->type == PTR_TO_PACKET_META)) { | |
7292 | /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */ | |
7293 | find_good_pkt_pointers(other_branch, src_reg, | |
7294 | src_reg->type, false); | |
7295 | } else { | |
7296 | return false; | |
7297 | } | |
7298 | break; | |
7299 | case BPF_JLE: | |
7300 | if ((dst_reg->type == PTR_TO_PACKET && | |
7301 | src_reg->type == PTR_TO_PACKET_END) || | |
7302 | (dst_reg->type == PTR_TO_PACKET_META && | |
7303 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
7304 | /* pkt_data' <= pkt_end, pkt_meta' <= pkt_data */ | |
7305 | find_good_pkt_pointers(other_branch, dst_reg, | |
7306 | dst_reg->type, false); | |
7307 | } else if ((dst_reg->type == PTR_TO_PACKET_END && | |
7308 | src_reg->type == PTR_TO_PACKET) || | |
7309 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
7310 | src_reg->type == PTR_TO_PACKET_META)) { | |
7311 | /* pkt_end <= pkt_data', pkt_data <= pkt_meta' */ | |
7312 | find_good_pkt_pointers(this_branch, src_reg, | |
7313 | src_reg->type, true); | |
7314 | } else { | |
7315 | return false; | |
7316 | } | |
7317 | break; | |
7318 | default: | |
7319 | return false; | |
7320 | } | |
7321 | ||
7322 | return true; | |
7323 | } | |
7324 | ||
58e2af8b | 7325 | static int check_cond_jmp_op(struct bpf_verifier_env *env, |
17a52670 AS |
7326 | struct bpf_insn *insn, int *insn_idx) |
7327 | { | |
f4d7e40a AS |
7328 | struct bpf_verifier_state *this_branch = env->cur_state; |
7329 | struct bpf_verifier_state *other_branch; | |
7330 | struct bpf_reg_state *regs = this_branch->frame[this_branch->curframe]->regs; | |
fb8d251e | 7331 | struct bpf_reg_state *dst_reg, *other_branch_regs, *src_reg = NULL; |
17a52670 | 7332 | u8 opcode = BPF_OP(insn->code); |
092ed096 | 7333 | bool is_jmp32; |
fb8d251e | 7334 | int pred = -1; |
17a52670 AS |
7335 | int err; |
7336 | ||
092ed096 JW |
7337 | /* Only conditional jumps are expected to reach here. */ |
7338 | if (opcode == BPF_JA || opcode > BPF_JSLE) { | |
7339 | verbose(env, "invalid BPF_JMP/JMP32 opcode %x\n", opcode); | |
17a52670 AS |
7340 | return -EINVAL; |
7341 | } | |
7342 | ||
7343 | if (BPF_SRC(insn->code) == BPF_X) { | |
7344 | if (insn->imm != 0) { | |
092ed096 | 7345 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
17a52670 AS |
7346 | return -EINVAL; |
7347 | } | |
7348 | ||
7349 | /* check src1 operand */ | |
dc503a8a | 7350 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
7351 | if (err) |
7352 | return err; | |
1be7f75d AS |
7353 | |
7354 | if (is_pointer_value(env, insn->src_reg)) { | |
61bd5218 | 7355 | verbose(env, "R%d pointer comparison prohibited\n", |
1be7f75d AS |
7356 | insn->src_reg); |
7357 | return -EACCES; | |
7358 | } | |
fb8d251e | 7359 | src_reg = ®s[insn->src_reg]; |
17a52670 AS |
7360 | } else { |
7361 | if (insn->src_reg != BPF_REG_0) { | |
092ed096 | 7362 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
17a52670 AS |
7363 | return -EINVAL; |
7364 | } | |
7365 | } | |
7366 | ||
7367 | /* check src2 operand */ | |
dc503a8a | 7368 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
7369 | if (err) |
7370 | return err; | |
7371 | ||
1a0dc1ac | 7372 | dst_reg = ®s[insn->dst_reg]; |
092ed096 | 7373 | is_jmp32 = BPF_CLASS(insn->code) == BPF_JMP32; |
1a0dc1ac | 7374 | |
3f50f132 JF |
7375 | if (BPF_SRC(insn->code) == BPF_K) { |
7376 | pred = is_branch_taken(dst_reg, insn->imm, opcode, is_jmp32); | |
7377 | } else if (src_reg->type == SCALAR_VALUE && | |
7378 | is_jmp32 && tnum_is_const(tnum_subreg(src_reg->var_off))) { | |
7379 | pred = is_branch_taken(dst_reg, | |
7380 | tnum_subreg(src_reg->var_off).value, | |
7381 | opcode, | |
7382 | is_jmp32); | |
7383 | } else if (src_reg->type == SCALAR_VALUE && | |
7384 | !is_jmp32 && tnum_is_const(src_reg->var_off)) { | |
7385 | pred = is_branch_taken(dst_reg, | |
7386 | src_reg->var_off.value, | |
7387 | opcode, | |
7388 | is_jmp32); | |
7389 | } | |
7390 | ||
b5dc0163 | 7391 | if (pred >= 0) { |
cac616db JF |
7392 | /* If we get here with a dst_reg pointer type it is because |
7393 | * above is_branch_taken() special cased the 0 comparison. | |
7394 | */ | |
7395 | if (!__is_pointer_value(false, dst_reg)) | |
7396 | err = mark_chain_precision(env, insn->dst_reg); | |
b5dc0163 AS |
7397 | if (BPF_SRC(insn->code) == BPF_X && !err) |
7398 | err = mark_chain_precision(env, insn->src_reg); | |
7399 | if (err) | |
7400 | return err; | |
7401 | } | |
fb8d251e AS |
7402 | if (pred == 1) { |
7403 | /* only follow the goto, ignore fall-through */ | |
7404 | *insn_idx += insn->off; | |
7405 | return 0; | |
7406 | } else if (pred == 0) { | |
7407 | /* only follow fall-through branch, since | |
7408 | * that's where the program will go | |
7409 | */ | |
7410 | return 0; | |
17a52670 AS |
7411 | } |
7412 | ||
979d63d5 DB |
7413 | other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx, |
7414 | false); | |
17a52670 AS |
7415 | if (!other_branch) |
7416 | return -EFAULT; | |
f4d7e40a | 7417 | other_branch_regs = other_branch->frame[other_branch->curframe]->regs; |
17a52670 | 7418 | |
48461135 JB |
7419 | /* detect if we are comparing against a constant value so we can adjust |
7420 | * our min/max values for our dst register. | |
f1174f77 EC |
7421 | * this is only legit if both are scalars (or pointers to the same |
7422 | * object, I suppose, but we don't support that right now), because | |
7423 | * otherwise the different base pointers mean the offsets aren't | |
7424 | * comparable. | |
48461135 JB |
7425 | */ |
7426 | if (BPF_SRC(insn->code) == BPF_X) { | |
092ed096 | 7427 | struct bpf_reg_state *src_reg = ®s[insn->src_reg]; |
092ed096 | 7428 | |
f1174f77 | 7429 | if (dst_reg->type == SCALAR_VALUE && |
092ed096 JW |
7430 | src_reg->type == SCALAR_VALUE) { |
7431 | if (tnum_is_const(src_reg->var_off) || | |
3f50f132 JF |
7432 | (is_jmp32 && |
7433 | tnum_is_const(tnum_subreg(src_reg->var_off)))) | |
f4d7e40a | 7434 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
092ed096 | 7435 | dst_reg, |
3f50f132 JF |
7436 | src_reg->var_off.value, |
7437 | tnum_subreg(src_reg->var_off).value, | |
092ed096 JW |
7438 | opcode, is_jmp32); |
7439 | else if (tnum_is_const(dst_reg->var_off) || | |
3f50f132 JF |
7440 | (is_jmp32 && |
7441 | tnum_is_const(tnum_subreg(dst_reg->var_off)))) | |
f4d7e40a | 7442 | reg_set_min_max_inv(&other_branch_regs[insn->src_reg], |
092ed096 | 7443 | src_reg, |
3f50f132 JF |
7444 | dst_reg->var_off.value, |
7445 | tnum_subreg(dst_reg->var_off).value, | |
092ed096 JW |
7446 | opcode, is_jmp32); |
7447 | else if (!is_jmp32 && | |
7448 | (opcode == BPF_JEQ || opcode == BPF_JNE)) | |
f1174f77 | 7449 | /* Comparing for equality, we can combine knowledge */ |
f4d7e40a AS |
7450 | reg_combine_min_max(&other_branch_regs[insn->src_reg], |
7451 | &other_branch_regs[insn->dst_reg], | |
092ed096 | 7452 | src_reg, dst_reg, opcode); |
f1174f77 EC |
7453 | } |
7454 | } else if (dst_reg->type == SCALAR_VALUE) { | |
f4d7e40a | 7455 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
3f50f132 JF |
7456 | dst_reg, insn->imm, (u32)insn->imm, |
7457 | opcode, is_jmp32); | |
48461135 JB |
7458 | } |
7459 | ||
092ed096 JW |
7460 | /* detect if R == 0 where R is returned from bpf_map_lookup_elem(). |
7461 | * NOTE: these optimizations below are related with pointer comparison | |
7462 | * which will never be JMP32. | |
7463 | */ | |
7464 | if (!is_jmp32 && BPF_SRC(insn->code) == BPF_K && | |
1a0dc1ac | 7465 | insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) && |
840b9615 JS |
7466 | reg_type_may_be_null(dst_reg->type)) { |
7467 | /* Mark all identical registers in each branch as either | |
57a09bf0 TG |
7468 | * safe or unknown depending R == 0 or R != 0 conditional. |
7469 | */ | |
840b9615 JS |
7470 | mark_ptr_or_null_regs(this_branch, insn->dst_reg, |
7471 | opcode == BPF_JNE); | |
7472 | mark_ptr_or_null_regs(other_branch, insn->dst_reg, | |
7473 | opcode == BPF_JEQ); | |
5beca081 DB |
7474 | } else if (!try_match_pkt_pointers(insn, dst_reg, ®s[insn->src_reg], |
7475 | this_branch, other_branch) && | |
7476 | is_pointer_value(env, insn->dst_reg)) { | |
61bd5218 JK |
7477 | verbose(env, "R%d pointer comparison prohibited\n", |
7478 | insn->dst_reg); | |
1be7f75d | 7479 | return -EACCES; |
17a52670 | 7480 | } |
06ee7115 | 7481 | if (env->log.level & BPF_LOG_LEVEL) |
f4d7e40a | 7482 | print_verifier_state(env, this_branch->frame[this_branch->curframe]); |
17a52670 AS |
7483 | return 0; |
7484 | } | |
7485 | ||
17a52670 | 7486 | /* verify BPF_LD_IMM64 instruction */ |
58e2af8b | 7487 | static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn) |
17a52670 | 7488 | { |
d8eca5bb | 7489 | struct bpf_insn_aux_data *aux = cur_aux(env); |
638f5b90 | 7490 | struct bpf_reg_state *regs = cur_regs(env); |
d8eca5bb | 7491 | struct bpf_map *map; |
17a52670 AS |
7492 | int err; |
7493 | ||
7494 | if (BPF_SIZE(insn->code) != BPF_DW) { | |
61bd5218 | 7495 | verbose(env, "invalid BPF_LD_IMM insn\n"); |
17a52670 AS |
7496 | return -EINVAL; |
7497 | } | |
7498 | if (insn->off != 0) { | |
61bd5218 | 7499 | verbose(env, "BPF_LD_IMM64 uses reserved fields\n"); |
17a52670 AS |
7500 | return -EINVAL; |
7501 | } | |
7502 | ||
dc503a8a | 7503 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
17a52670 AS |
7504 | if (err) |
7505 | return err; | |
7506 | ||
6b173873 | 7507 | if (insn->src_reg == 0) { |
6b173873 JK |
7508 | u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm; |
7509 | ||
f1174f77 | 7510 | regs[insn->dst_reg].type = SCALAR_VALUE; |
b03c9f9f | 7511 | __mark_reg_known(®s[insn->dst_reg], imm); |
17a52670 | 7512 | return 0; |
6b173873 | 7513 | } |
17a52670 | 7514 | |
d8eca5bb DB |
7515 | map = env->used_maps[aux->map_index]; |
7516 | mark_reg_known_zero(env, regs, insn->dst_reg); | |
7517 | regs[insn->dst_reg].map_ptr = map; | |
7518 | ||
7519 | if (insn->src_reg == BPF_PSEUDO_MAP_VALUE) { | |
7520 | regs[insn->dst_reg].type = PTR_TO_MAP_VALUE; | |
7521 | regs[insn->dst_reg].off = aux->map_off; | |
7522 | if (map_value_has_spin_lock(map)) | |
7523 | regs[insn->dst_reg].id = ++env->id_gen; | |
7524 | } else if (insn->src_reg == BPF_PSEUDO_MAP_FD) { | |
7525 | regs[insn->dst_reg].type = CONST_PTR_TO_MAP; | |
7526 | } else { | |
7527 | verbose(env, "bpf verifier is misconfigured\n"); | |
7528 | return -EINVAL; | |
7529 | } | |
17a52670 | 7530 | |
17a52670 AS |
7531 | return 0; |
7532 | } | |
7533 | ||
96be4325 DB |
7534 | static bool may_access_skb(enum bpf_prog_type type) |
7535 | { | |
7536 | switch (type) { | |
7537 | case BPF_PROG_TYPE_SOCKET_FILTER: | |
7538 | case BPF_PROG_TYPE_SCHED_CLS: | |
94caee8c | 7539 | case BPF_PROG_TYPE_SCHED_ACT: |
96be4325 DB |
7540 | return true; |
7541 | default: | |
7542 | return false; | |
7543 | } | |
7544 | } | |
7545 | ||
ddd872bc AS |
7546 | /* verify safety of LD_ABS|LD_IND instructions: |
7547 | * - they can only appear in the programs where ctx == skb | |
7548 | * - since they are wrappers of function calls, they scratch R1-R5 registers, | |
7549 | * preserve R6-R9, and store return value into R0 | |
7550 | * | |
7551 | * Implicit input: | |
7552 | * ctx == skb == R6 == CTX | |
7553 | * | |
7554 | * Explicit input: | |
7555 | * SRC == any register | |
7556 | * IMM == 32-bit immediate | |
7557 | * | |
7558 | * Output: | |
7559 | * R0 - 8/16/32-bit skb data converted to cpu endianness | |
7560 | */ | |
58e2af8b | 7561 | static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn) |
ddd872bc | 7562 | { |
638f5b90 | 7563 | struct bpf_reg_state *regs = cur_regs(env); |
6d4f151a | 7564 | static const int ctx_reg = BPF_REG_6; |
ddd872bc | 7565 | u8 mode = BPF_MODE(insn->code); |
ddd872bc AS |
7566 | int i, err; |
7567 | ||
7e40781c | 7568 | if (!may_access_skb(resolve_prog_type(env->prog))) { |
61bd5218 | 7569 | verbose(env, "BPF_LD_[ABS|IND] instructions not allowed for this program type\n"); |
ddd872bc AS |
7570 | return -EINVAL; |
7571 | } | |
7572 | ||
e0cea7ce DB |
7573 | if (!env->ops->gen_ld_abs) { |
7574 | verbose(env, "bpf verifier is misconfigured\n"); | |
7575 | return -EINVAL; | |
7576 | } | |
7577 | ||
ddd872bc | 7578 | if (insn->dst_reg != BPF_REG_0 || insn->off != 0 || |
d82bccc6 | 7579 | BPF_SIZE(insn->code) == BPF_DW || |
ddd872bc | 7580 | (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) { |
61bd5218 | 7581 | verbose(env, "BPF_LD_[ABS|IND] uses reserved fields\n"); |
ddd872bc AS |
7582 | return -EINVAL; |
7583 | } | |
7584 | ||
7585 | /* check whether implicit source operand (register R6) is readable */ | |
6d4f151a | 7586 | err = check_reg_arg(env, ctx_reg, SRC_OP); |
ddd872bc AS |
7587 | if (err) |
7588 | return err; | |
7589 | ||
fd978bf7 JS |
7590 | /* Disallow usage of BPF_LD_[ABS|IND] with reference tracking, as |
7591 | * gen_ld_abs() may terminate the program at runtime, leading to | |
7592 | * reference leak. | |
7593 | */ | |
7594 | err = check_reference_leak(env); | |
7595 | if (err) { | |
7596 | verbose(env, "BPF_LD_[ABS|IND] cannot be mixed with socket references\n"); | |
7597 | return err; | |
7598 | } | |
7599 | ||
d83525ca AS |
7600 | if (env->cur_state->active_spin_lock) { |
7601 | verbose(env, "BPF_LD_[ABS|IND] cannot be used inside bpf_spin_lock-ed region\n"); | |
7602 | return -EINVAL; | |
7603 | } | |
7604 | ||
6d4f151a | 7605 | if (regs[ctx_reg].type != PTR_TO_CTX) { |
61bd5218 JK |
7606 | verbose(env, |
7607 | "at the time of BPF_LD_ABS|IND R6 != pointer to skb\n"); | |
ddd872bc AS |
7608 | return -EINVAL; |
7609 | } | |
7610 | ||
7611 | if (mode == BPF_IND) { | |
7612 | /* check explicit source operand */ | |
dc503a8a | 7613 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
ddd872bc AS |
7614 | if (err) |
7615 | return err; | |
7616 | } | |
7617 | ||
6d4f151a DB |
7618 | err = check_ctx_reg(env, ®s[ctx_reg], ctx_reg); |
7619 | if (err < 0) | |
7620 | return err; | |
7621 | ||
ddd872bc | 7622 | /* reset caller saved regs to unreadable */ |
dc503a8a | 7623 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
61bd5218 | 7624 | mark_reg_not_init(env, regs, caller_saved[i]); |
dc503a8a EC |
7625 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
7626 | } | |
ddd872bc AS |
7627 | |
7628 | /* mark destination R0 register as readable, since it contains | |
dc503a8a EC |
7629 | * the value fetched from the packet. |
7630 | * Already marked as written above. | |
ddd872bc | 7631 | */ |
61bd5218 | 7632 | mark_reg_unknown(env, regs, BPF_REG_0); |
5327ed3d JW |
7633 | /* ld_abs load up to 32-bit skb data. */ |
7634 | regs[BPF_REG_0].subreg_def = env->insn_idx + 1; | |
ddd872bc AS |
7635 | return 0; |
7636 | } | |
7637 | ||
390ee7e2 AS |
7638 | static int check_return_code(struct bpf_verifier_env *env) |
7639 | { | |
5cf1e914 | 7640 | struct tnum enforce_attach_type_range = tnum_unknown; |
27ae7997 | 7641 | const struct bpf_prog *prog = env->prog; |
390ee7e2 AS |
7642 | struct bpf_reg_state *reg; |
7643 | struct tnum range = tnum_range(0, 1); | |
7e40781c | 7644 | enum bpf_prog_type prog_type = resolve_prog_type(env->prog); |
27ae7997 MKL |
7645 | int err; |
7646 | ||
9e4e01df | 7647 | /* LSM and struct_ops func-ptr's return type could be "void" */ |
7e40781c UP |
7648 | if ((prog_type == BPF_PROG_TYPE_STRUCT_OPS || |
7649 | prog_type == BPF_PROG_TYPE_LSM) && | |
27ae7997 MKL |
7650 | !prog->aux->attach_func_proto->type) |
7651 | return 0; | |
7652 | ||
7653 | /* eBPF calling convetion is such that R0 is used | |
7654 | * to return the value from eBPF program. | |
7655 | * Make sure that it's readable at this time | |
7656 | * of bpf_exit, which means that program wrote | |
7657 | * something into it earlier | |
7658 | */ | |
7659 | err = check_reg_arg(env, BPF_REG_0, SRC_OP); | |
7660 | if (err) | |
7661 | return err; | |
7662 | ||
7663 | if (is_pointer_value(env, BPF_REG_0)) { | |
7664 | verbose(env, "R0 leaks addr as return value\n"); | |
7665 | return -EACCES; | |
7666 | } | |
390ee7e2 | 7667 | |
7e40781c | 7668 | switch (prog_type) { |
983695fa DB |
7669 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
7670 | if (env->prog->expected_attach_type == BPF_CGROUP_UDP4_RECVMSG || | |
1b66d253 DB |
7671 | env->prog->expected_attach_type == BPF_CGROUP_UDP6_RECVMSG || |
7672 | env->prog->expected_attach_type == BPF_CGROUP_INET4_GETPEERNAME || | |
7673 | env->prog->expected_attach_type == BPF_CGROUP_INET6_GETPEERNAME || | |
7674 | env->prog->expected_attach_type == BPF_CGROUP_INET4_GETSOCKNAME || | |
7675 | env->prog->expected_attach_type == BPF_CGROUP_INET6_GETSOCKNAME) | |
983695fa | 7676 | range = tnum_range(1, 1); |
ed4ed404 | 7677 | break; |
390ee7e2 | 7678 | case BPF_PROG_TYPE_CGROUP_SKB: |
5cf1e914 | 7679 | if (env->prog->expected_attach_type == BPF_CGROUP_INET_EGRESS) { |
7680 | range = tnum_range(0, 3); | |
7681 | enforce_attach_type_range = tnum_range(2, 3); | |
7682 | } | |
ed4ed404 | 7683 | break; |
390ee7e2 AS |
7684 | case BPF_PROG_TYPE_CGROUP_SOCK: |
7685 | case BPF_PROG_TYPE_SOCK_OPS: | |
ebc614f6 | 7686 | case BPF_PROG_TYPE_CGROUP_DEVICE: |
7b146ceb | 7687 | case BPF_PROG_TYPE_CGROUP_SYSCTL: |
0d01da6a | 7688 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: |
390ee7e2 | 7689 | break; |
15ab09bd AS |
7690 | case BPF_PROG_TYPE_RAW_TRACEPOINT: |
7691 | if (!env->prog->aux->attach_btf_id) | |
7692 | return 0; | |
7693 | range = tnum_const(0); | |
7694 | break; | |
15d83c4d | 7695 | case BPF_PROG_TYPE_TRACING: |
e92888c7 YS |
7696 | switch (env->prog->expected_attach_type) { |
7697 | case BPF_TRACE_FENTRY: | |
7698 | case BPF_TRACE_FEXIT: | |
7699 | range = tnum_const(0); | |
7700 | break; | |
7701 | case BPF_TRACE_RAW_TP: | |
7702 | case BPF_MODIFY_RETURN: | |
15d83c4d | 7703 | return 0; |
2ec0616e DB |
7704 | case BPF_TRACE_ITER: |
7705 | break; | |
e92888c7 YS |
7706 | default: |
7707 | return -ENOTSUPP; | |
7708 | } | |
15d83c4d | 7709 | break; |
e9ddbb77 JS |
7710 | case BPF_PROG_TYPE_SK_LOOKUP: |
7711 | range = tnum_range(SK_DROP, SK_PASS); | |
7712 | break; | |
e92888c7 YS |
7713 | case BPF_PROG_TYPE_EXT: |
7714 | /* freplace program can return anything as its return value | |
7715 | * depends on the to-be-replaced kernel func or bpf program. | |
7716 | */ | |
390ee7e2 AS |
7717 | default: |
7718 | return 0; | |
7719 | } | |
7720 | ||
638f5b90 | 7721 | reg = cur_regs(env) + BPF_REG_0; |
390ee7e2 | 7722 | if (reg->type != SCALAR_VALUE) { |
61bd5218 | 7723 | verbose(env, "At program exit the register R0 is not a known value (%s)\n", |
390ee7e2 AS |
7724 | reg_type_str[reg->type]); |
7725 | return -EINVAL; | |
7726 | } | |
7727 | ||
7728 | if (!tnum_in(range, reg->var_off)) { | |
5cf1e914 | 7729 | char tn_buf[48]; |
7730 | ||
61bd5218 | 7731 | verbose(env, "At program exit the register R0 "); |
390ee7e2 | 7732 | if (!tnum_is_unknown(reg->var_off)) { |
390ee7e2 | 7733 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
61bd5218 | 7734 | verbose(env, "has value %s", tn_buf); |
390ee7e2 | 7735 | } else { |
61bd5218 | 7736 | verbose(env, "has unknown scalar value"); |
390ee7e2 | 7737 | } |
5cf1e914 | 7738 | tnum_strn(tn_buf, sizeof(tn_buf), range); |
983695fa | 7739 | verbose(env, " should have been in %s\n", tn_buf); |
390ee7e2 AS |
7740 | return -EINVAL; |
7741 | } | |
5cf1e914 | 7742 | |
7743 | if (!tnum_is_unknown(enforce_attach_type_range) && | |
7744 | tnum_in(enforce_attach_type_range, reg->var_off)) | |
7745 | env->prog->enforce_expected_attach_type = 1; | |
390ee7e2 AS |
7746 | return 0; |
7747 | } | |
7748 | ||
475fb78f AS |
7749 | /* non-recursive DFS pseudo code |
7750 | * 1 procedure DFS-iterative(G,v): | |
7751 | * 2 label v as discovered | |
7752 | * 3 let S be a stack | |
7753 | * 4 S.push(v) | |
7754 | * 5 while S is not empty | |
7755 | * 6 t <- S.pop() | |
7756 | * 7 if t is what we're looking for: | |
7757 | * 8 return t | |
7758 | * 9 for all edges e in G.adjacentEdges(t) do | |
7759 | * 10 if edge e is already labelled | |
7760 | * 11 continue with the next edge | |
7761 | * 12 w <- G.adjacentVertex(t,e) | |
7762 | * 13 if vertex w is not discovered and not explored | |
7763 | * 14 label e as tree-edge | |
7764 | * 15 label w as discovered | |
7765 | * 16 S.push(w) | |
7766 | * 17 continue at 5 | |
7767 | * 18 else if vertex w is discovered | |
7768 | * 19 label e as back-edge | |
7769 | * 20 else | |
7770 | * 21 // vertex w is explored | |
7771 | * 22 label e as forward- or cross-edge | |
7772 | * 23 label t as explored | |
7773 | * 24 S.pop() | |
7774 | * | |
7775 | * convention: | |
7776 | * 0x10 - discovered | |
7777 | * 0x11 - discovered and fall-through edge labelled | |
7778 | * 0x12 - discovered and fall-through and branch edges labelled | |
7779 | * 0x20 - explored | |
7780 | */ | |
7781 | ||
7782 | enum { | |
7783 | DISCOVERED = 0x10, | |
7784 | EXPLORED = 0x20, | |
7785 | FALLTHROUGH = 1, | |
7786 | BRANCH = 2, | |
7787 | }; | |
7788 | ||
dc2a4ebc AS |
7789 | static u32 state_htab_size(struct bpf_verifier_env *env) |
7790 | { | |
7791 | return env->prog->len; | |
7792 | } | |
7793 | ||
5d839021 AS |
7794 | static struct bpf_verifier_state_list **explored_state( |
7795 | struct bpf_verifier_env *env, | |
7796 | int idx) | |
7797 | { | |
dc2a4ebc AS |
7798 | struct bpf_verifier_state *cur = env->cur_state; |
7799 | struct bpf_func_state *state = cur->frame[cur->curframe]; | |
7800 | ||
7801 | return &env->explored_states[(idx ^ state->callsite) % state_htab_size(env)]; | |
5d839021 AS |
7802 | } |
7803 | ||
7804 | static void init_explored_state(struct bpf_verifier_env *env, int idx) | |
7805 | { | |
a8f500af | 7806 | env->insn_aux_data[idx].prune_point = true; |
5d839021 | 7807 | } |
f1bca824 | 7808 | |
475fb78f AS |
7809 | /* t, w, e - match pseudo-code above: |
7810 | * t - index of current instruction | |
7811 | * w - next instruction | |
7812 | * e - edge | |
7813 | */ | |
2589726d AS |
7814 | static int push_insn(int t, int w, int e, struct bpf_verifier_env *env, |
7815 | bool loop_ok) | |
475fb78f | 7816 | { |
7df737e9 AS |
7817 | int *insn_stack = env->cfg.insn_stack; |
7818 | int *insn_state = env->cfg.insn_state; | |
7819 | ||
475fb78f AS |
7820 | if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH)) |
7821 | return 0; | |
7822 | ||
7823 | if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH)) | |
7824 | return 0; | |
7825 | ||
7826 | if (w < 0 || w >= env->prog->len) { | |
d9762e84 | 7827 | verbose_linfo(env, t, "%d: ", t); |
61bd5218 | 7828 | verbose(env, "jump out of range from insn %d to %d\n", t, w); |
475fb78f AS |
7829 | return -EINVAL; |
7830 | } | |
7831 | ||
f1bca824 AS |
7832 | if (e == BRANCH) |
7833 | /* mark branch target for state pruning */ | |
5d839021 | 7834 | init_explored_state(env, w); |
f1bca824 | 7835 | |
475fb78f AS |
7836 | if (insn_state[w] == 0) { |
7837 | /* tree-edge */ | |
7838 | insn_state[t] = DISCOVERED | e; | |
7839 | insn_state[w] = DISCOVERED; | |
7df737e9 | 7840 | if (env->cfg.cur_stack >= env->prog->len) |
475fb78f | 7841 | return -E2BIG; |
7df737e9 | 7842 | insn_stack[env->cfg.cur_stack++] = w; |
475fb78f AS |
7843 | return 1; |
7844 | } else if ((insn_state[w] & 0xF0) == DISCOVERED) { | |
2c78ee89 | 7845 | if (loop_ok && env->bpf_capable) |
2589726d | 7846 | return 0; |
d9762e84 MKL |
7847 | verbose_linfo(env, t, "%d: ", t); |
7848 | verbose_linfo(env, w, "%d: ", w); | |
61bd5218 | 7849 | verbose(env, "back-edge from insn %d to %d\n", t, w); |
475fb78f AS |
7850 | return -EINVAL; |
7851 | } else if (insn_state[w] == EXPLORED) { | |
7852 | /* forward- or cross-edge */ | |
7853 | insn_state[t] = DISCOVERED | e; | |
7854 | } else { | |
61bd5218 | 7855 | verbose(env, "insn state internal bug\n"); |
475fb78f AS |
7856 | return -EFAULT; |
7857 | } | |
7858 | return 0; | |
7859 | } | |
7860 | ||
7861 | /* non-recursive depth-first-search to detect loops in BPF program | |
7862 | * loop == back-edge in directed graph | |
7863 | */ | |
58e2af8b | 7864 | static int check_cfg(struct bpf_verifier_env *env) |
475fb78f AS |
7865 | { |
7866 | struct bpf_insn *insns = env->prog->insnsi; | |
7867 | int insn_cnt = env->prog->len; | |
7df737e9 | 7868 | int *insn_stack, *insn_state; |
475fb78f AS |
7869 | int ret = 0; |
7870 | int i, t; | |
7871 | ||
7df737e9 | 7872 | insn_state = env->cfg.insn_state = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
475fb78f AS |
7873 | if (!insn_state) |
7874 | return -ENOMEM; | |
7875 | ||
7df737e9 | 7876 | insn_stack = env->cfg.insn_stack = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
475fb78f | 7877 | if (!insn_stack) { |
71dde681 | 7878 | kvfree(insn_state); |
475fb78f AS |
7879 | return -ENOMEM; |
7880 | } | |
7881 | ||
7882 | insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */ | |
7883 | insn_stack[0] = 0; /* 0 is the first instruction */ | |
7df737e9 | 7884 | env->cfg.cur_stack = 1; |
475fb78f AS |
7885 | |
7886 | peek_stack: | |
7df737e9 | 7887 | if (env->cfg.cur_stack == 0) |
475fb78f | 7888 | goto check_state; |
7df737e9 | 7889 | t = insn_stack[env->cfg.cur_stack - 1]; |
475fb78f | 7890 | |
092ed096 JW |
7891 | if (BPF_CLASS(insns[t].code) == BPF_JMP || |
7892 | BPF_CLASS(insns[t].code) == BPF_JMP32) { | |
475fb78f AS |
7893 | u8 opcode = BPF_OP(insns[t].code); |
7894 | ||
7895 | if (opcode == BPF_EXIT) { | |
7896 | goto mark_explored; | |
7897 | } else if (opcode == BPF_CALL) { | |
2589726d | 7898 | ret = push_insn(t, t + 1, FALLTHROUGH, env, false); |
475fb78f AS |
7899 | if (ret == 1) |
7900 | goto peek_stack; | |
7901 | else if (ret < 0) | |
7902 | goto err_free; | |
07016151 | 7903 | if (t + 1 < insn_cnt) |
5d839021 | 7904 | init_explored_state(env, t + 1); |
cc8b0b92 | 7905 | if (insns[t].src_reg == BPF_PSEUDO_CALL) { |
5d839021 | 7906 | init_explored_state(env, t); |
2589726d AS |
7907 | ret = push_insn(t, t + insns[t].imm + 1, BRANCH, |
7908 | env, false); | |
cc8b0b92 AS |
7909 | if (ret == 1) |
7910 | goto peek_stack; | |
7911 | else if (ret < 0) | |
7912 | goto err_free; | |
7913 | } | |
475fb78f AS |
7914 | } else if (opcode == BPF_JA) { |
7915 | if (BPF_SRC(insns[t].code) != BPF_K) { | |
7916 | ret = -EINVAL; | |
7917 | goto err_free; | |
7918 | } | |
7919 | /* unconditional jump with single edge */ | |
7920 | ret = push_insn(t, t + insns[t].off + 1, | |
2589726d | 7921 | FALLTHROUGH, env, true); |
475fb78f AS |
7922 | if (ret == 1) |
7923 | goto peek_stack; | |
7924 | else if (ret < 0) | |
7925 | goto err_free; | |
b5dc0163 AS |
7926 | /* unconditional jmp is not a good pruning point, |
7927 | * but it's marked, since backtracking needs | |
7928 | * to record jmp history in is_state_visited(). | |
7929 | */ | |
7930 | init_explored_state(env, t + insns[t].off + 1); | |
f1bca824 AS |
7931 | /* tell verifier to check for equivalent states |
7932 | * after every call and jump | |
7933 | */ | |
c3de6317 | 7934 | if (t + 1 < insn_cnt) |
5d839021 | 7935 | init_explored_state(env, t + 1); |
475fb78f AS |
7936 | } else { |
7937 | /* conditional jump with two edges */ | |
5d839021 | 7938 | init_explored_state(env, t); |
2589726d | 7939 | ret = push_insn(t, t + 1, FALLTHROUGH, env, true); |
475fb78f AS |
7940 | if (ret == 1) |
7941 | goto peek_stack; | |
7942 | else if (ret < 0) | |
7943 | goto err_free; | |
7944 | ||
2589726d | 7945 | ret = push_insn(t, t + insns[t].off + 1, BRANCH, env, true); |
475fb78f AS |
7946 | if (ret == 1) |
7947 | goto peek_stack; | |
7948 | else if (ret < 0) | |
7949 | goto err_free; | |
7950 | } | |
7951 | } else { | |
7952 | /* all other non-branch instructions with single | |
7953 | * fall-through edge | |
7954 | */ | |
2589726d | 7955 | ret = push_insn(t, t + 1, FALLTHROUGH, env, false); |
475fb78f AS |
7956 | if (ret == 1) |
7957 | goto peek_stack; | |
7958 | else if (ret < 0) | |
7959 | goto err_free; | |
7960 | } | |
7961 | ||
7962 | mark_explored: | |
7963 | insn_state[t] = EXPLORED; | |
7df737e9 | 7964 | if (env->cfg.cur_stack-- <= 0) { |
61bd5218 | 7965 | verbose(env, "pop stack internal bug\n"); |
475fb78f AS |
7966 | ret = -EFAULT; |
7967 | goto err_free; | |
7968 | } | |
7969 | goto peek_stack; | |
7970 | ||
7971 | check_state: | |
7972 | for (i = 0; i < insn_cnt; i++) { | |
7973 | if (insn_state[i] != EXPLORED) { | |
61bd5218 | 7974 | verbose(env, "unreachable insn %d\n", i); |
475fb78f AS |
7975 | ret = -EINVAL; |
7976 | goto err_free; | |
7977 | } | |
7978 | } | |
7979 | ret = 0; /* cfg looks good */ | |
7980 | ||
7981 | err_free: | |
71dde681 AS |
7982 | kvfree(insn_state); |
7983 | kvfree(insn_stack); | |
7df737e9 | 7984 | env->cfg.insn_state = env->cfg.insn_stack = NULL; |
475fb78f AS |
7985 | return ret; |
7986 | } | |
7987 | ||
09b28d76 AS |
7988 | static int check_abnormal_return(struct bpf_verifier_env *env) |
7989 | { | |
7990 | int i; | |
7991 | ||
7992 | for (i = 1; i < env->subprog_cnt; i++) { | |
7993 | if (env->subprog_info[i].has_ld_abs) { | |
7994 | verbose(env, "LD_ABS is not allowed in subprogs without BTF\n"); | |
7995 | return -EINVAL; | |
7996 | } | |
7997 | if (env->subprog_info[i].has_tail_call) { | |
7998 | verbose(env, "tail_call is not allowed in subprogs without BTF\n"); | |
7999 | return -EINVAL; | |
8000 | } | |
8001 | } | |
8002 | return 0; | |
8003 | } | |
8004 | ||
838e9690 YS |
8005 | /* The minimum supported BTF func info size */ |
8006 | #define MIN_BPF_FUNCINFO_SIZE 8 | |
8007 | #define MAX_FUNCINFO_REC_SIZE 252 | |
8008 | ||
c454a46b MKL |
8009 | static int check_btf_func(struct bpf_verifier_env *env, |
8010 | const union bpf_attr *attr, | |
8011 | union bpf_attr __user *uattr) | |
838e9690 | 8012 | { |
09b28d76 | 8013 | const struct btf_type *type, *func_proto, *ret_type; |
d0b2818e | 8014 | u32 i, nfuncs, urec_size, min_size; |
838e9690 | 8015 | u32 krec_size = sizeof(struct bpf_func_info); |
c454a46b | 8016 | struct bpf_func_info *krecord; |
8c1b6e69 | 8017 | struct bpf_func_info_aux *info_aux = NULL; |
c454a46b MKL |
8018 | struct bpf_prog *prog; |
8019 | const struct btf *btf; | |
838e9690 | 8020 | void __user *urecord; |
d0b2818e | 8021 | u32 prev_offset = 0; |
09b28d76 | 8022 | bool scalar_return; |
e7ed83d6 | 8023 | int ret = -ENOMEM; |
838e9690 YS |
8024 | |
8025 | nfuncs = attr->func_info_cnt; | |
09b28d76 AS |
8026 | if (!nfuncs) { |
8027 | if (check_abnormal_return(env)) | |
8028 | return -EINVAL; | |
838e9690 | 8029 | return 0; |
09b28d76 | 8030 | } |
838e9690 YS |
8031 | |
8032 | if (nfuncs != env->subprog_cnt) { | |
8033 | verbose(env, "number of funcs in func_info doesn't match number of subprogs\n"); | |
8034 | return -EINVAL; | |
8035 | } | |
8036 | ||
8037 | urec_size = attr->func_info_rec_size; | |
8038 | if (urec_size < MIN_BPF_FUNCINFO_SIZE || | |
8039 | urec_size > MAX_FUNCINFO_REC_SIZE || | |
8040 | urec_size % sizeof(u32)) { | |
8041 | verbose(env, "invalid func info rec size %u\n", urec_size); | |
8042 | return -EINVAL; | |
8043 | } | |
8044 | ||
c454a46b MKL |
8045 | prog = env->prog; |
8046 | btf = prog->aux->btf; | |
838e9690 YS |
8047 | |
8048 | urecord = u64_to_user_ptr(attr->func_info); | |
8049 | min_size = min_t(u32, krec_size, urec_size); | |
8050 | ||
ba64e7d8 | 8051 | krecord = kvcalloc(nfuncs, krec_size, GFP_KERNEL | __GFP_NOWARN); |
c454a46b MKL |
8052 | if (!krecord) |
8053 | return -ENOMEM; | |
8c1b6e69 AS |
8054 | info_aux = kcalloc(nfuncs, sizeof(*info_aux), GFP_KERNEL | __GFP_NOWARN); |
8055 | if (!info_aux) | |
8056 | goto err_free; | |
ba64e7d8 | 8057 | |
838e9690 YS |
8058 | for (i = 0; i < nfuncs; i++) { |
8059 | ret = bpf_check_uarg_tail_zero(urecord, krec_size, urec_size); | |
8060 | if (ret) { | |
8061 | if (ret == -E2BIG) { | |
8062 | verbose(env, "nonzero tailing record in func info"); | |
8063 | /* set the size kernel expects so loader can zero | |
8064 | * out the rest of the record. | |
8065 | */ | |
8066 | if (put_user(min_size, &uattr->func_info_rec_size)) | |
8067 | ret = -EFAULT; | |
8068 | } | |
c454a46b | 8069 | goto err_free; |
838e9690 YS |
8070 | } |
8071 | ||
ba64e7d8 | 8072 | if (copy_from_user(&krecord[i], urecord, min_size)) { |
838e9690 | 8073 | ret = -EFAULT; |
c454a46b | 8074 | goto err_free; |
838e9690 YS |
8075 | } |
8076 | ||
d30d42e0 | 8077 | /* check insn_off */ |
09b28d76 | 8078 | ret = -EINVAL; |
838e9690 | 8079 | if (i == 0) { |
d30d42e0 | 8080 | if (krecord[i].insn_off) { |
838e9690 | 8081 | verbose(env, |
d30d42e0 MKL |
8082 | "nonzero insn_off %u for the first func info record", |
8083 | krecord[i].insn_off); | |
c454a46b | 8084 | goto err_free; |
838e9690 | 8085 | } |
d30d42e0 | 8086 | } else if (krecord[i].insn_off <= prev_offset) { |
838e9690 YS |
8087 | verbose(env, |
8088 | "same or smaller insn offset (%u) than previous func info record (%u)", | |
d30d42e0 | 8089 | krecord[i].insn_off, prev_offset); |
c454a46b | 8090 | goto err_free; |
838e9690 YS |
8091 | } |
8092 | ||
d30d42e0 | 8093 | if (env->subprog_info[i].start != krecord[i].insn_off) { |
838e9690 | 8094 | verbose(env, "func_info BTF section doesn't match subprog layout in BPF program\n"); |
c454a46b | 8095 | goto err_free; |
838e9690 YS |
8096 | } |
8097 | ||
8098 | /* check type_id */ | |
ba64e7d8 | 8099 | type = btf_type_by_id(btf, krecord[i].type_id); |
51c39bb1 | 8100 | if (!type || !btf_type_is_func(type)) { |
838e9690 | 8101 | verbose(env, "invalid type id %d in func info", |
ba64e7d8 | 8102 | krecord[i].type_id); |
c454a46b | 8103 | goto err_free; |
838e9690 | 8104 | } |
51c39bb1 | 8105 | info_aux[i].linkage = BTF_INFO_VLEN(type->info); |
09b28d76 AS |
8106 | |
8107 | func_proto = btf_type_by_id(btf, type->type); | |
8108 | if (unlikely(!func_proto || !btf_type_is_func_proto(func_proto))) | |
8109 | /* btf_func_check() already verified it during BTF load */ | |
8110 | goto err_free; | |
8111 | ret_type = btf_type_skip_modifiers(btf, func_proto->type, NULL); | |
8112 | scalar_return = | |
8113 | btf_type_is_small_int(ret_type) || btf_type_is_enum(ret_type); | |
8114 | if (i && !scalar_return && env->subprog_info[i].has_ld_abs) { | |
8115 | verbose(env, "LD_ABS is only allowed in functions that return 'int'.\n"); | |
8116 | goto err_free; | |
8117 | } | |
8118 | if (i && !scalar_return && env->subprog_info[i].has_tail_call) { | |
8119 | verbose(env, "tail_call is only allowed in functions that return 'int'.\n"); | |
8120 | goto err_free; | |
8121 | } | |
8122 | ||
d30d42e0 | 8123 | prev_offset = krecord[i].insn_off; |
838e9690 YS |
8124 | urecord += urec_size; |
8125 | } | |
8126 | ||
ba64e7d8 YS |
8127 | prog->aux->func_info = krecord; |
8128 | prog->aux->func_info_cnt = nfuncs; | |
8c1b6e69 | 8129 | prog->aux->func_info_aux = info_aux; |
838e9690 YS |
8130 | return 0; |
8131 | ||
c454a46b | 8132 | err_free: |
ba64e7d8 | 8133 | kvfree(krecord); |
8c1b6e69 | 8134 | kfree(info_aux); |
838e9690 YS |
8135 | return ret; |
8136 | } | |
8137 | ||
ba64e7d8 YS |
8138 | static void adjust_btf_func(struct bpf_verifier_env *env) |
8139 | { | |
8c1b6e69 | 8140 | struct bpf_prog_aux *aux = env->prog->aux; |
ba64e7d8 YS |
8141 | int i; |
8142 | ||
8c1b6e69 | 8143 | if (!aux->func_info) |
ba64e7d8 YS |
8144 | return; |
8145 | ||
8146 | for (i = 0; i < env->subprog_cnt; i++) | |
8c1b6e69 | 8147 | aux->func_info[i].insn_off = env->subprog_info[i].start; |
ba64e7d8 YS |
8148 | } |
8149 | ||
c454a46b MKL |
8150 | #define MIN_BPF_LINEINFO_SIZE (offsetof(struct bpf_line_info, line_col) + \ |
8151 | sizeof(((struct bpf_line_info *)(0))->line_col)) | |
8152 | #define MAX_LINEINFO_REC_SIZE MAX_FUNCINFO_REC_SIZE | |
8153 | ||
8154 | static int check_btf_line(struct bpf_verifier_env *env, | |
8155 | const union bpf_attr *attr, | |
8156 | union bpf_attr __user *uattr) | |
8157 | { | |
8158 | u32 i, s, nr_linfo, ncopy, expected_size, rec_size, prev_offset = 0; | |
8159 | struct bpf_subprog_info *sub; | |
8160 | struct bpf_line_info *linfo; | |
8161 | struct bpf_prog *prog; | |
8162 | const struct btf *btf; | |
8163 | void __user *ulinfo; | |
8164 | int err; | |
8165 | ||
8166 | nr_linfo = attr->line_info_cnt; | |
8167 | if (!nr_linfo) | |
8168 | return 0; | |
8169 | ||
8170 | rec_size = attr->line_info_rec_size; | |
8171 | if (rec_size < MIN_BPF_LINEINFO_SIZE || | |
8172 | rec_size > MAX_LINEINFO_REC_SIZE || | |
8173 | rec_size & (sizeof(u32) - 1)) | |
8174 | return -EINVAL; | |
8175 | ||
8176 | /* Need to zero it in case the userspace may | |
8177 | * pass in a smaller bpf_line_info object. | |
8178 | */ | |
8179 | linfo = kvcalloc(nr_linfo, sizeof(struct bpf_line_info), | |
8180 | GFP_KERNEL | __GFP_NOWARN); | |
8181 | if (!linfo) | |
8182 | return -ENOMEM; | |
8183 | ||
8184 | prog = env->prog; | |
8185 | btf = prog->aux->btf; | |
8186 | ||
8187 | s = 0; | |
8188 | sub = env->subprog_info; | |
8189 | ulinfo = u64_to_user_ptr(attr->line_info); | |
8190 | expected_size = sizeof(struct bpf_line_info); | |
8191 | ncopy = min_t(u32, expected_size, rec_size); | |
8192 | for (i = 0; i < nr_linfo; i++) { | |
8193 | err = bpf_check_uarg_tail_zero(ulinfo, expected_size, rec_size); | |
8194 | if (err) { | |
8195 | if (err == -E2BIG) { | |
8196 | verbose(env, "nonzero tailing record in line_info"); | |
8197 | if (put_user(expected_size, | |
8198 | &uattr->line_info_rec_size)) | |
8199 | err = -EFAULT; | |
8200 | } | |
8201 | goto err_free; | |
8202 | } | |
8203 | ||
8204 | if (copy_from_user(&linfo[i], ulinfo, ncopy)) { | |
8205 | err = -EFAULT; | |
8206 | goto err_free; | |
8207 | } | |
8208 | ||
8209 | /* | |
8210 | * Check insn_off to ensure | |
8211 | * 1) strictly increasing AND | |
8212 | * 2) bounded by prog->len | |
8213 | * | |
8214 | * The linfo[0].insn_off == 0 check logically falls into | |
8215 | * the later "missing bpf_line_info for func..." case | |
8216 | * because the first linfo[0].insn_off must be the | |
8217 | * first sub also and the first sub must have | |
8218 | * subprog_info[0].start == 0. | |
8219 | */ | |
8220 | if ((i && linfo[i].insn_off <= prev_offset) || | |
8221 | linfo[i].insn_off >= prog->len) { | |
8222 | verbose(env, "Invalid line_info[%u].insn_off:%u (prev_offset:%u prog->len:%u)\n", | |
8223 | i, linfo[i].insn_off, prev_offset, | |
8224 | prog->len); | |
8225 | err = -EINVAL; | |
8226 | goto err_free; | |
8227 | } | |
8228 | ||
fdbaa0be MKL |
8229 | if (!prog->insnsi[linfo[i].insn_off].code) { |
8230 | verbose(env, | |
8231 | "Invalid insn code at line_info[%u].insn_off\n", | |
8232 | i); | |
8233 | err = -EINVAL; | |
8234 | goto err_free; | |
8235 | } | |
8236 | ||
23127b33 MKL |
8237 | if (!btf_name_by_offset(btf, linfo[i].line_off) || |
8238 | !btf_name_by_offset(btf, linfo[i].file_name_off)) { | |
c454a46b MKL |
8239 | verbose(env, "Invalid line_info[%u].line_off or .file_name_off\n", i); |
8240 | err = -EINVAL; | |
8241 | goto err_free; | |
8242 | } | |
8243 | ||
8244 | if (s != env->subprog_cnt) { | |
8245 | if (linfo[i].insn_off == sub[s].start) { | |
8246 | sub[s].linfo_idx = i; | |
8247 | s++; | |
8248 | } else if (sub[s].start < linfo[i].insn_off) { | |
8249 | verbose(env, "missing bpf_line_info for func#%u\n", s); | |
8250 | err = -EINVAL; | |
8251 | goto err_free; | |
8252 | } | |
8253 | } | |
8254 | ||
8255 | prev_offset = linfo[i].insn_off; | |
8256 | ulinfo += rec_size; | |
8257 | } | |
8258 | ||
8259 | if (s != env->subprog_cnt) { | |
8260 | verbose(env, "missing bpf_line_info for %u funcs starting from func#%u\n", | |
8261 | env->subprog_cnt - s, s); | |
8262 | err = -EINVAL; | |
8263 | goto err_free; | |
8264 | } | |
8265 | ||
8266 | prog->aux->linfo = linfo; | |
8267 | prog->aux->nr_linfo = nr_linfo; | |
8268 | ||
8269 | return 0; | |
8270 | ||
8271 | err_free: | |
8272 | kvfree(linfo); | |
8273 | return err; | |
8274 | } | |
8275 | ||
8276 | static int check_btf_info(struct bpf_verifier_env *env, | |
8277 | const union bpf_attr *attr, | |
8278 | union bpf_attr __user *uattr) | |
8279 | { | |
8280 | struct btf *btf; | |
8281 | int err; | |
8282 | ||
09b28d76 AS |
8283 | if (!attr->func_info_cnt && !attr->line_info_cnt) { |
8284 | if (check_abnormal_return(env)) | |
8285 | return -EINVAL; | |
c454a46b | 8286 | return 0; |
09b28d76 | 8287 | } |
c454a46b MKL |
8288 | |
8289 | btf = btf_get_by_fd(attr->prog_btf_fd); | |
8290 | if (IS_ERR(btf)) | |
8291 | return PTR_ERR(btf); | |
8292 | env->prog->aux->btf = btf; | |
8293 | ||
8294 | err = check_btf_func(env, attr, uattr); | |
8295 | if (err) | |
8296 | return err; | |
8297 | ||
8298 | err = check_btf_line(env, attr, uattr); | |
8299 | if (err) | |
8300 | return err; | |
8301 | ||
8302 | return 0; | |
ba64e7d8 YS |
8303 | } |
8304 | ||
f1174f77 EC |
8305 | /* check %cur's range satisfies %old's */ |
8306 | static bool range_within(struct bpf_reg_state *old, | |
8307 | struct bpf_reg_state *cur) | |
8308 | { | |
b03c9f9f EC |
8309 | return old->umin_value <= cur->umin_value && |
8310 | old->umax_value >= cur->umax_value && | |
8311 | old->smin_value <= cur->smin_value && | |
8312 | old->smax_value >= cur->smax_value; | |
f1174f77 EC |
8313 | } |
8314 | ||
8315 | /* Maximum number of register states that can exist at once */ | |
8316 | #define ID_MAP_SIZE (MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE) | |
8317 | struct idpair { | |
8318 | u32 old; | |
8319 | u32 cur; | |
8320 | }; | |
8321 | ||
8322 | /* If in the old state two registers had the same id, then they need to have | |
8323 | * the same id in the new state as well. But that id could be different from | |
8324 | * the old state, so we need to track the mapping from old to new ids. | |
8325 | * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent | |
8326 | * regs with old id 5 must also have new id 9 for the new state to be safe. But | |
8327 | * regs with a different old id could still have new id 9, we don't care about | |
8328 | * that. | |
8329 | * So we look through our idmap to see if this old id has been seen before. If | |
8330 | * so, we require the new id to match; otherwise, we add the id pair to the map. | |
969bf05e | 8331 | */ |
f1174f77 | 8332 | static bool check_ids(u32 old_id, u32 cur_id, struct idpair *idmap) |
969bf05e | 8333 | { |
f1174f77 | 8334 | unsigned int i; |
969bf05e | 8335 | |
f1174f77 EC |
8336 | for (i = 0; i < ID_MAP_SIZE; i++) { |
8337 | if (!idmap[i].old) { | |
8338 | /* Reached an empty slot; haven't seen this id before */ | |
8339 | idmap[i].old = old_id; | |
8340 | idmap[i].cur = cur_id; | |
8341 | return true; | |
8342 | } | |
8343 | if (idmap[i].old == old_id) | |
8344 | return idmap[i].cur == cur_id; | |
8345 | } | |
8346 | /* We ran out of idmap slots, which should be impossible */ | |
8347 | WARN_ON_ONCE(1); | |
8348 | return false; | |
8349 | } | |
8350 | ||
9242b5f5 AS |
8351 | static void clean_func_state(struct bpf_verifier_env *env, |
8352 | struct bpf_func_state *st) | |
8353 | { | |
8354 | enum bpf_reg_liveness live; | |
8355 | int i, j; | |
8356 | ||
8357 | for (i = 0; i < BPF_REG_FP; i++) { | |
8358 | live = st->regs[i].live; | |
8359 | /* liveness must not touch this register anymore */ | |
8360 | st->regs[i].live |= REG_LIVE_DONE; | |
8361 | if (!(live & REG_LIVE_READ)) | |
8362 | /* since the register is unused, clear its state | |
8363 | * to make further comparison simpler | |
8364 | */ | |
f54c7898 | 8365 | __mark_reg_not_init(env, &st->regs[i]); |
9242b5f5 AS |
8366 | } |
8367 | ||
8368 | for (i = 0; i < st->allocated_stack / BPF_REG_SIZE; i++) { | |
8369 | live = st->stack[i].spilled_ptr.live; | |
8370 | /* liveness must not touch this stack slot anymore */ | |
8371 | st->stack[i].spilled_ptr.live |= REG_LIVE_DONE; | |
8372 | if (!(live & REG_LIVE_READ)) { | |
f54c7898 | 8373 | __mark_reg_not_init(env, &st->stack[i].spilled_ptr); |
9242b5f5 AS |
8374 | for (j = 0; j < BPF_REG_SIZE; j++) |
8375 | st->stack[i].slot_type[j] = STACK_INVALID; | |
8376 | } | |
8377 | } | |
8378 | } | |
8379 | ||
8380 | static void clean_verifier_state(struct bpf_verifier_env *env, | |
8381 | struct bpf_verifier_state *st) | |
8382 | { | |
8383 | int i; | |
8384 | ||
8385 | if (st->frame[0]->regs[0].live & REG_LIVE_DONE) | |
8386 | /* all regs in this state in all frames were already marked */ | |
8387 | return; | |
8388 | ||
8389 | for (i = 0; i <= st->curframe; i++) | |
8390 | clean_func_state(env, st->frame[i]); | |
8391 | } | |
8392 | ||
8393 | /* the parentage chains form a tree. | |
8394 | * the verifier states are added to state lists at given insn and | |
8395 | * pushed into state stack for future exploration. | |
8396 | * when the verifier reaches bpf_exit insn some of the verifer states | |
8397 | * stored in the state lists have their final liveness state already, | |
8398 | * but a lot of states will get revised from liveness point of view when | |
8399 | * the verifier explores other branches. | |
8400 | * Example: | |
8401 | * 1: r0 = 1 | |
8402 | * 2: if r1 == 100 goto pc+1 | |
8403 | * 3: r0 = 2 | |
8404 | * 4: exit | |
8405 | * when the verifier reaches exit insn the register r0 in the state list of | |
8406 | * insn 2 will be seen as !REG_LIVE_READ. Then the verifier pops the other_branch | |
8407 | * of insn 2 and goes exploring further. At the insn 4 it will walk the | |
8408 | * parentage chain from insn 4 into insn 2 and will mark r0 as REG_LIVE_READ. | |
8409 | * | |
8410 | * Since the verifier pushes the branch states as it sees them while exploring | |
8411 | * the program the condition of walking the branch instruction for the second | |
8412 | * time means that all states below this branch were already explored and | |
8413 | * their final liveness markes are already propagated. | |
8414 | * Hence when the verifier completes the search of state list in is_state_visited() | |
8415 | * we can call this clean_live_states() function to mark all liveness states | |
8416 | * as REG_LIVE_DONE to indicate that 'parent' pointers of 'struct bpf_reg_state' | |
8417 | * will not be used. | |
8418 | * This function also clears the registers and stack for states that !READ | |
8419 | * to simplify state merging. | |
8420 | * | |
8421 | * Important note here that walking the same branch instruction in the callee | |
8422 | * doesn't meant that the states are DONE. The verifier has to compare | |
8423 | * the callsites | |
8424 | */ | |
8425 | static void clean_live_states(struct bpf_verifier_env *env, int insn, | |
8426 | struct bpf_verifier_state *cur) | |
8427 | { | |
8428 | struct bpf_verifier_state_list *sl; | |
8429 | int i; | |
8430 | ||
5d839021 | 8431 | sl = *explored_state(env, insn); |
a8f500af | 8432 | while (sl) { |
2589726d AS |
8433 | if (sl->state.branches) |
8434 | goto next; | |
dc2a4ebc AS |
8435 | if (sl->state.insn_idx != insn || |
8436 | sl->state.curframe != cur->curframe) | |
9242b5f5 AS |
8437 | goto next; |
8438 | for (i = 0; i <= cur->curframe; i++) | |
8439 | if (sl->state.frame[i]->callsite != cur->frame[i]->callsite) | |
8440 | goto next; | |
8441 | clean_verifier_state(env, &sl->state); | |
8442 | next: | |
8443 | sl = sl->next; | |
8444 | } | |
8445 | } | |
8446 | ||
f1174f77 | 8447 | /* Returns true if (rold safe implies rcur safe) */ |
1b688a19 EC |
8448 | static bool regsafe(struct bpf_reg_state *rold, struct bpf_reg_state *rcur, |
8449 | struct idpair *idmap) | |
f1174f77 | 8450 | { |
f4d7e40a AS |
8451 | bool equal; |
8452 | ||
dc503a8a EC |
8453 | if (!(rold->live & REG_LIVE_READ)) |
8454 | /* explored state didn't use this */ | |
8455 | return true; | |
8456 | ||
679c782d | 8457 | equal = memcmp(rold, rcur, offsetof(struct bpf_reg_state, parent)) == 0; |
f4d7e40a AS |
8458 | |
8459 | if (rold->type == PTR_TO_STACK) | |
8460 | /* two stack pointers are equal only if they're pointing to | |
8461 | * the same stack frame, since fp-8 in foo != fp-8 in bar | |
8462 | */ | |
8463 | return equal && rold->frameno == rcur->frameno; | |
8464 | ||
8465 | if (equal) | |
969bf05e AS |
8466 | return true; |
8467 | ||
f1174f77 EC |
8468 | if (rold->type == NOT_INIT) |
8469 | /* explored state can't have used this */ | |
969bf05e | 8470 | return true; |
f1174f77 EC |
8471 | if (rcur->type == NOT_INIT) |
8472 | return false; | |
8473 | switch (rold->type) { | |
8474 | case SCALAR_VALUE: | |
8475 | if (rcur->type == SCALAR_VALUE) { | |
b5dc0163 AS |
8476 | if (!rold->precise && !rcur->precise) |
8477 | return true; | |
f1174f77 EC |
8478 | /* new val must satisfy old val knowledge */ |
8479 | return range_within(rold, rcur) && | |
8480 | tnum_in(rold->var_off, rcur->var_off); | |
8481 | } else { | |
179d1c56 JH |
8482 | /* We're trying to use a pointer in place of a scalar. |
8483 | * Even if the scalar was unbounded, this could lead to | |
8484 | * pointer leaks because scalars are allowed to leak | |
8485 | * while pointers are not. We could make this safe in | |
8486 | * special cases if root is calling us, but it's | |
8487 | * probably not worth the hassle. | |
f1174f77 | 8488 | */ |
179d1c56 | 8489 | return false; |
f1174f77 EC |
8490 | } |
8491 | case PTR_TO_MAP_VALUE: | |
1b688a19 EC |
8492 | /* If the new min/max/var_off satisfy the old ones and |
8493 | * everything else matches, we are OK. | |
d83525ca AS |
8494 | * 'id' is not compared, since it's only used for maps with |
8495 | * bpf_spin_lock inside map element and in such cases if | |
8496 | * the rest of the prog is valid for one map element then | |
8497 | * it's valid for all map elements regardless of the key | |
8498 | * used in bpf_map_lookup() | |
1b688a19 EC |
8499 | */ |
8500 | return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 && | |
8501 | range_within(rold, rcur) && | |
8502 | tnum_in(rold->var_off, rcur->var_off); | |
f1174f77 EC |
8503 | case PTR_TO_MAP_VALUE_OR_NULL: |
8504 | /* a PTR_TO_MAP_VALUE could be safe to use as a | |
8505 | * PTR_TO_MAP_VALUE_OR_NULL into the same map. | |
8506 | * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL- | |
8507 | * checked, doing so could have affected others with the same | |
8508 | * id, and we can't check for that because we lost the id when | |
8509 | * we converted to a PTR_TO_MAP_VALUE. | |
8510 | */ | |
8511 | if (rcur->type != PTR_TO_MAP_VALUE_OR_NULL) | |
8512 | return false; | |
8513 | if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, id))) | |
8514 | return false; | |
8515 | /* Check our ids match any regs they're supposed to */ | |
8516 | return check_ids(rold->id, rcur->id, idmap); | |
de8f3a83 | 8517 | case PTR_TO_PACKET_META: |
f1174f77 | 8518 | case PTR_TO_PACKET: |
de8f3a83 | 8519 | if (rcur->type != rold->type) |
f1174f77 EC |
8520 | return false; |
8521 | /* We must have at least as much range as the old ptr | |
8522 | * did, so that any accesses which were safe before are | |
8523 | * still safe. This is true even if old range < old off, | |
8524 | * since someone could have accessed through (ptr - k), or | |
8525 | * even done ptr -= k in a register, to get a safe access. | |
8526 | */ | |
8527 | if (rold->range > rcur->range) | |
8528 | return false; | |
8529 | /* If the offsets don't match, we can't trust our alignment; | |
8530 | * nor can we be sure that we won't fall out of range. | |
8531 | */ | |
8532 | if (rold->off != rcur->off) | |
8533 | return false; | |
8534 | /* id relations must be preserved */ | |
8535 | if (rold->id && !check_ids(rold->id, rcur->id, idmap)) | |
8536 | return false; | |
8537 | /* new val must satisfy old val knowledge */ | |
8538 | return range_within(rold, rcur) && | |
8539 | tnum_in(rold->var_off, rcur->var_off); | |
8540 | case PTR_TO_CTX: | |
8541 | case CONST_PTR_TO_MAP: | |
f1174f77 | 8542 | case PTR_TO_PACKET_END: |
d58e468b | 8543 | case PTR_TO_FLOW_KEYS: |
c64b7983 JS |
8544 | case PTR_TO_SOCKET: |
8545 | case PTR_TO_SOCKET_OR_NULL: | |
46f8bc92 MKL |
8546 | case PTR_TO_SOCK_COMMON: |
8547 | case PTR_TO_SOCK_COMMON_OR_NULL: | |
655a51e5 MKL |
8548 | case PTR_TO_TCP_SOCK: |
8549 | case PTR_TO_TCP_SOCK_OR_NULL: | |
fada7fdc | 8550 | case PTR_TO_XDP_SOCK: |
f1174f77 EC |
8551 | /* Only valid matches are exact, which memcmp() above |
8552 | * would have accepted | |
8553 | */ | |
8554 | default: | |
8555 | /* Don't know what's going on, just say it's not safe */ | |
8556 | return false; | |
8557 | } | |
969bf05e | 8558 | |
f1174f77 EC |
8559 | /* Shouldn't get here; if we do, say it's not safe */ |
8560 | WARN_ON_ONCE(1); | |
969bf05e AS |
8561 | return false; |
8562 | } | |
8563 | ||
f4d7e40a AS |
8564 | static bool stacksafe(struct bpf_func_state *old, |
8565 | struct bpf_func_state *cur, | |
638f5b90 AS |
8566 | struct idpair *idmap) |
8567 | { | |
8568 | int i, spi; | |
8569 | ||
638f5b90 AS |
8570 | /* walk slots of the explored stack and ignore any additional |
8571 | * slots in the current stack, since explored(safe) state | |
8572 | * didn't use them | |
8573 | */ | |
8574 | for (i = 0; i < old->allocated_stack; i++) { | |
8575 | spi = i / BPF_REG_SIZE; | |
8576 | ||
b233920c AS |
8577 | if (!(old->stack[spi].spilled_ptr.live & REG_LIVE_READ)) { |
8578 | i += BPF_REG_SIZE - 1; | |
cc2b14d5 | 8579 | /* explored state didn't use this */ |
fd05e57b | 8580 | continue; |
b233920c | 8581 | } |
cc2b14d5 | 8582 | |
638f5b90 AS |
8583 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID) |
8584 | continue; | |
19e2dbb7 AS |
8585 | |
8586 | /* explored stack has more populated slots than current stack | |
8587 | * and these slots were used | |
8588 | */ | |
8589 | if (i >= cur->allocated_stack) | |
8590 | return false; | |
8591 | ||
cc2b14d5 AS |
8592 | /* if old state was safe with misc data in the stack |
8593 | * it will be safe with zero-initialized stack. | |
8594 | * The opposite is not true | |
8595 | */ | |
8596 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC && | |
8597 | cur->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_ZERO) | |
8598 | continue; | |
638f5b90 AS |
8599 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] != |
8600 | cur->stack[spi].slot_type[i % BPF_REG_SIZE]) | |
8601 | /* Ex: old explored (safe) state has STACK_SPILL in | |
b8c1a309 | 8602 | * this stack slot, but current has STACK_MISC -> |
638f5b90 AS |
8603 | * this verifier states are not equivalent, |
8604 | * return false to continue verification of this path | |
8605 | */ | |
8606 | return false; | |
8607 | if (i % BPF_REG_SIZE) | |
8608 | continue; | |
8609 | if (old->stack[spi].slot_type[0] != STACK_SPILL) | |
8610 | continue; | |
8611 | if (!regsafe(&old->stack[spi].spilled_ptr, | |
8612 | &cur->stack[spi].spilled_ptr, | |
8613 | idmap)) | |
8614 | /* when explored and current stack slot are both storing | |
8615 | * spilled registers, check that stored pointers types | |
8616 | * are the same as well. | |
8617 | * Ex: explored safe path could have stored | |
8618 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8} | |
8619 | * but current path has stored: | |
8620 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16} | |
8621 | * such verifier states are not equivalent. | |
8622 | * return false to continue verification of this path | |
8623 | */ | |
8624 | return false; | |
8625 | } | |
8626 | return true; | |
8627 | } | |
8628 | ||
fd978bf7 JS |
8629 | static bool refsafe(struct bpf_func_state *old, struct bpf_func_state *cur) |
8630 | { | |
8631 | if (old->acquired_refs != cur->acquired_refs) | |
8632 | return false; | |
8633 | return !memcmp(old->refs, cur->refs, | |
8634 | sizeof(*old->refs) * old->acquired_refs); | |
8635 | } | |
8636 | ||
f1bca824 AS |
8637 | /* compare two verifier states |
8638 | * | |
8639 | * all states stored in state_list are known to be valid, since | |
8640 | * verifier reached 'bpf_exit' instruction through them | |
8641 | * | |
8642 | * this function is called when verifier exploring different branches of | |
8643 | * execution popped from the state stack. If it sees an old state that has | |
8644 | * more strict register state and more strict stack state then this execution | |
8645 | * branch doesn't need to be explored further, since verifier already | |
8646 | * concluded that more strict state leads to valid finish. | |
8647 | * | |
8648 | * Therefore two states are equivalent if register state is more conservative | |
8649 | * and explored stack state is more conservative than the current one. | |
8650 | * Example: | |
8651 | * explored current | |
8652 | * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC) | |
8653 | * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC) | |
8654 | * | |
8655 | * In other words if current stack state (one being explored) has more | |
8656 | * valid slots than old one that already passed validation, it means | |
8657 | * the verifier can stop exploring and conclude that current state is valid too | |
8658 | * | |
8659 | * Similarly with registers. If explored state has register type as invalid | |
8660 | * whereas register type in current state is meaningful, it means that | |
8661 | * the current state will reach 'bpf_exit' instruction safely | |
8662 | */ | |
f4d7e40a AS |
8663 | static bool func_states_equal(struct bpf_func_state *old, |
8664 | struct bpf_func_state *cur) | |
f1bca824 | 8665 | { |
f1174f77 EC |
8666 | struct idpair *idmap; |
8667 | bool ret = false; | |
f1bca824 AS |
8668 | int i; |
8669 | ||
f1174f77 EC |
8670 | idmap = kcalloc(ID_MAP_SIZE, sizeof(struct idpair), GFP_KERNEL); |
8671 | /* If we failed to allocate the idmap, just say it's not safe */ | |
8672 | if (!idmap) | |
1a0dc1ac | 8673 | return false; |
f1174f77 EC |
8674 | |
8675 | for (i = 0; i < MAX_BPF_REG; i++) { | |
1b688a19 | 8676 | if (!regsafe(&old->regs[i], &cur->regs[i], idmap)) |
f1174f77 | 8677 | goto out_free; |
f1bca824 AS |
8678 | } |
8679 | ||
638f5b90 AS |
8680 | if (!stacksafe(old, cur, idmap)) |
8681 | goto out_free; | |
fd978bf7 JS |
8682 | |
8683 | if (!refsafe(old, cur)) | |
8684 | goto out_free; | |
f1174f77 EC |
8685 | ret = true; |
8686 | out_free: | |
8687 | kfree(idmap); | |
8688 | return ret; | |
f1bca824 AS |
8689 | } |
8690 | ||
f4d7e40a AS |
8691 | static bool states_equal(struct bpf_verifier_env *env, |
8692 | struct bpf_verifier_state *old, | |
8693 | struct bpf_verifier_state *cur) | |
8694 | { | |
8695 | int i; | |
8696 | ||
8697 | if (old->curframe != cur->curframe) | |
8698 | return false; | |
8699 | ||
979d63d5 DB |
8700 | /* Verification state from speculative execution simulation |
8701 | * must never prune a non-speculative execution one. | |
8702 | */ | |
8703 | if (old->speculative && !cur->speculative) | |
8704 | return false; | |
8705 | ||
d83525ca AS |
8706 | if (old->active_spin_lock != cur->active_spin_lock) |
8707 | return false; | |
8708 | ||
f4d7e40a AS |
8709 | /* for states to be equal callsites have to be the same |
8710 | * and all frame states need to be equivalent | |
8711 | */ | |
8712 | for (i = 0; i <= old->curframe; i++) { | |
8713 | if (old->frame[i]->callsite != cur->frame[i]->callsite) | |
8714 | return false; | |
8715 | if (!func_states_equal(old->frame[i], cur->frame[i])) | |
8716 | return false; | |
8717 | } | |
8718 | return true; | |
8719 | } | |
8720 | ||
5327ed3d JW |
8721 | /* Return 0 if no propagation happened. Return negative error code if error |
8722 | * happened. Otherwise, return the propagated bit. | |
8723 | */ | |
55e7f3b5 JW |
8724 | static int propagate_liveness_reg(struct bpf_verifier_env *env, |
8725 | struct bpf_reg_state *reg, | |
8726 | struct bpf_reg_state *parent_reg) | |
8727 | { | |
5327ed3d JW |
8728 | u8 parent_flag = parent_reg->live & REG_LIVE_READ; |
8729 | u8 flag = reg->live & REG_LIVE_READ; | |
55e7f3b5 JW |
8730 | int err; |
8731 | ||
5327ed3d JW |
8732 | /* When comes here, read flags of PARENT_REG or REG could be any of |
8733 | * REG_LIVE_READ64, REG_LIVE_READ32, REG_LIVE_NONE. There is no need | |
8734 | * of propagation if PARENT_REG has strongest REG_LIVE_READ64. | |
8735 | */ | |
8736 | if (parent_flag == REG_LIVE_READ64 || | |
8737 | /* Or if there is no read flag from REG. */ | |
8738 | !flag || | |
8739 | /* Or if the read flag from REG is the same as PARENT_REG. */ | |
8740 | parent_flag == flag) | |
55e7f3b5 JW |
8741 | return 0; |
8742 | ||
5327ed3d | 8743 | err = mark_reg_read(env, reg, parent_reg, flag); |
55e7f3b5 JW |
8744 | if (err) |
8745 | return err; | |
8746 | ||
5327ed3d | 8747 | return flag; |
55e7f3b5 JW |
8748 | } |
8749 | ||
8e9cd9ce | 8750 | /* A write screens off any subsequent reads; but write marks come from the |
f4d7e40a AS |
8751 | * straight-line code between a state and its parent. When we arrive at an |
8752 | * equivalent state (jump target or such) we didn't arrive by the straight-line | |
8753 | * code, so read marks in the state must propagate to the parent regardless | |
8754 | * of the state's write marks. That's what 'parent == state->parent' comparison | |
679c782d | 8755 | * in mark_reg_read() is for. |
8e9cd9ce | 8756 | */ |
f4d7e40a AS |
8757 | static int propagate_liveness(struct bpf_verifier_env *env, |
8758 | const struct bpf_verifier_state *vstate, | |
8759 | struct bpf_verifier_state *vparent) | |
dc503a8a | 8760 | { |
3f8cafa4 | 8761 | struct bpf_reg_state *state_reg, *parent_reg; |
f4d7e40a | 8762 | struct bpf_func_state *state, *parent; |
3f8cafa4 | 8763 | int i, frame, err = 0; |
dc503a8a | 8764 | |
f4d7e40a AS |
8765 | if (vparent->curframe != vstate->curframe) { |
8766 | WARN(1, "propagate_live: parent frame %d current frame %d\n", | |
8767 | vparent->curframe, vstate->curframe); | |
8768 | return -EFAULT; | |
8769 | } | |
dc503a8a EC |
8770 | /* Propagate read liveness of registers... */ |
8771 | BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG); | |
83d16312 | 8772 | for (frame = 0; frame <= vstate->curframe; frame++) { |
3f8cafa4 JW |
8773 | parent = vparent->frame[frame]; |
8774 | state = vstate->frame[frame]; | |
8775 | parent_reg = parent->regs; | |
8776 | state_reg = state->regs; | |
83d16312 JK |
8777 | /* We don't need to worry about FP liveness, it's read-only */ |
8778 | for (i = frame < vstate->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) { | |
55e7f3b5 JW |
8779 | err = propagate_liveness_reg(env, &state_reg[i], |
8780 | &parent_reg[i]); | |
5327ed3d | 8781 | if (err < 0) |
3f8cafa4 | 8782 | return err; |
5327ed3d JW |
8783 | if (err == REG_LIVE_READ64) |
8784 | mark_insn_zext(env, &parent_reg[i]); | |
dc503a8a | 8785 | } |
f4d7e40a | 8786 | |
1b04aee7 | 8787 | /* Propagate stack slots. */ |
f4d7e40a AS |
8788 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE && |
8789 | i < parent->allocated_stack / BPF_REG_SIZE; i++) { | |
3f8cafa4 JW |
8790 | parent_reg = &parent->stack[i].spilled_ptr; |
8791 | state_reg = &state->stack[i].spilled_ptr; | |
55e7f3b5 JW |
8792 | err = propagate_liveness_reg(env, state_reg, |
8793 | parent_reg); | |
5327ed3d | 8794 | if (err < 0) |
3f8cafa4 | 8795 | return err; |
dc503a8a EC |
8796 | } |
8797 | } | |
5327ed3d | 8798 | return 0; |
dc503a8a EC |
8799 | } |
8800 | ||
a3ce685d AS |
8801 | /* find precise scalars in the previous equivalent state and |
8802 | * propagate them into the current state | |
8803 | */ | |
8804 | static int propagate_precision(struct bpf_verifier_env *env, | |
8805 | const struct bpf_verifier_state *old) | |
8806 | { | |
8807 | struct bpf_reg_state *state_reg; | |
8808 | struct bpf_func_state *state; | |
8809 | int i, err = 0; | |
8810 | ||
8811 | state = old->frame[old->curframe]; | |
8812 | state_reg = state->regs; | |
8813 | for (i = 0; i < BPF_REG_FP; i++, state_reg++) { | |
8814 | if (state_reg->type != SCALAR_VALUE || | |
8815 | !state_reg->precise) | |
8816 | continue; | |
8817 | if (env->log.level & BPF_LOG_LEVEL2) | |
8818 | verbose(env, "propagating r%d\n", i); | |
8819 | err = mark_chain_precision(env, i); | |
8820 | if (err < 0) | |
8821 | return err; | |
8822 | } | |
8823 | ||
8824 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { | |
8825 | if (state->stack[i].slot_type[0] != STACK_SPILL) | |
8826 | continue; | |
8827 | state_reg = &state->stack[i].spilled_ptr; | |
8828 | if (state_reg->type != SCALAR_VALUE || | |
8829 | !state_reg->precise) | |
8830 | continue; | |
8831 | if (env->log.level & BPF_LOG_LEVEL2) | |
8832 | verbose(env, "propagating fp%d\n", | |
8833 | (-i - 1) * BPF_REG_SIZE); | |
8834 | err = mark_chain_precision_stack(env, i); | |
8835 | if (err < 0) | |
8836 | return err; | |
8837 | } | |
8838 | return 0; | |
8839 | } | |
8840 | ||
2589726d AS |
8841 | static bool states_maybe_looping(struct bpf_verifier_state *old, |
8842 | struct bpf_verifier_state *cur) | |
8843 | { | |
8844 | struct bpf_func_state *fold, *fcur; | |
8845 | int i, fr = cur->curframe; | |
8846 | ||
8847 | if (old->curframe != fr) | |
8848 | return false; | |
8849 | ||
8850 | fold = old->frame[fr]; | |
8851 | fcur = cur->frame[fr]; | |
8852 | for (i = 0; i < MAX_BPF_REG; i++) | |
8853 | if (memcmp(&fold->regs[i], &fcur->regs[i], | |
8854 | offsetof(struct bpf_reg_state, parent))) | |
8855 | return false; | |
8856 | return true; | |
8857 | } | |
8858 | ||
8859 | ||
58e2af8b | 8860 | static int is_state_visited(struct bpf_verifier_env *env, int insn_idx) |
f1bca824 | 8861 | { |
58e2af8b | 8862 | struct bpf_verifier_state_list *new_sl; |
9f4686c4 | 8863 | struct bpf_verifier_state_list *sl, **pprev; |
679c782d | 8864 | struct bpf_verifier_state *cur = env->cur_state, *new; |
ceefbc96 | 8865 | int i, j, err, states_cnt = 0; |
10d274e8 | 8866 | bool add_new_state = env->test_state_freq ? true : false; |
f1bca824 | 8867 | |
b5dc0163 | 8868 | cur->last_insn_idx = env->prev_insn_idx; |
a8f500af | 8869 | if (!env->insn_aux_data[insn_idx].prune_point) |
f1bca824 AS |
8870 | /* this 'insn_idx' instruction wasn't marked, so we will not |
8871 | * be doing state search here | |
8872 | */ | |
8873 | return 0; | |
8874 | ||
2589726d AS |
8875 | /* bpf progs typically have pruning point every 4 instructions |
8876 | * http://vger.kernel.org/bpfconf2019.html#session-1 | |
8877 | * Do not add new state for future pruning if the verifier hasn't seen | |
8878 | * at least 2 jumps and at least 8 instructions. | |
8879 | * This heuristics helps decrease 'total_states' and 'peak_states' metric. | |
8880 | * In tests that amounts to up to 50% reduction into total verifier | |
8881 | * memory consumption and 20% verifier time speedup. | |
8882 | */ | |
8883 | if (env->jmps_processed - env->prev_jmps_processed >= 2 && | |
8884 | env->insn_processed - env->prev_insn_processed >= 8) | |
8885 | add_new_state = true; | |
8886 | ||
a8f500af AS |
8887 | pprev = explored_state(env, insn_idx); |
8888 | sl = *pprev; | |
8889 | ||
9242b5f5 AS |
8890 | clean_live_states(env, insn_idx, cur); |
8891 | ||
a8f500af | 8892 | while (sl) { |
dc2a4ebc AS |
8893 | states_cnt++; |
8894 | if (sl->state.insn_idx != insn_idx) | |
8895 | goto next; | |
2589726d AS |
8896 | if (sl->state.branches) { |
8897 | if (states_maybe_looping(&sl->state, cur) && | |
8898 | states_equal(env, &sl->state, cur)) { | |
8899 | verbose_linfo(env, insn_idx, "; "); | |
8900 | verbose(env, "infinite loop detected at insn %d\n", insn_idx); | |
8901 | return -EINVAL; | |
8902 | } | |
8903 | /* if the verifier is processing a loop, avoid adding new state | |
8904 | * too often, since different loop iterations have distinct | |
8905 | * states and may not help future pruning. | |
8906 | * This threshold shouldn't be too low to make sure that | |
8907 | * a loop with large bound will be rejected quickly. | |
8908 | * The most abusive loop will be: | |
8909 | * r1 += 1 | |
8910 | * if r1 < 1000000 goto pc-2 | |
8911 | * 1M insn_procssed limit / 100 == 10k peak states. | |
8912 | * This threshold shouldn't be too high either, since states | |
8913 | * at the end of the loop are likely to be useful in pruning. | |
8914 | */ | |
8915 | if (env->jmps_processed - env->prev_jmps_processed < 20 && | |
8916 | env->insn_processed - env->prev_insn_processed < 100) | |
8917 | add_new_state = false; | |
8918 | goto miss; | |
8919 | } | |
638f5b90 | 8920 | if (states_equal(env, &sl->state, cur)) { |
9f4686c4 | 8921 | sl->hit_cnt++; |
f1bca824 | 8922 | /* reached equivalent register/stack state, |
dc503a8a EC |
8923 | * prune the search. |
8924 | * Registers read by the continuation are read by us. | |
8e9cd9ce EC |
8925 | * If we have any write marks in env->cur_state, they |
8926 | * will prevent corresponding reads in the continuation | |
8927 | * from reaching our parent (an explored_state). Our | |
8928 | * own state will get the read marks recorded, but | |
8929 | * they'll be immediately forgotten as we're pruning | |
8930 | * this state and will pop a new one. | |
f1bca824 | 8931 | */ |
f4d7e40a | 8932 | err = propagate_liveness(env, &sl->state, cur); |
a3ce685d AS |
8933 | |
8934 | /* if previous state reached the exit with precision and | |
8935 | * current state is equivalent to it (except precsion marks) | |
8936 | * the precision needs to be propagated back in | |
8937 | * the current state. | |
8938 | */ | |
8939 | err = err ? : push_jmp_history(env, cur); | |
8940 | err = err ? : propagate_precision(env, &sl->state); | |
f4d7e40a AS |
8941 | if (err) |
8942 | return err; | |
f1bca824 | 8943 | return 1; |
dc503a8a | 8944 | } |
2589726d AS |
8945 | miss: |
8946 | /* when new state is not going to be added do not increase miss count. | |
8947 | * Otherwise several loop iterations will remove the state | |
8948 | * recorded earlier. The goal of these heuristics is to have | |
8949 | * states from some iterations of the loop (some in the beginning | |
8950 | * and some at the end) to help pruning. | |
8951 | */ | |
8952 | if (add_new_state) | |
8953 | sl->miss_cnt++; | |
9f4686c4 AS |
8954 | /* heuristic to determine whether this state is beneficial |
8955 | * to keep checking from state equivalence point of view. | |
8956 | * Higher numbers increase max_states_per_insn and verification time, | |
8957 | * but do not meaningfully decrease insn_processed. | |
8958 | */ | |
8959 | if (sl->miss_cnt > sl->hit_cnt * 3 + 3) { | |
8960 | /* the state is unlikely to be useful. Remove it to | |
8961 | * speed up verification | |
8962 | */ | |
8963 | *pprev = sl->next; | |
8964 | if (sl->state.frame[0]->regs[0].live & REG_LIVE_DONE) { | |
2589726d AS |
8965 | u32 br = sl->state.branches; |
8966 | ||
8967 | WARN_ONCE(br, | |
8968 | "BUG live_done but branches_to_explore %d\n", | |
8969 | br); | |
9f4686c4 AS |
8970 | free_verifier_state(&sl->state, false); |
8971 | kfree(sl); | |
8972 | env->peak_states--; | |
8973 | } else { | |
8974 | /* cannot free this state, since parentage chain may | |
8975 | * walk it later. Add it for free_list instead to | |
8976 | * be freed at the end of verification | |
8977 | */ | |
8978 | sl->next = env->free_list; | |
8979 | env->free_list = sl; | |
8980 | } | |
8981 | sl = *pprev; | |
8982 | continue; | |
8983 | } | |
dc2a4ebc | 8984 | next: |
9f4686c4 AS |
8985 | pprev = &sl->next; |
8986 | sl = *pprev; | |
f1bca824 AS |
8987 | } |
8988 | ||
06ee7115 AS |
8989 | if (env->max_states_per_insn < states_cnt) |
8990 | env->max_states_per_insn = states_cnt; | |
8991 | ||
2c78ee89 | 8992 | if (!env->bpf_capable && states_cnt > BPF_COMPLEXITY_LIMIT_STATES) |
b5dc0163 | 8993 | return push_jmp_history(env, cur); |
ceefbc96 | 8994 | |
2589726d | 8995 | if (!add_new_state) |
b5dc0163 | 8996 | return push_jmp_history(env, cur); |
ceefbc96 | 8997 | |
2589726d AS |
8998 | /* There were no equivalent states, remember the current one. |
8999 | * Technically the current state is not proven to be safe yet, | |
f4d7e40a | 9000 | * but it will either reach outer most bpf_exit (which means it's safe) |
2589726d | 9001 | * or it will be rejected. When there are no loops the verifier won't be |
f4d7e40a | 9002 | * seeing this tuple (frame[0].callsite, frame[1].callsite, .. insn_idx) |
2589726d AS |
9003 | * again on the way to bpf_exit. |
9004 | * When looping the sl->state.branches will be > 0 and this state | |
9005 | * will not be considered for equivalence until branches == 0. | |
f1bca824 | 9006 | */ |
638f5b90 | 9007 | new_sl = kzalloc(sizeof(struct bpf_verifier_state_list), GFP_KERNEL); |
f1bca824 AS |
9008 | if (!new_sl) |
9009 | return -ENOMEM; | |
06ee7115 AS |
9010 | env->total_states++; |
9011 | env->peak_states++; | |
2589726d AS |
9012 | env->prev_jmps_processed = env->jmps_processed; |
9013 | env->prev_insn_processed = env->insn_processed; | |
f1bca824 AS |
9014 | |
9015 | /* add new state to the head of linked list */ | |
679c782d EC |
9016 | new = &new_sl->state; |
9017 | err = copy_verifier_state(new, cur); | |
1969db47 | 9018 | if (err) { |
679c782d | 9019 | free_verifier_state(new, false); |
1969db47 AS |
9020 | kfree(new_sl); |
9021 | return err; | |
9022 | } | |
dc2a4ebc | 9023 | new->insn_idx = insn_idx; |
2589726d AS |
9024 | WARN_ONCE(new->branches != 1, |
9025 | "BUG is_state_visited:branches_to_explore=%d insn %d\n", new->branches, insn_idx); | |
b5dc0163 | 9026 | |
2589726d | 9027 | cur->parent = new; |
b5dc0163 AS |
9028 | cur->first_insn_idx = insn_idx; |
9029 | clear_jmp_history(cur); | |
5d839021 AS |
9030 | new_sl->next = *explored_state(env, insn_idx); |
9031 | *explored_state(env, insn_idx) = new_sl; | |
7640ead9 JK |
9032 | /* connect new state to parentage chain. Current frame needs all |
9033 | * registers connected. Only r6 - r9 of the callers are alive (pushed | |
9034 | * to the stack implicitly by JITs) so in callers' frames connect just | |
9035 | * r6 - r9 as an optimization. Callers will have r1 - r5 connected to | |
9036 | * the state of the call instruction (with WRITTEN set), and r0 comes | |
9037 | * from callee with its full parentage chain, anyway. | |
9038 | */ | |
8e9cd9ce EC |
9039 | /* clear write marks in current state: the writes we did are not writes |
9040 | * our child did, so they don't screen off its reads from us. | |
9041 | * (There are no read marks in current state, because reads always mark | |
9042 | * their parent and current state never has children yet. Only | |
9043 | * explored_states can get read marks.) | |
9044 | */ | |
eea1c227 AS |
9045 | for (j = 0; j <= cur->curframe; j++) { |
9046 | for (i = j < cur->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) | |
9047 | cur->frame[j]->regs[i].parent = &new->frame[j]->regs[i]; | |
9048 | for (i = 0; i < BPF_REG_FP; i++) | |
9049 | cur->frame[j]->regs[i].live = REG_LIVE_NONE; | |
9050 | } | |
f4d7e40a AS |
9051 | |
9052 | /* all stack frames are accessible from callee, clear them all */ | |
9053 | for (j = 0; j <= cur->curframe; j++) { | |
9054 | struct bpf_func_state *frame = cur->frame[j]; | |
679c782d | 9055 | struct bpf_func_state *newframe = new->frame[j]; |
f4d7e40a | 9056 | |
679c782d | 9057 | for (i = 0; i < frame->allocated_stack / BPF_REG_SIZE; i++) { |
cc2b14d5 | 9058 | frame->stack[i].spilled_ptr.live = REG_LIVE_NONE; |
679c782d EC |
9059 | frame->stack[i].spilled_ptr.parent = |
9060 | &newframe->stack[i].spilled_ptr; | |
9061 | } | |
f4d7e40a | 9062 | } |
f1bca824 AS |
9063 | return 0; |
9064 | } | |
9065 | ||
c64b7983 JS |
9066 | /* Return true if it's OK to have the same insn return a different type. */ |
9067 | static bool reg_type_mismatch_ok(enum bpf_reg_type type) | |
9068 | { | |
9069 | switch (type) { | |
9070 | case PTR_TO_CTX: | |
9071 | case PTR_TO_SOCKET: | |
9072 | case PTR_TO_SOCKET_OR_NULL: | |
46f8bc92 MKL |
9073 | case PTR_TO_SOCK_COMMON: |
9074 | case PTR_TO_SOCK_COMMON_OR_NULL: | |
655a51e5 MKL |
9075 | case PTR_TO_TCP_SOCK: |
9076 | case PTR_TO_TCP_SOCK_OR_NULL: | |
fada7fdc | 9077 | case PTR_TO_XDP_SOCK: |
2a02759e | 9078 | case PTR_TO_BTF_ID: |
b121b341 | 9079 | case PTR_TO_BTF_ID_OR_NULL: |
c64b7983 JS |
9080 | return false; |
9081 | default: | |
9082 | return true; | |
9083 | } | |
9084 | } | |
9085 | ||
9086 | /* If an instruction was previously used with particular pointer types, then we | |
9087 | * need to be careful to avoid cases such as the below, where it may be ok | |
9088 | * for one branch accessing the pointer, but not ok for the other branch: | |
9089 | * | |
9090 | * R1 = sock_ptr | |
9091 | * goto X; | |
9092 | * ... | |
9093 | * R1 = some_other_valid_ptr; | |
9094 | * goto X; | |
9095 | * ... | |
9096 | * R2 = *(u32 *)(R1 + 0); | |
9097 | */ | |
9098 | static bool reg_type_mismatch(enum bpf_reg_type src, enum bpf_reg_type prev) | |
9099 | { | |
9100 | return src != prev && (!reg_type_mismatch_ok(src) || | |
9101 | !reg_type_mismatch_ok(prev)); | |
9102 | } | |
9103 | ||
58e2af8b | 9104 | static int do_check(struct bpf_verifier_env *env) |
17a52670 | 9105 | { |
6f8a57cc | 9106 | bool pop_log = !(env->log.level & BPF_LOG_LEVEL2); |
51c39bb1 | 9107 | struct bpf_verifier_state *state = env->cur_state; |
17a52670 | 9108 | struct bpf_insn *insns = env->prog->insnsi; |
638f5b90 | 9109 | struct bpf_reg_state *regs; |
06ee7115 | 9110 | int insn_cnt = env->prog->len; |
17a52670 | 9111 | bool do_print_state = false; |
b5dc0163 | 9112 | int prev_insn_idx = -1; |
17a52670 | 9113 | |
17a52670 AS |
9114 | for (;;) { |
9115 | struct bpf_insn *insn; | |
9116 | u8 class; | |
9117 | int err; | |
9118 | ||
b5dc0163 | 9119 | env->prev_insn_idx = prev_insn_idx; |
c08435ec | 9120 | if (env->insn_idx >= insn_cnt) { |
61bd5218 | 9121 | verbose(env, "invalid insn idx %d insn_cnt %d\n", |
c08435ec | 9122 | env->insn_idx, insn_cnt); |
17a52670 AS |
9123 | return -EFAULT; |
9124 | } | |
9125 | ||
c08435ec | 9126 | insn = &insns[env->insn_idx]; |
17a52670 AS |
9127 | class = BPF_CLASS(insn->code); |
9128 | ||
06ee7115 | 9129 | if (++env->insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) { |
61bd5218 JK |
9130 | verbose(env, |
9131 | "BPF program is too large. Processed %d insn\n", | |
06ee7115 | 9132 | env->insn_processed); |
17a52670 AS |
9133 | return -E2BIG; |
9134 | } | |
9135 | ||
c08435ec | 9136 | err = is_state_visited(env, env->insn_idx); |
f1bca824 AS |
9137 | if (err < 0) |
9138 | return err; | |
9139 | if (err == 1) { | |
9140 | /* found equivalent state, can prune the search */ | |
06ee7115 | 9141 | if (env->log.level & BPF_LOG_LEVEL) { |
f1bca824 | 9142 | if (do_print_state) |
979d63d5 DB |
9143 | verbose(env, "\nfrom %d to %d%s: safe\n", |
9144 | env->prev_insn_idx, env->insn_idx, | |
9145 | env->cur_state->speculative ? | |
9146 | " (speculative execution)" : ""); | |
f1bca824 | 9147 | else |
c08435ec | 9148 | verbose(env, "%d: safe\n", env->insn_idx); |
f1bca824 AS |
9149 | } |
9150 | goto process_bpf_exit; | |
9151 | } | |
9152 | ||
c3494801 AS |
9153 | if (signal_pending(current)) |
9154 | return -EAGAIN; | |
9155 | ||
3c2ce60b DB |
9156 | if (need_resched()) |
9157 | cond_resched(); | |
9158 | ||
06ee7115 AS |
9159 | if (env->log.level & BPF_LOG_LEVEL2 || |
9160 | (env->log.level & BPF_LOG_LEVEL && do_print_state)) { | |
9161 | if (env->log.level & BPF_LOG_LEVEL2) | |
c08435ec | 9162 | verbose(env, "%d:", env->insn_idx); |
c5fc9692 | 9163 | else |
979d63d5 DB |
9164 | verbose(env, "\nfrom %d to %d%s:", |
9165 | env->prev_insn_idx, env->insn_idx, | |
9166 | env->cur_state->speculative ? | |
9167 | " (speculative execution)" : ""); | |
f4d7e40a | 9168 | print_verifier_state(env, state->frame[state->curframe]); |
17a52670 AS |
9169 | do_print_state = false; |
9170 | } | |
9171 | ||
06ee7115 | 9172 | if (env->log.level & BPF_LOG_LEVEL) { |
7105e828 DB |
9173 | const struct bpf_insn_cbs cbs = { |
9174 | .cb_print = verbose, | |
abe08840 | 9175 | .private_data = env, |
7105e828 DB |
9176 | }; |
9177 | ||
c08435ec DB |
9178 | verbose_linfo(env, env->insn_idx, "; "); |
9179 | verbose(env, "%d: ", env->insn_idx); | |
abe08840 | 9180 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); |
17a52670 AS |
9181 | } |
9182 | ||
cae1927c | 9183 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
c08435ec DB |
9184 | err = bpf_prog_offload_verify_insn(env, env->insn_idx, |
9185 | env->prev_insn_idx); | |
cae1927c JK |
9186 | if (err) |
9187 | return err; | |
9188 | } | |
13a27dfc | 9189 | |
638f5b90 | 9190 | regs = cur_regs(env); |
51c39bb1 | 9191 | env->insn_aux_data[env->insn_idx].seen = env->pass_cnt; |
b5dc0163 | 9192 | prev_insn_idx = env->insn_idx; |
fd978bf7 | 9193 | |
17a52670 | 9194 | if (class == BPF_ALU || class == BPF_ALU64) { |
1be7f75d | 9195 | err = check_alu_op(env, insn); |
17a52670 AS |
9196 | if (err) |
9197 | return err; | |
9198 | ||
9199 | } else if (class == BPF_LDX) { | |
3df126f3 | 9200 | enum bpf_reg_type *prev_src_type, src_reg_type; |
9bac3d6d AS |
9201 | |
9202 | /* check for reserved fields is already done */ | |
9203 | ||
17a52670 | 9204 | /* check src operand */ |
dc503a8a | 9205 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
9206 | if (err) |
9207 | return err; | |
9208 | ||
dc503a8a | 9209 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
17a52670 AS |
9210 | if (err) |
9211 | return err; | |
9212 | ||
725f9dcd AS |
9213 | src_reg_type = regs[insn->src_reg].type; |
9214 | ||
17a52670 AS |
9215 | /* check that memory (src_reg + off) is readable, |
9216 | * the state of dst_reg will be updated by this func | |
9217 | */ | |
c08435ec DB |
9218 | err = check_mem_access(env, env->insn_idx, insn->src_reg, |
9219 | insn->off, BPF_SIZE(insn->code), | |
9220 | BPF_READ, insn->dst_reg, false); | |
17a52670 AS |
9221 | if (err) |
9222 | return err; | |
9223 | ||
c08435ec | 9224 | prev_src_type = &env->insn_aux_data[env->insn_idx].ptr_type; |
3df126f3 JK |
9225 | |
9226 | if (*prev_src_type == NOT_INIT) { | |
9bac3d6d AS |
9227 | /* saw a valid insn |
9228 | * dst_reg = *(u32 *)(src_reg + off) | |
3df126f3 | 9229 | * save type to validate intersecting paths |
9bac3d6d | 9230 | */ |
3df126f3 | 9231 | *prev_src_type = src_reg_type; |
9bac3d6d | 9232 | |
c64b7983 | 9233 | } else if (reg_type_mismatch(src_reg_type, *prev_src_type)) { |
9bac3d6d AS |
9234 | /* ABuser program is trying to use the same insn |
9235 | * dst_reg = *(u32*) (src_reg + off) | |
9236 | * with different pointer types: | |
9237 | * src_reg == ctx in one branch and | |
9238 | * src_reg == stack|map in some other branch. | |
9239 | * Reject it. | |
9240 | */ | |
61bd5218 | 9241 | verbose(env, "same insn cannot be used with different pointers\n"); |
9bac3d6d AS |
9242 | return -EINVAL; |
9243 | } | |
9244 | ||
17a52670 | 9245 | } else if (class == BPF_STX) { |
3df126f3 | 9246 | enum bpf_reg_type *prev_dst_type, dst_reg_type; |
d691f9e8 | 9247 | |
17a52670 | 9248 | if (BPF_MODE(insn->code) == BPF_XADD) { |
c08435ec | 9249 | err = check_xadd(env, env->insn_idx, insn); |
17a52670 AS |
9250 | if (err) |
9251 | return err; | |
c08435ec | 9252 | env->insn_idx++; |
17a52670 AS |
9253 | continue; |
9254 | } | |
9255 | ||
17a52670 | 9256 | /* check src1 operand */ |
dc503a8a | 9257 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
9258 | if (err) |
9259 | return err; | |
9260 | /* check src2 operand */ | |
dc503a8a | 9261 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
9262 | if (err) |
9263 | return err; | |
9264 | ||
d691f9e8 AS |
9265 | dst_reg_type = regs[insn->dst_reg].type; |
9266 | ||
17a52670 | 9267 | /* check that memory (dst_reg + off) is writeable */ |
c08435ec DB |
9268 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
9269 | insn->off, BPF_SIZE(insn->code), | |
9270 | BPF_WRITE, insn->src_reg, false); | |
17a52670 AS |
9271 | if (err) |
9272 | return err; | |
9273 | ||
c08435ec | 9274 | prev_dst_type = &env->insn_aux_data[env->insn_idx].ptr_type; |
3df126f3 JK |
9275 | |
9276 | if (*prev_dst_type == NOT_INIT) { | |
9277 | *prev_dst_type = dst_reg_type; | |
c64b7983 | 9278 | } else if (reg_type_mismatch(dst_reg_type, *prev_dst_type)) { |
61bd5218 | 9279 | verbose(env, "same insn cannot be used with different pointers\n"); |
d691f9e8 AS |
9280 | return -EINVAL; |
9281 | } | |
9282 | ||
17a52670 AS |
9283 | } else if (class == BPF_ST) { |
9284 | if (BPF_MODE(insn->code) != BPF_MEM || | |
9285 | insn->src_reg != BPF_REG_0) { | |
61bd5218 | 9286 | verbose(env, "BPF_ST uses reserved fields\n"); |
17a52670 AS |
9287 | return -EINVAL; |
9288 | } | |
9289 | /* check src operand */ | |
dc503a8a | 9290 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
9291 | if (err) |
9292 | return err; | |
9293 | ||
f37a8cb8 | 9294 | if (is_ctx_reg(env, insn->dst_reg)) { |
9d2be44a | 9295 | verbose(env, "BPF_ST stores into R%d %s is not allowed\n", |
2a159c6f DB |
9296 | insn->dst_reg, |
9297 | reg_type_str[reg_state(env, insn->dst_reg)->type]); | |
f37a8cb8 DB |
9298 | return -EACCES; |
9299 | } | |
9300 | ||
17a52670 | 9301 | /* check that memory (dst_reg + off) is writeable */ |
c08435ec DB |
9302 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
9303 | insn->off, BPF_SIZE(insn->code), | |
9304 | BPF_WRITE, -1, false); | |
17a52670 AS |
9305 | if (err) |
9306 | return err; | |
9307 | ||
092ed096 | 9308 | } else if (class == BPF_JMP || class == BPF_JMP32) { |
17a52670 AS |
9309 | u8 opcode = BPF_OP(insn->code); |
9310 | ||
2589726d | 9311 | env->jmps_processed++; |
17a52670 AS |
9312 | if (opcode == BPF_CALL) { |
9313 | if (BPF_SRC(insn->code) != BPF_K || | |
9314 | insn->off != 0 || | |
f4d7e40a AS |
9315 | (insn->src_reg != BPF_REG_0 && |
9316 | insn->src_reg != BPF_PSEUDO_CALL) || | |
092ed096 JW |
9317 | insn->dst_reg != BPF_REG_0 || |
9318 | class == BPF_JMP32) { | |
61bd5218 | 9319 | verbose(env, "BPF_CALL uses reserved fields\n"); |
17a52670 AS |
9320 | return -EINVAL; |
9321 | } | |
9322 | ||
d83525ca AS |
9323 | if (env->cur_state->active_spin_lock && |
9324 | (insn->src_reg == BPF_PSEUDO_CALL || | |
9325 | insn->imm != BPF_FUNC_spin_unlock)) { | |
9326 | verbose(env, "function calls are not allowed while holding a lock\n"); | |
9327 | return -EINVAL; | |
9328 | } | |
f4d7e40a | 9329 | if (insn->src_reg == BPF_PSEUDO_CALL) |
c08435ec | 9330 | err = check_func_call(env, insn, &env->insn_idx); |
f4d7e40a | 9331 | else |
c08435ec | 9332 | err = check_helper_call(env, insn->imm, env->insn_idx); |
17a52670 AS |
9333 | if (err) |
9334 | return err; | |
9335 | ||
9336 | } else if (opcode == BPF_JA) { | |
9337 | if (BPF_SRC(insn->code) != BPF_K || | |
9338 | insn->imm != 0 || | |
9339 | insn->src_reg != BPF_REG_0 || | |
092ed096 JW |
9340 | insn->dst_reg != BPF_REG_0 || |
9341 | class == BPF_JMP32) { | |
61bd5218 | 9342 | verbose(env, "BPF_JA uses reserved fields\n"); |
17a52670 AS |
9343 | return -EINVAL; |
9344 | } | |
9345 | ||
c08435ec | 9346 | env->insn_idx += insn->off + 1; |
17a52670 AS |
9347 | continue; |
9348 | ||
9349 | } else if (opcode == BPF_EXIT) { | |
9350 | if (BPF_SRC(insn->code) != BPF_K || | |
9351 | insn->imm != 0 || | |
9352 | insn->src_reg != BPF_REG_0 || | |
092ed096 JW |
9353 | insn->dst_reg != BPF_REG_0 || |
9354 | class == BPF_JMP32) { | |
61bd5218 | 9355 | verbose(env, "BPF_EXIT uses reserved fields\n"); |
17a52670 AS |
9356 | return -EINVAL; |
9357 | } | |
9358 | ||
d83525ca AS |
9359 | if (env->cur_state->active_spin_lock) { |
9360 | verbose(env, "bpf_spin_unlock is missing\n"); | |
9361 | return -EINVAL; | |
9362 | } | |
9363 | ||
f4d7e40a AS |
9364 | if (state->curframe) { |
9365 | /* exit from nested function */ | |
c08435ec | 9366 | err = prepare_func_exit(env, &env->insn_idx); |
f4d7e40a AS |
9367 | if (err) |
9368 | return err; | |
9369 | do_print_state = true; | |
9370 | continue; | |
9371 | } | |
9372 | ||
fd978bf7 JS |
9373 | err = check_reference_leak(env); |
9374 | if (err) | |
9375 | return err; | |
9376 | ||
390ee7e2 AS |
9377 | err = check_return_code(env); |
9378 | if (err) | |
9379 | return err; | |
f1bca824 | 9380 | process_bpf_exit: |
2589726d | 9381 | update_branch_counts(env, env->cur_state); |
b5dc0163 | 9382 | err = pop_stack(env, &prev_insn_idx, |
6f8a57cc | 9383 | &env->insn_idx, pop_log); |
638f5b90 AS |
9384 | if (err < 0) { |
9385 | if (err != -ENOENT) | |
9386 | return err; | |
17a52670 AS |
9387 | break; |
9388 | } else { | |
9389 | do_print_state = true; | |
9390 | continue; | |
9391 | } | |
9392 | } else { | |
c08435ec | 9393 | err = check_cond_jmp_op(env, insn, &env->insn_idx); |
17a52670 AS |
9394 | if (err) |
9395 | return err; | |
9396 | } | |
9397 | } else if (class == BPF_LD) { | |
9398 | u8 mode = BPF_MODE(insn->code); | |
9399 | ||
9400 | if (mode == BPF_ABS || mode == BPF_IND) { | |
ddd872bc AS |
9401 | err = check_ld_abs(env, insn); |
9402 | if (err) | |
9403 | return err; | |
9404 | ||
17a52670 AS |
9405 | } else if (mode == BPF_IMM) { |
9406 | err = check_ld_imm(env, insn); | |
9407 | if (err) | |
9408 | return err; | |
9409 | ||
c08435ec | 9410 | env->insn_idx++; |
51c39bb1 | 9411 | env->insn_aux_data[env->insn_idx].seen = env->pass_cnt; |
17a52670 | 9412 | } else { |
61bd5218 | 9413 | verbose(env, "invalid BPF_LD mode\n"); |
17a52670 AS |
9414 | return -EINVAL; |
9415 | } | |
9416 | } else { | |
61bd5218 | 9417 | verbose(env, "unknown insn class %d\n", class); |
17a52670 AS |
9418 | return -EINVAL; |
9419 | } | |
9420 | ||
c08435ec | 9421 | env->insn_idx++; |
17a52670 AS |
9422 | } |
9423 | ||
9424 | return 0; | |
9425 | } | |
9426 | ||
56f668df MKL |
9427 | static int check_map_prealloc(struct bpf_map *map) |
9428 | { | |
9429 | return (map->map_type != BPF_MAP_TYPE_HASH && | |
bcc6b1b7 MKL |
9430 | map->map_type != BPF_MAP_TYPE_PERCPU_HASH && |
9431 | map->map_type != BPF_MAP_TYPE_HASH_OF_MAPS) || | |
56f668df MKL |
9432 | !(map->map_flags & BPF_F_NO_PREALLOC); |
9433 | } | |
9434 | ||
d83525ca AS |
9435 | static bool is_tracing_prog_type(enum bpf_prog_type type) |
9436 | { | |
9437 | switch (type) { | |
9438 | case BPF_PROG_TYPE_KPROBE: | |
9439 | case BPF_PROG_TYPE_TRACEPOINT: | |
9440 | case BPF_PROG_TYPE_PERF_EVENT: | |
9441 | case BPF_PROG_TYPE_RAW_TRACEPOINT: | |
9442 | return true; | |
9443 | default: | |
9444 | return false; | |
9445 | } | |
9446 | } | |
9447 | ||
94dacdbd TG |
9448 | static bool is_preallocated_map(struct bpf_map *map) |
9449 | { | |
9450 | if (!check_map_prealloc(map)) | |
9451 | return false; | |
9452 | if (map->inner_map_meta && !check_map_prealloc(map->inner_map_meta)) | |
9453 | return false; | |
9454 | return true; | |
9455 | } | |
9456 | ||
61bd5218 JK |
9457 | static int check_map_prog_compatibility(struct bpf_verifier_env *env, |
9458 | struct bpf_map *map, | |
fdc15d38 AS |
9459 | struct bpf_prog *prog) |
9460 | ||
9461 | { | |
7e40781c | 9462 | enum bpf_prog_type prog_type = resolve_prog_type(prog); |
94dacdbd TG |
9463 | /* |
9464 | * Validate that trace type programs use preallocated hash maps. | |
9465 | * | |
9466 | * For programs attached to PERF events this is mandatory as the | |
9467 | * perf NMI can hit any arbitrary code sequence. | |
9468 | * | |
9469 | * All other trace types using preallocated hash maps are unsafe as | |
9470 | * well because tracepoint or kprobes can be inside locked regions | |
9471 | * of the memory allocator or at a place where a recursion into the | |
9472 | * memory allocator would see inconsistent state. | |
9473 | * | |
2ed905c5 TG |
9474 | * On RT enabled kernels run-time allocation of all trace type |
9475 | * programs is strictly prohibited due to lock type constraints. On | |
9476 | * !RT kernels it is allowed for backwards compatibility reasons for | |
9477 | * now, but warnings are emitted so developers are made aware of | |
9478 | * the unsafety and can fix their programs before this is enforced. | |
56f668df | 9479 | */ |
7e40781c UP |
9480 | if (is_tracing_prog_type(prog_type) && !is_preallocated_map(map)) { |
9481 | if (prog_type == BPF_PROG_TYPE_PERF_EVENT) { | |
61bd5218 | 9482 | verbose(env, "perf_event programs can only use preallocated hash map\n"); |
56f668df MKL |
9483 | return -EINVAL; |
9484 | } | |
2ed905c5 TG |
9485 | if (IS_ENABLED(CONFIG_PREEMPT_RT)) { |
9486 | verbose(env, "trace type programs can only use preallocated hash map\n"); | |
9487 | return -EINVAL; | |
9488 | } | |
94dacdbd TG |
9489 | WARN_ONCE(1, "trace type BPF program uses run-time allocation\n"); |
9490 | verbose(env, "trace type programs with run-time allocated hash maps are unsafe. Switch to preallocated hash maps.\n"); | |
fdc15d38 | 9491 | } |
a3884572 | 9492 | |
7e40781c UP |
9493 | if ((is_tracing_prog_type(prog_type) || |
9494 | prog_type == BPF_PROG_TYPE_SOCKET_FILTER) && | |
d83525ca AS |
9495 | map_value_has_spin_lock(map)) { |
9496 | verbose(env, "tracing progs cannot use bpf_spin_lock yet\n"); | |
9497 | return -EINVAL; | |
9498 | } | |
9499 | ||
a3884572 | 9500 | if ((bpf_prog_is_dev_bound(prog->aux) || bpf_map_is_dev_bound(map)) && |
09728266 | 9501 | !bpf_offload_prog_map_match(prog, map)) { |
a3884572 JK |
9502 | verbose(env, "offload device mismatch between prog and map\n"); |
9503 | return -EINVAL; | |
9504 | } | |
9505 | ||
85d33df3 MKL |
9506 | if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) { |
9507 | verbose(env, "bpf_struct_ops map cannot be used in prog\n"); | |
9508 | return -EINVAL; | |
9509 | } | |
9510 | ||
1e6c62a8 AS |
9511 | if (prog->aux->sleepable) |
9512 | switch (map->map_type) { | |
9513 | case BPF_MAP_TYPE_HASH: | |
9514 | case BPF_MAP_TYPE_LRU_HASH: | |
9515 | case BPF_MAP_TYPE_ARRAY: | |
9516 | if (!is_preallocated_map(map)) { | |
9517 | verbose(env, | |
9518 | "Sleepable programs can only use preallocated hash maps\n"); | |
9519 | return -EINVAL; | |
9520 | } | |
9521 | break; | |
9522 | default: | |
9523 | verbose(env, | |
9524 | "Sleepable programs can only use array and hash maps\n"); | |
9525 | return -EINVAL; | |
9526 | } | |
9527 | ||
fdc15d38 AS |
9528 | return 0; |
9529 | } | |
9530 | ||
b741f163 RG |
9531 | static bool bpf_map_is_cgroup_storage(struct bpf_map *map) |
9532 | { | |
9533 | return (map->map_type == BPF_MAP_TYPE_CGROUP_STORAGE || | |
9534 | map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE); | |
9535 | } | |
9536 | ||
0246e64d AS |
9537 | /* look for pseudo eBPF instructions that access map FDs and |
9538 | * replace them with actual map pointers | |
9539 | */ | |
58e2af8b | 9540 | static int replace_map_fd_with_map_ptr(struct bpf_verifier_env *env) |
0246e64d AS |
9541 | { |
9542 | struct bpf_insn *insn = env->prog->insnsi; | |
9543 | int insn_cnt = env->prog->len; | |
fdc15d38 | 9544 | int i, j, err; |
0246e64d | 9545 | |
f1f7714e | 9546 | err = bpf_prog_calc_tag(env->prog); |
aafe6ae9 DB |
9547 | if (err) |
9548 | return err; | |
9549 | ||
0246e64d | 9550 | for (i = 0; i < insn_cnt; i++, insn++) { |
9bac3d6d | 9551 | if (BPF_CLASS(insn->code) == BPF_LDX && |
d691f9e8 | 9552 | (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) { |
61bd5218 | 9553 | verbose(env, "BPF_LDX uses reserved fields\n"); |
9bac3d6d AS |
9554 | return -EINVAL; |
9555 | } | |
9556 | ||
d691f9e8 AS |
9557 | if (BPF_CLASS(insn->code) == BPF_STX && |
9558 | ((BPF_MODE(insn->code) != BPF_MEM && | |
9559 | BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) { | |
61bd5218 | 9560 | verbose(env, "BPF_STX uses reserved fields\n"); |
d691f9e8 AS |
9561 | return -EINVAL; |
9562 | } | |
9563 | ||
0246e64d | 9564 | if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) { |
d8eca5bb | 9565 | struct bpf_insn_aux_data *aux; |
0246e64d AS |
9566 | struct bpf_map *map; |
9567 | struct fd f; | |
d8eca5bb | 9568 | u64 addr; |
0246e64d AS |
9569 | |
9570 | if (i == insn_cnt - 1 || insn[1].code != 0 || | |
9571 | insn[1].dst_reg != 0 || insn[1].src_reg != 0 || | |
9572 | insn[1].off != 0) { | |
61bd5218 | 9573 | verbose(env, "invalid bpf_ld_imm64 insn\n"); |
0246e64d AS |
9574 | return -EINVAL; |
9575 | } | |
9576 | ||
d8eca5bb | 9577 | if (insn[0].src_reg == 0) |
0246e64d AS |
9578 | /* valid generic load 64-bit imm */ |
9579 | goto next_insn; | |
9580 | ||
d8eca5bb DB |
9581 | /* In final convert_pseudo_ld_imm64() step, this is |
9582 | * converted into regular 64-bit imm load insn. | |
9583 | */ | |
9584 | if ((insn[0].src_reg != BPF_PSEUDO_MAP_FD && | |
9585 | insn[0].src_reg != BPF_PSEUDO_MAP_VALUE) || | |
9586 | (insn[0].src_reg == BPF_PSEUDO_MAP_FD && | |
9587 | insn[1].imm != 0)) { | |
9588 | verbose(env, | |
9589 | "unrecognized bpf_ld_imm64 insn\n"); | |
0246e64d AS |
9590 | return -EINVAL; |
9591 | } | |
9592 | ||
20182390 | 9593 | f = fdget(insn[0].imm); |
c2101297 | 9594 | map = __bpf_map_get(f); |
0246e64d | 9595 | if (IS_ERR(map)) { |
61bd5218 | 9596 | verbose(env, "fd %d is not pointing to valid bpf_map\n", |
20182390 | 9597 | insn[0].imm); |
0246e64d AS |
9598 | return PTR_ERR(map); |
9599 | } | |
9600 | ||
61bd5218 | 9601 | err = check_map_prog_compatibility(env, map, env->prog); |
fdc15d38 AS |
9602 | if (err) { |
9603 | fdput(f); | |
9604 | return err; | |
9605 | } | |
9606 | ||
d8eca5bb DB |
9607 | aux = &env->insn_aux_data[i]; |
9608 | if (insn->src_reg == BPF_PSEUDO_MAP_FD) { | |
9609 | addr = (unsigned long)map; | |
9610 | } else { | |
9611 | u32 off = insn[1].imm; | |
9612 | ||
9613 | if (off >= BPF_MAX_VAR_OFF) { | |
9614 | verbose(env, "direct value offset of %u is not allowed\n", off); | |
9615 | fdput(f); | |
9616 | return -EINVAL; | |
9617 | } | |
9618 | ||
9619 | if (!map->ops->map_direct_value_addr) { | |
9620 | verbose(env, "no direct value access support for this map type\n"); | |
9621 | fdput(f); | |
9622 | return -EINVAL; | |
9623 | } | |
9624 | ||
9625 | err = map->ops->map_direct_value_addr(map, &addr, off); | |
9626 | if (err) { | |
9627 | verbose(env, "invalid access to map value pointer, value_size=%u off=%u\n", | |
9628 | map->value_size, off); | |
9629 | fdput(f); | |
9630 | return err; | |
9631 | } | |
9632 | ||
9633 | aux->map_off = off; | |
9634 | addr += off; | |
9635 | } | |
9636 | ||
9637 | insn[0].imm = (u32)addr; | |
9638 | insn[1].imm = addr >> 32; | |
0246e64d AS |
9639 | |
9640 | /* check whether we recorded this map already */ | |
d8eca5bb | 9641 | for (j = 0; j < env->used_map_cnt; j++) { |
0246e64d | 9642 | if (env->used_maps[j] == map) { |
d8eca5bb | 9643 | aux->map_index = j; |
0246e64d AS |
9644 | fdput(f); |
9645 | goto next_insn; | |
9646 | } | |
d8eca5bb | 9647 | } |
0246e64d AS |
9648 | |
9649 | if (env->used_map_cnt >= MAX_USED_MAPS) { | |
9650 | fdput(f); | |
9651 | return -E2BIG; | |
9652 | } | |
9653 | ||
0246e64d AS |
9654 | /* hold the map. If the program is rejected by verifier, |
9655 | * the map will be released by release_maps() or it | |
9656 | * will be used by the valid program until it's unloaded | |
ab7f5bf0 | 9657 | * and all maps are released in free_used_maps() |
0246e64d | 9658 | */ |
1e0bd5a0 | 9659 | bpf_map_inc(map); |
d8eca5bb DB |
9660 | |
9661 | aux->map_index = env->used_map_cnt; | |
92117d84 AS |
9662 | env->used_maps[env->used_map_cnt++] = map; |
9663 | ||
b741f163 | 9664 | if (bpf_map_is_cgroup_storage(map) && |
e4730423 | 9665 | bpf_cgroup_storage_assign(env->prog->aux, map)) { |
b741f163 | 9666 | verbose(env, "only one cgroup storage of each type is allowed\n"); |
de9cbbaa RG |
9667 | fdput(f); |
9668 | return -EBUSY; | |
9669 | } | |
9670 | ||
0246e64d AS |
9671 | fdput(f); |
9672 | next_insn: | |
9673 | insn++; | |
9674 | i++; | |
5e581dad DB |
9675 | continue; |
9676 | } | |
9677 | ||
9678 | /* Basic sanity check before we invest more work here. */ | |
9679 | if (!bpf_opcode_in_insntable(insn->code)) { | |
9680 | verbose(env, "unknown opcode %02x\n", insn->code); | |
9681 | return -EINVAL; | |
0246e64d AS |
9682 | } |
9683 | } | |
9684 | ||
9685 | /* now all pseudo BPF_LD_IMM64 instructions load valid | |
9686 | * 'struct bpf_map *' into a register instead of user map_fd. | |
9687 | * These pointers will be used later by verifier to validate map access. | |
9688 | */ | |
9689 | return 0; | |
9690 | } | |
9691 | ||
9692 | /* drop refcnt of maps used by the rejected program */ | |
58e2af8b | 9693 | static void release_maps(struct bpf_verifier_env *env) |
0246e64d | 9694 | { |
a2ea0746 DB |
9695 | __bpf_free_used_maps(env->prog->aux, env->used_maps, |
9696 | env->used_map_cnt); | |
0246e64d AS |
9697 | } |
9698 | ||
9699 | /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */ | |
58e2af8b | 9700 | static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env) |
0246e64d AS |
9701 | { |
9702 | struct bpf_insn *insn = env->prog->insnsi; | |
9703 | int insn_cnt = env->prog->len; | |
9704 | int i; | |
9705 | ||
9706 | for (i = 0; i < insn_cnt; i++, insn++) | |
9707 | if (insn->code == (BPF_LD | BPF_IMM | BPF_DW)) | |
9708 | insn->src_reg = 0; | |
9709 | } | |
9710 | ||
8041902d AS |
9711 | /* single env->prog->insni[off] instruction was replaced with the range |
9712 | * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying | |
9713 | * [0, off) and [off, end) to new locations, so the patched range stays zero | |
9714 | */ | |
b325fbca JW |
9715 | static int adjust_insn_aux_data(struct bpf_verifier_env *env, |
9716 | struct bpf_prog *new_prog, u32 off, u32 cnt) | |
8041902d AS |
9717 | { |
9718 | struct bpf_insn_aux_data *new_data, *old_data = env->insn_aux_data; | |
b325fbca JW |
9719 | struct bpf_insn *insn = new_prog->insnsi; |
9720 | u32 prog_len; | |
c131187d | 9721 | int i; |
8041902d | 9722 | |
b325fbca JW |
9723 | /* aux info at OFF always needs adjustment, no matter fast path |
9724 | * (cnt == 1) is taken or not. There is no guarantee INSN at OFF is the | |
9725 | * original insn at old prog. | |
9726 | */ | |
9727 | old_data[off].zext_dst = insn_has_def32(env, insn + off + cnt - 1); | |
9728 | ||
8041902d AS |
9729 | if (cnt == 1) |
9730 | return 0; | |
b325fbca | 9731 | prog_len = new_prog->len; |
fad953ce KC |
9732 | new_data = vzalloc(array_size(prog_len, |
9733 | sizeof(struct bpf_insn_aux_data))); | |
8041902d AS |
9734 | if (!new_data) |
9735 | return -ENOMEM; | |
9736 | memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off); | |
9737 | memcpy(new_data + off + cnt - 1, old_data + off, | |
9738 | sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1)); | |
b325fbca | 9739 | for (i = off; i < off + cnt - 1; i++) { |
51c39bb1 | 9740 | new_data[i].seen = env->pass_cnt; |
b325fbca JW |
9741 | new_data[i].zext_dst = insn_has_def32(env, insn + i); |
9742 | } | |
8041902d AS |
9743 | env->insn_aux_data = new_data; |
9744 | vfree(old_data); | |
9745 | return 0; | |
9746 | } | |
9747 | ||
cc8b0b92 AS |
9748 | static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len) |
9749 | { | |
9750 | int i; | |
9751 | ||
9752 | if (len == 1) | |
9753 | return; | |
4cb3d99c JW |
9754 | /* NOTE: fake 'exit' subprog should be updated as well. */ |
9755 | for (i = 0; i <= env->subprog_cnt; i++) { | |
afd59424 | 9756 | if (env->subprog_info[i].start <= off) |
cc8b0b92 | 9757 | continue; |
9c8105bd | 9758 | env->subprog_info[i].start += len - 1; |
cc8b0b92 AS |
9759 | } |
9760 | } | |
9761 | ||
a748c697 MF |
9762 | static void adjust_poke_descs(struct bpf_prog *prog, u32 len) |
9763 | { | |
9764 | struct bpf_jit_poke_descriptor *tab = prog->aux->poke_tab; | |
9765 | int i, sz = prog->aux->size_poke_tab; | |
9766 | struct bpf_jit_poke_descriptor *desc; | |
9767 | ||
9768 | for (i = 0; i < sz; i++) { | |
9769 | desc = &tab[i]; | |
9770 | desc->insn_idx += len - 1; | |
9771 | } | |
9772 | } | |
9773 | ||
8041902d AS |
9774 | static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off, |
9775 | const struct bpf_insn *patch, u32 len) | |
9776 | { | |
9777 | struct bpf_prog *new_prog; | |
9778 | ||
9779 | new_prog = bpf_patch_insn_single(env->prog, off, patch, len); | |
4f73379e AS |
9780 | if (IS_ERR(new_prog)) { |
9781 | if (PTR_ERR(new_prog) == -ERANGE) | |
9782 | verbose(env, | |
9783 | "insn %d cannot be patched due to 16-bit range\n", | |
9784 | env->insn_aux_data[off].orig_idx); | |
8041902d | 9785 | return NULL; |
4f73379e | 9786 | } |
b325fbca | 9787 | if (adjust_insn_aux_data(env, new_prog, off, len)) |
8041902d | 9788 | return NULL; |
cc8b0b92 | 9789 | adjust_subprog_starts(env, off, len); |
a748c697 | 9790 | adjust_poke_descs(new_prog, len); |
8041902d AS |
9791 | return new_prog; |
9792 | } | |
9793 | ||
52875a04 JK |
9794 | static int adjust_subprog_starts_after_remove(struct bpf_verifier_env *env, |
9795 | u32 off, u32 cnt) | |
9796 | { | |
9797 | int i, j; | |
9798 | ||
9799 | /* find first prog starting at or after off (first to remove) */ | |
9800 | for (i = 0; i < env->subprog_cnt; i++) | |
9801 | if (env->subprog_info[i].start >= off) | |
9802 | break; | |
9803 | /* find first prog starting at or after off + cnt (first to stay) */ | |
9804 | for (j = i; j < env->subprog_cnt; j++) | |
9805 | if (env->subprog_info[j].start >= off + cnt) | |
9806 | break; | |
9807 | /* if j doesn't start exactly at off + cnt, we are just removing | |
9808 | * the front of previous prog | |
9809 | */ | |
9810 | if (env->subprog_info[j].start != off + cnt) | |
9811 | j--; | |
9812 | ||
9813 | if (j > i) { | |
9814 | struct bpf_prog_aux *aux = env->prog->aux; | |
9815 | int move; | |
9816 | ||
9817 | /* move fake 'exit' subprog as well */ | |
9818 | move = env->subprog_cnt + 1 - j; | |
9819 | ||
9820 | memmove(env->subprog_info + i, | |
9821 | env->subprog_info + j, | |
9822 | sizeof(*env->subprog_info) * move); | |
9823 | env->subprog_cnt -= j - i; | |
9824 | ||
9825 | /* remove func_info */ | |
9826 | if (aux->func_info) { | |
9827 | move = aux->func_info_cnt - j; | |
9828 | ||
9829 | memmove(aux->func_info + i, | |
9830 | aux->func_info + j, | |
9831 | sizeof(*aux->func_info) * move); | |
9832 | aux->func_info_cnt -= j - i; | |
9833 | /* func_info->insn_off is set after all code rewrites, | |
9834 | * in adjust_btf_func() - no need to adjust | |
9835 | */ | |
9836 | } | |
9837 | } else { | |
9838 | /* convert i from "first prog to remove" to "first to adjust" */ | |
9839 | if (env->subprog_info[i].start == off) | |
9840 | i++; | |
9841 | } | |
9842 | ||
9843 | /* update fake 'exit' subprog as well */ | |
9844 | for (; i <= env->subprog_cnt; i++) | |
9845 | env->subprog_info[i].start -= cnt; | |
9846 | ||
9847 | return 0; | |
9848 | } | |
9849 | ||
9850 | static int bpf_adj_linfo_after_remove(struct bpf_verifier_env *env, u32 off, | |
9851 | u32 cnt) | |
9852 | { | |
9853 | struct bpf_prog *prog = env->prog; | |
9854 | u32 i, l_off, l_cnt, nr_linfo; | |
9855 | struct bpf_line_info *linfo; | |
9856 | ||
9857 | nr_linfo = prog->aux->nr_linfo; | |
9858 | if (!nr_linfo) | |
9859 | return 0; | |
9860 | ||
9861 | linfo = prog->aux->linfo; | |
9862 | ||
9863 | /* find first line info to remove, count lines to be removed */ | |
9864 | for (i = 0; i < nr_linfo; i++) | |
9865 | if (linfo[i].insn_off >= off) | |
9866 | break; | |
9867 | ||
9868 | l_off = i; | |
9869 | l_cnt = 0; | |
9870 | for (; i < nr_linfo; i++) | |
9871 | if (linfo[i].insn_off < off + cnt) | |
9872 | l_cnt++; | |
9873 | else | |
9874 | break; | |
9875 | ||
9876 | /* First live insn doesn't match first live linfo, it needs to "inherit" | |
9877 | * last removed linfo. prog is already modified, so prog->len == off | |
9878 | * means no live instructions after (tail of the program was removed). | |
9879 | */ | |
9880 | if (prog->len != off && l_cnt && | |
9881 | (i == nr_linfo || linfo[i].insn_off != off + cnt)) { | |
9882 | l_cnt--; | |
9883 | linfo[--i].insn_off = off + cnt; | |
9884 | } | |
9885 | ||
9886 | /* remove the line info which refer to the removed instructions */ | |
9887 | if (l_cnt) { | |
9888 | memmove(linfo + l_off, linfo + i, | |
9889 | sizeof(*linfo) * (nr_linfo - i)); | |
9890 | ||
9891 | prog->aux->nr_linfo -= l_cnt; | |
9892 | nr_linfo = prog->aux->nr_linfo; | |
9893 | } | |
9894 | ||
9895 | /* pull all linfo[i].insn_off >= off + cnt in by cnt */ | |
9896 | for (i = l_off; i < nr_linfo; i++) | |
9897 | linfo[i].insn_off -= cnt; | |
9898 | ||
9899 | /* fix up all subprogs (incl. 'exit') which start >= off */ | |
9900 | for (i = 0; i <= env->subprog_cnt; i++) | |
9901 | if (env->subprog_info[i].linfo_idx > l_off) { | |
9902 | /* program may have started in the removed region but | |
9903 | * may not be fully removed | |
9904 | */ | |
9905 | if (env->subprog_info[i].linfo_idx >= l_off + l_cnt) | |
9906 | env->subprog_info[i].linfo_idx -= l_cnt; | |
9907 | else | |
9908 | env->subprog_info[i].linfo_idx = l_off; | |
9909 | } | |
9910 | ||
9911 | return 0; | |
9912 | } | |
9913 | ||
9914 | static int verifier_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt) | |
9915 | { | |
9916 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
9917 | unsigned int orig_prog_len = env->prog->len; | |
9918 | int err; | |
9919 | ||
08ca90af JK |
9920 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
9921 | bpf_prog_offload_remove_insns(env, off, cnt); | |
9922 | ||
52875a04 JK |
9923 | err = bpf_remove_insns(env->prog, off, cnt); |
9924 | if (err) | |
9925 | return err; | |
9926 | ||
9927 | err = adjust_subprog_starts_after_remove(env, off, cnt); | |
9928 | if (err) | |
9929 | return err; | |
9930 | ||
9931 | err = bpf_adj_linfo_after_remove(env, off, cnt); | |
9932 | if (err) | |
9933 | return err; | |
9934 | ||
9935 | memmove(aux_data + off, aux_data + off + cnt, | |
9936 | sizeof(*aux_data) * (orig_prog_len - off - cnt)); | |
9937 | ||
9938 | return 0; | |
9939 | } | |
9940 | ||
2a5418a1 DB |
9941 | /* The verifier does more data flow analysis than llvm and will not |
9942 | * explore branches that are dead at run time. Malicious programs can | |
9943 | * have dead code too. Therefore replace all dead at-run-time code | |
9944 | * with 'ja -1'. | |
9945 | * | |
9946 | * Just nops are not optimal, e.g. if they would sit at the end of the | |
9947 | * program and through another bug we would manage to jump there, then | |
9948 | * we'd execute beyond program memory otherwise. Returning exception | |
9949 | * code also wouldn't work since we can have subprogs where the dead | |
9950 | * code could be located. | |
c131187d AS |
9951 | */ |
9952 | static void sanitize_dead_code(struct bpf_verifier_env *env) | |
9953 | { | |
9954 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
2a5418a1 | 9955 | struct bpf_insn trap = BPF_JMP_IMM(BPF_JA, 0, 0, -1); |
c131187d AS |
9956 | struct bpf_insn *insn = env->prog->insnsi; |
9957 | const int insn_cnt = env->prog->len; | |
9958 | int i; | |
9959 | ||
9960 | for (i = 0; i < insn_cnt; i++) { | |
9961 | if (aux_data[i].seen) | |
9962 | continue; | |
2a5418a1 | 9963 | memcpy(insn + i, &trap, sizeof(trap)); |
c131187d AS |
9964 | } |
9965 | } | |
9966 | ||
e2ae4ca2 JK |
9967 | static bool insn_is_cond_jump(u8 code) |
9968 | { | |
9969 | u8 op; | |
9970 | ||
092ed096 JW |
9971 | if (BPF_CLASS(code) == BPF_JMP32) |
9972 | return true; | |
9973 | ||
e2ae4ca2 JK |
9974 | if (BPF_CLASS(code) != BPF_JMP) |
9975 | return false; | |
9976 | ||
9977 | op = BPF_OP(code); | |
9978 | return op != BPF_JA && op != BPF_EXIT && op != BPF_CALL; | |
9979 | } | |
9980 | ||
9981 | static void opt_hard_wire_dead_code_branches(struct bpf_verifier_env *env) | |
9982 | { | |
9983 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
9984 | struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); | |
9985 | struct bpf_insn *insn = env->prog->insnsi; | |
9986 | const int insn_cnt = env->prog->len; | |
9987 | int i; | |
9988 | ||
9989 | for (i = 0; i < insn_cnt; i++, insn++) { | |
9990 | if (!insn_is_cond_jump(insn->code)) | |
9991 | continue; | |
9992 | ||
9993 | if (!aux_data[i + 1].seen) | |
9994 | ja.off = insn->off; | |
9995 | else if (!aux_data[i + 1 + insn->off].seen) | |
9996 | ja.off = 0; | |
9997 | else | |
9998 | continue; | |
9999 | ||
08ca90af JK |
10000 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
10001 | bpf_prog_offload_replace_insn(env, i, &ja); | |
10002 | ||
e2ae4ca2 JK |
10003 | memcpy(insn, &ja, sizeof(ja)); |
10004 | } | |
10005 | } | |
10006 | ||
52875a04 JK |
10007 | static int opt_remove_dead_code(struct bpf_verifier_env *env) |
10008 | { | |
10009 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
10010 | int insn_cnt = env->prog->len; | |
10011 | int i, err; | |
10012 | ||
10013 | for (i = 0; i < insn_cnt; i++) { | |
10014 | int j; | |
10015 | ||
10016 | j = 0; | |
10017 | while (i + j < insn_cnt && !aux_data[i + j].seen) | |
10018 | j++; | |
10019 | if (!j) | |
10020 | continue; | |
10021 | ||
10022 | err = verifier_remove_insns(env, i, j); | |
10023 | if (err) | |
10024 | return err; | |
10025 | insn_cnt = env->prog->len; | |
10026 | } | |
10027 | ||
10028 | return 0; | |
10029 | } | |
10030 | ||
a1b14abc JK |
10031 | static int opt_remove_nops(struct bpf_verifier_env *env) |
10032 | { | |
10033 | const struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); | |
10034 | struct bpf_insn *insn = env->prog->insnsi; | |
10035 | int insn_cnt = env->prog->len; | |
10036 | int i, err; | |
10037 | ||
10038 | for (i = 0; i < insn_cnt; i++) { | |
10039 | if (memcmp(&insn[i], &ja, sizeof(ja))) | |
10040 | continue; | |
10041 | ||
10042 | err = verifier_remove_insns(env, i, 1); | |
10043 | if (err) | |
10044 | return err; | |
10045 | insn_cnt--; | |
10046 | i--; | |
10047 | } | |
10048 | ||
10049 | return 0; | |
10050 | } | |
10051 | ||
d6c2308c JW |
10052 | static int opt_subreg_zext_lo32_rnd_hi32(struct bpf_verifier_env *env, |
10053 | const union bpf_attr *attr) | |
a4b1d3c1 | 10054 | { |
d6c2308c | 10055 | struct bpf_insn *patch, zext_patch[2], rnd_hi32_patch[4]; |
a4b1d3c1 | 10056 | struct bpf_insn_aux_data *aux = env->insn_aux_data; |
d6c2308c | 10057 | int i, patch_len, delta = 0, len = env->prog->len; |
a4b1d3c1 | 10058 | struct bpf_insn *insns = env->prog->insnsi; |
a4b1d3c1 | 10059 | struct bpf_prog *new_prog; |
d6c2308c | 10060 | bool rnd_hi32; |
a4b1d3c1 | 10061 | |
d6c2308c | 10062 | rnd_hi32 = attr->prog_flags & BPF_F_TEST_RND_HI32; |
a4b1d3c1 | 10063 | zext_patch[1] = BPF_ZEXT_REG(0); |
d6c2308c JW |
10064 | rnd_hi32_patch[1] = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, 0); |
10065 | rnd_hi32_patch[2] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32); | |
10066 | rnd_hi32_patch[3] = BPF_ALU64_REG(BPF_OR, 0, BPF_REG_AX); | |
a4b1d3c1 JW |
10067 | for (i = 0; i < len; i++) { |
10068 | int adj_idx = i + delta; | |
10069 | struct bpf_insn insn; | |
10070 | ||
d6c2308c JW |
10071 | insn = insns[adj_idx]; |
10072 | if (!aux[adj_idx].zext_dst) { | |
10073 | u8 code, class; | |
10074 | u32 imm_rnd; | |
10075 | ||
10076 | if (!rnd_hi32) | |
10077 | continue; | |
10078 | ||
10079 | code = insn.code; | |
10080 | class = BPF_CLASS(code); | |
10081 | if (insn_no_def(&insn)) | |
10082 | continue; | |
10083 | ||
10084 | /* NOTE: arg "reg" (the fourth one) is only used for | |
10085 | * BPF_STX which has been ruled out in above | |
10086 | * check, it is safe to pass NULL here. | |
10087 | */ | |
10088 | if (is_reg64(env, &insn, insn.dst_reg, NULL, DST_OP)) { | |
10089 | if (class == BPF_LD && | |
10090 | BPF_MODE(code) == BPF_IMM) | |
10091 | i++; | |
10092 | continue; | |
10093 | } | |
10094 | ||
10095 | /* ctx load could be transformed into wider load. */ | |
10096 | if (class == BPF_LDX && | |
10097 | aux[adj_idx].ptr_type == PTR_TO_CTX) | |
10098 | continue; | |
10099 | ||
10100 | imm_rnd = get_random_int(); | |
10101 | rnd_hi32_patch[0] = insn; | |
10102 | rnd_hi32_patch[1].imm = imm_rnd; | |
10103 | rnd_hi32_patch[3].dst_reg = insn.dst_reg; | |
10104 | patch = rnd_hi32_patch; | |
10105 | patch_len = 4; | |
10106 | goto apply_patch_buffer; | |
10107 | } | |
10108 | ||
10109 | if (!bpf_jit_needs_zext()) | |
a4b1d3c1 JW |
10110 | continue; |
10111 | ||
a4b1d3c1 JW |
10112 | zext_patch[0] = insn; |
10113 | zext_patch[1].dst_reg = insn.dst_reg; | |
10114 | zext_patch[1].src_reg = insn.dst_reg; | |
d6c2308c JW |
10115 | patch = zext_patch; |
10116 | patch_len = 2; | |
10117 | apply_patch_buffer: | |
10118 | new_prog = bpf_patch_insn_data(env, adj_idx, patch, patch_len); | |
a4b1d3c1 JW |
10119 | if (!new_prog) |
10120 | return -ENOMEM; | |
10121 | env->prog = new_prog; | |
10122 | insns = new_prog->insnsi; | |
10123 | aux = env->insn_aux_data; | |
d6c2308c | 10124 | delta += patch_len - 1; |
a4b1d3c1 JW |
10125 | } |
10126 | ||
10127 | return 0; | |
10128 | } | |
10129 | ||
c64b7983 JS |
10130 | /* convert load instructions that access fields of a context type into a |
10131 | * sequence of instructions that access fields of the underlying structure: | |
10132 | * struct __sk_buff -> struct sk_buff | |
10133 | * struct bpf_sock_ops -> struct sock | |
9bac3d6d | 10134 | */ |
58e2af8b | 10135 | static int convert_ctx_accesses(struct bpf_verifier_env *env) |
9bac3d6d | 10136 | { |
00176a34 | 10137 | const struct bpf_verifier_ops *ops = env->ops; |
f96da094 | 10138 | int i, cnt, size, ctx_field_size, delta = 0; |
3df126f3 | 10139 | const int insn_cnt = env->prog->len; |
36bbef52 | 10140 | struct bpf_insn insn_buf[16], *insn; |
46f53a65 | 10141 | u32 target_size, size_default, off; |
9bac3d6d | 10142 | struct bpf_prog *new_prog; |
d691f9e8 | 10143 | enum bpf_access_type type; |
f96da094 | 10144 | bool is_narrower_load; |
9bac3d6d | 10145 | |
b09928b9 DB |
10146 | if (ops->gen_prologue || env->seen_direct_write) { |
10147 | if (!ops->gen_prologue) { | |
10148 | verbose(env, "bpf verifier is misconfigured\n"); | |
10149 | return -EINVAL; | |
10150 | } | |
36bbef52 DB |
10151 | cnt = ops->gen_prologue(insn_buf, env->seen_direct_write, |
10152 | env->prog); | |
10153 | if (cnt >= ARRAY_SIZE(insn_buf)) { | |
61bd5218 | 10154 | verbose(env, "bpf verifier is misconfigured\n"); |
36bbef52 DB |
10155 | return -EINVAL; |
10156 | } else if (cnt) { | |
8041902d | 10157 | new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt); |
36bbef52 DB |
10158 | if (!new_prog) |
10159 | return -ENOMEM; | |
8041902d | 10160 | |
36bbef52 | 10161 | env->prog = new_prog; |
3df126f3 | 10162 | delta += cnt - 1; |
36bbef52 DB |
10163 | } |
10164 | } | |
10165 | ||
c64b7983 | 10166 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
9bac3d6d AS |
10167 | return 0; |
10168 | ||
3df126f3 | 10169 | insn = env->prog->insnsi + delta; |
36bbef52 | 10170 | |
9bac3d6d | 10171 | for (i = 0; i < insn_cnt; i++, insn++) { |
c64b7983 JS |
10172 | bpf_convert_ctx_access_t convert_ctx_access; |
10173 | ||
62c7989b DB |
10174 | if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) || |
10175 | insn->code == (BPF_LDX | BPF_MEM | BPF_H) || | |
10176 | insn->code == (BPF_LDX | BPF_MEM | BPF_W) || | |
ea2e7ce5 | 10177 | insn->code == (BPF_LDX | BPF_MEM | BPF_DW)) |
d691f9e8 | 10178 | type = BPF_READ; |
62c7989b DB |
10179 | else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) || |
10180 | insn->code == (BPF_STX | BPF_MEM | BPF_H) || | |
10181 | insn->code == (BPF_STX | BPF_MEM | BPF_W) || | |
ea2e7ce5 | 10182 | insn->code == (BPF_STX | BPF_MEM | BPF_DW)) |
d691f9e8 AS |
10183 | type = BPF_WRITE; |
10184 | else | |
9bac3d6d AS |
10185 | continue; |
10186 | ||
af86ca4e AS |
10187 | if (type == BPF_WRITE && |
10188 | env->insn_aux_data[i + delta].sanitize_stack_off) { | |
10189 | struct bpf_insn patch[] = { | |
10190 | /* Sanitize suspicious stack slot with zero. | |
10191 | * There are no memory dependencies for this store, | |
10192 | * since it's only using frame pointer and immediate | |
10193 | * constant of zero | |
10194 | */ | |
10195 | BPF_ST_MEM(BPF_DW, BPF_REG_FP, | |
10196 | env->insn_aux_data[i + delta].sanitize_stack_off, | |
10197 | 0), | |
10198 | /* the original STX instruction will immediately | |
10199 | * overwrite the same stack slot with appropriate value | |
10200 | */ | |
10201 | *insn, | |
10202 | }; | |
10203 | ||
10204 | cnt = ARRAY_SIZE(patch); | |
10205 | new_prog = bpf_patch_insn_data(env, i + delta, patch, cnt); | |
10206 | if (!new_prog) | |
10207 | return -ENOMEM; | |
10208 | ||
10209 | delta += cnt - 1; | |
10210 | env->prog = new_prog; | |
10211 | insn = new_prog->insnsi + i + delta; | |
10212 | continue; | |
10213 | } | |
10214 | ||
c64b7983 JS |
10215 | switch (env->insn_aux_data[i + delta].ptr_type) { |
10216 | case PTR_TO_CTX: | |
10217 | if (!ops->convert_ctx_access) | |
10218 | continue; | |
10219 | convert_ctx_access = ops->convert_ctx_access; | |
10220 | break; | |
10221 | case PTR_TO_SOCKET: | |
46f8bc92 | 10222 | case PTR_TO_SOCK_COMMON: |
c64b7983 JS |
10223 | convert_ctx_access = bpf_sock_convert_ctx_access; |
10224 | break; | |
655a51e5 MKL |
10225 | case PTR_TO_TCP_SOCK: |
10226 | convert_ctx_access = bpf_tcp_sock_convert_ctx_access; | |
10227 | break; | |
fada7fdc JL |
10228 | case PTR_TO_XDP_SOCK: |
10229 | convert_ctx_access = bpf_xdp_sock_convert_ctx_access; | |
10230 | break; | |
2a02759e | 10231 | case PTR_TO_BTF_ID: |
27ae7997 MKL |
10232 | if (type == BPF_READ) { |
10233 | insn->code = BPF_LDX | BPF_PROBE_MEM | | |
10234 | BPF_SIZE((insn)->code); | |
10235 | env->prog->aux->num_exentries++; | |
7e40781c | 10236 | } else if (resolve_prog_type(env->prog) != BPF_PROG_TYPE_STRUCT_OPS) { |
2a02759e AS |
10237 | verbose(env, "Writes through BTF pointers are not allowed\n"); |
10238 | return -EINVAL; | |
10239 | } | |
2a02759e | 10240 | continue; |
c64b7983 | 10241 | default: |
9bac3d6d | 10242 | continue; |
c64b7983 | 10243 | } |
9bac3d6d | 10244 | |
31fd8581 | 10245 | ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size; |
f96da094 | 10246 | size = BPF_LDST_BYTES(insn); |
31fd8581 YS |
10247 | |
10248 | /* If the read access is a narrower load of the field, | |
10249 | * convert to a 4/8-byte load, to minimum program type specific | |
10250 | * convert_ctx_access changes. If conversion is successful, | |
10251 | * we will apply proper mask to the result. | |
10252 | */ | |
f96da094 | 10253 | is_narrower_load = size < ctx_field_size; |
46f53a65 AI |
10254 | size_default = bpf_ctx_off_adjust_machine(ctx_field_size); |
10255 | off = insn->off; | |
31fd8581 | 10256 | if (is_narrower_load) { |
f96da094 DB |
10257 | u8 size_code; |
10258 | ||
10259 | if (type == BPF_WRITE) { | |
61bd5218 | 10260 | verbose(env, "bpf verifier narrow ctx access misconfigured\n"); |
f96da094 DB |
10261 | return -EINVAL; |
10262 | } | |
31fd8581 | 10263 | |
f96da094 | 10264 | size_code = BPF_H; |
31fd8581 YS |
10265 | if (ctx_field_size == 4) |
10266 | size_code = BPF_W; | |
10267 | else if (ctx_field_size == 8) | |
10268 | size_code = BPF_DW; | |
f96da094 | 10269 | |
bc23105c | 10270 | insn->off = off & ~(size_default - 1); |
31fd8581 YS |
10271 | insn->code = BPF_LDX | BPF_MEM | size_code; |
10272 | } | |
f96da094 DB |
10273 | |
10274 | target_size = 0; | |
c64b7983 JS |
10275 | cnt = convert_ctx_access(type, insn, insn_buf, env->prog, |
10276 | &target_size); | |
f96da094 DB |
10277 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) || |
10278 | (ctx_field_size && !target_size)) { | |
61bd5218 | 10279 | verbose(env, "bpf verifier is misconfigured\n"); |
9bac3d6d AS |
10280 | return -EINVAL; |
10281 | } | |
f96da094 DB |
10282 | |
10283 | if (is_narrower_load && size < target_size) { | |
d895a0f1 IL |
10284 | u8 shift = bpf_ctx_narrow_access_offset( |
10285 | off, size, size_default) * 8; | |
46f53a65 AI |
10286 | if (ctx_field_size <= 4) { |
10287 | if (shift) | |
10288 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_RSH, | |
10289 | insn->dst_reg, | |
10290 | shift); | |
31fd8581 | 10291 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg, |
f96da094 | 10292 | (1 << size * 8) - 1); |
46f53a65 AI |
10293 | } else { |
10294 | if (shift) | |
10295 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_RSH, | |
10296 | insn->dst_reg, | |
10297 | shift); | |
31fd8581 | 10298 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg, |
e2f7fc0a | 10299 | (1ULL << size * 8) - 1); |
46f53a65 | 10300 | } |
31fd8581 | 10301 | } |
9bac3d6d | 10302 | |
8041902d | 10303 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
9bac3d6d AS |
10304 | if (!new_prog) |
10305 | return -ENOMEM; | |
10306 | ||
3df126f3 | 10307 | delta += cnt - 1; |
9bac3d6d AS |
10308 | |
10309 | /* keep walking new program and skip insns we just inserted */ | |
10310 | env->prog = new_prog; | |
3df126f3 | 10311 | insn = new_prog->insnsi + i + delta; |
9bac3d6d AS |
10312 | } |
10313 | ||
10314 | return 0; | |
10315 | } | |
10316 | ||
1c2a088a AS |
10317 | static int jit_subprogs(struct bpf_verifier_env *env) |
10318 | { | |
10319 | struct bpf_prog *prog = env->prog, **func, *tmp; | |
10320 | int i, j, subprog_start, subprog_end = 0, len, subprog; | |
a748c697 | 10321 | struct bpf_map *map_ptr; |
7105e828 | 10322 | struct bpf_insn *insn; |
1c2a088a | 10323 | void *old_bpf_func; |
c4c0bdc0 | 10324 | int err, num_exentries; |
1c2a088a | 10325 | |
f910cefa | 10326 | if (env->subprog_cnt <= 1) |
1c2a088a AS |
10327 | return 0; |
10328 | ||
7105e828 | 10329 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
1c2a088a AS |
10330 | if (insn->code != (BPF_JMP | BPF_CALL) || |
10331 | insn->src_reg != BPF_PSEUDO_CALL) | |
10332 | continue; | |
c7a89784 DB |
10333 | /* Upon error here we cannot fall back to interpreter but |
10334 | * need a hard reject of the program. Thus -EFAULT is | |
10335 | * propagated in any case. | |
10336 | */ | |
1c2a088a AS |
10337 | subprog = find_subprog(env, i + insn->imm + 1); |
10338 | if (subprog < 0) { | |
10339 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", | |
10340 | i + insn->imm + 1); | |
10341 | return -EFAULT; | |
10342 | } | |
10343 | /* temporarily remember subprog id inside insn instead of | |
10344 | * aux_data, since next loop will split up all insns into funcs | |
10345 | */ | |
f910cefa | 10346 | insn->off = subprog; |
1c2a088a AS |
10347 | /* remember original imm in case JIT fails and fallback |
10348 | * to interpreter will be needed | |
10349 | */ | |
10350 | env->insn_aux_data[i].call_imm = insn->imm; | |
10351 | /* point imm to __bpf_call_base+1 from JITs point of view */ | |
10352 | insn->imm = 1; | |
10353 | } | |
10354 | ||
c454a46b MKL |
10355 | err = bpf_prog_alloc_jited_linfo(prog); |
10356 | if (err) | |
10357 | goto out_undo_insn; | |
10358 | ||
10359 | err = -ENOMEM; | |
6396bb22 | 10360 | func = kcalloc(env->subprog_cnt, sizeof(prog), GFP_KERNEL); |
1c2a088a | 10361 | if (!func) |
c7a89784 | 10362 | goto out_undo_insn; |
1c2a088a | 10363 | |
f910cefa | 10364 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a | 10365 | subprog_start = subprog_end; |
4cb3d99c | 10366 | subprog_end = env->subprog_info[i + 1].start; |
1c2a088a AS |
10367 | |
10368 | len = subprog_end - subprog_start; | |
492ecee8 AS |
10369 | /* BPF_PROG_RUN doesn't call subprogs directly, |
10370 | * hence main prog stats include the runtime of subprogs. | |
10371 | * subprogs don't have IDs and not reachable via prog_get_next_id | |
10372 | * func[i]->aux->stats will never be accessed and stays NULL | |
10373 | */ | |
10374 | func[i] = bpf_prog_alloc_no_stats(bpf_prog_size(len), GFP_USER); | |
1c2a088a AS |
10375 | if (!func[i]) |
10376 | goto out_free; | |
10377 | memcpy(func[i]->insnsi, &prog->insnsi[subprog_start], | |
10378 | len * sizeof(struct bpf_insn)); | |
4f74d809 | 10379 | func[i]->type = prog->type; |
1c2a088a | 10380 | func[i]->len = len; |
4f74d809 DB |
10381 | if (bpf_prog_calc_tag(func[i])) |
10382 | goto out_free; | |
1c2a088a | 10383 | func[i]->is_func = 1; |
ba64e7d8 YS |
10384 | func[i]->aux->func_idx = i; |
10385 | /* the btf and func_info will be freed only at prog->aux */ | |
10386 | func[i]->aux->btf = prog->aux->btf; | |
10387 | func[i]->aux->func_info = prog->aux->func_info; | |
10388 | ||
a748c697 MF |
10389 | for (j = 0; j < prog->aux->size_poke_tab; j++) { |
10390 | u32 insn_idx = prog->aux->poke_tab[j].insn_idx; | |
10391 | int ret; | |
10392 | ||
10393 | if (!(insn_idx >= subprog_start && | |
10394 | insn_idx <= subprog_end)) | |
10395 | continue; | |
10396 | ||
10397 | ret = bpf_jit_add_poke_descriptor(func[i], | |
10398 | &prog->aux->poke_tab[j]); | |
10399 | if (ret < 0) { | |
10400 | verbose(env, "adding tail call poke descriptor failed\n"); | |
10401 | goto out_free; | |
10402 | } | |
10403 | ||
10404 | func[i]->insnsi[insn_idx - subprog_start].imm = ret + 1; | |
10405 | ||
10406 | map_ptr = func[i]->aux->poke_tab[ret].tail_call.map; | |
10407 | ret = map_ptr->ops->map_poke_track(map_ptr, func[i]->aux); | |
10408 | if (ret < 0) { | |
10409 | verbose(env, "tracking tail call prog failed\n"); | |
10410 | goto out_free; | |
10411 | } | |
10412 | } | |
10413 | ||
1c2a088a AS |
10414 | /* Use bpf_prog_F_tag to indicate functions in stack traces. |
10415 | * Long term would need debug info to populate names | |
10416 | */ | |
10417 | func[i]->aux->name[0] = 'F'; | |
9c8105bd | 10418 | func[i]->aux->stack_depth = env->subprog_info[i].stack_depth; |
1c2a088a | 10419 | func[i]->jit_requested = 1; |
c454a46b MKL |
10420 | func[i]->aux->linfo = prog->aux->linfo; |
10421 | func[i]->aux->nr_linfo = prog->aux->nr_linfo; | |
10422 | func[i]->aux->jited_linfo = prog->aux->jited_linfo; | |
10423 | func[i]->aux->linfo_idx = env->subprog_info[i].linfo_idx; | |
c4c0bdc0 YS |
10424 | num_exentries = 0; |
10425 | insn = func[i]->insnsi; | |
10426 | for (j = 0; j < func[i]->len; j++, insn++) { | |
10427 | if (BPF_CLASS(insn->code) == BPF_LDX && | |
10428 | BPF_MODE(insn->code) == BPF_PROBE_MEM) | |
10429 | num_exentries++; | |
10430 | } | |
10431 | func[i]->aux->num_exentries = num_exentries; | |
ebf7d1f5 | 10432 | func[i]->aux->tail_call_reachable = env->subprog_info[i].tail_call_reachable; |
1c2a088a AS |
10433 | func[i] = bpf_int_jit_compile(func[i]); |
10434 | if (!func[i]->jited) { | |
10435 | err = -ENOTSUPP; | |
10436 | goto out_free; | |
10437 | } | |
10438 | cond_resched(); | |
10439 | } | |
a748c697 MF |
10440 | |
10441 | /* Untrack main program's aux structs so that during map_poke_run() | |
10442 | * we will not stumble upon the unfilled poke descriptors; each | |
10443 | * of the main program's poke descs got distributed across subprogs | |
10444 | * and got tracked onto map, so we are sure that none of them will | |
10445 | * be missed after the operation below | |
10446 | */ | |
10447 | for (i = 0; i < prog->aux->size_poke_tab; i++) { | |
10448 | map_ptr = prog->aux->poke_tab[i].tail_call.map; | |
10449 | ||
10450 | map_ptr->ops->map_poke_untrack(map_ptr, prog->aux); | |
10451 | } | |
10452 | ||
1c2a088a AS |
10453 | /* at this point all bpf functions were successfully JITed |
10454 | * now populate all bpf_calls with correct addresses and | |
10455 | * run last pass of JIT | |
10456 | */ | |
f910cefa | 10457 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a AS |
10458 | insn = func[i]->insnsi; |
10459 | for (j = 0; j < func[i]->len; j++, insn++) { | |
10460 | if (insn->code != (BPF_JMP | BPF_CALL) || | |
10461 | insn->src_reg != BPF_PSEUDO_CALL) | |
10462 | continue; | |
10463 | subprog = insn->off; | |
0d306c31 PB |
10464 | insn->imm = BPF_CAST_CALL(func[subprog]->bpf_func) - |
10465 | __bpf_call_base; | |
1c2a088a | 10466 | } |
2162fed4 SD |
10467 | |
10468 | /* we use the aux data to keep a list of the start addresses | |
10469 | * of the JITed images for each function in the program | |
10470 | * | |
10471 | * for some architectures, such as powerpc64, the imm field | |
10472 | * might not be large enough to hold the offset of the start | |
10473 | * address of the callee's JITed image from __bpf_call_base | |
10474 | * | |
10475 | * in such cases, we can lookup the start address of a callee | |
10476 | * by using its subprog id, available from the off field of | |
10477 | * the call instruction, as an index for this list | |
10478 | */ | |
10479 | func[i]->aux->func = func; | |
10480 | func[i]->aux->func_cnt = env->subprog_cnt; | |
1c2a088a | 10481 | } |
f910cefa | 10482 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a AS |
10483 | old_bpf_func = func[i]->bpf_func; |
10484 | tmp = bpf_int_jit_compile(func[i]); | |
10485 | if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) { | |
10486 | verbose(env, "JIT doesn't support bpf-to-bpf calls\n"); | |
c7a89784 | 10487 | err = -ENOTSUPP; |
1c2a088a AS |
10488 | goto out_free; |
10489 | } | |
10490 | cond_resched(); | |
10491 | } | |
10492 | ||
10493 | /* finally lock prog and jit images for all functions and | |
10494 | * populate kallsysm | |
10495 | */ | |
f910cefa | 10496 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a AS |
10497 | bpf_prog_lock_ro(func[i]); |
10498 | bpf_prog_kallsyms_add(func[i]); | |
10499 | } | |
7105e828 DB |
10500 | |
10501 | /* Last step: make now unused interpreter insns from main | |
10502 | * prog consistent for later dump requests, so they can | |
10503 | * later look the same as if they were interpreted only. | |
10504 | */ | |
10505 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { | |
7105e828 DB |
10506 | if (insn->code != (BPF_JMP | BPF_CALL) || |
10507 | insn->src_reg != BPF_PSEUDO_CALL) | |
10508 | continue; | |
10509 | insn->off = env->insn_aux_data[i].call_imm; | |
10510 | subprog = find_subprog(env, i + insn->off + 1); | |
dbecd738 | 10511 | insn->imm = subprog; |
7105e828 DB |
10512 | } |
10513 | ||
1c2a088a AS |
10514 | prog->jited = 1; |
10515 | prog->bpf_func = func[0]->bpf_func; | |
10516 | prog->aux->func = func; | |
f910cefa | 10517 | prog->aux->func_cnt = env->subprog_cnt; |
c454a46b | 10518 | bpf_prog_free_unused_jited_linfo(prog); |
1c2a088a AS |
10519 | return 0; |
10520 | out_free: | |
a748c697 MF |
10521 | for (i = 0; i < env->subprog_cnt; i++) { |
10522 | if (!func[i]) | |
10523 | continue; | |
10524 | ||
10525 | for (j = 0; j < func[i]->aux->size_poke_tab; j++) { | |
10526 | map_ptr = func[i]->aux->poke_tab[j].tail_call.map; | |
10527 | map_ptr->ops->map_poke_untrack(map_ptr, func[i]->aux); | |
10528 | } | |
10529 | bpf_jit_free(func[i]); | |
10530 | } | |
1c2a088a | 10531 | kfree(func); |
c7a89784 | 10532 | out_undo_insn: |
1c2a088a AS |
10533 | /* cleanup main prog to be interpreted */ |
10534 | prog->jit_requested = 0; | |
10535 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { | |
10536 | if (insn->code != (BPF_JMP | BPF_CALL) || | |
10537 | insn->src_reg != BPF_PSEUDO_CALL) | |
10538 | continue; | |
10539 | insn->off = 0; | |
10540 | insn->imm = env->insn_aux_data[i].call_imm; | |
10541 | } | |
c454a46b | 10542 | bpf_prog_free_jited_linfo(prog); |
1c2a088a AS |
10543 | return err; |
10544 | } | |
10545 | ||
1ea47e01 AS |
10546 | static int fixup_call_args(struct bpf_verifier_env *env) |
10547 | { | |
19d28fbd | 10548 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
1ea47e01 AS |
10549 | struct bpf_prog *prog = env->prog; |
10550 | struct bpf_insn *insn = prog->insnsi; | |
10551 | int i, depth; | |
19d28fbd | 10552 | #endif |
e4052d06 | 10553 | int err = 0; |
1ea47e01 | 10554 | |
e4052d06 QM |
10555 | if (env->prog->jit_requested && |
10556 | !bpf_prog_is_dev_bound(env->prog->aux)) { | |
19d28fbd DM |
10557 | err = jit_subprogs(env); |
10558 | if (err == 0) | |
1c2a088a | 10559 | return 0; |
c7a89784 DB |
10560 | if (err == -EFAULT) |
10561 | return err; | |
19d28fbd DM |
10562 | } |
10563 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON | |
e411901c MF |
10564 | if (env->subprog_cnt > 1 && env->prog->aux->tail_call_reachable) { |
10565 | /* When JIT fails the progs with bpf2bpf calls and tail_calls | |
10566 | * have to be rejected, since interpreter doesn't support them yet. | |
10567 | */ | |
10568 | verbose(env, "tail_calls are not allowed in non-JITed programs with bpf-to-bpf calls\n"); | |
10569 | return -EINVAL; | |
10570 | } | |
1ea47e01 AS |
10571 | for (i = 0; i < prog->len; i++, insn++) { |
10572 | if (insn->code != (BPF_JMP | BPF_CALL) || | |
10573 | insn->src_reg != BPF_PSEUDO_CALL) | |
10574 | continue; | |
10575 | depth = get_callee_stack_depth(env, insn, i); | |
10576 | if (depth < 0) | |
10577 | return depth; | |
10578 | bpf_patch_call_args(insn, depth); | |
10579 | } | |
19d28fbd DM |
10580 | err = 0; |
10581 | #endif | |
10582 | return err; | |
1ea47e01 AS |
10583 | } |
10584 | ||
79741b3b | 10585 | /* fixup insn->imm field of bpf_call instructions |
81ed18ab | 10586 | * and inline eligible helpers as explicit sequence of BPF instructions |
e245c5c6 AS |
10587 | * |
10588 | * this function is called after eBPF program passed verification | |
10589 | */ | |
79741b3b | 10590 | static int fixup_bpf_calls(struct bpf_verifier_env *env) |
e245c5c6 | 10591 | { |
79741b3b | 10592 | struct bpf_prog *prog = env->prog; |
d2e4c1e6 | 10593 | bool expect_blinding = bpf_jit_blinding_enabled(prog); |
79741b3b | 10594 | struct bpf_insn *insn = prog->insnsi; |
e245c5c6 | 10595 | const struct bpf_func_proto *fn; |
79741b3b | 10596 | const int insn_cnt = prog->len; |
09772d92 | 10597 | const struct bpf_map_ops *ops; |
c93552c4 | 10598 | struct bpf_insn_aux_data *aux; |
81ed18ab AS |
10599 | struct bpf_insn insn_buf[16]; |
10600 | struct bpf_prog *new_prog; | |
10601 | struct bpf_map *map_ptr; | |
d2e4c1e6 | 10602 | int i, ret, cnt, delta = 0; |
e245c5c6 | 10603 | |
79741b3b | 10604 | for (i = 0; i < insn_cnt; i++, insn++) { |
f6b1b3bf DB |
10605 | if (insn->code == (BPF_ALU64 | BPF_MOD | BPF_X) || |
10606 | insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) || | |
10607 | insn->code == (BPF_ALU | BPF_MOD | BPF_X) || | |
68fda450 | 10608 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { |
f6b1b3bf DB |
10609 | bool is64 = BPF_CLASS(insn->code) == BPF_ALU64; |
10610 | struct bpf_insn mask_and_div[] = { | |
10611 | BPF_MOV32_REG(insn->src_reg, insn->src_reg), | |
10612 | /* Rx div 0 -> 0 */ | |
10613 | BPF_JMP_IMM(BPF_JNE, insn->src_reg, 0, 2), | |
10614 | BPF_ALU32_REG(BPF_XOR, insn->dst_reg, insn->dst_reg), | |
10615 | BPF_JMP_IMM(BPF_JA, 0, 0, 1), | |
10616 | *insn, | |
10617 | }; | |
10618 | struct bpf_insn mask_and_mod[] = { | |
10619 | BPF_MOV32_REG(insn->src_reg, insn->src_reg), | |
10620 | /* Rx mod 0 -> Rx */ | |
10621 | BPF_JMP_IMM(BPF_JEQ, insn->src_reg, 0, 1), | |
10622 | *insn, | |
10623 | }; | |
10624 | struct bpf_insn *patchlet; | |
10625 | ||
10626 | if (insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) || | |
10627 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { | |
10628 | patchlet = mask_and_div + (is64 ? 1 : 0); | |
10629 | cnt = ARRAY_SIZE(mask_and_div) - (is64 ? 1 : 0); | |
10630 | } else { | |
10631 | patchlet = mask_and_mod + (is64 ? 1 : 0); | |
10632 | cnt = ARRAY_SIZE(mask_and_mod) - (is64 ? 1 : 0); | |
10633 | } | |
10634 | ||
10635 | new_prog = bpf_patch_insn_data(env, i + delta, patchlet, cnt); | |
68fda450 AS |
10636 | if (!new_prog) |
10637 | return -ENOMEM; | |
10638 | ||
10639 | delta += cnt - 1; | |
10640 | env->prog = prog = new_prog; | |
10641 | insn = new_prog->insnsi + i + delta; | |
10642 | continue; | |
10643 | } | |
10644 | ||
e0cea7ce DB |
10645 | if (BPF_CLASS(insn->code) == BPF_LD && |
10646 | (BPF_MODE(insn->code) == BPF_ABS || | |
10647 | BPF_MODE(insn->code) == BPF_IND)) { | |
10648 | cnt = env->ops->gen_ld_abs(insn, insn_buf); | |
10649 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { | |
10650 | verbose(env, "bpf verifier is misconfigured\n"); | |
10651 | return -EINVAL; | |
10652 | } | |
10653 | ||
10654 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
10655 | if (!new_prog) | |
10656 | return -ENOMEM; | |
10657 | ||
10658 | delta += cnt - 1; | |
10659 | env->prog = prog = new_prog; | |
10660 | insn = new_prog->insnsi + i + delta; | |
10661 | continue; | |
10662 | } | |
10663 | ||
979d63d5 DB |
10664 | if (insn->code == (BPF_ALU64 | BPF_ADD | BPF_X) || |
10665 | insn->code == (BPF_ALU64 | BPF_SUB | BPF_X)) { | |
10666 | const u8 code_add = BPF_ALU64 | BPF_ADD | BPF_X; | |
10667 | const u8 code_sub = BPF_ALU64 | BPF_SUB | BPF_X; | |
10668 | struct bpf_insn insn_buf[16]; | |
10669 | struct bpf_insn *patch = &insn_buf[0]; | |
10670 | bool issrc, isneg; | |
10671 | u32 off_reg; | |
10672 | ||
10673 | aux = &env->insn_aux_data[i + delta]; | |
3612af78 DB |
10674 | if (!aux->alu_state || |
10675 | aux->alu_state == BPF_ALU_NON_POINTER) | |
979d63d5 DB |
10676 | continue; |
10677 | ||
10678 | isneg = aux->alu_state & BPF_ALU_NEG_VALUE; | |
10679 | issrc = (aux->alu_state & BPF_ALU_SANITIZE) == | |
10680 | BPF_ALU_SANITIZE_SRC; | |
10681 | ||
10682 | off_reg = issrc ? insn->src_reg : insn->dst_reg; | |
10683 | if (isneg) | |
10684 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); | |
10685 | *patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit - 1); | |
10686 | *patch++ = BPF_ALU64_REG(BPF_SUB, BPF_REG_AX, off_reg); | |
10687 | *patch++ = BPF_ALU64_REG(BPF_OR, BPF_REG_AX, off_reg); | |
10688 | *patch++ = BPF_ALU64_IMM(BPF_NEG, BPF_REG_AX, 0); | |
10689 | *patch++ = BPF_ALU64_IMM(BPF_ARSH, BPF_REG_AX, 63); | |
10690 | if (issrc) { | |
10691 | *patch++ = BPF_ALU64_REG(BPF_AND, BPF_REG_AX, | |
10692 | off_reg); | |
10693 | insn->src_reg = BPF_REG_AX; | |
10694 | } else { | |
10695 | *patch++ = BPF_ALU64_REG(BPF_AND, off_reg, | |
10696 | BPF_REG_AX); | |
10697 | } | |
10698 | if (isneg) | |
10699 | insn->code = insn->code == code_add ? | |
10700 | code_sub : code_add; | |
10701 | *patch++ = *insn; | |
10702 | if (issrc && isneg) | |
10703 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); | |
10704 | cnt = patch - insn_buf; | |
10705 | ||
10706 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
10707 | if (!new_prog) | |
10708 | return -ENOMEM; | |
10709 | ||
10710 | delta += cnt - 1; | |
10711 | env->prog = prog = new_prog; | |
10712 | insn = new_prog->insnsi + i + delta; | |
10713 | continue; | |
10714 | } | |
10715 | ||
79741b3b AS |
10716 | if (insn->code != (BPF_JMP | BPF_CALL)) |
10717 | continue; | |
cc8b0b92 AS |
10718 | if (insn->src_reg == BPF_PSEUDO_CALL) |
10719 | continue; | |
e245c5c6 | 10720 | |
79741b3b AS |
10721 | if (insn->imm == BPF_FUNC_get_route_realm) |
10722 | prog->dst_needed = 1; | |
10723 | if (insn->imm == BPF_FUNC_get_prandom_u32) | |
10724 | bpf_user_rnd_init_once(); | |
9802d865 JB |
10725 | if (insn->imm == BPF_FUNC_override_return) |
10726 | prog->kprobe_override = 1; | |
79741b3b | 10727 | if (insn->imm == BPF_FUNC_tail_call) { |
7b9f6da1 DM |
10728 | /* If we tail call into other programs, we |
10729 | * cannot make any assumptions since they can | |
10730 | * be replaced dynamically during runtime in | |
10731 | * the program array. | |
10732 | */ | |
10733 | prog->cb_access = 1; | |
e411901c MF |
10734 | if (!allow_tail_call_in_subprogs(env)) |
10735 | prog->aux->stack_depth = MAX_BPF_STACK; | |
10736 | prog->aux->max_pkt_offset = MAX_PACKET_OFF; | |
7b9f6da1 | 10737 | |
79741b3b AS |
10738 | /* mark bpf_tail_call as different opcode to avoid |
10739 | * conditional branch in the interpeter for every normal | |
10740 | * call and to prevent accidental JITing by JIT compiler | |
10741 | * that doesn't support bpf_tail_call yet | |
e245c5c6 | 10742 | */ |
79741b3b | 10743 | insn->imm = 0; |
71189fa9 | 10744 | insn->code = BPF_JMP | BPF_TAIL_CALL; |
b2157399 | 10745 | |
c93552c4 | 10746 | aux = &env->insn_aux_data[i + delta]; |
2c78ee89 | 10747 | if (env->bpf_capable && !expect_blinding && |
cc52d914 | 10748 | prog->jit_requested && |
d2e4c1e6 DB |
10749 | !bpf_map_key_poisoned(aux) && |
10750 | !bpf_map_ptr_poisoned(aux) && | |
10751 | !bpf_map_ptr_unpriv(aux)) { | |
10752 | struct bpf_jit_poke_descriptor desc = { | |
10753 | .reason = BPF_POKE_REASON_TAIL_CALL, | |
10754 | .tail_call.map = BPF_MAP_PTR(aux->map_ptr_state), | |
10755 | .tail_call.key = bpf_map_key_immediate(aux), | |
a748c697 | 10756 | .insn_idx = i + delta, |
d2e4c1e6 DB |
10757 | }; |
10758 | ||
10759 | ret = bpf_jit_add_poke_descriptor(prog, &desc); | |
10760 | if (ret < 0) { | |
10761 | verbose(env, "adding tail call poke descriptor failed\n"); | |
10762 | return ret; | |
10763 | } | |
10764 | ||
10765 | insn->imm = ret + 1; | |
10766 | continue; | |
10767 | } | |
10768 | ||
c93552c4 DB |
10769 | if (!bpf_map_ptr_unpriv(aux)) |
10770 | continue; | |
10771 | ||
b2157399 AS |
10772 | /* instead of changing every JIT dealing with tail_call |
10773 | * emit two extra insns: | |
10774 | * if (index >= max_entries) goto out; | |
10775 | * index &= array->index_mask; | |
10776 | * to avoid out-of-bounds cpu speculation | |
10777 | */ | |
c93552c4 | 10778 | if (bpf_map_ptr_poisoned(aux)) { |
40950343 | 10779 | verbose(env, "tail_call abusing map_ptr\n"); |
b2157399 AS |
10780 | return -EINVAL; |
10781 | } | |
c93552c4 | 10782 | |
d2e4c1e6 | 10783 | map_ptr = BPF_MAP_PTR(aux->map_ptr_state); |
b2157399 AS |
10784 | insn_buf[0] = BPF_JMP_IMM(BPF_JGE, BPF_REG_3, |
10785 | map_ptr->max_entries, 2); | |
10786 | insn_buf[1] = BPF_ALU32_IMM(BPF_AND, BPF_REG_3, | |
10787 | container_of(map_ptr, | |
10788 | struct bpf_array, | |
10789 | map)->index_mask); | |
10790 | insn_buf[2] = *insn; | |
10791 | cnt = 3; | |
10792 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
10793 | if (!new_prog) | |
10794 | return -ENOMEM; | |
10795 | ||
10796 | delta += cnt - 1; | |
10797 | env->prog = prog = new_prog; | |
10798 | insn = new_prog->insnsi + i + delta; | |
79741b3b AS |
10799 | continue; |
10800 | } | |
e245c5c6 | 10801 | |
89c63074 | 10802 | /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup |
09772d92 DB |
10803 | * and other inlining handlers are currently limited to 64 bit |
10804 | * only. | |
89c63074 | 10805 | */ |
60b58afc | 10806 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
09772d92 DB |
10807 | (insn->imm == BPF_FUNC_map_lookup_elem || |
10808 | insn->imm == BPF_FUNC_map_update_elem || | |
84430d42 DB |
10809 | insn->imm == BPF_FUNC_map_delete_elem || |
10810 | insn->imm == BPF_FUNC_map_push_elem || | |
10811 | insn->imm == BPF_FUNC_map_pop_elem || | |
10812 | insn->imm == BPF_FUNC_map_peek_elem)) { | |
c93552c4 DB |
10813 | aux = &env->insn_aux_data[i + delta]; |
10814 | if (bpf_map_ptr_poisoned(aux)) | |
10815 | goto patch_call_imm; | |
10816 | ||
d2e4c1e6 | 10817 | map_ptr = BPF_MAP_PTR(aux->map_ptr_state); |
09772d92 DB |
10818 | ops = map_ptr->ops; |
10819 | if (insn->imm == BPF_FUNC_map_lookup_elem && | |
10820 | ops->map_gen_lookup) { | |
10821 | cnt = ops->map_gen_lookup(map_ptr, insn_buf); | |
10822 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { | |
10823 | verbose(env, "bpf verifier is misconfigured\n"); | |
10824 | return -EINVAL; | |
10825 | } | |
81ed18ab | 10826 | |
09772d92 DB |
10827 | new_prog = bpf_patch_insn_data(env, i + delta, |
10828 | insn_buf, cnt); | |
10829 | if (!new_prog) | |
10830 | return -ENOMEM; | |
81ed18ab | 10831 | |
09772d92 DB |
10832 | delta += cnt - 1; |
10833 | env->prog = prog = new_prog; | |
10834 | insn = new_prog->insnsi + i + delta; | |
10835 | continue; | |
10836 | } | |
81ed18ab | 10837 | |
09772d92 DB |
10838 | BUILD_BUG_ON(!__same_type(ops->map_lookup_elem, |
10839 | (void *(*)(struct bpf_map *map, void *key))NULL)); | |
10840 | BUILD_BUG_ON(!__same_type(ops->map_delete_elem, | |
10841 | (int (*)(struct bpf_map *map, void *key))NULL)); | |
10842 | BUILD_BUG_ON(!__same_type(ops->map_update_elem, | |
10843 | (int (*)(struct bpf_map *map, void *key, void *value, | |
10844 | u64 flags))NULL)); | |
84430d42 DB |
10845 | BUILD_BUG_ON(!__same_type(ops->map_push_elem, |
10846 | (int (*)(struct bpf_map *map, void *value, | |
10847 | u64 flags))NULL)); | |
10848 | BUILD_BUG_ON(!__same_type(ops->map_pop_elem, | |
10849 | (int (*)(struct bpf_map *map, void *value))NULL)); | |
10850 | BUILD_BUG_ON(!__same_type(ops->map_peek_elem, | |
10851 | (int (*)(struct bpf_map *map, void *value))NULL)); | |
10852 | ||
09772d92 DB |
10853 | switch (insn->imm) { |
10854 | case BPF_FUNC_map_lookup_elem: | |
10855 | insn->imm = BPF_CAST_CALL(ops->map_lookup_elem) - | |
10856 | __bpf_call_base; | |
10857 | continue; | |
10858 | case BPF_FUNC_map_update_elem: | |
10859 | insn->imm = BPF_CAST_CALL(ops->map_update_elem) - | |
10860 | __bpf_call_base; | |
10861 | continue; | |
10862 | case BPF_FUNC_map_delete_elem: | |
10863 | insn->imm = BPF_CAST_CALL(ops->map_delete_elem) - | |
10864 | __bpf_call_base; | |
10865 | continue; | |
84430d42 DB |
10866 | case BPF_FUNC_map_push_elem: |
10867 | insn->imm = BPF_CAST_CALL(ops->map_push_elem) - | |
10868 | __bpf_call_base; | |
10869 | continue; | |
10870 | case BPF_FUNC_map_pop_elem: | |
10871 | insn->imm = BPF_CAST_CALL(ops->map_pop_elem) - | |
10872 | __bpf_call_base; | |
10873 | continue; | |
10874 | case BPF_FUNC_map_peek_elem: | |
10875 | insn->imm = BPF_CAST_CALL(ops->map_peek_elem) - | |
10876 | __bpf_call_base; | |
10877 | continue; | |
09772d92 | 10878 | } |
81ed18ab | 10879 | |
09772d92 | 10880 | goto patch_call_imm; |
81ed18ab AS |
10881 | } |
10882 | ||
5576b991 MKL |
10883 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
10884 | insn->imm == BPF_FUNC_jiffies64) { | |
10885 | struct bpf_insn ld_jiffies_addr[2] = { | |
10886 | BPF_LD_IMM64(BPF_REG_0, | |
10887 | (unsigned long)&jiffies), | |
10888 | }; | |
10889 | ||
10890 | insn_buf[0] = ld_jiffies_addr[0]; | |
10891 | insn_buf[1] = ld_jiffies_addr[1]; | |
10892 | insn_buf[2] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, | |
10893 | BPF_REG_0, 0); | |
10894 | cnt = 3; | |
10895 | ||
10896 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, | |
10897 | cnt); | |
10898 | if (!new_prog) | |
10899 | return -ENOMEM; | |
10900 | ||
10901 | delta += cnt - 1; | |
10902 | env->prog = prog = new_prog; | |
10903 | insn = new_prog->insnsi + i + delta; | |
10904 | continue; | |
10905 | } | |
10906 | ||
81ed18ab | 10907 | patch_call_imm: |
5e43f899 | 10908 | fn = env->ops->get_func_proto(insn->imm, env->prog); |
79741b3b AS |
10909 | /* all functions that have prototype and verifier allowed |
10910 | * programs to call them, must be real in-kernel functions | |
10911 | */ | |
10912 | if (!fn->func) { | |
61bd5218 JK |
10913 | verbose(env, |
10914 | "kernel subsystem misconfigured func %s#%d\n", | |
79741b3b AS |
10915 | func_id_name(insn->imm), insn->imm); |
10916 | return -EFAULT; | |
e245c5c6 | 10917 | } |
79741b3b | 10918 | insn->imm = fn->func - __bpf_call_base; |
e245c5c6 | 10919 | } |
e245c5c6 | 10920 | |
d2e4c1e6 DB |
10921 | /* Since poke tab is now finalized, publish aux to tracker. */ |
10922 | for (i = 0; i < prog->aux->size_poke_tab; i++) { | |
10923 | map_ptr = prog->aux->poke_tab[i].tail_call.map; | |
10924 | if (!map_ptr->ops->map_poke_track || | |
10925 | !map_ptr->ops->map_poke_untrack || | |
10926 | !map_ptr->ops->map_poke_run) { | |
10927 | verbose(env, "bpf verifier is misconfigured\n"); | |
10928 | return -EINVAL; | |
10929 | } | |
10930 | ||
10931 | ret = map_ptr->ops->map_poke_track(map_ptr, prog->aux); | |
10932 | if (ret < 0) { | |
10933 | verbose(env, "tracking tail call prog failed\n"); | |
10934 | return ret; | |
10935 | } | |
10936 | } | |
10937 | ||
79741b3b AS |
10938 | return 0; |
10939 | } | |
e245c5c6 | 10940 | |
58e2af8b | 10941 | static void free_states(struct bpf_verifier_env *env) |
f1bca824 | 10942 | { |
58e2af8b | 10943 | struct bpf_verifier_state_list *sl, *sln; |
f1bca824 AS |
10944 | int i; |
10945 | ||
9f4686c4 AS |
10946 | sl = env->free_list; |
10947 | while (sl) { | |
10948 | sln = sl->next; | |
10949 | free_verifier_state(&sl->state, false); | |
10950 | kfree(sl); | |
10951 | sl = sln; | |
10952 | } | |
51c39bb1 | 10953 | env->free_list = NULL; |
9f4686c4 | 10954 | |
f1bca824 AS |
10955 | if (!env->explored_states) |
10956 | return; | |
10957 | ||
dc2a4ebc | 10958 | for (i = 0; i < state_htab_size(env); i++) { |
f1bca824 AS |
10959 | sl = env->explored_states[i]; |
10960 | ||
a8f500af AS |
10961 | while (sl) { |
10962 | sln = sl->next; | |
10963 | free_verifier_state(&sl->state, false); | |
10964 | kfree(sl); | |
10965 | sl = sln; | |
10966 | } | |
51c39bb1 | 10967 | env->explored_states[i] = NULL; |
f1bca824 | 10968 | } |
51c39bb1 | 10969 | } |
f1bca824 | 10970 | |
51c39bb1 AS |
10971 | /* The verifier is using insn_aux_data[] to store temporary data during |
10972 | * verification and to store information for passes that run after the | |
10973 | * verification like dead code sanitization. do_check_common() for subprogram N | |
10974 | * may analyze many other subprograms. sanitize_insn_aux_data() clears all | |
10975 | * temporary data after do_check_common() finds that subprogram N cannot be | |
10976 | * verified independently. pass_cnt counts the number of times | |
10977 | * do_check_common() was run and insn->aux->seen tells the pass number | |
10978 | * insn_aux_data was touched. These variables are compared to clear temporary | |
10979 | * data from failed pass. For testing and experiments do_check_common() can be | |
10980 | * run multiple times even when prior attempt to verify is unsuccessful. | |
10981 | */ | |
10982 | static void sanitize_insn_aux_data(struct bpf_verifier_env *env) | |
10983 | { | |
10984 | struct bpf_insn *insn = env->prog->insnsi; | |
10985 | struct bpf_insn_aux_data *aux; | |
10986 | int i, class; | |
10987 | ||
10988 | for (i = 0; i < env->prog->len; i++) { | |
10989 | class = BPF_CLASS(insn[i].code); | |
10990 | if (class != BPF_LDX && class != BPF_STX) | |
10991 | continue; | |
10992 | aux = &env->insn_aux_data[i]; | |
10993 | if (aux->seen != env->pass_cnt) | |
10994 | continue; | |
10995 | memset(aux, 0, offsetof(typeof(*aux), orig_idx)); | |
10996 | } | |
f1bca824 AS |
10997 | } |
10998 | ||
51c39bb1 AS |
10999 | static int do_check_common(struct bpf_verifier_env *env, int subprog) |
11000 | { | |
6f8a57cc | 11001 | bool pop_log = !(env->log.level & BPF_LOG_LEVEL2); |
51c39bb1 AS |
11002 | struct bpf_verifier_state *state; |
11003 | struct bpf_reg_state *regs; | |
11004 | int ret, i; | |
11005 | ||
11006 | env->prev_linfo = NULL; | |
11007 | env->pass_cnt++; | |
11008 | ||
11009 | state = kzalloc(sizeof(struct bpf_verifier_state), GFP_KERNEL); | |
11010 | if (!state) | |
11011 | return -ENOMEM; | |
11012 | state->curframe = 0; | |
11013 | state->speculative = false; | |
11014 | state->branches = 1; | |
11015 | state->frame[0] = kzalloc(sizeof(struct bpf_func_state), GFP_KERNEL); | |
11016 | if (!state->frame[0]) { | |
11017 | kfree(state); | |
11018 | return -ENOMEM; | |
11019 | } | |
11020 | env->cur_state = state; | |
11021 | init_func_state(env, state->frame[0], | |
11022 | BPF_MAIN_FUNC /* callsite */, | |
11023 | 0 /* frameno */, | |
11024 | subprog); | |
11025 | ||
11026 | regs = state->frame[state->curframe]->regs; | |
be8704ff | 11027 | if (subprog || env->prog->type == BPF_PROG_TYPE_EXT) { |
51c39bb1 AS |
11028 | ret = btf_prepare_func_args(env, subprog, regs); |
11029 | if (ret) | |
11030 | goto out; | |
11031 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) { | |
11032 | if (regs[i].type == PTR_TO_CTX) | |
11033 | mark_reg_known_zero(env, regs, i); | |
11034 | else if (regs[i].type == SCALAR_VALUE) | |
11035 | mark_reg_unknown(env, regs, i); | |
11036 | } | |
11037 | } else { | |
11038 | /* 1st arg to a function */ | |
11039 | regs[BPF_REG_1].type = PTR_TO_CTX; | |
11040 | mark_reg_known_zero(env, regs, BPF_REG_1); | |
11041 | ret = btf_check_func_arg_match(env, subprog, regs); | |
11042 | if (ret == -EFAULT) | |
11043 | /* unlikely verifier bug. abort. | |
11044 | * ret == 0 and ret < 0 are sadly acceptable for | |
11045 | * main() function due to backward compatibility. | |
11046 | * Like socket filter program may be written as: | |
11047 | * int bpf_prog(struct pt_regs *ctx) | |
11048 | * and never dereference that ctx in the program. | |
11049 | * 'struct pt_regs' is a type mismatch for socket | |
11050 | * filter that should be using 'struct __sk_buff'. | |
11051 | */ | |
11052 | goto out; | |
11053 | } | |
11054 | ||
11055 | ret = do_check(env); | |
11056 | out: | |
f59bbfc2 AS |
11057 | /* check for NULL is necessary, since cur_state can be freed inside |
11058 | * do_check() under memory pressure. | |
11059 | */ | |
11060 | if (env->cur_state) { | |
11061 | free_verifier_state(env->cur_state, true); | |
11062 | env->cur_state = NULL; | |
11063 | } | |
6f8a57cc AN |
11064 | while (!pop_stack(env, NULL, NULL, false)); |
11065 | if (!ret && pop_log) | |
11066 | bpf_vlog_reset(&env->log, 0); | |
51c39bb1 AS |
11067 | free_states(env); |
11068 | if (ret) | |
11069 | /* clean aux data in case subprog was rejected */ | |
11070 | sanitize_insn_aux_data(env); | |
11071 | return ret; | |
11072 | } | |
11073 | ||
11074 | /* Verify all global functions in a BPF program one by one based on their BTF. | |
11075 | * All global functions must pass verification. Otherwise the whole program is rejected. | |
11076 | * Consider: | |
11077 | * int bar(int); | |
11078 | * int foo(int f) | |
11079 | * { | |
11080 | * return bar(f); | |
11081 | * } | |
11082 | * int bar(int b) | |
11083 | * { | |
11084 | * ... | |
11085 | * } | |
11086 | * foo() will be verified first for R1=any_scalar_value. During verification it | |
11087 | * will be assumed that bar() already verified successfully and call to bar() | |
11088 | * from foo() will be checked for type match only. Later bar() will be verified | |
11089 | * independently to check that it's safe for R1=any_scalar_value. | |
11090 | */ | |
11091 | static int do_check_subprogs(struct bpf_verifier_env *env) | |
11092 | { | |
11093 | struct bpf_prog_aux *aux = env->prog->aux; | |
11094 | int i, ret; | |
11095 | ||
11096 | if (!aux->func_info) | |
11097 | return 0; | |
11098 | ||
11099 | for (i = 1; i < env->subprog_cnt; i++) { | |
11100 | if (aux->func_info_aux[i].linkage != BTF_FUNC_GLOBAL) | |
11101 | continue; | |
11102 | env->insn_idx = env->subprog_info[i].start; | |
11103 | WARN_ON_ONCE(env->insn_idx == 0); | |
11104 | ret = do_check_common(env, i); | |
11105 | if (ret) { | |
11106 | return ret; | |
11107 | } else if (env->log.level & BPF_LOG_LEVEL) { | |
11108 | verbose(env, | |
11109 | "Func#%d is safe for any args that match its prototype\n", | |
11110 | i); | |
11111 | } | |
11112 | } | |
11113 | return 0; | |
11114 | } | |
11115 | ||
11116 | static int do_check_main(struct bpf_verifier_env *env) | |
11117 | { | |
11118 | int ret; | |
11119 | ||
11120 | env->insn_idx = 0; | |
11121 | ret = do_check_common(env, 0); | |
11122 | if (!ret) | |
11123 | env->prog->aux->stack_depth = env->subprog_info[0].stack_depth; | |
11124 | return ret; | |
11125 | } | |
11126 | ||
11127 | ||
06ee7115 AS |
11128 | static void print_verification_stats(struct bpf_verifier_env *env) |
11129 | { | |
11130 | int i; | |
11131 | ||
11132 | if (env->log.level & BPF_LOG_STATS) { | |
11133 | verbose(env, "verification time %lld usec\n", | |
11134 | div_u64(env->verification_time, 1000)); | |
11135 | verbose(env, "stack depth "); | |
11136 | for (i = 0; i < env->subprog_cnt; i++) { | |
11137 | u32 depth = env->subprog_info[i].stack_depth; | |
11138 | ||
11139 | verbose(env, "%d", depth); | |
11140 | if (i + 1 < env->subprog_cnt) | |
11141 | verbose(env, "+"); | |
11142 | } | |
11143 | verbose(env, "\n"); | |
11144 | } | |
11145 | verbose(env, "processed %d insns (limit %d) max_states_per_insn %d " | |
11146 | "total_states %d peak_states %d mark_read %d\n", | |
11147 | env->insn_processed, BPF_COMPLEXITY_LIMIT_INSNS, | |
11148 | env->max_states_per_insn, env->total_states, | |
11149 | env->peak_states, env->longest_mark_read_walk); | |
f1bca824 AS |
11150 | } |
11151 | ||
27ae7997 MKL |
11152 | static int check_struct_ops_btf_id(struct bpf_verifier_env *env) |
11153 | { | |
11154 | const struct btf_type *t, *func_proto; | |
11155 | const struct bpf_struct_ops *st_ops; | |
11156 | const struct btf_member *member; | |
11157 | struct bpf_prog *prog = env->prog; | |
11158 | u32 btf_id, member_idx; | |
11159 | const char *mname; | |
11160 | ||
11161 | btf_id = prog->aux->attach_btf_id; | |
11162 | st_ops = bpf_struct_ops_find(btf_id); | |
11163 | if (!st_ops) { | |
11164 | verbose(env, "attach_btf_id %u is not a supported struct\n", | |
11165 | btf_id); | |
11166 | return -ENOTSUPP; | |
11167 | } | |
11168 | ||
11169 | t = st_ops->type; | |
11170 | member_idx = prog->expected_attach_type; | |
11171 | if (member_idx >= btf_type_vlen(t)) { | |
11172 | verbose(env, "attach to invalid member idx %u of struct %s\n", | |
11173 | member_idx, st_ops->name); | |
11174 | return -EINVAL; | |
11175 | } | |
11176 | ||
11177 | member = &btf_type_member(t)[member_idx]; | |
11178 | mname = btf_name_by_offset(btf_vmlinux, member->name_off); | |
11179 | func_proto = btf_type_resolve_func_ptr(btf_vmlinux, member->type, | |
11180 | NULL); | |
11181 | if (!func_proto) { | |
11182 | verbose(env, "attach to invalid member %s(@idx %u) of struct %s\n", | |
11183 | mname, member_idx, st_ops->name); | |
11184 | return -EINVAL; | |
11185 | } | |
11186 | ||
11187 | if (st_ops->check_member) { | |
11188 | int err = st_ops->check_member(t, member); | |
11189 | ||
11190 | if (err) { | |
11191 | verbose(env, "attach to unsupported member %s of struct %s\n", | |
11192 | mname, st_ops->name); | |
11193 | return err; | |
11194 | } | |
11195 | } | |
11196 | ||
11197 | prog->aux->attach_func_proto = func_proto; | |
11198 | prog->aux->attach_func_name = mname; | |
11199 | env->ops = st_ops->verifier_ops; | |
11200 | ||
11201 | return 0; | |
11202 | } | |
6ba43b76 KS |
11203 | #define SECURITY_PREFIX "security_" |
11204 | ||
f7b12b6f | 11205 | static int check_attach_modify_return(unsigned long addr, const char *func_name) |
6ba43b76 | 11206 | { |
69191754 | 11207 | if (within_error_injection_list(addr) || |
f7b12b6f | 11208 | !strncmp(SECURITY_PREFIX, func_name, sizeof(SECURITY_PREFIX) - 1)) |
6ba43b76 | 11209 | return 0; |
6ba43b76 | 11210 | |
6ba43b76 KS |
11211 | return -EINVAL; |
11212 | } | |
27ae7997 | 11213 | |
1e6c62a8 AS |
11214 | /* non exhaustive list of sleepable bpf_lsm_*() functions */ |
11215 | BTF_SET_START(btf_sleepable_lsm_hooks) | |
11216 | #ifdef CONFIG_BPF_LSM | |
1e6c62a8 | 11217 | BTF_ID(func, bpf_lsm_bprm_committed_creds) |
29523c5e AS |
11218 | #else |
11219 | BTF_ID_UNUSED | |
1e6c62a8 AS |
11220 | #endif |
11221 | BTF_SET_END(btf_sleepable_lsm_hooks) | |
11222 | ||
11223 | static int check_sleepable_lsm_hook(u32 btf_id) | |
11224 | { | |
11225 | return btf_id_set_contains(&btf_sleepable_lsm_hooks, btf_id); | |
11226 | } | |
11227 | ||
11228 | /* list of non-sleepable functions that are otherwise on | |
11229 | * ALLOW_ERROR_INJECTION list | |
11230 | */ | |
11231 | BTF_SET_START(btf_non_sleepable_error_inject) | |
11232 | /* Three functions below can be called from sleepable and non-sleepable context. | |
11233 | * Assume non-sleepable from bpf safety point of view. | |
11234 | */ | |
11235 | BTF_ID(func, __add_to_page_cache_locked) | |
11236 | BTF_ID(func, should_fail_alloc_page) | |
11237 | BTF_ID(func, should_failslab) | |
11238 | BTF_SET_END(btf_non_sleepable_error_inject) | |
11239 | ||
11240 | static int check_non_sleepable_error_inject(u32 btf_id) | |
11241 | { | |
11242 | return btf_id_set_contains(&btf_non_sleepable_error_inject, btf_id); | |
11243 | } | |
11244 | ||
f7b12b6f THJ |
11245 | int bpf_check_attach_target(struct bpf_verifier_log *log, |
11246 | const struct bpf_prog *prog, | |
11247 | const struct bpf_prog *tgt_prog, | |
11248 | u32 btf_id, | |
11249 | struct bpf_attach_target_info *tgt_info) | |
38207291 | 11250 | { |
be8704ff | 11251 | bool prog_extension = prog->type == BPF_PROG_TYPE_EXT; |
f1b9509c | 11252 | const char prefix[] = "btf_trace_"; |
5b92a28a | 11253 | int ret = 0, subprog = -1, i; |
38207291 | 11254 | const struct btf_type *t; |
5b92a28a | 11255 | bool conservative = true; |
38207291 | 11256 | const char *tname; |
5b92a28a | 11257 | struct btf *btf; |
f7b12b6f | 11258 | long addr = 0; |
38207291 | 11259 | |
f1b9509c | 11260 | if (!btf_id) { |
efc68158 | 11261 | bpf_log(log, "Tracing programs must provide btf_id\n"); |
f1b9509c AS |
11262 | return -EINVAL; |
11263 | } | |
f7b12b6f | 11264 | btf = tgt_prog ? tgt_prog->aux->btf : btf_vmlinux; |
5b92a28a | 11265 | if (!btf) { |
efc68158 | 11266 | bpf_log(log, |
5b92a28a AS |
11267 | "FENTRY/FEXIT program can only be attached to another program annotated with BTF\n"); |
11268 | return -EINVAL; | |
11269 | } | |
11270 | t = btf_type_by_id(btf, btf_id); | |
f1b9509c | 11271 | if (!t) { |
efc68158 | 11272 | bpf_log(log, "attach_btf_id %u is invalid\n", btf_id); |
f1b9509c AS |
11273 | return -EINVAL; |
11274 | } | |
5b92a28a | 11275 | tname = btf_name_by_offset(btf, t->name_off); |
f1b9509c | 11276 | if (!tname) { |
efc68158 | 11277 | bpf_log(log, "attach_btf_id %u doesn't have a name\n", btf_id); |
f1b9509c AS |
11278 | return -EINVAL; |
11279 | } | |
5b92a28a AS |
11280 | if (tgt_prog) { |
11281 | struct bpf_prog_aux *aux = tgt_prog->aux; | |
11282 | ||
11283 | for (i = 0; i < aux->func_info_cnt; i++) | |
11284 | if (aux->func_info[i].type_id == btf_id) { | |
11285 | subprog = i; | |
11286 | break; | |
11287 | } | |
11288 | if (subprog == -1) { | |
efc68158 | 11289 | bpf_log(log, "Subprog %s doesn't exist\n", tname); |
5b92a28a AS |
11290 | return -EINVAL; |
11291 | } | |
11292 | conservative = aux->func_info_aux[subprog].unreliable; | |
be8704ff AS |
11293 | if (prog_extension) { |
11294 | if (conservative) { | |
efc68158 | 11295 | bpf_log(log, |
be8704ff AS |
11296 | "Cannot replace static functions\n"); |
11297 | return -EINVAL; | |
11298 | } | |
11299 | if (!prog->jit_requested) { | |
efc68158 | 11300 | bpf_log(log, |
be8704ff AS |
11301 | "Extension programs should be JITed\n"); |
11302 | return -EINVAL; | |
11303 | } | |
be8704ff AS |
11304 | } |
11305 | if (!tgt_prog->jited) { | |
efc68158 | 11306 | bpf_log(log, "Can attach to only JITed progs\n"); |
be8704ff AS |
11307 | return -EINVAL; |
11308 | } | |
11309 | if (tgt_prog->type == prog->type) { | |
11310 | /* Cannot fentry/fexit another fentry/fexit program. | |
11311 | * Cannot attach program extension to another extension. | |
11312 | * It's ok to attach fentry/fexit to extension program. | |
11313 | */ | |
efc68158 | 11314 | bpf_log(log, "Cannot recursively attach\n"); |
be8704ff AS |
11315 | return -EINVAL; |
11316 | } | |
11317 | if (tgt_prog->type == BPF_PROG_TYPE_TRACING && | |
11318 | prog_extension && | |
11319 | (tgt_prog->expected_attach_type == BPF_TRACE_FENTRY || | |
11320 | tgt_prog->expected_attach_type == BPF_TRACE_FEXIT)) { | |
11321 | /* Program extensions can extend all program types | |
11322 | * except fentry/fexit. The reason is the following. | |
11323 | * The fentry/fexit programs are used for performance | |
11324 | * analysis, stats and can be attached to any program | |
11325 | * type except themselves. When extension program is | |
11326 | * replacing XDP function it is necessary to allow | |
11327 | * performance analysis of all functions. Both original | |
11328 | * XDP program and its program extension. Hence | |
11329 | * attaching fentry/fexit to BPF_PROG_TYPE_EXT is | |
11330 | * allowed. If extending of fentry/fexit was allowed it | |
11331 | * would be possible to create long call chain | |
11332 | * fentry->extension->fentry->extension beyond | |
11333 | * reasonable stack size. Hence extending fentry is not | |
11334 | * allowed. | |
11335 | */ | |
efc68158 | 11336 | bpf_log(log, "Cannot extend fentry/fexit\n"); |
be8704ff AS |
11337 | return -EINVAL; |
11338 | } | |
5b92a28a | 11339 | } else { |
be8704ff | 11340 | if (prog_extension) { |
efc68158 | 11341 | bpf_log(log, "Cannot replace kernel functions\n"); |
be8704ff AS |
11342 | return -EINVAL; |
11343 | } | |
5b92a28a | 11344 | } |
f1b9509c AS |
11345 | |
11346 | switch (prog->expected_attach_type) { | |
11347 | case BPF_TRACE_RAW_TP: | |
5b92a28a | 11348 | if (tgt_prog) { |
efc68158 | 11349 | bpf_log(log, |
5b92a28a AS |
11350 | "Only FENTRY/FEXIT progs are attachable to another BPF prog\n"); |
11351 | return -EINVAL; | |
11352 | } | |
38207291 | 11353 | if (!btf_type_is_typedef(t)) { |
efc68158 | 11354 | bpf_log(log, "attach_btf_id %u is not a typedef\n", |
38207291 MKL |
11355 | btf_id); |
11356 | return -EINVAL; | |
11357 | } | |
f1b9509c | 11358 | if (strncmp(prefix, tname, sizeof(prefix) - 1)) { |
efc68158 | 11359 | bpf_log(log, "attach_btf_id %u points to wrong type name %s\n", |
38207291 MKL |
11360 | btf_id, tname); |
11361 | return -EINVAL; | |
11362 | } | |
11363 | tname += sizeof(prefix) - 1; | |
5b92a28a | 11364 | t = btf_type_by_id(btf, t->type); |
38207291 MKL |
11365 | if (!btf_type_is_ptr(t)) |
11366 | /* should never happen in valid vmlinux build */ | |
11367 | return -EINVAL; | |
5b92a28a | 11368 | t = btf_type_by_id(btf, t->type); |
38207291 MKL |
11369 | if (!btf_type_is_func_proto(t)) |
11370 | /* should never happen in valid vmlinux build */ | |
11371 | return -EINVAL; | |
11372 | ||
f7b12b6f | 11373 | break; |
15d83c4d YS |
11374 | case BPF_TRACE_ITER: |
11375 | if (!btf_type_is_func(t)) { | |
efc68158 | 11376 | bpf_log(log, "attach_btf_id %u is not a function\n", |
15d83c4d YS |
11377 | btf_id); |
11378 | return -EINVAL; | |
11379 | } | |
11380 | t = btf_type_by_id(btf, t->type); | |
11381 | if (!btf_type_is_func_proto(t)) | |
11382 | return -EINVAL; | |
f7b12b6f THJ |
11383 | ret = btf_distill_func_proto(log, btf, t, tname, &tgt_info->fmodel); |
11384 | if (ret) | |
11385 | return ret; | |
11386 | break; | |
be8704ff AS |
11387 | default: |
11388 | if (!prog_extension) | |
11389 | return -EINVAL; | |
df561f66 | 11390 | fallthrough; |
ae240823 | 11391 | case BPF_MODIFY_RETURN: |
9e4e01df | 11392 | case BPF_LSM_MAC: |
fec56f58 AS |
11393 | case BPF_TRACE_FENTRY: |
11394 | case BPF_TRACE_FEXIT: | |
11395 | if (!btf_type_is_func(t)) { | |
efc68158 | 11396 | bpf_log(log, "attach_btf_id %u is not a function\n", |
fec56f58 AS |
11397 | btf_id); |
11398 | return -EINVAL; | |
11399 | } | |
be8704ff | 11400 | if (prog_extension && |
efc68158 | 11401 | btf_check_type_match(log, prog, btf, t)) |
be8704ff | 11402 | return -EINVAL; |
5b92a28a | 11403 | t = btf_type_by_id(btf, t->type); |
fec56f58 AS |
11404 | if (!btf_type_is_func_proto(t)) |
11405 | return -EINVAL; | |
f7b12b6f | 11406 | |
4a1e7c0c THJ |
11407 | if ((prog->aux->saved_dst_prog_type || prog->aux->saved_dst_attach_type) && |
11408 | (!tgt_prog || prog->aux->saved_dst_prog_type != tgt_prog->type || | |
11409 | prog->aux->saved_dst_attach_type != tgt_prog->expected_attach_type)) | |
11410 | return -EINVAL; | |
11411 | ||
f7b12b6f | 11412 | if (tgt_prog && conservative) |
5b92a28a | 11413 | t = NULL; |
f7b12b6f THJ |
11414 | |
11415 | ret = btf_distill_func_proto(log, btf, t, tname, &tgt_info->fmodel); | |
fec56f58 | 11416 | if (ret < 0) |
f7b12b6f THJ |
11417 | return ret; |
11418 | ||
5b92a28a | 11419 | if (tgt_prog) { |
e9eeec58 YS |
11420 | if (subprog == 0) |
11421 | addr = (long) tgt_prog->bpf_func; | |
11422 | else | |
11423 | addr = (long) tgt_prog->aux->func[subprog]->bpf_func; | |
5b92a28a AS |
11424 | } else { |
11425 | addr = kallsyms_lookup_name(tname); | |
11426 | if (!addr) { | |
efc68158 | 11427 | bpf_log(log, |
5b92a28a AS |
11428 | "The address of function %s cannot be found\n", |
11429 | tname); | |
f7b12b6f | 11430 | return -ENOENT; |
5b92a28a | 11431 | } |
fec56f58 | 11432 | } |
18644cec | 11433 | |
1e6c62a8 AS |
11434 | if (prog->aux->sleepable) { |
11435 | ret = -EINVAL; | |
11436 | switch (prog->type) { | |
11437 | case BPF_PROG_TYPE_TRACING: | |
11438 | /* fentry/fexit/fmod_ret progs can be sleepable only if they are | |
11439 | * attached to ALLOW_ERROR_INJECTION and are not in denylist. | |
11440 | */ | |
11441 | if (!check_non_sleepable_error_inject(btf_id) && | |
11442 | within_error_injection_list(addr)) | |
11443 | ret = 0; | |
11444 | break; | |
11445 | case BPF_PROG_TYPE_LSM: | |
11446 | /* LSM progs check that they are attached to bpf_lsm_*() funcs. | |
11447 | * Only some of them are sleepable. | |
11448 | */ | |
11449 | if (check_sleepable_lsm_hook(btf_id)) | |
11450 | ret = 0; | |
11451 | break; | |
11452 | default: | |
11453 | break; | |
11454 | } | |
f7b12b6f THJ |
11455 | if (ret) { |
11456 | bpf_log(log, "%s is not sleepable\n", tname); | |
11457 | return ret; | |
11458 | } | |
1e6c62a8 | 11459 | } else if (prog->expected_attach_type == BPF_MODIFY_RETURN) { |
1af9270e | 11460 | if (tgt_prog) { |
efc68158 | 11461 | bpf_log(log, "can't modify return codes of BPF programs\n"); |
f7b12b6f THJ |
11462 | return -EINVAL; |
11463 | } | |
11464 | ret = check_attach_modify_return(addr, tname); | |
11465 | if (ret) { | |
11466 | bpf_log(log, "%s() is not modifiable\n", tname); | |
11467 | return ret; | |
1af9270e | 11468 | } |
18644cec | 11469 | } |
f7b12b6f THJ |
11470 | |
11471 | break; | |
11472 | } | |
11473 | tgt_info->tgt_addr = addr; | |
11474 | tgt_info->tgt_name = tname; | |
11475 | tgt_info->tgt_type = t; | |
11476 | return 0; | |
11477 | } | |
11478 | ||
11479 | static int check_attach_btf_id(struct bpf_verifier_env *env) | |
11480 | { | |
11481 | struct bpf_prog *prog = env->prog; | |
3aac1ead | 11482 | struct bpf_prog *tgt_prog = prog->aux->dst_prog; |
f7b12b6f THJ |
11483 | struct bpf_attach_target_info tgt_info = {}; |
11484 | u32 btf_id = prog->aux->attach_btf_id; | |
11485 | struct bpf_trampoline *tr; | |
11486 | int ret; | |
11487 | u64 key; | |
11488 | ||
11489 | if (prog->aux->sleepable && prog->type != BPF_PROG_TYPE_TRACING && | |
11490 | prog->type != BPF_PROG_TYPE_LSM) { | |
11491 | verbose(env, "Only fentry/fexit/fmod_ret and lsm programs can be sleepable\n"); | |
11492 | return -EINVAL; | |
11493 | } | |
11494 | ||
11495 | if (prog->type == BPF_PROG_TYPE_STRUCT_OPS) | |
11496 | return check_struct_ops_btf_id(env); | |
11497 | ||
11498 | if (prog->type != BPF_PROG_TYPE_TRACING && | |
11499 | prog->type != BPF_PROG_TYPE_LSM && | |
11500 | prog->type != BPF_PROG_TYPE_EXT) | |
11501 | return 0; | |
11502 | ||
11503 | ret = bpf_check_attach_target(&env->log, prog, tgt_prog, btf_id, &tgt_info); | |
11504 | if (ret) | |
fec56f58 | 11505 | return ret; |
f7b12b6f THJ |
11506 | |
11507 | if (tgt_prog && prog->type == BPF_PROG_TYPE_EXT) { | |
3aac1ead THJ |
11508 | /* to make freplace equivalent to their targets, they need to |
11509 | * inherit env->ops and expected_attach_type for the rest of the | |
11510 | * verification | |
11511 | */ | |
f7b12b6f THJ |
11512 | env->ops = bpf_verifier_ops[tgt_prog->type]; |
11513 | prog->expected_attach_type = tgt_prog->expected_attach_type; | |
11514 | } | |
11515 | ||
11516 | /* store info about the attachment target that will be used later */ | |
11517 | prog->aux->attach_func_proto = tgt_info.tgt_type; | |
11518 | prog->aux->attach_func_name = tgt_info.tgt_name; | |
11519 | ||
4a1e7c0c THJ |
11520 | if (tgt_prog) { |
11521 | prog->aux->saved_dst_prog_type = tgt_prog->type; | |
11522 | prog->aux->saved_dst_attach_type = tgt_prog->expected_attach_type; | |
11523 | } | |
11524 | ||
f7b12b6f THJ |
11525 | if (prog->expected_attach_type == BPF_TRACE_RAW_TP) { |
11526 | prog->aux->attach_btf_trace = true; | |
11527 | return 0; | |
11528 | } else if (prog->expected_attach_type == BPF_TRACE_ITER) { | |
11529 | if (!bpf_iter_prog_supported(prog)) | |
11530 | return -EINVAL; | |
11531 | return 0; | |
11532 | } | |
11533 | ||
11534 | if (prog->type == BPF_PROG_TYPE_LSM) { | |
11535 | ret = bpf_lsm_verify_prog(&env->log, prog); | |
11536 | if (ret < 0) | |
11537 | return ret; | |
38207291 | 11538 | } |
f7b12b6f THJ |
11539 | |
11540 | key = bpf_trampoline_compute_key(tgt_prog, btf_id); | |
11541 | tr = bpf_trampoline_get(key, &tgt_info); | |
11542 | if (!tr) | |
11543 | return -ENOMEM; | |
11544 | ||
3aac1ead | 11545 | prog->aux->dst_trampoline = tr; |
f7b12b6f | 11546 | return 0; |
38207291 MKL |
11547 | } |
11548 | ||
76654e67 AM |
11549 | struct btf *bpf_get_btf_vmlinux(void) |
11550 | { | |
11551 | if (!btf_vmlinux && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) { | |
11552 | mutex_lock(&bpf_verifier_lock); | |
11553 | if (!btf_vmlinux) | |
11554 | btf_vmlinux = btf_parse_vmlinux(); | |
11555 | mutex_unlock(&bpf_verifier_lock); | |
11556 | } | |
11557 | return btf_vmlinux; | |
11558 | } | |
11559 | ||
838e9690 YS |
11560 | int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, |
11561 | union bpf_attr __user *uattr) | |
51580e79 | 11562 | { |
06ee7115 | 11563 | u64 start_time = ktime_get_ns(); |
58e2af8b | 11564 | struct bpf_verifier_env *env; |
b9193c1b | 11565 | struct bpf_verifier_log *log; |
9e4c24e7 | 11566 | int i, len, ret = -EINVAL; |
e2ae4ca2 | 11567 | bool is_priv; |
51580e79 | 11568 | |
eba0c929 AB |
11569 | /* no program is valid */ |
11570 | if (ARRAY_SIZE(bpf_verifier_ops) == 0) | |
11571 | return -EINVAL; | |
11572 | ||
58e2af8b | 11573 | /* 'struct bpf_verifier_env' can be global, but since it's not small, |
cbd35700 AS |
11574 | * allocate/free it every time bpf_check() is called |
11575 | */ | |
58e2af8b | 11576 | env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL); |
cbd35700 AS |
11577 | if (!env) |
11578 | return -ENOMEM; | |
61bd5218 | 11579 | log = &env->log; |
cbd35700 | 11580 | |
9e4c24e7 | 11581 | len = (*prog)->len; |
fad953ce | 11582 | env->insn_aux_data = |
9e4c24e7 | 11583 | vzalloc(array_size(sizeof(struct bpf_insn_aux_data), len)); |
3df126f3 JK |
11584 | ret = -ENOMEM; |
11585 | if (!env->insn_aux_data) | |
11586 | goto err_free_env; | |
9e4c24e7 JK |
11587 | for (i = 0; i < len; i++) |
11588 | env->insn_aux_data[i].orig_idx = i; | |
9bac3d6d | 11589 | env->prog = *prog; |
00176a34 | 11590 | env->ops = bpf_verifier_ops[env->prog->type]; |
2c78ee89 | 11591 | is_priv = bpf_capable(); |
0246e64d | 11592 | |
76654e67 | 11593 | bpf_get_btf_vmlinux(); |
8580ac94 | 11594 | |
cbd35700 | 11595 | /* grab the mutex to protect few globals used by verifier */ |
45a73c17 AS |
11596 | if (!is_priv) |
11597 | mutex_lock(&bpf_verifier_lock); | |
cbd35700 AS |
11598 | |
11599 | if (attr->log_level || attr->log_buf || attr->log_size) { | |
11600 | /* user requested verbose verifier output | |
11601 | * and supplied buffer to store the verification trace | |
11602 | */ | |
e7bf8249 JK |
11603 | log->level = attr->log_level; |
11604 | log->ubuf = (char __user *) (unsigned long) attr->log_buf; | |
11605 | log->len_total = attr->log_size; | |
cbd35700 AS |
11606 | |
11607 | ret = -EINVAL; | |
e7bf8249 | 11608 | /* log attributes have to be sane */ |
7a9f5c65 | 11609 | if (log->len_total < 128 || log->len_total > UINT_MAX >> 2 || |
06ee7115 | 11610 | !log->level || !log->ubuf || log->level & ~BPF_LOG_MASK) |
3df126f3 | 11611 | goto err_unlock; |
cbd35700 | 11612 | } |
1ad2f583 | 11613 | |
8580ac94 AS |
11614 | if (IS_ERR(btf_vmlinux)) { |
11615 | /* Either gcc or pahole or kernel are broken. */ | |
11616 | verbose(env, "in-kernel BTF is malformed\n"); | |
11617 | ret = PTR_ERR(btf_vmlinux); | |
38207291 | 11618 | goto skip_full_check; |
8580ac94 AS |
11619 | } |
11620 | ||
1ad2f583 DB |
11621 | env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT); |
11622 | if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) | |
e07b98d9 | 11623 | env->strict_alignment = true; |
e9ee9efc DM |
11624 | if (attr->prog_flags & BPF_F_ANY_ALIGNMENT) |
11625 | env->strict_alignment = false; | |
cbd35700 | 11626 | |
2c78ee89 | 11627 | env->allow_ptr_leaks = bpf_allow_ptr_leaks(); |
41c48f3a | 11628 | env->allow_ptr_to_map_access = bpf_allow_ptr_to_map_access(); |
2c78ee89 AS |
11629 | env->bypass_spec_v1 = bpf_bypass_spec_v1(); |
11630 | env->bypass_spec_v4 = bpf_bypass_spec_v4(); | |
11631 | env->bpf_capable = bpf_capable(); | |
e2ae4ca2 | 11632 | |
10d274e8 AS |
11633 | if (is_priv) |
11634 | env->test_state_freq = attr->prog_flags & BPF_F_TEST_STATE_FREQ; | |
11635 | ||
f4e3ec0d JK |
11636 | ret = replace_map_fd_with_map_ptr(env); |
11637 | if (ret < 0) | |
11638 | goto skip_full_check; | |
11639 | ||
cae1927c | 11640 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
a40a2632 | 11641 | ret = bpf_prog_offload_verifier_prep(env->prog); |
ab3f0063 | 11642 | if (ret) |
f4e3ec0d | 11643 | goto skip_full_check; |
ab3f0063 JK |
11644 | } |
11645 | ||
dc2a4ebc | 11646 | env->explored_states = kvcalloc(state_htab_size(env), |
58e2af8b | 11647 | sizeof(struct bpf_verifier_state_list *), |
f1bca824 AS |
11648 | GFP_USER); |
11649 | ret = -ENOMEM; | |
11650 | if (!env->explored_states) | |
11651 | goto skip_full_check; | |
11652 | ||
d9762e84 | 11653 | ret = check_subprogs(env); |
475fb78f AS |
11654 | if (ret < 0) |
11655 | goto skip_full_check; | |
11656 | ||
c454a46b | 11657 | ret = check_btf_info(env, attr, uattr); |
838e9690 YS |
11658 | if (ret < 0) |
11659 | goto skip_full_check; | |
11660 | ||
be8704ff AS |
11661 | ret = check_attach_btf_id(env); |
11662 | if (ret) | |
11663 | goto skip_full_check; | |
11664 | ||
d9762e84 MKL |
11665 | ret = check_cfg(env); |
11666 | if (ret < 0) | |
11667 | goto skip_full_check; | |
11668 | ||
51c39bb1 AS |
11669 | ret = do_check_subprogs(env); |
11670 | ret = ret ?: do_check_main(env); | |
cbd35700 | 11671 | |
c941ce9c QM |
11672 | if (ret == 0 && bpf_prog_is_dev_bound(env->prog->aux)) |
11673 | ret = bpf_prog_offload_finalize(env); | |
11674 | ||
0246e64d | 11675 | skip_full_check: |
51c39bb1 | 11676 | kvfree(env->explored_states); |
0246e64d | 11677 | |
c131187d | 11678 | if (ret == 0) |
9b38c405 | 11679 | ret = check_max_stack_depth(env); |
c131187d | 11680 | |
9b38c405 | 11681 | /* instruction rewrites happen after this point */ |
e2ae4ca2 JK |
11682 | if (is_priv) { |
11683 | if (ret == 0) | |
11684 | opt_hard_wire_dead_code_branches(env); | |
52875a04 JK |
11685 | if (ret == 0) |
11686 | ret = opt_remove_dead_code(env); | |
a1b14abc JK |
11687 | if (ret == 0) |
11688 | ret = opt_remove_nops(env); | |
52875a04 JK |
11689 | } else { |
11690 | if (ret == 0) | |
11691 | sanitize_dead_code(env); | |
e2ae4ca2 JK |
11692 | } |
11693 | ||
9bac3d6d AS |
11694 | if (ret == 0) |
11695 | /* program is valid, convert *(u32*)(ctx + off) accesses */ | |
11696 | ret = convert_ctx_accesses(env); | |
11697 | ||
e245c5c6 | 11698 | if (ret == 0) |
79741b3b | 11699 | ret = fixup_bpf_calls(env); |
e245c5c6 | 11700 | |
a4b1d3c1 JW |
11701 | /* do 32-bit optimization after insn patching has done so those patched |
11702 | * insns could be handled correctly. | |
11703 | */ | |
d6c2308c JW |
11704 | if (ret == 0 && !bpf_prog_is_dev_bound(env->prog->aux)) { |
11705 | ret = opt_subreg_zext_lo32_rnd_hi32(env, attr); | |
11706 | env->prog->aux->verifier_zext = bpf_jit_needs_zext() ? !ret | |
11707 | : false; | |
a4b1d3c1 JW |
11708 | } |
11709 | ||
1ea47e01 AS |
11710 | if (ret == 0) |
11711 | ret = fixup_call_args(env); | |
11712 | ||
06ee7115 AS |
11713 | env->verification_time = ktime_get_ns() - start_time; |
11714 | print_verification_stats(env); | |
11715 | ||
a2a7d570 | 11716 | if (log->level && bpf_verifier_log_full(log)) |
cbd35700 | 11717 | ret = -ENOSPC; |
a2a7d570 | 11718 | if (log->level && !log->ubuf) { |
cbd35700 | 11719 | ret = -EFAULT; |
a2a7d570 | 11720 | goto err_release_maps; |
cbd35700 AS |
11721 | } |
11722 | ||
0246e64d AS |
11723 | if (ret == 0 && env->used_map_cnt) { |
11724 | /* if program passed verifier, update used_maps in bpf_prog_info */ | |
9bac3d6d AS |
11725 | env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt, |
11726 | sizeof(env->used_maps[0]), | |
11727 | GFP_KERNEL); | |
0246e64d | 11728 | |
9bac3d6d | 11729 | if (!env->prog->aux->used_maps) { |
0246e64d | 11730 | ret = -ENOMEM; |
a2a7d570 | 11731 | goto err_release_maps; |
0246e64d AS |
11732 | } |
11733 | ||
9bac3d6d | 11734 | memcpy(env->prog->aux->used_maps, env->used_maps, |
0246e64d | 11735 | sizeof(env->used_maps[0]) * env->used_map_cnt); |
9bac3d6d | 11736 | env->prog->aux->used_map_cnt = env->used_map_cnt; |
0246e64d AS |
11737 | |
11738 | /* program is valid. Convert pseudo bpf_ld_imm64 into generic | |
11739 | * bpf_ld_imm64 instructions | |
11740 | */ | |
11741 | convert_pseudo_ld_imm64(env); | |
11742 | } | |
cbd35700 | 11743 | |
ba64e7d8 YS |
11744 | if (ret == 0) |
11745 | adjust_btf_func(env); | |
11746 | ||
a2a7d570 | 11747 | err_release_maps: |
9bac3d6d | 11748 | if (!env->prog->aux->used_maps) |
0246e64d | 11749 | /* if we didn't copy map pointers into bpf_prog_info, release |
ab7f5bf0 | 11750 | * them now. Otherwise free_used_maps() will release them. |
0246e64d AS |
11751 | */ |
11752 | release_maps(env); | |
03f87c0b THJ |
11753 | |
11754 | /* extension progs temporarily inherit the attach_type of their targets | |
11755 | for verification purposes, so set it back to zero before returning | |
11756 | */ | |
11757 | if (env->prog->type == BPF_PROG_TYPE_EXT) | |
11758 | env->prog->expected_attach_type = 0; | |
11759 | ||
9bac3d6d | 11760 | *prog = env->prog; |
3df126f3 | 11761 | err_unlock: |
45a73c17 AS |
11762 | if (!is_priv) |
11763 | mutex_unlock(&bpf_verifier_lock); | |
3df126f3 JK |
11764 | vfree(env->insn_aux_data); |
11765 | err_free_env: | |
11766 | kfree(env); | |
51580e79 AS |
11767 | return ret; |
11768 | } |