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