1 // SPDX-License-Identifier: GPL-2.0-only 2 // Copyright (c) 2026 Meta Platforms, Inc. and affiliates. 3 4 #include <linux/bpf.h> 5 #include <linux/bpf_verifier.h> 6 #include <linux/btf.h> 7 #include <linux/ctype.h> 8 #include <linux/kernel.h> 9 #include <linux/list.h> 10 #include <linux/seq_buf.h> 11 #include <linux/overflow.h> 12 #include <linux/slab.h> 13 #include <linux/stdarg.h> 14 #include <linux/string.h> 15 16 #include "disasm.h" 17 #include "diagnostics.h" 18 19 #define REGISTER_TYPE_SAFETY "Register Type Safety" 20 #define MEMORY_SAFETY "Memory Safety" 21 #define RESOURCE_LIFETIME_SAFETY "Resource Lifetime Safety" 22 #define CALL_TYPE_SAFETY "Call Type Safety" 23 #define EXECUTION_CONTEXT_SAFETY "Execution Context Safety" 24 #define PROGRAM_STRUCTURE "Program Structure" 25 #define POLICY "Policy" 26 27 #define BPF_DIAG_TEXT_WIDTH 100 28 #define BPF_DIAG_TEXT_INDENT " " 29 #define BPF_DIAG_CONTEXT 2 30 #define BPF_DIAG_CONTEXT_CNT (1 + BPF_DIAG_CONTEXT * 2) 31 #define BPF_DIAG_HISTORY_RENDER_MAX 64 32 #define BPF_DIAG_SOURCE_LANE_WIDTH 88 33 #define BPF_DIAG_TAB_WIDTH 8 34 #define BPF_DIAG_FMT_CHUNK_SIZE (PAGE_SIZE - sizeof(struct diag_fmt_chunk)) 35 #define BPF_DIAG_FMT_BUF_SIZE 256 36 #define BPF_DIAG_EVENT_LOG_MAX_SIZE (64U << 20) 37 #define DISASM_LINE_LEN 160 38 39 enum bpf_diag_mod_target_kind { 40 BPF_DIAG_MOD_TARGET_NONE, 41 BPF_DIAG_MOD_TARGET_REG, 42 BPF_DIAG_MOD_TARGET_STACK_ARG, 43 BPF_DIAG_MOD_TARGET_STACK_SLOT, 44 BPF_DIAG_MOD_TARGET_STACK_RANGE, 45 }; 46 47 struct bpf_diag_mod_target { 48 u32 frame_id; 49 union { 50 struct { 51 s16 min_off; 52 s16 max_off; 53 } range; 54 u16 spi; 55 u8 regno; 56 u8 stack_arg; 57 }; 58 u8 frameno; 59 u8 kind; 60 }; 61 62 static struct bpf_diag_mod_target diag_reg_target(u32 frame_id, u8 frameno, u8 regno) 63 { 64 return (struct bpf_diag_mod_target){ 65 .frame_id = frame_id, 66 .frameno = frameno, 67 .kind = BPF_DIAG_MOD_TARGET_REG, 68 .regno = regno, 69 }; 70 } 71 72 static struct bpf_diag_mod_target diag_stack_arg_target(u32 frame_id, u8 frameno, u8 slot) 73 { 74 return (struct bpf_diag_mod_target){ 75 .frame_id = frame_id, 76 .frameno = frameno, 77 .kind = BPF_DIAG_MOD_TARGET_STACK_ARG, 78 .stack_arg = slot, 79 }; 80 } 81 82 static struct bpf_diag_mod_target diag_stack_slot_target(u32 frame_id, u8 frameno, u16 spi) 83 { 84 return (struct bpf_diag_mod_target){ 85 .frame_id = frame_id, 86 .frameno = frameno, 87 .kind = BPF_DIAG_MOD_TARGET_STACK_SLOT, 88 .spi = spi, 89 }; 90 } 91 92 static struct bpf_diag_mod_target diag_stack_range_target(u32 frame_id, u8 frameno, 93 s16 min_off, s16 max_off) 94 { 95 return (struct bpf_diag_mod_target){ 96 .frame_id = frame_id, 97 .frameno = frameno, 98 .kind = BPF_DIAG_MOD_TARGET_STACK_RANGE, 99 .range.min_off = min_off, 100 .range.max_off = max_off, 101 }; 102 } 103 104 struct bpf_diag_reg_snapshot { 105 u32 type; 106 u32 btf_id; 107 const struct bpf_map *map_ptr; 108 const struct btf *btf; 109 struct tnum var_off; 110 struct cnum64 r64; 111 }; 112 113 enum bpf_diag_history_kind { 114 BPF_DIAG_HISTORY_BRANCH, 115 BPF_DIAG_HISTORY_MOD, 116 BPF_DIAG_HISTORY_REF_ACQUIRE, 117 BPF_DIAG_HISTORY_REF_RELEASE, 118 BPF_DIAG_HISTORY_CONTEXT, 119 }; 120 121 struct bpf_diag_history_event { 122 u32 insn_idx : 24; 123 u32 kind : 8; 124 u8 in_lineage : 1; 125 union { 126 struct { 127 bool cond_true; 128 } branch; 129 struct { 130 struct bpf_diag_mod_target target; 131 struct bpf_diag_mod_target origin; 132 struct bpf_diag_reg_snapshot old, new; 133 u8 reason; 134 bool origin_valid; 135 } mod; 136 struct { 137 u32 ref_id; 138 } ref; 139 struct { 140 u32 depth; 141 u8 kind; 142 bool enter; 143 } ctx; 144 }; 145 }; 146 147 enum bpf_diag_history_scope { 148 BPF_DIAG_HISTORY_SCOPE_REG, 149 BPF_DIAG_HISTORY_SCOPE_STACK_ARG, 150 BPF_DIAG_HISTORY_SCOPE_REF, 151 BPF_DIAG_HISTORY_SCOPE_CONTEXT, 152 }; 153 154 struct bpf_diag_history_opts { 155 enum bpf_diag_history_scope scope; 156 u32 frame_id; 157 u32 frameno; 158 int regno; 159 int stack_arg_slot; 160 u32 ref_id; 161 enum bpf_diag_context_kind ctx_kind; 162 u32 ctx_depth; 163 }; 164 165 static void diag_print_history(struct bpf_verifier_env *env, 166 const struct bpf_diag_history_opts *opts); 167 static bool diag_target_matches(const struct bpf_diag_mod_target *event_target, 168 const struct bpf_diag_mod_target *target); 169 static const char *diag_context_name(enum bpf_diag_context_kind kind); 170 struct disasm_line { 171 char text[DISASM_LINE_LEN]; 172 int idx; 173 bool valid; 174 }; 175 176 struct disasm_ctx { 177 struct bpf_verifier_env *env; 178 struct seq_buf seq; 179 }; 180 181 struct diag_fmt_chunk { 182 struct list_head node; 183 struct seq_buf seq; 184 char data[]; 185 }; 186 187 struct diag_fmt_mark { 188 struct diag_fmt_chunk *chunk; 189 size_t len; 190 }; 191 192 struct bpf_diag_log { 193 struct bpf_diag_history_event *events; 194 /* Sequence number of the oldest retained event on the active path. */ 195 u64 first_seq; 196 u32 cnt; 197 u32 cap; 198 u32 head; 199 bool growth_failed; 200 }; 201 202 struct bpf_diag_scratch { 203 struct bpf_linfo_source source_lines[BPF_DIAG_CONTEXT_CNT]; 204 struct disasm_line disasm_lines[BPF_DIAG_CONTEXT_CNT]; 205 }; 206 207 struct bpf_diag_mod_scope { 208 struct bpf_reg_state target_reg_snapshot; 209 struct bpf_diag_mod_target target; 210 struct bpf_diag_mod_target origin; 211 enum bpf_diag_mod_reason reason; 212 u32 insn_idx; 213 bool active; 214 bool origin_valid; 215 }; 216 217 struct bpf_diag { 218 struct bpf_diag_log log; 219 struct bpf_diag_scratch scratch; 220 struct list_head fmt_chunks; 221 struct bpf_diag_mod_scope mod; 222 u32 frame_id_gen; 223 }; 224 225 bool bpf_diag_enabled(const struct bpf_verifier_env *env) 226 { 227 return env->log.level & BPF_LOG_LEVEL; 228 } 229 230 static void diag_write(struct bpf_verifier_env *env, const char *fmt, ...) __printf(2, 3); 231 232 int bpf_diag_init(struct bpf_verifier_env *env) 233 { 234 if (!bpf_diag_enabled(env)) 235 return 0; 236 237 env->diag = kzalloc_obj(struct bpf_diag, GFP_KERNEL_ACCOUNT); 238 if (!env->diag) 239 return -ENOMEM; 240 241 INIT_LIST_HEAD(&env->diag->fmt_chunks); 242 return 0; 243 } 244 245 void bpf_diag_init_frame(struct bpf_verifier_env *env, struct bpf_func_state *state) 246 { 247 if (env->diag) 248 state->diag_frame_id = ++env->diag->frame_id_gen; 249 } 250 251 static char *diag_fmt_alloc(struct bpf_verifier_env *env, size_t size) 252 { 253 struct bpf_diag *diag = env->diag; 254 struct diag_fmt_chunk *chunk; 255 size_t capacity, available; 256 char *buf; 257 258 if (!diag || !size || size > INT_MAX) 259 return NULL; 260 261 if (!list_empty(&diag->fmt_chunks)) { 262 chunk = list_last_entry(&diag->fmt_chunks, struct diag_fmt_chunk, node); 263 available = seq_buf_get_buf(&chunk->seq, &buf); 264 if (available >= size) 265 goto commit; 266 } 267 268 capacity = max_t(size_t, BPF_DIAG_FMT_CHUNK_SIZE, size); 269 chunk = kmalloc_flex(*chunk, data, capacity, GFP_KERNEL_ACCOUNT); 270 if (!chunk) 271 return NULL; 272 273 seq_buf_init(&chunk->seq, chunk->data, capacity); 274 list_add_tail(&chunk->node, &diag->fmt_chunks); 275 available = seq_buf_get_buf(&chunk->seq, &buf); 276 if (WARN_ON_ONCE(available < size)) 277 return NULL; 278 279 commit: 280 seq_buf_commit(&chunk->seq, size); 281 return buf; 282 } 283 284 char *bpf_diag_fmt_buf(struct bpf_verifier_env *env, size_t size) 285 { 286 char *buf; 287 288 buf = diag_fmt_alloc(env, size); 289 if (buf) 290 buf[0] = '\0'; 291 return buf; 292 } 293 294 const char *bpf_diag_vfmt(struct bpf_verifier_env *env, const char *fmt, va_list args) 295 { 296 va_list copy; 297 char *buf; 298 int len; 299 300 va_copy(copy, args); 301 len = vsnprintf(NULL, 0, fmt, copy); 302 va_end(copy); 303 if (len < 0 || len == INT_MAX) 304 return ""; 305 306 buf = diag_fmt_alloc(env, len + 1); 307 if (buf) 308 vsnprintf(buf, len + 1, fmt, args); 309 return buf ?: ""; 310 } 311 312 const char *bpf_diag_fmt(struct bpf_verifier_env *env, const char *fmt, ...) 313 { 314 const char *buf; 315 va_list args; 316 317 va_start(args, fmt); 318 buf = bpf_diag_vfmt(env, fmt, args); 319 va_end(args); 320 return buf; 321 } 322 323 static struct diag_fmt_mark diag_fmt_save(struct bpf_verifier_env *env) 324 { 325 struct bpf_diag *diag = env->diag; 326 struct diag_fmt_mark mark = {}; 327 328 if (!diag || list_empty(&diag->fmt_chunks)) 329 return mark; 330 331 mark.chunk = list_last_entry(&diag->fmt_chunks, struct diag_fmt_chunk, node); 332 mark.len = mark.chunk->seq.len; 333 return mark; 334 } 335 336 static void diag_fmt_restore(struct bpf_verifier_env *env, struct diag_fmt_mark mark) 337 { 338 struct bpf_diag *diag = env->diag; 339 struct diag_fmt_chunk *chunk; 340 341 if (!diag) 342 return; 343 344 while (!list_empty(&diag->fmt_chunks)) { 345 chunk = list_last_entry(&diag->fmt_chunks, struct diag_fmt_chunk, node); 346 if (chunk == mark.chunk) 347 break; 348 list_del(&chunk->node); 349 kfree(chunk); 350 } 351 352 if (mark.chunk) { 353 mark.chunk->seq.len = mark.len; 354 seq_buf_terminate(&mark.chunk->seq); 355 } 356 } 357 358 void bpf_diag_free(struct bpf_verifier_env *env) 359 { 360 struct bpf_diag *diag = env->diag; 361 362 if (!diag) 363 return; 364 365 diag_fmt_restore(env, (struct diag_fmt_mark){}); 366 kvfree(diag->log.events); 367 kfree(diag); 368 env->diag = NULL; 369 } 370 371 static void diag_write(struct bpf_verifier_env *env, const char *fmt, ...) 372 { 373 va_list args; 374 375 if (!bpf_diag_enabled(env)) 376 return; 377 378 va_start(args, fmt); 379 bpf_verifier_vlog(&env->log, fmt, args); 380 va_end(args); 381 } 382 383 static u64 log_end(const struct bpf_diag_log *log) 384 { 385 return log->first_seq + log->cnt; 386 } 387 388 static u32 log_pos(const struct bpf_diag_log *log, u32 idx) 389 { 390 u32 pos = log->head + idx; 391 392 return pos < log->cap ? pos : pos - log->cap; 393 } 394 395 u64 bpf_diag_event_log_save(struct bpf_verifier_env *env) 396 { 397 struct bpf_diag *diag = env->diag; 398 399 return diag ? log_end(&diag->log) : 0; 400 } 401 402 void bpf_diag_event_log_restore(struct bpf_verifier_env *env, u64 log_pos) 403 { 404 struct bpf_diag *diag = env->diag; 405 struct bpf_diag_log *log; 406 u64 end_seq; 407 408 if (!diag) 409 return; 410 411 log = &diag->log; 412 end_seq = log_end(log); 413 if (WARN_ON_ONCE(log_pos > end_seq)) 414 log_pos = end_seq; 415 416 /* 417 * A deep abandoned path may have rotated away the shared prefix. In 418 * that case, restart with an empty retained suffix and remember that 419 * every event before the restored mark is unavailable. 420 */ 421 if (log_pos <= log->first_seq) { 422 log->first_seq = log_pos; 423 log->head = 0; 424 log->cnt = 0; 425 return; 426 } 427 428 log->cnt = log_pos - log->first_seq; 429 } 430 431 u32 bpf_diag_irq_depth(const struct bpf_verifier_state *state) 432 { 433 u32 depth = 0; 434 int i; 435 436 for (i = 0; i < state->acquired_refs; i++) { 437 if (state->refs[i].type == REF_TYPE_IRQ) 438 depth++; 439 } 440 441 return depth; 442 } 443 444 static void diag_append_history(struct bpf_verifier_env *env, 445 const struct bpf_diag_history_event *event) 446 { 447 struct bpf_diag_history_event *events; 448 struct bpf_diag *diag = env->diag; 449 struct bpf_diag_log *log; 450 u32 cap, max_events; 451 452 if (!diag) 453 return; 454 log = &diag->log; 455 456 if (log->cnt < log->cap) { 457 log->events[log_pos(log, log->cnt++)] = *event; 458 return; 459 } 460 461 max_events = BPF_DIAG_EVENT_LOG_MAX_SIZE / sizeof(*events); 462 if (log->growth_failed || log->cap == max_events) 463 goto rotate; 464 465 cap = min(log->cap ? log->cap * 2 : 64, max_events); 466 events = kvrealloc(log->events, array_size(cap, sizeof(*events)), GFP_KERNEL_ACCOUNT); 467 if (!events) { 468 log->growth_failed = true; 469 goto rotate; 470 } 471 log->events = events; 472 log->cap = cap; 473 log->events[log->cnt++] = *event; 474 return; 475 476 rotate: 477 if (log->cap) { 478 log->events[log->head++] = *event; 479 if (log->head == log->cap) 480 log->head = 0; 481 } 482 log->first_seq++; 483 } 484 485 static void diag_print_wrapped_prefixed(struct bpf_verifier_env *env, const char *first_prefix, 486 const char *next_prefix, const char *text) 487 { 488 const char *prefix = first_prefix; 489 490 while (*text) { 491 const char *line = text; 492 int prefix_len = strlen(prefix); 493 int text_width = BPF_DIAG_TEXT_WIDTH - prefix_len; 494 int len = 0, last_space = -1; 495 496 if (text_width < 1) 497 text_width = 1; 498 499 while (line[len] && line[len] != '\n' && len < text_width) { 500 if (line[len] == ' ') 501 last_space = len; 502 len++; 503 } 504 505 if (line[len] && line[len] != '\n' && line[len] != ' ' && last_space > 0) 506 len = last_space; 507 508 diag_write(env, "%s%.*s\n", prefix, len, line); 509 510 text = line + len; 511 while (*text == ' ') 512 text++; 513 if (*text == '\n') 514 text++; 515 516 prefix = next_prefix; 517 } 518 } 519 520 const char *bpf_diag_fmt_btf_type(struct bpf_verifier_env *env, const struct btf *btf, u32 type_id) 521 { 522 char *buf = bpf_diag_fmt_buf(env, BPF_DIAG_FMT_BUF_SIZE); 523 size_t len; 524 int ret; 525 526 if (!buf) 527 return ""; 528 529 buf[0] = '\0'; 530 ret = btf_type_name_to_buf(btf, type_id, buf, BPF_DIAG_FMT_BUF_SIZE); 531 if (ret < 0 || !buf[0]) { 532 scnprintf(buf, BPF_DIAG_FMT_BUF_SIZE, "BTF type ID %u", type_id); 533 return buf; 534 } 535 536 len = strlen(buf); 537 if (len && buf[len - 1] == '{') 538 buf[len - 1] = '\0'; 539 return buf; 540 } 541 542 static void diag_vprint_indented(struct bpf_verifier_env *env, const char *fmt, va_list args) 543 __printf(2, 0); 544 545 static void diag_vprint_indented(struct bpf_verifier_env *env, const char *fmt, va_list args) 546 { 547 char *buf; 548 549 if (!bpf_diag_enabled(env)) 550 return; 551 552 buf = kvasprintf(GFP_KERNEL_ACCOUNT, fmt, args); 553 if (!buf) { 554 diag_write(env, "%s<failed to allocate diagnostic text>\n", BPF_DIAG_TEXT_INDENT); 555 return; 556 } 557 558 diag_print_wrapped_prefixed(env, BPF_DIAG_TEXT_INDENT, BPF_DIAG_TEXT_INDENT, buf); 559 kfree(buf); 560 } 561 562 static int diag_line_width(unsigned int line) 563 { 564 int width = 1; 565 566 while (line >= 10) { 567 line /= 10; 568 width++; 569 } 570 571 return width; 572 } 573 574 static int diag_line_indent(const char *line) 575 { 576 int indent = 0; 577 578 while (*line == ' ' || *line == '\t') { 579 if (*line == '\t') 580 indent = round_up(indent + 1, BPF_DIAG_TAB_WIDTH); 581 else 582 indent++; 583 line++; 584 } 585 586 return indent; 587 } 588 589 static void disasm_print(void *private_data, const char *fmt, ...) __printf(2, 3); 590 591 static void disasm_print(void *private_data, const char *fmt, ...) 592 { 593 struct disasm_ctx *ctx = private_data; 594 va_list args; 595 596 va_start(args, fmt); 597 seq_buf_vprintf(&ctx->seq, fmt, args); 598 va_end(args); 599 } 600 601 static const char *disasm_kfunc_name(void *private_data, const struct bpf_insn *insn) 602 { 603 struct disasm_ctx *ctx = private_data; 604 605 return bpf_disasm_kfunc_name(ctx->env, insn); 606 } 607 608 static void format_disasm_line(struct bpf_verifier_env *env, int insn_idx, 609 struct disasm_line *line) 610 { 611 struct disasm_ctx ctx = { .env = env }; 612 struct bpf_insn *insn; 613 const struct bpf_insn_cbs cbs = { 614 .cb_call = disasm_kfunc_name, 615 .cb_print = disasm_print, 616 .private_data = &ctx, 617 }; 618 619 line->idx = insn_idx; 620 line->valid = false; 621 seq_buf_init(&ctx.seq, line->text, sizeof(line->text)); 622 623 if (insn_idx < 0 || insn_idx >= env->prog->len) 624 return; 625 626 if (insn_idx > 0 && bpf_is_ldimm64(&env->prog->insnsi[insn_idx - 1])) 627 return; 628 629 insn = &env->prog->insnsi[insn_idx]; 630 if (bpf_is_ldimm64(insn) && insn_idx + 1 >= env->prog->len) 631 return; 632 633 print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); 634 seq_buf_terminate(&ctx.seq); 635 636 line->valid = true; 637 } 638 639 static void diag_format_source_text(char *buf, size_t size, const char *line, int width) 640 { 641 int col = 0, len = 0; 642 643 if (!size) 644 return; 645 if (width <= 0) { 646 buf[0] = '\0'; 647 return; 648 } 649 650 line = line ?: "..."; 651 while (*line && col < width && len + 1 < size) { 652 if (*line == '\t') { 653 int next = round_up(col + 1, BPF_DIAG_TAB_WIDTH); 654 655 while (col < next && col < width && len + 1 < size) { 656 buf[len++] = ' '; 657 col++; 658 } 659 line++; 660 continue; 661 } 662 663 buf[len++] = *line++; 664 col++; 665 } 666 667 if (*line) { 668 int ellipsis_len = min(3, width); 669 670 while (len > 0 && col > width - ellipsis_len) { 671 len--; 672 col--; 673 } 674 while (ellipsis_len-- && len + 1 < size) 675 buf[len++] = '.'; 676 } 677 678 buf[len] = '\0'; 679 } 680 681 static void diag_format_source_lane(char *buf, size_t size, const char *source_prefix, 682 int source_line_width, int line_num, const char *line) 683 { 684 int len, text_width; 685 686 if (line_num <= 0) { 687 buf[0] = '\0'; 688 return; 689 } 690 691 len = scnprintf(buf, size, "%s%*d | ", source_prefix, source_line_width, line_num); 692 text_width = BPF_DIAG_SOURCE_LANE_WIDTH - len; 693 diag_format_source_text(buf + len, size - len, line, text_width); 694 } 695 696 static void bpf_diag_header(struct bpf_verifier_env *env, const char *category, 697 const char *problem) 698 { 699 char first; 700 701 if (!bpf_diag_enabled(env)) 702 return; 703 704 category = category ?: "Verifier Error"; 705 problem = problem ?: ""; 706 707 if (!problem[0]) { 708 diag_write(env, "\nVerification failed: %s\n", category); 709 return; 710 } 711 712 first = toupper(problem[0]); 713 diag_write(env, "\nVerification failed: %s: %c%s\n", category, first, problem + 1); 714 } 715 716 static void diag_reason(struct bpf_verifier_env *env, const char *fmt, ...) __printf(2, 3); 717 static void diag_suggestion(struct bpf_verifier_env *env, const char *fmt, ...) 718 __printf(2, 3); 719 720 static void diag_section(struct bpf_verifier_env *env, const char *title) 721 { 722 if (!bpf_diag_enabled(env)) 723 return; 724 725 diag_write(env, "\n%s:\n", title); 726 } 727 728 static void diag_reason(struct bpf_verifier_env *env, const char *fmt, ...) 729 { 730 va_list args; 731 732 if (!bpf_diag_enabled(env)) 733 return; 734 735 diag_section(env, "Reason"); 736 737 va_start(args, fmt); 738 diag_vprint_indented(env, fmt, args); 739 va_end(args); 740 } 741 742 static void diag_suggestion(struct bpf_verifier_env *env, const char *fmt, ...) 743 { 744 va_list args; 745 746 if (!bpf_diag_enabled(env)) 747 return; 748 749 diag_section(env, "Suggestion"); 750 751 va_start(args, fmt); 752 diag_vprint_indented(env, fmt, args); 753 va_end(args); 754 diag_write(env, "\n"); 755 } 756 757 static void diag_print_source_annotation(struct bpf_verifier_env *env, int line_width, int indent, 758 const char *label, const char *msg) 759 { 760 const char *first_prefix, *next_prefix, *text; 761 762 indent = min_t(int, indent, max_t(int, 0, BPF_DIAG_SOURCE_LANE_WIDTH - line_width - 8)); 763 text = bpf_diag_fmt(env, "%s: %s", label, msg); 764 first_prefix = bpf_diag_fmt(env, " %*s | %*s^-- ", line_width + 4, "", indent, ""); 765 next_prefix = bpf_diag_fmt(env, " %*s | %*s ", line_width + 4, "", indent, ""); 766 767 diag_print_wrapped_prefixed(env, first_prefix, next_prefix, text); 768 } 769 770 static void diag_print_insn_context(struct bpf_verifier_env *env, u32 insn_idx, 771 struct disasm_line *disasm_lines) 772 { 773 int insn_width = diag_line_width(env->prog->len ? env->prog->len - 1 : 0); 774 int i; 775 776 for (i = 0; i < BPF_DIAG_CONTEXT_CNT; i++) { 777 int row = i - BPF_DIAG_CONTEXT; 778 779 format_disasm_line(env, insn_idx + row, &disasm_lines[i]); 780 } 781 782 diag_write(env, " Instruction context:\n"); 783 for (i = 0; i < BPF_DIAG_CONTEXT_CNT; i++) { 784 struct disasm_line *line = &disasm_lines[i]; 785 786 if (line->valid) 787 diag_write(env, " %s%*d | %s\n", 788 line->idx == insn_idx ? ">>> " : " ", 789 insn_width, line->idx, line->text); 790 } 791 } 792 793 static __printf(4, 5) void bpf_diag_source(struct bpf_verifier_env *env, 794 u32 insn_idx, const char *label, 795 const char *fmt, ...) 796 { 797 struct bpf_diag_scratch *scratch; 798 struct bpf_linfo_source *source_lines; 799 struct disasm_line *disasm_lines; 800 struct bpf_linfo_source src = {}; 801 struct diag_fmt_mark mark; 802 const struct bpf_line_info *linfo; 803 const struct bpf_subprog_info *subprog; 804 struct btf *btf = env->prog->aux->btf; 805 char *source_lane; 806 const char *msg; 807 const char *func; 808 int start_line, end_line, width, indent, subprogno, linfo_start, linfo_end, i; 809 va_list args; 810 811 if (!bpf_diag_enabled(env)) 812 return; 813 if (!env->diag) 814 return; 815 816 mark = diag_fmt_save(env); 817 label = label ?: "note"; 818 scratch = &env->diag->scratch; 819 source_lines = scratch->source_lines; 820 disasm_lines = scratch->disasm_lines; 821 memset(source_lines, 0, sizeof(scratch->source_lines)); 822 memset(disasm_lines, 0, sizeof(scratch->disasm_lines)); 823 824 va_start(args, fmt); 825 msg = bpf_diag_vfmt(env, fmt, args); 826 va_end(args); 827 if (!*msg) 828 msg = "<failed to allocate diagnostic text>"; 829 830 linfo = bpf_find_linfo(env->prog, insn_idx); 831 if (btf && linfo) 832 bpf_get_linfo_source(btf, linfo, &src); 833 if (!src.file || !*src.file) { 834 diag_write(env, " insn %u\n", insn_idx); 835 goto out_annotation; 836 } 837 838 subprog = bpf_find_containing_subprog(env, insn_idx); 839 subprogno = subprog ? subprog - env->subprog_info : -ENOENT; 840 func = subprogno >= 0 ? bpf_subprog_name(env, subprogno) : NULL; 841 if (func && *func) 842 diag_write(env, " %s @ %s:%d:%d\n", func, src.file, src.line_num, src.line_col); 843 else 844 diag_write(env, " %s:%d:%d\n", src.file, src.line_num, src.line_col); 845 if (!src.line || !*src.line) 846 goto out_annotation; 847 848 start_line = src.line_num - BPF_DIAG_CONTEXT; 849 end_line = src.line_num + BPF_DIAG_CONTEXT; 850 width = diag_line_width(end_line); 851 indent = diag_line_indent(src.line); 852 for (i = 0; i < BPF_DIAG_CONTEXT_CNT; i++) 853 source_lines[i].line_num = start_line + i; 854 855 linfo = env->prog->aux->linfo; 856 linfo_start = subprog ? subprog->linfo_idx : 0; 857 linfo_end = subprogno >= 0 && subprogno + 1 < env->subprog_cnt ? 858 env->subprog_info[subprogno + 1].linfo_idx : env->prog->aux->nr_linfo; 859 for (i = linfo_start; i < linfo_end; i++) { 860 struct bpf_linfo_source line_src; 861 int idx; 862 863 bpf_get_linfo_source(btf, &linfo[i], &line_src); 864 if (line_src.file_name_off != src.file_name_off || 865 line_src.line_num < start_line || line_src.line_num > end_line || 866 !line_src.line || !*line_src.line) 867 continue; 868 869 idx = line_src.line_num - start_line; 870 if (!source_lines[idx].line) 871 source_lines[idx] = line_src; 872 } 873 874 diag_write(env, " Source context:\n"); 875 source_lane = bpf_diag_fmt_buf(env, BPF_DIAG_FMT_BUF_SIZE); 876 if (!source_lane) 877 goto out_restore; 878 for (i = 0; i < BPF_DIAG_CONTEXT_CNT; i++) { 879 const char *source_prefix; 880 881 source_prefix = source_lines[i].line_num == src.line_num ? ">>> " : " "; 882 diag_format_source_lane(source_lane, BPF_DIAG_FMT_BUF_SIZE, source_prefix, width, 883 source_lines[i].line_num, source_lines[i].line); 884 diag_write(env, " %s\n", source_lane); 885 if (source_lines[i].line_num == src.line_num) 886 diag_print_source_annotation(env, width, indent, label, msg); 887 } 888 diag_print_insn_context(env, insn_idx, disasm_lines); 889 goto out_restore; 890 891 out_annotation: 892 diag_print_source_annotation(env, 0, 0, label, msg); 893 diag_print_insn_context(env, insn_idx, disasm_lines); 894 out_restore: 895 diag_fmt_restore(env, mark); 896 } 897 898 static const struct bpf_func_state *diag_current_frame(const struct bpf_verifier_env *env) 899 { 900 return env->cur_state->frame[env->cur_state->curframe]; 901 } 902 903 void bpf_diag_register_type(struct bpf_verifier_env *env, u32 insn_idx, int regno, 904 const char *problem, const char *reason, const char *suggestion) 905 { 906 const struct bpf_func_state *frame = diag_current_frame(env); 907 struct bpf_diag_history_opts opts = { 908 .scope = BPF_DIAG_HISTORY_SCOPE_REG, 909 .frame_id = frame->diag_frame_id, 910 .frameno = frame->frameno, 911 .regno = regno, 912 }; 913 914 bpf_diag_header(env, REGISTER_TYPE_SAFETY, problem); 915 diag_reason(env, "%s", reason); 916 917 diag_section(env, "At"); 918 bpf_diag_source(env, insn_idx, "error", "%s", problem); 919 920 if (regno >= 0) 921 diag_print_history(env, &opts); 922 923 diag_suggestion(env, "%s", suggestion); 924 } 925 926 const char *bpf_diag_reg_type_plain(struct bpf_verifier_env *env, enum bpf_reg_type type) 927 { 928 switch (base_type(type)) { 929 case NOT_INIT: 930 return "an uninitialized value"; 931 case SCALAR_VALUE: 932 return "an integer scalar"; 933 case PTR_TO_CTX: 934 return "a context pointer"; 935 case PTR_TO_STACK: 936 return "a stack pointer"; 937 case PTR_TO_MAP_VALUE: 938 if (type_may_be_null(type)) 939 return "a nullable map value pointer"; 940 return "a map value pointer"; 941 case PTR_TO_MEM: 942 if (type_may_be_null(type)) 943 return "a nullable memory pointer"; 944 return "a memory pointer"; 945 case PTR_TO_BTF_ID: 946 if (type_may_be_null(type)) 947 return "a nullable kernel object pointer"; 948 if (type_is_non_owning_ref(type)) 949 return "a borrowed allocated object pointer"; 950 if (type_is_ptr_alloc_obj(type)) 951 return "an owned allocated object pointer"; 952 if (type_flag(type) & PTR_UNTRUSTED) 953 return "an untrusted kernel object pointer"; 954 return "a kernel object pointer"; 955 default: 956 return reg_type_str(env, type); 957 } 958 } 959 960 const char *bpf_diag_arg_type_plain(enum bpf_arg_type type) 961 { 962 switch (base_type(type)) { 963 case ARG_MEM_SIZE: 964 case ARG_CONST_MEM_SIZE: 965 return "an integer scalar length for this memory argument"; 966 case ARG_PTR_TO_CTX: 967 return "the original program context pointer or preserve it before modifying registers"; 968 case ARG_SCALAR: 969 case ARG_CONST_SCALAR: 970 case ARG_CONST_ALLOC_SIZE_OR_ZERO: 971 return "an integer scalar value for this argument, not a pointer or resource object"; 972 case ARG_PTR_TO_CONST_STR: 973 return "a constant string pointer that the verifier recognizes, such as a string stored in a read-only map value"; 974 case ARG_PTR_TO_DYNPTR: 975 return "the address of a stack dynptr object, or use a const dynptr pointer returned by the verifier-supported path"; 976 case ARG_PTR_TO_ALLOC_BTF_ID: 977 return "a pointer returned by the matching BPF object allocation path"; 978 case ARG_PTR_TO_REFCOUNTED_KPTR: 979 return "an owning or non-owning pointer to a BPF-managed object containing a bpf_refcount field"; 980 case ARG_PTR_TO_ITER: 981 return "the address of a stack iterator object for iterator new, next, and destroy calls"; 982 case ARG_PTR_TO_IRQ_FLAG: 983 return "the same stack slot used by bpf_local_irq_save() or bpf_res_spin_lock_irqsave()"; 984 case ARG_PTR_TO_CTX_OUT: 985 return "the attach hook's own output argument, loaded directly from the program context"; 986 default: 987 return "a value with one of the accepted pointer or scalar types for this call"; 988 } 989 } 990 991 static const char *diag_arg_ordinal(int argno) 992 { 993 switch (argno) { 994 case 1: 995 return "first"; 996 case 2: 997 return "second"; 998 case 3: 999 return "third"; 1000 case 4: 1001 return "fourth"; 1002 case 5: 1003 return "fifth"; 1004 case 6: 1005 return "sixth"; 1006 case 7: 1007 return "seventh"; 1008 case 8: 1009 return "eighth"; 1010 case 9: 1011 return "ninth"; 1012 case 10: 1013 return "tenth"; 1014 case 11: 1015 return "eleventh"; 1016 case 12: 1017 return "twelfth"; 1018 default: 1019 return NULL; 1020 } 1021 } 1022 1023 void bpf_diag_call_type(struct bpf_verifier_env *env, u32 insn_idx, int argno, int regno, 1024 int stack_arg_slot, const char *call_name, const char *arg_name, 1025 const char *reason, const char *suggestion) 1026 { 1027 const struct bpf_func_state *frame = diag_current_frame(env); 1028 struct bpf_diag_history_opts opts = { 1029 .frame_id = frame->diag_frame_id, 1030 .frameno = frame->frameno, 1031 }; 1032 const char *ordinal = diag_arg_ordinal(argno); 1033 const char *arg_desc; 1034 bool print_history = true; 1035 1036 if (regno >= 0) { 1037 opts.scope = BPF_DIAG_HISTORY_SCOPE_REG; 1038 opts.regno = regno; 1039 } else if (stack_arg_slot >= 0) { 1040 opts.scope = BPF_DIAG_HISTORY_SCOPE_STACK_ARG; 1041 opts.stack_arg_slot = stack_arg_slot; 1042 } else { 1043 print_history = false; 1044 } 1045 1046 if (ordinal && arg_name) 1047 arg_desc = bpf_diag_fmt(env, "%s argument (%s)", ordinal, arg_name); 1048 else if (ordinal) 1049 arg_desc = bpf_diag_fmt(env, "%s argument", ordinal); 1050 else if (arg_name) 1051 arg_desc = bpf_diag_fmt(env, "argument %s", arg_name); 1052 else 1053 arg_desc = "argument"; 1054 1055 bpf_diag_header(env, CALL_TYPE_SAFETY, "invalid call argument"); 1056 diag_reason(env, "The %s to %s does not satisfy the verifier contract: %s.", 1057 arg_desc, call_name, reason); 1058 1059 diag_section(env, "At"); 1060 bpf_diag_source(env, insn_idx, "error", "invalid %s for %s", arg_desc, call_name); 1061 1062 if (print_history) 1063 diag_print_history(env, &opts); 1064 1065 diag_suggestion(env, "%s", suggestion); 1066 } 1067 1068 static const char *diag_context_constraint(enum bpf_diag_context_kind kind) 1069 { 1070 switch (kind) { 1071 case BPF_DIAG_CONTEXT_RCU: 1072 return "RCU read-side critical sections cannot call operations that may sleep"; 1073 case BPF_DIAG_CONTEXT_PREEMPT: 1074 return "preemption-disabled code cannot call operations that may sleep"; 1075 case BPF_DIAG_CONTEXT_IRQ: 1076 return "IRQ-disabled code cannot call operations that may sleep"; 1077 case BPF_DIAG_CONTEXT_LOCK: 1078 return "code holding a BPF spin lock cannot call operations that may sleep"; 1079 case BPF_DIAG_CONTEXT_NONE: 1080 default: 1081 return NULL; 1082 } 1083 } 1084 1085 static const char *diag_active_context(struct bpf_verifier_env *env, u32 depth, 1086 const char *context) 1087 { 1088 if (depth == 1) 1089 return bpf_diag_fmt(env, "an active %s (depth 1)", context); 1090 return bpf_diag_fmt(env, "%u active %ss (depth %u)", depth, context, depth); 1091 } 1092 1093 static u32 diag_context_depth(struct bpf_verifier_env *env, enum bpf_diag_context_kind kind) 1094 { 1095 switch (kind) { 1096 case BPF_DIAG_CONTEXT_RCU: 1097 return env->cur_state->active_rcu_locks; 1098 case BPF_DIAG_CONTEXT_PREEMPT: 1099 return env->cur_state->active_preempt_locks; 1100 case BPF_DIAG_CONTEXT_IRQ: 1101 return bpf_diag_irq_depth(env->cur_state); 1102 case BPF_DIAG_CONTEXT_LOCK: 1103 return env->cur_state->active_locks; 1104 case BPF_DIAG_CONTEXT_NONE: 1105 default: 1106 return 0; 1107 } 1108 } 1109 1110 void bpf_diag_ctx_forbidden(struct bpf_verifier_env *env, u32 insn_idx, 1111 const char *operation, const char *suggestion) 1112 { 1113 struct bpf_diag_history_opts opts; 1114 enum bpf_diag_context_kind ctx_kind; 1115 const char *constraint, *context; 1116 u32 depth; 1117 1118 ctx_kind = BPF_DIAG_CONTEXT_NONE; 1119 if (env->cur_state->in_sleepable) { 1120 if (env->cur_state->active_rcu_locks) 1121 ctx_kind = BPF_DIAG_CONTEXT_RCU; 1122 else if (env->cur_state->active_preempt_locks) 1123 ctx_kind = BPF_DIAG_CONTEXT_PREEMPT; 1124 else if (env->cur_state->active_irq_id) 1125 ctx_kind = BPF_DIAG_CONTEXT_IRQ; 1126 else if (env->cur_state->active_locks) 1127 ctx_kind = BPF_DIAG_CONTEXT_LOCK; 1128 } 1129 1130 depth = diag_context_depth(env, ctx_kind); 1131 opts = (struct bpf_diag_history_opts) { 1132 .scope = BPF_DIAG_HISTORY_SCOPE_CONTEXT, 1133 .ctx_kind = ctx_kind, 1134 .ctx_depth = depth, 1135 }; 1136 constraint = diag_context_constraint(ctx_kind); 1137 context = diag_context_name(ctx_kind); 1138 1139 bpf_diag_header(env, EXECUTION_CONTEXT_SAFETY, 1140 "operation is not allowed in this context"); 1141 if (constraint) { 1142 if (depth) { 1143 diag_reason( 1144 env, "The operation %s cannot be used in %s because %s. This path is still inside %s.", 1145 operation, context, constraint, diag_active_context(env, depth, context)); 1146 } else { 1147 diag_reason(env, "The operation %s cannot be used in %s because %s.", 1148 operation, context, constraint); 1149 } 1150 } else { 1151 diag_reason(env, "The operation %s cannot be used in %s.", operation, 1152 context); 1153 } 1154 1155 diag_section(env, "At"); 1156 bpf_diag_source(env, insn_idx, "error", "%s is not allowed in %s", operation, 1157 context); 1158 1159 if (ctx_kind != BPF_DIAG_CONTEXT_NONE) 1160 diag_print_history(env, &opts); 1161 1162 diag_suggestion(env, "%s", suggestion); 1163 } 1164 1165 void bpf_diag_ctx_active(struct bpf_verifier_env *env, u32 insn_idx, const char *operation, 1166 enum bpf_diag_context_kind ctx_kind, const char *suggestion) 1167 { 1168 u32 depth = diag_context_depth(env, ctx_kind); 1169 struct bpf_diag_history_opts opts = { 1170 .scope = BPF_DIAG_HISTORY_SCOPE_CONTEXT, 1171 .ctx_kind = ctx_kind, 1172 .ctx_depth = depth, 1173 }; 1174 const char *context = diag_context_name(ctx_kind); 1175 1176 bpf_diag_header(env, EXECUTION_CONTEXT_SAFETY, 1177 "operation is not allowed in this context"); 1178 diag_reason( 1179 env, "The operation %s cannot be used while this path is still inside %s. Leave the region before this operation.", 1180 operation, diag_active_context(env, depth, context)); 1181 1182 diag_section(env, "At"); 1183 bpf_diag_source(env, insn_idx, "error", "%s is not allowed before leaving %s", 1184 operation, context); 1185 1186 diag_print_history(env, &opts); 1187 1188 diag_suggestion(env, "%s", suggestion); 1189 } 1190 1191 void bpf_diag_ctx_required(struct bpf_verifier_env *env, u32 insn_idx, const char *operation, 1192 enum bpf_diag_context_kind ctx_kind, const char *suggestion) 1193 { 1194 const char *context = diag_context_name(ctx_kind); 1195 1196 bpf_diag_header(env, EXECUTION_CONTEXT_SAFETY, "required context is not active"); 1197 diag_reason(env, "The operation %s requires an active %s, but this path is outside one.", 1198 operation, context); 1199 1200 diag_section(env, "At"); 1201 bpf_diag_source(env, insn_idx, "error", "%s requires %s", operation, context); 1202 1203 diag_suggestion(env, "%s", suggestion); 1204 } 1205 1206 void bpf_diag_ctx_underflow(struct bpf_verifier_env *env, u32 insn_idx, 1207 const char *operation, enum bpf_diag_context_kind ctx_kind, 1208 const char *suggestion) 1209 { 1210 struct bpf_diag_history_opts opts = { 1211 .scope = BPF_DIAG_HISTORY_SCOPE_CONTEXT, 1212 .ctx_kind = ctx_kind, 1213 }; 1214 const char *context = diag_context_name(ctx_kind); 1215 1216 bpf_diag_header(env, EXECUTION_CONTEXT_SAFETY, "unmatched context exit"); 1217 diag_reason( 1218 env, "The operation %s tries to leave %s, but this path has no active %s to leave. The current depth is 0.", 1219 operation, context, context); 1220 1221 diag_section(env, "At"); 1222 bpf_diag_source(env, insn_idx, "error", "%s has no matching enter on this path", 1223 operation); 1224 1225 diag_print_history(env, &opts); 1226 1227 diag_suggestion(env, "%s", suggestion); 1228 } 1229 1230 void bpf_diag_program_structure(struct bpf_verifier_env *env, u32 insn_idx, 1231 const char *problem, const char *suggestion, 1232 const char *reason_fmt, ...) 1233 { 1234 va_list args; 1235 1236 bpf_diag_header(env, PROGRAM_STRUCTURE, problem); 1237 diag_section(env, "Reason"); 1238 1239 va_start(args, reason_fmt); 1240 diag_vprint_indented(env, reason_fmt, args); 1241 va_end(args); 1242 1243 diag_section(env, "At"); 1244 bpf_diag_source(env, insn_idx, "error", "%s", problem); 1245 1246 diag_suggestion(env, "%s", suggestion); 1247 } 1248 1249 void bpf_diag_policy(struct bpf_verifier_env *env, u32 insn_idx, const char *operation, 1250 const char *reason, const char *suggestion) 1251 { 1252 bpf_diag_header(env, POLICY, "operation is not allowed"); 1253 diag_reason(env, "The %s is not allowed: %s.", operation, reason); 1254 1255 diag_section(env, "At"); 1256 bpf_diag_source(env, insn_idx, "error", "policy check failed for %s", operation); 1257 1258 diag_suggestion(env, "%s", suggestion); 1259 } 1260 1261 void bpf_diag_invalid_deref(struct bpf_verifier_env *env, u32 insn_idx, int regno, 1262 const char *reg_name, const struct bpf_reg_state *reg, 1263 enum bpf_diag_invalid_deref_kind kind, s64 offset) 1264 { 1265 const struct bpf_func_state *frame = diag_current_frame(env); 1266 struct bpf_diag_history_opts opts = { 1267 .scope = BPF_DIAG_HISTORY_SCOPE_REG, 1268 .frame_id = frame->diag_frame_id, 1269 .frameno = frame->frameno, 1270 .regno = regno, 1271 }; 1272 const char *type_name = bpf_diag_reg_type_plain(env, reg->type); 1273 1274 bpf_diag_header(env, REGISTER_TYPE_SAFETY, "invalid dereference"); 1275 1276 switch (kind) { 1277 case BPF_DIAG_DEREF_SCALAR: 1278 diag_reason(env, "%s is an integer scalar here, not a pointer to memory.", 1279 reg_name); 1280 break; 1281 case BPF_DIAG_DEREF_NULLABLE_PTR: 1282 diag_reason( 1283 env, "%s may be NULL here (%s). The program could dereference NULL on this path, so the verifier cannot prove this access is safe.", 1284 reg_name, type_name); 1285 break; 1286 case BPF_DIAG_DEREF_MODIFIED_PTR: 1287 diag_reason( 1288 env, "%s has offset %lld here, but this pointer type must be dereferenced in its original form.", 1289 reg_name, offset); 1290 break; 1291 case BPF_DIAG_DEREF_INVALID_PTR: 1292 default: 1293 diag_reason( 1294 env, "%s has type %s here, which is not valid for this memory access.", 1295 reg_name, type_name); 1296 break; 1297 } 1298 1299 diag_section(env, "At"); 1300 if (kind == BPF_DIAG_DEREF_MODIFIED_PTR) 1301 bpf_diag_source(env, insn_idx, "error", 1302 "dereference requires the original %s pointer", type_name); 1303 else 1304 bpf_diag_source(env, insn_idx, "error", "invalid dereference of %s (%s)", 1305 reg_name, type_name); 1306 1307 if (regno >= 0) 1308 diag_print_history(env, &opts); 1309 1310 switch (kind) { 1311 case BPF_DIAG_DEREF_NULLABLE_PTR: 1312 diag_suggestion( 1313 env, "Add a NULL check before the access and dereference the pointer only on the non-NULL path."); 1314 break; 1315 case BPF_DIAG_DEREF_MODIFIED_PTR: 1316 diag_suggestion( 1317 env, "Preserve the original pointer in another register, or use only offsets this pointer type permits before dereferencing it."); 1318 break; 1319 case BPF_DIAG_DEREF_SCALAR: 1320 case BPF_DIAG_DEREF_INVALID_PTR: 1321 default: 1322 diag_suggestion( 1323 env, "Preserve a pointer-valued register where needed, or reload and revalidate the pointer after scalar arithmetic, helper calls, or other operations that can invalidate it."); 1324 break; 1325 } 1326 } 1327 1328 void bpf_diag_unreadable_reg(struct bpf_verifier_env *env, u32 insn_idx, int regno) 1329 { 1330 const struct bpf_func_state *frame = diag_current_frame(env); 1331 struct bpf_diag_history_opts opts = { 1332 .scope = BPF_DIAG_HISTORY_SCOPE_REG, 1333 .frame_id = frame->diag_frame_id, 1334 .frameno = frame->frameno, 1335 .regno = regno, 1336 }; 1337 const struct bpf_diag_log *log = env->diag ? &env->diag->log : NULL; 1338 struct bpf_diag_mod_target target; 1339 bool invalidated = false; 1340 int i; 1341 1342 target = diag_reg_target(opts.frame_id, opts.frameno, regno); 1343 for (i = log ? log->cnt : 0; i > 0; i--) { 1344 const struct bpf_diag_history_event *event; 1345 1346 event = &log->events[log_pos(log, i - 1)]; 1347 1348 if (event->kind != BPF_DIAG_HISTORY_MOD || 1349 !diag_target_matches(&event->mod.target, &target)) 1350 continue; 1351 invalidated = event->mod.new.type == NOT_INIT; 1352 break; 1353 } 1354 1355 bpf_diag_header(env, REGISTER_TYPE_SAFETY, "unreadable register"); 1356 if (invalidated) 1357 diag_reason( 1358 env, "R%d is not readable here. A previous operation invalidated this register, so the verifier cannot use it as an input.", 1359 regno); 1360 else if (log && !log->first_seq) 1361 diag_reason(env, 1362 "R%d has never been initialized on this path, so the verifier cannot use it as an input.", 1363 regno); 1364 else 1365 diag_reason( 1366 env, "R%d is not readable here. It may never have been initialized, or an earlier operation may have invalidated it.", 1367 regno); 1368 1369 diag_section(env, "At"); 1370 bpf_diag_source(env, insn_idx, "error", "R%d is not readable", regno); 1371 1372 if (regno >= 0) 1373 diag_print_history(env, &opts); 1374 1375 if (invalidated) 1376 diag_suggestion( 1377 env, "Avoid using the register after it is invalidated, or initialize it again before this instruction."); 1378 else if (log && !log->first_seq) 1379 diag_suggestion(env, "Initialize R%d on every path before this instruction.", regno); 1380 else 1381 diag_suggestion( 1382 env, "Initialize the register on every path, or initialize it again after any operation that invalidates it."); 1383 } 1384 1385 static int diag_stack_argno(u8 slot) 1386 { 1387 return MAX_BPF_FUNC_REG_ARGS + slot + 1; 1388 } 1389 1390 static void diag_format_stack_arg(char *buf, size_t size, u8 slot, const char *arg_name) 1391 { 1392 int argno = diag_stack_argno(slot); 1393 const char *ordinal = diag_arg_ordinal(argno); 1394 1395 if (ordinal && arg_name) 1396 scnprintf(buf, size, "outgoing stack argument %u (%s argument, %s)", slot + 1, 1397 ordinal, arg_name); 1398 else if (ordinal) 1399 scnprintf(buf, size, "outgoing stack argument %u (%s argument)", slot + 1, ordinal); 1400 else if (arg_name) 1401 scnprintf(buf, size, "outgoing stack argument %u (%s)", slot + 1, arg_name); 1402 else 1403 scnprintf(buf, size, "outgoing stack argument %u", slot + 1); 1404 } 1405 1406 void bpf_diag_stack_arg_uninit(struct bpf_verifier_env *env, u32 insn_idx, int nargs, 1407 int stack_arg_slot, const char *callee_name, 1408 const char *arg_name) 1409 { 1410 const struct bpf_func_state *frame = diag_current_frame(env); 1411 struct bpf_diag_history_opts opts = { 1412 .scope = BPF_DIAG_HISTORY_SCOPE_STACK_ARG, 1413 .frame_id = frame->diag_frame_id, 1414 .frameno = frame->frameno, 1415 .stack_arg_slot = stack_arg_slot, 1416 }; 1417 const char *arg_buf; 1418 1419 arg_buf = bpf_diag_fmt_buf(env, BPF_DIAG_FMT_BUF_SIZE); 1420 if (arg_buf) 1421 diag_format_stack_arg((char *)arg_buf, BPF_DIAG_FMT_BUF_SIZE, stack_arg_slot, 1422 arg_name); 1423 else 1424 arg_buf = ""; 1425 bpf_diag_header(env, REGISTER_TYPE_SAFETY, "missing stack argument"); 1426 if (callee_name && *callee_name) 1427 diag_reason( 1428 env, "Function %s expects %d arguments, but %s is not initialized at this call.", 1429 callee_name, nargs, arg_buf); 1430 else 1431 diag_reason( 1432 env, "The callee expects %d arguments, but %s is not initialized at this call.", 1433 nargs, arg_buf); 1434 1435 diag_section(env, "At"); 1436 bpf_diag_source(env, insn_idx, "error", "%s is not initialized", arg_buf); 1437 1438 if (stack_arg_slot >= 0) 1439 diag_print_history(env, &opts); 1440 1441 diag_suggestion( 1442 env, "Write the outgoing stack argument after any operation that may invalidate stored pointer values, and before making this call."); 1443 } 1444 1445 void bpf_diag_memory(struct bpf_verifier_env *env, u32 insn_idx, const char *problem, 1446 const char *reason, const char *suggestion) 1447 { 1448 bpf_diag_header(env, MEMORY_SAFETY, problem); 1449 diag_reason(env, "%s", reason); 1450 1451 diag_section(env, "At"); 1452 bpf_diag_source(env, insn_idx, "error", "%s", problem); 1453 1454 diag_suggestion(env, "%s", suggestion); 1455 } 1456 1457 void bpf_diag_record_branch(struct bpf_verifier_env *env, u32 insn_idx, bool cond_true) 1458 { 1459 struct bpf_diag_history_event event = { 1460 .insn_idx = insn_idx, 1461 .kind = BPF_DIAG_HISTORY_BRANCH, 1462 .branch = { 1463 .cond_true = cond_true, 1464 }, 1465 }; 1466 1467 diag_append_history(env, &event); 1468 } 1469 1470 static void diag_snapshot_reg(struct bpf_diag_reg_snapshot *snapshot, 1471 const struct bpf_reg_state *reg) 1472 { 1473 snapshot->type = reg->type; 1474 if (type_is_map_ptr(reg->type)) 1475 snapshot->map_ptr = reg->map_ptr; 1476 if (base_type(reg->type) == PTR_TO_BTF_ID && reg->btf && reg->btf_id) { 1477 snapshot->btf_id = reg->btf_id; 1478 snapshot->btf = reg->btf; 1479 } 1480 snapshot->var_off = reg->var_off; 1481 snapshot->r64 = reg->r64; 1482 } 1483 1484 static bool diag_mod_insn_origin(struct bpf_verifier_env *env, u32 insn_idx, 1485 const struct bpf_diag_mod_target *target, 1486 struct bpf_diag_mod_target *origin) 1487 { 1488 const struct bpf_insn *insn = &env->prog->insnsi[insn_idx]; 1489 u8 class = BPF_CLASS(insn->code); 1490 const struct bpf_func_state *state; 1491 1492 if (target->kind == BPF_DIAG_MOD_TARGET_REG && (class == BPF_ALU || class == BPF_ALU64) && 1493 BPF_OP(insn->code) == BPF_MOV && BPF_SRC(insn->code) == BPF_X) { 1494 *origin = diag_reg_target(target->frame_id, target->frameno, insn->src_reg); 1495 return true; 1496 } 1497 1498 if ((target->kind != BPF_DIAG_MOD_TARGET_STACK_ARG && 1499 target->kind != BPF_DIAG_MOD_TARGET_STACK_SLOT) || 1500 class != BPF_STX) 1501 return false; 1502 1503 state = env->cur_state->frame[env->cur_state->curframe]; 1504 *origin = diag_reg_target(state->diag_frame_id, state->frameno, insn->src_reg); 1505 return true; 1506 } 1507 1508 static bool diag_mod_keeps_lineage(struct bpf_verifier_env *env, 1509 const struct bpf_diag_history_event *event) 1510 { 1511 const struct bpf_insn *insn; 1512 u8 class; 1513 1514 if (event->mod.reason != BPF_DIAG_MOD_WRITE || 1515 event->mod.target.kind != BPF_DIAG_MOD_TARGET_REG) 1516 return false; 1517 1518 insn = &env->prog->insnsi[event->insn_idx]; 1519 class = BPF_CLASS(insn->code); 1520 if (class != BPF_ALU && class != BPF_ALU64) 1521 return false; 1522 1523 switch (BPF_OP(insn->code)) { 1524 case BPF_ADD: 1525 case BPF_SUB: 1526 case BPF_MUL: 1527 case BPF_OR: 1528 case BPF_AND: 1529 case BPF_LSH: 1530 case BPF_RSH: 1531 case BPF_ARSH: 1532 case BPF_XOR: 1533 case BPF_NEG: 1534 case BPF_END: 1535 return true; 1536 default: 1537 return false; 1538 } 1539 } 1540 1541 static void diag_record_mod(struct bpf_verifier_env *env, u32 insn_idx, 1542 struct bpf_diag_mod_target target, 1543 enum bpf_diag_mod_reason reason, 1544 const struct bpf_reg_state *old_reg, 1545 const struct bpf_reg_state *new_reg, 1546 const struct bpf_diag_mod_target *origin) 1547 { 1548 struct bpf_diag_history_event event = { 1549 .insn_idx = insn_idx, 1550 .kind = BPF_DIAG_HISTORY_MOD, 1551 .mod = { 1552 .target = target, 1553 .reason = reason, 1554 }, 1555 }; 1556 1557 if (old_reg) 1558 diag_snapshot_reg(&event.mod.old, old_reg); 1559 if (new_reg) 1560 diag_snapshot_reg(&event.mod.new, new_reg); 1561 if (origin) { 1562 event.mod.origin = *origin; 1563 event.mod.origin_valid = true; 1564 } else if (diag_mod_insn_origin(env, insn_idx, &target, &event.mod.origin)) { 1565 event.mod.origin_valid = true; 1566 } 1567 if (old_reg && new_reg && reason == BPF_DIAG_MOD_WRITE && 1568 !memcmp(&event.mod.old, &event.mod.new, sizeof(event.mod.old)) && 1569 !event.mod.origin_valid && 1570 diag_mod_keeps_lineage(env, &event)) 1571 return; 1572 1573 diag_append_history(env, &event); 1574 } 1575 1576 static struct bpf_reg_state *target_to_reg(struct bpf_verifier_env *env, 1577 const struct bpf_diag_mod_target *target) 1578 { 1579 struct bpf_verifier_state *vstate = env->cur_state; 1580 struct bpf_func_state *state; 1581 1582 state = target->frameno <= vstate->curframe ? vstate->frame[target->frameno] : NULL; 1583 1584 if (!state) 1585 return NULL; 1586 if (state->diag_frame_id != target->frame_id) 1587 return NULL; 1588 1589 switch (target->kind) { 1590 case BPF_DIAG_MOD_TARGET_REG: 1591 if (target->regno >= MAX_BPF_REG) 1592 return NULL; 1593 return &state->regs[target->regno]; 1594 case BPF_DIAG_MOD_TARGET_STACK_ARG: 1595 if (target->stack_arg >= state->out_stack_arg_cnt) 1596 return NULL; 1597 return &state->stack_arg_regs[target->stack_arg]; 1598 case BPF_DIAG_MOD_TARGET_STACK_SLOT: 1599 if (target->spi >= bpf_stack_nr_slots(state)) 1600 return NULL; 1601 return &bpf_stack_slot(state, target->spi)->spilled_ptr; 1602 default: 1603 return NULL; 1604 } 1605 } 1606 1607 static bool reg_to_target(struct bpf_verifier_env *env, const struct bpf_reg_state *reg, 1608 struct bpf_diag_mod_target *target) 1609 { 1610 struct bpf_verifier_state *vstate = env->cur_state; 1611 unsigned long addr = (unsigned long)reg; 1612 int frame; 1613 1614 for (frame = 0; frame <= vstate->curframe; frame++) { 1615 struct bpf_func_state *state = vstate->frame[frame]; 1616 unsigned long start, end; 1617 u32 nslots = bpf_stack_nr_slots(state); 1618 int spi; 1619 1620 start = (unsigned long)state->regs; 1621 end = (unsigned long)(state->regs + MAX_BPF_REG); 1622 if (addr >= start && addr < end) { 1623 *target = diag_reg_target(state->diag_frame_id, state->frameno, 1624 reg - state->regs); 1625 return true; 1626 } 1627 1628 start = (unsigned long)state->stack_arg_regs; 1629 end = (unsigned long)(state->stack_arg_regs + state->out_stack_arg_cnt); 1630 if (state->out_stack_arg_cnt && addr >= start && addr < end) { 1631 *target = diag_stack_arg_target(state->diag_frame_id, state->frameno, 1632 reg - state->stack_arg_regs); 1633 return true; 1634 } 1635 1636 /* 1637 * Map the pointer back to its slot by address, which relies on 1638 * the slots forming one contiguous array as bpf_stack_slot() 1639 * indexes it. 1640 */ 1641 start = (unsigned long)state->stack; 1642 end = (unsigned long)(state->stack + nslots); 1643 if (nslots && addr >= start && addr < end) { 1644 spi = ((const char *)reg - (const char *)state->stack) / 1645 sizeof(*state->stack); 1646 *target = diag_stack_slot_target(state->diag_frame_id, state->frameno, spi); 1647 return true; 1648 } 1649 } 1650 return false; 1651 } 1652 1653 void bpf_diag_mod_begin(struct bpf_verifier_env *env, const struct bpf_reg_state *reg, 1654 const struct bpf_reg_state *origin, enum bpf_diag_mod_reason reason) 1655 { 1656 struct bpf_diag *diag = env->diag; 1657 1658 if (!diag) 1659 return; 1660 diag->mod.active = reg_to_target(env, reg, &diag->mod.target); 1661 if (!diag->mod.active) 1662 return; 1663 diag->mod.target_reg_snapshot = *reg; 1664 diag->mod.insn_idx = env->insn_idx; 1665 diag->mod.reason = reason; 1666 diag->mod.origin_valid = origin && reg_to_target(env, origin, &diag->mod.origin); 1667 } 1668 1669 void bpf_diag_mod_end(struct bpf_verifier_env *env) 1670 { 1671 struct bpf_diag *diag = env->diag; 1672 const struct bpf_reg_state *new_reg; 1673 1674 if (!diag || !diag->mod.active) 1675 return; 1676 diag->mod.active = false; 1677 /* 1678 * Resolve the target again because the enclosing function state's stack 1679 * may have been reallocated while the modification was in progress. 1680 */ 1681 new_reg = target_to_reg(env, &diag->mod.target); 1682 if (!new_reg) 1683 return; 1684 diag_record_mod(env, diag->mod.insn_idx, diag->mod.target, diag->mod.reason, 1685 &diag->mod.target_reg_snapshot, new_reg, 1686 diag->mod.origin_valid ? &diag->mod.origin : NULL); 1687 } 1688 1689 void bpf_diag_record_scrub(struct bpf_verifier_env *env, const struct bpf_reg_state *reg, 1690 enum bpf_diag_mod_reason reason) 1691 { 1692 struct bpf_diag_mod_target target; 1693 1694 if (!env->diag || reg->type == NOT_INIT || !reg_to_target(env, reg, &target)) 1695 return; 1696 diag_record_mod(env, env->insn_idx, target, reason, reg, NULL, NULL); 1697 } 1698 1699 void bpf_diag_record_scrub_stack(struct bpf_verifier_env *env, 1700 const struct bpf_func_state *state, s16 min_off, s16 max_off, 1701 enum bpf_diag_mod_reason reason) 1702 { 1703 diag_record_mod(env, env->insn_idx, 1704 diag_stack_range_target(state->diag_frame_id, state->frameno, min_off, max_off), 1705 reason, NULL, NULL, NULL); 1706 } 1707 1708 static void diag_record_ref(struct bpf_verifier_env *env, u32 insn_idx, u8 kind, u32 ref_id) 1709 { 1710 struct bpf_diag_history_event event = { 1711 .insn_idx = insn_idx, 1712 .kind = kind, 1713 .ref = { 1714 .ref_id = ref_id, 1715 }, 1716 }; 1717 1718 diag_append_history(env, &event); 1719 } 1720 1721 void bpf_diag_record_ref_acquire(struct bpf_verifier_env *env, u32 insn_idx, u32 ref_id) 1722 { 1723 diag_record_ref(env, insn_idx, BPF_DIAG_HISTORY_REF_ACQUIRE, ref_id); 1724 } 1725 1726 void bpf_diag_record_ref_release(struct bpf_verifier_env *env, u32 insn_idx, u32 ref_id) 1727 { 1728 diag_record_ref(env, insn_idx, BPF_DIAG_HISTORY_REF_RELEASE, ref_id); 1729 } 1730 1731 void bpf_diag_record_context(struct bpf_verifier_env *env, u32 insn_idx, 1732 enum bpf_diag_context_kind ctx_kind, bool enter, u32 depth) 1733 { 1734 /* 1735 * Keep leave events so context rendering can stop at a depth-zero exit 1736 * and show nested-region depth accurately for the active path. 1737 */ 1738 struct bpf_diag_history_event event = { 1739 .insn_idx = insn_idx, 1740 .kind = BPF_DIAG_HISTORY_CONTEXT, 1741 .ctx = { 1742 .kind = ctx_kind, 1743 .enter = enter, 1744 .depth = depth, 1745 }, 1746 }; 1747 1748 diag_append_history(env, &event); 1749 } 1750 1751 static int diag_history_context_start_idx(const struct bpf_diag_log *log, 1752 const struct bpf_diag_history_opts *opts) 1753 { 1754 int i; 1755 1756 if (!opts->ctx_depth) 1757 return 0; 1758 1759 /* Find the most recent outermost entry, or a depth-zero exit. */ 1760 for (i = log->cnt; i > 0; i--) { 1761 const struct bpf_diag_history_event *event; 1762 1763 event = &log->events[log_pos(log, i - 1)]; 1764 1765 if (event->kind != BPF_DIAG_HISTORY_CONTEXT || event->ctx.kind != opts->ctx_kind) 1766 continue; 1767 1768 if (event->ctx.enter && event->ctx.depth == 1) 1769 return i - 1; 1770 if (!event->ctx.enter && event->ctx.depth == 0) 1771 return 0; 1772 } 1773 1774 return 0; 1775 } 1776 1777 struct bpf_diag_history_filter { 1778 const struct bpf_diag_history_opts *opts; 1779 u32 lineage_start; 1780 bool lineage_valid; 1781 }; 1782 1783 static bool diag_target_matches(const struct bpf_diag_mod_target *event_target, 1784 const struct bpf_diag_mod_target *target) 1785 { 1786 int slot_off; 1787 1788 if (event_target->frame_id != target->frame_id || event_target->frameno != target->frameno) 1789 return false; 1790 1791 if (event_target->kind == BPF_DIAG_MOD_TARGET_STACK_RANGE && 1792 target->kind == BPF_DIAG_MOD_TARGET_STACK_SLOT) { 1793 slot_off = -(target->spi + 1) * BPF_REG_SIZE; 1794 return event_target->range.min_off < slot_off + BPF_REG_SIZE && 1795 event_target->range.max_off > slot_off; 1796 } 1797 1798 if (event_target->kind != target->kind) 1799 return false; 1800 1801 switch (target->kind) { 1802 case BPF_DIAG_MOD_TARGET_REG: 1803 return event_target->regno == target->regno; 1804 case BPF_DIAG_MOD_TARGET_STACK_ARG: 1805 return event_target->stack_arg == target->stack_arg; 1806 case BPF_DIAG_MOD_TARGET_STACK_SLOT: 1807 return event_target->spi == target->spi; 1808 default: 1809 return false; 1810 } 1811 } 1812 1813 static void diag_build_lineage(struct bpf_verifier_env *env, struct bpf_diag_log *log, 1814 struct bpf_diag_history_filter *filter) 1815 { 1816 const struct bpf_diag_history_opts *opts = filter->opts; 1817 struct bpf_diag_mod_target target; 1818 int i; 1819 1820 for (i = 0; i < log->cnt; i++) 1821 log->events[log_pos(log, i)].in_lineage = false; 1822 1823 if (opts->scope == BPF_DIAG_HISTORY_SCOPE_REG) 1824 target = diag_reg_target(opts->frame_id, opts->frameno, opts->regno); 1825 else if (opts->scope == BPF_DIAG_HISTORY_SCOPE_STACK_ARG) 1826 target = diag_stack_arg_target(opts->frame_id, opts->frameno, 1827 opts->stack_arg_slot); 1828 else 1829 return; 1830 1831 /* 1832 * Find the nearest mutation of the active target. A fill or spill changes 1833 * the target to its origin, so the same walk follows register/stack 1834 * lineage recursively until it reaches the write that created the value. 1835 */ 1836 for (i = log->cnt; i > 0; i--) { 1837 struct bpf_diag_history_event *event; 1838 1839 event = &log->events[log_pos(log, i - 1)]; 1840 if (event->kind != BPF_DIAG_HISTORY_MOD || 1841 !diag_target_matches(&event->mod.target, &target)) 1842 continue; 1843 1844 event->in_lineage = true; 1845 filter->lineage_start = i - 1; 1846 filter->lineage_valid = true; 1847 1848 if (event->mod.origin_valid) { 1849 target = event->mod.origin; 1850 continue; 1851 } 1852 if (event->mod.reason != BPF_DIAG_MOD_WRITE && 1853 event->mod.reason != BPF_DIAG_MOD_SPILL) 1854 continue; 1855 if (diag_mod_keeps_lineage(env, event)) 1856 continue; 1857 break; 1858 } 1859 } 1860 1861 static int diag_history_start_idx(const struct bpf_diag_log *log, 1862 const struct bpf_diag_history_filter *filter) 1863 { 1864 const struct bpf_diag_history_opts *opts = filter->opts; 1865 int i; 1866 1867 if (opts->scope == BPF_DIAG_HISTORY_SCOPE_CONTEXT) 1868 return diag_history_context_start_idx(log, opts); 1869 if (filter->lineage_valid) 1870 return filter->lineage_start; 1871 if (opts->scope != BPF_DIAG_HISTORY_SCOPE_REF) 1872 return 0; 1873 1874 for (i = log->cnt; i > 0; i--) { 1875 const struct bpf_diag_history_event *event; 1876 1877 event = &log->events[log_pos(log, i - 1)]; 1878 if (event->kind == BPF_DIAG_HISTORY_REF_ACQUIRE && 1879 event->ref.ref_id == opts->ref_id) 1880 return i - 1; 1881 } 1882 1883 return 0; 1884 } 1885 1886 static bool diag_history_event_visible(const struct bpf_diag_history_event *event, 1887 const struct bpf_diag_history_filter *filter) 1888 { 1889 const struct bpf_diag_history_opts *opts = filter->opts; 1890 1891 switch (event->kind) { 1892 case BPF_DIAG_HISTORY_BRANCH: 1893 return true; 1894 case BPF_DIAG_HISTORY_MOD: 1895 return filter->lineage_valid && event->in_lineage; 1896 case BPF_DIAG_HISTORY_REF_ACQUIRE: 1897 case BPF_DIAG_HISTORY_REF_RELEASE: 1898 return opts->scope == BPF_DIAG_HISTORY_SCOPE_REF && 1899 event->ref.ref_id == opts->ref_id; 1900 case BPF_DIAG_HISTORY_CONTEXT: 1901 return opts->scope == BPF_DIAG_HISTORY_SCOPE_CONTEXT && 1902 event->ctx.kind == opts->ctx_kind; 1903 default: 1904 return false; 1905 } 1906 } 1907 1908 static const char *diag_s64_bound_name(s64 value) 1909 { 1910 if (value == S64_MIN) 1911 return "S64_MIN"; 1912 if (value == S64_MAX) 1913 return "S64_MAX"; 1914 return NULL; 1915 } 1916 1917 static const char *diag_u64_bound_name(u64 value) 1918 { 1919 if (value == U64_MAX) 1920 return "U64_MAX"; 1921 return NULL; 1922 } 1923 1924 static const char *diag_s64_str(struct bpf_verifier_env *env, s64 value) 1925 { 1926 return diag_s64_bound_name(value) ?: bpf_diag_fmt(env, "%lld", value); 1927 } 1928 1929 static const char *diag_u64_str(struct bpf_verifier_env *env, u64 value) 1930 { 1931 return diag_u64_bound_name(value) ?: bpf_diag_fmt(env, "%llu", value); 1932 } 1933 1934 static bool diag_cnum64_unknown(struct cnum64 range) 1935 { 1936 return cnum64_smin(range) == S64_MIN && cnum64_smax(range) == S64_MAX && 1937 cnum64_umin(range) == 0 && cnum64_umax(range) == U64_MAX; 1938 } 1939 1940 static bool diag_snapshot_unknown(const struct bpf_diag_reg_snapshot *snapshot) 1941 { 1942 return tnum_is_unknown(snapshot->var_off) && diag_cnum64_unknown(snapshot->r64); 1943 } 1944 1945 static const char *diag_scalar_range(struct bpf_verifier_env *env, struct cnum64 range) 1946 { 1947 return bpf_diag_fmt(env, "signed range [%s, %s], unsigned range [%s, %s]", 1948 diag_s64_str(env, cnum64_smin(range)), 1949 diag_s64_str(env, cnum64_smax(range)), 1950 diag_u64_str(env, cnum64_umin(range)), 1951 diag_u64_str(env, cnum64_umax(range))); 1952 } 1953 1954 const char *bpf_diag_fmt_s64_sum(struct bpf_verifier_env *env, s64 value, int addend) 1955 { 1956 s64 sum; 1957 1958 if (check_add_overflow(value, (s64)addend, &sum)) 1959 return bpf_diag_fmt(env, "%lld plus %d (%s)", value, addend, 1960 addend < 0 ? "below S64_MIN" : "above S64_MAX"); 1961 1962 return bpf_diag_fmt(env, "%lld", sum); 1963 } 1964 1965 static const char *diag_access_offset(struct bpf_verifier_env *env, int off, 1966 const struct bpf_reg_state *reg) 1967 { 1968 if (tnum_is_const(reg->var_off)) 1969 return bpf_diag_fmt(env, "constant %s", 1970 bpf_diag_fmt_s64_sum(env, (s64)reg->var_off.value, off)); 1971 1972 if (tnum_is_unknown(reg->var_off) && diag_cnum64_unknown(reg->r64)) 1973 return bpf_diag_fmt(env, "unbounded"); 1974 1975 if (off) 1976 return bpf_diag_fmt(env, 1977 "variable: known bits %#llx, unknown mask %#llx, plus fixed offset %d; %s", 1978 (u64)reg->var_off.value, reg->var_off.mask, off, 1979 diag_scalar_range(env, reg->r64)); 1980 return bpf_diag_fmt(env, "variable: known bits %#llx, unknown mask %#llx; %s", 1981 (u64)reg->var_off.value, reg->var_off.mask, 1982 diag_scalar_range(env, reg->r64)); 1983 } 1984 1985 void bpf_diag_mem_bounds(struct bpf_verifier_env *env, u32 insn_idx, int regno, 1986 const char *reg_name, const char *type_name, const char *proof, 1987 int off, int size, u32 mem_size, const struct bpf_reg_state *reg) 1988 { 1989 const struct bpf_func_state *frame = diag_current_frame(env); 1990 struct bpf_diag_history_opts opts = { 1991 .scope = BPF_DIAG_HISTORY_SCOPE_REG, 1992 .frame_id = frame->diag_frame_id, 1993 .frameno = frame->frameno, 1994 .regno = regno, 1995 }; 1996 const char *offset_desc; 1997 1998 if (!bpf_diag_enabled(env)) 1999 return; 2000 2001 offset_desc = diag_access_offset(env, off, reg); 2002 2003 bpf_diag_header(env, MEMORY_SAFETY, "access outside bounds"); 2004 diag_reason( 2005 env, "The verifier cannot prove offset + access_size <= object_size. Here, %s. %s is %s; offset is %s; access_size is %d; object_size is %u.", 2006 proof, reg_name, type_name, offset_desc, size, mem_size); 2007 2008 diag_section(env, "At"); 2009 bpf_diag_source(env, insn_idx, "error", "access may be outside object bounds"); 2010 2011 if (regno >= 0) 2012 diag_print_history(env, &opts); 2013 2014 diag_suggestion( 2015 env, "Add or adjust a bounds check that proves offset + access_size stays within the object."); 2016 } 2017 2018 static const char *diag_lock_name(const struct bpf_reference_state *lock) 2019 { 2020 switch (lock->type) { 2021 case REF_TYPE_LOCK: 2022 return "bpf_spin_lock"; 2023 case REF_TYPE_RES_LOCK: 2024 return "resource spin lock"; 2025 case REF_TYPE_RES_LOCK_IRQ: 2026 return "IRQ-saving resource spin lock"; 2027 default: 2028 return "lock"; 2029 } 2030 } 2031 2032 static void diag_res_report(struct bpf_verifier_env *env, u32 insn_idx, const char *problem, 2033 const char *reason) 2034 { 2035 bpf_diag_header(env, RESOURCE_LIFETIME_SAFETY, problem); 2036 diag_reason(env, "%s", reason); 2037 2038 diag_section(env, "At"); 2039 bpf_diag_source(env, insn_idx, "error", "%s", problem); 2040 } 2041 2042 void bpf_diag_res(struct bpf_verifier_env *env, u32 insn_idx, const char *problem, 2043 const char *reason, const char *suggestion) 2044 { 2045 diag_res_report(env, insn_idx, problem, reason); 2046 diag_suggestion(env, "%s", suggestion); 2047 } 2048 2049 void bpf_diag_lock(struct bpf_verifier_env *env, u32 insn_idx, const char *problem, 2050 const char *reason, const char *suggestion, 2051 const struct bpf_reference_state *active_lock) 2052 { 2053 diag_res_report(env, insn_idx, problem, reason); 2054 2055 if (active_lock) { 2056 diag_section(env, "Active lock"); 2057 bpf_diag_source(env, active_lock->insn_idx, "acquired", 2058 "active %s has verifier identity %d", 2059 diag_lock_name(active_lock), active_lock->id); 2060 } 2061 2062 diag_suggestion(env, "%s", suggestion); 2063 } 2064 2065 void bpf_diag_irq(struct bpf_verifier_env *env, u32 insn_idx, const char *problem, 2066 const char *reason, const char *suggestion, u32 depth) 2067 { 2068 struct bpf_diag_history_opts opts = { 2069 .scope = BPF_DIAG_HISTORY_SCOPE_CONTEXT, 2070 .ctx_kind = BPF_DIAG_CONTEXT_IRQ, 2071 .ctx_depth = depth, 2072 }; 2073 2074 bpf_diag_header(env, RESOURCE_LIFETIME_SAFETY, problem); 2075 diag_reason(env, "%s", reason); 2076 2077 diag_section(env, "At"); 2078 bpf_diag_source(env, insn_idx, "error", "%s", problem); 2079 2080 if (depth) 2081 diag_print_history(env, &opts); 2082 2083 diag_suggestion(env, "%s", suggestion); 2084 } 2085 2086 void bpf_diag_leak(struct bpf_verifier_env *env, u32 ref_id, u32 alloc_insn, u32 fail_insn) 2087 { 2088 struct bpf_diag_history_opts opts = { 2089 .scope = BPF_DIAG_HISTORY_SCOPE_REF, 2090 .ref_id = ref_id, 2091 }; 2092 2093 bpf_diag_header(env, RESOURCE_LIFETIME_SAFETY, "unreleased resource"); 2094 diag_reason( 2095 env, "Owned resource (id=%u) was acquired at instruction %u and still needs to be released before this exit path.", 2096 ref_id, alloc_insn); 2097 2098 diag_section(env, "At"); 2099 bpf_diag_source(env, fail_insn, "error", 2100 "owned resource (id=%u) still needs release", ref_id); 2101 2102 diag_print_history(env, &opts); 2103 2104 diag_suggestion( 2105 env, "Release or transfer ownership of the acquired resource on every path before the program exits."); 2106 } 2107 2108 static const char *diag_var_offset(struct bpf_verifier_env *env, 2109 const struct bpf_diag_reg_snapshot *snapshot) 2110 { 2111 if (tnum_is_const(snapshot->var_off)) 2112 return bpf_diag_fmt(env, "at offset %lld", (s64)snapshot->var_off.value); 2113 2114 if (diag_snapshot_unknown(snapshot)) 2115 return bpf_diag_fmt(env, "with unknown offset"); 2116 2117 return bpf_diag_fmt(env, 2118 "with variable offset: known bits %#llx, unknown mask %#llx, %s", 2119 snapshot->var_off.value, snapshot->var_off.mask, 2120 diag_scalar_range(env, snapshot->r64)); 2121 } 2122 2123 static const char *diag_reg_map_name(const struct bpf_map *map) 2124 { 2125 if (!map || !map->name[0]) 2126 return NULL; 2127 2128 return map->name; 2129 } 2130 2131 static const char *diag_reg_snapshot(struct bpf_verifier_env *env, 2132 const struct bpf_diag_reg_snapshot *snapshot) 2133 { 2134 const char *type_name = reg_type_str(env, snapshot->type); 2135 const char *offset = diag_var_offset(env, snapshot); 2136 const char *btf = snapshot->btf && snapshot->btf_id ? 2137 bpf_diag_fmt_btf_type(env, snapshot->btf, snapshot->btf_id) : NULL; 2138 const char *map_name; 2139 2140 if (snapshot->type == SCALAR_VALUE) { 2141 if (tnum_is_const(snapshot->var_off)) 2142 return bpf_diag_fmt(env, "integer scalar value %lld", 2143 (s64)snapshot->var_off.value); 2144 if (diag_snapshot_unknown(snapshot)) 2145 return bpf_diag_fmt(env, "integer scalar with unknown value"); 2146 if (cnum64_is_const(snapshot->r64)) 2147 return bpf_diag_fmt(env, "integer scalar value %lld", 2148 cnum64_smin(snapshot->r64)); 2149 return bpf_diag_fmt(env, "integer scalar with %s", 2150 diag_scalar_range(env, snapshot->r64)); 2151 } 2152 2153 if (snapshot->type == NOT_INIT) 2154 return bpf_diag_fmt(env, "uninitialized value"); 2155 2156 if (base_type(snapshot->type) == PTR_TO_CTX) 2157 return bpf_diag_fmt(env, "context pointer %s", offset); 2158 2159 if (base_type(snapshot->type) == PTR_TO_STACK) 2160 return bpf_diag_fmt(env, "stack pointer %s", offset); 2161 2162 if (base_type(snapshot->type) == PTR_TO_MAP_VALUE) { 2163 const char *kind = type_may_be_null(snapshot->type) ? "nullable map value" : 2164 "map value"; 2165 2166 map_name = diag_reg_map_name(snapshot->map_ptr); 2167 if (map_name) 2168 return bpf_diag_fmt(env, "%s from %s %s", kind, map_name, offset); 2169 return bpf_diag_fmt(env, "%s %s", kind, offset); 2170 } 2171 2172 if (base_type(snapshot->type) == CONST_PTR_TO_MAP) { 2173 map_name = diag_reg_map_name(snapshot->map_ptr); 2174 if (map_name) 2175 return bpf_diag_fmt(env, "map pointer for map %s", map_name); 2176 return bpf_diag_fmt(env, "map pointer"); 2177 } 2178 2179 if (type_is_non_owning_ref(snapshot->type)) { 2180 if (btf) 2181 return bpf_diag_fmt(env, "borrowed allocated object pointer type=%s", btf); 2182 return bpf_diag_fmt(env, "borrowed allocated object pointer"); 2183 } 2184 2185 if (type_is_ptr_alloc_obj(snapshot->type)) { 2186 if (btf) 2187 return bpf_diag_fmt(env, "owned allocated object pointer type=%s", btf); 2188 return bpf_diag_fmt(env, "owned allocated object pointer"); 2189 } 2190 2191 if (base_type(snapshot->type) == PTR_TO_BTF_ID && btf) 2192 return bpf_diag_fmt(env, "%s type=%s %s", type_name, btf, offset); 2193 2194 return bpf_diag_fmt(env, "%s %s", type_name, offset); 2195 } 2196 2197 static const char *diag_mod_target_desc(struct bpf_verifier_env *env, 2198 const struct bpf_diag_mod_target *target) 2199 { 2200 switch (target->kind) { 2201 case BPF_DIAG_MOD_TARGET_REG: 2202 return bpf_diag_fmt(env, "R%u", target->regno); 2203 case BPF_DIAG_MOD_TARGET_STACK_ARG: 2204 return bpf_diag_fmt(env, "*(R11-%u)", (target->stack_arg + 1) * BPF_REG_SIZE); 2205 case BPF_DIAG_MOD_TARGET_STACK_SLOT: 2206 return bpf_diag_fmt(env, "stack slot fp%d", -(target->spi + 1) * BPF_REG_SIZE); 2207 default: 2208 return "value"; 2209 } 2210 } 2211 2212 static void diag_print_mod(struct bpf_verifier_env *env, const struct bpf_diag_history_event *event) 2213 { 2214 const struct bpf_diag_mod_target *target = &event->mod.target; 2215 const char *target_desc, *reason = NULL, *old, *new; 2216 const char *label = "update"; 2217 2218 if (target->kind == BPF_DIAG_MOD_TARGET_STACK_RANGE) { 2219 bpf_diag_source( 2220 env, event->insn_idx, "invalidated", 2221 "variable-offset stack write may affect bytes fp%d through fp%d", 2222 target->range.min_off, target->range.max_off - 1); 2223 return; 2224 } 2225 2226 old = diag_reg_snapshot(env, &event->mod.old); 2227 new = diag_reg_snapshot(env, &event->mod.new); 2228 target_desc = diag_mod_target_desc(env, target); 2229 2230 switch (event->mod.reason) { 2231 case BPF_DIAG_MOD_REF_RELEASE: 2232 reason = target->kind == BPF_DIAG_MOD_TARGET_REG ? "resource release invalidated " 2233 "this pointer" : 2234 "resource release invalidated " 2235 "this value"; 2236 break; 2237 case BPF_DIAG_MOD_PKT_DATA_CHANGE: 2238 reason = "packet data may have moved"; 2239 break; 2240 case BPF_DIAG_MOD_NON_OWN_REF: 2241 reason = "leaving the protected region invalidated this borrowed pointer"; 2242 break; 2243 case BPF_DIAG_MOD_CALLER_SAVED: 2244 reason = target->kind == BPF_DIAG_MOD_TARGET_STACK_ARG ? 2245 "call invalidated this outgoing stack argument" : 2246 "call invalidated this caller-saved register"; 2247 break; 2248 case BPF_DIAG_MOD_WRITE: 2249 if (target->kind == BPF_DIAG_MOD_TARGET_STACK_SLOT) 2250 reason = "a later stack write overwrote this spilled value"; 2251 break; 2252 case BPF_DIAG_MOD_SPILL: 2253 label = "spilled"; 2254 break; 2255 case BPF_DIAG_MOD_VAR_WRITE: 2256 default: 2257 break; 2258 } 2259 2260 if (reason) { 2261 bpf_diag_source(env, event->insn_idx, "invalidated", 2262 "%s: %s; previous value was %s", target_desc, reason, old); 2263 return; 2264 } 2265 2266 bpf_diag_source(env, event->insn_idx, label, "%s changed from %s to %s", target_desc, 2267 old, new); 2268 } 2269 2270 static void diag_print_ref_event(struct bpf_verifier_env *env, 2271 const struct bpf_diag_history_event *event) 2272 { 2273 const char *label; 2274 2275 label = event->kind == BPF_DIAG_HISTORY_REF_ACQUIRE ? "acquired" : "released"; 2276 bpf_diag_source(env, event->insn_idx, label, "owned resource (id=%u)", 2277 event->ref.ref_id); 2278 } 2279 2280 static const char *diag_context_name(enum bpf_diag_context_kind kind) 2281 { 2282 switch (kind) { 2283 case BPF_DIAG_CONTEXT_RCU: 2284 return "RCU read lock region"; 2285 case BPF_DIAG_CONTEXT_PREEMPT: 2286 return "non-preemptible region"; 2287 case BPF_DIAG_CONTEXT_IRQ: 2288 return "IRQ-disabled region"; 2289 case BPF_DIAG_CONTEXT_LOCK: 2290 return "lock region"; 2291 case BPF_DIAG_CONTEXT_NONE: 2292 default: 2293 return "non-sleepable program"; 2294 } 2295 } 2296 2297 static void diag_print_context_event(struct bpf_verifier_env *env, 2298 const struct bpf_diag_history_event *event) 2299 { 2300 bpf_diag_source(env, event->insn_idx, "context", "%s %s; depth is now %u", 2301 event->ctx.enter ? "entered" : "left", 2302 diag_context_name(event->ctx.kind), event->ctx.depth); 2303 } 2304 2305 static void diag_print_history(struct bpf_verifier_env *env, 2306 const struct bpf_diag_history_opts *opts) 2307 { 2308 const struct bpf_diag_history_event *event; 2309 struct bpf_diag_history_filter filter = { 2310 .opts = opts, 2311 }; 2312 struct bpf_diag_log *log; 2313 struct diag_fmt_mark mark; 2314 bool first = true; 2315 int start_idx; 2316 u32 i, visible_cnt = 0, visible_idx = 0; 2317 2318 if (!bpf_diag_enabled(env)) 2319 return; 2320 2321 if (!env->diag) 2322 return; 2323 log = &env->diag->log; 2324 2325 diag_build_lineage(env, log, &filter); 2326 2327 start_idx = diag_history_start_idx(log, &filter); 2328 for (i = start_idx; i < log->cnt; i++) { 2329 event = &log->events[log_pos(log, i)]; 2330 if (diag_history_event_visible(event, &filter)) 2331 visible_cnt++; 2332 } 2333 2334 if (!visible_cnt && !log->first_seq && opts->scope == BPF_DIAG_HISTORY_SCOPE_STACK_ARG) 2335 return; 2336 2337 diag_section(env, "Causal path"); 2338 mark = diag_fmt_save(env); 2339 for (i = start_idx; i < log->cnt; i++) { 2340 event = &log->events[log_pos(log, i)]; 2341 if (!diag_history_event_visible(event, &filter)) 2342 continue; 2343 2344 diag_fmt_restore(env, mark); 2345 if (visible_cnt > BPF_DIAG_HISTORY_RENDER_MAX && 2346 visible_idx >= BPF_DIAG_HISTORY_RENDER_MAX / 2 && 2347 visible_idx < visible_cnt - BPF_DIAG_HISTORY_RENDER_MAX / 2) { 2348 if (visible_idx++ != BPF_DIAG_HISTORY_RENDER_MAX / 2) 2349 continue; 2350 if (!first) 2351 diag_write(env, "\n"); 2352 first = false; 2353 diag_write(env, " %u intermediate causal-history events omitted\n", 2354 visible_cnt - BPF_DIAG_HISTORY_RENDER_MAX); 2355 continue; 2356 } 2357 visible_idx++; 2358 2359 if (!first) 2360 diag_write(env, "\n"); 2361 first = false; 2362 2363 switch (event->kind) { 2364 case BPF_DIAG_HISTORY_BRANCH: 2365 bpf_diag_source(env, event->insn_idx, "branch", 2366 "took the %s branch of this conditional, goto %s", 2367 event->branch.cond_true ? "true" : "false", 2368 event->branch.cond_true ? "followed" : "not followed"); 2369 break; 2370 case BPF_DIAG_HISTORY_MOD: 2371 diag_print_mod(env, event); 2372 break; 2373 case BPF_DIAG_HISTORY_REF_ACQUIRE: 2374 case BPF_DIAG_HISTORY_REF_RELEASE: 2375 diag_print_ref_event(env, event); 2376 break; 2377 case BPF_DIAG_HISTORY_CONTEXT: 2378 diag_print_context_event(env, event); 2379 break; 2380 default: 2381 break; 2382 } 2383 } 2384 2385 if (!visible_cnt) 2386 diag_write(env, " no retained diagnostic events on this path\n"); 2387 if (log->first_seq) 2388 diag_write(env, " %llu older causal-history event%s not retained because diagnostic " 2389 "event storage reached capacity\n", 2390 log->first_seq, log->first_seq == 1 ? "" : "s"); 2391 diag_fmt_restore(env, mark); 2392 } 2393