Commit | Line | Data |
---|---|---|
58e2af8b JK |
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 | ||
48461135 JB |
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) | |
f23cc643 | 17 | #define BPF_REGISTER_MIN_RANGE -1 |
48461135 | 18 | |
58e2af8b JK |
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 { | |
58e2af8b JK |
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 | }; | |
d2a4dd37 AS |
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; | |
d1174416 DM |
43 | u32 min_align; |
44 | u32 aux_off; | |
45 | u32 aux_off_align; | |
58e2af8b JK |
46 | }; |
47 | ||
48 | enum bpf_stack_slot_type { | |
49 | STACK_INVALID, /* nothing was stored in this stack slot */ | |
50 | STACK_SPILL, /* register spilled into stack */ | |
51 | STACK_MISC /* BPF program wrote some data into this slot */ | |
52 | }; | |
53 | ||
54 | #define BPF_REG_SIZE 8 /* size of eBPF register in bytes */ | |
55 | ||
56 | /* state of the program: | |
57 | * type of all registers and stack info | |
58 | */ | |
59 | struct bpf_verifier_state { | |
60 | struct bpf_reg_state regs[MAX_BPF_REG]; | |
61 | u8 stack_slot_type[MAX_BPF_STACK]; | |
62 | struct bpf_reg_state spilled_regs[MAX_BPF_STACK / BPF_REG_SIZE]; | |
63 | }; | |
64 | ||
65 | /* linked list of verifier states used to prune search */ | |
66 | struct bpf_verifier_state_list { | |
67 | struct bpf_verifier_state state; | |
68 | struct bpf_verifier_state_list *next; | |
69 | }; | |
70 | ||
71 | struct bpf_insn_aux_data { | |
81ed18ab AS |
72 | union { |
73 | enum bpf_reg_type ptr_type; /* pointer type for load/store insns */ | |
74 | struct bpf_map *map_ptr; /* pointer for call insn into lookup_elem */ | |
75 | }; | |
58e2af8b JK |
76 | }; |
77 | ||
78 | #define MAX_USED_MAPS 64 /* max number of maps accessed by one eBPF program */ | |
79 | ||
13a27dfc JK |
80 | struct bpf_verifier_env; |
81 | struct bpf_ext_analyzer_ops { | |
82 | int (*insn_hook)(struct bpf_verifier_env *env, | |
83 | int insn_idx, int prev_insn_idx); | |
84 | }; | |
85 | ||
58e2af8b JK |
86 | /* single container for all structs |
87 | * one verifier_env per bpf_check() call | |
88 | */ | |
89 | struct bpf_verifier_env { | |
90 | struct bpf_prog *prog; /* eBPF program being verified */ | |
91 | struct bpf_verifier_stack_elem *head; /* stack of verifier states to be processed */ | |
92 | int stack_size; /* number of states to be processed */ | |
93 | struct bpf_verifier_state cur_state; /* current verifier state */ | |
94 | struct bpf_verifier_state_list **explored_states; /* search pruning optimization */ | |
13a27dfc JK |
95 | const struct bpf_ext_analyzer_ops *analyzer_ops; /* external analyzer ops */ |
96 | void *analyzer_priv; /* pointer to external analyzer's private data */ | |
58e2af8b JK |
97 | struct bpf_map *used_maps[MAX_USED_MAPS]; /* array of map's used by eBPF program */ |
98 | u32 used_map_cnt; /* number of used maps */ | |
99 | u32 id_gen; /* used to generate unique reg IDs */ | |
100 | bool allow_ptr_leaks; | |
101 | bool seen_direct_write; | |
48461135 | 102 | bool varlen_map_value_access; |
58e2af8b JK |
103 | struct bpf_insn_aux_data *insn_aux_data; /* array of per-insn state */ |
104 | }; | |
105 | ||
13a27dfc JK |
106 | int bpf_analyzer(struct bpf_prog *prog, const struct bpf_ext_analyzer_ops *ops, |
107 | void *priv); | |
108 | ||
58e2af8b | 109 | #endif /* _LINUX_BPF_VERIFIER_H */ |