blob: 519aafabc40cb5294d35da1b2687012949273b78 [file] [log] [blame]
Thomas Gleixner25763b32019-05-28 10:10:09 -07001/* SPDX-License-Identifier: GPL-2.0-only */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002/* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003 */
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 */
Edward Creef1174f72017-08-07 15:26:19 +01009#include <linux/tnum.h>
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +010010
Edward Creeb03c9f92017-08-07 15:26:36 +010011/* 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 */
Alexei Starovoitovbb7f0f92017-12-18 20:12:00 -080015#define BPF_MAX_VAR_OFF (1 << 29)
Edward Creeb03c9f92017-08-07 15:26:36 +010016/* Maximum variable size permitted for ARG_CONST_SIZE[_OR_ZERO]. This ensures
17 * that converting umax_value to int cannot overflow.
18 */
Alexei Starovoitovbb7f0f92017-12-18 20:12:00 -080019#define BPF_MAX_VAR_SIZ (1 << 29)
Josef Bacik48461132016-09-28 10:54:32 -040020
Edward Cree8e9cd9c2017-08-23 15:11:21 +010021/* 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 */
Edward Creedc503a82017-08-15 20:34:35 +010034enum bpf_reg_liveness {
35 REG_LIVE_NONE = 0, /* reg hasn't been read or written this branch */
36 REG_LIVE_READ, /* reg was read, so we're sensitive to initial value */
37 REG_LIVE_WRITTEN, /* reg was written first, screening off later reads */
Alexei Starovoitov9242b5f2018-12-13 11:42:34 -080038 REG_LIVE_DONE = 4, /* liveness won't be updating this register anymore */
Edward Creedc503a82017-08-15 20:34:35 +010039};
40
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +010041struct bpf_reg_state {
Edward Cree679c7822018-08-22 20:02:19 +010042 /* Ordering of fields matters. See states_equal() */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +010043 enum bpf_reg_type type;
44 union {
Edward Creef1174f72017-08-07 15:26:19 +010045 /* valid when type == PTR_TO_PACKET */
46 u16 range;
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +010047
48 /* valid when type == CONST_PTR_TO_MAP | PTR_TO_MAP_VALUE |
49 * PTR_TO_MAP_VALUE_OR_NULL
50 */
51 struct bpf_map *map_ptr;
Daniel Borkmann09625902018-11-01 00:05:52 +010052
53 /* Max size from any of the above. */
54 unsigned long raw;
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +010055 };
Edward Creef1174f72017-08-07 15:26:19 +010056 /* Fixed part of pointer offset, pointer types only */
57 s32 off;
58 /* For PTR_TO_PACKET, used to find other pointers with the same variable
59 * offset, so they can share range knowledge.
60 * For PTR_TO_MAP_VALUE_OR_NULL this is used to share which map value we
61 * came from, when one is tested for != NULL.
Joe Stringerc64b7982018-10-02 13:35:33 -070062 * For PTR_TO_SOCKET this is used to share which pointers retain the
63 * same reference to the socket, to determine proper reference freeing.
Edward Creef1174f72017-08-07 15:26:19 +010064 */
Alexei Starovoitovd2a4dd32016-12-07 10:57:59 -080065 u32 id;
Martin KaFai Lau1b986582019-03-12 10:23:02 -070066 /* PTR_TO_SOCKET and PTR_TO_TCP_SOCK could be a ptr returned
67 * from a pointer-cast helper, bpf_sk_fullsock() and
68 * bpf_tcp_sock().
69 *
70 * Consider the following where "sk" is a reference counted
71 * pointer returned from "sk = bpf_sk_lookup_tcp();":
72 *
73 * 1: sk = bpf_sk_lookup_tcp();
74 * 2: if (!sk) { return 0; }
75 * 3: fullsock = bpf_sk_fullsock(sk);
76 * 4: if (!fullsock) { bpf_sk_release(sk); return 0; }
77 * 5: tp = bpf_tcp_sock(fullsock);
78 * 6: if (!tp) { bpf_sk_release(sk); return 0; }
79 * 7: bpf_sk_release(sk);
80 * 8: snd_cwnd = tp->snd_cwnd; // verifier will complain
81 *
82 * After bpf_sk_release(sk) at line 7, both "fullsock" ptr and
83 * "tp" ptr should be invalidated also. In order to do that,
84 * the reg holding "fullsock" and "sk" need to remember
85 * the original refcounted ptr id (i.e. sk_reg->id) in ref_obj_id
86 * such that the verifier can reset all regs which have
87 * ref_obj_id matching the sk_reg->id.
88 *
89 * sk_reg->ref_obj_id is set to sk_reg->id at line 1.
90 * sk_reg->id will stay as NULL-marking purpose only.
91 * After NULL-marking is done, sk_reg->id can be reset to 0.
92 *
93 * After "fullsock = bpf_sk_fullsock(sk);" at line 3,
94 * fullsock_reg->ref_obj_id is set to sk_reg->ref_obj_id.
95 *
96 * After "tp = bpf_tcp_sock(fullsock);" at line 5,
97 * tp_reg->ref_obj_id is set to fullsock_reg->ref_obj_id
98 * which is the same as sk_reg->ref_obj_id.
99 *
100 * From the verifier perspective, if sk, fullsock and tp
101 * are not NULL, they are the same ptr with different
102 * reg->type. In particular, bpf_sk_release(tp) is also
103 * allowed and has the same effect as bpf_sk_release(sk).
104 */
105 u32 ref_obj_id;
Edward Creef1174f72017-08-07 15:26:19 +0100106 /* For scalar types (SCALAR_VALUE), this represents our knowledge of
107 * the actual value.
108 * For pointer types, this represents the variable part of the offset
109 * from the pointed-to object, and is shared with all bpf_reg_states
110 * with the same id as us.
111 */
112 struct tnum var_off;
Alexei Starovoitovd2a4dd32016-12-07 10:57:59 -0800113 /* Used to determine if any memory access using this register will
Edward Creef1174f72017-08-07 15:26:19 +0100114 * result in a bad access.
115 * These refer to the same value as var_off, not necessarily the actual
116 * contents of the register.
Alexei Starovoitovd2a4dd32016-12-07 10:57:59 -0800117 */
Edward Creeb03c9f92017-08-07 15:26:36 +0100118 s64 smin_value; /* minimum possible (s64)value */
119 s64 smax_value; /* maximum possible (s64)value */
120 u64 umin_value; /* minimum possible (u64)value */
121 u64 umax_value; /* maximum possible (u64)value */
Edward Cree679c7822018-08-22 20:02:19 +0100122 /* parentage chain for liveness checking */
123 struct bpf_reg_state *parent;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800124 /* Inside the callee two registers can be both PTR_TO_STACK like
125 * R1=fp-8 and R2=fp-8, but one of them points to this function stack
126 * while another to the caller's stack. To differentiate them 'frameno'
127 * is used which is an index in bpf_verifier_state->frame[] array
128 * pointing to bpf_func_state.
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800129 */
130 u32 frameno;
Edward Creedc503a82017-08-15 20:34:35 +0100131 enum bpf_reg_liveness live;
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100132};
133
134enum bpf_stack_slot_type {
135 STACK_INVALID, /* nothing was stored in this stack slot */
136 STACK_SPILL, /* register spilled into stack */
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -0800137 STACK_MISC, /* BPF program wrote some data into this slot */
138 STACK_ZERO, /* BPF program wrote constant zero */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100139};
140
141#define BPF_REG_SIZE 8 /* size of eBPF register in bytes */
142
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700143struct bpf_stack_state {
144 struct bpf_reg_state spilled_ptr;
145 u8 slot_type[BPF_REG_SIZE];
146};
147
Joe Stringerfd978bf72018-10-02 13:35:35 -0700148struct bpf_reference_state {
149 /* Track each reference created with a unique id, even if the same
150 * instruction creates the reference multiple times (eg, via CALL).
151 */
152 int id;
153 /* Instruction where the allocation of this reference occurred. This
154 * is used purely to inform the user of a reference leak.
155 */
156 int insn_idx;
157};
158
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100159/* state of the program:
160 * type of all registers and stack info
161 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800162struct bpf_func_state {
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100163 struct bpf_reg_state regs[MAX_BPF_REG];
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800164 /* index of call instruction that called into this func */
165 int callsite;
166 /* stack frame number of this function state from pov of
167 * enclosing bpf_verifier_state.
168 * 0 = main function, 1 = first callee.
169 */
170 u32 frameno;
171 /* subprog number == index within subprog_stack_depth
172 * zero == main subprog
173 */
174 u32 subprogno;
175
Joe Stringerfd978bf72018-10-02 13:35:35 -0700176 /* The following fields should be last. See copy_func_state() */
177 int acquired_refs;
178 struct bpf_reference_state *refs;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700179 int allocated_stack;
180 struct bpf_stack_state *stack;
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100181};
182
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800183#define MAX_CALL_FRAMES 8
184struct bpf_verifier_state {
185 /* call stack tracking */
186 struct bpf_func_state *frame[MAX_CALL_FRAMES];
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800187 u32 curframe;
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800188 u32 active_spin_lock;
Daniel Borkmann979d63d2019-01-03 00:58:34 +0100189 bool speculative;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800190};
191
Joe Stringerf3709f62018-10-02 13:35:29 -0700192#define bpf_get_spilled_reg(slot, frame) \
193 (((slot < frame->allocated_stack / BPF_REG_SIZE) && \
194 (frame->stack[slot].slot_type[0] == STACK_SPILL)) \
195 ? &frame->stack[slot].spilled_ptr : NULL)
196
197/* Iterate over 'frame', setting 'reg' to either NULL or a spilled register. */
198#define bpf_for_each_spilled_reg(iter, frame, reg) \
199 for (iter = 0, reg = bpf_get_spilled_reg(iter, frame); \
200 iter < frame->allocated_stack / BPF_REG_SIZE; \
201 iter++, reg = bpf_get_spilled_reg(iter, frame))
202
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100203/* linked list of verifier states used to prune search */
204struct bpf_verifier_state_list {
205 struct bpf_verifier_state state;
206 struct bpf_verifier_state_list *next;
Alexei Starovoitov9f4686c2019-04-01 21:27:41 -0700207 int miss_cnt, hit_cnt;
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100208};
209
Daniel Borkmann979d63d2019-01-03 00:58:34 +0100210/* Possible states for alu_state member. */
211#define BPF_ALU_SANITIZE_SRC 1U
212#define BPF_ALU_SANITIZE_DST 2U
213#define BPF_ALU_NEG_VALUE (1U << 2)
Daniel Borkmannd3bd7412019-01-06 00:54:37 +0100214#define BPF_ALU_NON_POINTER (1U << 3)
Daniel Borkmann979d63d2019-01-03 00:58:34 +0100215#define BPF_ALU_SANITIZE (BPF_ALU_SANITIZE_SRC | \
216 BPF_ALU_SANITIZE_DST)
217
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100218struct bpf_insn_aux_data {
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -0700219 union {
220 enum bpf_reg_type ptr_type; /* pointer type for load/store insns */
Daniel Borkmannc93552c2018-05-24 02:32:53 +0200221 unsigned long map_state; /* pointer/poison value for maps */
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -0800222 s32 call_imm; /* saved imm field of call insn */
Daniel Borkmann979d63d2019-01-03 00:58:34 +0100223 u32 alu_limit; /* limit for add/sub register with pointer */
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +0200224 struct {
225 u32 map_index; /* index into used_maps[] */
226 u32 map_off; /* offset from value base address */
227 };
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -0700228 };
Yonghong Song23994632017-06-22 15:07:39 -0700229 int ctx_field_size; /* the ctx field size for load insn, maybe 0 */
Alexei Starovoitovaf86ca42018-05-15 09:27:05 -0700230 int sanitize_stack_off; /* stack slot to be cleared */
Alexei Starovoitovc1311872017-11-22 16:42:05 -0800231 bool seen; /* this insn was processed by the verifier */
Daniel Borkmann979d63d2019-01-03 00:58:34 +0100232 u8 alu_state; /* used in combination with alu_limit */
Jakub Kicinski9e4c24e2019-01-22 22:45:23 -0800233 unsigned int orig_idx; /* original instruction index */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100234};
235
236#define MAX_USED_MAPS 64 /* max number of maps accessed by one eBPF program */
237
Jakub Kicinskia2a7d572017-10-09 10:30:15 -0700238#define BPF_VERIFIER_TMP_LOG_SIZE 1024
239
Martin KaFai Laub9193c12018-03-24 11:44:22 -0700240struct bpf_verifier_log {
Jakub Kicinskie7bf8242017-10-09 10:30:10 -0700241 u32 level;
Jakub Kicinskia2a7d572017-10-09 10:30:15 -0700242 char kbuf[BPF_VERIFIER_TMP_LOG_SIZE];
Jakub Kicinskie7bf8242017-10-09 10:30:10 -0700243 char __user *ubuf;
244 u32 len_used;
245 u32 len_total;
246};
247
Martin KaFai Laub9193c12018-03-24 11:44:22 -0700248static inline bool bpf_verifier_log_full(const struct bpf_verifier_log *log)
Jakub Kicinskie7bf8242017-10-09 10:30:10 -0700249{
250 return log->len_used >= log->len_total - 1;
251}
252
Alexei Starovoitov06ee7112019-04-01 21:27:40 -0700253#define BPF_LOG_LEVEL1 1
254#define BPF_LOG_LEVEL2 2
255#define BPF_LOG_STATS 4
256#define BPF_LOG_LEVEL (BPF_LOG_LEVEL1 | BPF_LOG_LEVEL2)
257#define BPF_LOG_MASK (BPF_LOG_LEVEL | BPF_LOG_STATS)
258
Martin KaFai Lau77d2e052018-03-24 11:44:23 -0700259static inline bool bpf_verifier_log_needed(const struct bpf_verifier_log *log)
260{
261 return log->level && log->ubuf && !bpf_verifier_log_full(log);
262}
263
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -0800264#define BPF_MAX_SUBPROGS 256
265
Jiong Wang9c8105b2018-05-02 16:17:18 -0400266struct bpf_subprog_info {
267 u32 start; /* insn idx of function entry point */
Martin KaFai Lauc454a462018-12-07 16:42:25 -0800268 u32 linfo_idx; /* The idx to the main_prog->aux->linfo */
Jiong Wang9c8105b2018-05-02 16:17:18 -0400269 u16 stack_depth; /* max. stack depth used by this function */
270};
271
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100272/* single container for all structs
273 * one verifier_env per bpf_check() call
274 */
275struct bpf_verifier_env {
Daniel Borkmannc08435e2019-01-03 00:58:27 +0100276 u32 insn_idx;
277 u32 prev_insn_idx;
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100278 struct bpf_prog *prog; /* eBPF program being verified */
Jakub Kicinski00176a32017-10-16 16:40:54 -0700279 const struct bpf_verifier_ops *ops;
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100280 struct bpf_verifier_stack_elem *head; /* stack of verifier states to be processed */
281 int stack_size; /* number of states to be processed */
David S. Millere07b98d2017-05-10 11:38:07 -0700282 bool strict_alignment; /* perform strict pointer alignment checks */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700283 struct bpf_verifier_state *cur_state; /* current verifier state */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100284 struct bpf_verifier_state_list **explored_states; /* search pruning optimization */
Alexei Starovoitov9f4686c2019-04-01 21:27:41 -0700285 struct bpf_verifier_state_list *free_list;
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100286 struct bpf_map *used_maps[MAX_USED_MAPS]; /* array of map's used by eBPF program */
287 u32 used_map_cnt; /* number of used maps */
288 u32 id_gen; /* used to generate unique reg IDs */
289 bool allow_ptr_leaks;
290 bool seen_direct_write;
291 struct bpf_insn_aux_data *insn_aux_data; /* array of per-insn state */
Martin KaFai Laud9762e82018-12-13 10:41:48 -0800292 const struct bpf_line_info *prev_linfo;
Martin KaFai Laub9193c12018-03-24 11:44:22 -0700293 struct bpf_verifier_log log;
Jiong Wang9c8105b2018-05-02 16:17:18 -0400294 struct bpf_subprog_info subprog_info[BPF_MAX_SUBPROGS + 1];
Alexei Starovoitov7df737e2019-04-19 07:44:54 -0700295 struct {
296 int *insn_state;
297 int *insn_stack;
298 int cur_stack;
299 } cfg;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -0800300 u32 subprog_cnt;
Alexei Starovoitov06ee7112019-04-01 21:27:40 -0700301 /* number of instructions analyzed by the verifier */
302 u32 insn_processed;
303 /* total verification time */
304 u64 verification_time;
305 /* maximum number of verifier states kept in 'branching' instructions */
306 u32 max_states_per_insn;
307 /* total number of allocated verifier states */
308 u32 total_states;
309 /* some states are freed during program analysis.
310 * this is peak number of states. this number dominates kernel
311 * memory consumption during verification
312 */
313 u32 peak_states;
314 /* longest register parentage chain walked for liveness marking */
315 u32 longest_mark_read_walk;
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100316};
317
Mathieu Malaterrebe2d04d2018-05-16 22:27:41 +0200318__printf(2, 0) void bpf_verifier_vlog(struct bpf_verifier_log *log,
319 const char *fmt, va_list args);
Quentin Monnet430e68d2018-01-10 12:26:06 +0000320__printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env,
321 const char *fmt, ...);
322
Joe Stringerfd978bf72018-10-02 13:35:35 -0700323static inline struct bpf_func_state *cur_func(struct bpf_verifier_env *env)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700324{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800325 struct bpf_verifier_state *cur = env->cur_state;
326
Joe Stringerfd978bf72018-10-02 13:35:35 -0700327 return cur->frame[cur->curframe];
328}
329
330static inline struct bpf_reg_state *cur_regs(struct bpf_verifier_env *env)
331{
332 return cur_func(env)->regs;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700333}
334
Quentin Monneta40a2632018-11-09 13:03:31 +0000335int bpf_prog_offload_verifier_prep(struct bpf_prog *prog);
Jakub Kicinskicae19272017-12-27 18:39:05 -0800336int bpf_prog_offload_verify_insn(struct bpf_verifier_env *env,
337 int insn_idx, int prev_insn_idx);
Quentin Monnetc941ce92018-10-07 12:56:47 +0100338int bpf_prog_offload_finalize(struct bpf_verifier_env *env);
Jakub Kicinski08ca90a2019-01-22 22:45:24 -0800339void
340bpf_prog_offload_replace_insn(struct bpf_verifier_env *env, u32 off,
341 struct bpf_insn *insn);
342void
343bpf_prog_offload_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt);
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700344
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100345#endif /* _LINUX_BPF_VERIFIER_H */