]>
Commit | Line | Data |
---|---|---|
51580e79 | 1 | /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com |
969bf05e | 2 | * Copyright (c) 2016 Facebook |
51580e79 AS |
3 | * |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of version 2 of the GNU General Public | |
6 | * License as published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, but | |
9 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
11 | * General Public License for more details. | |
12 | */ | |
13 | #include <linux/kernel.h> | |
14 | #include <linux/types.h> | |
15 | #include <linux/slab.h> | |
16 | #include <linux/bpf.h> | |
58e2af8b | 17 | #include <linux/bpf_verifier.h> |
51580e79 AS |
18 | #include <linux/filter.h> |
19 | #include <net/netlink.h> | |
20 | #include <linux/file.h> | |
21 | #include <linux/vmalloc.h> | |
ebb676da | 22 | #include <linux/stringify.h> |
51580e79 | 23 | |
f4ac7e0b JK |
24 | #include "disasm.h" |
25 | ||
00176a34 JK |
26 | static const struct bpf_verifier_ops * const bpf_verifier_ops[] = { |
27 | #define BPF_PROG_TYPE(_id, _name) \ | |
28 | [_id] = & _name ## _verifier_ops, | |
29 | #define BPF_MAP_TYPE(_id, _ops) | |
30 | #include <linux/bpf_types.h> | |
31 | #undef BPF_PROG_TYPE | |
32 | #undef BPF_MAP_TYPE | |
33 | }; | |
34 | ||
51580e79 AS |
35 | /* bpf_check() is a static code analyzer that walks eBPF program |
36 | * instruction by instruction and updates register/stack state. | |
37 | * All paths of conditional branches are analyzed until 'bpf_exit' insn. | |
38 | * | |
39 | * The first pass is depth-first-search to check that the program is a DAG. | |
40 | * It rejects the following programs: | |
41 | * - larger than BPF_MAXINSNS insns | |
42 | * - if loop is present (detected via back-edge) | |
43 | * - unreachable insns exist (shouldn't be a forest. program = one function) | |
44 | * - out of bounds or malformed jumps | |
45 | * The second pass is all possible path descent from the 1st insn. | |
46 | * Since it's analyzing all pathes through the program, the length of the | |
eba38a96 | 47 | * analysis is limited to 64k insn, which may be hit even if total number of |
51580e79 AS |
48 | * insn is less then 4K, but there are too many branches that change stack/regs. |
49 | * Number of 'branches to be analyzed' is limited to 1k | |
50 | * | |
51 | * On entry to each instruction, each register has a type, and the instruction | |
52 | * changes the types of the registers depending on instruction semantics. | |
53 | * If instruction is BPF_MOV64_REG(BPF_REG_1, BPF_REG_5), then type of R5 is | |
54 | * copied to R1. | |
55 | * | |
56 | * All registers are 64-bit. | |
57 | * R0 - return register | |
58 | * R1-R5 argument passing registers | |
59 | * R6-R9 callee saved registers | |
60 | * R10 - frame pointer read-only | |
61 | * | |
62 | * At the start of BPF program the register R1 contains a pointer to bpf_context | |
63 | * and has type PTR_TO_CTX. | |
64 | * | |
65 | * Verifier tracks arithmetic operations on pointers in case: | |
66 | * BPF_MOV64_REG(BPF_REG_1, BPF_REG_10), | |
67 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -20), | |
68 | * 1st insn copies R10 (which has FRAME_PTR) type into R1 | |
69 | * and 2nd arithmetic instruction is pattern matched to recognize | |
70 | * that it wants to construct a pointer to some element within stack. | |
71 | * So after 2nd insn, the register R1 has type PTR_TO_STACK | |
72 | * (and -20 constant is saved for further stack bounds checking). | |
73 | * Meaning that this reg is a pointer to stack plus known immediate constant. | |
74 | * | |
f1174f77 | 75 | * Most of the time the registers have SCALAR_VALUE type, which |
51580e79 | 76 | * means the register has some value, but it's not a valid pointer. |
f1174f77 | 77 | * (like pointer plus pointer becomes SCALAR_VALUE type) |
51580e79 AS |
78 | * |
79 | * When verifier sees load or store instructions the type of base register | |
f1174f77 | 80 | * can be: PTR_TO_MAP_VALUE, PTR_TO_CTX, PTR_TO_STACK. These are three pointer |
51580e79 AS |
81 | * types recognized by check_mem_access() function. |
82 | * | |
83 | * PTR_TO_MAP_VALUE means that this register is pointing to 'map element value' | |
84 | * and the range of [ptr, ptr + map's value_size) is accessible. | |
85 | * | |
86 | * registers used to pass values to function calls are checked against | |
87 | * function argument constraints. | |
88 | * | |
89 | * ARG_PTR_TO_MAP_KEY is one of such argument constraints. | |
90 | * It means that the register type passed to this function must be | |
91 | * PTR_TO_STACK and it will be used inside the function as | |
92 | * 'pointer to map element key' | |
93 | * | |
94 | * For example the argument constraints for bpf_map_lookup_elem(): | |
95 | * .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL, | |
96 | * .arg1_type = ARG_CONST_MAP_PTR, | |
97 | * .arg2_type = ARG_PTR_TO_MAP_KEY, | |
98 | * | |
99 | * ret_type says that this function returns 'pointer to map elem value or null' | |
100 | * function expects 1st argument to be a const pointer to 'struct bpf_map' and | |
101 | * 2nd argument should be a pointer to stack, which will be used inside | |
102 | * the helper function as a pointer to map element key. | |
103 | * | |
104 | * On the kernel side the helper function looks like: | |
105 | * u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) | |
106 | * { | |
107 | * struct bpf_map *map = (struct bpf_map *) (unsigned long) r1; | |
108 | * void *key = (void *) (unsigned long) r2; | |
109 | * void *value; | |
110 | * | |
111 | * here kernel can access 'key' and 'map' pointers safely, knowing that | |
112 | * [key, key + map->key_size) bytes are valid and were initialized on | |
113 | * the stack of eBPF program. | |
114 | * } | |
115 | * | |
116 | * Corresponding eBPF program may look like: | |
117 | * BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), // after this insn R2 type is FRAME_PTR | |
118 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), // after this insn R2 type is PTR_TO_STACK | |
119 | * BPF_LD_MAP_FD(BPF_REG_1, map_fd), // after this insn R1 type is CONST_PTR_TO_MAP | |
120 | * BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem), | |
121 | * here verifier looks at prototype of map_lookup_elem() and sees: | |
122 | * .arg1_type == ARG_CONST_MAP_PTR and R1->type == CONST_PTR_TO_MAP, which is ok, | |
123 | * Now verifier knows that this map has key of R1->map_ptr->key_size bytes | |
124 | * | |
125 | * Then .arg2_type == ARG_PTR_TO_MAP_KEY and R2->type == PTR_TO_STACK, ok so far, | |
126 | * Now verifier checks that [R2, R2 + map's key_size) are within stack limits | |
127 | * and were initialized prior to this call. | |
128 | * If it's ok, then verifier allows this BPF_CALL insn and looks at | |
129 | * .ret_type which is RET_PTR_TO_MAP_VALUE_OR_NULL, so it sets | |
130 | * R0->type = PTR_TO_MAP_VALUE_OR_NULL which means bpf_map_lookup_elem() function | |
131 | * returns ether pointer to map value or NULL. | |
132 | * | |
133 | * When type PTR_TO_MAP_VALUE_OR_NULL passes through 'if (reg != 0) goto +off' | |
134 | * insn, the register holding that pointer in the true branch changes state to | |
135 | * PTR_TO_MAP_VALUE and the same register changes state to CONST_IMM in the false | |
136 | * branch. See check_cond_jmp_op(). | |
137 | * | |
138 | * After the call R0 is set to return type of the function and registers R1-R5 | |
139 | * are set to NOT_INIT to indicate that they are no longer readable. | |
140 | */ | |
141 | ||
17a52670 | 142 | /* verifier_state + insn_idx are pushed to stack when branch is encountered */ |
58e2af8b | 143 | struct bpf_verifier_stack_elem { |
17a52670 AS |
144 | /* verifer state is 'st' |
145 | * before processing instruction 'insn_idx' | |
146 | * and after processing instruction 'prev_insn_idx' | |
147 | */ | |
58e2af8b | 148 | struct bpf_verifier_state st; |
17a52670 AS |
149 | int insn_idx; |
150 | int prev_insn_idx; | |
58e2af8b | 151 | struct bpf_verifier_stack_elem *next; |
cbd35700 AS |
152 | }; |
153 | ||
8e17c1b1 | 154 | #define BPF_COMPLEXITY_LIMIT_INSNS 131072 |
07016151 DB |
155 | #define BPF_COMPLEXITY_LIMIT_STACK 1024 |
156 | ||
fad73a1a MKL |
157 | #define BPF_MAP_PTR_POISON ((void *)0xeB9F + POISON_POINTER_DELTA) |
158 | ||
33ff9823 DB |
159 | struct bpf_call_arg_meta { |
160 | struct bpf_map *map_ptr; | |
435faee1 | 161 | bool raw_mode; |
36bbef52 | 162 | bool pkt_access; |
435faee1 DB |
163 | int regno; |
164 | int access_size; | |
33ff9823 DB |
165 | }; |
166 | ||
cbd35700 AS |
167 | static DEFINE_MUTEX(bpf_verifier_lock); |
168 | ||
169 | /* log_level controls verbosity level of eBPF verifier. | |
170 | * verbose() is used to dump the verification trace to the log, so the user | |
171 | * can figure out what's wrong with the program | |
172 | */ | |
61bd5218 JK |
173 | static __printf(2, 3) void verbose(struct bpf_verifier_env *env, |
174 | const char *fmt, ...) | |
cbd35700 | 175 | { |
61bd5218 | 176 | struct bpf_verifer_log *log = &env->log; |
a2a7d570 | 177 | unsigned int n; |
cbd35700 AS |
178 | va_list args; |
179 | ||
a2a7d570 | 180 | if (!log->level || !log->ubuf || bpf_verifier_log_full(log)) |
cbd35700 AS |
181 | return; |
182 | ||
183 | va_start(args, fmt); | |
a2a7d570 | 184 | n = vscnprintf(log->kbuf, BPF_VERIFIER_TMP_LOG_SIZE, fmt, args); |
cbd35700 | 185 | va_end(args); |
a2a7d570 JK |
186 | |
187 | WARN_ONCE(n >= BPF_VERIFIER_TMP_LOG_SIZE - 1, | |
188 | "verifier log line truncated - local buffer too short\n"); | |
189 | ||
190 | n = min(log->len_total - log->len_used - 1, n); | |
191 | log->kbuf[n] = '\0'; | |
192 | ||
193 | if (!copy_to_user(log->ubuf + log->len_used, log->kbuf, n + 1)) | |
194 | log->len_used += n; | |
195 | else | |
196 | log->ubuf = NULL; | |
cbd35700 AS |
197 | } |
198 | ||
de8f3a83 DB |
199 | static bool type_is_pkt_pointer(enum bpf_reg_type type) |
200 | { | |
201 | return type == PTR_TO_PACKET || | |
202 | type == PTR_TO_PACKET_META; | |
203 | } | |
204 | ||
17a52670 AS |
205 | /* string representation of 'enum bpf_reg_type' */ |
206 | static const char * const reg_type_str[] = { | |
207 | [NOT_INIT] = "?", | |
f1174f77 | 208 | [SCALAR_VALUE] = "inv", |
17a52670 AS |
209 | [PTR_TO_CTX] = "ctx", |
210 | [CONST_PTR_TO_MAP] = "map_ptr", | |
211 | [PTR_TO_MAP_VALUE] = "map_value", | |
212 | [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null", | |
17a52670 | 213 | [PTR_TO_STACK] = "fp", |
969bf05e | 214 | [PTR_TO_PACKET] = "pkt", |
de8f3a83 | 215 | [PTR_TO_PACKET_META] = "pkt_meta", |
969bf05e | 216 | [PTR_TO_PACKET_END] = "pkt_end", |
17a52670 AS |
217 | }; |
218 | ||
4e92024a AS |
219 | static void print_liveness(struct bpf_verifier_env *env, |
220 | enum bpf_reg_liveness live) | |
221 | { | |
222 | if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN)) | |
223 | verbose(env, "_"); | |
224 | if (live & REG_LIVE_READ) | |
225 | verbose(env, "r"); | |
226 | if (live & REG_LIVE_WRITTEN) | |
227 | verbose(env, "w"); | |
228 | } | |
229 | ||
61bd5218 JK |
230 | static void print_verifier_state(struct bpf_verifier_env *env, |
231 | struct bpf_verifier_state *state) | |
17a52670 | 232 | { |
58e2af8b | 233 | struct bpf_reg_state *reg; |
17a52670 AS |
234 | enum bpf_reg_type t; |
235 | int i; | |
236 | ||
237 | for (i = 0; i < MAX_BPF_REG; i++) { | |
1a0dc1ac AS |
238 | reg = &state->regs[i]; |
239 | t = reg->type; | |
17a52670 AS |
240 | if (t == NOT_INIT) |
241 | continue; | |
4e92024a AS |
242 | verbose(env, " R%d", i); |
243 | print_liveness(env, reg->live); | |
244 | verbose(env, "=%s", reg_type_str[t]); | |
f1174f77 EC |
245 | if ((t == SCALAR_VALUE || t == PTR_TO_STACK) && |
246 | tnum_is_const(reg->var_off)) { | |
247 | /* reg->off should be 0 for SCALAR_VALUE */ | |
61bd5218 | 248 | verbose(env, "%lld", reg->var_off.value + reg->off); |
f1174f77 | 249 | } else { |
61bd5218 | 250 | verbose(env, "(id=%d", reg->id); |
f1174f77 | 251 | if (t != SCALAR_VALUE) |
61bd5218 | 252 | verbose(env, ",off=%d", reg->off); |
de8f3a83 | 253 | if (type_is_pkt_pointer(t)) |
61bd5218 | 254 | verbose(env, ",r=%d", reg->range); |
f1174f77 EC |
255 | else if (t == CONST_PTR_TO_MAP || |
256 | t == PTR_TO_MAP_VALUE || | |
257 | t == PTR_TO_MAP_VALUE_OR_NULL) | |
61bd5218 | 258 | verbose(env, ",ks=%d,vs=%d", |
f1174f77 EC |
259 | reg->map_ptr->key_size, |
260 | reg->map_ptr->value_size); | |
7d1238f2 EC |
261 | if (tnum_is_const(reg->var_off)) { |
262 | /* Typically an immediate SCALAR_VALUE, but | |
263 | * could be a pointer whose offset is too big | |
264 | * for reg->off | |
265 | */ | |
61bd5218 | 266 | verbose(env, ",imm=%llx", reg->var_off.value); |
7d1238f2 EC |
267 | } else { |
268 | if (reg->smin_value != reg->umin_value && | |
269 | reg->smin_value != S64_MIN) | |
61bd5218 | 270 | verbose(env, ",smin_value=%lld", |
7d1238f2 EC |
271 | (long long)reg->smin_value); |
272 | if (reg->smax_value != reg->umax_value && | |
273 | reg->smax_value != S64_MAX) | |
61bd5218 | 274 | verbose(env, ",smax_value=%lld", |
7d1238f2 EC |
275 | (long long)reg->smax_value); |
276 | if (reg->umin_value != 0) | |
61bd5218 | 277 | verbose(env, ",umin_value=%llu", |
7d1238f2 EC |
278 | (unsigned long long)reg->umin_value); |
279 | if (reg->umax_value != U64_MAX) | |
61bd5218 | 280 | verbose(env, ",umax_value=%llu", |
7d1238f2 EC |
281 | (unsigned long long)reg->umax_value); |
282 | if (!tnum_is_unknown(reg->var_off)) { | |
283 | char tn_buf[48]; | |
f1174f77 | 284 | |
7d1238f2 | 285 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
61bd5218 | 286 | verbose(env, ",var_off=%s", tn_buf); |
7d1238f2 | 287 | } |
f1174f77 | 288 | } |
61bd5218 | 289 | verbose(env, ")"); |
f1174f77 | 290 | } |
17a52670 | 291 | } |
638f5b90 | 292 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
4e92024a AS |
293 | if (state->stack[i].slot_type[0] == STACK_SPILL) { |
294 | verbose(env, " fp%d", | |
295 | (-i - 1) * BPF_REG_SIZE); | |
296 | print_liveness(env, state->stack[i].spilled_ptr.live); | |
297 | verbose(env, "=%s", | |
638f5b90 | 298 | reg_type_str[state->stack[i].spilled_ptr.type]); |
4e92024a | 299 | } |
17a52670 | 300 | } |
61bd5218 | 301 | verbose(env, "\n"); |
17a52670 AS |
302 | } |
303 | ||
638f5b90 AS |
304 | static int copy_stack_state(struct bpf_verifier_state *dst, |
305 | const struct bpf_verifier_state *src) | |
17a52670 | 306 | { |
638f5b90 AS |
307 | if (!src->stack) |
308 | return 0; | |
309 | if (WARN_ON_ONCE(dst->allocated_stack < src->allocated_stack)) { | |
310 | /* internal bug, make state invalid to reject the program */ | |
311 | memset(dst, 0, sizeof(*dst)); | |
312 | return -EFAULT; | |
313 | } | |
314 | memcpy(dst->stack, src->stack, | |
315 | sizeof(*src->stack) * (src->allocated_stack / BPF_REG_SIZE)); | |
316 | return 0; | |
317 | } | |
318 | ||
319 | /* do_check() starts with zero-sized stack in struct bpf_verifier_state to | |
320 | * make it consume minimal amount of memory. check_stack_write() access from | |
321 | * the program calls into realloc_verifier_state() to grow the stack size. | |
322 | * Note there is a non-zero 'parent' pointer inside bpf_verifier_state | |
323 | * which this function copies over. It points to previous bpf_verifier_state | |
324 | * which is never reallocated | |
325 | */ | |
326 | static int realloc_verifier_state(struct bpf_verifier_state *state, int size, | |
327 | bool copy_old) | |
328 | { | |
329 | u32 old_size = state->allocated_stack; | |
330 | struct bpf_stack_state *new_stack; | |
331 | int slot = size / BPF_REG_SIZE; | |
332 | ||
333 | if (size <= old_size || !size) { | |
334 | if (copy_old) | |
335 | return 0; | |
336 | state->allocated_stack = slot * BPF_REG_SIZE; | |
337 | if (!size && old_size) { | |
338 | kfree(state->stack); | |
339 | state->stack = NULL; | |
340 | } | |
341 | return 0; | |
342 | } | |
343 | new_stack = kmalloc_array(slot, sizeof(struct bpf_stack_state), | |
344 | GFP_KERNEL); | |
345 | if (!new_stack) | |
346 | return -ENOMEM; | |
347 | if (copy_old) { | |
348 | if (state->stack) | |
349 | memcpy(new_stack, state->stack, | |
350 | sizeof(*new_stack) * (old_size / BPF_REG_SIZE)); | |
351 | memset(new_stack + old_size / BPF_REG_SIZE, 0, | |
352 | sizeof(*new_stack) * (size - old_size) / BPF_REG_SIZE); | |
353 | } | |
354 | state->allocated_stack = slot * BPF_REG_SIZE; | |
355 | kfree(state->stack); | |
356 | state->stack = new_stack; | |
357 | return 0; | |
358 | } | |
359 | ||
1969db47 AS |
360 | static void free_verifier_state(struct bpf_verifier_state *state, |
361 | bool free_self) | |
638f5b90 AS |
362 | { |
363 | kfree(state->stack); | |
1969db47 AS |
364 | if (free_self) |
365 | kfree(state); | |
638f5b90 AS |
366 | } |
367 | ||
368 | /* copy verifier state from src to dst growing dst stack space | |
369 | * when necessary to accommodate larger src stack | |
370 | */ | |
371 | static int copy_verifier_state(struct bpf_verifier_state *dst, | |
372 | const struct bpf_verifier_state *src) | |
373 | { | |
374 | int err; | |
375 | ||
376 | err = realloc_verifier_state(dst, src->allocated_stack, false); | |
377 | if (err) | |
378 | return err; | |
379 | memcpy(dst, src, offsetof(struct bpf_verifier_state, allocated_stack)); | |
380 | return copy_stack_state(dst, src); | |
381 | } | |
382 | ||
383 | static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx, | |
384 | int *insn_idx) | |
385 | { | |
386 | struct bpf_verifier_state *cur = env->cur_state; | |
387 | struct bpf_verifier_stack_elem *elem, *head = env->head; | |
388 | int err; | |
17a52670 AS |
389 | |
390 | if (env->head == NULL) | |
638f5b90 | 391 | return -ENOENT; |
17a52670 | 392 | |
638f5b90 AS |
393 | if (cur) { |
394 | err = copy_verifier_state(cur, &head->st); | |
395 | if (err) | |
396 | return err; | |
397 | } | |
398 | if (insn_idx) | |
399 | *insn_idx = head->insn_idx; | |
17a52670 | 400 | if (prev_insn_idx) |
638f5b90 AS |
401 | *prev_insn_idx = head->prev_insn_idx; |
402 | elem = head->next; | |
1969db47 | 403 | free_verifier_state(&head->st, false); |
638f5b90 | 404 | kfree(head); |
17a52670 AS |
405 | env->head = elem; |
406 | env->stack_size--; | |
638f5b90 | 407 | return 0; |
17a52670 AS |
408 | } |
409 | ||
58e2af8b JK |
410 | static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, |
411 | int insn_idx, int prev_insn_idx) | |
17a52670 | 412 | { |
638f5b90 | 413 | struct bpf_verifier_state *cur = env->cur_state; |
58e2af8b | 414 | struct bpf_verifier_stack_elem *elem; |
638f5b90 | 415 | int err; |
17a52670 | 416 | |
638f5b90 | 417 | elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL); |
17a52670 AS |
418 | if (!elem) |
419 | goto err; | |
420 | ||
17a52670 AS |
421 | elem->insn_idx = insn_idx; |
422 | elem->prev_insn_idx = prev_insn_idx; | |
423 | elem->next = env->head; | |
424 | env->head = elem; | |
425 | env->stack_size++; | |
1969db47 AS |
426 | err = copy_verifier_state(&elem->st, cur); |
427 | if (err) | |
428 | goto err; | |
07016151 | 429 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_STACK) { |
61bd5218 | 430 | verbose(env, "BPF program is too complex\n"); |
17a52670 AS |
431 | goto err; |
432 | } | |
433 | return &elem->st; | |
434 | err: | |
435 | /* pop all elements and return */ | |
638f5b90 | 436 | while (!pop_stack(env, NULL, NULL)); |
17a52670 AS |
437 | return NULL; |
438 | } | |
439 | ||
440 | #define CALLER_SAVED_REGS 6 | |
441 | static const int caller_saved[CALLER_SAVED_REGS] = { | |
442 | BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5 | |
443 | }; | |
444 | ||
f1174f77 EC |
445 | static void __mark_reg_not_init(struct bpf_reg_state *reg); |
446 | ||
b03c9f9f EC |
447 | /* Mark the unknown part of a register (variable offset or scalar value) as |
448 | * known to have the value @imm. | |
449 | */ | |
450 | static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm) | |
451 | { | |
452 | reg->id = 0; | |
453 | reg->var_off = tnum_const(imm); | |
454 | reg->smin_value = (s64)imm; | |
455 | reg->smax_value = (s64)imm; | |
456 | reg->umin_value = imm; | |
457 | reg->umax_value = imm; | |
458 | } | |
459 | ||
f1174f77 EC |
460 | /* Mark the 'variable offset' part of a register as zero. This should be |
461 | * used only on registers holding a pointer type. | |
462 | */ | |
463 | static void __mark_reg_known_zero(struct bpf_reg_state *reg) | |
a9789ef9 | 464 | { |
b03c9f9f | 465 | __mark_reg_known(reg, 0); |
f1174f77 | 466 | } |
a9789ef9 | 467 | |
61bd5218 JK |
468 | static void mark_reg_known_zero(struct bpf_verifier_env *env, |
469 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
470 | { |
471 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 472 | verbose(env, "mark_reg_known_zero(regs, %u)\n", regno); |
f1174f77 EC |
473 | /* Something bad happened, let's kill all regs */ |
474 | for (regno = 0; regno < MAX_BPF_REG; regno++) | |
475 | __mark_reg_not_init(regs + regno); | |
476 | return; | |
477 | } | |
478 | __mark_reg_known_zero(regs + regno); | |
479 | } | |
480 | ||
de8f3a83 DB |
481 | static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg) |
482 | { | |
483 | return type_is_pkt_pointer(reg->type); | |
484 | } | |
485 | ||
486 | static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg) | |
487 | { | |
488 | return reg_is_pkt_pointer(reg) || | |
489 | reg->type == PTR_TO_PACKET_END; | |
490 | } | |
491 | ||
492 | /* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */ | |
493 | static bool reg_is_init_pkt_pointer(const struct bpf_reg_state *reg, | |
494 | enum bpf_reg_type which) | |
495 | { | |
496 | /* The register can already have a range from prior markings. | |
497 | * This is fine as long as it hasn't been advanced from its | |
498 | * origin. | |
499 | */ | |
500 | return reg->type == which && | |
501 | reg->id == 0 && | |
502 | reg->off == 0 && | |
503 | tnum_equals_const(reg->var_off, 0); | |
504 | } | |
505 | ||
b03c9f9f EC |
506 | /* Attempts to improve min/max values based on var_off information */ |
507 | static void __update_reg_bounds(struct bpf_reg_state *reg) | |
508 | { | |
509 | /* min signed is max(sign bit) | min(other bits) */ | |
510 | reg->smin_value = max_t(s64, reg->smin_value, | |
511 | reg->var_off.value | (reg->var_off.mask & S64_MIN)); | |
512 | /* max signed is min(sign bit) | max(other bits) */ | |
513 | reg->smax_value = min_t(s64, reg->smax_value, | |
514 | reg->var_off.value | (reg->var_off.mask & S64_MAX)); | |
515 | reg->umin_value = max(reg->umin_value, reg->var_off.value); | |
516 | reg->umax_value = min(reg->umax_value, | |
517 | reg->var_off.value | reg->var_off.mask); | |
518 | } | |
519 | ||
520 | /* Uses signed min/max values to inform unsigned, and vice-versa */ | |
521 | static void __reg_deduce_bounds(struct bpf_reg_state *reg) | |
522 | { | |
523 | /* Learn sign from signed bounds. | |
524 | * If we cannot cross the sign boundary, then signed and unsigned bounds | |
525 | * are the same, so combine. This works even in the negative case, e.g. | |
526 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. | |
527 | */ | |
528 | if (reg->smin_value >= 0 || reg->smax_value < 0) { | |
529 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, | |
530 | reg->umin_value); | |
531 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, | |
532 | reg->umax_value); | |
533 | return; | |
534 | } | |
535 | /* Learn sign from unsigned bounds. Signed bounds cross the sign | |
536 | * boundary, so we must be careful. | |
537 | */ | |
538 | if ((s64)reg->umax_value >= 0) { | |
539 | /* Positive. We can't learn anything from the smin, but smax | |
540 | * is positive, hence safe. | |
541 | */ | |
542 | reg->smin_value = reg->umin_value; | |
543 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, | |
544 | reg->umax_value); | |
545 | } else if ((s64)reg->umin_value < 0) { | |
546 | /* Negative. We can't learn anything from the smax, but smin | |
547 | * is negative, hence safe. | |
548 | */ | |
549 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, | |
550 | reg->umin_value); | |
551 | reg->smax_value = reg->umax_value; | |
552 | } | |
553 | } | |
554 | ||
555 | /* Attempts to improve var_off based on unsigned min/max information */ | |
556 | static void __reg_bound_offset(struct bpf_reg_state *reg) | |
557 | { | |
558 | reg->var_off = tnum_intersect(reg->var_off, | |
559 | tnum_range(reg->umin_value, | |
560 | reg->umax_value)); | |
561 | } | |
562 | ||
563 | /* Reset the min/max bounds of a register */ | |
564 | static void __mark_reg_unbounded(struct bpf_reg_state *reg) | |
565 | { | |
566 | reg->smin_value = S64_MIN; | |
567 | reg->smax_value = S64_MAX; | |
568 | reg->umin_value = 0; | |
569 | reg->umax_value = U64_MAX; | |
570 | } | |
571 | ||
f1174f77 EC |
572 | /* Mark a register as having a completely unknown (scalar) value. */ |
573 | static void __mark_reg_unknown(struct bpf_reg_state *reg) | |
574 | { | |
575 | reg->type = SCALAR_VALUE; | |
576 | reg->id = 0; | |
577 | reg->off = 0; | |
578 | reg->var_off = tnum_unknown; | |
b03c9f9f | 579 | __mark_reg_unbounded(reg); |
f1174f77 EC |
580 | } |
581 | ||
61bd5218 JK |
582 | static void mark_reg_unknown(struct bpf_verifier_env *env, |
583 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
584 | { |
585 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 586 | verbose(env, "mark_reg_unknown(regs, %u)\n", regno); |
f1174f77 EC |
587 | /* Something bad happened, let's kill all regs */ |
588 | for (regno = 0; regno < MAX_BPF_REG; regno++) | |
589 | __mark_reg_not_init(regs + regno); | |
590 | return; | |
591 | } | |
592 | __mark_reg_unknown(regs + regno); | |
593 | } | |
594 | ||
595 | static void __mark_reg_not_init(struct bpf_reg_state *reg) | |
596 | { | |
597 | __mark_reg_unknown(reg); | |
598 | reg->type = NOT_INIT; | |
599 | } | |
600 | ||
61bd5218 JK |
601 | static void mark_reg_not_init(struct bpf_verifier_env *env, |
602 | struct bpf_reg_state *regs, u32 regno) | |
f1174f77 EC |
603 | { |
604 | if (WARN_ON(regno >= MAX_BPF_REG)) { | |
61bd5218 | 605 | verbose(env, "mark_reg_not_init(regs, %u)\n", regno); |
f1174f77 EC |
606 | /* Something bad happened, let's kill all regs */ |
607 | for (regno = 0; regno < MAX_BPF_REG; regno++) | |
608 | __mark_reg_not_init(regs + regno); | |
609 | return; | |
610 | } | |
611 | __mark_reg_not_init(regs + regno); | |
a9789ef9 DB |
612 | } |
613 | ||
61bd5218 JK |
614 | static void init_reg_state(struct bpf_verifier_env *env, |
615 | struct bpf_reg_state *regs) | |
17a52670 AS |
616 | { |
617 | int i; | |
618 | ||
dc503a8a | 619 | for (i = 0; i < MAX_BPF_REG; i++) { |
61bd5218 | 620 | mark_reg_not_init(env, regs, i); |
dc503a8a EC |
621 | regs[i].live = REG_LIVE_NONE; |
622 | } | |
17a52670 AS |
623 | |
624 | /* frame pointer */ | |
f1174f77 | 625 | regs[BPF_REG_FP].type = PTR_TO_STACK; |
61bd5218 | 626 | mark_reg_known_zero(env, regs, BPF_REG_FP); |
17a52670 AS |
627 | |
628 | /* 1st arg to a function */ | |
629 | regs[BPF_REG_1].type = PTR_TO_CTX; | |
61bd5218 | 630 | mark_reg_known_zero(env, regs, BPF_REG_1); |
6760bf2d DB |
631 | } |
632 | ||
17a52670 AS |
633 | enum reg_arg_type { |
634 | SRC_OP, /* register is used as source operand */ | |
635 | DST_OP, /* register is used as destination operand */ | |
636 | DST_OP_NO_MARK /* same as above, check only, don't mark */ | |
637 | }; | |
638 | ||
dc503a8a EC |
639 | static void mark_reg_read(const struct bpf_verifier_state *state, u32 regno) |
640 | { | |
641 | struct bpf_verifier_state *parent = state->parent; | |
642 | ||
8fe2d6cc AS |
643 | if (regno == BPF_REG_FP) |
644 | /* We don't need to worry about FP liveness because it's read-only */ | |
645 | return; | |
646 | ||
dc503a8a EC |
647 | while (parent) { |
648 | /* if read wasn't screened by an earlier write ... */ | |
649 | if (state->regs[regno].live & REG_LIVE_WRITTEN) | |
650 | break; | |
651 | /* ... then we depend on parent's value */ | |
652 | parent->regs[regno].live |= REG_LIVE_READ; | |
653 | state = parent; | |
654 | parent = state->parent; | |
655 | } | |
656 | } | |
657 | ||
658 | static int check_reg_arg(struct bpf_verifier_env *env, u32 regno, | |
17a52670 AS |
659 | enum reg_arg_type t) |
660 | { | |
638f5b90 | 661 | struct bpf_reg_state *regs = env->cur_state->regs; |
dc503a8a | 662 | |
17a52670 | 663 | if (regno >= MAX_BPF_REG) { |
61bd5218 | 664 | verbose(env, "R%d is invalid\n", regno); |
17a52670 AS |
665 | return -EINVAL; |
666 | } | |
667 | ||
668 | if (t == SRC_OP) { | |
669 | /* check whether register used as source operand can be read */ | |
670 | if (regs[regno].type == NOT_INIT) { | |
61bd5218 | 671 | verbose(env, "R%d !read_ok\n", regno); |
17a52670 AS |
672 | return -EACCES; |
673 | } | |
638f5b90 | 674 | mark_reg_read(env->cur_state, regno); |
17a52670 AS |
675 | } else { |
676 | /* check whether register used as dest operand can be written to */ | |
677 | if (regno == BPF_REG_FP) { | |
61bd5218 | 678 | verbose(env, "frame pointer is read only\n"); |
17a52670 AS |
679 | return -EACCES; |
680 | } | |
dc503a8a | 681 | regs[regno].live |= REG_LIVE_WRITTEN; |
17a52670 | 682 | if (t == DST_OP) |
61bd5218 | 683 | mark_reg_unknown(env, regs, regno); |
17a52670 AS |
684 | } |
685 | return 0; | |
686 | } | |
687 | ||
1be7f75d AS |
688 | static bool is_spillable_regtype(enum bpf_reg_type type) |
689 | { | |
690 | switch (type) { | |
691 | case PTR_TO_MAP_VALUE: | |
692 | case PTR_TO_MAP_VALUE_OR_NULL: | |
693 | case PTR_TO_STACK: | |
694 | case PTR_TO_CTX: | |
969bf05e | 695 | case PTR_TO_PACKET: |
de8f3a83 | 696 | case PTR_TO_PACKET_META: |
969bf05e | 697 | case PTR_TO_PACKET_END: |
1be7f75d AS |
698 | case CONST_PTR_TO_MAP: |
699 | return true; | |
700 | default: | |
701 | return false; | |
702 | } | |
703 | } | |
704 | ||
17a52670 AS |
705 | /* check_stack_read/write functions track spill/fill of registers, |
706 | * stack boundary and alignment are checked in check_mem_access() | |
707 | */ | |
61bd5218 JK |
708 | static int check_stack_write(struct bpf_verifier_env *env, |
709 | struct bpf_verifier_state *state, int off, | |
58e2af8b | 710 | int size, int value_regno) |
17a52670 | 711 | { |
638f5b90 AS |
712 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err; |
713 | ||
714 | err = realloc_verifier_state(state, round_up(slot + 1, BPF_REG_SIZE), | |
715 | true); | |
716 | if (err) | |
717 | return err; | |
9c399760 AS |
718 | /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0, |
719 | * so it's aligned access and [off, off + size) are within stack limits | |
720 | */ | |
638f5b90 AS |
721 | if (!env->allow_ptr_leaks && |
722 | state->stack[spi].slot_type[0] == STACK_SPILL && | |
723 | size != BPF_REG_SIZE) { | |
724 | verbose(env, "attempt to corrupt spilled pointer on stack\n"); | |
725 | return -EACCES; | |
726 | } | |
17a52670 AS |
727 | |
728 | if (value_regno >= 0 && | |
1be7f75d | 729 | is_spillable_regtype(state->regs[value_regno].type)) { |
17a52670 AS |
730 | |
731 | /* register containing pointer is being spilled into stack */ | |
9c399760 | 732 | if (size != BPF_REG_SIZE) { |
61bd5218 | 733 | verbose(env, "invalid size of register spill\n"); |
17a52670 AS |
734 | return -EACCES; |
735 | } | |
736 | ||
17a52670 | 737 | /* save register state */ |
638f5b90 AS |
738 | state->stack[spi].spilled_ptr = state->regs[value_regno]; |
739 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; | |
17a52670 | 740 | |
9c399760 | 741 | for (i = 0; i < BPF_REG_SIZE; i++) |
638f5b90 | 742 | state->stack[spi].slot_type[i] = STACK_SPILL; |
9c399760 | 743 | } else { |
17a52670 | 744 | /* regular write of data into stack */ |
638f5b90 | 745 | state->stack[spi].spilled_ptr = (struct bpf_reg_state) {}; |
9c399760 AS |
746 | |
747 | for (i = 0; i < size; i++) | |
638f5b90 AS |
748 | state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] = |
749 | STACK_MISC; | |
17a52670 AS |
750 | } |
751 | return 0; | |
752 | } | |
753 | ||
dc503a8a EC |
754 | static void mark_stack_slot_read(const struct bpf_verifier_state *state, int slot) |
755 | { | |
756 | struct bpf_verifier_state *parent = state->parent; | |
757 | ||
758 | while (parent) { | |
759 | /* if read wasn't screened by an earlier write ... */ | |
638f5b90 | 760 | if (state->stack[slot].spilled_ptr.live & REG_LIVE_WRITTEN) |
dc503a8a EC |
761 | break; |
762 | /* ... then we depend on parent's value */ | |
638f5b90 | 763 | parent->stack[slot].spilled_ptr.live |= REG_LIVE_READ; |
dc503a8a EC |
764 | state = parent; |
765 | parent = state->parent; | |
766 | } | |
767 | } | |
768 | ||
61bd5218 JK |
769 | static int check_stack_read(struct bpf_verifier_env *env, |
770 | struct bpf_verifier_state *state, int off, int size, | |
17a52670 AS |
771 | int value_regno) |
772 | { | |
638f5b90 AS |
773 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE; |
774 | u8 *stype; | |
17a52670 | 775 | |
638f5b90 AS |
776 | if (state->allocated_stack <= slot) { |
777 | verbose(env, "invalid read from stack off %d+0 size %d\n", | |
778 | off, size); | |
779 | return -EACCES; | |
780 | } | |
781 | stype = state->stack[spi].slot_type; | |
17a52670 | 782 | |
638f5b90 | 783 | if (stype[0] == STACK_SPILL) { |
9c399760 | 784 | if (size != BPF_REG_SIZE) { |
61bd5218 | 785 | verbose(env, "invalid size of register spill\n"); |
17a52670 AS |
786 | return -EACCES; |
787 | } | |
9c399760 | 788 | for (i = 1; i < BPF_REG_SIZE; i++) { |
638f5b90 | 789 | if (stype[(slot - i) % BPF_REG_SIZE] != STACK_SPILL) { |
61bd5218 | 790 | verbose(env, "corrupted spill memory\n"); |
17a52670 AS |
791 | return -EACCES; |
792 | } | |
793 | } | |
794 | ||
dc503a8a | 795 | if (value_regno >= 0) { |
17a52670 | 796 | /* restore register state from stack */ |
638f5b90 | 797 | state->regs[value_regno] = state->stack[spi].spilled_ptr; |
dc503a8a EC |
798 | mark_stack_slot_read(state, spi); |
799 | } | |
17a52670 AS |
800 | return 0; |
801 | } else { | |
802 | for (i = 0; i < size; i++) { | |
638f5b90 | 803 | if (stype[(slot - i) % BPF_REG_SIZE] != STACK_MISC) { |
61bd5218 | 804 | verbose(env, "invalid read from stack off %d+%d size %d\n", |
17a52670 AS |
805 | off, i, size); |
806 | return -EACCES; | |
807 | } | |
808 | } | |
809 | if (value_regno >= 0) | |
810 | /* have read misc data from the stack */ | |
61bd5218 | 811 | mark_reg_unknown(env, state->regs, value_regno); |
17a52670 AS |
812 | return 0; |
813 | } | |
814 | } | |
815 | ||
816 | /* check read/write into map element returned by bpf_map_lookup_elem() */ | |
f1174f77 | 817 | static int __check_map_access(struct bpf_verifier_env *env, u32 regno, int off, |
9fd29c08 | 818 | int size, bool zero_size_allowed) |
17a52670 | 819 | { |
638f5b90 AS |
820 | struct bpf_reg_state *regs = cur_regs(env); |
821 | struct bpf_map *map = regs[regno].map_ptr; | |
17a52670 | 822 | |
9fd29c08 YS |
823 | if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) || |
824 | off + size > map->value_size) { | |
61bd5218 | 825 | verbose(env, "invalid access to map value, value_size=%d off=%d size=%d\n", |
17a52670 AS |
826 | map->value_size, off, size); |
827 | return -EACCES; | |
828 | } | |
829 | return 0; | |
830 | } | |
831 | ||
f1174f77 EC |
832 | /* check read/write into a map element with possible variable offset */ |
833 | static int check_map_access(struct bpf_verifier_env *env, u32 regno, | |
9fd29c08 | 834 | int off, int size, bool zero_size_allowed) |
dbcfe5f7 | 835 | { |
638f5b90 | 836 | struct bpf_verifier_state *state = env->cur_state; |
dbcfe5f7 GB |
837 | struct bpf_reg_state *reg = &state->regs[regno]; |
838 | int err; | |
839 | ||
f1174f77 EC |
840 | /* We may have adjusted the register to this map value, so we |
841 | * need to try adding each of min_value and max_value to off | |
842 | * to make sure our theoretical access will be safe. | |
dbcfe5f7 | 843 | */ |
61bd5218 JK |
844 | if (env->log.level) |
845 | print_verifier_state(env, state); | |
dbcfe5f7 GB |
846 | /* The minimum value is only important with signed |
847 | * comparisons where we can't assume the floor of a | |
848 | * value is 0. If we are using signed variables for our | |
849 | * index'es we need to make sure that whatever we use | |
850 | * will have a set floor within our range. | |
851 | */ | |
b03c9f9f | 852 | if (reg->smin_value < 0) { |
61bd5218 | 853 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
dbcfe5f7 GB |
854 | regno); |
855 | return -EACCES; | |
856 | } | |
9fd29c08 YS |
857 | err = __check_map_access(env, regno, reg->smin_value + off, size, |
858 | zero_size_allowed); | |
dbcfe5f7 | 859 | if (err) { |
61bd5218 JK |
860 | verbose(env, "R%d min value is outside of the array range\n", |
861 | regno); | |
dbcfe5f7 GB |
862 | return err; |
863 | } | |
864 | ||
b03c9f9f EC |
865 | /* If we haven't set a max value then we need to bail since we can't be |
866 | * sure we won't do bad things. | |
867 | * If reg->umax_value + off could overflow, treat that as unbounded too. | |
dbcfe5f7 | 868 | */ |
b03c9f9f | 869 | if (reg->umax_value >= BPF_MAX_VAR_OFF) { |
61bd5218 | 870 | verbose(env, "R%d unbounded memory access, make sure to bounds check any array access into a map\n", |
dbcfe5f7 GB |
871 | regno); |
872 | return -EACCES; | |
873 | } | |
9fd29c08 YS |
874 | err = __check_map_access(env, regno, reg->umax_value + off, size, |
875 | zero_size_allowed); | |
f1174f77 | 876 | if (err) |
61bd5218 JK |
877 | verbose(env, "R%d max value is outside of the array range\n", |
878 | regno); | |
f1174f77 | 879 | return err; |
dbcfe5f7 GB |
880 | } |
881 | ||
969bf05e AS |
882 | #define MAX_PACKET_OFF 0xffff |
883 | ||
58e2af8b | 884 | static bool may_access_direct_pkt_data(struct bpf_verifier_env *env, |
3a0af8fd TG |
885 | const struct bpf_call_arg_meta *meta, |
886 | enum bpf_access_type t) | |
4acf6c0b | 887 | { |
36bbef52 | 888 | switch (env->prog->type) { |
3a0af8fd TG |
889 | case BPF_PROG_TYPE_LWT_IN: |
890 | case BPF_PROG_TYPE_LWT_OUT: | |
891 | /* dst_input() and dst_output() can't write for now */ | |
892 | if (t == BPF_WRITE) | |
893 | return false; | |
7e57fbb2 | 894 | /* fallthrough */ |
36bbef52 DB |
895 | case BPF_PROG_TYPE_SCHED_CLS: |
896 | case BPF_PROG_TYPE_SCHED_ACT: | |
4acf6c0b | 897 | case BPF_PROG_TYPE_XDP: |
3a0af8fd | 898 | case BPF_PROG_TYPE_LWT_XMIT: |
8a31db56 | 899 | case BPF_PROG_TYPE_SK_SKB: |
36bbef52 DB |
900 | if (meta) |
901 | return meta->pkt_access; | |
902 | ||
903 | env->seen_direct_write = true; | |
4acf6c0b BB |
904 | return true; |
905 | default: | |
906 | return false; | |
907 | } | |
908 | } | |
909 | ||
f1174f77 | 910 | static int __check_packet_access(struct bpf_verifier_env *env, u32 regno, |
9fd29c08 | 911 | int off, int size, bool zero_size_allowed) |
969bf05e | 912 | { |
638f5b90 | 913 | struct bpf_reg_state *regs = cur_regs(env); |
58e2af8b | 914 | struct bpf_reg_state *reg = ®s[regno]; |
969bf05e | 915 | |
9fd29c08 YS |
916 | if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) || |
917 | (u64)off + size > reg->range) { | |
61bd5218 | 918 | verbose(env, "invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n", |
d91b28ed | 919 | off, size, regno, reg->id, reg->off, reg->range); |
969bf05e AS |
920 | return -EACCES; |
921 | } | |
922 | return 0; | |
923 | } | |
924 | ||
f1174f77 | 925 | static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off, |
9fd29c08 | 926 | int size, bool zero_size_allowed) |
f1174f77 | 927 | { |
638f5b90 | 928 | struct bpf_reg_state *regs = cur_regs(env); |
f1174f77 EC |
929 | struct bpf_reg_state *reg = ®s[regno]; |
930 | int err; | |
931 | ||
932 | /* We may have added a variable offset to the packet pointer; but any | |
933 | * reg->range we have comes after that. We are only checking the fixed | |
934 | * offset. | |
935 | */ | |
936 | ||
937 | /* We don't allow negative numbers, because we aren't tracking enough | |
938 | * detail to prove they're safe. | |
939 | */ | |
b03c9f9f | 940 | if (reg->smin_value < 0) { |
61bd5218 | 941 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
f1174f77 EC |
942 | regno); |
943 | return -EACCES; | |
944 | } | |
9fd29c08 | 945 | err = __check_packet_access(env, regno, off, size, zero_size_allowed); |
f1174f77 | 946 | if (err) { |
61bd5218 | 947 | verbose(env, "R%d offset is outside of the packet\n", regno); |
f1174f77 EC |
948 | return err; |
949 | } | |
950 | return err; | |
951 | } | |
952 | ||
953 | /* check access to 'struct bpf_context' fields. Supports fixed offsets only */ | |
31fd8581 | 954 | static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off, int size, |
19de99f7 | 955 | enum bpf_access_type t, enum bpf_reg_type *reg_type) |
17a52670 | 956 | { |
f96da094 DB |
957 | struct bpf_insn_access_aux info = { |
958 | .reg_type = *reg_type, | |
959 | }; | |
31fd8581 | 960 | |
4f9218aa JK |
961 | if (env->ops->is_valid_access && |
962 | env->ops->is_valid_access(off, size, t, &info)) { | |
f96da094 DB |
963 | /* A non zero info.ctx_field_size indicates that this field is a |
964 | * candidate for later verifier transformation to load the whole | |
965 | * field and then apply a mask when accessed with a narrower | |
966 | * access than actual ctx access size. A zero info.ctx_field_size | |
967 | * will only allow for whole field access and rejects any other | |
968 | * type of narrower access. | |
31fd8581 | 969 | */ |
23994631 | 970 | *reg_type = info.reg_type; |
31fd8581 | 971 | |
4f9218aa | 972 | env->insn_aux_data[insn_idx].ctx_field_size = info.ctx_field_size; |
32bbe007 AS |
973 | /* remember the offset of last byte accessed in ctx */ |
974 | if (env->prog->aux->max_ctx_offset < off + size) | |
975 | env->prog->aux->max_ctx_offset = off + size; | |
17a52670 | 976 | return 0; |
32bbe007 | 977 | } |
17a52670 | 978 | |
61bd5218 | 979 | verbose(env, "invalid bpf_context access off=%d size=%d\n", off, size); |
17a52670 AS |
980 | return -EACCES; |
981 | } | |
982 | ||
4cabc5b1 DB |
983 | static bool __is_pointer_value(bool allow_ptr_leaks, |
984 | const struct bpf_reg_state *reg) | |
1be7f75d | 985 | { |
4cabc5b1 | 986 | if (allow_ptr_leaks) |
1be7f75d AS |
987 | return false; |
988 | ||
f1174f77 | 989 | return reg->type != SCALAR_VALUE; |
1be7f75d AS |
990 | } |
991 | ||
4cabc5b1 DB |
992 | static bool is_pointer_value(struct bpf_verifier_env *env, int regno) |
993 | { | |
638f5b90 | 994 | return __is_pointer_value(env->allow_ptr_leaks, cur_regs(env) + regno); |
4cabc5b1 DB |
995 | } |
996 | ||
61bd5218 JK |
997 | static int check_pkt_ptr_alignment(struct bpf_verifier_env *env, |
998 | const struct bpf_reg_state *reg, | |
d1174416 | 999 | int off, int size, bool strict) |
969bf05e | 1000 | { |
f1174f77 | 1001 | struct tnum reg_off; |
e07b98d9 | 1002 | int ip_align; |
d1174416 DM |
1003 | |
1004 | /* Byte size accesses are always allowed. */ | |
1005 | if (!strict || size == 1) | |
1006 | return 0; | |
1007 | ||
e4eda884 DM |
1008 | /* For platforms that do not have a Kconfig enabling |
1009 | * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of | |
1010 | * NET_IP_ALIGN is universally set to '2'. And on platforms | |
1011 | * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get | |
1012 | * to this code only in strict mode where we want to emulate | |
1013 | * the NET_IP_ALIGN==2 checking. Therefore use an | |
1014 | * unconditional IP align value of '2'. | |
e07b98d9 | 1015 | */ |
e4eda884 | 1016 | ip_align = 2; |
f1174f77 EC |
1017 | |
1018 | reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off)); | |
1019 | if (!tnum_is_aligned(reg_off, size)) { | |
1020 | char tn_buf[48]; | |
1021 | ||
1022 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
61bd5218 JK |
1023 | verbose(env, |
1024 | "misaligned packet access off %d+%s+%d+%d size %d\n", | |
f1174f77 | 1025 | ip_align, tn_buf, reg->off, off, size); |
969bf05e AS |
1026 | return -EACCES; |
1027 | } | |
79adffcd | 1028 | |
969bf05e AS |
1029 | return 0; |
1030 | } | |
1031 | ||
61bd5218 JK |
1032 | static int check_generic_ptr_alignment(struct bpf_verifier_env *env, |
1033 | const struct bpf_reg_state *reg, | |
f1174f77 EC |
1034 | const char *pointer_desc, |
1035 | int off, int size, bool strict) | |
79adffcd | 1036 | { |
f1174f77 EC |
1037 | struct tnum reg_off; |
1038 | ||
1039 | /* Byte size accesses are always allowed. */ | |
1040 | if (!strict || size == 1) | |
1041 | return 0; | |
1042 | ||
1043 | reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off)); | |
1044 | if (!tnum_is_aligned(reg_off, size)) { | |
1045 | char tn_buf[48]; | |
1046 | ||
1047 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
61bd5218 | 1048 | verbose(env, "misaligned %saccess off %s+%d+%d size %d\n", |
f1174f77 | 1049 | pointer_desc, tn_buf, reg->off, off, size); |
79adffcd DB |
1050 | return -EACCES; |
1051 | } | |
1052 | ||
969bf05e AS |
1053 | return 0; |
1054 | } | |
1055 | ||
e07b98d9 DM |
1056 | static int check_ptr_alignment(struct bpf_verifier_env *env, |
1057 | const struct bpf_reg_state *reg, | |
79adffcd DB |
1058 | int off, int size) |
1059 | { | |
e07b98d9 | 1060 | bool strict = env->strict_alignment; |
f1174f77 | 1061 | const char *pointer_desc = ""; |
d1174416 | 1062 | |
79adffcd DB |
1063 | switch (reg->type) { |
1064 | case PTR_TO_PACKET: | |
de8f3a83 DB |
1065 | case PTR_TO_PACKET_META: |
1066 | /* Special case, because of NET_IP_ALIGN. Given metadata sits | |
1067 | * right in front, treat it the very same way. | |
1068 | */ | |
61bd5218 | 1069 | return check_pkt_ptr_alignment(env, reg, off, size, strict); |
f1174f77 EC |
1070 | case PTR_TO_MAP_VALUE: |
1071 | pointer_desc = "value "; | |
1072 | break; | |
1073 | case PTR_TO_CTX: | |
1074 | pointer_desc = "context "; | |
1075 | break; | |
1076 | case PTR_TO_STACK: | |
1077 | pointer_desc = "stack "; | |
1078 | break; | |
79adffcd | 1079 | default: |
f1174f77 | 1080 | break; |
79adffcd | 1081 | } |
61bd5218 JK |
1082 | return check_generic_ptr_alignment(env, reg, pointer_desc, off, size, |
1083 | strict); | |
79adffcd DB |
1084 | } |
1085 | ||
17a52670 AS |
1086 | /* check whether memory at (regno + off) is accessible for t = (read | write) |
1087 | * if t==write, value_regno is a register which value is stored into memory | |
1088 | * if t==read, value_regno is a register which will receive the value from memory | |
1089 | * if t==write && value_regno==-1, some unknown value is stored into memory | |
1090 | * if t==read && value_regno==-1, don't care what we read from memory | |
1091 | */ | |
31fd8581 | 1092 | static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno, int off, |
17a52670 AS |
1093 | int bpf_size, enum bpf_access_type t, |
1094 | int value_regno) | |
1095 | { | |
638f5b90 AS |
1096 | struct bpf_verifier_state *state = env->cur_state; |
1097 | struct bpf_reg_state *regs = cur_regs(env); | |
1098 | struct bpf_reg_state *reg = regs + regno; | |
17a52670 AS |
1099 | int size, err = 0; |
1100 | ||
1101 | size = bpf_size_to_bytes(bpf_size); | |
1102 | if (size < 0) | |
1103 | return size; | |
1104 | ||
f1174f77 | 1105 | /* alignment checks will add in reg->off themselves */ |
e07b98d9 | 1106 | err = check_ptr_alignment(env, reg, off, size); |
969bf05e AS |
1107 | if (err) |
1108 | return err; | |
17a52670 | 1109 | |
f1174f77 EC |
1110 | /* for access checks, reg->off is just part of off */ |
1111 | off += reg->off; | |
1112 | ||
1113 | if (reg->type == PTR_TO_MAP_VALUE) { | |
1be7f75d AS |
1114 | if (t == BPF_WRITE && value_regno >= 0 && |
1115 | is_pointer_value(env, value_regno)) { | |
61bd5218 | 1116 | verbose(env, "R%d leaks addr into map\n", value_regno); |
1be7f75d AS |
1117 | return -EACCES; |
1118 | } | |
48461135 | 1119 | |
9fd29c08 | 1120 | err = check_map_access(env, regno, off, size, false); |
17a52670 | 1121 | if (!err && t == BPF_READ && value_regno >= 0) |
638f5b90 | 1122 | mark_reg_unknown(env, regs, value_regno); |
17a52670 | 1123 | |
1a0dc1ac | 1124 | } else if (reg->type == PTR_TO_CTX) { |
f1174f77 | 1125 | enum bpf_reg_type reg_type = SCALAR_VALUE; |
19de99f7 | 1126 | |
1be7f75d AS |
1127 | if (t == BPF_WRITE && value_regno >= 0 && |
1128 | is_pointer_value(env, value_regno)) { | |
61bd5218 | 1129 | verbose(env, "R%d leaks addr into ctx\n", value_regno); |
1be7f75d AS |
1130 | return -EACCES; |
1131 | } | |
f1174f77 EC |
1132 | /* ctx accesses must be at a fixed offset, so that we can |
1133 | * determine what type of data were returned. | |
1134 | */ | |
28e33f9d | 1135 | if (reg->off) { |
f8ddadc4 DM |
1136 | verbose(env, |
1137 | "dereference of modified ctx ptr R%d off=%d+%d, ctx+const is allowed, ctx+const+const is not\n", | |
28e33f9d JK |
1138 | regno, reg->off, off - reg->off); |
1139 | return -EACCES; | |
1140 | } | |
1141 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { | |
f1174f77 EC |
1142 | char tn_buf[48]; |
1143 | ||
1144 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
61bd5218 JK |
1145 | verbose(env, |
1146 | "variable ctx access var_off=%s off=%d size=%d", | |
f1174f77 EC |
1147 | tn_buf, off, size); |
1148 | return -EACCES; | |
1149 | } | |
31fd8581 | 1150 | err = check_ctx_access(env, insn_idx, off, size, t, ®_type); |
969bf05e | 1151 | if (!err && t == BPF_READ && value_regno >= 0) { |
f1174f77 | 1152 | /* ctx access returns either a scalar, or a |
de8f3a83 DB |
1153 | * PTR_TO_PACKET[_META,_END]. In the latter |
1154 | * case, we know the offset is zero. | |
f1174f77 EC |
1155 | */ |
1156 | if (reg_type == SCALAR_VALUE) | |
638f5b90 | 1157 | mark_reg_unknown(env, regs, value_regno); |
f1174f77 | 1158 | else |
638f5b90 | 1159 | mark_reg_known_zero(env, regs, |
61bd5218 | 1160 | value_regno); |
638f5b90 AS |
1161 | regs[value_regno].id = 0; |
1162 | regs[value_regno].off = 0; | |
1163 | regs[value_regno].range = 0; | |
1164 | regs[value_regno].type = reg_type; | |
969bf05e | 1165 | } |
17a52670 | 1166 | |
f1174f77 EC |
1167 | } else if (reg->type == PTR_TO_STACK) { |
1168 | /* stack accesses must be at a fixed offset, so that we can | |
1169 | * determine what type of data were returned. | |
1170 | * See check_stack_read(). | |
1171 | */ | |
1172 | if (!tnum_is_const(reg->var_off)) { | |
1173 | char tn_buf[48]; | |
1174 | ||
1175 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
61bd5218 | 1176 | verbose(env, "variable stack access var_off=%s off=%d size=%d", |
f1174f77 EC |
1177 | tn_buf, off, size); |
1178 | return -EACCES; | |
1179 | } | |
1180 | off += reg->var_off.value; | |
17a52670 | 1181 | if (off >= 0 || off < -MAX_BPF_STACK) { |
61bd5218 JK |
1182 | verbose(env, "invalid stack off=%d size=%d\n", off, |
1183 | size); | |
17a52670 AS |
1184 | return -EACCES; |
1185 | } | |
8726679a AS |
1186 | |
1187 | if (env->prog->aux->stack_depth < -off) | |
1188 | env->prog->aux->stack_depth = -off; | |
1189 | ||
638f5b90 | 1190 | if (t == BPF_WRITE) |
61bd5218 JK |
1191 | err = check_stack_write(env, state, off, size, |
1192 | value_regno); | |
638f5b90 | 1193 | else |
61bd5218 JK |
1194 | err = check_stack_read(env, state, off, size, |
1195 | value_regno); | |
de8f3a83 | 1196 | } else if (reg_is_pkt_pointer(reg)) { |
3a0af8fd | 1197 | if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) { |
61bd5218 | 1198 | verbose(env, "cannot write into packet\n"); |
969bf05e AS |
1199 | return -EACCES; |
1200 | } | |
4acf6c0b BB |
1201 | if (t == BPF_WRITE && value_regno >= 0 && |
1202 | is_pointer_value(env, value_regno)) { | |
61bd5218 JK |
1203 | verbose(env, "R%d leaks addr into packet\n", |
1204 | value_regno); | |
4acf6c0b BB |
1205 | return -EACCES; |
1206 | } | |
9fd29c08 | 1207 | err = check_packet_access(env, regno, off, size, false); |
969bf05e | 1208 | if (!err && t == BPF_READ && value_regno >= 0) |
638f5b90 | 1209 | mark_reg_unknown(env, regs, value_regno); |
17a52670 | 1210 | } else { |
61bd5218 JK |
1211 | verbose(env, "R%d invalid mem access '%s'\n", regno, |
1212 | reg_type_str[reg->type]); | |
17a52670 AS |
1213 | return -EACCES; |
1214 | } | |
969bf05e | 1215 | |
f1174f77 | 1216 | if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ && |
638f5b90 | 1217 | regs[value_regno].type == SCALAR_VALUE) { |
f1174f77 | 1218 | /* b/h/w load zero-extends, mark upper bits as known 0 */ |
638f5b90 AS |
1219 | regs[value_regno].var_off = |
1220 | tnum_cast(regs[value_regno].var_off, size); | |
1221 | __update_reg_bounds(®s[value_regno]); | |
969bf05e | 1222 | } |
17a52670 AS |
1223 | return err; |
1224 | } | |
1225 | ||
31fd8581 | 1226 | static int check_xadd(struct bpf_verifier_env *env, int insn_idx, struct bpf_insn *insn) |
17a52670 | 1227 | { |
17a52670 AS |
1228 | int err; |
1229 | ||
1230 | if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) || | |
1231 | insn->imm != 0) { | |
61bd5218 | 1232 | verbose(env, "BPF_XADD uses reserved fields\n"); |
17a52670 AS |
1233 | return -EINVAL; |
1234 | } | |
1235 | ||
1236 | /* check src1 operand */ | |
dc503a8a | 1237 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
1238 | if (err) |
1239 | return err; | |
1240 | ||
1241 | /* check src2 operand */ | |
dc503a8a | 1242 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
1243 | if (err) |
1244 | return err; | |
1245 | ||
6bdf6abc | 1246 | if (is_pointer_value(env, insn->src_reg)) { |
61bd5218 | 1247 | verbose(env, "R%d leaks addr into mem\n", insn->src_reg); |
6bdf6abc DB |
1248 | return -EACCES; |
1249 | } | |
1250 | ||
17a52670 | 1251 | /* check whether atomic_add can read the memory */ |
31fd8581 | 1252 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
17a52670 AS |
1253 | BPF_SIZE(insn->code), BPF_READ, -1); |
1254 | if (err) | |
1255 | return err; | |
1256 | ||
1257 | /* check whether atomic_add can write into the same memory */ | |
31fd8581 | 1258 | return check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
17a52670 AS |
1259 | BPF_SIZE(insn->code), BPF_WRITE, -1); |
1260 | } | |
1261 | ||
f1174f77 EC |
1262 | /* Does this register contain a constant zero? */ |
1263 | static bool register_is_null(struct bpf_reg_state reg) | |
1264 | { | |
1265 | return reg.type == SCALAR_VALUE && tnum_equals_const(reg.var_off, 0); | |
1266 | } | |
1267 | ||
17a52670 AS |
1268 | /* when register 'regno' is passed into function that will read 'access_size' |
1269 | * bytes from that pointer, make sure that it's within stack boundary | |
f1174f77 EC |
1270 | * and all elements of stack are initialized. |
1271 | * Unlike most pointer bounds-checking functions, this one doesn't take an | |
1272 | * 'off' argument, so it has to add in reg->off itself. | |
17a52670 | 1273 | */ |
58e2af8b | 1274 | static int check_stack_boundary(struct bpf_verifier_env *env, int regno, |
435faee1 DB |
1275 | int access_size, bool zero_size_allowed, |
1276 | struct bpf_call_arg_meta *meta) | |
17a52670 | 1277 | { |
638f5b90 | 1278 | struct bpf_verifier_state *state = env->cur_state; |
58e2af8b | 1279 | struct bpf_reg_state *regs = state->regs; |
638f5b90 | 1280 | int off, i, slot, spi; |
17a52670 | 1281 | |
8e2fe1d9 | 1282 | if (regs[regno].type != PTR_TO_STACK) { |
f1174f77 | 1283 | /* Allow zero-byte read from NULL, regardless of pointer type */ |
8e2fe1d9 | 1284 | if (zero_size_allowed && access_size == 0 && |
f1174f77 | 1285 | register_is_null(regs[regno])) |
8e2fe1d9 DB |
1286 | return 0; |
1287 | ||
61bd5218 | 1288 | verbose(env, "R%d type=%s expected=%s\n", regno, |
8e2fe1d9 DB |
1289 | reg_type_str[regs[regno].type], |
1290 | reg_type_str[PTR_TO_STACK]); | |
17a52670 | 1291 | return -EACCES; |
8e2fe1d9 | 1292 | } |
17a52670 | 1293 | |
f1174f77 EC |
1294 | /* Only allow fixed-offset stack reads */ |
1295 | if (!tnum_is_const(regs[regno].var_off)) { | |
1296 | char tn_buf[48]; | |
1297 | ||
1298 | tnum_strn(tn_buf, sizeof(tn_buf), regs[regno].var_off); | |
61bd5218 | 1299 | verbose(env, "invalid variable stack read R%d var_off=%s\n", |
f1174f77 EC |
1300 | regno, tn_buf); |
1301 | } | |
1302 | off = regs[regno].off + regs[regno].var_off.value; | |
17a52670 | 1303 | if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 || |
9fd29c08 | 1304 | access_size < 0 || (access_size == 0 && !zero_size_allowed)) { |
61bd5218 | 1305 | verbose(env, "invalid stack type R%d off=%d access_size=%d\n", |
17a52670 AS |
1306 | regno, off, access_size); |
1307 | return -EACCES; | |
1308 | } | |
1309 | ||
8726679a AS |
1310 | if (env->prog->aux->stack_depth < -off) |
1311 | env->prog->aux->stack_depth = -off; | |
1312 | ||
435faee1 DB |
1313 | if (meta && meta->raw_mode) { |
1314 | meta->access_size = access_size; | |
1315 | meta->regno = regno; | |
1316 | return 0; | |
1317 | } | |
1318 | ||
17a52670 | 1319 | for (i = 0; i < access_size; i++) { |
638f5b90 AS |
1320 | slot = -(off + i) - 1; |
1321 | spi = slot / BPF_REG_SIZE; | |
1322 | if (state->allocated_stack <= slot || | |
1323 | state->stack[spi].slot_type[slot % BPF_REG_SIZE] != | |
1324 | STACK_MISC) { | |
61bd5218 | 1325 | verbose(env, "invalid indirect read from stack off %d+%d size %d\n", |
17a52670 AS |
1326 | off, i, access_size); |
1327 | return -EACCES; | |
1328 | } | |
1329 | } | |
1330 | return 0; | |
1331 | } | |
1332 | ||
06c1c049 GB |
1333 | static int check_helper_mem_access(struct bpf_verifier_env *env, int regno, |
1334 | int access_size, bool zero_size_allowed, | |
1335 | struct bpf_call_arg_meta *meta) | |
1336 | { | |
638f5b90 | 1337 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
06c1c049 | 1338 | |
f1174f77 | 1339 | switch (reg->type) { |
06c1c049 | 1340 | case PTR_TO_PACKET: |
de8f3a83 | 1341 | case PTR_TO_PACKET_META: |
9fd29c08 YS |
1342 | return check_packet_access(env, regno, reg->off, access_size, |
1343 | zero_size_allowed); | |
06c1c049 | 1344 | case PTR_TO_MAP_VALUE: |
9fd29c08 YS |
1345 | return check_map_access(env, regno, reg->off, access_size, |
1346 | zero_size_allowed); | |
f1174f77 | 1347 | default: /* scalar_value|ptr_to_stack or invalid ptr */ |
06c1c049 GB |
1348 | return check_stack_boundary(env, regno, access_size, |
1349 | zero_size_allowed, meta); | |
1350 | } | |
1351 | } | |
1352 | ||
58e2af8b | 1353 | static int check_func_arg(struct bpf_verifier_env *env, u32 regno, |
33ff9823 DB |
1354 | enum bpf_arg_type arg_type, |
1355 | struct bpf_call_arg_meta *meta) | |
17a52670 | 1356 | { |
638f5b90 | 1357 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
6841de8b | 1358 | enum bpf_reg_type expected_type, type = reg->type; |
17a52670 AS |
1359 | int err = 0; |
1360 | ||
80f1d68c | 1361 | if (arg_type == ARG_DONTCARE) |
17a52670 AS |
1362 | return 0; |
1363 | ||
dc503a8a EC |
1364 | err = check_reg_arg(env, regno, SRC_OP); |
1365 | if (err) | |
1366 | return err; | |
17a52670 | 1367 | |
1be7f75d AS |
1368 | if (arg_type == ARG_ANYTHING) { |
1369 | if (is_pointer_value(env, regno)) { | |
61bd5218 JK |
1370 | verbose(env, "R%d leaks addr into helper function\n", |
1371 | regno); | |
1be7f75d AS |
1372 | return -EACCES; |
1373 | } | |
80f1d68c | 1374 | return 0; |
1be7f75d | 1375 | } |
80f1d68c | 1376 | |
de8f3a83 | 1377 | if (type_is_pkt_pointer(type) && |
3a0af8fd | 1378 | !may_access_direct_pkt_data(env, meta, BPF_READ)) { |
61bd5218 | 1379 | verbose(env, "helper access to the packet is not allowed\n"); |
6841de8b AS |
1380 | return -EACCES; |
1381 | } | |
1382 | ||
8e2fe1d9 | 1383 | if (arg_type == ARG_PTR_TO_MAP_KEY || |
17a52670 AS |
1384 | arg_type == ARG_PTR_TO_MAP_VALUE) { |
1385 | expected_type = PTR_TO_STACK; | |
de8f3a83 DB |
1386 | if (!type_is_pkt_pointer(type) && |
1387 | type != expected_type) | |
6841de8b | 1388 | goto err_type; |
39f19ebb AS |
1389 | } else if (arg_type == ARG_CONST_SIZE || |
1390 | arg_type == ARG_CONST_SIZE_OR_ZERO) { | |
f1174f77 EC |
1391 | expected_type = SCALAR_VALUE; |
1392 | if (type != expected_type) | |
6841de8b | 1393 | goto err_type; |
17a52670 AS |
1394 | } else if (arg_type == ARG_CONST_MAP_PTR) { |
1395 | expected_type = CONST_PTR_TO_MAP; | |
6841de8b AS |
1396 | if (type != expected_type) |
1397 | goto err_type; | |
608cd71a AS |
1398 | } else if (arg_type == ARG_PTR_TO_CTX) { |
1399 | expected_type = PTR_TO_CTX; | |
6841de8b AS |
1400 | if (type != expected_type) |
1401 | goto err_type; | |
39f19ebb | 1402 | } else if (arg_type == ARG_PTR_TO_MEM || |
db1ac496 | 1403 | arg_type == ARG_PTR_TO_MEM_OR_NULL || |
39f19ebb | 1404 | arg_type == ARG_PTR_TO_UNINIT_MEM) { |
8e2fe1d9 DB |
1405 | expected_type = PTR_TO_STACK; |
1406 | /* One exception here. In case function allows for NULL to be | |
f1174f77 | 1407 | * passed in as argument, it's a SCALAR_VALUE type. Final test |
8e2fe1d9 DB |
1408 | * happens during stack boundary checking. |
1409 | */ | |
db1ac496 GB |
1410 | if (register_is_null(*reg) && |
1411 | arg_type == ARG_PTR_TO_MEM_OR_NULL) | |
6841de8b | 1412 | /* final test in check_stack_boundary() */; |
de8f3a83 DB |
1413 | else if (!type_is_pkt_pointer(type) && |
1414 | type != PTR_TO_MAP_VALUE && | |
f1174f77 | 1415 | type != expected_type) |
6841de8b | 1416 | goto err_type; |
39f19ebb | 1417 | meta->raw_mode = arg_type == ARG_PTR_TO_UNINIT_MEM; |
17a52670 | 1418 | } else { |
61bd5218 | 1419 | verbose(env, "unsupported arg_type %d\n", arg_type); |
17a52670 AS |
1420 | return -EFAULT; |
1421 | } | |
1422 | ||
17a52670 AS |
1423 | if (arg_type == ARG_CONST_MAP_PTR) { |
1424 | /* bpf_map_xxx(map_ptr) call: remember that map_ptr */ | |
33ff9823 | 1425 | meta->map_ptr = reg->map_ptr; |
17a52670 AS |
1426 | } else if (arg_type == ARG_PTR_TO_MAP_KEY) { |
1427 | /* bpf_map_xxx(..., map_ptr, ..., key) call: | |
1428 | * check that [key, key + map->key_size) are within | |
1429 | * stack limits and initialized | |
1430 | */ | |
33ff9823 | 1431 | if (!meta->map_ptr) { |
17a52670 AS |
1432 | /* in function declaration map_ptr must come before |
1433 | * map_key, so that it's verified and known before | |
1434 | * we have to check map_key here. Otherwise it means | |
1435 | * that kernel subsystem misconfigured verifier | |
1436 | */ | |
61bd5218 | 1437 | verbose(env, "invalid map_ptr to access map->key\n"); |
17a52670 AS |
1438 | return -EACCES; |
1439 | } | |
de8f3a83 | 1440 | if (type_is_pkt_pointer(type)) |
f1174f77 | 1441 | err = check_packet_access(env, regno, reg->off, |
9fd29c08 YS |
1442 | meta->map_ptr->key_size, |
1443 | false); | |
6841de8b AS |
1444 | else |
1445 | err = check_stack_boundary(env, regno, | |
1446 | meta->map_ptr->key_size, | |
1447 | false, NULL); | |
17a52670 AS |
1448 | } else if (arg_type == ARG_PTR_TO_MAP_VALUE) { |
1449 | /* bpf_map_xxx(..., map_ptr, ..., value) call: | |
1450 | * check [value, value + map->value_size) validity | |
1451 | */ | |
33ff9823 | 1452 | if (!meta->map_ptr) { |
17a52670 | 1453 | /* kernel subsystem misconfigured verifier */ |
61bd5218 | 1454 | verbose(env, "invalid map_ptr to access map->value\n"); |
17a52670 AS |
1455 | return -EACCES; |
1456 | } | |
de8f3a83 | 1457 | if (type_is_pkt_pointer(type)) |
f1174f77 | 1458 | err = check_packet_access(env, regno, reg->off, |
9fd29c08 YS |
1459 | meta->map_ptr->value_size, |
1460 | false); | |
6841de8b AS |
1461 | else |
1462 | err = check_stack_boundary(env, regno, | |
1463 | meta->map_ptr->value_size, | |
1464 | false, NULL); | |
39f19ebb AS |
1465 | } else if (arg_type == ARG_CONST_SIZE || |
1466 | arg_type == ARG_CONST_SIZE_OR_ZERO) { | |
1467 | bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO); | |
17a52670 | 1468 | |
17a52670 AS |
1469 | /* bpf_xxx(..., buf, len) call will access 'len' bytes |
1470 | * from stack pointer 'buf'. Check it | |
1471 | * note: regno == len, regno - 1 == buf | |
1472 | */ | |
1473 | if (regno == 0) { | |
1474 | /* kernel subsystem misconfigured verifier */ | |
61bd5218 JK |
1475 | verbose(env, |
1476 | "ARG_CONST_SIZE cannot be first argument\n"); | |
17a52670 AS |
1477 | return -EACCES; |
1478 | } | |
06c1c049 | 1479 | |
f1174f77 EC |
1480 | /* The register is SCALAR_VALUE; the access check |
1481 | * happens using its boundaries. | |
06c1c049 | 1482 | */ |
f1174f77 EC |
1483 | |
1484 | if (!tnum_is_const(reg->var_off)) | |
06c1c049 GB |
1485 | /* For unprivileged variable accesses, disable raw |
1486 | * mode so that the program is required to | |
1487 | * initialize all the memory that the helper could | |
1488 | * just partially fill up. | |
1489 | */ | |
1490 | meta = NULL; | |
1491 | ||
b03c9f9f | 1492 | if (reg->smin_value < 0) { |
61bd5218 | 1493 | verbose(env, "R%d min value is negative, either use unsigned or 'var &= const'\n", |
f1174f77 EC |
1494 | regno); |
1495 | return -EACCES; | |
1496 | } | |
06c1c049 | 1497 | |
b03c9f9f | 1498 | if (reg->umin_value == 0) { |
f1174f77 EC |
1499 | err = check_helper_mem_access(env, regno - 1, 0, |
1500 | zero_size_allowed, | |
1501 | meta); | |
06c1c049 GB |
1502 | if (err) |
1503 | return err; | |
06c1c049 | 1504 | } |
f1174f77 | 1505 | |
b03c9f9f | 1506 | if (reg->umax_value >= BPF_MAX_VAR_SIZ) { |
61bd5218 | 1507 | verbose(env, "R%d unbounded memory access, use 'var &= const' or 'if (var < const)'\n", |
f1174f77 EC |
1508 | regno); |
1509 | return -EACCES; | |
1510 | } | |
1511 | err = check_helper_mem_access(env, regno - 1, | |
b03c9f9f | 1512 | reg->umax_value, |
f1174f77 | 1513 | zero_size_allowed, meta); |
17a52670 AS |
1514 | } |
1515 | ||
1516 | return err; | |
6841de8b | 1517 | err_type: |
61bd5218 | 1518 | verbose(env, "R%d type=%s expected=%s\n", regno, |
6841de8b AS |
1519 | reg_type_str[type], reg_type_str[expected_type]); |
1520 | return -EACCES; | |
17a52670 AS |
1521 | } |
1522 | ||
61bd5218 JK |
1523 | static int check_map_func_compatibility(struct bpf_verifier_env *env, |
1524 | struct bpf_map *map, int func_id) | |
35578d79 | 1525 | { |
35578d79 KX |
1526 | if (!map) |
1527 | return 0; | |
1528 | ||
6aff67c8 AS |
1529 | /* We need a two way check, first is from map perspective ... */ |
1530 | switch (map->map_type) { | |
1531 | case BPF_MAP_TYPE_PROG_ARRAY: | |
1532 | if (func_id != BPF_FUNC_tail_call) | |
1533 | goto error; | |
1534 | break; | |
1535 | case BPF_MAP_TYPE_PERF_EVENT_ARRAY: | |
1536 | if (func_id != BPF_FUNC_perf_event_read && | |
908432ca YS |
1537 | func_id != BPF_FUNC_perf_event_output && |
1538 | func_id != BPF_FUNC_perf_event_read_value) | |
6aff67c8 AS |
1539 | goto error; |
1540 | break; | |
1541 | case BPF_MAP_TYPE_STACK_TRACE: | |
1542 | if (func_id != BPF_FUNC_get_stackid) | |
1543 | goto error; | |
1544 | break; | |
4ed8ec52 | 1545 | case BPF_MAP_TYPE_CGROUP_ARRAY: |
60747ef4 | 1546 | if (func_id != BPF_FUNC_skb_under_cgroup && |
60d20f91 | 1547 | func_id != BPF_FUNC_current_task_under_cgroup) |
4a482f34 MKL |
1548 | goto error; |
1549 | break; | |
546ac1ff JF |
1550 | /* devmap returns a pointer to a live net_device ifindex that we cannot |
1551 | * allow to be modified from bpf side. So do not allow lookup elements | |
1552 | * for now. | |
1553 | */ | |
1554 | case BPF_MAP_TYPE_DEVMAP: | |
2ddf71e2 | 1555 | if (func_id != BPF_FUNC_redirect_map) |
546ac1ff JF |
1556 | goto error; |
1557 | break; | |
6710e112 JDB |
1558 | /* Restrict bpf side of cpumap, open when use-cases appear */ |
1559 | case BPF_MAP_TYPE_CPUMAP: | |
1560 | if (func_id != BPF_FUNC_redirect_map) | |
1561 | goto error; | |
1562 | break; | |
56f668df | 1563 | case BPF_MAP_TYPE_ARRAY_OF_MAPS: |
bcc6b1b7 | 1564 | case BPF_MAP_TYPE_HASH_OF_MAPS: |
56f668df MKL |
1565 | if (func_id != BPF_FUNC_map_lookup_elem) |
1566 | goto error; | |
16a43625 | 1567 | break; |
174a79ff JF |
1568 | case BPF_MAP_TYPE_SOCKMAP: |
1569 | if (func_id != BPF_FUNC_sk_redirect_map && | |
1570 | func_id != BPF_FUNC_sock_map_update && | |
1571 | func_id != BPF_FUNC_map_delete_elem) | |
1572 | goto error; | |
1573 | break; | |
6aff67c8 AS |
1574 | default: |
1575 | break; | |
1576 | } | |
1577 | ||
1578 | /* ... and second from the function itself. */ | |
1579 | switch (func_id) { | |
1580 | case BPF_FUNC_tail_call: | |
1581 | if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY) | |
1582 | goto error; | |
1583 | break; | |
1584 | case BPF_FUNC_perf_event_read: | |
1585 | case BPF_FUNC_perf_event_output: | |
908432ca | 1586 | case BPF_FUNC_perf_event_read_value: |
6aff67c8 AS |
1587 | if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) |
1588 | goto error; | |
1589 | break; | |
1590 | case BPF_FUNC_get_stackid: | |
1591 | if (map->map_type != BPF_MAP_TYPE_STACK_TRACE) | |
1592 | goto error; | |
1593 | break; | |
60d20f91 | 1594 | case BPF_FUNC_current_task_under_cgroup: |
747ea55e | 1595 | case BPF_FUNC_skb_under_cgroup: |
4a482f34 MKL |
1596 | if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY) |
1597 | goto error; | |
1598 | break; | |
97f91a7c | 1599 | case BPF_FUNC_redirect_map: |
9c270af3 JDB |
1600 | if (map->map_type != BPF_MAP_TYPE_DEVMAP && |
1601 | map->map_type != BPF_MAP_TYPE_CPUMAP) | |
97f91a7c JF |
1602 | goto error; |
1603 | break; | |
174a79ff JF |
1604 | case BPF_FUNC_sk_redirect_map: |
1605 | if (map->map_type != BPF_MAP_TYPE_SOCKMAP) | |
1606 | goto error; | |
1607 | break; | |
1608 | case BPF_FUNC_sock_map_update: | |
1609 | if (map->map_type != BPF_MAP_TYPE_SOCKMAP) | |
1610 | goto error; | |
1611 | break; | |
6aff67c8 AS |
1612 | default: |
1613 | break; | |
35578d79 KX |
1614 | } |
1615 | ||
1616 | return 0; | |
6aff67c8 | 1617 | error: |
61bd5218 | 1618 | verbose(env, "cannot pass map_type %d into func %s#%d\n", |
ebb676da | 1619 | map->map_type, func_id_name(func_id), func_id); |
6aff67c8 | 1620 | return -EINVAL; |
35578d79 KX |
1621 | } |
1622 | ||
435faee1 DB |
1623 | static int check_raw_mode(const struct bpf_func_proto *fn) |
1624 | { | |
1625 | int count = 0; | |
1626 | ||
39f19ebb | 1627 | if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 1628 | count++; |
39f19ebb | 1629 | if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 1630 | count++; |
39f19ebb | 1631 | if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 1632 | count++; |
39f19ebb | 1633 | if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 | 1634 | count++; |
39f19ebb | 1635 | if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM) |
435faee1 DB |
1636 | count++; |
1637 | ||
1638 | return count > 1 ? -EINVAL : 0; | |
1639 | } | |
1640 | ||
de8f3a83 DB |
1641 | /* Packet data might have moved, any old PTR_TO_PACKET[_META,_END] |
1642 | * are now invalid, so turn them into unknown SCALAR_VALUE. | |
f1174f77 | 1643 | */ |
58e2af8b | 1644 | static void clear_all_pkt_pointers(struct bpf_verifier_env *env) |
969bf05e | 1645 | { |
638f5b90 | 1646 | struct bpf_verifier_state *state = env->cur_state; |
58e2af8b | 1647 | struct bpf_reg_state *regs = state->regs, *reg; |
969bf05e AS |
1648 | int i; |
1649 | ||
1650 | for (i = 0; i < MAX_BPF_REG; i++) | |
de8f3a83 | 1651 | if (reg_is_pkt_pointer_any(®s[i])) |
61bd5218 | 1652 | mark_reg_unknown(env, regs, i); |
969bf05e | 1653 | |
638f5b90 AS |
1654 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
1655 | if (state->stack[i].slot_type[0] != STACK_SPILL) | |
969bf05e | 1656 | continue; |
638f5b90 | 1657 | reg = &state->stack[i].spilled_ptr; |
de8f3a83 DB |
1658 | if (reg_is_pkt_pointer_any(reg)) |
1659 | __mark_reg_unknown(reg); | |
969bf05e AS |
1660 | } |
1661 | } | |
1662 | ||
81ed18ab | 1663 | static int check_call(struct bpf_verifier_env *env, int func_id, int insn_idx) |
17a52670 | 1664 | { |
17a52670 | 1665 | const struct bpf_func_proto *fn = NULL; |
638f5b90 | 1666 | struct bpf_reg_state *regs; |
33ff9823 | 1667 | struct bpf_call_arg_meta meta; |
969bf05e | 1668 | bool changes_data; |
17a52670 AS |
1669 | int i, err; |
1670 | ||
1671 | /* find function prototype */ | |
1672 | if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) { | |
61bd5218 JK |
1673 | verbose(env, "invalid func %s#%d\n", func_id_name(func_id), |
1674 | func_id); | |
17a52670 AS |
1675 | return -EINVAL; |
1676 | } | |
1677 | ||
00176a34 JK |
1678 | if (env->ops->get_func_proto) |
1679 | fn = env->ops->get_func_proto(func_id); | |
17a52670 AS |
1680 | |
1681 | if (!fn) { | |
61bd5218 JK |
1682 | verbose(env, "unknown func %s#%d\n", func_id_name(func_id), |
1683 | func_id); | |
17a52670 AS |
1684 | return -EINVAL; |
1685 | } | |
1686 | ||
1687 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ | |
24701ece | 1688 | if (!env->prog->gpl_compatible && fn->gpl_only) { |
61bd5218 | 1689 | verbose(env, "cannot call GPL only function from proprietary program\n"); |
17a52670 AS |
1690 | return -EINVAL; |
1691 | } | |
1692 | ||
17bedab2 | 1693 | changes_data = bpf_helper_changes_pkt_data(fn->func); |
969bf05e | 1694 | |
33ff9823 | 1695 | memset(&meta, 0, sizeof(meta)); |
36bbef52 | 1696 | meta.pkt_access = fn->pkt_access; |
33ff9823 | 1697 | |
435faee1 DB |
1698 | /* We only support one arg being in raw mode at the moment, which |
1699 | * is sufficient for the helper functions we have right now. | |
1700 | */ | |
1701 | err = check_raw_mode(fn); | |
1702 | if (err) { | |
61bd5218 | 1703 | verbose(env, "kernel subsystem misconfigured func %s#%d\n", |
ebb676da | 1704 | func_id_name(func_id), func_id); |
435faee1 DB |
1705 | return err; |
1706 | } | |
1707 | ||
17a52670 | 1708 | /* check args */ |
33ff9823 | 1709 | err = check_func_arg(env, BPF_REG_1, fn->arg1_type, &meta); |
17a52670 AS |
1710 | if (err) |
1711 | return err; | |
33ff9823 | 1712 | err = check_func_arg(env, BPF_REG_2, fn->arg2_type, &meta); |
17a52670 AS |
1713 | if (err) |
1714 | return err; | |
33ff9823 | 1715 | err = check_func_arg(env, BPF_REG_3, fn->arg3_type, &meta); |
17a52670 AS |
1716 | if (err) |
1717 | return err; | |
33ff9823 | 1718 | err = check_func_arg(env, BPF_REG_4, fn->arg4_type, &meta); |
17a52670 AS |
1719 | if (err) |
1720 | return err; | |
33ff9823 | 1721 | err = check_func_arg(env, BPF_REG_5, fn->arg5_type, &meta); |
17a52670 AS |
1722 | if (err) |
1723 | return err; | |
1724 | ||
435faee1 DB |
1725 | /* Mark slots with STACK_MISC in case of raw mode, stack offset |
1726 | * is inferred from register state. | |
1727 | */ | |
1728 | for (i = 0; i < meta.access_size; i++) { | |
31fd8581 | 1729 | err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B, BPF_WRITE, -1); |
435faee1 DB |
1730 | if (err) |
1731 | return err; | |
1732 | } | |
1733 | ||
638f5b90 | 1734 | regs = cur_regs(env); |
17a52670 | 1735 | /* reset caller saved regs */ |
dc503a8a | 1736 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
61bd5218 | 1737 | mark_reg_not_init(env, regs, caller_saved[i]); |
dc503a8a EC |
1738 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
1739 | } | |
17a52670 | 1740 | |
dc503a8a | 1741 | /* update return register (already marked as written above) */ |
17a52670 | 1742 | if (fn->ret_type == RET_INTEGER) { |
f1174f77 | 1743 | /* sets type to SCALAR_VALUE */ |
61bd5218 | 1744 | mark_reg_unknown(env, regs, BPF_REG_0); |
17a52670 AS |
1745 | } else if (fn->ret_type == RET_VOID) { |
1746 | regs[BPF_REG_0].type = NOT_INIT; | |
1747 | } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL) { | |
fad73a1a MKL |
1748 | struct bpf_insn_aux_data *insn_aux; |
1749 | ||
17a52670 | 1750 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL; |
f1174f77 | 1751 | /* There is no offset yet applied, variable or fixed */ |
61bd5218 | 1752 | mark_reg_known_zero(env, regs, BPF_REG_0); |
f1174f77 | 1753 | regs[BPF_REG_0].off = 0; |
17a52670 AS |
1754 | /* remember map_ptr, so that check_map_access() |
1755 | * can check 'value_size' boundary of memory access | |
1756 | * to map element returned from bpf_map_lookup_elem() | |
1757 | */ | |
33ff9823 | 1758 | if (meta.map_ptr == NULL) { |
61bd5218 JK |
1759 | verbose(env, |
1760 | "kernel subsystem misconfigured verifier\n"); | |
17a52670 AS |
1761 | return -EINVAL; |
1762 | } | |
33ff9823 | 1763 | regs[BPF_REG_0].map_ptr = meta.map_ptr; |
57a09bf0 | 1764 | regs[BPF_REG_0].id = ++env->id_gen; |
fad73a1a MKL |
1765 | insn_aux = &env->insn_aux_data[insn_idx]; |
1766 | if (!insn_aux->map_ptr) | |
1767 | insn_aux->map_ptr = meta.map_ptr; | |
1768 | else if (insn_aux->map_ptr != meta.map_ptr) | |
1769 | insn_aux->map_ptr = BPF_MAP_PTR_POISON; | |
17a52670 | 1770 | } else { |
61bd5218 | 1771 | verbose(env, "unknown return type %d of func %s#%d\n", |
ebb676da | 1772 | fn->ret_type, func_id_name(func_id), func_id); |
17a52670 AS |
1773 | return -EINVAL; |
1774 | } | |
04fd61ab | 1775 | |
61bd5218 | 1776 | err = check_map_func_compatibility(env, meta.map_ptr, func_id); |
35578d79 KX |
1777 | if (err) |
1778 | return err; | |
04fd61ab | 1779 | |
969bf05e AS |
1780 | if (changes_data) |
1781 | clear_all_pkt_pointers(env); | |
1782 | return 0; | |
1783 | } | |
1784 | ||
f1174f77 EC |
1785 | static void coerce_reg_to_32(struct bpf_reg_state *reg) |
1786 | { | |
f1174f77 EC |
1787 | /* clear high 32 bits */ |
1788 | reg->var_off = tnum_cast(reg->var_off, 4); | |
b03c9f9f EC |
1789 | /* Update bounds */ |
1790 | __update_reg_bounds(reg); | |
1791 | } | |
1792 | ||
1793 | static bool signed_add_overflows(s64 a, s64 b) | |
1794 | { | |
1795 | /* Do the add in u64, where overflow is well-defined */ | |
1796 | s64 res = (s64)((u64)a + (u64)b); | |
1797 | ||
1798 | if (b < 0) | |
1799 | return res > a; | |
1800 | return res < a; | |
1801 | } | |
1802 | ||
1803 | static bool signed_sub_overflows(s64 a, s64 b) | |
1804 | { | |
1805 | /* Do the sub in u64, where overflow is well-defined */ | |
1806 | s64 res = (s64)((u64)a - (u64)b); | |
1807 | ||
1808 | if (b < 0) | |
1809 | return res < a; | |
1810 | return res > a; | |
969bf05e AS |
1811 | } |
1812 | ||
f1174f77 | 1813 | /* Handles arithmetic on a pointer and a scalar: computes new min/max and var_off. |
f1174f77 EC |
1814 | * Caller should also handle BPF_MOV case separately. |
1815 | * If we return -EACCES, caller may want to try again treating pointer as a | |
1816 | * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks. | |
1817 | */ | |
1818 | static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, | |
1819 | struct bpf_insn *insn, | |
1820 | const struct bpf_reg_state *ptr_reg, | |
1821 | const struct bpf_reg_state *off_reg) | |
969bf05e | 1822 | { |
638f5b90 | 1823 | struct bpf_reg_state *regs = cur_regs(env), *dst_reg; |
f1174f77 | 1824 | bool known = tnum_is_const(off_reg->var_off); |
b03c9f9f EC |
1825 | s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value, |
1826 | smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value; | |
1827 | u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value, | |
1828 | umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value; | |
969bf05e | 1829 | u8 opcode = BPF_OP(insn->code); |
f1174f77 | 1830 | u32 dst = insn->dst_reg; |
969bf05e | 1831 | |
f1174f77 | 1832 | dst_reg = ®s[dst]; |
969bf05e | 1833 | |
b03c9f9f | 1834 | if (WARN_ON_ONCE(known && (smin_val != smax_val))) { |
638f5b90 | 1835 | print_verifier_state(env, env->cur_state); |
61bd5218 JK |
1836 | verbose(env, |
1837 | "verifier internal error: known but bad sbounds\n"); | |
b03c9f9f EC |
1838 | return -EINVAL; |
1839 | } | |
1840 | if (WARN_ON_ONCE(known && (umin_val != umax_val))) { | |
638f5b90 | 1841 | print_verifier_state(env, env->cur_state); |
61bd5218 JK |
1842 | verbose(env, |
1843 | "verifier internal error: known but bad ubounds\n"); | |
f1174f77 EC |
1844 | return -EINVAL; |
1845 | } | |
1846 | ||
1847 | if (BPF_CLASS(insn->code) != BPF_ALU64) { | |
1848 | /* 32-bit ALU ops on pointers produce (meaningless) scalars */ | |
1849 | if (!env->allow_ptr_leaks) | |
61bd5218 JK |
1850 | verbose(env, |
1851 | "R%d 32-bit pointer arithmetic prohibited\n", | |
f1174f77 EC |
1852 | dst); |
1853 | return -EACCES; | |
969bf05e AS |
1854 | } |
1855 | ||
f1174f77 EC |
1856 | if (ptr_reg->type == PTR_TO_MAP_VALUE_OR_NULL) { |
1857 | if (!env->allow_ptr_leaks) | |
61bd5218 | 1858 | verbose(env, "R%d pointer arithmetic on PTR_TO_MAP_VALUE_OR_NULL prohibited, null-check it first\n", |
f1174f77 EC |
1859 | dst); |
1860 | return -EACCES; | |
1861 | } | |
1862 | if (ptr_reg->type == CONST_PTR_TO_MAP) { | |
1863 | if (!env->allow_ptr_leaks) | |
61bd5218 | 1864 | verbose(env, "R%d pointer arithmetic on CONST_PTR_TO_MAP prohibited\n", |
f1174f77 EC |
1865 | dst); |
1866 | return -EACCES; | |
1867 | } | |
1868 | if (ptr_reg->type == PTR_TO_PACKET_END) { | |
1869 | if (!env->allow_ptr_leaks) | |
61bd5218 | 1870 | verbose(env, "R%d pointer arithmetic on PTR_TO_PACKET_END prohibited\n", |
f1174f77 EC |
1871 | dst); |
1872 | return -EACCES; | |
1873 | } | |
1874 | ||
1875 | /* In case of 'scalar += pointer', dst_reg inherits pointer type and id. | |
1876 | * The id may be overwritten later if we create a new variable offset. | |
969bf05e | 1877 | */ |
f1174f77 EC |
1878 | dst_reg->type = ptr_reg->type; |
1879 | dst_reg->id = ptr_reg->id; | |
969bf05e | 1880 | |
f1174f77 EC |
1881 | switch (opcode) { |
1882 | case BPF_ADD: | |
1883 | /* We can take a fixed offset as long as it doesn't overflow | |
1884 | * the s32 'off' field | |
969bf05e | 1885 | */ |
b03c9f9f EC |
1886 | if (known && (ptr_reg->off + smin_val == |
1887 | (s64)(s32)(ptr_reg->off + smin_val))) { | |
f1174f77 | 1888 | /* pointer += K. Accumulate it into fixed offset */ |
b03c9f9f EC |
1889 | dst_reg->smin_value = smin_ptr; |
1890 | dst_reg->smax_value = smax_ptr; | |
1891 | dst_reg->umin_value = umin_ptr; | |
1892 | dst_reg->umax_value = umax_ptr; | |
f1174f77 | 1893 | dst_reg->var_off = ptr_reg->var_off; |
b03c9f9f | 1894 | dst_reg->off = ptr_reg->off + smin_val; |
f1174f77 EC |
1895 | dst_reg->range = ptr_reg->range; |
1896 | break; | |
1897 | } | |
f1174f77 EC |
1898 | /* A new variable offset is created. Note that off_reg->off |
1899 | * == 0, since it's a scalar. | |
1900 | * dst_reg gets the pointer type and since some positive | |
1901 | * integer value was added to the pointer, give it a new 'id' | |
1902 | * if it's a PTR_TO_PACKET. | |
1903 | * this creates a new 'base' pointer, off_reg (variable) gets | |
1904 | * added into the variable offset, and we copy the fixed offset | |
1905 | * from ptr_reg. | |
969bf05e | 1906 | */ |
b03c9f9f EC |
1907 | if (signed_add_overflows(smin_ptr, smin_val) || |
1908 | signed_add_overflows(smax_ptr, smax_val)) { | |
1909 | dst_reg->smin_value = S64_MIN; | |
1910 | dst_reg->smax_value = S64_MAX; | |
1911 | } else { | |
1912 | dst_reg->smin_value = smin_ptr + smin_val; | |
1913 | dst_reg->smax_value = smax_ptr + smax_val; | |
1914 | } | |
1915 | if (umin_ptr + umin_val < umin_ptr || | |
1916 | umax_ptr + umax_val < umax_ptr) { | |
1917 | dst_reg->umin_value = 0; | |
1918 | dst_reg->umax_value = U64_MAX; | |
1919 | } else { | |
1920 | dst_reg->umin_value = umin_ptr + umin_val; | |
1921 | dst_reg->umax_value = umax_ptr + umax_val; | |
1922 | } | |
f1174f77 EC |
1923 | dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off); |
1924 | dst_reg->off = ptr_reg->off; | |
de8f3a83 | 1925 | if (reg_is_pkt_pointer(ptr_reg)) { |
f1174f77 EC |
1926 | dst_reg->id = ++env->id_gen; |
1927 | /* something was added to pkt_ptr, set range to zero */ | |
1928 | dst_reg->range = 0; | |
1929 | } | |
1930 | break; | |
1931 | case BPF_SUB: | |
1932 | if (dst_reg == off_reg) { | |
1933 | /* scalar -= pointer. Creates an unknown scalar */ | |
1934 | if (!env->allow_ptr_leaks) | |
61bd5218 | 1935 | verbose(env, "R%d tried to subtract pointer from scalar\n", |
f1174f77 EC |
1936 | dst); |
1937 | return -EACCES; | |
1938 | } | |
1939 | /* We don't allow subtraction from FP, because (according to | |
1940 | * test_verifier.c test "invalid fp arithmetic", JITs might not | |
1941 | * be able to deal with it. | |
969bf05e | 1942 | */ |
f1174f77 EC |
1943 | if (ptr_reg->type == PTR_TO_STACK) { |
1944 | if (!env->allow_ptr_leaks) | |
61bd5218 | 1945 | verbose(env, "R%d subtraction from stack pointer prohibited\n", |
f1174f77 EC |
1946 | dst); |
1947 | return -EACCES; | |
1948 | } | |
b03c9f9f EC |
1949 | if (known && (ptr_reg->off - smin_val == |
1950 | (s64)(s32)(ptr_reg->off - smin_val))) { | |
f1174f77 | 1951 | /* pointer -= K. Subtract it from fixed offset */ |
b03c9f9f EC |
1952 | dst_reg->smin_value = smin_ptr; |
1953 | dst_reg->smax_value = smax_ptr; | |
1954 | dst_reg->umin_value = umin_ptr; | |
1955 | dst_reg->umax_value = umax_ptr; | |
f1174f77 EC |
1956 | dst_reg->var_off = ptr_reg->var_off; |
1957 | dst_reg->id = ptr_reg->id; | |
b03c9f9f | 1958 | dst_reg->off = ptr_reg->off - smin_val; |
f1174f77 EC |
1959 | dst_reg->range = ptr_reg->range; |
1960 | break; | |
1961 | } | |
f1174f77 EC |
1962 | /* A new variable offset is created. If the subtrahend is known |
1963 | * nonnegative, then any reg->range we had before is still good. | |
969bf05e | 1964 | */ |
b03c9f9f EC |
1965 | if (signed_sub_overflows(smin_ptr, smax_val) || |
1966 | signed_sub_overflows(smax_ptr, smin_val)) { | |
1967 | /* Overflow possible, we know nothing */ | |
1968 | dst_reg->smin_value = S64_MIN; | |
1969 | dst_reg->smax_value = S64_MAX; | |
1970 | } else { | |
1971 | dst_reg->smin_value = smin_ptr - smax_val; | |
1972 | dst_reg->smax_value = smax_ptr - smin_val; | |
1973 | } | |
1974 | if (umin_ptr < umax_val) { | |
1975 | /* Overflow possible, we know nothing */ | |
1976 | dst_reg->umin_value = 0; | |
1977 | dst_reg->umax_value = U64_MAX; | |
1978 | } else { | |
1979 | /* Cannot overflow (as long as bounds are consistent) */ | |
1980 | dst_reg->umin_value = umin_ptr - umax_val; | |
1981 | dst_reg->umax_value = umax_ptr - umin_val; | |
1982 | } | |
f1174f77 EC |
1983 | dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off); |
1984 | dst_reg->off = ptr_reg->off; | |
de8f3a83 | 1985 | if (reg_is_pkt_pointer(ptr_reg)) { |
f1174f77 EC |
1986 | dst_reg->id = ++env->id_gen; |
1987 | /* something was added to pkt_ptr, set range to zero */ | |
b03c9f9f | 1988 | if (smin_val < 0) |
f1174f77 | 1989 | dst_reg->range = 0; |
43188702 | 1990 | } |
f1174f77 EC |
1991 | break; |
1992 | case BPF_AND: | |
1993 | case BPF_OR: | |
1994 | case BPF_XOR: | |
1995 | /* bitwise ops on pointers are troublesome, prohibit for now. | |
1996 | * (However, in principle we could allow some cases, e.g. | |
1997 | * ptr &= ~3 which would reduce min_value by 3.) | |
1998 | */ | |
1999 | if (!env->allow_ptr_leaks) | |
61bd5218 | 2000 | verbose(env, "R%d bitwise operator %s on pointer prohibited\n", |
f1174f77 EC |
2001 | dst, bpf_alu_string[opcode >> 4]); |
2002 | return -EACCES; | |
2003 | default: | |
2004 | /* other operators (e.g. MUL,LSH) produce non-pointer results */ | |
2005 | if (!env->allow_ptr_leaks) | |
61bd5218 | 2006 | verbose(env, "R%d pointer arithmetic with %s operator prohibited\n", |
f1174f77 EC |
2007 | dst, bpf_alu_string[opcode >> 4]); |
2008 | return -EACCES; | |
43188702 JF |
2009 | } |
2010 | ||
b03c9f9f EC |
2011 | __update_reg_bounds(dst_reg); |
2012 | __reg_deduce_bounds(dst_reg); | |
2013 | __reg_bound_offset(dst_reg); | |
43188702 JF |
2014 | return 0; |
2015 | } | |
2016 | ||
f1174f77 EC |
2017 | static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env, |
2018 | struct bpf_insn *insn, | |
2019 | struct bpf_reg_state *dst_reg, | |
2020 | struct bpf_reg_state src_reg) | |
969bf05e | 2021 | { |
638f5b90 | 2022 | struct bpf_reg_state *regs = cur_regs(env); |
48461135 | 2023 | u8 opcode = BPF_OP(insn->code); |
f1174f77 | 2024 | bool src_known, dst_known; |
b03c9f9f EC |
2025 | s64 smin_val, smax_val; |
2026 | u64 umin_val, umax_val; | |
48461135 | 2027 | |
f1174f77 EC |
2028 | if (BPF_CLASS(insn->code) != BPF_ALU64) { |
2029 | /* 32-bit ALU ops are (32,32)->64 */ | |
2030 | coerce_reg_to_32(dst_reg); | |
2031 | coerce_reg_to_32(&src_reg); | |
9305706c | 2032 | } |
b03c9f9f EC |
2033 | smin_val = src_reg.smin_value; |
2034 | smax_val = src_reg.smax_value; | |
2035 | umin_val = src_reg.umin_value; | |
2036 | umax_val = src_reg.umax_value; | |
f1174f77 EC |
2037 | src_known = tnum_is_const(src_reg.var_off); |
2038 | dst_known = tnum_is_const(dst_reg->var_off); | |
f23cc643 | 2039 | |
48461135 JB |
2040 | switch (opcode) { |
2041 | case BPF_ADD: | |
b03c9f9f EC |
2042 | if (signed_add_overflows(dst_reg->smin_value, smin_val) || |
2043 | signed_add_overflows(dst_reg->smax_value, smax_val)) { | |
2044 | dst_reg->smin_value = S64_MIN; | |
2045 | dst_reg->smax_value = S64_MAX; | |
2046 | } else { | |
2047 | dst_reg->smin_value += smin_val; | |
2048 | dst_reg->smax_value += smax_val; | |
2049 | } | |
2050 | if (dst_reg->umin_value + umin_val < umin_val || | |
2051 | dst_reg->umax_value + umax_val < umax_val) { | |
2052 | dst_reg->umin_value = 0; | |
2053 | dst_reg->umax_value = U64_MAX; | |
2054 | } else { | |
2055 | dst_reg->umin_value += umin_val; | |
2056 | dst_reg->umax_value += umax_val; | |
2057 | } | |
f1174f77 | 2058 | dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off); |
48461135 JB |
2059 | break; |
2060 | case BPF_SUB: | |
b03c9f9f EC |
2061 | if (signed_sub_overflows(dst_reg->smin_value, smax_val) || |
2062 | signed_sub_overflows(dst_reg->smax_value, smin_val)) { | |
2063 | /* Overflow possible, we know nothing */ | |
2064 | dst_reg->smin_value = S64_MIN; | |
2065 | dst_reg->smax_value = S64_MAX; | |
2066 | } else { | |
2067 | dst_reg->smin_value -= smax_val; | |
2068 | dst_reg->smax_value -= smin_val; | |
2069 | } | |
2070 | if (dst_reg->umin_value < umax_val) { | |
2071 | /* Overflow possible, we know nothing */ | |
2072 | dst_reg->umin_value = 0; | |
2073 | dst_reg->umax_value = U64_MAX; | |
2074 | } else { | |
2075 | /* Cannot overflow (as long as bounds are consistent) */ | |
2076 | dst_reg->umin_value -= umax_val; | |
2077 | dst_reg->umax_value -= umin_val; | |
2078 | } | |
f1174f77 | 2079 | dst_reg->var_off = tnum_sub(dst_reg->var_off, src_reg.var_off); |
48461135 JB |
2080 | break; |
2081 | case BPF_MUL: | |
b03c9f9f EC |
2082 | dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off); |
2083 | if (smin_val < 0 || dst_reg->smin_value < 0) { | |
f1174f77 | 2084 | /* Ain't nobody got time to multiply that sign */ |
b03c9f9f EC |
2085 | __mark_reg_unbounded(dst_reg); |
2086 | __update_reg_bounds(dst_reg); | |
f1174f77 EC |
2087 | break; |
2088 | } | |
b03c9f9f EC |
2089 | /* Both values are positive, so we can work with unsigned and |
2090 | * copy the result to signed (unless it exceeds S64_MAX). | |
f1174f77 | 2091 | */ |
b03c9f9f EC |
2092 | if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) { |
2093 | /* Potential overflow, we know nothing */ | |
2094 | __mark_reg_unbounded(dst_reg); | |
2095 | /* (except what we can learn from the var_off) */ | |
2096 | __update_reg_bounds(dst_reg); | |
2097 | break; | |
2098 | } | |
2099 | dst_reg->umin_value *= umin_val; | |
2100 | dst_reg->umax_value *= umax_val; | |
2101 | if (dst_reg->umax_value > S64_MAX) { | |
2102 | /* Overflow possible, we know nothing */ | |
2103 | dst_reg->smin_value = S64_MIN; | |
2104 | dst_reg->smax_value = S64_MAX; | |
2105 | } else { | |
2106 | dst_reg->smin_value = dst_reg->umin_value; | |
2107 | dst_reg->smax_value = dst_reg->umax_value; | |
2108 | } | |
48461135 JB |
2109 | break; |
2110 | case BPF_AND: | |
f1174f77 | 2111 | if (src_known && dst_known) { |
b03c9f9f EC |
2112 | __mark_reg_known(dst_reg, dst_reg->var_off.value & |
2113 | src_reg.var_off.value); | |
f1174f77 EC |
2114 | break; |
2115 | } | |
b03c9f9f EC |
2116 | /* We get our minimum from the var_off, since that's inherently |
2117 | * bitwise. Our maximum is the minimum of the operands' maxima. | |
f23cc643 | 2118 | */ |
f1174f77 | 2119 | dst_reg->var_off = tnum_and(dst_reg->var_off, src_reg.var_off); |
b03c9f9f EC |
2120 | dst_reg->umin_value = dst_reg->var_off.value; |
2121 | dst_reg->umax_value = min(dst_reg->umax_value, umax_val); | |
2122 | if (dst_reg->smin_value < 0 || smin_val < 0) { | |
2123 | /* Lose signed bounds when ANDing negative numbers, | |
2124 | * ain't nobody got time for that. | |
2125 | */ | |
2126 | dst_reg->smin_value = S64_MIN; | |
2127 | dst_reg->smax_value = S64_MAX; | |
2128 | } else { | |
2129 | /* ANDing two positives gives a positive, so safe to | |
2130 | * cast result into s64. | |
2131 | */ | |
2132 | dst_reg->smin_value = dst_reg->umin_value; | |
2133 | dst_reg->smax_value = dst_reg->umax_value; | |
2134 | } | |
2135 | /* We may learn something more from the var_off */ | |
2136 | __update_reg_bounds(dst_reg); | |
f1174f77 EC |
2137 | break; |
2138 | case BPF_OR: | |
2139 | if (src_known && dst_known) { | |
b03c9f9f EC |
2140 | __mark_reg_known(dst_reg, dst_reg->var_off.value | |
2141 | src_reg.var_off.value); | |
f1174f77 EC |
2142 | break; |
2143 | } | |
b03c9f9f EC |
2144 | /* We get our maximum from the var_off, and our minimum is the |
2145 | * maximum of the operands' minima | |
f1174f77 EC |
2146 | */ |
2147 | dst_reg->var_off = tnum_or(dst_reg->var_off, src_reg.var_off); | |
b03c9f9f EC |
2148 | dst_reg->umin_value = max(dst_reg->umin_value, umin_val); |
2149 | dst_reg->umax_value = dst_reg->var_off.value | | |
2150 | dst_reg->var_off.mask; | |
2151 | if (dst_reg->smin_value < 0 || smin_val < 0) { | |
2152 | /* Lose signed bounds when ORing negative numbers, | |
2153 | * ain't nobody got time for that. | |
2154 | */ | |
2155 | dst_reg->smin_value = S64_MIN; | |
2156 | dst_reg->smax_value = S64_MAX; | |
f1174f77 | 2157 | } else { |
b03c9f9f EC |
2158 | /* ORing two positives gives a positive, so safe to |
2159 | * cast result into s64. | |
2160 | */ | |
2161 | dst_reg->smin_value = dst_reg->umin_value; | |
2162 | dst_reg->smax_value = dst_reg->umax_value; | |
f1174f77 | 2163 | } |
b03c9f9f EC |
2164 | /* We may learn something more from the var_off */ |
2165 | __update_reg_bounds(dst_reg); | |
48461135 JB |
2166 | break; |
2167 | case BPF_LSH: | |
b03c9f9f EC |
2168 | if (umax_val > 63) { |
2169 | /* Shifts greater than 63 are undefined. This includes | |
2170 | * shifts by a negative number. | |
2171 | */ | |
61bd5218 | 2172 | mark_reg_unknown(env, regs, insn->dst_reg); |
f1174f77 EC |
2173 | break; |
2174 | } | |
b03c9f9f EC |
2175 | /* We lose all sign bit information (except what we can pick |
2176 | * up from var_off) | |
48461135 | 2177 | */ |
b03c9f9f EC |
2178 | dst_reg->smin_value = S64_MIN; |
2179 | dst_reg->smax_value = S64_MAX; | |
2180 | /* If we might shift our top bit out, then we know nothing */ | |
2181 | if (dst_reg->umax_value > 1ULL << (63 - umax_val)) { | |
2182 | dst_reg->umin_value = 0; | |
2183 | dst_reg->umax_value = U64_MAX; | |
d1174416 | 2184 | } else { |
b03c9f9f EC |
2185 | dst_reg->umin_value <<= umin_val; |
2186 | dst_reg->umax_value <<= umax_val; | |
d1174416 | 2187 | } |
b03c9f9f EC |
2188 | if (src_known) |
2189 | dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val); | |
2190 | else | |
2191 | dst_reg->var_off = tnum_lshift(tnum_unknown, umin_val); | |
2192 | /* We may learn something more from the var_off */ | |
2193 | __update_reg_bounds(dst_reg); | |
48461135 JB |
2194 | break; |
2195 | case BPF_RSH: | |
b03c9f9f EC |
2196 | if (umax_val > 63) { |
2197 | /* Shifts greater than 63 are undefined. This includes | |
2198 | * shifts by a negative number. | |
2199 | */ | |
61bd5218 | 2200 | mark_reg_unknown(env, regs, insn->dst_reg); |
f1174f77 EC |
2201 | break; |
2202 | } | |
2203 | /* BPF_RSH is an unsigned shift, so make the appropriate casts */ | |
b03c9f9f EC |
2204 | if (dst_reg->smin_value < 0) { |
2205 | if (umin_val) { | |
f1174f77 | 2206 | /* Sign bit will be cleared */ |
b03c9f9f EC |
2207 | dst_reg->smin_value = 0; |
2208 | } else { | |
2209 | /* Lost sign bit information */ | |
2210 | dst_reg->smin_value = S64_MIN; | |
2211 | dst_reg->smax_value = S64_MAX; | |
2212 | } | |
d1174416 | 2213 | } else { |
b03c9f9f EC |
2214 | dst_reg->smin_value = |
2215 | (u64)(dst_reg->smin_value) >> umax_val; | |
d1174416 | 2216 | } |
f1174f77 | 2217 | if (src_known) |
b03c9f9f EC |
2218 | dst_reg->var_off = tnum_rshift(dst_reg->var_off, |
2219 | umin_val); | |
f1174f77 | 2220 | else |
b03c9f9f EC |
2221 | dst_reg->var_off = tnum_rshift(tnum_unknown, umin_val); |
2222 | dst_reg->umin_value >>= umax_val; | |
2223 | dst_reg->umax_value >>= umin_val; | |
2224 | /* We may learn something more from the var_off */ | |
2225 | __update_reg_bounds(dst_reg); | |
48461135 JB |
2226 | break; |
2227 | default: | |
61bd5218 | 2228 | mark_reg_unknown(env, regs, insn->dst_reg); |
48461135 JB |
2229 | break; |
2230 | } | |
2231 | ||
b03c9f9f EC |
2232 | __reg_deduce_bounds(dst_reg); |
2233 | __reg_bound_offset(dst_reg); | |
f1174f77 EC |
2234 | return 0; |
2235 | } | |
2236 | ||
2237 | /* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max | |
2238 | * and var_off. | |
2239 | */ | |
2240 | static int adjust_reg_min_max_vals(struct bpf_verifier_env *env, | |
2241 | struct bpf_insn *insn) | |
2242 | { | |
638f5b90 | 2243 | struct bpf_reg_state *regs = cur_regs(env), *dst_reg, *src_reg; |
f1174f77 EC |
2244 | struct bpf_reg_state *ptr_reg = NULL, off_reg = {0}; |
2245 | u8 opcode = BPF_OP(insn->code); | |
2246 | int rc; | |
2247 | ||
2248 | dst_reg = ®s[insn->dst_reg]; | |
f1174f77 EC |
2249 | src_reg = NULL; |
2250 | if (dst_reg->type != SCALAR_VALUE) | |
2251 | ptr_reg = dst_reg; | |
2252 | if (BPF_SRC(insn->code) == BPF_X) { | |
2253 | src_reg = ®s[insn->src_reg]; | |
f1174f77 EC |
2254 | if (src_reg->type != SCALAR_VALUE) { |
2255 | if (dst_reg->type != SCALAR_VALUE) { | |
2256 | /* Combining two pointers by any ALU op yields | |
2257 | * an arbitrary scalar. | |
2258 | */ | |
2259 | if (!env->allow_ptr_leaks) { | |
61bd5218 | 2260 | verbose(env, "R%d pointer %s pointer prohibited\n", |
f1174f77 EC |
2261 | insn->dst_reg, |
2262 | bpf_alu_string[opcode >> 4]); | |
2263 | return -EACCES; | |
2264 | } | |
61bd5218 | 2265 | mark_reg_unknown(env, regs, insn->dst_reg); |
f1174f77 EC |
2266 | return 0; |
2267 | } else { | |
2268 | /* scalar += pointer | |
2269 | * This is legal, but we have to reverse our | |
2270 | * src/dest handling in computing the range | |
2271 | */ | |
2272 | rc = adjust_ptr_min_max_vals(env, insn, | |
2273 | src_reg, dst_reg); | |
2274 | if (rc == -EACCES && env->allow_ptr_leaks) { | |
2275 | /* scalar += unknown scalar */ | |
2276 | __mark_reg_unknown(&off_reg); | |
2277 | return adjust_scalar_min_max_vals( | |
2278 | env, insn, | |
2279 | dst_reg, off_reg); | |
2280 | } | |
2281 | return rc; | |
2282 | } | |
2283 | } else if (ptr_reg) { | |
2284 | /* pointer += scalar */ | |
2285 | rc = adjust_ptr_min_max_vals(env, insn, | |
2286 | dst_reg, src_reg); | |
2287 | if (rc == -EACCES && env->allow_ptr_leaks) { | |
2288 | /* unknown scalar += scalar */ | |
2289 | __mark_reg_unknown(dst_reg); | |
2290 | return adjust_scalar_min_max_vals( | |
2291 | env, insn, dst_reg, *src_reg); | |
2292 | } | |
2293 | return rc; | |
2294 | } | |
2295 | } else { | |
2296 | /* Pretend the src is a reg with a known value, since we only | |
2297 | * need to be able to read from this state. | |
2298 | */ | |
2299 | off_reg.type = SCALAR_VALUE; | |
b03c9f9f | 2300 | __mark_reg_known(&off_reg, insn->imm); |
f1174f77 | 2301 | src_reg = &off_reg; |
f1174f77 EC |
2302 | if (ptr_reg) { /* pointer += K */ |
2303 | rc = adjust_ptr_min_max_vals(env, insn, | |
2304 | ptr_reg, src_reg); | |
2305 | if (rc == -EACCES && env->allow_ptr_leaks) { | |
2306 | /* unknown scalar += K */ | |
2307 | __mark_reg_unknown(dst_reg); | |
2308 | return adjust_scalar_min_max_vals( | |
2309 | env, insn, dst_reg, off_reg); | |
2310 | } | |
2311 | return rc; | |
2312 | } | |
2313 | } | |
2314 | ||
2315 | /* Got here implies adding two SCALAR_VALUEs */ | |
2316 | if (WARN_ON_ONCE(ptr_reg)) { | |
638f5b90 | 2317 | print_verifier_state(env, env->cur_state); |
61bd5218 | 2318 | verbose(env, "verifier internal error: unexpected ptr_reg\n"); |
f1174f77 EC |
2319 | return -EINVAL; |
2320 | } | |
2321 | if (WARN_ON(!src_reg)) { | |
638f5b90 | 2322 | print_verifier_state(env, env->cur_state); |
61bd5218 | 2323 | verbose(env, "verifier internal error: no src_reg\n"); |
f1174f77 EC |
2324 | return -EINVAL; |
2325 | } | |
2326 | return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg); | |
48461135 JB |
2327 | } |
2328 | ||
17a52670 | 2329 | /* check validity of 32-bit and 64-bit arithmetic operations */ |
58e2af8b | 2330 | static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn) |
17a52670 | 2331 | { |
638f5b90 | 2332 | struct bpf_reg_state *regs = cur_regs(env); |
17a52670 AS |
2333 | u8 opcode = BPF_OP(insn->code); |
2334 | int err; | |
2335 | ||
2336 | if (opcode == BPF_END || opcode == BPF_NEG) { | |
2337 | if (opcode == BPF_NEG) { | |
2338 | if (BPF_SRC(insn->code) != 0 || | |
2339 | insn->src_reg != BPF_REG_0 || | |
2340 | insn->off != 0 || insn->imm != 0) { | |
61bd5218 | 2341 | verbose(env, "BPF_NEG uses reserved fields\n"); |
17a52670 AS |
2342 | return -EINVAL; |
2343 | } | |
2344 | } else { | |
2345 | if (insn->src_reg != BPF_REG_0 || insn->off != 0 || | |
e67b8a68 EC |
2346 | (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) || |
2347 | BPF_CLASS(insn->code) == BPF_ALU64) { | |
61bd5218 | 2348 | verbose(env, "BPF_END uses reserved fields\n"); |
17a52670 AS |
2349 | return -EINVAL; |
2350 | } | |
2351 | } | |
2352 | ||
2353 | /* check src operand */ | |
dc503a8a | 2354 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
2355 | if (err) |
2356 | return err; | |
2357 | ||
1be7f75d | 2358 | if (is_pointer_value(env, insn->dst_reg)) { |
61bd5218 | 2359 | verbose(env, "R%d pointer arithmetic prohibited\n", |
1be7f75d AS |
2360 | insn->dst_reg); |
2361 | return -EACCES; | |
2362 | } | |
2363 | ||
17a52670 | 2364 | /* check dest operand */ |
dc503a8a | 2365 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
17a52670 AS |
2366 | if (err) |
2367 | return err; | |
2368 | ||
2369 | } else if (opcode == BPF_MOV) { | |
2370 | ||
2371 | if (BPF_SRC(insn->code) == BPF_X) { | |
2372 | if (insn->imm != 0 || insn->off != 0) { | |
61bd5218 | 2373 | verbose(env, "BPF_MOV uses reserved fields\n"); |
17a52670 AS |
2374 | return -EINVAL; |
2375 | } | |
2376 | ||
2377 | /* check src operand */ | |
dc503a8a | 2378 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
2379 | if (err) |
2380 | return err; | |
2381 | } else { | |
2382 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { | |
61bd5218 | 2383 | verbose(env, "BPF_MOV uses reserved fields\n"); |
17a52670 AS |
2384 | return -EINVAL; |
2385 | } | |
2386 | } | |
2387 | ||
2388 | /* check dest operand */ | |
dc503a8a | 2389 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
17a52670 AS |
2390 | if (err) |
2391 | return err; | |
2392 | ||
2393 | if (BPF_SRC(insn->code) == BPF_X) { | |
2394 | if (BPF_CLASS(insn->code) == BPF_ALU64) { | |
2395 | /* case: R1 = R2 | |
2396 | * copy register state to dest reg | |
2397 | */ | |
2398 | regs[insn->dst_reg] = regs[insn->src_reg]; | |
8fe2d6cc | 2399 | regs[insn->dst_reg].live |= REG_LIVE_WRITTEN; |
17a52670 | 2400 | } else { |
f1174f77 | 2401 | /* R1 = (u32) R2 */ |
1be7f75d | 2402 | if (is_pointer_value(env, insn->src_reg)) { |
61bd5218 JK |
2403 | verbose(env, |
2404 | "R%d partial copy of pointer\n", | |
1be7f75d AS |
2405 | insn->src_reg); |
2406 | return -EACCES; | |
2407 | } | |
61bd5218 | 2408 | mark_reg_unknown(env, regs, insn->dst_reg); |
b03c9f9f | 2409 | /* high 32 bits are known zero. */ |
f1174f77 EC |
2410 | regs[insn->dst_reg].var_off = tnum_cast( |
2411 | regs[insn->dst_reg].var_off, 4); | |
b03c9f9f | 2412 | __update_reg_bounds(®s[insn->dst_reg]); |
17a52670 AS |
2413 | } |
2414 | } else { | |
2415 | /* case: R = imm | |
2416 | * remember the value we stored into this reg | |
2417 | */ | |
f1174f77 | 2418 | regs[insn->dst_reg].type = SCALAR_VALUE; |
b03c9f9f | 2419 | __mark_reg_known(regs + insn->dst_reg, insn->imm); |
17a52670 AS |
2420 | } |
2421 | ||
2422 | } else if (opcode > BPF_END) { | |
61bd5218 | 2423 | verbose(env, "invalid BPF_ALU opcode %x\n", opcode); |
17a52670 AS |
2424 | return -EINVAL; |
2425 | ||
2426 | } else { /* all other ALU ops: and, sub, xor, add, ... */ | |
2427 | ||
17a52670 AS |
2428 | if (BPF_SRC(insn->code) == BPF_X) { |
2429 | if (insn->imm != 0 || insn->off != 0) { | |
61bd5218 | 2430 | verbose(env, "BPF_ALU uses reserved fields\n"); |
17a52670 AS |
2431 | return -EINVAL; |
2432 | } | |
2433 | /* check src1 operand */ | |
dc503a8a | 2434 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
2435 | if (err) |
2436 | return err; | |
2437 | } else { | |
2438 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { | |
61bd5218 | 2439 | verbose(env, "BPF_ALU uses reserved fields\n"); |
17a52670 AS |
2440 | return -EINVAL; |
2441 | } | |
2442 | } | |
2443 | ||
2444 | /* check src2 operand */ | |
dc503a8a | 2445 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
2446 | if (err) |
2447 | return err; | |
2448 | ||
2449 | if ((opcode == BPF_MOD || opcode == BPF_DIV) && | |
2450 | BPF_SRC(insn->code) == BPF_K && insn->imm == 0) { | |
61bd5218 | 2451 | verbose(env, "div by zero\n"); |
17a52670 AS |
2452 | return -EINVAL; |
2453 | } | |
2454 | ||
229394e8 RV |
2455 | if ((opcode == BPF_LSH || opcode == BPF_RSH || |
2456 | opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) { | |
2457 | int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32; | |
2458 | ||
2459 | if (insn->imm < 0 || insn->imm >= size) { | |
61bd5218 | 2460 | verbose(env, "invalid shift %d\n", insn->imm); |
229394e8 RV |
2461 | return -EINVAL; |
2462 | } | |
2463 | } | |
2464 | ||
1a0dc1ac | 2465 | /* check dest operand */ |
dc503a8a | 2466 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
1a0dc1ac AS |
2467 | if (err) |
2468 | return err; | |
2469 | ||
f1174f77 | 2470 | return adjust_reg_min_max_vals(env, insn); |
17a52670 AS |
2471 | } |
2472 | ||
2473 | return 0; | |
2474 | } | |
2475 | ||
58e2af8b | 2476 | static void find_good_pkt_pointers(struct bpf_verifier_state *state, |
de8f3a83 | 2477 | struct bpf_reg_state *dst_reg, |
f8ddadc4 | 2478 | enum bpf_reg_type type, |
fb2a311a | 2479 | bool range_right_open) |
969bf05e | 2480 | { |
58e2af8b | 2481 | struct bpf_reg_state *regs = state->regs, *reg; |
fb2a311a | 2482 | u16 new_range; |
969bf05e | 2483 | int i; |
2d2be8ca | 2484 | |
fb2a311a DB |
2485 | if (dst_reg->off < 0 || |
2486 | (dst_reg->off == 0 && range_right_open)) | |
f1174f77 EC |
2487 | /* This doesn't give us any range */ |
2488 | return; | |
2489 | ||
b03c9f9f EC |
2490 | if (dst_reg->umax_value > MAX_PACKET_OFF || |
2491 | dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF) | |
f1174f77 EC |
2492 | /* Risk of overflow. For instance, ptr + (1<<63) may be less |
2493 | * than pkt_end, but that's because it's also less than pkt. | |
2494 | */ | |
2495 | return; | |
2496 | ||
fb2a311a DB |
2497 | new_range = dst_reg->off; |
2498 | if (range_right_open) | |
2499 | new_range--; | |
2500 | ||
2501 | /* Examples for register markings: | |
2d2be8ca | 2502 | * |
fb2a311a | 2503 | * pkt_data in dst register: |
2d2be8ca DB |
2504 | * |
2505 | * r2 = r3; | |
2506 | * r2 += 8; | |
2507 | * if (r2 > pkt_end) goto <handle exception> | |
2508 | * <access okay> | |
2509 | * | |
b4e432f1 DB |
2510 | * r2 = r3; |
2511 | * r2 += 8; | |
2512 | * if (r2 < pkt_end) goto <access okay> | |
2513 | * <handle exception> | |
2514 | * | |
2d2be8ca DB |
2515 | * Where: |
2516 | * r2 == dst_reg, pkt_end == src_reg | |
2517 | * r2=pkt(id=n,off=8,r=0) | |
2518 | * r3=pkt(id=n,off=0,r=0) | |
2519 | * | |
fb2a311a | 2520 | * pkt_data in src register: |
2d2be8ca DB |
2521 | * |
2522 | * r2 = r3; | |
2523 | * r2 += 8; | |
2524 | * if (pkt_end >= r2) goto <access okay> | |
2525 | * <handle exception> | |
2526 | * | |
b4e432f1 DB |
2527 | * r2 = r3; |
2528 | * r2 += 8; | |
2529 | * if (pkt_end <= r2) goto <handle exception> | |
2530 | * <access okay> | |
2531 | * | |
2d2be8ca DB |
2532 | * Where: |
2533 | * pkt_end == dst_reg, r2 == src_reg | |
2534 | * r2=pkt(id=n,off=8,r=0) | |
2535 | * r3=pkt(id=n,off=0,r=0) | |
2536 | * | |
2537 | * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8) | |
fb2a311a DB |
2538 | * or r3=pkt(id=n,off=0,r=8-1), so that range of bytes [r3, r3 + 8) |
2539 | * and [r3, r3 + 8-1) respectively is safe to access depending on | |
2540 | * the check. | |
969bf05e | 2541 | */ |
2d2be8ca | 2542 | |
f1174f77 EC |
2543 | /* If our ids match, then we must have the same max_value. And we |
2544 | * don't care about the other reg's fixed offset, since if it's too big | |
2545 | * the range won't allow anything. | |
2546 | * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16. | |
2547 | */ | |
969bf05e | 2548 | for (i = 0; i < MAX_BPF_REG; i++) |
de8f3a83 | 2549 | if (regs[i].type == type && regs[i].id == dst_reg->id) |
b1977682 | 2550 | /* keep the maximum range already checked */ |
fb2a311a | 2551 | regs[i].range = max(regs[i].range, new_range); |
969bf05e | 2552 | |
638f5b90 AS |
2553 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
2554 | if (state->stack[i].slot_type[0] != STACK_SPILL) | |
969bf05e | 2555 | continue; |
638f5b90 | 2556 | reg = &state->stack[i].spilled_ptr; |
de8f3a83 | 2557 | if (reg->type == type && reg->id == dst_reg->id) |
b06723da | 2558 | reg->range = max(reg->range, new_range); |
969bf05e AS |
2559 | } |
2560 | } | |
2561 | ||
48461135 JB |
2562 | /* Adjusts the register min/max values in the case that the dst_reg is the |
2563 | * variable register that we are working on, and src_reg is a constant or we're | |
2564 | * simply doing a BPF_K check. | |
f1174f77 | 2565 | * In JEQ/JNE cases we also adjust the var_off values. |
48461135 JB |
2566 | */ |
2567 | static void reg_set_min_max(struct bpf_reg_state *true_reg, | |
2568 | struct bpf_reg_state *false_reg, u64 val, | |
2569 | u8 opcode) | |
2570 | { | |
f1174f77 EC |
2571 | /* If the dst_reg is a pointer, we can't learn anything about its |
2572 | * variable offset from the compare (unless src_reg were a pointer into | |
2573 | * the same object, but we don't bother with that. | |
2574 | * Since false_reg and true_reg have the same type by construction, we | |
2575 | * only need to check one of them for pointerness. | |
2576 | */ | |
2577 | if (__is_pointer_value(false, false_reg)) | |
2578 | return; | |
4cabc5b1 | 2579 | |
48461135 JB |
2580 | switch (opcode) { |
2581 | case BPF_JEQ: | |
2582 | /* If this is false then we know nothing Jon Snow, but if it is | |
2583 | * true then we know for sure. | |
2584 | */ | |
b03c9f9f | 2585 | __mark_reg_known(true_reg, val); |
48461135 JB |
2586 | break; |
2587 | case BPF_JNE: | |
2588 | /* If this is true we know nothing Jon Snow, but if it is false | |
2589 | * we know the value for sure; | |
2590 | */ | |
b03c9f9f | 2591 | __mark_reg_known(false_reg, val); |
48461135 JB |
2592 | break; |
2593 | case BPF_JGT: | |
b03c9f9f EC |
2594 | false_reg->umax_value = min(false_reg->umax_value, val); |
2595 | true_reg->umin_value = max(true_reg->umin_value, val + 1); | |
2596 | break; | |
48461135 | 2597 | case BPF_JSGT: |
b03c9f9f EC |
2598 | false_reg->smax_value = min_t(s64, false_reg->smax_value, val); |
2599 | true_reg->smin_value = max_t(s64, true_reg->smin_value, val + 1); | |
48461135 | 2600 | break; |
b4e432f1 DB |
2601 | case BPF_JLT: |
2602 | false_reg->umin_value = max(false_reg->umin_value, val); | |
2603 | true_reg->umax_value = min(true_reg->umax_value, val - 1); | |
2604 | break; | |
2605 | case BPF_JSLT: | |
2606 | false_reg->smin_value = max_t(s64, false_reg->smin_value, val); | |
2607 | true_reg->smax_value = min_t(s64, true_reg->smax_value, val - 1); | |
2608 | break; | |
48461135 | 2609 | case BPF_JGE: |
b03c9f9f EC |
2610 | false_reg->umax_value = min(false_reg->umax_value, val - 1); |
2611 | true_reg->umin_value = max(true_reg->umin_value, val); | |
2612 | break; | |
48461135 | 2613 | case BPF_JSGE: |
b03c9f9f EC |
2614 | false_reg->smax_value = min_t(s64, false_reg->smax_value, val - 1); |
2615 | true_reg->smin_value = max_t(s64, true_reg->smin_value, val); | |
48461135 | 2616 | break; |
b4e432f1 DB |
2617 | case BPF_JLE: |
2618 | false_reg->umin_value = max(false_reg->umin_value, val + 1); | |
2619 | true_reg->umax_value = min(true_reg->umax_value, val); | |
2620 | break; | |
2621 | case BPF_JSLE: | |
2622 | false_reg->smin_value = max_t(s64, false_reg->smin_value, val + 1); | |
2623 | true_reg->smax_value = min_t(s64, true_reg->smax_value, val); | |
2624 | break; | |
48461135 JB |
2625 | default: |
2626 | break; | |
2627 | } | |
2628 | ||
b03c9f9f EC |
2629 | __reg_deduce_bounds(false_reg); |
2630 | __reg_deduce_bounds(true_reg); | |
2631 | /* We might have learned some bits from the bounds. */ | |
2632 | __reg_bound_offset(false_reg); | |
2633 | __reg_bound_offset(true_reg); | |
2634 | /* Intersecting with the old var_off might have improved our bounds | |
2635 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), | |
2636 | * then new var_off is (0; 0x7f...fc) which improves our umax. | |
2637 | */ | |
2638 | __update_reg_bounds(false_reg); | |
2639 | __update_reg_bounds(true_reg); | |
48461135 JB |
2640 | } |
2641 | ||
f1174f77 EC |
2642 | /* Same as above, but for the case that dst_reg holds a constant and src_reg is |
2643 | * the variable reg. | |
48461135 JB |
2644 | */ |
2645 | static void reg_set_min_max_inv(struct bpf_reg_state *true_reg, | |
2646 | struct bpf_reg_state *false_reg, u64 val, | |
2647 | u8 opcode) | |
2648 | { | |
f1174f77 EC |
2649 | if (__is_pointer_value(false, false_reg)) |
2650 | return; | |
4cabc5b1 | 2651 | |
48461135 JB |
2652 | switch (opcode) { |
2653 | case BPF_JEQ: | |
2654 | /* If this is false then we know nothing Jon Snow, but if it is | |
2655 | * true then we know for sure. | |
2656 | */ | |
b03c9f9f | 2657 | __mark_reg_known(true_reg, val); |
48461135 JB |
2658 | break; |
2659 | case BPF_JNE: | |
2660 | /* If this is true we know nothing Jon Snow, but if it is false | |
2661 | * we know the value for sure; | |
2662 | */ | |
b03c9f9f | 2663 | __mark_reg_known(false_reg, val); |
48461135 JB |
2664 | break; |
2665 | case BPF_JGT: | |
b03c9f9f EC |
2666 | true_reg->umax_value = min(true_reg->umax_value, val - 1); |
2667 | false_reg->umin_value = max(false_reg->umin_value, val); | |
2668 | break; | |
48461135 | 2669 | case BPF_JSGT: |
b03c9f9f EC |
2670 | true_reg->smax_value = min_t(s64, true_reg->smax_value, val - 1); |
2671 | false_reg->smin_value = max_t(s64, false_reg->smin_value, val); | |
48461135 | 2672 | break; |
b4e432f1 DB |
2673 | case BPF_JLT: |
2674 | true_reg->umin_value = max(true_reg->umin_value, val + 1); | |
2675 | false_reg->umax_value = min(false_reg->umax_value, val); | |
2676 | break; | |
2677 | case BPF_JSLT: | |
2678 | true_reg->smin_value = max_t(s64, true_reg->smin_value, val + 1); | |
2679 | false_reg->smax_value = min_t(s64, false_reg->smax_value, val); | |
2680 | break; | |
48461135 | 2681 | case BPF_JGE: |
b03c9f9f EC |
2682 | true_reg->umax_value = min(true_reg->umax_value, val); |
2683 | false_reg->umin_value = max(false_reg->umin_value, val + 1); | |
2684 | break; | |
48461135 | 2685 | case BPF_JSGE: |
b03c9f9f EC |
2686 | true_reg->smax_value = min_t(s64, true_reg->smax_value, val); |
2687 | false_reg->smin_value = max_t(s64, false_reg->smin_value, val + 1); | |
48461135 | 2688 | break; |
b4e432f1 DB |
2689 | case BPF_JLE: |
2690 | true_reg->umin_value = max(true_reg->umin_value, val); | |
2691 | false_reg->umax_value = min(false_reg->umax_value, val - 1); | |
2692 | break; | |
2693 | case BPF_JSLE: | |
2694 | true_reg->smin_value = max_t(s64, true_reg->smin_value, val); | |
2695 | false_reg->smax_value = min_t(s64, false_reg->smax_value, val - 1); | |
2696 | break; | |
48461135 JB |
2697 | default: |
2698 | break; | |
2699 | } | |
2700 | ||
b03c9f9f EC |
2701 | __reg_deduce_bounds(false_reg); |
2702 | __reg_deduce_bounds(true_reg); | |
2703 | /* We might have learned some bits from the bounds. */ | |
2704 | __reg_bound_offset(false_reg); | |
2705 | __reg_bound_offset(true_reg); | |
2706 | /* Intersecting with the old var_off might have improved our bounds | |
2707 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), | |
2708 | * then new var_off is (0; 0x7f...fc) which improves our umax. | |
2709 | */ | |
2710 | __update_reg_bounds(false_reg); | |
2711 | __update_reg_bounds(true_reg); | |
f1174f77 EC |
2712 | } |
2713 | ||
2714 | /* Regs are known to be equal, so intersect their min/max/var_off */ | |
2715 | static void __reg_combine_min_max(struct bpf_reg_state *src_reg, | |
2716 | struct bpf_reg_state *dst_reg) | |
2717 | { | |
b03c9f9f EC |
2718 | src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value, |
2719 | dst_reg->umin_value); | |
2720 | src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value, | |
2721 | dst_reg->umax_value); | |
2722 | src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value, | |
2723 | dst_reg->smin_value); | |
2724 | src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value, | |
2725 | dst_reg->smax_value); | |
f1174f77 EC |
2726 | src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off, |
2727 | dst_reg->var_off); | |
b03c9f9f EC |
2728 | /* We might have learned new bounds from the var_off. */ |
2729 | __update_reg_bounds(src_reg); | |
2730 | __update_reg_bounds(dst_reg); | |
2731 | /* We might have learned something about the sign bit. */ | |
2732 | __reg_deduce_bounds(src_reg); | |
2733 | __reg_deduce_bounds(dst_reg); | |
2734 | /* We might have learned some bits from the bounds. */ | |
2735 | __reg_bound_offset(src_reg); | |
2736 | __reg_bound_offset(dst_reg); | |
2737 | /* Intersecting with the old var_off might have improved our bounds | |
2738 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), | |
2739 | * then new var_off is (0; 0x7f...fc) which improves our umax. | |
2740 | */ | |
2741 | __update_reg_bounds(src_reg); | |
2742 | __update_reg_bounds(dst_reg); | |
f1174f77 EC |
2743 | } |
2744 | ||
2745 | static void reg_combine_min_max(struct bpf_reg_state *true_src, | |
2746 | struct bpf_reg_state *true_dst, | |
2747 | struct bpf_reg_state *false_src, | |
2748 | struct bpf_reg_state *false_dst, | |
2749 | u8 opcode) | |
2750 | { | |
2751 | switch (opcode) { | |
2752 | case BPF_JEQ: | |
2753 | __reg_combine_min_max(true_src, true_dst); | |
2754 | break; | |
2755 | case BPF_JNE: | |
2756 | __reg_combine_min_max(false_src, false_dst); | |
b03c9f9f | 2757 | break; |
4cabc5b1 | 2758 | } |
48461135 JB |
2759 | } |
2760 | ||
57a09bf0 | 2761 | static void mark_map_reg(struct bpf_reg_state *regs, u32 regno, u32 id, |
f1174f77 | 2762 | bool is_null) |
57a09bf0 TG |
2763 | { |
2764 | struct bpf_reg_state *reg = ®s[regno]; | |
2765 | ||
2766 | if (reg->type == PTR_TO_MAP_VALUE_OR_NULL && reg->id == id) { | |
f1174f77 EC |
2767 | /* Old offset (both fixed and variable parts) should |
2768 | * have been known-zero, because we don't allow pointer | |
2769 | * arithmetic on pointers that might be NULL. | |
2770 | */ | |
b03c9f9f EC |
2771 | if (WARN_ON_ONCE(reg->smin_value || reg->smax_value || |
2772 | !tnum_equals_const(reg->var_off, 0) || | |
f1174f77 | 2773 | reg->off)) { |
b03c9f9f EC |
2774 | __mark_reg_known_zero(reg); |
2775 | reg->off = 0; | |
f1174f77 EC |
2776 | } |
2777 | if (is_null) { | |
2778 | reg->type = SCALAR_VALUE; | |
56f668df MKL |
2779 | } else if (reg->map_ptr->inner_map_meta) { |
2780 | reg->type = CONST_PTR_TO_MAP; | |
2781 | reg->map_ptr = reg->map_ptr->inner_map_meta; | |
2782 | } else { | |
f1174f77 | 2783 | reg->type = PTR_TO_MAP_VALUE; |
56f668df | 2784 | } |
a08dd0da DB |
2785 | /* We don't need id from this point onwards anymore, thus we |
2786 | * should better reset it, so that state pruning has chances | |
2787 | * to take effect. | |
2788 | */ | |
2789 | reg->id = 0; | |
57a09bf0 TG |
2790 | } |
2791 | } | |
2792 | ||
2793 | /* The logic is similar to find_good_pkt_pointers(), both could eventually | |
2794 | * be folded together at some point. | |
2795 | */ | |
2796 | static void mark_map_regs(struct bpf_verifier_state *state, u32 regno, | |
f1174f77 | 2797 | bool is_null) |
57a09bf0 TG |
2798 | { |
2799 | struct bpf_reg_state *regs = state->regs; | |
a08dd0da | 2800 | u32 id = regs[regno].id; |
57a09bf0 TG |
2801 | int i; |
2802 | ||
2803 | for (i = 0; i < MAX_BPF_REG; i++) | |
f1174f77 | 2804 | mark_map_reg(regs, i, id, is_null); |
57a09bf0 | 2805 | |
638f5b90 AS |
2806 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
2807 | if (state->stack[i].slot_type[0] != STACK_SPILL) | |
57a09bf0 | 2808 | continue; |
638f5b90 | 2809 | mark_map_reg(&state->stack[i].spilled_ptr, 0, id, is_null); |
57a09bf0 TG |
2810 | } |
2811 | } | |
2812 | ||
5beca081 DB |
2813 | static bool try_match_pkt_pointers(const struct bpf_insn *insn, |
2814 | struct bpf_reg_state *dst_reg, | |
2815 | struct bpf_reg_state *src_reg, | |
2816 | struct bpf_verifier_state *this_branch, | |
2817 | struct bpf_verifier_state *other_branch) | |
2818 | { | |
2819 | if (BPF_SRC(insn->code) != BPF_X) | |
2820 | return false; | |
2821 | ||
2822 | switch (BPF_OP(insn->code)) { | |
2823 | case BPF_JGT: | |
2824 | if ((dst_reg->type == PTR_TO_PACKET && | |
2825 | src_reg->type == PTR_TO_PACKET_END) || | |
2826 | (dst_reg->type == PTR_TO_PACKET_META && | |
2827 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
2828 | /* pkt_data' > pkt_end, pkt_meta' > pkt_data */ | |
2829 | find_good_pkt_pointers(this_branch, dst_reg, | |
2830 | dst_reg->type, false); | |
2831 | } else if ((dst_reg->type == PTR_TO_PACKET_END && | |
2832 | src_reg->type == PTR_TO_PACKET) || | |
2833 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
2834 | src_reg->type == PTR_TO_PACKET_META)) { | |
2835 | /* pkt_end > pkt_data', pkt_data > pkt_meta' */ | |
2836 | find_good_pkt_pointers(other_branch, src_reg, | |
2837 | src_reg->type, true); | |
2838 | } else { | |
2839 | return false; | |
2840 | } | |
2841 | break; | |
2842 | case BPF_JLT: | |
2843 | if ((dst_reg->type == PTR_TO_PACKET && | |
2844 | src_reg->type == PTR_TO_PACKET_END) || | |
2845 | (dst_reg->type == PTR_TO_PACKET_META && | |
2846 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
2847 | /* pkt_data' < pkt_end, pkt_meta' < pkt_data */ | |
2848 | find_good_pkt_pointers(other_branch, dst_reg, | |
2849 | dst_reg->type, true); | |
2850 | } else if ((dst_reg->type == PTR_TO_PACKET_END && | |
2851 | src_reg->type == PTR_TO_PACKET) || | |
2852 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
2853 | src_reg->type == PTR_TO_PACKET_META)) { | |
2854 | /* pkt_end < pkt_data', pkt_data > pkt_meta' */ | |
2855 | find_good_pkt_pointers(this_branch, src_reg, | |
2856 | src_reg->type, false); | |
2857 | } else { | |
2858 | return false; | |
2859 | } | |
2860 | break; | |
2861 | case BPF_JGE: | |
2862 | if ((dst_reg->type == PTR_TO_PACKET && | |
2863 | src_reg->type == PTR_TO_PACKET_END) || | |
2864 | (dst_reg->type == PTR_TO_PACKET_META && | |
2865 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
2866 | /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */ | |
2867 | find_good_pkt_pointers(this_branch, dst_reg, | |
2868 | dst_reg->type, true); | |
2869 | } else if ((dst_reg->type == PTR_TO_PACKET_END && | |
2870 | src_reg->type == PTR_TO_PACKET) || | |
2871 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
2872 | src_reg->type == PTR_TO_PACKET_META)) { | |
2873 | /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */ | |
2874 | find_good_pkt_pointers(other_branch, src_reg, | |
2875 | src_reg->type, false); | |
2876 | } else { | |
2877 | return false; | |
2878 | } | |
2879 | break; | |
2880 | case BPF_JLE: | |
2881 | if ((dst_reg->type == PTR_TO_PACKET && | |
2882 | src_reg->type == PTR_TO_PACKET_END) || | |
2883 | (dst_reg->type == PTR_TO_PACKET_META && | |
2884 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { | |
2885 | /* pkt_data' <= pkt_end, pkt_meta' <= pkt_data */ | |
2886 | find_good_pkt_pointers(other_branch, dst_reg, | |
2887 | dst_reg->type, false); | |
2888 | } else if ((dst_reg->type == PTR_TO_PACKET_END && | |
2889 | src_reg->type == PTR_TO_PACKET) || | |
2890 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && | |
2891 | src_reg->type == PTR_TO_PACKET_META)) { | |
2892 | /* pkt_end <= pkt_data', pkt_data <= pkt_meta' */ | |
2893 | find_good_pkt_pointers(this_branch, src_reg, | |
2894 | src_reg->type, true); | |
2895 | } else { | |
2896 | return false; | |
2897 | } | |
2898 | break; | |
2899 | default: | |
2900 | return false; | |
2901 | } | |
2902 | ||
2903 | return true; | |
2904 | } | |
2905 | ||
58e2af8b | 2906 | static int check_cond_jmp_op(struct bpf_verifier_env *env, |
17a52670 AS |
2907 | struct bpf_insn *insn, int *insn_idx) |
2908 | { | |
638f5b90 | 2909 | struct bpf_verifier_state *other_branch, *this_branch = env->cur_state; |
58e2af8b | 2910 | struct bpf_reg_state *regs = this_branch->regs, *dst_reg; |
17a52670 AS |
2911 | u8 opcode = BPF_OP(insn->code); |
2912 | int err; | |
2913 | ||
b4e432f1 | 2914 | if (opcode > BPF_JSLE) { |
61bd5218 | 2915 | verbose(env, "invalid BPF_JMP opcode %x\n", opcode); |
17a52670 AS |
2916 | return -EINVAL; |
2917 | } | |
2918 | ||
2919 | if (BPF_SRC(insn->code) == BPF_X) { | |
2920 | if (insn->imm != 0) { | |
61bd5218 | 2921 | verbose(env, "BPF_JMP uses reserved fields\n"); |
17a52670 AS |
2922 | return -EINVAL; |
2923 | } | |
2924 | ||
2925 | /* check src1 operand */ | |
dc503a8a | 2926 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
2927 | if (err) |
2928 | return err; | |
1be7f75d AS |
2929 | |
2930 | if (is_pointer_value(env, insn->src_reg)) { | |
61bd5218 | 2931 | verbose(env, "R%d pointer comparison prohibited\n", |
1be7f75d AS |
2932 | insn->src_reg); |
2933 | return -EACCES; | |
2934 | } | |
17a52670 AS |
2935 | } else { |
2936 | if (insn->src_reg != BPF_REG_0) { | |
61bd5218 | 2937 | verbose(env, "BPF_JMP uses reserved fields\n"); |
17a52670 AS |
2938 | return -EINVAL; |
2939 | } | |
2940 | } | |
2941 | ||
2942 | /* check src2 operand */ | |
dc503a8a | 2943 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
2944 | if (err) |
2945 | return err; | |
2946 | ||
1a0dc1ac AS |
2947 | dst_reg = ®s[insn->dst_reg]; |
2948 | ||
17a52670 AS |
2949 | /* detect if R == 0 where R was initialized to zero earlier */ |
2950 | if (BPF_SRC(insn->code) == BPF_K && | |
2951 | (opcode == BPF_JEQ || opcode == BPF_JNE) && | |
f1174f77 EC |
2952 | dst_reg->type == SCALAR_VALUE && |
2953 | tnum_equals_const(dst_reg->var_off, insn->imm)) { | |
17a52670 AS |
2954 | if (opcode == BPF_JEQ) { |
2955 | /* if (imm == imm) goto pc+off; | |
2956 | * only follow the goto, ignore fall-through | |
2957 | */ | |
2958 | *insn_idx += insn->off; | |
2959 | return 0; | |
2960 | } else { | |
2961 | /* if (imm != imm) goto pc+off; | |
2962 | * only follow fall-through branch, since | |
2963 | * that's where the program will go | |
2964 | */ | |
2965 | return 0; | |
2966 | } | |
2967 | } | |
2968 | ||
2969 | other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx); | |
2970 | if (!other_branch) | |
2971 | return -EFAULT; | |
2972 | ||
48461135 JB |
2973 | /* detect if we are comparing against a constant value so we can adjust |
2974 | * our min/max values for our dst register. | |
f1174f77 EC |
2975 | * this is only legit if both are scalars (or pointers to the same |
2976 | * object, I suppose, but we don't support that right now), because | |
2977 | * otherwise the different base pointers mean the offsets aren't | |
2978 | * comparable. | |
48461135 JB |
2979 | */ |
2980 | if (BPF_SRC(insn->code) == BPF_X) { | |
f1174f77 EC |
2981 | if (dst_reg->type == SCALAR_VALUE && |
2982 | regs[insn->src_reg].type == SCALAR_VALUE) { | |
2983 | if (tnum_is_const(regs[insn->src_reg].var_off)) | |
2984 | reg_set_min_max(&other_branch->regs[insn->dst_reg], | |
2985 | dst_reg, regs[insn->src_reg].var_off.value, | |
2986 | opcode); | |
2987 | else if (tnum_is_const(dst_reg->var_off)) | |
2988 | reg_set_min_max_inv(&other_branch->regs[insn->src_reg], | |
2989 | ®s[insn->src_reg], | |
2990 | dst_reg->var_off.value, opcode); | |
2991 | else if (opcode == BPF_JEQ || opcode == BPF_JNE) | |
2992 | /* Comparing for equality, we can combine knowledge */ | |
2993 | reg_combine_min_max(&other_branch->regs[insn->src_reg], | |
2994 | &other_branch->regs[insn->dst_reg], | |
2995 | ®s[insn->src_reg], | |
2996 | ®s[insn->dst_reg], opcode); | |
2997 | } | |
2998 | } else if (dst_reg->type == SCALAR_VALUE) { | |
48461135 JB |
2999 | reg_set_min_max(&other_branch->regs[insn->dst_reg], |
3000 | dst_reg, insn->imm, opcode); | |
3001 | } | |
3002 | ||
58e2af8b | 3003 | /* detect if R == 0 where R is returned from bpf_map_lookup_elem() */ |
17a52670 | 3004 | if (BPF_SRC(insn->code) == BPF_K && |
1a0dc1ac AS |
3005 | insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) && |
3006 | dst_reg->type == PTR_TO_MAP_VALUE_OR_NULL) { | |
57a09bf0 TG |
3007 | /* Mark all identical map registers in each branch as either |
3008 | * safe or unknown depending R == 0 or R != 0 conditional. | |
3009 | */ | |
f1174f77 EC |
3010 | mark_map_regs(this_branch, insn->dst_reg, opcode == BPF_JNE); |
3011 | mark_map_regs(other_branch, insn->dst_reg, opcode == BPF_JEQ); | |
5beca081 DB |
3012 | } else if (!try_match_pkt_pointers(insn, dst_reg, ®s[insn->src_reg], |
3013 | this_branch, other_branch) && | |
3014 | is_pointer_value(env, insn->dst_reg)) { | |
61bd5218 JK |
3015 | verbose(env, "R%d pointer comparison prohibited\n", |
3016 | insn->dst_reg); | |
1be7f75d | 3017 | return -EACCES; |
17a52670 | 3018 | } |
61bd5218 JK |
3019 | if (env->log.level) |
3020 | print_verifier_state(env, this_branch); | |
17a52670 AS |
3021 | return 0; |
3022 | } | |
3023 | ||
0246e64d AS |
3024 | /* return the map pointer stored inside BPF_LD_IMM64 instruction */ |
3025 | static struct bpf_map *ld_imm64_to_map_ptr(struct bpf_insn *insn) | |
3026 | { | |
3027 | u64 imm64 = ((u64) (u32) insn[0].imm) | ((u64) (u32) insn[1].imm) << 32; | |
3028 | ||
3029 | return (struct bpf_map *) (unsigned long) imm64; | |
3030 | } | |
3031 | ||
17a52670 | 3032 | /* verify BPF_LD_IMM64 instruction */ |
58e2af8b | 3033 | static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn) |
17a52670 | 3034 | { |
638f5b90 | 3035 | struct bpf_reg_state *regs = cur_regs(env); |
17a52670 AS |
3036 | int err; |
3037 | ||
3038 | if (BPF_SIZE(insn->code) != BPF_DW) { | |
61bd5218 | 3039 | verbose(env, "invalid BPF_LD_IMM insn\n"); |
17a52670 AS |
3040 | return -EINVAL; |
3041 | } | |
3042 | if (insn->off != 0) { | |
61bd5218 | 3043 | verbose(env, "BPF_LD_IMM64 uses reserved fields\n"); |
17a52670 AS |
3044 | return -EINVAL; |
3045 | } | |
3046 | ||
dc503a8a | 3047 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
17a52670 AS |
3048 | if (err) |
3049 | return err; | |
3050 | ||
6b173873 | 3051 | if (insn->src_reg == 0) { |
6b173873 JK |
3052 | u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm; |
3053 | ||
f1174f77 | 3054 | regs[insn->dst_reg].type = SCALAR_VALUE; |
b03c9f9f | 3055 | __mark_reg_known(®s[insn->dst_reg], imm); |
17a52670 | 3056 | return 0; |
6b173873 | 3057 | } |
17a52670 AS |
3058 | |
3059 | /* replace_map_fd_with_map_ptr() should have caught bad ld_imm64 */ | |
3060 | BUG_ON(insn->src_reg != BPF_PSEUDO_MAP_FD); | |
3061 | ||
3062 | regs[insn->dst_reg].type = CONST_PTR_TO_MAP; | |
3063 | regs[insn->dst_reg].map_ptr = ld_imm64_to_map_ptr(insn); | |
3064 | return 0; | |
3065 | } | |
3066 | ||
96be4325 DB |
3067 | static bool may_access_skb(enum bpf_prog_type type) |
3068 | { | |
3069 | switch (type) { | |
3070 | case BPF_PROG_TYPE_SOCKET_FILTER: | |
3071 | case BPF_PROG_TYPE_SCHED_CLS: | |
94caee8c | 3072 | case BPF_PROG_TYPE_SCHED_ACT: |
96be4325 DB |
3073 | return true; |
3074 | default: | |
3075 | return false; | |
3076 | } | |
3077 | } | |
3078 | ||
ddd872bc AS |
3079 | /* verify safety of LD_ABS|LD_IND instructions: |
3080 | * - they can only appear in the programs where ctx == skb | |
3081 | * - since they are wrappers of function calls, they scratch R1-R5 registers, | |
3082 | * preserve R6-R9, and store return value into R0 | |
3083 | * | |
3084 | * Implicit input: | |
3085 | * ctx == skb == R6 == CTX | |
3086 | * | |
3087 | * Explicit input: | |
3088 | * SRC == any register | |
3089 | * IMM == 32-bit immediate | |
3090 | * | |
3091 | * Output: | |
3092 | * R0 - 8/16/32-bit skb data converted to cpu endianness | |
3093 | */ | |
58e2af8b | 3094 | static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn) |
ddd872bc | 3095 | { |
638f5b90 | 3096 | struct bpf_reg_state *regs = cur_regs(env); |
ddd872bc | 3097 | u8 mode = BPF_MODE(insn->code); |
ddd872bc AS |
3098 | int i, err; |
3099 | ||
24701ece | 3100 | if (!may_access_skb(env->prog->type)) { |
61bd5218 | 3101 | verbose(env, "BPF_LD_[ABS|IND] instructions not allowed for this program type\n"); |
ddd872bc AS |
3102 | return -EINVAL; |
3103 | } | |
3104 | ||
3105 | if (insn->dst_reg != BPF_REG_0 || insn->off != 0 || | |
d82bccc6 | 3106 | BPF_SIZE(insn->code) == BPF_DW || |
ddd872bc | 3107 | (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) { |
61bd5218 | 3108 | verbose(env, "BPF_LD_[ABS|IND] uses reserved fields\n"); |
ddd872bc AS |
3109 | return -EINVAL; |
3110 | } | |
3111 | ||
3112 | /* check whether implicit source operand (register R6) is readable */ | |
dc503a8a | 3113 | err = check_reg_arg(env, BPF_REG_6, SRC_OP); |
ddd872bc AS |
3114 | if (err) |
3115 | return err; | |
3116 | ||
3117 | if (regs[BPF_REG_6].type != PTR_TO_CTX) { | |
61bd5218 JK |
3118 | verbose(env, |
3119 | "at the time of BPF_LD_ABS|IND R6 != pointer to skb\n"); | |
ddd872bc AS |
3120 | return -EINVAL; |
3121 | } | |
3122 | ||
3123 | if (mode == BPF_IND) { | |
3124 | /* check explicit source operand */ | |
dc503a8a | 3125 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
ddd872bc AS |
3126 | if (err) |
3127 | return err; | |
3128 | } | |
3129 | ||
3130 | /* reset caller saved regs to unreadable */ | |
dc503a8a | 3131 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
61bd5218 | 3132 | mark_reg_not_init(env, regs, caller_saved[i]); |
dc503a8a EC |
3133 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
3134 | } | |
ddd872bc AS |
3135 | |
3136 | /* mark destination R0 register as readable, since it contains | |
dc503a8a EC |
3137 | * the value fetched from the packet. |
3138 | * Already marked as written above. | |
ddd872bc | 3139 | */ |
61bd5218 | 3140 | mark_reg_unknown(env, regs, BPF_REG_0); |
ddd872bc AS |
3141 | return 0; |
3142 | } | |
3143 | ||
390ee7e2 AS |
3144 | static int check_return_code(struct bpf_verifier_env *env) |
3145 | { | |
3146 | struct bpf_reg_state *reg; | |
3147 | struct tnum range = tnum_range(0, 1); | |
3148 | ||
3149 | switch (env->prog->type) { | |
3150 | case BPF_PROG_TYPE_CGROUP_SKB: | |
3151 | case BPF_PROG_TYPE_CGROUP_SOCK: | |
3152 | case BPF_PROG_TYPE_SOCK_OPS: | |
ebc614f6 | 3153 | case BPF_PROG_TYPE_CGROUP_DEVICE: |
390ee7e2 AS |
3154 | break; |
3155 | default: | |
3156 | return 0; | |
3157 | } | |
3158 | ||
638f5b90 | 3159 | reg = cur_regs(env) + BPF_REG_0; |
390ee7e2 | 3160 | if (reg->type != SCALAR_VALUE) { |
61bd5218 | 3161 | verbose(env, "At program exit the register R0 is not a known value (%s)\n", |
390ee7e2 AS |
3162 | reg_type_str[reg->type]); |
3163 | return -EINVAL; | |
3164 | } | |
3165 | ||
3166 | if (!tnum_in(range, reg->var_off)) { | |
61bd5218 | 3167 | verbose(env, "At program exit the register R0 "); |
390ee7e2 AS |
3168 | if (!tnum_is_unknown(reg->var_off)) { |
3169 | char tn_buf[48]; | |
3170 | ||
3171 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); | |
61bd5218 | 3172 | verbose(env, "has value %s", tn_buf); |
390ee7e2 | 3173 | } else { |
61bd5218 | 3174 | verbose(env, "has unknown scalar value"); |
390ee7e2 | 3175 | } |
61bd5218 | 3176 | verbose(env, " should have been 0 or 1\n"); |
390ee7e2 AS |
3177 | return -EINVAL; |
3178 | } | |
3179 | return 0; | |
3180 | } | |
3181 | ||
475fb78f AS |
3182 | /* non-recursive DFS pseudo code |
3183 | * 1 procedure DFS-iterative(G,v): | |
3184 | * 2 label v as discovered | |
3185 | * 3 let S be a stack | |
3186 | * 4 S.push(v) | |
3187 | * 5 while S is not empty | |
3188 | * 6 t <- S.pop() | |
3189 | * 7 if t is what we're looking for: | |
3190 | * 8 return t | |
3191 | * 9 for all edges e in G.adjacentEdges(t) do | |
3192 | * 10 if edge e is already labelled | |
3193 | * 11 continue with the next edge | |
3194 | * 12 w <- G.adjacentVertex(t,e) | |
3195 | * 13 if vertex w is not discovered and not explored | |
3196 | * 14 label e as tree-edge | |
3197 | * 15 label w as discovered | |
3198 | * 16 S.push(w) | |
3199 | * 17 continue at 5 | |
3200 | * 18 else if vertex w is discovered | |
3201 | * 19 label e as back-edge | |
3202 | * 20 else | |
3203 | * 21 // vertex w is explored | |
3204 | * 22 label e as forward- or cross-edge | |
3205 | * 23 label t as explored | |
3206 | * 24 S.pop() | |
3207 | * | |
3208 | * convention: | |
3209 | * 0x10 - discovered | |
3210 | * 0x11 - discovered and fall-through edge labelled | |
3211 | * 0x12 - discovered and fall-through and branch edges labelled | |
3212 | * 0x20 - explored | |
3213 | */ | |
3214 | ||
3215 | enum { | |
3216 | DISCOVERED = 0x10, | |
3217 | EXPLORED = 0x20, | |
3218 | FALLTHROUGH = 1, | |
3219 | BRANCH = 2, | |
3220 | }; | |
3221 | ||
58e2af8b | 3222 | #define STATE_LIST_MARK ((struct bpf_verifier_state_list *) -1L) |
f1bca824 | 3223 | |
475fb78f AS |
3224 | static int *insn_stack; /* stack of insns to process */ |
3225 | static int cur_stack; /* current stack index */ | |
3226 | static int *insn_state; | |
3227 | ||
3228 | /* t, w, e - match pseudo-code above: | |
3229 | * t - index of current instruction | |
3230 | * w - next instruction | |
3231 | * e - edge | |
3232 | */ | |
58e2af8b | 3233 | static int push_insn(int t, int w, int e, struct bpf_verifier_env *env) |
475fb78f AS |
3234 | { |
3235 | if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH)) | |
3236 | return 0; | |
3237 | ||
3238 | if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH)) | |
3239 | return 0; | |
3240 | ||
3241 | if (w < 0 || w >= env->prog->len) { | |
61bd5218 | 3242 | verbose(env, "jump out of range from insn %d to %d\n", t, w); |
475fb78f AS |
3243 | return -EINVAL; |
3244 | } | |
3245 | ||
f1bca824 AS |
3246 | if (e == BRANCH) |
3247 | /* mark branch target for state pruning */ | |
3248 | env->explored_states[w] = STATE_LIST_MARK; | |
3249 | ||
475fb78f AS |
3250 | if (insn_state[w] == 0) { |
3251 | /* tree-edge */ | |
3252 | insn_state[t] = DISCOVERED | e; | |
3253 | insn_state[w] = DISCOVERED; | |
3254 | if (cur_stack >= env->prog->len) | |
3255 | return -E2BIG; | |
3256 | insn_stack[cur_stack++] = w; | |
3257 | return 1; | |
3258 | } else if ((insn_state[w] & 0xF0) == DISCOVERED) { | |
61bd5218 | 3259 | verbose(env, "back-edge from insn %d to %d\n", t, w); |
475fb78f AS |
3260 | return -EINVAL; |
3261 | } else if (insn_state[w] == EXPLORED) { | |
3262 | /* forward- or cross-edge */ | |
3263 | insn_state[t] = DISCOVERED | e; | |
3264 | } else { | |
61bd5218 | 3265 | verbose(env, "insn state internal bug\n"); |
475fb78f AS |
3266 | return -EFAULT; |
3267 | } | |
3268 | return 0; | |
3269 | } | |
3270 | ||
3271 | /* non-recursive depth-first-search to detect loops in BPF program | |
3272 | * loop == back-edge in directed graph | |
3273 | */ | |
58e2af8b | 3274 | static int check_cfg(struct bpf_verifier_env *env) |
475fb78f AS |
3275 | { |
3276 | struct bpf_insn *insns = env->prog->insnsi; | |
3277 | int insn_cnt = env->prog->len; | |
3278 | int ret = 0; | |
3279 | int i, t; | |
3280 | ||
3281 | insn_state = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL); | |
3282 | if (!insn_state) | |
3283 | return -ENOMEM; | |
3284 | ||
3285 | insn_stack = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL); | |
3286 | if (!insn_stack) { | |
3287 | kfree(insn_state); | |
3288 | return -ENOMEM; | |
3289 | } | |
3290 | ||
3291 | insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */ | |
3292 | insn_stack[0] = 0; /* 0 is the first instruction */ | |
3293 | cur_stack = 1; | |
3294 | ||
3295 | peek_stack: | |
3296 | if (cur_stack == 0) | |
3297 | goto check_state; | |
3298 | t = insn_stack[cur_stack - 1]; | |
3299 | ||
3300 | if (BPF_CLASS(insns[t].code) == BPF_JMP) { | |
3301 | u8 opcode = BPF_OP(insns[t].code); | |
3302 | ||
3303 | if (opcode == BPF_EXIT) { | |
3304 | goto mark_explored; | |
3305 | } else if (opcode == BPF_CALL) { | |
3306 | ret = push_insn(t, t + 1, FALLTHROUGH, env); | |
3307 | if (ret == 1) | |
3308 | goto peek_stack; | |
3309 | else if (ret < 0) | |
3310 | goto err_free; | |
07016151 DB |
3311 | if (t + 1 < insn_cnt) |
3312 | env->explored_states[t + 1] = STATE_LIST_MARK; | |
475fb78f AS |
3313 | } else if (opcode == BPF_JA) { |
3314 | if (BPF_SRC(insns[t].code) != BPF_K) { | |
3315 | ret = -EINVAL; | |
3316 | goto err_free; | |
3317 | } | |
3318 | /* unconditional jump with single edge */ | |
3319 | ret = push_insn(t, t + insns[t].off + 1, | |
3320 | FALLTHROUGH, env); | |
3321 | if (ret == 1) | |
3322 | goto peek_stack; | |
3323 | else if (ret < 0) | |
3324 | goto err_free; | |
f1bca824 AS |
3325 | /* tell verifier to check for equivalent states |
3326 | * after every call and jump | |
3327 | */ | |
c3de6317 AS |
3328 | if (t + 1 < insn_cnt) |
3329 | env->explored_states[t + 1] = STATE_LIST_MARK; | |
475fb78f AS |
3330 | } else { |
3331 | /* conditional jump with two edges */ | |
3c2ce60b | 3332 | env->explored_states[t] = STATE_LIST_MARK; |
475fb78f AS |
3333 | ret = push_insn(t, t + 1, FALLTHROUGH, env); |
3334 | if (ret == 1) | |
3335 | goto peek_stack; | |
3336 | else if (ret < 0) | |
3337 | goto err_free; | |
3338 | ||
3339 | ret = push_insn(t, t + insns[t].off + 1, BRANCH, env); | |
3340 | if (ret == 1) | |
3341 | goto peek_stack; | |
3342 | else if (ret < 0) | |
3343 | goto err_free; | |
3344 | } | |
3345 | } else { | |
3346 | /* all other non-branch instructions with single | |
3347 | * fall-through edge | |
3348 | */ | |
3349 | ret = push_insn(t, t + 1, FALLTHROUGH, env); | |
3350 | if (ret == 1) | |
3351 | goto peek_stack; | |
3352 | else if (ret < 0) | |
3353 | goto err_free; | |
3354 | } | |
3355 | ||
3356 | mark_explored: | |
3357 | insn_state[t] = EXPLORED; | |
3358 | if (cur_stack-- <= 0) { | |
61bd5218 | 3359 | verbose(env, "pop stack internal bug\n"); |
475fb78f AS |
3360 | ret = -EFAULT; |
3361 | goto err_free; | |
3362 | } | |
3363 | goto peek_stack; | |
3364 | ||
3365 | check_state: | |
3366 | for (i = 0; i < insn_cnt; i++) { | |
3367 | if (insn_state[i] != EXPLORED) { | |
61bd5218 | 3368 | verbose(env, "unreachable insn %d\n", i); |
475fb78f AS |
3369 | ret = -EINVAL; |
3370 | goto err_free; | |
3371 | } | |
3372 | } | |
3373 | ret = 0; /* cfg looks good */ | |
3374 | ||
3375 | err_free: | |
3376 | kfree(insn_state); | |
3377 | kfree(insn_stack); | |
3378 | return ret; | |
3379 | } | |
3380 | ||
f1174f77 EC |
3381 | /* check %cur's range satisfies %old's */ |
3382 | static bool range_within(struct bpf_reg_state *old, | |
3383 | struct bpf_reg_state *cur) | |
3384 | { | |
b03c9f9f EC |
3385 | return old->umin_value <= cur->umin_value && |
3386 | old->umax_value >= cur->umax_value && | |
3387 | old->smin_value <= cur->smin_value && | |
3388 | old->smax_value >= cur->smax_value; | |
f1174f77 EC |
3389 | } |
3390 | ||
3391 | /* Maximum number of register states that can exist at once */ | |
3392 | #define ID_MAP_SIZE (MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE) | |
3393 | struct idpair { | |
3394 | u32 old; | |
3395 | u32 cur; | |
3396 | }; | |
3397 | ||
3398 | /* If in the old state two registers had the same id, then they need to have | |
3399 | * the same id in the new state as well. But that id could be different from | |
3400 | * the old state, so we need to track the mapping from old to new ids. | |
3401 | * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent | |
3402 | * regs with old id 5 must also have new id 9 for the new state to be safe. But | |
3403 | * regs with a different old id could still have new id 9, we don't care about | |
3404 | * that. | |
3405 | * So we look through our idmap to see if this old id has been seen before. If | |
3406 | * so, we require the new id to match; otherwise, we add the id pair to the map. | |
969bf05e | 3407 | */ |
f1174f77 | 3408 | static bool check_ids(u32 old_id, u32 cur_id, struct idpair *idmap) |
969bf05e | 3409 | { |
f1174f77 | 3410 | unsigned int i; |
969bf05e | 3411 | |
f1174f77 EC |
3412 | for (i = 0; i < ID_MAP_SIZE; i++) { |
3413 | if (!idmap[i].old) { | |
3414 | /* Reached an empty slot; haven't seen this id before */ | |
3415 | idmap[i].old = old_id; | |
3416 | idmap[i].cur = cur_id; | |
3417 | return true; | |
3418 | } | |
3419 | if (idmap[i].old == old_id) | |
3420 | return idmap[i].cur == cur_id; | |
3421 | } | |
3422 | /* We ran out of idmap slots, which should be impossible */ | |
3423 | WARN_ON_ONCE(1); | |
3424 | return false; | |
3425 | } | |
3426 | ||
3427 | /* Returns true if (rold safe implies rcur safe) */ | |
1b688a19 EC |
3428 | static bool regsafe(struct bpf_reg_state *rold, struct bpf_reg_state *rcur, |
3429 | struct idpair *idmap) | |
f1174f77 | 3430 | { |
dc503a8a EC |
3431 | if (!(rold->live & REG_LIVE_READ)) |
3432 | /* explored state didn't use this */ | |
3433 | return true; | |
3434 | ||
3435 | if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, live)) == 0) | |
969bf05e AS |
3436 | return true; |
3437 | ||
f1174f77 EC |
3438 | if (rold->type == NOT_INIT) |
3439 | /* explored state can't have used this */ | |
969bf05e | 3440 | return true; |
f1174f77 EC |
3441 | if (rcur->type == NOT_INIT) |
3442 | return false; | |
3443 | switch (rold->type) { | |
3444 | case SCALAR_VALUE: | |
3445 | if (rcur->type == SCALAR_VALUE) { | |
3446 | /* new val must satisfy old val knowledge */ | |
3447 | return range_within(rold, rcur) && | |
3448 | tnum_in(rold->var_off, rcur->var_off); | |
3449 | } else { | |
3450 | /* if we knew anything about the old value, we're not | |
3451 | * equal, because we can't know anything about the | |
3452 | * scalar value of the pointer in the new value. | |
3453 | */ | |
b03c9f9f EC |
3454 | return rold->umin_value == 0 && |
3455 | rold->umax_value == U64_MAX && | |
3456 | rold->smin_value == S64_MIN && | |
3457 | rold->smax_value == S64_MAX && | |
f1174f77 EC |
3458 | tnum_is_unknown(rold->var_off); |
3459 | } | |
3460 | case PTR_TO_MAP_VALUE: | |
1b688a19 EC |
3461 | /* If the new min/max/var_off satisfy the old ones and |
3462 | * everything else matches, we are OK. | |
3463 | * We don't care about the 'id' value, because nothing | |
3464 | * uses it for PTR_TO_MAP_VALUE (only for ..._OR_NULL) | |
3465 | */ | |
3466 | return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 && | |
3467 | range_within(rold, rcur) && | |
3468 | tnum_in(rold->var_off, rcur->var_off); | |
f1174f77 EC |
3469 | case PTR_TO_MAP_VALUE_OR_NULL: |
3470 | /* a PTR_TO_MAP_VALUE could be safe to use as a | |
3471 | * PTR_TO_MAP_VALUE_OR_NULL into the same map. | |
3472 | * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL- | |
3473 | * checked, doing so could have affected others with the same | |
3474 | * id, and we can't check for that because we lost the id when | |
3475 | * we converted to a PTR_TO_MAP_VALUE. | |
3476 | */ | |
3477 | if (rcur->type != PTR_TO_MAP_VALUE_OR_NULL) | |
3478 | return false; | |
3479 | if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, id))) | |
3480 | return false; | |
3481 | /* Check our ids match any regs they're supposed to */ | |
3482 | return check_ids(rold->id, rcur->id, idmap); | |
de8f3a83 | 3483 | case PTR_TO_PACKET_META: |
f1174f77 | 3484 | case PTR_TO_PACKET: |
de8f3a83 | 3485 | if (rcur->type != rold->type) |
f1174f77 EC |
3486 | return false; |
3487 | /* We must have at least as much range as the old ptr | |
3488 | * did, so that any accesses which were safe before are | |
3489 | * still safe. This is true even if old range < old off, | |
3490 | * since someone could have accessed through (ptr - k), or | |
3491 | * even done ptr -= k in a register, to get a safe access. | |
3492 | */ | |
3493 | if (rold->range > rcur->range) | |
3494 | return false; | |
3495 | /* If the offsets don't match, we can't trust our alignment; | |
3496 | * nor can we be sure that we won't fall out of range. | |
3497 | */ | |
3498 | if (rold->off != rcur->off) | |
3499 | return false; | |
3500 | /* id relations must be preserved */ | |
3501 | if (rold->id && !check_ids(rold->id, rcur->id, idmap)) | |
3502 | return false; | |
3503 | /* new val must satisfy old val knowledge */ | |
3504 | return range_within(rold, rcur) && | |
3505 | tnum_in(rold->var_off, rcur->var_off); | |
3506 | case PTR_TO_CTX: | |
3507 | case CONST_PTR_TO_MAP: | |
3508 | case PTR_TO_STACK: | |
3509 | case PTR_TO_PACKET_END: | |
3510 | /* Only valid matches are exact, which memcmp() above | |
3511 | * would have accepted | |
3512 | */ | |
3513 | default: | |
3514 | /* Don't know what's going on, just say it's not safe */ | |
3515 | return false; | |
3516 | } | |
969bf05e | 3517 | |
f1174f77 EC |
3518 | /* Shouldn't get here; if we do, say it's not safe */ |
3519 | WARN_ON_ONCE(1); | |
969bf05e AS |
3520 | return false; |
3521 | } | |
3522 | ||
638f5b90 AS |
3523 | static bool stacksafe(struct bpf_verifier_state *old, |
3524 | struct bpf_verifier_state *cur, | |
3525 | struct idpair *idmap) | |
3526 | { | |
3527 | int i, spi; | |
3528 | ||
3529 | /* if explored stack has more populated slots than current stack | |
3530 | * such stacks are not equivalent | |
3531 | */ | |
3532 | if (old->allocated_stack > cur->allocated_stack) | |
3533 | return false; | |
3534 | ||
3535 | /* walk slots of the explored stack and ignore any additional | |
3536 | * slots in the current stack, since explored(safe) state | |
3537 | * didn't use them | |
3538 | */ | |
3539 | for (i = 0; i < old->allocated_stack; i++) { | |
3540 | spi = i / BPF_REG_SIZE; | |
3541 | ||
3542 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID) | |
3543 | continue; | |
3544 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] != | |
3545 | cur->stack[spi].slot_type[i % BPF_REG_SIZE]) | |
3546 | /* Ex: old explored (safe) state has STACK_SPILL in | |
3547 | * this stack slot, but current has has STACK_MISC -> | |
3548 | * this verifier states are not equivalent, | |
3549 | * return false to continue verification of this path | |
3550 | */ | |
3551 | return false; | |
3552 | if (i % BPF_REG_SIZE) | |
3553 | continue; | |
3554 | if (old->stack[spi].slot_type[0] != STACK_SPILL) | |
3555 | continue; | |
3556 | if (!regsafe(&old->stack[spi].spilled_ptr, | |
3557 | &cur->stack[spi].spilled_ptr, | |
3558 | idmap)) | |
3559 | /* when explored and current stack slot are both storing | |
3560 | * spilled registers, check that stored pointers types | |
3561 | * are the same as well. | |
3562 | * Ex: explored safe path could have stored | |
3563 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8} | |
3564 | * but current path has stored: | |
3565 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16} | |
3566 | * such verifier states are not equivalent. | |
3567 | * return false to continue verification of this path | |
3568 | */ | |
3569 | return false; | |
3570 | } | |
3571 | return true; | |
3572 | } | |
3573 | ||
f1bca824 AS |
3574 | /* compare two verifier states |
3575 | * | |
3576 | * all states stored in state_list are known to be valid, since | |
3577 | * verifier reached 'bpf_exit' instruction through them | |
3578 | * | |
3579 | * this function is called when verifier exploring different branches of | |
3580 | * execution popped from the state stack. If it sees an old state that has | |
3581 | * more strict register state and more strict stack state then this execution | |
3582 | * branch doesn't need to be explored further, since verifier already | |
3583 | * concluded that more strict state leads to valid finish. | |
3584 | * | |
3585 | * Therefore two states are equivalent if register state is more conservative | |
3586 | * and explored stack state is more conservative than the current one. | |
3587 | * Example: | |
3588 | * explored current | |
3589 | * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC) | |
3590 | * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC) | |
3591 | * | |
3592 | * In other words if current stack state (one being explored) has more | |
3593 | * valid slots than old one that already passed validation, it means | |
3594 | * the verifier can stop exploring and conclude that current state is valid too | |
3595 | * | |
3596 | * Similarly with registers. If explored state has register type as invalid | |
3597 | * whereas register type in current state is meaningful, it means that | |
3598 | * the current state will reach 'bpf_exit' instruction safely | |
3599 | */ | |
48461135 JB |
3600 | static bool states_equal(struct bpf_verifier_env *env, |
3601 | struct bpf_verifier_state *old, | |
58e2af8b | 3602 | struct bpf_verifier_state *cur) |
f1bca824 | 3603 | { |
f1174f77 EC |
3604 | struct idpair *idmap; |
3605 | bool ret = false; | |
f1bca824 AS |
3606 | int i; |
3607 | ||
f1174f77 EC |
3608 | idmap = kcalloc(ID_MAP_SIZE, sizeof(struct idpair), GFP_KERNEL); |
3609 | /* If we failed to allocate the idmap, just say it's not safe */ | |
3610 | if (!idmap) | |
1a0dc1ac | 3611 | return false; |
f1174f77 EC |
3612 | |
3613 | for (i = 0; i < MAX_BPF_REG; i++) { | |
1b688a19 | 3614 | if (!regsafe(&old->regs[i], &cur->regs[i], idmap)) |
f1174f77 | 3615 | goto out_free; |
f1bca824 AS |
3616 | } |
3617 | ||
638f5b90 AS |
3618 | if (!stacksafe(old, cur, idmap)) |
3619 | goto out_free; | |
f1174f77 EC |
3620 | ret = true; |
3621 | out_free: | |
3622 | kfree(idmap); | |
3623 | return ret; | |
f1bca824 AS |
3624 | } |
3625 | ||
8e9cd9ce EC |
3626 | /* A write screens off any subsequent reads; but write marks come from the |
3627 | * straight-line code between a state and its parent. When we arrive at a | |
3628 | * jump target (in the first iteration of the propagate_liveness() loop), | |
3629 | * we didn't arrive by the straight-line code, so read marks in state must | |
3630 | * propagate to parent regardless of state's write marks. | |
3631 | */ | |
dc503a8a EC |
3632 | static bool do_propagate_liveness(const struct bpf_verifier_state *state, |
3633 | struct bpf_verifier_state *parent) | |
3634 | { | |
63f45f84 | 3635 | bool writes = parent == state->parent; /* Observe write marks */ |
dc503a8a EC |
3636 | bool touched = false; /* any changes made? */ |
3637 | int i; | |
3638 | ||
3639 | if (!parent) | |
3640 | return touched; | |
3641 | /* Propagate read liveness of registers... */ | |
3642 | BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG); | |
3643 | /* We don't need to worry about FP liveness because it's read-only */ | |
3644 | for (i = 0; i < BPF_REG_FP; i++) { | |
3645 | if (parent->regs[i].live & REG_LIVE_READ) | |
3646 | continue; | |
63f45f84 EC |
3647 | if (writes && (state->regs[i].live & REG_LIVE_WRITTEN)) |
3648 | continue; | |
3649 | if (state->regs[i].live & REG_LIVE_READ) { | |
dc503a8a EC |
3650 | parent->regs[i].live |= REG_LIVE_READ; |
3651 | touched = true; | |
3652 | } | |
3653 | } | |
3654 | /* ... and stack slots */ | |
638f5b90 AS |
3655 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE && |
3656 | i < parent->allocated_stack / BPF_REG_SIZE; i++) { | |
3657 | if (parent->stack[i].slot_type[0] != STACK_SPILL) | |
dc503a8a | 3658 | continue; |
638f5b90 | 3659 | if (state->stack[i].slot_type[0] != STACK_SPILL) |
dc503a8a | 3660 | continue; |
638f5b90 | 3661 | if (parent->stack[i].spilled_ptr.live & REG_LIVE_READ) |
dc503a8a | 3662 | continue; |
638f5b90 AS |
3663 | if (writes && |
3664 | (state->stack[i].spilled_ptr.live & REG_LIVE_WRITTEN)) | |
63f45f84 | 3665 | continue; |
638f5b90 AS |
3666 | if (state->stack[i].spilled_ptr.live & REG_LIVE_READ) { |
3667 | parent->stack[i].spilled_ptr.live |= REG_LIVE_READ; | |
dc503a8a EC |
3668 | touched = true; |
3669 | } | |
3670 | } | |
3671 | return touched; | |
3672 | } | |
3673 | ||
8e9cd9ce EC |
3674 | /* "parent" is "a state from which we reach the current state", but initially |
3675 | * it is not the state->parent (i.e. "the state whose straight-line code leads | |
3676 | * to the current state"), instead it is the state that happened to arrive at | |
3677 | * a (prunable) equivalent of the current state. See comment above | |
3678 | * do_propagate_liveness() for consequences of this. | |
3679 | * This function is just a more efficient way of calling mark_reg_read() or | |
3680 | * mark_stack_slot_read() on each reg in "parent" that is read in "state", | |
3681 | * though it requires that parent != state->parent in the call arguments. | |
3682 | */ | |
dc503a8a EC |
3683 | static void propagate_liveness(const struct bpf_verifier_state *state, |
3684 | struct bpf_verifier_state *parent) | |
3685 | { | |
3686 | while (do_propagate_liveness(state, parent)) { | |
3687 | /* Something changed, so we need to feed those changes onward */ | |
3688 | state = parent; | |
3689 | parent = state->parent; | |
3690 | } | |
3691 | } | |
3692 | ||
58e2af8b | 3693 | static int is_state_visited(struct bpf_verifier_env *env, int insn_idx) |
f1bca824 | 3694 | { |
58e2af8b JK |
3695 | struct bpf_verifier_state_list *new_sl; |
3696 | struct bpf_verifier_state_list *sl; | |
638f5b90 | 3697 | struct bpf_verifier_state *cur = env->cur_state; |
1969db47 | 3698 | int i, err; |
f1bca824 AS |
3699 | |
3700 | sl = env->explored_states[insn_idx]; | |
3701 | if (!sl) | |
3702 | /* this 'insn_idx' instruction wasn't marked, so we will not | |
3703 | * be doing state search here | |
3704 | */ | |
3705 | return 0; | |
3706 | ||
3707 | while (sl != STATE_LIST_MARK) { | |
638f5b90 | 3708 | if (states_equal(env, &sl->state, cur)) { |
f1bca824 | 3709 | /* reached equivalent register/stack state, |
dc503a8a EC |
3710 | * prune the search. |
3711 | * Registers read by the continuation are read by us. | |
8e9cd9ce EC |
3712 | * If we have any write marks in env->cur_state, they |
3713 | * will prevent corresponding reads in the continuation | |
3714 | * from reaching our parent (an explored_state). Our | |
3715 | * own state will get the read marks recorded, but | |
3716 | * they'll be immediately forgotten as we're pruning | |
3717 | * this state and will pop a new one. | |
f1bca824 | 3718 | */ |
638f5b90 | 3719 | propagate_liveness(&sl->state, cur); |
f1bca824 | 3720 | return 1; |
dc503a8a | 3721 | } |
f1bca824 AS |
3722 | sl = sl->next; |
3723 | } | |
3724 | ||
3725 | /* there were no equivalent states, remember current one. | |
3726 | * technically the current state is not proven to be safe yet, | |
3727 | * but it will either reach bpf_exit (which means it's safe) or | |
3728 | * it will be rejected. Since there are no loops, we won't be | |
3729 | * seeing this 'insn_idx' instruction again on the way to bpf_exit | |
3730 | */ | |
638f5b90 | 3731 | new_sl = kzalloc(sizeof(struct bpf_verifier_state_list), GFP_KERNEL); |
f1bca824 AS |
3732 | if (!new_sl) |
3733 | return -ENOMEM; | |
3734 | ||
3735 | /* add new state to the head of linked list */ | |
1969db47 AS |
3736 | err = copy_verifier_state(&new_sl->state, cur); |
3737 | if (err) { | |
3738 | free_verifier_state(&new_sl->state, false); | |
3739 | kfree(new_sl); | |
3740 | return err; | |
3741 | } | |
f1bca824 AS |
3742 | new_sl->next = env->explored_states[insn_idx]; |
3743 | env->explored_states[insn_idx] = new_sl; | |
dc503a8a | 3744 | /* connect new state to parentage chain */ |
638f5b90 | 3745 | cur->parent = &new_sl->state; |
8e9cd9ce EC |
3746 | /* clear write marks in current state: the writes we did are not writes |
3747 | * our child did, so they don't screen off its reads from us. | |
3748 | * (There are no read marks in current state, because reads always mark | |
3749 | * their parent and current state never has children yet. Only | |
3750 | * explored_states can get read marks.) | |
3751 | */ | |
dc503a8a | 3752 | for (i = 0; i < BPF_REG_FP; i++) |
638f5b90 AS |
3753 | cur->regs[i].live = REG_LIVE_NONE; |
3754 | for (i = 0; i < cur->allocated_stack / BPF_REG_SIZE; i++) | |
3755 | if (cur->stack[i].slot_type[0] == STACK_SPILL) | |
3756 | cur->stack[i].spilled_ptr.live = REG_LIVE_NONE; | |
f1bca824 AS |
3757 | return 0; |
3758 | } | |
3759 | ||
13a27dfc JK |
3760 | static int ext_analyzer_insn_hook(struct bpf_verifier_env *env, |
3761 | int insn_idx, int prev_insn_idx) | |
3762 | { | |
ab3f0063 JK |
3763 | if (env->dev_ops && env->dev_ops->insn_hook) |
3764 | return env->dev_ops->insn_hook(env, insn_idx, prev_insn_idx); | |
13a27dfc | 3765 | |
ab3f0063 | 3766 | return 0; |
13a27dfc JK |
3767 | } |
3768 | ||
58e2af8b | 3769 | static int do_check(struct bpf_verifier_env *env) |
17a52670 | 3770 | { |
638f5b90 | 3771 | struct bpf_verifier_state *state; |
17a52670 | 3772 | struct bpf_insn *insns = env->prog->insnsi; |
638f5b90 | 3773 | struct bpf_reg_state *regs; |
17a52670 AS |
3774 | int insn_cnt = env->prog->len; |
3775 | int insn_idx, prev_insn_idx = 0; | |
3776 | int insn_processed = 0; | |
3777 | bool do_print_state = false; | |
3778 | ||
638f5b90 AS |
3779 | state = kzalloc(sizeof(struct bpf_verifier_state), GFP_KERNEL); |
3780 | if (!state) | |
3781 | return -ENOMEM; | |
3782 | env->cur_state = state; | |
3783 | init_reg_state(env, state->regs); | |
dc503a8a | 3784 | state->parent = NULL; |
17a52670 AS |
3785 | insn_idx = 0; |
3786 | for (;;) { | |
3787 | struct bpf_insn *insn; | |
3788 | u8 class; | |
3789 | int err; | |
3790 | ||
3791 | if (insn_idx >= insn_cnt) { | |
61bd5218 | 3792 | verbose(env, "invalid insn idx %d insn_cnt %d\n", |
17a52670 AS |
3793 | insn_idx, insn_cnt); |
3794 | return -EFAULT; | |
3795 | } | |
3796 | ||
3797 | insn = &insns[insn_idx]; | |
3798 | class = BPF_CLASS(insn->code); | |
3799 | ||
07016151 | 3800 | if (++insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) { |
61bd5218 JK |
3801 | verbose(env, |
3802 | "BPF program is too large. Processed %d insn\n", | |
17a52670 AS |
3803 | insn_processed); |
3804 | return -E2BIG; | |
3805 | } | |
3806 | ||
f1bca824 AS |
3807 | err = is_state_visited(env, insn_idx); |
3808 | if (err < 0) | |
3809 | return err; | |
3810 | if (err == 1) { | |
3811 | /* found equivalent state, can prune the search */ | |
61bd5218 | 3812 | if (env->log.level) { |
f1bca824 | 3813 | if (do_print_state) |
61bd5218 | 3814 | verbose(env, "\nfrom %d to %d: safe\n", |
f1bca824 AS |
3815 | prev_insn_idx, insn_idx); |
3816 | else | |
61bd5218 | 3817 | verbose(env, "%d: safe\n", insn_idx); |
f1bca824 AS |
3818 | } |
3819 | goto process_bpf_exit; | |
3820 | } | |
3821 | ||
3c2ce60b DB |
3822 | if (need_resched()) |
3823 | cond_resched(); | |
3824 | ||
61bd5218 JK |
3825 | if (env->log.level > 1 || (env->log.level && do_print_state)) { |
3826 | if (env->log.level > 1) | |
3827 | verbose(env, "%d:", insn_idx); | |
c5fc9692 | 3828 | else |
61bd5218 | 3829 | verbose(env, "\nfrom %d to %d:", |
c5fc9692 | 3830 | prev_insn_idx, insn_idx); |
638f5b90 | 3831 | print_verifier_state(env, state); |
17a52670 AS |
3832 | do_print_state = false; |
3833 | } | |
3834 | ||
61bd5218 JK |
3835 | if (env->log.level) { |
3836 | verbose(env, "%d: ", insn_idx); | |
f4ac7e0b JK |
3837 | print_bpf_insn(verbose, env, insn, |
3838 | env->allow_ptr_leaks); | |
17a52670 AS |
3839 | } |
3840 | ||
13a27dfc JK |
3841 | err = ext_analyzer_insn_hook(env, insn_idx, prev_insn_idx); |
3842 | if (err) | |
3843 | return err; | |
3844 | ||
638f5b90 | 3845 | regs = cur_regs(env); |
c131187d | 3846 | env->insn_aux_data[insn_idx].seen = true; |
17a52670 | 3847 | if (class == BPF_ALU || class == BPF_ALU64) { |
1be7f75d | 3848 | err = check_alu_op(env, insn); |
17a52670 AS |
3849 | if (err) |
3850 | return err; | |
3851 | ||
3852 | } else if (class == BPF_LDX) { | |
3df126f3 | 3853 | enum bpf_reg_type *prev_src_type, src_reg_type; |
9bac3d6d AS |
3854 | |
3855 | /* check for reserved fields is already done */ | |
3856 | ||
17a52670 | 3857 | /* check src operand */ |
dc503a8a | 3858 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
3859 | if (err) |
3860 | return err; | |
3861 | ||
dc503a8a | 3862 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
17a52670 AS |
3863 | if (err) |
3864 | return err; | |
3865 | ||
725f9dcd AS |
3866 | src_reg_type = regs[insn->src_reg].type; |
3867 | ||
17a52670 AS |
3868 | /* check that memory (src_reg + off) is readable, |
3869 | * the state of dst_reg will be updated by this func | |
3870 | */ | |
31fd8581 | 3871 | err = check_mem_access(env, insn_idx, insn->src_reg, insn->off, |
17a52670 AS |
3872 | BPF_SIZE(insn->code), BPF_READ, |
3873 | insn->dst_reg); | |
3874 | if (err) | |
3875 | return err; | |
3876 | ||
3df126f3 JK |
3877 | prev_src_type = &env->insn_aux_data[insn_idx].ptr_type; |
3878 | ||
3879 | if (*prev_src_type == NOT_INIT) { | |
9bac3d6d AS |
3880 | /* saw a valid insn |
3881 | * dst_reg = *(u32 *)(src_reg + off) | |
3df126f3 | 3882 | * save type to validate intersecting paths |
9bac3d6d | 3883 | */ |
3df126f3 | 3884 | *prev_src_type = src_reg_type; |
9bac3d6d | 3885 | |
3df126f3 | 3886 | } else if (src_reg_type != *prev_src_type && |
9bac3d6d | 3887 | (src_reg_type == PTR_TO_CTX || |
3df126f3 | 3888 | *prev_src_type == PTR_TO_CTX)) { |
9bac3d6d AS |
3889 | /* ABuser program is trying to use the same insn |
3890 | * dst_reg = *(u32*) (src_reg + off) | |
3891 | * with different pointer types: | |
3892 | * src_reg == ctx in one branch and | |
3893 | * src_reg == stack|map in some other branch. | |
3894 | * Reject it. | |
3895 | */ | |
61bd5218 | 3896 | verbose(env, "same insn cannot be used with different pointers\n"); |
9bac3d6d AS |
3897 | return -EINVAL; |
3898 | } | |
3899 | ||
17a52670 | 3900 | } else if (class == BPF_STX) { |
3df126f3 | 3901 | enum bpf_reg_type *prev_dst_type, dst_reg_type; |
d691f9e8 | 3902 | |
17a52670 | 3903 | if (BPF_MODE(insn->code) == BPF_XADD) { |
31fd8581 | 3904 | err = check_xadd(env, insn_idx, insn); |
17a52670 AS |
3905 | if (err) |
3906 | return err; | |
3907 | insn_idx++; | |
3908 | continue; | |
3909 | } | |
3910 | ||
17a52670 | 3911 | /* check src1 operand */ |
dc503a8a | 3912 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
17a52670 AS |
3913 | if (err) |
3914 | return err; | |
3915 | /* check src2 operand */ | |
dc503a8a | 3916 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
3917 | if (err) |
3918 | return err; | |
3919 | ||
d691f9e8 AS |
3920 | dst_reg_type = regs[insn->dst_reg].type; |
3921 | ||
17a52670 | 3922 | /* check that memory (dst_reg + off) is writeable */ |
31fd8581 | 3923 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
17a52670 AS |
3924 | BPF_SIZE(insn->code), BPF_WRITE, |
3925 | insn->src_reg); | |
3926 | if (err) | |
3927 | return err; | |
3928 | ||
3df126f3 JK |
3929 | prev_dst_type = &env->insn_aux_data[insn_idx].ptr_type; |
3930 | ||
3931 | if (*prev_dst_type == NOT_INIT) { | |
3932 | *prev_dst_type = dst_reg_type; | |
3933 | } else if (dst_reg_type != *prev_dst_type && | |
d691f9e8 | 3934 | (dst_reg_type == PTR_TO_CTX || |
3df126f3 | 3935 | *prev_dst_type == PTR_TO_CTX)) { |
61bd5218 | 3936 | verbose(env, "same insn cannot be used with different pointers\n"); |
d691f9e8 AS |
3937 | return -EINVAL; |
3938 | } | |
3939 | ||
17a52670 AS |
3940 | } else if (class == BPF_ST) { |
3941 | if (BPF_MODE(insn->code) != BPF_MEM || | |
3942 | insn->src_reg != BPF_REG_0) { | |
61bd5218 | 3943 | verbose(env, "BPF_ST uses reserved fields\n"); |
17a52670 AS |
3944 | return -EINVAL; |
3945 | } | |
3946 | /* check src operand */ | |
dc503a8a | 3947 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
17a52670 AS |
3948 | if (err) |
3949 | return err; | |
3950 | ||
3951 | /* check that memory (dst_reg + off) is writeable */ | |
31fd8581 | 3952 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
17a52670 AS |
3953 | BPF_SIZE(insn->code), BPF_WRITE, |
3954 | -1); | |
3955 | if (err) | |
3956 | return err; | |
3957 | ||
3958 | } else if (class == BPF_JMP) { | |
3959 | u8 opcode = BPF_OP(insn->code); | |
3960 | ||
3961 | if (opcode == BPF_CALL) { | |
3962 | if (BPF_SRC(insn->code) != BPF_K || | |
3963 | insn->off != 0 || | |
3964 | insn->src_reg != BPF_REG_0 || | |
3965 | insn->dst_reg != BPF_REG_0) { | |
61bd5218 | 3966 | verbose(env, "BPF_CALL uses reserved fields\n"); |
17a52670 AS |
3967 | return -EINVAL; |
3968 | } | |
3969 | ||
81ed18ab | 3970 | err = check_call(env, insn->imm, insn_idx); |
17a52670 AS |
3971 | if (err) |
3972 | return err; | |
3973 | ||
3974 | } else if (opcode == BPF_JA) { | |
3975 | if (BPF_SRC(insn->code) != BPF_K || | |
3976 | insn->imm != 0 || | |
3977 | insn->src_reg != BPF_REG_0 || | |
3978 | insn->dst_reg != BPF_REG_0) { | |
61bd5218 | 3979 | verbose(env, "BPF_JA uses reserved fields\n"); |
17a52670 AS |
3980 | return -EINVAL; |
3981 | } | |
3982 | ||
3983 | insn_idx += insn->off + 1; | |
3984 | continue; | |
3985 | ||
3986 | } else if (opcode == BPF_EXIT) { | |
3987 | if (BPF_SRC(insn->code) != BPF_K || | |
3988 | insn->imm != 0 || | |
3989 | insn->src_reg != BPF_REG_0 || | |
3990 | insn->dst_reg != BPF_REG_0) { | |
61bd5218 | 3991 | verbose(env, "BPF_EXIT uses reserved fields\n"); |
17a52670 AS |
3992 | return -EINVAL; |
3993 | } | |
3994 | ||
3995 | /* eBPF calling convetion is such that R0 is used | |
3996 | * to return the value from eBPF program. | |
3997 | * Make sure that it's readable at this time | |
3998 | * of bpf_exit, which means that program wrote | |
3999 | * something into it earlier | |
4000 | */ | |
dc503a8a | 4001 | err = check_reg_arg(env, BPF_REG_0, SRC_OP); |
17a52670 AS |
4002 | if (err) |
4003 | return err; | |
4004 | ||
1be7f75d | 4005 | if (is_pointer_value(env, BPF_REG_0)) { |
61bd5218 | 4006 | verbose(env, "R0 leaks addr as return value\n"); |
1be7f75d AS |
4007 | return -EACCES; |
4008 | } | |
4009 | ||
390ee7e2 AS |
4010 | err = check_return_code(env); |
4011 | if (err) | |
4012 | return err; | |
f1bca824 | 4013 | process_bpf_exit: |
638f5b90 AS |
4014 | err = pop_stack(env, &prev_insn_idx, &insn_idx); |
4015 | if (err < 0) { | |
4016 | if (err != -ENOENT) | |
4017 | return err; | |
17a52670 AS |
4018 | break; |
4019 | } else { | |
4020 | do_print_state = true; | |
4021 | continue; | |
4022 | } | |
4023 | } else { | |
4024 | err = check_cond_jmp_op(env, insn, &insn_idx); | |
4025 | if (err) | |
4026 | return err; | |
4027 | } | |
4028 | } else if (class == BPF_LD) { | |
4029 | u8 mode = BPF_MODE(insn->code); | |
4030 | ||
4031 | if (mode == BPF_ABS || mode == BPF_IND) { | |
ddd872bc AS |
4032 | err = check_ld_abs(env, insn); |
4033 | if (err) | |
4034 | return err; | |
4035 | ||
17a52670 AS |
4036 | } else if (mode == BPF_IMM) { |
4037 | err = check_ld_imm(env, insn); | |
4038 | if (err) | |
4039 | return err; | |
4040 | ||
4041 | insn_idx++; | |
c131187d | 4042 | env->insn_aux_data[insn_idx].seen = true; |
17a52670 | 4043 | } else { |
61bd5218 | 4044 | verbose(env, "invalid BPF_LD mode\n"); |
17a52670 AS |
4045 | return -EINVAL; |
4046 | } | |
4047 | } else { | |
61bd5218 | 4048 | verbose(env, "unknown insn class %d\n", class); |
17a52670 AS |
4049 | return -EINVAL; |
4050 | } | |
4051 | ||
4052 | insn_idx++; | |
4053 | } | |
4054 | ||
61bd5218 JK |
4055 | verbose(env, "processed %d insns, stack depth %d\n", insn_processed, |
4056 | env->prog->aux->stack_depth); | |
17a52670 AS |
4057 | return 0; |
4058 | } | |
4059 | ||
56f668df MKL |
4060 | static int check_map_prealloc(struct bpf_map *map) |
4061 | { | |
4062 | return (map->map_type != BPF_MAP_TYPE_HASH && | |
bcc6b1b7 MKL |
4063 | map->map_type != BPF_MAP_TYPE_PERCPU_HASH && |
4064 | map->map_type != BPF_MAP_TYPE_HASH_OF_MAPS) || | |
56f668df MKL |
4065 | !(map->map_flags & BPF_F_NO_PREALLOC); |
4066 | } | |
4067 | ||
61bd5218 JK |
4068 | static int check_map_prog_compatibility(struct bpf_verifier_env *env, |
4069 | struct bpf_map *map, | |
fdc15d38 AS |
4070 | struct bpf_prog *prog) |
4071 | ||
4072 | { | |
56f668df MKL |
4073 | /* Make sure that BPF_PROG_TYPE_PERF_EVENT programs only use |
4074 | * preallocated hash maps, since doing memory allocation | |
4075 | * in overflow_handler can crash depending on where nmi got | |
4076 | * triggered. | |
4077 | */ | |
4078 | if (prog->type == BPF_PROG_TYPE_PERF_EVENT) { | |
4079 | if (!check_map_prealloc(map)) { | |
61bd5218 | 4080 | verbose(env, "perf_event programs can only use preallocated hash map\n"); |
56f668df MKL |
4081 | return -EINVAL; |
4082 | } | |
4083 | if (map->inner_map_meta && | |
4084 | !check_map_prealloc(map->inner_map_meta)) { | |
61bd5218 | 4085 | verbose(env, "perf_event programs can only use preallocated inner hash map\n"); |
56f668df MKL |
4086 | return -EINVAL; |
4087 | } | |
fdc15d38 AS |
4088 | } |
4089 | return 0; | |
4090 | } | |
4091 | ||
0246e64d AS |
4092 | /* look for pseudo eBPF instructions that access map FDs and |
4093 | * replace them with actual map pointers | |
4094 | */ | |
58e2af8b | 4095 | static int replace_map_fd_with_map_ptr(struct bpf_verifier_env *env) |
0246e64d AS |
4096 | { |
4097 | struct bpf_insn *insn = env->prog->insnsi; | |
4098 | int insn_cnt = env->prog->len; | |
fdc15d38 | 4099 | int i, j, err; |
0246e64d | 4100 | |
f1f7714e | 4101 | err = bpf_prog_calc_tag(env->prog); |
aafe6ae9 DB |
4102 | if (err) |
4103 | return err; | |
4104 | ||
0246e64d | 4105 | for (i = 0; i < insn_cnt; i++, insn++) { |
9bac3d6d | 4106 | if (BPF_CLASS(insn->code) == BPF_LDX && |
d691f9e8 | 4107 | (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) { |
61bd5218 | 4108 | verbose(env, "BPF_LDX uses reserved fields\n"); |
9bac3d6d AS |
4109 | return -EINVAL; |
4110 | } | |
4111 | ||
d691f9e8 AS |
4112 | if (BPF_CLASS(insn->code) == BPF_STX && |
4113 | ((BPF_MODE(insn->code) != BPF_MEM && | |
4114 | BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) { | |
61bd5218 | 4115 | verbose(env, "BPF_STX uses reserved fields\n"); |
d691f9e8 AS |
4116 | return -EINVAL; |
4117 | } | |
4118 | ||
0246e64d AS |
4119 | if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) { |
4120 | struct bpf_map *map; | |
4121 | struct fd f; | |
4122 | ||
4123 | if (i == insn_cnt - 1 || insn[1].code != 0 || | |
4124 | insn[1].dst_reg != 0 || insn[1].src_reg != 0 || | |
4125 | insn[1].off != 0) { | |
61bd5218 | 4126 | verbose(env, "invalid bpf_ld_imm64 insn\n"); |
0246e64d AS |
4127 | return -EINVAL; |
4128 | } | |
4129 | ||
4130 | if (insn->src_reg == 0) | |
4131 | /* valid generic load 64-bit imm */ | |
4132 | goto next_insn; | |
4133 | ||
4134 | if (insn->src_reg != BPF_PSEUDO_MAP_FD) { | |
61bd5218 JK |
4135 | verbose(env, |
4136 | "unrecognized bpf_ld_imm64 insn\n"); | |
0246e64d AS |
4137 | return -EINVAL; |
4138 | } | |
4139 | ||
4140 | f = fdget(insn->imm); | |
c2101297 | 4141 | map = __bpf_map_get(f); |
0246e64d | 4142 | if (IS_ERR(map)) { |
61bd5218 | 4143 | verbose(env, "fd %d is not pointing to valid bpf_map\n", |
0246e64d | 4144 | insn->imm); |
0246e64d AS |
4145 | return PTR_ERR(map); |
4146 | } | |
4147 | ||
61bd5218 | 4148 | err = check_map_prog_compatibility(env, map, env->prog); |
fdc15d38 AS |
4149 | if (err) { |
4150 | fdput(f); | |
4151 | return err; | |
4152 | } | |
4153 | ||
0246e64d AS |
4154 | /* store map pointer inside BPF_LD_IMM64 instruction */ |
4155 | insn[0].imm = (u32) (unsigned long) map; | |
4156 | insn[1].imm = ((u64) (unsigned long) map) >> 32; | |
4157 | ||
4158 | /* check whether we recorded this map already */ | |
4159 | for (j = 0; j < env->used_map_cnt; j++) | |
4160 | if (env->used_maps[j] == map) { | |
4161 | fdput(f); | |
4162 | goto next_insn; | |
4163 | } | |
4164 | ||
4165 | if (env->used_map_cnt >= MAX_USED_MAPS) { | |
4166 | fdput(f); | |
4167 | return -E2BIG; | |
4168 | } | |
4169 | ||
0246e64d AS |
4170 | /* hold the map. If the program is rejected by verifier, |
4171 | * the map will be released by release_maps() or it | |
4172 | * will be used by the valid program until it's unloaded | |
4173 | * and all maps are released in free_bpf_prog_info() | |
4174 | */ | |
92117d84 AS |
4175 | map = bpf_map_inc(map, false); |
4176 | if (IS_ERR(map)) { | |
4177 | fdput(f); | |
4178 | return PTR_ERR(map); | |
4179 | } | |
4180 | env->used_maps[env->used_map_cnt++] = map; | |
4181 | ||
0246e64d AS |
4182 | fdput(f); |
4183 | next_insn: | |
4184 | insn++; | |
4185 | i++; | |
4186 | } | |
4187 | } | |
4188 | ||
4189 | /* now all pseudo BPF_LD_IMM64 instructions load valid | |
4190 | * 'struct bpf_map *' into a register instead of user map_fd. | |
4191 | * These pointers will be used later by verifier to validate map access. | |
4192 | */ | |
4193 | return 0; | |
4194 | } | |
4195 | ||
4196 | /* drop refcnt of maps used by the rejected program */ | |
58e2af8b | 4197 | static void release_maps(struct bpf_verifier_env *env) |
0246e64d AS |
4198 | { |
4199 | int i; | |
4200 | ||
4201 | for (i = 0; i < env->used_map_cnt; i++) | |
4202 | bpf_map_put(env->used_maps[i]); | |
4203 | } | |
4204 | ||
4205 | /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */ | |
58e2af8b | 4206 | static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env) |
0246e64d AS |
4207 | { |
4208 | struct bpf_insn *insn = env->prog->insnsi; | |
4209 | int insn_cnt = env->prog->len; | |
4210 | int i; | |
4211 | ||
4212 | for (i = 0; i < insn_cnt; i++, insn++) | |
4213 | if (insn->code == (BPF_LD | BPF_IMM | BPF_DW)) | |
4214 | insn->src_reg = 0; | |
4215 | } | |
4216 | ||
8041902d AS |
4217 | /* single env->prog->insni[off] instruction was replaced with the range |
4218 | * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying | |
4219 | * [0, off) and [off, end) to new locations, so the patched range stays zero | |
4220 | */ | |
4221 | static int adjust_insn_aux_data(struct bpf_verifier_env *env, u32 prog_len, | |
4222 | u32 off, u32 cnt) | |
4223 | { | |
4224 | struct bpf_insn_aux_data *new_data, *old_data = env->insn_aux_data; | |
c131187d | 4225 | int i; |
8041902d AS |
4226 | |
4227 | if (cnt == 1) | |
4228 | return 0; | |
4229 | new_data = vzalloc(sizeof(struct bpf_insn_aux_data) * prog_len); | |
4230 | if (!new_data) | |
4231 | return -ENOMEM; | |
4232 | memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off); | |
4233 | memcpy(new_data + off + cnt - 1, old_data + off, | |
4234 | sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1)); | |
c131187d AS |
4235 | for (i = off; i < off + cnt - 1; i++) |
4236 | new_data[i].seen = true; | |
8041902d AS |
4237 | env->insn_aux_data = new_data; |
4238 | vfree(old_data); | |
4239 | return 0; | |
4240 | } | |
4241 | ||
4242 | static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off, | |
4243 | const struct bpf_insn *patch, u32 len) | |
4244 | { | |
4245 | struct bpf_prog *new_prog; | |
4246 | ||
4247 | new_prog = bpf_patch_insn_single(env->prog, off, patch, len); | |
4248 | if (!new_prog) | |
4249 | return NULL; | |
4250 | if (adjust_insn_aux_data(env, new_prog->len, off, len)) | |
4251 | return NULL; | |
4252 | return new_prog; | |
4253 | } | |
4254 | ||
c131187d AS |
4255 | /* The verifier does more data flow analysis than llvm and will not explore |
4256 | * branches that are dead at run time. Malicious programs can have dead code | |
4257 | * too. Therefore replace all dead at-run-time code with nops. | |
4258 | */ | |
4259 | static void sanitize_dead_code(struct bpf_verifier_env *env) | |
4260 | { | |
4261 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; | |
4262 | struct bpf_insn nop = BPF_MOV64_REG(BPF_REG_0, BPF_REG_0); | |
4263 | struct bpf_insn *insn = env->prog->insnsi; | |
4264 | const int insn_cnt = env->prog->len; | |
4265 | int i; | |
4266 | ||
4267 | for (i = 0; i < insn_cnt; i++) { | |
4268 | if (aux_data[i].seen) | |
4269 | continue; | |
4270 | memcpy(insn + i, &nop, sizeof(nop)); | |
4271 | } | |
4272 | } | |
4273 | ||
9bac3d6d AS |
4274 | /* convert load instructions that access fields of 'struct __sk_buff' |
4275 | * into sequence of instructions that access fields of 'struct sk_buff' | |
4276 | */ | |
58e2af8b | 4277 | static int convert_ctx_accesses(struct bpf_verifier_env *env) |
9bac3d6d | 4278 | { |
00176a34 | 4279 | const struct bpf_verifier_ops *ops = env->ops; |
f96da094 | 4280 | int i, cnt, size, ctx_field_size, delta = 0; |
3df126f3 | 4281 | const int insn_cnt = env->prog->len; |
36bbef52 | 4282 | struct bpf_insn insn_buf[16], *insn; |
9bac3d6d | 4283 | struct bpf_prog *new_prog; |
d691f9e8 | 4284 | enum bpf_access_type type; |
f96da094 DB |
4285 | bool is_narrower_load; |
4286 | u32 target_size; | |
9bac3d6d | 4287 | |
36bbef52 DB |
4288 | if (ops->gen_prologue) { |
4289 | cnt = ops->gen_prologue(insn_buf, env->seen_direct_write, | |
4290 | env->prog); | |
4291 | if (cnt >= ARRAY_SIZE(insn_buf)) { | |
61bd5218 | 4292 | verbose(env, "bpf verifier is misconfigured\n"); |
36bbef52 DB |
4293 | return -EINVAL; |
4294 | } else if (cnt) { | |
8041902d | 4295 | new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt); |
36bbef52 DB |
4296 | if (!new_prog) |
4297 | return -ENOMEM; | |
8041902d | 4298 | |
36bbef52 | 4299 | env->prog = new_prog; |
3df126f3 | 4300 | delta += cnt - 1; |
36bbef52 DB |
4301 | } |
4302 | } | |
4303 | ||
4304 | if (!ops->convert_ctx_access) | |
9bac3d6d AS |
4305 | return 0; |
4306 | ||
3df126f3 | 4307 | insn = env->prog->insnsi + delta; |
36bbef52 | 4308 | |
9bac3d6d | 4309 | for (i = 0; i < insn_cnt; i++, insn++) { |
62c7989b DB |
4310 | if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) || |
4311 | insn->code == (BPF_LDX | BPF_MEM | BPF_H) || | |
4312 | insn->code == (BPF_LDX | BPF_MEM | BPF_W) || | |
ea2e7ce5 | 4313 | insn->code == (BPF_LDX | BPF_MEM | BPF_DW)) |
d691f9e8 | 4314 | type = BPF_READ; |
62c7989b DB |
4315 | else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) || |
4316 | insn->code == (BPF_STX | BPF_MEM | BPF_H) || | |
4317 | insn->code == (BPF_STX | BPF_MEM | BPF_W) || | |
ea2e7ce5 | 4318 | insn->code == (BPF_STX | BPF_MEM | BPF_DW)) |
d691f9e8 AS |
4319 | type = BPF_WRITE; |
4320 | else | |
9bac3d6d AS |
4321 | continue; |
4322 | ||
8041902d | 4323 | if (env->insn_aux_data[i + delta].ptr_type != PTR_TO_CTX) |
9bac3d6d | 4324 | continue; |
9bac3d6d | 4325 | |
31fd8581 | 4326 | ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size; |
f96da094 | 4327 | size = BPF_LDST_BYTES(insn); |
31fd8581 YS |
4328 | |
4329 | /* If the read access is a narrower load of the field, | |
4330 | * convert to a 4/8-byte load, to minimum program type specific | |
4331 | * convert_ctx_access changes. If conversion is successful, | |
4332 | * we will apply proper mask to the result. | |
4333 | */ | |
f96da094 | 4334 | is_narrower_load = size < ctx_field_size; |
31fd8581 | 4335 | if (is_narrower_load) { |
f96da094 DB |
4336 | u32 off = insn->off; |
4337 | u8 size_code; | |
4338 | ||
4339 | if (type == BPF_WRITE) { | |
61bd5218 | 4340 | verbose(env, "bpf verifier narrow ctx access misconfigured\n"); |
f96da094 DB |
4341 | return -EINVAL; |
4342 | } | |
31fd8581 | 4343 | |
f96da094 | 4344 | size_code = BPF_H; |
31fd8581 YS |
4345 | if (ctx_field_size == 4) |
4346 | size_code = BPF_W; | |
4347 | else if (ctx_field_size == 8) | |
4348 | size_code = BPF_DW; | |
f96da094 | 4349 | |
31fd8581 YS |
4350 | insn->off = off & ~(ctx_field_size - 1); |
4351 | insn->code = BPF_LDX | BPF_MEM | size_code; | |
4352 | } | |
f96da094 DB |
4353 | |
4354 | target_size = 0; | |
4355 | cnt = ops->convert_ctx_access(type, insn, insn_buf, env->prog, | |
4356 | &target_size); | |
4357 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) || | |
4358 | (ctx_field_size && !target_size)) { | |
61bd5218 | 4359 | verbose(env, "bpf verifier is misconfigured\n"); |
9bac3d6d AS |
4360 | return -EINVAL; |
4361 | } | |
f96da094 DB |
4362 | |
4363 | if (is_narrower_load && size < target_size) { | |
31fd8581 YS |
4364 | if (ctx_field_size <= 4) |
4365 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg, | |
f96da094 | 4366 | (1 << size * 8) - 1); |
31fd8581 YS |
4367 | else |
4368 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg, | |
f96da094 | 4369 | (1 << size * 8) - 1); |
31fd8581 | 4370 | } |
9bac3d6d | 4371 | |
8041902d | 4372 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
9bac3d6d AS |
4373 | if (!new_prog) |
4374 | return -ENOMEM; | |
4375 | ||
3df126f3 | 4376 | delta += cnt - 1; |
9bac3d6d AS |
4377 | |
4378 | /* keep walking new program and skip insns we just inserted */ | |
4379 | env->prog = new_prog; | |
3df126f3 | 4380 | insn = new_prog->insnsi + i + delta; |
9bac3d6d AS |
4381 | } |
4382 | ||
4383 | return 0; | |
4384 | } | |
4385 | ||
79741b3b | 4386 | /* fixup insn->imm field of bpf_call instructions |
81ed18ab | 4387 | * and inline eligible helpers as explicit sequence of BPF instructions |
e245c5c6 AS |
4388 | * |
4389 | * this function is called after eBPF program passed verification | |
4390 | */ | |
79741b3b | 4391 | static int fixup_bpf_calls(struct bpf_verifier_env *env) |
e245c5c6 | 4392 | { |
79741b3b AS |
4393 | struct bpf_prog *prog = env->prog; |
4394 | struct bpf_insn *insn = prog->insnsi; | |
e245c5c6 | 4395 | const struct bpf_func_proto *fn; |
79741b3b | 4396 | const int insn_cnt = prog->len; |
81ed18ab AS |
4397 | struct bpf_insn insn_buf[16]; |
4398 | struct bpf_prog *new_prog; | |
4399 | struct bpf_map *map_ptr; | |
4400 | int i, cnt, delta = 0; | |
e245c5c6 | 4401 | |
79741b3b AS |
4402 | for (i = 0; i < insn_cnt; i++, insn++) { |
4403 | if (insn->code != (BPF_JMP | BPF_CALL)) | |
4404 | continue; | |
e245c5c6 | 4405 | |
79741b3b AS |
4406 | if (insn->imm == BPF_FUNC_get_route_realm) |
4407 | prog->dst_needed = 1; | |
4408 | if (insn->imm == BPF_FUNC_get_prandom_u32) | |
4409 | bpf_user_rnd_init_once(); | |
79741b3b | 4410 | if (insn->imm == BPF_FUNC_tail_call) { |
7b9f6da1 DM |
4411 | /* If we tail call into other programs, we |
4412 | * cannot make any assumptions since they can | |
4413 | * be replaced dynamically during runtime in | |
4414 | * the program array. | |
4415 | */ | |
4416 | prog->cb_access = 1; | |
80a58d02 | 4417 | env->prog->aux->stack_depth = MAX_BPF_STACK; |
7b9f6da1 | 4418 | |
79741b3b AS |
4419 | /* mark bpf_tail_call as different opcode to avoid |
4420 | * conditional branch in the interpeter for every normal | |
4421 | * call and to prevent accidental JITing by JIT compiler | |
4422 | * that doesn't support bpf_tail_call yet | |
e245c5c6 | 4423 | */ |
79741b3b | 4424 | insn->imm = 0; |
71189fa9 | 4425 | insn->code = BPF_JMP | BPF_TAIL_CALL; |
79741b3b AS |
4426 | continue; |
4427 | } | |
e245c5c6 | 4428 | |
89c63074 DB |
4429 | /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup |
4430 | * handlers are currently limited to 64 bit only. | |
4431 | */ | |
4432 | if (ebpf_jit_enabled() && BITS_PER_LONG == 64 && | |
4433 | insn->imm == BPF_FUNC_map_lookup_elem) { | |
81ed18ab | 4434 | map_ptr = env->insn_aux_data[i + delta].map_ptr; |
fad73a1a MKL |
4435 | if (map_ptr == BPF_MAP_PTR_POISON || |
4436 | !map_ptr->ops->map_gen_lookup) | |
81ed18ab AS |
4437 | goto patch_call_imm; |
4438 | ||
4439 | cnt = map_ptr->ops->map_gen_lookup(map_ptr, insn_buf); | |
4440 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { | |
61bd5218 | 4441 | verbose(env, "bpf verifier is misconfigured\n"); |
81ed18ab AS |
4442 | return -EINVAL; |
4443 | } | |
4444 | ||
4445 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, | |
4446 | cnt); | |
4447 | if (!new_prog) | |
4448 | return -ENOMEM; | |
4449 | ||
4450 | delta += cnt - 1; | |
4451 | ||
4452 | /* keep walking new program and skip insns we just inserted */ | |
4453 | env->prog = prog = new_prog; | |
4454 | insn = new_prog->insnsi + i + delta; | |
4455 | continue; | |
4456 | } | |
4457 | ||
109980b8 | 4458 | if (insn->imm == BPF_FUNC_redirect_map) { |
7c300131 DB |
4459 | /* Note, we cannot use prog directly as imm as subsequent |
4460 | * rewrites would still change the prog pointer. The only | |
4461 | * stable address we can use is aux, which also works with | |
4462 | * prog clones during blinding. | |
4463 | */ | |
4464 | u64 addr = (unsigned long)prog->aux; | |
109980b8 DB |
4465 | struct bpf_insn r4_ld[] = { |
4466 | BPF_LD_IMM64(BPF_REG_4, addr), | |
4467 | *insn, | |
4468 | }; | |
4469 | cnt = ARRAY_SIZE(r4_ld); | |
4470 | ||
4471 | new_prog = bpf_patch_insn_data(env, i + delta, r4_ld, cnt); | |
4472 | if (!new_prog) | |
4473 | return -ENOMEM; | |
4474 | ||
4475 | delta += cnt - 1; | |
4476 | env->prog = prog = new_prog; | |
4477 | insn = new_prog->insnsi + i + delta; | |
4478 | } | |
81ed18ab | 4479 | patch_call_imm: |
00176a34 | 4480 | fn = env->ops->get_func_proto(insn->imm); |
79741b3b AS |
4481 | /* all functions that have prototype and verifier allowed |
4482 | * programs to call them, must be real in-kernel functions | |
4483 | */ | |
4484 | if (!fn->func) { | |
61bd5218 JK |
4485 | verbose(env, |
4486 | "kernel subsystem misconfigured func %s#%d\n", | |
79741b3b AS |
4487 | func_id_name(insn->imm), insn->imm); |
4488 | return -EFAULT; | |
e245c5c6 | 4489 | } |
79741b3b | 4490 | insn->imm = fn->func - __bpf_call_base; |
e245c5c6 | 4491 | } |
e245c5c6 | 4492 | |
79741b3b AS |
4493 | return 0; |
4494 | } | |
e245c5c6 | 4495 | |
58e2af8b | 4496 | static void free_states(struct bpf_verifier_env *env) |
f1bca824 | 4497 | { |
58e2af8b | 4498 | struct bpf_verifier_state_list *sl, *sln; |
f1bca824 AS |
4499 | int i; |
4500 | ||
4501 | if (!env->explored_states) | |
4502 | return; | |
4503 | ||
4504 | for (i = 0; i < env->prog->len; i++) { | |
4505 | sl = env->explored_states[i]; | |
4506 | ||
4507 | if (sl) | |
4508 | while (sl != STATE_LIST_MARK) { | |
4509 | sln = sl->next; | |
1969db47 | 4510 | free_verifier_state(&sl->state, false); |
f1bca824 AS |
4511 | kfree(sl); |
4512 | sl = sln; | |
4513 | } | |
4514 | } | |
4515 | ||
4516 | kfree(env->explored_states); | |
4517 | } | |
4518 | ||
9bac3d6d | 4519 | int bpf_check(struct bpf_prog **prog, union bpf_attr *attr) |
51580e79 | 4520 | { |
58e2af8b | 4521 | struct bpf_verifier_env *env; |
61bd5218 | 4522 | struct bpf_verifer_log *log; |
51580e79 AS |
4523 | int ret = -EINVAL; |
4524 | ||
eba0c929 AB |
4525 | /* no program is valid */ |
4526 | if (ARRAY_SIZE(bpf_verifier_ops) == 0) | |
4527 | return -EINVAL; | |
4528 | ||
58e2af8b | 4529 | /* 'struct bpf_verifier_env' can be global, but since it's not small, |
cbd35700 AS |
4530 | * allocate/free it every time bpf_check() is called |
4531 | */ | |
58e2af8b | 4532 | env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL); |
cbd35700 AS |
4533 | if (!env) |
4534 | return -ENOMEM; | |
61bd5218 | 4535 | log = &env->log; |
cbd35700 | 4536 | |
3df126f3 JK |
4537 | env->insn_aux_data = vzalloc(sizeof(struct bpf_insn_aux_data) * |
4538 | (*prog)->len); | |
4539 | ret = -ENOMEM; | |
4540 | if (!env->insn_aux_data) | |
4541 | goto err_free_env; | |
9bac3d6d | 4542 | env->prog = *prog; |
00176a34 | 4543 | env->ops = bpf_verifier_ops[env->prog->type]; |
0246e64d | 4544 | |
cbd35700 AS |
4545 | /* grab the mutex to protect few globals used by verifier */ |
4546 | mutex_lock(&bpf_verifier_lock); | |
4547 | ||
4548 | if (attr->log_level || attr->log_buf || attr->log_size) { | |
4549 | /* user requested verbose verifier output | |
4550 | * and supplied buffer to store the verification trace | |
4551 | */ | |
e7bf8249 JK |
4552 | log->level = attr->log_level; |
4553 | log->ubuf = (char __user *) (unsigned long) attr->log_buf; | |
4554 | log->len_total = attr->log_size; | |
cbd35700 AS |
4555 | |
4556 | ret = -EINVAL; | |
e7bf8249 JK |
4557 | /* log attributes have to be sane */ |
4558 | if (log->len_total < 128 || log->len_total > UINT_MAX >> 8 || | |
4559 | !log->level || !log->ubuf) | |
3df126f3 | 4560 | goto err_unlock; |
cbd35700 | 4561 | } |
1ad2f583 DB |
4562 | |
4563 | env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT); | |
4564 | if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) | |
e07b98d9 | 4565 | env->strict_alignment = true; |
cbd35700 | 4566 | |
ab3f0063 JK |
4567 | if (env->prog->aux->offload) { |
4568 | ret = bpf_prog_offload_verifier_prep(env); | |
4569 | if (ret) | |
4570 | goto err_unlock; | |
4571 | } | |
4572 | ||
0246e64d AS |
4573 | ret = replace_map_fd_with_map_ptr(env); |
4574 | if (ret < 0) | |
4575 | goto skip_full_check; | |
4576 | ||
9bac3d6d | 4577 | env->explored_states = kcalloc(env->prog->len, |
58e2af8b | 4578 | sizeof(struct bpf_verifier_state_list *), |
f1bca824 AS |
4579 | GFP_USER); |
4580 | ret = -ENOMEM; | |
4581 | if (!env->explored_states) | |
4582 | goto skip_full_check; | |
4583 | ||
475fb78f AS |
4584 | ret = check_cfg(env); |
4585 | if (ret < 0) | |
4586 | goto skip_full_check; | |
4587 | ||
1be7f75d AS |
4588 | env->allow_ptr_leaks = capable(CAP_SYS_ADMIN); |
4589 | ||
17a52670 | 4590 | ret = do_check(env); |
8c01c4f8 CG |
4591 | if (env->cur_state) { |
4592 | free_verifier_state(env->cur_state, true); | |
4593 | env->cur_state = NULL; | |
4594 | } | |
cbd35700 | 4595 | |
0246e64d | 4596 | skip_full_check: |
638f5b90 | 4597 | while (!pop_stack(env, NULL, NULL)); |
f1bca824 | 4598 | free_states(env); |
0246e64d | 4599 | |
c131187d AS |
4600 | if (ret == 0) |
4601 | sanitize_dead_code(env); | |
4602 | ||
9bac3d6d AS |
4603 | if (ret == 0) |
4604 | /* program is valid, convert *(u32*)(ctx + off) accesses */ | |
4605 | ret = convert_ctx_accesses(env); | |
4606 | ||
e245c5c6 | 4607 | if (ret == 0) |
79741b3b | 4608 | ret = fixup_bpf_calls(env); |
e245c5c6 | 4609 | |
a2a7d570 | 4610 | if (log->level && bpf_verifier_log_full(log)) |
cbd35700 | 4611 | ret = -ENOSPC; |
a2a7d570 | 4612 | if (log->level && !log->ubuf) { |
cbd35700 | 4613 | ret = -EFAULT; |
a2a7d570 | 4614 | goto err_release_maps; |
cbd35700 AS |
4615 | } |
4616 | ||
0246e64d AS |
4617 | if (ret == 0 && env->used_map_cnt) { |
4618 | /* if program passed verifier, update used_maps in bpf_prog_info */ | |
9bac3d6d AS |
4619 | env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt, |
4620 | sizeof(env->used_maps[0]), | |
4621 | GFP_KERNEL); | |
0246e64d | 4622 | |
9bac3d6d | 4623 | if (!env->prog->aux->used_maps) { |
0246e64d | 4624 | ret = -ENOMEM; |
a2a7d570 | 4625 | goto err_release_maps; |
0246e64d AS |
4626 | } |
4627 | ||
9bac3d6d | 4628 | memcpy(env->prog->aux->used_maps, env->used_maps, |
0246e64d | 4629 | sizeof(env->used_maps[0]) * env->used_map_cnt); |
9bac3d6d | 4630 | env->prog->aux->used_map_cnt = env->used_map_cnt; |
0246e64d AS |
4631 | |
4632 | /* program is valid. Convert pseudo bpf_ld_imm64 into generic | |
4633 | * bpf_ld_imm64 instructions | |
4634 | */ | |
4635 | convert_pseudo_ld_imm64(env); | |
4636 | } | |
cbd35700 | 4637 | |
a2a7d570 | 4638 | err_release_maps: |
9bac3d6d | 4639 | if (!env->prog->aux->used_maps) |
0246e64d AS |
4640 | /* if we didn't copy map pointers into bpf_prog_info, release |
4641 | * them now. Otherwise free_bpf_prog_info() will release them. | |
4642 | */ | |
4643 | release_maps(env); | |
9bac3d6d | 4644 | *prog = env->prog; |
3df126f3 | 4645 | err_unlock: |
cbd35700 | 4646 | mutex_unlock(&bpf_verifier_lock); |
3df126f3 JK |
4647 | vfree(env->insn_aux_data); |
4648 | err_free_env: | |
4649 | kfree(env); | |
51580e79 AS |
4650 | return ret; |
4651 | } |