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