]>
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 */ | |
f1174f77 | 12 | #include <linux/tnum.h> |
58e2af8b | 13 | |
48461135 JB |
14 | /* Just some arbitrary values so we can safely do math without overflowing and |
15 | * are obviously wrong for any sort of memory access. | |
16 | */ | |
17 | #define BPF_REGISTER_MAX_RANGE (1024 * 1024 * 1024) | |
f23cc643 | 18 | #define BPF_REGISTER_MIN_RANGE -1 |
48461135 | 19 | |
58e2af8b JK |
20 | struct bpf_reg_state { |
21 | enum bpf_reg_type type; | |
22 | union { | |
f1174f77 EC |
23 | /* valid when type == PTR_TO_PACKET */ |
24 | u16 range; | |
58e2af8b JK |
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 | }; | |
f1174f77 EC |
31 | /* Fixed part of pointer offset, pointer types only */ |
32 | s32 off; | |
33 | /* For PTR_TO_PACKET, used to find other pointers with the same variable | |
34 | * offset, so they can share range knowledge. | |
35 | * For PTR_TO_MAP_VALUE_OR_NULL this is used to share which map value we | |
36 | * came from, when one is tested for != NULL. | |
37 | */ | |
d2a4dd37 | 38 | u32 id; |
f1174f77 EC |
39 | /* These three fields must be last. See states_equal() */ |
40 | /* For scalar types (SCALAR_VALUE), this represents our knowledge of | |
41 | * the actual value. | |
42 | * For pointer types, this represents the variable part of the offset | |
43 | * from the pointed-to object, and is shared with all bpf_reg_states | |
44 | * with the same id as us. | |
45 | */ | |
46 | struct tnum var_off; | |
d2a4dd37 | 47 | /* Used to determine if any memory access using this register will |
f1174f77 EC |
48 | * result in a bad access. |
49 | * These refer to the same value as var_off, not necessarily the actual | |
50 | * contents of the register. | |
d2a4dd37 AS |
51 | */ |
52 | s64 min_value; | |
53 | u64 max_value; | |
4cabc5b1 | 54 | bool value_from_signed; |
58e2af8b JK |
55 | }; |
56 | ||
57 | enum bpf_stack_slot_type { | |
58 | STACK_INVALID, /* nothing was stored in this stack slot */ | |
59 | STACK_SPILL, /* register spilled into stack */ | |
60 | STACK_MISC /* BPF program wrote some data into this slot */ | |
61 | }; | |
62 | ||
63 | #define BPF_REG_SIZE 8 /* size of eBPF register in bytes */ | |
64 | ||
65 | /* state of the program: | |
66 | * type of all registers and stack info | |
67 | */ | |
68 | struct bpf_verifier_state { | |
69 | struct bpf_reg_state regs[MAX_BPF_REG]; | |
70 | u8 stack_slot_type[MAX_BPF_STACK]; | |
71 | struct bpf_reg_state spilled_regs[MAX_BPF_STACK / BPF_REG_SIZE]; | |
72 | }; | |
73 | ||
74 | /* linked list of verifier states used to prune search */ | |
75 | struct bpf_verifier_state_list { | |
76 | struct bpf_verifier_state state; | |
77 | struct bpf_verifier_state_list *next; | |
78 | }; | |
79 | ||
80 | struct bpf_insn_aux_data { | |
81ed18ab AS |
81 | union { |
82 | enum bpf_reg_type ptr_type; /* pointer type for load/store insns */ | |
83 | struct bpf_map *map_ptr; /* pointer for call insn into lookup_elem */ | |
84 | }; | |
23994631 YS |
85 | int ctx_field_size; /* the ctx field size for load insn, maybe 0 */ |
86 | int converted_op_size; /* the valid value width after perceived conversion */ | |
58e2af8b JK |
87 | }; |
88 | ||
89 | #define MAX_USED_MAPS 64 /* max number of maps accessed by one eBPF program */ | |
90 | ||
13a27dfc JK |
91 | struct bpf_verifier_env; |
92 | struct bpf_ext_analyzer_ops { | |
93 | int (*insn_hook)(struct bpf_verifier_env *env, | |
94 | int insn_idx, int prev_insn_idx); | |
95 | }; | |
96 | ||
58e2af8b JK |
97 | /* single container for all structs |
98 | * one verifier_env per bpf_check() call | |
99 | */ | |
100 | struct bpf_verifier_env { | |
101 | struct bpf_prog *prog; /* eBPF program being verified */ | |
102 | struct bpf_verifier_stack_elem *head; /* stack of verifier states to be processed */ | |
103 | int stack_size; /* number of states to be processed */ | |
e07b98d9 | 104 | bool strict_alignment; /* perform strict pointer alignment checks */ |
58e2af8b JK |
105 | struct bpf_verifier_state cur_state; /* current verifier state */ |
106 | struct bpf_verifier_state_list **explored_states; /* search pruning optimization */ | |
13a27dfc JK |
107 | const struct bpf_ext_analyzer_ops *analyzer_ops; /* external analyzer ops */ |
108 | void *analyzer_priv; /* pointer to external analyzer's private data */ | |
58e2af8b JK |
109 | struct bpf_map *used_maps[MAX_USED_MAPS]; /* array of map's used by eBPF program */ |
110 | u32 used_map_cnt; /* number of used maps */ | |
111 | u32 id_gen; /* used to generate unique reg IDs */ | |
112 | bool allow_ptr_leaks; | |
113 | bool seen_direct_write; | |
48461135 | 114 | bool varlen_map_value_access; |
58e2af8b JK |
115 | struct bpf_insn_aux_data *insn_aux_data; /* array of per-insn state */ |
116 | }; | |
117 | ||
13a27dfc JK |
118 | int bpf_analyzer(struct bpf_prog *prog, const struct bpf_ext_analyzer_ops *ops, |
119 | void *priv); | |
120 | ||
58e2af8b | 121 | #endif /* _LINUX_BPF_VERIFIER_H */ |