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 | */ | |
14 | #include <linux/kernel.h> | |
15 | #include <linux/types.h> | |
16 | #include <linux/slab.h> | |
17 | #include <linux/bpf.h> | |
58e2af8b | 18 | #include <linux/bpf_verifier.h> |
51580e79 AS |
19 | #include <linux/filter.h> |
20 | #include <net/netlink.h> | |
21 | #include <linux/file.h> | |
22 | #include <linux/vmalloc.h> | |
ebb676da | 23 | #include <linux/stringify.h> |
cc8b0b92 AS |
24 | #include <linux/bsearch.h> |
25 | #include <linux/sort.h> | |
c195651e | 26 | #include <linux/perf_event.h> |
51580e79 | 27 | |
f4ac7e0b JK |
28 | #include "disasm.h" |
29 | ||
00176a34 JK |
30 | static const struct bpf_verifier_ops * const bpf_verifier_ops[] = { |
31 | #define BPF_PROG_TYPE(_id, _name) \ | |
32 | [_id] = & _name ## _verifier_ops, | |
33 | #define BPF_MAP_TYPE(_id, _ops) | |
34 | #include <linux/bpf_types.h> | |
35 | #undef BPF_PROG_TYPE | |
36 | #undef BPF_MAP_TYPE | |
37 | }; | |
38 | ||
51580e79 AS |
39 | /* bpf_check() is a static code analyzer that walks eBPF program |
40 | * instruction by instruction and updates register/stack state. | |
41 | * All paths of conditional branches are analyzed until 'bpf_exit' insn. | |
42 | * | |
43 | * The first pass is depth-first-search to check that the program is a DAG. | |
44 | * It rejects the following programs: | |
45 | * - larger than BPF_MAXINSNS insns | |
46 | * - if loop is present (detected via back-edge) | |
47 | * - unreachable insns exist (shouldn't be a forest. program = one function) | |
48 | * - out of bounds or malformed jumps | |
49 | * The second pass is all possible path descent from the 1st insn. | |
50 | * Since it's analyzing all pathes through the program, the length of the | |
eba38a96 | 51 | * analysis is limited to 64k insn, which may be hit even if total number of |
51580e79 AS |
52 | * insn is less then 4K, but there are too many branches that change stack/regs. |
53 | * Number of 'branches to be analyzed' is limited to 1k | |
54 | * | |
55 | * On entry to each instruction, each register has a type, and the instruction | |
56 | * changes the types of the registers depending on instruction semantics. | |
57 | * If instruction is BPF_MOV64_REG(BPF_REG_1, BPF_REG_5), then type of R5 is | |
58 | * copied to R1. | |
59 | * | |
60 | * All registers are 64-bit. | |
61 | * R0 - return register | |
62 | * R1-R5 argument passing registers | |
63 | * R6-R9 callee saved registers | |
64 | * R10 - frame pointer read-only | |
65 | * | |
66 | * At the start of BPF program the register R1 contains a pointer to bpf_context | |
67 | * and has type PTR_TO_CTX. | |
68 | * | |
69 | * Verifier tracks arithmetic operations on pointers in case: | |
70 | * BPF_MOV64_REG(BPF_REG_1, BPF_REG_10), | |
71 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -20), | |
72 | * 1st insn copies R10 (which has FRAME_PTR) type into R1 | |
73 | * and 2nd arithmetic instruction is pattern matched to recognize | |
74 | * that it wants to construct a pointer to some element within stack. | |
75 | * So after 2nd insn, the register R1 has type PTR_TO_STACK | |
76 | * (and -20 constant is saved for further stack bounds checking). | |
77 | * Meaning that this reg is a pointer to stack plus known immediate constant. | |
78 | * | |
f1174f77 | 79 | * Most of the time the registers have SCALAR_VALUE type, which |
51580e79 | 80 | * means the register has some value, but it's not a valid pointer. |
f1174f77 | 81 | * (like pointer plus pointer becomes SCALAR_VALUE type) |
51580e79 AS |
82 | * |
83 | * When verifier sees load or store instructions the type of base register | |
c64b7983 JS |
84 | * can be: PTR_TO_MAP_VALUE, PTR_TO_CTX, PTR_TO_STACK, PTR_TO_SOCKET. These are |
85 | * four pointer types recognized by check_mem_access() function. | |
51580e79 AS |
86 | * |
87 | * PTR_TO_MAP_VALUE means that this register is pointing to 'map element value' | |
88 | * and the range of [ptr, ptr + map's value_size) is accessible. | |
89 | * | |
90 | * registers used to pass values to function calls are checked against | |
91 | * function argument constraints. | |
92 | * | |
93 | * ARG_PTR_TO_MAP_KEY is one of such argument constraints. | |
94 | * It means that the register type passed to this function must be | |
95 | * PTR_TO_STACK and it will be used inside the function as | |
96 | * 'pointer to map element key' | |
97 | * | |
98 | * For example the argument constraints for bpf_map_lookup_elem(): | |
99 | * .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL, | |
100 | * .arg1_type = ARG_CONST_MAP_PTR, | |
101 | * .arg2_type = ARG_PTR_TO_MAP_KEY, | |
102 | * | |
103 | * ret_type says that this function returns 'pointer to map elem value or null' | |
104 | * function expects 1st argument to be a const pointer to 'struct bpf_map' and | |
105 | * 2nd argument should be a pointer to stack, which will be used inside | |
106 | * the helper function as a pointer to map element key. | |
107 | * | |
108 | * On the kernel side the helper function looks like: | |
109 | * u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) | |
110 | * { | |
111 | * struct bpf_map *map = (struct bpf_map *) (unsigned long) r1; | |
112 | * void *key = (void *) (unsigned long) r2; | |
113 | * void *value; | |
114 | * | |
115 | * here kernel can access 'key' and 'map' pointers safely, knowing that | |
116 | * [key, key + map->key_size) bytes are valid and were initialized on | |
117 | * the stack of eBPF program. | |
118 | * } | |
119 | * | |
120 | * Corresponding eBPF program may look like: | |
121 | * BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), // after this insn R2 type is FRAME_PTR | |
122 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), // after this insn R2 type is PTR_TO_STACK | |
123 | * BPF_LD_MAP_FD(BPF_REG_1, map_fd), // after this insn R1 type is CONST_PTR_TO_MAP | |
124 | * BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem), | |
125 | * here verifier looks at prototype of map_lookup_elem() and sees: | |
126 | * .arg1_type == ARG_CONST_MAP_PTR and R1->type == CONST_PTR_TO_MAP, which is ok, | |
127 | * Now verifier knows that this map has key of R1->map_ptr->key_size bytes | |
128 | * | |
129 | * Then .arg2_type == ARG_PTR_TO_MAP_KEY and R2->type == PTR_TO_STACK, ok so far, | |
130 | * Now verifier checks that [R2, R2 + map's key_size) are within stack limits | |
131 | * and were initialized prior to this call. | |
132 | * If it's ok, then verifier allows this BPF_CALL insn and looks at | |
133 | * .ret_type which is RET_PTR_TO_MAP_VALUE_OR_NULL, so it sets | |
134 | * R0->type = PTR_TO_MAP_VALUE_OR_NULL which means bpf_map_lookup_elem() function | |
135 | * returns ether pointer to map value or NULL. | |
136 | * | |
137 | * When type PTR_TO_MAP_VALUE_OR_NULL passes through 'if (reg != 0) goto +off' | |
138 | * insn, the register holding that pointer in the true branch changes state to | |
139 | * PTR_TO_MAP_VALUE and the same register changes state to CONST_IMM in the false | |
140 | * branch. See check_cond_jmp_op(). | |
141 | * | |
142 | * After the call R0 is set to return type of the function and registers R1-R5 | |
143 | * are set to NOT_INIT to indicate that they are no longer readable. | |
fd978bf7 JS |
144 | * |
145 | * The following reference types represent a potential reference to a kernel | |
146 | * resource which, after first being allocated, must be checked and freed by | |
147 | * the BPF program: | |
148 | * - PTR_TO_SOCKET_OR_NULL, PTR_TO_SOCKET | |
149 | * | |
150 | * When the verifier sees a helper call return a reference type, it allocates a | |
151 | * pointer id for the reference and stores it in the current function state. | |
152 | * Similar to the way that PTR_TO_MAP_VALUE_OR_NULL is converted into | |
153 | * PTR_TO_MAP_VALUE, PTR_TO_SOCKET_OR_NULL becomes PTR_TO_SOCKET when the type | |
154 | * passes through a NULL-check conditional. For the branch wherein the state is | |
155 | * changed to CONST_IMM, the verifier releases the reference. | |
51580e79 AS |
156 | */ |
157 | ||
17a52670 | 158 | /* verifier_state + insn_idx are pushed to stack when branch is encountered */ |
58e2af8b | 159 | struct bpf_verifier_stack_elem { |
17a52670 AS |
160 | /* verifer state is 'st' |
161 | * before processing instruction 'insn_idx' | |
162 | * and after processing instruction 'prev_insn_idx' | |
163 | */ | |
58e2af8b | 164 | struct bpf_verifier_state st; |
17a52670 AS |
165 | int insn_idx; |
166 | int prev_insn_idx; | |
58e2af8b | 167 | struct bpf_verifier_stack_elem *next; |
cbd35700 AS |
168 | }; |
169 | ||
8e17c1b1 | 170 | #define BPF_COMPLEXITY_LIMIT_INSNS 131072 |
07016151 DB |
171 | #define BPF_COMPLEXITY_LIMIT_STACK 1024 |
172 | ||
c93552c4 DB |
173 | #define BPF_MAP_PTR_UNPRIV 1UL |
174 | #define BPF_MAP_PTR_POISON ((void *)((0xeB9FUL << 1) + \ | |
175 | POISON_POINTER_DELTA)) | |
176 | #define BPF_MAP_PTR(X) ((struct bpf_map *)((X) & ~BPF_MAP_PTR_UNPRIV)) | |
177 | ||
178 | static bool bpf_map_ptr_poisoned(const struct bpf_insn_aux_data *aux) | |
179 | { | |
180 | return BPF_MAP_PTR(aux->map_state) == BPF_MAP_PTR_POISON; | |
181 | } | |
182 | ||
183 | static bool bpf_map_ptr_unpriv(const struct bpf_insn_aux_data *aux) | |
184 | { | |
185 | return aux->map_state & BPF_MAP_PTR_UNPRIV; | |
186 | } | |
187 | ||
188 | static void bpf_map_ptr_store(struct bpf_insn_aux_data *aux, | |
189 | const struct bpf_map *map, bool unpriv) | |
190 | { | |
191 | BUILD_BUG_ON((unsigned long)BPF_MAP_PTR_POISON & BPF_MAP_PTR_UNPRIV); | |
192 | unpriv |= bpf_map_ptr_unpriv(aux); | |
193 | aux->map_state = (unsigned long)map | | |
194 | (unpriv ? BPF_MAP_PTR_UNPRIV : 0UL); | |
195 | } | |
fad73a1a | 196 | |
33ff9823 DB |
197 | struct bpf_call_arg_meta { |
198 | struct bpf_map *map_ptr; | |
435faee1 | 199 | bool raw_mode; |
36bbef52 | 200 | bool pkt_access; |
435faee1 DB |
201 | int regno; |
202 | int access_size; | |
849fa506 YS |
203 | s64 msize_smax_value; |
204 | u64 msize_umax_value; | |
fd978bf7 | 205 | int ptr_id; |
33ff9823 DB |
206 | }; |
207 | ||
cbd35700 AS |
208 | static DEFINE_MUTEX(bpf_verifier_lock); |
209 | ||
77d2e05a MKL |
210 | void bpf_verifier_vlog(struct bpf_verifier_log *log, const char *fmt, |
211 | va_list args) | |
cbd35700 | 212 | { |
a2a7d570 | 213 | unsigned int n; |
cbd35700 | 214 | |
a2a7d570 | 215 | n = vscnprintf(log->kbuf, BPF_VERIFIER_TMP_LOG_SIZE, fmt, args); |
a2a7d570 JK |
216 | |
217 | WARN_ONCE(n >= BPF_VERIFIER_TMP_LOG_SIZE - 1, | |
218 | "verifier log line truncated - local buffer too short\n"); | |
219 | ||
220 | n = min(log->len_total - log->len_used - 1, n); | |
221 | log->kbuf[n] = '\0'; | |
222 | ||
223 | if (!copy_to_user(log->ubuf + log->len_used, log->kbuf, n + 1)) | |
224 | log->len_used += n; | |
225 | else | |
226 | log->ubuf = NULL; | |
cbd35700 | 227 | } |
abe08840 JO |
228 | |
229 | /* log_level controls verbosity level of eBPF verifier. | |
230 | * bpf_verifier_log_write() is used to dump the verification trace to the log, | |
231 | * so the user can figure out what's wrong with the program | |
430e68d1 | 232 | */ |
abe08840 JO |
233 | __printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env, |
234 | const char *fmt, ...) | |
235 | { | |
236 | va_list args; | |
237 | ||
77d2e05a MKL |
238 | if (!bpf_verifier_log_needed(&env->log)) |
239 | return; | |
240 | ||
abe08840 | 241 | va_start(args, fmt); |
77d2e05a | 242 | bpf_verifier_vlog(&env->log, fmt, args); |
abe08840 JO |
243 | va_end(args); |
244 | } | |
245 | EXPORT_SYMBOL_GPL(bpf_verifier_log_write); | |
246 | ||
247 | __printf(2, 3) static void verbose(void *private_data, const char *fmt, ...) | |
248 | { | |
77d2e05a | 249 | struct bpf_verifier_env *env = private_data; |
abe08840 JO |
250 | va_list args; |
251 | ||
77d2e05a MKL |
252 | if (!bpf_verifier_log_needed(&env->log)) |
253 | return; | |
254 | ||
abe08840 | 255 | va_start(args, fmt); |
77d2e05a | 256 | bpf_verifier_vlog(&env->log, fmt, args); |
abe08840 JO |
257 | va_end(args); |
258 | } | |
cbd35700 | 259 | |
de8f3a83 DB |
260 | static bool type_is_pkt_pointer(enum bpf_reg_type type) |
261 | { | |
262 | return type == PTR_TO_PACKET || | |
263 | type == PTR_TO_PACKET_META; | |
264 | } | |
265 | ||
840b9615 JS |
266 | static bool reg_type_may_be_null(enum bpf_reg_type type) |
267 | { | |
fd978bf7 JS |
268 | return type == PTR_TO_MAP_VALUE_OR_NULL || |
269 | type == PTR_TO_SOCKET_OR_NULL; | |
270 | } | |
271 | ||
272 | static bool type_is_refcounted(enum bpf_reg_type type) | |
273 | { | |
274 | return type == PTR_TO_SOCKET; | |
275 | } | |
276 | ||
277 | static bool type_is_refcounted_or_null(enum bpf_reg_type type) | |
278 | { | |
279 | return type == PTR_TO_SOCKET || type == PTR_TO_SOCKET_OR_NULL; | |
280 | } | |
281 | ||
282 | static bool reg_is_refcounted(const struct bpf_reg_state *reg) | |
283 | { | |
284 | return type_is_refcounted(reg->type); | |
285 | } | |
286 | ||
287 | static bool reg_is_refcounted_or_null(const struct bpf_reg_state *reg) | |
288 | { | |
289 | return type_is_refcounted_or_null(reg->type); | |
290 | } | |
291 | ||
292 | static bool arg_type_is_refcounted(enum bpf_arg_type type) | |
293 | { | |
294 | return type == ARG_PTR_TO_SOCKET; | |
295 | } | |
296 | ||
297 | /* Determine whether the function releases some resources allocated by another | |
298 | * function call. The first reference type argument will be assumed to be | |
299 | * released by release_reference(). | |
300 | */ | |
301 | static bool is_release_function(enum bpf_func_id func_id) | |
302 | { | |
303 | return false; | |
840b9615 JS |
304 | } |
305 | ||
17a52670 AS |
306 | /* string representation of 'enum bpf_reg_type' */ |
307 | static const char * const reg_type_str[] = { | |
308 | [NOT_INIT] = "?", | |
f1174f77 | 309 | [SCALAR_VALUE] = "inv", |
17a52670 AS |
310 | [PTR_TO_CTX] = "ctx", |
311 | [CONST_PTR_TO_MAP] = "map_ptr", | |
312 | [PTR_TO_MAP_VALUE] = "map_value", | |
313 | [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null", | |
17a52670 | 314 | [PTR_TO_STACK] = "fp", |
969bf05e | 315 | [PTR_TO_PACKET] = "pkt", |
de8f3a83 | 316 | [PTR_TO_PACKET_META] = "pkt_meta", |
969bf05e | 317 | [PTR_TO_PACKET_END] = "pkt_end", |
d58e468b | 318 | [PTR_TO_FLOW_KEYS] = "flow_keys", |
c64b7983 JS |
319 | [PTR_TO_SOCKET] = "sock", |
320 | [PTR_TO_SOCKET_OR_NULL] = "sock_or_null", | |
17a52670 AS |
321 | }; |
322 | ||
8efea21d EC |
323 | static char slot_type_char[] = { |
324 | [STACK_INVALID] = '?', | |
325 | [STACK_SPILL] = 'r', | |
326 | [STACK_MISC] = 'm', | |
327 | [STACK_ZERO] = '0', | |
328 | }; | |
329 | ||
4e92024a AS |
330 | static void print_liveness(struct bpf_verifier_env *env, |
331 | enum bpf_reg_liveness live) | |
332 | { | |
333 | if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN)) | |
334 | verbose(env, "_"); | |
335 | if (live & REG_LIVE_READ) | |
336 | verbose(env, "r"); | |
337 | if (live & REG_LIVE_WRITTEN) | |
338 | verbose(env, "w"); | |
339 | } | |
340 | ||
f4d7e40a AS |
341 | static struct bpf_func_state *func(struct bpf_verifier_env *env, |
342 | const struct bpf_reg_state *reg) | |
343 | { | |
344 | struct bpf_verifier_state *cur = env->cur_state; | |
345 | ||
346 | return cur->frame[reg->frameno]; | |
347 | } | |
348 | ||
61bd5218 | 349 | static void print_verifier_state(struct bpf_verifier_env *env, |
f4d7e40a | 350 | const struct bpf_func_state *state) |
17a52670 | 351 | { |
f4d7e40a | 352 | const struct bpf_reg_state *reg; |
17a52670 AS |
353 | enum bpf_reg_type t; |
354 | int i; | |
355 | ||
f4d7e40a AS |
356 | if (state->frameno) |
357 | verbose(env, " frame%d:", state->frameno); | |
17a52670 | 358 | for (i = 0; i < MAX_BPF_REG; i++) { |
1a0dc1ac AS |
359 | reg = &state->regs[i]; |
360 | t = reg->type; | |
17a52670 AS |
361 | if (t == NOT_INIT) |
362 | continue; | |
4e92024a AS |
363 | verbose(env, " R%d", i); |
364 | print_liveness(env, reg->live); | |
365 | verbose(env, "=%s", reg_type_str[t]); | |
f1174f77 EC |
366 | if ((t == SCALAR_VALUE || t == PTR_TO_STACK) && |
367 | tnum_is_const(reg->var_off)) { | |
368 | /* reg->off should be 0 for SCALAR_VALUE */ | |
61bd5218 | 369 | verbose(env, "%lld", reg->var_off.value + reg->off); |
f4d7e40a AS |
370 | if (t == PTR_TO_STACK) |
371 | verbose(env, ",call_%d", func(env, reg)->callsite); | |
f1174f77 | 372 | } else { |
61bd5218 | 373 | verbose(env, "(id=%d", reg->id); |
f1174f77 | 374 | if (t != SCALAR_VALUE) |
61bd5218 | 375 | verbose(env, ",off=%d", reg->off); |
de8f3a83 | 376 | if (type_is_pkt_pointer(t)) |
61bd5218 | 377 | verbose(env, ",r=%d", reg->range); |
f1174f77 EC |
378 | else if (t == CONST_PTR_TO_MAP || |
379 | t == PTR_TO_MAP_VALUE || | |
380 | t == PTR_TO_MAP_VALUE_OR_NULL) | |
61bd5218 | 381 | verbose(env, ",ks=%d,vs=%d", |
f1174f77 EC |
382 | reg->map_ptr->key_size, |
383 | reg->map_ptr->value_size); | |
7d1238f2 EC |
384 | if (tnum_is_const(reg->var_off)) { |
385 | /* Typically an immediate SCALAR_VALUE, but | |
386 | * could be a pointer whose offset is too big | |
387 | * for reg->off | |
388 | */ | |
61bd5218 | 389 | verbose(env, ",imm=%llx", reg->var_off.value); |
7d1238f2 EC |
390 | } else { |
391 | if (reg->smin_value != reg->umin_value && | |
392 | reg->smin_value != S64_MIN) | |
61bd5218 | 393 | verbose(env, ",smin_value=%lld", |
7d1238f2 EC |
394 | (long long)reg->smin_value); |
395 | if (reg->smax_value != reg->umax_value && | |
396 | reg->smax_value != S64_MAX) | |
61bd5218 | 397 | verbose(env, ",smax_value=%lld", |
7d1238f2 EC |
398 | (long long)reg->smax_value); |
399 | if (reg->umin_value != 0) | |
61bd5218 | 400 | verbose(env, ",umin_value=%llu", |
7d1238f2 EC |
401 | (unsigned long long)reg->umin_value); |
402 | if (reg->umax_value != U64_MAX) | |
61bd5218 | 403 | verbose(env, ",umax_value=%llu", |
7d1238f2 EC |
404 | (unsigned long long)reg->umax_value); |
405 | if (!tnum_is_unknown(reg->var_off)) { | |
406 | char tn_buf[48]; | |
f1174f77 | 407 | |
7d1238f2 | 408 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
61bd5218 | 409 | verbose(env, ",var_off=%s", tn_buf); |
7d1238f2 | 410 | } |
f1174f77 | 411 | } |
61bd5218 | 412 | verbose(env, ")"); |
f1174f77 | 413 | } |
17a52670 | 414 | } |
638f5b90 | 415 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
8efea21d EC |
416 | char types_buf[BPF_REG_SIZE + 1]; |
417 | bool valid = false; | |
418 | int j; | |
419 | ||
420 | for (j = 0; j < BPF_REG_SIZE; j++) { | |
421 | if (state->stack[i].slot_type[j] != STACK_INVALID) | |
422 | valid = true; | |
423 | types_buf[j] = slot_type_char[ | |
424 | state->stack[i].slot_type[j]]; | |
425 | } | |
426 | types_buf[BPF_REG_SIZE] = 0; | |
427 | if (!valid) | |
428 | continue; | |
429 | verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE); | |
430 | print_liveness(env, state->stack[i].spilled_ptr.live); | |
431 | if (state->stack[i].slot_type[0] == STACK_SPILL) | |
4e92024a | 432 | verbose(env, "=%s", |
638f5b90 | 433 | reg_type_str[state->stack[i].spilled_ptr.type]); |
8efea21d EC |
434 | else |
435 | verbose(env, "=%s", types_buf); | |
17a52670 | 436 | } |
fd978bf7 JS |
437 | if (state->acquired_refs && state->refs[0].id) { |
438 | verbose(env, " refs=%d", state->refs[0].id); | |
439 | for (i = 1; i < state->acquired_refs; i++) | |
440 | if (state->refs[i].id) | |
441 | verbose(env, ",%d", state->refs[i].id); | |
442 | } | |
61bd5218 | 443 | verbose(env, "\n"); |
17a52670 AS |
444 | } |
445 | ||
84dbf350 JS |
446 | #define COPY_STATE_FN(NAME, COUNT, FIELD, SIZE) \ |
447 | static int copy_##NAME##_state(struct bpf_func_state *dst, \ | |
448 | const struct bpf_func_state *src) \ | |
449 | { \ | |
450 | if (!src->FIELD) \ | |
451 | return 0; \ | |
452 | if (WARN_ON_ONCE(dst->COUNT < src->COUNT)) { \ | |
453 | /* internal bug, make state invalid to reject the program */ \ | |
454 | memset(dst, 0, sizeof(*dst)); \ | |
455 | return -EFAULT; \ | |
456 | } \ | |
457 | memcpy(dst->FIELD, src->FIELD, \ | |
458 | sizeof(*src->FIELD) * (src->COUNT / SIZE)); \ | |
459 | return 0; \ | |
638f5b90 | 460 | } |
fd978bf7 JS |
461 | /* copy_reference_state() */ |
462 | COPY_STATE_FN(reference, acquired_refs, refs, 1) | |
84dbf350 JS |
463 | /* copy_stack_state() */ |
464 | COPY_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE) | |
465 | #undef COPY_STATE_FN | |
466 | ||
467 | #define REALLOC_STATE_FN(NAME, COUNT, FIELD, SIZE) \ | |
468 | static int realloc_##NAME##_state(struct bpf_func_state *state, int size, \ | |
469 | bool copy_old) \ | |
470 | { \ | |
471 | u32 old_size = state->COUNT; \ | |
472 | struct bpf_##NAME##_state *new_##FIELD; \ | |
473 | int slot = size / SIZE; \ | |
474 | \ | |
475 | if (size <= old_size || !size) { \ | |
476 | if (copy_old) \ | |
477 | return 0; \ | |
478 | state->COUNT = slot * SIZE; \ | |
479 | if (!size && old_size) { \ | |
480 | kfree(state->FIELD); \ | |
481 | state->FIELD = NULL; \ | |
482 | } \ | |
483 | return 0; \ | |
484 | } \ | |
485 | new_##FIELD = kmalloc_array(slot, sizeof(struct bpf_##NAME##_state), \ | |
486 | GFP_KERNEL); \ | |
487 | if (!new_##FIELD) \ | |
488 | return -ENOMEM; \ | |
489 | if (copy_old) { \ | |
490 | if (state->FIELD) \ | |
491 | memcpy(new_##FIELD, state->FIELD, \ | |
492 | sizeof(*new_##FIELD) * (old_size / SIZE)); \ | |
493 | memset(new_##FIELD + old_size / SIZE, 0, \ | |
494 | sizeof(*new_##FIELD) * (size - old_size) / SIZE); \ | |
495 | } \ | |
496 | state->COUNT = slot * SIZE; \ | |
497 | kfree(state->FIELD); \ | |
498 | state->FIELD = new_##FIELD; \ | |
499 | return 0; \ | |
500 | } | |
fd978bf7 JS |
501 | /* realloc_reference_state() */ |
502 | REALLOC_STATE_FN(reference, acquired_refs, refs, 1) | |
84dbf350 JS |
503 | /* realloc_stack_state() */ |
504 | REALLOC_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE) | |
505 | #undef REALLOC_STATE_FN | |
638f5b90 AS |
506 | |
507 | /* do_check() starts with zero-sized stack in struct bpf_verifier_state to | |
508 | * make it consume minimal amount of memory. check_stack_write() access from | |
f4d7e40a | 509 | * the program calls into realloc_func_state() to grow the stack size. |
84dbf350 JS |
510 | * Note there is a non-zero 'parent' pointer inside bpf_verifier_state |
511 | * which realloc_stack_state() copies over. It points to previous | |
512 | * bpf_verifier_state which is never reallocated. | |
638f5b90 | 513 | */ |
fd978bf7 JS |
514 | static int realloc_func_state(struct bpf_func_state *state, int stack_size, |
515 | int refs_size, bool copy_old) | |
638f5b90 | 516 | { |
fd978bf7 JS |
517 | int err = realloc_reference_state(state, refs_size, copy_old); |
518 | if (err) | |
519 | return err; | |
520 | return realloc_stack_state(state, stack_size, copy_old); | |
521 | } | |
522 | ||
523 | /* Acquire a pointer id from the env and update the state->refs to include | |
524 | * this new pointer reference. | |
525 | * On success, returns a valid pointer id to associate with the register | |
526 | * On failure, returns a negative errno. | |
527 | */ | |
528 | static int acquire_reference_state(struct bpf_verifier_env *env, int insn_idx) | |
529 | { | |
530 | struct bpf_func_state *state = cur_func(env); | |
531 | int new_ofs = state->acquired_refs; | |
532 | int id, err; | |
533 | ||
534 | err = realloc_reference_state(state, state->acquired_refs + 1, true); | |
535 | if (err) | |
536 | return err; | |
537 | id = ++env->id_gen; | |
538 | state->refs[new_ofs].id = id; | |
539 | state->refs[new_ofs].insn_idx = insn_idx; | |
540 | ||
541 | return id; | |
542 | } | |
543 | ||
544 | /* release function corresponding to acquire_reference_state(). Idempotent. */ | |
545 | static int __release_reference_state(struct bpf_func_state *state, int ptr_id) | |
546 | { | |
547 | int i, last_idx; | |
548 | ||
549 | if (!ptr_id) | |
550 | return -EFAULT; | |
551 | ||
552 | last_idx = state->acquired_refs - 1; | |
553 | for (i = 0; i < state->acquired_refs; i++) { | |
554 | if (state->refs[i].id == ptr_id) { | |
555 | if (last_idx && i != last_idx) | |
556 | memcpy(&state->refs[i], &state->refs[last_idx], | |
557 | sizeof(*state->refs)); | |
558 | memset(&state->refs[last_idx], 0, sizeof(*state->refs)); | |
559 | state->acquired_refs--; | |
560 | return 0; | |
561 | } | |
562 | } | |
563 | return -EFAULT; | |
564 | } | |
565 | ||
566 | /* variation on the above for cases where we expect that there must be an | |
567 | * outstanding reference for the specified ptr_id. | |
568 | */ | |
569 | static int release_reference_state(struct bpf_verifier_env *env, int ptr_id) | |
570 | { | |
571 | struct bpf_func_state *state = cur_func(env); | |
572 | int err; | |
573 | ||
574 | err = __release_reference_state(state, ptr_id); | |
575 | if (WARN_ON_ONCE(err != 0)) | |
576 | verbose(env, "verifier internal error: can't release reference\n"); | |
577 | return err; | |
578 | } | |
579 | ||
580 | static int transfer_reference_state(struct bpf_func_state *dst, | |
581 | struct bpf_func_state *src) | |
582 | { | |
583 | int err = realloc_reference_state(dst, src->acquired_refs, false); | |
584 | if (err) | |
585 | return err; | |
586 | err = copy_reference_state(dst, src); | |
587 | if (err) | |
588 | return err; | |
589 | return 0; | |
638f5b90 AS |
590 | } |
591 | ||
f4d7e40a AS |
592 | static void free_func_state(struct bpf_func_state *state) |
593 | { | |
5896351e AS |
594 | if (!state) |
595 | return; | |
fd978bf7 | 596 | kfree(state->refs); |
f4d7e40a AS |
597 | kfree(state->stack); |
598 | kfree(state); | |
599 | } | |
600 | ||
1969db47 AS |
601 | static void free_verifier_state(struct bpf_verifier_state *state, |
602 | bool free_self) | |
638f5b90 | 603 | { |
f4d7e40a AS |
604 | int i; |
605 | ||
606 | for (i = 0; i <= state->curframe; i++) { | |
607 | free_func_state(state->frame[i]); | |
608 | state->frame[i] = NULL; | |
609 | } | |
1969db47 AS |
610 | if (free_self) |
611 | kfree(state); | |
638f5b90 AS |
612 | } |
613 | ||
614 | /* copy verifier state from src to dst growing dst stack space | |
615 | * when necessary to accommodate larger src stack | |
616 | */ | |
f4d7e40a AS |
617 | static int copy_func_state(struct bpf_func_state *dst, |
618 | const struct bpf_func_state *src) | |
638f5b90 AS |
619 | { |
620 | int err; | |
621 | ||
fd978bf7 JS |
622 | err = realloc_func_state(dst, src->allocated_stack, src->acquired_refs, |
623 | false); | |
624 | if (err) | |
625 | return err; | |
626 | memcpy(dst, src, offsetof(struct bpf_func_state, acquired_refs)); | |
627 | err = copy_reference_state(dst, src); | |
638f5b90 AS |
628 | if (err) |
629 | return err; | |
638f5b90 AS |
630 | return copy_stack_state(dst, src); |
631 | } | |
632 | ||
f4d7e40a AS |
633 | static int copy_verifier_state(struct bpf_verifier_state *dst_state, |
634 | const struct bpf_verifier_state *src) | |
635 | { | |
636 | struct bpf_func_state *dst; | |
637 | int i, err; | |
638 | ||
639 | /* if dst has more stack frames then src frame, free them */ | |
640 | for (i = src->curframe + 1; i <= dst_state->curframe; i++) { | |
641 | free_func_state(dst_state->frame[i]); | |
642 | dst_state->frame[i] = NULL; | |
643 | } | |
644 | dst_state->curframe = src->curframe; | |
f4d7e40a AS |
645 | for (i = 0; i <= src->curframe; i++) { |
646 | dst = dst_state->frame[i]; | |
647 | if (!dst) { | |
648 | dst = kzalloc(sizeof(*dst), GFP_KERNEL); | |
649 | if (!dst) | |
650 | return -ENOMEM; | |
651 | dst_state->frame[i] = dst; | |
652 | } | |
653 | err = copy_func_state(dst, src->frame[i]); | |
654 | if (err) | |
655 | return err; | |
656 | } | |
657 | return 0; | |
658 | } | |
659 | ||
638f5b90 AS |
660 | static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx, |
661 | int *insn_idx) | |
662 | { | |
663 | struct bpf_verifier_state *cur = env->cur_state; | |
664 | struct bpf_verifier_stack_elem *elem, *head = env->head; | |
665 | int err; | |
17a52670 AS |
666 | |
667 | if (env->head == NULL) | |
638f5b90 | 668 | return -ENOENT; |
17a52670 | 669 | |
638f5b90 AS |
670 | if (cur) { |
671 | err = copy_verifier_state(cur, &head->st); | |
672 | if (err) | |
673 | return err; | |
674 | } | |
675 | if (insn_idx) | |
676 | *insn_idx = head->insn_idx; | |
17a52670 | 677 | if (prev_insn_idx) |
638f5b90 AS |
678 | *prev_insn_idx = head->prev_insn_idx; |
679 | elem = head->next; | |
1969db47 | 680 | free_verifier_state(&head->st, false); |
638f5b90 | 681 | kfree(head); |
17a52670 AS |
682 | env->head = elem; |
683 | env->stack_size--; | |
638f5b90 | 684 | return 0; |
17a52670 AS |
685 | } |
686 | ||
58e2af8b JK |
687 | static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, |
688 | int insn_idx, int prev_insn_idx) | |
17a52670 | 689 | { |
638f5b90 | 690 | struct bpf_verifier_state *cur = env->cur_state; |
58e2af8b | 691 | struct bpf_verifier_stack_elem *elem; |
638f5b90 | 692 | int err; |
17a52670 | 693 | |
638f5b90 | 694 | elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL); |
17a52670 AS |
695 | if (!elem) |
696 | goto err; | |
697 | ||
17a52670 AS |
698 | elem->insn_idx = insn_idx; |
699 | elem->prev_insn_idx = prev_insn_idx; | |
700 | elem->next = env->head; | |
701 | env->head = elem; | |
702 | env->stack_size++; | |
1969db47 AS |
703 | err = copy_verifier_state(&elem->st, cur); |
704 | if (err) | |
705 | goto err; | |
07016151 | 706 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_STACK) { |
61bd5218 | 707 | verbose(env, "BPF program is too complex\n"); |
17a52670 AS |
708 | goto err; |
709 | } | |
710 | return &elem->st; | |
711 | err: | |
5896351e AS |
712 | free_verifier_state(env->cur_state, true); |
713 | env->cur_state = NULL; | |
17a52670 | 714 | /* pop all elements and return */ |
638f5b90 | 715 | while (!pop_stack(env, NULL, NULL)); |
17a52670 AS |
716 | return NULL; |
717 | } | |
718 | ||
719 | #define CALLER_SAVED_REGS 6 | |
720 | static const int caller_saved[CALLER_SAVED_REGS] = { | |
721 | BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5 | |
722 | }; | |
723 | ||
f1174f77 EC |
724 | static void __mark_reg_not_init(struct bpf_reg_state *reg); |
725 | ||
b03c9f9f EC |
726 | /* Mark the unknown part of a register (variable offset or scalar value) as |
727 | * known to have the value @imm. | |
728 | */ | |
729 | static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm) | |
730 | { | |
a9c676bc AS |
731 | /* Clear id, off, and union(map_ptr, range) */ |
732 | memset(((u8 *)reg) + sizeof(reg->type), 0, | |
733 | offsetof(struct bpf_reg_state, var_off) - sizeof(reg->type)); | |
b03c9f9f EC |
734 | reg->var_off = tnum_const(imm); |
735 | reg->smin_value = (s64)imm; | |
736 | reg->smax_value = (s64)imm; | |
737 | reg->umin_value = imm; | |
738 | reg->umax_value = imm; | |
739 | } | |
740 | ||
f1174f77 EC |
741 | /* Mark the 'variable offset' part of a register as zero. This should be |
742 | * used only on registers holding a pointer type. | |
743 | */ | |
744 | static void __mark_reg_known_zero(struct bpf_reg_state *reg) | |
a9789ef9 | 745 | { |
b03c9f9f | 746 | __mark_reg_known(reg, 0); |
f1174f77 | 747 | } |
a9789ef9 | 748 | |
cc2b14d5 AS |
749 | static void __mark_reg_const_zero(struct bpf_reg_state *reg) |
750 | { | |
751 | __mark_reg_known(reg, 0); | |
cc2b14d5 AS |
752 | reg->type = SCALAR_VALUE; |
753 | } | |
754 | ||
61bd5218 JK |
755 | static void mark_reg_known_zero(struct bpf_verifier_env *env, |
756 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
757 | { |
758 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 759 | verbose(env, "mark_reg_known_zero(regs, %u)\n", regno); |
f1174f77 EC |
760 | /* Something bad happened, let's kill all regs */ |
761 | for (regno = 0; regno < MAX_BPF_REG; regno++) | |
762 | __mark_reg_not_init(regs + regno); | |
763 | return; | |
764 | } | |
765 | __mark_reg_known_zero(regs + regno); | |
766 | } | |
767 | ||
de8f3a83 DB |
768 | static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg) |
769 | { | |
770 | return type_is_pkt_pointer(reg->type); | |
771 | } | |
772 | ||
773 | static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg) | |
774 | { | |
775 | return reg_is_pkt_pointer(reg) || | |
776 | reg->type == PTR_TO_PACKET_END; | |
777 | } | |
778 | ||
779 | /* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */ | |
780 | static bool reg_is_init_pkt_pointer(const struct bpf_reg_state *reg, | |
781 | enum bpf_reg_type which) | |
782 | { | |
783 | /* The register can already have a range from prior markings. | |
784 | * This is fine as long as it hasn't been advanced from its | |
785 | * origin. | |
786 | */ | |
787 | return reg->type == which && | |
788 | reg->id == 0 && | |
789 | reg->off == 0 && | |
790 | tnum_equals_const(reg->var_off, 0); | |
791 | } | |
792 | ||
b03c9f9f EC |
793 | /* Attempts to improve min/max values based on var_off information */ |
794 | static void __update_reg_bounds(struct bpf_reg_state *reg) | |
795 | { | |
796 | /* min signed is max(sign bit) | min(other bits) */ | |
797 | reg->smin_value = max_t(s64, reg->smin_value, | |
798 | reg->var_off.value | (reg->var_off.mask & S64_MIN)); | |
799 | /* max signed is min(sign bit) | max(other bits) */ | |
800 | reg->smax_value = min_t(s64, reg->smax_value, | |
801 | reg->var_off.value | (reg->var_off.mask & S64_MAX)); | |
802 | reg->umin_value = max(reg->umin_value, reg->var_off.value); | |
803 | reg->umax_value = min(reg->umax_value, | |
804 | reg->var_off.value | reg->var_off.mask); | |
805 | } | |
806 | ||
807 | /* Uses signed min/max values to inform unsigned, and vice-versa */ | |
808 | static void __reg_deduce_bounds(struct bpf_reg_state *reg) | |
809 | { | |
810 | /* Learn sign from signed bounds. | |
811 | * If we cannot cross the sign boundary, then signed and unsigned bounds | |
812 | * are the same, so combine. This works even in the negative case, e.g. | |
813 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. | |
814 | */ | |
815 | if (reg->smin_value >= 0 || reg->smax_value < 0) { | |
816 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, | |
817 | reg->umin_value); | |
818 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, | |
819 | reg->umax_value); | |
820 | return; | |
821 | } | |
822 | /* Learn sign from unsigned bounds. Signed bounds cross the sign | |
823 | * boundary, so we must be careful. | |
824 | */ | |
825 | if ((s64)reg->umax_value >= 0) { | |
826 | /* Positive. We can't learn anything from the smin, but smax | |
827 | * is positive, hence safe. | |
828 | */ | |
829 | reg->smin_value = reg->umin_value; | |
830 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, | |
831 | reg->umax_value); | |
832 | } else if ((s64)reg->umin_value < 0) { | |
833 | /* Negative. We can't learn anything from the smax, but smin | |
834 | * is negative, hence safe. | |
835 | */ | |
836 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, | |
837 | reg->umin_value); | |
838 | reg->smax_value = reg->umax_value; | |
839 | } | |
840 | } | |
841 | ||
842 | /* Attempts to improve var_off based on unsigned min/max information */ | |
843 | static void __reg_bound_offset(struct bpf_reg_state *reg) | |
844 | { | |
845 | reg->var_off = tnum_intersect(reg->var_off, | |
846 | tnum_range(reg->umin_value, | |
847 | reg->umax_value)); | |
848 | } | |
849 | ||
850 | /* Reset the min/max bounds of a register */ | |
851 | static void __mark_reg_unbounded(struct bpf_reg_state *reg) | |
852 | { | |
853 | reg->smin_value = S64_MIN; | |
854 | reg->smax_value = S64_MAX; | |
855 | reg->umin_value = 0; | |
856 | reg->umax_value = U64_MAX; | |
857 | } | |
858 | ||
f1174f77 EC |
859 | /* Mark a register as having a completely unknown (scalar) value. */ |
860 | static void __mark_reg_unknown(struct bpf_reg_state *reg) | |
861 | { | |
a9c676bc AS |
862 | /* |
863 | * Clear type, id, off, and union(map_ptr, range) and | |
864 | * padding between 'type' and union | |
865 | */ | |
866 | memset(reg, 0, offsetof(struct bpf_reg_state, var_off)); | |
f1174f77 | 867 | reg->type = SCALAR_VALUE; |
f1174f77 | 868 | reg->var_off = tnum_unknown; |
f4d7e40a | 869 | reg->frameno = 0; |
b03c9f9f | 870 | __mark_reg_unbounded(reg); |
f1174f77 EC |
871 | } |
872 | ||
61bd5218 JK |
873 | static void mark_reg_unknown(struct bpf_verifier_env *env, |
874 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
875 | { |
876 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 877 | verbose(env, "mark_reg_unknown(regs, %u)\n", regno); |
19ceb417 AS |
878 | /* Something bad happened, let's kill all regs except FP */ |
879 | for (regno = 0; regno < BPF_REG_FP; regno++) | |
f1174f77 EC |
880 | __mark_reg_not_init(regs + regno); |
881 | return; | |
882 | } | |
883 | __mark_reg_unknown(regs + regno); | |
884 | } | |
885 | ||
886 | static void __mark_reg_not_init(struct bpf_reg_state *reg) | |
887 | { | |
888 | __mark_reg_unknown(reg); | |
889 | reg->type = NOT_INIT; | |
890 | } | |
891 | ||
61bd5218 JK |
892 | static void mark_reg_not_init(struct bpf_verifier_env *env, |
893 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
894 | { |
895 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 896 | verbose(env, "mark_reg_not_init(regs, %u)\n", regno); |
19ceb417 AS |
897 | /* Something bad happened, let's kill all regs except FP */ |
898 | for (regno = 0; regno < BPF_REG_FP; regno++) | |
f1174f77 EC |
899 | __mark_reg_not_init(regs + regno); |
900 | return; | |
901 | } | |
902 | __mark_reg_not_init(regs + regno); | |
a9789ef9 DB |
903 | } |
904 | ||
61bd5218 | 905 | static void init_reg_state(struct bpf_verifier_env *env, |
f4d7e40a | 906 | struct bpf_func_state *state) |
17a52670 | 907 | { |
f4d7e40a | 908 | struct bpf_reg_state *regs = state->regs; |
17a52670 AS |
909 | int i; |
910 | ||
dc503a8a | 911 | for (i = 0; i < MAX_BPF_REG; i++) { |
61bd5218 | 912 | mark_reg_not_init(env, regs, i); |
dc503a8a | 913 | regs[i].live = REG_LIVE_NONE; |
679c782d | 914 | regs[i].parent = NULL; |
dc503a8a | 915 | } |
17a52670 AS |
916 | |
917 | /* frame pointer */ | |
f1174f77 | 918 | regs[BPF_REG_FP].type = PTR_TO_STACK; |
61bd5218 | 919 | mark_reg_known_zero(env, regs, BPF_REG_FP); |
f4d7e40a | 920 | regs[BPF_REG_FP].frameno = state->frameno; |
17a52670 AS |
921 | |
922 | /* 1st arg to a function */ | |
923 | regs[BPF_REG_1].type = PTR_TO_CTX; | |
61bd5218 | 924 | mark_reg_known_zero(env, regs, BPF_REG_1); |
6760bf2d DB |
925 | } |
926 | ||
f4d7e40a AS |
927 | #define BPF_MAIN_FUNC (-1) |
928 | static void init_func_state(struct bpf_verifier_env *env, | |
929 | struct bpf_func_state *state, | |
930 | int callsite, int frameno, int subprogno) | |
931 | { | |
932 | state->callsite = callsite; | |
933 | state->frameno = frameno; | |
934 | state->subprogno = subprogno; | |
935 | init_reg_state(env, state); | |
936 | } | |
937 | ||
17a52670 AS |
938 | enum reg_arg_type { |
939 | SRC_OP, /* register is used as source operand */ | |
940 | DST_OP, /* register is used as destination operand */ | |
941 | DST_OP_NO_MARK /* same as above, check only, don't mark */ | |
942 | }; | |
943 | ||
cc8b0b92 AS |
944 | static int cmp_subprogs(const void *a, const void *b) |
945 | { | |
9c8105bd JW |
946 | return ((struct bpf_subprog_info *)a)->start - |
947 | ((struct bpf_subprog_info *)b)->start; | |
cc8b0b92 AS |
948 | } |
949 | ||
950 | static int find_subprog(struct bpf_verifier_env *env, int off) | |
951 | { | |
9c8105bd | 952 | struct bpf_subprog_info *p; |
cc8b0b92 | 953 | |
9c8105bd JW |
954 | p = bsearch(&off, env->subprog_info, env->subprog_cnt, |
955 | sizeof(env->subprog_info[0]), cmp_subprogs); | |
cc8b0b92 AS |
956 | if (!p) |
957 | return -ENOENT; | |
9c8105bd | 958 | return p - env->subprog_info; |
cc8b0b92 AS |
959 | |
960 | } | |
961 | ||
962 | static int add_subprog(struct bpf_verifier_env *env, int off) | |
963 | { | |
964 | int insn_cnt = env->prog->len; | |
965 | int ret; | |
966 | ||
967 | if (off >= insn_cnt || off < 0) { | |
968 | verbose(env, "call to invalid destination\n"); | |
969 | return -EINVAL; | |
970 | } | |
971 | ret = find_subprog(env, off); | |
972 | if (ret >= 0) | |
973 | return 0; | |
4cb3d99c | 974 | if (env->subprog_cnt >= BPF_MAX_SUBPROGS) { |
cc8b0b92 AS |
975 | verbose(env, "too many subprograms\n"); |
976 | return -E2BIG; | |
977 | } | |
9c8105bd JW |
978 | env->subprog_info[env->subprog_cnt++].start = off; |
979 | sort(env->subprog_info, env->subprog_cnt, | |
980 | sizeof(env->subprog_info[0]), cmp_subprogs, NULL); | |
cc8b0b92 AS |
981 | return 0; |
982 | } | |
983 | ||
984 | static int check_subprogs(struct bpf_verifier_env *env) | |
985 | { | |
986 | int i, ret, subprog_start, subprog_end, off, cur_subprog = 0; | |
9c8105bd | 987 | struct bpf_subprog_info *subprog = env->subprog_info; |
cc8b0b92 AS |
988 | struct bpf_insn *insn = env->prog->insnsi; |
989 | int insn_cnt = env->prog->len; | |
990 | ||
f910cefa JW |
991 | /* Add entry function. */ |
992 | ret = add_subprog(env, 0); | |
993 | if (ret < 0) | |
994 | return ret; | |
995 | ||
cc8b0b92 AS |
996 | /* determine subprog starts. The end is one before the next starts */ |
997 | for (i = 0; i < insn_cnt; i++) { | |
998 | if (insn[i].code != (BPF_JMP | BPF_CALL)) | |
999 | continue; | |
1000 | if (insn[i].src_reg != BPF_PSEUDO_CALL) | |
1001 | continue; | |
1002 | if (!env->allow_ptr_leaks) { | |
1003 | verbose(env, "function calls to other bpf functions are allowed for root only\n"); | |
1004 | return -EPERM; | |
1005 | } | |
1006 | if (bpf_prog_is_dev_bound(env->prog->aux)) { | |
e90004d5 | 1007 | verbose(env, "function calls in offloaded programs are not supported yet\n"); |
cc8b0b92 AS |
1008 | return -EINVAL; |
1009 | } | |
1010 | ret = add_subprog(env, i + insn[i].imm + 1); | |
1011 | if (ret < 0) | |
1012 | return ret; | |
1013 | } | |
1014 | ||
4cb3d99c JW |
1015 | /* Add a fake 'exit' subprog which could simplify subprog iteration |
1016 | * logic. 'subprog_cnt' should not be increased. | |
1017 | */ | |
1018 | subprog[env->subprog_cnt].start = insn_cnt; | |
1019 | ||
cc8b0b92 AS |
1020 | if (env->log.level > 1) |
1021 | for (i = 0; i < env->subprog_cnt; i++) | |
9c8105bd | 1022 | verbose(env, "func#%d @%d\n", i, subprog[i].start); |
cc8b0b92 AS |
1023 | |
1024 | /* now check that all jumps are within the same subprog */ | |
4cb3d99c JW |
1025 | subprog_start = subprog[cur_subprog].start; |
1026 | subprog_end = subprog[cur_subprog + 1].start; | |
cc8b0b92 AS |
1027 | for (i = 0; i < insn_cnt; i++) { |
1028 | u8 code = insn[i].code; | |
1029 | ||
1030 | if (BPF_CLASS(code) != BPF_JMP) | |
1031 | goto next; | |
1032 | if (BPF_OP(code) == BPF_EXIT || BPF_OP(code) == BPF_CALL) | |
1033 | goto next; | |
1034 | off = i + insn[i].off + 1; | |
1035 | if (off < subprog_start || off >= subprog_end) { | |
1036 | verbose(env, "jump out of range from insn %d to %d\n", i, off); | |
1037 | return -EINVAL; | |
1038 | } | |
1039 | next: | |
1040 | if (i == subprog_end - 1) { | |
1041 | /* to avoid fall-through from one subprog into another | |
1042 | * the last insn of the subprog should be either exit | |
1043 | * or unconditional jump back | |
1044 | */ | |
1045 | if (code != (BPF_JMP | BPF_EXIT) && | |
1046 | code != (BPF_JMP | BPF_JA)) { | |
1047 | verbose(env, "last insn is not an exit or jmp\n"); | |
1048 | return -EINVAL; | |
1049 | } | |
1050 | subprog_start = subprog_end; | |
4cb3d99c JW |
1051 | cur_subprog++; |
1052 | if (cur_subprog < env->subprog_cnt) | |
9c8105bd | 1053 | subprog_end = subprog[cur_subprog + 1].start; |
cc8b0b92 AS |
1054 | } |
1055 | } | |
1056 | return 0; | |
1057 | } | |
1058 | ||
679c782d EC |
1059 | /* Parentage chain of this register (or stack slot) should take care of all |
1060 | * issues like callee-saved registers, stack slot allocation time, etc. | |
1061 | */ | |
f4d7e40a | 1062 | static int mark_reg_read(struct bpf_verifier_env *env, |
679c782d EC |
1063 | const struct bpf_reg_state *state, |
1064 | struct bpf_reg_state *parent) | |
f4d7e40a AS |
1065 | { |
1066 | bool writes = parent == state->parent; /* Observe write marks */ | |
dc503a8a EC |
1067 | |
1068 | while (parent) { | |
1069 | /* if read wasn't screened by an earlier write ... */ | |
679c782d | 1070 | if (writes && state->live & REG_LIVE_WRITTEN) |
dc503a8a EC |
1071 | break; |
1072 | /* ... then we depend on parent's value */ | |
679c782d | 1073 | parent->live |= REG_LIVE_READ; |
dc503a8a EC |
1074 | state = parent; |
1075 | parent = state->parent; | |
f4d7e40a | 1076 | writes = true; |
dc503a8a | 1077 | } |
f4d7e40a | 1078 | return 0; |
dc503a8a EC |
1079 | } |
1080 | ||
1081 | static int check_reg_arg(struct bpf_verifier_env *env, u32 regno, | |
17a52670 AS |
1082 | enum reg_arg_type t) |
1083 | { | |
f4d7e40a AS |
1084 | struct bpf_verifier_state *vstate = env->cur_state; |
1085 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
1086 | struct bpf_reg_state *regs = state->regs; | |
dc503a8a | 1087 | |
17a52670 | 1088 | if (regno >= MAX_BPF_REG) { |
61bd5218 | 1089 | verbose(env, "R%d is invalid\n", regno); |
17a52670 AS |
1090 | return -EINVAL; |
1091 | } | |
1092 | ||
1093 | if (t == SRC_OP) { | |
1094 | /* check whether register used as source operand can be read */ | |
1095 | if (regs[regno].type == NOT_INIT) { | |
61bd5218 | 1096 | verbose(env, "R%d !read_ok\n", regno); |
17a52670 AS |
1097 | return -EACCES; |
1098 | } | |
679c782d EC |
1099 | /* We don't need to worry about FP liveness because it's read-only */ |
1100 | if (regno != BPF_REG_FP) | |
1101 | return mark_reg_read(env, ®s[regno], | |
1102 | regs[regno].parent); | |
17a52670 AS |
1103 | } else { |
1104 | /* check whether register used as dest operand can be written to */ | |
1105 | if (regno == BPF_REG_FP) { | |
61bd5218 | 1106 | verbose(env, "frame pointer is read only\n"); |
17a52670 AS |
1107 | return -EACCES; |
1108 | } | |
dc503a8a | 1109 | regs[regno].live |= REG_LIVE_WRITTEN; |
17a52670 | 1110 | if (t == DST_OP) |
61bd5218 | 1111 | mark_reg_unknown(env, regs, regno); |
17a52670 AS |
1112 | } |
1113 | return 0; | |
1114 | } | |
1115 | ||
1be7f75d AS |
1116 | static bool is_spillable_regtype(enum bpf_reg_type type) |
1117 | { | |
1118 | switch (type) { | |
1119 | case PTR_TO_MAP_VALUE: | |
1120 | case PTR_TO_MAP_VALUE_OR_NULL: | |
1121 | case PTR_TO_STACK: | |
1122 | case PTR_TO_CTX: | |
969bf05e | 1123 | case PTR_TO_PACKET: |
de8f3a83 | 1124 | case PTR_TO_PACKET_META: |
969bf05e | 1125 | case PTR_TO_PACKET_END: |
d58e468b | 1126 | case PTR_TO_FLOW_KEYS: |
1be7f75d | 1127 | case CONST_PTR_TO_MAP: |
c64b7983 JS |
1128 | case PTR_TO_SOCKET: |
1129 | case PTR_TO_SOCKET_OR_NULL: | |
1be7f75d AS |
1130 | return true; |
1131 | default: | |
1132 | return false; | |
1133 | } | |
1134 | } | |
1135 | ||
cc2b14d5 AS |
1136 | /* Does this register contain a constant zero? */ |
1137 | static bool register_is_null(struct bpf_reg_state *reg) | |
1138 | { | |
1139 | return reg->type == SCALAR_VALUE && tnum_equals_const(reg->var_off, 0); | |
1140 | } | |
1141 | ||
17a52670 AS |
1142 | /* check_stack_read/write functions track spill/fill of registers, |
1143 | * stack boundary and alignment are checked in check_mem_access() | |
1144 | */ | |
61bd5218 | 1145 | static int check_stack_write(struct bpf_verifier_env *env, |
f4d7e40a | 1146 | struct bpf_func_state *state, /* func where register points to */ |
af86ca4e | 1147 | int off, int size, int value_regno, int insn_idx) |
17a52670 | 1148 | { |
f4d7e40a | 1149 | struct bpf_func_state *cur; /* state of the current function */ |
638f5b90 | 1150 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err; |
f4d7e40a | 1151 | enum bpf_reg_type type; |
638f5b90 | 1152 | |
f4d7e40a | 1153 | err = realloc_func_state(state, round_up(slot + 1, BPF_REG_SIZE), |
fd978bf7 | 1154 | state->acquired_refs, true); |
638f5b90 AS |
1155 | if (err) |
1156 | return err; | |
9c399760 AS |
1157 | /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0, |
1158 | * so it's aligned access and [off, off + size) are within stack limits | |
1159 | */ | |
638f5b90 AS |
1160 | if (!env->allow_ptr_leaks && |
1161 | state->stack[spi].slot_type[0] == STACK_SPILL && | |
1162 | size != BPF_REG_SIZE) { | |
1163 | verbose(env, "attempt to corrupt spilled pointer on stack\n"); | |
1164 | return -EACCES; | |
1165 | } | |
17a52670 | 1166 | |
f4d7e40a | 1167 | cur = env->cur_state->frame[env->cur_state->curframe]; |
17a52670 | 1168 | if (value_regno >= 0 && |
f4d7e40a | 1169 | is_spillable_regtype((type = cur->regs[value_regno].type))) { |
17a52670 AS |
1170 | |
1171 | /* register containing pointer is being spilled into stack */ | |
9c399760 | 1172 | if (size != BPF_REG_SIZE) { |
61bd5218 | 1173 | verbose(env, "invalid size of register spill\n"); |
17a52670 AS |
1174 | return -EACCES; |
1175 | } | |
1176 | ||
f4d7e40a AS |
1177 | if (state != cur && type == PTR_TO_STACK) { |
1178 | verbose(env, "cannot spill pointers to stack into stack frame of the caller\n"); | |
1179 | return -EINVAL; | |
1180 | } | |
1181 | ||
17a52670 | 1182 | /* save register state */ |
f4d7e40a | 1183 | state->stack[spi].spilled_ptr = cur->regs[value_regno]; |
638f5b90 | 1184 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; |
17a52670 | 1185 | |
af86ca4e AS |
1186 | for (i = 0; i < BPF_REG_SIZE; i++) { |
1187 | if (state->stack[spi].slot_type[i] == STACK_MISC && | |
1188 | !env->allow_ptr_leaks) { | |
1189 | int *poff = &env->insn_aux_data[insn_idx].sanitize_stack_off; | |
1190 | int soff = (-spi - 1) * BPF_REG_SIZE; | |
1191 | ||
1192 | /* detected reuse of integer stack slot with a pointer | |
1193 | * which means either llvm is reusing stack slot or | |
1194 | * an attacker is trying to exploit CVE-2018-3639 | |
1195 | * (speculative store bypass) | |
1196 | * Have to sanitize that slot with preemptive | |
1197 | * store of zero. | |
1198 | */ | |
1199 | if (*poff && *poff != soff) { | |
1200 | /* disallow programs where single insn stores | |
1201 | * into two different stack slots, since verifier | |
1202 | * cannot sanitize them | |
1203 | */ | |
1204 | verbose(env, | |
1205 | "insn %d cannot access two stack slots fp%d and fp%d", | |
1206 | insn_idx, *poff, soff); | |
1207 | return -EINVAL; | |
1208 | } | |
1209 | *poff = soff; | |
1210 | } | |
638f5b90 | 1211 | state->stack[spi].slot_type[i] = STACK_SPILL; |
af86ca4e | 1212 | } |
9c399760 | 1213 | } else { |
cc2b14d5 AS |
1214 | u8 type = STACK_MISC; |
1215 | ||
679c782d EC |
1216 | /* regular write of data into stack destroys any spilled ptr */ |
1217 | state->stack[spi].spilled_ptr.type = NOT_INIT; | |
9c399760 | 1218 | |
cc2b14d5 AS |
1219 | /* only mark the slot as written if all 8 bytes were written |
1220 | * otherwise read propagation may incorrectly stop too soon | |
1221 | * when stack slots are partially written. | |
1222 | * This heuristic means that read propagation will be | |
1223 | * conservative, since it will add reg_live_read marks | |
1224 | * to stack slots all the way to first state when programs | |
1225 | * writes+reads less than 8 bytes | |
1226 | */ | |
1227 | if (size == BPF_REG_SIZE) | |
1228 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; | |
1229 | ||
1230 | /* when we zero initialize stack slots mark them as such */ | |
1231 | if (value_regno >= 0 && | |
1232 | register_is_null(&cur->regs[value_regno])) | |
1233 | type = STACK_ZERO; | |
1234 | ||
9c399760 | 1235 | for (i = 0; i < size; i++) |
638f5b90 | 1236 | state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] = |
cc2b14d5 | 1237 | type; |
17a52670 AS |
1238 | } |
1239 | return 0; | |
1240 | } | |
1241 | ||
61bd5218 | 1242 | static int check_stack_read(struct bpf_verifier_env *env, |
f4d7e40a AS |
1243 | struct bpf_func_state *reg_state /* func where register points to */, |
1244 | int off, int size, int value_regno) | |
17a52670 | 1245 | { |
f4d7e40a AS |
1246 | struct bpf_verifier_state *vstate = env->cur_state; |
1247 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
638f5b90 AS |
1248 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE; |
1249 | u8 *stype; | |
17a52670 | 1250 | |
f4d7e40a | 1251 | if (reg_state->allocated_stack <= slot) { |
638f5b90 AS |
1252 | verbose(env, "invalid read from stack off %d+0 size %d\n", |
1253 | off, size); | |
1254 | return -EACCES; | |
1255 | } | |
f4d7e40a | 1256 | stype = reg_state->stack[spi].slot_type; |
17a52670 | 1257 | |
638f5b90 | 1258 | if (stype[0] == STACK_SPILL) { |
9c399760 | 1259 | if (size != BPF_REG_SIZE) { |
61bd5218 | 1260 | verbose(env, "invalid size of register spill\n"); |
17a52670 AS |
1261 | return -EACCES; |
1262 | } | |
9c399760 | 1263 | for (i = 1; i < BPF_REG_SIZE; i++) { |
638f5b90 | 1264 | if (stype[(slot - i) % BPF_REG_SIZE] != STACK_SPILL) { |
61bd5218 | 1265 | verbose(env, "corrupted spill memory\n"); |
17a52670 AS |
1266 | return -EACCES; |
1267 | } | |
1268 | } | |
1269 | ||
dc503a8a | 1270 | if (value_regno >= 0) { |
17a52670 | 1271 | /* restore register state from stack */ |
f4d7e40a | 1272 | state->regs[value_regno] = reg_state->stack[spi].spilled_ptr; |
2f18f62e AS |
1273 | /* mark reg as written since spilled pointer state likely |
1274 | * has its liveness marks cleared by is_state_visited() | |
1275 | * which resets stack/reg liveness for state transitions | |
1276 | */ | |
1277 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; | |
dc503a8a | 1278 | } |
679c782d EC |
1279 | mark_reg_read(env, ®_state->stack[spi].spilled_ptr, |
1280 | reg_state->stack[spi].spilled_ptr.parent); | |
17a52670 AS |
1281 | return 0; |
1282 | } else { | |
cc2b14d5 AS |
1283 | int zeros = 0; |
1284 | ||
17a52670 | 1285 | for (i = 0; i < size; i++) { |
cc2b14d5 AS |
1286 | if (stype[(slot - i) % BPF_REG_SIZE] == STACK_MISC) |
1287 | continue; | |
1288 | if (stype[(slot - i) % BPF_REG_SIZE] == STACK_ZERO) { | |
1289 | zeros++; | |
1290 | continue; | |
17a52670 | 1291 | } |
cc2b14d5 AS |
1292 | verbose(env, "invalid read from stack off %d+%d size %d\n", |
1293 | off, i, size); | |
1294 | return -EACCES; | |
1295 | } | |
679c782d EC |
1296 | mark_reg_read(env, ®_state->stack[spi].spilled_ptr, |
1297 | reg_state->stack[spi].spilled_ptr.parent); | |
cc2b14d5 AS |
1298 | if (value_regno >= 0) { |
1299 | if (zeros == size) { | |
1300 | /* any size read into register is zero extended, | |
1301 | * so the whole register == const_zero | |
1302 | */ | |
1303 | __mark_reg_const_zero(&state->regs[value_regno]); | |
1304 | } else { | |
1305 | /* have read misc data from the stack */ | |
1306 | mark_reg_unknown(env, state->regs, value_regno); | |
1307 | } | |
1308 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; | |
17a52670 | 1309 | } |
17a52670 AS |
1310 | return 0; |
1311 | } | |
1312 | } | |
1313 | ||
1314 | /* check read/write into map element returned by bpf_map_lookup_elem() */ | |
f1174f77 | 1315 | static int __check_map_access(struct bpf_verifier_env *env, u32 regno, int off, |
9fd29c08 | 1316 | int size, bool zero_size_allowed) |
17a52670 | 1317 | { |
638f5b90 AS |
1318 | struct bpf_reg_state *regs = cur_regs(env); |
1319 | struct bpf_map *map = regs[regno].map_ptr; | |
17a52670 | 1320 | |
9fd29c08 YS |
1321 | if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) || |
1322 | off + size > map->value_size) { | |
61bd5218 | 1323 | verbose(env, "invalid access to map value, value_size=%d off=%d size=%d\n", |
17a52670 AS |
1324 | map->value_size, off, size); |
1325 | return -EACCES; | |
1326 | } | |
1327 | return 0; | |
1328 | } | |
1329 | ||
f1174f77 EC |
1330 | /* check read/write into a map element with possible variable offset */ |
1331 | static int check_map_access(struct bpf_verifier_env *env, u32 regno, | |
9fd29c08 | 1332 | int off, int size, bool zero_size_allowed) |
dbcfe5f7 | 1333 | { |
f4d7e40a AS |
1334 | struct bpf_verifier_state *vstate = env->cur_state; |
1335 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
dbcfe5f7 GB |
1336 | struct bpf_reg_state *reg = &state->regs[regno]; |
1337 | int err; | |
1338 | ||
f1174f77 EC |
1339 | /* We may have adjusted the register to this map value, so we |
1340 | * need to try adding each of min_value and max_value to off | |
1341 | * to make sure our theoretical access will be safe. | |
dbcfe5f7 | 1342 | */ |
61bd5218 JK |
1343 | if (env->log.level) |
1344 | print_verifier_state(env, state); | |
dbcfe5f7 GB |
1345 | /* The minimum value is only important with signed |
1346 | * comparisons where we can't assume the floor of a | |
1347 | * value is 0. If we are using signed variables for our | |
1348 | * index'es we need to make sure that whatever we use | |
1349 | * will have a set floor within our range. | |
1350 | */ | |
b03c9f9f | 1351 | if (reg->smin_value < 0) { |
61bd5218 | 1352 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
dbcfe5f7 GB |
1353 | regno); |
1354 | return -EACCES; | |
1355 | } | |
9fd29c08 YS |
1356 | err = __check_map_access(env, regno, reg->smin_value + off, size, |
1357 | zero_size_allowed); | |
dbcfe5f7 | 1358 | if (err) { |
61bd5218 JK |
1359 | verbose(env, "R%d min value is outside of the array range\n", |
1360 | regno); | |
dbcfe5f7 GB |
1361 | return err; |
1362 | } | |
1363 | ||
b03c9f9f EC |
1364 | /* If we haven't set a max value then we need to bail since we can't be |
1365 | * sure we won't do bad things. | |
1366 | * If reg->umax_value + off could overflow, treat that as unbounded too. | |
dbcfe5f7 | 1367 | */ |
b03c9f9f | 1368 | if (reg->umax_value >= BPF_MAX_VAR_OFF) { |
61bd5218 | 1369 | verbose(env, "R%d unbounded memory access, make sure to bounds check any array access into a map\n", |
dbcfe5f7 GB |
1370 | regno); |
1371 | return -EACCES; | |
1372 | } | |
9fd29c08 YS |
1373 | err = __check_map_access(env, regno, reg->umax_value + off, size, |
1374 | zero_size_allowed); | |
f1174f77 | 1375 | if (err) |
61bd5218 JK |
1376 | verbose(env, "R%d max value is outside of the array range\n", |
1377 | regno); | |
f1174f77 | 1378 | return err; |
dbcfe5f7 GB |
1379 | } |
1380 | ||
969bf05e AS |
1381 | #define MAX_PACKET_OFF 0xffff |
1382 | ||
58e2af8b | 1383 | static bool may_access_direct_pkt_data(struct bpf_verifier_env *env, |
3a0af8fd TG |
1384 | const struct bpf_call_arg_meta *meta, |
1385 | enum bpf_access_type t) | |
4acf6c0b | 1386 | { |
36bbef52 | 1387 | switch (env->prog->type) { |
3a0af8fd TG |
1388 | case BPF_PROG_TYPE_LWT_IN: |
1389 | case BPF_PROG_TYPE_LWT_OUT: | |
004d4b27 | 1390 | case BPF_PROG_TYPE_LWT_SEG6LOCAL: |
2dbb9b9e | 1391 | case BPF_PROG_TYPE_SK_REUSEPORT: |
3a0af8fd TG |
1392 | /* dst_input() and dst_output() can't write for now */ |
1393 | if (t == BPF_WRITE) | |
1394 | return false; | |
7e57fbb2 | 1395 | /* fallthrough */ |
36bbef52 DB |
1396 | case BPF_PROG_TYPE_SCHED_CLS: |
1397 | case BPF_PROG_TYPE_SCHED_ACT: | |
4acf6c0b | 1398 | case BPF_PROG_TYPE_XDP: |
3a0af8fd | 1399 | case BPF_PROG_TYPE_LWT_XMIT: |
8a31db56 | 1400 | case BPF_PROG_TYPE_SK_SKB: |
4f738adb | 1401 | case BPF_PROG_TYPE_SK_MSG: |
d58e468b | 1402 | case BPF_PROG_TYPE_FLOW_DISSECTOR: |
36bbef52 DB |
1403 | if (meta) |
1404 | return meta->pkt_access; | |
1405 | ||
1406 | env->seen_direct_write = true; | |
4acf6c0b BB |
1407 | return true; |
1408 | default: | |
1409 | return false; | |
1410 | } | |
1411 | } | |
1412 | ||
f1174f77 | 1413 | static int __check_packet_access(struct bpf_verifier_env *env, u32 regno, |
9fd29c08 | 1414 | int off, int size, bool zero_size_allowed) |
969bf05e | 1415 | { |
638f5b90 | 1416 | struct bpf_reg_state *regs = cur_regs(env); |
58e2af8b | 1417 | struct bpf_reg_state *reg = ®s[regno]; |
969bf05e | 1418 | |
9fd29c08 YS |
1419 | if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) || |
1420 | (u64)off + size > reg->range) { | |
61bd5218 | 1421 | verbose(env, "invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n", |
d91b28ed | 1422 | off, size, regno, reg->id, reg->off, reg->range); |
969bf05e AS |
1423 | return -EACCES; |
1424 | } | |
1425 | return 0; | |
1426 | } | |
1427 | ||
f1174f77 | 1428 | static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off, |
9fd29c08 | 1429 | int size, bool zero_size_allowed) |
f1174f77 | 1430 | { |
638f5b90 | 1431 | struct bpf_reg_state *regs = cur_regs(env); |
f1174f77 EC |
1432 | struct bpf_reg_state *reg = ®s[regno]; |
1433 | int err; | |
1434 | ||
1435 | /* We may have added a variable offset to the packet pointer; but any | |
1436 | * reg->range we have comes after that. We are only checking the fixed | |
1437 | * offset. | |
1438 | */ | |
1439 | ||
1440 | /* We don't allow negative numbers, because we aren't tracking enough | |
1441 | * detail to prove they're safe. | |
1442 | */ | |
b03c9f9f | 1443 | if (reg->smin_value < 0) { |
61bd5218 | 1444 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
f1174f77 EC |
1445 | regno); |
1446 | return -EACCES; | |
1447 | } | |
9fd29c08 | 1448 | err = __check_packet_access(env, regno, off, size, zero_size_allowed); |
f1174f77 | 1449 | if (err) { |
61bd5218 | 1450 | verbose(env, "R%d offset is outside of the packet\n", regno); |
f1174f77 EC |
1451 | return err; |
1452 | } | |
1453 | return err; | |
1454 | } | |
1455 | ||
1456 | /* check access to 'struct bpf_context' fields. Supports fixed offsets only */ | |
31fd8581 | 1457 | static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off, int size, |
19de99f7 | 1458 | enum bpf_access_type t, enum bpf_reg_type *reg_type) |
17a52670 | 1459 | { |
f96da094 DB |
1460 | struct bpf_insn_access_aux info = { |
1461 | .reg_type = *reg_type, | |
1462 | }; | |
31fd8581 | 1463 | |
4f9218aa | 1464 | if (env->ops->is_valid_access && |
5e43f899 | 1465 | env->ops->is_valid_access(off, size, t, env->prog, &info)) { |
f96da094 DB |
1466 | /* A non zero info.ctx_field_size indicates that this field is a |
1467 | * candidate for later verifier transformation to load the whole | |
1468 | * field and then apply a mask when accessed with a narrower | |
1469 | * access than actual ctx access size. A zero info.ctx_field_size | |
1470 | * will only allow for whole field access and rejects any other | |
1471 | * type of narrower access. | |
31fd8581 | 1472 | */ |
23994631 | 1473 | *reg_type = info.reg_type; |
31fd8581 | 1474 | |
4f9218aa | 1475 | env->insn_aux_data[insn_idx].ctx_field_size = info.ctx_field_size; |
32bbe007 AS |
1476 | /* remember the offset of last byte accessed in ctx */ |
1477 | if (env->prog->aux->max_ctx_offset < off + size) | |
1478 | env->prog->aux->max_ctx_offset = off + size; | |
17a52670 | 1479 | return 0; |
32bbe007 | 1480 | } |
17a52670 | 1481 | |
61bd5218 | 1482 | verbose(env, "invalid bpf_context access off=%d size=%d\n", off, size); |
17a52670 AS |
1483 | return -EACCES; |
1484 | } | |
1485 | ||
d58e468b PP |
1486 | static int check_flow_keys_access(struct bpf_verifier_env *env, int off, |
1487 | int size) | |
1488 | { | |
1489 | if (size < 0 || off < 0 || | |
1490 | (u64)off + size > sizeof(struct bpf_flow_keys)) { | |
1491 | verbose(env, "invalid access to flow keys off=%d size=%d\n", | |
1492 | off, size); | |
1493 | return -EACCES; | |
1494 | } | |
1495 | return 0; | |
1496 | } | |
1497 | ||
c64b7983 JS |
1498 | static int check_sock_access(struct bpf_verifier_env *env, u32 regno, int off, |
1499 | int size, enum bpf_access_type t) | |
1500 | { | |
1501 | struct bpf_reg_state *regs = cur_regs(env); | |
1502 | struct bpf_reg_state *reg = ®s[regno]; | |
1503 | struct bpf_insn_access_aux info; | |
1504 | ||
1505 | if (reg->smin_value < 0) { | |
1506 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", | |
1507 | regno); | |
1508 | return -EACCES; | |
1509 | } | |
1510 | ||
1511 | if (!bpf_sock_is_valid_access(off, size, t, &info)) { | |
1512 | verbose(env, "invalid bpf_sock access off=%d size=%d\n", | |
1513 | off, size); | |
1514 | return -EACCES; | |
1515 | } | |
1516 | ||
1517 | return 0; | |
1518 | } | |
1519 | ||
4cabc5b1 DB |
1520 | static bool __is_pointer_value(bool allow_ptr_leaks, |
1521 | const struct bpf_reg_state *reg) | |
1be7f75d | 1522 | { |
4cabc5b1 | 1523 | if (allow_ptr_leaks) |
1be7f75d AS |
1524 | return false; |
1525 | ||
f1174f77 | 1526 | return reg->type != SCALAR_VALUE; |
1be7f75d AS |
1527 | } |
1528 | ||
4cabc5b1 DB |
1529 | static bool is_pointer_value(struct bpf_verifier_env *env, int regno) |
1530 | { | |
638f5b90 | 1531 | return __is_pointer_value(env->allow_ptr_leaks, cur_regs(env) + regno); |
4cabc5b1 DB |
1532 | } |
1533 | ||
f37a8cb8 DB |
1534 | static bool is_ctx_reg(struct bpf_verifier_env *env, int regno) |
1535 | { | |
1536 | const struct bpf_reg_state *reg = cur_regs(env) + regno; | |
1537 | ||
fd978bf7 JS |
1538 | return reg->type == PTR_TO_CTX || |
1539 | reg->type == PTR_TO_SOCKET; | |
f37a8cb8 DB |
1540 | } |
1541 | ||
ca369602 DB |
1542 | static bool is_pkt_reg(struct bpf_verifier_env *env, int regno) |
1543 | { | |
1544 | const struct bpf_reg_state *reg = cur_regs(env) + regno; | |
1545 | ||
1546 | return type_is_pkt_pointer(reg->type); | |
1547 | } | |
1548 | ||
61bd5218 JK |
1549 | static int check_pkt_ptr_alignment(struct bpf_verifier_env *env, |
1550 | const struct bpf_reg_state *reg, | |
d1174416 | 1551 | int off, int size, bool strict) |
969bf05e | 1552 | { |
f1174f77 | 1553 | struct tnum reg_off; |
e07b98d9 | 1554 | int ip_align; |
d1174416 DM |
1555 | |
1556 | /* Byte size accesses are always allowed. */ | |
1557 | if (!strict || size == 1) | |
1558 | return 0; | |
1559 | ||
e4eda884 DM |
1560 | /* For platforms that do not have a Kconfig enabling |
1561 | * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of | |
1562 | * NET_IP_ALIGN is universally set to '2'. And on platforms | |
1563 | * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get | |
1564 | * to this code only in strict mode where we want to emulate | |
1565 | * the NET_IP_ALIGN==2 checking. Therefore use an | |
1566 | * unconditional IP align value of '2'. | |
e07b98d9 | 1567 | */ |
e4eda884 | 1568 | ip_align = 2; |
f1174f77 EC |
1569 | |
1570 | reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off)); | |
1571 | if (!tnum_is_aligned(reg_off, size)) { | |
1572 | char tn_buf[48]; | |
1573 | ||
1574 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
61bd5218 JK |
1575 | verbose(env, |
1576 | "misaligned packet access off %d+%s+%d+%d size %d\n", | |
f1174f77 | 1577 | ip_align, tn_buf, reg->off, off, size); |
969bf05e AS |
1578 | return -EACCES; |
1579 | } | |
79adffcd | 1580 | |
969bf05e AS |
1581 | return 0; |
1582 | } | |
1583 | ||
61bd5218 JK |
1584 | static int check_generic_ptr_alignment(struct bpf_verifier_env *env, |
1585 | const struct bpf_reg_state *reg, | |
f1174f77 EC |
1586 | const char *pointer_desc, |
1587 | int off, int size, bool strict) | |
79adffcd | 1588 | { |
f1174f77 EC |
1589 | struct tnum reg_off; |
1590 | ||
1591 | /* Byte size accesses are always allowed. */ | |
1592 | if (!strict || size == 1) | |
1593 | return 0; | |
1594 | ||
1595 | reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off)); | |
1596 | if (!tnum_is_aligned(reg_off, size)) { | |
1597 | char tn_buf[48]; | |
1598 | ||
1599 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
61bd5218 | 1600 | verbose(env, "misaligned %saccess off %s+%d+%d size %d\n", |
f1174f77 | 1601 | pointer_desc, tn_buf, reg->off, off, size); |
79adffcd DB |
1602 | return -EACCES; |
1603 | } | |
1604 | ||
969bf05e AS |
1605 | return 0; |
1606 | } | |
1607 | ||
e07b98d9 | 1608 | static int check_ptr_alignment(struct bpf_verifier_env *env, |
ca369602 DB |
1609 | const struct bpf_reg_state *reg, int off, |
1610 | int size, bool strict_alignment_once) | |
79adffcd | 1611 | { |
ca369602 | 1612 | bool strict = env->strict_alignment || strict_alignment_once; |
f1174f77 | 1613 | const char *pointer_desc = ""; |
d1174416 | 1614 | |
79adffcd DB |
1615 | switch (reg->type) { |
1616 | case PTR_TO_PACKET: | |
de8f3a83 DB |
1617 | case PTR_TO_PACKET_META: |
1618 | /* Special case, because of NET_IP_ALIGN. Given metadata sits | |
1619 | * right in front, treat it the very same way. | |
1620 | */ | |
61bd5218 | 1621 | return check_pkt_ptr_alignment(env, reg, off, size, strict); |
d58e468b PP |
1622 | case PTR_TO_FLOW_KEYS: |
1623 | pointer_desc = "flow keys "; | |
1624 | break; | |
f1174f77 EC |
1625 | case PTR_TO_MAP_VALUE: |
1626 | pointer_desc = "value "; | |
1627 | break; | |
1628 | case PTR_TO_CTX: | |
1629 | pointer_desc = "context "; | |
1630 | break; | |
1631 | case PTR_TO_STACK: | |
1632 | pointer_desc = "stack "; | |
a5ec6ae1 JH |
1633 | /* The stack spill tracking logic in check_stack_write() |
1634 | * and check_stack_read() relies on stack accesses being | |
1635 | * aligned. | |
1636 | */ | |
1637 | strict = true; | |
f1174f77 | 1638 | break; |
c64b7983 JS |
1639 | case PTR_TO_SOCKET: |
1640 | pointer_desc = "sock "; | |
1641 | break; | |
79adffcd | 1642 | default: |
f1174f77 | 1643 | break; |
79adffcd | 1644 | } |
61bd5218 JK |
1645 | return check_generic_ptr_alignment(env, reg, pointer_desc, off, size, |
1646 | strict); | |
79adffcd DB |
1647 | } |
1648 | ||
f4d7e40a AS |
1649 | static int update_stack_depth(struct bpf_verifier_env *env, |
1650 | const struct bpf_func_state *func, | |
1651 | int off) | |
1652 | { | |
9c8105bd | 1653 | u16 stack = env->subprog_info[func->subprogno].stack_depth; |
f4d7e40a AS |
1654 | |
1655 | if (stack >= -off) | |
1656 | return 0; | |
1657 | ||
1658 | /* update known max for given subprogram */ | |
9c8105bd | 1659 | env->subprog_info[func->subprogno].stack_depth = -off; |
70a87ffe AS |
1660 | return 0; |
1661 | } | |
f4d7e40a | 1662 | |
70a87ffe AS |
1663 | /* starting from main bpf function walk all instructions of the function |
1664 | * and recursively walk all callees that given function can call. | |
1665 | * Ignore jump and exit insns. | |
1666 | * Since recursion is prevented by check_cfg() this algorithm | |
1667 | * only needs a local stack of MAX_CALL_FRAMES to remember callsites | |
1668 | */ | |
1669 | static int check_max_stack_depth(struct bpf_verifier_env *env) | |
1670 | { | |
9c8105bd JW |
1671 | int depth = 0, frame = 0, idx = 0, i = 0, subprog_end; |
1672 | struct bpf_subprog_info *subprog = env->subprog_info; | |
70a87ffe | 1673 | struct bpf_insn *insn = env->prog->insnsi; |
70a87ffe AS |
1674 | int ret_insn[MAX_CALL_FRAMES]; |
1675 | int ret_prog[MAX_CALL_FRAMES]; | |
f4d7e40a | 1676 | |
70a87ffe AS |
1677 | process_func: |
1678 | /* round up to 32-bytes, since this is granularity | |
1679 | * of interpreter stack size | |
1680 | */ | |
9c8105bd | 1681 | depth += round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
70a87ffe | 1682 | if (depth > MAX_BPF_STACK) { |
f4d7e40a | 1683 | verbose(env, "combined stack size of %d calls is %d. Too large\n", |
70a87ffe | 1684 | frame + 1, depth); |
f4d7e40a AS |
1685 | return -EACCES; |
1686 | } | |
70a87ffe | 1687 | continue_func: |
4cb3d99c | 1688 | subprog_end = subprog[idx + 1].start; |
70a87ffe AS |
1689 | for (; i < subprog_end; i++) { |
1690 | if (insn[i].code != (BPF_JMP | BPF_CALL)) | |
1691 | continue; | |
1692 | if (insn[i].src_reg != BPF_PSEUDO_CALL) | |
1693 | continue; | |
1694 | /* remember insn and function to return to */ | |
1695 | ret_insn[frame] = i + 1; | |
9c8105bd | 1696 | ret_prog[frame] = idx; |
70a87ffe AS |
1697 | |
1698 | /* find the callee */ | |
1699 | i = i + insn[i].imm + 1; | |
9c8105bd JW |
1700 | idx = find_subprog(env, i); |
1701 | if (idx < 0) { | |
70a87ffe AS |
1702 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
1703 | i); | |
1704 | return -EFAULT; | |
1705 | } | |
70a87ffe AS |
1706 | frame++; |
1707 | if (frame >= MAX_CALL_FRAMES) { | |
1708 | WARN_ONCE(1, "verifier bug. Call stack is too deep\n"); | |
1709 | return -EFAULT; | |
1710 | } | |
1711 | goto process_func; | |
1712 | } | |
1713 | /* end of for() loop means the last insn of the 'subprog' | |
1714 | * was reached. Doesn't matter whether it was JA or EXIT | |
1715 | */ | |
1716 | if (frame == 0) | |
1717 | return 0; | |
9c8105bd | 1718 | depth -= round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
70a87ffe AS |
1719 | frame--; |
1720 | i = ret_insn[frame]; | |
9c8105bd | 1721 | idx = ret_prog[frame]; |
70a87ffe | 1722 | goto continue_func; |
f4d7e40a AS |
1723 | } |
1724 | ||
19d28fbd | 1725 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
1ea47e01 AS |
1726 | static int get_callee_stack_depth(struct bpf_verifier_env *env, |
1727 | const struct bpf_insn *insn, int idx) | |
1728 | { | |
1729 | int start = idx + insn->imm + 1, subprog; | |
1730 | ||
1731 | subprog = find_subprog(env, start); | |
1732 | if (subprog < 0) { | |
1733 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", | |
1734 | start); | |
1735 | return -EFAULT; | |
1736 | } | |
9c8105bd | 1737 | return env->subprog_info[subprog].stack_depth; |
1ea47e01 | 1738 | } |
19d28fbd | 1739 | #endif |
1ea47e01 | 1740 | |
58990d1f DB |
1741 | static int check_ctx_reg(struct bpf_verifier_env *env, |
1742 | const struct bpf_reg_state *reg, int regno) | |
1743 | { | |
1744 | /* Access to ctx or passing it to a helper is only allowed in | |
1745 | * its original, unmodified form. | |
1746 | */ | |
1747 | ||
1748 | if (reg->off) { | |
1749 | verbose(env, "dereference of modified ctx ptr R%d off=%d disallowed\n", | |
1750 | regno, reg->off); | |
1751 | return -EACCES; | |
1752 | } | |
1753 | ||
1754 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
1755 | char tn_buf[48]; | |
1756 | ||
1757 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
1758 | verbose(env, "variable ctx access var_off=%s disallowed\n", tn_buf); | |
1759 | return -EACCES; | |
1760 | } | |
1761 | ||
1762 | return 0; | |
1763 | } | |
1764 | ||
0c17d1d2 JH |
1765 | /* truncate register to smaller size (in bytes) |
1766 | * must be called with size < BPF_REG_SIZE | |
1767 | */ | |
1768 | static void coerce_reg_to_size(struct bpf_reg_state *reg, int size) | |
1769 | { | |
1770 | u64 mask; | |
1771 | ||
1772 | /* clear high bits in bit representation */ | |
1773 | reg->var_off = tnum_cast(reg->var_off, size); | |
1774 | ||
1775 | /* fix arithmetic bounds */ | |
1776 | mask = ((u64)1 << (size * 8)) - 1; | |
1777 | if ((reg->umin_value & ~mask) == (reg->umax_value & ~mask)) { | |
1778 | reg->umin_value &= mask; | |
1779 | reg->umax_value &= mask; | |
1780 | } else { | |
1781 | reg->umin_value = 0; | |
1782 | reg->umax_value = mask; | |
1783 | } | |
1784 | reg->smin_value = reg->umin_value; | |
1785 | reg->smax_value = reg->umax_value; | |
1786 | } | |
1787 | ||
17a52670 AS |
1788 | /* check whether memory at (regno + off) is accessible for t = (read | write) |
1789 | * if t==write, value_regno is a register which value is stored into memory | |
1790 | * if t==read, value_regno is a register which will receive the value from memory | |
1791 | * if t==write && value_regno==-1, some unknown value is stored into memory | |
1792 | * if t==read && value_regno==-1, don't care what we read from memory | |
1793 | */ | |
ca369602 DB |
1794 | static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno, |
1795 | int off, int bpf_size, enum bpf_access_type t, | |
1796 | int value_regno, bool strict_alignment_once) | |
17a52670 | 1797 | { |
638f5b90 AS |
1798 | struct bpf_reg_state *regs = cur_regs(env); |
1799 | struct bpf_reg_state *reg = regs + regno; | |
f4d7e40a | 1800 | struct bpf_func_state *state; |
17a52670 AS |
1801 | int size, err = 0; |
1802 | ||
1803 | size = bpf_size_to_bytes(bpf_size); | |
1804 | if (size < 0) | |
1805 | return size; | |
1806 | ||
f1174f77 | 1807 | /* alignment checks will add in reg->off themselves */ |
ca369602 | 1808 | err = check_ptr_alignment(env, reg, off, size, strict_alignment_once); |
969bf05e AS |
1809 | if (err) |
1810 | return err; | |
17a52670 | 1811 | |
f1174f77 EC |
1812 | /* for access checks, reg->off is just part of off */ |
1813 | off += reg->off; | |
1814 | ||
1815 | if (reg->type == PTR_TO_MAP_VALUE) { | |
1be7f75d AS |
1816 | if (t == BPF_WRITE && value_regno >= 0 && |
1817 | is_pointer_value(env, value_regno)) { | |
61bd5218 | 1818 | verbose(env, "R%d leaks addr into map\n", value_regno); |
1be7f75d AS |
1819 | return -EACCES; |
1820 | } | |
48461135 | 1821 | |
9fd29c08 | 1822 | err = check_map_access(env, regno, off, size, false); |
17a52670 | 1823 | if (!err && t == BPF_READ && value_regno >= 0) |
638f5b90 | 1824 | mark_reg_unknown(env, regs, value_regno); |
17a52670 | 1825 | |
1a0dc1ac | 1826 | } else if (reg->type == PTR_TO_CTX) { |
f1174f77 | 1827 | enum bpf_reg_type reg_type = SCALAR_VALUE; |
19de99f7 | 1828 | |
1be7f75d AS |
1829 | if (t == BPF_WRITE && value_regno >= 0 && |
1830 | is_pointer_value(env, value_regno)) { | |
61bd5218 | 1831 | verbose(env, "R%d leaks addr into ctx\n", value_regno); |
1be7f75d AS |
1832 | return -EACCES; |
1833 | } | |
f1174f77 | 1834 | |
58990d1f DB |
1835 | err = check_ctx_reg(env, reg, regno); |
1836 | if (err < 0) | |
1837 | return err; | |
1838 | ||
31fd8581 | 1839 | err = check_ctx_access(env, insn_idx, off, size, t, ®_type); |
969bf05e | 1840 | if (!err && t == BPF_READ && value_regno >= 0) { |
f1174f77 | 1841 | /* ctx access returns either a scalar, or a |
de8f3a83 DB |
1842 | * PTR_TO_PACKET[_META,_END]. In the latter |
1843 | * case, we know the offset is zero. | |
f1174f77 EC |
1844 | */ |
1845 | if (reg_type == SCALAR_VALUE) | |
638f5b90 | 1846 | mark_reg_unknown(env, regs, value_regno); |
f1174f77 | 1847 | else |
638f5b90 | 1848 | mark_reg_known_zero(env, regs, |
61bd5218 | 1849 | value_regno); |
638f5b90 | 1850 | regs[value_regno].type = reg_type; |
969bf05e | 1851 | } |
17a52670 | 1852 | |
f1174f77 EC |
1853 | } else if (reg->type == PTR_TO_STACK) { |
1854 | /* stack accesses must be at a fixed offset, so that we can | |
1855 | * determine what type of data were returned. | |
1856 | * See check_stack_read(). | |
1857 | */ | |
1858 | if (!tnum_is_const(reg->var_off)) { | |
1859 | char tn_buf[48]; | |
1860 | ||
1861 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
61bd5218 | 1862 | verbose(env, "variable stack access var_off=%s off=%d size=%d", |
f1174f77 EC |
1863 | tn_buf, off, size); |
1864 | return -EACCES; | |
1865 | } | |
1866 | off += reg->var_off.value; | |
17a52670 | 1867 | if (off >= 0 || off < -MAX_BPF_STACK) { |
61bd5218 JK |
1868 | verbose(env, "invalid stack off=%d size=%d\n", off, |
1869 | size); | |
17a52670 AS |
1870 | return -EACCES; |
1871 | } | |
8726679a | 1872 | |
f4d7e40a AS |
1873 | state = func(env, reg); |
1874 | err = update_stack_depth(env, state, off); | |
1875 | if (err) | |
1876 | return err; | |
8726679a | 1877 | |
638f5b90 | 1878 | if (t == BPF_WRITE) |
61bd5218 | 1879 | err = check_stack_write(env, state, off, size, |
af86ca4e | 1880 | value_regno, insn_idx); |
638f5b90 | 1881 | else |
61bd5218 JK |
1882 | err = check_stack_read(env, state, off, size, |
1883 | value_regno); | |
de8f3a83 | 1884 | } else if (reg_is_pkt_pointer(reg)) { |
3a0af8fd | 1885 | if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) { |
61bd5218 | 1886 | verbose(env, "cannot write into packet\n"); |
969bf05e AS |
1887 | return -EACCES; |
1888 | } | |
4acf6c0b BB |
1889 | if (t == BPF_WRITE && value_regno >= 0 && |
1890 | is_pointer_value(env, value_regno)) { | |
61bd5218 JK |
1891 | verbose(env, "R%d leaks addr into packet\n", |
1892 | value_regno); | |
4acf6c0b BB |
1893 | return -EACCES; |
1894 | } | |
9fd29c08 | 1895 | err = check_packet_access(env, regno, off, size, false); |
969bf05e | 1896 | if (!err && t == BPF_READ && value_regno >= 0) |
638f5b90 | 1897 | mark_reg_unknown(env, regs, value_regno); |
d58e468b PP |
1898 | } else if (reg->type == PTR_TO_FLOW_KEYS) { |
1899 | if (t == BPF_WRITE && value_regno >= 0 && | |
1900 | is_pointer_value(env, value_regno)) { | |
1901 | verbose(env, "R%d leaks addr into flow keys\n", | |
1902 | value_regno); | |
1903 | return -EACCES; | |
1904 | } | |
1905 | ||
1906 | err = check_flow_keys_access(env, off, size); | |
1907 | if (!err && t == BPF_READ && value_regno >= 0) | |
1908 | mark_reg_unknown(env, regs, value_regno); | |
c64b7983 JS |
1909 | } else if (reg->type == PTR_TO_SOCKET) { |
1910 | if (t == BPF_WRITE) { | |
1911 | verbose(env, "cannot write into socket\n"); | |
1912 | return -EACCES; | |
1913 | } | |
1914 | err = check_sock_access(env, regno, off, size, t); | |
1915 | if (!err && value_regno >= 0) | |
1916 | mark_reg_unknown(env, regs, value_regno); | |
17a52670 | 1917 | } else { |
61bd5218 JK |
1918 | verbose(env, "R%d invalid mem access '%s'\n", regno, |
1919 | reg_type_str[reg->type]); | |
17a52670 AS |
1920 | return -EACCES; |
1921 | } | |
969bf05e | 1922 | |
f1174f77 | 1923 | if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ && |
638f5b90 | 1924 | regs[value_regno].type == SCALAR_VALUE) { |
f1174f77 | 1925 | /* b/h/w load zero-extends, mark upper bits as known 0 */ |
0c17d1d2 | 1926 | coerce_reg_to_size(®s[value_regno], size); |
969bf05e | 1927 | } |
17a52670 AS |
1928 | return err; |
1929 | } | |
1930 | ||
31fd8581 | 1931 | static int check_xadd(struct bpf_verifier_env *env, int insn_idx, struct bpf_insn *insn) |
17a52670 | 1932 | { |
17a52670 AS |
1933 | int err; |
1934 | ||
1935 | if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) || | |
1936 | insn->imm != 0) { | |
61bd5218 | 1937 | verbose(env, "BPF_XADD uses reserved fields\n"); |
17a52670 AS |
1938 | return -EINVAL; |
1939 | } | |
1940 | ||
1941 | /* check src1 operand */ | |
dc503a8a | 1942 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
1943 | if (err) |
1944 | return err; | |
1945 | ||
1946 | /* check src2 operand */ | |
dc503a8a | 1947 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
1948 | if (err) |
1949 | return err; | |
1950 | ||
6bdf6abc | 1951 | if (is_pointer_value(env, insn->src_reg)) { |
61bd5218 | 1952 | verbose(env, "R%d leaks addr into mem\n", insn->src_reg); |
6bdf6abc DB |
1953 | return -EACCES; |
1954 | } | |
1955 | ||
ca369602 DB |
1956 | if (is_ctx_reg(env, insn->dst_reg) || |
1957 | is_pkt_reg(env, insn->dst_reg)) { | |
1958 | verbose(env, "BPF_XADD stores into R%d %s is not allowed\n", | |
9d2be44a | 1959 | insn->dst_reg, reg_type_str[insn->dst_reg]); |
f37a8cb8 DB |
1960 | return -EACCES; |
1961 | } | |
1962 | ||
17a52670 | 1963 | /* check whether atomic_add can read the memory */ |
31fd8581 | 1964 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
ca369602 | 1965 | BPF_SIZE(insn->code), BPF_READ, -1, true); |
17a52670 AS |
1966 | if (err) |
1967 | return err; | |
1968 | ||
1969 | /* check whether atomic_add can write into the same memory */ | |
31fd8581 | 1970 | return check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
ca369602 | 1971 | BPF_SIZE(insn->code), BPF_WRITE, -1, true); |
17a52670 AS |
1972 | } |
1973 | ||
1974 | /* when register 'regno' is passed into function that will read 'access_size' | |
1975 | * bytes from that pointer, make sure that it's within stack boundary | |
f1174f77 EC |
1976 | * and all elements of stack are initialized. |
1977 | * Unlike most pointer bounds-checking functions, this one doesn't take an | |
1978 | * 'off' argument, so it has to add in reg->off itself. | |
17a52670 | 1979 | */ |
58e2af8b | 1980 | static int check_stack_boundary(struct bpf_verifier_env *env, int regno, |
435faee1 DB |
1981 | int access_size, bool zero_size_allowed, |
1982 | struct bpf_call_arg_meta *meta) | |
17a52670 | 1983 | { |
914cb781 | 1984 | struct bpf_reg_state *reg = cur_regs(env) + regno; |
f4d7e40a | 1985 | struct bpf_func_state *state = func(env, reg); |
638f5b90 | 1986 | int off, i, slot, spi; |
17a52670 | 1987 | |
914cb781 | 1988 | if (reg->type != PTR_TO_STACK) { |
f1174f77 | 1989 | /* Allow zero-byte read from NULL, regardless of pointer type */ |
8e2fe1d9 | 1990 | if (zero_size_allowed && access_size == 0 && |
914cb781 | 1991 | register_is_null(reg)) |
8e2fe1d9 DB |
1992 | return 0; |
1993 | ||
61bd5218 | 1994 | verbose(env, "R%d type=%s expected=%s\n", regno, |
914cb781 | 1995 | reg_type_str[reg->type], |
8e2fe1d9 | 1996 | reg_type_str[PTR_TO_STACK]); |
17a52670 | 1997 | return -EACCES; |
8e2fe1d9 | 1998 | } |
17a52670 | 1999 | |
f1174f77 | 2000 | /* Only allow fixed-offset stack reads */ |
914cb781 | 2001 | if (!tnum_is_const(reg->var_off)) { |
f1174f77 EC |
2002 | char tn_buf[48]; |
2003 | ||
914cb781 | 2004 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
61bd5218 | 2005 | verbose(env, "invalid variable stack read R%d var_off=%s\n", |
f1174f77 | 2006 | regno, tn_buf); |
ea25f914 | 2007 | return -EACCES; |
f1174f77 | 2008 | } |
914cb781 | 2009 | off = reg->off + reg->var_off.value; |
17a52670 | 2010 | if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 || |
9fd29c08 | 2011 | access_size < 0 || (access_size == 0 && !zero_size_allowed)) { |
61bd5218 | 2012 | verbose(env, "invalid stack type R%d off=%d access_size=%d\n", |
17a52670 AS |
2013 | regno, off, access_size); |
2014 | return -EACCES; | |
2015 | } | |
2016 | ||
435faee1 DB |
2017 | if (meta && meta->raw_mode) { |
2018 | meta->access_size = access_size; | |
2019 | meta->regno = regno; | |
2020 | return 0; | |
2021 | } | |
2022 | ||
17a52670 | 2023 | for (i = 0; i < access_size; i++) { |
cc2b14d5 AS |
2024 | u8 *stype; |
2025 | ||
638f5b90 AS |
2026 | slot = -(off + i) - 1; |
2027 | spi = slot / BPF_REG_SIZE; | |
cc2b14d5 AS |
2028 | if (state->allocated_stack <= slot) |
2029 | goto err; | |
2030 | stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE]; | |
2031 | if (*stype == STACK_MISC) | |
2032 | goto mark; | |
2033 | if (*stype == STACK_ZERO) { | |
2034 | /* helper can write anything into the stack */ | |
2035 | *stype = STACK_MISC; | |
2036 | goto mark; | |
17a52670 | 2037 | } |
cc2b14d5 AS |
2038 | err: |
2039 | verbose(env, "invalid indirect read from stack off %d+%d size %d\n", | |
2040 | off, i, access_size); | |
2041 | return -EACCES; | |
2042 | mark: | |
2043 | /* reading any byte out of 8-byte 'spill_slot' will cause | |
2044 | * the whole slot to be marked as 'read' | |
2045 | */ | |
679c782d EC |
2046 | mark_reg_read(env, &state->stack[spi].spilled_ptr, |
2047 | state->stack[spi].spilled_ptr.parent); | |
17a52670 | 2048 | } |
f4d7e40a | 2049 | return update_stack_depth(env, state, off); |
17a52670 AS |
2050 | } |
2051 | ||
06c1c049 GB |
2052 | static int check_helper_mem_access(struct bpf_verifier_env *env, int regno, |
2053 | int access_size, bool zero_size_allowed, | |
2054 | struct bpf_call_arg_meta *meta) | |
2055 | { | |
638f5b90 | 2056 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
06c1c049 | 2057 | |
f1174f77 | 2058 | switch (reg->type) { |
06c1c049 | 2059 | case PTR_TO_PACKET: |
de8f3a83 | 2060 | case PTR_TO_PACKET_META: |
9fd29c08 YS |
2061 | return check_packet_access(env, regno, reg->off, access_size, |
2062 | zero_size_allowed); | |
d58e468b PP |
2063 | case PTR_TO_FLOW_KEYS: |
2064 | return check_flow_keys_access(env, reg->off, access_size); | |
06c1c049 | 2065 | case PTR_TO_MAP_VALUE: |
9fd29c08 YS |
2066 | return check_map_access(env, regno, reg->off, access_size, |
2067 | zero_size_allowed); | |
f1174f77 | 2068 | default: /* scalar_value|ptr_to_stack or invalid ptr */ |
06c1c049 GB |
2069 | return check_stack_boundary(env, regno, access_size, |
2070 | zero_size_allowed, meta); | |
2071 | } | |
2072 | } | |
2073 | ||
90133415 DB |
2074 | static bool arg_type_is_mem_ptr(enum bpf_arg_type type) |
2075 | { | |
2076 | return type == ARG_PTR_TO_MEM || | |
2077 | type == ARG_PTR_TO_MEM_OR_NULL || | |
2078 | type == ARG_PTR_TO_UNINIT_MEM; | |
2079 | } | |
2080 | ||
2081 | static bool arg_type_is_mem_size(enum bpf_arg_type type) | |
2082 | { | |
2083 | return type == ARG_CONST_SIZE || | |
2084 | type == ARG_CONST_SIZE_OR_ZERO; | |
2085 | } | |
2086 | ||
58e2af8b | 2087 | static int check_func_arg(struct bpf_verifier_env *env, u32 regno, |
33ff9823 DB |
2088 | enum bpf_arg_type arg_type, |
2089 | struct bpf_call_arg_meta *meta) | |
17a52670 | 2090 | { |
638f5b90 | 2091 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
6841de8b | 2092 | enum bpf_reg_type expected_type, type = reg->type; |
17a52670 AS |
2093 | int err = 0; |
2094 | ||
80f1d68c | 2095 | if (arg_type == ARG_DONTCARE) |
17a52670 AS |
2096 | return 0; |
2097 | ||
dc503a8a EC |
2098 | err = check_reg_arg(env, regno, SRC_OP); |
2099 | if (err) | |
2100 | return err; | |
17a52670 | 2101 | |
1be7f75d AS |
2102 | if (arg_type == ARG_ANYTHING) { |
2103 | if (is_pointer_value(env, regno)) { | |
61bd5218 JK |
2104 | verbose(env, "R%d leaks addr into helper function\n", |
2105 | regno); | |
1be7f75d AS |
2106 | return -EACCES; |
2107 | } | |
80f1d68c | 2108 | return 0; |
1be7f75d | 2109 | } |
80f1d68c | 2110 | |
de8f3a83 | 2111 | if (type_is_pkt_pointer(type) && |
3a0af8fd | 2112 | !may_access_direct_pkt_data(env, meta, BPF_READ)) { |
61bd5218 | 2113 | verbose(env, "helper access to the packet is not allowed\n"); |
6841de8b AS |
2114 | return -EACCES; |
2115 | } | |
2116 | ||
8e2fe1d9 | 2117 | if (arg_type == ARG_PTR_TO_MAP_KEY || |
17a52670 AS |
2118 | arg_type == ARG_PTR_TO_MAP_VALUE) { |
2119 | expected_type = PTR_TO_STACK; | |
d71962f3 | 2120 | if (!type_is_pkt_pointer(type) && type != PTR_TO_MAP_VALUE && |
de8f3a83 | 2121 | type != expected_type) |
6841de8b | 2122 | goto err_type; |
39f19ebb AS |
2123 | } else if (arg_type == ARG_CONST_SIZE || |
2124 | arg_type == ARG_CONST_SIZE_OR_ZERO) { | |
f1174f77 EC |
2125 | expected_type = SCALAR_VALUE; |
2126 | if (type != expected_type) | |
6841de8b | 2127 | goto err_type; |
17a52670 AS |
2128 | } else if (arg_type == ARG_CONST_MAP_PTR) { |
2129 | expected_type = CONST_PTR_TO_MAP; | |
6841de8b AS |
2130 | if (type != expected_type) |
2131 | goto err_type; | |
608cd71a AS |
2132 | } else if (arg_type == ARG_PTR_TO_CTX) { |
2133 | expected_type = PTR_TO_CTX; | |
6841de8b AS |
2134 | if (type != expected_type) |
2135 | goto err_type; | |
58990d1f DB |
2136 | err = check_ctx_reg(env, reg, regno); |
2137 | if (err < 0) | |
2138 | return err; | |
c64b7983 JS |
2139 | } else if (arg_type == ARG_PTR_TO_SOCKET) { |
2140 | expected_type = PTR_TO_SOCKET; | |
2141 | if (type != expected_type) | |
2142 | goto err_type; | |
fd978bf7 JS |
2143 | if (meta->ptr_id || !reg->id) { |
2144 | verbose(env, "verifier internal error: mismatched references meta=%d, reg=%d\n", | |
2145 | meta->ptr_id, reg->id); | |
2146 | return -EFAULT; | |
2147 | } | |
2148 | meta->ptr_id = reg->id; | |
90133415 | 2149 | } else if (arg_type_is_mem_ptr(arg_type)) { |
8e2fe1d9 DB |
2150 | expected_type = PTR_TO_STACK; |
2151 | /* One exception here. In case function allows for NULL to be | |
f1174f77 | 2152 | * passed in as argument, it's a SCALAR_VALUE type. Final test |
8e2fe1d9 DB |
2153 | * happens during stack boundary checking. |
2154 | */ | |
914cb781 | 2155 | if (register_is_null(reg) && |
db1ac496 | 2156 | arg_type == ARG_PTR_TO_MEM_OR_NULL) |
6841de8b | 2157 | /* final test in check_stack_boundary() */; |
de8f3a83 DB |
2158 | else if (!type_is_pkt_pointer(type) && |
2159 | type != PTR_TO_MAP_VALUE && | |
f1174f77 | 2160 | type != expected_type) |
6841de8b | 2161 | goto err_type; |
39f19ebb | 2162 | meta->raw_mode = arg_type == ARG_PTR_TO_UNINIT_MEM; |
17a52670 | 2163 | } else { |
61bd5218 | 2164 | verbose(env, "unsupported arg_type %d\n", arg_type); |
17a52670 AS |
2165 | return -EFAULT; |
2166 | } | |
2167 | ||
17a52670 AS |
2168 | if (arg_type == ARG_CONST_MAP_PTR) { |
2169 | /* bpf_map_xxx(map_ptr) call: remember that map_ptr */ | |
33ff9823 | 2170 | meta->map_ptr = reg->map_ptr; |
17a52670 AS |
2171 | } else if (arg_type == ARG_PTR_TO_MAP_KEY) { |
2172 | /* bpf_map_xxx(..., map_ptr, ..., key) call: | |
2173 | * check that [key, key + map->key_size) are within | |
2174 | * stack limits and initialized | |
2175 | */ | |
33ff9823 | 2176 | if (!meta->map_ptr) { |
17a52670 AS |
2177 | /* in function declaration map_ptr must come before |
2178 | * map_key, so that it's verified and known before | |
2179 | * we have to check map_key here. Otherwise it means | |
2180 | * that kernel subsystem misconfigured verifier | |
2181 | */ | |
61bd5218 | 2182 | verbose(env, "invalid map_ptr to access map->key\n"); |
17a52670 AS |
2183 | return -EACCES; |
2184 | } | |
d71962f3 PC |
2185 | err = check_helper_mem_access(env, regno, |
2186 | meta->map_ptr->key_size, false, | |
2187 | NULL); | |
17a52670 AS |
2188 | } else if (arg_type == ARG_PTR_TO_MAP_VALUE) { |
2189 | /* bpf_map_xxx(..., map_ptr, ..., value) call: | |
2190 | * check [value, value + map->value_size) validity | |
2191 | */ | |
33ff9823 | 2192 | if (!meta->map_ptr) { |
17a52670 | 2193 | /* kernel subsystem misconfigured verifier */ |
61bd5218 | 2194 | verbose(env, "invalid map_ptr to access map->value\n"); |
17a52670 AS |
2195 | return -EACCES; |
2196 | } | |
d71962f3 PC |
2197 | err = check_helper_mem_access(env, regno, |
2198 | meta->map_ptr->value_size, false, | |
2199 | NULL); | |
90133415 | 2200 | } else if (arg_type_is_mem_size(arg_type)) { |
39f19ebb | 2201 | bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO); |
17a52670 | 2202 | |
849fa506 YS |
2203 | /* remember the mem_size which may be used later |
2204 | * to refine return values. | |
2205 | */ | |
2206 | meta->msize_smax_value = reg->smax_value; | |
2207 | meta->msize_umax_value = reg->umax_value; | |
2208 | ||
f1174f77 EC |
2209 | /* The register is SCALAR_VALUE; the access check |
2210 | * happens using its boundaries. | |
06c1c049 | 2211 | */ |
f1174f77 | 2212 | if (!tnum_is_const(reg->var_off)) |
06c1c049 GB |
2213 | /* For unprivileged variable accesses, disable raw |
2214 | * mode so that the program is required to | |
2215 | * initialize all the memory that the helper could | |
2216 | * just partially fill up. | |
2217 | */ | |
2218 | meta = NULL; | |
2219 | ||
b03c9f9f | 2220 | if (reg->smin_value < 0) { |
61bd5218 | 2221 | verbose(env, "R%d min value is negative, either use unsigned or 'var &= const'\n", |
f1174f77 EC |
2222 | regno); |
2223 | return -EACCES; | |
2224 | } | |
06c1c049 | 2225 | |
b03c9f9f | 2226 | if (reg->umin_value == 0) { |
f1174f77 EC |
2227 | err = check_helper_mem_access(env, regno - 1, 0, |
2228 | zero_size_allowed, | |
2229 | meta); | |
06c1c049 GB |
2230 | if (err) |
2231 | return err; | |
06c1c049 | 2232 | } |
f1174f77 | 2233 | |
b03c9f9f | 2234 | if (reg->umax_value >= BPF_MAX_VAR_SIZ) { |
61bd5218 | 2235 | verbose(env, "R%d unbounded memory access, use 'var &= const' or 'if (var < const)'\n", |
f1174f77 EC |
2236 | regno); |
2237 | return -EACCES; | |
2238 | } | |
2239 | err = check_helper_mem_access(env, regno - 1, | |
b03c9f9f | 2240 | reg->umax_value, |
f1174f77 | 2241 | zero_size_allowed, meta); |
17a52670 AS |
2242 | } |
2243 | ||
2244 | return err; | |
6841de8b | 2245 | err_type: |
61bd5218 | 2246 | verbose(env, "R%d type=%s expected=%s\n", regno, |
6841de8b AS |
2247 | reg_type_str[type], reg_type_str[expected_type]); |
2248 | return -EACCES; | |
17a52670 AS |
2249 | } |
2250 | ||
61bd5218 JK |
2251 | static int check_map_func_compatibility(struct bpf_verifier_env *env, |
2252 | struct bpf_map *map, int func_id) | |
35578d79 | 2253 | { |
35578d79 KX |
2254 | if (!map) |
2255 | return 0; | |
2256 | ||
6aff67c8 AS |
2257 | /* We need a two way check, first is from map perspective ... */ |
2258 | switch (map->map_type) { | |
2259 | case BPF_MAP_TYPE_PROG_ARRAY: | |
2260 | if (func_id != BPF_FUNC_tail_call) | |
2261 | goto error; | |
2262 | break; | |
2263 | case BPF_MAP_TYPE_PERF_EVENT_ARRAY: | |
2264 | if (func_id != BPF_FUNC_perf_event_read && | |
908432ca YS |
2265 | func_id != BPF_FUNC_perf_event_output && |
2266 | func_id != BPF_FUNC_perf_event_read_value) | |
6aff67c8 AS |
2267 | goto error; |
2268 | break; | |
2269 | case BPF_MAP_TYPE_STACK_TRACE: | |
2270 | if (func_id != BPF_FUNC_get_stackid) | |
2271 | goto error; | |
2272 | break; | |
4ed8ec52 | 2273 | case BPF_MAP_TYPE_CGROUP_ARRAY: |
60747ef4 | 2274 | if (func_id != BPF_FUNC_skb_under_cgroup && |
60d20f91 | 2275 | func_id != BPF_FUNC_current_task_under_cgroup) |
4a482f34 MKL |
2276 | goto error; |
2277 | break; | |
cd339431 | 2278 | case BPF_MAP_TYPE_CGROUP_STORAGE: |
b741f163 | 2279 | case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE: |
cd339431 RG |
2280 | if (func_id != BPF_FUNC_get_local_storage) |
2281 | goto error; | |
2282 | break; | |
546ac1ff JF |
2283 | /* devmap returns a pointer to a live net_device ifindex that we cannot |
2284 | * allow to be modified from bpf side. So do not allow lookup elements | |
2285 | * for now. | |
2286 | */ | |
2287 | case BPF_MAP_TYPE_DEVMAP: | |
2ddf71e2 | 2288 | if (func_id != BPF_FUNC_redirect_map) |
546ac1ff JF |
2289 | goto error; |
2290 | break; | |
fbfc504a BT |
2291 | /* Restrict bpf side of cpumap and xskmap, open when use-cases |
2292 | * appear. | |
2293 | */ | |
6710e112 | 2294 | case BPF_MAP_TYPE_CPUMAP: |
fbfc504a | 2295 | case BPF_MAP_TYPE_XSKMAP: |
6710e112 JDB |
2296 | if (func_id != BPF_FUNC_redirect_map) |
2297 | goto error; | |
2298 | break; | |
56f668df | 2299 | case BPF_MAP_TYPE_ARRAY_OF_MAPS: |
bcc6b1b7 | 2300 | case BPF_MAP_TYPE_HASH_OF_MAPS: |
56f668df MKL |
2301 | if (func_id != BPF_FUNC_map_lookup_elem) |
2302 | goto error; | |
16a43625 | 2303 | break; |
174a79ff JF |
2304 | case BPF_MAP_TYPE_SOCKMAP: |
2305 | if (func_id != BPF_FUNC_sk_redirect_map && | |
2306 | func_id != BPF_FUNC_sock_map_update && | |
4f738adb JF |
2307 | func_id != BPF_FUNC_map_delete_elem && |
2308 | func_id != BPF_FUNC_msg_redirect_map) | |
174a79ff JF |
2309 | goto error; |
2310 | break; | |
81110384 JF |
2311 | case BPF_MAP_TYPE_SOCKHASH: |
2312 | if (func_id != BPF_FUNC_sk_redirect_hash && | |
2313 | func_id != BPF_FUNC_sock_hash_update && | |
2314 | func_id != BPF_FUNC_map_delete_elem && | |
2315 | func_id != BPF_FUNC_msg_redirect_hash) | |
2316 | goto error; | |
2317 | break; | |
2dbb9b9e MKL |
2318 | case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY: |
2319 | if (func_id != BPF_FUNC_sk_select_reuseport) | |
2320 | goto error; | |
2321 | break; | |
6aff67c8 AS |
2322 | default: |
2323 | break; | |
2324 | } | |
2325 | ||
2326 | /* ... and second from the function itself. */ | |
2327 | switch (func_id) { | |
2328 | case BPF_FUNC_tail_call: | |
2329 | if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY) | |
2330 | goto error; | |
f910cefa | 2331 | if (env->subprog_cnt > 1) { |
f4d7e40a AS |
2332 | verbose(env, "tail_calls are not allowed in programs with bpf-to-bpf calls\n"); |
2333 | return -EINVAL; | |
2334 | } | |
6aff67c8 AS |
2335 | break; |
2336 | case BPF_FUNC_perf_event_read: | |
2337 | case BPF_FUNC_perf_event_output: | |
908432ca | 2338 | case BPF_FUNC_perf_event_read_value: |
6aff67c8 AS |
2339 | if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) |
2340 | goto error; | |
2341 | break; | |
2342 | case BPF_FUNC_get_stackid: | |
2343 | if (map->map_type != BPF_MAP_TYPE_STACK_TRACE) | |
2344 | goto error; | |
2345 | break; | |
60d20f91 | 2346 | case BPF_FUNC_current_task_under_cgroup: |
747ea55e | 2347 | case BPF_FUNC_skb_under_cgroup: |
4a482f34 MKL |
2348 | if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY) |
2349 | goto error; | |
2350 | break; | |
97f91a7c | 2351 | case BPF_FUNC_redirect_map: |
9c270af3 | 2352 | if (map->map_type != BPF_MAP_TYPE_DEVMAP && |
fbfc504a BT |
2353 | map->map_type != BPF_MAP_TYPE_CPUMAP && |
2354 | map->map_type != BPF_MAP_TYPE_XSKMAP) | |
97f91a7c JF |
2355 | goto error; |
2356 | break; | |
174a79ff | 2357 | case BPF_FUNC_sk_redirect_map: |
4f738adb | 2358 | case BPF_FUNC_msg_redirect_map: |
81110384 | 2359 | case BPF_FUNC_sock_map_update: |
174a79ff JF |
2360 | if (map->map_type != BPF_MAP_TYPE_SOCKMAP) |
2361 | goto error; | |
2362 | break; | |
81110384 JF |
2363 | case BPF_FUNC_sk_redirect_hash: |
2364 | case BPF_FUNC_msg_redirect_hash: | |
2365 | case BPF_FUNC_sock_hash_update: | |
2366 | if (map->map_type != BPF_MAP_TYPE_SOCKHASH) | |
174a79ff JF |
2367 | goto error; |
2368 | break; | |
cd339431 | 2369 | case BPF_FUNC_get_local_storage: |
b741f163 RG |
2370 | if (map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE && |
2371 | map->map_type != BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) | |
cd339431 RG |
2372 | goto error; |
2373 | break; | |
2dbb9b9e MKL |
2374 | case BPF_FUNC_sk_select_reuseport: |
2375 | if (map->map_type != BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) | |
2376 | goto error; | |
2377 | break; | |
6aff67c8 AS |
2378 | default: |
2379 | break; | |
35578d79 KX |
2380 | } |
2381 | ||
2382 | return 0; | |
6aff67c8 | 2383 | error: |
61bd5218 | 2384 | verbose(env, "cannot pass map_type %d into func %s#%d\n", |
ebb676da | 2385 | map->map_type, func_id_name(func_id), func_id); |
6aff67c8 | 2386 | return -EINVAL; |
35578d79 KX |
2387 | } |
2388 | ||
90133415 | 2389 | static bool check_raw_mode_ok(const struct bpf_func_proto *fn) |
435faee1 DB |
2390 | { |
2391 | int count = 0; | |
2392 | ||
39f19ebb | 2393 | if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 2394 | count++; |
39f19ebb | 2395 | if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 2396 | count++; |
39f19ebb | 2397 | if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 2398 | count++; |
39f19ebb | 2399 | if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 2400 | count++; |
39f19ebb | 2401 | if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 DB |
2402 | count++; |
2403 | ||
90133415 DB |
2404 | /* We only support one arg being in raw mode at the moment, |
2405 | * which is sufficient for the helper functions we have | |
2406 | * right now. | |
2407 | */ | |
2408 | return count <= 1; | |
2409 | } | |
2410 | ||
2411 | static bool check_args_pair_invalid(enum bpf_arg_type arg_curr, | |
2412 | enum bpf_arg_type arg_next) | |
2413 | { | |
2414 | return (arg_type_is_mem_ptr(arg_curr) && | |
2415 | !arg_type_is_mem_size(arg_next)) || | |
2416 | (!arg_type_is_mem_ptr(arg_curr) && | |
2417 | arg_type_is_mem_size(arg_next)); | |
2418 | } | |
2419 | ||
2420 | static bool check_arg_pair_ok(const struct bpf_func_proto *fn) | |
2421 | { | |
2422 | /* bpf_xxx(..., buf, len) call will access 'len' | |
2423 | * bytes from memory 'buf'. Both arg types need | |
2424 | * to be paired, so make sure there's no buggy | |
2425 | * helper function specification. | |
2426 | */ | |
2427 | if (arg_type_is_mem_size(fn->arg1_type) || | |
2428 | arg_type_is_mem_ptr(fn->arg5_type) || | |
2429 | check_args_pair_invalid(fn->arg1_type, fn->arg2_type) || | |
2430 | check_args_pair_invalid(fn->arg2_type, fn->arg3_type) || | |
2431 | check_args_pair_invalid(fn->arg3_type, fn->arg4_type) || | |
2432 | check_args_pair_invalid(fn->arg4_type, fn->arg5_type)) | |
2433 | return false; | |
2434 | ||
2435 | return true; | |
2436 | } | |
2437 | ||
fd978bf7 JS |
2438 | static bool check_refcount_ok(const struct bpf_func_proto *fn) |
2439 | { | |
2440 | int count = 0; | |
2441 | ||
2442 | if (arg_type_is_refcounted(fn->arg1_type)) | |
2443 | count++; | |
2444 | if (arg_type_is_refcounted(fn->arg2_type)) | |
2445 | count++; | |
2446 | if (arg_type_is_refcounted(fn->arg3_type)) | |
2447 | count++; | |
2448 | if (arg_type_is_refcounted(fn->arg4_type)) | |
2449 | count++; | |
2450 | if (arg_type_is_refcounted(fn->arg5_type)) | |
2451 | count++; | |
2452 | ||
2453 | /* We only support one arg being unreferenced at the moment, | |
2454 | * which is sufficient for the helper functions we have right now. | |
2455 | */ | |
2456 | return count <= 1; | |
2457 | } | |
2458 | ||
90133415 DB |
2459 | static int check_func_proto(const struct bpf_func_proto *fn) |
2460 | { | |
2461 | return check_raw_mode_ok(fn) && | |
fd978bf7 JS |
2462 | check_arg_pair_ok(fn) && |
2463 | check_refcount_ok(fn) ? 0 : -EINVAL; | |
435faee1 DB |
2464 | } |
2465 | ||
de8f3a83 DB |
2466 | /* Packet data might have moved, any old PTR_TO_PACKET[_META,_END] |
2467 | * are now invalid, so turn them into unknown SCALAR_VALUE. | |
f1174f77 | 2468 | */ |
f4d7e40a AS |
2469 | static void __clear_all_pkt_pointers(struct bpf_verifier_env *env, |
2470 | struct bpf_func_state *state) | |
969bf05e | 2471 | { |
58e2af8b | 2472 | struct bpf_reg_state *regs = state->regs, *reg; |
969bf05e AS |
2473 | int i; |
2474 | ||
2475 | for (i = 0; i < MAX_BPF_REG; i++) | |
de8f3a83 | 2476 | if (reg_is_pkt_pointer_any(®s[i])) |
61bd5218 | 2477 | mark_reg_unknown(env, regs, i); |
969bf05e | 2478 | |
f3709f69 JS |
2479 | bpf_for_each_spilled_reg(i, state, reg) { |
2480 | if (!reg) | |
969bf05e | 2481 | continue; |
de8f3a83 DB |
2482 | if (reg_is_pkt_pointer_any(reg)) |
2483 | __mark_reg_unknown(reg); | |
969bf05e AS |
2484 | } |
2485 | } | |
2486 | ||
f4d7e40a AS |
2487 | static void clear_all_pkt_pointers(struct bpf_verifier_env *env) |
2488 | { | |
2489 | struct bpf_verifier_state *vstate = env->cur_state; | |
2490 | int i; | |
2491 | ||
2492 | for (i = 0; i <= vstate->curframe; i++) | |
2493 | __clear_all_pkt_pointers(env, vstate->frame[i]); | |
2494 | } | |
2495 | ||
fd978bf7 JS |
2496 | static void release_reg_references(struct bpf_verifier_env *env, |
2497 | struct bpf_func_state *state, int id) | |
2498 | { | |
2499 | struct bpf_reg_state *regs = state->regs, *reg; | |
2500 | int i; | |
2501 | ||
2502 | for (i = 0; i < MAX_BPF_REG; i++) | |
2503 | if (regs[i].id == id) | |
2504 | mark_reg_unknown(env, regs, i); | |
2505 | ||
2506 | bpf_for_each_spilled_reg(i, state, reg) { | |
2507 | if (!reg) | |
2508 | continue; | |
2509 | if (reg_is_refcounted(reg) && reg->id == id) | |
2510 | __mark_reg_unknown(reg); | |
2511 | } | |
2512 | } | |
2513 | ||
2514 | /* The pointer with the specified id has released its reference to kernel | |
2515 | * resources. Identify all copies of the same pointer and clear the reference. | |
2516 | */ | |
2517 | static int release_reference(struct bpf_verifier_env *env, | |
2518 | struct bpf_call_arg_meta *meta) | |
2519 | { | |
2520 | struct bpf_verifier_state *vstate = env->cur_state; | |
2521 | int i; | |
2522 | ||
2523 | for (i = 0; i <= vstate->curframe; i++) | |
2524 | release_reg_references(env, vstate->frame[i], meta->ptr_id); | |
2525 | ||
2526 | return release_reference_state(env, meta->ptr_id); | |
2527 | } | |
2528 | ||
f4d7e40a AS |
2529 | static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, |
2530 | int *insn_idx) | |
2531 | { | |
2532 | struct bpf_verifier_state *state = env->cur_state; | |
2533 | struct bpf_func_state *caller, *callee; | |
fd978bf7 | 2534 | int i, err, subprog, target_insn; |
f4d7e40a | 2535 | |
aada9ce6 | 2536 | if (state->curframe + 1 >= MAX_CALL_FRAMES) { |
f4d7e40a | 2537 | verbose(env, "the call stack of %d frames is too deep\n", |
aada9ce6 | 2538 | state->curframe + 2); |
f4d7e40a AS |
2539 | return -E2BIG; |
2540 | } | |
2541 | ||
2542 | target_insn = *insn_idx + insn->imm; | |
2543 | subprog = find_subprog(env, target_insn + 1); | |
2544 | if (subprog < 0) { | |
2545 | verbose(env, "verifier bug. No program starts at insn %d\n", | |
2546 | target_insn + 1); | |
2547 | return -EFAULT; | |
2548 | } | |
2549 | ||
2550 | caller = state->frame[state->curframe]; | |
2551 | if (state->frame[state->curframe + 1]) { | |
2552 | verbose(env, "verifier bug. Frame %d already allocated\n", | |
2553 | state->curframe + 1); | |
2554 | return -EFAULT; | |
2555 | } | |
2556 | ||
2557 | callee = kzalloc(sizeof(*callee), GFP_KERNEL); | |
2558 | if (!callee) | |
2559 | return -ENOMEM; | |
2560 | state->frame[state->curframe + 1] = callee; | |
2561 | ||
2562 | /* callee cannot access r0, r6 - r9 for reading and has to write | |
2563 | * into its own stack before reading from it. | |
2564 | * callee can read/write into caller's stack | |
2565 | */ | |
2566 | init_func_state(env, callee, | |
2567 | /* remember the callsite, it will be used by bpf_exit */ | |
2568 | *insn_idx /* callsite */, | |
2569 | state->curframe + 1 /* frameno within this callchain */, | |
f910cefa | 2570 | subprog /* subprog number within this prog */); |
f4d7e40a | 2571 | |
fd978bf7 JS |
2572 | /* Transfer references to the callee */ |
2573 | err = transfer_reference_state(callee, caller); | |
2574 | if (err) | |
2575 | return err; | |
2576 | ||
679c782d EC |
2577 | /* copy r1 - r5 args that callee can access. The copy includes parent |
2578 | * pointers, which connects us up to the liveness chain | |
2579 | */ | |
f4d7e40a AS |
2580 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) |
2581 | callee->regs[i] = caller->regs[i]; | |
2582 | ||
679c782d | 2583 | /* after the call registers r0 - r5 were scratched */ |
f4d7e40a AS |
2584 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
2585 | mark_reg_not_init(env, caller->regs, caller_saved[i]); | |
2586 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); | |
2587 | } | |
2588 | ||
2589 | /* only increment it after check_reg_arg() finished */ | |
2590 | state->curframe++; | |
2591 | ||
2592 | /* and go analyze first insn of the callee */ | |
2593 | *insn_idx = target_insn; | |
2594 | ||
2595 | if (env->log.level) { | |
2596 | verbose(env, "caller:\n"); | |
2597 | print_verifier_state(env, caller); | |
2598 | verbose(env, "callee:\n"); | |
2599 | print_verifier_state(env, callee); | |
2600 | } | |
2601 | return 0; | |
2602 | } | |
2603 | ||
2604 | static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx) | |
2605 | { | |
2606 | struct bpf_verifier_state *state = env->cur_state; | |
2607 | struct bpf_func_state *caller, *callee; | |
2608 | struct bpf_reg_state *r0; | |
fd978bf7 | 2609 | int err; |
f4d7e40a AS |
2610 | |
2611 | callee = state->frame[state->curframe]; | |
2612 | r0 = &callee->regs[BPF_REG_0]; | |
2613 | if (r0->type == PTR_TO_STACK) { | |
2614 | /* technically it's ok to return caller's stack pointer | |
2615 | * (or caller's caller's pointer) back to the caller, | |
2616 | * since these pointers are valid. Only current stack | |
2617 | * pointer will be invalid as soon as function exits, | |
2618 | * but let's be conservative | |
2619 | */ | |
2620 | verbose(env, "cannot return stack pointer to the caller\n"); | |
2621 | return -EINVAL; | |
2622 | } | |
2623 | ||
2624 | state->curframe--; | |
2625 | caller = state->frame[state->curframe]; | |
2626 | /* return to the caller whatever r0 had in the callee */ | |
2627 | caller->regs[BPF_REG_0] = *r0; | |
2628 | ||
fd978bf7 JS |
2629 | /* Transfer references to the caller */ |
2630 | err = transfer_reference_state(caller, callee); | |
2631 | if (err) | |
2632 | return err; | |
2633 | ||
f4d7e40a AS |
2634 | *insn_idx = callee->callsite + 1; |
2635 | if (env->log.level) { | |
2636 | verbose(env, "returning from callee:\n"); | |
2637 | print_verifier_state(env, callee); | |
2638 | verbose(env, "to caller at %d:\n", *insn_idx); | |
2639 | print_verifier_state(env, caller); | |
2640 | } | |
2641 | /* clear everything in the callee */ | |
2642 | free_func_state(callee); | |
2643 | state->frame[state->curframe + 1] = NULL; | |
2644 | return 0; | |
2645 | } | |
2646 | ||
849fa506 YS |
2647 | static void do_refine_retval_range(struct bpf_reg_state *regs, int ret_type, |
2648 | int func_id, | |
2649 | struct bpf_call_arg_meta *meta) | |
2650 | { | |
2651 | struct bpf_reg_state *ret_reg = ®s[BPF_REG_0]; | |
2652 | ||
2653 | if (ret_type != RET_INTEGER || | |
2654 | (func_id != BPF_FUNC_get_stack && | |
2655 | func_id != BPF_FUNC_probe_read_str)) | |
2656 | return; | |
2657 | ||
2658 | ret_reg->smax_value = meta->msize_smax_value; | |
2659 | ret_reg->umax_value = meta->msize_umax_value; | |
2660 | __reg_deduce_bounds(ret_reg); | |
2661 | __reg_bound_offset(ret_reg); | |
2662 | } | |
2663 | ||
c93552c4 DB |
2664 | static int |
2665 | record_func_map(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, | |
2666 | int func_id, int insn_idx) | |
2667 | { | |
2668 | struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; | |
2669 | ||
2670 | if (func_id != BPF_FUNC_tail_call && | |
09772d92 DB |
2671 | func_id != BPF_FUNC_map_lookup_elem && |
2672 | func_id != BPF_FUNC_map_update_elem && | |
2673 | func_id != BPF_FUNC_map_delete_elem) | |
c93552c4 | 2674 | return 0; |
09772d92 | 2675 | |
c93552c4 DB |
2676 | if (meta->map_ptr == NULL) { |
2677 | verbose(env, "kernel subsystem misconfigured verifier\n"); | |
2678 | return -EINVAL; | |
2679 | } | |
2680 | ||
2681 | if (!BPF_MAP_PTR(aux->map_state)) | |
2682 | bpf_map_ptr_store(aux, meta->map_ptr, | |
2683 | meta->map_ptr->unpriv_array); | |
2684 | else if (BPF_MAP_PTR(aux->map_state) != meta->map_ptr) | |
2685 | bpf_map_ptr_store(aux, BPF_MAP_PTR_POISON, | |
2686 | meta->map_ptr->unpriv_array); | |
2687 | return 0; | |
2688 | } | |
2689 | ||
fd978bf7 JS |
2690 | static int check_reference_leak(struct bpf_verifier_env *env) |
2691 | { | |
2692 | struct bpf_func_state *state = cur_func(env); | |
2693 | int i; | |
2694 | ||
2695 | for (i = 0; i < state->acquired_refs; i++) { | |
2696 | verbose(env, "Unreleased reference id=%d alloc_insn=%d\n", | |
2697 | state->refs[i].id, state->refs[i].insn_idx); | |
2698 | } | |
2699 | return state->acquired_refs ? -EINVAL : 0; | |
2700 | } | |
2701 | ||
f4d7e40a | 2702 | static int check_helper_call(struct bpf_verifier_env *env, int func_id, int insn_idx) |
17a52670 | 2703 | { |
17a52670 | 2704 | const struct bpf_func_proto *fn = NULL; |
638f5b90 | 2705 | struct bpf_reg_state *regs; |
33ff9823 | 2706 | struct bpf_call_arg_meta meta; |
969bf05e | 2707 | bool changes_data; |
17a52670 AS |
2708 | int i, err; |
2709 | ||
2710 | /* find function prototype */ | |
2711 | if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) { | |
61bd5218 JK |
2712 | verbose(env, "invalid func %s#%d\n", func_id_name(func_id), |
2713 | func_id); | |
17a52670 AS |
2714 | return -EINVAL; |
2715 | } | |
2716 | ||
00176a34 | 2717 | if (env->ops->get_func_proto) |
5e43f899 | 2718 | fn = env->ops->get_func_proto(func_id, env->prog); |
17a52670 | 2719 | if (!fn) { |
61bd5218 JK |
2720 | verbose(env, "unknown func %s#%d\n", func_id_name(func_id), |
2721 | func_id); | |
17a52670 AS |
2722 | return -EINVAL; |
2723 | } | |
2724 | ||
2725 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ | |
24701ece | 2726 | if (!env->prog->gpl_compatible && fn->gpl_only) { |
3fe2867c | 2727 | verbose(env, "cannot call GPL-restricted function from non-GPL compatible program\n"); |
17a52670 AS |
2728 | return -EINVAL; |
2729 | } | |
2730 | ||
04514d13 | 2731 | /* With LD_ABS/IND some JITs save/restore skb from r1. */ |
17bedab2 | 2732 | changes_data = bpf_helper_changes_pkt_data(fn->func); |
04514d13 DB |
2733 | if (changes_data && fn->arg1_type != ARG_PTR_TO_CTX) { |
2734 | verbose(env, "kernel subsystem misconfigured func %s#%d: r1 != ctx\n", | |
2735 | func_id_name(func_id), func_id); | |
2736 | return -EINVAL; | |
2737 | } | |
969bf05e | 2738 | |
33ff9823 | 2739 | memset(&meta, 0, sizeof(meta)); |
36bbef52 | 2740 | meta.pkt_access = fn->pkt_access; |
33ff9823 | 2741 | |
90133415 | 2742 | err = check_func_proto(fn); |
435faee1 | 2743 | if (err) { |
61bd5218 | 2744 | verbose(env, "kernel subsystem misconfigured func %s#%d\n", |
ebb676da | 2745 | func_id_name(func_id), func_id); |
435faee1 DB |
2746 | return err; |
2747 | } | |
2748 | ||
17a52670 | 2749 | /* check args */ |
33ff9823 | 2750 | err = check_func_arg(env, BPF_REG_1, fn->arg1_type, &meta); |
17a52670 AS |
2751 | if (err) |
2752 | return err; | |
33ff9823 | 2753 | err = check_func_arg(env, BPF_REG_2, fn->arg2_type, &meta); |
17a52670 AS |
2754 | if (err) |
2755 | return err; | |
33ff9823 | 2756 | err = check_func_arg(env, BPF_REG_3, fn->arg3_type, &meta); |
17a52670 AS |
2757 | if (err) |
2758 | return err; | |
33ff9823 | 2759 | err = check_func_arg(env, BPF_REG_4, fn->arg4_type, &meta); |
17a52670 AS |
2760 | if (err) |
2761 | return err; | |
33ff9823 | 2762 | err = check_func_arg(env, BPF_REG_5, fn->arg5_type, &meta); |
17a52670 AS |
2763 | if (err) |
2764 | return err; | |
2765 | ||
c93552c4 DB |
2766 | err = record_func_map(env, &meta, func_id, insn_idx); |
2767 | if (err) | |
2768 | return err; | |
2769 | ||
435faee1 DB |
2770 | /* Mark slots with STACK_MISC in case of raw mode, stack offset |
2771 | * is inferred from register state. | |
2772 | */ | |
2773 | for (i = 0; i < meta.access_size; i++) { | |
ca369602 DB |
2774 | err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B, |
2775 | BPF_WRITE, -1, false); | |
435faee1 DB |
2776 | if (err) |
2777 | return err; | |
2778 | } | |
2779 | ||
fd978bf7 JS |
2780 | if (func_id == BPF_FUNC_tail_call) { |
2781 | err = check_reference_leak(env); | |
2782 | if (err) { | |
2783 | verbose(env, "tail_call would lead to reference leak\n"); | |
2784 | return err; | |
2785 | } | |
2786 | } else if (is_release_function(func_id)) { | |
2787 | err = release_reference(env, &meta); | |
2788 | if (err) | |
2789 | return err; | |
2790 | } | |
2791 | ||
638f5b90 | 2792 | regs = cur_regs(env); |
cd339431 RG |
2793 | |
2794 | /* check that flags argument in get_local_storage(map, flags) is 0, | |
2795 | * this is required because get_local_storage() can't return an error. | |
2796 | */ | |
2797 | if (func_id == BPF_FUNC_get_local_storage && | |
2798 | !register_is_null(®s[BPF_REG_2])) { | |
2799 | verbose(env, "get_local_storage() doesn't support non-zero flags\n"); | |
2800 | return -EINVAL; | |
2801 | } | |
2802 | ||
17a52670 | 2803 | /* reset caller saved regs */ |
dc503a8a | 2804 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
61bd5218 | 2805 | mark_reg_not_init(env, regs, caller_saved[i]); |
dc503a8a EC |
2806 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
2807 | } | |
17a52670 | 2808 | |
dc503a8a | 2809 | /* update return register (already marked as written above) */ |
17a52670 | 2810 | if (fn->ret_type == RET_INTEGER) { |
f1174f77 | 2811 | /* sets type to SCALAR_VALUE */ |
61bd5218 | 2812 | mark_reg_unknown(env, regs, BPF_REG_0); |
17a52670 AS |
2813 | } else if (fn->ret_type == RET_VOID) { |
2814 | regs[BPF_REG_0].type = NOT_INIT; | |
3e6a4b3e RG |
2815 | } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL || |
2816 | fn->ret_type == RET_PTR_TO_MAP_VALUE) { | |
2817 | if (fn->ret_type == RET_PTR_TO_MAP_VALUE) | |
2818 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE; | |
2819 | else | |
2820 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL; | |
f1174f77 | 2821 | /* There is no offset yet applied, variable or fixed */ |
61bd5218 | 2822 | mark_reg_known_zero(env, regs, BPF_REG_0); |
17a52670 AS |
2823 | /* remember map_ptr, so that check_map_access() |
2824 | * can check 'value_size' boundary of memory access | |
2825 | * to map element returned from bpf_map_lookup_elem() | |
2826 | */ | |
33ff9823 | 2827 | if (meta.map_ptr == NULL) { |
61bd5218 JK |
2828 | verbose(env, |
2829 | "kernel subsystem misconfigured verifier\n"); | |
17a52670 AS |
2830 | return -EINVAL; |
2831 | } | |
33ff9823 | 2832 | regs[BPF_REG_0].map_ptr = meta.map_ptr; |
57a09bf0 | 2833 | regs[BPF_REG_0].id = ++env->id_gen; |
c64b7983 | 2834 | } else if (fn->ret_type == RET_PTR_TO_SOCKET_OR_NULL) { |
fd978bf7 JS |
2835 | int id = acquire_reference_state(env, insn_idx); |
2836 | if (id < 0) | |
2837 | return id; | |
c64b7983 JS |
2838 | mark_reg_known_zero(env, regs, BPF_REG_0); |
2839 | regs[BPF_REG_0].type = PTR_TO_SOCKET_OR_NULL; | |
fd978bf7 | 2840 | regs[BPF_REG_0].id = id; |
17a52670 | 2841 | } else { |
61bd5218 | 2842 | verbose(env, "unknown return type %d of func %s#%d\n", |
ebb676da | 2843 | fn->ret_type, func_id_name(func_id), func_id); |
17a52670 AS |
2844 | return -EINVAL; |
2845 | } | |
04fd61ab | 2846 | |
849fa506 YS |
2847 | do_refine_retval_range(regs, fn->ret_type, func_id, &meta); |
2848 | ||
61bd5218 | 2849 | err = check_map_func_compatibility(env, meta.map_ptr, func_id); |
35578d79 KX |
2850 | if (err) |
2851 | return err; | |
04fd61ab | 2852 | |
c195651e YS |
2853 | if (func_id == BPF_FUNC_get_stack && !env->prog->has_callchain_buf) { |
2854 | const char *err_str; | |
2855 | ||
2856 | #ifdef CONFIG_PERF_EVENTS | |
2857 | err = get_callchain_buffers(sysctl_perf_event_max_stack); | |
2858 | err_str = "cannot get callchain buffer for func %s#%d\n"; | |
2859 | #else | |
2860 | err = -ENOTSUPP; | |
2861 | err_str = "func %s#%d not supported without CONFIG_PERF_EVENTS\n"; | |
2862 | #endif | |
2863 | if (err) { | |
2864 | verbose(env, err_str, func_id_name(func_id), func_id); | |
2865 | return err; | |
2866 | } | |
2867 | ||
2868 | env->prog->has_callchain_buf = true; | |
2869 | } | |
2870 | ||
969bf05e AS |
2871 | if (changes_data) |
2872 | clear_all_pkt_pointers(env); | |
2873 | return 0; | |
2874 | } | |
2875 | ||
b03c9f9f EC |
2876 | static bool signed_add_overflows(s64 a, s64 b) |
2877 | { | |
2878 | /* Do the add in u64, where overflow is well-defined */ | |
2879 | s64 res = (s64)((u64)a + (u64)b); | |
2880 | ||
2881 | if (b < 0) | |
2882 | return res > a; | |
2883 | return res < a; | |
2884 | } | |
2885 | ||
2886 | static bool signed_sub_overflows(s64 a, s64 b) | |
2887 | { | |
2888 | /* Do the sub in u64, where overflow is well-defined */ | |
2889 | s64 res = (s64)((u64)a - (u64)b); | |
2890 | ||
2891 | if (b < 0) | |
2892 | return res < a; | |
2893 | return res > a; | |
969bf05e AS |
2894 | } |
2895 | ||
bb7f0f98 AS |
2896 | static bool check_reg_sane_offset(struct bpf_verifier_env *env, |
2897 | const struct bpf_reg_state *reg, | |
2898 | enum bpf_reg_type type) | |
2899 | { | |
2900 | bool known = tnum_is_const(reg->var_off); | |
2901 | s64 val = reg->var_off.value; | |
2902 | s64 smin = reg->smin_value; | |
2903 | ||
2904 | if (known && (val >= BPF_MAX_VAR_OFF || val <= -BPF_MAX_VAR_OFF)) { | |
2905 | verbose(env, "math between %s pointer and %lld is not allowed\n", | |
2906 | reg_type_str[type], val); | |
2907 | return false; | |
2908 | } | |
2909 | ||
2910 | if (reg->off >= BPF_MAX_VAR_OFF || reg->off <= -BPF_MAX_VAR_OFF) { | |
2911 | verbose(env, "%s pointer offset %d is not allowed\n", | |
2912 | reg_type_str[type], reg->off); | |
2913 | return false; | |
2914 | } | |
2915 | ||
2916 | if (smin == S64_MIN) { | |
2917 | verbose(env, "math between %s pointer and register with unbounded min value is not allowed\n", | |
2918 | reg_type_str[type]); | |
2919 | return false; | |
2920 | } | |
2921 | ||
2922 | if (smin >= BPF_MAX_VAR_OFF || smin <= -BPF_MAX_VAR_OFF) { | |
2923 | verbose(env, "value %lld makes %s pointer be out of bounds\n", | |
2924 | smin, reg_type_str[type]); | |
2925 | return false; | |
2926 | } | |
2927 | ||
2928 | return true; | |
2929 | } | |
2930 | ||
f1174f77 | 2931 | /* Handles arithmetic on a pointer and a scalar: computes new min/max and var_off. |
f1174f77 EC |
2932 | * Caller should also handle BPF_MOV case separately. |
2933 | * If we return -EACCES, caller may want to try again treating pointer as a | |
2934 | * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks. | |
2935 | */ | |
2936 | static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, | |
2937 | struct bpf_insn *insn, | |
2938 | const struct bpf_reg_state *ptr_reg, | |
2939 | const struct bpf_reg_state *off_reg) | |
969bf05e | 2940 | { |
f4d7e40a AS |
2941 | struct bpf_verifier_state *vstate = env->cur_state; |
2942 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
2943 | struct bpf_reg_state *regs = state->regs, *dst_reg; | |
f1174f77 | 2944 | bool known = tnum_is_const(off_reg->var_off); |
b03c9f9f EC |
2945 | s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value, |
2946 | smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value; | |
2947 | u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value, | |
2948 | umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value; | |
969bf05e | 2949 | u8 opcode = BPF_OP(insn->code); |
f1174f77 | 2950 | u32 dst = insn->dst_reg; |
969bf05e | 2951 | |
f1174f77 | 2952 | dst_reg = ®s[dst]; |
969bf05e | 2953 | |
6f16101e DB |
2954 | if ((known && (smin_val != smax_val || umin_val != umax_val)) || |
2955 | smin_val > smax_val || umin_val > umax_val) { | |
2956 | /* Taint dst register if offset had invalid bounds derived from | |
2957 | * e.g. dead branches. | |
2958 | */ | |
2959 | __mark_reg_unknown(dst_reg); | |
2960 | return 0; | |
f1174f77 EC |
2961 | } |
2962 | ||
2963 | if (BPF_CLASS(insn->code) != BPF_ALU64) { | |
2964 | /* 32-bit ALU ops on pointers produce (meaningless) scalars */ | |
82abbf8d AS |
2965 | verbose(env, |
2966 | "R%d 32-bit pointer arithmetic prohibited\n", | |
2967 | dst); | |
f1174f77 | 2968 | return -EACCES; |
969bf05e AS |
2969 | } |
2970 | ||
aad2eeaf JS |
2971 | switch (ptr_reg->type) { |
2972 | case PTR_TO_MAP_VALUE_OR_NULL: | |
2973 | verbose(env, "R%d pointer arithmetic on %s prohibited, null-check it first\n", | |
2974 | dst, reg_type_str[ptr_reg->type]); | |
f1174f77 | 2975 | return -EACCES; |
aad2eeaf JS |
2976 | case CONST_PTR_TO_MAP: |
2977 | case PTR_TO_PACKET_END: | |
c64b7983 JS |
2978 | case PTR_TO_SOCKET: |
2979 | case PTR_TO_SOCKET_OR_NULL: | |
aad2eeaf JS |
2980 | verbose(env, "R%d pointer arithmetic on %s prohibited\n", |
2981 | dst, reg_type_str[ptr_reg->type]); | |
f1174f77 | 2982 | return -EACCES; |
aad2eeaf JS |
2983 | default: |
2984 | break; | |
f1174f77 EC |
2985 | } |
2986 | ||
2987 | /* In case of 'scalar += pointer', dst_reg inherits pointer type and id. | |
2988 | * The id may be overwritten later if we create a new variable offset. | |
969bf05e | 2989 | */ |
f1174f77 EC |
2990 | dst_reg->type = ptr_reg->type; |
2991 | dst_reg->id = ptr_reg->id; | |
969bf05e | 2992 | |
bb7f0f98 AS |
2993 | if (!check_reg_sane_offset(env, off_reg, ptr_reg->type) || |
2994 | !check_reg_sane_offset(env, ptr_reg, ptr_reg->type)) | |
2995 | return -EINVAL; | |
2996 | ||
f1174f77 EC |
2997 | switch (opcode) { |
2998 | case BPF_ADD: | |
2999 | /* We can take a fixed offset as long as it doesn't overflow | |
3000 | * the s32 'off' field | |
969bf05e | 3001 | */ |
b03c9f9f EC |
3002 | if (known && (ptr_reg->off + smin_val == |
3003 | (s64)(s32)(ptr_reg->off + smin_val))) { | |
f1174f77 | 3004 | /* pointer += K. Accumulate it into fixed offset */ |
b03c9f9f EC |
3005 | dst_reg->smin_value = smin_ptr; |
3006 | dst_reg->smax_value = smax_ptr; | |
3007 | dst_reg->umin_value = umin_ptr; | |
3008 | dst_reg->umax_value = umax_ptr; | |
f1174f77 | 3009 | dst_reg->var_off = ptr_reg->var_off; |
b03c9f9f | 3010 | dst_reg->off = ptr_reg->off + smin_val; |
f1174f77 EC |
3011 | dst_reg->range = ptr_reg->range; |
3012 | break; | |
3013 | } | |
f1174f77 EC |
3014 | /* A new variable offset is created. Note that off_reg->off |
3015 | * == 0, since it's a scalar. | |
3016 | * dst_reg gets the pointer type and since some positive | |
3017 | * integer value was added to the pointer, give it a new 'id' | |
3018 | * if it's a PTR_TO_PACKET. | |
3019 | * this creates a new 'base' pointer, off_reg (variable) gets | |
3020 | * added into the variable offset, and we copy the fixed offset | |
3021 | * from ptr_reg. | |
969bf05e | 3022 | */ |
b03c9f9f EC |
3023 | if (signed_add_overflows(smin_ptr, smin_val) || |
3024 | signed_add_overflows(smax_ptr, smax_val)) { | |
3025 | dst_reg->smin_value = S64_MIN; | |
3026 | dst_reg->smax_value = S64_MAX; | |
3027 | } else { | |
3028 | dst_reg->smin_value = smin_ptr + smin_val; | |
3029 | dst_reg->smax_value = smax_ptr + smax_val; | |
3030 | } | |
3031 | if (umin_ptr + umin_val < umin_ptr || | |
3032 | umax_ptr + umax_val < umax_ptr) { | |
3033 | dst_reg->umin_value = 0; | |
3034 | dst_reg->umax_value = U64_MAX; | |
3035 | } else { | |
3036 | dst_reg->umin_value = umin_ptr + umin_val; | |
3037 | dst_reg->umax_value = umax_ptr + umax_val; | |
3038 | } | |
f1174f77 EC |
3039 | dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off); |
3040 | dst_reg->off = ptr_reg->off; | |
de8f3a83 | 3041 | if (reg_is_pkt_pointer(ptr_reg)) { |
f1174f77 EC |
3042 | dst_reg->id = ++env->id_gen; |
3043 | /* something was added to pkt_ptr, set range to zero */ | |
3044 | dst_reg->range = 0; | |
3045 | } | |
3046 | break; | |
3047 | case BPF_SUB: | |
3048 | if (dst_reg == off_reg) { | |
3049 | /* scalar -= pointer. Creates an unknown scalar */ | |
82abbf8d AS |
3050 | verbose(env, "R%d tried to subtract pointer from scalar\n", |
3051 | dst); | |
f1174f77 EC |
3052 | return -EACCES; |
3053 | } | |
3054 | /* We don't allow subtraction from FP, because (according to | |
3055 | * test_verifier.c test "invalid fp arithmetic", JITs might not | |
3056 | * be able to deal with it. | |
969bf05e | 3057 | */ |
f1174f77 | 3058 | if (ptr_reg->type == PTR_TO_STACK) { |
82abbf8d AS |
3059 | verbose(env, "R%d subtraction from stack pointer prohibited\n", |
3060 | dst); | |
f1174f77 EC |
3061 | return -EACCES; |
3062 | } | |
b03c9f9f EC |
3063 | if (known && (ptr_reg->off - smin_val == |
3064 | (s64)(s32)(ptr_reg->off - smin_val))) { | |
f1174f77 | 3065 | /* pointer -= K. Subtract it from fixed offset */ |
b03c9f9f EC |
3066 | dst_reg->smin_value = smin_ptr; |
3067 | dst_reg->smax_value = smax_ptr; | |
3068 | dst_reg->umin_value = umin_ptr; | |
3069 | dst_reg->umax_value = umax_ptr; | |
f1174f77 EC |
3070 | dst_reg->var_off = ptr_reg->var_off; |
3071 | dst_reg->id = ptr_reg->id; | |
b03c9f9f | 3072 | dst_reg->off = ptr_reg->off - smin_val; |
f1174f77 EC |
3073 | dst_reg->range = ptr_reg->range; |
3074 | break; | |
3075 | } | |
f1174f77 EC |
3076 | /* A new variable offset is created. If the subtrahend is known |
3077 | * nonnegative, then any reg->range we had before is still good. | |
969bf05e | 3078 | */ |
b03c9f9f EC |
3079 | if (signed_sub_overflows(smin_ptr, smax_val) || |
3080 | signed_sub_overflows(smax_ptr, smin_val)) { | |
3081 | /* Overflow possible, we know nothing */ | |
3082 | dst_reg->smin_value = S64_MIN; | |
3083 | dst_reg->smax_value = S64_MAX; | |
3084 | } else { | |
3085 | dst_reg->smin_value = smin_ptr - smax_val; | |
3086 | dst_reg->smax_value = smax_ptr - smin_val; | |
3087 | } | |
3088 | if (umin_ptr < umax_val) { | |
3089 | /* Overflow possible, we know nothing */ | |
3090 | dst_reg->umin_value = 0; | |
3091 | dst_reg->umax_value = U64_MAX; | |
3092 | } else { | |
3093 | /* Cannot overflow (as long as bounds are consistent) */ | |
3094 | dst_reg->umin_value = umin_ptr - umax_val; | |
3095 | dst_reg->umax_value = umax_ptr - umin_val; | |
3096 | } | |
f1174f77 EC |
3097 | dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off); |
3098 | dst_reg->off = ptr_reg->off; | |
de8f3a83 | 3099 | if (reg_is_pkt_pointer(ptr_reg)) { |
f1174f77 EC |
3100 | dst_reg->id = ++env->id_gen; |
3101 | /* something was added to pkt_ptr, set range to zero */ | |
b03c9f9f | 3102 | if (smin_val < 0) |
f1174f77 | 3103 | dst_reg->range = 0; |
43188702 | 3104 | } |
f1174f77 EC |
3105 | break; |
3106 | case BPF_AND: | |
3107 | case BPF_OR: | |
3108 | case BPF_XOR: | |
82abbf8d AS |
3109 | /* bitwise ops on pointers are troublesome, prohibit. */ |
3110 | verbose(env, "R%d bitwise operator %s on pointer prohibited\n", | |
3111 | dst, bpf_alu_string[opcode >> 4]); | |
f1174f77 EC |
3112 | return -EACCES; |
3113 | default: | |
3114 | /* other operators (e.g. MUL,LSH) produce non-pointer results */ | |
82abbf8d AS |
3115 | verbose(env, "R%d pointer arithmetic with %s operator prohibited\n", |
3116 | dst, bpf_alu_string[opcode >> 4]); | |
f1174f77 | 3117 | return -EACCES; |
43188702 JF |
3118 | } |
3119 | ||
bb7f0f98 AS |
3120 | if (!check_reg_sane_offset(env, dst_reg, ptr_reg->type)) |
3121 | return -EINVAL; | |
3122 | ||
b03c9f9f EC |
3123 | __update_reg_bounds(dst_reg); |
3124 | __reg_deduce_bounds(dst_reg); | |
3125 | __reg_bound_offset(dst_reg); | |
43188702 JF |
3126 | return 0; |
3127 | } | |
3128 | ||
468f6eaf JH |
3129 | /* WARNING: This function does calculations on 64-bit values, but the actual |
3130 | * execution may occur on 32-bit values. Therefore, things like bitshifts | |
3131 | * need extra checks in the 32-bit case. | |
3132 | */ | |
f1174f77 EC |
3133 | static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env, |
3134 | struct bpf_insn *insn, | |
3135 | struct bpf_reg_state *dst_reg, | |
3136 | struct bpf_reg_state src_reg) | |
969bf05e | 3137 | { |
638f5b90 | 3138 | struct bpf_reg_state *regs = cur_regs(env); |
48461135 | 3139 | u8 opcode = BPF_OP(insn->code); |
f1174f77 | 3140 | bool src_known, dst_known; |
b03c9f9f EC |
3141 | s64 smin_val, smax_val; |
3142 | u64 umin_val, umax_val; | |
468f6eaf | 3143 | u64 insn_bitness = (BPF_CLASS(insn->code) == BPF_ALU64) ? 64 : 32; |
48461135 | 3144 | |
b03c9f9f EC |
3145 | smin_val = src_reg.smin_value; |
3146 | smax_val = src_reg.smax_value; | |
3147 | umin_val = src_reg.umin_value; | |
3148 | umax_val = src_reg.umax_value; | |
f1174f77 EC |
3149 | src_known = tnum_is_const(src_reg.var_off); |
3150 | dst_known = tnum_is_const(dst_reg->var_off); | |
f23cc643 | 3151 | |
6f16101e DB |
3152 | if ((src_known && (smin_val != smax_val || umin_val != umax_val)) || |
3153 | smin_val > smax_val || umin_val > umax_val) { | |
3154 | /* Taint dst register if offset had invalid bounds derived from | |
3155 | * e.g. dead branches. | |
3156 | */ | |
3157 | __mark_reg_unknown(dst_reg); | |
3158 | return 0; | |
3159 | } | |
3160 | ||
bb7f0f98 AS |
3161 | if (!src_known && |
3162 | opcode != BPF_ADD && opcode != BPF_SUB && opcode != BPF_AND) { | |
3163 | __mark_reg_unknown(dst_reg); | |
3164 | return 0; | |
3165 | } | |
3166 | ||
48461135 JB |
3167 | switch (opcode) { |
3168 | case BPF_ADD: | |
b03c9f9f EC |
3169 | if (signed_add_overflows(dst_reg->smin_value, smin_val) || |
3170 | signed_add_overflows(dst_reg->smax_value, smax_val)) { | |
3171 | dst_reg->smin_value = S64_MIN; | |
3172 | dst_reg->smax_value = S64_MAX; | |
3173 | } else { | |
3174 | dst_reg->smin_value += smin_val; | |
3175 | dst_reg->smax_value += smax_val; | |
3176 | } | |
3177 | if (dst_reg->umin_value + umin_val < umin_val || | |
3178 | dst_reg->umax_value + umax_val < umax_val) { | |
3179 | dst_reg->umin_value = 0; | |
3180 | dst_reg->umax_value = U64_MAX; | |
3181 | } else { | |
3182 | dst_reg->umin_value += umin_val; | |
3183 | dst_reg->umax_value += umax_val; | |
3184 | } | |
f1174f77 | 3185 | dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off); |
48461135 JB |
3186 | break; |
3187 | case BPF_SUB: | |
b03c9f9f EC |
3188 | if (signed_sub_overflows(dst_reg->smin_value, smax_val) || |
3189 | signed_sub_overflows(dst_reg->smax_value, smin_val)) { | |
3190 | /* Overflow possible, we know nothing */ | |
3191 | dst_reg->smin_value = S64_MIN; | |
3192 | dst_reg->smax_value = S64_MAX; | |
3193 | } else { | |
3194 | dst_reg->smin_value -= smax_val; | |
3195 | dst_reg->smax_value -= smin_val; | |
3196 | } | |
3197 | if (dst_reg->umin_value < umax_val) { | |
3198 | /* Overflow possible, we know nothing */ | |
3199 | dst_reg->umin_value = 0; | |
3200 | dst_reg->umax_value = U64_MAX; | |
3201 | } else { | |
3202 | /* Cannot overflow (as long as bounds are consistent) */ | |
3203 | dst_reg->umin_value -= umax_val; | |
3204 | dst_reg->umax_value -= umin_val; | |
3205 | } | |
f1174f77 | 3206 | dst_reg->var_off = tnum_sub(dst_reg->var_off, src_reg.var_off); |
48461135 JB |
3207 | break; |
3208 | case BPF_MUL: | |
b03c9f9f EC |
3209 | dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off); |
3210 | if (smin_val < 0 || dst_reg->smin_value < 0) { | |
f1174f77 | 3211 | /* Ain't nobody got time to multiply that sign */ |
b03c9f9f EC |
3212 | __mark_reg_unbounded(dst_reg); |
3213 | __update_reg_bounds(dst_reg); | |
f1174f77 EC |
3214 | break; |
3215 | } | |
b03c9f9f EC |
3216 | /* Both values are positive, so we can work with unsigned and |
3217 | * copy the result to signed (unless it exceeds S64_MAX). | |
f1174f77 | 3218 | */ |
b03c9f9f EC |
3219 | if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) { |
3220 | /* Potential overflow, we know nothing */ | |
3221 | __mark_reg_unbounded(dst_reg); | |
3222 | /* (except what we can learn from the var_off) */ | |
3223 | __update_reg_bounds(dst_reg); | |
3224 | break; | |
3225 | } | |
3226 | dst_reg->umin_value *= umin_val; | |
3227 | dst_reg->umax_value *= umax_val; | |
3228 | if (dst_reg->umax_value > S64_MAX) { | |
3229 | /* Overflow possible, we know nothing */ | |
3230 | dst_reg->smin_value = S64_MIN; | |
3231 | dst_reg->smax_value = S64_MAX; | |
3232 | } else { | |
3233 | dst_reg->smin_value = dst_reg->umin_value; | |
3234 | dst_reg->smax_value = dst_reg->umax_value; | |
3235 | } | |
48461135 JB |
3236 | break; |
3237 | case BPF_AND: | |
f1174f77 | 3238 | if (src_known && dst_known) { |
b03c9f9f EC |
3239 | __mark_reg_known(dst_reg, dst_reg->var_off.value & |
3240 | src_reg.var_off.value); | |
f1174f77 EC |
3241 | break; |
3242 | } | |
b03c9f9f EC |
3243 | /* We get our minimum from the var_off, since that's inherently |
3244 | * bitwise. Our maximum is the minimum of the operands' maxima. | |
f23cc643 | 3245 | */ |
f1174f77 | 3246 | dst_reg->var_off = tnum_and(dst_reg->var_off, src_reg.var_off); |
b03c9f9f EC |
3247 | dst_reg->umin_value = dst_reg->var_off.value; |
3248 | dst_reg->umax_value = min(dst_reg->umax_value, umax_val); | |
3249 | if (dst_reg->smin_value < 0 || smin_val < 0) { | |
3250 | /* Lose signed bounds when ANDing negative numbers, | |
3251 | * ain't nobody got time for that. | |
3252 | */ | |
3253 | dst_reg->smin_value = S64_MIN; | |
3254 | dst_reg->smax_value = S64_MAX; | |
3255 | } else { | |
3256 | /* ANDing two positives gives a positive, so safe to | |
3257 | * cast result into s64. | |
3258 | */ | |
3259 | dst_reg->smin_value = dst_reg->umin_value; | |
3260 | dst_reg->smax_value = dst_reg->umax_value; | |
3261 | } | |
3262 | /* We may learn something more from the var_off */ | |
3263 | __update_reg_bounds(dst_reg); | |
f1174f77 EC |
3264 | break; |
3265 | case BPF_OR: | |
3266 | if (src_known && dst_known) { | |
b03c9f9f EC |
3267 | __mark_reg_known(dst_reg, dst_reg->var_off.value | |
3268 | src_reg.var_off.value); | |
f1174f77 EC |
3269 | break; |
3270 | } | |
b03c9f9f EC |
3271 | /* We get our maximum from the var_off, and our minimum is the |
3272 | * maximum of the operands' minima | |
f1174f77 EC |
3273 | */ |
3274 | dst_reg->var_off = tnum_or(dst_reg->var_off, src_reg.var_off); | |
b03c9f9f EC |
3275 | dst_reg->umin_value = max(dst_reg->umin_value, umin_val); |
3276 | dst_reg->umax_value = dst_reg->var_off.value | | |
3277 | dst_reg->var_off.mask; | |
3278 | if (dst_reg->smin_value < 0 || smin_val < 0) { | |
3279 | /* Lose signed bounds when ORing negative numbers, | |
3280 | * ain't nobody got time for that. | |
3281 | */ | |
3282 | dst_reg->smin_value = S64_MIN; | |
3283 | dst_reg->smax_value = S64_MAX; | |
f1174f77 | 3284 | } else { |
b03c9f9f EC |
3285 | /* ORing two positives gives a positive, so safe to |
3286 | * cast result into s64. | |
3287 | */ | |
3288 | dst_reg->smin_value = dst_reg->umin_value; | |
3289 | dst_reg->smax_value = dst_reg->umax_value; | |
f1174f77 | 3290 | } |
b03c9f9f EC |
3291 | /* We may learn something more from the var_off */ |
3292 | __update_reg_bounds(dst_reg); | |
48461135 JB |
3293 | break; |
3294 | case BPF_LSH: | |
468f6eaf JH |
3295 | if (umax_val >= insn_bitness) { |
3296 | /* Shifts greater than 31 or 63 are undefined. | |
3297 | * This includes shifts by a negative number. | |
b03c9f9f | 3298 | */ |
61bd5218 | 3299 | mark_reg_unknown(env, regs, insn->dst_reg); |
f1174f77 EC |
3300 | break; |
3301 | } | |
b03c9f9f EC |
3302 | /* We lose all sign bit information (except what we can pick |
3303 | * up from var_off) | |
48461135 | 3304 | */ |
b03c9f9f EC |
3305 | dst_reg->smin_value = S64_MIN; |
3306 | dst_reg->smax_value = S64_MAX; | |
3307 | /* If we might shift our top bit out, then we know nothing */ | |
3308 | if (dst_reg->umax_value > 1ULL << (63 - umax_val)) { | |
3309 | dst_reg->umin_value = 0; | |
3310 | dst_reg->umax_value = U64_MAX; | |
d1174416 | 3311 | } else { |
b03c9f9f EC |
3312 | dst_reg->umin_value <<= umin_val; |
3313 | dst_reg->umax_value <<= umax_val; | |
d1174416 | 3314 | } |
afbe1a5b | 3315 | dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val); |
b03c9f9f EC |
3316 | /* We may learn something more from the var_off */ |
3317 | __update_reg_bounds(dst_reg); | |
48461135 JB |
3318 | break; |
3319 | case BPF_RSH: | |
468f6eaf JH |
3320 | if (umax_val >= insn_bitness) { |
3321 | /* Shifts greater than 31 or 63 are undefined. | |
3322 | * This includes shifts by a negative number. | |
b03c9f9f | 3323 | */ |
61bd5218 | 3324 | mark_reg_unknown(env, regs, insn->dst_reg); |
f1174f77 EC |
3325 | break; |
3326 | } | |
4374f256 EC |
3327 | /* BPF_RSH is an unsigned shift. If the value in dst_reg might |
3328 | * be negative, then either: | |
3329 | * 1) src_reg might be zero, so the sign bit of the result is | |
3330 | * unknown, so we lose our signed bounds | |
3331 | * 2) it's known negative, thus the unsigned bounds capture the | |
3332 | * signed bounds | |
3333 | * 3) the signed bounds cross zero, so they tell us nothing | |
3334 | * about the result | |
3335 | * If the value in dst_reg is known nonnegative, then again the | |
3336 | * unsigned bounts capture the signed bounds. | |
3337 | * Thus, in all cases it suffices to blow away our signed bounds | |
3338 | * and rely on inferring new ones from the unsigned bounds and | |
3339 | * var_off of the result. | |
3340 | */ | |
3341 | dst_reg->smin_value = S64_MIN; | |
3342 | dst_reg->smax_value = S64_MAX; | |
afbe1a5b | 3343 | dst_reg->var_off = tnum_rshift(dst_reg->var_off, umin_val); |
b03c9f9f EC |
3344 | dst_reg->umin_value >>= umax_val; |
3345 | dst_reg->umax_value >>= umin_val; | |
3346 | /* We may learn something more from the var_off */ | |
3347 | __update_reg_bounds(dst_reg); | |
48461135 | 3348 | break; |
9cbe1f5a YS |
3349 | case BPF_ARSH: |
3350 | if (umax_val >= insn_bitness) { | |
3351 | /* Shifts greater than 31 or 63 are undefined. | |
3352 | * This includes shifts by a negative number. | |
3353 | */ | |
3354 | mark_reg_unknown(env, regs, insn->dst_reg); | |
3355 | break; | |
3356 | } | |
3357 | ||
3358 | /* Upon reaching here, src_known is true and | |
3359 | * umax_val is equal to umin_val. | |
3360 | */ | |
3361 | dst_reg->smin_value >>= umin_val; | |
3362 | dst_reg->smax_value >>= umin_val; | |
3363 | dst_reg->var_off = tnum_arshift(dst_reg->var_off, umin_val); | |
3364 | ||
3365 | /* blow away the dst_reg umin_value/umax_value and rely on | |
3366 | * dst_reg var_off to refine the result. | |
3367 | */ | |
3368 | dst_reg->umin_value = 0; | |
3369 | dst_reg->umax_value = U64_MAX; | |
3370 | __update_reg_bounds(dst_reg); | |
3371 | break; | |
48461135 | 3372 | default: |
61bd5218 | 3373 | mark_reg_unknown(env, regs, insn->dst_reg); |
48461135 JB |
3374 | break; |
3375 | } | |
3376 | ||
468f6eaf JH |
3377 | if (BPF_CLASS(insn->code) != BPF_ALU64) { |
3378 | /* 32-bit ALU ops are (32,32)->32 */ | |
3379 | coerce_reg_to_size(dst_reg, 4); | |
3380 | coerce_reg_to_size(&src_reg, 4); | |
3381 | } | |
3382 | ||
b03c9f9f EC |
3383 | __reg_deduce_bounds(dst_reg); |
3384 | __reg_bound_offset(dst_reg); | |
f1174f77 EC |
3385 | return 0; |
3386 | } | |
3387 | ||
3388 | /* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max | |
3389 | * and var_off. | |
3390 | */ | |
3391 | static int adjust_reg_min_max_vals(struct bpf_verifier_env *env, | |
3392 | struct bpf_insn *insn) | |
3393 | { | |
f4d7e40a AS |
3394 | struct bpf_verifier_state *vstate = env->cur_state; |
3395 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; | |
3396 | struct bpf_reg_state *regs = state->regs, *dst_reg, *src_reg; | |
f1174f77 EC |
3397 | struct bpf_reg_state *ptr_reg = NULL, off_reg = {0}; |
3398 | u8 opcode = BPF_OP(insn->code); | |
f1174f77 EC |
3399 | |
3400 | dst_reg = ®s[insn->dst_reg]; | |
f1174f77 EC |
3401 | src_reg = NULL; |
3402 | if (dst_reg->type != SCALAR_VALUE) | |
3403 | ptr_reg = dst_reg; | |
3404 | if (BPF_SRC(insn->code) == BPF_X) { | |
3405 | src_reg = ®s[insn->src_reg]; | |
f1174f77 EC |
3406 | if (src_reg->type != SCALAR_VALUE) { |
3407 | if (dst_reg->type != SCALAR_VALUE) { | |
3408 | /* Combining two pointers by any ALU op yields | |
82abbf8d AS |
3409 | * an arbitrary scalar. Disallow all math except |
3410 | * pointer subtraction | |
f1174f77 | 3411 | */ |
dd066823 | 3412 | if (opcode == BPF_SUB && env->allow_ptr_leaks) { |
82abbf8d AS |
3413 | mark_reg_unknown(env, regs, insn->dst_reg); |
3414 | return 0; | |
f1174f77 | 3415 | } |
82abbf8d AS |
3416 | verbose(env, "R%d pointer %s pointer prohibited\n", |
3417 | insn->dst_reg, | |
3418 | bpf_alu_string[opcode >> 4]); | |
3419 | return -EACCES; | |
f1174f77 EC |
3420 | } else { |
3421 | /* scalar += pointer | |
3422 | * This is legal, but we have to reverse our | |
3423 | * src/dest handling in computing the range | |
3424 | */ | |
82abbf8d AS |
3425 | return adjust_ptr_min_max_vals(env, insn, |
3426 | src_reg, dst_reg); | |
f1174f77 EC |
3427 | } |
3428 | } else if (ptr_reg) { | |
3429 | /* pointer += scalar */ | |
82abbf8d AS |
3430 | return adjust_ptr_min_max_vals(env, insn, |
3431 | dst_reg, src_reg); | |
f1174f77 EC |
3432 | } |
3433 | } else { | |
3434 | /* Pretend the src is a reg with a known value, since we only | |
3435 | * need to be able to read from this state. | |
3436 | */ | |
3437 | off_reg.type = SCALAR_VALUE; | |
b03c9f9f | 3438 | __mark_reg_known(&off_reg, insn->imm); |
f1174f77 | 3439 | src_reg = &off_reg; |
82abbf8d AS |
3440 | if (ptr_reg) /* pointer += K */ |
3441 | return adjust_ptr_min_max_vals(env, insn, | |
3442 | ptr_reg, src_reg); | |
f1174f77 EC |
3443 | } |
3444 | ||
3445 | /* Got here implies adding two SCALAR_VALUEs */ | |
3446 | if (WARN_ON_ONCE(ptr_reg)) { | |
f4d7e40a | 3447 | print_verifier_state(env, state); |
61bd5218 | 3448 | verbose(env, "verifier internal error: unexpected ptr_reg\n"); |
f1174f77 EC |
3449 | return -EINVAL; |
3450 | } | |
3451 | if (WARN_ON(!src_reg)) { | |
f4d7e40a | 3452 | print_verifier_state(env, state); |
61bd5218 | 3453 | verbose(env, "verifier internal error: no src_reg\n"); |
f1174f77 EC |
3454 | return -EINVAL; |
3455 | } | |
3456 | return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg); | |
48461135 JB |
3457 | } |
3458 | ||
17a52670 | 3459 | /* check validity of 32-bit and 64-bit arithmetic operations */ |
58e2af8b | 3460 | static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn) |
17a52670 | 3461 | { |
638f5b90 | 3462 | struct bpf_reg_state *regs = cur_regs(env); |
17a52670 AS |
3463 | u8 opcode = BPF_OP(insn->code); |
3464 | int err; | |
3465 | ||
3466 | if (opcode == BPF_END || opcode == BPF_NEG) { | |
3467 | if (opcode == BPF_NEG) { | |
3468 | if (BPF_SRC(insn->code) != 0 || | |
3469 | insn->src_reg != BPF_REG_0 || | |
3470 | insn->off != 0 || insn->imm != 0) { | |
61bd5218 | 3471 | verbose(env, "BPF_NEG uses reserved fields\n"); |
17a52670 AS |
3472 | return -EINVAL; |
3473 | } | |
3474 | } else { | |
3475 | if (insn->src_reg != BPF_REG_0 || insn->off != 0 || | |
e67b8a68 EC |
3476 | (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) || |
3477 | BPF_CLASS(insn->code) == BPF_ALU64) { | |
61bd5218 | 3478 | verbose(env, "BPF_END uses reserved fields\n"); |
17a52670 AS |
3479 | return -EINVAL; |
3480 | } | |
3481 | } | |
3482 | ||
3483 | /* check src operand */ | |
dc503a8a | 3484 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
3485 | if (err) |
3486 | return err; | |
3487 | ||
1be7f75d | 3488 | if (is_pointer_value(env, insn->dst_reg)) { |
61bd5218 | 3489 | verbose(env, "R%d pointer arithmetic prohibited\n", |
1be7f75d AS |
3490 | insn->dst_reg); |
3491 | return -EACCES; | |
3492 | } | |
3493 | ||
17a52670 | 3494 | /* check dest operand */ |
dc503a8a | 3495 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
17a52670 AS |
3496 | if (err) |
3497 | return err; | |
3498 | ||
3499 | } else if (opcode == BPF_MOV) { | |
3500 | ||
3501 | if (BPF_SRC(insn->code) == BPF_X) { | |
3502 | if (insn->imm != 0 || insn->off != 0) { | |
61bd5218 | 3503 | verbose(env, "BPF_MOV uses reserved fields\n"); |
17a52670 AS |
3504 | return -EINVAL; |
3505 | } | |
3506 | ||
3507 | /* check src operand */ | |
dc503a8a | 3508 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
3509 | if (err) |
3510 | return err; | |
3511 | } else { | |
3512 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { | |
61bd5218 | 3513 | verbose(env, "BPF_MOV uses reserved fields\n"); |
17a52670 AS |
3514 | return -EINVAL; |
3515 | } | |
3516 | } | |
3517 | ||
fbeb1603 AF |
3518 | /* check dest operand, mark as required later */ |
3519 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); | |
17a52670 AS |
3520 | if (err) |
3521 | return err; | |
3522 | ||
3523 | if (BPF_SRC(insn->code) == BPF_X) { | |
3524 | if (BPF_CLASS(insn->code) == BPF_ALU64) { | |
3525 | /* case: R1 = R2 | |
3526 | * copy register state to dest reg | |
3527 | */ | |
3528 | regs[insn->dst_reg] = regs[insn->src_reg]; | |
8fe2d6cc | 3529 | regs[insn->dst_reg].live |= REG_LIVE_WRITTEN; |
17a52670 | 3530 | } else { |
f1174f77 | 3531 | /* R1 = (u32) R2 */ |
1be7f75d | 3532 | if (is_pointer_value(env, insn->src_reg)) { |
61bd5218 JK |
3533 | verbose(env, |
3534 | "R%d partial copy of pointer\n", | |
1be7f75d AS |
3535 | insn->src_reg); |
3536 | return -EACCES; | |
3537 | } | |
61bd5218 | 3538 | mark_reg_unknown(env, regs, insn->dst_reg); |
0c17d1d2 | 3539 | coerce_reg_to_size(®s[insn->dst_reg], 4); |
17a52670 AS |
3540 | } |
3541 | } else { | |
3542 | /* case: R = imm | |
3543 | * remember the value we stored into this reg | |
3544 | */ | |
fbeb1603 AF |
3545 | /* clear any state __mark_reg_known doesn't set */ |
3546 | mark_reg_unknown(env, regs, insn->dst_reg); | |
f1174f77 | 3547 | regs[insn->dst_reg].type = SCALAR_VALUE; |
95a762e2 JH |
3548 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
3549 | __mark_reg_known(regs + insn->dst_reg, | |
3550 | insn->imm); | |
3551 | } else { | |
3552 | __mark_reg_known(regs + insn->dst_reg, | |
3553 | (u32)insn->imm); | |
3554 | } | |
17a52670 AS |
3555 | } |
3556 | ||
3557 | } else if (opcode > BPF_END) { | |
61bd5218 | 3558 | verbose(env, "invalid BPF_ALU opcode %x\n", opcode); |
17a52670 AS |
3559 | return -EINVAL; |
3560 | ||
3561 | } else { /* all other ALU ops: and, sub, xor, add, ... */ | |
3562 | ||
17a52670 AS |
3563 | if (BPF_SRC(insn->code) == BPF_X) { |
3564 | if (insn->imm != 0 || insn->off != 0) { | |
61bd5218 | 3565 | verbose(env, "BPF_ALU uses reserved fields\n"); |
17a52670 AS |
3566 | return -EINVAL; |
3567 | } | |
3568 | /* check src1 operand */ | |
dc503a8a | 3569 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
3570 | if (err) |
3571 | return err; | |
3572 | } else { | |
3573 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { | |
61bd5218 | 3574 | verbose(env, "BPF_ALU uses reserved fields\n"); |
17a52670 AS |
3575 | return -EINVAL; |
3576 | } | |
3577 | } | |
3578 | ||
3579 | /* check src2 operand */ | |
dc503a8a | 3580 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
3581 | if (err) |
3582 | return err; | |
3583 | ||
3584 | if ((opcode == BPF_MOD || opcode == BPF_DIV) && | |
3585 | BPF_SRC(insn->code) == BPF_K && insn->imm == 0) { | |
61bd5218 | 3586 | verbose(env, "div by zero\n"); |
17a52670 AS |
3587 | return -EINVAL; |
3588 | } | |
3589 | ||
7891a87e DB |
3590 | if (opcode == BPF_ARSH && BPF_CLASS(insn->code) != BPF_ALU64) { |
3591 | verbose(env, "BPF_ARSH not supported for 32 bit ALU\n"); | |
3592 | return -EINVAL; | |
3593 | } | |
3594 | ||
229394e8 RV |
3595 | if ((opcode == BPF_LSH || opcode == BPF_RSH || |
3596 | opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) { | |
3597 | int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32; | |
3598 | ||
3599 | if (insn->imm < 0 || insn->imm >= size) { | |
61bd5218 | 3600 | verbose(env, "invalid shift %d\n", insn->imm); |
229394e8 RV |
3601 | return -EINVAL; |
3602 | } | |
3603 | } | |
3604 | ||
1a0dc1ac | 3605 | /* check dest operand */ |
dc503a8a | 3606 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
1a0dc1ac AS |
3607 | if (err) |
3608 | return err; | |
3609 | ||
f1174f77 | 3610 | return adjust_reg_min_max_vals(env, insn); |
17a52670 AS |
3611 | } |
3612 | ||
3613 | return 0; | |
3614 | } | |
3615 | ||
f4d7e40a | 3616 | static void find_good_pkt_pointers(struct bpf_verifier_state *vstate, |
de8f3a83 | 3617 | struct bpf_reg_state *dst_reg, |
f8ddadc4 | 3618 | enum bpf_reg_type type, |
fb2a311a | 3619 | bool range_right_open) |
969bf05e | 3620 | { |
f4d7e40a | 3621 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
58e2af8b | 3622 | struct bpf_reg_state *regs = state->regs, *reg; |
fb2a311a | 3623 | u16 new_range; |
f4d7e40a | 3624 | int i, j; |
2d2be8ca | 3625 | |
fb2a311a DB |
3626 | if (dst_reg->off < 0 || |
3627 | (dst_reg->off == 0 && range_right_open)) | |
f1174f77 EC |
3628 | /* This doesn't give us any range */ |
3629 | return; | |
3630 | ||
b03c9f9f EC |
3631 | if (dst_reg->umax_value > MAX_PACKET_OFF || |
3632 | dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF) | |
f1174f77 EC |
3633 | /* Risk of overflow. For instance, ptr + (1<<63) may be less |
3634 | * than pkt_end, but that's because it's also less than pkt. | |
3635 | */ | |
3636 | return; | |
3637 | ||
fb2a311a DB |
3638 | new_range = dst_reg->off; |
3639 | if (range_right_open) | |
3640 | new_range--; | |
3641 | ||
3642 | /* Examples for register markings: | |
2d2be8ca | 3643 | * |
fb2a311a | 3644 | * pkt_data in dst register: |
2d2be8ca DB |
3645 | * |
3646 | * r2 = r3; | |
3647 | * r2 += 8; | |
3648 | * if (r2 > pkt_end) goto <handle exception> | |
3649 | * <access okay> | |
3650 | * | |
b4e432f1 DB |
3651 | * r2 = r3; |
3652 | * r2 += 8; | |
3653 | * if (r2 < pkt_end) goto <access okay> | |
3654 | * <handle exception> | |
3655 | * | |
2d2be8ca DB |
3656 | * Where: |
3657 | * r2 == dst_reg, pkt_end == src_reg | |
3658 | * r2=pkt(id=n,off=8,r=0) | |
3659 | * r3=pkt(id=n,off=0,r=0) | |
3660 | * | |
fb2a311a | 3661 | * pkt_data in src register: |
2d2be8ca DB |
3662 | * |
3663 | * r2 = r3; | |
3664 | * r2 += 8; | |
3665 | * if (pkt_end >= r2) goto <access okay> | |
3666 | * <handle exception> | |
3667 | * | |
b4e432f1 DB |
3668 | * r2 = r3; |
3669 | * r2 += 8; | |
3670 | * if (pkt_end <= r2) goto <handle exception> | |
3671 | * <access okay> | |
3672 | * | |
2d2be8ca DB |
3673 | * Where: |
3674 | * pkt_end == dst_reg, r2 == src_reg | |
3675 | * r2=pkt(id=n,off=8,r=0) | |
3676 | * r3=pkt(id=n,off=0,r=0) | |
3677 | * | |
3678 | * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8) | |
fb2a311a DB |
3679 | * or r3=pkt(id=n,off=0,r=8-1), so that range of bytes [r3, r3 + 8) |
3680 | * and [r3, r3 + 8-1) respectively is safe to access depending on | |
3681 | * the check. | |
969bf05e | 3682 | */ |
2d2be8ca | 3683 | |
f1174f77 EC |
3684 | /* If our ids match, then we must have the same max_value. And we |
3685 | * don't care about the other reg's fixed offset, since if it's too big | |
3686 | * the range won't allow anything. | |
3687 | * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16. | |
3688 | */ | |
969bf05e | 3689 | for (i = 0; i < MAX_BPF_REG; i++) |
de8f3a83 | 3690 | if (regs[i].type == type && regs[i].id == dst_reg->id) |
b1977682 | 3691 | /* keep the maximum range already checked */ |
fb2a311a | 3692 | regs[i].range = max(regs[i].range, new_range); |
969bf05e | 3693 | |
f4d7e40a AS |
3694 | for (j = 0; j <= vstate->curframe; j++) { |
3695 | state = vstate->frame[j]; | |
f3709f69 JS |
3696 | bpf_for_each_spilled_reg(i, state, reg) { |
3697 | if (!reg) | |
f4d7e40a | 3698 | continue; |
f4d7e40a AS |
3699 | if (reg->type == type && reg->id == dst_reg->id) |
3700 | reg->range = max(reg->range, new_range); | |
3701 | } | |
969bf05e AS |
3702 | } |
3703 | } | |
3704 | ||
48461135 JB |
3705 | /* Adjusts the register min/max values in the case that the dst_reg is the |
3706 | * variable register that we are working on, and src_reg is a constant or we're | |
3707 | * simply doing a BPF_K check. | |
f1174f77 | 3708 | * In JEQ/JNE cases we also adjust the var_off values. |
48461135 JB |
3709 | */ |
3710 | static void reg_set_min_max(struct bpf_reg_state *true_reg, | |
3711 | struct bpf_reg_state *false_reg, u64 val, | |
3712 | u8 opcode) | |
3713 | { | |
f1174f77 EC |
3714 | /* If the dst_reg is a pointer, we can't learn anything about its |
3715 | * variable offset from the compare (unless src_reg were a pointer into | |
3716 | * the same object, but we don't bother with that. | |
3717 | * Since false_reg and true_reg have the same type by construction, we | |
3718 | * only need to check one of them for pointerness. | |
3719 | */ | |
3720 | if (__is_pointer_value(false, false_reg)) | |
3721 | return; | |
4cabc5b1 | 3722 | |
48461135 JB |
3723 | switch (opcode) { |
3724 | case BPF_JEQ: | |
3725 | /* If this is false then we know nothing Jon Snow, but if it is | |
3726 | * true then we know for sure. | |
3727 | */ | |
b03c9f9f | 3728 | __mark_reg_known(true_reg, val); |
48461135 JB |
3729 | break; |
3730 | case BPF_JNE: | |
3731 | /* If this is true we know nothing Jon Snow, but if it is false | |
3732 | * we know the value for sure; | |
3733 | */ | |
b03c9f9f | 3734 | __mark_reg_known(false_reg, val); |
48461135 JB |
3735 | break; |
3736 | case BPF_JGT: | |
b03c9f9f EC |
3737 | false_reg->umax_value = min(false_reg->umax_value, val); |
3738 | true_reg->umin_value = max(true_reg->umin_value, val + 1); | |
3739 | break; | |
48461135 | 3740 | case BPF_JSGT: |
b03c9f9f EC |
3741 | false_reg->smax_value = min_t(s64, false_reg->smax_value, val); |
3742 | true_reg->smin_value = max_t(s64, true_reg->smin_value, val + 1); | |
48461135 | 3743 | break; |
b4e432f1 DB |
3744 | case BPF_JLT: |
3745 | false_reg->umin_value = max(false_reg->umin_value, val); | |
3746 | true_reg->umax_value = min(true_reg->umax_value, val - 1); | |
3747 | break; | |
3748 | case BPF_JSLT: | |
3749 | false_reg->smin_value = max_t(s64, false_reg->smin_value, val); | |
3750 | true_reg->smax_value = min_t(s64, true_reg->smax_value, val - 1); | |
3751 | break; | |
48461135 | 3752 | case BPF_JGE: |
b03c9f9f EC |
3753 | false_reg->umax_value = min(false_reg->umax_value, val - 1); |
3754 | true_reg->umin_value = max(true_reg->umin_value, val); | |
3755 | break; | |
48461135 | 3756 | case BPF_JSGE: |
b03c9f9f EC |
3757 | false_reg->smax_value = min_t(s64, false_reg->smax_value, val - 1); |
3758 | true_reg->smin_value = max_t(s64, true_reg->smin_value, val); | |
48461135 | 3759 | break; |
b4e432f1 DB |
3760 | case BPF_JLE: |
3761 | false_reg->umin_value = max(false_reg->umin_value, val + 1); | |
3762 | true_reg->umax_value = min(true_reg->umax_value, val); | |
3763 | break; | |
3764 | case BPF_JSLE: | |
3765 | false_reg->smin_value = max_t(s64, false_reg->smin_value, val + 1); | |
3766 | true_reg->smax_value = min_t(s64, true_reg->smax_value, val); | |
3767 | break; | |
48461135 JB |
3768 | default: |
3769 | break; | |
3770 | } | |
3771 | ||
b03c9f9f EC |
3772 | __reg_deduce_bounds(false_reg); |
3773 | __reg_deduce_bounds(true_reg); | |
3774 | /* We might have learned some bits from the bounds. */ | |
3775 | __reg_bound_offset(false_reg); | |
3776 | __reg_bound_offset(true_reg); | |
3777 | /* Intersecting with the old var_off might have improved our bounds | |
3778 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), | |
3779 | * then new var_off is (0; 0x7f...fc) which improves our umax. | |
3780 | */ | |
3781 | __update_reg_bounds(false_reg); | |
3782 | __update_reg_bounds(true_reg); | |
48461135 JB |
3783 | } |
3784 | ||
f1174f77 EC |
3785 | /* Same as above, but for the case that dst_reg holds a constant and src_reg is |
3786 | * the variable reg. | |
48461135 JB |
3787 | */ |
3788 | static void reg_set_min_max_inv(struct bpf_reg_state *true_reg, | |
3789 | struct bpf_reg_state *false_reg, u64 val, | |
3790 | u8 opcode) | |
3791 | { | |
f1174f77 EC |
3792 | if (__is_pointer_value(false, false_reg)) |
3793 | return; | |
4cabc5b1 | 3794 | |
48461135 JB |
3795 | switch (opcode) { |
3796 | case BPF_JEQ: | |
3797 | /* If this is false then we know nothing Jon Snow, but if it is | |
3798 | * true then we know for sure. | |
3799 | */ | |
b03c9f9f | 3800 | __mark_reg_known(true_reg, val); |
48461135 JB |
3801 | break; |
3802 | case BPF_JNE: | |
3803 | /* If this is true we know nothing Jon Snow, but if it is false | |
3804 | * we know the value for sure; | |
3805 | */ | |
b03c9f9f | 3806 | __mark_reg_known(false_reg, val); |
48461135 JB |
3807 | break; |
3808 | case BPF_JGT: | |
b03c9f9f EC |
3809 | true_reg->umax_value = min(true_reg->umax_value, val - 1); |
3810 | false_reg->umin_value = max(false_reg->umin_value, val); | |
3811 | break; | |
48461135 | 3812 | case BPF_JSGT: |
b03c9f9f EC |
3813 | true_reg->smax_value = min_t(s64, true_reg->smax_value, val - 1); |
3814 | false_reg->smin_value = max_t(s64, false_reg->smin_value, val); | |
48461135 | 3815 | break; |
b4e432f1 DB |
3816 | case BPF_JLT: |
3817 | true_reg->umin_value = max(true_reg->umin_value, val + 1); | |
3818 | false_reg->umax_value = min(false_reg->umax_value, val); | |
3819 | break; | |
3820 | case BPF_JSLT: | |
3821 | true_reg->smin_value = max_t(s64, true_reg->smin_value, val + 1); | |
3822 | false_reg->smax_value = min_t(s64, false_reg->smax_value, val); | |
3823 | break; | |
48461135 | 3824 | case BPF_JGE: |
b03c9f9f EC |
3825 | true_reg->umax_value = min(true_reg->umax_value, val); |
3826 | false_reg->umin_value = max(false_reg->umin_value, val + 1); | |
3827 | break; | |
48461135 | 3828 | case BPF_JSGE: |
b03c9f9f EC |
3829 | true_reg->smax_value = min_t(s64, true_reg->smax_value, val); |
3830 | false_reg->smin_value = max_t(s64, false_reg->smin_value, val + 1); | |
48461135 | 3831 | break; |
b4e432f1 DB |
3832 | case BPF_JLE: |
3833 | true_reg->umin_value = max(true_reg->umin_value, val); | |
3834 | false_reg->umax_value = min(false_reg->umax_value, val - 1); | |
3835 | break; | |
3836 | case BPF_JSLE: | |
3837 | true_reg->smin_value = max_t(s64, true_reg->smin_value, val); | |
3838 | false_reg->smax_value = min_t(s64, false_reg->smax_value, val - 1); | |
3839 | break; | |
48461135 JB |
3840 | default: |
3841 | break; | |
3842 | } | |
3843 | ||
b03c9f9f EC |
3844 | __reg_deduce_bounds(false_reg); |
3845 | __reg_deduce_bounds(true_reg); | |
3846 | /* We might have learned some bits from the bounds. */ | |
3847 | __reg_bound_offset(false_reg); | |
3848 | __reg_bound_offset(true_reg); | |
3849 | /* Intersecting with the old var_off might have improved our bounds | |
3850 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), | |
3851 | * then new var_off is (0; 0x7f...fc) which improves our umax. | |
3852 | */ | |
3853 | __update_reg_bounds(false_reg); | |
3854 | __update_reg_bounds(true_reg); | |
f1174f77 EC |
3855 | } |
3856 | ||
3857 | /* Regs are known to be equal, so intersect their min/max/var_off */ | |
3858 | static void __reg_combine_min_max(struct bpf_reg_state *src_reg, | |
3859 | struct bpf_reg_state *dst_reg) | |
3860 | { | |
b03c9f9f EC |
3861 | src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value, |
3862 | dst_reg->umin_value); | |
3863 | src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value, | |
3864 | dst_reg->umax_value); | |
3865 | src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value, | |
3866 | dst_reg->smin_value); | |
3867 | src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value, | |
3868 | dst_reg->smax_value); | |
f1174f77 EC |
3869 | src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off, |
3870 | dst_reg->var_off); | |
b03c9f9f EC |
3871 | /* We might have learned new bounds from the var_off. */ |
3872 | __update_reg_bounds(src_reg); | |
3873 | __update_reg_bounds(dst_reg); | |
3874 | /* We might have learned something about the sign bit. */ | |
3875 | __reg_deduce_bounds(src_reg); | |
3876 | __reg_deduce_bounds(dst_reg); | |
3877 | /* We might have learned some bits from the bounds. */ | |
3878 | __reg_bound_offset(src_reg); | |
3879 | __reg_bound_offset(dst_reg); | |
3880 | /* Intersecting with the old var_off might have improved our bounds | |
3881 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), | |
3882 | * then new var_off is (0; 0x7f...fc) which improves our umax. | |
3883 | */ | |
3884 | __update_reg_bounds(src_reg); | |
3885 | __update_reg_bounds(dst_reg); | |
f1174f77 EC |
3886 | } |
3887 | ||
3888 | static void reg_combine_min_max(struct bpf_reg_state *true_src, | |
3889 | struct bpf_reg_state *true_dst, | |
3890 | struct bpf_reg_state *false_src, | |
3891 | struct bpf_reg_state *false_dst, | |
3892 | u8 opcode) | |
3893 | { | |
3894 | switch (opcode) { | |
3895 | case BPF_JEQ: | |
3896 | __reg_combine_min_max(true_src, true_dst); | |
3897 | break; | |
3898 | case BPF_JNE: | |
3899 | __reg_combine_min_max(false_src, false_dst); | |
b03c9f9f | 3900 | break; |
4cabc5b1 | 3901 | } |
48461135 JB |
3902 | } |
3903 | ||
fd978bf7 JS |
3904 | static void mark_ptr_or_null_reg(struct bpf_func_state *state, |
3905 | struct bpf_reg_state *reg, u32 id, | |
840b9615 | 3906 | bool is_null) |
57a09bf0 | 3907 | { |
840b9615 | 3908 | if (reg_type_may_be_null(reg->type) && reg->id == id) { |
f1174f77 EC |
3909 | /* Old offset (both fixed and variable parts) should |
3910 | * have been known-zero, because we don't allow pointer | |
3911 | * arithmetic on pointers that might be NULL. | |
3912 | */ | |
b03c9f9f EC |
3913 | if (WARN_ON_ONCE(reg->smin_value || reg->smax_value || |
3914 | !tnum_equals_const(reg->var_off, 0) || | |
f1174f77 | 3915 | reg->off)) { |
b03c9f9f EC |
3916 | __mark_reg_known_zero(reg); |
3917 | reg->off = 0; | |
f1174f77 EC |
3918 | } |
3919 | if (is_null) { | |
3920 | reg->type = SCALAR_VALUE; | |
840b9615 JS |
3921 | } else if (reg->type == PTR_TO_MAP_VALUE_OR_NULL) { |
3922 | if (reg->map_ptr->inner_map_meta) { | |
3923 | reg->type = CONST_PTR_TO_MAP; | |
3924 | reg->map_ptr = reg->map_ptr->inner_map_meta; | |
3925 | } else { | |
3926 | reg->type = PTR_TO_MAP_VALUE; | |
3927 | } | |
c64b7983 JS |
3928 | } else if (reg->type == PTR_TO_SOCKET_OR_NULL) { |
3929 | reg->type = PTR_TO_SOCKET; | |
56f668df | 3930 | } |
fd978bf7 JS |
3931 | if (is_null || !reg_is_refcounted(reg)) { |
3932 | /* We don't need id from this point onwards anymore, | |
3933 | * thus we should better reset it, so that state | |
3934 | * pruning has chances to take effect. | |
3935 | */ | |
3936 | reg->id = 0; | |
3937 | } | |
57a09bf0 TG |
3938 | } |
3939 | } | |
3940 | ||
3941 | /* The logic is similar to find_good_pkt_pointers(), both could eventually | |
3942 | * be folded together at some point. | |
3943 | */ | |
840b9615 JS |
3944 | static void mark_ptr_or_null_regs(struct bpf_verifier_state *vstate, u32 regno, |
3945 | bool is_null) | |
57a09bf0 | 3946 | { |
f4d7e40a | 3947 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
f3709f69 | 3948 | struct bpf_reg_state *reg, *regs = state->regs; |
a08dd0da | 3949 | u32 id = regs[regno].id; |
f4d7e40a | 3950 | int i, j; |
57a09bf0 | 3951 | |
fd978bf7 JS |
3952 | if (reg_is_refcounted_or_null(®s[regno]) && is_null) |
3953 | __release_reference_state(state, id); | |
3954 | ||
57a09bf0 | 3955 | for (i = 0; i < MAX_BPF_REG; i++) |
fd978bf7 | 3956 | mark_ptr_or_null_reg(state, ®s[i], id, is_null); |
57a09bf0 | 3957 | |
f4d7e40a AS |
3958 | for (j = 0; j <= vstate->curframe; j++) { |
3959 | state = vstate->frame[j]; | |
f3709f69 JS |
3960 | bpf_for_each_spilled_reg(i, state, reg) { |
3961 | if (!reg) | |
f4d7e40a | 3962 | continue; |
fd978bf7 | 3963 | mark_ptr_or_null_reg(state, reg, id, is_null); |
f4d7e40a | 3964 | } |
57a09bf0 TG |
3965 | } |
3966 | } | |
3967 | ||
5beca081 DB |
3968 | static bool try_match_pkt_pointers(const struct bpf_insn *insn, |
3969 | struct bpf_reg_state *dst_reg, | |
3970 | struct bpf_reg_state *src_reg, | |
3971 | struct bpf_verifier_state *this_branch, | |
3972 | struct bpf_verifier_state *other_branch) | |
3973 | { | |
3974 | if (BPF_SRC(insn->code) != BPF_X) | |
3975 | return false; | |
3976 | ||
3977 | switch (BPF_OP(insn->code)) { | |
3978 | case BPF_JGT: | |
3979 | if ((dst_reg->type == PTR_TO_PACKET && | |
3980 | src_reg->type == PTR_TO_PACKET_END) || | |
3981 | (dst_reg->type == PTR_TO_PACKET_META && | |
3982 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
3983 | /* pkt_data' > pkt_end, pkt_meta' > pkt_data */ | |
3984 | find_good_pkt_pointers(this_branch, dst_reg, | |
3985 | dst_reg->type, false); | |
3986 | } else if ((dst_reg->type == PTR_TO_PACKET_END && | |
3987 | src_reg->type == PTR_TO_PACKET) || | |
3988 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
3989 | src_reg->type == PTR_TO_PACKET_META)) { | |
3990 | /* pkt_end > pkt_data', pkt_data > pkt_meta' */ | |
3991 | find_good_pkt_pointers(other_branch, src_reg, | |
3992 | src_reg->type, true); | |
3993 | } else { | |
3994 | return false; | |
3995 | } | |
3996 | break; | |
3997 | case BPF_JLT: | |
3998 | if ((dst_reg->type == PTR_TO_PACKET && | |
3999 | src_reg->type == PTR_TO_PACKET_END) || | |
4000 | (dst_reg->type == PTR_TO_PACKET_META && | |
4001 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
4002 | /* pkt_data' < pkt_end, pkt_meta' < pkt_data */ | |
4003 | find_good_pkt_pointers(other_branch, dst_reg, | |
4004 | dst_reg->type, true); | |
4005 | } else if ((dst_reg->type == PTR_TO_PACKET_END && | |
4006 | src_reg->type == PTR_TO_PACKET) || | |
4007 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
4008 | src_reg->type == PTR_TO_PACKET_META)) { | |
4009 | /* pkt_end < pkt_data', pkt_data > pkt_meta' */ | |
4010 | find_good_pkt_pointers(this_branch, src_reg, | |
4011 | src_reg->type, false); | |
4012 | } else { | |
4013 | return false; | |
4014 | } | |
4015 | break; | |
4016 | case BPF_JGE: | |
4017 | if ((dst_reg->type == PTR_TO_PACKET && | |
4018 | src_reg->type == PTR_TO_PACKET_END) || | |
4019 | (dst_reg->type == PTR_TO_PACKET_META && | |
4020 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
4021 | /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */ | |
4022 | find_good_pkt_pointers(this_branch, dst_reg, | |
4023 | dst_reg->type, true); | |
4024 | } else if ((dst_reg->type == PTR_TO_PACKET_END && | |
4025 | src_reg->type == PTR_TO_PACKET) || | |
4026 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
4027 | src_reg->type == PTR_TO_PACKET_META)) { | |
4028 | /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */ | |
4029 | find_good_pkt_pointers(other_branch, src_reg, | |
4030 | src_reg->type, false); | |
4031 | } else { | |
4032 | return false; | |
4033 | } | |
4034 | break; | |
4035 | case BPF_JLE: | |
4036 | if ((dst_reg->type == PTR_TO_PACKET && | |
4037 | src_reg->type == PTR_TO_PACKET_END) || | |
4038 | (dst_reg->type == PTR_TO_PACKET_META && | |
4039 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
4040 | /* pkt_data' <= pkt_end, pkt_meta' <= pkt_data */ | |
4041 | find_good_pkt_pointers(other_branch, dst_reg, | |
4042 | dst_reg->type, false); | |
4043 | } else if ((dst_reg->type == PTR_TO_PACKET_END && | |
4044 | src_reg->type == PTR_TO_PACKET) || | |
4045 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
4046 | src_reg->type == PTR_TO_PACKET_META)) { | |
4047 | /* pkt_end <= pkt_data', pkt_data <= pkt_meta' */ | |
4048 | find_good_pkt_pointers(this_branch, src_reg, | |
4049 | src_reg->type, true); | |
4050 | } else { | |
4051 | return false; | |
4052 | } | |
4053 | break; | |
4054 | default: | |
4055 | return false; | |
4056 | } | |
4057 | ||
4058 | return true; | |
4059 | } | |
4060 | ||
58e2af8b | 4061 | static int check_cond_jmp_op(struct bpf_verifier_env *env, |
17a52670 AS |
4062 | struct bpf_insn *insn, int *insn_idx) |
4063 | { | |
f4d7e40a AS |
4064 | struct bpf_verifier_state *this_branch = env->cur_state; |
4065 | struct bpf_verifier_state *other_branch; | |
4066 | struct bpf_reg_state *regs = this_branch->frame[this_branch->curframe]->regs; | |
4067 | struct bpf_reg_state *dst_reg, *other_branch_regs; | |
17a52670 AS |
4068 | u8 opcode = BPF_OP(insn->code); |
4069 | int err; | |
4070 | ||
b4e432f1 | 4071 | if (opcode > BPF_JSLE) { |
61bd5218 | 4072 | verbose(env, "invalid BPF_JMP opcode %x\n", opcode); |
17a52670 AS |
4073 | return -EINVAL; |
4074 | } | |
4075 | ||
4076 | if (BPF_SRC(insn->code) == BPF_X) { | |
4077 | if (insn->imm != 0) { | |
61bd5218 | 4078 | verbose(env, "BPF_JMP uses reserved fields\n"); |
17a52670 AS |
4079 | return -EINVAL; |
4080 | } | |
4081 | ||
4082 | /* check src1 operand */ | |
dc503a8a | 4083 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
4084 | if (err) |
4085 | return err; | |
1be7f75d AS |
4086 | |
4087 | if (is_pointer_value(env, insn->src_reg)) { | |
61bd5218 | 4088 | verbose(env, "R%d pointer comparison prohibited\n", |
1be7f75d AS |
4089 | insn->src_reg); |
4090 | return -EACCES; | |
4091 | } | |
17a52670 AS |
4092 | } else { |
4093 | if (insn->src_reg != BPF_REG_0) { | |
61bd5218 | 4094 | verbose(env, "BPF_JMP uses reserved fields\n"); |
17a52670 AS |
4095 | return -EINVAL; |
4096 | } | |
4097 | } | |
4098 | ||
4099 | /* check src2 operand */ | |
dc503a8a | 4100 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
4101 | if (err) |
4102 | return err; | |
4103 | ||
1a0dc1ac AS |
4104 | dst_reg = ®s[insn->dst_reg]; |
4105 | ||
17a52670 AS |
4106 | /* detect if R == 0 where R was initialized to zero earlier */ |
4107 | if (BPF_SRC(insn->code) == BPF_K && | |
4108 | (opcode == BPF_JEQ || opcode == BPF_JNE) && | |
f1174f77 | 4109 | dst_reg->type == SCALAR_VALUE && |
3bf15921 AS |
4110 | tnum_is_const(dst_reg->var_off)) { |
4111 | if ((opcode == BPF_JEQ && dst_reg->var_off.value == insn->imm) || | |
4112 | (opcode == BPF_JNE && dst_reg->var_off.value != insn->imm)) { | |
17a52670 AS |
4113 | /* if (imm == imm) goto pc+off; |
4114 | * only follow the goto, ignore fall-through | |
4115 | */ | |
4116 | *insn_idx += insn->off; | |
4117 | return 0; | |
4118 | } else { | |
4119 | /* if (imm != imm) goto pc+off; | |
4120 | * only follow fall-through branch, since | |
4121 | * that's where the program will go | |
4122 | */ | |
4123 | return 0; | |
4124 | } | |
4125 | } | |
4126 | ||
4127 | other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx); | |
4128 | if (!other_branch) | |
4129 | return -EFAULT; | |
f4d7e40a | 4130 | other_branch_regs = other_branch->frame[other_branch->curframe]->regs; |
17a52670 | 4131 | |
48461135 JB |
4132 | /* detect if we are comparing against a constant value so we can adjust |
4133 | * our min/max values for our dst register. | |
f1174f77 EC |
4134 | * this is only legit if both are scalars (or pointers to the same |
4135 | * object, I suppose, but we don't support that right now), because | |
4136 | * otherwise the different base pointers mean the offsets aren't | |
4137 | * comparable. | |
48461135 JB |
4138 | */ |
4139 | if (BPF_SRC(insn->code) == BPF_X) { | |
f1174f77 EC |
4140 | if (dst_reg->type == SCALAR_VALUE && |
4141 | regs[insn->src_reg].type == SCALAR_VALUE) { | |
4142 | if (tnum_is_const(regs[insn->src_reg].var_off)) | |
f4d7e40a | 4143 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
f1174f77 EC |
4144 | dst_reg, regs[insn->src_reg].var_off.value, |
4145 | opcode); | |
4146 | else if (tnum_is_const(dst_reg->var_off)) | |
f4d7e40a | 4147 | reg_set_min_max_inv(&other_branch_regs[insn->src_reg], |
f1174f77 EC |
4148 | ®s[insn->src_reg], |
4149 | dst_reg->var_off.value, opcode); | |
4150 | else if (opcode == BPF_JEQ || opcode == BPF_JNE) | |
4151 | /* Comparing for equality, we can combine knowledge */ | |
f4d7e40a AS |
4152 | reg_combine_min_max(&other_branch_regs[insn->src_reg], |
4153 | &other_branch_regs[insn->dst_reg], | |
f1174f77 EC |
4154 | ®s[insn->src_reg], |
4155 | ®s[insn->dst_reg], opcode); | |
4156 | } | |
4157 | } else if (dst_reg->type == SCALAR_VALUE) { | |
f4d7e40a | 4158 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
48461135 JB |
4159 | dst_reg, insn->imm, opcode); |
4160 | } | |
4161 | ||
58e2af8b | 4162 | /* detect if R == 0 where R is returned from bpf_map_lookup_elem() */ |
17a52670 | 4163 | if (BPF_SRC(insn->code) == BPF_K && |
1a0dc1ac | 4164 | insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) && |
840b9615 JS |
4165 | reg_type_may_be_null(dst_reg->type)) { |
4166 | /* Mark all identical registers in each branch as either | |
57a09bf0 TG |
4167 | * safe or unknown depending R == 0 or R != 0 conditional. |
4168 | */ | |
840b9615 JS |
4169 | mark_ptr_or_null_regs(this_branch, insn->dst_reg, |
4170 | opcode == BPF_JNE); | |
4171 | mark_ptr_or_null_regs(other_branch, insn->dst_reg, | |
4172 | opcode == BPF_JEQ); | |
5beca081 DB |
4173 | } else if (!try_match_pkt_pointers(insn, dst_reg, ®s[insn->src_reg], |
4174 | this_branch, other_branch) && | |
4175 | is_pointer_value(env, insn->dst_reg)) { | |
61bd5218 JK |
4176 | verbose(env, "R%d pointer comparison prohibited\n", |
4177 | insn->dst_reg); | |
1be7f75d | 4178 | return -EACCES; |
17a52670 | 4179 | } |
61bd5218 | 4180 | if (env->log.level) |
f4d7e40a | 4181 | print_verifier_state(env, this_branch->frame[this_branch->curframe]); |
17a52670 AS |
4182 | return 0; |
4183 | } | |
4184 | ||
0246e64d AS |
4185 | /* return the map pointer stored inside BPF_LD_IMM64 instruction */ |
4186 | static struct bpf_map *ld_imm64_to_map_ptr(struct bpf_insn *insn) | |
4187 | { | |
4188 | u64 imm64 = ((u64) (u32) insn[0].imm) | ((u64) (u32) insn[1].imm) << 32; | |
4189 | ||
4190 | return (struct bpf_map *) (unsigned long) imm64; | |
4191 | } | |
4192 | ||
17a52670 | 4193 | /* verify BPF_LD_IMM64 instruction */ |
58e2af8b | 4194 | static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn) |
17a52670 | 4195 | { |
638f5b90 | 4196 | struct bpf_reg_state *regs = cur_regs(env); |
17a52670 AS |
4197 | int err; |
4198 | ||
4199 | if (BPF_SIZE(insn->code) != BPF_DW) { | |
61bd5218 | 4200 | verbose(env, "invalid BPF_LD_IMM insn\n"); |
17a52670 AS |
4201 | return -EINVAL; |
4202 | } | |
4203 | if (insn->off != 0) { | |
61bd5218 | 4204 | verbose(env, "BPF_LD_IMM64 uses reserved fields\n"); |
17a52670 AS |
4205 | return -EINVAL; |
4206 | } | |
4207 | ||
dc503a8a | 4208 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
17a52670 AS |
4209 | if (err) |
4210 | return err; | |
4211 | ||
6b173873 | 4212 | if (insn->src_reg == 0) { |
6b173873 JK |
4213 | u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm; |
4214 | ||
f1174f77 | 4215 | regs[insn->dst_reg].type = SCALAR_VALUE; |
b03c9f9f | 4216 | __mark_reg_known(®s[insn->dst_reg], imm); |
17a52670 | 4217 | return 0; |
6b173873 | 4218 | } |
17a52670 AS |
4219 | |
4220 | /* replace_map_fd_with_map_ptr() should have caught bad ld_imm64 */ | |
4221 | BUG_ON(insn->src_reg != BPF_PSEUDO_MAP_FD); | |
4222 | ||
4223 | regs[insn->dst_reg].type = CONST_PTR_TO_MAP; | |
4224 | regs[insn->dst_reg].map_ptr = ld_imm64_to_map_ptr(insn); | |
4225 | return 0; | |
4226 | } | |
4227 | ||
96be4325 DB |
4228 | static bool may_access_skb(enum bpf_prog_type type) |
4229 | { | |
4230 | switch (type) { | |
4231 | case BPF_PROG_TYPE_SOCKET_FILTER: | |
4232 | case BPF_PROG_TYPE_SCHED_CLS: | |
94caee8c | 4233 | case BPF_PROG_TYPE_SCHED_ACT: |
96be4325 DB |
4234 | return true; |
4235 | default: | |
4236 | return false; | |
4237 | } | |
4238 | } | |
4239 | ||
ddd872bc AS |
4240 | /* verify safety of LD_ABS|LD_IND instructions: |
4241 | * - they can only appear in the programs where ctx == skb | |
4242 | * - since they are wrappers of function calls, they scratch R1-R5 registers, | |
4243 | * preserve R6-R9, and store return value into R0 | |
4244 | * | |
4245 | * Implicit input: | |
4246 | * ctx == skb == R6 == CTX | |
4247 | * | |
4248 | * Explicit input: | |
4249 | * SRC == any register | |
4250 | * IMM == 32-bit immediate | |
4251 | * | |
4252 | * Output: | |
4253 | * R0 - 8/16/32-bit skb data converted to cpu endianness | |
4254 | */ | |
58e2af8b | 4255 | static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn) |
ddd872bc | 4256 | { |
638f5b90 | 4257 | struct bpf_reg_state *regs = cur_regs(env); |
ddd872bc | 4258 | u8 mode = BPF_MODE(insn->code); |
ddd872bc AS |
4259 | int i, err; |
4260 | ||
24701ece | 4261 | if (!may_access_skb(env->prog->type)) { |
61bd5218 | 4262 | verbose(env, "BPF_LD_[ABS|IND] instructions not allowed for this program type\n"); |
ddd872bc AS |
4263 | return -EINVAL; |
4264 | } | |
4265 | ||
e0cea7ce DB |
4266 | if (!env->ops->gen_ld_abs) { |
4267 | verbose(env, "bpf verifier is misconfigured\n"); | |
4268 | return -EINVAL; | |
4269 | } | |
4270 | ||
f910cefa | 4271 | if (env->subprog_cnt > 1) { |
f4d7e40a AS |
4272 | /* when program has LD_ABS insn JITs and interpreter assume |
4273 | * that r1 == ctx == skb which is not the case for callees | |
4274 | * that can have arbitrary arguments. It's problematic | |
4275 | * for main prog as well since JITs would need to analyze | |
4276 | * all functions in order to make proper register save/restore | |
4277 | * decisions in the main prog. Hence disallow LD_ABS with calls | |
4278 | */ | |
4279 | verbose(env, "BPF_LD_[ABS|IND] instructions cannot be mixed with bpf-to-bpf calls\n"); | |
4280 | return -EINVAL; | |
4281 | } | |
4282 | ||
ddd872bc | 4283 | if (insn->dst_reg != BPF_REG_0 || insn->off != 0 || |
d82bccc6 | 4284 | BPF_SIZE(insn->code) == BPF_DW || |
ddd872bc | 4285 | (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) { |
61bd5218 | 4286 | verbose(env, "BPF_LD_[ABS|IND] uses reserved fields\n"); |
ddd872bc AS |
4287 | return -EINVAL; |
4288 | } | |
4289 | ||
4290 | /* check whether implicit source operand (register R6) is readable */ | |
dc503a8a | 4291 | err = check_reg_arg(env, BPF_REG_6, SRC_OP); |
ddd872bc AS |
4292 | if (err) |
4293 | return err; | |
4294 | ||
fd978bf7 JS |
4295 | /* Disallow usage of BPF_LD_[ABS|IND] with reference tracking, as |
4296 | * gen_ld_abs() may terminate the program at runtime, leading to | |
4297 | * reference leak. | |
4298 | */ | |
4299 | err = check_reference_leak(env); | |
4300 | if (err) { | |
4301 | verbose(env, "BPF_LD_[ABS|IND] cannot be mixed with socket references\n"); | |
4302 | return err; | |
4303 | } | |
4304 | ||
ddd872bc | 4305 | if (regs[BPF_REG_6].type != PTR_TO_CTX) { |
61bd5218 JK |
4306 | verbose(env, |
4307 | "at the time of BPF_LD_ABS|IND R6 != pointer to skb\n"); | |
ddd872bc AS |
4308 | return -EINVAL; |
4309 | } | |
4310 | ||
4311 | if (mode == BPF_IND) { | |
4312 | /* check explicit source operand */ | |
dc503a8a | 4313 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
ddd872bc AS |
4314 | if (err) |
4315 | return err; | |
4316 | } | |
4317 | ||
4318 | /* reset caller saved regs to unreadable */ | |
dc503a8a | 4319 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
61bd5218 | 4320 | mark_reg_not_init(env, regs, caller_saved[i]); |
dc503a8a EC |
4321 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
4322 | } | |
ddd872bc AS |
4323 | |
4324 | /* mark destination R0 register as readable, since it contains | |
dc503a8a EC |
4325 | * the value fetched from the packet. |
4326 | * Already marked as written above. | |
ddd872bc | 4327 | */ |
61bd5218 | 4328 | mark_reg_unknown(env, regs, BPF_REG_0); |
ddd872bc AS |
4329 | return 0; |
4330 | } | |
4331 | ||
390ee7e2 AS |
4332 | static int check_return_code(struct bpf_verifier_env *env) |
4333 | { | |
4334 | struct bpf_reg_state *reg; | |
4335 | struct tnum range = tnum_range(0, 1); | |
4336 | ||
4337 | switch (env->prog->type) { | |
4338 | case BPF_PROG_TYPE_CGROUP_SKB: | |
4339 | case BPF_PROG_TYPE_CGROUP_SOCK: | |
4fbac77d | 4340 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
390ee7e2 | 4341 | case BPF_PROG_TYPE_SOCK_OPS: |
ebc614f6 | 4342 | case BPF_PROG_TYPE_CGROUP_DEVICE: |
390ee7e2 AS |
4343 | break; |
4344 | default: | |
4345 | return 0; | |
4346 | } | |
4347 | ||
638f5b90 | 4348 | reg = cur_regs(env) + BPF_REG_0; |
390ee7e2 | 4349 | if (reg->type != SCALAR_VALUE) { |
61bd5218 | 4350 | verbose(env, "At program exit the register R0 is not a known value (%s)\n", |
390ee7e2 AS |
4351 | reg_type_str[reg->type]); |
4352 | return -EINVAL; | |
4353 | } | |
4354 | ||
4355 | if (!tnum_in(range, reg->var_off)) { | |
61bd5218 | 4356 | verbose(env, "At program exit the register R0 "); |
390ee7e2 AS |
4357 | if (!tnum_is_unknown(reg->var_off)) { |
4358 | char tn_buf[48]; | |
4359 | ||
4360 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
61bd5218 | 4361 | verbose(env, "has value %s", tn_buf); |
390ee7e2 | 4362 | } else { |
61bd5218 | 4363 | verbose(env, "has unknown scalar value"); |
390ee7e2 | 4364 | } |
61bd5218 | 4365 | verbose(env, " should have been 0 or 1\n"); |
390ee7e2 AS |
4366 | return -EINVAL; |
4367 | } | |
4368 | return 0; | |
4369 | } | |
4370 | ||
475fb78f AS |
4371 | /* non-recursive DFS pseudo code |
4372 | * 1 procedure DFS-iterative(G,v): | |
4373 | * 2 label v as discovered | |
4374 | * 3 let S be a stack | |
4375 | * 4 S.push(v) | |
4376 | * 5 while S is not empty | |
4377 | * 6 t <- S.pop() | |
4378 | * 7 if t is what we're looking for: | |
4379 | * 8 return t | |
4380 | * 9 for all edges e in G.adjacentEdges(t) do | |
4381 | * 10 if edge e is already labelled | |
4382 | * 11 continue with the next edge | |
4383 | * 12 w <- G.adjacentVertex(t,e) | |
4384 | * 13 if vertex w is not discovered and not explored | |
4385 | * 14 label e as tree-edge | |
4386 | * 15 label w as discovered | |
4387 | * 16 S.push(w) | |
4388 | * 17 continue at 5 | |
4389 | * 18 else if vertex w is discovered | |
4390 | * 19 label e as back-edge | |
4391 | * 20 else | |
4392 | * 21 // vertex w is explored | |
4393 | * 22 label e as forward- or cross-edge | |
4394 | * 23 label t as explored | |
4395 | * 24 S.pop() | |
4396 | * | |
4397 | * convention: | |
4398 | * 0x10 - discovered | |
4399 | * 0x11 - discovered and fall-through edge labelled | |
4400 | * 0x12 - discovered and fall-through and branch edges labelled | |
4401 | * 0x20 - explored | |
4402 | */ | |
4403 | ||
4404 | enum { | |
4405 | DISCOVERED = 0x10, | |
4406 | EXPLORED = 0x20, | |
4407 | FALLTHROUGH = 1, | |
4408 | BRANCH = 2, | |
4409 | }; | |
4410 | ||
58e2af8b | 4411 | #define STATE_LIST_MARK ((struct bpf_verifier_state_list *) -1L) |
f1bca824 | 4412 | |
475fb78f AS |
4413 | static int *insn_stack; /* stack of insns to process */ |
4414 | static int cur_stack; /* current stack index */ | |
4415 | static int *insn_state; | |
4416 | ||
4417 | /* t, w, e - match pseudo-code above: | |
4418 | * t - index of current instruction | |
4419 | * w - next instruction | |
4420 | * e - edge | |
4421 | */ | |
58e2af8b | 4422 | static int push_insn(int t, int w, int e, struct bpf_verifier_env *env) |
475fb78f AS |
4423 | { |
4424 | if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH)) | |
4425 | return 0; | |
4426 | ||
4427 | if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH)) | |
4428 | return 0; | |
4429 | ||
4430 | if (w < 0 || w >= env->prog->len) { | |
61bd5218 | 4431 | verbose(env, "jump out of range from insn %d to %d\n", t, w); |
475fb78f AS |
4432 | return -EINVAL; |
4433 | } | |
4434 | ||
f1bca824 AS |
4435 | if (e == BRANCH) |
4436 | /* mark branch target for state pruning */ | |
4437 | env->explored_states[w] = STATE_LIST_MARK; | |
4438 | ||
475fb78f AS |
4439 | if (insn_state[w] == 0) { |
4440 | /* tree-edge */ | |
4441 | insn_state[t] = DISCOVERED | e; | |
4442 | insn_state[w] = DISCOVERED; | |
4443 | if (cur_stack >= env->prog->len) | |
4444 | return -E2BIG; | |
4445 | insn_stack[cur_stack++] = w; | |
4446 | return 1; | |
4447 | } else if ((insn_state[w] & 0xF0) == DISCOVERED) { | |
61bd5218 | 4448 | verbose(env, "back-edge from insn %d to %d\n", t, w); |
475fb78f AS |
4449 | return -EINVAL; |
4450 | } else if (insn_state[w] == EXPLORED) { | |
4451 | /* forward- or cross-edge */ | |
4452 | insn_state[t] = DISCOVERED | e; | |
4453 | } else { | |
61bd5218 | 4454 | verbose(env, "insn state internal bug\n"); |
475fb78f AS |
4455 | return -EFAULT; |
4456 | } | |
4457 | return 0; | |
4458 | } | |
4459 | ||
4460 | /* non-recursive depth-first-search to detect loops in BPF program | |
4461 | * loop == back-edge in directed graph | |
4462 | */ | |
58e2af8b | 4463 | static int check_cfg(struct bpf_verifier_env *env) |
475fb78f AS |
4464 | { |
4465 | struct bpf_insn *insns = env->prog->insnsi; | |
4466 | int insn_cnt = env->prog->len; | |
4467 | int ret = 0; | |
4468 | int i, t; | |
4469 | ||
cc8b0b92 AS |
4470 | ret = check_subprogs(env); |
4471 | if (ret < 0) | |
4472 | return ret; | |
4473 | ||
475fb78f AS |
4474 | insn_state = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
4475 | if (!insn_state) | |
4476 | return -ENOMEM; | |
4477 | ||
4478 | insn_stack = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL); | |
4479 | if (!insn_stack) { | |
4480 | kfree(insn_state); | |
4481 | return -ENOMEM; | |
4482 | } | |
4483 | ||
4484 | insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */ | |
4485 | insn_stack[0] = 0; /* 0 is the first instruction */ | |
4486 | cur_stack = 1; | |
4487 | ||
4488 | peek_stack: | |
4489 | if (cur_stack == 0) | |
4490 | goto check_state; | |
4491 | t = insn_stack[cur_stack - 1]; | |
4492 | ||
4493 | if (BPF_CLASS(insns[t].code) == BPF_JMP) { | |
4494 | u8 opcode = BPF_OP(insns[t].code); | |
4495 | ||
4496 | if (opcode == BPF_EXIT) { | |
4497 | goto mark_explored; | |
4498 | } else if (opcode == BPF_CALL) { | |
4499 | ret = push_insn(t, t + 1, FALLTHROUGH, env); | |
4500 | if (ret == 1) | |
4501 | goto peek_stack; | |
4502 | else if (ret < 0) | |
4503 | goto err_free; | |
07016151 DB |
4504 | if (t + 1 < insn_cnt) |
4505 | env->explored_states[t + 1] = STATE_LIST_MARK; | |
cc8b0b92 AS |
4506 | if (insns[t].src_reg == BPF_PSEUDO_CALL) { |
4507 | env->explored_states[t] = STATE_LIST_MARK; | |
4508 | ret = push_insn(t, t + insns[t].imm + 1, BRANCH, env); | |
4509 | if (ret == 1) | |
4510 | goto peek_stack; | |
4511 | else if (ret < 0) | |
4512 | goto err_free; | |
4513 | } | |
475fb78f AS |
4514 | } else if (opcode == BPF_JA) { |
4515 | if (BPF_SRC(insns[t].code) != BPF_K) { | |
4516 | ret = -EINVAL; | |
4517 | goto err_free; | |
4518 | } | |
4519 | /* unconditional jump with single edge */ | |
4520 | ret = push_insn(t, t + insns[t].off + 1, | |
4521 | FALLTHROUGH, env); | |
4522 | if (ret == 1) | |
4523 | goto peek_stack; | |
4524 | else if (ret < 0) | |
4525 | goto err_free; | |
f1bca824 AS |
4526 | /* tell verifier to check for equivalent states |
4527 | * after every call and jump | |
4528 | */ | |
c3de6317 AS |
4529 | if (t + 1 < insn_cnt) |
4530 | env->explored_states[t + 1] = STATE_LIST_MARK; | |
475fb78f AS |
4531 | } else { |
4532 | /* conditional jump with two edges */ | |
3c2ce60b | 4533 | env->explored_states[t] = STATE_LIST_MARK; |
475fb78f AS |
4534 | ret = push_insn(t, t + 1, FALLTHROUGH, env); |
4535 | if (ret == 1) | |
4536 | goto peek_stack; | |
4537 | else if (ret < 0) | |
4538 | goto err_free; | |
4539 | ||
4540 | ret = push_insn(t, t + insns[t].off + 1, BRANCH, env); | |
4541 | if (ret == 1) | |
4542 | goto peek_stack; | |
4543 | else if (ret < 0) | |
4544 | goto err_free; | |
4545 | } | |
4546 | } else { | |
4547 | /* all other non-branch instructions with single | |
4548 | * fall-through edge | |
4549 | */ | |
4550 | ret = push_insn(t, t + 1, FALLTHROUGH, env); | |
4551 | if (ret == 1) | |
4552 | goto peek_stack; | |
4553 | else if (ret < 0) | |
4554 | goto err_free; | |
4555 | } | |
4556 | ||
4557 | mark_explored: | |
4558 | insn_state[t] = EXPLORED; | |
4559 | if (cur_stack-- <= 0) { | |
61bd5218 | 4560 | verbose(env, "pop stack internal bug\n"); |
475fb78f AS |
4561 | ret = -EFAULT; |
4562 | goto err_free; | |
4563 | } | |
4564 | goto peek_stack; | |
4565 | ||
4566 | check_state: | |
4567 | for (i = 0; i < insn_cnt; i++) { | |
4568 | if (insn_state[i] != EXPLORED) { | |
61bd5218 | 4569 | verbose(env, "unreachable insn %d\n", i); |
475fb78f AS |
4570 | ret = -EINVAL; |
4571 | goto err_free; | |
4572 | } | |
4573 | } | |
4574 | ret = 0; /* cfg looks good */ | |
4575 | ||
4576 | err_free: | |
4577 | kfree(insn_state); | |
4578 | kfree(insn_stack); | |
4579 | return ret; | |
4580 | } | |
4581 | ||
f1174f77 EC |
4582 | /* check %cur's range satisfies %old's */ |
4583 | static bool range_within(struct bpf_reg_state *old, | |
4584 | struct bpf_reg_state *cur) | |
4585 | { | |
b03c9f9f EC |
4586 | return old->umin_value <= cur->umin_value && |
4587 | old->umax_value >= cur->umax_value && | |
4588 | old->smin_value <= cur->smin_value && | |
4589 | old->smax_value >= cur->smax_value; | |
f1174f77 EC |
4590 | } |
4591 | ||
4592 | /* Maximum number of register states that can exist at once */ | |
4593 | #define ID_MAP_SIZE (MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE) | |
4594 | struct idpair { | |
4595 | u32 old; | |
4596 | u32 cur; | |
4597 | }; | |
4598 | ||
4599 | /* If in the old state two registers had the same id, then they need to have | |
4600 | * the same id in the new state as well. But that id could be different from | |
4601 | * the old state, so we need to track the mapping from old to new ids. | |
4602 | * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent | |
4603 | * regs with old id 5 must also have new id 9 for the new state to be safe. But | |
4604 | * regs with a different old id could still have new id 9, we don't care about | |
4605 | * that. | |
4606 | * So we look through our idmap to see if this old id has been seen before. If | |
4607 | * so, we require the new id to match; otherwise, we add the id pair to the map. | |
969bf05e | 4608 | */ |
f1174f77 | 4609 | static bool check_ids(u32 old_id, u32 cur_id, struct idpair *idmap) |
969bf05e | 4610 | { |
f1174f77 | 4611 | unsigned int i; |
969bf05e | 4612 | |
f1174f77 EC |
4613 | for (i = 0; i < ID_MAP_SIZE; i++) { |
4614 | if (!idmap[i].old) { | |
4615 | /* Reached an empty slot; haven't seen this id before */ | |
4616 | idmap[i].old = old_id; | |
4617 | idmap[i].cur = cur_id; | |
4618 | return true; | |
4619 | } | |
4620 | if (idmap[i].old == old_id) | |
4621 | return idmap[i].cur == cur_id; | |
4622 | } | |
4623 | /* We ran out of idmap slots, which should be impossible */ | |
4624 | WARN_ON_ONCE(1); | |
4625 | return false; | |
4626 | } | |
4627 | ||
4628 | /* Returns true if (rold safe implies rcur safe) */ | |
1b688a19 EC |
4629 | static bool regsafe(struct bpf_reg_state *rold, struct bpf_reg_state *rcur, |
4630 | struct idpair *idmap) | |
f1174f77 | 4631 | { |
f4d7e40a AS |
4632 | bool equal; |
4633 | ||
dc503a8a EC |
4634 | if (!(rold->live & REG_LIVE_READ)) |
4635 | /* explored state didn't use this */ | |
4636 | return true; | |
4637 | ||
679c782d | 4638 | equal = memcmp(rold, rcur, offsetof(struct bpf_reg_state, parent)) == 0; |
f4d7e40a AS |
4639 | |
4640 | if (rold->type == PTR_TO_STACK) | |
4641 | /* two stack pointers are equal only if they're pointing to | |
4642 | * the same stack frame, since fp-8 in foo != fp-8 in bar | |
4643 | */ | |
4644 | return equal && rold->frameno == rcur->frameno; | |
4645 | ||
4646 | if (equal) | |
969bf05e AS |
4647 | return true; |
4648 | ||
f1174f77 EC |
4649 | if (rold->type == NOT_INIT) |
4650 | /* explored state can't have used this */ | |
969bf05e | 4651 | return true; |
f1174f77 EC |
4652 | if (rcur->type == NOT_INIT) |
4653 | return false; | |
4654 | switch (rold->type) { | |
4655 | case SCALAR_VALUE: | |
4656 | if (rcur->type == SCALAR_VALUE) { | |
4657 | /* new val must satisfy old val knowledge */ | |
4658 | return range_within(rold, rcur) && | |
4659 | tnum_in(rold->var_off, rcur->var_off); | |
4660 | } else { | |
179d1c56 JH |
4661 | /* We're trying to use a pointer in place of a scalar. |
4662 | * Even if the scalar was unbounded, this could lead to | |
4663 | * pointer leaks because scalars are allowed to leak | |
4664 | * while pointers are not. We could make this safe in | |
4665 | * special cases if root is calling us, but it's | |
4666 | * probably not worth the hassle. | |
f1174f77 | 4667 | */ |
179d1c56 | 4668 | return false; |
f1174f77 EC |
4669 | } |
4670 | case PTR_TO_MAP_VALUE: | |
1b688a19 EC |
4671 | /* If the new min/max/var_off satisfy the old ones and |
4672 | * everything else matches, we are OK. | |
4673 | * We don't care about the 'id' value, because nothing | |
4674 | * uses it for PTR_TO_MAP_VALUE (only for ..._OR_NULL) | |
4675 | */ | |
4676 | return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 && | |
4677 | range_within(rold, rcur) && | |
4678 | tnum_in(rold->var_off, rcur->var_off); | |
f1174f77 EC |
4679 | case PTR_TO_MAP_VALUE_OR_NULL: |
4680 | /* a PTR_TO_MAP_VALUE could be safe to use as a | |
4681 | * PTR_TO_MAP_VALUE_OR_NULL into the same map. | |
4682 | * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL- | |
4683 | * checked, doing so could have affected others with the same | |
4684 | * id, and we can't check for that because we lost the id when | |
4685 | * we converted to a PTR_TO_MAP_VALUE. | |
4686 | */ | |
4687 | if (rcur->type != PTR_TO_MAP_VALUE_OR_NULL) | |
4688 | return false; | |
4689 | if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, id))) | |
4690 | return false; | |
4691 | /* Check our ids match any regs they're supposed to */ | |
4692 | return check_ids(rold->id, rcur->id, idmap); | |
de8f3a83 | 4693 | case PTR_TO_PACKET_META: |
f1174f77 | 4694 | case PTR_TO_PACKET: |
de8f3a83 | 4695 | if (rcur->type != rold->type) |
f1174f77 EC |
4696 | return false; |
4697 | /* We must have at least as much range as the old ptr | |
4698 | * did, so that any accesses which were safe before are | |
4699 | * still safe. This is true even if old range < old off, | |
4700 | * since someone could have accessed through (ptr - k), or | |
4701 | * even done ptr -= k in a register, to get a safe access. | |
4702 | */ | |
4703 | if (rold->range > rcur->range) | |
4704 | return false; | |
4705 | /* If the offsets don't match, we can't trust our alignment; | |
4706 | * nor can we be sure that we won't fall out of range. | |
4707 | */ | |
4708 | if (rold->off != rcur->off) | |
4709 | return false; | |
4710 | /* id relations must be preserved */ | |
4711 | if (rold->id && !check_ids(rold->id, rcur->id, idmap)) | |
4712 | return false; | |
4713 | /* new val must satisfy old val knowledge */ | |
4714 | return range_within(rold, rcur) && | |
4715 | tnum_in(rold->var_off, rcur->var_off); | |
4716 | case PTR_TO_CTX: | |
4717 | case CONST_PTR_TO_MAP: | |
f1174f77 | 4718 | case PTR_TO_PACKET_END: |
d58e468b | 4719 | case PTR_TO_FLOW_KEYS: |
c64b7983 JS |
4720 | case PTR_TO_SOCKET: |
4721 | case PTR_TO_SOCKET_OR_NULL: | |
f1174f77 EC |
4722 | /* Only valid matches are exact, which memcmp() above |
4723 | * would have accepted | |
4724 | */ | |
4725 | default: | |
4726 | /* Don't know what's going on, just say it's not safe */ | |
4727 | return false; | |
4728 | } | |
969bf05e | 4729 | |
f1174f77 EC |
4730 | /* Shouldn't get here; if we do, say it's not safe */ |
4731 | WARN_ON_ONCE(1); | |
969bf05e AS |
4732 | return false; |
4733 | } | |
4734 | ||
f4d7e40a AS |
4735 | static bool stacksafe(struct bpf_func_state *old, |
4736 | struct bpf_func_state *cur, | |
638f5b90 AS |
4737 | struct idpair *idmap) |
4738 | { | |
4739 | int i, spi; | |
4740 | ||
4741 | /* if explored stack has more populated slots than current stack | |
4742 | * such stacks are not equivalent | |
4743 | */ | |
4744 | if (old->allocated_stack > cur->allocated_stack) | |
4745 | return false; | |
4746 | ||
4747 | /* walk slots of the explored stack and ignore any additional | |
4748 | * slots in the current stack, since explored(safe) state | |
4749 | * didn't use them | |
4750 | */ | |
4751 | for (i = 0; i < old->allocated_stack; i++) { | |
4752 | spi = i / BPF_REG_SIZE; | |
4753 | ||
cc2b14d5 AS |
4754 | if (!(old->stack[spi].spilled_ptr.live & REG_LIVE_READ)) |
4755 | /* explored state didn't use this */ | |
fd05e57b | 4756 | continue; |
cc2b14d5 | 4757 | |
638f5b90 AS |
4758 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID) |
4759 | continue; | |
cc2b14d5 AS |
4760 | /* if old state was safe with misc data in the stack |
4761 | * it will be safe with zero-initialized stack. | |
4762 | * The opposite is not true | |
4763 | */ | |
4764 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC && | |
4765 | cur->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_ZERO) | |
4766 | continue; | |
638f5b90 AS |
4767 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] != |
4768 | cur->stack[spi].slot_type[i % BPF_REG_SIZE]) | |
4769 | /* Ex: old explored (safe) state has STACK_SPILL in | |
4770 | * this stack slot, but current has has STACK_MISC -> | |
4771 | * this verifier states are not equivalent, | |
4772 | * return false to continue verification of this path | |
4773 | */ | |
4774 | return false; | |
4775 | if (i % BPF_REG_SIZE) | |
4776 | continue; | |
4777 | if (old->stack[spi].slot_type[0] != STACK_SPILL) | |
4778 | continue; | |
4779 | if (!regsafe(&old->stack[spi].spilled_ptr, | |
4780 | &cur->stack[spi].spilled_ptr, | |
4781 | idmap)) | |
4782 | /* when explored and current stack slot are both storing | |
4783 | * spilled registers, check that stored pointers types | |
4784 | * are the same as well. | |
4785 | * Ex: explored safe path could have stored | |
4786 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8} | |
4787 | * but current path has stored: | |
4788 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16} | |
4789 | * such verifier states are not equivalent. | |
4790 | * return false to continue verification of this path | |
4791 | */ | |
4792 | return false; | |
4793 | } | |
4794 | return true; | |
4795 | } | |
4796 | ||
fd978bf7 JS |
4797 | static bool refsafe(struct bpf_func_state *old, struct bpf_func_state *cur) |
4798 | { | |
4799 | if (old->acquired_refs != cur->acquired_refs) | |
4800 | return false; | |
4801 | return !memcmp(old->refs, cur->refs, | |
4802 | sizeof(*old->refs) * old->acquired_refs); | |
4803 | } | |
4804 | ||
f1bca824 AS |
4805 | /* compare two verifier states |
4806 | * | |
4807 | * all states stored in state_list are known to be valid, since | |
4808 | * verifier reached 'bpf_exit' instruction through them | |
4809 | * | |
4810 | * this function is called when verifier exploring different branches of | |
4811 | * execution popped from the state stack. If it sees an old state that has | |
4812 | * more strict register state and more strict stack state then this execution | |
4813 | * branch doesn't need to be explored further, since verifier already | |
4814 | * concluded that more strict state leads to valid finish. | |
4815 | * | |
4816 | * Therefore two states are equivalent if register state is more conservative | |
4817 | * and explored stack state is more conservative than the current one. | |
4818 | * Example: | |
4819 | * explored current | |
4820 | * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC) | |
4821 | * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC) | |
4822 | * | |
4823 | * In other words if current stack state (one being explored) has more | |
4824 | * valid slots than old one that already passed validation, it means | |
4825 | * the verifier can stop exploring and conclude that current state is valid too | |
4826 | * | |
4827 | * Similarly with registers. If explored state has register type as invalid | |
4828 | * whereas register type in current state is meaningful, it means that | |
4829 | * the current state will reach 'bpf_exit' instruction safely | |
4830 | */ | |
f4d7e40a AS |
4831 | static bool func_states_equal(struct bpf_func_state *old, |
4832 | struct bpf_func_state *cur) | |
f1bca824 | 4833 | { |
f1174f77 EC |
4834 | struct idpair *idmap; |
4835 | bool ret = false; | |
f1bca824 AS |
4836 | int i; |
4837 | ||
f1174f77 EC |
4838 | idmap = kcalloc(ID_MAP_SIZE, sizeof(struct idpair), GFP_KERNEL); |
4839 | /* If we failed to allocate the idmap, just say it's not safe */ | |
4840 | if (!idmap) | |
1a0dc1ac | 4841 | return false; |
f1174f77 EC |
4842 | |
4843 | for (i = 0; i < MAX_BPF_REG; i++) { | |
1b688a19 | 4844 | if (!regsafe(&old->regs[i], &cur->regs[i], idmap)) |
f1174f77 | 4845 | goto out_free; |
f1bca824 AS |
4846 | } |
4847 | ||
638f5b90 AS |
4848 | if (!stacksafe(old, cur, idmap)) |
4849 | goto out_free; | |
fd978bf7 JS |
4850 | |
4851 | if (!refsafe(old, cur)) | |
4852 | goto out_free; | |
f1174f77 EC |
4853 | ret = true; |
4854 | out_free: | |
4855 | kfree(idmap); | |
4856 | return ret; | |
f1bca824 AS |
4857 | } |
4858 | ||
f4d7e40a AS |
4859 | static bool states_equal(struct bpf_verifier_env *env, |
4860 | struct bpf_verifier_state *old, | |
4861 | struct bpf_verifier_state *cur) | |
4862 | { | |
4863 | int i; | |
4864 | ||
4865 | if (old->curframe != cur->curframe) | |
4866 | return false; | |
4867 | ||
4868 | /* for states to be equal callsites have to be the same | |
4869 | * and all frame states need to be equivalent | |
4870 | */ | |
4871 | for (i = 0; i <= old->curframe; i++) { | |
4872 | if (old->frame[i]->callsite != cur->frame[i]->callsite) | |
4873 | return false; | |
4874 | if (!func_states_equal(old->frame[i], cur->frame[i])) | |
4875 | return false; | |
4876 | } | |
4877 | return true; | |
4878 | } | |
4879 | ||
8e9cd9ce | 4880 | /* A write screens off any subsequent reads; but write marks come from the |
f4d7e40a AS |
4881 | * straight-line code between a state and its parent. When we arrive at an |
4882 | * equivalent state (jump target or such) we didn't arrive by the straight-line | |
4883 | * code, so read marks in the state must propagate to the parent regardless | |
4884 | * of the state's write marks. That's what 'parent == state->parent' comparison | |
679c782d | 4885 | * in mark_reg_read() is for. |
8e9cd9ce | 4886 | */ |
f4d7e40a AS |
4887 | static int propagate_liveness(struct bpf_verifier_env *env, |
4888 | const struct bpf_verifier_state *vstate, | |
4889 | struct bpf_verifier_state *vparent) | |
dc503a8a | 4890 | { |
f4d7e40a AS |
4891 | int i, frame, err = 0; |
4892 | struct bpf_func_state *state, *parent; | |
dc503a8a | 4893 | |
f4d7e40a AS |
4894 | if (vparent->curframe != vstate->curframe) { |
4895 | WARN(1, "propagate_live: parent frame %d current frame %d\n", | |
4896 | vparent->curframe, vstate->curframe); | |
4897 | return -EFAULT; | |
4898 | } | |
dc503a8a EC |
4899 | /* Propagate read liveness of registers... */ |
4900 | BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG); | |
4901 | /* We don't need to worry about FP liveness because it's read-only */ | |
4902 | for (i = 0; i < BPF_REG_FP; i++) { | |
f4d7e40a | 4903 | if (vparent->frame[vparent->curframe]->regs[i].live & REG_LIVE_READ) |
63f45f84 | 4904 | continue; |
f4d7e40a | 4905 | if (vstate->frame[vstate->curframe]->regs[i].live & REG_LIVE_READ) { |
679c782d EC |
4906 | err = mark_reg_read(env, &vstate->frame[vstate->curframe]->regs[i], |
4907 | &vparent->frame[vstate->curframe]->regs[i]); | |
f4d7e40a AS |
4908 | if (err) |
4909 | return err; | |
dc503a8a EC |
4910 | } |
4911 | } | |
f4d7e40a | 4912 | |
dc503a8a | 4913 | /* ... and stack slots */ |
f4d7e40a AS |
4914 | for (frame = 0; frame <= vstate->curframe; frame++) { |
4915 | state = vstate->frame[frame]; | |
4916 | parent = vparent->frame[frame]; | |
4917 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE && | |
4918 | i < parent->allocated_stack / BPF_REG_SIZE; i++) { | |
f4d7e40a AS |
4919 | if (parent->stack[i].spilled_ptr.live & REG_LIVE_READ) |
4920 | continue; | |
4921 | if (state->stack[i].spilled_ptr.live & REG_LIVE_READ) | |
679c782d EC |
4922 | mark_reg_read(env, &state->stack[i].spilled_ptr, |
4923 | &parent->stack[i].spilled_ptr); | |
dc503a8a EC |
4924 | } |
4925 | } | |
f4d7e40a | 4926 | return err; |
dc503a8a EC |
4927 | } |
4928 | ||
58e2af8b | 4929 | static int is_state_visited(struct bpf_verifier_env *env, int insn_idx) |
f1bca824 | 4930 | { |
58e2af8b JK |
4931 | struct bpf_verifier_state_list *new_sl; |
4932 | struct bpf_verifier_state_list *sl; | |
679c782d | 4933 | struct bpf_verifier_state *cur = env->cur_state, *new; |
f4d7e40a | 4934 | int i, j, err; |
f1bca824 AS |
4935 | |
4936 | sl = env->explored_states[insn_idx]; | |
4937 | if (!sl) | |
4938 | /* this 'insn_idx' instruction wasn't marked, so we will not | |
4939 | * be doing state search here | |
4940 | */ | |
4941 | return 0; | |
4942 | ||
4943 | while (sl != STATE_LIST_MARK) { | |
638f5b90 | 4944 | if (states_equal(env, &sl->state, cur)) { |
f1bca824 | 4945 | /* reached equivalent register/stack state, |
dc503a8a EC |
4946 | * prune the search. |
4947 | * Registers read by the continuation are read by us. | |
8e9cd9ce EC |
4948 | * If we have any write marks in env->cur_state, they |
4949 | * will prevent corresponding reads in the continuation | |
4950 | * from reaching our parent (an explored_state). Our | |
4951 | * own state will get the read marks recorded, but | |
4952 | * they'll be immediately forgotten as we're pruning | |
4953 | * this state and will pop a new one. | |
f1bca824 | 4954 | */ |
f4d7e40a AS |
4955 | err = propagate_liveness(env, &sl->state, cur); |
4956 | if (err) | |
4957 | return err; | |
f1bca824 | 4958 | return 1; |
dc503a8a | 4959 | } |
f1bca824 AS |
4960 | sl = sl->next; |
4961 | } | |
4962 | ||
4963 | /* there were no equivalent states, remember current one. | |
4964 | * technically the current state is not proven to be safe yet, | |
f4d7e40a AS |
4965 | * but it will either reach outer most bpf_exit (which means it's safe) |
4966 | * or it will be rejected. Since there are no loops, we won't be | |
4967 | * seeing this tuple (frame[0].callsite, frame[1].callsite, .. insn_idx) | |
4968 | * again on the way to bpf_exit | |
f1bca824 | 4969 | */ |
638f5b90 | 4970 | new_sl = kzalloc(sizeof(struct bpf_verifier_state_list), GFP_KERNEL); |
f1bca824 AS |
4971 | if (!new_sl) |
4972 | return -ENOMEM; | |
4973 | ||
4974 | /* add new state to the head of linked list */ | |
679c782d EC |
4975 | new = &new_sl->state; |
4976 | err = copy_verifier_state(new, cur); | |
1969db47 | 4977 | if (err) { |
679c782d | 4978 | free_verifier_state(new, false); |
1969db47 AS |
4979 | kfree(new_sl); |
4980 | return err; | |
4981 | } | |
f1bca824 AS |
4982 | new_sl->next = env->explored_states[insn_idx]; |
4983 | env->explored_states[insn_idx] = new_sl; | |
dc503a8a | 4984 | /* connect new state to parentage chain */ |
679c782d EC |
4985 | for (i = 0; i < BPF_REG_FP; i++) |
4986 | cur_regs(env)[i].parent = &new->frame[new->curframe]->regs[i]; | |
8e9cd9ce EC |
4987 | /* clear write marks in current state: the writes we did are not writes |
4988 | * our child did, so they don't screen off its reads from us. | |
4989 | * (There are no read marks in current state, because reads always mark | |
4990 | * their parent and current state never has children yet. Only | |
4991 | * explored_states can get read marks.) | |
4992 | */ | |
dc503a8a | 4993 | for (i = 0; i < BPF_REG_FP; i++) |
f4d7e40a AS |
4994 | cur->frame[cur->curframe]->regs[i].live = REG_LIVE_NONE; |
4995 | ||
4996 | /* all stack frames are accessible from callee, clear them all */ | |
4997 | for (j = 0; j <= cur->curframe; j++) { | |
4998 | struct bpf_func_state *frame = cur->frame[j]; | |
679c782d | 4999 | struct bpf_func_state *newframe = new->frame[j]; |
f4d7e40a | 5000 | |
679c782d | 5001 | for (i = 0; i < frame->allocated_stack / BPF_REG_SIZE; i++) { |
cc2b14d5 | 5002 | frame->stack[i].spilled_ptr.live = REG_LIVE_NONE; |
679c782d EC |
5003 | frame->stack[i].spilled_ptr.parent = |
5004 | &newframe->stack[i].spilled_ptr; | |
5005 | } | |
f4d7e40a | 5006 | } |
f1bca824 AS |
5007 | return 0; |
5008 | } | |
5009 | ||
c64b7983 JS |
5010 | /* Return true if it's OK to have the same insn return a different type. */ |
5011 | static bool reg_type_mismatch_ok(enum bpf_reg_type type) | |
5012 | { | |
5013 | switch (type) { | |
5014 | case PTR_TO_CTX: | |
5015 | case PTR_TO_SOCKET: | |
5016 | case PTR_TO_SOCKET_OR_NULL: | |
5017 | return false; | |
5018 | default: | |
5019 | return true; | |
5020 | } | |
5021 | } | |
5022 | ||
5023 | /* If an instruction was previously used with particular pointer types, then we | |
5024 | * need to be careful to avoid cases such as the below, where it may be ok | |
5025 | * for one branch accessing the pointer, but not ok for the other branch: | |
5026 | * | |
5027 | * R1 = sock_ptr | |
5028 | * goto X; | |
5029 | * ... | |
5030 | * R1 = some_other_valid_ptr; | |
5031 | * goto X; | |
5032 | * ... | |
5033 | * R2 = *(u32 *)(R1 + 0); | |
5034 | */ | |
5035 | static bool reg_type_mismatch(enum bpf_reg_type src, enum bpf_reg_type prev) | |
5036 | { | |
5037 | return src != prev && (!reg_type_mismatch_ok(src) || | |
5038 | !reg_type_mismatch_ok(prev)); | |
5039 | } | |
5040 | ||
58e2af8b | 5041 | static int do_check(struct bpf_verifier_env *env) |
17a52670 | 5042 | { |
638f5b90 | 5043 | struct bpf_verifier_state *state; |
17a52670 | 5044 | struct bpf_insn *insns = env->prog->insnsi; |
638f5b90 | 5045 | struct bpf_reg_state *regs; |
f4d7e40a | 5046 | int insn_cnt = env->prog->len, i; |
17a52670 AS |
5047 | int insn_idx, prev_insn_idx = 0; |
5048 | int insn_processed = 0; | |
5049 | bool do_print_state = false; | |
5050 | ||
638f5b90 AS |
5051 | state = kzalloc(sizeof(struct bpf_verifier_state), GFP_KERNEL); |
5052 | if (!state) | |
5053 | return -ENOMEM; | |
f4d7e40a | 5054 | state->curframe = 0; |
f4d7e40a AS |
5055 | state->frame[0] = kzalloc(sizeof(struct bpf_func_state), GFP_KERNEL); |
5056 | if (!state->frame[0]) { | |
5057 | kfree(state); | |
5058 | return -ENOMEM; | |
5059 | } | |
5060 | env->cur_state = state; | |
5061 | init_func_state(env, state->frame[0], | |
5062 | BPF_MAIN_FUNC /* callsite */, | |
5063 | 0 /* frameno */, | |
5064 | 0 /* subprogno, zero == main subprog */); | |
17a52670 AS |
5065 | insn_idx = 0; |
5066 | for (;;) { | |
5067 | struct bpf_insn *insn; | |
5068 | u8 class; | |
5069 | int err; | |
5070 | ||
5071 | if (insn_idx >= insn_cnt) { | |
61bd5218 | 5072 | verbose(env, "invalid insn idx %d insn_cnt %d\n", |
17a52670 AS |
5073 | insn_idx, insn_cnt); |
5074 | return -EFAULT; | |
5075 | } | |
5076 | ||
5077 | insn = &insns[insn_idx]; | |
5078 | class = BPF_CLASS(insn->code); | |
5079 | ||
07016151 | 5080 | if (++insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) { |
61bd5218 JK |
5081 | verbose(env, |
5082 | "BPF program is too large. Processed %d insn\n", | |
17a52670 AS |
5083 | insn_processed); |
5084 | return -E2BIG; | |
5085 | } | |
5086 | ||
f1bca824 AS |
5087 | err = is_state_visited(env, insn_idx); |
5088 | if (err < 0) | |
5089 | return err; | |
5090 | if (err == 1) { | |
5091 | /* found equivalent state, can prune the search */ | |
61bd5218 | 5092 | if (env->log.level) { |
f1bca824 | 5093 | if (do_print_state) |
61bd5218 | 5094 | verbose(env, "\nfrom %d to %d: safe\n", |
f1bca824 AS |
5095 | prev_insn_idx, insn_idx); |
5096 | else | |
61bd5218 | 5097 | verbose(env, "%d: safe\n", insn_idx); |
f1bca824 AS |
5098 | } |
5099 | goto process_bpf_exit; | |
5100 | } | |
5101 | ||
3c2ce60b DB |
5102 | if (need_resched()) |
5103 | cond_resched(); | |
5104 | ||
61bd5218 JK |
5105 | if (env->log.level > 1 || (env->log.level && do_print_state)) { |
5106 | if (env->log.level > 1) | |
5107 | verbose(env, "%d:", insn_idx); | |
c5fc9692 | 5108 | else |
61bd5218 | 5109 | verbose(env, "\nfrom %d to %d:", |
c5fc9692 | 5110 | prev_insn_idx, insn_idx); |
f4d7e40a | 5111 | print_verifier_state(env, state->frame[state->curframe]); |
17a52670 AS |
5112 | do_print_state = false; |
5113 | } | |
5114 | ||
61bd5218 | 5115 | if (env->log.level) { |
7105e828 DB |
5116 | const struct bpf_insn_cbs cbs = { |
5117 | .cb_print = verbose, | |
abe08840 | 5118 | .private_data = env, |
7105e828 DB |
5119 | }; |
5120 | ||
61bd5218 | 5121 | verbose(env, "%d: ", insn_idx); |
abe08840 | 5122 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); |
17a52670 AS |
5123 | } |
5124 | ||
cae1927c JK |
5125 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
5126 | err = bpf_prog_offload_verify_insn(env, insn_idx, | |
5127 | prev_insn_idx); | |
5128 | if (err) | |
5129 | return err; | |
5130 | } | |
13a27dfc | 5131 | |
638f5b90 | 5132 | regs = cur_regs(env); |
c131187d | 5133 | env->insn_aux_data[insn_idx].seen = true; |
fd978bf7 | 5134 | |
17a52670 | 5135 | if (class == BPF_ALU || class == BPF_ALU64) { |
1be7f75d | 5136 | err = check_alu_op(env, insn); |
17a52670 AS |
5137 | if (err) |
5138 | return err; | |
5139 | ||
5140 | } else if (class == BPF_LDX) { | |
3df126f3 | 5141 | enum bpf_reg_type *prev_src_type, src_reg_type; |
9bac3d6d AS |
5142 | |
5143 | /* check for reserved fields is already done */ | |
5144 | ||
17a52670 | 5145 | /* check src operand */ |
dc503a8a | 5146 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
5147 | if (err) |
5148 | return err; | |
5149 | ||
dc503a8a | 5150 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
17a52670 AS |
5151 | if (err) |
5152 | return err; | |
5153 | ||
725f9dcd AS |
5154 | src_reg_type = regs[insn->src_reg].type; |
5155 | ||
17a52670 AS |
5156 | /* check that memory (src_reg + off) is readable, |
5157 | * the state of dst_reg will be updated by this func | |
5158 | */ | |
31fd8581 | 5159 | err = check_mem_access(env, insn_idx, insn->src_reg, insn->off, |
17a52670 | 5160 | BPF_SIZE(insn->code), BPF_READ, |
ca369602 | 5161 | insn->dst_reg, false); |
17a52670 AS |
5162 | if (err) |
5163 | return err; | |
5164 | ||
3df126f3 JK |
5165 | prev_src_type = &env->insn_aux_data[insn_idx].ptr_type; |
5166 | ||
5167 | if (*prev_src_type == NOT_INIT) { | |
9bac3d6d AS |
5168 | /* saw a valid insn |
5169 | * dst_reg = *(u32 *)(src_reg + off) | |
3df126f3 | 5170 | * save type to validate intersecting paths |
9bac3d6d | 5171 | */ |
3df126f3 | 5172 | *prev_src_type = src_reg_type; |
9bac3d6d | 5173 | |
c64b7983 | 5174 | } else if (reg_type_mismatch(src_reg_type, *prev_src_type)) { |
9bac3d6d AS |
5175 | /* ABuser program is trying to use the same insn |
5176 | * dst_reg = *(u32*) (src_reg + off) | |
5177 | * with different pointer types: | |
5178 | * src_reg == ctx in one branch and | |
5179 | * src_reg == stack|map in some other branch. | |
5180 | * Reject it. | |
5181 | */ | |
61bd5218 | 5182 | verbose(env, "same insn cannot be used with different pointers\n"); |
9bac3d6d AS |
5183 | return -EINVAL; |
5184 | } | |
5185 | ||
17a52670 | 5186 | } else if (class == BPF_STX) { |
3df126f3 | 5187 | enum bpf_reg_type *prev_dst_type, dst_reg_type; |
d691f9e8 | 5188 | |
17a52670 | 5189 | if (BPF_MODE(insn->code) == BPF_XADD) { |
31fd8581 | 5190 | err = check_xadd(env, insn_idx, insn); |
17a52670 AS |
5191 | if (err) |
5192 | return err; | |
5193 | insn_idx++; | |
5194 | continue; | |
5195 | } | |
5196 | ||
17a52670 | 5197 | /* check src1 operand */ |
dc503a8a | 5198 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
5199 | if (err) |
5200 | return err; | |
5201 | /* check src2 operand */ | |
dc503a8a | 5202 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
5203 | if (err) |
5204 | return err; | |
5205 | ||
d691f9e8 AS |
5206 | dst_reg_type = regs[insn->dst_reg].type; |
5207 | ||
17a52670 | 5208 | /* check that memory (dst_reg + off) is writeable */ |
31fd8581 | 5209 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
17a52670 | 5210 | BPF_SIZE(insn->code), BPF_WRITE, |
ca369602 | 5211 | insn->src_reg, false); |
17a52670 AS |
5212 | if (err) |
5213 | return err; | |
5214 | ||
3df126f3 JK |
5215 | prev_dst_type = &env->insn_aux_data[insn_idx].ptr_type; |
5216 | ||
5217 | if (*prev_dst_type == NOT_INIT) { | |
5218 | *prev_dst_type = dst_reg_type; | |
c64b7983 | 5219 | } else if (reg_type_mismatch(dst_reg_type, *prev_dst_type)) { |
61bd5218 | 5220 | verbose(env, "same insn cannot be used with different pointers\n"); |
d691f9e8 AS |
5221 | return -EINVAL; |
5222 | } | |
5223 | ||
17a52670 AS |
5224 | } else if (class == BPF_ST) { |
5225 | if (BPF_MODE(insn->code) != BPF_MEM || | |
5226 | insn->src_reg != BPF_REG_0) { | |
61bd5218 | 5227 | verbose(env, "BPF_ST uses reserved fields\n"); |
17a52670 AS |
5228 | return -EINVAL; |
5229 | } | |
5230 | /* check src operand */ | |
dc503a8a | 5231 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
5232 | if (err) |
5233 | return err; | |
5234 | ||
f37a8cb8 | 5235 | if (is_ctx_reg(env, insn->dst_reg)) { |
9d2be44a JS |
5236 | verbose(env, "BPF_ST stores into R%d %s is not allowed\n", |
5237 | insn->dst_reg, reg_type_str[insn->dst_reg]); | |
f37a8cb8 DB |
5238 | return -EACCES; |
5239 | } | |
5240 | ||
17a52670 | 5241 | /* check that memory (dst_reg + off) is writeable */ |
31fd8581 | 5242 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
17a52670 | 5243 | BPF_SIZE(insn->code), BPF_WRITE, |
ca369602 | 5244 | -1, false); |
17a52670 AS |
5245 | if (err) |
5246 | return err; | |
5247 | ||
5248 | } else if (class == BPF_JMP) { | |
5249 | u8 opcode = BPF_OP(insn->code); | |
5250 | ||
5251 | if (opcode == BPF_CALL) { | |
5252 | if (BPF_SRC(insn->code) != BPF_K || | |
5253 | insn->off != 0 || | |
f4d7e40a AS |
5254 | (insn->src_reg != BPF_REG_0 && |
5255 | insn->src_reg != BPF_PSEUDO_CALL) || | |
17a52670 | 5256 | insn->dst_reg != BPF_REG_0) { |
61bd5218 | 5257 | verbose(env, "BPF_CALL uses reserved fields\n"); |
17a52670 AS |
5258 | return -EINVAL; |
5259 | } | |
5260 | ||
f4d7e40a AS |
5261 | if (insn->src_reg == BPF_PSEUDO_CALL) |
5262 | err = check_func_call(env, insn, &insn_idx); | |
5263 | else | |
5264 | err = check_helper_call(env, insn->imm, insn_idx); | |
17a52670 AS |
5265 | if (err) |
5266 | return err; | |
5267 | ||
5268 | } else if (opcode == BPF_JA) { | |
5269 | if (BPF_SRC(insn->code) != BPF_K || | |
5270 | insn->imm != 0 || | |
5271 | insn->src_reg != BPF_REG_0 || | |
5272 | insn->dst_reg != BPF_REG_0) { | |
61bd5218 | 5273 | verbose(env, "BPF_JA uses reserved fields\n"); |
17a52670 AS |
5274 | return -EINVAL; |
5275 | } | |
5276 | ||
5277 | insn_idx += insn->off + 1; | |
5278 | continue; | |
5279 | ||
5280 | } else if (opcode == BPF_EXIT) { | |
5281 | if (BPF_SRC(insn->code) != BPF_K || | |
5282 | insn->imm != 0 || | |
5283 | insn->src_reg != BPF_REG_0 || | |
5284 | insn->dst_reg != BPF_REG_0) { | |
61bd5218 | 5285 | verbose(env, "BPF_EXIT uses reserved fields\n"); |
17a52670 AS |
5286 | return -EINVAL; |
5287 | } | |
5288 | ||
f4d7e40a AS |
5289 | if (state->curframe) { |
5290 | /* exit from nested function */ | |
5291 | prev_insn_idx = insn_idx; | |
5292 | err = prepare_func_exit(env, &insn_idx); | |
5293 | if (err) | |
5294 | return err; | |
5295 | do_print_state = true; | |
5296 | continue; | |
5297 | } | |
5298 | ||
fd978bf7 JS |
5299 | err = check_reference_leak(env); |
5300 | if (err) | |
5301 | return err; | |
5302 | ||
17a52670 AS |
5303 | /* eBPF calling convetion is such that R0 is used |
5304 | * to return the value from eBPF program. | |
5305 | * Make sure that it's readable at this time | |
5306 | * of bpf_exit, which means that program wrote | |
5307 | * something into it earlier | |
5308 | */ | |
dc503a8a | 5309 | err = check_reg_arg(env, BPF_REG_0, SRC_OP); |
17a52670 AS |
5310 | if (err) |
5311 | return err; | |
5312 | ||
1be7f75d | 5313 | if (is_pointer_value(env, BPF_REG_0)) { |
61bd5218 | 5314 | verbose(env, "R0 leaks addr as return value\n"); |
1be7f75d AS |
5315 | return -EACCES; |
5316 | } | |
5317 | ||
390ee7e2 AS |
5318 | err = check_return_code(env); |
5319 | if (err) | |
5320 | return err; | |
f1bca824 | 5321 | process_bpf_exit: |
638f5b90 AS |
5322 | err = pop_stack(env, &prev_insn_idx, &insn_idx); |
5323 | if (err < 0) { | |
5324 | if (err != -ENOENT) | |
5325 | return err; | |
17a52670 AS |
5326 | break; |
5327 | } else { | |
5328 | do_print_state = true; | |
5329 | continue; | |
5330 | } | |
5331 | } else { | |
5332 | err = check_cond_jmp_op(env, insn, &insn_idx); | |
5333 | if (err) | |
5334 | return err; | |
5335 | } | |
5336 | } else if (class == BPF_LD) { | |
5337 | u8 mode = BPF_MODE(insn->code); | |
5338 | ||
5339 | if (mode == BPF_ABS || mode == BPF_IND) { | |
ddd872bc AS |
5340 | err = check_ld_abs(env, insn); |
5341 | if (err) | |
5342 | return err; | |
5343 | ||
17a52670 AS |
5344 | } else if (mode == BPF_IMM) { |
5345 | err = check_ld_imm(env, insn); | |
5346 | if (err) | |
5347 | return err; | |
5348 | ||
5349 | insn_idx++; | |
c131187d | 5350 | env->insn_aux_data[insn_idx].seen = true; |
17a52670 | 5351 | } else { |
61bd5218 | 5352 | verbose(env, "invalid BPF_LD mode\n"); |
17a52670 AS |
5353 | return -EINVAL; |
5354 | } | |
5355 | } else { | |
61bd5218 | 5356 | verbose(env, "unknown insn class %d\n", class); |
17a52670 AS |
5357 | return -EINVAL; |
5358 | } | |
5359 | ||
5360 | insn_idx++; | |
5361 | } | |
5362 | ||
4bd95f4b DB |
5363 | verbose(env, "processed %d insns (limit %d), stack depth ", |
5364 | insn_processed, BPF_COMPLEXITY_LIMIT_INSNS); | |
f910cefa | 5365 | for (i = 0; i < env->subprog_cnt; i++) { |
9c8105bd | 5366 | u32 depth = env->subprog_info[i].stack_depth; |
f4d7e40a AS |
5367 | |
5368 | verbose(env, "%d", depth); | |
f910cefa | 5369 | if (i + 1 < env->subprog_cnt) |
f4d7e40a AS |
5370 | verbose(env, "+"); |
5371 | } | |
5372 | verbose(env, "\n"); | |
9c8105bd | 5373 | env->prog->aux->stack_depth = env->subprog_info[0].stack_depth; |
17a52670 AS |
5374 | return 0; |
5375 | } | |
5376 | ||
56f668df MKL |
5377 | static int check_map_prealloc(struct bpf_map *map) |
5378 | { | |
5379 | return (map->map_type != BPF_MAP_TYPE_HASH && | |
bcc6b1b7 MKL |
5380 | map->map_type != BPF_MAP_TYPE_PERCPU_HASH && |
5381 | map->map_type != BPF_MAP_TYPE_HASH_OF_MAPS) || | |
56f668df MKL |
5382 | !(map->map_flags & BPF_F_NO_PREALLOC); |
5383 | } | |
5384 | ||
61bd5218 JK |
5385 | static int check_map_prog_compatibility(struct bpf_verifier_env *env, |
5386 | struct bpf_map *map, | |
fdc15d38 AS |
5387 | struct bpf_prog *prog) |
5388 | ||
5389 | { | |
56f668df MKL |
5390 | /* Make sure that BPF_PROG_TYPE_PERF_EVENT programs only use |
5391 | * preallocated hash maps, since doing memory allocation | |
5392 | * in overflow_handler can crash depending on where nmi got | |
5393 | * triggered. | |
5394 | */ | |
5395 | if (prog->type == BPF_PROG_TYPE_PERF_EVENT) { | |
5396 | if (!check_map_prealloc(map)) { | |
61bd5218 | 5397 | verbose(env, "perf_event programs can only use preallocated hash map\n"); |
56f668df MKL |
5398 | return -EINVAL; |
5399 | } | |
5400 | if (map->inner_map_meta && | |
5401 | !check_map_prealloc(map->inner_map_meta)) { | |
61bd5218 | 5402 | verbose(env, "perf_event programs can only use preallocated inner hash map\n"); |
56f668df MKL |
5403 | return -EINVAL; |
5404 | } | |
fdc15d38 | 5405 | } |
a3884572 JK |
5406 | |
5407 | if ((bpf_prog_is_dev_bound(prog->aux) || bpf_map_is_dev_bound(map)) && | |
09728266 | 5408 | !bpf_offload_prog_map_match(prog, map)) { |
a3884572 JK |
5409 | verbose(env, "offload device mismatch between prog and map\n"); |
5410 | return -EINVAL; | |
5411 | } | |
5412 | ||
fdc15d38 AS |
5413 | return 0; |
5414 | } | |
5415 | ||
b741f163 RG |
5416 | static bool bpf_map_is_cgroup_storage(struct bpf_map *map) |
5417 | { | |
5418 | return (map->map_type == BPF_MAP_TYPE_CGROUP_STORAGE || | |
5419 | map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE); | |
5420 | } | |
5421 | ||
0246e64d AS |
5422 | /* look for pseudo eBPF instructions that access map FDs and |
5423 | * replace them with actual map pointers | |
5424 | */ | |
58e2af8b | 5425 | static int replace_map_fd_with_map_ptr(struct bpf_verifier_env *env) |
0246e64d AS |
5426 | { |
5427 | struct bpf_insn *insn = env->prog->insnsi; | |
5428 | int insn_cnt = env->prog->len; | |
fdc15d38 | 5429 | int i, j, err; |
0246e64d | 5430 | |
f1f7714e | 5431 | err = bpf_prog_calc_tag(env->prog); |
aafe6ae9 DB |
5432 | if (err) |
5433 | return err; | |
5434 | ||
0246e64d | 5435 | for (i = 0; i < insn_cnt; i++, insn++) { |
9bac3d6d | 5436 | if (BPF_CLASS(insn->code) == BPF_LDX && |
d691f9e8 | 5437 | (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) { |
61bd5218 | 5438 | verbose(env, "BPF_LDX uses reserved fields\n"); |
9bac3d6d AS |
5439 | return -EINVAL; |
5440 | } | |
5441 | ||
d691f9e8 AS |
5442 | if (BPF_CLASS(insn->code) == BPF_STX && |
5443 | ((BPF_MODE(insn->code) != BPF_MEM && | |
5444 | BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) { | |
61bd5218 | 5445 | verbose(env, "BPF_STX uses reserved fields\n"); |
d691f9e8 AS |
5446 | return -EINVAL; |
5447 | } | |
5448 | ||
0246e64d AS |
5449 | if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) { |
5450 | struct bpf_map *map; | |
5451 | struct fd f; | |
5452 | ||
5453 | if (i == insn_cnt - 1 || insn[1].code != 0 || | |
5454 | insn[1].dst_reg != 0 || insn[1].src_reg != 0 || | |
5455 | insn[1].off != 0) { | |
61bd5218 | 5456 | verbose(env, "invalid bpf_ld_imm64 insn\n"); |
0246e64d AS |
5457 | return -EINVAL; |
5458 | } | |
5459 | ||
5460 | if (insn->src_reg == 0) | |
5461 | /* valid generic load 64-bit imm */ | |
5462 | goto next_insn; | |
5463 | ||
5464 | if (insn->src_reg != BPF_PSEUDO_MAP_FD) { | |
61bd5218 JK |
5465 | verbose(env, |
5466 | "unrecognized bpf_ld_imm64 insn\n"); | |
0246e64d AS |
5467 | return -EINVAL; |
5468 | } | |
5469 | ||
5470 | f = fdget(insn->imm); | |
c2101297 | 5471 | map = __bpf_map_get(f); |
0246e64d | 5472 | if (IS_ERR(map)) { |
61bd5218 | 5473 | verbose(env, "fd %d is not pointing to valid bpf_map\n", |
0246e64d | 5474 | insn->imm); |
0246e64d AS |
5475 | return PTR_ERR(map); |
5476 | } | |
5477 | ||
61bd5218 | 5478 | err = check_map_prog_compatibility(env, map, env->prog); |
fdc15d38 AS |
5479 | if (err) { |
5480 | fdput(f); | |
5481 | return err; | |
5482 | } | |
5483 | ||
0246e64d AS |
5484 | /* store map pointer inside BPF_LD_IMM64 instruction */ |
5485 | insn[0].imm = (u32) (unsigned long) map; | |
5486 | insn[1].imm = ((u64) (unsigned long) map) >> 32; | |
5487 | ||
5488 | /* check whether we recorded this map already */ | |
5489 | for (j = 0; j < env->used_map_cnt; j++) | |
5490 | if (env->used_maps[j] == map) { | |
5491 | fdput(f); | |
5492 | goto next_insn; | |
5493 | } | |
5494 | ||
5495 | if (env->used_map_cnt >= MAX_USED_MAPS) { | |
5496 | fdput(f); | |
5497 | return -E2BIG; | |
5498 | } | |
5499 | ||
0246e64d AS |
5500 | /* hold the map. If the program is rejected by verifier, |
5501 | * the map will be released by release_maps() or it | |
5502 | * will be used by the valid program until it's unloaded | |
ab7f5bf0 | 5503 | * and all maps are released in free_used_maps() |
0246e64d | 5504 | */ |
92117d84 AS |
5505 | map = bpf_map_inc(map, false); |
5506 | if (IS_ERR(map)) { | |
5507 | fdput(f); | |
5508 | return PTR_ERR(map); | |
5509 | } | |
5510 | env->used_maps[env->used_map_cnt++] = map; | |
5511 | ||
b741f163 | 5512 | if (bpf_map_is_cgroup_storage(map) && |
de9cbbaa | 5513 | bpf_cgroup_storage_assign(env->prog, map)) { |
b741f163 | 5514 | verbose(env, "only one cgroup storage of each type is allowed\n"); |
de9cbbaa RG |
5515 | fdput(f); |
5516 | return -EBUSY; | |
5517 | } | |
5518 | ||
0246e64d AS |
5519 | fdput(f); |
5520 | next_insn: | |
5521 | insn++; | |
5522 | i++; | |
5e581dad DB |
5523 | continue; |
5524 | } | |
5525 | ||
5526 | /* Basic sanity check before we invest more work here. */ | |
5527 | if (!bpf_opcode_in_insntable(insn->code)) { | |
5528 | verbose(env, "unknown opcode %02x\n", insn->code); | |
5529 | return -EINVAL; | |
0246e64d AS |
5530 | } |
5531 | } | |
5532 | ||
5533 | /* now all pseudo BPF_LD_IMM64 instructions load valid | |
5534 | * 'struct bpf_map *' into a register instead of user map_fd. | |
5535 | * These pointers will be used later by verifier to validate map access. | |
5536 | */ | |
5537 | return 0; | |
5538 | } | |
5539 | ||
5540 | /* drop refcnt of maps used by the rejected program */ | |
58e2af8b | 5541 | static void release_maps(struct bpf_verifier_env *env) |
0246e64d | 5542 | { |
8bad74f9 | 5543 | enum bpf_cgroup_storage_type stype; |
0246e64d AS |
5544 | int i; |
5545 | ||
8bad74f9 RG |
5546 | for_each_cgroup_storage_type(stype) { |
5547 | if (!env->prog->aux->cgroup_storage[stype]) | |
5548 | continue; | |
de9cbbaa | 5549 | bpf_cgroup_storage_release(env->prog, |
8bad74f9 RG |
5550 | env->prog->aux->cgroup_storage[stype]); |
5551 | } | |
de9cbbaa | 5552 | |
0246e64d AS |
5553 | for (i = 0; i < env->used_map_cnt; i++) |
5554 | bpf_map_put(env->used_maps[i]); | |
5555 | } | |
5556 | ||
5557 | /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */ | |
58e2af8b | 5558 | static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env) |
0246e64d AS |
5559 | { |
5560 | struct bpf_insn *insn = env->prog->insnsi; | |
5561 | int insn_cnt = env->prog->len; | |
5562 | int i; | |
5563 | ||
5564 | for (i = 0; i < insn_cnt; i++, insn++) | |
5565 | if (insn->code == (BPF_LD | BPF_IMM | BPF_DW)) | |
5566 | insn->src_reg = 0; | |
5567 | } | |
5568 | ||
8041902d AS |
5569 | /* single env->prog->insni[off] instruction was replaced with the range |
5570 | * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying | |
5571 | * [0, off) and [off, end) to new locations, so the patched range stays zero | |
5572 | */ | |
5573 | static int adjust_insn_aux_data(struct bpf_verifier_env *env, u32 prog_len, | |
5574 | u32 off, u32 cnt) | |
5575 | { | |
5576 | struct bpf_insn_aux_data *new_data, *old_data = env->insn_aux_data; | |
c131187d | 5577 | int i; |
8041902d AS |
5578 | |
5579 | if (cnt == 1) | |
5580 | return 0; | |
fad953ce KC |
5581 | new_data = vzalloc(array_size(prog_len, |
5582 | sizeof(struct bpf_insn_aux_data))); | |
8041902d AS |
5583 | if (!new_data) |
5584 | return -ENOMEM; | |
5585 | memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off); | |
5586 | memcpy(new_data + off + cnt - 1, old_data + off, | |
5587 | sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1)); | |
c131187d AS |
5588 | for (i = off; i < off + cnt - 1; i++) |
5589 | new_data[i].seen = true; | |
8041902d AS |
5590 | env->insn_aux_data = new_data; |
5591 | vfree(old_data); | |
5592 | return 0; | |
5593 | } | |
5594 | ||
cc8b0b92 AS |
5595 | static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len) |
5596 | { | |
5597 | int i; | |
5598 | ||
5599 | if (len == 1) | |
5600 | return; | |
4cb3d99c JW |
5601 | /* NOTE: fake 'exit' subprog should be updated as well. */ |
5602 | for (i = 0; i <= env->subprog_cnt; i++) { | |
9c8105bd | 5603 | if (env->subprog_info[i].start < off) |
cc8b0b92 | 5604 | continue; |
9c8105bd | 5605 | env->subprog_info[i].start += len - 1; |
cc8b0b92 AS |
5606 | } |
5607 | } | |
5608 | ||
8041902d AS |
5609 | static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off, |
5610 | const struct bpf_insn *patch, u32 len) | |
5611 | { | |
5612 | struct bpf_prog *new_prog; | |
5613 | ||
5614 | new_prog = bpf_patch_insn_single(env->prog, off, patch, len); | |
5615 | if (!new_prog) | |
5616 | return NULL; | |
5617 | if (adjust_insn_aux_data(env, new_prog->len, off, len)) | |
5618 | return NULL; | |
cc8b0b92 | 5619 | adjust_subprog_starts(env, off, len); |
8041902d AS |
5620 | return new_prog; |
5621 | } | |
5622 | ||
2a5418a1 DB |
5623 | /* The verifier does more data flow analysis than llvm and will not |
5624 | * explore branches that are dead at run time. Malicious programs can | |
5625 | * have dead code too. Therefore replace all dead at-run-time code | |
5626 | * with 'ja -1'. | |
5627 | * | |
5628 | * Just nops are not optimal, e.g. if they would sit at the end of the | |
5629 | * program and through another bug we would manage to jump there, then | |
5630 | * we'd execute beyond program memory otherwise. Returning exception | |
5631 | * code also wouldn't work since we can have subprogs where the dead | |
5632 | * code could be located. | |
c131187d AS |
5633 | */ |
5634 | static void sanitize_dead_code(struct bpf_verifier_env *env) | |
5635 | { | |
5636 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
2a5418a1 | 5637 | struct bpf_insn trap = BPF_JMP_IMM(BPF_JA, 0, 0, -1); |
c131187d AS |
5638 | struct bpf_insn *insn = env->prog->insnsi; |
5639 | const int insn_cnt = env->prog->len; | |
5640 | int i; | |
5641 | ||
5642 | for (i = 0; i < insn_cnt; i++) { | |
5643 | if (aux_data[i].seen) | |
5644 | continue; | |
2a5418a1 | 5645 | memcpy(insn + i, &trap, sizeof(trap)); |
c131187d AS |
5646 | } |
5647 | } | |
5648 | ||
c64b7983 JS |
5649 | /* convert load instructions that access fields of a context type into a |
5650 | * sequence of instructions that access fields of the underlying structure: | |
5651 | * struct __sk_buff -> struct sk_buff | |
5652 | * struct bpf_sock_ops -> struct sock | |
9bac3d6d | 5653 | */ |
58e2af8b | 5654 | static int convert_ctx_accesses(struct bpf_verifier_env *env) |
9bac3d6d | 5655 | { |
00176a34 | 5656 | const struct bpf_verifier_ops *ops = env->ops; |
f96da094 | 5657 | int i, cnt, size, ctx_field_size, delta = 0; |
3df126f3 | 5658 | const int insn_cnt = env->prog->len; |
36bbef52 | 5659 | struct bpf_insn insn_buf[16], *insn; |
9bac3d6d | 5660 | struct bpf_prog *new_prog; |
d691f9e8 | 5661 | enum bpf_access_type type; |
f96da094 DB |
5662 | bool is_narrower_load; |
5663 | u32 target_size; | |
9bac3d6d | 5664 | |
36bbef52 DB |
5665 | if (ops->gen_prologue) { |
5666 | cnt = ops->gen_prologue(insn_buf, env->seen_direct_write, | |
5667 | env->prog); | |
5668 | if (cnt >= ARRAY_SIZE(insn_buf)) { | |
61bd5218 | 5669 | verbose(env, "bpf verifier is misconfigured\n"); |
36bbef52 DB |
5670 | return -EINVAL; |
5671 | } else if (cnt) { | |
8041902d | 5672 | new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt); |
36bbef52 DB |
5673 | if (!new_prog) |
5674 | return -ENOMEM; | |
8041902d | 5675 | |
36bbef52 | 5676 | env->prog = new_prog; |
3df126f3 | 5677 | delta += cnt - 1; |
36bbef52 DB |
5678 | } |
5679 | } | |
5680 | ||
c64b7983 | 5681 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
9bac3d6d AS |
5682 | return 0; |
5683 | ||
3df126f3 | 5684 | insn = env->prog->insnsi + delta; |
36bbef52 | 5685 | |
9bac3d6d | 5686 | for (i = 0; i < insn_cnt; i++, insn++) { |
c64b7983 JS |
5687 | bpf_convert_ctx_access_t convert_ctx_access; |
5688 | ||
62c7989b DB |
5689 | if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) || |
5690 | insn->code == (BPF_LDX | BPF_MEM | BPF_H) || | |
5691 | insn->code == (BPF_LDX | BPF_MEM | BPF_W) || | |
ea2e7ce5 | 5692 | insn->code == (BPF_LDX | BPF_MEM | BPF_DW)) |
d691f9e8 | 5693 | type = BPF_READ; |
62c7989b DB |
5694 | else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) || |
5695 | insn->code == (BPF_STX | BPF_MEM | BPF_H) || | |
5696 | insn->code == (BPF_STX | BPF_MEM | BPF_W) || | |
ea2e7ce5 | 5697 | insn->code == (BPF_STX | BPF_MEM | BPF_DW)) |
d691f9e8 AS |
5698 | type = BPF_WRITE; |
5699 | else | |
9bac3d6d AS |
5700 | continue; |
5701 | ||
af86ca4e AS |
5702 | if (type == BPF_WRITE && |
5703 | env->insn_aux_data[i + delta].sanitize_stack_off) { | |
5704 | struct bpf_insn patch[] = { | |
5705 | /* Sanitize suspicious stack slot with zero. | |
5706 | * There are no memory dependencies for this store, | |
5707 | * since it's only using frame pointer and immediate | |
5708 | * constant of zero | |
5709 | */ | |
5710 | BPF_ST_MEM(BPF_DW, BPF_REG_FP, | |
5711 | env->insn_aux_data[i + delta].sanitize_stack_off, | |
5712 | 0), | |
5713 | /* the original STX instruction will immediately | |
5714 | * overwrite the same stack slot with appropriate value | |
5715 | */ | |
5716 | *insn, | |
5717 | }; | |
5718 | ||
5719 | cnt = ARRAY_SIZE(patch); | |
5720 | new_prog = bpf_patch_insn_data(env, i + delta, patch, cnt); | |
5721 | if (!new_prog) | |
5722 | return -ENOMEM; | |
5723 | ||
5724 | delta += cnt - 1; | |
5725 | env->prog = new_prog; | |
5726 | insn = new_prog->insnsi + i + delta; | |
5727 | continue; | |
5728 | } | |
5729 | ||
c64b7983 JS |
5730 | switch (env->insn_aux_data[i + delta].ptr_type) { |
5731 | case PTR_TO_CTX: | |
5732 | if (!ops->convert_ctx_access) | |
5733 | continue; | |
5734 | convert_ctx_access = ops->convert_ctx_access; | |
5735 | break; | |
5736 | case PTR_TO_SOCKET: | |
5737 | convert_ctx_access = bpf_sock_convert_ctx_access; | |
5738 | break; | |
5739 | default: | |
9bac3d6d | 5740 | continue; |
c64b7983 | 5741 | } |
9bac3d6d | 5742 | |
31fd8581 | 5743 | ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size; |
f96da094 | 5744 | size = BPF_LDST_BYTES(insn); |
31fd8581 YS |
5745 | |
5746 | /* If the read access is a narrower load of the field, | |
5747 | * convert to a 4/8-byte load, to minimum program type specific | |
5748 | * convert_ctx_access changes. If conversion is successful, | |
5749 | * we will apply proper mask to the result. | |
5750 | */ | |
f96da094 | 5751 | is_narrower_load = size < ctx_field_size; |
31fd8581 | 5752 | if (is_narrower_load) { |
bc23105c | 5753 | u32 size_default = bpf_ctx_off_adjust_machine(ctx_field_size); |
f96da094 DB |
5754 | u32 off = insn->off; |
5755 | u8 size_code; | |
5756 | ||
5757 | if (type == BPF_WRITE) { | |
61bd5218 | 5758 | verbose(env, "bpf verifier narrow ctx access misconfigured\n"); |
f96da094 DB |
5759 | return -EINVAL; |
5760 | } | |
31fd8581 | 5761 | |
f96da094 | 5762 | size_code = BPF_H; |
31fd8581 YS |
5763 | if (ctx_field_size == 4) |
5764 | size_code = BPF_W; | |
5765 | else if (ctx_field_size == 8) | |
5766 | size_code = BPF_DW; | |
f96da094 | 5767 | |
bc23105c | 5768 | insn->off = off & ~(size_default - 1); |
31fd8581 YS |
5769 | insn->code = BPF_LDX | BPF_MEM | size_code; |
5770 | } | |
f96da094 DB |
5771 | |
5772 | target_size = 0; | |
c64b7983 JS |
5773 | cnt = convert_ctx_access(type, insn, insn_buf, env->prog, |
5774 | &target_size); | |
f96da094 DB |
5775 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) || |
5776 | (ctx_field_size && !target_size)) { | |
61bd5218 | 5777 | verbose(env, "bpf verifier is misconfigured\n"); |
9bac3d6d AS |
5778 | return -EINVAL; |
5779 | } | |
f96da094 DB |
5780 | |
5781 | if (is_narrower_load && size < target_size) { | |
31fd8581 YS |
5782 | if (ctx_field_size <= 4) |
5783 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg, | |
f96da094 | 5784 | (1 << size * 8) - 1); |
31fd8581 YS |
5785 | else |
5786 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg, | |
f96da094 | 5787 | (1 << size * 8) - 1); |
31fd8581 | 5788 | } |
9bac3d6d | 5789 | |
8041902d | 5790 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
9bac3d6d AS |
5791 | if (!new_prog) |
5792 | return -ENOMEM; | |
5793 | ||
3df126f3 | 5794 | delta += cnt - 1; |
9bac3d6d AS |
5795 | |
5796 | /* keep walking new program and skip insns we just inserted */ | |
5797 | env->prog = new_prog; | |
3df126f3 | 5798 | insn = new_prog->insnsi + i + delta; |
9bac3d6d AS |
5799 | } |
5800 | ||
5801 | return 0; | |
5802 | } | |
5803 | ||
1c2a088a AS |
5804 | static int jit_subprogs(struct bpf_verifier_env *env) |
5805 | { | |
5806 | struct bpf_prog *prog = env->prog, **func, *tmp; | |
5807 | int i, j, subprog_start, subprog_end = 0, len, subprog; | |
7105e828 | 5808 | struct bpf_insn *insn; |
1c2a088a AS |
5809 | void *old_bpf_func; |
5810 | int err = -ENOMEM; | |
5811 | ||
f910cefa | 5812 | if (env->subprog_cnt <= 1) |
1c2a088a AS |
5813 | return 0; |
5814 | ||
7105e828 | 5815 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
1c2a088a AS |
5816 | if (insn->code != (BPF_JMP | BPF_CALL) || |
5817 | insn->src_reg != BPF_PSEUDO_CALL) | |
5818 | continue; | |
c7a89784 DB |
5819 | /* Upon error here we cannot fall back to interpreter but |
5820 | * need a hard reject of the program. Thus -EFAULT is | |
5821 | * propagated in any case. | |
5822 | */ | |
1c2a088a AS |
5823 | subprog = find_subprog(env, i + insn->imm + 1); |
5824 | if (subprog < 0) { | |
5825 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", | |
5826 | i + insn->imm + 1); | |
5827 | return -EFAULT; | |
5828 | } | |
5829 | /* temporarily remember subprog id inside insn instead of | |
5830 | * aux_data, since next loop will split up all insns into funcs | |
5831 | */ | |
f910cefa | 5832 | insn->off = subprog; |
1c2a088a AS |
5833 | /* remember original imm in case JIT fails and fallback |
5834 | * to interpreter will be needed | |
5835 | */ | |
5836 | env->insn_aux_data[i].call_imm = insn->imm; | |
5837 | /* point imm to __bpf_call_base+1 from JITs point of view */ | |
5838 | insn->imm = 1; | |
5839 | } | |
5840 | ||
6396bb22 | 5841 | func = kcalloc(env->subprog_cnt, sizeof(prog), GFP_KERNEL); |
1c2a088a | 5842 | if (!func) |
c7a89784 | 5843 | goto out_undo_insn; |
1c2a088a | 5844 | |
f910cefa | 5845 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a | 5846 | subprog_start = subprog_end; |
4cb3d99c | 5847 | subprog_end = env->subprog_info[i + 1].start; |
1c2a088a AS |
5848 | |
5849 | len = subprog_end - subprog_start; | |
5850 | func[i] = bpf_prog_alloc(bpf_prog_size(len), GFP_USER); | |
5851 | if (!func[i]) | |
5852 | goto out_free; | |
5853 | memcpy(func[i]->insnsi, &prog->insnsi[subprog_start], | |
5854 | len * sizeof(struct bpf_insn)); | |
4f74d809 | 5855 | func[i]->type = prog->type; |
1c2a088a | 5856 | func[i]->len = len; |
4f74d809 DB |
5857 | if (bpf_prog_calc_tag(func[i])) |
5858 | goto out_free; | |
1c2a088a AS |
5859 | func[i]->is_func = 1; |
5860 | /* Use bpf_prog_F_tag to indicate functions in stack traces. | |
5861 | * Long term would need debug info to populate names | |
5862 | */ | |
5863 | func[i]->aux->name[0] = 'F'; | |
9c8105bd | 5864 | func[i]->aux->stack_depth = env->subprog_info[i].stack_depth; |
1c2a088a AS |
5865 | func[i]->jit_requested = 1; |
5866 | func[i] = bpf_int_jit_compile(func[i]); | |
5867 | if (!func[i]->jited) { | |
5868 | err = -ENOTSUPP; | |
5869 | goto out_free; | |
5870 | } | |
5871 | cond_resched(); | |
5872 | } | |
5873 | /* at this point all bpf functions were successfully JITed | |
5874 | * now populate all bpf_calls with correct addresses and | |
5875 | * run last pass of JIT | |
5876 | */ | |
f910cefa | 5877 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a AS |
5878 | insn = func[i]->insnsi; |
5879 | for (j = 0; j < func[i]->len; j++, insn++) { | |
5880 | if (insn->code != (BPF_JMP | BPF_CALL) || | |
5881 | insn->src_reg != BPF_PSEUDO_CALL) | |
5882 | continue; | |
5883 | subprog = insn->off; | |
1c2a088a AS |
5884 | insn->imm = (u64 (*)(u64, u64, u64, u64, u64)) |
5885 | func[subprog]->bpf_func - | |
5886 | __bpf_call_base; | |
5887 | } | |
2162fed4 SD |
5888 | |
5889 | /* we use the aux data to keep a list of the start addresses | |
5890 | * of the JITed images for each function in the program | |
5891 | * | |
5892 | * for some architectures, such as powerpc64, the imm field | |
5893 | * might not be large enough to hold the offset of the start | |
5894 | * address of the callee's JITed image from __bpf_call_base | |
5895 | * | |
5896 | * in such cases, we can lookup the start address of a callee | |
5897 | * by using its subprog id, available from the off field of | |
5898 | * the call instruction, as an index for this list | |
5899 | */ | |
5900 | func[i]->aux->func = func; | |
5901 | func[i]->aux->func_cnt = env->subprog_cnt; | |
1c2a088a | 5902 | } |
f910cefa | 5903 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a AS |
5904 | old_bpf_func = func[i]->bpf_func; |
5905 | tmp = bpf_int_jit_compile(func[i]); | |
5906 | if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) { | |
5907 | verbose(env, "JIT doesn't support bpf-to-bpf calls\n"); | |
c7a89784 | 5908 | err = -ENOTSUPP; |
1c2a088a AS |
5909 | goto out_free; |
5910 | } | |
5911 | cond_resched(); | |
5912 | } | |
5913 | ||
5914 | /* finally lock prog and jit images for all functions and | |
5915 | * populate kallsysm | |
5916 | */ | |
f910cefa | 5917 | for (i = 0; i < env->subprog_cnt; i++) { |
1c2a088a AS |
5918 | bpf_prog_lock_ro(func[i]); |
5919 | bpf_prog_kallsyms_add(func[i]); | |
5920 | } | |
7105e828 DB |
5921 | |
5922 | /* Last step: make now unused interpreter insns from main | |
5923 | * prog consistent for later dump requests, so they can | |
5924 | * later look the same as if they were interpreted only. | |
5925 | */ | |
5926 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { | |
7105e828 DB |
5927 | if (insn->code != (BPF_JMP | BPF_CALL) || |
5928 | insn->src_reg != BPF_PSEUDO_CALL) | |
5929 | continue; | |
5930 | insn->off = env->insn_aux_data[i].call_imm; | |
5931 | subprog = find_subprog(env, i + insn->off + 1); | |
dbecd738 | 5932 | insn->imm = subprog; |
7105e828 DB |
5933 | } |
5934 | ||
1c2a088a AS |
5935 | prog->jited = 1; |
5936 | prog->bpf_func = func[0]->bpf_func; | |
5937 | prog->aux->func = func; | |
f910cefa | 5938 | prog->aux->func_cnt = env->subprog_cnt; |
1c2a088a AS |
5939 | return 0; |
5940 | out_free: | |
f910cefa | 5941 | for (i = 0; i < env->subprog_cnt; i++) |
1c2a088a AS |
5942 | if (func[i]) |
5943 | bpf_jit_free(func[i]); | |
5944 | kfree(func); | |
c7a89784 | 5945 | out_undo_insn: |
1c2a088a AS |
5946 | /* cleanup main prog to be interpreted */ |
5947 | prog->jit_requested = 0; | |
5948 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { | |
5949 | if (insn->code != (BPF_JMP | BPF_CALL) || | |
5950 | insn->src_reg != BPF_PSEUDO_CALL) | |
5951 | continue; | |
5952 | insn->off = 0; | |
5953 | insn->imm = env->insn_aux_data[i].call_imm; | |
5954 | } | |
5955 | return err; | |
5956 | } | |
5957 | ||
1ea47e01 AS |
5958 | static int fixup_call_args(struct bpf_verifier_env *env) |
5959 | { | |
19d28fbd | 5960 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
1ea47e01 AS |
5961 | struct bpf_prog *prog = env->prog; |
5962 | struct bpf_insn *insn = prog->insnsi; | |
5963 | int i, depth; | |
19d28fbd DM |
5964 | #endif |
5965 | int err; | |
1ea47e01 | 5966 | |
19d28fbd DM |
5967 | err = 0; |
5968 | if (env->prog->jit_requested) { | |
5969 | err = jit_subprogs(env); | |
5970 | if (err == 0) | |
1c2a088a | 5971 | return 0; |
c7a89784 DB |
5972 | if (err == -EFAULT) |
5973 | return err; | |
19d28fbd DM |
5974 | } |
5975 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON | |
1ea47e01 AS |
5976 | for (i = 0; i < prog->len; i++, insn++) { |
5977 | if (insn->code != (BPF_JMP | BPF_CALL) || | |
5978 | insn->src_reg != BPF_PSEUDO_CALL) | |
5979 | continue; | |
5980 | depth = get_callee_stack_depth(env, insn, i); | |
5981 | if (depth < 0) | |
5982 | return depth; | |
5983 | bpf_patch_call_args(insn, depth); | |
5984 | } | |
19d28fbd DM |
5985 | err = 0; |
5986 | #endif | |
5987 | return err; | |
1ea47e01 AS |
5988 | } |
5989 | ||
79741b3b | 5990 | /* fixup insn->imm field of bpf_call instructions |
81ed18ab | 5991 | * and inline eligible helpers as explicit sequence of BPF instructions |
e245c5c6 AS |
5992 | * |
5993 | * this function is called after eBPF program passed verification | |
5994 | */ | |
79741b3b | 5995 | static int fixup_bpf_calls(struct bpf_verifier_env *env) |
e245c5c6 | 5996 | { |
79741b3b AS |
5997 | struct bpf_prog *prog = env->prog; |
5998 | struct bpf_insn *insn = prog->insnsi; | |
e245c5c6 | 5999 | const struct bpf_func_proto *fn; |
79741b3b | 6000 | const int insn_cnt = prog->len; |
09772d92 | 6001 | const struct bpf_map_ops *ops; |
c93552c4 | 6002 | struct bpf_insn_aux_data *aux; |
81ed18ab AS |
6003 | struct bpf_insn insn_buf[16]; |
6004 | struct bpf_prog *new_prog; | |
6005 | struct bpf_map *map_ptr; | |
6006 | int i, cnt, delta = 0; | |
e245c5c6 | 6007 | |
79741b3b | 6008 | for (i = 0; i < insn_cnt; i++, insn++) { |
f6b1b3bf DB |
6009 | if (insn->code == (BPF_ALU64 | BPF_MOD | BPF_X) || |
6010 | insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) || | |
6011 | insn->code == (BPF_ALU | BPF_MOD | BPF_X) || | |
68fda450 | 6012 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { |
f6b1b3bf DB |
6013 | bool is64 = BPF_CLASS(insn->code) == BPF_ALU64; |
6014 | struct bpf_insn mask_and_div[] = { | |
6015 | BPF_MOV32_REG(insn->src_reg, insn->src_reg), | |
6016 | /* Rx div 0 -> 0 */ | |
6017 | BPF_JMP_IMM(BPF_JNE, insn->src_reg, 0, 2), | |
6018 | BPF_ALU32_REG(BPF_XOR, insn->dst_reg, insn->dst_reg), | |
6019 | BPF_JMP_IMM(BPF_JA, 0, 0, 1), | |
6020 | *insn, | |
6021 | }; | |
6022 | struct bpf_insn mask_and_mod[] = { | |
6023 | BPF_MOV32_REG(insn->src_reg, insn->src_reg), | |
6024 | /* Rx mod 0 -> Rx */ | |
6025 | BPF_JMP_IMM(BPF_JEQ, insn->src_reg, 0, 1), | |
6026 | *insn, | |
6027 | }; | |
6028 | struct bpf_insn *patchlet; | |
6029 | ||
6030 | if (insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) || | |
6031 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { | |
6032 | patchlet = mask_and_div + (is64 ? 1 : 0); | |
6033 | cnt = ARRAY_SIZE(mask_and_div) - (is64 ? 1 : 0); | |
6034 | } else { | |
6035 | patchlet = mask_and_mod + (is64 ? 1 : 0); | |
6036 | cnt = ARRAY_SIZE(mask_and_mod) - (is64 ? 1 : 0); | |
6037 | } | |
6038 | ||
6039 | new_prog = bpf_patch_insn_data(env, i + delta, patchlet, cnt); | |
68fda450 AS |
6040 | if (!new_prog) |
6041 | return -ENOMEM; | |
6042 | ||
6043 | delta += cnt - 1; | |
6044 | env->prog = prog = new_prog; | |
6045 | insn = new_prog->insnsi + i + delta; | |
6046 | continue; | |
6047 | } | |
6048 | ||
e0cea7ce DB |
6049 | if (BPF_CLASS(insn->code) == BPF_LD && |
6050 | (BPF_MODE(insn->code) == BPF_ABS || | |
6051 | BPF_MODE(insn->code) == BPF_IND)) { | |
6052 | cnt = env->ops->gen_ld_abs(insn, insn_buf); | |
6053 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { | |
6054 | verbose(env, "bpf verifier is misconfigured\n"); | |
6055 | return -EINVAL; | |
6056 | } | |
6057 | ||
6058 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
6059 | if (!new_prog) | |
6060 | return -ENOMEM; | |
6061 | ||
6062 | delta += cnt - 1; | |
6063 | env->prog = prog = new_prog; | |
6064 | insn = new_prog->insnsi + i + delta; | |
6065 | continue; | |
6066 | } | |
6067 | ||
79741b3b AS |
6068 | if (insn->code != (BPF_JMP | BPF_CALL)) |
6069 | continue; | |
cc8b0b92 AS |
6070 | if (insn->src_reg == BPF_PSEUDO_CALL) |
6071 | continue; | |
e245c5c6 | 6072 | |
79741b3b AS |
6073 | if (insn->imm == BPF_FUNC_get_route_realm) |
6074 | prog->dst_needed = 1; | |
6075 | if (insn->imm == BPF_FUNC_get_prandom_u32) | |
6076 | bpf_user_rnd_init_once(); | |
9802d865 JB |
6077 | if (insn->imm == BPF_FUNC_override_return) |
6078 | prog->kprobe_override = 1; | |
79741b3b | 6079 | if (insn->imm == BPF_FUNC_tail_call) { |
7b9f6da1 DM |
6080 | /* If we tail call into other programs, we |
6081 | * cannot make any assumptions since they can | |
6082 | * be replaced dynamically during runtime in | |
6083 | * the program array. | |
6084 | */ | |
6085 | prog->cb_access = 1; | |
80a58d02 | 6086 | env->prog->aux->stack_depth = MAX_BPF_STACK; |
7b9f6da1 | 6087 | |
79741b3b AS |
6088 | /* mark bpf_tail_call as different opcode to avoid |
6089 | * conditional branch in the interpeter for every normal | |
6090 | * call and to prevent accidental JITing by JIT compiler | |
6091 | * that doesn't support bpf_tail_call yet | |
e245c5c6 | 6092 | */ |
79741b3b | 6093 | insn->imm = 0; |
71189fa9 | 6094 | insn->code = BPF_JMP | BPF_TAIL_CALL; |
b2157399 | 6095 | |
c93552c4 DB |
6096 | aux = &env->insn_aux_data[i + delta]; |
6097 | if (!bpf_map_ptr_unpriv(aux)) | |
6098 | continue; | |
6099 | ||
b2157399 AS |
6100 | /* instead of changing every JIT dealing with tail_call |
6101 | * emit two extra insns: | |
6102 | * if (index >= max_entries) goto out; | |
6103 | * index &= array->index_mask; | |
6104 | * to avoid out-of-bounds cpu speculation | |
6105 | */ | |
c93552c4 | 6106 | if (bpf_map_ptr_poisoned(aux)) { |
40950343 | 6107 | verbose(env, "tail_call abusing map_ptr\n"); |
b2157399 AS |
6108 | return -EINVAL; |
6109 | } | |
c93552c4 DB |
6110 | |
6111 | map_ptr = BPF_MAP_PTR(aux->map_state); | |
b2157399 AS |
6112 | insn_buf[0] = BPF_JMP_IMM(BPF_JGE, BPF_REG_3, |
6113 | map_ptr->max_entries, 2); | |
6114 | insn_buf[1] = BPF_ALU32_IMM(BPF_AND, BPF_REG_3, | |
6115 | container_of(map_ptr, | |
6116 | struct bpf_array, | |
6117 | map)->index_mask); | |
6118 | insn_buf[2] = *insn; | |
6119 | cnt = 3; | |
6120 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); | |
6121 | if (!new_prog) | |
6122 | return -ENOMEM; | |
6123 | ||
6124 | delta += cnt - 1; | |
6125 | env->prog = prog = new_prog; | |
6126 | insn = new_prog->insnsi + i + delta; | |
79741b3b AS |
6127 | continue; |
6128 | } | |
e245c5c6 | 6129 | |
89c63074 | 6130 | /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup |
09772d92 DB |
6131 | * and other inlining handlers are currently limited to 64 bit |
6132 | * only. | |
89c63074 | 6133 | */ |
60b58afc | 6134 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
09772d92 DB |
6135 | (insn->imm == BPF_FUNC_map_lookup_elem || |
6136 | insn->imm == BPF_FUNC_map_update_elem || | |
6137 | insn->imm == BPF_FUNC_map_delete_elem)) { | |
c93552c4 DB |
6138 | aux = &env->insn_aux_data[i + delta]; |
6139 | if (bpf_map_ptr_poisoned(aux)) | |
6140 | goto patch_call_imm; | |
6141 | ||
6142 | map_ptr = BPF_MAP_PTR(aux->map_state); | |
09772d92 DB |
6143 | ops = map_ptr->ops; |
6144 | if (insn->imm == BPF_FUNC_map_lookup_elem && | |
6145 | ops->map_gen_lookup) { | |
6146 | cnt = ops->map_gen_lookup(map_ptr, insn_buf); | |
6147 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { | |
6148 | verbose(env, "bpf verifier is misconfigured\n"); | |
6149 | return -EINVAL; | |
6150 | } | |
81ed18ab | 6151 | |
09772d92 DB |
6152 | new_prog = bpf_patch_insn_data(env, i + delta, |
6153 | insn_buf, cnt); | |
6154 | if (!new_prog) | |
6155 | return -ENOMEM; | |
81ed18ab | 6156 | |
09772d92 DB |
6157 | delta += cnt - 1; |
6158 | env->prog = prog = new_prog; | |
6159 | insn = new_prog->insnsi + i + delta; | |
6160 | continue; | |
6161 | } | |
81ed18ab | 6162 | |
09772d92 DB |
6163 | BUILD_BUG_ON(!__same_type(ops->map_lookup_elem, |
6164 | (void *(*)(struct bpf_map *map, void *key))NULL)); | |
6165 | BUILD_BUG_ON(!__same_type(ops->map_delete_elem, | |
6166 | (int (*)(struct bpf_map *map, void *key))NULL)); | |
6167 | BUILD_BUG_ON(!__same_type(ops->map_update_elem, | |
6168 | (int (*)(struct bpf_map *map, void *key, void *value, | |
6169 | u64 flags))NULL)); | |
6170 | switch (insn->imm) { | |
6171 | case BPF_FUNC_map_lookup_elem: | |
6172 | insn->imm = BPF_CAST_CALL(ops->map_lookup_elem) - | |
6173 | __bpf_call_base; | |
6174 | continue; | |
6175 | case BPF_FUNC_map_update_elem: | |
6176 | insn->imm = BPF_CAST_CALL(ops->map_update_elem) - | |
6177 | __bpf_call_base; | |
6178 | continue; | |
6179 | case BPF_FUNC_map_delete_elem: | |
6180 | insn->imm = BPF_CAST_CALL(ops->map_delete_elem) - | |
6181 | __bpf_call_base; | |
6182 | continue; | |
6183 | } | |
81ed18ab | 6184 | |
09772d92 | 6185 | goto patch_call_imm; |
81ed18ab AS |
6186 | } |
6187 | ||
6188 | patch_call_imm: | |
5e43f899 | 6189 | fn = env->ops->get_func_proto(insn->imm, env->prog); |
79741b3b AS |
6190 | /* all functions that have prototype and verifier allowed |
6191 | * programs to call them, must be real in-kernel functions | |
6192 | */ | |
6193 | if (!fn->func) { | |
61bd5218 JK |
6194 | verbose(env, |
6195 | "kernel subsystem misconfigured func %s#%d\n", | |
79741b3b AS |
6196 | func_id_name(insn->imm), insn->imm); |
6197 | return -EFAULT; | |
e245c5c6 | 6198 | } |
79741b3b | 6199 | insn->imm = fn->func - __bpf_call_base; |
e245c5c6 | 6200 | } |
e245c5c6 | 6201 | |
79741b3b AS |
6202 | return 0; |
6203 | } | |
e245c5c6 | 6204 | |
58e2af8b | 6205 | static void free_states(struct bpf_verifier_env *env) |
f1bca824 | 6206 | { |
58e2af8b | 6207 | struct bpf_verifier_state_list *sl, *sln; |
f1bca824 AS |
6208 | int i; |
6209 | ||
6210 | if (!env->explored_states) | |
6211 | return; | |
6212 | ||
6213 | for (i = 0; i < env->prog->len; i++) { | |
6214 | sl = env->explored_states[i]; | |
6215 | ||
6216 | if (sl) | |
6217 | while (sl != STATE_LIST_MARK) { | |
6218 | sln = sl->next; | |
1969db47 | 6219 | free_verifier_state(&sl->state, false); |
f1bca824 AS |
6220 | kfree(sl); |
6221 | sl = sln; | |
6222 | } | |
6223 | } | |
6224 | ||
6225 | kfree(env->explored_states); | |
6226 | } | |
6227 | ||
9bac3d6d | 6228 | int bpf_check(struct bpf_prog **prog, union bpf_attr *attr) |
51580e79 | 6229 | { |
58e2af8b | 6230 | struct bpf_verifier_env *env; |
b9193c1b | 6231 | struct bpf_verifier_log *log; |
51580e79 AS |
6232 | int ret = -EINVAL; |
6233 | ||
eba0c929 AB |
6234 | /* no program is valid */ |
6235 | if (ARRAY_SIZE(bpf_verifier_ops) == 0) | |
6236 | return -EINVAL; | |
6237 | ||
58e2af8b | 6238 | /* 'struct bpf_verifier_env' can be global, but since it's not small, |
cbd35700 AS |
6239 | * allocate/free it every time bpf_check() is called |
6240 | */ | |
58e2af8b | 6241 | env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL); |
cbd35700 AS |
6242 | if (!env) |
6243 | return -ENOMEM; | |
61bd5218 | 6244 | log = &env->log; |
cbd35700 | 6245 | |
fad953ce KC |
6246 | env->insn_aux_data = |
6247 | vzalloc(array_size(sizeof(struct bpf_insn_aux_data), | |
6248 | (*prog)->len)); | |
3df126f3 JK |
6249 | ret = -ENOMEM; |
6250 | if (!env->insn_aux_data) | |
6251 | goto err_free_env; | |
9bac3d6d | 6252 | env->prog = *prog; |
00176a34 | 6253 | env->ops = bpf_verifier_ops[env->prog->type]; |
0246e64d | 6254 | |
cbd35700 AS |
6255 | /* grab the mutex to protect few globals used by verifier */ |
6256 | mutex_lock(&bpf_verifier_lock); | |
6257 | ||
6258 | if (attr->log_level || attr->log_buf || attr->log_size) { | |
6259 | /* user requested verbose verifier output | |
6260 | * and supplied buffer to store the verification trace | |
6261 | */ | |
e7bf8249 JK |
6262 | log->level = attr->log_level; |
6263 | log->ubuf = (char __user *) (unsigned long) attr->log_buf; | |
6264 | log->len_total = attr->log_size; | |
cbd35700 AS |
6265 | |
6266 | ret = -EINVAL; | |
e7bf8249 JK |
6267 | /* log attributes have to be sane */ |
6268 | if (log->len_total < 128 || log->len_total > UINT_MAX >> 8 || | |
6269 | !log->level || !log->ubuf) | |
3df126f3 | 6270 | goto err_unlock; |
cbd35700 | 6271 | } |
1ad2f583 DB |
6272 | |
6273 | env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT); | |
6274 | if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) | |
e07b98d9 | 6275 | env->strict_alignment = true; |
cbd35700 | 6276 | |
f4e3ec0d JK |
6277 | ret = replace_map_fd_with_map_ptr(env); |
6278 | if (ret < 0) | |
6279 | goto skip_full_check; | |
6280 | ||
cae1927c | 6281 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
ab3f0063 JK |
6282 | ret = bpf_prog_offload_verifier_prep(env); |
6283 | if (ret) | |
f4e3ec0d | 6284 | goto skip_full_check; |
ab3f0063 JK |
6285 | } |
6286 | ||
9bac3d6d | 6287 | env->explored_states = kcalloc(env->prog->len, |
58e2af8b | 6288 | sizeof(struct bpf_verifier_state_list *), |
f1bca824 AS |
6289 | GFP_USER); |
6290 | ret = -ENOMEM; | |
6291 | if (!env->explored_states) | |
6292 | goto skip_full_check; | |
6293 | ||
cc8b0b92 AS |
6294 | env->allow_ptr_leaks = capable(CAP_SYS_ADMIN); |
6295 | ||
475fb78f AS |
6296 | ret = check_cfg(env); |
6297 | if (ret < 0) | |
6298 | goto skip_full_check; | |
6299 | ||
17a52670 | 6300 | ret = do_check(env); |
8c01c4f8 CG |
6301 | if (env->cur_state) { |
6302 | free_verifier_state(env->cur_state, true); | |
6303 | env->cur_state = NULL; | |
6304 | } | |
cbd35700 | 6305 | |
0246e64d | 6306 | skip_full_check: |
638f5b90 | 6307 | while (!pop_stack(env, NULL, NULL)); |
f1bca824 | 6308 | free_states(env); |
0246e64d | 6309 | |
c131187d AS |
6310 | if (ret == 0) |
6311 | sanitize_dead_code(env); | |
6312 | ||
70a87ffe AS |
6313 | if (ret == 0) |
6314 | ret = check_max_stack_depth(env); | |
6315 | ||
9bac3d6d AS |
6316 | if (ret == 0) |
6317 | /* program is valid, convert *(u32*)(ctx + off) accesses */ | |
6318 | ret = convert_ctx_accesses(env); | |
6319 | ||
e245c5c6 | 6320 | if (ret == 0) |
79741b3b | 6321 | ret = fixup_bpf_calls(env); |
e245c5c6 | 6322 | |
1ea47e01 AS |
6323 | if (ret == 0) |
6324 | ret = fixup_call_args(env); | |
6325 | ||
a2a7d570 | 6326 | if (log->level && bpf_verifier_log_full(log)) |
cbd35700 | 6327 | ret = -ENOSPC; |
a2a7d570 | 6328 | if (log->level && !log->ubuf) { |
cbd35700 | 6329 | ret = -EFAULT; |
a2a7d570 | 6330 | goto err_release_maps; |
cbd35700 AS |
6331 | } |
6332 | ||
0246e64d AS |
6333 | if (ret == 0 && env->used_map_cnt) { |
6334 | /* if program passed verifier, update used_maps in bpf_prog_info */ | |
9bac3d6d AS |
6335 | env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt, |
6336 | sizeof(env->used_maps[0]), | |
6337 | GFP_KERNEL); | |
0246e64d | 6338 | |
9bac3d6d | 6339 | if (!env->prog->aux->used_maps) { |
0246e64d | 6340 | ret = -ENOMEM; |
a2a7d570 | 6341 | goto err_release_maps; |
0246e64d AS |
6342 | } |
6343 | ||
9bac3d6d | 6344 | memcpy(env->prog->aux->used_maps, env->used_maps, |
0246e64d | 6345 | sizeof(env->used_maps[0]) * env->used_map_cnt); |
9bac3d6d | 6346 | env->prog->aux->used_map_cnt = env->used_map_cnt; |
0246e64d AS |
6347 | |
6348 | /* program is valid. Convert pseudo bpf_ld_imm64 into generic | |
6349 | * bpf_ld_imm64 instructions | |
6350 | */ | |
6351 | convert_pseudo_ld_imm64(env); | |
6352 | } | |
cbd35700 | 6353 | |
a2a7d570 | 6354 | err_release_maps: |
9bac3d6d | 6355 | if (!env->prog->aux->used_maps) |
0246e64d | 6356 | /* if we didn't copy map pointers into bpf_prog_info, release |
ab7f5bf0 | 6357 | * them now. Otherwise free_used_maps() will release them. |
0246e64d AS |
6358 | */ |
6359 | release_maps(env); | |
9bac3d6d | 6360 | *prog = env->prog; |
3df126f3 | 6361 | err_unlock: |
cbd35700 | 6362 | mutex_unlock(&bpf_verifier_lock); |
3df126f3 JK |
6363 | vfree(env->insn_aux_data); |
6364 | err_free_env: | |
6365 | kfree(env); | |
51580e79 AS |
6366 | return ret; |
6367 | } |