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/cnum.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_MEM_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 tmp_str_buf in bpf_verifier.
23 * we need at least 306 bytes to fit full stack mask representation
24 * (in the "-8,-16,...,-512" form)
25 */
26 #define TMP_STR_BUF_LEN 320
27 /* Patch buffer size */
28 #define INSN_BUF_SIZE 32
29
30 #define ITER_PREFIX "bpf_iter_"
31
32 enum bpf_iter_state {
33 BPF_ITER_STATE_INVALID, /* for non-first slot */
34 BPF_ITER_STATE_ACTIVE,
35 BPF_ITER_STATE_DRAINED,
36 };
37
38 struct bpf_reg_state {
39 /* Ordering of fields matters. See states_equal() */
40 enum bpf_reg_type type;
41 /*
42 * Constant delta between "linked" scalars with the same ID.
43 */
44 s32 delta;
45 union {
46 /* valid when type == PTR_TO_PACKET */
47 int range;
48
49 /* valid when type == CONST_PTR_TO_MAP | PTR_TO_MAP_VALUE |
50 * PTR_TO_MAP_VALUE_OR_NULL
51 */
52 struct {
53 struct bpf_map *map_ptr;
54 /* To distinguish map lookups from outer map
55 * the map_uid is non-zero for registers
56 * pointing to inner maps.
57 */
58 u32 map_uid;
59 };
60
61 /* for PTR_TO_BTF_ID */
62 struct {
63 struct btf *btf;
64 u32 btf_id;
65 };
66
67 struct { /* for PTR_TO_MEM | PTR_TO_MEM_OR_NULL */
68 u32 mem_size;
69 };
70
71 /* For dynptr stack slots */
72 struct {
73 enum bpf_dynptr_type type;
74 /* A dynptr is 16 bytes so it takes up 2 stack slots.
75 * We need to track which slot is the first slot
76 * to protect against cases where the user may try to
77 * pass in an address starting at the second slot of the
78 * dynptr.
79 */
80 bool first_slot;
81 } dynptr;
82
83 /* For bpf_iter stack slots */
84 struct {
85 /* BTF container and BTF type ID describing
86 * struct bpf_iter_<type> of an iterator state
87 */
88 struct btf *btf;
89 u32 btf_id;
90 /* packing following two fields to fit iter state into 16 bytes */
91 enum bpf_iter_state state:2;
92 int depth:30;
93 } iter;
94
95 /* For irq stack slots */
96 struct {
97 enum {
98 IRQ_NATIVE_KFUNC,
99 IRQ_LOCK_KFUNC,
100 } kfunc_class;
101 } irq;
102
103 /* Max size from any of the above. */
104 struct {
105 unsigned long raw1;
106 unsigned long raw2;
107 } raw;
108
109 u32 subprogno; /* for PTR_TO_FUNC */
110 };
111 /* For scalar types (SCALAR_VALUE), this represents our knowledge of
112 * the actual value.
113 * For pointer types, this represents the variable part of the offset
114 * from the pointed-to object, and is shared with all bpf_reg_states
115 * with the same id as us.
116 */
117 struct tnum var_off;
118 /* Used to determine if any memory access using this register will
119 * result in a bad access.
120 * These refer to the same value as var_off, not necessarily the actual
121 * contents of the register.
122 */
123 struct cnum64 r64; /* 64-bit range as circular number */
124 struct cnum32 r32; /* 32-bit range as circular number */
125 /* For PTR_TO_PACKET, used to find other pointers with the same variable
126 * offset, so they can share range knowledge.
127 * For PTR_TO_MAP_VALUE_OR_NULL this is used to share which map value we
128 * came from, when one is tested for != NULL.
129 * For PTR_TO_MEM_OR_NULL this is used to identify memory allocation
130 * for the purpose of tracking that it's freed.
131 * For PTR_TO_SOCKET this is used to share which pointers retain the
132 * same reference to the socket, to determine proper reference freeing.
133 * For stack slots that are dynptrs, this is used to track references to
134 * the dynptr to determine proper reference freeing.
135 * Similarly to dynptrs, we use ID to track "belonging" of a reference
136 * to a specific instance of bpf_iter.
137 */
138 /*
139 * Upper bit of ID is used to remember relationship between "linked"
140 * registers. Example:
141 * r1 = r2; both will have r1->id == r2->id == N
142 * r1 += 10; r1->id == N | BPF_ADD_CONST and r1->delta == 10
143 * r3 = r2; both will have r3->id == r2->id == N
144 * w3 += 10; r3->id == N | BPF_ADD_CONST32 and r3->delta == 10
145 */
146 #define BPF_ADD_CONST64 (1U << 31)
147 #define BPF_ADD_CONST32 (1U << 30)
148 #define BPF_ADD_CONST (BPF_ADD_CONST64 | BPF_ADD_CONST32)
149 u32 id;
150 /*
151 * Tracks the parent object this register was derived from.
152 * Used for cascading invalidation: when the parent object is
153 * released or invalidated, all registers with matching parent_id
154 * are also invalidated. For example, a slice from bpf_dynptr_data()
155 * gets parent_id set to the dynptr's id.
156 */
157 u32 parent_id;
158 /* Inside the callee two registers can be both PTR_TO_STACK like
159 * R1=fp-8 and R2=fp-8, but one of them points to this function stack
160 * while another to the caller's stack. To differentiate them 'frameno'
161 * is used which is an index in bpf_verifier_state->frame[] array
162 * pointing to bpf_func_state.
163 */
164 u32 frameno;
165 /* if (!precise && SCALAR_VALUE) min/max/tnum don't affect safety */
166 bool precise;
167 };
168
reg_smin(const struct bpf_reg_state * reg)169 static inline s64 reg_smin(const struct bpf_reg_state *reg)
170 {
171 return cnum64_smin(reg->r64);
172 }
173
reg_smax(const struct bpf_reg_state * reg)174 static inline s64 reg_smax(const struct bpf_reg_state *reg)
175 {
176 return cnum64_smax(reg->r64);
177 }
178
reg_umin(const struct bpf_reg_state * reg)179 static inline u64 reg_umin(const struct bpf_reg_state *reg)
180 {
181 return cnum64_umin(reg->r64);
182 }
183
reg_umax(const struct bpf_reg_state * reg)184 static inline u64 reg_umax(const struct bpf_reg_state *reg)
185 {
186 return cnum64_umax(reg->r64);
187 }
188
reg_s32_min(const struct bpf_reg_state * reg)189 static inline s32 reg_s32_min(const struct bpf_reg_state *reg)
190 {
191 return cnum32_smin(reg->r32);
192 }
193
reg_s32_max(const struct bpf_reg_state * reg)194 static inline s32 reg_s32_max(const struct bpf_reg_state *reg)
195 {
196 return cnum32_smax(reg->r32);
197 }
198
reg_u32_min(const struct bpf_reg_state * reg)199 static inline u32 reg_u32_min(const struct bpf_reg_state *reg)
200 {
201 return cnum32_umin(reg->r32);
202 }
203
reg_u32_max(const struct bpf_reg_state * reg)204 static inline u32 reg_u32_max(const struct bpf_reg_state *reg)
205 {
206 return cnum32_umax(reg->r32);
207 }
208
reg_set_srange32(struct bpf_reg_state * reg,s32 smin,s32 smax)209 static inline void reg_set_srange32(struct bpf_reg_state *reg, s32 smin, s32 smax)
210 {
211 reg->r32 = cnum32_from_srange(smin, smax);
212 }
213
reg_set_urange32(struct bpf_reg_state * reg,u32 umin,u32 umax)214 static inline void reg_set_urange32(struct bpf_reg_state *reg, u32 umin, u32 umax)
215 {
216 reg->r32 = cnum32_from_urange(umin, umax);
217 }
218
reg_set_srange64(struct bpf_reg_state * reg,s64 smin,s64 smax)219 static inline void reg_set_srange64(struct bpf_reg_state *reg, s64 smin, s64 smax)
220 {
221 reg->r64 = cnum64_from_srange(smin, smax);
222 }
223
reg_set_urange64(struct bpf_reg_state * reg,u64 umin,u64 umax)224 static inline void reg_set_urange64(struct bpf_reg_state *reg, u64 umin, u64 umax)
225 {
226 reg->r64 = cnum64_from_urange(umin, umax);
227 }
228
229 enum bpf_stack_slot_type {
230 STACK_INVALID, /* nothing was stored in this stack slot */
231 STACK_SPILL, /* register spilled into stack */
232 STACK_MISC, /* BPF program wrote some data into this slot */
233 STACK_ZERO, /* BPF program wrote constant zero */
234 /* A dynptr is stored in this stack slot. The type of dynptr
235 * is stored in bpf_stack_state->spilled_ptr.dynptr.type
236 */
237 STACK_DYNPTR,
238 STACK_ITER,
239 STACK_IRQ_FLAG,
240 STACK_POISON,
241 };
242
243 #define BPF_REG_SIZE 8 /* size of eBPF register in bytes */
244
245 /* 4-byte stack slot granularity for liveness analysis */
246 #define BPF_HALF_REG_SIZE 4
247 #define STACK_SLOT_SZ 4
248 #define STACK_SLOTS (MAX_BPF_STACK / BPF_HALF_REG_SIZE) /* 128 */
249
250 typedef struct {
251 u64 v[2];
252 } spis_t;
253
254 #define SPIS_ZERO ((spis_t){})
255 #define SPIS_ALL ((spis_t){{ U64_MAX, U64_MAX }})
256
spis_is_zero(spis_t s)257 static inline bool spis_is_zero(spis_t s)
258 {
259 return s.v[0] == 0 && s.v[1] == 0;
260 }
261
spis_equal(spis_t a,spis_t b)262 static inline bool spis_equal(spis_t a, spis_t b)
263 {
264 return a.v[0] == b.v[0] && a.v[1] == b.v[1];
265 }
266
spis_or(spis_t a,spis_t b)267 static inline spis_t spis_or(spis_t a, spis_t b)
268 {
269 return (spis_t){{ a.v[0] | b.v[0], a.v[1] | b.v[1] }};
270 }
271
spis_and(spis_t a,spis_t b)272 static inline spis_t spis_and(spis_t a, spis_t b)
273 {
274 return (spis_t){{ a.v[0] & b.v[0], a.v[1] & b.v[1] }};
275 }
276
spis_not(spis_t s)277 static inline spis_t spis_not(spis_t s)
278 {
279 return (spis_t){{ ~s.v[0], ~s.v[1] }};
280 }
281
spis_test_bit(spis_t s,u32 slot)282 static inline bool spis_test_bit(spis_t s, u32 slot)
283 {
284 return s.v[slot / 64] & BIT_ULL(slot % 64);
285 }
286
spis_or_range(spis_t * mask,u32 lo,u32 hi)287 static inline void spis_or_range(spis_t *mask, u32 lo, u32 hi)
288 {
289 u32 w;
290
291 for (w = lo; w <= hi && w < STACK_SLOTS; w++)
292 mask->v[w / 64] |= BIT_ULL(w % 64);
293 }
294
295 #define BPF_REGMASK_ARGS ((1 << BPF_REG_1) | (1 << BPF_REG_2) | \
296 (1 << BPF_REG_3) | (1 << BPF_REG_4) | \
297 (1 << BPF_REG_5))
298
299 #define BPF_MAIN_FUNC (-1)
300
301 #define BPF_DYNPTR_SIZE sizeof(struct bpf_dynptr_kern)
302 #define BPF_DYNPTR_NR_SLOTS (BPF_DYNPTR_SIZE / BPF_REG_SIZE)
303
304 struct bpf_stack_state {
305 struct bpf_reg_state spilled_ptr;
306 u8 slot_type[BPF_REG_SIZE];
307 };
308
309 struct bpf_reference_state {
310 /* Each reference object has a type. Ensure REF_TYPE_PTR is zero to
311 * default to pointer reference on zero initialization of a state.
312 */
313 enum ref_state_type {
314 REF_TYPE_PTR = (1 << 1),
315 REF_TYPE_IRQ = (1 << 2),
316 REF_TYPE_LOCK = (1 << 3),
317 REF_TYPE_RES_LOCK = (1 << 4),
318 REF_TYPE_RES_LOCK_IRQ = (1 << 5),
319 REF_TYPE_LOCK_MASK = REF_TYPE_LOCK | REF_TYPE_RES_LOCK | REF_TYPE_RES_LOCK_IRQ,
320 } type;
321 /* Track each reference created with a unique id, even if the same
322 * instruction creates the reference multiple times (eg, via CALL).
323 */
324 int id;
325 /* Instruction where the allocation of this reference occurred. This
326 * is used purely to inform the user of a reference leak.
327 */
328 int insn_idx;
329 union {
330 /* For REF_TYPE_PTR */
331 int parent_id;
332 /* Use to keep track of the source object of a lock, to ensure
333 * it matches on unlock.
334 */
335 void *ptr;
336 };
337 };
338
339 struct bpf_retval_range {
340 s32 minval;
341 s32 maxval;
342 bool return_32bit;
343 };
344
345 /* state of the program:
346 * type of all registers and stack info
347 */
348 struct bpf_func_state {
349 struct bpf_reg_state regs[MAX_BPF_REG];
350 /* index of call instruction that called into this func */
351 int callsite;
352 /* stack frame number of this function state from pov of
353 * enclosing bpf_verifier_state.
354 * 0 = main function, 1 = first callee.
355 */
356 u32 frameno;
357 /*
358 * Unique diagnostic identity for this function invocation. Frame depth is
359 * reused after returns, while this ID is preserved across state clones.
360 */
361 u32 diag_frame_id;
362 /* subprog number == index within subprog_info
363 * zero == main subprog
364 */
365 u32 subprogno;
366 /* Every bpf_timer_start will increment async_entry_cnt.
367 * It's used to distinguish:
368 * void foo(void) { for(;;); }
369 * void foo(void) { bpf_timer_set_callback(,foo); }
370 */
371 u32 async_entry_cnt;
372 struct bpf_retval_range callback_ret_range;
373 bool in_callback_fn;
374 bool in_async_callback_fn;
375 bool in_exception_callback_fn;
376 bool no_stack_arg_load;
377 /* For callback calling functions that limit number of possible
378 * callback executions (e.g. bpf_loop) keeps track of current
379 * simulated iteration number.
380 * Value in frame N refers to number of times callback with frame
381 * N+1 was simulated, e.g. for the following call:
382 *
383 * bpf_loop(..., fn, ...); | suppose current frame is N
384 * | fn would be simulated in frame N+1
385 * | number of simulations is tracked in frame N
386 */
387 u32 callback_depth;
388 /* Instructions processed in this frame and callees on the current path. */
389 u32 insns_subtotal;
390
391 /* The following fields should be last. See copy_func_state() */
392 /* The state of the stack. Each element of the array describes BPF_REG_SIZE
393 * (i.e. 8) bytes worth of stack memory.
394 * stack[0] represents bytes [*(r10-8)..*(r10-1)]
395 * stack[1] represents bytes [*(r10-16)..*(r10-9)]
396 * ...
397 * stack[allocated_stack/8 - 1] represents [*(r10-allocated_stack)..*(r10-allocated_stack+7)]
398 */
399 struct bpf_stack_state *stack;
400 /* Size of the current stack, in bytes. The stack state is tracked below, in
401 * `stack`. allocated_stack is always a multiple of BPF_REG_SIZE.
402 */
403 int allocated_stack;
404
405 u16 out_stack_arg_cnt; /* Number of outgoing on-stack argument slots */
406 struct bpf_reg_state *stack_arg_regs; /* Outgoing on-stack arguments */
407 };
408
409 #define MAX_CALL_FRAMES 16
410
411 /* instruction history flags, used in bpf_jmp_history_entry.flags field.
412 * Frame number and SPI are stored in dedicated fields of bpf_jmp_history_entry.
413 */
414 enum {
415 INSN_F_STACK_ACCESS = BIT(0),
416
417 INSN_F_DST_REG_STACK = BIT(1), /* dst_reg is PTR_TO_STACK */
418 INSN_F_SRC_REG_STACK = BIT(2), /* src_reg is PTR_TO_STACK */
419
420 INSN_F_STACK_ARG_ACCESS = BIT(3),
421 };
422
423 struct bpf_jmp_history_entry {
424 /* insn idx can't be bigger than 1 million */
425 u32 idx : 20;
426 u32 frame : 4; /* stack access frame number */
427 u32 spi : 6; /* stack slot index (0..63) */
428 u32 : 2;
429 u32 prev_idx : 20;
430 /* special INSN_F_xxx flags */
431 u32 flags : 4;
432 u32 : 8;
433 /*
434 * additional registers that need precision tracking when this
435 * jump is backtracked, vector of five 11-bit records
436 */
437 u64 linked_regs;
438 };
439
440 static_assert(MAX_CALL_FRAMES <= (1 << 4));
441 static_assert(MAX_BPF_STACK / 8 <= (1 << 6));
442
443 /* Maximum number of bpf_reg_state objects that can exist at once */
444 #define MAX_STACK_ARG_SLOTS (MAX_BPF_FUNC_ARGS - MAX_BPF_FUNC_REG_ARGS)
445 #define BPF_ID_MAP_SIZE ((MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE + \
446 MAX_STACK_ARG_SLOTS) * MAX_CALL_FRAMES)
447 struct bpf_verifier_state {
448 /* call stack tracking */
449 struct bpf_func_state *frame[MAX_CALL_FRAMES];
450 struct bpf_verifier_state *parent;
451 /* Acquired reference states */
452 struct bpf_reference_state *refs;
453 /*
454 * 'branches' field is the number of branches left to explore:
455 * 0 - all possible paths from this state reached bpf_exit or
456 * were safely pruned
457 * 1 - at least one path is being explored.
458 * This state hasn't reached bpf_exit
459 * 2 - at least two paths are being explored.
460 * This state is an immediate parent of two children.
461 * One is fallthrough branch with branches==1 and another
462 * state is pushed into stack (to be explored later) also with
463 * branches==1. The parent of this state has branches==1.
464 * The verifier state tree connected via 'parent' pointer looks like:
465 * 1
466 * 1
467 * 2 -> 1 (first 'if' pushed into stack)
468 * 1
469 * 2 -> 1 (second 'if' pushed into stack)
470 * 1
471 * 1
472 * 1 bpf_exit.
473 *
474 * Once do_check() reaches bpf_exit, it calls update_branch_counts()
475 * and the verifier state tree will look:
476 * 1
477 * 1
478 * 2 -> 1 (first 'if' pushed into stack)
479 * 1
480 * 1 -> 1 (second 'if' pushed into stack)
481 * 0
482 * 0
483 * 0 bpf_exit.
484 * After pop_stack() the do_check() will resume at second 'if'.
485 *
486 * If is_state_visited() sees a state with branches > 0 it means
487 * there is a loop. If such state is exactly equal to the current state
488 * it's an infinite loop. Note states_equal() checks for states
489 * equivalency, so two states being 'states_equal' does not mean
490 * infinite loop. The exact comparison is provided by
491 * states_maybe_looping() function. It's a stronger pre-check and
492 * much faster than states_equal().
493 *
494 * This algorithm may not find all possible infinite loops or
495 * loop iteration count may be too high.
496 * In such cases BPF_COMPLEXITY_LIMIT_INSNS limit kicks in.
497 */
498 u32 branches;
499 u32 insn_idx;
500 u32 curframe;
501
502 u32 acquired_refs;
503 u32 active_locks;
504 u32 active_preempt_locks;
505 u32 active_irq_id;
506 u32 active_lock_id;
507 void *active_lock_ptr;
508 u32 active_rcu_locks;
509
510 bool speculative;
511 bool in_sleepable;
512
513 /* first and last insn idx of this verifier state */
514 u32 first_insn_idx;
515 u32 last_insn_idx;
516 /* if this state is a backedge state then equal_state
517 * records cached state to which this state is equal.
518 */
519 struct bpf_verifier_state *equal_state;
520 /* jmp history recorded from first to last.
521 * backtracking is using it to go from last to first.
522 * For most states jmp_history_cnt is [0-3].
523 * For loops can go up to ~40.
524 */
525 struct bpf_jmp_history_entry *jmp_history;
526 u32 jmp_history_cnt;
527 u32 dfs_depth;
528 u32 callback_unroll_depth;
529 u32 may_goto_depth;
530 };
531
532 static inline struct bpf_reg_state *
bpf_get_spilled_reg(int slot,struct bpf_func_state * frame,u32 mask)533 bpf_get_spilled_reg(int slot, struct bpf_func_state *frame, u32 mask)
534 {
535 if (slot < frame->allocated_stack / BPF_REG_SIZE &&
536 (1 << frame->stack[slot].slot_type[BPF_REG_SIZE - 1]) & mask)
537 return &frame->stack[slot].spilled_ptr;
538 return NULL;
539 }
540
541 static inline struct bpf_reg_state *
bpf_get_spilled_stack_arg(int slot,struct bpf_func_state * frame)542 bpf_get_spilled_stack_arg(int slot, struct bpf_func_state *frame)
543 {
544 if (slot < frame->out_stack_arg_cnt &&
545 frame->stack_arg_regs[slot].type != NOT_INIT)
546 return &frame->stack_arg_regs[slot];
547 return NULL;
548 }
549
550 /* Iterate over 'frame', setting 'reg' to either NULL or a spilled register. */
551 #define bpf_for_each_spilled_reg(iter, frame, reg, mask) \
552 for (iter = 0, reg = bpf_get_spilled_reg(iter, frame, mask); \
553 iter < frame->allocated_stack / BPF_REG_SIZE; \
554 iter++, reg = bpf_get_spilled_reg(iter, frame, mask))
555
556 /* Iterate over 'frame', setting 'reg' to either NULL or a spilled stack arg. */
557 #define bpf_for_each_spilled_stack_arg(iter, frame, reg) \
558 for (iter = 0, reg = bpf_get_spilled_stack_arg(iter, frame); \
559 iter < frame->out_stack_arg_cnt; \
560 iter++, reg = bpf_get_spilled_stack_arg(iter, frame))
561
562 #define bpf_for_each_reg_in_vstate_mask(__vst, __state, __reg, __stack, __mask, __expr) \
563 ({ \
564 struct bpf_verifier_state *___vstate = __vst; \
565 int ___i, ___j; \
566 for (___i = 0; ___i <= ___vstate->curframe; ___i++) { \
567 struct bpf_reg_state *___regs; \
568 __state = ___vstate->frame[___i]; \
569 ___regs = __state->regs; \
570 __stack = NULL; \
571 for (___j = 0; ___j < MAX_BPF_REG; ___j++) { \
572 __reg = &___regs[___j]; \
573 (void)(__expr); \
574 } \
575 bpf_for_each_spilled_reg(___j, __state, __reg, __mask) { \
576 if (!__reg) \
577 continue; \
578 __stack = &__state->stack[___j]; \
579 (void)(__expr); \
580 } \
581 __stack = NULL; \
582 bpf_for_each_spilled_stack_arg(___j, __state, __reg) { \
583 if (!__reg) \
584 continue; \
585 (void)(__expr); \
586 } \
587 } \
588 (void)__stack; \
589 })
590
591 /* Invoke __expr over regsiters in __vst, setting __state and __reg */
592 #define bpf_for_each_reg_in_vstate(__vst, __state, __reg, __expr) \
593 ({ \
594 struct bpf_stack_state * ___stack; \
595 (void)___stack; \
596 bpf_for_each_reg_in_vstate_mask(__vst, __state, __reg, ___stack,\
597 1 << STACK_SPILL, __expr); \
598 })
599
600 /* linked list of verifier states used to prune search */
601 struct bpf_verifier_state_list {
602 struct bpf_verifier_state state;
603 struct list_head node;
604 u32 miss_cnt;
605 u32 hit_cnt:31;
606 u32 in_free_list:1;
607 };
608
609 struct bpf_loop_inline_state {
610 unsigned int initialized:1; /* set to true upon first entry */
611 unsigned int fit_for_inline:1; /* true if callback function is the same
612 * at each call and flags are always zero
613 */
614 u32 callback_subprogno; /* valid when fit_for_inline is true */
615 };
616
617 /* pointer and state for maps */
618 struct bpf_map_ptr_state {
619 struct bpf_map *map_ptr;
620 bool poison;
621 bool unpriv;
622 };
623
624 /* Possible states for alu_state member. */
625 #define BPF_ALU_SANITIZE_SRC (1U << 0)
626 #define BPF_ALU_SANITIZE_DST (1U << 1)
627 #define BPF_ALU_NEG_VALUE (1U << 2)
628 #define BPF_ALU_NON_POINTER (1U << 3)
629 #define BPF_ALU_IMMEDIATE (1U << 4)
630 #define BPF_ALU_SANITIZE (BPF_ALU_SANITIZE_SRC | \
631 BPF_ALU_SANITIZE_DST)
632
633 /*
634 * An array of BPF instructions.
635 * Primary usage: return value of bpf_insn_successors.
636 */
637 struct bpf_iarray {
638 int cnt;
639 u32 items[];
640 };
641
642 struct bpf_insn_aux_data {
643 union {
644 enum bpf_reg_type ptr_type; /* pointer type for load/store insns */
645 struct bpf_map_ptr_state map_ptr_state;
646 s32 call_imm; /* saved imm field of call insn */
647 u32 alu_limit; /* limit for add/sub register with pointer */
648 struct {
649 u32 map_index; /* index into used_maps[] */
650 u32 map_off; /* offset from value base address */
651 };
652 struct {
653 enum bpf_reg_type reg_type; /* type of pseudo_btf_id */
654 union {
655 struct {
656 struct btf *btf;
657 u32 btf_id; /* btf_id for struct typed var */
658 };
659 u32 mem_size; /* mem_size for non-struct typed var */
660 };
661 } btf_var;
662 /* if instruction is a call to bpf_loop this field tracks
663 * the state of the relevant registers to make decision about inlining
664 */
665 struct bpf_loop_inline_state loop_inline_state;
666 };
667 union {
668 /* remember the size of type passed to bpf_obj_new to rewrite R1 */
669 u64 obj_new_size;
670 /* remember the offset of node field within type to rewrite */
671 u64 insert_off;
672 };
673 struct bpf_iarray *jt; /* jump table for gotox or bpf_tailcall call instruction */
674 struct btf_struct_meta *kptr_struct_meta;
675 u64 map_key_state; /* constant (32 bit) key tracking for maps */
676 int ctx_field_size; /* the ctx field size for load insn, maybe 0 */
677 u32 seen; /* this insn was processed by the verifier at env->pass_cnt */
678 bool nospec; /* do not execute this instruction speculatively */
679 bool nospec_result; /* result is unsafe under speculation, nospec must follow */
680 bool zext_dst; /* this insn zero extends dst reg */
681 bool needs_zext; /* alu op needs to clear upper bits */
682 bool non_sleepable; /* helper/kfunc may be called from non-sleepable context */
683 bool is_iter_next; /* bpf_iter_<type>_next() kfunc call */
684 bool call_with_percpu_alloc_ptr; /* {this,per}_cpu_ptr() with prog percpu alloc */
685 u8 alu_state; /* used in combination with alu_limit */
686 /* true if STX or LDX instruction is a part of a spill/fill
687 * pattern for a bpf_fastcall call.
688 */
689 u8 fastcall_pattern:1;
690 /* for CALL instructions, a number of spill/fill pairs in the
691 * bpf_fastcall pattern.
692 */
693 u8 fastcall_spills_num:3;
694 u8 arg_prog:4;
695
696 /* below fields are initialized once */
697 unsigned int orig_idx; /* original instruction index */
698 u32 jmp_point:1;
699 u32 prune_point:1;
700 /* ensure we check state equivalence and save state checkpoint and
701 * this instruction, regardless of any heuristics
702 */
703 u32 force_checkpoint:1;
704 /* true if instruction is a call to a helper function that
705 * accepts callback function as a parameter.
706 */
707 u32 calls_callback:1;
708 u32 indirect_target:1; /* if it is an indirect jump target */
709 /* true if some jump or call instruction targets this instruction */
710 u32 jump_target:1;
711 /*
712 * CFG strongly connected component this instruction belongs to,
713 * zero if it is a singleton SCC.
714 */
715 u32 scc;
716 /* registers alive before this instruction. */
717 u16 live_regs_before;
718 /*
719 * Bitmask of R0-R9 that hold known values at this instruction.
720 * const_reg_mask: scalar constants that fit in 32 bits.
721 * const_reg_map_mask: map pointers, val is map_index into used_maps[].
722 * const_reg_subprog_mask: subprog pointers, val is subprog number.
723 * const_reg_vals[i] holds the 32-bit value for register i.
724 * Populated by compute_const_regs() pre-pass.
725 */
726 u16 const_reg_mask;
727 u16 const_reg_map_mask;
728 u16 const_reg_subprog_mask;
729 u32 const_reg_vals[10];
730 };
731
732 #define MAX_USED_MAPS 64 /* max number of maps accessed by one eBPF program */
733 #define MAX_USED_BTFS 64 /* max number of BTFs accessed by one BPF program */
734
735 #define BPF_VERIFIER_TMP_LOG_SIZE 1024
736
737 struct bpf_verifier_log {
738 /* Logical start and end positions of a "log window" of the verifier log.
739 * start_pos == 0 means we haven't truncated anything.
740 * Once truncation starts to happen, start_pos + len_total == end_pos,
741 * except during log reset situations, in which (end_pos - start_pos)
742 * might get smaller than len_total (see bpf_vlog_reset()).
743 * Generally, (end_pos - start_pos) gives number of useful data in
744 * user log buffer.
745 */
746 u64 start_pos;
747 u64 end_pos;
748 char __user *ubuf;
749 u32 level;
750 u32 len_total;
751 u32 len_max;
752 char kbuf[BPF_VERIFIER_TMP_LOG_SIZE];
753 };
754
755 #define BPF_LOG_LEVEL1 1
756 #define BPF_LOG_LEVEL2 2
757 #define BPF_LOG_STATS 4
758 #define BPF_LOG_FIXED 8
759 #define BPF_LOG_LEVEL (BPF_LOG_LEVEL1 | BPF_LOG_LEVEL2)
760 #define BPF_LOG_MASK (BPF_LOG_LEVEL | BPF_LOG_STATS | BPF_LOG_FIXED)
761 #define BPF_LOG_KERNEL (BPF_LOG_MASK + 1) /* kernel internal flag */
762 #define BPF_LOG_MIN_ALIGNMENT 8U
763 #define BPF_LOG_ALIGNMENT 40U
764
bpf_verifier_log_needed(const struct bpf_verifier_log * log)765 static inline bool bpf_verifier_log_needed(const struct bpf_verifier_log *log)
766 {
767 return log && log->level;
768 }
769
770 struct bpf_log_attr {
771 char __user *ubuf;
772 u32 size;
773 u32 level;
774 u32 offsetof_true_size;
775 bpfptr_t uattr;
776 };
777
778 int bpf_log_attr_init(struct bpf_log_attr *log, u64 log_buf, u32 log_size, u32 log_level,
779 u32 offsetof_log_true_size, bpfptr_t uattr, struct bpf_common_attr *common,
780 bpfptr_t uattr_common, u32 size_common);
781 struct bpf_verifier_log *bpf_log_attr_create_vlog(struct bpf_log_attr *attr_log,
782 struct bpf_common_attr *common, bpfptr_t uattr,
783 u32 size);
784 int bpf_log_attr_finalize(struct bpf_log_attr *attr, struct bpf_verifier_log *log);
785
786 #define BPF_MAX_SUBPROGS 256
787
788 struct bpf_subprog_arg_info {
789 enum bpf_arg_type arg_type;
790 union {
791 u32 mem_size;
792 u32 btf_id;
793 };
794 };
795
796 enum priv_stack_mode {
797 PRIV_STACK_UNKNOWN,
798 NO_PRIV_STACK,
799 PRIV_STACK_ADAPTIVE,
800 };
801
802 struct bpf_subprog_info {
803 const char *name; /* name extracted from BTF */
804 u32 start; /* insn idx of function entry point */
805 u32 linfo_idx; /* The idx to the main_prog->aux->linfo */
806 u32 postorder_start; /* The idx to the env->cfg.insn_postorder */
807 u32 exit_idx; /* Index of one of the BPF_EXIT instructions in this subprogram */
808 u16 stack_depth; /* max. stack depth used by this function */
809 u16 stack_extra;
810 u32 insns_total;
811 u32 insns_self;
812 /* offsets in range [stack_depth .. fastcall_stack_off)
813 * are used for bpf_fastcall spills and fills.
814 */
815 s16 fastcall_stack_off;
816 bool has_tail_call: 1;
817 bool might_throw: 1;
818 bool tail_call_reachable: 1;
819 bool has_ld_abs: 1;
820 bool is_cb: 1;
821 bool is_async_cb: 1;
822 bool is_exception_cb: 1;
823 bool args_cached: 1;
824 /* true if bpf_fastcall stack region is used by functions that can't be inlined */
825 bool keep_fastcall_stack: 1;
826 bool changes_pkt_data: 1;
827 bool might_sleep: 1;
828 u8 arg_cnt:4;
829
830 enum priv_stack_mode priv_stack_mode;
831 struct bpf_subprog_arg_info args[MAX_BPF_FUNC_ARGS];
832 u16 stack_arg_cnt; /* incoming + max outgoing */
833 u16 max_out_stack_arg_cnt;
834 };
835
bpf_in_stack_arg_cnt(const struct bpf_subprog_info * sub)836 static inline u16 bpf_in_stack_arg_cnt(const struct bpf_subprog_info *sub)
837 {
838 if (sub->arg_cnt > MAX_BPF_FUNC_REG_ARGS)
839 return sub->arg_cnt - MAX_BPF_FUNC_REG_ARGS;
840 return 0;
841 }
842
843 struct bpf_diag;
844 struct bpf_verifier_env;
845
846 struct backtrack_state {
847 struct bpf_verifier_env *env;
848 u32 frame;
849 u32 reg_masks[MAX_CALL_FRAMES];
850 u64 stack_masks[MAX_CALL_FRAMES];
851 u8 stack_arg_masks[MAX_CALL_FRAMES];
852 };
853
854 struct bpf_id_pair {
855 u32 old;
856 u32 cur;
857 };
858
859 struct bpf_idmap {
860 u32 tmp_id_gen;
861 u32 cnt;
862 struct bpf_id_pair map[BPF_ID_MAP_SIZE];
863 };
864
865 struct bpf_idset {
866 u32 num_ids;
867 struct {
868 u32 id;
869 u32 cnt;
870 } entries[BPF_ID_MAP_SIZE];
871 };
872
873 /* see verifier.c:compute_scc_callchain() */
874 struct bpf_scc_callchain {
875 /* call sites from bpf_verifier_state->frame[*]->callsite leading to this SCC */
876 u32 callsites[MAX_CALL_FRAMES - 1];
877 /* last frame in a chain is identified by SCC id */
878 u32 scc;
879 };
880
881 /* verifier state waiting for propagate_backedges() */
882 struct bpf_scc_backedge {
883 struct bpf_scc_backedge *next;
884 struct bpf_verifier_state state;
885 };
886
887 struct bpf_scc_visit {
888 struct bpf_scc_callchain callchain;
889 /* first state in current verification path that entered SCC
890 * identified by the callchain
891 */
892 struct bpf_verifier_state *entry_state;
893 struct bpf_scc_backedge *backedges; /* list of backedges */
894 u32 num_backedges;
895 };
896
897 /* An array of bpf_scc_visit structs sharing tht same bpf_scc_callchain->scc
898 * but having different bpf_scc_callchain->callsites.
899 */
900 struct bpf_scc_info {
901 u32 num_visits;
902 struct bpf_scc_visit visits[];
903 };
904
905 struct bpf_liveness;
906
907 struct bpf_fd_array {
908 union {
909 struct bpf_map *map;
910 struct btf *btf;
911 unsigned long val;
912 };
913 };
914
915 /* single container for all structs
916 * one verifier_env per bpf_check() call
917 */
918 struct bpf_verifier_env {
919 u32 insn_idx;
920 u32 prev_insn_idx;
921 struct bpf_prog *prog; /* eBPF program being verified */
922 const struct bpf_verifier_ops *ops;
923 struct module *attach_btf_mod; /* The owner module of prog->aux->attach_btf */
924 struct bpf_verifier_stack_elem *head; /* stack of verifier states to be processed */
925 int stack_size; /* number of states to be processed */
926 bool strict_alignment; /* perform strict pointer alignment checks */
927 bool test_state_freq; /* test verifier with different pruning frequency */
928 bool test_reg_invariants; /* fail verification on register invariants violations */
929 struct bpf_verifier_state *cur_state; /* current verifier state */
930 /* Search pruning optimization, array of list_heads for
931 * lists of struct bpf_verifier_state_list.
932 */
933 struct list_head *explored_states;
934 struct list_head free_list; /* list of struct bpf_verifier_state_list */
935 struct bpf_map *used_maps[MAX_USED_MAPS]; /* array of map's used by eBPF program */
936 struct btf_mod_pair used_btfs[MAX_USED_BTFS]; /* array of BTF's used by BPF program */
937 struct bpf_map *insn_array_maps[MAX_USED_MAPS]; /* array of INSN_ARRAY map's to be relocated */
938 u32 used_map_cnt; /* number of used maps */
939 u32 used_btf_cnt; /* number of used BTF objects */
940 u32 insn_array_map_cnt; /* number of used maps of type BPF_MAP_TYPE_INSN_ARRAY */
941 u32 id_gen; /* used to generate unique reg IDs */
942 u32 hidden_subprog_cnt; /* number of hidden subprogs */
943 int exception_callback_subprog;
944 bool explore_alu_limits;
945 bool allow_ptr_leaks;
946 /* Allow access to uninitialized stack memory. Writes with fixed offset are
947 * always allowed, so this refers to reads (with fixed or variable offset),
948 * to writes with variable offset and to indirect (helper) accesses.
949 */
950 bool allow_uninit_stack;
951 bool bpf_capable;
952 bool bypass_spec_v1;
953 bool bypass_spec_v4;
954 bool seen_direct_write;
955 bool seen_exception;
956 bool signature;
957 u32 insn_aux_data_len;
958 struct bpf_insn_aux_data *insn_aux_data; /* array of per-insn state */
959 const struct bpf_line_info *prev_linfo;
960 struct bpf_verifier_log log;
961 struct bpf_diag *diag;
962 struct bpf_subprog_info subprog_info[BPF_MAX_SUBPROGS + 2]; /* max + 2 for the fake and exception subprogs */
963 /* subprog indices sorted in topological order: leaves first, callers last */
964 int subprog_topo_order[BPF_MAX_SUBPROGS + 2];
965 union {
966 struct bpf_idmap idmap_scratch;
967 struct bpf_idset idset_scratch;
968 };
969 struct {
970 int *insn_state;
971 int *insn_stack;
972 /*
973 * vector of instruction indexes sorted in post-order, grouped by subprogram,
974 * see bpf_subprog_info->postorder_start.
975 */
976 int *insn_postorder;
977 int cur_stack;
978 /* current position in the insn_postorder vector */
979 int cur_postorder;
980 } cfg;
981 struct backtrack_state bt;
982 struct bpf_jmp_history_entry *cur_hist_ent;
983 /* Per-callsite copy of parent's converged at_stack_in for cross-frame fills. */
984 struct arg_track **callsite_at_stack;
985 u32 pass_cnt; /* number of times do_check() was called */
986 u32 subprog_cnt;
987 /* number of instructions analyzed by the verifier */
988 u32 prev_insn_processed, insn_processed;
989 /* number of jmps, calls, exits analyzed so far */
990 u32 prev_jmps_processed, jmps_processed;
991 /* maximum combined stack depth */
992 u32 max_stack_depth;
993 /* total verification time */
994 u64 verification_time;
995 /* maximum number of verifier states kept in 'branching' instructions */
996 u32 max_states_per_insn;
997 /* total number of allocated verifier states */
998 u32 total_states;
999 /* some states are freed during program analysis.
1000 * this is peak number of states. this number dominates kernel
1001 * memory consumption during verification
1002 */
1003 u32 peak_states;
1004 /* longest register parentage chain walked for liveness marking */
1005 u32 longest_mark_read_walk;
1006 u32 free_list_size;
1007 u32 explored_states_size;
1008 u32 num_backedges;
1009 /*
1010 * The program's fd_array comes in two shapes, told apart by whether
1011 * the caller passed fd_array_cnt. They are mutually exclusive:
1012 * - continuous (fd_array_cnt given): ->fd_array holds every entry
1013 * resolved to its object up front, indexed by fd_array position,
1014 * with ->fd_array_cnt slots; ->fd_array_raw is unused.
1015 * - sparse (no fd_array_cnt): ->fd_array is NULL, and entries are
1016 * read from ->fd_array_raw (the caller's fd_array) and resolved
1017 * on the spot at each reference.
1018 */
1019 struct bpf_fd_array *fd_array;
1020 u32 fd_array_cnt;
1021 bpfptr_t fd_array_raw;
1022
1023 /* bit mask to keep track of whether a register has been accessed
1024 * since the last time the function state was printed
1025 */
1026 u32 scratched_regs;
1027 /* Same as scratched_regs but for stack slots */
1028 u64 scratched_stack_slots;
1029 u64 prev_log_pos, prev_insn_print_pos;
1030 /* buffer used to temporary hold constants as scalar registers */
1031 struct bpf_reg_state fake_reg[1];
1032 /* buffers used to save updated reg states while simulating branches */
1033 struct bpf_reg_state true_reg1, true_reg2, false_reg1, false_reg2;
1034 /* buffer used to generate temporary string representations,
1035 * e.g., in reg_type_str() to generate reg_type string
1036 */
1037 char tmp_str_buf[TMP_STR_BUF_LEN];
1038 char tmp_arg_name[32];
1039 struct bpf_insn insn_buf[INSN_BUF_SIZE];
1040 struct bpf_insn epilogue_buf[INSN_BUF_SIZE];
1041 struct bpf_scc_callchain callchain_buf;
1042 struct bpf_liveness *liveness;
1043 /* array of pointers to bpf_scc_info indexed by SCC id */
1044 struct bpf_scc_info **scc_info;
1045 u32 scc_cnt;
1046 struct bpf_iarray *succ;
1047 struct bpf_iarray *gotox_tmp_buf;
1048 };
1049
subprog_aux(struct bpf_verifier_env * env,int subprog)1050 static inline struct bpf_func_info_aux *subprog_aux(struct bpf_verifier_env *env, int subprog)
1051 {
1052 return &env->prog->aux->func_info_aux[subprog];
1053 }
1054
subprog_info(struct bpf_verifier_env * env,int subprog)1055 static inline struct bpf_subprog_info *subprog_info(struct bpf_verifier_env *env, int subprog)
1056 {
1057 return &env->subprog_info[subprog];
1058 }
1059
1060 struct bpf_call_summary {
1061 u8 num_params;
1062 bool is_void;
1063 bool fastcall;
1064 };
1065
bpf_helper_call(const struct bpf_insn * insn)1066 static inline bool bpf_helper_call(const struct bpf_insn *insn)
1067 {
1068 return insn->code == (BPF_JMP | BPF_CALL) &&
1069 insn->src_reg == 0;
1070 }
1071
bpf_pseudo_call(const struct bpf_insn * insn)1072 static inline bool bpf_pseudo_call(const struct bpf_insn *insn)
1073 {
1074 return insn->code == (BPF_JMP | BPF_CALL) &&
1075 insn->src_reg == BPF_PSEUDO_CALL;
1076 }
1077
bpf_pseudo_kfunc_call(const struct bpf_insn * insn)1078 static inline bool bpf_pseudo_kfunc_call(const struct bpf_insn *insn)
1079 {
1080 return insn->code == (BPF_JMP | BPF_CALL) &&
1081 insn->src_reg == BPF_PSEUDO_KFUNC_CALL;
1082 }
1083
1084 __printf(2, 0) void bpf_verifier_vlog(struct bpf_verifier_log *log,
1085 const char *fmt, va_list args);
1086 __printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env,
1087 const char *fmt, ...);
1088 __printf(2, 3) void bpf_log(struct bpf_verifier_log *log,
1089 const char *fmt, ...);
1090 int bpf_vlog_init(struct bpf_verifier_log *log, u32 log_level,
1091 char __user *log_buf, u32 log_size);
1092 void bpf_vlog_reset(struct bpf_verifier_log *log, u64 new_pos);
1093 int bpf_vlog_finalize(struct bpf_verifier_log *log, u32 *log_size_actual);
1094
1095 __printf(3, 4) void verbose_linfo(struct bpf_verifier_env *env,
1096 u32 insn_off,
1097 const char *prefix_fmt, ...);
1098
1099 #define verifier_bug_if(cond, env, fmt, args...) \
1100 ({ \
1101 bool __cond = (cond); \
1102 if (unlikely(__cond)) \
1103 verifier_bug(env, fmt " (" #cond ")", ##args); \
1104 (__cond); \
1105 })
1106 #define verifier_bug(env, fmt, args...) \
1107 ({ \
1108 BPF_WARN_ONCE(1, "verifier bug: " fmt "\n", ##args); \
1109 bpf_log(&env->log, "verifier bug: " fmt "\n", ##args); \
1110 })
1111
mark_prune_point(struct bpf_verifier_env * env,int idx)1112 static inline void mark_prune_point(struct bpf_verifier_env *env, int idx)
1113 {
1114 env->insn_aux_data[idx].prune_point = true;
1115 }
1116
bpf_is_prune_point(struct bpf_verifier_env * env,int insn_idx)1117 static inline bool bpf_is_prune_point(struct bpf_verifier_env *env, int insn_idx)
1118 {
1119 return env->insn_aux_data[insn_idx].prune_point;
1120 }
1121
mark_force_checkpoint(struct bpf_verifier_env * env,int idx)1122 static inline void mark_force_checkpoint(struct bpf_verifier_env *env, int idx)
1123 {
1124 env->insn_aux_data[idx].force_checkpoint = true;
1125 }
1126
bpf_is_force_checkpoint(struct bpf_verifier_env * env,int insn_idx)1127 static inline bool bpf_is_force_checkpoint(struct bpf_verifier_env *env, int insn_idx)
1128 {
1129 return env->insn_aux_data[insn_idx].force_checkpoint;
1130 }
1131
mark_calls_callback(struct bpf_verifier_env * env,int idx)1132 static inline void mark_calls_callback(struct bpf_verifier_env *env, int idx)
1133 {
1134 env->insn_aux_data[idx].calls_callback = true;
1135 }
1136
bpf_calls_callback(struct bpf_verifier_env * env,int insn_idx)1137 static inline bool bpf_calls_callback(struct bpf_verifier_env *env, int insn_idx)
1138 {
1139 return env->insn_aux_data[insn_idx].calls_callback;
1140 }
1141
mark_jmp_point(struct bpf_verifier_env * env,int idx)1142 static inline void mark_jmp_point(struct bpf_verifier_env *env, int idx)
1143 {
1144 env->insn_aux_data[idx].jmp_point = true;
1145 }
1146
mark_jump_target(struct bpf_verifier_env * env,int idx)1147 static inline void mark_jump_target(struct bpf_verifier_env *env, int idx)
1148 {
1149 env->insn_aux_data[idx].jump_target = true;
1150 }
1151
bpf_is_jump_target(struct bpf_verifier_env * env,int insn_idx)1152 static inline bool bpf_is_jump_target(struct bpf_verifier_env *env, int insn_idx)
1153 {
1154 return env->insn_aux_data[insn_idx].jump_target;
1155 }
1156
cur_func(struct bpf_verifier_env * env)1157 static inline struct bpf_func_state *cur_func(struct bpf_verifier_env *env)
1158 {
1159 struct bpf_verifier_state *cur = env->cur_state;
1160
1161 return cur->frame[cur->curframe];
1162 }
1163
cur_regs(struct bpf_verifier_env * env)1164 static inline struct bpf_reg_state *cur_regs(struct bpf_verifier_env *env)
1165 {
1166 return cur_func(env)->regs;
1167 }
1168
1169 int bpf_prog_offload_verifier_prep(struct bpf_prog *prog);
1170 int bpf_prog_offload_verify_insn(struct bpf_verifier_env *env,
1171 int insn_idx, int prev_insn_idx);
1172 int bpf_prog_offload_finalize(struct bpf_verifier_env *env);
1173 void
1174 bpf_prog_offload_replace_insn(struct bpf_verifier_env *env, u32 off,
1175 struct bpf_insn *insn);
1176 void
1177 bpf_prog_offload_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt);
1178
1179 /* 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)1180 static inline u64 bpf_trampoline_compute_key(const struct bpf_prog *tgt_prog,
1181 struct btf *btf, u32 btf_id)
1182 {
1183 if (tgt_prog)
1184 return ((u64)tgt_prog->aux->id << 32) | btf_id;
1185 else
1186 return ((u64)btf_obj_id(btf) << 32) | 0x80000000 | btf_id;
1187 }
1188
1189 /* unpack the IDs from the key as constructed above */
bpf_trampoline_unpack_key(u64 key,u32 * obj_id,u32 * btf_id)1190 static inline void bpf_trampoline_unpack_key(u64 key, u32 *obj_id, u32 *btf_id)
1191 {
1192 if (obj_id)
1193 *obj_id = key >> 32;
1194 if (btf_id)
1195 *btf_id = key & 0x7FFFFFFF;
1196 }
1197
1198 int bpf_prepare_btf_info(struct bpf_verifier_env *env,
1199 const union bpf_attr *attr, bpfptr_t uattr);
1200 int bpf_check_btf_info(struct bpf_verifier_env *env,
1201 const union bpf_attr *attr, bpfptr_t uattr);
1202
1203 int bpf_check_attach_target(struct bpf_verifier_log *log,
1204 const struct bpf_prog *prog,
1205 const struct bpf_prog *tgt_prog,
1206 u32 btf_id,
1207 struct bpf_attach_target_info *tgt_info);
1208 void bpf_free_kfunc_btf_tab(struct bpf_kfunc_btf_tab *tab);
1209
1210 int mark_chain_precision(struct bpf_verifier_env *env, int regno);
1211
1212 int bpf_is_state_visited(struct bpf_verifier_env *env, int insn_idx);
1213 int bpf_update_branch_counts(struct bpf_verifier_env *env, struct bpf_verifier_state *st);
1214
1215 void bpf_clear_jmp_history(struct bpf_verifier_state *state);
1216 int bpf_copy_verifier_state(struct bpf_verifier_state *dst_state,
1217 const struct bpf_verifier_state *src);
1218 struct list_head *bpf_explored_state(struct bpf_verifier_env *env, int idx);
1219 void bpf_free_verifier_state(struct bpf_verifier_state *state, bool free_self);
1220 void bpf_free_backedges(struct bpf_scc_visit *visit);
1221 int bpf_push_jmp_history(struct bpf_verifier_env *env, struct bpf_verifier_state *cur,
1222 int insn_flags, int spi, int frame, u64 linked_regs);
1223 void bpf_bt_sync_linked_regs(struct backtrack_state *bt, struct bpf_jmp_history_entry *hist);
1224 void bpf_mark_reg_not_init(const struct bpf_verifier_env *env,
1225 struct bpf_reg_state *reg);
1226 void bpf_mark_reg_unknown_imprecise(struct bpf_reg_state *reg);
1227 void bpf_mark_all_scalars_precise(struct bpf_verifier_env *env,
1228 struct bpf_verifier_state *st);
1229 void bpf_clear_singular_ids(struct bpf_verifier_env *env, struct bpf_verifier_state *st);
1230 int bpf_mark_chain_precision(struct bpf_verifier_env *env,
1231 struct bpf_verifier_state *starting_state,
1232 int regno, bool *changed);
1233
bpf_get_spi(s32 off)1234 static inline int bpf_get_spi(s32 off)
1235 {
1236 return (-off - 1) / BPF_REG_SIZE;
1237 }
1238
bpf_func(struct bpf_verifier_env * env,const struct bpf_reg_state * reg)1239 static inline struct bpf_func_state *bpf_func(struct bpf_verifier_env *env,
1240 const struct bpf_reg_state *reg)
1241 {
1242 struct bpf_verifier_state *cur = env->cur_state;
1243
1244 return cur->frame[reg->frameno];
1245 }
1246
1247 /* Return IP for a given frame in a call stack */
bpf_frame_insn_idx(struct bpf_verifier_state * st,u32 frame)1248 static inline u32 bpf_frame_insn_idx(struct bpf_verifier_state *st, u32 frame)
1249 {
1250 return frame == st->curframe
1251 ? st->insn_idx
1252 : st->frame[frame + 1]->callsite;
1253 }
1254
bpf_is_jmp_point(struct bpf_verifier_env * env,int insn_idx)1255 static inline bool bpf_is_jmp_point(struct bpf_verifier_env *env, int insn_idx)
1256 {
1257 return env->insn_aux_data[insn_idx].jmp_point;
1258 }
1259
bpf_is_spilled_reg(const struct bpf_stack_state * stack)1260 static inline bool bpf_is_spilled_reg(const struct bpf_stack_state *stack)
1261 {
1262 return stack->slot_type[BPF_REG_SIZE - 1] == STACK_SPILL;
1263 }
1264
bpf_is_spilled_scalar_reg(const struct bpf_stack_state * stack)1265 static inline bool bpf_is_spilled_scalar_reg(const struct bpf_stack_state *stack)
1266 {
1267 return bpf_is_spilled_reg(stack) && stack->spilled_ptr.type == SCALAR_VALUE;
1268 }
1269
bpf_register_is_null(struct bpf_reg_state * reg)1270 static inline bool bpf_register_is_null(struct bpf_reg_state *reg)
1271 {
1272 return reg->type == SCALAR_VALUE && tnum_equals_const(reg->var_off, 0);
1273 }
1274
bpf_bt_set_frame_reg(struct backtrack_state * bt,u32 frame,u32 reg)1275 static inline void bpf_bt_set_frame_reg(struct backtrack_state *bt, u32 frame, u32 reg)
1276 {
1277 bt->reg_masks[frame] |= 1 << reg;
1278 }
1279
bpf_bt_set_frame_slot(struct backtrack_state * bt,u32 frame,u32 slot)1280 static inline void bpf_bt_set_frame_slot(struct backtrack_state *bt, u32 frame, u32 slot)
1281 {
1282 bt->stack_masks[frame] |= 1ull << slot;
1283 }
1284
bpf_bt_set_frame_slot_mask(struct backtrack_state * bt,u32 frame,u64 mask)1285 static inline void bpf_bt_set_frame_slot_mask(struct backtrack_state *bt, u32 frame, u64 mask)
1286 {
1287 bt->stack_masks[frame] |= mask;
1288 }
1289
bt_set_frame_stack_arg_slot(struct backtrack_state * bt,u32 frame,u32 slot)1290 static inline void bt_set_frame_stack_arg_slot(struct backtrack_state *bt, u32 frame, u32 slot)
1291 {
1292 bt->stack_arg_masks[frame] |= 1 << slot;
1293 }
1294
bt_is_frame_reg_set(struct backtrack_state * bt,u32 frame,u32 reg)1295 static inline bool bt_is_frame_reg_set(struct backtrack_state *bt, u32 frame, u32 reg)
1296 {
1297 return bt->reg_masks[frame] & (1 << reg);
1298 }
1299
bt_is_frame_slot_set(struct backtrack_state * bt,u32 frame,u32 slot)1300 static inline bool bt_is_frame_slot_set(struct backtrack_state *bt, u32 frame, u32 slot)
1301 {
1302 return bt->stack_masks[frame] & (1ull << slot);
1303 }
1304
1305 bool bpf_map_is_rdonly(const struct bpf_map *map);
1306 int bpf_map_direct_read(struct bpf_map *map, int off, int size, u64 *val,
1307 bool is_ldsx);
1308
1309 #define BPF_BASE_TYPE_MASK GENMASK(BPF_BASE_TYPE_BITS - 1, 0)
1310
1311 /* extract base type from bpf_{arg, return, reg}_type. */
base_type(u32 type)1312 static inline u32 base_type(u32 type)
1313 {
1314 return type & BPF_BASE_TYPE_MASK;
1315 }
1316
1317 /* extract flags from an extended type. See bpf_type_flag in bpf.h. */
type_flag(u32 type)1318 static inline u32 type_flag(u32 type)
1319 {
1320 return type & ~BPF_BASE_TYPE_MASK;
1321 }
1322
bpf_is_ptr_to_mem_or_btf_id(enum bpf_reg_type type)1323 static inline bool bpf_is_ptr_to_mem_or_btf_id(enum bpf_reg_type type)
1324 {
1325 switch (base_type(type)) {
1326 case PTR_TO_MEM:
1327 case PTR_TO_BTF_ID:
1328 return true;
1329 default:
1330 return false;
1331 }
1332 }
1333
bpf_may_fault_on_deref(enum bpf_reg_type type)1334 static inline bool bpf_may_fault_on_deref(enum bpf_reg_type type)
1335 {
1336 /*
1337 * The pointer types which must not be dereferenced without fault
1338 * protection, that is, the ones bpf_convert_ctx_accesses() has to
1339 * turn a BPF_LDX into a BPF_PROBE_MEM one for.
1340 */
1341 return type == PTR_TO_BTF_ID || (type_flag(type) & PTR_UNTRUSTED);
1342 }
1343
bpf_prog_has_arena_ctx_arg(const struct bpf_prog * prog)1344 static inline bool bpf_prog_has_arena_ctx_arg(const struct bpf_prog *prog)
1345 {
1346 int i;
1347
1348 for (i = 0; i < prog->aux->ctx_arg_info_size; i++)
1349 if (base_type(prog->aux->ctx_arg_info[i].reg_type) == PTR_TO_ARENA)
1350 return true;
1351 return false;
1352 }
1353
resolve_prog_type(const struct bpf_prog * prog)1354 static inline enum bpf_prog_type resolve_prog_type(const struct bpf_prog *prog)
1355 {
1356 return (prog->type == BPF_PROG_TYPE_EXT && prog->aux->saved_dst_prog_type) ?
1357 prog->aux->saved_dst_prog_type : prog->type;
1358 }
1359
bpf_prog_check_recur(const struct bpf_prog * prog)1360 static inline bool bpf_prog_check_recur(const struct bpf_prog *prog)
1361 {
1362 switch (resolve_prog_type(prog)) {
1363 case BPF_PROG_TYPE_TRACING:
1364 return prog->expected_attach_type != BPF_TRACE_ITER;
1365 case BPF_PROG_TYPE_STRUCT_OPS:
1366 return prog->aux->jits_use_priv_stack;
1367 case BPF_PROG_TYPE_LSM:
1368 case BPF_PROG_TYPE_SYSCALL:
1369 return false;
1370 default:
1371 return true;
1372 }
1373 }
1374
1375 #define BPF_REG_TRUSTED_MODIFIERS (MEM_ALLOC | PTR_TRUSTED | NON_OWN_REF)
1376
bpf_type_has_unsafe_modifiers(u32 type)1377 static inline bool bpf_type_has_unsafe_modifiers(u32 type)
1378 {
1379 return type_flag(type) & ~BPF_REG_TRUSTED_MODIFIERS;
1380 }
1381
type_is_ptr_alloc_obj(u32 type)1382 static inline bool type_is_ptr_alloc_obj(u32 type)
1383 {
1384 return base_type(type) == PTR_TO_BTF_ID &&
1385 type_flag(type) & MEM_ALLOC &&
1386 !(type_flag(type) & PTR_UNTRUSTED);
1387 }
1388
type_is_non_owning_ref(u32 type)1389 static inline bool type_is_non_owning_ref(u32 type)
1390 {
1391 return type_is_ptr_alloc_obj(type) && type_flag(type) & NON_OWN_REF;
1392 }
1393
type_is_map_ptr(enum bpf_reg_type type)1394 static inline bool type_is_map_ptr(enum bpf_reg_type type)
1395 {
1396 switch (base_type(type)) {
1397 case CONST_PTR_TO_MAP:
1398 case PTR_TO_MAP_KEY:
1399 case PTR_TO_MAP_VALUE:
1400 return true;
1401 default:
1402 return false;
1403 }
1404 }
1405
type_is_pkt_pointer(enum bpf_reg_type type)1406 static inline bool type_is_pkt_pointer(enum bpf_reg_type type)
1407 {
1408 type = base_type(type);
1409 return type == PTR_TO_PACKET ||
1410 type == PTR_TO_PACKET_META;
1411 }
1412
type_is_sk_pointer(enum bpf_reg_type type)1413 static inline bool type_is_sk_pointer(enum bpf_reg_type type)
1414 {
1415 return type == PTR_TO_SOCKET ||
1416 type == PTR_TO_SOCK_COMMON ||
1417 type == PTR_TO_TCP_SOCK ||
1418 type == PTR_TO_XDP_SOCK;
1419 }
1420
type_may_be_null(u32 type)1421 static inline bool type_may_be_null(u32 type)
1422 {
1423 return type & PTR_MAYBE_NULL;
1424 }
1425
mark_reg_scratched(struct bpf_verifier_env * env,u32 regno)1426 static inline void mark_reg_scratched(struct bpf_verifier_env *env, u32 regno)
1427 {
1428 env->scratched_regs |= 1U << regno;
1429 }
1430
mark_stack_slot_scratched(struct bpf_verifier_env * env,u32 spi)1431 static inline void mark_stack_slot_scratched(struct bpf_verifier_env *env, u32 spi)
1432 {
1433 env->scratched_stack_slots |= 1ULL << spi;
1434 }
1435
reg_scratched(const struct bpf_verifier_env * env,u32 regno)1436 static inline bool reg_scratched(const struct bpf_verifier_env *env, u32 regno)
1437 {
1438 return (env->scratched_regs >> regno) & 1;
1439 }
1440
stack_slot_scratched(const struct bpf_verifier_env * env,u64 regno)1441 static inline bool stack_slot_scratched(const struct bpf_verifier_env *env, u64 regno)
1442 {
1443 return (env->scratched_stack_slots >> regno) & 1;
1444 }
1445
verifier_state_scratched(const struct bpf_verifier_env * env)1446 static inline bool verifier_state_scratched(const struct bpf_verifier_env *env)
1447 {
1448 return env->scratched_regs || env->scratched_stack_slots;
1449 }
1450
mark_verifier_state_clean(struct bpf_verifier_env * env)1451 static inline void mark_verifier_state_clean(struct bpf_verifier_env *env)
1452 {
1453 env->scratched_regs = 0U;
1454 env->scratched_stack_slots = 0ULL;
1455 }
1456
1457 /* Used for printing the entire verifier state. */
mark_verifier_state_scratched(struct bpf_verifier_env * env)1458 static inline void mark_verifier_state_scratched(struct bpf_verifier_env *env)
1459 {
1460 env->scratched_regs = ~0U;
1461 env->scratched_stack_slots = ~0ULL;
1462 }
1463
bpf_stack_narrow_access_ok(int off,int fill_size,int spill_size)1464 static inline bool bpf_stack_narrow_access_ok(int off, int fill_size, int spill_size)
1465 {
1466 #ifdef __BIG_ENDIAN
1467 off -= spill_size - fill_size;
1468 #endif
1469
1470 return !(off % BPF_REG_SIZE);
1471 }
1472
insn_is_gotox(struct bpf_insn * insn)1473 static inline bool insn_is_gotox(struct bpf_insn *insn)
1474 {
1475 return BPF_CLASS(insn->code) == BPF_JMP &&
1476 BPF_OP(insn->code) == BPF_JA &&
1477 BPF_SRC(insn->code) == BPF_X;
1478 }
1479
1480 const char *reg_type_str(struct bpf_verifier_env *env, enum bpf_reg_type type);
1481 const char *dynptr_type_str(enum bpf_dynptr_type type);
1482 const char *iter_type_str(const struct btf *btf, u32 btf_id);
1483 const char *iter_state_str(enum bpf_iter_state state);
1484
1485 void print_verifier_state(struct bpf_verifier_env *env, const struct bpf_verifier_state *vstate,
1486 u32 frameno, bool print_all);
1487 void print_insn_state(struct bpf_verifier_env *env, const struct bpf_verifier_state *vstate,
1488 u32 frameno);
1489 u32 bpf_vlog_alignment(u32 pos);
1490 const char *bpf_disasm_kfunc_name(void *data, const struct bpf_insn *insn);
1491
1492 struct bpf_subprog_info *bpf_find_containing_subprog(struct bpf_verifier_env *env, int off);
1493 const char *bpf_subprog_name(const struct bpf_verifier_env *env, int subprog);
1494 int bpf_jmp_offset(struct bpf_insn *insn);
1495 struct bpf_iarray *bpf_insn_successors(struct bpf_verifier_env *env, u32 idx);
1496 void bpf_fmt_stack_mask(char *buf, ssize_t buf_sz, u64 stack_mask);
1497 bool bpf_subprog_is_global(const struct bpf_verifier_env *env, int subprog);
1498
1499 int bpf_find_subprog(struct bpf_verifier_env *env, int off);
1500 bool bpf_is_throw_kfunc(struct bpf_insn *insn);
1501 int bpf_compute_const_regs(struct bpf_verifier_env *env);
1502 int bpf_prune_dead_branches(struct bpf_verifier_env *env);
1503 int bpf_check_cfg(struct bpf_verifier_env *env);
1504 int bpf_compute_postorder(struct bpf_verifier_env *env);
1505 int bpf_compute_scc(struct bpf_verifier_env *env);
1506
1507 struct bpf_map_desc {
1508 struct bpf_map *ptr;
1509 int uid;
1510 };
1511
1512 /* The last initialized dynptr; Populated by process_dynptr_func() */
1513 struct bpf_dynptr_desc {
1514 enum bpf_dynptr_type type;
1515 u32 id;
1516 u32 parent_id;
1517 };
1518
1519 /*
1520 * The last seen rereferenced object; Updated by update_ref_obj() when a register refers to a
1521 * referenced object. Used when the helper or kfunc is casting a referenced object, returning
1522 * allocated memory derived from referenced object or creating a dynptr with a referenced
1523 * object as parent.
1524 */
1525 struct ref_obj_desc {
1526 u32 id;
1527 u32 parent_id;
1528 u8 cnt;
1529 };
1530
1531 /*
1532 * A memory argument a call fills in. The verifier allows the stack to be uninitialized if
1533 * the range is a known constant. Stack slots are marked as STACK_MISC by check_mem_access().
1534 */
1535 struct arg_raw_mem_desc {
1536 u8 regno;
1537 int size;
1538 };
1539
1540 /* Size of PTR_TO_MEM returned, taken from a constant allocation-size argument */
1541 struct ret_mem_desc {
1542 u32 size;
1543 bool found;
1544 };
1545
1546 /* A constant scalar argument; Populated by process_const_arg() */
1547 struct arg_constant_desc {
1548 u64 value;
1549 bool found;
1550 };
1551
1552 struct bpf_call_arg_meta {
1553 /* Common */
1554 struct btf *btf;
1555 u32 func_id;
1556 const struct bpf_func_proto *fn;
1557 u8 release_regno;
1558 u32 ret_btf_id;
1559 u32 subprogno;
1560 struct bpf_map_desc map;
1561 struct bpf_dynptr_desc dynptr;
1562 struct ref_obj_desc ref_obj;
1563 struct ret_mem_desc ret_mem;
1564
1565 /* Only set by kfunc */
1566 bool r0_rdonly;
1567 u32 kfunc_flags;
1568 const struct btf_type *func_proto;
1569 const char *func_name;
1570 struct arg_constant_desc arg_constant;
1571
1572 /* arg_{btf,btf_id,owning_ref} are used by kfunc-specific handling,
1573 * generally to pass info about user-defined local kptr types to later
1574 * verification logic
1575 * bpf_obj_drop/bpf_percpu_obj_drop
1576 * Record the local kptr type to be drop'd
1577 * bpf_refcount_acquire (via KF_ARG_PTR_TO_REFCOUNTED_KPTR arg type)
1578 * Record the local kptr type to be refcount_incr'd and use
1579 * arg_owning_ref to determine whether refcount_acquire should be
1580 * fallible
1581 */
1582 struct btf *arg_btf;
1583 u32 arg_btf_id;
1584 bool arg_owning_ref;
1585 bool arg_prog;
1586
1587 struct {
1588 struct btf_field *field;
1589 } arg_list_head;
1590 struct {
1591 struct btf_field *field;
1592 } arg_rbtree_root;
1593 struct {
1594 u8 spi;
1595 u8 frameno;
1596 } iter;
1597
1598 /* Only set by helper */
1599 u64 msize_max_value;
1600 s64 const_map_key;
1601 struct btf *ret_btf;
1602 struct btf_field *kptr_field;
1603 struct arg_raw_mem_desc arg_raw_mem;
1604 };
1605
1606 int bpf_get_helper_proto(struct bpf_verifier_env *env, int func_id,
1607 const struct bpf_func_proto **ptr);
1608 int bpf_fetch_kfunc_arg_meta(struct bpf_verifier_env *env, s32 func_id,
1609 s16 offset, struct bpf_call_arg_meta *meta);
1610 bool bpf_is_async_callback_calling_insn(struct bpf_insn *insn);
1611 bool bpf_is_sync_callback_calling_insn(struct bpf_insn *insn);
bpf_is_iter_next_kfunc(struct bpf_call_arg_meta * meta)1612 static inline bool bpf_is_iter_next_kfunc(struct bpf_call_arg_meta *meta)
1613 {
1614 return meta->kfunc_flags & KF_ITER_NEXT;
1615 }
1616
bpf_is_kfunc_sleepable(struct bpf_call_arg_meta * meta)1617 static inline bool bpf_is_kfunc_sleepable(struct bpf_call_arg_meta *meta)
1618 {
1619 return meta->kfunc_flags & KF_SLEEPABLE;
1620 }
1621 bool bpf_is_kfunc_pkt_changing(struct bpf_call_arg_meta *meta);
1622 struct bpf_iarray *bpf_iarray_realloc(struct bpf_iarray *old, size_t n_elem);
1623 int bpf_copy_insn_array_uniq(struct bpf_map *map, u32 start, u32 end, u32 *off);
1624 bool bpf_insn_is_cond_jump(u8 code);
1625 bool bpf_is_may_goto_insn(struct bpf_insn *insn);
1626
1627 void bpf_verbose_insn(struct bpf_verifier_env *env, struct bpf_insn *insn);
1628 bool bpf_get_call_summary(struct bpf_verifier_env *env, struct bpf_insn *call,
1629 struct bpf_call_summary *cs);
1630 s64 bpf_helper_stack_access_bytes(struct bpf_verifier_env *env,
1631 struct bpf_insn *insn, int arg,
1632 int insn_idx);
1633 s64 bpf_kfunc_stack_access_bytes(struct bpf_verifier_env *env,
1634 struct bpf_insn *insn, int arg,
1635 int insn_idx);
1636 int bpf_compute_subprog_arg_access(struct bpf_verifier_env *env);
1637
1638 int bpf_stack_liveness_init(struct bpf_verifier_env *env);
1639 void bpf_stack_liveness_free(struct bpf_verifier_env *env);
1640 int bpf_live_stack_query_init(struct bpf_verifier_env *env, struct bpf_verifier_state *st);
1641 bool bpf_stack_slot_alive(struct bpf_verifier_env *env, u32 frameno, u32 spi);
1642 int bpf_compute_live_registers(struct bpf_verifier_env *env);
1643
1644 #define BPF_MAP_KEY_POISON (1ULL << 63)
1645 #define BPF_MAP_KEY_SEEN (1ULL << 62)
1646
bpf_map_ptr_poisoned(const struct bpf_insn_aux_data * aux)1647 static inline bool bpf_map_ptr_poisoned(const struct bpf_insn_aux_data *aux)
1648 {
1649 return aux->map_ptr_state.poison;
1650 }
1651
bpf_map_ptr_unpriv(const struct bpf_insn_aux_data * aux)1652 static inline bool bpf_map_ptr_unpriv(const struct bpf_insn_aux_data *aux)
1653 {
1654 return aux->map_ptr_state.unpriv;
1655 }
1656
bpf_map_key_poisoned(const struct bpf_insn_aux_data * aux)1657 static inline bool bpf_map_key_poisoned(const struct bpf_insn_aux_data *aux)
1658 {
1659 return aux->map_key_state & BPF_MAP_KEY_POISON;
1660 }
1661
bpf_map_key_unseen(const struct bpf_insn_aux_data * aux)1662 static inline bool bpf_map_key_unseen(const struct bpf_insn_aux_data *aux)
1663 {
1664 return !(aux->map_key_state & BPF_MAP_KEY_SEEN);
1665 }
1666
bpf_map_key_immediate(const struct bpf_insn_aux_data * aux)1667 static inline u64 bpf_map_key_immediate(const struct bpf_insn_aux_data *aux)
1668 {
1669 return aux->map_key_state & ~(BPF_MAP_KEY_SEEN | BPF_MAP_KEY_POISON);
1670 }
1671
1672 #define MAX_PACKET_OFF 0xffff
1673 #define CALLER_SAVED_REGS 6
1674
1675 enum bpf_reg_arg_type {
1676 SRC_OP, /* register is used as source operand */
1677 DST_OP, /* register is used as destination operand */
1678 DST_OP_NO_MARK /* same as above, check only, don't mark */
1679 };
1680
1681 #define MAX_KFUNC_DESCS 256
1682
1683 struct bpf_kfunc_desc {
1684 struct btf_func_model func_model;
1685 struct bpf_func_proto proto;
1686 u32 func_id;
1687 s32 imm;
1688 u16 offset;
1689 unsigned long addr;
1690 };
1691
1692 struct bpf_kfunc_desc_tab {
1693 u32 nr_descs;
1694 /* Sorted by func_id (BTF ID) and offset (fd_array offset) during
1695 * verification. JITs do lookups by bpf_insn, where func_id may not be
1696 * available, therefore at the end of verification do_misc_fixups()
1697 * sorts this by imm and offset.
1698 *
1699 * Grown one entry at a time by bpf_add_kfunc_call().
1700 */
1701 struct bpf_kfunc_desc descs[];
1702 };
1703
1704 /* Functions exported from verifier.c, used by fixups.c */
1705 void bpf_clear_insn_aux_data(struct bpf_verifier_env *env, int start, int len);
1706 void bpf_mark_subprog_exc_cb(struct bpf_verifier_env *env, int subprog);
1707 bool bpf_allow_tail_call_in_subprogs(struct bpf_verifier_env *env);
1708 bool bpf_verifier_inlines_helper_call(struct bpf_verifier_env *env, s32 imm);
1709 int bpf_add_kfunc_call(struct bpf_verifier_env *env, u32 func_id, u16 offset);
1710 int bpf_fixup_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
1711 struct bpf_insn *insn_buf, int insn_idx, int *cnt);
1712
1713 /* Functions exported from verifier.c, used by trampoline.c */
1714 int bpf_check_attach_btf_id_multi(struct btf *btf, struct bpf_prog *prog, u32 btf_id,
1715 struct bpf_attach_target_info *tgt_info);
1716
1717 /* Functions in fixups.c, called from bpf_check() */
1718 int bpf_remove_fastcall_spills_fills(struct bpf_verifier_env *env);
1719 int bpf_optimize_bpf_loop(struct bpf_verifier_env *env);
1720 void bpf_opt_hard_wire_dead_code_branches(struct bpf_verifier_env *env);
1721 int bpf_opt_remove_dead_code(struct bpf_verifier_env *env);
1722 int bpf_opt_remove_nops(struct bpf_verifier_env *env);
1723 int bpf_opt_subreg_zext_lo32_rnd_hi32(struct bpf_verifier_env *env, const union bpf_attr *attr);
1724 int bpf_convert_ctx_accesses(struct bpf_verifier_env *env);
1725 int bpf_jit_subprogs(struct bpf_verifier_env *env);
1726 int bpf_fixup_call_args(struct bpf_verifier_env *env);
1727 int bpf_do_misc_fixups(struct bpf_verifier_env *env);
1728 int bpf_insn_def32(struct bpf_prog *prog, struct bpf_insn *insn);
1729
1730 #endif /* _LINUX_BPF_VERIFIER_H */
1731