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