Commit | Line | Data |
---|---|---|
5b497af4 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
51580e79 | 2 | /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com |
969bf05e | 3 | * Copyright (c) 2016 Facebook |
fd978bf7 | 4 | * Copyright (c) 2018 Covalent IO, Inc. http://covalent.io |
51580e79 | 5 | */ |
838e9690 | 6 | #include <uapi/linux/btf.h> |
51580e79 AS |
7 | #include <linux/kernel.h> |
8 | #include <linux/types.h> | |
9 | #include <linux/slab.h> | |
10 | #include <linux/bpf.h> | |
838e9690 | 11 | #include <linux/btf.h> |
58e2af8b | 12 | #include <linux/bpf_verifier.h> |
51580e79 AS |
13 | #include <linux/filter.h> |
14 | #include <net/netlink.h> | |
15 | #include <linux/file.h> | |
16 | #include <linux/vmalloc.h> | |
ebb676da | 17 | #include <linux/stringify.h> |
cc8b0b92 AS |
18 | #include <linux/bsearch.h> |
19 | #include <linux/sort.h> | |
c195651e | 20 | #include <linux/perf_event.h> |
d9762e84 | 21 | #include <linux/ctype.h> |
51580e79 | 22 | |
f4ac7e0b JK |
23 | #include "disasm.h" |
24 | ||
00176a34 JK |
25 | static const struct bpf_verifier_ops * const bpf_verifier_ops[] = { |
26 | #define BPF_PROG_TYPE(_id, _name) \ | |
27 | [_id] = & _name ## _verifier_ops, | |
28 | #define BPF_MAP_TYPE(_id, _ops) | |
29 | #include <linux/bpf_types.h> | |
30 | #undef BPF_PROG_TYPE | |
31 | #undef BPF_MAP_TYPE | |
32 | }; | |
33 | ||
51580e79 AS |
34 | /* bpf_check() is a static code analyzer that walks eBPF program |
35 | * instruction by instruction and updates register/stack state. | |
36 | * All paths of conditional branches are analyzed until 'bpf_exit' insn. | |
37 | * | |
38 | * The first pass is depth-first-search to check that the program is a DAG. | |
39 | * It rejects the following programs: | |
40 | * - larger than BPF_MAXINSNS insns | |
41 | * - if loop is present (detected via back-edge) | |
42 | * - unreachable insns exist (shouldn't be a forest. program = one function) | |
43 | * - out of bounds or malformed jumps | |
44 | * The second pass is all possible path descent from the 1st insn. | |
45 | * Since it's analyzing all pathes through the program, the length of the | |
eba38a96 | 46 | * analysis is limited to 64k insn, which may be hit even if total number of |
51580e79 AS |
47 | * insn is less then 4K, but there are too many branches that change stack/regs. |
48 | * Number of 'branches to be analyzed' is limited to 1k | |
49 | * | |
50 | * On entry to each instruction, each register has a type, and the instruction | |
51 | * changes the types of the registers depending on instruction semantics. | |
52 | * If instruction is BPF_MOV64_REG(BPF_REG_1, BPF_REG_5), then type of R5 is | |
53 | * copied to R1. | |
54 | * | |
55 | * All registers are 64-bit. | |
56 | * R0 - return register | |
57 | * R1-R5 argument passing registers | |
58 | * R6-R9 callee saved registers | |
59 | * R10 - frame pointer read-only | |
60 | * | |
61 | * At the start of BPF program the register R1 contains a pointer to bpf_context | |
62 | * and has type PTR_TO_CTX. | |
63 | * | |
64 | * Verifier tracks arithmetic operations on pointers in case: | |
65 | * BPF_MOV64_REG(BPF_REG_1, BPF_REG_10), | |
66 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -20), | |
67 | * 1st insn copies R10 (which has FRAME_PTR) type into R1 | |
68 | * and 2nd arithmetic instruction is pattern matched to recognize | |
69 | * that it wants to construct a pointer to some element within stack. | |
70 | * So after 2nd insn, the register R1 has type PTR_TO_STACK | |
71 | * (and -20 constant is saved for further stack bounds checking). | |
72 | * Meaning that this reg is a pointer to stack plus known immediate constant. | |
73 | * | |
f1174f77 | 74 | * Most of the time the registers have SCALAR_VALUE type, which |
51580e79 | 75 | * means the register has some value, but it's not a valid pointer. |
f1174f77 | 76 | * (like pointer plus pointer becomes SCALAR_VALUE type) |
51580e79 AS |
77 | * |
78 | * When verifier sees load or store instructions the type of base register | |
c64b7983 JS |
79 | * can be: PTR_TO_MAP_VALUE, PTR_TO_CTX, PTR_TO_STACK, PTR_TO_SOCKET. These are |
80 | * four pointer types recognized by check_mem_access() function. | |
51580e79 AS |
81 | * |
82 | * PTR_TO_MAP_VALUE means that this register is pointing to 'map element value' | |
83 | * and the range of [ptr, ptr + map's value_size) is accessible. | |
84 | * | |
85 | * registers used to pass values to function calls are checked against | |
86 | * function argument constraints. | |
87 | * | |
88 | * ARG_PTR_TO_MAP_KEY is one of such argument constraints. | |
89 | * It means that the register type passed to this function must be | |
90 | * PTR_TO_STACK and it will be used inside the function as | |
91 | * 'pointer to map element key' | |
92 | * | |
93 | * For example the argument constraints for bpf_map_lookup_elem(): | |
94 | * .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL, | |
95 | * .arg1_type = ARG_CONST_MAP_PTR, | |
96 | * .arg2_type = ARG_PTR_TO_MAP_KEY, | |
97 | * | |
98 | * ret_type says that this function returns 'pointer to map elem value or null' | |
99 | * function expects 1st argument to be a const pointer to 'struct bpf_map' and | |
100 | * 2nd argument should be a pointer to stack, which will be used inside | |
101 | * the helper function as a pointer to map element key. | |
102 | * | |
103 | * On the kernel side the helper function looks like: | |
104 | * u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) | |
105 | * { | |
106 | * struct bpf_map *map = (struct bpf_map *) (unsigned long) r1; | |
107 | * void *key = (void *) (unsigned long) r2; | |
108 | * void *value; | |
109 | * | |
110 | * here kernel can access 'key' and 'map' pointers safely, knowing that | |
111 | * [key, key + map->key_size) bytes are valid and were initialized on | |
112 | * the stack of eBPF program. | |
113 | * } | |
114 | * | |
115 | * Corresponding eBPF program may look like: | |
116 | * BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), // after this insn R2 type is FRAME_PTR | |
117 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), // after this insn R2 type is PTR_TO_STACK | |
118 | * BPF_LD_MAP_FD(BPF_REG_1, map_fd), // after this insn R1 type is CONST_PTR_TO_MAP | |
119 | * BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem), | |
120 | * here verifier looks at prototype of map_lookup_elem() and sees: | |
121 | * .arg1_type == ARG_CONST_MAP_PTR and R1->type == CONST_PTR_TO_MAP, which is ok, | |
122 | * Now verifier knows that this map has key of R1->map_ptr->key_size bytes | |
123 | * | |
124 | * Then .arg2_type == ARG_PTR_TO_MAP_KEY and R2->type == PTR_TO_STACK, ok so far, | |
125 | * Now verifier checks that [R2, R2 + map's key_size) are within stack limits | |
126 | * and were initialized prior to this call. | |
127 | * If it's ok, then verifier allows this BPF_CALL insn and looks at | |
128 | * .ret_type which is RET_PTR_TO_MAP_VALUE_OR_NULL, so it sets | |
129 | * R0->type = PTR_TO_MAP_VALUE_OR_NULL which means bpf_map_lookup_elem() function | |
130 | * returns ether pointer to map value or NULL. | |
131 | * | |
132 | * When type PTR_TO_MAP_VALUE_OR_NULL passes through 'if (reg != 0) goto +off' | |
133 | * insn, the register holding that pointer in the true branch changes state to | |
134 | * PTR_TO_MAP_VALUE and the same register changes state to CONST_IMM in the false | |
135 | * branch. See check_cond_jmp_op(). | |
136 | * | |
137 | * After the call R0 is set to return type of the function and registers R1-R5 | |
138 | * are set to NOT_INIT to indicate that they are no longer readable. | |
fd978bf7 JS |
139 | * |
140 | * The following reference types represent a potential reference to a kernel | |
141 | * resource which, after first being allocated, must be checked and freed by | |
142 | * the BPF program: | |
143 | * - PTR_TO_SOCKET_OR_NULL, PTR_TO_SOCKET | |
144 | * | |
145 | * When the verifier sees a helper call return a reference type, it allocates a | |
146 | * pointer id for the reference and stores it in the current function state. | |
147 | * Similar to the way that PTR_TO_MAP_VALUE_OR_NULL is converted into | |
148 | * PTR_TO_MAP_VALUE, PTR_TO_SOCKET_OR_NULL becomes PTR_TO_SOCKET when the type | |
149 | * passes through a NULL-check conditional. For the branch wherein the state is | |
150 | * changed to CONST_IMM, the verifier releases the reference. | |
6acc9b43 JS |
151 | * |
152 | * For each helper function that allocates a reference, such as | |
153 | * bpf_sk_lookup_tcp(), there is a corresponding release function, such as | |
154 | * bpf_sk_release(). When a reference type passes into the release function, | |
155 | * the verifier also releases the reference. If any unchecked or unreleased | |
156 | * reference remains at the end of the program, the verifier rejects it. | |
51580e79 AS |
157 | */ |
158 | ||
17a52670 | 159 | /* verifier_state + insn_idx are pushed to stack when branch is encountered */ |
58e2af8b | 160 | struct bpf_verifier_stack_elem { |
17a52670 AS |
161 | /* verifer state is 'st' |
162 | * before processing instruction 'insn_idx' | |
163 | * and after processing instruction 'prev_insn_idx' | |
164 | */ | |
58e2af8b | 165 | struct bpf_verifier_state st; |
17a52670 AS |
166 | int insn_idx; |
167 | int prev_insn_idx; | |
58e2af8b | 168 | struct bpf_verifier_stack_elem *next; |
cbd35700 AS |
169 | }; |
170 | ||
b285fcb7 | 171 | #define BPF_COMPLEXITY_LIMIT_JMP_SEQ 8192 |
ceefbc96 | 172 | #define BPF_COMPLEXITY_LIMIT_STATES 64 |
07016151 | 173 | |
c93552c4 DB |
174 | #define BPF_MAP_PTR_UNPRIV 1UL |
175 | #define BPF_MAP_PTR_POISON ((void *)((0xeB9FUL << 1) + \ | |
176 | POISON_POINTER_DELTA)) | |
177 | #define BPF_MAP_PTR(X) ((struct bpf_map *)((X) & ~BPF_MAP_PTR_UNPRIV)) | |
178 | ||
179 | static bool bpf_map_ptr_poisoned(const struct bpf_insn_aux_data *aux) | |
180 | { | |
181 | return BPF_MAP_PTR(aux->map_state) == BPF_MAP_PTR_POISON; | |
182 | } | |
183 | ||
184 | static bool bpf_map_ptr_unpriv(const struct bpf_insn_aux_data *aux) | |
185 | { | |
186 | return aux->map_state & BPF_MAP_PTR_UNPRIV; | |
187 | } | |
188 | ||
189 | static void bpf_map_ptr_store(struct bpf_insn_aux_data *aux, | |
190 | const struct bpf_map *map, bool unpriv) | |
191 | { | |
192 | BUILD_BUG_ON((unsigned long)BPF_MAP_PTR_POISON & BPF_MAP_PTR_UNPRIV); | |
193 | unpriv |= bpf_map_ptr_unpriv(aux); | |
194 | aux->map_state = (unsigned long)map | | |
195 | (unpriv ? BPF_MAP_PTR_UNPRIV : 0UL); | |
196 | } | |
fad73a1a | 197 | |
33ff9823 DB |
198 | struct bpf_call_arg_meta { |
199 | struct bpf_map *map_ptr; | |
435faee1 | 200 | bool raw_mode; |
36bbef52 | 201 | bool pkt_access; |
435faee1 DB |
202 | int regno; |
203 | int access_size; | |
849fa506 YS |
204 | s64 msize_smax_value; |
205 | u64 msize_umax_value; | |
1b986589 | 206 | int ref_obj_id; |
d83525ca | 207 | int func_id; |
33ff9823 DB |
208 | }; |
209 | ||
8580ac94 AS |
210 | struct btf *btf_vmlinux; |
211 | ||
cbd35700 AS |
212 | static DEFINE_MUTEX(bpf_verifier_lock); |
213 | ||
d9762e84 MKL |
214 | static const struct bpf_line_info * |
215 | find_linfo(const struct bpf_verifier_env *env, u32 insn_off) | |
216 | { | |
217 | const struct bpf_line_info *linfo; | |
218 | const struct bpf_prog *prog; | |
219 | u32 i, nr_linfo; | |
220 | ||
221 | prog = env->prog; | |
222 | nr_linfo = prog->aux->nr_linfo; | |
223 | ||
224 | if (!nr_linfo || insn_off >= prog->len) | |
225 | return NULL; | |
226 | ||
227 | linfo = prog->aux->linfo; | |
228 | for (i = 1; i < nr_linfo; i++) | |
229 | if (insn_off < linfo[i].insn_off) | |
230 | break; | |
231 | ||
232 | return &linfo[i - 1]; | |
233 | } | |
234 | ||
77d2e05a MKL |
235 | void bpf_verifier_vlog(struct bpf_verifier_log *log, const char *fmt, |
236 | va_list args) | |
cbd35700 | 237 | { |
a2a7d570 | 238 | unsigned int n; |
cbd35700 | 239 | |
a2a7d570 | 240 | n = vscnprintf(log->kbuf, BPF_VERIFIER_TMP_LOG_SIZE, fmt, args); |
a2a7d570 JK |
241 | |
242 | WARN_ONCE(n >= BPF_VERIFIER_TMP_LOG_SIZE - 1, | |
243 | "verifier log line truncated - local buffer too short\n"); | |
244 | ||
245 | n = min(log->len_total - log->len_used - 1, n); | |
246 | log->kbuf[n] = '\0'; | |
247 | ||
8580ac94 AS |
248 | if (log->level == BPF_LOG_KERNEL) { |
249 | pr_err("BPF:%s\n", log->kbuf); | |
250 | return; | |
251 | } | |
a2a7d570 JK |
252 | if (!copy_to_user(log->ubuf + log->len_used, log->kbuf, n + 1)) |
253 | log->len_used += n; | |
254 | else | |
255 | log->ubuf = NULL; | |
cbd35700 | 256 | } |
abe08840 JO |
257 | |
258 | /* log_level controls verbosity level of eBPF verifier. | |
259 | * bpf_verifier_log_write() is used to dump the verification trace to the log, | |
260 | * so the user can figure out what's wrong with the program | |
430e68d1 | 261 | */ |
abe08840 JO |
262 | __printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env, |
263 | const char *fmt, ...) | |
264 | { | |
265 | va_list args; | |
266 | ||
77d2e05a MKL |
267 | if (!bpf_verifier_log_needed(&env->log)) |
268 | return; | |
269 | ||
abe08840 | 270 | va_start(args, fmt); |
77d2e05a | 271 | bpf_verifier_vlog(&env->log, fmt, args); |
abe08840 JO |
272 | va_end(args); |
273 | } | |
274 | EXPORT_SYMBOL_GPL(bpf_verifier_log_write); | |
275 | ||
276 | __printf(2, 3) static void verbose(void *private_data, const char *fmt, ...) | |
277 | { | |
77d2e05a | 278 | struct bpf_verifier_env *env = private_data; |
abe08840 JO |
279 | va_list args; |
280 | ||
77d2e05a MKL |
281 | if (!bpf_verifier_log_needed(&env->log)) |
282 | return; | |
283 | ||
abe08840 | 284 | va_start(args, fmt); |
77d2e05a | 285 | bpf_verifier_vlog(&env->log, fmt, args); |
abe08840 JO |
286 | va_end(args); |
287 | } | |
cbd35700 | 288 | |
d9762e84 MKL |
289 | static const char *ltrim(const char *s) |
290 | { | |
291 | while (isspace(*s)) | |
292 | s++; | |
293 | ||
294 | return s; | |
295 | } | |
296 | ||
297 | __printf(3, 4) static void verbose_linfo(struct bpf_verifier_env *env, | |
298 | u32 insn_off, | |
299 | const char *prefix_fmt, ...) | |
300 | { | |
301 | const struct bpf_line_info *linfo; | |
302 | ||
303 | if (!bpf_verifier_log_needed(&env->log)) | |
304 | return; | |
305 | ||
306 | linfo = find_linfo(env, insn_off); | |
307 | if (!linfo || linfo == env->prev_linfo) | |
308 | return; | |
309 | ||
310 | if (prefix_fmt) { | |
311 | va_list args; | |
312 | ||
313 | va_start(args, prefix_fmt); | |
314 | bpf_verifier_vlog(&env->log, prefix_fmt, args); | |
315 | va_end(args); | |
316 | } | |
317 | ||
318 | verbose(env, "%s\n", | |
319 | ltrim(btf_name_by_offset(env->prog->aux->btf, | |
320 | linfo->line_off))); | |
321 | ||
322 | env->prev_linfo = linfo; | |
323 | } | |
324 | ||
de8f3a83 DB |
325 | static bool type_is_pkt_pointer(enum bpf_reg_type type) |
326 | { | |
327 | return type == PTR_TO_PACKET || | |
328 | type == PTR_TO_PACKET_META; | |
329 | } | |
330 | ||
46f8bc92 MKL |
331 | static bool type_is_sk_pointer(enum bpf_reg_type type) |
332 | { | |
333 | return type == PTR_TO_SOCKET || | |
655a51e5 | 334 | type == PTR_TO_SOCK_COMMON || |
fada7fdc JL |
335 | type == PTR_TO_TCP_SOCK || |
336 | type == PTR_TO_XDP_SOCK; | |
46f8bc92 MKL |
337 | } |
338 | ||
840b9615 JS |
339 | static bool reg_type_may_be_null(enum bpf_reg_type type) |
340 | { | |
fd978bf7 | 341 | return type == PTR_TO_MAP_VALUE_OR_NULL || |
46f8bc92 | 342 | type == PTR_TO_SOCKET_OR_NULL || |
655a51e5 MKL |
343 | type == PTR_TO_SOCK_COMMON_OR_NULL || |
344 | type == PTR_TO_TCP_SOCK_OR_NULL; | |
fd978bf7 JS |
345 | } |
346 | ||
d83525ca AS |
347 | static bool reg_may_point_to_spin_lock(const struct bpf_reg_state *reg) |
348 | { | |
349 | return reg->type == PTR_TO_MAP_VALUE && | |
350 | map_value_has_spin_lock(reg->map_ptr); | |
351 | } | |
352 | ||
cba368c1 MKL |
353 | static bool reg_type_may_be_refcounted_or_null(enum bpf_reg_type type) |
354 | { | |
355 | return type == PTR_TO_SOCKET || | |
356 | type == PTR_TO_SOCKET_OR_NULL || | |
357 | type == PTR_TO_TCP_SOCK || | |
358 | type == PTR_TO_TCP_SOCK_OR_NULL; | |
359 | } | |
360 | ||
1b986589 | 361 | static bool arg_type_may_be_refcounted(enum bpf_arg_type type) |
fd978bf7 | 362 | { |
1b986589 | 363 | return type == ARG_PTR_TO_SOCK_COMMON; |
fd978bf7 JS |
364 | } |
365 | ||
366 | /* Determine whether the function releases some resources allocated by another | |
367 | * function call. The first reference type argument will be assumed to be | |
368 | * released by release_reference(). | |
369 | */ | |
370 | static bool is_release_function(enum bpf_func_id func_id) | |
371 | { | |
6acc9b43 | 372 | return func_id == BPF_FUNC_sk_release; |
840b9615 JS |
373 | } |
374 | ||
46f8bc92 MKL |
375 | static bool is_acquire_function(enum bpf_func_id func_id) |
376 | { | |
377 | return func_id == BPF_FUNC_sk_lookup_tcp || | |
edbf8c01 LB |
378 | func_id == BPF_FUNC_sk_lookup_udp || |
379 | func_id == BPF_FUNC_skc_lookup_tcp; | |
46f8bc92 MKL |
380 | } |
381 | ||
1b986589 MKL |
382 | static bool is_ptr_cast_function(enum bpf_func_id func_id) |
383 | { | |
384 | return func_id == BPF_FUNC_tcp_sock || | |
385 | func_id == BPF_FUNC_sk_fullsock; | |
386 | } | |
387 | ||
17a52670 AS |
388 | /* string representation of 'enum bpf_reg_type' */ |
389 | static const char * const reg_type_str[] = { | |
390 | [NOT_INIT] = "?", | |
f1174f77 | 391 | [SCALAR_VALUE] = "inv", |
17a52670 AS |
392 | [PTR_TO_CTX] = "ctx", |
393 | [CONST_PTR_TO_MAP] = "map_ptr", | |
394 | [PTR_TO_MAP_VALUE] = "map_value", | |
395 | [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null", | |
17a52670 | 396 | [PTR_TO_STACK] = "fp", |
969bf05e | 397 | [PTR_TO_PACKET] = "pkt", |
de8f3a83 | 398 | [PTR_TO_PACKET_META] = "pkt_meta", |
969bf05e | 399 | [PTR_TO_PACKET_END] = "pkt_end", |
d58e468b | 400 | [PTR_TO_FLOW_KEYS] = "flow_keys", |
c64b7983 JS |
401 | [PTR_TO_SOCKET] = "sock", |
402 | [PTR_TO_SOCKET_OR_NULL] = "sock_or_null", | |
46f8bc92 MKL |
403 | [PTR_TO_SOCK_COMMON] = "sock_common", |
404 | [PTR_TO_SOCK_COMMON_OR_NULL] = "sock_common_or_null", | |
655a51e5 MKL |
405 | [PTR_TO_TCP_SOCK] = "tcp_sock", |
406 | [PTR_TO_TCP_SOCK_OR_NULL] = "tcp_sock_or_null", | |
9df1c28b | 407 | [PTR_TO_TP_BUFFER] = "tp_buffer", |
fada7fdc | 408 | [PTR_TO_XDP_SOCK] = "xdp_sock", |
17a52670 AS |
409 | }; |
410 | ||
8efea21d EC |
411 | static char slot_type_char[] = { |
412 | [STACK_INVALID] = '?', | |
413 | [STACK_SPILL] = 'r', | |
414 | [STACK_MISC] = 'm', | |
415 | [STACK_ZERO] = '0', | |
416 | }; | |
417 | ||
4e92024a AS |
418 | static void print_liveness(struct bpf_verifier_env *env, |
419 | enum bpf_reg_liveness live) | |
420 | { | |
9242b5f5 | 421 | if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN | REG_LIVE_DONE)) |
4e92024a AS |
422 | verbose(env, "_"); |
423 | if (live & REG_LIVE_READ) | |
424 | verbose(env, "r"); | |
425 | if (live & REG_LIVE_WRITTEN) | |
426 | verbose(env, "w"); | |
9242b5f5 AS |
427 | if (live & REG_LIVE_DONE) |
428 | verbose(env, "D"); | |
4e92024a AS |
429 | } |
430 | ||
f4d7e40a AS |
431 | static struct bpf_func_state *func(struct bpf_verifier_env *env, |
432 | const struct bpf_reg_state *reg) | |
433 | { | |
434 | struct bpf_verifier_state *cur = env->cur_state; | |
435 | ||
436 | return cur->frame[reg->frameno]; | |
437 | } | |
438 | ||
61bd5218 | 439 | static void print_verifier_state(struct bpf_verifier_env *env, |
f4d7e40a | 440 | const struct bpf_func_state *state) |
17a52670 | 441 | { |
f4d7e40a | 442 | const struct bpf_reg_state *reg; |
17a52670 AS |
443 | enum bpf_reg_type t; |
444 | int i; | |
445 | ||
f4d7e40a AS |
446 | if (state->frameno) |
447 | verbose(env, " frame%d:", state->frameno); | |
17a52670 | 448 | for (i = 0; i < MAX_BPF_REG; i++) { |
1a0dc1ac AS |
449 | reg = &state->regs[i]; |
450 | t = reg->type; | |
17a52670 AS |
451 | if (t == NOT_INIT) |
452 | continue; | |
4e92024a AS |
453 | verbose(env, " R%d", i); |
454 | print_liveness(env, reg->live); | |
455 | verbose(env, "=%s", reg_type_str[t]); | |
b5dc0163 AS |
456 | if (t == SCALAR_VALUE && reg->precise) |
457 | verbose(env, "P"); | |
f1174f77 EC |
458 | if ((t == SCALAR_VALUE || t == PTR_TO_STACK) && |
459 | tnum_is_const(reg->var_off)) { | |
460 | /* reg->off should be 0 for SCALAR_VALUE */ | |
61bd5218 | 461 | verbose(env, "%lld", reg->var_off.value + reg->off); |
f1174f77 | 462 | } else { |
cba368c1 MKL |
463 | verbose(env, "(id=%d", reg->id); |
464 | if (reg_type_may_be_refcounted_or_null(t)) | |
465 | verbose(env, ",ref_obj_id=%d", reg->ref_obj_id); | |
f1174f77 | 466 | if (t != SCALAR_VALUE) |
61bd5218 | 467 | verbose(env, ",off=%d", reg->off); |
de8f3a83 | 468 | if (type_is_pkt_pointer(t)) |
61bd5218 | 469 | verbose(env, ",r=%d", reg->range); |
f1174f77 EC |
470 | else if (t == CONST_PTR_TO_MAP || |
471 | t == PTR_TO_MAP_VALUE || | |
472 | t == PTR_TO_MAP_VALUE_OR_NULL) | |
61bd5218 | 473 | verbose(env, ",ks=%d,vs=%d", |
f1174f77 EC |
474 | reg->map_ptr->key_size, |
475 | reg->map_ptr->value_size); | |
7d1238f2 EC |
476 | if (tnum_is_const(reg->var_off)) { |
477 | /* Typically an immediate SCALAR_VALUE, but | |
478 | * could be a pointer whose offset is too big | |
479 | * for reg->off | |
480 | */ | |
61bd5218 | 481 | verbose(env, ",imm=%llx", reg->var_off.value); |
7d1238f2 EC |
482 | } else { |
483 | if (reg->smin_value != reg->umin_value && | |
484 | reg->smin_value != S64_MIN) | |
61bd5218 | 485 | verbose(env, ",smin_value=%lld", |
7d1238f2 EC |
486 | (long long)reg->smin_value); |
487 | if (reg->smax_value != reg->umax_value && | |
488 | reg->smax_value != S64_MAX) | |
61bd5218 | 489 | verbose(env, ",smax_value=%lld", |
7d1238f2 EC |
490 | (long long)reg->smax_value); |
491 | if (reg->umin_value != 0) | |
61bd5218 | 492 | verbose(env, ",umin_value=%llu", |
7d1238f2 EC |
493 | (unsigned long long)reg->umin_value); |
494 | if (reg->umax_value != U64_MAX) | |
61bd5218 | 495 | verbose(env, ",umax_value=%llu", |
7d1238f2 EC |
496 | (unsigned long long)reg->umax_value); |
497 | if (!tnum_is_unknown(reg->var_off)) { | |
498 | char tn_buf[48]; | |
f1174f77 | 499 | |
7d1238f2 | 500 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
61bd5218 | 501 | verbose(env, ",var_off=%s", tn_buf); |
7d1238f2 | 502 | } |
f1174f77 | 503 | } |
61bd5218 | 504 | verbose(env, ")"); |
f1174f77 | 505 | } |
17a52670 | 506 | } |
638f5b90 | 507 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
8efea21d EC |
508 | char types_buf[BPF_REG_SIZE + 1]; |
509 | bool valid = false; | |
510 | int j; | |
511 | ||
512 | for (j = 0; j < BPF_REG_SIZE; j++) { | |
513 | if (state->stack[i].slot_type[j] != STACK_INVALID) | |
514 | valid = true; | |
515 | types_buf[j] = slot_type_char[ | |
516 | state->stack[i].slot_type[j]]; | |
517 | } | |
518 | types_buf[BPF_REG_SIZE] = 0; | |
519 | if (!valid) | |
520 | continue; | |
521 | verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE); | |
522 | print_liveness(env, state->stack[i].spilled_ptr.live); | |
b5dc0163 AS |
523 | if (state->stack[i].slot_type[0] == STACK_SPILL) { |
524 | reg = &state->stack[i].spilled_ptr; | |
525 | t = reg->type; | |
526 | verbose(env, "=%s", reg_type_str[t]); | |
527 | if (t == SCALAR_VALUE && reg->precise) | |
528 | verbose(env, "P"); | |
529 | if (t == SCALAR_VALUE && tnum_is_const(reg->var_off)) | |
530 | verbose(env, "%lld", reg->var_off.value + reg->off); | |
531 | } else { | |
8efea21d | 532 | verbose(env, "=%s", types_buf); |
b5dc0163 | 533 | } |
17a52670 | 534 | } |
fd978bf7 JS |
535 | if (state->acquired_refs && state->refs[0].id) { |
536 | verbose(env, " refs=%d", state->refs[0].id); | |
537 | for (i = 1; i < state->acquired_refs; i++) | |
538 | if (state->refs[i].id) | |
539 | verbose(env, ",%d", state->refs[i].id); | |
540 | } | |
61bd5218 | 541 | verbose(env, "\n"); |
17a52670 AS |
542 | } |
543 | ||
84dbf350 JS |
544 | #define COPY_STATE_FN(NAME, COUNT, FIELD, SIZE) \ |
545 | static int copy_##NAME##_state(struct bpf_func_state *dst, \ | |
546 | const struct bpf_func_state *src) \ | |
547 | { \ | |
548 | if (!src->FIELD) \ | |
549 | return 0; \ | |
550 | if (WARN_ON_ONCE(dst->COUNT < src->COUNT)) { \ | |
551 | /* internal bug, make state invalid to reject the program */ \ | |
552 | memset(dst, 0, sizeof(*dst)); \ | |
553 | return -EFAULT; \ | |
554 | } \ | |
555 | memcpy(dst->FIELD, src->FIELD, \ | |
556 | sizeof(*src->FIELD) * (src->COUNT / SIZE)); \ | |
557 | return 0; \ | |
638f5b90 | 558 | } |
fd978bf7 JS |
559 | /* copy_reference_state() */ |
560 | COPY_STATE_FN(reference, acquired_refs, refs, 1) | |
84dbf350 JS |
561 | /* copy_stack_state() */ |
562 | COPY_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE) | |
563 | #undef COPY_STATE_FN | |
564 | ||
565 | #define REALLOC_STATE_FN(NAME, COUNT, FIELD, SIZE) \ | |
566 | static int realloc_##NAME##_state(struct bpf_func_state *state, int size, \ | |
567 | bool copy_old) \ | |
568 | { \ | |
569 | u32 old_size = state->COUNT; \ | |
570 | struct bpf_##NAME##_state *new_##FIELD; \ | |
571 | int slot = size / SIZE; \ | |
572 | \ | |
573 | if (size <= old_size || !size) { \ | |
574 | if (copy_old) \ | |
575 | return 0; \ | |
576 | state->COUNT = slot * SIZE; \ | |
577 | if (!size && old_size) { \ | |
578 | kfree(state->FIELD); \ | |
579 | state->FIELD = NULL; \ | |
580 | } \ | |
581 | return 0; \ | |
582 | } \ | |
583 | new_##FIELD = kmalloc_array(slot, sizeof(struct bpf_##NAME##_state), \ | |
584 | GFP_KERNEL); \ | |
585 | if (!new_##FIELD) \ | |
586 | return -ENOMEM; \ | |
587 | if (copy_old) { \ | |
588 | if (state->FIELD) \ | |
589 | memcpy(new_##FIELD, state->FIELD, \ | |
590 | sizeof(*new_##FIELD) * (old_size / SIZE)); \ | |
591 | memset(new_##FIELD + old_size / SIZE, 0, \ | |
592 | sizeof(*new_##FIELD) * (size - old_size) / SIZE); \ | |
593 | } \ | |
594 | state->COUNT = slot * SIZE; \ | |
595 | kfree(state->FIELD); \ | |
596 | state->FIELD = new_##FIELD; \ | |
597 | return 0; \ | |
598 | } | |
fd978bf7 JS |
599 | /* realloc_reference_state() */ |
600 | REALLOC_STATE_FN(reference, acquired_refs, refs, 1) | |
84dbf350 JS |
601 | /* realloc_stack_state() */ |
602 | REALLOC_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE) | |
603 | #undef REALLOC_STATE_FN | |
638f5b90 AS |
604 | |
605 | /* do_check() starts with zero-sized stack in struct bpf_verifier_state to | |
606 | * make it consume minimal amount of memory. check_stack_write() access from | |
f4d7e40a | 607 | * the program calls into realloc_func_state() to grow the stack size. |
84dbf350 JS |
608 | * Note there is a non-zero 'parent' pointer inside bpf_verifier_state |
609 | * which realloc_stack_state() copies over. It points to previous | |
610 | * bpf_verifier_state which is never reallocated. | |
638f5b90 | 611 | */ |
fd978bf7 JS |
612 | static int realloc_func_state(struct bpf_func_state *state, int stack_size, |
613 | int refs_size, bool copy_old) | |
638f5b90 | 614 | { |
fd978bf7 JS |
615 | int err = realloc_reference_state(state, refs_size, copy_old); |
616 | if (err) | |
617 | return err; | |
618 | return realloc_stack_state(state, stack_size, copy_old); | |
619 | } | |
620 | ||
621 | /* Acquire a pointer id from the env and update the state->refs to include | |
622 | * this new pointer reference. | |
623 | * On success, returns a valid pointer id to associate with the register | |
624 | * On failure, returns a negative errno. | |
638f5b90 | 625 | */ |
fd978bf7 | 626 | static int acquire_reference_state(struct bpf_verifier_env *env, int insn_idx) |
638f5b90 | 627 | { |
fd978bf7 JS |
628 | struct bpf_func_state *state = cur_func(env); |
629 | int new_ofs = state->acquired_refs; | |
630 | int id, err; | |
631 | ||
632 | err = realloc_reference_state(state, state->acquired_refs + 1, true); | |
633 | if (err) | |
634 | return err; | |
635 | id = ++env->id_gen; | |
636 | state->refs[new_ofs].id = id; | |
637 | state->refs[new_ofs].insn_idx = insn_idx; | |
638f5b90 | 638 | |
fd978bf7 JS |
639 | return id; |
640 | } | |
641 | ||
642 | /* release function corresponding to acquire_reference_state(). Idempotent. */ | |
46f8bc92 | 643 | static int release_reference_state(struct bpf_func_state *state, int ptr_id) |
fd978bf7 JS |
644 | { |
645 | int i, last_idx; | |
646 | ||
fd978bf7 JS |
647 | last_idx = state->acquired_refs - 1; |
648 | for (i = 0; i < state->acquired_refs; i++) { | |
649 | if (state->refs[i].id == ptr_id) { | |
650 | if (last_idx && i != last_idx) | |
651 | memcpy(&state->refs[i], &state->refs[last_idx], | |
652 | sizeof(*state->refs)); | |
653 | memset(&state->refs[last_idx], 0, sizeof(*state->refs)); | |
654 | state->acquired_refs--; | |
638f5b90 | 655 | return 0; |
638f5b90 | 656 | } |
638f5b90 | 657 | } |
46f8bc92 | 658 | return -EINVAL; |
fd978bf7 JS |
659 | } |
660 | ||
661 | static int transfer_reference_state(struct bpf_func_state *dst, | |
662 | struct bpf_func_state *src) | |
663 | { | |
664 | int err = realloc_reference_state(dst, src->acquired_refs, false); | |
665 | if (err) | |
666 | return err; | |
667 | err = copy_reference_state(dst, src); | |
668 | if (err) | |
669 | return err; | |
638f5b90 AS |
670 | return 0; |
671 | } | |
672 | ||
f4d7e40a AS |
673 | static void free_func_state(struct bpf_func_state *state) |
674 | { | |
5896351e AS |
675 | if (!state) |
676 | return; | |
fd978bf7 | 677 | kfree(state->refs); |
f4d7e40a AS |
678 | kfree(state->stack); |
679 | kfree(state); | |
680 | } | |
681 | ||
b5dc0163 AS |
682 | static void clear_jmp_history(struct bpf_verifier_state *state) |
683 | { | |
684 | kfree(state->jmp_history); | |
685 | state->jmp_history = NULL; | |
686 | state->jmp_history_cnt = 0; | |
687 | } | |
688 | ||
1969db47 AS |
689 | static void free_verifier_state(struct bpf_verifier_state *state, |
690 | bool free_self) | |
638f5b90 | 691 | { |
f4d7e40a AS |
692 | int i; |
693 | ||
694 | for (i = 0; i <= state->curframe; i++) { | |
695 | free_func_state(state->frame[i]); | |
696 | state->frame[i] = NULL; | |
697 | } | |
b5dc0163 | 698 | clear_jmp_history(state); |
1969db47 AS |
699 | if (free_self) |
700 | kfree(state); | |
638f5b90 AS |
701 | } |
702 | ||
703 | /* copy verifier state from src to dst growing dst stack space | |
704 | * when necessary to accommodate larger src stack | |
705 | */ | |
f4d7e40a AS |
706 | static int copy_func_state(struct bpf_func_state *dst, |
707 | const struct bpf_func_state *src) | |
638f5b90 AS |
708 | { |
709 | int err; | |
710 | ||
fd978bf7 JS |
711 | err = realloc_func_state(dst, src->allocated_stack, src->acquired_refs, |
712 | false); | |
713 | if (err) | |
714 | return err; | |
715 | memcpy(dst, src, offsetof(struct bpf_func_state, acquired_refs)); | |
716 | err = copy_reference_state(dst, src); | |
638f5b90 AS |
717 | if (err) |
718 | return err; | |
638f5b90 AS |
719 | return copy_stack_state(dst, src); |
720 | } | |
721 | ||
f4d7e40a AS |
722 | static int copy_verifier_state(struct bpf_verifier_state *dst_state, |
723 | const struct bpf_verifier_state *src) | |
724 | { | |
725 | struct bpf_func_state *dst; | |
b5dc0163 | 726 | u32 jmp_sz = sizeof(struct bpf_idx_pair) * src->jmp_history_cnt; |
f4d7e40a AS |
727 | int i, err; |
728 | ||
b5dc0163 AS |
729 | if (dst_state->jmp_history_cnt < src->jmp_history_cnt) { |
730 | kfree(dst_state->jmp_history); | |
731 | dst_state->jmp_history = kmalloc(jmp_sz, GFP_USER); | |
732 | if (!dst_state->jmp_history) | |
733 | return -ENOMEM; | |
734 | } | |
735 | memcpy(dst_state->jmp_history, src->jmp_history, jmp_sz); | |
736 | dst_state->jmp_history_cnt = src->jmp_history_cnt; | |
737 | ||
f4d7e40a AS |
738 | /* if dst has more stack frames then src frame, free them */ |
739 | for (i = src->curframe + 1; i <= dst_state->curframe; i++) { | |
740 | free_func_state(dst_state->frame[i]); | |
741 | dst_state->frame[i] = NULL; | |
742 | } | |
979d63d5 | 743 | dst_state->speculative = src->speculative; |
f4d7e40a | 744 | dst_state->curframe = src->curframe; |
d83525ca | 745 | dst_state->active_spin_lock = src->active_spin_lock; |
2589726d AS |
746 | dst_state->branches = src->branches; |
747 | dst_state->parent = src->parent; | |
b5dc0163 AS |
748 | dst_state->first_insn_idx = src->first_insn_idx; |
749 | dst_state->last_insn_idx = src->last_insn_idx; | |
f4d7e40a AS |
750 | for (i = 0; i <= src->curframe; i++) { |
751 | dst = dst_state->frame[i]; | |
752 | if (!dst) { | |
753 | dst = kzalloc(sizeof(*dst), GFP_KERNEL); | |
754 | if (!dst) | |
755 | return -ENOMEM; | |
756 | dst_state->frame[i] = dst; | |
757 | } | |
758 | err = copy_func_state(dst, src->frame[i]); | |
759 | if (err) | |
760 | return err; | |
761 | } | |
762 | return 0; | |
763 | } | |
764 | ||
2589726d AS |
765 | static void update_branch_counts(struct bpf_verifier_env *env, struct bpf_verifier_state *st) |
766 | { | |
767 | while (st) { | |
768 | u32 br = --st->branches; | |
769 | ||
770 | /* WARN_ON(br > 1) technically makes sense here, | |
771 | * but see comment in push_stack(), hence: | |
772 | */ | |
773 | WARN_ONCE((int)br < 0, | |
774 | "BUG update_branch_counts:branches_to_explore=%d\n", | |
775 | br); | |
776 | if (br) | |
777 | break; | |
778 | st = st->parent; | |
779 | } | |
780 | } | |
781 | ||
638f5b90 AS |
782 | static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx, |
783 | int *insn_idx) | |
784 | { | |
785 | struct bpf_verifier_state *cur = env->cur_state; | |
786 | struct bpf_verifier_stack_elem *elem, *head = env->head; | |
787 | int err; | |
17a52670 AS |
788 | |
789 | if (env->head == NULL) | |
638f5b90 | 790 | return -ENOENT; |
17a52670 | 791 | |
638f5b90 AS |
792 | if (cur) { |
793 | err = copy_verifier_state(cur, &head->st); | |
794 | if (err) | |
795 | return err; | |
796 | } | |
797 | if (insn_idx) | |
798 | *insn_idx = head->insn_idx; | |
17a52670 | 799 | if (prev_insn_idx) |
638f5b90 AS |
800 | *prev_insn_idx = head->prev_insn_idx; |
801 | elem = head->next; | |
1969db47 | 802 | free_verifier_state(&head->st, false); |
638f5b90 | 803 | kfree(head); |
17a52670 AS |
804 | env->head = elem; |
805 | env->stack_size--; | |
638f5b90 | 806 | return 0; |
17a52670 AS |
807 | } |
808 | ||
58e2af8b | 809 | static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, |
979d63d5 DB |
810 | int insn_idx, int prev_insn_idx, |
811 | bool speculative) | |
17a52670 | 812 | { |
638f5b90 | 813 | struct bpf_verifier_state *cur = env->cur_state; |
58e2af8b | 814 | struct bpf_verifier_stack_elem *elem; |
638f5b90 | 815 | int err; |
17a52670 | 816 | |
638f5b90 | 817 | elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL); |
17a52670 AS |
818 | if (!elem) |
819 | goto err; | |
820 | ||
17a52670 AS |
821 | elem->insn_idx = insn_idx; |
822 | elem->prev_insn_idx = prev_insn_idx; | |
823 | elem->next = env->head; | |
824 | env->head = elem; | |
825 | env->stack_size++; | |
1969db47 AS |
826 | err = copy_verifier_state(&elem->st, cur); |
827 | if (err) | |
828 | goto err; | |
979d63d5 | 829 | elem->st.speculative |= speculative; |
b285fcb7 AS |
830 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_JMP_SEQ) { |
831 | verbose(env, "The sequence of %d jumps is too complex.\n", | |
832 | env->stack_size); | |
17a52670 AS |
833 | goto err; |
834 | } | |
2589726d AS |
835 | if (elem->st.parent) { |
836 | ++elem->st.parent->branches; | |
837 | /* WARN_ON(branches > 2) technically makes sense here, | |
838 | * but | |
839 | * 1. speculative states will bump 'branches' for non-branch | |
840 | * instructions | |
841 | * 2. is_state_visited() heuristics may decide not to create | |
842 | * a new state for a sequence of branches and all such current | |
843 | * and cloned states will be pointing to a single parent state | |
844 | * which might have large 'branches' count. | |
845 | */ | |
846 | } | |
17a52670 AS |
847 | return &elem->st; |
848 | err: | |
5896351e AS |
849 | free_verifier_state(env->cur_state, true); |
850 | env->cur_state = NULL; | |
17a52670 | 851 | /* pop all elements and return */ |
638f5b90 | 852 | while (!pop_stack(env, NULL, NULL)); |
17a52670 AS |
853 | return NULL; |
854 | } | |
855 | ||
856 | #define CALLER_SAVED_REGS 6 | |
857 | static const int caller_saved[CALLER_SAVED_REGS] = { | |
858 | BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5 | |
859 | }; | |
860 | ||
f1174f77 EC |
861 | static void __mark_reg_not_init(struct bpf_reg_state *reg); |
862 | ||
b03c9f9f EC |
863 | /* Mark the unknown part of a register (variable offset or scalar value) as |
864 | * known to have the value @imm. | |
865 | */ | |
866 | static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm) | |
867 | { | |
a9c676bc AS |
868 | /* Clear id, off, and union(map_ptr, range) */ |
869 | memset(((u8 *)reg) + sizeof(reg->type), 0, | |
870 | offsetof(struct bpf_reg_state, var_off) - sizeof(reg->type)); | |
b03c9f9f EC |
871 | reg->var_off = tnum_const(imm); |
872 | reg->smin_value = (s64)imm; | |
873 | reg->smax_value = (s64)imm; | |
874 | reg->umin_value = imm; | |
875 | reg->umax_value = imm; | |
876 | } | |
877 | ||
f1174f77 EC |
878 | /* Mark the 'variable offset' part of a register as zero. This should be |
879 | * used only on registers holding a pointer type. | |
880 | */ | |
881 | static void __mark_reg_known_zero(struct bpf_reg_state *reg) | |
a9789ef9 | 882 | { |
b03c9f9f | 883 | __mark_reg_known(reg, 0); |
f1174f77 | 884 | } |
a9789ef9 | 885 | |
cc2b14d5 AS |
886 | static void __mark_reg_const_zero(struct bpf_reg_state *reg) |
887 | { | |
888 | __mark_reg_known(reg, 0); | |
cc2b14d5 AS |
889 | reg->type = SCALAR_VALUE; |
890 | } | |
891 | ||
61bd5218 JK |
892 | static void mark_reg_known_zero(struct bpf_verifier_env *env, |
893 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
894 | { |
895 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 896 | verbose(env, "mark_reg_known_zero(regs, %u)\n", regno); |
f1174f77 EC |
897 | /* Something bad happened, let's kill all regs */ |
898 | for (regno = 0; regno < MAX_BPF_REG; regno++) | |
899 | __mark_reg_not_init(regs + regno); | |
900 | return; | |
901 | } | |
902 | __mark_reg_known_zero(regs + regno); | |
903 | } | |
904 | ||
de8f3a83 DB |
905 | static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg) |
906 | { | |
907 | return type_is_pkt_pointer(reg->type); | |
908 | } | |
909 | ||
910 | static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg) | |
911 | { | |
912 | return reg_is_pkt_pointer(reg) || | |
913 | reg->type == PTR_TO_PACKET_END; | |
914 | } | |
915 | ||
916 | /* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */ | |
917 | static bool reg_is_init_pkt_pointer(const struct bpf_reg_state *reg, | |
918 | enum bpf_reg_type which) | |
919 | { | |
920 | /* The register can already have a range from prior markings. | |
921 | * This is fine as long as it hasn't been advanced from its | |
922 | * origin. | |
923 | */ | |
924 | return reg->type == which && | |
925 | reg->id == 0 && | |
926 | reg->off == 0 && | |
927 | tnum_equals_const(reg->var_off, 0); | |
928 | } | |
929 | ||
b03c9f9f EC |
930 | /* Attempts to improve min/max values based on var_off information */ |
931 | static void __update_reg_bounds(struct bpf_reg_state *reg) | |
932 | { | |
933 | /* min signed is max(sign bit) | min(other bits) */ | |
934 | reg->smin_value = max_t(s64, reg->smin_value, | |
935 | reg->var_off.value | (reg->var_off.mask & S64_MIN)); | |
936 | /* max signed is min(sign bit) | max(other bits) */ | |
937 | reg->smax_value = min_t(s64, reg->smax_value, | |
938 | reg->var_off.value | (reg->var_off.mask & S64_MAX)); | |
939 | reg->umin_value = max(reg->umin_value, reg->var_off.value); | |
940 | reg->umax_value = min(reg->umax_value, | |
941 | reg->var_off.value | reg->var_off.mask); | |
942 | } | |
943 | ||
944 | /* Uses signed min/max values to inform unsigned, and vice-versa */ | |
945 | static void __reg_deduce_bounds(struct bpf_reg_state *reg) | |
946 | { | |
947 | /* Learn sign from signed bounds. | |
948 | * If we cannot cross the sign boundary, then signed and unsigned bounds | |
949 | * are the same, so combine. This works even in the negative case, e.g. | |
950 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. | |
951 | */ | |
952 | if (reg->smin_value >= 0 || reg->smax_value < 0) { | |
953 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, | |
954 | reg->umin_value); | |
955 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, | |
956 | reg->umax_value); | |
957 | return; | |
958 | } | |
959 | /* Learn sign from unsigned bounds. Signed bounds cross the sign | |
960 | * boundary, so we must be careful. | |
961 | */ | |
962 | if ((s64)reg->umax_value >= 0) { | |
963 | /* Positive. We can't learn anything from the smin, but smax | |
964 | * is positive, hence safe. | |
965 | */ | |
966 | reg->smin_value = reg->umin_value; | |
967 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, | |
968 | reg->umax_value); | |
969 | } else if ((s64)reg->umin_value < 0) { | |
970 | /* Negative. We can't learn anything from the smax, but smin | |
971 | * is negative, hence safe. | |
972 | */ | |
973 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, | |
974 | reg->umin_value); | |
975 | reg->smax_value = reg->umax_value; | |
976 | } | |
977 | } | |
978 | ||
979 | /* Attempts to improve var_off based on unsigned min/max information */ | |
980 | static void __reg_bound_offset(struct bpf_reg_state *reg) | |
981 | { | |
982 | reg->var_off = tnum_intersect(reg->var_off, | |
983 | tnum_range(reg->umin_value, | |
984 | reg->umax_value)); | |
985 | } | |
986 | ||
987 | /* Reset the min/max bounds of a register */ | |
988 | static void __mark_reg_unbounded(struct bpf_reg_state *reg) | |
989 | { | |
990 | reg->smin_value = S64_MIN; | |
991 | reg->smax_value = S64_MAX; | |
992 | reg->umin_value = 0; | |
993 | reg->umax_value = U64_MAX; | |
994 | } | |
995 | ||
f1174f77 EC |
996 | /* Mark a register as having a completely unknown (scalar) value. */ |
997 | static void __mark_reg_unknown(struct bpf_reg_state *reg) | |
998 | { | |
a9c676bc AS |
999 | /* |
1000 | * Clear type, id, off, and union(map_ptr, range) and | |
1001 | * padding between 'type' and union | |
1002 | */ | |
1003 | memset(reg, 0, offsetof(struct bpf_reg_state, var_off)); | |
f1174f77 | 1004 | reg->type = SCALAR_VALUE; |
f1174f77 | 1005 | reg->var_off = tnum_unknown; |
f4d7e40a | 1006 | reg->frameno = 0; |
b03c9f9f | 1007 | __mark_reg_unbounded(reg); |
f1174f77 EC |
1008 | } |
1009 | ||
61bd5218 JK |
1010 | static void mark_reg_unknown(struct bpf_verifier_env *env, |
1011 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
1012 | { |
1013 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 1014 | verbose(env, "mark_reg_unknown(regs, %u)\n", regno); |
19ceb417 AS |
1015 | /* Something bad happened, let's kill all regs except FP */ |
1016 | for (regno = 0; regno < BPF_REG_FP; regno++) | |
f1174f77 EC |
1017 | __mark_reg_not_init(regs + regno); |
1018 | return; | |
1019 | } | |
6754172c AS |
1020 | regs += regno; |
1021 | __mark_reg_unknown(regs); | |
1022 | /* constant backtracking is enabled for root without bpf2bpf calls */ | |
1023 | regs->precise = env->subprog_cnt > 1 || !env->allow_ptr_leaks ? | |
1024 | true : false; | |
f1174f77 EC |
1025 | } |
1026 | ||
1027 | static void __mark_reg_not_init(struct bpf_reg_state *reg) | |
1028 | { | |
1029 | __mark_reg_unknown(reg); | |
1030 | reg->type = NOT_INIT; | |
1031 | } | |
1032 | ||
61bd5218 JK |
1033 | static void mark_reg_not_init(struct bpf_verifier_env *env, |
1034 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
1035 | { |
1036 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 1037 | verbose(env, "mark_reg_not_init(regs, %u)\n", regno); |
19ceb417 AS |
1038 | /* Something bad happened, let's kill all regs except FP */ |
1039 | for (regno = 0; regno < BPF_REG_FP; regno++) | |
f1174f77 EC |
1040 | __mark_reg_not_init(regs + regno); |
1041 | return; | |
1042 | } | |
1043 | __mark_reg_not_init(regs + regno); | |
a9789ef9 DB |
1044 | } |
1045 | ||
5327ed3d | 1046 | #define DEF_NOT_SUBREG (0) |
61bd5218 | 1047 | static void init_reg_state(struct bpf_verifier_env *env, |
f4d7e40a | 1048 | struct bpf_func_state *state) |
17a52670 | 1049 | { |
f4d7e40a | 1050 | struct bpf_reg_state *regs = state->regs; |
17a52670 AS |
1051 | int i; |
1052 | ||
dc503a8a | 1053 | for (i = 0; i < MAX_BPF_REG; i++) { |
61bd5218 | 1054 | mark_reg_not_init(env, regs, i); |
dc503a8a | 1055 | regs[i].live = REG_LIVE_NONE; |
679c782d | 1056 | regs[i].parent = NULL; |
5327ed3d | 1057 | regs[i].subreg_def = DEF_NOT_SUBREG; |
dc503a8a | 1058 | } |
17a52670 AS |
1059 | |
1060 | /* frame pointer */ | |
f1174f77 | 1061 | regs[BPF_REG_FP].type = PTR_TO_STACK; |
61bd5218 | 1062 | mark_reg_known_zero(env, regs, BPF_REG_FP); |
f4d7e40a | 1063 | regs[BPF_REG_FP].frameno = state->frameno; |
17a52670 AS |
1064 | |
1065 | /* 1st arg to a function */ | |
1066 | regs[BPF_REG_1].type = PTR_TO_CTX; | |
61bd5218 | 1067 | mark_reg_known_zero(env, regs, BPF_REG_1); |
6760bf2d DB |
1068 | } |
1069 | ||
f4d7e40a AS |
1070 | #define BPF_MAIN_FUNC (-1) |
1071 | static void init_func_state(struct bpf_verifier_env *env, | |
1072 | struct bpf_func_state *state, | |
1073 | int callsite, int frameno, int subprogno) | |
1074 | { | |
1075 | state->callsite = callsite; | |
1076 | state->frameno = frameno; | |
1077 | state->subprogno = subprogno; | |
1078 | init_reg_state(env, state); | |
1079 | } | |
1080 | ||
17a52670 AS |
1081 | enum reg_arg_type { |
1082 | SRC_OP, /* register is used as source operand */ | |
1083 | DST_OP, /* register is used as destination operand */ | |
1084 | DST_OP_NO_MARK /* same as above, check only, don't mark */ | |
1085 | }; | |
1086 | ||
cc8b0b92 AS |
1087 | static int cmp_subprogs(const void *a, const void *b) |
1088 | { | |
9c8105bd JW |
1089 | return ((struct bpf_subprog_info *)a)->start - |
1090 | ((struct bpf_subprog_info *)b)->start; | |
cc8b0b92 AS |
1091 | } |
1092 | ||
1093 | static int find_subprog(struct bpf_verifier_env *env, int off) | |
1094 | { | |
9c8105bd | 1095 | struct bpf_subprog_info *p; |
cc8b0b92 | 1096 | |
9c8105bd JW |
1097 | p = bsearch(&off, env->subprog_info, env->subprog_cnt, |
1098 | sizeof(env->subprog_info[0]), cmp_subprogs); | |
cc8b0b92 AS |
1099 | if (!p) |
1100 | return -ENOENT; | |
9c8105bd | 1101 | return p - env->subprog_info; |
cc8b0b92 AS |
1102 | |
1103 | } | |
1104 | ||
1105 | static int add_subprog(struct bpf_verifier_env *env, int off) | |
1106 | { | |
1107 | int insn_cnt = env->prog->len; | |
1108 | int ret; | |
1109 | ||
1110 | if (off >= insn_cnt || off < 0) { | |
1111 | verbose(env, "call to invalid destination\n"); | |
1112 | return -EINVAL; | |
1113 | } | |
1114 | ret = find_subprog(env, off); | |
1115 | if (ret >= 0) | |
1116 | return 0; | |
4cb3d99c | 1117 | if (env->subprog_cnt >= BPF_MAX_SUBPROGS) { |
cc8b0b92 AS |
1118 | verbose(env, "too many subprograms\n"); |
1119 | return -E2BIG; | |
1120 | } | |
9c8105bd JW |
1121 | env->subprog_info[env->subprog_cnt++].start = off; |
1122 | sort(env->subprog_info, env->subprog_cnt, | |
1123 | sizeof(env->subprog_info[0]), cmp_subprogs, NULL); | |
cc8b0b92 AS |
1124 | return 0; |
1125 | } | |
1126 | ||
1127 | static int check_subprogs(struct bpf_verifier_env *env) | |
1128 | { | |
1129 | int i, ret, subprog_start, subprog_end, off, cur_subprog = 0; | |
9c8105bd | 1130 | struct bpf_subprog_info *subprog = env->subprog_info; |
cc8b0b92 AS |
1131 | struct bpf_insn *insn = env->prog->insnsi; |
1132 | int insn_cnt = env->prog->len; | |
1133 | ||
f910cefa JW |
1134 | /* Add entry function. */ |
1135 | ret = add_subprog(env, 0); | |
1136 | if (ret < 0) | |
1137 | return ret; | |
1138 | ||
cc8b0b92 AS |
1139 | /* determine subprog starts. The end is one before the next starts */ |
1140 | for (i = 0; i < insn_cnt; i++) { | |
1141 | if (insn[i].code != (BPF_JMP | BPF_CALL)) | |
1142 | continue; | |
1143 | if (insn[i].src_reg != BPF_PSEUDO_CALL) | |
1144 | continue; | |
1145 | if (!env->allow_ptr_leaks) { | |
1146 | verbose(env, "function calls to other bpf functions are allowed for root only\n"); | |
1147 | return -EPERM; | |
1148 | } | |
cc8b0b92 AS |
1149 | ret = add_subprog(env, i + insn[i].imm + 1); |
1150 | if (ret < 0) | |
1151 | return ret; | |
1152 | } | |
1153 | ||
4cb3d99c JW |
1154 | /* Add a fake 'exit' subprog which could simplify subprog iteration |
1155 | * logic. 'subprog_cnt' should not be increased. | |
1156 | */ | |
1157 | subprog[env->subprog_cnt].start = insn_cnt; | |
1158 | ||
06ee7115 | 1159 | if (env->log.level & BPF_LOG_LEVEL2) |
cc8b0b92 | 1160 | for (i = 0; i < env->subprog_cnt; i++) |
9c8105bd | 1161 | verbose(env, "func#%d @%d\n", i, subprog[i].start); |
cc8b0b92 AS |
1162 | |
1163 | /* now check that all jumps are within the same subprog */ | |
4cb3d99c JW |
1164 | subprog_start = subprog[cur_subprog].start; |
1165 | subprog_end = subprog[cur_subprog + 1].start; | |
cc8b0b92 AS |
1166 | for (i = 0; i < insn_cnt; i++) { |
1167 | u8 code = insn[i].code; | |
1168 | ||
092ed096 | 1169 | if (BPF_CLASS(code) != BPF_JMP && BPF_CLASS(code) != BPF_JMP32) |
cc8b0b92 AS |
1170 | goto next; |
1171 | if (BPF_OP(code) == BPF_EXIT || BPF_OP(code) == BPF_CALL) | |
1172 | goto next; | |
1173 | off = i + insn[i].off + 1; | |
1174 | if (off < subprog_start || off >= subprog_end) { | |
1175 | verbose(env, "jump out of range from insn %d to %d\n", i, off); | |
1176 | return -EINVAL; | |
1177 | } | |
1178 | next: | |
1179 | if (i == subprog_end - 1) { | |
1180 | /* to avoid fall-through from one subprog into another | |
1181 | * the last insn of the subprog should be either exit | |
1182 | * or unconditional jump back | |
1183 | */ | |
1184 | if (code != (BPF_JMP | BPF_EXIT) && | |
1185 | code != (BPF_JMP | BPF_JA)) { | |
1186 | verbose(env, "last insn is not an exit or jmp\n"); | |
1187 | return -EINVAL; | |
1188 | } | |
1189 | subprog_start = subprog_end; | |
4cb3d99c JW |
1190 | cur_subprog++; |
1191 | if (cur_subprog < env->subprog_cnt) | |
9c8105bd | 1192 | subprog_end = subprog[cur_subprog + 1].start; |
cc8b0b92 AS |
1193 | } |
1194 | } | |
1195 | return 0; | |
1196 | } | |
1197 | ||
679c782d EC |
1198 | /* Parentage chain of this register (or stack slot) should take care of all |
1199 | * issues like callee-saved registers, stack slot allocation time, etc. | |
1200 | */ | |
f4d7e40a | 1201 | static int mark_reg_read(struct bpf_verifier_env *env, |
679c782d | 1202 | const struct bpf_reg_state *state, |
5327ed3d | 1203 | struct bpf_reg_state *parent, u8 flag) |
f4d7e40a AS |
1204 | { |
1205 | bool writes = parent == state->parent; /* Observe write marks */ | |
06ee7115 | 1206 | int cnt = 0; |
dc503a8a EC |
1207 | |
1208 | while (parent) { | |
1209 | /* if read wasn't screened by an earlier write ... */ | |
679c782d | 1210 | if (writes && state->live & REG_LIVE_WRITTEN) |
dc503a8a | 1211 | break; |
9242b5f5 AS |
1212 | if (parent->live & REG_LIVE_DONE) { |
1213 | verbose(env, "verifier BUG type %s var_off %lld off %d\n", | |
1214 | reg_type_str[parent->type], | |
1215 | parent->var_off.value, parent->off); | |
1216 | return -EFAULT; | |
1217 | } | |
5327ed3d JW |
1218 | /* The first condition is more likely to be true than the |
1219 | * second, checked it first. | |
1220 | */ | |
1221 | if ((parent->live & REG_LIVE_READ) == flag || | |
1222 | parent->live & REG_LIVE_READ64) | |
25af32da AS |
1223 | /* The parentage chain never changes and |
1224 | * this parent was already marked as LIVE_READ. | |
1225 | * There is no need to keep walking the chain again and | |
1226 | * keep re-marking all parents as LIVE_READ. | |
1227 | * This case happens when the same register is read | |
1228 | * multiple times without writes into it in-between. | |
5327ed3d JW |
1229 | * Also, if parent has the stronger REG_LIVE_READ64 set, |
1230 | * then no need to set the weak REG_LIVE_READ32. | |
25af32da AS |
1231 | */ |
1232 | break; | |
dc503a8a | 1233 | /* ... then we depend on parent's value */ |
5327ed3d JW |
1234 | parent->live |= flag; |
1235 | /* REG_LIVE_READ64 overrides REG_LIVE_READ32. */ | |
1236 | if (flag == REG_LIVE_READ64) | |
1237 | parent->live &= ~REG_LIVE_READ32; | |
dc503a8a EC |
1238 | state = parent; |
1239 | parent = state->parent; | |
f4d7e40a | 1240 | writes = true; |
06ee7115 | 1241 | cnt++; |
dc503a8a | 1242 | } |
06ee7115 AS |
1243 | |
1244 | if (env->longest_mark_read_walk < cnt) | |
1245 | env->longest_mark_read_walk = cnt; | |
f4d7e40a | 1246 | return 0; |
dc503a8a EC |
1247 | } |
1248 | ||
5327ed3d JW |
1249 | /* This function is supposed to be used by the following 32-bit optimization |
1250 | * code only. It returns TRUE if the source or destination register operates | |
1251 | * on 64-bit, otherwise return FALSE. | |
1252 | */ | |
1253 | static bool is_reg64(struct bpf_verifier_env *env, struct bpf_insn *insn, | |
1254 | u32 regno, struct bpf_reg_state *reg, enum reg_arg_type t) | |
1255 | { | |
1256 | u8 code, class, op; | |
1257 | ||
1258 | code = insn->code; | |
1259 | class = BPF_CLASS(code); | |
1260 | op = BPF_OP(code); | |
1261 | if (class == BPF_JMP) { | |
1262 | /* BPF_EXIT for "main" will reach here. Return TRUE | |
1263 | * conservatively. | |
1264 | */ | |
1265 | if (op == BPF_EXIT) | |
1266 | return true; | |
1267 | if (op == BPF_CALL) { | |
1268 | /* BPF to BPF call will reach here because of marking | |
1269 | * caller saved clobber with DST_OP_NO_MARK for which we | |
1270 | * don't care the register def because they are anyway | |
1271 | * marked as NOT_INIT already. | |
1272 | */ | |
1273 | if (insn->src_reg == BPF_PSEUDO_CALL) | |
1274 | return false; | |
1275 | /* Helper call will reach here because of arg type | |
1276 | * check, conservatively return TRUE. | |
1277 | */ | |
1278 | if (t == SRC_OP) | |
1279 | return true; | |
1280 | ||
1281 | return false; | |
1282 | } | |
1283 | } | |
1284 | ||
1285 | if (class == BPF_ALU64 || class == BPF_JMP || | |
1286 | /* BPF_END always use BPF_ALU class. */ | |
1287 | (class == BPF_ALU && op == BPF_END && insn->imm == 64)) | |
1288 | return true; | |
1289 | ||
1290 | if (class == BPF_ALU || class == BPF_JMP32) | |
1291 | return false; | |
1292 | ||
1293 | if (class == BPF_LDX) { | |
1294 | if (t != SRC_OP) | |
1295 | return BPF_SIZE(code) == BPF_DW; | |
1296 | /* LDX source must be ptr. */ | |
1297 | return true; | |
1298 | } | |
1299 | ||
1300 | if (class == BPF_STX) { | |
1301 | if (reg->type != SCALAR_VALUE) | |
1302 | return true; | |
1303 | return BPF_SIZE(code) == BPF_DW; | |
1304 | } | |
1305 | ||
1306 | if (class == BPF_LD) { | |
1307 | u8 mode = BPF_MODE(code); | |
1308 | ||
1309 | /* LD_IMM64 */ | |
1310 | if (mode == BPF_IMM) | |
1311 | return true; | |
1312 | ||
1313 | /* Both LD_IND and LD_ABS return 32-bit data. */ | |
1314 | if (t != SRC_OP) | |
1315 | return false; | |
1316 | ||
1317 | /* Implicit ctx ptr. */ | |
1318 | if (regno == BPF_REG_6) | |
1319 | return true; | |
1320 | ||
1321 | /* Explicit source could be any width. */ | |
1322 | return true; | |
1323 | } | |
1324 | ||
1325 | if (class == BPF_ST) | |
1326 | /* The only source register for BPF_ST is a ptr. */ | |
1327 | return true; | |
1328 | ||
1329 | /* Conservatively return true at default. */ | |
1330 | return true; | |
1331 | } | |
1332 | ||
b325fbca JW |
1333 | /* Return TRUE if INSN doesn't have explicit value define. */ |
1334 | static bool insn_no_def(struct bpf_insn *insn) | |
1335 | { | |
1336 | u8 class = BPF_CLASS(insn->code); | |
1337 | ||
1338 | return (class == BPF_JMP || class == BPF_JMP32 || | |
1339 | class == BPF_STX || class == BPF_ST); | |
1340 | } | |
1341 | ||
1342 | /* Return TRUE if INSN has defined any 32-bit value explicitly. */ | |
1343 | static bool insn_has_def32(struct bpf_verifier_env *env, struct bpf_insn *insn) | |
1344 | { | |
1345 | if (insn_no_def(insn)) | |
1346 | return false; | |
1347 | ||
1348 | return !is_reg64(env, insn, insn->dst_reg, NULL, DST_OP); | |
1349 | } | |
1350 | ||
5327ed3d JW |
1351 | static void mark_insn_zext(struct bpf_verifier_env *env, |
1352 | struct bpf_reg_state *reg) | |
1353 | { | |
1354 | s32 def_idx = reg->subreg_def; | |
1355 | ||
1356 | if (def_idx == DEF_NOT_SUBREG) | |
1357 | return; | |
1358 | ||
1359 | env->insn_aux_data[def_idx - 1].zext_dst = true; | |
1360 | /* The dst will be zero extended, so won't be sub-register anymore. */ | |
1361 | reg->subreg_def = DEF_NOT_SUBREG; | |
1362 | } | |
1363 | ||
dc503a8a | 1364 | static int check_reg_arg(struct bpf_verifier_env *env, u32 regno, |
17a52670 AS |
1365 | enum reg_arg_type t) |
1366 | { | |
f4d7e40a AS |
1367 | struct bpf_verifier_state *vstate = env->cur_state; |
1368 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
5327ed3d | 1369 | struct bpf_insn *insn = env->prog->insnsi + env->insn_idx; |
c342dc10 | 1370 | struct bpf_reg_state *reg, *regs = state->regs; |
5327ed3d | 1371 | bool rw64; |
dc503a8a | 1372 | |
17a52670 | 1373 | if (regno >= MAX_BPF_REG) { |
61bd5218 | 1374 | verbose(env, "R%d is invalid\n", regno); |
17a52670 AS |
1375 | return -EINVAL; |
1376 | } | |
1377 | ||
c342dc10 | 1378 | reg = ®s[regno]; |
5327ed3d | 1379 | rw64 = is_reg64(env, insn, regno, reg, t); |
17a52670 AS |
1380 | if (t == SRC_OP) { |
1381 | /* check whether register used as source operand can be read */ | |
c342dc10 | 1382 | if (reg->type == NOT_INIT) { |
61bd5218 | 1383 | verbose(env, "R%d !read_ok\n", regno); |
17a52670 AS |
1384 | return -EACCES; |
1385 | } | |
679c782d | 1386 | /* We don't need to worry about FP liveness because it's read-only */ |
c342dc10 JW |
1387 | if (regno == BPF_REG_FP) |
1388 | return 0; | |
1389 | ||
5327ed3d JW |
1390 | if (rw64) |
1391 | mark_insn_zext(env, reg); | |
1392 | ||
1393 | return mark_reg_read(env, reg, reg->parent, | |
1394 | rw64 ? REG_LIVE_READ64 : REG_LIVE_READ32); | |
17a52670 AS |
1395 | } else { |
1396 | /* check whether register used as dest operand can be written to */ | |
1397 | if (regno == BPF_REG_FP) { | |
61bd5218 | 1398 | verbose(env, "frame pointer is read only\n"); |
17a52670 AS |
1399 | return -EACCES; |
1400 | } | |
c342dc10 | 1401 | reg->live |= REG_LIVE_WRITTEN; |
5327ed3d | 1402 | reg->subreg_def = rw64 ? DEF_NOT_SUBREG : env->insn_idx + 1; |
17a52670 | 1403 | if (t == DST_OP) |
61bd5218 | 1404 | mark_reg_unknown(env, regs, regno); |
17a52670 AS |
1405 | } |
1406 | return 0; | |
1407 | } | |
1408 | ||
b5dc0163 AS |
1409 | /* for any branch, call, exit record the history of jmps in the given state */ |
1410 | static int push_jmp_history(struct bpf_verifier_env *env, | |
1411 | struct bpf_verifier_state *cur) | |
1412 | { | |
1413 | u32 cnt = cur->jmp_history_cnt; | |
1414 | struct bpf_idx_pair *p; | |
1415 | ||
1416 | cnt++; | |
1417 | p = krealloc(cur->jmp_history, cnt * sizeof(*p), GFP_USER); | |
1418 | if (!p) | |
1419 | return -ENOMEM; | |
1420 | p[cnt - 1].idx = env->insn_idx; | |
1421 | p[cnt - 1].prev_idx = env->prev_insn_idx; | |
1422 | cur->jmp_history = p; | |
1423 | cur->jmp_history_cnt = cnt; | |
1424 | return 0; | |
1425 | } | |
1426 | ||
1427 | /* Backtrack one insn at a time. If idx is not at the top of recorded | |
1428 | * history then previous instruction came from straight line execution. | |
1429 | */ | |
1430 | static int get_prev_insn_idx(struct bpf_verifier_state *st, int i, | |
1431 | u32 *history) | |
1432 | { | |
1433 | u32 cnt = *history; | |
1434 | ||
1435 | if (cnt && st->jmp_history[cnt - 1].idx == i) { | |
1436 | i = st->jmp_history[cnt - 1].prev_idx; | |
1437 | (*history)--; | |
1438 | } else { | |
1439 | i--; | |
1440 | } | |
1441 | return i; | |
1442 | } | |
1443 | ||
1444 | /* For given verifier state backtrack_insn() is called from the last insn to | |
1445 | * the first insn. Its purpose is to compute a bitmask of registers and | |
1446 | * stack slots that needs precision in the parent verifier state. | |
1447 | */ | |
1448 | static int backtrack_insn(struct bpf_verifier_env *env, int idx, | |
1449 | u32 *reg_mask, u64 *stack_mask) | |
1450 | { | |
1451 | const struct bpf_insn_cbs cbs = { | |
1452 | .cb_print = verbose, | |
1453 | .private_data = env, | |
1454 | }; | |
1455 | struct bpf_insn *insn = env->prog->insnsi + idx; | |
1456 | u8 class = BPF_CLASS(insn->code); | |
1457 | u8 opcode = BPF_OP(insn->code); | |
1458 | u8 mode = BPF_MODE(insn->code); | |
1459 | u32 dreg = 1u << insn->dst_reg; | |
1460 | u32 sreg = 1u << insn->src_reg; | |
1461 | u32 spi; | |
1462 | ||
1463 | if (insn->code == 0) | |
1464 | return 0; | |
1465 | if (env->log.level & BPF_LOG_LEVEL) { | |
1466 | verbose(env, "regs=%x stack=%llx before ", *reg_mask, *stack_mask); | |
1467 | verbose(env, "%d: ", idx); | |
1468 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); | |
1469 | } | |
1470 | ||
1471 | if (class == BPF_ALU || class == BPF_ALU64) { | |
1472 | if (!(*reg_mask & dreg)) | |
1473 | return 0; | |
1474 | if (opcode == BPF_MOV) { | |
1475 | if (BPF_SRC(insn->code) == BPF_X) { | |
1476 | /* dreg = sreg | |
1477 | * dreg needs precision after this insn | |
1478 | * sreg needs precision before this insn | |
1479 | */ | |
1480 | *reg_mask &= ~dreg; | |
1481 | *reg_mask |= sreg; | |
1482 | } else { | |
1483 | /* dreg = K | |
1484 | * dreg needs precision after this insn. | |
1485 | * Corresponding register is already marked | |
1486 | * as precise=true in this verifier state. | |
1487 | * No further markings in parent are necessary | |
1488 | */ | |
1489 | *reg_mask &= ~dreg; | |
1490 | } | |
1491 | } else { | |
1492 | if (BPF_SRC(insn->code) == BPF_X) { | |
1493 | /* dreg += sreg | |
1494 | * both dreg and sreg need precision | |
1495 | * before this insn | |
1496 | */ | |
1497 | *reg_mask |= sreg; | |
1498 | } /* else dreg += K | |
1499 | * dreg still needs precision before this insn | |
1500 | */ | |
1501 | } | |
1502 | } else if (class == BPF_LDX) { | |
1503 | if (!(*reg_mask & dreg)) | |
1504 | return 0; | |
1505 | *reg_mask &= ~dreg; | |
1506 | ||
1507 | /* scalars can only be spilled into stack w/o losing precision. | |
1508 | * Load from any other memory can be zero extended. | |
1509 | * The desire to keep that precision is already indicated | |
1510 | * by 'precise' mark in corresponding register of this state. | |
1511 | * No further tracking necessary. | |
1512 | */ | |
1513 | if (insn->src_reg != BPF_REG_FP) | |
1514 | return 0; | |
1515 | if (BPF_SIZE(insn->code) != BPF_DW) | |
1516 | return 0; | |
1517 | ||
1518 | /* dreg = *(u64 *)[fp - off] was a fill from the stack. | |
1519 | * that [fp - off] slot contains scalar that needs to be | |
1520 | * tracked with precision | |
1521 | */ | |
1522 | spi = (-insn->off - 1) / BPF_REG_SIZE; | |
1523 | if (spi >= 64) { | |
1524 | verbose(env, "BUG spi %d\n", spi); | |
1525 | WARN_ONCE(1, "verifier backtracking bug"); | |
1526 | return -EFAULT; | |
1527 | } | |
1528 | *stack_mask |= 1ull << spi; | |
b3b50f05 | 1529 | } else if (class == BPF_STX || class == BPF_ST) { |
b5dc0163 | 1530 | if (*reg_mask & dreg) |
b3b50f05 | 1531 | /* stx & st shouldn't be using _scalar_ dst_reg |
b5dc0163 AS |
1532 | * to access memory. It means backtracking |
1533 | * encountered a case of pointer subtraction. | |
1534 | */ | |
1535 | return -ENOTSUPP; | |
1536 | /* scalars can only be spilled into stack */ | |
1537 | if (insn->dst_reg != BPF_REG_FP) | |
1538 | return 0; | |
1539 | if (BPF_SIZE(insn->code) != BPF_DW) | |
1540 | return 0; | |
1541 | spi = (-insn->off - 1) / BPF_REG_SIZE; | |
1542 | if (spi >= 64) { | |
1543 | verbose(env, "BUG spi %d\n", spi); | |
1544 | WARN_ONCE(1, "verifier backtracking bug"); | |
1545 | return -EFAULT; | |
1546 | } | |
1547 | if (!(*stack_mask & (1ull << spi))) | |
1548 | return 0; | |
1549 | *stack_mask &= ~(1ull << spi); | |
b3b50f05 AN |
1550 | if (class == BPF_STX) |
1551 | *reg_mask |= sreg; | |
b5dc0163 AS |
1552 | } else if (class == BPF_JMP || class == BPF_JMP32) { |
1553 | if (opcode == BPF_CALL) { | |
1554 | if (insn->src_reg == BPF_PSEUDO_CALL) | |
1555 | return -ENOTSUPP; | |
1556 | /* regular helper call sets R0 */ | |
1557 | *reg_mask &= ~1; | |
1558 | if (*reg_mask & 0x3f) { | |
1559 | /* if backtracing was looking for registers R1-R5 | |
1560 | * they should have been found already. | |
1561 | */ | |
1562 | verbose(env, "BUG regs %x\n", *reg_mask); | |
1563 | WARN_ONCE(1, "verifier backtracking bug"); | |
1564 | return -EFAULT; | |
1565 | } | |
1566 | } else if (opcode == BPF_EXIT) { | |
1567 | return -ENOTSUPP; | |
1568 | } | |
1569 | } else if (class == BPF_LD) { | |
1570 | if (!(*reg_mask & dreg)) | |
1571 | return 0; | |
1572 | *reg_mask &= ~dreg; | |
1573 | /* It's ld_imm64 or ld_abs or ld_ind. | |
1574 | * For ld_imm64 no further tracking of precision | |
1575 | * into parent is necessary | |
1576 | */ | |
1577 | if (mode == BPF_IND || mode == BPF_ABS) | |
1578 | /* to be analyzed */ | |
1579 | return -ENOTSUPP; | |
b5dc0163 AS |
1580 | } |
1581 | return 0; | |
1582 | } | |
1583 | ||
1584 | /* the scalar precision tracking algorithm: | |
1585 | * . at the start all registers have precise=false. | |
1586 | * . scalar ranges are tracked as normal through alu and jmp insns. | |
1587 | * . once precise value of the scalar register is used in: | |
1588 | * . ptr + scalar alu | |
1589 | * . if (scalar cond K|scalar) | |
1590 | * . helper_call(.., scalar, ...) where ARG_CONST is expected | |
1591 | * backtrack through the verifier states and mark all registers and | |
1592 | * stack slots with spilled constants that these scalar regisers | |
1593 | * should be precise. | |
1594 | * . during state pruning two registers (or spilled stack slots) | |
1595 | * are equivalent if both are not precise. | |
1596 | * | |
1597 | * Note the verifier cannot simply walk register parentage chain, | |
1598 | * since many different registers and stack slots could have been | |
1599 | * used to compute single precise scalar. | |
1600 | * | |
1601 | * The approach of starting with precise=true for all registers and then | |
1602 | * backtrack to mark a register as not precise when the verifier detects | |
1603 | * that program doesn't care about specific value (e.g., when helper | |
1604 | * takes register as ARG_ANYTHING parameter) is not safe. | |
1605 | * | |
1606 | * It's ok to walk single parentage chain of the verifier states. | |
1607 | * It's possible that this backtracking will go all the way till 1st insn. | |
1608 | * All other branches will be explored for needing precision later. | |
1609 | * | |
1610 | * The backtracking needs to deal with cases like: | |
1611 | * 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) | |
1612 | * r9 -= r8 | |
1613 | * r5 = r9 | |
1614 | * if r5 > 0x79f goto pc+7 | |
1615 | * R5_w=inv(id=0,umax_value=1951,var_off=(0x0; 0x7ff)) | |
1616 | * r5 += 1 | |
1617 | * ... | |
1618 | * call bpf_perf_event_output#25 | |
1619 | * where .arg5_type = ARG_CONST_SIZE_OR_ZERO | |
1620 | * | |
1621 | * and this case: | |
1622 | * r6 = 1 | |
1623 | * call foo // uses callee's r6 inside to compute r0 | |
1624 | * r0 += r6 | |
1625 | * if r0 == 0 goto | |
1626 | * | |
1627 | * to track above reg_mask/stack_mask needs to be independent for each frame. | |
1628 | * | |
1629 | * Also if parent's curframe > frame where backtracking started, | |
1630 | * the verifier need to mark registers in both frames, otherwise callees | |
1631 | * may incorrectly prune callers. This is similar to | |
1632 | * commit 7640ead93924 ("bpf: verifier: make sure callees don't prune with caller differences") | |
1633 | * | |
1634 | * For now backtracking falls back into conservative marking. | |
1635 | */ | |
1636 | static void mark_all_scalars_precise(struct bpf_verifier_env *env, | |
1637 | struct bpf_verifier_state *st) | |
1638 | { | |
1639 | struct bpf_func_state *func; | |
1640 | struct bpf_reg_state *reg; | |
1641 | int i, j; | |
1642 | ||
1643 | /* big hammer: mark all scalars precise in this path. | |
1644 | * pop_stack may still get !precise scalars. | |
1645 | */ | |
1646 | for (; st; st = st->parent) | |
1647 | for (i = 0; i <= st->curframe; i++) { | |
1648 | func = st->frame[i]; | |
1649 | for (j = 0; j < BPF_REG_FP; j++) { | |
1650 | reg = &func->regs[j]; | |
1651 | if (reg->type != SCALAR_VALUE) | |
1652 | continue; | |
1653 | reg->precise = true; | |
1654 | } | |
1655 | for (j = 0; j < func->allocated_stack / BPF_REG_SIZE; j++) { | |
1656 | if (func->stack[j].slot_type[0] != STACK_SPILL) | |
1657 | continue; | |
1658 | reg = &func->stack[j].spilled_ptr; | |
1659 | if (reg->type != SCALAR_VALUE) | |
1660 | continue; | |
1661 | reg->precise = true; | |
1662 | } | |
1663 | } | |
1664 | } | |
1665 | ||
a3ce685d AS |
1666 | static int __mark_chain_precision(struct bpf_verifier_env *env, int regno, |
1667 | int spi) | |
b5dc0163 AS |
1668 | { |
1669 | struct bpf_verifier_state *st = env->cur_state; | |
1670 | int first_idx = st->first_insn_idx; | |
1671 | int last_idx = env->insn_idx; | |
1672 | struct bpf_func_state *func; | |
1673 | struct bpf_reg_state *reg; | |
a3ce685d AS |
1674 | u32 reg_mask = regno >= 0 ? 1u << regno : 0; |
1675 | u64 stack_mask = spi >= 0 ? 1ull << spi : 0; | |
b5dc0163 | 1676 | bool skip_first = true; |
a3ce685d | 1677 | bool new_marks = false; |
b5dc0163 AS |
1678 | int i, err; |
1679 | ||
1680 | if (!env->allow_ptr_leaks) | |
1681 | /* backtracking is root only for now */ | |
1682 | return 0; | |
1683 | ||
1684 | func = st->frame[st->curframe]; | |
a3ce685d AS |
1685 | if (regno >= 0) { |
1686 | reg = &func->regs[regno]; | |
1687 | if (reg->type != SCALAR_VALUE) { | |
1688 | WARN_ONCE(1, "backtracing misuse"); | |
1689 | return -EFAULT; | |
1690 | } | |
1691 | if (!reg->precise) | |
1692 | new_marks = true; | |
1693 | else | |
1694 | reg_mask = 0; | |
1695 | reg->precise = true; | |
b5dc0163 | 1696 | } |
b5dc0163 | 1697 | |
a3ce685d AS |
1698 | while (spi >= 0) { |
1699 | if (func->stack[spi].slot_type[0] != STACK_SPILL) { | |
1700 | stack_mask = 0; | |
1701 | break; | |
1702 | } | |
1703 | reg = &func->stack[spi].spilled_ptr; | |
1704 | if (reg->type != SCALAR_VALUE) { | |
1705 | stack_mask = 0; | |
1706 | break; | |
1707 | } | |
1708 | if (!reg->precise) | |
1709 | new_marks = true; | |
1710 | else | |
1711 | stack_mask = 0; | |
1712 | reg->precise = true; | |
1713 | break; | |
1714 | } | |
1715 | ||
1716 | if (!new_marks) | |
1717 | return 0; | |
1718 | if (!reg_mask && !stack_mask) | |
1719 | return 0; | |
b5dc0163 AS |
1720 | for (;;) { |
1721 | DECLARE_BITMAP(mask, 64); | |
b5dc0163 AS |
1722 | u32 history = st->jmp_history_cnt; |
1723 | ||
1724 | if (env->log.level & BPF_LOG_LEVEL) | |
1725 | verbose(env, "last_idx %d first_idx %d\n", last_idx, first_idx); | |
1726 | for (i = last_idx;;) { | |
1727 | if (skip_first) { | |
1728 | err = 0; | |
1729 | skip_first = false; | |
1730 | } else { | |
1731 | err = backtrack_insn(env, i, ®_mask, &stack_mask); | |
1732 | } | |
1733 | if (err == -ENOTSUPP) { | |
1734 | mark_all_scalars_precise(env, st); | |
1735 | return 0; | |
1736 | } else if (err) { | |
1737 | return err; | |
1738 | } | |
1739 | if (!reg_mask && !stack_mask) | |
1740 | /* Found assignment(s) into tracked register in this state. | |
1741 | * Since this state is already marked, just return. | |
1742 | * Nothing to be tracked further in the parent state. | |
1743 | */ | |
1744 | return 0; | |
1745 | if (i == first_idx) | |
1746 | break; | |
1747 | i = get_prev_insn_idx(st, i, &history); | |
1748 | if (i >= env->prog->len) { | |
1749 | /* This can happen if backtracking reached insn 0 | |
1750 | * and there are still reg_mask or stack_mask | |
1751 | * to backtrack. | |
1752 | * It means the backtracking missed the spot where | |
1753 | * particular register was initialized with a constant. | |
1754 | */ | |
1755 | verbose(env, "BUG backtracking idx %d\n", i); | |
1756 | WARN_ONCE(1, "verifier backtracking bug"); | |
1757 | return -EFAULT; | |
1758 | } | |
1759 | } | |
1760 | st = st->parent; | |
1761 | if (!st) | |
1762 | break; | |
1763 | ||
a3ce685d | 1764 | new_marks = false; |
b5dc0163 AS |
1765 | func = st->frame[st->curframe]; |
1766 | bitmap_from_u64(mask, reg_mask); | |
1767 | for_each_set_bit(i, mask, 32) { | |
1768 | reg = &func->regs[i]; | |
a3ce685d AS |
1769 | if (reg->type != SCALAR_VALUE) { |
1770 | reg_mask &= ~(1u << i); | |
b5dc0163 | 1771 | continue; |
a3ce685d | 1772 | } |
b5dc0163 AS |
1773 | if (!reg->precise) |
1774 | new_marks = true; | |
1775 | reg->precise = true; | |
1776 | } | |
1777 | ||
1778 | bitmap_from_u64(mask, stack_mask); | |
1779 | for_each_set_bit(i, mask, 64) { | |
1780 | if (i >= func->allocated_stack / BPF_REG_SIZE) { | |
2339cd6c AS |
1781 | /* the sequence of instructions: |
1782 | * 2: (bf) r3 = r10 | |
1783 | * 3: (7b) *(u64 *)(r3 -8) = r0 | |
1784 | * 4: (79) r4 = *(u64 *)(r10 -8) | |
1785 | * doesn't contain jmps. It's backtracked | |
1786 | * as a single block. | |
1787 | * During backtracking insn 3 is not recognized as | |
1788 | * stack access, so at the end of backtracking | |
1789 | * stack slot fp-8 is still marked in stack_mask. | |
1790 | * However the parent state may not have accessed | |
1791 | * fp-8 and it's "unallocated" stack space. | |
1792 | * In such case fallback to conservative. | |
b5dc0163 | 1793 | */ |
2339cd6c AS |
1794 | mark_all_scalars_precise(env, st); |
1795 | return 0; | |
b5dc0163 AS |
1796 | } |
1797 | ||
a3ce685d AS |
1798 | if (func->stack[i].slot_type[0] != STACK_SPILL) { |
1799 | stack_mask &= ~(1ull << i); | |
b5dc0163 | 1800 | continue; |
a3ce685d | 1801 | } |
b5dc0163 | 1802 | reg = &func->stack[i].spilled_ptr; |
a3ce685d AS |
1803 | if (reg->type != SCALAR_VALUE) { |
1804 | stack_mask &= ~(1ull << i); | |
b5dc0163 | 1805 | continue; |
a3ce685d | 1806 | } |
b5dc0163 AS |
1807 | if (!reg->precise) |
1808 | new_marks = true; | |
1809 | reg->precise = true; | |
1810 | } | |
1811 | if (env->log.level & BPF_LOG_LEVEL) { | |
1812 | print_verifier_state(env, func); | |
1813 | verbose(env, "parent %s regs=%x stack=%llx marks\n", | |
1814 | new_marks ? "didn't have" : "already had", | |
1815 | reg_mask, stack_mask); | |
1816 | } | |
1817 | ||
a3ce685d AS |
1818 | if (!reg_mask && !stack_mask) |
1819 | break; | |
b5dc0163 AS |
1820 | if (!new_marks) |
1821 | break; | |
1822 | ||
1823 | last_idx = st->last_insn_idx; | |
1824 | first_idx = st->first_insn_idx; | |
1825 | } | |
1826 | return 0; | |
1827 | } | |
1828 | ||
a3ce685d AS |
1829 | static int mark_chain_precision(struct bpf_verifier_env *env, int regno) |
1830 | { | |
1831 | return __mark_chain_precision(env, regno, -1); | |
1832 | } | |
1833 | ||
1834 | static int mark_chain_precision_stack(struct bpf_verifier_env *env, int spi) | |
1835 | { | |
1836 | return __mark_chain_precision(env, -1, spi); | |
1837 | } | |
b5dc0163 | 1838 | |
1be7f75d AS |
1839 | static bool is_spillable_regtype(enum bpf_reg_type type) |
1840 | { | |
1841 | switch (type) { | |
1842 | case PTR_TO_MAP_VALUE: | |
1843 | case PTR_TO_MAP_VALUE_OR_NULL: | |
1844 | case PTR_TO_STACK: | |
1845 | case PTR_TO_CTX: | |
969bf05e | 1846 | case PTR_TO_PACKET: |
de8f3a83 | 1847 | case PTR_TO_PACKET_META: |
969bf05e | 1848 | case PTR_TO_PACKET_END: |
d58e468b | 1849 | case PTR_TO_FLOW_KEYS: |
1be7f75d | 1850 | case CONST_PTR_TO_MAP: |
c64b7983 JS |
1851 | case PTR_TO_SOCKET: |
1852 | case PTR_TO_SOCKET_OR_NULL: | |
46f8bc92 MKL |
1853 | case PTR_TO_SOCK_COMMON: |
1854 | case PTR_TO_SOCK_COMMON_OR_NULL: | |
655a51e5 MKL |
1855 | case PTR_TO_TCP_SOCK: |
1856 | case PTR_TO_TCP_SOCK_OR_NULL: | |
fada7fdc | 1857 | case PTR_TO_XDP_SOCK: |
1be7f75d AS |
1858 | return true; |
1859 | default: | |
1860 | return false; | |
1861 | } | |
1862 | } | |
1863 | ||
cc2b14d5 AS |
1864 | /* Does this register contain a constant zero? */ |
1865 | static bool register_is_null(struct bpf_reg_state *reg) | |
1866 | { | |
1867 | return reg->type == SCALAR_VALUE && tnum_equals_const(reg->var_off, 0); | |
1868 | } | |
1869 | ||
f7cf25b2 AS |
1870 | static bool register_is_const(struct bpf_reg_state *reg) |
1871 | { | |
1872 | return reg->type == SCALAR_VALUE && tnum_is_const(reg->var_off); | |
1873 | } | |
1874 | ||
1875 | static void save_register_state(struct bpf_func_state *state, | |
1876 | int spi, struct bpf_reg_state *reg) | |
1877 | { | |
1878 | int i; | |
1879 | ||
1880 | state->stack[spi].spilled_ptr = *reg; | |
1881 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; | |
1882 | ||
1883 | for (i = 0; i < BPF_REG_SIZE; i++) | |
1884 | state->stack[spi].slot_type[i] = STACK_SPILL; | |
1885 | } | |
1886 | ||
17a52670 AS |
1887 | /* check_stack_read/write functions track spill/fill of registers, |
1888 | * stack boundary and alignment are checked in check_mem_access() | |
1889 | */ | |
61bd5218 | 1890 | static int check_stack_write(struct bpf_verifier_env *env, |
f4d7e40a | 1891 | struct bpf_func_state *state, /* func where register points to */ |
af86ca4e | 1892 | int off, int size, int value_regno, int insn_idx) |
17a52670 | 1893 | { |
f4d7e40a | 1894 | struct bpf_func_state *cur; /* state of the current function */ |
638f5b90 | 1895 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err; |
b5dc0163 | 1896 | u32 dst_reg = env->prog->insnsi[insn_idx].dst_reg; |
f7cf25b2 | 1897 | struct bpf_reg_state *reg = NULL; |
638f5b90 | 1898 | |
f4d7e40a | 1899 | err = realloc_func_state(state, round_up(slot + 1, BPF_REG_SIZE), |
fd978bf7 | 1900 | state->acquired_refs, true); |
638f5b90 AS |
1901 | if (err) |
1902 | return err; | |
9c399760 AS |
1903 | /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0, |
1904 | * so it's aligned access and [off, off + size) are within stack limits | |
1905 | */ | |
638f5b90 AS |
1906 | if (!env->allow_ptr_leaks && |
1907 | state->stack[spi].slot_type[0] == STACK_SPILL && | |
1908 | size != BPF_REG_SIZE) { | |
1909 | verbose(env, "attempt to corrupt spilled pointer on stack\n"); | |
1910 | return -EACCES; | |
1911 | } | |
17a52670 | 1912 | |
f4d7e40a | 1913 | cur = env->cur_state->frame[env->cur_state->curframe]; |
f7cf25b2 AS |
1914 | if (value_regno >= 0) |
1915 | reg = &cur->regs[value_regno]; | |
17a52670 | 1916 | |
f7cf25b2 AS |
1917 | if (reg && size == BPF_REG_SIZE && register_is_const(reg) && |
1918 | !register_is_null(reg) && env->allow_ptr_leaks) { | |
b5dc0163 AS |
1919 | if (dst_reg != BPF_REG_FP) { |
1920 | /* The backtracking logic can only recognize explicit | |
1921 | * stack slot address like [fp - 8]. Other spill of | |
1922 | * scalar via different register has to be conervative. | |
1923 | * Backtrack from here and mark all registers as precise | |
1924 | * that contributed into 'reg' being a constant. | |
1925 | */ | |
1926 | err = mark_chain_precision(env, value_regno); | |
1927 | if (err) | |
1928 | return err; | |
1929 | } | |
f7cf25b2 AS |
1930 | save_register_state(state, spi, reg); |
1931 | } else if (reg && is_spillable_regtype(reg->type)) { | |
17a52670 | 1932 | /* register containing pointer is being spilled into stack */ |
9c399760 | 1933 | if (size != BPF_REG_SIZE) { |
f7cf25b2 | 1934 | verbose_linfo(env, insn_idx, "; "); |
61bd5218 | 1935 | verbose(env, "invalid size of register spill\n"); |
17a52670 AS |
1936 | return -EACCES; |
1937 | } | |
1938 | ||
f7cf25b2 | 1939 | if (state != cur && reg->type == PTR_TO_STACK) { |
f4d7e40a AS |
1940 | verbose(env, "cannot spill pointers to stack into stack frame of the caller\n"); |
1941 | return -EINVAL; | |
1942 | } | |
1943 | ||
f7cf25b2 AS |
1944 | if (!env->allow_ptr_leaks) { |
1945 | bool sanitize = false; | |
17a52670 | 1946 | |
f7cf25b2 AS |
1947 | if (state->stack[spi].slot_type[0] == STACK_SPILL && |
1948 | register_is_const(&state->stack[spi].spilled_ptr)) | |
1949 | sanitize = true; | |
1950 | for (i = 0; i < BPF_REG_SIZE; i++) | |
1951 | if (state->stack[spi].slot_type[i] == STACK_MISC) { | |
1952 | sanitize = true; | |
1953 | break; | |
1954 | } | |
1955 | if (sanitize) { | |
af86ca4e AS |
1956 | int *poff = &env->insn_aux_data[insn_idx].sanitize_stack_off; |
1957 | int soff = (-spi - 1) * BPF_REG_SIZE; | |
1958 | ||
1959 | /* detected reuse of integer stack slot with a pointer | |
1960 | * which means either llvm is reusing stack slot or | |
1961 | * an attacker is trying to exploit CVE-2018-3639 | |
1962 | * (speculative store bypass) | |
1963 | * Have to sanitize that slot with preemptive | |
1964 | * store of zero. | |
1965 | */ | |
1966 | if (*poff && *poff != soff) { | |
1967 | /* disallow programs where single insn stores | |
1968 | * into two different stack slots, since verifier | |
1969 | * cannot sanitize them | |
1970 | */ | |
1971 | verbose(env, | |
1972 | "insn %d cannot access two stack slots fp%d and fp%d", | |
1973 | insn_idx, *poff, soff); | |
1974 | return -EINVAL; | |
1975 | } | |
1976 | *poff = soff; | |
1977 | } | |
af86ca4e | 1978 | } |
f7cf25b2 | 1979 | save_register_state(state, spi, reg); |
9c399760 | 1980 | } else { |
cc2b14d5 AS |
1981 | u8 type = STACK_MISC; |
1982 | ||
679c782d EC |
1983 | /* regular write of data into stack destroys any spilled ptr */ |
1984 | state->stack[spi].spilled_ptr.type = NOT_INIT; | |
0bae2d4d JW |
1985 | /* Mark slots as STACK_MISC if they belonged to spilled ptr. */ |
1986 | if (state->stack[spi].slot_type[0] == STACK_SPILL) | |
1987 | for (i = 0; i < BPF_REG_SIZE; i++) | |
1988 | state->stack[spi].slot_type[i] = STACK_MISC; | |
9c399760 | 1989 | |
cc2b14d5 AS |
1990 | /* only mark the slot as written if all 8 bytes were written |
1991 | * otherwise read propagation may incorrectly stop too soon | |
1992 | * when stack slots are partially written. | |
1993 | * This heuristic means that read propagation will be | |
1994 | * conservative, since it will add reg_live_read marks | |
1995 | * to stack slots all the way to first state when programs | |
1996 | * writes+reads less than 8 bytes | |
1997 | */ | |
1998 | if (size == BPF_REG_SIZE) | |
1999 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; | |
2000 | ||
2001 | /* when we zero initialize stack slots mark them as such */ | |
b5dc0163 AS |
2002 | if (reg && register_is_null(reg)) { |
2003 | /* backtracking doesn't work for STACK_ZERO yet. */ | |
2004 | err = mark_chain_precision(env, value_regno); | |
2005 | if (err) | |
2006 | return err; | |
cc2b14d5 | 2007 | type = STACK_ZERO; |
b5dc0163 | 2008 | } |
cc2b14d5 | 2009 | |
0bae2d4d | 2010 | /* Mark slots affected by this stack write. */ |
9c399760 | 2011 | for (i = 0; i < size; i++) |
638f5b90 | 2012 | state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] = |
cc2b14d5 | 2013 | type; |
17a52670 AS |
2014 | } |
2015 | return 0; | |
2016 | } | |
2017 | ||
61bd5218 | 2018 | static int check_stack_read(struct bpf_verifier_env *env, |
f4d7e40a AS |
2019 | struct bpf_func_state *reg_state /* func where register points to */, |
2020 | int off, int size, int value_regno) | |
17a52670 | 2021 | { |
f4d7e40a AS |
2022 | struct bpf_verifier_state *vstate = env->cur_state; |
2023 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
638f5b90 | 2024 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE; |
f7cf25b2 | 2025 | struct bpf_reg_state *reg; |
638f5b90 | 2026 | u8 *stype; |
17a52670 | 2027 | |
f4d7e40a | 2028 | if (reg_state->allocated_stack <= slot) { |
638f5b90 AS |
2029 | verbose(env, "invalid read from stack off %d+0 size %d\n", |
2030 | off, size); | |
2031 | return -EACCES; | |
2032 | } | |
f4d7e40a | 2033 | stype = reg_state->stack[spi].slot_type; |
f7cf25b2 | 2034 | reg = ®_state->stack[spi].spilled_ptr; |
17a52670 | 2035 | |
638f5b90 | 2036 | if (stype[0] == STACK_SPILL) { |
9c399760 | 2037 | if (size != BPF_REG_SIZE) { |
f7cf25b2 AS |
2038 | if (reg->type != SCALAR_VALUE) { |
2039 | verbose_linfo(env, env->insn_idx, "; "); | |
2040 | verbose(env, "invalid size of register fill\n"); | |
2041 | return -EACCES; | |
2042 | } | |
2043 | if (value_regno >= 0) { | |
2044 | mark_reg_unknown(env, state->regs, value_regno); | |
2045 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; | |
2046 | } | |
2047 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); | |
2048 | return 0; | |
17a52670 | 2049 | } |
9c399760 | 2050 | for (i = 1; i < BPF_REG_SIZE; i++) { |
638f5b90 | 2051 | if (stype[(slot - i) % BPF_REG_SIZE] != STACK_SPILL) { |
61bd5218 | 2052 | verbose(env, "corrupted spill memory\n"); |
17a52670 AS |
2053 | return -EACCES; |
2054 | } | |
2055 | } | |
2056 | ||
dc503a8a | 2057 | if (value_regno >= 0) { |
17a52670 | 2058 | /* restore register state from stack */ |
f7cf25b2 | 2059 | state->regs[value_regno] = *reg; |
2f18f62e AS |
2060 | /* mark reg as written since spilled pointer state likely |
2061 | * has its liveness marks cleared by is_state_visited() | |
2062 | * which resets stack/reg liveness for state transitions | |
2063 | */ | |
2064 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; | |
dc503a8a | 2065 | } |
f7cf25b2 | 2066 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
17a52670 | 2067 | } else { |
cc2b14d5 AS |
2068 | int zeros = 0; |
2069 | ||
17a52670 | 2070 | for (i = 0; i < size; i++) { |
cc2b14d5 AS |
2071 | if (stype[(slot - i) % BPF_REG_SIZE] == STACK_MISC) |
2072 | continue; | |
2073 | if (stype[(slot - i) % BPF_REG_SIZE] == STACK_ZERO) { | |
2074 | zeros++; | |
2075 | continue; | |
17a52670 | 2076 | } |
cc2b14d5 AS |
2077 | verbose(env, "invalid read from stack off %d+%d size %d\n", |
2078 | off, i, size); | |
2079 | return -EACCES; | |
2080 | } | |
f7cf25b2 | 2081 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
cc2b14d5 AS |
2082 | if (value_regno >= 0) { |
2083 | if (zeros == size) { | |
2084 | /* any size read into register is zero extended, | |
2085 | * so the whole register == const_zero | |
2086 | */ | |
2087 | __mark_reg_const_zero(&state->regs[value_regno]); | |
b5dc0163 AS |
2088 | /* backtracking doesn't support STACK_ZERO yet, |
2089 | * so mark it precise here, so that later | |
2090 | * backtracking can stop here. | |
2091 | * Backtracking may not need this if this register | |
2092 | * doesn't participate in pointer adjustment. | |
2093 | * Forward propagation of precise flag is not | |
2094 | * necessary either. This mark is only to stop | |
2095 | * backtracking. Any register that contributed | |
2096 | * to const 0 was marked precise before spill. | |
2097 | */ | |
2098 | state->regs[value_regno].precise = true; | |
cc2b14d5 AS |
2099 | } else { |
2100 | /* have read misc data from the stack */ | |
2101 | mark_reg_unknown(env, state->regs, value_regno); | |
2102 | } | |
2103 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; | |
17a52670 | 2104 | } |
17a52670 | 2105 | } |
f7cf25b2 | 2106 | return 0; |
17a52670 AS |
2107 | } |
2108 | ||
e4298d25 DB |
2109 | static int check_stack_access(struct bpf_verifier_env *env, |
2110 | const struct bpf_reg_state *reg, | |
2111 | int off, int size) | |
2112 | { | |
2113 | /* Stack accesses must be at a fixed offset, so that we | |
2114 | * can determine what type of data were returned. See | |
2115 | * check_stack_read(). | |
2116 | */ | |
2117 | if (!tnum_is_const(reg->var_off)) { | |
2118 | char tn_buf[48]; | |
2119 | ||
2120 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
1fbd20f8 | 2121 | verbose(env, "variable stack access var_off=%s off=%d size=%d\n", |
e4298d25 DB |
2122 | tn_buf, off, size); |
2123 | return -EACCES; | |
2124 | } | |
2125 | ||
2126 | if (off >= 0 || off < -MAX_BPF_STACK) { | |
2127 | verbose(env, "invalid stack off=%d size=%d\n", off, size); | |
2128 | return -EACCES; | |
2129 | } | |
2130 | ||
2131 | return 0; | |
2132 | } | |
2133 | ||
591fe988 DB |
2134 | static int check_map_access_type(struct bpf_verifier_env *env, u32 regno, |
2135 | int off, int size, enum bpf_access_type type) | |
2136 | { | |
2137 | struct bpf_reg_state *regs = cur_regs(env); | |
2138 | struct bpf_map *map = regs[regno].map_ptr; | |
2139 | u32 cap = bpf_map_flags_to_cap(map); | |
2140 | ||
2141 | if (type == BPF_WRITE && !(cap & BPF_MAP_CAN_WRITE)) { | |
2142 | verbose(env, "write into map forbidden, value_size=%d off=%d size=%d\n", | |
2143 | map->value_size, off, size); | |
2144 | return -EACCES; | |
2145 | } | |
2146 | ||
2147 | if (type == BPF_READ && !(cap & BPF_MAP_CAN_READ)) { | |
2148 | verbose(env, "read from map forbidden, value_size=%d off=%d size=%d\n", | |
2149 | map->value_size, off, size); | |
2150 | return -EACCES; | |
2151 | } | |
2152 | ||
2153 | return 0; | |
2154 | } | |
2155 | ||
17a52670 | 2156 | /* check read/write into map element returned by bpf_map_lookup_elem() */ |
f1174f77 | 2157 | static int __check_map_access(struct bpf_verifier_env *env, u32 regno, int off, |
9fd29c08 | 2158 | int size, bool zero_size_allowed) |
17a52670 | 2159 | { |
638f5b90 AS |
2160 | struct bpf_reg_state *regs = cur_regs(env); |
2161 | struct bpf_map *map = regs[regno].map_ptr; | |
17a52670 | 2162 | |
9fd29c08 YS |
2163 | if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) || |
2164 | off + size > map->value_size) { | |
61bd5218 | 2165 | verbose(env, "invalid access to map value, value_size=%d off=%d size=%d\n", |
17a52670 AS |
2166 | map->value_size, off, size); |
2167 | return -EACCES; | |
2168 | } | |
2169 | return 0; | |
2170 | } | |
2171 | ||
f1174f77 EC |
2172 | /* check read/write into a map element with possible variable offset */ |
2173 | static int check_map_access(struct bpf_verifier_env *env, u32 regno, | |
9fd29c08 | 2174 | int off, int size, bool zero_size_allowed) |
dbcfe5f7 | 2175 | { |
f4d7e40a AS |
2176 | struct bpf_verifier_state *vstate = env->cur_state; |
2177 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
dbcfe5f7 GB |
2178 | struct bpf_reg_state *reg = &state->regs[regno]; |
2179 | int err; | |
2180 | ||
f1174f77 EC |
2181 | /* We may have adjusted the register to this map value, so we |
2182 | * need to try adding each of min_value and max_value to off | |
2183 | * to make sure our theoretical access will be safe. | |
dbcfe5f7 | 2184 | */ |
06ee7115 | 2185 | if (env->log.level & BPF_LOG_LEVEL) |
61bd5218 | 2186 | print_verifier_state(env, state); |
b7137c4e | 2187 | |
dbcfe5f7 GB |
2188 | /* The minimum value is only important with signed |
2189 | * comparisons where we can't assume the floor of a | |
2190 | * value is 0. If we are using signed variables for our | |
2191 | * index'es we need to make sure that whatever we use | |
2192 | * will have a set floor within our range. | |
2193 | */ | |
b7137c4e DB |
2194 | if (reg->smin_value < 0 && |
2195 | (reg->smin_value == S64_MIN || | |
2196 | (off + reg->smin_value != (s64)(s32)(off + reg->smin_value)) || | |
2197 | reg->smin_value + off < 0)) { | |
61bd5218 | 2198 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
dbcfe5f7 GB |
2199 | regno); |
2200 | return -EACCES; | |
2201 | } | |
9fd29c08 YS |
2202 | err = __check_map_access(env, regno, reg->smin_value + off, size, |
2203 | zero_size_allowed); | |
dbcfe5f7 | 2204 | if (err) { |
61bd5218 JK |
2205 | verbose(env, "R%d min value is outside of the array range\n", |
2206 | regno); | |
dbcfe5f7 GB |
2207 | return err; |
2208 | } | |
2209 | ||
b03c9f9f EC |
2210 | /* If we haven't set a max value then we need to bail since we can't be |
2211 | * sure we won't do bad things. | |
2212 | * If reg->umax_value + off could overflow, treat that as unbounded too. | |
dbcfe5f7 | 2213 | */ |
b03c9f9f | 2214 | if (reg->umax_value >= BPF_MAX_VAR_OFF) { |
61bd5218 | 2215 | verbose(env, "R%d unbounded memory access, make sure to bounds check any array access into a map\n", |
dbcfe5f7 GB |
2216 | regno); |
2217 | return -EACCES; | |
2218 | } | |
9fd29c08 YS |
2219 | err = __check_map_access(env, regno, reg->umax_value + off, size, |
2220 | zero_size_allowed); | |
f1174f77 | 2221 | if (err) |
61bd5218 JK |
2222 | verbose(env, "R%d max value is outside of the array range\n", |
2223 | regno); | |
d83525ca AS |
2224 | |
2225 | if (map_value_has_spin_lock(reg->map_ptr)) { | |
2226 | u32 lock = reg->map_ptr->spin_lock_off; | |
2227 | ||
2228 | /* if any part of struct bpf_spin_lock can be touched by | |
2229 | * load/store reject this program. | |
2230 | * To check that [x1, x2) overlaps with [y1, y2) | |
2231 | * it is sufficient to check x1 < y2 && y1 < x2. | |
2232 | */ | |
2233 | if (reg->smin_value + off < lock + sizeof(struct bpf_spin_lock) && | |
2234 | lock < reg->umax_value + off + size) { | |
2235 | verbose(env, "bpf_spin_lock cannot be accessed directly by load/store\n"); | |
2236 | return -EACCES; | |
2237 | } | |
2238 | } | |
f1174f77 | 2239 | return err; |
dbcfe5f7 GB |
2240 | } |
2241 | ||
969bf05e AS |
2242 | #define MAX_PACKET_OFF 0xffff |
2243 | ||
58e2af8b | 2244 | static bool may_access_direct_pkt_data(struct bpf_verifier_env *env, |
3a0af8fd TG |
2245 | const struct bpf_call_arg_meta *meta, |
2246 | enum bpf_access_type t) | |
4acf6c0b | 2247 | { |
36bbef52 | 2248 | switch (env->prog->type) { |
5d66fa7d | 2249 | /* Program types only with direct read access go here! */ |
3a0af8fd TG |
2250 | case BPF_PROG_TYPE_LWT_IN: |
2251 | case BPF_PROG_TYPE_LWT_OUT: | |
004d4b27 | 2252 | case BPF_PROG_TYPE_LWT_SEG6LOCAL: |
2dbb9b9e | 2253 | case BPF_PROG_TYPE_SK_REUSEPORT: |
5d66fa7d | 2254 | case BPF_PROG_TYPE_FLOW_DISSECTOR: |
d5563d36 | 2255 | case BPF_PROG_TYPE_CGROUP_SKB: |
3a0af8fd TG |
2256 | if (t == BPF_WRITE) |
2257 | return false; | |
7e57fbb2 | 2258 | /* fallthrough */ |
5d66fa7d DB |
2259 | |
2260 | /* Program types with direct read + write access go here! */ | |
36bbef52 DB |
2261 | case BPF_PROG_TYPE_SCHED_CLS: |
2262 | case BPF_PROG_TYPE_SCHED_ACT: | |
4acf6c0b | 2263 | case BPF_PROG_TYPE_XDP: |
3a0af8fd | 2264 | case BPF_PROG_TYPE_LWT_XMIT: |
8a31db56 | 2265 | case BPF_PROG_TYPE_SK_SKB: |
4f738adb | 2266 | case BPF_PROG_TYPE_SK_MSG: |
36bbef52 DB |
2267 | if (meta) |
2268 | return meta->pkt_access; | |
2269 | ||
2270 | env->seen_direct_write = true; | |
4acf6c0b | 2271 | return true; |
0d01da6a SF |
2272 | |
2273 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: | |
2274 | if (t == BPF_WRITE) | |
2275 | env->seen_direct_write = true; | |
2276 | ||
2277 | return true; | |
2278 | ||
4acf6c0b BB |
2279 | default: |
2280 | return false; | |
2281 | } | |
2282 | } | |
2283 | ||
f1174f77 | 2284 | static int __check_packet_access(struct bpf_verifier_env *env, u32 regno, |
9fd29c08 | 2285 | int off, int size, bool zero_size_allowed) |
969bf05e | 2286 | { |
638f5b90 | 2287 | struct bpf_reg_state *regs = cur_regs(env); |
58e2af8b | 2288 | struct bpf_reg_state *reg = ®s[regno]; |
969bf05e | 2289 | |
9fd29c08 YS |
2290 | if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) || |
2291 | (u64)off + size > reg->range) { | |
61bd5218 | 2292 | verbose(env, "invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n", |
d91b28ed | 2293 | off, size, regno, reg->id, reg->off, reg->range); |
969bf05e AS |
2294 | return -EACCES; |
2295 | } | |
2296 | return 0; | |
2297 | } | |
2298 | ||
f1174f77 | 2299 | static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off, |
9fd29c08 | 2300 | int size, bool zero_size_allowed) |
f1174f77 | 2301 | { |
638f5b90 | 2302 | struct bpf_reg_state *regs = cur_regs(env); |
f1174f77 EC |
2303 | struct bpf_reg_state *reg = ®s[regno]; |
2304 | int err; | |
2305 | ||
2306 | /* We may have added a variable offset to the packet pointer; but any | |
2307 | * reg->range we have comes after that. We are only checking the fixed | |
2308 | * offset. | |
2309 | */ | |
2310 | ||
2311 | /* We don't allow negative numbers, because we aren't tracking enough | |
2312 | * detail to prove they're safe. | |
2313 | */ | |
b03c9f9f | 2314 | if (reg->smin_value < 0) { |
61bd5218 | 2315 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
f1174f77 EC |
2316 | regno); |
2317 | return -EACCES; | |
2318 | } | |
9fd29c08 | 2319 | err = __check_packet_access(env, regno, off, size, zero_size_allowed); |
f1174f77 | 2320 | if (err) { |
61bd5218 | 2321 | verbose(env, "R%d offset is outside of the packet\n", regno); |
f1174f77 EC |
2322 | return err; |
2323 | } | |
e647815a JW |
2324 | |
2325 | /* __check_packet_access has made sure "off + size - 1" is within u16. | |
2326 | * reg->umax_value can't be bigger than MAX_PACKET_OFF which is 0xffff, | |
2327 | * otherwise find_good_pkt_pointers would have refused to set range info | |
2328 | * that __check_packet_access would have rejected this pkt access. | |
2329 | * Therefore, "off + reg->umax_value + size - 1" won't overflow u32. | |
2330 | */ | |
2331 | env->prog->aux->max_pkt_offset = | |
2332 | max_t(u32, env->prog->aux->max_pkt_offset, | |
2333 | off + reg->umax_value + size - 1); | |
2334 | ||
f1174f77 EC |
2335 | return err; |
2336 | } | |
2337 | ||
2338 | /* check access to 'struct bpf_context' fields. Supports fixed offsets only */ | |
31fd8581 | 2339 | static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off, int size, |
19de99f7 | 2340 | enum bpf_access_type t, enum bpf_reg_type *reg_type) |
17a52670 | 2341 | { |
f96da094 DB |
2342 | struct bpf_insn_access_aux info = { |
2343 | .reg_type = *reg_type, | |
2344 | }; | |
31fd8581 | 2345 | |
4f9218aa | 2346 | if (env->ops->is_valid_access && |
5e43f899 | 2347 | env->ops->is_valid_access(off, size, t, env->prog, &info)) { |
f96da094 DB |
2348 | /* A non zero info.ctx_field_size indicates that this field is a |
2349 | * candidate for later verifier transformation to load the whole | |
2350 | * field and then apply a mask when accessed with a narrower | |
2351 | * access than actual ctx access size. A zero info.ctx_field_size | |
2352 | * will only allow for whole field access and rejects any other | |
2353 | * type of narrower access. | |
31fd8581 | 2354 | */ |
23994631 | 2355 | *reg_type = info.reg_type; |
31fd8581 | 2356 | |
4f9218aa | 2357 | env->insn_aux_data[insn_idx].ctx_field_size = info.ctx_field_size; |
32bbe007 AS |
2358 | /* remember the offset of last byte accessed in ctx */ |
2359 | if (env->prog->aux->max_ctx_offset < off + size) | |
2360 | env->prog->aux->max_ctx_offset = off + size; | |
17a52670 | 2361 | return 0; |
32bbe007 | 2362 | } |
17a52670 | 2363 | |
61bd5218 | 2364 | verbose(env, "invalid bpf_context access off=%d size=%d\n", off, size); |
17a52670 AS |
2365 | return -EACCES; |
2366 | } | |
2367 | ||
d58e468b PP |
2368 | static int check_flow_keys_access(struct bpf_verifier_env *env, int off, |
2369 | int size) | |
2370 | { | |
2371 | if (size < 0 || off < 0 || | |
2372 | (u64)off + size > sizeof(struct bpf_flow_keys)) { | |
2373 | verbose(env, "invalid access to flow keys off=%d size=%d\n", | |
2374 | off, size); | |
2375 | return -EACCES; | |
2376 | } | |
2377 | return 0; | |
2378 | } | |
2379 | ||
5f456649 MKL |
2380 | static int check_sock_access(struct bpf_verifier_env *env, int insn_idx, |
2381 | u32 regno, int off, int size, | |
2382 | enum bpf_access_type t) | |
c64b7983 JS |
2383 | { |
2384 | struct bpf_reg_state *regs = cur_regs(env); | |
2385 | struct bpf_reg_state *reg = ®s[regno]; | |
5f456649 | 2386 | struct bpf_insn_access_aux info = {}; |
46f8bc92 | 2387 | bool valid; |
c64b7983 JS |
2388 | |
2389 | if (reg->smin_value < 0) { | |
2390 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", | |
2391 | regno); | |
2392 | return -EACCES; | |
2393 | } | |
2394 | ||
46f8bc92 MKL |
2395 | switch (reg->type) { |
2396 | case PTR_TO_SOCK_COMMON: | |
2397 | valid = bpf_sock_common_is_valid_access(off, size, t, &info); | |
2398 | break; | |
2399 | case PTR_TO_SOCKET: | |
2400 | valid = bpf_sock_is_valid_access(off, size, t, &info); | |
2401 | break; | |
655a51e5 MKL |
2402 | case PTR_TO_TCP_SOCK: |
2403 | valid = bpf_tcp_sock_is_valid_access(off, size, t, &info); | |
2404 | break; | |
fada7fdc JL |
2405 | case PTR_TO_XDP_SOCK: |
2406 | valid = bpf_xdp_sock_is_valid_access(off, size, t, &info); | |
2407 | break; | |
46f8bc92 MKL |
2408 | default: |
2409 | valid = false; | |
c64b7983 JS |
2410 | } |
2411 | ||
5f456649 | 2412 | |
46f8bc92 MKL |
2413 | if (valid) { |
2414 | env->insn_aux_data[insn_idx].ctx_field_size = | |
2415 | info.ctx_field_size; | |
2416 | return 0; | |
2417 | } | |
2418 | ||
2419 | verbose(env, "R%d invalid %s access off=%d size=%d\n", | |
2420 | regno, reg_type_str[reg->type], off, size); | |
2421 | ||
2422 | return -EACCES; | |
c64b7983 JS |
2423 | } |
2424 | ||
4cabc5b1 DB |
2425 | static bool __is_pointer_value(bool allow_ptr_leaks, |
2426 | const struct bpf_reg_state *reg) | |
1be7f75d | 2427 | { |
4cabc5b1 | 2428 | if (allow_ptr_leaks) |
1be7f75d AS |
2429 | return false; |
2430 | ||
f1174f77 | 2431 | return reg->type != SCALAR_VALUE; |
1be7f75d AS |
2432 | } |
2433 | ||
2a159c6f DB |
2434 | static struct bpf_reg_state *reg_state(struct bpf_verifier_env *env, int regno) |
2435 | { | |
2436 | return cur_regs(env) + regno; | |
2437 | } | |
2438 | ||
4cabc5b1 DB |
2439 | static bool is_pointer_value(struct bpf_verifier_env *env, int regno) |
2440 | { | |
2a159c6f | 2441 | return __is_pointer_value(env->allow_ptr_leaks, reg_state(env, regno)); |
4cabc5b1 DB |
2442 | } |
2443 | ||
f37a8cb8 DB |
2444 | static bool is_ctx_reg(struct bpf_verifier_env *env, int regno) |
2445 | { | |
2a159c6f | 2446 | const struct bpf_reg_state *reg = reg_state(env, regno); |
f37a8cb8 | 2447 | |
46f8bc92 MKL |
2448 | return reg->type == PTR_TO_CTX; |
2449 | } | |
2450 | ||
2451 | static bool is_sk_reg(struct bpf_verifier_env *env, int regno) | |
2452 | { | |
2453 | const struct bpf_reg_state *reg = reg_state(env, regno); | |
2454 | ||
2455 | return type_is_sk_pointer(reg->type); | |
f37a8cb8 DB |
2456 | } |
2457 | ||
ca369602 DB |
2458 | static bool is_pkt_reg(struct bpf_verifier_env *env, int regno) |
2459 | { | |
2a159c6f | 2460 | const struct bpf_reg_state *reg = reg_state(env, regno); |
ca369602 DB |
2461 | |
2462 | return type_is_pkt_pointer(reg->type); | |
2463 | } | |
2464 | ||
4b5defde DB |
2465 | static bool is_flow_key_reg(struct bpf_verifier_env *env, int regno) |
2466 | { | |
2467 | const struct bpf_reg_state *reg = reg_state(env, regno); | |
2468 | ||
2469 | /* Separate to is_ctx_reg() since we still want to allow BPF_ST here. */ | |
2470 | return reg->type == PTR_TO_FLOW_KEYS; | |
2471 | } | |
2472 | ||
61bd5218 JK |
2473 | static int check_pkt_ptr_alignment(struct bpf_verifier_env *env, |
2474 | const struct bpf_reg_state *reg, | |
d1174416 | 2475 | int off, int size, bool strict) |
969bf05e | 2476 | { |
f1174f77 | 2477 | struct tnum reg_off; |
e07b98d9 | 2478 | int ip_align; |
d1174416 DM |
2479 | |
2480 | /* Byte size accesses are always allowed. */ | |
2481 | if (!strict || size == 1) | |
2482 | return 0; | |
2483 | ||
e4eda884 DM |
2484 | /* For platforms that do not have a Kconfig enabling |
2485 | * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of | |
2486 | * NET_IP_ALIGN is universally set to '2'. And on platforms | |
2487 | * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get | |
2488 | * to this code only in strict mode where we want to emulate | |
2489 | * the NET_IP_ALIGN==2 checking. Therefore use an | |
2490 | * unconditional IP align value of '2'. | |
e07b98d9 | 2491 | */ |
e4eda884 | 2492 | ip_align = 2; |
f1174f77 EC |
2493 | |
2494 | reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off)); | |
2495 | if (!tnum_is_aligned(reg_off, size)) { | |
2496 | char tn_buf[48]; | |
2497 | ||
2498 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
61bd5218 JK |
2499 | verbose(env, |
2500 | "misaligned packet access off %d+%s+%d+%d size %d\n", | |
f1174f77 | 2501 | ip_align, tn_buf, reg->off, off, size); |
969bf05e AS |
2502 | return -EACCES; |
2503 | } | |
79adffcd | 2504 | |
969bf05e AS |
2505 | return 0; |
2506 | } | |
2507 | ||
61bd5218 JK |
2508 | static int check_generic_ptr_alignment(struct bpf_verifier_env *env, |
2509 | const struct bpf_reg_state *reg, | |
f1174f77 EC |
2510 | const char *pointer_desc, |
2511 | int off, int size, bool strict) | |
79adffcd | 2512 | { |
f1174f77 EC |
2513 | struct tnum reg_off; |
2514 | ||
2515 | /* Byte size accesses are always allowed. */ | |
2516 | if (!strict || size == 1) | |
2517 | return 0; | |
2518 | ||
2519 | reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off)); | |
2520 | if (!tnum_is_aligned(reg_off, size)) { | |
2521 | char tn_buf[48]; | |
2522 | ||
2523 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
61bd5218 | 2524 | verbose(env, "misaligned %saccess off %s+%d+%d size %d\n", |
f1174f77 | 2525 | pointer_desc, tn_buf, reg->off, off, size); |
79adffcd DB |
2526 | return -EACCES; |
2527 | } | |
2528 | ||
969bf05e AS |
2529 | return 0; |
2530 | } | |
2531 | ||
e07b98d9 | 2532 | static int check_ptr_alignment(struct bpf_verifier_env *env, |
ca369602 DB |
2533 | const struct bpf_reg_state *reg, int off, |
2534 | int size, bool strict_alignment_once) | |
79adffcd | 2535 | { |
ca369602 | 2536 | bool strict = env->strict_alignment || strict_alignment_once; |
f1174f77 | 2537 | const char *pointer_desc = ""; |
d1174416 | 2538 | |
79adffcd DB |
2539 | switch (reg->type) { |
2540 | case PTR_TO_PACKET: | |
de8f3a83 DB |
2541 | case PTR_TO_PACKET_META: |
2542 | /* Special case, because of NET_IP_ALIGN. Given metadata sits | |
2543 | * right in front, treat it the very same way. | |
2544 | */ | |
61bd5218 | 2545 | return check_pkt_ptr_alignment(env, reg, off, size, strict); |
d58e468b PP |
2546 | case PTR_TO_FLOW_KEYS: |
2547 | pointer_desc = "flow keys "; | |
2548 | break; | |
f1174f77 EC |
2549 | case PTR_TO_MAP_VALUE: |
2550 | pointer_desc = "value "; | |
2551 | break; | |
2552 | case PTR_TO_CTX: | |
2553 | pointer_desc = "context "; | |
2554 | break; | |
2555 | case PTR_TO_STACK: | |
2556 | pointer_desc = "stack "; | |
a5ec6ae1 JH |
2557 | /* The stack spill tracking logic in check_stack_write() |
2558 | * and check_stack_read() relies on stack accesses being | |
2559 | * aligned. | |
2560 | */ | |
2561 | strict = true; | |
f1174f77 | 2562 | break; |
c64b7983 JS |
2563 | case PTR_TO_SOCKET: |
2564 | pointer_desc = "sock "; | |
2565 | break; | |
46f8bc92 MKL |
2566 | case PTR_TO_SOCK_COMMON: |
2567 | pointer_desc = "sock_common "; | |
2568 | break; | |
655a51e5 MKL |
2569 | case PTR_TO_TCP_SOCK: |
2570 | pointer_desc = "tcp_sock "; | |
2571 | break; | |
fada7fdc JL |
2572 | case PTR_TO_XDP_SOCK: |
2573 | pointer_desc = "xdp_sock "; | |
2574 | break; | |
79adffcd | 2575 | default: |
f1174f77 | 2576 | break; |
79adffcd | 2577 | } |
61bd5218 JK |
2578 | return check_generic_ptr_alignment(env, reg, pointer_desc, off, size, |
2579 | strict); | |
79adffcd DB |
2580 | } |
2581 | ||
f4d7e40a AS |
2582 | static int update_stack_depth(struct bpf_verifier_env *env, |
2583 | const struct bpf_func_state *func, | |
2584 | int off) | |
2585 | { | |
9c8105bd | 2586 | u16 stack = env->subprog_info[func->subprogno].stack_depth; |
f4d7e40a AS |
2587 | |
2588 | if (stack >= -off) | |
2589 | return 0; | |
2590 | ||
2591 | /* update known max for given subprogram */ | |
9c8105bd | 2592 | env->subprog_info[func->subprogno].stack_depth = -off; |
70a87ffe AS |
2593 | return 0; |
2594 | } | |
f4d7e40a | 2595 | |
70a87ffe AS |
2596 | /* starting from main bpf function walk all instructions of the function |
2597 | * and recursively walk all callees that given function can call. | |
2598 | * Ignore jump and exit insns. | |
2599 | * Since recursion is prevented by check_cfg() this algorithm | |
2600 | * only needs a local stack of MAX_CALL_FRAMES to remember callsites | |
2601 | */ | |
2602 | static int check_max_stack_depth(struct bpf_verifier_env *env) | |
2603 | { | |
9c8105bd JW |
2604 | int depth = 0, frame = 0, idx = 0, i = 0, subprog_end; |
2605 | struct bpf_subprog_info *subprog = env->subprog_info; | |
70a87ffe | 2606 | struct bpf_insn *insn = env->prog->insnsi; |
70a87ffe AS |
2607 | int ret_insn[MAX_CALL_FRAMES]; |
2608 | int ret_prog[MAX_CALL_FRAMES]; | |
f4d7e40a | 2609 | |
70a87ffe AS |
2610 | process_func: |
2611 | /* round up to 32-bytes, since this is granularity | |
2612 | * of interpreter stack size | |
2613 | */ | |
9c8105bd | 2614 | depth += round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
70a87ffe | 2615 | if (depth > MAX_BPF_STACK) { |
f4d7e40a | 2616 | verbose(env, "combined stack size of %d calls is %d. Too large\n", |
70a87ffe | 2617 | frame + 1, depth); |
f4d7e40a AS |
2618 | return -EACCES; |
2619 | } | |
70a87ffe | 2620 | continue_func: |
4cb3d99c | 2621 | subprog_end = subprog[idx + 1].start; |
70a87ffe AS |
2622 | for (; i < subprog_end; i++) { |
2623 | if (insn[i].code != (BPF_JMP | BPF_CALL)) | |
2624 | continue; | |
2625 | if (insn[i].src_reg != BPF_PSEUDO_CALL) | |
2626 | continue; | |
2627 | /* remember insn and function to return to */ | |
2628 | ret_insn[frame] = i + 1; | |
9c8105bd | 2629 | ret_prog[frame] = idx; |
70a87ffe AS |
2630 | |
2631 | /* find the callee */ | |
2632 | i = i + insn[i].imm + 1; | |
9c8105bd JW |
2633 | idx = find_subprog(env, i); |
2634 | if (idx < 0) { | |
70a87ffe AS |
2635 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
2636 | i); | |
2637 | return -EFAULT; | |
2638 | } | |
70a87ffe AS |
2639 | frame++; |
2640 | if (frame >= MAX_CALL_FRAMES) { | |
927cb781 PC |
2641 | verbose(env, "the call stack of %d frames is too deep !\n", |
2642 | frame); | |
2643 | return -E2BIG; | |
70a87ffe AS |
2644 | } |
2645 | goto process_func; | |
2646 | } | |
2647 | /* end of for() loop means the last insn of the 'subprog' | |
2648 | * was reached. Doesn't matter whether it was JA or EXIT | |
2649 | */ | |
2650 | if (frame == 0) | |
2651 | return 0; | |
9c8105bd | 2652 | depth -= round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
70a87ffe AS |
2653 | frame--; |
2654 | i = ret_insn[frame]; | |
9c8105bd | 2655 | idx = ret_prog[frame]; |
70a87ffe | 2656 | goto continue_func; |
f4d7e40a AS |
2657 | } |
2658 | ||
19d28fbd | 2659 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
1ea47e01 AS |
2660 | static int get_callee_stack_depth(struct bpf_verifier_env *env, |
2661 | const struct bpf_insn *insn, int idx) | |
2662 | { | |
2663 | int start = idx + insn->imm + 1, subprog; | |
2664 | ||
2665 | subprog = find_subprog(env, start); | |
2666 | if (subprog < 0) { | |
2667 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", | |
2668 | start); | |
2669 | return -EFAULT; | |
2670 | } | |
9c8105bd | 2671 | return env->subprog_info[subprog].stack_depth; |
1ea47e01 | 2672 | } |
19d28fbd | 2673 | #endif |
1ea47e01 | 2674 | |
58990d1f DB |
2675 | static int check_ctx_reg(struct bpf_verifier_env *env, |
2676 | const struct bpf_reg_state *reg, int regno) | |
2677 | { | |
2678 | /* Access to ctx or passing it to a helper is only allowed in | |
2679 | * its original, unmodified form. | |
2680 | */ | |
2681 | ||
2682 | if (reg->off) { | |
2683 | verbose(env, "dereference of modified ctx ptr R%d off=%d disallowed\n", | |
2684 | regno, reg->off); | |
2685 | return -EACCES; | |
2686 | } | |
2687 | ||
2688 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
2689 | char tn_buf[48]; | |
2690 | ||
2691 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
2692 | verbose(env, "variable ctx access var_off=%s disallowed\n", tn_buf); | |
2693 | return -EACCES; | |
2694 | } | |
2695 | ||
2696 | return 0; | |
2697 | } | |
2698 | ||
9df1c28b MM |
2699 | static int check_tp_buffer_access(struct bpf_verifier_env *env, |
2700 | const struct bpf_reg_state *reg, | |
2701 | int regno, int off, int size) | |
2702 | { | |
2703 | if (off < 0) { | |
2704 | verbose(env, | |
2705 | "R%d invalid tracepoint buffer access: off=%d, size=%d", | |
2706 | regno, off, size); | |
2707 | return -EACCES; | |
2708 | } | |
2709 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
2710 | char tn_buf[48]; | |
2711 | ||
2712 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
2713 | verbose(env, | |
2714 | "R%d invalid variable buffer offset: off=%d, var_off=%s", | |
2715 | regno, off, tn_buf); | |
2716 | return -EACCES; | |
2717 | } | |
2718 | if (off + size > env->prog->aux->max_tp_access) | |
2719 | env->prog->aux->max_tp_access = off + size; | |
2720 | ||
2721 | return 0; | |
2722 | } | |
2723 | ||
2724 | ||
0c17d1d2 JH |
2725 | /* truncate register to smaller size (in bytes) |
2726 | * must be called with size < BPF_REG_SIZE | |
2727 | */ | |
2728 | static void coerce_reg_to_size(struct bpf_reg_state *reg, int size) | |
2729 | { | |
2730 | u64 mask; | |
2731 | ||
2732 | /* clear high bits in bit representation */ | |
2733 | reg->var_off = tnum_cast(reg->var_off, size); | |
2734 | ||
2735 | /* fix arithmetic bounds */ | |
2736 | mask = ((u64)1 << (size * 8)) - 1; | |
2737 | if ((reg->umin_value & ~mask) == (reg->umax_value & ~mask)) { | |
2738 | reg->umin_value &= mask; | |
2739 | reg->umax_value &= mask; | |
2740 | } else { | |
2741 | reg->umin_value = 0; | |
2742 | reg->umax_value = mask; | |
2743 | } | |
2744 | reg->smin_value = reg->umin_value; | |
2745 | reg->smax_value = reg->umax_value; | |
2746 | } | |
2747 | ||
a23740ec AN |
2748 | static bool bpf_map_is_rdonly(const struct bpf_map *map) |
2749 | { | |
2750 | return (map->map_flags & BPF_F_RDONLY_PROG) && map->frozen; | |
2751 | } | |
2752 | ||
2753 | static int bpf_map_direct_read(struct bpf_map *map, int off, int size, u64 *val) | |
2754 | { | |
2755 | void *ptr; | |
2756 | u64 addr; | |
2757 | int err; | |
2758 | ||
2759 | err = map->ops->map_direct_value_addr(map, &addr, off); | |
2760 | if (err) | |
2761 | return err; | |
2dedd7d2 | 2762 | ptr = (void *)(long)addr + off; |
a23740ec AN |
2763 | |
2764 | switch (size) { | |
2765 | case sizeof(u8): | |
2766 | *val = (u64)*(u8 *)ptr; | |
2767 | break; | |
2768 | case sizeof(u16): | |
2769 | *val = (u64)*(u16 *)ptr; | |
2770 | break; | |
2771 | case sizeof(u32): | |
2772 | *val = (u64)*(u32 *)ptr; | |
2773 | break; | |
2774 | case sizeof(u64): | |
2775 | *val = *(u64 *)ptr; | |
2776 | break; | |
2777 | default: | |
2778 | return -EINVAL; | |
2779 | } | |
2780 | return 0; | |
2781 | } | |
2782 | ||
17a52670 AS |
2783 | /* check whether memory at (regno + off) is accessible for t = (read | write) |
2784 | * if t==write, value_regno is a register which value is stored into memory | |
2785 | * if t==read, value_regno is a register which will receive the value from memory | |
2786 | * if t==write && value_regno==-1, some unknown value is stored into memory | |
2787 | * if t==read && value_regno==-1, don't care what we read from memory | |
2788 | */ | |
ca369602 DB |
2789 | static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno, |
2790 | int off, int bpf_size, enum bpf_access_type t, | |
2791 | int value_regno, bool strict_alignment_once) | |
17a52670 | 2792 | { |
638f5b90 AS |
2793 | struct bpf_reg_state *regs = cur_regs(env); |
2794 | struct bpf_reg_state *reg = regs + regno; | |
f4d7e40a | 2795 | struct bpf_func_state *state; |
17a52670 AS |
2796 | int size, err = 0; |
2797 | ||
2798 | size = bpf_size_to_bytes(bpf_size); | |
2799 | if (size < 0) | |
2800 | return size; | |
2801 | ||
f1174f77 | 2802 | /* alignment checks will add in reg->off themselves */ |
ca369602 | 2803 | err = check_ptr_alignment(env, reg, off, size, strict_alignment_once); |
969bf05e AS |
2804 | if (err) |
2805 | return err; | |
17a52670 | 2806 | |
f1174f77 EC |
2807 | /* for access checks, reg->off is just part of off */ |
2808 | off += reg->off; | |
2809 | ||
2810 | if (reg->type == PTR_TO_MAP_VALUE) { | |
1be7f75d AS |
2811 | if (t == BPF_WRITE && value_regno >= 0 && |
2812 | is_pointer_value(env, value_regno)) { | |
61bd5218 | 2813 | verbose(env, "R%d leaks addr into map\n", value_regno); |
1be7f75d AS |
2814 | return -EACCES; |
2815 | } | |
591fe988 DB |
2816 | err = check_map_access_type(env, regno, off, size, t); |
2817 | if (err) | |
2818 | return err; | |
9fd29c08 | 2819 | err = check_map_access(env, regno, off, size, false); |
a23740ec AN |
2820 | if (!err && t == BPF_READ && value_regno >= 0) { |
2821 | struct bpf_map *map = reg->map_ptr; | |
2822 | ||
2823 | /* if map is read-only, track its contents as scalars */ | |
2824 | if (tnum_is_const(reg->var_off) && | |
2825 | bpf_map_is_rdonly(map) && | |
2826 | map->ops->map_direct_value_addr) { | |
2827 | int map_off = off + reg->var_off.value; | |
2828 | u64 val = 0; | |
2829 | ||
2830 | err = bpf_map_direct_read(map, map_off, size, | |
2831 | &val); | |
2832 | if (err) | |
2833 | return err; | |
2834 | ||
2835 | regs[value_regno].type = SCALAR_VALUE; | |
2836 | __mark_reg_known(®s[value_regno], val); | |
2837 | } else { | |
2838 | mark_reg_unknown(env, regs, value_regno); | |
2839 | } | |
2840 | } | |
1a0dc1ac | 2841 | } else if (reg->type == PTR_TO_CTX) { |
f1174f77 | 2842 | enum bpf_reg_type reg_type = SCALAR_VALUE; |
19de99f7 | 2843 | |
1be7f75d AS |
2844 | if (t == BPF_WRITE && value_regno >= 0 && |
2845 | is_pointer_value(env, value_regno)) { | |
61bd5218 | 2846 | verbose(env, "R%d leaks addr into ctx\n", value_regno); |
1be7f75d AS |
2847 | return -EACCES; |
2848 | } | |
f1174f77 | 2849 | |
58990d1f DB |
2850 | err = check_ctx_reg(env, reg, regno); |
2851 | if (err < 0) | |
2852 | return err; | |
2853 | ||
31fd8581 | 2854 | err = check_ctx_access(env, insn_idx, off, size, t, ®_type); |
969bf05e | 2855 | if (!err && t == BPF_READ && value_regno >= 0) { |
f1174f77 | 2856 | /* ctx access returns either a scalar, or a |
de8f3a83 DB |
2857 | * PTR_TO_PACKET[_META,_END]. In the latter |
2858 | * case, we know the offset is zero. | |
f1174f77 | 2859 | */ |
46f8bc92 | 2860 | if (reg_type == SCALAR_VALUE) { |
638f5b90 | 2861 | mark_reg_unknown(env, regs, value_regno); |
46f8bc92 | 2862 | } else { |
638f5b90 | 2863 | mark_reg_known_zero(env, regs, |
61bd5218 | 2864 | value_regno); |
46f8bc92 MKL |
2865 | if (reg_type_may_be_null(reg_type)) |
2866 | regs[value_regno].id = ++env->id_gen; | |
5327ed3d JW |
2867 | /* A load of ctx field could have different |
2868 | * actual load size with the one encoded in the | |
2869 | * insn. When the dst is PTR, it is for sure not | |
2870 | * a sub-register. | |
2871 | */ | |
2872 | regs[value_regno].subreg_def = DEF_NOT_SUBREG; | |
46f8bc92 | 2873 | } |
638f5b90 | 2874 | regs[value_regno].type = reg_type; |
969bf05e | 2875 | } |
17a52670 | 2876 | |
f1174f77 | 2877 | } else if (reg->type == PTR_TO_STACK) { |
f1174f77 | 2878 | off += reg->var_off.value; |
e4298d25 DB |
2879 | err = check_stack_access(env, reg, off, size); |
2880 | if (err) | |
2881 | return err; | |
8726679a | 2882 | |
f4d7e40a AS |
2883 | state = func(env, reg); |
2884 | err = update_stack_depth(env, state, off); | |
2885 | if (err) | |
2886 | return err; | |
8726679a | 2887 | |
638f5b90 | 2888 | if (t == BPF_WRITE) |
61bd5218 | 2889 | err = check_stack_write(env, state, off, size, |
af86ca4e | 2890 | value_regno, insn_idx); |
638f5b90 | 2891 | else |
61bd5218 JK |
2892 | err = check_stack_read(env, state, off, size, |
2893 | value_regno); | |
de8f3a83 | 2894 | } else if (reg_is_pkt_pointer(reg)) { |
3a0af8fd | 2895 | if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) { |
61bd5218 | 2896 | verbose(env, "cannot write into packet\n"); |
969bf05e AS |
2897 | return -EACCES; |
2898 | } | |
4acf6c0b BB |
2899 | if (t == BPF_WRITE && value_regno >= 0 && |
2900 | is_pointer_value(env, value_regno)) { | |
61bd5218 JK |
2901 | verbose(env, "R%d leaks addr into packet\n", |
2902 | value_regno); | |
4acf6c0b BB |
2903 | return -EACCES; |
2904 | } | |
9fd29c08 | 2905 | err = check_packet_access(env, regno, off, size, false); |
969bf05e | 2906 | if (!err && t == BPF_READ && value_regno >= 0) |
638f5b90 | 2907 | mark_reg_unknown(env, regs, value_regno); |
d58e468b PP |
2908 | } else if (reg->type == PTR_TO_FLOW_KEYS) { |
2909 | if (t == BPF_WRITE && value_regno >= 0 && | |
2910 | is_pointer_value(env, value_regno)) { | |
2911 | verbose(env, "R%d leaks addr into flow keys\n", | |
2912 | value_regno); | |
2913 | return -EACCES; | |
2914 | } | |
2915 | ||
2916 | err = check_flow_keys_access(env, off, size); | |
2917 | if (!err && t == BPF_READ && value_regno >= 0) | |
2918 | mark_reg_unknown(env, regs, value_regno); | |
46f8bc92 | 2919 | } else if (type_is_sk_pointer(reg->type)) { |
c64b7983 | 2920 | if (t == BPF_WRITE) { |
46f8bc92 MKL |
2921 | verbose(env, "R%d cannot write into %s\n", |
2922 | regno, reg_type_str[reg->type]); | |
c64b7983 JS |
2923 | return -EACCES; |
2924 | } | |
5f456649 | 2925 | err = check_sock_access(env, insn_idx, regno, off, size, t); |
c64b7983 JS |
2926 | if (!err && value_regno >= 0) |
2927 | mark_reg_unknown(env, regs, value_regno); | |
9df1c28b MM |
2928 | } else if (reg->type == PTR_TO_TP_BUFFER) { |
2929 | err = check_tp_buffer_access(env, reg, regno, off, size); | |
2930 | if (!err && t == BPF_READ && value_regno >= 0) | |
2931 | mark_reg_unknown(env, regs, value_regno); | |
17a52670 | 2932 | } else { |
61bd5218 JK |
2933 | verbose(env, "R%d invalid mem access '%s'\n", regno, |
2934 | reg_type_str[reg->type]); | |
17a52670 AS |
2935 | return -EACCES; |
2936 | } | |
969bf05e | 2937 | |
f1174f77 | 2938 | if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ && |
638f5b90 | 2939 | regs[value_regno].type == SCALAR_VALUE) { |
f1174f77 | 2940 | /* b/h/w load zero-extends, mark upper bits as known 0 */ |
0c17d1d2 | 2941 | coerce_reg_to_size(®s[value_regno], size); |
969bf05e | 2942 | } |
17a52670 AS |
2943 | return err; |
2944 | } | |
2945 | ||
31fd8581 | 2946 | static int check_xadd(struct bpf_verifier_env *env, int insn_idx, struct bpf_insn *insn) |
17a52670 | 2947 | { |
17a52670 AS |
2948 | int err; |
2949 | ||
2950 | if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) || | |
2951 | insn->imm != 0) { | |
61bd5218 | 2952 | verbose(env, "BPF_XADD uses reserved fields\n"); |
17a52670 AS |
2953 | return -EINVAL; |
2954 | } | |
2955 | ||
2956 | /* check src1 operand */ | |
dc503a8a | 2957 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
2958 | if (err) |
2959 | return err; | |
2960 | ||
2961 | /* check src2 operand */ | |
dc503a8a | 2962 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
2963 | if (err) |
2964 | return err; | |
2965 | ||
6bdf6abc | 2966 | if (is_pointer_value(env, insn->src_reg)) { |
61bd5218 | 2967 | verbose(env, "R%d leaks addr into mem\n", insn->src_reg); |
6bdf6abc DB |
2968 | return -EACCES; |
2969 | } | |
2970 | ||
ca369602 | 2971 | if (is_ctx_reg(env, insn->dst_reg) || |
4b5defde | 2972 | is_pkt_reg(env, insn->dst_reg) || |
46f8bc92 MKL |
2973 | is_flow_key_reg(env, insn->dst_reg) || |
2974 | is_sk_reg(env, insn->dst_reg)) { | |
ca369602 | 2975 | verbose(env, "BPF_XADD stores into R%d %s is not allowed\n", |
2a159c6f DB |
2976 | insn->dst_reg, |
2977 | reg_type_str[reg_state(env, insn->dst_reg)->type]); | |
f37a8cb8 DB |
2978 | return -EACCES; |
2979 | } | |
2980 | ||
17a52670 | 2981 | /* check whether atomic_add can read the memory */ |
31fd8581 | 2982 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
ca369602 | 2983 | BPF_SIZE(insn->code), BPF_READ, -1, true); |
17a52670 AS |
2984 | if (err) |
2985 | return err; | |
2986 | ||
2987 | /* check whether atomic_add can write into the same memory */ | |
31fd8581 | 2988 | return check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
ca369602 | 2989 | BPF_SIZE(insn->code), BPF_WRITE, -1, true); |
17a52670 AS |
2990 | } |
2991 | ||
2011fccf AI |
2992 | static int __check_stack_boundary(struct bpf_verifier_env *env, u32 regno, |
2993 | int off, int access_size, | |
2994 | bool zero_size_allowed) | |
2995 | { | |
2996 | struct bpf_reg_state *reg = reg_state(env, regno); | |
2997 | ||
2998 | if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 || | |
2999 | access_size < 0 || (access_size == 0 && !zero_size_allowed)) { | |
3000 | if (tnum_is_const(reg->var_off)) { | |
3001 | verbose(env, "invalid stack type R%d off=%d access_size=%d\n", | |
3002 | regno, off, access_size); | |
3003 | } else { | |
3004 | char tn_buf[48]; | |
3005 | ||
3006 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
3007 | verbose(env, "invalid stack type R%d var_off=%s access_size=%d\n", | |
3008 | regno, tn_buf, access_size); | |
3009 | } | |
3010 | return -EACCES; | |
3011 | } | |
3012 | return 0; | |
3013 | } | |
3014 | ||
17a52670 AS |
3015 | /* when register 'regno' is passed into function that will read 'access_size' |
3016 | * bytes from that pointer, make sure that it's within stack boundary | |
f1174f77 EC |
3017 | * and all elements of stack are initialized. |
3018 | * Unlike most pointer bounds-checking functions, this one doesn't take an | |
3019 | * 'off' argument, so it has to add in reg->off itself. | |
17a52670 | 3020 | */ |
58e2af8b | 3021 | static int check_stack_boundary(struct bpf_verifier_env *env, int regno, |
435faee1 DB |
3022 | int access_size, bool zero_size_allowed, |
3023 | struct bpf_call_arg_meta *meta) | |
17a52670 | 3024 | { |
2a159c6f | 3025 | struct bpf_reg_state *reg = reg_state(env, regno); |
f4d7e40a | 3026 | struct bpf_func_state *state = func(env, reg); |
f7cf25b2 | 3027 | int err, min_off, max_off, i, j, slot, spi; |
17a52670 | 3028 | |
914cb781 | 3029 | if (reg->type != PTR_TO_STACK) { |
f1174f77 | 3030 | /* Allow zero-byte read from NULL, regardless of pointer type */ |
8e2fe1d9 | 3031 | if (zero_size_allowed && access_size == 0 && |
914cb781 | 3032 | register_is_null(reg)) |
8e2fe1d9 DB |
3033 | return 0; |
3034 | ||
61bd5218 | 3035 | verbose(env, "R%d type=%s expected=%s\n", regno, |
914cb781 | 3036 | reg_type_str[reg->type], |
8e2fe1d9 | 3037 | reg_type_str[PTR_TO_STACK]); |
17a52670 | 3038 | return -EACCES; |
8e2fe1d9 | 3039 | } |
17a52670 | 3040 | |
2011fccf AI |
3041 | if (tnum_is_const(reg->var_off)) { |
3042 | min_off = max_off = reg->var_off.value + reg->off; | |
3043 | err = __check_stack_boundary(env, regno, min_off, access_size, | |
3044 | zero_size_allowed); | |
3045 | if (err) | |
3046 | return err; | |
3047 | } else { | |
088ec26d AI |
3048 | /* Variable offset is prohibited for unprivileged mode for |
3049 | * simplicity since it requires corresponding support in | |
3050 | * Spectre masking for stack ALU. | |
3051 | * See also retrieve_ptr_limit(). | |
3052 | */ | |
3053 | if (!env->allow_ptr_leaks) { | |
3054 | char tn_buf[48]; | |
f1174f77 | 3055 | |
088ec26d AI |
3056 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
3057 | verbose(env, "R%d indirect variable offset stack access prohibited for !root, var_off=%s\n", | |
3058 | regno, tn_buf); | |
3059 | return -EACCES; | |
3060 | } | |
f2bcd05e AI |
3061 | /* Only initialized buffer on stack is allowed to be accessed |
3062 | * with variable offset. With uninitialized buffer it's hard to | |
3063 | * guarantee that whole memory is marked as initialized on | |
3064 | * helper return since specific bounds are unknown what may | |
3065 | * cause uninitialized stack leaking. | |
3066 | */ | |
3067 | if (meta && meta->raw_mode) | |
3068 | meta = NULL; | |
3069 | ||
107c26a7 AI |
3070 | if (reg->smax_value >= BPF_MAX_VAR_OFF || |
3071 | reg->smax_value <= -BPF_MAX_VAR_OFF) { | |
3072 | verbose(env, "R%d unbounded indirect variable offset stack access\n", | |
3073 | regno); | |
3074 | return -EACCES; | |
3075 | } | |
2011fccf | 3076 | min_off = reg->smin_value + reg->off; |
107c26a7 | 3077 | max_off = reg->smax_value + reg->off; |
2011fccf AI |
3078 | err = __check_stack_boundary(env, regno, min_off, access_size, |
3079 | zero_size_allowed); | |
107c26a7 AI |
3080 | if (err) { |
3081 | verbose(env, "R%d min value is outside of stack bound\n", | |
3082 | regno); | |
2011fccf | 3083 | return err; |
107c26a7 | 3084 | } |
2011fccf AI |
3085 | err = __check_stack_boundary(env, regno, max_off, access_size, |
3086 | zero_size_allowed); | |
107c26a7 AI |
3087 | if (err) { |
3088 | verbose(env, "R%d max value is outside of stack bound\n", | |
3089 | regno); | |
2011fccf | 3090 | return err; |
107c26a7 | 3091 | } |
17a52670 AS |
3092 | } |
3093 | ||
435faee1 DB |
3094 | if (meta && meta->raw_mode) { |
3095 | meta->access_size = access_size; | |
3096 | meta->regno = regno; | |
3097 | return 0; | |
3098 | } | |
3099 | ||
2011fccf | 3100 | for (i = min_off; i < max_off + access_size; i++) { |
cc2b14d5 AS |
3101 | u8 *stype; |
3102 | ||
2011fccf | 3103 | slot = -i - 1; |
638f5b90 | 3104 | spi = slot / BPF_REG_SIZE; |
cc2b14d5 AS |
3105 | if (state->allocated_stack <= slot) |
3106 | goto err; | |
3107 | stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE]; | |
3108 | if (*stype == STACK_MISC) | |
3109 | goto mark; | |
3110 | if (*stype == STACK_ZERO) { | |
3111 | /* helper can write anything into the stack */ | |
3112 | *stype = STACK_MISC; | |
3113 | goto mark; | |
17a52670 | 3114 | } |
f7cf25b2 AS |
3115 | if (state->stack[spi].slot_type[0] == STACK_SPILL && |
3116 | state->stack[spi].spilled_ptr.type == SCALAR_VALUE) { | |
3117 | __mark_reg_unknown(&state->stack[spi].spilled_ptr); | |
3118 | for (j = 0; j < BPF_REG_SIZE; j++) | |
3119 | state->stack[spi].slot_type[j] = STACK_MISC; | |
3120 | goto mark; | |
3121 | } | |
3122 | ||
cc2b14d5 | 3123 | err: |
2011fccf AI |
3124 | if (tnum_is_const(reg->var_off)) { |
3125 | verbose(env, "invalid indirect read from stack off %d+%d size %d\n", | |
3126 | min_off, i - min_off, access_size); | |
3127 | } else { | |
3128 | char tn_buf[48]; | |
3129 | ||
3130 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
3131 | verbose(env, "invalid indirect read from stack var_off %s+%d size %d\n", | |
3132 | tn_buf, i - min_off, access_size); | |
3133 | } | |
cc2b14d5 AS |
3134 | return -EACCES; |
3135 | mark: | |
3136 | /* reading any byte out of 8-byte 'spill_slot' will cause | |
3137 | * the whole slot to be marked as 'read' | |
3138 | */ | |
679c782d | 3139 | mark_reg_read(env, &state->stack[spi].spilled_ptr, |
5327ed3d JW |
3140 | state->stack[spi].spilled_ptr.parent, |
3141 | REG_LIVE_READ64); | |
17a52670 | 3142 | } |
2011fccf | 3143 | return update_stack_depth(env, state, min_off); |
17a52670 AS |
3144 | } |
3145 | ||
06c1c049 GB |
3146 | static int check_helper_mem_access(struct bpf_verifier_env *env, int regno, |
3147 | int access_size, bool zero_size_allowed, | |
3148 | struct bpf_call_arg_meta *meta) | |
3149 | { | |
638f5b90 | 3150 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
06c1c049 | 3151 | |
f1174f77 | 3152 | switch (reg->type) { |
06c1c049 | 3153 | case PTR_TO_PACKET: |
de8f3a83 | 3154 | case PTR_TO_PACKET_META: |
9fd29c08 YS |
3155 | return check_packet_access(env, regno, reg->off, access_size, |
3156 | zero_size_allowed); | |
06c1c049 | 3157 | case PTR_TO_MAP_VALUE: |
591fe988 DB |
3158 | if (check_map_access_type(env, regno, reg->off, access_size, |
3159 | meta && meta->raw_mode ? BPF_WRITE : | |
3160 | BPF_READ)) | |
3161 | return -EACCES; | |
9fd29c08 YS |
3162 | return check_map_access(env, regno, reg->off, access_size, |
3163 | zero_size_allowed); | |
f1174f77 | 3164 | default: /* scalar_value|ptr_to_stack or invalid ptr */ |
06c1c049 GB |
3165 | return check_stack_boundary(env, regno, access_size, |
3166 | zero_size_allowed, meta); | |
3167 | } | |
3168 | } | |
3169 | ||
d83525ca AS |
3170 | /* Implementation details: |
3171 | * bpf_map_lookup returns PTR_TO_MAP_VALUE_OR_NULL | |
3172 | * Two bpf_map_lookups (even with the same key) will have different reg->id. | |
3173 | * For traditional PTR_TO_MAP_VALUE the verifier clears reg->id after | |
3174 | * value_or_null->value transition, since the verifier only cares about | |
3175 | * the range of access to valid map value pointer and doesn't care about actual | |
3176 | * address of the map element. | |
3177 | * For maps with 'struct bpf_spin_lock' inside map value the verifier keeps | |
3178 | * reg->id > 0 after value_or_null->value transition. By doing so | |
3179 | * two bpf_map_lookups will be considered two different pointers that | |
3180 | * point to different bpf_spin_locks. | |
3181 | * The verifier allows taking only one bpf_spin_lock at a time to avoid | |
3182 | * dead-locks. | |
3183 | * Since only one bpf_spin_lock is allowed the checks are simpler than | |
3184 | * reg_is_refcounted() logic. The verifier needs to remember only | |
3185 | * one spin_lock instead of array of acquired_refs. | |
3186 | * cur_state->active_spin_lock remembers which map value element got locked | |
3187 | * and clears it after bpf_spin_unlock. | |
3188 | */ | |
3189 | static int process_spin_lock(struct bpf_verifier_env *env, int regno, | |
3190 | bool is_lock) | |
3191 | { | |
3192 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; | |
3193 | struct bpf_verifier_state *cur = env->cur_state; | |
3194 | bool is_const = tnum_is_const(reg->var_off); | |
3195 | struct bpf_map *map = reg->map_ptr; | |
3196 | u64 val = reg->var_off.value; | |
3197 | ||
3198 | if (reg->type != PTR_TO_MAP_VALUE) { | |
3199 | verbose(env, "R%d is not a pointer to map_value\n", regno); | |
3200 | return -EINVAL; | |
3201 | } | |
3202 | if (!is_const) { | |
3203 | verbose(env, | |
3204 | "R%d doesn't have constant offset. bpf_spin_lock has to be at the constant offset\n", | |
3205 | regno); | |
3206 | return -EINVAL; | |
3207 | } | |
3208 | if (!map->btf) { | |
3209 | verbose(env, | |
3210 | "map '%s' has to have BTF in order to use bpf_spin_lock\n", | |
3211 | map->name); | |
3212 | return -EINVAL; | |
3213 | } | |
3214 | if (!map_value_has_spin_lock(map)) { | |
3215 | if (map->spin_lock_off == -E2BIG) | |
3216 | verbose(env, | |
3217 | "map '%s' has more than one 'struct bpf_spin_lock'\n", | |
3218 | map->name); | |
3219 | else if (map->spin_lock_off == -ENOENT) | |
3220 | verbose(env, | |
3221 | "map '%s' doesn't have 'struct bpf_spin_lock'\n", | |
3222 | map->name); | |
3223 | else | |
3224 | verbose(env, | |
3225 | "map '%s' is not a struct type or bpf_spin_lock is mangled\n", | |
3226 | map->name); | |
3227 | return -EINVAL; | |
3228 | } | |
3229 | if (map->spin_lock_off != val + reg->off) { | |
3230 | verbose(env, "off %lld doesn't point to 'struct bpf_spin_lock'\n", | |
3231 | val + reg->off); | |
3232 | return -EINVAL; | |
3233 | } | |
3234 | if (is_lock) { | |
3235 | if (cur->active_spin_lock) { | |
3236 | verbose(env, | |
3237 | "Locking two bpf_spin_locks are not allowed\n"); | |
3238 | return -EINVAL; | |
3239 | } | |
3240 | cur->active_spin_lock = reg->id; | |
3241 | } else { | |
3242 | if (!cur->active_spin_lock) { | |
3243 | verbose(env, "bpf_spin_unlock without taking a lock\n"); | |
3244 | return -EINVAL; | |
3245 | } | |
3246 | if (cur->active_spin_lock != reg->id) { | |
3247 | verbose(env, "bpf_spin_unlock of different lock\n"); | |
3248 | return -EINVAL; | |
3249 | } | |
3250 | cur->active_spin_lock = 0; | |
3251 | } | |
3252 | return 0; | |
3253 | } | |
3254 | ||
90133415 DB |
3255 | static bool arg_type_is_mem_ptr(enum bpf_arg_type type) |
3256 | { | |
3257 | return type == ARG_PTR_TO_MEM || | |
3258 | type == ARG_PTR_TO_MEM_OR_NULL || | |
3259 | type == ARG_PTR_TO_UNINIT_MEM; | |
3260 | } | |
3261 | ||
3262 | static bool arg_type_is_mem_size(enum bpf_arg_type type) | |
3263 | { | |
3264 | return type == ARG_CONST_SIZE || | |
3265 | type == ARG_CONST_SIZE_OR_ZERO; | |
3266 | } | |
3267 | ||
57c3bb72 AI |
3268 | static bool arg_type_is_int_ptr(enum bpf_arg_type type) |
3269 | { | |
3270 | return type == ARG_PTR_TO_INT || | |
3271 | type == ARG_PTR_TO_LONG; | |
3272 | } | |
3273 | ||
3274 | static int int_ptr_type_to_size(enum bpf_arg_type type) | |
3275 | { | |
3276 | if (type == ARG_PTR_TO_INT) | |
3277 | return sizeof(u32); | |
3278 | else if (type == ARG_PTR_TO_LONG) | |
3279 | return sizeof(u64); | |
3280 | ||
3281 | return -EINVAL; | |
3282 | } | |
3283 | ||
58e2af8b | 3284 | static int check_func_arg(struct bpf_verifier_env *env, u32 regno, |
33ff9823 DB |
3285 | enum bpf_arg_type arg_type, |
3286 | struct bpf_call_arg_meta *meta) | |
17a52670 | 3287 | { |
638f5b90 | 3288 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
6841de8b | 3289 | enum bpf_reg_type expected_type, type = reg->type; |
17a52670 AS |
3290 | int err = 0; |
3291 | ||
80f1d68c | 3292 | if (arg_type == ARG_DONTCARE) |
17a52670 AS |
3293 | return 0; |
3294 | ||
dc503a8a EC |
3295 | err = check_reg_arg(env, regno, SRC_OP); |
3296 | if (err) | |
3297 | return err; | |
17a52670 | 3298 | |
1be7f75d AS |
3299 | if (arg_type == ARG_ANYTHING) { |
3300 | if (is_pointer_value(env, regno)) { | |
61bd5218 JK |
3301 | verbose(env, "R%d leaks addr into helper function\n", |
3302 | regno); | |
1be7f75d AS |
3303 | return -EACCES; |
3304 | } | |
80f1d68c | 3305 | return 0; |
1be7f75d | 3306 | } |
80f1d68c | 3307 | |
de8f3a83 | 3308 | if (type_is_pkt_pointer(type) && |
3a0af8fd | 3309 | !may_access_direct_pkt_data(env, meta, BPF_READ)) { |
61bd5218 | 3310 | verbose(env, "helper access to the packet is not allowed\n"); |
6841de8b AS |
3311 | return -EACCES; |
3312 | } | |
3313 | ||
8e2fe1d9 | 3314 | if (arg_type == ARG_PTR_TO_MAP_KEY || |
2ea864c5 | 3315 | arg_type == ARG_PTR_TO_MAP_VALUE || |
6ac99e8f MKL |
3316 | arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE || |
3317 | arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL) { | |
17a52670 | 3318 | expected_type = PTR_TO_STACK; |
6ac99e8f MKL |
3319 | if (register_is_null(reg) && |
3320 | arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL) | |
3321 | /* final test in check_stack_boundary() */; | |
3322 | else if (!type_is_pkt_pointer(type) && | |
3323 | type != PTR_TO_MAP_VALUE && | |
3324 | type != expected_type) | |
6841de8b | 3325 | goto err_type; |
39f19ebb AS |
3326 | } else if (arg_type == ARG_CONST_SIZE || |
3327 | arg_type == ARG_CONST_SIZE_OR_ZERO) { | |
f1174f77 EC |
3328 | expected_type = SCALAR_VALUE; |
3329 | if (type != expected_type) | |
6841de8b | 3330 | goto err_type; |
17a52670 AS |
3331 | } else if (arg_type == ARG_CONST_MAP_PTR) { |
3332 | expected_type = CONST_PTR_TO_MAP; | |
6841de8b AS |
3333 | if (type != expected_type) |
3334 | goto err_type; | |
608cd71a AS |
3335 | } else if (arg_type == ARG_PTR_TO_CTX) { |
3336 | expected_type = PTR_TO_CTX; | |
6841de8b AS |
3337 | if (type != expected_type) |
3338 | goto err_type; | |
58990d1f DB |
3339 | err = check_ctx_reg(env, reg, regno); |
3340 | if (err < 0) | |
3341 | return err; | |
46f8bc92 MKL |
3342 | } else if (arg_type == ARG_PTR_TO_SOCK_COMMON) { |
3343 | expected_type = PTR_TO_SOCK_COMMON; | |
3344 | /* Any sk pointer can be ARG_PTR_TO_SOCK_COMMON */ | |
3345 | if (!type_is_sk_pointer(type)) | |
3346 | goto err_type; | |
1b986589 MKL |
3347 | if (reg->ref_obj_id) { |
3348 | if (meta->ref_obj_id) { | |
3349 | verbose(env, "verifier internal error: more than one arg with ref_obj_id R%d %u %u\n", | |
3350 | regno, reg->ref_obj_id, | |
3351 | meta->ref_obj_id); | |
3352 | return -EFAULT; | |
3353 | } | |
3354 | meta->ref_obj_id = reg->ref_obj_id; | |
fd978bf7 | 3355 | } |
6ac99e8f MKL |
3356 | } else if (arg_type == ARG_PTR_TO_SOCKET) { |
3357 | expected_type = PTR_TO_SOCKET; | |
3358 | if (type != expected_type) | |
3359 | goto err_type; | |
d83525ca AS |
3360 | } else if (arg_type == ARG_PTR_TO_SPIN_LOCK) { |
3361 | if (meta->func_id == BPF_FUNC_spin_lock) { | |
3362 | if (process_spin_lock(env, regno, true)) | |
3363 | return -EACCES; | |
3364 | } else if (meta->func_id == BPF_FUNC_spin_unlock) { | |
3365 | if (process_spin_lock(env, regno, false)) | |
3366 | return -EACCES; | |
3367 | } else { | |
3368 | verbose(env, "verifier internal error\n"); | |
3369 | return -EFAULT; | |
3370 | } | |
90133415 | 3371 | } else if (arg_type_is_mem_ptr(arg_type)) { |
8e2fe1d9 DB |
3372 | expected_type = PTR_TO_STACK; |
3373 | /* One exception here. In case function allows for NULL to be | |
f1174f77 | 3374 | * passed in as argument, it's a SCALAR_VALUE type. Final test |
8e2fe1d9 DB |
3375 | * happens during stack boundary checking. |
3376 | */ | |
914cb781 | 3377 | if (register_is_null(reg) && |
db1ac496 | 3378 | arg_type == ARG_PTR_TO_MEM_OR_NULL) |
6841de8b | 3379 | /* final test in check_stack_boundary() */; |
de8f3a83 DB |
3380 | else if (!type_is_pkt_pointer(type) && |
3381 | type != PTR_TO_MAP_VALUE && | |
f1174f77 | 3382 | type != expected_type) |
6841de8b | 3383 | goto err_type; |
39f19ebb | 3384 | meta->raw_mode = arg_type == ARG_PTR_TO_UNINIT_MEM; |
57c3bb72 AI |
3385 | } else if (arg_type_is_int_ptr(arg_type)) { |
3386 | expected_type = PTR_TO_STACK; | |
3387 | if (!type_is_pkt_pointer(type) && | |
3388 | type != PTR_TO_MAP_VALUE && | |
3389 | type != expected_type) | |
3390 | goto err_type; | |
17a52670 | 3391 | } else { |
61bd5218 | 3392 | verbose(env, "unsupported arg_type %d\n", arg_type); |
17a52670 AS |
3393 | return -EFAULT; |
3394 | } | |
3395 | ||
17a52670 AS |
3396 | if (arg_type == ARG_CONST_MAP_PTR) { |
3397 | /* bpf_map_xxx(map_ptr) call: remember that map_ptr */ | |
33ff9823 | 3398 | meta->map_ptr = reg->map_ptr; |
17a52670 AS |
3399 | } else if (arg_type == ARG_PTR_TO_MAP_KEY) { |
3400 | /* bpf_map_xxx(..., map_ptr, ..., key) call: | |
3401 | * check that [key, key + map->key_size) are within | |
3402 | * stack limits and initialized | |
3403 | */ | |
33ff9823 | 3404 | if (!meta->map_ptr) { |
17a52670 AS |
3405 | /* in function declaration map_ptr must come before |
3406 | * map_key, so that it's verified and known before | |
3407 | * we have to check map_key here. Otherwise it means | |
3408 | * that kernel subsystem misconfigured verifier | |
3409 | */ | |
61bd5218 | 3410 | verbose(env, "invalid map_ptr to access map->key\n"); |
17a52670 AS |
3411 | return -EACCES; |
3412 | } | |
d71962f3 PC |
3413 | err = check_helper_mem_access(env, regno, |
3414 | meta->map_ptr->key_size, false, | |
3415 | NULL); | |
2ea864c5 | 3416 | } else if (arg_type == ARG_PTR_TO_MAP_VALUE || |
6ac99e8f MKL |
3417 | (arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL && |
3418 | !register_is_null(reg)) || | |
2ea864c5 | 3419 | arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE) { |
17a52670 AS |
3420 | /* bpf_map_xxx(..., map_ptr, ..., value) call: |
3421 | * check [value, value + map->value_size) validity | |
3422 | */ | |
33ff9823 | 3423 | if (!meta->map_ptr) { |
17a52670 | 3424 | /* kernel subsystem misconfigured verifier */ |
61bd5218 | 3425 | verbose(env, "invalid map_ptr to access map->value\n"); |
17a52670 AS |
3426 | return -EACCES; |
3427 | } | |
2ea864c5 | 3428 | meta->raw_mode = (arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE); |
d71962f3 PC |
3429 | err = check_helper_mem_access(env, regno, |
3430 | meta->map_ptr->value_size, false, | |
2ea864c5 | 3431 | meta); |
90133415 | 3432 | } else if (arg_type_is_mem_size(arg_type)) { |
39f19ebb | 3433 | bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO); |
17a52670 | 3434 | |
849fa506 YS |
3435 | /* remember the mem_size which may be used later |
3436 | * to refine return values. | |
3437 | */ | |
3438 | meta->msize_smax_value = reg->smax_value; | |
3439 | meta->msize_umax_value = reg->umax_value; | |
3440 | ||
f1174f77 EC |
3441 | /* The register is SCALAR_VALUE; the access check |
3442 | * happens using its boundaries. | |
06c1c049 | 3443 | */ |
f1174f77 | 3444 | if (!tnum_is_const(reg->var_off)) |
06c1c049 GB |
3445 | /* For unprivileged variable accesses, disable raw |
3446 | * mode so that the program is required to | |
3447 | * initialize all the memory that the helper could | |
3448 | * just partially fill up. | |
3449 | */ | |
3450 | meta = NULL; | |
3451 | ||
b03c9f9f | 3452 | if (reg->smin_value < 0) { |
61bd5218 | 3453 | verbose(env, "R%d min value is negative, either use unsigned or 'var &= const'\n", |
f1174f77 EC |
3454 | regno); |
3455 | return -EACCES; | |
3456 | } | |
06c1c049 | 3457 | |
b03c9f9f | 3458 | if (reg->umin_value == 0) { |
f1174f77 EC |
3459 | err = check_helper_mem_access(env, regno - 1, 0, |
3460 | zero_size_allowed, | |
3461 | meta); | |
06c1c049 GB |
3462 | if (err) |
3463 | return err; | |
06c1c049 | 3464 | } |
f1174f77 | 3465 | |
b03c9f9f | 3466 | if (reg->umax_value >= BPF_MAX_VAR_SIZ) { |
61bd5218 | 3467 | verbose(env, "R%d unbounded memory access, use 'var &= const' or 'if (var < const)'\n", |
f1174f77 EC |
3468 | regno); |
3469 | return -EACCES; | |
3470 | } | |
3471 | err = check_helper_mem_access(env, regno - 1, | |
b03c9f9f | 3472 | reg->umax_value, |
f1174f77 | 3473 | zero_size_allowed, meta); |
b5dc0163 AS |
3474 | if (!err) |
3475 | err = mark_chain_precision(env, regno); | |
57c3bb72 AI |
3476 | } else if (arg_type_is_int_ptr(arg_type)) { |
3477 | int size = int_ptr_type_to_size(arg_type); | |
3478 | ||
3479 | err = check_helper_mem_access(env, regno, size, false, meta); | |
3480 | if (err) | |
3481 | return err; | |
3482 | err = check_ptr_alignment(env, reg, 0, size, true); | |
17a52670 AS |
3483 | } |
3484 | ||
3485 | return err; | |
6841de8b | 3486 | err_type: |
61bd5218 | 3487 | verbose(env, "R%d type=%s expected=%s\n", regno, |
6841de8b AS |
3488 | reg_type_str[type], reg_type_str[expected_type]); |
3489 | return -EACCES; | |
17a52670 AS |
3490 | } |
3491 | ||
61bd5218 JK |
3492 | static int check_map_func_compatibility(struct bpf_verifier_env *env, |
3493 | struct bpf_map *map, int func_id) | |
35578d79 | 3494 | { |
35578d79 KX |
3495 | if (!map) |
3496 | return 0; | |
3497 | ||
6aff67c8 AS |
3498 | /* We need a two way check, first is from map perspective ... */ |
3499 | switch (map->map_type) { | |
3500 | case BPF_MAP_TYPE_PROG_ARRAY: | |
3501 | if (func_id != BPF_FUNC_tail_call) | |
3502 | goto error; | |
3503 | break; | |
3504 | case BPF_MAP_TYPE_PERF_EVENT_ARRAY: | |
3505 | if (func_id != BPF_FUNC_perf_event_read && | |
908432ca YS |
3506 | func_id != BPF_FUNC_perf_event_output && |
3507 | func_id != BPF_FUNC_perf_event_read_value) | |
6aff67c8 AS |
3508 | goto error; |
3509 | break; | |
3510 | case BPF_MAP_TYPE_STACK_TRACE: | |
3511 | if (func_id != BPF_FUNC_get_stackid) | |
3512 | goto error; | |
3513 | break; | |
4ed8ec52 | 3514 | case BPF_MAP_TYPE_CGROUP_ARRAY: |
60747ef4 | 3515 | if (func_id != BPF_FUNC_skb_under_cgroup && |
60d20f91 | 3516 | func_id != BPF_FUNC_current_task_under_cgroup) |
4a482f34 MKL |
3517 | goto error; |
3518 | break; | |
cd339431 | 3519 | case BPF_MAP_TYPE_CGROUP_STORAGE: |
b741f163 | 3520 | case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE: |
cd339431 RG |
3521 | if (func_id != BPF_FUNC_get_local_storage) |
3522 | goto error; | |
3523 | break; | |
546ac1ff | 3524 | case BPF_MAP_TYPE_DEVMAP: |
6f9d451a | 3525 | case BPF_MAP_TYPE_DEVMAP_HASH: |
0cdbb4b0 THJ |
3526 | if (func_id != BPF_FUNC_redirect_map && |
3527 | func_id != BPF_FUNC_map_lookup_elem) | |
546ac1ff JF |
3528 | goto error; |
3529 | break; | |
fbfc504a BT |
3530 | /* Restrict bpf side of cpumap and xskmap, open when use-cases |
3531 | * appear. | |
3532 | */ | |
6710e112 JDB |
3533 | case BPF_MAP_TYPE_CPUMAP: |
3534 | if (func_id != BPF_FUNC_redirect_map) | |
3535 | goto error; | |
3536 | break; | |
fada7fdc JL |
3537 | case BPF_MAP_TYPE_XSKMAP: |
3538 | if (func_id != BPF_FUNC_redirect_map && | |
3539 | func_id != BPF_FUNC_map_lookup_elem) | |
3540 | goto error; | |
3541 | break; | |
56f668df | 3542 | case BPF_MAP_TYPE_ARRAY_OF_MAPS: |
bcc6b1b7 | 3543 | case BPF_MAP_TYPE_HASH_OF_MAPS: |
56f668df MKL |
3544 | if (func_id != BPF_FUNC_map_lookup_elem) |
3545 | goto error; | |
16a43625 | 3546 | break; |
174a79ff JF |
3547 | case BPF_MAP_TYPE_SOCKMAP: |
3548 | if (func_id != BPF_FUNC_sk_redirect_map && | |
3549 | func_id != BPF_FUNC_sock_map_update && | |
4f738adb JF |
3550 | func_id != BPF_FUNC_map_delete_elem && |
3551 | func_id != BPF_FUNC_msg_redirect_map) | |
174a79ff JF |
3552 | goto error; |
3553 | break; | |
81110384 JF |
3554 | case BPF_MAP_TYPE_SOCKHASH: |
3555 | if (func_id != BPF_FUNC_sk_redirect_hash && | |
3556 | func_id != BPF_FUNC_sock_hash_update && | |
3557 | func_id != BPF_FUNC_map_delete_elem && | |
3558 | func_id != BPF_FUNC_msg_redirect_hash) | |
3559 | goto error; | |
3560 | break; | |
2dbb9b9e MKL |
3561 | case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY: |
3562 | if (func_id != BPF_FUNC_sk_select_reuseport) | |
3563 | goto error; | |
3564 | break; | |
f1a2e44a MV |
3565 | case BPF_MAP_TYPE_QUEUE: |
3566 | case BPF_MAP_TYPE_STACK: | |
3567 | if (func_id != BPF_FUNC_map_peek_elem && | |
3568 | func_id != BPF_FUNC_map_pop_elem && | |
3569 | func_id != BPF_FUNC_map_push_elem) | |
3570 | goto error; | |
3571 | break; | |
6ac99e8f MKL |
3572 | case BPF_MAP_TYPE_SK_STORAGE: |
3573 | if (func_id != BPF_FUNC_sk_storage_get && | |
3574 | func_id != BPF_FUNC_sk_storage_delete) | |
3575 | goto error; | |
3576 | break; | |
6aff67c8 AS |
3577 | default: |
3578 | break; | |
3579 | } | |
3580 | ||
3581 | /* ... and second from the function itself. */ | |
3582 | switch (func_id) { | |
3583 | case BPF_FUNC_tail_call: | |
3584 | if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY) | |
3585 | goto error; | |
f910cefa | 3586 | if (env->subprog_cnt > 1) { |
f4d7e40a AS |
3587 | verbose(env, "tail_calls are not allowed in programs with bpf-to-bpf calls\n"); |
3588 | return -EINVAL; | |
3589 | } | |
6aff67c8 AS |
3590 | break; |
3591 | case BPF_FUNC_perf_event_read: | |
3592 | case BPF_FUNC_perf_event_output: | |
908432ca | 3593 | case BPF_FUNC_perf_event_read_value: |
6aff67c8 AS |
3594 | if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) |
3595 | goto error; | |
3596 | break; | |
3597 | case BPF_FUNC_get_stackid: | |
3598 | if (map->map_type != BPF_MAP_TYPE_STACK_TRACE) | |
3599 | goto error; | |
3600 | break; | |
60d20f91 | 3601 | case BPF_FUNC_current_task_under_cgroup: |
747ea55e | 3602 | case BPF_FUNC_skb_under_cgroup: |
4a482f34 MKL |
3603 | if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY) |
3604 | goto error; | |
3605 | break; | |
97f91a7c | 3606 | case BPF_FUNC_redirect_map: |
9c270af3 | 3607 | if (map->map_type != BPF_MAP_TYPE_DEVMAP && |
6f9d451a | 3608 | map->map_type != BPF_MAP_TYPE_DEVMAP_HASH && |
fbfc504a BT |
3609 | map->map_type != BPF_MAP_TYPE_CPUMAP && |
3610 | map->map_type != BPF_MAP_TYPE_XSKMAP) | |
97f91a7c JF |
3611 | goto error; |
3612 | break; | |
174a79ff | 3613 | case BPF_FUNC_sk_redirect_map: |
4f738adb | 3614 | case BPF_FUNC_msg_redirect_map: |
81110384 | 3615 | case BPF_FUNC_sock_map_update: |
174a79ff JF |
3616 | if (map->map_type != BPF_MAP_TYPE_SOCKMAP) |
3617 | goto error; | |
3618 | break; | |
81110384 JF |
3619 | case BPF_FUNC_sk_redirect_hash: |
3620 | case BPF_FUNC_msg_redirect_hash: | |
3621 | case BPF_FUNC_sock_hash_update: | |
3622 | if (map->map_type != BPF_MAP_TYPE_SOCKHASH) | |
174a79ff JF |
3623 | goto error; |
3624 | break; | |
cd339431 | 3625 | case BPF_FUNC_get_local_storage: |
b741f163 RG |
3626 | if (map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE && |
3627 | map->map_type != BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) | |
cd339431 RG |
3628 | goto error; |
3629 | break; | |
2dbb9b9e MKL |
3630 | case BPF_FUNC_sk_select_reuseport: |
3631 | if (map->map_type != BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) | |
3632 | goto error; | |
3633 | break; | |
f1a2e44a MV |
3634 | case BPF_FUNC_map_peek_elem: |
3635 | case BPF_FUNC_map_pop_elem: | |
3636 | case BPF_FUNC_map_push_elem: | |
3637 | if (map->map_type != BPF_MAP_TYPE_QUEUE && | |
3638 | map->map_type != BPF_MAP_TYPE_STACK) | |
3639 | goto error; | |
3640 | break; | |
6ac99e8f MKL |
3641 | case BPF_FUNC_sk_storage_get: |
3642 | case BPF_FUNC_sk_storage_delete: | |
3643 | if (map->map_type != BPF_MAP_TYPE_SK_STORAGE) | |
3644 | goto error; | |
3645 | break; | |
6aff67c8 AS |
3646 | default: |
3647 | break; | |
35578d79 KX |
3648 | } |
3649 | ||
3650 | return 0; | |
6aff67c8 | 3651 | error: |
61bd5218 | 3652 | verbose(env, "cannot pass map_type %d into func %s#%d\n", |
ebb676da | 3653 | map->map_type, func_id_name(func_id), func_id); |
6aff67c8 | 3654 | return -EINVAL; |
35578d79 KX |
3655 | } |
3656 | ||
90133415 | 3657 | static bool check_raw_mode_ok(const struct bpf_func_proto *fn) |
435faee1 DB |
3658 | { |
3659 | int count = 0; | |
3660 | ||
39f19ebb | 3661 | if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 3662 | count++; |
39f19ebb | 3663 | if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 3664 | count++; |
39f19ebb | 3665 | if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 3666 | count++; |
39f19ebb | 3667 | if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 3668 | count++; |
39f19ebb | 3669 | if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 DB |
3670 | count++; |
3671 | ||
90133415 DB |
3672 | /* We only support one arg being in raw mode at the moment, |
3673 | * which is sufficient for the helper functions we have | |
3674 | * right now. | |
3675 | */ | |
3676 | return count <= 1; | |
3677 | } | |
3678 | ||
3679 | static bool check_args_pair_invalid(enum bpf_arg_type arg_curr, | |
3680 | enum bpf_arg_type arg_next) | |
3681 | { | |
3682 | return (arg_type_is_mem_ptr(arg_curr) && | |
3683 | !arg_type_is_mem_size(arg_next)) || | |
3684 | (!arg_type_is_mem_ptr(arg_curr) && | |
3685 | arg_type_is_mem_size(arg_next)); | |
3686 | } | |
3687 | ||
3688 | static bool check_arg_pair_ok(const struct bpf_func_proto *fn) | |
3689 | { | |
3690 | /* bpf_xxx(..., buf, len) call will access 'len' | |
3691 | * bytes from memory 'buf'. Both arg types need | |
3692 | * to be paired, so make sure there's no buggy | |
3693 | * helper function specification. | |
3694 | */ | |
3695 | if (arg_type_is_mem_size(fn->arg1_type) || | |
3696 | arg_type_is_mem_ptr(fn->arg5_type) || | |
3697 | check_args_pair_invalid(fn->arg1_type, fn->arg2_type) || | |
3698 | check_args_pair_invalid(fn->arg2_type, fn->arg3_type) || | |
3699 | check_args_pair_invalid(fn->arg3_type, fn->arg4_type) || | |
3700 | check_args_pair_invalid(fn->arg4_type, fn->arg5_type)) | |
3701 | return false; | |
3702 | ||
3703 | return true; | |
3704 | } | |
3705 | ||
1b986589 | 3706 | static bool check_refcount_ok(const struct bpf_func_proto *fn, int func_id) |
fd978bf7 JS |
3707 | { |
3708 | int count = 0; | |
3709 | ||
1b986589 | 3710 | if (arg_type_may_be_refcounted(fn->arg1_type)) |
fd978bf7 | 3711 | count++; |
1b986589 | 3712 | if (arg_type_may_be_refcounted(fn->arg2_type)) |
fd978bf7 | 3713 | count++; |
1b986589 | 3714 | if (arg_type_may_be_refcounted(fn->arg3_type)) |
fd978bf7 | 3715 | count++; |
1b986589 | 3716 | if (arg_type_may_be_refcounted(fn->arg4_type)) |
fd978bf7 | 3717 | count++; |
1b986589 | 3718 | if (arg_type_may_be_refcounted(fn->arg5_type)) |
fd978bf7 JS |
3719 | count++; |
3720 | ||
1b986589 MKL |
3721 | /* A reference acquiring function cannot acquire |
3722 | * another refcounted ptr. | |
3723 | */ | |
3724 | if (is_acquire_function(func_id) && count) | |
3725 | return false; | |
3726 | ||
fd978bf7 JS |
3727 | /* We only support one arg being unreferenced at the moment, |
3728 | * which is sufficient for the helper functions we have right now. | |
3729 | */ | |
3730 | return count <= 1; | |
3731 | } | |
3732 | ||
1b986589 | 3733 | static int check_func_proto(const struct bpf_func_proto *fn, int func_id) |
90133415 DB |
3734 | { |
3735 | return check_raw_mode_ok(fn) && | |
fd978bf7 | 3736 | check_arg_pair_ok(fn) && |
1b986589 | 3737 | check_refcount_ok(fn, func_id) ? 0 : -EINVAL; |
435faee1 DB |
3738 | } |
3739 | ||
de8f3a83 DB |
3740 | /* Packet data might have moved, any old PTR_TO_PACKET[_META,_END] |
3741 | * are now invalid, so turn them into unknown SCALAR_VALUE. | |
f1174f77 | 3742 | */ |
f4d7e40a AS |
3743 | static void __clear_all_pkt_pointers(struct bpf_verifier_env *env, |
3744 | struct bpf_func_state *state) | |
969bf05e | 3745 | { |
58e2af8b | 3746 | struct bpf_reg_state *regs = state->regs, *reg; |
969bf05e AS |
3747 | int i; |
3748 | ||
3749 | for (i = 0; i < MAX_BPF_REG; i++) | |
de8f3a83 | 3750 | if (reg_is_pkt_pointer_any(®s[i])) |
61bd5218 | 3751 | mark_reg_unknown(env, regs, i); |
969bf05e | 3752 | |
f3709f69 JS |
3753 | bpf_for_each_spilled_reg(i, state, reg) { |
3754 | if (!reg) | |
969bf05e | 3755 | continue; |
de8f3a83 DB |
3756 | if (reg_is_pkt_pointer_any(reg)) |
3757 | __mark_reg_unknown(reg); | |
969bf05e AS |
3758 | } |
3759 | } | |
3760 | ||
f4d7e40a AS |
3761 | static void clear_all_pkt_pointers(struct bpf_verifier_env *env) |
3762 | { | |
3763 | struct bpf_verifier_state *vstate = env->cur_state; | |
3764 | int i; | |
3765 | ||
3766 | for (i = 0; i <= vstate->curframe; i++) | |
3767 | __clear_all_pkt_pointers(env, vstate->frame[i]); | |
3768 | } | |
3769 | ||
fd978bf7 | 3770 | static void release_reg_references(struct bpf_verifier_env *env, |
1b986589 MKL |
3771 | struct bpf_func_state *state, |
3772 | int ref_obj_id) | |
fd978bf7 JS |
3773 | { |
3774 | struct bpf_reg_state *regs = state->regs, *reg; | |
3775 | int i; | |
3776 | ||
3777 | for (i = 0; i < MAX_BPF_REG; i++) | |
1b986589 | 3778 | if (regs[i].ref_obj_id == ref_obj_id) |
fd978bf7 JS |
3779 | mark_reg_unknown(env, regs, i); |
3780 | ||
3781 | bpf_for_each_spilled_reg(i, state, reg) { | |
3782 | if (!reg) | |
3783 | continue; | |
1b986589 | 3784 | if (reg->ref_obj_id == ref_obj_id) |
fd978bf7 JS |
3785 | __mark_reg_unknown(reg); |
3786 | } | |
3787 | } | |
3788 | ||
3789 | /* The pointer with the specified id has released its reference to kernel | |
3790 | * resources. Identify all copies of the same pointer and clear the reference. | |
3791 | */ | |
3792 | static int release_reference(struct bpf_verifier_env *env, | |
1b986589 | 3793 | int ref_obj_id) |
fd978bf7 JS |
3794 | { |
3795 | struct bpf_verifier_state *vstate = env->cur_state; | |
1b986589 | 3796 | int err; |
fd978bf7 JS |
3797 | int i; |
3798 | ||
1b986589 MKL |
3799 | err = release_reference_state(cur_func(env), ref_obj_id); |
3800 | if (err) | |
3801 | return err; | |
3802 | ||
fd978bf7 | 3803 | for (i = 0; i <= vstate->curframe; i++) |
1b986589 | 3804 | release_reg_references(env, vstate->frame[i], ref_obj_id); |
fd978bf7 | 3805 | |
1b986589 | 3806 | return 0; |
fd978bf7 JS |
3807 | } |
3808 | ||
f4d7e40a AS |
3809 | static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, |
3810 | int *insn_idx) | |
3811 | { | |
3812 | struct bpf_verifier_state *state = env->cur_state; | |
3813 | struct bpf_func_state *caller, *callee; | |
fd978bf7 | 3814 | int i, err, subprog, target_insn; |
f4d7e40a | 3815 | |
aada9ce6 | 3816 | if (state->curframe + 1 >= MAX_CALL_FRAMES) { |
f4d7e40a | 3817 | verbose(env, "the call stack of %d frames is too deep\n", |
aada9ce6 | 3818 | state->curframe + 2); |
f4d7e40a AS |
3819 | return -E2BIG; |
3820 | } | |
3821 | ||
3822 | target_insn = *insn_idx + insn->imm; | |
3823 | subprog = find_subprog(env, target_insn + 1); | |
3824 | if (subprog < 0) { | |
3825 | verbose(env, "verifier bug. No program starts at insn %d\n", | |
3826 | target_insn + 1); | |
3827 | return -EFAULT; | |
3828 | } | |
3829 | ||
3830 | caller = state->frame[state->curframe]; | |
3831 | if (state->frame[state->curframe + 1]) { | |
3832 | verbose(env, "verifier bug. Frame %d already allocated\n", | |
3833 | state->curframe + 1); | |
3834 | return -EFAULT; | |
3835 | } | |
3836 | ||
3837 | callee = kzalloc(sizeof(*callee), GFP_KERNEL); | |
3838 | if (!callee) | |
3839 | return -ENOMEM; | |
3840 | state->frame[state->curframe + 1] = callee; | |
3841 | ||
3842 | /* callee cannot access r0, r6 - r9 for reading and has to write | |
3843 | * into its own stack before reading from it. | |
3844 | * callee can read/write into caller's stack | |
3845 | */ | |
3846 | init_func_state(env, callee, | |
3847 | /* remember the callsite, it will be used by bpf_exit */ | |
3848 | *insn_idx /* callsite */, | |
3849 | state->curframe + 1 /* frameno within this callchain */, | |
f910cefa | 3850 | subprog /* subprog number within this prog */); |
f4d7e40a | 3851 | |
fd978bf7 JS |
3852 | /* Transfer references to the callee */ |
3853 | err = transfer_reference_state(callee, caller); | |
3854 | if (err) | |
3855 | return err; | |
3856 | ||
679c782d EC |
3857 | /* copy r1 - r5 args that callee can access. The copy includes parent |
3858 | * pointers, which connects us up to the liveness chain | |
3859 | */ | |
f4d7e40a AS |
3860 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) |
3861 | callee->regs[i] = caller->regs[i]; | |
3862 | ||
679c782d | 3863 | /* after the call registers r0 - r5 were scratched */ |
f4d7e40a AS |
3864 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
3865 | mark_reg_not_init(env, caller->regs, caller_saved[i]); | |
3866 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); | |
3867 | } | |
3868 | ||
3869 | /* only increment it after check_reg_arg() finished */ | |
3870 | state->curframe++; | |
3871 | ||
3872 | /* and go analyze first insn of the callee */ | |
3873 | *insn_idx = target_insn; | |
3874 | ||
06ee7115 | 3875 | if (env->log.level & BPF_LOG_LEVEL) { |
f4d7e40a AS |
3876 | verbose(env, "caller:\n"); |
3877 | print_verifier_state(env, caller); | |
3878 | verbose(env, "callee:\n"); | |
3879 | print_verifier_state(env, callee); | |
3880 | } | |
3881 | return 0; | |
3882 | } | |
3883 | ||
3884 | static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx) | |
3885 | { | |
3886 | struct bpf_verifier_state *state = env->cur_state; | |
3887 | struct bpf_func_state *caller, *callee; | |
3888 | struct bpf_reg_state *r0; | |
fd978bf7 | 3889 | int err; |
f4d7e40a AS |
3890 | |
3891 | callee = state->frame[state->curframe]; | |
3892 | r0 = &callee->regs[BPF_REG_0]; | |
3893 | if (r0->type == PTR_TO_STACK) { | |
3894 | /* technically it's ok to return caller's stack pointer | |
3895 | * (or caller's caller's pointer) back to the caller, | |
3896 | * since these pointers are valid. Only current stack | |
3897 | * pointer will be invalid as soon as function exits, | |
3898 | * but let's be conservative | |
3899 | */ | |
3900 | verbose(env, "cannot return stack pointer to the caller\n"); | |
3901 | return -EINVAL; | |
3902 | } | |
3903 | ||
3904 | state->curframe--; | |
3905 | caller = state->frame[state->curframe]; | |
3906 | /* return to the caller whatever r0 had in the callee */ | |
3907 | caller->regs[BPF_REG_0] = *r0; | |
3908 | ||
fd978bf7 JS |
3909 | /* Transfer references to the caller */ |
3910 | err = transfer_reference_state(caller, callee); | |
3911 | if (err) | |
3912 | return err; | |
3913 | ||
f4d7e40a | 3914 | *insn_idx = callee->callsite + 1; |
06ee7115 | 3915 | if (env->log.level & BPF_LOG_LEVEL) { |
f4d7e40a AS |
3916 | verbose(env, "returning from callee:\n"); |
3917 | print_verifier_state(env, callee); | |
3918 | verbose(env, "to caller at %d:\n", *insn_idx); | |
3919 | print_verifier_state(env, caller); | |
3920 | } | |
3921 | /* clear everything in the callee */ | |
3922 | free_func_state(callee); | |
3923 | state->frame[state->curframe + 1] = NULL; | |
3924 | return 0; | |
3925 | } | |
3926 | ||
849fa506 YS |
3927 | static void do_refine_retval_range(struct bpf_reg_state *regs, int ret_type, |
3928 | int func_id, | |
3929 | struct bpf_call_arg_meta *meta) | |
3930 | { | |
3931 | struct bpf_reg_state *ret_reg = ®s[BPF_REG_0]; | |
3932 | ||
3933 | if (ret_type != RET_INTEGER || | |
3934 | (func_id != BPF_FUNC_get_stack && | |
3935 | func_id != BPF_FUNC_probe_read_str)) | |
3936 | return; | |
3937 | ||
3938 | ret_reg->smax_value = meta->msize_smax_value; | |
3939 | ret_reg->umax_value = meta->msize_umax_value; | |
3940 | __reg_deduce_bounds(ret_reg); | |
3941 | __reg_bound_offset(ret_reg); | |
3942 | } | |
3943 | ||
c93552c4 DB |
3944 | static int |
3945 | record_func_map(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, | |
3946 | int func_id, int insn_idx) | |
3947 | { | |
3948 | struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; | |
591fe988 | 3949 | struct bpf_map *map = meta->map_ptr; |
c93552c4 DB |
3950 | |
3951 | if (func_id != BPF_FUNC_tail_call && | |
09772d92 DB |
3952 | func_id != BPF_FUNC_map_lookup_elem && |
3953 | func_id != BPF_FUNC_map_update_elem && | |
f1a2e44a MV |
3954 | func_id != BPF_FUNC_map_delete_elem && |
3955 | func_id != BPF_FUNC_map_push_elem && | |
3956 | func_id != BPF_FUNC_map_pop_elem && | |
3957 | func_id != BPF_FUNC_map_peek_elem) | |
c93552c4 | 3958 | return 0; |
09772d92 | 3959 | |
591fe988 | 3960 | if (map == NULL) { |
c93552c4 DB |
3961 | verbose(env, "kernel subsystem misconfigured verifier\n"); |
3962 | return -EINVAL; | |
3963 | } | |
3964 | ||
591fe988 DB |
3965 | /* In case of read-only, some additional restrictions |
3966 | * need to be applied in order to prevent altering the | |
3967 | * state of the map from program side. | |
3968 | */ | |
3969 | if ((map->map_flags & BPF_F_RDONLY_PROG) && | |
3970 | (func_id == BPF_FUNC_map_delete_elem || | |
3971 | func_id == BPF_FUNC_map_update_elem || | |
3972 | func_id == BPF_FUNC_map_push_elem || | |
3973 | func_id == BPF_FUNC_map_pop_elem)) { | |
3974 | verbose(env, "write into map forbidden\n"); | |
3975 | return -EACCES; | |
3976 | } | |
3977 | ||
c93552c4 DB |
3978 | if (!BPF_MAP_PTR(aux->map_state)) |
3979 | bpf_map_ptr_store(aux, meta->map_ptr, | |
3980 | meta->map_ptr->unpriv_array); | |
3981 | else if (BPF_MAP_PTR(aux->map_state) != meta->map_ptr) | |
3982 | bpf_map_ptr_store(aux, BPF_MAP_PTR_POISON, | |
3983 | meta->map_ptr->unpriv_array); | |
3984 | return 0; | |
3985 | } | |
3986 | ||
fd978bf7 JS |
3987 | static int check_reference_leak(struct bpf_verifier_env *env) |
3988 | { | |
3989 | struct bpf_func_state *state = cur_func(env); | |
3990 | int i; | |
3991 | ||
3992 | for (i = 0; i < state->acquired_refs; i++) { | |
3993 | verbose(env, "Unreleased reference id=%d alloc_insn=%d\n", | |
3994 | state->refs[i].id, state->refs[i].insn_idx); | |
3995 | } | |
3996 | return state->acquired_refs ? -EINVAL : 0; | |
3997 | } | |
3998 | ||
f4d7e40a | 3999 | static int check_helper_call(struct bpf_verifier_env *env, int func_id, int insn_idx) |
17a52670 | 4000 | { |
17a52670 | 4001 | const struct bpf_func_proto *fn = NULL; |
638f5b90 | 4002 | struct bpf_reg_state *regs; |
33ff9823 | 4003 | struct bpf_call_arg_meta meta; |
969bf05e | 4004 | bool changes_data; |
17a52670 AS |
4005 | int i, err; |
4006 | ||
4007 | /* find function prototype */ | |
4008 | if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) { | |
61bd5218 JK |
4009 | verbose(env, "invalid func %s#%d\n", func_id_name(func_id), |
4010 | func_id); | |
17a52670 AS |
4011 | return -EINVAL; |
4012 | } | |
4013 | ||
00176a34 | 4014 | if (env->ops->get_func_proto) |
5e43f899 | 4015 | fn = env->ops->get_func_proto(func_id, env->prog); |
17a52670 | 4016 | if (!fn) { |
61bd5218 JK |
4017 | verbose(env, "unknown func %s#%d\n", func_id_name(func_id), |
4018 | func_id); | |
17a52670 AS |
4019 | return -EINVAL; |
4020 | } | |
4021 | ||
4022 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ | |
24701ece | 4023 | if (!env->prog->gpl_compatible && fn->gpl_only) { |
3fe2867c | 4024 | verbose(env, "cannot call GPL-restricted function from non-GPL compatible program\n"); |
17a52670 AS |
4025 | return -EINVAL; |
4026 | } | |
4027 | ||
04514d13 | 4028 | /* With LD_ABS/IND some JITs save/restore skb from r1. */ |
17bedab2 | 4029 | changes_data = bpf_helper_changes_pkt_data(fn->func); |
04514d13 DB |
4030 | if (changes_data && fn->arg1_type != ARG_PTR_TO_CTX) { |
4031 | verbose(env, "kernel subsystem misconfigured func %s#%d: r1 != ctx\n", | |
4032 | func_id_name(func_id), func_id); | |
4033 | return -EINVAL; | |
4034 | } | |
969bf05e | 4035 | |
33ff9823 | 4036 | memset(&meta, 0, sizeof(meta)); |
36bbef52 | 4037 | meta.pkt_access = fn->pkt_access; |
33ff9823 | 4038 | |
1b986589 | 4039 | err = check_func_proto(fn, func_id); |
435faee1 | 4040 | if (err) { |
61bd5218 | 4041 | verbose(env, "kernel subsystem misconfigured func %s#%d\n", |
ebb676da | 4042 | func_id_name(func_id), func_id); |
435faee1 DB |
4043 | return err; |
4044 | } | |
4045 | ||
d83525ca | 4046 | meta.func_id = func_id; |
17a52670 | 4047 | /* check args */ |
33ff9823 | 4048 | err = check_func_arg(env, BPF_REG_1, fn->arg1_type, &meta); |
17a52670 AS |
4049 | if (err) |
4050 | return err; | |
33ff9823 | 4051 | err = check_func_arg(env, BPF_REG_2, fn->arg2_type, &meta); |
17a52670 AS |
4052 | if (err) |
4053 | return err; | |
33ff9823 | 4054 | err = check_func_arg(env, BPF_REG_3, fn->arg3_type, &meta); |
17a52670 AS |
4055 | if (err) |
4056 | return err; | |
33ff9823 | 4057 | err = check_func_arg(env, BPF_REG_4, fn->arg4_type, &meta); |
17a52670 AS |
4058 | if (err) |
4059 | return err; | |
33ff9823 | 4060 | err = check_func_arg(env, BPF_REG_5, fn->arg5_type, &meta); |
17a52670 AS |
4061 | if (err) |
4062 | return err; | |
4063 | ||
c93552c4 DB |
4064 | err = record_func_map(env, &meta, func_id, insn_idx); |
4065 | if (err) | |
4066 | return err; | |
4067 | ||
435faee1 DB |
4068 | /* Mark slots with STACK_MISC in case of raw mode, stack offset |
4069 | * is inferred from register state. | |
4070 | */ | |
4071 | for (i = 0; i < meta.access_size; i++) { | |
ca369602 DB |
4072 | err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B, |
4073 | BPF_WRITE, -1, false); | |
435faee1 DB |
4074 | if (err) |
4075 | return err; | |
4076 | } | |
4077 | ||
fd978bf7 JS |
4078 | if (func_id == BPF_FUNC_tail_call) { |
4079 | err = check_reference_leak(env); | |
4080 | if (err) { | |
4081 | verbose(env, "tail_call would lead to reference leak\n"); | |
4082 | return err; | |
4083 | } | |
4084 | } else if (is_release_function(func_id)) { | |
1b986589 | 4085 | err = release_reference(env, meta.ref_obj_id); |
46f8bc92 MKL |
4086 | if (err) { |
4087 | verbose(env, "func %s#%d reference has not been acquired before\n", | |
4088 | func_id_name(func_id), func_id); | |
fd978bf7 | 4089 | return err; |
46f8bc92 | 4090 | } |
fd978bf7 JS |
4091 | } |
4092 | ||
638f5b90 | 4093 | regs = cur_regs(env); |
cd339431 RG |
4094 | |
4095 | /* check that flags argument in get_local_storage(map, flags) is 0, | |
4096 | * this is required because get_local_storage() can't return an error. | |
4097 | */ | |
4098 | if (func_id == BPF_FUNC_get_local_storage && | |
4099 | !register_is_null(®s[BPF_REG_2])) { | |
4100 | verbose(env, "get_local_storage() doesn't support non-zero flags\n"); | |
4101 | return -EINVAL; | |
4102 | } | |
4103 | ||
17a52670 | 4104 | /* reset caller saved regs */ |
dc503a8a | 4105 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
61bd5218 | 4106 | mark_reg_not_init(env, regs, caller_saved[i]); |
dc503a8a EC |
4107 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
4108 | } | |
17a52670 | 4109 | |
5327ed3d JW |
4110 | /* helper call returns 64-bit value. */ |
4111 | regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG; | |
4112 | ||
dc503a8a | 4113 | /* update return register (already marked as written above) */ |
17a52670 | 4114 | if (fn->ret_type == RET_INTEGER) { |
f1174f77 | 4115 | /* sets type to SCALAR_VALUE */ |
61bd5218 | 4116 | mark_reg_unknown(env, regs, BPF_REG_0); |
17a52670 AS |
4117 | } else if (fn->ret_type == RET_VOID) { |
4118 | regs[BPF_REG_0].type = NOT_INIT; | |
3e6a4b3e RG |
4119 | } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL || |
4120 | fn->ret_type == RET_PTR_TO_MAP_VALUE) { | |
f1174f77 | 4121 | /* There is no offset yet applied, variable or fixed */ |
61bd5218 | 4122 | mark_reg_known_zero(env, regs, BPF_REG_0); |
17a52670 AS |
4123 | /* remember map_ptr, so that check_map_access() |
4124 | * can check 'value_size' boundary of memory access | |
4125 | * to map element returned from bpf_map_lookup_elem() | |
4126 | */ | |
33ff9823 | 4127 | if (meta.map_ptr == NULL) { |
61bd5218 JK |
4128 | verbose(env, |
4129 | "kernel subsystem misconfigured verifier\n"); | |
17a52670 AS |
4130 | return -EINVAL; |
4131 | } | |
33ff9823 | 4132 | regs[BPF_REG_0].map_ptr = meta.map_ptr; |
4d31f301 DB |
4133 | if (fn->ret_type == RET_PTR_TO_MAP_VALUE) { |
4134 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE; | |
e16d2f1a AS |
4135 | if (map_value_has_spin_lock(meta.map_ptr)) |
4136 | regs[BPF_REG_0].id = ++env->id_gen; | |
4d31f301 DB |
4137 | } else { |
4138 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL; | |
4139 | regs[BPF_REG_0].id = ++env->id_gen; | |
4140 | } | |
c64b7983 JS |
4141 | } else if (fn->ret_type == RET_PTR_TO_SOCKET_OR_NULL) { |
4142 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
4143 | regs[BPF_REG_0].type = PTR_TO_SOCKET_OR_NULL; | |
0f3adc28 | 4144 | regs[BPF_REG_0].id = ++env->id_gen; |
85a51f8c LB |
4145 | } else if (fn->ret_type == RET_PTR_TO_SOCK_COMMON_OR_NULL) { |
4146 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
4147 | regs[BPF_REG_0].type = PTR_TO_SOCK_COMMON_OR_NULL; | |
4148 | regs[BPF_REG_0].id = ++env->id_gen; | |
655a51e5 MKL |
4149 | } else if (fn->ret_type == RET_PTR_TO_TCP_SOCK_OR_NULL) { |
4150 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
4151 | regs[BPF_REG_0].type = PTR_TO_TCP_SOCK_OR_NULL; | |
4152 | regs[BPF_REG_0].id = ++env->id_gen; | |
17a52670 | 4153 | } else { |
61bd5218 | 4154 | verbose(env, "unknown return type %d of func %s#%d\n", |
ebb676da | 4155 | fn->ret_type, func_id_name(func_id), func_id); |
17a52670 AS |
4156 | return -EINVAL; |
4157 | } | |
04fd61ab | 4158 | |
0f3adc28 | 4159 | if (is_ptr_cast_function(func_id)) { |
1b986589 MKL |
4160 | /* For release_reference() */ |
4161 | regs[BPF_REG_0].ref_obj_id = meta.ref_obj_id; | |
0f3adc28 LB |
4162 | } else if (is_acquire_function(func_id)) { |
4163 | int id = acquire_reference_state(env, insn_idx); | |
4164 | ||
4165 | if (id < 0) | |
4166 | return id; | |
4167 | /* For mark_ptr_or_null_reg() */ | |
4168 | regs[BPF_REG_0].id = id; | |
4169 | /* For release_reference() */ | |
4170 | regs[BPF_REG_0].ref_obj_id = id; | |
4171 | } | |
1b986589 | 4172 | |
849fa506 YS |
4173 | do_refine_retval_range(regs, fn->ret_type, func_id, &meta); |
4174 | ||
61bd5218 | 4175 | err = check_map_func_compatibility(env, meta.map_ptr, func_id); |
35578d79 KX |
4176 | if (err) |
4177 | return err; | |
04fd61ab | 4178 | |
c195651e YS |
4179 | if (func_id == BPF_FUNC_get_stack && !env->prog->has_callchain_buf) { |
4180 | const char *err_str; | |
4181 | ||
4182 | #ifdef CONFIG_PERF_EVENTS | |
4183 | err = get_callchain_buffers(sysctl_perf_event_max_stack); | |
4184 | err_str = "cannot get callchain buffer for func %s#%d\n"; | |
4185 | #else | |
4186 | err = -ENOTSUPP; | |
4187 | err_str = "func %s#%d not supported without CONFIG_PERF_EVENTS\n"; | |
4188 | #endif | |
4189 | if (err) { | |
4190 | verbose(env, err_str, func_id_name(func_id), func_id); | |
4191 | return err; | |
4192 | } | |
4193 | ||
4194 | env->prog->has_callchain_buf = true; | |
4195 | } | |
4196 | ||
969bf05e AS |
4197 | if (changes_data) |
4198 | clear_all_pkt_pointers(env); | |
4199 | return 0; | |
4200 | } | |
4201 | ||
b03c9f9f EC |
4202 | static bool signed_add_overflows(s64 a, s64 b) |
4203 | { | |
4204 | /* Do the add in u64, where overflow is well-defined */ | |
4205 | s64 res = (s64)((u64)a + (u64)b); | |
4206 | ||
4207 | if (b < 0) | |
4208 | return res > a; | |
4209 | return res < a; | |
4210 | } | |
4211 | ||
4212 | static bool signed_sub_overflows(s64 a, s64 b) | |
4213 | { | |
4214 | /* Do the sub in u64, where overflow is well-defined */ | |
4215 | s64 res = (s64)((u64)a - (u64)b); | |
4216 | ||
4217 | if (b < 0) | |
4218 | return res < a; | |
4219 | return res > a; | |
969bf05e AS |
4220 | } |
4221 | ||
bb7f0f98 AS |
4222 | static bool check_reg_sane_offset(struct bpf_verifier_env *env, |
4223 | const struct bpf_reg_state *reg, | |
4224 | enum bpf_reg_type type) | |
4225 | { | |
4226 | bool known = tnum_is_const(reg->var_off); | |
4227 | s64 val = reg->var_off.value; | |
4228 | s64 smin = reg->smin_value; | |
4229 | ||
4230 | if (known && (val >= BPF_MAX_VAR_OFF || val <= -BPF_MAX_VAR_OFF)) { | |
4231 | verbose(env, "math between %s pointer and %lld is not allowed\n", | |
4232 | reg_type_str[type], val); | |
4233 | return false; | |
4234 | } | |
4235 | ||
4236 | if (reg->off >= BPF_MAX_VAR_OFF || reg->off <= -BPF_MAX_VAR_OFF) { | |
4237 | verbose(env, "%s pointer offset %d is not allowed\n", | |
4238 | reg_type_str[type], reg->off); | |
4239 | return false; | |
4240 | } | |
4241 | ||
4242 | if (smin == S64_MIN) { | |
4243 | verbose(env, "math between %s pointer and register with unbounded min value is not allowed\n", | |
4244 | reg_type_str[type]); | |
4245 | return false; | |
4246 | } | |
4247 | ||
4248 | if (smin >= BPF_MAX_VAR_OFF || smin <= -BPF_MAX_VAR_OFF) { | |
4249 | verbose(env, "value %lld makes %s pointer be out of bounds\n", | |
4250 | smin, reg_type_str[type]); | |
4251 | return false; | |
4252 | } | |
4253 | ||
4254 | return true; | |
4255 | } | |
4256 | ||
979d63d5 DB |
4257 | static struct bpf_insn_aux_data *cur_aux(struct bpf_verifier_env *env) |
4258 | { | |
4259 | return &env->insn_aux_data[env->insn_idx]; | |
4260 | } | |
4261 | ||
4262 | static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg, | |
4263 | u32 *ptr_limit, u8 opcode, bool off_is_neg) | |
4264 | { | |
4265 | bool mask_to_left = (opcode == BPF_ADD && off_is_neg) || | |
4266 | (opcode == BPF_SUB && !off_is_neg); | |
4267 | u32 off; | |
4268 | ||
4269 | switch (ptr_reg->type) { | |
4270 | case PTR_TO_STACK: | |
088ec26d AI |
4271 | /* Indirect variable offset stack access is prohibited in |
4272 | * unprivileged mode so it's not handled here. | |
4273 | */ | |
979d63d5 DB |
4274 | off = ptr_reg->off + ptr_reg->var_off.value; |
4275 | if (mask_to_left) | |
4276 | *ptr_limit = MAX_BPF_STACK + off; | |
4277 | else | |
4278 | *ptr_limit = -off; | |
4279 | return 0; | |
4280 | case PTR_TO_MAP_VALUE: | |
4281 | if (mask_to_left) { | |
4282 | *ptr_limit = ptr_reg->umax_value + ptr_reg->off; | |
4283 | } else { | |
4284 | off = ptr_reg->smin_value + ptr_reg->off; | |
4285 | *ptr_limit = ptr_reg->map_ptr->value_size - off; | |
4286 | } | |
4287 | return 0; | |
4288 | default: | |
4289 | return -EINVAL; | |
4290 | } | |
4291 | } | |
4292 | ||
d3bd7413 DB |
4293 | static bool can_skip_alu_sanitation(const struct bpf_verifier_env *env, |
4294 | const struct bpf_insn *insn) | |
4295 | { | |
4296 | return env->allow_ptr_leaks || BPF_SRC(insn->code) == BPF_K; | |
4297 | } | |
4298 | ||
4299 | static int update_alu_sanitation_state(struct bpf_insn_aux_data *aux, | |
4300 | u32 alu_state, u32 alu_limit) | |
4301 | { | |
4302 | /* If we arrived here from different branches with different | |
4303 | * state or limits to sanitize, then this won't work. | |
4304 | */ | |
4305 | if (aux->alu_state && | |
4306 | (aux->alu_state != alu_state || | |
4307 | aux->alu_limit != alu_limit)) | |
4308 | return -EACCES; | |
4309 | ||
4310 | /* Corresponding fixup done in fixup_bpf_calls(). */ | |
4311 | aux->alu_state = alu_state; | |
4312 | aux->alu_limit = alu_limit; | |
4313 | return 0; | |
4314 | } | |
4315 | ||
4316 | static int sanitize_val_alu(struct bpf_verifier_env *env, | |
4317 | struct bpf_insn *insn) | |
4318 | { | |
4319 | struct bpf_insn_aux_data *aux = cur_aux(env); | |
4320 | ||
4321 | if (can_skip_alu_sanitation(env, insn)) | |
4322 | return 0; | |
4323 | ||
4324 | return update_alu_sanitation_state(aux, BPF_ALU_NON_POINTER, 0); | |
4325 | } | |
4326 | ||
979d63d5 DB |
4327 | static int sanitize_ptr_alu(struct bpf_verifier_env *env, |
4328 | struct bpf_insn *insn, | |
4329 | const struct bpf_reg_state *ptr_reg, | |
4330 | struct bpf_reg_state *dst_reg, | |
4331 | bool off_is_neg) | |
4332 | { | |
4333 | struct bpf_verifier_state *vstate = env->cur_state; | |
4334 | struct bpf_insn_aux_data *aux = cur_aux(env); | |
4335 | bool ptr_is_dst_reg = ptr_reg == dst_reg; | |
4336 | u8 opcode = BPF_OP(insn->code); | |
4337 | u32 alu_state, alu_limit; | |
4338 | struct bpf_reg_state tmp; | |
4339 | bool ret; | |
4340 | ||
d3bd7413 | 4341 | if (can_skip_alu_sanitation(env, insn)) |
979d63d5 DB |
4342 | return 0; |
4343 | ||
4344 | /* We already marked aux for masking from non-speculative | |
4345 | * paths, thus we got here in the first place. We only care | |
4346 | * to explore bad access from here. | |
4347 | */ | |
4348 | if (vstate->speculative) | |
4349 | goto do_sim; | |
4350 | ||
4351 | alu_state = off_is_neg ? BPF_ALU_NEG_VALUE : 0; | |
4352 | alu_state |= ptr_is_dst_reg ? | |
4353 | BPF_ALU_SANITIZE_SRC : BPF_ALU_SANITIZE_DST; | |
4354 | ||
4355 | if (retrieve_ptr_limit(ptr_reg, &alu_limit, opcode, off_is_neg)) | |
4356 | return 0; | |
d3bd7413 | 4357 | if (update_alu_sanitation_state(aux, alu_state, alu_limit)) |
979d63d5 | 4358 | return -EACCES; |
979d63d5 DB |
4359 | do_sim: |
4360 | /* Simulate and find potential out-of-bounds access under | |
4361 | * speculative execution from truncation as a result of | |
4362 | * masking when off was not within expected range. If off | |
4363 | * sits in dst, then we temporarily need to move ptr there | |
4364 | * to simulate dst (== 0) +/-= ptr. Needed, for example, | |
4365 | * for cases where we use K-based arithmetic in one direction | |
4366 | * and truncated reg-based in the other in order to explore | |
4367 | * bad access. | |
4368 | */ | |
4369 | if (!ptr_is_dst_reg) { | |
4370 | tmp = *dst_reg; | |
4371 | *dst_reg = *ptr_reg; | |
4372 | } | |
4373 | ret = push_stack(env, env->insn_idx + 1, env->insn_idx, true); | |
0803278b | 4374 | if (!ptr_is_dst_reg && ret) |
979d63d5 DB |
4375 | *dst_reg = tmp; |
4376 | return !ret ? -EFAULT : 0; | |
4377 | } | |
4378 | ||
f1174f77 | 4379 | /* Handles arithmetic on a pointer and a scalar: computes new min/max and var_off. |
f1174f77 EC |
4380 | * Caller should also handle BPF_MOV case separately. |
4381 | * If we return -EACCES, caller may want to try again treating pointer as a | |
4382 | * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks. | |
4383 | */ | |
4384 | static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, | |
4385 | struct bpf_insn *insn, | |
4386 | const struct bpf_reg_state *ptr_reg, | |
4387 | const struct bpf_reg_state *off_reg) | |
969bf05e | 4388 | { |
f4d7e40a AS |
4389 | struct bpf_verifier_state *vstate = env->cur_state; |
4390 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
4391 | struct bpf_reg_state *regs = state->regs, *dst_reg; | |
f1174f77 | 4392 | bool known = tnum_is_const(off_reg->var_off); |
b03c9f9f EC |
4393 | s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value, |
4394 | smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value; | |
4395 | u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value, | |
4396 | umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value; | |
9d7eceed | 4397 | u32 dst = insn->dst_reg, src = insn->src_reg; |
969bf05e | 4398 | u8 opcode = BPF_OP(insn->code); |
979d63d5 | 4399 | int ret; |
969bf05e | 4400 | |
f1174f77 | 4401 | dst_reg = ®s[dst]; |
969bf05e | 4402 | |
6f16101e DB |
4403 | if ((known && (smin_val != smax_val || umin_val != umax_val)) || |
4404 | smin_val > smax_val || umin_val > umax_val) { | |
4405 | /* Taint dst register if offset had invalid bounds derived from | |
4406 | * e.g. dead branches. | |
4407 | */ | |
4408 | __mark_reg_unknown(dst_reg); | |
4409 | return 0; | |
f1174f77 EC |
4410 | } |
4411 | ||
4412 | if (BPF_CLASS(insn->code) != BPF_ALU64) { | |
4413 | /* 32-bit ALU ops on pointers produce (meaningless) scalars */ | |
82abbf8d AS |
4414 | verbose(env, |
4415 | "R%d 32-bit pointer arithmetic prohibited\n", | |
4416 | dst); | |
f1174f77 | 4417 | return -EACCES; |
969bf05e AS |
4418 | } |
4419 | ||
aad2eeaf JS |
4420 | switch (ptr_reg->type) { |
4421 | case PTR_TO_MAP_VALUE_OR_NULL: | |
4422 | verbose(env, "R%d pointer arithmetic on %s prohibited, null-check it first\n", | |
4423 | dst, reg_type_str[ptr_reg->type]); | |
f1174f77 | 4424 | return -EACCES; |
aad2eeaf JS |
4425 | case CONST_PTR_TO_MAP: |
4426 | case PTR_TO_PACKET_END: | |
c64b7983 JS |
4427 | case PTR_TO_SOCKET: |
4428 | case PTR_TO_SOCKET_OR_NULL: | |
46f8bc92 MKL |
4429 | case PTR_TO_SOCK_COMMON: |
4430 | case PTR_TO_SOCK_COMMON_OR_NULL: | |
655a51e5 MKL |
4431 | case PTR_TO_TCP_SOCK: |
4432 | case PTR_TO_TCP_SOCK_OR_NULL: | |
fada7fdc | 4433 | case PTR_TO_XDP_SOCK: |
aad2eeaf JS |
4434 | verbose(env, "R%d pointer arithmetic on %s prohibited\n", |
4435 | dst, reg_type_str[ptr_reg->type]); | |
f1174f77 | 4436 | return -EACCES; |
9d7eceed DB |
4437 | case PTR_TO_MAP_VALUE: |
4438 | if (!env->allow_ptr_leaks && !known && (smin_val < 0) != (smax_val < 0)) { | |
4439 | verbose(env, "R%d has unknown scalar with mixed signed bounds, pointer arithmetic with it prohibited for !root\n", | |
4440 | off_reg == dst_reg ? dst : src); | |
4441 | return -EACCES; | |
4442 | } | |
4443 | /* fall-through */ | |
aad2eeaf JS |
4444 | default: |
4445 | break; | |
f1174f77 EC |
4446 | } |
4447 | ||
4448 | /* In case of 'scalar += pointer', dst_reg inherits pointer type and id. | |
4449 | * The id may be overwritten later if we create a new variable offset. | |
969bf05e | 4450 | */ |
f1174f77 EC |
4451 | dst_reg->type = ptr_reg->type; |
4452 | dst_reg->id = ptr_reg->id; | |
969bf05e | 4453 | |
bb7f0f98 AS |
4454 | if (!check_reg_sane_offset(env, off_reg, ptr_reg->type) || |
4455 | !check_reg_sane_offset(env, ptr_reg, ptr_reg->type)) | |
4456 | return -EINVAL; | |
4457 | ||
f1174f77 EC |
4458 | switch (opcode) { |
4459 | case BPF_ADD: | |
979d63d5 DB |
4460 | ret = sanitize_ptr_alu(env, insn, ptr_reg, dst_reg, smin_val < 0); |
4461 | if (ret < 0) { | |
4462 | verbose(env, "R%d tried to add from different maps or paths\n", dst); | |
4463 | return ret; | |
4464 | } | |
f1174f77 EC |
4465 | /* We can take a fixed offset as long as it doesn't overflow |
4466 | * the s32 'off' field | |
969bf05e | 4467 | */ |
b03c9f9f EC |
4468 | if (known && (ptr_reg->off + smin_val == |
4469 | (s64)(s32)(ptr_reg->off + smin_val))) { | |
f1174f77 | 4470 | /* pointer += K. Accumulate it into fixed offset */ |
b03c9f9f EC |
4471 | dst_reg->smin_value = smin_ptr; |
4472 | dst_reg->smax_value = smax_ptr; | |
4473 | dst_reg->umin_value = umin_ptr; | |
4474 | dst_reg->umax_value = umax_ptr; | |
f1174f77 | 4475 | dst_reg->var_off = ptr_reg->var_off; |
b03c9f9f | 4476 | dst_reg->off = ptr_reg->off + smin_val; |
0962590e | 4477 | dst_reg->raw = ptr_reg->raw; |
f1174f77 EC |
4478 | break; |
4479 | } | |
f1174f77 EC |
4480 | /* A new variable offset is created. Note that off_reg->off |
4481 | * == 0, since it's a scalar. | |
4482 | * dst_reg gets the pointer type and since some positive | |
4483 | * integer value was added to the pointer, give it a new 'id' | |
4484 | * if it's a PTR_TO_PACKET. | |
4485 | * this creates a new 'base' pointer, off_reg (variable) gets | |
4486 | * added into the variable offset, and we copy the fixed offset | |
4487 | * from ptr_reg. | |
969bf05e | 4488 | */ |
b03c9f9f EC |
4489 | if (signed_add_overflows(smin_ptr, smin_val) || |
4490 | signed_add_overflows(smax_ptr, smax_val)) { | |
4491 | dst_reg->smin_value = S64_MIN; | |
4492 | dst_reg->smax_value = S64_MAX; | |
4493 | } else { | |
4494 | dst_reg->smin_value = smin_ptr + smin_val; | |
4495 | dst_reg->smax_value = smax_ptr + smax_val; | |
4496 | } | |
4497 | if (umin_ptr + umin_val < umin_ptr || | |
4498 | umax_ptr + umax_val < umax_ptr) { | |
4499 | dst_reg->umin_value = 0; | |
4500 | dst_reg->umax_value = U64_MAX; | |
4501 | } else { | |
4502 | dst_reg->umin_value = umin_ptr + umin_val; | |
4503 | dst_reg->umax_value = umax_ptr + umax_val; | |
4504 | } | |
f1174f77 EC |
4505 | dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off); |
4506 | dst_reg->off = ptr_reg->off; | |
0962590e | 4507 | dst_reg->raw = ptr_reg->raw; |
de8f3a83 | 4508 | if (reg_is_pkt_pointer(ptr_reg)) { |
f1174f77 EC |
4509 | dst_reg->id = ++env->id_gen; |
4510 | /* something was added to pkt_ptr, set range to zero */ | |
0962590e | 4511 | dst_reg->raw = 0; |
f1174f77 EC |
4512 | } |
4513 | break; | |
4514 | case BPF_SUB: | |
979d63d5 DB |
4515 | ret = sanitize_ptr_alu(env, insn, ptr_reg, dst_reg, smin_val < 0); |
4516 | if (ret < 0) { | |
4517 | verbose(env, "R%d tried to sub from different maps or paths\n", dst); | |
4518 | return ret; | |
4519 | } | |
f1174f77 EC |
4520 | if (dst_reg == off_reg) { |
4521 | /* scalar -= pointer. Creates an unknown scalar */ | |
82abbf8d AS |
4522 | verbose(env, "R%d tried to subtract pointer from scalar\n", |
4523 | dst); | |
f1174f77 EC |
4524 | return -EACCES; |
4525 | } | |
4526 | /* We don't allow subtraction from FP, because (according to | |
4527 | * test_verifier.c test "invalid fp arithmetic", JITs might not | |
4528 | * be able to deal with it. | |
969bf05e | 4529 | */ |
f1174f77 | 4530 | if (ptr_reg->type == PTR_TO_STACK) { |
82abbf8d AS |
4531 | verbose(env, "R%d subtraction from stack pointer prohibited\n", |
4532 | dst); | |
f1174f77 EC |
4533 | return -EACCES; |
4534 | } | |
b03c9f9f EC |
4535 | if (known && (ptr_reg->off - smin_val == |
4536 | (s64)(s32)(ptr_reg->off - smin_val))) { | |
f1174f77 | 4537 | /* pointer -= K. Subtract it from fixed offset */ |
b03c9f9f EC |
4538 | dst_reg->smin_value = smin_ptr; |
4539 | dst_reg->smax_value = smax_ptr; | |
4540 | dst_reg->umin_value = umin_ptr; | |
4541 | dst_reg->umax_value = umax_ptr; | |
f1174f77 EC |
4542 | dst_reg->var_off = ptr_reg->var_off; |
4543 | dst_reg->id = ptr_reg->id; | |
b03c9f9f | 4544 | dst_reg->off = ptr_reg->off - smin_val; |
0962590e | 4545 | dst_reg->raw = ptr_reg->raw; |
f1174f77 EC |
4546 | break; |
4547 | } | |
f1174f77 EC |
4548 | /* A new variable offset is created. If the subtrahend is known |
4549 | * nonnegative, then any reg->range we had before is still good. | |
969bf05e | 4550 | */ |
b03c9f9f EC |
4551 | if (signed_sub_overflows(smin_ptr, smax_val) || |
4552 | signed_sub_overflows(smax_ptr, smin_val)) { | |
4553 | /* Overflow possible, we know nothing */ | |
4554 | dst_reg->smin_value = S64_MIN; | |
4555 | dst_reg->smax_value = S64_MAX; | |
4556 | } else { | |
4557 | dst_reg->smin_value = smin_ptr - smax_val; | |
4558 | dst_reg->smax_value = smax_ptr - smin_val; | |
4559 | } | |
4560 | if (umin_ptr < umax_val) { | |
4561 | /* Overflow possible, we know nothing */ | |
4562 | dst_reg->umin_value = 0; | |
4563 | dst_reg->umax_value = U64_MAX; | |
4564 | } else { | |
4565 | /* Cannot overflow (as long as bounds are consistent) */ | |
4566 | dst_reg->umin_value = umin_ptr - umax_val; | |
4567 | dst_reg->umax_value = umax_ptr - umin_val; | |
4568 | } | |
f1174f77 EC |
4569 | dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off); |
4570 | dst_reg->off = ptr_reg->off; | |
0962590e | 4571 | dst_reg->raw = ptr_reg->raw; |
de8f3a83 | 4572 | if (reg_is_pkt_pointer(ptr_reg)) { |
f1174f77 EC |
4573 | dst_reg->id = ++env->id_gen; |
4574 | /* something was added to pkt_ptr, set range to zero */ | |
b03c9f9f | 4575 | if (smin_val < 0) |
0962590e | 4576 | dst_reg->raw = 0; |
43188702 | 4577 | } |
f1174f77 EC |
4578 | break; |
4579 | case BPF_AND: | |
4580 | case BPF_OR: | |
4581 | case BPF_XOR: | |
82abbf8d AS |
4582 | /* bitwise ops on pointers are troublesome, prohibit. */ |
4583 | verbose(env, "R%d bitwise operator %s on pointer prohibited\n", | |
4584 | dst, bpf_alu_string[opcode >> 4]); | |
f1174f77 EC |
4585 | return -EACCES; |
4586 | default: | |
4587 | /* other operators (e.g. MUL,LSH) produce non-pointer results */ | |
82abbf8d AS |
4588 | verbose(env, "R%d pointer arithmetic with %s operator prohibited\n", |
4589 | dst, bpf_alu_string[opcode >> 4]); | |
f1174f77 | 4590 | return -EACCES; |
43188702 JF |
4591 | } |
4592 | ||
bb7f0f98 AS |
4593 | if (!check_reg_sane_offset(env, dst_reg, ptr_reg->type)) |
4594 | return -EINVAL; | |
4595 | ||
b03c9f9f EC |
4596 | __update_reg_bounds(dst_reg); |
4597 | __reg_deduce_bounds(dst_reg); | |
4598 | __reg_bound_offset(dst_reg); | |
0d6303db DB |
4599 | |
4600 | /* For unprivileged we require that resulting offset must be in bounds | |
4601 | * in order to be able to sanitize access later on. | |
4602 | */ | |
e4298d25 DB |
4603 | if (!env->allow_ptr_leaks) { |
4604 | if (dst_reg->type == PTR_TO_MAP_VALUE && | |
4605 | check_map_access(env, dst, dst_reg->off, 1, false)) { | |
4606 | verbose(env, "R%d pointer arithmetic of map value goes out of range, " | |
4607 | "prohibited for !root\n", dst); | |
4608 | return -EACCES; | |
4609 | } else if (dst_reg->type == PTR_TO_STACK && | |
4610 | check_stack_access(env, dst_reg, dst_reg->off + | |
4611 | dst_reg->var_off.value, 1)) { | |
4612 | verbose(env, "R%d stack pointer arithmetic goes out of range, " | |
4613 | "prohibited for !root\n", dst); | |
4614 | return -EACCES; | |
4615 | } | |
0d6303db DB |
4616 | } |
4617 | ||
43188702 JF |
4618 | return 0; |
4619 | } | |
4620 | ||
468f6eaf JH |
4621 | /* WARNING: This function does calculations on 64-bit values, but the actual |
4622 | * execution may occur on 32-bit values. Therefore, things like bitshifts | |
4623 | * need extra checks in the 32-bit case. | |
4624 | */ | |
f1174f77 EC |
4625 | static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env, |
4626 | struct bpf_insn *insn, | |
4627 | struct bpf_reg_state *dst_reg, | |
4628 | struct bpf_reg_state src_reg) | |
969bf05e | 4629 | { |
638f5b90 | 4630 | struct bpf_reg_state *regs = cur_regs(env); |
48461135 | 4631 | u8 opcode = BPF_OP(insn->code); |
f1174f77 | 4632 | bool src_known, dst_known; |
b03c9f9f EC |
4633 | s64 smin_val, smax_val; |
4634 | u64 umin_val, umax_val; | |
468f6eaf | 4635 | u64 insn_bitness = (BPF_CLASS(insn->code) == BPF_ALU64) ? 64 : 32; |
d3bd7413 DB |
4636 | u32 dst = insn->dst_reg; |
4637 | int ret; | |
48461135 | 4638 | |
b799207e JH |
4639 | if (insn_bitness == 32) { |
4640 | /* Relevant for 32-bit RSH: Information can propagate towards | |
4641 | * LSB, so it isn't sufficient to only truncate the output to | |
4642 | * 32 bits. | |
4643 | */ | |
4644 | coerce_reg_to_size(dst_reg, 4); | |
4645 | coerce_reg_to_size(&src_reg, 4); | |
4646 | } | |
4647 | ||
b03c9f9f EC |
4648 | smin_val = src_reg.smin_value; |
4649 | smax_val = src_reg.smax_value; | |
4650 | umin_val = src_reg.umin_value; | |
4651 | umax_val = src_reg.umax_value; | |
f1174f77 EC |
4652 | src_known = tnum_is_const(src_reg.var_off); |
4653 | dst_known = tnum_is_const(dst_reg->var_off); | |
f23cc643 | 4654 | |
6f16101e DB |
4655 | if ((src_known && (smin_val != smax_val || umin_val != umax_val)) || |
4656 | smin_val > smax_val || umin_val > umax_val) { | |
4657 | /* Taint dst register if offset had invalid bounds derived from | |
4658 | * e.g. dead branches. | |
4659 | */ | |
4660 | __mark_reg_unknown(dst_reg); | |
4661 | return 0; | |
4662 | } | |
4663 | ||
bb7f0f98 AS |
4664 | if (!src_known && |
4665 | opcode != BPF_ADD && opcode != BPF_SUB && opcode != BPF_AND) { | |
4666 | __mark_reg_unknown(dst_reg); | |
4667 | return 0; | |
4668 | } | |
4669 | ||
48461135 JB |
4670 | switch (opcode) { |
4671 | case BPF_ADD: | |
d3bd7413 DB |
4672 | ret = sanitize_val_alu(env, insn); |
4673 | if (ret < 0) { | |
4674 | verbose(env, "R%d tried to add from different pointers or scalars\n", dst); | |
4675 | return ret; | |
4676 | } | |
b03c9f9f EC |
4677 | if (signed_add_overflows(dst_reg->smin_value, smin_val) || |
4678 | signed_add_overflows(dst_reg->smax_value, smax_val)) { | |
4679 | dst_reg->smin_value = S64_MIN; | |
4680 | dst_reg->smax_value = S64_MAX; | |
4681 | } else { | |
4682 | dst_reg->smin_value += smin_val; | |
4683 | dst_reg->smax_value += smax_val; | |
4684 | } | |
4685 | if (dst_reg->umin_value + umin_val < umin_val || | |
4686 | dst_reg->umax_value + umax_val < umax_val) { | |
4687 | dst_reg->umin_value = 0; | |
4688 | dst_reg->umax_value = U64_MAX; | |
4689 | } else { | |
4690 | dst_reg->umin_value += umin_val; | |
4691 | dst_reg->umax_value += umax_val; | |
4692 | } | |
f1174f77 | 4693 | dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off); |
48461135 JB |
4694 | break; |
4695 | case BPF_SUB: | |
d3bd7413 DB |
4696 | ret = sanitize_val_alu(env, insn); |
4697 | if (ret < 0) { | |
4698 | verbose(env, "R%d tried to sub from different pointers or scalars\n", dst); | |
4699 | return ret; | |
4700 | } | |
b03c9f9f EC |
4701 | if (signed_sub_overflows(dst_reg->smin_value, smax_val) || |
4702 | signed_sub_overflows(dst_reg->smax_value, smin_val)) { | |
4703 | /* Overflow possible, we know nothing */ | |
4704 | dst_reg->smin_value = S64_MIN; | |
4705 | dst_reg->smax_value = S64_MAX; | |
4706 | } else { | |
4707 | dst_reg->smin_value -= smax_val; | |
4708 | dst_reg->smax_value -= smin_val; | |
4709 | } | |
4710 | if (dst_reg->umin_value < umax_val) { | |
4711 | /* Overflow possible, we know nothing */ | |
4712 | dst_reg->umin_value = 0; | |
4713 | dst_reg->umax_value = U64_MAX; | |
4714 | } else { | |
4715 | /* Cannot overflow (as long as bounds are consistent) */ | |
4716 | dst_reg->umin_value -= umax_val; | |
4717 | dst_reg->umax_value -= umin_val; | |
4718 | } | |
f1174f77 | 4719 | dst_reg->var_off = tnum_sub(dst_reg->var_off, src_reg.var_off); |
48461135 JB |
4720 | break; |
4721 | case BPF_MUL: | |
b03c9f9f EC |
4722 | dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off); |
4723 | if (smin_val < 0 || dst_reg->smin_value < 0) { | |
f1174f77 | 4724 | /* Ain't nobody got time to multiply that sign */ |
b03c9f9f EC |
4725 | __mark_reg_unbounded(dst_reg); |
4726 | __update_reg_bounds(dst_reg); | |
f1174f77 EC |
4727 | break; |
4728 | } | |
b03c9f9f EC |
4729 | /* Both values are positive, so we can work with unsigned and |
4730 | * copy the result to signed (unless it exceeds S64_MAX). | |
f1174f77 | 4731 | */ |
b03c9f9f EC |
4732 | if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) { |
4733 | /* Potential overflow, we know nothing */ | |
4734 | __mark_reg_unbounded(dst_reg); | |
4735 | /* (except what we can learn from the var_off) */ | |
4736 | __update_reg_bounds(dst_reg); | |
4737 | break; | |
4738 | } | |
4739 | dst_reg->umin_value *= umin_val; | |
4740 | dst_reg->umax_value *= umax_val; | |
4741 | if (dst_reg->umax_value > S64_MAX) { | |
4742 | /* Overflow possible, we know nothing */ | |
4743 | dst_reg->smin_value = S64_MIN; | |
4744 | dst_reg->smax_value = S64_MAX; | |
4745 | } else { | |
4746 | dst_reg->smin_value = dst_reg->umin_value; | |
4747 | dst_reg->smax_value = dst_reg->umax_value; | |
4748 | } | |
48461135 JB |
4749 | break; |
4750 | case BPF_AND: | |
f1174f77 | 4751 | if (src_known && dst_known) { |
b03c9f9f EC |
4752 | __mark_reg_known(dst_reg, dst_reg->var_off.value & |
4753 | src_reg.var_off.value); | |
f1174f77 EC |
4754 | break; |
4755 | } | |
b03c9f9f EC |
4756 | /* We get our minimum from the var_off, since that's inherently |
4757 | * bitwise. Our maximum is the minimum of the operands' maxima. | |
f23cc643 | 4758 | */ |
f1174f77 | 4759 | dst_reg->var_off = tnum_and(dst_reg->var_off, src_reg.var_off); |
b03c9f9f EC |
4760 | dst_reg->umin_value = dst_reg->var_off.value; |
4761 | dst_reg->umax_value = min(dst_reg->umax_value, umax_val); | |
4762 | if (dst_reg->smin_value < 0 || smin_val < 0) { | |
4763 | /* Lose signed bounds when ANDing negative numbers, | |
4764 | * ain't nobody got time for that. | |
4765 | */ | |
4766 | dst_reg->smin_value = S64_MIN; | |
4767 | dst_reg->smax_value = S64_MAX; | |
4768 | } else { | |
4769 | /* ANDing two positives gives a positive, so safe to | |
4770 | * cast result into s64. | |
4771 | */ | |
4772 | dst_reg->smin_value = dst_reg->umin_value; | |
4773 | dst_reg->smax_value = dst_reg->umax_value; | |
4774 | } | |
4775 | /* We may learn something more from the var_off */ | |
4776 | __update_reg_bounds(dst_reg); | |
f1174f77 EC |
4777 | break; |
4778 | case BPF_OR: | |
4779 | if (src_known && dst_known) { | |
b03c9f9f EC |
4780 | __mark_reg_known(dst_reg, dst_reg->var_off.value | |
4781 | src_reg.var_off.value); | |
f1174f77 EC |
4782 | break; |
4783 | } | |
b03c9f9f EC |
4784 | /* We get our maximum from the var_off, and our minimum is the |
4785 | * maximum of the operands' minima | |
f1174f77 EC |
4786 | */ |
4787 | dst_reg->var_off = tnum_or(dst_reg->var_off, src_reg.var_off); | |
b03c9f9f EC |
4788 | dst_reg->umin_value = max(dst_reg->umin_value, umin_val); |
4789 | dst_reg->umax_value = dst_reg->var_off.value | | |
4790 | dst_reg->var_off.mask; | |
4791 | if (dst_reg->smin_value < 0 || smin_val < 0) { | |
4792 | /* Lose signed bounds when ORing negative numbers, | |
4793 | * ain't nobody got time for that. | |
4794 | */ | |
4795 | dst_reg->smin_value = S64_MIN; | |
4796 | dst_reg->smax_value = S64_MAX; | |
f1174f77 | 4797 | } else { |
b03c9f9f EC |
4798 | /* ORing two positives gives a positive, so safe to |
4799 | * cast result into s64. | |
4800 | */ | |
4801 | dst_reg->smin_value = dst_reg->umin_value; | |
4802 | dst_reg->smax_value = dst_reg->umax_value; | |
f1174f77 | 4803 | } |
b03c9f9f EC |
4804 | /* We may learn something more from the var_off */ |
4805 | __update_reg_bounds(dst_reg); | |
48461135 JB |
4806 | break; |
4807 | case BPF_LSH: | |
468f6eaf JH |
4808 | if (umax_val >= insn_bitness) { |
4809 | /* Shifts greater than 31 or 63 are undefined. | |
4810 | * This includes shifts by a negative number. | |
b03c9f9f | 4811 | */ |
61bd5218 | 4812 | mark_reg_unknown(env, regs, insn->dst_reg); |
f1174f77 EC |
4813 | break; |
4814 | } | |
b03c9f9f EC |
4815 | /* We lose all sign bit information (except what we can pick |
4816 | * up from var_off) | |
48461135 | 4817 | */ |
b03c9f9f EC |
4818 | dst_reg->smin_value = S64_MIN; |
4819 | dst_reg->smax_value = S64_MAX; | |
4820 | /* If we might shift our top bit out, then we know nothing */ | |
4821 | if (dst_reg->umax_value > 1ULL << (63 - umax_val)) { | |
4822 | dst_reg->umin_value = 0; | |
4823 | dst_reg->umax_value = U64_MAX; | |
d1174416 | 4824 | } else { |
b03c9f9f EC |
4825 | dst_reg->umin_value <<= umin_val; |
4826 | dst_reg->umax_value <<= umax_val; | |
d1174416 | 4827 | } |
afbe1a5b | 4828 | dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val); |
b03c9f9f EC |
4829 | /* We may learn something more from the var_off */ |
4830 | __update_reg_bounds(dst_reg); | |
48461135 JB |
4831 | break; |
4832 | case BPF_RSH: | |
468f6eaf JH |
4833 | if (umax_val >= insn_bitness) { |
4834 | /* Shifts greater than 31 or 63 are undefined. | |
4835 | * This includes shifts by a negative number. | |
b03c9f9f | 4836 | */ |
61bd5218 | 4837 | mark_reg_unknown(env, regs, insn->dst_reg); |
f1174f77 EC |
4838 | break; |
4839 | } | |
4374f256 EC |
4840 | /* BPF_RSH is an unsigned shift. If the value in dst_reg might |
4841 | * be negative, then either: | |
4842 | * 1) src_reg might be zero, so the sign bit of the result is | |
4843 | * unknown, so we lose our signed bounds | |
4844 | * 2) it's known negative, thus the unsigned bounds capture the | |
4845 | * signed bounds | |
4846 | * 3) the signed bounds cross zero, so they tell us nothing | |
4847 | * about the result | |
4848 | * If the value in dst_reg is known nonnegative, then again the | |
4849 | * unsigned bounts capture the signed bounds. | |
4850 | * Thus, in all cases it suffices to blow away our signed bounds | |
4851 | * and rely on inferring new ones from the unsigned bounds and | |
4852 | * var_off of the result. | |
4853 | */ | |
4854 | dst_reg->smin_value = S64_MIN; | |
4855 | dst_reg->smax_value = S64_MAX; | |
afbe1a5b | 4856 | dst_reg->var_off = tnum_rshift(dst_reg->var_off, umin_val); |
b03c9f9f EC |
4857 | dst_reg->umin_value >>= umax_val; |
4858 | dst_reg->umax_value >>= umin_val; | |
4859 | /* We may learn something more from the var_off */ | |
4860 | __update_reg_bounds(dst_reg); | |
48461135 | 4861 | break; |
9cbe1f5a YS |
4862 | case BPF_ARSH: |
4863 | if (umax_val >= insn_bitness) { | |
4864 | /* Shifts greater than 31 or 63 are undefined. | |
4865 | * This includes shifts by a negative number. | |
4866 | */ | |
4867 | mark_reg_unknown(env, regs, insn->dst_reg); | |
4868 | break; | |
4869 | } | |
4870 | ||
4871 | /* Upon reaching here, src_known is true and | |
4872 | * umax_val is equal to umin_val. | |
4873 | */ | |
4874 | dst_reg->smin_value >>= umin_val; | |
4875 | dst_reg->smax_value >>= umin_val; | |
4876 | dst_reg->var_off = tnum_arshift(dst_reg->var_off, umin_val); | |
4877 | ||
4878 | /* blow away the dst_reg umin_value/umax_value and rely on | |
4879 | * dst_reg var_off to refine the result. | |
4880 | */ | |
4881 | dst_reg->umin_value = 0; | |
4882 | dst_reg->umax_value = U64_MAX; | |
4883 | __update_reg_bounds(dst_reg); | |
4884 | break; | |
48461135 | 4885 | default: |
61bd5218 | 4886 | mark_reg_unknown(env, regs, insn->dst_reg); |
48461135 JB |
4887 | break; |
4888 | } | |
4889 | ||
468f6eaf JH |
4890 | if (BPF_CLASS(insn->code) != BPF_ALU64) { |
4891 | /* 32-bit ALU ops are (32,32)->32 */ | |
4892 | coerce_reg_to_size(dst_reg, 4); | |
468f6eaf JH |
4893 | } |
4894 | ||
b03c9f9f EC |
4895 | __reg_deduce_bounds(dst_reg); |
4896 | __reg_bound_offset(dst_reg); | |
f1174f77 EC |
4897 | return 0; |
4898 | } | |
4899 | ||
4900 | /* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max | |
4901 | * and var_off. | |
4902 | */ | |
4903 | static int adjust_reg_min_max_vals(struct bpf_verifier_env *env, | |
4904 | struct bpf_insn *insn) | |
4905 | { | |
f4d7e40a AS |
4906 | struct bpf_verifier_state *vstate = env->cur_state; |
4907 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
4908 | struct bpf_reg_state *regs = state->regs, *dst_reg, *src_reg; | |
f1174f77 EC |
4909 | struct bpf_reg_state *ptr_reg = NULL, off_reg = {0}; |
4910 | u8 opcode = BPF_OP(insn->code); | |
b5dc0163 | 4911 | int err; |
f1174f77 EC |
4912 | |
4913 | dst_reg = ®s[insn->dst_reg]; | |
f1174f77 EC |
4914 | src_reg = NULL; |
4915 | if (dst_reg->type != SCALAR_VALUE) | |
4916 | ptr_reg = dst_reg; | |
4917 | if (BPF_SRC(insn->code) == BPF_X) { | |
4918 | src_reg = ®s[insn->src_reg]; | |
f1174f77 EC |
4919 | if (src_reg->type != SCALAR_VALUE) { |
4920 | if (dst_reg->type != SCALAR_VALUE) { | |
4921 | /* Combining two pointers by any ALU op yields | |
82abbf8d AS |
4922 | * an arbitrary scalar. Disallow all math except |
4923 | * pointer subtraction | |
f1174f77 | 4924 | */ |
dd066823 | 4925 | if (opcode == BPF_SUB && env->allow_ptr_leaks) { |
82abbf8d AS |
4926 | mark_reg_unknown(env, regs, insn->dst_reg); |
4927 | return 0; | |
f1174f77 | 4928 | } |
82abbf8d AS |
4929 | verbose(env, "R%d pointer %s pointer prohibited\n", |
4930 | insn->dst_reg, | |
4931 | bpf_alu_string[opcode >> 4]); | |
4932 | return -EACCES; | |
f1174f77 EC |
4933 | } else { |
4934 | /* scalar += pointer | |
4935 | * This is legal, but we have to reverse our | |
4936 | * src/dest handling in computing the range | |
4937 | */ | |
b5dc0163 AS |
4938 | err = mark_chain_precision(env, insn->dst_reg); |
4939 | if (err) | |
4940 | return err; | |
82abbf8d AS |
4941 | return adjust_ptr_min_max_vals(env, insn, |
4942 | src_reg, dst_reg); | |
f1174f77 EC |
4943 | } |
4944 | } else if (ptr_reg) { | |
4945 | /* pointer += scalar */ | |
b5dc0163 AS |
4946 | err = mark_chain_precision(env, insn->src_reg); |
4947 | if (err) | |
4948 | return err; | |
82abbf8d AS |
4949 | return adjust_ptr_min_max_vals(env, insn, |
4950 | dst_reg, src_reg); | |
f1174f77 EC |
4951 | } |
4952 | } else { | |
4953 | /* Pretend the src is a reg with a known value, since we only | |
4954 | * need to be able to read from this state. | |
4955 | */ | |
4956 | off_reg.type = SCALAR_VALUE; | |
b03c9f9f | 4957 | __mark_reg_known(&off_reg, insn->imm); |
f1174f77 | 4958 | src_reg = &off_reg; |
82abbf8d AS |
4959 | if (ptr_reg) /* pointer += K */ |
4960 | return adjust_ptr_min_max_vals(env, insn, | |
4961 | ptr_reg, src_reg); | |
f1174f77 EC |
4962 | } |
4963 | ||
4964 | /* Got here implies adding two SCALAR_VALUEs */ | |
4965 | if (WARN_ON_ONCE(ptr_reg)) { | |
f4d7e40a | 4966 | print_verifier_state(env, state); |
61bd5218 | 4967 | verbose(env, "verifier internal error: unexpected ptr_reg\n"); |
f1174f77 EC |
4968 | return -EINVAL; |
4969 | } | |
4970 | if (WARN_ON(!src_reg)) { | |
f4d7e40a | 4971 | print_verifier_state(env, state); |
61bd5218 | 4972 | verbose(env, "verifier internal error: no src_reg\n"); |
f1174f77 EC |
4973 | return -EINVAL; |
4974 | } | |
4975 | return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg); | |
48461135 JB |
4976 | } |
4977 | ||
17a52670 | 4978 | /* check validity of 32-bit and 64-bit arithmetic operations */ |
58e2af8b | 4979 | static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn) |
17a52670 | 4980 | { |
638f5b90 | 4981 | struct bpf_reg_state *regs = cur_regs(env); |
17a52670 AS |
4982 | u8 opcode = BPF_OP(insn->code); |
4983 | int err; | |
4984 | ||
4985 | if (opcode == BPF_END || opcode == BPF_NEG) { | |
4986 | if (opcode == BPF_NEG) { | |
4987 | if (BPF_SRC(insn->code) != 0 || | |
4988 | insn->src_reg != BPF_REG_0 || | |
4989 | insn->off != 0 || insn->imm != 0) { | |
61bd5218 | 4990 | verbose(env, "BPF_NEG uses reserved fields\n"); |
17a52670 AS |
4991 | return -EINVAL; |
4992 | } | |
4993 | } else { | |
4994 | if (insn->src_reg != BPF_REG_0 || insn->off != 0 || | |
e67b8a68 EC |
4995 | (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) || |
4996 | BPF_CLASS(insn->code) == BPF_ALU64) { | |
61bd5218 | 4997 | verbose(env, "BPF_END uses reserved fields\n"); |
17a52670 AS |
4998 | return -EINVAL; |
4999 | } | |
5000 | } | |
5001 | ||
5002 | /* check src operand */ | |
dc503a8a | 5003 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
5004 | if (err) |
5005 | return err; | |
5006 | ||
1be7f75d | 5007 | if (is_pointer_value(env, insn->dst_reg)) { |
61bd5218 | 5008 | verbose(env, "R%d pointer arithmetic prohibited\n", |
1be7f75d AS |
5009 | insn->dst_reg); |
5010 | return -EACCES; | |
5011 | } | |
5012 | ||
17a52670 | 5013 | /* check dest operand */ |
dc503a8a | 5014 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
17a52670 AS |
5015 | if (err) |
5016 | return err; | |
5017 | ||
5018 | } else if (opcode == BPF_MOV) { | |
5019 | ||
5020 | if (BPF_SRC(insn->code) == BPF_X) { | |
5021 | if (insn->imm != 0 || insn->off != 0) { | |
61bd5218 | 5022 | verbose(env, "BPF_MOV uses reserved fields\n"); |
17a52670 AS |
5023 | return -EINVAL; |
5024 | } | |
5025 | ||
5026 | /* check src operand */ | |
dc503a8a | 5027 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
5028 | if (err) |
5029 | return err; | |
5030 | } else { | |
5031 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { | |
61bd5218 | 5032 | verbose(env, "BPF_MOV uses reserved fields\n"); |
17a52670 AS |
5033 | return -EINVAL; |
5034 | } | |
5035 | } | |
5036 | ||
fbeb1603 AF |
5037 | /* check dest operand, mark as required later */ |
5038 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); | |
17a52670 AS |
5039 | if (err) |
5040 | return err; | |
5041 | ||
5042 | if (BPF_SRC(insn->code) == BPF_X) { | |
e434b8cd JW |
5043 | struct bpf_reg_state *src_reg = regs + insn->src_reg; |
5044 | struct bpf_reg_state *dst_reg = regs + insn->dst_reg; | |
5045 | ||
17a52670 AS |
5046 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
5047 | /* case: R1 = R2 | |
5048 | * copy register state to dest reg | |
5049 | */ | |
e434b8cd JW |
5050 | *dst_reg = *src_reg; |
5051 | dst_reg->live |= REG_LIVE_WRITTEN; | |
5327ed3d | 5052 | dst_reg->subreg_def = DEF_NOT_SUBREG; |
17a52670 | 5053 | } else { |
f1174f77 | 5054 | /* R1 = (u32) R2 */ |
1be7f75d | 5055 | if (is_pointer_value(env, insn->src_reg)) { |
61bd5218 JK |
5056 | verbose(env, |
5057 | "R%d partial copy of pointer\n", | |
1be7f75d AS |
5058 | insn->src_reg); |
5059 | return -EACCES; | |
e434b8cd JW |
5060 | } else if (src_reg->type == SCALAR_VALUE) { |
5061 | *dst_reg = *src_reg; | |
5062 | dst_reg->live |= REG_LIVE_WRITTEN; | |
5327ed3d | 5063 | dst_reg->subreg_def = env->insn_idx + 1; |
e434b8cd JW |
5064 | } else { |
5065 | mark_reg_unknown(env, regs, | |
5066 | insn->dst_reg); | |
1be7f75d | 5067 | } |
e434b8cd | 5068 | coerce_reg_to_size(dst_reg, 4); |
17a52670 AS |
5069 | } |
5070 | } else { | |
5071 | /* case: R = imm | |
5072 | * remember the value we stored into this reg | |
5073 | */ | |
fbeb1603 AF |
5074 | /* clear any state __mark_reg_known doesn't set */ |
5075 | mark_reg_unknown(env, regs, insn->dst_reg); | |
f1174f77 | 5076 | regs[insn->dst_reg].type = SCALAR_VALUE; |
95a762e2 JH |
5077 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
5078 | __mark_reg_known(regs + insn->dst_reg, | |
5079 | insn->imm); | |
5080 | } else { | |
5081 | __mark_reg_known(regs + insn->dst_reg, | |
5082 | (u32)insn->imm); | |
5083 | } | |
17a52670 AS |
5084 | } |
5085 | ||
5086 | } else if (opcode > BPF_END) { | |
61bd5218 | 5087 | verbose(env, "invalid BPF_ALU opcode %x\n", opcode); |
17a52670 AS |
5088 | return -EINVAL; |
5089 | ||
5090 | } else { /* all other ALU ops: and, sub, xor, add, ... */ | |
5091 | ||
17a52670 AS |
5092 | if (BPF_SRC(insn->code) == BPF_X) { |
5093 | if (insn->imm != 0 || insn->off != 0) { | |
61bd5218 | 5094 | verbose(env, "BPF_ALU uses reserved fields\n"); |
17a52670 AS |
5095 | return -EINVAL; |
5096 | } | |
5097 | /* check src1 operand */ | |
dc503a8a | 5098 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
5099 | if (err) |
5100 | return err; | |
5101 | } else { | |
5102 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { | |
61bd5218 | 5103 | verbose(env, "BPF_ALU uses reserved fields\n"); |
17a52670 AS |
5104 | return -EINVAL; |
5105 | } | |
5106 | } | |
5107 | ||
5108 | /* check src2 operand */ | |
dc503a8a | 5109 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
5110 | if (err) |
5111 | return err; | |
5112 | ||
5113 | if ((opcode == BPF_MOD || opcode == BPF_DIV) && | |
5114 | BPF_SRC(insn->code) == BPF_K && insn->imm == 0) { | |
61bd5218 | 5115 | verbose(env, "div by zero\n"); |
17a52670 AS |
5116 | return -EINVAL; |
5117 | } | |
5118 | ||
229394e8 RV |
5119 | if ((opcode == BPF_LSH || opcode == BPF_RSH || |
5120 | opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) { | |
5121 | int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32; | |
5122 | ||
5123 | if (insn->imm < 0 || insn->imm >= size) { | |
61bd5218 | 5124 | verbose(env, "invalid shift %d\n", insn->imm); |
229394e8 RV |
5125 | return -EINVAL; |
5126 | } | |
5127 | } | |
5128 | ||
1a0dc1ac | 5129 | /* check dest operand */ |
dc503a8a | 5130 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
1a0dc1ac AS |
5131 | if (err) |
5132 | return err; | |
5133 | ||
f1174f77 | 5134 | return adjust_reg_min_max_vals(env, insn); |
17a52670 AS |
5135 | } |
5136 | ||
5137 | return 0; | |
5138 | } | |
5139 | ||
c6a9efa1 PC |
5140 | static void __find_good_pkt_pointers(struct bpf_func_state *state, |
5141 | struct bpf_reg_state *dst_reg, | |
5142 | enum bpf_reg_type type, u16 new_range) | |
5143 | { | |
5144 | struct bpf_reg_state *reg; | |
5145 | int i; | |
5146 | ||
5147 | for (i = 0; i < MAX_BPF_REG; i++) { | |
5148 | reg = &state->regs[i]; | |
5149 | if (reg->type == type && reg->id == dst_reg->id) | |
5150 | /* keep the maximum range already checked */ | |
5151 | reg->range = max(reg->range, new_range); | |
5152 | } | |
5153 | ||
5154 | bpf_for_each_spilled_reg(i, state, reg) { | |
5155 | if (!reg) | |
5156 | continue; | |
5157 | if (reg->type == type && reg->id == dst_reg->id) | |
5158 | reg->range = max(reg->range, new_range); | |
5159 | } | |
5160 | } | |
5161 | ||
f4d7e40a | 5162 | static void find_good_pkt_pointers(struct bpf_verifier_state *vstate, |
de8f3a83 | 5163 | struct bpf_reg_state *dst_reg, |
f8ddadc4 | 5164 | enum bpf_reg_type type, |
fb2a311a | 5165 | bool range_right_open) |
969bf05e | 5166 | { |
fb2a311a | 5167 | u16 new_range; |
c6a9efa1 | 5168 | int i; |
2d2be8ca | 5169 | |
fb2a311a DB |
5170 | if (dst_reg->off < 0 || |
5171 | (dst_reg->off == 0 && range_right_open)) | |
f1174f77 EC |
5172 | /* This doesn't give us any range */ |
5173 | return; | |
5174 | ||
b03c9f9f EC |
5175 | if (dst_reg->umax_value > MAX_PACKET_OFF || |
5176 | dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF) | |
f1174f77 EC |
5177 | /* Risk of overflow. For instance, ptr + (1<<63) may be less |
5178 | * than pkt_end, but that's because it's also less than pkt. | |
5179 | */ | |
5180 | return; | |
5181 | ||
fb2a311a DB |
5182 | new_range = dst_reg->off; |
5183 | if (range_right_open) | |
5184 | new_range--; | |
5185 | ||
5186 | /* Examples for register markings: | |
2d2be8ca | 5187 | * |
fb2a311a | 5188 | * pkt_data in dst register: |
2d2be8ca DB |
5189 | * |
5190 | * r2 = r3; | |
5191 | * r2 += 8; | |
5192 | * if (r2 > pkt_end) goto <handle exception> | |
5193 | * <access okay> | |
5194 | * | |
b4e432f1 DB |
5195 | * r2 = r3; |
5196 | * r2 += 8; | |
5197 | * if (r2 < pkt_end) goto <access okay> | |
5198 | * <handle exception> | |
5199 | * | |
2d2be8ca DB |
5200 | * Where: |
5201 | * r2 == dst_reg, pkt_end == src_reg | |
5202 | * r2=pkt(id=n,off=8,r=0) | |
5203 | * r3=pkt(id=n,off=0,r=0) | |
5204 | * | |
fb2a311a | 5205 | * pkt_data in src register: |
2d2be8ca DB |
5206 | * |
5207 | * r2 = r3; | |
5208 | * r2 += 8; | |
5209 | * if (pkt_end >= r2) goto <access okay> | |
5210 | * <handle exception> | |
5211 | * | |
b4e432f1 DB |
5212 | * r2 = r3; |
5213 | * r2 += 8; | |
5214 | * if (pkt_end <= r2) goto <handle exception> | |
5215 | * <access okay> | |
5216 | * | |
2d2be8ca DB |
5217 | * Where: |
5218 | * pkt_end == dst_reg, r2 == src_reg | |
5219 | * r2=pkt(id=n,off=8,r=0) | |
5220 | * r3=pkt(id=n,off=0,r=0) | |
5221 | * | |
5222 | * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8) | |
fb2a311a DB |
5223 | * or r3=pkt(id=n,off=0,r=8-1), so that range of bytes [r3, r3 + 8) |
5224 | * and [r3, r3 + 8-1) respectively is safe to access depending on | |
5225 | * the check. | |
969bf05e | 5226 | */ |
2d2be8ca | 5227 | |
f1174f77 EC |
5228 | /* If our ids match, then we must have the same max_value. And we |
5229 | * don't care about the other reg's fixed offset, since if it's too big | |
5230 | * the range won't allow anything. | |
5231 | * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16. | |
5232 | */ | |
c6a9efa1 PC |
5233 | for (i = 0; i <= vstate->curframe; i++) |
5234 | __find_good_pkt_pointers(vstate->frame[i], dst_reg, type, | |
5235 | new_range); | |
969bf05e AS |
5236 | } |
5237 | ||
4f7b3e82 AS |
5238 | /* compute branch direction of the expression "if (reg opcode val) goto target;" |
5239 | * and return: | |
5240 | * 1 - branch will be taken and "goto target" will be executed | |
5241 | * 0 - branch will not be taken and fall-through to next insn | |
5242 | * -1 - unknown. Example: "if (reg < 5)" is unknown when register value range [0,10] | |
5243 | */ | |
092ed096 JW |
5244 | static int is_branch_taken(struct bpf_reg_state *reg, u64 val, u8 opcode, |
5245 | bool is_jmp32) | |
4f7b3e82 | 5246 | { |
092ed096 | 5247 | struct bpf_reg_state reg_lo; |
a72dafaf JW |
5248 | s64 sval; |
5249 | ||
4f7b3e82 AS |
5250 | if (__is_pointer_value(false, reg)) |
5251 | return -1; | |
5252 | ||
092ed096 JW |
5253 | if (is_jmp32) { |
5254 | reg_lo = *reg; | |
5255 | reg = ®_lo; | |
5256 | /* For JMP32, only low 32 bits are compared, coerce_reg_to_size | |
5257 | * could truncate high bits and update umin/umax according to | |
5258 | * information of low bits. | |
5259 | */ | |
5260 | coerce_reg_to_size(reg, 4); | |
5261 | /* smin/smax need special handling. For example, after coerce, | |
5262 | * if smin_value is 0x00000000ffffffffLL, the value is -1 when | |
5263 | * used as operand to JMP32. It is a negative number from s32's | |
5264 | * point of view, while it is a positive number when seen as | |
5265 | * s64. The smin/smax are kept as s64, therefore, when used with | |
5266 | * JMP32, they need to be transformed into s32, then sign | |
5267 | * extended back to s64. | |
5268 | * | |
5269 | * Also, smin/smax were copied from umin/umax. If umin/umax has | |
5270 | * different sign bit, then min/max relationship doesn't | |
5271 | * maintain after casting into s32, for this case, set smin/smax | |
5272 | * to safest range. | |
5273 | */ | |
5274 | if ((reg->umax_value ^ reg->umin_value) & | |
5275 | (1ULL << 31)) { | |
5276 | reg->smin_value = S32_MIN; | |
5277 | reg->smax_value = S32_MAX; | |
5278 | } | |
5279 | reg->smin_value = (s64)(s32)reg->smin_value; | |
5280 | reg->smax_value = (s64)(s32)reg->smax_value; | |
5281 | ||
5282 | val = (u32)val; | |
5283 | sval = (s64)(s32)val; | |
5284 | } else { | |
5285 | sval = (s64)val; | |
5286 | } | |
a72dafaf | 5287 | |
4f7b3e82 AS |
5288 | switch (opcode) { |
5289 | case BPF_JEQ: | |
5290 | if (tnum_is_const(reg->var_off)) | |
5291 | return !!tnum_equals_const(reg->var_off, val); | |
5292 | break; | |
5293 | case BPF_JNE: | |
5294 | if (tnum_is_const(reg->var_off)) | |
5295 | return !tnum_equals_const(reg->var_off, val); | |
5296 | break; | |
960ea056 JK |
5297 | case BPF_JSET: |
5298 | if ((~reg->var_off.mask & reg->var_off.value) & val) | |
5299 | return 1; | |
5300 | if (!((reg->var_off.mask | reg->var_off.value) & val)) | |
5301 | return 0; | |
5302 | break; | |
4f7b3e82 AS |
5303 | case BPF_JGT: |
5304 | if (reg->umin_value > val) | |
5305 | return 1; | |
5306 | else if (reg->umax_value <= val) | |
5307 | return 0; | |
5308 | break; | |
5309 | case BPF_JSGT: | |
a72dafaf | 5310 | if (reg->smin_value > sval) |
4f7b3e82 | 5311 | return 1; |
a72dafaf | 5312 | else if (reg->smax_value < sval) |
4f7b3e82 AS |
5313 | return 0; |
5314 | break; | |
5315 | case BPF_JLT: | |
5316 | if (reg->umax_value < val) | |
5317 | return 1; | |
5318 | else if (reg->umin_value >= val) | |
5319 | return 0; | |
5320 | break; | |
5321 | case BPF_JSLT: | |
a72dafaf | 5322 | if (reg->smax_value < sval) |
4f7b3e82 | 5323 | return 1; |
a72dafaf | 5324 | else if (reg->smin_value >= sval) |
4f7b3e82 AS |
5325 | return 0; |
5326 | break; | |
5327 | case BPF_JGE: | |
5328 | if (reg->umin_value >= val) | |
5329 | return 1; | |
5330 | else if (reg->umax_value < val) | |
5331 | return 0; | |
5332 | break; | |
5333 | case BPF_JSGE: | |
a72dafaf | 5334 | if (reg->smin_value >= sval) |
4f7b3e82 | 5335 | return 1; |
a72dafaf | 5336 | else if (reg->smax_value < sval) |
4f7b3e82 AS |
5337 | return 0; |
5338 | break; | |
5339 | case BPF_JLE: | |
5340 | if (reg->umax_value <= val) | |
5341 | return 1; | |
5342 | else if (reg->umin_value > val) | |
5343 | return 0; | |
5344 | break; | |
5345 | case BPF_JSLE: | |
a72dafaf | 5346 | if (reg->smax_value <= sval) |
4f7b3e82 | 5347 | return 1; |
a72dafaf | 5348 | else if (reg->smin_value > sval) |
4f7b3e82 AS |
5349 | return 0; |
5350 | break; | |
5351 | } | |
5352 | ||
5353 | return -1; | |
5354 | } | |
5355 | ||
092ed096 JW |
5356 | /* Generate min value of the high 32-bit from TNUM info. */ |
5357 | static u64 gen_hi_min(struct tnum var) | |
5358 | { | |
5359 | return var.value & ~0xffffffffULL; | |
5360 | } | |
5361 | ||
5362 | /* Generate max value of the high 32-bit from TNUM info. */ | |
5363 | static u64 gen_hi_max(struct tnum var) | |
5364 | { | |
5365 | return (var.value | var.mask) & ~0xffffffffULL; | |
5366 | } | |
5367 | ||
5368 | /* Return true if VAL is compared with a s64 sign extended from s32, and they | |
5369 | * are with the same signedness. | |
5370 | */ | |
5371 | static bool cmp_val_with_extended_s64(s64 sval, struct bpf_reg_state *reg) | |
5372 | { | |
5373 | return ((s32)sval >= 0 && | |
5374 | reg->smin_value >= 0 && reg->smax_value <= S32_MAX) || | |
5375 | ((s32)sval < 0 && | |
5376 | reg->smax_value <= 0 && reg->smin_value >= S32_MIN); | |
5377 | } | |
5378 | ||
48461135 JB |
5379 | /* Adjusts the register min/max values in the case that the dst_reg is the |
5380 | * variable register that we are working on, and src_reg is a constant or we're | |
5381 | * simply doing a BPF_K check. | |
f1174f77 | 5382 | * In JEQ/JNE cases we also adjust the var_off values. |
48461135 JB |
5383 | */ |
5384 | static void reg_set_min_max(struct bpf_reg_state *true_reg, | |
5385 | struct bpf_reg_state *false_reg, u64 val, | |
092ed096 | 5386 | u8 opcode, bool is_jmp32) |
48461135 | 5387 | { |
a72dafaf JW |
5388 | s64 sval; |
5389 | ||
f1174f77 EC |
5390 | /* If the dst_reg is a pointer, we can't learn anything about its |
5391 | * variable offset from the compare (unless src_reg were a pointer into | |
5392 | * the same object, but we don't bother with that. | |
5393 | * Since false_reg and true_reg have the same type by construction, we | |
5394 | * only need to check one of them for pointerness. | |
5395 | */ | |
5396 | if (__is_pointer_value(false, false_reg)) | |
5397 | return; | |
4cabc5b1 | 5398 | |
092ed096 JW |
5399 | val = is_jmp32 ? (u32)val : val; |
5400 | sval = is_jmp32 ? (s64)(s32)val : (s64)val; | |
a72dafaf | 5401 | |
48461135 JB |
5402 | switch (opcode) { |
5403 | case BPF_JEQ: | |
48461135 | 5404 | case BPF_JNE: |
a72dafaf JW |
5405 | { |
5406 | struct bpf_reg_state *reg = | |
5407 | opcode == BPF_JEQ ? true_reg : false_reg; | |
5408 | ||
5409 | /* For BPF_JEQ, if this is false we know nothing Jon Snow, but | |
5410 | * if it is true we know the value for sure. Likewise for | |
5411 | * BPF_JNE. | |
48461135 | 5412 | */ |
092ed096 JW |
5413 | if (is_jmp32) { |
5414 | u64 old_v = reg->var_off.value; | |
5415 | u64 hi_mask = ~0xffffffffULL; | |
5416 | ||
5417 | reg->var_off.value = (old_v & hi_mask) | val; | |
5418 | reg->var_off.mask &= hi_mask; | |
5419 | } else { | |
5420 | __mark_reg_known(reg, val); | |
5421 | } | |
48461135 | 5422 | break; |
a72dafaf | 5423 | } |
960ea056 JK |
5424 | case BPF_JSET: |
5425 | false_reg->var_off = tnum_and(false_reg->var_off, | |
5426 | tnum_const(~val)); | |
5427 | if (is_power_of_2(val)) | |
5428 | true_reg->var_off = tnum_or(true_reg->var_off, | |
5429 | tnum_const(val)); | |
5430 | break; | |
48461135 | 5431 | case BPF_JGE: |
a72dafaf JW |
5432 | case BPF_JGT: |
5433 | { | |
5434 | u64 false_umax = opcode == BPF_JGT ? val : val - 1; | |
5435 | u64 true_umin = opcode == BPF_JGT ? val + 1 : val; | |
5436 | ||
092ed096 JW |
5437 | if (is_jmp32) { |
5438 | false_umax += gen_hi_max(false_reg->var_off); | |
5439 | true_umin += gen_hi_min(true_reg->var_off); | |
5440 | } | |
a72dafaf JW |
5441 | false_reg->umax_value = min(false_reg->umax_value, false_umax); |
5442 | true_reg->umin_value = max(true_reg->umin_value, true_umin); | |
b03c9f9f | 5443 | break; |
a72dafaf | 5444 | } |
48461135 | 5445 | case BPF_JSGE: |
a72dafaf JW |
5446 | case BPF_JSGT: |
5447 | { | |
5448 | s64 false_smax = opcode == BPF_JSGT ? sval : sval - 1; | |
5449 | s64 true_smin = opcode == BPF_JSGT ? sval + 1 : sval; | |
5450 | ||
092ed096 JW |
5451 | /* If the full s64 was not sign-extended from s32 then don't |
5452 | * deduct further info. | |
5453 | */ | |
5454 | if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg)) | |
5455 | break; | |
a72dafaf JW |
5456 | false_reg->smax_value = min(false_reg->smax_value, false_smax); |
5457 | true_reg->smin_value = max(true_reg->smin_value, true_smin); | |
48461135 | 5458 | break; |
a72dafaf | 5459 | } |
b4e432f1 | 5460 | case BPF_JLE: |
a72dafaf JW |
5461 | case BPF_JLT: |
5462 | { | |
5463 | u64 false_umin = opcode == BPF_JLT ? val : val + 1; | |
5464 | u64 true_umax = opcode == BPF_JLT ? val - 1 : val; | |
5465 | ||
092ed096 JW |
5466 | if (is_jmp32) { |
5467 | false_umin += gen_hi_min(false_reg->var_off); | |
5468 | true_umax += gen_hi_max(true_reg->var_off); | |
5469 | } | |
a72dafaf JW |
5470 | false_reg->umin_value = max(false_reg->umin_value, false_umin); |
5471 | true_reg->umax_value = min(true_reg->umax_value, true_umax); | |
b4e432f1 | 5472 | break; |
a72dafaf | 5473 | } |
b4e432f1 | 5474 | case BPF_JSLE: |
a72dafaf JW |
5475 | case BPF_JSLT: |
5476 | { | |
5477 | s64 false_smin = opcode == BPF_JSLT ? sval : sval + 1; | |
5478 | s64 true_smax = opcode == BPF_JSLT ? sval - 1 : sval; | |
5479 | ||
092ed096 JW |
5480 | if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg)) |
5481 | break; | |
a72dafaf JW |
5482 | false_reg->smin_value = max(false_reg->smin_value, false_smin); |
5483 | true_reg->smax_value = min(true_reg->smax_value, true_smax); | |
b4e432f1 | 5484 | break; |
a72dafaf | 5485 | } |
48461135 JB |
5486 | default: |
5487 | break; | |
5488 | } | |
5489 | ||
b03c9f9f EC |
5490 | __reg_deduce_bounds(false_reg); |
5491 | __reg_deduce_bounds(true_reg); | |
5492 | /* We might have learned some bits from the bounds. */ | |
5493 | __reg_bound_offset(false_reg); | |
5494 | __reg_bound_offset(true_reg); | |
5495 | /* Intersecting with the old var_off might have improved our bounds | |
5496 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), | |
5497 | * then new var_off is (0; 0x7f...fc) which improves our umax. | |
5498 | */ | |
5499 | __update_reg_bounds(false_reg); | |
5500 | __update_reg_bounds(true_reg); | |
48461135 JB |
5501 | } |
5502 | ||
f1174f77 EC |
5503 | /* Same as above, but for the case that dst_reg holds a constant and src_reg is |
5504 | * the variable reg. | |
48461135 JB |
5505 | */ |
5506 | static void reg_set_min_max_inv(struct bpf_reg_state *true_reg, | |
5507 | struct bpf_reg_state *false_reg, u64 val, | |
092ed096 | 5508 | u8 opcode, bool is_jmp32) |
48461135 | 5509 | { |
a72dafaf JW |
5510 | s64 sval; |
5511 | ||
f1174f77 EC |
5512 | if (__is_pointer_value(false, false_reg)) |
5513 | return; | |
4cabc5b1 | 5514 | |
092ed096 JW |
5515 | val = is_jmp32 ? (u32)val : val; |
5516 | sval = is_jmp32 ? (s64)(s32)val : (s64)val; | |
a72dafaf | 5517 | |
48461135 JB |
5518 | switch (opcode) { |
5519 | case BPF_JEQ: | |
48461135 | 5520 | case BPF_JNE: |
a72dafaf JW |
5521 | { |
5522 | struct bpf_reg_state *reg = | |
5523 | opcode == BPF_JEQ ? true_reg : false_reg; | |
5524 | ||
092ed096 JW |
5525 | if (is_jmp32) { |
5526 | u64 old_v = reg->var_off.value; | |
5527 | u64 hi_mask = ~0xffffffffULL; | |
5528 | ||
5529 | reg->var_off.value = (old_v & hi_mask) | val; | |
5530 | reg->var_off.mask &= hi_mask; | |
5531 | } else { | |
5532 | __mark_reg_known(reg, val); | |
5533 | } | |
48461135 | 5534 | break; |
a72dafaf | 5535 | } |
960ea056 JK |
5536 | case BPF_JSET: |
5537 | false_reg->var_off = tnum_and(false_reg->var_off, | |
5538 | tnum_const(~val)); | |
5539 | if (is_power_of_2(val)) | |
5540 | true_reg->var_off = tnum_or(true_reg->var_off, | |
5541 | tnum_const(val)); | |
5542 | break; | |
48461135 | 5543 | case BPF_JGE: |
a72dafaf JW |
5544 | case BPF_JGT: |
5545 | { | |
5546 | u64 false_umin = opcode == BPF_JGT ? val : val + 1; | |
5547 | u64 true_umax = opcode == BPF_JGT ? val - 1 : val; | |
5548 | ||
092ed096 JW |
5549 | if (is_jmp32) { |
5550 | false_umin += gen_hi_min(false_reg->var_off); | |
5551 | true_umax += gen_hi_max(true_reg->var_off); | |
5552 | } | |
a72dafaf JW |
5553 | false_reg->umin_value = max(false_reg->umin_value, false_umin); |
5554 | true_reg->umax_value = min(true_reg->umax_value, true_umax); | |
b03c9f9f | 5555 | break; |
a72dafaf | 5556 | } |
48461135 | 5557 | case BPF_JSGE: |
a72dafaf JW |
5558 | case BPF_JSGT: |
5559 | { | |
5560 | s64 false_smin = opcode == BPF_JSGT ? sval : sval + 1; | |
5561 | s64 true_smax = opcode == BPF_JSGT ? sval - 1 : sval; | |
5562 | ||
092ed096 JW |
5563 | if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg)) |
5564 | break; | |
a72dafaf JW |
5565 | false_reg->smin_value = max(false_reg->smin_value, false_smin); |
5566 | true_reg->smax_value = min(true_reg->smax_value, true_smax); | |
48461135 | 5567 | break; |
a72dafaf | 5568 | } |
b4e432f1 | 5569 | case BPF_JLE: |
a72dafaf JW |
5570 | case BPF_JLT: |
5571 | { | |
5572 | u64 false_umax = opcode == BPF_JLT ? val : val - 1; | |
5573 | u64 true_umin = opcode == BPF_JLT ? val + 1 : val; | |
5574 | ||
092ed096 JW |
5575 | if (is_jmp32) { |
5576 | false_umax += gen_hi_max(false_reg->var_off); | |
5577 | true_umin += gen_hi_min(true_reg->var_off); | |
5578 | } | |
a72dafaf JW |
5579 | false_reg->umax_value = min(false_reg->umax_value, false_umax); |
5580 | true_reg->umin_value = max(true_reg->umin_value, true_umin); | |
b4e432f1 | 5581 | break; |
a72dafaf | 5582 | } |
b4e432f1 | 5583 | case BPF_JSLE: |
a72dafaf JW |
5584 | case BPF_JSLT: |
5585 | { | |
5586 | s64 false_smax = opcode == BPF_JSLT ? sval : sval - 1; | |
5587 | s64 true_smin = opcode == BPF_JSLT ? sval + 1 : sval; | |
5588 | ||
092ed096 JW |
5589 | if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg)) |
5590 | break; | |
a72dafaf JW |
5591 | false_reg->smax_value = min(false_reg->smax_value, false_smax); |
5592 | true_reg->smin_value = max(true_reg->smin_value, true_smin); | |
b4e432f1 | 5593 | break; |
a72dafaf | 5594 | } |
48461135 JB |
5595 | default: |
5596 | break; | |
5597 | } | |
5598 | ||
b03c9f9f EC |
5599 | __reg_deduce_bounds(false_reg); |
5600 | __reg_deduce_bounds(true_reg); | |
5601 | /* We might have learned some bits from the bounds. */ | |
5602 | __reg_bound_offset(false_reg); | |
5603 | __reg_bound_offset(true_reg); | |
5604 | /* Intersecting with the old var_off might have improved our bounds | |
5605 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), | |
5606 | * then new var_off is (0; 0x7f...fc) which improves our umax. | |
5607 | */ | |
5608 | __update_reg_bounds(false_reg); | |
5609 | __update_reg_bounds(true_reg); | |
f1174f77 EC |
5610 | } |
5611 | ||
5612 | /* Regs are known to be equal, so intersect their min/max/var_off */ | |
5613 | static void __reg_combine_min_max(struct bpf_reg_state *src_reg, | |
5614 | struct bpf_reg_state *dst_reg) | |
5615 | { | |
b03c9f9f EC |
5616 | src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value, |
5617 | dst_reg->umin_value); | |
5618 | src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value, | |
5619 | dst_reg->umax_value); | |
5620 | src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value, | |
5621 | dst_reg->smin_value); | |
5622 | src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value, | |
5623 | dst_reg->smax_value); | |
f1174f77 EC |
5624 | src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off, |
5625 | dst_reg->var_off); | |
b03c9f9f EC |
5626 | /* We might have learned new bounds from the var_off. */ |
5627 | __update_reg_bounds(src_reg); | |
5628 | __update_reg_bounds(dst_reg); | |
5629 | /* We might have learned something about the sign bit. */ | |
5630 | __reg_deduce_bounds(src_reg); | |
5631 | __reg_deduce_bounds(dst_reg); | |
5632 | /* We might have learned some bits from the bounds. */ | |
5633 | __reg_bound_offset(src_reg); | |
5634 | __reg_bound_offset(dst_reg); | |
5635 | /* Intersecting with the old var_off might have improved our bounds | |
5636 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), | |
5637 | * then new var_off is (0; 0x7f...fc) which improves our umax. | |
5638 | */ | |
5639 | __update_reg_bounds(src_reg); | |
5640 | __update_reg_bounds(dst_reg); | |
f1174f77 EC |
5641 | } |
5642 | ||
5643 | static void reg_combine_min_max(struct bpf_reg_state *true_src, | |
5644 | struct bpf_reg_state *true_dst, | |
5645 | struct bpf_reg_state *false_src, | |
5646 | struct bpf_reg_state *false_dst, | |
5647 | u8 opcode) | |
5648 | { | |
5649 | switch (opcode) { | |
5650 | case BPF_JEQ: | |
5651 | __reg_combine_min_max(true_src, true_dst); | |
5652 | break; | |
5653 | case BPF_JNE: | |
5654 | __reg_combine_min_max(false_src, false_dst); | |
b03c9f9f | 5655 | break; |
4cabc5b1 | 5656 | } |
48461135 JB |
5657 | } |
5658 | ||
fd978bf7 JS |
5659 | static void mark_ptr_or_null_reg(struct bpf_func_state *state, |
5660 | struct bpf_reg_state *reg, u32 id, | |
840b9615 | 5661 | bool is_null) |
57a09bf0 | 5662 | { |
840b9615 | 5663 | if (reg_type_may_be_null(reg->type) && reg->id == id) { |
f1174f77 EC |
5664 | /* Old offset (both fixed and variable parts) should |
5665 | * have been known-zero, because we don't allow pointer | |
5666 | * arithmetic on pointers that might be NULL. | |
5667 | */ | |
b03c9f9f EC |
5668 | if (WARN_ON_ONCE(reg->smin_value || reg->smax_value || |
5669 | !tnum_equals_const(reg->var_off, 0) || | |
f1174f77 | 5670 | reg->off)) { |
b03c9f9f EC |
5671 | __mark_reg_known_zero(reg); |
5672 | reg->off = 0; | |
f1174f77 EC |
5673 | } |
5674 | if (is_null) { | |
5675 | reg->type = SCALAR_VALUE; | |
840b9615 JS |
5676 | } else if (reg->type == PTR_TO_MAP_VALUE_OR_NULL) { |
5677 | if (reg->map_ptr->inner_map_meta) { | |
5678 | reg->type = CONST_PTR_TO_MAP; | |
5679 | reg->map_ptr = reg->map_ptr->inner_map_meta; | |
fada7fdc JL |
5680 | } else if (reg->map_ptr->map_type == |
5681 | BPF_MAP_TYPE_XSKMAP) { | |
5682 | reg->type = PTR_TO_XDP_SOCK; | |
840b9615 JS |
5683 | } else { |
5684 | reg->type = PTR_TO_MAP_VALUE; | |
5685 | } | |
c64b7983 JS |
5686 | } else if (reg->type == PTR_TO_SOCKET_OR_NULL) { |
5687 | reg->type = PTR_TO_SOCKET; | |
46f8bc92 MKL |
5688 | } else if (reg->type == PTR_TO_SOCK_COMMON_OR_NULL) { |
5689 | reg->type = PTR_TO_SOCK_COMMON; | |
655a51e5 MKL |
5690 | } else if (reg->type == PTR_TO_TCP_SOCK_OR_NULL) { |
5691 | reg->type = PTR_TO_TCP_SOCK; | |
56f668df | 5692 | } |
1b986589 MKL |
5693 | if (is_null) { |
5694 | /* We don't need id and ref_obj_id from this point | |
5695 | * onwards anymore, thus we should better reset it, | |
5696 | * so that state pruning has chances to take effect. | |
5697 | */ | |
5698 | reg->id = 0; | |
5699 | reg->ref_obj_id = 0; | |
5700 | } else if (!reg_may_point_to_spin_lock(reg)) { | |
5701 | /* For not-NULL ptr, reg->ref_obj_id will be reset | |
5702 | * in release_reg_references(). | |
5703 | * | |
5704 | * reg->id is still used by spin_lock ptr. Other | |
5705 | * than spin_lock ptr type, reg->id can be reset. | |
fd978bf7 JS |
5706 | */ |
5707 | reg->id = 0; | |
56f668df | 5708 | } |
57a09bf0 TG |
5709 | } |
5710 | } | |
5711 | ||
c6a9efa1 PC |
5712 | static void __mark_ptr_or_null_regs(struct bpf_func_state *state, u32 id, |
5713 | bool is_null) | |
5714 | { | |
5715 | struct bpf_reg_state *reg; | |
5716 | int i; | |
5717 | ||
5718 | for (i = 0; i < MAX_BPF_REG; i++) | |
5719 | mark_ptr_or_null_reg(state, &state->regs[i], id, is_null); | |
5720 | ||
5721 | bpf_for_each_spilled_reg(i, state, reg) { | |
5722 | if (!reg) | |
5723 | continue; | |
5724 | mark_ptr_or_null_reg(state, reg, id, is_null); | |
5725 | } | |
5726 | } | |
5727 | ||
57a09bf0 TG |
5728 | /* The logic is similar to find_good_pkt_pointers(), both could eventually |
5729 | * be folded together at some point. | |
5730 | */ | |
840b9615 JS |
5731 | static void mark_ptr_or_null_regs(struct bpf_verifier_state *vstate, u32 regno, |
5732 | bool is_null) | |
57a09bf0 | 5733 | { |
f4d7e40a | 5734 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
c6a9efa1 | 5735 | struct bpf_reg_state *regs = state->regs; |
1b986589 | 5736 | u32 ref_obj_id = regs[regno].ref_obj_id; |
a08dd0da | 5737 | u32 id = regs[regno].id; |
c6a9efa1 | 5738 | int i; |
57a09bf0 | 5739 | |
1b986589 MKL |
5740 | if (ref_obj_id && ref_obj_id == id && is_null) |
5741 | /* regs[regno] is in the " == NULL" branch. | |
5742 | * No one could have freed the reference state before | |
5743 | * doing the NULL check. | |
5744 | */ | |
5745 | WARN_ON_ONCE(release_reference_state(state, id)); | |
fd978bf7 | 5746 | |
c6a9efa1 PC |
5747 | for (i = 0; i <= vstate->curframe; i++) |
5748 | __mark_ptr_or_null_regs(vstate->frame[i], id, is_null); | |
57a09bf0 TG |
5749 | } |
5750 | ||
5beca081 DB |
5751 | static bool try_match_pkt_pointers(const struct bpf_insn *insn, |
5752 | struct bpf_reg_state *dst_reg, | |
5753 | struct bpf_reg_state *src_reg, | |
5754 | struct bpf_verifier_state *this_branch, | |
5755 | struct bpf_verifier_state *other_branch) | |
5756 | { | |
5757 | if (BPF_SRC(insn->code) != BPF_X) | |
5758 | return false; | |
5759 | ||
092ed096 JW |
5760 | /* Pointers are always 64-bit. */ |
5761 | if (BPF_CLASS(insn->code) == BPF_JMP32) | |
5762 | return false; | |
5763 | ||
5beca081 DB |
5764 | switch (BPF_OP(insn->code)) { |
5765 | case BPF_JGT: | |
5766 | if ((dst_reg->type == PTR_TO_PACKET && | |
5767 | src_reg->type == PTR_TO_PACKET_END) || | |
5768 | (dst_reg->type == PTR_TO_PACKET_META && | |
5769 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
5770 | /* pkt_data' > pkt_end, pkt_meta' > pkt_data */ | |
5771 | find_good_pkt_pointers(this_branch, dst_reg, | |
5772 | dst_reg->type, false); | |
5773 | } else if ((dst_reg->type == PTR_TO_PACKET_END && | |
5774 | src_reg->type == PTR_TO_PACKET) || | |
5775 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
5776 | src_reg->type == PTR_TO_PACKET_META)) { | |
5777 | /* pkt_end > pkt_data', pkt_data > pkt_meta' */ | |
5778 | find_good_pkt_pointers(other_branch, src_reg, | |
5779 | src_reg->type, true); | |
5780 | } else { | |
5781 | return false; | |
5782 | } | |
5783 | break; | |
5784 | case BPF_JLT: | |
5785 | if ((dst_reg->type == PTR_TO_PACKET && | |
5786 | src_reg->type == PTR_TO_PACKET_END) || | |
5787 | (dst_reg->type == PTR_TO_PACKET_META && | |
5788 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
5789 | /* pkt_data' < pkt_end, pkt_meta' < pkt_data */ | |
5790 | find_good_pkt_pointers(other_branch, dst_reg, | |
5791 | dst_reg->type, true); | |
5792 | } else if ((dst_reg->type == PTR_TO_PACKET_END && | |
5793 | src_reg->type == PTR_TO_PACKET) || | |
5794 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
5795 | src_reg->type == PTR_TO_PACKET_META)) { | |
5796 | /* pkt_end < pkt_data', pkt_data > pkt_meta' */ | |
5797 | find_good_pkt_pointers(this_branch, src_reg, | |
5798 | src_reg->type, false); | |
5799 | } else { | |
5800 | return false; | |
5801 | } | |
5802 | break; | |
5803 | case BPF_JGE: | |
5804 | if ((dst_reg->type == PTR_TO_PACKET && | |
5805 | src_reg->type == PTR_TO_PACKET_END) || | |
5806 | (dst_reg->type == PTR_TO_PACKET_META && | |
5807 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
5808 | /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */ | |
5809 | find_good_pkt_pointers(this_branch, dst_reg, | |
5810 | dst_reg->type, true); | |
5811 | } else if ((dst_reg->type == PTR_TO_PACKET_END && | |
5812 | src_reg->type == PTR_TO_PACKET) || | |
5813 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
5814 | src_reg->type == PTR_TO_PACKET_META)) { | |
5815 | /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */ | |
5816 | find_good_pkt_pointers(other_branch, src_reg, | |
5817 | src_reg->type, false); | |
5818 | } else { | |
5819 | return false; | |
5820 | } | |
5821 | break; | |
5822 | case BPF_JLE: | |
5823 | if ((dst_reg->type == PTR_TO_PACKET && | |
5824 | src_reg->type == PTR_TO_PACKET_END) || | |
5825 | (dst_reg->type == PTR_TO_PACKET_META && | |
5826 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
5827 | /* pkt_data' <= pkt_end, pkt_meta' <= pkt_data */ | |
5828 | find_good_pkt_pointers(other_branch, dst_reg, | |
5829 | dst_reg->type, false); | |
5830 | } else if ((dst_reg->type == PTR_TO_PACKET_END && | |
5831 | src_reg->type == PTR_TO_PACKET) || | |
5832 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
5833 | src_reg->type == PTR_TO_PACKET_META)) { | |
5834 | /* pkt_end <= pkt_data', pkt_data <= pkt_meta' */ | |
5835 | find_good_pkt_pointers(this_branch, src_reg, | |
5836 | src_reg->type, true); | |
5837 | } else { | |
5838 | return false; | |
5839 | } | |
5840 | break; | |
5841 | default: | |
5842 | return false; | |
5843 | } | |
5844 | ||
5845 | return true; | |
5846 | } | |
5847 | ||
58e2af8b | 5848 | static int check_cond_jmp_op(struct bpf_verifier_env *env, |
17a52670 AS |
5849 | struct bpf_insn *insn, int *insn_idx) |
5850 | { | |
f4d7e40a AS |
5851 | struct bpf_verifier_state *this_branch = env->cur_state; |
5852 | struct bpf_verifier_state *other_branch; | |
5853 | struct bpf_reg_state *regs = this_branch->frame[this_branch->curframe]->regs; | |
fb8d251e | 5854 | struct bpf_reg_state *dst_reg, *other_branch_regs, *src_reg = NULL; |
17a52670 | 5855 | u8 opcode = BPF_OP(insn->code); |
092ed096 | 5856 | bool is_jmp32; |
fb8d251e | 5857 | int pred = -1; |
17a52670 AS |
5858 | int err; |
5859 | ||
092ed096 JW |
5860 | /* Only conditional jumps are expected to reach here. */ |
5861 | if (opcode == BPF_JA || opcode > BPF_JSLE) { | |
5862 | verbose(env, "invalid BPF_JMP/JMP32 opcode %x\n", opcode); | |
17a52670 AS |
5863 | return -EINVAL; |
5864 | } | |
5865 | ||
5866 | if (BPF_SRC(insn->code) == BPF_X) { | |
5867 | if (insn->imm != 0) { | |
092ed096 | 5868 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
17a52670 AS |
5869 | return -EINVAL; |
5870 | } | |
5871 | ||
5872 | /* check src1 operand */ | |
dc503a8a | 5873 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
5874 | if (err) |
5875 | return err; | |
1be7f75d AS |
5876 | |
5877 | if (is_pointer_value(env, insn->src_reg)) { | |
61bd5218 | 5878 | verbose(env, "R%d pointer comparison prohibited\n", |
1be7f75d AS |
5879 | insn->src_reg); |
5880 | return -EACCES; | |
5881 | } | |
fb8d251e | 5882 | src_reg = ®s[insn->src_reg]; |
17a52670 AS |
5883 | } else { |
5884 | if (insn->src_reg != BPF_REG_0) { | |
092ed096 | 5885 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
17a52670 AS |
5886 | return -EINVAL; |
5887 | } | |
5888 | } | |
5889 | ||
5890 | /* check src2 operand */ | |
dc503a8a | 5891 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
5892 | if (err) |
5893 | return err; | |
5894 | ||
1a0dc1ac | 5895 | dst_reg = ®s[insn->dst_reg]; |
092ed096 | 5896 | is_jmp32 = BPF_CLASS(insn->code) == BPF_JMP32; |
1a0dc1ac | 5897 | |
fb8d251e AS |
5898 | if (BPF_SRC(insn->code) == BPF_K) |
5899 | pred = is_branch_taken(dst_reg, insn->imm, | |
5900 | opcode, is_jmp32); | |
5901 | else if (src_reg->type == SCALAR_VALUE && | |
5902 | tnum_is_const(src_reg->var_off)) | |
5903 | pred = is_branch_taken(dst_reg, src_reg->var_off.value, | |
5904 | opcode, is_jmp32); | |
b5dc0163 AS |
5905 | if (pred >= 0) { |
5906 | err = mark_chain_precision(env, insn->dst_reg); | |
5907 | if (BPF_SRC(insn->code) == BPF_X && !err) | |
5908 | err = mark_chain_precision(env, insn->src_reg); | |
5909 | if (err) | |
5910 | return err; | |
5911 | } | |
fb8d251e AS |
5912 | if (pred == 1) { |
5913 | /* only follow the goto, ignore fall-through */ | |
5914 | *insn_idx += insn->off; | |
5915 | return 0; | |
5916 | } else if (pred == 0) { | |
5917 | /* only follow fall-through branch, since | |
5918 | * that's where the program will go | |
5919 | */ | |
5920 | return 0; | |
17a52670 AS |
5921 | } |
5922 | ||
979d63d5 DB |
5923 | other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx, |
5924 | false); | |
17a52670 AS |
5925 | if (!other_branch) |
5926 | return -EFAULT; | |
f4d7e40a | 5927 | other_branch_regs = other_branch->frame[other_branch->curframe]->regs; |
17a52670 | 5928 | |
48461135 JB |
5929 | /* detect if we are comparing against a constant value so we can adjust |
5930 | * our min/max values for our dst register. | |
f1174f77 EC |
5931 | * this is only legit if both are scalars (or pointers to the same |
5932 | * object, I suppose, but we don't support that right now), because | |
5933 | * otherwise the different base pointers mean the offsets aren't | |
5934 | * comparable. | |
48461135 JB |
5935 | */ |
5936 | if (BPF_SRC(insn->code) == BPF_X) { | |
092ed096 JW |
5937 | struct bpf_reg_state *src_reg = ®s[insn->src_reg]; |
5938 | struct bpf_reg_state lo_reg0 = *dst_reg; | |
5939 | struct bpf_reg_state lo_reg1 = *src_reg; | |
5940 | struct bpf_reg_state *src_lo, *dst_lo; | |
5941 | ||
5942 | dst_lo = &lo_reg0; | |
5943 | src_lo = &lo_reg1; | |
5944 | coerce_reg_to_size(dst_lo, 4); | |
5945 | coerce_reg_to_size(src_lo, 4); | |
5946 | ||
f1174f77 | 5947 | if (dst_reg->type == SCALAR_VALUE && |
092ed096 JW |
5948 | src_reg->type == SCALAR_VALUE) { |
5949 | if (tnum_is_const(src_reg->var_off) || | |
5950 | (is_jmp32 && tnum_is_const(src_lo->var_off))) | |
f4d7e40a | 5951 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
092ed096 JW |
5952 | dst_reg, |
5953 | is_jmp32 | |
5954 | ? src_lo->var_off.value | |
5955 | : src_reg->var_off.value, | |
5956 | opcode, is_jmp32); | |
5957 | else if (tnum_is_const(dst_reg->var_off) || | |
5958 | (is_jmp32 && tnum_is_const(dst_lo->var_off))) | |
f4d7e40a | 5959 | reg_set_min_max_inv(&other_branch_regs[insn->src_reg], |
092ed096 JW |
5960 | src_reg, |
5961 | is_jmp32 | |
5962 | ? dst_lo->var_off.value | |
5963 | : dst_reg->var_off.value, | |
5964 | opcode, is_jmp32); | |
5965 | else if (!is_jmp32 && | |
5966 | (opcode == BPF_JEQ || opcode == BPF_JNE)) | |
f1174f77 | 5967 | /* Comparing for equality, we can combine knowledge */ |
f4d7e40a AS |
5968 | reg_combine_min_max(&other_branch_regs[insn->src_reg], |
5969 | &other_branch_regs[insn->dst_reg], | |
092ed096 | 5970 | src_reg, dst_reg, opcode); |
f1174f77 EC |
5971 | } |
5972 | } else if (dst_reg->type == SCALAR_VALUE) { | |
f4d7e40a | 5973 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
092ed096 | 5974 | dst_reg, insn->imm, opcode, is_jmp32); |
48461135 JB |
5975 | } |
5976 | ||
092ed096 JW |
5977 | /* detect if R == 0 where R is returned from bpf_map_lookup_elem(). |
5978 | * NOTE: these optimizations below are related with pointer comparison | |
5979 | * which will never be JMP32. | |
5980 | */ | |
5981 | if (!is_jmp32 && BPF_SRC(insn->code) == BPF_K && | |
1a0dc1ac | 5982 | insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) && |
840b9615 JS |
5983 | reg_type_may_be_null(dst_reg->type)) { |
5984 | /* Mark all identical registers in each branch as either | |
57a09bf0 TG |
5985 | * safe or unknown depending R == 0 or R != 0 conditional. |
5986 | */ | |
840b9615 JS |
5987 | mark_ptr_or_null_regs(this_branch, insn->dst_reg, |
5988 | opcode == BPF_JNE); | |
5989 | mark_ptr_or_null_regs(other_branch, insn->dst_reg, | |
5990 | opcode == BPF_JEQ); | |
5beca081 DB |
5991 | } else if (!try_match_pkt_pointers(insn, dst_reg, ®s[insn->src_reg], |
5992 | this_branch, other_branch) && | |
5993 | is_pointer_value(env, insn->dst_reg)) { | |
61bd5218 JK |
5994 | verbose(env, "R%d pointer comparison prohibited\n", |
5995 | insn->dst_reg); | |
1be7f75d | 5996 | return -EACCES; |
17a52670 | 5997 | } |
06ee7115 | 5998 | if (env->log.level & BPF_LOG_LEVEL) |
f4d7e40a | 5999 | print_verifier_state(env, this_branch->frame[this_branch->curframe]); |
17a52670 AS |
6000 | return 0; |
6001 | } | |
6002 | ||
17a52670 | 6003 | /* verify BPF_LD_IMM64 instruction */ |
58e2af8b | 6004 | static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn) |
17a52670 | 6005 | { |
d8eca5bb | 6006 | struct bpf_insn_aux_data *aux = cur_aux(env); |
638f5b90 | 6007 | struct bpf_reg_state *regs = cur_regs(env); |
d8eca5bb | 6008 | struct bpf_map *map; |
17a52670 AS |
6009 | int err; |
6010 | ||
6011 | if (BPF_SIZE(insn->code) != BPF_DW) { | |
61bd5218 | 6012 | verbose(env, "invalid BPF_LD_IMM insn\n"); |
17a52670 AS |
6013 | return -EINVAL; |
6014 | } | |
6015 | if (insn->off != 0) { | |
61bd5218 | 6016 | verbose(env, "BPF_LD_IMM64 uses reserved fields\n"); |
17a52670 AS |
6017 | return -EINVAL; |
6018 | } | |
6019 | ||
dc503a8a | 6020 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
17a52670 AS |
6021 | if (err) |
6022 | return err; | |
6023 | ||
6b173873 | 6024 | if (insn->src_reg == 0) { |
6b173873 JK |
6025 | u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm; |
6026 | ||
f1174f77 | 6027 | regs[insn->dst_reg].type = SCALAR_VALUE; |
b03c9f9f | 6028 | __mark_reg_known(®s[insn->dst_reg], imm); |
17a52670 | 6029 | return 0; |
6b173873 | 6030 | } |
17a52670 | 6031 | |
d8eca5bb DB |
6032 | map = env->used_maps[aux->map_index]; |
6033 | mark_reg_known_zero(env, regs, insn->dst_reg); | |
6034 | regs[insn->dst_reg].map_ptr = map; | |
6035 | ||
6036 | if (insn->src_reg == BPF_PSEUDO_MAP_VALUE) { | |
6037 | regs[insn->dst_reg].type = PTR_TO_MAP_VALUE; | |
6038 | regs[insn->dst_reg].off = aux->map_off; | |
6039 | if (map_value_has_spin_lock(map)) | |
6040 | regs[insn->dst_reg].id = ++env->id_gen; | |
6041 | } else if (insn->src_reg == BPF_PSEUDO_MAP_FD) { | |
6042 | regs[insn->dst_reg].type = CONST_PTR_TO_MAP; | |
6043 | } else { | |
6044 | verbose(env, "bpf verifier is misconfigured\n"); | |
6045 | return -EINVAL; | |
6046 | } | |
17a52670 | 6047 | |
17a52670 AS |
6048 | return 0; |
6049 | } | |
6050 | ||
96be4325 DB |
6051 | static bool may_access_skb(enum bpf_prog_type type) |
6052 | { | |
6053 | switch (type) { | |
6054 | case BPF_PROG_TYPE_SOCKET_FILTER: | |
6055 | case BPF_PROG_TYPE_SCHED_CLS: | |
94caee8c | 6056 | case BPF_PROG_TYPE_SCHED_ACT: |
96be4325 DB |
6057 | return true; |
6058 | default: | |
6059 | return false; | |
6060 | } | |
6061 | } | |
6062 | ||
ddd872bc AS |
6063 | /* verify safety of LD_ABS|LD_IND instructions: |
6064 | * - they can only appear in the programs where ctx == skb | |
6065 | * - since they are wrappers of function calls, they scratch R1-R5 registers, | |
6066 | * preserve R6-R9, and store return value into R0 | |
6067 | * | |
6068 | * Implicit input: | |
6069 | * ctx == skb == R6 == CTX | |
6070 | * | |
6071 | * Explicit input: | |
6072 | * SRC == any register | |
6073 | * IMM == 32-bit immediate | |
6074 | * | |
6075 | * Output: | |
6076 | * R0 - 8/16/32-bit skb data converted to cpu endianness | |
6077 | */ | |
58e2af8b | 6078 | static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn) |
ddd872bc | 6079 | { |
638f5b90 | 6080 | struct bpf_reg_state *regs = cur_regs(env); |
ddd872bc | 6081 | u8 mode = BPF_MODE(insn->code); |
ddd872bc AS |
6082 | int i, err; |
6083 | ||
24701ece | 6084 | if (!may_access_skb(env->prog->type)) { |
61bd5218 | 6085 | verbose(env, "BPF_LD_[ABS|IND] instructions not allowed for this program type\n"); |
ddd872bc AS |
6086 | return -EINVAL; |
6087 | } | |
6088 | ||
e0cea7ce DB |
6089 | if (!env->ops->gen_ld_abs) { |
6090 | verbose(env, "bpf verifier is misconfigured\n"); | |
6091 | return -EINVAL; | |
6092 | } | |
6093 | ||
f910cefa | 6094 | if (env->subprog_cnt > 1) { |
f4d7e40a AS |
6095 | /* when program has LD_ABS insn JITs and interpreter assume |
6096 | * that r1 == ctx == skb which is not the case for callees | |
6097 | * that can have arbitrary arguments. It's problematic | |
6098 | * for main prog as well since JITs would need to analyze | |
6099 | * all functions in order to make proper register save/restore | |
6100 | * decisions in the main prog. Hence disallow LD_ABS with calls | |
6101 | */ | |
6102 | verbose(env, "BPF_LD_[ABS|IND] instructions cannot be mixed with bpf-to-bpf calls\n"); | |
6103 | return -EINVAL; | |
6104 | } | |
6105 | ||
ddd872bc | 6106 | if (insn->dst_reg != BPF_REG_0 || insn->off != 0 || |
d82bccc6 | 6107 | BPF_SIZE(insn->code) == BPF_DW || |
ddd872bc | 6108 | (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) { |
61bd5218 | 6109 | verbose(env, "BPF_LD_[ABS|IND] uses reserved fields\n"); |
ddd872bc AS |
6110 | return -EINVAL; |
6111 | } | |
6112 | ||
6113 | /* check whether implicit source operand (register R6) is readable */ | |
dc503a8a | 6114 | err = check_reg_arg(env, BPF_REG_6, SRC_OP); |
ddd872bc AS |
6115 | if (err) |
6116 | return err; | |
6117 | ||
fd978bf7 JS |
6118 | /* Disallow usage of BPF_LD_[ABS|IND] with reference tracking, as |
6119 | * gen_ld_abs() may terminate the program at runtime, leading to | |
6120 | * reference leak. | |
6121 | */ | |
6122 | err = check_reference_leak(env); | |
6123 | if (err) { | |
6124 | verbose(env, "BPF_LD_[ABS|IND] cannot be mixed with socket references\n"); | |
6125 | return err; | |
6126 | } | |
6127 | ||
d83525ca AS |
6128 | if (env->cur_state->active_spin_lock) { |
6129 | verbose(env, "BPF_LD_[ABS|IND] cannot be used inside bpf_spin_lock-ed region\n"); | |
6130 | return -EINVAL; | |
6131 | } | |
6132 | ||
ddd872bc | 6133 | if (regs[BPF_REG_6].type != PTR_TO_CTX) { |
61bd5218 JK |
6134 | verbose(env, |
6135 | "at the time of BPF_LD_ABS|IND R6 != pointer to skb\n"); | |
ddd872bc AS |
6136 | return -EINVAL; |
6137 | } | |
6138 | ||
6139 | if (mode == BPF_IND) { | |
6140 | /* check explicit source operand */ | |
dc503a8a | 6141 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
ddd872bc AS |
6142 | if (err) |
6143 | return err; | |
6144 | } | |
6145 | ||
6146 | /* reset caller saved regs to unreadable */ | |
dc503a8a | 6147 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
61bd5218 | 6148 | mark_reg_not_init(env, regs, caller_saved[i]); |
dc503a8a EC |
6149 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
6150 | } | |
ddd872bc AS |
6151 | |
6152 | /* mark destination R0 register as readable, since it contains | |
dc503a8a EC |
6153 | * the value fetched from the packet. |
6154 | * Already marked as written above. | |
ddd872bc | 6155 | */ |
61bd5218 | 6156 | mark_reg_unknown(env, regs, BPF_REG_0); |
5327ed3d JW |
6157 | /* ld_abs load up to 32-bit skb data. */ |
6158 | regs[BPF_REG_0].subreg_def = env->insn_idx + 1; | |
ddd872bc AS |
6159 | return 0; |
6160 | } | |
6161 | ||
390ee7e2 AS |
6162 | static int check_return_code(struct bpf_verifier_env *env) |
6163 | { | |
5cf1e914 | 6164 | struct tnum enforce_attach_type_range = tnum_unknown; |
390ee7e2 AS |
6165 | struct bpf_reg_state *reg; |
6166 | struct tnum range = tnum_range(0, 1); | |
6167 | ||
6168 | switch (env->prog->type) { | |
983695fa DB |
6169 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
6170 | if (env->prog->expected_attach_type == BPF_CGROUP_UDP4_RECVMSG || | |
6171 | env->prog->expected_attach_type == BPF_CGROUP_UDP6_RECVMSG) | |
6172 | range = tnum_range(1, 1); | |
ed4ed404 | 6173 | break; |
390ee7e2 | 6174 | case BPF_PROG_TYPE_CGROUP_SKB: |
5cf1e914 | 6175 | if (env->prog->expected_attach_type == BPF_CGROUP_INET_EGRESS) { |
6176 | range = tnum_range(0, 3); | |
6177 | enforce_attach_type_range = tnum_range(2, 3); | |
6178 | } | |
ed4ed404 | 6179 | break; |
390ee7e2 AS |
6180 | case BPF_PROG_TYPE_CGROUP_SOCK: |
6181 | case BPF_PROG_TYPE_SOCK_OPS: | |
ebc614f6 | 6182 | case BPF_PROG_TYPE_CGROUP_DEVICE: |
7b146ceb | 6183 | case BPF_PROG_TYPE_CGROUP_SYSCTL: |
0d01da6a | 6184 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: |
390ee7e2 AS |
6185 | break; |
6186 | default: | |
6187 | return 0; | |
6188 | } | |
6189 | ||
638f5b90 | 6190 | reg = cur_regs(env) + BPF_REG_0; |
390ee7e2 | 6191 | if (reg->type != SCALAR_VALUE) { |
61bd5218 | 6192 | verbose(env, "At program exit the register R0 is not a known value (%s)\n", |
390ee7e2 AS |
6193 | reg_type_str[reg->type]); |
6194 | return -EINVAL; | |
6195 | } | |
6196 | ||
6197 | if (!tnum_in(range, reg->var_off)) { | |
5cf1e914 | 6198 | char tn_buf[48]; |
6199 | ||
61bd5218 | 6200 | verbose(env, "At program exit the register R0 "); |
390ee7e2 | 6201 | if (!tnum_is_unknown(reg->var_off)) { |
390ee7e2 | 6202 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
61bd5218 | 6203 | verbose(env, "has value %s", tn_buf); |
390ee7e2 | 6204 | } else { |
61bd5218 | 6205 | verbose(env, "has unknown scalar value"); |
390ee7e2 | 6206 | } |
5cf1e914 | 6207 | tnum_strn(tn_buf, sizeof(tn_buf), range); |
983695fa | 6208 | verbose(env, " should have been in %s\n", tn_buf); |
390ee7e2 AS |
6209 | return -EINVAL; |
6210 | } | |
5cf1e914 | 6211 | |
6212 | if (!tnum_is_unknown(enforce_attach_type_range) && | |
6213 | tnum_in(enforce_attach_type_range, reg->var_off)) | |
6214 | env->prog->enforce_expected_attach_type = 1; | |
390ee7e2 AS |
6215 | return 0; |
6216 | } | |
6217 | ||
475fb78f AS |
6218 | /* non-recursive DFS pseudo code |
6219 | * 1 procedure DFS-iterative(G,v): | |
6220 | * 2 label v as discovered | |
6221 | * 3 let S be a stack | |
6222 | * 4 S.push(v) | |
6223 | * 5 while S is not empty | |
6224 | * 6 t <- S.pop() | |
6225 | * 7 if t is what we're looking for: | |
6226 | * 8 return t | |
6227 | * 9 for all edges e in G.adjacentEdges(t) do | |
6228 | * 10 if edge e is already labelled | |
6229 | * 11 continue with the next edge | |
6230 | * 12 w <- G.adjacentVertex(t,e) | |
6231 | * 13 if vertex w is not discovered and not explored | |
6232 | * 14 label e as tree-edge | |
6233 | * 15 label w as discovered | |
6234 | * 16 S.push(w) | |
6235 | * 17 continue at 5 | |
6236 | * 18 else if vertex w is discovered | |
6237 | * 19 label e as back-edge | |
6238 | * 20 else | |
6239 | * 21 // vertex w is explored | |
6240 | * 22 label e as forward- or cross-edge | |
6241 | * 23 label t as explored | |
6242 | * 24 S.pop() | |
6243 | * | |
6244 | * convention: | |
6245 | * 0x10 - discovered | |
6246 | * 0x11 - discovered and fall-through edge labelled | |
6247 | * 0x12 - discovered and fall-through and branch edges labelled | |
6248 | * 0x20 - explored | |
6249 | */ | |
6250 | ||
6251 | enum { | |
6252 | DISCOVERED = 0x10, | |
6253 | EXPLORED = 0x20, | |
6254 | FALLTHROUGH = 1, | |
6255 | BRANCH = 2, | |
6256 | }; | |
6257 | ||
dc2a4ebc AS |
6258 | static u32 state_htab_size(struct bpf_verifier_env *env) |
6259 | { | |
6260 | return env->prog->len; | |
6261 | } | |
6262 | ||
5d839021 AS |
6263 | static struct bpf_verifier_state_list **explored_state( |
6264 | struct bpf_verifier_env *env, | |
6265 | int idx) | |
6266 | { | |
dc2a4ebc AS |
6267 | struct bpf_verifier_state *cur = env->cur_state; |
6268 | struct bpf_func_state *state = cur->frame[cur->curframe]; | |
6269 | ||
6270 | return &env->explored_states[(idx ^ state->callsite) % state_htab_size(env)]; | |
5d839021 AS |
6271 | } |
6272 | ||
6273 | static void init_explored_state(struct bpf_verifier_env *env, int idx) | |
6274 | { | |
a8f500af | 6275 | env->insn_aux_data[idx].prune_point = true; |
5d839021 | 6276 | } |
f1bca824 | 6277 | |
475fb78f AS |
6278 | /* t, w, e - match pseudo-code above: |
6279 | * t - index of current instruction | |
6280 | * w - next instruction | |
6281 | * e - edge | |
6282 | */ | |
2589726d AS |
6283 | static int push_insn(int t, int w, int e, struct bpf_verifier_env *env, |
6284 | bool loop_ok) | |
475fb78f | 6285 | { |
7df737e9 AS |
6286 | int *insn_stack = env->cfg.insn_stack; |
6287 | int *insn_state = env->cfg.insn_state; | |
6288 | ||
475fb78f AS |
6289 | if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH)) |
6290 | return 0; | |
6291 | ||
6292 | if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH)) | |
6293 | return 0; | |
6294 | ||
6295 | if (w < 0 || w >= env->prog->len) { | |
d9762e84 | 6296 | verbose_linfo(env, t, "%d: ", t); |
61bd5218 | 6297 | verbose(env, "jump out of range from insn %d to %d\n", t, w); |
475fb78f AS |
6298 | return -EINVAL; |
6299 | } | |
6300 | ||
f1bca824 AS |
6301 | if (e == BRANCH) |
6302 | /* mark branch target for state pruning */ | |
5d839021 | 6303 | init_explored_state(env, w); |
f1bca824 | 6304 | |
475fb78f AS |
6305 | if (insn_state[w] == 0) { |
6306 | /* tree-edge */ | |
6307 | insn_state[t] = DISCOVERED | e; | |
6308 | insn_state[w] = DISCOVERED; | |
7df737e9 | 6309 | if (env->cfg.cur_stack >= env->prog->len) |
475fb78f | 6310 | return -E2BIG; |
7df737e9 | 6311 | insn_stack[env->cfg.cur_stack++] = w; |
475fb78f AS |
6312 | return 1; |
6313 | } else if ((insn_state[w] & 0xF0) == DISCOVERED) { | |
2589726d AS |
6314 | if (loop_ok && env->allow_ptr_leaks) |
6315 | return 0; | |
d9762e84 MKL |
6316 | verbose_linfo(env, t, "%d: ", t); |
6317 | verbose_linfo(env, w, "%d: ", w); | |
61bd5218 | 6318 | verbose(env, "back-edge from insn %d to %d\n", t, w); |
475fb78f AS |
6319 | return -EINVAL; |
6320 | } else if (insn_state[w] == EXPLORED) { | |
6321 | /* forward- or cross-edge */ | |
6322 | insn_state[t] = DISCOVERED | e; | |
6323 | } else { | |
61bd5218 | 6324 | verbose(env, "insn state internal bug\n"); |
475fb78f AS |
6325 | return -EFAULT; |
6326 | } | |
6327 | return 0; | |
6328 | } | |
6329 | ||
6330 | /* non-recursive depth-first-search to detect loops in BPF program | |
6331 | * loop == back-edge in directed graph | |
6332 | */ | |
58e2af8b | 6333 | static int check_cfg(struct bpf_verifier_env *env) |
475fb78f AS |
6334 | { |
6335 | struct bpf_insn *insns = env->prog->insnsi; | |
6336 | int insn_cnt = env->prog->len; | |
7df737e9 | 6337 | int *insn_stack, *insn_state; |
475fb78f AS |
6338 | int ret = 0; |
6339 | int i, t; | |
6340 | ||
7df737e9 | 6341 | insn_state = env->cfg.insn_state = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
475fb78f AS |
6342 | if (!insn_state) |
6343 | return -ENOMEM; | |
6344 | ||
7df737e9 | 6345 | insn_stack = env->cfg.insn_stack = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
475fb78f | 6346 | if (!insn_stack) { |
71dde681 | 6347 | kvfree(insn_state); |
475fb78f AS |
6348 | return -ENOMEM; |
6349 | } | |
6350 | ||
6351 | insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */ | |
6352 | insn_stack[0] = 0; /* 0 is the first instruction */ | |
7df737e9 | 6353 | env->cfg.cur_stack = 1; |
475fb78f AS |
6354 | |
6355 | peek_stack: | |
7df737e9 | 6356 | if (env->cfg.cur_stack == 0) |
475fb78f | 6357 | goto check_state; |
7df737e9 | 6358 | t = insn_stack[env->cfg.cur_stack - 1]; |
475fb78f | 6359 | |
092ed096 JW |
6360 | if (BPF_CLASS(insns[t].code) == BPF_JMP || |
6361 | BPF_CLASS(insns[t].code) == BPF_JMP32) { | |
475fb78f AS |
6362 | u8 opcode = BPF_OP(insns[t].code); |
6363 | ||
6364 | if (opcode == BPF_EXIT) { | |
6365 | goto mark_explored; | |
6366 | } else if (opcode == BPF_CALL) { | |
2589726d | 6367 | ret = push_insn(t, t + 1, FALLTHROUGH, env, false); |
475fb78f AS |
6368 | if (ret == 1) |
6369 | goto peek_stack; | |
6370 | else if (ret < 0) | |
6371 | goto err_free; | |
07016151 | 6372 | if (t + 1 < insn_cnt) |
5d839021 | 6373 | init_explored_state(env, t + 1); |
cc8b0b92 | 6374 | if (insns[t].src_reg == BPF_PSEUDO_CALL) { |
5d839021 | 6375 | init_explored_state(env, t); |
2589726d AS |
6376 | ret = push_insn(t, t + insns[t].imm + 1, BRANCH, |
6377 | env, false); | |
cc8b0b92 AS |
6378 | if (ret == 1) |
6379 | goto peek_stack; | |
6380 | else if (ret < 0) | |
6381 | goto err_free; | |
6382 | } | |
475fb78f AS |
6383 | } else if (opcode == BPF_JA) { |
6384 | if (BPF_SRC(insns[t].code) != BPF_K) { | |
6385 | ret = -EINVAL; | |
6386 | goto err_free; | |
6387 | } | |
6388 | /* unconditional jump with single edge */ | |
6389 | ret = push_insn(t, t + insns[t].off + 1, | |
2589726d | 6390 | FALLTHROUGH, env, true); |
475fb78f AS |
6391 | if (ret == 1) |
6392 | goto peek_stack; | |
6393 | else if (ret < 0) | |
6394 | goto err_free; | |
b5dc0163 AS |
6395 | /* unconditional jmp is not a good pruning point, |
6396 | * but it's marked, since backtracking needs | |
6397 | * to record jmp history in is_state_visited(). | |
6398 | */ | |
6399 | init_explored_state(env, t + insns[t].off + 1); | |
f1bca824 AS |
6400 | /* tell verifier to check for equivalent states |
6401 | * after every call and jump | |
6402 | */ | |
c3de6317 | 6403 | if (t + 1 < insn_cnt) |
5d839021 | 6404 | init_explored_state(env, t + 1); |
475fb78f AS |
6405 | } else { |
6406 | /* conditional jump with two edges */ | |
5d839021 | 6407 | init_explored_state(env, t); |
2589726d | 6408 | ret = push_insn(t, t + 1, FALLTHROUGH, env, true); |
475fb78f AS |
6409 | if (ret == 1) |
6410 | goto peek_stack; | |
6411 | else if (ret < 0) | |
6412 | goto err_free; | |
6413 | ||
2589726d | 6414 | ret = push_insn(t, t + insns[t].off + 1, BRANCH, env, true); |
475fb78f AS |
6415 | if (ret == 1) |
6416 | goto peek_stack; | |
6417 | else if (ret < 0) | |
6418 | goto err_free; | |
6419 | } | |
6420 | } else { | |
6421 | /* all other non-branch instructions with single | |
6422 | * fall-through edge | |
6423 | */ | |
2589726d | 6424 | ret = push_insn(t, t + 1, FALLTHROUGH, env, false); |
475fb78f AS |
6425 | if (ret == 1) |
6426 | goto peek_stack; | |
6427 | else if (ret < 0) | |
6428 | goto err_free; | |
6429 | } | |
6430 | ||
6431 | mark_explored: | |
6432 | insn_state[t] = EXPLORED; | |
7df737e9 | 6433 | if (env->cfg.cur_stack-- <= 0) { |
61bd5218 | 6434 | verbose(env, "pop stack internal bug\n"); |
475fb78f AS |
6435 | ret = -EFAULT; |
6436 | goto err_free; | |
6437 | } | |
6438 | goto peek_stack; | |
6439 | ||
6440 | check_state: | |
6441 | for (i = 0; i < insn_cnt; i++) { | |
6442 | if (insn_state[i] != EXPLORED) { | |
61bd5218 | 6443 | verbose(env, "unreachable insn %d\n", i); |
475fb78f AS |
6444 | ret = -EINVAL; |
6445 | goto err_free; | |
6446 | } | |
6447 | } | |
6448 | ret = 0; /* cfg looks good */ | |
6449 | ||
6450 | err_free: | |
71dde681 AS |
6451 | kvfree(insn_state); |
6452 | kvfree(insn_stack); | |
7df737e9 | 6453 | env->cfg.insn_state = env->cfg.insn_stack = NULL; |
475fb78f AS |
6454 | return ret; |
6455 | } | |
6456 | ||
838e9690 YS |
6457 | /* The minimum supported BTF func info size */ |
6458 | #define MIN_BPF_FUNCINFO_SIZE 8 | |
6459 | #define MAX_FUNCINFO_REC_SIZE 252 | |
6460 | ||
c454a46b MKL |
6461 | static int check_btf_func(struct bpf_verifier_env *env, |
6462 | const union bpf_attr *attr, | |
6463 | union bpf_attr __user *uattr) | |
838e9690 | 6464 | { |
d0b2818e | 6465 | u32 i, nfuncs, urec_size, min_size; |
838e9690 | 6466 | u32 krec_size = sizeof(struct bpf_func_info); |
c454a46b | 6467 | struct bpf_func_info *krecord; |
838e9690 | 6468 | const struct btf_type *type; |
c454a46b MKL |
6469 | struct bpf_prog *prog; |
6470 | const struct btf *btf; | |
838e9690 | 6471 | void __user *urecord; |
d0b2818e | 6472 | u32 prev_offset = 0; |
838e9690 YS |
6473 | int ret = 0; |
6474 | ||
6475 | nfuncs = attr->func_info_cnt; | |
6476 | if (!nfuncs) | |
6477 | return 0; | |
6478 | ||
6479 | if (nfuncs != env->subprog_cnt) { | |
6480 | verbose(env, "number of funcs in func_info doesn't match number of subprogs\n"); | |
6481 | return -EINVAL; | |
6482 | } | |
6483 | ||
6484 | urec_size = attr->func_info_rec_size; | |
6485 | if (urec_size < MIN_BPF_FUNCINFO_SIZE || | |
6486 | urec_size > MAX_FUNCINFO_REC_SIZE || | |
6487 | urec_size % sizeof(u32)) { | |
6488 | verbose(env, "invalid func info rec size %u\n", urec_size); | |
6489 | return -EINVAL; | |
6490 | } | |
6491 | ||
c454a46b MKL |
6492 | prog = env->prog; |
6493 | btf = prog->aux->btf; | |
838e9690 YS |
6494 | |
6495 | urecord = u64_to_user_ptr(attr->func_info); | |
6496 | min_size = min_t(u32, krec_size, urec_size); | |
6497 | ||
ba64e7d8 | 6498 | krecord = kvcalloc(nfuncs, krec_size, GFP_KERNEL | __GFP_NOWARN); |
c454a46b MKL |
6499 | if (!krecord) |
6500 | return -ENOMEM; | |
ba64e7d8 | 6501 | |
838e9690 YS |
6502 | for (i = 0; i < nfuncs; i++) { |
6503 | ret = bpf_check_uarg_tail_zero(urecord, krec_size, urec_size); | |
6504 | if (ret) { | |
6505 | if (ret == -E2BIG) { | |
6506 | verbose(env, "nonzero tailing record in func info"); | |
6507 | /* set the size kernel expects so loader can zero | |
6508 | * out the rest of the record. | |
6509 | */ | |
6510 | if (put_user(min_size, &uattr->func_info_rec_size)) | |
6511 | ret = -EFAULT; | |
6512 | } | |
c454a46b | 6513 | goto err_free; |
838e9690 YS |
6514 | } |
6515 | ||
ba64e7d8 | 6516 | if (copy_from_user(&krecord[i], urecord, min_size)) { |
838e9690 | 6517 | ret = -EFAULT; |
c454a46b | 6518 | goto err_free; |
838e9690 YS |
6519 | } |
6520 | ||
d30d42e0 | 6521 | /* check insn_off */ |
838e9690 | 6522 | if (i == 0) { |
d30d42e0 | 6523 | if (krecord[i].insn_off) { |
838e9690 | 6524 | verbose(env, |
d30d42e0 MKL |
6525 | "nonzero insn_off %u for the first func info record", |
6526 | krecord[i].insn_off); | |
838e9690 | 6527 | ret = -EINVAL; |
c454a46b | 6528 | goto err_free; |
838e9690 | 6529 | } |
d30d42e0 | 6530 | } else if (krecord[i].insn_off <= prev_offset) { |
838e9690 YS |
6531 | verbose(env, |
6532 | "same or smaller insn offset (%u) than previous func info record (%u)", | |
d30d42e0 | 6533 | krecord[i].insn_off, prev_offset); |
838e9690 | 6534 | ret = -EINVAL; |
c454a46b | 6535 | goto err_free; |
838e9690 YS |
6536 | } |
6537 | ||
d30d42e0 | 6538 | if (env->subprog_info[i].start != krecord[i].insn_off) { |
838e9690 YS |
6539 | verbose(env, "func_info BTF section doesn't match subprog layout in BPF program\n"); |
6540 | ret = -EINVAL; | |
c454a46b | 6541 | goto err_free; |
838e9690 YS |
6542 | } |
6543 | ||
6544 | /* check type_id */ | |
ba64e7d8 | 6545 | type = btf_type_by_id(btf, krecord[i].type_id); |
838e9690 YS |
6546 | if (!type || BTF_INFO_KIND(type->info) != BTF_KIND_FUNC) { |
6547 | verbose(env, "invalid type id %d in func info", | |
ba64e7d8 | 6548 | krecord[i].type_id); |
838e9690 | 6549 | ret = -EINVAL; |
c454a46b | 6550 | goto err_free; |
838e9690 YS |
6551 | } |
6552 | ||
d30d42e0 | 6553 | prev_offset = krecord[i].insn_off; |
838e9690 YS |
6554 | urecord += urec_size; |
6555 | } | |
6556 | ||
ba64e7d8 YS |
6557 | prog->aux->func_info = krecord; |
6558 | prog->aux->func_info_cnt = nfuncs; | |
838e9690 YS |
6559 | return 0; |
6560 | ||
c454a46b | 6561 | err_free: |
ba64e7d8 | 6562 | kvfree(krecord); |
838e9690 YS |
6563 | return ret; |
6564 | } | |
6565 | ||
ba64e7d8 YS |
6566 | static void adjust_btf_func(struct bpf_verifier_env *env) |
6567 | { | |
6568 | int i; | |
6569 | ||
6570 | if (!env->prog->aux->func_info) | |
6571 | return; | |
6572 | ||
6573 | for (i = 0; i < env->subprog_cnt; i++) | |
d30d42e0 | 6574 | env->prog->aux->func_info[i].insn_off = env->subprog_info[i].start; |
ba64e7d8 YS |
6575 | } |
6576 | ||
c454a46b MKL |
6577 | #define MIN_BPF_LINEINFO_SIZE (offsetof(struct bpf_line_info, line_col) + \ |
6578 | sizeof(((struct bpf_line_info *)(0))->line_col)) | |
6579 | #define MAX_LINEINFO_REC_SIZE MAX_FUNCINFO_REC_SIZE | |
6580 | ||
6581 | static int check_btf_line(struct bpf_verifier_env *env, | |
6582 | const union bpf_attr *attr, | |
6583 | union bpf_attr __user *uattr) | |
6584 | { | |
6585 | u32 i, s, nr_linfo, ncopy, expected_size, rec_size, prev_offset = 0; | |
6586 | struct bpf_subprog_info *sub; | |
6587 | struct bpf_line_info *linfo; | |
6588 | struct bpf_prog *prog; | |
6589 | const struct btf *btf; | |
6590 | void __user *ulinfo; | |
6591 | int err; | |
6592 | ||
6593 | nr_linfo = attr->line_info_cnt; | |
6594 | if (!nr_linfo) | |
6595 | return 0; | |
6596 | ||
6597 | rec_size = attr->line_info_rec_size; | |
6598 | if (rec_size < MIN_BPF_LINEINFO_SIZE || | |
6599 | rec_size > MAX_LINEINFO_REC_SIZE || | |
6600 | rec_size & (sizeof(u32) - 1)) | |
6601 | return -EINVAL; | |
6602 | ||
6603 | /* Need to zero it in case the userspace may | |
6604 | * pass in a smaller bpf_line_info object. | |
6605 | */ | |
6606 | linfo = kvcalloc(nr_linfo, sizeof(struct bpf_line_info), | |
6607 | GFP_KERNEL | __GFP_NOWARN); | |
6608 | if (!linfo) | |
6609 | return -ENOMEM; | |
6610 | ||
6611 | prog = env->prog; | |
6612 | btf = prog->aux->btf; | |
6613 | ||
6614 | s = 0; | |
6615 | sub = env->subprog_info; | |
6616 | ulinfo = u64_to_user_ptr(attr->line_info); | |
6617 | expected_size = sizeof(struct bpf_line_info); | |
6618 | ncopy = min_t(u32, expected_size, rec_size); | |
6619 | for (i = 0; i < nr_linfo; i++) { | |
6620 | err = bpf_check_uarg_tail_zero(ulinfo, expected_size, rec_size); | |
6621 | if (err) { | |
6622 | if (err == -E2BIG) { | |
6623 | verbose(env, "nonzero tailing record in line_info"); | |
6624 | if (put_user(expected_size, | |
6625 | &uattr->line_info_rec_size)) | |
6626 | err = -EFAULT; | |
6627 | } | |
6628 | goto err_free; | |
6629 | } | |
6630 | ||
6631 | if (copy_from_user(&linfo[i], ulinfo, ncopy)) { | |
6632 | err = -EFAULT; | |
6633 | goto err_free; | |
6634 | } | |
6635 | ||
6636 | /* | |
6637 | * Check insn_off to ensure | |
6638 | * 1) strictly increasing AND | |
6639 | * 2) bounded by prog->len | |
6640 | * | |
6641 | * The linfo[0].insn_off == 0 check logically falls into | |
6642 | * the later "missing bpf_line_info for func..." case | |
6643 | * because the first linfo[0].insn_off must be the | |
6644 | * first sub also and the first sub must have | |
6645 | * subprog_info[0].start == 0. | |
6646 | */ | |
6647 | if ((i && linfo[i].insn_off <= prev_offset) || | |
6648 | linfo[i].insn_off >= prog->len) { | |
6649 | verbose(env, "Invalid line_info[%u].insn_off:%u (prev_offset:%u prog->len:%u)\n", | |
6650 | i, linfo[i].insn_off, prev_offset, | |
6651 | prog->len); | |
6652 | err = -EINVAL; | |
6653 | goto err_free; | |
6654 | } | |
6655 | ||
fdbaa0be MKL |
6656 | if (!prog->insnsi[linfo[i].insn_off].code) { |
6657 | verbose(env, | |
6658 | "Invalid insn code at line_info[%u].insn_off\n", | |
6659 | i); | |
6660 | err = -EINVAL; | |
6661 | goto err_free; | |
6662 | } | |
6663 | ||
23127b33 MKL |
6664 | if (!btf_name_by_offset(btf, linfo[i].line_off) || |
6665 | !btf_name_by_offset(btf, linfo[i].file_name_off)) { | |
c454a46b MKL |
6666 | verbose(env, "Invalid line_info[%u].line_off or .file_name_off\n", i); |
6667 | err = -EINVAL; | |
6668 | goto err_free; | |
6669 | } | |
6670 | ||
6671 | if (s != env->subprog_cnt) { | |
6672 | if (linfo[i].insn_off == sub[s].start) { | |
6673 | sub[s].linfo_idx = i; | |
6674 | s++; | |
6675 | } else if (sub[s].start < linfo[i].insn_off) { | |
6676 | verbose(env, "missing bpf_line_info for func#%u\n", s); | |
6677 | err = -EINVAL; | |
6678 | goto err_free; | |
6679 | } | |
6680 | } | |
6681 | ||
6682 | prev_offset = linfo[i].insn_off; | |
6683 | ulinfo += rec_size; | |
6684 | } | |
6685 | ||
6686 | if (s != env->subprog_cnt) { | |
6687 | verbose(env, "missing bpf_line_info for %u funcs starting from func#%u\n", | |
6688 | env->subprog_cnt - s, s); | |
6689 | err = -EINVAL; | |
6690 | goto err_free; | |
6691 | } | |
6692 | ||
6693 | prog->aux->linfo = linfo; | |
6694 | prog->aux->nr_linfo = nr_linfo; | |
6695 | ||
6696 | return 0; | |
6697 | ||
6698 | err_free: | |
6699 | kvfree(linfo); | |
6700 | return err; | |
6701 | } | |
6702 | ||
6703 | static int check_btf_info(struct bpf_verifier_env *env, | |
6704 | const union bpf_attr *attr, | |
6705 | union bpf_attr __user *uattr) | |
6706 | { | |
6707 | struct btf *btf; | |
6708 | int err; | |
6709 | ||
6710 | if (!attr->func_info_cnt && !attr->line_info_cnt) | |
6711 | return 0; | |
6712 | ||
6713 | btf = btf_get_by_fd(attr->prog_btf_fd); | |
6714 | if (IS_ERR(btf)) | |
6715 | return PTR_ERR(btf); | |
6716 | env->prog->aux->btf = btf; | |
6717 | ||
6718 | err = check_btf_func(env, attr, uattr); | |
6719 | if (err) | |
6720 | return err; | |
6721 | ||
6722 | err = check_btf_line(env, attr, uattr); | |
6723 | if (err) | |
6724 | return err; | |
6725 | ||
6726 | return 0; | |
ba64e7d8 YS |
6727 | } |
6728 | ||
f1174f77 EC |
6729 | /* check %cur's range satisfies %old's */ |
6730 | static bool range_within(struct bpf_reg_state *old, | |
6731 | struct bpf_reg_state *cur) | |
6732 | { | |
b03c9f9f EC |
6733 | return old->umin_value <= cur->umin_value && |
6734 | old->umax_value >= cur->umax_value && | |
6735 | old->smin_value <= cur->smin_value && | |
6736 | old->smax_value >= cur->smax_value; | |
f1174f77 EC |
6737 | } |
6738 | ||
6739 | /* Maximum number of register states that can exist at once */ | |
6740 | #define ID_MAP_SIZE (MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE) | |
6741 | struct idpair { | |
6742 | u32 old; | |
6743 | u32 cur; | |
6744 | }; | |
6745 | ||
6746 | /* If in the old state two registers had the same id, then they need to have | |
6747 | * the same id in the new state as well. But that id could be different from | |
6748 | * the old state, so we need to track the mapping from old to new ids. | |
6749 | * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent | |
6750 | * regs with old id 5 must also have new id 9 for the new state to be safe. But | |
6751 | * regs with a different old id could still have new id 9, we don't care about | |
6752 | * that. | |
6753 | * So we look through our idmap to see if this old id has been seen before. If | |
6754 | * so, we require the new id to match; otherwise, we add the id pair to the map. | |
969bf05e | 6755 | */ |
f1174f77 | 6756 | static bool check_ids(u32 old_id, u32 cur_id, struct idpair *idmap) |
969bf05e | 6757 | { |
f1174f77 | 6758 | unsigned int i; |
969bf05e | 6759 | |
f1174f77 EC |
6760 | for (i = 0; i < ID_MAP_SIZE; i++) { |
6761 | if (!idmap[i].old) { | |
6762 | /* Reached an empty slot; haven't seen this id before */ | |
6763 | idmap[i].old = old_id; | |
6764 | idmap[i].cur = cur_id; | |
6765 | return true; | |
6766 | } | |
6767 | if (idmap[i].old == old_id) | |
6768 | return idmap[i].cur == cur_id; | |
6769 | } | |
6770 | /* We ran out of idmap slots, which should be impossible */ | |
6771 | WARN_ON_ONCE(1); | |
6772 | return false; | |
6773 | } | |
6774 | ||
9242b5f5 AS |
6775 | static void clean_func_state(struct bpf_verifier_env *env, |
6776 | struct bpf_func_state *st) | |
6777 | { | |
6778 | enum bpf_reg_liveness live; | |
6779 | int i, j; | |
6780 | ||
6781 | for (i = 0; i < BPF_REG_FP; i++) { | |
6782 | live = st->regs[i].live; | |
6783 | /* liveness must not touch this register anymore */ | |
6784 | st->regs[i].live |= REG_LIVE_DONE; | |
6785 | if (!(live & REG_LIVE_READ)) | |
6786 | /* since the register is unused, clear its state | |
6787 | * to make further comparison simpler | |
6788 | */ | |
6789 | __mark_reg_not_init(&st->regs[i]); | |
6790 | } | |
6791 | ||
6792 | for (i = 0; i < st->allocated_stack / BPF_REG_SIZE; i++) { | |
6793 | live = st->stack[i].spilled_ptr.live; | |
6794 | /* liveness must not touch this stack slot anymore */ | |
6795 | st->stack[i].spilled_ptr.live |= REG_LIVE_DONE; | |
6796 | if (!(live & REG_LIVE_READ)) { | |
6797 | __mark_reg_not_init(&st->stack[i].spilled_ptr); | |
6798 | for (j = 0; j < BPF_REG_SIZE; j++) | |
6799 | st->stack[i].slot_type[j] = STACK_INVALID; | |
6800 | } | |
6801 | } | |
6802 | } | |
6803 | ||
6804 | static void clean_verifier_state(struct bpf_verifier_env *env, | |
6805 | struct bpf_verifier_state *st) | |
6806 | { | |
6807 | int i; | |
6808 | ||
6809 | if (st->frame[0]->regs[0].live & REG_LIVE_DONE) | |
6810 | /* all regs in this state in all frames were already marked */ | |
6811 | return; | |
6812 | ||
6813 | for (i = 0; i <= st->curframe; i++) | |
6814 | clean_func_state(env, st->frame[i]); | |
6815 | } | |
6816 | ||
6817 | /* the parentage chains form a tree. | |
6818 | * the verifier states are added to state lists at given insn and | |
6819 | * pushed into state stack for future exploration. | |
6820 | * when the verifier reaches bpf_exit insn some of the verifer states | |
6821 | * stored in the state lists have their final liveness state already, | |
6822 | * but a lot of states will get revised from liveness point of view when | |
6823 | * the verifier explores other branches. | |
6824 | * Example: | |
6825 | * 1: r0 = 1 | |
6826 | * 2: if r1 == 100 goto pc+1 | |
6827 | * 3: r0 = 2 | |
6828 | * 4: exit | |
6829 | * when the verifier reaches exit insn the register r0 in the state list of | |
6830 | * insn 2 will be seen as !REG_LIVE_READ. Then the verifier pops the other_branch | |
6831 | * of insn 2 and goes exploring further. At the insn 4 it will walk the | |
6832 | * parentage chain from insn 4 into insn 2 and will mark r0 as REG_LIVE_READ. | |
6833 | * | |
6834 | * Since the verifier pushes the branch states as it sees them while exploring | |
6835 | * the program the condition of walking the branch instruction for the second | |
6836 | * time means that all states below this branch were already explored and | |
6837 | * their final liveness markes are already propagated. | |
6838 | * Hence when the verifier completes the search of state list in is_state_visited() | |
6839 | * we can call this clean_live_states() function to mark all liveness states | |
6840 | * as REG_LIVE_DONE to indicate that 'parent' pointers of 'struct bpf_reg_state' | |
6841 | * will not be used. | |
6842 | * This function also clears the registers and stack for states that !READ | |
6843 | * to simplify state merging. | |
6844 | * | |
6845 | * Important note here that walking the same branch instruction in the callee | |
6846 | * doesn't meant that the states are DONE. The verifier has to compare | |
6847 | * the callsites | |
6848 | */ | |
6849 | static void clean_live_states(struct bpf_verifier_env *env, int insn, | |
6850 | struct bpf_verifier_state *cur) | |
6851 | { | |
6852 | struct bpf_verifier_state_list *sl; | |
6853 | int i; | |
6854 | ||
5d839021 | 6855 | sl = *explored_state(env, insn); |
a8f500af | 6856 | while (sl) { |
2589726d AS |
6857 | if (sl->state.branches) |
6858 | goto next; | |
dc2a4ebc AS |
6859 | if (sl->state.insn_idx != insn || |
6860 | sl->state.curframe != cur->curframe) | |
9242b5f5 AS |
6861 | goto next; |
6862 | for (i = 0; i <= cur->curframe; i++) | |
6863 | if (sl->state.frame[i]->callsite != cur->frame[i]->callsite) | |
6864 | goto next; | |
6865 | clean_verifier_state(env, &sl->state); | |
6866 | next: | |
6867 | sl = sl->next; | |
6868 | } | |
6869 | } | |
6870 | ||
f1174f77 | 6871 | /* Returns true if (rold safe implies rcur safe) */ |
1b688a19 EC |
6872 | static bool regsafe(struct bpf_reg_state *rold, struct bpf_reg_state *rcur, |
6873 | struct idpair *idmap) | |
f1174f77 | 6874 | { |
f4d7e40a AS |
6875 | bool equal; |
6876 | ||
dc503a8a EC |
6877 | if (!(rold->live & REG_LIVE_READ)) |
6878 | /* explored state didn't use this */ | |
6879 | return true; | |
6880 | ||
679c782d | 6881 | equal = memcmp(rold, rcur, offsetof(struct bpf_reg_state, parent)) == 0; |
f4d7e40a AS |
6882 | |
6883 | if (rold->type == PTR_TO_STACK) | |
6884 | /* two stack pointers are equal only if they're pointing to | |
6885 | * the same stack frame, since fp-8 in foo != fp-8 in bar | |
6886 | */ | |
6887 | return equal && rold->frameno == rcur->frameno; | |
6888 | ||
6889 | if (equal) | |
969bf05e AS |
6890 | return true; |
6891 | ||
f1174f77 EC |
6892 | if (rold->type == NOT_INIT) |
6893 | /* explored state can't have used this */ | |
969bf05e | 6894 | return true; |
f1174f77 EC |
6895 | if (rcur->type == NOT_INIT) |
6896 | return false; | |
6897 | switch (rold->type) { | |
6898 | case SCALAR_VALUE: | |
6899 | if (rcur->type == SCALAR_VALUE) { | |
b5dc0163 AS |
6900 | if (!rold->precise && !rcur->precise) |
6901 | return true; | |
f1174f77 EC |
6902 | /* new val must satisfy old val knowledge */ |
6903 | return range_within(rold, rcur) && | |
6904 | tnum_in(rold->var_off, rcur->var_off); | |
6905 | } else { | |
179d1c56 JH |
6906 | /* We're trying to use a pointer in place of a scalar. |
6907 | * Even if the scalar was unbounded, this could lead to | |
6908 | * pointer leaks because scalars are allowed to leak | |
6909 | * while pointers are not. We could make this safe in | |
6910 | * special cases if root is calling us, but it's | |
6911 | * probably not worth the hassle. | |
f1174f77 | 6912 | */ |
179d1c56 | 6913 | return false; |
f1174f77 EC |
6914 | } |
6915 | case PTR_TO_MAP_VALUE: | |
1b688a19 EC |
6916 | /* If the new min/max/var_off satisfy the old ones and |
6917 | * everything else matches, we are OK. | |
d83525ca AS |
6918 | * 'id' is not compared, since it's only used for maps with |
6919 | * bpf_spin_lock inside map element and in such cases if | |
6920 | * the rest of the prog is valid for one map element then | |
6921 | * it's valid for all map elements regardless of the key | |
6922 | * used in bpf_map_lookup() | |
1b688a19 EC |
6923 | */ |
6924 | return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 && | |
6925 | range_within(rold, rcur) && | |
6926 | tnum_in(rold->var_off, rcur->var_off); | |
f1174f77 EC |
6927 | case PTR_TO_MAP_VALUE_OR_NULL: |
6928 | /* a PTR_TO_MAP_VALUE could be safe to use as a | |
6929 | * PTR_TO_MAP_VALUE_OR_NULL into the same map. | |
6930 | * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL- | |
6931 | * checked, doing so could have affected others with the same | |
6932 | * id, and we can't check for that because we lost the id when | |
6933 | * we converted to a PTR_TO_MAP_VALUE. | |
6934 | */ | |
6935 | if (rcur->type != PTR_TO_MAP_VALUE_OR_NULL) | |
6936 | return false; | |
6937 | if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, id))) | |
6938 | return false; | |
6939 | /* Check our ids match any regs they're supposed to */ | |
6940 | return check_ids(rold->id, rcur->id, idmap); | |
de8f3a83 | 6941 | case PTR_TO_PACKET_META: |
f1174f77 | 6942 | case PTR_TO_PACKET: |
de8f3a83 | 6943 | if (rcur->type != rold->type) |
f1174f77 EC |
6944 | return false; |
6945 | /* We must have at least as much range as the old ptr | |
6946 | * did, so that any accesses which were safe before are | |
6947 | * still safe. This is true even if old range < old off, | |
6948 | * since someone could have accessed through (ptr - k), or | |
6949 | * even done ptr -= k in a register, to get a safe access. | |
6950 | */ | |
6951 | if (rold->range > rcur->range) | |
6952 | return false; | |
6953 | /* If the offsets don't match, we can't trust our alignment; | |
6954 | * nor can we be sure that we won't fall out of range. | |
6955 | */ | |
6956 | if (rold->off != rcur->off) | |
6957 | return false; | |
6958 | /* id relations must be preserved */ | |
6959 | if (rold->id && !check_ids(rold->id, rcur->id, idmap)) | |
6960 | return false; | |
6961 | /* new val must satisfy old val knowledge */ | |
6962 | return range_within(rold, rcur) && | |
6963 | tnum_in(rold->var_off, rcur->var_off); | |
6964 | case PTR_TO_CTX: | |
6965 | case CONST_PTR_TO_MAP: | |
f1174f77 | 6966 | case PTR_TO_PACKET_END: |
d58e468b | 6967 | case PTR_TO_FLOW_KEYS: |
c64b7983 JS |
6968 | case PTR_TO_SOCKET: |
6969 | case PTR_TO_SOCKET_OR_NULL: | |
46f8bc92 MKL |
6970 | case PTR_TO_SOCK_COMMON: |
6971 | case PTR_TO_SOCK_COMMON_OR_NULL: | |
655a51e5 MKL |
6972 | case PTR_TO_TCP_SOCK: |
6973 | case PTR_TO_TCP_SOCK_OR_NULL: | |
fada7fdc | 6974 | case PTR_TO_XDP_SOCK: |
f1174f77 EC |
6975 | /* Only valid matches are exact, which memcmp() above |
6976 | * would have accepted | |
6977 | */ | |
6978 | default: | |
6979 | /* Don't know what's going on, just say it's not safe */ | |
6980 | return false; | |
6981 | } | |
969bf05e | 6982 | |
f1174f77 EC |
6983 | /* Shouldn't get here; if we do, say it's not safe */ |
6984 | WARN_ON_ONCE(1); | |
969bf05e AS |
6985 | return false; |
6986 | } | |
6987 | ||
f4d7e40a AS |
6988 | static bool stacksafe(struct bpf_func_state *old, |
6989 | struct bpf_func_state *cur, | |
638f5b90 AS |
6990 | struct idpair *idmap) |
6991 | { | |
6992 | int i, spi; | |
6993 | ||
638f5b90 AS |
6994 | /* walk slots of the explored stack and ignore any additional |
6995 | * slots in the current stack, since explored(safe) state | |
6996 | * didn't use them | |
6997 | */ | |
6998 | for (i = 0; i < old->allocated_stack; i++) { | |
6999 | spi = i / BPF_REG_SIZE; | |
7000 | ||
b233920c AS |
7001 | if (!(old->stack[spi].spilled_ptr.live & REG_LIVE_READ)) { |
7002 | i += BPF_REG_SIZE - 1; | |
cc2b14d5 | 7003 | /* explored state didn't use this */ |
fd05e57b | 7004 | continue; |
b233920c | 7005 | } |
cc2b14d5 | 7006 | |
638f5b90 AS |
7007 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID) |
7008 | continue; | |
19e2dbb7 AS |
7009 | |
7010 | /* explored stack has more populated slots than current stack | |
7011 | * and these slots were used | |
7012 | */ | |
7013 | if (i >= cur->allocated_stack) | |
7014 | return false; | |
7015 | ||
cc2b14d5 AS |
7016 | /* if old state was safe with misc data in the stack |
7017 | * it will be safe with zero-initialized stack. | |
7018 | * The opposite is not true | |
7019 | */ | |
7020 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC && | |
7021 | cur->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_ZERO) | |
7022 | continue; | |
638f5b90 AS |
7023 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] != |
7024 | cur->stack[spi].slot_type[i % BPF_REG_SIZE]) | |
7025 | /* Ex: old explored (safe) state has STACK_SPILL in | |
7026 | * this stack slot, but current has has STACK_MISC -> | |
7027 | * this verifier states are not equivalent, | |
7028 | * return false to continue verification of this path | |
7029 | */ | |
7030 | return false; | |
7031 | if (i % BPF_REG_SIZE) | |
7032 | continue; | |
7033 | if (old->stack[spi].slot_type[0] != STACK_SPILL) | |
7034 | continue; | |
7035 | if (!regsafe(&old->stack[spi].spilled_ptr, | |
7036 | &cur->stack[spi].spilled_ptr, | |
7037 | idmap)) | |
7038 | /* when explored and current stack slot are both storing | |
7039 | * spilled registers, check that stored pointers types | |
7040 | * are the same as well. | |
7041 | * Ex: explored safe path could have stored | |
7042 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8} | |
7043 | * but current path has stored: | |
7044 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16} | |
7045 | * such verifier states are not equivalent. | |
7046 | * return false to continue verification of this path | |
7047 | */ | |
7048 | return false; | |
7049 | } | |
7050 | return true; | |
7051 | } | |
7052 | ||
fd978bf7 JS |
7053 | static bool refsafe(struct bpf_func_state *old, struct bpf_func_state *cur) |
7054 | { | |
7055 | if (old->acquired_refs != cur->acquired_refs) | |
7056 | return false; | |
7057 | return !memcmp(old->refs, cur->refs, | |
7058 | sizeof(*old->refs) * old->acquired_refs); | |
7059 | } | |
7060 | ||
f1bca824 AS |
7061 | /* compare two verifier states |
7062 | * | |
7063 | * all states stored in state_list are known to be valid, since | |
7064 | * verifier reached 'bpf_exit' instruction through them | |
7065 | * | |
7066 | * this function is called when verifier exploring different branches of | |
7067 | * execution popped from the state stack. If it sees an old state that has | |
7068 | * more strict register state and more strict stack state then this execution | |
7069 | * branch doesn't need to be explored further, since verifier already | |
7070 | * concluded that more strict state leads to valid finish. | |
7071 | * | |
7072 | * Therefore two states are equivalent if register state is more conservative | |
7073 | * and explored stack state is more conservative than the current one. | |
7074 | * Example: | |
7075 | * explored current | |
7076 | * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC) | |
7077 | * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC) | |
7078 | * | |
7079 | * In other words if current stack state (one being explored) has more | |
7080 | * valid slots than old one that already passed validation, it means | |
7081 | * the verifier can stop exploring and conclude that current state is valid too | |
7082 | * | |
7083 | * Similarly with registers. If explored state has register type as invalid | |
7084 | * whereas register type in current state is meaningful, it means that | |
7085 | * the current state will reach 'bpf_exit' instruction safely | |
7086 | */ | |
f4d7e40a AS |
7087 | static bool func_states_equal(struct bpf_func_state *old, |
7088 | struct bpf_func_state *cur) | |
f1bca824 | 7089 | { |
f1174f77 EC |
7090 | struct idpair *idmap; |
7091 | bool ret = false; | |
f1bca824 AS |
7092 | int i; |
7093 | ||
f1174f77 EC |
7094 | idmap = kcalloc(ID_MAP_SIZE, sizeof(struct idpair), GFP_KERNEL); |
7095 | /* If we failed to allocate the idmap, just say it's not safe */ | |
7096 | if (!idmap) | |
1a0dc1ac | 7097 | return false; |
f1174f77 EC |
7098 | |
7099 | for (i = 0; i < MAX_BPF_REG; i++) { | |
1b688a19 | 7100 | if (!regsafe(&old->regs[i], &cur->regs[i], idmap)) |
f1174f77 | 7101 | goto out_free; |
f1bca824 AS |
7102 | } |
7103 | ||
638f5b90 AS |
7104 | if (!stacksafe(old, cur, idmap)) |
7105 | goto out_free; | |
fd978bf7 JS |
7106 | |
7107 | if (!refsafe(old, cur)) | |
7108 | goto out_free; | |
f1174f77 EC |
7109 | ret = true; |
7110 | out_free: | |
7111 | kfree(idmap); | |
7112 | return ret; | |
f1bca824 AS |
7113 | } |
7114 | ||
f4d7e40a AS |
7115 | static bool states_equal(struct bpf_verifier_env *env, |
7116 | struct bpf_verifier_state *old, | |
7117 | struct bpf_verifier_state *cur) | |
7118 | { | |
7119 | int i; | |
7120 | ||
7121 | if (old->curframe != cur->curframe) | |
7122 | return false; | |
7123 | ||
979d63d5 DB |
7124 | /* Verification state from speculative execution simulation |
7125 | * must never prune a non-speculative execution one. | |
7126 | */ | |
7127 | if (old->speculative && !cur->speculative) | |
7128 | return false; | |
7129 | ||
d83525ca AS |
7130 | if (old->active_spin_lock != cur->active_spin_lock) |
7131 | return false; | |
7132 | ||
f4d7e40a AS |
7133 | /* for states to be equal callsites have to be the same |
7134 | * and all frame states need to be equivalent | |
7135 | */ | |
7136 | for (i = 0; i <= old->curframe; i++) { | |
7137 | if (old->frame[i]->callsite != cur->frame[i]->callsite) | |
7138 | return false; | |
7139 | if (!func_states_equal(old->frame[i], cur->frame[i])) | |
7140 | return false; | |
7141 | } | |
7142 | return true; | |
7143 | } | |
7144 | ||
5327ed3d JW |
7145 | /* Return 0 if no propagation happened. Return negative error code if error |
7146 | * happened. Otherwise, return the propagated bit. | |
7147 | */ | |
55e7f3b5 JW |
7148 | static int propagate_liveness_reg(struct bpf_verifier_env *env, |
7149 | struct bpf_reg_state *reg, | |
7150 | struct bpf_reg_state *parent_reg) | |
7151 | { | |
5327ed3d JW |
7152 | u8 parent_flag = parent_reg->live & REG_LIVE_READ; |
7153 | u8 flag = reg->live & REG_LIVE_READ; | |
55e7f3b5 JW |
7154 | int err; |
7155 | ||
5327ed3d JW |
7156 | /* When comes here, read flags of PARENT_REG or REG could be any of |
7157 | * REG_LIVE_READ64, REG_LIVE_READ32, REG_LIVE_NONE. There is no need | |
7158 | * of propagation if PARENT_REG has strongest REG_LIVE_READ64. | |
7159 | */ | |
7160 | if (parent_flag == REG_LIVE_READ64 || | |
7161 | /* Or if there is no read flag from REG. */ | |
7162 | !flag || | |
7163 | /* Or if the read flag from REG is the same as PARENT_REG. */ | |
7164 | parent_flag == flag) | |
55e7f3b5 JW |
7165 | return 0; |
7166 | ||
5327ed3d | 7167 | err = mark_reg_read(env, reg, parent_reg, flag); |
55e7f3b5 JW |
7168 | if (err) |
7169 | return err; | |
7170 | ||
5327ed3d | 7171 | return flag; |
55e7f3b5 JW |
7172 | } |
7173 | ||
8e9cd9ce | 7174 | /* A write screens off any subsequent reads; but write marks come from the |
f4d7e40a AS |
7175 | * straight-line code between a state and its parent. When we arrive at an |
7176 | * equivalent state (jump target or such) we didn't arrive by the straight-line | |
7177 | * code, so read marks in the state must propagate to the parent regardless | |
7178 | * of the state's write marks. That's what 'parent == state->parent' comparison | |
679c782d | 7179 | * in mark_reg_read() is for. |
8e9cd9ce | 7180 | */ |
f4d7e40a AS |
7181 | static int propagate_liveness(struct bpf_verifier_env *env, |
7182 | const struct bpf_verifier_state *vstate, | |
7183 | struct bpf_verifier_state *vparent) | |
dc503a8a | 7184 | { |
3f8cafa4 | 7185 | struct bpf_reg_state *state_reg, *parent_reg; |
f4d7e40a | 7186 | struct bpf_func_state *state, *parent; |
3f8cafa4 | 7187 | int i, frame, err = 0; |
dc503a8a | 7188 | |
f4d7e40a AS |
7189 | if (vparent->curframe != vstate->curframe) { |
7190 | WARN(1, "propagate_live: parent frame %d current frame %d\n", | |
7191 | vparent->curframe, vstate->curframe); | |
7192 | return -EFAULT; | |
7193 | } | |
dc503a8a EC |
7194 | /* Propagate read liveness of registers... */ |
7195 | BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG); | |
83d16312 | 7196 | for (frame = 0; frame <= vstate->curframe; frame++) { |
3f8cafa4 JW |
7197 | parent = vparent->frame[frame]; |
7198 | state = vstate->frame[frame]; | |
7199 | parent_reg = parent->regs; | |
7200 | state_reg = state->regs; | |
83d16312 JK |
7201 | /* We don't need to worry about FP liveness, it's read-only */ |
7202 | for (i = frame < vstate->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) { | |
55e7f3b5 JW |
7203 | err = propagate_liveness_reg(env, &state_reg[i], |
7204 | &parent_reg[i]); | |
5327ed3d | 7205 | if (err < 0) |
3f8cafa4 | 7206 | return err; |
5327ed3d JW |
7207 | if (err == REG_LIVE_READ64) |
7208 | mark_insn_zext(env, &parent_reg[i]); | |
dc503a8a | 7209 | } |
f4d7e40a | 7210 | |
1b04aee7 | 7211 | /* Propagate stack slots. */ |
f4d7e40a AS |
7212 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE && |
7213 | i < parent->allocated_stack / BPF_REG_SIZE; i++) { | |
3f8cafa4 JW |
7214 | parent_reg = &parent->stack[i].spilled_ptr; |
7215 | state_reg = &state->stack[i].spilled_ptr; | |
55e7f3b5 JW |
7216 | err = propagate_liveness_reg(env, state_reg, |
7217 | parent_reg); | |
5327ed3d | 7218 | if (err < 0) |
3f8cafa4 | 7219 | return err; |
dc503a8a EC |
7220 | } |
7221 | } | |
5327ed3d | 7222 | return 0; |
dc503a8a EC |
7223 | } |
7224 | ||
a3ce685d AS |
7225 | /* find precise scalars in the previous equivalent state and |
7226 | * propagate them into the current state | |
7227 | */ | |
7228 | static int propagate_precision(struct bpf_verifier_env *env, | |
7229 | const struct bpf_verifier_state *old) | |
7230 | { | |
7231 | struct bpf_reg_state *state_reg; | |
7232 | struct bpf_func_state *state; | |
7233 | int i, err = 0; | |
7234 | ||
7235 | state = old->frame[old->curframe]; | |
7236 | state_reg = state->regs; | |
7237 | for (i = 0; i < BPF_REG_FP; i++, state_reg++) { | |
7238 | if (state_reg->type != SCALAR_VALUE || | |
7239 | !state_reg->precise) | |
7240 | continue; | |
7241 | if (env->log.level & BPF_LOG_LEVEL2) | |
7242 | verbose(env, "propagating r%d\n", i); | |
7243 | err = mark_chain_precision(env, i); | |
7244 | if (err < 0) | |
7245 | return err; | |
7246 | } | |
7247 | ||
7248 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { | |
7249 | if (state->stack[i].slot_type[0] != STACK_SPILL) | |
7250 | continue; | |
7251 | state_reg = &state->stack[i].spilled_ptr; | |
7252 | if (state_reg->type != SCALAR_VALUE || | |
7253 | !state_reg->precise) | |
7254 | continue; | |
7255 | if (env->log.level & BPF_LOG_LEVEL2) | |
7256 | verbose(env, "propagating fp%d\n", | |
7257 | (-i - 1) * BPF_REG_SIZE); | |
7258 | err = mark_chain_precision_stack(env, i); | |
7259 | if (err < 0) | |
7260 | return err; | |
7261 | } | |
7262 | return 0; | |
7263 | } | |
7264 | ||
2589726d AS |
7265 | static bool states_maybe_looping(struct bpf_verifier_state *old, |
7266 | struct bpf_verifier_state *cur) | |
7267 | { | |
7268 | struct bpf_func_state *fold, *fcur; | |
7269 | int i, fr = cur->curframe; | |
7270 | ||
7271 | if (old->curframe != fr) | |
7272 | return false; | |
7273 | ||
7274 | fold = old->frame[fr]; | |
7275 | fcur = cur->frame[fr]; | |
7276 | for (i = 0; i < MAX_BPF_REG; i++) | |
7277 | if (memcmp(&fold->regs[i], &fcur->regs[i], | |
7278 | offsetof(struct bpf_reg_state, parent))) | |
7279 | return false; | |
7280 | return true; | |
7281 | } | |
7282 | ||
7283 | ||
58e2af8b | 7284 | static int is_state_visited(struct bpf_verifier_env *env, int insn_idx) |
f1bca824 | 7285 | { |
58e2af8b | 7286 | struct bpf_verifier_state_list *new_sl; |
9f4686c4 | 7287 | struct bpf_verifier_state_list *sl, **pprev; |
679c782d | 7288 | struct bpf_verifier_state *cur = env->cur_state, *new; |
ceefbc96 | 7289 | int i, j, err, states_cnt = 0; |
10d274e8 | 7290 | bool add_new_state = env->test_state_freq ? true : false; |
f1bca824 | 7291 | |
b5dc0163 | 7292 | cur->last_insn_idx = env->prev_insn_idx; |
a8f500af | 7293 | if (!env->insn_aux_data[insn_idx].prune_point) |
f1bca824 AS |
7294 | /* this 'insn_idx' instruction wasn't marked, so we will not |
7295 | * be doing state search here | |
7296 | */ | |
7297 | return 0; | |
7298 | ||
2589726d AS |
7299 | /* bpf progs typically have pruning point every 4 instructions |
7300 | * http://vger.kernel.org/bpfconf2019.html#session-1 | |
7301 | * Do not add new state for future pruning if the verifier hasn't seen | |
7302 | * at least 2 jumps and at least 8 instructions. | |
7303 | * This heuristics helps decrease 'total_states' and 'peak_states' metric. | |
7304 | * In tests that amounts to up to 50% reduction into total verifier | |
7305 | * memory consumption and 20% verifier time speedup. | |
7306 | */ | |
7307 | if (env->jmps_processed - env->prev_jmps_processed >= 2 && | |
7308 | env->insn_processed - env->prev_insn_processed >= 8) | |
7309 | add_new_state = true; | |
7310 | ||
a8f500af AS |
7311 | pprev = explored_state(env, insn_idx); |
7312 | sl = *pprev; | |
7313 | ||
9242b5f5 AS |
7314 | clean_live_states(env, insn_idx, cur); |
7315 | ||
a8f500af | 7316 | while (sl) { |
dc2a4ebc AS |
7317 | states_cnt++; |
7318 | if (sl->state.insn_idx != insn_idx) | |
7319 | goto next; | |
2589726d AS |
7320 | if (sl->state.branches) { |
7321 | if (states_maybe_looping(&sl->state, cur) && | |
7322 | states_equal(env, &sl->state, cur)) { | |
7323 | verbose_linfo(env, insn_idx, "; "); | |
7324 | verbose(env, "infinite loop detected at insn %d\n", insn_idx); | |
7325 | return -EINVAL; | |
7326 | } | |
7327 | /* if the verifier is processing a loop, avoid adding new state | |
7328 | * too often, since different loop iterations have distinct | |
7329 | * states and may not help future pruning. | |
7330 | * This threshold shouldn't be too low to make sure that | |
7331 | * a loop with large bound will be rejected quickly. | |
7332 | * The most abusive loop will be: | |
7333 | * r1 += 1 | |
7334 | * if r1 < 1000000 goto pc-2 | |
7335 | * 1M insn_procssed limit / 100 == 10k peak states. | |
7336 | * This threshold shouldn't be too high either, since states | |
7337 | * at the end of the loop are likely to be useful in pruning. | |
7338 | */ | |
7339 | if (env->jmps_processed - env->prev_jmps_processed < 20 && | |
7340 | env->insn_processed - env->prev_insn_processed < 100) | |
7341 | add_new_state = false; | |
7342 | goto miss; | |
7343 | } | |
638f5b90 | 7344 | if (states_equal(env, &sl->state, cur)) { |
9f4686c4 | 7345 | sl->hit_cnt++; |
f1bca824 | 7346 | /* reached equivalent register/stack state, |
dc503a8a EC |
7347 | * prune the search. |
7348 | * Registers read by the continuation are read by us. | |
8e9cd9ce EC |
7349 | * If we have any write marks in env->cur_state, they |
7350 | * will prevent corresponding reads in the continuation | |
7351 | * from reaching our parent (an explored_state). Our | |
7352 | * own state will get the read marks recorded, but | |
7353 | * they'll be immediately forgotten as we're pruning | |
7354 | * this state and will pop a new one. | |
f1bca824 | 7355 | */ |
f4d7e40a | 7356 | err = propagate_liveness(env, &sl->state, cur); |
a3ce685d AS |
7357 | |
7358 | /* if previous state reached the exit with precision and | |
7359 | * current state is equivalent to it (except precsion marks) | |
7360 | * the precision needs to be propagated back in | |
7361 | * the current state. | |
7362 | */ | |
7363 | err = err ? : push_jmp_history(env, cur); | |
7364 | err = err ? : propagate_precision(env, &sl->state); | |
f4d7e40a AS |
7365 | if (err) |
7366 | return err; | |
f1bca824 | 7367 | return 1; |
dc503a8a | 7368 | } |
2589726d AS |
7369 | miss: |
7370 | /* when new state is not going to be added do not increase miss count. | |
7371 | * Otherwise several loop iterations will remove the state | |
7372 | * recorded earlier. The goal of these heuristics is to have | |
7373 | * states from some iterations of the loop (some in the beginning | |
7374 | * and some at the end) to help pruning. | |
7375 | */ | |
7376 | if (add_new_state) | |
7377 | sl->miss_cnt++; | |
9f4686c4 AS |
7378 | /* heuristic to determine whether this state is beneficial |
7379 | * to keep checking from state equivalence point of view. | |
7380 | * Higher numbers increase max_states_per_insn and verification time, | |
7381 | * but do not meaningfully decrease insn_processed. | |
7382 | */ | |
7383 | if (sl->miss_cnt > sl->hit_cnt * 3 + 3) { | |
7384 | /* the state is unlikely to be useful. Remove it to | |
7385 | * speed up verification | |
7386 | */ | |
7387 | *pprev = sl->next; | |
7388 | if (sl->state.frame[0]->regs[0].live & REG_LIVE_DONE) { | |
2589726d AS |
7389 | u32 br = sl->state.branches; |
7390 | ||
7391 | WARN_ONCE(br, | |
7392 | "BUG live_done but branches_to_explore %d\n", | |
7393 | br); | |
9f4686c4 AS |
7394 | free_verifier_state(&sl->state, false); |
7395 | kfree(sl); | |
7396 | env->peak_states--; | |
7397 | } else { | |
7398 | /* cannot free this state, since parentage chain may | |
7399 | * walk it later. Add it for free_list instead to | |
7400 | * be freed at the end of verification | |
7401 | */ | |
7402 | sl->next = env->free_list; | |
7403 | env->free_list = sl; | |
7404 | } | |
7405 | sl = *pprev; | |
7406 | continue; | |
7407 | } | |
dc2a4ebc | 7408 | next: |
9f4686c4 AS |
7409 | pprev = &sl->next; |
7410 | sl = *pprev; | |
f1bca824 AS |
7411 | } |
7412 | ||
06ee7115 AS |
7413 | if (env->max_states_per_insn < states_cnt) |
7414 | env->max_states_per_insn = states_cnt; | |
7415 | ||
ceefbc96 | 7416 | if (!env->allow_ptr_leaks && states_cnt > BPF_COMPLEXITY_LIMIT_STATES) |
b5dc0163 | 7417 | return push_jmp_history(env, cur); |
ceefbc96 | 7418 | |
2589726d | 7419 | if (!add_new_state) |
b5dc0163 | 7420 | return push_jmp_history(env, cur); |
ceefbc96 | 7421 | |
2589726d AS |
7422 | /* There were no equivalent states, remember the current one. |
7423 | * Technically the current state is not proven to be safe yet, | |
f4d7e40a | 7424 | * but it will either reach outer most bpf_exit (which means it's safe) |
2589726d | 7425 | * or it will be rejected. When there are no loops the verifier won't be |
f4d7e40a | 7426 | * seeing this tuple (frame[0].callsite, frame[1].callsite, .. insn_idx) |
2589726d AS |
7427 | * again on the way to bpf_exit. |
7428 | * When looping the sl->state.branches will be > 0 and this state | |
7429 | * will not be considered for equivalence until branches == 0. | |
f1bca824 | 7430 | */ |
638f5b90 | 7431 | new_sl = kzalloc(sizeof(struct bpf_verifier_state_list), GFP_KERNEL); |
f1bca824 AS |
7432 | if (!new_sl) |
7433 | return -ENOMEM; | |
06ee7115 AS |
7434 | env->total_states++; |
7435 | env->peak_states++; | |
2589726d AS |
7436 | env->prev_jmps_processed = env->jmps_processed; |
7437 | env->prev_insn_processed = env->insn_processed; | |
f1bca824 AS |
7438 | |
7439 | /* add new state to the head of linked list */ | |
679c782d EC |
7440 | new = &new_sl->state; |
7441 | err = copy_verifier_state(new, cur); | |
1969db47 | 7442 | if (err) { |
679c782d | 7443 | free_verifier_state(new, false); |
1969db47 AS |
7444 | kfree(new_sl); |
7445 | return err; | |
7446 | } | |
dc2a4ebc | 7447 | new->insn_idx = insn_idx; |
2589726d AS |
7448 | WARN_ONCE(new->branches != 1, |
7449 | "BUG is_state_visited:branches_to_explore=%d insn %d\n", new->branches, insn_idx); | |
b5dc0163 | 7450 | |
2589726d | 7451 | cur->parent = new; |
b5dc0163 AS |
7452 | cur->first_insn_idx = insn_idx; |
7453 | clear_jmp_history(cur); | |
5d839021 AS |
7454 | new_sl->next = *explored_state(env, insn_idx); |
7455 | *explored_state(env, insn_idx) = new_sl; | |
7640ead9 JK |
7456 | /* connect new state to parentage chain. Current frame needs all |
7457 | * registers connected. Only r6 - r9 of the callers are alive (pushed | |
7458 | * to the stack implicitly by JITs) so in callers' frames connect just | |
7459 | * r6 - r9 as an optimization. Callers will have r1 - r5 connected to | |
7460 | * the state of the call instruction (with WRITTEN set), and r0 comes | |
7461 | * from callee with its full parentage chain, anyway. | |
7462 | */ | |
8e9cd9ce EC |
7463 | /* clear write marks in current state: the writes we did are not writes |
7464 | * our child did, so they don't screen off its reads from us. | |
7465 | * (There are no read marks in current state, because reads always mark | |
7466 | * their parent and current state never has children yet. Only | |
7467 | * explored_states can get read marks.) | |
7468 | */ | |
eea1c227 AS |
7469 | for (j = 0; j <= cur->curframe; j++) { |
7470 | for (i = j < cur->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) | |
7471 | cur->frame[j]->regs[i].parent = &new->frame[j]->regs[i]; | |
7472 | for (i = 0; i < BPF_REG_FP; i++) | |
7473 | cur->frame[j]->regs[i].live = REG_LIVE_NONE; | |
7474 | } | |
f4d7e40a AS |
7475 | |
7476 | /* all stack frames are accessible from callee, clear them all */ | |
7477 | for (j = 0; j <= cur->curframe; j++) { | |
7478 | struct bpf_func_state *frame = cur->frame[j]; | |
679c782d | 7479 | struct bpf_func_state *newframe = new->frame[j]; |
f4d7e40a | 7480 | |
679c782d | 7481 | for (i = 0; i < frame->allocated_stack / BPF_REG_SIZE; i++) { |
cc2b14d5 | 7482 | frame->stack[i].spilled_ptr.live = REG_LIVE_NONE; |
679c782d EC |
7483 | frame->stack[i].spilled_ptr.parent = |
7484 | &newframe->stack[i].spilled_ptr; | |
7485 | } | |
f4d7e40a | 7486 | } |
f1bca824 AS |
7487 | return 0; |
7488 | } | |
7489 | ||
c64b7983 JS |
7490 | /* Return true if it's OK to have the same insn return a different type. */ |
7491 | static bool reg_type_mismatch_ok(enum bpf_reg_type type) | |
7492 | { | |
7493 | switch (type) { | |
7494 | case PTR_TO_CTX: | |
7495 | case PTR_TO_SOCKET: | |
7496 | case PTR_TO_SOCKET_OR_NULL: | |
46f8bc92 MKL |
7497 | case PTR_TO_SOCK_COMMON: |
7498 | case PTR_TO_SOCK_COMMON_OR_NULL: | |
655a51e5 MKL |
7499 | case PTR_TO_TCP_SOCK: |
7500 | case PTR_TO_TCP_SOCK_OR_NULL: | |
fada7fdc | 7501 | case PTR_TO_XDP_SOCK: |
c64b7983 JS |
7502 | return false; |
7503 | default: | |
7504 | return true; | |
7505 | } | |
7506 | } | |
7507 | ||
7508 | /* If an instruction was previously used with particular pointer types, then we | |
7509 | * need to be careful to avoid cases such as the below, where it may be ok | |
7510 | * for one branch accessing the pointer, but not ok for the other branch: | |
7511 | * | |
7512 | * R1 = sock_ptr | |
7513 | * goto X; | |
7514 | * ... | |
7515 | * R1 = some_other_valid_ptr; | |
7516 | * goto X; | |
7517 | * ... | |
7518 | * R2 = *(u32 *)(R1 + 0); | |
7519 | */ | |
7520 | static bool reg_type_mismatch(enum bpf_reg_type src, enum bpf_reg_type prev) | |
7521 | { | |
7522 | return src != prev && (!reg_type_mismatch_ok(src) || | |
7523 | !reg_type_mismatch_ok(prev)); | |
7524 | } | |
7525 | ||
58e2af8b | 7526 | static int do_check(struct bpf_verifier_env *env) |
17a52670 | 7527 | { |
638f5b90 | 7528 | struct bpf_verifier_state *state; |
17a52670 | 7529 | struct bpf_insn *insns = env->prog->insnsi; |
638f5b90 | 7530 | struct bpf_reg_state *regs; |
06ee7115 | 7531 | int insn_cnt = env->prog->len; |
17a52670 | 7532 | bool do_print_state = false; |
b5dc0163 | 7533 | int prev_insn_idx = -1; |
17a52670 | 7534 | |
d9762e84 MKL |
7535 | env->prev_linfo = NULL; |
7536 | ||
638f5b90 AS |
7537 | state = kzalloc(sizeof(struct bpf_verifier_state), GFP_KERNEL); |
7538 | if (!state) | |
7539 | return -ENOMEM; | |
f4d7e40a | 7540 | state->curframe = 0; |
979d63d5 | 7541 | state->speculative = false; |
2589726d | 7542 | state->branches = 1; |
f4d7e40a AS |
7543 | state->frame[0] = kzalloc(sizeof(struct bpf_func_state), GFP_KERNEL); |
7544 | if (!state->frame[0]) { | |
7545 | kfree(state); | |
7546 | return -ENOMEM; | |
7547 | } | |
7548 | env->cur_state = state; | |
7549 | init_func_state(env, state->frame[0], | |
7550 | BPF_MAIN_FUNC /* callsite */, | |
7551 | 0 /* frameno */, | |
7552 | 0 /* subprogno, zero == main subprog */); | |
c08435ec | 7553 | |
17a52670 AS |
7554 | for (;;) { |
7555 | struct bpf_insn *insn; | |
7556 | u8 class; | |
7557 | int err; | |
7558 | ||
b5dc0163 | 7559 | env->prev_insn_idx = prev_insn_idx; |
c08435ec | 7560 | if (env->insn_idx >= insn_cnt) { |
61bd5218 | 7561 | verbose(env, "invalid insn idx %d insn_cnt %d\n", |
c08435ec | 7562 | env->insn_idx, insn_cnt); |
17a52670 AS |
7563 | return -EFAULT; |
7564 | } | |
7565 | ||
c08435ec | 7566 | insn = &insns[env->insn_idx]; |
17a52670 AS |
7567 | class = BPF_CLASS(insn->code); |
7568 | ||
06ee7115 | 7569 | if (++env->insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) { |
61bd5218 JK |
7570 | verbose(env, |
7571 | "BPF program is too large. Processed %d insn\n", | |
06ee7115 | 7572 | env->insn_processed); |
17a52670 AS |
7573 | return -E2BIG; |
7574 | } | |
7575 | ||
c08435ec | 7576 | err = is_state_visited(env, env->insn_idx); |
f1bca824 AS |
7577 | if (err < 0) |
7578 | return err; | |
7579 | if (err == 1) { | |
7580 | /* found equivalent state, can prune the search */ | |
06ee7115 | 7581 | if (env->log.level & BPF_LOG_LEVEL) { |
f1bca824 | 7582 | if (do_print_state) |
979d63d5 DB |
7583 | verbose(env, "\nfrom %d to %d%s: safe\n", |
7584 | env->prev_insn_idx, env->insn_idx, | |
7585 | env->cur_state->speculative ? | |
7586 | " (speculative execution)" : ""); | |
f1bca824 | 7587 | else |
c08435ec | 7588 | verbose(env, "%d: safe\n", env->insn_idx); |
f1bca824 AS |
7589 | } |
7590 | goto process_bpf_exit; | |
7591 | } | |
7592 | ||
c3494801 AS |
7593 | if (signal_pending(current)) |
7594 | return -EAGAIN; | |
7595 | ||
3c2ce60b DB |
7596 | if (need_resched()) |
7597 | cond_resched(); | |
7598 | ||
06ee7115 AS |
7599 | if (env->log.level & BPF_LOG_LEVEL2 || |
7600 | (env->log.level & BPF_LOG_LEVEL && do_print_state)) { | |
7601 | if (env->log.level & BPF_LOG_LEVEL2) | |
c08435ec | 7602 | verbose(env, "%d:", env->insn_idx); |
c5fc9692 | 7603 | else |
979d63d5 DB |
7604 | verbose(env, "\nfrom %d to %d%s:", |
7605 | env->prev_insn_idx, env->insn_idx, | |
7606 | env->cur_state->speculative ? | |
7607 | " (speculative execution)" : ""); | |
f4d7e40a | 7608 | print_verifier_state(env, state->frame[state->curframe]); |
17a52670 AS |
7609 | do_print_state = false; |
7610 | } | |
7611 | ||
06ee7115 | 7612 | if (env->log.level & BPF_LOG_LEVEL) { |
7105e828 DB |
7613 | const struct bpf_insn_cbs cbs = { |
7614 | .cb_print = verbose, | |
abe08840 | 7615 | .private_data = env, |
7105e828 DB |
7616 | }; |
7617 | ||
c08435ec DB |
7618 | verbose_linfo(env, env->insn_idx, "; "); |
7619 | verbose(env, "%d: ", env->insn_idx); | |
abe08840 | 7620 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); |
17a52670 AS |
7621 | } |
7622 | ||
cae1927c | 7623 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
c08435ec DB |
7624 | err = bpf_prog_offload_verify_insn(env, env->insn_idx, |
7625 | env->prev_insn_idx); | |
cae1927c JK |
7626 | if (err) |
7627 | return err; | |
7628 | } | |
13a27dfc | 7629 | |
638f5b90 | 7630 | regs = cur_regs(env); |
c08435ec | 7631 | env->insn_aux_data[env->insn_idx].seen = true; |
b5dc0163 | 7632 | prev_insn_idx = env->insn_idx; |
fd978bf7 | 7633 | |
17a52670 | 7634 | if (class == BPF_ALU || class == BPF_ALU64) { |
1be7f75d | 7635 | err = check_alu_op(env, insn); |
17a52670 AS |
7636 | if (err) |
7637 | return err; | |
7638 | ||
7639 | } else if (class == BPF_LDX) { | |
3df126f3 | 7640 | enum bpf_reg_type *prev_src_type, src_reg_type; |
9bac3d6d AS |
7641 | |
7642 | /* check for reserved fields is already done */ | |
7643 | ||
17a52670 | 7644 | /* check src operand */ |
dc503a8a | 7645 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
7646 | if (err) |
7647 | return err; | |
7648 | ||
dc503a8a | 7649 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
17a52670 AS |
7650 | if (err) |
7651 | return err; | |
7652 | ||
725f9dcd AS |
7653 | src_reg_type = regs[insn->src_reg].type; |
7654 | ||
17a52670 AS |
7655 | /* check that memory (src_reg + off) is readable, |
7656 | * the state of dst_reg will be updated by this func | |
7657 | */ | |
c08435ec DB |
7658 | err = check_mem_access(env, env->insn_idx, insn->src_reg, |
7659 | insn->off, BPF_SIZE(insn->code), | |
7660 | BPF_READ, insn->dst_reg, false); | |
17a52670 AS |
7661 | if (err) |
7662 | return err; | |
7663 | ||
c08435ec | 7664 | prev_src_type = &env->insn_aux_data[env->insn_idx].ptr_type; |
3df126f3 JK |
7665 | |
7666 | if (*prev_src_type == NOT_INIT) { | |
9bac3d6d AS |
7667 | /* saw a valid insn |
7668 | * dst_reg = *(u32 *)(src_reg + off) | |
3df126f3 | 7669 | * save type to validate intersecting paths |
9bac3d6d | 7670 | */ |
3df126f3 | 7671 | *prev_src_type = src_reg_type; |
9bac3d6d | 7672 | |
c64b7983 | 7673 | } else if (reg_type_mismatch(src_reg_type, *prev_src_type)) { |
9bac3d6d AS |
7674 | /* ABuser program is trying to use the same insn |
7675 | * dst_reg = *(u32*) (src_reg + off) | |
7676 | * with different pointer types: | |
7677 | * src_reg == ctx in one branch and | |
7678 | * src_reg == stack|map in some other branch. | |
7679 | * Reject it. | |
7680 | */ | |
61bd5218 | 7681 | verbose(env, "same insn cannot be used with different pointers\n"); |
9bac3d6d AS |
7682 | return -EINVAL; |
7683 | } | |
7684 | ||
17a52670 | 7685 | } else if (class == BPF_STX) { |
3df126f3 | 7686 | enum bpf_reg_type *prev_dst_type, dst_reg_type; |
d691f9e8 | 7687 | |
17a52670 | 7688 | if (BPF_MODE(insn->code) == BPF_XADD) { |
c08435ec | 7689 | err = check_xadd(env, env->insn_idx, insn); |
17a52670 AS |
7690 | if (err) |
7691 | return err; | |
c08435ec | 7692 | env->insn_idx++; |
17a52670 AS |
7693 | continue; |
7694 | } | |
7695 | ||
17a52670 | 7696 | /* check src1 operand */ |
dc503a8a | 7697 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
7698 | if (err) |
7699 | return err; | |
7700 | /* check src2 operand */ | |
dc503a8a | 7701 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
7702 | if (err) |
7703 | return err; | |
7704 | ||
d691f9e8 AS |
7705 | dst_reg_type = regs[insn->dst_reg].type; |
7706 | ||
17a52670 | 7707 | /* check that memory (dst_reg + off) is writeable */ |
c08435ec DB |
7708 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
7709 | insn->off, BPF_SIZE(insn->code), | |
7710 | BPF_WRITE, insn->src_reg, false); | |
17a52670 AS |
7711 | if (err) |
7712 | return err; | |
7713 | ||
c08435ec | 7714 | prev_dst_type = &env->insn_aux_data[env->insn_idx].ptr_type; |
3df126f3 JK |
7715 | |
7716 | if (*prev_dst_type == NOT_INIT) { | |
7717 | *prev_dst_type = dst_reg_type; | |
c64b7983 | 7718 | } else if (reg_type_mismatch(dst_reg_type, *prev_dst_type)) { |
61bd5218 | 7719 | verbose(env, "same insn cannot be used with different pointers\n"); |
d691f9e8 AS |
7720 | return -EINVAL; |
7721 | } | |
7722 | ||
17a52670 AS |
7723 | } else if (class == BPF_ST) { |
7724 | if (BPF_MODE(insn->code) != BPF_MEM || | |
7725 | insn->src_reg != BPF_REG_0) { | |
61bd5218 | 7726 | verbose(env, "BPF_ST uses reserved fields\n"); |
17a52670 AS |
7727 | return -EINVAL; |
7728 | } | |
7729 | /* check src operand */ | |
dc503a8a | 7730 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
7731 | if (err) |
7732 | return err; | |
7733 | ||
f37a8cb8 | 7734 | if (is_ctx_reg(env, insn->dst_reg)) { |
9d2be44a | 7735 | verbose(env, "BPF_ST stores into R%d %s is not allowed\n", |
2a159c6f DB |
7736 | insn->dst_reg, |
7737 | reg_type_str[reg_state(env, insn->dst_reg)->type]); | |
f37a8cb8 DB |
7738 | return -EACCES; |
7739 | } | |
7740 | ||
17a52670 | 7741 | /* check that memory (dst_reg + off) is writeable */ |
c08435ec DB |
7742 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
7743 | insn->off, BPF_SIZE(insn->code), | |
7744 | BPF_WRITE, -1, false); | |
17a52670 AS |
7745 | if (err) |
7746 | return err; | |
7747 | ||
092ed096 | 7748 | } else if (class == BPF_JMP || class == BPF_JMP32) { |
17a52670 AS |
7749 | u8 opcode = BPF_OP(insn->code); |
7750 | ||
2589726d | 7751 | env->jmps_processed++; |
17a52670 AS |
7752 | if (opcode == BPF_CALL) { |
7753 | if (BPF_SRC(insn->code) != BPF_K || | |
7754 | insn->off != 0 || | |
f4d7e40a AS |
7755 | (insn->src_reg != BPF_REG_0 && |
7756 | insn->src_reg != BPF_PSEUDO_CALL) || | |
092ed096 JW |
7757 | insn->dst_reg != BPF_REG_0 || |
7758 | class == BPF_JMP32) { | |
61bd5218 | 7759 | verbose(env, "BPF_CALL uses reserved fields\n"); |
17a52670 AS |
7760 | return -EINVAL; |
7761 | } | |
7762 | ||
d83525ca AS |
7763 | if (env->cur_state->active_spin_lock && |
7764 | (insn->src_reg == BPF_PSEUDO_CALL || | |
7765 | insn->imm != BPF_FUNC_spin_unlock)) { | |
7766 | verbose(env, "function calls are not allowed while holding a lock\n"); | |
7767 | return -EINVAL; | |
7768 | } | |
f4d7e40a | 7769 | if (insn->src_reg == BPF_PSEUDO_CALL) |
c08435ec | 7770 | err = check_func_call(env, insn, &env->insn_idx); |
f4d7e40a | 7771 | else |
c08435ec | 7772 | err = check_helper_call(env, insn->imm, env->insn_idx); |
17a52670 AS |
7773 | if (err) |
7774 | return err; | |
7775 | ||
7776 | } else if (opcode == BPF_JA) { | |
7777 | if (BPF_SRC(insn->code) != BPF_K || | |
7778 | insn->imm != 0 || | |
7779 | insn->src_reg != BPF_REG_0 || | |
092ed096 JW |
7780 | insn->dst_reg != BPF_REG_0 || |
7781 | class == BPF_JMP32) { | |
61bd5218 | 7782 | verbose(env, "BPF_JA uses reserved fields\n"); |
17a52670 AS |
7783 | return -EINVAL; |
7784 | } | |
7785 | ||
c08435ec | 7786 | env->insn_idx += insn->off + 1; |
17a52670 AS |
7787 | continue; |
7788 | ||
7789 | } else if (opcode == BPF_EXIT) { | |
7790 | if (BPF_SRC(insn->code) != BPF_K || | |
7791 | insn->imm != 0 || | |
7792 | insn->src_reg != BPF_REG_0 || | |
092ed096 JW |
7793 | insn->dst_reg != BPF_REG_0 || |
7794 | class == BPF_JMP32) { | |
61bd5218 | 7795 | verbose(env, "BPF_EXIT uses reserved fields\n"); |
17a52670 AS |
7796 | return -EINVAL; |
7797 | } | |
7798 | ||
d83525ca AS |
7799 | if (env->cur_state->active_spin_lock) { |
7800 | verbose(env, "bpf_spin_unlock is missing\n"); | |
7801 | return -EINVAL; | |
7802 | } | |
7803 | ||
f4d7e40a AS |
7804 | if (state->curframe) { |
7805 | /* exit from nested function */ | |
c08435ec | 7806 | err = prepare_func_exit(env, &env->insn_idx); |
f4d7e40a AS |
7807 | if (err) |
7808 | return err; | |
7809 | do_print_state = true; | |
7810 | continue; | |
7811 | } | |
7812 | ||
fd978bf7 JS |
7813 | err = check_reference_leak(env); |
7814 | if (err) | |
7815 | return err; | |
7816 | ||
17a52670 AS |
7817 | /* eBPF calling convetion is such that R0 is used |
7818 | * to return the value from eBPF program. | |
7819 | * Make sure that it's readable at this time | |
7820 | * of bpf_exit, which means that program wrote | |
7821 | * something into it earlier | |
7822 | */ | |
dc503a8a | 7823 | err = check_reg_arg(env, BPF_REG_0, SRC_OP); |
17a52670 AS |
7824 | if (err) |
7825 | return err; | |
7826 | ||
1be7f75d | 7827 | if (is_pointer_value(env, BPF_REG_0)) { |
61bd5218 | 7828 | verbose(env, "R0 leaks addr as return value\n"); |
1be7f75d AS |
7829 | return -EACCES; |
7830 | } | |
7831 | ||
390ee7e2 AS |
7832 | err = check_return_code(env); |
7833 | if (err) | |
7834 | return err; | |
f1bca824 | 7835 | process_bpf_exit: |
2589726d | 7836 | update_branch_counts(env, env->cur_state); |
b5dc0163 | 7837 | err = pop_stack(env, &prev_insn_idx, |
c08435ec | 7838 | &env->insn_idx); |
638f5b90 AS |
7839 | if (err < 0) { |
7840 | if (err != -ENOENT) | |
7841 | return err; | |
17a52670 AS |
7842 | break; |
7843 | } else { | |
7844 | do_print_state = true; | |
7845 | continue; | |
7846 | } | |
7847 | } else { | |
c08435ec | 7848 | err = check_cond_jmp_op(env, insn, &env->insn_idx); |
17a52670 AS |
7849 | if (err) |
7850 | return err; | |
7851 | } | |
7852 | } else if (class == BPF_LD) { | |
7853 | u8 mode = BPF_MODE(insn->code); | |
7854 | ||
7855 | if (mode == BPF_ABS || mode == BPF_IND) { | |
ddd872bc AS |
7856 | err = check_ld_abs(env, insn); |
7857 | if (err) | |
7858 | return err; | |
7859 | ||
17a52670 AS |
7860 | } else if (mode == BPF_IMM) { |
7861 | err = check_ld_imm(env, insn); | |
7862 | if (err) | |
7863 | return err; | |
7864 | ||
c08435ec DB |
7865 | env->insn_idx++; |
7866 | env->insn_aux_data[env->insn_idx].seen = true; | |
17a52670 | 7867 | } else { |
61bd5218 | 7868 | verbose(env, "invalid BPF_LD mode\n"); |
17a52670 AS |
7869 | return -EINVAL; |
7870 | } | |
7871 | } else { | |
61bd5218 | 7872 | verbose(env, "unknown insn class %d\n", class); |
17a52670 AS |
7873 | return -EINVAL; |
7874 | } | |
7875 | ||
c08435ec | 7876 | env->insn_idx++; |
17a52670 AS |
7877 | } |
7878 | ||
9c8105bd | 7879 | env->prog->aux->stack_depth = env->subprog_info[0].stack_depth; |
17a52670 AS |
7880 | return 0; |
7881 | } | |
7882 | ||
56f668df MKL |
7883 | static int check_map_prealloc(struct bpf_map *map) |
7884 | { | |
7885 | return (map->map_type != BPF_MAP_TYPE_HASH && | |
bcc6b1b7 MKL |
7886 | map->map_type != BPF_MAP_TYPE_PERCPU_HASH && |
7887 | map->map_type != BPF_MAP_TYPE_HASH_OF_MAPS) || | |
56f668df MKL |
7888 | !(map->map_flags & BPF_F_NO_PREALLOC); |
7889 | } | |
7890 | ||
d83525ca AS |
7891 | static bool is_tracing_prog_type(enum bpf_prog_type type) |
7892 | { | |
7893 | switch (type) { | |
7894 | case BPF_PROG_TYPE_KPROBE: | |
7895 | case BPF_PROG_TYPE_TRACEPOINT: | |
7896 | case BPF_PROG_TYPE_PERF_EVENT: | |
7897 | case BPF_PROG_TYPE_RAW_TRACEPOINT: | |
7898 | return true; | |
7899 | default: | |
7900 | return false; | |
7901 | } | |
7902 | } | |
7903 | ||
61bd5218 JK |
7904 | static int check_map_prog_compatibility(struct bpf_verifier_env *env, |
7905 | struct bpf_map *map, | |
fdc15d38 AS |
7906 | struct bpf_prog *prog) |
7907 | ||
7908 | { | |
56f668df MKL |
7909 | /* Make sure that BPF_PROG_TYPE_PERF_EVENT programs only use |
7910 | * preallocated hash maps, since doing memory allocation | |
7911 | * in overflow_handler can crash depending on where nmi got | |
7912 | * triggered. | |
7913 | */ | |
7914 | if (prog->type == BPF_PROG_TYPE_PERF_EVENT) { | |
7915 | if (!check_map_prealloc(map)) { | |
61bd5218 | 7916 | verbose(env, "perf_event programs can only use preallocated hash map\n"); |
56f668df MKL |
7917 | return -EINVAL; |
7918 | } | |
7919 | if (map->inner_map_meta && | |
7920 | !check_map_prealloc(map->inner_map_meta)) { | |
61bd5218 | 7921 | verbose(env, "perf_event programs can only use preallocated inner hash map\n"); |
56f668df MKL |
7922 | return -EINVAL; |
7923 | } | |
fdc15d38 | 7924 | } |
a3884572 | 7925 | |
d83525ca AS |
7926 | if ((is_tracing_prog_type(prog->type) || |
7927 | prog->type == BPF_PROG_TYPE_SOCKET_FILTER) && | |
7928 | map_value_has_spin_lock(map)) { | |
7929 | verbose(env, "tracing progs cannot use bpf_spin_lock yet\n"); | |
7930 | return -EINVAL; | |
7931 | } | |
7932 | ||
a3884572 | 7933 | if ((bpf_prog_is_dev_bound(prog->aux) || bpf_map_is_dev_bound(map)) && |
09728266 | 7934 | !bpf_offload_prog_map_match(prog, map)) { |
a3884572 JK |
7935 | verbose(env, "offload device mismatch between prog and map\n"); |
7936 | return -EINVAL; | |
7937 | } | |
7938 | ||
fdc15d38 AS |
7939 | return 0; |
7940 | } | |
7941 | ||
b741f163 RG |
7942 | static bool bpf_map_is_cgroup_storage(struct bpf_map *map) |
7943 | { | |
7944 | return (map->map_type == BPF_MAP_TYPE_CGROUP_STORAGE || | |
7945 | map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE); | |
7946 | } | |
7947 | ||
0246e64d AS |
7948 | /* look for pseudo eBPF instructions that access map FDs and |
7949 | * replace them with actual map pointers | |
7950 | */ | |
58e2af8b | 7951 | static int replace_map_fd_with_map_ptr(struct bpf_verifier_env *env) |
0246e64d AS |
7952 | { |
7953 | struct bpf_insn *insn = env->prog->insnsi; | |
7954 | int insn_cnt = env->prog->len; | |
fdc15d38 | 7955 | int i, j, err; |
0246e64d | 7956 | |
f1f7714e | 7957 | err = bpf_prog_calc_tag(env->prog); |
aafe6ae9 DB |
7958 | if (err) |
7959 | return err; | |
7960 | ||
0246e64d | 7961 | for (i = 0; i < insn_cnt; i++, insn++) { |
9bac3d6d | 7962 | if (BPF_CLASS(insn->code) == BPF_LDX && |
d691f9e8 | 7963 | (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) { |
61bd5218 | 7964 | verbose(env, "BPF_LDX uses reserved fields\n"); |
9bac3d6d AS |
7965 | return -EINVAL; |
7966 | } | |
7967 | ||
d691f9e8 AS |
7968 | if (BPF_CLASS(insn->code) == BPF_STX && |
7969 | ((BPF_MODE(insn->code) != BPF_MEM && | |
7970 | BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) { | |
61bd5218 | 7971 | verbose(env, "BPF_STX uses reserved fields\n"); |
d691f9e8 AS |
7972 | return -EINVAL; |
7973 | } | |
7974 | ||
0246e64d | 7975 | if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) { |
d8eca5bb | 7976 | struct bpf_insn_aux_data *aux; |
0246e64d AS |
7977 | struct bpf_map *map; |
7978 | struct fd f; | |
d8eca5bb | 7979 | u64 addr; |
0246e64d AS |
7980 | |
7981 | if (i == insn_cnt - 1 || insn[1].code != 0 || | |
7982 | insn[1].dst_reg != 0 || insn[1].src_reg != 0 || | |
7983 | insn[1].off != 0) { | |
61bd5218 | 7984 | verbose(env, "invalid bpf_ld_imm64 insn\n"); |
0246e64d AS |
7985 | return -EINVAL; |
7986 | } | |
7987 | ||
d8eca5bb | 7988 | if (insn[0].src_reg == 0) |
0246e64d AS |
7989 | /* valid generic load 64-bit imm */ |
7990 | goto next_insn; | |
7991 | ||
d8eca5bb DB |
7992 | /* In final convert_pseudo_ld_imm64() step, this is |
7993 | * converted into regular 64-bit imm load insn. | |
7994 | */ | |
7995 | if ((insn[0].src_reg != BPF_PSEUDO_MAP_FD && | |
7996 | insn[0].src_reg != BPF_PSEUDO_MAP_VALUE) || | |
7997 | (insn[0].src_reg == BPF_PSEUDO_MAP_FD && | |
7998 | insn[1].imm != 0)) { | |
7999 | verbose(env, | |
8000 | "unrecognized bpf_ld_imm64 insn\n"); | |
0246e64d AS |
8001 | return -EINVAL; |
8002 | } | |
8003 | ||
20182390 | 8004 | f = fdget(insn[0].imm); |
c2101297 | 8005 | map = __bpf_map_get(f); |
0246e64d | 8006 | if (IS_ERR(map)) { |
61bd5218 | 8007 | verbose(env, "fd %d is not pointing to valid bpf_map\n", |
20182390 | 8008 | insn[0].imm); |
0246e64d AS |
8009 | return PTR_ERR(map); |
8010 | } | |
8011 | ||
61bd5218 | 8012 | err = check_map_prog_compatibility(env, map, env->prog); |
fdc15d38 AS |
8013 | if (err) { |
8014 | fdput(f); | |
8015 | return err; | |
8016 | } | |
8017 | ||
d8eca5bb DB |
8018 | aux = &env->insn_aux_data[i]; |
8019 | if (insn->src_reg == BPF_PSEUDO_MAP_FD) { | |
8020 | addr = (unsigned long)map; | |
8021 | } else { | |
8022 | u32 off = insn[1].imm; | |
8023 | ||
8024 | if (off >= BPF_MAX_VAR_OFF) { | |
8025 | verbose(env, "direct value offset of %u is not allowed\n", off); | |
8026 | fdput(f); | |
8027 | return -EINVAL; | |
8028 | } | |
8029 | ||
8030 | if (!map->ops->map_direct_value_addr) { | |
8031 | verbose(env, "no direct value access support for this map type\n"); | |
8032 | fdput(f); | |
8033 | return -EINVAL; | |
8034 | } | |
8035 | ||
8036 | err = map->ops->map_direct_value_addr(map, &addr, off); | |
8037 | if (err) { | |
8038 | verbose(env, "invalid access to map value pointer, value_size=%u off=%u\n", | |
8039 | map->value_size, off); | |
8040 | fdput(f); | |
8041 | return err; | |
8042 | } | |
8043 | ||
8044 | aux->map_off = off; | |
8045 | addr += off; | |
8046 | } | |
8047 | ||
8048 | insn[0].imm = (u32)addr; | |
8049 | insn[1].imm = addr >> 32; | |
0246e64d AS |
8050 | |
8051 | /* check whether we recorded this map already */ | |
d8eca5bb | 8052 | for (j = 0; j < env->used_map_cnt; j++) { |
0246e64d | 8053 | if (env->used_maps[j] == map) { |
d8eca5bb | 8054 | aux->map_index = j; |
0246e64d AS |
8055 | fdput(f); |
8056 | goto next_insn; | |
8057 | } | |
d8eca5bb | 8058 | } |
0246e64d AS |
8059 | |
8060 | if (env->used_map_cnt >= MAX_USED_MAPS) { | |
8061 | fdput(f); | |
8062 | return -E2BIG; | |
8063 | } | |
8064 | ||
0246e64d AS |
8065 | /* hold the map. If the program is rejected by verifier, |
8066 | * the map will be released by release_maps() or it | |
8067 | * will be used by the valid program until it's unloaded | |
ab7f5bf0 | 8068 | * and all maps are released in free_used_maps() |
0246e64d | 8069 | */ |
92117d84 AS |
8070 | map = bpf_map_inc(map, false); |
8071 | if (IS_ERR(map)) { | |
8072 | fdput(f); | |
8073 | return PTR_ERR(map); | |
8074 | } | |
d8eca5bb DB |
8075 | |
8076 | aux->map_index = env->used_map_cnt; | |
92117d84 AS |
8077 | env->used_maps[env->used_map_cnt++] = map; |
8078 | ||
b741f163 | 8079 | if (bpf_map_is_cgroup_storage(map) && |
de9cbbaa | 8080 | bpf_cgroup_storage_assign(env->prog, map)) { |
b741f163 | 8081 | verbose(env, "only one cgroup storage of each type is allowed\n"); |
de9cbbaa RG |
8082 | fdput(f); |
8083 | return -EBUSY; | |
8084 | } | |
8085 | ||
0246e64d AS |
8086 | fdput(f); |
8087 | next_insn: | |
8088 | insn++; | |
8089 | i++; | |
5e581dad DB |
8090 | continue; |
8091 | } | |
8092 | ||
8093 | /* Basic sanity check before we invest more work here. */ | |
8094 | if (!bpf_opcode_in_insntable(insn->code)) { | |
8095 | verbose(env, "unknown opcode %02x\n", insn->code); | |
8096 | return -EINVAL; | |
0246e64d AS |
8097 | } |
8098 | } | |
8099 | ||
8100 | /* now all pseudo BPF_LD_IMM64 instructions load valid | |
8101 | * 'struct bpf_map *' into a register instead of user map_fd. | |
8102 | * These pointers will be used later by verifier to validate map access. | |
8103 | */ | |
8104 | return 0; | |
8105 | } | |
8106 | ||
8107 | /* drop refcnt of maps used by the rejected program */ | |
58e2af8b | 8108 | static void release_maps(struct bpf_verifier_env *env) |
0246e64d | 8109 | { |
8bad74f9 | 8110 | enum bpf_cgroup_storage_type stype; |
0246e64d AS |
8111 | int i; |
8112 | ||
8bad74f9 RG |
8113 | for_each_cgroup_storage_type(stype) { |
8114 | if (!env->prog->aux->cgroup_storage[stype]) | |
8115 | continue; | |
de9cbbaa | 8116 | bpf_cgroup_storage_release(env->prog, |
8bad74f9 RG |
8117 | env->prog->aux->cgroup_storage[stype]); |
8118 | } | |
de9cbbaa | 8119 | |
0246e64d AS |
8120 | for (i = 0; i < env->used_map_cnt; i++) |
8121 | bpf_map_put(env->used_maps[i]); | |
8122 | } | |
8123 | ||
8124 | /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */ | |
58e2af8b | 8125 | static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env) |
0246e64d AS |
8126 | { |
8127 | struct bpf_insn *insn = env->prog->insnsi; | |
8128 | int insn_cnt = env->prog->len; | |
8129 | int i; | |
8130 | ||
8131 | for (i = 0; i < insn_cnt; i++, insn++) | |
8132 | if (insn->code == (BPF_LD | BPF_IMM | BPF_DW)) | |
8133 | insn->src_reg = 0; | |
8134 | } | |
8135 | ||
8041902d AS |
8136 | /* single env->prog->insni[off] instruction was replaced with the range |
8137 | * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying | |
8138 | * [0, off) and [off, end) to new locations, so the patched range stays zero | |
8139 | */ | |
b325fbca JW |
8140 | static int adjust_insn_aux_data(struct bpf_verifier_env *env, |
8141 | struct bpf_prog *new_prog, u32 off, u32 cnt) | |
8041902d AS |
8142 | { |
8143 | struct bpf_insn_aux_data *new_data, *old_data = env->insn_aux_data; | |
b325fbca JW |
8144 | struct bpf_insn *insn = new_prog->insnsi; |
8145 | u32 prog_len; | |
c131187d | 8146 | int i; |
8041902d | 8147 | |
b325fbca JW |
8148 | /* aux info at OFF always needs adjustment, no matter fast path |
8149 | * (cnt == 1) is taken or not. There is no guarantee INSN at OFF is the | |
8150 | * original insn at old prog. | |
8151 | */ | |
8152 | old_data[off].zext_dst = insn_has_def32(env, insn + off + cnt - 1); | |
8153 | ||
8041902d AS |
8154 | if (cnt == 1) |
8155 | return 0; | |
b325fbca | 8156 | prog_len = new_prog->len; |
fad953ce KC |
8157 | new_data = vzalloc(array_size(prog_len, |
8158 | sizeof(struct bpf_insn_aux_data))); | |
8041902d AS |
8159 | if (!new_data) |
8160 | return -ENOMEM; | |
8161 | memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off); | |
8162 | memcpy(new_data + off + cnt - 1, old_data + off, | |
8163 | sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1)); | |
b325fbca | 8164 | for (i = off; i < off + cnt - 1; i++) { |
c131187d | 8165 | new_data[i].seen = true; |
b325fbca JW |
8166 | new_data[i].zext_dst = insn_has_def32(env, insn + i); |
8167 | } | |
8041902d AS |
8168 | env->insn_aux_data = new_data; |
8169 | vfree(old_data); | |
8170 | return 0; | |
8171 | } | |
8172 | ||
cc8b0b92 AS |
8173 | static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len) |
8174 | { | |
8175 | int i; | |
8176 | ||
8177 | if (len == 1) | |
8178 | return; | |
4cb3d99c JW |
8179 | /* NOTE: fake 'exit' subprog should be updated as well. */ |
8180 | for (i = 0; i <= env->subprog_cnt; i++) { | |
afd59424 | 8181 | if (env->subprog_info[i].start <= off) |
cc8b0b92 | 8182 | continue; |
9c8105bd | 8183 | env->subprog_info[i].start += len - 1; |
cc8b0b92 AS |
8184 | } |
8185 | } | |
8186 | ||
8041902d AS |
8187 | static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off, |
8188 | const struct bpf_insn *patch, u32 len) | |
8189 | { | |
8190 | struct bpf_prog *new_prog; | |
8191 | ||
8192 | new_prog = bpf_patch_insn_single(env->prog, off, patch, len); | |
4f73379e AS |
8193 | if (IS_ERR(new_prog)) { |
8194 | if (PTR_ERR(new_prog) == -ERANGE) | |
8195 | verbose(env, | |
8196 | "insn %d cannot be patched due to 16-bit range\n", | |
8197 | env->insn_aux_data[off].orig_idx); | |
8041902d | 8198 | return NULL; |
4f73379e | 8199 | } |
b325fbca | 8200 | if (adjust_insn_aux_data(env, new_prog, off, len)) |
8041902d | 8201 | return NULL; |
cc8b0b92 | 8202 | adjust_subprog_starts(env, off, len); |
8041902d AS |
8203 | return new_prog; |
8204 | } | |
8205 | ||
52875a04 JK |
8206 | static int adjust_subprog_starts_after_remove(struct bpf_verifier_env *env, |
8207 | u32 off, u32 cnt) | |
8208 | { | |
8209 | int i, j; | |
8210 | ||
8211 | /* find first prog starting at or after off (first to remove) */ | |
8212 | for (i = 0; i < env->subprog_cnt; i++) | |
8213 | if (env->subprog_info[i].start >= off) | |
8214 | break; | |
8215 | /* find first prog starting at or after off + cnt (first to stay) */ | |
8216 | for (j = i; j < env->subprog_cnt; j++) | |
8217 | if (env->subprog_info[j].start >= off + cnt) | |
8218 | break; | |
8219 | /* if j doesn't start exactly at off + cnt, we are just removing | |
8220 | * the front of previous prog | |
8221 | */ | |
8222 | if (env->subprog_info[j].start != off + cnt) | |
8223 | j--; | |
8224 | ||
8225 | if (j > i) { | |
8226 | struct bpf_prog_aux *aux = env->prog->aux; | |
8227 | int move; | |
8228 | ||
8229 | /* move fake 'exit' subprog as well */ | |
8230 | move = env->subprog_cnt + 1 - j; | |
8231 | ||
8232 | memmove(env->subprog_info + i, | |
8233 | env->subprog_info + j, | |
8234 | sizeof(*env->subprog_info) * move); | |
8235 | env->subprog_cnt -= j - i; | |
8236 | ||
8237 | /* remove func_info */ | |
8238 | if (aux->func_info) { | |
8239 | move = aux->func_info_cnt - j; | |
8240 | ||
8241 | memmove(aux->func_info + i, | |
8242 | aux->func_info + j, | |
8243 | sizeof(*aux->func_info) * move); | |
8244 | aux->func_info_cnt -= j - i; | |
8245 | /* func_info->insn_off is set after all code rewrites, | |
8246 | * in adjust_btf_func() - no need to adjust | |
8247 | */ | |
8248 | } | |
8249 | } else { | |
8250 | /* convert i from "first prog to remove" to "first to adjust" */ | |
8251 | if (env->subprog_info[i].start == off) | |
8252 | i++; | |
8253 | } | |
8254 | ||
8255 | /* update fake 'exit' subprog as well */ | |
8256 | for (; i <= env->subprog_cnt; i++) | |
8257 | env->subprog_info[i].start -= cnt; | |
8258 | ||
8259 | return 0; | |
8260 | } | |
8261 | ||
8262 | static int bpf_adj_linfo_after_remove(struct bpf_verifier_env *env, u32 off, | |
8263 | u32 cnt) | |
8264 | { | |
8265 | struct bpf_prog *prog = env->prog; | |
8266 | u32 i, l_off, l_cnt, nr_linfo; | |
8267 | struct bpf_line_info *linfo; | |
8268 | ||
8269 | nr_linfo = prog->aux->nr_linfo; | |
8270 | if (!nr_linfo) | |
8271 | return 0; | |
8272 | ||
8273 | linfo = prog->aux->linfo; | |
8274 | ||
8275 | /* find first line info to remove, count lines to be removed */ | |
8276 | for (i = 0; i < nr_linfo; i++) | |
8277 | if (linfo[i].insn_off >= off) | |
8278 | break; | |
8279 | ||
8280 | l_off = i; | |
8281 | l_cnt = 0; | |
8282 | for (; i < nr_linfo; i++) | |
8283 | if (linfo[i].insn_off < off + cnt) | |
8284 | l_cnt++; | |
8285 | else | |
8286 | break; | |
8287 | ||
8288 | /* First live insn doesn't match first live linfo, it needs to "inherit" | |
8289 | * last removed linfo. prog is already modified, so prog->len == off | |
8290 | * means no live instructions after (tail of the program was removed). | |
8291 | */ | |
8292 | if (prog->len != off && l_cnt && | |
8293 | (i == nr_linfo || linfo[i].insn_off != off + cnt)) { | |
8294 | l_cnt--; | |
8295 | linfo[--i].insn_off = off + cnt; | |
8296 | } | |
8297 | ||
8298 | /* remove the line info which refer to the removed instructions */ | |
8299 | if (l_cnt) { | |
8300 | memmove(linfo + l_off, linfo + i, | |
8301 | sizeof(*linfo) * (nr_linfo - i)); | |
8302 | ||
8303 | prog->aux->nr_linfo -= l_cnt; | |
8304 | nr_linfo = prog->aux->nr_linfo; | |
8305 | } | |
8306 | ||
8307 | /* pull all linfo[i].insn_off >= off + cnt in by cnt */ | |
8308 | for (i = l_off; i < nr_linfo; i++) | |
8309 | linfo[i].insn_off -= cnt; | |
8310 | ||
8311 | /* fix up all subprogs (incl. 'exit') which start >= off */ | |
8312 | for (i = 0; i <= env->subprog_cnt; i++) | |
8313 | if (env->subprog_info[i].linfo_idx > l_off) { | |
8314 | /* program may have started in the removed region but | |
8315 | * may not be fully removed | |
8316 | */ | |
8317 | if (env->subprog_info[i].linfo_idx >= l_off + l_cnt) | |
8318 | env->subprog_info[i].linfo_idx -= l_cnt; | |
8319 | else | |
8320 | env->subprog_info[i].linfo_idx = l_off; | |
8321 | } | |
8322 | ||
8323 | return 0; | |
8324 | } | |
8325 | ||
8326 | static int verifier_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt) | |
8327 | { | |
8328 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
8329 | unsigned int orig_prog_len = env->prog->len; | |
8330 | int err; | |
8331 | ||
08ca90af JK |
8332 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
8333 | bpf_prog_offload_remove_insns(env, off, cnt); | |
8334 | ||
52875a04 JK |
8335 | err = bpf_remove_insns(env->prog, off, cnt); |
8336 | if (err) | |
8337 | return err; | |
8338 | ||
8339 | err = adjust_subprog_starts_after_remove(env, off, cnt); | |
8340 | if (err) | |
8341 | return err; | |
8342 | ||
8343 | err = bpf_adj_linfo_after_remove(env, off, cnt); | |
8344 | if (err) | |
8345 | return err; | |
8346 | ||
8347 | memmove(aux_data + off, aux_data + off + cnt, | |
8348 | sizeof(*aux_data) * (orig_prog_len - off - cnt)); | |
8349 | ||
8350 | return 0; | |
8351 | } | |
8352 | ||
2a5418a1 DB |
8353 | /* The verifier does more data flow analysis than llvm and will not |
8354 | * explore branches that are dead at run time. Malicious programs can | |
8355 | * have dead code too. Therefore replace all dead at-run-time code | |
8356 | * with 'ja -1'. | |
8357 | * | |
8358 | * Just nops are not optimal, e.g. if they would sit at the end of the | |
8359 | * program and through another bug we would manage to jump there, then | |
8360 | * we'd execute beyond program memory otherwise. Returning exception | |
8361 | * code also wouldn't work since we can have subprogs where the dead | |
8362 | * code could be located. | |
c131187d AS |
8363 | */ |
8364 | static void sanitize_dead_code(struct bpf_verifier_env *env) | |
8365 | { | |
8366 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
2a5418a1 | 8367 | struct bpf_insn trap = BPF_JMP_IMM(BPF_JA, 0, 0, -1); |
c131187d AS |
8368 | struct bpf_insn *insn = env->prog->insnsi; |
8369 | const int insn_cnt = env->prog->len; | |
8370 | int i; | |
8371 | ||
8372 | for (i = 0; i < insn_cnt; i++) { | |
8373 | if (aux_data[i].seen) | |
8374 | continue; | |
2a5418a1 | 8375 | memcpy(insn + i, &trap, sizeof(trap)); |
c131187d AS |
8376 | } |
8377 | } | |
8378 | ||
e2ae4ca2 JK |
8379 | static bool insn_is_cond_jump(u8 code) |
8380 | { | |
8381 | u8 op; | |
8382 | ||
092ed096 JW |
8383 | if (BPF_CLASS(code) == BPF_JMP32) |
8384 | return true; | |
8385 | ||
e2ae4ca2 JK |
8386 | if (BPF_CLASS(code) != BPF_JMP) |
8387 | return false; | |
8388 | ||
8389 | op = BPF_OP(code); | |
8390 | return op != BPF_JA && op != BPF_EXIT && op != BPF_CALL; | |
8391 | } | |
8392 | ||
8393 | static void opt_hard_wire_dead_code_branches(struct bpf_verifier_env *env) | |
8394 | { | |
8395 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
8396 | struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); | |
8397 | struct bpf_insn *insn = env->prog->insnsi; | |
8398 | const int insn_cnt = env->prog->len; | |
8399 | int i; | |
8400 | ||
8401 | for (i = 0; i < insn_cnt; i++, insn++) { | |
8402 | if (!insn_is_cond_jump(insn->code)) | |
8403 | continue; | |
8404 | ||
8405 | if (!aux_data[i + 1].seen) | |
8406 | ja.off = insn->off; | |
8407 | else if (!aux_data[i + 1 + insn->off].seen) | |
8408 | ja.off = 0; | |
8409 | else | |
8410 | continue; | |
8411 | ||
08ca90af JK |
8412 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
8413 | bpf_prog_offload_replace_insn(env, i, &ja); | |
8414 | ||
e2ae4ca2 JK |
8415 | memcpy(insn, &ja, sizeof(ja)); |
8416 | } | |
8417 | } | |
8418 | ||
52875a04 JK |
8419 | static int opt_remove_dead_code(struct bpf_verifier_env *env) |
8420 | { | |
8421 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
8422 | int insn_cnt = env->prog->len; | |
8423 | int i, err; | |
8424 | ||
8425 | for (i = 0; i < insn_cnt; i++) { | |
8426 | int j; | |
8427 | ||
8428 | j = 0; | |
8429 | while (i + j < insn_cnt && !aux_data[i + j].seen) | |
8430 | j++; | |
8431 | if (!j) | |
8432 | continue; | |
8433 | ||
8434 | err = verifier_remove_insns(env, i, j); | |
8435 | if (err) | |
8436 | return err; | |
8437 | insn_cnt = env->prog->len; | |
8438 | } | |
8439 | ||
8440 | return 0; | |
8441 | } | |
8442 | ||
a1b14abc JK |
8443 | static int opt_remove_nops(struct bpf_verifier_env *env) |
8444 | { | |
8445 | const struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); | |
8446 | struct bpf_insn *insn = env->prog->insnsi; | |
8447 | int insn_cnt = env->prog->len; | |
8448 | int i, err; | |
8449 | ||
8450 | for (i = 0; i < insn_cnt; i++) { | |
8451 | if (memcmp(&insn[i], &ja, sizeof(ja))) | |
8452 | continue; | |
8453 | ||
8454 | err = verifier_remove_insns(env, i, 1); | |
8455 | if (err) | |
8456 | return err; | |
8457 | insn_cnt--; | |
8458 | i--; | |
8459 | } | |
8460 | ||
8461 | return 0; | |
8462 | } | |
8463 | ||
d6c2308c JW |
8464 | static int opt_subreg_zext_lo32_rnd_hi32(struct bpf_verifier_env *env, |
8465 | const union bpf_attr *attr) | |
a4b1d3c1 | 8466 | { |
d6c2308c | 8467 | struct bpf_insn *patch, zext_patch[2], rnd_hi32_patch[4]; |
a4b1d3c1 | 8468 | struct bpf_insn_aux_data *aux = env->insn_aux_data; |
d6c2308c | 8469 | int i, patch_len, delta = 0, len = env->prog->len; |
a4b1d3c1 | 8470 | struct bpf_insn *insns = env->prog->insnsi; |
a4b1d3c1 | 8471 | struct bpf_prog *new_prog; |
d6c2308c | 8472 | bool rnd_hi32; |
a4b1d3c1 | 8473 | |
d6c2308c | 8474 | rnd_hi32 = attr->prog_flags & BPF_F_TEST_RND_HI32; |
a4b1d3c1 | 8475 | zext_patch[1] = BPF_ZEXT_REG(0); |
d6c2308c JW |
8476 | rnd_hi32_patch[1] = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, 0); |
8477 | rnd_hi32_patch[2] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32); | |
8478 | rnd_hi32_patch[3] = BPF_ALU64_REG(BPF_OR, 0, BPF_REG_AX); | |
a4b1d3c1 JW |
8479 | for (i = 0; i < len; i++) { |
8480 | int adj_idx = i + delta; | |
8481 | struct bpf_insn insn; | |
8482 | ||
d6c2308c JW |
8483 | insn = insns[adj_idx]; |
8484 | if (!aux[adj_idx].zext_dst) { | |
8485 | u8 code, class; | |
8486 | u32 imm_rnd; | |
8487 | ||
8488 | if (!rnd_hi32) | |
8489 | continue; | |
8490 | ||
8491 | code = insn.code; | |
8492 | class = BPF_CLASS(code); | |
8493 | if (insn_no_def(&insn)) | |
8494 | continue; | |
8495 | ||
8496 | /* NOTE: arg "reg" (the fourth one) is only used for | |
8497 | * BPF_STX which has been ruled out in above | |
8498 | * check, it is safe to pass NULL here. | |
8499 | */ | |
8500 | if (is_reg64(env, &insn, insn.dst_reg, NULL, DST_OP)) { | |
8501 | if (class == BPF_LD && | |
8502 | BPF_MODE(code) == BPF_IMM) | |
8503 | i++; | |
8504 | continue; | |
8505 | } | |
8506 | ||
8507 | /* ctx load could be transformed into wider load. */ | |
8508 | if (class == BPF_LDX && | |
8509 | aux[adj_idx].ptr_type == PTR_TO_CTX) | |
8510 | continue; | |
8511 | ||
8512 | imm_rnd = get_random_int(); | |
8513 | rnd_hi32_patch[0] = insn; | |
8514 | rnd_hi32_patch[1].imm = imm_rnd; | |
8515 | rnd_hi32_patch[3].dst_reg = insn.dst_reg; | |
8516 | patch = rnd_hi32_patch; | |
8517 | patch_len = 4; | |
8518 | goto apply_patch_buffer; | |
8519 | } | |
8520 | ||
8521 | if (!bpf_jit_needs_zext()) | |
a4b1d3c1 JW |
8522 | continue; |
8523 | ||
a4b1d3c1 JW |
8524 | zext_patch[0] = insn; |
8525 | zext_patch[1].dst_reg = insn.dst_reg; | |
8526 | zext_patch[1].src_reg = insn.dst_reg; | |
d6c2308c JW |
8527 | patch = zext_patch; |
8528 | patch_len = 2; | |
8529 | apply_patch_buffer: | |
8530 | new_prog = bpf_patch_insn_data(env, adj_idx, patch, patch_len); | |
a4b1d3c1 JW |
8531 | if (!new_prog) |
8532 | return -ENOMEM; | |
8533 | env->prog = new_prog; | |
8534 | insns = new_prog->insnsi; | |
8535 | aux = env->insn_aux_data; | |
d6c2308c | 8536 | delta += patch_len - 1; |
a4b1d3c1 JW |
8537 | } |
8538 | ||
8539 | return 0; | |
8540 | } | |
8541 | ||
c64b7983 JS |
8542 | /* convert load instructions that access fields of a context type into a |
8543 | * sequence of instructions that access fields of the underlying structure: | |
8544 | * struct __sk_buff -> struct sk_buff | |
8545 | * struct bpf_sock_ops -> struct sock | |
9bac3d6d | 8546 | */ |
58e2af8b | 8547 | static int convert_ctx_accesses(struct bpf_verifier_env *env) |
9bac3d6d | 8548 | { |
00176a34 | 8549 | const struct bpf_verifier_ops *ops = env->ops; |
f96da094 | 8550 | int i, cnt, size, ctx_field_size, delta = 0; |
3df126f3 | 8551 | const int insn_cnt = env->prog->len; |
36bbef52 | 8552 | struct bpf_insn insn_buf[16], *insn; |
46f53a65 | 8553 | u32 target_size, size_default, off; |
9bac3d6d | 8554 | struct bpf_prog *new_prog; |
d691f9e8 | 8555 | enum bpf_access_type type; |
f96da094 | 8556 | bool is_narrower_load; |
9bac3d6d | 8557 | |
b09928b9 DB |
8558 | if (ops->gen_prologue || env->seen_direct_write) { |
8559 | if (!ops->gen_prologue) { | |
8560 | verbose(env, "bpf verifier is misconfigured\n"); | |
8561 | return -EINVAL; | |
8562 | } | |
36bbef52 DB |
8563 | cnt = ops->gen_prologue(insn_buf, env->seen_direct_write, |
8564 | env->prog); | |
8565 | if (cnt >= ARRAY_SIZE(insn_buf)) { | |
61bd5218 | 8566 | verbose(env, "bpf verifier is misconfigured\n"); |
36bbef52 DB |
8567 | return -EINVAL; |
8568 | } else if (cnt) { | |
8041902d | 8569 | new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt); |
36bbef52 DB |
8570 | if (!new_prog) |
8571 | return -ENOMEM; | |
8041902d | 8572 | |
36bbef52 | 8573 | env->prog = new_prog; |
3df126f3 | 8574 | delta += cnt - 1; |
36bbef52 DB |
8575 | } |
8576 | } | |
8577 | ||
c64b7983 | 8578 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
9bac3d6d AS |
8579 | return 0; |
8580 | ||
3df126f3 | 8581 | insn = env->prog->insnsi + delta; |
36bbef52 | 8582 | |
9bac3d6d | 8583 | for (i = 0; i < insn_cnt; i++, insn++) { |
c64b7983 JS |
8584 | bpf_convert_ctx_access_t convert_ctx_access; |
8585 | ||
62c7989b DB |
8586 | if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) || |
8587 | insn->code == (BPF_LDX | BPF_MEM | BPF_H) || | |
8588 | insn->code == (BPF_LDX | BPF_MEM | BPF_W) || | |
ea2e7ce5 | 8589 | insn->code == (BPF_LDX | BPF_MEM | BPF_DW)) |
d691f9e8 | 8590 | type = BPF_READ; |
62c7989b DB |
8591 | else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) || |
8592 | insn->code == (BPF_STX | BPF_MEM | BPF_H) || | |
8593 | insn->code == (BPF_STX | BPF_MEM | BPF_W) || | |
ea2e7ce5 | 8594 | insn->code == (BPF_STX | BPF_MEM | BPF_DW)) |
d691f9e8 AS |
8595 | type = BPF_WRITE; |
8596 | else | |
9bac3d6d AS |
8597 | continue; |
8598 | ||
af86ca4e AS |
8599 | if (type == BPF_WRITE && |
8600 | env->insn_aux_data[i + delta].sanitize_stack_off) { | |
8601 | struct bpf_insn patch[] = { | |
8602 | /* Sanitize suspicious stack slot with zero. | |
8603 | * There are no memory dependencies for this store, | |
8604 | * since it's only using frame pointer and immediate | |
8605 | * constant of zero | |
8606 | */ | |
8607 | BPF_ST_MEM(BPF_DW, BPF_REG_FP, | |
8608 | env->insn_aux_data[i + delta].sanitize_stack_off, | |
8609 | 0), | |
8610 | /* the original STX instruction will immediately | |
8611 | * overwrite the same stack slot with appropriate value | |
8612 | */ | |
8613 | *insn, | |
8614 | }; | |
8615 | ||
8616 | cnt = ARRAY_SIZE(patch); | |
8617 | new_prog = bpf_patch_insn_data(env, i + delta, patch, cnt); | |
8618 | if (!new_prog) | |
8619 | return -ENOMEM; | |
8620 | ||
8621 | delta += cnt - 1; | |
8622 | env->prog = new_prog; | |
8623 | insn = new_prog->insnsi + i + delta; | |
8624 | continue; | |
8625 | } | |
8626 | ||
c64b7983 JS |
8627 | switch (env->insn_aux_data[i + delta].ptr_type) { |
8628 | case PTR_TO_CTX: | |
8629 | if (!ops->convert_ctx_access) | |
8630 | continue; | |
8631 | convert_ctx_access = ops->convert_ctx_access; | |
8632 | break; | |
8633 | case PTR_TO_SOCKET: | |
46f8bc92 | 8634 | case PTR_TO_SOCK_COMMON: |
c64b7983 JS |
8635 | convert_ctx_access = bpf_sock_convert_ctx_access; |
8636 | break; | |
655a51e5 MKL |
8637 | case PTR_TO_TCP_SOCK: |
8638 | convert_ctx_access = bpf_tcp_sock_convert_ctx_access; | |
8639 | break; | |
fada7fdc JL |
8640 | case PTR_TO_XDP_SOCK: |
8641 | convert_ctx_access = bpf_xdp_sock_convert_ctx_access; | |
8642 | break; | |
c64b7983 | 8643 | default: |
9bac3d6d | 8644 | continue; |
c64b7983 | 8645 | } |
9bac3d6d | 8646 | |
31fd8581 | 8647 | ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size; |
f96da094 | 8648 | size = BPF_LDST_BYTES(insn); |
31fd8581 YS |
8649 | |
8650 | /* If the read access is a narrower load of the field, | |
8651 | * convert to a 4/8-byte load, to minimum program type specific | |
8652 | * convert_ctx_access changes. If conversion is successful, | |
8653 | * we will apply proper mask to the result. | |
8654 | */ | |
f96da094 | 8655 | is_narrower_load = size < ctx_field_size; |
46f53a65 AI |
8656 | size_default = bpf_ctx_off_adjust_machine(ctx_field_size); |
8657 | off = insn->off; | |
31fd8581 | 8658 | if (is_narrower_load) { |
f96da094 DB |
8659 | u8 size_code; |
8660 | ||
8661 | if (type == BPF_WRITE) { | |
61bd5218 | 8662 | verbose(env, "bpf verifier narrow ctx access misconfigured\n"); |
f96da094 DB |
8663 | return -EINVAL; |
8664 | } | |
31fd8581 | 8665 | |
f96da094 | 8666 | size_code = BPF_H; |
31fd8581 YS |
8667 | if (ctx_field_size == 4) |
8668 | size_code = BPF_W; | |
8669 | else if (ctx_field_size == 8) | |
8670 | size_code = BPF_DW; | |
f96da094 | 8671 | |
bc23105c | 8672 | insn->off = off & ~(size_default - 1); |
31fd8581 YS |
8673 | insn->code = BPF_LDX | BPF_MEM | size_code; |
8674 | } | |
f96da094 DB |
8675 | |
8676 | target_size = 0; | |
c64b7983 JS |
8677 | cnt = convert_ctx_access(type, insn, insn_buf, env->prog, |
8678 | &target_size); | |
f96da094 DB |
8679 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) || |
8680 | (ctx_field_size && !target_size)) { | |
61bd5218 | 8681 | verbose(env, "bpf verifier is misconfigured\n"); |
9bac3d6d AS |
8682 | return -EINVAL; |
8683 | } | |
f96da094 DB |
8684 | |
8685 | if (is_narrower_load && size < target_size) { | |
d895a0f1 IL |
8686 | u8 shift = bpf_ctx_narrow_access_offset( |
8687 | off, size, size_default) * 8; | |
46f53a65 AI |
8688 | if (ctx_field_size <= 4) { |
8689 | if (shift) | |
8690 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_RSH, | |
8691 | insn->dst_reg, | |
8692 | shift); | |
31fd8581 | 8693 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg, |
f96da094 | 8694 | (1 << size * 8) - 1); |
46f53a65 AI |
8695 | } else { |
8696 | if (shift) | |
8697 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_RSH, | |
8698 | insn->dst_reg, | |
8699 | shift); | |
31fd8581 | 8700 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg, |
e2f7fc0a | 8701 | (1ULL << size * 8) - 1); |
46f53a65 | 8702 | } |
31fd8581 | 8703 | } |
9bac3d6d | 8704 | |
8041902d | 8705 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
9bac3d6d AS |
8706 | if (!new_prog) |
8707 | return -ENOMEM; | |
8708 | ||
3df126f3 | 8709 | delta += cnt - 1; |
9bac3d6d AS |
8710 | |
8711 | /* keep walking new program and skip insns we just inserted */ | |
8712 | env->prog = new_prog; | |
3df126f3 | 8713 | insn = new_prog->insnsi + i + delta; |
9bac3d6d AS |
8714 | } |
8715 | ||
8716 | return 0; | |
8717 | } | |
8718 | ||
1c2a088a AS |
8719 | static int jit_subprogs(struct bpf_verifier_env *env) |
8720 | { | |
8721 | struct bpf_prog *prog = env->prog, **func, *tmp; | |
8722 | int i, j, subprog_start, subprog_end = 0, len, subprog; | |
7105e828 | 8723 | struct bpf_insn *insn; |
1c2a088a | 8724 | void *old_bpf_func; |
c454a46b | 8725 | int err; |
1c2a088a | 8726 | |
f910cefa | 8727 | if (env->subprog_cnt <= 1) |
1c2a088a AS |
8728 | return 0; |
8729 | ||
7105e828 | 8730 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
1c2a088a AS |
8731 | if (insn->code != (BPF_JMP | BPF_CALL) || |
8732 | insn->src_reg != BPF_PSEUDO_CALL) | |
8733 | continue; | |
c7a89784 DB |
8734 | /* Upon error here we cannot fall back to interpreter but |
8735 | * need a hard reject of the program. Thus -EFAULT is | |
8736 | * propagated in any case. | |
8737 | */ | |
1c2a088a AS |
8738 | subprog = find_subprog(env, i + insn->imm + 1); |
8739 | if (subprog < 0) { | |
8740 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", | |
8741 | i + insn->imm + 1); | |
8742 | return -EFAULT; | |
8743 | } | |
8744 | /* temporarily remember subprog id inside insn instead of | |
8745 | * aux_data, since next loop will split up all insns into funcs | |
8746 | */ | |
f910cefa | 8747 | insn->off = subprog; |
1c2a088a AS |
8748 | /* remember original imm in case JIT fails and fallback |
8749 | * to interpreter will be needed | |
8750 | */ | |
8751 | env->insn_aux_data[i].call_imm = insn->imm; | |
8752 | /* point imm to __bpf_call_base+1 from JITs point of view */ | |
8753 | insn->imm = 1; | |
8754 | } | |
8755 | ||
c454a46b MKL |
8756 | err = bpf_prog_alloc_jited_linfo(prog); |
8757 | if (err) | |
8758 | goto out_undo_insn; | |
8759 | ||
8760 | err = -ENOMEM; | |
6396bb22 | 8761 | func = kcalloc(env->subprog_cnt, sizeof(prog), GFP_KERNEL); |
1c2a088a | 8762 | if (!func) |
c7a89784 | 8763 | goto out_undo_insn; |
1c2a088a | 8764 | |
f910cefa | 8765 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a | 8766 | subprog_start = subprog_end; |
4cb3d99c | 8767 | subprog_end = env->subprog_info[i + 1].start; |
1c2a088a AS |
8768 | |
8769 | len = subprog_end - subprog_start; | |
492ecee8 AS |
8770 | /* BPF_PROG_RUN doesn't call subprogs directly, |
8771 | * hence main prog stats include the runtime of subprogs. | |
8772 | * subprogs don't have IDs and not reachable via prog_get_next_id | |
8773 | * func[i]->aux->stats will never be accessed and stays NULL | |
8774 | */ | |
8775 | func[i] = bpf_prog_alloc_no_stats(bpf_prog_size(len), GFP_USER); | |
1c2a088a AS |
8776 | if (!func[i]) |
8777 | goto out_free; | |
8778 | memcpy(func[i]->insnsi, &prog->insnsi[subprog_start], | |
8779 | len * sizeof(struct bpf_insn)); | |
4f74d809 | 8780 | func[i]->type = prog->type; |
1c2a088a | 8781 | func[i]->len = len; |
4f74d809 DB |
8782 | if (bpf_prog_calc_tag(func[i])) |
8783 | goto out_free; | |
1c2a088a | 8784 | func[i]->is_func = 1; |
ba64e7d8 YS |
8785 | func[i]->aux->func_idx = i; |
8786 | /* the btf and func_info will be freed only at prog->aux */ | |
8787 | func[i]->aux->btf = prog->aux->btf; | |
8788 | func[i]->aux->func_info = prog->aux->func_info; | |
8789 | ||
1c2a088a AS |
8790 | /* Use bpf_prog_F_tag to indicate functions in stack traces. |
8791 | * Long term would need debug info to populate names | |
8792 | */ | |
8793 | func[i]->aux->name[0] = 'F'; | |
9c8105bd | 8794 | func[i]->aux->stack_depth = env->subprog_info[i].stack_depth; |
1c2a088a | 8795 | func[i]->jit_requested = 1; |
c454a46b MKL |
8796 | func[i]->aux->linfo = prog->aux->linfo; |
8797 | func[i]->aux->nr_linfo = prog->aux->nr_linfo; | |
8798 | func[i]->aux->jited_linfo = prog->aux->jited_linfo; | |
8799 | func[i]->aux->linfo_idx = env->subprog_info[i].linfo_idx; | |
1c2a088a AS |
8800 | func[i] = bpf_int_jit_compile(func[i]); |
8801 | if (!func[i]->jited) { | |
8802 | err = -ENOTSUPP; | |
8803 | goto out_free; | |
8804 | } | |
8805 | cond_resched(); | |
8806 | } | |
8807 | /* at this point all bpf functions were successfully JITed | |
8808 | * now populate all bpf_calls with correct addresses and | |
8809 | * run last pass of JIT | |
8810 | */ | |
f910cefa | 8811 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a AS |
8812 | insn = func[i]->insnsi; |
8813 | for (j = 0; j < func[i]->len; j++, insn++) { | |
8814 | if (insn->code != (BPF_JMP | BPF_CALL) || | |
8815 | insn->src_reg != BPF_PSEUDO_CALL) | |
8816 | continue; | |
8817 | subprog = insn->off; | |
0d306c31 PB |
8818 | insn->imm = BPF_CAST_CALL(func[subprog]->bpf_func) - |
8819 | __bpf_call_base; | |
1c2a088a | 8820 | } |
2162fed4 SD |
8821 | |
8822 | /* we use the aux data to keep a list of the start addresses | |
8823 | * of the JITed images for each function in the program | |
8824 | * | |
8825 | * for some architectures, such as powerpc64, the imm field | |
8826 | * might not be large enough to hold the offset of the start | |
8827 | * address of the callee's JITed image from __bpf_call_base | |
8828 | * | |
8829 | * in such cases, we can lookup the start address of a callee | |
8830 | * by using its subprog id, available from the off field of | |
8831 | * the call instruction, as an index for this list | |
8832 | */ | |
8833 | func[i]->aux->func = func; | |
8834 | func[i]->aux->func_cnt = env->subprog_cnt; | |
1c2a088a | 8835 | } |
f910cefa | 8836 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a AS |
8837 | old_bpf_func = func[i]->bpf_func; |
8838 | tmp = bpf_int_jit_compile(func[i]); | |
8839 | if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) { | |
8840 | verbose(env, "JIT doesn't support bpf-to-bpf calls\n"); | |
c7a89784 | 8841 | err = -ENOTSUPP; |
1c2a088a AS |
8842 | goto out_free; |
8843 | } | |
8844 | cond_resched(); | |
8845 | } | |
8846 | ||
8847 | /* finally lock prog and jit images for all functions and | |
8848 | * populate kallsysm | |
8849 | */ | |
f910cefa | 8850 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a AS |
8851 | bpf_prog_lock_ro(func[i]); |
8852 | bpf_prog_kallsyms_add(func[i]); | |
8853 | } | |
7105e828 DB |
8854 | |
8855 | /* Last step: make now unused interpreter insns from main | |
8856 | * prog consistent for later dump requests, so they can | |
8857 | * later look the same as if they were interpreted only. | |
8858 | */ | |
8859 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { | |
7105e828 DB |
8860 | if (insn->code != (BPF_JMP | BPF_CALL) || |
8861 | insn->src_reg != BPF_PSEUDO_CALL) | |
8862 | continue; | |
8863 | insn->off = env->insn_aux_data[i].call_imm; | |
8864 | subprog = find_subprog(env, i + insn->off + 1); | |
dbecd738 | 8865 | insn->imm = subprog; |
7105e828 DB |
8866 | } |
8867 | ||
1c2a088a AS |
8868 | prog->jited = 1; |
8869 | prog->bpf_func = func[0]->bpf_func; | |
8870 | prog->aux->func = func; | |
f910cefa | 8871 | prog->aux->func_cnt = env->subprog_cnt; |
c454a46b | 8872 | bpf_prog_free_unused_jited_linfo(prog); |
1c2a088a AS |
8873 | return 0; |
8874 | out_free: | |
f910cefa | 8875 | for (i = 0; i < env->subprog_cnt; i++) |
1c2a088a AS |
8876 | if (func[i]) |
8877 | bpf_jit_free(func[i]); | |
8878 | kfree(func); | |
c7a89784 | 8879 | out_undo_insn: |
1c2a088a AS |
8880 | /* cleanup main prog to be interpreted */ |
8881 | prog->jit_requested = 0; | |
8882 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { | |
8883 | if (insn->code != (BPF_JMP | BPF_CALL) || | |
8884 | insn->src_reg != BPF_PSEUDO_CALL) | |
8885 | continue; | |
8886 | insn->off = 0; | |
8887 | insn->imm = env->insn_aux_data[i].call_imm; | |
8888 | } | |
c454a46b | 8889 | bpf_prog_free_jited_linfo(prog); |
1c2a088a AS |
8890 | return err; |
8891 | } | |
8892 | ||
1ea47e01 AS |
8893 | static int fixup_call_args(struct bpf_verifier_env *env) |
8894 | { | |
19d28fbd | 8895 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
1ea47e01 AS |
8896 | struct bpf_prog *prog = env->prog; |
8897 | struct bpf_insn *insn = prog->insnsi; | |
8898 | int i, depth; | |
19d28fbd | 8899 | #endif |
e4052d06 | 8900 | int err = 0; |
1ea47e01 | 8901 | |
e4052d06 QM |
8902 | if (env->prog->jit_requested && |
8903 | !bpf_prog_is_dev_bound(env->prog->aux)) { | |
19d28fbd DM |
8904 | err = jit_subprogs(env); |
8905 | if (err == 0) | |
1c2a088a | 8906 | return 0; |
c7a89784 DB |
8907 | if (err == -EFAULT) |
8908 | return err; | |
19d28fbd DM |
8909 | } |
8910 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON | |
1ea47e01 AS |
8911 | for (i = 0; i < prog->len; i++, insn++) { |
8912 | if (insn->code != (BPF_JMP | BPF_CALL) || | |
8913 | insn->src_reg != BPF_PSEUDO_CALL) | |
8914 | continue; | |
8915 | depth = get_callee_stack_depth(env, insn, i); | |
8916 | if (depth < 0) | |
8917 | return depth; | |
8918 | bpf_patch_call_args(insn, depth); | |
8919 | } | |
19d28fbd DM |
8920 | err = 0; |
8921 | #endif | |
8922 | return err; | |
1ea47e01 AS |
8923 | } |
8924 | ||
79741b3b | 8925 | /* fixup insn->imm field of bpf_call instructions |
81ed18ab | 8926 | * and inline eligible helpers as explicit sequence of BPF instructions |
e245c5c6 AS |
8927 | * |
8928 | * this function is called after eBPF program passed verification | |
8929 | */ | |
79741b3b | 8930 | static int fixup_bpf_calls(struct bpf_verifier_env *env) |
e245c5c6 | 8931 | { |
79741b3b AS |
8932 | struct bpf_prog *prog = env->prog; |
8933 | struct bpf_insn *insn = prog->insnsi; | |
e245c5c6 | 8934 | const struct bpf_func_proto *fn; |
79741b3b | 8935 | const int insn_cnt = prog->len; |
09772d92 | 8936 | const struct bpf_map_ops *ops; |
c93552c4 | 8937 | struct bpf_insn_aux_data *aux; |
81ed18ab AS |
8938 | struct bpf_insn insn_buf[16]; |
8939 | struct bpf_prog *new_prog; | |
8940 | struct bpf_map *map_ptr; | |
8941 | int i, cnt, delta = 0; | |
e245c5c6 | 8942 | |
79741b3b | 8943 | for (i = 0; i < insn_cnt; i++, insn++) { |
f6b1b3bf DB |
8944 | if (insn->code == (BPF_ALU64 | BPF_MOD | BPF_X) || |
8945 | insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) || | |
8946 | insn->code == (BPF_ALU | BPF_MOD | BPF_X) || | |
68fda450 | 8947 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { |
f6b1b3bf DB |
8948 | bool is64 = BPF_CLASS(insn->code) == BPF_ALU64; |
8949 | struct bpf_insn mask_and_div[] = { | |
8950 | BPF_MOV32_REG(insn->src_reg, insn->src_reg), | |
8951 | /* Rx div 0 -> 0 */ | |
8952 | BPF_JMP_IMM(BPF_JNE, insn->src_reg, 0, 2), | |
8953 | BPF_ALU32_REG(BPF_XOR, insn->dst_reg, insn->dst_reg), | |
8954 | BPF_JMP_IMM(BPF_JA, 0, 0, 1), | |
8955 | *insn, | |
8956 | }; | |
8957 | struct bpf_insn mask_and_mod[] = { | |
8958 | BPF_MOV32_REG(insn->src_reg, insn->src_reg), | |
8959 | /* Rx mod 0 -> Rx */ | |
8960 | BPF_JMP_IMM(BPF_JEQ, insn->src_reg, 0, 1), | |
8961 | *insn, | |
8962 | }; | |
8963 | struct bpf_insn *patchlet; | |
8964 | ||
8965 | if (insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) || | |
8966 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { | |
8967 | patchlet = mask_and_div + (is64 ? 1 : 0); | |
8968 | cnt = ARRAY_SIZE(mask_and_div) - (is64 ? 1 : 0); | |
8969 | } else { | |
8970 | patchlet = mask_and_mod + (is64 ? 1 : 0); | |
8971 | cnt = ARRAY_SIZE(mask_and_mod) - (is64 ? 1 : 0); | |
8972 | } | |
8973 | ||
8974 | new_prog = bpf_patch_insn_data(env, i + delta, patchlet, cnt); | |
68fda450 AS |
8975 | if (!new_prog) |
8976 | return -ENOMEM; | |
8977 | ||
8978 | delta += cnt - 1; | |
8979 | env->prog = prog = new_prog; | |
8980 | insn = new_prog->insnsi + i + delta; | |
8981 | continue; | |
8982 | } | |
8983 | ||
e0cea7ce DB |
8984 | if (BPF_CLASS(insn->code) == BPF_LD && |
8985 | (BPF_MODE(insn->code) == BPF_ABS || | |
8986 | BPF_MODE(insn->code) == BPF_IND)) { | |
8987 | cnt = env->ops->gen_ld_abs(insn, insn_buf); | |
8988 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { | |
8989 | verbose(env, "bpf verifier is misconfigured\n"); | |
8990 | return -EINVAL; | |
8991 | } | |
8992 | ||
8993 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
8994 | if (!new_prog) | |
8995 | return -ENOMEM; | |
8996 | ||
8997 | delta += cnt - 1; | |
8998 | env->prog = prog = new_prog; | |
8999 | insn = new_prog->insnsi + i + delta; | |
9000 | continue; | |
9001 | } | |
9002 | ||
979d63d5 DB |
9003 | if (insn->code == (BPF_ALU64 | BPF_ADD | BPF_X) || |
9004 | insn->code == (BPF_ALU64 | BPF_SUB | BPF_X)) { | |
9005 | const u8 code_add = BPF_ALU64 | BPF_ADD | BPF_X; | |
9006 | const u8 code_sub = BPF_ALU64 | BPF_SUB | BPF_X; | |
9007 | struct bpf_insn insn_buf[16]; | |
9008 | struct bpf_insn *patch = &insn_buf[0]; | |
9009 | bool issrc, isneg; | |
9010 | u32 off_reg; | |
9011 | ||
9012 | aux = &env->insn_aux_data[i + delta]; | |
3612af78 DB |
9013 | if (!aux->alu_state || |
9014 | aux->alu_state == BPF_ALU_NON_POINTER) | |
979d63d5 DB |
9015 | continue; |
9016 | ||
9017 | isneg = aux->alu_state & BPF_ALU_NEG_VALUE; | |
9018 | issrc = (aux->alu_state & BPF_ALU_SANITIZE) == | |
9019 | BPF_ALU_SANITIZE_SRC; | |
9020 | ||
9021 | off_reg = issrc ? insn->src_reg : insn->dst_reg; | |
9022 | if (isneg) | |
9023 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); | |
9024 | *patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit - 1); | |
9025 | *patch++ = BPF_ALU64_REG(BPF_SUB, BPF_REG_AX, off_reg); | |
9026 | *patch++ = BPF_ALU64_REG(BPF_OR, BPF_REG_AX, off_reg); | |
9027 | *patch++ = BPF_ALU64_IMM(BPF_NEG, BPF_REG_AX, 0); | |
9028 | *patch++ = BPF_ALU64_IMM(BPF_ARSH, BPF_REG_AX, 63); | |
9029 | if (issrc) { | |
9030 | *patch++ = BPF_ALU64_REG(BPF_AND, BPF_REG_AX, | |
9031 | off_reg); | |
9032 | insn->src_reg = BPF_REG_AX; | |
9033 | } else { | |
9034 | *patch++ = BPF_ALU64_REG(BPF_AND, off_reg, | |
9035 | BPF_REG_AX); | |
9036 | } | |
9037 | if (isneg) | |
9038 | insn->code = insn->code == code_add ? | |
9039 | code_sub : code_add; | |
9040 | *patch++ = *insn; | |
9041 | if (issrc && isneg) | |
9042 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); | |
9043 | cnt = patch - insn_buf; | |
9044 | ||
9045 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
9046 | if (!new_prog) | |
9047 | return -ENOMEM; | |
9048 | ||
9049 | delta += cnt - 1; | |
9050 | env->prog = prog = new_prog; | |
9051 | insn = new_prog->insnsi + i + delta; | |
9052 | continue; | |
9053 | } | |
9054 | ||
79741b3b AS |
9055 | if (insn->code != (BPF_JMP | BPF_CALL)) |
9056 | continue; | |
cc8b0b92 AS |
9057 | if (insn->src_reg == BPF_PSEUDO_CALL) |
9058 | continue; | |
e245c5c6 | 9059 | |
79741b3b AS |
9060 | if (insn->imm == BPF_FUNC_get_route_realm) |
9061 | prog->dst_needed = 1; | |
9062 | if (insn->imm == BPF_FUNC_get_prandom_u32) | |
9063 | bpf_user_rnd_init_once(); | |
9802d865 JB |
9064 | if (insn->imm == BPF_FUNC_override_return) |
9065 | prog->kprobe_override = 1; | |
79741b3b | 9066 | if (insn->imm == BPF_FUNC_tail_call) { |
7b9f6da1 DM |
9067 | /* If we tail call into other programs, we |
9068 | * cannot make any assumptions since they can | |
9069 | * be replaced dynamically during runtime in | |
9070 | * the program array. | |
9071 | */ | |
9072 | prog->cb_access = 1; | |
80a58d02 | 9073 | env->prog->aux->stack_depth = MAX_BPF_STACK; |
e647815a | 9074 | env->prog->aux->max_pkt_offset = MAX_PACKET_OFF; |
7b9f6da1 | 9075 | |
79741b3b AS |
9076 | /* mark bpf_tail_call as different opcode to avoid |
9077 | * conditional branch in the interpeter for every normal | |
9078 | * call and to prevent accidental JITing by JIT compiler | |
9079 | * that doesn't support bpf_tail_call yet | |
e245c5c6 | 9080 | */ |
79741b3b | 9081 | insn->imm = 0; |
71189fa9 | 9082 | insn->code = BPF_JMP | BPF_TAIL_CALL; |
b2157399 | 9083 | |
c93552c4 DB |
9084 | aux = &env->insn_aux_data[i + delta]; |
9085 | if (!bpf_map_ptr_unpriv(aux)) | |
9086 | continue; | |
9087 | ||
b2157399 AS |
9088 | /* instead of changing every JIT dealing with tail_call |
9089 | * emit two extra insns: | |
9090 | * if (index >= max_entries) goto out; | |
9091 | * index &= array->index_mask; | |
9092 | * to avoid out-of-bounds cpu speculation | |
9093 | */ | |
c93552c4 | 9094 | if (bpf_map_ptr_poisoned(aux)) { |
40950343 | 9095 | verbose(env, "tail_call abusing map_ptr\n"); |
b2157399 AS |
9096 | return -EINVAL; |
9097 | } | |
c93552c4 DB |
9098 | |
9099 | map_ptr = BPF_MAP_PTR(aux->map_state); | |
b2157399 AS |
9100 | insn_buf[0] = BPF_JMP_IMM(BPF_JGE, BPF_REG_3, |
9101 | map_ptr->max_entries, 2); | |
9102 | insn_buf[1] = BPF_ALU32_IMM(BPF_AND, BPF_REG_3, | |
9103 | container_of(map_ptr, | |
9104 | struct bpf_array, | |
9105 | map)->index_mask); | |
9106 | insn_buf[2] = *insn; | |
9107 | cnt = 3; | |
9108 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
9109 | if (!new_prog) | |
9110 | return -ENOMEM; | |
9111 | ||
9112 | delta += cnt - 1; | |
9113 | env->prog = prog = new_prog; | |
9114 | insn = new_prog->insnsi + i + delta; | |
79741b3b AS |
9115 | continue; |
9116 | } | |
e245c5c6 | 9117 | |
89c63074 | 9118 | /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup |
09772d92 DB |
9119 | * and other inlining handlers are currently limited to 64 bit |
9120 | * only. | |
89c63074 | 9121 | */ |
60b58afc | 9122 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
09772d92 DB |
9123 | (insn->imm == BPF_FUNC_map_lookup_elem || |
9124 | insn->imm == BPF_FUNC_map_update_elem || | |
84430d42 DB |
9125 | insn->imm == BPF_FUNC_map_delete_elem || |
9126 | insn->imm == BPF_FUNC_map_push_elem || | |
9127 | insn->imm == BPF_FUNC_map_pop_elem || | |
9128 | insn->imm == BPF_FUNC_map_peek_elem)) { | |
c93552c4 DB |
9129 | aux = &env->insn_aux_data[i + delta]; |
9130 | if (bpf_map_ptr_poisoned(aux)) | |
9131 | goto patch_call_imm; | |
9132 | ||
9133 | map_ptr = BPF_MAP_PTR(aux->map_state); | |
09772d92 DB |
9134 | ops = map_ptr->ops; |
9135 | if (insn->imm == BPF_FUNC_map_lookup_elem && | |
9136 | ops->map_gen_lookup) { | |
9137 | cnt = ops->map_gen_lookup(map_ptr, insn_buf); | |
9138 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { | |
9139 | verbose(env, "bpf verifier is misconfigured\n"); | |
9140 | return -EINVAL; | |
9141 | } | |
81ed18ab | 9142 | |
09772d92 DB |
9143 | new_prog = bpf_patch_insn_data(env, i + delta, |
9144 | insn_buf, cnt); | |
9145 | if (!new_prog) | |
9146 | return -ENOMEM; | |
81ed18ab | 9147 | |
09772d92 DB |
9148 | delta += cnt - 1; |
9149 | env->prog = prog = new_prog; | |
9150 | insn = new_prog->insnsi + i + delta; | |
9151 | continue; | |
9152 | } | |
81ed18ab | 9153 | |
09772d92 DB |
9154 | BUILD_BUG_ON(!__same_type(ops->map_lookup_elem, |
9155 | (void *(*)(struct bpf_map *map, void *key))NULL)); | |
9156 | BUILD_BUG_ON(!__same_type(ops->map_delete_elem, | |
9157 | (int (*)(struct bpf_map *map, void *key))NULL)); | |
9158 | BUILD_BUG_ON(!__same_type(ops->map_update_elem, | |
9159 | (int (*)(struct bpf_map *map, void *key, void *value, | |
9160 | u64 flags))NULL)); | |
84430d42 DB |
9161 | BUILD_BUG_ON(!__same_type(ops->map_push_elem, |
9162 | (int (*)(struct bpf_map *map, void *value, | |
9163 | u64 flags))NULL)); | |
9164 | BUILD_BUG_ON(!__same_type(ops->map_pop_elem, | |
9165 | (int (*)(struct bpf_map *map, void *value))NULL)); | |
9166 | BUILD_BUG_ON(!__same_type(ops->map_peek_elem, | |
9167 | (int (*)(struct bpf_map *map, void *value))NULL)); | |
9168 | ||
09772d92 DB |
9169 | switch (insn->imm) { |
9170 | case BPF_FUNC_map_lookup_elem: | |
9171 | insn->imm = BPF_CAST_CALL(ops->map_lookup_elem) - | |
9172 | __bpf_call_base; | |
9173 | continue; | |
9174 | case BPF_FUNC_map_update_elem: | |
9175 | insn->imm = BPF_CAST_CALL(ops->map_update_elem) - | |
9176 | __bpf_call_base; | |
9177 | continue; | |
9178 | case BPF_FUNC_map_delete_elem: | |
9179 | insn->imm = BPF_CAST_CALL(ops->map_delete_elem) - | |
9180 | __bpf_call_base; | |
9181 | continue; | |
84430d42 DB |
9182 | case BPF_FUNC_map_push_elem: |
9183 | insn->imm = BPF_CAST_CALL(ops->map_push_elem) - | |
9184 | __bpf_call_base; | |
9185 | continue; | |
9186 | case BPF_FUNC_map_pop_elem: | |
9187 | insn->imm = BPF_CAST_CALL(ops->map_pop_elem) - | |
9188 | __bpf_call_base; | |
9189 | continue; | |
9190 | case BPF_FUNC_map_peek_elem: | |
9191 | insn->imm = BPF_CAST_CALL(ops->map_peek_elem) - | |
9192 | __bpf_call_base; | |
9193 | continue; | |
09772d92 | 9194 | } |
81ed18ab | 9195 | |
09772d92 | 9196 | goto patch_call_imm; |
81ed18ab AS |
9197 | } |
9198 | ||
9199 | patch_call_imm: | |
5e43f899 | 9200 | fn = env->ops->get_func_proto(insn->imm, env->prog); |
79741b3b AS |
9201 | /* all functions that have prototype and verifier allowed |
9202 | * programs to call them, must be real in-kernel functions | |
9203 | */ | |
9204 | if (!fn->func) { | |
61bd5218 JK |
9205 | verbose(env, |
9206 | "kernel subsystem misconfigured func %s#%d\n", | |
79741b3b AS |
9207 | func_id_name(insn->imm), insn->imm); |
9208 | return -EFAULT; | |
e245c5c6 | 9209 | } |
79741b3b | 9210 | insn->imm = fn->func - __bpf_call_base; |
e245c5c6 | 9211 | } |
e245c5c6 | 9212 | |
79741b3b AS |
9213 | return 0; |
9214 | } | |
e245c5c6 | 9215 | |
58e2af8b | 9216 | static void free_states(struct bpf_verifier_env *env) |
f1bca824 | 9217 | { |
58e2af8b | 9218 | struct bpf_verifier_state_list *sl, *sln; |
f1bca824 AS |
9219 | int i; |
9220 | ||
9f4686c4 AS |
9221 | sl = env->free_list; |
9222 | while (sl) { | |
9223 | sln = sl->next; | |
9224 | free_verifier_state(&sl->state, false); | |
9225 | kfree(sl); | |
9226 | sl = sln; | |
9227 | } | |
9228 | ||
f1bca824 AS |
9229 | if (!env->explored_states) |
9230 | return; | |
9231 | ||
dc2a4ebc | 9232 | for (i = 0; i < state_htab_size(env); i++) { |
f1bca824 AS |
9233 | sl = env->explored_states[i]; |
9234 | ||
a8f500af AS |
9235 | while (sl) { |
9236 | sln = sl->next; | |
9237 | free_verifier_state(&sl->state, false); | |
9238 | kfree(sl); | |
9239 | sl = sln; | |
9240 | } | |
f1bca824 AS |
9241 | } |
9242 | ||
71dde681 | 9243 | kvfree(env->explored_states); |
f1bca824 AS |
9244 | } |
9245 | ||
06ee7115 AS |
9246 | static void print_verification_stats(struct bpf_verifier_env *env) |
9247 | { | |
9248 | int i; | |
9249 | ||
9250 | if (env->log.level & BPF_LOG_STATS) { | |
9251 | verbose(env, "verification time %lld usec\n", | |
9252 | div_u64(env->verification_time, 1000)); | |
9253 | verbose(env, "stack depth "); | |
9254 | for (i = 0; i < env->subprog_cnt; i++) { | |
9255 | u32 depth = env->subprog_info[i].stack_depth; | |
9256 | ||
9257 | verbose(env, "%d", depth); | |
9258 | if (i + 1 < env->subprog_cnt) | |
9259 | verbose(env, "+"); | |
9260 | } | |
9261 | verbose(env, "\n"); | |
9262 | } | |
9263 | verbose(env, "processed %d insns (limit %d) max_states_per_insn %d " | |
9264 | "total_states %d peak_states %d mark_read %d\n", | |
9265 | env->insn_processed, BPF_COMPLEXITY_LIMIT_INSNS, | |
9266 | env->max_states_per_insn, env->total_states, | |
9267 | env->peak_states, env->longest_mark_read_walk); | |
f1bca824 AS |
9268 | } |
9269 | ||
838e9690 YS |
9270 | int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, |
9271 | union bpf_attr __user *uattr) | |
51580e79 | 9272 | { |
06ee7115 | 9273 | u64 start_time = ktime_get_ns(); |
58e2af8b | 9274 | struct bpf_verifier_env *env; |
b9193c1b | 9275 | struct bpf_verifier_log *log; |
9e4c24e7 | 9276 | int i, len, ret = -EINVAL; |
e2ae4ca2 | 9277 | bool is_priv; |
51580e79 | 9278 | |
eba0c929 AB |
9279 | /* no program is valid */ |
9280 | if (ARRAY_SIZE(bpf_verifier_ops) == 0) | |
9281 | return -EINVAL; | |
9282 | ||
58e2af8b | 9283 | /* 'struct bpf_verifier_env' can be global, but since it's not small, |
cbd35700 AS |
9284 | * allocate/free it every time bpf_check() is called |
9285 | */ | |
58e2af8b | 9286 | env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL); |
cbd35700 AS |
9287 | if (!env) |
9288 | return -ENOMEM; | |
61bd5218 | 9289 | log = &env->log; |
cbd35700 | 9290 | |
9e4c24e7 | 9291 | len = (*prog)->len; |
fad953ce | 9292 | env->insn_aux_data = |
9e4c24e7 | 9293 | vzalloc(array_size(sizeof(struct bpf_insn_aux_data), len)); |
3df126f3 JK |
9294 | ret = -ENOMEM; |
9295 | if (!env->insn_aux_data) | |
9296 | goto err_free_env; | |
9e4c24e7 JK |
9297 | for (i = 0; i < len; i++) |
9298 | env->insn_aux_data[i].orig_idx = i; | |
9bac3d6d | 9299 | env->prog = *prog; |
00176a34 | 9300 | env->ops = bpf_verifier_ops[env->prog->type]; |
45a73c17 | 9301 | is_priv = capable(CAP_SYS_ADMIN); |
0246e64d | 9302 | |
8580ac94 AS |
9303 | if (!btf_vmlinux && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) { |
9304 | mutex_lock(&bpf_verifier_lock); | |
9305 | if (!btf_vmlinux) | |
9306 | btf_vmlinux = btf_parse_vmlinux(); | |
9307 | mutex_unlock(&bpf_verifier_lock); | |
9308 | } | |
9309 | ||
cbd35700 | 9310 | /* grab the mutex to protect few globals used by verifier */ |
45a73c17 AS |
9311 | if (!is_priv) |
9312 | mutex_lock(&bpf_verifier_lock); | |
cbd35700 AS |
9313 | |
9314 | if (attr->log_level || attr->log_buf || attr->log_size) { | |
9315 | /* user requested verbose verifier output | |
9316 | * and supplied buffer to store the verification trace | |
9317 | */ | |
e7bf8249 JK |
9318 | log->level = attr->log_level; |
9319 | log->ubuf = (char __user *) (unsigned long) attr->log_buf; | |
9320 | log->len_total = attr->log_size; | |
cbd35700 AS |
9321 | |
9322 | ret = -EINVAL; | |
e7bf8249 | 9323 | /* log attributes have to be sane */ |
7a9f5c65 | 9324 | if (log->len_total < 128 || log->len_total > UINT_MAX >> 2 || |
06ee7115 | 9325 | !log->level || !log->ubuf || log->level & ~BPF_LOG_MASK) |
3df126f3 | 9326 | goto err_unlock; |
cbd35700 | 9327 | } |
1ad2f583 | 9328 | |
8580ac94 AS |
9329 | if (IS_ERR(btf_vmlinux)) { |
9330 | /* Either gcc or pahole or kernel are broken. */ | |
9331 | verbose(env, "in-kernel BTF is malformed\n"); | |
9332 | ret = PTR_ERR(btf_vmlinux); | |
9333 | goto err_unlock; | |
9334 | } | |
9335 | ||
1ad2f583 DB |
9336 | env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT); |
9337 | if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) | |
e07b98d9 | 9338 | env->strict_alignment = true; |
e9ee9efc DM |
9339 | if (attr->prog_flags & BPF_F_ANY_ALIGNMENT) |
9340 | env->strict_alignment = false; | |
cbd35700 | 9341 | |
e2ae4ca2 JK |
9342 | env->allow_ptr_leaks = is_priv; |
9343 | ||
10d274e8 AS |
9344 | if (is_priv) |
9345 | env->test_state_freq = attr->prog_flags & BPF_F_TEST_STATE_FREQ; | |
9346 | ||
f4e3ec0d JK |
9347 | ret = replace_map_fd_with_map_ptr(env); |
9348 | if (ret < 0) | |
9349 | goto skip_full_check; | |
9350 | ||
cae1927c | 9351 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
a40a2632 | 9352 | ret = bpf_prog_offload_verifier_prep(env->prog); |
ab3f0063 | 9353 | if (ret) |
f4e3ec0d | 9354 | goto skip_full_check; |
ab3f0063 JK |
9355 | } |
9356 | ||
dc2a4ebc | 9357 | env->explored_states = kvcalloc(state_htab_size(env), |
58e2af8b | 9358 | sizeof(struct bpf_verifier_state_list *), |
f1bca824 AS |
9359 | GFP_USER); |
9360 | ret = -ENOMEM; | |
9361 | if (!env->explored_states) | |
9362 | goto skip_full_check; | |
9363 | ||
d9762e84 | 9364 | ret = check_subprogs(env); |
475fb78f AS |
9365 | if (ret < 0) |
9366 | goto skip_full_check; | |
9367 | ||
c454a46b | 9368 | ret = check_btf_info(env, attr, uattr); |
838e9690 YS |
9369 | if (ret < 0) |
9370 | goto skip_full_check; | |
9371 | ||
d9762e84 MKL |
9372 | ret = check_cfg(env); |
9373 | if (ret < 0) | |
9374 | goto skip_full_check; | |
9375 | ||
17a52670 | 9376 | ret = do_check(env); |
8c01c4f8 CG |
9377 | if (env->cur_state) { |
9378 | free_verifier_state(env->cur_state, true); | |
9379 | env->cur_state = NULL; | |
9380 | } | |
cbd35700 | 9381 | |
c941ce9c QM |
9382 | if (ret == 0 && bpf_prog_is_dev_bound(env->prog->aux)) |
9383 | ret = bpf_prog_offload_finalize(env); | |
9384 | ||
0246e64d | 9385 | skip_full_check: |
638f5b90 | 9386 | while (!pop_stack(env, NULL, NULL)); |
f1bca824 | 9387 | free_states(env); |
0246e64d | 9388 | |
c131187d | 9389 | if (ret == 0) |
9b38c405 | 9390 | ret = check_max_stack_depth(env); |
c131187d | 9391 | |
9b38c405 | 9392 | /* instruction rewrites happen after this point */ |
e2ae4ca2 JK |
9393 | if (is_priv) { |
9394 | if (ret == 0) | |
9395 | opt_hard_wire_dead_code_branches(env); | |
52875a04 JK |
9396 | if (ret == 0) |
9397 | ret = opt_remove_dead_code(env); | |
a1b14abc JK |
9398 | if (ret == 0) |
9399 | ret = opt_remove_nops(env); | |
52875a04 JK |
9400 | } else { |
9401 | if (ret == 0) | |
9402 | sanitize_dead_code(env); | |
e2ae4ca2 JK |
9403 | } |
9404 | ||
9bac3d6d AS |
9405 | if (ret == 0) |
9406 | /* program is valid, convert *(u32*)(ctx + off) accesses */ | |
9407 | ret = convert_ctx_accesses(env); | |
9408 | ||
e245c5c6 | 9409 | if (ret == 0) |
79741b3b | 9410 | ret = fixup_bpf_calls(env); |
e245c5c6 | 9411 | |
a4b1d3c1 JW |
9412 | /* do 32-bit optimization after insn patching has done so those patched |
9413 | * insns could be handled correctly. | |
9414 | */ | |
d6c2308c JW |
9415 | if (ret == 0 && !bpf_prog_is_dev_bound(env->prog->aux)) { |
9416 | ret = opt_subreg_zext_lo32_rnd_hi32(env, attr); | |
9417 | env->prog->aux->verifier_zext = bpf_jit_needs_zext() ? !ret | |
9418 | : false; | |
a4b1d3c1 JW |
9419 | } |
9420 | ||
1ea47e01 AS |
9421 | if (ret == 0) |
9422 | ret = fixup_call_args(env); | |
9423 | ||
06ee7115 AS |
9424 | env->verification_time = ktime_get_ns() - start_time; |
9425 | print_verification_stats(env); | |
9426 | ||
a2a7d570 | 9427 | if (log->level && bpf_verifier_log_full(log)) |
cbd35700 | 9428 | ret = -ENOSPC; |
a2a7d570 | 9429 | if (log->level && !log->ubuf) { |
cbd35700 | 9430 | ret = -EFAULT; |
a2a7d570 | 9431 | goto err_release_maps; |
cbd35700 AS |
9432 | } |
9433 | ||
0246e64d AS |
9434 | if (ret == 0 && env->used_map_cnt) { |
9435 | /* if program passed verifier, update used_maps in bpf_prog_info */ | |
9bac3d6d AS |
9436 | env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt, |
9437 | sizeof(env->used_maps[0]), | |
9438 | GFP_KERNEL); | |
0246e64d | 9439 | |
9bac3d6d | 9440 | if (!env->prog->aux->used_maps) { |
0246e64d | 9441 | ret = -ENOMEM; |
a2a7d570 | 9442 | goto err_release_maps; |
0246e64d AS |
9443 | } |
9444 | ||
9bac3d6d | 9445 | memcpy(env->prog->aux->used_maps, env->used_maps, |
0246e64d | 9446 | sizeof(env->used_maps[0]) * env->used_map_cnt); |
9bac3d6d | 9447 | env->prog->aux->used_map_cnt = env->used_map_cnt; |
0246e64d AS |
9448 | |
9449 | /* program is valid. Convert pseudo bpf_ld_imm64 into generic | |
9450 | * bpf_ld_imm64 instructions | |
9451 | */ | |
9452 | convert_pseudo_ld_imm64(env); | |
9453 | } | |
cbd35700 | 9454 | |
ba64e7d8 YS |
9455 | if (ret == 0) |
9456 | adjust_btf_func(env); | |
9457 | ||
a2a7d570 | 9458 | err_release_maps: |
9bac3d6d | 9459 | if (!env->prog->aux->used_maps) |
0246e64d | 9460 | /* if we didn't copy map pointers into bpf_prog_info, release |
ab7f5bf0 | 9461 | * them now. Otherwise free_used_maps() will release them. |
0246e64d AS |
9462 | */ |
9463 | release_maps(env); | |
9bac3d6d | 9464 | *prog = env->prog; |
3df126f3 | 9465 | err_unlock: |
45a73c17 AS |
9466 | if (!is_priv) |
9467 | mutex_unlock(&bpf_verifier_lock); | |
3df126f3 JK |
9468 | vfree(env->insn_aux_data); |
9469 | err_free_env: | |
9470 | kfree(env); | |
51580e79 AS |
9471 | return ret; |
9472 | } |