1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* Copyright (c) 2018 Facebook */ 3 4 #include <uapi/linux/btf.h> 5 #include <uapi/linux/bpf.h> 6 #include <uapi/linux/bpf_perf_event.h> 7 #include <uapi/linux/types.h> 8 #include <linux/seq_file.h> 9 #include <linux/compiler.h> 10 #include <linux/ctype.h> 11 #include <linux/errno.h> 12 #include <linux/slab.h> 13 #include <linux/anon_inodes.h> 14 #include <linux/file.h> 15 #include <linux/uaccess.h> 16 #include <linux/kernel.h> 17 #include <linux/idr.h> 18 #include <linux/sort.h> 19 #include <linux/bpf_verifier.h> 20 #include <linux/btf.h> 21 #include <linux/skmsg.h> 22 #include <linux/perf_event.h> 23 #include <net/sock.h> 24 25 /* BTF (BPF Type Format) is the meta data format which describes 26 * the data types of BPF program/map. Hence, it basically focus 27 * on the C programming language which the modern BPF is primary 28 * using. 29 * 30 * ELF Section: 31 * ~~~~~~~~~~~ 32 * The BTF data is stored under the ".BTF" ELF section 33 * 34 * struct btf_type: 35 * ~~~~~~~~~~~~~~~ 36 * Each 'struct btf_type' object describes a C data type. 37 * Depending on the type it is describing, a 'struct btf_type' 38 * object may be followed by more data. F.e. 39 * To describe an array, 'struct btf_type' is followed by 40 * 'struct btf_array'. 41 * 42 * 'struct btf_type' and any extra data following it are 43 * 4 bytes aligned. 44 * 45 * Type section: 46 * ~~~~~~~~~~~~~ 47 * The BTF type section contains a list of 'struct btf_type' objects. 48 * Each one describes a C type. Recall from the above section 49 * that a 'struct btf_type' object could be immediately followed by extra 50 * data in order to desribe some particular C types. 51 * 52 * type_id: 53 * ~~~~~~~ 54 * Each btf_type object is identified by a type_id. The type_id 55 * is implicitly implied by the location of the btf_type object in 56 * the BTF type section. The first one has type_id 1. The second 57 * one has type_id 2...etc. Hence, an earlier btf_type has 58 * a smaller type_id. 59 * 60 * A btf_type object may refer to another btf_type object by using 61 * type_id (i.e. the "type" in the "struct btf_type"). 62 * 63 * NOTE that we cannot assume any reference-order. 64 * A btf_type object can refer to an earlier btf_type object 65 * but it can also refer to a later btf_type object. 66 * 67 * For example, to describe "const void *". A btf_type 68 * object describing "const" may refer to another btf_type 69 * object describing "void *". This type-reference is done 70 * by specifying type_id: 71 * 72 * [1] CONST (anon) type_id=2 73 * [2] PTR (anon) type_id=0 74 * 75 * The above is the btf_verifier debug log: 76 * - Each line started with "[?]" is a btf_type object 77 * - [?] is the type_id of the btf_type object. 78 * - CONST/PTR is the BTF_KIND_XXX 79 * - "(anon)" is the name of the type. It just 80 * happens that CONST and PTR has no name. 81 * - type_id=XXX is the 'u32 type' in btf_type 82 * 83 * NOTE: "void" has type_id 0 84 * 85 * String section: 86 * ~~~~~~~~~~~~~~ 87 * The BTF string section contains the names used by the type section. 88 * Each string is referred by an "offset" from the beginning of the 89 * string section. 90 * 91 * Each string is '\0' terminated. 92 * 93 * The first character in the string section must be '\0' 94 * which is used to mean 'anonymous'. Some btf_type may not 95 * have a name. 96 */ 97 98 /* BTF verification: 99 * 100 * To verify BTF data, two passes are needed. 101 * 102 * Pass #1 103 * ~~~~~~~ 104 * The first pass is to collect all btf_type objects to 105 * an array: "btf->types". 106 * 107 * Depending on the C type that a btf_type is describing, 108 * a btf_type may be followed by extra data. We don't know 109 * how many btf_type is there, and more importantly we don't 110 * know where each btf_type is located in the type section. 111 * 112 * Without knowing the location of each type_id, most verifications 113 * cannot be done. e.g. an earlier btf_type may refer to a later 114 * btf_type (recall the "const void *" above), so we cannot 115 * check this type-reference in the first pass. 116 * 117 * In the first pass, it still does some verifications (e.g. 118 * checking the name is a valid offset to the string section). 119 * 120 * Pass #2 121 * ~~~~~~~ 122 * The main focus is to resolve a btf_type that is referring 123 * to another type. 124 * 125 * We have to ensure the referring type: 126 * 1) does exist in the BTF (i.e. in btf->types[]) 127 * 2) does not cause a loop: 128 * struct A { 129 * struct B b; 130 * }; 131 * 132 * struct B { 133 * struct A a; 134 * }; 135 * 136 * btf_type_needs_resolve() decides if a btf_type needs 137 * to be resolved. 138 * 139 * The needs_resolve type implements the "resolve()" ops which 140 * essentially does a DFS and detects backedge. 141 * 142 * During resolve (or DFS), different C types have different 143 * "RESOLVED" conditions. 144 * 145 * When resolving a BTF_KIND_STRUCT, we need to resolve all its 146 * members because a member is always referring to another 147 * type. A struct's member can be treated as "RESOLVED" if 148 * it is referring to a BTF_KIND_PTR. Otherwise, the 149 * following valid C struct would be rejected: 150 * 151 * struct A { 152 * int m; 153 * struct A *a; 154 * }; 155 * 156 * When resolving a BTF_KIND_PTR, it needs to keep resolving if 157 * it is referring to another BTF_KIND_PTR. Otherwise, we cannot 158 * detect a pointer loop, e.g.: 159 * BTF_KIND_CONST -> BTF_KIND_PTR -> BTF_KIND_CONST -> BTF_KIND_PTR + 160 * ^ | 161 * +-----------------------------------------+ 162 * 163 */ 164 165 #define BITS_PER_U128 (sizeof(u64) * BITS_PER_BYTE * 2) 166 #define BITS_PER_BYTE_MASK (BITS_PER_BYTE - 1) 167 #define BITS_PER_BYTE_MASKED(bits) ((bits) & BITS_PER_BYTE_MASK) 168 #define BITS_ROUNDDOWN_BYTES(bits) ((bits) >> 3) 169 #define BITS_ROUNDUP_BYTES(bits) \ 170 (BITS_ROUNDDOWN_BYTES(bits) + !!BITS_PER_BYTE_MASKED(bits)) 171 172 #define BTF_INFO_MASK 0x8f00ffff 173 #define BTF_INT_MASK 0x0fffffff 174 #define BTF_TYPE_ID_VALID(type_id) ((type_id) <= BTF_MAX_TYPE) 175 #define BTF_STR_OFFSET_VALID(name_off) ((name_off) <= BTF_MAX_NAME_OFFSET) 176 177 /* 16MB for 64k structs and each has 16 members and 178 * a few MB spaces for the string section. 179 * The hard limit is S32_MAX. 180 */ 181 #define BTF_MAX_SIZE (16 * 1024 * 1024) 182 183 #define for_each_member_from(i, from, struct_type, member) \ 184 for (i = from, member = btf_type_member(struct_type) + from; \ 185 i < btf_type_vlen(struct_type); \ 186 i++, member++) 187 188 #define for_each_vsi(i, struct_type, member) \ 189 for (i = 0, member = btf_type_var_secinfo(struct_type); \ 190 i < btf_type_vlen(struct_type); \ 191 i++, member++) 192 193 #define for_each_vsi_from(i, from, struct_type, member) \ 194 for (i = from, member = btf_type_var_secinfo(struct_type) + from; \ 195 i < btf_type_vlen(struct_type); \ 196 i++, member++) 197 198 DEFINE_IDR(btf_idr); 199 DEFINE_SPINLOCK(btf_idr_lock); 200 201 struct btf { 202 void *data; 203 struct btf_type **types; 204 u32 *resolved_ids; 205 u32 *resolved_sizes; 206 const char *strings; 207 void *nohdr_data; 208 struct btf_header hdr; 209 u32 nr_types; 210 u32 types_size; 211 u32 data_size; 212 refcount_t refcnt; 213 u32 id; 214 struct rcu_head rcu; 215 }; 216 217 enum verifier_phase { 218 CHECK_META, 219 CHECK_TYPE, 220 }; 221 222 struct resolve_vertex { 223 const struct btf_type *t; 224 u32 type_id; 225 u16 next_member; 226 }; 227 228 enum visit_state { 229 NOT_VISITED, 230 VISITED, 231 RESOLVED, 232 }; 233 234 enum resolve_mode { 235 RESOLVE_TBD, /* To Be Determined */ 236 RESOLVE_PTR, /* Resolving for Pointer */ 237 RESOLVE_STRUCT_OR_ARRAY, /* Resolving for struct/union 238 * or array 239 */ 240 }; 241 242 #define MAX_RESOLVE_DEPTH 32 243 244 struct btf_sec_info { 245 u32 off; 246 u32 len; 247 }; 248 249 struct btf_verifier_env { 250 struct btf *btf; 251 u8 *visit_states; 252 struct resolve_vertex stack[MAX_RESOLVE_DEPTH]; 253 struct bpf_verifier_log log; 254 u32 log_type_id; 255 u32 top_stack; 256 enum verifier_phase phase; 257 enum resolve_mode resolve_mode; 258 }; 259 260 static const char * const btf_kind_str[NR_BTF_KINDS] = { 261 [BTF_KIND_UNKN] = "UNKNOWN", 262 [BTF_KIND_INT] = "INT", 263 [BTF_KIND_PTR] = "PTR", 264 [BTF_KIND_ARRAY] = "ARRAY", 265 [BTF_KIND_STRUCT] = "STRUCT", 266 [BTF_KIND_UNION] = "UNION", 267 [BTF_KIND_ENUM] = "ENUM", 268 [BTF_KIND_FWD] = "FWD", 269 [BTF_KIND_TYPEDEF] = "TYPEDEF", 270 [BTF_KIND_VOLATILE] = "VOLATILE", 271 [BTF_KIND_CONST] = "CONST", 272 [BTF_KIND_RESTRICT] = "RESTRICT", 273 [BTF_KIND_FUNC] = "FUNC", 274 [BTF_KIND_FUNC_PROTO] = "FUNC_PROTO", 275 [BTF_KIND_VAR] = "VAR", 276 [BTF_KIND_DATASEC] = "DATASEC", 277 }; 278 279 struct btf_kind_operations { 280 s32 (*check_meta)(struct btf_verifier_env *env, 281 const struct btf_type *t, 282 u32 meta_left); 283 int (*resolve)(struct btf_verifier_env *env, 284 const struct resolve_vertex *v); 285 int (*check_member)(struct btf_verifier_env *env, 286 const struct btf_type *struct_type, 287 const struct btf_member *member, 288 const struct btf_type *member_type); 289 int (*check_kflag_member)(struct btf_verifier_env *env, 290 const struct btf_type *struct_type, 291 const struct btf_member *member, 292 const struct btf_type *member_type); 293 void (*log_details)(struct btf_verifier_env *env, 294 const struct btf_type *t); 295 void (*seq_show)(const struct btf *btf, const struct btf_type *t, 296 u32 type_id, void *data, u8 bits_offsets, 297 struct seq_file *m); 298 }; 299 300 static const struct btf_kind_operations * const kind_ops[NR_BTF_KINDS]; 301 static struct btf_type btf_void; 302 303 static int btf_resolve(struct btf_verifier_env *env, 304 const struct btf_type *t, u32 type_id); 305 306 static bool btf_type_is_modifier(const struct btf_type *t) 307 { 308 /* Some of them is not strictly a C modifier 309 * but they are grouped into the same bucket 310 * for BTF concern: 311 * A type (t) that refers to another 312 * type through t->type AND its size cannot 313 * be determined without following the t->type. 314 * 315 * ptr does not fall into this bucket 316 * because its size is always sizeof(void *). 317 */ 318 switch (BTF_INFO_KIND(t->info)) { 319 case BTF_KIND_TYPEDEF: 320 case BTF_KIND_VOLATILE: 321 case BTF_KIND_CONST: 322 case BTF_KIND_RESTRICT: 323 return true; 324 } 325 326 return false; 327 } 328 329 bool btf_type_is_void(const struct btf_type *t) 330 { 331 return t == &btf_void; 332 } 333 334 static bool btf_type_is_fwd(const struct btf_type *t) 335 { 336 return BTF_INFO_KIND(t->info) == BTF_KIND_FWD; 337 } 338 339 static bool btf_type_nosize(const struct btf_type *t) 340 { 341 return btf_type_is_void(t) || btf_type_is_fwd(t) || 342 btf_type_is_func(t) || btf_type_is_func_proto(t); 343 } 344 345 static bool btf_type_nosize_or_null(const struct btf_type *t) 346 { 347 return !t || btf_type_nosize(t); 348 } 349 350 /* union is only a special case of struct: 351 * all its offsetof(member) == 0 352 */ 353 static bool btf_type_is_struct(const struct btf_type *t) 354 { 355 u8 kind = BTF_INFO_KIND(t->info); 356 357 return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION; 358 } 359 360 static bool __btf_type_is_struct(const struct btf_type *t) 361 { 362 return BTF_INFO_KIND(t->info) == BTF_KIND_STRUCT; 363 } 364 365 static bool btf_type_is_array(const struct btf_type *t) 366 { 367 return BTF_INFO_KIND(t->info) == BTF_KIND_ARRAY; 368 } 369 370 static bool btf_type_is_var(const struct btf_type *t) 371 { 372 return BTF_INFO_KIND(t->info) == BTF_KIND_VAR; 373 } 374 375 static bool btf_type_is_datasec(const struct btf_type *t) 376 { 377 return BTF_INFO_KIND(t->info) == BTF_KIND_DATASEC; 378 } 379 380 s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind) 381 { 382 const struct btf_type *t; 383 const char *tname; 384 u32 i; 385 386 for (i = 1; i <= btf->nr_types; i++) { 387 t = btf->types[i]; 388 if (BTF_INFO_KIND(t->info) != kind) 389 continue; 390 391 tname = btf_name_by_offset(btf, t->name_off); 392 if (!strcmp(tname, name)) 393 return i; 394 } 395 396 return -ENOENT; 397 } 398 399 const struct btf_type *btf_type_skip_modifiers(const struct btf *btf, 400 u32 id, u32 *res_id) 401 { 402 const struct btf_type *t = btf_type_by_id(btf, id); 403 404 while (btf_type_is_modifier(t)) { 405 id = t->type; 406 t = btf_type_by_id(btf, t->type); 407 } 408 409 if (res_id) 410 *res_id = id; 411 412 return t; 413 } 414 415 const struct btf_type *btf_type_resolve_ptr(const struct btf *btf, 416 u32 id, u32 *res_id) 417 { 418 const struct btf_type *t; 419 420 t = btf_type_skip_modifiers(btf, id, NULL); 421 if (!btf_type_is_ptr(t)) 422 return NULL; 423 424 return btf_type_skip_modifiers(btf, t->type, res_id); 425 } 426 427 const struct btf_type *btf_type_resolve_func_ptr(const struct btf *btf, 428 u32 id, u32 *res_id) 429 { 430 const struct btf_type *ptype; 431 432 ptype = btf_type_resolve_ptr(btf, id, res_id); 433 if (ptype && btf_type_is_func_proto(ptype)) 434 return ptype; 435 436 return NULL; 437 } 438 439 /* Types that act only as a source, not sink or intermediate 440 * type when resolving. 441 */ 442 static bool btf_type_is_resolve_source_only(const struct btf_type *t) 443 { 444 return btf_type_is_var(t) || 445 btf_type_is_datasec(t); 446 } 447 448 /* What types need to be resolved? 449 * 450 * btf_type_is_modifier() is an obvious one. 451 * 452 * btf_type_is_struct() because its member refers to 453 * another type (through member->type). 454 * 455 * btf_type_is_var() because the variable refers to 456 * another type. btf_type_is_datasec() holds multiple 457 * btf_type_is_var() types that need resolving. 458 * 459 * btf_type_is_array() because its element (array->type) 460 * refers to another type. Array can be thought of a 461 * special case of struct while array just has the same 462 * member-type repeated by array->nelems of times. 463 */ 464 static bool btf_type_needs_resolve(const struct btf_type *t) 465 { 466 return btf_type_is_modifier(t) || 467 btf_type_is_ptr(t) || 468 btf_type_is_struct(t) || 469 btf_type_is_array(t) || 470 btf_type_is_var(t) || 471 btf_type_is_datasec(t); 472 } 473 474 /* t->size can be used */ 475 static bool btf_type_has_size(const struct btf_type *t) 476 { 477 switch (BTF_INFO_KIND(t->info)) { 478 case BTF_KIND_INT: 479 case BTF_KIND_STRUCT: 480 case BTF_KIND_UNION: 481 case BTF_KIND_ENUM: 482 case BTF_KIND_DATASEC: 483 return true; 484 } 485 486 return false; 487 } 488 489 static const char *btf_int_encoding_str(u8 encoding) 490 { 491 if (encoding == 0) 492 return "(none)"; 493 else if (encoding == BTF_INT_SIGNED) 494 return "SIGNED"; 495 else if (encoding == BTF_INT_CHAR) 496 return "CHAR"; 497 else if (encoding == BTF_INT_BOOL) 498 return "BOOL"; 499 else 500 return "UNKN"; 501 } 502 503 static u32 btf_type_int(const struct btf_type *t) 504 { 505 return *(u32 *)(t + 1); 506 } 507 508 static const struct btf_array *btf_type_array(const struct btf_type *t) 509 { 510 return (const struct btf_array *)(t + 1); 511 } 512 513 static const struct btf_enum *btf_type_enum(const struct btf_type *t) 514 { 515 return (const struct btf_enum *)(t + 1); 516 } 517 518 static const struct btf_var *btf_type_var(const struct btf_type *t) 519 { 520 return (const struct btf_var *)(t + 1); 521 } 522 523 static const struct btf_var_secinfo *btf_type_var_secinfo(const struct btf_type *t) 524 { 525 return (const struct btf_var_secinfo *)(t + 1); 526 } 527 528 static const struct btf_kind_operations *btf_type_ops(const struct btf_type *t) 529 { 530 return kind_ops[BTF_INFO_KIND(t->info)]; 531 } 532 533 static bool btf_name_offset_valid(const struct btf *btf, u32 offset) 534 { 535 return BTF_STR_OFFSET_VALID(offset) && 536 offset < btf->hdr.str_len; 537 } 538 539 static bool __btf_name_char_ok(char c, bool first, bool dot_ok) 540 { 541 if ((first ? !isalpha(c) : 542 !isalnum(c)) && 543 c != '_' && 544 ((c == '.' && !dot_ok) || 545 c != '.')) 546 return false; 547 return true; 548 } 549 550 static bool __btf_name_valid(const struct btf *btf, u32 offset, bool dot_ok) 551 { 552 /* offset must be valid */ 553 const char *src = &btf->strings[offset]; 554 const char *src_limit; 555 556 if (!__btf_name_char_ok(*src, true, dot_ok)) 557 return false; 558 559 /* set a limit on identifier length */ 560 src_limit = src + KSYM_NAME_LEN; 561 src++; 562 while (*src && src < src_limit) { 563 if (!__btf_name_char_ok(*src, false, dot_ok)) 564 return false; 565 src++; 566 } 567 568 return !*src; 569 } 570 571 /* Only C-style identifier is permitted. This can be relaxed if 572 * necessary. 573 */ 574 static bool btf_name_valid_identifier(const struct btf *btf, u32 offset) 575 { 576 return __btf_name_valid(btf, offset, false); 577 } 578 579 static bool btf_name_valid_section(const struct btf *btf, u32 offset) 580 { 581 return __btf_name_valid(btf, offset, true); 582 } 583 584 static const char *__btf_name_by_offset(const struct btf *btf, u32 offset) 585 { 586 if (!offset) 587 return "(anon)"; 588 else if (offset < btf->hdr.str_len) 589 return &btf->strings[offset]; 590 else 591 return "(invalid-name-offset)"; 592 } 593 594 const char *btf_name_by_offset(const struct btf *btf, u32 offset) 595 { 596 if (offset < btf->hdr.str_len) 597 return &btf->strings[offset]; 598 599 return NULL; 600 } 601 602 const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id) 603 { 604 if (type_id > btf->nr_types) 605 return NULL; 606 607 return btf->types[type_id]; 608 } 609 610 /* 611 * Regular int is not a bit field and it must be either 612 * u8/u16/u32/u64 or __int128. 613 */ 614 static bool btf_type_int_is_regular(const struct btf_type *t) 615 { 616 u8 nr_bits, nr_bytes; 617 u32 int_data; 618 619 int_data = btf_type_int(t); 620 nr_bits = BTF_INT_BITS(int_data); 621 nr_bytes = BITS_ROUNDUP_BYTES(nr_bits); 622 if (BITS_PER_BYTE_MASKED(nr_bits) || 623 BTF_INT_OFFSET(int_data) || 624 (nr_bytes != sizeof(u8) && nr_bytes != sizeof(u16) && 625 nr_bytes != sizeof(u32) && nr_bytes != sizeof(u64) && 626 nr_bytes != (2 * sizeof(u64)))) { 627 return false; 628 } 629 630 return true; 631 } 632 633 /* 634 * Check that given struct member is a regular int with expected 635 * offset and size. 636 */ 637 bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s, 638 const struct btf_member *m, 639 u32 expected_offset, u32 expected_size) 640 { 641 const struct btf_type *t; 642 u32 id, int_data; 643 u8 nr_bits; 644 645 id = m->type; 646 t = btf_type_id_size(btf, &id, NULL); 647 if (!t || !btf_type_is_int(t)) 648 return false; 649 650 int_data = btf_type_int(t); 651 nr_bits = BTF_INT_BITS(int_data); 652 if (btf_type_kflag(s)) { 653 u32 bitfield_size = BTF_MEMBER_BITFIELD_SIZE(m->offset); 654 u32 bit_offset = BTF_MEMBER_BIT_OFFSET(m->offset); 655 656 /* if kflag set, int should be a regular int and 657 * bit offset should be at byte boundary. 658 */ 659 return !bitfield_size && 660 BITS_ROUNDUP_BYTES(bit_offset) == expected_offset && 661 BITS_ROUNDUP_BYTES(nr_bits) == expected_size; 662 } 663 664 if (BTF_INT_OFFSET(int_data) || 665 BITS_PER_BYTE_MASKED(m->offset) || 666 BITS_ROUNDUP_BYTES(m->offset) != expected_offset || 667 BITS_PER_BYTE_MASKED(nr_bits) || 668 BITS_ROUNDUP_BYTES(nr_bits) != expected_size) 669 return false; 670 671 return true; 672 } 673 674 __printf(2, 3) static void __btf_verifier_log(struct bpf_verifier_log *log, 675 const char *fmt, ...) 676 { 677 va_list args; 678 679 va_start(args, fmt); 680 bpf_verifier_vlog(log, fmt, args); 681 va_end(args); 682 } 683 684 __printf(2, 3) static void btf_verifier_log(struct btf_verifier_env *env, 685 const char *fmt, ...) 686 { 687 struct bpf_verifier_log *log = &env->log; 688 va_list args; 689 690 if (!bpf_verifier_log_needed(log)) 691 return; 692 693 va_start(args, fmt); 694 bpf_verifier_vlog(log, fmt, args); 695 va_end(args); 696 } 697 698 __printf(4, 5) static void __btf_verifier_log_type(struct btf_verifier_env *env, 699 const struct btf_type *t, 700 bool log_details, 701 const char *fmt, ...) 702 { 703 struct bpf_verifier_log *log = &env->log; 704 u8 kind = BTF_INFO_KIND(t->info); 705 struct btf *btf = env->btf; 706 va_list args; 707 708 if (!bpf_verifier_log_needed(log)) 709 return; 710 711 /* btf verifier prints all types it is processing via 712 * btf_verifier_log_type(..., fmt = NULL). 713 * Skip those prints for in-kernel BTF verification. 714 */ 715 if (log->level == BPF_LOG_KERNEL && !fmt) 716 return; 717 718 __btf_verifier_log(log, "[%u] %s %s%s", 719 env->log_type_id, 720 btf_kind_str[kind], 721 __btf_name_by_offset(btf, t->name_off), 722 log_details ? " " : ""); 723 724 if (log_details) 725 btf_type_ops(t)->log_details(env, t); 726 727 if (fmt && *fmt) { 728 __btf_verifier_log(log, " "); 729 va_start(args, fmt); 730 bpf_verifier_vlog(log, fmt, args); 731 va_end(args); 732 } 733 734 __btf_verifier_log(log, "\n"); 735 } 736 737 #define btf_verifier_log_type(env, t, ...) \ 738 __btf_verifier_log_type((env), (t), true, __VA_ARGS__) 739 #define btf_verifier_log_basic(env, t, ...) \ 740 __btf_verifier_log_type((env), (t), false, __VA_ARGS__) 741 742 __printf(4, 5) 743 static void btf_verifier_log_member(struct btf_verifier_env *env, 744 const struct btf_type *struct_type, 745 const struct btf_member *member, 746 const char *fmt, ...) 747 { 748 struct bpf_verifier_log *log = &env->log; 749 struct btf *btf = env->btf; 750 va_list args; 751 752 if (!bpf_verifier_log_needed(log)) 753 return; 754 755 if (log->level == BPF_LOG_KERNEL && !fmt) 756 return; 757 /* The CHECK_META phase already did a btf dump. 758 * 759 * If member is logged again, it must hit an error in 760 * parsing this member. It is useful to print out which 761 * struct this member belongs to. 762 */ 763 if (env->phase != CHECK_META) 764 btf_verifier_log_type(env, struct_type, NULL); 765 766 if (btf_type_kflag(struct_type)) 767 __btf_verifier_log(log, 768 "\t%s type_id=%u bitfield_size=%u bits_offset=%u", 769 __btf_name_by_offset(btf, member->name_off), 770 member->type, 771 BTF_MEMBER_BITFIELD_SIZE(member->offset), 772 BTF_MEMBER_BIT_OFFSET(member->offset)); 773 else 774 __btf_verifier_log(log, "\t%s type_id=%u bits_offset=%u", 775 __btf_name_by_offset(btf, member->name_off), 776 member->type, member->offset); 777 778 if (fmt && *fmt) { 779 __btf_verifier_log(log, " "); 780 va_start(args, fmt); 781 bpf_verifier_vlog(log, fmt, args); 782 va_end(args); 783 } 784 785 __btf_verifier_log(log, "\n"); 786 } 787 788 __printf(4, 5) 789 static void btf_verifier_log_vsi(struct btf_verifier_env *env, 790 const struct btf_type *datasec_type, 791 const struct btf_var_secinfo *vsi, 792 const char *fmt, ...) 793 { 794 struct bpf_verifier_log *log = &env->log; 795 va_list args; 796 797 if (!bpf_verifier_log_needed(log)) 798 return; 799 if (log->level == BPF_LOG_KERNEL && !fmt) 800 return; 801 if (env->phase != CHECK_META) 802 btf_verifier_log_type(env, datasec_type, NULL); 803 804 __btf_verifier_log(log, "\t type_id=%u offset=%u size=%u", 805 vsi->type, vsi->offset, vsi->size); 806 if (fmt && *fmt) { 807 __btf_verifier_log(log, " "); 808 va_start(args, fmt); 809 bpf_verifier_vlog(log, fmt, args); 810 va_end(args); 811 } 812 813 __btf_verifier_log(log, "\n"); 814 } 815 816 static void btf_verifier_log_hdr(struct btf_verifier_env *env, 817 u32 btf_data_size) 818 { 819 struct bpf_verifier_log *log = &env->log; 820 const struct btf *btf = env->btf; 821 const struct btf_header *hdr; 822 823 if (!bpf_verifier_log_needed(log)) 824 return; 825 826 if (log->level == BPF_LOG_KERNEL) 827 return; 828 hdr = &btf->hdr; 829 __btf_verifier_log(log, "magic: 0x%x\n", hdr->magic); 830 __btf_verifier_log(log, "version: %u\n", hdr->version); 831 __btf_verifier_log(log, "flags: 0x%x\n", hdr->flags); 832 __btf_verifier_log(log, "hdr_len: %u\n", hdr->hdr_len); 833 __btf_verifier_log(log, "type_off: %u\n", hdr->type_off); 834 __btf_verifier_log(log, "type_len: %u\n", hdr->type_len); 835 __btf_verifier_log(log, "str_off: %u\n", hdr->str_off); 836 __btf_verifier_log(log, "str_len: %u\n", hdr->str_len); 837 __btf_verifier_log(log, "btf_total_size: %u\n", btf_data_size); 838 } 839 840 static int btf_add_type(struct btf_verifier_env *env, struct btf_type *t) 841 { 842 struct btf *btf = env->btf; 843 844 /* < 2 because +1 for btf_void which is always in btf->types[0]. 845 * btf_void is not accounted in btf->nr_types because btf_void 846 * does not come from the BTF file. 847 */ 848 if (btf->types_size - btf->nr_types < 2) { 849 /* Expand 'types' array */ 850 851 struct btf_type **new_types; 852 u32 expand_by, new_size; 853 854 if (btf->types_size == BTF_MAX_TYPE) { 855 btf_verifier_log(env, "Exceeded max num of types"); 856 return -E2BIG; 857 } 858 859 expand_by = max_t(u32, btf->types_size >> 2, 16); 860 new_size = min_t(u32, BTF_MAX_TYPE, 861 btf->types_size + expand_by); 862 863 new_types = kvcalloc(new_size, sizeof(*new_types), 864 GFP_KERNEL | __GFP_NOWARN); 865 if (!new_types) 866 return -ENOMEM; 867 868 if (btf->nr_types == 0) 869 new_types[0] = &btf_void; 870 else 871 memcpy(new_types, btf->types, 872 sizeof(*btf->types) * (btf->nr_types + 1)); 873 874 kvfree(btf->types); 875 btf->types = new_types; 876 btf->types_size = new_size; 877 } 878 879 btf->types[++(btf->nr_types)] = t; 880 881 return 0; 882 } 883 884 static int btf_alloc_id(struct btf *btf) 885 { 886 int id; 887 888 idr_preload(GFP_KERNEL); 889 spin_lock_bh(&btf_idr_lock); 890 id = idr_alloc_cyclic(&btf_idr, btf, 1, INT_MAX, GFP_ATOMIC); 891 if (id > 0) 892 btf->id = id; 893 spin_unlock_bh(&btf_idr_lock); 894 idr_preload_end(); 895 896 if (WARN_ON_ONCE(!id)) 897 return -ENOSPC; 898 899 return id > 0 ? 0 : id; 900 } 901 902 static void btf_free_id(struct btf *btf) 903 { 904 unsigned long flags; 905 906 /* 907 * In map-in-map, calling map_delete_elem() on outer 908 * map will call bpf_map_put on the inner map. 909 * It will then eventually call btf_free_id() 910 * on the inner map. Some of the map_delete_elem() 911 * implementation may have irq disabled, so 912 * we need to use the _irqsave() version instead 913 * of the _bh() version. 914 */ 915 spin_lock_irqsave(&btf_idr_lock, flags); 916 idr_remove(&btf_idr, btf->id); 917 spin_unlock_irqrestore(&btf_idr_lock, flags); 918 } 919 920 static void btf_free(struct btf *btf) 921 { 922 kvfree(btf->types); 923 kvfree(btf->resolved_sizes); 924 kvfree(btf->resolved_ids); 925 kvfree(btf->data); 926 kfree(btf); 927 } 928 929 static void btf_free_rcu(struct rcu_head *rcu) 930 { 931 struct btf *btf = container_of(rcu, struct btf, rcu); 932 933 btf_free(btf); 934 } 935 936 void btf_put(struct btf *btf) 937 { 938 if (btf && refcount_dec_and_test(&btf->refcnt)) { 939 btf_free_id(btf); 940 call_rcu(&btf->rcu, btf_free_rcu); 941 } 942 } 943 944 static int env_resolve_init(struct btf_verifier_env *env) 945 { 946 struct btf *btf = env->btf; 947 u32 nr_types = btf->nr_types; 948 u32 *resolved_sizes = NULL; 949 u32 *resolved_ids = NULL; 950 u8 *visit_states = NULL; 951 952 /* +1 for btf_void */ 953 resolved_sizes = kvcalloc(nr_types + 1, sizeof(*resolved_sizes), 954 GFP_KERNEL | __GFP_NOWARN); 955 if (!resolved_sizes) 956 goto nomem; 957 958 resolved_ids = kvcalloc(nr_types + 1, sizeof(*resolved_ids), 959 GFP_KERNEL | __GFP_NOWARN); 960 if (!resolved_ids) 961 goto nomem; 962 963 visit_states = kvcalloc(nr_types + 1, sizeof(*visit_states), 964 GFP_KERNEL | __GFP_NOWARN); 965 if (!visit_states) 966 goto nomem; 967 968 btf->resolved_sizes = resolved_sizes; 969 btf->resolved_ids = resolved_ids; 970 env->visit_states = visit_states; 971 972 return 0; 973 974 nomem: 975 kvfree(resolved_sizes); 976 kvfree(resolved_ids); 977 kvfree(visit_states); 978 return -ENOMEM; 979 } 980 981 static void btf_verifier_env_free(struct btf_verifier_env *env) 982 { 983 kvfree(env->visit_states); 984 kfree(env); 985 } 986 987 static bool env_type_is_resolve_sink(const struct btf_verifier_env *env, 988 const struct btf_type *next_type) 989 { 990 switch (env->resolve_mode) { 991 case RESOLVE_TBD: 992 /* int, enum or void is a sink */ 993 return !btf_type_needs_resolve(next_type); 994 case RESOLVE_PTR: 995 /* int, enum, void, struct, array, func or func_proto is a sink 996 * for ptr 997 */ 998 return !btf_type_is_modifier(next_type) && 999 !btf_type_is_ptr(next_type); 1000 case RESOLVE_STRUCT_OR_ARRAY: 1001 /* int, enum, void, ptr, func or func_proto is a sink 1002 * for struct and array 1003 */ 1004 return !btf_type_is_modifier(next_type) && 1005 !btf_type_is_array(next_type) && 1006 !btf_type_is_struct(next_type); 1007 default: 1008 BUG(); 1009 } 1010 } 1011 1012 static bool env_type_is_resolved(const struct btf_verifier_env *env, 1013 u32 type_id) 1014 { 1015 return env->visit_states[type_id] == RESOLVED; 1016 } 1017 1018 static int env_stack_push(struct btf_verifier_env *env, 1019 const struct btf_type *t, u32 type_id) 1020 { 1021 struct resolve_vertex *v; 1022 1023 if (env->top_stack == MAX_RESOLVE_DEPTH) 1024 return -E2BIG; 1025 1026 if (env->visit_states[type_id] != NOT_VISITED) 1027 return -EEXIST; 1028 1029 env->visit_states[type_id] = VISITED; 1030 1031 v = &env->stack[env->top_stack++]; 1032 v->t = t; 1033 v->type_id = type_id; 1034 v->next_member = 0; 1035 1036 if (env->resolve_mode == RESOLVE_TBD) { 1037 if (btf_type_is_ptr(t)) 1038 env->resolve_mode = RESOLVE_PTR; 1039 else if (btf_type_is_struct(t) || btf_type_is_array(t)) 1040 env->resolve_mode = RESOLVE_STRUCT_OR_ARRAY; 1041 } 1042 1043 return 0; 1044 } 1045 1046 static void env_stack_set_next_member(struct btf_verifier_env *env, 1047 u16 next_member) 1048 { 1049 env->stack[env->top_stack - 1].next_member = next_member; 1050 } 1051 1052 static void env_stack_pop_resolved(struct btf_verifier_env *env, 1053 u32 resolved_type_id, 1054 u32 resolved_size) 1055 { 1056 u32 type_id = env->stack[--(env->top_stack)].type_id; 1057 struct btf *btf = env->btf; 1058 1059 btf->resolved_sizes[type_id] = resolved_size; 1060 btf->resolved_ids[type_id] = resolved_type_id; 1061 env->visit_states[type_id] = RESOLVED; 1062 } 1063 1064 static const struct resolve_vertex *env_stack_peak(struct btf_verifier_env *env) 1065 { 1066 return env->top_stack ? &env->stack[env->top_stack - 1] : NULL; 1067 } 1068 1069 /* Resolve the size of a passed-in "type" 1070 * 1071 * type: is an array (e.g. u32 array[x][y]) 1072 * return type: type "u32[x][y]", i.e. BTF_KIND_ARRAY, 1073 * *type_size: (x * y * sizeof(u32)). Hence, *type_size always 1074 * corresponds to the return type. 1075 * *elem_type: u32 1076 * *total_nelems: (x * y). Hence, individual elem size is 1077 * (*type_size / *total_nelems) 1078 * 1079 * type: is not an array (e.g. const struct X) 1080 * return type: type "struct X" 1081 * *type_size: sizeof(struct X) 1082 * *elem_type: same as return type ("struct X") 1083 * *total_nelems: 1 1084 */ 1085 const struct btf_type * 1086 btf_resolve_size(const struct btf *btf, const struct btf_type *type, 1087 u32 *type_size, const struct btf_type **elem_type, 1088 u32 *total_nelems) 1089 { 1090 const struct btf_type *array_type = NULL; 1091 const struct btf_array *array; 1092 u32 i, size, nelems = 1; 1093 1094 for (i = 0; i < MAX_RESOLVE_DEPTH; i++) { 1095 switch (BTF_INFO_KIND(type->info)) { 1096 /* type->size can be used */ 1097 case BTF_KIND_INT: 1098 case BTF_KIND_STRUCT: 1099 case BTF_KIND_UNION: 1100 case BTF_KIND_ENUM: 1101 size = type->size; 1102 goto resolved; 1103 1104 case BTF_KIND_PTR: 1105 size = sizeof(void *); 1106 goto resolved; 1107 1108 /* Modifiers */ 1109 case BTF_KIND_TYPEDEF: 1110 case BTF_KIND_VOLATILE: 1111 case BTF_KIND_CONST: 1112 case BTF_KIND_RESTRICT: 1113 type = btf_type_by_id(btf, type->type); 1114 break; 1115 1116 case BTF_KIND_ARRAY: 1117 if (!array_type) 1118 array_type = type; 1119 array = btf_type_array(type); 1120 if (nelems && array->nelems > U32_MAX / nelems) 1121 return ERR_PTR(-EINVAL); 1122 nelems *= array->nelems; 1123 type = btf_type_by_id(btf, array->type); 1124 break; 1125 1126 /* type without size */ 1127 default: 1128 return ERR_PTR(-EINVAL); 1129 } 1130 } 1131 1132 return ERR_PTR(-EINVAL); 1133 1134 resolved: 1135 if (nelems && size > U32_MAX / nelems) 1136 return ERR_PTR(-EINVAL); 1137 1138 *type_size = nelems * size; 1139 if (total_nelems) 1140 *total_nelems = nelems; 1141 if (elem_type) 1142 *elem_type = type; 1143 1144 return array_type ? : type; 1145 } 1146 1147 /* The input param "type_id" must point to a needs_resolve type */ 1148 static const struct btf_type *btf_type_id_resolve(const struct btf *btf, 1149 u32 *type_id) 1150 { 1151 *type_id = btf->resolved_ids[*type_id]; 1152 return btf_type_by_id(btf, *type_id); 1153 } 1154 1155 const struct btf_type *btf_type_id_size(const struct btf *btf, 1156 u32 *type_id, u32 *ret_size) 1157 { 1158 const struct btf_type *size_type; 1159 u32 size_type_id = *type_id; 1160 u32 size = 0; 1161 1162 size_type = btf_type_by_id(btf, size_type_id); 1163 if (btf_type_nosize_or_null(size_type)) 1164 return NULL; 1165 1166 if (btf_type_has_size(size_type)) { 1167 size = size_type->size; 1168 } else if (btf_type_is_array(size_type)) { 1169 size = btf->resolved_sizes[size_type_id]; 1170 } else if (btf_type_is_ptr(size_type)) { 1171 size = sizeof(void *); 1172 } else { 1173 if (WARN_ON_ONCE(!btf_type_is_modifier(size_type) && 1174 !btf_type_is_var(size_type))) 1175 return NULL; 1176 1177 size_type_id = btf->resolved_ids[size_type_id]; 1178 size_type = btf_type_by_id(btf, size_type_id); 1179 if (btf_type_nosize_or_null(size_type)) 1180 return NULL; 1181 else if (btf_type_has_size(size_type)) 1182 size = size_type->size; 1183 else if (btf_type_is_array(size_type)) 1184 size = btf->resolved_sizes[size_type_id]; 1185 else if (btf_type_is_ptr(size_type)) 1186 size = sizeof(void *); 1187 else 1188 return NULL; 1189 } 1190 1191 *type_id = size_type_id; 1192 if (ret_size) 1193 *ret_size = size; 1194 1195 return size_type; 1196 } 1197 1198 static int btf_df_check_member(struct btf_verifier_env *env, 1199 const struct btf_type *struct_type, 1200 const struct btf_member *member, 1201 const struct btf_type *member_type) 1202 { 1203 btf_verifier_log_basic(env, struct_type, 1204 "Unsupported check_member"); 1205 return -EINVAL; 1206 } 1207 1208 static int btf_df_check_kflag_member(struct btf_verifier_env *env, 1209 const struct btf_type *struct_type, 1210 const struct btf_member *member, 1211 const struct btf_type *member_type) 1212 { 1213 btf_verifier_log_basic(env, struct_type, 1214 "Unsupported check_kflag_member"); 1215 return -EINVAL; 1216 } 1217 1218 /* Used for ptr, array and struct/union type members. 1219 * int, enum and modifier types have their specific callback functions. 1220 */ 1221 static int btf_generic_check_kflag_member(struct btf_verifier_env *env, 1222 const struct btf_type *struct_type, 1223 const struct btf_member *member, 1224 const struct btf_type *member_type) 1225 { 1226 if (BTF_MEMBER_BITFIELD_SIZE(member->offset)) { 1227 btf_verifier_log_member(env, struct_type, member, 1228 "Invalid member bitfield_size"); 1229 return -EINVAL; 1230 } 1231 1232 /* bitfield size is 0, so member->offset represents bit offset only. 1233 * It is safe to call non kflag check_member variants. 1234 */ 1235 return btf_type_ops(member_type)->check_member(env, struct_type, 1236 member, 1237 member_type); 1238 } 1239 1240 static int btf_df_resolve(struct btf_verifier_env *env, 1241 const struct resolve_vertex *v) 1242 { 1243 btf_verifier_log_basic(env, v->t, "Unsupported resolve"); 1244 return -EINVAL; 1245 } 1246 1247 static void btf_df_seq_show(const struct btf *btf, const struct btf_type *t, 1248 u32 type_id, void *data, u8 bits_offsets, 1249 struct seq_file *m) 1250 { 1251 seq_printf(m, "<unsupported kind:%u>", BTF_INFO_KIND(t->info)); 1252 } 1253 1254 static int btf_int_check_member(struct btf_verifier_env *env, 1255 const struct btf_type *struct_type, 1256 const struct btf_member *member, 1257 const struct btf_type *member_type) 1258 { 1259 u32 int_data = btf_type_int(member_type); 1260 u32 struct_bits_off = member->offset; 1261 u32 struct_size = struct_type->size; 1262 u32 nr_copy_bits; 1263 u32 bytes_offset; 1264 1265 if (U32_MAX - struct_bits_off < BTF_INT_OFFSET(int_data)) { 1266 btf_verifier_log_member(env, struct_type, member, 1267 "bits_offset exceeds U32_MAX"); 1268 return -EINVAL; 1269 } 1270 1271 struct_bits_off += BTF_INT_OFFSET(int_data); 1272 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off); 1273 nr_copy_bits = BTF_INT_BITS(int_data) + 1274 BITS_PER_BYTE_MASKED(struct_bits_off); 1275 1276 if (nr_copy_bits > BITS_PER_U128) { 1277 btf_verifier_log_member(env, struct_type, member, 1278 "nr_copy_bits exceeds 128"); 1279 return -EINVAL; 1280 } 1281 1282 if (struct_size < bytes_offset || 1283 struct_size - bytes_offset < BITS_ROUNDUP_BYTES(nr_copy_bits)) { 1284 btf_verifier_log_member(env, struct_type, member, 1285 "Member exceeds struct_size"); 1286 return -EINVAL; 1287 } 1288 1289 return 0; 1290 } 1291 1292 static int btf_int_check_kflag_member(struct btf_verifier_env *env, 1293 const struct btf_type *struct_type, 1294 const struct btf_member *member, 1295 const struct btf_type *member_type) 1296 { 1297 u32 struct_bits_off, nr_bits, nr_int_data_bits, bytes_offset; 1298 u32 int_data = btf_type_int(member_type); 1299 u32 struct_size = struct_type->size; 1300 u32 nr_copy_bits; 1301 1302 /* a regular int type is required for the kflag int member */ 1303 if (!btf_type_int_is_regular(member_type)) { 1304 btf_verifier_log_member(env, struct_type, member, 1305 "Invalid member base type"); 1306 return -EINVAL; 1307 } 1308 1309 /* check sanity of bitfield size */ 1310 nr_bits = BTF_MEMBER_BITFIELD_SIZE(member->offset); 1311 struct_bits_off = BTF_MEMBER_BIT_OFFSET(member->offset); 1312 nr_int_data_bits = BTF_INT_BITS(int_data); 1313 if (!nr_bits) { 1314 /* Not a bitfield member, member offset must be at byte 1315 * boundary. 1316 */ 1317 if (BITS_PER_BYTE_MASKED(struct_bits_off)) { 1318 btf_verifier_log_member(env, struct_type, member, 1319 "Invalid member offset"); 1320 return -EINVAL; 1321 } 1322 1323 nr_bits = nr_int_data_bits; 1324 } else if (nr_bits > nr_int_data_bits) { 1325 btf_verifier_log_member(env, struct_type, member, 1326 "Invalid member bitfield_size"); 1327 return -EINVAL; 1328 } 1329 1330 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off); 1331 nr_copy_bits = nr_bits + BITS_PER_BYTE_MASKED(struct_bits_off); 1332 if (nr_copy_bits > BITS_PER_U128) { 1333 btf_verifier_log_member(env, struct_type, member, 1334 "nr_copy_bits exceeds 128"); 1335 return -EINVAL; 1336 } 1337 1338 if (struct_size < bytes_offset || 1339 struct_size - bytes_offset < BITS_ROUNDUP_BYTES(nr_copy_bits)) { 1340 btf_verifier_log_member(env, struct_type, member, 1341 "Member exceeds struct_size"); 1342 return -EINVAL; 1343 } 1344 1345 return 0; 1346 } 1347 1348 static s32 btf_int_check_meta(struct btf_verifier_env *env, 1349 const struct btf_type *t, 1350 u32 meta_left) 1351 { 1352 u32 int_data, nr_bits, meta_needed = sizeof(int_data); 1353 u16 encoding; 1354 1355 if (meta_left < meta_needed) { 1356 btf_verifier_log_basic(env, t, 1357 "meta_left:%u meta_needed:%u", 1358 meta_left, meta_needed); 1359 return -EINVAL; 1360 } 1361 1362 if (btf_type_vlen(t)) { 1363 btf_verifier_log_type(env, t, "vlen != 0"); 1364 return -EINVAL; 1365 } 1366 1367 if (btf_type_kflag(t)) { 1368 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); 1369 return -EINVAL; 1370 } 1371 1372 int_data = btf_type_int(t); 1373 if (int_data & ~BTF_INT_MASK) { 1374 btf_verifier_log_basic(env, t, "Invalid int_data:%x", 1375 int_data); 1376 return -EINVAL; 1377 } 1378 1379 nr_bits = BTF_INT_BITS(int_data) + BTF_INT_OFFSET(int_data); 1380 1381 if (nr_bits > BITS_PER_U128) { 1382 btf_verifier_log_type(env, t, "nr_bits exceeds %zu", 1383 BITS_PER_U128); 1384 return -EINVAL; 1385 } 1386 1387 if (BITS_ROUNDUP_BYTES(nr_bits) > t->size) { 1388 btf_verifier_log_type(env, t, "nr_bits exceeds type_size"); 1389 return -EINVAL; 1390 } 1391 1392 /* 1393 * Only one of the encoding bits is allowed and it 1394 * should be sufficient for the pretty print purpose (i.e. decoding). 1395 * Multiple bits can be allowed later if it is found 1396 * to be insufficient. 1397 */ 1398 encoding = BTF_INT_ENCODING(int_data); 1399 if (encoding && 1400 encoding != BTF_INT_SIGNED && 1401 encoding != BTF_INT_CHAR && 1402 encoding != BTF_INT_BOOL) { 1403 btf_verifier_log_type(env, t, "Unsupported encoding"); 1404 return -ENOTSUPP; 1405 } 1406 1407 btf_verifier_log_type(env, t, NULL); 1408 1409 return meta_needed; 1410 } 1411 1412 static void btf_int_log(struct btf_verifier_env *env, 1413 const struct btf_type *t) 1414 { 1415 int int_data = btf_type_int(t); 1416 1417 btf_verifier_log(env, 1418 "size=%u bits_offset=%u nr_bits=%u encoding=%s", 1419 t->size, BTF_INT_OFFSET(int_data), 1420 BTF_INT_BITS(int_data), 1421 btf_int_encoding_str(BTF_INT_ENCODING(int_data))); 1422 } 1423 1424 static void btf_int128_print(struct seq_file *m, void *data) 1425 { 1426 /* data points to a __int128 number. 1427 * Suppose 1428 * int128_num = *(__int128 *)data; 1429 * The below formulas shows what upper_num and lower_num represents: 1430 * upper_num = int128_num >> 64; 1431 * lower_num = int128_num & 0xffffffffFFFFFFFFULL; 1432 */ 1433 u64 upper_num, lower_num; 1434 1435 #ifdef __BIG_ENDIAN_BITFIELD 1436 upper_num = *(u64 *)data; 1437 lower_num = *(u64 *)(data + 8); 1438 #else 1439 upper_num = *(u64 *)(data + 8); 1440 lower_num = *(u64 *)data; 1441 #endif 1442 if (upper_num == 0) 1443 seq_printf(m, "0x%llx", lower_num); 1444 else 1445 seq_printf(m, "0x%llx%016llx", upper_num, lower_num); 1446 } 1447 1448 static void btf_int128_shift(u64 *print_num, u16 left_shift_bits, 1449 u16 right_shift_bits) 1450 { 1451 u64 upper_num, lower_num; 1452 1453 #ifdef __BIG_ENDIAN_BITFIELD 1454 upper_num = print_num[0]; 1455 lower_num = print_num[1]; 1456 #else 1457 upper_num = print_num[1]; 1458 lower_num = print_num[0]; 1459 #endif 1460 1461 /* shake out un-needed bits by shift/or operations */ 1462 if (left_shift_bits >= 64) { 1463 upper_num = lower_num << (left_shift_bits - 64); 1464 lower_num = 0; 1465 } else { 1466 upper_num = (upper_num << left_shift_bits) | 1467 (lower_num >> (64 - left_shift_bits)); 1468 lower_num = lower_num << left_shift_bits; 1469 } 1470 1471 if (right_shift_bits >= 64) { 1472 lower_num = upper_num >> (right_shift_bits - 64); 1473 upper_num = 0; 1474 } else { 1475 lower_num = (lower_num >> right_shift_bits) | 1476 (upper_num << (64 - right_shift_bits)); 1477 upper_num = upper_num >> right_shift_bits; 1478 } 1479 1480 #ifdef __BIG_ENDIAN_BITFIELD 1481 print_num[0] = upper_num; 1482 print_num[1] = lower_num; 1483 #else 1484 print_num[0] = lower_num; 1485 print_num[1] = upper_num; 1486 #endif 1487 } 1488 1489 static void btf_bitfield_seq_show(void *data, u8 bits_offset, 1490 u8 nr_bits, struct seq_file *m) 1491 { 1492 u16 left_shift_bits, right_shift_bits; 1493 u8 nr_copy_bytes; 1494 u8 nr_copy_bits; 1495 u64 print_num[2] = {}; 1496 1497 nr_copy_bits = nr_bits + bits_offset; 1498 nr_copy_bytes = BITS_ROUNDUP_BYTES(nr_copy_bits); 1499 1500 memcpy(print_num, data, nr_copy_bytes); 1501 1502 #ifdef __BIG_ENDIAN_BITFIELD 1503 left_shift_bits = bits_offset; 1504 #else 1505 left_shift_bits = BITS_PER_U128 - nr_copy_bits; 1506 #endif 1507 right_shift_bits = BITS_PER_U128 - nr_bits; 1508 1509 btf_int128_shift(print_num, left_shift_bits, right_shift_bits); 1510 btf_int128_print(m, print_num); 1511 } 1512 1513 1514 static void btf_int_bits_seq_show(const struct btf *btf, 1515 const struct btf_type *t, 1516 void *data, u8 bits_offset, 1517 struct seq_file *m) 1518 { 1519 u32 int_data = btf_type_int(t); 1520 u8 nr_bits = BTF_INT_BITS(int_data); 1521 u8 total_bits_offset; 1522 1523 /* 1524 * bits_offset is at most 7. 1525 * BTF_INT_OFFSET() cannot exceed 128 bits. 1526 */ 1527 total_bits_offset = bits_offset + BTF_INT_OFFSET(int_data); 1528 data += BITS_ROUNDDOWN_BYTES(total_bits_offset); 1529 bits_offset = BITS_PER_BYTE_MASKED(total_bits_offset); 1530 btf_bitfield_seq_show(data, bits_offset, nr_bits, m); 1531 } 1532 1533 static void btf_int_seq_show(const struct btf *btf, const struct btf_type *t, 1534 u32 type_id, void *data, u8 bits_offset, 1535 struct seq_file *m) 1536 { 1537 u32 int_data = btf_type_int(t); 1538 u8 encoding = BTF_INT_ENCODING(int_data); 1539 bool sign = encoding & BTF_INT_SIGNED; 1540 u8 nr_bits = BTF_INT_BITS(int_data); 1541 1542 if (bits_offset || BTF_INT_OFFSET(int_data) || 1543 BITS_PER_BYTE_MASKED(nr_bits)) { 1544 btf_int_bits_seq_show(btf, t, data, bits_offset, m); 1545 return; 1546 } 1547 1548 switch (nr_bits) { 1549 case 128: 1550 btf_int128_print(m, data); 1551 break; 1552 case 64: 1553 if (sign) 1554 seq_printf(m, "%lld", *(s64 *)data); 1555 else 1556 seq_printf(m, "%llu", *(u64 *)data); 1557 break; 1558 case 32: 1559 if (sign) 1560 seq_printf(m, "%d", *(s32 *)data); 1561 else 1562 seq_printf(m, "%u", *(u32 *)data); 1563 break; 1564 case 16: 1565 if (sign) 1566 seq_printf(m, "%d", *(s16 *)data); 1567 else 1568 seq_printf(m, "%u", *(u16 *)data); 1569 break; 1570 case 8: 1571 if (sign) 1572 seq_printf(m, "%d", *(s8 *)data); 1573 else 1574 seq_printf(m, "%u", *(u8 *)data); 1575 break; 1576 default: 1577 btf_int_bits_seq_show(btf, t, data, bits_offset, m); 1578 } 1579 } 1580 1581 static const struct btf_kind_operations int_ops = { 1582 .check_meta = btf_int_check_meta, 1583 .resolve = btf_df_resolve, 1584 .check_member = btf_int_check_member, 1585 .check_kflag_member = btf_int_check_kflag_member, 1586 .log_details = btf_int_log, 1587 .seq_show = btf_int_seq_show, 1588 }; 1589 1590 static int btf_modifier_check_member(struct btf_verifier_env *env, 1591 const struct btf_type *struct_type, 1592 const struct btf_member *member, 1593 const struct btf_type *member_type) 1594 { 1595 const struct btf_type *resolved_type; 1596 u32 resolved_type_id = member->type; 1597 struct btf_member resolved_member; 1598 struct btf *btf = env->btf; 1599 1600 resolved_type = btf_type_id_size(btf, &resolved_type_id, NULL); 1601 if (!resolved_type) { 1602 btf_verifier_log_member(env, struct_type, member, 1603 "Invalid member"); 1604 return -EINVAL; 1605 } 1606 1607 resolved_member = *member; 1608 resolved_member.type = resolved_type_id; 1609 1610 return btf_type_ops(resolved_type)->check_member(env, struct_type, 1611 &resolved_member, 1612 resolved_type); 1613 } 1614 1615 static int btf_modifier_check_kflag_member(struct btf_verifier_env *env, 1616 const struct btf_type *struct_type, 1617 const struct btf_member *member, 1618 const struct btf_type *member_type) 1619 { 1620 const struct btf_type *resolved_type; 1621 u32 resolved_type_id = member->type; 1622 struct btf_member resolved_member; 1623 struct btf *btf = env->btf; 1624 1625 resolved_type = btf_type_id_size(btf, &resolved_type_id, NULL); 1626 if (!resolved_type) { 1627 btf_verifier_log_member(env, struct_type, member, 1628 "Invalid member"); 1629 return -EINVAL; 1630 } 1631 1632 resolved_member = *member; 1633 resolved_member.type = resolved_type_id; 1634 1635 return btf_type_ops(resolved_type)->check_kflag_member(env, struct_type, 1636 &resolved_member, 1637 resolved_type); 1638 } 1639 1640 static int btf_ptr_check_member(struct btf_verifier_env *env, 1641 const struct btf_type *struct_type, 1642 const struct btf_member *member, 1643 const struct btf_type *member_type) 1644 { 1645 u32 struct_size, struct_bits_off, bytes_offset; 1646 1647 struct_size = struct_type->size; 1648 struct_bits_off = member->offset; 1649 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off); 1650 1651 if (BITS_PER_BYTE_MASKED(struct_bits_off)) { 1652 btf_verifier_log_member(env, struct_type, member, 1653 "Member is not byte aligned"); 1654 return -EINVAL; 1655 } 1656 1657 if (struct_size - bytes_offset < sizeof(void *)) { 1658 btf_verifier_log_member(env, struct_type, member, 1659 "Member exceeds struct_size"); 1660 return -EINVAL; 1661 } 1662 1663 return 0; 1664 } 1665 1666 static int btf_ref_type_check_meta(struct btf_verifier_env *env, 1667 const struct btf_type *t, 1668 u32 meta_left) 1669 { 1670 if (btf_type_vlen(t)) { 1671 btf_verifier_log_type(env, t, "vlen != 0"); 1672 return -EINVAL; 1673 } 1674 1675 if (btf_type_kflag(t)) { 1676 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); 1677 return -EINVAL; 1678 } 1679 1680 if (!BTF_TYPE_ID_VALID(t->type)) { 1681 btf_verifier_log_type(env, t, "Invalid type_id"); 1682 return -EINVAL; 1683 } 1684 1685 /* typedef type must have a valid name, and other ref types, 1686 * volatile, const, restrict, should have a null name. 1687 */ 1688 if (BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF) { 1689 if (!t->name_off || 1690 !btf_name_valid_identifier(env->btf, t->name_off)) { 1691 btf_verifier_log_type(env, t, "Invalid name"); 1692 return -EINVAL; 1693 } 1694 } else { 1695 if (t->name_off) { 1696 btf_verifier_log_type(env, t, "Invalid name"); 1697 return -EINVAL; 1698 } 1699 } 1700 1701 btf_verifier_log_type(env, t, NULL); 1702 1703 return 0; 1704 } 1705 1706 static int btf_modifier_resolve(struct btf_verifier_env *env, 1707 const struct resolve_vertex *v) 1708 { 1709 const struct btf_type *t = v->t; 1710 const struct btf_type *next_type; 1711 u32 next_type_id = t->type; 1712 struct btf *btf = env->btf; 1713 1714 next_type = btf_type_by_id(btf, next_type_id); 1715 if (!next_type || btf_type_is_resolve_source_only(next_type)) { 1716 btf_verifier_log_type(env, v->t, "Invalid type_id"); 1717 return -EINVAL; 1718 } 1719 1720 if (!env_type_is_resolve_sink(env, next_type) && 1721 !env_type_is_resolved(env, next_type_id)) 1722 return env_stack_push(env, next_type, next_type_id); 1723 1724 /* Figure out the resolved next_type_id with size. 1725 * They will be stored in the current modifier's 1726 * resolved_ids and resolved_sizes such that it can 1727 * save us a few type-following when we use it later (e.g. in 1728 * pretty print). 1729 */ 1730 if (!btf_type_id_size(btf, &next_type_id, NULL)) { 1731 if (env_type_is_resolved(env, next_type_id)) 1732 next_type = btf_type_id_resolve(btf, &next_type_id); 1733 1734 /* "typedef void new_void", "const void"...etc */ 1735 if (!btf_type_is_void(next_type) && 1736 !btf_type_is_fwd(next_type) && 1737 !btf_type_is_func_proto(next_type)) { 1738 btf_verifier_log_type(env, v->t, "Invalid type_id"); 1739 return -EINVAL; 1740 } 1741 } 1742 1743 env_stack_pop_resolved(env, next_type_id, 0); 1744 1745 return 0; 1746 } 1747 1748 static int btf_var_resolve(struct btf_verifier_env *env, 1749 const struct resolve_vertex *v) 1750 { 1751 const struct btf_type *next_type; 1752 const struct btf_type *t = v->t; 1753 u32 next_type_id = t->type; 1754 struct btf *btf = env->btf; 1755 1756 next_type = btf_type_by_id(btf, next_type_id); 1757 if (!next_type || btf_type_is_resolve_source_only(next_type)) { 1758 btf_verifier_log_type(env, v->t, "Invalid type_id"); 1759 return -EINVAL; 1760 } 1761 1762 if (!env_type_is_resolve_sink(env, next_type) && 1763 !env_type_is_resolved(env, next_type_id)) 1764 return env_stack_push(env, next_type, next_type_id); 1765 1766 if (btf_type_is_modifier(next_type)) { 1767 const struct btf_type *resolved_type; 1768 u32 resolved_type_id; 1769 1770 resolved_type_id = next_type_id; 1771 resolved_type = btf_type_id_resolve(btf, &resolved_type_id); 1772 1773 if (btf_type_is_ptr(resolved_type) && 1774 !env_type_is_resolve_sink(env, resolved_type) && 1775 !env_type_is_resolved(env, resolved_type_id)) 1776 return env_stack_push(env, resolved_type, 1777 resolved_type_id); 1778 } 1779 1780 /* We must resolve to something concrete at this point, no 1781 * forward types or similar that would resolve to size of 1782 * zero is allowed. 1783 */ 1784 if (!btf_type_id_size(btf, &next_type_id, NULL)) { 1785 btf_verifier_log_type(env, v->t, "Invalid type_id"); 1786 return -EINVAL; 1787 } 1788 1789 env_stack_pop_resolved(env, next_type_id, 0); 1790 1791 return 0; 1792 } 1793 1794 static int btf_ptr_resolve(struct btf_verifier_env *env, 1795 const struct resolve_vertex *v) 1796 { 1797 const struct btf_type *next_type; 1798 const struct btf_type *t = v->t; 1799 u32 next_type_id = t->type; 1800 struct btf *btf = env->btf; 1801 1802 next_type = btf_type_by_id(btf, next_type_id); 1803 if (!next_type || btf_type_is_resolve_source_only(next_type)) { 1804 btf_verifier_log_type(env, v->t, "Invalid type_id"); 1805 return -EINVAL; 1806 } 1807 1808 if (!env_type_is_resolve_sink(env, next_type) && 1809 !env_type_is_resolved(env, next_type_id)) 1810 return env_stack_push(env, next_type, next_type_id); 1811 1812 /* If the modifier was RESOLVED during RESOLVE_STRUCT_OR_ARRAY, 1813 * the modifier may have stopped resolving when it was resolved 1814 * to a ptr (last-resolved-ptr). 1815 * 1816 * We now need to continue from the last-resolved-ptr to 1817 * ensure the last-resolved-ptr will not referring back to 1818 * the currenct ptr (t). 1819 */ 1820 if (btf_type_is_modifier(next_type)) { 1821 const struct btf_type *resolved_type; 1822 u32 resolved_type_id; 1823 1824 resolved_type_id = next_type_id; 1825 resolved_type = btf_type_id_resolve(btf, &resolved_type_id); 1826 1827 if (btf_type_is_ptr(resolved_type) && 1828 !env_type_is_resolve_sink(env, resolved_type) && 1829 !env_type_is_resolved(env, resolved_type_id)) 1830 return env_stack_push(env, resolved_type, 1831 resolved_type_id); 1832 } 1833 1834 if (!btf_type_id_size(btf, &next_type_id, NULL)) { 1835 if (env_type_is_resolved(env, next_type_id)) 1836 next_type = btf_type_id_resolve(btf, &next_type_id); 1837 1838 if (!btf_type_is_void(next_type) && 1839 !btf_type_is_fwd(next_type) && 1840 !btf_type_is_func_proto(next_type)) { 1841 btf_verifier_log_type(env, v->t, "Invalid type_id"); 1842 return -EINVAL; 1843 } 1844 } 1845 1846 env_stack_pop_resolved(env, next_type_id, 0); 1847 1848 return 0; 1849 } 1850 1851 static void btf_modifier_seq_show(const struct btf *btf, 1852 const struct btf_type *t, 1853 u32 type_id, void *data, 1854 u8 bits_offset, struct seq_file *m) 1855 { 1856 if (btf->resolved_ids) 1857 t = btf_type_id_resolve(btf, &type_id); 1858 else 1859 t = btf_type_skip_modifiers(btf, type_id, NULL); 1860 1861 btf_type_ops(t)->seq_show(btf, t, type_id, data, bits_offset, m); 1862 } 1863 1864 static void btf_var_seq_show(const struct btf *btf, const struct btf_type *t, 1865 u32 type_id, void *data, u8 bits_offset, 1866 struct seq_file *m) 1867 { 1868 t = btf_type_id_resolve(btf, &type_id); 1869 1870 btf_type_ops(t)->seq_show(btf, t, type_id, data, bits_offset, m); 1871 } 1872 1873 static void btf_ptr_seq_show(const struct btf *btf, const struct btf_type *t, 1874 u32 type_id, void *data, u8 bits_offset, 1875 struct seq_file *m) 1876 { 1877 /* It is a hashed value */ 1878 seq_printf(m, "%p", *(void **)data); 1879 } 1880 1881 static void btf_ref_type_log(struct btf_verifier_env *env, 1882 const struct btf_type *t) 1883 { 1884 btf_verifier_log(env, "type_id=%u", t->type); 1885 } 1886 1887 static struct btf_kind_operations modifier_ops = { 1888 .check_meta = btf_ref_type_check_meta, 1889 .resolve = btf_modifier_resolve, 1890 .check_member = btf_modifier_check_member, 1891 .check_kflag_member = btf_modifier_check_kflag_member, 1892 .log_details = btf_ref_type_log, 1893 .seq_show = btf_modifier_seq_show, 1894 }; 1895 1896 static struct btf_kind_operations ptr_ops = { 1897 .check_meta = btf_ref_type_check_meta, 1898 .resolve = btf_ptr_resolve, 1899 .check_member = btf_ptr_check_member, 1900 .check_kflag_member = btf_generic_check_kflag_member, 1901 .log_details = btf_ref_type_log, 1902 .seq_show = btf_ptr_seq_show, 1903 }; 1904 1905 static s32 btf_fwd_check_meta(struct btf_verifier_env *env, 1906 const struct btf_type *t, 1907 u32 meta_left) 1908 { 1909 if (btf_type_vlen(t)) { 1910 btf_verifier_log_type(env, t, "vlen != 0"); 1911 return -EINVAL; 1912 } 1913 1914 if (t->type) { 1915 btf_verifier_log_type(env, t, "type != 0"); 1916 return -EINVAL; 1917 } 1918 1919 /* fwd type must have a valid name */ 1920 if (!t->name_off || 1921 !btf_name_valid_identifier(env->btf, t->name_off)) { 1922 btf_verifier_log_type(env, t, "Invalid name"); 1923 return -EINVAL; 1924 } 1925 1926 btf_verifier_log_type(env, t, NULL); 1927 1928 return 0; 1929 } 1930 1931 static void btf_fwd_type_log(struct btf_verifier_env *env, 1932 const struct btf_type *t) 1933 { 1934 btf_verifier_log(env, "%s", btf_type_kflag(t) ? "union" : "struct"); 1935 } 1936 1937 static struct btf_kind_operations fwd_ops = { 1938 .check_meta = btf_fwd_check_meta, 1939 .resolve = btf_df_resolve, 1940 .check_member = btf_df_check_member, 1941 .check_kflag_member = btf_df_check_kflag_member, 1942 .log_details = btf_fwd_type_log, 1943 .seq_show = btf_df_seq_show, 1944 }; 1945 1946 static int btf_array_check_member(struct btf_verifier_env *env, 1947 const struct btf_type *struct_type, 1948 const struct btf_member *member, 1949 const struct btf_type *member_type) 1950 { 1951 u32 struct_bits_off = member->offset; 1952 u32 struct_size, bytes_offset; 1953 u32 array_type_id, array_size; 1954 struct btf *btf = env->btf; 1955 1956 if (BITS_PER_BYTE_MASKED(struct_bits_off)) { 1957 btf_verifier_log_member(env, struct_type, member, 1958 "Member is not byte aligned"); 1959 return -EINVAL; 1960 } 1961 1962 array_type_id = member->type; 1963 btf_type_id_size(btf, &array_type_id, &array_size); 1964 struct_size = struct_type->size; 1965 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off); 1966 if (struct_size - bytes_offset < array_size) { 1967 btf_verifier_log_member(env, struct_type, member, 1968 "Member exceeds struct_size"); 1969 return -EINVAL; 1970 } 1971 1972 return 0; 1973 } 1974 1975 static s32 btf_array_check_meta(struct btf_verifier_env *env, 1976 const struct btf_type *t, 1977 u32 meta_left) 1978 { 1979 const struct btf_array *array = btf_type_array(t); 1980 u32 meta_needed = sizeof(*array); 1981 1982 if (meta_left < meta_needed) { 1983 btf_verifier_log_basic(env, t, 1984 "meta_left:%u meta_needed:%u", 1985 meta_left, meta_needed); 1986 return -EINVAL; 1987 } 1988 1989 /* array type should not have a name */ 1990 if (t->name_off) { 1991 btf_verifier_log_type(env, t, "Invalid name"); 1992 return -EINVAL; 1993 } 1994 1995 if (btf_type_vlen(t)) { 1996 btf_verifier_log_type(env, t, "vlen != 0"); 1997 return -EINVAL; 1998 } 1999 2000 if (btf_type_kflag(t)) { 2001 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); 2002 return -EINVAL; 2003 } 2004 2005 if (t->size) { 2006 btf_verifier_log_type(env, t, "size != 0"); 2007 return -EINVAL; 2008 } 2009 2010 /* Array elem type and index type cannot be in type void, 2011 * so !array->type and !array->index_type are not allowed. 2012 */ 2013 if (!array->type || !BTF_TYPE_ID_VALID(array->type)) { 2014 btf_verifier_log_type(env, t, "Invalid elem"); 2015 return -EINVAL; 2016 } 2017 2018 if (!array->index_type || !BTF_TYPE_ID_VALID(array->index_type)) { 2019 btf_verifier_log_type(env, t, "Invalid index"); 2020 return -EINVAL; 2021 } 2022 2023 btf_verifier_log_type(env, t, NULL); 2024 2025 return meta_needed; 2026 } 2027 2028 static int btf_array_resolve(struct btf_verifier_env *env, 2029 const struct resolve_vertex *v) 2030 { 2031 const struct btf_array *array = btf_type_array(v->t); 2032 const struct btf_type *elem_type, *index_type; 2033 u32 elem_type_id, index_type_id; 2034 struct btf *btf = env->btf; 2035 u32 elem_size; 2036 2037 /* Check array->index_type */ 2038 index_type_id = array->index_type; 2039 index_type = btf_type_by_id(btf, index_type_id); 2040 if (btf_type_nosize_or_null(index_type) || 2041 btf_type_is_resolve_source_only(index_type)) { 2042 btf_verifier_log_type(env, v->t, "Invalid index"); 2043 return -EINVAL; 2044 } 2045 2046 if (!env_type_is_resolve_sink(env, index_type) && 2047 !env_type_is_resolved(env, index_type_id)) 2048 return env_stack_push(env, index_type, index_type_id); 2049 2050 index_type = btf_type_id_size(btf, &index_type_id, NULL); 2051 if (!index_type || !btf_type_is_int(index_type) || 2052 !btf_type_int_is_regular(index_type)) { 2053 btf_verifier_log_type(env, v->t, "Invalid index"); 2054 return -EINVAL; 2055 } 2056 2057 /* Check array->type */ 2058 elem_type_id = array->type; 2059 elem_type = btf_type_by_id(btf, elem_type_id); 2060 if (btf_type_nosize_or_null(elem_type) || 2061 btf_type_is_resolve_source_only(elem_type)) { 2062 btf_verifier_log_type(env, v->t, 2063 "Invalid elem"); 2064 return -EINVAL; 2065 } 2066 2067 if (!env_type_is_resolve_sink(env, elem_type) && 2068 !env_type_is_resolved(env, elem_type_id)) 2069 return env_stack_push(env, elem_type, elem_type_id); 2070 2071 elem_type = btf_type_id_size(btf, &elem_type_id, &elem_size); 2072 if (!elem_type) { 2073 btf_verifier_log_type(env, v->t, "Invalid elem"); 2074 return -EINVAL; 2075 } 2076 2077 if (btf_type_is_int(elem_type) && !btf_type_int_is_regular(elem_type)) { 2078 btf_verifier_log_type(env, v->t, "Invalid array of int"); 2079 return -EINVAL; 2080 } 2081 2082 if (array->nelems && elem_size > U32_MAX / array->nelems) { 2083 btf_verifier_log_type(env, v->t, 2084 "Array size overflows U32_MAX"); 2085 return -EINVAL; 2086 } 2087 2088 env_stack_pop_resolved(env, elem_type_id, elem_size * array->nelems); 2089 2090 return 0; 2091 } 2092 2093 static void btf_array_log(struct btf_verifier_env *env, 2094 const struct btf_type *t) 2095 { 2096 const struct btf_array *array = btf_type_array(t); 2097 2098 btf_verifier_log(env, "type_id=%u index_type_id=%u nr_elems=%u", 2099 array->type, array->index_type, array->nelems); 2100 } 2101 2102 static void btf_array_seq_show(const struct btf *btf, const struct btf_type *t, 2103 u32 type_id, void *data, u8 bits_offset, 2104 struct seq_file *m) 2105 { 2106 const struct btf_array *array = btf_type_array(t); 2107 const struct btf_kind_operations *elem_ops; 2108 const struct btf_type *elem_type; 2109 u32 i, elem_size, elem_type_id; 2110 2111 elem_type_id = array->type; 2112 elem_type = btf_type_id_size(btf, &elem_type_id, &elem_size); 2113 elem_ops = btf_type_ops(elem_type); 2114 seq_puts(m, "["); 2115 for (i = 0; i < array->nelems; i++) { 2116 if (i) 2117 seq_puts(m, ","); 2118 2119 elem_ops->seq_show(btf, elem_type, elem_type_id, data, 2120 bits_offset, m); 2121 data += elem_size; 2122 } 2123 seq_puts(m, "]"); 2124 } 2125 2126 static struct btf_kind_operations array_ops = { 2127 .check_meta = btf_array_check_meta, 2128 .resolve = btf_array_resolve, 2129 .check_member = btf_array_check_member, 2130 .check_kflag_member = btf_generic_check_kflag_member, 2131 .log_details = btf_array_log, 2132 .seq_show = btf_array_seq_show, 2133 }; 2134 2135 static int btf_struct_check_member(struct btf_verifier_env *env, 2136 const struct btf_type *struct_type, 2137 const struct btf_member *member, 2138 const struct btf_type *member_type) 2139 { 2140 u32 struct_bits_off = member->offset; 2141 u32 struct_size, bytes_offset; 2142 2143 if (BITS_PER_BYTE_MASKED(struct_bits_off)) { 2144 btf_verifier_log_member(env, struct_type, member, 2145 "Member is not byte aligned"); 2146 return -EINVAL; 2147 } 2148 2149 struct_size = struct_type->size; 2150 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off); 2151 if (struct_size - bytes_offset < member_type->size) { 2152 btf_verifier_log_member(env, struct_type, member, 2153 "Member exceeds struct_size"); 2154 return -EINVAL; 2155 } 2156 2157 return 0; 2158 } 2159 2160 static s32 btf_struct_check_meta(struct btf_verifier_env *env, 2161 const struct btf_type *t, 2162 u32 meta_left) 2163 { 2164 bool is_union = BTF_INFO_KIND(t->info) == BTF_KIND_UNION; 2165 const struct btf_member *member; 2166 u32 meta_needed, last_offset; 2167 struct btf *btf = env->btf; 2168 u32 struct_size = t->size; 2169 u32 offset; 2170 u16 i; 2171 2172 meta_needed = btf_type_vlen(t) * sizeof(*member); 2173 if (meta_left < meta_needed) { 2174 btf_verifier_log_basic(env, t, 2175 "meta_left:%u meta_needed:%u", 2176 meta_left, meta_needed); 2177 return -EINVAL; 2178 } 2179 2180 /* struct type either no name or a valid one */ 2181 if (t->name_off && 2182 !btf_name_valid_identifier(env->btf, t->name_off)) { 2183 btf_verifier_log_type(env, t, "Invalid name"); 2184 return -EINVAL; 2185 } 2186 2187 btf_verifier_log_type(env, t, NULL); 2188 2189 last_offset = 0; 2190 for_each_member(i, t, member) { 2191 if (!btf_name_offset_valid(btf, member->name_off)) { 2192 btf_verifier_log_member(env, t, member, 2193 "Invalid member name_offset:%u", 2194 member->name_off); 2195 return -EINVAL; 2196 } 2197 2198 /* struct member either no name or a valid one */ 2199 if (member->name_off && 2200 !btf_name_valid_identifier(btf, member->name_off)) { 2201 btf_verifier_log_member(env, t, member, "Invalid name"); 2202 return -EINVAL; 2203 } 2204 /* A member cannot be in type void */ 2205 if (!member->type || !BTF_TYPE_ID_VALID(member->type)) { 2206 btf_verifier_log_member(env, t, member, 2207 "Invalid type_id"); 2208 return -EINVAL; 2209 } 2210 2211 offset = btf_member_bit_offset(t, member); 2212 if (is_union && offset) { 2213 btf_verifier_log_member(env, t, member, 2214 "Invalid member bits_offset"); 2215 return -EINVAL; 2216 } 2217 2218 /* 2219 * ">" instead of ">=" because the last member could be 2220 * "char a[0];" 2221 */ 2222 if (last_offset > offset) { 2223 btf_verifier_log_member(env, t, member, 2224 "Invalid member bits_offset"); 2225 return -EINVAL; 2226 } 2227 2228 if (BITS_ROUNDUP_BYTES(offset) > struct_size) { 2229 btf_verifier_log_member(env, t, member, 2230 "Member bits_offset exceeds its struct size"); 2231 return -EINVAL; 2232 } 2233 2234 btf_verifier_log_member(env, t, member, NULL); 2235 last_offset = offset; 2236 } 2237 2238 return meta_needed; 2239 } 2240 2241 static int btf_struct_resolve(struct btf_verifier_env *env, 2242 const struct resolve_vertex *v) 2243 { 2244 const struct btf_member *member; 2245 int err; 2246 u16 i; 2247 2248 /* Before continue resolving the next_member, 2249 * ensure the last member is indeed resolved to a 2250 * type with size info. 2251 */ 2252 if (v->next_member) { 2253 const struct btf_type *last_member_type; 2254 const struct btf_member *last_member; 2255 u16 last_member_type_id; 2256 2257 last_member = btf_type_member(v->t) + v->next_member - 1; 2258 last_member_type_id = last_member->type; 2259 if (WARN_ON_ONCE(!env_type_is_resolved(env, 2260 last_member_type_id))) 2261 return -EINVAL; 2262 2263 last_member_type = btf_type_by_id(env->btf, 2264 last_member_type_id); 2265 if (btf_type_kflag(v->t)) 2266 err = btf_type_ops(last_member_type)->check_kflag_member(env, v->t, 2267 last_member, 2268 last_member_type); 2269 else 2270 err = btf_type_ops(last_member_type)->check_member(env, v->t, 2271 last_member, 2272 last_member_type); 2273 if (err) 2274 return err; 2275 } 2276 2277 for_each_member_from(i, v->next_member, v->t, member) { 2278 u32 member_type_id = member->type; 2279 const struct btf_type *member_type = btf_type_by_id(env->btf, 2280 member_type_id); 2281 2282 if (btf_type_nosize_or_null(member_type) || 2283 btf_type_is_resolve_source_only(member_type)) { 2284 btf_verifier_log_member(env, v->t, member, 2285 "Invalid member"); 2286 return -EINVAL; 2287 } 2288 2289 if (!env_type_is_resolve_sink(env, member_type) && 2290 !env_type_is_resolved(env, member_type_id)) { 2291 env_stack_set_next_member(env, i + 1); 2292 return env_stack_push(env, member_type, member_type_id); 2293 } 2294 2295 if (btf_type_kflag(v->t)) 2296 err = btf_type_ops(member_type)->check_kflag_member(env, v->t, 2297 member, 2298 member_type); 2299 else 2300 err = btf_type_ops(member_type)->check_member(env, v->t, 2301 member, 2302 member_type); 2303 if (err) 2304 return err; 2305 } 2306 2307 env_stack_pop_resolved(env, 0, 0); 2308 2309 return 0; 2310 } 2311 2312 static void btf_struct_log(struct btf_verifier_env *env, 2313 const struct btf_type *t) 2314 { 2315 btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t)); 2316 } 2317 2318 /* find 'struct bpf_spin_lock' in map value. 2319 * return >= 0 offset if found 2320 * and < 0 in case of error 2321 */ 2322 int btf_find_spin_lock(const struct btf *btf, const struct btf_type *t) 2323 { 2324 const struct btf_member *member; 2325 u32 i, off = -ENOENT; 2326 2327 if (!__btf_type_is_struct(t)) 2328 return -EINVAL; 2329 2330 for_each_member(i, t, member) { 2331 const struct btf_type *member_type = btf_type_by_id(btf, 2332 member->type); 2333 if (!__btf_type_is_struct(member_type)) 2334 continue; 2335 if (member_type->size != sizeof(struct bpf_spin_lock)) 2336 continue; 2337 if (strcmp(__btf_name_by_offset(btf, member_type->name_off), 2338 "bpf_spin_lock")) 2339 continue; 2340 if (off != -ENOENT) 2341 /* only one 'struct bpf_spin_lock' is allowed */ 2342 return -E2BIG; 2343 off = btf_member_bit_offset(t, member); 2344 if (off % 8) 2345 /* valid C code cannot generate such BTF */ 2346 return -EINVAL; 2347 off /= 8; 2348 if (off % __alignof__(struct bpf_spin_lock)) 2349 /* valid struct bpf_spin_lock will be 4 byte aligned */ 2350 return -EINVAL; 2351 } 2352 return off; 2353 } 2354 2355 static void btf_struct_seq_show(const struct btf *btf, const struct btf_type *t, 2356 u32 type_id, void *data, u8 bits_offset, 2357 struct seq_file *m) 2358 { 2359 const char *seq = BTF_INFO_KIND(t->info) == BTF_KIND_UNION ? "|" : ","; 2360 const struct btf_member *member; 2361 u32 i; 2362 2363 seq_puts(m, "{"); 2364 for_each_member(i, t, member) { 2365 const struct btf_type *member_type = btf_type_by_id(btf, 2366 member->type); 2367 const struct btf_kind_operations *ops; 2368 u32 member_offset, bitfield_size; 2369 u32 bytes_offset; 2370 u8 bits8_offset; 2371 2372 if (i) 2373 seq_puts(m, seq); 2374 2375 member_offset = btf_member_bit_offset(t, member); 2376 bitfield_size = btf_member_bitfield_size(t, member); 2377 bytes_offset = BITS_ROUNDDOWN_BYTES(member_offset); 2378 bits8_offset = BITS_PER_BYTE_MASKED(member_offset); 2379 if (bitfield_size) { 2380 btf_bitfield_seq_show(data + bytes_offset, bits8_offset, 2381 bitfield_size, m); 2382 } else { 2383 ops = btf_type_ops(member_type); 2384 ops->seq_show(btf, member_type, member->type, 2385 data + bytes_offset, bits8_offset, m); 2386 } 2387 } 2388 seq_puts(m, "}"); 2389 } 2390 2391 static struct btf_kind_operations struct_ops = { 2392 .check_meta = btf_struct_check_meta, 2393 .resolve = btf_struct_resolve, 2394 .check_member = btf_struct_check_member, 2395 .check_kflag_member = btf_generic_check_kflag_member, 2396 .log_details = btf_struct_log, 2397 .seq_show = btf_struct_seq_show, 2398 }; 2399 2400 static int btf_enum_check_member(struct btf_verifier_env *env, 2401 const struct btf_type *struct_type, 2402 const struct btf_member *member, 2403 const struct btf_type *member_type) 2404 { 2405 u32 struct_bits_off = member->offset; 2406 u32 struct_size, bytes_offset; 2407 2408 if (BITS_PER_BYTE_MASKED(struct_bits_off)) { 2409 btf_verifier_log_member(env, struct_type, member, 2410 "Member is not byte aligned"); 2411 return -EINVAL; 2412 } 2413 2414 struct_size = struct_type->size; 2415 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off); 2416 if (struct_size - bytes_offset < sizeof(int)) { 2417 btf_verifier_log_member(env, struct_type, member, 2418 "Member exceeds struct_size"); 2419 return -EINVAL; 2420 } 2421 2422 return 0; 2423 } 2424 2425 static int btf_enum_check_kflag_member(struct btf_verifier_env *env, 2426 const struct btf_type *struct_type, 2427 const struct btf_member *member, 2428 const struct btf_type *member_type) 2429 { 2430 u32 struct_bits_off, nr_bits, bytes_end, struct_size; 2431 u32 int_bitsize = sizeof(int) * BITS_PER_BYTE; 2432 2433 struct_bits_off = BTF_MEMBER_BIT_OFFSET(member->offset); 2434 nr_bits = BTF_MEMBER_BITFIELD_SIZE(member->offset); 2435 if (!nr_bits) { 2436 if (BITS_PER_BYTE_MASKED(struct_bits_off)) { 2437 btf_verifier_log_member(env, struct_type, member, 2438 "Member is not byte aligned"); 2439 return -EINVAL; 2440 } 2441 2442 nr_bits = int_bitsize; 2443 } else if (nr_bits > int_bitsize) { 2444 btf_verifier_log_member(env, struct_type, member, 2445 "Invalid member bitfield_size"); 2446 return -EINVAL; 2447 } 2448 2449 struct_size = struct_type->size; 2450 bytes_end = BITS_ROUNDUP_BYTES(struct_bits_off + nr_bits); 2451 if (struct_size < bytes_end) { 2452 btf_verifier_log_member(env, struct_type, member, 2453 "Member exceeds struct_size"); 2454 return -EINVAL; 2455 } 2456 2457 return 0; 2458 } 2459 2460 static s32 btf_enum_check_meta(struct btf_verifier_env *env, 2461 const struct btf_type *t, 2462 u32 meta_left) 2463 { 2464 const struct btf_enum *enums = btf_type_enum(t); 2465 struct btf *btf = env->btf; 2466 u16 i, nr_enums; 2467 u32 meta_needed; 2468 2469 nr_enums = btf_type_vlen(t); 2470 meta_needed = nr_enums * sizeof(*enums); 2471 2472 if (meta_left < meta_needed) { 2473 btf_verifier_log_basic(env, t, 2474 "meta_left:%u meta_needed:%u", 2475 meta_left, meta_needed); 2476 return -EINVAL; 2477 } 2478 2479 if (btf_type_kflag(t)) { 2480 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); 2481 return -EINVAL; 2482 } 2483 2484 if (t->size > 8 || !is_power_of_2(t->size)) { 2485 btf_verifier_log_type(env, t, "Unexpected size"); 2486 return -EINVAL; 2487 } 2488 2489 /* enum type either no name or a valid one */ 2490 if (t->name_off && 2491 !btf_name_valid_identifier(env->btf, t->name_off)) { 2492 btf_verifier_log_type(env, t, "Invalid name"); 2493 return -EINVAL; 2494 } 2495 2496 btf_verifier_log_type(env, t, NULL); 2497 2498 for (i = 0; i < nr_enums; i++) { 2499 if (!btf_name_offset_valid(btf, enums[i].name_off)) { 2500 btf_verifier_log(env, "\tInvalid name_offset:%u", 2501 enums[i].name_off); 2502 return -EINVAL; 2503 } 2504 2505 /* enum member must have a valid name */ 2506 if (!enums[i].name_off || 2507 !btf_name_valid_identifier(btf, enums[i].name_off)) { 2508 btf_verifier_log_type(env, t, "Invalid name"); 2509 return -EINVAL; 2510 } 2511 2512 if (env->log.level == BPF_LOG_KERNEL) 2513 continue; 2514 btf_verifier_log(env, "\t%s val=%d\n", 2515 __btf_name_by_offset(btf, enums[i].name_off), 2516 enums[i].val); 2517 } 2518 2519 return meta_needed; 2520 } 2521 2522 static void btf_enum_log(struct btf_verifier_env *env, 2523 const struct btf_type *t) 2524 { 2525 btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t)); 2526 } 2527 2528 static void btf_enum_seq_show(const struct btf *btf, const struct btf_type *t, 2529 u32 type_id, void *data, u8 bits_offset, 2530 struct seq_file *m) 2531 { 2532 const struct btf_enum *enums = btf_type_enum(t); 2533 u32 i, nr_enums = btf_type_vlen(t); 2534 int v = *(int *)data; 2535 2536 for (i = 0; i < nr_enums; i++) { 2537 if (v == enums[i].val) { 2538 seq_printf(m, "%s", 2539 __btf_name_by_offset(btf, 2540 enums[i].name_off)); 2541 return; 2542 } 2543 } 2544 2545 seq_printf(m, "%d", v); 2546 } 2547 2548 static struct btf_kind_operations enum_ops = { 2549 .check_meta = btf_enum_check_meta, 2550 .resolve = btf_df_resolve, 2551 .check_member = btf_enum_check_member, 2552 .check_kflag_member = btf_enum_check_kflag_member, 2553 .log_details = btf_enum_log, 2554 .seq_show = btf_enum_seq_show, 2555 }; 2556 2557 static s32 btf_func_proto_check_meta(struct btf_verifier_env *env, 2558 const struct btf_type *t, 2559 u32 meta_left) 2560 { 2561 u32 meta_needed = btf_type_vlen(t) * sizeof(struct btf_param); 2562 2563 if (meta_left < meta_needed) { 2564 btf_verifier_log_basic(env, t, 2565 "meta_left:%u meta_needed:%u", 2566 meta_left, meta_needed); 2567 return -EINVAL; 2568 } 2569 2570 if (t->name_off) { 2571 btf_verifier_log_type(env, t, "Invalid name"); 2572 return -EINVAL; 2573 } 2574 2575 if (btf_type_kflag(t)) { 2576 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); 2577 return -EINVAL; 2578 } 2579 2580 btf_verifier_log_type(env, t, NULL); 2581 2582 return meta_needed; 2583 } 2584 2585 static void btf_func_proto_log(struct btf_verifier_env *env, 2586 const struct btf_type *t) 2587 { 2588 const struct btf_param *args = (const struct btf_param *)(t + 1); 2589 u16 nr_args = btf_type_vlen(t), i; 2590 2591 btf_verifier_log(env, "return=%u args=(", t->type); 2592 if (!nr_args) { 2593 btf_verifier_log(env, "void"); 2594 goto done; 2595 } 2596 2597 if (nr_args == 1 && !args[0].type) { 2598 /* Only one vararg */ 2599 btf_verifier_log(env, "vararg"); 2600 goto done; 2601 } 2602 2603 btf_verifier_log(env, "%u %s", args[0].type, 2604 __btf_name_by_offset(env->btf, 2605 args[0].name_off)); 2606 for (i = 1; i < nr_args - 1; i++) 2607 btf_verifier_log(env, ", %u %s", args[i].type, 2608 __btf_name_by_offset(env->btf, 2609 args[i].name_off)); 2610 2611 if (nr_args > 1) { 2612 const struct btf_param *last_arg = &args[nr_args - 1]; 2613 2614 if (last_arg->type) 2615 btf_verifier_log(env, ", %u %s", last_arg->type, 2616 __btf_name_by_offset(env->btf, 2617 last_arg->name_off)); 2618 else 2619 btf_verifier_log(env, ", vararg"); 2620 } 2621 2622 done: 2623 btf_verifier_log(env, ")"); 2624 } 2625 2626 static struct btf_kind_operations func_proto_ops = { 2627 .check_meta = btf_func_proto_check_meta, 2628 .resolve = btf_df_resolve, 2629 /* 2630 * BTF_KIND_FUNC_PROTO cannot be directly referred by 2631 * a struct's member. 2632 * 2633 * It should be a funciton pointer instead. 2634 * (i.e. struct's member -> BTF_KIND_PTR -> BTF_KIND_FUNC_PROTO) 2635 * 2636 * Hence, there is no btf_func_check_member(). 2637 */ 2638 .check_member = btf_df_check_member, 2639 .check_kflag_member = btf_df_check_kflag_member, 2640 .log_details = btf_func_proto_log, 2641 .seq_show = btf_df_seq_show, 2642 }; 2643 2644 static s32 btf_func_check_meta(struct btf_verifier_env *env, 2645 const struct btf_type *t, 2646 u32 meta_left) 2647 { 2648 if (!t->name_off || 2649 !btf_name_valid_identifier(env->btf, t->name_off)) { 2650 btf_verifier_log_type(env, t, "Invalid name"); 2651 return -EINVAL; 2652 } 2653 2654 if (btf_type_vlen(t) > BTF_FUNC_GLOBAL) { 2655 btf_verifier_log_type(env, t, "Invalid func linkage"); 2656 return -EINVAL; 2657 } 2658 2659 if (btf_type_kflag(t)) { 2660 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); 2661 return -EINVAL; 2662 } 2663 2664 btf_verifier_log_type(env, t, NULL); 2665 2666 return 0; 2667 } 2668 2669 static struct btf_kind_operations func_ops = { 2670 .check_meta = btf_func_check_meta, 2671 .resolve = btf_df_resolve, 2672 .check_member = btf_df_check_member, 2673 .check_kflag_member = btf_df_check_kflag_member, 2674 .log_details = btf_ref_type_log, 2675 .seq_show = btf_df_seq_show, 2676 }; 2677 2678 static s32 btf_var_check_meta(struct btf_verifier_env *env, 2679 const struct btf_type *t, 2680 u32 meta_left) 2681 { 2682 const struct btf_var *var; 2683 u32 meta_needed = sizeof(*var); 2684 2685 if (meta_left < meta_needed) { 2686 btf_verifier_log_basic(env, t, 2687 "meta_left:%u meta_needed:%u", 2688 meta_left, meta_needed); 2689 return -EINVAL; 2690 } 2691 2692 if (btf_type_vlen(t)) { 2693 btf_verifier_log_type(env, t, "vlen != 0"); 2694 return -EINVAL; 2695 } 2696 2697 if (btf_type_kflag(t)) { 2698 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); 2699 return -EINVAL; 2700 } 2701 2702 if (!t->name_off || 2703 !__btf_name_valid(env->btf, t->name_off, true)) { 2704 btf_verifier_log_type(env, t, "Invalid name"); 2705 return -EINVAL; 2706 } 2707 2708 /* A var cannot be in type void */ 2709 if (!t->type || !BTF_TYPE_ID_VALID(t->type)) { 2710 btf_verifier_log_type(env, t, "Invalid type_id"); 2711 return -EINVAL; 2712 } 2713 2714 var = btf_type_var(t); 2715 if (var->linkage != BTF_VAR_STATIC && 2716 var->linkage != BTF_VAR_GLOBAL_ALLOCATED) { 2717 btf_verifier_log_type(env, t, "Linkage not supported"); 2718 return -EINVAL; 2719 } 2720 2721 btf_verifier_log_type(env, t, NULL); 2722 2723 return meta_needed; 2724 } 2725 2726 static void btf_var_log(struct btf_verifier_env *env, const struct btf_type *t) 2727 { 2728 const struct btf_var *var = btf_type_var(t); 2729 2730 btf_verifier_log(env, "type_id=%u linkage=%u", t->type, var->linkage); 2731 } 2732 2733 static const struct btf_kind_operations var_ops = { 2734 .check_meta = btf_var_check_meta, 2735 .resolve = btf_var_resolve, 2736 .check_member = btf_df_check_member, 2737 .check_kflag_member = btf_df_check_kflag_member, 2738 .log_details = btf_var_log, 2739 .seq_show = btf_var_seq_show, 2740 }; 2741 2742 static s32 btf_datasec_check_meta(struct btf_verifier_env *env, 2743 const struct btf_type *t, 2744 u32 meta_left) 2745 { 2746 const struct btf_var_secinfo *vsi; 2747 u64 last_vsi_end_off = 0, sum = 0; 2748 u32 i, meta_needed; 2749 2750 meta_needed = btf_type_vlen(t) * sizeof(*vsi); 2751 if (meta_left < meta_needed) { 2752 btf_verifier_log_basic(env, t, 2753 "meta_left:%u meta_needed:%u", 2754 meta_left, meta_needed); 2755 return -EINVAL; 2756 } 2757 2758 if (!btf_type_vlen(t)) { 2759 btf_verifier_log_type(env, t, "vlen == 0"); 2760 return -EINVAL; 2761 } 2762 2763 if (!t->size) { 2764 btf_verifier_log_type(env, t, "size == 0"); 2765 return -EINVAL; 2766 } 2767 2768 if (btf_type_kflag(t)) { 2769 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); 2770 return -EINVAL; 2771 } 2772 2773 if (!t->name_off || 2774 !btf_name_valid_section(env->btf, t->name_off)) { 2775 btf_verifier_log_type(env, t, "Invalid name"); 2776 return -EINVAL; 2777 } 2778 2779 btf_verifier_log_type(env, t, NULL); 2780 2781 for_each_vsi(i, t, vsi) { 2782 /* A var cannot be in type void */ 2783 if (!vsi->type || !BTF_TYPE_ID_VALID(vsi->type)) { 2784 btf_verifier_log_vsi(env, t, vsi, 2785 "Invalid type_id"); 2786 return -EINVAL; 2787 } 2788 2789 if (vsi->offset < last_vsi_end_off || vsi->offset >= t->size) { 2790 btf_verifier_log_vsi(env, t, vsi, 2791 "Invalid offset"); 2792 return -EINVAL; 2793 } 2794 2795 if (!vsi->size || vsi->size > t->size) { 2796 btf_verifier_log_vsi(env, t, vsi, 2797 "Invalid size"); 2798 return -EINVAL; 2799 } 2800 2801 last_vsi_end_off = vsi->offset + vsi->size; 2802 if (last_vsi_end_off > t->size) { 2803 btf_verifier_log_vsi(env, t, vsi, 2804 "Invalid offset+size"); 2805 return -EINVAL; 2806 } 2807 2808 btf_verifier_log_vsi(env, t, vsi, NULL); 2809 sum += vsi->size; 2810 } 2811 2812 if (t->size < sum) { 2813 btf_verifier_log_type(env, t, "Invalid btf_info size"); 2814 return -EINVAL; 2815 } 2816 2817 return meta_needed; 2818 } 2819 2820 static int btf_datasec_resolve(struct btf_verifier_env *env, 2821 const struct resolve_vertex *v) 2822 { 2823 const struct btf_var_secinfo *vsi; 2824 struct btf *btf = env->btf; 2825 u16 i; 2826 2827 for_each_vsi_from(i, v->next_member, v->t, vsi) { 2828 u32 var_type_id = vsi->type, type_id, type_size = 0; 2829 const struct btf_type *var_type = btf_type_by_id(env->btf, 2830 var_type_id); 2831 if (!var_type || !btf_type_is_var(var_type)) { 2832 btf_verifier_log_vsi(env, v->t, vsi, 2833 "Not a VAR kind member"); 2834 return -EINVAL; 2835 } 2836 2837 if (!env_type_is_resolve_sink(env, var_type) && 2838 !env_type_is_resolved(env, var_type_id)) { 2839 env_stack_set_next_member(env, i + 1); 2840 return env_stack_push(env, var_type, var_type_id); 2841 } 2842 2843 type_id = var_type->type; 2844 if (!btf_type_id_size(btf, &type_id, &type_size)) { 2845 btf_verifier_log_vsi(env, v->t, vsi, "Invalid type"); 2846 return -EINVAL; 2847 } 2848 2849 if (vsi->size < type_size) { 2850 btf_verifier_log_vsi(env, v->t, vsi, "Invalid size"); 2851 return -EINVAL; 2852 } 2853 } 2854 2855 env_stack_pop_resolved(env, 0, 0); 2856 return 0; 2857 } 2858 2859 static void btf_datasec_log(struct btf_verifier_env *env, 2860 const struct btf_type *t) 2861 { 2862 btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t)); 2863 } 2864 2865 static void btf_datasec_seq_show(const struct btf *btf, 2866 const struct btf_type *t, u32 type_id, 2867 void *data, u8 bits_offset, 2868 struct seq_file *m) 2869 { 2870 const struct btf_var_secinfo *vsi; 2871 const struct btf_type *var; 2872 u32 i; 2873 2874 seq_printf(m, "section (\"%s\") = {", __btf_name_by_offset(btf, t->name_off)); 2875 for_each_vsi(i, t, vsi) { 2876 var = btf_type_by_id(btf, vsi->type); 2877 if (i) 2878 seq_puts(m, ","); 2879 btf_type_ops(var)->seq_show(btf, var, vsi->type, 2880 data + vsi->offset, bits_offset, m); 2881 } 2882 seq_puts(m, "}"); 2883 } 2884 2885 static const struct btf_kind_operations datasec_ops = { 2886 .check_meta = btf_datasec_check_meta, 2887 .resolve = btf_datasec_resolve, 2888 .check_member = btf_df_check_member, 2889 .check_kflag_member = btf_df_check_kflag_member, 2890 .log_details = btf_datasec_log, 2891 .seq_show = btf_datasec_seq_show, 2892 }; 2893 2894 static int btf_func_proto_check(struct btf_verifier_env *env, 2895 const struct btf_type *t) 2896 { 2897 const struct btf_type *ret_type; 2898 const struct btf_param *args; 2899 const struct btf *btf; 2900 u16 nr_args, i; 2901 int err; 2902 2903 btf = env->btf; 2904 args = (const struct btf_param *)(t + 1); 2905 nr_args = btf_type_vlen(t); 2906 2907 /* Check func return type which could be "void" (t->type == 0) */ 2908 if (t->type) { 2909 u32 ret_type_id = t->type; 2910 2911 ret_type = btf_type_by_id(btf, ret_type_id); 2912 if (!ret_type) { 2913 btf_verifier_log_type(env, t, "Invalid return type"); 2914 return -EINVAL; 2915 } 2916 2917 if (btf_type_needs_resolve(ret_type) && 2918 !env_type_is_resolved(env, ret_type_id)) { 2919 err = btf_resolve(env, ret_type, ret_type_id); 2920 if (err) 2921 return err; 2922 } 2923 2924 /* Ensure the return type is a type that has a size */ 2925 if (!btf_type_id_size(btf, &ret_type_id, NULL)) { 2926 btf_verifier_log_type(env, t, "Invalid return type"); 2927 return -EINVAL; 2928 } 2929 } 2930 2931 if (!nr_args) 2932 return 0; 2933 2934 /* Last func arg type_id could be 0 if it is a vararg */ 2935 if (!args[nr_args - 1].type) { 2936 if (args[nr_args - 1].name_off) { 2937 btf_verifier_log_type(env, t, "Invalid arg#%u", 2938 nr_args); 2939 return -EINVAL; 2940 } 2941 nr_args--; 2942 } 2943 2944 err = 0; 2945 for (i = 0; i < nr_args; i++) { 2946 const struct btf_type *arg_type; 2947 u32 arg_type_id; 2948 2949 arg_type_id = args[i].type; 2950 arg_type = btf_type_by_id(btf, arg_type_id); 2951 if (!arg_type) { 2952 btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1); 2953 err = -EINVAL; 2954 break; 2955 } 2956 2957 if (args[i].name_off && 2958 (!btf_name_offset_valid(btf, args[i].name_off) || 2959 !btf_name_valid_identifier(btf, args[i].name_off))) { 2960 btf_verifier_log_type(env, t, 2961 "Invalid arg#%u", i + 1); 2962 err = -EINVAL; 2963 break; 2964 } 2965 2966 if (btf_type_needs_resolve(arg_type) && 2967 !env_type_is_resolved(env, arg_type_id)) { 2968 err = btf_resolve(env, arg_type, arg_type_id); 2969 if (err) 2970 break; 2971 } 2972 2973 if (!btf_type_id_size(btf, &arg_type_id, NULL)) { 2974 btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1); 2975 err = -EINVAL; 2976 break; 2977 } 2978 } 2979 2980 return err; 2981 } 2982 2983 static int btf_func_check(struct btf_verifier_env *env, 2984 const struct btf_type *t) 2985 { 2986 const struct btf_type *proto_type; 2987 const struct btf_param *args; 2988 const struct btf *btf; 2989 u16 nr_args, i; 2990 2991 btf = env->btf; 2992 proto_type = btf_type_by_id(btf, t->type); 2993 2994 if (!proto_type || !btf_type_is_func_proto(proto_type)) { 2995 btf_verifier_log_type(env, t, "Invalid type_id"); 2996 return -EINVAL; 2997 } 2998 2999 args = (const struct btf_param *)(proto_type + 1); 3000 nr_args = btf_type_vlen(proto_type); 3001 for (i = 0; i < nr_args; i++) { 3002 if (!args[i].name_off && args[i].type) { 3003 btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1); 3004 return -EINVAL; 3005 } 3006 } 3007 3008 return 0; 3009 } 3010 3011 static const struct btf_kind_operations * const kind_ops[NR_BTF_KINDS] = { 3012 [BTF_KIND_INT] = &int_ops, 3013 [BTF_KIND_PTR] = &ptr_ops, 3014 [BTF_KIND_ARRAY] = &array_ops, 3015 [BTF_KIND_STRUCT] = &struct_ops, 3016 [BTF_KIND_UNION] = &struct_ops, 3017 [BTF_KIND_ENUM] = &enum_ops, 3018 [BTF_KIND_FWD] = &fwd_ops, 3019 [BTF_KIND_TYPEDEF] = &modifier_ops, 3020 [BTF_KIND_VOLATILE] = &modifier_ops, 3021 [BTF_KIND_CONST] = &modifier_ops, 3022 [BTF_KIND_RESTRICT] = &modifier_ops, 3023 [BTF_KIND_FUNC] = &func_ops, 3024 [BTF_KIND_FUNC_PROTO] = &func_proto_ops, 3025 [BTF_KIND_VAR] = &var_ops, 3026 [BTF_KIND_DATASEC] = &datasec_ops, 3027 }; 3028 3029 static s32 btf_check_meta(struct btf_verifier_env *env, 3030 const struct btf_type *t, 3031 u32 meta_left) 3032 { 3033 u32 saved_meta_left = meta_left; 3034 s32 var_meta_size; 3035 3036 if (meta_left < sizeof(*t)) { 3037 btf_verifier_log(env, "[%u] meta_left:%u meta_needed:%zu", 3038 env->log_type_id, meta_left, sizeof(*t)); 3039 return -EINVAL; 3040 } 3041 meta_left -= sizeof(*t); 3042 3043 if (t->info & ~BTF_INFO_MASK) { 3044 btf_verifier_log(env, "[%u] Invalid btf_info:%x", 3045 env->log_type_id, t->info); 3046 return -EINVAL; 3047 } 3048 3049 if (BTF_INFO_KIND(t->info) > BTF_KIND_MAX || 3050 BTF_INFO_KIND(t->info) == BTF_KIND_UNKN) { 3051 btf_verifier_log(env, "[%u] Invalid kind:%u", 3052 env->log_type_id, BTF_INFO_KIND(t->info)); 3053 return -EINVAL; 3054 } 3055 3056 if (!btf_name_offset_valid(env->btf, t->name_off)) { 3057 btf_verifier_log(env, "[%u] Invalid name_offset:%u", 3058 env->log_type_id, t->name_off); 3059 return -EINVAL; 3060 } 3061 3062 var_meta_size = btf_type_ops(t)->check_meta(env, t, meta_left); 3063 if (var_meta_size < 0) 3064 return var_meta_size; 3065 3066 meta_left -= var_meta_size; 3067 3068 return saved_meta_left - meta_left; 3069 } 3070 3071 static int btf_check_all_metas(struct btf_verifier_env *env) 3072 { 3073 struct btf *btf = env->btf; 3074 struct btf_header *hdr; 3075 void *cur, *end; 3076 3077 hdr = &btf->hdr; 3078 cur = btf->nohdr_data + hdr->type_off; 3079 end = cur + hdr->type_len; 3080 3081 env->log_type_id = 1; 3082 while (cur < end) { 3083 struct btf_type *t = cur; 3084 s32 meta_size; 3085 3086 meta_size = btf_check_meta(env, t, end - cur); 3087 if (meta_size < 0) 3088 return meta_size; 3089 3090 btf_add_type(env, t); 3091 cur += meta_size; 3092 env->log_type_id++; 3093 } 3094 3095 return 0; 3096 } 3097 3098 static bool btf_resolve_valid(struct btf_verifier_env *env, 3099 const struct btf_type *t, 3100 u32 type_id) 3101 { 3102 struct btf *btf = env->btf; 3103 3104 if (!env_type_is_resolved(env, type_id)) 3105 return false; 3106 3107 if (btf_type_is_struct(t) || btf_type_is_datasec(t)) 3108 return !btf->resolved_ids[type_id] && 3109 !btf->resolved_sizes[type_id]; 3110 3111 if (btf_type_is_modifier(t) || btf_type_is_ptr(t) || 3112 btf_type_is_var(t)) { 3113 t = btf_type_id_resolve(btf, &type_id); 3114 return t && 3115 !btf_type_is_modifier(t) && 3116 !btf_type_is_var(t) && 3117 !btf_type_is_datasec(t); 3118 } 3119 3120 if (btf_type_is_array(t)) { 3121 const struct btf_array *array = btf_type_array(t); 3122 const struct btf_type *elem_type; 3123 u32 elem_type_id = array->type; 3124 u32 elem_size; 3125 3126 elem_type = btf_type_id_size(btf, &elem_type_id, &elem_size); 3127 return elem_type && !btf_type_is_modifier(elem_type) && 3128 (array->nelems * elem_size == 3129 btf->resolved_sizes[type_id]); 3130 } 3131 3132 return false; 3133 } 3134 3135 static int btf_resolve(struct btf_verifier_env *env, 3136 const struct btf_type *t, u32 type_id) 3137 { 3138 u32 save_log_type_id = env->log_type_id; 3139 const struct resolve_vertex *v; 3140 int err = 0; 3141 3142 env->resolve_mode = RESOLVE_TBD; 3143 env_stack_push(env, t, type_id); 3144 while (!err && (v = env_stack_peak(env))) { 3145 env->log_type_id = v->type_id; 3146 err = btf_type_ops(v->t)->resolve(env, v); 3147 } 3148 3149 env->log_type_id = type_id; 3150 if (err == -E2BIG) { 3151 btf_verifier_log_type(env, t, 3152 "Exceeded max resolving depth:%u", 3153 MAX_RESOLVE_DEPTH); 3154 } else if (err == -EEXIST) { 3155 btf_verifier_log_type(env, t, "Loop detected"); 3156 } 3157 3158 /* Final sanity check */ 3159 if (!err && !btf_resolve_valid(env, t, type_id)) { 3160 btf_verifier_log_type(env, t, "Invalid resolve state"); 3161 err = -EINVAL; 3162 } 3163 3164 env->log_type_id = save_log_type_id; 3165 return err; 3166 } 3167 3168 static int btf_check_all_types(struct btf_verifier_env *env) 3169 { 3170 struct btf *btf = env->btf; 3171 u32 type_id; 3172 int err; 3173 3174 err = env_resolve_init(env); 3175 if (err) 3176 return err; 3177 3178 env->phase++; 3179 for (type_id = 1; type_id <= btf->nr_types; type_id++) { 3180 const struct btf_type *t = btf_type_by_id(btf, type_id); 3181 3182 env->log_type_id = type_id; 3183 if (btf_type_needs_resolve(t) && 3184 !env_type_is_resolved(env, type_id)) { 3185 err = btf_resolve(env, t, type_id); 3186 if (err) 3187 return err; 3188 } 3189 3190 if (btf_type_is_func_proto(t)) { 3191 err = btf_func_proto_check(env, t); 3192 if (err) 3193 return err; 3194 } 3195 3196 if (btf_type_is_func(t)) { 3197 err = btf_func_check(env, t); 3198 if (err) 3199 return err; 3200 } 3201 } 3202 3203 return 0; 3204 } 3205 3206 static int btf_parse_type_sec(struct btf_verifier_env *env) 3207 { 3208 const struct btf_header *hdr = &env->btf->hdr; 3209 int err; 3210 3211 /* Type section must align to 4 bytes */ 3212 if (hdr->type_off & (sizeof(u32) - 1)) { 3213 btf_verifier_log(env, "Unaligned type_off"); 3214 return -EINVAL; 3215 } 3216 3217 if (!hdr->type_len) { 3218 btf_verifier_log(env, "No type found"); 3219 return -EINVAL; 3220 } 3221 3222 err = btf_check_all_metas(env); 3223 if (err) 3224 return err; 3225 3226 return btf_check_all_types(env); 3227 } 3228 3229 static int btf_parse_str_sec(struct btf_verifier_env *env) 3230 { 3231 const struct btf_header *hdr; 3232 struct btf *btf = env->btf; 3233 const char *start, *end; 3234 3235 hdr = &btf->hdr; 3236 start = btf->nohdr_data + hdr->str_off; 3237 end = start + hdr->str_len; 3238 3239 if (end != btf->data + btf->data_size) { 3240 btf_verifier_log(env, "String section is not at the end"); 3241 return -EINVAL; 3242 } 3243 3244 if (!hdr->str_len || hdr->str_len - 1 > BTF_MAX_NAME_OFFSET || 3245 start[0] || end[-1]) { 3246 btf_verifier_log(env, "Invalid string section"); 3247 return -EINVAL; 3248 } 3249 3250 btf->strings = start; 3251 3252 return 0; 3253 } 3254 3255 static const size_t btf_sec_info_offset[] = { 3256 offsetof(struct btf_header, type_off), 3257 offsetof(struct btf_header, str_off), 3258 }; 3259 3260 static int btf_sec_info_cmp(const void *a, const void *b) 3261 { 3262 const struct btf_sec_info *x = a; 3263 const struct btf_sec_info *y = b; 3264 3265 return (int)(x->off - y->off) ? : (int)(x->len - y->len); 3266 } 3267 3268 static int btf_check_sec_info(struct btf_verifier_env *env, 3269 u32 btf_data_size) 3270 { 3271 struct btf_sec_info secs[ARRAY_SIZE(btf_sec_info_offset)]; 3272 u32 total, expected_total, i; 3273 const struct btf_header *hdr; 3274 const struct btf *btf; 3275 3276 btf = env->btf; 3277 hdr = &btf->hdr; 3278 3279 /* Populate the secs from hdr */ 3280 for (i = 0; i < ARRAY_SIZE(btf_sec_info_offset); i++) 3281 secs[i] = *(struct btf_sec_info *)((void *)hdr + 3282 btf_sec_info_offset[i]); 3283 3284 sort(secs, ARRAY_SIZE(btf_sec_info_offset), 3285 sizeof(struct btf_sec_info), btf_sec_info_cmp, NULL); 3286 3287 /* Check for gaps and overlap among sections */ 3288 total = 0; 3289 expected_total = btf_data_size - hdr->hdr_len; 3290 for (i = 0; i < ARRAY_SIZE(btf_sec_info_offset); i++) { 3291 if (expected_total < secs[i].off) { 3292 btf_verifier_log(env, "Invalid section offset"); 3293 return -EINVAL; 3294 } 3295 if (total < secs[i].off) { 3296 /* gap */ 3297 btf_verifier_log(env, "Unsupported section found"); 3298 return -EINVAL; 3299 } 3300 if (total > secs[i].off) { 3301 btf_verifier_log(env, "Section overlap found"); 3302 return -EINVAL; 3303 } 3304 if (expected_total - total < secs[i].len) { 3305 btf_verifier_log(env, 3306 "Total section length too long"); 3307 return -EINVAL; 3308 } 3309 total += secs[i].len; 3310 } 3311 3312 /* There is data other than hdr and known sections */ 3313 if (expected_total != total) { 3314 btf_verifier_log(env, "Unsupported section found"); 3315 return -EINVAL; 3316 } 3317 3318 return 0; 3319 } 3320 3321 static int btf_parse_hdr(struct btf_verifier_env *env) 3322 { 3323 u32 hdr_len, hdr_copy, btf_data_size; 3324 const struct btf_header *hdr; 3325 struct btf *btf; 3326 int err; 3327 3328 btf = env->btf; 3329 btf_data_size = btf->data_size; 3330 3331 if (btf_data_size < 3332 offsetof(struct btf_header, hdr_len) + sizeof(hdr->hdr_len)) { 3333 btf_verifier_log(env, "hdr_len not found"); 3334 return -EINVAL; 3335 } 3336 3337 hdr = btf->data; 3338 hdr_len = hdr->hdr_len; 3339 if (btf_data_size < hdr_len) { 3340 btf_verifier_log(env, "btf_header not found"); 3341 return -EINVAL; 3342 } 3343 3344 /* Ensure the unsupported header fields are zero */ 3345 if (hdr_len > sizeof(btf->hdr)) { 3346 u8 *expected_zero = btf->data + sizeof(btf->hdr); 3347 u8 *end = btf->data + hdr_len; 3348 3349 for (; expected_zero < end; expected_zero++) { 3350 if (*expected_zero) { 3351 btf_verifier_log(env, "Unsupported btf_header"); 3352 return -E2BIG; 3353 } 3354 } 3355 } 3356 3357 hdr_copy = min_t(u32, hdr_len, sizeof(btf->hdr)); 3358 memcpy(&btf->hdr, btf->data, hdr_copy); 3359 3360 hdr = &btf->hdr; 3361 3362 btf_verifier_log_hdr(env, btf_data_size); 3363 3364 if (hdr->magic != BTF_MAGIC) { 3365 btf_verifier_log(env, "Invalid magic"); 3366 return -EINVAL; 3367 } 3368 3369 if (hdr->version != BTF_VERSION) { 3370 btf_verifier_log(env, "Unsupported version"); 3371 return -ENOTSUPP; 3372 } 3373 3374 if (hdr->flags) { 3375 btf_verifier_log(env, "Unsupported flags"); 3376 return -ENOTSUPP; 3377 } 3378 3379 if (btf_data_size == hdr->hdr_len) { 3380 btf_verifier_log(env, "No data"); 3381 return -EINVAL; 3382 } 3383 3384 err = btf_check_sec_info(env, btf_data_size); 3385 if (err) 3386 return err; 3387 3388 return 0; 3389 } 3390 3391 static struct btf *btf_parse(void __user *btf_data, u32 btf_data_size, 3392 u32 log_level, char __user *log_ubuf, u32 log_size) 3393 { 3394 struct btf_verifier_env *env = NULL; 3395 struct bpf_verifier_log *log; 3396 struct btf *btf = NULL; 3397 u8 *data; 3398 int err; 3399 3400 if (btf_data_size > BTF_MAX_SIZE) 3401 return ERR_PTR(-E2BIG); 3402 3403 env = kzalloc(sizeof(*env), GFP_KERNEL | __GFP_NOWARN); 3404 if (!env) 3405 return ERR_PTR(-ENOMEM); 3406 3407 log = &env->log; 3408 if (log_level || log_ubuf || log_size) { 3409 /* user requested verbose verifier output 3410 * and supplied buffer to store the verification trace 3411 */ 3412 log->level = log_level; 3413 log->ubuf = log_ubuf; 3414 log->len_total = log_size; 3415 3416 /* log attributes have to be sane */ 3417 if (log->len_total < 128 || log->len_total > UINT_MAX >> 8 || 3418 !log->level || !log->ubuf) { 3419 err = -EINVAL; 3420 goto errout; 3421 } 3422 } 3423 3424 btf = kzalloc(sizeof(*btf), GFP_KERNEL | __GFP_NOWARN); 3425 if (!btf) { 3426 err = -ENOMEM; 3427 goto errout; 3428 } 3429 env->btf = btf; 3430 3431 data = kvmalloc(btf_data_size, GFP_KERNEL | __GFP_NOWARN); 3432 if (!data) { 3433 err = -ENOMEM; 3434 goto errout; 3435 } 3436 3437 btf->data = data; 3438 btf->data_size = btf_data_size; 3439 3440 if (copy_from_user(data, btf_data, btf_data_size)) { 3441 err = -EFAULT; 3442 goto errout; 3443 } 3444 3445 err = btf_parse_hdr(env); 3446 if (err) 3447 goto errout; 3448 3449 btf->nohdr_data = btf->data + btf->hdr.hdr_len; 3450 3451 err = btf_parse_str_sec(env); 3452 if (err) 3453 goto errout; 3454 3455 err = btf_parse_type_sec(env); 3456 if (err) 3457 goto errout; 3458 3459 if (log->level && bpf_verifier_log_full(log)) { 3460 err = -ENOSPC; 3461 goto errout; 3462 } 3463 3464 btf_verifier_env_free(env); 3465 refcount_set(&btf->refcnt, 1); 3466 return btf; 3467 3468 errout: 3469 btf_verifier_env_free(env); 3470 if (btf) 3471 btf_free(btf); 3472 return ERR_PTR(err); 3473 } 3474 3475 extern char __weak _binary__btf_vmlinux_bin_start[]; 3476 extern char __weak _binary__btf_vmlinux_bin_end[]; 3477 extern struct btf *btf_vmlinux; 3478 3479 #define BPF_MAP_TYPE(_id, _ops) 3480 static union { 3481 struct bpf_ctx_convert { 3482 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \ 3483 prog_ctx_type _id##_prog; \ 3484 kern_ctx_type _id##_kern; 3485 #include <linux/bpf_types.h> 3486 #undef BPF_PROG_TYPE 3487 } *__t; 3488 /* 't' is written once under lock. Read many times. */ 3489 const struct btf_type *t; 3490 } bpf_ctx_convert; 3491 enum { 3492 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \ 3493 __ctx_convert##_id, 3494 #include <linux/bpf_types.h> 3495 #undef BPF_PROG_TYPE 3496 __ctx_convert_unused, /* to avoid empty enum in extreme .config */ 3497 }; 3498 static u8 bpf_ctx_convert_map[] = { 3499 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \ 3500 [_id] = __ctx_convert##_id, 3501 #include <linux/bpf_types.h> 3502 #undef BPF_PROG_TYPE 3503 0, /* avoid empty array */ 3504 }; 3505 #undef BPF_MAP_TYPE 3506 3507 static const struct btf_member * 3508 btf_get_prog_ctx_type(struct bpf_verifier_log *log, struct btf *btf, 3509 const struct btf_type *t, enum bpf_prog_type prog_type, 3510 int arg) 3511 { 3512 const struct btf_type *conv_struct; 3513 const struct btf_type *ctx_struct; 3514 const struct btf_member *ctx_type; 3515 const char *tname, *ctx_tname; 3516 3517 conv_struct = bpf_ctx_convert.t; 3518 if (!conv_struct) { 3519 bpf_log(log, "btf_vmlinux is malformed\n"); 3520 return NULL; 3521 } 3522 t = btf_type_by_id(btf, t->type); 3523 while (btf_type_is_modifier(t)) 3524 t = btf_type_by_id(btf, t->type); 3525 if (!btf_type_is_struct(t)) { 3526 /* Only pointer to struct is supported for now. 3527 * That means that BPF_PROG_TYPE_TRACEPOINT with BTF 3528 * is not supported yet. 3529 * BPF_PROG_TYPE_RAW_TRACEPOINT is fine. 3530 */ 3531 if (log->level & BPF_LOG_LEVEL) 3532 bpf_log(log, "arg#%d type is not a struct\n", arg); 3533 return NULL; 3534 } 3535 tname = btf_name_by_offset(btf, t->name_off); 3536 if (!tname) { 3537 bpf_log(log, "arg#%d struct doesn't have a name\n", arg); 3538 return NULL; 3539 } 3540 /* prog_type is valid bpf program type. No need for bounds check. */ 3541 ctx_type = btf_type_member(conv_struct) + bpf_ctx_convert_map[prog_type] * 2; 3542 /* ctx_struct is a pointer to prog_ctx_type in vmlinux. 3543 * Like 'struct __sk_buff' 3544 */ 3545 ctx_struct = btf_type_by_id(btf_vmlinux, ctx_type->type); 3546 if (!ctx_struct) 3547 /* should not happen */ 3548 return NULL; 3549 ctx_tname = btf_name_by_offset(btf_vmlinux, ctx_struct->name_off); 3550 if (!ctx_tname) { 3551 /* should not happen */ 3552 bpf_log(log, "Please fix kernel include/linux/bpf_types.h\n"); 3553 return NULL; 3554 } 3555 /* only compare that prog's ctx type name is the same as 3556 * kernel expects. No need to compare field by field. 3557 * It's ok for bpf prog to do: 3558 * struct __sk_buff {}; 3559 * int socket_filter_bpf_prog(struct __sk_buff *skb) 3560 * { // no fields of skb are ever used } 3561 */ 3562 if (strcmp(ctx_tname, tname)) 3563 return NULL; 3564 return ctx_type; 3565 } 3566 3567 static int btf_translate_to_vmlinux(struct bpf_verifier_log *log, 3568 struct btf *btf, 3569 const struct btf_type *t, 3570 enum bpf_prog_type prog_type, 3571 int arg) 3572 { 3573 const struct btf_member *prog_ctx_type, *kern_ctx_type; 3574 3575 prog_ctx_type = btf_get_prog_ctx_type(log, btf, t, prog_type, arg); 3576 if (!prog_ctx_type) 3577 return -ENOENT; 3578 kern_ctx_type = prog_ctx_type + 1; 3579 return kern_ctx_type->type; 3580 } 3581 3582 struct btf *btf_parse_vmlinux(void) 3583 { 3584 struct btf_verifier_env *env = NULL; 3585 struct bpf_verifier_log *log; 3586 struct btf *btf = NULL; 3587 int err, i; 3588 3589 env = kzalloc(sizeof(*env), GFP_KERNEL | __GFP_NOWARN); 3590 if (!env) 3591 return ERR_PTR(-ENOMEM); 3592 3593 log = &env->log; 3594 log->level = BPF_LOG_KERNEL; 3595 3596 btf = kzalloc(sizeof(*btf), GFP_KERNEL | __GFP_NOWARN); 3597 if (!btf) { 3598 err = -ENOMEM; 3599 goto errout; 3600 } 3601 env->btf = btf; 3602 3603 btf->data = _binary__btf_vmlinux_bin_start; 3604 btf->data_size = _binary__btf_vmlinux_bin_end - 3605 _binary__btf_vmlinux_bin_start; 3606 3607 err = btf_parse_hdr(env); 3608 if (err) 3609 goto errout; 3610 3611 btf->nohdr_data = btf->data + btf->hdr.hdr_len; 3612 3613 err = btf_parse_str_sec(env); 3614 if (err) 3615 goto errout; 3616 3617 err = btf_check_all_metas(env); 3618 if (err) 3619 goto errout; 3620 3621 /* find struct bpf_ctx_convert for type checking later */ 3622 for (i = 1; i <= btf->nr_types; i++) { 3623 const struct btf_type *t; 3624 const char *tname; 3625 3626 t = btf_type_by_id(btf, i); 3627 if (!__btf_type_is_struct(t)) 3628 continue; 3629 tname = __btf_name_by_offset(btf, t->name_off); 3630 if (!strcmp(tname, "bpf_ctx_convert")) { 3631 /* btf_parse_vmlinux() runs under bpf_verifier_lock */ 3632 bpf_ctx_convert.t = t; 3633 break; 3634 } 3635 } 3636 if (i > btf->nr_types) { 3637 err = -ENOENT; 3638 goto errout; 3639 } 3640 3641 bpf_struct_ops_init(btf); 3642 3643 btf_verifier_env_free(env); 3644 refcount_set(&btf->refcnt, 1); 3645 return btf; 3646 3647 errout: 3648 btf_verifier_env_free(env); 3649 if (btf) { 3650 kvfree(btf->types); 3651 kfree(btf); 3652 } 3653 return ERR_PTR(err); 3654 } 3655 3656 struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog) 3657 { 3658 struct bpf_prog *tgt_prog = prog->aux->linked_prog; 3659 3660 if (tgt_prog) { 3661 return tgt_prog->aux->btf; 3662 } else { 3663 return btf_vmlinux; 3664 } 3665 } 3666 3667 bool btf_ctx_access(int off, int size, enum bpf_access_type type, 3668 const struct bpf_prog *prog, 3669 struct bpf_insn_access_aux *info) 3670 { 3671 const struct btf_type *t = prog->aux->attach_func_proto; 3672 struct bpf_prog *tgt_prog = prog->aux->linked_prog; 3673 struct btf *btf = bpf_prog_get_target_btf(prog); 3674 const char *tname = prog->aux->attach_func_name; 3675 struct bpf_verifier_log *log = info->log; 3676 const struct btf_param *args; 3677 u32 nr_args, arg; 3678 int ret; 3679 3680 if (off % 8) { 3681 bpf_log(log, "func '%s' offset %d is not multiple of 8\n", 3682 tname, off); 3683 return false; 3684 } 3685 arg = off / 8; 3686 args = (const struct btf_param *)(t + 1); 3687 /* if (t == NULL) Fall back to default BPF prog with 5 u64 arguments */ 3688 nr_args = t ? btf_type_vlen(t) : 5; 3689 if (prog->aux->attach_btf_trace) { 3690 /* skip first 'void *__data' argument in btf_trace_##name typedef */ 3691 args++; 3692 nr_args--; 3693 } 3694 3695 if (prog->expected_attach_type == BPF_TRACE_FEXIT && 3696 arg == nr_args) { 3697 if (!t) 3698 /* Default prog with 5 args. 6th arg is retval. */ 3699 return true; 3700 /* function return type */ 3701 t = btf_type_by_id(btf, t->type); 3702 } else if (arg >= nr_args) { 3703 bpf_log(log, "func '%s' doesn't have %d-th argument\n", 3704 tname, arg + 1); 3705 return false; 3706 } else { 3707 if (!t) 3708 /* Default prog with 5 args */ 3709 return true; 3710 t = btf_type_by_id(btf, args[arg].type); 3711 } 3712 /* skip modifiers */ 3713 while (btf_type_is_modifier(t)) 3714 t = btf_type_by_id(btf, t->type); 3715 if (btf_type_is_int(t) || btf_type_is_enum(t)) 3716 /* accessing a scalar */ 3717 return true; 3718 if (!btf_type_is_ptr(t)) { 3719 bpf_log(log, 3720 "func '%s' arg%d '%s' has type %s. Only pointer access is allowed\n", 3721 tname, arg, 3722 __btf_name_by_offset(btf, t->name_off), 3723 btf_kind_str[BTF_INFO_KIND(t->info)]); 3724 return false; 3725 } 3726 if (t->type == 0) 3727 /* This is a pointer to void. 3728 * It is the same as scalar from the verifier safety pov. 3729 * No further pointer walking is allowed. 3730 */ 3731 return true; 3732 3733 /* this is a pointer to another type */ 3734 info->reg_type = PTR_TO_BTF_ID; 3735 3736 if (tgt_prog) { 3737 ret = btf_translate_to_vmlinux(log, btf, t, tgt_prog->type, arg); 3738 if (ret > 0) { 3739 info->btf_id = ret; 3740 return true; 3741 } else { 3742 return false; 3743 } 3744 } 3745 3746 info->btf_id = t->type; 3747 t = btf_type_by_id(btf, t->type); 3748 /* skip modifiers */ 3749 while (btf_type_is_modifier(t)) { 3750 info->btf_id = t->type; 3751 t = btf_type_by_id(btf, t->type); 3752 } 3753 if (!btf_type_is_struct(t)) { 3754 bpf_log(log, 3755 "func '%s' arg%d type %s is not a struct\n", 3756 tname, arg, btf_kind_str[BTF_INFO_KIND(t->info)]); 3757 return false; 3758 } 3759 bpf_log(log, "func '%s' arg%d has btf_id %d type %s '%s'\n", 3760 tname, arg, info->btf_id, btf_kind_str[BTF_INFO_KIND(t->info)], 3761 __btf_name_by_offset(btf, t->name_off)); 3762 return true; 3763 } 3764 3765 int btf_struct_access(struct bpf_verifier_log *log, 3766 const struct btf_type *t, int off, int size, 3767 enum bpf_access_type atype, 3768 u32 *next_btf_id) 3769 { 3770 u32 i, moff, mtrue_end, msize = 0, total_nelems = 0; 3771 const struct btf_type *mtype, *elem_type = NULL; 3772 const struct btf_member *member; 3773 const char *tname, *mname; 3774 3775 again: 3776 tname = __btf_name_by_offset(btf_vmlinux, t->name_off); 3777 if (!btf_type_is_struct(t)) { 3778 bpf_log(log, "Type '%s' is not a struct\n", tname); 3779 return -EINVAL; 3780 } 3781 3782 if (off + size > t->size) { 3783 bpf_log(log, "access beyond struct %s at off %u size %u\n", 3784 tname, off, size); 3785 return -EACCES; 3786 } 3787 3788 for_each_member(i, t, member) { 3789 /* offset of the field in bytes */ 3790 moff = btf_member_bit_offset(t, member) / 8; 3791 if (off + size <= moff) 3792 /* won't find anything, field is already too far */ 3793 break; 3794 3795 if (btf_member_bitfield_size(t, member)) { 3796 u32 end_bit = btf_member_bit_offset(t, member) + 3797 btf_member_bitfield_size(t, member); 3798 3799 /* off <= moff instead of off == moff because clang 3800 * does not generate a BTF member for anonymous 3801 * bitfield like the ":16" here: 3802 * struct { 3803 * int :16; 3804 * int x:8; 3805 * }; 3806 */ 3807 if (off <= moff && 3808 BITS_ROUNDUP_BYTES(end_bit) <= off + size) 3809 return SCALAR_VALUE; 3810 3811 /* off may be accessing a following member 3812 * 3813 * or 3814 * 3815 * Doing partial access at either end of this 3816 * bitfield. Continue on this case also to 3817 * treat it as not accessing this bitfield 3818 * and eventually error out as field not 3819 * found to keep it simple. 3820 * It could be relaxed if there was a legit 3821 * partial access case later. 3822 */ 3823 continue; 3824 } 3825 3826 /* In case of "off" is pointing to holes of a struct */ 3827 if (off < moff) 3828 break; 3829 3830 /* type of the field */ 3831 mtype = btf_type_by_id(btf_vmlinux, member->type); 3832 mname = __btf_name_by_offset(btf_vmlinux, member->name_off); 3833 3834 mtype = btf_resolve_size(btf_vmlinux, mtype, &msize, 3835 &elem_type, &total_nelems); 3836 if (IS_ERR(mtype)) { 3837 bpf_log(log, "field %s doesn't have size\n", mname); 3838 return -EFAULT; 3839 } 3840 3841 mtrue_end = moff + msize; 3842 if (off >= mtrue_end) 3843 /* no overlap with member, keep iterating */ 3844 continue; 3845 3846 if (btf_type_is_array(mtype)) { 3847 u32 elem_idx; 3848 3849 /* btf_resolve_size() above helps to 3850 * linearize a multi-dimensional array. 3851 * 3852 * The logic here is treating an array 3853 * in a struct as the following way: 3854 * 3855 * struct outer { 3856 * struct inner array[2][2]; 3857 * }; 3858 * 3859 * looks like: 3860 * 3861 * struct outer { 3862 * struct inner array_elem0; 3863 * struct inner array_elem1; 3864 * struct inner array_elem2; 3865 * struct inner array_elem3; 3866 * }; 3867 * 3868 * When accessing outer->array[1][0], it moves 3869 * moff to "array_elem2", set mtype to 3870 * "struct inner", and msize also becomes 3871 * sizeof(struct inner). Then most of the 3872 * remaining logic will fall through without 3873 * caring the current member is an array or 3874 * not. 3875 * 3876 * Unlike mtype/msize/moff, mtrue_end does not 3877 * change. The naming difference ("_true") tells 3878 * that it is not always corresponding to 3879 * the current mtype/msize/moff. 3880 * It is the true end of the current 3881 * member (i.e. array in this case). That 3882 * will allow an int array to be accessed like 3883 * a scratch space, 3884 * i.e. allow access beyond the size of 3885 * the array's element as long as it is 3886 * within the mtrue_end boundary. 3887 */ 3888 3889 /* skip empty array */ 3890 if (moff == mtrue_end) 3891 continue; 3892 3893 msize /= total_nelems; 3894 elem_idx = (off - moff) / msize; 3895 moff += elem_idx * msize; 3896 mtype = elem_type; 3897 } 3898 3899 /* the 'off' we're looking for is either equal to start 3900 * of this field or inside of this struct 3901 */ 3902 if (btf_type_is_struct(mtype)) { 3903 /* our field must be inside that union or struct */ 3904 t = mtype; 3905 3906 /* adjust offset we're looking for */ 3907 off -= moff; 3908 goto again; 3909 } 3910 3911 if (btf_type_is_ptr(mtype)) { 3912 const struct btf_type *stype; 3913 3914 if (msize != size || off != moff) { 3915 bpf_log(log, 3916 "cannot access ptr member %s with moff %u in struct %s with off %u size %u\n", 3917 mname, moff, tname, off, size); 3918 return -EACCES; 3919 } 3920 3921 stype = btf_type_by_id(btf_vmlinux, mtype->type); 3922 /* skip modifiers */ 3923 while (btf_type_is_modifier(stype)) 3924 stype = btf_type_by_id(btf_vmlinux, stype->type); 3925 if (btf_type_is_struct(stype)) { 3926 *next_btf_id = mtype->type; 3927 return PTR_TO_BTF_ID; 3928 } 3929 } 3930 3931 /* Allow more flexible access within an int as long as 3932 * it is within mtrue_end. 3933 * Since mtrue_end could be the end of an array, 3934 * that also allows using an array of int as a scratch 3935 * space. e.g. skb->cb[]. 3936 */ 3937 if (off + size > mtrue_end) { 3938 bpf_log(log, 3939 "access beyond the end of member %s (mend:%u) in struct %s with off %u size %u\n", 3940 mname, mtrue_end, tname, off, size); 3941 return -EACCES; 3942 } 3943 3944 return SCALAR_VALUE; 3945 } 3946 bpf_log(log, "struct %s doesn't have field at offset %d\n", tname, off); 3947 return -EINVAL; 3948 } 3949 3950 static int __btf_resolve_helper_id(struct bpf_verifier_log *log, void *fn, 3951 int arg) 3952 { 3953 char fnname[KSYM_SYMBOL_LEN + 4] = "btf_"; 3954 const struct btf_param *args; 3955 const struct btf_type *t; 3956 const char *tname, *sym; 3957 u32 btf_id, i; 3958 3959 if (IS_ERR(btf_vmlinux)) { 3960 bpf_log(log, "btf_vmlinux is malformed\n"); 3961 return -EINVAL; 3962 } 3963 3964 sym = kallsyms_lookup((long)fn, NULL, NULL, NULL, fnname + 4); 3965 if (!sym) { 3966 bpf_log(log, "kernel doesn't have kallsyms\n"); 3967 return -EFAULT; 3968 } 3969 3970 for (i = 1; i <= btf_vmlinux->nr_types; i++) { 3971 t = btf_type_by_id(btf_vmlinux, i); 3972 if (BTF_INFO_KIND(t->info) != BTF_KIND_TYPEDEF) 3973 continue; 3974 tname = __btf_name_by_offset(btf_vmlinux, t->name_off); 3975 if (!strcmp(tname, fnname)) 3976 break; 3977 } 3978 if (i > btf_vmlinux->nr_types) { 3979 bpf_log(log, "helper %s type is not found\n", fnname); 3980 return -ENOENT; 3981 } 3982 3983 t = btf_type_by_id(btf_vmlinux, t->type); 3984 if (!btf_type_is_ptr(t)) 3985 return -EFAULT; 3986 t = btf_type_by_id(btf_vmlinux, t->type); 3987 if (!btf_type_is_func_proto(t)) 3988 return -EFAULT; 3989 3990 args = (const struct btf_param *)(t + 1); 3991 if (arg >= btf_type_vlen(t)) { 3992 bpf_log(log, "bpf helper %s doesn't have %d-th argument\n", 3993 fnname, arg); 3994 return -EINVAL; 3995 } 3996 3997 t = btf_type_by_id(btf_vmlinux, args[arg].type); 3998 if (!btf_type_is_ptr(t) || !t->type) { 3999 /* anything but the pointer to struct is a helper config bug */ 4000 bpf_log(log, "ARG_PTR_TO_BTF is misconfigured\n"); 4001 return -EFAULT; 4002 } 4003 btf_id = t->type; 4004 t = btf_type_by_id(btf_vmlinux, t->type); 4005 /* skip modifiers */ 4006 while (btf_type_is_modifier(t)) { 4007 btf_id = t->type; 4008 t = btf_type_by_id(btf_vmlinux, t->type); 4009 } 4010 if (!btf_type_is_struct(t)) { 4011 bpf_log(log, "ARG_PTR_TO_BTF is not a struct\n"); 4012 return -EFAULT; 4013 } 4014 bpf_log(log, "helper %s arg%d has btf_id %d struct %s\n", fnname + 4, 4015 arg, btf_id, __btf_name_by_offset(btf_vmlinux, t->name_off)); 4016 return btf_id; 4017 } 4018 4019 int btf_resolve_helper_id(struct bpf_verifier_log *log, 4020 const struct bpf_func_proto *fn, int arg) 4021 { 4022 int *btf_id = &fn->btf_id[arg]; 4023 int ret; 4024 4025 if (fn->arg_type[arg] != ARG_PTR_TO_BTF_ID) 4026 return -EINVAL; 4027 4028 ret = READ_ONCE(*btf_id); 4029 if (ret) 4030 return ret; 4031 /* ok to race the search. The result is the same */ 4032 ret = __btf_resolve_helper_id(log, fn->func, arg); 4033 if (!ret) { 4034 /* Function argument cannot be type 'void' */ 4035 bpf_log(log, "BTF resolution bug\n"); 4036 return -EFAULT; 4037 } 4038 WRITE_ONCE(*btf_id, ret); 4039 return ret; 4040 } 4041 4042 static int __get_type_size(struct btf *btf, u32 btf_id, 4043 const struct btf_type **bad_type) 4044 { 4045 const struct btf_type *t; 4046 4047 if (!btf_id) 4048 /* void */ 4049 return 0; 4050 t = btf_type_by_id(btf, btf_id); 4051 while (t && btf_type_is_modifier(t)) 4052 t = btf_type_by_id(btf, t->type); 4053 if (!t) { 4054 *bad_type = btf->types[0]; 4055 return -EINVAL; 4056 } 4057 if (btf_type_is_ptr(t)) 4058 /* kernel size of pointer. Not BPF's size of pointer*/ 4059 return sizeof(void *); 4060 if (btf_type_is_int(t) || btf_type_is_enum(t)) 4061 return t->size; 4062 *bad_type = t; 4063 return -EINVAL; 4064 } 4065 4066 int btf_distill_func_proto(struct bpf_verifier_log *log, 4067 struct btf *btf, 4068 const struct btf_type *func, 4069 const char *tname, 4070 struct btf_func_model *m) 4071 { 4072 const struct btf_param *args; 4073 const struct btf_type *t; 4074 u32 i, nargs; 4075 int ret; 4076 4077 if (!func) { 4078 /* BTF function prototype doesn't match the verifier types. 4079 * Fall back to 5 u64 args. 4080 */ 4081 for (i = 0; i < 5; i++) 4082 m->arg_size[i] = 8; 4083 m->ret_size = 8; 4084 m->nr_args = 5; 4085 return 0; 4086 } 4087 args = (const struct btf_param *)(func + 1); 4088 nargs = btf_type_vlen(func); 4089 if (nargs >= MAX_BPF_FUNC_ARGS) { 4090 bpf_log(log, 4091 "The function %s has %d arguments. Too many.\n", 4092 tname, nargs); 4093 return -EINVAL; 4094 } 4095 ret = __get_type_size(btf, func->type, &t); 4096 if (ret < 0) { 4097 bpf_log(log, 4098 "The function %s return type %s is unsupported.\n", 4099 tname, btf_kind_str[BTF_INFO_KIND(t->info)]); 4100 return -EINVAL; 4101 } 4102 m->ret_size = ret; 4103 4104 for (i = 0; i < nargs; i++) { 4105 ret = __get_type_size(btf, args[i].type, &t); 4106 if (ret < 0) { 4107 bpf_log(log, 4108 "The function %s arg%d type %s is unsupported.\n", 4109 tname, i, btf_kind_str[BTF_INFO_KIND(t->info)]); 4110 return -EINVAL; 4111 } 4112 m->arg_size[i] = ret; 4113 } 4114 m->nr_args = nargs; 4115 return 0; 4116 } 4117 4118 /* Compare BTF of a function with given bpf_reg_state. 4119 * Returns: 4120 * EFAULT - there is a verifier bug. Abort verification. 4121 * EINVAL - there is a type mismatch or BTF is not available. 4122 * 0 - BTF matches with what bpf_reg_state expects. 4123 * Only PTR_TO_CTX and SCALAR_VALUE states are recognized. 4124 */ 4125 int btf_check_func_arg_match(struct bpf_verifier_env *env, int subprog, 4126 struct bpf_reg_state *reg) 4127 { 4128 struct bpf_verifier_log *log = &env->log; 4129 struct bpf_prog *prog = env->prog; 4130 struct btf *btf = prog->aux->btf; 4131 const struct btf_param *args; 4132 const struct btf_type *t; 4133 u32 i, nargs, btf_id; 4134 const char *tname; 4135 4136 if (!prog->aux->func_info) 4137 return -EINVAL; 4138 4139 btf_id = prog->aux->func_info[subprog].type_id; 4140 if (!btf_id) 4141 return -EFAULT; 4142 4143 if (prog->aux->func_info_aux[subprog].unreliable) 4144 return -EINVAL; 4145 4146 t = btf_type_by_id(btf, btf_id); 4147 if (!t || !btf_type_is_func(t)) { 4148 /* These checks were already done by the verifier while loading 4149 * struct bpf_func_info 4150 */ 4151 bpf_log(log, "BTF of func#%d doesn't point to KIND_FUNC\n", 4152 subprog); 4153 return -EFAULT; 4154 } 4155 tname = btf_name_by_offset(btf, t->name_off); 4156 4157 t = btf_type_by_id(btf, t->type); 4158 if (!t || !btf_type_is_func_proto(t)) { 4159 bpf_log(log, "Invalid BTF of func %s\n", tname); 4160 return -EFAULT; 4161 } 4162 args = (const struct btf_param *)(t + 1); 4163 nargs = btf_type_vlen(t); 4164 if (nargs > 5) { 4165 bpf_log(log, "Function %s has %d > 5 args\n", tname, nargs); 4166 goto out; 4167 } 4168 /* check that BTF function arguments match actual types that the 4169 * verifier sees. 4170 */ 4171 for (i = 0; i < nargs; i++) { 4172 t = btf_type_by_id(btf, args[i].type); 4173 while (btf_type_is_modifier(t)) 4174 t = btf_type_by_id(btf, t->type); 4175 if (btf_type_is_int(t) || btf_type_is_enum(t)) { 4176 if (reg[i + 1].type == SCALAR_VALUE) 4177 continue; 4178 bpf_log(log, "R%d is not a scalar\n", i + 1); 4179 goto out; 4180 } 4181 if (btf_type_is_ptr(t)) { 4182 if (reg[i + 1].type == SCALAR_VALUE) { 4183 bpf_log(log, "R%d is not a pointer\n", i + 1); 4184 goto out; 4185 } 4186 /* If function expects ctx type in BTF check that caller 4187 * is passing PTR_TO_CTX. 4188 */ 4189 if (btf_get_prog_ctx_type(log, btf, t, prog->type, i)) { 4190 if (reg[i + 1].type != PTR_TO_CTX) { 4191 bpf_log(log, 4192 "arg#%d expected pointer to ctx, but got %s\n", 4193 i, btf_kind_str[BTF_INFO_KIND(t->info)]); 4194 goto out; 4195 } 4196 if (check_ctx_reg(env, ®[i + 1], i + 1)) 4197 goto out; 4198 continue; 4199 } 4200 } 4201 bpf_log(log, "Unrecognized arg#%d type %s\n", 4202 i, btf_kind_str[BTF_INFO_KIND(t->info)]); 4203 goto out; 4204 } 4205 return 0; 4206 out: 4207 /* Compiler optimizations can remove arguments from static functions 4208 * or mismatched type can be passed into a global function. 4209 * In such cases mark the function as unreliable from BTF point of view. 4210 */ 4211 prog->aux->func_info_aux[subprog].unreliable = true; 4212 return -EINVAL; 4213 } 4214 4215 /* Convert BTF of a function into bpf_reg_state if possible 4216 * Returns: 4217 * EFAULT - there is a verifier bug. Abort verification. 4218 * EINVAL - cannot convert BTF. 4219 * 0 - Successfully converted BTF into bpf_reg_state 4220 * (either PTR_TO_CTX or SCALAR_VALUE). 4221 */ 4222 int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog, 4223 struct bpf_reg_state *reg) 4224 { 4225 struct bpf_verifier_log *log = &env->log; 4226 struct bpf_prog *prog = env->prog; 4227 struct btf *btf = prog->aux->btf; 4228 const struct btf_param *args; 4229 const struct btf_type *t; 4230 u32 i, nargs, btf_id; 4231 const char *tname; 4232 4233 if (!prog->aux->func_info || 4234 prog->aux->func_info_aux[subprog].linkage != BTF_FUNC_GLOBAL) { 4235 bpf_log(log, "Verifier bug\n"); 4236 return -EFAULT; 4237 } 4238 4239 btf_id = prog->aux->func_info[subprog].type_id; 4240 if (!btf_id) { 4241 bpf_log(log, "Global functions need valid BTF\n"); 4242 return -EFAULT; 4243 } 4244 4245 t = btf_type_by_id(btf, btf_id); 4246 if (!t || !btf_type_is_func(t)) { 4247 /* These checks were already done by the verifier while loading 4248 * struct bpf_func_info 4249 */ 4250 bpf_log(log, "BTF of func#%d doesn't point to KIND_FUNC\n", 4251 subprog); 4252 return -EFAULT; 4253 } 4254 tname = btf_name_by_offset(btf, t->name_off); 4255 4256 if (log->level & BPF_LOG_LEVEL) 4257 bpf_log(log, "Validating %s() func#%d...\n", 4258 tname, subprog); 4259 4260 if (prog->aux->func_info_aux[subprog].unreliable) { 4261 bpf_log(log, "Verifier bug in function %s()\n", tname); 4262 return -EFAULT; 4263 } 4264 4265 t = btf_type_by_id(btf, t->type); 4266 if (!t || !btf_type_is_func_proto(t)) { 4267 bpf_log(log, "Invalid type of function %s()\n", tname); 4268 return -EFAULT; 4269 } 4270 args = (const struct btf_param *)(t + 1); 4271 nargs = btf_type_vlen(t); 4272 if (nargs > 5) { 4273 bpf_log(log, "Global function %s() with %d > 5 args. Buggy compiler.\n", 4274 tname, nargs); 4275 return -EINVAL; 4276 } 4277 /* check that function returns int */ 4278 t = btf_type_by_id(btf, t->type); 4279 while (btf_type_is_modifier(t)) 4280 t = btf_type_by_id(btf, t->type); 4281 if (!btf_type_is_int(t) && !btf_type_is_enum(t)) { 4282 bpf_log(log, 4283 "Global function %s() doesn't return scalar. Only those are supported.\n", 4284 tname); 4285 return -EINVAL; 4286 } 4287 /* Convert BTF function arguments into verifier types. 4288 * Only PTR_TO_CTX and SCALAR are supported atm. 4289 */ 4290 for (i = 0; i < nargs; i++) { 4291 t = btf_type_by_id(btf, args[i].type); 4292 while (btf_type_is_modifier(t)) 4293 t = btf_type_by_id(btf, t->type); 4294 if (btf_type_is_int(t) || btf_type_is_enum(t)) { 4295 reg[i + 1].type = SCALAR_VALUE; 4296 continue; 4297 } 4298 if (btf_type_is_ptr(t) && 4299 btf_get_prog_ctx_type(log, btf, t, prog->type, i)) { 4300 reg[i + 1].type = PTR_TO_CTX; 4301 continue; 4302 } 4303 bpf_log(log, "Arg#%d type %s in %s() is not supported yet.\n", 4304 i, btf_kind_str[BTF_INFO_KIND(t->info)], tname); 4305 return -EINVAL; 4306 } 4307 return 0; 4308 } 4309 4310 void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj, 4311 struct seq_file *m) 4312 { 4313 const struct btf_type *t = btf_type_by_id(btf, type_id); 4314 4315 btf_type_ops(t)->seq_show(btf, t, type_id, obj, 0, m); 4316 } 4317 4318 #ifdef CONFIG_PROC_FS 4319 static void bpf_btf_show_fdinfo(struct seq_file *m, struct file *filp) 4320 { 4321 const struct btf *btf = filp->private_data; 4322 4323 seq_printf(m, "btf_id:\t%u\n", btf->id); 4324 } 4325 #endif 4326 4327 static int btf_release(struct inode *inode, struct file *filp) 4328 { 4329 btf_put(filp->private_data); 4330 return 0; 4331 } 4332 4333 const struct file_operations btf_fops = { 4334 #ifdef CONFIG_PROC_FS 4335 .show_fdinfo = bpf_btf_show_fdinfo, 4336 #endif 4337 .release = btf_release, 4338 }; 4339 4340 static int __btf_new_fd(struct btf *btf) 4341 { 4342 return anon_inode_getfd("btf", &btf_fops, btf, O_RDONLY | O_CLOEXEC); 4343 } 4344 4345 int btf_new_fd(const union bpf_attr *attr) 4346 { 4347 struct btf *btf; 4348 int ret; 4349 4350 btf = btf_parse(u64_to_user_ptr(attr->btf), 4351 attr->btf_size, attr->btf_log_level, 4352 u64_to_user_ptr(attr->btf_log_buf), 4353 attr->btf_log_size); 4354 if (IS_ERR(btf)) 4355 return PTR_ERR(btf); 4356 4357 ret = btf_alloc_id(btf); 4358 if (ret) { 4359 btf_free(btf); 4360 return ret; 4361 } 4362 4363 /* 4364 * The BTF ID is published to the userspace. 4365 * All BTF free must go through call_rcu() from 4366 * now on (i.e. free by calling btf_put()). 4367 */ 4368 4369 ret = __btf_new_fd(btf); 4370 if (ret < 0) 4371 btf_put(btf); 4372 4373 return ret; 4374 } 4375 4376 struct btf *btf_get_by_fd(int fd) 4377 { 4378 struct btf *btf; 4379 struct fd f; 4380 4381 f = fdget(fd); 4382 4383 if (!f.file) 4384 return ERR_PTR(-EBADF); 4385 4386 if (f.file->f_op != &btf_fops) { 4387 fdput(f); 4388 return ERR_PTR(-EINVAL); 4389 } 4390 4391 btf = f.file->private_data; 4392 refcount_inc(&btf->refcnt); 4393 fdput(f); 4394 4395 return btf; 4396 } 4397 4398 int btf_get_info_by_fd(const struct btf *btf, 4399 const union bpf_attr *attr, 4400 union bpf_attr __user *uattr) 4401 { 4402 struct bpf_btf_info __user *uinfo; 4403 struct bpf_btf_info info = {}; 4404 u32 info_copy, btf_copy; 4405 void __user *ubtf; 4406 u32 uinfo_len; 4407 4408 uinfo = u64_to_user_ptr(attr->info.info); 4409 uinfo_len = attr->info.info_len; 4410 4411 info_copy = min_t(u32, uinfo_len, sizeof(info)); 4412 if (copy_from_user(&info, uinfo, info_copy)) 4413 return -EFAULT; 4414 4415 info.id = btf->id; 4416 ubtf = u64_to_user_ptr(info.btf); 4417 btf_copy = min_t(u32, btf->data_size, info.btf_size); 4418 if (copy_to_user(ubtf, btf->data, btf_copy)) 4419 return -EFAULT; 4420 info.btf_size = btf->data_size; 4421 4422 if (copy_to_user(uinfo, &info, info_copy) || 4423 put_user(info_copy, &uattr->info.info_len)) 4424 return -EFAULT; 4425 4426 return 0; 4427 } 4428 4429 int btf_get_fd_by_id(u32 id) 4430 { 4431 struct btf *btf; 4432 int fd; 4433 4434 rcu_read_lock(); 4435 btf = idr_find(&btf_idr, id); 4436 if (!btf || !refcount_inc_not_zero(&btf->refcnt)) 4437 btf = ERR_PTR(-ENOENT); 4438 rcu_read_unlock(); 4439 4440 if (IS_ERR(btf)) 4441 return PTR_ERR(btf); 4442 4443 fd = __btf_new_fd(btf); 4444 if (fd < 0) 4445 btf_put(btf); 4446 4447 return fd; 4448 } 4449 4450 u32 btf_id(const struct btf *btf) 4451 { 4452 return btf->id; 4453 } 4454