]>
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 */ | |
22dc4a0f | 8 | #include <linux/btf.h> /* for struct btf and btf_id() */ |
58e2af8b | 9 | #include <linux/filter.h> /* for MAX_BPF_STACK */ |
f1174f77 | 10 | #include <linux/tnum.h> |
58e2af8b | 11 | |
b03c9f9f EC |
12 | /* Maximum variable offset umax_value permitted when resolving memory accesses. |
13 | * In practice this is far bigger than any realistic pointer offset; this limit | |
14 | * ensures that umax_value + (int)off + (int)size cannot overflow a u64. | |
15 | */ | |
bb7f0f98 | 16 | #define BPF_MAX_VAR_OFF (1 << 29) |
b03c9f9f EC |
17 | /* Maximum variable size permitted for ARG_CONST_SIZE[_OR_ZERO]. This ensures |
18 | * that converting umax_value to int cannot overflow. | |
19 | */ | |
bb7f0f98 | 20 | #define BPF_MAX_VAR_SIZ (1 << 29) |
48461135 | 21 | |
8e9cd9ce EC |
22 | /* Liveness marks, used for registers and spilled-regs (in stack slots). |
23 | * Read marks propagate upwards until they find a write mark; they record that | |
24 | * "one of this state's descendants read this reg" (and therefore the reg is | |
25 | * relevant for states_equal() checks). | |
26 | * Write marks collect downwards and do not propagate; they record that "the | |
27 | * straight-line code that reached this state (from its parent) wrote this reg" | |
28 | * (and therefore that reads propagated from this state or its descendants | |
29 | * should not propagate to its parent). | |
30 | * A state with a write mark can receive read marks; it just won't propagate | |
31 | * them to its parent, since the write mark is a property, not of the state, | |
32 | * but of the link between it and its parent. See mark_reg_read() and | |
33 | * mark_stack_slot_read() in kernel/bpf/verifier.c. | |
34 | */ | |
dc503a8a EC |
35 | enum bpf_reg_liveness { |
36 | REG_LIVE_NONE = 0, /* reg hasn't been read or written this branch */ | |
5327ed3d JW |
37 | REG_LIVE_READ32 = 0x1, /* reg was read, so we're sensitive to initial value */ |
38 | REG_LIVE_READ64 = 0x2, /* likewise, but full 64-bit content matters */ | |
39 | REG_LIVE_READ = REG_LIVE_READ32 | REG_LIVE_READ64, | |
40 | REG_LIVE_WRITTEN = 0x4, /* reg was written first, screening off later reads */ | |
41 | REG_LIVE_DONE = 0x8, /* liveness won't be updating this register anymore */ | |
dc503a8a EC |
42 | }; |
43 | ||
58e2af8b | 44 | struct bpf_reg_state { |
679c782d | 45 | /* Ordering of fields matters. See states_equal() */ |
58e2af8b | 46 | enum bpf_reg_type type; |
22dc4a0f AN |
47 | /* Fixed part of pointer offset, pointer types only */ |
48 | s32 off; | |
58e2af8b | 49 | union { |
f1174f77 | 50 | /* valid when type == PTR_TO_PACKET */ |
6d94e741 | 51 | int range; |
58e2af8b JK |
52 | |
53 | /* valid when type == CONST_PTR_TO_MAP | PTR_TO_MAP_VALUE | | |
54 | * PTR_TO_MAP_VALUE_OR_NULL | |
55 | */ | |
56 | struct bpf_map *map_ptr; | |
0962590e | 57 | |
22dc4a0f AN |
58 | /* for PTR_TO_BTF_ID */ |
59 | struct { | |
60 | struct btf *btf; | |
61 | u32 btf_id; | |
62 | }; | |
9e15db66 | 63 | |
457f4436 AN |
64 | u32 mem_size; /* for PTR_TO_MEM | PTR_TO_MEM_OR_NULL */ |
65 | ||
0962590e | 66 | /* Max size from any of the above. */ |
22dc4a0f AN |
67 | struct { |
68 | unsigned long raw1; | |
69 | unsigned long raw2; | |
70 | } raw; | |
69c087ba YS |
71 | |
72 | u32 subprogno; /* for PTR_TO_FUNC */ | |
58e2af8b | 73 | }; |
f1174f77 EC |
74 | /* For PTR_TO_PACKET, used to find other pointers with the same variable |
75 | * offset, so they can share range knowledge. | |
76 | * For PTR_TO_MAP_VALUE_OR_NULL this is used to share which map value we | |
77 | * came from, when one is tested for != NULL. | |
457f4436 AN |
78 | * For PTR_TO_MEM_OR_NULL this is used to identify memory allocation |
79 | * for the purpose of tracking that it's freed. | |
c64b7983 JS |
80 | * For PTR_TO_SOCKET this is used to share which pointers retain the |
81 | * same reference to the socket, to determine proper reference freeing. | |
f1174f77 | 82 | */ |
d2a4dd37 | 83 | u32 id; |
1b986589 MKL |
84 | /* PTR_TO_SOCKET and PTR_TO_TCP_SOCK could be a ptr returned |
85 | * from a pointer-cast helper, bpf_sk_fullsock() and | |
86 | * bpf_tcp_sock(). | |
87 | * | |
88 | * Consider the following where "sk" is a reference counted | |
89 | * pointer returned from "sk = bpf_sk_lookup_tcp();": | |
90 | * | |
91 | * 1: sk = bpf_sk_lookup_tcp(); | |
92 | * 2: if (!sk) { return 0; } | |
93 | * 3: fullsock = bpf_sk_fullsock(sk); | |
94 | * 4: if (!fullsock) { bpf_sk_release(sk); return 0; } | |
95 | * 5: tp = bpf_tcp_sock(fullsock); | |
96 | * 6: if (!tp) { bpf_sk_release(sk); return 0; } | |
97 | * 7: bpf_sk_release(sk); | |
98 | * 8: snd_cwnd = tp->snd_cwnd; // verifier will complain | |
99 | * | |
100 | * After bpf_sk_release(sk) at line 7, both "fullsock" ptr and | |
101 | * "tp" ptr should be invalidated also. In order to do that, | |
102 | * the reg holding "fullsock" and "sk" need to remember | |
103 | * the original refcounted ptr id (i.e. sk_reg->id) in ref_obj_id | |
104 | * such that the verifier can reset all regs which have | |
105 | * ref_obj_id matching the sk_reg->id. | |
106 | * | |
107 | * sk_reg->ref_obj_id is set to sk_reg->id at line 1. | |
108 | * sk_reg->id will stay as NULL-marking purpose only. | |
109 | * After NULL-marking is done, sk_reg->id can be reset to 0. | |
110 | * | |
111 | * After "fullsock = bpf_sk_fullsock(sk);" at line 3, | |
112 | * fullsock_reg->ref_obj_id is set to sk_reg->ref_obj_id. | |
113 | * | |
114 | * After "tp = bpf_tcp_sock(fullsock);" at line 5, | |
115 | * tp_reg->ref_obj_id is set to fullsock_reg->ref_obj_id | |
116 | * which is the same as sk_reg->ref_obj_id. | |
117 | * | |
118 | * From the verifier perspective, if sk, fullsock and tp | |
119 | * are not NULL, they are the same ptr with different | |
120 | * reg->type. In particular, bpf_sk_release(tp) is also | |
121 | * allowed and has the same effect as bpf_sk_release(sk). | |
122 | */ | |
123 | u32 ref_obj_id; | |
f1174f77 EC |
124 | /* For scalar types (SCALAR_VALUE), this represents our knowledge of |
125 | * the actual value. | |
126 | * For pointer types, this represents the variable part of the offset | |
127 | * from the pointed-to object, and is shared with all bpf_reg_states | |
128 | * with the same id as us. | |
129 | */ | |
130 | struct tnum var_off; | |
d2a4dd37 | 131 | /* Used to determine if any memory access using this register will |
f1174f77 EC |
132 | * result in a bad access. |
133 | * These refer to the same value as var_off, not necessarily the actual | |
134 | * contents of the register. | |
d2a4dd37 | 135 | */ |
b03c9f9f EC |
136 | s64 smin_value; /* minimum possible (s64)value */ |
137 | s64 smax_value; /* maximum possible (s64)value */ | |
138 | u64 umin_value; /* minimum possible (u64)value */ | |
139 | u64 umax_value; /* maximum possible (u64)value */ | |
3f50f132 JF |
140 | s32 s32_min_value; /* minimum possible (s32)value */ |
141 | s32 s32_max_value; /* maximum possible (s32)value */ | |
142 | u32 u32_min_value; /* minimum possible (u32)value */ | |
143 | u32 u32_max_value; /* maximum possible (u32)value */ | |
679c782d EC |
144 | /* parentage chain for liveness checking */ |
145 | struct bpf_reg_state *parent; | |
f4d7e40a AS |
146 | /* Inside the callee two registers can be both PTR_TO_STACK like |
147 | * R1=fp-8 and R2=fp-8, but one of them points to this function stack | |
148 | * while another to the caller's stack. To differentiate them 'frameno' | |
149 | * is used which is an index in bpf_verifier_state->frame[] array | |
150 | * pointing to bpf_func_state. | |
f4d7e40a AS |
151 | */ |
152 | u32 frameno; | |
5327ed3d JW |
153 | /* Tracks subreg definition. The stored value is the insn_idx of the |
154 | * writing insn. This is safe because subreg_def is used before any insn | |
155 | * patching which only happens after main verification finished. | |
156 | */ | |
157 | s32 subreg_def; | |
dc503a8a | 158 | enum bpf_reg_liveness live; |
b5dc0163 AS |
159 | /* if (!precise && SCALAR_VALUE) min/max/tnum don't affect safety */ |
160 | bool precise; | |
58e2af8b JK |
161 | }; |
162 | ||
163 | enum bpf_stack_slot_type { | |
164 | STACK_INVALID, /* nothing was stored in this stack slot */ | |
165 | STACK_SPILL, /* register spilled into stack */ | |
cc2b14d5 AS |
166 | STACK_MISC, /* BPF program wrote some data into this slot */ |
167 | STACK_ZERO, /* BPF program wrote constant zero */ | |
58e2af8b JK |
168 | }; |
169 | ||
170 | #define BPF_REG_SIZE 8 /* size of eBPF register in bytes */ | |
171 | ||
638f5b90 AS |
172 | struct bpf_stack_state { |
173 | struct bpf_reg_state spilled_ptr; | |
174 | u8 slot_type[BPF_REG_SIZE]; | |
175 | }; | |
176 | ||
fd978bf7 JS |
177 | struct bpf_reference_state { |
178 | /* Track each reference created with a unique id, even if the same | |
179 | * instruction creates the reference multiple times (eg, via CALL). | |
180 | */ | |
181 | int id; | |
182 | /* Instruction where the allocation of this reference occurred. This | |
183 | * is used purely to inform the user of a reference leak. | |
184 | */ | |
185 | int insn_idx; | |
186 | }; | |
187 | ||
58e2af8b JK |
188 | /* state of the program: |
189 | * type of all registers and stack info | |
190 | */ | |
f4d7e40a | 191 | struct bpf_func_state { |
58e2af8b | 192 | struct bpf_reg_state regs[MAX_BPF_REG]; |
f4d7e40a AS |
193 | /* index of call instruction that called into this func */ |
194 | int callsite; | |
195 | /* stack frame number of this function state from pov of | |
196 | * enclosing bpf_verifier_state. | |
197 | * 0 = main function, 1 = first callee. | |
198 | */ | |
199 | u32 frameno; | |
01f810ac | 200 | /* subprog number == index within subprog_info |
f4d7e40a AS |
201 | * zero == main subprog |
202 | */ | |
203 | u32 subprogno; | |
204 | ||
fd978bf7 JS |
205 | /* The following fields should be last. See copy_func_state() */ |
206 | int acquired_refs; | |
207 | struct bpf_reference_state *refs; | |
638f5b90 | 208 | int allocated_stack; |
69c087ba | 209 | bool in_callback_fn; |
638f5b90 | 210 | struct bpf_stack_state *stack; |
58e2af8b JK |
211 | }; |
212 | ||
b5dc0163 AS |
213 | struct bpf_idx_pair { |
214 | u32 prev_idx; | |
215 | u32 idx; | |
216 | }; | |
217 | ||
f4d7e40a AS |
218 | #define MAX_CALL_FRAMES 8 |
219 | struct bpf_verifier_state { | |
220 | /* call stack tracking */ | |
221 | struct bpf_func_state *frame[MAX_CALL_FRAMES]; | |
2589726d AS |
222 | struct bpf_verifier_state *parent; |
223 | /* | |
224 | * 'branches' field is the number of branches left to explore: | |
225 | * 0 - all possible paths from this state reached bpf_exit or | |
226 | * were safely pruned | |
227 | * 1 - at least one path is being explored. | |
228 | * This state hasn't reached bpf_exit | |
229 | * 2 - at least two paths are being explored. | |
230 | * This state is an immediate parent of two children. | |
231 | * One is fallthrough branch with branches==1 and another | |
232 | * state is pushed into stack (to be explored later) also with | |
233 | * branches==1. The parent of this state has branches==1. | |
234 | * The verifier state tree connected via 'parent' pointer looks like: | |
235 | * 1 | |
236 | * 1 | |
237 | * 2 -> 1 (first 'if' pushed into stack) | |
238 | * 1 | |
239 | * 2 -> 1 (second 'if' pushed into stack) | |
240 | * 1 | |
241 | * 1 | |
242 | * 1 bpf_exit. | |
243 | * | |
244 | * Once do_check() reaches bpf_exit, it calls update_branch_counts() | |
245 | * and the verifier state tree will look: | |
246 | * 1 | |
247 | * 1 | |
248 | * 2 -> 1 (first 'if' pushed into stack) | |
249 | * 1 | |
250 | * 1 -> 1 (second 'if' pushed into stack) | |
251 | * 0 | |
252 | * 0 | |
253 | * 0 bpf_exit. | |
254 | * After pop_stack() the do_check() will resume at second 'if'. | |
255 | * | |
256 | * If is_state_visited() sees a state with branches > 0 it means | |
257 | * there is a loop. If such state is exactly equal to the current state | |
258 | * it's an infinite loop. Note states_equal() checks for states | |
259 | * equvalency, so two states being 'states_equal' does not mean | |
260 | * infinite loop. The exact comparison is provided by | |
261 | * states_maybe_looping() function. It's a stronger pre-check and | |
262 | * much faster than states_equal(). | |
263 | * | |
264 | * This algorithm may not find all possible infinite loops or | |
265 | * loop iteration count may be too high. | |
266 | * In such cases BPF_COMPLEXITY_LIMIT_INSNS limit kicks in. | |
267 | */ | |
268 | u32 branches; | |
dc2a4ebc | 269 | u32 insn_idx; |
f4d7e40a | 270 | u32 curframe; |
d83525ca | 271 | u32 active_spin_lock; |
979d63d5 | 272 | bool speculative; |
b5dc0163 AS |
273 | |
274 | /* first and last insn idx of this verifier state */ | |
275 | u32 first_insn_idx; | |
276 | u32 last_insn_idx; | |
277 | /* jmp history recorded from first to last. | |
278 | * backtracking is using it to go from last to first. | |
279 | * For most states jmp_history_cnt is [0-3]. | |
280 | * For loops can go up to ~40. | |
281 | */ | |
282 | struct bpf_idx_pair *jmp_history; | |
283 | u32 jmp_history_cnt; | |
f4d7e40a AS |
284 | }; |
285 | ||
f3709f69 JS |
286 | #define bpf_get_spilled_reg(slot, frame) \ |
287 | (((slot < frame->allocated_stack / BPF_REG_SIZE) && \ | |
288 | (frame->stack[slot].slot_type[0] == STACK_SPILL)) \ | |
289 | ? &frame->stack[slot].spilled_ptr : NULL) | |
290 | ||
291 | /* Iterate over 'frame', setting 'reg' to either NULL or a spilled register. */ | |
292 | #define bpf_for_each_spilled_reg(iter, frame, reg) \ | |
293 | for (iter = 0, reg = bpf_get_spilled_reg(iter, frame); \ | |
294 | iter < frame->allocated_stack / BPF_REG_SIZE; \ | |
295 | iter++, reg = bpf_get_spilled_reg(iter, frame)) | |
296 | ||
58e2af8b JK |
297 | /* linked list of verifier states used to prune search */ |
298 | struct bpf_verifier_state_list { | |
299 | struct bpf_verifier_state state; | |
300 | struct bpf_verifier_state_list *next; | |
9f4686c4 | 301 | int miss_cnt, hit_cnt; |
58e2af8b JK |
302 | }; |
303 | ||
979d63d5 | 304 | /* Possible states for alu_state member. */ |
801c6058 DB |
305 | #define BPF_ALU_SANITIZE_SRC (1U << 0) |
306 | #define BPF_ALU_SANITIZE_DST (1U << 1) | |
979d63d5 | 307 | #define BPF_ALU_NEG_VALUE (1U << 2) |
d3bd7413 | 308 | #define BPF_ALU_NON_POINTER (1U << 3) |
801c6058 | 309 | #define BPF_ALU_IMMEDIATE (1U << 4) |
979d63d5 DB |
310 | #define BPF_ALU_SANITIZE (BPF_ALU_SANITIZE_SRC | \ |
311 | BPF_ALU_SANITIZE_DST) | |
312 | ||
58e2af8b | 313 | struct bpf_insn_aux_data { |
81ed18ab AS |
314 | union { |
315 | enum bpf_reg_type ptr_type; /* pointer type for load/store insns */ | |
d2e4c1e6 | 316 | unsigned long map_ptr_state; /* pointer/poison value for maps */ |
1c2a088a | 317 | s32 call_imm; /* saved imm field of call insn */ |
979d63d5 | 318 | u32 alu_limit; /* limit for add/sub register with pointer */ |
d8eca5bb DB |
319 | struct { |
320 | u32 map_index; /* index into used_maps[] */ | |
321 | u32 map_off; /* offset from value base address */ | |
322 | }; | |
4976b718 HL |
323 | struct { |
324 | enum bpf_reg_type reg_type; /* type of pseudo_btf_id */ | |
325 | union { | |
22dc4a0f AN |
326 | struct { |
327 | struct btf *btf; | |
328 | u32 btf_id; /* btf_id for struct typed var */ | |
329 | }; | |
4976b718 HL |
330 | u32 mem_size; /* mem_size for non-struct typed var */ |
331 | }; | |
332 | } btf_var; | |
81ed18ab | 333 | }; |
d2e4c1e6 | 334 | u64 map_key_state; /* constant (32 bit) key tracking for maps */ |
23994631 | 335 | int ctx_field_size; /* the ctx field size for load insn, maybe 0 */ |
af86ca4e | 336 | int sanitize_stack_off; /* stack slot to be cleared */ |
51c39bb1 | 337 | u32 seen; /* this insn was processed by the verifier at env->pass_cnt */ |
5327ed3d | 338 | bool zext_dst; /* this insn zero extends dst reg */ |
979d63d5 | 339 | u8 alu_state; /* used in combination with alu_limit */ |
51c39bb1 AS |
340 | |
341 | /* below fields are initialized once */ | |
9e4c24e7 | 342 | unsigned int orig_idx; /* original instruction index */ |
51c39bb1 | 343 | bool prune_point; |
58e2af8b JK |
344 | }; |
345 | ||
346 | #define MAX_USED_MAPS 64 /* max number of maps accessed by one eBPF program */ | |
541c3bad | 347 | #define MAX_USED_BTFS 64 /* max number of BTFs accessed by one BPF program */ |
58e2af8b | 348 | |
a2a7d570 JK |
349 | #define BPF_VERIFIER_TMP_LOG_SIZE 1024 |
350 | ||
b9193c1b | 351 | struct bpf_verifier_log { |
e7bf8249 | 352 | u32 level; |
a2a7d570 | 353 | char kbuf[BPF_VERIFIER_TMP_LOG_SIZE]; |
e7bf8249 JK |
354 | char __user *ubuf; |
355 | u32 len_used; | |
356 | u32 len_total; | |
357 | }; | |
358 | ||
b9193c1b | 359 | static inline bool bpf_verifier_log_full(const struct bpf_verifier_log *log) |
e7bf8249 JK |
360 | { |
361 | return log->len_used >= log->len_total - 1; | |
362 | } | |
363 | ||
06ee7115 AS |
364 | #define BPF_LOG_LEVEL1 1 |
365 | #define BPF_LOG_LEVEL2 2 | |
366 | #define BPF_LOG_STATS 4 | |
367 | #define BPF_LOG_LEVEL (BPF_LOG_LEVEL1 | BPF_LOG_LEVEL2) | |
368 | #define BPF_LOG_MASK (BPF_LOG_LEVEL | BPF_LOG_STATS) | |
8580ac94 | 369 | #define BPF_LOG_KERNEL (BPF_LOG_MASK + 1) /* kernel internal flag */ |
06ee7115 | 370 | |
77d2e05a MKL |
371 | static inline bool bpf_verifier_log_needed(const struct bpf_verifier_log *log) |
372 | { | |
efc68158 THJ |
373 | return log && |
374 | ((log->level && log->ubuf && !bpf_verifier_log_full(log)) || | |
375 | log->level == BPF_LOG_KERNEL); | |
77d2e05a MKL |
376 | } |
377 | ||
cc8b0b92 AS |
378 | #define BPF_MAX_SUBPROGS 256 |
379 | ||
9c8105bd | 380 | struct bpf_subprog_info { |
8c1b6e69 | 381 | /* 'start' has to be the first field otherwise find_subprog() won't work */ |
9c8105bd | 382 | u32 start; /* insn idx of function entry point */ |
c454a46b | 383 | u32 linfo_idx; /* The idx to the main_prog->aux->linfo */ |
9c8105bd | 384 | u16 stack_depth; /* max. stack depth used by this function */ |
7f6e4312 | 385 | bool has_tail_call; |
ebf7d1f5 | 386 | bool tail_call_reachable; |
09b28d76 | 387 | bool has_ld_abs; |
9c8105bd JW |
388 | }; |
389 | ||
58e2af8b JK |
390 | /* single container for all structs |
391 | * one verifier_env per bpf_check() call | |
392 | */ | |
393 | struct bpf_verifier_env { | |
c08435ec DB |
394 | u32 insn_idx; |
395 | u32 prev_insn_idx; | |
58e2af8b | 396 | struct bpf_prog *prog; /* eBPF program being verified */ |
00176a34 | 397 | const struct bpf_verifier_ops *ops; |
58e2af8b JK |
398 | struct bpf_verifier_stack_elem *head; /* stack of verifier states to be processed */ |
399 | int stack_size; /* number of states to be processed */ | |
e07b98d9 | 400 | bool strict_alignment; /* perform strict pointer alignment checks */ |
10d274e8 | 401 | bool test_state_freq; /* test verifier with different pruning frequency */ |
638f5b90 | 402 | struct bpf_verifier_state *cur_state; /* current verifier state */ |
58e2af8b | 403 | struct bpf_verifier_state_list **explored_states; /* search pruning optimization */ |
9f4686c4 | 404 | struct bpf_verifier_state_list *free_list; |
58e2af8b | 405 | struct bpf_map *used_maps[MAX_USED_MAPS]; /* array of map's used by eBPF program */ |
541c3bad | 406 | struct btf_mod_pair used_btfs[MAX_USED_BTFS]; /* array of BTF's used by BPF program */ |
58e2af8b | 407 | u32 used_map_cnt; /* number of used maps */ |
541c3bad | 408 | u32 used_btf_cnt; /* number of used BTF objects */ |
58e2af8b JK |
409 | u32 id_gen; /* used to generate unique reg IDs */ |
410 | bool allow_ptr_leaks; | |
01f810ac | 411 | bool allow_uninit_stack; |
41c48f3a | 412 | bool allow_ptr_to_map_access; |
2c78ee89 AS |
413 | bool bpf_capable; |
414 | bool bypass_spec_v1; | |
415 | bool bypass_spec_v4; | |
58e2af8b JK |
416 | bool seen_direct_write; |
417 | struct bpf_insn_aux_data *insn_aux_data; /* array of per-insn state */ | |
d9762e84 | 418 | const struct bpf_line_info *prev_linfo; |
b9193c1b | 419 | struct bpf_verifier_log log; |
9c8105bd | 420 | struct bpf_subprog_info subprog_info[BPF_MAX_SUBPROGS + 1]; |
7df737e9 AS |
421 | struct { |
422 | int *insn_state; | |
423 | int *insn_stack; | |
424 | int cur_stack; | |
425 | } cfg; | |
51c39bb1 | 426 | u32 pass_cnt; /* number of times do_check() was called */ |
cc8b0b92 | 427 | u32 subprog_cnt; |
06ee7115 | 428 | /* number of instructions analyzed by the verifier */ |
2589726d AS |
429 | u32 prev_insn_processed, insn_processed; |
430 | /* number of jmps, calls, exits analyzed so far */ | |
431 | u32 prev_jmps_processed, jmps_processed; | |
06ee7115 AS |
432 | /* total verification time */ |
433 | u64 verification_time; | |
434 | /* maximum number of verifier states kept in 'branching' instructions */ | |
435 | u32 max_states_per_insn; | |
436 | /* total number of allocated verifier states */ | |
437 | u32 total_states; | |
438 | /* some states are freed during program analysis. | |
439 | * this is peak number of states. this number dominates kernel | |
440 | * memory consumption during verification | |
441 | */ | |
442 | u32 peak_states; | |
443 | /* longest register parentage chain walked for liveness marking */ | |
444 | u32 longest_mark_read_walk; | |
58e2af8b JK |
445 | }; |
446 | ||
be2d04d1 MM |
447 | __printf(2, 0) void bpf_verifier_vlog(struct bpf_verifier_log *log, |
448 | const char *fmt, va_list args); | |
430e68d1 QM |
449 | __printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env, |
450 | const char *fmt, ...); | |
9e15db66 AS |
451 | __printf(2, 3) void bpf_log(struct bpf_verifier_log *log, |
452 | const char *fmt, ...); | |
430e68d1 | 453 | |
fd978bf7 | 454 | static inline struct bpf_func_state *cur_func(struct bpf_verifier_env *env) |
638f5b90 | 455 | { |
f4d7e40a AS |
456 | struct bpf_verifier_state *cur = env->cur_state; |
457 | ||
fd978bf7 JS |
458 | return cur->frame[cur->curframe]; |
459 | } | |
460 | ||
461 | static inline struct bpf_reg_state *cur_regs(struct bpf_verifier_env *env) | |
462 | { | |
463 | return cur_func(env)->regs; | |
638f5b90 AS |
464 | } |
465 | ||
a40a2632 | 466 | int bpf_prog_offload_verifier_prep(struct bpf_prog *prog); |
cae1927c JK |
467 | int bpf_prog_offload_verify_insn(struct bpf_verifier_env *env, |
468 | int insn_idx, int prev_insn_idx); | |
c941ce9c | 469 | int bpf_prog_offload_finalize(struct bpf_verifier_env *env); |
08ca90af JK |
470 | void |
471 | bpf_prog_offload_replace_insn(struct bpf_verifier_env *env, u32 off, | |
472 | struct bpf_insn *insn); | |
473 | void | |
474 | bpf_prog_offload_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt); | |
ab3f0063 | 475 | |
51c39bb1 AS |
476 | int check_ctx_reg(struct bpf_verifier_env *env, |
477 | const struct bpf_reg_state *reg, int regno); | |
e5069b9c DB |
478 | int check_mem_reg(struct bpf_verifier_env *env, struct bpf_reg_state *reg, |
479 | u32 regno, u32 mem_size); | |
51c39bb1 | 480 | |
f7b12b6f THJ |
481 | /* this lives here instead of in bpf.h because it needs to dereference tgt_prog */ |
482 | static inline u64 bpf_trampoline_compute_key(const struct bpf_prog *tgt_prog, | |
22dc4a0f | 483 | struct btf *btf, u32 btf_id) |
f7b12b6f | 484 | { |
22dc4a0f AN |
485 | if (tgt_prog) |
486 | return ((u64)tgt_prog->aux->id << 32) | btf_id; | |
487 | else | |
488 | return ((u64)btf_obj_id(btf) << 32) | 0x80000000 | btf_id; | |
f7b12b6f THJ |
489 | } |
490 | ||
441e8c66 THJ |
491 | /* unpack the IDs from the key as constructed above */ |
492 | static inline void bpf_trampoline_unpack_key(u64 key, u32 *obj_id, u32 *btf_id) | |
493 | { | |
494 | if (obj_id) | |
495 | *obj_id = key >> 32; | |
496 | if (btf_id) | |
497 | *btf_id = key & 0x7FFFFFFF; | |
498 | } | |
499 | ||
f7b12b6f THJ |
500 | int bpf_check_attach_target(struct bpf_verifier_log *log, |
501 | const struct bpf_prog *prog, | |
502 | const struct bpf_prog *tgt_prog, | |
503 | u32 btf_id, | |
504 | struct bpf_attach_target_info *tgt_info); | |
505 | ||
58e2af8b | 506 | #endif /* _LINUX_BPF_VERIFIER_H */ |