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