1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* Copyright (c) 2018 Facebook */ 3 4 #include <uapi/linux/btf.h> 5 #include <uapi/linux/types.h> 6 #include <linux/seq_file.h> 7 #include <linux/compiler.h> 8 #include <linux/ctype.h> 9 #include <linux/errno.h> 10 #include <linux/slab.h> 11 #include <linux/anon_inodes.h> 12 #include <linux/file.h> 13 #include <linux/uaccess.h> 14 #include <linux/kernel.h> 15 #include <linux/idr.h> 16 #include <linux/sort.h> 17 #include <linux/bpf_verifier.h> 18 #include <linux/btf.h> 19 20 /* BTF (BPF Type Format) is the meta data format which describes 21 * the data types of BPF program/map. Hence, it basically focus 22 * on the C programming language which the modern BPF is primary 23 * using. 24 * 25 * ELF Section: 26 * ~~~~~~~~~~~ 27 * The BTF data is stored under the ".BTF" ELF section 28 * 29 * struct btf_type: 30 * ~~~~~~~~~~~~~~~ 31 * Each 'struct btf_type' object describes a C data type. 32 * Depending on the type it is describing, a 'struct btf_type' 33 * object may be followed by more data. F.e. 34 * To describe an array, 'struct btf_type' is followed by 35 * 'struct btf_array'. 36 * 37 * 'struct btf_type' and any extra data following it are 38 * 4 bytes aligned. 39 * 40 * Type section: 41 * ~~~~~~~~~~~~~ 42 * The BTF type section contains a list of 'struct btf_type' objects. 43 * Each one describes a C type. Recall from the above section 44 * that a 'struct btf_type' object could be immediately followed by extra 45 * data in order to desribe some particular C types. 46 * 47 * type_id: 48 * ~~~~~~~ 49 * Each btf_type object is identified by a type_id. The type_id 50 * is implicitly implied by the location of the btf_type object in 51 * the BTF type section. The first one has type_id 1. The second 52 * one has type_id 2...etc. Hence, an earlier btf_type has 53 * a smaller type_id. 54 * 55 * A btf_type object may refer to another btf_type object by using 56 * type_id (i.e. the "type" in the "struct btf_type"). 57 * 58 * NOTE that we cannot assume any reference-order. 59 * A btf_type object can refer to an earlier btf_type object 60 * but it can also refer to a later btf_type object. 61 * 62 * For example, to describe "const void *". A btf_type 63 * object describing "const" may refer to another btf_type 64 * object describing "void *". This type-reference is done 65 * by specifying type_id: 66 * 67 * [1] CONST (anon) type_id=2 68 * [2] PTR (anon) type_id=0 69 * 70 * The above is the btf_verifier debug log: 71 * - Each line started with "[?]" is a btf_type object 72 * - [?] is the type_id of the btf_type object. 73 * - CONST/PTR is the BTF_KIND_XXX 74 * - "(anon)" is the name of the type. It just 75 * happens that CONST and PTR has no name. 76 * - type_id=XXX is the 'u32 type' in btf_type 77 * 78 * NOTE: "void" has type_id 0 79 * 80 * String section: 81 * ~~~~~~~~~~~~~~ 82 * The BTF string section contains the names used by the type section. 83 * Each string is referred by an "offset" from the beginning of the 84 * string section. 85 * 86 * Each string is '\0' terminated. 87 * 88 * The first character in the string section must be '\0' 89 * which is used to mean 'anonymous'. Some btf_type may not 90 * have a name. 91 */ 92 93 /* BTF verification: 94 * 95 * To verify BTF data, two passes are needed. 96 * 97 * Pass #1 98 * ~~~~~~~ 99 * The first pass is to collect all btf_type objects to 100 * an array: "btf->types". 101 * 102 * Depending on the C type that a btf_type is describing, 103 * a btf_type may be followed by extra data. We don't know 104 * how many btf_type is there, and more importantly we don't 105 * know where each btf_type is located in the type section. 106 * 107 * Without knowing the location of each type_id, most verifications 108 * cannot be done. e.g. an earlier btf_type may refer to a later 109 * btf_type (recall the "const void *" above), so we cannot 110 * check this type-reference in the first pass. 111 * 112 * In the first pass, it still does some verifications (e.g. 113 * checking the name is a valid offset to the string section). 114 * 115 * Pass #2 116 * ~~~~~~~ 117 * The main focus is to resolve a btf_type that is referring 118 * to another type. 119 * 120 * We have to ensure the referring type: 121 * 1) does exist in the BTF (i.e. in btf->types[]) 122 * 2) does not cause a loop: 123 * struct A { 124 * struct B b; 125 * }; 126 * 127 * struct B { 128 * struct A a; 129 * }; 130 * 131 * btf_type_needs_resolve() decides if a btf_type needs 132 * to be resolved. 133 * 134 * The needs_resolve type implements the "resolve()" ops which 135 * essentially does a DFS and detects backedge. 136 * 137 * During resolve (or DFS), different C types have different 138 * "RESOLVED" conditions. 139 * 140 * When resolving a BTF_KIND_STRUCT, we need to resolve all its 141 * members because a member is always referring to another 142 * type. A struct's member can be treated as "RESOLVED" if 143 * it is referring to a BTF_KIND_PTR. Otherwise, the 144 * following valid C struct would be rejected: 145 * 146 * struct A { 147 * int m; 148 * struct A *a; 149 * }; 150 * 151 * When resolving a BTF_KIND_PTR, it needs to keep resolving if 152 * it is referring to another BTF_KIND_PTR. Otherwise, we cannot 153 * detect a pointer loop, e.g.: 154 * BTF_KIND_CONST -> BTF_KIND_PTR -> BTF_KIND_CONST -> BTF_KIND_PTR + 155 * ^ | 156 * +-----------------------------------------+ 157 * 158 */ 159 160 #define BITS_PER_U64 (sizeof(u64) * BITS_PER_BYTE) 161 #define BITS_PER_BYTE_MASK (BITS_PER_BYTE - 1) 162 #define BITS_PER_BYTE_MASKED(bits) ((bits) & BITS_PER_BYTE_MASK) 163 #define BITS_ROUNDDOWN_BYTES(bits) ((bits) >> 3) 164 #define BITS_ROUNDUP_BYTES(bits) \ 165 (BITS_ROUNDDOWN_BYTES(bits) + !!BITS_PER_BYTE_MASKED(bits)) 166 167 #define BTF_INFO_MASK 0x0f00ffff 168 #define BTF_INT_MASK 0x0fffffff 169 #define BTF_TYPE_ID_VALID(type_id) ((type_id) <= BTF_MAX_TYPE) 170 #define BTF_STR_OFFSET_VALID(name_off) ((name_off) <= BTF_MAX_NAME_OFFSET) 171 172 /* 16MB for 64k structs and each has 16 members and 173 * a few MB spaces for the string section. 174 * The hard limit is S32_MAX. 175 */ 176 #define BTF_MAX_SIZE (16 * 1024 * 1024) 177 178 #define for_each_member(i, struct_type, member) \ 179 for (i = 0, member = btf_type_member(struct_type); \ 180 i < btf_type_vlen(struct_type); \ 181 i++, member++) 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 static DEFINE_IDR(btf_idr); 189 static DEFINE_SPINLOCK(btf_idr_lock); 190 191 struct btf { 192 void *data; 193 struct btf_type **types; 194 u32 *resolved_ids; 195 u32 *resolved_sizes; 196 const char *strings; 197 void *nohdr_data; 198 struct btf_header hdr; 199 u32 nr_types; 200 u32 types_size; 201 u32 data_size; 202 refcount_t refcnt; 203 u32 id; 204 struct rcu_head rcu; 205 }; 206 207 enum verifier_phase { 208 CHECK_META, 209 CHECK_TYPE, 210 }; 211 212 struct resolve_vertex { 213 const struct btf_type *t; 214 u32 type_id; 215 u16 next_member; 216 }; 217 218 enum visit_state { 219 NOT_VISITED, 220 VISITED, 221 RESOLVED, 222 }; 223 224 enum resolve_mode { 225 RESOLVE_TBD, /* To Be Determined */ 226 RESOLVE_PTR, /* Resolving for Pointer */ 227 RESOLVE_STRUCT_OR_ARRAY, /* Resolving for struct/union 228 * or array 229 */ 230 }; 231 232 #define MAX_RESOLVE_DEPTH 32 233 234 struct btf_sec_info { 235 u32 off; 236 u32 len; 237 }; 238 239 struct btf_verifier_env { 240 struct btf *btf; 241 u8 *visit_states; 242 struct resolve_vertex stack[MAX_RESOLVE_DEPTH]; 243 struct bpf_verifier_log log; 244 u32 log_type_id; 245 u32 top_stack; 246 enum verifier_phase phase; 247 enum resolve_mode resolve_mode; 248 }; 249 250 static const char * const btf_kind_str[NR_BTF_KINDS] = { 251 [BTF_KIND_UNKN] = "UNKNOWN", 252 [BTF_KIND_INT] = "INT", 253 [BTF_KIND_PTR] = "PTR", 254 [BTF_KIND_ARRAY] = "ARRAY", 255 [BTF_KIND_STRUCT] = "STRUCT", 256 [BTF_KIND_UNION] = "UNION", 257 [BTF_KIND_ENUM] = "ENUM", 258 [BTF_KIND_FWD] = "FWD", 259 [BTF_KIND_TYPEDEF] = "TYPEDEF", 260 [BTF_KIND_VOLATILE] = "VOLATILE", 261 [BTF_KIND_CONST] = "CONST", 262 [BTF_KIND_RESTRICT] = "RESTRICT", 263 [BTF_KIND_FUNC] = "FUNC", 264 [BTF_KIND_FUNC_PROTO] = "FUNC_PROTO", 265 }; 266 267 struct btf_kind_operations { 268 s32 (*check_meta)(struct btf_verifier_env *env, 269 const struct btf_type *t, 270 u32 meta_left); 271 int (*resolve)(struct btf_verifier_env *env, 272 const struct resolve_vertex *v); 273 int (*check_member)(struct btf_verifier_env *env, 274 const struct btf_type *struct_type, 275 const struct btf_member *member, 276 const struct btf_type *member_type); 277 void (*log_details)(struct btf_verifier_env *env, 278 const struct btf_type *t); 279 void (*seq_show)(const struct btf *btf, const struct btf_type *t, 280 u32 type_id, void *data, u8 bits_offsets, 281 struct seq_file *m); 282 }; 283 284 static const struct btf_kind_operations * const kind_ops[NR_BTF_KINDS]; 285 static struct btf_type btf_void; 286 287 static int btf_resolve(struct btf_verifier_env *env, 288 const struct btf_type *t, u32 type_id); 289 290 static bool btf_type_is_modifier(const struct btf_type *t) 291 { 292 /* Some of them is not strictly a C modifier 293 * but they are grouped into the same bucket 294 * for BTF concern: 295 * A type (t) that refers to another 296 * type through t->type AND its size cannot 297 * be determined without following the t->type. 298 * 299 * ptr does not fall into this bucket 300 * because its size is always sizeof(void *). 301 */ 302 switch (BTF_INFO_KIND(t->info)) { 303 case BTF_KIND_TYPEDEF: 304 case BTF_KIND_VOLATILE: 305 case BTF_KIND_CONST: 306 case BTF_KIND_RESTRICT: 307 return true; 308 } 309 310 return false; 311 } 312 313 static bool btf_type_is_void(const struct btf_type *t) 314 { 315 return t == &btf_void; 316 } 317 318 static bool btf_type_is_fwd(const struct btf_type *t) 319 { 320 return BTF_INFO_KIND(t->info) == BTF_KIND_FWD; 321 } 322 323 static bool btf_type_is_func(const struct btf_type *t) 324 { 325 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC; 326 } 327 328 static bool btf_type_is_func_proto(const struct btf_type *t) 329 { 330 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC_PROTO; 331 } 332 333 static bool btf_type_nosize(const struct btf_type *t) 334 { 335 return btf_type_is_void(t) || btf_type_is_fwd(t) || 336 btf_type_is_func(t) || btf_type_is_func_proto(t); 337 } 338 339 static bool btf_type_nosize_or_null(const struct btf_type *t) 340 { 341 return !t || btf_type_nosize(t); 342 } 343 344 /* union is only a special case of struct: 345 * all its offsetof(member) == 0 346 */ 347 static bool btf_type_is_struct(const struct btf_type *t) 348 { 349 u8 kind = BTF_INFO_KIND(t->info); 350 351 return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION; 352 } 353 354 static bool btf_type_is_array(const struct btf_type *t) 355 { 356 return BTF_INFO_KIND(t->info) == BTF_KIND_ARRAY; 357 } 358 359 static bool btf_type_is_ptr(const struct btf_type *t) 360 { 361 return BTF_INFO_KIND(t->info) == BTF_KIND_PTR; 362 } 363 364 static bool btf_type_is_int(const struct btf_type *t) 365 { 366 return BTF_INFO_KIND(t->info) == BTF_KIND_INT; 367 } 368 369 /* What types need to be resolved? 370 * 371 * btf_type_is_modifier() is an obvious one. 372 * 373 * btf_type_is_struct() because its member refers to 374 * another type (through member->type). 375 376 * btf_type_is_array() because its element (array->type) 377 * refers to another type. Array can be thought of a 378 * special case of struct while array just has the same 379 * member-type repeated by array->nelems of times. 380 */ 381 static bool btf_type_needs_resolve(const struct btf_type *t) 382 { 383 return btf_type_is_modifier(t) || 384 btf_type_is_ptr(t) || 385 btf_type_is_struct(t) || 386 btf_type_is_array(t); 387 } 388 389 /* t->size can be used */ 390 static bool btf_type_has_size(const struct btf_type *t) 391 { 392 switch (BTF_INFO_KIND(t->info)) { 393 case BTF_KIND_INT: 394 case BTF_KIND_STRUCT: 395 case BTF_KIND_UNION: 396 case BTF_KIND_ENUM: 397 return true; 398 } 399 400 return false; 401 } 402 403 static const char *btf_int_encoding_str(u8 encoding) 404 { 405 if (encoding == 0) 406 return "(none)"; 407 else if (encoding == BTF_INT_SIGNED) 408 return "SIGNED"; 409 else if (encoding == BTF_INT_CHAR) 410 return "CHAR"; 411 else if (encoding == BTF_INT_BOOL) 412 return "BOOL"; 413 else 414 return "UNKN"; 415 } 416 417 static u16 btf_type_vlen(const struct btf_type *t) 418 { 419 return BTF_INFO_VLEN(t->info); 420 } 421 422 static u32 btf_type_int(const struct btf_type *t) 423 { 424 return *(u32 *)(t + 1); 425 } 426 427 static const struct btf_array *btf_type_array(const struct btf_type *t) 428 { 429 return (const struct btf_array *)(t + 1); 430 } 431 432 static const struct btf_member *btf_type_member(const struct btf_type *t) 433 { 434 return (const struct btf_member *)(t + 1); 435 } 436 437 static const struct btf_enum *btf_type_enum(const struct btf_type *t) 438 { 439 return (const struct btf_enum *)(t + 1); 440 } 441 442 static const struct btf_kind_operations *btf_type_ops(const struct btf_type *t) 443 { 444 return kind_ops[BTF_INFO_KIND(t->info)]; 445 } 446 447 bool btf_name_offset_valid(const struct btf *btf, u32 offset) 448 { 449 return BTF_STR_OFFSET_VALID(offset) && 450 offset < btf->hdr.str_len; 451 } 452 453 /* Only C-style identifier is permitted. This can be relaxed if 454 * necessary. 455 */ 456 static bool btf_name_valid_identifier(const struct btf *btf, u32 offset) 457 { 458 /* offset must be valid */ 459 const char *src = &btf->strings[offset]; 460 const char *src_limit; 461 462 if (!isalpha(*src) && *src != '_') 463 return false; 464 465 /* set a limit on identifier length */ 466 src_limit = src + KSYM_NAME_LEN; 467 src++; 468 while (*src && src < src_limit) { 469 if (!isalnum(*src) && *src != '_') 470 return false; 471 src++; 472 } 473 474 return !*src; 475 } 476 477 const char *btf_name_by_offset(const struct btf *btf, u32 offset) 478 { 479 if (!offset) 480 return "(anon)"; 481 else if (offset < btf->hdr.str_len) 482 return &btf->strings[offset]; 483 else 484 return "(invalid-name-offset)"; 485 } 486 487 const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id) 488 { 489 if (type_id > btf->nr_types) 490 return NULL; 491 492 return btf->types[type_id]; 493 } 494 495 /* 496 * Regular int is not a bit field and it must be either 497 * u8/u16/u32/u64. 498 */ 499 static bool btf_type_int_is_regular(const struct btf_type *t) 500 { 501 u8 nr_bits, nr_bytes; 502 u32 int_data; 503 504 int_data = btf_type_int(t); 505 nr_bits = BTF_INT_BITS(int_data); 506 nr_bytes = BITS_ROUNDUP_BYTES(nr_bits); 507 if (BITS_PER_BYTE_MASKED(nr_bits) || 508 BTF_INT_OFFSET(int_data) || 509 (nr_bytes != sizeof(u8) && nr_bytes != sizeof(u16) && 510 nr_bytes != sizeof(u32) && nr_bytes != sizeof(u64))) { 511 return false; 512 } 513 514 return true; 515 } 516 517 /* 518 * Check that given type is a regular int and has the expected size. 519 */ 520 bool btf_type_is_reg_int(const struct btf_type *t, u32 expected_size) 521 { 522 u8 nr_bits, nr_bytes; 523 u32 int_data; 524 525 if (!btf_type_is_int(t)) 526 return false; 527 528 int_data = btf_type_int(t); 529 nr_bits = BTF_INT_BITS(int_data); 530 nr_bytes = BITS_ROUNDUP_BYTES(nr_bits); 531 if (BITS_PER_BYTE_MASKED(nr_bits) || 532 BTF_INT_OFFSET(int_data) || 533 nr_bytes != expected_size) 534 return false; 535 536 return true; 537 } 538 539 __printf(2, 3) static void __btf_verifier_log(struct bpf_verifier_log *log, 540 const char *fmt, ...) 541 { 542 va_list args; 543 544 va_start(args, fmt); 545 bpf_verifier_vlog(log, fmt, args); 546 va_end(args); 547 } 548 549 __printf(2, 3) static void btf_verifier_log(struct btf_verifier_env *env, 550 const char *fmt, ...) 551 { 552 struct bpf_verifier_log *log = &env->log; 553 va_list args; 554 555 if (!bpf_verifier_log_needed(log)) 556 return; 557 558 va_start(args, fmt); 559 bpf_verifier_vlog(log, fmt, args); 560 va_end(args); 561 } 562 563 __printf(4, 5) static void __btf_verifier_log_type(struct btf_verifier_env *env, 564 const struct btf_type *t, 565 bool log_details, 566 const char *fmt, ...) 567 { 568 struct bpf_verifier_log *log = &env->log; 569 u8 kind = BTF_INFO_KIND(t->info); 570 struct btf *btf = env->btf; 571 va_list args; 572 573 if (!bpf_verifier_log_needed(log)) 574 return; 575 576 __btf_verifier_log(log, "[%u] %s %s%s", 577 env->log_type_id, 578 btf_kind_str[kind], 579 btf_name_by_offset(btf, t->name_off), 580 log_details ? " " : ""); 581 582 if (log_details) 583 btf_type_ops(t)->log_details(env, t); 584 585 if (fmt && *fmt) { 586 __btf_verifier_log(log, " "); 587 va_start(args, fmt); 588 bpf_verifier_vlog(log, fmt, args); 589 va_end(args); 590 } 591 592 __btf_verifier_log(log, "\n"); 593 } 594 595 #define btf_verifier_log_type(env, t, ...) \ 596 __btf_verifier_log_type((env), (t), true, __VA_ARGS__) 597 #define btf_verifier_log_basic(env, t, ...) \ 598 __btf_verifier_log_type((env), (t), false, __VA_ARGS__) 599 600 __printf(4, 5) 601 static void btf_verifier_log_member(struct btf_verifier_env *env, 602 const struct btf_type *struct_type, 603 const struct btf_member *member, 604 const char *fmt, ...) 605 { 606 struct bpf_verifier_log *log = &env->log; 607 struct btf *btf = env->btf; 608 va_list args; 609 610 if (!bpf_verifier_log_needed(log)) 611 return; 612 613 /* The CHECK_META phase already did a btf dump. 614 * 615 * If member is logged again, it must hit an error in 616 * parsing this member. It is useful to print out which 617 * struct this member belongs to. 618 */ 619 if (env->phase != CHECK_META) 620 btf_verifier_log_type(env, struct_type, NULL); 621 622 __btf_verifier_log(log, "\t%s type_id=%u bits_offset=%u", 623 btf_name_by_offset(btf, member->name_off), 624 member->type, member->offset); 625 626 if (fmt && *fmt) { 627 __btf_verifier_log(log, " "); 628 va_start(args, fmt); 629 bpf_verifier_vlog(log, fmt, args); 630 va_end(args); 631 } 632 633 __btf_verifier_log(log, "\n"); 634 } 635 636 static void btf_verifier_log_hdr(struct btf_verifier_env *env, 637 u32 btf_data_size) 638 { 639 struct bpf_verifier_log *log = &env->log; 640 const struct btf *btf = env->btf; 641 const struct btf_header *hdr; 642 643 if (!bpf_verifier_log_needed(log)) 644 return; 645 646 hdr = &btf->hdr; 647 __btf_verifier_log(log, "magic: 0x%x\n", hdr->magic); 648 __btf_verifier_log(log, "version: %u\n", hdr->version); 649 __btf_verifier_log(log, "flags: 0x%x\n", hdr->flags); 650 __btf_verifier_log(log, "hdr_len: %u\n", hdr->hdr_len); 651 __btf_verifier_log(log, "type_off: %u\n", hdr->type_off); 652 __btf_verifier_log(log, "type_len: %u\n", hdr->type_len); 653 __btf_verifier_log(log, "str_off: %u\n", hdr->str_off); 654 __btf_verifier_log(log, "str_len: %u\n", hdr->str_len); 655 __btf_verifier_log(log, "btf_total_size: %u\n", btf_data_size); 656 } 657 658 static int btf_add_type(struct btf_verifier_env *env, struct btf_type *t) 659 { 660 struct btf *btf = env->btf; 661 662 /* < 2 because +1 for btf_void which is always in btf->types[0]. 663 * btf_void is not accounted in btf->nr_types because btf_void 664 * does not come from the BTF file. 665 */ 666 if (btf->types_size - btf->nr_types < 2) { 667 /* Expand 'types' array */ 668 669 struct btf_type **new_types; 670 u32 expand_by, new_size; 671 672 if (btf->types_size == BTF_MAX_TYPE) { 673 btf_verifier_log(env, "Exceeded max num of types"); 674 return -E2BIG; 675 } 676 677 expand_by = max_t(u32, btf->types_size >> 2, 16); 678 new_size = min_t(u32, BTF_MAX_TYPE, 679 btf->types_size + expand_by); 680 681 new_types = kvcalloc(new_size, sizeof(*new_types), 682 GFP_KERNEL | __GFP_NOWARN); 683 if (!new_types) 684 return -ENOMEM; 685 686 if (btf->nr_types == 0) 687 new_types[0] = &btf_void; 688 else 689 memcpy(new_types, btf->types, 690 sizeof(*btf->types) * (btf->nr_types + 1)); 691 692 kvfree(btf->types); 693 btf->types = new_types; 694 btf->types_size = new_size; 695 } 696 697 btf->types[++(btf->nr_types)] = t; 698 699 return 0; 700 } 701 702 static int btf_alloc_id(struct btf *btf) 703 { 704 int id; 705 706 idr_preload(GFP_KERNEL); 707 spin_lock_bh(&btf_idr_lock); 708 id = idr_alloc_cyclic(&btf_idr, btf, 1, INT_MAX, GFP_ATOMIC); 709 if (id > 0) 710 btf->id = id; 711 spin_unlock_bh(&btf_idr_lock); 712 idr_preload_end(); 713 714 if (WARN_ON_ONCE(!id)) 715 return -ENOSPC; 716 717 return id > 0 ? 0 : id; 718 } 719 720 static void btf_free_id(struct btf *btf) 721 { 722 unsigned long flags; 723 724 /* 725 * In map-in-map, calling map_delete_elem() on outer 726 * map will call bpf_map_put on the inner map. 727 * It will then eventually call btf_free_id() 728 * on the inner map. Some of the map_delete_elem() 729 * implementation may have irq disabled, so 730 * we need to use the _irqsave() version instead 731 * of the _bh() version. 732 */ 733 spin_lock_irqsave(&btf_idr_lock, flags); 734 idr_remove(&btf_idr, btf->id); 735 spin_unlock_irqrestore(&btf_idr_lock, flags); 736 } 737 738 static void btf_free(struct btf *btf) 739 { 740 kvfree(btf->types); 741 kvfree(btf->resolved_sizes); 742 kvfree(btf->resolved_ids); 743 kvfree(btf->data); 744 kfree(btf); 745 } 746 747 static void btf_free_rcu(struct rcu_head *rcu) 748 { 749 struct btf *btf = container_of(rcu, struct btf, rcu); 750 751 btf_free(btf); 752 } 753 754 void btf_put(struct btf *btf) 755 { 756 if (btf && refcount_dec_and_test(&btf->refcnt)) { 757 btf_free_id(btf); 758 call_rcu(&btf->rcu, btf_free_rcu); 759 } 760 } 761 762 static int env_resolve_init(struct btf_verifier_env *env) 763 { 764 struct btf *btf = env->btf; 765 u32 nr_types = btf->nr_types; 766 u32 *resolved_sizes = NULL; 767 u32 *resolved_ids = NULL; 768 u8 *visit_states = NULL; 769 770 /* +1 for btf_void */ 771 resolved_sizes = kvcalloc(nr_types + 1, sizeof(*resolved_sizes), 772 GFP_KERNEL | __GFP_NOWARN); 773 if (!resolved_sizes) 774 goto nomem; 775 776 resolved_ids = kvcalloc(nr_types + 1, sizeof(*resolved_ids), 777 GFP_KERNEL | __GFP_NOWARN); 778 if (!resolved_ids) 779 goto nomem; 780 781 visit_states = kvcalloc(nr_types + 1, sizeof(*visit_states), 782 GFP_KERNEL | __GFP_NOWARN); 783 if (!visit_states) 784 goto nomem; 785 786 btf->resolved_sizes = resolved_sizes; 787 btf->resolved_ids = resolved_ids; 788 env->visit_states = visit_states; 789 790 return 0; 791 792 nomem: 793 kvfree(resolved_sizes); 794 kvfree(resolved_ids); 795 kvfree(visit_states); 796 return -ENOMEM; 797 } 798 799 static void btf_verifier_env_free(struct btf_verifier_env *env) 800 { 801 kvfree(env->visit_states); 802 kfree(env); 803 } 804 805 static bool env_type_is_resolve_sink(const struct btf_verifier_env *env, 806 const struct btf_type *next_type) 807 { 808 switch (env->resolve_mode) { 809 case RESOLVE_TBD: 810 /* int, enum or void is a sink */ 811 return !btf_type_needs_resolve(next_type); 812 case RESOLVE_PTR: 813 /* int, enum, void, struct, array, func or func_proto is a sink 814 * for ptr 815 */ 816 return !btf_type_is_modifier(next_type) && 817 !btf_type_is_ptr(next_type); 818 case RESOLVE_STRUCT_OR_ARRAY: 819 /* int, enum, void, ptr, func or func_proto is a sink 820 * for struct and array 821 */ 822 return !btf_type_is_modifier(next_type) && 823 !btf_type_is_array(next_type) && 824 !btf_type_is_struct(next_type); 825 default: 826 BUG(); 827 } 828 } 829 830 static bool env_type_is_resolved(const struct btf_verifier_env *env, 831 u32 type_id) 832 { 833 return env->visit_states[type_id] == RESOLVED; 834 } 835 836 static int env_stack_push(struct btf_verifier_env *env, 837 const struct btf_type *t, u32 type_id) 838 { 839 struct resolve_vertex *v; 840 841 if (env->top_stack == MAX_RESOLVE_DEPTH) 842 return -E2BIG; 843 844 if (env->visit_states[type_id] != NOT_VISITED) 845 return -EEXIST; 846 847 env->visit_states[type_id] = VISITED; 848 849 v = &env->stack[env->top_stack++]; 850 v->t = t; 851 v->type_id = type_id; 852 v->next_member = 0; 853 854 if (env->resolve_mode == RESOLVE_TBD) { 855 if (btf_type_is_ptr(t)) 856 env->resolve_mode = RESOLVE_PTR; 857 else if (btf_type_is_struct(t) || btf_type_is_array(t)) 858 env->resolve_mode = RESOLVE_STRUCT_OR_ARRAY; 859 } 860 861 return 0; 862 } 863 864 static void env_stack_set_next_member(struct btf_verifier_env *env, 865 u16 next_member) 866 { 867 env->stack[env->top_stack - 1].next_member = next_member; 868 } 869 870 static void env_stack_pop_resolved(struct btf_verifier_env *env, 871 u32 resolved_type_id, 872 u32 resolved_size) 873 { 874 u32 type_id = env->stack[--(env->top_stack)].type_id; 875 struct btf *btf = env->btf; 876 877 btf->resolved_sizes[type_id] = resolved_size; 878 btf->resolved_ids[type_id] = resolved_type_id; 879 env->visit_states[type_id] = RESOLVED; 880 } 881 882 static const struct resolve_vertex *env_stack_peak(struct btf_verifier_env *env) 883 { 884 return env->top_stack ? &env->stack[env->top_stack - 1] : NULL; 885 } 886 887 /* The input param "type_id" must point to a needs_resolve type */ 888 static const struct btf_type *btf_type_id_resolve(const struct btf *btf, 889 u32 *type_id) 890 { 891 *type_id = btf->resolved_ids[*type_id]; 892 return btf_type_by_id(btf, *type_id); 893 } 894 895 const struct btf_type *btf_type_id_size(const struct btf *btf, 896 u32 *type_id, u32 *ret_size) 897 { 898 const struct btf_type *size_type; 899 u32 size_type_id = *type_id; 900 u32 size = 0; 901 902 size_type = btf_type_by_id(btf, size_type_id); 903 if (btf_type_nosize_or_null(size_type)) 904 return NULL; 905 906 if (btf_type_has_size(size_type)) { 907 size = size_type->size; 908 } else if (btf_type_is_array(size_type)) { 909 size = btf->resolved_sizes[size_type_id]; 910 } else if (btf_type_is_ptr(size_type)) { 911 size = sizeof(void *); 912 } else { 913 if (WARN_ON_ONCE(!btf_type_is_modifier(size_type))) 914 return NULL; 915 916 size = btf->resolved_sizes[size_type_id]; 917 size_type_id = btf->resolved_ids[size_type_id]; 918 size_type = btf_type_by_id(btf, size_type_id); 919 if (btf_type_nosize_or_null(size_type)) 920 return NULL; 921 } 922 923 *type_id = size_type_id; 924 if (ret_size) 925 *ret_size = size; 926 927 return size_type; 928 } 929 930 static int btf_df_check_member(struct btf_verifier_env *env, 931 const struct btf_type *struct_type, 932 const struct btf_member *member, 933 const struct btf_type *member_type) 934 { 935 btf_verifier_log_basic(env, struct_type, 936 "Unsupported check_member"); 937 return -EINVAL; 938 } 939 940 static int btf_df_resolve(struct btf_verifier_env *env, 941 const struct resolve_vertex *v) 942 { 943 btf_verifier_log_basic(env, v->t, "Unsupported resolve"); 944 return -EINVAL; 945 } 946 947 static void btf_df_seq_show(const struct btf *btf, const struct btf_type *t, 948 u32 type_id, void *data, u8 bits_offsets, 949 struct seq_file *m) 950 { 951 seq_printf(m, "<unsupported kind:%u>", BTF_INFO_KIND(t->info)); 952 } 953 954 static int btf_int_check_member(struct btf_verifier_env *env, 955 const struct btf_type *struct_type, 956 const struct btf_member *member, 957 const struct btf_type *member_type) 958 { 959 u32 int_data = btf_type_int(member_type); 960 u32 struct_bits_off = member->offset; 961 u32 struct_size = struct_type->size; 962 u32 nr_copy_bits; 963 u32 bytes_offset; 964 965 if (U32_MAX - struct_bits_off < BTF_INT_OFFSET(int_data)) { 966 btf_verifier_log_member(env, struct_type, member, 967 "bits_offset exceeds U32_MAX"); 968 return -EINVAL; 969 } 970 971 struct_bits_off += BTF_INT_OFFSET(int_data); 972 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off); 973 nr_copy_bits = BTF_INT_BITS(int_data) + 974 BITS_PER_BYTE_MASKED(struct_bits_off); 975 976 if (nr_copy_bits > BITS_PER_U64) { 977 btf_verifier_log_member(env, struct_type, member, 978 "nr_copy_bits exceeds 64"); 979 return -EINVAL; 980 } 981 982 if (struct_size < bytes_offset || 983 struct_size - bytes_offset < BITS_ROUNDUP_BYTES(nr_copy_bits)) { 984 btf_verifier_log_member(env, struct_type, member, 985 "Member exceeds struct_size"); 986 return -EINVAL; 987 } 988 989 return 0; 990 } 991 992 static s32 btf_int_check_meta(struct btf_verifier_env *env, 993 const struct btf_type *t, 994 u32 meta_left) 995 { 996 u32 int_data, nr_bits, meta_needed = sizeof(int_data); 997 u16 encoding; 998 999 if (meta_left < meta_needed) { 1000 btf_verifier_log_basic(env, t, 1001 "meta_left:%u meta_needed:%u", 1002 meta_left, meta_needed); 1003 return -EINVAL; 1004 } 1005 1006 if (btf_type_vlen(t)) { 1007 btf_verifier_log_type(env, t, "vlen != 0"); 1008 return -EINVAL; 1009 } 1010 1011 int_data = btf_type_int(t); 1012 if (int_data & ~BTF_INT_MASK) { 1013 btf_verifier_log_basic(env, t, "Invalid int_data:%x", 1014 int_data); 1015 return -EINVAL; 1016 } 1017 1018 nr_bits = BTF_INT_BITS(int_data) + BTF_INT_OFFSET(int_data); 1019 1020 if (nr_bits > BITS_PER_U64) { 1021 btf_verifier_log_type(env, t, "nr_bits exceeds %zu", 1022 BITS_PER_U64); 1023 return -EINVAL; 1024 } 1025 1026 if (BITS_ROUNDUP_BYTES(nr_bits) > t->size) { 1027 btf_verifier_log_type(env, t, "nr_bits exceeds type_size"); 1028 return -EINVAL; 1029 } 1030 1031 /* 1032 * Only one of the encoding bits is allowed and it 1033 * should be sufficient for the pretty print purpose (i.e. decoding). 1034 * Multiple bits can be allowed later if it is found 1035 * to be insufficient. 1036 */ 1037 encoding = BTF_INT_ENCODING(int_data); 1038 if (encoding && 1039 encoding != BTF_INT_SIGNED && 1040 encoding != BTF_INT_CHAR && 1041 encoding != BTF_INT_BOOL) { 1042 btf_verifier_log_type(env, t, "Unsupported encoding"); 1043 return -ENOTSUPP; 1044 } 1045 1046 btf_verifier_log_type(env, t, NULL); 1047 1048 return meta_needed; 1049 } 1050 1051 static void btf_int_log(struct btf_verifier_env *env, 1052 const struct btf_type *t) 1053 { 1054 int int_data = btf_type_int(t); 1055 1056 btf_verifier_log(env, 1057 "size=%u bits_offset=%u nr_bits=%u encoding=%s", 1058 t->size, BTF_INT_OFFSET(int_data), 1059 BTF_INT_BITS(int_data), 1060 btf_int_encoding_str(BTF_INT_ENCODING(int_data))); 1061 } 1062 1063 static void btf_int_bits_seq_show(const struct btf *btf, 1064 const struct btf_type *t, 1065 void *data, u8 bits_offset, 1066 struct seq_file *m) 1067 { 1068 u16 left_shift_bits, right_shift_bits; 1069 u32 int_data = btf_type_int(t); 1070 u8 nr_bits = BTF_INT_BITS(int_data); 1071 u8 total_bits_offset; 1072 u8 nr_copy_bytes; 1073 u8 nr_copy_bits; 1074 u64 print_num; 1075 1076 /* 1077 * bits_offset is at most 7. 1078 * BTF_INT_OFFSET() cannot exceed 64 bits. 1079 */ 1080 total_bits_offset = bits_offset + BTF_INT_OFFSET(int_data); 1081 data += BITS_ROUNDDOWN_BYTES(total_bits_offset); 1082 bits_offset = BITS_PER_BYTE_MASKED(total_bits_offset); 1083 nr_copy_bits = nr_bits + bits_offset; 1084 nr_copy_bytes = BITS_ROUNDUP_BYTES(nr_copy_bits); 1085 1086 print_num = 0; 1087 memcpy(&print_num, data, nr_copy_bytes); 1088 1089 #ifdef __BIG_ENDIAN_BITFIELD 1090 left_shift_bits = bits_offset; 1091 #else 1092 left_shift_bits = BITS_PER_U64 - nr_copy_bits; 1093 #endif 1094 right_shift_bits = BITS_PER_U64 - nr_bits; 1095 1096 print_num <<= left_shift_bits; 1097 print_num >>= right_shift_bits; 1098 1099 seq_printf(m, "0x%llx", print_num); 1100 } 1101 1102 static void btf_int_seq_show(const struct btf *btf, const struct btf_type *t, 1103 u32 type_id, void *data, u8 bits_offset, 1104 struct seq_file *m) 1105 { 1106 u32 int_data = btf_type_int(t); 1107 u8 encoding = BTF_INT_ENCODING(int_data); 1108 bool sign = encoding & BTF_INT_SIGNED; 1109 u8 nr_bits = BTF_INT_BITS(int_data); 1110 1111 if (bits_offset || BTF_INT_OFFSET(int_data) || 1112 BITS_PER_BYTE_MASKED(nr_bits)) { 1113 btf_int_bits_seq_show(btf, t, data, bits_offset, m); 1114 return; 1115 } 1116 1117 switch (nr_bits) { 1118 case 64: 1119 if (sign) 1120 seq_printf(m, "%lld", *(s64 *)data); 1121 else 1122 seq_printf(m, "%llu", *(u64 *)data); 1123 break; 1124 case 32: 1125 if (sign) 1126 seq_printf(m, "%d", *(s32 *)data); 1127 else 1128 seq_printf(m, "%u", *(u32 *)data); 1129 break; 1130 case 16: 1131 if (sign) 1132 seq_printf(m, "%d", *(s16 *)data); 1133 else 1134 seq_printf(m, "%u", *(u16 *)data); 1135 break; 1136 case 8: 1137 if (sign) 1138 seq_printf(m, "%d", *(s8 *)data); 1139 else 1140 seq_printf(m, "%u", *(u8 *)data); 1141 break; 1142 default: 1143 btf_int_bits_seq_show(btf, t, data, bits_offset, m); 1144 } 1145 } 1146 1147 static const struct btf_kind_operations int_ops = { 1148 .check_meta = btf_int_check_meta, 1149 .resolve = btf_df_resolve, 1150 .check_member = btf_int_check_member, 1151 .log_details = btf_int_log, 1152 .seq_show = btf_int_seq_show, 1153 }; 1154 1155 static int btf_modifier_check_member(struct btf_verifier_env *env, 1156 const struct btf_type *struct_type, 1157 const struct btf_member *member, 1158 const struct btf_type *member_type) 1159 { 1160 const struct btf_type *resolved_type; 1161 u32 resolved_type_id = member->type; 1162 struct btf_member resolved_member; 1163 struct btf *btf = env->btf; 1164 1165 resolved_type = btf_type_id_size(btf, &resolved_type_id, NULL); 1166 if (!resolved_type) { 1167 btf_verifier_log_member(env, struct_type, member, 1168 "Invalid member"); 1169 return -EINVAL; 1170 } 1171 1172 resolved_member = *member; 1173 resolved_member.type = resolved_type_id; 1174 1175 return btf_type_ops(resolved_type)->check_member(env, struct_type, 1176 &resolved_member, 1177 resolved_type); 1178 } 1179 1180 static int btf_ptr_check_member(struct btf_verifier_env *env, 1181 const struct btf_type *struct_type, 1182 const struct btf_member *member, 1183 const struct btf_type *member_type) 1184 { 1185 u32 struct_size, struct_bits_off, bytes_offset; 1186 1187 struct_size = struct_type->size; 1188 struct_bits_off = member->offset; 1189 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off); 1190 1191 if (BITS_PER_BYTE_MASKED(struct_bits_off)) { 1192 btf_verifier_log_member(env, struct_type, member, 1193 "Member is not byte aligned"); 1194 return -EINVAL; 1195 } 1196 1197 if (struct_size - bytes_offset < sizeof(void *)) { 1198 btf_verifier_log_member(env, struct_type, member, 1199 "Member exceeds struct_size"); 1200 return -EINVAL; 1201 } 1202 1203 return 0; 1204 } 1205 1206 static int btf_ref_type_check_meta(struct btf_verifier_env *env, 1207 const struct btf_type *t, 1208 u32 meta_left) 1209 { 1210 if (btf_type_vlen(t)) { 1211 btf_verifier_log_type(env, t, "vlen != 0"); 1212 return -EINVAL; 1213 } 1214 1215 if (!BTF_TYPE_ID_VALID(t->type)) { 1216 btf_verifier_log_type(env, t, "Invalid type_id"); 1217 return -EINVAL; 1218 } 1219 1220 /* typedef type must have a valid name, and other ref types, 1221 * volatile, const, restrict, should have a null name. 1222 */ 1223 if (BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF) { 1224 if (!t->name_off || 1225 !btf_name_valid_identifier(env->btf, t->name_off)) { 1226 btf_verifier_log_type(env, t, "Invalid name"); 1227 return -EINVAL; 1228 } 1229 } else { 1230 if (t->name_off) { 1231 btf_verifier_log_type(env, t, "Invalid name"); 1232 return -EINVAL; 1233 } 1234 } 1235 1236 btf_verifier_log_type(env, t, NULL); 1237 1238 return 0; 1239 } 1240 1241 static int btf_modifier_resolve(struct btf_verifier_env *env, 1242 const struct resolve_vertex *v) 1243 { 1244 const struct btf_type *t = v->t; 1245 const struct btf_type *next_type; 1246 u32 next_type_id = t->type; 1247 struct btf *btf = env->btf; 1248 u32 next_type_size = 0; 1249 1250 next_type = btf_type_by_id(btf, next_type_id); 1251 if (!next_type) { 1252 btf_verifier_log_type(env, v->t, "Invalid type_id"); 1253 return -EINVAL; 1254 } 1255 1256 if (!env_type_is_resolve_sink(env, next_type) && 1257 !env_type_is_resolved(env, next_type_id)) 1258 return env_stack_push(env, next_type, next_type_id); 1259 1260 /* Figure out the resolved next_type_id with size. 1261 * They will be stored in the current modifier's 1262 * resolved_ids and resolved_sizes such that it can 1263 * save us a few type-following when we use it later (e.g. in 1264 * pretty print). 1265 */ 1266 if (!btf_type_id_size(btf, &next_type_id, &next_type_size)) { 1267 if (env_type_is_resolved(env, next_type_id)) 1268 next_type = btf_type_id_resolve(btf, &next_type_id); 1269 1270 /* "typedef void new_void", "const void"...etc */ 1271 if (!btf_type_is_void(next_type) && 1272 !btf_type_is_fwd(next_type)) { 1273 btf_verifier_log_type(env, v->t, "Invalid type_id"); 1274 return -EINVAL; 1275 } 1276 } 1277 1278 env_stack_pop_resolved(env, next_type_id, next_type_size); 1279 1280 return 0; 1281 } 1282 1283 static int btf_ptr_resolve(struct btf_verifier_env *env, 1284 const struct resolve_vertex *v) 1285 { 1286 const struct btf_type *next_type; 1287 const struct btf_type *t = v->t; 1288 u32 next_type_id = t->type; 1289 struct btf *btf = env->btf; 1290 1291 next_type = btf_type_by_id(btf, next_type_id); 1292 if (!next_type) { 1293 btf_verifier_log_type(env, v->t, "Invalid type_id"); 1294 return -EINVAL; 1295 } 1296 1297 if (!env_type_is_resolve_sink(env, next_type) && 1298 !env_type_is_resolved(env, next_type_id)) 1299 return env_stack_push(env, next_type, next_type_id); 1300 1301 /* If the modifier was RESOLVED during RESOLVE_STRUCT_OR_ARRAY, 1302 * the modifier may have stopped resolving when it was resolved 1303 * to a ptr (last-resolved-ptr). 1304 * 1305 * We now need to continue from the last-resolved-ptr to 1306 * ensure the last-resolved-ptr will not referring back to 1307 * the currenct ptr (t). 1308 */ 1309 if (btf_type_is_modifier(next_type)) { 1310 const struct btf_type *resolved_type; 1311 u32 resolved_type_id; 1312 1313 resolved_type_id = next_type_id; 1314 resolved_type = btf_type_id_resolve(btf, &resolved_type_id); 1315 1316 if (btf_type_is_ptr(resolved_type) && 1317 !env_type_is_resolve_sink(env, resolved_type) && 1318 !env_type_is_resolved(env, resolved_type_id)) 1319 return env_stack_push(env, resolved_type, 1320 resolved_type_id); 1321 } 1322 1323 if (!btf_type_id_size(btf, &next_type_id, NULL)) { 1324 if (env_type_is_resolved(env, next_type_id)) 1325 next_type = btf_type_id_resolve(btf, &next_type_id); 1326 1327 if (!btf_type_is_void(next_type) && 1328 !btf_type_is_fwd(next_type) && 1329 !btf_type_is_func_proto(next_type)) { 1330 btf_verifier_log_type(env, v->t, "Invalid type_id"); 1331 return -EINVAL; 1332 } 1333 } 1334 1335 env_stack_pop_resolved(env, next_type_id, 0); 1336 1337 return 0; 1338 } 1339 1340 static void btf_modifier_seq_show(const struct btf *btf, 1341 const struct btf_type *t, 1342 u32 type_id, void *data, 1343 u8 bits_offset, struct seq_file *m) 1344 { 1345 t = btf_type_id_resolve(btf, &type_id); 1346 1347 btf_type_ops(t)->seq_show(btf, t, type_id, data, bits_offset, m); 1348 } 1349 1350 static void btf_ptr_seq_show(const struct btf *btf, const struct btf_type *t, 1351 u32 type_id, void *data, u8 bits_offset, 1352 struct seq_file *m) 1353 { 1354 /* It is a hashed value */ 1355 seq_printf(m, "%p", *(void **)data); 1356 } 1357 1358 static void btf_ref_type_log(struct btf_verifier_env *env, 1359 const struct btf_type *t) 1360 { 1361 btf_verifier_log(env, "type_id=%u", t->type); 1362 } 1363 1364 static struct btf_kind_operations modifier_ops = { 1365 .check_meta = btf_ref_type_check_meta, 1366 .resolve = btf_modifier_resolve, 1367 .check_member = btf_modifier_check_member, 1368 .log_details = btf_ref_type_log, 1369 .seq_show = btf_modifier_seq_show, 1370 }; 1371 1372 static struct btf_kind_operations ptr_ops = { 1373 .check_meta = btf_ref_type_check_meta, 1374 .resolve = btf_ptr_resolve, 1375 .check_member = btf_ptr_check_member, 1376 .log_details = btf_ref_type_log, 1377 .seq_show = btf_ptr_seq_show, 1378 }; 1379 1380 static s32 btf_fwd_check_meta(struct btf_verifier_env *env, 1381 const struct btf_type *t, 1382 u32 meta_left) 1383 { 1384 if (btf_type_vlen(t)) { 1385 btf_verifier_log_type(env, t, "vlen != 0"); 1386 return -EINVAL; 1387 } 1388 1389 if (t->type) { 1390 btf_verifier_log_type(env, t, "type != 0"); 1391 return -EINVAL; 1392 } 1393 1394 /* fwd type must have a valid name */ 1395 if (!t->name_off || 1396 !btf_name_valid_identifier(env->btf, t->name_off)) { 1397 btf_verifier_log_type(env, t, "Invalid name"); 1398 return -EINVAL; 1399 } 1400 1401 btf_verifier_log_type(env, t, NULL); 1402 1403 return 0; 1404 } 1405 1406 static struct btf_kind_operations fwd_ops = { 1407 .check_meta = btf_fwd_check_meta, 1408 .resolve = btf_df_resolve, 1409 .check_member = btf_df_check_member, 1410 .log_details = btf_ref_type_log, 1411 .seq_show = btf_df_seq_show, 1412 }; 1413 1414 static int btf_array_check_member(struct btf_verifier_env *env, 1415 const struct btf_type *struct_type, 1416 const struct btf_member *member, 1417 const struct btf_type *member_type) 1418 { 1419 u32 struct_bits_off = member->offset; 1420 u32 struct_size, bytes_offset; 1421 u32 array_type_id, array_size; 1422 struct btf *btf = env->btf; 1423 1424 if (BITS_PER_BYTE_MASKED(struct_bits_off)) { 1425 btf_verifier_log_member(env, struct_type, member, 1426 "Member is not byte aligned"); 1427 return -EINVAL; 1428 } 1429 1430 array_type_id = member->type; 1431 btf_type_id_size(btf, &array_type_id, &array_size); 1432 struct_size = struct_type->size; 1433 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off); 1434 if (struct_size - bytes_offset < array_size) { 1435 btf_verifier_log_member(env, struct_type, member, 1436 "Member exceeds struct_size"); 1437 return -EINVAL; 1438 } 1439 1440 return 0; 1441 } 1442 1443 static s32 btf_array_check_meta(struct btf_verifier_env *env, 1444 const struct btf_type *t, 1445 u32 meta_left) 1446 { 1447 const struct btf_array *array = btf_type_array(t); 1448 u32 meta_needed = sizeof(*array); 1449 1450 if (meta_left < meta_needed) { 1451 btf_verifier_log_basic(env, t, 1452 "meta_left:%u meta_needed:%u", 1453 meta_left, meta_needed); 1454 return -EINVAL; 1455 } 1456 1457 /* array type should not have a name */ 1458 if (t->name_off) { 1459 btf_verifier_log_type(env, t, "Invalid name"); 1460 return -EINVAL; 1461 } 1462 1463 if (btf_type_vlen(t)) { 1464 btf_verifier_log_type(env, t, "vlen != 0"); 1465 return -EINVAL; 1466 } 1467 1468 if (t->size) { 1469 btf_verifier_log_type(env, t, "size != 0"); 1470 return -EINVAL; 1471 } 1472 1473 /* Array elem type and index type cannot be in type void, 1474 * so !array->type and !array->index_type are not allowed. 1475 */ 1476 if (!array->type || !BTF_TYPE_ID_VALID(array->type)) { 1477 btf_verifier_log_type(env, t, "Invalid elem"); 1478 return -EINVAL; 1479 } 1480 1481 if (!array->index_type || !BTF_TYPE_ID_VALID(array->index_type)) { 1482 btf_verifier_log_type(env, t, "Invalid index"); 1483 return -EINVAL; 1484 } 1485 1486 btf_verifier_log_type(env, t, NULL); 1487 1488 return meta_needed; 1489 } 1490 1491 static int btf_array_resolve(struct btf_verifier_env *env, 1492 const struct resolve_vertex *v) 1493 { 1494 const struct btf_array *array = btf_type_array(v->t); 1495 const struct btf_type *elem_type, *index_type; 1496 u32 elem_type_id, index_type_id; 1497 struct btf *btf = env->btf; 1498 u32 elem_size; 1499 1500 /* Check array->index_type */ 1501 index_type_id = array->index_type; 1502 index_type = btf_type_by_id(btf, index_type_id); 1503 if (btf_type_nosize_or_null(index_type)) { 1504 btf_verifier_log_type(env, v->t, "Invalid index"); 1505 return -EINVAL; 1506 } 1507 1508 if (!env_type_is_resolve_sink(env, index_type) && 1509 !env_type_is_resolved(env, index_type_id)) 1510 return env_stack_push(env, index_type, index_type_id); 1511 1512 index_type = btf_type_id_size(btf, &index_type_id, NULL); 1513 if (!index_type || !btf_type_is_int(index_type) || 1514 !btf_type_int_is_regular(index_type)) { 1515 btf_verifier_log_type(env, v->t, "Invalid index"); 1516 return -EINVAL; 1517 } 1518 1519 /* Check array->type */ 1520 elem_type_id = array->type; 1521 elem_type = btf_type_by_id(btf, elem_type_id); 1522 if (btf_type_nosize_or_null(elem_type)) { 1523 btf_verifier_log_type(env, v->t, 1524 "Invalid elem"); 1525 return -EINVAL; 1526 } 1527 1528 if (!env_type_is_resolve_sink(env, elem_type) && 1529 !env_type_is_resolved(env, elem_type_id)) 1530 return env_stack_push(env, elem_type, elem_type_id); 1531 1532 elem_type = btf_type_id_size(btf, &elem_type_id, &elem_size); 1533 if (!elem_type) { 1534 btf_verifier_log_type(env, v->t, "Invalid elem"); 1535 return -EINVAL; 1536 } 1537 1538 if (btf_type_is_int(elem_type) && !btf_type_int_is_regular(elem_type)) { 1539 btf_verifier_log_type(env, v->t, "Invalid array of int"); 1540 return -EINVAL; 1541 } 1542 1543 if (array->nelems && elem_size > U32_MAX / array->nelems) { 1544 btf_verifier_log_type(env, v->t, 1545 "Array size overflows U32_MAX"); 1546 return -EINVAL; 1547 } 1548 1549 env_stack_pop_resolved(env, elem_type_id, elem_size * array->nelems); 1550 1551 return 0; 1552 } 1553 1554 static void btf_array_log(struct btf_verifier_env *env, 1555 const struct btf_type *t) 1556 { 1557 const struct btf_array *array = btf_type_array(t); 1558 1559 btf_verifier_log(env, "type_id=%u index_type_id=%u nr_elems=%u", 1560 array->type, array->index_type, array->nelems); 1561 } 1562 1563 static void btf_array_seq_show(const struct btf *btf, const struct btf_type *t, 1564 u32 type_id, void *data, u8 bits_offset, 1565 struct seq_file *m) 1566 { 1567 const struct btf_array *array = btf_type_array(t); 1568 const struct btf_kind_operations *elem_ops; 1569 const struct btf_type *elem_type; 1570 u32 i, elem_size, elem_type_id; 1571 1572 elem_type_id = array->type; 1573 elem_type = btf_type_id_size(btf, &elem_type_id, &elem_size); 1574 elem_ops = btf_type_ops(elem_type); 1575 seq_puts(m, "["); 1576 for (i = 0; i < array->nelems; i++) { 1577 if (i) 1578 seq_puts(m, ","); 1579 1580 elem_ops->seq_show(btf, elem_type, elem_type_id, data, 1581 bits_offset, m); 1582 data += elem_size; 1583 } 1584 seq_puts(m, "]"); 1585 } 1586 1587 static struct btf_kind_operations array_ops = { 1588 .check_meta = btf_array_check_meta, 1589 .resolve = btf_array_resolve, 1590 .check_member = btf_array_check_member, 1591 .log_details = btf_array_log, 1592 .seq_show = btf_array_seq_show, 1593 }; 1594 1595 static int btf_struct_check_member(struct btf_verifier_env *env, 1596 const struct btf_type *struct_type, 1597 const struct btf_member *member, 1598 const struct btf_type *member_type) 1599 { 1600 u32 struct_bits_off = member->offset; 1601 u32 struct_size, bytes_offset; 1602 1603 if (BITS_PER_BYTE_MASKED(struct_bits_off)) { 1604 btf_verifier_log_member(env, struct_type, member, 1605 "Member is not byte aligned"); 1606 return -EINVAL; 1607 } 1608 1609 struct_size = struct_type->size; 1610 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off); 1611 if (struct_size - bytes_offset < member_type->size) { 1612 btf_verifier_log_member(env, struct_type, member, 1613 "Member exceeds struct_size"); 1614 return -EINVAL; 1615 } 1616 1617 return 0; 1618 } 1619 1620 static s32 btf_struct_check_meta(struct btf_verifier_env *env, 1621 const struct btf_type *t, 1622 u32 meta_left) 1623 { 1624 bool is_union = BTF_INFO_KIND(t->info) == BTF_KIND_UNION; 1625 const struct btf_member *member; 1626 u32 meta_needed, last_offset; 1627 struct btf *btf = env->btf; 1628 u32 struct_size = t->size; 1629 u16 i; 1630 1631 meta_needed = btf_type_vlen(t) * sizeof(*member); 1632 if (meta_left < meta_needed) { 1633 btf_verifier_log_basic(env, t, 1634 "meta_left:%u meta_needed:%u", 1635 meta_left, meta_needed); 1636 return -EINVAL; 1637 } 1638 1639 /* struct type either no name or a valid one */ 1640 if (t->name_off && 1641 !btf_name_valid_identifier(env->btf, t->name_off)) { 1642 btf_verifier_log_type(env, t, "Invalid name"); 1643 return -EINVAL; 1644 } 1645 1646 btf_verifier_log_type(env, t, NULL); 1647 1648 last_offset = 0; 1649 for_each_member(i, t, member) { 1650 if (!btf_name_offset_valid(btf, member->name_off)) { 1651 btf_verifier_log_member(env, t, member, 1652 "Invalid member name_offset:%u", 1653 member->name_off); 1654 return -EINVAL; 1655 } 1656 1657 /* struct member either no name or a valid one */ 1658 if (member->name_off && 1659 !btf_name_valid_identifier(btf, member->name_off)) { 1660 btf_verifier_log_member(env, t, member, "Invalid name"); 1661 return -EINVAL; 1662 } 1663 /* A member cannot be in type void */ 1664 if (!member->type || !BTF_TYPE_ID_VALID(member->type)) { 1665 btf_verifier_log_member(env, t, member, 1666 "Invalid type_id"); 1667 return -EINVAL; 1668 } 1669 1670 if (is_union && member->offset) { 1671 btf_verifier_log_member(env, t, member, 1672 "Invalid member bits_offset"); 1673 return -EINVAL; 1674 } 1675 1676 /* 1677 * ">" instead of ">=" because the last member could be 1678 * "char a[0];" 1679 */ 1680 if (last_offset > member->offset) { 1681 btf_verifier_log_member(env, t, member, 1682 "Invalid member bits_offset"); 1683 return -EINVAL; 1684 } 1685 1686 if (BITS_ROUNDUP_BYTES(member->offset) > struct_size) { 1687 btf_verifier_log_member(env, t, member, 1688 "Member bits_offset exceeds its struct size"); 1689 return -EINVAL; 1690 } 1691 1692 btf_verifier_log_member(env, t, member, NULL); 1693 last_offset = member->offset; 1694 } 1695 1696 return meta_needed; 1697 } 1698 1699 static int btf_struct_resolve(struct btf_verifier_env *env, 1700 const struct resolve_vertex *v) 1701 { 1702 const struct btf_member *member; 1703 int err; 1704 u16 i; 1705 1706 /* Before continue resolving the next_member, 1707 * ensure the last member is indeed resolved to a 1708 * type with size info. 1709 */ 1710 if (v->next_member) { 1711 const struct btf_type *last_member_type; 1712 const struct btf_member *last_member; 1713 u16 last_member_type_id; 1714 1715 last_member = btf_type_member(v->t) + v->next_member - 1; 1716 last_member_type_id = last_member->type; 1717 if (WARN_ON_ONCE(!env_type_is_resolved(env, 1718 last_member_type_id))) 1719 return -EINVAL; 1720 1721 last_member_type = btf_type_by_id(env->btf, 1722 last_member_type_id); 1723 err = btf_type_ops(last_member_type)->check_member(env, v->t, 1724 last_member, 1725 last_member_type); 1726 if (err) 1727 return err; 1728 } 1729 1730 for_each_member_from(i, v->next_member, v->t, member) { 1731 u32 member_type_id = member->type; 1732 const struct btf_type *member_type = btf_type_by_id(env->btf, 1733 member_type_id); 1734 1735 if (btf_type_nosize_or_null(member_type)) { 1736 btf_verifier_log_member(env, v->t, member, 1737 "Invalid member"); 1738 return -EINVAL; 1739 } 1740 1741 if (!env_type_is_resolve_sink(env, member_type) && 1742 !env_type_is_resolved(env, member_type_id)) { 1743 env_stack_set_next_member(env, i + 1); 1744 return env_stack_push(env, member_type, member_type_id); 1745 } 1746 1747 err = btf_type_ops(member_type)->check_member(env, v->t, 1748 member, 1749 member_type); 1750 if (err) 1751 return err; 1752 } 1753 1754 env_stack_pop_resolved(env, 0, 0); 1755 1756 return 0; 1757 } 1758 1759 static void btf_struct_log(struct btf_verifier_env *env, 1760 const struct btf_type *t) 1761 { 1762 btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t)); 1763 } 1764 1765 static void btf_struct_seq_show(const struct btf *btf, const struct btf_type *t, 1766 u32 type_id, void *data, u8 bits_offset, 1767 struct seq_file *m) 1768 { 1769 const char *seq = BTF_INFO_KIND(t->info) == BTF_KIND_UNION ? "|" : ","; 1770 const struct btf_member *member; 1771 u32 i; 1772 1773 seq_puts(m, "{"); 1774 for_each_member(i, t, member) { 1775 const struct btf_type *member_type = btf_type_by_id(btf, 1776 member->type); 1777 u32 member_offset = member->offset; 1778 u32 bytes_offset = BITS_ROUNDDOWN_BYTES(member_offset); 1779 u8 bits8_offset = BITS_PER_BYTE_MASKED(member_offset); 1780 const struct btf_kind_operations *ops; 1781 1782 if (i) 1783 seq_puts(m, seq); 1784 1785 ops = btf_type_ops(member_type); 1786 ops->seq_show(btf, member_type, member->type, 1787 data + bytes_offset, bits8_offset, m); 1788 } 1789 seq_puts(m, "}"); 1790 } 1791 1792 static struct btf_kind_operations struct_ops = { 1793 .check_meta = btf_struct_check_meta, 1794 .resolve = btf_struct_resolve, 1795 .check_member = btf_struct_check_member, 1796 .log_details = btf_struct_log, 1797 .seq_show = btf_struct_seq_show, 1798 }; 1799 1800 static int btf_enum_check_member(struct btf_verifier_env *env, 1801 const struct btf_type *struct_type, 1802 const struct btf_member *member, 1803 const struct btf_type *member_type) 1804 { 1805 u32 struct_bits_off = member->offset; 1806 u32 struct_size, bytes_offset; 1807 1808 if (BITS_PER_BYTE_MASKED(struct_bits_off)) { 1809 btf_verifier_log_member(env, struct_type, member, 1810 "Member is not byte aligned"); 1811 return -EINVAL; 1812 } 1813 1814 struct_size = struct_type->size; 1815 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off); 1816 if (struct_size - bytes_offset < sizeof(int)) { 1817 btf_verifier_log_member(env, struct_type, member, 1818 "Member exceeds struct_size"); 1819 return -EINVAL; 1820 } 1821 1822 return 0; 1823 } 1824 1825 static s32 btf_enum_check_meta(struct btf_verifier_env *env, 1826 const struct btf_type *t, 1827 u32 meta_left) 1828 { 1829 const struct btf_enum *enums = btf_type_enum(t); 1830 struct btf *btf = env->btf; 1831 u16 i, nr_enums; 1832 u32 meta_needed; 1833 1834 nr_enums = btf_type_vlen(t); 1835 meta_needed = nr_enums * sizeof(*enums); 1836 1837 if (meta_left < meta_needed) { 1838 btf_verifier_log_basic(env, t, 1839 "meta_left:%u meta_needed:%u", 1840 meta_left, meta_needed); 1841 return -EINVAL; 1842 } 1843 1844 if (t->size != sizeof(int)) { 1845 btf_verifier_log_type(env, t, "Expected size:%zu", 1846 sizeof(int)); 1847 return -EINVAL; 1848 } 1849 1850 /* enum type either no name or a valid one */ 1851 if (t->name_off && 1852 !btf_name_valid_identifier(env->btf, t->name_off)) { 1853 btf_verifier_log_type(env, t, "Invalid name"); 1854 return -EINVAL; 1855 } 1856 1857 btf_verifier_log_type(env, t, NULL); 1858 1859 for (i = 0; i < nr_enums; i++) { 1860 if (!btf_name_offset_valid(btf, enums[i].name_off)) { 1861 btf_verifier_log(env, "\tInvalid name_offset:%u", 1862 enums[i].name_off); 1863 return -EINVAL; 1864 } 1865 1866 /* enum member must have a valid name */ 1867 if (!enums[i].name_off || 1868 !btf_name_valid_identifier(btf, enums[i].name_off)) { 1869 btf_verifier_log_type(env, t, "Invalid name"); 1870 return -EINVAL; 1871 } 1872 1873 1874 btf_verifier_log(env, "\t%s val=%d\n", 1875 btf_name_by_offset(btf, enums[i].name_off), 1876 enums[i].val); 1877 } 1878 1879 return meta_needed; 1880 } 1881 1882 static void btf_enum_log(struct btf_verifier_env *env, 1883 const struct btf_type *t) 1884 { 1885 btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t)); 1886 } 1887 1888 static void btf_enum_seq_show(const struct btf *btf, const struct btf_type *t, 1889 u32 type_id, void *data, u8 bits_offset, 1890 struct seq_file *m) 1891 { 1892 const struct btf_enum *enums = btf_type_enum(t); 1893 u32 i, nr_enums = btf_type_vlen(t); 1894 int v = *(int *)data; 1895 1896 for (i = 0; i < nr_enums; i++) { 1897 if (v == enums[i].val) { 1898 seq_printf(m, "%s", 1899 btf_name_by_offset(btf, enums[i].name_off)); 1900 return; 1901 } 1902 } 1903 1904 seq_printf(m, "%d", v); 1905 } 1906 1907 static struct btf_kind_operations enum_ops = { 1908 .check_meta = btf_enum_check_meta, 1909 .resolve = btf_df_resolve, 1910 .check_member = btf_enum_check_member, 1911 .log_details = btf_enum_log, 1912 .seq_show = btf_enum_seq_show, 1913 }; 1914 1915 static s32 btf_func_proto_check_meta(struct btf_verifier_env *env, 1916 const struct btf_type *t, 1917 u32 meta_left) 1918 { 1919 u32 meta_needed = btf_type_vlen(t) * sizeof(struct btf_param); 1920 1921 if (meta_left < meta_needed) { 1922 btf_verifier_log_basic(env, t, 1923 "meta_left:%u meta_needed:%u", 1924 meta_left, meta_needed); 1925 return -EINVAL; 1926 } 1927 1928 if (t->name_off) { 1929 btf_verifier_log_type(env, t, "Invalid name"); 1930 return -EINVAL; 1931 } 1932 1933 btf_verifier_log_type(env, t, NULL); 1934 1935 return meta_needed; 1936 } 1937 1938 static void btf_func_proto_log(struct btf_verifier_env *env, 1939 const struct btf_type *t) 1940 { 1941 const struct btf_param *args = (const struct btf_param *)(t + 1); 1942 u16 nr_args = btf_type_vlen(t), i; 1943 1944 btf_verifier_log(env, "return=%u args=(", t->type); 1945 if (!nr_args) { 1946 btf_verifier_log(env, "void"); 1947 goto done; 1948 } 1949 1950 if (nr_args == 1 && !args[0].type) { 1951 /* Only one vararg */ 1952 btf_verifier_log(env, "vararg"); 1953 goto done; 1954 } 1955 1956 btf_verifier_log(env, "%u %s", args[0].type, 1957 btf_name_by_offset(env->btf, 1958 args[0].name_off)); 1959 for (i = 1; i < nr_args - 1; i++) 1960 btf_verifier_log(env, ", %u %s", args[i].type, 1961 btf_name_by_offset(env->btf, 1962 args[i].name_off)); 1963 1964 if (nr_args > 1) { 1965 const struct btf_param *last_arg = &args[nr_args - 1]; 1966 1967 if (last_arg->type) 1968 btf_verifier_log(env, ", %u %s", last_arg->type, 1969 btf_name_by_offset(env->btf, 1970 last_arg->name_off)); 1971 else 1972 btf_verifier_log(env, ", vararg"); 1973 } 1974 1975 done: 1976 btf_verifier_log(env, ")"); 1977 } 1978 1979 static struct btf_kind_operations func_proto_ops = { 1980 .check_meta = btf_func_proto_check_meta, 1981 .resolve = btf_df_resolve, 1982 /* 1983 * BTF_KIND_FUNC_PROTO cannot be directly referred by 1984 * a struct's member. 1985 * 1986 * It should be a funciton pointer instead. 1987 * (i.e. struct's member -> BTF_KIND_PTR -> BTF_KIND_FUNC_PROTO) 1988 * 1989 * Hence, there is no btf_func_check_member(). 1990 */ 1991 .check_member = btf_df_check_member, 1992 .log_details = btf_func_proto_log, 1993 .seq_show = btf_df_seq_show, 1994 }; 1995 1996 static s32 btf_func_check_meta(struct btf_verifier_env *env, 1997 const struct btf_type *t, 1998 u32 meta_left) 1999 { 2000 if (!t->name_off || 2001 !btf_name_valid_identifier(env->btf, t->name_off)) { 2002 btf_verifier_log_type(env, t, "Invalid name"); 2003 return -EINVAL; 2004 } 2005 2006 if (btf_type_vlen(t)) { 2007 btf_verifier_log_type(env, t, "vlen != 0"); 2008 return -EINVAL; 2009 } 2010 2011 btf_verifier_log_type(env, t, NULL); 2012 2013 return 0; 2014 } 2015 2016 static struct btf_kind_operations func_ops = { 2017 .check_meta = btf_func_check_meta, 2018 .resolve = btf_df_resolve, 2019 .check_member = btf_df_check_member, 2020 .log_details = btf_ref_type_log, 2021 .seq_show = btf_df_seq_show, 2022 }; 2023 2024 static int btf_func_proto_check(struct btf_verifier_env *env, 2025 const struct btf_type *t) 2026 { 2027 const struct btf_type *ret_type; 2028 const struct btf_param *args; 2029 const struct btf *btf; 2030 u16 nr_args, i; 2031 int err; 2032 2033 btf = env->btf; 2034 args = (const struct btf_param *)(t + 1); 2035 nr_args = btf_type_vlen(t); 2036 2037 /* Check func return type which could be "void" (t->type == 0) */ 2038 if (t->type) { 2039 u32 ret_type_id = t->type; 2040 2041 ret_type = btf_type_by_id(btf, ret_type_id); 2042 if (!ret_type) { 2043 btf_verifier_log_type(env, t, "Invalid return type"); 2044 return -EINVAL; 2045 } 2046 2047 if (btf_type_needs_resolve(ret_type) && 2048 !env_type_is_resolved(env, ret_type_id)) { 2049 err = btf_resolve(env, ret_type, ret_type_id); 2050 if (err) 2051 return err; 2052 } 2053 2054 /* Ensure the return type is a type that has a size */ 2055 if (!btf_type_id_size(btf, &ret_type_id, NULL)) { 2056 btf_verifier_log_type(env, t, "Invalid return type"); 2057 return -EINVAL; 2058 } 2059 } 2060 2061 if (!nr_args) 2062 return 0; 2063 2064 /* Last func arg type_id could be 0 if it is a vararg */ 2065 if (!args[nr_args - 1].type) { 2066 if (args[nr_args - 1].name_off) { 2067 btf_verifier_log_type(env, t, "Invalid arg#%u", 2068 nr_args); 2069 return -EINVAL; 2070 } 2071 nr_args--; 2072 } 2073 2074 err = 0; 2075 for (i = 0; i < nr_args; i++) { 2076 const struct btf_type *arg_type; 2077 u32 arg_type_id; 2078 2079 arg_type_id = args[i].type; 2080 arg_type = btf_type_by_id(btf, arg_type_id); 2081 if (!arg_type) { 2082 btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1); 2083 err = -EINVAL; 2084 break; 2085 } 2086 2087 if (args[i].name_off && 2088 (!btf_name_offset_valid(btf, args[i].name_off) || 2089 !btf_name_valid_identifier(btf, args[i].name_off))) { 2090 btf_verifier_log_type(env, t, 2091 "Invalid arg#%u", i + 1); 2092 err = -EINVAL; 2093 break; 2094 } 2095 2096 if (btf_type_needs_resolve(arg_type) && 2097 !env_type_is_resolved(env, arg_type_id)) { 2098 err = btf_resolve(env, arg_type, arg_type_id); 2099 if (err) 2100 break; 2101 } 2102 2103 if (!btf_type_id_size(btf, &arg_type_id, NULL)) { 2104 btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1); 2105 err = -EINVAL; 2106 break; 2107 } 2108 } 2109 2110 return err; 2111 } 2112 2113 static int btf_func_check(struct btf_verifier_env *env, 2114 const struct btf_type *t) 2115 { 2116 const struct btf_type *proto_type; 2117 const struct btf_param *args; 2118 const struct btf *btf; 2119 u16 nr_args, i; 2120 2121 btf = env->btf; 2122 proto_type = btf_type_by_id(btf, t->type); 2123 2124 if (!proto_type || !btf_type_is_func_proto(proto_type)) { 2125 btf_verifier_log_type(env, t, "Invalid type_id"); 2126 return -EINVAL; 2127 } 2128 2129 args = (const struct btf_param *)(proto_type + 1); 2130 nr_args = btf_type_vlen(proto_type); 2131 for (i = 0; i < nr_args; i++) { 2132 if (!args[i].name_off && args[i].type) { 2133 btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1); 2134 return -EINVAL; 2135 } 2136 } 2137 2138 return 0; 2139 } 2140 2141 static const struct btf_kind_operations * const kind_ops[NR_BTF_KINDS] = { 2142 [BTF_KIND_INT] = &int_ops, 2143 [BTF_KIND_PTR] = &ptr_ops, 2144 [BTF_KIND_ARRAY] = &array_ops, 2145 [BTF_KIND_STRUCT] = &struct_ops, 2146 [BTF_KIND_UNION] = &struct_ops, 2147 [BTF_KIND_ENUM] = &enum_ops, 2148 [BTF_KIND_FWD] = &fwd_ops, 2149 [BTF_KIND_TYPEDEF] = &modifier_ops, 2150 [BTF_KIND_VOLATILE] = &modifier_ops, 2151 [BTF_KIND_CONST] = &modifier_ops, 2152 [BTF_KIND_RESTRICT] = &modifier_ops, 2153 [BTF_KIND_FUNC] = &func_ops, 2154 [BTF_KIND_FUNC_PROTO] = &func_proto_ops, 2155 }; 2156 2157 static s32 btf_check_meta(struct btf_verifier_env *env, 2158 const struct btf_type *t, 2159 u32 meta_left) 2160 { 2161 u32 saved_meta_left = meta_left; 2162 s32 var_meta_size; 2163 2164 if (meta_left < sizeof(*t)) { 2165 btf_verifier_log(env, "[%u] meta_left:%u meta_needed:%zu", 2166 env->log_type_id, meta_left, sizeof(*t)); 2167 return -EINVAL; 2168 } 2169 meta_left -= sizeof(*t); 2170 2171 if (t->info & ~BTF_INFO_MASK) { 2172 btf_verifier_log(env, "[%u] Invalid btf_info:%x", 2173 env->log_type_id, t->info); 2174 return -EINVAL; 2175 } 2176 2177 if (BTF_INFO_KIND(t->info) > BTF_KIND_MAX || 2178 BTF_INFO_KIND(t->info) == BTF_KIND_UNKN) { 2179 btf_verifier_log(env, "[%u] Invalid kind:%u", 2180 env->log_type_id, BTF_INFO_KIND(t->info)); 2181 return -EINVAL; 2182 } 2183 2184 if (!btf_name_offset_valid(env->btf, t->name_off)) { 2185 btf_verifier_log(env, "[%u] Invalid name_offset:%u", 2186 env->log_type_id, t->name_off); 2187 return -EINVAL; 2188 } 2189 2190 var_meta_size = btf_type_ops(t)->check_meta(env, t, meta_left); 2191 if (var_meta_size < 0) 2192 return var_meta_size; 2193 2194 meta_left -= var_meta_size; 2195 2196 return saved_meta_left - meta_left; 2197 } 2198 2199 static int btf_check_all_metas(struct btf_verifier_env *env) 2200 { 2201 struct btf *btf = env->btf; 2202 struct btf_header *hdr; 2203 void *cur, *end; 2204 2205 hdr = &btf->hdr; 2206 cur = btf->nohdr_data + hdr->type_off; 2207 end = cur + hdr->type_len; 2208 2209 env->log_type_id = 1; 2210 while (cur < end) { 2211 struct btf_type *t = cur; 2212 s32 meta_size; 2213 2214 meta_size = btf_check_meta(env, t, end - cur); 2215 if (meta_size < 0) 2216 return meta_size; 2217 2218 btf_add_type(env, t); 2219 cur += meta_size; 2220 env->log_type_id++; 2221 } 2222 2223 return 0; 2224 } 2225 2226 static bool btf_resolve_valid(struct btf_verifier_env *env, 2227 const struct btf_type *t, 2228 u32 type_id) 2229 { 2230 struct btf *btf = env->btf; 2231 2232 if (!env_type_is_resolved(env, type_id)) 2233 return false; 2234 2235 if (btf_type_is_struct(t)) 2236 return !btf->resolved_ids[type_id] && 2237 !btf->resolved_sizes[type_id]; 2238 2239 if (btf_type_is_modifier(t) || btf_type_is_ptr(t)) { 2240 t = btf_type_id_resolve(btf, &type_id); 2241 return t && !btf_type_is_modifier(t); 2242 } 2243 2244 if (btf_type_is_array(t)) { 2245 const struct btf_array *array = btf_type_array(t); 2246 const struct btf_type *elem_type; 2247 u32 elem_type_id = array->type; 2248 u32 elem_size; 2249 2250 elem_type = btf_type_id_size(btf, &elem_type_id, &elem_size); 2251 return elem_type && !btf_type_is_modifier(elem_type) && 2252 (array->nelems * elem_size == 2253 btf->resolved_sizes[type_id]); 2254 } 2255 2256 return false; 2257 } 2258 2259 static int btf_resolve(struct btf_verifier_env *env, 2260 const struct btf_type *t, u32 type_id) 2261 { 2262 u32 save_log_type_id = env->log_type_id; 2263 const struct resolve_vertex *v; 2264 int err = 0; 2265 2266 env->resolve_mode = RESOLVE_TBD; 2267 env_stack_push(env, t, type_id); 2268 while (!err && (v = env_stack_peak(env))) { 2269 env->log_type_id = v->type_id; 2270 err = btf_type_ops(v->t)->resolve(env, v); 2271 } 2272 2273 env->log_type_id = type_id; 2274 if (err == -E2BIG) { 2275 btf_verifier_log_type(env, t, 2276 "Exceeded max resolving depth:%u", 2277 MAX_RESOLVE_DEPTH); 2278 } else if (err == -EEXIST) { 2279 btf_verifier_log_type(env, t, "Loop detected"); 2280 } 2281 2282 /* Final sanity check */ 2283 if (!err && !btf_resolve_valid(env, t, type_id)) { 2284 btf_verifier_log_type(env, t, "Invalid resolve state"); 2285 err = -EINVAL; 2286 } 2287 2288 env->log_type_id = save_log_type_id; 2289 return err; 2290 } 2291 2292 static int btf_check_all_types(struct btf_verifier_env *env) 2293 { 2294 struct btf *btf = env->btf; 2295 u32 type_id; 2296 int err; 2297 2298 err = env_resolve_init(env); 2299 if (err) 2300 return err; 2301 2302 env->phase++; 2303 for (type_id = 1; type_id <= btf->nr_types; type_id++) { 2304 const struct btf_type *t = btf_type_by_id(btf, type_id); 2305 2306 env->log_type_id = type_id; 2307 if (btf_type_needs_resolve(t) && 2308 !env_type_is_resolved(env, type_id)) { 2309 err = btf_resolve(env, t, type_id); 2310 if (err) 2311 return err; 2312 } 2313 2314 if (btf_type_is_func_proto(t)) { 2315 err = btf_func_proto_check(env, t); 2316 if (err) 2317 return err; 2318 } 2319 2320 if (btf_type_is_func(t)) { 2321 err = btf_func_check(env, t); 2322 if (err) 2323 return err; 2324 } 2325 } 2326 2327 return 0; 2328 } 2329 2330 static int btf_parse_type_sec(struct btf_verifier_env *env) 2331 { 2332 const struct btf_header *hdr = &env->btf->hdr; 2333 int err; 2334 2335 /* Type section must align to 4 bytes */ 2336 if (hdr->type_off & (sizeof(u32) - 1)) { 2337 btf_verifier_log(env, "Unaligned type_off"); 2338 return -EINVAL; 2339 } 2340 2341 if (!hdr->type_len) { 2342 btf_verifier_log(env, "No type found"); 2343 return -EINVAL; 2344 } 2345 2346 err = btf_check_all_metas(env); 2347 if (err) 2348 return err; 2349 2350 return btf_check_all_types(env); 2351 } 2352 2353 static int btf_parse_str_sec(struct btf_verifier_env *env) 2354 { 2355 const struct btf_header *hdr; 2356 struct btf *btf = env->btf; 2357 const char *start, *end; 2358 2359 hdr = &btf->hdr; 2360 start = btf->nohdr_data + hdr->str_off; 2361 end = start + hdr->str_len; 2362 2363 if (end != btf->data + btf->data_size) { 2364 btf_verifier_log(env, "String section is not at the end"); 2365 return -EINVAL; 2366 } 2367 2368 if (!hdr->str_len || hdr->str_len - 1 > BTF_MAX_NAME_OFFSET || 2369 start[0] || end[-1]) { 2370 btf_verifier_log(env, "Invalid string section"); 2371 return -EINVAL; 2372 } 2373 2374 btf->strings = start; 2375 2376 return 0; 2377 } 2378 2379 static const size_t btf_sec_info_offset[] = { 2380 offsetof(struct btf_header, type_off), 2381 offsetof(struct btf_header, str_off), 2382 }; 2383 2384 static int btf_sec_info_cmp(const void *a, const void *b) 2385 { 2386 const struct btf_sec_info *x = a; 2387 const struct btf_sec_info *y = b; 2388 2389 return (int)(x->off - y->off) ? : (int)(x->len - y->len); 2390 } 2391 2392 static int btf_check_sec_info(struct btf_verifier_env *env, 2393 u32 btf_data_size) 2394 { 2395 struct btf_sec_info secs[ARRAY_SIZE(btf_sec_info_offset)]; 2396 u32 total, expected_total, i; 2397 const struct btf_header *hdr; 2398 const struct btf *btf; 2399 2400 btf = env->btf; 2401 hdr = &btf->hdr; 2402 2403 /* Populate the secs from hdr */ 2404 for (i = 0; i < ARRAY_SIZE(btf_sec_info_offset); i++) 2405 secs[i] = *(struct btf_sec_info *)((void *)hdr + 2406 btf_sec_info_offset[i]); 2407 2408 sort(secs, ARRAY_SIZE(btf_sec_info_offset), 2409 sizeof(struct btf_sec_info), btf_sec_info_cmp, NULL); 2410 2411 /* Check for gaps and overlap among sections */ 2412 total = 0; 2413 expected_total = btf_data_size - hdr->hdr_len; 2414 for (i = 0; i < ARRAY_SIZE(btf_sec_info_offset); i++) { 2415 if (expected_total < secs[i].off) { 2416 btf_verifier_log(env, "Invalid section offset"); 2417 return -EINVAL; 2418 } 2419 if (total < secs[i].off) { 2420 /* gap */ 2421 btf_verifier_log(env, "Unsupported section found"); 2422 return -EINVAL; 2423 } 2424 if (total > secs[i].off) { 2425 btf_verifier_log(env, "Section overlap found"); 2426 return -EINVAL; 2427 } 2428 if (expected_total - total < secs[i].len) { 2429 btf_verifier_log(env, 2430 "Total section length too long"); 2431 return -EINVAL; 2432 } 2433 total += secs[i].len; 2434 } 2435 2436 /* There is data other than hdr and known sections */ 2437 if (expected_total != total) { 2438 btf_verifier_log(env, "Unsupported section found"); 2439 return -EINVAL; 2440 } 2441 2442 return 0; 2443 } 2444 2445 static int btf_parse_hdr(struct btf_verifier_env *env) 2446 { 2447 u32 hdr_len, hdr_copy, btf_data_size; 2448 const struct btf_header *hdr; 2449 struct btf *btf; 2450 int err; 2451 2452 btf = env->btf; 2453 btf_data_size = btf->data_size; 2454 2455 if (btf_data_size < 2456 offsetof(struct btf_header, hdr_len) + sizeof(hdr->hdr_len)) { 2457 btf_verifier_log(env, "hdr_len not found"); 2458 return -EINVAL; 2459 } 2460 2461 hdr = btf->data; 2462 hdr_len = hdr->hdr_len; 2463 if (btf_data_size < hdr_len) { 2464 btf_verifier_log(env, "btf_header not found"); 2465 return -EINVAL; 2466 } 2467 2468 /* Ensure the unsupported header fields are zero */ 2469 if (hdr_len > sizeof(btf->hdr)) { 2470 u8 *expected_zero = btf->data + sizeof(btf->hdr); 2471 u8 *end = btf->data + hdr_len; 2472 2473 for (; expected_zero < end; expected_zero++) { 2474 if (*expected_zero) { 2475 btf_verifier_log(env, "Unsupported btf_header"); 2476 return -E2BIG; 2477 } 2478 } 2479 } 2480 2481 hdr_copy = min_t(u32, hdr_len, sizeof(btf->hdr)); 2482 memcpy(&btf->hdr, btf->data, hdr_copy); 2483 2484 hdr = &btf->hdr; 2485 2486 btf_verifier_log_hdr(env, btf_data_size); 2487 2488 if (hdr->magic != BTF_MAGIC) { 2489 btf_verifier_log(env, "Invalid magic"); 2490 return -EINVAL; 2491 } 2492 2493 if (hdr->version != BTF_VERSION) { 2494 btf_verifier_log(env, "Unsupported version"); 2495 return -ENOTSUPP; 2496 } 2497 2498 if (hdr->flags) { 2499 btf_verifier_log(env, "Unsupported flags"); 2500 return -ENOTSUPP; 2501 } 2502 2503 if (btf_data_size == hdr->hdr_len) { 2504 btf_verifier_log(env, "No data"); 2505 return -EINVAL; 2506 } 2507 2508 err = btf_check_sec_info(env, btf_data_size); 2509 if (err) 2510 return err; 2511 2512 return 0; 2513 } 2514 2515 static struct btf *btf_parse(void __user *btf_data, u32 btf_data_size, 2516 u32 log_level, char __user *log_ubuf, u32 log_size) 2517 { 2518 struct btf_verifier_env *env = NULL; 2519 struct bpf_verifier_log *log; 2520 struct btf *btf = NULL; 2521 u8 *data; 2522 int err; 2523 2524 if (btf_data_size > BTF_MAX_SIZE) 2525 return ERR_PTR(-E2BIG); 2526 2527 env = kzalloc(sizeof(*env), GFP_KERNEL | __GFP_NOWARN); 2528 if (!env) 2529 return ERR_PTR(-ENOMEM); 2530 2531 log = &env->log; 2532 if (log_level || log_ubuf || log_size) { 2533 /* user requested verbose verifier output 2534 * and supplied buffer to store the verification trace 2535 */ 2536 log->level = log_level; 2537 log->ubuf = log_ubuf; 2538 log->len_total = log_size; 2539 2540 /* log attributes have to be sane */ 2541 if (log->len_total < 128 || log->len_total > UINT_MAX >> 8 || 2542 !log->level || !log->ubuf) { 2543 err = -EINVAL; 2544 goto errout; 2545 } 2546 } 2547 2548 btf = kzalloc(sizeof(*btf), GFP_KERNEL | __GFP_NOWARN); 2549 if (!btf) { 2550 err = -ENOMEM; 2551 goto errout; 2552 } 2553 env->btf = btf; 2554 2555 data = kvmalloc(btf_data_size, GFP_KERNEL | __GFP_NOWARN); 2556 if (!data) { 2557 err = -ENOMEM; 2558 goto errout; 2559 } 2560 2561 btf->data = data; 2562 btf->data_size = btf_data_size; 2563 2564 if (copy_from_user(data, btf_data, btf_data_size)) { 2565 err = -EFAULT; 2566 goto errout; 2567 } 2568 2569 err = btf_parse_hdr(env); 2570 if (err) 2571 goto errout; 2572 2573 btf->nohdr_data = btf->data + btf->hdr.hdr_len; 2574 2575 err = btf_parse_str_sec(env); 2576 if (err) 2577 goto errout; 2578 2579 err = btf_parse_type_sec(env); 2580 if (err) 2581 goto errout; 2582 2583 if (log->level && bpf_verifier_log_full(log)) { 2584 err = -ENOSPC; 2585 goto errout; 2586 } 2587 2588 btf_verifier_env_free(env); 2589 refcount_set(&btf->refcnt, 1); 2590 return btf; 2591 2592 errout: 2593 btf_verifier_env_free(env); 2594 if (btf) 2595 btf_free(btf); 2596 return ERR_PTR(err); 2597 } 2598 2599 void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj, 2600 struct seq_file *m) 2601 { 2602 const struct btf_type *t = btf_type_by_id(btf, type_id); 2603 2604 btf_type_ops(t)->seq_show(btf, t, type_id, obj, 0, m); 2605 } 2606 2607 static int btf_release(struct inode *inode, struct file *filp) 2608 { 2609 btf_put(filp->private_data); 2610 return 0; 2611 } 2612 2613 const struct file_operations btf_fops = { 2614 .release = btf_release, 2615 }; 2616 2617 static int __btf_new_fd(struct btf *btf) 2618 { 2619 return anon_inode_getfd("btf", &btf_fops, btf, O_RDONLY | O_CLOEXEC); 2620 } 2621 2622 int btf_new_fd(const union bpf_attr *attr) 2623 { 2624 struct btf *btf; 2625 int ret; 2626 2627 btf = btf_parse(u64_to_user_ptr(attr->btf), 2628 attr->btf_size, attr->btf_log_level, 2629 u64_to_user_ptr(attr->btf_log_buf), 2630 attr->btf_log_size); 2631 if (IS_ERR(btf)) 2632 return PTR_ERR(btf); 2633 2634 ret = btf_alloc_id(btf); 2635 if (ret) { 2636 btf_free(btf); 2637 return ret; 2638 } 2639 2640 /* 2641 * The BTF ID is published to the userspace. 2642 * All BTF free must go through call_rcu() from 2643 * now on (i.e. free by calling btf_put()). 2644 */ 2645 2646 ret = __btf_new_fd(btf); 2647 if (ret < 0) 2648 btf_put(btf); 2649 2650 return ret; 2651 } 2652 2653 struct btf *btf_get_by_fd(int fd) 2654 { 2655 struct btf *btf; 2656 struct fd f; 2657 2658 f = fdget(fd); 2659 2660 if (!f.file) 2661 return ERR_PTR(-EBADF); 2662 2663 if (f.file->f_op != &btf_fops) { 2664 fdput(f); 2665 return ERR_PTR(-EINVAL); 2666 } 2667 2668 btf = f.file->private_data; 2669 refcount_inc(&btf->refcnt); 2670 fdput(f); 2671 2672 return btf; 2673 } 2674 2675 int btf_get_info_by_fd(const struct btf *btf, 2676 const union bpf_attr *attr, 2677 union bpf_attr __user *uattr) 2678 { 2679 struct bpf_btf_info __user *uinfo; 2680 struct bpf_btf_info info = {}; 2681 u32 info_copy, btf_copy; 2682 void __user *ubtf; 2683 u32 uinfo_len; 2684 2685 uinfo = u64_to_user_ptr(attr->info.info); 2686 uinfo_len = attr->info.info_len; 2687 2688 info_copy = min_t(u32, uinfo_len, sizeof(info)); 2689 if (copy_from_user(&info, uinfo, info_copy)) 2690 return -EFAULT; 2691 2692 info.id = btf->id; 2693 ubtf = u64_to_user_ptr(info.btf); 2694 btf_copy = min_t(u32, btf->data_size, info.btf_size); 2695 if (copy_to_user(ubtf, btf->data, btf_copy)) 2696 return -EFAULT; 2697 info.btf_size = btf->data_size; 2698 2699 if (copy_to_user(uinfo, &info, info_copy) || 2700 put_user(info_copy, &uattr->info.info_len)) 2701 return -EFAULT; 2702 2703 return 0; 2704 } 2705 2706 int btf_get_fd_by_id(u32 id) 2707 { 2708 struct btf *btf; 2709 int fd; 2710 2711 rcu_read_lock(); 2712 btf = idr_find(&btf_idr, id); 2713 if (!btf || !refcount_inc_not_zero(&btf->refcnt)) 2714 btf = ERR_PTR(-ENOENT); 2715 rcu_read_unlock(); 2716 2717 if (IS_ERR(btf)) 2718 return PTR_ERR(btf); 2719 2720 fd = __btf_new_fd(btf); 2721 if (fd < 0) 2722 btf_put(btf); 2723 2724 return fd; 2725 } 2726 2727 u32 btf_id(const struct btf *btf) 2728 { 2729 return btf->id; 2730 } 2731