]>
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 | ||
07016151 | 171 | #define BPF_COMPLEXITY_LIMIT_STACK 1024 |
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 MKL |
328 | type == PTR_TO_SOCK_COMMON || |
329 | type == PTR_TO_TCP_SOCK; | |
46f8bc92 MKL |
330 | } |
331 | ||
840b9615 JS |
332 | static bool reg_type_may_be_null(enum bpf_reg_type type) |
333 | { | |
fd978bf7 | 334 | return type == PTR_TO_MAP_VALUE_OR_NULL || |
46f8bc92 | 335 | type == PTR_TO_SOCKET_OR_NULL || |
655a51e5 MKL |
336 | type == PTR_TO_SOCK_COMMON_OR_NULL || |
337 | type == PTR_TO_TCP_SOCK_OR_NULL; | |
fd978bf7 JS |
338 | } |
339 | ||
d83525ca AS |
340 | static bool reg_may_point_to_spin_lock(const struct bpf_reg_state *reg) |
341 | { | |
342 | return reg->type == PTR_TO_MAP_VALUE && | |
343 | map_value_has_spin_lock(reg->map_ptr); | |
344 | } | |
345 | ||
cba368c1 MKL |
346 | static bool reg_type_may_be_refcounted_or_null(enum bpf_reg_type type) |
347 | { | |
348 | return type == PTR_TO_SOCKET || | |
349 | type == PTR_TO_SOCKET_OR_NULL || | |
350 | type == PTR_TO_TCP_SOCK || | |
351 | type == PTR_TO_TCP_SOCK_OR_NULL; | |
352 | } | |
353 | ||
1b986589 | 354 | static bool arg_type_may_be_refcounted(enum bpf_arg_type type) |
fd978bf7 | 355 | { |
1b986589 | 356 | return type == ARG_PTR_TO_SOCK_COMMON; |
fd978bf7 JS |
357 | } |
358 | ||
359 | /* Determine whether the function releases some resources allocated by another | |
360 | * function call. The first reference type argument will be assumed to be | |
361 | * released by release_reference(). | |
362 | */ | |
363 | static bool is_release_function(enum bpf_func_id func_id) | |
364 | { | |
6acc9b43 | 365 | return func_id == BPF_FUNC_sk_release; |
840b9615 JS |
366 | } |
367 | ||
46f8bc92 MKL |
368 | static bool is_acquire_function(enum bpf_func_id func_id) |
369 | { | |
370 | return func_id == BPF_FUNC_sk_lookup_tcp || | |
edbf8c01 LB |
371 | func_id == BPF_FUNC_sk_lookup_udp || |
372 | func_id == BPF_FUNC_skc_lookup_tcp; | |
46f8bc92 MKL |
373 | } |
374 | ||
1b986589 MKL |
375 | static bool is_ptr_cast_function(enum bpf_func_id func_id) |
376 | { | |
377 | return func_id == BPF_FUNC_tcp_sock || | |
378 | func_id == BPF_FUNC_sk_fullsock; | |
379 | } | |
380 | ||
17a52670 AS |
381 | /* string representation of 'enum bpf_reg_type' */ |
382 | static const char * const reg_type_str[] = { | |
383 | [NOT_INIT] = "?", | |
f1174f77 | 384 | [SCALAR_VALUE] = "inv", |
17a52670 AS |
385 | [PTR_TO_CTX] = "ctx", |
386 | [CONST_PTR_TO_MAP] = "map_ptr", | |
387 | [PTR_TO_MAP_VALUE] = "map_value", | |
388 | [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null", | |
17a52670 | 389 | [PTR_TO_STACK] = "fp", |
969bf05e | 390 | [PTR_TO_PACKET] = "pkt", |
de8f3a83 | 391 | [PTR_TO_PACKET_META] = "pkt_meta", |
969bf05e | 392 | [PTR_TO_PACKET_END] = "pkt_end", |
d58e468b | 393 | [PTR_TO_FLOW_KEYS] = "flow_keys", |
c64b7983 JS |
394 | [PTR_TO_SOCKET] = "sock", |
395 | [PTR_TO_SOCKET_OR_NULL] = "sock_or_null", | |
46f8bc92 MKL |
396 | [PTR_TO_SOCK_COMMON] = "sock_common", |
397 | [PTR_TO_SOCK_COMMON_OR_NULL] = "sock_common_or_null", | |
655a51e5 MKL |
398 | [PTR_TO_TCP_SOCK] = "tcp_sock", |
399 | [PTR_TO_TCP_SOCK_OR_NULL] = "tcp_sock_or_null", | |
9df1c28b | 400 | [PTR_TO_TP_BUFFER] = "tp_buffer", |
17a52670 AS |
401 | }; |
402 | ||
8efea21d EC |
403 | static char slot_type_char[] = { |
404 | [STACK_INVALID] = '?', | |
405 | [STACK_SPILL] = 'r', | |
406 | [STACK_MISC] = 'm', | |
407 | [STACK_ZERO] = '0', | |
408 | }; | |
409 | ||
4e92024a AS |
410 | static void print_liveness(struct bpf_verifier_env *env, |
411 | enum bpf_reg_liveness live) | |
412 | { | |
9242b5f5 | 413 | if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN | REG_LIVE_DONE)) |
4e92024a AS |
414 | verbose(env, "_"); |
415 | if (live & REG_LIVE_READ) | |
416 | verbose(env, "r"); | |
417 | if (live & REG_LIVE_WRITTEN) | |
418 | verbose(env, "w"); | |
9242b5f5 AS |
419 | if (live & REG_LIVE_DONE) |
420 | verbose(env, "D"); | |
4e92024a AS |
421 | } |
422 | ||
f4d7e40a AS |
423 | static struct bpf_func_state *func(struct bpf_verifier_env *env, |
424 | const struct bpf_reg_state *reg) | |
425 | { | |
426 | struct bpf_verifier_state *cur = env->cur_state; | |
427 | ||
428 | return cur->frame[reg->frameno]; | |
429 | } | |
430 | ||
61bd5218 | 431 | static void print_verifier_state(struct bpf_verifier_env *env, |
f4d7e40a | 432 | const struct bpf_func_state *state) |
17a52670 | 433 | { |
f4d7e40a | 434 | const struct bpf_reg_state *reg; |
17a52670 AS |
435 | enum bpf_reg_type t; |
436 | int i; | |
437 | ||
f4d7e40a AS |
438 | if (state->frameno) |
439 | verbose(env, " frame%d:", state->frameno); | |
17a52670 | 440 | for (i = 0; i < MAX_BPF_REG; i++) { |
1a0dc1ac AS |
441 | reg = &state->regs[i]; |
442 | t = reg->type; | |
17a52670 AS |
443 | if (t == NOT_INIT) |
444 | continue; | |
4e92024a AS |
445 | verbose(env, " R%d", i); |
446 | print_liveness(env, reg->live); | |
447 | verbose(env, "=%s", reg_type_str[t]); | |
f1174f77 EC |
448 | if ((t == SCALAR_VALUE || t == PTR_TO_STACK) && |
449 | tnum_is_const(reg->var_off)) { | |
450 | /* reg->off should be 0 for SCALAR_VALUE */ | |
61bd5218 | 451 | verbose(env, "%lld", reg->var_off.value + reg->off); |
f4d7e40a AS |
452 | if (t == PTR_TO_STACK) |
453 | verbose(env, ",call_%d", func(env, reg)->callsite); | |
f1174f77 | 454 | } else { |
cba368c1 MKL |
455 | verbose(env, "(id=%d", reg->id); |
456 | if (reg_type_may_be_refcounted_or_null(t)) | |
457 | verbose(env, ",ref_obj_id=%d", reg->ref_obj_id); | |
f1174f77 | 458 | if (t != SCALAR_VALUE) |
61bd5218 | 459 | verbose(env, ",off=%d", reg->off); |
de8f3a83 | 460 | if (type_is_pkt_pointer(t)) |
61bd5218 | 461 | verbose(env, ",r=%d", reg->range); |
f1174f77 EC |
462 | else if (t == CONST_PTR_TO_MAP || |
463 | t == PTR_TO_MAP_VALUE || | |
464 | t == PTR_TO_MAP_VALUE_OR_NULL) | |
61bd5218 | 465 | verbose(env, ",ks=%d,vs=%d", |
f1174f77 EC |
466 | reg->map_ptr->key_size, |
467 | reg->map_ptr->value_size); | |
7d1238f2 EC |
468 | if (tnum_is_const(reg->var_off)) { |
469 | /* Typically an immediate SCALAR_VALUE, but | |
470 | * could be a pointer whose offset is too big | |
471 | * for reg->off | |
472 | */ | |
61bd5218 | 473 | verbose(env, ",imm=%llx", reg->var_off.value); |
7d1238f2 EC |
474 | } else { |
475 | if (reg->smin_value != reg->umin_value && | |
476 | reg->smin_value != S64_MIN) | |
61bd5218 | 477 | verbose(env, ",smin_value=%lld", |
7d1238f2 EC |
478 | (long long)reg->smin_value); |
479 | if (reg->smax_value != reg->umax_value && | |
480 | reg->smax_value != S64_MAX) | |
61bd5218 | 481 | verbose(env, ",smax_value=%lld", |
7d1238f2 EC |
482 | (long long)reg->smax_value); |
483 | if (reg->umin_value != 0) | |
61bd5218 | 484 | verbose(env, ",umin_value=%llu", |
7d1238f2 EC |
485 | (unsigned long long)reg->umin_value); |
486 | if (reg->umax_value != U64_MAX) | |
61bd5218 | 487 | verbose(env, ",umax_value=%llu", |
7d1238f2 EC |
488 | (unsigned long long)reg->umax_value); |
489 | if (!tnum_is_unknown(reg->var_off)) { | |
490 | char tn_buf[48]; | |
f1174f77 | 491 | |
7d1238f2 | 492 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
61bd5218 | 493 | verbose(env, ",var_off=%s", tn_buf); |
7d1238f2 | 494 | } |
f1174f77 | 495 | } |
61bd5218 | 496 | verbose(env, ")"); |
f1174f77 | 497 | } |
17a52670 | 498 | } |
638f5b90 | 499 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
8efea21d EC |
500 | char types_buf[BPF_REG_SIZE + 1]; |
501 | bool valid = false; | |
502 | int j; | |
503 | ||
504 | for (j = 0; j < BPF_REG_SIZE; j++) { | |
505 | if (state->stack[i].slot_type[j] != STACK_INVALID) | |
506 | valid = true; | |
507 | types_buf[j] = slot_type_char[ | |
508 | state->stack[i].slot_type[j]]; | |
509 | } | |
510 | types_buf[BPF_REG_SIZE] = 0; | |
511 | if (!valid) | |
512 | continue; | |
513 | verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE); | |
514 | print_liveness(env, state->stack[i].spilled_ptr.live); | |
515 | if (state->stack[i].slot_type[0] == STACK_SPILL) | |
4e92024a | 516 | verbose(env, "=%s", |
638f5b90 | 517 | reg_type_str[state->stack[i].spilled_ptr.type]); |
8efea21d EC |
518 | else |
519 | verbose(env, "=%s", types_buf); | |
17a52670 | 520 | } |
fd978bf7 JS |
521 | if (state->acquired_refs && state->refs[0].id) { |
522 | verbose(env, " refs=%d", state->refs[0].id); | |
523 | for (i = 1; i < state->acquired_refs; i++) | |
524 | if (state->refs[i].id) | |
525 | verbose(env, ",%d", state->refs[i].id); | |
526 | } | |
61bd5218 | 527 | verbose(env, "\n"); |
17a52670 AS |
528 | } |
529 | ||
84dbf350 JS |
530 | #define COPY_STATE_FN(NAME, COUNT, FIELD, SIZE) \ |
531 | static int copy_##NAME##_state(struct bpf_func_state *dst, \ | |
532 | const struct bpf_func_state *src) \ | |
533 | { \ | |
534 | if (!src->FIELD) \ | |
535 | return 0; \ | |
536 | if (WARN_ON_ONCE(dst->COUNT < src->COUNT)) { \ | |
537 | /* internal bug, make state invalid to reject the program */ \ | |
538 | memset(dst, 0, sizeof(*dst)); \ | |
539 | return -EFAULT; \ | |
540 | } \ | |
541 | memcpy(dst->FIELD, src->FIELD, \ | |
542 | sizeof(*src->FIELD) * (src->COUNT / SIZE)); \ | |
543 | return 0; \ | |
638f5b90 | 544 | } |
fd978bf7 JS |
545 | /* copy_reference_state() */ |
546 | COPY_STATE_FN(reference, acquired_refs, refs, 1) | |
84dbf350 JS |
547 | /* copy_stack_state() */ |
548 | COPY_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE) | |
549 | #undef COPY_STATE_FN | |
550 | ||
551 | #define REALLOC_STATE_FN(NAME, COUNT, FIELD, SIZE) \ | |
552 | static int realloc_##NAME##_state(struct bpf_func_state *state, int size, \ | |
553 | bool copy_old) \ | |
554 | { \ | |
555 | u32 old_size = state->COUNT; \ | |
556 | struct bpf_##NAME##_state *new_##FIELD; \ | |
557 | int slot = size / SIZE; \ | |
558 | \ | |
559 | if (size <= old_size || !size) { \ | |
560 | if (copy_old) \ | |
561 | return 0; \ | |
562 | state->COUNT = slot * SIZE; \ | |
563 | if (!size && old_size) { \ | |
564 | kfree(state->FIELD); \ | |
565 | state->FIELD = NULL; \ | |
566 | } \ | |
567 | return 0; \ | |
568 | } \ | |
569 | new_##FIELD = kmalloc_array(slot, sizeof(struct bpf_##NAME##_state), \ | |
570 | GFP_KERNEL); \ | |
571 | if (!new_##FIELD) \ | |
572 | return -ENOMEM; \ | |
573 | if (copy_old) { \ | |
574 | if (state->FIELD) \ | |
575 | memcpy(new_##FIELD, state->FIELD, \ | |
576 | sizeof(*new_##FIELD) * (old_size / SIZE)); \ | |
577 | memset(new_##FIELD + old_size / SIZE, 0, \ | |
578 | sizeof(*new_##FIELD) * (size - old_size) / SIZE); \ | |
579 | } \ | |
580 | state->COUNT = slot * SIZE; \ | |
581 | kfree(state->FIELD); \ | |
582 | state->FIELD = new_##FIELD; \ | |
583 | return 0; \ | |
584 | } | |
fd978bf7 JS |
585 | /* realloc_reference_state() */ |
586 | REALLOC_STATE_FN(reference, acquired_refs, refs, 1) | |
84dbf350 JS |
587 | /* realloc_stack_state() */ |
588 | REALLOC_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE) | |
589 | #undef REALLOC_STATE_FN | |
638f5b90 AS |
590 | |
591 | /* do_check() starts with zero-sized stack in struct bpf_verifier_state to | |
592 | * make it consume minimal amount of memory. check_stack_write() access from | |
f4d7e40a | 593 | * the program calls into realloc_func_state() to grow the stack size. |
84dbf350 JS |
594 | * Note there is a non-zero 'parent' pointer inside bpf_verifier_state |
595 | * which realloc_stack_state() copies over. It points to previous | |
596 | * bpf_verifier_state which is never reallocated. | |
638f5b90 | 597 | */ |
fd978bf7 JS |
598 | static int realloc_func_state(struct bpf_func_state *state, int stack_size, |
599 | int refs_size, bool copy_old) | |
638f5b90 | 600 | { |
fd978bf7 JS |
601 | int err = realloc_reference_state(state, refs_size, copy_old); |
602 | if (err) | |
603 | return err; | |
604 | return realloc_stack_state(state, stack_size, copy_old); | |
605 | } | |
606 | ||
607 | /* Acquire a pointer id from the env and update the state->refs to include | |
608 | * this new pointer reference. | |
609 | * On success, returns a valid pointer id to associate with the register | |
610 | * On failure, returns a negative errno. | |
638f5b90 | 611 | */ |
fd978bf7 | 612 | static int acquire_reference_state(struct bpf_verifier_env *env, int insn_idx) |
638f5b90 | 613 | { |
fd978bf7 JS |
614 | struct bpf_func_state *state = cur_func(env); |
615 | int new_ofs = state->acquired_refs; | |
616 | int id, err; | |
617 | ||
618 | err = realloc_reference_state(state, state->acquired_refs + 1, true); | |
619 | if (err) | |
620 | return err; | |
621 | id = ++env->id_gen; | |
622 | state->refs[new_ofs].id = id; | |
623 | state->refs[new_ofs].insn_idx = insn_idx; | |
638f5b90 | 624 | |
fd978bf7 JS |
625 | return id; |
626 | } | |
627 | ||
628 | /* release function corresponding to acquire_reference_state(). Idempotent. */ | |
46f8bc92 | 629 | static int release_reference_state(struct bpf_func_state *state, int ptr_id) |
fd978bf7 JS |
630 | { |
631 | int i, last_idx; | |
632 | ||
fd978bf7 JS |
633 | last_idx = state->acquired_refs - 1; |
634 | for (i = 0; i < state->acquired_refs; i++) { | |
635 | if (state->refs[i].id == ptr_id) { | |
636 | if (last_idx && i != last_idx) | |
637 | memcpy(&state->refs[i], &state->refs[last_idx], | |
638 | sizeof(*state->refs)); | |
639 | memset(&state->refs[last_idx], 0, sizeof(*state->refs)); | |
640 | state->acquired_refs--; | |
638f5b90 | 641 | return 0; |
638f5b90 | 642 | } |
638f5b90 | 643 | } |
46f8bc92 | 644 | return -EINVAL; |
fd978bf7 JS |
645 | } |
646 | ||
647 | static int transfer_reference_state(struct bpf_func_state *dst, | |
648 | struct bpf_func_state *src) | |
649 | { | |
650 | int err = realloc_reference_state(dst, src->acquired_refs, false); | |
651 | if (err) | |
652 | return err; | |
653 | err = copy_reference_state(dst, src); | |
654 | if (err) | |
655 | return err; | |
638f5b90 AS |
656 | return 0; |
657 | } | |
658 | ||
f4d7e40a AS |
659 | static void free_func_state(struct bpf_func_state *state) |
660 | { | |
5896351e AS |
661 | if (!state) |
662 | return; | |
fd978bf7 | 663 | kfree(state->refs); |
f4d7e40a AS |
664 | kfree(state->stack); |
665 | kfree(state); | |
666 | } | |
667 | ||
1969db47 AS |
668 | static void free_verifier_state(struct bpf_verifier_state *state, |
669 | bool free_self) | |
638f5b90 | 670 | { |
f4d7e40a AS |
671 | int i; |
672 | ||
673 | for (i = 0; i <= state->curframe; i++) { | |
674 | free_func_state(state->frame[i]); | |
675 | state->frame[i] = NULL; | |
676 | } | |
1969db47 AS |
677 | if (free_self) |
678 | kfree(state); | |
638f5b90 AS |
679 | } |
680 | ||
681 | /* copy verifier state from src to dst growing dst stack space | |
682 | * when necessary to accommodate larger src stack | |
683 | */ | |
f4d7e40a AS |
684 | static int copy_func_state(struct bpf_func_state *dst, |
685 | const struct bpf_func_state *src) | |
638f5b90 AS |
686 | { |
687 | int err; | |
688 | ||
fd978bf7 JS |
689 | err = realloc_func_state(dst, src->allocated_stack, src->acquired_refs, |
690 | false); | |
691 | if (err) | |
692 | return err; | |
693 | memcpy(dst, src, offsetof(struct bpf_func_state, acquired_refs)); | |
694 | err = copy_reference_state(dst, src); | |
638f5b90 AS |
695 | if (err) |
696 | return err; | |
638f5b90 AS |
697 | return copy_stack_state(dst, src); |
698 | } | |
699 | ||
f4d7e40a AS |
700 | static int copy_verifier_state(struct bpf_verifier_state *dst_state, |
701 | const struct bpf_verifier_state *src) | |
702 | { | |
703 | struct bpf_func_state *dst; | |
704 | int i, err; | |
705 | ||
706 | /* if dst has more stack frames then src frame, free them */ | |
707 | for (i = src->curframe + 1; i <= dst_state->curframe; i++) { | |
708 | free_func_state(dst_state->frame[i]); | |
709 | dst_state->frame[i] = NULL; | |
710 | } | |
979d63d5 | 711 | dst_state->speculative = src->speculative; |
f4d7e40a | 712 | dst_state->curframe = src->curframe; |
d83525ca | 713 | dst_state->active_spin_lock = src->active_spin_lock; |
f4d7e40a AS |
714 | for (i = 0; i <= src->curframe; i++) { |
715 | dst = dst_state->frame[i]; | |
716 | if (!dst) { | |
717 | dst = kzalloc(sizeof(*dst), GFP_KERNEL); | |
718 | if (!dst) | |
719 | return -ENOMEM; | |
720 | dst_state->frame[i] = dst; | |
721 | } | |
722 | err = copy_func_state(dst, src->frame[i]); | |
723 | if (err) | |
724 | return err; | |
725 | } | |
726 | return 0; | |
727 | } | |
728 | ||
638f5b90 AS |
729 | static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx, |
730 | int *insn_idx) | |
731 | { | |
732 | struct bpf_verifier_state *cur = env->cur_state; | |
733 | struct bpf_verifier_stack_elem *elem, *head = env->head; | |
734 | int err; | |
17a52670 AS |
735 | |
736 | if (env->head == NULL) | |
638f5b90 | 737 | return -ENOENT; |
17a52670 | 738 | |
638f5b90 AS |
739 | if (cur) { |
740 | err = copy_verifier_state(cur, &head->st); | |
741 | if (err) | |
742 | return err; | |
743 | } | |
744 | if (insn_idx) | |
745 | *insn_idx = head->insn_idx; | |
17a52670 | 746 | if (prev_insn_idx) |
638f5b90 AS |
747 | *prev_insn_idx = head->prev_insn_idx; |
748 | elem = head->next; | |
1969db47 | 749 | free_verifier_state(&head->st, false); |
638f5b90 | 750 | kfree(head); |
17a52670 AS |
751 | env->head = elem; |
752 | env->stack_size--; | |
638f5b90 | 753 | return 0; |
17a52670 AS |
754 | } |
755 | ||
58e2af8b | 756 | static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, |
979d63d5 DB |
757 | int insn_idx, int prev_insn_idx, |
758 | bool speculative) | |
17a52670 | 759 | { |
638f5b90 | 760 | struct bpf_verifier_state *cur = env->cur_state; |
58e2af8b | 761 | struct bpf_verifier_stack_elem *elem; |
638f5b90 | 762 | int err; |
17a52670 | 763 | |
638f5b90 | 764 | elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL); |
17a52670 AS |
765 | if (!elem) |
766 | goto err; | |
767 | ||
17a52670 AS |
768 | elem->insn_idx = insn_idx; |
769 | elem->prev_insn_idx = prev_insn_idx; | |
770 | elem->next = env->head; | |
771 | env->head = elem; | |
772 | env->stack_size++; | |
1969db47 AS |
773 | err = copy_verifier_state(&elem->st, cur); |
774 | if (err) | |
775 | goto err; | |
979d63d5 | 776 | elem->st.speculative |= speculative; |
07016151 | 777 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_STACK) { |
61bd5218 | 778 | verbose(env, "BPF program is too complex\n"); |
17a52670 AS |
779 | goto err; |
780 | } | |
781 | return &elem->st; | |
782 | err: | |
5896351e AS |
783 | free_verifier_state(env->cur_state, true); |
784 | env->cur_state = NULL; | |
17a52670 | 785 | /* pop all elements and return */ |
638f5b90 | 786 | while (!pop_stack(env, NULL, NULL)); |
17a52670 AS |
787 | return NULL; |
788 | } | |
789 | ||
790 | #define CALLER_SAVED_REGS 6 | |
791 | static const int caller_saved[CALLER_SAVED_REGS] = { | |
792 | BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5 | |
793 | }; | |
794 | ||
f1174f77 EC |
795 | static void __mark_reg_not_init(struct bpf_reg_state *reg); |
796 | ||
b03c9f9f EC |
797 | /* Mark the unknown part of a register (variable offset or scalar value) as |
798 | * known to have the value @imm. | |
799 | */ | |
800 | static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm) | |
801 | { | |
a9c676bc AS |
802 | /* Clear id, off, and union(map_ptr, range) */ |
803 | memset(((u8 *)reg) + sizeof(reg->type), 0, | |
804 | offsetof(struct bpf_reg_state, var_off) - sizeof(reg->type)); | |
b03c9f9f EC |
805 | reg->var_off = tnum_const(imm); |
806 | reg->smin_value = (s64)imm; | |
807 | reg->smax_value = (s64)imm; | |
808 | reg->umin_value = imm; | |
809 | reg->umax_value = imm; | |
810 | } | |
811 | ||
f1174f77 EC |
812 | /* Mark the 'variable offset' part of a register as zero. This should be |
813 | * used only on registers holding a pointer type. | |
814 | */ | |
815 | static void __mark_reg_known_zero(struct bpf_reg_state *reg) | |
a9789ef9 | 816 | { |
b03c9f9f | 817 | __mark_reg_known(reg, 0); |
f1174f77 | 818 | } |
a9789ef9 | 819 | |
cc2b14d5 AS |
820 | static void __mark_reg_const_zero(struct bpf_reg_state *reg) |
821 | { | |
822 | __mark_reg_known(reg, 0); | |
cc2b14d5 AS |
823 | reg->type = SCALAR_VALUE; |
824 | } | |
825 | ||
61bd5218 JK |
826 | static void mark_reg_known_zero(struct bpf_verifier_env *env, |
827 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
828 | { |
829 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 830 | verbose(env, "mark_reg_known_zero(regs, %u)\n", regno); |
f1174f77 EC |
831 | /* Something bad happened, let's kill all regs */ |
832 | for (regno = 0; regno < MAX_BPF_REG; regno++) | |
833 | __mark_reg_not_init(regs + regno); | |
834 | return; | |
835 | } | |
836 | __mark_reg_known_zero(regs + regno); | |
837 | } | |
838 | ||
de8f3a83 DB |
839 | static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg) |
840 | { | |
841 | return type_is_pkt_pointer(reg->type); | |
842 | } | |
843 | ||
844 | static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg) | |
845 | { | |
846 | return reg_is_pkt_pointer(reg) || | |
847 | reg->type == PTR_TO_PACKET_END; | |
848 | } | |
849 | ||
850 | /* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */ | |
851 | static bool reg_is_init_pkt_pointer(const struct bpf_reg_state *reg, | |
852 | enum bpf_reg_type which) | |
853 | { | |
854 | /* The register can already have a range from prior markings. | |
855 | * This is fine as long as it hasn't been advanced from its | |
856 | * origin. | |
857 | */ | |
858 | return reg->type == which && | |
859 | reg->id == 0 && | |
860 | reg->off == 0 && | |
861 | tnum_equals_const(reg->var_off, 0); | |
862 | } | |
863 | ||
b03c9f9f EC |
864 | /* Attempts to improve min/max values based on var_off information */ |
865 | static void __update_reg_bounds(struct bpf_reg_state *reg) | |
866 | { | |
867 | /* min signed is max(sign bit) | min(other bits) */ | |
868 | reg->smin_value = max_t(s64, reg->smin_value, | |
869 | reg->var_off.value | (reg->var_off.mask & S64_MIN)); | |
870 | /* max signed is min(sign bit) | max(other bits) */ | |
871 | reg->smax_value = min_t(s64, reg->smax_value, | |
872 | reg->var_off.value | (reg->var_off.mask & S64_MAX)); | |
873 | reg->umin_value = max(reg->umin_value, reg->var_off.value); | |
874 | reg->umax_value = min(reg->umax_value, | |
875 | reg->var_off.value | reg->var_off.mask); | |
876 | } | |
877 | ||
878 | /* Uses signed min/max values to inform unsigned, and vice-versa */ | |
879 | static void __reg_deduce_bounds(struct bpf_reg_state *reg) | |
880 | { | |
881 | /* Learn sign from signed bounds. | |
882 | * If we cannot cross the sign boundary, then signed and unsigned bounds | |
883 | * are the same, so combine. This works even in the negative case, e.g. | |
884 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. | |
885 | */ | |
886 | if (reg->smin_value >= 0 || reg->smax_value < 0) { | |
887 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, | |
888 | reg->umin_value); | |
889 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, | |
890 | reg->umax_value); | |
891 | return; | |
892 | } | |
893 | /* Learn sign from unsigned bounds. Signed bounds cross the sign | |
894 | * boundary, so we must be careful. | |
895 | */ | |
896 | if ((s64)reg->umax_value >= 0) { | |
897 | /* Positive. We can't learn anything from the smin, but smax | |
898 | * is positive, hence safe. | |
899 | */ | |
900 | reg->smin_value = reg->umin_value; | |
901 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, | |
902 | reg->umax_value); | |
903 | } else if ((s64)reg->umin_value < 0) { | |
904 | /* Negative. We can't learn anything from the smax, but smin | |
905 | * is negative, hence safe. | |
906 | */ | |
907 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, | |
908 | reg->umin_value); | |
909 | reg->smax_value = reg->umax_value; | |
910 | } | |
911 | } | |
912 | ||
913 | /* Attempts to improve var_off based on unsigned min/max information */ | |
914 | static void __reg_bound_offset(struct bpf_reg_state *reg) | |
915 | { | |
916 | reg->var_off = tnum_intersect(reg->var_off, | |
917 | tnum_range(reg->umin_value, | |
918 | reg->umax_value)); | |
919 | } | |
920 | ||
921 | /* Reset the min/max bounds of a register */ | |
922 | static void __mark_reg_unbounded(struct bpf_reg_state *reg) | |
923 | { | |
924 | reg->smin_value = S64_MIN; | |
925 | reg->smax_value = S64_MAX; | |
926 | reg->umin_value = 0; | |
927 | reg->umax_value = U64_MAX; | |
928 | } | |
929 | ||
f1174f77 EC |
930 | /* Mark a register as having a completely unknown (scalar) value. */ |
931 | static void __mark_reg_unknown(struct bpf_reg_state *reg) | |
932 | { | |
a9c676bc AS |
933 | /* |
934 | * Clear type, id, off, and union(map_ptr, range) and | |
935 | * padding between 'type' and union | |
936 | */ | |
937 | memset(reg, 0, offsetof(struct bpf_reg_state, var_off)); | |
f1174f77 | 938 | reg->type = SCALAR_VALUE; |
f1174f77 | 939 | reg->var_off = tnum_unknown; |
f4d7e40a | 940 | reg->frameno = 0; |
b03c9f9f | 941 | __mark_reg_unbounded(reg); |
f1174f77 EC |
942 | } |
943 | ||
61bd5218 JK |
944 | static void mark_reg_unknown(struct bpf_verifier_env *env, |
945 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
946 | { |
947 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 948 | verbose(env, "mark_reg_unknown(regs, %u)\n", regno); |
19ceb417 AS |
949 | /* Something bad happened, let's kill all regs except FP */ |
950 | for (regno = 0; regno < BPF_REG_FP; regno++) | |
f1174f77 EC |
951 | __mark_reg_not_init(regs + regno); |
952 | return; | |
953 | } | |
954 | __mark_reg_unknown(regs + regno); | |
955 | } | |
956 | ||
957 | static void __mark_reg_not_init(struct bpf_reg_state *reg) | |
958 | { | |
959 | __mark_reg_unknown(reg); | |
960 | reg->type = NOT_INIT; | |
961 | } | |
962 | ||
61bd5218 JK |
963 | static void mark_reg_not_init(struct bpf_verifier_env *env, |
964 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
965 | { |
966 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 967 | verbose(env, "mark_reg_not_init(regs, %u)\n", regno); |
19ceb417 AS |
968 | /* Something bad happened, let's kill all regs except FP */ |
969 | for (regno = 0; regno < BPF_REG_FP; regno++) | |
f1174f77 EC |
970 | __mark_reg_not_init(regs + regno); |
971 | return; | |
972 | } | |
973 | __mark_reg_not_init(regs + regno); | |
a9789ef9 DB |
974 | } |
975 | ||
61bd5218 | 976 | static void init_reg_state(struct bpf_verifier_env *env, |
f4d7e40a | 977 | struct bpf_func_state *state) |
17a52670 | 978 | { |
f4d7e40a | 979 | struct bpf_reg_state *regs = state->regs; |
17a52670 AS |
980 | int i; |
981 | ||
dc503a8a | 982 | for (i = 0; i < MAX_BPF_REG; i++) { |
61bd5218 | 983 | mark_reg_not_init(env, regs, i); |
dc503a8a | 984 | regs[i].live = REG_LIVE_NONE; |
679c782d | 985 | regs[i].parent = NULL; |
dc503a8a | 986 | } |
17a52670 AS |
987 | |
988 | /* frame pointer */ | |
f1174f77 | 989 | regs[BPF_REG_FP].type = PTR_TO_STACK; |
61bd5218 | 990 | mark_reg_known_zero(env, regs, BPF_REG_FP); |
f4d7e40a | 991 | regs[BPF_REG_FP].frameno = state->frameno; |
17a52670 AS |
992 | |
993 | /* 1st arg to a function */ | |
994 | regs[BPF_REG_1].type = PTR_TO_CTX; | |
61bd5218 | 995 | mark_reg_known_zero(env, regs, BPF_REG_1); |
6760bf2d DB |
996 | } |
997 | ||
f4d7e40a AS |
998 | #define BPF_MAIN_FUNC (-1) |
999 | static void init_func_state(struct bpf_verifier_env *env, | |
1000 | struct bpf_func_state *state, | |
1001 | int callsite, int frameno, int subprogno) | |
1002 | { | |
1003 | state->callsite = callsite; | |
1004 | state->frameno = frameno; | |
1005 | state->subprogno = subprogno; | |
1006 | init_reg_state(env, state); | |
1007 | } | |
1008 | ||
17a52670 AS |
1009 | enum reg_arg_type { |
1010 | SRC_OP, /* register is used as source operand */ | |
1011 | DST_OP, /* register is used as destination operand */ | |
1012 | DST_OP_NO_MARK /* same as above, check only, don't mark */ | |
1013 | }; | |
1014 | ||
cc8b0b92 AS |
1015 | static int cmp_subprogs(const void *a, const void *b) |
1016 | { | |
9c8105bd JW |
1017 | return ((struct bpf_subprog_info *)a)->start - |
1018 | ((struct bpf_subprog_info *)b)->start; | |
cc8b0b92 AS |
1019 | } |
1020 | ||
1021 | static int find_subprog(struct bpf_verifier_env *env, int off) | |
1022 | { | |
9c8105bd | 1023 | struct bpf_subprog_info *p; |
cc8b0b92 | 1024 | |
9c8105bd JW |
1025 | p = bsearch(&off, env->subprog_info, env->subprog_cnt, |
1026 | sizeof(env->subprog_info[0]), cmp_subprogs); | |
cc8b0b92 AS |
1027 | if (!p) |
1028 | return -ENOENT; | |
9c8105bd | 1029 | return p - env->subprog_info; |
cc8b0b92 AS |
1030 | |
1031 | } | |
1032 | ||
1033 | static int add_subprog(struct bpf_verifier_env *env, int off) | |
1034 | { | |
1035 | int insn_cnt = env->prog->len; | |
1036 | int ret; | |
1037 | ||
1038 | if (off >= insn_cnt || off < 0) { | |
1039 | verbose(env, "call to invalid destination\n"); | |
1040 | return -EINVAL; | |
1041 | } | |
1042 | ret = find_subprog(env, off); | |
1043 | if (ret >= 0) | |
1044 | return 0; | |
4cb3d99c | 1045 | if (env->subprog_cnt >= BPF_MAX_SUBPROGS) { |
cc8b0b92 AS |
1046 | verbose(env, "too many subprograms\n"); |
1047 | return -E2BIG; | |
1048 | } | |
9c8105bd JW |
1049 | env->subprog_info[env->subprog_cnt++].start = off; |
1050 | sort(env->subprog_info, env->subprog_cnt, | |
1051 | sizeof(env->subprog_info[0]), cmp_subprogs, NULL); | |
cc8b0b92 AS |
1052 | return 0; |
1053 | } | |
1054 | ||
1055 | static int check_subprogs(struct bpf_verifier_env *env) | |
1056 | { | |
1057 | int i, ret, subprog_start, subprog_end, off, cur_subprog = 0; | |
9c8105bd | 1058 | struct bpf_subprog_info *subprog = env->subprog_info; |
cc8b0b92 AS |
1059 | struct bpf_insn *insn = env->prog->insnsi; |
1060 | int insn_cnt = env->prog->len; | |
1061 | ||
f910cefa JW |
1062 | /* Add entry function. */ |
1063 | ret = add_subprog(env, 0); | |
1064 | if (ret < 0) | |
1065 | return ret; | |
1066 | ||
cc8b0b92 AS |
1067 | /* determine subprog starts. The end is one before the next starts */ |
1068 | for (i = 0; i < insn_cnt; i++) { | |
1069 | if (insn[i].code != (BPF_JMP | BPF_CALL)) | |
1070 | continue; | |
1071 | if (insn[i].src_reg != BPF_PSEUDO_CALL) | |
1072 | continue; | |
1073 | if (!env->allow_ptr_leaks) { | |
1074 | verbose(env, "function calls to other bpf functions are allowed for root only\n"); | |
1075 | return -EPERM; | |
1076 | } | |
cc8b0b92 AS |
1077 | ret = add_subprog(env, i + insn[i].imm + 1); |
1078 | if (ret < 0) | |
1079 | return ret; | |
1080 | } | |
1081 | ||
4cb3d99c JW |
1082 | /* Add a fake 'exit' subprog which could simplify subprog iteration |
1083 | * logic. 'subprog_cnt' should not be increased. | |
1084 | */ | |
1085 | subprog[env->subprog_cnt].start = insn_cnt; | |
1086 | ||
06ee7115 | 1087 | if (env->log.level & BPF_LOG_LEVEL2) |
cc8b0b92 | 1088 | for (i = 0; i < env->subprog_cnt; i++) |
9c8105bd | 1089 | verbose(env, "func#%d @%d\n", i, subprog[i].start); |
cc8b0b92 AS |
1090 | |
1091 | /* now check that all jumps are within the same subprog */ | |
4cb3d99c JW |
1092 | subprog_start = subprog[cur_subprog].start; |
1093 | subprog_end = subprog[cur_subprog + 1].start; | |
cc8b0b92 AS |
1094 | for (i = 0; i < insn_cnt; i++) { |
1095 | u8 code = insn[i].code; | |
1096 | ||
092ed096 | 1097 | if (BPF_CLASS(code) != BPF_JMP && BPF_CLASS(code) != BPF_JMP32) |
cc8b0b92 AS |
1098 | goto next; |
1099 | if (BPF_OP(code) == BPF_EXIT || BPF_OP(code) == BPF_CALL) | |
1100 | goto next; | |
1101 | off = i + insn[i].off + 1; | |
1102 | if (off < subprog_start || off >= subprog_end) { | |
1103 | verbose(env, "jump out of range from insn %d to %d\n", i, off); | |
1104 | return -EINVAL; | |
1105 | } | |
1106 | next: | |
1107 | if (i == subprog_end - 1) { | |
1108 | /* to avoid fall-through from one subprog into another | |
1109 | * the last insn of the subprog should be either exit | |
1110 | * or unconditional jump back | |
1111 | */ | |
1112 | if (code != (BPF_JMP | BPF_EXIT) && | |
1113 | code != (BPF_JMP | BPF_JA)) { | |
1114 | verbose(env, "last insn is not an exit or jmp\n"); | |
1115 | return -EINVAL; | |
1116 | } | |
1117 | subprog_start = subprog_end; | |
4cb3d99c JW |
1118 | cur_subprog++; |
1119 | if (cur_subprog < env->subprog_cnt) | |
9c8105bd | 1120 | subprog_end = subprog[cur_subprog + 1].start; |
cc8b0b92 AS |
1121 | } |
1122 | } | |
1123 | return 0; | |
1124 | } | |
1125 | ||
679c782d EC |
1126 | /* Parentage chain of this register (or stack slot) should take care of all |
1127 | * issues like callee-saved registers, stack slot allocation time, etc. | |
1128 | */ | |
f4d7e40a | 1129 | static int mark_reg_read(struct bpf_verifier_env *env, |
679c782d EC |
1130 | const struct bpf_reg_state *state, |
1131 | struct bpf_reg_state *parent) | |
f4d7e40a AS |
1132 | { |
1133 | bool writes = parent == state->parent; /* Observe write marks */ | |
06ee7115 | 1134 | int cnt = 0; |
dc503a8a EC |
1135 | |
1136 | while (parent) { | |
1137 | /* if read wasn't screened by an earlier write ... */ | |
679c782d | 1138 | if (writes && state->live & REG_LIVE_WRITTEN) |
dc503a8a | 1139 | break; |
9242b5f5 AS |
1140 | if (parent->live & REG_LIVE_DONE) { |
1141 | verbose(env, "verifier BUG type %s var_off %lld off %d\n", | |
1142 | reg_type_str[parent->type], | |
1143 | parent->var_off.value, parent->off); | |
1144 | return -EFAULT; | |
1145 | } | |
25af32da AS |
1146 | if (parent->live & REG_LIVE_READ) |
1147 | /* The parentage chain never changes and | |
1148 | * this parent was already marked as LIVE_READ. | |
1149 | * There is no need to keep walking the chain again and | |
1150 | * keep re-marking all parents as LIVE_READ. | |
1151 | * This case happens when the same register is read | |
1152 | * multiple times without writes into it in-between. | |
1153 | */ | |
1154 | break; | |
dc503a8a | 1155 | /* ... then we depend on parent's value */ |
679c782d | 1156 | parent->live |= REG_LIVE_READ; |
dc503a8a EC |
1157 | state = parent; |
1158 | parent = state->parent; | |
f4d7e40a | 1159 | writes = true; |
06ee7115 | 1160 | cnt++; |
dc503a8a | 1161 | } |
06ee7115 AS |
1162 | |
1163 | if (env->longest_mark_read_walk < cnt) | |
1164 | env->longest_mark_read_walk = cnt; | |
f4d7e40a | 1165 | return 0; |
dc503a8a EC |
1166 | } |
1167 | ||
1168 | static int check_reg_arg(struct bpf_verifier_env *env, u32 regno, | |
17a52670 AS |
1169 | enum reg_arg_type t) |
1170 | { | |
f4d7e40a AS |
1171 | struct bpf_verifier_state *vstate = env->cur_state; |
1172 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
c342dc10 | 1173 | struct bpf_reg_state *reg, *regs = state->regs; |
dc503a8a | 1174 | |
17a52670 | 1175 | if (regno >= MAX_BPF_REG) { |
61bd5218 | 1176 | verbose(env, "R%d is invalid\n", regno); |
17a52670 AS |
1177 | return -EINVAL; |
1178 | } | |
1179 | ||
c342dc10 | 1180 | reg = ®s[regno]; |
17a52670 AS |
1181 | if (t == SRC_OP) { |
1182 | /* check whether register used as source operand can be read */ | |
c342dc10 | 1183 | if (reg->type == NOT_INIT) { |
61bd5218 | 1184 | verbose(env, "R%d !read_ok\n", regno); |
17a52670 AS |
1185 | return -EACCES; |
1186 | } | |
679c782d | 1187 | /* We don't need to worry about FP liveness because it's read-only */ |
c342dc10 JW |
1188 | if (regno == BPF_REG_FP) |
1189 | return 0; | |
1190 | ||
1191 | return mark_reg_read(env, reg, reg->parent); | |
17a52670 AS |
1192 | } else { |
1193 | /* check whether register used as dest operand can be written to */ | |
1194 | if (regno == BPF_REG_FP) { | |
61bd5218 | 1195 | verbose(env, "frame pointer is read only\n"); |
17a52670 AS |
1196 | return -EACCES; |
1197 | } | |
c342dc10 | 1198 | reg->live |= REG_LIVE_WRITTEN; |
17a52670 | 1199 | if (t == DST_OP) |
61bd5218 | 1200 | mark_reg_unknown(env, regs, regno); |
17a52670 AS |
1201 | } |
1202 | return 0; | |
1203 | } | |
1204 | ||
1be7f75d AS |
1205 | static bool is_spillable_regtype(enum bpf_reg_type type) |
1206 | { | |
1207 | switch (type) { | |
1208 | case PTR_TO_MAP_VALUE: | |
1209 | case PTR_TO_MAP_VALUE_OR_NULL: | |
1210 | case PTR_TO_STACK: | |
1211 | case PTR_TO_CTX: | |
969bf05e | 1212 | case PTR_TO_PACKET: |
de8f3a83 | 1213 | case PTR_TO_PACKET_META: |
969bf05e | 1214 | case PTR_TO_PACKET_END: |
d58e468b | 1215 | case PTR_TO_FLOW_KEYS: |
1be7f75d | 1216 | case CONST_PTR_TO_MAP: |
c64b7983 JS |
1217 | case PTR_TO_SOCKET: |
1218 | case PTR_TO_SOCKET_OR_NULL: | |
46f8bc92 MKL |
1219 | case PTR_TO_SOCK_COMMON: |
1220 | case PTR_TO_SOCK_COMMON_OR_NULL: | |
655a51e5 MKL |
1221 | case PTR_TO_TCP_SOCK: |
1222 | case PTR_TO_TCP_SOCK_OR_NULL: | |
1be7f75d AS |
1223 | return true; |
1224 | default: | |
1225 | return false; | |
1226 | } | |
1227 | } | |
1228 | ||
cc2b14d5 AS |
1229 | /* Does this register contain a constant zero? */ |
1230 | static bool register_is_null(struct bpf_reg_state *reg) | |
1231 | { | |
1232 | return reg->type == SCALAR_VALUE && tnum_equals_const(reg->var_off, 0); | |
1233 | } | |
1234 | ||
17a52670 AS |
1235 | /* check_stack_read/write functions track spill/fill of registers, |
1236 | * stack boundary and alignment are checked in check_mem_access() | |
1237 | */ | |
61bd5218 | 1238 | static int check_stack_write(struct bpf_verifier_env *env, |
f4d7e40a | 1239 | struct bpf_func_state *state, /* func where register points to */ |
af86ca4e | 1240 | int off, int size, int value_regno, int insn_idx) |
17a52670 | 1241 | { |
f4d7e40a | 1242 | struct bpf_func_state *cur; /* state of the current function */ |
638f5b90 | 1243 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err; |
f4d7e40a | 1244 | enum bpf_reg_type type; |
638f5b90 | 1245 | |
f4d7e40a | 1246 | err = realloc_func_state(state, round_up(slot + 1, BPF_REG_SIZE), |
fd978bf7 | 1247 | state->acquired_refs, true); |
638f5b90 AS |
1248 | if (err) |
1249 | return err; | |
9c399760 AS |
1250 | /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0, |
1251 | * so it's aligned access and [off, off + size) are within stack limits | |
1252 | */ | |
638f5b90 AS |
1253 | if (!env->allow_ptr_leaks && |
1254 | state->stack[spi].slot_type[0] == STACK_SPILL && | |
1255 | size != BPF_REG_SIZE) { | |
1256 | verbose(env, "attempt to corrupt spilled pointer on stack\n"); | |
1257 | return -EACCES; | |
1258 | } | |
17a52670 | 1259 | |
f4d7e40a | 1260 | cur = env->cur_state->frame[env->cur_state->curframe]; |
17a52670 | 1261 | if (value_regno >= 0 && |
f4d7e40a | 1262 | is_spillable_regtype((type = cur->regs[value_regno].type))) { |
17a52670 AS |
1263 | |
1264 | /* register containing pointer is being spilled into stack */ | |
9c399760 | 1265 | if (size != BPF_REG_SIZE) { |
61bd5218 | 1266 | verbose(env, "invalid size of register spill\n"); |
17a52670 AS |
1267 | return -EACCES; |
1268 | } | |
1269 | ||
f4d7e40a AS |
1270 | if (state != cur && type == PTR_TO_STACK) { |
1271 | verbose(env, "cannot spill pointers to stack into stack frame of the caller\n"); | |
1272 | return -EINVAL; | |
1273 | } | |
1274 | ||
17a52670 | 1275 | /* save register state */ |
f4d7e40a | 1276 | state->stack[spi].spilled_ptr = cur->regs[value_regno]; |
638f5b90 | 1277 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; |
17a52670 | 1278 | |
af86ca4e AS |
1279 | for (i = 0; i < BPF_REG_SIZE; i++) { |
1280 | if (state->stack[spi].slot_type[i] == STACK_MISC && | |
1281 | !env->allow_ptr_leaks) { | |
1282 | int *poff = &env->insn_aux_data[insn_idx].sanitize_stack_off; | |
1283 | int soff = (-spi - 1) * BPF_REG_SIZE; | |
1284 | ||
1285 | /* detected reuse of integer stack slot with a pointer | |
1286 | * which means either llvm is reusing stack slot or | |
1287 | * an attacker is trying to exploit CVE-2018-3639 | |
1288 | * (speculative store bypass) | |
1289 | * Have to sanitize that slot with preemptive | |
1290 | * store of zero. | |
1291 | */ | |
1292 | if (*poff && *poff != soff) { | |
1293 | /* disallow programs where single insn stores | |
1294 | * into two different stack slots, since verifier | |
1295 | * cannot sanitize them | |
1296 | */ | |
1297 | verbose(env, | |
1298 | "insn %d cannot access two stack slots fp%d and fp%d", | |
1299 | insn_idx, *poff, soff); | |
1300 | return -EINVAL; | |
1301 | } | |
1302 | *poff = soff; | |
1303 | } | |
638f5b90 | 1304 | state->stack[spi].slot_type[i] = STACK_SPILL; |
af86ca4e | 1305 | } |
9c399760 | 1306 | } else { |
cc2b14d5 AS |
1307 | u8 type = STACK_MISC; |
1308 | ||
679c782d EC |
1309 | /* regular write of data into stack destroys any spilled ptr */ |
1310 | state->stack[spi].spilled_ptr.type = NOT_INIT; | |
0bae2d4d JW |
1311 | /* Mark slots as STACK_MISC if they belonged to spilled ptr. */ |
1312 | if (state->stack[spi].slot_type[0] == STACK_SPILL) | |
1313 | for (i = 0; i < BPF_REG_SIZE; i++) | |
1314 | state->stack[spi].slot_type[i] = STACK_MISC; | |
9c399760 | 1315 | |
cc2b14d5 AS |
1316 | /* only mark the slot as written if all 8 bytes were written |
1317 | * otherwise read propagation may incorrectly stop too soon | |
1318 | * when stack slots are partially written. | |
1319 | * This heuristic means that read propagation will be | |
1320 | * conservative, since it will add reg_live_read marks | |
1321 | * to stack slots all the way to first state when programs | |
1322 | * writes+reads less than 8 bytes | |
1323 | */ | |
1324 | if (size == BPF_REG_SIZE) | |
1325 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; | |
1326 | ||
1327 | /* when we zero initialize stack slots mark them as such */ | |
1328 | if (value_regno >= 0 && | |
1329 | register_is_null(&cur->regs[value_regno])) | |
1330 | type = STACK_ZERO; | |
1331 | ||
0bae2d4d | 1332 | /* Mark slots affected by this stack write. */ |
9c399760 | 1333 | for (i = 0; i < size; i++) |
638f5b90 | 1334 | state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] = |
cc2b14d5 | 1335 | type; |
17a52670 AS |
1336 | } |
1337 | return 0; | |
1338 | } | |
1339 | ||
61bd5218 | 1340 | static int check_stack_read(struct bpf_verifier_env *env, |
f4d7e40a AS |
1341 | struct bpf_func_state *reg_state /* func where register points to */, |
1342 | int off, int size, int value_regno) | |
17a52670 | 1343 | { |
f4d7e40a AS |
1344 | struct bpf_verifier_state *vstate = env->cur_state; |
1345 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
638f5b90 AS |
1346 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE; |
1347 | u8 *stype; | |
17a52670 | 1348 | |
f4d7e40a | 1349 | if (reg_state->allocated_stack <= slot) { |
638f5b90 AS |
1350 | verbose(env, "invalid read from stack off %d+0 size %d\n", |
1351 | off, size); | |
1352 | return -EACCES; | |
1353 | } | |
f4d7e40a | 1354 | stype = reg_state->stack[spi].slot_type; |
17a52670 | 1355 | |
638f5b90 | 1356 | if (stype[0] == STACK_SPILL) { |
9c399760 | 1357 | if (size != BPF_REG_SIZE) { |
61bd5218 | 1358 | verbose(env, "invalid size of register spill\n"); |
17a52670 AS |
1359 | return -EACCES; |
1360 | } | |
9c399760 | 1361 | for (i = 1; i < BPF_REG_SIZE; i++) { |
638f5b90 | 1362 | if (stype[(slot - i) % BPF_REG_SIZE] != STACK_SPILL) { |
61bd5218 | 1363 | verbose(env, "corrupted spill memory\n"); |
17a52670 AS |
1364 | return -EACCES; |
1365 | } | |
1366 | } | |
1367 | ||
dc503a8a | 1368 | if (value_regno >= 0) { |
17a52670 | 1369 | /* restore register state from stack */ |
f4d7e40a | 1370 | state->regs[value_regno] = reg_state->stack[spi].spilled_ptr; |
2f18f62e AS |
1371 | /* mark reg as written since spilled pointer state likely |
1372 | * has its liveness marks cleared by is_state_visited() | |
1373 | * which resets stack/reg liveness for state transitions | |
1374 | */ | |
1375 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; | |
dc503a8a | 1376 | } |
679c782d EC |
1377 | mark_reg_read(env, ®_state->stack[spi].spilled_ptr, |
1378 | reg_state->stack[spi].spilled_ptr.parent); | |
17a52670 AS |
1379 | return 0; |
1380 | } else { | |
cc2b14d5 AS |
1381 | int zeros = 0; |
1382 | ||
17a52670 | 1383 | for (i = 0; i < size; i++) { |
cc2b14d5 AS |
1384 | if (stype[(slot - i) % BPF_REG_SIZE] == STACK_MISC) |
1385 | continue; | |
1386 | if (stype[(slot - i) % BPF_REG_SIZE] == STACK_ZERO) { | |
1387 | zeros++; | |
1388 | continue; | |
17a52670 | 1389 | } |
cc2b14d5 AS |
1390 | verbose(env, "invalid read from stack off %d+%d size %d\n", |
1391 | off, i, size); | |
1392 | return -EACCES; | |
1393 | } | |
679c782d EC |
1394 | mark_reg_read(env, ®_state->stack[spi].spilled_ptr, |
1395 | reg_state->stack[spi].spilled_ptr.parent); | |
cc2b14d5 AS |
1396 | if (value_regno >= 0) { |
1397 | if (zeros == size) { | |
1398 | /* any size read into register is zero extended, | |
1399 | * so the whole register == const_zero | |
1400 | */ | |
1401 | __mark_reg_const_zero(&state->regs[value_regno]); | |
1402 | } else { | |
1403 | /* have read misc data from the stack */ | |
1404 | mark_reg_unknown(env, state->regs, value_regno); | |
1405 | } | |
1406 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; | |
17a52670 | 1407 | } |
17a52670 AS |
1408 | return 0; |
1409 | } | |
1410 | } | |
1411 | ||
e4298d25 DB |
1412 | static int check_stack_access(struct bpf_verifier_env *env, |
1413 | const struct bpf_reg_state *reg, | |
1414 | int off, int size) | |
1415 | { | |
1416 | /* Stack accesses must be at a fixed offset, so that we | |
1417 | * can determine what type of data were returned. See | |
1418 | * check_stack_read(). | |
1419 | */ | |
1420 | if (!tnum_is_const(reg->var_off)) { | |
1421 | char tn_buf[48]; | |
1422 | ||
1423 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
1fbd20f8 | 1424 | verbose(env, "variable stack access var_off=%s off=%d size=%d\n", |
e4298d25 DB |
1425 | tn_buf, off, size); |
1426 | return -EACCES; | |
1427 | } | |
1428 | ||
1429 | if (off >= 0 || off < -MAX_BPF_STACK) { | |
1430 | verbose(env, "invalid stack off=%d size=%d\n", off, size); | |
1431 | return -EACCES; | |
1432 | } | |
1433 | ||
1434 | return 0; | |
1435 | } | |
1436 | ||
591fe988 DB |
1437 | static int check_map_access_type(struct bpf_verifier_env *env, u32 regno, |
1438 | int off, int size, enum bpf_access_type type) | |
1439 | { | |
1440 | struct bpf_reg_state *regs = cur_regs(env); | |
1441 | struct bpf_map *map = regs[regno].map_ptr; | |
1442 | u32 cap = bpf_map_flags_to_cap(map); | |
1443 | ||
1444 | if (type == BPF_WRITE && !(cap & BPF_MAP_CAN_WRITE)) { | |
1445 | verbose(env, "write into map forbidden, value_size=%d off=%d size=%d\n", | |
1446 | map->value_size, off, size); | |
1447 | return -EACCES; | |
1448 | } | |
1449 | ||
1450 | if (type == BPF_READ && !(cap & BPF_MAP_CAN_READ)) { | |
1451 | verbose(env, "read from map forbidden, value_size=%d off=%d size=%d\n", | |
1452 | map->value_size, off, size); | |
1453 | return -EACCES; | |
1454 | } | |
1455 | ||
1456 | return 0; | |
1457 | } | |
1458 | ||
17a52670 | 1459 | /* check read/write into map element returned by bpf_map_lookup_elem() */ |
f1174f77 | 1460 | static int __check_map_access(struct bpf_verifier_env *env, u32 regno, int off, |
9fd29c08 | 1461 | int size, bool zero_size_allowed) |
17a52670 | 1462 | { |
638f5b90 AS |
1463 | struct bpf_reg_state *regs = cur_regs(env); |
1464 | struct bpf_map *map = regs[regno].map_ptr; | |
17a52670 | 1465 | |
9fd29c08 YS |
1466 | if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) || |
1467 | off + size > map->value_size) { | |
61bd5218 | 1468 | verbose(env, "invalid access to map value, value_size=%d off=%d size=%d\n", |
17a52670 AS |
1469 | map->value_size, off, size); |
1470 | return -EACCES; | |
1471 | } | |
1472 | return 0; | |
1473 | } | |
1474 | ||
f1174f77 EC |
1475 | /* check read/write into a map element with possible variable offset */ |
1476 | static int check_map_access(struct bpf_verifier_env *env, u32 regno, | |
9fd29c08 | 1477 | int off, int size, bool zero_size_allowed) |
dbcfe5f7 | 1478 | { |
f4d7e40a AS |
1479 | struct bpf_verifier_state *vstate = env->cur_state; |
1480 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
dbcfe5f7 GB |
1481 | struct bpf_reg_state *reg = &state->regs[regno]; |
1482 | int err; | |
1483 | ||
f1174f77 EC |
1484 | /* We may have adjusted the register to this map value, so we |
1485 | * need to try adding each of min_value and max_value to off | |
1486 | * to make sure our theoretical access will be safe. | |
dbcfe5f7 | 1487 | */ |
06ee7115 | 1488 | if (env->log.level & BPF_LOG_LEVEL) |
61bd5218 | 1489 | print_verifier_state(env, state); |
b7137c4e | 1490 | |
dbcfe5f7 GB |
1491 | /* The minimum value is only important with signed |
1492 | * comparisons where we can't assume the floor of a | |
1493 | * value is 0. If we are using signed variables for our | |
1494 | * index'es we need to make sure that whatever we use | |
1495 | * will have a set floor within our range. | |
1496 | */ | |
b7137c4e DB |
1497 | if (reg->smin_value < 0 && |
1498 | (reg->smin_value == S64_MIN || | |
1499 | (off + reg->smin_value != (s64)(s32)(off + reg->smin_value)) || | |
1500 | reg->smin_value + off < 0)) { | |
61bd5218 | 1501 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
dbcfe5f7 GB |
1502 | regno); |
1503 | return -EACCES; | |
1504 | } | |
9fd29c08 YS |
1505 | err = __check_map_access(env, regno, reg->smin_value + off, size, |
1506 | zero_size_allowed); | |
dbcfe5f7 | 1507 | if (err) { |
61bd5218 JK |
1508 | verbose(env, "R%d min value is outside of the array range\n", |
1509 | regno); | |
dbcfe5f7 GB |
1510 | return err; |
1511 | } | |
1512 | ||
b03c9f9f EC |
1513 | /* If we haven't set a max value then we need to bail since we can't be |
1514 | * sure we won't do bad things. | |
1515 | * If reg->umax_value + off could overflow, treat that as unbounded too. | |
dbcfe5f7 | 1516 | */ |
b03c9f9f | 1517 | if (reg->umax_value >= BPF_MAX_VAR_OFF) { |
61bd5218 | 1518 | verbose(env, "R%d unbounded memory access, make sure to bounds check any array access into a map\n", |
dbcfe5f7 GB |
1519 | regno); |
1520 | return -EACCES; | |
1521 | } | |
9fd29c08 YS |
1522 | err = __check_map_access(env, regno, reg->umax_value + off, size, |
1523 | zero_size_allowed); | |
f1174f77 | 1524 | if (err) |
61bd5218 JK |
1525 | verbose(env, "R%d max value is outside of the array range\n", |
1526 | regno); | |
d83525ca AS |
1527 | |
1528 | if (map_value_has_spin_lock(reg->map_ptr)) { | |
1529 | u32 lock = reg->map_ptr->spin_lock_off; | |
1530 | ||
1531 | /* if any part of struct bpf_spin_lock can be touched by | |
1532 | * load/store reject this program. | |
1533 | * To check that [x1, x2) overlaps with [y1, y2) | |
1534 | * it is sufficient to check x1 < y2 && y1 < x2. | |
1535 | */ | |
1536 | if (reg->smin_value + off < lock + sizeof(struct bpf_spin_lock) && | |
1537 | lock < reg->umax_value + off + size) { | |
1538 | verbose(env, "bpf_spin_lock cannot be accessed directly by load/store\n"); | |
1539 | return -EACCES; | |
1540 | } | |
1541 | } | |
f1174f77 | 1542 | return err; |
dbcfe5f7 GB |
1543 | } |
1544 | ||
969bf05e AS |
1545 | #define MAX_PACKET_OFF 0xffff |
1546 | ||
58e2af8b | 1547 | static bool may_access_direct_pkt_data(struct bpf_verifier_env *env, |
3a0af8fd TG |
1548 | const struct bpf_call_arg_meta *meta, |
1549 | enum bpf_access_type t) | |
4acf6c0b | 1550 | { |
36bbef52 | 1551 | switch (env->prog->type) { |
5d66fa7d | 1552 | /* Program types only with direct read access go here! */ |
3a0af8fd TG |
1553 | case BPF_PROG_TYPE_LWT_IN: |
1554 | case BPF_PROG_TYPE_LWT_OUT: | |
004d4b27 | 1555 | case BPF_PROG_TYPE_LWT_SEG6LOCAL: |
2dbb9b9e | 1556 | case BPF_PROG_TYPE_SK_REUSEPORT: |
5d66fa7d | 1557 | case BPF_PROG_TYPE_FLOW_DISSECTOR: |
d5563d36 | 1558 | case BPF_PROG_TYPE_CGROUP_SKB: |
3a0af8fd TG |
1559 | if (t == BPF_WRITE) |
1560 | return false; | |
7e57fbb2 | 1561 | /* fallthrough */ |
5d66fa7d DB |
1562 | |
1563 | /* Program types with direct read + write access go here! */ | |
36bbef52 DB |
1564 | case BPF_PROG_TYPE_SCHED_CLS: |
1565 | case BPF_PROG_TYPE_SCHED_ACT: | |
4acf6c0b | 1566 | case BPF_PROG_TYPE_XDP: |
3a0af8fd | 1567 | case BPF_PROG_TYPE_LWT_XMIT: |
8a31db56 | 1568 | case BPF_PROG_TYPE_SK_SKB: |
4f738adb | 1569 | case BPF_PROG_TYPE_SK_MSG: |
36bbef52 DB |
1570 | if (meta) |
1571 | return meta->pkt_access; | |
1572 | ||
1573 | env->seen_direct_write = true; | |
4acf6c0b BB |
1574 | return true; |
1575 | default: | |
1576 | return false; | |
1577 | } | |
1578 | } | |
1579 | ||
f1174f77 | 1580 | static int __check_packet_access(struct bpf_verifier_env *env, u32 regno, |
9fd29c08 | 1581 | int off, int size, bool zero_size_allowed) |
969bf05e | 1582 | { |
638f5b90 | 1583 | struct bpf_reg_state *regs = cur_regs(env); |
58e2af8b | 1584 | struct bpf_reg_state *reg = ®s[regno]; |
969bf05e | 1585 | |
9fd29c08 YS |
1586 | if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) || |
1587 | (u64)off + size > reg->range) { | |
61bd5218 | 1588 | verbose(env, "invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n", |
d91b28ed | 1589 | off, size, regno, reg->id, reg->off, reg->range); |
969bf05e AS |
1590 | return -EACCES; |
1591 | } | |
1592 | return 0; | |
1593 | } | |
1594 | ||
f1174f77 | 1595 | static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off, |
9fd29c08 | 1596 | int size, bool zero_size_allowed) |
f1174f77 | 1597 | { |
638f5b90 | 1598 | struct bpf_reg_state *regs = cur_regs(env); |
f1174f77 EC |
1599 | struct bpf_reg_state *reg = ®s[regno]; |
1600 | int err; | |
1601 | ||
1602 | /* We may have added a variable offset to the packet pointer; but any | |
1603 | * reg->range we have comes after that. We are only checking the fixed | |
1604 | * offset. | |
1605 | */ | |
1606 | ||
1607 | /* We don't allow negative numbers, because we aren't tracking enough | |
1608 | * detail to prove they're safe. | |
1609 | */ | |
b03c9f9f | 1610 | if (reg->smin_value < 0) { |
61bd5218 | 1611 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
f1174f77 EC |
1612 | regno); |
1613 | return -EACCES; | |
1614 | } | |
9fd29c08 | 1615 | err = __check_packet_access(env, regno, off, size, zero_size_allowed); |
f1174f77 | 1616 | if (err) { |
61bd5218 | 1617 | verbose(env, "R%d offset is outside of the packet\n", regno); |
f1174f77 EC |
1618 | return err; |
1619 | } | |
e647815a JW |
1620 | |
1621 | /* __check_packet_access has made sure "off + size - 1" is within u16. | |
1622 | * reg->umax_value can't be bigger than MAX_PACKET_OFF which is 0xffff, | |
1623 | * otherwise find_good_pkt_pointers would have refused to set range info | |
1624 | * that __check_packet_access would have rejected this pkt access. | |
1625 | * Therefore, "off + reg->umax_value + size - 1" won't overflow u32. | |
1626 | */ | |
1627 | env->prog->aux->max_pkt_offset = | |
1628 | max_t(u32, env->prog->aux->max_pkt_offset, | |
1629 | off + reg->umax_value + size - 1); | |
1630 | ||
f1174f77 EC |
1631 | return err; |
1632 | } | |
1633 | ||
1634 | /* check access to 'struct bpf_context' fields. Supports fixed offsets only */ | |
31fd8581 | 1635 | static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off, int size, |
19de99f7 | 1636 | enum bpf_access_type t, enum bpf_reg_type *reg_type) |
17a52670 | 1637 | { |
f96da094 DB |
1638 | struct bpf_insn_access_aux info = { |
1639 | .reg_type = *reg_type, | |
1640 | }; | |
31fd8581 | 1641 | |
4f9218aa | 1642 | if (env->ops->is_valid_access && |
5e43f899 | 1643 | env->ops->is_valid_access(off, size, t, env->prog, &info)) { |
f96da094 DB |
1644 | /* A non zero info.ctx_field_size indicates that this field is a |
1645 | * candidate for later verifier transformation to load the whole | |
1646 | * field and then apply a mask when accessed with a narrower | |
1647 | * access than actual ctx access size. A zero info.ctx_field_size | |
1648 | * will only allow for whole field access and rejects any other | |
1649 | * type of narrower access. | |
31fd8581 | 1650 | */ |
23994631 | 1651 | *reg_type = info.reg_type; |
31fd8581 | 1652 | |
4f9218aa | 1653 | env->insn_aux_data[insn_idx].ctx_field_size = info.ctx_field_size; |
32bbe007 AS |
1654 | /* remember the offset of last byte accessed in ctx */ |
1655 | if (env->prog->aux->max_ctx_offset < off + size) | |
1656 | env->prog->aux->max_ctx_offset = off + size; | |
17a52670 | 1657 | return 0; |
32bbe007 | 1658 | } |
17a52670 | 1659 | |
61bd5218 | 1660 | verbose(env, "invalid bpf_context access off=%d size=%d\n", off, size); |
17a52670 AS |
1661 | return -EACCES; |
1662 | } | |
1663 | ||
d58e468b PP |
1664 | static int check_flow_keys_access(struct bpf_verifier_env *env, int off, |
1665 | int size) | |
1666 | { | |
1667 | if (size < 0 || off < 0 || | |
1668 | (u64)off + size > sizeof(struct bpf_flow_keys)) { | |
1669 | verbose(env, "invalid access to flow keys off=%d size=%d\n", | |
1670 | off, size); | |
1671 | return -EACCES; | |
1672 | } | |
1673 | return 0; | |
1674 | } | |
1675 | ||
5f456649 MKL |
1676 | static int check_sock_access(struct bpf_verifier_env *env, int insn_idx, |
1677 | u32 regno, int off, int size, | |
1678 | enum bpf_access_type t) | |
c64b7983 JS |
1679 | { |
1680 | struct bpf_reg_state *regs = cur_regs(env); | |
1681 | struct bpf_reg_state *reg = ®s[regno]; | |
5f456649 | 1682 | struct bpf_insn_access_aux info = {}; |
46f8bc92 | 1683 | bool valid; |
c64b7983 JS |
1684 | |
1685 | if (reg->smin_value < 0) { | |
1686 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", | |
1687 | regno); | |
1688 | return -EACCES; | |
1689 | } | |
1690 | ||
46f8bc92 MKL |
1691 | switch (reg->type) { |
1692 | case PTR_TO_SOCK_COMMON: | |
1693 | valid = bpf_sock_common_is_valid_access(off, size, t, &info); | |
1694 | break; | |
1695 | case PTR_TO_SOCKET: | |
1696 | valid = bpf_sock_is_valid_access(off, size, t, &info); | |
1697 | break; | |
655a51e5 MKL |
1698 | case PTR_TO_TCP_SOCK: |
1699 | valid = bpf_tcp_sock_is_valid_access(off, size, t, &info); | |
1700 | break; | |
46f8bc92 MKL |
1701 | default: |
1702 | valid = false; | |
c64b7983 JS |
1703 | } |
1704 | ||
5f456649 | 1705 | |
46f8bc92 MKL |
1706 | if (valid) { |
1707 | env->insn_aux_data[insn_idx].ctx_field_size = | |
1708 | info.ctx_field_size; | |
1709 | return 0; | |
1710 | } | |
1711 | ||
1712 | verbose(env, "R%d invalid %s access off=%d size=%d\n", | |
1713 | regno, reg_type_str[reg->type], off, size); | |
1714 | ||
1715 | return -EACCES; | |
c64b7983 JS |
1716 | } |
1717 | ||
4cabc5b1 DB |
1718 | static bool __is_pointer_value(bool allow_ptr_leaks, |
1719 | const struct bpf_reg_state *reg) | |
1be7f75d | 1720 | { |
4cabc5b1 | 1721 | if (allow_ptr_leaks) |
1be7f75d AS |
1722 | return false; |
1723 | ||
f1174f77 | 1724 | return reg->type != SCALAR_VALUE; |
1be7f75d AS |
1725 | } |
1726 | ||
2a159c6f DB |
1727 | static struct bpf_reg_state *reg_state(struct bpf_verifier_env *env, int regno) |
1728 | { | |
1729 | return cur_regs(env) + regno; | |
1730 | } | |
1731 | ||
4cabc5b1 DB |
1732 | static bool is_pointer_value(struct bpf_verifier_env *env, int regno) |
1733 | { | |
2a159c6f | 1734 | return __is_pointer_value(env->allow_ptr_leaks, reg_state(env, regno)); |
4cabc5b1 DB |
1735 | } |
1736 | ||
f37a8cb8 DB |
1737 | static bool is_ctx_reg(struct bpf_verifier_env *env, int regno) |
1738 | { | |
2a159c6f | 1739 | const struct bpf_reg_state *reg = reg_state(env, regno); |
f37a8cb8 | 1740 | |
46f8bc92 MKL |
1741 | return reg->type == PTR_TO_CTX; |
1742 | } | |
1743 | ||
1744 | static bool is_sk_reg(struct bpf_verifier_env *env, int regno) | |
1745 | { | |
1746 | const struct bpf_reg_state *reg = reg_state(env, regno); | |
1747 | ||
1748 | return type_is_sk_pointer(reg->type); | |
f37a8cb8 DB |
1749 | } |
1750 | ||
ca369602 DB |
1751 | static bool is_pkt_reg(struct bpf_verifier_env *env, int regno) |
1752 | { | |
2a159c6f | 1753 | const struct bpf_reg_state *reg = reg_state(env, regno); |
ca369602 DB |
1754 | |
1755 | return type_is_pkt_pointer(reg->type); | |
1756 | } | |
1757 | ||
4b5defde DB |
1758 | static bool is_flow_key_reg(struct bpf_verifier_env *env, int regno) |
1759 | { | |
1760 | const struct bpf_reg_state *reg = reg_state(env, regno); | |
1761 | ||
1762 | /* Separate to is_ctx_reg() since we still want to allow BPF_ST here. */ | |
1763 | return reg->type == PTR_TO_FLOW_KEYS; | |
1764 | } | |
1765 | ||
61bd5218 JK |
1766 | static int check_pkt_ptr_alignment(struct bpf_verifier_env *env, |
1767 | const struct bpf_reg_state *reg, | |
d1174416 | 1768 | int off, int size, bool strict) |
969bf05e | 1769 | { |
f1174f77 | 1770 | struct tnum reg_off; |
e07b98d9 | 1771 | int ip_align; |
d1174416 DM |
1772 | |
1773 | /* Byte size accesses are always allowed. */ | |
1774 | if (!strict || size == 1) | |
1775 | return 0; | |
1776 | ||
e4eda884 DM |
1777 | /* For platforms that do not have a Kconfig enabling |
1778 | * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of | |
1779 | * NET_IP_ALIGN is universally set to '2'. And on platforms | |
1780 | * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get | |
1781 | * to this code only in strict mode where we want to emulate | |
1782 | * the NET_IP_ALIGN==2 checking. Therefore use an | |
1783 | * unconditional IP align value of '2'. | |
e07b98d9 | 1784 | */ |
e4eda884 | 1785 | ip_align = 2; |
f1174f77 EC |
1786 | |
1787 | reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off)); | |
1788 | if (!tnum_is_aligned(reg_off, size)) { | |
1789 | char tn_buf[48]; | |
1790 | ||
1791 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
61bd5218 JK |
1792 | verbose(env, |
1793 | "misaligned packet access off %d+%s+%d+%d size %d\n", | |
f1174f77 | 1794 | ip_align, tn_buf, reg->off, off, size); |
969bf05e AS |
1795 | return -EACCES; |
1796 | } | |
79adffcd | 1797 | |
969bf05e AS |
1798 | return 0; |
1799 | } | |
1800 | ||
61bd5218 JK |
1801 | static int check_generic_ptr_alignment(struct bpf_verifier_env *env, |
1802 | const struct bpf_reg_state *reg, | |
f1174f77 EC |
1803 | const char *pointer_desc, |
1804 | int off, int size, bool strict) | |
79adffcd | 1805 | { |
f1174f77 EC |
1806 | struct tnum reg_off; |
1807 | ||
1808 | /* Byte size accesses are always allowed. */ | |
1809 | if (!strict || size == 1) | |
1810 | return 0; | |
1811 | ||
1812 | reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off)); | |
1813 | if (!tnum_is_aligned(reg_off, size)) { | |
1814 | char tn_buf[48]; | |
1815 | ||
1816 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
61bd5218 | 1817 | verbose(env, "misaligned %saccess off %s+%d+%d size %d\n", |
f1174f77 | 1818 | pointer_desc, tn_buf, reg->off, off, size); |
79adffcd DB |
1819 | return -EACCES; |
1820 | } | |
1821 | ||
969bf05e AS |
1822 | return 0; |
1823 | } | |
1824 | ||
e07b98d9 | 1825 | static int check_ptr_alignment(struct bpf_verifier_env *env, |
ca369602 DB |
1826 | const struct bpf_reg_state *reg, int off, |
1827 | int size, bool strict_alignment_once) | |
79adffcd | 1828 | { |
ca369602 | 1829 | bool strict = env->strict_alignment || strict_alignment_once; |
f1174f77 | 1830 | const char *pointer_desc = ""; |
d1174416 | 1831 | |
79adffcd DB |
1832 | switch (reg->type) { |
1833 | case PTR_TO_PACKET: | |
de8f3a83 DB |
1834 | case PTR_TO_PACKET_META: |
1835 | /* Special case, because of NET_IP_ALIGN. Given metadata sits | |
1836 | * right in front, treat it the very same way. | |
1837 | */ | |
61bd5218 | 1838 | return check_pkt_ptr_alignment(env, reg, off, size, strict); |
d58e468b PP |
1839 | case PTR_TO_FLOW_KEYS: |
1840 | pointer_desc = "flow keys "; | |
1841 | break; | |
f1174f77 EC |
1842 | case PTR_TO_MAP_VALUE: |
1843 | pointer_desc = "value "; | |
1844 | break; | |
1845 | case PTR_TO_CTX: | |
1846 | pointer_desc = "context "; | |
1847 | break; | |
1848 | case PTR_TO_STACK: | |
1849 | pointer_desc = "stack "; | |
a5ec6ae1 JH |
1850 | /* The stack spill tracking logic in check_stack_write() |
1851 | * and check_stack_read() relies on stack accesses being | |
1852 | * aligned. | |
1853 | */ | |
1854 | strict = true; | |
f1174f77 | 1855 | break; |
c64b7983 JS |
1856 | case PTR_TO_SOCKET: |
1857 | pointer_desc = "sock "; | |
1858 | break; | |
46f8bc92 MKL |
1859 | case PTR_TO_SOCK_COMMON: |
1860 | pointer_desc = "sock_common "; | |
1861 | break; | |
655a51e5 MKL |
1862 | case PTR_TO_TCP_SOCK: |
1863 | pointer_desc = "tcp_sock "; | |
1864 | break; | |
79adffcd | 1865 | default: |
f1174f77 | 1866 | break; |
79adffcd | 1867 | } |
61bd5218 JK |
1868 | return check_generic_ptr_alignment(env, reg, pointer_desc, off, size, |
1869 | strict); | |
79adffcd DB |
1870 | } |
1871 | ||
f4d7e40a AS |
1872 | static int update_stack_depth(struct bpf_verifier_env *env, |
1873 | const struct bpf_func_state *func, | |
1874 | int off) | |
1875 | { | |
9c8105bd | 1876 | u16 stack = env->subprog_info[func->subprogno].stack_depth; |
f4d7e40a AS |
1877 | |
1878 | if (stack >= -off) | |
1879 | return 0; | |
1880 | ||
1881 | /* update known max for given subprogram */ | |
9c8105bd | 1882 | env->subprog_info[func->subprogno].stack_depth = -off; |
70a87ffe AS |
1883 | return 0; |
1884 | } | |
f4d7e40a | 1885 | |
70a87ffe AS |
1886 | /* starting from main bpf function walk all instructions of the function |
1887 | * and recursively walk all callees that given function can call. | |
1888 | * Ignore jump and exit insns. | |
1889 | * Since recursion is prevented by check_cfg() this algorithm | |
1890 | * only needs a local stack of MAX_CALL_FRAMES to remember callsites | |
1891 | */ | |
1892 | static int check_max_stack_depth(struct bpf_verifier_env *env) | |
1893 | { | |
9c8105bd JW |
1894 | int depth = 0, frame = 0, idx = 0, i = 0, subprog_end; |
1895 | struct bpf_subprog_info *subprog = env->subprog_info; | |
70a87ffe | 1896 | struct bpf_insn *insn = env->prog->insnsi; |
70a87ffe AS |
1897 | int ret_insn[MAX_CALL_FRAMES]; |
1898 | int ret_prog[MAX_CALL_FRAMES]; | |
f4d7e40a | 1899 | |
70a87ffe AS |
1900 | process_func: |
1901 | /* round up to 32-bytes, since this is granularity | |
1902 | * of interpreter stack size | |
1903 | */ | |
9c8105bd | 1904 | depth += round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
70a87ffe | 1905 | if (depth > MAX_BPF_STACK) { |
f4d7e40a | 1906 | verbose(env, "combined stack size of %d calls is %d. Too large\n", |
70a87ffe | 1907 | frame + 1, depth); |
f4d7e40a AS |
1908 | return -EACCES; |
1909 | } | |
70a87ffe | 1910 | continue_func: |
4cb3d99c | 1911 | subprog_end = subprog[idx + 1].start; |
70a87ffe AS |
1912 | for (; i < subprog_end; i++) { |
1913 | if (insn[i].code != (BPF_JMP | BPF_CALL)) | |
1914 | continue; | |
1915 | if (insn[i].src_reg != BPF_PSEUDO_CALL) | |
1916 | continue; | |
1917 | /* remember insn and function to return to */ | |
1918 | ret_insn[frame] = i + 1; | |
9c8105bd | 1919 | ret_prog[frame] = idx; |
70a87ffe AS |
1920 | |
1921 | /* find the callee */ | |
1922 | i = i + insn[i].imm + 1; | |
9c8105bd JW |
1923 | idx = find_subprog(env, i); |
1924 | if (idx < 0) { | |
70a87ffe AS |
1925 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
1926 | i); | |
1927 | return -EFAULT; | |
1928 | } | |
70a87ffe AS |
1929 | frame++; |
1930 | if (frame >= MAX_CALL_FRAMES) { | |
927cb781 PC |
1931 | verbose(env, "the call stack of %d frames is too deep !\n", |
1932 | frame); | |
1933 | return -E2BIG; | |
70a87ffe AS |
1934 | } |
1935 | goto process_func; | |
1936 | } | |
1937 | /* end of for() loop means the last insn of the 'subprog' | |
1938 | * was reached. Doesn't matter whether it was JA or EXIT | |
1939 | */ | |
1940 | if (frame == 0) | |
1941 | return 0; | |
9c8105bd | 1942 | depth -= round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
70a87ffe AS |
1943 | frame--; |
1944 | i = ret_insn[frame]; | |
9c8105bd | 1945 | idx = ret_prog[frame]; |
70a87ffe | 1946 | goto continue_func; |
f4d7e40a AS |
1947 | } |
1948 | ||
19d28fbd | 1949 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
1ea47e01 AS |
1950 | static int get_callee_stack_depth(struct bpf_verifier_env *env, |
1951 | const struct bpf_insn *insn, int idx) | |
1952 | { | |
1953 | int start = idx + insn->imm + 1, subprog; | |
1954 | ||
1955 | subprog = find_subprog(env, start); | |
1956 | if (subprog < 0) { | |
1957 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", | |
1958 | start); | |
1959 | return -EFAULT; | |
1960 | } | |
9c8105bd | 1961 | return env->subprog_info[subprog].stack_depth; |
1ea47e01 | 1962 | } |
19d28fbd | 1963 | #endif |
1ea47e01 | 1964 | |
58990d1f DB |
1965 | static int check_ctx_reg(struct bpf_verifier_env *env, |
1966 | const struct bpf_reg_state *reg, int regno) | |
1967 | { | |
1968 | /* Access to ctx or passing it to a helper is only allowed in | |
1969 | * its original, unmodified form. | |
1970 | */ | |
1971 | ||
1972 | if (reg->off) { | |
1973 | verbose(env, "dereference of modified ctx ptr R%d off=%d disallowed\n", | |
1974 | regno, reg->off); | |
1975 | return -EACCES; | |
1976 | } | |
1977 | ||
1978 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
1979 | char tn_buf[48]; | |
1980 | ||
1981 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
1982 | verbose(env, "variable ctx access var_off=%s disallowed\n", tn_buf); | |
1983 | return -EACCES; | |
1984 | } | |
1985 | ||
1986 | return 0; | |
1987 | } | |
1988 | ||
9df1c28b MM |
1989 | static int check_tp_buffer_access(struct bpf_verifier_env *env, |
1990 | const struct bpf_reg_state *reg, | |
1991 | int regno, int off, int size) | |
1992 | { | |
1993 | if (off < 0) { | |
1994 | verbose(env, | |
1995 | "R%d invalid tracepoint buffer access: off=%d, size=%d", | |
1996 | regno, off, size); | |
1997 | return -EACCES; | |
1998 | } | |
1999 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
2000 | char tn_buf[48]; | |
2001 | ||
2002 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
2003 | verbose(env, | |
2004 | "R%d invalid variable buffer offset: off=%d, var_off=%s", | |
2005 | regno, off, tn_buf); | |
2006 | return -EACCES; | |
2007 | } | |
2008 | if (off + size > env->prog->aux->max_tp_access) | |
2009 | env->prog->aux->max_tp_access = off + size; | |
2010 | ||
2011 | return 0; | |
2012 | } | |
2013 | ||
2014 | ||
0c17d1d2 JH |
2015 | /* truncate register to smaller size (in bytes) |
2016 | * must be called with size < BPF_REG_SIZE | |
2017 | */ | |
2018 | static void coerce_reg_to_size(struct bpf_reg_state *reg, int size) | |
2019 | { | |
2020 | u64 mask; | |
2021 | ||
2022 | /* clear high bits in bit representation */ | |
2023 | reg->var_off = tnum_cast(reg->var_off, size); | |
2024 | ||
2025 | /* fix arithmetic bounds */ | |
2026 | mask = ((u64)1 << (size * 8)) - 1; | |
2027 | if ((reg->umin_value & ~mask) == (reg->umax_value & ~mask)) { | |
2028 | reg->umin_value &= mask; | |
2029 | reg->umax_value &= mask; | |
2030 | } else { | |
2031 | reg->umin_value = 0; | |
2032 | reg->umax_value = mask; | |
2033 | } | |
2034 | reg->smin_value = reg->umin_value; | |
2035 | reg->smax_value = reg->umax_value; | |
2036 | } | |
2037 | ||
17a52670 AS |
2038 | /* check whether memory at (regno + off) is accessible for t = (read | write) |
2039 | * if t==write, value_regno is a register which value is stored into memory | |
2040 | * if t==read, value_regno is a register which will receive the value from memory | |
2041 | * if t==write && value_regno==-1, some unknown value is stored into memory | |
2042 | * if t==read && value_regno==-1, don't care what we read from memory | |
2043 | */ | |
ca369602 DB |
2044 | static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno, |
2045 | int off, int bpf_size, enum bpf_access_type t, | |
2046 | int value_regno, bool strict_alignment_once) | |
17a52670 | 2047 | { |
638f5b90 AS |
2048 | struct bpf_reg_state *regs = cur_regs(env); |
2049 | struct bpf_reg_state *reg = regs + regno; | |
f4d7e40a | 2050 | struct bpf_func_state *state; |
17a52670 AS |
2051 | int size, err = 0; |
2052 | ||
2053 | size = bpf_size_to_bytes(bpf_size); | |
2054 | if (size < 0) | |
2055 | return size; | |
2056 | ||
f1174f77 | 2057 | /* alignment checks will add in reg->off themselves */ |
ca369602 | 2058 | err = check_ptr_alignment(env, reg, off, size, strict_alignment_once); |
969bf05e AS |
2059 | if (err) |
2060 | return err; | |
17a52670 | 2061 | |
f1174f77 EC |
2062 | /* for access checks, reg->off is just part of off */ |
2063 | off += reg->off; | |
2064 | ||
2065 | if (reg->type == PTR_TO_MAP_VALUE) { | |
1be7f75d AS |
2066 | if (t == BPF_WRITE && value_regno >= 0 && |
2067 | is_pointer_value(env, value_regno)) { | |
61bd5218 | 2068 | verbose(env, "R%d leaks addr into map\n", value_regno); |
1be7f75d AS |
2069 | return -EACCES; |
2070 | } | |
591fe988 DB |
2071 | err = check_map_access_type(env, regno, off, size, t); |
2072 | if (err) | |
2073 | return err; | |
9fd29c08 | 2074 | err = check_map_access(env, regno, off, size, false); |
17a52670 | 2075 | if (!err && t == BPF_READ && value_regno >= 0) |
638f5b90 | 2076 | mark_reg_unknown(env, regs, value_regno); |
17a52670 | 2077 | |
1a0dc1ac | 2078 | } else if (reg->type == PTR_TO_CTX) { |
f1174f77 | 2079 | enum bpf_reg_type reg_type = SCALAR_VALUE; |
19de99f7 | 2080 | |
1be7f75d AS |
2081 | if (t == BPF_WRITE && value_regno >= 0 && |
2082 | is_pointer_value(env, value_regno)) { | |
61bd5218 | 2083 | verbose(env, "R%d leaks addr into ctx\n", value_regno); |
1be7f75d AS |
2084 | return -EACCES; |
2085 | } | |
f1174f77 | 2086 | |
58990d1f DB |
2087 | err = check_ctx_reg(env, reg, regno); |
2088 | if (err < 0) | |
2089 | return err; | |
2090 | ||
31fd8581 | 2091 | err = check_ctx_access(env, insn_idx, off, size, t, ®_type); |
969bf05e | 2092 | if (!err && t == BPF_READ && value_regno >= 0) { |
f1174f77 | 2093 | /* ctx access returns either a scalar, or a |
de8f3a83 DB |
2094 | * PTR_TO_PACKET[_META,_END]. In the latter |
2095 | * case, we know the offset is zero. | |
f1174f77 | 2096 | */ |
46f8bc92 | 2097 | if (reg_type == SCALAR_VALUE) { |
638f5b90 | 2098 | mark_reg_unknown(env, regs, value_regno); |
46f8bc92 | 2099 | } else { |
638f5b90 | 2100 | mark_reg_known_zero(env, regs, |
61bd5218 | 2101 | value_regno); |
46f8bc92 MKL |
2102 | if (reg_type_may_be_null(reg_type)) |
2103 | regs[value_regno].id = ++env->id_gen; | |
2104 | } | |
638f5b90 | 2105 | regs[value_regno].type = reg_type; |
969bf05e | 2106 | } |
17a52670 | 2107 | |
f1174f77 | 2108 | } else if (reg->type == PTR_TO_STACK) { |
f1174f77 | 2109 | off += reg->var_off.value; |
e4298d25 DB |
2110 | err = check_stack_access(env, reg, off, size); |
2111 | if (err) | |
2112 | return err; | |
8726679a | 2113 | |
f4d7e40a AS |
2114 | state = func(env, reg); |
2115 | err = update_stack_depth(env, state, off); | |
2116 | if (err) | |
2117 | return err; | |
8726679a | 2118 | |
638f5b90 | 2119 | if (t == BPF_WRITE) |
61bd5218 | 2120 | err = check_stack_write(env, state, off, size, |
af86ca4e | 2121 | value_regno, insn_idx); |
638f5b90 | 2122 | else |
61bd5218 JK |
2123 | err = check_stack_read(env, state, off, size, |
2124 | value_regno); | |
de8f3a83 | 2125 | } else if (reg_is_pkt_pointer(reg)) { |
3a0af8fd | 2126 | if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) { |
61bd5218 | 2127 | verbose(env, "cannot write into packet\n"); |
969bf05e AS |
2128 | return -EACCES; |
2129 | } | |
4acf6c0b BB |
2130 | if (t == BPF_WRITE && value_regno >= 0 && |
2131 | is_pointer_value(env, value_regno)) { | |
61bd5218 JK |
2132 | verbose(env, "R%d leaks addr into packet\n", |
2133 | value_regno); | |
4acf6c0b BB |
2134 | return -EACCES; |
2135 | } | |
9fd29c08 | 2136 | err = check_packet_access(env, regno, off, size, false); |
969bf05e | 2137 | if (!err && t == BPF_READ && value_regno >= 0) |
638f5b90 | 2138 | mark_reg_unknown(env, regs, value_regno); |
d58e468b PP |
2139 | } else if (reg->type == PTR_TO_FLOW_KEYS) { |
2140 | if (t == BPF_WRITE && value_regno >= 0 && | |
2141 | is_pointer_value(env, value_regno)) { | |
2142 | verbose(env, "R%d leaks addr into flow keys\n", | |
2143 | value_regno); | |
2144 | return -EACCES; | |
2145 | } | |
2146 | ||
2147 | err = check_flow_keys_access(env, off, size); | |
2148 | if (!err && t == BPF_READ && value_regno >= 0) | |
2149 | mark_reg_unknown(env, regs, value_regno); | |
46f8bc92 | 2150 | } else if (type_is_sk_pointer(reg->type)) { |
c64b7983 | 2151 | if (t == BPF_WRITE) { |
46f8bc92 MKL |
2152 | verbose(env, "R%d cannot write into %s\n", |
2153 | regno, reg_type_str[reg->type]); | |
c64b7983 JS |
2154 | return -EACCES; |
2155 | } | |
5f456649 | 2156 | err = check_sock_access(env, insn_idx, regno, off, size, t); |
c64b7983 JS |
2157 | if (!err && value_regno >= 0) |
2158 | mark_reg_unknown(env, regs, value_regno); | |
9df1c28b MM |
2159 | } else if (reg->type == PTR_TO_TP_BUFFER) { |
2160 | err = check_tp_buffer_access(env, reg, regno, off, size); | |
2161 | if (!err && t == BPF_READ && value_regno >= 0) | |
2162 | mark_reg_unknown(env, regs, value_regno); | |
17a52670 | 2163 | } else { |
61bd5218 JK |
2164 | verbose(env, "R%d invalid mem access '%s'\n", regno, |
2165 | reg_type_str[reg->type]); | |
17a52670 AS |
2166 | return -EACCES; |
2167 | } | |
969bf05e | 2168 | |
f1174f77 | 2169 | if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ && |
638f5b90 | 2170 | regs[value_regno].type == SCALAR_VALUE) { |
f1174f77 | 2171 | /* b/h/w load zero-extends, mark upper bits as known 0 */ |
0c17d1d2 | 2172 | coerce_reg_to_size(®s[value_regno], size); |
969bf05e | 2173 | } |
17a52670 AS |
2174 | return err; |
2175 | } | |
2176 | ||
31fd8581 | 2177 | static int check_xadd(struct bpf_verifier_env *env, int insn_idx, struct bpf_insn *insn) |
17a52670 | 2178 | { |
17a52670 AS |
2179 | int err; |
2180 | ||
2181 | if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) || | |
2182 | insn->imm != 0) { | |
61bd5218 | 2183 | verbose(env, "BPF_XADD uses reserved fields\n"); |
17a52670 AS |
2184 | return -EINVAL; |
2185 | } | |
2186 | ||
2187 | /* check src1 operand */ | |
dc503a8a | 2188 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
2189 | if (err) |
2190 | return err; | |
2191 | ||
2192 | /* check src2 operand */ | |
dc503a8a | 2193 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
2194 | if (err) |
2195 | return err; | |
2196 | ||
6bdf6abc | 2197 | if (is_pointer_value(env, insn->src_reg)) { |
61bd5218 | 2198 | verbose(env, "R%d leaks addr into mem\n", insn->src_reg); |
6bdf6abc DB |
2199 | return -EACCES; |
2200 | } | |
2201 | ||
ca369602 | 2202 | if (is_ctx_reg(env, insn->dst_reg) || |
4b5defde | 2203 | is_pkt_reg(env, insn->dst_reg) || |
46f8bc92 MKL |
2204 | is_flow_key_reg(env, insn->dst_reg) || |
2205 | is_sk_reg(env, insn->dst_reg)) { | |
ca369602 | 2206 | verbose(env, "BPF_XADD stores into R%d %s is not allowed\n", |
2a159c6f DB |
2207 | insn->dst_reg, |
2208 | reg_type_str[reg_state(env, insn->dst_reg)->type]); | |
f37a8cb8 DB |
2209 | return -EACCES; |
2210 | } | |
2211 | ||
17a52670 | 2212 | /* check whether atomic_add can read the memory */ |
31fd8581 | 2213 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
ca369602 | 2214 | BPF_SIZE(insn->code), BPF_READ, -1, true); |
17a52670 AS |
2215 | if (err) |
2216 | return err; | |
2217 | ||
2218 | /* check whether atomic_add can write into the same memory */ | |
31fd8581 | 2219 | return check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
ca369602 | 2220 | BPF_SIZE(insn->code), BPF_WRITE, -1, true); |
17a52670 AS |
2221 | } |
2222 | ||
2011fccf AI |
2223 | static int __check_stack_boundary(struct bpf_verifier_env *env, u32 regno, |
2224 | int off, int access_size, | |
2225 | bool zero_size_allowed) | |
2226 | { | |
2227 | struct bpf_reg_state *reg = reg_state(env, regno); | |
2228 | ||
2229 | if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 || | |
2230 | access_size < 0 || (access_size == 0 && !zero_size_allowed)) { | |
2231 | if (tnum_is_const(reg->var_off)) { | |
2232 | verbose(env, "invalid stack type R%d off=%d access_size=%d\n", | |
2233 | regno, off, access_size); | |
2234 | } else { | |
2235 | char tn_buf[48]; | |
2236 | ||
2237 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
2238 | verbose(env, "invalid stack type R%d var_off=%s access_size=%d\n", | |
2239 | regno, tn_buf, access_size); | |
2240 | } | |
2241 | return -EACCES; | |
2242 | } | |
2243 | return 0; | |
2244 | } | |
2245 | ||
17a52670 AS |
2246 | /* when register 'regno' is passed into function that will read 'access_size' |
2247 | * bytes from that pointer, make sure that it's within stack boundary | |
f1174f77 EC |
2248 | * and all elements of stack are initialized. |
2249 | * Unlike most pointer bounds-checking functions, this one doesn't take an | |
2250 | * 'off' argument, so it has to add in reg->off itself. | |
17a52670 | 2251 | */ |
58e2af8b | 2252 | static int check_stack_boundary(struct bpf_verifier_env *env, int regno, |
435faee1 DB |
2253 | int access_size, bool zero_size_allowed, |
2254 | struct bpf_call_arg_meta *meta) | |
17a52670 | 2255 | { |
2a159c6f | 2256 | struct bpf_reg_state *reg = reg_state(env, regno); |
f4d7e40a | 2257 | struct bpf_func_state *state = func(env, reg); |
2011fccf | 2258 | int err, min_off, max_off, i, slot, spi; |
17a52670 | 2259 | |
914cb781 | 2260 | if (reg->type != PTR_TO_STACK) { |
f1174f77 | 2261 | /* Allow zero-byte read from NULL, regardless of pointer type */ |
8e2fe1d9 | 2262 | if (zero_size_allowed && access_size == 0 && |
914cb781 | 2263 | register_is_null(reg)) |
8e2fe1d9 DB |
2264 | return 0; |
2265 | ||
61bd5218 | 2266 | verbose(env, "R%d type=%s expected=%s\n", regno, |
914cb781 | 2267 | reg_type_str[reg->type], |
8e2fe1d9 | 2268 | reg_type_str[PTR_TO_STACK]); |
17a52670 | 2269 | return -EACCES; |
8e2fe1d9 | 2270 | } |
17a52670 | 2271 | |
2011fccf AI |
2272 | if (tnum_is_const(reg->var_off)) { |
2273 | min_off = max_off = reg->var_off.value + reg->off; | |
2274 | err = __check_stack_boundary(env, regno, min_off, access_size, | |
2275 | zero_size_allowed); | |
2276 | if (err) | |
2277 | return err; | |
2278 | } else { | |
088ec26d AI |
2279 | /* Variable offset is prohibited for unprivileged mode for |
2280 | * simplicity since it requires corresponding support in | |
2281 | * Spectre masking for stack ALU. | |
2282 | * See also retrieve_ptr_limit(). | |
2283 | */ | |
2284 | if (!env->allow_ptr_leaks) { | |
2285 | char tn_buf[48]; | |
f1174f77 | 2286 | |
088ec26d AI |
2287 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
2288 | verbose(env, "R%d indirect variable offset stack access prohibited for !root, var_off=%s\n", | |
2289 | regno, tn_buf); | |
2290 | return -EACCES; | |
2291 | } | |
f2bcd05e AI |
2292 | /* Only initialized buffer on stack is allowed to be accessed |
2293 | * with variable offset. With uninitialized buffer it's hard to | |
2294 | * guarantee that whole memory is marked as initialized on | |
2295 | * helper return since specific bounds are unknown what may | |
2296 | * cause uninitialized stack leaking. | |
2297 | */ | |
2298 | if (meta && meta->raw_mode) | |
2299 | meta = NULL; | |
2300 | ||
107c26a7 AI |
2301 | if (reg->smax_value >= BPF_MAX_VAR_OFF || |
2302 | reg->smax_value <= -BPF_MAX_VAR_OFF) { | |
2303 | verbose(env, "R%d unbounded indirect variable offset stack access\n", | |
2304 | regno); | |
2305 | return -EACCES; | |
2306 | } | |
2011fccf | 2307 | min_off = reg->smin_value + reg->off; |
107c26a7 | 2308 | max_off = reg->smax_value + reg->off; |
2011fccf AI |
2309 | err = __check_stack_boundary(env, regno, min_off, access_size, |
2310 | zero_size_allowed); | |
107c26a7 AI |
2311 | if (err) { |
2312 | verbose(env, "R%d min value is outside of stack bound\n", | |
2313 | regno); | |
2011fccf | 2314 | return err; |
107c26a7 | 2315 | } |
2011fccf AI |
2316 | err = __check_stack_boundary(env, regno, max_off, access_size, |
2317 | zero_size_allowed); | |
107c26a7 AI |
2318 | if (err) { |
2319 | verbose(env, "R%d max value is outside of stack bound\n", | |
2320 | regno); | |
2011fccf | 2321 | return err; |
107c26a7 | 2322 | } |
17a52670 AS |
2323 | } |
2324 | ||
435faee1 DB |
2325 | if (meta && meta->raw_mode) { |
2326 | meta->access_size = access_size; | |
2327 | meta->regno = regno; | |
2328 | return 0; | |
2329 | } | |
2330 | ||
2011fccf | 2331 | for (i = min_off; i < max_off + access_size; i++) { |
cc2b14d5 AS |
2332 | u8 *stype; |
2333 | ||
2011fccf | 2334 | slot = -i - 1; |
638f5b90 | 2335 | spi = slot / BPF_REG_SIZE; |
cc2b14d5 AS |
2336 | if (state->allocated_stack <= slot) |
2337 | goto err; | |
2338 | stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE]; | |
2339 | if (*stype == STACK_MISC) | |
2340 | goto mark; | |
2341 | if (*stype == STACK_ZERO) { | |
2342 | /* helper can write anything into the stack */ | |
2343 | *stype = STACK_MISC; | |
2344 | goto mark; | |
17a52670 | 2345 | } |
cc2b14d5 | 2346 | err: |
2011fccf AI |
2347 | if (tnum_is_const(reg->var_off)) { |
2348 | verbose(env, "invalid indirect read from stack off %d+%d size %d\n", | |
2349 | min_off, i - min_off, access_size); | |
2350 | } else { | |
2351 | char tn_buf[48]; | |
2352 | ||
2353 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
2354 | verbose(env, "invalid indirect read from stack var_off %s+%d size %d\n", | |
2355 | tn_buf, i - min_off, access_size); | |
2356 | } | |
cc2b14d5 AS |
2357 | return -EACCES; |
2358 | mark: | |
2359 | /* reading any byte out of 8-byte 'spill_slot' will cause | |
2360 | * the whole slot to be marked as 'read' | |
2361 | */ | |
679c782d EC |
2362 | mark_reg_read(env, &state->stack[spi].spilled_ptr, |
2363 | state->stack[spi].spilled_ptr.parent); | |
17a52670 | 2364 | } |
2011fccf | 2365 | return update_stack_depth(env, state, min_off); |
17a52670 AS |
2366 | } |
2367 | ||
06c1c049 GB |
2368 | static int check_helper_mem_access(struct bpf_verifier_env *env, int regno, |
2369 | int access_size, bool zero_size_allowed, | |
2370 | struct bpf_call_arg_meta *meta) | |
2371 | { | |
638f5b90 | 2372 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
06c1c049 | 2373 | |
f1174f77 | 2374 | switch (reg->type) { |
06c1c049 | 2375 | case PTR_TO_PACKET: |
de8f3a83 | 2376 | case PTR_TO_PACKET_META: |
9fd29c08 YS |
2377 | return check_packet_access(env, regno, reg->off, access_size, |
2378 | zero_size_allowed); | |
06c1c049 | 2379 | case PTR_TO_MAP_VALUE: |
591fe988 DB |
2380 | if (check_map_access_type(env, regno, reg->off, access_size, |
2381 | meta && meta->raw_mode ? BPF_WRITE : | |
2382 | BPF_READ)) | |
2383 | return -EACCES; | |
9fd29c08 YS |
2384 | return check_map_access(env, regno, reg->off, access_size, |
2385 | zero_size_allowed); | |
f1174f77 | 2386 | default: /* scalar_value|ptr_to_stack or invalid ptr */ |
06c1c049 GB |
2387 | return check_stack_boundary(env, regno, access_size, |
2388 | zero_size_allowed, meta); | |
2389 | } | |
2390 | } | |
2391 | ||
d83525ca AS |
2392 | /* Implementation details: |
2393 | * bpf_map_lookup returns PTR_TO_MAP_VALUE_OR_NULL | |
2394 | * Two bpf_map_lookups (even with the same key) will have different reg->id. | |
2395 | * For traditional PTR_TO_MAP_VALUE the verifier clears reg->id after | |
2396 | * value_or_null->value transition, since the verifier only cares about | |
2397 | * the range of access to valid map value pointer and doesn't care about actual | |
2398 | * address of the map element. | |
2399 | * For maps with 'struct bpf_spin_lock' inside map value the verifier keeps | |
2400 | * reg->id > 0 after value_or_null->value transition. By doing so | |
2401 | * two bpf_map_lookups will be considered two different pointers that | |
2402 | * point to different bpf_spin_locks. | |
2403 | * The verifier allows taking only one bpf_spin_lock at a time to avoid | |
2404 | * dead-locks. | |
2405 | * Since only one bpf_spin_lock is allowed the checks are simpler than | |
2406 | * reg_is_refcounted() logic. The verifier needs to remember only | |
2407 | * one spin_lock instead of array of acquired_refs. | |
2408 | * cur_state->active_spin_lock remembers which map value element got locked | |
2409 | * and clears it after bpf_spin_unlock. | |
2410 | */ | |
2411 | static int process_spin_lock(struct bpf_verifier_env *env, int regno, | |
2412 | bool is_lock) | |
2413 | { | |
2414 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; | |
2415 | struct bpf_verifier_state *cur = env->cur_state; | |
2416 | bool is_const = tnum_is_const(reg->var_off); | |
2417 | struct bpf_map *map = reg->map_ptr; | |
2418 | u64 val = reg->var_off.value; | |
2419 | ||
2420 | if (reg->type != PTR_TO_MAP_VALUE) { | |
2421 | verbose(env, "R%d is not a pointer to map_value\n", regno); | |
2422 | return -EINVAL; | |
2423 | } | |
2424 | if (!is_const) { | |
2425 | verbose(env, | |
2426 | "R%d doesn't have constant offset. bpf_spin_lock has to be at the constant offset\n", | |
2427 | regno); | |
2428 | return -EINVAL; | |
2429 | } | |
2430 | if (!map->btf) { | |
2431 | verbose(env, | |
2432 | "map '%s' has to have BTF in order to use bpf_spin_lock\n", | |
2433 | map->name); | |
2434 | return -EINVAL; | |
2435 | } | |
2436 | if (!map_value_has_spin_lock(map)) { | |
2437 | if (map->spin_lock_off == -E2BIG) | |
2438 | verbose(env, | |
2439 | "map '%s' has more than one 'struct bpf_spin_lock'\n", | |
2440 | map->name); | |
2441 | else if (map->spin_lock_off == -ENOENT) | |
2442 | verbose(env, | |
2443 | "map '%s' doesn't have 'struct bpf_spin_lock'\n", | |
2444 | map->name); | |
2445 | else | |
2446 | verbose(env, | |
2447 | "map '%s' is not a struct type or bpf_spin_lock is mangled\n", | |
2448 | map->name); | |
2449 | return -EINVAL; | |
2450 | } | |
2451 | if (map->spin_lock_off != val + reg->off) { | |
2452 | verbose(env, "off %lld doesn't point to 'struct bpf_spin_lock'\n", | |
2453 | val + reg->off); | |
2454 | return -EINVAL; | |
2455 | } | |
2456 | if (is_lock) { | |
2457 | if (cur->active_spin_lock) { | |
2458 | verbose(env, | |
2459 | "Locking two bpf_spin_locks are not allowed\n"); | |
2460 | return -EINVAL; | |
2461 | } | |
2462 | cur->active_spin_lock = reg->id; | |
2463 | } else { | |
2464 | if (!cur->active_spin_lock) { | |
2465 | verbose(env, "bpf_spin_unlock without taking a lock\n"); | |
2466 | return -EINVAL; | |
2467 | } | |
2468 | if (cur->active_spin_lock != reg->id) { | |
2469 | verbose(env, "bpf_spin_unlock of different lock\n"); | |
2470 | return -EINVAL; | |
2471 | } | |
2472 | cur->active_spin_lock = 0; | |
2473 | } | |
2474 | return 0; | |
2475 | } | |
2476 | ||
90133415 DB |
2477 | static bool arg_type_is_mem_ptr(enum bpf_arg_type type) |
2478 | { | |
2479 | return type == ARG_PTR_TO_MEM || | |
2480 | type == ARG_PTR_TO_MEM_OR_NULL || | |
2481 | type == ARG_PTR_TO_UNINIT_MEM; | |
2482 | } | |
2483 | ||
2484 | static bool arg_type_is_mem_size(enum bpf_arg_type type) | |
2485 | { | |
2486 | return type == ARG_CONST_SIZE || | |
2487 | type == ARG_CONST_SIZE_OR_ZERO; | |
2488 | } | |
2489 | ||
57c3bb72 AI |
2490 | static bool arg_type_is_int_ptr(enum bpf_arg_type type) |
2491 | { | |
2492 | return type == ARG_PTR_TO_INT || | |
2493 | type == ARG_PTR_TO_LONG; | |
2494 | } | |
2495 | ||
2496 | static int int_ptr_type_to_size(enum bpf_arg_type type) | |
2497 | { | |
2498 | if (type == ARG_PTR_TO_INT) | |
2499 | return sizeof(u32); | |
2500 | else if (type == ARG_PTR_TO_LONG) | |
2501 | return sizeof(u64); | |
2502 | ||
2503 | return -EINVAL; | |
2504 | } | |
2505 | ||
58e2af8b | 2506 | static int check_func_arg(struct bpf_verifier_env *env, u32 regno, |
33ff9823 DB |
2507 | enum bpf_arg_type arg_type, |
2508 | struct bpf_call_arg_meta *meta) | |
17a52670 | 2509 | { |
638f5b90 | 2510 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
6841de8b | 2511 | enum bpf_reg_type expected_type, type = reg->type; |
17a52670 AS |
2512 | int err = 0; |
2513 | ||
80f1d68c | 2514 | if (arg_type == ARG_DONTCARE) |
17a52670 AS |
2515 | return 0; |
2516 | ||
dc503a8a EC |
2517 | err = check_reg_arg(env, regno, SRC_OP); |
2518 | if (err) | |
2519 | return err; | |
17a52670 | 2520 | |
1be7f75d AS |
2521 | if (arg_type == ARG_ANYTHING) { |
2522 | if (is_pointer_value(env, regno)) { | |
61bd5218 JK |
2523 | verbose(env, "R%d leaks addr into helper function\n", |
2524 | regno); | |
1be7f75d AS |
2525 | return -EACCES; |
2526 | } | |
80f1d68c | 2527 | return 0; |
1be7f75d | 2528 | } |
80f1d68c | 2529 | |
de8f3a83 | 2530 | if (type_is_pkt_pointer(type) && |
3a0af8fd | 2531 | !may_access_direct_pkt_data(env, meta, BPF_READ)) { |
61bd5218 | 2532 | verbose(env, "helper access to the packet is not allowed\n"); |
6841de8b AS |
2533 | return -EACCES; |
2534 | } | |
2535 | ||
8e2fe1d9 | 2536 | if (arg_type == ARG_PTR_TO_MAP_KEY || |
2ea864c5 | 2537 | arg_type == ARG_PTR_TO_MAP_VALUE || |
6ac99e8f MKL |
2538 | arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE || |
2539 | arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL) { | |
17a52670 | 2540 | expected_type = PTR_TO_STACK; |
6ac99e8f MKL |
2541 | if (register_is_null(reg) && |
2542 | arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL) | |
2543 | /* final test in check_stack_boundary() */; | |
2544 | else if (!type_is_pkt_pointer(type) && | |
2545 | type != PTR_TO_MAP_VALUE && | |
2546 | type != expected_type) | |
6841de8b | 2547 | goto err_type; |
39f19ebb AS |
2548 | } else if (arg_type == ARG_CONST_SIZE || |
2549 | arg_type == ARG_CONST_SIZE_OR_ZERO) { | |
f1174f77 EC |
2550 | expected_type = SCALAR_VALUE; |
2551 | if (type != expected_type) | |
6841de8b | 2552 | goto err_type; |
17a52670 AS |
2553 | } else if (arg_type == ARG_CONST_MAP_PTR) { |
2554 | expected_type = CONST_PTR_TO_MAP; | |
6841de8b AS |
2555 | if (type != expected_type) |
2556 | goto err_type; | |
608cd71a AS |
2557 | } else if (arg_type == ARG_PTR_TO_CTX) { |
2558 | expected_type = PTR_TO_CTX; | |
6841de8b AS |
2559 | if (type != expected_type) |
2560 | goto err_type; | |
58990d1f DB |
2561 | err = check_ctx_reg(env, reg, regno); |
2562 | if (err < 0) | |
2563 | return err; | |
46f8bc92 MKL |
2564 | } else if (arg_type == ARG_PTR_TO_SOCK_COMMON) { |
2565 | expected_type = PTR_TO_SOCK_COMMON; | |
2566 | /* Any sk pointer can be ARG_PTR_TO_SOCK_COMMON */ | |
2567 | if (!type_is_sk_pointer(type)) | |
2568 | goto err_type; | |
1b986589 MKL |
2569 | if (reg->ref_obj_id) { |
2570 | if (meta->ref_obj_id) { | |
2571 | verbose(env, "verifier internal error: more than one arg with ref_obj_id R%d %u %u\n", | |
2572 | regno, reg->ref_obj_id, | |
2573 | meta->ref_obj_id); | |
2574 | return -EFAULT; | |
2575 | } | |
2576 | meta->ref_obj_id = reg->ref_obj_id; | |
fd978bf7 | 2577 | } |
6ac99e8f MKL |
2578 | } else if (arg_type == ARG_PTR_TO_SOCKET) { |
2579 | expected_type = PTR_TO_SOCKET; | |
2580 | if (type != expected_type) | |
2581 | goto err_type; | |
d83525ca AS |
2582 | } else if (arg_type == ARG_PTR_TO_SPIN_LOCK) { |
2583 | if (meta->func_id == BPF_FUNC_spin_lock) { | |
2584 | if (process_spin_lock(env, regno, true)) | |
2585 | return -EACCES; | |
2586 | } else if (meta->func_id == BPF_FUNC_spin_unlock) { | |
2587 | if (process_spin_lock(env, regno, false)) | |
2588 | return -EACCES; | |
2589 | } else { | |
2590 | verbose(env, "verifier internal error\n"); | |
2591 | return -EFAULT; | |
2592 | } | |
90133415 | 2593 | } else if (arg_type_is_mem_ptr(arg_type)) { |
8e2fe1d9 DB |
2594 | expected_type = PTR_TO_STACK; |
2595 | /* One exception here. In case function allows for NULL to be | |
f1174f77 | 2596 | * passed in as argument, it's a SCALAR_VALUE type. Final test |
8e2fe1d9 DB |
2597 | * happens during stack boundary checking. |
2598 | */ | |
914cb781 | 2599 | if (register_is_null(reg) && |
db1ac496 | 2600 | arg_type == ARG_PTR_TO_MEM_OR_NULL) |
6841de8b | 2601 | /* final test in check_stack_boundary() */; |
de8f3a83 DB |
2602 | else if (!type_is_pkt_pointer(type) && |
2603 | type != PTR_TO_MAP_VALUE && | |
f1174f77 | 2604 | type != expected_type) |
6841de8b | 2605 | goto err_type; |
39f19ebb | 2606 | meta->raw_mode = arg_type == ARG_PTR_TO_UNINIT_MEM; |
57c3bb72 AI |
2607 | } else if (arg_type_is_int_ptr(arg_type)) { |
2608 | expected_type = PTR_TO_STACK; | |
2609 | if (!type_is_pkt_pointer(type) && | |
2610 | type != PTR_TO_MAP_VALUE && | |
2611 | type != expected_type) | |
2612 | goto err_type; | |
17a52670 | 2613 | } else { |
61bd5218 | 2614 | verbose(env, "unsupported arg_type %d\n", arg_type); |
17a52670 AS |
2615 | return -EFAULT; |
2616 | } | |
2617 | ||
17a52670 AS |
2618 | if (arg_type == ARG_CONST_MAP_PTR) { |
2619 | /* bpf_map_xxx(map_ptr) call: remember that map_ptr */ | |
33ff9823 | 2620 | meta->map_ptr = reg->map_ptr; |
17a52670 AS |
2621 | } else if (arg_type == ARG_PTR_TO_MAP_KEY) { |
2622 | /* bpf_map_xxx(..., map_ptr, ..., key) call: | |
2623 | * check that [key, key + map->key_size) are within | |
2624 | * stack limits and initialized | |
2625 | */ | |
33ff9823 | 2626 | if (!meta->map_ptr) { |
17a52670 AS |
2627 | /* in function declaration map_ptr must come before |
2628 | * map_key, so that it's verified and known before | |
2629 | * we have to check map_key here. Otherwise it means | |
2630 | * that kernel subsystem misconfigured verifier | |
2631 | */ | |
61bd5218 | 2632 | verbose(env, "invalid map_ptr to access map->key\n"); |
17a52670 AS |
2633 | return -EACCES; |
2634 | } | |
d71962f3 PC |
2635 | err = check_helper_mem_access(env, regno, |
2636 | meta->map_ptr->key_size, false, | |
2637 | NULL); | |
2ea864c5 | 2638 | } else if (arg_type == ARG_PTR_TO_MAP_VALUE || |
6ac99e8f MKL |
2639 | (arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL && |
2640 | !register_is_null(reg)) || | |
2ea864c5 | 2641 | arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE) { |
17a52670 AS |
2642 | /* bpf_map_xxx(..., map_ptr, ..., value) call: |
2643 | * check [value, value + map->value_size) validity | |
2644 | */ | |
33ff9823 | 2645 | if (!meta->map_ptr) { |
17a52670 | 2646 | /* kernel subsystem misconfigured verifier */ |
61bd5218 | 2647 | verbose(env, "invalid map_ptr to access map->value\n"); |
17a52670 AS |
2648 | return -EACCES; |
2649 | } | |
2ea864c5 | 2650 | meta->raw_mode = (arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE); |
d71962f3 PC |
2651 | err = check_helper_mem_access(env, regno, |
2652 | meta->map_ptr->value_size, false, | |
2ea864c5 | 2653 | meta); |
90133415 | 2654 | } else if (arg_type_is_mem_size(arg_type)) { |
39f19ebb | 2655 | bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO); |
17a52670 | 2656 | |
849fa506 YS |
2657 | /* remember the mem_size which may be used later |
2658 | * to refine return values. | |
2659 | */ | |
2660 | meta->msize_smax_value = reg->smax_value; | |
2661 | meta->msize_umax_value = reg->umax_value; | |
2662 | ||
f1174f77 EC |
2663 | /* The register is SCALAR_VALUE; the access check |
2664 | * happens using its boundaries. | |
06c1c049 | 2665 | */ |
f1174f77 | 2666 | if (!tnum_is_const(reg->var_off)) |
06c1c049 GB |
2667 | /* For unprivileged variable accesses, disable raw |
2668 | * mode so that the program is required to | |
2669 | * initialize all the memory that the helper could | |
2670 | * just partially fill up. | |
2671 | */ | |
2672 | meta = NULL; | |
2673 | ||
b03c9f9f | 2674 | if (reg->smin_value < 0) { |
61bd5218 | 2675 | verbose(env, "R%d min value is negative, either use unsigned or 'var &= const'\n", |
f1174f77 EC |
2676 | regno); |
2677 | return -EACCES; | |
2678 | } | |
06c1c049 | 2679 | |
b03c9f9f | 2680 | if (reg->umin_value == 0) { |
f1174f77 EC |
2681 | err = check_helper_mem_access(env, regno - 1, 0, |
2682 | zero_size_allowed, | |
2683 | meta); | |
06c1c049 GB |
2684 | if (err) |
2685 | return err; | |
06c1c049 | 2686 | } |
f1174f77 | 2687 | |
b03c9f9f | 2688 | if (reg->umax_value >= BPF_MAX_VAR_SIZ) { |
61bd5218 | 2689 | verbose(env, "R%d unbounded memory access, use 'var &= const' or 'if (var < const)'\n", |
f1174f77 EC |
2690 | regno); |
2691 | return -EACCES; | |
2692 | } | |
2693 | err = check_helper_mem_access(env, regno - 1, | |
b03c9f9f | 2694 | reg->umax_value, |
f1174f77 | 2695 | zero_size_allowed, meta); |
57c3bb72 AI |
2696 | } else if (arg_type_is_int_ptr(arg_type)) { |
2697 | int size = int_ptr_type_to_size(arg_type); | |
2698 | ||
2699 | err = check_helper_mem_access(env, regno, size, false, meta); | |
2700 | if (err) | |
2701 | return err; | |
2702 | err = check_ptr_alignment(env, reg, 0, size, true); | |
17a52670 AS |
2703 | } |
2704 | ||
2705 | return err; | |
6841de8b | 2706 | err_type: |
61bd5218 | 2707 | verbose(env, "R%d type=%s expected=%s\n", regno, |
6841de8b AS |
2708 | reg_type_str[type], reg_type_str[expected_type]); |
2709 | return -EACCES; | |
17a52670 AS |
2710 | } |
2711 | ||
61bd5218 JK |
2712 | static int check_map_func_compatibility(struct bpf_verifier_env *env, |
2713 | struct bpf_map *map, int func_id) | |
35578d79 | 2714 | { |
35578d79 KX |
2715 | if (!map) |
2716 | return 0; | |
2717 | ||
6aff67c8 AS |
2718 | /* We need a two way check, first is from map perspective ... */ |
2719 | switch (map->map_type) { | |
2720 | case BPF_MAP_TYPE_PROG_ARRAY: | |
2721 | if (func_id != BPF_FUNC_tail_call) | |
2722 | goto error; | |
2723 | break; | |
2724 | case BPF_MAP_TYPE_PERF_EVENT_ARRAY: | |
2725 | if (func_id != BPF_FUNC_perf_event_read && | |
908432ca YS |
2726 | func_id != BPF_FUNC_perf_event_output && |
2727 | func_id != BPF_FUNC_perf_event_read_value) | |
6aff67c8 AS |
2728 | goto error; |
2729 | break; | |
2730 | case BPF_MAP_TYPE_STACK_TRACE: | |
2731 | if (func_id != BPF_FUNC_get_stackid) | |
2732 | goto error; | |
2733 | break; | |
4ed8ec52 | 2734 | case BPF_MAP_TYPE_CGROUP_ARRAY: |
60747ef4 | 2735 | if (func_id != BPF_FUNC_skb_under_cgroup && |
60d20f91 | 2736 | func_id != BPF_FUNC_current_task_under_cgroup) |
4a482f34 MKL |
2737 | goto error; |
2738 | break; | |
cd339431 | 2739 | case BPF_MAP_TYPE_CGROUP_STORAGE: |
b741f163 | 2740 | case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE: |
cd339431 RG |
2741 | if (func_id != BPF_FUNC_get_local_storage) |
2742 | goto error; | |
2743 | break; | |
546ac1ff JF |
2744 | /* devmap returns a pointer to a live net_device ifindex that we cannot |
2745 | * allow to be modified from bpf side. So do not allow lookup elements | |
2746 | * for now. | |
2747 | */ | |
2748 | case BPF_MAP_TYPE_DEVMAP: | |
2ddf71e2 | 2749 | if (func_id != BPF_FUNC_redirect_map) |
546ac1ff JF |
2750 | goto error; |
2751 | break; | |
fbfc504a BT |
2752 | /* Restrict bpf side of cpumap and xskmap, open when use-cases |
2753 | * appear. | |
2754 | */ | |
6710e112 | 2755 | case BPF_MAP_TYPE_CPUMAP: |
fbfc504a | 2756 | case BPF_MAP_TYPE_XSKMAP: |
6710e112 JDB |
2757 | if (func_id != BPF_FUNC_redirect_map) |
2758 | goto error; | |
2759 | break; | |
56f668df | 2760 | case BPF_MAP_TYPE_ARRAY_OF_MAPS: |
bcc6b1b7 | 2761 | case BPF_MAP_TYPE_HASH_OF_MAPS: |
56f668df MKL |
2762 | if (func_id != BPF_FUNC_map_lookup_elem) |
2763 | goto error; | |
16a43625 | 2764 | break; |
174a79ff JF |
2765 | case BPF_MAP_TYPE_SOCKMAP: |
2766 | if (func_id != BPF_FUNC_sk_redirect_map && | |
2767 | func_id != BPF_FUNC_sock_map_update && | |
4f738adb JF |
2768 | func_id != BPF_FUNC_map_delete_elem && |
2769 | func_id != BPF_FUNC_msg_redirect_map) | |
174a79ff JF |
2770 | goto error; |
2771 | break; | |
81110384 JF |
2772 | case BPF_MAP_TYPE_SOCKHASH: |
2773 | if (func_id != BPF_FUNC_sk_redirect_hash && | |
2774 | func_id != BPF_FUNC_sock_hash_update && | |
2775 | func_id != BPF_FUNC_map_delete_elem && | |
2776 | func_id != BPF_FUNC_msg_redirect_hash) | |
2777 | goto error; | |
2778 | break; | |
2dbb9b9e MKL |
2779 | case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY: |
2780 | if (func_id != BPF_FUNC_sk_select_reuseport) | |
2781 | goto error; | |
2782 | break; | |
f1a2e44a MV |
2783 | case BPF_MAP_TYPE_QUEUE: |
2784 | case BPF_MAP_TYPE_STACK: | |
2785 | if (func_id != BPF_FUNC_map_peek_elem && | |
2786 | func_id != BPF_FUNC_map_pop_elem && | |
2787 | func_id != BPF_FUNC_map_push_elem) | |
2788 | goto error; | |
2789 | break; | |
6ac99e8f MKL |
2790 | case BPF_MAP_TYPE_SK_STORAGE: |
2791 | if (func_id != BPF_FUNC_sk_storage_get && | |
2792 | func_id != BPF_FUNC_sk_storage_delete) | |
2793 | goto error; | |
2794 | break; | |
6aff67c8 AS |
2795 | default: |
2796 | break; | |
2797 | } | |
2798 | ||
2799 | /* ... and second from the function itself. */ | |
2800 | switch (func_id) { | |
2801 | case BPF_FUNC_tail_call: | |
2802 | if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY) | |
2803 | goto error; | |
f910cefa | 2804 | if (env->subprog_cnt > 1) { |
f4d7e40a AS |
2805 | verbose(env, "tail_calls are not allowed in programs with bpf-to-bpf calls\n"); |
2806 | return -EINVAL; | |
2807 | } | |
6aff67c8 AS |
2808 | break; |
2809 | case BPF_FUNC_perf_event_read: | |
2810 | case BPF_FUNC_perf_event_output: | |
908432ca | 2811 | case BPF_FUNC_perf_event_read_value: |
6aff67c8 AS |
2812 | if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) |
2813 | goto error; | |
2814 | break; | |
2815 | case BPF_FUNC_get_stackid: | |
2816 | if (map->map_type != BPF_MAP_TYPE_STACK_TRACE) | |
2817 | goto error; | |
2818 | break; | |
60d20f91 | 2819 | case BPF_FUNC_current_task_under_cgroup: |
747ea55e | 2820 | case BPF_FUNC_skb_under_cgroup: |
4a482f34 MKL |
2821 | if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY) |
2822 | goto error; | |
2823 | break; | |
97f91a7c | 2824 | case BPF_FUNC_redirect_map: |
9c270af3 | 2825 | if (map->map_type != BPF_MAP_TYPE_DEVMAP && |
fbfc504a BT |
2826 | map->map_type != BPF_MAP_TYPE_CPUMAP && |
2827 | map->map_type != BPF_MAP_TYPE_XSKMAP) | |
97f91a7c JF |
2828 | goto error; |
2829 | break; | |
174a79ff | 2830 | case BPF_FUNC_sk_redirect_map: |
4f738adb | 2831 | case BPF_FUNC_msg_redirect_map: |
81110384 | 2832 | case BPF_FUNC_sock_map_update: |
174a79ff JF |
2833 | if (map->map_type != BPF_MAP_TYPE_SOCKMAP) |
2834 | goto error; | |
2835 | break; | |
81110384 JF |
2836 | case BPF_FUNC_sk_redirect_hash: |
2837 | case BPF_FUNC_msg_redirect_hash: | |
2838 | case BPF_FUNC_sock_hash_update: | |
2839 | if (map->map_type != BPF_MAP_TYPE_SOCKHASH) | |
174a79ff JF |
2840 | goto error; |
2841 | break; | |
cd339431 | 2842 | case BPF_FUNC_get_local_storage: |
b741f163 RG |
2843 | if (map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE && |
2844 | map->map_type != BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) | |
cd339431 RG |
2845 | goto error; |
2846 | break; | |
2dbb9b9e MKL |
2847 | case BPF_FUNC_sk_select_reuseport: |
2848 | if (map->map_type != BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) | |
2849 | goto error; | |
2850 | break; | |
f1a2e44a MV |
2851 | case BPF_FUNC_map_peek_elem: |
2852 | case BPF_FUNC_map_pop_elem: | |
2853 | case BPF_FUNC_map_push_elem: | |
2854 | if (map->map_type != BPF_MAP_TYPE_QUEUE && | |
2855 | map->map_type != BPF_MAP_TYPE_STACK) | |
2856 | goto error; | |
2857 | break; | |
6ac99e8f MKL |
2858 | case BPF_FUNC_sk_storage_get: |
2859 | case BPF_FUNC_sk_storage_delete: | |
2860 | if (map->map_type != BPF_MAP_TYPE_SK_STORAGE) | |
2861 | goto error; | |
2862 | break; | |
6aff67c8 AS |
2863 | default: |
2864 | break; | |
35578d79 KX |
2865 | } |
2866 | ||
2867 | return 0; | |
6aff67c8 | 2868 | error: |
61bd5218 | 2869 | verbose(env, "cannot pass map_type %d into func %s#%d\n", |
ebb676da | 2870 | map->map_type, func_id_name(func_id), func_id); |
6aff67c8 | 2871 | return -EINVAL; |
35578d79 KX |
2872 | } |
2873 | ||
90133415 | 2874 | static bool check_raw_mode_ok(const struct bpf_func_proto *fn) |
435faee1 DB |
2875 | { |
2876 | int count = 0; | |
2877 | ||
39f19ebb | 2878 | if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 2879 | count++; |
39f19ebb | 2880 | if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 2881 | count++; |
39f19ebb | 2882 | if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 2883 | count++; |
39f19ebb | 2884 | if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 2885 | count++; |
39f19ebb | 2886 | if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 DB |
2887 | count++; |
2888 | ||
90133415 DB |
2889 | /* We only support one arg being in raw mode at the moment, |
2890 | * which is sufficient for the helper functions we have | |
2891 | * right now. | |
2892 | */ | |
2893 | return count <= 1; | |
2894 | } | |
2895 | ||
2896 | static bool check_args_pair_invalid(enum bpf_arg_type arg_curr, | |
2897 | enum bpf_arg_type arg_next) | |
2898 | { | |
2899 | return (arg_type_is_mem_ptr(arg_curr) && | |
2900 | !arg_type_is_mem_size(arg_next)) || | |
2901 | (!arg_type_is_mem_ptr(arg_curr) && | |
2902 | arg_type_is_mem_size(arg_next)); | |
2903 | } | |
2904 | ||
2905 | static bool check_arg_pair_ok(const struct bpf_func_proto *fn) | |
2906 | { | |
2907 | /* bpf_xxx(..., buf, len) call will access 'len' | |
2908 | * bytes from memory 'buf'. Both arg types need | |
2909 | * to be paired, so make sure there's no buggy | |
2910 | * helper function specification. | |
2911 | */ | |
2912 | if (arg_type_is_mem_size(fn->arg1_type) || | |
2913 | arg_type_is_mem_ptr(fn->arg5_type) || | |
2914 | check_args_pair_invalid(fn->arg1_type, fn->arg2_type) || | |
2915 | check_args_pair_invalid(fn->arg2_type, fn->arg3_type) || | |
2916 | check_args_pair_invalid(fn->arg3_type, fn->arg4_type) || | |
2917 | check_args_pair_invalid(fn->arg4_type, fn->arg5_type)) | |
2918 | return false; | |
2919 | ||
2920 | return true; | |
2921 | } | |
2922 | ||
1b986589 | 2923 | static bool check_refcount_ok(const struct bpf_func_proto *fn, int func_id) |
fd978bf7 JS |
2924 | { |
2925 | int count = 0; | |
2926 | ||
1b986589 | 2927 | if (arg_type_may_be_refcounted(fn->arg1_type)) |
fd978bf7 | 2928 | count++; |
1b986589 | 2929 | if (arg_type_may_be_refcounted(fn->arg2_type)) |
fd978bf7 | 2930 | count++; |
1b986589 | 2931 | if (arg_type_may_be_refcounted(fn->arg3_type)) |
fd978bf7 | 2932 | count++; |
1b986589 | 2933 | if (arg_type_may_be_refcounted(fn->arg4_type)) |
fd978bf7 | 2934 | count++; |
1b986589 | 2935 | if (arg_type_may_be_refcounted(fn->arg5_type)) |
fd978bf7 JS |
2936 | count++; |
2937 | ||
1b986589 MKL |
2938 | /* A reference acquiring function cannot acquire |
2939 | * another refcounted ptr. | |
2940 | */ | |
2941 | if (is_acquire_function(func_id) && count) | |
2942 | return false; | |
2943 | ||
fd978bf7 JS |
2944 | /* We only support one arg being unreferenced at the moment, |
2945 | * which is sufficient for the helper functions we have right now. | |
2946 | */ | |
2947 | return count <= 1; | |
2948 | } | |
2949 | ||
1b986589 | 2950 | static int check_func_proto(const struct bpf_func_proto *fn, int func_id) |
90133415 DB |
2951 | { |
2952 | return check_raw_mode_ok(fn) && | |
fd978bf7 | 2953 | check_arg_pair_ok(fn) && |
1b986589 | 2954 | check_refcount_ok(fn, func_id) ? 0 : -EINVAL; |
435faee1 DB |
2955 | } |
2956 | ||
de8f3a83 DB |
2957 | /* Packet data might have moved, any old PTR_TO_PACKET[_META,_END] |
2958 | * are now invalid, so turn them into unknown SCALAR_VALUE. | |
f1174f77 | 2959 | */ |
f4d7e40a AS |
2960 | static void __clear_all_pkt_pointers(struct bpf_verifier_env *env, |
2961 | struct bpf_func_state *state) | |
969bf05e | 2962 | { |
58e2af8b | 2963 | struct bpf_reg_state *regs = state->regs, *reg; |
969bf05e AS |
2964 | int i; |
2965 | ||
2966 | for (i = 0; i < MAX_BPF_REG; i++) | |
de8f3a83 | 2967 | if (reg_is_pkt_pointer_any(®s[i])) |
61bd5218 | 2968 | mark_reg_unknown(env, regs, i); |
969bf05e | 2969 | |
f3709f69 JS |
2970 | bpf_for_each_spilled_reg(i, state, reg) { |
2971 | if (!reg) | |
969bf05e | 2972 | continue; |
de8f3a83 DB |
2973 | if (reg_is_pkt_pointer_any(reg)) |
2974 | __mark_reg_unknown(reg); | |
969bf05e AS |
2975 | } |
2976 | } | |
2977 | ||
f4d7e40a AS |
2978 | static void clear_all_pkt_pointers(struct bpf_verifier_env *env) |
2979 | { | |
2980 | struct bpf_verifier_state *vstate = env->cur_state; | |
2981 | int i; | |
2982 | ||
2983 | for (i = 0; i <= vstate->curframe; i++) | |
2984 | __clear_all_pkt_pointers(env, vstate->frame[i]); | |
2985 | } | |
2986 | ||
fd978bf7 | 2987 | static void release_reg_references(struct bpf_verifier_env *env, |
1b986589 MKL |
2988 | struct bpf_func_state *state, |
2989 | int ref_obj_id) | |
fd978bf7 JS |
2990 | { |
2991 | struct bpf_reg_state *regs = state->regs, *reg; | |
2992 | int i; | |
2993 | ||
2994 | for (i = 0; i < MAX_BPF_REG; i++) | |
1b986589 | 2995 | if (regs[i].ref_obj_id == ref_obj_id) |
fd978bf7 JS |
2996 | mark_reg_unknown(env, regs, i); |
2997 | ||
2998 | bpf_for_each_spilled_reg(i, state, reg) { | |
2999 | if (!reg) | |
3000 | continue; | |
1b986589 | 3001 | if (reg->ref_obj_id == ref_obj_id) |
fd978bf7 JS |
3002 | __mark_reg_unknown(reg); |
3003 | } | |
3004 | } | |
3005 | ||
3006 | /* The pointer with the specified id has released its reference to kernel | |
3007 | * resources. Identify all copies of the same pointer and clear the reference. | |
3008 | */ | |
3009 | static int release_reference(struct bpf_verifier_env *env, | |
1b986589 | 3010 | int ref_obj_id) |
fd978bf7 JS |
3011 | { |
3012 | struct bpf_verifier_state *vstate = env->cur_state; | |
1b986589 | 3013 | int err; |
fd978bf7 JS |
3014 | int i; |
3015 | ||
1b986589 MKL |
3016 | err = release_reference_state(cur_func(env), ref_obj_id); |
3017 | if (err) | |
3018 | return err; | |
3019 | ||
fd978bf7 | 3020 | for (i = 0; i <= vstate->curframe; i++) |
1b986589 | 3021 | release_reg_references(env, vstate->frame[i], ref_obj_id); |
fd978bf7 | 3022 | |
1b986589 | 3023 | return 0; |
fd978bf7 JS |
3024 | } |
3025 | ||
f4d7e40a AS |
3026 | static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, |
3027 | int *insn_idx) | |
3028 | { | |
3029 | struct bpf_verifier_state *state = env->cur_state; | |
3030 | struct bpf_func_state *caller, *callee; | |
fd978bf7 | 3031 | int i, err, subprog, target_insn; |
f4d7e40a | 3032 | |
aada9ce6 | 3033 | if (state->curframe + 1 >= MAX_CALL_FRAMES) { |
f4d7e40a | 3034 | verbose(env, "the call stack of %d frames is too deep\n", |
aada9ce6 | 3035 | state->curframe + 2); |
f4d7e40a AS |
3036 | return -E2BIG; |
3037 | } | |
3038 | ||
3039 | target_insn = *insn_idx + insn->imm; | |
3040 | subprog = find_subprog(env, target_insn + 1); | |
3041 | if (subprog < 0) { | |
3042 | verbose(env, "verifier bug. No program starts at insn %d\n", | |
3043 | target_insn + 1); | |
3044 | return -EFAULT; | |
3045 | } | |
3046 | ||
3047 | caller = state->frame[state->curframe]; | |
3048 | if (state->frame[state->curframe + 1]) { | |
3049 | verbose(env, "verifier bug. Frame %d already allocated\n", | |
3050 | state->curframe + 1); | |
3051 | return -EFAULT; | |
3052 | } | |
3053 | ||
3054 | callee = kzalloc(sizeof(*callee), GFP_KERNEL); | |
3055 | if (!callee) | |
3056 | return -ENOMEM; | |
3057 | state->frame[state->curframe + 1] = callee; | |
3058 | ||
3059 | /* callee cannot access r0, r6 - r9 for reading and has to write | |
3060 | * into its own stack before reading from it. | |
3061 | * callee can read/write into caller's stack | |
3062 | */ | |
3063 | init_func_state(env, callee, | |
3064 | /* remember the callsite, it will be used by bpf_exit */ | |
3065 | *insn_idx /* callsite */, | |
3066 | state->curframe + 1 /* frameno within this callchain */, | |
f910cefa | 3067 | subprog /* subprog number within this prog */); |
f4d7e40a | 3068 | |
fd978bf7 JS |
3069 | /* Transfer references to the callee */ |
3070 | err = transfer_reference_state(callee, caller); | |
3071 | if (err) | |
3072 | return err; | |
3073 | ||
679c782d EC |
3074 | /* copy r1 - r5 args that callee can access. The copy includes parent |
3075 | * pointers, which connects us up to the liveness chain | |
3076 | */ | |
f4d7e40a AS |
3077 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) |
3078 | callee->regs[i] = caller->regs[i]; | |
3079 | ||
679c782d | 3080 | /* after the call registers r0 - r5 were scratched */ |
f4d7e40a AS |
3081 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
3082 | mark_reg_not_init(env, caller->regs, caller_saved[i]); | |
3083 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); | |
3084 | } | |
3085 | ||
3086 | /* only increment it after check_reg_arg() finished */ | |
3087 | state->curframe++; | |
3088 | ||
3089 | /* and go analyze first insn of the callee */ | |
3090 | *insn_idx = target_insn; | |
3091 | ||
06ee7115 | 3092 | if (env->log.level & BPF_LOG_LEVEL) { |
f4d7e40a AS |
3093 | verbose(env, "caller:\n"); |
3094 | print_verifier_state(env, caller); | |
3095 | verbose(env, "callee:\n"); | |
3096 | print_verifier_state(env, callee); | |
3097 | } | |
3098 | return 0; | |
3099 | } | |
3100 | ||
3101 | static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx) | |
3102 | { | |
3103 | struct bpf_verifier_state *state = env->cur_state; | |
3104 | struct bpf_func_state *caller, *callee; | |
3105 | struct bpf_reg_state *r0; | |
fd978bf7 | 3106 | int err; |
f4d7e40a AS |
3107 | |
3108 | callee = state->frame[state->curframe]; | |
3109 | r0 = &callee->regs[BPF_REG_0]; | |
3110 | if (r0->type == PTR_TO_STACK) { | |
3111 | /* technically it's ok to return caller's stack pointer | |
3112 | * (or caller's caller's pointer) back to the caller, | |
3113 | * since these pointers are valid. Only current stack | |
3114 | * pointer will be invalid as soon as function exits, | |
3115 | * but let's be conservative | |
3116 | */ | |
3117 | verbose(env, "cannot return stack pointer to the caller\n"); | |
3118 | return -EINVAL; | |
3119 | } | |
3120 | ||
3121 | state->curframe--; | |
3122 | caller = state->frame[state->curframe]; | |
3123 | /* return to the caller whatever r0 had in the callee */ | |
3124 | caller->regs[BPF_REG_0] = *r0; | |
3125 | ||
fd978bf7 JS |
3126 | /* Transfer references to the caller */ |
3127 | err = transfer_reference_state(caller, callee); | |
3128 | if (err) | |
3129 | return err; | |
3130 | ||
f4d7e40a | 3131 | *insn_idx = callee->callsite + 1; |
06ee7115 | 3132 | if (env->log.level & BPF_LOG_LEVEL) { |
f4d7e40a AS |
3133 | verbose(env, "returning from callee:\n"); |
3134 | print_verifier_state(env, callee); | |
3135 | verbose(env, "to caller at %d:\n", *insn_idx); | |
3136 | print_verifier_state(env, caller); | |
3137 | } | |
3138 | /* clear everything in the callee */ | |
3139 | free_func_state(callee); | |
3140 | state->frame[state->curframe + 1] = NULL; | |
3141 | return 0; | |
3142 | } | |
3143 | ||
849fa506 YS |
3144 | static void do_refine_retval_range(struct bpf_reg_state *regs, int ret_type, |
3145 | int func_id, | |
3146 | struct bpf_call_arg_meta *meta) | |
3147 | { | |
3148 | struct bpf_reg_state *ret_reg = ®s[BPF_REG_0]; | |
3149 | ||
3150 | if (ret_type != RET_INTEGER || | |
3151 | (func_id != BPF_FUNC_get_stack && | |
3152 | func_id != BPF_FUNC_probe_read_str)) | |
3153 | return; | |
3154 | ||
3155 | ret_reg->smax_value = meta->msize_smax_value; | |
3156 | ret_reg->umax_value = meta->msize_umax_value; | |
3157 | __reg_deduce_bounds(ret_reg); | |
3158 | __reg_bound_offset(ret_reg); | |
3159 | } | |
3160 | ||
c93552c4 DB |
3161 | static int |
3162 | record_func_map(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, | |
3163 | int func_id, int insn_idx) | |
3164 | { | |
3165 | struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; | |
591fe988 | 3166 | struct bpf_map *map = meta->map_ptr; |
c93552c4 DB |
3167 | |
3168 | if (func_id != BPF_FUNC_tail_call && | |
09772d92 DB |
3169 | func_id != BPF_FUNC_map_lookup_elem && |
3170 | func_id != BPF_FUNC_map_update_elem && | |
f1a2e44a MV |
3171 | func_id != BPF_FUNC_map_delete_elem && |
3172 | func_id != BPF_FUNC_map_push_elem && | |
3173 | func_id != BPF_FUNC_map_pop_elem && | |
3174 | func_id != BPF_FUNC_map_peek_elem) | |
c93552c4 | 3175 | return 0; |
09772d92 | 3176 | |
591fe988 | 3177 | if (map == NULL) { |
c93552c4 DB |
3178 | verbose(env, "kernel subsystem misconfigured verifier\n"); |
3179 | return -EINVAL; | |
3180 | } | |
3181 | ||
591fe988 DB |
3182 | /* In case of read-only, some additional restrictions |
3183 | * need to be applied in order to prevent altering the | |
3184 | * state of the map from program side. | |
3185 | */ | |
3186 | if ((map->map_flags & BPF_F_RDONLY_PROG) && | |
3187 | (func_id == BPF_FUNC_map_delete_elem || | |
3188 | func_id == BPF_FUNC_map_update_elem || | |
3189 | func_id == BPF_FUNC_map_push_elem || | |
3190 | func_id == BPF_FUNC_map_pop_elem)) { | |
3191 | verbose(env, "write into map forbidden\n"); | |
3192 | return -EACCES; | |
3193 | } | |
3194 | ||
c93552c4 DB |
3195 | if (!BPF_MAP_PTR(aux->map_state)) |
3196 | bpf_map_ptr_store(aux, meta->map_ptr, | |
3197 | meta->map_ptr->unpriv_array); | |
3198 | else if (BPF_MAP_PTR(aux->map_state) != meta->map_ptr) | |
3199 | bpf_map_ptr_store(aux, BPF_MAP_PTR_POISON, | |
3200 | meta->map_ptr->unpriv_array); | |
3201 | return 0; | |
3202 | } | |
3203 | ||
fd978bf7 JS |
3204 | static int check_reference_leak(struct bpf_verifier_env *env) |
3205 | { | |
3206 | struct bpf_func_state *state = cur_func(env); | |
3207 | int i; | |
3208 | ||
3209 | for (i = 0; i < state->acquired_refs; i++) { | |
3210 | verbose(env, "Unreleased reference id=%d alloc_insn=%d\n", | |
3211 | state->refs[i].id, state->refs[i].insn_idx); | |
3212 | } | |
3213 | return state->acquired_refs ? -EINVAL : 0; | |
3214 | } | |
3215 | ||
f4d7e40a | 3216 | static int check_helper_call(struct bpf_verifier_env *env, int func_id, int insn_idx) |
17a52670 | 3217 | { |
17a52670 | 3218 | const struct bpf_func_proto *fn = NULL; |
638f5b90 | 3219 | struct bpf_reg_state *regs; |
33ff9823 | 3220 | struct bpf_call_arg_meta meta; |
969bf05e | 3221 | bool changes_data; |
17a52670 AS |
3222 | int i, err; |
3223 | ||
3224 | /* find function prototype */ | |
3225 | if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) { | |
61bd5218 JK |
3226 | verbose(env, "invalid func %s#%d\n", func_id_name(func_id), |
3227 | func_id); | |
17a52670 AS |
3228 | return -EINVAL; |
3229 | } | |
3230 | ||
00176a34 | 3231 | if (env->ops->get_func_proto) |
5e43f899 | 3232 | fn = env->ops->get_func_proto(func_id, env->prog); |
17a52670 | 3233 | if (!fn) { |
61bd5218 JK |
3234 | verbose(env, "unknown func %s#%d\n", func_id_name(func_id), |
3235 | func_id); | |
17a52670 AS |
3236 | return -EINVAL; |
3237 | } | |
3238 | ||
3239 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ | |
24701ece | 3240 | if (!env->prog->gpl_compatible && fn->gpl_only) { |
3fe2867c | 3241 | verbose(env, "cannot call GPL-restricted function from non-GPL compatible program\n"); |
17a52670 AS |
3242 | return -EINVAL; |
3243 | } | |
3244 | ||
04514d13 | 3245 | /* With LD_ABS/IND some JITs save/restore skb from r1. */ |
17bedab2 | 3246 | changes_data = bpf_helper_changes_pkt_data(fn->func); |
04514d13 DB |
3247 | if (changes_data && fn->arg1_type != ARG_PTR_TO_CTX) { |
3248 | verbose(env, "kernel subsystem misconfigured func %s#%d: r1 != ctx\n", | |
3249 | func_id_name(func_id), func_id); | |
3250 | return -EINVAL; | |
3251 | } | |
969bf05e | 3252 | |
33ff9823 | 3253 | memset(&meta, 0, sizeof(meta)); |
36bbef52 | 3254 | meta.pkt_access = fn->pkt_access; |
33ff9823 | 3255 | |
1b986589 | 3256 | err = check_func_proto(fn, func_id); |
435faee1 | 3257 | if (err) { |
61bd5218 | 3258 | verbose(env, "kernel subsystem misconfigured func %s#%d\n", |
ebb676da | 3259 | func_id_name(func_id), func_id); |
435faee1 DB |
3260 | return err; |
3261 | } | |
3262 | ||
d83525ca | 3263 | meta.func_id = func_id; |
17a52670 | 3264 | /* check args */ |
33ff9823 | 3265 | err = check_func_arg(env, BPF_REG_1, fn->arg1_type, &meta); |
17a52670 AS |
3266 | if (err) |
3267 | return err; | |
33ff9823 | 3268 | err = check_func_arg(env, BPF_REG_2, fn->arg2_type, &meta); |
17a52670 AS |
3269 | if (err) |
3270 | return err; | |
33ff9823 | 3271 | err = check_func_arg(env, BPF_REG_3, fn->arg3_type, &meta); |
17a52670 AS |
3272 | if (err) |
3273 | return err; | |
33ff9823 | 3274 | err = check_func_arg(env, BPF_REG_4, fn->arg4_type, &meta); |
17a52670 AS |
3275 | if (err) |
3276 | return err; | |
33ff9823 | 3277 | err = check_func_arg(env, BPF_REG_5, fn->arg5_type, &meta); |
17a52670 AS |
3278 | if (err) |
3279 | return err; | |
3280 | ||
c93552c4 DB |
3281 | err = record_func_map(env, &meta, func_id, insn_idx); |
3282 | if (err) | |
3283 | return err; | |
3284 | ||
435faee1 DB |
3285 | /* Mark slots with STACK_MISC in case of raw mode, stack offset |
3286 | * is inferred from register state. | |
3287 | */ | |
3288 | for (i = 0; i < meta.access_size; i++) { | |
ca369602 DB |
3289 | err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B, |
3290 | BPF_WRITE, -1, false); | |
435faee1 DB |
3291 | if (err) |
3292 | return err; | |
3293 | } | |
3294 | ||
fd978bf7 JS |
3295 | if (func_id == BPF_FUNC_tail_call) { |
3296 | err = check_reference_leak(env); | |
3297 | if (err) { | |
3298 | verbose(env, "tail_call would lead to reference leak\n"); | |
3299 | return err; | |
3300 | } | |
3301 | } else if (is_release_function(func_id)) { | |
1b986589 | 3302 | err = release_reference(env, meta.ref_obj_id); |
46f8bc92 MKL |
3303 | if (err) { |
3304 | verbose(env, "func %s#%d reference has not been acquired before\n", | |
3305 | func_id_name(func_id), func_id); | |
fd978bf7 | 3306 | return err; |
46f8bc92 | 3307 | } |
fd978bf7 JS |
3308 | } |
3309 | ||
638f5b90 | 3310 | regs = cur_regs(env); |
cd339431 RG |
3311 | |
3312 | /* check that flags argument in get_local_storage(map, flags) is 0, | |
3313 | * this is required because get_local_storage() can't return an error. | |
3314 | */ | |
3315 | if (func_id == BPF_FUNC_get_local_storage && | |
3316 | !register_is_null(®s[BPF_REG_2])) { | |
3317 | verbose(env, "get_local_storage() doesn't support non-zero flags\n"); | |
3318 | return -EINVAL; | |
3319 | } | |
3320 | ||
17a52670 | 3321 | /* reset caller saved regs */ |
dc503a8a | 3322 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
61bd5218 | 3323 | mark_reg_not_init(env, regs, caller_saved[i]); |
dc503a8a EC |
3324 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
3325 | } | |
17a52670 | 3326 | |
dc503a8a | 3327 | /* update return register (already marked as written above) */ |
17a52670 | 3328 | if (fn->ret_type == RET_INTEGER) { |
f1174f77 | 3329 | /* sets type to SCALAR_VALUE */ |
61bd5218 | 3330 | mark_reg_unknown(env, regs, BPF_REG_0); |
17a52670 AS |
3331 | } else if (fn->ret_type == RET_VOID) { |
3332 | regs[BPF_REG_0].type = NOT_INIT; | |
3e6a4b3e RG |
3333 | } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL || |
3334 | fn->ret_type == RET_PTR_TO_MAP_VALUE) { | |
f1174f77 | 3335 | /* There is no offset yet applied, variable or fixed */ |
61bd5218 | 3336 | mark_reg_known_zero(env, regs, BPF_REG_0); |
17a52670 AS |
3337 | /* remember map_ptr, so that check_map_access() |
3338 | * can check 'value_size' boundary of memory access | |
3339 | * to map element returned from bpf_map_lookup_elem() | |
3340 | */ | |
33ff9823 | 3341 | if (meta.map_ptr == NULL) { |
61bd5218 JK |
3342 | verbose(env, |
3343 | "kernel subsystem misconfigured verifier\n"); | |
17a52670 AS |
3344 | return -EINVAL; |
3345 | } | |
33ff9823 | 3346 | regs[BPF_REG_0].map_ptr = meta.map_ptr; |
4d31f301 DB |
3347 | if (fn->ret_type == RET_PTR_TO_MAP_VALUE) { |
3348 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE; | |
e16d2f1a AS |
3349 | if (map_value_has_spin_lock(meta.map_ptr)) |
3350 | regs[BPF_REG_0].id = ++env->id_gen; | |
4d31f301 DB |
3351 | } else { |
3352 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL; | |
3353 | regs[BPF_REG_0].id = ++env->id_gen; | |
3354 | } | |
c64b7983 JS |
3355 | } else if (fn->ret_type == RET_PTR_TO_SOCKET_OR_NULL) { |
3356 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
3357 | regs[BPF_REG_0].type = PTR_TO_SOCKET_OR_NULL; | |
0f3adc28 | 3358 | regs[BPF_REG_0].id = ++env->id_gen; |
85a51f8c LB |
3359 | } else if (fn->ret_type == RET_PTR_TO_SOCK_COMMON_OR_NULL) { |
3360 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
3361 | regs[BPF_REG_0].type = PTR_TO_SOCK_COMMON_OR_NULL; | |
3362 | regs[BPF_REG_0].id = ++env->id_gen; | |
655a51e5 MKL |
3363 | } else if (fn->ret_type == RET_PTR_TO_TCP_SOCK_OR_NULL) { |
3364 | mark_reg_known_zero(env, regs, BPF_REG_0); | |
3365 | regs[BPF_REG_0].type = PTR_TO_TCP_SOCK_OR_NULL; | |
3366 | regs[BPF_REG_0].id = ++env->id_gen; | |
17a52670 | 3367 | } else { |
61bd5218 | 3368 | verbose(env, "unknown return type %d of func %s#%d\n", |
ebb676da | 3369 | fn->ret_type, func_id_name(func_id), func_id); |
17a52670 AS |
3370 | return -EINVAL; |
3371 | } | |
04fd61ab | 3372 | |
0f3adc28 | 3373 | if (is_ptr_cast_function(func_id)) { |
1b986589 MKL |
3374 | /* For release_reference() */ |
3375 | regs[BPF_REG_0].ref_obj_id = meta.ref_obj_id; | |
0f3adc28 LB |
3376 | } else if (is_acquire_function(func_id)) { |
3377 | int id = acquire_reference_state(env, insn_idx); | |
3378 | ||
3379 | if (id < 0) | |
3380 | return id; | |
3381 | /* For mark_ptr_or_null_reg() */ | |
3382 | regs[BPF_REG_0].id = id; | |
3383 | /* For release_reference() */ | |
3384 | regs[BPF_REG_0].ref_obj_id = id; | |
3385 | } | |
1b986589 | 3386 | |
849fa506 YS |
3387 | do_refine_retval_range(regs, fn->ret_type, func_id, &meta); |
3388 | ||
61bd5218 | 3389 | err = check_map_func_compatibility(env, meta.map_ptr, func_id); |
35578d79 KX |
3390 | if (err) |
3391 | return err; | |
04fd61ab | 3392 | |
c195651e YS |
3393 | if (func_id == BPF_FUNC_get_stack && !env->prog->has_callchain_buf) { |
3394 | const char *err_str; | |
3395 | ||
3396 | #ifdef CONFIG_PERF_EVENTS | |
3397 | err = get_callchain_buffers(sysctl_perf_event_max_stack); | |
3398 | err_str = "cannot get callchain buffer for func %s#%d\n"; | |
3399 | #else | |
3400 | err = -ENOTSUPP; | |
3401 | err_str = "func %s#%d not supported without CONFIG_PERF_EVENTS\n"; | |
3402 | #endif | |
3403 | if (err) { | |
3404 | verbose(env, err_str, func_id_name(func_id), func_id); | |
3405 | return err; | |
3406 | } | |
3407 | ||
3408 | env->prog->has_callchain_buf = true; | |
3409 | } | |
3410 | ||
969bf05e AS |
3411 | if (changes_data) |
3412 | clear_all_pkt_pointers(env); | |
3413 | return 0; | |
3414 | } | |
3415 | ||
b03c9f9f EC |
3416 | static bool signed_add_overflows(s64 a, s64 b) |
3417 | { | |
3418 | /* Do the add in u64, where overflow is well-defined */ | |
3419 | s64 res = (s64)((u64)a + (u64)b); | |
3420 | ||
3421 | if (b < 0) | |
3422 | return res > a; | |
3423 | return res < a; | |
3424 | } | |
3425 | ||
3426 | static bool signed_sub_overflows(s64 a, s64 b) | |
3427 | { | |
3428 | /* Do the sub in u64, where overflow is well-defined */ | |
3429 | s64 res = (s64)((u64)a - (u64)b); | |
3430 | ||
3431 | if (b < 0) | |
3432 | return res < a; | |
3433 | return res > a; | |
969bf05e AS |
3434 | } |
3435 | ||
bb7f0f98 AS |
3436 | static bool check_reg_sane_offset(struct bpf_verifier_env *env, |
3437 | const struct bpf_reg_state *reg, | |
3438 | enum bpf_reg_type type) | |
3439 | { | |
3440 | bool known = tnum_is_const(reg->var_off); | |
3441 | s64 val = reg->var_off.value; | |
3442 | s64 smin = reg->smin_value; | |
3443 | ||
3444 | if (known && (val >= BPF_MAX_VAR_OFF || val <= -BPF_MAX_VAR_OFF)) { | |
3445 | verbose(env, "math between %s pointer and %lld is not allowed\n", | |
3446 | reg_type_str[type], val); | |
3447 | return false; | |
3448 | } | |
3449 | ||
3450 | if (reg->off >= BPF_MAX_VAR_OFF || reg->off <= -BPF_MAX_VAR_OFF) { | |
3451 | verbose(env, "%s pointer offset %d is not allowed\n", | |
3452 | reg_type_str[type], reg->off); | |
3453 | return false; | |
3454 | } | |
3455 | ||
3456 | if (smin == S64_MIN) { | |
3457 | verbose(env, "math between %s pointer and register with unbounded min value is not allowed\n", | |
3458 | reg_type_str[type]); | |
3459 | return false; | |
3460 | } | |
3461 | ||
3462 | if (smin >= BPF_MAX_VAR_OFF || smin <= -BPF_MAX_VAR_OFF) { | |
3463 | verbose(env, "value %lld makes %s pointer be out of bounds\n", | |
3464 | smin, reg_type_str[type]); | |
3465 | return false; | |
3466 | } | |
3467 | ||
3468 | return true; | |
3469 | } | |
3470 | ||
979d63d5 DB |
3471 | static struct bpf_insn_aux_data *cur_aux(struct bpf_verifier_env *env) |
3472 | { | |
3473 | return &env->insn_aux_data[env->insn_idx]; | |
3474 | } | |
3475 | ||
3476 | static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg, | |
3477 | u32 *ptr_limit, u8 opcode, bool off_is_neg) | |
3478 | { | |
3479 | bool mask_to_left = (opcode == BPF_ADD && off_is_neg) || | |
3480 | (opcode == BPF_SUB && !off_is_neg); | |
3481 | u32 off; | |
3482 | ||
3483 | switch (ptr_reg->type) { | |
3484 | case PTR_TO_STACK: | |
088ec26d AI |
3485 | /* Indirect variable offset stack access is prohibited in |
3486 | * unprivileged mode so it's not handled here. | |
3487 | */ | |
979d63d5 DB |
3488 | off = ptr_reg->off + ptr_reg->var_off.value; |
3489 | if (mask_to_left) | |
3490 | *ptr_limit = MAX_BPF_STACK + off; | |
3491 | else | |
3492 | *ptr_limit = -off; | |
3493 | return 0; | |
3494 | case PTR_TO_MAP_VALUE: | |
3495 | if (mask_to_left) { | |
3496 | *ptr_limit = ptr_reg->umax_value + ptr_reg->off; | |
3497 | } else { | |
3498 | off = ptr_reg->smin_value + ptr_reg->off; | |
3499 | *ptr_limit = ptr_reg->map_ptr->value_size - off; | |
3500 | } | |
3501 | return 0; | |
3502 | default: | |
3503 | return -EINVAL; | |
3504 | } | |
3505 | } | |
3506 | ||
d3bd7413 DB |
3507 | static bool can_skip_alu_sanitation(const struct bpf_verifier_env *env, |
3508 | const struct bpf_insn *insn) | |
3509 | { | |
3510 | return env->allow_ptr_leaks || BPF_SRC(insn->code) == BPF_K; | |
3511 | } | |
3512 | ||
3513 | static int update_alu_sanitation_state(struct bpf_insn_aux_data *aux, | |
3514 | u32 alu_state, u32 alu_limit) | |
3515 | { | |
3516 | /* If we arrived here from different branches with different | |
3517 | * state or limits to sanitize, then this won't work. | |
3518 | */ | |
3519 | if (aux->alu_state && | |
3520 | (aux->alu_state != alu_state || | |
3521 | aux->alu_limit != alu_limit)) | |
3522 | return -EACCES; | |
3523 | ||
3524 | /* Corresponding fixup done in fixup_bpf_calls(). */ | |
3525 | aux->alu_state = alu_state; | |
3526 | aux->alu_limit = alu_limit; | |
3527 | return 0; | |
3528 | } | |
3529 | ||
3530 | static int sanitize_val_alu(struct bpf_verifier_env *env, | |
3531 | struct bpf_insn *insn) | |
3532 | { | |
3533 | struct bpf_insn_aux_data *aux = cur_aux(env); | |
3534 | ||
3535 | if (can_skip_alu_sanitation(env, insn)) | |
3536 | return 0; | |
3537 | ||
3538 | return update_alu_sanitation_state(aux, BPF_ALU_NON_POINTER, 0); | |
3539 | } | |
3540 | ||
979d63d5 DB |
3541 | static int sanitize_ptr_alu(struct bpf_verifier_env *env, |
3542 | struct bpf_insn *insn, | |
3543 | const struct bpf_reg_state *ptr_reg, | |
3544 | struct bpf_reg_state *dst_reg, | |
3545 | bool off_is_neg) | |
3546 | { | |
3547 | struct bpf_verifier_state *vstate = env->cur_state; | |
3548 | struct bpf_insn_aux_data *aux = cur_aux(env); | |
3549 | bool ptr_is_dst_reg = ptr_reg == dst_reg; | |
3550 | u8 opcode = BPF_OP(insn->code); | |
3551 | u32 alu_state, alu_limit; | |
3552 | struct bpf_reg_state tmp; | |
3553 | bool ret; | |
3554 | ||
d3bd7413 | 3555 | if (can_skip_alu_sanitation(env, insn)) |
979d63d5 DB |
3556 | return 0; |
3557 | ||
3558 | /* We already marked aux for masking from non-speculative | |
3559 | * paths, thus we got here in the first place. We only care | |
3560 | * to explore bad access from here. | |
3561 | */ | |
3562 | if (vstate->speculative) | |
3563 | goto do_sim; | |
3564 | ||
3565 | alu_state = off_is_neg ? BPF_ALU_NEG_VALUE : 0; | |
3566 | alu_state |= ptr_is_dst_reg ? | |
3567 | BPF_ALU_SANITIZE_SRC : BPF_ALU_SANITIZE_DST; | |
3568 | ||
3569 | if (retrieve_ptr_limit(ptr_reg, &alu_limit, opcode, off_is_neg)) | |
3570 | return 0; | |
d3bd7413 | 3571 | if (update_alu_sanitation_state(aux, alu_state, alu_limit)) |
979d63d5 | 3572 | return -EACCES; |
979d63d5 DB |
3573 | do_sim: |
3574 | /* Simulate and find potential out-of-bounds access under | |
3575 | * speculative execution from truncation as a result of | |
3576 | * masking when off was not within expected range. If off | |
3577 | * sits in dst, then we temporarily need to move ptr there | |
3578 | * to simulate dst (== 0) +/-= ptr. Needed, for example, | |
3579 | * for cases where we use K-based arithmetic in one direction | |
3580 | * and truncated reg-based in the other in order to explore | |
3581 | * bad access. | |
3582 | */ | |
3583 | if (!ptr_is_dst_reg) { | |
3584 | tmp = *dst_reg; | |
3585 | *dst_reg = *ptr_reg; | |
3586 | } | |
3587 | ret = push_stack(env, env->insn_idx + 1, env->insn_idx, true); | |
0803278b | 3588 | if (!ptr_is_dst_reg && ret) |
979d63d5 DB |
3589 | *dst_reg = tmp; |
3590 | return !ret ? -EFAULT : 0; | |
3591 | } | |
3592 | ||
f1174f77 | 3593 | /* Handles arithmetic on a pointer and a scalar: computes new min/max and var_off. |
f1174f77 EC |
3594 | * Caller should also handle BPF_MOV case separately. |
3595 | * If we return -EACCES, caller may want to try again treating pointer as a | |
3596 | * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks. | |
3597 | */ | |
3598 | static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, | |
3599 | struct bpf_insn *insn, | |
3600 | const struct bpf_reg_state *ptr_reg, | |
3601 | const struct bpf_reg_state *off_reg) | |
969bf05e | 3602 | { |
f4d7e40a AS |
3603 | struct bpf_verifier_state *vstate = env->cur_state; |
3604 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
3605 | struct bpf_reg_state *regs = state->regs, *dst_reg; | |
f1174f77 | 3606 | bool known = tnum_is_const(off_reg->var_off); |
b03c9f9f EC |
3607 | s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value, |
3608 | smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value; | |
3609 | u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value, | |
3610 | umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value; | |
9d7eceed | 3611 | u32 dst = insn->dst_reg, src = insn->src_reg; |
969bf05e | 3612 | u8 opcode = BPF_OP(insn->code); |
979d63d5 | 3613 | int ret; |
969bf05e | 3614 | |
f1174f77 | 3615 | dst_reg = ®s[dst]; |
969bf05e | 3616 | |
6f16101e DB |
3617 | if ((known && (smin_val != smax_val || umin_val != umax_val)) || |
3618 | smin_val > smax_val || umin_val > umax_val) { | |
3619 | /* Taint dst register if offset had invalid bounds derived from | |
3620 | * e.g. dead branches. | |
3621 | */ | |
3622 | __mark_reg_unknown(dst_reg); | |
3623 | return 0; | |
f1174f77 EC |
3624 | } |
3625 | ||
3626 | if (BPF_CLASS(insn->code) != BPF_ALU64) { | |
3627 | /* 32-bit ALU ops on pointers produce (meaningless) scalars */ | |
82abbf8d AS |
3628 | verbose(env, |
3629 | "R%d 32-bit pointer arithmetic prohibited\n", | |
3630 | dst); | |
f1174f77 | 3631 | return -EACCES; |
969bf05e AS |
3632 | } |
3633 | ||
aad2eeaf JS |
3634 | switch (ptr_reg->type) { |
3635 | case PTR_TO_MAP_VALUE_OR_NULL: | |
3636 | verbose(env, "R%d pointer arithmetic on %s prohibited, null-check it first\n", | |
3637 | dst, reg_type_str[ptr_reg->type]); | |
f1174f77 | 3638 | return -EACCES; |
aad2eeaf JS |
3639 | case CONST_PTR_TO_MAP: |
3640 | case PTR_TO_PACKET_END: | |
c64b7983 JS |
3641 | case PTR_TO_SOCKET: |
3642 | case PTR_TO_SOCKET_OR_NULL: | |
46f8bc92 MKL |
3643 | case PTR_TO_SOCK_COMMON: |
3644 | case PTR_TO_SOCK_COMMON_OR_NULL: | |
655a51e5 MKL |
3645 | case PTR_TO_TCP_SOCK: |
3646 | case PTR_TO_TCP_SOCK_OR_NULL: | |
aad2eeaf JS |
3647 | verbose(env, "R%d pointer arithmetic on %s prohibited\n", |
3648 | dst, reg_type_str[ptr_reg->type]); | |
f1174f77 | 3649 | return -EACCES; |
9d7eceed DB |
3650 | case PTR_TO_MAP_VALUE: |
3651 | if (!env->allow_ptr_leaks && !known && (smin_val < 0) != (smax_val < 0)) { | |
3652 | verbose(env, "R%d has unknown scalar with mixed signed bounds, pointer arithmetic with it prohibited for !root\n", | |
3653 | off_reg == dst_reg ? dst : src); | |
3654 | return -EACCES; | |
3655 | } | |
3656 | /* fall-through */ | |
aad2eeaf JS |
3657 | default: |
3658 | break; | |
f1174f77 EC |
3659 | } |
3660 | ||
3661 | /* In case of 'scalar += pointer', dst_reg inherits pointer type and id. | |
3662 | * The id may be overwritten later if we create a new variable offset. | |
969bf05e | 3663 | */ |
f1174f77 EC |
3664 | dst_reg->type = ptr_reg->type; |
3665 | dst_reg->id = ptr_reg->id; | |
969bf05e | 3666 | |
bb7f0f98 AS |
3667 | if (!check_reg_sane_offset(env, off_reg, ptr_reg->type) || |
3668 | !check_reg_sane_offset(env, ptr_reg, ptr_reg->type)) | |
3669 | return -EINVAL; | |
3670 | ||
f1174f77 EC |
3671 | switch (opcode) { |
3672 | case BPF_ADD: | |
979d63d5 DB |
3673 | ret = sanitize_ptr_alu(env, insn, ptr_reg, dst_reg, smin_val < 0); |
3674 | if (ret < 0) { | |
3675 | verbose(env, "R%d tried to add from different maps or paths\n", dst); | |
3676 | return ret; | |
3677 | } | |
f1174f77 EC |
3678 | /* We can take a fixed offset as long as it doesn't overflow |
3679 | * the s32 'off' field | |
969bf05e | 3680 | */ |
b03c9f9f EC |
3681 | if (known && (ptr_reg->off + smin_val == |
3682 | (s64)(s32)(ptr_reg->off + smin_val))) { | |
f1174f77 | 3683 | /* pointer += K. Accumulate it into fixed offset */ |
b03c9f9f EC |
3684 | dst_reg->smin_value = smin_ptr; |
3685 | dst_reg->smax_value = smax_ptr; | |
3686 | dst_reg->umin_value = umin_ptr; | |
3687 | dst_reg->umax_value = umax_ptr; | |
f1174f77 | 3688 | dst_reg->var_off = ptr_reg->var_off; |
b03c9f9f | 3689 | dst_reg->off = ptr_reg->off + smin_val; |
0962590e | 3690 | dst_reg->raw = ptr_reg->raw; |
f1174f77 EC |
3691 | break; |
3692 | } | |
f1174f77 EC |
3693 | /* A new variable offset is created. Note that off_reg->off |
3694 | * == 0, since it's a scalar. | |
3695 | * dst_reg gets the pointer type and since some positive | |
3696 | * integer value was added to the pointer, give it a new 'id' | |
3697 | * if it's a PTR_TO_PACKET. | |
3698 | * this creates a new 'base' pointer, off_reg (variable) gets | |
3699 | * added into the variable offset, and we copy the fixed offset | |
3700 | * from ptr_reg. | |
969bf05e | 3701 | */ |
b03c9f9f EC |
3702 | if (signed_add_overflows(smin_ptr, smin_val) || |
3703 | signed_add_overflows(smax_ptr, smax_val)) { | |
3704 | dst_reg->smin_value = S64_MIN; | |
3705 | dst_reg->smax_value = S64_MAX; | |
3706 | } else { | |
3707 | dst_reg->smin_value = smin_ptr + smin_val; | |
3708 | dst_reg->smax_value = smax_ptr + smax_val; | |
3709 | } | |
3710 | if (umin_ptr + umin_val < umin_ptr || | |
3711 | umax_ptr + umax_val < umax_ptr) { | |
3712 | dst_reg->umin_value = 0; | |
3713 | dst_reg->umax_value = U64_MAX; | |
3714 | } else { | |
3715 | dst_reg->umin_value = umin_ptr + umin_val; | |
3716 | dst_reg->umax_value = umax_ptr + umax_val; | |
3717 | } | |
f1174f77 EC |
3718 | dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off); |
3719 | dst_reg->off = ptr_reg->off; | |
0962590e | 3720 | dst_reg->raw = ptr_reg->raw; |
de8f3a83 | 3721 | if (reg_is_pkt_pointer(ptr_reg)) { |
f1174f77 EC |
3722 | dst_reg->id = ++env->id_gen; |
3723 | /* something was added to pkt_ptr, set range to zero */ | |
0962590e | 3724 | dst_reg->raw = 0; |
f1174f77 EC |
3725 | } |
3726 | break; | |
3727 | case BPF_SUB: | |
979d63d5 DB |
3728 | ret = sanitize_ptr_alu(env, insn, ptr_reg, dst_reg, smin_val < 0); |
3729 | if (ret < 0) { | |
3730 | verbose(env, "R%d tried to sub from different maps or paths\n", dst); | |
3731 | return ret; | |
3732 | } | |
f1174f77 EC |
3733 | if (dst_reg == off_reg) { |
3734 | /* scalar -= pointer. Creates an unknown scalar */ | |
82abbf8d AS |
3735 | verbose(env, "R%d tried to subtract pointer from scalar\n", |
3736 | dst); | |
f1174f77 EC |
3737 | return -EACCES; |
3738 | } | |
3739 | /* We don't allow subtraction from FP, because (according to | |
3740 | * test_verifier.c test "invalid fp arithmetic", JITs might not | |
3741 | * be able to deal with it. | |
969bf05e | 3742 | */ |
f1174f77 | 3743 | if (ptr_reg->type == PTR_TO_STACK) { |
82abbf8d AS |
3744 | verbose(env, "R%d subtraction from stack pointer prohibited\n", |
3745 | dst); | |
f1174f77 EC |
3746 | return -EACCES; |
3747 | } | |
b03c9f9f EC |
3748 | if (known && (ptr_reg->off - smin_val == |
3749 | (s64)(s32)(ptr_reg->off - smin_val))) { | |
f1174f77 | 3750 | /* pointer -= K. Subtract it from fixed offset */ |
b03c9f9f EC |
3751 | dst_reg->smin_value = smin_ptr; |
3752 | dst_reg->smax_value = smax_ptr; | |
3753 | dst_reg->umin_value = umin_ptr; | |
3754 | dst_reg->umax_value = umax_ptr; | |
f1174f77 EC |
3755 | dst_reg->var_off = ptr_reg->var_off; |
3756 | dst_reg->id = ptr_reg->id; | |
b03c9f9f | 3757 | dst_reg->off = ptr_reg->off - smin_val; |
0962590e | 3758 | dst_reg->raw = ptr_reg->raw; |
f1174f77 EC |
3759 | break; |
3760 | } | |
f1174f77 EC |
3761 | /* A new variable offset is created. If the subtrahend is known |
3762 | * nonnegative, then any reg->range we had before is still good. | |
969bf05e | 3763 | */ |
b03c9f9f EC |
3764 | if (signed_sub_overflows(smin_ptr, smax_val) || |
3765 | signed_sub_overflows(smax_ptr, smin_val)) { | |
3766 | /* Overflow possible, we know nothing */ | |
3767 | dst_reg->smin_value = S64_MIN; | |
3768 | dst_reg->smax_value = S64_MAX; | |
3769 | } else { | |
3770 | dst_reg->smin_value = smin_ptr - smax_val; | |
3771 | dst_reg->smax_value = smax_ptr - smin_val; | |
3772 | } | |
3773 | if (umin_ptr < umax_val) { | |
3774 | /* Overflow possible, we know nothing */ | |
3775 | dst_reg->umin_value = 0; | |
3776 | dst_reg->umax_value = U64_MAX; | |
3777 | } else { | |
3778 | /* Cannot overflow (as long as bounds are consistent) */ | |
3779 | dst_reg->umin_value = umin_ptr - umax_val; | |
3780 | dst_reg->umax_value = umax_ptr - umin_val; | |
3781 | } | |
f1174f77 EC |
3782 | dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off); |
3783 | dst_reg->off = ptr_reg->off; | |
0962590e | 3784 | dst_reg->raw = ptr_reg->raw; |
de8f3a83 | 3785 | if (reg_is_pkt_pointer(ptr_reg)) { |
f1174f77 EC |
3786 | dst_reg->id = ++env->id_gen; |
3787 | /* something was added to pkt_ptr, set range to zero */ | |
b03c9f9f | 3788 | if (smin_val < 0) |
0962590e | 3789 | dst_reg->raw = 0; |
43188702 | 3790 | } |
f1174f77 EC |
3791 | break; |
3792 | case BPF_AND: | |
3793 | case BPF_OR: | |
3794 | case BPF_XOR: | |
82abbf8d AS |
3795 | /* bitwise ops on pointers are troublesome, prohibit. */ |
3796 | verbose(env, "R%d bitwise operator %s on pointer prohibited\n", | |
3797 | dst, bpf_alu_string[opcode >> 4]); | |
f1174f77 EC |
3798 | return -EACCES; |
3799 | default: | |
3800 | /* other operators (e.g. MUL,LSH) produce non-pointer results */ | |
82abbf8d AS |
3801 | verbose(env, "R%d pointer arithmetic with %s operator prohibited\n", |
3802 | dst, bpf_alu_string[opcode >> 4]); | |
f1174f77 | 3803 | return -EACCES; |
43188702 JF |
3804 | } |
3805 | ||
bb7f0f98 AS |
3806 | if (!check_reg_sane_offset(env, dst_reg, ptr_reg->type)) |
3807 | return -EINVAL; | |
3808 | ||
b03c9f9f EC |
3809 | __update_reg_bounds(dst_reg); |
3810 | __reg_deduce_bounds(dst_reg); | |
3811 | __reg_bound_offset(dst_reg); | |
0d6303db DB |
3812 | |
3813 | /* For unprivileged we require that resulting offset must be in bounds | |
3814 | * in order to be able to sanitize access later on. | |
3815 | */ | |
e4298d25 DB |
3816 | if (!env->allow_ptr_leaks) { |
3817 | if (dst_reg->type == PTR_TO_MAP_VALUE && | |
3818 | check_map_access(env, dst, dst_reg->off, 1, false)) { | |
3819 | verbose(env, "R%d pointer arithmetic of map value goes out of range, " | |
3820 | "prohibited for !root\n", dst); | |
3821 | return -EACCES; | |
3822 | } else if (dst_reg->type == PTR_TO_STACK && | |
3823 | check_stack_access(env, dst_reg, dst_reg->off + | |
3824 | dst_reg->var_off.value, 1)) { | |
3825 | verbose(env, "R%d stack pointer arithmetic goes out of range, " | |
3826 | "prohibited for !root\n", dst); | |
3827 | return -EACCES; | |
3828 | } | |
0d6303db DB |
3829 | } |
3830 | ||
43188702 JF |
3831 | return 0; |
3832 | } | |
3833 | ||
468f6eaf JH |
3834 | /* WARNING: This function does calculations on 64-bit values, but the actual |
3835 | * execution may occur on 32-bit values. Therefore, things like bitshifts | |
3836 | * need extra checks in the 32-bit case. | |
3837 | */ | |
f1174f77 EC |
3838 | static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env, |
3839 | struct bpf_insn *insn, | |
3840 | struct bpf_reg_state *dst_reg, | |
3841 | struct bpf_reg_state src_reg) | |
969bf05e | 3842 | { |
638f5b90 | 3843 | struct bpf_reg_state *regs = cur_regs(env); |
48461135 | 3844 | u8 opcode = BPF_OP(insn->code); |
f1174f77 | 3845 | bool src_known, dst_known; |
b03c9f9f EC |
3846 | s64 smin_val, smax_val; |
3847 | u64 umin_val, umax_val; | |
468f6eaf | 3848 | u64 insn_bitness = (BPF_CLASS(insn->code) == BPF_ALU64) ? 64 : 32; |
d3bd7413 DB |
3849 | u32 dst = insn->dst_reg; |
3850 | int ret; | |
48461135 | 3851 | |
b799207e JH |
3852 | if (insn_bitness == 32) { |
3853 | /* Relevant for 32-bit RSH: Information can propagate towards | |
3854 | * LSB, so it isn't sufficient to only truncate the output to | |
3855 | * 32 bits. | |
3856 | */ | |
3857 | coerce_reg_to_size(dst_reg, 4); | |
3858 | coerce_reg_to_size(&src_reg, 4); | |
3859 | } | |
3860 | ||
b03c9f9f EC |
3861 | smin_val = src_reg.smin_value; |
3862 | smax_val = src_reg.smax_value; | |
3863 | umin_val = src_reg.umin_value; | |
3864 | umax_val = src_reg.umax_value; | |
f1174f77 EC |
3865 | src_known = tnum_is_const(src_reg.var_off); |
3866 | dst_known = tnum_is_const(dst_reg->var_off); | |
f23cc643 | 3867 | |
6f16101e DB |
3868 | if ((src_known && (smin_val != smax_val || umin_val != umax_val)) || |
3869 | smin_val > smax_val || umin_val > umax_val) { | |
3870 | /* Taint dst register if offset had invalid bounds derived from | |
3871 | * e.g. dead branches. | |
3872 | */ | |
3873 | __mark_reg_unknown(dst_reg); | |
3874 | return 0; | |
3875 | } | |
3876 | ||
bb7f0f98 AS |
3877 | if (!src_known && |
3878 | opcode != BPF_ADD && opcode != BPF_SUB && opcode != BPF_AND) { | |
3879 | __mark_reg_unknown(dst_reg); | |
3880 | return 0; | |
3881 | } | |
3882 | ||
48461135 JB |
3883 | switch (opcode) { |
3884 | case BPF_ADD: | |
d3bd7413 DB |
3885 | ret = sanitize_val_alu(env, insn); |
3886 | if (ret < 0) { | |
3887 | verbose(env, "R%d tried to add from different pointers or scalars\n", dst); | |
3888 | return ret; | |
3889 | } | |
b03c9f9f EC |
3890 | if (signed_add_overflows(dst_reg->smin_value, smin_val) || |
3891 | signed_add_overflows(dst_reg->smax_value, smax_val)) { | |
3892 | dst_reg->smin_value = S64_MIN; | |
3893 | dst_reg->smax_value = S64_MAX; | |
3894 | } else { | |
3895 | dst_reg->smin_value += smin_val; | |
3896 | dst_reg->smax_value += smax_val; | |
3897 | } | |
3898 | if (dst_reg->umin_value + umin_val < umin_val || | |
3899 | dst_reg->umax_value + umax_val < umax_val) { | |
3900 | dst_reg->umin_value = 0; | |
3901 | dst_reg->umax_value = U64_MAX; | |
3902 | } else { | |
3903 | dst_reg->umin_value += umin_val; | |
3904 | dst_reg->umax_value += umax_val; | |
3905 | } | |
f1174f77 | 3906 | dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off); |
48461135 JB |
3907 | break; |
3908 | case BPF_SUB: | |
d3bd7413 DB |
3909 | ret = sanitize_val_alu(env, insn); |
3910 | if (ret < 0) { | |
3911 | verbose(env, "R%d tried to sub from different pointers or scalars\n", dst); | |
3912 | return ret; | |
3913 | } | |
b03c9f9f EC |
3914 | if (signed_sub_overflows(dst_reg->smin_value, smax_val) || |
3915 | signed_sub_overflows(dst_reg->smax_value, smin_val)) { | |
3916 | /* Overflow possible, we know nothing */ | |
3917 | dst_reg->smin_value = S64_MIN; | |
3918 | dst_reg->smax_value = S64_MAX; | |
3919 | } else { | |
3920 | dst_reg->smin_value -= smax_val; | |
3921 | dst_reg->smax_value -= smin_val; | |
3922 | } | |
3923 | if (dst_reg->umin_value < umax_val) { | |
3924 | /* Overflow possible, we know nothing */ | |
3925 | dst_reg->umin_value = 0; | |
3926 | dst_reg->umax_value = U64_MAX; | |
3927 | } else { | |
3928 | /* Cannot overflow (as long as bounds are consistent) */ | |
3929 | dst_reg->umin_value -= umax_val; | |
3930 | dst_reg->umax_value -= umin_val; | |
3931 | } | |
f1174f77 | 3932 | dst_reg->var_off = tnum_sub(dst_reg->var_off, src_reg.var_off); |
48461135 JB |
3933 | break; |
3934 | case BPF_MUL: | |
b03c9f9f EC |
3935 | dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off); |
3936 | if (smin_val < 0 || dst_reg->smin_value < 0) { | |
f1174f77 | 3937 | /* Ain't nobody got time to multiply that sign */ |
b03c9f9f EC |
3938 | __mark_reg_unbounded(dst_reg); |
3939 | __update_reg_bounds(dst_reg); | |
f1174f77 EC |
3940 | break; |
3941 | } | |
b03c9f9f EC |
3942 | /* Both values are positive, so we can work with unsigned and |
3943 | * copy the result to signed (unless it exceeds S64_MAX). | |
f1174f77 | 3944 | */ |
b03c9f9f EC |
3945 | if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) { |
3946 | /* Potential overflow, we know nothing */ | |
3947 | __mark_reg_unbounded(dst_reg); | |
3948 | /* (except what we can learn from the var_off) */ | |
3949 | __update_reg_bounds(dst_reg); | |
3950 | break; | |
3951 | } | |
3952 | dst_reg->umin_value *= umin_val; | |
3953 | dst_reg->umax_value *= umax_val; | |
3954 | if (dst_reg->umax_value > S64_MAX) { | |
3955 | /* Overflow possible, we know nothing */ | |
3956 | dst_reg->smin_value = S64_MIN; | |
3957 | dst_reg->smax_value = S64_MAX; | |
3958 | } else { | |
3959 | dst_reg->smin_value = dst_reg->umin_value; | |
3960 | dst_reg->smax_value = dst_reg->umax_value; | |
3961 | } | |
48461135 JB |
3962 | break; |
3963 | case BPF_AND: | |
f1174f77 | 3964 | if (src_known && dst_known) { |
b03c9f9f EC |
3965 | __mark_reg_known(dst_reg, dst_reg->var_off.value & |
3966 | src_reg.var_off.value); | |
f1174f77 EC |
3967 | break; |
3968 | } | |
b03c9f9f EC |
3969 | /* We get our minimum from the var_off, since that's inherently |
3970 | * bitwise. Our maximum is the minimum of the operands' maxima. | |
f23cc643 | 3971 | */ |
f1174f77 | 3972 | dst_reg->var_off = tnum_and(dst_reg->var_off, src_reg.var_off); |
b03c9f9f EC |
3973 | dst_reg->umin_value = dst_reg->var_off.value; |
3974 | dst_reg->umax_value = min(dst_reg->umax_value, umax_val); | |
3975 | if (dst_reg->smin_value < 0 || smin_val < 0) { | |
3976 | /* Lose signed bounds when ANDing negative numbers, | |
3977 | * ain't nobody got time for that. | |
3978 | */ | |
3979 | dst_reg->smin_value = S64_MIN; | |
3980 | dst_reg->smax_value = S64_MAX; | |
3981 | } else { | |
3982 | /* ANDing two positives gives a positive, so safe to | |
3983 | * cast result into s64. | |
3984 | */ | |
3985 | dst_reg->smin_value = dst_reg->umin_value; | |
3986 | dst_reg->smax_value = dst_reg->umax_value; | |
3987 | } | |
3988 | /* We may learn something more from the var_off */ | |
3989 | __update_reg_bounds(dst_reg); | |
f1174f77 EC |
3990 | break; |
3991 | case BPF_OR: | |
3992 | if (src_known && dst_known) { | |
b03c9f9f EC |
3993 | __mark_reg_known(dst_reg, dst_reg->var_off.value | |
3994 | src_reg.var_off.value); | |
f1174f77 EC |
3995 | break; |
3996 | } | |
b03c9f9f EC |
3997 | /* We get our maximum from the var_off, and our minimum is the |
3998 | * maximum of the operands' minima | |
f1174f77 EC |
3999 | */ |
4000 | dst_reg->var_off = tnum_or(dst_reg->var_off, src_reg.var_off); | |
b03c9f9f EC |
4001 | dst_reg->umin_value = max(dst_reg->umin_value, umin_val); |
4002 | dst_reg->umax_value = dst_reg->var_off.value | | |
4003 | dst_reg->var_off.mask; | |
4004 | if (dst_reg->smin_value < 0 || smin_val < 0) { | |
4005 | /* Lose signed bounds when ORing negative numbers, | |
4006 | * ain't nobody got time for that. | |
4007 | */ | |
4008 | dst_reg->smin_value = S64_MIN; | |
4009 | dst_reg->smax_value = S64_MAX; | |
f1174f77 | 4010 | } else { |
b03c9f9f EC |
4011 | /* ORing two positives gives a positive, so safe to |
4012 | * cast result into s64. | |
4013 | */ | |
4014 | dst_reg->smin_value = dst_reg->umin_value; | |
4015 | dst_reg->smax_value = dst_reg->umax_value; | |
f1174f77 | 4016 | } |
b03c9f9f EC |
4017 | /* We may learn something more from the var_off */ |
4018 | __update_reg_bounds(dst_reg); | |
48461135 JB |
4019 | break; |
4020 | case BPF_LSH: | |
468f6eaf JH |
4021 | if (umax_val >= insn_bitness) { |
4022 | /* Shifts greater than 31 or 63 are undefined. | |
4023 | * This includes shifts by a negative number. | |
b03c9f9f | 4024 | */ |
61bd5218 | 4025 | mark_reg_unknown(env, regs, insn->dst_reg); |
f1174f77 EC |
4026 | break; |
4027 | } | |
b03c9f9f EC |
4028 | /* We lose all sign bit information (except what we can pick |
4029 | * up from var_off) | |
48461135 | 4030 | */ |
b03c9f9f EC |
4031 | dst_reg->smin_value = S64_MIN; |
4032 | dst_reg->smax_value = S64_MAX; | |
4033 | /* If we might shift our top bit out, then we know nothing */ | |
4034 | if (dst_reg->umax_value > 1ULL << (63 - umax_val)) { | |
4035 | dst_reg->umin_value = 0; | |
4036 | dst_reg->umax_value = U64_MAX; | |
d1174416 | 4037 | } else { |
b03c9f9f EC |
4038 | dst_reg->umin_value <<= umin_val; |
4039 | dst_reg->umax_value <<= umax_val; | |
d1174416 | 4040 | } |
afbe1a5b | 4041 | dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val); |
b03c9f9f EC |
4042 | /* We may learn something more from the var_off */ |
4043 | __update_reg_bounds(dst_reg); | |
48461135 JB |
4044 | break; |
4045 | case BPF_RSH: | |
468f6eaf JH |
4046 | if (umax_val >= insn_bitness) { |
4047 | /* Shifts greater than 31 or 63 are undefined. | |
4048 | * This includes shifts by a negative number. | |
b03c9f9f | 4049 | */ |
61bd5218 | 4050 | mark_reg_unknown(env, regs, insn->dst_reg); |
f1174f77 EC |
4051 | break; |
4052 | } | |
4374f256 EC |
4053 | /* BPF_RSH is an unsigned shift. If the value in dst_reg might |
4054 | * be negative, then either: | |
4055 | * 1) src_reg might be zero, so the sign bit of the result is | |
4056 | * unknown, so we lose our signed bounds | |
4057 | * 2) it's known negative, thus the unsigned bounds capture the | |
4058 | * signed bounds | |
4059 | * 3) the signed bounds cross zero, so they tell us nothing | |
4060 | * about the result | |
4061 | * If the value in dst_reg is known nonnegative, then again the | |
4062 | * unsigned bounts capture the signed bounds. | |
4063 | * Thus, in all cases it suffices to blow away our signed bounds | |
4064 | * and rely on inferring new ones from the unsigned bounds and | |
4065 | * var_off of the result. | |
4066 | */ | |
4067 | dst_reg->smin_value = S64_MIN; | |
4068 | dst_reg->smax_value = S64_MAX; | |
afbe1a5b | 4069 | dst_reg->var_off = tnum_rshift(dst_reg->var_off, umin_val); |
b03c9f9f EC |
4070 | dst_reg->umin_value >>= umax_val; |
4071 | dst_reg->umax_value >>= umin_val; | |
4072 | /* We may learn something more from the var_off */ | |
4073 | __update_reg_bounds(dst_reg); | |
48461135 | 4074 | break; |
9cbe1f5a YS |
4075 | case BPF_ARSH: |
4076 | if (umax_val >= insn_bitness) { | |
4077 | /* Shifts greater than 31 or 63 are undefined. | |
4078 | * This includes shifts by a negative number. | |
4079 | */ | |
4080 | mark_reg_unknown(env, regs, insn->dst_reg); | |
4081 | break; | |
4082 | } | |
4083 | ||
4084 | /* Upon reaching here, src_known is true and | |
4085 | * umax_val is equal to umin_val. | |
4086 | */ | |
4087 | dst_reg->smin_value >>= umin_val; | |
4088 | dst_reg->smax_value >>= umin_val; | |
4089 | dst_reg->var_off = tnum_arshift(dst_reg->var_off, umin_val); | |
4090 | ||
4091 | /* blow away the dst_reg umin_value/umax_value and rely on | |
4092 | * dst_reg var_off to refine the result. | |
4093 | */ | |
4094 | dst_reg->umin_value = 0; | |
4095 | dst_reg->umax_value = U64_MAX; | |
4096 | __update_reg_bounds(dst_reg); | |
4097 | break; | |
48461135 | 4098 | default: |
61bd5218 | 4099 | mark_reg_unknown(env, regs, insn->dst_reg); |
48461135 JB |
4100 | break; |
4101 | } | |
4102 | ||
468f6eaf JH |
4103 | if (BPF_CLASS(insn->code) != BPF_ALU64) { |
4104 | /* 32-bit ALU ops are (32,32)->32 */ | |
4105 | coerce_reg_to_size(dst_reg, 4); | |
468f6eaf JH |
4106 | } |
4107 | ||
b03c9f9f EC |
4108 | __reg_deduce_bounds(dst_reg); |
4109 | __reg_bound_offset(dst_reg); | |
f1174f77 EC |
4110 | return 0; |
4111 | } | |
4112 | ||
4113 | /* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max | |
4114 | * and var_off. | |
4115 | */ | |
4116 | static int adjust_reg_min_max_vals(struct bpf_verifier_env *env, | |
4117 | struct bpf_insn *insn) | |
4118 | { | |
f4d7e40a AS |
4119 | struct bpf_verifier_state *vstate = env->cur_state; |
4120 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
4121 | struct bpf_reg_state *regs = state->regs, *dst_reg, *src_reg; | |
f1174f77 EC |
4122 | struct bpf_reg_state *ptr_reg = NULL, off_reg = {0}; |
4123 | u8 opcode = BPF_OP(insn->code); | |
f1174f77 EC |
4124 | |
4125 | dst_reg = ®s[insn->dst_reg]; | |
f1174f77 EC |
4126 | src_reg = NULL; |
4127 | if (dst_reg->type != SCALAR_VALUE) | |
4128 | ptr_reg = dst_reg; | |
4129 | if (BPF_SRC(insn->code) == BPF_X) { | |
4130 | src_reg = ®s[insn->src_reg]; | |
f1174f77 EC |
4131 | if (src_reg->type != SCALAR_VALUE) { |
4132 | if (dst_reg->type != SCALAR_VALUE) { | |
4133 | /* Combining two pointers by any ALU op yields | |
82abbf8d AS |
4134 | * an arbitrary scalar. Disallow all math except |
4135 | * pointer subtraction | |
f1174f77 | 4136 | */ |
dd066823 | 4137 | if (opcode == BPF_SUB && env->allow_ptr_leaks) { |
82abbf8d AS |
4138 | mark_reg_unknown(env, regs, insn->dst_reg); |
4139 | return 0; | |
f1174f77 | 4140 | } |
82abbf8d AS |
4141 | verbose(env, "R%d pointer %s pointer prohibited\n", |
4142 | insn->dst_reg, | |
4143 | bpf_alu_string[opcode >> 4]); | |
4144 | return -EACCES; | |
f1174f77 EC |
4145 | } else { |
4146 | /* scalar += pointer | |
4147 | * This is legal, but we have to reverse our | |
4148 | * src/dest handling in computing the range | |
4149 | */ | |
82abbf8d AS |
4150 | return adjust_ptr_min_max_vals(env, insn, |
4151 | src_reg, dst_reg); | |
f1174f77 EC |
4152 | } |
4153 | } else if (ptr_reg) { | |
4154 | /* pointer += scalar */ | |
82abbf8d AS |
4155 | return adjust_ptr_min_max_vals(env, insn, |
4156 | dst_reg, src_reg); | |
f1174f77 EC |
4157 | } |
4158 | } else { | |
4159 | /* Pretend the src is a reg with a known value, since we only | |
4160 | * need to be able to read from this state. | |
4161 | */ | |
4162 | off_reg.type = SCALAR_VALUE; | |
b03c9f9f | 4163 | __mark_reg_known(&off_reg, insn->imm); |
f1174f77 | 4164 | src_reg = &off_reg; |
82abbf8d AS |
4165 | if (ptr_reg) /* pointer += K */ |
4166 | return adjust_ptr_min_max_vals(env, insn, | |
4167 | ptr_reg, src_reg); | |
f1174f77 EC |
4168 | } |
4169 | ||
4170 | /* Got here implies adding two SCALAR_VALUEs */ | |
4171 | if (WARN_ON_ONCE(ptr_reg)) { | |
f4d7e40a | 4172 | print_verifier_state(env, state); |
61bd5218 | 4173 | verbose(env, "verifier internal error: unexpected ptr_reg\n"); |
f1174f77 EC |
4174 | return -EINVAL; |
4175 | } | |
4176 | if (WARN_ON(!src_reg)) { | |
f4d7e40a | 4177 | print_verifier_state(env, state); |
61bd5218 | 4178 | verbose(env, "verifier internal error: no src_reg\n"); |
f1174f77 EC |
4179 | return -EINVAL; |
4180 | } | |
4181 | return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg); | |
48461135 JB |
4182 | } |
4183 | ||
17a52670 | 4184 | /* check validity of 32-bit and 64-bit arithmetic operations */ |
58e2af8b | 4185 | static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn) |
17a52670 | 4186 | { |
638f5b90 | 4187 | struct bpf_reg_state *regs = cur_regs(env); |
17a52670 AS |
4188 | u8 opcode = BPF_OP(insn->code); |
4189 | int err; | |
4190 | ||
4191 | if (opcode == BPF_END || opcode == BPF_NEG) { | |
4192 | if (opcode == BPF_NEG) { | |
4193 | if (BPF_SRC(insn->code) != 0 || | |
4194 | insn->src_reg != BPF_REG_0 || | |
4195 | insn->off != 0 || insn->imm != 0) { | |
61bd5218 | 4196 | verbose(env, "BPF_NEG uses reserved fields\n"); |
17a52670 AS |
4197 | return -EINVAL; |
4198 | } | |
4199 | } else { | |
4200 | if (insn->src_reg != BPF_REG_0 || insn->off != 0 || | |
e67b8a68 EC |
4201 | (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) || |
4202 | BPF_CLASS(insn->code) == BPF_ALU64) { | |
61bd5218 | 4203 | verbose(env, "BPF_END uses reserved fields\n"); |
17a52670 AS |
4204 | return -EINVAL; |
4205 | } | |
4206 | } | |
4207 | ||
4208 | /* check src operand */ | |
dc503a8a | 4209 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
4210 | if (err) |
4211 | return err; | |
4212 | ||
1be7f75d | 4213 | if (is_pointer_value(env, insn->dst_reg)) { |
61bd5218 | 4214 | verbose(env, "R%d pointer arithmetic prohibited\n", |
1be7f75d AS |
4215 | insn->dst_reg); |
4216 | return -EACCES; | |
4217 | } | |
4218 | ||
17a52670 | 4219 | /* check dest operand */ |
dc503a8a | 4220 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
17a52670 AS |
4221 | if (err) |
4222 | return err; | |
4223 | ||
4224 | } else if (opcode == BPF_MOV) { | |
4225 | ||
4226 | if (BPF_SRC(insn->code) == BPF_X) { | |
4227 | if (insn->imm != 0 || insn->off != 0) { | |
61bd5218 | 4228 | verbose(env, "BPF_MOV uses reserved fields\n"); |
17a52670 AS |
4229 | return -EINVAL; |
4230 | } | |
4231 | ||
4232 | /* check src operand */ | |
dc503a8a | 4233 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
4234 | if (err) |
4235 | return err; | |
4236 | } else { | |
4237 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { | |
61bd5218 | 4238 | verbose(env, "BPF_MOV uses reserved fields\n"); |
17a52670 AS |
4239 | return -EINVAL; |
4240 | } | |
4241 | } | |
4242 | ||
fbeb1603 AF |
4243 | /* check dest operand, mark as required later */ |
4244 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); | |
17a52670 AS |
4245 | if (err) |
4246 | return err; | |
4247 | ||
4248 | if (BPF_SRC(insn->code) == BPF_X) { | |
e434b8cd JW |
4249 | struct bpf_reg_state *src_reg = regs + insn->src_reg; |
4250 | struct bpf_reg_state *dst_reg = regs + insn->dst_reg; | |
4251 | ||
17a52670 AS |
4252 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
4253 | /* case: R1 = R2 | |
4254 | * copy register state to dest reg | |
4255 | */ | |
e434b8cd JW |
4256 | *dst_reg = *src_reg; |
4257 | dst_reg->live |= REG_LIVE_WRITTEN; | |
17a52670 | 4258 | } else { |
f1174f77 | 4259 | /* R1 = (u32) R2 */ |
1be7f75d | 4260 | if (is_pointer_value(env, insn->src_reg)) { |
61bd5218 JK |
4261 | verbose(env, |
4262 | "R%d partial copy of pointer\n", | |
1be7f75d AS |
4263 | insn->src_reg); |
4264 | return -EACCES; | |
e434b8cd JW |
4265 | } else if (src_reg->type == SCALAR_VALUE) { |
4266 | *dst_reg = *src_reg; | |
4267 | dst_reg->live |= REG_LIVE_WRITTEN; | |
4268 | } else { | |
4269 | mark_reg_unknown(env, regs, | |
4270 | insn->dst_reg); | |
1be7f75d | 4271 | } |
e434b8cd | 4272 | coerce_reg_to_size(dst_reg, 4); |
17a52670 AS |
4273 | } |
4274 | } else { | |
4275 | /* case: R = imm | |
4276 | * remember the value we stored into this reg | |
4277 | */ | |
fbeb1603 AF |
4278 | /* clear any state __mark_reg_known doesn't set */ |
4279 | mark_reg_unknown(env, regs, insn->dst_reg); | |
f1174f77 | 4280 | regs[insn->dst_reg].type = SCALAR_VALUE; |
95a762e2 JH |
4281 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
4282 | __mark_reg_known(regs + insn->dst_reg, | |
4283 | insn->imm); | |
4284 | } else { | |
4285 | __mark_reg_known(regs + insn->dst_reg, | |
4286 | (u32)insn->imm); | |
4287 | } | |
17a52670 AS |
4288 | } |
4289 | ||
4290 | } else if (opcode > BPF_END) { | |
61bd5218 | 4291 | verbose(env, "invalid BPF_ALU opcode %x\n", opcode); |
17a52670 AS |
4292 | return -EINVAL; |
4293 | ||
4294 | } else { /* all other ALU ops: and, sub, xor, add, ... */ | |
4295 | ||
17a52670 AS |
4296 | if (BPF_SRC(insn->code) == BPF_X) { |
4297 | if (insn->imm != 0 || insn->off != 0) { | |
61bd5218 | 4298 | verbose(env, "BPF_ALU uses reserved fields\n"); |
17a52670 AS |
4299 | return -EINVAL; |
4300 | } | |
4301 | /* check src1 operand */ | |
dc503a8a | 4302 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
4303 | if (err) |
4304 | return err; | |
4305 | } else { | |
4306 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { | |
61bd5218 | 4307 | verbose(env, "BPF_ALU uses reserved fields\n"); |
17a52670 AS |
4308 | return -EINVAL; |
4309 | } | |
4310 | } | |
4311 | ||
4312 | /* check src2 operand */ | |
dc503a8a | 4313 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
4314 | if (err) |
4315 | return err; | |
4316 | ||
4317 | if ((opcode == BPF_MOD || opcode == BPF_DIV) && | |
4318 | BPF_SRC(insn->code) == BPF_K && insn->imm == 0) { | |
61bd5218 | 4319 | verbose(env, "div by zero\n"); |
17a52670 AS |
4320 | return -EINVAL; |
4321 | } | |
4322 | ||
229394e8 RV |
4323 | if ((opcode == BPF_LSH || opcode == BPF_RSH || |
4324 | opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) { | |
4325 | int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32; | |
4326 | ||
4327 | if (insn->imm < 0 || insn->imm >= size) { | |
61bd5218 | 4328 | verbose(env, "invalid shift %d\n", insn->imm); |
229394e8 RV |
4329 | return -EINVAL; |
4330 | } | |
4331 | } | |
4332 | ||
1a0dc1ac | 4333 | /* check dest operand */ |
dc503a8a | 4334 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
1a0dc1ac AS |
4335 | if (err) |
4336 | return err; | |
4337 | ||
f1174f77 | 4338 | return adjust_reg_min_max_vals(env, insn); |
17a52670 AS |
4339 | } |
4340 | ||
4341 | return 0; | |
4342 | } | |
4343 | ||
c6a9efa1 PC |
4344 | static void __find_good_pkt_pointers(struct bpf_func_state *state, |
4345 | struct bpf_reg_state *dst_reg, | |
4346 | enum bpf_reg_type type, u16 new_range) | |
4347 | { | |
4348 | struct bpf_reg_state *reg; | |
4349 | int i; | |
4350 | ||
4351 | for (i = 0; i < MAX_BPF_REG; i++) { | |
4352 | reg = &state->regs[i]; | |
4353 | if (reg->type == type && reg->id == dst_reg->id) | |
4354 | /* keep the maximum range already checked */ | |
4355 | reg->range = max(reg->range, new_range); | |
4356 | } | |
4357 | ||
4358 | bpf_for_each_spilled_reg(i, state, reg) { | |
4359 | if (!reg) | |
4360 | continue; | |
4361 | if (reg->type == type && reg->id == dst_reg->id) | |
4362 | reg->range = max(reg->range, new_range); | |
4363 | } | |
4364 | } | |
4365 | ||
f4d7e40a | 4366 | static void find_good_pkt_pointers(struct bpf_verifier_state *vstate, |
de8f3a83 | 4367 | struct bpf_reg_state *dst_reg, |
f8ddadc4 | 4368 | enum bpf_reg_type type, |
fb2a311a | 4369 | bool range_right_open) |
969bf05e | 4370 | { |
fb2a311a | 4371 | u16 new_range; |
c6a9efa1 | 4372 | int i; |
2d2be8ca | 4373 | |
fb2a311a DB |
4374 | if (dst_reg->off < 0 || |
4375 | (dst_reg->off == 0 && range_right_open)) | |
f1174f77 EC |
4376 | /* This doesn't give us any range */ |
4377 | return; | |
4378 | ||
b03c9f9f EC |
4379 | if (dst_reg->umax_value > MAX_PACKET_OFF || |
4380 | dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF) | |
f1174f77 EC |
4381 | /* Risk of overflow. For instance, ptr + (1<<63) may be less |
4382 | * than pkt_end, but that's because it's also less than pkt. | |
4383 | */ | |
4384 | return; | |
4385 | ||
fb2a311a DB |
4386 | new_range = dst_reg->off; |
4387 | if (range_right_open) | |
4388 | new_range--; | |
4389 | ||
4390 | /* Examples for register markings: | |
2d2be8ca | 4391 | * |
fb2a311a | 4392 | * pkt_data in dst register: |
2d2be8ca DB |
4393 | * |
4394 | * r2 = r3; | |
4395 | * r2 += 8; | |
4396 | * if (r2 > pkt_end) goto <handle exception> | |
4397 | * <access okay> | |
4398 | * | |
b4e432f1 DB |
4399 | * r2 = r3; |
4400 | * r2 += 8; | |
4401 | * if (r2 < pkt_end) goto <access okay> | |
4402 | * <handle exception> | |
4403 | * | |
2d2be8ca DB |
4404 | * Where: |
4405 | * r2 == dst_reg, pkt_end == src_reg | |
4406 | * r2=pkt(id=n,off=8,r=0) | |
4407 | * r3=pkt(id=n,off=0,r=0) | |
4408 | * | |
fb2a311a | 4409 | * pkt_data in src register: |
2d2be8ca DB |
4410 | * |
4411 | * r2 = r3; | |
4412 | * r2 += 8; | |
4413 | * if (pkt_end >= r2) goto <access okay> | |
4414 | * <handle exception> | |
4415 | * | |
b4e432f1 DB |
4416 | * r2 = r3; |
4417 | * r2 += 8; | |
4418 | * if (pkt_end <= r2) goto <handle exception> | |
4419 | * <access okay> | |
4420 | * | |
2d2be8ca DB |
4421 | * Where: |
4422 | * pkt_end == dst_reg, r2 == src_reg | |
4423 | * r2=pkt(id=n,off=8,r=0) | |
4424 | * r3=pkt(id=n,off=0,r=0) | |
4425 | * | |
4426 | * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8) | |
fb2a311a DB |
4427 | * or r3=pkt(id=n,off=0,r=8-1), so that range of bytes [r3, r3 + 8) |
4428 | * and [r3, r3 + 8-1) respectively is safe to access depending on | |
4429 | * the check. | |
969bf05e | 4430 | */ |
2d2be8ca | 4431 | |
f1174f77 EC |
4432 | /* If our ids match, then we must have the same max_value. And we |
4433 | * don't care about the other reg's fixed offset, since if it's too big | |
4434 | * the range won't allow anything. | |
4435 | * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16. | |
4436 | */ | |
c6a9efa1 PC |
4437 | for (i = 0; i <= vstate->curframe; i++) |
4438 | __find_good_pkt_pointers(vstate->frame[i], dst_reg, type, | |
4439 | new_range); | |
969bf05e AS |
4440 | } |
4441 | ||
4f7b3e82 AS |
4442 | /* compute branch direction of the expression "if (reg opcode val) goto target;" |
4443 | * and return: | |
4444 | * 1 - branch will be taken and "goto target" will be executed | |
4445 | * 0 - branch will not be taken and fall-through to next insn | |
4446 | * -1 - unknown. Example: "if (reg < 5)" is unknown when register value range [0,10] | |
4447 | */ | |
092ed096 JW |
4448 | static int is_branch_taken(struct bpf_reg_state *reg, u64 val, u8 opcode, |
4449 | bool is_jmp32) | |
4f7b3e82 | 4450 | { |
092ed096 | 4451 | struct bpf_reg_state reg_lo; |
a72dafaf JW |
4452 | s64 sval; |
4453 | ||
4f7b3e82 AS |
4454 | if (__is_pointer_value(false, reg)) |
4455 | return -1; | |
4456 | ||
092ed096 JW |
4457 | if (is_jmp32) { |
4458 | reg_lo = *reg; | |
4459 | reg = ®_lo; | |
4460 | /* For JMP32, only low 32 bits are compared, coerce_reg_to_size | |
4461 | * could truncate high bits and update umin/umax according to | |
4462 | * information of low bits. | |
4463 | */ | |
4464 | coerce_reg_to_size(reg, 4); | |
4465 | /* smin/smax need special handling. For example, after coerce, | |
4466 | * if smin_value is 0x00000000ffffffffLL, the value is -1 when | |
4467 | * used as operand to JMP32. It is a negative number from s32's | |
4468 | * point of view, while it is a positive number when seen as | |
4469 | * s64. The smin/smax are kept as s64, therefore, when used with | |
4470 | * JMP32, they need to be transformed into s32, then sign | |
4471 | * extended back to s64. | |
4472 | * | |
4473 | * Also, smin/smax were copied from umin/umax. If umin/umax has | |
4474 | * different sign bit, then min/max relationship doesn't | |
4475 | * maintain after casting into s32, for this case, set smin/smax | |
4476 | * to safest range. | |
4477 | */ | |
4478 | if ((reg->umax_value ^ reg->umin_value) & | |
4479 | (1ULL << 31)) { | |
4480 | reg->smin_value = S32_MIN; | |
4481 | reg->smax_value = S32_MAX; | |
4482 | } | |
4483 | reg->smin_value = (s64)(s32)reg->smin_value; | |
4484 | reg->smax_value = (s64)(s32)reg->smax_value; | |
4485 | ||
4486 | val = (u32)val; | |
4487 | sval = (s64)(s32)val; | |
4488 | } else { | |
4489 | sval = (s64)val; | |
4490 | } | |
a72dafaf | 4491 | |
4f7b3e82 AS |
4492 | switch (opcode) { |
4493 | case BPF_JEQ: | |
4494 | if (tnum_is_const(reg->var_off)) | |
4495 | return !!tnum_equals_const(reg->var_off, val); | |
4496 | break; | |
4497 | case BPF_JNE: | |
4498 | if (tnum_is_const(reg->var_off)) | |
4499 | return !tnum_equals_const(reg->var_off, val); | |
4500 | break; | |
960ea056 JK |
4501 | case BPF_JSET: |
4502 | if ((~reg->var_off.mask & reg->var_off.value) & val) | |
4503 | return 1; | |
4504 | if (!((reg->var_off.mask | reg->var_off.value) & val)) | |
4505 | return 0; | |
4506 | break; | |
4f7b3e82 AS |
4507 | case BPF_JGT: |
4508 | if (reg->umin_value > val) | |
4509 | return 1; | |
4510 | else if (reg->umax_value <= val) | |
4511 | return 0; | |
4512 | break; | |
4513 | case BPF_JSGT: | |
a72dafaf | 4514 | if (reg->smin_value > sval) |
4f7b3e82 | 4515 | return 1; |
a72dafaf | 4516 | else if (reg->smax_value < sval) |
4f7b3e82 AS |
4517 | return 0; |
4518 | break; | |
4519 | case BPF_JLT: | |
4520 | if (reg->umax_value < val) | |
4521 | return 1; | |
4522 | else if (reg->umin_value >= val) | |
4523 | return 0; | |
4524 | break; | |
4525 | case BPF_JSLT: | |
a72dafaf | 4526 | if (reg->smax_value < sval) |
4f7b3e82 | 4527 | return 1; |
a72dafaf | 4528 | else if (reg->smin_value >= sval) |
4f7b3e82 AS |
4529 | return 0; |
4530 | break; | |
4531 | case BPF_JGE: | |
4532 | if (reg->umin_value >= val) | |
4533 | return 1; | |
4534 | else if (reg->umax_value < val) | |
4535 | return 0; | |
4536 | break; | |
4537 | case BPF_JSGE: | |
a72dafaf | 4538 | if (reg->smin_value >= sval) |
4f7b3e82 | 4539 | return 1; |
a72dafaf | 4540 | else if (reg->smax_value < sval) |
4f7b3e82 AS |
4541 | return 0; |
4542 | break; | |
4543 | case BPF_JLE: | |
4544 | if (reg->umax_value <= val) | |
4545 | return 1; | |
4546 | else if (reg->umin_value > val) | |
4547 | return 0; | |
4548 | break; | |
4549 | case BPF_JSLE: | |
a72dafaf | 4550 | if (reg->smax_value <= sval) |
4f7b3e82 | 4551 | return 1; |
a72dafaf | 4552 | else if (reg->smin_value > sval) |
4f7b3e82 AS |
4553 | return 0; |
4554 | break; | |
4555 | } | |
4556 | ||
4557 | return -1; | |
4558 | } | |
4559 | ||
092ed096 JW |
4560 | /* Generate min value of the high 32-bit from TNUM info. */ |
4561 | static u64 gen_hi_min(struct tnum var) | |
4562 | { | |
4563 | return var.value & ~0xffffffffULL; | |
4564 | } | |
4565 | ||
4566 | /* Generate max value of the high 32-bit from TNUM info. */ | |
4567 | static u64 gen_hi_max(struct tnum var) | |
4568 | { | |
4569 | return (var.value | var.mask) & ~0xffffffffULL; | |
4570 | } | |
4571 | ||
4572 | /* Return true if VAL is compared with a s64 sign extended from s32, and they | |
4573 | * are with the same signedness. | |
4574 | */ | |
4575 | static bool cmp_val_with_extended_s64(s64 sval, struct bpf_reg_state *reg) | |
4576 | { | |
4577 | return ((s32)sval >= 0 && | |
4578 | reg->smin_value >= 0 && reg->smax_value <= S32_MAX) || | |
4579 | ((s32)sval < 0 && | |
4580 | reg->smax_value <= 0 && reg->smin_value >= S32_MIN); | |
4581 | } | |
4582 | ||
48461135 JB |
4583 | /* Adjusts the register min/max values in the case that the dst_reg is the |
4584 | * variable register that we are working on, and src_reg is a constant or we're | |
4585 | * simply doing a BPF_K check. | |
f1174f77 | 4586 | * In JEQ/JNE cases we also adjust the var_off values. |
48461135 JB |
4587 | */ |
4588 | static void reg_set_min_max(struct bpf_reg_state *true_reg, | |
4589 | struct bpf_reg_state *false_reg, u64 val, | |
092ed096 | 4590 | u8 opcode, bool is_jmp32) |
48461135 | 4591 | { |
a72dafaf JW |
4592 | s64 sval; |
4593 | ||
f1174f77 EC |
4594 | /* If the dst_reg is a pointer, we can't learn anything about its |
4595 | * variable offset from the compare (unless src_reg were a pointer into | |
4596 | * the same object, but we don't bother with that. | |
4597 | * Since false_reg and true_reg have the same type by construction, we | |
4598 | * only need to check one of them for pointerness. | |
4599 | */ | |
4600 | if (__is_pointer_value(false, false_reg)) | |
4601 | return; | |
4cabc5b1 | 4602 | |
092ed096 JW |
4603 | val = is_jmp32 ? (u32)val : val; |
4604 | sval = is_jmp32 ? (s64)(s32)val : (s64)val; | |
a72dafaf | 4605 | |
48461135 JB |
4606 | switch (opcode) { |
4607 | case BPF_JEQ: | |
48461135 | 4608 | case BPF_JNE: |
a72dafaf JW |
4609 | { |
4610 | struct bpf_reg_state *reg = | |
4611 | opcode == BPF_JEQ ? true_reg : false_reg; | |
4612 | ||
4613 | /* For BPF_JEQ, if this is false we know nothing Jon Snow, but | |
4614 | * if it is true we know the value for sure. Likewise for | |
4615 | * BPF_JNE. | |
48461135 | 4616 | */ |
092ed096 JW |
4617 | if (is_jmp32) { |
4618 | u64 old_v = reg->var_off.value; | |
4619 | u64 hi_mask = ~0xffffffffULL; | |
4620 | ||
4621 | reg->var_off.value = (old_v & hi_mask) | val; | |
4622 | reg->var_off.mask &= hi_mask; | |
4623 | } else { | |
4624 | __mark_reg_known(reg, val); | |
4625 | } | |
48461135 | 4626 | break; |
a72dafaf | 4627 | } |
960ea056 JK |
4628 | case BPF_JSET: |
4629 | false_reg->var_off = tnum_and(false_reg->var_off, | |
4630 | tnum_const(~val)); | |
4631 | if (is_power_of_2(val)) | |
4632 | true_reg->var_off = tnum_or(true_reg->var_off, | |
4633 | tnum_const(val)); | |
4634 | break; | |
48461135 | 4635 | case BPF_JGE: |
a72dafaf JW |
4636 | case BPF_JGT: |
4637 | { | |
4638 | u64 false_umax = opcode == BPF_JGT ? val : val - 1; | |
4639 | u64 true_umin = opcode == BPF_JGT ? val + 1 : val; | |
4640 | ||
092ed096 JW |
4641 | if (is_jmp32) { |
4642 | false_umax += gen_hi_max(false_reg->var_off); | |
4643 | true_umin += gen_hi_min(true_reg->var_off); | |
4644 | } | |
a72dafaf JW |
4645 | false_reg->umax_value = min(false_reg->umax_value, false_umax); |
4646 | true_reg->umin_value = max(true_reg->umin_value, true_umin); | |
b03c9f9f | 4647 | break; |
a72dafaf | 4648 | } |
48461135 | 4649 | case BPF_JSGE: |
a72dafaf JW |
4650 | case BPF_JSGT: |
4651 | { | |
4652 | s64 false_smax = opcode == BPF_JSGT ? sval : sval - 1; | |
4653 | s64 true_smin = opcode == BPF_JSGT ? sval + 1 : sval; | |
4654 | ||
092ed096 JW |
4655 | /* If the full s64 was not sign-extended from s32 then don't |
4656 | * deduct further info. | |
4657 | */ | |
4658 | if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg)) | |
4659 | break; | |
a72dafaf JW |
4660 | false_reg->smax_value = min(false_reg->smax_value, false_smax); |
4661 | true_reg->smin_value = max(true_reg->smin_value, true_smin); | |
48461135 | 4662 | break; |
a72dafaf | 4663 | } |
b4e432f1 | 4664 | case BPF_JLE: |
a72dafaf JW |
4665 | case BPF_JLT: |
4666 | { | |
4667 | u64 false_umin = opcode == BPF_JLT ? val : val + 1; | |
4668 | u64 true_umax = opcode == BPF_JLT ? val - 1 : val; | |
4669 | ||
092ed096 JW |
4670 | if (is_jmp32) { |
4671 | false_umin += gen_hi_min(false_reg->var_off); | |
4672 | true_umax += gen_hi_max(true_reg->var_off); | |
4673 | } | |
a72dafaf JW |
4674 | false_reg->umin_value = max(false_reg->umin_value, false_umin); |
4675 | true_reg->umax_value = min(true_reg->umax_value, true_umax); | |
b4e432f1 | 4676 | break; |
a72dafaf | 4677 | } |
b4e432f1 | 4678 | case BPF_JSLE: |
a72dafaf JW |
4679 | case BPF_JSLT: |
4680 | { | |
4681 | s64 false_smin = opcode == BPF_JSLT ? sval : sval + 1; | |
4682 | s64 true_smax = opcode == BPF_JSLT ? sval - 1 : sval; | |
4683 | ||
092ed096 JW |
4684 | if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg)) |
4685 | break; | |
a72dafaf JW |
4686 | false_reg->smin_value = max(false_reg->smin_value, false_smin); |
4687 | true_reg->smax_value = min(true_reg->smax_value, true_smax); | |
b4e432f1 | 4688 | break; |
a72dafaf | 4689 | } |
48461135 JB |
4690 | default: |
4691 | break; | |
4692 | } | |
4693 | ||
b03c9f9f EC |
4694 | __reg_deduce_bounds(false_reg); |
4695 | __reg_deduce_bounds(true_reg); | |
4696 | /* We might have learned some bits from the bounds. */ | |
4697 | __reg_bound_offset(false_reg); | |
4698 | __reg_bound_offset(true_reg); | |
4699 | /* Intersecting with the old var_off might have improved our bounds | |
4700 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), | |
4701 | * then new var_off is (0; 0x7f...fc) which improves our umax. | |
4702 | */ | |
4703 | __update_reg_bounds(false_reg); | |
4704 | __update_reg_bounds(true_reg); | |
48461135 JB |
4705 | } |
4706 | ||
f1174f77 EC |
4707 | /* Same as above, but for the case that dst_reg holds a constant and src_reg is |
4708 | * the variable reg. | |
48461135 JB |
4709 | */ |
4710 | static void reg_set_min_max_inv(struct bpf_reg_state *true_reg, | |
4711 | struct bpf_reg_state *false_reg, u64 val, | |
092ed096 | 4712 | u8 opcode, bool is_jmp32) |
48461135 | 4713 | { |
a72dafaf JW |
4714 | s64 sval; |
4715 | ||
f1174f77 EC |
4716 | if (__is_pointer_value(false, false_reg)) |
4717 | return; | |
4cabc5b1 | 4718 | |
092ed096 JW |
4719 | val = is_jmp32 ? (u32)val : val; |
4720 | sval = is_jmp32 ? (s64)(s32)val : (s64)val; | |
a72dafaf | 4721 | |
48461135 JB |
4722 | switch (opcode) { |
4723 | case BPF_JEQ: | |
48461135 | 4724 | case BPF_JNE: |
a72dafaf JW |
4725 | { |
4726 | struct bpf_reg_state *reg = | |
4727 | opcode == BPF_JEQ ? true_reg : false_reg; | |
4728 | ||
092ed096 JW |
4729 | if (is_jmp32) { |
4730 | u64 old_v = reg->var_off.value; | |
4731 | u64 hi_mask = ~0xffffffffULL; | |
4732 | ||
4733 | reg->var_off.value = (old_v & hi_mask) | val; | |
4734 | reg->var_off.mask &= hi_mask; | |
4735 | } else { | |
4736 | __mark_reg_known(reg, val); | |
4737 | } | |
48461135 | 4738 | break; |
a72dafaf | 4739 | } |
960ea056 JK |
4740 | case BPF_JSET: |
4741 | false_reg->var_off = tnum_and(false_reg->var_off, | |
4742 | tnum_const(~val)); | |
4743 | if (is_power_of_2(val)) | |
4744 | true_reg->var_off = tnum_or(true_reg->var_off, | |
4745 | tnum_const(val)); | |
4746 | break; | |
48461135 | 4747 | case BPF_JGE: |
a72dafaf JW |
4748 | case BPF_JGT: |
4749 | { | |
4750 | u64 false_umin = opcode == BPF_JGT ? val : val + 1; | |
4751 | u64 true_umax = opcode == BPF_JGT ? val - 1 : val; | |
4752 | ||
092ed096 JW |
4753 | if (is_jmp32) { |
4754 | false_umin += gen_hi_min(false_reg->var_off); | |
4755 | true_umax += gen_hi_max(true_reg->var_off); | |
4756 | } | |
a72dafaf JW |
4757 | false_reg->umin_value = max(false_reg->umin_value, false_umin); |
4758 | true_reg->umax_value = min(true_reg->umax_value, true_umax); | |
b03c9f9f | 4759 | break; |
a72dafaf | 4760 | } |
48461135 | 4761 | case BPF_JSGE: |
a72dafaf JW |
4762 | case BPF_JSGT: |
4763 | { | |
4764 | s64 false_smin = opcode == BPF_JSGT ? sval : sval + 1; | |
4765 | s64 true_smax = opcode == BPF_JSGT ? sval - 1 : sval; | |
4766 | ||
092ed096 JW |
4767 | if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg)) |
4768 | break; | |
a72dafaf JW |
4769 | false_reg->smin_value = max(false_reg->smin_value, false_smin); |
4770 | true_reg->smax_value = min(true_reg->smax_value, true_smax); | |
48461135 | 4771 | break; |
a72dafaf | 4772 | } |
b4e432f1 | 4773 | case BPF_JLE: |
a72dafaf JW |
4774 | case BPF_JLT: |
4775 | { | |
4776 | u64 false_umax = opcode == BPF_JLT ? val : val - 1; | |
4777 | u64 true_umin = opcode == BPF_JLT ? val + 1 : val; | |
4778 | ||
092ed096 JW |
4779 | if (is_jmp32) { |
4780 | false_umax += gen_hi_max(false_reg->var_off); | |
4781 | true_umin += gen_hi_min(true_reg->var_off); | |
4782 | } | |
a72dafaf JW |
4783 | false_reg->umax_value = min(false_reg->umax_value, false_umax); |
4784 | true_reg->umin_value = max(true_reg->umin_value, true_umin); | |
b4e432f1 | 4785 | break; |
a72dafaf | 4786 | } |
b4e432f1 | 4787 | case BPF_JSLE: |
a72dafaf JW |
4788 | case BPF_JSLT: |
4789 | { | |
4790 | s64 false_smax = opcode == BPF_JSLT ? sval : sval - 1; | |
4791 | s64 true_smin = opcode == BPF_JSLT ? sval + 1 : sval; | |
4792 | ||
092ed096 JW |
4793 | if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg)) |
4794 | break; | |
a72dafaf JW |
4795 | false_reg->smax_value = min(false_reg->smax_value, false_smax); |
4796 | true_reg->smin_value = max(true_reg->smin_value, true_smin); | |
b4e432f1 | 4797 | break; |
a72dafaf | 4798 | } |
48461135 JB |
4799 | default: |
4800 | break; | |
4801 | } | |
4802 | ||
b03c9f9f EC |
4803 | __reg_deduce_bounds(false_reg); |
4804 | __reg_deduce_bounds(true_reg); | |
4805 | /* We might have learned some bits from the bounds. */ | |
4806 | __reg_bound_offset(false_reg); | |
4807 | __reg_bound_offset(true_reg); | |
4808 | /* Intersecting with the old var_off might have improved our bounds | |
4809 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), | |
4810 | * then new var_off is (0; 0x7f...fc) which improves our umax. | |
4811 | */ | |
4812 | __update_reg_bounds(false_reg); | |
4813 | __update_reg_bounds(true_reg); | |
f1174f77 EC |
4814 | } |
4815 | ||
4816 | /* Regs are known to be equal, so intersect their min/max/var_off */ | |
4817 | static void __reg_combine_min_max(struct bpf_reg_state *src_reg, | |
4818 | struct bpf_reg_state *dst_reg) | |
4819 | { | |
b03c9f9f EC |
4820 | src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value, |
4821 | dst_reg->umin_value); | |
4822 | src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value, | |
4823 | dst_reg->umax_value); | |
4824 | src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value, | |
4825 | dst_reg->smin_value); | |
4826 | src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value, | |
4827 | dst_reg->smax_value); | |
f1174f77 EC |
4828 | src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off, |
4829 | dst_reg->var_off); | |
b03c9f9f EC |
4830 | /* We might have learned new bounds from the var_off. */ |
4831 | __update_reg_bounds(src_reg); | |
4832 | __update_reg_bounds(dst_reg); | |
4833 | /* We might have learned something about the sign bit. */ | |
4834 | __reg_deduce_bounds(src_reg); | |
4835 | __reg_deduce_bounds(dst_reg); | |
4836 | /* We might have learned some bits from the bounds. */ | |
4837 | __reg_bound_offset(src_reg); | |
4838 | __reg_bound_offset(dst_reg); | |
4839 | /* Intersecting with the old var_off might have improved our bounds | |
4840 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), | |
4841 | * then new var_off is (0; 0x7f...fc) which improves our umax. | |
4842 | */ | |
4843 | __update_reg_bounds(src_reg); | |
4844 | __update_reg_bounds(dst_reg); | |
f1174f77 EC |
4845 | } |
4846 | ||
4847 | static void reg_combine_min_max(struct bpf_reg_state *true_src, | |
4848 | struct bpf_reg_state *true_dst, | |
4849 | struct bpf_reg_state *false_src, | |
4850 | struct bpf_reg_state *false_dst, | |
4851 | u8 opcode) | |
4852 | { | |
4853 | switch (opcode) { | |
4854 | case BPF_JEQ: | |
4855 | __reg_combine_min_max(true_src, true_dst); | |
4856 | break; | |
4857 | case BPF_JNE: | |
4858 | __reg_combine_min_max(false_src, false_dst); | |
b03c9f9f | 4859 | break; |
4cabc5b1 | 4860 | } |
48461135 JB |
4861 | } |
4862 | ||
fd978bf7 JS |
4863 | static void mark_ptr_or_null_reg(struct bpf_func_state *state, |
4864 | struct bpf_reg_state *reg, u32 id, | |
840b9615 | 4865 | bool is_null) |
57a09bf0 | 4866 | { |
840b9615 | 4867 | if (reg_type_may_be_null(reg->type) && reg->id == id) { |
f1174f77 EC |
4868 | /* Old offset (both fixed and variable parts) should |
4869 | * have been known-zero, because we don't allow pointer | |
4870 | * arithmetic on pointers that might be NULL. | |
4871 | */ | |
b03c9f9f EC |
4872 | if (WARN_ON_ONCE(reg->smin_value || reg->smax_value || |
4873 | !tnum_equals_const(reg->var_off, 0) || | |
f1174f77 | 4874 | reg->off)) { |
b03c9f9f EC |
4875 | __mark_reg_known_zero(reg); |
4876 | reg->off = 0; | |
f1174f77 EC |
4877 | } |
4878 | if (is_null) { | |
4879 | reg->type = SCALAR_VALUE; | |
840b9615 JS |
4880 | } else if (reg->type == PTR_TO_MAP_VALUE_OR_NULL) { |
4881 | if (reg->map_ptr->inner_map_meta) { | |
4882 | reg->type = CONST_PTR_TO_MAP; | |
4883 | reg->map_ptr = reg->map_ptr->inner_map_meta; | |
4884 | } else { | |
4885 | reg->type = PTR_TO_MAP_VALUE; | |
4886 | } | |
c64b7983 JS |
4887 | } else if (reg->type == PTR_TO_SOCKET_OR_NULL) { |
4888 | reg->type = PTR_TO_SOCKET; | |
46f8bc92 MKL |
4889 | } else if (reg->type == PTR_TO_SOCK_COMMON_OR_NULL) { |
4890 | reg->type = PTR_TO_SOCK_COMMON; | |
655a51e5 MKL |
4891 | } else if (reg->type == PTR_TO_TCP_SOCK_OR_NULL) { |
4892 | reg->type = PTR_TO_TCP_SOCK; | |
56f668df | 4893 | } |
1b986589 MKL |
4894 | if (is_null) { |
4895 | /* We don't need id and ref_obj_id from this point | |
4896 | * onwards anymore, thus we should better reset it, | |
4897 | * so that state pruning has chances to take effect. | |
4898 | */ | |
4899 | reg->id = 0; | |
4900 | reg->ref_obj_id = 0; | |
4901 | } else if (!reg_may_point_to_spin_lock(reg)) { | |
4902 | /* For not-NULL ptr, reg->ref_obj_id will be reset | |
4903 | * in release_reg_references(). | |
4904 | * | |
4905 | * reg->id is still used by spin_lock ptr. Other | |
4906 | * than spin_lock ptr type, reg->id can be reset. | |
fd978bf7 JS |
4907 | */ |
4908 | reg->id = 0; | |
56f668df | 4909 | } |
57a09bf0 TG |
4910 | } |
4911 | } | |
4912 | ||
c6a9efa1 PC |
4913 | static void __mark_ptr_or_null_regs(struct bpf_func_state *state, u32 id, |
4914 | bool is_null) | |
4915 | { | |
4916 | struct bpf_reg_state *reg; | |
4917 | int i; | |
4918 | ||
4919 | for (i = 0; i < MAX_BPF_REG; i++) | |
4920 | mark_ptr_or_null_reg(state, &state->regs[i], id, is_null); | |
4921 | ||
4922 | bpf_for_each_spilled_reg(i, state, reg) { | |
4923 | if (!reg) | |
4924 | continue; | |
4925 | mark_ptr_or_null_reg(state, reg, id, is_null); | |
4926 | } | |
4927 | } | |
4928 | ||
57a09bf0 TG |
4929 | /* The logic is similar to find_good_pkt_pointers(), both could eventually |
4930 | * be folded together at some point. | |
4931 | */ | |
840b9615 JS |
4932 | static void mark_ptr_or_null_regs(struct bpf_verifier_state *vstate, u32 regno, |
4933 | bool is_null) | |
57a09bf0 | 4934 | { |
f4d7e40a | 4935 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
c6a9efa1 | 4936 | struct bpf_reg_state *regs = state->regs; |
1b986589 | 4937 | u32 ref_obj_id = regs[regno].ref_obj_id; |
a08dd0da | 4938 | u32 id = regs[regno].id; |
c6a9efa1 | 4939 | int i; |
57a09bf0 | 4940 | |
1b986589 MKL |
4941 | if (ref_obj_id && ref_obj_id == id && is_null) |
4942 | /* regs[regno] is in the " == NULL" branch. | |
4943 | * No one could have freed the reference state before | |
4944 | * doing the NULL check. | |
4945 | */ | |
4946 | WARN_ON_ONCE(release_reference_state(state, id)); | |
fd978bf7 | 4947 | |
c6a9efa1 PC |
4948 | for (i = 0; i <= vstate->curframe; i++) |
4949 | __mark_ptr_or_null_regs(vstate->frame[i], id, is_null); | |
57a09bf0 TG |
4950 | } |
4951 | ||
5beca081 DB |
4952 | static bool try_match_pkt_pointers(const struct bpf_insn *insn, |
4953 | struct bpf_reg_state *dst_reg, | |
4954 | struct bpf_reg_state *src_reg, | |
4955 | struct bpf_verifier_state *this_branch, | |
4956 | struct bpf_verifier_state *other_branch) | |
4957 | { | |
4958 | if (BPF_SRC(insn->code) != BPF_X) | |
4959 | return false; | |
4960 | ||
092ed096 JW |
4961 | /* Pointers are always 64-bit. */ |
4962 | if (BPF_CLASS(insn->code) == BPF_JMP32) | |
4963 | return false; | |
4964 | ||
5beca081 DB |
4965 | switch (BPF_OP(insn->code)) { |
4966 | case BPF_JGT: | |
4967 | if ((dst_reg->type == PTR_TO_PACKET && | |
4968 | src_reg->type == PTR_TO_PACKET_END) || | |
4969 | (dst_reg->type == PTR_TO_PACKET_META && | |
4970 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
4971 | /* pkt_data' > pkt_end, pkt_meta' > pkt_data */ | |
4972 | find_good_pkt_pointers(this_branch, dst_reg, | |
4973 | dst_reg->type, false); | |
4974 | } else if ((dst_reg->type == PTR_TO_PACKET_END && | |
4975 | src_reg->type == PTR_TO_PACKET) || | |
4976 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
4977 | src_reg->type == PTR_TO_PACKET_META)) { | |
4978 | /* pkt_end > pkt_data', pkt_data > pkt_meta' */ | |
4979 | find_good_pkt_pointers(other_branch, src_reg, | |
4980 | src_reg->type, true); | |
4981 | } else { | |
4982 | return false; | |
4983 | } | |
4984 | break; | |
4985 | case BPF_JLT: | |
4986 | if ((dst_reg->type == PTR_TO_PACKET && | |
4987 | src_reg->type == PTR_TO_PACKET_END) || | |
4988 | (dst_reg->type == PTR_TO_PACKET_META && | |
4989 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
4990 | /* pkt_data' < pkt_end, pkt_meta' < pkt_data */ | |
4991 | find_good_pkt_pointers(other_branch, dst_reg, | |
4992 | dst_reg->type, true); | |
4993 | } else if ((dst_reg->type == PTR_TO_PACKET_END && | |
4994 | src_reg->type == PTR_TO_PACKET) || | |
4995 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
4996 | src_reg->type == PTR_TO_PACKET_META)) { | |
4997 | /* pkt_end < pkt_data', pkt_data > pkt_meta' */ | |
4998 | find_good_pkt_pointers(this_branch, src_reg, | |
4999 | src_reg->type, false); | |
5000 | } else { | |
5001 | return false; | |
5002 | } | |
5003 | break; | |
5004 | case BPF_JGE: | |
5005 | if ((dst_reg->type == PTR_TO_PACKET && | |
5006 | src_reg->type == PTR_TO_PACKET_END) || | |
5007 | (dst_reg->type == PTR_TO_PACKET_META && | |
5008 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
5009 | /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */ | |
5010 | find_good_pkt_pointers(this_branch, dst_reg, | |
5011 | dst_reg->type, true); | |
5012 | } else if ((dst_reg->type == PTR_TO_PACKET_END && | |
5013 | src_reg->type == PTR_TO_PACKET) || | |
5014 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
5015 | src_reg->type == PTR_TO_PACKET_META)) { | |
5016 | /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */ | |
5017 | find_good_pkt_pointers(other_branch, src_reg, | |
5018 | src_reg->type, false); | |
5019 | } else { | |
5020 | return false; | |
5021 | } | |
5022 | break; | |
5023 | case BPF_JLE: | |
5024 | if ((dst_reg->type == PTR_TO_PACKET && | |
5025 | src_reg->type == PTR_TO_PACKET_END) || | |
5026 | (dst_reg->type == PTR_TO_PACKET_META && | |
5027 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
5028 | /* pkt_data' <= pkt_end, pkt_meta' <= pkt_data */ | |
5029 | find_good_pkt_pointers(other_branch, dst_reg, | |
5030 | dst_reg->type, false); | |
5031 | } else if ((dst_reg->type == PTR_TO_PACKET_END && | |
5032 | src_reg->type == PTR_TO_PACKET) || | |
5033 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
5034 | src_reg->type == PTR_TO_PACKET_META)) { | |
5035 | /* pkt_end <= pkt_data', pkt_data <= pkt_meta' */ | |
5036 | find_good_pkt_pointers(this_branch, src_reg, | |
5037 | src_reg->type, true); | |
5038 | } else { | |
5039 | return false; | |
5040 | } | |
5041 | break; | |
5042 | default: | |
5043 | return false; | |
5044 | } | |
5045 | ||
5046 | return true; | |
5047 | } | |
5048 | ||
58e2af8b | 5049 | static int check_cond_jmp_op(struct bpf_verifier_env *env, |
17a52670 AS |
5050 | struct bpf_insn *insn, int *insn_idx) |
5051 | { | |
f4d7e40a AS |
5052 | struct bpf_verifier_state *this_branch = env->cur_state; |
5053 | struct bpf_verifier_state *other_branch; | |
5054 | struct bpf_reg_state *regs = this_branch->frame[this_branch->curframe]->regs; | |
5055 | struct bpf_reg_state *dst_reg, *other_branch_regs; | |
17a52670 | 5056 | u8 opcode = BPF_OP(insn->code); |
092ed096 | 5057 | bool is_jmp32; |
17a52670 AS |
5058 | int err; |
5059 | ||
092ed096 JW |
5060 | /* Only conditional jumps are expected to reach here. */ |
5061 | if (opcode == BPF_JA || opcode > BPF_JSLE) { | |
5062 | verbose(env, "invalid BPF_JMP/JMP32 opcode %x\n", opcode); | |
17a52670 AS |
5063 | return -EINVAL; |
5064 | } | |
5065 | ||
5066 | if (BPF_SRC(insn->code) == BPF_X) { | |
5067 | if (insn->imm != 0) { | |
092ed096 | 5068 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
17a52670 AS |
5069 | return -EINVAL; |
5070 | } | |
5071 | ||
5072 | /* check src1 operand */ | |
dc503a8a | 5073 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
5074 | if (err) |
5075 | return err; | |
1be7f75d AS |
5076 | |
5077 | if (is_pointer_value(env, insn->src_reg)) { | |
61bd5218 | 5078 | verbose(env, "R%d pointer comparison prohibited\n", |
1be7f75d AS |
5079 | insn->src_reg); |
5080 | return -EACCES; | |
5081 | } | |
17a52670 AS |
5082 | } else { |
5083 | if (insn->src_reg != BPF_REG_0) { | |
092ed096 | 5084 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
17a52670 AS |
5085 | return -EINVAL; |
5086 | } | |
5087 | } | |
5088 | ||
5089 | /* check src2 operand */ | |
dc503a8a | 5090 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
5091 | if (err) |
5092 | return err; | |
5093 | ||
1a0dc1ac | 5094 | dst_reg = ®s[insn->dst_reg]; |
092ed096 | 5095 | is_jmp32 = BPF_CLASS(insn->code) == BPF_JMP32; |
1a0dc1ac | 5096 | |
4f7b3e82 | 5097 | if (BPF_SRC(insn->code) == BPF_K) { |
092ed096 JW |
5098 | int pred = is_branch_taken(dst_reg, insn->imm, opcode, |
5099 | is_jmp32); | |
4f7b3e82 AS |
5100 | |
5101 | if (pred == 1) { | |
5102 | /* only follow the goto, ignore fall-through */ | |
17a52670 AS |
5103 | *insn_idx += insn->off; |
5104 | return 0; | |
4f7b3e82 AS |
5105 | } else if (pred == 0) { |
5106 | /* only follow fall-through branch, since | |
17a52670 AS |
5107 | * that's where the program will go |
5108 | */ | |
5109 | return 0; | |
5110 | } | |
5111 | } | |
5112 | ||
979d63d5 DB |
5113 | other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx, |
5114 | false); | |
17a52670 AS |
5115 | if (!other_branch) |
5116 | return -EFAULT; | |
f4d7e40a | 5117 | other_branch_regs = other_branch->frame[other_branch->curframe]->regs; |
17a52670 | 5118 | |
48461135 JB |
5119 | /* detect if we are comparing against a constant value so we can adjust |
5120 | * our min/max values for our dst register. | |
f1174f77 EC |
5121 | * this is only legit if both are scalars (or pointers to the same |
5122 | * object, I suppose, but we don't support that right now), because | |
5123 | * otherwise the different base pointers mean the offsets aren't | |
5124 | * comparable. | |
48461135 JB |
5125 | */ |
5126 | if (BPF_SRC(insn->code) == BPF_X) { | |
092ed096 JW |
5127 | struct bpf_reg_state *src_reg = ®s[insn->src_reg]; |
5128 | struct bpf_reg_state lo_reg0 = *dst_reg; | |
5129 | struct bpf_reg_state lo_reg1 = *src_reg; | |
5130 | struct bpf_reg_state *src_lo, *dst_lo; | |
5131 | ||
5132 | dst_lo = &lo_reg0; | |
5133 | src_lo = &lo_reg1; | |
5134 | coerce_reg_to_size(dst_lo, 4); | |
5135 | coerce_reg_to_size(src_lo, 4); | |
5136 | ||
f1174f77 | 5137 | if (dst_reg->type == SCALAR_VALUE && |
092ed096 JW |
5138 | src_reg->type == SCALAR_VALUE) { |
5139 | if (tnum_is_const(src_reg->var_off) || | |
5140 | (is_jmp32 && tnum_is_const(src_lo->var_off))) | |
f4d7e40a | 5141 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
092ed096 JW |
5142 | dst_reg, |
5143 | is_jmp32 | |
5144 | ? src_lo->var_off.value | |
5145 | : src_reg->var_off.value, | |
5146 | opcode, is_jmp32); | |
5147 | else if (tnum_is_const(dst_reg->var_off) || | |
5148 | (is_jmp32 && tnum_is_const(dst_lo->var_off))) | |
f4d7e40a | 5149 | reg_set_min_max_inv(&other_branch_regs[insn->src_reg], |
092ed096 JW |
5150 | src_reg, |
5151 | is_jmp32 | |
5152 | ? dst_lo->var_off.value | |
5153 | : dst_reg->var_off.value, | |
5154 | opcode, is_jmp32); | |
5155 | else if (!is_jmp32 && | |
5156 | (opcode == BPF_JEQ || opcode == BPF_JNE)) | |
f1174f77 | 5157 | /* Comparing for equality, we can combine knowledge */ |
f4d7e40a AS |
5158 | reg_combine_min_max(&other_branch_regs[insn->src_reg], |
5159 | &other_branch_regs[insn->dst_reg], | |
092ed096 | 5160 | src_reg, dst_reg, opcode); |
f1174f77 EC |
5161 | } |
5162 | } else if (dst_reg->type == SCALAR_VALUE) { | |
f4d7e40a | 5163 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
092ed096 | 5164 | dst_reg, insn->imm, opcode, is_jmp32); |
48461135 JB |
5165 | } |
5166 | ||
092ed096 JW |
5167 | /* detect if R == 0 where R is returned from bpf_map_lookup_elem(). |
5168 | * NOTE: these optimizations below are related with pointer comparison | |
5169 | * which will never be JMP32. | |
5170 | */ | |
5171 | if (!is_jmp32 && BPF_SRC(insn->code) == BPF_K && | |
1a0dc1ac | 5172 | insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) && |
840b9615 JS |
5173 | reg_type_may_be_null(dst_reg->type)) { |
5174 | /* Mark all identical registers in each branch as either | |
57a09bf0 TG |
5175 | * safe or unknown depending R == 0 or R != 0 conditional. |
5176 | */ | |
840b9615 JS |
5177 | mark_ptr_or_null_regs(this_branch, insn->dst_reg, |
5178 | opcode == BPF_JNE); | |
5179 | mark_ptr_or_null_regs(other_branch, insn->dst_reg, | |
5180 | opcode == BPF_JEQ); | |
5beca081 DB |
5181 | } else if (!try_match_pkt_pointers(insn, dst_reg, ®s[insn->src_reg], |
5182 | this_branch, other_branch) && | |
5183 | is_pointer_value(env, insn->dst_reg)) { | |
61bd5218 JK |
5184 | verbose(env, "R%d pointer comparison prohibited\n", |
5185 | insn->dst_reg); | |
1be7f75d | 5186 | return -EACCES; |
17a52670 | 5187 | } |
06ee7115 | 5188 | if (env->log.level & BPF_LOG_LEVEL) |
f4d7e40a | 5189 | print_verifier_state(env, this_branch->frame[this_branch->curframe]); |
17a52670 AS |
5190 | return 0; |
5191 | } | |
5192 | ||
17a52670 | 5193 | /* verify BPF_LD_IMM64 instruction */ |
58e2af8b | 5194 | static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn) |
17a52670 | 5195 | { |
d8eca5bb | 5196 | struct bpf_insn_aux_data *aux = cur_aux(env); |
638f5b90 | 5197 | struct bpf_reg_state *regs = cur_regs(env); |
d8eca5bb | 5198 | struct bpf_map *map; |
17a52670 AS |
5199 | int err; |
5200 | ||
5201 | if (BPF_SIZE(insn->code) != BPF_DW) { | |
61bd5218 | 5202 | verbose(env, "invalid BPF_LD_IMM insn\n"); |
17a52670 AS |
5203 | return -EINVAL; |
5204 | } | |
5205 | if (insn->off != 0) { | |
61bd5218 | 5206 | verbose(env, "BPF_LD_IMM64 uses reserved fields\n"); |
17a52670 AS |
5207 | return -EINVAL; |
5208 | } | |
5209 | ||
dc503a8a | 5210 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
17a52670 AS |
5211 | if (err) |
5212 | return err; | |
5213 | ||
6b173873 | 5214 | if (insn->src_reg == 0) { |
6b173873 JK |
5215 | u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm; |
5216 | ||
f1174f77 | 5217 | regs[insn->dst_reg].type = SCALAR_VALUE; |
b03c9f9f | 5218 | __mark_reg_known(®s[insn->dst_reg], imm); |
17a52670 | 5219 | return 0; |
6b173873 | 5220 | } |
17a52670 | 5221 | |
d8eca5bb DB |
5222 | map = env->used_maps[aux->map_index]; |
5223 | mark_reg_known_zero(env, regs, insn->dst_reg); | |
5224 | regs[insn->dst_reg].map_ptr = map; | |
5225 | ||
5226 | if (insn->src_reg == BPF_PSEUDO_MAP_VALUE) { | |
5227 | regs[insn->dst_reg].type = PTR_TO_MAP_VALUE; | |
5228 | regs[insn->dst_reg].off = aux->map_off; | |
5229 | if (map_value_has_spin_lock(map)) | |
5230 | regs[insn->dst_reg].id = ++env->id_gen; | |
5231 | } else if (insn->src_reg == BPF_PSEUDO_MAP_FD) { | |
5232 | regs[insn->dst_reg].type = CONST_PTR_TO_MAP; | |
5233 | } else { | |
5234 | verbose(env, "bpf verifier is misconfigured\n"); | |
5235 | return -EINVAL; | |
5236 | } | |
17a52670 | 5237 | |
17a52670 AS |
5238 | return 0; |
5239 | } | |
5240 | ||
96be4325 DB |
5241 | static bool may_access_skb(enum bpf_prog_type type) |
5242 | { | |
5243 | switch (type) { | |
5244 | case BPF_PROG_TYPE_SOCKET_FILTER: | |
5245 | case BPF_PROG_TYPE_SCHED_CLS: | |
94caee8c | 5246 | case BPF_PROG_TYPE_SCHED_ACT: |
96be4325 DB |
5247 | return true; |
5248 | default: | |
5249 | return false; | |
5250 | } | |
5251 | } | |
5252 | ||
ddd872bc AS |
5253 | /* verify safety of LD_ABS|LD_IND instructions: |
5254 | * - they can only appear in the programs where ctx == skb | |
5255 | * - since they are wrappers of function calls, they scratch R1-R5 registers, | |
5256 | * preserve R6-R9, and store return value into R0 | |
5257 | * | |
5258 | * Implicit input: | |
5259 | * ctx == skb == R6 == CTX | |
5260 | * | |
5261 | * Explicit input: | |
5262 | * SRC == any register | |
5263 | * IMM == 32-bit immediate | |
5264 | * | |
5265 | * Output: | |
5266 | * R0 - 8/16/32-bit skb data converted to cpu endianness | |
5267 | */ | |
58e2af8b | 5268 | static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn) |
ddd872bc | 5269 | { |
638f5b90 | 5270 | struct bpf_reg_state *regs = cur_regs(env); |
ddd872bc | 5271 | u8 mode = BPF_MODE(insn->code); |
ddd872bc AS |
5272 | int i, err; |
5273 | ||
24701ece | 5274 | if (!may_access_skb(env->prog->type)) { |
61bd5218 | 5275 | verbose(env, "BPF_LD_[ABS|IND] instructions not allowed for this program type\n"); |
ddd872bc AS |
5276 | return -EINVAL; |
5277 | } | |
5278 | ||
e0cea7ce DB |
5279 | if (!env->ops->gen_ld_abs) { |
5280 | verbose(env, "bpf verifier is misconfigured\n"); | |
5281 | return -EINVAL; | |
5282 | } | |
5283 | ||
f910cefa | 5284 | if (env->subprog_cnt > 1) { |
f4d7e40a AS |
5285 | /* when program has LD_ABS insn JITs and interpreter assume |
5286 | * that r1 == ctx == skb which is not the case for callees | |
5287 | * that can have arbitrary arguments. It's problematic | |
5288 | * for main prog as well since JITs would need to analyze | |
5289 | * all functions in order to make proper register save/restore | |
5290 | * decisions in the main prog. Hence disallow LD_ABS with calls | |
5291 | */ | |
5292 | verbose(env, "BPF_LD_[ABS|IND] instructions cannot be mixed with bpf-to-bpf calls\n"); | |
5293 | return -EINVAL; | |
5294 | } | |
5295 | ||
ddd872bc | 5296 | if (insn->dst_reg != BPF_REG_0 || insn->off != 0 || |
d82bccc6 | 5297 | BPF_SIZE(insn->code) == BPF_DW || |
ddd872bc | 5298 | (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) { |
61bd5218 | 5299 | verbose(env, "BPF_LD_[ABS|IND] uses reserved fields\n"); |
ddd872bc AS |
5300 | return -EINVAL; |
5301 | } | |
5302 | ||
5303 | /* check whether implicit source operand (register R6) is readable */ | |
dc503a8a | 5304 | err = check_reg_arg(env, BPF_REG_6, SRC_OP); |
ddd872bc AS |
5305 | if (err) |
5306 | return err; | |
5307 | ||
fd978bf7 JS |
5308 | /* Disallow usage of BPF_LD_[ABS|IND] with reference tracking, as |
5309 | * gen_ld_abs() may terminate the program at runtime, leading to | |
5310 | * reference leak. | |
5311 | */ | |
5312 | err = check_reference_leak(env); | |
5313 | if (err) { | |
5314 | verbose(env, "BPF_LD_[ABS|IND] cannot be mixed with socket references\n"); | |
5315 | return err; | |
5316 | } | |
5317 | ||
d83525ca AS |
5318 | if (env->cur_state->active_spin_lock) { |
5319 | verbose(env, "BPF_LD_[ABS|IND] cannot be used inside bpf_spin_lock-ed region\n"); | |
5320 | return -EINVAL; | |
5321 | } | |
5322 | ||
ddd872bc | 5323 | if (regs[BPF_REG_6].type != PTR_TO_CTX) { |
61bd5218 JK |
5324 | verbose(env, |
5325 | "at the time of BPF_LD_ABS|IND R6 != pointer to skb\n"); | |
ddd872bc AS |
5326 | return -EINVAL; |
5327 | } | |
5328 | ||
5329 | if (mode == BPF_IND) { | |
5330 | /* check explicit source operand */ | |
dc503a8a | 5331 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
ddd872bc AS |
5332 | if (err) |
5333 | return err; | |
5334 | } | |
5335 | ||
5336 | /* reset caller saved regs to unreadable */ | |
dc503a8a | 5337 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
61bd5218 | 5338 | mark_reg_not_init(env, regs, caller_saved[i]); |
dc503a8a EC |
5339 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
5340 | } | |
ddd872bc AS |
5341 | |
5342 | /* mark destination R0 register as readable, since it contains | |
dc503a8a EC |
5343 | * the value fetched from the packet. |
5344 | * Already marked as written above. | |
ddd872bc | 5345 | */ |
61bd5218 | 5346 | mark_reg_unknown(env, regs, BPF_REG_0); |
ddd872bc AS |
5347 | return 0; |
5348 | } | |
5349 | ||
390ee7e2 AS |
5350 | static int check_return_code(struct bpf_verifier_env *env) |
5351 | { | |
5352 | struct bpf_reg_state *reg; | |
5353 | struct tnum range = tnum_range(0, 1); | |
5354 | ||
5355 | switch (env->prog->type) { | |
5356 | case BPF_PROG_TYPE_CGROUP_SKB: | |
5357 | case BPF_PROG_TYPE_CGROUP_SOCK: | |
4fbac77d | 5358 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
390ee7e2 | 5359 | case BPF_PROG_TYPE_SOCK_OPS: |
ebc614f6 | 5360 | case BPF_PROG_TYPE_CGROUP_DEVICE: |
7b146ceb | 5361 | case BPF_PROG_TYPE_CGROUP_SYSCTL: |
390ee7e2 AS |
5362 | break; |
5363 | default: | |
5364 | return 0; | |
5365 | } | |
5366 | ||
638f5b90 | 5367 | reg = cur_regs(env) + BPF_REG_0; |
390ee7e2 | 5368 | if (reg->type != SCALAR_VALUE) { |
61bd5218 | 5369 | verbose(env, "At program exit the register R0 is not a known value (%s)\n", |
390ee7e2 AS |
5370 | reg_type_str[reg->type]); |
5371 | return -EINVAL; | |
5372 | } | |
5373 | ||
5374 | if (!tnum_in(range, reg->var_off)) { | |
61bd5218 | 5375 | verbose(env, "At program exit the register R0 "); |
390ee7e2 AS |
5376 | if (!tnum_is_unknown(reg->var_off)) { |
5377 | char tn_buf[48]; | |
5378 | ||
5379 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
61bd5218 | 5380 | verbose(env, "has value %s", tn_buf); |
390ee7e2 | 5381 | } else { |
61bd5218 | 5382 | verbose(env, "has unknown scalar value"); |
390ee7e2 | 5383 | } |
61bd5218 | 5384 | verbose(env, " should have been 0 or 1\n"); |
390ee7e2 AS |
5385 | return -EINVAL; |
5386 | } | |
5387 | return 0; | |
5388 | } | |
5389 | ||
475fb78f AS |
5390 | /* non-recursive DFS pseudo code |
5391 | * 1 procedure DFS-iterative(G,v): | |
5392 | * 2 label v as discovered | |
5393 | * 3 let S be a stack | |
5394 | * 4 S.push(v) | |
5395 | * 5 while S is not empty | |
5396 | * 6 t <- S.pop() | |
5397 | * 7 if t is what we're looking for: | |
5398 | * 8 return t | |
5399 | * 9 for all edges e in G.adjacentEdges(t) do | |
5400 | * 10 if edge e is already labelled | |
5401 | * 11 continue with the next edge | |
5402 | * 12 w <- G.adjacentVertex(t,e) | |
5403 | * 13 if vertex w is not discovered and not explored | |
5404 | * 14 label e as tree-edge | |
5405 | * 15 label w as discovered | |
5406 | * 16 S.push(w) | |
5407 | * 17 continue at 5 | |
5408 | * 18 else if vertex w is discovered | |
5409 | * 19 label e as back-edge | |
5410 | * 20 else | |
5411 | * 21 // vertex w is explored | |
5412 | * 22 label e as forward- or cross-edge | |
5413 | * 23 label t as explored | |
5414 | * 24 S.pop() | |
5415 | * | |
5416 | * convention: | |
5417 | * 0x10 - discovered | |
5418 | * 0x11 - discovered and fall-through edge labelled | |
5419 | * 0x12 - discovered and fall-through and branch edges labelled | |
5420 | * 0x20 - explored | |
5421 | */ | |
5422 | ||
5423 | enum { | |
5424 | DISCOVERED = 0x10, | |
5425 | EXPLORED = 0x20, | |
5426 | FALLTHROUGH = 1, | |
5427 | BRANCH = 2, | |
5428 | }; | |
5429 | ||
58e2af8b | 5430 | #define STATE_LIST_MARK ((struct bpf_verifier_state_list *) -1L) |
f1bca824 | 5431 | |
475fb78f AS |
5432 | /* t, w, e - match pseudo-code above: |
5433 | * t - index of current instruction | |
5434 | * w - next instruction | |
5435 | * e - edge | |
5436 | */ | |
58e2af8b | 5437 | static int push_insn(int t, int w, int e, struct bpf_verifier_env *env) |
475fb78f | 5438 | { |
7df737e9 AS |
5439 | int *insn_stack = env->cfg.insn_stack; |
5440 | int *insn_state = env->cfg.insn_state; | |
5441 | ||
475fb78f AS |
5442 | if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH)) |
5443 | return 0; | |
5444 | ||
5445 | if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH)) | |
5446 | return 0; | |
5447 | ||
5448 | if (w < 0 || w >= env->prog->len) { | |
d9762e84 | 5449 | verbose_linfo(env, t, "%d: ", t); |
61bd5218 | 5450 | verbose(env, "jump out of range from insn %d to %d\n", t, w); |
475fb78f AS |
5451 | return -EINVAL; |
5452 | } | |
5453 | ||
f1bca824 AS |
5454 | if (e == BRANCH) |
5455 | /* mark branch target for state pruning */ | |
5456 | env->explored_states[w] = STATE_LIST_MARK; | |
5457 | ||
475fb78f AS |
5458 | if (insn_state[w] == 0) { |
5459 | /* tree-edge */ | |
5460 | insn_state[t] = DISCOVERED | e; | |
5461 | insn_state[w] = DISCOVERED; | |
7df737e9 | 5462 | if (env->cfg.cur_stack >= env->prog->len) |
475fb78f | 5463 | return -E2BIG; |
7df737e9 | 5464 | insn_stack[env->cfg.cur_stack++] = w; |
475fb78f AS |
5465 | return 1; |
5466 | } else if ((insn_state[w] & 0xF0) == DISCOVERED) { | |
d9762e84 MKL |
5467 | verbose_linfo(env, t, "%d: ", t); |
5468 | verbose_linfo(env, w, "%d: ", w); | |
61bd5218 | 5469 | verbose(env, "back-edge from insn %d to %d\n", t, w); |
475fb78f AS |
5470 | return -EINVAL; |
5471 | } else if (insn_state[w] == EXPLORED) { | |
5472 | /* forward- or cross-edge */ | |
5473 | insn_state[t] = DISCOVERED | e; | |
5474 | } else { | |
61bd5218 | 5475 | verbose(env, "insn state internal bug\n"); |
475fb78f AS |
5476 | return -EFAULT; |
5477 | } | |
5478 | return 0; | |
5479 | } | |
5480 | ||
5481 | /* non-recursive depth-first-search to detect loops in BPF program | |
5482 | * loop == back-edge in directed graph | |
5483 | */ | |
58e2af8b | 5484 | static int check_cfg(struct bpf_verifier_env *env) |
475fb78f AS |
5485 | { |
5486 | struct bpf_insn *insns = env->prog->insnsi; | |
5487 | int insn_cnt = env->prog->len; | |
7df737e9 | 5488 | int *insn_stack, *insn_state; |
475fb78f AS |
5489 | int ret = 0; |
5490 | int i, t; | |
5491 | ||
7df737e9 | 5492 | insn_state = env->cfg.insn_state = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
475fb78f AS |
5493 | if (!insn_state) |
5494 | return -ENOMEM; | |
5495 | ||
7df737e9 | 5496 | insn_stack = env->cfg.insn_stack = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
475fb78f | 5497 | if (!insn_stack) { |
71dde681 | 5498 | kvfree(insn_state); |
475fb78f AS |
5499 | return -ENOMEM; |
5500 | } | |
5501 | ||
5502 | insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */ | |
5503 | insn_stack[0] = 0; /* 0 is the first instruction */ | |
7df737e9 | 5504 | env->cfg.cur_stack = 1; |
475fb78f AS |
5505 | |
5506 | peek_stack: | |
7df737e9 | 5507 | if (env->cfg.cur_stack == 0) |
475fb78f | 5508 | goto check_state; |
7df737e9 | 5509 | t = insn_stack[env->cfg.cur_stack - 1]; |
475fb78f | 5510 | |
092ed096 JW |
5511 | if (BPF_CLASS(insns[t].code) == BPF_JMP || |
5512 | BPF_CLASS(insns[t].code) == BPF_JMP32) { | |
475fb78f AS |
5513 | u8 opcode = BPF_OP(insns[t].code); |
5514 | ||
5515 | if (opcode == BPF_EXIT) { | |
5516 | goto mark_explored; | |
5517 | } else if (opcode == BPF_CALL) { | |
5518 | ret = push_insn(t, t + 1, FALLTHROUGH, env); | |
5519 | if (ret == 1) | |
5520 | goto peek_stack; | |
5521 | else if (ret < 0) | |
5522 | goto err_free; | |
07016151 DB |
5523 | if (t + 1 < insn_cnt) |
5524 | env->explored_states[t + 1] = STATE_LIST_MARK; | |
cc8b0b92 AS |
5525 | if (insns[t].src_reg == BPF_PSEUDO_CALL) { |
5526 | env->explored_states[t] = STATE_LIST_MARK; | |
5527 | ret = push_insn(t, t + insns[t].imm + 1, BRANCH, env); | |
5528 | if (ret == 1) | |
5529 | goto peek_stack; | |
5530 | else if (ret < 0) | |
5531 | goto err_free; | |
5532 | } | |
475fb78f AS |
5533 | } else if (opcode == BPF_JA) { |
5534 | if (BPF_SRC(insns[t].code) != BPF_K) { | |
5535 | ret = -EINVAL; | |
5536 | goto err_free; | |
5537 | } | |
5538 | /* unconditional jump with single edge */ | |
5539 | ret = push_insn(t, t + insns[t].off + 1, | |
5540 | FALLTHROUGH, env); | |
5541 | if (ret == 1) | |
5542 | goto peek_stack; | |
5543 | else if (ret < 0) | |
5544 | goto err_free; | |
f1bca824 AS |
5545 | /* tell verifier to check for equivalent states |
5546 | * after every call and jump | |
5547 | */ | |
c3de6317 AS |
5548 | if (t + 1 < insn_cnt) |
5549 | env->explored_states[t + 1] = STATE_LIST_MARK; | |
475fb78f AS |
5550 | } else { |
5551 | /* conditional jump with two edges */ | |
3c2ce60b | 5552 | env->explored_states[t] = STATE_LIST_MARK; |
475fb78f AS |
5553 | ret = push_insn(t, t + 1, FALLTHROUGH, env); |
5554 | if (ret == 1) | |
5555 | goto peek_stack; | |
5556 | else if (ret < 0) | |
5557 | goto err_free; | |
5558 | ||
5559 | ret = push_insn(t, t + insns[t].off + 1, BRANCH, env); | |
5560 | if (ret == 1) | |
5561 | goto peek_stack; | |
5562 | else if (ret < 0) | |
5563 | goto err_free; | |
5564 | } | |
5565 | } else { | |
5566 | /* all other non-branch instructions with single | |
5567 | * fall-through edge | |
5568 | */ | |
5569 | ret = push_insn(t, t + 1, FALLTHROUGH, env); | |
5570 | if (ret == 1) | |
5571 | goto peek_stack; | |
5572 | else if (ret < 0) | |
5573 | goto err_free; | |
5574 | } | |
5575 | ||
5576 | mark_explored: | |
5577 | insn_state[t] = EXPLORED; | |
7df737e9 | 5578 | if (env->cfg.cur_stack-- <= 0) { |
61bd5218 | 5579 | verbose(env, "pop stack internal bug\n"); |
475fb78f AS |
5580 | ret = -EFAULT; |
5581 | goto err_free; | |
5582 | } | |
5583 | goto peek_stack; | |
5584 | ||
5585 | check_state: | |
5586 | for (i = 0; i < insn_cnt; i++) { | |
5587 | if (insn_state[i] != EXPLORED) { | |
61bd5218 | 5588 | verbose(env, "unreachable insn %d\n", i); |
475fb78f AS |
5589 | ret = -EINVAL; |
5590 | goto err_free; | |
5591 | } | |
5592 | } | |
5593 | ret = 0; /* cfg looks good */ | |
5594 | ||
5595 | err_free: | |
71dde681 AS |
5596 | kvfree(insn_state); |
5597 | kvfree(insn_stack); | |
7df737e9 | 5598 | env->cfg.insn_state = env->cfg.insn_stack = NULL; |
475fb78f AS |
5599 | return ret; |
5600 | } | |
5601 | ||
838e9690 YS |
5602 | /* The minimum supported BTF func info size */ |
5603 | #define MIN_BPF_FUNCINFO_SIZE 8 | |
5604 | #define MAX_FUNCINFO_REC_SIZE 252 | |
5605 | ||
c454a46b MKL |
5606 | static int check_btf_func(struct bpf_verifier_env *env, |
5607 | const union bpf_attr *attr, | |
5608 | union bpf_attr __user *uattr) | |
838e9690 | 5609 | { |
d0b2818e | 5610 | u32 i, nfuncs, urec_size, min_size; |
838e9690 | 5611 | u32 krec_size = sizeof(struct bpf_func_info); |
c454a46b | 5612 | struct bpf_func_info *krecord; |
838e9690 | 5613 | const struct btf_type *type; |
c454a46b MKL |
5614 | struct bpf_prog *prog; |
5615 | const struct btf *btf; | |
838e9690 | 5616 | void __user *urecord; |
d0b2818e | 5617 | u32 prev_offset = 0; |
838e9690 YS |
5618 | int ret = 0; |
5619 | ||
5620 | nfuncs = attr->func_info_cnt; | |
5621 | if (!nfuncs) | |
5622 | return 0; | |
5623 | ||
5624 | if (nfuncs != env->subprog_cnt) { | |
5625 | verbose(env, "number of funcs in func_info doesn't match number of subprogs\n"); | |
5626 | return -EINVAL; | |
5627 | } | |
5628 | ||
5629 | urec_size = attr->func_info_rec_size; | |
5630 | if (urec_size < MIN_BPF_FUNCINFO_SIZE || | |
5631 | urec_size > MAX_FUNCINFO_REC_SIZE || | |
5632 | urec_size % sizeof(u32)) { | |
5633 | verbose(env, "invalid func info rec size %u\n", urec_size); | |
5634 | return -EINVAL; | |
5635 | } | |
5636 | ||
c454a46b MKL |
5637 | prog = env->prog; |
5638 | btf = prog->aux->btf; | |
838e9690 YS |
5639 | |
5640 | urecord = u64_to_user_ptr(attr->func_info); | |
5641 | min_size = min_t(u32, krec_size, urec_size); | |
5642 | ||
ba64e7d8 | 5643 | krecord = kvcalloc(nfuncs, krec_size, GFP_KERNEL | __GFP_NOWARN); |
c454a46b MKL |
5644 | if (!krecord) |
5645 | return -ENOMEM; | |
ba64e7d8 | 5646 | |
838e9690 YS |
5647 | for (i = 0; i < nfuncs; i++) { |
5648 | ret = bpf_check_uarg_tail_zero(urecord, krec_size, urec_size); | |
5649 | if (ret) { | |
5650 | if (ret == -E2BIG) { | |
5651 | verbose(env, "nonzero tailing record in func info"); | |
5652 | /* set the size kernel expects so loader can zero | |
5653 | * out the rest of the record. | |
5654 | */ | |
5655 | if (put_user(min_size, &uattr->func_info_rec_size)) | |
5656 | ret = -EFAULT; | |
5657 | } | |
c454a46b | 5658 | goto err_free; |
838e9690 YS |
5659 | } |
5660 | ||
ba64e7d8 | 5661 | if (copy_from_user(&krecord[i], urecord, min_size)) { |
838e9690 | 5662 | ret = -EFAULT; |
c454a46b | 5663 | goto err_free; |
838e9690 YS |
5664 | } |
5665 | ||
d30d42e0 | 5666 | /* check insn_off */ |
838e9690 | 5667 | if (i == 0) { |
d30d42e0 | 5668 | if (krecord[i].insn_off) { |
838e9690 | 5669 | verbose(env, |
d30d42e0 MKL |
5670 | "nonzero insn_off %u for the first func info record", |
5671 | krecord[i].insn_off); | |
838e9690 | 5672 | ret = -EINVAL; |
c454a46b | 5673 | goto err_free; |
838e9690 | 5674 | } |
d30d42e0 | 5675 | } else if (krecord[i].insn_off <= prev_offset) { |
838e9690 YS |
5676 | verbose(env, |
5677 | "same or smaller insn offset (%u) than previous func info record (%u)", | |
d30d42e0 | 5678 | krecord[i].insn_off, prev_offset); |
838e9690 | 5679 | ret = -EINVAL; |
c454a46b | 5680 | goto err_free; |
838e9690 YS |
5681 | } |
5682 | ||
d30d42e0 | 5683 | if (env->subprog_info[i].start != krecord[i].insn_off) { |
838e9690 YS |
5684 | verbose(env, "func_info BTF section doesn't match subprog layout in BPF program\n"); |
5685 | ret = -EINVAL; | |
c454a46b | 5686 | goto err_free; |
838e9690 YS |
5687 | } |
5688 | ||
5689 | /* check type_id */ | |
ba64e7d8 | 5690 | type = btf_type_by_id(btf, krecord[i].type_id); |
838e9690 YS |
5691 | if (!type || BTF_INFO_KIND(type->info) != BTF_KIND_FUNC) { |
5692 | verbose(env, "invalid type id %d in func info", | |
ba64e7d8 | 5693 | krecord[i].type_id); |
838e9690 | 5694 | ret = -EINVAL; |
c454a46b | 5695 | goto err_free; |
838e9690 YS |
5696 | } |
5697 | ||
d30d42e0 | 5698 | prev_offset = krecord[i].insn_off; |
838e9690 YS |
5699 | urecord += urec_size; |
5700 | } | |
5701 | ||
ba64e7d8 YS |
5702 | prog->aux->func_info = krecord; |
5703 | prog->aux->func_info_cnt = nfuncs; | |
838e9690 YS |
5704 | return 0; |
5705 | ||
c454a46b | 5706 | err_free: |
ba64e7d8 | 5707 | kvfree(krecord); |
838e9690 YS |
5708 | return ret; |
5709 | } | |
5710 | ||
ba64e7d8 YS |
5711 | static void adjust_btf_func(struct bpf_verifier_env *env) |
5712 | { | |
5713 | int i; | |
5714 | ||
5715 | if (!env->prog->aux->func_info) | |
5716 | return; | |
5717 | ||
5718 | for (i = 0; i < env->subprog_cnt; i++) | |
d30d42e0 | 5719 | env->prog->aux->func_info[i].insn_off = env->subprog_info[i].start; |
ba64e7d8 YS |
5720 | } |
5721 | ||
c454a46b MKL |
5722 | #define MIN_BPF_LINEINFO_SIZE (offsetof(struct bpf_line_info, line_col) + \ |
5723 | sizeof(((struct bpf_line_info *)(0))->line_col)) | |
5724 | #define MAX_LINEINFO_REC_SIZE MAX_FUNCINFO_REC_SIZE | |
5725 | ||
5726 | static int check_btf_line(struct bpf_verifier_env *env, | |
5727 | const union bpf_attr *attr, | |
5728 | union bpf_attr __user *uattr) | |
5729 | { | |
5730 | u32 i, s, nr_linfo, ncopy, expected_size, rec_size, prev_offset = 0; | |
5731 | struct bpf_subprog_info *sub; | |
5732 | struct bpf_line_info *linfo; | |
5733 | struct bpf_prog *prog; | |
5734 | const struct btf *btf; | |
5735 | void __user *ulinfo; | |
5736 | int err; | |
5737 | ||
5738 | nr_linfo = attr->line_info_cnt; | |
5739 | if (!nr_linfo) | |
5740 | return 0; | |
5741 | ||
5742 | rec_size = attr->line_info_rec_size; | |
5743 | if (rec_size < MIN_BPF_LINEINFO_SIZE || | |
5744 | rec_size > MAX_LINEINFO_REC_SIZE || | |
5745 | rec_size & (sizeof(u32) - 1)) | |
5746 | return -EINVAL; | |
5747 | ||
5748 | /* Need to zero it in case the userspace may | |
5749 | * pass in a smaller bpf_line_info object. | |
5750 | */ | |
5751 | linfo = kvcalloc(nr_linfo, sizeof(struct bpf_line_info), | |
5752 | GFP_KERNEL | __GFP_NOWARN); | |
5753 | if (!linfo) | |
5754 | return -ENOMEM; | |
5755 | ||
5756 | prog = env->prog; | |
5757 | btf = prog->aux->btf; | |
5758 | ||
5759 | s = 0; | |
5760 | sub = env->subprog_info; | |
5761 | ulinfo = u64_to_user_ptr(attr->line_info); | |
5762 | expected_size = sizeof(struct bpf_line_info); | |
5763 | ncopy = min_t(u32, expected_size, rec_size); | |
5764 | for (i = 0; i < nr_linfo; i++) { | |
5765 | err = bpf_check_uarg_tail_zero(ulinfo, expected_size, rec_size); | |
5766 | if (err) { | |
5767 | if (err == -E2BIG) { | |
5768 | verbose(env, "nonzero tailing record in line_info"); | |
5769 | if (put_user(expected_size, | |
5770 | &uattr->line_info_rec_size)) | |
5771 | err = -EFAULT; | |
5772 | } | |
5773 | goto err_free; | |
5774 | } | |
5775 | ||
5776 | if (copy_from_user(&linfo[i], ulinfo, ncopy)) { | |
5777 | err = -EFAULT; | |
5778 | goto err_free; | |
5779 | } | |
5780 | ||
5781 | /* | |
5782 | * Check insn_off to ensure | |
5783 | * 1) strictly increasing AND | |
5784 | * 2) bounded by prog->len | |
5785 | * | |
5786 | * The linfo[0].insn_off == 0 check logically falls into | |
5787 | * the later "missing bpf_line_info for func..." case | |
5788 | * because the first linfo[0].insn_off must be the | |
5789 | * first sub also and the first sub must have | |
5790 | * subprog_info[0].start == 0. | |
5791 | */ | |
5792 | if ((i && linfo[i].insn_off <= prev_offset) || | |
5793 | linfo[i].insn_off >= prog->len) { | |
5794 | verbose(env, "Invalid line_info[%u].insn_off:%u (prev_offset:%u prog->len:%u)\n", | |
5795 | i, linfo[i].insn_off, prev_offset, | |
5796 | prog->len); | |
5797 | err = -EINVAL; | |
5798 | goto err_free; | |
5799 | } | |
5800 | ||
fdbaa0be MKL |
5801 | if (!prog->insnsi[linfo[i].insn_off].code) { |
5802 | verbose(env, | |
5803 | "Invalid insn code at line_info[%u].insn_off\n", | |
5804 | i); | |
5805 | err = -EINVAL; | |
5806 | goto err_free; | |
5807 | } | |
5808 | ||
23127b33 MKL |
5809 | if (!btf_name_by_offset(btf, linfo[i].line_off) || |
5810 | !btf_name_by_offset(btf, linfo[i].file_name_off)) { | |
c454a46b MKL |
5811 | verbose(env, "Invalid line_info[%u].line_off or .file_name_off\n", i); |
5812 | err = -EINVAL; | |
5813 | goto err_free; | |
5814 | } | |
5815 | ||
5816 | if (s != env->subprog_cnt) { | |
5817 | if (linfo[i].insn_off == sub[s].start) { | |
5818 | sub[s].linfo_idx = i; | |
5819 | s++; | |
5820 | } else if (sub[s].start < linfo[i].insn_off) { | |
5821 | verbose(env, "missing bpf_line_info for func#%u\n", s); | |
5822 | err = -EINVAL; | |
5823 | goto err_free; | |
5824 | } | |
5825 | } | |
5826 | ||
5827 | prev_offset = linfo[i].insn_off; | |
5828 | ulinfo += rec_size; | |
5829 | } | |
5830 | ||
5831 | if (s != env->subprog_cnt) { | |
5832 | verbose(env, "missing bpf_line_info for %u funcs starting from func#%u\n", | |
5833 | env->subprog_cnt - s, s); | |
5834 | err = -EINVAL; | |
5835 | goto err_free; | |
5836 | } | |
5837 | ||
5838 | prog->aux->linfo = linfo; | |
5839 | prog->aux->nr_linfo = nr_linfo; | |
5840 | ||
5841 | return 0; | |
5842 | ||
5843 | err_free: | |
5844 | kvfree(linfo); | |
5845 | return err; | |
5846 | } | |
5847 | ||
5848 | static int check_btf_info(struct bpf_verifier_env *env, | |
5849 | const union bpf_attr *attr, | |
5850 | union bpf_attr __user *uattr) | |
5851 | { | |
5852 | struct btf *btf; | |
5853 | int err; | |
5854 | ||
5855 | if (!attr->func_info_cnt && !attr->line_info_cnt) | |
5856 | return 0; | |
5857 | ||
5858 | btf = btf_get_by_fd(attr->prog_btf_fd); | |
5859 | if (IS_ERR(btf)) | |
5860 | return PTR_ERR(btf); | |
5861 | env->prog->aux->btf = btf; | |
5862 | ||
5863 | err = check_btf_func(env, attr, uattr); | |
5864 | if (err) | |
5865 | return err; | |
5866 | ||
5867 | err = check_btf_line(env, attr, uattr); | |
5868 | if (err) | |
5869 | return err; | |
5870 | ||
5871 | return 0; | |
ba64e7d8 YS |
5872 | } |
5873 | ||
f1174f77 EC |
5874 | /* check %cur's range satisfies %old's */ |
5875 | static bool range_within(struct bpf_reg_state *old, | |
5876 | struct bpf_reg_state *cur) | |
5877 | { | |
b03c9f9f EC |
5878 | return old->umin_value <= cur->umin_value && |
5879 | old->umax_value >= cur->umax_value && | |
5880 | old->smin_value <= cur->smin_value && | |
5881 | old->smax_value >= cur->smax_value; | |
f1174f77 EC |
5882 | } |
5883 | ||
5884 | /* Maximum number of register states that can exist at once */ | |
5885 | #define ID_MAP_SIZE (MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE) | |
5886 | struct idpair { | |
5887 | u32 old; | |
5888 | u32 cur; | |
5889 | }; | |
5890 | ||
5891 | /* If in the old state two registers had the same id, then they need to have | |
5892 | * the same id in the new state as well. But that id could be different from | |
5893 | * the old state, so we need to track the mapping from old to new ids. | |
5894 | * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent | |
5895 | * regs with old id 5 must also have new id 9 for the new state to be safe. But | |
5896 | * regs with a different old id could still have new id 9, we don't care about | |
5897 | * that. | |
5898 | * So we look through our idmap to see if this old id has been seen before. If | |
5899 | * so, we require the new id to match; otherwise, we add the id pair to the map. | |
969bf05e | 5900 | */ |
f1174f77 | 5901 | static bool check_ids(u32 old_id, u32 cur_id, struct idpair *idmap) |
969bf05e | 5902 | { |
f1174f77 | 5903 | unsigned int i; |
969bf05e | 5904 | |
f1174f77 EC |
5905 | for (i = 0; i < ID_MAP_SIZE; i++) { |
5906 | if (!idmap[i].old) { | |
5907 | /* Reached an empty slot; haven't seen this id before */ | |
5908 | idmap[i].old = old_id; | |
5909 | idmap[i].cur = cur_id; | |
5910 | return true; | |
5911 | } | |
5912 | if (idmap[i].old == old_id) | |
5913 | return idmap[i].cur == cur_id; | |
5914 | } | |
5915 | /* We ran out of idmap slots, which should be impossible */ | |
5916 | WARN_ON_ONCE(1); | |
5917 | return false; | |
5918 | } | |
5919 | ||
9242b5f5 AS |
5920 | static void clean_func_state(struct bpf_verifier_env *env, |
5921 | struct bpf_func_state *st) | |
5922 | { | |
5923 | enum bpf_reg_liveness live; | |
5924 | int i, j; | |
5925 | ||
5926 | for (i = 0; i < BPF_REG_FP; i++) { | |
5927 | live = st->regs[i].live; | |
5928 | /* liveness must not touch this register anymore */ | |
5929 | st->regs[i].live |= REG_LIVE_DONE; | |
5930 | if (!(live & REG_LIVE_READ)) | |
5931 | /* since the register is unused, clear its state | |
5932 | * to make further comparison simpler | |
5933 | */ | |
5934 | __mark_reg_not_init(&st->regs[i]); | |
5935 | } | |
5936 | ||
5937 | for (i = 0; i < st->allocated_stack / BPF_REG_SIZE; i++) { | |
5938 | live = st->stack[i].spilled_ptr.live; | |
5939 | /* liveness must not touch this stack slot anymore */ | |
5940 | st->stack[i].spilled_ptr.live |= REG_LIVE_DONE; | |
5941 | if (!(live & REG_LIVE_READ)) { | |
5942 | __mark_reg_not_init(&st->stack[i].spilled_ptr); | |
5943 | for (j = 0; j < BPF_REG_SIZE; j++) | |
5944 | st->stack[i].slot_type[j] = STACK_INVALID; | |
5945 | } | |
5946 | } | |
5947 | } | |
5948 | ||
5949 | static void clean_verifier_state(struct bpf_verifier_env *env, | |
5950 | struct bpf_verifier_state *st) | |
5951 | { | |
5952 | int i; | |
5953 | ||
5954 | if (st->frame[0]->regs[0].live & REG_LIVE_DONE) | |
5955 | /* all regs in this state in all frames were already marked */ | |
5956 | return; | |
5957 | ||
5958 | for (i = 0; i <= st->curframe; i++) | |
5959 | clean_func_state(env, st->frame[i]); | |
5960 | } | |
5961 | ||
5962 | /* the parentage chains form a tree. | |
5963 | * the verifier states are added to state lists at given insn and | |
5964 | * pushed into state stack for future exploration. | |
5965 | * when the verifier reaches bpf_exit insn some of the verifer states | |
5966 | * stored in the state lists have their final liveness state already, | |
5967 | * but a lot of states will get revised from liveness point of view when | |
5968 | * the verifier explores other branches. | |
5969 | * Example: | |
5970 | * 1: r0 = 1 | |
5971 | * 2: if r1 == 100 goto pc+1 | |
5972 | * 3: r0 = 2 | |
5973 | * 4: exit | |
5974 | * when the verifier reaches exit insn the register r0 in the state list of | |
5975 | * insn 2 will be seen as !REG_LIVE_READ. Then the verifier pops the other_branch | |
5976 | * of insn 2 and goes exploring further. At the insn 4 it will walk the | |
5977 | * parentage chain from insn 4 into insn 2 and will mark r0 as REG_LIVE_READ. | |
5978 | * | |
5979 | * Since the verifier pushes the branch states as it sees them while exploring | |
5980 | * the program the condition of walking the branch instruction for the second | |
5981 | * time means that all states below this branch were already explored and | |
5982 | * their final liveness markes are already propagated. | |
5983 | * Hence when the verifier completes the search of state list in is_state_visited() | |
5984 | * we can call this clean_live_states() function to mark all liveness states | |
5985 | * as REG_LIVE_DONE to indicate that 'parent' pointers of 'struct bpf_reg_state' | |
5986 | * will not be used. | |
5987 | * This function also clears the registers and stack for states that !READ | |
5988 | * to simplify state merging. | |
5989 | * | |
5990 | * Important note here that walking the same branch instruction in the callee | |
5991 | * doesn't meant that the states are DONE. The verifier has to compare | |
5992 | * the callsites | |
5993 | */ | |
5994 | static void clean_live_states(struct bpf_verifier_env *env, int insn, | |
5995 | struct bpf_verifier_state *cur) | |
5996 | { | |
5997 | struct bpf_verifier_state_list *sl; | |
5998 | int i; | |
5999 | ||
6000 | sl = env->explored_states[insn]; | |
6001 | if (!sl) | |
6002 | return; | |
6003 | ||
6004 | while (sl != STATE_LIST_MARK) { | |
6005 | if (sl->state.curframe != cur->curframe) | |
6006 | goto next; | |
6007 | for (i = 0; i <= cur->curframe; i++) | |
6008 | if (sl->state.frame[i]->callsite != cur->frame[i]->callsite) | |
6009 | goto next; | |
6010 | clean_verifier_state(env, &sl->state); | |
6011 | next: | |
6012 | sl = sl->next; | |
6013 | } | |
6014 | } | |
6015 | ||
f1174f77 | 6016 | /* Returns true if (rold safe implies rcur safe) */ |
1b688a19 EC |
6017 | static bool regsafe(struct bpf_reg_state *rold, struct bpf_reg_state *rcur, |
6018 | struct idpair *idmap) | |
f1174f77 | 6019 | { |
f4d7e40a AS |
6020 | bool equal; |
6021 | ||
dc503a8a EC |
6022 | if (!(rold->live & REG_LIVE_READ)) |
6023 | /* explored state didn't use this */ | |
6024 | return true; | |
6025 | ||
679c782d | 6026 | equal = memcmp(rold, rcur, offsetof(struct bpf_reg_state, parent)) == 0; |
f4d7e40a AS |
6027 | |
6028 | if (rold->type == PTR_TO_STACK) | |
6029 | /* two stack pointers are equal only if they're pointing to | |
6030 | * the same stack frame, since fp-8 in foo != fp-8 in bar | |
6031 | */ | |
6032 | return equal && rold->frameno == rcur->frameno; | |
6033 | ||
6034 | if (equal) | |
969bf05e AS |
6035 | return true; |
6036 | ||
f1174f77 EC |
6037 | if (rold->type == NOT_INIT) |
6038 | /* explored state can't have used this */ | |
969bf05e | 6039 | return true; |
f1174f77 EC |
6040 | if (rcur->type == NOT_INIT) |
6041 | return false; | |
6042 | switch (rold->type) { | |
6043 | case SCALAR_VALUE: | |
6044 | if (rcur->type == SCALAR_VALUE) { | |
6045 | /* new val must satisfy old val knowledge */ | |
6046 | return range_within(rold, rcur) && | |
6047 | tnum_in(rold->var_off, rcur->var_off); | |
6048 | } else { | |
179d1c56 JH |
6049 | /* We're trying to use a pointer in place of a scalar. |
6050 | * Even if the scalar was unbounded, this could lead to | |
6051 | * pointer leaks because scalars are allowed to leak | |
6052 | * while pointers are not. We could make this safe in | |
6053 | * special cases if root is calling us, but it's | |
6054 | * probably not worth the hassle. | |
f1174f77 | 6055 | */ |
179d1c56 | 6056 | return false; |
f1174f77 EC |
6057 | } |
6058 | case PTR_TO_MAP_VALUE: | |
1b688a19 EC |
6059 | /* If the new min/max/var_off satisfy the old ones and |
6060 | * everything else matches, we are OK. | |
d83525ca AS |
6061 | * 'id' is not compared, since it's only used for maps with |
6062 | * bpf_spin_lock inside map element and in such cases if | |
6063 | * the rest of the prog is valid for one map element then | |
6064 | * it's valid for all map elements regardless of the key | |
6065 | * used in bpf_map_lookup() | |
1b688a19 EC |
6066 | */ |
6067 | return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 && | |
6068 | range_within(rold, rcur) && | |
6069 | tnum_in(rold->var_off, rcur->var_off); | |
f1174f77 EC |
6070 | case PTR_TO_MAP_VALUE_OR_NULL: |
6071 | /* a PTR_TO_MAP_VALUE could be safe to use as a | |
6072 | * PTR_TO_MAP_VALUE_OR_NULL into the same map. | |
6073 | * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL- | |
6074 | * checked, doing so could have affected others with the same | |
6075 | * id, and we can't check for that because we lost the id when | |
6076 | * we converted to a PTR_TO_MAP_VALUE. | |
6077 | */ | |
6078 | if (rcur->type != PTR_TO_MAP_VALUE_OR_NULL) | |
6079 | return false; | |
6080 | if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, id))) | |
6081 | return false; | |
6082 | /* Check our ids match any regs they're supposed to */ | |
6083 | return check_ids(rold->id, rcur->id, idmap); | |
de8f3a83 | 6084 | case PTR_TO_PACKET_META: |
f1174f77 | 6085 | case PTR_TO_PACKET: |
de8f3a83 | 6086 | if (rcur->type != rold->type) |
f1174f77 EC |
6087 | return false; |
6088 | /* We must have at least as much range as the old ptr | |
6089 | * did, so that any accesses which were safe before are | |
6090 | * still safe. This is true even if old range < old off, | |
6091 | * since someone could have accessed through (ptr - k), or | |
6092 | * even done ptr -= k in a register, to get a safe access. | |
6093 | */ | |
6094 | if (rold->range > rcur->range) | |
6095 | return false; | |
6096 | /* If the offsets don't match, we can't trust our alignment; | |
6097 | * nor can we be sure that we won't fall out of range. | |
6098 | */ | |
6099 | if (rold->off != rcur->off) | |
6100 | return false; | |
6101 | /* id relations must be preserved */ | |
6102 | if (rold->id && !check_ids(rold->id, rcur->id, idmap)) | |
6103 | return false; | |
6104 | /* new val must satisfy old val knowledge */ | |
6105 | return range_within(rold, rcur) && | |
6106 | tnum_in(rold->var_off, rcur->var_off); | |
6107 | case PTR_TO_CTX: | |
6108 | case CONST_PTR_TO_MAP: | |
f1174f77 | 6109 | case PTR_TO_PACKET_END: |
d58e468b | 6110 | case PTR_TO_FLOW_KEYS: |
c64b7983 JS |
6111 | case PTR_TO_SOCKET: |
6112 | case PTR_TO_SOCKET_OR_NULL: | |
46f8bc92 MKL |
6113 | case PTR_TO_SOCK_COMMON: |
6114 | case PTR_TO_SOCK_COMMON_OR_NULL: | |
655a51e5 MKL |
6115 | case PTR_TO_TCP_SOCK: |
6116 | case PTR_TO_TCP_SOCK_OR_NULL: | |
f1174f77 EC |
6117 | /* Only valid matches are exact, which memcmp() above |
6118 | * would have accepted | |
6119 | */ | |
6120 | default: | |
6121 | /* Don't know what's going on, just say it's not safe */ | |
6122 | return false; | |
6123 | } | |
969bf05e | 6124 | |
f1174f77 EC |
6125 | /* Shouldn't get here; if we do, say it's not safe */ |
6126 | WARN_ON_ONCE(1); | |
969bf05e AS |
6127 | return false; |
6128 | } | |
6129 | ||
f4d7e40a AS |
6130 | static bool stacksafe(struct bpf_func_state *old, |
6131 | struct bpf_func_state *cur, | |
638f5b90 AS |
6132 | struct idpair *idmap) |
6133 | { | |
6134 | int i, spi; | |
6135 | ||
638f5b90 AS |
6136 | /* walk slots of the explored stack and ignore any additional |
6137 | * slots in the current stack, since explored(safe) state | |
6138 | * didn't use them | |
6139 | */ | |
6140 | for (i = 0; i < old->allocated_stack; i++) { | |
6141 | spi = i / BPF_REG_SIZE; | |
6142 | ||
b233920c AS |
6143 | if (!(old->stack[spi].spilled_ptr.live & REG_LIVE_READ)) { |
6144 | i += BPF_REG_SIZE - 1; | |
cc2b14d5 | 6145 | /* explored state didn't use this */ |
fd05e57b | 6146 | continue; |
b233920c | 6147 | } |
cc2b14d5 | 6148 | |
638f5b90 AS |
6149 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID) |
6150 | continue; | |
19e2dbb7 AS |
6151 | |
6152 | /* explored stack has more populated slots than current stack | |
6153 | * and these slots were used | |
6154 | */ | |
6155 | if (i >= cur->allocated_stack) | |
6156 | return false; | |
6157 | ||
cc2b14d5 AS |
6158 | /* if old state was safe with misc data in the stack |
6159 | * it will be safe with zero-initialized stack. | |
6160 | * The opposite is not true | |
6161 | */ | |
6162 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC && | |
6163 | cur->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_ZERO) | |
6164 | continue; | |
638f5b90 AS |
6165 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] != |
6166 | cur->stack[spi].slot_type[i % BPF_REG_SIZE]) | |
6167 | /* Ex: old explored (safe) state has STACK_SPILL in | |
6168 | * this stack slot, but current has has STACK_MISC -> | |
6169 | * this verifier states are not equivalent, | |
6170 | * return false to continue verification of this path | |
6171 | */ | |
6172 | return false; | |
6173 | if (i % BPF_REG_SIZE) | |
6174 | continue; | |
6175 | if (old->stack[spi].slot_type[0] != STACK_SPILL) | |
6176 | continue; | |
6177 | if (!regsafe(&old->stack[spi].spilled_ptr, | |
6178 | &cur->stack[spi].spilled_ptr, | |
6179 | idmap)) | |
6180 | /* when explored and current stack slot are both storing | |
6181 | * spilled registers, check that stored pointers types | |
6182 | * are the same as well. | |
6183 | * Ex: explored safe path could have stored | |
6184 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8} | |
6185 | * but current path has stored: | |
6186 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16} | |
6187 | * such verifier states are not equivalent. | |
6188 | * return false to continue verification of this path | |
6189 | */ | |
6190 | return false; | |
6191 | } | |
6192 | return true; | |
6193 | } | |
6194 | ||
fd978bf7 JS |
6195 | static bool refsafe(struct bpf_func_state *old, struct bpf_func_state *cur) |
6196 | { | |
6197 | if (old->acquired_refs != cur->acquired_refs) | |
6198 | return false; | |
6199 | return !memcmp(old->refs, cur->refs, | |
6200 | sizeof(*old->refs) * old->acquired_refs); | |
6201 | } | |
6202 | ||
f1bca824 AS |
6203 | /* compare two verifier states |
6204 | * | |
6205 | * all states stored in state_list are known to be valid, since | |
6206 | * verifier reached 'bpf_exit' instruction through them | |
6207 | * | |
6208 | * this function is called when verifier exploring different branches of | |
6209 | * execution popped from the state stack. If it sees an old state that has | |
6210 | * more strict register state and more strict stack state then this execution | |
6211 | * branch doesn't need to be explored further, since verifier already | |
6212 | * concluded that more strict state leads to valid finish. | |
6213 | * | |
6214 | * Therefore two states are equivalent if register state is more conservative | |
6215 | * and explored stack state is more conservative than the current one. | |
6216 | * Example: | |
6217 | * explored current | |
6218 | * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC) | |
6219 | * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC) | |
6220 | * | |
6221 | * In other words if current stack state (one being explored) has more | |
6222 | * valid slots than old one that already passed validation, it means | |
6223 | * the verifier can stop exploring and conclude that current state is valid too | |
6224 | * | |
6225 | * Similarly with registers. If explored state has register type as invalid | |
6226 | * whereas register type in current state is meaningful, it means that | |
6227 | * the current state will reach 'bpf_exit' instruction safely | |
6228 | */ | |
f4d7e40a AS |
6229 | static bool func_states_equal(struct bpf_func_state *old, |
6230 | struct bpf_func_state *cur) | |
f1bca824 | 6231 | { |
f1174f77 EC |
6232 | struct idpair *idmap; |
6233 | bool ret = false; | |
f1bca824 AS |
6234 | int i; |
6235 | ||
f1174f77 EC |
6236 | idmap = kcalloc(ID_MAP_SIZE, sizeof(struct idpair), GFP_KERNEL); |
6237 | /* If we failed to allocate the idmap, just say it's not safe */ | |
6238 | if (!idmap) | |
1a0dc1ac | 6239 | return false; |
f1174f77 EC |
6240 | |
6241 | for (i = 0; i < MAX_BPF_REG; i++) { | |
1b688a19 | 6242 | if (!regsafe(&old->regs[i], &cur->regs[i], idmap)) |
f1174f77 | 6243 | goto out_free; |
f1bca824 AS |
6244 | } |
6245 | ||
638f5b90 AS |
6246 | if (!stacksafe(old, cur, idmap)) |
6247 | goto out_free; | |
fd978bf7 JS |
6248 | |
6249 | if (!refsafe(old, cur)) | |
6250 | goto out_free; | |
f1174f77 EC |
6251 | ret = true; |
6252 | out_free: | |
6253 | kfree(idmap); | |
6254 | return ret; | |
f1bca824 AS |
6255 | } |
6256 | ||
f4d7e40a AS |
6257 | static bool states_equal(struct bpf_verifier_env *env, |
6258 | struct bpf_verifier_state *old, | |
6259 | struct bpf_verifier_state *cur) | |
6260 | { | |
6261 | int i; | |
6262 | ||
6263 | if (old->curframe != cur->curframe) | |
6264 | return false; | |
6265 | ||
979d63d5 DB |
6266 | /* Verification state from speculative execution simulation |
6267 | * must never prune a non-speculative execution one. | |
6268 | */ | |
6269 | if (old->speculative && !cur->speculative) | |
6270 | return false; | |
6271 | ||
d83525ca AS |
6272 | if (old->active_spin_lock != cur->active_spin_lock) |
6273 | return false; | |
6274 | ||
f4d7e40a AS |
6275 | /* for states to be equal callsites have to be the same |
6276 | * and all frame states need to be equivalent | |
6277 | */ | |
6278 | for (i = 0; i <= old->curframe; i++) { | |
6279 | if (old->frame[i]->callsite != cur->frame[i]->callsite) | |
6280 | return false; | |
6281 | if (!func_states_equal(old->frame[i], cur->frame[i])) | |
6282 | return false; | |
6283 | } | |
6284 | return true; | |
6285 | } | |
6286 | ||
55e7f3b5 JW |
6287 | static int propagate_liveness_reg(struct bpf_verifier_env *env, |
6288 | struct bpf_reg_state *reg, | |
6289 | struct bpf_reg_state *parent_reg) | |
6290 | { | |
6291 | int err; | |
6292 | ||
6293 | if (parent_reg->live & REG_LIVE_READ || !(reg->live & REG_LIVE_READ)) | |
6294 | return 0; | |
6295 | ||
6296 | err = mark_reg_read(env, reg, parent_reg); | |
6297 | if (err) | |
6298 | return err; | |
6299 | ||
6300 | return 0; | |
6301 | } | |
6302 | ||
8e9cd9ce | 6303 | /* A write screens off any subsequent reads; but write marks come from the |
f4d7e40a AS |
6304 | * straight-line code between a state and its parent. When we arrive at an |
6305 | * equivalent state (jump target or such) we didn't arrive by the straight-line | |
6306 | * code, so read marks in the state must propagate to the parent regardless | |
6307 | * of the state's write marks. That's what 'parent == state->parent' comparison | |
679c782d | 6308 | * in mark_reg_read() is for. |
8e9cd9ce | 6309 | */ |
f4d7e40a AS |
6310 | static int propagate_liveness(struct bpf_verifier_env *env, |
6311 | const struct bpf_verifier_state *vstate, | |
6312 | struct bpf_verifier_state *vparent) | |
dc503a8a | 6313 | { |
3f8cafa4 | 6314 | struct bpf_reg_state *state_reg, *parent_reg; |
f4d7e40a | 6315 | struct bpf_func_state *state, *parent; |
3f8cafa4 | 6316 | int i, frame, err = 0; |
dc503a8a | 6317 | |
f4d7e40a AS |
6318 | if (vparent->curframe != vstate->curframe) { |
6319 | WARN(1, "propagate_live: parent frame %d current frame %d\n", | |
6320 | vparent->curframe, vstate->curframe); | |
6321 | return -EFAULT; | |
6322 | } | |
dc503a8a EC |
6323 | /* Propagate read liveness of registers... */ |
6324 | BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG); | |
83d16312 | 6325 | for (frame = 0; frame <= vstate->curframe; frame++) { |
3f8cafa4 JW |
6326 | parent = vparent->frame[frame]; |
6327 | state = vstate->frame[frame]; | |
6328 | parent_reg = parent->regs; | |
6329 | state_reg = state->regs; | |
83d16312 JK |
6330 | /* We don't need to worry about FP liveness, it's read-only */ |
6331 | for (i = frame < vstate->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) { | |
55e7f3b5 JW |
6332 | err = propagate_liveness_reg(env, &state_reg[i], |
6333 | &parent_reg[i]); | |
3f8cafa4 JW |
6334 | if (err) |
6335 | return err; | |
dc503a8a | 6336 | } |
f4d7e40a | 6337 | |
1b04aee7 | 6338 | /* Propagate stack slots. */ |
f4d7e40a AS |
6339 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE && |
6340 | i < parent->allocated_stack / BPF_REG_SIZE; i++) { | |
3f8cafa4 JW |
6341 | parent_reg = &parent->stack[i].spilled_ptr; |
6342 | state_reg = &state->stack[i].spilled_ptr; | |
55e7f3b5 JW |
6343 | err = propagate_liveness_reg(env, state_reg, |
6344 | parent_reg); | |
3f8cafa4 JW |
6345 | if (err) |
6346 | return err; | |
dc503a8a EC |
6347 | } |
6348 | } | |
f4d7e40a | 6349 | return err; |
dc503a8a EC |
6350 | } |
6351 | ||
58e2af8b | 6352 | static int is_state_visited(struct bpf_verifier_env *env, int insn_idx) |
f1bca824 | 6353 | { |
58e2af8b | 6354 | struct bpf_verifier_state_list *new_sl; |
9f4686c4 | 6355 | struct bpf_verifier_state_list *sl, **pprev; |
679c782d | 6356 | struct bpf_verifier_state *cur = env->cur_state, *new; |
ceefbc96 | 6357 | int i, j, err, states_cnt = 0; |
f1bca824 | 6358 | |
9f4686c4 AS |
6359 | pprev = &env->explored_states[insn_idx]; |
6360 | sl = *pprev; | |
6361 | ||
f1bca824 AS |
6362 | if (!sl) |
6363 | /* this 'insn_idx' instruction wasn't marked, so we will not | |
6364 | * be doing state search here | |
6365 | */ | |
6366 | return 0; | |
6367 | ||
9242b5f5 AS |
6368 | clean_live_states(env, insn_idx, cur); |
6369 | ||
f1bca824 | 6370 | while (sl != STATE_LIST_MARK) { |
638f5b90 | 6371 | if (states_equal(env, &sl->state, cur)) { |
9f4686c4 | 6372 | sl->hit_cnt++; |
f1bca824 | 6373 | /* reached equivalent register/stack state, |
dc503a8a EC |
6374 | * prune the search. |
6375 | * Registers read by the continuation are read by us. | |
8e9cd9ce EC |
6376 | * If we have any write marks in env->cur_state, they |
6377 | * will prevent corresponding reads in the continuation | |
6378 | * from reaching our parent (an explored_state). Our | |
6379 | * own state will get the read marks recorded, but | |
6380 | * they'll be immediately forgotten as we're pruning | |
6381 | * this state and will pop a new one. | |
f1bca824 | 6382 | */ |
f4d7e40a AS |
6383 | err = propagate_liveness(env, &sl->state, cur); |
6384 | if (err) | |
6385 | return err; | |
f1bca824 | 6386 | return 1; |
dc503a8a | 6387 | } |
ceefbc96 | 6388 | states_cnt++; |
9f4686c4 AS |
6389 | sl->miss_cnt++; |
6390 | /* heuristic to determine whether this state is beneficial | |
6391 | * to keep checking from state equivalence point of view. | |
6392 | * Higher numbers increase max_states_per_insn and verification time, | |
6393 | * but do not meaningfully decrease insn_processed. | |
6394 | */ | |
6395 | if (sl->miss_cnt > sl->hit_cnt * 3 + 3) { | |
6396 | /* the state is unlikely to be useful. Remove it to | |
6397 | * speed up verification | |
6398 | */ | |
6399 | *pprev = sl->next; | |
6400 | if (sl->state.frame[0]->regs[0].live & REG_LIVE_DONE) { | |
6401 | free_verifier_state(&sl->state, false); | |
6402 | kfree(sl); | |
6403 | env->peak_states--; | |
6404 | } else { | |
6405 | /* cannot free this state, since parentage chain may | |
6406 | * walk it later. Add it for free_list instead to | |
6407 | * be freed at the end of verification | |
6408 | */ | |
6409 | sl->next = env->free_list; | |
6410 | env->free_list = sl; | |
6411 | } | |
6412 | sl = *pprev; | |
6413 | continue; | |
6414 | } | |
6415 | pprev = &sl->next; | |
6416 | sl = *pprev; | |
f1bca824 AS |
6417 | } |
6418 | ||
06ee7115 AS |
6419 | if (env->max_states_per_insn < states_cnt) |
6420 | env->max_states_per_insn = states_cnt; | |
6421 | ||
ceefbc96 AS |
6422 | if (!env->allow_ptr_leaks && states_cnt > BPF_COMPLEXITY_LIMIT_STATES) |
6423 | return 0; | |
6424 | ||
f1bca824 AS |
6425 | /* there were no equivalent states, remember current one. |
6426 | * technically the current state is not proven to be safe yet, | |
f4d7e40a AS |
6427 | * but it will either reach outer most bpf_exit (which means it's safe) |
6428 | * or it will be rejected. Since there are no loops, we won't be | |
6429 | * seeing this tuple (frame[0].callsite, frame[1].callsite, .. insn_idx) | |
6430 | * again on the way to bpf_exit | |
f1bca824 | 6431 | */ |
638f5b90 | 6432 | new_sl = kzalloc(sizeof(struct bpf_verifier_state_list), GFP_KERNEL); |
f1bca824 AS |
6433 | if (!new_sl) |
6434 | return -ENOMEM; | |
06ee7115 AS |
6435 | env->total_states++; |
6436 | env->peak_states++; | |
f1bca824 AS |
6437 | |
6438 | /* add new state to the head of linked list */ | |
679c782d EC |
6439 | new = &new_sl->state; |
6440 | err = copy_verifier_state(new, cur); | |
1969db47 | 6441 | if (err) { |
679c782d | 6442 | free_verifier_state(new, false); |
1969db47 AS |
6443 | kfree(new_sl); |
6444 | return err; | |
6445 | } | |
f1bca824 AS |
6446 | new_sl->next = env->explored_states[insn_idx]; |
6447 | env->explored_states[insn_idx] = new_sl; | |
7640ead9 JK |
6448 | /* connect new state to parentage chain. Current frame needs all |
6449 | * registers connected. Only r6 - r9 of the callers are alive (pushed | |
6450 | * to the stack implicitly by JITs) so in callers' frames connect just | |
6451 | * r6 - r9 as an optimization. Callers will have r1 - r5 connected to | |
6452 | * the state of the call instruction (with WRITTEN set), and r0 comes | |
6453 | * from callee with its full parentage chain, anyway. | |
6454 | */ | |
6455 | for (j = 0; j <= cur->curframe; j++) | |
6456 | for (i = j < cur->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) | |
6457 | cur->frame[j]->regs[i].parent = &new->frame[j]->regs[i]; | |
8e9cd9ce EC |
6458 | /* clear write marks in current state: the writes we did are not writes |
6459 | * our child did, so they don't screen off its reads from us. | |
6460 | * (There are no read marks in current state, because reads always mark | |
6461 | * their parent and current state never has children yet. Only | |
6462 | * explored_states can get read marks.) | |
6463 | */ | |
dc503a8a | 6464 | for (i = 0; i < BPF_REG_FP; i++) |
f4d7e40a AS |
6465 | cur->frame[cur->curframe]->regs[i].live = REG_LIVE_NONE; |
6466 | ||
6467 | /* all stack frames are accessible from callee, clear them all */ | |
6468 | for (j = 0; j <= cur->curframe; j++) { | |
6469 | struct bpf_func_state *frame = cur->frame[j]; | |
679c782d | 6470 | struct bpf_func_state *newframe = new->frame[j]; |
f4d7e40a | 6471 | |
679c782d | 6472 | for (i = 0; i < frame->allocated_stack / BPF_REG_SIZE; i++) { |
cc2b14d5 | 6473 | frame->stack[i].spilled_ptr.live = REG_LIVE_NONE; |
679c782d EC |
6474 | frame->stack[i].spilled_ptr.parent = |
6475 | &newframe->stack[i].spilled_ptr; | |
6476 | } | |
f4d7e40a | 6477 | } |
f1bca824 AS |
6478 | return 0; |
6479 | } | |
6480 | ||
c64b7983 JS |
6481 | /* Return true if it's OK to have the same insn return a different type. */ |
6482 | static bool reg_type_mismatch_ok(enum bpf_reg_type type) | |
6483 | { | |
6484 | switch (type) { | |
6485 | case PTR_TO_CTX: | |
6486 | case PTR_TO_SOCKET: | |
6487 | case PTR_TO_SOCKET_OR_NULL: | |
46f8bc92 MKL |
6488 | case PTR_TO_SOCK_COMMON: |
6489 | case PTR_TO_SOCK_COMMON_OR_NULL: | |
655a51e5 MKL |
6490 | case PTR_TO_TCP_SOCK: |
6491 | case PTR_TO_TCP_SOCK_OR_NULL: | |
c64b7983 JS |
6492 | return false; |
6493 | default: | |
6494 | return true; | |
6495 | } | |
6496 | } | |
6497 | ||
6498 | /* If an instruction was previously used with particular pointer types, then we | |
6499 | * need to be careful to avoid cases such as the below, where it may be ok | |
6500 | * for one branch accessing the pointer, but not ok for the other branch: | |
6501 | * | |
6502 | * R1 = sock_ptr | |
6503 | * goto X; | |
6504 | * ... | |
6505 | * R1 = some_other_valid_ptr; | |
6506 | * goto X; | |
6507 | * ... | |
6508 | * R2 = *(u32 *)(R1 + 0); | |
6509 | */ | |
6510 | static bool reg_type_mismatch(enum bpf_reg_type src, enum bpf_reg_type prev) | |
6511 | { | |
6512 | return src != prev && (!reg_type_mismatch_ok(src) || | |
6513 | !reg_type_mismatch_ok(prev)); | |
6514 | } | |
6515 | ||
58e2af8b | 6516 | static int do_check(struct bpf_verifier_env *env) |
17a52670 | 6517 | { |
638f5b90 | 6518 | struct bpf_verifier_state *state; |
17a52670 | 6519 | struct bpf_insn *insns = env->prog->insnsi; |
638f5b90 | 6520 | struct bpf_reg_state *regs; |
06ee7115 | 6521 | int insn_cnt = env->prog->len; |
17a52670 AS |
6522 | bool do_print_state = false; |
6523 | ||
d9762e84 MKL |
6524 | env->prev_linfo = NULL; |
6525 | ||
638f5b90 AS |
6526 | state = kzalloc(sizeof(struct bpf_verifier_state), GFP_KERNEL); |
6527 | if (!state) | |
6528 | return -ENOMEM; | |
f4d7e40a | 6529 | state->curframe = 0; |
979d63d5 | 6530 | state->speculative = false; |
f4d7e40a AS |
6531 | state->frame[0] = kzalloc(sizeof(struct bpf_func_state), GFP_KERNEL); |
6532 | if (!state->frame[0]) { | |
6533 | kfree(state); | |
6534 | return -ENOMEM; | |
6535 | } | |
6536 | env->cur_state = state; | |
6537 | init_func_state(env, state->frame[0], | |
6538 | BPF_MAIN_FUNC /* callsite */, | |
6539 | 0 /* frameno */, | |
6540 | 0 /* subprogno, zero == main subprog */); | |
c08435ec | 6541 | |
17a52670 AS |
6542 | for (;;) { |
6543 | struct bpf_insn *insn; | |
6544 | u8 class; | |
6545 | int err; | |
6546 | ||
c08435ec | 6547 | if (env->insn_idx >= insn_cnt) { |
61bd5218 | 6548 | verbose(env, "invalid insn idx %d insn_cnt %d\n", |
c08435ec | 6549 | env->insn_idx, insn_cnt); |
17a52670 AS |
6550 | return -EFAULT; |
6551 | } | |
6552 | ||
c08435ec | 6553 | insn = &insns[env->insn_idx]; |
17a52670 AS |
6554 | class = BPF_CLASS(insn->code); |
6555 | ||
06ee7115 | 6556 | if (++env->insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) { |
61bd5218 JK |
6557 | verbose(env, |
6558 | "BPF program is too large. Processed %d insn\n", | |
06ee7115 | 6559 | env->insn_processed); |
17a52670 AS |
6560 | return -E2BIG; |
6561 | } | |
6562 | ||
c08435ec | 6563 | err = is_state_visited(env, env->insn_idx); |
f1bca824 AS |
6564 | if (err < 0) |
6565 | return err; | |
6566 | if (err == 1) { | |
6567 | /* found equivalent state, can prune the search */ | |
06ee7115 | 6568 | if (env->log.level & BPF_LOG_LEVEL) { |
f1bca824 | 6569 | if (do_print_state) |
979d63d5 DB |
6570 | verbose(env, "\nfrom %d to %d%s: safe\n", |
6571 | env->prev_insn_idx, env->insn_idx, | |
6572 | env->cur_state->speculative ? | |
6573 | " (speculative execution)" : ""); | |
f1bca824 | 6574 | else |
c08435ec | 6575 | verbose(env, "%d: safe\n", env->insn_idx); |
f1bca824 AS |
6576 | } |
6577 | goto process_bpf_exit; | |
6578 | } | |
6579 | ||
c3494801 AS |
6580 | if (signal_pending(current)) |
6581 | return -EAGAIN; | |
6582 | ||
3c2ce60b DB |
6583 | if (need_resched()) |
6584 | cond_resched(); | |
6585 | ||
06ee7115 AS |
6586 | if (env->log.level & BPF_LOG_LEVEL2 || |
6587 | (env->log.level & BPF_LOG_LEVEL && do_print_state)) { | |
6588 | if (env->log.level & BPF_LOG_LEVEL2) | |
c08435ec | 6589 | verbose(env, "%d:", env->insn_idx); |
c5fc9692 | 6590 | else |
979d63d5 DB |
6591 | verbose(env, "\nfrom %d to %d%s:", |
6592 | env->prev_insn_idx, env->insn_idx, | |
6593 | env->cur_state->speculative ? | |
6594 | " (speculative execution)" : ""); | |
f4d7e40a | 6595 | print_verifier_state(env, state->frame[state->curframe]); |
17a52670 AS |
6596 | do_print_state = false; |
6597 | } | |
6598 | ||
06ee7115 | 6599 | if (env->log.level & BPF_LOG_LEVEL) { |
7105e828 DB |
6600 | const struct bpf_insn_cbs cbs = { |
6601 | .cb_print = verbose, | |
abe08840 | 6602 | .private_data = env, |
7105e828 DB |
6603 | }; |
6604 | ||
c08435ec DB |
6605 | verbose_linfo(env, env->insn_idx, "; "); |
6606 | verbose(env, "%d: ", env->insn_idx); | |
abe08840 | 6607 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); |
17a52670 AS |
6608 | } |
6609 | ||
cae1927c | 6610 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
c08435ec DB |
6611 | err = bpf_prog_offload_verify_insn(env, env->insn_idx, |
6612 | env->prev_insn_idx); | |
cae1927c JK |
6613 | if (err) |
6614 | return err; | |
6615 | } | |
13a27dfc | 6616 | |
638f5b90 | 6617 | regs = cur_regs(env); |
c08435ec | 6618 | env->insn_aux_data[env->insn_idx].seen = true; |
fd978bf7 | 6619 | |
17a52670 | 6620 | if (class == BPF_ALU || class == BPF_ALU64) { |
1be7f75d | 6621 | err = check_alu_op(env, insn); |
17a52670 AS |
6622 | if (err) |
6623 | return err; | |
6624 | ||
6625 | } else if (class == BPF_LDX) { | |
3df126f3 | 6626 | enum bpf_reg_type *prev_src_type, src_reg_type; |
9bac3d6d AS |
6627 | |
6628 | /* check for reserved fields is already done */ | |
6629 | ||
17a52670 | 6630 | /* check src operand */ |
dc503a8a | 6631 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
6632 | if (err) |
6633 | return err; | |
6634 | ||
dc503a8a | 6635 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
17a52670 AS |
6636 | if (err) |
6637 | return err; | |
6638 | ||
725f9dcd AS |
6639 | src_reg_type = regs[insn->src_reg].type; |
6640 | ||
17a52670 AS |
6641 | /* check that memory (src_reg + off) is readable, |
6642 | * the state of dst_reg will be updated by this func | |
6643 | */ | |
c08435ec DB |
6644 | err = check_mem_access(env, env->insn_idx, insn->src_reg, |
6645 | insn->off, BPF_SIZE(insn->code), | |
6646 | BPF_READ, insn->dst_reg, false); | |
17a52670 AS |
6647 | if (err) |
6648 | return err; | |
6649 | ||
c08435ec | 6650 | prev_src_type = &env->insn_aux_data[env->insn_idx].ptr_type; |
3df126f3 JK |
6651 | |
6652 | if (*prev_src_type == NOT_INIT) { | |
9bac3d6d AS |
6653 | /* saw a valid insn |
6654 | * dst_reg = *(u32 *)(src_reg + off) | |
3df126f3 | 6655 | * save type to validate intersecting paths |
9bac3d6d | 6656 | */ |
3df126f3 | 6657 | *prev_src_type = src_reg_type; |
9bac3d6d | 6658 | |
c64b7983 | 6659 | } else if (reg_type_mismatch(src_reg_type, *prev_src_type)) { |
9bac3d6d AS |
6660 | /* ABuser program is trying to use the same insn |
6661 | * dst_reg = *(u32*) (src_reg + off) | |
6662 | * with different pointer types: | |
6663 | * src_reg == ctx in one branch and | |
6664 | * src_reg == stack|map in some other branch. | |
6665 | * Reject it. | |
6666 | */ | |
61bd5218 | 6667 | verbose(env, "same insn cannot be used with different pointers\n"); |
9bac3d6d AS |
6668 | return -EINVAL; |
6669 | } | |
6670 | ||
17a52670 | 6671 | } else if (class == BPF_STX) { |
3df126f3 | 6672 | enum bpf_reg_type *prev_dst_type, dst_reg_type; |
d691f9e8 | 6673 | |
17a52670 | 6674 | if (BPF_MODE(insn->code) == BPF_XADD) { |
c08435ec | 6675 | err = check_xadd(env, env->insn_idx, insn); |
17a52670 AS |
6676 | if (err) |
6677 | return err; | |
c08435ec | 6678 | env->insn_idx++; |
17a52670 AS |
6679 | continue; |
6680 | } | |
6681 | ||
17a52670 | 6682 | /* check src1 operand */ |
dc503a8a | 6683 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
6684 | if (err) |
6685 | return err; | |
6686 | /* check src2 operand */ | |
dc503a8a | 6687 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
6688 | if (err) |
6689 | return err; | |
6690 | ||
d691f9e8 AS |
6691 | dst_reg_type = regs[insn->dst_reg].type; |
6692 | ||
17a52670 | 6693 | /* check that memory (dst_reg + off) is writeable */ |
c08435ec DB |
6694 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
6695 | insn->off, BPF_SIZE(insn->code), | |
6696 | BPF_WRITE, insn->src_reg, false); | |
17a52670 AS |
6697 | if (err) |
6698 | return err; | |
6699 | ||
c08435ec | 6700 | prev_dst_type = &env->insn_aux_data[env->insn_idx].ptr_type; |
3df126f3 JK |
6701 | |
6702 | if (*prev_dst_type == NOT_INIT) { | |
6703 | *prev_dst_type = dst_reg_type; | |
c64b7983 | 6704 | } else if (reg_type_mismatch(dst_reg_type, *prev_dst_type)) { |
61bd5218 | 6705 | verbose(env, "same insn cannot be used with different pointers\n"); |
d691f9e8 AS |
6706 | return -EINVAL; |
6707 | } | |
6708 | ||
17a52670 AS |
6709 | } else if (class == BPF_ST) { |
6710 | if (BPF_MODE(insn->code) != BPF_MEM || | |
6711 | insn->src_reg != BPF_REG_0) { | |
61bd5218 | 6712 | verbose(env, "BPF_ST uses reserved fields\n"); |
17a52670 AS |
6713 | return -EINVAL; |
6714 | } | |
6715 | /* check src operand */ | |
dc503a8a | 6716 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
6717 | if (err) |
6718 | return err; | |
6719 | ||
f37a8cb8 | 6720 | if (is_ctx_reg(env, insn->dst_reg)) { |
9d2be44a | 6721 | verbose(env, "BPF_ST stores into R%d %s is not allowed\n", |
2a159c6f DB |
6722 | insn->dst_reg, |
6723 | reg_type_str[reg_state(env, insn->dst_reg)->type]); | |
f37a8cb8 DB |
6724 | return -EACCES; |
6725 | } | |
6726 | ||
17a52670 | 6727 | /* check that memory (dst_reg + off) is writeable */ |
c08435ec DB |
6728 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
6729 | insn->off, BPF_SIZE(insn->code), | |
6730 | BPF_WRITE, -1, false); | |
17a52670 AS |
6731 | if (err) |
6732 | return err; | |
6733 | ||
092ed096 | 6734 | } else if (class == BPF_JMP || class == BPF_JMP32) { |
17a52670 AS |
6735 | u8 opcode = BPF_OP(insn->code); |
6736 | ||
6737 | if (opcode == BPF_CALL) { | |
6738 | if (BPF_SRC(insn->code) != BPF_K || | |
6739 | insn->off != 0 || | |
f4d7e40a AS |
6740 | (insn->src_reg != BPF_REG_0 && |
6741 | insn->src_reg != BPF_PSEUDO_CALL) || | |
092ed096 JW |
6742 | insn->dst_reg != BPF_REG_0 || |
6743 | class == BPF_JMP32) { | |
61bd5218 | 6744 | verbose(env, "BPF_CALL uses reserved fields\n"); |
17a52670 AS |
6745 | return -EINVAL; |
6746 | } | |
6747 | ||
d83525ca AS |
6748 | if (env->cur_state->active_spin_lock && |
6749 | (insn->src_reg == BPF_PSEUDO_CALL || | |
6750 | insn->imm != BPF_FUNC_spin_unlock)) { | |
6751 | verbose(env, "function calls are not allowed while holding a lock\n"); | |
6752 | return -EINVAL; | |
6753 | } | |
f4d7e40a | 6754 | if (insn->src_reg == BPF_PSEUDO_CALL) |
c08435ec | 6755 | err = check_func_call(env, insn, &env->insn_idx); |
f4d7e40a | 6756 | else |
c08435ec | 6757 | err = check_helper_call(env, insn->imm, env->insn_idx); |
17a52670 AS |
6758 | if (err) |
6759 | return err; | |
6760 | ||
6761 | } else if (opcode == BPF_JA) { | |
6762 | if (BPF_SRC(insn->code) != BPF_K || | |
6763 | insn->imm != 0 || | |
6764 | insn->src_reg != BPF_REG_0 || | |
092ed096 JW |
6765 | insn->dst_reg != BPF_REG_0 || |
6766 | class == BPF_JMP32) { | |
61bd5218 | 6767 | verbose(env, "BPF_JA uses reserved fields\n"); |
17a52670 AS |
6768 | return -EINVAL; |
6769 | } | |
6770 | ||
c08435ec | 6771 | env->insn_idx += insn->off + 1; |
17a52670 AS |
6772 | continue; |
6773 | ||
6774 | } else if (opcode == BPF_EXIT) { | |
6775 | if (BPF_SRC(insn->code) != BPF_K || | |
6776 | insn->imm != 0 || | |
6777 | insn->src_reg != BPF_REG_0 || | |
092ed096 JW |
6778 | insn->dst_reg != BPF_REG_0 || |
6779 | class == BPF_JMP32) { | |
61bd5218 | 6780 | verbose(env, "BPF_EXIT uses reserved fields\n"); |
17a52670 AS |
6781 | return -EINVAL; |
6782 | } | |
6783 | ||
d83525ca AS |
6784 | if (env->cur_state->active_spin_lock) { |
6785 | verbose(env, "bpf_spin_unlock is missing\n"); | |
6786 | return -EINVAL; | |
6787 | } | |
6788 | ||
f4d7e40a AS |
6789 | if (state->curframe) { |
6790 | /* exit from nested function */ | |
c08435ec DB |
6791 | env->prev_insn_idx = env->insn_idx; |
6792 | err = prepare_func_exit(env, &env->insn_idx); | |
f4d7e40a AS |
6793 | if (err) |
6794 | return err; | |
6795 | do_print_state = true; | |
6796 | continue; | |
6797 | } | |
6798 | ||
fd978bf7 JS |
6799 | err = check_reference_leak(env); |
6800 | if (err) | |
6801 | return err; | |
6802 | ||
17a52670 AS |
6803 | /* eBPF calling convetion is such that R0 is used |
6804 | * to return the value from eBPF program. | |
6805 | * Make sure that it's readable at this time | |
6806 | * of bpf_exit, which means that program wrote | |
6807 | * something into it earlier | |
6808 | */ | |
dc503a8a | 6809 | err = check_reg_arg(env, BPF_REG_0, SRC_OP); |
17a52670 AS |
6810 | if (err) |
6811 | return err; | |
6812 | ||
1be7f75d | 6813 | if (is_pointer_value(env, BPF_REG_0)) { |
61bd5218 | 6814 | verbose(env, "R0 leaks addr as return value\n"); |
1be7f75d AS |
6815 | return -EACCES; |
6816 | } | |
6817 | ||
390ee7e2 AS |
6818 | err = check_return_code(env); |
6819 | if (err) | |
6820 | return err; | |
f1bca824 | 6821 | process_bpf_exit: |
c08435ec DB |
6822 | err = pop_stack(env, &env->prev_insn_idx, |
6823 | &env->insn_idx); | |
638f5b90 AS |
6824 | if (err < 0) { |
6825 | if (err != -ENOENT) | |
6826 | return err; | |
17a52670 AS |
6827 | break; |
6828 | } else { | |
6829 | do_print_state = true; | |
6830 | continue; | |
6831 | } | |
6832 | } else { | |
c08435ec | 6833 | err = check_cond_jmp_op(env, insn, &env->insn_idx); |
17a52670 AS |
6834 | if (err) |
6835 | return err; | |
6836 | } | |
6837 | } else if (class == BPF_LD) { | |
6838 | u8 mode = BPF_MODE(insn->code); | |
6839 | ||
6840 | if (mode == BPF_ABS || mode == BPF_IND) { | |
ddd872bc AS |
6841 | err = check_ld_abs(env, insn); |
6842 | if (err) | |
6843 | return err; | |
6844 | ||
17a52670 AS |
6845 | } else if (mode == BPF_IMM) { |
6846 | err = check_ld_imm(env, insn); | |
6847 | if (err) | |
6848 | return err; | |
6849 | ||
c08435ec DB |
6850 | env->insn_idx++; |
6851 | env->insn_aux_data[env->insn_idx].seen = true; | |
17a52670 | 6852 | } else { |
61bd5218 | 6853 | verbose(env, "invalid BPF_LD mode\n"); |
17a52670 AS |
6854 | return -EINVAL; |
6855 | } | |
6856 | } else { | |
61bd5218 | 6857 | verbose(env, "unknown insn class %d\n", class); |
17a52670 AS |
6858 | return -EINVAL; |
6859 | } | |
6860 | ||
c08435ec | 6861 | env->insn_idx++; |
17a52670 AS |
6862 | } |
6863 | ||
9c8105bd | 6864 | env->prog->aux->stack_depth = env->subprog_info[0].stack_depth; |
17a52670 AS |
6865 | return 0; |
6866 | } | |
6867 | ||
56f668df MKL |
6868 | static int check_map_prealloc(struct bpf_map *map) |
6869 | { | |
6870 | return (map->map_type != BPF_MAP_TYPE_HASH && | |
bcc6b1b7 MKL |
6871 | map->map_type != BPF_MAP_TYPE_PERCPU_HASH && |
6872 | map->map_type != BPF_MAP_TYPE_HASH_OF_MAPS) || | |
56f668df MKL |
6873 | !(map->map_flags & BPF_F_NO_PREALLOC); |
6874 | } | |
6875 | ||
d83525ca AS |
6876 | static bool is_tracing_prog_type(enum bpf_prog_type type) |
6877 | { | |
6878 | switch (type) { | |
6879 | case BPF_PROG_TYPE_KPROBE: | |
6880 | case BPF_PROG_TYPE_TRACEPOINT: | |
6881 | case BPF_PROG_TYPE_PERF_EVENT: | |
6882 | case BPF_PROG_TYPE_RAW_TRACEPOINT: | |
6883 | return true; | |
6884 | default: | |
6885 | return false; | |
6886 | } | |
6887 | } | |
6888 | ||
61bd5218 JK |
6889 | static int check_map_prog_compatibility(struct bpf_verifier_env *env, |
6890 | struct bpf_map *map, | |
fdc15d38 AS |
6891 | struct bpf_prog *prog) |
6892 | ||
6893 | { | |
56f668df MKL |
6894 | /* Make sure that BPF_PROG_TYPE_PERF_EVENT programs only use |
6895 | * preallocated hash maps, since doing memory allocation | |
6896 | * in overflow_handler can crash depending on where nmi got | |
6897 | * triggered. | |
6898 | */ | |
6899 | if (prog->type == BPF_PROG_TYPE_PERF_EVENT) { | |
6900 | if (!check_map_prealloc(map)) { | |
61bd5218 | 6901 | verbose(env, "perf_event programs can only use preallocated hash map\n"); |
56f668df MKL |
6902 | return -EINVAL; |
6903 | } | |
6904 | if (map->inner_map_meta && | |
6905 | !check_map_prealloc(map->inner_map_meta)) { | |
61bd5218 | 6906 | verbose(env, "perf_event programs can only use preallocated inner hash map\n"); |
56f668df MKL |
6907 | return -EINVAL; |
6908 | } | |
fdc15d38 | 6909 | } |
a3884572 | 6910 | |
d83525ca AS |
6911 | if ((is_tracing_prog_type(prog->type) || |
6912 | prog->type == BPF_PROG_TYPE_SOCKET_FILTER) && | |
6913 | map_value_has_spin_lock(map)) { | |
6914 | verbose(env, "tracing progs cannot use bpf_spin_lock yet\n"); | |
6915 | return -EINVAL; | |
6916 | } | |
6917 | ||
a3884572 | 6918 | if ((bpf_prog_is_dev_bound(prog->aux) || bpf_map_is_dev_bound(map)) && |
09728266 | 6919 | !bpf_offload_prog_map_match(prog, map)) { |
a3884572 JK |
6920 | verbose(env, "offload device mismatch between prog and map\n"); |
6921 | return -EINVAL; | |
6922 | } | |
6923 | ||
fdc15d38 AS |
6924 | return 0; |
6925 | } | |
6926 | ||
b741f163 RG |
6927 | static bool bpf_map_is_cgroup_storage(struct bpf_map *map) |
6928 | { | |
6929 | return (map->map_type == BPF_MAP_TYPE_CGROUP_STORAGE || | |
6930 | map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE); | |
6931 | } | |
6932 | ||
0246e64d AS |
6933 | /* look for pseudo eBPF instructions that access map FDs and |
6934 | * replace them with actual map pointers | |
6935 | */ | |
58e2af8b | 6936 | static int replace_map_fd_with_map_ptr(struct bpf_verifier_env *env) |
0246e64d AS |
6937 | { |
6938 | struct bpf_insn *insn = env->prog->insnsi; | |
6939 | int insn_cnt = env->prog->len; | |
fdc15d38 | 6940 | int i, j, err; |
0246e64d | 6941 | |
f1f7714e | 6942 | err = bpf_prog_calc_tag(env->prog); |
aafe6ae9 DB |
6943 | if (err) |
6944 | return err; | |
6945 | ||
0246e64d | 6946 | for (i = 0; i < insn_cnt; i++, insn++) { |
9bac3d6d | 6947 | if (BPF_CLASS(insn->code) == BPF_LDX && |
d691f9e8 | 6948 | (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) { |
61bd5218 | 6949 | verbose(env, "BPF_LDX uses reserved fields\n"); |
9bac3d6d AS |
6950 | return -EINVAL; |
6951 | } | |
6952 | ||
d691f9e8 AS |
6953 | if (BPF_CLASS(insn->code) == BPF_STX && |
6954 | ((BPF_MODE(insn->code) != BPF_MEM && | |
6955 | BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) { | |
61bd5218 | 6956 | verbose(env, "BPF_STX uses reserved fields\n"); |
d691f9e8 AS |
6957 | return -EINVAL; |
6958 | } | |
6959 | ||
0246e64d | 6960 | if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) { |
d8eca5bb | 6961 | struct bpf_insn_aux_data *aux; |
0246e64d AS |
6962 | struct bpf_map *map; |
6963 | struct fd f; | |
d8eca5bb | 6964 | u64 addr; |
0246e64d AS |
6965 | |
6966 | if (i == insn_cnt - 1 || insn[1].code != 0 || | |
6967 | insn[1].dst_reg != 0 || insn[1].src_reg != 0 || | |
6968 | insn[1].off != 0) { | |
61bd5218 | 6969 | verbose(env, "invalid bpf_ld_imm64 insn\n"); |
0246e64d AS |
6970 | return -EINVAL; |
6971 | } | |
6972 | ||
d8eca5bb | 6973 | if (insn[0].src_reg == 0) |
0246e64d AS |
6974 | /* valid generic load 64-bit imm */ |
6975 | goto next_insn; | |
6976 | ||
d8eca5bb DB |
6977 | /* In final convert_pseudo_ld_imm64() step, this is |
6978 | * converted into regular 64-bit imm load insn. | |
6979 | */ | |
6980 | if ((insn[0].src_reg != BPF_PSEUDO_MAP_FD && | |
6981 | insn[0].src_reg != BPF_PSEUDO_MAP_VALUE) || | |
6982 | (insn[0].src_reg == BPF_PSEUDO_MAP_FD && | |
6983 | insn[1].imm != 0)) { | |
6984 | verbose(env, | |
6985 | "unrecognized bpf_ld_imm64 insn\n"); | |
0246e64d AS |
6986 | return -EINVAL; |
6987 | } | |
6988 | ||
20182390 | 6989 | f = fdget(insn[0].imm); |
c2101297 | 6990 | map = __bpf_map_get(f); |
0246e64d | 6991 | if (IS_ERR(map)) { |
61bd5218 | 6992 | verbose(env, "fd %d is not pointing to valid bpf_map\n", |
20182390 | 6993 | insn[0].imm); |
0246e64d AS |
6994 | return PTR_ERR(map); |
6995 | } | |
6996 | ||
61bd5218 | 6997 | err = check_map_prog_compatibility(env, map, env->prog); |
fdc15d38 AS |
6998 | if (err) { |
6999 | fdput(f); | |
7000 | return err; | |
7001 | } | |
7002 | ||
d8eca5bb DB |
7003 | aux = &env->insn_aux_data[i]; |
7004 | if (insn->src_reg == BPF_PSEUDO_MAP_FD) { | |
7005 | addr = (unsigned long)map; | |
7006 | } else { | |
7007 | u32 off = insn[1].imm; | |
7008 | ||
7009 | if (off >= BPF_MAX_VAR_OFF) { | |
7010 | verbose(env, "direct value offset of %u is not allowed\n", off); | |
7011 | fdput(f); | |
7012 | return -EINVAL; | |
7013 | } | |
7014 | ||
7015 | if (!map->ops->map_direct_value_addr) { | |
7016 | verbose(env, "no direct value access support for this map type\n"); | |
7017 | fdput(f); | |
7018 | return -EINVAL; | |
7019 | } | |
7020 | ||
7021 | err = map->ops->map_direct_value_addr(map, &addr, off); | |
7022 | if (err) { | |
7023 | verbose(env, "invalid access to map value pointer, value_size=%u off=%u\n", | |
7024 | map->value_size, off); | |
7025 | fdput(f); | |
7026 | return err; | |
7027 | } | |
7028 | ||
7029 | aux->map_off = off; | |
7030 | addr += off; | |
7031 | } | |
7032 | ||
7033 | insn[0].imm = (u32)addr; | |
7034 | insn[1].imm = addr >> 32; | |
0246e64d AS |
7035 | |
7036 | /* check whether we recorded this map already */ | |
d8eca5bb | 7037 | for (j = 0; j < env->used_map_cnt; j++) { |
0246e64d | 7038 | if (env->used_maps[j] == map) { |
d8eca5bb | 7039 | aux->map_index = j; |
0246e64d AS |
7040 | fdput(f); |
7041 | goto next_insn; | |
7042 | } | |
d8eca5bb | 7043 | } |
0246e64d AS |
7044 | |
7045 | if (env->used_map_cnt >= MAX_USED_MAPS) { | |
7046 | fdput(f); | |
7047 | return -E2BIG; | |
7048 | } | |
7049 | ||
0246e64d AS |
7050 | /* hold the map. If the program is rejected by verifier, |
7051 | * the map will be released by release_maps() or it | |
7052 | * will be used by the valid program until it's unloaded | |
ab7f5bf0 | 7053 | * and all maps are released in free_used_maps() |
0246e64d | 7054 | */ |
92117d84 AS |
7055 | map = bpf_map_inc(map, false); |
7056 | if (IS_ERR(map)) { | |
7057 | fdput(f); | |
7058 | return PTR_ERR(map); | |
7059 | } | |
d8eca5bb DB |
7060 | |
7061 | aux->map_index = env->used_map_cnt; | |
92117d84 AS |
7062 | env->used_maps[env->used_map_cnt++] = map; |
7063 | ||
b741f163 | 7064 | if (bpf_map_is_cgroup_storage(map) && |
de9cbbaa | 7065 | bpf_cgroup_storage_assign(env->prog, map)) { |
b741f163 | 7066 | verbose(env, "only one cgroup storage of each type is allowed\n"); |
de9cbbaa RG |
7067 | fdput(f); |
7068 | return -EBUSY; | |
7069 | } | |
7070 | ||
0246e64d AS |
7071 | fdput(f); |
7072 | next_insn: | |
7073 | insn++; | |
7074 | i++; | |
5e581dad DB |
7075 | continue; |
7076 | } | |
7077 | ||
7078 | /* Basic sanity check before we invest more work here. */ | |
7079 | if (!bpf_opcode_in_insntable(insn->code)) { | |
7080 | verbose(env, "unknown opcode %02x\n", insn->code); | |
7081 | return -EINVAL; | |
0246e64d AS |
7082 | } |
7083 | } | |
7084 | ||
7085 | /* now all pseudo BPF_LD_IMM64 instructions load valid | |
7086 | * 'struct bpf_map *' into a register instead of user map_fd. | |
7087 | * These pointers will be used later by verifier to validate map access. | |
7088 | */ | |
7089 | return 0; | |
7090 | } | |
7091 | ||
7092 | /* drop refcnt of maps used by the rejected program */ | |
58e2af8b | 7093 | static void release_maps(struct bpf_verifier_env *env) |
0246e64d | 7094 | { |
8bad74f9 | 7095 | enum bpf_cgroup_storage_type stype; |
0246e64d AS |
7096 | int i; |
7097 | ||
8bad74f9 RG |
7098 | for_each_cgroup_storage_type(stype) { |
7099 | if (!env->prog->aux->cgroup_storage[stype]) | |
7100 | continue; | |
de9cbbaa | 7101 | bpf_cgroup_storage_release(env->prog, |
8bad74f9 RG |
7102 | env->prog->aux->cgroup_storage[stype]); |
7103 | } | |
de9cbbaa | 7104 | |
0246e64d AS |
7105 | for (i = 0; i < env->used_map_cnt; i++) |
7106 | bpf_map_put(env->used_maps[i]); | |
7107 | } | |
7108 | ||
7109 | /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */ | |
58e2af8b | 7110 | static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env) |
0246e64d AS |
7111 | { |
7112 | struct bpf_insn *insn = env->prog->insnsi; | |
7113 | int insn_cnt = env->prog->len; | |
7114 | int i; | |
7115 | ||
7116 | for (i = 0; i < insn_cnt; i++, insn++) | |
7117 | if (insn->code == (BPF_LD | BPF_IMM | BPF_DW)) | |
7118 | insn->src_reg = 0; | |
7119 | } | |
7120 | ||
8041902d AS |
7121 | /* single env->prog->insni[off] instruction was replaced with the range |
7122 | * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying | |
7123 | * [0, off) and [off, end) to new locations, so the patched range stays zero | |
7124 | */ | |
7125 | static int adjust_insn_aux_data(struct bpf_verifier_env *env, u32 prog_len, | |
7126 | u32 off, u32 cnt) | |
7127 | { | |
7128 | struct bpf_insn_aux_data *new_data, *old_data = env->insn_aux_data; | |
c131187d | 7129 | int i; |
8041902d AS |
7130 | |
7131 | if (cnt == 1) | |
7132 | return 0; | |
fad953ce KC |
7133 | new_data = vzalloc(array_size(prog_len, |
7134 | sizeof(struct bpf_insn_aux_data))); | |
8041902d AS |
7135 | if (!new_data) |
7136 | return -ENOMEM; | |
7137 | memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off); | |
7138 | memcpy(new_data + off + cnt - 1, old_data + off, | |
7139 | sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1)); | |
c131187d AS |
7140 | for (i = off; i < off + cnt - 1; i++) |
7141 | new_data[i].seen = true; | |
8041902d AS |
7142 | env->insn_aux_data = new_data; |
7143 | vfree(old_data); | |
7144 | return 0; | |
7145 | } | |
7146 | ||
cc8b0b92 AS |
7147 | static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len) |
7148 | { | |
7149 | int i; | |
7150 | ||
7151 | if (len == 1) | |
7152 | return; | |
4cb3d99c JW |
7153 | /* NOTE: fake 'exit' subprog should be updated as well. */ |
7154 | for (i = 0; i <= env->subprog_cnt; i++) { | |
afd59424 | 7155 | if (env->subprog_info[i].start <= off) |
cc8b0b92 | 7156 | continue; |
9c8105bd | 7157 | env->subprog_info[i].start += len - 1; |
cc8b0b92 AS |
7158 | } |
7159 | } | |
7160 | ||
8041902d AS |
7161 | static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off, |
7162 | const struct bpf_insn *patch, u32 len) | |
7163 | { | |
7164 | struct bpf_prog *new_prog; | |
7165 | ||
7166 | new_prog = bpf_patch_insn_single(env->prog, off, patch, len); | |
4f73379e AS |
7167 | if (IS_ERR(new_prog)) { |
7168 | if (PTR_ERR(new_prog) == -ERANGE) | |
7169 | verbose(env, | |
7170 | "insn %d cannot be patched due to 16-bit range\n", | |
7171 | env->insn_aux_data[off].orig_idx); | |
8041902d | 7172 | return NULL; |
4f73379e | 7173 | } |
8041902d AS |
7174 | if (adjust_insn_aux_data(env, new_prog->len, off, len)) |
7175 | return NULL; | |
cc8b0b92 | 7176 | adjust_subprog_starts(env, off, len); |
8041902d AS |
7177 | return new_prog; |
7178 | } | |
7179 | ||
52875a04 JK |
7180 | static int adjust_subprog_starts_after_remove(struct bpf_verifier_env *env, |
7181 | u32 off, u32 cnt) | |
7182 | { | |
7183 | int i, j; | |
7184 | ||
7185 | /* find first prog starting at or after off (first to remove) */ | |
7186 | for (i = 0; i < env->subprog_cnt; i++) | |
7187 | if (env->subprog_info[i].start >= off) | |
7188 | break; | |
7189 | /* find first prog starting at or after off + cnt (first to stay) */ | |
7190 | for (j = i; j < env->subprog_cnt; j++) | |
7191 | if (env->subprog_info[j].start >= off + cnt) | |
7192 | break; | |
7193 | /* if j doesn't start exactly at off + cnt, we are just removing | |
7194 | * the front of previous prog | |
7195 | */ | |
7196 | if (env->subprog_info[j].start != off + cnt) | |
7197 | j--; | |
7198 | ||
7199 | if (j > i) { | |
7200 | struct bpf_prog_aux *aux = env->prog->aux; | |
7201 | int move; | |
7202 | ||
7203 | /* move fake 'exit' subprog as well */ | |
7204 | move = env->subprog_cnt + 1 - j; | |
7205 | ||
7206 | memmove(env->subprog_info + i, | |
7207 | env->subprog_info + j, | |
7208 | sizeof(*env->subprog_info) * move); | |
7209 | env->subprog_cnt -= j - i; | |
7210 | ||
7211 | /* remove func_info */ | |
7212 | if (aux->func_info) { | |
7213 | move = aux->func_info_cnt - j; | |
7214 | ||
7215 | memmove(aux->func_info + i, | |
7216 | aux->func_info + j, | |
7217 | sizeof(*aux->func_info) * move); | |
7218 | aux->func_info_cnt -= j - i; | |
7219 | /* func_info->insn_off is set after all code rewrites, | |
7220 | * in adjust_btf_func() - no need to adjust | |
7221 | */ | |
7222 | } | |
7223 | } else { | |
7224 | /* convert i from "first prog to remove" to "first to adjust" */ | |
7225 | if (env->subprog_info[i].start == off) | |
7226 | i++; | |
7227 | } | |
7228 | ||
7229 | /* update fake 'exit' subprog as well */ | |
7230 | for (; i <= env->subprog_cnt; i++) | |
7231 | env->subprog_info[i].start -= cnt; | |
7232 | ||
7233 | return 0; | |
7234 | } | |
7235 | ||
7236 | static int bpf_adj_linfo_after_remove(struct bpf_verifier_env *env, u32 off, | |
7237 | u32 cnt) | |
7238 | { | |
7239 | struct bpf_prog *prog = env->prog; | |
7240 | u32 i, l_off, l_cnt, nr_linfo; | |
7241 | struct bpf_line_info *linfo; | |
7242 | ||
7243 | nr_linfo = prog->aux->nr_linfo; | |
7244 | if (!nr_linfo) | |
7245 | return 0; | |
7246 | ||
7247 | linfo = prog->aux->linfo; | |
7248 | ||
7249 | /* find first line info to remove, count lines to be removed */ | |
7250 | for (i = 0; i < nr_linfo; i++) | |
7251 | if (linfo[i].insn_off >= off) | |
7252 | break; | |
7253 | ||
7254 | l_off = i; | |
7255 | l_cnt = 0; | |
7256 | for (; i < nr_linfo; i++) | |
7257 | if (linfo[i].insn_off < off + cnt) | |
7258 | l_cnt++; | |
7259 | else | |
7260 | break; | |
7261 | ||
7262 | /* First live insn doesn't match first live linfo, it needs to "inherit" | |
7263 | * last removed linfo. prog is already modified, so prog->len == off | |
7264 | * means no live instructions after (tail of the program was removed). | |
7265 | */ | |
7266 | if (prog->len != off && l_cnt && | |
7267 | (i == nr_linfo || linfo[i].insn_off != off + cnt)) { | |
7268 | l_cnt--; | |
7269 | linfo[--i].insn_off = off + cnt; | |
7270 | } | |
7271 | ||
7272 | /* remove the line info which refer to the removed instructions */ | |
7273 | if (l_cnt) { | |
7274 | memmove(linfo + l_off, linfo + i, | |
7275 | sizeof(*linfo) * (nr_linfo - i)); | |
7276 | ||
7277 | prog->aux->nr_linfo -= l_cnt; | |
7278 | nr_linfo = prog->aux->nr_linfo; | |
7279 | } | |
7280 | ||
7281 | /* pull all linfo[i].insn_off >= off + cnt in by cnt */ | |
7282 | for (i = l_off; i < nr_linfo; i++) | |
7283 | linfo[i].insn_off -= cnt; | |
7284 | ||
7285 | /* fix up all subprogs (incl. 'exit') which start >= off */ | |
7286 | for (i = 0; i <= env->subprog_cnt; i++) | |
7287 | if (env->subprog_info[i].linfo_idx > l_off) { | |
7288 | /* program may have started in the removed region but | |
7289 | * may not be fully removed | |
7290 | */ | |
7291 | if (env->subprog_info[i].linfo_idx >= l_off + l_cnt) | |
7292 | env->subprog_info[i].linfo_idx -= l_cnt; | |
7293 | else | |
7294 | env->subprog_info[i].linfo_idx = l_off; | |
7295 | } | |
7296 | ||
7297 | return 0; | |
7298 | } | |
7299 | ||
7300 | static int verifier_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt) | |
7301 | { | |
7302 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
7303 | unsigned int orig_prog_len = env->prog->len; | |
7304 | int err; | |
7305 | ||
08ca90af JK |
7306 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
7307 | bpf_prog_offload_remove_insns(env, off, cnt); | |
7308 | ||
52875a04 JK |
7309 | err = bpf_remove_insns(env->prog, off, cnt); |
7310 | if (err) | |
7311 | return err; | |
7312 | ||
7313 | err = adjust_subprog_starts_after_remove(env, off, cnt); | |
7314 | if (err) | |
7315 | return err; | |
7316 | ||
7317 | err = bpf_adj_linfo_after_remove(env, off, cnt); | |
7318 | if (err) | |
7319 | return err; | |
7320 | ||
7321 | memmove(aux_data + off, aux_data + off + cnt, | |
7322 | sizeof(*aux_data) * (orig_prog_len - off - cnt)); | |
7323 | ||
7324 | return 0; | |
7325 | } | |
7326 | ||
2a5418a1 DB |
7327 | /* The verifier does more data flow analysis than llvm and will not |
7328 | * explore branches that are dead at run time. Malicious programs can | |
7329 | * have dead code too. Therefore replace all dead at-run-time code | |
7330 | * with 'ja -1'. | |
7331 | * | |
7332 | * Just nops are not optimal, e.g. if they would sit at the end of the | |
7333 | * program and through another bug we would manage to jump there, then | |
7334 | * we'd execute beyond program memory otherwise. Returning exception | |
7335 | * code also wouldn't work since we can have subprogs where the dead | |
7336 | * code could be located. | |
c131187d AS |
7337 | */ |
7338 | static void sanitize_dead_code(struct bpf_verifier_env *env) | |
7339 | { | |
7340 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
2a5418a1 | 7341 | struct bpf_insn trap = BPF_JMP_IMM(BPF_JA, 0, 0, -1); |
c131187d AS |
7342 | struct bpf_insn *insn = env->prog->insnsi; |
7343 | const int insn_cnt = env->prog->len; | |
7344 | int i; | |
7345 | ||
7346 | for (i = 0; i < insn_cnt; i++) { | |
7347 | if (aux_data[i].seen) | |
7348 | continue; | |
2a5418a1 | 7349 | memcpy(insn + i, &trap, sizeof(trap)); |
c131187d AS |
7350 | } |
7351 | } | |
7352 | ||
e2ae4ca2 JK |
7353 | static bool insn_is_cond_jump(u8 code) |
7354 | { | |
7355 | u8 op; | |
7356 | ||
092ed096 JW |
7357 | if (BPF_CLASS(code) == BPF_JMP32) |
7358 | return true; | |
7359 | ||
e2ae4ca2 JK |
7360 | if (BPF_CLASS(code) != BPF_JMP) |
7361 | return false; | |
7362 | ||
7363 | op = BPF_OP(code); | |
7364 | return op != BPF_JA && op != BPF_EXIT && op != BPF_CALL; | |
7365 | } | |
7366 | ||
7367 | static void opt_hard_wire_dead_code_branches(struct bpf_verifier_env *env) | |
7368 | { | |
7369 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
7370 | struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); | |
7371 | struct bpf_insn *insn = env->prog->insnsi; | |
7372 | const int insn_cnt = env->prog->len; | |
7373 | int i; | |
7374 | ||
7375 | for (i = 0; i < insn_cnt; i++, insn++) { | |
7376 | if (!insn_is_cond_jump(insn->code)) | |
7377 | continue; | |
7378 | ||
7379 | if (!aux_data[i + 1].seen) | |
7380 | ja.off = insn->off; | |
7381 | else if (!aux_data[i + 1 + insn->off].seen) | |
7382 | ja.off = 0; | |
7383 | else | |
7384 | continue; | |
7385 | ||
08ca90af JK |
7386 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
7387 | bpf_prog_offload_replace_insn(env, i, &ja); | |
7388 | ||
e2ae4ca2 JK |
7389 | memcpy(insn, &ja, sizeof(ja)); |
7390 | } | |
7391 | } | |
7392 | ||
52875a04 JK |
7393 | static int opt_remove_dead_code(struct bpf_verifier_env *env) |
7394 | { | |
7395 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
7396 | int insn_cnt = env->prog->len; | |
7397 | int i, err; | |
7398 | ||
7399 | for (i = 0; i < insn_cnt; i++) { | |
7400 | int j; | |
7401 | ||
7402 | j = 0; | |
7403 | while (i + j < insn_cnt && !aux_data[i + j].seen) | |
7404 | j++; | |
7405 | if (!j) | |
7406 | continue; | |
7407 | ||
7408 | err = verifier_remove_insns(env, i, j); | |
7409 | if (err) | |
7410 | return err; | |
7411 | insn_cnt = env->prog->len; | |
7412 | } | |
7413 | ||
7414 | return 0; | |
7415 | } | |
7416 | ||
a1b14abc JK |
7417 | static int opt_remove_nops(struct bpf_verifier_env *env) |
7418 | { | |
7419 | const struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); | |
7420 | struct bpf_insn *insn = env->prog->insnsi; | |
7421 | int insn_cnt = env->prog->len; | |
7422 | int i, err; | |
7423 | ||
7424 | for (i = 0; i < insn_cnt; i++) { | |
7425 | if (memcmp(&insn[i], &ja, sizeof(ja))) | |
7426 | continue; | |
7427 | ||
7428 | err = verifier_remove_insns(env, i, 1); | |
7429 | if (err) | |
7430 | return err; | |
7431 | insn_cnt--; | |
7432 | i--; | |
7433 | } | |
7434 | ||
7435 | return 0; | |
7436 | } | |
7437 | ||
c64b7983 JS |
7438 | /* convert load instructions that access fields of a context type into a |
7439 | * sequence of instructions that access fields of the underlying structure: | |
7440 | * struct __sk_buff -> struct sk_buff | |
7441 | * struct bpf_sock_ops -> struct sock | |
9bac3d6d | 7442 | */ |
58e2af8b | 7443 | static int convert_ctx_accesses(struct bpf_verifier_env *env) |
9bac3d6d | 7444 | { |
00176a34 | 7445 | const struct bpf_verifier_ops *ops = env->ops; |
f96da094 | 7446 | int i, cnt, size, ctx_field_size, delta = 0; |
3df126f3 | 7447 | const int insn_cnt = env->prog->len; |
36bbef52 | 7448 | struct bpf_insn insn_buf[16], *insn; |
46f53a65 | 7449 | u32 target_size, size_default, off; |
9bac3d6d | 7450 | struct bpf_prog *new_prog; |
d691f9e8 | 7451 | enum bpf_access_type type; |
f96da094 | 7452 | bool is_narrower_load; |
9bac3d6d | 7453 | |
b09928b9 DB |
7454 | if (ops->gen_prologue || env->seen_direct_write) { |
7455 | if (!ops->gen_prologue) { | |
7456 | verbose(env, "bpf verifier is misconfigured\n"); | |
7457 | return -EINVAL; | |
7458 | } | |
36bbef52 DB |
7459 | cnt = ops->gen_prologue(insn_buf, env->seen_direct_write, |
7460 | env->prog); | |
7461 | if (cnt >= ARRAY_SIZE(insn_buf)) { | |
61bd5218 | 7462 | verbose(env, "bpf verifier is misconfigured\n"); |
36bbef52 DB |
7463 | return -EINVAL; |
7464 | } else if (cnt) { | |
8041902d | 7465 | new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt); |
36bbef52 DB |
7466 | if (!new_prog) |
7467 | return -ENOMEM; | |
8041902d | 7468 | |
36bbef52 | 7469 | env->prog = new_prog; |
3df126f3 | 7470 | delta += cnt - 1; |
36bbef52 DB |
7471 | } |
7472 | } | |
7473 | ||
c64b7983 | 7474 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
9bac3d6d AS |
7475 | return 0; |
7476 | ||
3df126f3 | 7477 | insn = env->prog->insnsi + delta; |
36bbef52 | 7478 | |
9bac3d6d | 7479 | for (i = 0; i < insn_cnt; i++, insn++) { |
c64b7983 JS |
7480 | bpf_convert_ctx_access_t convert_ctx_access; |
7481 | ||
62c7989b DB |
7482 | if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) || |
7483 | insn->code == (BPF_LDX | BPF_MEM | BPF_H) || | |
7484 | insn->code == (BPF_LDX | BPF_MEM | BPF_W) || | |
ea2e7ce5 | 7485 | insn->code == (BPF_LDX | BPF_MEM | BPF_DW)) |
d691f9e8 | 7486 | type = BPF_READ; |
62c7989b DB |
7487 | else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) || |
7488 | insn->code == (BPF_STX | BPF_MEM | BPF_H) || | |
7489 | insn->code == (BPF_STX | BPF_MEM | BPF_W) || | |
ea2e7ce5 | 7490 | insn->code == (BPF_STX | BPF_MEM | BPF_DW)) |
d691f9e8 AS |
7491 | type = BPF_WRITE; |
7492 | else | |
9bac3d6d AS |
7493 | continue; |
7494 | ||
af86ca4e AS |
7495 | if (type == BPF_WRITE && |
7496 | env->insn_aux_data[i + delta].sanitize_stack_off) { | |
7497 | struct bpf_insn patch[] = { | |
7498 | /* Sanitize suspicious stack slot with zero. | |
7499 | * There are no memory dependencies for this store, | |
7500 | * since it's only using frame pointer and immediate | |
7501 | * constant of zero | |
7502 | */ | |
7503 | BPF_ST_MEM(BPF_DW, BPF_REG_FP, | |
7504 | env->insn_aux_data[i + delta].sanitize_stack_off, | |
7505 | 0), | |
7506 | /* the original STX instruction will immediately | |
7507 | * overwrite the same stack slot with appropriate value | |
7508 | */ | |
7509 | *insn, | |
7510 | }; | |
7511 | ||
7512 | cnt = ARRAY_SIZE(patch); | |
7513 | new_prog = bpf_patch_insn_data(env, i + delta, patch, cnt); | |
7514 | if (!new_prog) | |
7515 | return -ENOMEM; | |
7516 | ||
7517 | delta += cnt - 1; | |
7518 | env->prog = new_prog; | |
7519 | insn = new_prog->insnsi + i + delta; | |
7520 | continue; | |
7521 | } | |
7522 | ||
c64b7983 JS |
7523 | switch (env->insn_aux_data[i + delta].ptr_type) { |
7524 | case PTR_TO_CTX: | |
7525 | if (!ops->convert_ctx_access) | |
7526 | continue; | |
7527 | convert_ctx_access = ops->convert_ctx_access; | |
7528 | break; | |
7529 | case PTR_TO_SOCKET: | |
46f8bc92 | 7530 | case PTR_TO_SOCK_COMMON: |
c64b7983 JS |
7531 | convert_ctx_access = bpf_sock_convert_ctx_access; |
7532 | break; | |
655a51e5 MKL |
7533 | case PTR_TO_TCP_SOCK: |
7534 | convert_ctx_access = bpf_tcp_sock_convert_ctx_access; | |
7535 | break; | |
c64b7983 | 7536 | default: |
9bac3d6d | 7537 | continue; |
c64b7983 | 7538 | } |
9bac3d6d | 7539 | |
31fd8581 | 7540 | ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size; |
f96da094 | 7541 | size = BPF_LDST_BYTES(insn); |
31fd8581 YS |
7542 | |
7543 | /* If the read access is a narrower load of the field, | |
7544 | * convert to a 4/8-byte load, to minimum program type specific | |
7545 | * convert_ctx_access changes. If conversion is successful, | |
7546 | * we will apply proper mask to the result. | |
7547 | */ | |
f96da094 | 7548 | is_narrower_load = size < ctx_field_size; |
46f53a65 AI |
7549 | size_default = bpf_ctx_off_adjust_machine(ctx_field_size); |
7550 | off = insn->off; | |
31fd8581 | 7551 | if (is_narrower_load) { |
f96da094 DB |
7552 | u8 size_code; |
7553 | ||
7554 | if (type == BPF_WRITE) { | |
61bd5218 | 7555 | verbose(env, "bpf verifier narrow ctx access misconfigured\n"); |
f96da094 DB |
7556 | return -EINVAL; |
7557 | } | |
31fd8581 | 7558 | |
f96da094 | 7559 | size_code = BPF_H; |
31fd8581 YS |
7560 | if (ctx_field_size == 4) |
7561 | size_code = BPF_W; | |
7562 | else if (ctx_field_size == 8) | |
7563 | size_code = BPF_DW; | |
f96da094 | 7564 | |
bc23105c | 7565 | insn->off = off & ~(size_default - 1); |
31fd8581 YS |
7566 | insn->code = BPF_LDX | BPF_MEM | size_code; |
7567 | } | |
f96da094 DB |
7568 | |
7569 | target_size = 0; | |
c64b7983 JS |
7570 | cnt = convert_ctx_access(type, insn, insn_buf, env->prog, |
7571 | &target_size); | |
f96da094 DB |
7572 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) || |
7573 | (ctx_field_size && !target_size)) { | |
61bd5218 | 7574 | verbose(env, "bpf verifier is misconfigured\n"); |
9bac3d6d AS |
7575 | return -EINVAL; |
7576 | } | |
f96da094 DB |
7577 | |
7578 | if (is_narrower_load && size < target_size) { | |
46f53a65 AI |
7579 | u8 shift = (off & (size_default - 1)) * 8; |
7580 | ||
7581 | if (ctx_field_size <= 4) { | |
7582 | if (shift) | |
7583 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_RSH, | |
7584 | insn->dst_reg, | |
7585 | shift); | |
31fd8581 | 7586 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg, |
f96da094 | 7587 | (1 << size * 8) - 1); |
46f53a65 AI |
7588 | } else { |
7589 | if (shift) | |
7590 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_RSH, | |
7591 | insn->dst_reg, | |
7592 | shift); | |
31fd8581 | 7593 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg, |
e2f7fc0a | 7594 | (1ULL << size * 8) - 1); |
46f53a65 | 7595 | } |
31fd8581 | 7596 | } |
9bac3d6d | 7597 | |
8041902d | 7598 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
9bac3d6d AS |
7599 | if (!new_prog) |
7600 | return -ENOMEM; | |
7601 | ||
3df126f3 | 7602 | delta += cnt - 1; |
9bac3d6d AS |
7603 | |
7604 | /* keep walking new program and skip insns we just inserted */ | |
7605 | env->prog = new_prog; | |
3df126f3 | 7606 | insn = new_prog->insnsi + i + delta; |
9bac3d6d AS |
7607 | } |
7608 | ||
7609 | return 0; | |
7610 | } | |
7611 | ||
1c2a088a AS |
7612 | static int jit_subprogs(struct bpf_verifier_env *env) |
7613 | { | |
7614 | struct bpf_prog *prog = env->prog, **func, *tmp; | |
7615 | int i, j, subprog_start, subprog_end = 0, len, subprog; | |
7105e828 | 7616 | struct bpf_insn *insn; |
1c2a088a | 7617 | void *old_bpf_func; |
c454a46b | 7618 | int err; |
1c2a088a | 7619 | |
f910cefa | 7620 | if (env->subprog_cnt <= 1) |
1c2a088a AS |
7621 | return 0; |
7622 | ||
7105e828 | 7623 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
1c2a088a AS |
7624 | if (insn->code != (BPF_JMP | BPF_CALL) || |
7625 | insn->src_reg != BPF_PSEUDO_CALL) | |
7626 | continue; | |
c7a89784 DB |
7627 | /* Upon error here we cannot fall back to interpreter but |
7628 | * need a hard reject of the program. Thus -EFAULT is | |
7629 | * propagated in any case. | |
7630 | */ | |
1c2a088a AS |
7631 | subprog = find_subprog(env, i + insn->imm + 1); |
7632 | if (subprog < 0) { | |
7633 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", | |
7634 | i + insn->imm + 1); | |
7635 | return -EFAULT; | |
7636 | } | |
7637 | /* temporarily remember subprog id inside insn instead of | |
7638 | * aux_data, since next loop will split up all insns into funcs | |
7639 | */ | |
f910cefa | 7640 | insn->off = subprog; |
1c2a088a AS |
7641 | /* remember original imm in case JIT fails and fallback |
7642 | * to interpreter will be needed | |
7643 | */ | |
7644 | env->insn_aux_data[i].call_imm = insn->imm; | |
7645 | /* point imm to __bpf_call_base+1 from JITs point of view */ | |
7646 | insn->imm = 1; | |
7647 | } | |
7648 | ||
c454a46b MKL |
7649 | err = bpf_prog_alloc_jited_linfo(prog); |
7650 | if (err) | |
7651 | goto out_undo_insn; | |
7652 | ||
7653 | err = -ENOMEM; | |
6396bb22 | 7654 | func = kcalloc(env->subprog_cnt, sizeof(prog), GFP_KERNEL); |
1c2a088a | 7655 | if (!func) |
c7a89784 | 7656 | goto out_undo_insn; |
1c2a088a | 7657 | |
f910cefa | 7658 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a | 7659 | subprog_start = subprog_end; |
4cb3d99c | 7660 | subprog_end = env->subprog_info[i + 1].start; |
1c2a088a AS |
7661 | |
7662 | len = subprog_end - subprog_start; | |
492ecee8 AS |
7663 | /* BPF_PROG_RUN doesn't call subprogs directly, |
7664 | * hence main prog stats include the runtime of subprogs. | |
7665 | * subprogs don't have IDs and not reachable via prog_get_next_id | |
7666 | * func[i]->aux->stats will never be accessed and stays NULL | |
7667 | */ | |
7668 | func[i] = bpf_prog_alloc_no_stats(bpf_prog_size(len), GFP_USER); | |
1c2a088a AS |
7669 | if (!func[i]) |
7670 | goto out_free; | |
7671 | memcpy(func[i]->insnsi, &prog->insnsi[subprog_start], | |
7672 | len * sizeof(struct bpf_insn)); | |
4f74d809 | 7673 | func[i]->type = prog->type; |
1c2a088a | 7674 | func[i]->len = len; |
4f74d809 DB |
7675 | if (bpf_prog_calc_tag(func[i])) |
7676 | goto out_free; | |
1c2a088a | 7677 | func[i]->is_func = 1; |
ba64e7d8 YS |
7678 | func[i]->aux->func_idx = i; |
7679 | /* the btf and func_info will be freed only at prog->aux */ | |
7680 | func[i]->aux->btf = prog->aux->btf; | |
7681 | func[i]->aux->func_info = prog->aux->func_info; | |
7682 | ||
1c2a088a AS |
7683 | /* Use bpf_prog_F_tag to indicate functions in stack traces. |
7684 | * Long term would need debug info to populate names | |
7685 | */ | |
7686 | func[i]->aux->name[0] = 'F'; | |
9c8105bd | 7687 | func[i]->aux->stack_depth = env->subprog_info[i].stack_depth; |
1c2a088a | 7688 | func[i]->jit_requested = 1; |
c454a46b MKL |
7689 | func[i]->aux->linfo = prog->aux->linfo; |
7690 | func[i]->aux->nr_linfo = prog->aux->nr_linfo; | |
7691 | func[i]->aux->jited_linfo = prog->aux->jited_linfo; | |
7692 | func[i]->aux->linfo_idx = env->subprog_info[i].linfo_idx; | |
1c2a088a AS |
7693 | func[i] = bpf_int_jit_compile(func[i]); |
7694 | if (!func[i]->jited) { | |
7695 | err = -ENOTSUPP; | |
7696 | goto out_free; | |
7697 | } | |
7698 | cond_resched(); | |
7699 | } | |
7700 | /* at this point all bpf functions were successfully JITed | |
7701 | * now populate all bpf_calls with correct addresses and | |
7702 | * run last pass of JIT | |
7703 | */ | |
f910cefa | 7704 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a AS |
7705 | insn = func[i]->insnsi; |
7706 | for (j = 0; j < func[i]->len; j++, insn++) { | |
7707 | if (insn->code != (BPF_JMP | BPF_CALL) || | |
7708 | insn->src_reg != BPF_PSEUDO_CALL) | |
7709 | continue; | |
7710 | subprog = insn->off; | |
0d306c31 PB |
7711 | insn->imm = BPF_CAST_CALL(func[subprog]->bpf_func) - |
7712 | __bpf_call_base; | |
1c2a088a | 7713 | } |
2162fed4 SD |
7714 | |
7715 | /* we use the aux data to keep a list of the start addresses | |
7716 | * of the JITed images for each function in the program | |
7717 | * | |
7718 | * for some architectures, such as powerpc64, the imm field | |
7719 | * might not be large enough to hold the offset of the start | |
7720 | * address of the callee's JITed image from __bpf_call_base | |
7721 | * | |
7722 | * in such cases, we can lookup the start address of a callee | |
7723 | * by using its subprog id, available from the off field of | |
7724 | * the call instruction, as an index for this list | |
7725 | */ | |
7726 | func[i]->aux->func = func; | |
7727 | func[i]->aux->func_cnt = env->subprog_cnt; | |
1c2a088a | 7728 | } |
f910cefa | 7729 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a AS |
7730 | old_bpf_func = func[i]->bpf_func; |
7731 | tmp = bpf_int_jit_compile(func[i]); | |
7732 | if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) { | |
7733 | verbose(env, "JIT doesn't support bpf-to-bpf calls\n"); | |
c7a89784 | 7734 | err = -ENOTSUPP; |
1c2a088a AS |
7735 | goto out_free; |
7736 | } | |
7737 | cond_resched(); | |
7738 | } | |
7739 | ||
7740 | /* finally lock prog and jit images for all functions and | |
7741 | * populate kallsysm | |
7742 | */ | |
f910cefa | 7743 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a AS |
7744 | bpf_prog_lock_ro(func[i]); |
7745 | bpf_prog_kallsyms_add(func[i]); | |
7746 | } | |
7105e828 DB |
7747 | |
7748 | /* Last step: make now unused interpreter insns from main | |
7749 | * prog consistent for later dump requests, so they can | |
7750 | * later look the same as if they were interpreted only. | |
7751 | */ | |
7752 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { | |
7105e828 DB |
7753 | if (insn->code != (BPF_JMP | BPF_CALL) || |
7754 | insn->src_reg != BPF_PSEUDO_CALL) | |
7755 | continue; | |
7756 | insn->off = env->insn_aux_data[i].call_imm; | |
7757 | subprog = find_subprog(env, i + insn->off + 1); | |
dbecd738 | 7758 | insn->imm = subprog; |
7105e828 DB |
7759 | } |
7760 | ||
1c2a088a AS |
7761 | prog->jited = 1; |
7762 | prog->bpf_func = func[0]->bpf_func; | |
7763 | prog->aux->func = func; | |
f910cefa | 7764 | prog->aux->func_cnt = env->subprog_cnt; |
c454a46b | 7765 | bpf_prog_free_unused_jited_linfo(prog); |
1c2a088a AS |
7766 | return 0; |
7767 | out_free: | |
f910cefa | 7768 | for (i = 0; i < env->subprog_cnt; i++) |
1c2a088a AS |
7769 | if (func[i]) |
7770 | bpf_jit_free(func[i]); | |
7771 | kfree(func); | |
c7a89784 | 7772 | out_undo_insn: |
1c2a088a AS |
7773 | /* cleanup main prog to be interpreted */ |
7774 | prog->jit_requested = 0; | |
7775 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { | |
7776 | if (insn->code != (BPF_JMP | BPF_CALL) || | |
7777 | insn->src_reg != BPF_PSEUDO_CALL) | |
7778 | continue; | |
7779 | insn->off = 0; | |
7780 | insn->imm = env->insn_aux_data[i].call_imm; | |
7781 | } | |
c454a46b | 7782 | bpf_prog_free_jited_linfo(prog); |
1c2a088a AS |
7783 | return err; |
7784 | } | |
7785 | ||
1ea47e01 AS |
7786 | static int fixup_call_args(struct bpf_verifier_env *env) |
7787 | { | |
19d28fbd | 7788 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
1ea47e01 AS |
7789 | struct bpf_prog *prog = env->prog; |
7790 | struct bpf_insn *insn = prog->insnsi; | |
7791 | int i, depth; | |
19d28fbd | 7792 | #endif |
e4052d06 | 7793 | int err = 0; |
1ea47e01 | 7794 | |
e4052d06 QM |
7795 | if (env->prog->jit_requested && |
7796 | !bpf_prog_is_dev_bound(env->prog->aux)) { | |
19d28fbd DM |
7797 | err = jit_subprogs(env); |
7798 | if (err == 0) | |
1c2a088a | 7799 | return 0; |
c7a89784 DB |
7800 | if (err == -EFAULT) |
7801 | return err; | |
19d28fbd DM |
7802 | } |
7803 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON | |
1ea47e01 AS |
7804 | for (i = 0; i < prog->len; i++, insn++) { |
7805 | if (insn->code != (BPF_JMP | BPF_CALL) || | |
7806 | insn->src_reg != BPF_PSEUDO_CALL) | |
7807 | continue; | |
7808 | depth = get_callee_stack_depth(env, insn, i); | |
7809 | if (depth < 0) | |
7810 | return depth; | |
7811 | bpf_patch_call_args(insn, depth); | |
7812 | } | |
19d28fbd DM |
7813 | err = 0; |
7814 | #endif | |
7815 | return err; | |
1ea47e01 AS |
7816 | } |
7817 | ||
79741b3b | 7818 | /* fixup insn->imm field of bpf_call instructions |
81ed18ab | 7819 | * and inline eligible helpers as explicit sequence of BPF instructions |
e245c5c6 AS |
7820 | * |
7821 | * this function is called after eBPF program passed verification | |
7822 | */ | |
79741b3b | 7823 | static int fixup_bpf_calls(struct bpf_verifier_env *env) |
e245c5c6 | 7824 | { |
79741b3b AS |
7825 | struct bpf_prog *prog = env->prog; |
7826 | struct bpf_insn *insn = prog->insnsi; | |
e245c5c6 | 7827 | const struct bpf_func_proto *fn; |
79741b3b | 7828 | const int insn_cnt = prog->len; |
09772d92 | 7829 | const struct bpf_map_ops *ops; |
c93552c4 | 7830 | struct bpf_insn_aux_data *aux; |
81ed18ab AS |
7831 | struct bpf_insn insn_buf[16]; |
7832 | struct bpf_prog *new_prog; | |
7833 | struct bpf_map *map_ptr; | |
7834 | int i, cnt, delta = 0; | |
e245c5c6 | 7835 | |
79741b3b | 7836 | for (i = 0; i < insn_cnt; i++, insn++) { |
f6b1b3bf DB |
7837 | if (insn->code == (BPF_ALU64 | BPF_MOD | BPF_X) || |
7838 | insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) || | |
7839 | insn->code == (BPF_ALU | BPF_MOD | BPF_X) || | |
68fda450 | 7840 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { |
f6b1b3bf DB |
7841 | bool is64 = BPF_CLASS(insn->code) == BPF_ALU64; |
7842 | struct bpf_insn mask_and_div[] = { | |
7843 | BPF_MOV32_REG(insn->src_reg, insn->src_reg), | |
7844 | /* Rx div 0 -> 0 */ | |
7845 | BPF_JMP_IMM(BPF_JNE, insn->src_reg, 0, 2), | |
7846 | BPF_ALU32_REG(BPF_XOR, insn->dst_reg, insn->dst_reg), | |
7847 | BPF_JMP_IMM(BPF_JA, 0, 0, 1), | |
7848 | *insn, | |
7849 | }; | |
7850 | struct bpf_insn mask_and_mod[] = { | |
7851 | BPF_MOV32_REG(insn->src_reg, insn->src_reg), | |
7852 | /* Rx mod 0 -> Rx */ | |
7853 | BPF_JMP_IMM(BPF_JEQ, insn->src_reg, 0, 1), | |
7854 | *insn, | |
7855 | }; | |
7856 | struct bpf_insn *patchlet; | |
7857 | ||
7858 | if (insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) || | |
7859 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { | |
7860 | patchlet = mask_and_div + (is64 ? 1 : 0); | |
7861 | cnt = ARRAY_SIZE(mask_and_div) - (is64 ? 1 : 0); | |
7862 | } else { | |
7863 | patchlet = mask_and_mod + (is64 ? 1 : 0); | |
7864 | cnt = ARRAY_SIZE(mask_and_mod) - (is64 ? 1 : 0); | |
7865 | } | |
7866 | ||
7867 | new_prog = bpf_patch_insn_data(env, i + delta, patchlet, cnt); | |
68fda450 AS |
7868 | if (!new_prog) |
7869 | return -ENOMEM; | |
7870 | ||
7871 | delta += cnt - 1; | |
7872 | env->prog = prog = new_prog; | |
7873 | insn = new_prog->insnsi + i + delta; | |
7874 | continue; | |
7875 | } | |
7876 | ||
e0cea7ce DB |
7877 | if (BPF_CLASS(insn->code) == BPF_LD && |
7878 | (BPF_MODE(insn->code) == BPF_ABS || | |
7879 | BPF_MODE(insn->code) == BPF_IND)) { | |
7880 | cnt = env->ops->gen_ld_abs(insn, insn_buf); | |
7881 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { | |
7882 | verbose(env, "bpf verifier is misconfigured\n"); | |
7883 | return -EINVAL; | |
7884 | } | |
7885 | ||
7886 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
7887 | if (!new_prog) | |
7888 | return -ENOMEM; | |
7889 | ||
7890 | delta += cnt - 1; | |
7891 | env->prog = prog = new_prog; | |
7892 | insn = new_prog->insnsi + i + delta; | |
7893 | continue; | |
7894 | } | |
7895 | ||
979d63d5 DB |
7896 | if (insn->code == (BPF_ALU64 | BPF_ADD | BPF_X) || |
7897 | insn->code == (BPF_ALU64 | BPF_SUB | BPF_X)) { | |
7898 | const u8 code_add = BPF_ALU64 | BPF_ADD | BPF_X; | |
7899 | const u8 code_sub = BPF_ALU64 | BPF_SUB | BPF_X; | |
7900 | struct bpf_insn insn_buf[16]; | |
7901 | struct bpf_insn *patch = &insn_buf[0]; | |
7902 | bool issrc, isneg; | |
7903 | u32 off_reg; | |
7904 | ||
7905 | aux = &env->insn_aux_data[i + delta]; | |
3612af78 DB |
7906 | if (!aux->alu_state || |
7907 | aux->alu_state == BPF_ALU_NON_POINTER) | |
979d63d5 DB |
7908 | continue; |
7909 | ||
7910 | isneg = aux->alu_state & BPF_ALU_NEG_VALUE; | |
7911 | issrc = (aux->alu_state & BPF_ALU_SANITIZE) == | |
7912 | BPF_ALU_SANITIZE_SRC; | |
7913 | ||
7914 | off_reg = issrc ? insn->src_reg : insn->dst_reg; | |
7915 | if (isneg) | |
7916 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); | |
7917 | *patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit - 1); | |
7918 | *patch++ = BPF_ALU64_REG(BPF_SUB, BPF_REG_AX, off_reg); | |
7919 | *patch++ = BPF_ALU64_REG(BPF_OR, BPF_REG_AX, off_reg); | |
7920 | *patch++ = BPF_ALU64_IMM(BPF_NEG, BPF_REG_AX, 0); | |
7921 | *patch++ = BPF_ALU64_IMM(BPF_ARSH, BPF_REG_AX, 63); | |
7922 | if (issrc) { | |
7923 | *patch++ = BPF_ALU64_REG(BPF_AND, BPF_REG_AX, | |
7924 | off_reg); | |
7925 | insn->src_reg = BPF_REG_AX; | |
7926 | } else { | |
7927 | *patch++ = BPF_ALU64_REG(BPF_AND, off_reg, | |
7928 | BPF_REG_AX); | |
7929 | } | |
7930 | if (isneg) | |
7931 | insn->code = insn->code == code_add ? | |
7932 | code_sub : code_add; | |
7933 | *patch++ = *insn; | |
7934 | if (issrc && isneg) | |
7935 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); | |
7936 | cnt = patch - insn_buf; | |
7937 | ||
7938 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
7939 | if (!new_prog) | |
7940 | return -ENOMEM; | |
7941 | ||
7942 | delta += cnt - 1; | |
7943 | env->prog = prog = new_prog; | |
7944 | insn = new_prog->insnsi + i + delta; | |
7945 | continue; | |
7946 | } | |
7947 | ||
79741b3b AS |
7948 | if (insn->code != (BPF_JMP | BPF_CALL)) |
7949 | continue; | |
cc8b0b92 AS |
7950 | if (insn->src_reg == BPF_PSEUDO_CALL) |
7951 | continue; | |
e245c5c6 | 7952 | |
79741b3b AS |
7953 | if (insn->imm == BPF_FUNC_get_route_realm) |
7954 | prog->dst_needed = 1; | |
7955 | if (insn->imm == BPF_FUNC_get_prandom_u32) | |
7956 | bpf_user_rnd_init_once(); | |
9802d865 JB |
7957 | if (insn->imm == BPF_FUNC_override_return) |
7958 | prog->kprobe_override = 1; | |
79741b3b | 7959 | if (insn->imm == BPF_FUNC_tail_call) { |
7b9f6da1 DM |
7960 | /* If we tail call into other programs, we |
7961 | * cannot make any assumptions since they can | |
7962 | * be replaced dynamically during runtime in | |
7963 | * the program array. | |
7964 | */ | |
7965 | prog->cb_access = 1; | |
80a58d02 | 7966 | env->prog->aux->stack_depth = MAX_BPF_STACK; |
e647815a | 7967 | env->prog->aux->max_pkt_offset = MAX_PACKET_OFF; |
7b9f6da1 | 7968 | |
79741b3b AS |
7969 | /* mark bpf_tail_call as different opcode to avoid |
7970 | * conditional branch in the interpeter for every normal | |
7971 | * call and to prevent accidental JITing by JIT compiler | |
7972 | * that doesn't support bpf_tail_call yet | |
e245c5c6 | 7973 | */ |
79741b3b | 7974 | insn->imm = 0; |
71189fa9 | 7975 | insn->code = BPF_JMP | BPF_TAIL_CALL; |
b2157399 | 7976 | |
c93552c4 DB |
7977 | aux = &env->insn_aux_data[i + delta]; |
7978 | if (!bpf_map_ptr_unpriv(aux)) | |
7979 | continue; | |
7980 | ||
b2157399 AS |
7981 | /* instead of changing every JIT dealing with tail_call |
7982 | * emit two extra insns: | |
7983 | * if (index >= max_entries) goto out; | |
7984 | * index &= array->index_mask; | |
7985 | * to avoid out-of-bounds cpu speculation | |
7986 | */ | |
c93552c4 | 7987 | if (bpf_map_ptr_poisoned(aux)) { |
40950343 | 7988 | verbose(env, "tail_call abusing map_ptr\n"); |
b2157399 AS |
7989 | return -EINVAL; |
7990 | } | |
c93552c4 DB |
7991 | |
7992 | map_ptr = BPF_MAP_PTR(aux->map_state); | |
b2157399 AS |
7993 | insn_buf[0] = BPF_JMP_IMM(BPF_JGE, BPF_REG_3, |
7994 | map_ptr->max_entries, 2); | |
7995 | insn_buf[1] = BPF_ALU32_IMM(BPF_AND, BPF_REG_3, | |
7996 | container_of(map_ptr, | |
7997 | struct bpf_array, | |
7998 | map)->index_mask); | |
7999 | insn_buf[2] = *insn; | |
8000 | cnt = 3; | |
8001 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
8002 | if (!new_prog) | |
8003 | return -ENOMEM; | |
8004 | ||
8005 | delta += cnt - 1; | |
8006 | env->prog = prog = new_prog; | |
8007 | insn = new_prog->insnsi + i + delta; | |
79741b3b AS |
8008 | continue; |
8009 | } | |
e245c5c6 | 8010 | |
89c63074 | 8011 | /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup |
09772d92 DB |
8012 | * and other inlining handlers are currently limited to 64 bit |
8013 | * only. | |
89c63074 | 8014 | */ |
60b58afc | 8015 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
09772d92 DB |
8016 | (insn->imm == BPF_FUNC_map_lookup_elem || |
8017 | insn->imm == BPF_FUNC_map_update_elem || | |
84430d42 DB |
8018 | insn->imm == BPF_FUNC_map_delete_elem || |
8019 | insn->imm == BPF_FUNC_map_push_elem || | |
8020 | insn->imm == BPF_FUNC_map_pop_elem || | |
8021 | insn->imm == BPF_FUNC_map_peek_elem)) { | |
c93552c4 DB |
8022 | aux = &env->insn_aux_data[i + delta]; |
8023 | if (bpf_map_ptr_poisoned(aux)) | |
8024 | goto patch_call_imm; | |
8025 | ||
8026 | map_ptr = BPF_MAP_PTR(aux->map_state); | |
09772d92 DB |
8027 | ops = map_ptr->ops; |
8028 | if (insn->imm == BPF_FUNC_map_lookup_elem && | |
8029 | ops->map_gen_lookup) { | |
8030 | cnt = ops->map_gen_lookup(map_ptr, insn_buf); | |
8031 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { | |
8032 | verbose(env, "bpf verifier is misconfigured\n"); | |
8033 | return -EINVAL; | |
8034 | } | |
81ed18ab | 8035 | |
09772d92 DB |
8036 | new_prog = bpf_patch_insn_data(env, i + delta, |
8037 | insn_buf, cnt); | |
8038 | if (!new_prog) | |
8039 | return -ENOMEM; | |
81ed18ab | 8040 | |
09772d92 DB |
8041 | delta += cnt - 1; |
8042 | env->prog = prog = new_prog; | |
8043 | insn = new_prog->insnsi + i + delta; | |
8044 | continue; | |
8045 | } | |
81ed18ab | 8046 | |
09772d92 DB |
8047 | BUILD_BUG_ON(!__same_type(ops->map_lookup_elem, |
8048 | (void *(*)(struct bpf_map *map, void *key))NULL)); | |
8049 | BUILD_BUG_ON(!__same_type(ops->map_delete_elem, | |
8050 | (int (*)(struct bpf_map *map, void *key))NULL)); | |
8051 | BUILD_BUG_ON(!__same_type(ops->map_update_elem, | |
8052 | (int (*)(struct bpf_map *map, void *key, void *value, | |
8053 | u64 flags))NULL)); | |
84430d42 DB |
8054 | BUILD_BUG_ON(!__same_type(ops->map_push_elem, |
8055 | (int (*)(struct bpf_map *map, void *value, | |
8056 | u64 flags))NULL)); | |
8057 | BUILD_BUG_ON(!__same_type(ops->map_pop_elem, | |
8058 | (int (*)(struct bpf_map *map, void *value))NULL)); | |
8059 | BUILD_BUG_ON(!__same_type(ops->map_peek_elem, | |
8060 | (int (*)(struct bpf_map *map, void *value))NULL)); | |
8061 | ||
09772d92 DB |
8062 | switch (insn->imm) { |
8063 | case BPF_FUNC_map_lookup_elem: | |
8064 | insn->imm = BPF_CAST_CALL(ops->map_lookup_elem) - | |
8065 | __bpf_call_base; | |
8066 | continue; | |
8067 | case BPF_FUNC_map_update_elem: | |
8068 | insn->imm = BPF_CAST_CALL(ops->map_update_elem) - | |
8069 | __bpf_call_base; | |
8070 | continue; | |
8071 | case BPF_FUNC_map_delete_elem: | |
8072 | insn->imm = BPF_CAST_CALL(ops->map_delete_elem) - | |
8073 | __bpf_call_base; | |
8074 | continue; | |
84430d42 DB |
8075 | case BPF_FUNC_map_push_elem: |
8076 | insn->imm = BPF_CAST_CALL(ops->map_push_elem) - | |
8077 | __bpf_call_base; | |
8078 | continue; | |
8079 | case BPF_FUNC_map_pop_elem: | |
8080 | insn->imm = BPF_CAST_CALL(ops->map_pop_elem) - | |
8081 | __bpf_call_base; | |
8082 | continue; | |
8083 | case BPF_FUNC_map_peek_elem: | |
8084 | insn->imm = BPF_CAST_CALL(ops->map_peek_elem) - | |
8085 | __bpf_call_base; | |
8086 | continue; | |
09772d92 | 8087 | } |
81ed18ab | 8088 | |
09772d92 | 8089 | goto patch_call_imm; |
81ed18ab AS |
8090 | } |
8091 | ||
8092 | patch_call_imm: | |
5e43f899 | 8093 | fn = env->ops->get_func_proto(insn->imm, env->prog); |
79741b3b AS |
8094 | /* all functions that have prototype and verifier allowed |
8095 | * programs to call them, must be real in-kernel functions | |
8096 | */ | |
8097 | if (!fn->func) { | |
61bd5218 JK |
8098 | verbose(env, |
8099 | "kernel subsystem misconfigured func %s#%d\n", | |
79741b3b AS |
8100 | func_id_name(insn->imm), insn->imm); |
8101 | return -EFAULT; | |
e245c5c6 | 8102 | } |
79741b3b | 8103 | insn->imm = fn->func - __bpf_call_base; |
e245c5c6 | 8104 | } |
e245c5c6 | 8105 | |
79741b3b AS |
8106 | return 0; |
8107 | } | |
e245c5c6 | 8108 | |
58e2af8b | 8109 | static void free_states(struct bpf_verifier_env *env) |
f1bca824 | 8110 | { |
58e2af8b | 8111 | struct bpf_verifier_state_list *sl, *sln; |
f1bca824 AS |
8112 | int i; |
8113 | ||
9f4686c4 AS |
8114 | sl = env->free_list; |
8115 | while (sl) { | |
8116 | sln = sl->next; | |
8117 | free_verifier_state(&sl->state, false); | |
8118 | kfree(sl); | |
8119 | sl = sln; | |
8120 | } | |
8121 | ||
f1bca824 AS |
8122 | if (!env->explored_states) |
8123 | return; | |
8124 | ||
8125 | for (i = 0; i < env->prog->len; i++) { | |
8126 | sl = env->explored_states[i]; | |
8127 | ||
8128 | if (sl) | |
8129 | while (sl != STATE_LIST_MARK) { | |
8130 | sln = sl->next; | |
1969db47 | 8131 | free_verifier_state(&sl->state, false); |
f1bca824 AS |
8132 | kfree(sl); |
8133 | sl = sln; | |
8134 | } | |
8135 | } | |
8136 | ||
71dde681 | 8137 | kvfree(env->explored_states); |
f1bca824 AS |
8138 | } |
8139 | ||
06ee7115 AS |
8140 | static void print_verification_stats(struct bpf_verifier_env *env) |
8141 | { | |
8142 | int i; | |
8143 | ||
8144 | if (env->log.level & BPF_LOG_STATS) { | |
8145 | verbose(env, "verification time %lld usec\n", | |
8146 | div_u64(env->verification_time, 1000)); | |
8147 | verbose(env, "stack depth "); | |
8148 | for (i = 0; i < env->subprog_cnt; i++) { | |
8149 | u32 depth = env->subprog_info[i].stack_depth; | |
8150 | ||
8151 | verbose(env, "%d", depth); | |
8152 | if (i + 1 < env->subprog_cnt) | |
8153 | verbose(env, "+"); | |
8154 | } | |
8155 | verbose(env, "\n"); | |
8156 | } | |
8157 | verbose(env, "processed %d insns (limit %d) max_states_per_insn %d " | |
8158 | "total_states %d peak_states %d mark_read %d\n", | |
8159 | env->insn_processed, BPF_COMPLEXITY_LIMIT_INSNS, | |
8160 | env->max_states_per_insn, env->total_states, | |
8161 | env->peak_states, env->longest_mark_read_walk); | |
f1bca824 AS |
8162 | } |
8163 | ||
838e9690 YS |
8164 | int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, |
8165 | union bpf_attr __user *uattr) | |
51580e79 | 8166 | { |
06ee7115 | 8167 | u64 start_time = ktime_get_ns(); |
58e2af8b | 8168 | struct bpf_verifier_env *env; |
b9193c1b | 8169 | struct bpf_verifier_log *log; |
9e4c24e7 | 8170 | int i, len, ret = -EINVAL; |
e2ae4ca2 | 8171 | bool is_priv; |
51580e79 | 8172 | |
eba0c929 AB |
8173 | /* no program is valid */ |
8174 | if (ARRAY_SIZE(bpf_verifier_ops) == 0) | |
8175 | return -EINVAL; | |
8176 | ||
58e2af8b | 8177 | /* 'struct bpf_verifier_env' can be global, but since it's not small, |
cbd35700 AS |
8178 | * allocate/free it every time bpf_check() is called |
8179 | */ | |
58e2af8b | 8180 | env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL); |
cbd35700 AS |
8181 | if (!env) |
8182 | return -ENOMEM; | |
61bd5218 | 8183 | log = &env->log; |
cbd35700 | 8184 | |
9e4c24e7 | 8185 | len = (*prog)->len; |
fad953ce | 8186 | env->insn_aux_data = |
9e4c24e7 | 8187 | vzalloc(array_size(sizeof(struct bpf_insn_aux_data), len)); |
3df126f3 JK |
8188 | ret = -ENOMEM; |
8189 | if (!env->insn_aux_data) | |
8190 | goto err_free_env; | |
9e4c24e7 JK |
8191 | for (i = 0; i < len; i++) |
8192 | env->insn_aux_data[i].orig_idx = i; | |
9bac3d6d | 8193 | env->prog = *prog; |
00176a34 | 8194 | env->ops = bpf_verifier_ops[env->prog->type]; |
45a73c17 | 8195 | is_priv = capable(CAP_SYS_ADMIN); |
0246e64d | 8196 | |
cbd35700 | 8197 | /* grab the mutex to protect few globals used by verifier */ |
45a73c17 AS |
8198 | if (!is_priv) |
8199 | mutex_lock(&bpf_verifier_lock); | |
cbd35700 AS |
8200 | |
8201 | if (attr->log_level || attr->log_buf || attr->log_size) { | |
8202 | /* user requested verbose verifier output | |
8203 | * and supplied buffer to store the verification trace | |
8204 | */ | |
e7bf8249 JK |
8205 | log->level = attr->log_level; |
8206 | log->ubuf = (char __user *) (unsigned long) attr->log_buf; | |
8207 | log->len_total = attr->log_size; | |
cbd35700 AS |
8208 | |
8209 | ret = -EINVAL; | |
e7bf8249 | 8210 | /* log attributes have to be sane */ |
7a9f5c65 | 8211 | if (log->len_total < 128 || log->len_total > UINT_MAX >> 2 || |
06ee7115 | 8212 | !log->level || !log->ubuf || log->level & ~BPF_LOG_MASK) |
3df126f3 | 8213 | goto err_unlock; |
cbd35700 | 8214 | } |
1ad2f583 DB |
8215 | |
8216 | env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT); | |
8217 | if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) | |
e07b98d9 | 8218 | env->strict_alignment = true; |
e9ee9efc DM |
8219 | if (attr->prog_flags & BPF_F_ANY_ALIGNMENT) |
8220 | env->strict_alignment = false; | |
cbd35700 | 8221 | |
e2ae4ca2 JK |
8222 | env->allow_ptr_leaks = is_priv; |
8223 | ||
f4e3ec0d JK |
8224 | ret = replace_map_fd_with_map_ptr(env); |
8225 | if (ret < 0) | |
8226 | goto skip_full_check; | |
8227 | ||
cae1927c | 8228 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
a40a2632 | 8229 | ret = bpf_prog_offload_verifier_prep(env->prog); |
ab3f0063 | 8230 | if (ret) |
f4e3ec0d | 8231 | goto skip_full_check; |
ab3f0063 JK |
8232 | } |
8233 | ||
71dde681 | 8234 | env->explored_states = kvcalloc(env->prog->len, |
58e2af8b | 8235 | sizeof(struct bpf_verifier_state_list *), |
f1bca824 AS |
8236 | GFP_USER); |
8237 | ret = -ENOMEM; | |
8238 | if (!env->explored_states) | |
8239 | goto skip_full_check; | |
8240 | ||
d9762e84 | 8241 | ret = check_subprogs(env); |
475fb78f AS |
8242 | if (ret < 0) |
8243 | goto skip_full_check; | |
8244 | ||
c454a46b | 8245 | ret = check_btf_info(env, attr, uattr); |
838e9690 YS |
8246 | if (ret < 0) |
8247 | goto skip_full_check; | |
8248 | ||
d9762e84 MKL |
8249 | ret = check_cfg(env); |
8250 | if (ret < 0) | |
8251 | goto skip_full_check; | |
8252 | ||
17a52670 | 8253 | ret = do_check(env); |
8c01c4f8 CG |
8254 | if (env->cur_state) { |
8255 | free_verifier_state(env->cur_state, true); | |
8256 | env->cur_state = NULL; | |
8257 | } | |
cbd35700 | 8258 | |
c941ce9c QM |
8259 | if (ret == 0 && bpf_prog_is_dev_bound(env->prog->aux)) |
8260 | ret = bpf_prog_offload_finalize(env); | |
8261 | ||
0246e64d | 8262 | skip_full_check: |
638f5b90 | 8263 | while (!pop_stack(env, NULL, NULL)); |
f1bca824 | 8264 | free_states(env); |
0246e64d | 8265 | |
c131187d | 8266 | if (ret == 0) |
9b38c405 | 8267 | ret = check_max_stack_depth(env); |
c131187d | 8268 | |
9b38c405 | 8269 | /* instruction rewrites happen after this point */ |
e2ae4ca2 JK |
8270 | if (is_priv) { |
8271 | if (ret == 0) | |
8272 | opt_hard_wire_dead_code_branches(env); | |
52875a04 JK |
8273 | if (ret == 0) |
8274 | ret = opt_remove_dead_code(env); | |
a1b14abc JK |
8275 | if (ret == 0) |
8276 | ret = opt_remove_nops(env); | |
52875a04 JK |
8277 | } else { |
8278 | if (ret == 0) | |
8279 | sanitize_dead_code(env); | |
e2ae4ca2 JK |
8280 | } |
8281 | ||
9bac3d6d AS |
8282 | if (ret == 0) |
8283 | /* program is valid, convert *(u32*)(ctx + off) accesses */ | |
8284 | ret = convert_ctx_accesses(env); | |
8285 | ||
e245c5c6 | 8286 | if (ret == 0) |
79741b3b | 8287 | ret = fixup_bpf_calls(env); |
e245c5c6 | 8288 | |
1ea47e01 AS |
8289 | if (ret == 0) |
8290 | ret = fixup_call_args(env); | |
8291 | ||
06ee7115 AS |
8292 | env->verification_time = ktime_get_ns() - start_time; |
8293 | print_verification_stats(env); | |
8294 | ||
a2a7d570 | 8295 | if (log->level && bpf_verifier_log_full(log)) |
cbd35700 | 8296 | ret = -ENOSPC; |
a2a7d570 | 8297 | if (log->level && !log->ubuf) { |
cbd35700 | 8298 | ret = -EFAULT; |
a2a7d570 | 8299 | goto err_release_maps; |
cbd35700 AS |
8300 | } |
8301 | ||
0246e64d AS |
8302 | if (ret == 0 && env->used_map_cnt) { |
8303 | /* if program passed verifier, update used_maps in bpf_prog_info */ | |
9bac3d6d AS |
8304 | env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt, |
8305 | sizeof(env->used_maps[0]), | |
8306 | GFP_KERNEL); | |
0246e64d | 8307 | |
9bac3d6d | 8308 | if (!env->prog->aux->used_maps) { |
0246e64d | 8309 | ret = -ENOMEM; |
a2a7d570 | 8310 | goto err_release_maps; |
0246e64d AS |
8311 | } |
8312 | ||
9bac3d6d | 8313 | memcpy(env->prog->aux->used_maps, env->used_maps, |
0246e64d | 8314 | sizeof(env->used_maps[0]) * env->used_map_cnt); |
9bac3d6d | 8315 | env->prog->aux->used_map_cnt = env->used_map_cnt; |
0246e64d AS |
8316 | |
8317 | /* program is valid. Convert pseudo bpf_ld_imm64 into generic | |
8318 | * bpf_ld_imm64 instructions | |
8319 | */ | |
8320 | convert_pseudo_ld_imm64(env); | |
8321 | } | |
cbd35700 | 8322 | |
ba64e7d8 YS |
8323 | if (ret == 0) |
8324 | adjust_btf_func(env); | |
8325 | ||
a2a7d570 | 8326 | err_release_maps: |
9bac3d6d | 8327 | if (!env->prog->aux->used_maps) |
0246e64d | 8328 | /* if we didn't copy map pointers into bpf_prog_info, release |
ab7f5bf0 | 8329 | * them now. Otherwise free_used_maps() will release them. |
0246e64d AS |
8330 | */ |
8331 | release_maps(env); | |
9bac3d6d | 8332 | *prog = env->prog; |
3df126f3 | 8333 | err_unlock: |
45a73c17 AS |
8334 | if (!is_priv) |
8335 | mutex_unlock(&bpf_verifier_lock); | |
3df126f3 JK |
8336 | vfree(env->insn_aux_data); |
8337 | err_free_env: | |
8338 | kfree(env); | |
51580e79 AS |
8339 | return ret; |
8340 | } |