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/filter.h> /* for MAX_BPF_STACK */ 9#include <linux/tnum.h> 10 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 */ 15#define BPF_MAX_VAR_OFF (1 << 29) 16/* Maximum variable size permitted for ARG_CONST_SIZE[_OR_ZERO]. This ensures 17 * that converting umax_value to int cannot overflow. 18 */ 19#define BPF_MAX_VAR_SIZ (1 << 29) 20 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 */ 34enum 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 */ 38 REG_LIVE_DONE = 4, /* liveness won't be updating this register anymore */ 39}; 40 41struct bpf_reg_state { 42 /* Ordering of fields matters. See states_equal() */ 43 enum bpf_reg_type type; 44 union { 45 /* valid when type == PTR_TO_PACKET */ 46 u16 range; 47 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; 52 53 /* Max size from any of the above. */ 54 unsigned long raw; 55 }; 56 /* 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. 62 * For PTR_TO_SOCKET this is used to share which pointers retain the 63 * same reference to the socket, to determine proper reference freeing. 64 */ 65 u32 id; 66 /* 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; 106 /* 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; 113 /* Used to determine if any memory access using this register will 114 * result in a bad access. 115 * These refer to the same value as var_off, not necessarily the actual 116 * contents of the register. 117 */ 118 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 */ 122 /* parentage chain for liveness checking */ 123 struct bpf_reg_state *parent; 124 /* 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. 129 */ 130 u32 frameno; 131 enum bpf_reg_liveness live; 132}; 133 134enum bpf_stack_slot_type { 135 STACK_INVALID, /* nothing was stored in this stack slot */ 136 STACK_SPILL, /* register spilled into stack */ 137 STACK_MISC, /* BPF program wrote some data into this slot */ 138 STACK_ZERO, /* BPF program wrote constant zero */ 139}; 140 141#define BPF_REG_SIZE 8 /* size of eBPF register in bytes */ 142 143struct bpf_stack_state { 144 struct bpf_reg_state spilled_ptr; 145 u8 slot_type[BPF_REG_SIZE]; 146}; 147 148struct 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 159/* state of the program: 160 * type of all registers and stack info 161 */ 162struct bpf_func_state { 163 struct bpf_reg_state regs[MAX_BPF_REG]; 164 /* 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 176 /* The following fields should be last. See copy_func_state() */ 177 int acquired_refs; 178 struct bpf_reference_state *refs; 179 int allocated_stack; 180 struct bpf_stack_state *stack; 181}; 182 183#define MAX_CALL_FRAMES 8 184struct bpf_verifier_state { 185 /* call stack tracking */ 186 struct bpf_func_state *frame[MAX_CALL_FRAMES]; 187 u32 curframe; 188 u32 active_spin_lock; 189 bool speculative; 190}; 191 192#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 203/* 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; 207 int miss_cnt, hit_cnt; 208}; 209 210/* 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) 214#define BPF_ALU_NON_POINTER (1U << 3) 215#define BPF_ALU_SANITIZE (BPF_ALU_SANITIZE_SRC | \ 216 BPF_ALU_SANITIZE_DST) 217 218struct bpf_insn_aux_data { 219 union { 220 enum bpf_reg_type ptr_type; /* pointer type for load/store insns */ 221 unsigned long map_state; /* pointer/poison value for maps */ 222 s32 call_imm; /* saved imm field of call insn */ 223 u32 alu_limit; /* limit for add/sub register with pointer */ 224 struct { 225 u32 map_index; /* index into used_maps[] */ 226 u32 map_off; /* offset from value base address */ 227 }; 228 }; 229 int ctx_field_size; /* the ctx field size for load insn, maybe 0 */ 230 int sanitize_stack_off; /* stack slot to be cleared */ 231 bool seen; /* this insn was processed by the verifier */ 232 u8 alu_state; /* used in combination with alu_limit */ 233 unsigned int orig_idx; /* original instruction index */ 234}; 235 236#define MAX_USED_MAPS 64 /* max number of maps accessed by one eBPF program */ 237 238#define BPF_VERIFIER_TMP_LOG_SIZE 1024 239 240struct bpf_verifier_log { 241 u32 level; 242 char kbuf[BPF_VERIFIER_TMP_LOG_SIZE]; 243 char __user *ubuf; 244 u32 len_used; 245 u32 len_total; 246}; 247 248static inline bool bpf_verifier_log_full(const struct bpf_verifier_log *log) 249{ 250 return log->len_used >= log->len_total - 1; 251} 252 253#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 259static 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 264#define BPF_MAX_SUBPROGS 256 265 266struct bpf_subprog_info { 267 u32 start; /* insn idx of function entry point */ 268 u32 linfo_idx; /* The idx to the main_prog->aux->linfo */ 269 u16 stack_depth; /* max. stack depth used by this function */ 270}; 271 272/* single container for all structs 273 * one verifier_env per bpf_check() call 274 */ 275struct bpf_verifier_env { 276 u32 insn_idx; 277 u32 prev_insn_idx; 278 struct bpf_prog *prog; /* eBPF program being verified */ 279 const struct bpf_verifier_ops *ops; 280 struct bpf_verifier_stack_elem *head; /* stack of verifier states to be processed */ 281 int stack_size; /* number of states to be processed */ 282 bool strict_alignment; /* perform strict pointer alignment checks */ 283 struct bpf_verifier_state *cur_state; /* current verifier state */ 284 struct bpf_verifier_state_list **explored_states; /* search pruning optimization */ 285 struct bpf_verifier_state_list *free_list; 286 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 */ 292 const struct bpf_line_info *prev_linfo; 293 struct bpf_verifier_log log; 294 struct bpf_subprog_info subprog_info[BPF_MAX_SUBPROGS + 1]; 295 struct { 296 int *insn_state; 297 int *insn_stack; 298 int cur_stack; 299 } cfg; 300 u32 subprog_cnt; 301 /* 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; 316}; 317 318__printf(2, 0) void bpf_verifier_vlog(struct bpf_verifier_log *log, 319 const char *fmt, va_list args); 320__printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env, 321 const char *fmt, ...); 322 323static inline struct bpf_func_state *cur_func(struct bpf_verifier_env *env) 324{ 325 struct bpf_verifier_state *cur = env->cur_state; 326 327 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; 333} 334 335int bpf_prog_offload_verifier_prep(struct bpf_prog *prog); 336int bpf_prog_offload_verify_insn(struct bpf_verifier_env *env, 337 int insn_idx, int prev_insn_idx); 338int bpf_prog_offload_finalize(struct bpf_verifier_env *env); 339void 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); 344 345#endif /* _LINUX_BPF_VERIFIER_H */ 346