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