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 | ||
cbd35700 AS |
210 | static DEFINE_MUTEX(bpf_verifier_lock); |
211 | ||
d9762e84 MKL |
212 | static const struct bpf_line_info * |
213 | find_linfo(const struct bpf_verifier_env *env, u32 insn_off) | |
214 | { | |
215 | const struct bpf_line_info *linfo; | |
216 | const struct bpf_prog *prog; | |
217 | u32 i, nr_linfo; | |
218 | ||
219 | prog = env->prog; | |
220 | nr_linfo = prog->aux->nr_linfo; | |
221 | ||
222 | if (!nr_linfo || insn_off >= prog->len) | |
223 | return NULL; | |
224 | ||
225 | linfo = prog->aux->linfo; | |
226 | for (i = 1; i < nr_linfo; i++) | |
227 | if (insn_off < linfo[i].insn_off) | |
228 | break; | |
229 | ||
230 | return &linfo[i - 1]; | |
231 | } | |
232 | ||
77d2e05a MKL |
233 | void bpf_verifier_vlog(struct bpf_verifier_log *log, const char *fmt, |
234 | va_list args) | |
cbd35700 | 235 | { |
a2a7d570 | 236 | unsigned int n; |
cbd35700 | 237 | |
a2a7d570 | 238 | n = vscnprintf(log->kbuf, BPF_VERIFIER_TMP_LOG_SIZE, fmt, args); |
a2a7d570 JK |
239 | |
240 | WARN_ONCE(n >= BPF_VERIFIER_TMP_LOG_SIZE - 1, | |
241 | "verifier log line truncated - local buffer too short\n"); | |
242 | ||
243 | n = min(log->len_total - log->len_used - 1, n); | |
244 | log->kbuf[n] = '\0'; | |
245 | ||
246 | if (!copy_to_user(log->ubuf + log->len_used, log->kbuf, n + 1)) | |
247 | log->len_used += n; | |
248 | else | |
249 | log->ubuf = NULL; | |
cbd35700 | 250 | } |
abe08840 JO |
251 | |
252 | /* log_level controls verbosity level of eBPF verifier. | |
253 | * bpf_verifier_log_write() is used to dump the verification trace to the log, | |
254 | * so the user can figure out what's wrong with the program | |
430e68d1 | 255 | */ |
abe08840 JO |
256 | __printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env, |
257 | const char *fmt, ...) | |
258 | { | |
259 | va_list args; | |
260 | ||
77d2e05a MKL |
261 | if (!bpf_verifier_log_needed(&env->log)) |
262 | return; | |
263 | ||
abe08840 | 264 | va_start(args, fmt); |
77d2e05a | 265 | bpf_verifier_vlog(&env->log, fmt, args); |
abe08840 JO |
266 | va_end(args); |
267 | } | |
268 | EXPORT_SYMBOL_GPL(bpf_verifier_log_write); | |
269 | ||
270 | __printf(2, 3) static void verbose(void *private_data, const char *fmt, ...) | |
271 | { | |
77d2e05a | 272 | struct bpf_verifier_env *env = private_data; |
abe08840 JO |
273 | va_list args; |
274 | ||
77d2e05a MKL |
275 | if (!bpf_verifier_log_needed(&env->log)) |
276 | return; | |
277 | ||
abe08840 | 278 | va_start(args, fmt); |
77d2e05a | 279 | bpf_verifier_vlog(&env->log, fmt, args); |
abe08840 JO |
280 | va_end(args); |
281 | } | |
cbd35700 | 282 | |
d9762e84 MKL |
283 | static const char *ltrim(const char *s) |
284 | { | |
285 | while (isspace(*s)) | |
286 | s++; | |
287 | ||
288 | return s; | |
289 | } | |
290 | ||
291 | __printf(3, 4) static void verbose_linfo(struct bpf_verifier_env *env, | |
292 | u32 insn_off, | |
293 | const char *prefix_fmt, ...) | |
294 | { | |
295 | const struct bpf_line_info *linfo; | |
296 | ||
297 | if (!bpf_verifier_log_needed(&env->log)) | |
298 | return; | |
299 | ||
300 | linfo = find_linfo(env, insn_off); | |
301 | if (!linfo || linfo == env->prev_linfo) | |
302 | return; | |
303 | ||
304 | if (prefix_fmt) { | |
305 | va_list args; | |
306 | ||
307 | va_start(args, prefix_fmt); | |
308 | bpf_verifier_vlog(&env->log, prefix_fmt, args); | |
309 | va_end(args); | |
310 | } | |
311 | ||
312 | verbose(env, "%s\n", | |
313 | ltrim(btf_name_by_offset(env->prog->aux->btf, | |
314 | linfo->line_off))); | |
315 | ||
316 | env->prev_linfo = linfo; | |
317 | } | |
318 | ||
de8f3a83 DB |
319 | static bool type_is_pkt_pointer(enum bpf_reg_type type) |
320 | { | |
321 | return type == PTR_TO_PACKET || | |
322 | type == PTR_TO_PACKET_META; | |
323 | } | |
324 | ||
46f8bc92 MKL |
325 | static bool type_is_sk_pointer(enum bpf_reg_type type) |
326 | { | |
327 | return type == PTR_TO_SOCKET || | |
655a51e5 | 328 | type == PTR_TO_SOCK_COMMON || |
fada7fdc JL |
329 | type == PTR_TO_TCP_SOCK || |
330 | type == PTR_TO_XDP_SOCK; | |
46f8bc92 MKL |
331 | } |
332 | ||
840b9615 JS |
333 | static bool reg_type_may_be_null(enum bpf_reg_type type) |
334 | { | |
fd978bf7 | 335 | return type == PTR_TO_MAP_VALUE_OR_NULL || |
46f8bc92 | 336 | type == PTR_TO_SOCKET_OR_NULL || |
655a51e5 MKL |
337 | type == PTR_TO_SOCK_COMMON_OR_NULL || |
338 | type == PTR_TO_TCP_SOCK_OR_NULL; | |
fd978bf7 JS |
339 | } |
340 | ||
d83525ca AS |
341 | static bool reg_may_point_to_spin_lock(const struct bpf_reg_state *reg) |
342 | { | |
343 | return reg->type == PTR_TO_MAP_VALUE && | |
344 | map_value_has_spin_lock(reg->map_ptr); | |
345 | } | |
346 | ||
cba368c1 MKL |
347 | static bool reg_type_may_be_refcounted_or_null(enum bpf_reg_type type) |
348 | { | |
349 | return type == PTR_TO_SOCKET || | |
350 | type == PTR_TO_SOCKET_OR_NULL || | |
351 | type == PTR_TO_TCP_SOCK || | |
352 | type == PTR_TO_TCP_SOCK_OR_NULL; | |
353 | } | |
354 | ||
1b986589 | 355 | static bool arg_type_may_be_refcounted(enum bpf_arg_type type) |
fd978bf7 | 356 | { |
1b986589 | 357 | return type == ARG_PTR_TO_SOCK_COMMON; |
fd978bf7 JS |
358 | } |
359 | ||
360 | /* Determine whether the function releases some resources allocated by another | |
361 | * function call. The first reference type argument will be assumed to be | |
362 | * released by release_reference(). | |
363 | */ | |
364 | static bool is_release_function(enum bpf_func_id func_id) | |
365 | { | |
6acc9b43 | 366 | return func_id == BPF_FUNC_sk_release; |
840b9615 JS |
367 | } |
368 | ||
46f8bc92 MKL |
369 | static bool is_acquire_function(enum bpf_func_id func_id) |
370 | { | |
371 | return func_id == BPF_FUNC_sk_lookup_tcp || | |
edbf8c01 LB |
372 | func_id == BPF_FUNC_sk_lookup_udp || |
373 | func_id == BPF_FUNC_skc_lookup_tcp; | |
46f8bc92 MKL |
374 | } |
375 | ||
1b986589 MKL |
376 | static bool is_ptr_cast_function(enum bpf_func_id func_id) |
377 | { | |
378 | return func_id == BPF_FUNC_tcp_sock || | |
379 | func_id == BPF_FUNC_sk_fullsock; | |
380 | } | |
381 | ||
17a52670 AS |
382 | /* string representation of 'enum bpf_reg_type' */ |
383 | static const char * const reg_type_str[] = { | |
384 | [NOT_INIT] = "?", | |
f1174f77 | 385 | [SCALAR_VALUE] = "inv", |
17a52670 AS |
386 | [PTR_TO_CTX] = "ctx", |
387 | [CONST_PTR_TO_MAP] = "map_ptr", | |
388 | [PTR_TO_MAP_VALUE] = "map_value", | |
389 | [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null", | |
17a52670 | 390 | [PTR_TO_STACK] = "fp", |
969bf05e | 391 | [PTR_TO_PACKET] = "pkt", |
de8f3a83 | 392 | [PTR_TO_PACKET_META] = "pkt_meta", |
969bf05e | 393 | [PTR_TO_PACKET_END] = "pkt_end", |
d58e468b | 394 | [PTR_TO_FLOW_KEYS] = "flow_keys", |
c64b7983 JS |
395 | [PTR_TO_SOCKET] = "sock", |
396 | [PTR_TO_SOCKET_OR_NULL] = "sock_or_null", | |
46f8bc92 MKL |
397 | [PTR_TO_SOCK_COMMON] = "sock_common", |
398 | [PTR_TO_SOCK_COMMON_OR_NULL] = "sock_common_or_null", | |
655a51e5 MKL |
399 | [PTR_TO_TCP_SOCK] = "tcp_sock", |
400 | [PTR_TO_TCP_SOCK_OR_NULL] = "tcp_sock_or_null", | |
9df1c28b | 401 | [PTR_TO_TP_BUFFER] = "tp_buffer", |
fada7fdc | 402 | [PTR_TO_XDP_SOCK] = "xdp_sock", |
17a52670 AS |
403 | }; |
404 | ||
8efea21d EC |
405 | static char slot_type_char[] = { |
406 | [STACK_INVALID] = '?', | |
407 | [STACK_SPILL] = 'r', | |
408 | [STACK_MISC] = 'm', | |
409 | [STACK_ZERO] = '0', | |
410 | }; | |
411 | ||
4e92024a AS |
412 | static void print_liveness(struct bpf_verifier_env *env, |
413 | enum bpf_reg_liveness live) | |
414 | { | |
9242b5f5 | 415 | if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN | REG_LIVE_DONE)) |
4e92024a AS |
416 | verbose(env, "_"); |
417 | if (live & REG_LIVE_READ) | |
418 | verbose(env, "r"); | |
419 | if (live & REG_LIVE_WRITTEN) | |
420 | verbose(env, "w"); | |
9242b5f5 AS |
421 | if (live & REG_LIVE_DONE) |
422 | verbose(env, "D"); | |
4e92024a AS |
423 | } |
424 | ||
f4d7e40a AS |
425 | static struct bpf_func_state *func(struct bpf_verifier_env *env, |
426 | const struct bpf_reg_state *reg) | |
427 | { | |
428 | struct bpf_verifier_state *cur = env->cur_state; | |
429 | ||
430 | return cur->frame[reg->frameno]; | |
431 | } | |
432 | ||
61bd5218 | 433 | static void print_verifier_state(struct bpf_verifier_env *env, |
f4d7e40a | 434 | const struct bpf_func_state *state) |
17a52670 | 435 | { |
f4d7e40a | 436 | const struct bpf_reg_state *reg; |
17a52670 AS |
437 | enum bpf_reg_type t; |
438 | int i; | |
439 | ||
f4d7e40a AS |
440 | if (state->frameno) |
441 | verbose(env, " frame%d:", state->frameno); | |
17a52670 | 442 | for (i = 0; i < MAX_BPF_REG; i++) { |
1a0dc1ac AS |
443 | reg = &state->regs[i]; |
444 | t = reg->type; | |
17a52670 AS |
445 | if (t == NOT_INIT) |
446 | continue; | |
4e92024a AS |
447 | verbose(env, " R%d", i); |
448 | print_liveness(env, reg->live); | |
449 | verbose(env, "=%s", reg_type_str[t]); | |
b5dc0163 AS |
450 | if (t == SCALAR_VALUE && reg->precise) |
451 | verbose(env, "P"); | |
f1174f77 EC |
452 | if ((t == SCALAR_VALUE || t == PTR_TO_STACK) && |
453 | tnum_is_const(reg->var_off)) { | |
454 | /* reg->off should be 0 for SCALAR_VALUE */ | |
61bd5218 | 455 | verbose(env, "%lld", reg->var_off.value + reg->off); |
f1174f77 | 456 | } else { |
cba368c1 MKL |
457 | verbose(env, "(id=%d", reg->id); |
458 | if (reg_type_may_be_refcounted_or_null(t)) | |
459 | verbose(env, ",ref_obj_id=%d", reg->ref_obj_id); | |
f1174f77 | 460 | if (t != SCALAR_VALUE) |
61bd5218 | 461 | verbose(env, ",off=%d", reg->off); |
de8f3a83 | 462 | if (type_is_pkt_pointer(t)) |
61bd5218 | 463 | verbose(env, ",r=%d", reg->range); |
f1174f77 EC |
464 | else if (t == CONST_PTR_TO_MAP || |
465 | t == PTR_TO_MAP_VALUE || | |
466 | t == PTR_TO_MAP_VALUE_OR_NULL) | |
61bd5218 | 467 | verbose(env, ",ks=%d,vs=%d", |
f1174f77 EC |
468 | reg->map_ptr->key_size, |
469 | reg->map_ptr->value_size); | |
7d1238f2 EC |
470 | if (tnum_is_const(reg->var_off)) { |
471 | /* Typically an immediate SCALAR_VALUE, but | |
472 | * could be a pointer whose offset is too big | |
473 | * for reg->off | |
474 | */ | |
61bd5218 | 475 | verbose(env, ",imm=%llx", reg->var_off.value); |
7d1238f2 EC |
476 | } else { |
477 | if (reg->smin_value != reg->umin_value && | |
478 | reg->smin_value != S64_MIN) | |
61bd5218 | 479 | verbose(env, ",smin_value=%lld", |
7d1238f2 EC |
480 | (long long)reg->smin_value); |
481 | if (reg->smax_value != reg->umax_value && | |
482 | reg->smax_value != S64_MAX) | |
61bd5218 | 483 | verbose(env, ",smax_value=%lld", |
7d1238f2 EC |
484 | (long long)reg->smax_value); |
485 | if (reg->umin_value != 0) | |
61bd5218 | 486 | verbose(env, ",umin_value=%llu", |
7d1238f2 EC |
487 | (unsigned long long)reg->umin_value); |
488 | if (reg->umax_value != U64_MAX) | |
61bd5218 | 489 | verbose(env, ",umax_value=%llu", |
7d1238f2 EC |
490 | (unsigned long long)reg->umax_value); |
491 | if (!tnum_is_unknown(reg->var_off)) { | |
492 | char tn_buf[48]; | |
f1174f77 | 493 | |
7d1238f2 | 494 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
61bd5218 | 495 | verbose(env, ",var_off=%s", tn_buf); |
7d1238f2 | 496 | } |
f1174f77 | 497 | } |
61bd5218 | 498 | verbose(env, ")"); |
f1174f77 | 499 | } |
17a52670 | 500 | } |
638f5b90 | 501 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
8efea21d EC |
502 | char types_buf[BPF_REG_SIZE + 1]; |
503 | bool valid = false; | |
504 | int j; | |
505 | ||
506 | for (j = 0; j < BPF_REG_SIZE; j++) { | |
507 | if (state->stack[i].slot_type[j] != STACK_INVALID) | |
508 | valid = true; | |
509 | types_buf[j] = slot_type_char[ | |
510 | state->stack[i].slot_type[j]]; | |
511 | } | |
512 | types_buf[BPF_REG_SIZE] = 0; | |
513 | if (!valid) | |
514 | continue; | |
515 | verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE); | |
516 | print_liveness(env, state->stack[i].spilled_ptr.live); | |
b5dc0163 AS |
517 | if (state->stack[i].slot_type[0] == STACK_SPILL) { |
518 | reg = &state->stack[i].spilled_ptr; | |
519 | t = reg->type; | |
520 | verbose(env, "=%s", reg_type_str[t]); | |
521 | if (t == SCALAR_VALUE && reg->precise) | |
522 | verbose(env, "P"); | |
523 | if (t == SCALAR_VALUE && tnum_is_const(reg->var_off)) | |
524 | verbose(env, "%lld", reg->var_off.value + reg->off); | |
525 | } else { | |
8efea21d | 526 | verbose(env, "=%s", types_buf); |
b5dc0163 | 527 | } |
17a52670 | 528 | } |
fd978bf7 JS |
529 | if (state->acquired_refs && state->refs[0].id) { |
530 | verbose(env, " refs=%d", state->refs[0].id); | |
531 | for (i = 1; i < state->acquired_refs; i++) | |
532 | if (state->refs[i].id) | |
533 | verbose(env, ",%d", state->refs[i].id); | |
534 | } | |
61bd5218 | 535 | verbose(env, "\n"); |
17a52670 AS |
536 | } |
537 | ||
84dbf350 JS |
538 | #define COPY_STATE_FN(NAME, COUNT, FIELD, SIZE) \ |
539 | static int copy_##NAME##_state(struct bpf_func_state *dst, \ | |
540 | const struct bpf_func_state *src) \ | |
541 | { \ | |
542 | if (!src->FIELD) \ | |
543 | return 0; \ | |
544 | if (WARN_ON_ONCE(dst->COUNT < src->COUNT)) { \ | |
545 | /* internal bug, make state invalid to reject the program */ \ | |
546 | memset(dst, 0, sizeof(*dst)); \ | |
547 | return -EFAULT; \ | |
548 | } \ | |
549 | memcpy(dst->FIELD, src->FIELD, \ | |
550 | sizeof(*src->FIELD) * (src->COUNT / SIZE)); \ | |
551 | return 0; \ | |
638f5b90 | 552 | } |
fd978bf7 JS |
553 | /* copy_reference_state() */ |
554 | COPY_STATE_FN(reference, acquired_refs, refs, 1) | |
84dbf350 JS |
555 | /* copy_stack_state() */ |
556 | COPY_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE) | |
557 | #undef COPY_STATE_FN | |
558 | ||
559 | #define REALLOC_STATE_FN(NAME, COUNT, FIELD, SIZE) \ | |
560 | static int realloc_##NAME##_state(struct bpf_func_state *state, int size, \ | |
561 | bool copy_old) \ | |
562 | { \ | |
563 | u32 old_size = state->COUNT; \ | |
564 | struct bpf_##NAME##_state *new_##FIELD; \ | |
565 | int slot = size / SIZE; \ | |
566 | \ | |
567 | if (size <= old_size || !size) { \ | |
568 | if (copy_old) \ | |
569 | return 0; \ | |
570 | state->COUNT = slot * SIZE; \ | |
571 | if (!size && old_size) { \ | |
572 | kfree(state->FIELD); \ | |
573 | state->FIELD = NULL; \ | |
574 | } \ | |
575 | return 0; \ | |
576 | } \ | |
577 | new_##FIELD = kmalloc_array(slot, sizeof(struct bpf_##NAME##_state), \ | |
578 | GFP_KERNEL); \ | |
579 | if (!new_##FIELD) \ | |
580 | return -ENOMEM; \ | |
581 | if (copy_old) { \ | |
582 | if (state->FIELD) \ | |
583 | memcpy(new_##FIELD, state->FIELD, \ | |
584 | sizeof(*new_##FIELD) * (old_size / SIZE)); \ | |
585 | memset(new_##FIELD + old_size / SIZE, 0, \ | |
586 | sizeof(*new_##FIELD) * (size - old_size) / SIZE); \ | |
587 | } \ | |
588 | state->COUNT = slot * SIZE; \ | |
589 | kfree(state->FIELD); \ | |
590 | state->FIELD = new_##FIELD; \ | |
591 | return 0; \ | |
592 | } | |
fd978bf7 JS |
593 | /* realloc_reference_state() */ |
594 | REALLOC_STATE_FN(reference, acquired_refs, refs, 1) | |
84dbf350 JS |
595 | /* realloc_stack_state() */ |
596 | REALLOC_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE) | |
597 | #undef REALLOC_STATE_FN | |
638f5b90 AS |
598 | |
599 | /* do_check() starts with zero-sized stack in struct bpf_verifier_state to | |
600 | * make it consume minimal amount of memory. check_stack_write() access from | |
f4d7e40a | 601 | * the program calls into realloc_func_state() to grow the stack size. |
84dbf350 JS |
602 | * Note there is a non-zero 'parent' pointer inside bpf_verifier_state |
603 | * which realloc_stack_state() copies over. It points to previous | |
604 | * bpf_verifier_state which is never reallocated. | |
638f5b90 | 605 | */ |
fd978bf7 JS |
606 | static int realloc_func_state(struct bpf_func_state *state, int stack_size, |
607 | int refs_size, bool copy_old) | |
638f5b90 | 608 | { |
fd978bf7 JS |
609 | int err = realloc_reference_state(state, refs_size, copy_old); |
610 | if (err) | |
611 | return err; | |
612 | return realloc_stack_state(state, stack_size, copy_old); | |
613 | } | |
614 | ||
615 | /* Acquire a pointer id from the env and update the state->refs to include | |
616 | * this new pointer reference. | |
617 | * On success, returns a valid pointer id to associate with the register | |
618 | * On failure, returns a negative errno. | |
638f5b90 | 619 | */ |
fd978bf7 | 620 | static int acquire_reference_state(struct bpf_verifier_env *env, int insn_idx) |
638f5b90 | 621 | { |
fd978bf7 JS |
622 | struct bpf_func_state *state = cur_func(env); |
623 | int new_ofs = state->acquired_refs; | |
624 | int id, err; | |
625 | ||
626 | err = realloc_reference_state(state, state->acquired_refs + 1, true); | |
627 | if (err) | |
628 | return err; | |
629 | id = ++env->id_gen; | |
630 | state->refs[new_ofs].id = id; | |
631 | state->refs[new_ofs].insn_idx = insn_idx; | |
638f5b90 | 632 | |
fd978bf7 JS |
633 | return id; |
634 | } | |
635 | ||
636 | /* release function corresponding to acquire_reference_state(). Idempotent. */ | |
46f8bc92 | 637 | static int release_reference_state(struct bpf_func_state *state, int ptr_id) |
fd978bf7 JS |
638 | { |
639 | int i, last_idx; | |
640 | ||
fd978bf7 JS |
641 | last_idx = state->acquired_refs - 1; |
642 | for (i = 0; i < state->acquired_refs; i++) { | |
643 | if (state->refs[i].id == ptr_id) { | |
644 | if (last_idx && i != last_idx) | |
645 | memcpy(&state->refs[i], &state->refs[last_idx], | |
646 | sizeof(*state->refs)); | |
647 | memset(&state->refs[last_idx], 0, sizeof(*state->refs)); | |
648 | state->acquired_refs--; | |
638f5b90 | 649 | return 0; |
638f5b90 | 650 | } |
638f5b90 | 651 | } |
46f8bc92 | 652 | return -EINVAL; |
fd978bf7 JS |
653 | } |
654 | ||
655 | static int transfer_reference_state(struct bpf_func_state *dst, | |
656 | struct bpf_func_state *src) | |
657 | { | |
658 | int err = realloc_reference_state(dst, src->acquired_refs, false); | |
659 | if (err) | |
660 | return err; | |
661 | err = copy_reference_state(dst, src); | |
662 | if (err) | |
663 | return err; | |
638f5b90 AS |
664 | return 0; |
665 | } | |
666 | ||
f4d7e40a AS |
667 | static void free_func_state(struct bpf_func_state *state) |
668 | { | |
5896351e AS |
669 | if (!state) |
670 | return; | |
fd978bf7 | 671 | kfree(state->refs); |
f4d7e40a AS |
672 | kfree(state->stack); |
673 | kfree(state); | |
674 | } | |
675 | ||
b5dc0163 AS |
676 | static void clear_jmp_history(struct bpf_verifier_state *state) |
677 | { | |
678 | kfree(state->jmp_history); | |
679 | state->jmp_history = NULL; | |
680 | state->jmp_history_cnt = 0; | |
681 | } | |
682 | ||
1969db47 AS |
683 | static void free_verifier_state(struct bpf_verifier_state *state, |
684 | bool free_self) | |
638f5b90 | 685 | { |
f4d7e40a AS |
686 | int i; |
687 | ||
688 | for (i = 0; i <= state->curframe; i++) { | |
689 | free_func_state(state->frame[i]); | |
690 | state->frame[i] = NULL; | |
691 | } | |
b5dc0163 | 692 | clear_jmp_history(state); |
1969db47 AS |
693 | if (free_self) |
694 | kfree(state); | |
638f5b90 AS |
695 | } |
696 | ||
697 | /* copy verifier state from src to dst growing dst stack space | |
698 | * when necessary to accommodate larger src stack | |
699 | */ | |
f4d7e40a AS |
700 | static int copy_func_state(struct bpf_func_state *dst, |
701 | const struct bpf_func_state *src) | |
638f5b90 AS |
702 | { |
703 | int err; | |
704 | ||
fd978bf7 JS |
705 | err = realloc_func_state(dst, src->allocated_stack, src->acquired_refs, |
706 | false); | |
707 | if (err) | |
708 | return err; | |
709 | memcpy(dst, src, offsetof(struct bpf_func_state, acquired_refs)); | |
710 | err = copy_reference_state(dst, src); | |
638f5b90 AS |
711 | if (err) |
712 | return err; | |
638f5b90 AS |
713 | return copy_stack_state(dst, src); |
714 | } | |
715 | ||
f4d7e40a AS |
716 | static int copy_verifier_state(struct bpf_verifier_state *dst_state, |
717 | const struct bpf_verifier_state *src) | |
718 | { | |
719 | struct bpf_func_state *dst; | |
b5dc0163 | 720 | u32 jmp_sz = sizeof(struct bpf_idx_pair) * src->jmp_history_cnt; |
f4d7e40a AS |
721 | int i, err; |
722 | ||
b5dc0163 AS |
723 | if (dst_state->jmp_history_cnt < src->jmp_history_cnt) { |
724 | kfree(dst_state->jmp_history); | |
725 | dst_state->jmp_history = kmalloc(jmp_sz, GFP_USER); | |
726 | if (!dst_state->jmp_history) | |
727 | return -ENOMEM; | |
728 | } | |
729 | memcpy(dst_state->jmp_history, src->jmp_history, jmp_sz); | |
730 | dst_state->jmp_history_cnt = src->jmp_history_cnt; | |
731 | ||
f4d7e40a AS |
732 | /* if dst has more stack frames then src frame, free them */ |
733 | for (i = src->curframe + 1; i <= dst_state->curframe; i++) { | |
734 | free_func_state(dst_state->frame[i]); | |
735 | dst_state->frame[i] = NULL; | |
736 | } | |
979d63d5 | 737 | dst_state->speculative = src->speculative; |
f4d7e40a | 738 | dst_state->curframe = src->curframe; |
d83525ca | 739 | dst_state->active_spin_lock = src->active_spin_lock; |
2589726d AS |
740 | dst_state->branches = src->branches; |
741 | dst_state->parent = src->parent; | |
b5dc0163 AS |
742 | dst_state->first_insn_idx = src->first_insn_idx; |
743 | dst_state->last_insn_idx = src->last_insn_idx; | |
f4d7e40a AS |
744 | for (i = 0; i <= src->curframe; i++) { |
745 | dst = dst_state->frame[i]; | |
746 | if (!dst) { | |
747 | dst = kzalloc(sizeof(*dst), GFP_KERNEL); | |
748 | if (!dst) | |
749 | return -ENOMEM; | |
750 | dst_state->frame[i] = dst; | |
751 | } | |
752 | err = copy_func_state(dst, src->frame[i]); | |
753 | if (err) | |
754 | return err; | |
755 | } | |
756 | return 0; | |
757 | } | |
758 | ||
2589726d AS |
759 | static void update_branch_counts(struct bpf_verifier_env *env, struct bpf_verifier_state *st) |
760 | { | |
761 | while (st) { | |
762 | u32 br = --st->branches; | |
763 | ||
764 | /* WARN_ON(br > 1) technically makes sense here, | |
765 | * but see comment in push_stack(), hence: | |
766 | */ | |
767 | WARN_ONCE((int)br < 0, | |
768 | "BUG update_branch_counts:branches_to_explore=%d\n", | |
769 | br); | |
770 | if (br) | |
771 | break; | |
772 | st = st->parent; | |
773 | } | |
774 | } | |
775 | ||
638f5b90 AS |
776 | static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx, |
777 | int *insn_idx) | |
778 | { | |
779 | struct bpf_verifier_state *cur = env->cur_state; | |
780 | struct bpf_verifier_stack_elem *elem, *head = env->head; | |
781 | int err; | |
17a52670 AS |
782 | |
783 | if (env->head == NULL) | |
638f5b90 | 784 | return -ENOENT; |
17a52670 | 785 | |
638f5b90 AS |
786 | if (cur) { |
787 | err = copy_verifier_state(cur, &head->st); | |
788 | if (err) | |
789 | return err; | |
790 | } | |
791 | if (insn_idx) | |
792 | *insn_idx = head->insn_idx; | |
17a52670 | 793 | if (prev_insn_idx) |
638f5b90 AS |
794 | *prev_insn_idx = head->prev_insn_idx; |
795 | elem = head->next; | |
1969db47 | 796 | free_verifier_state(&head->st, false); |
638f5b90 | 797 | kfree(head); |
17a52670 AS |
798 | env->head = elem; |
799 | env->stack_size--; | |
638f5b90 | 800 | return 0; |
17a52670 AS |
801 | } |
802 | ||
58e2af8b | 803 | static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, |
979d63d5 DB |
804 | int insn_idx, int prev_insn_idx, |
805 | bool speculative) | |
17a52670 | 806 | { |
638f5b90 | 807 | struct bpf_verifier_state *cur = env->cur_state; |
58e2af8b | 808 | struct bpf_verifier_stack_elem *elem; |
638f5b90 | 809 | int err; |
17a52670 | 810 | |
638f5b90 | 811 | elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL); |
17a52670 AS |
812 | if (!elem) |
813 | goto err; | |
814 | ||
17a52670 AS |
815 | elem->insn_idx = insn_idx; |
816 | elem->prev_insn_idx = prev_insn_idx; | |
817 | elem->next = env->head; | |
818 | env->head = elem; | |
819 | env->stack_size++; | |
1969db47 AS |
820 | err = copy_verifier_state(&elem->st, cur); |
821 | if (err) | |
822 | goto err; | |
979d63d5 | 823 | elem->st.speculative |= speculative; |
b285fcb7 AS |
824 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_JMP_SEQ) { |
825 | verbose(env, "The sequence of %d jumps is too complex.\n", | |
826 | env->stack_size); | |
17a52670 AS |
827 | goto err; |
828 | } | |
2589726d AS |
829 | if (elem->st.parent) { |
830 | ++elem->st.parent->branches; | |
831 | /* WARN_ON(branches > 2) technically makes sense here, | |
832 | * but | |
833 | * 1. speculative states will bump 'branches' for non-branch | |
834 | * instructions | |
835 | * 2. is_state_visited() heuristics may decide not to create | |
836 | * a new state for a sequence of branches and all such current | |
837 | * and cloned states will be pointing to a single parent state | |
838 | * which might have large 'branches' count. | |
839 | */ | |
840 | } | |
17a52670 AS |
841 | return &elem->st; |
842 | err: | |
5896351e AS |
843 | free_verifier_state(env->cur_state, true); |
844 | env->cur_state = NULL; | |
17a52670 | 845 | /* pop all elements and return */ |
638f5b90 | 846 | while (!pop_stack(env, NULL, NULL)); |
17a52670 AS |
847 | return NULL; |
848 | } | |
849 | ||
850 | #define CALLER_SAVED_REGS 6 | |
851 | static const int caller_saved[CALLER_SAVED_REGS] = { | |
852 | BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5 | |
853 | }; | |
854 | ||
f1174f77 EC |
855 | static void __mark_reg_not_init(struct bpf_reg_state *reg); |
856 | ||
b03c9f9f EC |
857 | /* Mark the unknown part of a register (variable offset or scalar value) as |
858 | * known to have the value @imm. | |
859 | */ | |
860 | static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm) | |
861 | { | |
a9c676bc AS |
862 | /* Clear id, off, and union(map_ptr, range) */ |
863 | memset(((u8 *)reg) + sizeof(reg->type), 0, | |
864 | offsetof(struct bpf_reg_state, var_off) - sizeof(reg->type)); | |
b03c9f9f EC |
865 | reg->var_off = tnum_const(imm); |
866 | reg->smin_value = (s64)imm; | |
867 | reg->smax_value = (s64)imm; | |
868 | reg->umin_value = imm; | |
869 | reg->umax_value = imm; | |
870 | } | |
871 | ||
f1174f77 EC |
872 | /* Mark the 'variable offset' part of a register as zero. This should be |
873 | * used only on registers holding a pointer type. | |
874 | */ | |
875 | static void __mark_reg_known_zero(struct bpf_reg_state *reg) | |
a9789ef9 | 876 | { |
b03c9f9f | 877 | __mark_reg_known(reg, 0); |
f1174f77 | 878 | } |
a9789ef9 | 879 | |
cc2b14d5 AS |
880 | static void __mark_reg_const_zero(struct bpf_reg_state *reg) |
881 | { | |
882 | __mark_reg_known(reg, 0); | |
cc2b14d5 AS |
883 | reg->type = SCALAR_VALUE; |
884 | } | |
885 | ||
61bd5218 JK |
886 | static void mark_reg_known_zero(struct bpf_verifier_env *env, |
887 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
888 | { |
889 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 890 | verbose(env, "mark_reg_known_zero(regs, %u)\n", regno); |
f1174f77 EC |
891 | /* Something bad happened, let's kill all regs */ |
892 | for (regno = 0; regno < MAX_BPF_REG; regno++) | |
893 | __mark_reg_not_init(regs + regno); | |
894 | return; | |
895 | } | |
896 | __mark_reg_known_zero(regs + regno); | |
897 | } | |
898 | ||
de8f3a83 DB |
899 | static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg) |
900 | { | |
901 | return type_is_pkt_pointer(reg->type); | |
902 | } | |
903 | ||
904 | static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg) | |
905 | { | |
906 | return reg_is_pkt_pointer(reg) || | |
907 | reg->type == PTR_TO_PACKET_END; | |
908 | } | |
909 | ||
910 | /* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */ | |
911 | static bool reg_is_init_pkt_pointer(const struct bpf_reg_state *reg, | |
912 | enum bpf_reg_type which) | |
913 | { | |
914 | /* The register can already have a range from prior markings. | |
915 | * This is fine as long as it hasn't been advanced from its | |
916 | * origin. | |
917 | */ | |
918 | return reg->type == which && | |
919 | reg->id == 0 && | |
920 | reg->off == 0 && | |
921 | tnum_equals_const(reg->var_off, 0); | |
922 | } | |
923 | ||
b03c9f9f EC |
924 | /* Attempts to improve min/max values based on var_off information */ |
925 | static void __update_reg_bounds(struct bpf_reg_state *reg) | |
926 | { | |
927 | /* min signed is max(sign bit) | min(other bits) */ | |
928 | reg->smin_value = max_t(s64, reg->smin_value, | |
929 | reg->var_off.value | (reg->var_off.mask & S64_MIN)); | |
930 | /* max signed is min(sign bit) | max(other bits) */ | |
931 | reg->smax_value = min_t(s64, reg->smax_value, | |
932 | reg->var_off.value | (reg->var_off.mask & S64_MAX)); | |
933 | reg->umin_value = max(reg->umin_value, reg->var_off.value); | |
934 | reg->umax_value = min(reg->umax_value, | |
935 | reg->var_off.value | reg->var_off.mask); | |
936 | } | |
937 | ||
938 | /* Uses signed min/max values to inform unsigned, and vice-versa */ | |
939 | static void __reg_deduce_bounds(struct bpf_reg_state *reg) | |
940 | { | |
941 | /* Learn sign from signed bounds. | |
942 | * If we cannot cross the sign boundary, then signed and unsigned bounds | |
943 | * are the same, so combine. This works even in the negative case, e.g. | |
944 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. | |
945 | */ | |
946 | if (reg->smin_value >= 0 || reg->smax_value < 0) { | |
947 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, | |
948 | reg->umin_value); | |
949 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, | |
950 | reg->umax_value); | |
951 | return; | |
952 | } | |
953 | /* Learn sign from unsigned bounds. Signed bounds cross the sign | |
954 | * boundary, so we must be careful. | |
955 | */ | |
956 | if ((s64)reg->umax_value >= 0) { | |
957 | /* Positive. We can't learn anything from the smin, but smax | |
958 | * is positive, hence safe. | |
959 | */ | |
960 | reg->smin_value = reg->umin_value; | |
961 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, | |
962 | reg->umax_value); | |
963 | } else if ((s64)reg->umin_value < 0) { | |
964 | /* Negative. We can't learn anything from the smax, but smin | |
965 | * is negative, hence safe. | |
966 | */ | |
967 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, | |
968 | reg->umin_value); | |
969 | reg->smax_value = reg->umax_value; | |
970 | } | |
971 | } | |
972 | ||
973 | /* Attempts to improve var_off based on unsigned min/max information */ | |
974 | static void __reg_bound_offset(struct bpf_reg_state *reg) | |
975 | { | |
976 | reg->var_off = tnum_intersect(reg->var_off, | |
977 | tnum_range(reg->umin_value, | |
978 | reg->umax_value)); | |
979 | } | |
980 | ||
981 | /* Reset the min/max bounds of a register */ | |
982 | static void __mark_reg_unbounded(struct bpf_reg_state *reg) | |
983 | { | |
984 | reg->smin_value = S64_MIN; | |
985 | reg->smax_value = S64_MAX; | |
986 | reg->umin_value = 0; | |
987 | reg->umax_value = U64_MAX; | |
988 | } | |
989 | ||
f1174f77 EC |
990 | /* Mark a register as having a completely unknown (scalar) value. */ |
991 | static void __mark_reg_unknown(struct bpf_reg_state *reg) | |
992 | { | |
a9c676bc AS |
993 | /* |
994 | * Clear type, id, off, and union(map_ptr, range) and | |
995 | * padding between 'type' and union | |
996 | */ | |
997 | memset(reg, 0, offsetof(struct bpf_reg_state, var_off)); | |
f1174f77 | 998 | reg->type = SCALAR_VALUE; |
f1174f77 | 999 | reg->var_off = tnum_unknown; |
f4d7e40a | 1000 | reg->frameno = 0; |
b03c9f9f | 1001 | __mark_reg_unbounded(reg); |
f1174f77 EC |
1002 | } |
1003 | ||
61bd5218 JK |
1004 | static void mark_reg_unknown(struct bpf_verifier_env *env, |
1005 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
1006 | { |
1007 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 1008 | verbose(env, "mark_reg_unknown(regs, %u)\n", regno); |
19ceb417 AS |
1009 | /* Something bad happened, let's kill all regs except FP */ |
1010 | for (regno = 0; regno < BPF_REG_FP; regno++) | |
f1174f77 EC |
1011 | __mark_reg_not_init(regs + regno); |
1012 | return; | |
1013 | } | |
6754172c AS |
1014 | regs += regno; |
1015 | __mark_reg_unknown(regs); | |
1016 | /* constant backtracking is enabled for root without bpf2bpf calls */ | |
1017 | regs->precise = env->subprog_cnt > 1 || !env->allow_ptr_leaks ? | |
1018 | true : false; | |
f1174f77 EC |
1019 | } |
1020 | ||
1021 | static void __mark_reg_not_init(struct bpf_reg_state *reg) | |
1022 | { | |
1023 | __mark_reg_unknown(reg); | |
1024 | reg->type = NOT_INIT; | |
1025 | } | |
1026 | ||
61bd5218 JK |
1027 | static void mark_reg_not_init(struct bpf_verifier_env *env, |
1028 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
1029 | { |
1030 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 1031 | verbose(env, "mark_reg_not_init(regs, %u)\n", regno); |
19ceb417 AS |
1032 | /* Something bad happened, let's kill all regs except FP */ |
1033 | for (regno = 0; regno < BPF_REG_FP; regno++) | |
f1174f77 EC |
1034 | __mark_reg_not_init(regs + regno); |
1035 | return; | |
1036 | } | |
1037 | __mark_reg_not_init(regs + regno); | |
a9789ef9 DB |
1038 | } |
1039 | ||
5327ed3d | 1040 | #define DEF_NOT_SUBREG (0) |
61bd5218 | 1041 | static void init_reg_state(struct bpf_verifier_env *env, |
f4d7e40a | 1042 | struct bpf_func_state *state) |
17a52670 | 1043 | { |
f4d7e40a | 1044 | struct bpf_reg_state *regs = state->regs; |
17a52670 AS |
1045 | int i; |
1046 | ||
dc503a8a | 1047 | for (i = 0; i < MAX_BPF_REG; i++) { |
61bd5218 | 1048 | mark_reg_not_init(env, regs, i); |
dc503a8a | 1049 | regs[i].live = REG_LIVE_NONE; |
679c782d | 1050 | regs[i].parent = NULL; |
5327ed3d | 1051 | regs[i].subreg_def = DEF_NOT_SUBREG; |
dc503a8a | 1052 | } |
17a52670 AS |
1053 | |
1054 | /* frame pointer */ | |
f1174f77 | 1055 | regs[BPF_REG_FP].type = PTR_TO_STACK; |
61bd5218 | 1056 | mark_reg_known_zero(env, regs, BPF_REG_FP); |
f4d7e40a | 1057 | regs[BPF_REG_FP].frameno = state->frameno; |
17a52670 AS |
1058 | |
1059 | /* 1st arg to a function */ | |
1060 | regs[BPF_REG_1].type = PTR_TO_CTX; | |
61bd5218 | 1061 | mark_reg_known_zero(env, regs, BPF_REG_1); |
6760bf2d DB |
1062 | } |
1063 | ||
f4d7e40a AS |
1064 | #define BPF_MAIN_FUNC (-1) |
1065 | static void init_func_state(struct bpf_verifier_env *env, | |
1066 | struct bpf_func_state *state, | |
1067 | int callsite, int frameno, int subprogno) | |
1068 | { | |
1069 | state->callsite = callsite; | |
1070 | state->frameno = frameno; | |
1071 | state->subprogno = subprogno; | |
1072 | init_reg_state(env, state); | |
1073 | } | |
1074 | ||
17a52670 AS |
1075 | enum reg_arg_type { |
1076 | SRC_OP, /* register is used as source operand */ | |
1077 | DST_OP, /* register is used as destination operand */ | |
1078 | DST_OP_NO_MARK /* same as above, check only, don't mark */ | |
1079 | }; | |
1080 | ||
cc8b0b92 AS |
1081 | static int cmp_subprogs(const void *a, const void *b) |
1082 | { | |
9c8105bd JW |
1083 | return ((struct bpf_subprog_info *)a)->start - |
1084 | ((struct bpf_subprog_info *)b)->start; | |
cc8b0b92 AS |
1085 | } |
1086 | ||
1087 | static int find_subprog(struct bpf_verifier_env *env, int off) | |
1088 | { | |
9c8105bd | 1089 | struct bpf_subprog_info *p; |
cc8b0b92 | 1090 | |
9c8105bd JW |
1091 | p = bsearch(&off, env->subprog_info, env->subprog_cnt, |
1092 | sizeof(env->subprog_info[0]), cmp_subprogs); | |
cc8b0b92 AS |
1093 | if (!p) |
1094 | return -ENOENT; | |
9c8105bd | 1095 | return p - env->subprog_info; |
cc8b0b92 AS |
1096 | |
1097 | } | |
1098 | ||
1099 | static int add_subprog(struct bpf_verifier_env *env, int off) | |
1100 | { | |
1101 | int insn_cnt = env->prog->len; | |
1102 | int ret; | |
1103 | ||
1104 | if (off >= insn_cnt || off < 0) { | |
1105 | verbose(env, "call to invalid destination\n"); | |
1106 | return -EINVAL; | |
1107 | } | |
1108 | ret = find_subprog(env, off); | |
1109 | if (ret >= 0) | |
1110 | return 0; | |
4cb3d99c | 1111 | if (env->subprog_cnt >= BPF_MAX_SUBPROGS) { |
cc8b0b92 AS |
1112 | verbose(env, "too many subprograms\n"); |
1113 | return -E2BIG; | |
1114 | } | |
9c8105bd JW |
1115 | env->subprog_info[env->subprog_cnt++].start = off; |
1116 | sort(env->subprog_info, env->subprog_cnt, | |
1117 | sizeof(env->subprog_info[0]), cmp_subprogs, NULL); | |
cc8b0b92 AS |
1118 | return 0; |
1119 | } | |
1120 | ||
1121 | static int check_subprogs(struct bpf_verifier_env *env) | |
1122 | { | |
1123 | int i, ret, subprog_start, subprog_end, off, cur_subprog = 0; | |
9c8105bd | 1124 | struct bpf_subprog_info *subprog = env->subprog_info; |
cc8b0b92 AS |
1125 | struct bpf_insn *insn = env->prog->insnsi; |
1126 | int insn_cnt = env->prog->len; | |
1127 | ||
f910cefa JW |
1128 | /* Add entry function. */ |
1129 | ret = add_subprog(env, 0); | |
1130 | if (ret < 0) | |
1131 | return ret; | |
1132 | ||
cc8b0b92 AS |
1133 | /* determine subprog starts. The end is one before the next starts */ |
1134 | for (i = 0; i < insn_cnt; i++) { | |
1135 | if (insn[i].code != (BPF_JMP | BPF_CALL)) | |
1136 | continue; | |
1137 | if (insn[i].src_reg != BPF_PSEUDO_CALL) | |
1138 | continue; | |
1139 | if (!env->allow_ptr_leaks) { | |
1140 | verbose(env, "function calls to other bpf functions are allowed for root only\n"); | |
1141 | return -EPERM; | |
1142 | } | |
cc8b0b92 AS |
1143 | ret = add_subprog(env, i + insn[i].imm + 1); |
1144 | if (ret < 0) | |
1145 | return ret; | |
1146 | } | |
1147 | ||
4cb3d99c JW |
1148 | /* Add a fake 'exit' subprog which could simplify subprog iteration |
1149 | * logic. 'subprog_cnt' should not be increased. | |
1150 | */ | |
1151 | subprog[env->subprog_cnt].start = insn_cnt; | |
1152 | ||
06ee7115 | 1153 | if (env->log.level & BPF_LOG_LEVEL2) |
cc8b0b92 | 1154 | for (i = 0; i < env->subprog_cnt; i++) |
9c8105bd | 1155 | verbose(env, "func#%d @%d\n", i, subprog[i].start); |
cc8b0b92 AS |
1156 | |
1157 | /* now check that all jumps are within the same subprog */ | |
4cb3d99c JW |
1158 | subprog_start = subprog[cur_subprog].start; |
1159 | subprog_end = subprog[cur_subprog + 1].start; | |
cc8b0b92 AS |
1160 | for (i = 0; i < insn_cnt; i++) { |
1161 | u8 code = insn[i].code; | |
1162 | ||
092ed096 | 1163 | if (BPF_CLASS(code) != BPF_JMP && BPF_CLASS(code) != BPF_JMP32) |
cc8b0b92 AS |
1164 | goto next; |
1165 | if (BPF_OP(code) == BPF_EXIT || BPF_OP(code) == BPF_CALL) | |
1166 | goto next; | |
1167 | off = i + insn[i].off + 1; | |
1168 | if (off < subprog_start || off >= subprog_end) { | |
1169 | verbose(env, "jump out of range from insn %d to %d\n", i, off); | |
1170 | return -EINVAL; | |
1171 | } | |
1172 | next: | |
1173 | if (i == subprog_end - 1) { | |
1174 | /* to avoid fall-through from one subprog into another | |
1175 | * the last insn of the subprog should be either exit | |
1176 | * or unconditional jump back | |
1177 | */ | |
1178 | if (code != (BPF_JMP | BPF_EXIT) && | |
1179 | code != (BPF_JMP | BPF_JA)) { | |
1180 | verbose(env, "last insn is not an exit or jmp\n"); | |
1181 | return -EINVAL; | |
1182 | } | |
1183 | subprog_start = subprog_end; | |
4cb3d99c JW |
1184 | cur_subprog++; |
1185 | if (cur_subprog < env->subprog_cnt) | |
9c8105bd | 1186 | subprog_end = subprog[cur_subprog + 1].start; |
cc8b0b92 AS |
1187 | } |
1188 | } | |
1189 | return 0; | |
1190 | } | |
1191 | ||
679c782d EC |
1192 | /* Parentage chain of this register (or stack slot) should take care of all |
1193 | * issues like callee-saved registers, stack slot allocation time, etc. | |
1194 | */ | |
f4d7e40a | 1195 | static int mark_reg_read(struct bpf_verifier_env *env, |
679c782d | 1196 | const struct bpf_reg_state *state, |
5327ed3d | 1197 | struct bpf_reg_state *parent, u8 flag) |
f4d7e40a AS |
1198 | { |
1199 | bool writes = parent == state->parent; /* Observe write marks */ | |
06ee7115 | 1200 | int cnt = 0; |
dc503a8a EC |
1201 | |
1202 | while (parent) { | |
1203 | /* if read wasn't screened by an earlier write ... */ | |
679c782d | 1204 | if (writes && state->live & REG_LIVE_WRITTEN) |
dc503a8a | 1205 | break; |
9242b5f5 AS |
1206 | if (parent->live & REG_LIVE_DONE) { |
1207 | verbose(env, "verifier BUG type %s var_off %lld off %d\n", | |
1208 | reg_type_str[parent->type], | |
1209 | parent->var_off.value, parent->off); | |
1210 | return -EFAULT; | |
1211 | } | |
5327ed3d JW |
1212 | /* The first condition is more likely to be true than the |
1213 | * second, checked it first. | |
1214 | */ | |
1215 | if ((parent->live & REG_LIVE_READ) == flag || | |
1216 | parent->live & REG_LIVE_READ64) | |
25af32da AS |
1217 | /* The parentage chain never changes and |
1218 | * this parent was already marked as LIVE_READ. | |
1219 | * There is no need to keep walking the chain again and | |
1220 | * keep re-marking all parents as LIVE_READ. | |
1221 | * This case happens when the same register is read | |
1222 | * multiple times without writes into it in-between. | |
5327ed3d JW |
1223 | * Also, if parent has the stronger REG_LIVE_READ64 set, |
1224 | * then no need to set the weak REG_LIVE_READ32. | |
25af32da AS |
1225 | */ |
1226 | break; | |
dc503a8a | 1227 | /* ... then we depend on parent's value */ |
5327ed3d JW |
1228 | parent->live |= flag; |
1229 | /* REG_LIVE_READ64 overrides REG_LIVE_READ32. */ | |
1230 | if (flag == REG_LIVE_READ64) | |
1231 | parent->live &= ~REG_LIVE_READ32; | |
dc503a8a EC |
1232 | state = parent; |
1233 | parent = state->parent; | |
f4d7e40a | 1234 | writes = true; |
06ee7115 | 1235 | cnt++; |
dc503a8a | 1236 | } |
06ee7115 AS |
1237 | |
1238 | if (env->longest_mark_read_walk < cnt) | |
1239 | env->longest_mark_read_walk = cnt; | |
f4d7e40a | 1240 | return 0; |
dc503a8a EC |
1241 | } |
1242 | ||
5327ed3d JW |
1243 | /* This function is supposed to be used by the following 32-bit optimization |
1244 | * code only. It returns TRUE if the source or destination register operates | |
1245 | * on 64-bit, otherwise return FALSE. | |
1246 | */ | |
1247 | static bool is_reg64(struct bpf_verifier_env *env, struct bpf_insn *insn, | |
1248 | u32 regno, struct bpf_reg_state *reg, enum reg_arg_type t) | |
1249 | { | |
1250 | u8 code, class, op; | |
1251 | ||
1252 | code = insn->code; | |
1253 | class = BPF_CLASS(code); | |
1254 | op = BPF_OP(code); | |
1255 | if (class == BPF_JMP) { | |
1256 | /* BPF_EXIT for "main" will reach here. Return TRUE | |
1257 | * conservatively. | |
1258 | */ | |
1259 | if (op == BPF_EXIT) | |
1260 | return true; | |
1261 | if (op == BPF_CALL) { | |
1262 | /* BPF to BPF call will reach here because of marking | |
1263 | * caller saved clobber with DST_OP_NO_MARK for which we | |
1264 | * don't care the register def because they are anyway | |
1265 | * marked as NOT_INIT already. | |
1266 | */ | |
1267 | if (insn->src_reg == BPF_PSEUDO_CALL) | |
1268 | return false; | |
1269 | /* Helper call will reach here because of arg type | |
1270 | * check, conservatively return TRUE. | |
1271 | */ | |
1272 | if (t == SRC_OP) | |
1273 | return true; | |
1274 | ||
1275 | return false; | |
1276 | } | |
1277 | } | |
1278 | ||
1279 | if (class == BPF_ALU64 || class == BPF_JMP || | |
1280 | /* BPF_END always use BPF_ALU class. */ | |
1281 | (class == BPF_ALU && op == BPF_END && insn->imm == 64)) | |
1282 | return true; | |
1283 | ||
1284 | if (class == BPF_ALU || class == BPF_JMP32) | |
1285 | return false; | |
1286 | ||
1287 | if (class == BPF_LDX) { | |
1288 | if (t != SRC_OP) | |
1289 | return BPF_SIZE(code) == BPF_DW; | |
1290 | /* LDX source must be ptr. */ | |
1291 | return true; | |
1292 | } | |
1293 | ||
1294 | if (class == BPF_STX) { | |
1295 | if (reg->type != SCALAR_VALUE) | |
1296 | return true; | |
1297 | return BPF_SIZE(code) == BPF_DW; | |
1298 | } | |
1299 | ||
1300 | if (class == BPF_LD) { | |
1301 | u8 mode = BPF_MODE(code); | |
1302 | ||
1303 | /* LD_IMM64 */ | |
1304 | if (mode == BPF_IMM) | |
1305 | return true; | |
1306 | ||
1307 | /* Both LD_IND and LD_ABS return 32-bit data. */ | |
1308 | if (t != SRC_OP) | |
1309 | return false; | |
1310 | ||
1311 | /* Implicit ctx ptr. */ | |
1312 | if (regno == BPF_REG_6) | |
1313 | return true; | |
1314 | ||
1315 | /* Explicit source could be any width. */ | |
1316 | return true; | |
1317 | } | |
1318 | ||
1319 | if (class == BPF_ST) | |
1320 | /* The only source register for BPF_ST is a ptr. */ | |
1321 | return true; | |
1322 | ||
1323 | /* Conservatively return true at default. */ | |
1324 | return true; | |
1325 | } | |
1326 | ||
b325fbca JW |
1327 | /* Return TRUE if INSN doesn't have explicit value define. */ |
1328 | static bool insn_no_def(struct bpf_insn *insn) | |
1329 | { | |
1330 | u8 class = BPF_CLASS(insn->code); | |
1331 | ||
1332 | return (class == BPF_JMP || class == BPF_JMP32 || | |
1333 | class == BPF_STX || class == BPF_ST); | |
1334 | } | |
1335 | ||
1336 | /* Return TRUE if INSN has defined any 32-bit value explicitly. */ | |
1337 | static bool insn_has_def32(struct bpf_verifier_env *env, struct bpf_insn *insn) | |
1338 | { | |
1339 | if (insn_no_def(insn)) | |
1340 | return false; | |
1341 | ||
1342 | return !is_reg64(env, insn, insn->dst_reg, NULL, DST_OP); | |
1343 | } | |
1344 | ||
5327ed3d JW |
1345 | static void mark_insn_zext(struct bpf_verifier_env *env, |
1346 | struct bpf_reg_state *reg) | |
1347 | { | |
1348 | s32 def_idx = reg->subreg_def; | |
1349 | ||
1350 | if (def_idx == DEF_NOT_SUBREG) | |
1351 | return; | |
1352 | ||
1353 | env->insn_aux_data[def_idx - 1].zext_dst = true; | |
1354 | /* The dst will be zero extended, so won't be sub-register anymore. */ | |
1355 | reg->subreg_def = DEF_NOT_SUBREG; | |
1356 | } | |
1357 | ||
dc503a8a | 1358 | static int check_reg_arg(struct bpf_verifier_env *env, u32 regno, |
17a52670 AS |
1359 | enum reg_arg_type t) |
1360 | { | |
f4d7e40a AS |
1361 | struct bpf_verifier_state *vstate = env->cur_state; |
1362 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
5327ed3d | 1363 | struct bpf_insn *insn = env->prog->insnsi + env->insn_idx; |
c342dc10 | 1364 | struct bpf_reg_state *reg, *regs = state->regs; |
5327ed3d | 1365 | bool rw64; |
dc503a8a | 1366 | |
17a52670 | 1367 | if (regno >= MAX_BPF_REG) { |
61bd5218 | 1368 | verbose(env, "R%d is invalid\n", regno); |
17a52670 AS |
1369 | return -EINVAL; |
1370 | } | |
1371 | ||
c342dc10 | 1372 | reg = ®s[regno]; |
5327ed3d | 1373 | rw64 = is_reg64(env, insn, regno, reg, t); |
17a52670 AS |
1374 | if (t == SRC_OP) { |
1375 | /* check whether register used as source operand can be read */ | |
c342dc10 | 1376 | if (reg->type == NOT_INIT) { |
61bd5218 | 1377 | verbose(env, "R%d !read_ok\n", regno); |
17a52670 AS |
1378 | return -EACCES; |
1379 | } | |
679c782d | 1380 | /* We don't need to worry about FP liveness because it's read-only */ |
c342dc10 JW |
1381 | if (regno == BPF_REG_FP) |
1382 | return 0; | |
1383 | ||
5327ed3d JW |
1384 | if (rw64) |
1385 | mark_insn_zext(env, reg); | |
1386 | ||
1387 | return mark_reg_read(env, reg, reg->parent, | |
1388 | rw64 ? REG_LIVE_READ64 : REG_LIVE_READ32); | |
17a52670 AS |
1389 | } else { |
1390 | /* check whether register used as dest operand can be written to */ | |
1391 | if (regno == BPF_REG_FP) { | |
61bd5218 | 1392 | verbose(env, "frame pointer is read only\n"); |
17a52670 AS |
1393 | return -EACCES; |
1394 | } | |
c342dc10 | 1395 | reg->live |= REG_LIVE_WRITTEN; |
5327ed3d | 1396 | reg->subreg_def = rw64 ? DEF_NOT_SUBREG : env->insn_idx + 1; |
17a52670 | 1397 | if (t == DST_OP) |
61bd5218 | 1398 | mark_reg_unknown(env, regs, regno); |
17a52670 AS |
1399 | } |
1400 | return 0; | |
1401 | } | |
1402 | ||
b5dc0163 AS |
1403 | /* for any branch, call, exit record the history of jmps in the given state */ |
1404 | static int push_jmp_history(struct bpf_verifier_env *env, | |
1405 | struct bpf_verifier_state *cur) | |
1406 | { | |
1407 | u32 cnt = cur->jmp_history_cnt; | |
1408 | struct bpf_idx_pair *p; | |
1409 | ||
1410 | cnt++; | |
1411 | p = krealloc(cur->jmp_history, cnt * sizeof(*p), GFP_USER); | |
1412 | if (!p) | |
1413 | return -ENOMEM; | |
1414 | p[cnt - 1].idx = env->insn_idx; | |
1415 | p[cnt - 1].prev_idx = env->prev_insn_idx; | |
1416 | cur->jmp_history = p; | |
1417 | cur->jmp_history_cnt = cnt; | |
1418 | return 0; | |
1419 | } | |
1420 | ||
1421 | /* Backtrack one insn at a time. If idx is not at the top of recorded | |
1422 | * history then previous instruction came from straight line execution. | |
1423 | */ | |
1424 | static int get_prev_insn_idx(struct bpf_verifier_state *st, int i, | |
1425 | u32 *history) | |
1426 | { | |
1427 | u32 cnt = *history; | |
1428 | ||
1429 | if (cnt && st->jmp_history[cnt - 1].idx == i) { | |
1430 | i = st->jmp_history[cnt - 1].prev_idx; | |
1431 | (*history)--; | |
1432 | } else { | |
1433 | i--; | |
1434 | } | |
1435 | return i; | |
1436 | } | |
1437 | ||
1438 | /* For given verifier state backtrack_insn() is called from the last insn to | |
1439 | * the first insn. Its purpose is to compute a bitmask of registers and | |
1440 | * stack slots that needs precision in the parent verifier state. | |
1441 | */ | |
1442 | static int backtrack_insn(struct bpf_verifier_env *env, int idx, | |
1443 | u32 *reg_mask, u64 *stack_mask) | |
1444 | { | |
1445 | const struct bpf_insn_cbs cbs = { | |
1446 | .cb_print = verbose, | |
1447 | .private_data = env, | |
1448 | }; | |
1449 | struct bpf_insn *insn = env->prog->insnsi + idx; | |
1450 | u8 class = BPF_CLASS(insn->code); | |
1451 | u8 opcode = BPF_OP(insn->code); | |
1452 | u8 mode = BPF_MODE(insn->code); | |
1453 | u32 dreg = 1u << insn->dst_reg; | |
1454 | u32 sreg = 1u << insn->src_reg; | |
1455 | u32 spi; | |
1456 | ||
1457 | if (insn->code == 0) | |
1458 | return 0; | |
1459 | if (env->log.level & BPF_LOG_LEVEL) { | |
1460 | verbose(env, "regs=%x stack=%llx before ", *reg_mask, *stack_mask); | |
1461 | verbose(env, "%d: ", idx); | |
1462 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); | |
1463 | } | |
1464 | ||
1465 | if (class == BPF_ALU || class == BPF_ALU64) { | |
1466 | if (!(*reg_mask & dreg)) | |
1467 | return 0; | |
1468 | if (opcode == BPF_MOV) { | |
1469 | if (BPF_SRC(insn->code) == BPF_X) { | |
1470 | /* dreg = sreg | |
1471 | * dreg needs precision after this insn | |
1472 | * sreg needs precision before this insn | |
1473 | */ | |
1474 | *reg_mask &= ~dreg; | |
1475 | *reg_mask |= sreg; | |
1476 | } else { | |
1477 | /* dreg = K | |
1478 | * dreg needs precision after this insn. | |
1479 | * Corresponding register is already marked | |
1480 | * as precise=true in this verifier state. | |
1481 | * No further markings in parent are necessary | |
1482 | */ | |
1483 | *reg_mask &= ~dreg; | |
1484 | } | |
1485 | } else { | |
1486 | if (BPF_SRC(insn->code) == BPF_X) { | |
1487 | /* dreg += sreg | |
1488 | * both dreg and sreg need precision | |
1489 | * before this insn | |
1490 | */ | |
1491 | *reg_mask |= sreg; | |
1492 | } /* else dreg += K | |
1493 | * dreg still needs precision before this insn | |
1494 | */ | |
1495 | } | |
1496 | } else if (class == BPF_LDX) { | |
1497 | if (!(*reg_mask & dreg)) | |
1498 | return 0; | |
1499 | *reg_mask &= ~dreg; | |
1500 | ||
1501 | /* scalars can only be spilled into stack w/o losing precision. | |
1502 | * Load from any other memory can be zero extended. | |
1503 | * The desire to keep that precision is already indicated | |
1504 | * by 'precise' mark in corresponding register of this state. | |
1505 | * No further tracking necessary. | |
1506 | */ | |
1507 | if (insn->src_reg != BPF_REG_FP) | |
1508 | return 0; | |
1509 | if (BPF_SIZE(insn->code) != BPF_DW) | |
1510 | return 0; | |
1511 | ||
1512 | /* dreg = *(u64 *)[fp - off] was a fill from the stack. | |
1513 | * that [fp - off] slot contains scalar that needs to be | |
1514 | * tracked with precision | |
1515 | */ | |
1516 | spi = (-insn->off - 1) / BPF_REG_SIZE; | |
1517 | if (spi >= 64) { | |
1518 | verbose(env, "BUG spi %d\n", spi); | |
1519 | WARN_ONCE(1, "verifier backtracking bug"); | |
1520 | return -EFAULT; | |
1521 | } | |
1522 | *stack_mask |= 1ull << spi; | |
b3b50f05 | 1523 | } else if (class == BPF_STX || class == BPF_ST) { |
b5dc0163 | 1524 | if (*reg_mask & dreg) |
b3b50f05 | 1525 | /* stx & st shouldn't be using _scalar_ dst_reg |
b5dc0163 AS |
1526 | * to access memory. It means backtracking |
1527 | * encountered a case of pointer subtraction. | |
1528 | */ | |
1529 | return -ENOTSUPP; | |
1530 | /* scalars can only be spilled into stack */ | |
1531 | if (insn->dst_reg != BPF_REG_FP) | |
1532 | return 0; | |
1533 | if (BPF_SIZE(insn->code) != BPF_DW) | |
1534 | return 0; | |
1535 | spi = (-insn->off - 1) / BPF_REG_SIZE; | |
1536 | if (spi >= 64) { | |
1537 | verbose(env, "BUG spi %d\n", spi); | |
1538 | WARN_ONCE(1, "verifier backtracking bug"); | |
1539 | return -EFAULT; | |
1540 | } | |
1541 | if (!(*stack_mask & (1ull << spi))) | |
1542 | return 0; | |
1543 | *stack_mask &= ~(1ull << spi); | |
b3b50f05 AN |
1544 | if (class == BPF_STX) |
1545 | *reg_mask |= sreg; | |
b5dc0163 AS |
1546 | } else if (class == BPF_JMP || class == BPF_JMP32) { |
1547 | if (opcode == BPF_CALL) { | |
1548 | if (insn->src_reg == BPF_PSEUDO_CALL) | |
1549 | return -ENOTSUPP; | |
1550 | /* regular helper call sets R0 */ | |
1551 | *reg_mask &= ~1; | |
1552 | if (*reg_mask & 0x3f) { | |
1553 | /* if backtracing was looking for registers R1-R5 | |
1554 | * they should have been found already. | |
1555 | */ | |
1556 | verbose(env, "BUG regs %x\n", *reg_mask); | |
1557 | WARN_ONCE(1, "verifier backtracking bug"); | |
1558 | return -EFAULT; | |
1559 | } | |
1560 | } else if (opcode == BPF_EXIT) { | |
1561 | return -ENOTSUPP; | |
1562 | } | |
1563 | } else if (class == BPF_LD) { | |
1564 | if (!(*reg_mask & dreg)) | |
1565 | return 0; | |
1566 | *reg_mask &= ~dreg; | |
1567 | /* It's ld_imm64 or ld_abs or ld_ind. | |
1568 | * For ld_imm64 no further tracking of precision | |
1569 | * into parent is necessary | |
1570 | */ | |
1571 | if (mode == BPF_IND || mode == BPF_ABS) | |
1572 | /* to be analyzed */ | |
1573 | return -ENOTSUPP; | |
b5dc0163 AS |
1574 | } |
1575 | return 0; | |
1576 | } | |
1577 | ||
1578 | /* the scalar precision tracking algorithm: | |
1579 | * . at the start all registers have precise=false. | |
1580 | * . scalar ranges are tracked as normal through alu and jmp insns. | |
1581 | * . once precise value of the scalar register is used in: | |
1582 | * . ptr + scalar alu | |
1583 | * . if (scalar cond K|scalar) | |
1584 | * . helper_call(.., scalar, ...) where ARG_CONST is expected | |
1585 | * backtrack through the verifier states and mark all registers and | |
1586 | * stack slots with spilled constants that these scalar regisers | |
1587 | * should be precise. | |
1588 | * . during state pruning two registers (or spilled stack slots) | |
1589 | * are equivalent if both are not precise. | |
1590 | * | |
1591 | * Note the verifier cannot simply walk register parentage chain, | |
1592 | * since many different registers and stack slots could have been | |
1593 | * used to compute single precise scalar. | |
1594 | * | |
1595 | * The approach of starting with precise=true for all registers and then | |
1596 | * backtrack to mark a register as not precise when the verifier detects | |
1597 | * that program doesn't care about specific value (e.g., when helper | |
1598 | * takes register as ARG_ANYTHING parameter) is not safe. | |
1599 | * | |
1600 | * It's ok to walk single parentage chain of the verifier states. | |
1601 | * It's possible that this backtracking will go all the way till 1st insn. | |
1602 | * All other branches will be explored for needing precision later. | |
1603 | * | |
1604 | * The backtracking needs to deal with cases like: | |
1605 | * 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) | |
1606 | * r9 -= r8 | |
1607 | * r5 = r9 | |
1608 | * if r5 > 0x79f goto pc+7 | |
1609 | * R5_w=inv(id=0,umax_value=1951,var_off=(0x0; 0x7ff)) | |
1610 | * r5 += 1 | |
1611 | * ... | |
1612 | * call bpf_perf_event_output#25 | |
1613 | * where .arg5_type = ARG_CONST_SIZE_OR_ZERO | |
1614 | * | |
1615 | * and this case: | |
1616 | * r6 = 1 | |
1617 | * call foo // uses callee's r6 inside to compute r0 | |
1618 | * r0 += r6 | |
1619 | * if r0 == 0 goto | |
1620 | * | |
1621 | * to track above reg_mask/stack_mask needs to be independent for each frame. | |
1622 | * | |
1623 | * Also if parent's curframe > frame where backtracking started, | |
1624 | * the verifier need to mark registers in both frames, otherwise callees | |
1625 | * may incorrectly prune callers. This is similar to | |
1626 | * commit 7640ead93924 ("bpf: verifier: make sure callees don't prune with caller differences") | |
1627 | * | |
1628 | * For now backtracking falls back into conservative marking. | |
1629 | */ | |
1630 | static void mark_all_scalars_precise(struct bpf_verifier_env *env, | |
1631 | struct bpf_verifier_state *st) | |
1632 | { | |
1633 | struct bpf_func_state *func; | |
1634 | struct bpf_reg_state *reg; | |
1635 | int i, j; | |
1636 | ||
1637 | /* big hammer: mark all scalars precise in this path. | |
1638 | * pop_stack may still get !precise scalars. | |
1639 | */ | |
1640 | for (; st; st = st->parent) | |
1641 | for (i = 0; i <= st->curframe; i++) { | |
1642 | func = st->frame[i]; | |
1643 | for (j = 0; j < BPF_REG_FP; j++) { | |
1644 | reg = &func->regs[j]; | |
1645 | if (reg->type != SCALAR_VALUE) | |
1646 | continue; | |
1647 | reg->precise = true; | |
1648 | } | |
1649 | for (j = 0; j < func->allocated_stack / BPF_REG_SIZE; j++) { | |
1650 | if (func->stack[j].slot_type[0] != STACK_SPILL) | |
1651 | continue; | |
1652 | reg = &func->stack[j].spilled_ptr; | |
1653 | if (reg->type != SCALAR_VALUE) | |
1654 | continue; | |
1655 | reg->precise = true; | |
1656 | } | |
1657 | } | |
1658 | } | |
1659 | ||
a3ce685d AS |
1660 | static int __mark_chain_precision(struct bpf_verifier_env *env, int regno, |
1661 | int spi) | |
b5dc0163 AS |
1662 | { |
1663 | struct bpf_verifier_state *st = env->cur_state; | |
1664 | int first_idx = st->first_insn_idx; | |
1665 | int last_idx = env->insn_idx; | |
1666 | struct bpf_func_state *func; | |
1667 | struct bpf_reg_state *reg; | |
a3ce685d AS |
1668 | u32 reg_mask = regno >= 0 ? 1u << regno : 0; |
1669 | u64 stack_mask = spi >= 0 ? 1ull << spi : 0; | |
b5dc0163 | 1670 | bool skip_first = true; |
a3ce685d | 1671 | bool new_marks = false; |
b5dc0163 AS |
1672 | int i, err; |
1673 | ||
1674 | if (!env->allow_ptr_leaks) | |
1675 | /* backtracking is root only for now */ | |
1676 | return 0; | |
1677 | ||
1678 | func = st->frame[st->curframe]; | |
a3ce685d AS |
1679 | if (regno >= 0) { |
1680 | reg = &func->regs[regno]; | |
1681 | if (reg->type != SCALAR_VALUE) { | |
1682 | WARN_ONCE(1, "backtracing misuse"); | |
1683 | return -EFAULT; | |
1684 | } | |
1685 | if (!reg->precise) | |
1686 | new_marks = true; | |
1687 | else | |
1688 | reg_mask = 0; | |
1689 | reg->precise = true; | |
b5dc0163 | 1690 | } |
b5dc0163 | 1691 | |
a3ce685d AS |
1692 | while (spi >= 0) { |
1693 | if (func->stack[spi].slot_type[0] != STACK_SPILL) { | |
1694 | stack_mask = 0; | |
1695 | break; | |
1696 | } | |
1697 | reg = &func->stack[spi].spilled_ptr; | |
1698 | if (reg->type != SCALAR_VALUE) { | |
1699 | stack_mask = 0; | |
1700 | break; | |
1701 | } | |
1702 | if (!reg->precise) | |
1703 | new_marks = true; | |
1704 | else | |
1705 | stack_mask = 0; | |
1706 | reg->precise = true; | |
1707 | break; | |
1708 | } | |
1709 | ||
1710 | if (!new_marks) | |
1711 | return 0; | |
1712 | if (!reg_mask && !stack_mask) | |
1713 | return 0; | |
b5dc0163 AS |
1714 | for (;;) { |
1715 | DECLARE_BITMAP(mask, 64); | |
b5dc0163 AS |
1716 | u32 history = st->jmp_history_cnt; |
1717 | ||
1718 | if (env->log.level & BPF_LOG_LEVEL) | |
1719 | verbose(env, "last_idx %d first_idx %d\n", last_idx, first_idx); | |
1720 | for (i = last_idx;;) { | |
1721 | if (skip_first) { | |
1722 | err = 0; | |
1723 | skip_first = false; | |
1724 | } else { | |
1725 | err = backtrack_insn(env, i, ®_mask, &stack_mask); | |
1726 | } | |
1727 | if (err == -ENOTSUPP) { | |
1728 | mark_all_scalars_precise(env, st); | |
1729 | return 0; | |
1730 | } else if (err) { | |
1731 | return err; | |
1732 | } | |
1733 | if (!reg_mask && !stack_mask) | |
1734 | /* Found assignment(s) into tracked register in this state. | |
1735 | * Since this state is already marked, just return. | |
1736 | * Nothing to be tracked further in the parent state. | |
1737 | */ | |
1738 | return 0; | |
1739 | if (i == first_idx) | |
1740 | break; | |
1741 | i = get_prev_insn_idx(st, i, &history); | |
1742 | if (i >= env->prog->len) { | |
1743 | /* This can happen if backtracking reached insn 0 | |
1744 | * and there are still reg_mask or stack_mask | |
1745 | * to backtrack. | |
1746 | * It means the backtracking missed the spot where | |
1747 | * particular register was initialized with a constant. | |
1748 | */ | |
1749 | verbose(env, "BUG backtracking idx %d\n", i); | |
1750 | WARN_ONCE(1, "verifier backtracking bug"); | |
1751 | return -EFAULT; | |
1752 | } | |
1753 | } | |
1754 | st = st->parent; | |
1755 | if (!st) | |
1756 | break; | |
1757 | ||
a3ce685d | 1758 | new_marks = false; |
b5dc0163 AS |
1759 | func = st->frame[st->curframe]; |
1760 | bitmap_from_u64(mask, reg_mask); | |
1761 | for_each_set_bit(i, mask, 32) { | |
1762 | reg = &func->regs[i]; | |
a3ce685d AS |
1763 | if (reg->type != SCALAR_VALUE) { |
1764 | reg_mask &= ~(1u << i); | |
b5dc0163 | 1765 | continue; |
a3ce685d | 1766 | } |
b5dc0163 AS |
1767 | if (!reg->precise) |
1768 | new_marks = true; | |
1769 | reg->precise = true; | |
1770 | } | |
1771 | ||
1772 | bitmap_from_u64(mask, stack_mask); | |
1773 | for_each_set_bit(i, mask, 64) { | |
1774 | if (i >= func->allocated_stack / BPF_REG_SIZE) { | |
1775 | /* This can happen if backtracking | |
1776 | * is propagating stack precision where | |
1777 | * caller has larger stack frame | |
1778 | * than callee, but backtrack_insn() should | |
1779 | * have returned -ENOTSUPP. | |
1780 | */ | |
1781 | verbose(env, "BUG spi %d stack_size %d\n", | |
1782 | i, func->allocated_stack); | |
1783 | WARN_ONCE(1, "verifier backtracking bug"); | |
1784 | return -EFAULT; | |
1785 | } | |
1786 | ||
a3ce685d AS |
1787 | if (func->stack[i].slot_type[0] != STACK_SPILL) { |
1788 | stack_mask &= ~(1ull << i); | |
b5dc0163 | 1789 | continue; |
a3ce685d | 1790 | } |
b5dc0163 | 1791 | reg = &func->stack[i].spilled_ptr; |
a3ce685d AS |
1792 | if (reg->type != SCALAR_VALUE) { |
1793 | stack_mask &= ~(1ull << i); | |
b5dc0163 | 1794 | continue; |
a3ce685d | 1795 | } |
b5dc0163 AS |
1796 | if (!reg->precise) |
1797 | new_marks = true; | |
1798 | reg->precise = true; | |
1799 | } | |
1800 | if (env->log.level & BPF_LOG_LEVEL) { | |
1801 | print_verifier_state(env, func); | |
1802 | verbose(env, "parent %s regs=%x stack=%llx marks\n", | |
1803 | new_marks ? "didn't have" : "already had", | |
1804 | reg_mask, stack_mask); | |
1805 | } | |
1806 | ||
a3ce685d AS |
1807 | if (!reg_mask && !stack_mask) |
1808 | break; | |
b5dc0163 AS |
1809 | if (!new_marks) |
1810 | break; | |
1811 | ||
1812 | last_idx = st->last_insn_idx; | |
1813 | first_idx = st->first_insn_idx; | |
1814 | } | |
1815 | return 0; | |
1816 | } | |
1817 | ||
a3ce685d AS |
1818 | static int mark_chain_precision(struct bpf_verifier_env *env, int regno) |
1819 | { | |
1820 | return __mark_chain_precision(env, regno, -1); | |
1821 | } | |
1822 | ||
1823 | static int mark_chain_precision_stack(struct bpf_verifier_env *env, int spi) | |
1824 | { | |
1825 | return __mark_chain_precision(env, -1, spi); | |
1826 | } | |
b5dc0163 | 1827 | |
1be7f75d AS |
1828 | static bool is_spillable_regtype(enum bpf_reg_type type) |
1829 | { | |
1830 | switch (type) { | |
1831 | case PTR_TO_MAP_VALUE: | |
1832 | case PTR_TO_MAP_VALUE_OR_NULL: | |
1833 | case PTR_TO_STACK: | |
1834 | case PTR_TO_CTX: | |
969bf05e | 1835 | case PTR_TO_PACKET: |
de8f3a83 | 1836 | case PTR_TO_PACKET_META: |
969bf05e | 1837 | case PTR_TO_PACKET_END: |
d58e468b | 1838 | case PTR_TO_FLOW_KEYS: |
1be7f75d | 1839 | case CONST_PTR_TO_MAP: |
c64b7983 JS |
1840 | case PTR_TO_SOCKET: |
1841 | case PTR_TO_SOCKET_OR_NULL: | |
46f8bc92 MKL |
1842 | case PTR_TO_SOCK_COMMON: |
1843 | case PTR_TO_SOCK_COMMON_OR_NULL: | |
655a51e5 MKL |
1844 | case PTR_TO_TCP_SOCK: |
1845 | case PTR_TO_TCP_SOCK_OR_NULL: | |
fada7fdc | 1846 | case PTR_TO_XDP_SOCK: |
1be7f75d AS |
1847 | return true; |
1848 | default: | |
1849 | return false; | |
1850 | } | |
1851 | } | |
1852 | ||
cc2b14d5 AS |
1853 | /* Does this register contain a constant zero? */ |
1854 | static bool register_is_null(struct bpf_reg_state *reg) | |
1855 | { | |
1856 | return reg->type == SCALAR_VALUE && tnum_equals_const(reg->var_off, 0); | |
1857 | } | |
1858 | ||
f7cf25b2 AS |
1859 | static bool register_is_const(struct bpf_reg_state *reg) |
1860 | { | |
1861 | return reg->type == SCALAR_VALUE && tnum_is_const(reg->var_off); | |
1862 | } | |
1863 | ||
1864 | static void save_register_state(struct bpf_func_state *state, | |
1865 | int spi, struct bpf_reg_state *reg) | |
1866 | { | |
1867 | int i; | |
1868 | ||
1869 | state->stack[spi].spilled_ptr = *reg; | |
1870 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; | |
1871 | ||
1872 | for (i = 0; i < BPF_REG_SIZE; i++) | |
1873 | state->stack[spi].slot_type[i] = STACK_SPILL; | |
1874 | } | |
1875 | ||
17a52670 AS |
1876 | /* check_stack_read/write functions track spill/fill of registers, |
1877 | * stack boundary and alignment are checked in check_mem_access() | |
1878 | */ | |
61bd5218 | 1879 | static int check_stack_write(struct bpf_verifier_env *env, |
f4d7e40a | 1880 | struct bpf_func_state *state, /* func where register points to */ |
af86ca4e | 1881 | int off, int size, int value_regno, int insn_idx) |
17a52670 | 1882 | { |
f4d7e40a | 1883 | struct bpf_func_state *cur; /* state of the current function */ |
638f5b90 | 1884 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err; |
b5dc0163 | 1885 | u32 dst_reg = env->prog->insnsi[insn_idx].dst_reg; |
f7cf25b2 | 1886 | struct bpf_reg_state *reg = NULL; |
638f5b90 | 1887 | |
f4d7e40a | 1888 | err = realloc_func_state(state, round_up(slot + 1, BPF_REG_SIZE), |
fd978bf7 | 1889 | state->acquired_refs, true); |
638f5b90 AS |
1890 | if (err) |
1891 | return err; | |
9c399760 AS |
1892 | /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0, |
1893 | * so it's aligned access and [off, off + size) are within stack limits | |
1894 | */ | |
638f5b90 AS |
1895 | if (!env->allow_ptr_leaks && |
1896 | state->stack[spi].slot_type[0] == STACK_SPILL && | |
1897 | size != BPF_REG_SIZE) { | |
1898 | verbose(env, "attempt to corrupt spilled pointer on stack\n"); | |
1899 | return -EACCES; | |
1900 | } | |
17a52670 | 1901 | |
f4d7e40a | 1902 | cur = env->cur_state->frame[env->cur_state->curframe]; |
f7cf25b2 AS |
1903 | if (value_regno >= 0) |
1904 | reg = &cur->regs[value_regno]; | |
17a52670 | 1905 | |
f7cf25b2 AS |
1906 | if (reg && size == BPF_REG_SIZE && register_is_const(reg) && |
1907 | !register_is_null(reg) && env->allow_ptr_leaks) { | |
b5dc0163 AS |
1908 | if (dst_reg != BPF_REG_FP) { |
1909 | /* The backtracking logic can only recognize explicit | |
1910 | * stack slot address like [fp - 8]. Other spill of | |
1911 | * scalar via different register has to be conervative. | |
1912 | * Backtrack from here and mark all registers as precise | |
1913 | * that contributed into 'reg' being a constant. | |
1914 | */ | |
1915 | err = mark_chain_precision(env, value_regno); | |
1916 | if (err) | |
1917 | return err; | |
1918 | } | |
f7cf25b2 AS |
1919 | save_register_state(state, spi, reg); |
1920 | } else if (reg && is_spillable_regtype(reg->type)) { | |
17a52670 | 1921 | /* register containing pointer is being spilled into stack */ |
9c399760 | 1922 | if (size != BPF_REG_SIZE) { |
f7cf25b2 | 1923 | verbose_linfo(env, insn_idx, "; "); |
61bd5218 | 1924 | verbose(env, "invalid size of register spill\n"); |
17a52670 AS |
1925 | return -EACCES; |
1926 | } | |
1927 | ||
f7cf25b2 | 1928 | if (state != cur && reg->type == PTR_TO_STACK) { |
f4d7e40a AS |
1929 | verbose(env, "cannot spill pointers to stack into stack frame of the caller\n"); |
1930 | return -EINVAL; | |
1931 | } | |
1932 | ||
f7cf25b2 AS |
1933 | if (!env->allow_ptr_leaks) { |
1934 | bool sanitize = false; | |
17a52670 | 1935 | |
f7cf25b2 AS |
1936 | if (state->stack[spi].slot_type[0] == STACK_SPILL && |
1937 | register_is_const(&state->stack[spi].spilled_ptr)) | |
1938 | sanitize = true; | |
1939 | for (i = 0; i < BPF_REG_SIZE; i++) | |
1940 | if (state->stack[spi].slot_type[i] == STACK_MISC) { | |
1941 | sanitize = true; | |
1942 | break; | |
1943 | } | |
1944 | if (sanitize) { | |
af86ca4e AS |
1945 | int *poff = &env->insn_aux_data[insn_idx].sanitize_stack_off; |
1946 | int soff = (-spi - 1) * BPF_REG_SIZE; | |
1947 | ||
1948 | /* detected reuse of integer stack slot with a pointer | |
1949 | * which means either llvm is reusing stack slot or | |
1950 | * an attacker is trying to exploit CVE-2018-3639 | |
1951 | * (speculative store bypass) | |
1952 | * Have to sanitize that slot with preemptive | |
1953 | * store of zero. | |
1954 | */ | |
1955 | if (*poff && *poff != soff) { | |
1956 | /* disallow programs where single insn stores | |
1957 | * into two different stack slots, since verifier | |
1958 | * cannot sanitize them | |
1959 | */ | |
1960 | verbose(env, | |
1961 | "insn %d cannot access two stack slots fp%d and fp%d", | |
1962 | insn_idx, *poff, soff); | |
1963 | return -EINVAL; | |
1964 | } | |
1965 | *poff = soff; | |
1966 | } | |
af86ca4e | 1967 | } |
f7cf25b2 | 1968 | save_register_state(state, spi, reg); |
9c399760 | 1969 | } else { |
cc2b14d5 AS |
1970 | u8 type = STACK_MISC; |
1971 | ||
679c782d EC |
1972 | /* regular write of data into stack destroys any spilled ptr */ |
1973 | state->stack[spi].spilled_ptr.type = NOT_INIT; | |
0bae2d4d JW |
1974 | /* Mark slots as STACK_MISC if they belonged to spilled ptr. */ |
1975 | if (state->stack[spi].slot_type[0] == STACK_SPILL) | |
1976 | for (i = 0; i < BPF_REG_SIZE; i++) | |
1977 | state->stack[spi].slot_type[i] = STACK_MISC; | |
9c399760 | 1978 | |
cc2b14d5 AS |
1979 | /* only mark the slot as written if all 8 bytes were written |
1980 | * otherwise read propagation may incorrectly stop too soon | |
1981 | * when stack slots are partially written. | |
1982 | * This heuristic means that read propagation will be | |
1983 | * conservative, since it will add reg_live_read marks | |
1984 | * to stack slots all the way to first state when programs | |
1985 | * writes+reads less than 8 bytes | |
1986 | */ | |
1987 | if (size == BPF_REG_SIZE) | |
1988 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; | |
1989 | ||
1990 | /* when we zero initialize stack slots mark them as such */ | |
b5dc0163 AS |
1991 | if (reg && register_is_null(reg)) { |
1992 | /* backtracking doesn't work for STACK_ZERO yet. */ | |
1993 | err = mark_chain_precision(env, value_regno); | |
1994 | if (err) | |
1995 | return err; | |
cc2b14d5 | 1996 | type = STACK_ZERO; |
b5dc0163 | 1997 | } |
cc2b14d5 | 1998 | |
0bae2d4d | 1999 | /* Mark slots affected by this stack write. */ |
9c399760 | 2000 | for (i = 0; i < size; i++) |
638f5b90 | 2001 | state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] = |
cc2b14d5 | 2002 | type; |
17a52670 AS |
2003 | } |
2004 | return 0; | |
2005 | } | |
2006 | ||
61bd5218 | 2007 | static int check_stack_read(struct bpf_verifier_env *env, |
f4d7e40a AS |
2008 | struct bpf_func_state *reg_state /* func where register points to */, |
2009 | int off, int size, int value_regno) | |
17a52670 | 2010 | { |
f4d7e40a AS |
2011 | struct bpf_verifier_state *vstate = env->cur_state; |
2012 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
638f5b90 | 2013 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE; |
f7cf25b2 | 2014 | struct bpf_reg_state *reg; |
638f5b90 | 2015 | u8 *stype; |
17a52670 | 2016 | |
f4d7e40a | 2017 | if (reg_state->allocated_stack <= slot) { |
638f5b90 AS |
2018 | verbose(env, "invalid read from stack off %d+0 size %d\n", |
2019 | off, size); | |
2020 | return -EACCES; | |
2021 | } | |
f4d7e40a | 2022 | stype = reg_state->stack[spi].slot_type; |
f7cf25b2 | 2023 | reg = ®_state->stack[spi].spilled_ptr; |
17a52670 | 2024 | |
638f5b90 | 2025 | if (stype[0] == STACK_SPILL) { |
9c399760 | 2026 | if (size != BPF_REG_SIZE) { |
f7cf25b2 AS |
2027 | if (reg->type != SCALAR_VALUE) { |
2028 | verbose_linfo(env, env->insn_idx, "; "); | |
2029 | verbose(env, "invalid size of register fill\n"); | |
2030 | return -EACCES; | |
2031 | } | |
2032 | if (value_regno >= 0) { | |
2033 | mark_reg_unknown(env, state->regs, value_regno); | |
2034 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; | |
2035 | } | |
2036 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); | |
2037 | return 0; | |
17a52670 | 2038 | } |
9c399760 | 2039 | for (i = 1; i < BPF_REG_SIZE; i++) { |
638f5b90 | 2040 | if (stype[(slot - i) % BPF_REG_SIZE] != STACK_SPILL) { |
61bd5218 | 2041 | verbose(env, "corrupted spill memory\n"); |
17a52670 AS |
2042 | return -EACCES; |
2043 | } | |
2044 | } | |
2045 | ||
dc503a8a | 2046 | if (value_regno >= 0) { |
17a52670 | 2047 | /* restore register state from stack */ |
f7cf25b2 | 2048 | state->regs[value_regno] = *reg; |
2f18f62e AS |
2049 | /* mark reg as written since spilled pointer state likely |
2050 | * has its liveness marks cleared by is_state_visited() | |
2051 | * which resets stack/reg liveness for state transitions | |
2052 | */ | |
2053 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; | |
dc503a8a | 2054 | } |
f7cf25b2 | 2055 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
17a52670 | 2056 | } else { |
cc2b14d5 AS |
2057 | int zeros = 0; |
2058 | ||
17a52670 | 2059 | for (i = 0; i < size; i++) { |
cc2b14d5 AS |
2060 | if (stype[(slot - i) % BPF_REG_SIZE] == STACK_MISC) |
2061 | continue; | |
2062 | if (stype[(slot - i) % BPF_REG_SIZE] == STACK_ZERO) { | |
2063 | zeros++; | |
2064 | continue; | |
17a52670 | 2065 | } |
cc2b14d5 AS |
2066 | verbose(env, "invalid read from stack off %d+%d size %d\n", |
2067 | off, i, size); | |
2068 | return -EACCES; | |
2069 | } | |
f7cf25b2 | 2070 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
cc2b14d5 AS |
2071 | if (value_regno >= 0) { |
2072 | if (zeros == size) { | |
2073 | /* any size read into register is zero extended, | |
2074 | * so the whole register == const_zero | |
2075 | */ | |
2076 | __mark_reg_const_zero(&state->regs[value_regno]); | |
b5dc0163 AS |
2077 | /* backtracking doesn't support STACK_ZERO yet, |
2078 | * so mark it precise here, so that later | |
2079 | * backtracking can stop here. | |
2080 | * Backtracking may not need this if this register | |
2081 | * doesn't participate in pointer adjustment. | |
2082 | * Forward propagation of precise flag is not | |
2083 | * necessary either. This mark is only to stop | |
2084 | * backtracking. Any register that contributed | |
2085 | * to const 0 was marked precise before spill. | |
2086 | */ | |
2087 | state->regs[value_regno].precise = true; | |
cc2b14d5 AS |
2088 | } else { |
2089 | /* have read misc data from the stack */ | |
2090 | mark_reg_unknown(env, state->regs, value_regno); | |
2091 | } | |
2092 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; | |
17a52670 | 2093 | } |
17a52670 | 2094 | } |
f7cf25b2 | 2095 | return 0; |
17a52670 AS |
2096 | } |
2097 | ||
e4298d25 DB |
2098 | static int check_stack_access(struct bpf_verifier_env *env, |
2099 | const struct bpf_reg_state *reg, | |
2100 | int off, int size) | |
2101 | { | |
2102 | /* Stack accesses must be at a fixed offset, so that we | |
2103 | * can determine what type of data were returned. See | |
2104 | * check_stack_read(). | |
2105 | */ | |
2106 | if (!tnum_is_const(reg->var_off)) { | |
2107 | char tn_buf[48]; | |
2108 | ||
2109 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
1fbd20f8 | 2110 | verbose(env, "variable stack access var_off=%s off=%d size=%d\n", |
e4298d25 DB |
2111 | tn_buf, off, size); |
2112 | return -EACCES; | |
2113 | } | |
2114 | ||
2115 | if (off >= 0 || off < -MAX_BPF_STACK) { | |
2116 | verbose(env, "invalid stack off=%d size=%d\n", off, size); | |
2117 | return -EACCES; | |
2118 | } | |
2119 | ||
2120 | return 0; | |
2121 | } | |
2122 | ||
591fe988 DB |
2123 | static int check_map_access_type(struct bpf_verifier_env *env, u32 regno, |
2124 | int off, int size, enum bpf_access_type type) | |
2125 | { | |
2126 | struct bpf_reg_state *regs = cur_regs(env); | |
2127 | struct bpf_map *map = regs[regno].map_ptr; | |
2128 | u32 cap = bpf_map_flags_to_cap(map); | |
2129 | ||
2130 | if (type == BPF_WRITE && !(cap & BPF_MAP_CAN_WRITE)) { | |
2131 | verbose(env, "write into map forbidden, value_size=%d off=%d size=%d\n", | |
2132 | map->value_size, off, size); | |
2133 | return -EACCES; | |
2134 | } | |
2135 | ||
2136 | if (type == BPF_READ && !(cap & BPF_MAP_CAN_READ)) { | |
2137 | verbose(env, "read from map forbidden, value_size=%d off=%d size=%d\n", | |
2138 | map->value_size, off, size); | |
2139 | return -EACCES; | |
2140 | } | |
2141 | ||
2142 | return 0; | |
2143 | } | |
2144 | ||
17a52670 | 2145 | /* check read/write into map element returned by bpf_map_lookup_elem() */ |
f1174f77 | 2146 | static int __check_map_access(struct bpf_verifier_env *env, u32 regno, int off, |
9fd29c08 | 2147 | int size, bool zero_size_allowed) |
17a52670 | 2148 | { |
638f5b90 AS |
2149 | struct bpf_reg_state *regs = cur_regs(env); |
2150 | struct bpf_map *map = regs[regno].map_ptr; | |
17a52670 | 2151 | |
9fd29c08 YS |
2152 | if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) || |
2153 | off + size > map->value_size) { | |
61bd5218 | 2154 | verbose(env, "invalid access to map value, value_size=%d off=%d size=%d\n", |
17a52670 AS |
2155 | map->value_size, off, size); |
2156 | return -EACCES; | |
2157 | } | |
2158 | return 0; | |
2159 | } | |
2160 | ||
f1174f77 EC |
2161 | /* check read/write into a map element with possible variable offset */ |
2162 | static int check_map_access(struct bpf_verifier_env *env, u32 regno, | |
9fd29c08 | 2163 | int off, int size, bool zero_size_allowed) |
dbcfe5f7 | 2164 | { |
f4d7e40a AS |
2165 | struct bpf_verifier_state *vstate = env->cur_state; |
2166 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
dbcfe5f7 GB |
2167 | struct bpf_reg_state *reg = &state->regs[regno]; |
2168 | int err; | |
2169 | ||
f1174f77 EC |
2170 | /* We may have adjusted the register to this map value, so we |
2171 | * need to try adding each of min_value and max_value to off | |
2172 | * to make sure our theoretical access will be safe. | |
dbcfe5f7 | 2173 | */ |
06ee7115 | 2174 | if (env->log.level & BPF_LOG_LEVEL) |
61bd5218 | 2175 | print_verifier_state(env, state); |
b7137c4e | 2176 | |
dbcfe5f7 GB |
2177 | /* The minimum value is only important with signed |
2178 | * comparisons where we can't assume the floor of a | |
2179 | * value is 0. If we are using signed variables for our | |
2180 | * index'es we need to make sure that whatever we use | |
2181 | * will have a set floor within our range. | |
2182 | */ | |
b7137c4e DB |
2183 | if (reg->smin_value < 0 && |
2184 | (reg->smin_value == S64_MIN || | |
2185 | (off + reg->smin_value != (s64)(s32)(off + reg->smin_value)) || | |
2186 | reg->smin_value + off < 0)) { | |
61bd5218 | 2187 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
dbcfe5f7 GB |
2188 | regno); |
2189 | return -EACCES; | |
2190 | } | |
9fd29c08 YS |
2191 | err = __check_map_access(env, regno, reg->smin_value + off, size, |
2192 | zero_size_allowed); | |
dbcfe5f7 | 2193 | if (err) { |
61bd5218 JK |
2194 | verbose(env, "R%d min value is outside of the array range\n", |
2195 | regno); | |
dbcfe5f7 GB |
2196 | return err; |
2197 | } | |
2198 | ||
b03c9f9f EC |
2199 | /* If we haven't set a max value then we need to bail since we can't be |
2200 | * sure we won't do bad things. | |
2201 | * If reg->umax_value + off could overflow, treat that as unbounded too. | |
dbcfe5f7 | 2202 | */ |
b03c9f9f | 2203 | if (reg->umax_value >= BPF_MAX_VAR_OFF) { |
61bd5218 | 2204 | verbose(env, "R%d unbounded memory access, make sure to bounds check any array access into a map\n", |
dbcfe5f7 GB |
2205 | regno); |
2206 | return -EACCES; | |
2207 | } | |
9fd29c08 YS |
2208 | err = __check_map_access(env, regno, reg->umax_value + off, size, |
2209 | zero_size_allowed); | |
f1174f77 | 2210 | if (err) |
61bd5218 JK |
2211 | verbose(env, "R%d max value is outside of the array range\n", |
2212 | regno); | |
d83525ca AS |
2213 | |
2214 | if (map_value_has_spin_lock(reg->map_ptr)) { | |
2215 | u32 lock = reg->map_ptr->spin_lock_off; | |
2216 | ||
2217 | /* if any part of struct bpf_spin_lock can be touched by | |
2218 | * load/store reject this program. | |
2219 | * To check that [x1, x2) overlaps with [y1, y2) | |
2220 | * it is sufficient to check x1 < y2 && y1 < x2. | |
2221 | */ | |
2222 | if (reg->smin_value + off < lock + sizeof(struct bpf_spin_lock) && | |
2223 | lock < reg->umax_value + off + size) { | |
2224 | verbose(env, "bpf_spin_lock cannot be accessed directly by load/store\n"); | |
2225 | return -EACCES; | |
2226 | } | |
2227 | } | |
f1174f77 | 2228 | return err; |
dbcfe5f7 GB |
2229 | } |
2230 | ||
969bf05e AS |
2231 | #define MAX_PACKET_OFF 0xffff |
2232 | ||
58e2af8b | 2233 | static bool may_access_direct_pkt_data(struct bpf_verifier_env *env, |
3a0af8fd TG |
2234 | const struct bpf_call_arg_meta *meta, |
2235 | enum bpf_access_type t) | |
4acf6c0b | 2236 | { |
36bbef52 | 2237 | switch (env->prog->type) { |
5d66fa7d | 2238 | /* Program types only with direct read access go here! */ |
3a0af8fd TG |
2239 | case BPF_PROG_TYPE_LWT_IN: |
2240 | case BPF_PROG_TYPE_LWT_OUT: | |
004d4b27 | 2241 | case BPF_PROG_TYPE_LWT_SEG6LOCAL: |
2dbb9b9e | 2242 | case BPF_PROG_TYPE_SK_REUSEPORT: |
5d66fa7d | 2243 | case BPF_PROG_TYPE_FLOW_DISSECTOR: |
d5563d36 | 2244 | case BPF_PROG_TYPE_CGROUP_SKB: |
3a0af8fd TG |
2245 | if (t == BPF_WRITE) |
2246 | return false; | |
7e57fbb2 | 2247 | /* fallthrough */ |
5d66fa7d DB |
2248 | |
2249 | /* Program types with direct read + write access go here! */ | |
36bbef52 DB |
2250 | case BPF_PROG_TYPE_SCHED_CLS: |
2251 | case BPF_PROG_TYPE_SCHED_ACT: | |
4acf6c0b | 2252 | case BPF_PROG_TYPE_XDP: |
3a0af8fd | 2253 | case BPF_PROG_TYPE_LWT_XMIT: |
8a31db56 | 2254 | case BPF_PROG_TYPE_SK_SKB: |
4f738adb | 2255 | case BPF_PROG_TYPE_SK_MSG: |
36bbef52 DB |
2256 | if (meta) |
2257 | return meta->pkt_access; | |
2258 | ||
2259 | env->seen_direct_write = true; | |
4acf6c0b | 2260 | return true; |
0d01da6a SF |
2261 | |
2262 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: | |
2263 | if (t == BPF_WRITE) | |
2264 | env->seen_direct_write = true; | |
2265 | ||
2266 | return true; | |
2267 | ||
4acf6c0b BB |
2268 | default: |
2269 | return false; | |
2270 | } | |
2271 | } | |
2272 | ||
f1174f77 | 2273 | static int __check_packet_access(struct bpf_verifier_env *env, u32 regno, |
9fd29c08 | 2274 | int off, int size, bool zero_size_allowed) |
969bf05e | 2275 | { |
638f5b90 | 2276 | struct bpf_reg_state *regs = cur_regs(env); |
58e2af8b | 2277 | struct bpf_reg_state *reg = ®s[regno]; |
969bf05e | 2278 | |
9fd29c08 YS |
2279 | if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) || |
2280 | (u64)off + size > reg->range) { | |
61bd5218 | 2281 | verbose(env, "invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n", |
d91b28ed | 2282 | off, size, regno, reg->id, reg->off, reg->range); |
969bf05e AS |
2283 | return -EACCES; |
2284 | } | |
2285 | return 0; | |
2286 | } | |
2287 | ||
f1174f77 | 2288 | static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off, |
9fd29c08 | 2289 | int size, bool zero_size_allowed) |
f1174f77 | 2290 | { |
638f5b90 | 2291 | struct bpf_reg_state *regs = cur_regs(env); |
f1174f77 EC |
2292 | struct bpf_reg_state *reg = ®s[regno]; |
2293 | int err; | |
2294 | ||
2295 | /* We may have added a variable offset to the packet pointer; but any | |
2296 | * reg->range we have comes after that. We are only checking the fixed | |
2297 | * offset. | |
2298 | */ | |
2299 | ||
2300 | /* We don't allow negative numbers, because we aren't tracking enough | |
2301 | * detail to prove they're safe. | |
2302 | */ | |
b03c9f9f | 2303 | if (reg->smin_value < 0) { |
61bd5218 | 2304 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
f1174f77 EC |
2305 | regno); |
2306 | return -EACCES; | |
2307 | } | |
9fd29c08 | 2308 | err = __check_packet_access(env, regno, off, size, zero_size_allowed); |
f1174f77 | 2309 | if (err) { |
61bd5218 | 2310 | verbose(env, "R%d offset is outside of the packet\n", regno); |
f1174f77 EC |
2311 | return err; |
2312 | } | |
e647815a JW |
2313 | |
2314 | /* __check_packet_access has made sure "off + size - 1" is within u16. | |
2315 | * reg->umax_value can't be bigger than MAX_PACKET_OFF which is 0xffff, | |
2316 | * otherwise find_good_pkt_pointers would have refused to set range info | |
2317 | * that __check_packet_access would have rejected this pkt access. | |
2318 | * Therefore, "off + reg->umax_value + size - 1" won't overflow u32. | |
2319 | */ | |
2320 | env->prog->aux->max_pkt_offset = | |
2321 | max_t(u32, env->prog->aux->max_pkt_offset, | |
2322 | off + reg->umax_value + size - 1); | |
2323 | ||
f1174f77 EC |
2324 | return err; |
2325 | } | |
2326 | ||
2327 | /* check access to 'struct bpf_context' fields. Supports fixed offsets only */ | |
31fd8581 | 2328 | static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off, int size, |
19de99f7 | 2329 | enum bpf_access_type t, enum bpf_reg_type *reg_type) |
17a52670 | 2330 | { |
f96da094 DB |
2331 | struct bpf_insn_access_aux info = { |
2332 | .reg_type = *reg_type, | |
2333 | }; | |
31fd8581 | 2334 | |
4f9218aa | 2335 | if (env->ops->is_valid_access && |
5e43f899 | 2336 | env->ops->is_valid_access(off, size, t, env->prog, &info)) { |
f96da094 DB |
2337 | /* A non zero info.ctx_field_size indicates that this field is a |
2338 | * candidate for later verifier transformation to load the whole | |
2339 | * field and then apply a mask when accessed with a narrower | |
2340 | * access than actual ctx access size. A zero info.ctx_field_size | |
2341 | * will only allow for whole field access and rejects any other | |
2342 | * type of narrower access. | |
31fd8581 | 2343 | */ |
23994631 | 2344 | *reg_type = info.reg_type; |
31fd8581 | 2345 | |
4f9218aa | 2346 | env->insn_aux_data[insn_idx].ctx_field_size = info.ctx_field_size; |
32bbe007 AS |
2347 | /* remember the offset of last byte accessed in ctx */ |
2348 | if (env->prog->aux->max_ctx_offset < off + size) | |
2349 | env->prog->aux->max_ctx_offset = off + size; | |
17a52670 | 2350 | return 0; |
32bbe007 | 2351 | } |
17a52670 | 2352 | |
61bd5218 | 2353 | verbose(env, "invalid bpf_context access off=%d size=%d\n", off, size); |
17a52670 AS |
2354 | return -EACCES; |
2355 | } | |
2356 | ||
d58e468b PP |
2357 | static int check_flow_keys_access(struct bpf_verifier_env *env, int off, |
2358 | int size) | |
2359 | { | |
2360 | if (size < 0 || off < 0 || | |
2361 | (u64)off + size > sizeof(struct bpf_flow_keys)) { | |
2362 | verbose(env, "invalid access to flow keys off=%d size=%d\n", | |
2363 | off, size); | |
2364 | return -EACCES; | |
2365 | } | |
2366 | return 0; | |
2367 | } | |
2368 | ||
5f456649 MKL |
2369 | static int check_sock_access(struct bpf_verifier_env *env, int insn_idx, |
2370 | u32 regno, int off, int size, | |
2371 | enum bpf_access_type t) | |
c64b7983 JS |
2372 | { |
2373 | struct bpf_reg_state *regs = cur_regs(env); | |
2374 | struct bpf_reg_state *reg = ®s[regno]; | |
5f456649 | 2375 | struct bpf_insn_access_aux info = {}; |
46f8bc92 | 2376 | bool valid; |
c64b7983 JS |
2377 | |
2378 | if (reg->smin_value < 0) { | |
2379 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", | |
2380 | regno); | |
2381 | return -EACCES; | |
2382 | } | |
2383 | ||
46f8bc92 MKL |
2384 | switch (reg->type) { |
2385 | case PTR_TO_SOCK_COMMON: | |
2386 | valid = bpf_sock_common_is_valid_access(off, size, t, &info); | |
2387 | break; | |
2388 | case PTR_TO_SOCKET: | |
2389 | valid = bpf_sock_is_valid_access(off, size, t, &info); | |
2390 | break; | |
655a51e5 MKL |
2391 | case PTR_TO_TCP_SOCK: |
2392 | valid = bpf_tcp_sock_is_valid_access(off, size, t, &info); | |
2393 | break; | |
fada7fdc JL |
2394 | case PTR_TO_XDP_SOCK: |
2395 | valid = bpf_xdp_sock_is_valid_access(off, size, t, &info); | |
2396 | break; | |
46f8bc92 MKL |
2397 | default: |
2398 | valid = false; | |
c64b7983 JS |
2399 | } |
2400 | ||
5f456649 | 2401 | |
46f8bc92 MKL |
2402 | if (valid) { |
2403 | env->insn_aux_data[insn_idx].ctx_field_size = | |
2404 | info.ctx_field_size; | |
2405 | return 0; | |
2406 | } | |
2407 | ||
2408 | verbose(env, "R%d invalid %s access off=%d size=%d\n", | |
2409 | regno, reg_type_str[reg->type], off, size); | |
2410 | ||
2411 | return -EACCES; | |
c64b7983 JS |
2412 | } |
2413 | ||
4cabc5b1 DB |
2414 | static bool __is_pointer_value(bool allow_ptr_leaks, |
2415 | const struct bpf_reg_state *reg) | |
1be7f75d | 2416 | { |
4cabc5b1 | 2417 | if (allow_ptr_leaks) |
1be7f75d AS |
2418 | return false; |
2419 | ||
f1174f77 | 2420 | return reg->type != SCALAR_VALUE; |
1be7f75d AS |
2421 | } |
2422 | ||
2a159c6f DB |
2423 | static struct bpf_reg_state *reg_state(struct bpf_verifier_env *env, int regno) |
2424 | { | |
2425 | return cur_regs(env) + regno; | |
2426 | } | |
2427 | ||
4cabc5b1 DB |
2428 | static bool is_pointer_value(struct bpf_verifier_env *env, int regno) |
2429 | { | |
2a159c6f | 2430 | return __is_pointer_value(env->allow_ptr_leaks, reg_state(env, regno)); |
4cabc5b1 DB |
2431 | } |
2432 | ||
f37a8cb8 DB |
2433 | static bool is_ctx_reg(struct bpf_verifier_env *env, int regno) |
2434 | { | |
2a159c6f | 2435 | const struct bpf_reg_state *reg = reg_state(env, regno); |
f37a8cb8 | 2436 | |
46f8bc92 MKL |
2437 | return reg->type == PTR_TO_CTX; |
2438 | } | |
2439 | ||
2440 | static bool is_sk_reg(struct bpf_verifier_env *env, int regno) | |
2441 | { | |
2442 | const struct bpf_reg_state *reg = reg_state(env, regno); | |
2443 | ||
2444 | return type_is_sk_pointer(reg->type); | |
f37a8cb8 DB |
2445 | } |
2446 | ||
ca369602 DB |
2447 | static bool is_pkt_reg(struct bpf_verifier_env *env, int regno) |
2448 | { | |
2a159c6f | 2449 | const struct bpf_reg_state *reg = reg_state(env, regno); |
ca369602 DB |
2450 | |
2451 | return type_is_pkt_pointer(reg->type); | |
2452 | } | |
2453 | ||
4b5defde DB |
2454 | static bool is_flow_key_reg(struct bpf_verifier_env *env, int regno) |
2455 | { | |
2456 | const struct bpf_reg_state *reg = reg_state(env, regno); | |
2457 | ||
2458 | /* Separate to is_ctx_reg() since we still want to allow BPF_ST here. */ | |
2459 | return reg->type == PTR_TO_FLOW_KEYS; | |
2460 | } | |
2461 | ||
61bd5218 JK |
2462 | static int check_pkt_ptr_alignment(struct bpf_verifier_env *env, |
2463 | const struct bpf_reg_state *reg, | |
d1174416 | 2464 | int off, int size, bool strict) |
969bf05e | 2465 | { |
f1174f77 | 2466 | struct tnum reg_off; |
e07b98d9 | 2467 | int ip_align; |
d1174416 DM |
2468 | |
2469 | /* Byte size accesses are always allowed. */ | |
2470 | if (!strict || size == 1) | |
2471 | return 0; | |
2472 | ||
e4eda884 DM |
2473 | /* For platforms that do not have a Kconfig enabling |
2474 | * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of | |
2475 | * NET_IP_ALIGN is universally set to '2'. And on platforms | |
2476 | * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get | |
2477 | * to this code only in strict mode where we want to emulate | |
2478 | * the NET_IP_ALIGN==2 checking. Therefore use an | |
2479 | * unconditional IP align value of '2'. | |
e07b98d9 | 2480 | */ |
e4eda884 | 2481 | ip_align = 2; |
f1174f77 EC |
2482 | |
2483 | reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off)); | |
2484 | if (!tnum_is_aligned(reg_off, size)) { | |
2485 | char tn_buf[48]; | |
2486 | ||
2487 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
61bd5218 JK |
2488 | verbose(env, |
2489 | "misaligned packet access off %d+%s+%d+%d size %d\n", | |
f1174f77 | 2490 | ip_align, tn_buf, reg->off, off, size); |
969bf05e AS |
2491 | return -EACCES; |
2492 | } | |
79adffcd | 2493 | |
969bf05e AS |
2494 | return 0; |
2495 | } | |
2496 | ||
61bd5218 JK |
2497 | static int check_generic_ptr_alignment(struct bpf_verifier_env *env, |
2498 | const struct bpf_reg_state *reg, | |
f1174f77 EC |
2499 | const char *pointer_desc, |
2500 | int off, int size, bool strict) | |
79adffcd | 2501 | { |
f1174f77 EC |
2502 | struct tnum reg_off; |
2503 | ||
2504 | /* Byte size accesses are always allowed. */ | |
2505 | if (!strict || size == 1) | |
2506 | return 0; | |
2507 | ||
2508 | reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off)); | |
2509 | if (!tnum_is_aligned(reg_off, size)) { | |
2510 | char tn_buf[48]; | |
2511 | ||
2512 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
61bd5218 | 2513 | verbose(env, "misaligned %saccess off %s+%d+%d size %d\n", |
f1174f77 | 2514 | pointer_desc, tn_buf, reg->off, off, size); |
79adffcd DB |
2515 | return -EACCES; |
2516 | } | |
2517 | ||
969bf05e AS |
2518 | return 0; |
2519 | } | |
2520 | ||
e07b98d9 | 2521 | static int check_ptr_alignment(struct bpf_verifier_env *env, |
ca369602 DB |
2522 | const struct bpf_reg_state *reg, int off, |
2523 | int size, bool strict_alignment_once) | |
79adffcd | 2524 | { |
ca369602 | 2525 | bool strict = env->strict_alignment || strict_alignment_once; |
f1174f77 | 2526 | const char *pointer_desc = ""; |
d1174416 | 2527 | |
79adffcd DB |
2528 | switch (reg->type) { |
2529 | case PTR_TO_PACKET: | |
de8f3a83 DB |
2530 | case PTR_TO_PACKET_META: |
2531 | /* Special case, because of NET_IP_ALIGN. Given metadata sits | |
2532 | * right in front, treat it the very same way. | |
2533 | */ | |
61bd5218 | 2534 | return check_pkt_ptr_alignment(env, reg, off, size, strict); |
d58e468b PP |
2535 | case PTR_TO_FLOW_KEYS: |
2536 | pointer_desc = "flow keys "; | |
2537 | break; | |
f1174f77 EC |
2538 | case PTR_TO_MAP_VALUE: |
2539 | pointer_desc = "value "; | |
2540 | break; | |
2541 | case PTR_TO_CTX: | |
2542 | pointer_desc = "context "; | |
2543 | break; | |
2544 | case PTR_TO_STACK: | |
2545 | pointer_desc = "stack "; | |
a5ec6ae1 JH |
2546 | /* The stack spill tracking logic in check_stack_write() |
2547 | * and check_stack_read() relies on stack accesses being | |
2548 | * aligned. | |
2549 | */ | |
2550 | strict = true; | |
f1174f77 | 2551 | break; |
c64b7983 JS |
2552 | case PTR_TO_SOCKET: |
2553 | pointer_desc = "sock "; | |
2554 | break; | |
46f8bc92 MKL |
2555 | case PTR_TO_SOCK_COMMON: |
2556 | pointer_desc = "sock_common "; | |
2557 | break; | |
655a51e5 MKL |
2558 | case PTR_TO_TCP_SOCK: |
2559 | pointer_desc = "tcp_sock "; | |
2560 | break; | |
fada7fdc JL |
2561 | case PTR_TO_XDP_SOCK: |
2562 | pointer_desc = "xdp_sock "; | |
2563 | break; | |
79adffcd | 2564 | default: |
f1174f77 | 2565 | break; |
79adffcd | 2566 | } |
61bd5218 JK |
2567 | return check_generic_ptr_alignment(env, reg, pointer_desc, off, size, |
2568 | strict); | |
79adffcd DB |
2569 | } |
2570 | ||
f4d7e40a AS |
2571 | static int update_stack_depth(struct bpf_verifier_env *env, |
2572 | const struct bpf_func_state *func, | |
2573 | int off) | |
2574 | { | |
9c8105bd | 2575 | u16 stack = env->subprog_info[func->subprogno].stack_depth; |
f4d7e40a AS |
2576 | |
2577 | if (stack >= -off) | |
2578 | return 0; | |
2579 | ||
2580 | /* update known max for given subprogram */ | |
9c8105bd | 2581 | env->subprog_info[func->subprogno].stack_depth = -off; |
70a87ffe AS |
2582 | return 0; |
2583 | } | |
f4d7e40a | 2584 | |
70a87ffe AS |
2585 | /* starting from main bpf function walk all instructions of the function |
2586 | * and recursively walk all callees that given function can call. | |
2587 | * Ignore jump and exit insns. | |
2588 | * Since recursion is prevented by check_cfg() this algorithm | |
2589 | * only needs a local stack of MAX_CALL_FRAMES to remember callsites | |
2590 | */ | |
2591 | static int check_max_stack_depth(struct bpf_verifier_env *env) | |
2592 | { | |
9c8105bd JW |
2593 | int depth = 0, frame = 0, idx = 0, i = 0, subprog_end; |
2594 | struct bpf_subprog_info *subprog = env->subprog_info; | |
70a87ffe | 2595 | struct bpf_insn *insn = env->prog->insnsi; |
70a87ffe AS |
2596 | int ret_insn[MAX_CALL_FRAMES]; |
2597 | int ret_prog[MAX_CALL_FRAMES]; | |
f4d7e40a | 2598 | |
70a87ffe AS |
2599 | process_func: |
2600 | /* round up to 32-bytes, since this is granularity | |
2601 | * of interpreter stack size | |
2602 | */ | |
9c8105bd | 2603 | depth += round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
70a87ffe | 2604 | if (depth > MAX_BPF_STACK) { |
f4d7e40a | 2605 | verbose(env, "combined stack size of %d calls is %d. Too large\n", |
70a87ffe | 2606 | frame + 1, depth); |
f4d7e40a AS |
2607 | return -EACCES; |
2608 | } | |
70a87ffe | 2609 | continue_func: |
4cb3d99c | 2610 | subprog_end = subprog[idx + 1].start; |
70a87ffe AS |
2611 | for (; i < subprog_end; i++) { |
2612 | if (insn[i].code != (BPF_JMP | BPF_CALL)) | |
2613 | continue; | |
2614 | if (insn[i].src_reg != BPF_PSEUDO_CALL) | |
2615 | continue; | |
2616 | /* remember insn and function to return to */ | |
2617 | ret_insn[frame] = i + 1; | |
9c8105bd | 2618 | ret_prog[frame] = idx; |
70a87ffe AS |
2619 | |
2620 | /* find the callee */ | |
2621 | i = i + insn[i].imm + 1; | |
9c8105bd JW |
2622 | idx = find_subprog(env, i); |
2623 | if (idx < 0) { | |
70a87ffe AS |
2624 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
2625 | i); | |
2626 | return -EFAULT; | |
2627 | } | |
70a87ffe AS |
2628 | frame++; |
2629 | if (frame >= MAX_CALL_FRAMES) { | |
927cb781 PC |
2630 | verbose(env, "the call stack of %d frames is too deep !\n", |
2631 | frame); | |
2632 | return -E2BIG; | |
70a87ffe AS |
2633 | } |
2634 | goto process_func; | |
2635 | } | |
2636 | /* end of for() loop means the last insn of the 'subprog' | |
2637 | * was reached. Doesn't matter whether it was JA or EXIT | |
2638 | */ | |
2639 | if (frame == 0) | |
2640 | return 0; | |
9c8105bd | 2641 | depth -= round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
70a87ffe AS |
2642 | frame--; |
2643 | i = ret_insn[frame]; | |
9c8105bd | 2644 | idx = ret_prog[frame]; |
70a87ffe | 2645 | goto continue_func; |
f4d7e40a AS |
2646 | } |
2647 | ||
19d28fbd | 2648 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
1ea47e01 AS |
2649 | static int get_callee_stack_depth(struct bpf_verifier_env *env, |
2650 | const struct bpf_insn *insn, int idx) | |
2651 | { | |
2652 | int start = idx + insn->imm + 1, subprog; | |
2653 | ||
2654 | subprog = find_subprog(env, start); | |
2655 | if (subprog < 0) { | |
2656 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", | |
2657 | start); | |
2658 | return -EFAULT; | |
2659 | } | |
9c8105bd | 2660 | return env->subprog_info[subprog].stack_depth; |
1ea47e01 | 2661 | } |
19d28fbd | 2662 | #endif |
1ea47e01 | 2663 | |
58990d1f DB |
2664 | static int check_ctx_reg(struct bpf_verifier_env *env, |
2665 | const struct bpf_reg_state *reg, int regno) | |
2666 | { | |
2667 | /* Access to ctx or passing it to a helper is only allowed in | |
2668 | * its original, unmodified form. | |
2669 | */ | |
2670 | ||
2671 | if (reg->off) { | |
2672 | verbose(env, "dereference of modified ctx ptr R%d off=%d disallowed\n", | |
2673 | regno, reg->off); | |
2674 | return -EACCES; | |
2675 | } | |
2676 | ||
2677 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
2678 | char tn_buf[48]; | |
2679 | ||
2680 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
2681 | verbose(env, "variable ctx access var_off=%s disallowed\n", tn_buf); | |
2682 | return -EACCES; | |
2683 | } | |
2684 | ||
2685 | return 0; | |
2686 | } | |
2687 | ||
9df1c28b MM |
2688 | static int check_tp_buffer_access(struct bpf_verifier_env *env, |
2689 | const struct bpf_reg_state *reg, | |
2690 | int regno, int off, int size) | |
2691 | { | |
2692 | if (off < 0) { | |
2693 | verbose(env, | |
2694 | "R%d invalid tracepoint buffer access: off=%d, size=%d", | |
2695 | regno, off, size); | |
2696 | return -EACCES; | |
2697 | } | |
2698 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
2699 | char tn_buf[48]; | |
2700 | ||
2701 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
2702 | verbose(env, | |
2703 | "R%d invalid variable buffer offset: off=%d, var_off=%s", | |
2704 | regno, off, tn_buf); | |
2705 | return -EACCES; | |
2706 | } | |
2707 | if (off + size > env->prog->aux->max_tp_access) | |
2708 | env->prog->aux->max_tp_access = off + size; | |
2709 | ||
2710 | return 0; | |
2711 | } | |
2712 | ||
2713 | ||
0c17d1d2 JH |
2714 | /* truncate register to smaller size (in bytes) |
2715 | * must be called with size < BPF_REG_SIZE | |
2716 | */ | |
2717 | static void coerce_reg_to_size(struct bpf_reg_state *reg, int size) | |
2718 | { | |
2719 | u64 mask; | |
2720 | ||
2721 | /* clear high bits in bit representation */ | |
2722 | reg->var_off = tnum_cast(reg->var_off, size); | |
2723 | ||
2724 | /* fix arithmetic bounds */ | |
2725 | mask = ((u64)1 << (size * 8)) - 1; | |
2726 | if ((reg->umin_value & ~mask) == (reg->umax_value & ~mask)) { | |
2727 | reg->umin_value &= mask; | |
2728 | reg->umax_value &= mask; | |
2729 | } else { | |
2730 | reg->umin_value = 0; | |
2731 | reg->umax_value = mask; | |
2732 | } | |
2733 | reg->smin_value = reg->umin_value; | |
2734 | reg->smax_value = reg->umax_value; | |
2735 | } | |
2736 | ||
17a52670 AS |
2737 | /* check whether memory at (regno + off) is accessible for t = (read | write) |
2738 | * if t==write, value_regno is a register which value is stored into memory | |
2739 | * if t==read, value_regno is a register which will receive the value from memory | |
2740 | * if t==write && value_regno==-1, some unknown value is stored into memory | |
2741 | * if t==read && value_regno==-1, don't care what we read from memory | |
2742 | */ | |
ca369602 DB |
2743 | static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno, |
2744 | int off, int bpf_size, enum bpf_access_type t, | |
2745 | int value_regno, bool strict_alignment_once) | |
17a52670 | 2746 | { |
638f5b90 AS |
2747 | struct bpf_reg_state *regs = cur_regs(env); |
2748 | struct bpf_reg_state *reg = regs + regno; | |
f4d7e40a | 2749 | struct bpf_func_state *state; |
17a52670 AS |
2750 | int size, err = 0; |
2751 | ||
2752 | size = bpf_size_to_bytes(bpf_size); | |
2753 | if (size < 0) | |
2754 | return size; | |
2755 | ||
f1174f77 | 2756 | /* alignment checks will add in reg->off themselves */ |
ca369602 | 2757 | err = check_ptr_alignment(env, reg, off, size, strict_alignment_once); |
969bf05e AS |
2758 | if (err) |
2759 | return err; | |
17a52670 | 2760 | |
f1174f77 EC |
2761 | /* for access checks, reg->off is just part of off */ |
2762 | off += reg->off; | |
2763 | ||
2764 | if (reg->type == PTR_TO_MAP_VALUE) { | |
1be7f75d AS |
2765 | if (t == BPF_WRITE && value_regno >= 0 && |
2766 | is_pointer_value(env, value_regno)) { | |
61bd5218 | 2767 | verbose(env, "R%d leaks addr into map\n", value_regno); |
1be7f75d AS |
2768 | return -EACCES; |
2769 | } | |
591fe988 DB |
2770 | err = check_map_access_type(env, regno, off, size, t); |
2771 | if (err) | |
2772 | return err; | |
9fd29c08 | 2773 | err = check_map_access(env, regno, off, size, false); |
17a52670 | 2774 | if (!err && t == BPF_READ && value_regno >= 0) |
638f5b90 | 2775 | mark_reg_unknown(env, regs, value_regno); |
17a52670 | 2776 | |
1a0dc1ac | 2777 | } else if (reg->type == PTR_TO_CTX) { |
f1174f77 | 2778 | enum bpf_reg_type reg_type = SCALAR_VALUE; |
19de99f7 | 2779 | |
1be7f75d AS |
2780 | if (t == BPF_WRITE && value_regno >= 0 && |
2781 | is_pointer_value(env, value_regno)) { | |
61bd5218 | 2782 | verbose(env, "R%d leaks addr into ctx\n", value_regno); |
1be7f75d AS |
2783 | return -EACCES; |
2784 | } | |
f1174f77 | 2785 | |
58990d1f DB |
2786 | err = check_ctx_reg(env, reg, regno); |
2787 | if (err < 0) | |
2788 | return err; | |
2789 | ||
31fd8581 | 2790 | err = check_ctx_access(env, insn_idx, off, size, t, ®_type); |
969bf05e | 2791 | if (!err && t == BPF_READ && value_regno >= 0) { |
f1174f77 | 2792 | /* ctx access returns either a scalar, or a |
de8f3a83 DB |
2793 | * PTR_TO_PACKET[_META,_END]. In the latter |
2794 | * case, we know the offset is zero. | |
f1174f77 | 2795 | */ |
46f8bc92 | 2796 | if (reg_type == SCALAR_VALUE) { |
638f5b90 | 2797 | mark_reg_unknown(env, regs, value_regno); |
46f8bc92 | 2798 | } else { |
638f5b90 | 2799 | mark_reg_known_zero(env, regs, |
61bd5218 | 2800 | value_regno); |
46f8bc92 MKL |
2801 | if (reg_type_may_be_null(reg_type)) |
2802 | regs[value_regno].id = ++env->id_gen; | |
5327ed3d JW |
2803 | /* A load of ctx field could have different |
2804 | * actual load size with the one encoded in the | |
2805 | * insn. When the dst is PTR, it is for sure not | |
2806 | * a sub-register. | |
2807 | */ | |
2808 | regs[value_regno].subreg_def = DEF_NOT_SUBREG; | |
46f8bc92 | 2809 | } |
638f5b90 | 2810 | regs[value_regno].type = reg_type; |
969bf05e | 2811 | } |
17a52670 | 2812 | |
f1174f77 | 2813 | } else if (reg->type == PTR_TO_STACK) { |
f1174f77 | 2814 | off += reg->var_off.value; |
e4298d25 DB |
2815 | err = check_stack_access(env, reg, off, size); |
2816 | if (err) | |
2817 | return err; | |
8726679a | 2818 | |
f4d7e40a AS |
2819 | state = func(env, reg); |
2820 | err = update_stack_depth(env, state, off); | |
2821 | if (err) | |
2822 | return err; | |
8726679a | 2823 | |
638f5b90 | 2824 | if (t == BPF_WRITE) |
61bd5218 | 2825 | err = check_stack_write(env, state, off, size, |
af86ca4e | 2826 | value_regno, insn_idx); |
638f5b90 | 2827 | else |
61bd5218 JK |
2828 | err = check_stack_read(env, state, off, size, |
2829 | value_regno); | |
de8f3a83 | 2830 | } else if (reg_is_pkt_pointer(reg)) { |
3a0af8fd | 2831 | if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) { |
61bd5218 | 2832 | verbose(env, "cannot write into packet\n"); |
969bf05e AS |
2833 | return -EACCES; |
2834 | } | |
4acf6c0b BB |
2835 | if (t == BPF_WRITE && value_regno >= 0 && |
2836 | is_pointer_value(env, value_regno)) { | |
61bd5218 JK |
2837 | verbose(env, "R%d leaks addr into packet\n", |
2838 | value_regno); | |
4acf6c0b BB |
2839 | return -EACCES; |
2840 | } | |
9fd29c08 | 2841 | err = check_packet_access(env, regno, off, size, false); |
969bf05e | 2842 | if (!err && t == BPF_READ && value_regno >= 0) |
638f5b90 | 2843 | mark_reg_unknown(env, regs, value_regno); |
d58e468b PP |
2844 | } else if (reg->type == PTR_TO_FLOW_KEYS) { |
2845 | if (t == BPF_WRITE && value_regno >= 0 && | |
2846 | is_pointer_value(env, value_regno)) { | |
2847 | verbose(env, "R%d leaks addr into flow keys\n", | |
2848 | value_regno); | |
2849 | return -EACCES; | |
2850 | } | |
2851 | ||
2852 | err = check_flow_keys_access(env, off, size); | |
2853 | if (!err && t == BPF_READ && value_regno >= 0) | |
2854 | mark_reg_unknown(env, regs, value_regno); | |
46f8bc92 | 2855 | } else if (type_is_sk_pointer(reg->type)) { |
c64b7983 | 2856 | if (t == BPF_WRITE) { |
46f8bc92 MKL |
2857 | verbose(env, "R%d cannot write into %s\n", |
2858 | regno, reg_type_str[reg->type]); | |
c64b7983 JS |
2859 | return -EACCES; |
2860 | } | |
5f456649 | 2861 | err = check_sock_access(env, insn_idx, regno, off, size, t); |
c64b7983 JS |
2862 | if (!err && value_regno >= 0) |
2863 | mark_reg_unknown(env, regs, value_regno); | |
9df1c28b MM |
2864 | } else if (reg->type == PTR_TO_TP_BUFFER) { |
2865 | err = check_tp_buffer_access(env, reg, regno, off, size); | |
2866 | if (!err && t == BPF_READ && value_regno >= 0) | |
2867 | mark_reg_unknown(env, regs, value_regno); | |
17a52670 | 2868 | } else { |
61bd5218 JK |
2869 | verbose(env, "R%d invalid mem access '%s'\n", regno, |
2870 | reg_type_str[reg->type]); | |
17a52670 AS |
2871 | return -EACCES; |
2872 | } | |
969bf05e | 2873 | |
f1174f77 | 2874 | if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ && |
638f5b90 | 2875 | regs[value_regno].type == SCALAR_VALUE) { |
f1174f77 | 2876 | /* b/h/w load zero-extends, mark upper bits as known 0 */ |
0c17d1d2 | 2877 | coerce_reg_to_size(®s[value_regno], size); |
969bf05e | 2878 | } |
17a52670 AS |
2879 | return err; |
2880 | } | |
2881 | ||
31fd8581 | 2882 | static int check_xadd(struct bpf_verifier_env *env, int insn_idx, struct bpf_insn *insn) |
17a52670 | 2883 | { |
17a52670 AS |
2884 | int err; |
2885 | ||
2886 | if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) || | |
2887 | insn->imm != 0) { | |
61bd5218 | 2888 | verbose(env, "BPF_XADD uses reserved fields\n"); |
17a52670 AS |
2889 | return -EINVAL; |
2890 | } | |
2891 | ||
2892 | /* check src1 operand */ | |
dc503a8a | 2893 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
2894 | if (err) |
2895 | return err; | |
2896 | ||
2897 | /* check src2 operand */ | |
dc503a8a | 2898 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
2899 | if (err) |
2900 | return err; | |
2901 | ||
6bdf6abc | 2902 | if (is_pointer_value(env, insn->src_reg)) { |
61bd5218 | 2903 | verbose(env, "R%d leaks addr into mem\n", insn->src_reg); |
6bdf6abc DB |
2904 | return -EACCES; |
2905 | } | |
2906 | ||
ca369602 | 2907 | if (is_ctx_reg(env, insn->dst_reg) || |
4b5defde | 2908 | is_pkt_reg(env, insn->dst_reg) || |
46f8bc92 MKL |
2909 | is_flow_key_reg(env, insn->dst_reg) || |
2910 | is_sk_reg(env, insn->dst_reg)) { | |
ca369602 | 2911 | verbose(env, "BPF_XADD stores into R%d %s is not allowed\n", |
2a159c6f DB |
2912 | insn->dst_reg, |
2913 | reg_type_str[reg_state(env, insn->dst_reg)->type]); | |
f37a8cb8 DB |
2914 | return -EACCES; |
2915 | } | |
2916 | ||
17a52670 | 2917 | /* check whether atomic_add can read the memory */ |
31fd8581 | 2918 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
ca369602 | 2919 | BPF_SIZE(insn->code), BPF_READ, -1, true); |
17a52670 AS |
2920 | if (err) |
2921 | return err; | |
2922 | ||
2923 | /* check whether atomic_add can write into the same memory */ | |
31fd8581 | 2924 | return check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
ca369602 | 2925 | BPF_SIZE(insn->code), BPF_WRITE, -1, true); |
17a52670 AS |
2926 | } |
2927 | ||
2011fccf AI |
2928 | static int __check_stack_boundary(struct bpf_verifier_env *env, u32 regno, |
2929 | int off, int access_size, | |
2930 | bool zero_size_allowed) | |
2931 | { | |
2932 | struct bpf_reg_state *reg = reg_state(env, regno); | |
2933 | ||
2934 | if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 || | |
2935 | access_size < 0 || (access_size == 0 && !zero_size_allowed)) { | |
2936 | if (tnum_is_const(reg->var_off)) { | |
2937 | verbose(env, "invalid stack type R%d off=%d access_size=%d\n", | |
2938 | regno, off, access_size); | |
2939 | } else { | |
2940 | char tn_buf[48]; | |
2941 | ||
2942 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
2943 | verbose(env, "invalid stack type R%d var_off=%s access_size=%d\n", | |
2944 | regno, tn_buf, access_size); | |
2945 | } | |
2946 | return -EACCES; | |
2947 | } | |
2948 | return 0; | |
2949 | } | |
2950 | ||
17a52670 AS |
2951 | /* when register 'regno' is passed into function that will read 'access_size' |
2952 | * bytes from that pointer, make sure that it's within stack boundary | |
f1174f77 EC |
2953 | * and all elements of stack are initialized. |
2954 | * Unlike most pointer bounds-checking functions, this one doesn't take an | |
2955 | * 'off' argument, so it has to add in reg->off itself. | |
17a52670 | 2956 | */ |
58e2af8b | 2957 | static int check_stack_boundary(struct bpf_verifier_env *env, int regno, |
435faee1 DB |
2958 | int access_size, bool zero_size_allowed, |
2959 | struct bpf_call_arg_meta *meta) | |
17a52670 | 2960 | { |
2a159c6f | 2961 | struct bpf_reg_state *reg = reg_state(env, regno); |
f4d7e40a | 2962 | struct bpf_func_state *state = func(env, reg); |
f7cf25b2 | 2963 | int err, min_off, max_off, i, j, slot, spi; |
17a52670 | 2964 | |
914cb781 | 2965 | if (reg->type != PTR_TO_STACK) { |
f1174f77 | 2966 | /* Allow zero-byte read from NULL, regardless of pointer type */ |
8e2fe1d9 | 2967 | if (zero_size_allowed && access_size == 0 && |
914cb781 | 2968 | register_is_null(reg)) |
8e2fe1d9 DB |
2969 | return 0; |
2970 | ||
61bd5218 | 2971 | verbose(env, "R%d type=%s expected=%s\n", regno, |
914cb781 | 2972 | reg_type_str[reg->type], |
8e2fe1d9 | 2973 | reg_type_str[PTR_TO_STACK]); |
17a52670 | 2974 | return -EACCES; |
8e2fe1d9 | 2975 | } |
17a52670 | 2976 | |
2011fccf AI |
2977 | if (tnum_is_const(reg->var_off)) { |
2978 | min_off = max_off = reg->var_off.value + reg->off; | |
2979 | err = __check_stack_boundary(env, regno, min_off, access_size, | |
2980 | zero_size_allowed); | |
2981 | if (err) | |
2982 | return err; | |
2983 | } else { | |
088ec26d AI |
2984 | /* Variable offset is prohibited for unprivileged mode for |
2985 | * simplicity since it requires corresponding support in | |
2986 | * Spectre masking for stack ALU. | |
2987 | * See also retrieve_ptr_limit(). | |
2988 | */ | |
2989 | if (!env->allow_ptr_leaks) { | |
2990 | char tn_buf[48]; | |
f1174f77 | 2991 | |
088ec26d AI |
2992 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
2993 | verbose(env, "R%d indirect variable offset stack access prohibited for !root, var_off=%s\n", | |
2994 | regno, tn_buf); | |
2995 | return -EACCES; | |
2996 | } | |
f2bcd05e AI |
2997 | /* Only initialized buffer on stack is allowed to be accessed |
2998 | * with variable offset. With uninitialized buffer it's hard to | |
2999 | * guarantee that whole memory is marked as initialized on | |
3000 | * helper return since specific bounds are unknown what may | |
3001 | * cause uninitialized stack leaking. | |
3002 | */ | |
3003 | if (meta && meta->raw_mode) | |
3004 | meta = NULL; | |
3005 | ||
107c26a7 AI |
3006 | if (reg->smax_value >= BPF_MAX_VAR_OFF || |
3007 | reg->smax_value <= -BPF_MAX_VAR_OFF) { | |
3008 | verbose(env, "R%d unbounded indirect variable offset stack access\n", | |
3009 | regno); | |
3010 | return -EACCES; | |
3011 | } | |
2011fccf | 3012 | min_off = reg->smin_value + reg->off; |
107c26a7 | 3013 | max_off = reg->smax_value + reg->off; |
2011fccf AI |
3014 | err = __check_stack_boundary(env, regno, min_off, access_size, |
3015 | zero_size_allowed); | |
107c26a7 AI |
3016 | if (err) { |
3017 | verbose(env, "R%d min value is outside of stack bound\n", | |
3018 | regno); | |
2011fccf | 3019 | return err; |
107c26a7 | 3020 | } |
2011fccf AI |
3021 | err = __check_stack_boundary(env, regno, max_off, access_size, |
3022 | zero_size_allowed); | |
107c26a7 AI |
3023 | if (err) { |
3024 | verbose(env, "R%d max value is outside of stack bound\n", | |
3025 | regno); | |
2011fccf | 3026 | return err; |
107c26a7 | 3027 | } |
17a52670 AS |
3028 | } |
3029 | ||
435faee1 DB |
3030 | if (meta && meta->raw_mode) { |
3031 | meta->access_size = access_size; | |
3032 | meta->regno = regno; | |
3033 | return 0; | |
3034 | } | |
3035 | ||
2011fccf | 3036 | for (i = min_off; i < max_off + access_size; i++) { |
cc2b14d5 AS |
3037 | u8 *stype; |
3038 | ||
2011fccf | 3039 | slot = -i - 1; |
638f5b90 | 3040 | spi = slot / BPF_REG_SIZE; |
cc2b14d5 AS |
3041 | if (state->allocated_stack <= slot) |
3042 | goto err; | |
3043 | stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE]; | |
3044 | if (*stype == STACK_MISC) | |
3045 | goto mark; | |
3046 | if (*stype == STACK_ZERO) { | |
3047 | /* helper can write anything into the stack */ | |
3048 | *stype = STACK_MISC; | |
3049 | goto mark; | |
17a52670 | 3050 | } |
f7cf25b2 AS |
3051 | if (state->stack[spi].slot_type[0] == STACK_SPILL && |
3052 | state->stack[spi].spilled_ptr.type == SCALAR_VALUE) { | |
3053 | __mark_reg_unknown(&state->stack[spi].spilled_ptr); | |
3054 | for (j = 0; j < BPF_REG_SIZE; j++) | |
3055 | state->stack[spi].slot_type[j] = STACK_MISC; | |
3056 | goto mark; | |
3057 | } | |
3058 | ||
cc2b14d5 | 3059 | err: |
2011fccf AI |
3060 | if (tnum_is_const(reg->var_off)) { |
3061 | verbose(env, "invalid indirect read from stack off %d+%d size %d\n", | |
3062 | min_off, i - min_off, access_size); | |
3063 | } else { | |
3064 | char tn_buf[48]; | |
3065 | ||
3066 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
3067 | verbose(env, "invalid indirect read from stack var_off %s+%d size %d\n", | |
3068 | tn_buf, i - min_off, access_size); | |
3069 | } | |
cc2b14d5 AS |
3070 | return -EACCES; |
3071 | mark: | |
3072 | /* reading any byte out of 8-byte 'spill_slot' will cause | |
3073 | * the whole slot to be marked as 'read' | |
3074 | */ | |
679c782d | 3075 | mark_reg_read(env, &state->stack[spi].spilled_ptr, |
5327ed3d JW |
3076 | state->stack[spi].spilled_ptr.parent, |
3077 | REG_LIVE_READ64); | |
17a52670 | 3078 | } |
2011fccf | 3079 | return update_stack_depth(env, state, min_off); |
17a52670 AS |
3080 | } |
3081 | ||
06c1c049 GB |
3082 | static int check_helper_mem_access(struct bpf_verifier_env *env, int regno, |
3083 | int access_size, bool zero_size_allowed, | |
3084 | struct bpf_call_arg_meta *meta) | |
3085 | { | |
638f5b90 | 3086 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
06c1c049 | 3087 | |
f1174f77 | 3088 | switch (reg->type) { |
06c1c049 | 3089 | case PTR_TO_PACKET: |
de8f3a83 | 3090 | case PTR_TO_PACKET_META: |
9fd29c08 YS |
3091 | return check_packet_access(env, regno, reg->off, access_size, |
3092 | zero_size_allowed); | |
06c1c049 | 3093 | case PTR_TO_MAP_VALUE: |
591fe988 DB |
3094 | if (check_map_access_type(env, regno, reg->off, access_size, |
3095 | meta && meta->raw_mode ? BPF_WRITE : | |
3096 | BPF_READ)) | |
3097 | return -EACCES; | |
9fd29c08 YS |
3098 | return check_map_access(env, regno, reg->off, access_size, |
3099 | zero_size_allowed); | |
f1174f77 | 3100 | default: /* scalar_value|ptr_to_stack or invalid ptr */ |
06c1c049 GB |
3101 | return check_stack_boundary(env, regno, access_size, |
3102 | zero_size_allowed, meta); | |
3103 | } | |
3104 | } | |
3105 | ||
d83525ca AS |
3106 | /* Implementation details: |
3107 | * bpf_map_lookup returns PTR_TO_MAP_VALUE_OR_NULL | |
3108 | * Two bpf_map_lookups (even with the same key) will have different reg->id. | |
3109 | * For traditional PTR_TO_MAP_VALUE the verifier clears reg->id after | |
3110 | * value_or_null->value transition, since the verifier only cares about | |
3111 | * the range of access to valid map value pointer and doesn't care about actual | |
3112 | * address of the map element. | |
3113 | * For maps with 'struct bpf_spin_lock' inside map value the verifier keeps | |
3114 | * reg->id > 0 after value_or_null->value transition. By doing so | |
3115 | * two bpf_map_lookups will be considered two different pointers that | |
3116 | * point to different bpf_spin_locks. | |
3117 | * The verifier allows taking only one bpf_spin_lock at a time to avoid | |
3118 | * dead-locks. | |
3119 | * Since only one bpf_spin_lock is allowed the checks are simpler than | |
3120 | * reg_is_refcounted() logic. The verifier needs to remember only | |
3121 | * one spin_lock instead of array of acquired_refs. | |
3122 | * cur_state->active_spin_lock remembers which map value element got locked | |
3123 | * and clears it after bpf_spin_unlock. | |
3124 | */ | |
3125 | static int process_spin_lock(struct bpf_verifier_env *env, int regno, | |
3126 | bool is_lock) | |
3127 | { | |
3128 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; | |
3129 | struct bpf_verifier_state *cur = env->cur_state; | |
3130 | bool is_const = tnum_is_const(reg->var_off); | |
3131 | struct bpf_map *map = reg->map_ptr; | |
3132 | u64 val = reg->var_off.value; | |
3133 | ||
3134 | if (reg->type != PTR_TO_MAP_VALUE) { | |
3135 | verbose(env, "R%d is not a pointer to map_value\n", regno); | |
3136 | return -EINVAL; | |
3137 | } | |
3138 | if (!is_const) { | |
3139 | verbose(env, | |
3140 | "R%d doesn't have constant offset. bpf_spin_lock has to be at the constant offset\n", | |
3141 | regno); | |
3142 | return -EINVAL; | |
3143 | } | |
3144 | if (!map->btf) { | |
3145 | verbose(env, | |
3146 | "map '%s' has to have BTF in order to use bpf_spin_lock\n", | |
3147 | map->name); | |
3148 | return -EINVAL; | |
3149 | } | |
3150 | if (!map_value_has_spin_lock(map)) { | |
3151 | if (map->spin_lock_off == -E2BIG) | |
3152 | verbose(env, | |
3153 | "map '%s' has more than one 'struct bpf_spin_lock'\n", | |
3154 | map->name); | |
3155 | else if (map->spin_lock_off == -ENOENT) | |
3156 | verbose(env, | |
3157 | "map '%s' doesn't have 'struct bpf_spin_lock'\n", | |
3158 | map->name); | |
3159 | else | |
3160 | verbose(env, | |
3161 | "map '%s' is not a struct type or bpf_spin_lock is mangled\n", | |
3162 | map->name); | |
3163 | return -EINVAL; | |
3164 | } | |
3165 | if (map->spin_lock_off != val + reg->off) { | |
3166 | verbose(env, "off %lld doesn't point to 'struct bpf_spin_lock'\n", | |
3167 | val + reg->off); | |
3168 | return -EINVAL; | |
3169 | } | |
3170 | if (is_lock) { | |
3171 | if (cur->active_spin_lock) { | |
3172 | verbose(env, | |
3173 | "Locking two bpf_spin_locks are not allowed\n"); | |
3174 | return -EINVAL; | |
3175 | } | |
3176 | cur->active_spin_lock = reg->id; | |
3177 | } else { | |
3178 | if (!cur->active_spin_lock) { | |
3179 | verbose(env, "bpf_spin_unlock without taking a lock\n"); | |
3180 | return -EINVAL; | |
3181 | } | |
3182 | if (cur->active_spin_lock != reg->id) { | |
3183 | verbose(env, "bpf_spin_unlock of different lock\n"); | |
3184 | return -EINVAL; | |
3185 | } | |
3186 | cur->active_spin_lock = 0; | |
3187 | } | |
3188 | return 0; | |
3189 | } | |
3190 | ||
90133415 DB |
3191 | static bool arg_type_is_mem_ptr(enum bpf_arg_type type) |
3192 | { | |
3193 | return type == ARG_PTR_TO_MEM || | |
3194 | type == ARG_PTR_TO_MEM_OR_NULL || | |
3195 | type == ARG_PTR_TO_UNINIT_MEM; | |
3196 | } | |
3197 | ||
3198 | static bool arg_type_is_mem_size(enum bpf_arg_type type) | |
3199 | { | |
3200 | return type == ARG_CONST_SIZE || | |
3201 | type == ARG_CONST_SIZE_OR_ZERO; | |
3202 | } | |
3203 | ||
57c3bb72 AI |
3204 | static bool arg_type_is_int_ptr(enum bpf_arg_type type) |
3205 | { | |
3206 | return type == ARG_PTR_TO_INT || | |
3207 | type == ARG_PTR_TO_LONG; | |
3208 | } | |
3209 | ||
3210 | static int int_ptr_type_to_size(enum bpf_arg_type type) | |
3211 | { | |
3212 | if (type == ARG_PTR_TO_INT) | |
3213 | return sizeof(u32); | |
3214 | else if (type == ARG_PTR_TO_LONG) | |
3215 | return sizeof(u64); | |
3216 | ||
3217 | return -EINVAL; | |
3218 | } | |
3219 | ||
58e2af8b | 3220 | static int check_func_arg(struct bpf_verifier_env *env, u32 regno, |
33ff9823 DB |
3221 | enum bpf_arg_type arg_type, |
3222 | struct bpf_call_arg_meta *meta) | |
17a52670 | 3223 | { |
638f5b90 | 3224 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
6841de8b | 3225 | enum bpf_reg_type expected_type, type = reg->type; |
17a52670 AS |
3226 | int err = 0; |
3227 | ||
80f1d68c | 3228 | if (arg_type == ARG_DONTCARE) |
17a52670 AS |
3229 | return 0; |
3230 | ||
dc503a8a EC |
3231 | err = check_reg_arg(env, regno, SRC_OP); |
3232 | if (err) | |
3233 | return err; | |
17a52670 | 3234 | |
1be7f75d AS |
3235 | if (arg_type == ARG_ANYTHING) { |
3236 | if (is_pointer_value(env, regno)) { | |
61bd5218 JK |
3237 | verbose(env, "R%d leaks addr into helper function\n", |
3238 | regno); | |
1be7f75d AS |
3239 | return -EACCES; |
3240 | } | |
80f1d68c | 3241 | return 0; |
1be7f75d | 3242 | } |
80f1d68c | 3243 | |
de8f3a83 | 3244 | if (type_is_pkt_pointer(type) && |
3a0af8fd | 3245 | !may_access_direct_pkt_data(env, meta, BPF_READ)) { |
61bd5218 | 3246 | verbose(env, "helper access to the packet is not allowed\n"); |
6841de8b AS |
3247 | return -EACCES; |
3248 | } | |
3249 | ||
8e2fe1d9 | 3250 | if (arg_type == ARG_PTR_TO_MAP_KEY || |
2ea864c5 | 3251 | arg_type == ARG_PTR_TO_MAP_VALUE || |
6ac99e8f MKL |
3252 | arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE || |
3253 | arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL) { | |
17a52670 | 3254 | expected_type = PTR_TO_STACK; |
6ac99e8f MKL |
3255 | if (register_is_null(reg) && |
3256 | arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL) | |
3257 | /* final test in check_stack_boundary() */; | |
3258 | else if (!type_is_pkt_pointer(type) && | |
3259 | type != PTR_TO_MAP_VALUE && | |
3260 | type != expected_type) | |
6841de8b | 3261 | goto err_type; |
39f19ebb AS |
3262 | } else if (arg_type == ARG_CONST_SIZE || |
3263 | arg_type == ARG_CONST_SIZE_OR_ZERO) { | |
f1174f77 EC |
3264 | expected_type = SCALAR_VALUE; |
3265 | if (type != expected_type) | |
6841de8b | 3266 | goto err_type; |
17a52670 AS |
3267 | } else if (arg_type == ARG_CONST_MAP_PTR) { |
3268 | expected_type = CONST_PTR_TO_MAP; | |
6841de8b AS |
3269 | if (type != expected_type) |
3270 | goto err_type; | |
608cd71a AS |
3271 | } else if (arg_type == ARG_PTR_TO_CTX) { |
3272 | expected_type = PTR_TO_CTX; | |
6841de8b AS |
3273 | if (type != expected_type) |
3274 | goto err_type; | |
58990d1f DB |
3275 | err = check_ctx_reg(env, reg, regno); |
3276 | if (err < 0) | |
3277 | return err; | |
46f8bc92 MKL |
3278 | } else if (arg_type == ARG_PTR_TO_SOCK_COMMON) { |
3279 | expected_type = PTR_TO_SOCK_COMMON; | |
3280 | /* Any sk pointer can be ARG_PTR_TO_SOCK_COMMON */ | |
3281 | if (!type_is_sk_pointer(type)) | |
3282 | goto err_type; | |
1b986589 MKL |
3283 | if (reg->ref_obj_id) { |
3284 | if (meta->ref_obj_id) { | |
3285 | verbose(env, "verifier internal error: more than one arg with ref_obj_id R%d %u %u\n", | |
3286 | regno, reg->ref_obj_id, | |
3287 | meta->ref_obj_id); | |
3288 | return -EFAULT; | |
3289 | } | |
3290 | meta->ref_obj_id = reg->ref_obj_id; | |
fd978bf7 | 3291 | } |
6ac99e8f MKL |
3292 | } else if (arg_type == ARG_PTR_TO_SOCKET) { |
3293 | expected_type = PTR_TO_SOCKET; | |
3294 | if (type != expected_type) | |
3295 | goto err_type; | |
d83525ca AS |
3296 | } else if (arg_type == ARG_PTR_TO_SPIN_LOCK) { |
3297 | if (meta->func_id == BPF_FUNC_spin_lock) { | |
3298 | if (process_spin_lock(env, regno, true)) | |
3299 | return -EACCES; | |
3300 | } else if (meta->func_id == BPF_FUNC_spin_unlock) { | |
3301 | if (process_spin_lock(env, regno, false)) | |
3302 | return -EACCES; | |
3303 | } else { | |
3304 | verbose(env, "verifier internal error\n"); | |
3305 | return -EFAULT; | |
3306 | } | |
90133415 | 3307 | } else if (arg_type_is_mem_ptr(arg_type)) { |
8e2fe1d9 DB |
3308 | expected_type = PTR_TO_STACK; |
3309 | /* One exception here. In case function allows for NULL to be | |
f1174f77 | 3310 | * passed in as argument, it's a SCALAR_VALUE type. Final test |
8e2fe1d9 DB |
3311 | * happens during stack boundary checking. |
3312 | */ | |
914cb781 | 3313 | if (register_is_null(reg) && |
db1ac496 | 3314 | arg_type == ARG_PTR_TO_MEM_OR_NULL) |
6841de8b | 3315 | /* final test in check_stack_boundary() */; |
de8f3a83 DB |
3316 | else if (!type_is_pkt_pointer(type) && |
3317 | type != PTR_TO_MAP_VALUE && | |
f1174f77 | 3318 | type != expected_type) |
6841de8b | 3319 | goto err_type; |
39f19ebb | 3320 | meta->raw_mode = arg_type == ARG_PTR_TO_UNINIT_MEM; |
57c3bb72 AI |
3321 | } else if (arg_type_is_int_ptr(arg_type)) { |
3322 | expected_type = PTR_TO_STACK; | |
3323 | if (!type_is_pkt_pointer(type) && | |
3324 | type != PTR_TO_MAP_VALUE && | |
3325 | type != expected_type) | |
3326 | goto err_type; | |
17a52670 | 3327 | } else { |
61bd5218 | 3328 | verbose(env, "unsupported arg_type %d\n", arg_type); |
17a52670 AS |
3329 | return -EFAULT; |
3330 | } | |
3331 | ||
17a52670 AS |
3332 | if (arg_type == ARG_CONST_MAP_PTR) { |
3333 | /* bpf_map_xxx(map_ptr) call: remember that map_ptr */ | |
33ff9823 | 3334 | meta->map_ptr = reg->map_ptr; |
17a52670 AS |
3335 | } else if (arg_type == ARG_PTR_TO_MAP_KEY) { |
3336 | /* bpf_map_xxx(..., map_ptr, ..., key) call: | |
3337 | * check that [key, key + map->key_size) are within | |
3338 | * stack limits and initialized | |
3339 | */ | |
33ff9823 | 3340 | if (!meta->map_ptr) { |
17a52670 AS |
3341 | /* in function declaration map_ptr must come before |
3342 | * map_key, so that it's verified and known before | |
3343 | * we have to check map_key here. Otherwise it means | |
3344 | * that kernel subsystem misconfigured verifier | |
3345 | */ | |
61bd5218 | 3346 | verbose(env, "invalid map_ptr to access map->key\n"); |
17a52670 AS |
3347 | return -EACCES; |
3348 | } | |
d71962f3 PC |
3349 | err = check_helper_mem_access(env, regno, |
3350 | meta->map_ptr->key_size, false, | |
3351 | NULL); | |
2ea864c5 | 3352 | } else if (arg_type == ARG_PTR_TO_MAP_VALUE || |
6ac99e8f MKL |
3353 | (arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL && |
3354 | !register_is_null(reg)) || | |
2ea864c5 | 3355 | arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE) { |
17a52670 AS |
3356 | /* bpf_map_xxx(..., map_ptr, ..., value) call: |
3357 | * check [value, value + map->value_size) validity | |
3358 | */ | |
33ff9823 | 3359 | if (!meta->map_ptr) { |
17a52670 | 3360 | /* kernel subsystem misconfigured verifier */ |
61bd5218 | 3361 | verbose(env, "invalid map_ptr to access map->value\n"); |
17a52670 AS |
3362 | return -EACCES; |
3363 | } | |
2ea864c5 | 3364 | meta->raw_mode = (arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE); |
d71962f3 PC |
3365 | err = check_helper_mem_access(env, regno, |
3366 | meta->map_ptr->value_size, false, | |
2ea864c5 | 3367 | meta); |
90133415 | 3368 | } else if (arg_type_is_mem_size(arg_type)) { |
39f19ebb | 3369 | bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO); |
17a52670 | 3370 | |
849fa506 YS |
3371 | /* remember the mem_size which may be used later |
3372 | * to refine return values. | |
3373 | */ | |
3374 | meta->msize_smax_value = reg->smax_value; | |
3375 | meta->msize_umax_value = reg->umax_value; | |
3376 | ||
f1174f77 EC |
3377 | /* The register is SCALAR_VALUE; the access check |
3378 | * happens using its boundaries. | |
06c1c049 | 3379 | */ |
f1174f77 | 3380 | if (!tnum_is_const(reg->var_off)) |
06c1c049 GB |
3381 | /* For unprivileged variable accesses, disable raw |
3382 | * mode so that the program is required to | |
3383 | * initialize all the memory that the helper could | |
3384 | * just partially fill up. | |
3385 | */ | |
3386 | meta = NULL; | |
3387 | ||
b03c9f9f | 3388 | if (reg->smin_value < 0) { |
61bd5218 | 3389 | verbose(env, "R%d min value is negative, either use unsigned or 'var &= const'\n", |
f1174f77 EC |
3390 | regno); |
3391 | return -EACCES; | |
3392 | } | |
06c1c049 | 3393 | |
b03c9f9f | 3394 | if (reg->umin_value == 0) { |
f1174f77 EC |
3395 | err = check_helper_mem_access(env, regno - 1, 0, |
3396 | zero_size_allowed, | |
3397 | meta); | |
06c1c049 GB |
3398 | if (err) |
3399 | return err; | |
06c1c049 | 3400 | } |
f1174f77 | 3401 | |
b03c9f9f | 3402 | if (reg->umax_value >= BPF_MAX_VAR_SIZ) { |
61bd5218 | 3403 | verbose(env, "R%d unbounded memory access, use 'var &= const' or 'if (var < const)'\n", |
f1174f77 EC |
3404 | regno); |
3405 | return -EACCES; | |
3406 | } | |
3407 | err = check_helper_mem_access(env, regno - 1, | |
b03c9f9f | 3408 | reg->umax_value, |
f1174f77 | 3409 | zero_size_allowed, meta); |
b5dc0163 AS |
3410 | if (!err) |
3411 | err = mark_chain_precision(env, regno); | |
57c3bb72 AI |
3412 | } else if (arg_type_is_int_ptr(arg_type)) { |
3413 | int size = int_ptr_type_to_size(arg_type); | |
3414 | ||
3415 | err = check_helper_mem_access(env, regno, size, false, meta); | |
3416 | if (err) | |
3417 | return err; | |
3418 | err = check_ptr_alignment(env, reg, 0, size, true); | |
17a52670 AS |
3419 | } |
3420 | ||
3421 | return err; | |
6841de8b | 3422 | err_type: |
61bd5218 | 3423 | verbose(env, "R%d type=%s expected=%s\n", regno, |
6841de8b AS |
3424 | reg_type_str[type], reg_type_str[expected_type]); |
3425 | return -EACCES; | |
17a52670 AS |
3426 | } |
3427 | ||
61bd5218 JK |
3428 | static int check_map_func_compatibility(struct bpf_verifier_env *env, |
3429 | struct bpf_map *map, int func_id) | |
35578d79 | 3430 | { |
35578d79 KX |
3431 | if (!map) |
3432 | return 0; | |
3433 | ||
6aff67c8 AS |
3434 | /* We need a two way check, first is from map perspective ... */ |
3435 | switch (map->map_type) { | |
3436 | case BPF_MAP_TYPE_PROG_ARRAY: | |
3437 | if (func_id != BPF_FUNC_tail_call) | |
3438 | goto error; | |
3439 | break; | |
3440 | case BPF_MAP_TYPE_PERF_EVENT_ARRAY: | |
3441 | if (func_id != BPF_FUNC_perf_event_read && | |
908432ca YS |
3442 | func_id != BPF_FUNC_perf_event_output && |
3443 | func_id != BPF_FUNC_perf_event_read_value) | |
6aff67c8 AS |
3444 | goto error; |
3445 | break; | |
3446 | case BPF_MAP_TYPE_STACK_TRACE: | |
3447 | if (func_id != BPF_FUNC_get_stackid) | |
3448 | goto error; | |
3449 | break; | |
4ed8ec52 | 3450 | case BPF_MAP_TYPE_CGROUP_ARRAY: |
60747ef4 | 3451 | if (func_id != BPF_FUNC_skb_under_cgroup && |
60d20f91 | 3452 | func_id != BPF_FUNC_current_task_under_cgroup) |
4a482f34 MKL |
3453 | goto error; |
3454 | break; | |
cd339431 | 3455 | case BPF_MAP_TYPE_CGROUP_STORAGE: |
b741f163 | 3456 | case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE: |
cd339431 RG |
3457 | if (func_id != BPF_FUNC_get_local_storage) |
3458 | goto error; | |
3459 | break; | |
546ac1ff | 3460 | case BPF_MAP_TYPE_DEVMAP: |
6f9d451a | 3461 | case BPF_MAP_TYPE_DEVMAP_HASH: |
0cdbb4b0 THJ |
3462 | if (func_id != BPF_FUNC_redirect_map && |
3463 | func_id != BPF_FUNC_map_lookup_elem) | |
546ac1ff JF |
3464 | goto error; |
3465 | break; | |
fbfc504a BT |
3466 | /* Restrict bpf side of cpumap and xskmap, open when use-cases |
3467 | * appear. | |
3468 | */ | |
6710e112 JDB |
3469 | case BPF_MAP_TYPE_CPUMAP: |
3470 | if (func_id != BPF_FUNC_redirect_map) | |
3471 | goto error; | |
3472 | break; | |
fada7fdc JL |
3473 | case BPF_MAP_TYPE_XSKMAP: |
3474 | if (func_id != BPF_FUNC_redirect_map && | |
3475 | func_id != BPF_FUNC_map_lookup_elem) | |
3476 | goto error; | |
3477 | break; | |
56f668df | 3478 | case BPF_MAP_TYPE_ARRAY_OF_MAPS: |
bcc6b1b7 | 3479 | case BPF_MAP_TYPE_HASH_OF_MAPS: |
56f668df MKL |
3480 | if (func_id != BPF_FUNC_map_lookup_elem) |
3481 | goto error; | |
16a43625 | 3482 | break; |
174a79ff JF |
3483 | case BPF_MAP_TYPE_SOCKMAP: |
3484 | if (func_id != BPF_FUNC_sk_redirect_map && | |
3485 | func_id != BPF_FUNC_sock_map_update && | |
4f738adb JF |
3486 | func_id != BPF_FUNC_map_delete_elem && |
3487 | func_id != BPF_FUNC_msg_redirect_map) | |
174a79ff JF |
3488 | goto error; |
3489 | break; | |
81110384 JF |
3490 | case BPF_MAP_TYPE_SOCKHASH: |
3491 | if (func_id != BPF_FUNC_sk_redirect_hash && | |
3492 | func_id != BPF_FUNC_sock_hash_update && | |
3493 | func_id != BPF_FUNC_map_delete_elem && | |
3494 | func_id != BPF_FUNC_msg_redirect_hash) | |
3495 | goto error; | |
3496 | break; | |
2dbb9b9e MKL |
3497 | case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY: |
3498 | if (func_id != BPF_FUNC_sk_select_reuseport) | |
3499 | goto error; | |
3500 | break; | |
f1a2e44a MV |
3501 | case BPF_MAP_TYPE_QUEUE: |
3502 | case BPF_MAP_TYPE_STACK: | |
3503 | if (func_id != BPF_FUNC_map_peek_elem && | |
3504 | func_id != BPF_FUNC_map_pop_elem && | |
3505 | func_id != BPF_FUNC_map_push_elem) | |
3506 | goto error; | |
3507 | break; | |
6ac99e8f MKL |
3508 | case BPF_MAP_TYPE_SK_STORAGE: |
3509 | if (func_id != BPF_FUNC_sk_storage_get && | |
3510 | func_id != BPF_FUNC_sk_storage_delete) | |
3511 | goto error; | |
3512 | break; | |
6aff67c8 AS |
3513 | default: |
3514 | break; | |
3515 | } | |
3516 | ||
3517 | /* ... and second from the function itself. */ | |
3518 | switch (func_id) { | |
3519 | case BPF_FUNC_tail_call: | |
3520 | if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY) | |
3521 | goto error; | |
f910cefa | 3522 | if (env->subprog_cnt > 1) { |
f4d7e40a AS |
3523 | verbose(env, "tail_calls are not allowed in programs with bpf-to-bpf calls\n"); |
3524 | return -EINVAL; | |
3525 | } | |
6aff67c8 AS |
3526 | break; |
3527 | case BPF_FUNC_perf_event_read: | |
3528 | case BPF_FUNC_perf_event_output: | |
908432ca | 3529 | case BPF_FUNC_perf_event_read_value: |
6aff67c8 AS |
3530 | if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) |
3531 | goto error; | |
3532 | break; | |
3533 | case BPF_FUNC_get_stackid: | |
3534 | if (map->map_type != BPF_MAP_TYPE_STACK_TRACE) | |
3535 | goto error; | |
3536 | break; | |
60d20f91 | 3537 | case BPF_FUNC_current_task_under_cgroup: |
747ea55e | 3538 | case BPF_FUNC_skb_under_cgroup: |
4a482f34 MKL |
3539 | if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY) |
3540 | goto error; | |
3541 | break; | |
97f91a7c | 3542 | case BPF_FUNC_redirect_map: |
9c270af3 | 3543 | if (map->map_type != BPF_MAP_TYPE_DEVMAP && |
6f9d451a | 3544 | map->map_type != BPF_MAP_TYPE_DEVMAP_HASH && |
fbfc504a BT |
3545 | map->map_type != BPF_MAP_TYPE_CPUMAP && |
3546 | map->map_type != BPF_MAP_TYPE_XSKMAP) | |
97f91a7c JF |
3547 | goto error; |
3548 | break; | |
174a79ff | 3549 | case BPF_FUNC_sk_redirect_map: |
4f738adb | 3550 | case BPF_FUNC_msg_redirect_map: |
81110384 | 3551 | case BPF_FUNC_sock_map_update: |
174a79ff JF |
3552 | if (map->map_type != BPF_MAP_TYPE_SOCKMAP) |
3553 | goto error; | |
3554 | break; | |
81110384 JF |
3555 | case BPF_FUNC_sk_redirect_hash: |
3556 | case BPF_FUNC_msg_redirect_hash: | |
3557 | case BPF_FUNC_sock_hash_update: | |
3558 | if (map->map_type != BPF_MAP_TYPE_SOCKHASH) | |
174a79ff JF |
3559 | goto error; |
3560 | break; | |
cd339431 | 3561 | case BPF_FUNC_get_local_storage: |
b741f163 RG |
3562 | if (map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE && |
3563 | map->map_type != BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) | |
cd339431 RG |
3564 | goto error; |
3565 | break; | |
2dbb9b9e MKL |
3566 | case BPF_FUNC_sk_select_reuseport: |
3567 | if (map->map_type != BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) | |
3568 | goto error; | |
3569 | break; | |
f1a2e44a MV |
3570 | case BPF_FUNC_map_peek_elem: |
3571 | case BPF_FUNC_map_pop_elem: | |
3572 | case BPF_FUNC_map_push_elem: | |
3573 | if (map->map_type != BPF_MAP_TYPE_QUEUE && | |
3574 | map->map_type != BPF_MAP_TYPE_STACK) | |
3575 | goto error; | |
3576 | break; | |
6ac99e8f MKL |
3577 | case BPF_FUNC_sk_storage_get: |
3578 | case BPF_FUNC_sk_storage_delete: | |
3579 | if (map->map_type != BPF_MAP_TYPE_SK_STORAGE) | |
3580 | goto error; | |
3581 | break; | |
6aff67c8 AS |
3582 | default: |
3583 | break; | |
35578d79 KX |
3584 | } |
3585 | ||
3586 | return 0; | |
6aff67c8 | 3587 | error: |
61bd5218 | 3588 | verbose(env, "cannot pass map_type %d into func %s#%d\n", |
ebb676da | 3589 | map->map_type, func_id_name(func_id), func_id); |
6aff67c8 | 3590 | return -EINVAL; |
35578d79 KX |
3591 | } |
3592 | ||
90133415 | 3593 | static bool check_raw_mode_ok(const struct bpf_func_proto *fn) |
435faee1 DB |
3594 | { |
3595 | int count = 0; | |
3596 | ||
39f19ebb | 3597 | if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 3598 | count++; |
39f19ebb | 3599 | if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 3600 | count++; |
39f19ebb | 3601 | if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 3602 | count++; |
39f19ebb | 3603 | if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 3604 | count++; |
39f19ebb | 3605 | if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 DB |
3606 | count++; |
3607 | ||
90133415 DB |
3608 | /* We only support one arg being in raw mode at the moment, |
3609 | * which is sufficient for the helper functions we have | |
3610 | * right now. | |
3611 | */ | |
3612 | return count <= 1; | |
3613 | } | |
3614 | ||
3615 | static bool check_args_pair_invalid(enum bpf_arg_type arg_curr, | |
3616 | enum bpf_arg_type arg_next) | |
3617 | { | |
3618 | return (arg_type_is_mem_ptr(arg_curr) && | |
3619 | !arg_type_is_mem_size(arg_next)) || | |
3620 | (!arg_type_is_mem_ptr(arg_curr) && | |
3621 | arg_type_is_mem_size(arg_next)); | |
3622 | } | |
3623 | ||
3624 | static bool check_arg_pair_ok(const struct bpf_func_proto *fn) | |
3625 | { | |
3626 | /* bpf_xxx(..., buf, len) call will access 'len' | |
3627 | * bytes from memory 'buf'. Both arg types need | |
3628 | * to be paired, so make sure there's no buggy | |
3629 | * helper function specification. | |
3630 | */ | |
3631 | if (arg_type_is_mem_size(fn->arg1_type) || | |
3632 | arg_type_is_mem_ptr(fn->arg5_type) || | |
3633 | check_args_pair_invalid(fn->arg1_type, fn->arg2_type) || | |
3634 | check_args_pair_invalid(fn->arg2_type, fn->arg3_type) || | |
3635 | check_args_pair_invalid(fn->arg3_type, fn->arg4_type) || | |
3636 | check_args_pair_invalid(fn->arg4_type, fn->arg5_type)) | |
3637 | return false; | |
3638 | ||
3639 | return true; | |
3640 | } | |
3641 | ||
1b986589 | 3642 | static bool check_refcount_ok(const struct bpf_func_proto *fn, int func_id) |
fd978bf7 JS |
3643 | { |
3644 | int count = 0; | |
3645 | ||
1b986589 | 3646 | if (arg_type_may_be_refcounted(fn->arg1_type)) |
fd978bf7 | 3647 | count++; |
1b986589 | 3648 | if (arg_type_may_be_refcounted(fn->arg2_type)) |
fd978bf7 | 3649 | count++; |
1b986589 | 3650 | if (arg_type_may_be_refcounted(fn->arg3_type)) |
fd978bf7 | 3651 | count++; |
1b986589 | 3652 | if (arg_type_may_be_refcounted(fn->arg4_type)) |
fd978bf7 | 3653 | count++; |
1b986589 | 3654 | if (arg_type_may_be_refcounted(fn->arg5_type)) |
fd978bf7 JS |
3655 | count++; |
3656 | ||
1b986589 MKL |
3657 | /* A reference acquiring function cannot acquire |
3658 | * another refcounted ptr. | |
3659 | */ | |
3660 | if (is_acquire_function(func_id) && count) | |
3661 | return false; | |
3662 | ||
fd978bf7 JS |
3663 | /* We only support one arg being unreferenced at the moment, |
3664 | * which is sufficient for the helper functions we have right now. | |
3665 | */ | |
3666 | return count <= 1; | |
3667 | } | |
3668 | ||
1b986589 | 3669 | static int check_func_proto(const struct bpf_func_proto *fn, int func_id) |
90133415 DB |
3670 | { |
3671 | return check_raw_mode_ok(fn) && | |
fd978bf7 | 3672 | check_arg_pair_ok(fn) && |
1b986589 | 3673 | check_refcount_ok(fn, func_id) ? 0 : -EINVAL; |
435faee1 DB |
3674 | } |
3675 | ||
de8f3a83 DB |
3676 | /* Packet data might have moved, any old PTR_TO_PACKET[_META,_END] |
3677 | * are now invalid, so turn them into unknown SCALAR_VALUE. | |
f1174f77 | 3678 | */ |
f4d7e40a AS |
3679 | static void __clear_all_pkt_pointers(struct bpf_verifier_env *env, |
3680 | struct bpf_func_state *state) | |
969bf05e | 3681 | { |
58e2af8b | 3682 | struct bpf_reg_state *regs = state->regs, *reg; |
969bf05e AS |
3683 | int i; |
3684 | ||
3685 | for (i = 0; i < MAX_BPF_REG; i++) | |
de8f3a83 | 3686 | if (reg_is_pkt_pointer_any(®s[i])) |
61bd5218 | 3687 | mark_reg_unknown(env, regs, i); |
969bf05e | 3688 | |
f3709f69 JS |
3689 | bpf_for_each_spilled_reg(i, state, reg) { |
3690 | if (!reg) | |
969bf05e | 3691 | continue; |
de8f3a83 DB |
3692 | if (reg_is_pkt_pointer_any(reg)) |
3693 | __mark_reg_unknown(reg); | |
969bf05e AS |
3694 | } |
3695 | } | |
3696 | ||
f4d7e40a AS |
3697 | static void clear_all_pkt_pointers(struct bpf_verifier_env *env) |
3698 | { | |
3699 | struct bpf_verifier_state *vstate = env->cur_state; | |
3700 | int i; | |
3701 | ||
3702 | for (i = 0; i <= vstate->curframe; i++) | |
3703 | __clear_all_pkt_pointers(env, vstate->frame[i]); | |
3704 | } | |
3705 | ||
fd978bf7 | 3706 | static void release_reg_references(struct bpf_verifier_env *env, |
1b986589 MKL |
3707 | struct bpf_func_state *state, |
3708 | int ref_obj_id) | |
fd978bf7 JS |
3709 | { |
3710 | struct bpf_reg_state *regs = state->regs, *reg; | |
3711 | int i; | |
3712 | ||
3713 | for (i = 0; i < MAX_BPF_REG; i++) | |
1b986589 | 3714 | if (regs[i].ref_obj_id == ref_obj_id) |
fd978bf7 JS |
3715 | mark_reg_unknown(env, regs, i); |
3716 | ||
3717 | bpf_for_each_spilled_reg(i, state, reg) { | |
3718 | if (!reg) | |
3719 | continue; | |
1b986589 | 3720 | if (reg->ref_obj_id == ref_obj_id) |
fd978bf7 JS |
3721 | __mark_reg_unknown(reg); |
3722 | } | |
3723 | } | |
3724 | ||
3725 | /* The pointer with the specified id has released its reference to kernel | |
3726 | * resources. Identify all copies of the same pointer and clear the reference. | |
3727 | */ | |
3728 | static int release_reference(struct bpf_verifier_env *env, | |
1b986589 | 3729 | int ref_obj_id) |
fd978bf7 JS |
3730 | { |
3731 | struct bpf_verifier_state *vstate = env->cur_state; | |
1b986589 | 3732 | int err; |
fd978bf7 JS |
3733 | int i; |
3734 | ||
1b986589 MKL |
3735 | err = release_reference_state(cur_func(env), ref_obj_id); |
3736 | if (err) | |
3737 | return err; | |
3738 | ||
fd978bf7 | 3739 | for (i = 0; i <= vstate->curframe; i++) |
1b986589 | 3740 | release_reg_references(env, vstate->frame[i], ref_obj_id); |
fd978bf7 | 3741 | |
1b986589 | 3742 | return 0; |
fd978bf7 JS |
3743 | } |
3744 | ||
f4d7e40a AS |
3745 | static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, |
3746 | int *insn_idx) | |
3747 | { | |
3748 | struct bpf_verifier_state *state = env->cur_state; | |
3749 | struct bpf_func_state *caller, *callee; | |
fd978bf7 | 3750 | int i, err, subprog, target_insn; |
f4d7e40a | 3751 | |
aada9ce6 | 3752 | if (state->curframe + 1 >= MAX_CALL_FRAMES) { |
f4d7e40a | 3753 | verbose(env, "the call stack of %d frames is too deep\n", |
aada9ce6 | 3754 | state->curframe + 2); |
f4d7e40a AS |
3755 | return -E2BIG; |
3756 | } | |
3757 | ||
3758 | target_insn = *insn_idx + insn->imm; | |
3759 | subprog = find_subprog(env, target_insn + 1); | |
3760 | if (subprog < 0) { | |
3761 | verbose(env, "verifier bug. No program starts at insn %d\n", | |
3762 | target_insn + 1); | |
3763 | return -EFAULT; | |
3764 | } | |
3765 | ||
3766 | caller = state->frame[state->curframe]; | |
3767 | if (state->frame[state->curframe + 1]) { | |
3768 | verbose(env, "verifier bug. Frame %d already allocated\n", | |
3769 | state->curframe + 1); | |
3770 | return -EFAULT; | |
3771 | } | |
3772 | ||
3773 | callee = kzalloc(sizeof(*callee), GFP_KERNEL); | |
3774 | if (!callee) | |
3775 | return -ENOMEM; | |
3776 | state->frame[state->curframe + 1] = callee; | |
3777 | ||
3778 | /* callee cannot access r0, r6 - r9 for reading and has to write | |
3779 | * into its own stack before reading from it. | |
3780 | * callee can read/write into caller's stack | |
3781 | */ | |
3782 | init_func_state(env, callee, | |
3783 | /* remember the callsite, it will be used by bpf_exit */ | |
3784 | *insn_idx /* callsite */, | |
3785 | state->curframe + 1 /* frameno within this callchain */, | |
f910cefa | 3786 | subprog /* subprog number within this prog */); |
f4d7e40a | 3787 | |
fd978bf7 JS |
3788 | /* Transfer references to the callee */ |
3789 | err = transfer_reference_state(callee, caller); | |
3790 | if (err) | |
3791 | return err; | |
3792 | ||
679c782d EC |
3793 | /* copy r1 - r5 args that callee can access. The copy includes parent |
3794 | * pointers, which connects us up to the liveness chain | |
3795 | */ | |
f4d7e40a AS |
3796 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) |
3797 | callee->regs[i] = caller->regs[i]; | |
3798 | ||
679c782d | 3799 | /* after the call registers r0 - r5 were scratched */ |
f4d7e40a AS |
3800 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
3801 | mark_reg_not_init(env, caller->regs, caller_saved[i]); | |
3802 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); | |
3803 | } | |
3804 | ||
3805 | /* only increment it after check_reg_arg() finished */ | |
3806 | state->curframe++; | |
3807 | ||
3808 | /* and go analyze first insn of the callee */ | |
3809 | *insn_idx = target_insn; | |
3810 | ||
06ee7115 | 3811 | if (env->log.level & BPF_LOG_LEVEL) { |
f4d7e40a AS |
3812 | verbose(env, "caller:\n"); |
3813 | print_verifier_state(env, caller); | |
3814 | verbose(env, "callee:\n"); | |
3815 | print_verifier_state(env, callee); | |
3816 | } | |
3817 | return 0; | |
3818 | } | |
3819 | ||
3820 | static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx) | |
3821 | { | |
3822 | struct bpf_verifier_state *state = env->cur_state; | |
3823 | struct bpf_func_state *caller, *callee; | |
3824 | struct bpf_reg_state *r0; | |
fd978bf7 | 3825 | int err; |
f4d7e40a AS |
3826 | |
3827 | callee = state->frame[state->curframe]; | |
3828 | r0 = &callee->regs[BPF_REG_0]; | |
3829 | if (r0->type == PTR_TO_STACK) { | |
3830 | /* technically it's ok to return caller's stack pointer | |
3831 | * (or caller's caller's pointer) back to the caller, | |
3832 | * since these pointers are valid. Only current stack | |
3833 | * pointer will be invalid as soon as function exits, | |
3834 | * but let's be conservative | |
3835 | */ | |
3836 | verbose(env, "cannot return stack pointer to the caller\n"); | |
3837 | return -EINVAL; | |
3838 | } | |
3839 | ||
3840 | state->curframe--; | |
3841 | caller = state->frame[state->curframe]; | |
3842 | /* return to the caller whatever r0 had in the callee */ | |
3843 | caller->regs[BPF_REG_0] = *r0; | |
3844 | ||
fd978bf7 JS |
3845 | /* Transfer references to the caller */ |
3846 | err = transfer_reference_state(caller, callee); | |
3847 | if (err) | |
3848 | return err; | |
3849 | ||
f4d7e40a | 3850 | *insn_idx = callee->callsite + 1; |
06ee7115 | 3851 | if (env->log.level & BPF_LOG_LEVEL) { |
f4d7e40a AS |
3852 | verbose(env, "returning from callee:\n"); |
3853 | print_verifier_state(env, callee); | |
3854 | verbose(env, "to caller at %d:\n", *insn_idx); | |
3855 | print_verifier_state(env, caller); | |
3856 | } | |
3857 | /* clear everything in the callee */ | |
3858 | free_func_state(callee); | |
3859 | state->frame[state->curframe + 1] = NULL; | |
3860 | return 0; | |
3861 | } | |
3862 | ||
849fa506 YS |
3863 | static void do_refine_retval_range(struct bpf_reg_state *regs, int ret_type, |
3864 | int func_id, | |
3865 | struct bpf_call_arg_meta *meta) | |
3866 | { | |
3867 | struct bpf_reg_state *ret_reg = ®s[BPF_REG_0]; | |
3868 | ||
3869 | if (ret_type != RET_INTEGER || | |
3870 | (func_id != BPF_FUNC_get_stack && | |
3871 | func_id != BPF_FUNC_probe_read_str)) | |
3872 | return; | |
3873 | ||
3874 | ret_reg->smax_value = meta->msize_smax_value; | |
3875 | ret_reg->umax_value = meta->msize_umax_value; | |
3876 | __reg_deduce_bounds(ret_reg); | |
3877 | __reg_bound_offset(ret_reg); | |
3878 | } | |
3879 | ||
c93552c4 DB |
3880 | static int |
3881 | record_func_map(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, | |
3882 | int func_id, int insn_idx) | |
3883 | { | |
3884 | struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; | |
591fe988 | 3885 | struct bpf_map *map = meta->map_ptr; |
c93552c4 DB |
3886 | |
3887 | if (func_id != BPF_FUNC_tail_call && | |
09772d92 DB |
3888 | func_id != BPF_FUNC_map_lookup_elem && |
3889 | func_id != BPF_FUNC_map_update_elem && | |
f1a2e44a MV |
3890 | func_id != BPF_FUNC_map_delete_elem && |
3891 | func_id != BPF_FUNC_map_push_elem && | |
3892 | func_id != BPF_FUNC_map_pop_elem && | |
3893 | func_id != BPF_FUNC_map_peek_elem) | |
c93552c4 | 3894 | return 0; |
09772d92 | 3895 | |
591fe988 | 3896 | if (map == NULL) { |
c93552c4 DB |
3897 | verbose(env, "kernel subsystem misconfigured verifier\n"); |
3898 | return -EINVAL; | |
3899 | } | |
3900 | ||
591fe988 DB |
3901 | /* In case of read-only, some additional restrictions |
3902 | * need to be applied in order to prevent altering the | |
3903 | * state of the map from program side. | |
3904 | */ | |
3905 | if ((map->map_flags & BPF_F_RDONLY_PROG) && | |
3906 | (func_id == BPF_FUNC_map_delete_elem || | |
3907 | func_id == BPF_FUNC_map_update_elem || | |
3908 | func_id == BPF_FUNC_map_push_elem || | |
3909 | func_id == BPF_FUNC_map_pop_elem)) { | |
3910 | verbose(env, "write into map forbidden\n"); | |
3911 | return -EACCES; | |
3912 | } | |
3913 | ||
c93552c4 DB |
3914 | if (!BPF_MAP_PTR(aux->map_state)) |
3915 | bpf_map_ptr_store(aux, meta->map_ptr, | |
3916 | meta->map_ptr->unpriv_array); | |
3917 | else if (BPF_MAP_PTR(aux->map_state) != meta->map_ptr) | |
3918 | bpf_map_ptr_store(aux, BPF_MAP_PTR_POISON, | |
3919 | meta->map_ptr->unpriv_array); | |
3920 | return 0; | |
3921 | } | |
3922 | ||
fd978bf7 JS |
3923 | static int check_reference_leak(struct bpf_verifier_env *env) |
3924 | { | |
3925 | struct bpf_func_state *state = cur_func(env); | |
3926 | int i; | |
3927 | ||
3928 | for (i = 0; i < state->acquired_refs; i++) { | |
3929 | verbose(env, "Unreleased reference id=%d alloc_insn=%d\n", | |
3930 | state->refs[i].id, state->refs[i].insn_idx); | |
3931 | } | |
3932 | return state->acquired_refs ? -EINVAL : 0; | |
3933 | } | |
3934 | ||
f4d7e40a | 3935 | static int check_helper_call(struct bpf_verifier_env *env, int func_id, int insn_idx) |
17a52670 | 3936 | { |
17a52670 | 3937 | const struct bpf_func_proto *fn = NULL; |
638f5b90 | 3938 | struct bpf_reg_state *regs; |
33ff9823 | 3939 | struct bpf_call_arg_meta meta; |
969bf05e | 3940 | bool changes_data; |
17a52670 AS |
3941 | int i, err; |
3942 | ||
3943 | /* find function prototype */ | |
3944 | if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) { | |
61bd5218 JK |
3945 | verbose(env, "invalid func %s#%d\n", func_id_name(func_id), |
3946 | func_id); | |
17a52670 AS |
3947 | return -EINVAL; |
3948 | } | |
3949 | ||
00176a34 | 3950 | if (env->ops->get_func_proto) |
5e43f899 | 3951 | fn = env->ops->get_func_proto(func_id, env->prog); |
17a52670 | 3952 | if (!fn) { |
61bd5218 JK |
3953 | verbose(env, "unknown func %s#%d\n", func_id_name(func_id), |
3954 | func_id); | |
17a52670 AS |
3955 | return -EINVAL; |
3956 | } | |
3957 | ||
3958 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ | |
24701ece | 3959 | if (!env->prog->gpl_compatible && fn->gpl_only) { |
3fe2867c | 3960 | verbose(env, "cannot call GPL-restricted function from non-GPL compatible program\n"); |
17a52670 AS |
3961 | return -EINVAL; |
3962 | } | |
3963 | ||
04514d13 | 3964 | /* With LD_ABS/IND some JITs save/restore skb from r1. */ |
17bedab2 | 3965 | changes_data = bpf_helper_changes_pkt_data(fn->func); |
04514d13 DB |
3966 | if (changes_data && fn->arg1_type != ARG_PTR_TO_CTX) { |
3967 | verbose(env, "kernel subsystem misconfigured func %s#%d: r1 != ctx\n", | |
3968 | func_id_name(func_id), func_id); | |
3969 | return -EINVAL; | |
3970 | } | |
969bf05e | 3971 | |
33ff9823 | 3972 | memset(&meta, 0, sizeof(meta)); |
36bbef52 | 3973 | meta.pkt_access = fn->pkt_access; |
33ff9823 | 3974 | |
1b986589 | 3975 | err = check_func_proto(fn, func_id); |
435faee1 | 3976 | if (err) { |
61bd5218 | 3977 | verbose(env, "kernel subsystem misconfigured func %s#%d\n", |
ebb676da | 3978 | func_id_name(func_id), func_id); |
435faee1 DB |
3979 | return err; |
3980 | } | |
3981 | ||
d83525ca | 3982 | meta.func_id = func_id; |
17a52670 | 3983 | /* check args */ |
33ff9823 | 3984 | err = check_func_arg(env, BPF_REG_1, fn->arg1_type, &meta); |
17a52670 AS |
3985 | if (err) |
3986 | return err; | |
33ff9823 | 3987 | err = check_func_arg(env, BPF_REG_2, fn->arg2_type, &meta); |
17a52670 AS |
3988 | if (err) |
3989 | return err; | |
33ff9823 | 3990 | err = check_func_arg(env, BPF_REG_3, fn->arg3_type, &meta); |
17a52670 AS |
3991 | if (err) |
3992 | return err; | |
33ff9823 | 3993 | err = check_func_arg(env, BPF_REG_4, fn->arg4_type, &meta); |
17a52670 AS |
3994 | if (err) |
3995 | return err; | |
33ff9823 | 3996 | err = check_func_arg(env, BPF_REG_5, fn->arg5_type, &meta); |
17a52670 AS |
3997 | if (err) |
3998 | return err; | |
3999 | ||
c93552c4 DB |
4000 | err = record_func_map(env, &meta, func_id, insn_idx); |
4001 | if (err) | |
4002 | return err; | |
4003 | ||
435faee1 DB |
4004 | /* Mark slots with STACK_MISC in case of raw mode, stack offset |
4005 | * is inferred from register state. | |
4006 | */ | |
4007 | for (i = 0; i < meta.access_size; i++) { | |
ca369602 DB |
4008 | err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B, |
4009 | BPF_WRITE, -1, false); | |
435faee1 DB |
4010 | if (err) |
4011 | return err; | |
4012 | } | |
4013 | ||
fd978bf7 JS |
4014 | if (func_id == BPF_FUNC_tail_call) { |
4015 | err = check_reference_leak(env); | |
4016 | if (err) { | |
4017 | verbose(env, "tail_call would lead to reference leak\n"); | |
4018 | return err; | |
4019 | } | |
4020 | } else if (is_release_function(func_id)) { | |
1b986589 | 4021 | err = release_reference(env, meta.ref_obj_id); |
46f8bc92 MKL |
4022 | if (err) { |
4023 | verbose(env, "func %s#%d reference has not been acquired before\n", | |
4024 | func_id_name(func_id), func_id); | |
fd978bf7 | 4025 | return err; |
46f8bc92 | 4026 | } |
fd978bf7 JS |
4027 | } |
4028 | ||
638f5b90 | 4029 | regs = cur_regs(env); |
cd339431 RG |
4030 | |
4031 | /* check that flags argument in get_local_storage(map, flags) is 0, | |
4032 | * this is required because get_local_storage() can't return an error. | |
4033 | */ | |
4034 | if (func_id == BPF_FUNC_get_local_storage && | |
4035 | !register_is_null(®s[BPF_REG_2])) { | |
4036 | verbose(env, "get_local_storage() doesn't support non-zero flags\n"); | |
4037 | return -EINVAL; | |
4038 | } | |
4039 | ||
17a52670 | 4040 | /* reset caller saved regs */ |
dc503a8a | 4041 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
61bd5218 | 4042 | mark_reg_not_init(env, regs, caller_saved[i]); |
dc503a8a EC |
4043 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
4044 | } | |
17a52670 | 4045 | |
5327ed3d JW |
4046 | /* helper call returns 64-bit value. */ |
4047 | regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG; | |
4048 | ||
dc503a8a | 4049 | /* update return register (already marked as written above) */ |
17a52670 | 4050 | if (fn->ret_type == RET_INTEGER) { |
f1174f77 | 4051 | /* sets type to SCALAR_VALUE */ |
61bd5218 | 4052 | mark_reg_unknown(env, regs, BPF_REG_0); |
17a52670 AS |
4053 | } else if (fn->ret_type == RET_VOID) { |
4054 | regs[BPF_REG_0].type = NOT_INIT; | |
3e6a4b3e RG |
4055 | } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL || |
4056 | fn->ret_type == RET_PTR_TO_MAP_VALUE) { | |
f1174f77 | 4057 | /* There is no offset yet applied, variable or fixed */ |
61bd5218 | 4058 | mark_reg_known_zero(env, regs, BPF_REG_0); |
17a52670 AS |
4059 | /* remember map_ptr, so that check_map_access() |
4060 | * can check 'value_size' boundary of memory access | |
4061 | * to map element returned from bpf_map_lookup_elem() | |
4062 | */ | |
33ff9823 | 4063 | if (meta.map_ptr == NULL) { |
61bd5218 JK |
4064 | verbose(env, |
4065 | "kernel subsystem misconfigured verifier\n"); | |
17a52670 AS |
4066 | return -EINVAL; |
4067 | } | |
33ff9823 | 4068 | regs[BPF_REG_0].map_ptr = meta.map_ptr; |
4d31f301 DB |
4069 | if (fn->ret_type == RET_PTR_TO_MAP_VALUE) { |
4070 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE; | |
e16d2f1a AS |
4071 | if (map_value_has_spin_lock(meta.map_ptr)) |
4072 | regs[BPF_REG_0].id = ++env->id_gen; | |
4d31f301 DB |
4073 | } else { |
4074 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL; | |
4075 | regs[BPF_REG_0].id = ++env->id_gen; | |
4076 | } | |
c64b7983 JS |
4077 | } else if (fn->ret_type == RET_PTR_TO_SOCKET_OR_NULL) { |
4078 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
4079 | regs[BPF_REG_0].type = PTR_TO_SOCKET_OR_NULL; | |
0f3adc28 | 4080 | regs[BPF_REG_0].id = ++env->id_gen; |
85a51f8c LB |
4081 | } else if (fn->ret_type == RET_PTR_TO_SOCK_COMMON_OR_NULL) { |
4082 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
4083 | regs[BPF_REG_0].type = PTR_TO_SOCK_COMMON_OR_NULL; | |
4084 | regs[BPF_REG_0].id = ++env->id_gen; | |
655a51e5 MKL |
4085 | } else if (fn->ret_type == RET_PTR_TO_TCP_SOCK_OR_NULL) { |
4086 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
4087 | regs[BPF_REG_0].type = PTR_TO_TCP_SOCK_OR_NULL; | |
4088 | regs[BPF_REG_0].id = ++env->id_gen; | |
17a52670 | 4089 | } else { |
61bd5218 | 4090 | verbose(env, "unknown return type %d of func %s#%d\n", |
ebb676da | 4091 | fn->ret_type, func_id_name(func_id), func_id); |
17a52670 AS |
4092 | return -EINVAL; |
4093 | } | |
04fd61ab | 4094 | |
0f3adc28 | 4095 | if (is_ptr_cast_function(func_id)) { |
1b986589 MKL |
4096 | /* For release_reference() */ |
4097 | regs[BPF_REG_0].ref_obj_id = meta.ref_obj_id; | |
0f3adc28 LB |
4098 | } else if (is_acquire_function(func_id)) { |
4099 | int id = acquire_reference_state(env, insn_idx); | |
4100 | ||
4101 | if (id < 0) | |
4102 | return id; | |
4103 | /* For mark_ptr_or_null_reg() */ | |
4104 | regs[BPF_REG_0].id = id; | |
4105 | /* For release_reference() */ | |
4106 | regs[BPF_REG_0].ref_obj_id = id; | |
4107 | } | |
1b986589 | 4108 | |
849fa506 YS |
4109 | do_refine_retval_range(regs, fn->ret_type, func_id, &meta); |
4110 | ||
61bd5218 | 4111 | err = check_map_func_compatibility(env, meta.map_ptr, func_id); |
35578d79 KX |
4112 | if (err) |
4113 | return err; | |
04fd61ab | 4114 | |
c195651e YS |
4115 | if (func_id == BPF_FUNC_get_stack && !env->prog->has_callchain_buf) { |
4116 | const char *err_str; | |
4117 | ||
4118 | #ifdef CONFIG_PERF_EVENTS | |
4119 | err = get_callchain_buffers(sysctl_perf_event_max_stack); | |
4120 | err_str = "cannot get callchain buffer for func %s#%d\n"; | |
4121 | #else | |
4122 | err = -ENOTSUPP; | |
4123 | err_str = "func %s#%d not supported without CONFIG_PERF_EVENTS\n"; | |
4124 | #endif | |
4125 | if (err) { | |
4126 | verbose(env, err_str, func_id_name(func_id), func_id); | |
4127 | return err; | |
4128 | } | |
4129 | ||
4130 | env->prog->has_callchain_buf = true; | |
4131 | } | |
4132 | ||
969bf05e AS |
4133 | if (changes_data) |
4134 | clear_all_pkt_pointers(env); | |
4135 | return 0; | |
4136 | } | |
4137 | ||
b03c9f9f EC |
4138 | static bool signed_add_overflows(s64 a, s64 b) |
4139 | { | |
4140 | /* Do the add in u64, where overflow is well-defined */ | |
4141 | s64 res = (s64)((u64)a + (u64)b); | |
4142 | ||
4143 | if (b < 0) | |
4144 | return res > a; | |
4145 | return res < a; | |
4146 | } | |
4147 | ||
4148 | static bool signed_sub_overflows(s64 a, s64 b) | |
4149 | { | |
4150 | /* Do the sub in u64, where overflow is well-defined */ | |
4151 | s64 res = (s64)((u64)a - (u64)b); | |
4152 | ||
4153 | if (b < 0) | |
4154 | return res < a; | |
4155 | return res > a; | |
969bf05e AS |
4156 | } |
4157 | ||
bb7f0f98 AS |
4158 | static bool check_reg_sane_offset(struct bpf_verifier_env *env, |
4159 | const struct bpf_reg_state *reg, | |
4160 | enum bpf_reg_type type) | |
4161 | { | |
4162 | bool known = tnum_is_const(reg->var_off); | |
4163 | s64 val = reg->var_off.value; | |
4164 | s64 smin = reg->smin_value; | |
4165 | ||
4166 | if (known && (val >= BPF_MAX_VAR_OFF || val <= -BPF_MAX_VAR_OFF)) { | |
4167 | verbose(env, "math between %s pointer and %lld is not allowed\n", | |
4168 | reg_type_str[type], val); | |
4169 | return false; | |
4170 | } | |
4171 | ||
4172 | if (reg->off >= BPF_MAX_VAR_OFF || reg->off <= -BPF_MAX_VAR_OFF) { | |
4173 | verbose(env, "%s pointer offset %d is not allowed\n", | |
4174 | reg_type_str[type], reg->off); | |
4175 | return false; | |
4176 | } | |
4177 | ||
4178 | if (smin == S64_MIN) { | |
4179 | verbose(env, "math between %s pointer and register with unbounded min value is not allowed\n", | |
4180 | reg_type_str[type]); | |
4181 | return false; | |
4182 | } | |
4183 | ||
4184 | if (smin >= BPF_MAX_VAR_OFF || smin <= -BPF_MAX_VAR_OFF) { | |
4185 | verbose(env, "value %lld makes %s pointer be out of bounds\n", | |
4186 | smin, reg_type_str[type]); | |
4187 | return false; | |
4188 | } | |
4189 | ||
4190 | return true; | |
4191 | } | |
4192 | ||
979d63d5 DB |
4193 | static struct bpf_insn_aux_data *cur_aux(struct bpf_verifier_env *env) |
4194 | { | |
4195 | return &env->insn_aux_data[env->insn_idx]; | |
4196 | } | |
4197 | ||
4198 | static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg, | |
4199 | u32 *ptr_limit, u8 opcode, bool off_is_neg) | |
4200 | { | |
4201 | bool mask_to_left = (opcode == BPF_ADD && off_is_neg) || | |
4202 | (opcode == BPF_SUB && !off_is_neg); | |
4203 | u32 off; | |
4204 | ||
4205 | switch (ptr_reg->type) { | |
4206 | case PTR_TO_STACK: | |
088ec26d AI |
4207 | /* Indirect variable offset stack access is prohibited in |
4208 | * unprivileged mode so it's not handled here. | |
4209 | */ | |
979d63d5 DB |
4210 | off = ptr_reg->off + ptr_reg->var_off.value; |
4211 | if (mask_to_left) | |
4212 | *ptr_limit = MAX_BPF_STACK + off; | |
4213 | else | |
4214 | *ptr_limit = -off; | |
4215 | return 0; | |
4216 | case PTR_TO_MAP_VALUE: | |
4217 | if (mask_to_left) { | |
4218 | *ptr_limit = ptr_reg->umax_value + ptr_reg->off; | |
4219 | } else { | |
4220 | off = ptr_reg->smin_value + ptr_reg->off; | |
4221 | *ptr_limit = ptr_reg->map_ptr->value_size - off; | |
4222 | } | |
4223 | return 0; | |
4224 | default: | |
4225 | return -EINVAL; | |
4226 | } | |
4227 | } | |
4228 | ||
d3bd7413 DB |
4229 | static bool can_skip_alu_sanitation(const struct bpf_verifier_env *env, |
4230 | const struct bpf_insn *insn) | |
4231 | { | |
4232 | return env->allow_ptr_leaks || BPF_SRC(insn->code) == BPF_K; | |
4233 | } | |
4234 | ||
4235 | static int update_alu_sanitation_state(struct bpf_insn_aux_data *aux, | |
4236 | u32 alu_state, u32 alu_limit) | |
4237 | { | |
4238 | /* If we arrived here from different branches with different | |
4239 | * state or limits to sanitize, then this won't work. | |
4240 | */ | |
4241 | if (aux->alu_state && | |
4242 | (aux->alu_state != alu_state || | |
4243 | aux->alu_limit != alu_limit)) | |
4244 | return -EACCES; | |
4245 | ||
4246 | /* Corresponding fixup done in fixup_bpf_calls(). */ | |
4247 | aux->alu_state = alu_state; | |
4248 | aux->alu_limit = alu_limit; | |
4249 | return 0; | |
4250 | } | |
4251 | ||
4252 | static int sanitize_val_alu(struct bpf_verifier_env *env, | |
4253 | struct bpf_insn *insn) | |
4254 | { | |
4255 | struct bpf_insn_aux_data *aux = cur_aux(env); | |
4256 | ||
4257 | if (can_skip_alu_sanitation(env, insn)) | |
4258 | return 0; | |
4259 | ||
4260 | return update_alu_sanitation_state(aux, BPF_ALU_NON_POINTER, 0); | |
4261 | } | |
4262 | ||
979d63d5 DB |
4263 | static int sanitize_ptr_alu(struct bpf_verifier_env *env, |
4264 | struct bpf_insn *insn, | |
4265 | const struct bpf_reg_state *ptr_reg, | |
4266 | struct bpf_reg_state *dst_reg, | |
4267 | bool off_is_neg) | |
4268 | { | |
4269 | struct bpf_verifier_state *vstate = env->cur_state; | |
4270 | struct bpf_insn_aux_data *aux = cur_aux(env); | |
4271 | bool ptr_is_dst_reg = ptr_reg == dst_reg; | |
4272 | u8 opcode = BPF_OP(insn->code); | |
4273 | u32 alu_state, alu_limit; | |
4274 | struct bpf_reg_state tmp; | |
4275 | bool ret; | |
4276 | ||
d3bd7413 | 4277 | if (can_skip_alu_sanitation(env, insn)) |
979d63d5 DB |
4278 | return 0; |
4279 | ||
4280 | /* We already marked aux for masking from non-speculative | |
4281 | * paths, thus we got here in the first place. We only care | |
4282 | * to explore bad access from here. | |
4283 | */ | |
4284 | if (vstate->speculative) | |
4285 | goto do_sim; | |
4286 | ||
4287 | alu_state = off_is_neg ? BPF_ALU_NEG_VALUE : 0; | |
4288 | alu_state |= ptr_is_dst_reg ? | |
4289 | BPF_ALU_SANITIZE_SRC : BPF_ALU_SANITIZE_DST; | |
4290 | ||
4291 | if (retrieve_ptr_limit(ptr_reg, &alu_limit, opcode, off_is_neg)) | |
4292 | return 0; | |
d3bd7413 | 4293 | if (update_alu_sanitation_state(aux, alu_state, alu_limit)) |
979d63d5 | 4294 | return -EACCES; |
979d63d5 DB |
4295 | do_sim: |
4296 | /* Simulate and find potential out-of-bounds access under | |
4297 | * speculative execution from truncation as a result of | |
4298 | * masking when off was not within expected range. If off | |
4299 | * sits in dst, then we temporarily need to move ptr there | |
4300 | * to simulate dst (== 0) +/-= ptr. Needed, for example, | |
4301 | * for cases where we use K-based arithmetic in one direction | |
4302 | * and truncated reg-based in the other in order to explore | |
4303 | * bad access. | |
4304 | */ | |
4305 | if (!ptr_is_dst_reg) { | |
4306 | tmp = *dst_reg; | |
4307 | *dst_reg = *ptr_reg; | |
4308 | } | |
4309 | ret = push_stack(env, env->insn_idx + 1, env->insn_idx, true); | |
0803278b | 4310 | if (!ptr_is_dst_reg && ret) |
979d63d5 DB |
4311 | *dst_reg = tmp; |
4312 | return !ret ? -EFAULT : 0; | |
4313 | } | |
4314 | ||
f1174f77 | 4315 | /* Handles arithmetic on a pointer and a scalar: computes new min/max and var_off. |
f1174f77 EC |
4316 | * Caller should also handle BPF_MOV case separately. |
4317 | * If we return -EACCES, caller may want to try again treating pointer as a | |
4318 | * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks. | |
4319 | */ | |
4320 | static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, | |
4321 | struct bpf_insn *insn, | |
4322 | const struct bpf_reg_state *ptr_reg, | |
4323 | const struct bpf_reg_state *off_reg) | |
969bf05e | 4324 | { |
f4d7e40a AS |
4325 | struct bpf_verifier_state *vstate = env->cur_state; |
4326 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
4327 | struct bpf_reg_state *regs = state->regs, *dst_reg; | |
f1174f77 | 4328 | bool known = tnum_is_const(off_reg->var_off); |
b03c9f9f EC |
4329 | s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value, |
4330 | smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value; | |
4331 | u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value, | |
4332 | umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value; | |
9d7eceed | 4333 | u32 dst = insn->dst_reg, src = insn->src_reg; |
969bf05e | 4334 | u8 opcode = BPF_OP(insn->code); |
979d63d5 | 4335 | int ret; |
969bf05e | 4336 | |
f1174f77 | 4337 | dst_reg = ®s[dst]; |
969bf05e | 4338 | |
6f16101e DB |
4339 | if ((known && (smin_val != smax_val || umin_val != umax_val)) || |
4340 | smin_val > smax_val || umin_val > umax_val) { | |
4341 | /* Taint dst register if offset had invalid bounds derived from | |
4342 | * e.g. dead branches. | |
4343 | */ | |
4344 | __mark_reg_unknown(dst_reg); | |
4345 | return 0; | |
f1174f77 EC |
4346 | } |
4347 | ||
4348 | if (BPF_CLASS(insn->code) != BPF_ALU64) { | |
4349 | /* 32-bit ALU ops on pointers produce (meaningless) scalars */ | |
82abbf8d AS |
4350 | verbose(env, |
4351 | "R%d 32-bit pointer arithmetic prohibited\n", | |
4352 | dst); | |
f1174f77 | 4353 | return -EACCES; |
969bf05e AS |
4354 | } |
4355 | ||
aad2eeaf JS |
4356 | switch (ptr_reg->type) { |
4357 | case PTR_TO_MAP_VALUE_OR_NULL: | |
4358 | verbose(env, "R%d pointer arithmetic on %s prohibited, null-check it first\n", | |
4359 | dst, reg_type_str[ptr_reg->type]); | |
f1174f77 | 4360 | return -EACCES; |
aad2eeaf JS |
4361 | case CONST_PTR_TO_MAP: |
4362 | case PTR_TO_PACKET_END: | |
c64b7983 JS |
4363 | case PTR_TO_SOCKET: |
4364 | case PTR_TO_SOCKET_OR_NULL: | |
46f8bc92 MKL |
4365 | case PTR_TO_SOCK_COMMON: |
4366 | case PTR_TO_SOCK_COMMON_OR_NULL: | |
655a51e5 MKL |
4367 | case PTR_TO_TCP_SOCK: |
4368 | case PTR_TO_TCP_SOCK_OR_NULL: | |
fada7fdc | 4369 | case PTR_TO_XDP_SOCK: |
aad2eeaf JS |
4370 | verbose(env, "R%d pointer arithmetic on %s prohibited\n", |
4371 | dst, reg_type_str[ptr_reg->type]); | |
f1174f77 | 4372 | return -EACCES; |
9d7eceed DB |
4373 | case PTR_TO_MAP_VALUE: |
4374 | if (!env->allow_ptr_leaks && !known && (smin_val < 0) != (smax_val < 0)) { | |
4375 | verbose(env, "R%d has unknown scalar with mixed signed bounds, pointer arithmetic with it prohibited for !root\n", | |
4376 | off_reg == dst_reg ? dst : src); | |
4377 | return -EACCES; | |
4378 | } | |
4379 | /* fall-through */ | |
aad2eeaf JS |
4380 | default: |
4381 | break; | |
f1174f77 EC |
4382 | } |
4383 | ||
4384 | /* In case of 'scalar += pointer', dst_reg inherits pointer type and id. | |
4385 | * The id may be overwritten later if we create a new variable offset. | |
969bf05e | 4386 | */ |
f1174f77 EC |
4387 | dst_reg->type = ptr_reg->type; |
4388 | dst_reg->id = ptr_reg->id; | |
969bf05e | 4389 | |
bb7f0f98 AS |
4390 | if (!check_reg_sane_offset(env, off_reg, ptr_reg->type) || |
4391 | !check_reg_sane_offset(env, ptr_reg, ptr_reg->type)) | |
4392 | return -EINVAL; | |
4393 | ||
f1174f77 EC |
4394 | switch (opcode) { |
4395 | case BPF_ADD: | |
979d63d5 DB |
4396 | ret = sanitize_ptr_alu(env, insn, ptr_reg, dst_reg, smin_val < 0); |
4397 | if (ret < 0) { | |
4398 | verbose(env, "R%d tried to add from different maps or paths\n", dst); | |
4399 | return ret; | |
4400 | } | |
f1174f77 EC |
4401 | /* We can take a fixed offset as long as it doesn't overflow |
4402 | * the s32 'off' field | |
969bf05e | 4403 | */ |
b03c9f9f EC |
4404 | if (known && (ptr_reg->off + smin_val == |
4405 | (s64)(s32)(ptr_reg->off + smin_val))) { | |
f1174f77 | 4406 | /* pointer += K. Accumulate it into fixed offset */ |
b03c9f9f EC |
4407 | dst_reg->smin_value = smin_ptr; |
4408 | dst_reg->smax_value = smax_ptr; | |
4409 | dst_reg->umin_value = umin_ptr; | |
4410 | dst_reg->umax_value = umax_ptr; | |
f1174f77 | 4411 | dst_reg->var_off = ptr_reg->var_off; |
b03c9f9f | 4412 | dst_reg->off = ptr_reg->off + smin_val; |
0962590e | 4413 | dst_reg->raw = ptr_reg->raw; |
f1174f77 EC |
4414 | break; |
4415 | } | |
f1174f77 EC |
4416 | /* A new variable offset is created. Note that off_reg->off |
4417 | * == 0, since it's a scalar. | |
4418 | * dst_reg gets the pointer type and since some positive | |
4419 | * integer value was added to the pointer, give it a new 'id' | |
4420 | * if it's a PTR_TO_PACKET. | |
4421 | * this creates a new 'base' pointer, off_reg (variable) gets | |
4422 | * added into the variable offset, and we copy the fixed offset | |
4423 | * from ptr_reg. | |
969bf05e | 4424 | */ |
b03c9f9f EC |
4425 | if (signed_add_overflows(smin_ptr, smin_val) || |
4426 | signed_add_overflows(smax_ptr, smax_val)) { | |
4427 | dst_reg->smin_value = S64_MIN; | |
4428 | dst_reg->smax_value = S64_MAX; | |
4429 | } else { | |
4430 | dst_reg->smin_value = smin_ptr + smin_val; | |
4431 | dst_reg->smax_value = smax_ptr + smax_val; | |
4432 | } | |
4433 | if (umin_ptr + umin_val < umin_ptr || | |
4434 | umax_ptr + umax_val < umax_ptr) { | |
4435 | dst_reg->umin_value = 0; | |
4436 | dst_reg->umax_value = U64_MAX; | |
4437 | } else { | |
4438 | dst_reg->umin_value = umin_ptr + umin_val; | |
4439 | dst_reg->umax_value = umax_ptr + umax_val; | |
4440 | } | |
f1174f77 EC |
4441 | dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off); |
4442 | dst_reg->off = ptr_reg->off; | |
0962590e | 4443 | dst_reg->raw = ptr_reg->raw; |
de8f3a83 | 4444 | if (reg_is_pkt_pointer(ptr_reg)) { |
f1174f77 EC |
4445 | dst_reg->id = ++env->id_gen; |
4446 | /* something was added to pkt_ptr, set range to zero */ | |
0962590e | 4447 | dst_reg->raw = 0; |
f1174f77 EC |
4448 | } |
4449 | break; | |
4450 | case BPF_SUB: | |
979d63d5 DB |
4451 | ret = sanitize_ptr_alu(env, insn, ptr_reg, dst_reg, smin_val < 0); |
4452 | if (ret < 0) { | |
4453 | verbose(env, "R%d tried to sub from different maps or paths\n", dst); | |
4454 | return ret; | |
4455 | } | |
f1174f77 EC |
4456 | if (dst_reg == off_reg) { |
4457 | /* scalar -= pointer. Creates an unknown scalar */ | |
82abbf8d AS |
4458 | verbose(env, "R%d tried to subtract pointer from scalar\n", |
4459 | dst); | |
f1174f77 EC |
4460 | return -EACCES; |
4461 | } | |
4462 | /* We don't allow subtraction from FP, because (according to | |
4463 | * test_verifier.c test "invalid fp arithmetic", JITs might not | |
4464 | * be able to deal with it. | |
969bf05e | 4465 | */ |
f1174f77 | 4466 | if (ptr_reg->type == PTR_TO_STACK) { |
82abbf8d AS |
4467 | verbose(env, "R%d subtraction from stack pointer prohibited\n", |
4468 | dst); | |
f1174f77 EC |
4469 | return -EACCES; |
4470 | } | |
b03c9f9f EC |
4471 | if (known && (ptr_reg->off - smin_val == |
4472 | (s64)(s32)(ptr_reg->off - smin_val))) { | |
f1174f77 | 4473 | /* pointer -= K. Subtract it from fixed offset */ |
b03c9f9f EC |
4474 | dst_reg->smin_value = smin_ptr; |
4475 | dst_reg->smax_value = smax_ptr; | |
4476 | dst_reg->umin_value = umin_ptr; | |
4477 | dst_reg->umax_value = umax_ptr; | |
f1174f77 EC |
4478 | dst_reg->var_off = ptr_reg->var_off; |
4479 | dst_reg->id = ptr_reg->id; | |
b03c9f9f | 4480 | dst_reg->off = ptr_reg->off - smin_val; |
0962590e | 4481 | dst_reg->raw = ptr_reg->raw; |
f1174f77 EC |
4482 | break; |
4483 | } | |
f1174f77 EC |
4484 | /* A new variable offset is created. If the subtrahend is known |
4485 | * nonnegative, then any reg->range we had before is still good. | |
969bf05e | 4486 | */ |
b03c9f9f EC |
4487 | if (signed_sub_overflows(smin_ptr, smax_val) || |
4488 | signed_sub_overflows(smax_ptr, smin_val)) { | |
4489 | /* Overflow possible, we know nothing */ | |
4490 | dst_reg->smin_value = S64_MIN; | |
4491 | dst_reg->smax_value = S64_MAX; | |
4492 | } else { | |
4493 | dst_reg->smin_value = smin_ptr - smax_val; | |
4494 | dst_reg->smax_value = smax_ptr - smin_val; | |
4495 | } | |
4496 | if (umin_ptr < umax_val) { | |
4497 | /* Overflow possible, we know nothing */ | |
4498 | dst_reg->umin_value = 0; | |
4499 | dst_reg->umax_value = U64_MAX; | |
4500 | } else { | |
4501 | /* Cannot overflow (as long as bounds are consistent) */ | |
4502 | dst_reg->umin_value = umin_ptr - umax_val; | |
4503 | dst_reg->umax_value = umax_ptr - umin_val; | |
4504 | } | |
f1174f77 EC |
4505 | dst_reg->var_off = tnum_sub(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 */ | |
b03c9f9f | 4511 | if (smin_val < 0) |
0962590e | 4512 | dst_reg->raw = 0; |
43188702 | 4513 | } |
f1174f77 EC |
4514 | break; |
4515 | case BPF_AND: | |
4516 | case BPF_OR: | |
4517 | case BPF_XOR: | |
82abbf8d AS |
4518 | /* bitwise ops on pointers are troublesome, prohibit. */ |
4519 | verbose(env, "R%d bitwise operator %s on pointer prohibited\n", | |
4520 | dst, bpf_alu_string[opcode >> 4]); | |
f1174f77 EC |
4521 | return -EACCES; |
4522 | default: | |
4523 | /* other operators (e.g. MUL,LSH) produce non-pointer results */ | |
82abbf8d AS |
4524 | verbose(env, "R%d pointer arithmetic with %s operator prohibited\n", |
4525 | dst, bpf_alu_string[opcode >> 4]); | |
f1174f77 | 4526 | return -EACCES; |
43188702 JF |
4527 | } |
4528 | ||
bb7f0f98 AS |
4529 | if (!check_reg_sane_offset(env, dst_reg, ptr_reg->type)) |
4530 | return -EINVAL; | |
4531 | ||
b03c9f9f EC |
4532 | __update_reg_bounds(dst_reg); |
4533 | __reg_deduce_bounds(dst_reg); | |
4534 | __reg_bound_offset(dst_reg); | |
0d6303db DB |
4535 | |
4536 | /* For unprivileged we require that resulting offset must be in bounds | |
4537 | * in order to be able to sanitize access later on. | |
4538 | */ | |
e4298d25 DB |
4539 | if (!env->allow_ptr_leaks) { |
4540 | if (dst_reg->type == PTR_TO_MAP_VALUE && | |
4541 | check_map_access(env, dst, dst_reg->off, 1, false)) { | |
4542 | verbose(env, "R%d pointer arithmetic of map value goes out of range, " | |
4543 | "prohibited for !root\n", dst); | |
4544 | return -EACCES; | |
4545 | } else if (dst_reg->type == PTR_TO_STACK && | |
4546 | check_stack_access(env, dst_reg, dst_reg->off + | |
4547 | dst_reg->var_off.value, 1)) { | |
4548 | verbose(env, "R%d stack pointer arithmetic goes out of range, " | |
4549 | "prohibited for !root\n", dst); | |
4550 | return -EACCES; | |
4551 | } | |
0d6303db DB |
4552 | } |
4553 | ||
43188702 JF |
4554 | return 0; |
4555 | } | |
4556 | ||
468f6eaf JH |
4557 | /* WARNING: This function does calculations on 64-bit values, but the actual |
4558 | * execution may occur on 32-bit values. Therefore, things like bitshifts | |
4559 | * need extra checks in the 32-bit case. | |
4560 | */ | |
f1174f77 EC |
4561 | static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env, |
4562 | struct bpf_insn *insn, | |
4563 | struct bpf_reg_state *dst_reg, | |
4564 | struct bpf_reg_state src_reg) | |
969bf05e | 4565 | { |
638f5b90 | 4566 | struct bpf_reg_state *regs = cur_regs(env); |
48461135 | 4567 | u8 opcode = BPF_OP(insn->code); |
f1174f77 | 4568 | bool src_known, dst_known; |
b03c9f9f EC |
4569 | s64 smin_val, smax_val; |
4570 | u64 umin_val, umax_val; | |
468f6eaf | 4571 | u64 insn_bitness = (BPF_CLASS(insn->code) == BPF_ALU64) ? 64 : 32; |
d3bd7413 DB |
4572 | u32 dst = insn->dst_reg; |
4573 | int ret; | |
48461135 | 4574 | |
b799207e JH |
4575 | if (insn_bitness == 32) { |
4576 | /* Relevant for 32-bit RSH: Information can propagate towards | |
4577 | * LSB, so it isn't sufficient to only truncate the output to | |
4578 | * 32 bits. | |
4579 | */ | |
4580 | coerce_reg_to_size(dst_reg, 4); | |
4581 | coerce_reg_to_size(&src_reg, 4); | |
4582 | } | |
4583 | ||
b03c9f9f EC |
4584 | smin_val = src_reg.smin_value; |
4585 | smax_val = src_reg.smax_value; | |
4586 | umin_val = src_reg.umin_value; | |
4587 | umax_val = src_reg.umax_value; | |
f1174f77 EC |
4588 | src_known = tnum_is_const(src_reg.var_off); |
4589 | dst_known = tnum_is_const(dst_reg->var_off); | |
f23cc643 | 4590 | |
6f16101e DB |
4591 | if ((src_known && (smin_val != smax_val || umin_val != umax_val)) || |
4592 | smin_val > smax_val || umin_val > umax_val) { | |
4593 | /* Taint dst register if offset had invalid bounds derived from | |
4594 | * e.g. dead branches. | |
4595 | */ | |
4596 | __mark_reg_unknown(dst_reg); | |
4597 | return 0; | |
4598 | } | |
4599 | ||
bb7f0f98 AS |
4600 | if (!src_known && |
4601 | opcode != BPF_ADD && opcode != BPF_SUB && opcode != BPF_AND) { | |
4602 | __mark_reg_unknown(dst_reg); | |
4603 | return 0; | |
4604 | } | |
4605 | ||
48461135 JB |
4606 | switch (opcode) { |
4607 | case BPF_ADD: | |
d3bd7413 DB |
4608 | ret = sanitize_val_alu(env, insn); |
4609 | if (ret < 0) { | |
4610 | verbose(env, "R%d tried to add from different pointers or scalars\n", dst); | |
4611 | return ret; | |
4612 | } | |
b03c9f9f EC |
4613 | if (signed_add_overflows(dst_reg->smin_value, smin_val) || |
4614 | signed_add_overflows(dst_reg->smax_value, smax_val)) { | |
4615 | dst_reg->smin_value = S64_MIN; | |
4616 | dst_reg->smax_value = S64_MAX; | |
4617 | } else { | |
4618 | dst_reg->smin_value += smin_val; | |
4619 | dst_reg->smax_value += smax_val; | |
4620 | } | |
4621 | if (dst_reg->umin_value + umin_val < umin_val || | |
4622 | dst_reg->umax_value + umax_val < umax_val) { | |
4623 | dst_reg->umin_value = 0; | |
4624 | dst_reg->umax_value = U64_MAX; | |
4625 | } else { | |
4626 | dst_reg->umin_value += umin_val; | |
4627 | dst_reg->umax_value += umax_val; | |
4628 | } | |
f1174f77 | 4629 | dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off); |
48461135 JB |
4630 | break; |
4631 | case BPF_SUB: | |
d3bd7413 DB |
4632 | ret = sanitize_val_alu(env, insn); |
4633 | if (ret < 0) { | |
4634 | verbose(env, "R%d tried to sub from different pointers or scalars\n", dst); | |
4635 | return ret; | |
4636 | } | |
b03c9f9f EC |
4637 | if (signed_sub_overflows(dst_reg->smin_value, smax_val) || |
4638 | signed_sub_overflows(dst_reg->smax_value, smin_val)) { | |
4639 | /* Overflow possible, we know nothing */ | |
4640 | dst_reg->smin_value = S64_MIN; | |
4641 | dst_reg->smax_value = S64_MAX; | |
4642 | } else { | |
4643 | dst_reg->smin_value -= smax_val; | |
4644 | dst_reg->smax_value -= smin_val; | |
4645 | } | |
4646 | if (dst_reg->umin_value < umax_val) { | |
4647 | /* Overflow possible, we know nothing */ | |
4648 | dst_reg->umin_value = 0; | |
4649 | dst_reg->umax_value = U64_MAX; | |
4650 | } else { | |
4651 | /* Cannot overflow (as long as bounds are consistent) */ | |
4652 | dst_reg->umin_value -= umax_val; | |
4653 | dst_reg->umax_value -= umin_val; | |
4654 | } | |
f1174f77 | 4655 | dst_reg->var_off = tnum_sub(dst_reg->var_off, src_reg.var_off); |
48461135 JB |
4656 | break; |
4657 | case BPF_MUL: | |
b03c9f9f EC |
4658 | dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off); |
4659 | if (smin_val < 0 || dst_reg->smin_value < 0) { | |
f1174f77 | 4660 | /* Ain't nobody got time to multiply that sign */ |
b03c9f9f EC |
4661 | __mark_reg_unbounded(dst_reg); |
4662 | __update_reg_bounds(dst_reg); | |
f1174f77 EC |
4663 | break; |
4664 | } | |
b03c9f9f EC |
4665 | /* Both values are positive, so we can work with unsigned and |
4666 | * copy the result to signed (unless it exceeds S64_MAX). | |
f1174f77 | 4667 | */ |
b03c9f9f EC |
4668 | if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) { |
4669 | /* Potential overflow, we know nothing */ | |
4670 | __mark_reg_unbounded(dst_reg); | |
4671 | /* (except what we can learn from the var_off) */ | |
4672 | __update_reg_bounds(dst_reg); | |
4673 | break; | |
4674 | } | |
4675 | dst_reg->umin_value *= umin_val; | |
4676 | dst_reg->umax_value *= umax_val; | |
4677 | if (dst_reg->umax_value > S64_MAX) { | |
4678 | /* Overflow possible, we know nothing */ | |
4679 | dst_reg->smin_value = S64_MIN; | |
4680 | dst_reg->smax_value = S64_MAX; | |
4681 | } else { | |
4682 | dst_reg->smin_value = dst_reg->umin_value; | |
4683 | dst_reg->smax_value = dst_reg->umax_value; | |
4684 | } | |
48461135 JB |
4685 | break; |
4686 | case BPF_AND: | |
f1174f77 | 4687 | if (src_known && dst_known) { |
b03c9f9f EC |
4688 | __mark_reg_known(dst_reg, dst_reg->var_off.value & |
4689 | src_reg.var_off.value); | |
f1174f77 EC |
4690 | break; |
4691 | } | |
b03c9f9f EC |
4692 | /* We get our minimum from the var_off, since that's inherently |
4693 | * bitwise. Our maximum is the minimum of the operands' maxima. | |
f23cc643 | 4694 | */ |
f1174f77 | 4695 | dst_reg->var_off = tnum_and(dst_reg->var_off, src_reg.var_off); |
b03c9f9f EC |
4696 | dst_reg->umin_value = dst_reg->var_off.value; |
4697 | dst_reg->umax_value = min(dst_reg->umax_value, umax_val); | |
4698 | if (dst_reg->smin_value < 0 || smin_val < 0) { | |
4699 | /* Lose signed bounds when ANDing negative numbers, | |
4700 | * ain't nobody got time for that. | |
4701 | */ | |
4702 | dst_reg->smin_value = S64_MIN; | |
4703 | dst_reg->smax_value = S64_MAX; | |
4704 | } else { | |
4705 | /* ANDing two positives gives a positive, so safe to | |
4706 | * cast result into s64. | |
4707 | */ | |
4708 | dst_reg->smin_value = dst_reg->umin_value; | |
4709 | dst_reg->smax_value = dst_reg->umax_value; | |
4710 | } | |
4711 | /* We may learn something more from the var_off */ | |
4712 | __update_reg_bounds(dst_reg); | |
f1174f77 EC |
4713 | break; |
4714 | case BPF_OR: | |
4715 | if (src_known && dst_known) { | |
b03c9f9f EC |
4716 | __mark_reg_known(dst_reg, dst_reg->var_off.value | |
4717 | src_reg.var_off.value); | |
f1174f77 EC |
4718 | break; |
4719 | } | |
b03c9f9f EC |
4720 | /* We get our maximum from the var_off, and our minimum is the |
4721 | * maximum of the operands' minima | |
f1174f77 EC |
4722 | */ |
4723 | dst_reg->var_off = tnum_or(dst_reg->var_off, src_reg.var_off); | |
b03c9f9f EC |
4724 | dst_reg->umin_value = max(dst_reg->umin_value, umin_val); |
4725 | dst_reg->umax_value = dst_reg->var_off.value | | |
4726 | dst_reg->var_off.mask; | |
4727 | if (dst_reg->smin_value < 0 || smin_val < 0) { | |
4728 | /* Lose signed bounds when ORing negative numbers, | |
4729 | * ain't nobody got time for that. | |
4730 | */ | |
4731 | dst_reg->smin_value = S64_MIN; | |
4732 | dst_reg->smax_value = S64_MAX; | |
f1174f77 | 4733 | } else { |
b03c9f9f EC |
4734 | /* ORing two positives gives a positive, so safe to |
4735 | * cast result into s64. | |
4736 | */ | |
4737 | dst_reg->smin_value = dst_reg->umin_value; | |
4738 | dst_reg->smax_value = dst_reg->umax_value; | |
f1174f77 | 4739 | } |
b03c9f9f EC |
4740 | /* We may learn something more from the var_off */ |
4741 | __update_reg_bounds(dst_reg); | |
48461135 JB |
4742 | break; |
4743 | case BPF_LSH: | |
468f6eaf JH |
4744 | if (umax_val >= insn_bitness) { |
4745 | /* Shifts greater than 31 or 63 are undefined. | |
4746 | * This includes shifts by a negative number. | |
b03c9f9f | 4747 | */ |
61bd5218 | 4748 | mark_reg_unknown(env, regs, insn->dst_reg); |
f1174f77 EC |
4749 | break; |
4750 | } | |
b03c9f9f EC |
4751 | /* We lose all sign bit information (except what we can pick |
4752 | * up from var_off) | |
48461135 | 4753 | */ |
b03c9f9f EC |
4754 | dst_reg->smin_value = S64_MIN; |
4755 | dst_reg->smax_value = S64_MAX; | |
4756 | /* If we might shift our top bit out, then we know nothing */ | |
4757 | if (dst_reg->umax_value > 1ULL << (63 - umax_val)) { | |
4758 | dst_reg->umin_value = 0; | |
4759 | dst_reg->umax_value = U64_MAX; | |
d1174416 | 4760 | } else { |
b03c9f9f EC |
4761 | dst_reg->umin_value <<= umin_val; |
4762 | dst_reg->umax_value <<= umax_val; | |
d1174416 | 4763 | } |
afbe1a5b | 4764 | dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val); |
b03c9f9f EC |
4765 | /* We may learn something more from the var_off */ |
4766 | __update_reg_bounds(dst_reg); | |
48461135 JB |
4767 | break; |
4768 | case BPF_RSH: | |
468f6eaf JH |
4769 | if (umax_val >= insn_bitness) { |
4770 | /* Shifts greater than 31 or 63 are undefined. | |
4771 | * This includes shifts by a negative number. | |
b03c9f9f | 4772 | */ |
61bd5218 | 4773 | mark_reg_unknown(env, regs, insn->dst_reg); |
f1174f77 EC |
4774 | break; |
4775 | } | |
4374f256 EC |
4776 | /* BPF_RSH is an unsigned shift. If the value in dst_reg might |
4777 | * be negative, then either: | |
4778 | * 1) src_reg might be zero, so the sign bit of the result is | |
4779 | * unknown, so we lose our signed bounds | |
4780 | * 2) it's known negative, thus the unsigned bounds capture the | |
4781 | * signed bounds | |
4782 | * 3) the signed bounds cross zero, so they tell us nothing | |
4783 | * about the result | |
4784 | * If the value in dst_reg is known nonnegative, then again the | |
4785 | * unsigned bounts capture the signed bounds. | |
4786 | * Thus, in all cases it suffices to blow away our signed bounds | |
4787 | * and rely on inferring new ones from the unsigned bounds and | |
4788 | * var_off of the result. | |
4789 | */ | |
4790 | dst_reg->smin_value = S64_MIN; | |
4791 | dst_reg->smax_value = S64_MAX; | |
afbe1a5b | 4792 | dst_reg->var_off = tnum_rshift(dst_reg->var_off, umin_val); |
b03c9f9f EC |
4793 | dst_reg->umin_value >>= umax_val; |
4794 | dst_reg->umax_value >>= umin_val; | |
4795 | /* We may learn something more from the var_off */ | |
4796 | __update_reg_bounds(dst_reg); | |
48461135 | 4797 | break; |
9cbe1f5a YS |
4798 | case BPF_ARSH: |
4799 | if (umax_val >= insn_bitness) { | |
4800 | /* Shifts greater than 31 or 63 are undefined. | |
4801 | * This includes shifts by a negative number. | |
4802 | */ | |
4803 | mark_reg_unknown(env, regs, insn->dst_reg); | |
4804 | break; | |
4805 | } | |
4806 | ||
4807 | /* Upon reaching here, src_known is true and | |
4808 | * umax_val is equal to umin_val. | |
4809 | */ | |
4810 | dst_reg->smin_value >>= umin_val; | |
4811 | dst_reg->smax_value >>= umin_val; | |
4812 | dst_reg->var_off = tnum_arshift(dst_reg->var_off, umin_val); | |
4813 | ||
4814 | /* blow away the dst_reg umin_value/umax_value and rely on | |
4815 | * dst_reg var_off to refine the result. | |
4816 | */ | |
4817 | dst_reg->umin_value = 0; | |
4818 | dst_reg->umax_value = U64_MAX; | |
4819 | __update_reg_bounds(dst_reg); | |
4820 | break; | |
48461135 | 4821 | default: |
61bd5218 | 4822 | mark_reg_unknown(env, regs, insn->dst_reg); |
48461135 JB |
4823 | break; |
4824 | } | |
4825 | ||
468f6eaf JH |
4826 | if (BPF_CLASS(insn->code) != BPF_ALU64) { |
4827 | /* 32-bit ALU ops are (32,32)->32 */ | |
4828 | coerce_reg_to_size(dst_reg, 4); | |
468f6eaf JH |
4829 | } |
4830 | ||
b03c9f9f EC |
4831 | __reg_deduce_bounds(dst_reg); |
4832 | __reg_bound_offset(dst_reg); | |
f1174f77 EC |
4833 | return 0; |
4834 | } | |
4835 | ||
4836 | /* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max | |
4837 | * and var_off. | |
4838 | */ | |
4839 | static int adjust_reg_min_max_vals(struct bpf_verifier_env *env, | |
4840 | struct bpf_insn *insn) | |
4841 | { | |
f4d7e40a AS |
4842 | struct bpf_verifier_state *vstate = env->cur_state; |
4843 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
4844 | struct bpf_reg_state *regs = state->regs, *dst_reg, *src_reg; | |
f1174f77 EC |
4845 | struct bpf_reg_state *ptr_reg = NULL, off_reg = {0}; |
4846 | u8 opcode = BPF_OP(insn->code); | |
b5dc0163 | 4847 | int err; |
f1174f77 EC |
4848 | |
4849 | dst_reg = ®s[insn->dst_reg]; | |
f1174f77 EC |
4850 | src_reg = NULL; |
4851 | if (dst_reg->type != SCALAR_VALUE) | |
4852 | ptr_reg = dst_reg; | |
4853 | if (BPF_SRC(insn->code) == BPF_X) { | |
4854 | src_reg = ®s[insn->src_reg]; | |
f1174f77 EC |
4855 | if (src_reg->type != SCALAR_VALUE) { |
4856 | if (dst_reg->type != SCALAR_VALUE) { | |
4857 | /* Combining two pointers by any ALU op yields | |
82abbf8d AS |
4858 | * an arbitrary scalar. Disallow all math except |
4859 | * pointer subtraction | |
f1174f77 | 4860 | */ |
dd066823 | 4861 | if (opcode == BPF_SUB && env->allow_ptr_leaks) { |
82abbf8d AS |
4862 | mark_reg_unknown(env, regs, insn->dst_reg); |
4863 | return 0; | |
f1174f77 | 4864 | } |
82abbf8d AS |
4865 | verbose(env, "R%d pointer %s pointer prohibited\n", |
4866 | insn->dst_reg, | |
4867 | bpf_alu_string[opcode >> 4]); | |
4868 | return -EACCES; | |
f1174f77 EC |
4869 | } else { |
4870 | /* scalar += pointer | |
4871 | * This is legal, but we have to reverse our | |
4872 | * src/dest handling in computing the range | |
4873 | */ | |
b5dc0163 AS |
4874 | err = mark_chain_precision(env, insn->dst_reg); |
4875 | if (err) | |
4876 | return err; | |
82abbf8d AS |
4877 | return adjust_ptr_min_max_vals(env, insn, |
4878 | src_reg, dst_reg); | |
f1174f77 EC |
4879 | } |
4880 | } else if (ptr_reg) { | |
4881 | /* pointer += scalar */ | |
b5dc0163 AS |
4882 | err = mark_chain_precision(env, insn->src_reg); |
4883 | if (err) | |
4884 | return err; | |
82abbf8d AS |
4885 | return adjust_ptr_min_max_vals(env, insn, |
4886 | dst_reg, src_reg); | |
f1174f77 EC |
4887 | } |
4888 | } else { | |
4889 | /* Pretend the src is a reg with a known value, since we only | |
4890 | * need to be able to read from this state. | |
4891 | */ | |
4892 | off_reg.type = SCALAR_VALUE; | |
b03c9f9f | 4893 | __mark_reg_known(&off_reg, insn->imm); |
f1174f77 | 4894 | src_reg = &off_reg; |
82abbf8d AS |
4895 | if (ptr_reg) /* pointer += K */ |
4896 | return adjust_ptr_min_max_vals(env, insn, | |
4897 | ptr_reg, src_reg); | |
f1174f77 EC |
4898 | } |
4899 | ||
4900 | /* Got here implies adding two SCALAR_VALUEs */ | |
4901 | if (WARN_ON_ONCE(ptr_reg)) { | |
f4d7e40a | 4902 | print_verifier_state(env, state); |
61bd5218 | 4903 | verbose(env, "verifier internal error: unexpected ptr_reg\n"); |
f1174f77 EC |
4904 | return -EINVAL; |
4905 | } | |
4906 | if (WARN_ON(!src_reg)) { | |
f4d7e40a | 4907 | print_verifier_state(env, state); |
61bd5218 | 4908 | verbose(env, "verifier internal error: no src_reg\n"); |
f1174f77 EC |
4909 | return -EINVAL; |
4910 | } | |
4911 | return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg); | |
48461135 JB |
4912 | } |
4913 | ||
17a52670 | 4914 | /* check validity of 32-bit and 64-bit arithmetic operations */ |
58e2af8b | 4915 | static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn) |
17a52670 | 4916 | { |
638f5b90 | 4917 | struct bpf_reg_state *regs = cur_regs(env); |
17a52670 AS |
4918 | u8 opcode = BPF_OP(insn->code); |
4919 | int err; | |
4920 | ||
4921 | if (opcode == BPF_END || opcode == BPF_NEG) { | |
4922 | if (opcode == BPF_NEG) { | |
4923 | if (BPF_SRC(insn->code) != 0 || | |
4924 | insn->src_reg != BPF_REG_0 || | |
4925 | insn->off != 0 || insn->imm != 0) { | |
61bd5218 | 4926 | verbose(env, "BPF_NEG uses reserved fields\n"); |
17a52670 AS |
4927 | return -EINVAL; |
4928 | } | |
4929 | } else { | |
4930 | if (insn->src_reg != BPF_REG_0 || insn->off != 0 || | |
e67b8a68 EC |
4931 | (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) || |
4932 | BPF_CLASS(insn->code) == BPF_ALU64) { | |
61bd5218 | 4933 | verbose(env, "BPF_END uses reserved fields\n"); |
17a52670 AS |
4934 | return -EINVAL; |
4935 | } | |
4936 | } | |
4937 | ||
4938 | /* check src operand */ | |
dc503a8a | 4939 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
4940 | if (err) |
4941 | return err; | |
4942 | ||
1be7f75d | 4943 | if (is_pointer_value(env, insn->dst_reg)) { |
61bd5218 | 4944 | verbose(env, "R%d pointer arithmetic prohibited\n", |
1be7f75d AS |
4945 | insn->dst_reg); |
4946 | return -EACCES; | |
4947 | } | |
4948 | ||
17a52670 | 4949 | /* check dest operand */ |
dc503a8a | 4950 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
17a52670 AS |
4951 | if (err) |
4952 | return err; | |
4953 | ||
4954 | } else if (opcode == BPF_MOV) { | |
4955 | ||
4956 | if (BPF_SRC(insn->code) == BPF_X) { | |
4957 | if (insn->imm != 0 || insn->off != 0) { | |
61bd5218 | 4958 | verbose(env, "BPF_MOV uses reserved fields\n"); |
17a52670 AS |
4959 | return -EINVAL; |
4960 | } | |
4961 | ||
4962 | /* check src operand */ | |
dc503a8a | 4963 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
4964 | if (err) |
4965 | return err; | |
4966 | } else { | |
4967 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { | |
61bd5218 | 4968 | verbose(env, "BPF_MOV uses reserved fields\n"); |
17a52670 AS |
4969 | return -EINVAL; |
4970 | } | |
4971 | } | |
4972 | ||
fbeb1603 AF |
4973 | /* check dest operand, mark as required later */ |
4974 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); | |
17a52670 AS |
4975 | if (err) |
4976 | return err; | |
4977 | ||
4978 | if (BPF_SRC(insn->code) == BPF_X) { | |
e434b8cd JW |
4979 | struct bpf_reg_state *src_reg = regs + insn->src_reg; |
4980 | struct bpf_reg_state *dst_reg = regs + insn->dst_reg; | |
4981 | ||
17a52670 AS |
4982 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
4983 | /* case: R1 = R2 | |
4984 | * copy register state to dest reg | |
4985 | */ | |
e434b8cd JW |
4986 | *dst_reg = *src_reg; |
4987 | dst_reg->live |= REG_LIVE_WRITTEN; | |
5327ed3d | 4988 | dst_reg->subreg_def = DEF_NOT_SUBREG; |
17a52670 | 4989 | } else { |
f1174f77 | 4990 | /* R1 = (u32) R2 */ |
1be7f75d | 4991 | if (is_pointer_value(env, insn->src_reg)) { |
61bd5218 JK |
4992 | verbose(env, |
4993 | "R%d partial copy of pointer\n", | |
1be7f75d AS |
4994 | insn->src_reg); |
4995 | return -EACCES; | |
e434b8cd JW |
4996 | } else if (src_reg->type == SCALAR_VALUE) { |
4997 | *dst_reg = *src_reg; | |
4998 | dst_reg->live |= REG_LIVE_WRITTEN; | |
5327ed3d | 4999 | dst_reg->subreg_def = env->insn_idx + 1; |
e434b8cd JW |
5000 | } else { |
5001 | mark_reg_unknown(env, regs, | |
5002 | insn->dst_reg); | |
1be7f75d | 5003 | } |
e434b8cd | 5004 | coerce_reg_to_size(dst_reg, 4); |
17a52670 AS |
5005 | } |
5006 | } else { | |
5007 | /* case: R = imm | |
5008 | * remember the value we stored into this reg | |
5009 | */ | |
fbeb1603 AF |
5010 | /* clear any state __mark_reg_known doesn't set */ |
5011 | mark_reg_unknown(env, regs, insn->dst_reg); | |
f1174f77 | 5012 | regs[insn->dst_reg].type = SCALAR_VALUE; |
95a762e2 JH |
5013 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
5014 | __mark_reg_known(regs + insn->dst_reg, | |
5015 | insn->imm); | |
5016 | } else { | |
5017 | __mark_reg_known(regs + insn->dst_reg, | |
5018 | (u32)insn->imm); | |
5019 | } | |
17a52670 AS |
5020 | } |
5021 | ||
5022 | } else if (opcode > BPF_END) { | |
61bd5218 | 5023 | verbose(env, "invalid BPF_ALU opcode %x\n", opcode); |
17a52670 AS |
5024 | return -EINVAL; |
5025 | ||
5026 | } else { /* all other ALU ops: and, sub, xor, add, ... */ | |
5027 | ||
17a52670 AS |
5028 | if (BPF_SRC(insn->code) == BPF_X) { |
5029 | if (insn->imm != 0 || insn->off != 0) { | |
61bd5218 | 5030 | verbose(env, "BPF_ALU uses reserved fields\n"); |
17a52670 AS |
5031 | return -EINVAL; |
5032 | } | |
5033 | /* check src1 operand */ | |
dc503a8a | 5034 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
5035 | if (err) |
5036 | return err; | |
5037 | } else { | |
5038 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { | |
61bd5218 | 5039 | verbose(env, "BPF_ALU uses reserved fields\n"); |
17a52670 AS |
5040 | return -EINVAL; |
5041 | } | |
5042 | } | |
5043 | ||
5044 | /* check src2 operand */ | |
dc503a8a | 5045 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
5046 | if (err) |
5047 | return err; | |
5048 | ||
5049 | if ((opcode == BPF_MOD || opcode == BPF_DIV) && | |
5050 | BPF_SRC(insn->code) == BPF_K && insn->imm == 0) { | |
61bd5218 | 5051 | verbose(env, "div by zero\n"); |
17a52670 AS |
5052 | return -EINVAL; |
5053 | } | |
5054 | ||
229394e8 RV |
5055 | if ((opcode == BPF_LSH || opcode == BPF_RSH || |
5056 | opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) { | |
5057 | int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32; | |
5058 | ||
5059 | if (insn->imm < 0 || insn->imm >= size) { | |
61bd5218 | 5060 | verbose(env, "invalid shift %d\n", insn->imm); |
229394e8 RV |
5061 | return -EINVAL; |
5062 | } | |
5063 | } | |
5064 | ||
1a0dc1ac | 5065 | /* check dest operand */ |
dc503a8a | 5066 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
1a0dc1ac AS |
5067 | if (err) |
5068 | return err; | |
5069 | ||
f1174f77 | 5070 | return adjust_reg_min_max_vals(env, insn); |
17a52670 AS |
5071 | } |
5072 | ||
5073 | return 0; | |
5074 | } | |
5075 | ||
c6a9efa1 PC |
5076 | static void __find_good_pkt_pointers(struct bpf_func_state *state, |
5077 | struct bpf_reg_state *dst_reg, | |
5078 | enum bpf_reg_type type, u16 new_range) | |
5079 | { | |
5080 | struct bpf_reg_state *reg; | |
5081 | int i; | |
5082 | ||
5083 | for (i = 0; i < MAX_BPF_REG; i++) { | |
5084 | reg = &state->regs[i]; | |
5085 | if (reg->type == type && reg->id == dst_reg->id) | |
5086 | /* keep the maximum range already checked */ | |
5087 | reg->range = max(reg->range, new_range); | |
5088 | } | |
5089 | ||
5090 | bpf_for_each_spilled_reg(i, state, reg) { | |
5091 | if (!reg) | |
5092 | continue; | |
5093 | if (reg->type == type && reg->id == dst_reg->id) | |
5094 | reg->range = max(reg->range, new_range); | |
5095 | } | |
5096 | } | |
5097 | ||
f4d7e40a | 5098 | static void find_good_pkt_pointers(struct bpf_verifier_state *vstate, |
de8f3a83 | 5099 | struct bpf_reg_state *dst_reg, |
f8ddadc4 | 5100 | enum bpf_reg_type type, |
fb2a311a | 5101 | bool range_right_open) |
969bf05e | 5102 | { |
fb2a311a | 5103 | u16 new_range; |
c6a9efa1 | 5104 | int i; |
2d2be8ca | 5105 | |
fb2a311a DB |
5106 | if (dst_reg->off < 0 || |
5107 | (dst_reg->off == 0 && range_right_open)) | |
f1174f77 EC |
5108 | /* This doesn't give us any range */ |
5109 | return; | |
5110 | ||
b03c9f9f EC |
5111 | if (dst_reg->umax_value > MAX_PACKET_OFF || |
5112 | dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF) | |
f1174f77 EC |
5113 | /* Risk of overflow. For instance, ptr + (1<<63) may be less |
5114 | * than pkt_end, but that's because it's also less than pkt. | |
5115 | */ | |
5116 | return; | |
5117 | ||
fb2a311a DB |
5118 | new_range = dst_reg->off; |
5119 | if (range_right_open) | |
5120 | new_range--; | |
5121 | ||
5122 | /* Examples for register markings: | |
2d2be8ca | 5123 | * |
fb2a311a | 5124 | * pkt_data in dst register: |
2d2be8ca DB |
5125 | * |
5126 | * r2 = r3; | |
5127 | * r2 += 8; | |
5128 | * if (r2 > pkt_end) goto <handle exception> | |
5129 | * <access okay> | |
5130 | * | |
b4e432f1 DB |
5131 | * r2 = r3; |
5132 | * r2 += 8; | |
5133 | * if (r2 < pkt_end) goto <access okay> | |
5134 | * <handle exception> | |
5135 | * | |
2d2be8ca DB |
5136 | * Where: |
5137 | * r2 == dst_reg, pkt_end == src_reg | |
5138 | * r2=pkt(id=n,off=8,r=0) | |
5139 | * r3=pkt(id=n,off=0,r=0) | |
5140 | * | |
fb2a311a | 5141 | * pkt_data in src register: |
2d2be8ca DB |
5142 | * |
5143 | * r2 = r3; | |
5144 | * r2 += 8; | |
5145 | * if (pkt_end >= r2) goto <access okay> | |
5146 | * <handle exception> | |
5147 | * | |
b4e432f1 DB |
5148 | * r2 = r3; |
5149 | * r2 += 8; | |
5150 | * if (pkt_end <= r2) goto <handle exception> | |
5151 | * <access okay> | |
5152 | * | |
2d2be8ca DB |
5153 | * Where: |
5154 | * pkt_end == dst_reg, r2 == src_reg | |
5155 | * r2=pkt(id=n,off=8,r=0) | |
5156 | * r3=pkt(id=n,off=0,r=0) | |
5157 | * | |
5158 | * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8) | |
fb2a311a DB |
5159 | * or r3=pkt(id=n,off=0,r=8-1), so that range of bytes [r3, r3 + 8) |
5160 | * and [r3, r3 + 8-1) respectively is safe to access depending on | |
5161 | * the check. | |
969bf05e | 5162 | */ |
2d2be8ca | 5163 | |
f1174f77 EC |
5164 | /* If our ids match, then we must have the same max_value. And we |
5165 | * don't care about the other reg's fixed offset, since if it's too big | |
5166 | * the range won't allow anything. | |
5167 | * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16. | |
5168 | */ | |
c6a9efa1 PC |
5169 | for (i = 0; i <= vstate->curframe; i++) |
5170 | __find_good_pkt_pointers(vstate->frame[i], dst_reg, type, | |
5171 | new_range); | |
969bf05e AS |
5172 | } |
5173 | ||
4f7b3e82 AS |
5174 | /* compute branch direction of the expression "if (reg opcode val) goto target;" |
5175 | * and return: | |
5176 | * 1 - branch will be taken and "goto target" will be executed | |
5177 | * 0 - branch will not be taken and fall-through to next insn | |
5178 | * -1 - unknown. Example: "if (reg < 5)" is unknown when register value range [0,10] | |
5179 | */ | |
092ed096 JW |
5180 | static int is_branch_taken(struct bpf_reg_state *reg, u64 val, u8 opcode, |
5181 | bool is_jmp32) | |
4f7b3e82 | 5182 | { |
092ed096 | 5183 | struct bpf_reg_state reg_lo; |
a72dafaf JW |
5184 | s64 sval; |
5185 | ||
4f7b3e82 AS |
5186 | if (__is_pointer_value(false, reg)) |
5187 | return -1; | |
5188 | ||
092ed096 JW |
5189 | if (is_jmp32) { |
5190 | reg_lo = *reg; | |
5191 | reg = ®_lo; | |
5192 | /* For JMP32, only low 32 bits are compared, coerce_reg_to_size | |
5193 | * could truncate high bits and update umin/umax according to | |
5194 | * information of low bits. | |
5195 | */ | |
5196 | coerce_reg_to_size(reg, 4); | |
5197 | /* smin/smax need special handling. For example, after coerce, | |
5198 | * if smin_value is 0x00000000ffffffffLL, the value is -1 when | |
5199 | * used as operand to JMP32. It is a negative number from s32's | |
5200 | * point of view, while it is a positive number when seen as | |
5201 | * s64. The smin/smax are kept as s64, therefore, when used with | |
5202 | * JMP32, they need to be transformed into s32, then sign | |
5203 | * extended back to s64. | |
5204 | * | |
5205 | * Also, smin/smax were copied from umin/umax. If umin/umax has | |
5206 | * different sign bit, then min/max relationship doesn't | |
5207 | * maintain after casting into s32, for this case, set smin/smax | |
5208 | * to safest range. | |
5209 | */ | |
5210 | if ((reg->umax_value ^ reg->umin_value) & | |
5211 | (1ULL << 31)) { | |
5212 | reg->smin_value = S32_MIN; | |
5213 | reg->smax_value = S32_MAX; | |
5214 | } | |
5215 | reg->smin_value = (s64)(s32)reg->smin_value; | |
5216 | reg->smax_value = (s64)(s32)reg->smax_value; | |
5217 | ||
5218 | val = (u32)val; | |
5219 | sval = (s64)(s32)val; | |
5220 | } else { | |
5221 | sval = (s64)val; | |
5222 | } | |
a72dafaf | 5223 | |
4f7b3e82 AS |
5224 | switch (opcode) { |
5225 | case BPF_JEQ: | |
5226 | if (tnum_is_const(reg->var_off)) | |
5227 | return !!tnum_equals_const(reg->var_off, val); | |
5228 | break; | |
5229 | case BPF_JNE: | |
5230 | if (tnum_is_const(reg->var_off)) | |
5231 | return !tnum_equals_const(reg->var_off, val); | |
5232 | break; | |
960ea056 JK |
5233 | case BPF_JSET: |
5234 | if ((~reg->var_off.mask & reg->var_off.value) & val) | |
5235 | return 1; | |
5236 | if (!((reg->var_off.mask | reg->var_off.value) & val)) | |
5237 | return 0; | |
5238 | break; | |
4f7b3e82 AS |
5239 | case BPF_JGT: |
5240 | if (reg->umin_value > val) | |
5241 | return 1; | |
5242 | else if (reg->umax_value <= val) | |
5243 | return 0; | |
5244 | break; | |
5245 | case BPF_JSGT: | |
a72dafaf | 5246 | if (reg->smin_value > sval) |
4f7b3e82 | 5247 | return 1; |
a72dafaf | 5248 | else if (reg->smax_value < sval) |
4f7b3e82 AS |
5249 | return 0; |
5250 | break; | |
5251 | case BPF_JLT: | |
5252 | if (reg->umax_value < val) | |
5253 | return 1; | |
5254 | else if (reg->umin_value >= val) | |
5255 | return 0; | |
5256 | break; | |
5257 | case BPF_JSLT: | |
a72dafaf | 5258 | if (reg->smax_value < sval) |
4f7b3e82 | 5259 | return 1; |
a72dafaf | 5260 | else if (reg->smin_value >= sval) |
4f7b3e82 AS |
5261 | return 0; |
5262 | break; | |
5263 | case BPF_JGE: | |
5264 | if (reg->umin_value >= val) | |
5265 | return 1; | |
5266 | else if (reg->umax_value < val) | |
5267 | return 0; | |
5268 | break; | |
5269 | case BPF_JSGE: | |
a72dafaf | 5270 | if (reg->smin_value >= sval) |
4f7b3e82 | 5271 | return 1; |
a72dafaf | 5272 | else if (reg->smax_value < sval) |
4f7b3e82 AS |
5273 | return 0; |
5274 | break; | |
5275 | case BPF_JLE: | |
5276 | if (reg->umax_value <= val) | |
5277 | return 1; | |
5278 | else if (reg->umin_value > val) | |
5279 | return 0; | |
5280 | break; | |
5281 | case BPF_JSLE: | |
a72dafaf | 5282 | if (reg->smax_value <= sval) |
4f7b3e82 | 5283 | return 1; |
a72dafaf | 5284 | else if (reg->smin_value > sval) |
4f7b3e82 AS |
5285 | return 0; |
5286 | break; | |
5287 | } | |
5288 | ||
5289 | return -1; | |
5290 | } | |
5291 | ||
092ed096 JW |
5292 | /* Generate min value of the high 32-bit from TNUM info. */ |
5293 | static u64 gen_hi_min(struct tnum var) | |
5294 | { | |
5295 | return var.value & ~0xffffffffULL; | |
5296 | } | |
5297 | ||
5298 | /* Generate max value of the high 32-bit from TNUM info. */ | |
5299 | static u64 gen_hi_max(struct tnum var) | |
5300 | { | |
5301 | return (var.value | var.mask) & ~0xffffffffULL; | |
5302 | } | |
5303 | ||
5304 | /* Return true if VAL is compared with a s64 sign extended from s32, and they | |
5305 | * are with the same signedness. | |
5306 | */ | |
5307 | static bool cmp_val_with_extended_s64(s64 sval, struct bpf_reg_state *reg) | |
5308 | { | |
5309 | return ((s32)sval >= 0 && | |
5310 | reg->smin_value >= 0 && reg->smax_value <= S32_MAX) || | |
5311 | ((s32)sval < 0 && | |
5312 | reg->smax_value <= 0 && reg->smin_value >= S32_MIN); | |
5313 | } | |
5314 | ||
48461135 JB |
5315 | /* Adjusts the register min/max values in the case that the dst_reg is the |
5316 | * variable register that we are working on, and src_reg is a constant or we're | |
5317 | * simply doing a BPF_K check. | |
f1174f77 | 5318 | * In JEQ/JNE cases we also adjust the var_off values. |
48461135 JB |
5319 | */ |
5320 | static void reg_set_min_max(struct bpf_reg_state *true_reg, | |
5321 | struct bpf_reg_state *false_reg, u64 val, | |
092ed096 | 5322 | u8 opcode, bool is_jmp32) |
48461135 | 5323 | { |
a72dafaf JW |
5324 | s64 sval; |
5325 | ||
f1174f77 EC |
5326 | /* If the dst_reg is a pointer, we can't learn anything about its |
5327 | * variable offset from the compare (unless src_reg were a pointer into | |
5328 | * the same object, but we don't bother with that. | |
5329 | * Since false_reg and true_reg have the same type by construction, we | |
5330 | * only need to check one of them for pointerness. | |
5331 | */ | |
5332 | if (__is_pointer_value(false, false_reg)) | |
5333 | return; | |
4cabc5b1 | 5334 | |
092ed096 JW |
5335 | val = is_jmp32 ? (u32)val : val; |
5336 | sval = is_jmp32 ? (s64)(s32)val : (s64)val; | |
a72dafaf | 5337 | |
48461135 JB |
5338 | switch (opcode) { |
5339 | case BPF_JEQ: | |
48461135 | 5340 | case BPF_JNE: |
a72dafaf JW |
5341 | { |
5342 | struct bpf_reg_state *reg = | |
5343 | opcode == BPF_JEQ ? true_reg : false_reg; | |
5344 | ||
5345 | /* For BPF_JEQ, if this is false we know nothing Jon Snow, but | |
5346 | * if it is true we know the value for sure. Likewise for | |
5347 | * BPF_JNE. | |
48461135 | 5348 | */ |
092ed096 JW |
5349 | if (is_jmp32) { |
5350 | u64 old_v = reg->var_off.value; | |
5351 | u64 hi_mask = ~0xffffffffULL; | |
5352 | ||
5353 | reg->var_off.value = (old_v & hi_mask) | val; | |
5354 | reg->var_off.mask &= hi_mask; | |
5355 | } else { | |
5356 | __mark_reg_known(reg, val); | |
5357 | } | |
48461135 | 5358 | break; |
a72dafaf | 5359 | } |
960ea056 JK |
5360 | case BPF_JSET: |
5361 | false_reg->var_off = tnum_and(false_reg->var_off, | |
5362 | tnum_const(~val)); | |
5363 | if (is_power_of_2(val)) | |
5364 | true_reg->var_off = tnum_or(true_reg->var_off, | |
5365 | tnum_const(val)); | |
5366 | break; | |
48461135 | 5367 | case BPF_JGE: |
a72dafaf JW |
5368 | case BPF_JGT: |
5369 | { | |
5370 | u64 false_umax = opcode == BPF_JGT ? val : val - 1; | |
5371 | u64 true_umin = opcode == BPF_JGT ? val + 1 : val; | |
5372 | ||
092ed096 JW |
5373 | if (is_jmp32) { |
5374 | false_umax += gen_hi_max(false_reg->var_off); | |
5375 | true_umin += gen_hi_min(true_reg->var_off); | |
5376 | } | |
a72dafaf JW |
5377 | false_reg->umax_value = min(false_reg->umax_value, false_umax); |
5378 | true_reg->umin_value = max(true_reg->umin_value, true_umin); | |
b03c9f9f | 5379 | break; |
a72dafaf | 5380 | } |
48461135 | 5381 | case BPF_JSGE: |
a72dafaf JW |
5382 | case BPF_JSGT: |
5383 | { | |
5384 | s64 false_smax = opcode == BPF_JSGT ? sval : sval - 1; | |
5385 | s64 true_smin = opcode == BPF_JSGT ? sval + 1 : sval; | |
5386 | ||
092ed096 JW |
5387 | /* If the full s64 was not sign-extended from s32 then don't |
5388 | * deduct further info. | |
5389 | */ | |
5390 | if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg)) | |
5391 | break; | |
a72dafaf JW |
5392 | false_reg->smax_value = min(false_reg->smax_value, false_smax); |
5393 | true_reg->smin_value = max(true_reg->smin_value, true_smin); | |
48461135 | 5394 | break; |
a72dafaf | 5395 | } |
b4e432f1 | 5396 | case BPF_JLE: |
a72dafaf JW |
5397 | case BPF_JLT: |
5398 | { | |
5399 | u64 false_umin = opcode == BPF_JLT ? val : val + 1; | |
5400 | u64 true_umax = opcode == BPF_JLT ? val - 1 : val; | |
5401 | ||
092ed096 JW |
5402 | if (is_jmp32) { |
5403 | false_umin += gen_hi_min(false_reg->var_off); | |
5404 | true_umax += gen_hi_max(true_reg->var_off); | |
5405 | } | |
a72dafaf JW |
5406 | false_reg->umin_value = max(false_reg->umin_value, false_umin); |
5407 | true_reg->umax_value = min(true_reg->umax_value, true_umax); | |
b4e432f1 | 5408 | break; |
a72dafaf | 5409 | } |
b4e432f1 | 5410 | case BPF_JSLE: |
a72dafaf JW |
5411 | case BPF_JSLT: |
5412 | { | |
5413 | s64 false_smin = opcode == BPF_JSLT ? sval : sval + 1; | |
5414 | s64 true_smax = opcode == BPF_JSLT ? sval - 1 : sval; | |
5415 | ||
092ed096 JW |
5416 | if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg)) |
5417 | break; | |
a72dafaf JW |
5418 | false_reg->smin_value = max(false_reg->smin_value, false_smin); |
5419 | true_reg->smax_value = min(true_reg->smax_value, true_smax); | |
b4e432f1 | 5420 | break; |
a72dafaf | 5421 | } |
48461135 JB |
5422 | default: |
5423 | break; | |
5424 | } | |
5425 | ||
b03c9f9f EC |
5426 | __reg_deduce_bounds(false_reg); |
5427 | __reg_deduce_bounds(true_reg); | |
5428 | /* We might have learned some bits from the bounds. */ | |
5429 | __reg_bound_offset(false_reg); | |
5430 | __reg_bound_offset(true_reg); | |
5431 | /* Intersecting with the old var_off might have improved our bounds | |
5432 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), | |
5433 | * then new var_off is (0; 0x7f...fc) which improves our umax. | |
5434 | */ | |
5435 | __update_reg_bounds(false_reg); | |
5436 | __update_reg_bounds(true_reg); | |
48461135 JB |
5437 | } |
5438 | ||
f1174f77 EC |
5439 | /* Same as above, but for the case that dst_reg holds a constant and src_reg is |
5440 | * the variable reg. | |
48461135 JB |
5441 | */ |
5442 | static void reg_set_min_max_inv(struct bpf_reg_state *true_reg, | |
5443 | struct bpf_reg_state *false_reg, u64 val, | |
092ed096 | 5444 | u8 opcode, bool is_jmp32) |
48461135 | 5445 | { |
a72dafaf JW |
5446 | s64 sval; |
5447 | ||
f1174f77 EC |
5448 | if (__is_pointer_value(false, false_reg)) |
5449 | return; | |
4cabc5b1 | 5450 | |
092ed096 JW |
5451 | val = is_jmp32 ? (u32)val : val; |
5452 | sval = is_jmp32 ? (s64)(s32)val : (s64)val; | |
a72dafaf | 5453 | |
48461135 JB |
5454 | switch (opcode) { |
5455 | case BPF_JEQ: | |
48461135 | 5456 | case BPF_JNE: |
a72dafaf JW |
5457 | { |
5458 | struct bpf_reg_state *reg = | |
5459 | opcode == BPF_JEQ ? true_reg : false_reg; | |
5460 | ||
092ed096 JW |
5461 | if (is_jmp32) { |
5462 | u64 old_v = reg->var_off.value; | |
5463 | u64 hi_mask = ~0xffffffffULL; | |
5464 | ||
5465 | reg->var_off.value = (old_v & hi_mask) | val; | |
5466 | reg->var_off.mask &= hi_mask; | |
5467 | } else { | |
5468 | __mark_reg_known(reg, val); | |
5469 | } | |
48461135 | 5470 | break; |
a72dafaf | 5471 | } |
960ea056 JK |
5472 | case BPF_JSET: |
5473 | false_reg->var_off = tnum_and(false_reg->var_off, | |
5474 | tnum_const(~val)); | |
5475 | if (is_power_of_2(val)) | |
5476 | true_reg->var_off = tnum_or(true_reg->var_off, | |
5477 | tnum_const(val)); | |
5478 | break; | |
48461135 | 5479 | case BPF_JGE: |
a72dafaf JW |
5480 | case BPF_JGT: |
5481 | { | |
5482 | u64 false_umin = opcode == BPF_JGT ? val : val + 1; | |
5483 | u64 true_umax = opcode == BPF_JGT ? val - 1 : val; | |
5484 | ||
092ed096 JW |
5485 | if (is_jmp32) { |
5486 | false_umin += gen_hi_min(false_reg->var_off); | |
5487 | true_umax += gen_hi_max(true_reg->var_off); | |
5488 | } | |
a72dafaf JW |
5489 | false_reg->umin_value = max(false_reg->umin_value, false_umin); |
5490 | true_reg->umax_value = min(true_reg->umax_value, true_umax); | |
b03c9f9f | 5491 | break; |
a72dafaf | 5492 | } |
48461135 | 5493 | case BPF_JSGE: |
a72dafaf JW |
5494 | case BPF_JSGT: |
5495 | { | |
5496 | s64 false_smin = opcode == BPF_JSGT ? sval : sval + 1; | |
5497 | s64 true_smax = opcode == BPF_JSGT ? sval - 1 : sval; | |
5498 | ||
092ed096 JW |
5499 | if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg)) |
5500 | break; | |
a72dafaf JW |
5501 | false_reg->smin_value = max(false_reg->smin_value, false_smin); |
5502 | true_reg->smax_value = min(true_reg->smax_value, true_smax); | |
48461135 | 5503 | break; |
a72dafaf | 5504 | } |
b4e432f1 | 5505 | case BPF_JLE: |
a72dafaf JW |
5506 | case BPF_JLT: |
5507 | { | |
5508 | u64 false_umax = opcode == BPF_JLT ? val : val - 1; | |
5509 | u64 true_umin = opcode == BPF_JLT ? val + 1 : val; | |
5510 | ||
092ed096 JW |
5511 | if (is_jmp32) { |
5512 | false_umax += gen_hi_max(false_reg->var_off); | |
5513 | true_umin += gen_hi_min(true_reg->var_off); | |
5514 | } | |
a72dafaf JW |
5515 | false_reg->umax_value = min(false_reg->umax_value, false_umax); |
5516 | true_reg->umin_value = max(true_reg->umin_value, true_umin); | |
b4e432f1 | 5517 | break; |
a72dafaf | 5518 | } |
b4e432f1 | 5519 | case BPF_JSLE: |
a72dafaf JW |
5520 | case BPF_JSLT: |
5521 | { | |
5522 | s64 false_smax = opcode == BPF_JSLT ? sval : sval - 1; | |
5523 | s64 true_smin = opcode == BPF_JSLT ? sval + 1 : sval; | |
5524 | ||
092ed096 JW |
5525 | if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg)) |
5526 | break; | |
a72dafaf JW |
5527 | false_reg->smax_value = min(false_reg->smax_value, false_smax); |
5528 | true_reg->smin_value = max(true_reg->smin_value, true_smin); | |
b4e432f1 | 5529 | break; |
a72dafaf | 5530 | } |
48461135 JB |
5531 | default: |
5532 | break; | |
5533 | } | |
5534 | ||
b03c9f9f EC |
5535 | __reg_deduce_bounds(false_reg); |
5536 | __reg_deduce_bounds(true_reg); | |
5537 | /* We might have learned some bits from the bounds. */ | |
5538 | __reg_bound_offset(false_reg); | |
5539 | __reg_bound_offset(true_reg); | |
5540 | /* Intersecting with the old var_off might have improved our bounds | |
5541 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), | |
5542 | * then new var_off is (0; 0x7f...fc) which improves our umax. | |
5543 | */ | |
5544 | __update_reg_bounds(false_reg); | |
5545 | __update_reg_bounds(true_reg); | |
f1174f77 EC |
5546 | } |
5547 | ||
5548 | /* Regs are known to be equal, so intersect their min/max/var_off */ | |
5549 | static void __reg_combine_min_max(struct bpf_reg_state *src_reg, | |
5550 | struct bpf_reg_state *dst_reg) | |
5551 | { | |
b03c9f9f EC |
5552 | src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value, |
5553 | dst_reg->umin_value); | |
5554 | src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value, | |
5555 | dst_reg->umax_value); | |
5556 | src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value, | |
5557 | dst_reg->smin_value); | |
5558 | src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value, | |
5559 | dst_reg->smax_value); | |
f1174f77 EC |
5560 | src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off, |
5561 | dst_reg->var_off); | |
b03c9f9f EC |
5562 | /* We might have learned new bounds from the var_off. */ |
5563 | __update_reg_bounds(src_reg); | |
5564 | __update_reg_bounds(dst_reg); | |
5565 | /* We might have learned something about the sign bit. */ | |
5566 | __reg_deduce_bounds(src_reg); | |
5567 | __reg_deduce_bounds(dst_reg); | |
5568 | /* We might have learned some bits from the bounds. */ | |
5569 | __reg_bound_offset(src_reg); | |
5570 | __reg_bound_offset(dst_reg); | |
5571 | /* Intersecting with the old var_off might have improved our bounds | |
5572 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), | |
5573 | * then new var_off is (0; 0x7f...fc) which improves our umax. | |
5574 | */ | |
5575 | __update_reg_bounds(src_reg); | |
5576 | __update_reg_bounds(dst_reg); | |
f1174f77 EC |
5577 | } |
5578 | ||
5579 | static void reg_combine_min_max(struct bpf_reg_state *true_src, | |
5580 | struct bpf_reg_state *true_dst, | |
5581 | struct bpf_reg_state *false_src, | |
5582 | struct bpf_reg_state *false_dst, | |
5583 | u8 opcode) | |
5584 | { | |
5585 | switch (opcode) { | |
5586 | case BPF_JEQ: | |
5587 | __reg_combine_min_max(true_src, true_dst); | |
5588 | break; | |
5589 | case BPF_JNE: | |
5590 | __reg_combine_min_max(false_src, false_dst); | |
b03c9f9f | 5591 | break; |
4cabc5b1 | 5592 | } |
48461135 JB |
5593 | } |
5594 | ||
fd978bf7 JS |
5595 | static void mark_ptr_or_null_reg(struct bpf_func_state *state, |
5596 | struct bpf_reg_state *reg, u32 id, | |
840b9615 | 5597 | bool is_null) |
57a09bf0 | 5598 | { |
840b9615 | 5599 | if (reg_type_may_be_null(reg->type) && reg->id == id) { |
f1174f77 EC |
5600 | /* Old offset (both fixed and variable parts) should |
5601 | * have been known-zero, because we don't allow pointer | |
5602 | * arithmetic on pointers that might be NULL. | |
5603 | */ | |
b03c9f9f EC |
5604 | if (WARN_ON_ONCE(reg->smin_value || reg->smax_value || |
5605 | !tnum_equals_const(reg->var_off, 0) || | |
f1174f77 | 5606 | reg->off)) { |
b03c9f9f EC |
5607 | __mark_reg_known_zero(reg); |
5608 | reg->off = 0; | |
f1174f77 EC |
5609 | } |
5610 | if (is_null) { | |
5611 | reg->type = SCALAR_VALUE; | |
840b9615 JS |
5612 | } else if (reg->type == PTR_TO_MAP_VALUE_OR_NULL) { |
5613 | if (reg->map_ptr->inner_map_meta) { | |
5614 | reg->type = CONST_PTR_TO_MAP; | |
5615 | reg->map_ptr = reg->map_ptr->inner_map_meta; | |
fada7fdc JL |
5616 | } else if (reg->map_ptr->map_type == |
5617 | BPF_MAP_TYPE_XSKMAP) { | |
5618 | reg->type = PTR_TO_XDP_SOCK; | |
840b9615 JS |
5619 | } else { |
5620 | reg->type = PTR_TO_MAP_VALUE; | |
5621 | } | |
c64b7983 JS |
5622 | } else if (reg->type == PTR_TO_SOCKET_OR_NULL) { |
5623 | reg->type = PTR_TO_SOCKET; | |
46f8bc92 MKL |
5624 | } else if (reg->type == PTR_TO_SOCK_COMMON_OR_NULL) { |
5625 | reg->type = PTR_TO_SOCK_COMMON; | |
655a51e5 MKL |
5626 | } else if (reg->type == PTR_TO_TCP_SOCK_OR_NULL) { |
5627 | reg->type = PTR_TO_TCP_SOCK; | |
56f668df | 5628 | } |
1b986589 MKL |
5629 | if (is_null) { |
5630 | /* We don't need id and ref_obj_id from this point | |
5631 | * onwards anymore, thus we should better reset it, | |
5632 | * so that state pruning has chances to take effect. | |
5633 | */ | |
5634 | reg->id = 0; | |
5635 | reg->ref_obj_id = 0; | |
5636 | } else if (!reg_may_point_to_spin_lock(reg)) { | |
5637 | /* For not-NULL ptr, reg->ref_obj_id will be reset | |
5638 | * in release_reg_references(). | |
5639 | * | |
5640 | * reg->id is still used by spin_lock ptr. Other | |
5641 | * than spin_lock ptr type, reg->id can be reset. | |
fd978bf7 JS |
5642 | */ |
5643 | reg->id = 0; | |
56f668df | 5644 | } |
57a09bf0 TG |
5645 | } |
5646 | } | |
5647 | ||
c6a9efa1 PC |
5648 | static void __mark_ptr_or_null_regs(struct bpf_func_state *state, u32 id, |
5649 | bool is_null) | |
5650 | { | |
5651 | struct bpf_reg_state *reg; | |
5652 | int i; | |
5653 | ||
5654 | for (i = 0; i < MAX_BPF_REG; i++) | |
5655 | mark_ptr_or_null_reg(state, &state->regs[i], id, is_null); | |
5656 | ||
5657 | bpf_for_each_spilled_reg(i, state, reg) { | |
5658 | if (!reg) | |
5659 | continue; | |
5660 | mark_ptr_or_null_reg(state, reg, id, is_null); | |
5661 | } | |
5662 | } | |
5663 | ||
57a09bf0 TG |
5664 | /* The logic is similar to find_good_pkt_pointers(), both could eventually |
5665 | * be folded together at some point. | |
5666 | */ | |
840b9615 JS |
5667 | static void mark_ptr_or_null_regs(struct bpf_verifier_state *vstate, u32 regno, |
5668 | bool is_null) | |
57a09bf0 | 5669 | { |
f4d7e40a | 5670 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
c6a9efa1 | 5671 | struct bpf_reg_state *regs = state->regs; |
1b986589 | 5672 | u32 ref_obj_id = regs[regno].ref_obj_id; |
a08dd0da | 5673 | u32 id = regs[regno].id; |
c6a9efa1 | 5674 | int i; |
57a09bf0 | 5675 | |
1b986589 MKL |
5676 | if (ref_obj_id && ref_obj_id == id && is_null) |
5677 | /* regs[regno] is in the " == NULL" branch. | |
5678 | * No one could have freed the reference state before | |
5679 | * doing the NULL check. | |
5680 | */ | |
5681 | WARN_ON_ONCE(release_reference_state(state, id)); | |
fd978bf7 | 5682 | |
c6a9efa1 PC |
5683 | for (i = 0; i <= vstate->curframe; i++) |
5684 | __mark_ptr_or_null_regs(vstate->frame[i], id, is_null); | |
57a09bf0 TG |
5685 | } |
5686 | ||
5beca081 DB |
5687 | static bool try_match_pkt_pointers(const struct bpf_insn *insn, |
5688 | struct bpf_reg_state *dst_reg, | |
5689 | struct bpf_reg_state *src_reg, | |
5690 | struct bpf_verifier_state *this_branch, | |
5691 | struct bpf_verifier_state *other_branch) | |
5692 | { | |
5693 | if (BPF_SRC(insn->code) != BPF_X) | |
5694 | return false; | |
5695 | ||
092ed096 JW |
5696 | /* Pointers are always 64-bit. */ |
5697 | if (BPF_CLASS(insn->code) == BPF_JMP32) | |
5698 | return false; | |
5699 | ||
5beca081 DB |
5700 | switch (BPF_OP(insn->code)) { |
5701 | case BPF_JGT: | |
5702 | if ((dst_reg->type == PTR_TO_PACKET && | |
5703 | src_reg->type == PTR_TO_PACKET_END) || | |
5704 | (dst_reg->type == PTR_TO_PACKET_META && | |
5705 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
5706 | /* pkt_data' > pkt_end, pkt_meta' > pkt_data */ | |
5707 | find_good_pkt_pointers(this_branch, dst_reg, | |
5708 | dst_reg->type, false); | |
5709 | } else if ((dst_reg->type == PTR_TO_PACKET_END && | |
5710 | src_reg->type == PTR_TO_PACKET) || | |
5711 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
5712 | src_reg->type == PTR_TO_PACKET_META)) { | |
5713 | /* pkt_end > pkt_data', pkt_data > pkt_meta' */ | |
5714 | find_good_pkt_pointers(other_branch, src_reg, | |
5715 | src_reg->type, true); | |
5716 | } else { | |
5717 | return false; | |
5718 | } | |
5719 | break; | |
5720 | case BPF_JLT: | |
5721 | if ((dst_reg->type == PTR_TO_PACKET && | |
5722 | src_reg->type == PTR_TO_PACKET_END) || | |
5723 | (dst_reg->type == PTR_TO_PACKET_META && | |
5724 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
5725 | /* pkt_data' < pkt_end, pkt_meta' < pkt_data */ | |
5726 | find_good_pkt_pointers(other_branch, dst_reg, | |
5727 | dst_reg->type, true); | |
5728 | } else if ((dst_reg->type == PTR_TO_PACKET_END && | |
5729 | src_reg->type == PTR_TO_PACKET) || | |
5730 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
5731 | src_reg->type == PTR_TO_PACKET_META)) { | |
5732 | /* pkt_end < pkt_data', pkt_data > pkt_meta' */ | |
5733 | find_good_pkt_pointers(this_branch, src_reg, | |
5734 | src_reg->type, false); | |
5735 | } else { | |
5736 | return false; | |
5737 | } | |
5738 | break; | |
5739 | case BPF_JGE: | |
5740 | if ((dst_reg->type == PTR_TO_PACKET && | |
5741 | src_reg->type == PTR_TO_PACKET_END) || | |
5742 | (dst_reg->type == PTR_TO_PACKET_META && | |
5743 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
5744 | /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */ | |
5745 | find_good_pkt_pointers(this_branch, dst_reg, | |
5746 | dst_reg->type, true); | |
5747 | } else if ((dst_reg->type == PTR_TO_PACKET_END && | |
5748 | src_reg->type == PTR_TO_PACKET) || | |
5749 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
5750 | src_reg->type == PTR_TO_PACKET_META)) { | |
5751 | /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */ | |
5752 | find_good_pkt_pointers(other_branch, src_reg, | |
5753 | src_reg->type, false); | |
5754 | } else { | |
5755 | return false; | |
5756 | } | |
5757 | break; | |
5758 | case BPF_JLE: | |
5759 | if ((dst_reg->type == PTR_TO_PACKET && | |
5760 | src_reg->type == PTR_TO_PACKET_END) || | |
5761 | (dst_reg->type == PTR_TO_PACKET_META && | |
5762 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
5763 | /* pkt_data' <= pkt_end, pkt_meta' <= pkt_data */ | |
5764 | find_good_pkt_pointers(other_branch, dst_reg, | |
5765 | dst_reg->type, false); | |
5766 | } else if ((dst_reg->type == PTR_TO_PACKET_END && | |
5767 | src_reg->type == PTR_TO_PACKET) || | |
5768 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
5769 | src_reg->type == PTR_TO_PACKET_META)) { | |
5770 | /* pkt_end <= pkt_data', pkt_data <= pkt_meta' */ | |
5771 | find_good_pkt_pointers(this_branch, src_reg, | |
5772 | src_reg->type, true); | |
5773 | } else { | |
5774 | return false; | |
5775 | } | |
5776 | break; | |
5777 | default: | |
5778 | return false; | |
5779 | } | |
5780 | ||
5781 | return true; | |
5782 | } | |
5783 | ||
58e2af8b | 5784 | static int check_cond_jmp_op(struct bpf_verifier_env *env, |
17a52670 AS |
5785 | struct bpf_insn *insn, int *insn_idx) |
5786 | { | |
f4d7e40a AS |
5787 | struct bpf_verifier_state *this_branch = env->cur_state; |
5788 | struct bpf_verifier_state *other_branch; | |
5789 | struct bpf_reg_state *regs = this_branch->frame[this_branch->curframe]->regs; | |
fb8d251e | 5790 | struct bpf_reg_state *dst_reg, *other_branch_regs, *src_reg = NULL; |
17a52670 | 5791 | u8 opcode = BPF_OP(insn->code); |
092ed096 | 5792 | bool is_jmp32; |
fb8d251e | 5793 | int pred = -1; |
17a52670 AS |
5794 | int err; |
5795 | ||
092ed096 JW |
5796 | /* Only conditional jumps are expected to reach here. */ |
5797 | if (opcode == BPF_JA || opcode > BPF_JSLE) { | |
5798 | verbose(env, "invalid BPF_JMP/JMP32 opcode %x\n", opcode); | |
17a52670 AS |
5799 | return -EINVAL; |
5800 | } | |
5801 | ||
5802 | if (BPF_SRC(insn->code) == BPF_X) { | |
5803 | if (insn->imm != 0) { | |
092ed096 | 5804 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
17a52670 AS |
5805 | return -EINVAL; |
5806 | } | |
5807 | ||
5808 | /* check src1 operand */ | |
dc503a8a | 5809 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
5810 | if (err) |
5811 | return err; | |
1be7f75d AS |
5812 | |
5813 | if (is_pointer_value(env, insn->src_reg)) { | |
61bd5218 | 5814 | verbose(env, "R%d pointer comparison prohibited\n", |
1be7f75d AS |
5815 | insn->src_reg); |
5816 | return -EACCES; | |
5817 | } | |
fb8d251e | 5818 | src_reg = ®s[insn->src_reg]; |
17a52670 AS |
5819 | } else { |
5820 | if (insn->src_reg != BPF_REG_0) { | |
092ed096 | 5821 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
17a52670 AS |
5822 | return -EINVAL; |
5823 | } | |
5824 | } | |
5825 | ||
5826 | /* check src2 operand */ | |
dc503a8a | 5827 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
5828 | if (err) |
5829 | return err; | |
5830 | ||
1a0dc1ac | 5831 | dst_reg = ®s[insn->dst_reg]; |
092ed096 | 5832 | is_jmp32 = BPF_CLASS(insn->code) == BPF_JMP32; |
1a0dc1ac | 5833 | |
fb8d251e AS |
5834 | if (BPF_SRC(insn->code) == BPF_K) |
5835 | pred = is_branch_taken(dst_reg, insn->imm, | |
5836 | opcode, is_jmp32); | |
5837 | else if (src_reg->type == SCALAR_VALUE && | |
5838 | tnum_is_const(src_reg->var_off)) | |
5839 | pred = is_branch_taken(dst_reg, src_reg->var_off.value, | |
5840 | opcode, is_jmp32); | |
b5dc0163 AS |
5841 | if (pred >= 0) { |
5842 | err = mark_chain_precision(env, insn->dst_reg); | |
5843 | if (BPF_SRC(insn->code) == BPF_X && !err) | |
5844 | err = mark_chain_precision(env, insn->src_reg); | |
5845 | if (err) | |
5846 | return err; | |
5847 | } | |
fb8d251e AS |
5848 | if (pred == 1) { |
5849 | /* only follow the goto, ignore fall-through */ | |
5850 | *insn_idx += insn->off; | |
5851 | return 0; | |
5852 | } else if (pred == 0) { | |
5853 | /* only follow fall-through branch, since | |
5854 | * that's where the program will go | |
5855 | */ | |
5856 | return 0; | |
17a52670 AS |
5857 | } |
5858 | ||
979d63d5 DB |
5859 | other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx, |
5860 | false); | |
17a52670 AS |
5861 | if (!other_branch) |
5862 | return -EFAULT; | |
f4d7e40a | 5863 | other_branch_regs = other_branch->frame[other_branch->curframe]->regs; |
17a52670 | 5864 | |
48461135 JB |
5865 | /* detect if we are comparing against a constant value so we can adjust |
5866 | * our min/max values for our dst register. | |
f1174f77 EC |
5867 | * this is only legit if both are scalars (or pointers to the same |
5868 | * object, I suppose, but we don't support that right now), because | |
5869 | * otherwise the different base pointers mean the offsets aren't | |
5870 | * comparable. | |
48461135 JB |
5871 | */ |
5872 | if (BPF_SRC(insn->code) == BPF_X) { | |
092ed096 JW |
5873 | struct bpf_reg_state *src_reg = ®s[insn->src_reg]; |
5874 | struct bpf_reg_state lo_reg0 = *dst_reg; | |
5875 | struct bpf_reg_state lo_reg1 = *src_reg; | |
5876 | struct bpf_reg_state *src_lo, *dst_lo; | |
5877 | ||
5878 | dst_lo = &lo_reg0; | |
5879 | src_lo = &lo_reg1; | |
5880 | coerce_reg_to_size(dst_lo, 4); | |
5881 | coerce_reg_to_size(src_lo, 4); | |
5882 | ||
f1174f77 | 5883 | if (dst_reg->type == SCALAR_VALUE && |
092ed096 JW |
5884 | src_reg->type == SCALAR_VALUE) { |
5885 | if (tnum_is_const(src_reg->var_off) || | |
5886 | (is_jmp32 && tnum_is_const(src_lo->var_off))) | |
f4d7e40a | 5887 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
092ed096 JW |
5888 | dst_reg, |
5889 | is_jmp32 | |
5890 | ? src_lo->var_off.value | |
5891 | : src_reg->var_off.value, | |
5892 | opcode, is_jmp32); | |
5893 | else if (tnum_is_const(dst_reg->var_off) || | |
5894 | (is_jmp32 && tnum_is_const(dst_lo->var_off))) | |
f4d7e40a | 5895 | reg_set_min_max_inv(&other_branch_regs[insn->src_reg], |
092ed096 JW |
5896 | src_reg, |
5897 | is_jmp32 | |
5898 | ? dst_lo->var_off.value | |
5899 | : dst_reg->var_off.value, | |
5900 | opcode, is_jmp32); | |
5901 | else if (!is_jmp32 && | |
5902 | (opcode == BPF_JEQ || opcode == BPF_JNE)) | |
f1174f77 | 5903 | /* Comparing for equality, we can combine knowledge */ |
f4d7e40a AS |
5904 | reg_combine_min_max(&other_branch_regs[insn->src_reg], |
5905 | &other_branch_regs[insn->dst_reg], | |
092ed096 | 5906 | src_reg, dst_reg, opcode); |
f1174f77 EC |
5907 | } |
5908 | } else if (dst_reg->type == SCALAR_VALUE) { | |
f4d7e40a | 5909 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
092ed096 | 5910 | dst_reg, insn->imm, opcode, is_jmp32); |
48461135 JB |
5911 | } |
5912 | ||
092ed096 JW |
5913 | /* detect if R == 0 where R is returned from bpf_map_lookup_elem(). |
5914 | * NOTE: these optimizations below are related with pointer comparison | |
5915 | * which will never be JMP32. | |
5916 | */ | |
5917 | if (!is_jmp32 && BPF_SRC(insn->code) == BPF_K && | |
1a0dc1ac | 5918 | insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) && |
840b9615 JS |
5919 | reg_type_may_be_null(dst_reg->type)) { |
5920 | /* Mark all identical registers in each branch as either | |
57a09bf0 TG |
5921 | * safe or unknown depending R == 0 or R != 0 conditional. |
5922 | */ | |
840b9615 JS |
5923 | mark_ptr_or_null_regs(this_branch, insn->dst_reg, |
5924 | opcode == BPF_JNE); | |
5925 | mark_ptr_or_null_regs(other_branch, insn->dst_reg, | |
5926 | opcode == BPF_JEQ); | |
5beca081 DB |
5927 | } else if (!try_match_pkt_pointers(insn, dst_reg, ®s[insn->src_reg], |
5928 | this_branch, other_branch) && | |
5929 | is_pointer_value(env, insn->dst_reg)) { | |
61bd5218 JK |
5930 | verbose(env, "R%d pointer comparison prohibited\n", |
5931 | insn->dst_reg); | |
1be7f75d | 5932 | return -EACCES; |
17a52670 | 5933 | } |
06ee7115 | 5934 | if (env->log.level & BPF_LOG_LEVEL) |
f4d7e40a | 5935 | print_verifier_state(env, this_branch->frame[this_branch->curframe]); |
17a52670 AS |
5936 | return 0; |
5937 | } | |
5938 | ||
17a52670 | 5939 | /* verify BPF_LD_IMM64 instruction */ |
58e2af8b | 5940 | static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn) |
17a52670 | 5941 | { |
d8eca5bb | 5942 | struct bpf_insn_aux_data *aux = cur_aux(env); |
638f5b90 | 5943 | struct bpf_reg_state *regs = cur_regs(env); |
d8eca5bb | 5944 | struct bpf_map *map; |
17a52670 AS |
5945 | int err; |
5946 | ||
5947 | if (BPF_SIZE(insn->code) != BPF_DW) { | |
61bd5218 | 5948 | verbose(env, "invalid BPF_LD_IMM insn\n"); |
17a52670 AS |
5949 | return -EINVAL; |
5950 | } | |
5951 | if (insn->off != 0) { | |
61bd5218 | 5952 | verbose(env, "BPF_LD_IMM64 uses reserved fields\n"); |
17a52670 AS |
5953 | return -EINVAL; |
5954 | } | |
5955 | ||
dc503a8a | 5956 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
17a52670 AS |
5957 | if (err) |
5958 | return err; | |
5959 | ||
6b173873 | 5960 | if (insn->src_reg == 0) { |
6b173873 JK |
5961 | u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm; |
5962 | ||
f1174f77 | 5963 | regs[insn->dst_reg].type = SCALAR_VALUE; |
b03c9f9f | 5964 | __mark_reg_known(®s[insn->dst_reg], imm); |
17a52670 | 5965 | return 0; |
6b173873 | 5966 | } |
17a52670 | 5967 | |
d8eca5bb DB |
5968 | map = env->used_maps[aux->map_index]; |
5969 | mark_reg_known_zero(env, regs, insn->dst_reg); | |
5970 | regs[insn->dst_reg].map_ptr = map; | |
5971 | ||
5972 | if (insn->src_reg == BPF_PSEUDO_MAP_VALUE) { | |
5973 | regs[insn->dst_reg].type = PTR_TO_MAP_VALUE; | |
5974 | regs[insn->dst_reg].off = aux->map_off; | |
5975 | if (map_value_has_spin_lock(map)) | |
5976 | regs[insn->dst_reg].id = ++env->id_gen; | |
5977 | } else if (insn->src_reg == BPF_PSEUDO_MAP_FD) { | |
5978 | regs[insn->dst_reg].type = CONST_PTR_TO_MAP; | |
5979 | } else { | |
5980 | verbose(env, "bpf verifier is misconfigured\n"); | |
5981 | return -EINVAL; | |
5982 | } | |
17a52670 | 5983 | |
17a52670 AS |
5984 | return 0; |
5985 | } | |
5986 | ||
96be4325 DB |
5987 | static bool may_access_skb(enum bpf_prog_type type) |
5988 | { | |
5989 | switch (type) { | |
5990 | case BPF_PROG_TYPE_SOCKET_FILTER: | |
5991 | case BPF_PROG_TYPE_SCHED_CLS: | |
94caee8c | 5992 | case BPF_PROG_TYPE_SCHED_ACT: |
96be4325 DB |
5993 | return true; |
5994 | default: | |
5995 | return false; | |
5996 | } | |
5997 | } | |
5998 | ||
ddd872bc AS |
5999 | /* verify safety of LD_ABS|LD_IND instructions: |
6000 | * - they can only appear in the programs where ctx == skb | |
6001 | * - since they are wrappers of function calls, they scratch R1-R5 registers, | |
6002 | * preserve R6-R9, and store return value into R0 | |
6003 | * | |
6004 | * Implicit input: | |
6005 | * ctx == skb == R6 == CTX | |
6006 | * | |
6007 | * Explicit input: | |
6008 | * SRC == any register | |
6009 | * IMM == 32-bit immediate | |
6010 | * | |
6011 | * Output: | |
6012 | * R0 - 8/16/32-bit skb data converted to cpu endianness | |
6013 | */ | |
58e2af8b | 6014 | static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn) |
ddd872bc | 6015 | { |
638f5b90 | 6016 | struct bpf_reg_state *regs = cur_regs(env); |
ddd872bc | 6017 | u8 mode = BPF_MODE(insn->code); |
ddd872bc AS |
6018 | int i, err; |
6019 | ||
24701ece | 6020 | if (!may_access_skb(env->prog->type)) { |
61bd5218 | 6021 | verbose(env, "BPF_LD_[ABS|IND] instructions not allowed for this program type\n"); |
ddd872bc AS |
6022 | return -EINVAL; |
6023 | } | |
6024 | ||
e0cea7ce DB |
6025 | if (!env->ops->gen_ld_abs) { |
6026 | verbose(env, "bpf verifier is misconfigured\n"); | |
6027 | return -EINVAL; | |
6028 | } | |
6029 | ||
f910cefa | 6030 | if (env->subprog_cnt > 1) { |
f4d7e40a AS |
6031 | /* when program has LD_ABS insn JITs and interpreter assume |
6032 | * that r1 == ctx == skb which is not the case for callees | |
6033 | * that can have arbitrary arguments. It's problematic | |
6034 | * for main prog as well since JITs would need to analyze | |
6035 | * all functions in order to make proper register save/restore | |
6036 | * decisions in the main prog. Hence disallow LD_ABS with calls | |
6037 | */ | |
6038 | verbose(env, "BPF_LD_[ABS|IND] instructions cannot be mixed with bpf-to-bpf calls\n"); | |
6039 | return -EINVAL; | |
6040 | } | |
6041 | ||
ddd872bc | 6042 | if (insn->dst_reg != BPF_REG_0 || insn->off != 0 || |
d82bccc6 | 6043 | BPF_SIZE(insn->code) == BPF_DW || |
ddd872bc | 6044 | (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) { |
61bd5218 | 6045 | verbose(env, "BPF_LD_[ABS|IND] uses reserved fields\n"); |
ddd872bc AS |
6046 | return -EINVAL; |
6047 | } | |
6048 | ||
6049 | /* check whether implicit source operand (register R6) is readable */ | |
dc503a8a | 6050 | err = check_reg_arg(env, BPF_REG_6, SRC_OP); |
ddd872bc AS |
6051 | if (err) |
6052 | return err; | |
6053 | ||
fd978bf7 JS |
6054 | /* Disallow usage of BPF_LD_[ABS|IND] with reference tracking, as |
6055 | * gen_ld_abs() may terminate the program at runtime, leading to | |
6056 | * reference leak. | |
6057 | */ | |
6058 | err = check_reference_leak(env); | |
6059 | if (err) { | |
6060 | verbose(env, "BPF_LD_[ABS|IND] cannot be mixed with socket references\n"); | |
6061 | return err; | |
6062 | } | |
6063 | ||
d83525ca AS |
6064 | if (env->cur_state->active_spin_lock) { |
6065 | verbose(env, "BPF_LD_[ABS|IND] cannot be used inside bpf_spin_lock-ed region\n"); | |
6066 | return -EINVAL; | |
6067 | } | |
6068 | ||
ddd872bc | 6069 | if (regs[BPF_REG_6].type != PTR_TO_CTX) { |
61bd5218 JK |
6070 | verbose(env, |
6071 | "at the time of BPF_LD_ABS|IND R6 != pointer to skb\n"); | |
ddd872bc AS |
6072 | return -EINVAL; |
6073 | } | |
6074 | ||
6075 | if (mode == BPF_IND) { | |
6076 | /* check explicit source operand */ | |
dc503a8a | 6077 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
ddd872bc AS |
6078 | if (err) |
6079 | return err; | |
6080 | } | |
6081 | ||
6082 | /* reset caller saved regs to unreadable */ | |
dc503a8a | 6083 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
61bd5218 | 6084 | mark_reg_not_init(env, regs, caller_saved[i]); |
dc503a8a EC |
6085 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
6086 | } | |
ddd872bc AS |
6087 | |
6088 | /* mark destination R0 register as readable, since it contains | |
dc503a8a EC |
6089 | * the value fetched from the packet. |
6090 | * Already marked as written above. | |
ddd872bc | 6091 | */ |
61bd5218 | 6092 | mark_reg_unknown(env, regs, BPF_REG_0); |
5327ed3d JW |
6093 | /* ld_abs load up to 32-bit skb data. */ |
6094 | regs[BPF_REG_0].subreg_def = env->insn_idx + 1; | |
ddd872bc AS |
6095 | return 0; |
6096 | } | |
6097 | ||
390ee7e2 AS |
6098 | static int check_return_code(struct bpf_verifier_env *env) |
6099 | { | |
5cf1e914 | 6100 | struct tnum enforce_attach_type_range = tnum_unknown; |
390ee7e2 AS |
6101 | struct bpf_reg_state *reg; |
6102 | struct tnum range = tnum_range(0, 1); | |
6103 | ||
6104 | switch (env->prog->type) { | |
983695fa DB |
6105 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
6106 | if (env->prog->expected_attach_type == BPF_CGROUP_UDP4_RECVMSG || | |
6107 | env->prog->expected_attach_type == BPF_CGROUP_UDP6_RECVMSG) | |
6108 | range = tnum_range(1, 1); | |
ed4ed404 | 6109 | break; |
390ee7e2 | 6110 | case BPF_PROG_TYPE_CGROUP_SKB: |
5cf1e914 | 6111 | if (env->prog->expected_attach_type == BPF_CGROUP_INET_EGRESS) { |
6112 | range = tnum_range(0, 3); | |
6113 | enforce_attach_type_range = tnum_range(2, 3); | |
6114 | } | |
ed4ed404 | 6115 | break; |
390ee7e2 AS |
6116 | case BPF_PROG_TYPE_CGROUP_SOCK: |
6117 | case BPF_PROG_TYPE_SOCK_OPS: | |
ebc614f6 | 6118 | case BPF_PROG_TYPE_CGROUP_DEVICE: |
7b146ceb | 6119 | case BPF_PROG_TYPE_CGROUP_SYSCTL: |
0d01da6a | 6120 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: |
390ee7e2 AS |
6121 | break; |
6122 | default: | |
6123 | return 0; | |
6124 | } | |
6125 | ||
638f5b90 | 6126 | reg = cur_regs(env) + BPF_REG_0; |
390ee7e2 | 6127 | if (reg->type != SCALAR_VALUE) { |
61bd5218 | 6128 | verbose(env, "At program exit the register R0 is not a known value (%s)\n", |
390ee7e2 AS |
6129 | reg_type_str[reg->type]); |
6130 | return -EINVAL; | |
6131 | } | |
6132 | ||
6133 | if (!tnum_in(range, reg->var_off)) { | |
5cf1e914 | 6134 | char tn_buf[48]; |
6135 | ||
61bd5218 | 6136 | verbose(env, "At program exit the register R0 "); |
390ee7e2 | 6137 | if (!tnum_is_unknown(reg->var_off)) { |
390ee7e2 | 6138 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
61bd5218 | 6139 | verbose(env, "has value %s", tn_buf); |
390ee7e2 | 6140 | } else { |
61bd5218 | 6141 | verbose(env, "has unknown scalar value"); |
390ee7e2 | 6142 | } |
5cf1e914 | 6143 | tnum_strn(tn_buf, sizeof(tn_buf), range); |
983695fa | 6144 | verbose(env, " should have been in %s\n", tn_buf); |
390ee7e2 AS |
6145 | return -EINVAL; |
6146 | } | |
5cf1e914 | 6147 | |
6148 | if (!tnum_is_unknown(enforce_attach_type_range) && | |
6149 | tnum_in(enforce_attach_type_range, reg->var_off)) | |
6150 | env->prog->enforce_expected_attach_type = 1; | |
390ee7e2 AS |
6151 | return 0; |
6152 | } | |
6153 | ||
475fb78f AS |
6154 | /* non-recursive DFS pseudo code |
6155 | * 1 procedure DFS-iterative(G,v): | |
6156 | * 2 label v as discovered | |
6157 | * 3 let S be a stack | |
6158 | * 4 S.push(v) | |
6159 | * 5 while S is not empty | |
6160 | * 6 t <- S.pop() | |
6161 | * 7 if t is what we're looking for: | |
6162 | * 8 return t | |
6163 | * 9 for all edges e in G.adjacentEdges(t) do | |
6164 | * 10 if edge e is already labelled | |
6165 | * 11 continue with the next edge | |
6166 | * 12 w <- G.adjacentVertex(t,e) | |
6167 | * 13 if vertex w is not discovered and not explored | |
6168 | * 14 label e as tree-edge | |
6169 | * 15 label w as discovered | |
6170 | * 16 S.push(w) | |
6171 | * 17 continue at 5 | |
6172 | * 18 else if vertex w is discovered | |
6173 | * 19 label e as back-edge | |
6174 | * 20 else | |
6175 | * 21 // vertex w is explored | |
6176 | * 22 label e as forward- or cross-edge | |
6177 | * 23 label t as explored | |
6178 | * 24 S.pop() | |
6179 | * | |
6180 | * convention: | |
6181 | * 0x10 - discovered | |
6182 | * 0x11 - discovered and fall-through edge labelled | |
6183 | * 0x12 - discovered and fall-through and branch edges labelled | |
6184 | * 0x20 - explored | |
6185 | */ | |
6186 | ||
6187 | enum { | |
6188 | DISCOVERED = 0x10, | |
6189 | EXPLORED = 0x20, | |
6190 | FALLTHROUGH = 1, | |
6191 | BRANCH = 2, | |
6192 | }; | |
6193 | ||
dc2a4ebc AS |
6194 | static u32 state_htab_size(struct bpf_verifier_env *env) |
6195 | { | |
6196 | return env->prog->len; | |
6197 | } | |
6198 | ||
5d839021 AS |
6199 | static struct bpf_verifier_state_list **explored_state( |
6200 | struct bpf_verifier_env *env, | |
6201 | int idx) | |
6202 | { | |
dc2a4ebc AS |
6203 | struct bpf_verifier_state *cur = env->cur_state; |
6204 | struct bpf_func_state *state = cur->frame[cur->curframe]; | |
6205 | ||
6206 | return &env->explored_states[(idx ^ state->callsite) % state_htab_size(env)]; | |
5d839021 AS |
6207 | } |
6208 | ||
6209 | static void init_explored_state(struct bpf_verifier_env *env, int idx) | |
6210 | { | |
a8f500af | 6211 | env->insn_aux_data[idx].prune_point = true; |
5d839021 | 6212 | } |
f1bca824 | 6213 | |
475fb78f AS |
6214 | /* t, w, e - match pseudo-code above: |
6215 | * t - index of current instruction | |
6216 | * w - next instruction | |
6217 | * e - edge | |
6218 | */ | |
2589726d AS |
6219 | static int push_insn(int t, int w, int e, struct bpf_verifier_env *env, |
6220 | bool loop_ok) | |
475fb78f | 6221 | { |
7df737e9 AS |
6222 | int *insn_stack = env->cfg.insn_stack; |
6223 | int *insn_state = env->cfg.insn_state; | |
6224 | ||
475fb78f AS |
6225 | if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH)) |
6226 | return 0; | |
6227 | ||
6228 | if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH)) | |
6229 | return 0; | |
6230 | ||
6231 | if (w < 0 || w >= env->prog->len) { | |
d9762e84 | 6232 | verbose_linfo(env, t, "%d: ", t); |
61bd5218 | 6233 | verbose(env, "jump out of range from insn %d to %d\n", t, w); |
475fb78f AS |
6234 | return -EINVAL; |
6235 | } | |
6236 | ||
f1bca824 AS |
6237 | if (e == BRANCH) |
6238 | /* mark branch target for state pruning */ | |
5d839021 | 6239 | init_explored_state(env, w); |
f1bca824 | 6240 | |
475fb78f AS |
6241 | if (insn_state[w] == 0) { |
6242 | /* tree-edge */ | |
6243 | insn_state[t] = DISCOVERED | e; | |
6244 | insn_state[w] = DISCOVERED; | |
7df737e9 | 6245 | if (env->cfg.cur_stack >= env->prog->len) |
475fb78f | 6246 | return -E2BIG; |
7df737e9 | 6247 | insn_stack[env->cfg.cur_stack++] = w; |
475fb78f AS |
6248 | return 1; |
6249 | } else if ((insn_state[w] & 0xF0) == DISCOVERED) { | |
2589726d AS |
6250 | if (loop_ok && env->allow_ptr_leaks) |
6251 | return 0; | |
d9762e84 MKL |
6252 | verbose_linfo(env, t, "%d: ", t); |
6253 | verbose_linfo(env, w, "%d: ", w); | |
61bd5218 | 6254 | verbose(env, "back-edge from insn %d to %d\n", t, w); |
475fb78f AS |
6255 | return -EINVAL; |
6256 | } else if (insn_state[w] == EXPLORED) { | |
6257 | /* forward- or cross-edge */ | |
6258 | insn_state[t] = DISCOVERED | e; | |
6259 | } else { | |
61bd5218 | 6260 | verbose(env, "insn state internal bug\n"); |
475fb78f AS |
6261 | return -EFAULT; |
6262 | } | |
6263 | return 0; | |
6264 | } | |
6265 | ||
6266 | /* non-recursive depth-first-search to detect loops in BPF program | |
6267 | * loop == back-edge in directed graph | |
6268 | */ | |
58e2af8b | 6269 | static int check_cfg(struct bpf_verifier_env *env) |
475fb78f AS |
6270 | { |
6271 | struct bpf_insn *insns = env->prog->insnsi; | |
6272 | int insn_cnt = env->prog->len; | |
7df737e9 | 6273 | int *insn_stack, *insn_state; |
475fb78f AS |
6274 | int ret = 0; |
6275 | int i, t; | |
6276 | ||
7df737e9 | 6277 | insn_state = env->cfg.insn_state = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
475fb78f AS |
6278 | if (!insn_state) |
6279 | return -ENOMEM; | |
6280 | ||
7df737e9 | 6281 | insn_stack = env->cfg.insn_stack = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
475fb78f | 6282 | if (!insn_stack) { |
71dde681 | 6283 | kvfree(insn_state); |
475fb78f AS |
6284 | return -ENOMEM; |
6285 | } | |
6286 | ||
6287 | insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */ | |
6288 | insn_stack[0] = 0; /* 0 is the first instruction */ | |
7df737e9 | 6289 | env->cfg.cur_stack = 1; |
475fb78f AS |
6290 | |
6291 | peek_stack: | |
7df737e9 | 6292 | if (env->cfg.cur_stack == 0) |
475fb78f | 6293 | goto check_state; |
7df737e9 | 6294 | t = insn_stack[env->cfg.cur_stack - 1]; |
475fb78f | 6295 | |
092ed096 JW |
6296 | if (BPF_CLASS(insns[t].code) == BPF_JMP || |
6297 | BPF_CLASS(insns[t].code) == BPF_JMP32) { | |
475fb78f AS |
6298 | u8 opcode = BPF_OP(insns[t].code); |
6299 | ||
6300 | if (opcode == BPF_EXIT) { | |
6301 | goto mark_explored; | |
6302 | } else if (opcode == BPF_CALL) { | |
2589726d | 6303 | ret = push_insn(t, t + 1, FALLTHROUGH, env, false); |
475fb78f AS |
6304 | if (ret == 1) |
6305 | goto peek_stack; | |
6306 | else if (ret < 0) | |
6307 | goto err_free; | |
07016151 | 6308 | if (t + 1 < insn_cnt) |
5d839021 | 6309 | init_explored_state(env, t + 1); |
cc8b0b92 | 6310 | if (insns[t].src_reg == BPF_PSEUDO_CALL) { |
5d839021 | 6311 | init_explored_state(env, t); |
2589726d AS |
6312 | ret = push_insn(t, t + insns[t].imm + 1, BRANCH, |
6313 | env, false); | |
cc8b0b92 AS |
6314 | if (ret == 1) |
6315 | goto peek_stack; | |
6316 | else if (ret < 0) | |
6317 | goto err_free; | |
6318 | } | |
475fb78f AS |
6319 | } else if (opcode == BPF_JA) { |
6320 | if (BPF_SRC(insns[t].code) != BPF_K) { | |
6321 | ret = -EINVAL; | |
6322 | goto err_free; | |
6323 | } | |
6324 | /* unconditional jump with single edge */ | |
6325 | ret = push_insn(t, t + insns[t].off + 1, | |
2589726d | 6326 | FALLTHROUGH, env, true); |
475fb78f AS |
6327 | if (ret == 1) |
6328 | goto peek_stack; | |
6329 | else if (ret < 0) | |
6330 | goto err_free; | |
b5dc0163 AS |
6331 | /* unconditional jmp is not a good pruning point, |
6332 | * but it's marked, since backtracking needs | |
6333 | * to record jmp history in is_state_visited(). | |
6334 | */ | |
6335 | init_explored_state(env, t + insns[t].off + 1); | |
f1bca824 AS |
6336 | /* tell verifier to check for equivalent states |
6337 | * after every call and jump | |
6338 | */ | |
c3de6317 | 6339 | if (t + 1 < insn_cnt) |
5d839021 | 6340 | init_explored_state(env, t + 1); |
475fb78f AS |
6341 | } else { |
6342 | /* conditional jump with two edges */ | |
5d839021 | 6343 | init_explored_state(env, t); |
2589726d | 6344 | ret = push_insn(t, t + 1, FALLTHROUGH, env, true); |
475fb78f AS |
6345 | if (ret == 1) |
6346 | goto peek_stack; | |
6347 | else if (ret < 0) | |
6348 | goto err_free; | |
6349 | ||
2589726d | 6350 | ret = push_insn(t, t + insns[t].off + 1, BRANCH, env, true); |
475fb78f AS |
6351 | if (ret == 1) |
6352 | goto peek_stack; | |
6353 | else if (ret < 0) | |
6354 | goto err_free; | |
6355 | } | |
6356 | } else { | |
6357 | /* all other non-branch instructions with single | |
6358 | * fall-through edge | |
6359 | */ | |
2589726d | 6360 | ret = push_insn(t, t + 1, FALLTHROUGH, env, false); |
475fb78f AS |
6361 | if (ret == 1) |
6362 | goto peek_stack; | |
6363 | else if (ret < 0) | |
6364 | goto err_free; | |
6365 | } | |
6366 | ||
6367 | mark_explored: | |
6368 | insn_state[t] = EXPLORED; | |
7df737e9 | 6369 | if (env->cfg.cur_stack-- <= 0) { |
61bd5218 | 6370 | verbose(env, "pop stack internal bug\n"); |
475fb78f AS |
6371 | ret = -EFAULT; |
6372 | goto err_free; | |
6373 | } | |
6374 | goto peek_stack; | |
6375 | ||
6376 | check_state: | |
6377 | for (i = 0; i < insn_cnt; i++) { | |
6378 | if (insn_state[i] != EXPLORED) { | |
61bd5218 | 6379 | verbose(env, "unreachable insn %d\n", i); |
475fb78f AS |
6380 | ret = -EINVAL; |
6381 | goto err_free; | |
6382 | } | |
6383 | } | |
6384 | ret = 0; /* cfg looks good */ | |
6385 | ||
6386 | err_free: | |
71dde681 AS |
6387 | kvfree(insn_state); |
6388 | kvfree(insn_stack); | |
7df737e9 | 6389 | env->cfg.insn_state = env->cfg.insn_stack = NULL; |
475fb78f AS |
6390 | return ret; |
6391 | } | |
6392 | ||
838e9690 YS |
6393 | /* The minimum supported BTF func info size */ |
6394 | #define MIN_BPF_FUNCINFO_SIZE 8 | |
6395 | #define MAX_FUNCINFO_REC_SIZE 252 | |
6396 | ||
c454a46b MKL |
6397 | static int check_btf_func(struct bpf_verifier_env *env, |
6398 | const union bpf_attr *attr, | |
6399 | union bpf_attr __user *uattr) | |
838e9690 | 6400 | { |
d0b2818e | 6401 | u32 i, nfuncs, urec_size, min_size; |
838e9690 | 6402 | u32 krec_size = sizeof(struct bpf_func_info); |
c454a46b | 6403 | struct bpf_func_info *krecord; |
838e9690 | 6404 | const struct btf_type *type; |
c454a46b MKL |
6405 | struct bpf_prog *prog; |
6406 | const struct btf *btf; | |
838e9690 | 6407 | void __user *urecord; |
d0b2818e | 6408 | u32 prev_offset = 0; |
838e9690 YS |
6409 | int ret = 0; |
6410 | ||
6411 | nfuncs = attr->func_info_cnt; | |
6412 | if (!nfuncs) | |
6413 | return 0; | |
6414 | ||
6415 | if (nfuncs != env->subprog_cnt) { | |
6416 | verbose(env, "number of funcs in func_info doesn't match number of subprogs\n"); | |
6417 | return -EINVAL; | |
6418 | } | |
6419 | ||
6420 | urec_size = attr->func_info_rec_size; | |
6421 | if (urec_size < MIN_BPF_FUNCINFO_SIZE || | |
6422 | urec_size > MAX_FUNCINFO_REC_SIZE || | |
6423 | urec_size % sizeof(u32)) { | |
6424 | verbose(env, "invalid func info rec size %u\n", urec_size); | |
6425 | return -EINVAL; | |
6426 | } | |
6427 | ||
c454a46b MKL |
6428 | prog = env->prog; |
6429 | btf = prog->aux->btf; | |
838e9690 YS |
6430 | |
6431 | urecord = u64_to_user_ptr(attr->func_info); | |
6432 | min_size = min_t(u32, krec_size, urec_size); | |
6433 | ||
ba64e7d8 | 6434 | krecord = kvcalloc(nfuncs, krec_size, GFP_KERNEL | __GFP_NOWARN); |
c454a46b MKL |
6435 | if (!krecord) |
6436 | return -ENOMEM; | |
ba64e7d8 | 6437 | |
838e9690 YS |
6438 | for (i = 0; i < nfuncs; i++) { |
6439 | ret = bpf_check_uarg_tail_zero(urecord, krec_size, urec_size); | |
6440 | if (ret) { | |
6441 | if (ret == -E2BIG) { | |
6442 | verbose(env, "nonzero tailing record in func info"); | |
6443 | /* set the size kernel expects so loader can zero | |
6444 | * out the rest of the record. | |
6445 | */ | |
6446 | if (put_user(min_size, &uattr->func_info_rec_size)) | |
6447 | ret = -EFAULT; | |
6448 | } | |
c454a46b | 6449 | goto err_free; |
838e9690 YS |
6450 | } |
6451 | ||
ba64e7d8 | 6452 | if (copy_from_user(&krecord[i], urecord, min_size)) { |
838e9690 | 6453 | ret = -EFAULT; |
c454a46b | 6454 | goto err_free; |
838e9690 YS |
6455 | } |
6456 | ||
d30d42e0 | 6457 | /* check insn_off */ |
838e9690 | 6458 | if (i == 0) { |
d30d42e0 | 6459 | if (krecord[i].insn_off) { |
838e9690 | 6460 | verbose(env, |
d30d42e0 MKL |
6461 | "nonzero insn_off %u for the first func info record", |
6462 | krecord[i].insn_off); | |
838e9690 | 6463 | ret = -EINVAL; |
c454a46b | 6464 | goto err_free; |
838e9690 | 6465 | } |
d30d42e0 | 6466 | } else if (krecord[i].insn_off <= prev_offset) { |
838e9690 YS |
6467 | verbose(env, |
6468 | "same or smaller insn offset (%u) than previous func info record (%u)", | |
d30d42e0 | 6469 | krecord[i].insn_off, prev_offset); |
838e9690 | 6470 | ret = -EINVAL; |
c454a46b | 6471 | goto err_free; |
838e9690 YS |
6472 | } |
6473 | ||
d30d42e0 | 6474 | if (env->subprog_info[i].start != krecord[i].insn_off) { |
838e9690 YS |
6475 | verbose(env, "func_info BTF section doesn't match subprog layout in BPF program\n"); |
6476 | ret = -EINVAL; | |
c454a46b | 6477 | goto err_free; |
838e9690 YS |
6478 | } |
6479 | ||
6480 | /* check type_id */ | |
ba64e7d8 | 6481 | type = btf_type_by_id(btf, krecord[i].type_id); |
838e9690 YS |
6482 | if (!type || BTF_INFO_KIND(type->info) != BTF_KIND_FUNC) { |
6483 | verbose(env, "invalid type id %d in func info", | |
ba64e7d8 | 6484 | krecord[i].type_id); |
838e9690 | 6485 | ret = -EINVAL; |
c454a46b | 6486 | goto err_free; |
838e9690 YS |
6487 | } |
6488 | ||
d30d42e0 | 6489 | prev_offset = krecord[i].insn_off; |
838e9690 YS |
6490 | urecord += urec_size; |
6491 | } | |
6492 | ||
ba64e7d8 YS |
6493 | prog->aux->func_info = krecord; |
6494 | prog->aux->func_info_cnt = nfuncs; | |
838e9690 YS |
6495 | return 0; |
6496 | ||
c454a46b | 6497 | err_free: |
ba64e7d8 | 6498 | kvfree(krecord); |
838e9690 YS |
6499 | return ret; |
6500 | } | |
6501 | ||
ba64e7d8 YS |
6502 | static void adjust_btf_func(struct bpf_verifier_env *env) |
6503 | { | |
6504 | int i; | |
6505 | ||
6506 | if (!env->prog->aux->func_info) | |
6507 | return; | |
6508 | ||
6509 | for (i = 0; i < env->subprog_cnt; i++) | |
d30d42e0 | 6510 | env->prog->aux->func_info[i].insn_off = env->subprog_info[i].start; |
ba64e7d8 YS |
6511 | } |
6512 | ||
c454a46b MKL |
6513 | #define MIN_BPF_LINEINFO_SIZE (offsetof(struct bpf_line_info, line_col) + \ |
6514 | sizeof(((struct bpf_line_info *)(0))->line_col)) | |
6515 | #define MAX_LINEINFO_REC_SIZE MAX_FUNCINFO_REC_SIZE | |
6516 | ||
6517 | static int check_btf_line(struct bpf_verifier_env *env, | |
6518 | const union bpf_attr *attr, | |
6519 | union bpf_attr __user *uattr) | |
6520 | { | |
6521 | u32 i, s, nr_linfo, ncopy, expected_size, rec_size, prev_offset = 0; | |
6522 | struct bpf_subprog_info *sub; | |
6523 | struct bpf_line_info *linfo; | |
6524 | struct bpf_prog *prog; | |
6525 | const struct btf *btf; | |
6526 | void __user *ulinfo; | |
6527 | int err; | |
6528 | ||
6529 | nr_linfo = attr->line_info_cnt; | |
6530 | if (!nr_linfo) | |
6531 | return 0; | |
6532 | ||
6533 | rec_size = attr->line_info_rec_size; | |
6534 | if (rec_size < MIN_BPF_LINEINFO_SIZE || | |
6535 | rec_size > MAX_LINEINFO_REC_SIZE || | |
6536 | rec_size & (sizeof(u32) - 1)) | |
6537 | return -EINVAL; | |
6538 | ||
6539 | /* Need to zero it in case the userspace may | |
6540 | * pass in a smaller bpf_line_info object. | |
6541 | */ | |
6542 | linfo = kvcalloc(nr_linfo, sizeof(struct bpf_line_info), | |
6543 | GFP_KERNEL | __GFP_NOWARN); | |
6544 | if (!linfo) | |
6545 | return -ENOMEM; | |
6546 | ||
6547 | prog = env->prog; | |
6548 | btf = prog->aux->btf; | |
6549 | ||
6550 | s = 0; | |
6551 | sub = env->subprog_info; | |
6552 | ulinfo = u64_to_user_ptr(attr->line_info); | |
6553 | expected_size = sizeof(struct bpf_line_info); | |
6554 | ncopy = min_t(u32, expected_size, rec_size); | |
6555 | for (i = 0; i < nr_linfo; i++) { | |
6556 | err = bpf_check_uarg_tail_zero(ulinfo, expected_size, rec_size); | |
6557 | if (err) { | |
6558 | if (err == -E2BIG) { | |
6559 | verbose(env, "nonzero tailing record in line_info"); | |
6560 | if (put_user(expected_size, | |
6561 | &uattr->line_info_rec_size)) | |
6562 | err = -EFAULT; | |
6563 | } | |
6564 | goto err_free; | |
6565 | } | |
6566 | ||
6567 | if (copy_from_user(&linfo[i], ulinfo, ncopy)) { | |
6568 | err = -EFAULT; | |
6569 | goto err_free; | |
6570 | } | |
6571 | ||
6572 | /* | |
6573 | * Check insn_off to ensure | |
6574 | * 1) strictly increasing AND | |
6575 | * 2) bounded by prog->len | |
6576 | * | |
6577 | * The linfo[0].insn_off == 0 check logically falls into | |
6578 | * the later "missing bpf_line_info for func..." case | |
6579 | * because the first linfo[0].insn_off must be the | |
6580 | * first sub also and the first sub must have | |
6581 | * subprog_info[0].start == 0. | |
6582 | */ | |
6583 | if ((i && linfo[i].insn_off <= prev_offset) || | |
6584 | linfo[i].insn_off >= prog->len) { | |
6585 | verbose(env, "Invalid line_info[%u].insn_off:%u (prev_offset:%u prog->len:%u)\n", | |
6586 | i, linfo[i].insn_off, prev_offset, | |
6587 | prog->len); | |
6588 | err = -EINVAL; | |
6589 | goto err_free; | |
6590 | } | |
6591 | ||
fdbaa0be MKL |
6592 | if (!prog->insnsi[linfo[i].insn_off].code) { |
6593 | verbose(env, | |
6594 | "Invalid insn code at line_info[%u].insn_off\n", | |
6595 | i); | |
6596 | err = -EINVAL; | |
6597 | goto err_free; | |
6598 | } | |
6599 | ||
23127b33 MKL |
6600 | if (!btf_name_by_offset(btf, linfo[i].line_off) || |
6601 | !btf_name_by_offset(btf, linfo[i].file_name_off)) { | |
c454a46b MKL |
6602 | verbose(env, "Invalid line_info[%u].line_off or .file_name_off\n", i); |
6603 | err = -EINVAL; | |
6604 | goto err_free; | |
6605 | } | |
6606 | ||
6607 | if (s != env->subprog_cnt) { | |
6608 | if (linfo[i].insn_off == sub[s].start) { | |
6609 | sub[s].linfo_idx = i; | |
6610 | s++; | |
6611 | } else if (sub[s].start < linfo[i].insn_off) { | |
6612 | verbose(env, "missing bpf_line_info for func#%u\n", s); | |
6613 | err = -EINVAL; | |
6614 | goto err_free; | |
6615 | } | |
6616 | } | |
6617 | ||
6618 | prev_offset = linfo[i].insn_off; | |
6619 | ulinfo += rec_size; | |
6620 | } | |
6621 | ||
6622 | if (s != env->subprog_cnt) { | |
6623 | verbose(env, "missing bpf_line_info for %u funcs starting from func#%u\n", | |
6624 | env->subprog_cnt - s, s); | |
6625 | err = -EINVAL; | |
6626 | goto err_free; | |
6627 | } | |
6628 | ||
6629 | prog->aux->linfo = linfo; | |
6630 | prog->aux->nr_linfo = nr_linfo; | |
6631 | ||
6632 | return 0; | |
6633 | ||
6634 | err_free: | |
6635 | kvfree(linfo); | |
6636 | return err; | |
6637 | } | |
6638 | ||
6639 | static int check_btf_info(struct bpf_verifier_env *env, | |
6640 | const union bpf_attr *attr, | |
6641 | union bpf_attr __user *uattr) | |
6642 | { | |
6643 | struct btf *btf; | |
6644 | int err; | |
6645 | ||
6646 | if (!attr->func_info_cnt && !attr->line_info_cnt) | |
6647 | return 0; | |
6648 | ||
6649 | btf = btf_get_by_fd(attr->prog_btf_fd); | |
6650 | if (IS_ERR(btf)) | |
6651 | return PTR_ERR(btf); | |
6652 | env->prog->aux->btf = btf; | |
6653 | ||
6654 | err = check_btf_func(env, attr, uattr); | |
6655 | if (err) | |
6656 | return err; | |
6657 | ||
6658 | err = check_btf_line(env, attr, uattr); | |
6659 | if (err) | |
6660 | return err; | |
6661 | ||
6662 | return 0; | |
ba64e7d8 YS |
6663 | } |
6664 | ||
f1174f77 EC |
6665 | /* check %cur's range satisfies %old's */ |
6666 | static bool range_within(struct bpf_reg_state *old, | |
6667 | struct bpf_reg_state *cur) | |
6668 | { | |
b03c9f9f EC |
6669 | return old->umin_value <= cur->umin_value && |
6670 | old->umax_value >= cur->umax_value && | |
6671 | old->smin_value <= cur->smin_value && | |
6672 | old->smax_value >= cur->smax_value; | |
f1174f77 EC |
6673 | } |
6674 | ||
6675 | /* Maximum number of register states that can exist at once */ | |
6676 | #define ID_MAP_SIZE (MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE) | |
6677 | struct idpair { | |
6678 | u32 old; | |
6679 | u32 cur; | |
6680 | }; | |
6681 | ||
6682 | /* If in the old state two registers had the same id, then they need to have | |
6683 | * the same id in the new state as well. But that id could be different from | |
6684 | * the old state, so we need to track the mapping from old to new ids. | |
6685 | * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent | |
6686 | * regs with old id 5 must also have new id 9 for the new state to be safe. But | |
6687 | * regs with a different old id could still have new id 9, we don't care about | |
6688 | * that. | |
6689 | * So we look through our idmap to see if this old id has been seen before. If | |
6690 | * so, we require the new id to match; otherwise, we add the id pair to the map. | |
969bf05e | 6691 | */ |
f1174f77 | 6692 | static bool check_ids(u32 old_id, u32 cur_id, struct idpair *idmap) |
969bf05e | 6693 | { |
f1174f77 | 6694 | unsigned int i; |
969bf05e | 6695 | |
f1174f77 EC |
6696 | for (i = 0; i < ID_MAP_SIZE; i++) { |
6697 | if (!idmap[i].old) { | |
6698 | /* Reached an empty slot; haven't seen this id before */ | |
6699 | idmap[i].old = old_id; | |
6700 | idmap[i].cur = cur_id; | |
6701 | return true; | |
6702 | } | |
6703 | if (idmap[i].old == old_id) | |
6704 | return idmap[i].cur == cur_id; | |
6705 | } | |
6706 | /* We ran out of idmap slots, which should be impossible */ | |
6707 | WARN_ON_ONCE(1); | |
6708 | return false; | |
6709 | } | |
6710 | ||
9242b5f5 AS |
6711 | static void clean_func_state(struct bpf_verifier_env *env, |
6712 | struct bpf_func_state *st) | |
6713 | { | |
6714 | enum bpf_reg_liveness live; | |
6715 | int i, j; | |
6716 | ||
6717 | for (i = 0; i < BPF_REG_FP; i++) { | |
6718 | live = st->regs[i].live; | |
6719 | /* liveness must not touch this register anymore */ | |
6720 | st->regs[i].live |= REG_LIVE_DONE; | |
6721 | if (!(live & REG_LIVE_READ)) | |
6722 | /* since the register is unused, clear its state | |
6723 | * to make further comparison simpler | |
6724 | */ | |
6725 | __mark_reg_not_init(&st->regs[i]); | |
6726 | } | |
6727 | ||
6728 | for (i = 0; i < st->allocated_stack / BPF_REG_SIZE; i++) { | |
6729 | live = st->stack[i].spilled_ptr.live; | |
6730 | /* liveness must not touch this stack slot anymore */ | |
6731 | st->stack[i].spilled_ptr.live |= REG_LIVE_DONE; | |
6732 | if (!(live & REG_LIVE_READ)) { | |
6733 | __mark_reg_not_init(&st->stack[i].spilled_ptr); | |
6734 | for (j = 0; j < BPF_REG_SIZE; j++) | |
6735 | st->stack[i].slot_type[j] = STACK_INVALID; | |
6736 | } | |
6737 | } | |
6738 | } | |
6739 | ||
6740 | static void clean_verifier_state(struct bpf_verifier_env *env, | |
6741 | struct bpf_verifier_state *st) | |
6742 | { | |
6743 | int i; | |
6744 | ||
6745 | if (st->frame[0]->regs[0].live & REG_LIVE_DONE) | |
6746 | /* all regs in this state in all frames were already marked */ | |
6747 | return; | |
6748 | ||
6749 | for (i = 0; i <= st->curframe; i++) | |
6750 | clean_func_state(env, st->frame[i]); | |
6751 | } | |
6752 | ||
6753 | /* the parentage chains form a tree. | |
6754 | * the verifier states are added to state lists at given insn and | |
6755 | * pushed into state stack for future exploration. | |
6756 | * when the verifier reaches bpf_exit insn some of the verifer states | |
6757 | * stored in the state lists have their final liveness state already, | |
6758 | * but a lot of states will get revised from liveness point of view when | |
6759 | * the verifier explores other branches. | |
6760 | * Example: | |
6761 | * 1: r0 = 1 | |
6762 | * 2: if r1 == 100 goto pc+1 | |
6763 | * 3: r0 = 2 | |
6764 | * 4: exit | |
6765 | * when the verifier reaches exit insn the register r0 in the state list of | |
6766 | * insn 2 will be seen as !REG_LIVE_READ. Then the verifier pops the other_branch | |
6767 | * of insn 2 and goes exploring further. At the insn 4 it will walk the | |
6768 | * parentage chain from insn 4 into insn 2 and will mark r0 as REG_LIVE_READ. | |
6769 | * | |
6770 | * Since the verifier pushes the branch states as it sees them while exploring | |
6771 | * the program the condition of walking the branch instruction for the second | |
6772 | * time means that all states below this branch were already explored and | |
6773 | * their final liveness markes are already propagated. | |
6774 | * Hence when the verifier completes the search of state list in is_state_visited() | |
6775 | * we can call this clean_live_states() function to mark all liveness states | |
6776 | * as REG_LIVE_DONE to indicate that 'parent' pointers of 'struct bpf_reg_state' | |
6777 | * will not be used. | |
6778 | * This function also clears the registers and stack for states that !READ | |
6779 | * to simplify state merging. | |
6780 | * | |
6781 | * Important note here that walking the same branch instruction in the callee | |
6782 | * doesn't meant that the states are DONE. The verifier has to compare | |
6783 | * the callsites | |
6784 | */ | |
6785 | static void clean_live_states(struct bpf_verifier_env *env, int insn, | |
6786 | struct bpf_verifier_state *cur) | |
6787 | { | |
6788 | struct bpf_verifier_state_list *sl; | |
6789 | int i; | |
6790 | ||
5d839021 | 6791 | sl = *explored_state(env, insn); |
a8f500af | 6792 | while (sl) { |
2589726d AS |
6793 | if (sl->state.branches) |
6794 | goto next; | |
dc2a4ebc AS |
6795 | if (sl->state.insn_idx != insn || |
6796 | sl->state.curframe != cur->curframe) | |
9242b5f5 AS |
6797 | goto next; |
6798 | for (i = 0; i <= cur->curframe; i++) | |
6799 | if (sl->state.frame[i]->callsite != cur->frame[i]->callsite) | |
6800 | goto next; | |
6801 | clean_verifier_state(env, &sl->state); | |
6802 | next: | |
6803 | sl = sl->next; | |
6804 | } | |
6805 | } | |
6806 | ||
f1174f77 | 6807 | /* Returns true if (rold safe implies rcur safe) */ |
1b688a19 EC |
6808 | static bool regsafe(struct bpf_reg_state *rold, struct bpf_reg_state *rcur, |
6809 | struct idpair *idmap) | |
f1174f77 | 6810 | { |
f4d7e40a AS |
6811 | bool equal; |
6812 | ||
dc503a8a EC |
6813 | if (!(rold->live & REG_LIVE_READ)) |
6814 | /* explored state didn't use this */ | |
6815 | return true; | |
6816 | ||
679c782d | 6817 | equal = memcmp(rold, rcur, offsetof(struct bpf_reg_state, parent)) == 0; |
f4d7e40a AS |
6818 | |
6819 | if (rold->type == PTR_TO_STACK) | |
6820 | /* two stack pointers are equal only if they're pointing to | |
6821 | * the same stack frame, since fp-8 in foo != fp-8 in bar | |
6822 | */ | |
6823 | return equal && rold->frameno == rcur->frameno; | |
6824 | ||
6825 | if (equal) | |
969bf05e AS |
6826 | return true; |
6827 | ||
f1174f77 EC |
6828 | if (rold->type == NOT_INIT) |
6829 | /* explored state can't have used this */ | |
969bf05e | 6830 | return true; |
f1174f77 EC |
6831 | if (rcur->type == NOT_INIT) |
6832 | return false; | |
6833 | switch (rold->type) { | |
6834 | case SCALAR_VALUE: | |
6835 | if (rcur->type == SCALAR_VALUE) { | |
b5dc0163 AS |
6836 | if (!rold->precise && !rcur->precise) |
6837 | return true; | |
f1174f77 EC |
6838 | /* new val must satisfy old val knowledge */ |
6839 | return range_within(rold, rcur) && | |
6840 | tnum_in(rold->var_off, rcur->var_off); | |
6841 | } else { | |
179d1c56 JH |
6842 | /* We're trying to use a pointer in place of a scalar. |
6843 | * Even if the scalar was unbounded, this could lead to | |
6844 | * pointer leaks because scalars are allowed to leak | |
6845 | * while pointers are not. We could make this safe in | |
6846 | * special cases if root is calling us, but it's | |
6847 | * probably not worth the hassle. | |
f1174f77 | 6848 | */ |
179d1c56 | 6849 | return false; |
f1174f77 EC |
6850 | } |
6851 | case PTR_TO_MAP_VALUE: | |
1b688a19 EC |
6852 | /* If the new min/max/var_off satisfy the old ones and |
6853 | * everything else matches, we are OK. | |
d83525ca AS |
6854 | * 'id' is not compared, since it's only used for maps with |
6855 | * bpf_spin_lock inside map element and in such cases if | |
6856 | * the rest of the prog is valid for one map element then | |
6857 | * it's valid for all map elements regardless of the key | |
6858 | * used in bpf_map_lookup() | |
1b688a19 EC |
6859 | */ |
6860 | return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 && | |
6861 | range_within(rold, rcur) && | |
6862 | tnum_in(rold->var_off, rcur->var_off); | |
f1174f77 EC |
6863 | case PTR_TO_MAP_VALUE_OR_NULL: |
6864 | /* a PTR_TO_MAP_VALUE could be safe to use as a | |
6865 | * PTR_TO_MAP_VALUE_OR_NULL into the same map. | |
6866 | * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL- | |
6867 | * checked, doing so could have affected others with the same | |
6868 | * id, and we can't check for that because we lost the id when | |
6869 | * we converted to a PTR_TO_MAP_VALUE. | |
6870 | */ | |
6871 | if (rcur->type != PTR_TO_MAP_VALUE_OR_NULL) | |
6872 | return false; | |
6873 | if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, id))) | |
6874 | return false; | |
6875 | /* Check our ids match any regs they're supposed to */ | |
6876 | return check_ids(rold->id, rcur->id, idmap); | |
de8f3a83 | 6877 | case PTR_TO_PACKET_META: |
f1174f77 | 6878 | case PTR_TO_PACKET: |
de8f3a83 | 6879 | if (rcur->type != rold->type) |
f1174f77 EC |
6880 | return false; |
6881 | /* We must have at least as much range as the old ptr | |
6882 | * did, so that any accesses which were safe before are | |
6883 | * still safe. This is true even if old range < old off, | |
6884 | * since someone could have accessed through (ptr - k), or | |
6885 | * even done ptr -= k in a register, to get a safe access. | |
6886 | */ | |
6887 | if (rold->range > rcur->range) | |
6888 | return false; | |
6889 | /* If the offsets don't match, we can't trust our alignment; | |
6890 | * nor can we be sure that we won't fall out of range. | |
6891 | */ | |
6892 | if (rold->off != rcur->off) | |
6893 | return false; | |
6894 | /* id relations must be preserved */ | |
6895 | if (rold->id && !check_ids(rold->id, rcur->id, idmap)) | |
6896 | return false; | |
6897 | /* new val must satisfy old val knowledge */ | |
6898 | return range_within(rold, rcur) && | |
6899 | tnum_in(rold->var_off, rcur->var_off); | |
6900 | case PTR_TO_CTX: | |
6901 | case CONST_PTR_TO_MAP: | |
f1174f77 | 6902 | case PTR_TO_PACKET_END: |
d58e468b | 6903 | case PTR_TO_FLOW_KEYS: |
c64b7983 JS |
6904 | case PTR_TO_SOCKET: |
6905 | case PTR_TO_SOCKET_OR_NULL: | |
46f8bc92 MKL |
6906 | case PTR_TO_SOCK_COMMON: |
6907 | case PTR_TO_SOCK_COMMON_OR_NULL: | |
655a51e5 MKL |
6908 | case PTR_TO_TCP_SOCK: |
6909 | case PTR_TO_TCP_SOCK_OR_NULL: | |
fada7fdc | 6910 | case PTR_TO_XDP_SOCK: |
f1174f77 EC |
6911 | /* Only valid matches are exact, which memcmp() above |
6912 | * would have accepted | |
6913 | */ | |
6914 | default: | |
6915 | /* Don't know what's going on, just say it's not safe */ | |
6916 | return false; | |
6917 | } | |
969bf05e | 6918 | |
f1174f77 EC |
6919 | /* Shouldn't get here; if we do, say it's not safe */ |
6920 | WARN_ON_ONCE(1); | |
969bf05e AS |
6921 | return false; |
6922 | } | |
6923 | ||
f4d7e40a AS |
6924 | static bool stacksafe(struct bpf_func_state *old, |
6925 | struct bpf_func_state *cur, | |
638f5b90 AS |
6926 | struct idpair *idmap) |
6927 | { | |
6928 | int i, spi; | |
6929 | ||
638f5b90 AS |
6930 | /* walk slots of the explored stack and ignore any additional |
6931 | * slots in the current stack, since explored(safe) state | |
6932 | * didn't use them | |
6933 | */ | |
6934 | for (i = 0; i < old->allocated_stack; i++) { | |
6935 | spi = i / BPF_REG_SIZE; | |
6936 | ||
b233920c AS |
6937 | if (!(old->stack[spi].spilled_ptr.live & REG_LIVE_READ)) { |
6938 | i += BPF_REG_SIZE - 1; | |
cc2b14d5 | 6939 | /* explored state didn't use this */ |
fd05e57b | 6940 | continue; |
b233920c | 6941 | } |
cc2b14d5 | 6942 | |
638f5b90 AS |
6943 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID) |
6944 | continue; | |
19e2dbb7 AS |
6945 | |
6946 | /* explored stack has more populated slots than current stack | |
6947 | * and these slots were used | |
6948 | */ | |
6949 | if (i >= cur->allocated_stack) | |
6950 | return false; | |
6951 | ||
cc2b14d5 AS |
6952 | /* if old state was safe with misc data in the stack |
6953 | * it will be safe with zero-initialized stack. | |
6954 | * The opposite is not true | |
6955 | */ | |
6956 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC && | |
6957 | cur->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_ZERO) | |
6958 | continue; | |
638f5b90 AS |
6959 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] != |
6960 | cur->stack[spi].slot_type[i % BPF_REG_SIZE]) | |
6961 | /* Ex: old explored (safe) state has STACK_SPILL in | |
6962 | * this stack slot, but current has has STACK_MISC -> | |
6963 | * this verifier states are not equivalent, | |
6964 | * return false to continue verification of this path | |
6965 | */ | |
6966 | return false; | |
6967 | if (i % BPF_REG_SIZE) | |
6968 | continue; | |
6969 | if (old->stack[spi].slot_type[0] != STACK_SPILL) | |
6970 | continue; | |
6971 | if (!regsafe(&old->stack[spi].spilled_ptr, | |
6972 | &cur->stack[spi].spilled_ptr, | |
6973 | idmap)) | |
6974 | /* when explored and current stack slot are both storing | |
6975 | * spilled registers, check that stored pointers types | |
6976 | * are the same as well. | |
6977 | * Ex: explored safe path could have stored | |
6978 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8} | |
6979 | * but current path has stored: | |
6980 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16} | |
6981 | * such verifier states are not equivalent. | |
6982 | * return false to continue verification of this path | |
6983 | */ | |
6984 | return false; | |
6985 | } | |
6986 | return true; | |
6987 | } | |
6988 | ||
fd978bf7 JS |
6989 | static bool refsafe(struct bpf_func_state *old, struct bpf_func_state *cur) |
6990 | { | |
6991 | if (old->acquired_refs != cur->acquired_refs) | |
6992 | return false; | |
6993 | return !memcmp(old->refs, cur->refs, | |
6994 | sizeof(*old->refs) * old->acquired_refs); | |
6995 | } | |
6996 | ||
f1bca824 AS |
6997 | /* compare two verifier states |
6998 | * | |
6999 | * all states stored in state_list are known to be valid, since | |
7000 | * verifier reached 'bpf_exit' instruction through them | |
7001 | * | |
7002 | * this function is called when verifier exploring different branches of | |
7003 | * execution popped from the state stack. If it sees an old state that has | |
7004 | * more strict register state and more strict stack state then this execution | |
7005 | * branch doesn't need to be explored further, since verifier already | |
7006 | * concluded that more strict state leads to valid finish. | |
7007 | * | |
7008 | * Therefore two states are equivalent if register state is more conservative | |
7009 | * and explored stack state is more conservative than the current one. | |
7010 | * Example: | |
7011 | * explored current | |
7012 | * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC) | |
7013 | * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC) | |
7014 | * | |
7015 | * In other words if current stack state (one being explored) has more | |
7016 | * valid slots than old one that already passed validation, it means | |
7017 | * the verifier can stop exploring and conclude that current state is valid too | |
7018 | * | |
7019 | * Similarly with registers. If explored state has register type as invalid | |
7020 | * whereas register type in current state is meaningful, it means that | |
7021 | * the current state will reach 'bpf_exit' instruction safely | |
7022 | */ | |
f4d7e40a AS |
7023 | static bool func_states_equal(struct bpf_func_state *old, |
7024 | struct bpf_func_state *cur) | |
f1bca824 | 7025 | { |
f1174f77 EC |
7026 | struct idpair *idmap; |
7027 | bool ret = false; | |
f1bca824 AS |
7028 | int i; |
7029 | ||
f1174f77 EC |
7030 | idmap = kcalloc(ID_MAP_SIZE, sizeof(struct idpair), GFP_KERNEL); |
7031 | /* If we failed to allocate the idmap, just say it's not safe */ | |
7032 | if (!idmap) | |
1a0dc1ac | 7033 | return false; |
f1174f77 EC |
7034 | |
7035 | for (i = 0; i < MAX_BPF_REG; i++) { | |
1b688a19 | 7036 | if (!regsafe(&old->regs[i], &cur->regs[i], idmap)) |
f1174f77 | 7037 | goto out_free; |
f1bca824 AS |
7038 | } |
7039 | ||
638f5b90 AS |
7040 | if (!stacksafe(old, cur, idmap)) |
7041 | goto out_free; | |
fd978bf7 JS |
7042 | |
7043 | if (!refsafe(old, cur)) | |
7044 | goto out_free; | |
f1174f77 EC |
7045 | ret = true; |
7046 | out_free: | |
7047 | kfree(idmap); | |
7048 | return ret; | |
f1bca824 AS |
7049 | } |
7050 | ||
f4d7e40a AS |
7051 | static bool states_equal(struct bpf_verifier_env *env, |
7052 | struct bpf_verifier_state *old, | |
7053 | struct bpf_verifier_state *cur) | |
7054 | { | |
7055 | int i; | |
7056 | ||
7057 | if (old->curframe != cur->curframe) | |
7058 | return false; | |
7059 | ||
979d63d5 DB |
7060 | /* Verification state from speculative execution simulation |
7061 | * must never prune a non-speculative execution one. | |
7062 | */ | |
7063 | if (old->speculative && !cur->speculative) | |
7064 | return false; | |
7065 | ||
d83525ca AS |
7066 | if (old->active_spin_lock != cur->active_spin_lock) |
7067 | return false; | |
7068 | ||
f4d7e40a AS |
7069 | /* for states to be equal callsites have to be the same |
7070 | * and all frame states need to be equivalent | |
7071 | */ | |
7072 | for (i = 0; i <= old->curframe; i++) { | |
7073 | if (old->frame[i]->callsite != cur->frame[i]->callsite) | |
7074 | return false; | |
7075 | if (!func_states_equal(old->frame[i], cur->frame[i])) | |
7076 | return false; | |
7077 | } | |
7078 | return true; | |
7079 | } | |
7080 | ||
5327ed3d JW |
7081 | /* Return 0 if no propagation happened. Return negative error code if error |
7082 | * happened. Otherwise, return the propagated bit. | |
7083 | */ | |
55e7f3b5 JW |
7084 | static int propagate_liveness_reg(struct bpf_verifier_env *env, |
7085 | struct bpf_reg_state *reg, | |
7086 | struct bpf_reg_state *parent_reg) | |
7087 | { | |
5327ed3d JW |
7088 | u8 parent_flag = parent_reg->live & REG_LIVE_READ; |
7089 | u8 flag = reg->live & REG_LIVE_READ; | |
55e7f3b5 JW |
7090 | int err; |
7091 | ||
5327ed3d JW |
7092 | /* When comes here, read flags of PARENT_REG or REG could be any of |
7093 | * REG_LIVE_READ64, REG_LIVE_READ32, REG_LIVE_NONE. There is no need | |
7094 | * of propagation if PARENT_REG has strongest REG_LIVE_READ64. | |
7095 | */ | |
7096 | if (parent_flag == REG_LIVE_READ64 || | |
7097 | /* Or if there is no read flag from REG. */ | |
7098 | !flag || | |
7099 | /* Or if the read flag from REG is the same as PARENT_REG. */ | |
7100 | parent_flag == flag) | |
55e7f3b5 JW |
7101 | return 0; |
7102 | ||
5327ed3d | 7103 | err = mark_reg_read(env, reg, parent_reg, flag); |
55e7f3b5 JW |
7104 | if (err) |
7105 | return err; | |
7106 | ||
5327ed3d | 7107 | return flag; |
55e7f3b5 JW |
7108 | } |
7109 | ||
8e9cd9ce | 7110 | /* A write screens off any subsequent reads; but write marks come from the |
f4d7e40a AS |
7111 | * straight-line code between a state and its parent. When we arrive at an |
7112 | * equivalent state (jump target or such) we didn't arrive by the straight-line | |
7113 | * code, so read marks in the state must propagate to the parent regardless | |
7114 | * of the state's write marks. That's what 'parent == state->parent' comparison | |
679c782d | 7115 | * in mark_reg_read() is for. |
8e9cd9ce | 7116 | */ |
f4d7e40a AS |
7117 | static int propagate_liveness(struct bpf_verifier_env *env, |
7118 | const struct bpf_verifier_state *vstate, | |
7119 | struct bpf_verifier_state *vparent) | |
dc503a8a | 7120 | { |
3f8cafa4 | 7121 | struct bpf_reg_state *state_reg, *parent_reg; |
f4d7e40a | 7122 | struct bpf_func_state *state, *parent; |
3f8cafa4 | 7123 | int i, frame, err = 0; |
dc503a8a | 7124 | |
f4d7e40a AS |
7125 | if (vparent->curframe != vstate->curframe) { |
7126 | WARN(1, "propagate_live: parent frame %d current frame %d\n", | |
7127 | vparent->curframe, vstate->curframe); | |
7128 | return -EFAULT; | |
7129 | } | |
dc503a8a EC |
7130 | /* Propagate read liveness of registers... */ |
7131 | BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG); | |
83d16312 | 7132 | for (frame = 0; frame <= vstate->curframe; frame++) { |
3f8cafa4 JW |
7133 | parent = vparent->frame[frame]; |
7134 | state = vstate->frame[frame]; | |
7135 | parent_reg = parent->regs; | |
7136 | state_reg = state->regs; | |
83d16312 JK |
7137 | /* We don't need to worry about FP liveness, it's read-only */ |
7138 | for (i = frame < vstate->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) { | |
55e7f3b5 JW |
7139 | err = propagate_liveness_reg(env, &state_reg[i], |
7140 | &parent_reg[i]); | |
5327ed3d | 7141 | if (err < 0) |
3f8cafa4 | 7142 | return err; |
5327ed3d JW |
7143 | if (err == REG_LIVE_READ64) |
7144 | mark_insn_zext(env, &parent_reg[i]); | |
dc503a8a | 7145 | } |
f4d7e40a | 7146 | |
1b04aee7 | 7147 | /* Propagate stack slots. */ |
f4d7e40a AS |
7148 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE && |
7149 | i < parent->allocated_stack / BPF_REG_SIZE; i++) { | |
3f8cafa4 JW |
7150 | parent_reg = &parent->stack[i].spilled_ptr; |
7151 | state_reg = &state->stack[i].spilled_ptr; | |
55e7f3b5 JW |
7152 | err = propagate_liveness_reg(env, state_reg, |
7153 | parent_reg); | |
5327ed3d | 7154 | if (err < 0) |
3f8cafa4 | 7155 | return err; |
dc503a8a EC |
7156 | } |
7157 | } | |
5327ed3d | 7158 | return 0; |
dc503a8a EC |
7159 | } |
7160 | ||
a3ce685d AS |
7161 | /* find precise scalars in the previous equivalent state and |
7162 | * propagate them into the current state | |
7163 | */ | |
7164 | static int propagate_precision(struct bpf_verifier_env *env, | |
7165 | const struct bpf_verifier_state *old) | |
7166 | { | |
7167 | struct bpf_reg_state *state_reg; | |
7168 | struct bpf_func_state *state; | |
7169 | int i, err = 0; | |
7170 | ||
7171 | state = old->frame[old->curframe]; | |
7172 | state_reg = state->regs; | |
7173 | for (i = 0; i < BPF_REG_FP; i++, state_reg++) { | |
7174 | if (state_reg->type != SCALAR_VALUE || | |
7175 | !state_reg->precise) | |
7176 | continue; | |
7177 | if (env->log.level & BPF_LOG_LEVEL2) | |
7178 | verbose(env, "propagating r%d\n", i); | |
7179 | err = mark_chain_precision(env, i); | |
7180 | if (err < 0) | |
7181 | return err; | |
7182 | } | |
7183 | ||
7184 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { | |
7185 | if (state->stack[i].slot_type[0] != STACK_SPILL) | |
7186 | continue; | |
7187 | state_reg = &state->stack[i].spilled_ptr; | |
7188 | if (state_reg->type != SCALAR_VALUE || | |
7189 | !state_reg->precise) | |
7190 | continue; | |
7191 | if (env->log.level & BPF_LOG_LEVEL2) | |
7192 | verbose(env, "propagating fp%d\n", | |
7193 | (-i - 1) * BPF_REG_SIZE); | |
7194 | err = mark_chain_precision_stack(env, i); | |
7195 | if (err < 0) | |
7196 | return err; | |
7197 | } | |
7198 | return 0; | |
7199 | } | |
7200 | ||
2589726d AS |
7201 | static bool states_maybe_looping(struct bpf_verifier_state *old, |
7202 | struct bpf_verifier_state *cur) | |
7203 | { | |
7204 | struct bpf_func_state *fold, *fcur; | |
7205 | int i, fr = cur->curframe; | |
7206 | ||
7207 | if (old->curframe != fr) | |
7208 | return false; | |
7209 | ||
7210 | fold = old->frame[fr]; | |
7211 | fcur = cur->frame[fr]; | |
7212 | for (i = 0; i < MAX_BPF_REG; i++) | |
7213 | if (memcmp(&fold->regs[i], &fcur->regs[i], | |
7214 | offsetof(struct bpf_reg_state, parent))) | |
7215 | return false; | |
7216 | return true; | |
7217 | } | |
7218 | ||
7219 | ||
58e2af8b | 7220 | static int is_state_visited(struct bpf_verifier_env *env, int insn_idx) |
f1bca824 | 7221 | { |
58e2af8b | 7222 | struct bpf_verifier_state_list *new_sl; |
9f4686c4 | 7223 | struct bpf_verifier_state_list *sl, **pprev; |
679c782d | 7224 | struct bpf_verifier_state *cur = env->cur_state, *new; |
ceefbc96 | 7225 | int i, j, err, states_cnt = 0; |
10d274e8 | 7226 | bool add_new_state = env->test_state_freq ? true : false; |
f1bca824 | 7227 | |
b5dc0163 | 7228 | cur->last_insn_idx = env->prev_insn_idx; |
a8f500af | 7229 | if (!env->insn_aux_data[insn_idx].prune_point) |
f1bca824 AS |
7230 | /* this 'insn_idx' instruction wasn't marked, so we will not |
7231 | * be doing state search here | |
7232 | */ | |
7233 | return 0; | |
7234 | ||
2589726d AS |
7235 | /* bpf progs typically have pruning point every 4 instructions |
7236 | * http://vger.kernel.org/bpfconf2019.html#session-1 | |
7237 | * Do not add new state for future pruning if the verifier hasn't seen | |
7238 | * at least 2 jumps and at least 8 instructions. | |
7239 | * This heuristics helps decrease 'total_states' and 'peak_states' metric. | |
7240 | * In tests that amounts to up to 50% reduction into total verifier | |
7241 | * memory consumption and 20% verifier time speedup. | |
7242 | */ | |
7243 | if (env->jmps_processed - env->prev_jmps_processed >= 2 && | |
7244 | env->insn_processed - env->prev_insn_processed >= 8) | |
7245 | add_new_state = true; | |
7246 | ||
a8f500af AS |
7247 | pprev = explored_state(env, insn_idx); |
7248 | sl = *pprev; | |
7249 | ||
9242b5f5 AS |
7250 | clean_live_states(env, insn_idx, cur); |
7251 | ||
a8f500af | 7252 | while (sl) { |
dc2a4ebc AS |
7253 | states_cnt++; |
7254 | if (sl->state.insn_idx != insn_idx) | |
7255 | goto next; | |
2589726d AS |
7256 | if (sl->state.branches) { |
7257 | if (states_maybe_looping(&sl->state, cur) && | |
7258 | states_equal(env, &sl->state, cur)) { | |
7259 | verbose_linfo(env, insn_idx, "; "); | |
7260 | verbose(env, "infinite loop detected at insn %d\n", insn_idx); | |
7261 | return -EINVAL; | |
7262 | } | |
7263 | /* if the verifier is processing a loop, avoid adding new state | |
7264 | * too often, since different loop iterations have distinct | |
7265 | * states and may not help future pruning. | |
7266 | * This threshold shouldn't be too low to make sure that | |
7267 | * a loop with large bound will be rejected quickly. | |
7268 | * The most abusive loop will be: | |
7269 | * r1 += 1 | |
7270 | * if r1 < 1000000 goto pc-2 | |
7271 | * 1M insn_procssed limit / 100 == 10k peak states. | |
7272 | * This threshold shouldn't be too high either, since states | |
7273 | * at the end of the loop are likely to be useful in pruning. | |
7274 | */ | |
7275 | if (env->jmps_processed - env->prev_jmps_processed < 20 && | |
7276 | env->insn_processed - env->prev_insn_processed < 100) | |
7277 | add_new_state = false; | |
7278 | goto miss; | |
7279 | } | |
638f5b90 | 7280 | if (states_equal(env, &sl->state, cur)) { |
9f4686c4 | 7281 | sl->hit_cnt++; |
f1bca824 | 7282 | /* reached equivalent register/stack state, |
dc503a8a EC |
7283 | * prune the search. |
7284 | * Registers read by the continuation are read by us. | |
8e9cd9ce EC |
7285 | * If we have any write marks in env->cur_state, they |
7286 | * will prevent corresponding reads in the continuation | |
7287 | * from reaching our parent (an explored_state). Our | |
7288 | * own state will get the read marks recorded, but | |
7289 | * they'll be immediately forgotten as we're pruning | |
7290 | * this state and will pop a new one. | |
f1bca824 | 7291 | */ |
f4d7e40a | 7292 | err = propagate_liveness(env, &sl->state, cur); |
a3ce685d AS |
7293 | |
7294 | /* if previous state reached the exit with precision and | |
7295 | * current state is equivalent to it (except precsion marks) | |
7296 | * the precision needs to be propagated back in | |
7297 | * the current state. | |
7298 | */ | |
7299 | err = err ? : push_jmp_history(env, cur); | |
7300 | err = err ? : propagate_precision(env, &sl->state); | |
f4d7e40a AS |
7301 | if (err) |
7302 | return err; | |
f1bca824 | 7303 | return 1; |
dc503a8a | 7304 | } |
2589726d AS |
7305 | miss: |
7306 | /* when new state is not going to be added do not increase miss count. | |
7307 | * Otherwise several loop iterations will remove the state | |
7308 | * recorded earlier. The goal of these heuristics is to have | |
7309 | * states from some iterations of the loop (some in the beginning | |
7310 | * and some at the end) to help pruning. | |
7311 | */ | |
7312 | if (add_new_state) | |
7313 | sl->miss_cnt++; | |
9f4686c4 AS |
7314 | /* heuristic to determine whether this state is beneficial |
7315 | * to keep checking from state equivalence point of view. | |
7316 | * Higher numbers increase max_states_per_insn and verification time, | |
7317 | * but do not meaningfully decrease insn_processed. | |
7318 | */ | |
7319 | if (sl->miss_cnt > sl->hit_cnt * 3 + 3) { | |
7320 | /* the state is unlikely to be useful. Remove it to | |
7321 | * speed up verification | |
7322 | */ | |
7323 | *pprev = sl->next; | |
7324 | if (sl->state.frame[0]->regs[0].live & REG_LIVE_DONE) { | |
2589726d AS |
7325 | u32 br = sl->state.branches; |
7326 | ||
7327 | WARN_ONCE(br, | |
7328 | "BUG live_done but branches_to_explore %d\n", | |
7329 | br); | |
9f4686c4 AS |
7330 | free_verifier_state(&sl->state, false); |
7331 | kfree(sl); | |
7332 | env->peak_states--; | |
7333 | } else { | |
7334 | /* cannot free this state, since parentage chain may | |
7335 | * walk it later. Add it for free_list instead to | |
7336 | * be freed at the end of verification | |
7337 | */ | |
7338 | sl->next = env->free_list; | |
7339 | env->free_list = sl; | |
7340 | } | |
7341 | sl = *pprev; | |
7342 | continue; | |
7343 | } | |
dc2a4ebc | 7344 | next: |
9f4686c4 AS |
7345 | pprev = &sl->next; |
7346 | sl = *pprev; | |
f1bca824 AS |
7347 | } |
7348 | ||
06ee7115 AS |
7349 | if (env->max_states_per_insn < states_cnt) |
7350 | env->max_states_per_insn = states_cnt; | |
7351 | ||
ceefbc96 | 7352 | if (!env->allow_ptr_leaks && states_cnt > BPF_COMPLEXITY_LIMIT_STATES) |
b5dc0163 | 7353 | return push_jmp_history(env, cur); |
ceefbc96 | 7354 | |
2589726d | 7355 | if (!add_new_state) |
b5dc0163 | 7356 | return push_jmp_history(env, cur); |
ceefbc96 | 7357 | |
2589726d AS |
7358 | /* There were no equivalent states, remember the current one. |
7359 | * Technically the current state is not proven to be safe yet, | |
f4d7e40a | 7360 | * but it will either reach outer most bpf_exit (which means it's safe) |
2589726d | 7361 | * or it will be rejected. When there are no loops the verifier won't be |
f4d7e40a | 7362 | * seeing this tuple (frame[0].callsite, frame[1].callsite, .. insn_idx) |
2589726d AS |
7363 | * again on the way to bpf_exit. |
7364 | * When looping the sl->state.branches will be > 0 and this state | |
7365 | * will not be considered for equivalence until branches == 0. | |
f1bca824 | 7366 | */ |
638f5b90 | 7367 | new_sl = kzalloc(sizeof(struct bpf_verifier_state_list), GFP_KERNEL); |
f1bca824 AS |
7368 | if (!new_sl) |
7369 | return -ENOMEM; | |
06ee7115 AS |
7370 | env->total_states++; |
7371 | env->peak_states++; | |
2589726d AS |
7372 | env->prev_jmps_processed = env->jmps_processed; |
7373 | env->prev_insn_processed = env->insn_processed; | |
f1bca824 AS |
7374 | |
7375 | /* add new state to the head of linked list */ | |
679c782d EC |
7376 | new = &new_sl->state; |
7377 | err = copy_verifier_state(new, cur); | |
1969db47 | 7378 | if (err) { |
679c782d | 7379 | free_verifier_state(new, false); |
1969db47 AS |
7380 | kfree(new_sl); |
7381 | return err; | |
7382 | } | |
dc2a4ebc | 7383 | new->insn_idx = insn_idx; |
2589726d AS |
7384 | WARN_ONCE(new->branches != 1, |
7385 | "BUG is_state_visited:branches_to_explore=%d insn %d\n", new->branches, insn_idx); | |
b5dc0163 | 7386 | |
2589726d | 7387 | cur->parent = new; |
b5dc0163 AS |
7388 | cur->first_insn_idx = insn_idx; |
7389 | clear_jmp_history(cur); | |
5d839021 AS |
7390 | new_sl->next = *explored_state(env, insn_idx); |
7391 | *explored_state(env, insn_idx) = new_sl; | |
7640ead9 JK |
7392 | /* connect new state to parentage chain. Current frame needs all |
7393 | * registers connected. Only r6 - r9 of the callers are alive (pushed | |
7394 | * to the stack implicitly by JITs) so in callers' frames connect just | |
7395 | * r6 - r9 as an optimization. Callers will have r1 - r5 connected to | |
7396 | * the state of the call instruction (with WRITTEN set), and r0 comes | |
7397 | * from callee with its full parentage chain, anyway. | |
7398 | */ | |
8e9cd9ce EC |
7399 | /* clear write marks in current state: the writes we did are not writes |
7400 | * our child did, so they don't screen off its reads from us. | |
7401 | * (There are no read marks in current state, because reads always mark | |
7402 | * their parent and current state never has children yet. Only | |
7403 | * explored_states can get read marks.) | |
7404 | */ | |
eea1c227 AS |
7405 | for (j = 0; j <= cur->curframe; j++) { |
7406 | for (i = j < cur->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) | |
7407 | cur->frame[j]->regs[i].parent = &new->frame[j]->regs[i]; | |
7408 | for (i = 0; i < BPF_REG_FP; i++) | |
7409 | cur->frame[j]->regs[i].live = REG_LIVE_NONE; | |
7410 | } | |
f4d7e40a AS |
7411 | |
7412 | /* all stack frames are accessible from callee, clear them all */ | |
7413 | for (j = 0; j <= cur->curframe; j++) { | |
7414 | struct bpf_func_state *frame = cur->frame[j]; | |
679c782d | 7415 | struct bpf_func_state *newframe = new->frame[j]; |
f4d7e40a | 7416 | |
679c782d | 7417 | for (i = 0; i < frame->allocated_stack / BPF_REG_SIZE; i++) { |
cc2b14d5 | 7418 | frame->stack[i].spilled_ptr.live = REG_LIVE_NONE; |
679c782d EC |
7419 | frame->stack[i].spilled_ptr.parent = |
7420 | &newframe->stack[i].spilled_ptr; | |
7421 | } | |
f4d7e40a | 7422 | } |
f1bca824 AS |
7423 | return 0; |
7424 | } | |
7425 | ||
c64b7983 JS |
7426 | /* Return true if it's OK to have the same insn return a different type. */ |
7427 | static bool reg_type_mismatch_ok(enum bpf_reg_type type) | |
7428 | { | |
7429 | switch (type) { | |
7430 | case PTR_TO_CTX: | |
7431 | case PTR_TO_SOCKET: | |
7432 | case PTR_TO_SOCKET_OR_NULL: | |
46f8bc92 MKL |
7433 | case PTR_TO_SOCK_COMMON: |
7434 | case PTR_TO_SOCK_COMMON_OR_NULL: | |
655a51e5 MKL |
7435 | case PTR_TO_TCP_SOCK: |
7436 | case PTR_TO_TCP_SOCK_OR_NULL: | |
fada7fdc | 7437 | case PTR_TO_XDP_SOCK: |
c64b7983 JS |
7438 | return false; |
7439 | default: | |
7440 | return true; | |
7441 | } | |
7442 | } | |
7443 | ||
7444 | /* If an instruction was previously used with particular pointer types, then we | |
7445 | * need to be careful to avoid cases such as the below, where it may be ok | |
7446 | * for one branch accessing the pointer, but not ok for the other branch: | |
7447 | * | |
7448 | * R1 = sock_ptr | |
7449 | * goto X; | |
7450 | * ... | |
7451 | * R1 = some_other_valid_ptr; | |
7452 | * goto X; | |
7453 | * ... | |
7454 | * R2 = *(u32 *)(R1 + 0); | |
7455 | */ | |
7456 | static bool reg_type_mismatch(enum bpf_reg_type src, enum bpf_reg_type prev) | |
7457 | { | |
7458 | return src != prev && (!reg_type_mismatch_ok(src) || | |
7459 | !reg_type_mismatch_ok(prev)); | |
7460 | } | |
7461 | ||
58e2af8b | 7462 | static int do_check(struct bpf_verifier_env *env) |
17a52670 | 7463 | { |
638f5b90 | 7464 | struct bpf_verifier_state *state; |
17a52670 | 7465 | struct bpf_insn *insns = env->prog->insnsi; |
638f5b90 | 7466 | struct bpf_reg_state *regs; |
06ee7115 | 7467 | int insn_cnt = env->prog->len; |
17a52670 | 7468 | bool do_print_state = false; |
b5dc0163 | 7469 | int prev_insn_idx = -1; |
17a52670 | 7470 | |
d9762e84 MKL |
7471 | env->prev_linfo = NULL; |
7472 | ||
638f5b90 AS |
7473 | state = kzalloc(sizeof(struct bpf_verifier_state), GFP_KERNEL); |
7474 | if (!state) | |
7475 | return -ENOMEM; | |
f4d7e40a | 7476 | state->curframe = 0; |
979d63d5 | 7477 | state->speculative = false; |
2589726d | 7478 | state->branches = 1; |
f4d7e40a AS |
7479 | state->frame[0] = kzalloc(sizeof(struct bpf_func_state), GFP_KERNEL); |
7480 | if (!state->frame[0]) { | |
7481 | kfree(state); | |
7482 | return -ENOMEM; | |
7483 | } | |
7484 | env->cur_state = state; | |
7485 | init_func_state(env, state->frame[0], | |
7486 | BPF_MAIN_FUNC /* callsite */, | |
7487 | 0 /* frameno */, | |
7488 | 0 /* subprogno, zero == main subprog */); | |
c08435ec | 7489 | |
17a52670 AS |
7490 | for (;;) { |
7491 | struct bpf_insn *insn; | |
7492 | u8 class; | |
7493 | int err; | |
7494 | ||
b5dc0163 | 7495 | env->prev_insn_idx = prev_insn_idx; |
c08435ec | 7496 | if (env->insn_idx >= insn_cnt) { |
61bd5218 | 7497 | verbose(env, "invalid insn idx %d insn_cnt %d\n", |
c08435ec | 7498 | env->insn_idx, insn_cnt); |
17a52670 AS |
7499 | return -EFAULT; |
7500 | } | |
7501 | ||
c08435ec | 7502 | insn = &insns[env->insn_idx]; |
17a52670 AS |
7503 | class = BPF_CLASS(insn->code); |
7504 | ||
06ee7115 | 7505 | if (++env->insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) { |
61bd5218 JK |
7506 | verbose(env, |
7507 | "BPF program is too large. Processed %d insn\n", | |
06ee7115 | 7508 | env->insn_processed); |
17a52670 AS |
7509 | return -E2BIG; |
7510 | } | |
7511 | ||
c08435ec | 7512 | err = is_state_visited(env, env->insn_idx); |
f1bca824 AS |
7513 | if (err < 0) |
7514 | return err; | |
7515 | if (err == 1) { | |
7516 | /* found equivalent state, can prune the search */ | |
06ee7115 | 7517 | if (env->log.level & BPF_LOG_LEVEL) { |
f1bca824 | 7518 | if (do_print_state) |
979d63d5 DB |
7519 | verbose(env, "\nfrom %d to %d%s: safe\n", |
7520 | env->prev_insn_idx, env->insn_idx, | |
7521 | env->cur_state->speculative ? | |
7522 | " (speculative execution)" : ""); | |
f1bca824 | 7523 | else |
c08435ec | 7524 | verbose(env, "%d: safe\n", env->insn_idx); |
f1bca824 AS |
7525 | } |
7526 | goto process_bpf_exit; | |
7527 | } | |
7528 | ||
c3494801 AS |
7529 | if (signal_pending(current)) |
7530 | return -EAGAIN; | |
7531 | ||
3c2ce60b DB |
7532 | if (need_resched()) |
7533 | cond_resched(); | |
7534 | ||
06ee7115 AS |
7535 | if (env->log.level & BPF_LOG_LEVEL2 || |
7536 | (env->log.level & BPF_LOG_LEVEL && do_print_state)) { | |
7537 | if (env->log.level & BPF_LOG_LEVEL2) | |
c08435ec | 7538 | verbose(env, "%d:", env->insn_idx); |
c5fc9692 | 7539 | else |
979d63d5 DB |
7540 | verbose(env, "\nfrom %d to %d%s:", |
7541 | env->prev_insn_idx, env->insn_idx, | |
7542 | env->cur_state->speculative ? | |
7543 | " (speculative execution)" : ""); | |
f4d7e40a | 7544 | print_verifier_state(env, state->frame[state->curframe]); |
17a52670 AS |
7545 | do_print_state = false; |
7546 | } | |
7547 | ||
06ee7115 | 7548 | if (env->log.level & BPF_LOG_LEVEL) { |
7105e828 DB |
7549 | const struct bpf_insn_cbs cbs = { |
7550 | .cb_print = verbose, | |
abe08840 | 7551 | .private_data = env, |
7105e828 DB |
7552 | }; |
7553 | ||
c08435ec DB |
7554 | verbose_linfo(env, env->insn_idx, "; "); |
7555 | verbose(env, "%d: ", env->insn_idx); | |
abe08840 | 7556 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); |
17a52670 AS |
7557 | } |
7558 | ||
cae1927c | 7559 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
c08435ec DB |
7560 | err = bpf_prog_offload_verify_insn(env, env->insn_idx, |
7561 | env->prev_insn_idx); | |
cae1927c JK |
7562 | if (err) |
7563 | return err; | |
7564 | } | |
13a27dfc | 7565 | |
638f5b90 | 7566 | regs = cur_regs(env); |
c08435ec | 7567 | env->insn_aux_data[env->insn_idx].seen = true; |
b5dc0163 | 7568 | prev_insn_idx = env->insn_idx; |
fd978bf7 | 7569 | |
17a52670 | 7570 | if (class == BPF_ALU || class == BPF_ALU64) { |
1be7f75d | 7571 | err = check_alu_op(env, insn); |
17a52670 AS |
7572 | if (err) |
7573 | return err; | |
7574 | ||
7575 | } else if (class == BPF_LDX) { | |
3df126f3 | 7576 | enum bpf_reg_type *prev_src_type, src_reg_type; |
9bac3d6d AS |
7577 | |
7578 | /* check for reserved fields is already done */ | |
7579 | ||
17a52670 | 7580 | /* check src operand */ |
dc503a8a | 7581 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
7582 | if (err) |
7583 | return err; | |
7584 | ||
dc503a8a | 7585 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
17a52670 AS |
7586 | if (err) |
7587 | return err; | |
7588 | ||
725f9dcd AS |
7589 | src_reg_type = regs[insn->src_reg].type; |
7590 | ||
17a52670 AS |
7591 | /* check that memory (src_reg + off) is readable, |
7592 | * the state of dst_reg will be updated by this func | |
7593 | */ | |
c08435ec DB |
7594 | err = check_mem_access(env, env->insn_idx, insn->src_reg, |
7595 | insn->off, BPF_SIZE(insn->code), | |
7596 | BPF_READ, insn->dst_reg, false); | |
17a52670 AS |
7597 | if (err) |
7598 | return err; | |
7599 | ||
c08435ec | 7600 | prev_src_type = &env->insn_aux_data[env->insn_idx].ptr_type; |
3df126f3 JK |
7601 | |
7602 | if (*prev_src_type == NOT_INIT) { | |
9bac3d6d AS |
7603 | /* saw a valid insn |
7604 | * dst_reg = *(u32 *)(src_reg + off) | |
3df126f3 | 7605 | * save type to validate intersecting paths |
9bac3d6d | 7606 | */ |
3df126f3 | 7607 | *prev_src_type = src_reg_type; |
9bac3d6d | 7608 | |
c64b7983 | 7609 | } else if (reg_type_mismatch(src_reg_type, *prev_src_type)) { |
9bac3d6d AS |
7610 | /* ABuser program is trying to use the same insn |
7611 | * dst_reg = *(u32*) (src_reg + off) | |
7612 | * with different pointer types: | |
7613 | * src_reg == ctx in one branch and | |
7614 | * src_reg == stack|map in some other branch. | |
7615 | * Reject it. | |
7616 | */ | |
61bd5218 | 7617 | verbose(env, "same insn cannot be used with different pointers\n"); |
9bac3d6d AS |
7618 | return -EINVAL; |
7619 | } | |
7620 | ||
17a52670 | 7621 | } else if (class == BPF_STX) { |
3df126f3 | 7622 | enum bpf_reg_type *prev_dst_type, dst_reg_type; |
d691f9e8 | 7623 | |
17a52670 | 7624 | if (BPF_MODE(insn->code) == BPF_XADD) { |
c08435ec | 7625 | err = check_xadd(env, env->insn_idx, insn); |
17a52670 AS |
7626 | if (err) |
7627 | return err; | |
c08435ec | 7628 | env->insn_idx++; |
17a52670 AS |
7629 | continue; |
7630 | } | |
7631 | ||
17a52670 | 7632 | /* check src1 operand */ |
dc503a8a | 7633 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
7634 | if (err) |
7635 | return err; | |
7636 | /* check src2 operand */ | |
dc503a8a | 7637 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
7638 | if (err) |
7639 | return err; | |
7640 | ||
d691f9e8 AS |
7641 | dst_reg_type = regs[insn->dst_reg].type; |
7642 | ||
17a52670 | 7643 | /* check that memory (dst_reg + off) is writeable */ |
c08435ec DB |
7644 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
7645 | insn->off, BPF_SIZE(insn->code), | |
7646 | BPF_WRITE, insn->src_reg, false); | |
17a52670 AS |
7647 | if (err) |
7648 | return err; | |
7649 | ||
c08435ec | 7650 | prev_dst_type = &env->insn_aux_data[env->insn_idx].ptr_type; |
3df126f3 JK |
7651 | |
7652 | if (*prev_dst_type == NOT_INIT) { | |
7653 | *prev_dst_type = dst_reg_type; | |
c64b7983 | 7654 | } else if (reg_type_mismatch(dst_reg_type, *prev_dst_type)) { |
61bd5218 | 7655 | verbose(env, "same insn cannot be used with different pointers\n"); |
d691f9e8 AS |
7656 | return -EINVAL; |
7657 | } | |
7658 | ||
17a52670 AS |
7659 | } else if (class == BPF_ST) { |
7660 | if (BPF_MODE(insn->code) != BPF_MEM || | |
7661 | insn->src_reg != BPF_REG_0) { | |
61bd5218 | 7662 | verbose(env, "BPF_ST uses reserved fields\n"); |
17a52670 AS |
7663 | return -EINVAL; |
7664 | } | |
7665 | /* check src operand */ | |
dc503a8a | 7666 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
7667 | if (err) |
7668 | return err; | |
7669 | ||
f37a8cb8 | 7670 | if (is_ctx_reg(env, insn->dst_reg)) { |
9d2be44a | 7671 | verbose(env, "BPF_ST stores into R%d %s is not allowed\n", |
2a159c6f DB |
7672 | insn->dst_reg, |
7673 | reg_type_str[reg_state(env, insn->dst_reg)->type]); | |
f37a8cb8 DB |
7674 | return -EACCES; |
7675 | } | |
7676 | ||
17a52670 | 7677 | /* check that memory (dst_reg + off) is writeable */ |
c08435ec DB |
7678 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
7679 | insn->off, BPF_SIZE(insn->code), | |
7680 | BPF_WRITE, -1, false); | |
17a52670 AS |
7681 | if (err) |
7682 | return err; | |
7683 | ||
092ed096 | 7684 | } else if (class == BPF_JMP || class == BPF_JMP32) { |
17a52670 AS |
7685 | u8 opcode = BPF_OP(insn->code); |
7686 | ||
2589726d | 7687 | env->jmps_processed++; |
17a52670 AS |
7688 | if (opcode == BPF_CALL) { |
7689 | if (BPF_SRC(insn->code) != BPF_K || | |
7690 | insn->off != 0 || | |
f4d7e40a AS |
7691 | (insn->src_reg != BPF_REG_0 && |
7692 | insn->src_reg != BPF_PSEUDO_CALL) || | |
092ed096 JW |
7693 | insn->dst_reg != BPF_REG_0 || |
7694 | class == BPF_JMP32) { | |
61bd5218 | 7695 | verbose(env, "BPF_CALL uses reserved fields\n"); |
17a52670 AS |
7696 | return -EINVAL; |
7697 | } | |
7698 | ||
d83525ca AS |
7699 | if (env->cur_state->active_spin_lock && |
7700 | (insn->src_reg == BPF_PSEUDO_CALL || | |
7701 | insn->imm != BPF_FUNC_spin_unlock)) { | |
7702 | verbose(env, "function calls are not allowed while holding a lock\n"); | |
7703 | return -EINVAL; | |
7704 | } | |
f4d7e40a | 7705 | if (insn->src_reg == BPF_PSEUDO_CALL) |
c08435ec | 7706 | err = check_func_call(env, insn, &env->insn_idx); |
f4d7e40a | 7707 | else |
c08435ec | 7708 | err = check_helper_call(env, insn->imm, env->insn_idx); |
17a52670 AS |
7709 | if (err) |
7710 | return err; | |
7711 | ||
7712 | } else if (opcode == BPF_JA) { | |
7713 | if (BPF_SRC(insn->code) != BPF_K || | |
7714 | insn->imm != 0 || | |
7715 | insn->src_reg != BPF_REG_0 || | |
092ed096 JW |
7716 | insn->dst_reg != BPF_REG_0 || |
7717 | class == BPF_JMP32) { | |
61bd5218 | 7718 | verbose(env, "BPF_JA uses reserved fields\n"); |
17a52670 AS |
7719 | return -EINVAL; |
7720 | } | |
7721 | ||
c08435ec | 7722 | env->insn_idx += insn->off + 1; |
17a52670 AS |
7723 | continue; |
7724 | ||
7725 | } else if (opcode == BPF_EXIT) { | |
7726 | if (BPF_SRC(insn->code) != BPF_K || | |
7727 | insn->imm != 0 || | |
7728 | insn->src_reg != BPF_REG_0 || | |
092ed096 JW |
7729 | insn->dst_reg != BPF_REG_0 || |
7730 | class == BPF_JMP32) { | |
61bd5218 | 7731 | verbose(env, "BPF_EXIT uses reserved fields\n"); |
17a52670 AS |
7732 | return -EINVAL; |
7733 | } | |
7734 | ||
d83525ca AS |
7735 | if (env->cur_state->active_spin_lock) { |
7736 | verbose(env, "bpf_spin_unlock is missing\n"); | |
7737 | return -EINVAL; | |
7738 | } | |
7739 | ||
f4d7e40a AS |
7740 | if (state->curframe) { |
7741 | /* exit from nested function */ | |
c08435ec | 7742 | err = prepare_func_exit(env, &env->insn_idx); |
f4d7e40a AS |
7743 | if (err) |
7744 | return err; | |
7745 | do_print_state = true; | |
7746 | continue; | |
7747 | } | |
7748 | ||
fd978bf7 JS |
7749 | err = check_reference_leak(env); |
7750 | if (err) | |
7751 | return err; | |
7752 | ||
17a52670 AS |
7753 | /* eBPF calling convetion is such that R0 is used |
7754 | * to return the value from eBPF program. | |
7755 | * Make sure that it's readable at this time | |
7756 | * of bpf_exit, which means that program wrote | |
7757 | * something into it earlier | |
7758 | */ | |
dc503a8a | 7759 | err = check_reg_arg(env, BPF_REG_0, SRC_OP); |
17a52670 AS |
7760 | if (err) |
7761 | return err; | |
7762 | ||
1be7f75d | 7763 | if (is_pointer_value(env, BPF_REG_0)) { |
61bd5218 | 7764 | verbose(env, "R0 leaks addr as return value\n"); |
1be7f75d AS |
7765 | return -EACCES; |
7766 | } | |
7767 | ||
390ee7e2 AS |
7768 | err = check_return_code(env); |
7769 | if (err) | |
7770 | return err; | |
f1bca824 | 7771 | process_bpf_exit: |
2589726d | 7772 | update_branch_counts(env, env->cur_state); |
b5dc0163 | 7773 | err = pop_stack(env, &prev_insn_idx, |
c08435ec | 7774 | &env->insn_idx); |
638f5b90 AS |
7775 | if (err < 0) { |
7776 | if (err != -ENOENT) | |
7777 | return err; | |
17a52670 AS |
7778 | break; |
7779 | } else { | |
7780 | do_print_state = true; | |
7781 | continue; | |
7782 | } | |
7783 | } else { | |
c08435ec | 7784 | err = check_cond_jmp_op(env, insn, &env->insn_idx); |
17a52670 AS |
7785 | if (err) |
7786 | return err; | |
7787 | } | |
7788 | } else if (class == BPF_LD) { | |
7789 | u8 mode = BPF_MODE(insn->code); | |
7790 | ||
7791 | if (mode == BPF_ABS || mode == BPF_IND) { | |
ddd872bc AS |
7792 | err = check_ld_abs(env, insn); |
7793 | if (err) | |
7794 | return err; | |
7795 | ||
17a52670 AS |
7796 | } else if (mode == BPF_IMM) { |
7797 | err = check_ld_imm(env, insn); | |
7798 | if (err) | |
7799 | return err; | |
7800 | ||
c08435ec DB |
7801 | env->insn_idx++; |
7802 | env->insn_aux_data[env->insn_idx].seen = true; | |
17a52670 | 7803 | } else { |
61bd5218 | 7804 | verbose(env, "invalid BPF_LD mode\n"); |
17a52670 AS |
7805 | return -EINVAL; |
7806 | } | |
7807 | } else { | |
61bd5218 | 7808 | verbose(env, "unknown insn class %d\n", class); |
17a52670 AS |
7809 | return -EINVAL; |
7810 | } | |
7811 | ||
c08435ec | 7812 | env->insn_idx++; |
17a52670 AS |
7813 | } |
7814 | ||
9c8105bd | 7815 | env->prog->aux->stack_depth = env->subprog_info[0].stack_depth; |
17a52670 AS |
7816 | return 0; |
7817 | } | |
7818 | ||
56f668df MKL |
7819 | static int check_map_prealloc(struct bpf_map *map) |
7820 | { | |
7821 | return (map->map_type != BPF_MAP_TYPE_HASH && | |
bcc6b1b7 MKL |
7822 | map->map_type != BPF_MAP_TYPE_PERCPU_HASH && |
7823 | map->map_type != BPF_MAP_TYPE_HASH_OF_MAPS) || | |
56f668df MKL |
7824 | !(map->map_flags & BPF_F_NO_PREALLOC); |
7825 | } | |
7826 | ||
d83525ca AS |
7827 | static bool is_tracing_prog_type(enum bpf_prog_type type) |
7828 | { | |
7829 | switch (type) { | |
7830 | case BPF_PROG_TYPE_KPROBE: | |
7831 | case BPF_PROG_TYPE_TRACEPOINT: | |
7832 | case BPF_PROG_TYPE_PERF_EVENT: | |
7833 | case BPF_PROG_TYPE_RAW_TRACEPOINT: | |
7834 | return true; | |
7835 | default: | |
7836 | return false; | |
7837 | } | |
7838 | } | |
7839 | ||
61bd5218 JK |
7840 | static int check_map_prog_compatibility(struct bpf_verifier_env *env, |
7841 | struct bpf_map *map, | |
fdc15d38 AS |
7842 | struct bpf_prog *prog) |
7843 | ||
7844 | { | |
56f668df MKL |
7845 | /* Make sure that BPF_PROG_TYPE_PERF_EVENT programs only use |
7846 | * preallocated hash maps, since doing memory allocation | |
7847 | * in overflow_handler can crash depending on where nmi got | |
7848 | * triggered. | |
7849 | */ | |
7850 | if (prog->type == BPF_PROG_TYPE_PERF_EVENT) { | |
7851 | if (!check_map_prealloc(map)) { | |
61bd5218 | 7852 | verbose(env, "perf_event programs can only use preallocated hash map\n"); |
56f668df MKL |
7853 | return -EINVAL; |
7854 | } | |
7855 | if (map->inner_map_meta && | |
7856 | !check_map_prealloc(map->inner_map_meta)) { | |
61bd5218 | 7857 | verbose(env, "perf_event programs can only use preallocated inner hash map\n"); |
56f668df MKL |
7858 | return -EINVAL; |
7859 | } | |
fdc15d38 | 7860 | } |
a3884572 | 7861 | |
d83525ca AS |
7862 | if ((is_tracing_prog_type(prog->type) || |
7863 | prog->type == BPF_PROG_TYPE_SOCKET_FILTER) && | |
7864 | map_value_has_spin_lock(map)) { | |
7865 | verbose(env, "tracing progs cannot use bpf_spin_lock yet\n"); | |
7866 | return -EINVAL; | |
7867 | } | |
7868 | ||
a3884572 | 7869 | if ((bpf_prog_is_dev_bound(prog->aux) || bpf_map_is_dev_bound(map)) && |
09728266 | 7870 | !bpf_offload_prog_map_match(prog, map)) { |
a3884572 JK |
7871 | verbose(env, "offload device mismatch between prog and map\n"); |
7872 | return -EINVAL; | |
7873 | } | |
7874 | ||
fdc15d38 AS |
7875 | return 0; |
7876 | } | |
7877 | ||
b741f163 RG |
7878 | static bool bpf_map_is_cgroup_storage(struct bpf_map *map) |
7879 | { | |
7880 | return (map->map_type == BPF_MAP_TYPE_CGROUP_STORAGE || | |
7881 | map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE); | |
7882 | } | |
7883 | ||
0246e64d AS |
7884 | /* look for pseudo eBPF instructions that access map FDs and |
7885 | * replace them with actual map pointers | |
7886 | */ | |
58e2af8b | 7887 | static int replace_map_fd_with_map_ptr(struct bpf_verifier_env *env) |
0246e64d AS |
7888 | { |
7889 | struct bpf_insn *insn = env->prog->insnsi; | |
7890 | int insn_cnt = env->prog->len; | |
fdc15d38 | 7891 | int i, j, err; |
0246e64d | 7892 | |
f1f7714e | 7893 | err = bpf_prog_calc_tag(env->prog); |
aafe6ae9 DB |
7894 | if (err) |
7895 | return err; | |
7896 | ||
0246e64d | 7897 | for (i = 0; i < insn_cnt; i++, insn++) { |
9bac3d6d | 7898 | if (BPF_CLASS(insn->code) == BPF_LDX && |
d691f9e8 | 7899 | (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) { |
61bd5218 | 7900 | verbose(env, "BPF_LDX uses reserved fields\n"); |
9bac3d6d AS |
7901 | return -EINVAL; |
7902 | } | |
7903 | ||
d691f9e8 AS |
7904 | if (BPF_CLASS(insn->code) == BPF_STX && |
7905 | ((BPF_MODE(insn->code) != BPF_MEM && | |
7906 | BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) { | |
61bd5218 | 7907 | verbose(env, "BPF_STX uses reserved fields\n"); |
d691f9e8 AS |
7908 | return -EINVAL; |
7909 | } | |
7910 | ||
0246e64d | 7911 | if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) { |
d8eca5bb | 7912 | struct bpf_insn_aux_data *aux; |
0246e64d AS |
7913 | struct bpf_map *map; |
7914 | struct fd f; | |
d8eca5bb | 7915 | u64 addr; |
0246e64d AS |
7916 | |
7917 | if (i == insn_cnt - 1 || insn[1].code != 0 || | |
7918 | insn[1].dst_reg != 0 || insn[1].src_reg != 0 || | |
7919 | insn[1].off != 0) { | |
61bd5218 | 7920 | verbose(env, "invalid bpf_ld_imm64 insn\n"); |
0246e64d AS |
7921 | return -EINVAL; |
7922 | } | |
7923 | ||
d8eca5bb | 7924 | if (insn[0].src_reg == 0) |
0246e64d AS |
7925 | /* valid generic load 64-bit imm */ |
7926 | goto next_insn; | |
7927 | ||
d8eca5bb DB |
7928 | /* In final convert_pseudo_ld_imm64() step, this is |
7929 | * converted into regular 64-bit imm load insn. | |
7930 | */ | |
7931 | if ((insn[0].src_reg != BPF_PSEUDO_MAP_FD && | |
7932 | insn[0].src_reg != BPF_PSEUDO_MAP_VALUE) || | |
7933 | (insn[0].src_reg == BPF_PSEUDO_MAP_FD && | |
7934 | insn[1].imm != 0)) { | |
7935 | verbose(env, | |
7936 | "unrecognized bpf_ld_imm64 insn\n"); | |
0246e64d AS |
7937 | return -EINVAL; |
7938 | } | |
7939 | ||
20182390 | 7940 | f = fdget(insn[0].imm); |
c2101297 | 7941 | map = __bpf_map_get(f); |
0246e64d | 7942 | if (IS_ERR(map)) { |
61bd5218 | 7943 | verbose(env, "fd %d is not pointing to valid bpf_map\n", |
20182390 | 7944 | insn[0].imm); |
0246e64d AS |
7945 | return PTR_ERR(map); |
7946 | } | |
7947 | ||
61bd5218 | 7948 | err = check_map_prog_compatibility(env, map, env->prog); |
fdc15d38 AS |
7949 | if (err) { |
7950 | fdput(f); | |
7951 | return err; | |
7952 | } | |
7953 | ||
d8eca5bb DB |
7954 | aux = &env->insn_aux_data[i]; |
7955 | if (insn->src_reg == BPF_PSEUDO_MAP_FD) { | |
7956 | addr = (unsigned long)map; | |
7957 | } else { | |
7958 | u32 off = insn[1].imm; | |
7959 | ||
7960 | if (off >= BPF_MAX_VAR_OFF) { | |
7961 | verbose(env, "direct value offset of %u is not allowed\n", off); | |
7962 | fdput(f); | |
7963 | return -EINVAL; | |
7964 | } | |
7965 | ||
7966 | if (!map->ops->map_direct_value_addr) { | |
7967 | verbose(env, "no direct value access support for this map type\n"); | |
7968 | fdput(f); | |
7969 | return -EINVAL; | |
7970 | } | |
7971 | ||
7972 | err = map->ops->map_direct_value_addr(map, &addr, off); | |
7973 | if (err) { | |
7974 | verbose(env, "invalid access to map value pointer, value_size=%u off=%u\n", | |
7975 | map->value_size, off); | |
7976 | fdput(f); | |
7977 | return err; | |
7978 | } | |
7979 | ||
7980 | aux->map_off = off; | |
7981 | addr += off; | |
7982 | } | |
7983 | ||
7984 | insn[0].imm = (u32)addr; | |
7985 | insn[1].imm = addr >> 32; | |
0246e64d AS |
7986 | |
7987 | /* check whether we recorded this map already */ | |
d8eca5bb | 7988 | for (j = 0; j < env->used_map_cnt; j++) { |
0246e64d | 7989 | if (env->used_maps[j] == map) { |
d8eca5bb | 7990 | aux->map_index = j; |
0246e64d AS |
7991 | fdput(f); |
7992 | goto next_insn; | |
7993 | } | |
d8eca5bb | 7994 | } |
0246e64d AS |
7995 | |
7996 | if (env->used_map_cnt >= MAX_USED_MAPS) { | |
7997 | fdput(f); | |
7998 | return -E2BIG; | |
7999 | } | |
8000 | ||
0246e64d AS |
8001 | /* hold the map. If the program is rejected by verifier, |
8002 | * the map will be released by release_maps() or it | |
8003 | * will be used by the valid program until it's unloaded | |
ab7f5bf0 | 8004 | * and all maps are released in free_used_maps() |
0246e64d | 8005 | */ |
92117d84 AS |
8006 | map = bpf_map_inc(map, false); |
8007 | if (IS_ERR(map)) { | |
8008 | fdput(f); | |
8009 | return PTR_ERR(map); | |
8010 | } | |
d8eca5bb DB |
8011 | |
8012 | aux->map_index = env->used_map_cnt; | |
92117d84 AS |
8013 | env->used_maps[env->used_map_cnt++] = map; |
8014 | ||
b741f163 | 8015 | if (bpf_map_is_cgroup_storage(map) && |
de9cbbaa | 8016 | bpf_cgroup_storage_assign(env->prog, map)) { |
b741f163 | 8017 | verbose(env, "only one cgroup storage of each type is allowed\n"); |
de9cbbaa RG |
8018 | fdput(f); |
8019 | return -EBUSY; | |
8020 | } | |
8021 | ||
0246e64d AS |
8022 | fdput(f); |
8023 | next_insn: | |
8024 | insn++; | |
8025 | i++; | |
5e581dad DB |
8026 | continue; |
8027 | } | |
8028 | ||
8029 | /* Basic sanity check before we invest more work here. */ | |
8030 | if (!bpf_opcode_in_insntable(insn->code)) { | |
8031 | verbose(env, "unknown opcode %02x\n", insn->code); | |
8032 | return -EINVAL; | |
0246e64d AS |
8033 | } |
8034 | } | |
8035 | ||
8036 | /* now all pseudo BPF_LD_IMM64 instructions load valid | |
8037 | * 'struct bpf_map *' into a register instead of user map_fd. | |
8038 | * These pointers will be used later by verifier to validate map access. | |
8039 | */ | |
8040 | return 0; | |
8041 | } | |
8042 | ||
8043 | /* drop refcnt of maps used by the rejected program */ | |
58e2af8b | 8044 | static void release_maps(struct bpf_verifier_env *env) |
0246e64d | 8045 | { |
8bad74f9 | 8046 | enum bpf_cgroup_storage_type stype; |
0246e64d AS |
8047 | int i; |
8048 | ||
8bad74f9 RG |
8049 | for_each_cgroup_storage_type(stype) { |
8050 | if (!env->prog->aux->cgroup_storage[stype]) | |
8051 | continue; | |
de9cbbaa | 8052 | bpf_cgroup_storage_release(env->prog, |
8bad74f9 RG |
8053 | env->prog->aux->cgroup_storage[stype]); |
8054 | } | |
de9cbbaa | 8055 | |
0246e64d AS |
8056 | for (i = 0; i < env->used_map_cnt; i++) |
8057 | bpf_map_put(env->used_maps[i]); | |
8058 | } | |
8059 | ||
8060 | /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */ | |
58e2af8b | 8061 | static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env) |
0246e64d AS |
8062 | { |
8063 | struct bpf_insn *insn = env->prog->insnsi; | |
8064 | int insn_cnt = env->prog->len; | |
8065 | int i; | |
8066 | ||
8067 | for (i = 0; i < insn_cnt; i++, insn++) | |
8068 | if (insn->code == (BPF_LD | BPF_IMM | BPF_DW)) | |
8069 | insn->src_reg = 0; | |
8070 | } | |
8071 | ||
8041902d AS |
8072 | /* single env->prog->insni[off] instruction was replaced with the range |
8073 | * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying | |
8074 | * [0, off) and [off, end) to new locations, so the patched range stays zero | |
8075 | */ | |
b325fbca JW |
8076 | static int adjust_insn_aux_data(struct bpf_verifier_env *env, |
8077 | struct bpf_prog *new_prog, u32 off, u32 cnt) | |
8041902d AS |
8078 | { |
8079 | struct bpf_insn_aux_data *new_data, *old_data = env->insn_aux_data; | |
b325fbca JW |
8080 | struct bpf_insn *insn = new_prog->insnsi; |
8081 | u32 prog_len; | |
c131187d | 8082 | int i; |
8041902d | 8083 | |
b325fbca JW |
8084 | /* aux info at OFF always needs adjustment, no matter fast path |
8085 | * (cnt == 1) is taken or not. There is no guarantee INSN at OFF is the | |
8086 | * original insn at old prog. | |
8087 | */ | |
8088 | old_data[off].zext_dst = insn_has_def32(env, insn + off + cnt - 1); | |
8089 | ||
8041902d AS |
8090 | if (cnt == 1) |
8091 | return 0; | |
b325fbca | 8092 | prog_len = new_prog->len; |
fad953ce KC |
8093 | new_data = vzalloc(array_size(prog_len, |
8094 | sizeof(struct bpf_insn_aux_data))); | |
8041902d AS |
8095 | if (!new_data) |
8096 | return -ENOMEM; | |
8097 | memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off); | |
8098 | memcpy(new_data + off + cnt - 1, old_data + off, | |
8099 | sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1)); | |
b325fbca | 8100 | for (i = off; i < off + cnt - 1; i++) { |
c131187d | 8101 | new_data[i].seen = true; |
b325fbca JW |
8102 | new_data[i].zext_dst = insn_has_def32(env, insn + i); |
8103 | } | |
8041902d AS |
8104 | env->insn_aux_data = new_data; |
8105 | vfree(old_data); | |
8106 | return 0; | |
8107 | } | |
8108 | ||
cc8b0b92 AS |
8109 | static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len) |
8110 | { | |
8111 | int i; | |
8112 | ||
8113 | if (len == 1) | |
8114 | return; | |
4cb3d99c JW |
8115 | /* NOTE: fake 'exit' subprog should be updated as well. */ |
8116 | for (i = 0; i <= env->subprog_cnt; i++) { | |
afd59424 | 8117 | if (env->subprog_info[i].start <= off) |
cc8b0b92 | 8118 | continue; |
9c8105bd | 8119 | env->subprog_info[i].start += len - 1; |
cc8b0b92 AS |
8120 | } |
8121 | } | |
8122 | ||
8041902d AS |
8123 | static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off, |
8124 | const struct bpf_insn *patch, u32 len) | |
8125 | { | |
8126 | struct bpf_prog *new_prog; | |
8127 | ||
8128 | new_prog = bpf_patch_insn_single(env->prog, off, patch, len); | |
4f73379e AS |
8129 | if (IS_ERR(new_prog)) { |
8130 | if (PTR_ERR(new_prog) == -ERANGE) | |
8131 | verbose(env, | |
8132 | "insn %d cannot be patched due to 16-bit range\n", | |
8133 | env->insn_aux_data[off].orig_idx); | |
8041902d | 8134 | return NULL; |
4f73379e | 8135 | } |
b325fbca | 8136 | if (adjust_insn_aux_data(env, new_prog, off, len)) |
8041902d | 8137 | return NULL; |
cc8b0b92 | 8138 | adjust_subprog_starts(env, off, len); |
8041902d AS |
8139 | return new_prog; |
8140 | } | |
8141 | ||
52875a04 JK |
8142 | static int adjust_subprog_starts_after_remove(struct bpf_verifier_env *env, |
8143 | u32 off, u32 cnt) | |
8144 | { | |
8145 | int i, j; | |
8146 | ||
8147 | /* find first prog starting at or after off (first to remove) */ | |
8148 | for (i = 0; i < env->subprog_cnt; i++) | |
8149 | if (env->subprog_info[i].start >= off) | |
8150 | break; | |
8151 | /* find first prog starting at or after off + cnt (first to stay) */ | |
8152 | for (j = i; j < env->subprog_cnt; j++) | |
8153 | if (env->subprog_info[j].start >= off + cnt) | |
8154 | break; | |
8155 | /* if j doesn't start exactly at off + cnt, we are just removing | |
8156 | * the front of previous prog | |
8157 | */ | |
8158 | if (env->subprog_info[j].start != off + cnt) | |
8159 | j--; | |
8160 | ||
8161 | if (j > i) { | |
8162 | struct bpf_prog_aux *aux = env->prog->aux; | |
8163 | int move; | |
8164 | ||
8165 | /* move fake 'exit' subprog as well */ | |
8166 | move = env->subprog_cnt + 1 - j; | |
8167 | ||
8168 | memmove(env->subprog_info + i, | |
8169 | env->subprog_info + j, | |
8170 | sizeof(*env->subprog_info) * move); | |
8171 | env->subprog_cnt -= j - i; | |
8172 | ||
8173 | /* remove func_info */ | |
8174 | if (aux->func_info) { | |
8175 | move = aux->func_info_cnt - j; | |
8176 | ||
8177 | memmove(aux->func_info + i, | |
8178 | aux->func_info + j, | |
8179 | sizeof(*aux->func_info) * move); | |
8180 | aux->func_info_cnt -= j - i; | |
8181 | /* func_info->insn_off is set after all code rewrites, | |
8182 | * in adjust_btf_func() - no need to adjust | |
8183 | */ | |
8184 | } | |
8185 | } else { | |
8186 | /* convert i from "first prog to remove" to "first to adjust" */ | |
8187 | if (env->subprog_info[i].start == off) | |
8188 | i++; | |
8189 | } | |
8190 | ||
8191 | /* update fake 'exit' subprog as well */ | |
8192 | for (; i <= env->subprog_cnt; i++) | |
8193 | env->subprog_info[i].start -= cnt; | |
8194 | ||
8195 | return 0; | |
8196 | } | |
8197 | ||
8198 | static int bpf_adj_linfo_after_remove(struct bpf_verifier_env *env, u32 off, | |
8199 | u32 cnt) | |
8200 | { | |
8201 | struct bpf_prog *prog = env->prog; | |
8202 | u32 i, l_off, l_cnt, nr_linfo; | |
8203 | struct bpf_line_info *linfo; | |
8204 | ||
8205 | nr_linfo = prog->aux->nr_linfo; | |
8206 | if (!nr_linfo) | |
8207 | return 0; | |
8208 | ||
8209 | linfo = prog->aux->linfo; | |
8210 | ||
8211 | /* find first line info to remove, count lines to be removed */ | |
8212 | for (i = 0; i < nr_linfo; i++) | |
8213 | if (linfo[i].insn_off >= off) | |
8214 | break; | |
8215 | ||
8216 | l_off = i; | |
8217 | l_cnt = 0; | |
8218 | for (; i < nr_linfo; i++) | |
8219 | if (linfo[i].insn_off < off + cnt) | |
8220 | l_cnt++; | |
8221 | else | |
8222 | break; | |
8223 | ||
8224 | /* First live insn doesn't match first live linfo, it needs to "inherit" | |
8225 | * last removed linfo. prog is already modified, so prog->len == off | |
8226 | * means no live instructions after (tail of the program was removed). | |
8227 | */ | |
8228 | if (prog->len != off && l_cnt && | |
8229 | (i == nr_linfo || linfo[i].insn_off != off + cnt)) { | |
8230 | l_cnt--; | |
8231 | linfo[--i].insn_off = off + cnt; | |
8232 | } | |
8233 | ||
8234 | /* remove the line info which refer to the removed instructions */ | |
8235 | if (l_cnt) { | |
8236 | memmove(linfo + l_off, linfo + i, | |
8237 | sizeof(*linfo) * (nr_linfo - i)); | |
8238 | ||
8239 | prog->aux->nr_linfo -= l_cnt; | |
8240 | nr_linfo = prog->aux->nr_linfo; | |
8241 | } | |
8242 | ||
8243 | /* pull all linfo[i].insn_off >= off + cnt in by cnt */ | |
8244 | for (i = l_off; i < nr_linfo; i++) | |
8245 | linfo[i].insn_off -= cnt; | |
8246 | ||
8247 | /* fix up all subprogs (incl. 'exit') which start >= off */ | |
8248 | for (i = 0; i <= env->subprog_cnt; i++) | |
8249 | if (env->subprog_info[i].linfo_idx > l_off) { | |
8250 | /* program may have started in the removed region but | |
8251 | * may not be fully removed | |
8252 | */ | |
8253 | if (env->subprog_info[i].linfo_idx >= l_off + l_cnt) | |
8254 | env->subprog_info[i].linfo_idx -= l_cnt; | |
8255 | else | |
8256 | env->subprog_info[i].linfo_idx = l_off; | |
8257 | } | |
8258 | ||
8259 | return 0; | |
8260 | } | |
8261 | ||
8262 | static int verifier_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt) | |
8263 | { | |
8264 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
8265 | unsigned int orig_prog_len = env->prog->len; | |
8266 | int err; | |
8267 | ||
08ca90af JK |
8268 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
8269 | bpf_prog_offload_remove_insns(env, off, cnt); | |
8270 | ||
52875a04 JK |
8271 | err = bpf_remove_insns(env->prog, off, cnt); |
8272 | if (err) | |
8273 | return err; | |
8274 | ||
8275 | err = adjust_subprog_starts_after_remove(env, off, cnt); | |
8276 | if (err) | |
8277 | return err; | |
8278 | ||
8279 | err = bpf_adj_linfo_after_remove(env, off, cnt); | |
8280 | if (err) | |
8281 | return err; | |
8282 | ||
8283 | memmove(aux_data + off, aux_data + off + cnt, | |
8284 | sizeof(*aux_data) * (orig_prog_len - off - cnt)); | |
8285 | ||
8286 | return 0; | |
8287 | } | |
8288 | ||
2a5418a1 DB |
8289 | /* The verifier does more data flow analysis than llvm and will not |
8290 | * explore branches that are dead at run time. Malicious programs can | |
8291 | * have dead code too. Therefore replace all dead at-run-time code | |
8292 | * with 'ja -1'. | |
8293 | * | |
8294 | * Just nops are not optimal, e.g. if they would sit at the end of the | |
8295 | * program and through another bug we would manage to jump there, then | |
8296 | * we'd execute beyond program memory otherwise. Returning exception | |
8297 | * code also wouldn't work since we can have subprogs where the dead | |
8298 | * code could be located. | |
c131187d AS |
8299 | */ |
8300 | static void sanitize_dead_code(struct bpf_verifier_env *env) | |
8301 | { | |
8302 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
2a5418a1 | 8303 | struct bpf_insn trap = BPF_JMP_IMM(BPF_JA, 0, 0, -1); |
c131187d AS |
8304 | struct bpf_insn *insn = env->prog->insnsi; |
8305 | const int insn_cnt = env->prog->len; | |
8306 | int i; | |
8307 | ||
8308 | for (i = 0; i < insn_cnt; i++) { | |
8309 | if (aux_data[i].seen) | |
8310 | continue; | |
2a5418a1 | 8311 | memcpy(insn + i, &trap, sizeof(trap)); |
c131187d AS |
8312 | } |
8313 | } | |
8314 | ||
e2ae4ca2 JK |
8315 | static bool insn_is_cond_jump(u8 code) |
8316 | { | |
8317 | u8 op; | |
8318 | ||
092ed096 JW |
8319 | if (BPF_CLASS(code) == BPF_JMP32) |
8320 | return true; | |
8321 | ||
e2ae4ca2 JK |
8322 | if (BPF_CLASS(code) != BPF_JMP) |
8323 | return false; | |
8324 | ||
8325 | op = BPF_OP(code); | |
8326 | return op != BPF_JA && op != BPF_EXIT && op != BPF_CALL; | |
8327 | } | |
8328 | ||
8329 | static void opt_hard_wire_dead_code_branches(struct bpf_verifier_env *env) | |
8330 | { | |
8331 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
8332 | struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); | |
8333 | struct bpf_insn *insn = env->prog->insnsi; | |
8334 | const int insn_cnt = env->prog->len; | |
8335 | int i; | |
8336 | ||
8337 | for (i = 0; i < insn_cnt; i++, insn++) { | |
8338 | if (!insn_is_cond_jump(insn->code)) | |
8339 | continue; | |
8340 | ||
8341 | if (!aux_data[i + 1].seen) | |
8342 | ja.off = insn->off; | |
8343 | else if (!aux_data[i + 1 + insn->off].seen) | |
8344 | ja.off = 0; | |
8345 | else | |
8346 | continue; | |
8347 | ||
08ca90af JK |
8348 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
8349 | bpf_prog_offload_replace_insn(env, i, &ja); | |
8350 | ||
e2ae4ca2 JK |
8351 | memcpy(insn, &ja, sizeof(ja)); |
8352 | } | |
8353 | } | |
8354 | ||
52875a04 JK |
8355 | static int opt_remove_dead_code(struct bpf_verifier_env *env) |
8356 | { | |
8357 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
8358 | int insn_cnt = env->prog->len; | |
8359 | int i, err; | |
8360 | ||
8361 | for (i = 0; i < insn_cnt; i++) { | |
8362 | int j; | |
8363 | ||
8364 | j = 0; | |
8365 | while (i + j < insn_cnt && !aux_data[i + j].seen) | |
8366 | j++; | |
8367 | if (!j) | |
8368 | continue; | |
8369 | ||
8370 | err = verifier_remove_insns(env, i, j); | |
8371 | if (err) | |
8372 | return err; | |
8373 | insn_cnt = env->prog->len; | |
8374 | } | |
8375 | ||
8376 | return 0; | |
8377 | } | |
8378 | ||
a1b14abc JK |
8379 | static int opt_remove_nops(struct bpf_verifier_env *env) |
8380 | { | |
8381 | const struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); | |
8382 | struct bpf_insn *insn = env->prog->insnsi; | |
8383 | int insn_cnt = env->prog->len; | |
8384 | int i, err; | |
8385 | ||
8386 | for (i = 0; i < insn_cnt; i++) { | |
8387 | if (memcmp(&insn[i], &ja, sizeof(ja))) | |
8388 | continue; | |
8389 | ||
8390 | err = verifier_remove_insns(env, i, 1); | |
8391 | if (err) | |
8392 | return err; | |
8393 | insn_cnt--; | |
8394 | i--; | |
8395 | } | |
8396 | ||
8397 | return 0; | |
8398 | } | |
8399 | ||
d6c2308c JW |
8400 | static int opt_subreg_zext_lo32_rnd_hi32(struct bpf_verifier_env *env, |
8401 | const union bpf_attr *attr) | |
a4b1d3c1 | 8402 | { |
d6c2308c | 8403 | struct bpf_insn *patch, zext_patch[2], rnd_hi32_patch[4]; |
a4b1d3c1 | 8404 | struct bpf_insn_aux_data *aux = env->insn_aux_data; |
d6c2308c | 8405 | int i, patch_len, delta = 0, len = env->prog->len; |
a4b1d3c1 | 8406 | struct bpf_insn *insns = env->prog->insnsi; |
a4b1d3c1 | 8407 | struct bpf_prog *new_prog; |
d6c2308c | 8408 | bool rnd_hi32; |
a4b1d3c1 | 8409 | |
d6c2308c | 8410 | rnd_hi32 = attr->prog_flags & BPF_F_TEST_RND_HI32; |
a4b1d3c1 | 8411 | zext_patch[1] = BPF_ZEXT_REG(0); |
d6c2308c JW |
8412 | rnd_hi32_patch[1] = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, 0); |
8413 | rnd_hi32_patch[2] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32); | |
8414 | rnd_hi32_patch[3] = BPF_ALU64_REG(BPF_OR, 0, BPF_REG_AX); | |
a4b1d3c1 JW |
8415 | for (i = 0; i < len; i++) { |
8416 | int adj_idx = i + delta; | |
8417 | struct bpf_insn insn; | |
8418 | ||
d6c2308c JW |
8419 | insn = insns[adj_idx]; |
8420 | if (!aux[adj_idx].zext_dst) { | |
8421 | u8 code, class; | |
8422 | u32 imm_rnd; | |
8423 | ||
8424 | if (!rnd_hi32) | |
8425 | continue; | |
8426 | ||
8427 | code = insn.code; | |
8428 | class = BPF_CLASS(code); | |
8429 | if (insn_no_def(&insn)) | |
8430 | continue; | |
8431 | ||
8432 | /* NOTE: arg "reg" (the fourth one) is only used for | |
8433 | * BPF_STX which has been ruled out in above | |
8434 | * check, it is safe to pass NULL here. | |
8435 | */ | |
8436 | if (is_reg64(env, &insn, insn.dst_reg, NULL, DST_OP)) { | |
8437 | if (class == BPF_LD && | |
8438 | BPF_MODE(code) == BPF_IMM) | |
8439 | i++; | |
8440 | continue; | |
8441 | } | |
8442 | ||
8443 | /* ctx load could be transformed into wider load. */ | |
8444 | if (class == BPF_LDX && | |
8445 | aux[adj_idx].ptr_type == PTR_TO_CTX) | |
8446 | continue; | |
8447 | ||
8448 | imm_rnd = get_random_int(); | |
8449 | rnd_hi32_patch[0] = insn; | |
8450 | rnd_hi32_patch[1].imm = imm_rnd; | |
8451 | rnd_hi32_patch[3].dst_reg = insn.dst_reg; | |
8452 | patch = rnd_hi32_patch; | |
8453 | patch_len = 4; | |
8454 | goto apply_patch_buffer; | |
8455 | } | |
8456 | ||
8457 | if (!bpf_jit_needs_zext()) | |
a4b1d3c1 JW |
8458 | continue; |
8459 | ||
a4b1d3c1 JW |
8460 | zext_patch[0] = insn; |
8461 | zext_patch[1].dst_reg = insn.dst_reg; | |
8462 | zext_patch[1].src_reg = insn.dst_reg; | |
d6c2308c JW |
8463 | patch = zext_patch; |
8464 | patch_len = 2; | |
8465 | apply_patch_buffer: | |
8466 | new_prog = bpf_patch_insn_data(env, adj_idx, patch, patch_len); | |
a4b1d3c1 JW |
8467 | if (!new_prog) |
8468 | return -ENOMEM; | |
8469 | env->prog = new_prog; | |
8470 | insns = new_prog->insnsi; | |
8471 | aux = env->insn_aux_data; | |
d6c2308c | 8472 | delta += patch_len - 1; |
a4b1d3c1 JW |
8473 | } |
8474 | ||
8475 | return 0; | |
8476 | } | |
8477 | ||
c64b7983 JS |
8478 | /* convert load instructions that access fields of a context type into a |
8479 | * sequence of instructions that access fields of the underlying structure: | |
8480 | * struct __sk_buff -> struct sk_buff | |
8481 | * struct bpf_sock_ops -> struct sock | |
9bac3d6d | 8482 | */ |
58e2af8b | 8483 | static int convert_ctx_accesses(struct bpf_verifier_env *env) |
9bac3d6d | 8484 | { |
00176a34 | 8485 | const struct bpf_verifier_ops *ops = env->ops; |
f96da094 | 8486 | int i, cnt, size, ctx_field_size, delta = 0; |
3df126f3 | 8487 | const int insn_cnt = env->prog->len; |
36bbef52 | 8488 | struct bpf_insn insn_buf[16], *insn; |
46f53a65 | 8489 | u32 target_size, size_default, off; |
9bac3d6d | 8490 | struct bpf_prog *new_prog; |
d691f9e8 | 8491 | enum bpf_access_type type; |
f96da094 | 8492 | bool is_narrower_load; |
9bac3d6d | 8493 | |
b09928b9 DB |
8494 | if (ops->gen_prologue || env->seen_direct_write) { |
8495 | if (!ops->gen_prologue) { | |
8496 | verbose(env, "bpf verifier is misconfigured\n"); | |
8497 | return -EINVAL; | |
8498 | } | |
36bbef52 DB |
8499 | cnt = ops->gen_prologue(insn_buf, env->seen_direct_write, |
8500 | env->prog); | |
8501 | if (cnt >= ARRAY_SIZE(insn_buf)) { | |
61bd5218 | 8502 | verbose(env, "bpf verifier is misconfigured\n"); |
36bbef52 DB |
8503 | return -EINVAL; |
8504 | } else if (cnt) { | |
8041902d | 8505 | new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt); |
36bbef52 DB |
8506 | if (!new_prog) |
8507 | return -ENOMEM; | |
8041902d | 8508 | |
36bbef52 | 8509 | env->prog = new_prog; |
3df126f3 | 8510 | delta += cnt - 1; |
36bbef52 DB |
8511 | } |
8512 | } | |
8513 | ||
c64b7983 | 8514 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
9bac3d6d AS |
8515 | return 0; |
8516 | ||
3df126f3 | 8517 | insn = env->prog->insnsi + delta; |
36bbef52 | 8518 | |
9bac3d6d | 8519 | for (i = 0; i < insn_cnt; i++, insn++) { |
c64b7983 JS |
8520 | bpf_convert_ctx_access_t convert_ctx_access; |
8521 | ||
62c7989b DB |
8522 | if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) || |
8523 | insn->code == (BPF_LDX | BPF_MEM | BPF_H) || | |
8524 | insn->code == (BPF_LDX | BPF_MEM | BPF_W) || | |
ea2e7ce5 | 8525 | insn->code == (BPF_LDX | BPF_MEM | BPF_DW)) |
d691f9e8 | 8526 | type = BPF_READ; |
62c7989b DB |
8527 | else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) || |
8528 | insn->code == (BPF_STX | BPF_MEM | BPF_H) || | |
8529 | insn->code == (BPF_STX | BPF_MEM | BPF_W) || | |
ea2e7ce5 | 8530 | insn->code == (BPF_STX | BPF_MEM | BPF_DW)) |
d691f9e8 AS |
8531 | type = BPF_WRITE; |
8532 | else | |
9bac3d6d AS |
8533 | continue; |
8534 | ||
af86ca4e AS |
8535 | if (type == BPF_WRITE && |
8536 | env->insn_aux_data[i + delta].sanitize_stack_off) { | |
8537 | struct bpf_insn patch[] = { | |
8538 | /* Sanitize suspicious stack slot with zero. | |
8539 | * There are no memory dependencies for this store, | |
8540 | * since it's only using frame pointer and immediate | |
8541 | * constant of zero | |
8542 | */ | |
8543 | BPF_ST_MEM(BPF_DW, BPF_REG_FP, | |
8544 | env->insn_aux_data[i + delta].sanitize_stack_off, | |
8545 | 0), | |
8546 | /* the original STX instruction will immediately | |
8547 | * overwrite the same stack slot with appropriate value | |
8548 | */ | |
8549 | *insn, | |
8550 | }; | |
8551 | ||
8552 | cnt = ARRAY_SIZE(patch); | |
8553 | new_prog = bpf_patch_insn_data(env, i + delta, patch, cnt); | |
8554 | if (!new_prog) | |
8555 | return -ENOMEM; | |
8556 | ||
8557 | delta += cnt - 1; | |
8558 | env->prog = new_prog; | |
8559 | insn = new_prog->insnsi + i + delta; | |
8560 | continue; | |
8561 | } | |
8562 | ||
c64b7983 JS |
8563 | switch (env->insn_aux_data[i + delta].ptr_type) { |
8564 | case PTR_TO_CTX: | |
8565 | if (!ops->convert_ctx_access) | |
8566 | continue; | |
8567 | convert_ctx_access = ops->convert_ctx_access; | |
8568 | break; | |
8569 | case PTR_TO_SOCKET: | |
46f8bc92 | 8570 | case PTR_TO_SOCK_COMMON: |
c64b7983 JS |
8571 | convert_ctx_access = bpf_sock_convert_ctx_access; |
8572 | break; | |
655a51e5 MKL |
8573 | case PTR_TO_TCP_SOCK: |
8574 | convert_ctx_access = bpf_tcp_sock_convert_ctx_access; | |
8575 | break; | |
fada7fdc JL |
8576 | case PTR_TO_XDP_SOCK: |
8577 | convert_ctx_access = bpf_xdp_sock_convert_ctx_access; | |
8578 | break; | |
c64b7983 | 8579 | default: |
9bac3d6d | 8580 | continue; |
c64b7983 | 8581 | } |
9bac3d6d | 8582 | |
31fd8581 | 8583 | ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size; |
f96da094 | 8584 | size = BPF_LDST_BYTES(insn); |
31fd8581 YS |
8585 | |
8586 | /* If the read access is a narrower load of the field, | |
8587 | * convert to a 4/8-byte load, to minimum program type specific | |
8588 | * convert_ctx_access changes. If conversion is successful, | |
8589 | * we will apply proper mask to the result. | |
8590 | */ | |
f96da094 | 8591 | is_narrower_load = size < ctx_field_size; |
46f53a65 AI |
8592 | size_default = bpf_ctx_off_adjust_machine(ctx_field_size); |
8593 | off = insn->off; | |
31fd8581 | 8594 | if (is_narrower_load) { |
f96da094 DB |
8595 | u8 size_code; |
8596 | ||
8597 | if (type == BPF_WRITE) { | |
61bd5218 | 8598 | verbose(env, "bpf verifier narrow ctx access misconfigured\n"); |
f96da094 DB |
8599 | return -EINVAL; |
8600 | } | |
31fd8581 | 8601 | |
f96da094 | 8602 | size_code = BPF_H; |
31fd8581 YS |
8603 | if (ctx_field_size == 4) |
8604 | size_code = BPF_W; | |
8605 | else if (ctx_field_size == 8) | |
8606 | size_code = BPF_DW; | |
f96da094 | 8607 | |
bc23105c | 8608 | insn->off = off & ~(size_default - 1); |
31fd8581 YS |
8609 | insn->code = BPF_LDX | BPF_MEM | size_code; |
8610 | } | |
f96da094 DB |
8611 | |
8612 | target_size = 0; | |
c64b7983 JS |
8613 | cnt = convert_ctx_access(type, insn, insn_buf, env->prog, |
8614 | &target_size); | |
f96da094 DB |
8615 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) || |
8616 | (ctx_field_size && !target_size)) { | |
61bd5218 | 8617 | verbose(env, "bpf verifier is misconfigured\n"); |
9bac3d6d AS |
8618 | return -EINVAL; |
8619 | } | |
f96da094 DB |
8620 | |
8621 | if (is_narrower_load && size < target_size) { | |
d9b8aada IL |
8622 | u8 shift = bpf_ctx_narrow_load_shift(off, size, |
8623 | size_default); | |
46f53a65 AI |
8624 | if (ctx_field_size <= 4) { |
8625 | if (shift) | |
8626 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_RSH, | |
8627 | insn->dst_reg, | |
8628 | shift); | |
31fd8581 | 8629 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg, |
f96da094 | 8630 | (1 << size * 8) - 1); |
46f53a65 AI |
8631 | } else { |
8632 | if (shift) | |
8633 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_RSH, | |
8634 | insn->dst_reg, | |
8635 | shift); | |
31fd8581 | 8636 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg, |
e2f7fc0a | 8637 | (1ULL << size * 8) - 1); |
46f53a65 | 8638 | } |
31fd8581 | 8639 | } |
9bac3d6d | 8640 | |
8041902d | 8641 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
9bac3d6d AS |
8642 | if (!new_prog) |
8643 | return -ENOMEM; | |
8644 | ||
3df126f3 | 8645 | delta += cnt - 1; |
9bac3d6d AS |
8646 | |
8647 | /* keep walking new program and skip insns we just inserted */ | |
8648 | env->prog = new_prog; | |
3df126f3 | 8649 | insn = new_prog->insnsi + i + delta; |
9bac3d6d AS |
8650 | } |
8651 | ||
8652 | return 0; | |
8653 | } | |
8654 | ||
1c2a088a AS |
8655 | static int jit_subprogs(struct bpf_verifier_env *env) |
8656 | { | |
8657 | struct bpf_prog *prog = env->prog, **func, *tmp; | |
8658 | int i, j, subprog_start, subprog_end = 0, len, subprog; | |
7105e828 | 8659 | struct bpf_insn *insn; |
1c2a088a | 8660 | void *old_bpf_func; |
c454a46b | 8661 | int err; |
1c2a088a | 8662 | |
f910cefa | 8663 | if (env->subprog_cnt <= 1) |
1c2a088a AS |
8664 | return 0; |
8665 | ||
7105e828 | 8666 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
1c2a088a AS |
8667 | if (insn->code != (BPF_JMP | BPF_CALL) || |
8668 | insn->src_reg != BPF_PSEUDO_CALL) | |
8669 | continue; | |
c7a89784 DB |
8670 | /* Upon error here we cannot fall back to interpreter but |
8671 | * need a hard reject of the program. Thus -EFAULT is | |
8672 | * propagated in any case. | |
8673 | */ | |
1c2a088a AS |
8674 | subprog = find_subprog(env, i + insn->imm + 1); |
8675 | if (subprog < 0) { | |
8676 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", | |
8677 | i + insn->imm + 1); | |
8678 | return -EFAULT; | |
8679 | } | |
8680 | /* temporarily remember subprog id inside insn instead of | |
8681 | * aux_data, since next loop will split up all insns into funcs | |
8682 | */ | |
f910cefa | 8683 | insn->off = subprog; |
1c2a088a AS |
8684 | /* remember original imm in case JIT fails and fallback |
8685 | * to interpreter will be needed | |
8686 | */ | |
8687 | env->insn_aux_data[i].call_imm = insn->imm; | |
8688 | /* point imm to __bpf_call_base+1 from JITs point of view */ | |
8689 | insn->imm = 1; | |
8690 | } | |
8691 | ||
c454a46b MKL |
8692 | err = bpf_prog_alloc_jited_linfo(prog); |
8693 | if (err) | |
8694 | goto out_undo_insn; | |
8695 | ||
8696 | err = -ENOMEM; | |
6396bb22 | 8697 | func = kcalloc(env->subprog_cnt, sizeof(prog), GFP_KERNEL); |
1c2a088a | 8698 | if (!func) |
c7a89784 | 8699 | goto out_undo_insn; |
1c2a088a | 8700 | |
f910cefa | 8701 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a | 8702 | subprog_start = subprog_end; |
4cb3d99c | 8703 | subprog_end = env->subprog_info[i + 1].start; |
1c2a088a AS |
8704 | |
8705 | len = subprog_end - subprog_start; | |
492ecee8 AS |
8706 | /* BPF_PROG_RUN doesn't call subprogs directly, |
8707 | * hence main prog stats include the runtime of subprogs. | |
8708 | * subprogs don't have IDs and not reachable via prog_get_next_id | |
8709 | * func[i]->aux->stats will never be accessed and stays NULL | |
8710 | */ | |
8711 | func[i] = bpf_prog_alloc_no_stats(bpf_prog_size(len), GFP_USER); | |
1c2a088a AS |
8712 | if (!func[i]) |
8713 | goto out_free; | |
8714 | memcpy(func[i]->insnsi, &prog->insnsi[subprog_start], | |
8715 | len * sizeof(struct bpf_insn)); | |
4f74d809 | 8716 | func[i]->type = prog->type; |
1c2a088a | 8717 | func[i]->len = len; |
4f74d809 DB |
8718 | if (bpf_prog_calc_tag(func[i])) |
8719 | goto out_free; | |
1c2a088a | 8720 | func[i]->is_func = 1; |
ba64e7d8 YS |
8721 | func[i]->aux->func_idx = i; |
8722 | /* the btf and func_info will be freed only at prog->aux */ | |
8723 | func[i]->aux->btf = prog->aux->btf; | |
8724 | func[i]->aux->func_info = prog->aux->func_info; | |
8725 | ||
1c2a088a AS |
8726 | /* Use bpf_prog_F_tag to indicate functions in stack traces. |
8727 | * Long term would need debug info to populate names | |
8728 | */ | |
8729 | func[i]->aux->name[0] = 'F'; | |
9c8105bd | 8730 | func[i]->aux->stack_depth = env->subprog_info[i].stack_depth; |
1c2a088a | 8731 | func[i]->jit_requested = 1; |
c454a46b MKL |
8732 | func[i]->aux->linfo = prog->aux->linfo; |
8733 | func[i]->aux->nr_linfo = prog->aux->nr_linfo; | |
8734 | func[i]->aux->jited_linfo = prog->aux->jited_linfo; | |
8735 | func[i]->aux->linfo_idx = env->subprog_info[i].linfo_idx; | |
1c2a088a AS |
8736 | func[i] = bpf_int_jit_compile(func[i]); |
8737 | if (!func[i]->jited) { | |
8738 | err = -ENOTSUPP; | |
8739 | goto out_free; | |
8740 | } | |
8741 | cond_resched(); | |
8742 | } | |
8743 | /* at this point all bpf functions were successfully JITed | |
8744 | * now populate all bpf_calls with correct addresses and | |
8745 | * run last pass of JIT | |
8746 | */ | |
f910cefa | 8747 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a AS |
8748 | insn = func[i]->insnsi; |
8749 | for (j = 0; j < func[i]->len; j++, insn++) { | |
8750 | if (insn->code != (BPF_JMP | BPF_CALL) || | |
8751 | insn->src_reg != BPF_PSEUDO_CALL) | |
8752 | continue; | |
8753 | subprog = insn->off; | |
0d306c31 PB |
8754 | insn->imm = BPF_CAST_CALL(func[subprog]->bpf_func) - |
8755 | __bpf_call_base; | |
1c2a088a | 8756 | } |
2162fed4 SD |
8757 | |
8758 | /* we use the aux data to keep a list of the start addresses | |
8759 | * of the JITed images for each function in the program | |
8760 | * | |
8761 | * for some architectures, such as powerpc64, the imm field | |
8762 | * might not be large enough to hold the offset of the start | |
8763 | * address of the callee's JITed image from __bpf_call_base | |
8764 | * | |
8765 | * in such cases, we can lookup the start address of a callee | |
8766 | * by using its subprog id, available from the off field of | |
8767 | * the call instruction, as an index for this list | |
8768 | */ | |
8769 | func[i]->aux->func = func; | |
8770 | func[i]->aux->func_cnt = env->subprog_cnt; | |
1c2a088a | 8771 | } |
f910cefa | 8772 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a AS |
8773 | old_bpf_func = func[i]->bpf_func; |
8774 | tmp = bpf_int_jit_compile(func[i]); | |
8775 | if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) { | |
8776 | verbose(env, "JIT doesn't support bpf-to-bpf calls\n"); | |
c7a89784 | 8777 | err = -ENOTSUPP; |
1c2a088a AS |
8778 | goto out_free; |
8779 | } | |
8780 | cond_resched(); | |
8781 | } | |
8782 | ||
8783 | /* finally lock prog and jit images for all functions and | |
8784 | * populate kallsysm | |
8785 | */ | |
f910cefa | 8786 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a AS |
8787 | bpf_prog_lock_ro(func[i]); |
8788 | bpf_prog_kallsyms_add(func[i]); | |
8789 | } | |
7105e828 DB |
8790 | |
8791 | /* Last step: make now unused interpreter insns from main | |
8792 | * prog consistent for later dump requests, so they can | |
8793 | * later look the same as if they were interpreted only. | |
8794 | */ | |
8795 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { | |
7105e828 DB |
8796 | if (insn->code != (BPF_JMP | BPF_CALL) || |
8797 | insn->src_reg != BPF_PSEUDO_CALL) | |
8798 | continue; | |
8799 | insn->off = env->insn_aux_data[i].call_imm; | |
8800 | subprog = find_subprog(env, i + insn->off + 1); | |
dbecd738 | 8801 | insn->imm = subprog; |
7105e828 DB |
8802 | } |
8803 | ||
1c2a088a AS |
8804 | prog->jited = 1; |
8805 | prog->bpf_func = func[0]->bpf_func; | |
8806 | prog->aux->func = func; | |
f910cefa | 8807 | prog->aux->func_cnt = env->subprog_cnt; |
c454a46b | 8808 | bpf_prog_free_unused_jited_linfo(prog); |
1c2a088a AS |
8809 | return 0; |
8810 | out_free: | |
f910cefa | 8811 | for (i = 0; i < env->subprog_cnt; i++) |
1c2a088a AS |
8812 | if (func[i]) |
8813 | bpf_jit_free(func[i]); | |
8814 | kfree(func); | |
c7a89784 | 8815 | out_undo_insn: |
1c2a088a AS |
8816 | /* cleanup main prog to be interpreted */ |
8817 | prog->jit_requested = 0; | |
8818 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { | |
8819 | if (insn->code != (BPF_JMP | BPF_CALL) || | |
8820 | insn->src_reg != BPF_PSEUDO_CALL) | |
8821 | continue; | |
8822 | insn->off = 0; | |
8823 | insn->imm = env->insn_aux_data[i].call_imm; | |
8824 | } | |
c454a46b | 8825 | bpf_prog_free_jited_linfo(prog); |
1c2a088a AS |
8826 | return err; |
8827 | } | |
8828 | ||
1ea47e01 AS |
8829 | static int fixup_call_args(struct bpf_verifier_env *env) |
8830 | { | |
19d28fbd | 8831 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
1ea47e01 AS |
8832 | struct bpf_prog *prog = env->prog; |
8833 | struct bpf_insn *insn = prog->insnsi; | |
8834 | int i, depth; | |
19d28fbd | 8835 | #endif |
e4052d06 | 8836 | int err = 0; |
1ea47e01 | 8837 | |
e4052d06 QM |
8838 | if (env->prog->jit_requested && |
8839 | !bpf_prog_is_dev_bound(env->prog->aux)) { | |
19d28fbd DM |
8840 | err = jit_subprogs(env); |
8841 | if (err == 0) | |
1c2a088a | 8842 | return 0; |
c7a89784 DB |
8843 | if (err == -EFAULT) |
8844 | return err; | |
19d28fbd DM |
8845 | } |
8846 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON | |
1ea47e01 AS |
8847 | for (i = 0; i < prog->len; i++, insn++) { |
8848 | if (insn->code != (BPF_JMP | BPF_CALL) || | |
8849 | insn->src_reg != BPF_PSEUDO_CALL) | |
8850 | continue; | |
8851 | depth = get_callee_stack_depth(env, insn, i); | |
8852 | if (depth < 0) | |
8853 | return depth; | |
8854 | bpf_patch_call_args(insn, depth); | |
8855 | } | |
19d28fbd DM |
8856 | err = 0; |
8857 | #endif | |
8858 | return err; | |
1ea47e01 AS |
8859 | } |
8860 | ||
79741b3b | 8861 | /* fixup insn->imm field of bpf_call instructions |
81ed18ab | 8862 | * and inline eligible helpers as explicit sequence of BPF instructions |
e245c5c6 AS |
8863 | * |
8864 | * this function is called after eBPF program passed verification | |
8865 | */ | |
79741b3b | 8866 | static int fixup_bpf_calls(struct bpf_verifier_env *env) |
e245c5c6 | 8867 | { |
79741b3b AS |
8868 | struct bpf_prog *prog = env->prog; |
8869 | struct bpf_insn *insn = prog->insnsi; | |
e245c5c6 | 8870 | const struct bpf_func_proto *fn; |
79741b3b | 8871 | const int insn_cnt = prog->len; |
09772d92 | 8872 | const struct bpf_map_ops *ops; |
c93552c4 | 8873 | struct bpf_insn_aux_data *aux; |
81ed18ab AS |
8874 | struct bpf_insn insn_buf[16]; |
8875 | struct bpf_prog *new_prog; | |
8876 | struct bpf_map *map_ptr; | |
8877 | int i, cnt, delta = 0; | |
e245c5c6 | 8878 | |
79741b3b | 8879 | for (i = 0; i < insn_cnt; i++, insn++) { |
f6b1b3bf DB |
8880 | if (insn->code == (BPF_ALU64 | BPF_MOD | BPF_X) || |
8881 | insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) || | |
8882 | insn->code == (BPF_ALU | BPF_MOD | BPF_X) || | |
68fda450 | 8883 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { |
f6b1b3bf DB |
8884 | bool is64 = BPF_CLASS(insn->code) == BPF_ALU64; |
8885 | struct bpf_insn mask_and_div[] = { | |
8886 | BPF_MOV32_REG(insn->src_reg, insn->src_reg), | |
8887 | /* Rx div 0 -> 0 */ | |
8888 | BPF_JMP_IMM(BPF_JNE, insn->src_reg, 0, 2), | |
8889 | BPF_ALU32_REG(BPF_XOR, insn->dst_reg, insn->dst_reg), | |
8890 | BPF_JMP_IMM(BPF_JA, 0, 0, 1), | |
8891 | *insn, | |
8892 | }; | |
8893 | struct bpf_insn mask_and_mod[] = { | |
8894 | BPF_MOV32_REG(insn->src_reg, insn->src_reg), | |
8895 | /* Rx mod 0 -> Rx */ | |
8896 | BPF_JMP_IMM(BPF_JEQ, insn->src_reg, 0, 1), | |
8897 | *insn, | |
8898 | }; | |
8899 | struct bpf_insn *patchlet; | |
8900 | ||
8901 | if (insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) || | |
8902 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { | |
8903 | patchlet = mask_and_div + (is64 ? 1 : 0); | |
8904 | cnt = ARRAY_SIZE(mask_and_div) - (is64 ? 1 : 0); | |
8905 | } else { | |
8906 | patchlet = mask_and_mod + (is64 ? 1 : 0); | |
8907 | cnt = ARRAY_SIZE(mask_and_mod) - (is64 ? 1 : 0); | |
8908 | } | |
8909 | ||
8910 | new_prog = bpf_patch_insn_data(env, i + delta, patchlet, cnt); | |
68fda450 AS |
8911 | if (!new_prog) |
8912 | return -ENOMEM; | |
8913 | ||
8914 | delta += cnt - 1; | |
8915 | env->prog = prog = new_prog; | |
8916 | insn = new_prog->insnsi + i + delta; | |
8917 | continue; | |
8918 | } | |
8919 | ||
e0cea7ce DB |
8920 | if (BPF_CLASS(insn->code) == BPF_LD && |
8921 | (BPF_MODE(insn->code) == BPF_ABS || | |
8922 | BPF_MODE(insn->code) == BPF_IND)) { | |
8923 | cnt = env->ops->gen_ld_abs(insn, insn_buf); | |
8924 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { | |
8925 | verbose(env, "bpf verifier is misconfigured\n"); | |
8926 | return -EINVAL; | |
8927 | } | |
8928 | ||
8929 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
8930 | if (!new_prog) | |
8931 | return -ENOMEM; | |
8932 | ||
8933 | delta += cnt - 1; | |
8934 | env->prog = prog = new_prog; | |
8935 | insn = new_prog->insnsi + i + delta; | |
8936 | continue; | |
8937 | } | |
8938 | ||
979d63d5 DB |
8939 | if (insn->code == (BPF_ALU64 | BPF_ADD | BPF_X) || |
8940 | insn->code == (BPF_ALU64 | BPF_SUB | BPF_X)) { | |
8941 | const u8 code_add = BPF_ALU64 | BPF_ADD | BPF_X; | |
8942 | const u8 code_sub = BPF_ALU64 | BPF_SUB | BPF_X; | |
8943 | struct bpf_insn insn_buf[16]; | |
8944 | struct bpf_insn *patch = &insn_buf[0]; | |
8945 | bool issrc, isneg; | |
8946 | u32 off_reg; | |
8947 | ||
8948 | aux = &env->insn_aux_data[i + delta]; | |
3612af78 DB |
8949 | if (!aux->alu_state || |
8950 | aux->alu_state == BPF_ALU_NON_POINTER) | |
979d63d5 DB |
8951 | continue; |
8952 | ||
8953 | isneg = aux->alu_state & BPF_ALU_NEG_VALUE; | |
8954 | issrc = (aux->alu_state & BPF_ALU_SANITIZE) == | |
8955 | BPF_ALU_SANITIZE_SRC; | |
8956 | ||
8957 | off_reg = issrc ? insn->src_reg : insn->dst_reg; | |
8958 | if (isneg) | |
8959 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); | |
8960 | *patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit - 1); | |
8961 | *patch++ = BPF_ALU64_REG(BPF_SUB, BPF_REG_AX, off_reg); | |
8962 | *patch++ = BPF_ALU64_REG(BPF_OR, BPF_REG_AX, off_reg); | |
8963 | *patch++ = BPF_ALU64_IMM(BPF_NEG, BPF_REG_AX, 0); | |
8964 | *patch++ = BPF_ALU64_IMM(BPF_ARSH, BPF_REG_AX, 63); | |
8965 | if (issrc) { | |
8966 | *patch++ = BPF_ALU64_REG(BPF_AND, BPF_REG_AX, | |
8967 | off_reg); | |
8968 | insn->src_reg = BPF_REG_AX; | |
8969 | } else { | |
8970 | *patch++ = BPF_ALU64_REG(BPF_AND, off_reg, | |
8971 | BPF_REG_AX); | |
8972 | } | |
8973 | if (isneg) | |
8974 | insn->code = insn->code == code_add ? | |
8975 | code_sub : code_add; | |
8976 | *patch++ = *insn; | |
8977 | if (issrc && isneg) | |
8978 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); | |
8979 | cnt = patch - insn_buf; | |
8980 | ||
8981 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
8982 | if (!new_prog) | |
8983 | return -ENOMEM; | |
8984 | ||
8985 | delta += cnt - 1; | |
8986 | env->prog = prog = new_prog; | |
8987 | insn = new_prog->insnsi + i + delta; | |
8988 | continue; | |
8989 | } | |
8990 | ||
79741b3b AS |
8991 | if (insn->code != (BPF_JMP | BPF_CALL)) |
8992 | continue; | |
cc8b0b92 AS |
8993 | if (insn->src_reg == BPF_PSEUDO_CALL) |
8994 | continue; | |
e245c5c6 | 8995 | |
79741b3b AS |
8996 | if (insn->imm == BPF_FUNC_get_route_realm) |
8997 | prog->dst_needed = 1; | |
8998 | if (insn->imm == BPF_FUNC_get_prandom_u32) | |
8999 | bpf_user_rnd_init_once(); | |
9802d865 JB |
9000 | if (insn->imm == BPF_FUNC_override_return) |
9001 | prog->kprobe_override = 1; | |
79741b3b | 9002 | if (insn->imm == BPF_FUNC_tail_call) { |
7b9f6da1 DM |
9003 | /* If we tail call into other programs, we |
9004 | * cannot make any assumptions since they can | |
9005 | * be replaced dynamically during runtime in | |
9006 | * the program array. | |
9007 | */ | |
9008 | prog->cb_access = 1; | |
80a58d02 | 9009 | env->prog->aux->stack_depth = MAX_BPF_STACK; |
e647815a | 9010 | env->prog->aux->max_pkt_offset = MAX_PACKET_OFF; |
7b9f6da1 | 9011 | |
79741b3b AS |
9012 | /* mark bpf_tail_call as different opcode to avoid |
9013 | * conditional branch in the interpeter for every normal | |
9014 | * call and to prevent accidental JITing by JIT compiler | |
9015 | * that doesn't support bpf_tail_call yet | |
e245c5c6 | 9016 | */ |
79741b3b | 9017 | insn->imm = 0; |
71189fa9 | 9018 | insn->code = BPF_JMP | BPF_TAIL_CALL; |
b2157399 | 9019 | |
c93552c4 DB |
9020 | aux = &env->insn_aux_data[i + delta]; |
9021 | if (!bpf_map_ptr_unpriv(aux)) | |
9022 | continue; | |
9023 | ||
b2157399 AS |
9024 | /* instead of changing every JIT dealing with tail_call |
9025 | * emit two extra insns: | |
9026 | * if (index >= max_entries) goto out; | |
9027 | * index &= array->index_mask; | |
9028 | * to avoid out-of-bounds cpu speculation | |
9029 | */ | |
c93552c4 | 9030 | if (bpf_map_ptr_poisoned(aux)) { |
40950343 | 9031 | verbose(env, "tail_call abusing map_ptr\n"); |
b2157399 AS |
9032 | return -EINVAL; |
9033 | } | |
c93552c4 DB |
9034 | |
9035 | map_ptr = BPF_MAP_PTR(aux->map_state); | |
b2157399 AS |
9036 | insn_buf[0] = BPF_JMP_IMM(BPF_JGE, BPF_REG_3, |
9037 | map_ptr->max_entries, 2); | |
9038 | insn_buf[1] = BPF_ALU32_IMM(BPF_AND, BPF_REG_3, | |
9039 | container_of(map_ptr, | |
9040 | struct bpf_array, | |
9041 | map)->index_mask); | |
9042 | insn_buf[2] = *insn; | |
9043 | cnt = 3; | |
9044 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
9045 | if (!new_prog) | |
9046 | return -ENOMEM; | |
9047 | ||
9048 | delta += cnt - 1; | |
9049 | env->prog = prog = new_prog; | |
9050 | insn = new_prog->insnsi + i + delta; | |
79741b3b AS |
9051 | continue; |
9052 | } | |
e245c5c6 | 9053 | |
89c63074 | 9054 | /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup |
09772d92 DB |
9055 | * and other inlining handlers are currently limited to 64 bit |
9056 | * only. | |
89c63074 | 9057 | */ |
60b58afc | 9058 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
09772d92 DB |
9059 | (insn->imm == BPF_FUNC_map_lookup_elem || |
9060 | insn->imm == BPF_FUNC_map_update_elem || | |
84430d42 DB |
9061 | insn->imm == BPF_FUNC_map_delete_elem || |
9062 | insn->imm == BPF_FUNC_map_push_elem || | |
9063 | insn->imm == BPF_FUNC_map_pop_elem || | |
9064 | insn->imm == BPF_FUNC_map_peek_elem)) { | |
c93552c4 DB |
9065 | aux = &env->insn_aux_data[i + delta]; |
9066 | if (bpf_map_ptr_poisoned(aux)) | |
9067 | goto patch_call_imm; | |
9068 | ||
9069 | map_ptr = BPF_MAP_PTR(aux->map_state); | |
09772d92 DB |
9070 | ops = map_ptr->ops; |
9071 | if (insn->imm == BPF_FUNC_map_lookup_elem && | |
9072 | ops->map_gen_lookup) { | |
9073 | cnt = ops->map_gen_lookup(map_ptr, insn_buf); | |
9074 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { | |
9075 | verbose(env, "bpf verifier is misconfigured\n"); | |
9076 | return -EINVAL; | |
9077 | } | |
81ed18ab | 9078 | |
09772d92 DB |
9079 | new_prog = bpf_patch_insn_data(env, i + delta, |
9080 | insn_buf, cnt); | |
9081 | if (!new_prog) | |
9082 | return -ENOMEM; | |
81ed18ab | 9083 | |
09772d92 DB |
9084 | delta += cnt - 1; |
9085 | env->prog = prog = new_prog; | |
9086 | insn = new_prog->insnsi + i + delta; | |
9087 | continue; | |
9088 | } | |
81ed18ab | 9089 | |
09772d92 DB |
9090 | BUILD_BUG_ON(!__same_type(ops->map_lookup_elem, |
9091 | (void *(*)(struct bpf_map *map, void *key))NULL)); | |
9092 | BUILD_BUG_ON(!__same_type(ops->map_delete_elem, | |
9093 | (int (*)(struct bpf_map *map, void *key))NULL)); | |
9094 | BUILD_BUG_ON(!__same_type(ops->map_update_elem, | |
9095 | (int (*)(struct bpf_map *map, void *key, void *value, | |
9096 | u64 flags))NULL)); | |
84430d42 DB |
9097 | BUILD_BUG_ON(!__same_type(ops->map_push_elem, |
9098 | (int (*)(struct bpf_map *map, void *value, | |
9099 | u64 flags))NULL)); | |
9100 | BUILD_BUG_ON(!__same_type(ops->map_pop_elem, | |
9101 | (int (*)(struct bpf_map *map, void *value))NULL)); | |
9102 | BUILD_BUG_ON(!__same_type(ops->map_peek_elem, | |
9103 | (int (*)(struct bpf_map *map, void *value))NULL)); | |
9104 | ||
09772d92 DB |
9105 | switch (insn->imm) { |
9106 | case BPF_FUNC_map_lookup_elem: | |
9107 | insn->imm = BPF_CAST_CALL(ops->map_lookup_elem) - | |
9108 | __bpf_call_base; | |
9109 | continue; | |
9110 | case BPF_FUNC_map_update_elem: | |
9111 | insn->imm = BPF_CAST_CALL(ops->map_update_elem) - | |
9112 | __bpf_call_base; | |
9113 | continue; | |
9114 | case BPF_FUNC_map_delete_elem: | |
9115 | insn->imm = BPF_CAST_CALL(ops->map_delete_elem) - | |
9116 | __bpf_call_base; | |
9117 | continue; | |
84430d42 DB |
9118 | case BPF_FUNC_map_push_elem: |
9119 | insn->imm = BPF_CAST_CALL(ops->map_push_elem) - | |
9120 | __bpf_call_base; | |
9121 | continue; | |
9122 | case BPF_FUNC_map_pop_elem: | |
9123 | insn->imm = BPF_CAST_CALL(ops->map_pop_elem) - | |
9124 | __bpf_call_base; | |
9125 | continue; | |
9126 | case BPF_FUNC_map_peek_elem: | |
9127 | insn->imm = BPF_CAST_CALL(ops->map_peek_elem) - | |
9128 | __bpf_call_base; | |
9129 | continue; | |
09772d92 | 9130 | } |
81ed18ab | 9131 | |
09772d92 | 9132 | goto patch_call_imm; |
81ed18ab AS |
9133 | } |
9134 | ||
9135 | patch_call_imm: | |
5e43f899 | 9136 | fn = env->ops->get_func_proto(insn->imm, env->prog); |
79741b3b AS |
9137 | /* all functions that have prototype and verifier allowed |
9138 | * programs to call them, must be real in-kernel functions | |
9139 | */ | |
9140 | if (!fn->func) { | |
61bd5218 JK |
9141 | verbose(env, |
9142 | "kernel subsystem misconfigured func %s#%d\n", | |
79741b3b AS |
9143 | func_id_name(insn->imm), insn->imm); |
9144 | return -EFAULT; | |
e245c5c6 | 9145 | } |
79741b3b | 9146 | insn->imm = fn->func - __bpf_call_base; |
e245c5c6 | 9147 | } |
e245c5c6 | 9148 | |
79741b3b AS |
9149 | return 0; |
9150 | } | |
e245c5c6 | 9151 | |
58e2af8b | 9152 | static void free_states(struct bpf_verifier_env *env) |
f1bca824 | 9153 | { |
58e2af8b | 9154 | struct bpf_verifier_state_list *sl, *sln; |
f1bca824 AS |
9155 | int i; |
9156 | ||
9f4686c4 AS |
9157 | sl = env->free_list; |
9158 | while (sl) { | |
9159 | sln = sl->next; | |
9160 | free_verifier_state(&sl->state, false); | |
9161 | kfree(sl); | |
9162 | sl = sln; | |
9163 | } | |
9164 | ||
f1bca824 AS |
9165 | if (!env->explored_states) |
9166 | return; | |
9167 | ||
dc2a4ebc | 9168 | for (i = 0; i < state_htab_size(env); i++) { |
f1bca824 AS |
9169 | sl = env->explored_states[i]; |
9170 | ||
a8f500af AS |
9171 | while (sl) { |
9172 | sln = sl->next; | |
9173 | free_verifier_state(&sl->state, false); | |
9174 | kfree(sl); | |
9175 | sl = sln; | |
9176 | } | |
f1bca824 AS |
9177 | } |
9178 | ||
71dde681 | 9179 | kvfree(env->explored_states); |
f1bca824 AS |
9180 | } |
9181 | ||
06ee7115 AS |
9182 | static void print_verification_stats(struct bpf_verifier_env *env) |
9183 | { | |
9184 | int i; | |
9185 | ||
9186 | if (env->log.level & BPF_LOG_STATS) { | |
9187 | verbose(env, "verification time %lld usec\n", | |
9188 | div_u64(env->verification_time, 1000)); | |
9189 | verbose(env, "stack depth "); | |
9190 | for (i = 0; i < env->subprog_cnt; i++) { | |
9191 | u32 depth = env->subprog_info[i].stack_depth; | |
9192 | ||
9193 | verbose(env, "%d", depth); | |
9194 | if (i + 1 < env->subprog_cnt) | |
9195 | verbose(env, "+"); | |
9196 | } | |
9197 | verbose(env, "\n"); | |
9198 | } | |
9199 | verbose(env, "processed %d insns (limit %d) max_states_per_insn %d " | |
9200 | "total_states %d peak_states %d mark_read %d\n", | |
9201 | env->insn_processed, BPF_COMPLEXITY_LIMIT_INSNS, | |
9202 | env->max_states_per_insn, env->total_states, | |
9203 | env->peak_states, env->longest_mark_read_walk); | |
f1bca824 AS |
9204 | } |
9205 | ||
838e9690 YS |
9206 | int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, |
9207 | union bpf_attr __user *uattr) | |
51580e79 | 9208 | { |
06ee7115 | 9209 | u64 start_time = ktime_get_ns(); |
58e2af8b | 9210 | struct bpf_verifier_env *env; |
b9193c1b | 9211 | struct bpf_verifier_log *log; |
9e4c24e7 | 9212 | int i, len, ret = -EINVAL; |
e2ae4ca2 | 9213 | bool is_priv; |
51580e79 | 9214 | |
eba0c929 AB |
9215 | /* no program is valid */ |
9216 | if (ARRAY_SIZE(bpf_verifier_ops) == 0) | |
9217 | return -EINVAL; | |
9218 | ||
58e2af8b | 9219 | /* 'struct bpf_verifier_env' can be global, but since it's not small, |
cbd35700 AS |
9220 | * allocate/free it every time bpf_check() is called |
9221 | */ | |
58e2af8b | 9222 | env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL); |
cbd35700 AS |
9223 | if (!env) |
9224 | return -ENOMEM; | |
61bd5218 | 9225 | log = &env->log; |
cbd35700 | 9226 | |
9e4c24e7 | 9227 | len = (*prog)->len; |
fad953ce | 9228 | env->insn_aux_data = |
9e4c24e7 | 9229 | vzalloc(array_size(sizeof(struct bpf_insn_aux_data), len)); |
3df126f3 JK |
9230 | ret = -ENOMEM; |
9231 | if (!env->insn_aux_data) | |
9232 | goto err_free_env; | |
9e4c24e7 JK |
9233 | for (i = 0; i < len; i++) |
9234 | env->insn_aux_data[i].orig_idx = i; | |
9bac3d6d | 9235 | env->prog = *prog; |
00176a34 | 9236 | env->ops = bpf_verifier_ops[env->prog->type]; |
45a73c17 | 9237 | is_priv = capable(CAP_SYS_ADMIN); |
0246e64d | 9238 | |
cbd35700 | 9239 | /* grab the mutex to protect few globals used by verifier */ |
45a73c17 AS |
9240 | if (!is_priv) |
9241 | mutex_lock(&bpf_verifier_lock); | |
cbd35700 AS |
9242 | |
9243 | if (attr->log_level || attr->log_buf || attr->log_size) { | |
9244 | /* user requested verbose verifier output | |
9245 | * and supplied buffer to store the verification trace | |
9246 | */ | |
e7bf8249 JK |
9247 | log->level = attr->log_level; |
9248 | log->ubuf = (char __user *) (unsigned long) attr->log_buf; | |
9249 | log->len_total = attr->log_size; | |
cbd35700 AS |
9250 | |
9251 | ret = -EINVAL; | |
e7bf8249 | 9252 | /* log attributes have to be sane */ |
7a9f5c65 | 9253 | if (log->len_total < 128 || log->len_total > UINT_MAX >> 2 || |
06ee7115 | 9254 | !log->level || !log->ubuf || log->level & ~BPF_LOG_MASK) |
3df126f3 | 9255 | goto err_unlock; |
cbd35700 | 9256 | } |
1ad2f583 DB |
9257 | |
9258 | env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT); | |
9259 | if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) | |
e07b98d9 | 9260 | env->strict_alignment = true; |
e9ee9efc DM |
9261 | if (attr->prog_flags & BPF_F_ANY_ALIGNMENT) |
9262 | env->strict_alignment = false; | |
cbd35700 | 9263 | |
e2ae4ca2 JK |
9264 | env->allow_ptr_leaks = is_priv; |
9265 | ||
10d274e8 AS |
9266 | if (is_priv) |
9267 | env->test_state_freq = attr->prog_flags & BPF_F_TEST_STATE_FREQ; | |
9268 | ||
f4e3ec0d JK |
9269 | ret = replace_map_fd_with_map_ptr(env); |
9270 | if (ret < 0) | |
9271 | goto skip_full_check; | |
9272 | ||
cae1927c | 9273 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
a40a2632 | 9274 | ret = bpf_prog_offload_verifier_prep(env->prog); |
ab3f0063 | 9275 | if (ret) |
f4e3ec0d | 9276 | goto skip_full_check; |
ab3f0063 JK |
9277 | } |
9278 | ||
dc2a4ebc | 9279 | env->explored_states = kvcalloc(state_htab_size(env), |
58e2af8b | 9280 | sizeof(struct bpf_verifier_state_list *), |
f1bca824 AS |
9281 | GFP_USER); |
9282 | ret = -ENOMEM; | |
9283 | if (!env->explored_states) | |
9284 | goto skip_full_check; | |
9285 | ||
d9762e84 | 9286 | ret = check_subprogs(env); |
475fb78f AS |
9287 | if (ret < 0) |
9288 | goto skip_full_check; | |
9289 | ||
c454a46b | 9290 | ret = check_btf_info(env, attr, uattr); |
838e9690 YS |
9291 | if (ret < 0) |
9292 | goto skip_full_check; | |
9293 | ||
d9762e84 MKL |
9294 | ret = check_cfg(env); |
9295 | if (ret < 0) | |
9296 | goto skip_full_check; | |
9297 | ||
17a52670 | 9298 | ret = do_check(env); |
8c01c4f8 CG |
9299 | if (env->cur_state) { |
9300 | free_verifier_state(env->cur_state, true); | |
9301 | env->cur_state = NULL; | |
9302 | } | |
cbd35700 | 9303 | |
c941ce9c QM |
9304 | if (ret == 0 && bpf_prog_is_dev_bound(env->prog->aux)) |
9305 | ret = bpf_prog_offload_finalize(env); | |
9306 | ||
0246e64d | 9307 | skip_full_check: |
638f5b90 | 9308 | while (!pop_stack(env, NULL, NULL)); |
f1bca824 | 9309 | free_states(env); |
0246e64d | 9310 | |
c131187d | 9311 | if (ret == 0) |
9b38c405 | 9312 | ret = check_max_stack_depth(env); |
c131187d | 9313 | |
9b38c405 | 9314 | /* instruction rewrites happen after this point */ |
e2ae4ca2 JK |
9315 | if (is_priv) { |
9316 | if (ret == 0) | |
9317 | opt_hard_wire_dead_code_branches(env); | |
52875a04 JK |
9318 | if (ret == 0) |
9319 | ret = opt_remove_dead_code(env); | |
a1b14abc JK |
9320 | if (ret == 0) |
9321 | ret = opt_remove_nops(env); | |
52875a04 JK |
9322 | } else { |
9323 | if (ret == 0) | |
9324 | sanitize_dead_code(env); | |
e2ae4ca2 JK |
9325 | } |
9326 | ||
9bac3d6d AS |
9327 | if (ret == 0) |
9328 | /* program is valid, convert *(u32*)(ctx + off) accesses */ | |
9329 | ret = convert_ctx_accesses(env); | |
9330 | ||
e245c5c6 | 9331 | if (ret == 0) |
79741b3b | 9332 | ret = fixup_bpf_calls(env); |
e245c5c6 | 9333 | |
a4b1d3c1 JW |
9334 | /* do 32-bit optimization after insn patching has done so those patched |
9335 | * insns could be handled correctly. | |
9336 | */ | |
d6c2308c JW |
9337 | if (ret == 0 && !bpf_prog_is_dev_bound(env->prog->aux)) { |
9338 | ret = opt_subreg_zext_lo32_rnd_hi32(env, attr); | |
9339 | env->prog->aux->verifier_zext = bpf_jit_needs_zext() ? !ret | |
9340 | : false; | |
a4b1d3c1 JW |
9341 | } |
9342 | ||
1ea47e01 AS |
9343 | if (ret == 0) |
9344 | ret = fixup_call_args(env); | |
9345 | ||
06ee7115 AS |
9346 | env->verification_time = ktime_get_ns() - start_time; |
9347 | print_verification_stats(env); | |
9348 | ||
a2a7d570 | 9349 | if (log->level && bpf_verifier_log_full(log)) |
cbd35700 | 9350 | ret = -ENOSPC; |
a2a7d570 | 9351 | if (log->level && !log->ubuf) { |
cbd35700 | 9352 | ret = -EFAULT; |
a2a7d570 | 9353 | goto err_release_maps; |
cbd35700 AS |
9354 | } |
9355 | ||
0246e64d AS |
9356 | if (ret == 0 && env->used_map_cnt) { |
9357 | /* if program passed verifier, update used_maps in bpf_prog_info */ | |
9bac3d6d AS |
9358 | env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt, |
9359 | sizeof(env->used_maps[0]), | |
9360 | GFP_KERNEL); | |
0246e64d | 9361 | |
9bac3d6d | 9362 | if (!env->prog->aux->used_maps) { |
0246e64d | 9363 | ret = -ENOMEM; |
a2a7d570 | 9364 | goto err_release_maps; |
0246e64d AS |
9365 | } |
9366 | ||
9bac3d6d | 9367 | memcpy(env->prog->aux->used_maps, env->used_maps, |
0246e64d | 9368 | sizeof(env->used_maps[0]) * env->used_map_cnt); |
9bac3d6d | 9369 | env->prog->aux->used_map_cnt = env->used_map_cnt; |
0246e64d AS |
9370 | |
9371 | /* program is valid. Convert pseudo bpf_ld_imm64 into generic | |
9372 | * bpf_ld_imm64 instructions | |
9373 | */ | |
9374 | convert_pseudo_ld_imm64(env); | |
9375 | } | |
cbd35700 | 9376 | |
ba64e7d8 YS |
9377 | if (ret == 0) |
9378 | adjust_btf_func(env); | |
9379 | ||
a2a7d570 | 9380 | err_release_maps: |
9bac3d6d | 9381 | if (!env->prog->aux->used_maps) |
0246e64d | 9382 | /* if we didn't copy map pointers into bpf_prog_info, release |
ab7f5bf0 | 9383 | * them now. Otherwise free_used_maps() will release them. |
0246e64d AS |
9384 | */ |
9385 | release_maps(env); | |
9bac3d6d | 9386 | *prog = env->prog; |
3df126f3 | 9387 | err_unlock: |
45a73c17 AS |
9388 | if (!is_priv) |
9389 | mutex_unlock(&bpf_verifier_lock); | |
3df126f3 JK |
9390 | vfree(env->insn_aux_data); |
9391 | err_free_env: | |
9392 | kfree(env); | |
51580e79 AS |
9393 | return ret; |
9394 | } |