xref: /openbmc/linux/include/linux/bpf_verifier.h (revision bfc5c19b)
125763b3cSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
258e2af8bSJakub Kicinski /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
358e2af8bSJakub Kicinski  */
458e2af8bSJakub Kicinski #ifndef _LINUX_BPF_VERIFIER_H
558e2af8bSJakub Kicinski #define _LINUX_BPF_VERIFIER_H 1
658e2af8bSJakub Kicinski 
758e2af8bSJakub Kicinski #include <linux/bpf.h> /* for enum bpf_reg_type */
822dc4a0fSAndrii Nakryiko #include <linux/btf.h> /* for struct btf and btf_id() */
958e2af8bSJakub Kicinski #include <linux/filter.h> /* for MAX_BPF_STACK */
10f1174f77SEdward Cree #include <linux/tnum.h>
1158e2af8bSJakub Kicinski 
12b03c9f9fSEdward Cree /* Maximum variable offset umax_value permitted when resolving memory accesses.
13b03c9f9fSEdward Cree  * In practice this is far bigger than any realistic pointer offset; this limit
14b03c9f9fSEdward Cree  * ensures that umax_value + (int)off + (int)size cannot overflow a u64.
1548461135SJosef Bacik  */
16bb7f0f98SAlexei Starovoitov #define BPF_MAX_VAR_OFF	(1 << 29)
17b03c9f9fSEdward Cree /* Maximum variable size permitted for ARG_CONST_SIZE[_OR_ZERO].  This ensures
18b03c9f9fSEdward Cree  * that converting umax_value to int cannot overflow.
19b03c9f9fSEdward Cree  */
20bb7f0f98SAlexei Starovoitov #define BPF_MAX_VAR_SIZ	(1 << 29)
21d9439c21SAndrii Nakryiko /* size of tmp_str_buf in bpf_verifier.
22d9439c21SAndrii Nakryiko  * we need at least 306 bytes to fit full stack mask representation
23d9439c21SAndrii Nakryiko  * (in the "-8,-16,...,-512" form)
24d9439c21SAndrii Nakryiko  */
25d9439c21SAndrii Nakryiko #define TMP_STR_BUF_LEN 320
2648461135SJosef Bacik 
278e9cd9ceSEdward Cree /* Liveness marks, used for registers and spilled-regs (in stack slots).
288e9cd9ceSEdward Cree  * Read marks propagate upwards until they find a write mark; they record that
298e9cd9ceSEdward Cree  * "one of this state's descendants read this reg" (and therefore the reg is
308e9cd9ceSEdward Cree  * relevant for states_equal() checks).
318e9cd9ceSEdward Cree  * Write marks collect downwards and do not propagate; they record that "the
328e9cd9ceSEdward Cree  * straight-line code that reached this state (from its parent) wrote this reg"
338e9cd9ceSEdward Cree  * (and therefore that reads propagated from this state or its descendants
348e9cd9ceSEdward Cree  * should not propagate to its parent).
358e9cd9ceSEdward Cree  * A state with a write mark can receive read marks; it just won't propagate
368e9cd9ceSEdward Cree  * them to its parent, since the write mark is a property, not of the state,
378e9cd9ceSEdward Cree  * but of the link between it and its parent.  See mark_reg_read() and
388e9cd9ceSEdward Cree  * mark_stack_slot_read() in kernel/bpf/verifier.c.
398e9cd9ceSEdward Cree  */
40dc503a8aSEdward Cree enum bpf_reg_liveness {
41dc503a8aSEdward Cree 	REG_LIVE_NONE = 0, /* reg hasn't been read or written this branch */
425327ed3dSJiong Wang 	REG_LIVE_READ32 = 0x1, /* reg was read, so we're sensitive to initial value */
435327ed3dSJiong Wang 	REG_LIVE_READ64 = 0x2, /* likewise, but full 64-bit content matters */
445327ed3dSJiong Wang 	REG_LIVE_READ = REG_LIVE_READ32 | REG_LIVE_READ64,
455327ed3dSJiong Wang 	REG_LIVE_WRITTEN = 0x4, /* reg was written first, screening off later reads */
465327ed3dSJiong Wang 	REG_LIVE_DONE = 0x8, /* liveness won't be updating this register anymore */
47dc503a8aSEdward Cree };
48dc503a8aSEdward Cree 
496a3cd331SDave Marchevsky /* For every reg representing a map value or allocated object pointer,
506a3cd331SDave Marchevsky  * we consider the tuple of (ptr, id) for them to be unique in verifier
516a3cd331SDave Marchevsky  * context and conside them to not alias each other for the purposes of
526a3cd331SDave Marchevsky  * tracking lock state.
536a3cd331SDave Marchevsky  */
546a3cd331SDave Marchevsky struct bpf_active_lock {
556a3cd331SDave Marchevsky 	/* This can either be reg->map_ptr or reg->btf. If ptr is NULL,
566a3cd331SDave Marchevsky 	 * there's no active lock held, and other fields have no
576a3cd331SDave Marchevsky 	 * meaning. If non-NULL, it indicates that a lock is held and
586a3cd331SDave Marchevsky 	 * id member has the reg->id of the register which can be >= 0.
596a3cd331SDave Marchevsky 	 */
606a3cd331SDave Marchevsky 	void *ptr;
616a3cd331SDave Marchevsky 	/* This will be reg->id */
626a3cd331SDave Marchevsky 	u32 id;
636a3cd331SDave Marchevsky };
646a3cd331SDave Marchevsky 
65215bf496SAndrii Nakryiko #define ITER_PREFIX "bpf_iter_"
66215bf496SAndrii Nakryiko 
6706accc87SAndrii Nakryiko enum bpf_iter_state {
6806accc87SAndrii Nakryiko 	BPF_ITER_STATE_INVALID, /* for non-first slot */
6906accc87SAndrii Nakryiko 	BPF_ITER_STATE_ACTIVE,
7006accc87SAndrii Nakryiko 	BPF_ITER_STATE_DRAINED,
7106accc87SAndrii Nakryiko };
7206accc87SAndrii Nakryiko 
7358e2af8bSJakub Kicinski struct bpf_reg_state {
74679c782dSEdward Cree 	/* Ordering of fields matters.  See states_equal() */
7558e2af8bSJakub Kicinski 	enum bpf_reg_type type;
7622dc4a0fSAndrii Nakryiko 	/* Fixed part of pointer offset, pointer types only */
7722dc4a0fSAndrii Nakryiko 	s32 off;
7858e2af8bSJakub Kicinski 	union {
79f1174f77SEdward Cree 		/* valid when type == PTR_TO_PACKET */
806d94e741SAlexei Starovoitov 		int range;
8158e2af8bSJakub Kicinski 
8258e2af8bSJakub Kicinski 		/* valid when type == CONST_PTR_TO_MAP | PTR_TO_MAP_VALUE |
8358e2af8bSJakub Kicinski 		 *   PTR_TO_MAP_VALUE_OR_NULL
8458e2af8bSJakub Kicinski 		 */
853e8ce298SAlexei Starovoitov 		struct {
8658e2af8bSJakub Kicinski 			struct bpf_map *map_ptr;
873e8ce298SAlexei Starovoitov 			/* To distinguish map lookups from outer map
883e8ce298SAlexei Starovoitov 			 * the map_uid is non-zero for registers
893e8ce298SAlexei Starovoitov 			 * pointing to inner maps.
903e8ce298SAlexei Starovoitov 			 */
913e8ce298SAlexei Starovoitov 			u32 map_uid;
923e8ce298SAlexei Starovoitov 		};
930962590eSDaniel Borkmann 
9422dc4a0fSAndrii Nakryiko 		/* for PTR_TO_BTF_ID */
9522dc4a0fSAndrii Nakryiko 		struct {
9622dc4a0fSAndrii Nakryiko 			struct btf *btf;
9722dc4a0fSAndrii Nakryiko 			u32 btf_id;
9822dc4a0fSAndrii Nakryiko 		};
999e15db66SAlexei Starovoitov 
100f8064ab9SKumar Kartikeya Dwivedi 		struct { /* for PTR_TO_MEM | PTR_TO_MEM_OR_NULL */
101f8064ab9SKumar Kartikeya Dwivedi 			u32 mem_size;
102f8064ab9SKumar Kartikeya Dwivedi 			u32 dynptr_id; /* for dynptr slices */
103f8064ab9SKumar Kartikeya Dwivedi 		};
104457f4436SAndrii Nakryiko 
10597e03f52SJoanne Koong 		/* For dynptr stack slots */
10697e03f52SJoanne Koong 		struct {
10797e03f52SJoanne Koong 			enum bpf_dynptr_type type;
10897e03f52SJoanne Koong 			/* A dynptr is 16 bytes so it takes up 2 stack slots.
10997e03f52SJoanne Koong 			 * We need to track which slot is the first slot
11097e03f52SJoanne Koong 			 * to protect against cases where the user may try to
11197e03f52SJoanne Koong 			 * pass in an address starting at the second slot of the
11297e03f52SJoanne Koong 			 * dynptr.
11397e03f52SJoanne Koong 			 */
11497e03f52SJoanne Koong 			bool first_slot;
11597e03f52SJoanne Koong 		} dynptr;
11697e03f52SJoanne Koong 
11706accc87SAndrii Nakryiko 		/* For bpf_iter stack slots */
11806accc87SAndrii Nakryiko 		struct {
11906accc87SAndrii Nakryiko 			/* BTF container and BTF type ID describing
12006accc87SAndrii Nakryiko 			 * struct bpf_iter_<type> of an iterator state
12106accc87SAndrii Nakryiko 			 */
12206accc87SAndrii Nakryiko 			struct btf *btf;
12306accc87SAndrii Nakryiko 			u32 btf_id;
12406accc87SAndrii Nakryiko 			/* packing following two fields to fit iter state into 16 bytes */
12506accc87SAndrii Nakryiko 			enum bpf_iter_state state:2;
12606accc87SAndrii Nakryiko 			int depth:30;
12706accc87SAndrii Nakryiko 		} iter;
12806accc87SAndrii Nakryiko 
1290962590eSDaniel Borkmann 		/* Max size from any of the above. */
13022dc4a0fSAndrii Nakryiko 		struct {
13122dc4a0fSAndrii Nakryiko 			unsigned long raw1;
13222dc4a0fSAndrii Nakryiko 			unsigned long raw2;
13322dc4a0fSAndrii Nakryiko 		} raw;
13469c087baSYonghong Song 
13569c087baSYonghong Song 		u32 subprogno; /* for PTR_TO_FUNC */
13658e2af8bSJakub Kicinski 	};
137a73bf9f2SAndrii Nakryiko 	/* For scalar types (SCALAR_VALUE), this represents our knowledge of
138a73bf9f2SAndrii Nakryiko 	 * the actual value.
139a73bf9f2SAndrii Nakryiko 	 * For pointer types, this represents the variable part of the offset
140a73bf9f2SAndrii Nakryiko 	 * from the pointed-to object, and is shared with all bpf_reg_states
141a73bf9f2SAndrii Nakryiko 	 * with the same id as us.
142a73bf9f2SAndrii Nakryiko 	 */
143a73bf9f2SAndrii Nakryiko 	struct tnum var_off;
144a73bf9f2SAndrii Nakryiko 	/* Used to determine if any memory access using this register will
145a73bf9f2SAndrii Nakryiko 	 * result in a bad access.
146a73bf9f2SAndrii Nakryiko 	 * These refer to the same value as var_off, not necessarily the actual
147a73bf9f2SAndrii Nakryiko 	 * contents of the register.
148a73bf9f2SAndrii Nakryiko 	 */
149a73bf9f2SAndrii Nakryiko 	s64 smin_value; /* minimum possible (s64)value */
150a73bf9f2SAndrii Nakryiko 	s64 smax_value; /* maximum possible (s64)value */
151a73bf9f2SAndrii Nakryiko 	u64 umin_value; /* minimum possible (u64)value */
152a73bf9f2SAndrii Nakryiko 	u64 umax_value; /* maximum possible (u64)value */
153a73bf9f2SAndrii Nakryiko 	s32 s32_min_value; /* minimum possible (s32)value */
154a73bf9f2SAndrii Nakryiko 	s32 s32_max_value; /* maximum possible (s32)value */
155a73bf9f2SAndrii Nakryiko 	u32 u32_min_value; /* minimum possible (u32)value */
156a73bf9f2SAndrii Nakryiko 	u32 u32_max_value; /* maximum possible (u32)value */
157f1174f77SEdward Cree 	/* For PTR_TO_PACKET, used to find other pointers with the same variable
158f1174f77SEdward Cree 	 * offset, so they can share range knowledge.
159f1174f77SEdward Cree 	 * For PTR_TO_MAP_VALUE_OR_NULL this is used to share which map value we
160f1174f77SEdward Cree 	 * came from, when one is tested for != NULL.
161457f4436SAndrii Nakryiko 	 * For PTR_TO_MEM_OR_NULL this is used to identify memory allocation
162457f4436SAndrii Nakryiko 	 * for the purpose of tracking that it's freed.
163c64b7983SJoe Stringer 	 * For PTR_TO_SOCKET this is used to share which pointers retain the
164c64b7983SJoe Stringer 	 * same reference to the socket, to determine proper reference freeing.
165bc34dee6SJoanne Koong 	 * For stack slots that are dynptrs, this is used to track references to
166bc34dee6SJoanne Koong 	 * the dynptr to determine proper reference freeing.
16706accc87SAndrii Nakryiko 	 * Similarly to dynptrs, we use ID to track "belonging" of a reference
16806accc87SAndrii Nakryiko 	 * to a specific instance of bpf_iter.
169f1174f77SEdward Cree 	 */
170d2a4dd37SAlexei Starovoitov 	u32 id;
1711b986589SMartin KaFai Lau 	/* PTR_TO_SOCKET and PTR_TO_TCP_SOCK could be a ptr returned
1721b986589SMartin KaFai Lau 	 * from a pointer-cast helper, bpf_sk_fullsock() and
1731b986589SMartin KaFai Lau 	 * bpf_tcp_sock().
1741b986589SMartin KaFai Lau 	 *
1751b986589SMartin KaFai Lau 	 * Consider the following where "sk" is a reference counted
1761b986589SMartin KaFai Lau 	 * pointer returned from "sk = bpf_sk_lookup_tcp();":
1771b986589SMartin KaFai Lau 	 *
1781b986589SMartin KaFai Lau 	 * 1: sk = bpf_sk_lookup_tcp();
1791b986589SMartin KaFai Lau 	 * 2: if (!sk) { return 0; }
1801b986589SMartin KaFai Lau 	 * 3: fullsock = bpf_sk_fullsock(sk);
1811b986589SMartin KaFai Lau 	 * 4: if (!fullsock) { bpf_sk_release(sk); return 0; }
1821b986589SMartin KaFai Lau 	 * 5: tp = bpf_tcp_sock(fullsock);
1831b986589SMartin KaFai Lau 	 * 6: if (!tp) { bpf_sk_release(sk); return 0; }
1841b986589SMartin KaFai Lau 	 * 7: bpf_sk_release(sk);
1851b986589SMartin KaFai Lau 	 * 8: snd_cwnd = tp->snd_cwnd;  // verifier will complain
1861b986589SMartin KaFai Lau 	 *
1871b986589SMartin KaFai Lau 	 * After bpf_sk_release(sk) at line 7, both "fullsock" ptr and
1881b986589SMartin KaFai Lau 	 * "tp" ptr should be invalidated also.  In order to do that,
1891b986589SMartin KaFai Lau 	 * the reg holding "fullsock" and "sk" need to remember
1901b986589SMartin KaFai Lau 	 * the original refcounted ptr id (i.e. sk_reg->id) in ref_obj_id
1911b986589SMartin KaFai Lau 	 * such that the verifier can reset all regs which have
1921b986589SMartin KaFai Lau 	 * ref_obj_id matching the sk_reg->id.
1931b986589SMartin KaFai Lau 	 *
1941b986589SMartin KaFai Lau 	 * sk_reg->ref_obj_id is set to sk_reg->id at line 1.
1951b986589SMartin KaFai Lau 	 * sk_reg->id will stay as NULL-marking purpose only.
1961b986589SMartin KaFai Lau 	 * After NULL-marking is done, sk_reg->id can be reset to 0.
1971b986589SMartin KaFai Lau 	 *
1981b986589SMartin KaFai Lau 	 * After "fullsock = bpf_sk_fullsock(sk);" at line 3,
1991b986589SMartin KaFai Lau 	 * fullsock_reg->ref_obj_id is set to sk_reg->ref_obj_id.
2001b986589SMartin KaFai Lau 	 *
2011b986589SMartin KaFai Lau 	 * After "tp = bpf_tcp_sock(fullsock);" at line 5,
2021b986589SMartin KaFai Lau 	 * tp_reg->ref_obj_id is set to fullsock_reg->ref_obj_id
2031b986589SMartin KaFai Lau 	 * which is the same as sk_reg->ref_obj_id.
2041b986589SMartin KaFai Lau 	 *
2051b986589SMartin KaFai Lau 	 * From the verifier perspective, if sk, fullsock and tp
2061b986589SMartin KaFai Lau 	 * are not NULL, they are the same ptr with different
2071b986589SMartin KaFai Lau 	 * reg->type.  In particular, bpf_sk_release(tp) is also
2081b986589SMartin KaFai Lau 	 * allowed and has the same effect as bpf_sk_release(sk).
2091b986589SMartin KaFai Lau 	 */
2101b986589SMartin KaFai Lau 	u32 ref_obj_id;
211679c782dSEdward Cree 	/* parentage chain for liveness checking */
212679c782dSEdward Cree 	struct bpf_reg_state *parent;
213f4d7e40aSAlexei Starovoitov 	/* Inside the callee two registers can be both PTR_TO_STACK like
214f4d7e40aSAlexei Starovoitov 	 * R1=fp-8 and R2=fp-8, but one of them points to this function stack
215f4d7e40aSAlexei Starovoitov 	 * while another to the caller's stack. To differentiate them 'frameno'
216f4d7e40aSAlexei Starovoitov 	 * is used which is an index in bpf_verifier_state->frame[] array
217f4d7e40aSAlexei Starovoitov 	 * pointing to bpf_func_state.
218f4d7e40aSAlexei Starovoitov 	 */
219f4d7e40aSAlexei Starovoitov 	u32 frameno;
2205327ed3dSJiong Wang 	/* Tracks subreg definition. The stored value is the insn_idx of the
2215327ed3dSJiong Wang 	 * writing insn. This is safe because subreg_def is used before any insn
2225327ed3dSJiong Wang 	 * patching which only happens after main verification finished.
2235327ed3dSJiong Wang 	 */
2245327ed3dSJiong Wang 	s32 subreg_def;
225dc503a8aSEdward Cree 	enum bpf_reg_liveness live;
226b5dc0163SAlexei Starovoitov 	/* if (!precise && SCALAR_VALUE) min/max/tnum don't affect safety */
227b5dc0163SAlexei Starovoitov 	bool precise;
22858e2af8bSJakub Kicinski };
22958e2af8bSJakub Kicinski 
23058e2af8bSJakub Kicinski enum bpf_stack_slot_type {
23158e2af8bSJakub Kicinski 	STACK_INVALID,    /* nothing was stored in this stack slot */
23258e2af8bSJakub Kicinski 	STACK_SPILL,      /* register spilled into stack */
233cc2b14d5SAlexei Starovoitov 	STACK_MISC,	  /* BPF program wrote some data into this slot */
234cc2b14d5SAlexei Starovoitov 	STACK_ZERO,	  /* BPF program wrote constant zero */
23597e03f52SJoanne Koong 	/* A dynptr is stored in this stack slot. The type of dynptr
23697e03f52SJoanne Koong 	 * is stored in bpf_stack_state->spilled_ptr.dynptr.type
23797e03f52SJoanne Koong 	 */
23897e03f52SJoanne Koong 	STACK_DYNPTR,
23906accc87SAndrii Nakryiko 	STACK_ITER,
24058e2af8bSJakub Kicinski };
24158e2af8bSJakub Kicinski 
24258e2af8bSJakub Kicinski #define BPF_REG_SIZE 8	/* size of eBPF register in bytes */
24306accc87SAndrii Nakryiko 
244407958a0SAndrii Nakryiko #define BPF_REGMASK_ARGS ((1 << BPF_REG_1) | (1 << BPF_REG_2) | \
245407958a0SAndrii Nakryiko 			  (1 << BPF_REG_3) | (1 << BPF_REG_4) | \
246407958a0SAndrii Nakryiko 			  (1 << BPF_REG_5))
247407958a0SAndrii Nakryiko 
24897e03f52SJoanne Koong #define BPF_DYNPTR_SIZE		sizeof(struct bpf_dynptr_kern)
24997e03f52SJoanne Koong #define BPF_DYNPTR_NR_SLOTS		(BPF_DYNPTR_SIZE / BPF_REG_SIZE)
25058e2af8bSJakub Kicinski 
251638f5b90SAlexei Starovoitov struct bpf_stack_state {
252638f5b90SAlexei Starovoitov 	struct bpf_reg_state spilled_ptr;
253638f5b90SAlexei Starovoitov 	u8 slot_type[BPF_REG_SIZE];
254638f5b90SAlexei Starovoitov };
255638f5b90SAlexei Starovoitov 
256fd978bf7SJoe Stringer struct bpf_reference_state {
257fd978bf7SJoe Stringer 	/* Track each reference created with a unique id, even if the same
258fd978bf7SJoe Stringer 	 * instruction creates the reference multiple times (eg, via CALL).
259fd978bf7SJoe Stringer 	 */
260fd978bf7SJoe Stringer 	int id;
261fd978bf7SJoe Stringer 	/* Instruction where the allocation of this reference occurred. This
262fd978bf7SJoe Stringer 	 * is used purely to inform the user of a reference leak.
263fd978bf7SJoe Stringer 	 */
264fd978bf7SJoe Stringer 	int insn_idx;
2659d9d00acSKumar Kartikeya Dwivedi 	/* There can be a case like:
2669d9d00acSKumar Kartikeya Dwivedi 	 * main (frame 0)
2679d9d00acSKumar Kartikeya Dwivedi 	 *  cb (frame 1)
2689d9d00acSKumar Kartikeya Dwivedi 	 *   func (frame 3)
2699d9d00acSKumar Kartikeya Dwivedi 	 *    cb (frame 4)
2709d9d00acSKumar Kartikeya Dwivedi 	 * Hence for frame 4, if callback_ref just stored boolean, it would be
2719d9d00acSKumar Kartikeya Dwivedi 	 * impossible to distinguish nested callback refs. Hence store the
2729d9d00acSKumar Kartikeya Dwivedi 	 * frameno and compare that to callback_ref in check_reference_leak when
2739d9d00acSKumar Kartikeya Dwivedi 	 * exiting a callback function.
2749d9d00acSKumar Kartikeya Dwivedi 	 */
2759d9d00acSKumar Kartikeya Dwivedi 	int callback_ref;
276fd978bf7SJoe Stringer };
277fd978bf7SJoe Stringer 
27858e2af8bSJakub Kicinski /* state of the program:
27958e2af8bSJakub Kicinski  * type of all registers and stack info
28058e2af8bSJakub Kicinski  */
281f4d7e40aSAlexei Starovoitov struct bpf_func_state {
28258e2af8bSJakub Kicinski 	struct bpf_reg_state regs[MAX_BPF_REG];
283f4d7e40aSAlexei Starovoitov 	/* index of call instruction that called into this func */
284f4d7e40aSAlexei Starovoitov 	int callsite;
285f4d7e40aSAlexei Starovoitov 	/* stack frame number of this function state from pov of
286f4d7e40aSAlexei Starovoitov 	 * enclosing bpf_verifier_state.
287f4d7e40aSAlexei Starovoitov 	 * 0 = main function, 1 = first callee.
288f4d7e40aSAlexei Starovoitov 	 */
289f4d7e40aSAlexei Starovoitov 	u32 frameno;
29001f810acSAndrei Matei 	/* subprog number == index within subprog_info
291f4d7e40aSAlexei Starovoitov 	 * zero == main subprog
292f4d7e40aSAlexei Starovoitov 	 */
293f4d7e40aSAlexei Starovoitov 	u32 subprogno;
294bfc6bb74SAlexei Starovoitov 	/* Every bpf_timer_start will increment async_entry_cnt.
295bfc6bb74SAlexei Starovoitov 	 * It's used to distinguish:
296bfc6bb74SAlexei Starovoitov 	 * void foo(void) { for(;;); }
297bfc6bb74SAlexei Starovoitov 	 * void foo(void) { bpf_timer_set_callback(,foo); }
298bfc6bb74SAlexei Starovoitov 	 */
299bfc6bb74SAlexei Starovoitov 	u32 async_entry_cnt;
300bfc6bb74SAlexei Starovoitov 	bool in_callback_fn;
3011bfe26fbSDave Marchevsky 	struct tnum callback_ret_range;
302bfc6bb74SAlexei Starovoitov 	bool in_async_callback_fn;
303*bfc5c19bSEduard Zingerman 	/* For callback calling functions that limit number of possible
304*bfc5c19bSEduard Zingerman 	 * callback executions (e.g. bpf_loop) keeps track of current
305*bfc5c19bSEduard Zingerman 	 * simulated iteration number.
306*bfc5c19bSEduard Zingerman 	 * Value in frame N refers to number of times callback with frame
307*bfc5c19bSEduard Zingerman 	 * N+1 was simulated, e.g. for the following call:
308*bfc5c19bSEduard Zingerman 	 *
309*bfc5c19bSEduard Zingerman 	 *   bpf_loop(..., fn, ...); | suppose current frame is N
310*bfc5c19bSEduard Zingerman 	 *                           | fn would be simulated in frame N+1
311*bfc5c19bSEduard Zingerman 	 *                           | number of simulations is tracked in frame N
312*bfc5c19bSEduard Zingerman 	 */
313*bfc5c19bSEduard Zingerman 	u32 callback_depth;
314f4d7e40aSAlexei Starovoitov 
315fd978bf7SJoe Stringer 	/* The following fields should be last. See copy_func_state() */
316fd978bf7SJoe Stringer 	int acquired_refs;
317fd978bf7SJoe Stringer 	struct bpf_reference_state *refs;
318638f5b90SAlexei Starovoitov 	int allocated_stack;
319638f5b90SAlexei Starovoitov 	struct bpf_stack_state *stack;
32058e2af8bSJakub Kicinski };
32158e2af8bSJakub Kicinski 
322b5dc0163SAlexei Starovoitov struct bpf_idx_pair {
323b5dc0163SAlexei Starovoitov 	u32 prev_idx;
324b5dc0163SAlexei Starovoitov 	u32 idx;
325b5dc0163SAlexei Starovoitov };
326b5dc0163SAlexei Starovoitov 
327f4d7e40aSAlexei Starovoitov #define MAX_CALL_FRAMES 8
3285dd9cdbcSEduard Zingerman /* Maximum number of register states that can exist at once */
3295dd9cdbcSEduard Zingerman #define BPF_ID_MAP_SIZE ((MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE) * MAX_CALL_FRAMES)
330f4d7e40aSAlexei Starovoitov struct bpf_verifier_state {
331f4d7e40aSAlexei Starovoitov 	/* call stack tracking */
332f4d7e40aSAlexei Starovoitov 	struct bpf_func_state *frame[MAX_CALL_FRAMES];
3332589726dSAlexei Starovoitov 	struct bpf_verifier_state *parent;
3342589726dSAlexei Starovoitov 	/*
3352589726dSAlexei Starovoitov 	 * 'branches' field is the number of branches left to explore:
3362589726dSAlexei Starovoitov 	 * 0 - all possible paths from this state reached bpf_exit or
3372589726dSAlexei Starovoitov 	 * were safely pruned
3382589726dSAlexei Starovoitov 	 * 1 - at least one path is being explored.
3392589726dSAlexei Starovoitov 	 * This state hasn't reached bpf_exit
3402589726dSAlexei Starovoitov 	 * 2 - at least two paths are being explored.
3412589726dSAlexei Starovoitov 	 * This state is an immediate parent of two children.
3422589726dSAlexei Starovoitov 	 * One is fallthrough branch with branches==1 and another
3432589726dSAlexei Starovoitov 	 * state is pushed into stack (to be explored later) also with
3442589726dSAlexei Starovoitov 	 * branches==1. The parent of this state has branches==1.
3452589726dSAlexei Starovoitov 	 * The verifier state tree connected via 'parent' pointer looks like:
3462589726dSAlexei Starovoitov 	 * 1
3472589726dSAlexei Starovoitov 	 * 1
3482589726dSAlexei Starovoitov 	 * 2 -> 1 (first 'if' pushed into stack)
3492589726dSAlexei Starovoitov 	 * 1
3502589726dSAlexei Starovoitov 	 * 2 -> 1 (second 'if' pushed into stack)
3512589726dSAlexei Starovoitov 	 * 1
3522589726dSAlexei Starovoitov 	 * 1
3532589726dSAlexei Starovoitov 	 * 1 bpf_exit.
3542589726dSAlexei Starovoitov 	 *
3552589726dSAlexei Starovoitov 	 * Once do_check() reaches bpf_exit, it calls update_branch_counts()
3562589726dSAlexei Starovoitov 	 * and the verifier state tree will look:
3572589726dSAlexei Starovoitov 	 * 1
3582589726dSAlexei Starovoitov 	 * 1
3592589726dSAlexei Starovoitov 	 * 2 -> 1 (first 'if' pushed into stack)
3602589726dSAlexei Starovoitov 	 * 1
3612589726dSAlexei Starovoitov 	 * 1 -> 1 (second 'if' pushed into stack)
3622589726dSAlexei Starovoitov 	 * 0
3632589726dSAlexei Starovoitov 	 * 0
3642589726dSAlexei Starovoitov 	 * 0 bpf_exit.
3652589726dSAlexei Starovoitov 	 * After pop_stack() the do_check() will resume at second 'if'.
3662589726dSAlexei Starovoitov 	 *
3672589726dSAlexei Starovoitov 	 * If is_state_visited() sees a state with branches > 0 it means
3682589726dSAlexei Starovoitov 	 * there is a loop. If such state is exactly equal to the current state
3692589726dSAlexei Starovoitov 	 * it's an infinite loop. Note states_equal() checks for states
3706dbdc9f3SHongyi Lu 	 * equivalency, so two states being 'states_equal' does not mean
3712589726dSAlexei Starovoitov 	 * infinite loop. The exact comparison is provided by
3722589726dSAlexei Starovoitov 	 * states_maybe_looping() function. It's a stronger pre-check and
3732589726dSAlexei Starovoitov 	 * much faster than states_equal().
3742589726dSAlexei Starovoitov 	 *
3752589726dSAlexei Starovoitov 	 * This algorithm may not find all possible infinite loops or
3762589726dSAlexei Starovoitov 	 * loop iteration count may be too high.
3772589726dSAlexei Starovoitov 	 * In such cases BPF_COMPLEXITY_LIMIT_INSNS limit kicks in.
3782589726dSAlexei Starovoitov 	 */
3792589726dSAlexei Starovoitov 	u32 branches;
380dc2a4ebcSAlexei Starovoitov 	u32 insn_idx;
381f4d7e40aSAlexei Starovoitov 	u32 curframe;
3826a3cd331SDave Marchevsky 
3836a3cd331SDave Marchevsky 	struct bpf_active_lock active_lock;
384979d63d5SDaniel Borkmann 	bool speculative;
3859bb00b28SYonghong Song 	bool active_rcu_lock;
386c8f6d285SEduard Zingerman 	/* If this state was ever pointed-to by other state's loop_entry field
387c8f6d285SEduard Zingerman 	 * this flag would be set to true. Used to avoid freeing such states
388c8f6d285SEduard Zingerman 	 * while they are still in use.
389c8f6d285SEduard Zingerman 	 */
390c8f6d285SEduard Zingerman 	bool used_as_loop_entry;
391b5dc0163SAlexei Starovoitov 
392b5dc0163SAlexei Starovoitov 	/* first and last insn idx of this verifier state */
393b5dc0163SAlexei Starovoitov 	u32 first_insn_idx;
394b5dc0163SAlexei Starovoitov 	u32 last_insn_idx;
395c8f6d285SEduard Zingerman 	/* If this state is a part of states loop this field points to some
396c8f6d285SEduard Zingerman 	 * parent of this state such that:
397c8f6d285SEduard Zingerman 	 * - it is also a member of the same states loop;
398c8f6d285SEduard Zingerman 	 * - DFS states traversal starting from initial state visits loop_entry
399c8f6d285SEduard Zingerman 	 *   state before this state.
400c8f6d285SEduard Zingerman 	 * Used to compute topmost loop entry for state loops.
401c8f6d285SEduard Zingerman 	 * State loops might appear because of open coded iterators logic.
402c8f6d285SEduard Zingerman 	 * See get_loop_entry() for more information.
403c8f6d285SEduard Zingerman 	 */
404c8f6d285SEduard Zingerman 	struct bpf_verifier_state *loop_entry;
405b5dc0163SAlexei Starovoitov 	/* jmp history recorded from first to last.
406b5dc0163SAlexei Starovoitov 	 * backtracking is using it to go from last to first.
407b5dc0163SAlexei Starovoitov 	 * For most states jmp_history_cnt is [0-3].
408b5dc0163SAlexei Starovoitov 	 * For loops can go up to ~40.
409b5dc0163SAlexei Starovoitov 	 */
410b5dc0163SAlexei Starovoitov 	struct bpf_idx_pair *jmp_history;
411b5dc0163SAlexei Starovoitov 	u32 jmp_history_cnt;
412ab470fefSEduard Zingerman 	u32 dfs_depth;
413b43550d7SEduard Zingerman 	u32 callback_unroll_depth;
414f4d7e40aSAlexei Starovoitov };
415f4d7e40aSAlexei Starovoitov 
416f3709f69SJoe Stringer #define bpf_get_spilled_reg(slot, frame)				\
417f3709f69SJoe Stringer 	(((slot < frame->allocated_stack / BPF_REG_SIZE) &&		\
418f3709f69SJoe Stringer 	  (frame->stack[slot].slot_type[0] == STACK_SPILL))		\
419f3709f69SJoe Stringer 	 ? &frame->stack[slot].spilled_ptr : NULL)
420f3709f69SJoe Stringer 
421f3709f69SJoe Stringer /* Iterate over 'frame', setting 'reg' to either NULL or a spilled register. */
422f3709f69SJoe Stringer #define bpf_for_each_spilled_reg(iter, frame, reg)			\
423f3709f69SJoe Stringer 	for (iter = 0, reg = bpf_get_spilled_reg(iter, frame);		\
424f3709f69SJoe Stringer 	     iter < frame->allocated_stack / BPF_REG_SIZE;		\
425f3709f69SJoe Stringer 	     iter++, reg = bpf_get_spilled_reg(iter, frame))
426f3709f69SJoe Stringer 
427b239da34SKumar Kartikeya Dwivedi /* Invoke __expr over regsiters in __vst, setting __state and __reg */
428b239da34SKumar Kartikeya Dwivedi #define bpf_for_each_reg_in_vstate(__vst, __state, __reg, __expr)   \
429b239da34SKumar Kartikeya Dwivedi 	({                                                               \
430b239da34SKumar Kartikeya Dwivedi 		struct bpf_verifier_state *___vstate = __vst;            \
431b239da34SKumar Kartikeya Dwivedi 		int ___i, ___j;                                          \
432b239da34SKumar Kartikeya Dwivedi 		for (___i = 0; ___i <= ___vstate->curframe; ___i++) {    \
433b239da34SKumar Kartikeya Dwivedi 			struct bpf_reg_state *___regs;                   \
434b239da34SKumar Kartikeya Dwivedi 			__state = ___vstate->frame[___i];                \
435b239da34SKumar Kartikeya Dwivedi 			___regs = __state->regs;                         \
436b239da34SKumar Kartikeya Dwivedi 			for (___j = 0; ___j < MAX_BPF_REG; ___j++) {     \
437b239da34SKumar Kartikeya Dwivedi 				__reg = &___regs[___j];                  \
438b239da34SKumar Kartikeya Dwivedi 				(void)(__expr);                          \
439b239da34SKumar Kartikeya Dwivedi 			}                                                \
440b239da34SKumar Kartikeya Dwivedi 			bpf_for_each_spilled_reg(___j, __state, __reg) { \
441b239da34SKumar Kartikeya Dwivedi 				if (!__reg)                              \
442b239da34SKumar Kartikeya Dwivedi 					continue;                        \
443b239da34SKumar Kartikeya Dwivedi 				(void)(__expr);                          \
444b239da34SKumar Kartikeya Dwivedi 			}                                                \
445b239da34SKumar Kartikeya Dwivedi 		}                                                        \
446b239da34SKumar Kartikeya Dwivedi 	})
447b239da34SKumar Kartikeya Dwivedi 
44858e2af8bSJakub Kicinski /* linked list of verifier states used to prune search */
44958e2af8bSJakub Kicinski struct bpf_verifier_state_list {
45058e2af8bSJakub Kicinski 	struct bpf_verifier_state state;
45158e2af8bSJakub Kicinski 	struct bpf_verifier_state_list *next;
4529f4686c4SAlexei Starovoitov 	int miss_cnt, hit_cnt;
45358e2af8bSJakub Kicinski };
45458e2af8bSJakub Kicinski 
4551ade2371SEduard Zingerman struct bpf_loop_inline_state {
456f16214c1SMatthieu Baerts 	unsigned int initialized:1; /* set to true upon first entry */
457f16214c1SMatthieu Baerts 	unsigned int fit_for_inline:1; /* true if callback function is the same
4581ade2371SEduard Zingerman 					* at each call and flags are always zero
4591ade2371SEduard Zingerman 					*/
4601ade2371SEduard Zingerman 	u32 callback_subprogno; /* valid when fit_for_inline is true */
4611ade2371SEduard Zingerman };
4621ade2371SEduard Zingerman 
463979d63d5SDaniel Borkmann /* Possible states for alu_state member. */
464801c6058SDaniel Borkmann #define BPF_ALU_SANITIZE_SRC		(1U << 0)
465801c6058SDaniel Borkmann #define BPF_ALU_SANITIZE_DST		(1U << 1)
466979d63d5SDaniel Borkmann #define BPF_ALU_NEG_VALUE		(1U << 2)
467d3bd7413SDaniel Borkmann #define BPF_ALU_NON_POINTER		(1U << 3)
468801c6058SDaniel Borkmann #define BPF_ALU_IMMEDIATE		(1U << 4)
469979d63d5SDaniel Borkmann #define BPF_ALU_SANITIZE		(BPF_ALU_SANITIZE_SRC | \
470979d63d5SDaniel Borkmann 					 BPF_ALU_SANITIZE_DST)
471979d63d5SDaniel Borkmann 
47258e2af8bSJakub Kicinski struct bpf_insn_aux_data {
47381ed18abSAlexei Starovoitov 	union {
47458e2af8bSJakub Kicinski 		enum bpf_reg_type ptr_type;	/* pointer type for load/store insns */
475d2e4c1e6SDaniel Borkmann 		unsigned long map_ptr_state;	/* pointer/poison value for maps */
4761c2a088aSAlexei Starovoitov 		s32 call_imm;			/* saved imm field of call insn */
477979d63d5SDaniel Borkmann 		u32 alu_limit;			/* limit for add/sub register with pointer */
478d8eca5bbSDaniel Borkmann 		struct {
479d8eca5bbSDaniel Borkmann 			u32 map_index;		/* index into used_maps[] */
480d8eca5bbSDaniel Borkmann 			u32 map_off;		/* offset from value base address */
481d8eca5bbSDaniel Borkmann 		};
4824976b718SHao Luo 		struct {
4834976b718SHao Luo 			enum bpf_reg_type reg_type;	/* type of pseudo_btf_id */
4844976b718SHao Luo 			union {
48522dc4a0fSAndrii Nakryiko 				struct {
48622dc4a0fSAndrii Nakryiko 					struct btf *btf;
4874976b718SHao Luo 					u32 btf_id;	/* btf_id for struct typed var */
48822dc4a0fSAndrii Nakryiko 				};
4894976b718SHao Luo 				u32 mem_size;	/* mem_size for non-struct typed var */
4904976b718SHao Luo 			};
4914976b718SHao Luo 		} btf_var;
4921ade2371SEduard Zingerman 		/* if instruction is a call to bpf_loop this field tracks
4931ade2371SEduard Zingerman 		 * the state of the relevant registers to make decision about inlining
4941ade2371SEduard Zingerman 		 */
4951ade2371SEduard Zingerman 		struct bpf_loop_inline_state loop_inline_state;
49681ed18abSAlexei Starovoitov 	};
497d2dcc67dSDave Marchevsky 	union {
498d2dcc67dSDave Marchevsky 		/* remember the size of type passed to bpf_obj_new to rewrite R1 */
499d2dcc67dSDave Marchevsky 		u64 obj_new_size;
500d2dcc67dSDave Marchevsky 		/* remember the offset of node field within type to rewrite */
501d2dcc67dSDave Marchevsky 		u64 insert_off;
502d2dcc67dSDave Marchevsky 	};
503958cf2e2SKumar Kartikeya Dwivedi 	struct btf_struct_meta *kptr_struct_meta;
504d2e4c1e6SDaniel Borkmann 	u64 map_key_state; /* constant (32 bit) key tracking for maps */
50523994631SYonghong Song 	int ctx_field_size; /* the ctx field size for load insn, maybe 0 */
50651c39bb1SAlexei Starovoitov 	u32 seen; /* this insn was processed by the verifier at env->pass_cnt */
5072039f26fSDaniel Borkmann 	bool sanitize_stack_spill; /* subject to Spectre v4 sanitation */
5085327ed3dSJiong Wang 	bool zext_dst; /* this insn zero extends dst reg */
5099bb00b28SYonghong Song 	bool storage_get_func_atomic; /* bpf_*_storage_get() with atomic memory alloc */
51006accc87SAndrii Nakryiko 	bool is_iter_next; /* bpf_iter_<type>_next() kfunc call */
511979d63d5SDaniel Borkmann 	u8 alu_state; /* used in combination with alu_limit */
51251c39bb1SAlexei Starovoitov 
51351c39bb1SAlexei Starovoitov 	/* below fields are initialized once */
5149e4c24e7SJakub Kicinski 	unsigned int orig_idx; /* original instruction index */
515bffdeaa8SAndrii Nakryiko 	bool jmp_point;
5164b5ce570SAndrii Nakryiko 	bool prune_point;
5174b5ce570SAndrii Nakryiko 	/* ensure we check state equivalence and save state checkpoint and
5184b5ce570SAndrii Nakryiko 	 * this instruction, regardless of any heuristics
5194b5ce570SAndrii Nakryiko 	 */
5204b5ce570SAndrii Nakryiko 	bool force_checkpoint;
521b43550d7SEduard Zingerman 	/* true if instruction is a call to a helper function that
522b43550d7SEduard Zingerman 	 * accepts callback function as a parameter.
523b43550d7SEduard Zingerman 	 */
524b43550d7SEduard Zingerman 	bool calls_callback;
52558e2af8bSJakub Kicinski };
52658e2af8bSJakub Kicinski 
52758e2af8bSJakub Kicinski #define MAX_USED_MAPS 64 /* max number of maps accessed by one eBPF program */
528541c3badSAndrii Nakryiko #define MAX_USED_BTFS 64 /* max number of BTFs accessed by one BPF program */
52958e2af8bSJakub Kicinski 
530a2a7d570SJakub Kicinski #define BPF_VERIFIER_TMP_LOG_SIZE	1024
531a2a7d570SJakub Kicinski 
532b9193c1bSMartin KaFai Lau struct bpf_verifier_log {
53312166409SAndrii Nakryiko 	/* Logical start and end positions of a "log window" of the verifier log.
53412166409SAndrii Nakryiko 	 * start_pos == 0 means we haven't truncated anything.
53512166409SAndrii Nakryiko 	 * Once truncation starts to happen, start_pos + len_total == end_pos,
53612166409SAndrii Nakryiko 	 * except during log reset situations, in which (end_pos - start_pos)
53712166409SAndrii Nakryiko 	 * might get smaller than len_total (see bpf_vlog_reset()).
53812166409SAndrii Nakryiko 	 * Generally, (end_pos - start_pos) gives number of useful data in
53912166409SAndrii Nakryiko 	 * user log buffer.
54012166409SAndrii Nakryiko 	 */
54112166409SAndrii Nakryiko 	u64 start_pos;
54212166409SAndrii Nakryiko 	u64 end_pos;
543e7bf8249SJakub Kicinski 	char __user *ubuf;
54412166409SAndrii Nakryiko 	u32 level;
545e7bf8249SJakub Kicinski 	u32 len_total;
546fa1c7d5cSAndrii Nakryiko 	u32 len_max;
54712166409SAndrii Nakryiko 	char kbuf[BPF_VERIFIER_TMP_LOG_SIZE];
548e7bf8249SJakub Kicinski };
549e7bf8249SJakub Kicinski 
55006ee7115SAlexei Starovoitov #define BPF_LOG_LEVEL1	1
55106ee7115SAlexei Starovoitov #define BPF_LOG_LEVEL2	2
55206ee7115SAlexei Starovoitov #define BPF_LOG_STATS	4
55312166409SAndrii Nakryiko #define BPF_LOG_FIXED	8
55406ee7115SAlexei Starovoitov #define BPF_LOG_LEVEL	(BPF_LOG_LEVEL1 | BPF_LOG_LEVEL2)
55512166409SAndrii Nakryiko #define BPF_LOG_MASK	(BPF_LOG_LEVEL | BPF_LOG_STATS | BPF_LOG_FIXED)
5568580ac94SAlexei Starovoitov #define BPF_LOG_KERNEL	(BPF_LOG_MASK + 1) /* kernel internal flag */
5572e576648SChristy Lee #define BPF_LOG_MIN_ALIGNMENT 8U
5582e576648SChristy Lee #define BPF_LOG_ALIGNMENT 40U
55906ee7115SAlexei Starovoitov 
bpf_verifier_log_needed(const struct bpf_verifier_log * log)56077d2e05aSMartin KaFai Lau static inline bool bpf_verifier_log_needed(const struct bpf_verifier_log *log)
56177d2e05aSMartin KaFai Lau {
562fa1c7d5cSAndrii Nakryiko 	return log && log->level;
56377d2e05aSMartin KaFai Lau }
56477d2e05aSMartin KaFai Lau 
565cc8b0b92SAlexei Starovoitov #define BPF_MAX_SUBPROGS 256
566cc8b0b92SAlexei Starovoitov 
5679c8105bdSJiong Wang struct bpf_subprog_info {
5688c1b6e69SAlexei Starovoitov 	/* 'start' has to be the first field otherwise find_subprog() won't work */
5699c8105bdSJiong Wang 	u32 start; /* insn idx of function entry point */
570c454a46bSMartin KaFai Lau 	u32 linfo_idx; /* The idx to the main_prog->aux->linfo */
5719c8105bdSJiong Wang 	u16 stack_depth; /* max. stack depth used by this function */
5727f6e4312SMaciej Fijalkowski 	bool has_tail_call;
573ebf7d1f5SMaciej Fijalkowski 	bool tail_call_reachable;
57409b28d76SAlexei Starovoitov 	bool has_ld_abs;
5757ddc80a4SAlexei Starovoitov 	bool is_async_cb;
5769c8105bdSJiong Wang };
5779c8105bdSJiong Wang 
578407958a0SAndrii Nakryiko struct bpf_verifier_env;
579407958a0SAndrii Nakryiko 
580407958a0SAndrii Nakryiko struct backtrack_state {
581407958a0SAndrii Nakryiko 	struct bpf_verifier_env *env;
582407958a0SAndrii Nakryiko 	u32 frame;
583407958a0SAndrii Nakryiko 	u32 reg_masks[MAX_CALL_FRAMES];
584407958a0SAndrii Nakryiko 	u64 stack_masks[MAX_CALL_FRAMES];
585407958a0SAndrii Nakryiko };
586407958a0SAndrii Nakryiko 
5871ffc85d9SEduard Zingerman struct bpf_id_pair {
5881ffc85d9SEduard Zingerman 	u32 old;
5891ffc85d9SEduard Zingerman 	u32 cur;
5901ffc85d9SEduard Zingerman };
5911ffc85d9SEduard Zingerman 
5921ffc85d9SEduard Zingerman struct bpf_idmap {
5931ffc85d9SEduard Zingerman 	u32 tmp_id_gen;
5941ffc85d9SEduard Zingerman 	struct bpf_id_pair map[BPF_ID_MAP_SIZE];
5951ffc85d9SEduard Zingerman };
5961ffc85d9SEduard Zingerman 
597904e6ddfSEduard Zingerman struct bpf_idset {
598904e6ddfSEduard Zingerman 	u32 count;
599904e6ddfSEduard Zingerman 	u32 ids[BPF_ID_MAP_SIZE];
600904e6ddfSEduard Zingerman };
601904e6ddfSEduard Zingerman 
60258e2af8bSJakub Kicinski /* single container for all structs
60358e2af8bSJakub Kicinski  * one verifier_env per bpf_check() call
60458e2af8bSJakub Kicinski  */
60558e2af8bSJakub Kicinski struct bpf_verifier_env {
606c08435ecSDaniel Borkmann 	u32 insn_idx;
607c08435ecSDaniel Borkmann 	u32 prev_insn_idx;
60858e2af8bSJakub Kicinski 	struct bpf_prog *prog;		/* eBPF program being verified */
60900176a34SJakub Kicinski 	const struct bpf_verifier_ops *ops;
61058e2af8bSJakub Kicinski 	struct bpf_verifier_stack_elem *head; /* stack of verifier states to be processed */
61158e2af8bSJakub Kicinski 	int stack_size;			/* number of states to be processed */
612e07b98d9SDavid S. Miller 	bool strict_alignment;		/* perform strict pointer alignment checks */
61310d274e8SAlexei Starovoitov 	bool test_state_freq;		/* test verifier with different pruning frequency */
614638f5b90SAlexei Starovoitov 	struct bpf_verifier_state *cur_state; /* current verifier state */
61558e2af8bSJakub Kicinski 	struct bpf_verifier_state_list **explored_states; /* search pruning optimization */
6169f4686c4SAlexei Starovoitov 	struct bpf_verifier_state_list *free_list;
61758e2af8bSJakub Kicinski 	struct bpf_map *used_maps[MAX_USED_MAPS]; /* array of map's used by eBPF program */
618541c3badSAndrii Nakryiko 	struct btf_mod_pair used_btfs[MAX_USED_BTFS]; /* array of BTF's used by BPF program */
61958e2af8bSJakub Kicinski 	u32 used_map_cnt;		/* number of used maps */
620541c3badSAndrii Nakryiko 	u32 used_btf_cnt;		/* number of used BTF objects */
62158e2af8bSJakub Kicinski 	u32 id_gen;			/* used to generate unique reg IDs */
622e042aa53SDaniel Borkmann 	bool explore_alu_limits;
62358e2af8bSJakub Kicinski 	bool allow_ptr_leaks;
62401f810acSAndrei Matei 	bool allow_uninit_stack;
6252c78ee89SAlexei Starovoitov 	bool bpf_capable;
6262c78ee89SAlexei Starovoitov 	bool bypass_spec_v1;
6272c78ee89SAlexei Starovoitov 	bool bypass_spec_v4;
62858e2af8bSJakub Kicinski 	bool seen_direct_write;
62958e2af8bSJakub Kicinski 	struct bpf_insn_aux_data *insn_aux_data; /* array of per-insn state */
630d9762e84SMartin KaFai Lau 	const struct bpf_line_info *prev_linfo;
631b9193c1bSMartin KaFai Lau 	struct bpf_verifier_log log;
6329c8105bdSJiong Wang 	struct bpf_subprog_info subprog_info[BPF_MAX_SUBPROGS + 1];
633904e6ddfSEduard Zingerman 	union {
6341ffc85d9SEduard Zingerman 		struct bpf_idmap idmap_scratch;
635904e6ddfSEduard Zingerman 		struct bpf_idset idset_scratch;
636904e6ddfSEduard Zingerman 	};
6377df737e9SAlexei Starovoitov 	struct {
6387df737e9SAlexei Starovoitov 		int *insn_state;
6397df737e9SAlexei Starovoitov 		int *insn_stack;
6407df737e9SAlexei Starovoitov 		int cur_stack;
6417df737e9SAlexei Starovoitov 	} cfg;
642407958a0SAndrii Nakryiko 	struct backtrack_state bt;
64351c39bb1SAlexei Starovoitov 	u32 pass_cnt; /* number of times do_check() was called */
644cc8b0b92SAlexei Starovoitov 	u32 subprog_cnt;
64506ee7115SAlexei Starovoitov 	/* number of instructions analyzed by the verifier */
6462589726dSAlexei Starovoitov 	u32 prev_insn_processed, insn_processed;
6472589726dSAlexei Starovoitov 	/* number of jmps, calls, exits analyzed so far */
6482589726dSAlexei Starovoitov 	u32 prev_jmps_processed, jmps_processed;
64906ee7115SAlexei Starovoitov 	/* total verification time */
65006ee7115SAlexei Starovoitov 	u64 verification_time;
65106ee7115SAlexei Starovoitov 	/* maximum number of verifier states kept in 'branching' instructions */
65206ee7115SAlexei Starovoitov 	u32 max_states_per_insn;
65306ee7115SAlexei Starovoitov 	/* total number of allocated verifier states */
65406ee7115SAlexei Starovoitov 	u32 total_states;
65506ee7115SAlexei Starovoitov 	/* some states are freed during program analysis.
65606ee7115SAlexei Starovoitov 	 * this is peak number of states. this number dominates kernel
65706ee7115SAlexei Starovoitov 	 * memory consumption during verification
65806ee7115SAlexei Starovoitov 	 */
65906ee7115SAlexei Starovoitov 	u32 peak_states;
66006ee7115SAlexei Starovoitov 	/* longest register parentage chain walked for liveness marking */
66106ee7115SAlexei Starovoitov 	u32 longest_mark_read_walk;
662387544bfSAlexei Starovoitov 	bpfptr_t fd_array;
6630f55f9edSChristy Lee 
6640f55f9edSChristy Lee 	/* bit mask to keep track of whether a register has been accessed
6650f55f9edSChristy Lee 	 * since the last time the function state was printed
6660f55f9edSChristy Lee 	 */
6670f55f9edSChristy Lee 	u32 scratched_regs;
6680f55f9edSChristy Lee 	/* Same as scratched_regs but for stack slots */
6690f55f9edSChristy Lee 	u64 scratched_stack_slots;
67012166409SAndrii Nakryiko 	u64 prev_log_pos, prev_insn_print_pos;
671d9439c21SAndrii Nakryiko 	/* buffer used to generate temporary string representations,
672d9439c21SAndrii Nakryiko 	 * e.g., in reg_type_str() to generate reg_type string
673d9439c21SAndrii Nakryiko 	 */
674d9439c21SAndrii Nakryiko 	char tmp_str_buf[TMP_STR_BUF_LEN];
67558e2af8bSJakub Kicinski };
67658e2af8bSJakub Kicinski 
677be2d04d1SMathieu Malaterre __printf(2, 0) void bpf_verifier_vlog(struct bpf_verifier_log *log,
678be2d04d1SMathieu Malaterre 				      const char *fmt, va_list args);
679430e68d1SQuentin Monnet __printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env,
680430e68d1SQuentin Monnet 					   const char *fmt, ...);
6819e15db66SAlexei Starovoitov __printf(2, 3) void bpf_log(struct bpf_verifier_log *log,
6829e15db66SAlexei Starovoitov 			    const char *fmt, ...);
683bdcab414SAndrii Nakryiko int bpf_vlog_init(struct bpf_verifier_log *log, u32 log_level,
684bdcab414SAndrii Nakryiko 		  char __user *log_buf, u32 log_size);
68512166409SAndrii Nakryiko void bpf_vlog_reset(struct bpf_verifier_log *log, u64 new_pos);
686bdcab414SAndrii Nakryiko int bpf_vlog_finalize(struct bpf_verifier_log *log, u32 *log_size_actual);
687430e68d1SQuentin Monnet 
cur_func(struct bpf_verifier_env * env)688fd978bf7SJoe Stringer static inline struct bpf_func_state *cur_func(struct bpf_verifier_env *env)
689638f5b90SAlexei Starovoitov {
690f4d7e40aSAlexei Starovoitov 	struct bpf_verifier_state *cur = env->cur_state;
691f4d7e40aSAlexei Starovoitov 
692fd978bf7SJoe Stringer 	return cur->frame[cur->curframe];
693fd978bf7SJoe Stringer }
694fd978bf7SJoe Stringer 
cur_regs(struct bpf_verifier_env * env)695fd978bf7SJoe Stringer static inline struct bpf_reg_state *cur_regs(struct bpf_verifier_env *env)
696fd978bf7SJoe Stringer {
697fd978bf7SJoe Stringer 	return cur_func(env)->regs;
698638f5b90SAlexei Starovoitov }
699638f5b90SAlexei Starovoitov 
700a40a2632SQuentin Monnet int bpf_prog_offload_verifier_prep(struct bpf_prog *prog);
701cae1927cSJakub Kicinski int bpf_prog_offload_verify_insn(struct bpf_verifier_env *env,
702cae1927cSJakub Kicinski 				 int insn_idx, int prev_insn_idx);
703c941ce9cSQuentin Monnet int bpf_prog_offload_finalize(struct bpf_verifier_env *env);
70408ca90afSJakub Kicinski void
70508ca90afSJakub Kicinski bpf_prog_offload_replace_insn(struct bpf_verifier_env *env, u32 off,
70608ca90afSJakub Kicinski 			      struct bpf_insn *insn);
70708ca90afSJakub Kicinski void
70808ca90afSJakub Kicinski bpf_prog_offload_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt);
709ab3f0063SJakub Kicinski 
710be80a1d3SDaniel Borkmann int check_ptr_off_reg(struct bpf_verifier_env *env,
71151c39bb1SAlexei Starovoitov 		      const struct bpf_reg_state *reg, int regno);
71225b35dd2SKumar Kartikeya Dwivedi int check_func_arg_reg_off(struct bpf_verifier_env *env,
71325b35dd2SKumar Kartikeya Dwivedi 			   const struct bpf_reg_state *reg, int regno,
7148f14852eSKumar Kartikeya Dwivedi 			   enum bpf_arg_type arg_type);
715e5069b9cSDmitrii Banshchikov int check_mem_reg(struct bpf_verifier_env *env, struct bpf_reg_state *reg,
716e5069b9cSDmitrii Banshchikov 		   u32 regno, u32 mem_size);
71751c39bb1SAlexei Starovoitov 
718f7b12b6fSToke Høiland-Jørgensen /* this lives here instead of in bpf.h because it needs to dereference tgt_prog */
bpf_trampoline_compute_key(const struct bpf_prog * tgt_prog,struct btf * btf,u32 btf_id)719f7b12b6fSToke Høiland-Jørgensen static inline u64 bpf_trampoline_compute_key(const struct bpf_prog *tgt_prog,
72022dc4a0fSAndrii Nakryiko 					     struct btf *btf, u32 btf_id)
721f7b12b6fSToke Høiland-Jørgensen {
72222dc4a0fSAndrii Nakryiko 	if (tgt_prog)
72322dc4a0fSAndrii Nakryiko 		return ((u64)tgt_prog->aux->id << 32) | btf_id;
72422dc4a0fSAndrii Nakryiko 	else
72522dc4a0fSAndrii Nakryiko 		return ((u64)btf_obj_id(btf) << 32) | 0x80000000 | btf_id;
726f7b12b6fSToke Høiland-Jørgensen }
727f7b12b6fSToke Høiland-Jørgensen 
728441e8c66SToke Høiland-Jørgensen /* unpack the IDs from the key as constructed above */
bpf_trampoline_unpack_key(u64 key,u32 * obj_id,u32 * btf_id)729441e8c66SToke Høiland-Jørgensen static inline void bpf_trampoline_unpack_key(u64 key, u32 *obj_id, u32 *btf_id)
730441e8c66SToke Høiland-Jørgensen {
731441e8c66SToke Høiland-Jørgensen 	if (obj_id)
732441e8c66SToke Høiland-Jørgensen 		*obj_id = key >> 32;
733441e8c66SToke Høiland-Jørgensen 	if (btf_id)
734441e8c66SToke Høiland-Jørgensen 		*btf_id = key & 0x7FFFFFFF;
735441e8c66SToke Høiland-Jørgensen }
736441e8c66SToke Høiland-Jørgensen 
737f7b12b6fSToke Høiland-Jørgensen int bpf_check_attach_target(struct bpf_verifier_log *log,
738f7b12b6fSToke Høiland-Jørgensen 			    const struct bpf_prog *prog,
739f7b12b6fSToke Høiland-Jørgensen 			    const struct bpf_prog *tgt_prog,
740f7b12b6fSToke Høiland-Jørgensen 			    u32 btf_id,
741f7b12b6fSToke Høiland-Jørgensen 			    struct bpf_attach_target_info *tgt_info);
7422357672cSKumar Kartikeya Dwivedi void bpf_free_kfunc_btf_tab(struct bpf_kfunc_btf_tab *tab);
7432357672cSKumar Kartikeya Dwivedi 
744eb1f7f71SBenjamin Tissoires int mark_chain_precision(struct bpf_verifier_env *env, int regno);
745eb1f7f71SBenjamin Tissoires 
746d639b9d1SHao Luo #define BPF_BASE_TYPE_MASK	GENMASK(BPF_BASE_TYPE_BITS - 1, 0)
747d639b9d1SHao Luo 
748d639b9d1SHao Luo /* extract base type from bpf_{arg, return, reg}_type. */
base_type(u32 type)749d639b9d1SHao Luo static inline u32 base_type(u32 type)
750d639b9d1SHao Luo {
751d639b9d1SHao Luo 	return type & BPF_BASE_TYPE_MASK;
752d639b9d1SHao Luo }
753d639b9d1SHao Luo 
754d639b9d1SHao Luo /* extract flags from an extended type. See bpf_type_flag in bpf.h. */
type_flag(u32 type)755d639b9d1SHao Luo static inline u32 type_flag(u32 type)
756d639b9d1SHao Luo {
757d639b9d1SHao Luo 	return type & ~BPF_BASE_TYPE_MASK;
758d639b9d1SHao Luo }
759f7b12b6fSToke Høiland-Jørgensen 
7604a9c7bbeSMartin KaFai Lau /* only use after check_attach_btf_id() */
resolve_prog_type(const struct bpf_prog * prog)761271de525SMartin KaFai Lau static inline enum bpf_prog_type resolve_prog_type(const struct bpf_prog *prog)
7625c073f26SKumar Kartikeya Dwivedi {
7634a9c7bbeSMartin KaFai Lau 	return prog->type == BPF_PROG_TYPE_EXT ?
7644a9c7bbeSMartin KaFai Lau 		prog->aux->dst_prog->type : prog->type;
7655c073f26SKumar Kartikeya Dwivedi }
7665c073f26SKumar Kartikeya Dwivedi 
bpf_prog_check_recur(const struct bpf_prog * prog)767271de525SMartin KaFai Lau static inline bool bpf_prog_check_recur(const struct bpf_prog *prog)
768271de525SMartin KaFai Lau {
769271de525SMartin KaFai Lau 	switch (resolve_prog_type(prog)) {
770271de525SMartin KaFai Lau 	case BPF_PROG_TYPE_TRACING:
771271de525SMartin KaFai Lau 		return prog->expected_attach_type != BPF_TRACE_ITER;
772271de525SMartin KaFai Lau 	case BPF_PROG_TYPE_STRUCT_OPS:
773271de525SMartin KaFai Lau 	case BPF_PROG_TYPE_LSM:
774271de525SMartin KaFai Lau 		return false;
775271de525SMartin KaFai Lau 	default:
776271de525SMartin KaFai Lau 		return true;
777271de525SMartin KaFai Lau 	}
778271de525SMartin KaFai Lau }
779271de525SMartin KaFai Lau 
7802a6d50b5SDave Marchevsky #define BPF_REG_TRUSTED_MODIFIERS (MEM_ALLOC | PTR_TRUSTED | NON_OWN_REF)
7813f00c523SDavid Vernet 
bpf_type_has_unsafe_modifiers(u32 type)7823f00c523SDavid Vernet static inline bool bpf_type_has_unsafe_modifiers(u32 type)
7833f00c523SDavid Vernet {
7843f00c523SDavid Vernet 	return type_flag(type) & ~BPF_REG_TRUSTED_MODIFIERS;
7853f00c523SDavid Vernet }
7863f00c523SDavid Vernet 
78758e2af8bSJakub Kicinski #endif /* _LINUX_BPF_VERIFIER_H */
788