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