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