at v6.4 26 kB view raw
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/* size of type_str_buf in bpf_verifier. */ 22#define TYPE_STR_BUF_LEN 128 23 24/* Liveness marks, used for registers and spilled-regs (in stack slots). 25 * Read marks propagate upwards until they find a write mark; they record that 26 * "one of this state's descendants read this reg" (and therefore the reg is 27 * relevant for states_equal() checks). 28 * Write marks collect downwards and do not propagate; they record that "the 29 * straight-line code that reached this state (from its parent) wrote this reg" 30 * (and therefore that reads propagated from this state or its descendants 31 * should not propagate to its parent). 32 * A state with a write mark can receive read marks; it just won't propagate 33 * them to its parent, since the write mark is a property, not of the state, 34 * but of the link between it and its parent. See mark_reg_read() and 35 * mark_stack_slot_read() in kernel/bpf/verifier.c. 36 */ 37enum bpf_reg_liveness { 38 REG_LIVE_NONE = 0, /* reg hasn't been read or written this branch */ 39 REG_LIVE_READ32 = 0x1, /* reg was read, so we're sensitive to initial value */ 40 REG_LIVE_READ64 = 0x2, /* likewise, but full 64-bit content matters */ 41 REG_LIVE_READ = REG_LIVE_READ32 | REG_LIVE_READ64, 42 REG_LIVE_WRITTEN = 0x4, /* reg was written first, screening off later reads */ 43 REG_LIVE_DONE = 0x8, /* liveness won't be updating this register anymore */ 44}; 45 46/* For every reg representing a map value or allocated object pointer, 47 * we consider the tuple of (ptr, id) for them to be unique in verifier 48 * context and conside them to not alias each other for the purposes of 49 * tracking lock state. 50 */ 51struct bpf_active_lock { 52 /* This can either be reg->map_ptr or reg->btf. If ptr is NULL, 53 * there's no active lock held, and other fields have no 54 * meaning. If non-NULL, it indicates that a lock is held and 55 * id member has the reg->id of the register which can be >= 0. 56 */ 57 void *ptr; 58 /* This will be reg->id */ 59 u32 id; 60}; 61 62#define ITER_PREFIX "bpf_iter_" 63 64enum bpf_iter_state { 65 BPF_ITER_STATE_INVALID, /* for non-first slot */ 66 BPF_ITER_STATE_ACTIVE, 67 BPF_ITER_STATE_DRAINED, 68}; 69 70struct bpf_reg_state { 71 /* Ordering of fields matters. See states_equal() */ 72 enum bpf_reg_type type; 73 /* Fixed part of pointer offset, pointer types only */ 74 s32 off; 75 union { 76 /* valid when type == PTR_TO_PACKET */ 77 int range; 78 79 /* valid when type == CONST_PTR_TO_MAP | PTR_TO_MAP_VALUE | 80 * PTR_TO_MAP_VALUE_OR_NULL 81 */ 82 struct { 83 struct bpf_map *map_ptr; 84 /* To distinguish map lookups from outer map 85 * the map_uid is non-zero for registers 86 * pointing to inner maps. 87 */ 88 u32 map_uid; 89 }; 90 91 /* for PTR_TO_BTF_ID */ 92 struct { 93 struct btf *btf; 94 u32 btf_id; 95 }; 96 97 struct { /* for PTR_TO_MEM | PTR_TO_MEM_OR_NULL */ 98 u32 mem_size; 99 u32 dynptr_id; /* for dynptr slices */ 100 }; 101 102 /* For dynptr stack slots */ 103 struct { 104 enum bpf_dynptr_type type; 105 /* A dynptr is 16 bytes so it takes up 2 stack slots. 106 * We need to track which slot is the first slot 107 * to protect against cases where the user may try to 108 * pass in an address starting at the second slot of the 109 * dynptr. 110 */ 111 bool first_slot; 112 } dynptr; 113 114 /* For bpf_iter stack slots */ 115 struct { 116 /* BTF container and BTF type ID describing 117 * struct bpf_iter_<type> of an iterator state 118 */ 119 struct btf *btf; 120 u32 btf_id; 121 /* packing following two fields to fit iter state into 16 bytes */ 122 enum bpf_iter_state state:2; 123 int depth:30; 124 } iter; 125 126 /* Max size from any of the above. */ 127 struct { 128 unsigned long raw1; 129 unsigned long raw2; 130 } raw; 131 132 u32 subprogno; /* for PTR_TO_FUNC */ 133 }; 134 /* For scalar types (SCALAR_VALUE), this represents our knowledge of 135 * the actual value. 136 * For pointer types, this represents the variable part of the offset 137 * from the pointed-to object, and is shared with all bpf_reg_states 138 * with the same id as us. 139 */ 140 struct tnum var_off; 141 /* Used to determine if any memory access using this register will 142 * result in a bad access. 143 * These refer to the same value as var_off, not necessarily the actual 144 * contents of the register. 145 */ 146 s64 smin_value; /* minimum possible (s64)value */ 147 s64 smax_value; /* maximum possible (s64)value */ 148 u64 umin_value; /* minimum possible (u64)value */ 149 u64 umax_value; /* maximum possible (u64)value */ 150 s32 s32_min_value; /* minimum possible (s32)value */ 151 s32 s32_max_value; /* maximum possible (s32)value */ 152 u32 u32_min_value; /* minimum possible (u32)value */ 153 u32 u32_max_value; /* maximum possible (u32)value */ 154 /* For PTR_TO_PACKET, used to find other pointers with the same variable 155 * offset, so they can share range knowledge. 156 * For PTR_TO_MAP_VALUE_OR_NULL this is used to share which map value we 157 * came from, when one is tested for != NULL. 158 * For PTR_TO_MEM_OR_NULL this is used to identify memory allocation 159 * for the purpose of tracking that it's freed. 160 * For PTR_TO_SOCKET this is used to share which pointers retain the 161 * same reference to the socket, to determine proper reference freeing. 162 * For stack slots that are dynptrs, this is used to track references to 163 * the dynptr to determine proper reference freeing. 164 * Similarly to dynptrs, we use ID to track "belonging" of a reference 165 * to a specific instance of bpf_iter. 166 */ 167 u32 id; 168 /* PTR_TO_SOCKET and PTR_TO_TCP_SOCK could be a ptr returned 169 * from a pointer-cast helper, bpf_sk_fullsock() and 170 * bpf_tcp_sock(). 171 * 172 * Consider the following where "sk" is a reference counted 173 * pointer returned from "sk = bpf_sk_lookup_tcp();": 174 * 175 * 1: sk = bpf_sk_lookup_tcp(); 176 * 2: if (!sk) { return 0; } 177 * 3: fullsock = bpf_sk_fullsock(sk); 178 * 4: if (!fullsock) { bpf_sk_release(sk); return 0; } 179 * 5: tp = bpf_tcp_sock(fullsock); 180 * 6: if (!tp) { bpf_sk_release(sk); return 0; } 181 * 7: bpf_sk_release(sk); 182 * 8: snd_cwnd = tp->snd_cwnd; // verifier will complain 183 * 184 * After bpf_sk_release(sk) at line 7, both "fullsock" ptr and 185 * "tp" ptr should be invalidated also. In order to do that, 186 * the reg holding "fullsock" and "sk" need to remember 187 * the original refcounted ptr id (i.e. sk_reg->id) in ref_obj_id 188 * such that the verifier can reset all regs which have 189 * ref_obj_id matching the sk_reg->id. 190 * 191 * sk_reg->ref_obj_id is set to sk_reg->id at line 1. 192 * sk_reg->id will stay as NULL-marking purpose only. 193 * After NULL-marking is done, sk_reg->id can be reset to 0. 194 * 195 * After "fullsock = bpf_sk_fullsock(sk);" at line 3, 196 * fullsock_reg->ref_obj_id is set to sk_reg->ref_obj_id. 197 * 198 * After "tp = bpf_tcp_sock(fullsock);" at line 5, 199 * tp_reg->ref_obj_id is set to fullsock_reg->ref_obj_id 200 * which is the same as sk_reg->ref_obj_id. 201 * 202 * From the verifier perspective, if sk, fullsock and tp 203 * are not NULL, they are the same ptr with different 204 * reg->type. In particular, bpf_sk_release(tp) is also 205 * allowed and has the same effect as bpf_sk_release(sk). 206 */ 207 u32 ref_obj_id; 208 /* parentage chain for liveness checking */ 209 struct bpf_reg_state *parent; 210 /* Inside the callee two registers can be both PTR_TO_STACK like 211 * R1=fp-8 and R2=fp-8, but one of them points to this function stack 212 * while another to the caller's stack. To differentiate them 'frameno' 213 * is used which is an index in bpf_verifier_state->frame[] array 214 * pointing to bpf_func_state. 215 */ 216 u32 frameno; 217 /* Tracks subreg definition. The stored value is the insn_idx of the 218 * writing insn. This is safe because subreg_def is used before any insn 219 * patching which only happens after main verification finished. 220 */ 221 s32 subreg_def; 222 enum bpf_reg_liveness live; 223 /* if (!precise && SCALAR_VALUE) min/max/tnum don't affect safety */ 224 bool precise; 225}; 226 227enum bpf_stack_slot_type { 228 STACK_INVALID, /* nothing was stored in this stack slot */ 229 STACK_SPILL, /* register spilled into stack */ 230 STACK_MISC, /* BPF program wrote some data into this slot */ 231 STACK_ZERO, /* BPF program wrote constant zero */ 232 /* A dynptr is stored in this stack slot. The type of dynptr 233 * is stored in bpf_stack_state->spilled_ptr.dynptr.type 234 */ 235 STACK_DYNPTR, 236 STACK_ITER, 237}; 238 239#define BPF_REG_SIZE 8 /* size of eBPF register in bytes */ 240 241#define BPF_DYNPTR_SIZE sizeof(struct bpf_dynptr_kern) 242#define BPF_DYNPTR_NR_SLOTS (BPF_DYNPTR_SIZE / BPF_REG_SIZE) 243 244struct bpf_stack_state { 245 struct bpf_reg_state spilled_ptr; 246 u8 slot_type[BPF_REG_SIZE]; 247}; 248 249struct bpf_reference_state { 250 /* Track each reference created with a unique id, even if the same 251 * instruction creates the reference multiple times (eg, via CALL). 252 */ 253 int id; 254 /* Instruction where the allocation of this reference occurred. This 255 * is used purely to inform the user of a reference leak. 256 */ 257 int insn_idx; 258 /* There can be a case like: 259 * main (frame 0) 260 * cb (frame 1) 261 * func (frame 3) 262 * cb (frame 4) 263 * Hence for frame 4, if callback_ref just stored boolean, it would be 264 * impossible to distinguish nested callback refs. Hence store the 265 * frameno and compare that to callback_ref in check_reference_leak when 266 * exiting a callback function. 267 */ 268 int callback_ref; 269}; 270 271/* state of the program: 272 * type of all registers and stack info 273 */ 274struct bpf_func_state { 275 struct bpf_reg_state regs[MAX_BPF_REG]; 276 /* index of call instruction that called into this func */ 277 int callsite; 278 /* stack frame number of this function state from pov of 279 * enclosing bpf_verifier_state. 280 * 0 = main function, 1 = first callee. 281 */ 282 u32 frameno; 283 /* subprog number == index within subprog_info 284 * zero == main subprog 285 */ 286 u32 subprogno; 287 /* Every bpf_timer_start will increment async_entry_cnt. 288 * It's used to distinguish: 289 * void foo(void) { for(;;); } 290 * void foo(void) { bpf_timer_set_callback(,foo); } 291 */ 292 u32 async_entry_cnt; 293 bool in_callback_fn; 294 struct tnum callback_ret_range; 295 bool in_async_callback_fn; 296 297 /* The following fields should be last. See copy_func_state() */ 298 int acquired_refs; 299 struct bpf_reference_state *refs; 300 int allocated_stack; 301 struct bpf_stack_state *stack; 302}; 303 304struct bpf_idx_pair { 305 u32 prev_idx; 306 u32 idx; 307}; 308 309struct bpf_id_pair { 310 u32 old; 311 u32 cur; 312}; 313 314#define MAX_CALL_FRAMES 8 315/* Maximum number of register states that can exist at once */ 316#define BPF_ID_MAP_SIZE ((MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE) * MAX_CALL_FRAMES) 317struct bpf_verifier_state { 318 /* call stack tracking */ 319 struct bpf_func_state *frame[MAX_CALL_FRAMES]; 320 struct bpf_verifier_state *parent; 321 /* 322 * 'branches' field is the number of branches left to explore: 323 * 0 - all possible paths from this state reached bpf_exit or 324 * were safely pruned 325 * 1 - at least one path is being explored. 326 * This state hasn't reached bpf_exit 327 * 2 - at least two paths are being explored. 328 * This state is an immediate parent of two children. 329 * One is fallthrough branch with branches==1 and another 330 * state is pushed into stack (to be explored later) also with 331 * branches==1. The parent of this state has branches==1. 332 * The verifier state tree connected via 'parent' pointer looks like: 333 * 1 334 * 1 335 * 2 -> 1 (first 'if' pushed into stack) 336 * 1 337 * 2 -> 1 (second 'if' pushed into stack) 338 * 1 339 * 1 340 * 1 bpf_exit. 341 * 342 * Once do_check() reaches bpf_exit, it calls update_branch_counts() 343 * and the verifier state tree will look: 344 * 1 345 * 1 346 * 2 -> 1 (first 'if' pushed into stack) 347 * 1 348 * 1 -> 1 (second 'if' pushed into stack) 349 * 0 350 * 0 351 * 0 bpf_exit. 352 * After pop_stack() the do_check() will resume at second 'if'. 353 * 354 * If is_state_visited() sees a state with branches > 0 it means 355 * there is a loop. If such state is exactly equal to the current state 356 * it's an infinite loop. Note states_equal() checks for states 357 * equivalency, so two states being 'states_equal' does not mean 358 * infinite loop. The exact comparison is provided by 359 * states_maybe_looping() function. It's a stronger pre-check and 360 * much faster than states_equal(). 361 * 362 * This algorithm may not find all possible infinite loops or 363 * loop iteration count may be too high. 364 * In such cases BPF_COMPLEXITY_LIMIT_INSNS limit kicks in. 365 */ 366 u32 branches; 367 u32 insn_idx; 368 u32 curframe; 369 370 struct bpf_active_lock active_lock; 371 bool speculative; 372 bool active_rcu_lock; 373 374 /* first and last insn idx of this verifier state */ 375 u32 first_insn_idx; 376 u32 last_insn_idx; 377 /* jmp history recorded from first to last. 378 * backtracking is using it to go from last to first. 379 * For most states jmp_history_cnt is [0-3]. 380 * For loops can go up to ~40. 381 */ 382 struct bpf_idx_pair *jmp_history; 383 u32 jmp_history_cnt; 384}; 385 386#define bpf_get_spilled_reg(slot, frame) \ 387 (((slot < frame->allocated_stack / BPF_REG_SIZE) && \ 388 (frame->stack[slot].slot_type[0] == STACK_SPILL)) \ 389 ? &frame->stack[slot].spilled_ptr : NULL) 390 391/* Iterate over 'frame', setting 'reg' to either NULL or a spilled register. */ 392#define bpf_for_each_spilled_reg(iter, frame, reg) \ 393 for (iter = 0, reg = bpf_get_spilled_reg(iter, frame); \ 394 iter < frame->allocated_stack / BPF_REG_SIZE; \ 395 iter++, reg = bpf_get_spilled_reg(iter, frame)) 396 397/* Invoke __expr over regsiters in __vst, setting __state and __reg */ 398#define bpf_for_each_reg_in_vstate(__vst, __state, __reg, __expr) \ 399 ({ \ 400 struct bpf_verifier_state *___vstate = __vst; \ 401 int ___i, ___j; \ 402 for (___i = 0; ___i <= ___vstate->curframe; ___i++) { \ 403 struct bpf_reg_state *___regs; \ 404 __state = ___vstate->frame[___i]; \ 405 ___regs = __state->regs; \ 406 for (___j = 0; ___j < MAX_BPF_REG; ___j++) { \ 407 __reg = &___regs[___j]; \ 408 (void)(__expr); \ 409 } \ 410 bpf_for_each_spilled_reg(___j, __state, __reg) { \ 411 if (!__reg) \ 412 continue; \ 413 (void)(__expr); \ 414 } \ 415 } \ 416 }) 417 418/* linked list of verifier states used to prune search */ 419struct bpf_verifier_state_list { 420 struct bpf_verifier_state state; 421 struct bpf_verifier_state_list *next; 422 int miss_cnt, hit_cnt; 423}; 424 425struct bpf_loop_inline_state { 426 unsigned int initialized:1; /* set to true upon first entry */ 427 unsigned int fit_for_inline:1; /* true if callback function is the same 428 * at each call and flags are always zero 429 */ 430 u32 callback_subprogno; /* valid when fit_for_inline is true */ 431}; 432 433/* Possible states for alu_state member. */ 434#define BPF_ALU_SANITIZE_SRC (1U << 0) 435#define BPF_ALU_SANITIZE_DST (1U << 1) 436#define BPF_ALU_NEG_VALUE (1U << 2) 437#define BPF_ALU_NON_POINTER (1U << 3) 438#define BPF_ALU_IMMEDIATE (1U << 4) 439#define BPF_ALU_SANITIZE (BPF_ALU_SANITIZE_SRC | \ 440 BPF_ALU_SANITIZE_DST) 441 442struct bpf_insn_aux_data { 443 union { 444 enum bpf_reg_type ptr_type; /* pointer type for load/store insns */ 445 unsigned long map_ptr_state; /* pointer/poison value for maps */ 446 s32 call_imm; /* saved imm field of call insn */ 447 u32 alu_limit; /* limit for add/sub register with pointer */ 448 struct { 449 u32 map_index; /* index into used_maps[] */ 450 u32 map_off; /* offset from value base address */ 451 }; 452 struct { 453 enum bpf_reg_type reg_type; /* type of pseudo_btf_id */ 454 union { 455 struct { 456 struct btf *btf; 457 u32 btf_id; /* btf_id for struct typed var */ 458 }; 459 u32 mem_size; /* mem_size for non-struct typed var */ 460 }; 461 } btf_var; 462 /* if instruction is a call to bpf_loop this field tracks 463 * the state of the relevant registers to make decision about inlining 464 */ 465 struct bpf_loop_inline_state loop_inline_state; 466 }; 467 union { 468 /* remember the size of type passed to bpf_obj_new to rewrite R1 */ 469 u64 obj_new_size; 470 /* remember the offset of node field within type to rewrite */ 471 u64 insert_off; 472 }; 473 struct btf_struct_meta *kptr_struct_meta; 474 u64 map_key_state; /* constant (32 bit) key tracking for maps */ 475 int ctx_field_size; /* the ctx field size for load insn, maybe 0 */ 476 u32 seen; /* this insn was processed by the verifier at env->pass_cnt */ 477 bool sanitize_stack_spill; /* subject to Spectre v4 sanitation */ 478 bool zext_dst; /* this insn zero extends dst reg */ 479 bool storage_get_func_atomic; /* bpf_*_storage_get() with atomic memory alloc */ 480 bool is_iter_next; /* bpf_iter_<type>_next() kfunc call */ 481 u8 alu_state; /* used in combination with alu_limit */ 482 483 /* below fields are initialized once */ 484 unsigned int orig_idx; /* original instruction index */ 485 bool jmp_point; 486 bool prune_point; 487 /* ensure we check state equivalence and save state checkpoint and 488 * this instruction, regardless of any heuristics 489 */ 490 bool force_checkpoint; 491}; 492 493#define MAX_USED_MAPS 64 /* max number of maps accessed by one eBPF program */ 494#define MAX_USED_BTFS 64 /* max number of BTFs accessed by one BPF program */ 495 496#define BPF_VERIFIER_TMP_LOG_SIZE 1024 497 498struct bpf_verifier_log { 499 /* Logical start and end positions of a "log window" of the verifier log. 500 * start_pos == 0 means we haven't truncated anything. 501 * Once truncation starts to happen, start_pos + len_total == end_pos, 502 * except during log reset situations, in which (end_pos - start_pos) 503 * might get smaller than len_total (see bpf_vlog_reset()). 504 * Generally, (end_pos - start_pos) gives number of useful data in 505 * user log buffer. 506 */ 507 u64 start_pos; 508 u64 end_pos; 509 char __user *ubuf; 510 u32 level; 511 u32 len_total; 512 u32 len_max; 513 char kbuf[BPF_VERIFIER_TMP_LOG_SIZE]; 514}; 515 516#define BPF_LOG_LEVEL1 1 517#define BPF_LOG_LEVEL2 2 518#define BPF_LOG_STATS 4 519#define BPF_LOG_FIXED 8 520#define BPF_LOG_LEVEL (BPF_LOG_LEVEL1 | BPF_LOG_LEVEL2) 521#define BPF_LOG_MASK (BPF_LOG_LEVEL | BPF_LOG_STATS | BPF_LOG_FIXED) 522#define BPF_LOG_KERNEL (BPF_LOG_MASK + 1) /* kernel internal flag */ 523#define BPF_LOG_MIN_ALIGNMENT 8U 524#define BPF_LOG_ALIGNMENT 40U 525 526static inline bool bpf_verifier_log_needed(const struct bpf_verifier_log *log) 527{ 528 return log && log->level; 529} 530 531#define BPF_MAX_SUBPROGS 256 532 533struct bpf_subprog_info { 534 /* 'start' has to be the first field otherwise find_subprog() won't work */ 535 u32 start; /* insn idx of function entry point */ 536 u32 linfo_idx; /* The idx to the main_prog->aux->linfo */ 537 u16 stack_depth; /* max. stack depth used by this function */ 538 bool has_tail_call; 539 bool tail_call_reachable; 540 bool has_ld_abs; 541 bool is_async_cb; 542}; 543 544/* single container for all structs 545 * one verifier_env per bpf_check() call 546 */ 547struct bpf_verifier_env { 548 u32 insn_idx; 549 u32 prev_insn_idx; 550 struct bpf_prog *prog; /* eBPF program being verified */ 551 const struct bpf_verifier_ops *ops; 552 struct bpf_verifier_stack_elem *head; /* stack of verifier states to be processed */ 553 int stack_size; /* number of states to be processed */ 554 bool strict_alignment; /* perform strict pointer alignment checks */ 555 bool test_state_freq; /* test verifier with different pruning frequency */ 556 struct bpf_verifier_state *cur_state; /* current verifier state */ 557 struct bpf_verifier_state_list **explored_states; /* search pruning optimization */ 558 struct bpf_verifier_state_list *free_list; 559 struct bpf_map *used_maps[MAX_USED_MAPS]; /* array of map's used by eBPF program */ 560 struct btf_mod_pair used_btfs[MAX_USED_BTFS]; /* array of BTF's used by BPF program */ 561 u32 used_map_cnt; /* number of used maps */ 562 u32 used_btf_cnt; /* number of used BTF objects */ 563 u32 id_gen; /* used to generate unique reg IDs */ 564 bool explore_alu_limits; 565 bool allow_ptr_leaks; 566 bool allow_uninit_stack; 567 bool bpf_capable; 568 bool bypass_spec_v1; 569 bool bypass_spec_v4; 570 bool seen_direct_write; 571 struct bpf_insn_aux_data *insn_aux_data; /* array of per-insn state */ 572 const struct bpf_line_info *prev_linfo; 573 struct bpf_verifier_log log; 574 struct bpf_subprog_info subprog_info[BPF_MAX_SUBPROGS + 1]; 575 struct bpf_id_pair idmap_scratch[BPF_ID_MAP_SIZE]; 576 struct { 577 int *insn_state; 578 int *insn_stack; 579 int cur_stack; 580 } cfg; 581 u32 pass_cnt; /* number of times do_check() was called */ 582 u32 subprog_cnt; 583 /* number of instructions analyzed by the verifier */ 584 u32 prev_insn_processed, insn_processed; 585 /* number of jmps, calls, exits analyzed so far */ 586 u32 prev_jmps_processed, jmps_processed; 587 /* total verification time */ 588 u64 verification_time; 589 /* maximum number of verifier states kept in 'branching' instructions */ 590 u32 max_states_per_insn; 591 /* total number of allocated verifier states */ 592 u32 total_states; 593 /* some states are freed during program analysis. 594 * this is peak number of states. this number dominates kernel 595 * memory consumption during verification 596 */ 597 u32 peak_states; 598 /* longest register parentage chain walked for liveness marking */ 599 u32 longest_mark_read_walk; 600 bpfptr_t fd_array; 601 602 /* bit mask to keep track of whether a register has been accessed 603 * since the last time the function state was printed 604 */ 605 u32 scratched_regs; 606 /* Same as scratched_regs but for stack slots */ 607 u64 scratched_stack_slots; 608 u64 prev_log_pos, prev_insn_print_pos; 609 /* buffer used in reg_type_str() to generate reg_type string */ 610 char type_str_buf[TYPE_STR_BUF_LEN]; 611}; 612 613__printf(2, 0) void bpf_verifier_vlog(struct bpf_verifier_log *log, 614 const char *fmt, va_list args); 615__printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env, 616 const char *fmt, ...); 617__printf(2, 3) void bpf_log(struct bpf_verifier_log *log, 618 const char *fmt, ...); 619int bpf_vlog_init(struct bpf_verifier_log *log, u32 log_level, 620 char __user *log_buf, u32 log_size); 621void bpf_vlog_reset(struct bpf_verifier_log *log, u64 new_pos); 622int bpf_vlog_finalize(struct bpf_verifier_log *log, u32 *log_size_actual); 623 624static inline struct bpf_func_state *cur_func(struct bpf_verifier_env *env) 625{ 626 struct bpf_verifier_state *cur = env->cur_state; 627 628 return cur->frame[cur->curframe]; 629} 630 631static inline struct bpf_reg_state *cur_regs(struct bpf_verifier_env *env) 632{ 633 return cur_func(env)->regs; 634} 635 636int bpf_prog_offload_verifier_prep(struct bpf_prog *prog); 637int bpf_prog_offload_verify_insn(struct bpf_verifier_env *env, 638 int insn_idx, int prev_insn_idx); 639int bpf_prog_offload_finalize(struct bpf_verifier_env *env); 640void 641bpf_prog_offload_replace_insn(struct bpf_verifier_env *env, u32 off, 642 struct bpf_insn *insn); 643void 644bpf_prog_offload_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt); 645 646int check_ptr_off_reg(struct bpf_verifier_env *env, 647 const struct bpf_reg_state *reg, int regno); 648int check_func_arg_reg_off(struct bpf_verifier_env *env, 649 const struct bpf_reg_state *reg, int regno, 650 enum bpf_arg_type arg_type); 651int check_mem_reg(struct bpf_verifier_env *env, struct bpf_reg_state *reg, 652 u32 regno, u32 mem_size); 653 654/* this lives here instead of in bpf.h because it needs to dereference tgt_prog */ 655static inline u64 bpf_trampoline_compute_key(const struct bpf_prog *tgt_prog, 656 struct btf *btf, u32 btf_id) 657{ 658 if (tgt_prog) 659 return ((u64)tgt_prog->aux->id << 32) | btf_id; 660 else 661 return ((u64)btf_obj_id(btf) << 32) | 0x80000000 | btf_id; 662} 663 664/* unpack the IDs from the key as constructed above */ 665static inline void bpf_trampoline_unpack_key(u64 key, u32 *obj_id, u32 *btf_id) 666{ 667 if (obj_id) 668 *obj_id = key >> 32; 669 if (btf_id) 670 *btf_id = key & 0x7FFFFFFF; 671} 672 673int bpf_check_attach_target(struct bpf_verifier_log *log, 674 const struct bpf_prog *prog, 675 const struct bpf_prog *tgt_prog, 676 u32 btf_id, 677 struct bpf_attach_target_info *tgt_info); 678void bpf_free_kfunc_btf_tab(struct bpf_kfunc_btf_tab *tab); 679 680int mark_chain_precision(struct bpf_verifier_env *env, int regno); 681 682#define BPF_BASE_TYPE_MASK GENMASK(BPF_BASE_TYPE_BITS - 1, 0) 683 684/* extract base type from bpf_{arg, return, reg}_type. */ 685static inline u32 base_type(u32 type) 686{ 687 return type & BPF_BASE_TYPE_MASK; 688} 689 690/* extract flags from an extended type. See bpf_type_flag in bpf.h. */ 691static inline u32 type_flag(u32 type) 692{ 693 return type & ~BPF_BASE_TYPE_MASK; 694} 695 696/* only use after check_attach_btf_id() */ 697static inline enum bpf_prog_type resolve_prog_type(const struct bpf_prog *prog) 698{ 699 return prog->type == BPF_PROG_TYPE_EXT ? 700 prog->aux->dst_prog->type : prog->type; 701} 702 703static inline bool bpf_prog_check_recur(const struct bpf_prog *prog) 704{ 705 switch (resolve_prog_type(prog)) { 706 case BPF_PROG_TYPE_TRACING: 707 return prog->expected_attach_type != BPF_TRACE_ITER; 708 case BPF_PROG_TYPE_STRUCT_OPS: 709 case BPF_PROG_TYPE_LSM: 710 return false; 711 default: 712 return true; 713 } 714} 715 716#define BPF_REG_TRUSTED_MODIFIERS (MEM_ALLOC | PTR_TRUSTED) 717 718static inline bool bpf_type_has_unsafe_modifiers(u32 type) 719{ 720 return type_flag(type) & ~BPF_REG_TRUSTED_MODIFIERS; 721} 722 723#endif /* _LINUX_BPF_VERIFIER_H */