• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
3  */
4 #ifndef _LINUX_BPF_VERIFIER_H
5 #define _LINUX_BPF_VERIFIER_H 1
6 
7 #include <linux/bpf.h> /* for enum bpf_reg_type */
8 #include <linux/btf.h> /* for struct btf and btf_id() */
9 #include <linux/filter.h> /* for MAX_BPF_STACK */
10 #include <linux/tnum.h>
11 #include <linux/android_kabi.h>
12 
13 /* Maximum variable offset umax_value permitted when resolving memory accesses.
14  * In practice this is far bigger than any realistic pointer offset; this limit
15  * ensures that umax_value + (int)off + (int)size cannot overflow a u64.
16  */
17 #define BPF_MAX_VAR_OFF	(1 << 29)
18 /* Maximum variable size permitted for ARG_CONST_SIZE[_OR_ZERO].  This ensures
19  * that converting umax_value to int cannot overflow.
20  */
21 #define BPF_MAX_VAR_SIZ	(1 << 29)
22 /* size of type_str_buf in bpf_verifier. */
23 #define TYPE_STR_BUF_LEN 64
24 
25 /* Liveness marks, used for registers and spilled-regs (in stack slots).
26  * Read marks propagate upwards until they find a write mark; they record that
27  * "one of this state's descendants read this reg" (and therefore the reg is
28  * relevant for states_equal() checks).
29  * Write marks collect downwards and do not propagate; they record that "the
30  * straight-line code that reached this state (from its parent) wrote this reg"
31  * (and therefore that reads propagated from this state or its descendants
32  * should not propagate to its parent).
33  * A state with a write mark can receive read marks; it just won't propagate
34  * them to its parent, since the write mark is a property, not of the state,
35  * but of the link between it and its parent.  See mark_reg_read() and
36  * mark_stack_slot_read() in kernel/bpf/verifier.c.
37  */
38 enum bpf_reg_liveness {
39 	REG_LIVE_NONE = 0, /* reg hasn't been read or written this branch */
40 	REG_LIVE_READ32 = 0x1, /* reg was read, so we're sensitive to initial value */
41 	REG_LIVE_READ64 = 0x2, /* likewise, but full 64-bit content matters */
42 	REG_LIVE_READ = REG_LIVE_READ32 | REG_LIVE_READ64,
43 	REG_LIVE_WRITTEN = 0x4, /* reg was written first, screening off later reads */
44 	REG_LIVE_DONE = 0x8, /* liveness won't be updating this register anymore */
45 };
46 
47 struct bpf_reg_state {
48 	/* Ordering of fields matters.  See states_equal() */
49 	enum bpf_reg_type type;
50 	/* Fixed part of pointer offset, pointer types only */
51 	s32 off;
52 	union {
53 		/* valid when type == PTR_TO_PACKET */
54 		int range;
55 
56 		/* valid when type == CONST_PTR_TO_MAP | PTR_TO_MAP_VALUE |
57 		 *   PTR_TO_MAP_VALUE_OR_NULL
58 		 */
59 		struct {
60 			struct bpf_map *map_ptr;
61 			/* To distinguish map lookups from outer map
62 			 * the map_uid is non-zero for registers
63 			 * pointing to inner maps.
64 			 */
65 			u32 map_uid;
66 		};
67 
68 		/* for PTR_TO_BTF_ID */
69 		struct {
70 			struct btf *btf;
71 			u32 btf_id;
72 		};
73 
74 		u32 mem_size; /* for PTR_TO_MEM | PTR_TO_MEM_OR_NULL */
75 
76 		/* Max size from any of the above. */
77 		struct {
78 			unsigned long raw1;
79 			unsigned long raw2;
80 		} raw;
81 
82 		u32 subprogno; /* for PTR_TO_FUNC */
83 	};
84 	/* For PTR_TO_PACKET, used to find other pointers with the same variable
85 	 * offset, so they can share range knowledge.
86 	 * For PTR_TO_MAP_VALUE_OR_NULL this is used to share which map value we
87 	 * came from, when one is tested for != NULL.
88 	 * For PTR_TO_MEM_OR_NULL this is used to identify memory allocation
89 	 * for the purpose of tracking that it's freed.
90 	 * For PTR_TO_SOCKET this is used to share which pointers retain the
91 	 * same reference to the socket, to determine proper reference freeing.
92 	 */
93 	u32 id;
94 	/* PTR_TO_SOCKET and PTR_TO_TCP_SOCK could be a ptr returned
95 	 * from a pointer-cast helper, bpf_sk_fullsock() and
96 	 * bpf_tcp_sock().
97 	 *
98 	 * Consider the following where "sk" is a reference counted
99 	 * pointer returned from "sk = bpf_sk_lookup_tcp();":
100 	 *
101 	 * 1: sk = bpf_sk_lookup_tcp();
102 	 * 2: if (!sk) { return 0; }
103 	 * 3: fullsock = bpf_sk_fullsock(sk);
104 	 * 4: if (!fullsock) { bpf_sk_release(sk); return 0; }
105 	 * 5: tp = bpf_tcp_sock(fullsock);
106 	 * 6: if (!tp) { bpf_sk_release(sk); return 0; }
107 	 * 7: bpf_sk_release(sk);
108 	 * 8: snd_cwnd = tp->snd_cwnd;  // verifier will complain
109 	 *
110 	 * After bpf_sk_release(sk) at line 7, both "fullsock" ptr and
111 	 * "tp" ptr should be invalidated also.  In order to do that,
112 	 * the reg holding "fullsock" and "sk" need to remember
113 	 * the original refcounted ptr id (i.e. sk_reg->id) in ref_obj_id
114 	 * such that the verifier can reset all regs which have
115 	 * ref_obj_id matching the sk_reg->id.
116 	 *
117 	 * sk_reg->ref_obj_id is set to sk_reg->id at line 1.
118 	 * sk_reg->id will stay as NULL-marking purpose only.
119 	 * After NULL-marking is done, sk_reg->id can be reset to 0.
120 	 *
121 	 * After "fullsock = bpf_sk_fullsock(sk);" at line 3,
122 	 * fullsock_reg->ref_obj_id is set to sk_reg->ref_obj_id.
123 	 *
124 	 * After "tp = bpf_tcp_sock(fullsock);" at line 5,
125 	 * tp_reg->ref_obj_id is set to fullsock_reg->ref_obj_id
126 	 * which is the same as sk_reg->ref_obj_id.
127 	 *
128 	 * From the verifier perspective, if sk, fullsock and tp
129 	 * are not NULL, they are the same ptr with different
130 	 * reg->type.  In particular, bpf_sk_release(tp) is also
131 	 * allowed and has the same effect as bpf_sk_release(sk).
132 	 */
133 	u32 ref_obj_id;
134 	/* For scalar types (SCALAR_VALUE), this represents our knowledge of
135 	 * the actual value.
136 	 * For pointer types, this represents the variable part of the offset
137 	 * from the pointed-to object, and is shared with all bpf_reg_states
138 	 * with the same id as us.
139 	 */
140 	struct tnum var_off;
141 	/* Used to determine if any memory access using this register will
142 	 * result in a bad access.
143 	 * These refer to the same value as var_off, not necessarily the actual
144 	 * contents of the register.
145 	 */
146 	s64 smin_value; /* minimum possible (s64)value */
147 	s64 smax_value; /* maximum possible (s64)value */
148 	u64 umin_value; /* minimum possible (u64)value */
149 	u64 umax_value; /* maximum possible (u64)value */
150 	s32 s32_min_value; /* minimum possible (s32)value */
151 	s32 s32_max_value; /* maximum possible (s32)value */
152 	u32 u32_min_value; /* minimum possible (u32)value */
153 	u32 u32_max_value; /* maximum possible (u32)value */
154 	/* parentage chain for liveness checking */
155 	struct bpf_reg_state *parent;
156 	/* Inside the callee two registers can be both PTR_TO_STACK like
157 	 * R1=fp-8 and R2=fp-8, but one of them points to this function stack
158 	 * while another to the caller's stack. To differentiate them 'frameno'
159 	 * is used which is an index in bpf_verifier_state->frame[] array
160 	 * pointing to bpf_func_state.
161 	 */
162 	u32 frameno;
163 	/* Tracks subreg definition. The stored value is the insn_idx of the
164 	 * writing insn. This is safe because subreg_def is used before any insn
165 	 * patching which only happens after main verification finished.
166 	 */
167 	s32 subreg_def;
168 	enum bpf_reg_liveness live;
169 	/* if (!precise && SCALAR_VALUE) min/max/tnum don't affect safety */
170 	bool precise;
171 };
172 
173 enum bpf_stack_slot_type {
174 	STACK_INVALID,    /* nothing was stored in this stack slot */
175 	STACK_SPILL,      /* register spilled into stack */
176 	STACK_MISC,	  /* BPF program wrote some data into this slot */
177 	STACK_ZERO,	  /* BPF program wrote constant zero */
178 };
179 
180 #define BPF_REG_SIZE 8	/* size of eBPF register in bytes */
181 
182 struct bpf_stack_state {
183 	struct bpf_reg_state spilled_ptr;
184 	u8 slot_type[BPF_REG_SIZE];
185 };
186 
187 struct bpf_reference_state {
188 	/* Track each reference created with a unique id, even if the same
189 	 * instruction creates the reference multiple times (eg, via CALL).
190 	 */
191 	int id;
192 	/* Instruction where the allocation of this reference occurred. This
193 	 * is used purely to inform the user of a reference leak.
194 	 */
195 	int insn_idx;
196 };
197 
198 /* state of the program:
199  * type of all registers and stack info
200  */
201 struct bpf_func_state {
202 	struct bpf_reg_state regs[MAX_BPF_REG];
203 	/* index of call instruction that called into this func */
204 	int callsite;
205 	/* stack frame number of this function state from pov of
206 	 * enclosing bpf_verifier_state.
207 	 * 0 = main function, 1 = first callee.
208 	 */
209 	u32 frameno;
210 	/* subprog number == index within subprog_info
211 	 * zero == main subprog
212 	 */
213 	u32 subprogno;
214 	/* Every bpf_timer_start will increment async_entry_cnt.
215 	 * It's used to distinguish:
216 	 * void foo(void) { for(;;); }
217 	 * void foo(void) { bpf_timer_set_callback(,foo); }
218 	 */
219 	u32 async_entry_cnt;
220 	bool in_callback_fn;
221 	bool in_async_callback_fn;
222 
223 	/* The following fields should be last. See copy_func_state() */
224 	int acquired_refs;
225 	struct bpf_reference_state *refs;
226 	int allocated_stack;
227 	struct bpf_stack_state *stack;
228 };
229 
230 struct bpf_idx_pair {
231 	u32 prev_idx;
232 	u32 idx;
233 };
234 
235 struct bpf_id_pair {
236 	u32 old;
237 	u32 cur;
238 };
239 
240 /* Maximum number of register states that can exist at once */
241 #define BPF_ID_MAP_SIZE (MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE)
242 #define MAX_CALL_FRAMES 8
243 struct bpf_verifier_state {
244 	/* call stack tracking */
245 	struct bpf_func_state *frame[MAX_CALL_FRAMES];
246 	struct bpf_verifier_state *parent;
247 	/*
248 	 * 'branches' field is the number of branches left to explore:
249 	 * 0 - all possible paths from this state reached bpf_exit or
250 	 * were safely pruned
251 	 * 1 - at least one path is being explored.
252 	 * This state hasn't reached bpf_exit
253 	 * 2 - at least two paths are being explored.
254 	 * This state is an immediate parent of two children.
255 	 * One is fallthrough branch with branches==1 and another
256 	 * state is pushed into stack (to be explored later) also with
257 	 * branches==1. The parent of this state has branches==1.
258 	 * The verifier state tree connected via 'parent' pointer looks like:
259 	 * 1
260 	 * 1
261 	 * 2 -> 1 (first 'if' pushed into stack)
262 	 * 1
263 	 * 2 -> 1 (second 'if' pushed into stack)
264 	 * 1
265 	 * 1
266 	 * 1 bpf_exit.
267 	 *
268 	 * Once do_check() reaches bpf_exit, it calls update_branch_counts()
269 	 * and the verifier state tree will look:
270 	 * 1
271 	 * 1
272 	 * 2 -> 1 (first 'if' pushed into stack)
273 	 * 1
274 	 * 1 -> 1 (second 'if' pushed into stack)
275 	 * 0
276 	 * 0
277 	 * 0 bpf_exit.
278 	 * After pop_stack() the do_check() will resume at second 'if'.
279 	 *
280 	 * If is_state_visited() sees a state with branches > 0 it means
281 	 * there is a loop. If such state is exactly equal to the current state
282 	 * it's an infinite loop. Note states_equal() checks for states
283 	 * equvalency, so two states being 'states_equal' does not mean
284 	 * infinite loop. The exact comparison is provided by
285 	 * states_maybe_looping() function. It's a stronger pre-check and
286 	 * much faster than states_equal().
287 	 *
288 	 * This algorithm may not find all possible infinite loops or
289 	 * loop iteration count may be too high.
290 	 * In such cases BPF_COMPLEXITY_LIMIT_INSNS limit kicks in.
291 	 */
292 	u32 branches;
293 	u32 insn_idx;
294 	u32 curframe;
295 	u32 active_spin_lock;
296 	bool speculative;
297 
298 	/* first and last insn idx of this verifier state */
299 	u32 first_insn_idx;
300 	u32 last_insn_idx;
301 	/* jmp history recorded from first to last.
302 	 * backtracking is using it to go from last to first.
303 	 * For most states jmp_history_cnt is [0-3].
304 	 * For loops can go up to ~40.
305 	 */
306 	struct bpf_idx_pair *jmp_history;
307 	u32 jmp_history_cnt;
308 };
309 
310 #define bpf_get_spilled_reg(slot, frame)				\
311 	(((slot < frame->allocated_stack / BPF_REG_SIZE) &&		\
312 	  (frame->stack[slot].slot_type[0] == STACK_SPILL))		\
313 	 ? &frame->stack[slot].spilled_ptr : NULL)
314 
315 /* Iterate over 'frame', setting 'reg' to either NULL or a spilled register. */
316 #define bpf_for_each_spilled_reg(iter, frame, reg)			\
317 	for (iter = 0, reg = bpf_get_spilled_reg(iter, frame);		\
318 	     iter < frame->allocated_stack / BPF_REG_SIZE;		\
319 	     iter++, reg = bpf_get_spilled_reg(iter, frame))
320 
321 /* Invoke __expr over regsiters in __vst, setting __state and __reg */
322 #define bpf_for_each_reg_in_vstate(__vst, __state, __reg, __expr)   \
323 	({                                                               \
324 		struct bpf_verifier_state *___vstate = __vst;            \
325 		int ___i, ___j;                                          \
326 		for (___i = 0; ___i <= ___vstate->curframe; ___i++) {    \
327 			struct bpf_reg_state *___regs;                   \
328 			__state = ___vstate->frame[___i];                \
329 			___regs = __state->regs;                         \
330 			for (___j = 0; ___j < MAX_BPF_REG; ___j++) {     \
331 				__reg = &___regs[___j];                  \
332 				(void)(__expr);                          \
333 			}                                                \
334 			bpf_for_each_spilled_reg(___j, __state, __reg) { \
335 				if (!__reg)                              \
336 					continue;                        \
337 				(void)(__expr);                          \
338 			}                                                \
339 		}                                                        \
340 	})
341 
342 /* linked list of verifier states used to prune search */
343 struct bpf_verifier_state_list {
344 	struct bpf_verifier_state state;
345 	struct bpf_verifier_state_list *next;
346 	int miss_cnt, hit_cnt;
347 };
348 
349 /* Possible states for alu_state member. */
350 #define BPF_ALU_SANITIZE_SRC		(1U << 0)
351 #define BPF_ALU_SANITIZE_DST		(1U << 1)
352 #define BPF_ALU_NEG_VALUE		(1U << 2)
353 #define BPF_ALU_NON_POINTER		(1U << 3)
354 #define BPF_ALU_IMMEDIATE		(1U << 4)
355 #define BPF_ALU_SANITIZE		(BPF_ALU_SANITIZE_SRC | \
356 					 BPF_ALU_SANITIZE_DST)
357 
358 struct bpf_insn_aux_data {
359 	union {
360 		enum bpf_reg_type ptr_type;	/* pointer type for load/store insns */
361 		unsigned long map_ptr_state;	/* pointer/poison value for maps */
362 		s32 call_imm;			/* saved imm field of call insn */
363 		u32 alu_limit;			/* limit for add/sub register with pointer */
364 		struct {
365 			u32 map_index;		/* index into used_maps[] */
366 			u32 map_off;		/* offset from value base address */
367 		};
368 		struct {
369 			enum bpf_reg_type reg_type;	/* type of pseudo_btf_id */
370 			union {
371 				struct {
372 					struct btf *btf;
373 					u32 btf_id;	/* btf_id for struct typed var */
374 				};
375 				u32 mem_size;	/* mem_size for non-struct typed var */
376 			};
377 		} btf_var;
378 	};
379 	u64 map_key_state; /* constant (32 bit) key tracking for maps */
380 	int ctx_field_size; /* the ctx field size for load insn, maybe 0 */
381 	u32 seen; /* this insn was processed by the verifier at env->pass_cnt */
382 	bool sanitize_stack_spill; /* subject to Spectre v4 sanitation */
383 	bool zext_dst; /* this insn zero extends dst reg */
384 	u8 alu_state; /* used in combination with alu_limit */
385 
386 	/* below fields are initialized once */
387 	unsigned int orig_idx; /* original instruction index */
388 	bool prune_point;
389 };
390 
391 #define MAX_USED_MAPS 64 /* max number of maps accessed by one eBPF program */
392 #define MAX_USED_BTFS 64 /* max number of BTFs accessed by one BPF program */
393 
394 #define BPF_VERIFIER_TMP_LOG_SIZE	1024
395 
396 struct bpf_verifier_log {
397 	u32 level;
398 	char kbuf[BPF_VERIFIER_TMP_LOG_SIZE];
399 	char __user *ubuf;
400 	u32 len_used;
401 	u32 len_total;
402 };
403 
bpf_verifier_log_full(const struct bpf_verifier_log * log)404 static inline bool bpf_verifier_log_full(const struct bpf_verifier_log *log)
405 {
406 	return log->len_used >= log->len_total - 1;
407 }
408 
409 #define BPF_LOG_LEVEL1	1
410 #define BPF_LOG_LEVEL2	2
411 #define BPF_LOG_STATS	4
412 #define BPF_LOG_LEVEL	(BPF_LOG_LEVEL1 | BPF_LOG_LEVEL2)
413 #define BPF_LOG_MASK	(BPF_LOG_LEVEL | BPF_LOG_STATS)
414 #define BPF_LOG_KERNEL	(BPF_LOG_MASK + 1) /* kernel internal flag */
415 
bpf_verifier_log_needed(const struct bpf_verifier_log * log)416 static inline bool bpf_verifier_log_needed(const struct bpf_verifier_log *log)
417 {
418 	return log &&
419 		((log->level && log->ubuf && !bpf_verifier_log_full(log)) ||
420 		 log->level == BPF_LOG_KERNEL);
421 }
422 
423 static inline bool
bpf_verifier_log_attr_valid(const struct bpf_verifier_log * log)424 bpf_verifier_log_attr_valid(const struct bpf_verifier_log *log)
425 {
426 	return log->len_total >= 128 && log->len_total <= UINT_MAX >> 2 &&
427 	       log->level && log->ubuf && !(log->level & ~BPF_LOG_MASK);
428 }
429 
430 #define BPF_MAX_SUBPROGS 256
431 
432 struct bpf_subprog_info {
433 	/* 'start' has to be the first field otherwise find_subprog() won't work */
434 	u32 start; /* insn idx of function entry point */
435 	u32 linfo_idx; /* The idx to the main_prog->aux->linfo */
436 	u16 stack_depth; /* max. stack depth used by this function */
437 	bool has_tail_call;
438 	bool tail_call_reachable;
439 	bool has_ld_abs;
440 	bool is_async_cb;
441 
442 	ANDROID_KABI_RESERVE(1);
443 };
444 
445 /* single container for all structs
446  * one verifier_env per bpf_check() call
447  */
448 struct bpf_verifier_env {
449 	u32 insn_idx;
450 	u32 prev_insn_idx;
451 	struct bpf_prog *prog;		/* eBPF program being verified */
452 	const struct bpf_verifier_ops *ops;
453 	struct bpf_verifier_stack_elem *head; /* stack of verifier states to be processed */
454 	int stack_size;			/* number of states to be processed */
455 	bool strict_alignment;		/* perform strict pointer alignment checks */
456 	bool test_state_freq;		/* test verifier with different pruning frequency */
457 	struct bpf_verifier_state *cur_state; /* current verifier state */
458 	struct bpf_verifier_state_list **explored_states; /* search pruning optimization */
459 	struct bpf_verifier_state_list *free_list;
460 	struct bpf_map *used_maps[MAX_USED_MAPS]; /* array of map's used by eBPF program */
461 	struct btf_mod_pair used_btfs[MAX_USED_BTFS]; /* array of BTF's used by BPF program */
462 	u32 used_map_cnt;		/* number of used maps */
463 	u32 used_btf_cnt;		/* number of used BTF objects */
464 	u32 id_gen;			/* used to generate unique reg IDs */
465 	bool explore_alu_limits;
466 	bool allow_ptr_leaks;
467 	bool allow_uninit_stack;
468 	bool allow_ptr_to_map_access;
469 	bool bpf_capable;
470 	bool bypass_spec_v1;
471 	bool bypass_spec_v4;
472 	bool seen_direct_write;
473 	struct bpf_insn_aux_data *insn_aux_data; /* array of per-insn state */
474 	const struct bpf_line_info *prev_linfo;
475 	struct bpf_verifier_log log;
476 	struct bpf_subprog_info subprog_info[BPF_MAX_SUBPROGS + 1];
477 	struct bpf_id_pair idmap_scratch[BPF_ID_MAP_SIZE];
478 	struct {
479 		int *insn_state;
480 		int *insn_stack;
481 		int cur_stack;
482 	} cfg;
483 	u32 pass_cnt; /* number of times do_check() was called */
484 	u32 subprog_cnt;
485 	/* number of instructions analyzed by the verifier */
486 	u32 prev_insn_processed, insn_processed;
487 	/* number of jmps, calls, exits analyzed so far */
488 	u32 prev_jmps_processed, jmps_processed;
489 	/* total verification time */
490 	u64 verification_time;
491 	/* maximum number of verifier states kept in 'branching' instructions */
492 	u32 max_states_per_insn;
493 	/* total number of allocated verifier states */
494 	u32 total_states;
495 	/* some states are freed during program analysis.
496 	 * this is peak number of states. this number dominates kernel
497 	 * memory consumption during verification
498 	 */
499 	u32 peak_states;
500 	/* longest register parentage chain walked for liveness marking */
501 	u32 longest_mark_read_walk;
502 	bpfptr_t fd_array;
503 	/* buffer used in reg_type_str() to generate reg_type string */
504 	char type_str_buf[TYPE_STR_BUF_LEN];
505 
506 	ANDROID_KABI_RESERVE(1);
507 	ANDROID_KABI_RESERVE(2);
508 };
509 
510 __printf(2, 0) void bpf_verifier_vlog(struct bpf_verifier_log *log,
511 				      const char *fmt, va_list args);
512 __printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env,
513 					   const char *fmt, ...);
514 __printf(2, 3) void bpf_log(struct bpf_verifier_log *log,
515 			    const char *fmt, ...);
516 
cur_func(struct bpf_verifier_env * env)517 static inline struct bpf_func_state *cur_func(struct bpf_verifier_env *env)
518 {
519 	struct bpf_verifier_state *cur = env->cur_state;
520 
521 	return cur->frame[cur->curframe];
522 }
523 
cur_regs(struct bpf_verifier_env * env)524 static inline struct bpf_reg_state *cur_regs(struct bpf_verifier_env *env)
525 {
526 	return cur_func(env)->regs;
527 }
528 
529 int bpf_prog_offload_verifier_prep(struct bpf_prog *prog);
530 int bpf_prog_offload_verify_insn(struct bpf_verifier_env *env,
531 				 int insn_idx, int prev_insn_idx);
532 int bpf_prog_offload_finalize(struct bpf_verifier_env *env);
533 void
534 bpf_prog_offload_replace_insn(struct bpf_verifier_env *env, u32 off,
535 			      struct bpf_insn *insn);
536 void
537 bpf_prog_offload_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt);
538 
539 int check_ctx_reg(struct bpf_verifier_env *env,
540 		  const struct bpf_reg_state *reg, int regno);
541 int check_mem_reg(struct bpf_verifier_env *env, struct bpf_reg_state *reg,
542 		   u32 regno, u32 mem_size);
543 
544 /* 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)545 static inline u64 bpf_trampoline_compute_key(const struct bpf_prog *tgt_prog,
546 					     struct btf *btf, u32 btf_id)
547 {
548 	if (tgt_prog)
549 		return ((u64)tgt_prog->aux->id << 32) | btf_id;
550 	else
551 		return ((u64)btf_obj_id(btf) << 32) | 0x80000000 | btf_id;
552 }
553 
554 /* unpack the IDs from the key as constructed above */
bpf_trampoline_unpack_key(u64 key,u32 * obj_id,u32 * btf_id)555 static inline void bpf_trampoline_unpack_key(u64 key, u32 *obj_id, u32 *btf_id)
556 {
557 	if (obj_id)
558 		*obj_id = key >> 32;
559 	if (btf_id)
560 		*btf_id = key & 0x7FFFFFFF;
561 }
562 
563 int bpf_check_attach_target(struct bpf_verifier_log *log,
564 			    const struct bpf_prog *prog,
565 			    const struct bpf_prog *tgt_prog,
566 			    u32 btf_id,
567 			    struct bpf_attach_target_info *tgt_info);
568 
569 #define BPF_BASE_TYPE_MASK	GENMASK(BPF_BASE_TYPE_BITS - 1, 0)
570 
571 /* extract base type from bpf_{arg, return, reg}_type. */
base_type(u32 type)572 static inline u32 base_type(u32 type)
573 {
574 	return type & BPF_BASE_TYPE_MASK;
575 }
576 
577 /* extract flags from an extended type. See bpf_type_flag in bpf.h. */
type_flag(u32 type)578 static inline u32 type_flag(u32 type)
579 {
580 	return type & ~BPF_BASE_TYPE_MASK;
581 }
582 
583 #endif /* _LINUX_BPF_VERIFIER_H */
584