bpf_verifier.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. /* Just some arbitrary values so we can safely do math without overflowing and
  12. * are obviously wrong for any sort of memory access.
  13. */
  14. #define BPF_REGISTER_MAX_RANGE (1024 * 1024 * 1024)
  15. #define BPF_REGISTER_MIN_RANGE -1
  16. struct bpf_reg_state {
  17. enum bpf_reg_type type;
  18. union {
  19. /* valid when type == CONST_IMM | PTR_TO_STACK | UNKNOWN_VALUE */
  20. s64 imm;
  21. /* valid when type == PTR_TO_PACKET* */
  22. struct {
  23. u16 off;
  24. u16 range;
  25. };
  26. /* valid when type == CONST_PTR_TO_MAP | PTR_TO_MAP_VALUE |
  27. * PTR_TO_MAP_VALUE_OR_NULL
  28. */
  29. struct bpf_map *map_ptr;
  30. };
  31. u32 id;
  32. /* Used to determine if any memory access using this register will
  33. * result in a bad access. These two fields must be last.
  34. * See states_equal()
  35. */
  36. s64 min_value;
  37. u64 max_value;
  38. bool value_from_signed;
  39. };
  40. enum bpf_stack_slot_type {
  41. STACK_INVALID, /* nothing was stored in this stack slot */
  42. STACK_SPILL, /* register spilled into stack */
  43. STACK_MISC /* BPF program wrote some data into this slot */
  44. };
  45. #define BPF_REG_SIZE 8 /* size of eBPF register in bytes */
  46. /* state of the program:
  47. * type of all registers and stack info
  48. */
  49. struct bpf_verifier_state {
  50. struct bpf_reg_state regs[MAX_BPF_REG];
  51. u8 stack_slot_type[MAX_BPF_STACK];
  52. struct bpf_reg_state spilled_regs[MAX_BPF_STACK / BPF_REG_SIZE];
  53. };
  54. /* linked list of verifier states used to prune search */
  55. struct bpf_verifier_state_list {
  56. struct bpf_verifier_state state;
  57. struct bpf_verifier_state_list *next;
  58. };
  59. struct bpf_insn_aux_data {
  60. union {
  61. enum bpf_reg_type ptr_type; /* pointer type for load/store insns */
  62. struct bpf_map *map_ptr; /* pointer for call insn into lookup_elem */
  63. };
  64. bool seen; /* this insn was processed by the verifier */
  65. };
  66. #define MAX_USED_MAPS 64 /* max number of maps accessed by one eBPF program */
  67. struct bpf_verifier_env;
  68. struct bpf_ext_analyzer_ops {
  69. int (*insn_hook)(struct bpf_verifier_env *env,
  70. int insn_idx, int prev_insn_idx);
  71. };
  72. /* single container for all structs
  73. * one verifier_env per bpf_check() call
  74. */
  75. struct bpf_verifier_env {
  76. struct bpf_prog *prog; /* eBPF program being verified */
  77. struct bpf_verifier_stack_elem *head; /* stack of verifier states to be processed */
  78. int stack_size; /* number of states to be processed */
  79. struct bpf_verifier_state cur_state; /* current verifier state */
  80. struct bpf_verifier_state_list **explored_states; /* search pruning optimization */
  81. const struct bpf_ext_analyzer_ops *analyzer_ops; /* external analyzer ops */
  82. void *analyzer_priv; /* pointer to external analyzer's private data */
  83. struct bpf_map *used_maps[MAX_USED_MAPS]; /* array of map's used by eBPF program */
  84. u32 used_map_cnt; /* number of used maps */
  85. u32 id_gen; /* used to generate unique reg IDs */
  86. bool allow_ptr_leaks;
  87. bool seen_direct_write;
  88. bool varlen_map_value_access;
  89. struct bpf_insn_aux_data *insn_aux_data; /* array of per-insn state */
  90. };
  91. int bpf_analyzer(struct bpf_prog *prog, const struct bpf_ext_analyzer_ops *ops,
  92. void *priv);
  93. #endif /* _LINUX_BPF_VERIFIER_H */