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 #include <linux/cnum.h> 12 #include <linux/math64.h> /* for div_s64_rem() */ 13 14 /* 15 * Mathematical modulo, the residue of 'v' modulo 'step', normalized to [0, step). 16 * This differs from C's '%' operator, which truncates the quotient toward zero 17 * and so returns a remainder with the sign of the dividend (e.g. -1 % 3 == -1, not 2). 18 */ 19 static inline u16 imod(s64 v, u16 step) 20 { 21 s32 rem; 22 23 div_s64_rem(v, step, &rem); 24 return rem < 0 ? rem + step : rem; 25 } 26 27 /* Maximum variable offset umax_value permitted when resolving memory accesses. 28 * In practice this is far bigger than any realistic pointer offset; this limit 29 * ensures that umax_value + (int)off + (int)size cannot overflow a u64. 30 */ 31 #define BPF_MAX_VAR_OFF (1 << 29) 32 /* Maximum variable size permitted for ARG_MEM_SIZE[_OR_ZERO]. This ensures 33 * that converting umax_value to int cannot overflow. 34 */ 35 #define BPF_MAX_VAR_SIZ (1 << 29) 36 /* 37 * size of tmp_str_buf in bpf_verifier. 38 * we need at least 1399 bytes to fit full stack mask representation 39 * (in the "-8,-16,...,-2048" form) 40 */ 41 #define TMP_STR_BUF_LEN 1408 42 /* Patch buffer size */ 43 #define INSN_BUF_SIZE 32 44 45 #define ITER_PREFIX "bpf_iter_" 46 47 enum bpf_iter_state { 48 BPF_ITER_STATE_INVALID, /* for non-first slot */ 49 BPF_ITER_STATE_ACTIVE, 50 BPF_ITER_STATE_DRAINED, 51 }; 52 53 struct bpf_reg_state { 54 /* Ordering of fields matters. See states_equal() */ 55 enum bpf_reg_type type; 56 /* 57 * Constant delta between "linked" scalars with the same ID. 58 */ 59 s32 delta; 60 union { 61 /* valid when type == PTR_TO_PACKET */ 62 int range; 63 /* 64 * Valid when type == PTR_TO_STACK. Inside the callee two registers 65 * can be both PTR_TO_STACK like R1=fp-8 and R2=fp-8, but one of them 66 * points to this function stack while another to the caller's stack. 67 * To differentiate them 'frameno' is used which is an index in 68 * bpf_verifier_state->frame[] array pointing to bpf_func_state. 69 */ 70 u8 frameno; 71 72 /* 73 * For CONST_PTR_TO_MAP, PTR_TO_MAP_KEY, PTR_TO_MAP_VALUE and 74 * PTR_TO_INSN. 75 */ 76 struct bpf_map *map_ptr; 77 78 /* for PTR_TO_BTF_ID */ 79 struct { 80 struct btf *btf; 81 u32 btf_id; 82 }; 83 84 struct { /* for PTR_TO_MEM | PTR_TO_MEM_OR_NULL */ 85 u32 mem_size; 86 }; 87 88 /* For dynptr stack slots */ 89 struct { 90 enum bpf_dynptr_type type; 91 /* A dynptr is 16 bytes so it takes up 2 stack slots. 92 * We need to track which slot is the first slot 93 * to protect against cases where the user may try to 94 * pass in an address starting at the second slot of the 95 * dynptr. 96 */ 97 bool first_slot; 98 } dynptr; 99 100 /* For bpf_iter stack slots */ 101 struct { 102 /* BTF container and BTF type ID describing 103 * struct bpf_iter_<type> of an iterator state 104 */ 105 struct btf *btf; 106 u32 btf_id; 107 /* packing following two fields to fit iter state into 16 bytes */ 108 enum bpf_iter_state state:2; 109 int depth:30; 110 } iter; 111 112 /* For irq stack slots */ 113 struct { 114 enum { 115 IRQ_NATIVE_KFUNC, 116 IRQ_LOCK_KFUNC, 117 } kfunc_class; 118 } irq; 119 120 /* Max size from any of the above. */ 121 struct { 122 unsigned long raw1; 123 unsigned long raw2; 124 } raw; 125 126 u32 subprogno; /* for PTR_TO_FUNC */ 127 }; 128 /* For scalar types (SCALAR_VALUE), this represents our knowledge of 129 * the actual value. 130 * For pointer types, this represents the variable part of the offset 131 * from the pointed-to object, and is shared with all bpf_reg_states 132 * with the same id as us. 133 */ 134 struct tnum var_off; 135 /* Used to determine if any memory access using this register will 136 * result in a bad access. 137 * These refer to the same value as var_off, not necessarily the actual 138 * contents of the register. 139 */ 140 struct cnum64 r64; /* 64-bit range as circular number */ 141 struct cnum32 r32; /* 32-bit range as circular number */ 142 /* 143 * The value described by this register, interpreted as s64, lies on 144 * a line described by a linear equation base + step * k. 145 * Invariant: base < step. 146 */ 147 u16 base; 148 u16 step; 149 /* For PTR_TO_PACKET, used to find other pointers with the same variable 150 * offset, so they can share range knowledge. 151 * For PTR_TO_MAP_VALUE_OR_NULL this is used to share which map value we 152 * came from, when one is tested for != NULL. 153 * For PTR_TO_MEM_OR_NULL this is used to identify memory allocation 154 * for the purpose of tracking that it's freed. 155 * For PTR_TO_SOCKET this is used to share which pointers retain the 156 * same reference to the socket, to determine proper reference freeing. 157 * For stack slots that are dynptrs, this is used to track references to 158 * the dynptr to determine proper reference freeing. 159 * Similarly to dynptrs, we use ID to track "belonging" of a reference 160 * to a specific instance of bpf_iter. 161 */ 162 /* 163 * Upper bit of ID is used to remember relationship between "linked" 164 * registers. Example: 165 * r1 = r2; both will have r1->id == r2->id == N 166 * r1 += 10; r1->id == N | BPF_ADD_CONST and r1->delta == 10 167 * r3 = r2; both will have r3->id == r2->id == N 168 * w3 += 10; r3->id == N | BPF_ADD_CONST32 and r3->delta == 10 169 */ 170 #define BPF_ADD_CONST64 (1U << 31) 171 #define BPF_ADD_CONST32 (1U << 30) 172 #define BPF_ADD_CONST (BPF_ADD_CONST64 | BPF_ADD_CONST32) 173 u32 id; 174 /* 175 * Tracks the parent object this register was derived from. 176 * Used for cascading invalidation: when the parent object is 177 * released or invalidated, all registers with matching parent_id 178 * are also invalidated. For example, a slice from bpf_dynptr_data() 179 * gets parent_id set to the dynptr's id. 180 */ 181 u32 parent_id; 182 /* 183 * Distinguishes inner-map lookups and their keys and values. Zero for 184 * other registers. Kept outside the metadata union for ID remapping 185 * during state comparisons. 186 */ 187 u32 map_uid:31; 188 /* if (!precise && SCALAR_VALUE) min/max/tnum don't affect safety */ 189 u32 precise:1; 190 }; 191 192 static inline s64 reg_smin(const struct bpf_reg_state *reg) 193 { 194 return cnum64_smin(reg->r64); 195 } 196 197 static inline s64 reg_smax(const struct bpf_reg_state *reg) 198 { 199 return cnum64_smax(reg->r64); 200 } 201 202 static inline u64 reg_umin(const struct bpf_reg_state *reg) 203 { 204 return cnum64_umin(reg->r64); 205 } 206 207 static inline u64 reg_umax(const struct bpf_reg_state *reg) 208 { 209 return cnum64_umax(reg->r64); 210 } 211 212 static inline s32 reg_s32_min(const struct bpf_reg_state *reg) 213 { 214 return cnum32_smin(reg->r32); 215 } 216 217 static inline s32 reg_s32_max(const struct bpf_reg_state *reg) 218 { 219 return cnum32_smax(reg->r32); 220 } 221 222 static inline u32 reg_u32_min(const struct bpf_reg_state *reg) 223 { 224 return cnum32_umin(reg->r32); 225 } 226 227 static inline u32 reg_u32_max(const struct bpf_reg_state *reg) 228 { 229 return cnum32_umax(reg->r32); 230 } 231 232 static inline void reg_set_srange32(struct bpf_reg_state *reg, s32 smin, s32 smax) 233 { 234 reg->r32 = cnum32_from_srange(smin, smax); 235 } 236 237 static inline void reg_set_urange32(struct bpf_reg_state *reg, u32 umin, u32 umax) 238 { 239 reg->r32 = cnum32_from_urange(umin, umax); 240 } 241 242 static inline void reg_set_srange64(struct bpf_reg_state *reg, s64 smin, s64 smax) 243 { 244 reg->r64 = cnum64_from_srange(smin, smax); 245 } 246 247 static inline void reg_set_urange64(struct bpf_reg_state *reg, u64 umin, u64 umax) 248 { 249 reg->r64 = cnum64_from_urange(umin, umax); 250 } 251 252 enum bpf_stack_slot_type { 253 STACK_INVALID, /* nothing was stored in this stack slot */ 254 STACK_SPILL, /* register spilled into stack */ 255 STACK_MISC, /* BPF program wrote some data into this slot */ 256 STACK_ZERO, /* BPF program wrote constant zero */ 257 /* A dynptr is stored in this stack slot. The type of dynptr 258 * is stored in bpf_stack_state->spilled_ptr.dynptr.type 259 */ 260 STACK_DYNPTR, 261 STACK_ITER, 262 STACK_IRQ_FLAG, 263 STACK_POISON, 264 }; 265 266 #define BPF_REG_SIZE 8 /* size of eBPF register in bytes */ 267 268 /* 269 * Largest number of BPF_REG_SIZE stack slots a single frame can have, sized 270 * for the largest stack budget any JIT supports. A frame may use any part of 271 * its program's budget; check_max_stack_depth() enforces the budget on the 272 * combined depth of frames sharing the kernel stack and on each frame using 273 * a private stack. 274 */ 275 #define MAX_BPF_STACK_SLOTS (MAX_BPF_STACK_JIT / BPF_REG_SIZE) 276 277 /* 4-byte stack slot granularity for liveness analysis */ 278 #define BPF_HALF_REG_SIZE 4 279 #define STACK_SLOT_SZ 4 280 281 #define BPF_REGMASK_ARGS ((1 << BPF_REG_1) | (1 << BPF_REG_2) | \ 282 (1 << BPF_REG_3) | (1 << BPF_REG_4) | \ 283 (1 << BPF_REG_5)) 284 285 #define BPF_MAIN_FUNC (-1) 286 287 #define BPF_DYNPTR_SIZE sizeof(struct bpf_dynptr_kern) 288 #define BPF_DYNPTR_NR_SLOTS (BPF_DYNPTR_SIZE / BPF_REG_SIZE) 289 290 struct bpf_stack_state { 291 struct bpf_reg_state spilled_ptr; 292 u8 slot_type[BPF_REG_SIZE]; 293 }; 294 295 struct bpf_reference_state { 296 /* Each reference object has a type. Ensure REF_TYPE_PTR is zero to 297 * default to pointer reference on zero initialization of a state. 298 */ 299 enum ref_state_type { 300 REF_TYPE_PTR = (1 << 1), 301 REF_TYPE_IRQ = (1 << 2), 302 REF_TYPE_LOCK = (1 << 3), 303 REF_TYPE_RES_LOCK = (1 << 4), 304 REF_TYPE_RES_LOCK_IRQ = (1 << 5), 305 REF_TYPE_LOCK_MASK = REF_TYPE_LOCK | REF_TYPE_RES_LOCK | REF_TYPE_RES_LOCK_IRQ, 306 } type; 307 /* Track each reference created with a unique id, even if the same 308 * instruction creates the reference multiple times (eg, via CALL). 309 */ 310 int id; 311 /* Instruction where the allocation of this reference occurred. This 312 * is used purely to inform the user of a reference leak. 313 */ 314 int insn_idx; 315 union { 316 /* For REF_TYPE_PTR */ 317 int parent_id; 318 /* Use to keep track of the source object of a lock, to ensure 319 * it matches on unlock. 320 */ 321 void *ptr; 322 }; 323 }; 324 325 struct bpf_retval_range { 326 s32 minval; 327 s32 maxval; 328 bool return_32bit; 329 }; 330 331 /* SCEV/widening register space: r0..r10 plus every stack slot of a frame. */ 332 #define BPF_SCEV_REGS_NUM (MAX_BPF_REG + MAX_BPF_STACK_SLOTS) 333 334 struct bpf_loop_iters { 335 u32 min_header_count; /* min number of times header is executed */ 336 u32 max_header_count; /* max number of times header is executed */ 337 unsigned long precise_regs[BITS_TO_LONGS(BPF_SCEV_REGS_NUM)]; 338 }; 339 340 struct loop_stack_entry { 341 struct bpf_verifier_state *entry_state; 342 u32 min_header_count; 343 u32 max_header_count; 344 u32 loop_id:31; 345 u32 terminates:1; 346 }; 347 348 #define LOOP_STACK_SIZE 16 349 350 /* state of the program: 351 * type of all registers and stack info 352 */ 353 struct bpf_func_state { 354 struct bpf_reg_state regs[MAX_BPF_REG]; 355 /* index of call instruction that called into this func */ 356 int callsite; 357 /* stack frame number of this function state from pov of 358 * enclosing bpf_verifier_state. 359 * 0 = main function, 1 = first callee. 360 */ 361 u32 frameno; 362 /* 363 * Unique diagnostic identity for this function invocation. Frame depth is 364 * reused after returns, while this ID is preserved across state clones. 365 */ 366 u32 diag_frame_id; 367 /* subprog number == index within subprog_info 368 * zero == main subprog 369 */ 370 u32 subprogno; 371 /* Every bpf_timer_start will increment async_entry_cnt. 372 * It's used to distinguish: 373 * void foo(void) { for(;;); } 374 * void foo(void) { bpf_timer_set_callback(,foo); } 375 */ 376 u32 async_entry_cnt; 377 struct bpf_retval_range callback_ret_range; 378 bool in_callback_fn; 379 bool in_async_callback_fn; 380 bool in_exception_callback_fn; 381 bool no_stack_arg_load; 382 /* 383 * The helper invokes this callback under rcu_read_lock(), so the 384 * callback and its callees run in an RCU read-side critical section. 385 */ 386 bool in_rcu_callback_fn; 387 /* For callback calling functions that limit number of possible 388 * callback executions (e.g. bpf_loop) keeps track of current 389 * simulated iteration number. 390 * Value in frame N refers to number of times callback with frame 391 * N+1 was simulated, e.g. for the following call: 392 * 393 * bpf_loop(..., fn, ...); | suppose current frame is N 394 * | fn would be simulated in frame N+1 395 * | number of simulations is tracked in frame N 396 */ 397 u32 callback_depth; 398 /* Instructions processed in this frame and callees on the current path. */ 399 u32 insns_subtotal; 400 /* 401 * Control-flow loop nesting at the current insn within this frame's 402 * subprogram (loops never cross subprogram boundaries). 403 */ 404 u32 loop_stack_cnt; 405 406 /* The following fields should be last. See copy_func_state() */ 407 /* The state of the stack. Each element of the array describes BPF_REG_SIZE 408 * (i.e. 8) bytes worth of stack memory. 409 * stack[0] represents bytes [*(r10-8)..*(r10-1)] 410 * stack[1] represents bytes [*(r10-16)..*(r10-9)] 411 * ... 412 * stack[allocated_stack/8 - 1] represents [*(r10-allocated_stack)..*(r10-allocated_stack+7)] 413 */ 414 struct bpf_stack_state *stack; 415 /* Size of the current stack, in bytes. The stack state is tracked below, in 416 * `stack`. allocated_stack is always a multiple of BPF_REG_SIZE. 417 */ 418 int allocated_stack; 419 420 u16 out_stack_arg_cnt; /* Number of outgoing on-stack argument slots */ 421 struct bpf_reg_state *stack_arg_regs; /* Outgoing on-stack arguments */ 422 struct loop_stack_entry *loop_stack; 423 }; 424 425 #define MAX_CALL_FRAMES 16 426 427 /* instruction history flags, used in bpf_jmp_history_entry.flags field. 428 * Frame number and SPI are stored in dedicated fields of bpf_jmp_history_entry. 429 */ 430 enum { 431 INSN_F_STACK_ACCESS = BIT(0), 432 433 INSN_F_DST_REG_STACK = BIT(1), /* dst_reg is PTR_TO_STACK */ 434 INSN_F_SRC_REG_STACK = BIT(2), /* src_reg is PTR_TO_STACK */ 435 436 INSN_F_STACK_ARG_ACCESS = BIT(3), 437 }; 438 439 /* Registers linked to one jump condition that a history entry can record */ 440 #define BPF_LINKED_REGS_MAX 5 441 442 struct bpf_jmp_history_entry { 443 /* insn idx can't be bigger than 1 million */ 444 u32 idx : 20; 445 u32 frame : 4; /* stack access frame number */ 446 /* special INSN_F_xxx flags */ 447 u32 flags : 4; 448 u32 : 4; 449 u32 prev_idx : 20; 450 u32 spi : 12; /* stack slot index */ 451 /* 452 * Scalar registers and spilled scalars linked to the condition of 453 * this jump, which need precision tracking together when the jump is 454 * backtracked. Each is packed as 4 bits of frame number, one bit 455 * telling a register from a stack slot and 11 bits of register or 456 * slot index, see linked_regs_pack(). 457 */ 458 u16 linked_regs[BPF_LINKED_REGS_MAX]; 459 u8 linked_regs_cnt; 460 }; 461 462 static_assert(MAX_CALL_FRAMES <= (1 << 4)); 463 static_assert(MAX_BPF_STACK_SLOTS <= (1 << 12)); 464 465 /* Maximum number of bpf_reg_state objects that can exist at once */ 466 #define MAX_STACK_ARG_SLOTS (MAX_BPF_FUNC_ARGS - MAX_BPF_FUNC_REG_ARGS) 467 #define BPF_ID_MAP_SIZE ((MAX_BPF_REG + MAX_BPF_STACK_SLOTS + MAX_STACK_ARG_SLOTS) * \ 468 MAX_CALL_FRAMES) 469 470 struct bpf_verifier_state { 471 /* call stack tracking */ 472 struct bpf_func_state *frame[MAX_CALL_FRAMES]; 473 struct bpf_verifier_state *parent; 474 /* Acquired reference states */ 475 struct bpf_reference_state *refs; 476 /* 477 * 'branches' field is the number of branches left to explore: 478 * 0 - all possible paths from this state reached bpf_exit or 479 * were safely pruned 480 * 1 - at least one path is being explored. 481 * This state hasn't reached bpf_exit 482 * 2 - at least two paths are being explored. 483 * This state is an immediate parent of two children. 484 * One is fallthrough branch with branches==1 and another 485 * state is pushed into stack (to be explored later) also with 486 * branches==1. The parent of this state has branches==1. 487 * The verifier state tree connected via 'parent' pointer looks like: 488 * 1 489 * 1 490 * 2 -> 1 (first 'if' pushed into stack) 491 * 1 492 * 2 -> 1 (second 'if' pushed into stack) 493 * 1 494 * 1 495 * 1 bpf_exit. 496 * 497 * Once do_check() reaches bpf_exit, it calls update_branch_counts() 498 * and the verifier state tree will look: 499 * 1 500 * 1 501 * 2 -> 1 (first 'if' pushed into stack) 502 * 1 503 * 1 -> 1 (second 'if' pushed into stack) 504 * 0 505 * 0 506 * 0 bpf_exit. 507 * After pop_stack() the do_check() will resume at second 'if'. 508 * 509 * If is_state_visited() sees a state with branches > 0 it means 510 * there is a loop. If such state is exactly equal to the current state 511 * it's an infinite loop. Note states_equal() checks for states 512 * equivalency, so two states being 'states_equal' does not mean 513 * infinite loop. The exact comparison is provided by 514 * states_maybe_looping() function. It's a stronger pre-check and 515 * much faster than states_equal(). 516 * 517 * This algorithm may not find all possible infinite loops or 518 * loop iteration count may be too high. 519 * In such cases BPF_COMPLEXITY_LIMIT_INSNS limit kicks in. 520 */ 521 u32 branches; 522 u32 insn_idx; 523 u32 curframe; 524 525 u32 acquired_refs; 526 u32 active_locks; 527 u32 active_preempt_locks; 528 u32 active_irq_id; 529 u32 active_lock_id; 530 void *active_lock_ptr; 531 u32 active_rcu_locks; 532 533 bool speculative; 534 bool in_sleepable; 535 /* The callee left with unwind_exit. Next insn is unwind_goto. */ 536 bool unwound; 537 538 /* 539 * First and last insn idx of this verifier state. 540 * last_insn_idx is -1 if no instructions have been executed yet. 541 */ 542 u32 first_insn_idx; 543 u32 last_insn_idx; 544 /* if this state is a backedge state then equal_state 545 * records cached state to which this state is equal. 546 */ 547 struct bpf_verifier_state *equal_state; 548 /* jmp history recorded from first to last. 549 * backtracking is using it to go from last to first. 550 * For most states jmp_history_cnt is [0-3]. 551 * For loops can go up to ~40. 552 */ 553 struct bpf_jmp_history_entry *jmp_history; 554 u32 jmp_history_cnt; 555 u32 dfs_depth; 556 u32 callback_unroll_depth; 557 u32 may_goto_depth; 558 }; 559 560 /* Number of BPF_REG_SIZE stack slots tracked for the frame so far. */ 561 static inline u32 bpf_stack_nr_slots(const struct bpf_func_state *frame) 562 { 563 return frame->allocated_stack / BPF_REG_SIZE; 564 } 565 566 /* 567 * Stack slot @spi of @frame, covering bytes [fp - (spi + 1) * 8, fp - spi * 8). 568 * The caller must ensure spi < bpf_stack_nr_slots(frame), see bpf_grow_stack_state(). 569 */ 570 static inline struct bpf_stack_state *bpf_stack_slot(const struct bpf_func_state *frame, u32 spi) 571 { 572 return &frame->stack[spi]; 573 } 574 575 static inline struct bpf_reg_state * 576 bpf_get_spilled_reg(int slot, struct bpf_func_state *frame, u32 mask) 577 { 578 struct bpf_stack_state *ss; 579 580 if (slot >= bpf_stack_nr_slots(frame)) 581 return NULL; 582 ss = bpf_stack_slot(frame, slot); 583 if ((1 << ss->slot_type[BPF_REG_SIZE - 1]) & mask) 584 return &ss->spilled_ptr; 585 return NULL; 586 } 587 588 static inline struct bpf_reg_state * 589 bpf_get_spilled_stack_arg(int slot, struct bpf_func_state *frame) 590 { 591 if (slot < frame->out_stack_arg_cnt && 592 frame->stack_arg_regs[slot].type != NOT_INIT) 593 return &frame->stack_arg_regs[slot]; 594 return NULL; 595 } 596 597 /* Iterate over 'frame', setting 'reg' to either NULL or a spilled register. */ 598 #define bpf_for_each_spilled_reg(iter, frame, reg, mask) \ 599 for (iter = 0, reg = bpf_get_spilled_reg(iter, frame, mask); \ 600 iter < bpf_stack_nr_slots(frame); \ 601 iter++, reg = bpf_get_spilled_reg(iter, frame, mask)) 602 603 /* Iterate over 'frame', setting 'reg' to either NULL or a spilled stack arg. */ 604 #define bpf_for_each_spilled_stack_arg(iter, frame, reg) \ 605 for (iter = 0, reg = bpf_get_spilled_stack_arg(iter, frame); \ 606 iter < frame->out_stack_arg_cnt; \ 607 iter++, reg = bpf_get_spilled_stack_arg(iter, frame)) 608 609 #define bpf_for_each_reg_in_vstate_mask(__vst, __state, __reg, __stack, __mask, __expr) \ 610 ({ \ 611 struct bpf_verifier_state *___vstate = __vst; \ 612 int ___i, ___j; \ 613 for (___i = 0; ___i <= ___vstate->curframe; ___i++) { \ 614 struct bpf_reg_state *___regs; \ 615 __state = ___vstate->frame[___i]; \ 616 ___regs = __state->regs; \ 617 __stack = NULL; \ 618 for (___j = 0; ___j < MAX_BPF_REG; ___j++) { \ 619 __reg = &___regs[___j]; \ 620 (void)(__expr); \ 621 } \ 622 bpf_for_each_spilled_reg(___j, __state, __reg, __mask) { \ 623 if (!__reg) \ 624 continue; \ 625 __stack = bpf_stack_slot(__state, ___j); \ 626 (void)(__expr); \ 627 } \ 628 __stack = NULL; \ 629 bpf_for_each_spilled_stack_arg(___j, __state, __reg) { \ 630 if (!__reg) \ 631 continue; \ 632 (void)(__expr); \ 633 } \ 634 } \ 635 (void)__stack; \ 636 }) 637 638 /* Invoke __expr over regsiters in __vst, setting __state and __reg */ 639 #define bpf_for_each_reg_in_vstate(__vst, __state, __reg, __expr) \ 640 ({ \ 641 struct bpf_stack_state * ___stack; \ 642 (void)___stack; \ 643 bpf_for_each_reg_in_vstate_mask(__vst, __state, __reg, ___stack,\ 644 1 << STACK_SPILL, __expr); \ 645 }) 646 647 /* linked list of verifier states used to prune search */ 648 struct bpf_verifier_state_list { 649 struct bpf_verifier_state state; 650 struct list_head node; 651 u32 miss_cnt; 652 u32 hit_cnt:31; 653 u32 in_free_list:1; 654 }; 655 656 struct bpf_loop_inline_state { 657 unsigned int initialized:1; /* set to true upon first entry */ 658 unsigned int fit_for_inline:1; /* true if callback function is the same 659 * at each call and flags are always zero 660 */ 661 u32 callback_subprogno; /* valid when fit_for_inline is true */ 662 }; 663 664 /* pointer and state for maps */ 665 struct bpf_map_ptr_state { 666 struct bpf_map *map_ptr; 667 bool poison; 668 bool unpriv; 669 }; 670 671 /* Possible states for alu_state member. */ 672 #define BPF_ALU_SANITIZE_SRC (1U << 0) 673 #define BPF_ALU_SANITIZE_DST (1U << 1) 674 #define BPF_ALU_NEG_VALUE (1U << 2) 675 #define BPF_ALU_NON_POINTER (1U << 3) 676 #define BPF_ALU_IMMEDIATE (1U << 4) 677 #define BPF_ALU_SANITIZE (BPF_ALU_SANITIZE_SRC | \ 678 BPF_ALU_SANITIZE_DST) 679 680 /* 681 * An array of BPF instructions. 682 * Primary usage: return value of bpf_insn_successors. 683 */ 684 struct bpf_iarray { 685 int cnt; 686 u32 items[]; 687 }; 688 689 #define iarray_for_each(item, arr) \ 690 for (int ___idx = 0; \ 691 ___idx < (arr)->cnt && ({ item = (arr)->items[___idx]; 1; }); \ 692 ___idx++) 693 694 #define MAX_BACKEDGES 16 695 #define MAX_LOOP_EXITS 256 696 697 /* Number of exit conditions dominating a backedge. */ 698 #define MAX_LATCHES 4 699 700 struct bpf_backedge { 701 int from; 702 /* 703 * Conditional jumps within the loop that dominate 'from' and have 704 * a successor outside the loop, nearest first. latches[0] is the 705 * latch; the remaining entries are additional exit conditions. 706 * Each usable condition gives an upper bound on the iteration count, 707 * so keeping only a subset is sound. 708 */ 709 int latches[MAX_LATCHES]; 710 u8 latches_cnt; 711 }; 712 713 struct bpf_loop_exit { 714 int from; /* instruction inside the loop */ 715 int to; /* instruction outside the loop */ 716 }; 717 718 struct bpf_loop { 719 struct bpf_backedge backedges[MAX_BACKEDGES]; 720 /* edges exiting from this loop, includes edges from nested loops */ 721 struct bpf_loop_exit *exits; 722 int backedges_cnt; 723 int exits_cnt; 724 bool irreducible; 725 bool backedges_overflow; 726 bool exits_overflow; 727 /* 728 * Loop-entry registers used to compute stack addresses for loads, 729 * stores and calls requiring fixed stack offsets, in this and nested 730 * loops. 731 */ 732 unsigned long store_base_regs[BITS_TO_LONGS(BPF_SCEV_REGS_NUM)]; 733 }; 734 735 struct bpf_insn_aux_data { 736 union { 737 enum bpf_reg_type ptr_type; /* pointer type for load/store insns */ 738 struct bpf_map_ptr_state map_ptr_state; 739 s32 call_imm; /* saved imm field of call insn */ 740 u32 alu_limit; /* limit for add/sub register with pointer */ 741 struct { 742 u32 map_index; /* index into used_maps[] */ 743 u32 map_off; /* offset from value base address */ 744 }; 745 struct { 746 enum bpf_reg_type reg_type; /* type of pseudo_btf_id */ 747 union { 748 struct { 749 struct btf *btf; 750 u32 btf_id; /* btf_id for struct typed var */ 751 }; 752 u32 mem_size; /* mem_size for non-struct typed var */ 753 }; 754 } btf_var; 755 /* if instruction is a call to bpf_loop this field tracks 756 * the state of the relevant registers to make decision about inlining 757 */ 758 struct bpf_loop_inline_state loop_inline_state; 759 }; 760 union { 761 /* remember the size of type passed to bpf_obj_new to rewrite R1 */ 762 u64 obj_new_size; 763 /* remember the offset of node field within type to rewrite */ 764 u64 insert_off; 765 }; 766 struct btf_struct_meta *kptr_struct_meta; 767 u64 map_key_state; /* constant (32 bit) key tracking for maps */ 768 /* 769 * Per-instruction summary of stack slots in the current frame 770 * that this instruction may write to. 771 */ 772 DECLARE_BITMAP(may_write_mask, MAX_BPF_STACK_SLOTS); 773 DECLARE_BITMAP(live_stack_before, MAX_BPF_STACK_SLOTS); 774 u16 stack_ptrs; /* bitmask of regs that may hold a frame pointer here (arg_track) */ 775 int ctx_field_size; /* the ctx field size for load insn, maybe 0 */ 776 u32 seen; /* this insn was processed by the verifier at env->pass_cnt */ 777 u64 nospec:1; /* do not execute this instruction speculatively */ 778 u64 nospec_result:1; /* result is unsafe under speculation, nospec must follow */ 779 u64 zext_dst:1; /* this insn zero extends dst reg */ 780 u64 needs_zext:1; /* alu op needs to clear upper bits */ 781 u64 prevent_zext:1; /* alu op cannot be zext (already used with 64-bit scalars) */ 782 u64 non_sleepable:1; /* helper/kfunc may be called from non-sleepable context */ 783 u64 is_iter_next:1; /* bpf_iter_<type>_next() kfunc call */ 784 u64 call_with_percpu_alloc_ptr:1; /* {this,per}_cpu_ptr() with prog percpu alloc */ 785 u64 bb_end:1; /* last instruction of a basic block */ 786 u64 need_scev:1; 787 u64 alu_state:5; /* used in combination with alu_limit */ 788 /* true if STX or LDX instruction is a part of a spill/fill 789 * pattern for a bpf_fastcall call. 790 */ 791 u64 fastcall_pattern:1; 792 /* for CALL instructions, a number of spill/fill pairs in the 793 * bpf_fastcall pattern. 794 */ 795 u64 fastcall_spills_num:3; 796 u64 arg_prog:4; 797 /* 798 * What a load found, on any path: an __arena pointer member of a 799 * kernel struct, which arena_member_fixup() rewrites, one that is 800 * __nullable as well, or anything else through any pointer. 801 */ 802 u64 arena_member:1; 803 u64 arena_member_nullable:1; 804 u64 non_arena_load:1; 805 806 /* below flags are initialized once */ 807 u64 jmp_point:1; 808 u64 prune_point:1; 809 /* ensure we check state equivalence and save state checkpoint and 810 * this instruction, regardless of any heuristics 811 */ 812 u64 force_checkpoint:1; 813 /* true if instruction is a call to a helper function that 814 * accepts callback function as a parameter. 815 */ 816 u64 calls_callback:1; 817 u64 indirect_target:1; /* if it is an indirect jump target */ 818 u64 non_stack_access:1; /* instruction can access non-stack memory */ 819 /* true if some jump or call instruction targets this instruction */ 820 u64 jump_target:1; 821 822 unsigned int orig_idx; /* original instruction index, initialized once */ 823 /* 824 * CFG strongly connected component this instruction belongs to, 825 * zero if it is a singleton SCC. 826 */ 827 u32 scc; 828 /* registers alive before this instruction. */ 829 u16 live_regs_before; 830 /* 831 * Bitmask of R0-R9 that hold known values at this instruction. 832 * const_reg_mask: scalar constants that fit in 32 bits. 833 * const_reg_map_mask: map pointers, val is map_index into used_maps[]. 834 * const_reg_subprog_mask: subprog pointers, val is subprog number. 835 * const_reg_vals[i] holds the 32-bit value for register i. 836 * Populated by compute_const_regs() pre-pass. 837 */ 838 u16 const_reg_mask; 839 u16 const_reg_map_mask; 840 u16 const_reg_subprog_mask; 841 u32 const_reg_vals[10]; 842 /* 843 * Header index of the innermost loop containing this instruction, -1 if none. 844 * For a loop header, identifies the parent loop instead. 845 */ 846 s32 loop_header; 847 /* additional information about the loop if this instruction is a loop header */ 848 struct bpf_loop *loop; 849 }; 850 851 #define MAX_USED_MAPS 64 /* max number of maps accessed by one eBPF program */ 852 #define MAX_USED_BTFS 64 /* max number of BTFs accessed by one BPF program */ 853 854 /* 855 * Longest line the verifier log can carry: a full stack mask of 856 * MAX_BPF_STACK_SLOTS slots, see TMP_STR_BUF_LEN, plus its prefix. 857 */ 858 #define BPF_VERIFIER_TMP_LOG_SIZE 2048 859 860 struct bpf_verifier_log { 861 /* Logical start and end positions of a "log window" of the verifier log. 862 * start_pos == 0 means we haven't truncated anything. 863 * Once truncation starts to happen, start_pos + len_total == end_pos, 864 * except during log reset situations, in which (end_pos - start_pos) 865 * might get smaller than len_total (see bpf_vlog_reset()). 866 * Generally, (end_pos - start_pos) gives number of useful data in 867 * user log buffer. 868 */ 869 u64 start_pos; 870 u64 end_pos; 871 char __user *ubuf; 872 u32 level; 873 u32 len_total; 874 u32 len_max; 875 char kbuf[BPF_VERIFIER_TMP_LOG_SIZE]; 876 }; 877 878 #define BPF_LOG_LEVEL1 1 879 #define BPF_LOG_LEVEL2 2 880 #define BPF_LOG_STATS 4 881 #define BPF_LOG_FIXED 8 882 #define BPF_LOG_LEVEL (BPF_LOG_LEVEL1 | BPF_LOG_LEVEL2) 883 #define BPF_LOG_MASK (BPF_LOG_LEVEL | BPF_LOG_STATS | BPF_LOG_FIXED) 884 #define BPF_LOG_KERNEL (BPF_LOG_MASK + 1) /* kernel internal flag */ 885 #define BPF_LOG_MIN_ALIGNMENT 8U 886 #define BPF_LOG_ALIGNMENT 40U 887 888 static inline bool bpf_verifier_log_needed(const struct bpf_verifier_log *log) 889 { 890 return log && log->level; 891 } 892 893 struct bpf_log_attr { 894 char __user *ubuf; 895 u32 size; 896 u32 level; 897 u32 offsetof_true_size; 898 bpfptr_t uattr; 899 }; 900 901 int bpf_log_attr_init(struct bpf_log_attr *log, u64 log_buf, u32 log_size, u32 log_level, 902 u32 offsetof_log_true_size, bpfptr_t uattr, struct bpf_common_attr *common, 903 bpfptr_t uattr_common, u32 size_common); 904 struct bpf_verifier_log *bpf_log_attr_create_vlog(struct bpf_log_attr *attr_log, 905 struct bpf_common_attr *common, bpfptr_t uattr, 906 u32 size); 907 int bpf_log_attr_finalize(struct bpf_log_attr *attr, struct bpf_verifier_log *log); 908 909 #define BPF_MAX_SUBPROGS 256 910 911 /* 912 * A pointer to a static subprog in the value of a frozen read-only array map: 913 * a 64-bit value that is the offset in bytes of the first instruction of 914 * the subprog in the program. 915 */ 916 struct bpf_func_ptr { 917 struct bpf_map *map; 918 u32 map_off; /* offset of the pointer in the value of the map */ 919 u32 orig_off; /* what the map has: the first instruction of the subprog */ 920 u32 xlated_off; /* the same after instructions were patched and removed */ 921 }; 922 923 /* the subprog that a bpf_func_ptr pointed to was removed as dead code */ 924 #define BPF_FUNC_PTR_DELETED ((u32)-1) 925 926 struct bpf_subprog_arg_info { 927 enum bpf_arg_type arg_type; 928 union { 929 u32 mem_size; 930 u32 btf_id; 931 }; 932 }; 933 934 enum priv_stack_mode { 935 PRIV_STACK_UNKNOWN, 936 NO_PRIV_STACK, 937 PRIV_STACK_ADAPTIVE, 938 }; 939 940 struct bpf_subprog_info { 941 const char *name; /* name extracted from BTF */ 942 u32 start; /* insn idx of function entry point */ 943 u32 linfo_idx; /* The idx to the main_prog->aux->linfo */ 944 u32 postorder_start; /* The idx to the env->cfg.insn_postorder */ 945 /* 946 * Index of one of the BPF_EXIT instructions in this subprogram, or 947 * U32_MAX when it has none. 948 */ 949 u32 exit_idx; 950 struct bpf_iarray *jt; /* jump table shared by all gotox of this subprogram */ 951 u16 stack_depth; /* max. stack depth used by this function */ 952 u16 stack_extra; 953 u32 insns_total; 954 u32 insns_self; 955 /* offsets in range [stack_depth .. fastcall_stack_off) 956 * are used for bpf_fastcall spills and fills. 957 */ 958 s16 fastcall_stack_off; 959 bool has_tail_call: 1; 960 bool might_throw: 1; 961 bool can_unwind: 1; /* has unwind_exit */ 962 bool tail_call_reachable: 1; 963 bool has_ld_abs: 1; 964 bool is_cb: 1; 965 bool is_async_cb: 1; 966 bool is_exception_cb: 1; 967 bool args_cached: 1; 968 /* true if the return value is passed in the R0:R2 register pair */ 969 bool ret_reg_pair: 1; 970 /* true if bpf_fastcall stack region is used by functions that can't be inlined */ 971 bool keep_fastcall_stack: 1; 972 bool changes_pkt_data: 1; 973 bool might_sleep: 1; 974 u8 arg_slot_cnt:4; 975 976 enum priv_stack_mode priv_stack_mode; 977 struct bpf_subprog_arg_info args[MAX_BPF_FUNC_ARGS]; 978 u16 stack_arg_cnt; /* incoming + max outgoing */ 979 u16 max_out_stack_arg_cnt; 980 }; 981 982 static inline u16 bpf_in_stack_arg_cnt(const struct bpf_subprog_info *sub) 983 { 984 if (sub->arg_slot_cnt > MAX_BPF_FUNC_REG_ARGS) 985 return sub->arg_slot_cnt - MAX_BPF_FUNC_REG_ARGS; 986 return 0; 987 } 988 989 struct bpf_diag; 990 struct bpf_verifier_env; 991 992 struct backtrack_state { 993 struct bpf_verifier_env *env; 994 u32 frame; 995 u32 reg_masks[MAX_CALL_FRAMES]; 996 unsigned long stack_masks[MAX_CALL_FRAMES][BITS_TO_LONGS(MAX_BPF_STACK_SLOTS)]; 997 u8 stack_arg_masks[MAX_CALL_FRAMES]; 998 }; 999 1000 struct bpf_id_pair { 1001 u32 old; 1002 u32 cur; 1003 }; 1004 1005 struct bpf_idmap { 1006 u32 tmp_id_gen; 1007 u32 cnt; 1008 struct bpf_id_pair map[BPF_ID_MAP_SIZE]; 1009 }; 1010 1011 struct bpf_idset { 1012 u32 num_ids; 1013 struct { 1014 u32 id; 1015 u32 cnt; 1016 } entries[BPF_ID_MAP_SIZE]; 1017 }; 1018 1019 /* see verifier.c:compute_scc_callchain() */ 1020 struct bpf_scc_callchain { 1021 /* call sites from bpf_verifier_state->frame[*]->callsite leading to this SCC */ 1022 u32 callsites[MAX_CALL_FRAMES - 1]; 1023 /* last frame in a chain is identified by SCC id */ 1024 u32 scc; 1025 }; 1026 1027 /* verifier state waiting for propagate_backedges() */ 1028 struct bpf_scc_backedge { 1029 struct bpf_scc_backedge *next; 1030 struct bpf_verifier_state state; 1031 }; 1032 1033 struct bpf_scc_visit { 1034 struct bpf_scc_callchain callchain; 1035 /* first state in current verification path that entered SCC 1036 * identified by the callchain 1037 */ 1038 struct bpf_verifier_state *entry_state; 1039 struct bpf_scc_backedge *backedges; /* list of backedges */ 1040 u32 num_backedges; 1041 }; 1042 1043 /* An array of bpf_scc_visit structs sharing tht same bpf_scc_callchain->scc 1044 * but having different bpf_scc_callchain->callsites. 1045 */ 1046 struct bpf_scc_info { 1047 u32 num_visits; 1048 struct bpf_scc_visit visits[]; 1049 }; 1050 1051 struct bpf_liveness; 1052 struct scev; 1053 1054 struct bpf_fd_array { 1055 union { 1056 struct bpf_map *map; 1057 struct btf *btf; 1058 unsigned long val; 1059 }; 1060 }; 1061 1062 /* single container for all structs 1063 * one verifier_env per bpf_check() call 1064 */ 1065 struct bpf_verifier_env { 1066 u32 insn_idx; 1067 u32 prev_insn_idx; 1068 struct bpf_prog *prog; /* eBPF program being verified */ 1069 const struct bpf_verifier_ops *ops; 1070 struct module *attach_btf_mod; /* The owner module of prog->aux->attach_btf */ 1071 struct bpf_verifier_stack_elem *head; /* stack of verifier states to be processed */ 1072 int stack_size; /* number of states to be processed */ 1073 bool strict_alignment; /* perform strict pointer alignment checks */ 1074 bool test_state_freq; /* test verifier with different pruning frequency */ 1075 bool test_reg_invariants; /* fail verification on register invariants violations */ 1076 struct bpf_verifier_state *cur_state; /* current verifier state */ 1077 /* Search pruning optimization, array of list_heads for 1078 * lists of struct bpf_verifier_state_list. 1079 */ 1080 struct list_head *explored_states; 1081 struct list_head free_list; /* list of struct bpf_verifier_state_list */ 1082 struct bpf_map *used_maps[MAX_USED_MAPS]; /* array of map's used by eBPF program */ 1083 struct btf_mod_pair used_btfs[MAX_USED_BTFS]; /* array of BTF's used by BPF program */ 1084 struct bpf_map *insn_array_maps[MAX_USED_MAPS]; /* array of INSN_ARRAY map's to be relocated */ 1085 u32 used_map_cnt; /* number of used maps */ 1086 u32 used_btf_cnt; /* number of used BTF objects */ 1087 u32 insn_array_map_cnt; /* number of used maps of type BPF_MAP_TYPE_INSN_ARRAY */ 1088 u32 id_gen; /* used to generate unique reg IDs */ 1089 u32 hidden_subprog_cnt; /* number of hidden subprogs */ 1090 int exception_callback_subprog; 1091 bool explore_alu_limits; 1092 bool allow_ptr_leaks; 1093 /* Allow access to uninitialized stack memory. Writes with fixed offset are 1094 * always allowed, so this refers to reads (with fixed or variable offset), 1095 * to writes with variable offset and to indirect (helper) accesses. 1096 */ 1097 bool allow_uninit_stack; 1098 bool bpf_capable; 1099 bool bypass_spec_v1; 1100 bool bypass_spec_v4; 1101 bool seen_direct_write; 1102 /* a global function is verified for BPF_FUNC_CTX_NO_RCU_LOCK */ 1103 bool no_rcu_read_lock; 1104 bool seen_exception; 1105 bool signature; 1106 u32 insn_aux_data_len; 1107 struct bpf_insn_aux_data *insn_aux_data; /* array of per-insn state */ 1108 const struct bpf_line_info *prev_linfo; 1109 struct bpf_verifier_log log; 1110 struct bpf_diag *diag; 1111 struct bpf_func_proto bpf_subprog_scratch; 1112 struct bpf_subprog_info subprog_info[BPF_MAX_SUBPROGS + 2]; /* max + 2 for the fake and exception subprogs */ 1113 /* subprog indices sorted in topological order: leaves first, callers last */ 1114 int subprog_topo_order[BPF_MAX_SUBPROGS + 2]; 1115 /* 1116 * Pointers to static subprogs found in frozen read-only maps of the 1117 * program, see resolve_func_ptrs(). Sorted by map and map_off. 1118 */ 1119 struct bpf_func_ptr *func_ptrs; 1120 u32 func_ptr_cnt; 1121 bool has_callx; 1122 /* 1123 * Call graph edges created by callx instructions. A bitmap of 1124 * subprog_cnt * subprog_cnt bits, where bit (caller * subprog_cnt + callee) 1125 * is set when the main verification pass sees 'caller' calling 'callee' 1126 * via callx. Allocated when the first such edge is recorded. 1127 */ 1128 unsigned long *callx_edges; 1129 union { 1130 struct bpf_idmap idmap_scratch; 1131 struct bpf_idset idset_scratch; 1132 }; 1133 struct { 1134 int *insn_state; 1135 int *insn_stack; 1136 /* 1137 * vector of instruction indexes sorted in post-order, grouped by subprogram, 1138 * see bpf_subprog_info->postorder_start. 1139 */ 1140 int *insn_postorder; 1141 int *postorder_nums; 1142 int cur_stack; 1143 /* current position in the insn_postorder vector */ 1144 int cur_postorder; 1145 u32 gotox_edges; 1146 bool subprog_jts_ready; 1147 } cfg; 1148 struct backtrack_state bt; 1149 struct bpf_jmp_history_entry *cur_hist_ent; 1150 /* Per-callsite snapshot of the parent's tracked spill slots for cross-frame fills. */ 1151 struct spill_snapshot **callsite_at_stack; 1152 u32 pass_cnt; /* number of times do_check() was called */ 1153 u32 subprog_cnt; 1154 /* number of instructions analyzed by the verifier */ 1155 u32 prev_insn_processed, insn_processed; 1156 /* number of jmps, calls, exits analyzed so far */ 1157 u32 prev_jmps_processed, jmps_processed; 1158 /* maximum combined stack depth */ 1159 u32 max_stack_depth; 1160 /* stack budget of the program, see bpf_prog_stack_limit() */ 1161 u32 stack_limit; 1162 /* total verification time */ 1163 u64 verification_time; 1164 /* maximum number of verifier states kept in 'branching' instructions */ 1165 u32 max_states_per_insn; 1166 /* total number of allocated verifier states */ 1167 u32 total_states; 1168 /* some states are freed during program analysis. 1169 * this is peak number of states. this number dominates kernel 1170 * memory consumption during verification 1171 */ 1172 u32 peak_states; 1173 /* longest register parentage chain walked for liveness marking */ 1174 u32 longest_mark_read_walk; 1175 u32 free_list_size; 1176 u32 explored_states_size; 1177 u32 num_backedges; 1178 /* 1179 * The program's fd_array comes in two shapes, told apart by whether 1180 * the caller passed fd_array_cnt. They are mutually exclusive: 1181 * - continuous (fd_array_cnt given): ->fd_array holds every entry 1182 * resolved to its object up front, indexed by fd_array position, 1183 * with ->fd_array_cnt slots; ->fd_array_raw is unused. 1184 * - sparse (no fd_array_cnt): ->fd_array is NULL, and entries are 1185 * read from ->fd_array_raw (the caller's fd_array) and resolved 1186 * on the spot at each reference. 1187 */ 1188 struct bpf_fd_array *fd_array; 1189 u32 fd_array_cnt; 1190 bpfptr_t fd_array_raw; 1191 1192 /* bit mask to keep track of whether a register has been accessed 1193 * since the last time the function state was printed 1194 */ 1195 u32 scratched_regs; 1196 /* Same as scratched_regs but for stack slots */ 1197 DECLARE_BITMAP(scratched_stack_slots, MAX_BPF_STACK_SLOTS); 1198 u64 prev_log_pos, prev_insn_print_pos; 1199 /* buffer used to temporary hold constants as scalar registers */ 1200 struct bpf_reg_state fake_reg[1]; 1201 /* buffers used to save updated reg states while simulating branches */ 1202 struct bpf_reg_state true_reg1, true_reg2, false_reg1, false_reg2; 1203 /* buffer used to generate temporary string representations, 1204 * e.g., in reg_type_str() to generate reg_type string 1205 */ 1206 char tmp_str_buf[TMP_STR_BUF_LEN]; 1207 char tmp_arg_name[32]; 1208 struct bpf_insn insn_buf[INSN_BUF_SIZE]; 1209 struct bpf_insn epilogue_buf[INSN_BUF_SIZE]; 1210 struct bpf_scc_callchain callchain_buf; 1211 struct bpf_liveness *liveness; 1212 /* array of pointers to bpf_scc_info indexed by SCC id */ 1213 struct bpf_scc_info **scc_info; 1214 u32 scc_cnt; 1215 struct bpf_iarray *succ; 1216 struct bpf_iarray *gotox_tmp_buf; 1217 int *idoms; 1218 struct scev *scev; 1219 }; 1220 1221 static inline struct bpf_func_info_aux *subprog_aux(struct bpf_verifier_env *env, int subprog) 1222 { 1223 return &env->prog->aux->func_info_aux[subprog]; 1224 } 1225 1226 static inline struct bpf_subprog_info *subprog_info(struct bpf_verifier_env *env, int subprog) 1227 { 1228 return &env->subprog_info[subprog]; 1229 } 1230 1231 static inline bool bpf_ret_reg_pair(struct bpf_verifier_env *env, int subprog) 1232 { 1233 return subprog_info(env, subprog)->ret_reg_pair; 1234 } 1235 1236 struct bpf_call_summary { 1237 u8 arg_slot_cnt; 1238 bool is_void; 1239 bool fastcall; 1240 }; 1241 1242 static inline bool bpf_helper_call(const struct bpf_insn *insn) 1243 { 1244 return insn->code == (BPF_JMP | BPF_CALL) && 1245 insn->src_reg == 0; 1246 } 1247 1248 static inline bool bpf_pseudo_call(const struct bpf_insn *insn) 1249 { 1250 return insn->code == (BPF_JMP | BPF_CALL) && 1251 insn->src_reg == BPF_PSEUDO_CALL; 1252 } 1253 1254 static inline bool bpf_pseudo_kfunc_call(const struct bpf_insn *insn) 1255 { 1256 return insn->code == (BPF_JMP | BPF_CALL) && 1257 insn->src_reg == BPF_PSEUDO_KFUNC_CALL; 1258 } 1259 1260 /* callx: indirect call of a bpf subprog whose address is in insn->dst_reg */ 1261 static inline bool bpf_is_callx(const struct bpf_insn *insn) 1262 { 1263 return insn->code == (BPF_JMP | BPF_CALL | BPF_X); 1264 } 1265 1266 __printf(2, 0) void bpf_verifier_vlog(struct bpf_verifier_log *log, 1267 const char *fmt, va_list args); 1268 __printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env, 1269 const char *fmt, ...); 1270 __printf(2, 3) void bpf_log(struct bpf_verifier_log *log, 1271 const char *fmt, ...); 1272 int bpf_vlog_init(struct bpf_verifier_log *log, u32 log_level, 1273 char __user *log_buf, u32 log_size); 1274 void bpf_vlog_reset(struct bpf_verifier_log *log, u64 new_pos); 1275 int bpf_vlog_finalize(struct bpf_verifier_log *log, u32 *log_size_actual); 1276 1277 __printf(3, 4) void verbose_linfo(struct bpf_verifier_env *env, 1278 u32 insn_off, 1279 const char *prefix_fmt, ...); 1280 void bpf_verbose_snum(struct bpf_verifier_env *env, s64 num); 1281 void bpf_verbose_snum32(struct bpf_verifier_env *env, s32 num); 1282 1283 #define verifier_bug_if(cond, env, fmt, args...) \ 1284 ({ \ 1285 bool __cond = (cond); \ 1286 if (unlikely(__cond)) \ 1287 verifier_bug(env, fmt " (" #cond ")", ##args); \ 1288 (__cond); \ 1289 }) 1290 #define verifier_bug(env, fmt, args...) \ 1291 ({ \ 1292 BPF_WARN_ONCE(1, "verifier bug: " fmt "\n", ##args); \ 1293 bpf_log(&env->log, "verifier bug: " fmt "\n", ##args); \ 1294 }) 1295 1296 static inline void mark_prune_point(struct bpf_verifier_env *env, int idx) 1297 { 1298 env->insn_aux_data[idx].prune_point = true; 1299 } 1300 1301 static inline bool bpf_is_prune_point(struct bpf_verifier_env *env, int insn_idx) 1302 { 1303 return env->insn_aux_data[insn_idx].prune_point; 1304 } 1305 1306 static inline void mark_force_checkpoint(struct bpf_verifier_env *env, int idx) 1307 { 1308 env->insn_aux_data[idx].force_checkpoint = true; 1309 } 1310 1311 static inline bool bpf_is_force_checkpoint(struct bpf_verifier_env *env, int insn_idx) 1312 { 1313 return env->insn_aux_data[insn_idx].force_checkpoint; 1314 } 1315 1316 static inline void clear_scalar_id(struct bpf_reg_state *reg) 1317 { 1318 reg->id = 0; 1319 reg->delta = 0; 1320 } 1321 1322 static inline void mark_calls_callback(struct bpf_verifier_env *env, int idx) 1323 { 1324 env->insn_aux_data[idx].calls_callback = true; 1325 } 1326 1327 static inline bool bpf_calls_callback(struct bpf_verifier_env *env, int insn_idx) 1328 { 1329 return env->insn_aux_data[insn_idx].calls_callback; 1330 } 1331 1332 static inline void mark_jmp_point(struct bpf_verifier_env *env, int idx) 1333 { 1334 env->insn_aux_data[idx].jmp_point = true; 1335 } 1336 1337 static inline void mark_jump_target(struct bpf_verifier_env *env, int idx) 1338 { 1339 env->insn_aux_data[idx].jump_target = true; 1340 } 1341 1342 static inline bool bpf_is_jump_target(struct bpf_verifier_env *env, int insn_idx) 1343 { 1344 return env->insn_aux_data[insn_idx].jump_target; 1345 } 1346 1347 static inline struct bpf_func_state *cur_func(struct bpf_verifier_env *env) 1348 { 1349 struct bpf_verifier_state *cur = env->cur_state; 1350 1351 return cur->frame[cur->curframe]; 1352 } 1353 1354 static inline struct bpf_reg_state *cur_regs(struct bpf_verifier_env *env) 1355 { 1356 return cur_func(env)->regs; 1357 } 1358 1359 int bpf_prog_offload_verifier_prep(struct bpf_prog *prog); 1360 int bpf_prog_offload_verify_insn(struct bpf_verifier_env *env, 1361 int insn_idx, int prev_insn_idx); 1362 int bpf_prog_offload_finalize(struct bpf_verifier_env *env); 1363 void 1364 bpf_prog_offload_replace_insn(struct bpf_verifier_env *env, u32 off, 1365 struct bpf_insn *insn); 1366 void 1367 bpf_prog_offload_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt); 1368 1369 /* this lives here instead of in bpf.h because it needs to dereference tgt_prog */ 1370 static inline u64 bpf_trampoline_compute_key(const struct bpf_prog *tgt_prog, 1371 struct btf *btf, u32 btf_id) 1372 { 1373 if (tgt_prog) 1374 return ((u64)tgt_prog->aux->id << 32) | btf_id; 1375 else 1376 return ((u64)btf_obj_id(btf) << 32) | 0x80000000 | btf_id; 1377 } 1378 1379 /* unpack the IDs from the key as constructed above */ 1380 static inline void bpf_trampoline_unpack_key(u64 key, u32 *obj_id, u32 *btf_id) 1381 { 1382 if (obj_id) 1383 *obj_id = key >> 32; 1384 if (btf_id) 1385 *btf_id = key & 0x7FFFFFFF; 1386 } 1387 1388 int bpf_prepare_btf_info(struct bpf_verifier_env *env, 1389 const union bpf_attr *attr, bpfptr_t uattr); 1390 int bpf_check_core_relo(struct bpf_verifier_env *env, 1391 const union bpf_attr *attr, bpfptr_t uattr); 1392 int bpf_check_btf_info(struct bpf_verifier_env *env, 1393 const union bpf_attr *attr, bpfptr_t uattr); 1394 1395 int bpf_check_attach_target(struct bpf_verifier_log *log, 1396 const struct bpf_prog *prog, 1397 const struct bpf_prog *tgt_prog, 1398 u32 btf_id, 1399 struct bpf_attach_target_info *tgt_info); 1400 int bpf_check_ext_prologue(struct bpf_verifier_log *log, 1401 const struct bpf_prog *tgt_prog, u32 btf_id); 1402 void bpf_free_kfunc_btf_tab(struct bpf_kfunc_btf_tab *tab); 1403 1404 int mark_chain_precision(struct bpf_verifier_env *env, int regno); 1405 1406 int bpf_is_state_visited(struct bpf_verifier_env *env, int insn_idx); 1407 int bpf_split_cur_state(struct bpf_verifier_env *env); 1408 int bpf_update_branch_counts(struct bpf_verifier_env *env, struct bpf_verifier_state *st); 1409 1410 void bpf_clear_jmp_history(struct bpf_verifier_state *state); 1411 int bpf_copy_verifier_state(struct bpf_verifier_state *dst_state, 1412 const struct bpf_verifier_state *src); 1413 struct list_head *bpf_explored_state(struct bpf_verifier_env *env, int idx); 1414 void bpf_free_verifier_state(struct bpf_verifier_state *state, bool free_self); 1415 void bpf_free_backedges(struct bpf_scc_visit *visit); 1416 int bpf_push_jmp_history(struct bpf_verifier_env *env, struct bpf_verifier_state *cur, 1417 int insn_flags, int spi, int frame, const u16 *linked_regs, 1418 u8 linked_regs_cnt); 1419 void bpf_bt_sync_linked_regs(struct backtrack_state *bt, struct bpf_jmp_history_entry *hist); 1420 void bpf_mark_reg_not_init(const struct bpf_verifier_env *env, 1421 struct bpf_reg_state *reg); 1422 void bpf_mark_reg_known_scalar(struct bpf_reg_state *reg, u64 imm); 1423 void bpf_mark_reg_unknown_imprecise(struct bpf_reg_state *reg); 1424 int bpf_grow_stack_state(struct bpf_verifier_env *env, struct bpf_func_state *state, int size); 1425 void bpf_mark_all_scalars_precise(struct bpf_verifier_env *env, 1426 struct bpf_verifier_state *st); 1427 void bpf_clear_singular_ids(struct bpf_verifier_env *env, struct bpf_verifier_state *st); 1428 int bpf_mark_chain_precision(struct bpf_verifier_env *env, 1429 struct bpf_verifier_state *starting_state, 1430 int regno, bool *changed); 1431 1432 static inline int bpf_get_spi(s32 off) 1433 { 1434 return (-off - 1) / BPF_REG_SIZE; 1435 } 1436 1437 /* 1438 * Stack a program may use in total: combined over the frames of a call 1439 * chain on the kernel stack, or per frame on a private stack. Any single 1440 * frame may reach that deep. Only a JIT that lays out such frames may go 1441 * beyond MAX_BPF_STACK, the interpreter's frame size, and only one whose 1442 * tail calls let the target set up its own frame: without subprogram 1443 * tail calls, do_misc_fixups() gives every program with tail calls a 1444 * MAX_BPF_STACK frame, which a deeper frame would overrun. 1445 */ 1446 static inline u32 bpf_prog_stack_limit(const struct bpf_prog *prog) 1447 { 1448 /* an offloaded program never runs on the host JIT, whatever it supports */ 1449 if (prog->jit_requested && !bpf_prog_is_offloaded(prog->aux) && 1450 bpf_jit_supports_large_stack() && bpf_jit_supports_subprog_tailcalls()) 1451 return MAX_BPF_STACK_JIT; 1452 return MAX_BPF_STACK; 1453 } 1454 1455 /* 1456 * Return the function state a stack pointer register refers to. frameno 1457 * shares storage with other pointer metadata, so return NULL for any 1458 * other register type instead of indexing frame[] with aliased bytes. 1459 */ 1460 static inline struct bpf_func_state *bpf_func(struct bpf_verifier_env *env, 1461 const struct bpf_reg_state *reg) 1462 { 1463 struct bpf_verifier_state *cur = env->cur_state; 1464 1465 if (reg->type != PTR_TO_STACK) 1466 return NULL; 1467 return cur->frame[reg->frameno]; 1468 } 1469 1470 /* Return IP for a given frame in a call stack */ 1471 static inline u32 bpf_frame_insn_idx(struct bpf_verifier_state *st, u32 frame) 1472 { 1473 return frame == st->curframe 1474 ? st->insn_idx 1475 : st->frame[frame + 1]->callsite; 1476 } 1477 1478 static inline bool bpf_is_jmp_point(struct bpf_verifier_env *env, int insn_idx) 1479 { 1480 return env->insn_aux_data[insn_idx].jmp_point; 1481 } 1482 1483 static inline bool bpf_is_spilled_reg(const struct bpf_stack_state *stack) 1484 { 1485 return stack->slot_type[BPF_REG_SIZE - 1] == STACK_SPILL; 1486 } 1487 1488 static inline int bpf_spill_size(const struct bpf_stack_state *stack) 1489 { 1490 int i, spill_size = 0; 1491 1492 for (i = BPF_REG_SIZE - 1; i >= 0 && stack->slot_type[i] == STACK_SPILL; i--) 1493 spill_size++; 1494 return spill_size; 1495 } 1496 1497 static inline bool bpf_is_spilled_scalar_reg(const struct bpf_stack_state *stack) 1498 { 1499 return bpf_is_spilled_reg(stack) && stack->spilled_ptr.type == SCALAR_VALUE; 1500 } 1501 1502 static inline bool bpf_register_is_null(struct bpf_reg_state *reg) 1503 { 1504 return reg->type == SCALAR_VALUE && tnum_equals_const(reg->var_off, 0); 1505 } 1506 1507 static inline void bpf_bt_set_frame_reg(struct backtrack_state *bt, u32 frame, u32 reg) 1508 { 1509 bt->reg_masks[frame] |= 1 << reg; 1510 } 1511 1512 static inline void bpf_bt_set_frame_slot(struct backtrack_state *bt, u32 frame, u32 slot) 1513 { 1514 __set_bit(slot, bt->stack_masks[frame]); 1515 } 1516 1517 static inline void bt_set_frame_stack_arg_slot(struct backtrack_state *bt, u32 frame, u32 slot) 1518 { 1519 bt->stack_arg_masks[frame] |= 1 << slot; 1520 } 1521 1522 static inline bool bt_is_frame_reg_set(struct backtrack_state *bt, u32 frame, u32 reg) 1523 { 1524 return bt->reg_masks[frame] & (1 << reg); 1525 } 1526 1527 static inline bool bt_is_frame_slot_set(struct backtrack_state *bt, u32 frame, u32 slot) 1528 { 1529 return test_bit(slot, bt->stack_masks[frame]); 1530 } 1531 1532 bool bpf_map_is_rdonly(const struct bpf_map *map); 1533 struct bpf_func_ptr *bpf_map_func_ptrs(struct bpf_verifier_env *env, 1534 const struct bpf_map *map, u32 *cnt); 1535 struct bpf_func_ptr *bpf_map_range_func_ptrs(struct bpf_verifier_env *env, 1536 const struct bpf_map *map, 1537 u64 off, u64 size, u32 *cnt); 1538 void bpf_adjust_func_ptrs(struct bpf_verifier_env *env, u32 off, u32 len); 1539 void bpf_adjust_func_ptrs_after_remove(struct bpf_verifier_env *env, u32 off, u32 len); 1540 int bpf_map_direct_read(struct bpf_map *map, int off, int size, u64 *val, 1541 bool is_ldsx); 1542 1543 #define BPF_BASE_TYPE_MASK GENMASK(BPF_BASE_TYPE_BITS - 1, 0) 1544 1545 /* extract base type from bpf_{arg, return, reg}_type. */ 1546 static inline u32 base_type(u32 type) 1547 { 1548 return type & BPF_BASE_TYPE_MASK; 1549 } 1550 1551 /* extract flags from an extended type. See bpf_type_flag in bpf.h. */ 1552 static inline u32 type_flag(u32 type) 1553 { 1554 return type & ~BPF_BASE_TYPE_MASK; 1555 } 1556 1557 static inline bool bpf_is_ptr_to_mem_or_btf_id(enum bpf_reg_type type) 1558 { 1559 switch (base_type(type)) { 1560 case PTR_TO_MEM: 1561 case PTR_TO_BTF_ID: 1562 return true; 1563 default: 1564 return false; 1565 } 1566 } 1567 1568 static inline bool bpf_may_fault_on_deref(enum bpf_reg_type type) 1569 { 1570 /* 1571 * The pointer types which must not be dereferenced without fault 1572 * protection, that is, the ones bpf_convert_ctx_accesses() has to 1573 * turn a BPF_LDX into a BPF_PROBE_MEM one for. 1574 */ 1575 return type == PTR_TO_BTF_ID || (type_flag(type) & PTR_UNTRUSTED); 1576 } 1577 1578 static inline bool bpf_prog_has_arena_ctx_arg(const struct bpf_prog *prog) 1579 { 1580 int i; 1581 1582 for (i = 0; i < prog->aux->ctx_arg_info_size; i++) 1583 if (base_type(prog->aux->ctx_arg_info[i].reg_type) == PTR_TO_ARENA) 1584 return true; 1585 return false; 1586 } 1587 1588 static inline enum bpf_prog_type resolve_prog_type(const struct bpf_prog *prog) 1589 { 1590 return (prog->type == BPF_PROG_TYPE_EXT && prog->aux->saved_dst_prog_type) ? 1591 prog->aux->saved_dst_prog_type : prog->type; 1592 } 1593 1594 static inline bool bpf_prog_check_recur(const struct bpf_prog *prog) 1595 { 1596 switch (resolve_prog_type(prog)) { 1597 case BPF_PROG_TYPE_TRACING: 1598 return prog->expected_attach_type != BPF_TRACE_ITER; 1599 case BPF_PROG_TYPE_STRUCT_OPS: 1600 return prog->aux->jits_use_priv_stack; 1601 case BPF_PROG_TYPE_LSM: 1602 case BPF_PROG_TYPE_SYSCALL: 1603 return false; 1604 default: 1605 return true; 1606 } 1607 } 1608 1609 #define BPF_REG_TRUSTED_MODIFIERS (MEM_ALLOC | PTR_TRUSTED | NON_OWN_REF) 1610 1611 static inline bool bpf_type_has_unsafe_modifiers(u32 type) 1612 { 1613 return type_flag(type) & ~BPF_REG_TRUSTED_MODIFIERS; 1614 } 1615 1616 static inline bool type_is_ptr_alloc_obj(u32 type) 1617 { 1618 return base_type(type) == PTR_TO_BTF_ID && 1619 type_flag(type) & MEM_ALLOC && 1620 !(type_flag(type) & PTR_UNTRUSTED); 1621 } 1622 1623 static inline bool type_is_non_owning_ref(u32 type) 1624 { 1625 return type_is_ptr_alloc_obj(type) && type_flag(type) & NON_OWN_REF; 1626 } 1627 1628 static inline bool type_is_map_ptr(enum bpf_reg_type type) 1629 { 1630 switch (base_type(type)) { 1631 case CONST_PTR_TO_MAP: 1632 case PTR_TO_MAP_KEY: 1633 case PTR_TO_MAP_VALUE: 1634 return true; 1635 default: 1636 return false; 1637 } 1638 } 1639 1640 static inline bool type_is_pkt_pointer(enum bpf_reg_type type) 1641 { 1642 type = base_type(type); 1643 return type == PTR_TO_PACKET || 1644 type == PTR_TO_PACKET_META; 1645 } 1646 1647 static inline bool type_is_sk_pointer(enum bpf_reg_type type) 1648 { 1649 return type == PTR_TO_SOCKET || 1650 type == PTR_TO_SOCK_COMMON || 1651 type == PTR_TO_TCP_SOCK || 1652 type == PTR_TO_XDP_SOCK; 1653 } 1654 1655 static inline bool type_may_be_null(u32 type) 1656 { 1657 return type & PTR_MAYBE_NULL; 1658 } 1659 1660 static inline void mark_reg_scratched(struct bpf_verifier_env *env, u32 regno) 1661 { 1662 env->scratched_regs |= 1U << regno; 1663 } 1664 1665 static inline void mark_stack_slot_scratched(struct bpf_verifier_env *env, u32 spi) 1666 { 1667 __set_bit(spi, env->scratched_stack_slots); 1668 } 1669 1670 static inline bool reg_scratched(const struct bpf_verifier_env *env, u32 regno) 1671 { 1672 return (env->scratched_regs >> regno) & 1; 1673 } 1674 1675 static inline bool stack_slot_scratched(const struct bpf_verifier_env *env, u32 spi) 1676 { 1677 return test_bit(spi, env->scratched_stack_slots); 1678 } 1679 1680 static inline bool verifier_state_scratched(const struct bpf_verifier_env *env) 1681 { 1682 return env->scratched_regs || 1683 !bitmap_empty(env->scratched_stack_slots, MAX_BPF_STACK_SLOTS); 1684 } 1685 1686 static inline void mark_verifier_state_clean(struct bpf_verifier_env *env) 1687 { 1688 env->scratched_regs = 0U; 1689 bitmap_zero(env->scratched_stack_slots, MAX_BPF_STACK_SLOTS); 1690 } 1691 1692 /* Used for printing the entire verifier state. */ 1693 static inline void mark_verifier_state_scratched(struct bpf_verifier_env *env) 1694 { 1695 env->scratched_regs = ~0U; 1696 bitmap_fill(env->scratched_stack_slots, MAX_BPF_STACK_SLOTS); 1697 } 1698 1699 static inline bool bpf_stack_narrow_access_ok(int off, int fill_size, int spill_size) 1700 { 1701 #ifdef __BIG_ENDIAN 1702 off -= spill_size - fill_size; 1703 #endif 1704 1705 return !(off % BPF_REG_SIZE); 1706 } 1707 1708 static inline bool insn_is_gotox(struct bpf_insn *insn) 1709 { 1710 return BPF_CLASS(insn->code) == BPF_JMP && 1711 BPF_OP(insn->code) == BPF_JA && 1712 BPF_SRC(insn->code) == BPF_X; 1713 } 1714 1715 const char *reg_type_str(struct bpf_verifier_env *env, enum bpf_reg_type type); 1716 const char *dynptr_type_str(enum bpf_dynptr_type type); 1717 const char *iter_type_str(const struct btf *btf, u32 btf_id); 1718 const char *iter_state_str(enum bpf_iter_state state); 1719 1720 void print_verifier_state(struct bpf_verifier_env *env, const struct bpf_verifier_state *vstate, 1721 u32 frameno, bool print_all); 1722 void print_insn_state(struct bpf_verifier_env *env, const struct bpf_verifier_state *vstate, 1723 u32 frameno); 1724 u32 bpf_vlog_alignment(u32 pos); 1725 const char *bpf_disasm_kfunc_name(void *data, const struct bpf_insn *insn); 1726 1727 struct bpf_subprog_info *bpf_find_containing_subprog(struct bpf_verifier_env *env, int off); 1728 const char *bpf_subprog_name(const struct bpf_verifier_env *env, int subprog); 1729 int bpf_jmp_offset(struct bpf_insn *insn); 1730 struct bpf_iarray *bpf_insn_successors(struct bpf_verifier_env *env, u32 idx); 1731 void bpf_fmt_stack_mask(char *buf, ssize_t buf_sz, const unsigned long *stack_mask); 1732 bool bpf_subprog_is_global(const struct bpf_verifier_env *env, int subprog); 1733 1734 /* Kinds of member a by-value struct or union may be composed of. */ 1735 enum btf_member_kind { 1736 BTF_MEMBER_SCALAR = BIT(0), /* an int or an enum */ 1737 BTF_MEMBER_ARENA_PTR = BIT(1), /* a pointer carrying the "arena" type tag */ 1738 }; 1739 1740 bool btf_struct_is_composed_of(struct bpf_verifier_env *env, const struct btf *btf, 1741 const struct btf_type *t, u32 member_kinds); 1742 u32 btf_func_arg_align(const struct btf *btf, const struct btf_type *t); 1743 1744 int bpf_find_subprog(struct bpf_verifier_env *env, int off); 1745 bool bpf_is_throw_kfunc(struct bpf_insn *insn); 1746 int bpf_compute_const_regs(struct bpf_verifier_env *env); 1747 int bpf_prune_dead_branches(struct bpf_verifier_env *env); 1748 int bpf_check_cfg(struct bpf_verifier_env *env); 1749 void bpf_free_subprog_jts(struct bpf_verifier_env *env); 1750 int bpf_compute_postorder(struct bpf_verifier_env *env); 1751 int bpf_compute_scc(struct bpf_verifier_env *env); 1752 1753 struct bpf_map_desc { 1754 struct bpf_map *ptr; 1755 int uid; 1756 }; 1757 1758 /* The last initialized dynptr; Populated by process_dynptr_func() */ 1759 struct bpf_dynptr_desc { 1760 enum bpf_dynptr_type type; 1761 u32 id; 1762 u32 parent_id; 1763 }; 1764 1765 /* 1766 * The last seen rereferenced object; Updated by update_ref_obj() when a register refers to a 1767 * referenced object. Used when the helper or kfunc is casting a referenced object, returning 1768 * allocated memory derived from referenced object or creating a dynptr with a referenced 1769 * object as parent. 1770 */ 1771 struct ref_obj_desc { 1772 u32 id; 1773 u32 parent_id; 1774 u8 cnt; 1775 }; 1776 1777 /* 1778 * Generic MEM_UNINIT arguments, indexed by ABI slot. var_size_mask excludes 1779 * variable-sized buffers from raw mode without losing the output annotation. 1780 * size records constant ranges to mark initialized after checking all arguments, 1781 * only when the caller is allowed to read uninitialized stack memory. 1782 */ 1783 struct arg_raw_mem_desc { 1784 u16 mask; 1785 u16 var_size_mask; 1786 int size[MAX_BPF_FUNC_ARGS]; 1787 }; 1788 1789 /* Size of PTR_TO_MEM returned, taken from a constant allocation-size argument */ 1790 struct ret_mem_desc { 1791 u32 size; 1792 bool found; 1793 }; 1794 1795 /* A constant scalar argument; Populated by process_const_arg() */ 1796 struct arg_constant_desc { 1797 u64 value; 1798 bool found; 1799 }; 1800 1801 struct bpf_call_arg_meta { 1802 /* Common */ 1803 struct btf *btf; 1804 u32 func_id; 1805 const struct bpf_func_proto *fn; 1806 const struct btf_type *func_proto; 1807 u8 release_regno; 1808 u32 ret_btf_id; 1809 u32 subprogno; 1810 struct bpf_map_desc map; 1811 struct bpf_dynptr_desc dynptr; 1812 struct ref_obj_desc ref_obj; 1813 struct ret_mem_desc ret_mem; 1814 struct arg_raw_mem_desc arg_raw_mem; 1815 1816 /* Only set by subprog */ 1817 bool subprog_may_change_pkt; 1818 1819 /* Only set by kfunc */ 1820 bool r0_rdonly; 1821 u32 kfunc_flags; 1822 const char *func_name; 1823 struct arg_constant_desc arg_constant; 1824 /* The type of the dynptr that the kfunc writes through */ 1825 enum bpf_dynptr_type dynptr_write_type; 1826 1827 /* arg_{btf,btf_id,owning_ref} are used by kfunc-specific handling, 1828 * generally to pass info about user-defined local kptr types to later 1829 * verification logic 1830 * bpf_obj_drop/bpf_percpu_obj_drop 1831 * Record the local kptr type to be drop'd 1832 * bpf_refcount_acquire (via ARG_PTR_TO_REFCOUNTED_KPTR arg type) 1833 * Record the local kptr type to be refcount_incr'd and use 1834 * arg_owning_ref to determine whether refcount_acquire should be 1835 * fallible 1836 */ 1837 struct btf *arg_btf; 1838 u32 arg_btf_id; 1839 bool arg_owning_ref; 1840 1841 struct { 1842 struct btf_field *field; 1843 } arg_list_head; 1844 struct { 1845 struct btf_field *field; 1846 } arg_rbtree_root; 1847 struct { 1848 u8 spi; 1849 u8 frameno; 1850 } iter; 1851 1852 /* Only set by helper */ 1853 u64 msize_max_value; 1854 s64 const_map_key; 1855 struct btf *ret_btf; 1856 struct btf_field *kptr_field; 1857 }; 1858 1859 int bpf_get_helper_proto(struct bpf_verifier_env *env, int func_id, 1860 const struct bpf_func_proto **ptr); 1861 int bpf_fetch_kfunc_arg_meta(struct bpf_verifier_env *env, s32 func_id, 1862 s16 offset, struct bpf_call_arg_meta *meta); 1863 bool bpf_is_async_callback_calling_insn(struct bpf_insn *insn); 1864 bool bpf_is_sync_callback_calling_insn(struct bpf_insn *insn); 1865 static inline bool bpf_is_iter_next_kfunc(struct bpf_call_arg_meta *meta) 1866 { 1867 return meta->kfunc_flags & KF_ITER_NEXT; 1868 } 1869 1870 static inline bool bpf_is_kfunc_sleepable(struct bpf_call_arg_meta *meta) 1871 { 1872 return meta->kfunc_flags & KF_SLEEPABLE; 1873 } 1874 bool bpf_is_kfunc_pkt_changing(struct bpf_call_arg_meta *meta); 1875 struct bpf_iarray *bpf_iarray_realloc(struct bpf_iarray *old, size_t n_elem); 1876 int bpf_copy_insn_array_uniq(struct bpf_map *map, u32 start, u32 end, u32 *off); 1877 bool bpf_insn_is_cond_jump(u8 code); 1878 bool bpf_is_may_goto_insn(struct bpf_insn *insn); 1879 1880 void bpf_verbose_insn(struct bpf_verifier_env *env, struct bpf_insn *insn); 1881 bool bpf_get_call_summary(struct bpf_verifier_env *env, struct bpf_insn *call, 1882 struct bpf_call_summary *cs); 1883 /* Stack effects used for state pruning; must_write implies may_write. */ 1884 struct arg_access_info { 1885 u32 size; /* Maximum extent; U32_MAX if unknown. */ 1886 u8 may_read:1; /* Incoming contents or initialization may be needed. */ 1887 u8 may_write:1; 1888 u8 must_write:1; /* Prior verifier state is destroyed throughout size. */ 1889 }; 1890 1891 struct arg_access_info 1892 bpf_helper_stack_access_bytes(struct bpf_verifier_env *env, 1893 struct bpf_insn *insn, int arg, int insn_idx); 1894 struct arg_access_info 1895 bpf_kfunc_stack_access_bytes(struct bpf_verifier_env *env, 1896 struct bpf_insn *insn, int arg, int insn_idx); 1897 struct arg_access_info 1898 bpf_global_subprog_stack_access_bytes(struct bpf_verifier_env *env, 1899 struct bpf_insn *insn, int arg, int insn_idx); 1900 int bpf_compute_subprog_arg_access(struct bpf_verifier_env *env); 1901 bool bpf_same_memory_origin(const struct bpf_reg_state *reg_a, 1902 const struct bpf_reg_state *reg_b); 1903 int bpf_set_reg_range(struct bpf_verifier_env *env, struct bpf_reg_state *reg, 1904 struct cnum64 r64, struct cnum32 r32, u16 base, u16 step); 1905 bool bpf_reg_union_compatible(const struct bpf_reg_state *a, const struct bpf_reg_state *b); 1906 int bpf_reg_union(struct bpf_verifier_env *env, struct bpf_reg_state *acc, 1907 const struct bpf_reg_state *src); 1908 1909 int bpf_stack_liveness_init(struct bpf_verifier_env *env); 1910 void bpf_stack_liveness_free(struct bpf_verifier_env *env); 1911 int bpf_live_stack_query_init(struct bpf_verifier_env *env, struct bpf_verifier_state *st); 1912 const unsigned long *bpf_may_write_mask(struct bpf_verifier_env *env, u32 insn_idx); 1913 bool bpf_needs_fixed_stack_off(struct bpf_verifier_env *env, int insn_idx); 1914 bool bpf_stack_slot_alive(struct bpf_verifier_env *env, u32 frameno, u32 spi); 1915 int bpf_compute_live_registers(struct bpf_verifier_env *env); 1916 1917 #define BPF_MAP_KEY_POISON (1ULL << 63) 1918 #define BPF_MAP_KEY_SEEN (1ULL << 62) 1919 1920 static inline bool bpf_map_ptr_poisoned(const struct bpf_insn_aux_data *aux) 1921 { 1922 return aux->map_ptr_state.poison; 1923 } 1924 1925 static inline bool bpf_map_ptr_unpriv(const struct bpf_insn_aux_data *aux) 1926 { 1927 return aux->map_ptr_state.unpriv; 1928 } 1929 1930 static inline bool bpf_map_key_poisoned(const struct bpf_insn_aux_data *aux) 1931 { 1932 return aux->map_key_state & BPF_MAP_KEY_POISON; 1933 } 1934 1935 static inline bool bpf_map_key_unseen(const struct bpf_insn_aux_data *aux) 1936 { 1937 return !(aux->map_key_state & BPF_MAP_KEY_SEEN); 1938 } 1939 1940 static inline u64 bpf_map_key_immediate(const struct bpf_insn_aux_data *aux) 1941 { 1942 return aux->map_key_state & ~(BPF_MAP_KEY_SEEN | BPF_MAP_KEY_POISON); 1943 } 1944 1945 static inline bool bpf_is_mem_insn(struct bpf_insn *insn) 1946 { 1947 if (BPF_CLASS(insn->code) != BPF_ST && 1948 BPF_CLASS(insn->code) != BPF_STX && 1949 BPF_CLASS(insn->code) != BPF_LDX) 1950 return false; 1951 1952 if (insn->code == (BPF_ST | BPF_NOSPEC)) 1953 return false; 1954 1955 return (BPF_MODE(insn->code) == BPF_MEM || 1956 BPF_MODE(insn->code) == BPF_MEMSX || 1957 BPF_MODE(insn->code) == BPF_ATOMIC); 1958 } 1959 1960 #define MAX_PACKET_OFF 0xffff 1961 #define CALLER_SAVED_REGS 6 1962 1963 enum bpf_reg_arg_type { 1964 SRC_OP, /* register is used as source operand */ 1965 DST_OP, /* register is used as destination operand */ 1966 DST_OP_NO_MARK /* same as above, check only, don't mark */ 1967 }; 1968 1969 #define MAX_KFUNC_DESCS 256 1970 /* Each kfunc can have its canonical and one specialized call target. */ 1971 #define MAX_KFUNC_CALL_DESCS (MAX_KFUNC_DESCS * 2) 1972 static_assert(MAX_KFUNC_CALL_DESCS <= S16_MAX + 1); 1973 1974 struct bpf_kfunc_desc { 1975 struct btf_func_model func_model; 1976 struct bpf_func_proto proto; 1977 u32 func_id; 1978 u16 offset; 1979 unsigned long addr; 1980 }; 1981 1982 struct bpf_kfunc_desc_tab { 1983 u32 nr_descs; 1984 u32 nr_base_descs; 1985 /* Sorted by func_id (BTF ID) and offset (fd_array offset) during 1986 * verification. JITs use the descriptor index stored in the finalized 1987 * call's off field. The first nr_base_descs entries are the canonical 1988 * descriptors used for verifier lookups. Call specialization may append 1989 * immutable descriptors for additional targets. 1990 * 1991 * Grown one entry at a time by bpf_add_kfunc_call() and during 1992 * call specialization. 1993 */ 1994 struct bpf_kfunc_desc descs[]; 1995 }; 1996 1997 /* Functions exported from verifier.c, used by fixups.c */ 1998 void bpf_mark_subprog_exc_cb(struct bpf_verifier_env *env, int subprog); 1999 bool bpf_allow_tail_call_in_subprogs(struct bpf_verifier_env *env); 2000 bool bpf_verifier_inlines_helper_call(struct bpf_verifier_env *env, s32 imm); 2001 int bpf_add_kfunc_call(struct bpf_verifier_env *env, u32 func_id, u16 offset); 2002 int bpf_fixup_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, 2003 struct bpf_insn *insn_buf, int insn_idx, int *cnt); 2004 2005 /* Functions exported from verifier.c, used by trampoline.c */ 2006 int bpf_check_attach_btf_id_multi(struct btf *btf, struct bpf_prog *prog, u32 btf_id, 2007 struct bpf_attach_target_info *tgt_info); 2008 2009 /* Functions in fixups.c, called from bpf_check() */ 2010 void bpf_clear_insn_aux_data(struct bpf_verifier_env *env, int start, int len); 2011 int bpf_remove_fastcall_spills_fills(struct bpf_verifier_env *env); 2012 int bpf_optimize_bpf_loop(struct bpf_verifier_env *env); 2013 void bpf_opt_hard_wire_dead_code_branches(struct bpf_verifier_env *env); 2014 int bpf_opt_remove_dead_code(struct bpf_verifier_env *env); 2015 int bpf_opt_remove_nops(struct bpf_verifier_env *env); 2016 int bpf_opt_subreg_zext_lo32_rnd_hi32(struct bpf_verifier_env *env, const union bpf_attr *attr); 2017 int bpf_convert_ctx_accesses(struct bpf_verifier_env *env); 2018 int bpf_jit_subprogs(struct bpf_verifier_env *env); 2019 int bpf_fixup_call_args(struct bpf_verifier_env *env); 2020 int bpf_do_misc_fixups(struct bpf_verifier_env *env); 2021 int bpf_insn_def32(struct bpf_prog *prog, struct bpf_insn *insn); 2022 2023 int bpf_flip_opcode(u32 opcode); 2024 u8 bpf_rev_opcode(u8 opcode); 2025 2026 int bpf_compute_idoms(struct bpf_verifier_env *env); 2027 int bpf_compute_loops(struct bpf_verifier_env *env); 2028 int bpf_loop_at_index(struct bpf_verifier_env *env, u32 idx); 2029 bool bpf_is_nested_loop(struct bpf_verifier_env *env, int inner_header, int outer_header); 2030 2031 int bpf_init_scev(struct bpf_verifier_env *env); 2032 void bpf_free_scev(struct bpf_verifier_env *env); 2033 int bpf_compute_scev(struct bpf_verifier_env *env); 2034 2035 int bpf_compute_loop_iters(struct bpf_verifier_env *env, struct bpf_verifier_state *st, 2036 struct bpf_loop_iters *iters); 2037 int bpf_widen_scev_regs(struct bpf_verifier_env *env, struct bpf_verifier_state *st, 2038 struct bpf_verifier_state *loop_entry, struct bpf_loop_iters *iters); 2039 int bpf_clamp_scev_regs(struct bpf_verifier_env *env, struct bpf_func_state *st, u32 insn_idx, 2040 struct bpf_verifier_state *entry_state, u32 max_header_count); 2041 2042 #endif /* _LINUX_BPF_VERIFIER_H */ 2043