bpf_verifier.h 23 KB

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