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) |
d9439c21 AN |
21 | /* size of tmp_str_buf in bpf_verifier. |
22 | * we need at least 306 bytes to fit full stack mask representation | |
23 | * (in the "-8,-16,...,-512" form) | |
24 | */ | |
25 | #define TMP_STR_BUF_LEN 320 | |
48461135 | 26 | |
8e9cd9ce EC |
27 | /* Liveness marks, used for registers and spilled-regs (in stack slots). |
28 | * Read marks propagate upwards until they find a write mark; they record that | |
29 | * "one of this state's descendants read this reg" (and therefore the reg is | |
30 | * relevant for states_equal() checks). | |
31 | * Write marks collect downwards and do not propagate; they record that "the | |
32 | * straight-line code that reached this state (from its parent) wrote this reg" | |
33 | * (and therefore that reads propagated from this state or its descendants | |
34 | * should not propagate to its parent). | |
35 | * A state with a write mark can receive read marks; it just won't propagate | |
36 | * them to its parent, since the write mark is a property, not of the state, | |
37 | * but of the link between it and its parent. See mark_reg_read() and | |
38 | * mark_stack_slot_read() in kernel/bpf/verifier.c. | |
39 | */ | |
dc503a8a EC |
40 | enum bpf_reg_liveness { |
41 | REG_LIVE_NONE = 0, /* reg hasn't been read or written this branch */ | |
5327ed3d JW |
42 | REG_LIVE_READ32 = 0x1, /* reg was read, so we're sensitive to initial value */ |
43 | REG_LIVE_READ64 = 0x2, /* likewise, but full 64-bit content matters */ | |
44 | REG_LIVE_READ = REG_LIVE_READ32 | REG_LIVE_READ64, | |
45 | REG_LIVE_WRITTEN = 0x4, /* reg was written first, screening off later reads */ | |
46 | REG_LIVE_DONE = 0x8, /* liveness won't be updating this register anymore */ | |
dc503a8a EC |
47 | }; |
48 | ||
6a3cd331 DM |
49 | /* For every reg representing a map value or allocated object pointer, |
50 | * we consider the tuple of (ptr, id) for them to be unique in verifier | |
51 | * context and conside them to not alias each other for the purposes of | |
52 | * tracking lock state. | |
53 | */ | |
54 | struct bpf_active_lock { | |
55 | /* This can either be reg->map_ptr or reg->btf. If ptr is NULL, | |
56 | * there's no active lock held, and other fields have no | |
57 | * meaning. If non-NULL, it indicates that a lock is held and | |
58 | * id member has the reg->id of the register which can be >= 0. | |
59 | */ | |
60 | void *ptr; | |
61 | /* This will be reg->id */ | |
62 | u32 id; | |
63 | }; | |
64 | ||
215bf496 AN |
65 | #define ITER_PREFIX "bpf_iter_" |
66 | ||
06accc87 AN |
67 | enum bpf_iter_state { |
68 | BPF_ITER_STATE_INVALID, /* for non-first slot */ | |
69 | BPF_ITER_STATE_ACTIVE, | |
70 | BPF_ITER_STATE_DRAINED, | |
71 | }; | |
72 | ||
58e2af8b | 73 | struct bpf_reg_state { |
679c782d | 74 | /* Ordering of fields matters. See states_equal() */ |
58e2af8b | 75 | enum bpf_reg_type type; |
22dc4a0f AN |
76 | /* Fixed part of pointer offset, pointer types only */ |
77 | s32 off; | |
58e2af8b | 78 | union { |
f1174f77 | 79 | /* valid when type == PTR_TO_PACKET */ |
6d94e741 | 80 | int range; |
58e2af8b JK |
81 | |
82 | /* valid when type == CONST_PTR_TO_MAP | PTR_TO_MAP_VALUE | | |
83 | * PTR_TO_MAP_VALUE_OR_NULL | |
84 | */ | |
3e8ce298 AS |
85 | struct { |
86 | struct bpf_map *map_ptr; | |
87 | /* To distinguish map lookups from outer map | |
88 | * the map_uid is non-zero for registers | |
89 | * pointing to inner maps. | |
90 | */ | |
91 | u32 map_uid; | |
92 | }; | |
0962590e | 93 | |
22dc4a0f AN |
94 | /* for PTR_TO_BTF_ID */ |
95 | struct { | |
96 | struct btf *btf; | |
97 | u32 btf_id; | |
98 | }; | |
9e15db66 | 99 | |
f8064ab9 KKD |
100 | struct { /* for PTR_TO_MEM | PTR_TO_MEM_OR_NULL */ |
101 | u32 mem_size; | |
102 | u32 dynptr_id; /* for dynptr slices */ | |
103 | }; | |
457f4436 | 104 | |
97e03f52 JK |
105 | /* For dynptr stack slots */ |
106 | struct { | |
107 | enum bpf_dynptr_type type; | |
108 | /* A dynptr is 16 bytes so it takes up 2 stack slots. | |
109 | * We need to track which slot is the first slot | |
110 | * to protect against cases where the user may try to | |
111 | * pass in an address starting at the second slot of the | |
112 | * dynptr. | |
113 | */ | |
114 | bool first_slot; | |
115 | } dynptr; | |
116 | ||
06accc87 AN |
117 | /* For bpf_iter stack slots */ |
118 | struct { | |
119 | /* BTF container and BTF type ID describing | |
120 | * struct bpf_iter_<type> of an iterator state | |
121 | */ | |
122 | struct btf *btf; | |
123 | u32 btf_id; | |
124 | /* packing following two fields to fit iter state into 16 bytes */ | |
125 | enum bpf_iter_state state:2; | |
126 | int depth:30; | |
127 | } iter; | |
128 | ||
0962590e | 129 | /* Max size from any of the above. */ |
22dc4a0f AN |
130 | struct { |
131 | unsigned long raw1; | |
132 | unsigned long raw2; | |
133 | } raw; | |
69c087ba YS |
134 | |
135 | u32 subprogno; /* for PTR_TO_FUNC */ | |
58e2af8b | 136 | }; |
a73bf9f2 AN |
137 | /* For scalar types (SCALAR_VALUE), this represents our knowledge of |
138 | * the actual value. | |
139 | * For pointer types, this represents the variable part of the offset | |
140 | * from the pointed-to object, and is shared with all bpf_reg_states | |
141 | * with the same id as us. | |
142 | */ | |
143 | struct tnum var_off; | |
144 | /* Used to determine if any memory access using this register will | |
145 | * result in a bad access. | |
146 | * These refer to the same value as var_off, not necessarily the actual | |
147 | * contents of the register. | |
148 | */ | |
149 | s64 smin_value; /* minimum possible (s64)value */ | |
150 | s64 smax_value; /* maximum possible (s64)value */ | |
151 | u64 umin_value; /* minimum possible (u64)value */ | |
152 | u64 umax_value; /* maximum possible (u64)value */ | |
153 | s32 s32_min_value; /* minimum possible (s32)value */ | |
154 | s32 s32_max_value; /* maximum possible (s32)value */ | |
155 | u32 u32_min_value; /* minimum possible (u32)value */ | |
156 | u32 u32_max_value; /* maximum possible (u32)value */ | |
f1174f77 EC |
157 | /* For PTR_TO_PACKET, used to find other pointers with the same variable |
158 | * offset, so they can share range knowledge. | |
159 | * For PTR_TO_MAP_VALUE_OR_NULL this is used to share which map value we | |
160 | * came from, when one is tested for != NULL. | |
457f4436 AN |
161 | * For PTR_TO_MEM_OR_NULL this is used to identify memory allocation |
162 | * for the purpose of tracking that it's freed. | |
c64b7983 JS |
163 | * For PTR_TO_SOCKET this is used to share which pointers retain the |
164 | * same reference to the socket, to determine proper reference freeing. | |
bc34dee6 JK |
165 | * For stack slots that are dynptrs, this is used to track references to |
166 | * the dynptr to determine proper reference freeing. | |
06accc87 AN |
167 | * Similarly to dynptrs, we use ID to track "belonging" of a reference |
168 | * to a specific instance of bpf_iter. | |
f1174f77 | 169 | */ |
d2a4dd37 | 170 | u32 id; |
1b986589 MKL |
171 | /* PTR_TO_SOCKET and PTR_TO_TCP_SOCK could be a ptr returned |
172 | * from a pointer-cast helper, bpf_sk_fullsock() and | |
173 | * bpf_tcp_sock(). | |
174 | * | |
175 | * Consider the following where "sk" is a reference counted | |
176 | * pointer returned from "sk = bpf_sk_lookup_tcp();": | |
177 | * | |
178 | * 1: sk = bpf_sk_lookup_tcp(); | |
179 | * 2: if (!sk) { return 0; } | |
180 | * 3: fullsock = bpf_sk_fullsock(sk); | |
181 | * 4: if (!fullsock) { bpf_sk_release(sk); return 0; } | |
182 | * 5: tp = bpf_tcp_sock(fullsock); | |
183 | * 6: if (!tp) { bpf_sk_release(sk); return 0; } | |
184 | * 7: bpf_sk_release(sk); | |
185 | * 8: snd_cwnd = tp->snd_cwnd; // verifier will complain | |
186 | * | |
187 | * After bpf_sk_release(sk) at line 7, both "fullsock" ptr and | |
188 | * "tp" ptr should be invalidated also. In order to do that, | |
189 | * the reg holding "fullsock" and "sk" need to remember | |
190 | * the original refcounted ptr id (i.e. sk_reg->id) in ref_obj_id | |
191 | * such that the verifier can reset all regs which have | |
192 | * ref_obj_id matching the sk_reg->id. | |
193 | * | |
194 | * sk_reg->ref_obj_id is set to sk_reg->id at line 1. | |
195 | * sk_reg->id will stay as NULL-marking purpose only. | |
196 | * After NULL-marking is done, sk_reg->id can be reset to 0. | |
197 | * | |
198 | * After "fullsock = bpf_sk_fullsock(sk);" at line 3, | |
199 | * fullsock_reg->ref_obj_id is set to sk_reg->ref_obj_id. | |
200 | * | |
201 | * After "tp = bpf_tcp_sock(fullsock);" at line 5, | |
202 | * tp_reg->ref_obj_id is set to fullsock_reg->ref_obj_id | |
203 | * which is the same as sk_reg->ref_obj_id. | |
204 | * | |
205 | * From the verifier perspective, if sk, fullsock and tp | |
206 | * are not NULL, they are the same ptr with different | |
207 | * reg->type. In particular, bpf_sk_release(tp) is also | |
208 | * allowed and has the same effect as bpf_sk_release(sk). | |
209 | */ | |
210 | u32 ref_obj_id; | |
679c782d EC |
211 | /* parentage chain for liveness checking */ |
212 | struct bpf_reg_state *parent; | |
f4d7e40a AS |
213 | /* Inside the callee two registers can be both PTR_TO_STACK like |
214 | * R1=fp-8 and R2=fp-8, but one of them points to this function stack | |
215 | * while another to the caller's stack. To differentiate them 'frameno' | |
216 | * is used which is an index in bpf_verifier_state->frame[] array | |
217 | * pointing to bpf_func_state. | |
f4d7e40a AS |
218 | */ |
219 | u32 frameno; | |
5327ed3d JW |
220 | /* Tracks subreg definition. The stored value is the insn_idx of the |
221 | * writing insn. This is safe because subreg_def is used before any insn | |
222 | * patching which only happens after main verification finished. | |
223 | */ | |
224 | s32 subreg_def; | |
dc503a8a | 225 | enum bpf_reg_liveness live; |
b5dc0163 AS |
226 | /* if (!precise && SCALAR_VALUE) min/max/tnum don't affect safety */ |
227 | bool precise; | |
58e2af8b JK |
228 | }; |
229 | ||
230 | enum bpf_stack_slot_type { | |
231 | STACK_INVALID, /* nothing was stored in this stack slot */ | |
232 | STACK_SPILL, /* register spilled into stack */ | |
cc2b14d5 AS |
233 | STACK_MISC, /* BPF program wrote some data into this slot */ |
234 | STACK_ZERO, /* BPF program wrote constant zero */ | |
97e03f52 JK |
235 | /* A dynptr is stored in this stack slot. The type of dynptr |
236 | * is stored in bpf_stack_state->spilled_ptr.dynptr.type | |
237 | */ | |
238 | STACK_DYNPTR, | |
06accc87 | 239 | STACK_ITER, |
58e2af8b JK |
240 | }; |
241 | ||
242 | #define BPF_REG_SIZE 8 /* size of eBPF register in bytes */ | |
06accc87 | 243 | |
407958a0 AN |
244 | #define BPF_REGMASK_ARGS ((1 << BPF_REG_1) | (1 << BPF_REG_2) | \ |
245 | (1 << BPF_REG_3) | (1 << BPF_REG_4) | \ | |
246 | (1 << BPF_REG_5)) | |
247 | ||
97e03f52 JK |
248 | #define BPF_DYNPTR_SIZE sizeof(struct bpf_dynptr_kern) |
249 | #define BPF_DYNPTR_NR_SLOTS (BPF_DYNPTR_SIZE / BPF_REG_SIZE) | |
58e2af8b | 250 | |
638f5b90 AS |
251 | struct bpf_stack_state { |
252 | struct bpf_reg_state spilled_ptr; | |
253 | u8 slot_type[BPF_REG_SIZE]; | |
254 | }; | |
255 | ||
fd978bf7 JS |
256 | struct bpf_reference_state { |
257 | /* Track each reference created with a unique id, even if the same | |
258 | * instruction creates the reference multiple times (eg, via CALL). | |
259 | */ | |
260 | int id; | |
261 | /* Instruction where the allocation of this reference occurred. This | |
262 | * is used purely to inform the user of a reference leak. | |
263 | */ | |
264 | int insn_idx; | |
9d9d00ac KKD |
265 | /* There can be a case like: |
266 | * main (frame 0) | |
267 | * cb (frame 1) | |
268 | * func (frame 3) | |
269 | * cb (frame 4) | |
270 | * Hence for frame 4, if callback_ref just stored boolean, it would be | |
271 | * impossible to distinguish nested callback refs. Hence store the | |
272 | * frameno and compare that to callback_ref in check_reference_leak when | |
273 | * exiting a callback function. | |
274 | */ | |
275 | int callback_ref; | |
fd978bf7 JS |
276 | }; |
277 | ||
8fa4ecd4 AN |
278 | struct bpf_retval_range { |
279 | s32 minval; | |
280 | s32 maxval; | |
281 | }; | |
282 | ||
58e2af8b JK |
283 | /* state of the program: |
284 | * type of all registers and stack info | |
285 | */ | |
f4d7e40a | 286 | struct bpf_func_state { |
58e2af8b | 287 | struct bpf_reg_state regs[MAX_BPF_REG]; |
f4d7e40a AS |
288 | /* index of call instruction that called into this func */ |
289 | int callsite; | |
290 | /* stack frame number of this function state from pov of | |
291 | * enclosing bpf_verifier_state. | |
292 | * 0 = main function, 1 = first callee. | |
293 | */ | |
294 | u32 frameno; | |
01f810ac | 295 | /* subprog number == index within subprog_info |
f4d7e40a AS |
296 | * zero == main subprog |
297 | */ | |
298 | u32 subprogno; | |
bfc6bb74 AS |
299 | /* Every bpf_timer_start will increment async_entry_cnt. |
300 | * It's used to distinguish: | |
301 | * void foo(void) { for(;;); } | |
302 | * void foo(void) { bpf_timer_set_callback(,foo); } | |
303 | */ | |
304 | u32 async_entry_cnt; | |
8fa4ecd4 | 305 | struct bpf_retval_range callback_ret_range; |
45b5623f | 306 | bool in_callback_fn; |
bfc6bb74 | 307 | bool in_async_callback_fn; |
b9ae0c9d | 308 | bool in_exception_callback_fn; |
bb124da6 EZ |
309 | /* For callback calling functions that limit number of possible |
310 | * callback executions (e.g. bpf_loop) keeps track of current | |
311 | * simulated iteration number. | |
312 | * Value in frame N refers to number of times callback with frame | |
313 | * N+1 was simulated, e.g. for the following call: | |
314 | * | |
315 | * bpf_loop(..., fn, ...); | suppose current frame is N | |
316 | * | fn would be simulated in frame N+1 | |
317 | * | number of simulations is tracked in frame N | |
318 | */ | |
319 | u32 callback_depth; | |
f4d7e40a | 320 | |
fd978bf7 JS |
321 | /* The following fields should be last. See copy_func_state() */ |
322 | int acquired_refs; | |
323 | struct bpf_reference_state *refs; | |
92e1567e AM |
324 | /* The state of the stack. Each element of the array describes BPF_REG_SIZE |
325 | * (i.e. 8) bytes worth of stack memory. | |
326 | * stack[0] represents bytes [*(r10-8)..*(r10-1)] | |
327 | * stack[1] represents bytes [*(r10-16)..*(r10-9)] | |
328 | * ... | |
329 | * stack[allocated_stack/8 - 1] represents [*(r10-allocated_stack)..*(r10-allocated_stack+7)] | |
330 | */ | |
638f5b90 | 331 | struct bpf_stack_state *stack; |
92e1567e AM |
332 | /* Size of the current stack, in bytes. The stack state is tracked below, in |
333 | * `stack`. allocated_stack is always a multiple of BPF_REG_SIZE. | |
334 | */ | |
45b5623f | 335 | int allocated_stack; |
58e2af8b JK |
336 | }; |
337 | ||
41f6f64e AN |
338 | #define MAX_CALL_FRAMES 8 |
339 | ||
340 | /* instruction history flags, used in bpf_jmp_history_entry.flags field */ | |
341 | enum { | |
342 | /* instruction references stack slot through PTR_TO_STACK register; | |
343 | * we also store stack's frame number in lower 3 bits (MAX_CALL_FRAMES is 8) | |
344 | * and accessed stack slot's index in next 6 bits (MAX_BPF_STACK is 512, | |
345 | * 8 bytes per slot, so slot index (spi) is [0, 63]) | |
346 | */ | |
347 | INSN_F_FRAMENO_MASK = 0x7, /* 3 bits */ | |
348 | ||
349 | INSN_F_SPI_MASK = 0x3f, /* 6 bits */ | |
350 | INSN_F_SPI_SHIFT = 3, /* shifted 3 bits to the left */ | |
351 | ||
352 | INSN_F_STACK_ACCESS = BIT(9), /* we need 10 bits total */ | |
353 | }; | |
354 | ||
355 | static_assert(INSN_F_FRAMENO_MASK + 1 >= MAX_CALL_FRAMES); | |
356 | static_assert(INSN_F_SPI_MASK + 1 >= MAX_BPF_STACK / 8); | |
357 | ||
358 | struct bpf_jmp_history_entry { | |
b5dc0163 | 359 | u32 idx; |
41f6f64e AN |
360 | /* insn idx can't be bigger than 1 million */ |
361 | u32 prev_idx : 22; | |
362 | /* special flags, e.g., whether insn is doing register stack spill/load */ | |
363 | u32 flags : 10; | |
b5dc0163 AS |
364 | }; |
365 | ||
5dd9cdbc EZ |
366 | /* Maximum number of register states that can exist at once */ |
367 | #define BPF_ID_MAP_SIZE ((MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE) * MAX_CALL_FRAMES) | |
f4d7e40a AS |
368 | struct bpf_verifier_state { |
369 | /* call stack tracking */ | |
370 | struct bpf_func_state *frame[MAX_CALL_FRAMES]; | |
2589726d AS |
371 | struct bpf_verifier_state *parent; |
372 | /* | |
373 | * 'branches' field is the number of branches left to explore: | |
374 | * 0 - all possible paths from this state reached bpf_exit or | |
375 | * were safely pruned | |
376 | * 1 - at least one path is being explored. | |
377 | * This state hasn't reached bpf_exit | |
378 | * 2 - at least two paths are being explored. | |
379 | * This state is an immediate parent of two children. | |
380 | * One is fallthrough branch with branches==1 and another | |
381 | * state is pushed into stack (to be explored later) also with | |
382 | * branches==1. The parent of this state has branches==1. | |
383 | * The verifier state tree connected via 'parent' pointer looks like: | |
384 | * 1 | |
385 | * 1 | |
386 | * 2 -> 1 (first 'if' pushed into stack) | |
387 | * 1 | |
388 | * 2 -> 1 (second 'if' pushed into stack) | |
389 | * 1 | |
390 | * 1 | |
391 | * 1 bpf_exit. | |
392 | * | |
393 | * Once do_check() reaches bpf_exit, it calls update_branch_counts() | |
394 | * and the verifier state tree will look: | |
395 | * 1 | |
396 | * 1 | |
397 | * 2 -> 1 (first 'if' pushed into stack) | |
398 | * 1 | |
399 | * 1 -> 1 (second 'if' pushed into stack) | |
400 | * 0 | |
401 | * 0 | |
402 | * 0 bpf_exit. | |
403 | * After pop_stack() the do_check() will resume at second 'if'. | |
404 | * | |
405 | * If is_state_visited() sees a state with branches > 0 it means | |
406 | * there is a loop. If such state is exactly equal to the current state | |
407 | * it's an infinite loop. Note states_equal() checks for states | |
6dbdc9f3 | 408 | * equivalency, so two states being 'states_equal' does not mean |
2589726d AS |
409 | * infinite loop. The exact comparison is provided by |
410 | * states_maybe_looping() function. It's a stronger pre-check and | |
411 | * much faster than states_equal(). | |
412 | * | |
413 | * This algorithm may not find all possible infinite loops or | |
414 | * loop iteration count may be too high. | |
415 | * In such cases BPF_COMPLEXITY_LIMIT_INSNS limit kicks in. | |
416 | */ | |
417 | u32 branches; | |
dc2a4ebc | 418 | u32 insn_idx; |
f4d7e40a | 419 | u32 curframe; |
6a3cd331 DM |
420 | |
421 | struct bpf_active_lock active_lock; | |
979d63d5 | 422 | bool speculative; |
9bb00b28 | 423 | bool active_rcu_lock; |
fc7566ad | 424 | u32 active_preempt_lock; |
2a099282 EZ |
425 | /* If this state was ever pointed-to by other state's loop_entry field |
426 | * this flag would be set to true. Used to avoid freeing such states | |
427 | * while they are still in use. | |
428 | */ | |
429 | bool used_as_loop_entry; | |
81f1d7a5 | 430 | bool in_sleepable; |
b5dc0163 AS |
431 | |
432 | /* first and last insn idx of this verifier state */ | |
433 | u32 first_insn_idx; | |
434 | u32 last_insn_idx; | |
2a099282 EZ |
435 | /* If this state is a part of states loop this field points to some |
436 | * parent of this state such that: | |
437 | * - it is also a member of the same states loop; | |
438 | * - DFS states traversal starting from initial state visits loop_entry | |
439 | * state before this state. | |
440 | * Used to compute topmost loop entry for state loops. | |
441 | * State loops might appear because of open coded iterators logic. | |
442 | * See get_loop_entry() for more information. | |
443 | */ | |
444 | struct bpf_verifier_state *loop_entry; | |
b5dc0163 AS |
445 | /* jmp history recorded from first to last. |
446 | * backtracking is using it to go from last to first. | |
447 | * For most states jmp_history_cnt is [0-3]. | |
448 | * For loops can go up to ~40. | |
449 | */ | |
41f6f64e | 450 | struct bpf_jmp_history_entry *jmp_history; |
b5dc0163 | 451 | u32 jmp_history_cnt; |
2793a8b0 | 452 | u32 dfs_depth; |
ab5cfac1 | 453 | u32 callback_unroll_depth; |
011832b9 | 454 | u32 may_goto_depth; |
f4d7e40a AS |
455 | }; |
456 | ||
dfab99df | 457 | #define bpf_get_spilled_reg(slot, frame, mask) \ |
f3709f69 | 458 | (((slot < frame->allocated_stack / BPF_REG_SIZE) && \ |
32f55dd4 | 459 | ((1 << frame->stack[slot].slot_type[BPF_REG_SIZE - 1]) & (mask))) \ |
f3709f69 JS |
460 | ? &frame->stack[slot].spilled_ptr : NULL) |
461 | ||
462 | /* Iterate over 'frame', setting 'reg' to either NULL or a spilled register. */ | |
dfab99df CZ |
463 | #define bpf_for_each_spilled_reg(iter, frame, reg, mask) \ |
464 | for (iter = 0, reg = bpf_get_spilled_reg(iter, frame, mask); \ | |
f3709f69 | 465 | iter < frame->allocated_stack / BPF_REG_SIZE; \ |
dfab99df | 466 | iter++, reg = bpf_get_spilled_reg(iter, frame, mask)) |
f3709f69 | 467 | |
dfab99df | 468 | #define bpf_for_each_reg_in_vstate_mask(__vst, __state, __reg, __mask, __expr) \ |
b239da34 KKD |
469 | ({ \ |
470 | struct bpf_verifier_state *___vstate = __vst; \ | |
471 | int ___i, ___j; \ | |
472 | for (___i = 0; ___i <= ___vstate->curframe; ___i++) { \ | |
473 | struct bpf_reg_state *___regs; \ | |
474 | __state = ___vstate->frame[___i]; \ | |
475 | ___regs = __state->regs; \ | |
476 | for (___j = 0; ___j < MAX_BPF_REG; ___j++) { \ | |
477 | __reg = &___regs[___j]; \ | |
478 | (void)(__expr); \ | |
479 | } \ | |
dfab99df | 480 | bpf_for_each_spilled_reg(___j, __state, __reg, __mask) { \ |
b239da34 KKD |
481 | if (!__reg) \ |
482 | continue; \ | |
483 | (void)(__expr); \ | |
484 | } \ | |
485 | } \ | |
486 | }) | |
487 | ||
dfab99df CZ |
488 | /* Invoke __expr over regsiters in __vst, setting __state and __reg */ |
489 | #define bpf_for_each_reg_in_vstate(__vst, __state, __reg, __expr) \ | |
490 | bpf_for_each_reg_in_vstate_mask(__vst, __state, __reg, 1 << STACK_SPILL, __expr) | |
491 | ||
58e2af8b JK |
492 | /* linked list of verifier states used to prune search */ |
493 | struct bpf_verifier_state_list { | |
494 | struct bpf_verifier_state state; | |
495 | struct bpf_verifier_state_list *next; | |
9f4686c4 | 496 | int miss_cnt, hit_cnt; |
58e2af8b JK |
497 | }; |
498 | ||
1ade2371 | 499 | struct bpf_loop_inline_state { |
f16214c1 MB |
500 | unsigned int initialized:1; /* set to true upon first entry */ |
501 | unsigned int fit_for_inline:1; /* true if callback function is the same | |
502 | * at each call and flags are always zero | |
503 | */ | |
1ade2371 EZ |
504 | u32 callback_subprogno; /* valid when fit_for_inline is true */ |
505 | }; | |
506 | ||
0a525621 PL |
507 | /* pointer and state for maps */ |
508 | struct bpf_map_ptr_state { | |
509 | struct bpf_map *map_ptr; | |
510 | bool poison; | |
511 | bool unpriv; | |
512 | }; | |
513 | ||
979d63d5 | 514 | /* Possible states for alu_state member. */ |
801c6058 DB |
515 | #define BPF_ALU_SANITIZE_SRC (1U << 0) |
516 | #define BPF_ALU_SANITIZE_DST (1U << 1) | |
979d63d5 | 517 | #define BPF_ALU_NEG_VALUE (1U << 2) |
d3bd7413 | 518 | #define BPF_ALU_NON_POINTER (1U << 3) |
801c6058 | 519 | #define BPF_ALU_IMMEDIATE (1U << 4) |
979d63d5 DB |
520 | #define BPF_ALU_SANITIZE (BPF_ALU_SANITIZE_SRC | \ |
521 | BPF_ALU_SANITIZE_DST) | |
522 | ||
58e2af8b | 523 | struct bpf_insn_aux_data { |
81ed18ab AS |
524 | union { |
525 | enum bpf_reg_type ptr_type; /* pointer type for load/store insns */ | |
0a525621 | 526 | struct bpf_map_ptr_state map_ptr_state; |
1c2a088a | 527 | s32 call_imm; /* saved imm field of call insn */ |
979d63d5 | 528 | u32 alu_limit; /* limit for add/sub register with pointer */ |
d8eca5bb DB |
529 | struct { |
530 | u32 map_index; /* index into used_maps[] */ | |
531 | u32 map_off; /* offset from value base address */ | |
532 | }; | |
4976b718 HL |
533 | struct { |
534 | enum bpf_reg_type reg_type; /* type of pseudo_btf_id */ | |
535 | union { | |
22dc4a0f AN |
536 | struct { |
537 | struct btf *btf; | |
538 | u32 btf_id; /* btf_id for struct typed var */ | |
539 | }; | |
4976b718 HL |
540 | u32 mem_size; /* mem_size for non-struct typed var */ |
541 | }; | |
542 | } btf_var; | |
1ade2371 EZ |
543 | /* if instruction is a call to bpf_loop this field tracks |
544 | * the state of the relevant registers to make decision about inlining | |
545 | */ | |
546 | struct bpf_loop_inline_state loop_inline_state; | |
81ed18ab | 547 | }; |
d2dcc67d DM |
548 | union { |
549 | /* remember the size of type passed to bpf_obj_new to rewrite R1 */ | |
550 | u64 obj_new_size; | |
551 | /* remember the offset of node field within type to rewrite */ | |
552 | u64 insert_off; | |
553 | }; | |
958cf2e2 | 554 | struct btf_struct_meta *kptr_struct_meta; |
d2e4c1e6 | 555 | u64 map_key_state; /* constant (32 bit) key tracking for maps */ |
23994631 | 556 | int ctx_field_size; /* the ctx field size for load insn, maybe 0 */ |
51c39bb1 | 557 | u32 seen; /* this insn was processed by the verifier at env->pass_cnt */ |
2039f26f | 558 | bool sanitize_stack_spill; /* subject to Spectre v4 sanitation */ |
5327ed3d | 559 | bool zext_dst; /* this insn zero extends dst reg */ |
6082b6c3 | 560 | bool needs_zext; /* alu op needs to clear upper bits */ |
9bb00b28 | 561 | bool storage_get_func_atomic; /* bpf_*_storage_get() with atomic memory alloc */ |
06accc87 | 562 | bool is_iter_next; /* bpf_iter_<type>_next() kfunc call */ |
01cc55af | 563 | bool call_with_percpu_alloc_ptr; /* {this,per}_cpu_ptr() with prog percpu alloc */ |
979d63d5 | 564 | u8 alu_state; /* used in combination with alu_limit */ |
51c39bb1 AS |
565 | |
566 | /* below fields are initialized once */ | |
9e4c24e7 | 567 | unsigned int orig_idx; /* original instruction index */ |
bffdeaa8 | 568 | bool jmp_point; |
4b5ce570 AN |
569 | bool prune_point; |
570 | /* ensure we check state equivalence and save state checkpoint and | |
571 | * this instruction, regardless of any heuristics | |
572 | */ | |
573 | bool force_checkpoint; | |
ab5cfac1 EZ |
574 | /* true if instruction is a call to a helper function that |
575 | * accepts callback function as a parameter. | |
576 | */ | |
577 | bool calls_callback; | |
58e2af8b JK |
578 | }; |
579 | ||
580 | #define MAX_USED_MAPS 64 /* max number of maps accessed by one eBPF program */ | |
541c3bad | 581 | #define MAX_USED_BTFS 64 /* max number of BTFs accessed by one BPF program */ |
58e2af8b | 582 | |
a2a7d570 JK |
583 | #define BPF_VERIFIER_TMP_LOG_SIZE 1024 |
584 | ||
b9193c1b | 585 | struct bpf_verifier_log { |
12166409 AN |
586 | /* Logical start and end positions of a "log window" of the verifier log. |
587 | * start_pos == 0 means we haven't truncated anything. | |
588 | * Once truncation starts to happen, start_pos + len_total == end_pos, | |
589 | * except during log reset situations, in which (end_pos - start_pos) | |
590 | * might get smaller than len_total (see bpf_vlog_reset()). | |
591 | * Generally, (end_pos - start_pos) gives number of useful data in | |
592 | * user log buffer. | |
593 | */ | |
594 | u64 start_pos; | |
595 | u64 end_pos; | |
e7bf8249 | 596 | char __user *ubuf; |
12166409 | 597 | u32 level; |
e7bf8249 | 598 | u32 len_total; |
fa1c7d5c | 599 | u32 len_max; |
12166409 | 600 | char kbuf[BPF_VERIFIER_TMP_LOG_SIZE]; |
e7bf8249 JK |
601 | }; |
602 | ||
06ee7115 AS |
603 | #define BPF_LOG_LEVEL1 1 |
604 | #define BPF_LOG_LEVEL2 2 | |
605 | #define BPF_LOG_STATS 4 | |
12166409 | 606 | #define BPF_LOG_FIXED 8 |
06ee7115 | 607 | #define BPF_LOG_LEVEL (BPF_LOG_LEVEL1 | BPF_LOG_LEVEL2) |
12166409 | 608 | #define BPF_LOG_MASK (BPF_LOG_LEVEL | BPF_LOG_STATS | BPF_LOG_FIXED) |
8580ac94 | 609 | #define BPF_LOG_KERNEL (BPF_LOG_MASK + 1) /* kernel internal flag */ |
2e576648 CL |
610 | #define BPF_LOG_MIN_ALIGNMENT 8U |
611 | #define BPF_LOG_ALIGNMENT 40U | |
06ee7115 | 612 | |
77d2e05a MKL |
613 | static inline bool bpf_verifier_log_needed(const struct bpf_verifier_log *log) |
614 | { | |
fa1c7d5c | 615 | return log && log->level; |
77d2e05a MKL |
616 | } |
617 | ||
cc8b0b92 AS |
618 | #define BPF_MAX_SUBPROGS 256 |
619 | ||
4ba1d0f2 AN |
620 | struct bpf_subprog_arg_info { |
621 | enum bpf_arg_type arg_type; | |
622 | union { | |
623 | u32 mem_size; | |
e2b3c4ff | 624 | u32 btf_id; |
4ba1d0f2 AN |
625 | }; |
626 | }; | |
627 | ||
9c8105bd | 628 | struct bpf_subprog_info { |
8c1b6e69 | 629 | /* 'start' has to be the first field otherwise find_subprog() won't work */ |
9c8105bd | 630 | u32 start; /* insn idx of function entry point */ |
c454a46b | 631 | u32 linfo_idx; /* The idx to the main_prog->aux->linfo */ |
9c8105bd | 632 | u16 stack_depth; /* max. stack depth used by this function */ |
011832b9 | 633 | u16 stack_extra; |
406a6fa4 AN |
634 | bool has_tail_call: 1; |
635 | bool tail_call_reachable: 1; | |
636 | bool has_ld_abs: 1; | |
637 | bool is_cb: 1; | |
638 | bool is_async_cb: 1; | |
639 | bool is_exception_cb: 1; | |
4ba1d0f2 AN |
640 | bool args_cached: 1; |
641 | ||
642 | u8 arg_cnt; | |
643 | struct bpf_subprog_arg_info args[MAX_BPF_FUNC_REG_ARGS]; | |
9c8105bd JW |
644 | }; |
645 | ||
407958a0 AN |
646 | struct bpf_verifier_env; |
647 | ||
648 | struct backtrack_state { | |
649 | struct bpf_verifier_env *env; | |
650 | u32 frame; | |
651 | u32 reg_masks[MAX_CALL_FRAMES]; | |
652 | u64 stack_masks[MAX_CALL_FRAMES]; | |
653 | }; | |
654 | ||
1ffc85d9 EZ |
655 | struct bpf_id_pair { |
656 | u32 old; | |
657 | u32 cur; | |
658 | }; | |
659 | ||
660 | struct bpf_idmap { | |
661 | u32 tmp_id_gen; | |
662 | struct bpf_id_pair map[BPF_ID_MAP_SIZE]; | |
663 | }; | |
664 | ||
904e6ddf EZ |
665 | struct bpf_idset { |
666 | u32 count; | |
667 | u32 ids[BPF_ID_MAP_SIZE]; | |
668 | }; | |
669 | ||
58e2af8b JK |
670 | /* single container for all structs |
671 | * one verifier_env per bpf_check() call | |
672 | */ | |
673 | struct bpf_verifier_env { | |
c08435ec DB |
674 | u32 insn_idx; |
675 | u32 prev_insn_idx; | |
58e2af8b | 676 | struct bpf_prog *prog; /* eBPF program being verified */ |
00176a34 | 677 | const struct bpf_verifier_ops *ops; |
e3f87fdf | 678 | struct module *attach_btf_mod; /* The owner module of prog->aux->attach_btf */ |
58e2af8b JK |
679 | struct bpf_verifier_stack_elem *head; /* stack of verifier states to be processed */ |
680 | int stack_size; /* number of states to be processed */ | |
e07b98d9 | 681 | bool strict_alignment; /* perform strict pointer alignment checks */ |
10d274e8 | 682 | bool test_state_freq; /* test verifier with different pruning frequency */ |
ff8867af | 683 | bool test_reg_invariants; /* fail verification on register invariants violations */ |
638f5b90 | 684 | struct bpf_verifier_state *cur_state; /* current verifier state */ |
58e2af8b | 685 | struct bpf_verifier_state_list **explored_states; /* search pruning optimization */ |
9f4686c4 | 686 | struct bpf_verifier_state_list *free_list; |
58e2af8b | 687 | struct bpf_map *used_maps[MAX_USED_MAPS]; /* array of map's used by eBPF program */ |
541c3bad | 688 | struct btf_mod_pair used_btfs[MAX_USED_BTFS]; /* array of BTF's used by BPF program */ |
58e2af8b | 689 | u32 used_map_cnt; /* number of used maps */ |
541c3bad | 690 | u32 used_btf_cnt; /* number of used BTF objects */ |
58e2af8b | 691 | u32 id_gen; /* used to generate unique reg IDs */ |
335d1c5b | 692 | u32 hidden_subprog_cnt; /* number of hidden subprogs */ |
f18b03fa | 693 | int exception_callback_subprog; |
e042aa53 | 694 | bool explore_alu_limits; |
58e2af8b | 695 | bool allow_ptr_leaks; |
92e1567e AM |
696 | /* Allow access to uninitialized stack memory. Writes with fixed offset are |
697 | * always allowed, so this refers to reads (with fixed or variable offset), | |
698 | * to writes with variable offset and to indirect (helper) accesses. | |
699 | */ | |
01f810ac | 700 | bool allow_uninit_stack; |
2c78ee89 AS |
701 | bool bpf_capable; |
702 | bool bypass_spec_v1; | |
703 | bool bypass_spec_v4; | |
58e2af8b | 704 | bool seen_direct_write; |
f18b03fa | 705 | bool seen_exception; |
58e2af8b | 706 | struct bpf_insn_aux_data *insn_aux_data; /* array of per-insn state */ |
d9762e84 | 707 | const struct bpf_line_info *prev_linfo; |
b9193c1b | 708 | struct bpf_verifier_log log; |
335d1c5b | 709 | struct bpf_subprog_info subprog_info[BPF_MAX_SUBPROGS + 2]; /* max + 2 for the fake and exception subprogs */ |
904e6ddf | 710 | union { |
1ffc85d9 | 711 | struct bpf_idmap idmap_scratch; |
904e6ddf EZ |
712 | struct bpf_idset idset_scratch; |
713 | }; | |
7df737e9 AS |
714 | struct { |
715 | int *insn_state; | |
716 | int *insn_stack; | |
717 | int cur_stack; | |
718 | } cfg; | |
407958a0 | 719 | struct backtrack_state bt; |
41f6f64e | 720 | struct bpf_jmp_history_entry *cur_hist_ent; |
51c39bb1 | 721 | u32 pass_cnt; /* number of times do_check() was called */ |
cc8b0b92 | 722 | u32 subprog_cnt; |
06ee7115 | 723 | /* number of instructions analyzed by the verifier */ |
2589726d AS |
724 | u32 prev_insn_processed, insn_processed; |
725 | /* number of jmps, calls, exits analyzed so far */ | |
726 | u32 prev_jmps_processed, jmps_processed; | |
06ee7115 AS |
727 | /* total verification time */ |
728 | u64 verification_time; | |
729 | /* maximum number of verifier states kept in 'branching' instructions */ | |
730 | u32 max_states_per_insn; | |
731 | /* total number of allocated verifier states */ | |
732 | u32 total_states; | |
733 | /* some states are freed during program analysis. | |
734 | * this is peak number of states. this number dominates kernel | |
735 | * memory consumption during verification | |
736 | */ | |
737 | u32 peak_states; | |
738 | /* longest register parentage chain walked for liveness marking */ | |
739 | u32 longest_mark_read_walk; | |
387544bf | 740 | bpfptr_t fd_array; |
0f55f9ed CL |
741 | |
742 | /* bit mask to keep track of whether a register has been accessed | |
743 | * since the last time the function state was printed | |
744 | */ | |
745 | u32 scratched_regs; | |
746 | /* Same as scratched_regs but for stack slots */ | |
747 | u64 scratched_stack_slots; | |
12166409 | 748 | u64 prev_log_pos, prev_insn_print_pos; |
92424801 DB |
749 | /* buffer used to temporary hold constants as scalar registers */ |
750 | struct bpf_reg_state fake_reg[2]; | |
d9439c21 AN |
751 | /* buffer used to generate temporary string representations, |
752 | * e.g., in reg_type_str() to generate reg_type string | |
753 | */ | |
754 | char tmp_str_buf[TMP_STR_BUF_LEN]; | |
58e2af8b JK |
755 | }; |
756 | ||
e26080d0 AN |
757 | static inline struct bpf_func_info_aux *subprog_aux(struct bpf_verifier_env *env, int subprog) |
758 | { | |
759 | return &env->prog->aux->func_info_aux[subprog]; | |
760 | } | |
761 | ||
4ba1d0f2 AN |
762 | static inline struct bpf_subprog_info *subprog_info(struct bpf_verifier_env *env, int subprog) |
763 | { | |
764 | return &env->subprog_info[subprog]; | |
765 | } | |
766 | ||
be2d04d1 MM |
767 | __printf(2, 0) void bpf_verifier_vlog(struct bpf_verifier_log *log, |
768 | const char *fmt, va_list args); | |
430e68d1 QM |
769 | __printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env, |
770 | const char *fmt, ...); | |
9e15db66 AS |
771 | __printf(2, 3) void bpf_log(struct bpf_verifier_log *log, |
772 | const char *fmt, ...); | |
bdcab414 AN |
773 | int bpf_vlog_init(struct bpf_verifier_log *log, u32 log_level, |
774 | char __user *log_buf, u32 log_size); | |
12166409 | 775 | void bpf_vlog_reset(struct bpf_verifier_log *log, u64 new_pos); |
bdcab414 | 776 | int bpf_vlog_finalize(struct bpf_verifier_log *log, u32 *log_size_actual); |
430e68d1 | 777 | |
db840d38 AN |
778 | __printf(3, 4) void verbose_linfo(struct bpf_verifier_env *env, |
779 | u32 insn_off, | |
780 | const char *prefix_fmt, ...); | |
781 | ||
fd978bf7 | 782 | static inline struct bpf_func_state *cur_func(struct bpf_verifier_env *env) |
638f5b90 | 783 | { |
f4d7e40a AS |
784 | struct bpf_verifier_state *cur = env->cur_state; |
785 | ||
fd978bf7 JS |
786 | return cur->frame[cur->curframe]; |
787 | } | |
788 | ||
789 | static inline struct bpf_reg_state *cur_regs(struct bpf_verifier_env *env) | |
790 | { | |
791 | return cur_func(env)->regs; | |
638f5b90 AS |
792 | } |
793 | ||
a40a2632 | 794 | int bpf_prog_offload_verifier_prep(struct bpf_prog *prog); |
cae1927c JK |
795 | int bpf_prog_offload_verify_insn(struct bpf_verifier_env *env, |
796 | int insn_idx, int prev_insn_idx); | |
c941ce9c | 797 | int bpf_prog_offload_finalize(struct bpf_verifier_env *env); |
08ca90af JK |
798 | void |
799 | bpf_prog_offload_replace_insn(struct bpf_verifier_env *env, u32 off, | |
800 | struct bpf_insn *insn); | |
801 | void | |
802 | bpf_prog_offload_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt); | |
ab3f0063 | 803 | |
f7b12b6f THJ |
804 | /* this lives here instead of in bpf.h because it needs to dereference tgt_prog */ |
805 | static inline u64 bpf_trampoline_compute_key(const struct bpf_prog *tgt_prog, | |
22dc4a0f | 806 | struct btf *btf, u32 btf_id) |
f7b12b6f | 807 | { |
22dc4a0f AN |
808 | if (tgt_prog) |
809 | return ((u64)tgt_prog->aux->id << 32) | btf_id; | |
810 | else | |
811 | return ((u64)btf_obj_id(btf) << 32) | 0x80000000 | btf_id; | |
f7b12b6f THJ |
812 | } |
813 | ||
441e8c66 THJ |
814 | /* unpack the IDs from the key as constructed above */ |
815 | static inline void bpf_trampoline_unpack_key(u64 key, u32 *obj_id, u32 *btf_id) | |
816 | { | |
817 | if (obj_id) | |
818 | *obj_id = key >> 32; | |
819 | if (btf_id) | |
820 | *btf_id = key & 0x7FFFFFFF; | |
821 | } | |
822 | ||
f7b12b6f THJ |
823 | int bpf_check_attach_target(struct bpf_verifier_log *log, |
824 | const struct bpf_prog *prog, | |
825 | const struct bpf_prog *tgt_prog, | |
826 | u32 btf_id, | |
827 | struct bpf_attach_target_info *tgt_info); | |
2357672c KKD |
828 | void bpf_free_kfunc_btf_tab(struct bpf_kfunc_btf_tab *tab); |
829 | ||
eb1f7f71 BT |
830 | int mark_chain_precision(struct bpf_verifier_env *env, int regno); |
831 | ||
d639b9d1 HL |
832 | #define BPF_BASE_TYPE_MASK GENMASK(BPF_BASE_TYPE_BITS - 1, 0) |
833 | ||
834 | /* extract base type from bpf_{arg, return, reg}_type. */ | |
835 | static inline u32 base_type(u32 type) | |
836 | { | |
837 | return type & BPF_BASE_TYPE_MASK; | |
838 | } | |
839 | ||
840 | /* extract flags from an extended type. See bpf_type_flag in bpf.h. */ | |
841 | static inline u32 type_flag(u32 type) | |
842 | { | |
843 | return type & ~BPF_BASE_TYPE_MASK; | |
844 | } | |
f7b12b6f | 845 | |
4a9c7bbe | 846 | /* only use after check_attach_btf_id() */ |
271de525 | 847 | static inline enum bpf_prog_type resolve_prog_type(const struct bpf_prog *prog) |
5c073f26 | 848 | { |
4a9c7bbe MKL |
849 | return prog->type == BPF_PROG_TYPE_EXT ? |
850 | prog->aux->dst_prog->type : prog->type; | |
5c073f26 KKD |
851 | } |
852 | ||
271de525 MKL |
853 | static inline bool bpf_prog_check_recur(const struct bpf_prog *prog) |
854 | { | |
855 | switch (resolve_prog_type(prog)) { | |
856 | case BPF_PROG_TYPE_TRACING: | |
857 | return prog->expected_attach_type != BPF_TRACE_ITER; | |
858 | case BPF_PROG_TYPE_STRUCT_OPS: | |
859 | case BPF_PROG_TYPE_LSM: | |
860 | return false; | |
861 | default: | |
862 | return true; | |
863 | } | |
864 | } | |
865 | ||
2a6d50b5 | 866 | #define BPF_REG_TRUSTED_MODIFIERS (MEM_ALLOC | PTR_TRUSTED | NON_OWN_REF) |
3f00c523 DV |
867 | |
868 | static inline bool bpf_type_has_unsafe_modifiers(u32 type) | |
869 | { | |
870 | return type_flag(type) & ~BPF_REG_TRUSTED_MODIFIERS; | |
871 | } | |
872 | ||
42feb662 AN |
873 | static inline bool type_is_ptr_alloc_obj(u32 type) |
874 | { | |
875 | return base_type(type) == PTR_TO_BTF_ID && type_flag(type) & MEM_ALLOC; | |
876 | } | |
877 | ||
878 | static inline bool type_is_non_owning_ref(u32 type) | |
879 | { | |
880 | return type_is_ptr_alloc_obj(type) && type_flag(type) & NON_OWN_REF; | |
881 | } | |
882 | ||
883 | static inline bool type_is_pkt_pointer(enum bpf_reg_type type) | |
884 | { | |
885 | type = base_type(type); | |
886 | return type == PTR_TO_PACKET || | |
887 | type == PTR_TO_PACKET_META; | |
888 | } | |
889 | ||
890 | static inline bool type_is_sk_pointer(enum bpf_reg_type type) | |
891 | { | |
892 | return type == PTR_TO_SOCKET || | |
893 | type == PTR_TO_SOCK_COMMON || | |
894 | type == PTR_TO_TCP_SOCK || | |
895 | type == PTR_TO_XDP_SOCK; | |
896 | } | |
897 | ||
898 | static inline void mark_reg_scratched(struct bpf_verifier_env *env, u32 regno) | |
899 | { | |
900 | env->scratched_regs |= 1U << regno; | |
901 | } | |
902 | ||
903 | static inline void mark_stack_slot_scratched(struct bpf_verifier_env *env, u32 spi) | |
904 | { | |
905 | env->scratched_stack_slots |= 1ULL << spi; | |
906 | } | |
907 | ||
908 | static inline bool reg_scratched(const struct bpf_verifier_env *env, u32 regno) | |
909 | { | |
910 | return (env->scratched_regs >> regno) & 1; | |
911 | } | |
912 | ||
913 | static inline bool stack_slot_scratched(const struct bpf_verifier_env *env, u64 regno) | |
914 | { | |
915 | return (env->scratched_stack_slots >> regno) & 1; | |
916 | } | |
917 | ||
918 | static inline bool verifier_state_scratched(const struct bpf_verifier_env *env) | |
919 | { | |
920 | return env->scratched_regs || env->scratched_stack_slots; | |
921 | } | |
922 | ||
923 | static inline void mark_verifier_state_clean(struct bpf_verifier_env *env) | |
924 | { | |
925 | env->scratched_regs = 0U; | |
926 | env->scratched_stack_slots = 0ULL; | |
927 | } | |
928 | ||
929 | /* Used for printing the entire verifier state. */ | |
930 | static inline void mark_verifier_state_scratched(struct bpf_verifier_env *env) | |
931 | { | |
932 | env->scratched_regs = ~0U; | |
933 | env->scratched_stack_slots = ~0ULL; | |
934 | } | |
935 | ||
c1e6148c MM |
936 | static inline bool bpf_stack_narrow_access_ok(int off, int fill_size, int spill_size) |
937 | { | |
938 | #ifdef __BIG_ENDIAN | |
939 | off -= spill_size - fill_size; | |
940 | #endif | |
941 | ||
942 | return !(off % BPF_REG_SIZE); | |
943 | } | |
944 | ||
42feb662 AN |
945 | const char *reg_type_str(struct bpf_verifier_env *env, enum bpf_reg_type type); |
946 | const char *dynptr_type_str(enum bpf_dynptr_type type); | |
947 | const char *iter_type_str(const struct btf *btf, u32 btf_id); | |
948 | const char *iter_state_str(enum bpf_iter_state state); | |
949 | ||
950 | void print_verifier_state(struct bpf_verifier_env *env, | |
951 | const struct bpf_func_state *state, bool print_all); | |
952 | void print_insn_state(struct bpf_verifier_env *env, const struct bpf_func_state *state); | |
953 | ||
58e2af8b | 954 | #endif /* _LINUX_BPF_VERIFIER_H */ |