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