bpf_verifier.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
  2. *
  3. * This program is free software; you can redistribute it and/or
  4. * modify it under the terms of version 2 of the GNU General Public
  5. * License as published by the Free Software Foundation.
  6. */
  7. #ifndef _LINUX_BPF_VERIFIER_H
  8. #define _LINUX_BPF_VERIFIER_H 1
  9. #include <linux/bpf.h> /* for enum bpf_reg_type */
  10. #include <linux/filter.h> /* for MAX_BPF_STACK */
  11. #include <linux/tnum.h>
  12. /* Maximum variable offset umax_value permitted when resolving memory accesses.
  13. * In practice this is far bigger than any realistic pointer offset; this limit
  14. * ensures that umax_value + (int)off + (int)size cannot overflow a u64.
  15. */
  16. #define BPF_MAX_VAR_OFF (1 << 29)
  17. /* Maximum variable size permitted for ARG_CONST_SIZE[_OR_ZERO]. This ensures
  18. * that converting umax_value to int cannot overflow.
  19. */
  20. #define BPF_MAX_VAR_SIZ (1 << 29)
  21. /* Liveness marks, used for registers and spilled-regs (in stack slots).
  22. * Read marks propagate upwards until they find a write mark; they record that
  23. * "one of this state's descendants read this reg" (and therefore the reg is
  24. * relevant for states_equal() checks).
  25. * Write marks collect downwards and do not propagate; they record that "the
  26. * straight-line code that reached this state (from its parent) wrote this reg"
  27. * (and therefore that reads propagated from this state or its descendants
  28. * should not propagate to its parent).
  29. * A state with a write mark can receive read marks; it just won't propagate
  30. * them to its parent, since the write mark is a property, not of the state,
  31. * but of the link between it and its parent. See mark_reg_read() and
  32. * mark_stack_slot_read() in kernel/bpf/verifier.c.
  33. */
  34. enum bpf_reg_liveness {
  35. REG_LIVE_NONE = 0, /* reg hasn't been read or written this branch */
  36. REG_LIVE_READ, /* reg was read, so we're sensitive to initial value */
  37. REG_LIVE_WRITTEN, /* reg was written first, screening off later reads */
  38. };
  39. struct bpf_reg_state {
  40. enum bpf_reg_type type;
  41. union {
  42. /* valid when type == PTR_TO_PACKET */
  43. u16 range;
  44. /* valid when type == CONST_PTR_TO_MAP | PTR_TO_MAP_VALUE |
  45. * PTR_TO_MAP_VALUE_OR_NULL
  46. */
  47. struct bpf_map *map_ptr;
  48. /* Max size from any of the above. */
  49. unsigned long raw;
  50. };
  51. /* Fixed part of pointer offset, pointer types only */
  52. s32 off;
  53. /* For PTR_TO_PACKET, used to find other pointers with the same variable
  54. * offset, so they can share range knowledge.
  55. * For PTR_TO_MAP_VALUE_OR_NULL this is used to share which map value we
  56. * came from, when one is tested for != NULL.
  57. */
  58. u32 id;
  59. /* Ordering of fields matters. See states_equal() */
  60. /* For scalar types (SCALAR_VALUE), this represents our knowledge of
  61. * the actual value.
  62. * For pointer types, this represents the variable part of the offset
  63. * from the pointed-to object, and is shared with all bpf_reg_states
  64. * with the same id as us.
  65. */
  66. struct tnum var_off;
  67. /* Used to determine if any memory access using this register will
  68. * result in a bad access.
  69. * These refer to the same value as var_off, not necessarily the actual
  70. * contents of the register.
  71. */
  72. s64 smin_value; /* minimum possible (s64)value */
  73. s64 smax_value; /* maximum possible (s64)value */
  74. u64 umin_value; /* minimum possible (u64)value */
  75. u64 umax_value; /* maximum possible (u64)value */
  76. /* Inside the callee two registers can be both PTR_TO_STACK like
  77. * R1=fp-8 and R2=fp-8, but one of them points to this function stack
  78. * while another to the caller's stack. To differentiate them 'frameno'
  79. * is used which is an index in bpf_verifier_state->frame[] array
  80. * pointing to bpf_func_state.
  81. * This field must be second to last, for states_equal() reasons.
  82. */
  83. u32 frameno;
  84. /* This field must be last, for states_equal() reasons. */
  85. enum bpf_reg_liveness live;
  86. };
  87. enum bpf_stack_slot_type {
  88. STACK_INVALID, /* nothing was stored in this stack slot */
  89. STACK_SPILL, /* register spilled into stack */
  90. STACK_MISC, /* BPF program wrote some data into this slot */
  91. STACK_ZERO, /* BPF program wrote constant zero */
  92. };
  93. #define BPF_REG_SIZE 8 /* size of eBPF register in bytes */
  94. struct bpf_stack_state {
  95. struct bpf_reg_state spilled_ptr;
  96. u8 slot_type[BPF_REG_SIZE];
  97. };
  98. /* state of the program:
  99. * type of all registers and stack info
  100. */
  101. struct bpf_func_state {
  102. struct bpf_reg_state regs[MAX_BPF_REG];
  103. struct bpf_verifier_state *parent;
  104. /* index of call instruction that called into this func */
  105. int callsite;
  106. /* stack frame number of this function state from pov of
  107. * enclosing bpf_verifier_state.
  108. * 0 = main function, 1 = first callee.
  109. */
  110. u32 frameno;
  111. /* subprog number == index within subprog_stack_depth
  112. * zero == main subprog
  113. */
  114. u32 subprogno;
  115. /* should be second to last. See copy_func_state() */
  116. int allocated_stack;
  117. struct bpf_stack_state *stack;
  118. };
  119. #define MAX_CALL_FRAMES 8
  120. struct bpf_verifier_state {
  121. /* call stack tracking */
  122. struct bpf_func_state *frame[MAX_CALL_FRAMES];
  123. struct bpf_verifier_state *parent;
  124. u32 curframe;
  125. bool speculative;
  126. };
  127. /* linked list of verifier states used to prune search */
  128. struct bpf_verifier_state_list {
  129. struct bpf_verifier_state state;
  130. struct bpf_verifier_state_list *next;
  131. };
  132. /* Possible states for alu_state member. */
  133. #define BPF_ALU_SANITIZE_SRC 1U
  134. #define BPF_ALU_SANITIZE_DST 2U
  135. #define BPF_ALU_NEG_VALUE (1U << 2)
  136. #define BPF_ALU_NON_POINTER (1U << 3)
  137. #define BPF_ALU_SANITIZE (BPF_ALU_SANITIZE_SRC | \
  138. BPF_ALU_SANITIZE_DST)
  139. struct bpf_insn_aux_data {
  140. union {
  141. enum bpf_reg_type ptr_type; /* pointer type for load/store insns */
  142. unsigned long map_state; /* pointer/poison value for maps */
  143. s32 call_imm; /* saved imm field of call insn */
  144. u32 alu_limit; /* limit for add/sub register with pointer */
  145. };
  146. int ctx_field_size; /* the ctx field size for load insn, maybe 0 */
  147. int sanitize_stack_off; /* stack slot to be cleared */
  148. bool seen; /* this insn was processed by the verifier */
  149. u8 alu_state; /* used in combination with alu_limit */
  150. };
  151. #define MAX_USED_MAPS 64 /* max number of maps accessed by one eBPF program */
  152. #define BPF_VERIFIER_TMP_LOG_SIZE 1024
  153. struct bpf_verifier_log {
  154. u32 level;
  155. char kbuf[BPF_VERIFIER_TMP_LOG_SIZE];
  156. char __user *ubuf;
  157. u32 len_used;
  158. u32 len_total;
  159. };
  160. static inline bool bpf_verifier_log_full(const struct bpf_verifier_log *log)
  161. {
  162. return log->len_used >= log->len_total - 1;
  163. }
  164. static inline bool bpf_verifier_log_needed(const struct bpf_verifier_log *log)
  165. {
  166. return log->level && log->ubuf && !bpf_verifier_log_full(log);
  167. }
  168. #define BPF_MAX_SUBPROGS 256
  169. struct bpf_subprog_info {
  170. u32 start; /* insn idx of function entry point */
  171. u16 stack_depth; /* max. stack depth used by this function */
  172. };
  173. /* single container for all structs
  174. * one verifier_env per bpf_check() call
  175. */
  176. struct bpf_verifier_env {
  177. u32 insn_idx;
  178. u32 prev_insn_idx;
  179. struct bpf_prog *prog; /* eBPF program being verified */
  180. const struct bpf_verifier_ops *ops;
  181. struct bpf_verifier_stack_elem *head; /* stack of verifier states to be processed */
  182. int stack_size; /* number of states to be processed */
  183. bool strict_alignment; /* perform strict pointer alignment checks */
  184. struct bpf_verifier_state *cur_state; /* current verifier state */
  185. struct bpf_verifier_state_list **explored_states; /* search pruning optimization */
  186. struct bpf_map *used_maps[MAX_USED_MAPS]; /* array of map's used by eBPF program */
  187. u32 used_map_cnt; /* number of used maps */
  188. u32 id_gen; /* used to generate unique reg IDs */
  189. bool allow_ptr_leaks;
  190. bool seen_direct_write;
  191. struct bpf_insn_aux_data *insn_aux_data; /* array of per-insn state */
  192. struct bpf_verifier_log log;
  193. struct bpf_subprog_info subprog_info[BPF_MAX_SUBPROGS + 1];
  194. u32 subprog_cnt;
  195. };
  196. __printf(2, 0) void bpf_verifier_vlog(struct bpf_verifier_log *log,
  197. const char *fmt, va_list args);
  198. __printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env,
  199. const char *fmt, ...);
  200. static inline struct bpf_reg_state *cur_regs(struct bpf_verifier_env *env)
  201. {
  202. struct bpf_verifier_state *cur = env->cur_state;
  203. return cur->frame[cur->curframe]->regs;
  204. }
  205. int bpf_prog_offload_verifier_prep(struct bpf_verifier_env *env);
  206. int bpf_prog_offload_verify_insn(struct bpf_verifier_env *env,
  207. int insn_idx, int prev_insn_idx);
  208. #endif /* _LINUX_BPF_VERIFIER_H */