]>
Commit | Line | Data |
---|---|---|
25763b3c | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
58e2af8b | 2 | /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com |
58e2af8b JK |
3 | */ |
4 | #ifndef _LINUX_BPF_VERIFIER_H | |
5 | #define _LINUX_BPF_VERIFIER_H 1 | |
6 | ||
7 | #include <linux/bpf.h> /* for enum bpf_reg_type */ | |
8 | #include <linux/filter.h> /* for MAX_BPF_STACK */ | |
f1174f77 | 9 | #include <linux/tnum.h> |
58e2af8b | 10 | |
b03c9f9f EC |
11 | /* Maximum variable offset umax_value permitted when resolving memory accesses. |
12 | * In practice this is far bigger than any realistic pointer offset; this limit | |
13 | * ensures that umax_value + (int)off + (int)size cannot overflow a u64. | |
14 | */ | |
bb7f0f98 | 15 | #define BPF_MAX_VAR_OFF (1 << 29) |
b03c9f9f EC |
16 | /* Maximum variable size permitted for ARG_CONST_SIZE[_OR_ZERO]. This ensures |
17 | * that converting umax_value to int cannot overflow. | |
18 | */ | |
bb7f0f98 | 19 | #define BPF_MAX_VAR_SIZ (1 << 29) |
48461135 | 20 | |
8e9cd9ce EC |
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 | */ | |
dc503a8a EC |
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 */ | |
9242b5f5 | 38 | REG_LIVE_DONE = 4, /* liveness won't be updating this register anymore */ |
dc503a8a EC |
39 | }; |
40 | ||
58e2af8b | 41 | struct bpf_reg_state { |
679c782d | 42 | /* Ordering of fields matters. See states_equal() */ |
58e2af8b JK |
43 | enum bpf_reg_type type; |
44 | union { | |
f1174f77 EC |
45 | /* valid when type == PTR_TO_PACKET */ |
46 | u16 range; | |
58e2af8b JK |
47 | |
48 | /* valid when type == CONST_PTR_TO_MAP | PTR_TO_MAP_VALUE | | |
49 | * PTR_TO_MAP_VALUE_OR_NULL | |
50 | */ | |
51 | struct bpf_map *map_ptr; | |
0962590e DB |
52 | |
53 | /* Max size from any of the above. */ | |
54 | unsigned long raw; | |
58e2af8b | 55 | }; |
f1174f77 EC |
56 | /* Fixed part of pointer offset, pointer types only */ |
57 | s32 off; | |
58 | /* For PTR_TO_PACKET, used to find other pointers with the same variable | |
59 | * offset, so they can share range knowledge. | |
60 | * For PTR_TO_MAP_VALUE_OR_NULL this is used to share which map value we | |
61 | * came from, when one is tested for != NULL. | |
c64b7983 JS |
62 | * For PTR_TO_SOCKET this is used to share which pointers retain the |
63 | * same reference to the socket, to determine proper reference freeing. | |
f1174f77 | 64 | */ |
d2a4dd37 | 65 | u32 id; |
1b986589 MKL |
66 | /* PTR_TO_SOCKET and PTR_TO_TCP_SOCK could be a ptr returned |
67 | * from a pointer-cast helper, bpf_sk_fullsock() and | |
68 | * bpf_tcp_sock(). | |
69 | * | |
70 | * Consider the following where "sk" is a reference counted | |
71 | * pointer returned from "sk = bpf_sk_lookup_tcp();": | |
72 | * | |
73 | * 1: sk = bpf_sk_lookup_tcp(); | |
74 | * 2: if (!sk) { return 0; } | |
75 | * 3: fullsock = bpf_sk_fullsock(sk); | |
76 | * 4: if (!fullsock) { bpf_sk_release(sk); return 0; } | |
77 | * 5: tp = bpf_tcp_sock(fullsock); | |
78 | * 6: if (!tp) { bpf_sk_release(sk); return 0; } | |
79 | * 7: bpf_sk_release(sk); | |
80 | * 8: snd_cwnd = tp->snd_cwnd; // verifier will complain | |
81 | * | |
82 | * After bpf_sk_release(sk) at line 7, both "fullsock" ptr and | |
83 | * "tp" ptr should be invalidated also. In order to do that, | |
84 | * the reg holding "fullsock" and "sk" need to remember | |
85 | * the original refcounted ptr id (i.e. sk_reg->id) in ref_obj_id | |
86 | * such that the verifier can reset all regs which have | |
87 | * ref_obj_id matching the sk_reg->id. | |
88 | * | |
89 | * sk_reg->ref_obj_id is set to sk_reg->id at line 1. | |
90 | * sk_reg->id will stay as NULL-marking purpose only. | |
91 | * After NULL-marking is done, sk_reg->id can be reset to 0. | |
92 | * | |
93 | * After "fullsock = bpf_sk_fullsock(sk);" at line 3, | |
94 | * fullsock_reg->ref_obj_id is set to sk_reg->ref_obj_id. | |
95 | * | |
96 | * After "tp = bpf_tcp_sock(fullsock);" at line 5, | |
97 | * tp_reg->ref_obj_id is set to fullsock_reg->ref_obj_id | |
98 | * which is the same as sk_reg->ref_obj_id. | |
99 | * | |
100 | * From the verifier perspective, if sk, fullsock and tp | |
101 | * are not NULL, they are the same ptr with different | |
102 | * reg->type. In particular, bpf_sk_release(tp) is also | |
103 | * allowed and has the same effect as bpf_sk_release(sk). | |
104 | */ | |
105 | u32 ref_obj_id; | |
f1174f77 EC |
106 | /* For scalar types (SCALAR_VALUE), this represents our knowledge of |
107 | * the actual value. | |
108 | * For pointer types, this represents the variable part of the offset | |
109 | * from the pointed-to object, and is shared with all bpf_reg_states | |
110 | * with the same id as us. | |
111 | */ | |
112 | struct tnum var_off; | |
d2a4dd37 | 113 | /* Used to determine if any memory access using this register will |
f1174f77 EC |
114 | * result in a bad access. |
115 | * These refer to the same value as var_off, not necessarily the actual | |
116 | * contents of the register. | |
d2a4dd37 | 117 | */ |
b03c9f9f EC |
118 | s64 smin_value; /* minimum possible (s64)value */ |
119 | s64 smax_value; /* maximum possible (s64)value */ | |
120 | u64 umin_value; /* minimum possible (u64)value */ | |
121 | u64 umax_value; /* maximum possible (u64)value */ | |
679c782d EC |
122 | /* parentage chain for liveness checking */ |
123 | struct bpf_reg_state *parent; | |
f4d7e40a AS |
124 | /* Inside the callee two registers can be both PTR_TO_STACK like |
125 | * R1=fp-8 and R2=fp-8, but one of them points to this function stack | |
126 | * while another to the caller's stack. To differentiate them 'frameno' | |
127 | * is used which is an index in bpf_verifier_state->frame[] array | |
128 | * pointing to bpf_func_state. | |
f4d7e40a AS |
129 | */ |
130 | u32 frameno; | |
dc503a8a | 131 | enum bpf_reg_liveness live; |
58e2af8b JK |
132 | }; |
133 | ||
134 | enum bpf_stack_slot_type { | |
135 | STACK_INVALID, /* nothing was stored in this stack slot */ | |
136 | STACK_SPILL, /* register spilled into stack */ | |
cc2b14d5 AS |
137 | STACK_MISC, /* BPF program wrote some data into this slot */ |
138 | STACK_ZERO, /* BPF program wrote constant zero */ | |
58e2af8b JK |
139 | }; |
140 | ||
141 | #define BPF_REG_SIZE 8 /* size of eBPF register in bytes */ | |
142 | ||
638f5b90 AS |
143 | struct bpf_stack_state { |
144 | struct bpf_reg_state spilled_ptr; | |
145 | u8 slot_type[BPF_REG_SIZE]; | |
146 | }; | |
147 | ||
fd978bf7 JS |
148 | struct bpf_reference_state { |
149 | /* Track each reference created with a unique id, even if the same | |
150 | * instruction creates the reference multiple times (eg, via CALL). | |
151 | */ | |
152 | int id; | |
153 | /* Instruction where the allocation of this reference occurred. This | |
154 | * is used purely to inform the user of a reference leak. | |
155 | */ | |
156 | int insn_idx; | |
157 | }; | |
158 | ||
58e2af8b JK |
159 | /* state of the program: |
160 | * type of all registers and stack info | |
161 | */ | |
f4d7e40a | 162 | struct bpf_func_state { |
58e2af8b | 163 | struct bpf_reg_state regs[MAX_BPF_REG]; |
f4d7e40a AS |
164 | /* index of call instruction that called into this func */ |
165 | int callsite; | |
166 | /* stack frame number of this function state from pov of | |
167 | * enclosing bpf_verifier_state. | |
168 | * 0 = main function, 1 = first callee. | |
169 | */ | |
170 | u32 frameno; | |
171 | /* subprog number == index within subprog_stack_depth | |
172 | * zero == main subprog | |
173 | */ | |
174 | u32 subprogno; | |
175 | ||
fd978bf7 JS |
176 | /* The following fields should be last. See copy_func_state() */ |
177 | int acquired_refs; | |
178 | struct bpf_reference_state *refs; | |
638f5b90 AS |
179 | int allocated_stack; |
180 | struct bpf_stack_state *stack; | |
58e2af8b JK |
181 | }; |
182 | ||
f4d7e40a AS |
183 | #define MAX_CALL_FRAMES 8 |
184 | struct bpf_verifier_state { | |
185 | /* call stack tracking */ | |
186 | struct bpf_func_state *frame[MAX_CALL_FRAMES]; | |
f4d7e40a | 187 | u32 curframe; |
d83525ca | 188 | u32 active_spin_lock; |
979d63d5 | 189 | bool speculative; |
f4d7e40a AS |
190 | }; |
191 | ||
f3709f69 JS |
192 | #define bpf_get_spilled_reg(slot, frame) \ |
193 | (((slot < frame->allocated_stack / BPF_REG_SIZE) && \ | |
194 | (frame->stack[slot].slot_type[0] == STACK_SPILL)) \ | |
195 | ? &frame->stack[slot].spilled_ptr : NULL) | |
196 | ||
197 | /* Iterate over 'frame', setting 'reg' to either NULL or a spilled register. */ | |
198 | #define bpf_for_each_spilled_reg(iter, frame, reg) \ | |
199 | for (iter = 0, reg = bpf_get_spilled_reg(iter, frame); \ | |
200 | iter < frame->allocated_stack / BPF_REG_SIZE; \ | |
201 | iter++, reg = bpf_get_spilled_reg(iter, frame)) | |
202 | ||
58e2af8b JK |
203 | /* linked list of verifier states used to prune search */ |
204 | struct bpf_verifier_state_list { | |
205 | struct bpf_verifier_state state; | |
206 | struct bpf_verifier_state_list *next; | |
9f4686c4 | 207 | int miss_cnt, hit_cnt; |
58e2af8b JK |
208 | }; |
209 | ||
979d63d5 DB |
210 | /* Possible states for alu_state member. */ |
211 | #define BPF_ALU_SANITIZE_SRC 1U | |
212 | #define BPF_ALU_SANITIZE_DST 2U | |
213 | #define BPF_ALU_NEG_VALUE (1U << 2) | |
d3bd7413 | 214 | #define BPF_ALU_NON_POINTER (1U << 3) |
979d63d5 DB |
215 | #define BPF_ALU_SANITIZE (BPF_ALU_SANITIZE_SRC | \ |
216 | BPF_ALU_SANITIZE_DST) | |
217 | ||
58e2af8b | 218 | struct bpf_insn_aux_data { |
81ed18ab AS |
219 | union { |
220 | enum bpf_reg_type ptr_type; /* pointer type for load/store insns */ | |
c93552c4 | 221 | unsigned long map_state; /* pointer/poison value for maps */ |
1c2a088a | 222 | s32 call_imm; /* saved imm field of call insn */ |
979d63d5 | 223 | u32 alu_limit; /* limit for add/sub register with pointer */ |
d8eca5bb DB |
224 | struct { |
225 | u32 map_index; /* index into used_maps[] */ | |
226 | u32 map_off; /* offset from value base address */ | |
227 | }; | |
81ed18ab | 228 | }; |
23994631 | 229 | int ctx_field_size; /* the ctx field size for load insn, maybe 0 */ |
af86ca4e | 230 | int sanitize_stack_off; /* stack slot to be cleared */ |
c131187d | 231 | bool seen; /* this insn was processed by the verifier */ |
979d63d5 | 232 | u8 alu_state; /* used in combination with alu_limit */ |
9e4c24e7 | 233 | unsigned int orig_idx; /* original instruction index */ |
58e2af8b JK |
234 | }; |
235 | ||
236 | #define MAX_USED_MAPS 64 /* max number of maps accessed by one eBPF program */ | |
237 | ||
a2a7d570 JK |
238 | #define BPF_VERIFIER_TMP_LOG_SIZE 1024 |
239 | ||
b9193c1b | 240 | struct bpf_verifier_log { |
e7bf8249 | 241 | u32 level; |
a2a7d570 | 242 | char kbuf[BPF_VERIFIER_TMP_LOG_SIZE]; |
e7bf8249 JK |
243 | char __user *ubuf; |
244 | u32 len_used; | |
245 | u32 len_total; | |
246 | }; | |
247 | ||
b9193c1b | 248 | static inline bool bpf_verifier_log_full(const struct bpf_verifier_log *log) |
e7bf8249 JK |
249 | { |
250 | return log->len_used >= log->len_total - 1; | |
251 | } | |
252 | ||
06ee7115 AS |
253 | #define BPF_LOG_LEVEL1 1 |
254 | #define BPF_LOG_LEVEL2 2 | |
255 | #define BPF_LOG_STATS 4 | |
256 | #define BPF_LOG_LEVEL (BPF_LOG_LEVEL1 | BPF_LOG_LEVEL2) | |
257 | #define BPF_LOG_MASK (BPF_LOG_LEVEL | BPF_LOG_STATS) | |
258 | ||
77d2e05a MKL |
259 | static inline bool bpf_verifier_log_needed(const struct bpf_verifier_log *log) |
260 | { | |
261 | return log->level && log->ubuf && !bpf_verifier_log_full(log); | |
262 | } | |
263 | ||
cc8b0b92 AS |
264 | #define BPF_MAX_SUBPROGS 256 |
265 | ||
9c8105bd JW |
266 | struct bpf_subprog_info { |
267 | u32 start; /* insn idx of function entry point */ | |
c454a46b | 268 | u32 linfo_idx; /* The idx to the main_prog->aux->linfo */ |
9c8105bd JW |
269 | u16 stack_depth; /* max. stack depth used by this function */ |
270 | }; | |
271 | ||
58e2af8b JK |
272 | /* single container for all structs |
273 | * one verifier_env per bpf_check() call | |
274 | */ | |
275 | struct bpf_verifier_env { | |
c08435ec DB |
276 | u32 insn_idx; |
277 | u32 prev_insn_idx; | |
58e2af8b | 278 | struct bpf_prog *prog; /* eBPF program being verified */ |
00176a34 | 279 | const struct bpf_verifier_ops *ops; |
58e2af8b JK |
280 | struct bpf_verifier_stack_elem *head; /* stack of verifier states to be processed */ |
281 | int stack_size; /* number of states to be processed */ | |
e07b98d9 | 282 | bool strict_alignment; /* perform strict pointer alignment checks */ |
638f5b90 | 283 | struct bpf_verifier_state *cur_state; /* current verifier state */ |
58e2af8b | 284 | struct bpf_verifier_state_list **explored_states; /* search pruning optimization */ |
9f4686c4 | 285 | struct bpf_verifier_state_list *free_list; |
58e2af8b JK |
286 | struct bpf_map *used_maps[MAX_USED_MAPS]; /* array of map's used by eBPF program */ |
287 | u32 used_map_cnt; /* number of used maps */ | |
288 | u32 id_gen; /* used to generate unique reg IDs */ | |
289 | bool allow_ptr_leaks; | |
290 | bool seen_direct_write; | |
291 | struct bpf_insn_aux_data *insn_aux_data; /* array of per-insn state */ | |
d9762e84 | 292 | const struct bpf_line_info *prev_linfo; |
b9193c1b | 293 | struct bpf_verifier_log log; |
9c8105bd | 294 | struct bpf_subprog_info subprog_info[BPF_MAX_SUBPROGS + 1]; |
7df737e9 AS |
295 | struct { |
296 | int *insn_state; | |
297 | int *insn_stack; | |
298 | int cur_stack; | |
299 | } cfg; | |
cc8b0b92 | 300 | u32 subprog_cnt; |
06ee7115 AS |
301 | /* number of instructions analyzed by the verifier */ |
302 | u32 insn_processed; | |
303 | /* total verification time */ | |
304 | u64 verification_time; | |
305 | /* maximum number of verifier states kept in 'branching' instructions */ | |
306 | u32 max_states_per_insn; | |
307 | /* total number of allocated verifier states */ | |
308 | u32 total_states; | |
309 | /* some states are freed during program analysis. | |
310 | * this is peak number of states. this number dominates kernel | |
311 | * memory consumption during verification | |
312 | */ | |
313 | u32 peak_states; | |
314 | /* longest register parentage chain walked for liveness marking */ | |
315 | u32 longest_mark_read_walk; | |
58e2af8b JK |
316 | }; |
317 | ||
be2d04d1 MM |
318 | __printf(2, 0) void bpf_verifier_vlog(struct bpf_verifier_log *log, |
319 | const char *fmt, va_list args); | |
430e68d1 QM |
320 | __printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env, |
321 | const char *fmt, ...); | |
322 | ||
fd978bf7 | 323 | static inline struct bpf_func_state *cur_func(struct bpf_verifier_env *env) |
638f5b90 | 324 | { |
f4d7e40a AS |
325 | struct bpf_verifier_state *cur = env->cur_state; |
326 | ||
fd978bf7 JS |
327 | return cur->frame[cur->curframe]; |
328 | } | |
329 | ||
330 | static inline struct bpf_reg_state *cur_regs(struct bpf_verifier_env *env) | |
331 | { | |
332 | return cur_func(env)->regs; | |
638f5b90 AS |
333 | } |
334 | ||
a40a2632 | 335 | int bpf_prog_offload_verifier_prep(struct bpf_prog *prog); |
cae1927c JK |
336 | int bpf_prog_offload_verify_insn(struct bpf_verifier_env *env, |
337 | int insn_idx, int prev_insn_idx); | |
c941ce9c | 338 | int bpf_prog_offload_finalize(struct bpf_verifier_env *env); |
08ca90af JK |
339 | void |
340 | bpf_prog_offload_replace_insn(struct bpf_verifier_env *env, u32 off, | |
341 | struct bpf_insn *insn); | |
342 | void | |
343 | bpf_prog_offload_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt); | |
ab3f0063 | 344 | |
58e2af8b | 345 | #endif /* _LINUX_BPF_VERIFIER_H */ |