bpf_verifier.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. };
  49. /* Fixed part of pointer offset, pointer types only */
  50. s32 off;
  51. /* For PTR_TO_PACKET, used to find other pointers with the same variable
  52. * offset, so they can share range knowledge.
  53. * For PTR_TO_MAP_VALUE_OR_NULL this is used to share which map value we
  54. * came from, when one is tested for != NULL.
  55. */
  56. u32 id;
  57. /* Ordering of fields matters. See states_equal() */
  58. /* For scalar types (SCALAR_VALUE), this represents our knowledge of
  59. * the actual value.
  60. * For pointer types, this represents the variable part of the offset
  61. * from the pointed-to object, and is shared with all bpf_reg_states
  62. * with the same id as us.
  63. */
  64. struct tnum var_off;
  65. /* Used to determine if any memory access using this register will
  66. * result in a bad access.
  67. * These refer to the same value as var_off, not necessarily the actual
  68. * contents of the register.
  69. */
  70. s64 smin_value; /* minimum possible (s64)value */
  71. s64 smax_value; /* maximum possible (s64)value */
  72. u64 umin_value; /* minimum possible (u64)value */
  73. u64 umax_value; /* maximum possible (u64)value */
  74. /* Inside the callee two registers can be both PTR_TO_STACK like
  75. * R1=fp-8 and R2=fp-8, but one of them points to this function stack
  76. * while another to the caller's stack. To differentiate them 'frameno'
  77. * is used which is an index in bpf_verifier_state->frame[] array
  78. * pointing to bpf_func_state.
  79. * This field must be second to last, for states_equal() reasons.
  80. */
  81. u32 frameno;
  82. /* This field must be last, for states_equal() reasons. */
  83. enum bpf_reg_liveness live;
  84. };
  85. enum bpf_stack_slot_type {
  86. STACK_INVALID, /* nothing was stored in this stack slot */
  87. STACK_SPILL, /* register spilled into stack */
  88. STACK_MISC, /* BPF program wrote some data into this slot */
  89. STACK_ZERO, /* BPF program wrote constant zero */
  90. };
  91. #define BPF_REG_SIZE 8 /* size of eBPF register in bytes */
  92. struct bpf_stack_state {
  93. struct bpf_reg_state spilled_ptr;
  94. u8 slot_type[BPF_REG_SIZE];
  95. };
  96. /* state of the program:
  97. * type of all registers and stack info
  98. */
  99. struct bpf_func_state {
  100. struct bpf_reg_state regs[MAX_BPF_REG];
  101. struct bpf_verifier_state *parent;
  102. /* index of call instruction that called into this func */
  103. int callsite;
  104. /* stack frame number of this function state from pov of
  105. * enclosing bpf_verifier_state.
  106. * 0 = main function, 1 = first callee.
  107. */
  108. u32 frameno;
  109. /* subprog number == index within subprog_stack_depth
  110. * zero == main subprog
  111. */
  112. u32 subprogno;
  113. /* should be second to last. See copy_func_state() */
  114. int allocated_stack;
  115. struct bpf_stack_state *stack;
  116. };
  117. #define MAX_CALL_FRAMES 8
  118. struct bpf_verifier_state {
  119. /* call stack tracking */
  120. struct bpf_func_state *frame[MAX_CALL_FRAMES];
  121. struct bpf_verifier_state *parent;
  122. u32 curframe;
  123. };
  124. /* linked list of verifier states used to prune search */
  125. struct bpf_verifier_state_list {
  126. struct bpf_verifier_state state;
  127. struct bpf_verifier_state_list *next;
  128. };
  129. struct bpf_insn_aux_data {
  130. union {
  131. enum bpf_reg_type ptr_type; /* pointer type for load/store insns */
  132. struct bpf_map *map_ptr; /* pointer for call insn into lookup_elem */
  133. s32 call_imm; /* saved imm field of call insn */
  134. };
  135. int ctx_field_size; /* the ctx field size for load insn, maybe 0 */
  136. bool seen; /* this insn was processed by the verifier */
  137. };
  138. #define MAX_USED_MAPS 64 /* max number of maps accessed by one eBPF program */
  139. #define BPF_VERIFIER_TMP_LOG_SIZE 1024
  140. struct bpf_verifier_log {
  141. u32 level;
  142. char kbuf[BPF_VERIFIER_TMP_LOG_SIZE];
  143. char __user *ubuf;
  144. u32 len_used;
  145. u32 len_total;
  146. };
  147. static inline bool bpf_verifier_log_full(const struct bpf_verifier_log *log)
  148. {
  149. return log->len_used >= log->len_total - 1;
  150. }
  151. static inline bool bpf_verifier_log_needed(const struct bpf_verifier_log *log)
  152. {
  153. return log->level && log->ubuf && !bpf_verifier_log_full(log);
  154. }
  155. #define BPF_MAX_SUBPROGS 256
  156. /* single container for all structs
  157. * one verifier_env per bpf_check() call
  158. */
  159. struct bpf_verifier_env {
  160. struct bpf_prog *prog; /* eBPF program being verified */
  161. const struct bpf_verifier_ops *ops;
  162. struct bpf_verifier_stack_elem *head; /* stack of verifier states to be processed */
  163. int stack_size; /* number of states to be processed */
  164. bool strict_alignment; /* perform strict pointer alignment checks */
  165. struct bpf_verifier_state *cur_state; /* current verifier state */
  166. struct bpf_verifier_state_list **explored_states; /* search pruning optimization */
  167. struct bpf_map *used_maps[MAX_USED_MAPS]; /* array of map's used by eBPF program */
  168. u32 used_map_cnt; /* number of used maps */
  169. u32 id_gen; /* used to generate unique reg IDs */
  170. bool allow_ptr_leaks;
  171. bool seen_direct_write;
  172. struct bpf_insn_aux_data *insn_aux_data; /* array of per-insn state */
  173. struct bpf_verifier_log log;
  174. u32 subprog_starts[BPF_MAX_SUBPROGS];
  175. /* computes the stack depth of each bpf function */
  176. u16 subprog_stack_depth[BPF_MAX_SUBPROGS + 1];
  177. u32 subprog_cnt;
  178. };
  179. void bpf_verifier_vlog(struct bpf_verifier_log *log, const char *fmt,
  180. va_list args);
  181. __printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env,
  182. const char *fmt, ...);
  183. static inline struct bpf_reg_state *cur_regs(struct bpf_verifier_env *env)
  184. {
  185. struct bpf_verifier_state *cur = env->cur_state;
  186. return cur->frame[cur->curframe]->regs;
  187. }
  188. int bpf_prog_offload_verifier_prep(struct bpf_verifier_env *env);
  189. int bpf_prog_offload_verify_insn(struct bpf_verifier_env *env,
  190. int insn_idx, int prev_insn_idx);
  191. #endif /* _LINUX_BPF_VERIFIER_H */