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/btf_ids.h> 22 #include <linux/skmsg.h> 23 #include <linux/perf_event.h> 24 #include <linux/bsearch.h> 25 #include <linux/kobject.h> 26 #include <linux/sysfs.h> 27 #include <net/sock.h> 28 29 /* BTF (BPF Type Format) is the meta data format which describes 30 * the data types of BPF program/map. Hence, it basically focus 31 * on the C programming language which the modern BPF is primary 32 * using. 33 * 34 * ELF Section: 35 * ~~~~~~~~~~~ 36 * The BTF data is stored under the ".BTF" ELF section 37 * 38 * struct btf_type: 39 * ~~~~~~~~~~~~~~~ 40 * Each 'struct btf_type' object describes a C data type. 41 * Depending on the type it is describing, a 'struct btf_type' 42 * object may be followed by more data. F.e. 43 * To describe an array, 'struct btf_type' is followed by 44 * 'struct btf_array'. 45 * 46 * 'struct btf_type' and any extra data following it are 47 * 4 bytes aligned. 48 * 49 * Type section: 50 * ~~~~~~~~~~~~~ 51 * The BTF type section contains a list of 'struct btf_type' objects. 52 * Each one describes a C type. Recall from the above section 53 * that a 'struct btf_type' object could be immediately followed by extra 54 * data in order to describe some particular C types. 55 * 56 * type_id: 57 * ~~~~~~~ 58 * Each btf_type object is identified by a type_id. The type_id 59 * is implicitly implied by the location of the btf_type object in 60 * the BTF type section. The first one has type_id 1. The second 61 * one has type_id 2...etc. Hence, an earlier btf_type has 62 * a smaller type_id. 63 * 64 * A btf_type object may refer to another btf_type object by using 65 * type_id (i.e. the "type" in the "struct btf_type"). 66 * 67 * NOTE that we cannot assume any reference-order. 68 * A btf_type object can refer to an earlier btf_type object 69 * but it can also refer to a later btf_type object. 70 * 71 * For example, to describe "const void *". A btf_type 72 * object describing "const" may refer to another btf_type 73 * object describing "void *". This type-reference is done 74 * by specifying type_id: 75 * 76 * [1] CONST (anon) type_id=2 77 * [2] PTR (anon) type_id=0 78 * 79 * The above is the btf_verifier debug log: 80 * - Each line started with "[?]" is a btf_type object 81 * - [?] is the type_id of the btf_type object. 82 * - CONST/PTR is the BTF_KIND_XXX 83 * - "(anon)" is the name of the type. It just 84 * happens that CONST and PTR has no name. 85 * - type_id=XXX is the 'u32 type' in btf_type 86 * 87 * NOTE: "void" has type_id 0 88 * 89 * String section: 90 * ~~~~~~~~~~~~~~ 91 * The BTF string section contains the names used by the type section. 92 * Each string is referred by an "offset" from the beginning of the 93 * string section. 94 * 95 * Each string is '\0' terminated. 96 * 97 * The first character in the string section must be '\0' 98 * which is used to mean 'anonymous'. Some btf_type may not 99 * have a name. 100 */ 101 102 /* BTF verification: 103 * 104 * To verify BTF data, two passes are needed. 105 * 106 * Pass #1 107 * ~~~~~~~ 108 * The first pass is to collect all btf_type objects to 109 * an array: "btf->types". 110 * 111 * Depending on the C type that a btf_type is describing, 112 * a btf_type may be followed by extra data. We don't know 113 * how many btf_type is there, and more importantly we don't 114 * know where each btf_type is located in the type section. 115 * 116 * Without knowing the location of each type_id, most verifications 117 * cannot be done. e.g. an earlier btf_type may refer to a later 118 * btf_type (recall the "const void *" above), so we cannot 119 * check this type-reference in the first pass. 120 * 121 * In the first pass, it still does some verifications (e.g. 122 * checking the name is a valid offset to the string section). 123 * 124 * Pass #2 125 * ~~~~~~~ 126 * The main focus is to resolve a btf_type that is referring 127 * to another type. 128 * 129 * We have to ensure the referring type: 130 * 1) does exist in the BTF (i.e. in btf->types[]) 131 * 2) does not cause a loop: 132 * struct A { 133 * struct B b; 134 * }; 135 * 136 * struct B { 137 * struct A a; 138 * }; 139 * 140 * btf_type_needs_resolve() decides if a btf_type needs 141 * to be resolved. 142 * 143 * The needs_resolve type implements the "resolve()" ops which 144 * essentially does a DFS and detects backedge. 145 * 146 * During resolve (or DFS), different C types have different 147 * "RESOLVED" conditions. 148 * 149 * When resolving a BTF_KIND_STRUCT, we need to resolve all its 150 * members because a member is always referring to another 151 * type. A struct's member can be treated as "RESOLVED" if 152 * it is referring to a BTF_KIND_PTR. Otherwise, the 153 * following valid C struct would be rejected: 154 * 155 * struct A { 156 * int m; 157 * struct A *a; 158 * }; 159 * 160 * When resolving a BTF_KIND_PTR, it needs to keep resolving if 161 * it is referring to another BTF_KIND_PTR. Otherwise, we cannot 162 * detect a pointer loop, e.g.: 163 * BTF_KIND_CONST -> BTF_KIND_PTR -> BTF_KIND_CONST -> BTF_KIND_PTR + 164 * ^ | 165 * +-----------------------------------------+ 166 * 167 */ 168 169 #define BITS_PER_U128 (sizeof(u64) * BITS_PER_BYTE * 2) 170 #define BITS_PER_BYTE_MASK (BITS_PER_BYTE - 1) 171 #define BITS_PER_BYTE_MASKED(bits) ((bits) & BITS_PER_BYTE_MASK) 172 #define BITS_ROUNDDOWN_BYTES(bits) ((bits) >> 3) 173 #define BITS_ROUNDUP_BYTES(bits) \ 174 (BITS_ROUNDDOWN_BYTES(bits) + !!BITS_PER_BYTE_MASKED(bits)) 175 176 #define BTF_INFO_MASK 0x9f00ffff 177 #define BTF_INT_MASK 0x0fffffff 178 #define BTF_TYPE_ID_VALID(type_id) ((type_id) <= BTF_MAX_TYPE) 179 #define BTF_STR_OFFSET_VALID(name_off) ((name_off) <= BTF_MAX_NAME_OFFSET) 180 181 /* 16MB for 64k structs and each has 16 members and 182 * a few MB spaces for the string section. 183 * The hard limit is S32_MAX. 184 */ 185 #define BTF_MAX_SIZE (16 * 1024 * 1024) 186 187 #define for_each_member_from(i, from, struct_type, member) \ 188 for (i = from, member = btf_type_member(struct_type) + from; \ 189 i < btf_type_vlen(struct_type); \ 190 i++, member++) 191 192 #define for_each_vsi_from(i, from, struct_type, member) \ 193 for (i = from, member = btf_type_var_secinfo(struct_type) + from; \ 194 i < btf_type_vlen(struct_type); \ 195 i++, member++) 196 197 DEFINE_IDR(btf_idr); 198 DEFINE_SPINLOCK(btf_idr_lock); 199 200 struct btf { 201 void *data; 202 struct btf_type **types; 203 u32 *resolved_ids; 204 u32 *resolved_sizes; 205 const char *strings; 206 void *nohdr_data; 207 struct btf_header hdr; 208 u32 nr_types; /* includes VOID for base BTF */ 209 u32 types_size; 210 u32 data_size; 211 refcount_t refcnt; 212 u32 id; 213 struct rcu_head rcu; 214 215 /* split BTF support */ 216 struct btf *base_btf; 217 u32 start_id; /* first type ID in this BTF (0 for base BTF) */ 218 u32 start_str_off; /* first string offset (0 for base BTF) */ 219 char name[MODULE_NAME_LEN]; 220 bool kernel_btf; 221 }; 222 223 enum verifier_phase { 224 CHECK_META, 225 CHECK_TYPE, 226 }; 227 228 struct resolve_vertex { 229 const struct btf_type *t; 230 u32 type_id; 231 u16 next_member; 232 }; 233 234 enum visit_state { 235 NOT_VISITED, 236 VISITED, 237 RESOLVED, 238 }; 239 240 enum resolve_mode { 241 RESOLVE_TBD, /* To Be Determined */ 242 RESOLVE_PTR, /* Resolving for Pointer */ 243 RESOLVE_STRUCT_OR_ARRAY, /* Resolving for struct/union 244 * or array 245 */ 246 }; 247 248 #define MAX_RESOLVE_DEPTH 32 249 250 struct btf_sec_info { 251 u32 off; 252 u32 len; 253 }; 254 255 struct btf_verifier_env { 256 struct btf *btf; 257 u8 *visit_states; 258 struct resolve_vertex stack[MAX_RESOLVE_DEPTH]; 259 struct bpf_verifier_log log; 260 u32 log_type_id; 261 u32 top_stack; 262 enum verifier_phase phase; 263 enum resolve_mode resolve_mode; 264 }; 265 266 static const char * const btf_kind_str[NR_BTF_KINDS] = { 267 [BTF_KIND_UNKN] = "UNKNOWN", 268 [BTF_KIND_INT] = "INT", 269 [BTF_KIND_PTR] = "PTR", 270 [BTF_KIND_ARRAY] = "ARRAY", 271 [BTF_KIND_STRUCT] = "STRUCT", 272 [BTF_KIND_UNION] = "UNION", 273 [BTF_KIND_ENUM] = "ENUM", 274 [BTF_KIND_FWD] = "FWD", 275 [BTF_KIND_TYPEDEF] = "TYPEDEF", 276 [BTF_KIND_VOLATILE] = "VOLATILE", 277 [BTF_KIND_CONST] = "CONST", 278 [BTF_KIND_RESTRICT] = "RESTRICT", 279 [BTF_KIND_FUNC] = "FUNC", 280 [BTF_KIND_FUNC_PROTO] = "FUNC_PROTO", 281 [BTF_KIND_VAR] = "VAR", 282 [BTF_KIND_DATASEC] = "DATASEC", 283 [BTF_KIND_FLOAT] = "FLOAT", 284 [BTF_KIND_DECL_TAG] = "DECL_TAG", 285 [BTF_KIND_TYPE_TAG] = "TYPE_TAG", 286 }; 287 288 const char *btf_type_str(const struct btf_type *t) 289 { 290 return btf_kind_str[BTF_INFO_KIND(t->info)]; 291 } 292 293 /* Chunk size we use in safe copy of data to be shown. */ 294 #define BTF_SHOW_OBJ_SAFE_SIZE 32 295 296 /* 297 * This is the maximum size of a base type value (equivalent to a 298 * 128-bit int); if we are at the end of our safe buffer and have 299 * less than 16 bytes space we can't be assured of being able 300 * to copy the next type safely, so in such cases we will initiate 301 * a new copy. 302 */ 303 #define BTF_SHOW_OBJ_BASE_TYPE_SIZE 16 304 305 /* Type name size */ 306 #define BTF_SHOW_NAME_SIZE 80 307 308 /* 309 * Common data to all BTF show operations. Private show functions can add 310 * their own data to a structure containing a struct btf_show and consult it 311 * in the show callback. See btf_type_show() below. 312 * 313 * One challenge with showing nested data is we want to skip 0-valued 314 * data, but in order to figure out whether a nested object is all zeros 315 * we need to walk through it. As a result, we need to make two passes 316 * when handling structs, unions and arrays; the first path simply looks 317 * for nonzero data, while the second actually does the display. The first 318 * pass is signalled by show->state.depth_check being set, and if we 319 * encounter a non-zero value we set show->state.depth_to_show to 320 * the depth at which we encountered it. When we have completed the 321 * first pass, we will know if anything needs to be displayed if 322 * depth_to_show > depth. See btf_[struct,array]_show() for the 323 * implementation of this. 324 * 325 * Another problem is we want to ensure the data for display is safe to 326 * access. To support this, the anonymous "struct {} obj" tracks the data 327 * object and our safe copy of it. We copy portions of the data needed 328 * to the object "copy" buffer, but because its size is limited to 329 * BTF_SHOW_OBJ_COPY_LEN bytes, multiple copies may be required as we 330 * traverse larger objects for display. 331 * 332 * The various data type show functions all start with a call to 333 * btf_show_start_type() which returns a pointer to the safe copy 334 * of the data needed (or if BTF_SHOW_UNSAFE is specified, to the 335 * raw data itself). btf_show_obj_safe() is responsible for 336 * using copy_from_kernel_nofault() to update the safe data if necessary 337 * as we traverse the object's data. skbuff-like semantics are 338 * used: 339 * 340 * - obj.head points to the start of the toplevel object for display 341 * - obj.size is the size of the toplevel object 342 * - obj.data points to the current point in the original data at 343 * which our safe data starts. obj.data will advance as we copy 344 * portions of the data. 345 * 346 * In most cases a single copy will suffice, but larger data structures 347 * such as "struct task_struct" will require many copies. The logic in 348 * btf_show_obj_safe() handles the logic that determines if a new 349 * copy_from_kernel_nofault() is needed. 350 */ 351 struct btf_show { 352 u64 flags; 353 void *target; /* target of show operation (seq file, buffer) */ 354 void (*showfn)(struct btf_show *show, const char *fmt, va_list args); 355 const struct btf *btf; 356 /* below are used during iteration */ 357 struct { 358 u8 depth; 359 u8 depth_to_show; 360 u8 depth_check; 361 u8 array_member:1, 362 array_terminated:1; 363 u16 array_encoding; 364 u32 type_id; 365 int status; /* non-zero for error */ 366 const struct btf_type *type; 367 const struct btf_member *member; 368 char name[BTF_SHOW_NAME_SIZE]; /* space for member name/type */ 369 } state; 370 struct { 371 u32 size; 372 void *head; 373 void *data; 374 u8 safe[BTF_SHOW_OBJ_SAFE_SIZE]; 375 } obj; 376 }; 377 378 struct btf_kind_operations { 379 s32 (*check_meta)(struct btf_verifier_env *env, 380 const struct btf_type *t, 381 u32 meta_left); 382 int (*resolve)(struct btf_verifier_env *env, 383 const struct resolve_vertex *v); 384 int (*check_member)(struct btf_verifier_env *env, 385 const struct btf_type *struct_type, 386 const struct btf_member *member, 387 const struct btf_type *member_type); 388 int (*check_kflag_member)(struct btf_verifier_env *env, 389 const struct btf_type *struct_type, 390 const struct btf_member *member, 391 const struct btf_type *member_type); 392 void (*log_details)(struct btf_verifier_env *env, 393 const struct btf_type *t); 394 void (*show)(const struct btf *btf, const struct btf_type *t, 395 u32 type_id, void *data, u8 bits_offsets, 396 struct btf_show *show); 397 }; 398 399 static const struct btf_kind_operations * const kind_ops[NR_BTF_KINDS]; 400 static struct btf_type btf_void; 401 402 static int btf_resolve(struct btf_verifier_env *env, 403 const struct btf_type *t, u32 type_id); 404 405 static bool btf_type_is_modifier(const struct btf_type *t) 406 { 407 /* Some of them is not strictly a C modifier 408 * but they are grouped into the same bucket 409 * for BTF concern: 410 * A type (t) that refers to another 411 * type through t->type AND its size cannot 412 * be determined without following the t->type. 413 * 414 * ptr does not fall into this bucket 415 * because its size is always sizeof(void *). 416 */ 417 switch (BTF_INFO_KIND(t->info)) { 418 case BTF_KIND_TYPEDEF: 419 case BTF_KIND_VOLATILE: 420 case BTF_KIND_CONST: 421 case BTF_KIND_RESTRICT: 422 case BTF_KIND_TYPE_TAG: 423 return true; 424 } 425 426 return false; 427 } 428 429 bool btf_type_is_void(const struct btf_type *t) 430 { 431 return t == &btf_void; 432 } 433 434 static bool btf_type_is_fwd(const struct btf_type *t) 435 { 436 return BTF_INFO_KIND(t->info) == BTF_KIND_FWD; 437 } 438 439 static bool btf_type_nosize(const struct btf_type *t) 440 { 441 return btf_type_is_void(t) || btf_type_is_fwd(t) || 442 btf_type_is_func(t) || btf_type_is_func_proto(t); 443 } 444 445 static bool btf_type_nosize_or_null(const struct btf_type *t) 446 { 447 return !t || btf_type_nosize(t); 448 } 449 450 static bool __btf_type_is_struct(const struct btf_type *t) 451 { 452 return BTF_INFO_KIND(t->info) == BTF_KIND_STRUCT; 453 } 454 455 static bool btf_type_is_array(const struct btf_type *t) 456 { 457 return BTF_INFO_KIND(t->info) == BTF_KIND_ARRAY; 458 } 459 460 static bool btf_type_is_datasec(const struct btf_type *t) 461 { 462 return BTF_INFO_KIND(t->info) == BTF_KIND_DATASEC; 463 } 464 465 static bool btf_type_is_decl_tag(const struct btf_type *t) 466 { 467 return BTF_INFO_KIND(t->info) == BTF_KIND_DECL_TAG; 468 } 469 470 static bool btf_type_is_decl_tag_target(const struct btf_type *t) 471 { 472 return btf_type_is_func(t) || btf_type_is_struct(t) || 473 btf_type_is_var(t) || btf_type_is_typedef(t); 474 } 475 476 u32 btf_nr_types(const struct btf *btf) 477 { 478 u32 total = 0; 479 480 while (btf) { 481 total += btf->nr_types; 482 btf = btf->base_btf; 483 } 484 485 return total; 486 } 487 488 s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind) 489 { 490 const struct btf_type *t; 491 const char *tname; 492 u32 i, total; 493 494 total = btf_nr_types(btf); 495 for (i = 1; i < total; i++) { 496 t = btf_type_by_id(btf, i); 497 if (BTF_INFO_KIND(t->info) != kind) 498 continue; 499 500 tname = btf_name_by_offset(btf, t->name_off); 501 if (!strcmp(tname, name)) 502 return i; 503 } 504 505 return -ENOENT; 506 } 507 508 const struct btf_type *btf_type_skip_modifiers(const struct btf *btf, 509 u32 id, u32 *res_id) 510 { 511 const struct btf_type *t = btf_type_by_id(btf, id); 512 513 while (btf_type_is_modifier(t)) { 514 id = t->type; 515 t = btf_type_by_id(btf, t->type); 516 } 517 518 if (res_id) 519 *res_id = id; 520 521 return t; 522 } 523 524 const struct btf_type *btf_type_resolve_ptr(const struct btf *btf, 525 u32 id, u32 *res_id) 526 { 527 const struct btf_type *t; 528 529 t = btf_type_skip_modifiers(btf, id, NULL); 530 if (!btf_type_is_ptr(t)) 531 return NULL; 532 533 return btf_type_skip_modifiers(btf, t->type, res_id); 534 } 535 536 const struct btf_type *btf_type_resolve_func_ptr(const struct btf *btf, 537 u32 id, u32 *res_id) 538 { 539 const struct btf_type *ptype; 540 541 ptype = btf_type_resolve_ptr(btf, id, res_id); 542 if (ptype && btf_type_is_func_proto(ptype)) 543 return ptype; 544 545 return NULL; 546 } 547 548 /* Types that act only as a source, not sink or intermediate 549 * type when resolving. 550 */ 551 static bool btf_type_is_resolve_source_only(const struct btf_type *t) 552 { 553 return btf_type_is_var(t) || 554 btf_type_is_decl_tag(t) || 555 btf_type_is_datasec(t); 556 } 557 558 /* What types need to be resolved? 559 * 560 * btf_type_is_modifier() is an obvious one. 561 * 562 * btf_type_is_struct() because its member refers to 563 * another type (through member->type). 564 * 565 * btf_type_is_var() because the variable refers to 566 * another type. btf_type_is_datasec() holds multiple 567 * btf_type_is_var() types that need resolving. 568 * 569 * btf_type_is_array() because its element (array->type) 570 * refers to another type. Array can be thought of a 571 * special case of struct while array just has the same 572 * member-type repeated by array->nelems of times. 573 */ 574 static bool btf_type_needs_resolve(const struct btf_type *t) 575 { 576 return btf_type_is_modifier(t) || 577 btf_type_is_ptr(t) || 578 btf_type_is_struct(t) || 579 btf_type_is_array(t) || 580 btf_type_is_var(t) || 581 btf_type_is_decl_tag(t) || 582 btf_type_is_datasec(t); 583 } 584 585 /* t->size can be used */ 586 static bool btf_type_has_size(const struct btf_type *t) 587 { 588 switch (BTF_INFO_KIND(t->info)) { 589 case BTF_KIND_INT: 590 case BTF_KIND_STRUCT: 591 case BTF_KIND_UNION: 592 case BTF_KIND_ENUM: 593 case BTF_KIND_DATASEC: 594 case BTF_KIND_FLOAT: 595 return true; 596 } 597 598 return false; 599 } 600 601 static const char *btf_int_encoding_str(u8 encoding) 602 { 603 if (encoding == 0) 604 return "(none)"; 605 else if (encoding == BTF_INT_SIGNED) 606 return "SIGNED"; 607 else if (encoding == BTF_INT_CHAR) 608 return "CHAR"; 609 else if (encoding == BTF_INT_BOOL) 610 return "BOOL"; 611 else 612 return "UNKN"; 613 } 614 615 static u32 btf_type_int(const struct btf_type *t) 616 { 617 return *(u32 *)(t + 1); 618 } 619 620 static const struct btf_array *btf_type_array(const struct btf_type *t) 621 { 622 return (const struct btf_array *)(t + 1); 623 } 624 625 static const struct btf_enum *btf_type_enum(const struct btf_type *t) 626 { 627 return (const struct btf_enum *)(t + 1); 628 } 629 630 static const struct btf_var *btf_type_var(const struct btf_type *t) 631 { 632 return (const struct btf_var *)(t + 1); 633 } 634 635 static const struct btf_decl_tag *btf_type_decl_tag(const struct btf_type *t) 636 { 637 return (const struct btf_decl_tag *)(t + 1); 638 } 639 640 static const struct btf_kind_operations *btf_type_ops(const struct btf_type *t) 641 { 642 return kind_ops[BTF_INFO_KIND(t->info)]; 643 } 644 645 static bool btf_name_offset_valid(const struct btf *btf, u32 offset) 646 { 647 if (!BTF_STR_OFFSET_VALID(offset)) 648 return false; 649 650 while (offset < btf->start_str_off) 651 btf = btf->base_btf; 652 653 offset -= btf->start_str_off; 654 return offset < btf->hdr.str_len; 655 } 656 657 static bool __btf_name_char_ok(char c, bool first, bool dot_ok) 658 { 659 if ((first ? !isalpha(c) : 660 !isalnum(c)) && 661 c != '_' && 662 ((c == '.' && !dot_ok) || 663 c != '.')) 664 return false; 665 return true; 666 } 667 668 static const char *btf_str_by_offset(const struct btf *btf, u32 offset) 669 { 670 while (offset < btf->start_str_off) 671 btf = btf->base_btf; 672 673 offset -= btf->start_str_off; 674 if (offset < btf->hdr.str_len) 675 return &btf->strings[offset]; 676 677 return NULL; 678 } 679 680 static bool __btf_name_valid(const struct btf *btf, u32 offset, bool dot_ok) 681 { 682 /* offset must be valid */ 683 const char *src = btf_str_by_offset(btf, offset); 684 const char *src_limit; 685 686 if (!__btf_name_char_ok(*src, true, dot_ok)) 687 return false; 688 689 /* set a limit on identifier length */ 690 src_limit = src + KSYM_NAME_LEN; 691 src++; 692 while (*src && src < src_limit) { 693 if (!__btf_name_char_ok(*src, false, dot_ok)) 694 return false; 695 src++; 696 } 697 698 return !*src; 699 } 700 701 /* Only C-style identifier is permitted. This can be relaxed if 702 * necessary. 703 */ 704 static bool btf_name_valid_identifier(const struct btf *btf, u32 offset) 705 { 706 return __btf_name_valid(btf, offset, false); 707 } 708 709 static bool btf_name_valid_section(const struct btf *btf, u32 offset) 710 { 711 return __btf_name_valid(btf, offset, true); 712 } 713 714 static const char *__btf_name_by_offset(const struct btf *btf, u32 offset) 715 { 716 const char *name; 717 718 if (!offset) 719 return "(anon)"; 720 721 name = btf_str_by_offset(btf, offset); 722 return name ?: "(invalid-name-offset)"; 723 } 724 725 const char *btf_name_by_offset(const struct btf *btf, u32 offset) 726 { 727 return btf_str_by_offset(btf, offset); 728 } 729 730 const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id) 731 { 732 while (type_id < btf->start_id) 733 btf = btf->base_btf; 734 735 type_id -= btf->start_id; 736 if (type_id >= btf->nr_types) 737 return NULL; 738 return btf->types[type_id]; 739 } 740 741 /* 742 * Regular int is not a bit field and it must be either 743 * u8/u16/u32/u64 or __int128. 744 */ 745 static bool btf_type_int_is_regular(const struct btf_type *t) 746 { 747 u8 nr_bits, nr_bytes; 748 u32 int_data; 749 750 int_data = btf_type_int(t); 751 nr_bits = BTF_INT_BITS(int_data); 752 nr_bytes = BITS_ROUNDUP_BYTES(nr_bits); 753 if (BITS_PER_BYTE_MASKED(nr_bits) || 754 BTF_INT_OFFSET(int_data) || 755 (nr_bytes != sizeof(u8) && nr_bytes != sizeof(u16) && 756 nr_bytes != sizeof(u32) && nr_bytes != sizeof(u64) && 757 nr_bytes != (2 * sizeof(u64)))) { 758 return false; 759 } 760 761 return true; 762 } 763 764 /* 765 * Check that given struct member is a regular int with expected 766 * offset and size. 767 */ 768 bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s, 769 const struct btf_member *m, 770 u32 expected_offset, u32 expected_size) 771 { 772 const struct btf_type *t; 773 u32 id, int_data; 774 u8 nr_bits; 775 776 id = m->type; 777 t = btf_type_id_size(btf, &id, NULL); 778 if (!t || !btf_type_is_int(t)) 779 return false; 780 781 int_data = btf_type_int(t); 782 nr_bits = BTF_INT_BITS(int_data); 783 if (btf_type_kflag(s)) { 784 u32 bitfield_size = BTF_MEMBER_BITFIELD_SIZE(m->offset); 785 u32 bit_offset = BTF_MEMBER_BIT_OFFSET(m->offset); 786 787 /* if kflag set, int should be a regular int and 788 * bit offset should be at byte boundary. 789 */ 790 return !bitfield_size && 791 BITS_ROUNDUP_BYTES(bit_offset) == expected_offset && 792 BITS_ROUNDUP_BYTES(nr_bits) == expected_size; 793 } 794 795 if (BTF_INT_OFFSET(int_data) || 796 BITS_PER_BYTE_MASKED(m->offset) || 797 BITS_ROUNDUP_BYTES(m->offset) != expected_offset || 798 BITS_PER_BYTE_MASKED(nr_bits) || 799 BITS_ROUNDUP_BYTES(nr_bits) != expected_size) 800 return false; 801 802 return true; 803 } 804 805 /* Similar to btf_type_skip_modifiers() but does not skip typedefs. */ 806 static const struct btf_type *btf_type_skip_qualifiers(const struct btf *btf, 807 u32 id) 808 { 809 const struct btf_type *t = btf_type_by_id(btf, id); 810 811 while (btf_type_is_modifier(t) && 812 BTF_INFO_KIND(t->info) != BTF_KIND_TYPEDEF) { 813 t = btf_type_by_id(btf, t->type); 814 } 815 816 return t; 817 } 818 819 #define BTF_SHOW_MAX_ITER 10 820 821 #define BTF_KIND_BIT(kind) (1ULL << kind) 822 823 /* 824 * Populate show->state.name with type name information. 825 * Format of type name is 826 * 827 * [.member_name = ] (type_name) 828 */ 829 static const char *btf_show_name(struct btf_show *show) 830 { 831 /* BTF_MAX_ITER array suffixes "[]" */ 832 const char *array_suffixes = "[][][][][][][][][][]"; 833 const char *array_suffix = &array_suffixes[strlen(array_suffixes)]; 834 /* BTF_MAX_ITER pointer suffixes "*" */ 835 const char *ptr_suffixes = "**********"; 836 const char *ptr_suffix = &ptr_suffixes[strlen(ptr_suffixes)]; 837 const char *name = NULL, *prefix = "", *parens = ""; 838 const struct btf_member *m = show->state.member; 839 const struct btf_type *t = show->state.type; 840 const struct btf_array *array; 841 u32 id = show->state.type_id; 842 const char *member = NULL; 843 bool show_member = false; 844 u64 kinds = 0; 845 int i; 846 847 show->state.name[0] = '\0'; 848 849 /* 850 * Don't show type name if we're showing an array member; 851 * in that case we show the array type so don't need to repeat 852 * ourselves for each member. 853 */ 854 if (show->state.array_member) 855 return ""; 856 857 /* Retrieve member name, if any. */ 858 if (m) { 859 member = btf_name_by_offset(show->btf, m->name_off); 860 show_member = strlen(member) > 0; 861 id = m->type; 862 } 863 864 /* 865 * Start with type_id, as we have resolved the struct btf_type * 866 * via btf_modifier_show() past the parent typedef to the child 867 * struct, int etc it is defined as. In such cases, the type_id 868 * still represents the starting type while the struct btf_type * 869 * in our show->state points at the resolved type of the typedef. 870 */ 871 t = btf_type_by_id(show->btf, id); 872 if (!t) 873 return ""; 874 875 /* 876 * The goal here is to build up the right number of pointer and 877 * array suffixes while ensuring the type name for a typedef 878 * is represented. Along the way we accumulate a list of 879 * BTF kinds we have encountered, since these will inform later 880 * display; for example, pointer types will not require an 881 * opening "{" for struct, we will just display the pointer value. 882 * 883 * We also want to accumulate the right number of pointer or array 884 * indices in the format string while iterating until we get to 885 * the typedef/pointee/array member target type. 886 * 887 * We start by pointing at the end of pointer and array suffix 888 * strings; as we accumulate pointers and arrays we move the pointer 889 * or array string backwards so it will show the expected number of 890 * '*' or '[]' for the type. BTF_SHOW_MAX_ITER of nesting of pointers 891 * and/or arrays and typedefs are supported as a precaution. 892 * 893 * We also want to get typedef name while proceeding to resolve 894 * type it points to so that we can add parentheses if it is a 895 * "typedef struct" etc. 896 */ 897 for (i = 0; i < BTF_SHOW_MAX_ITER; i++) { 898 899 switch (BTF_INFO_KIND(t->info)) { 900 case BTF_KIND_TYPEDEF: 901 if (!name) 902 name = btf_name_by_offset(show->btf, 903 t->name_off); 904 kinds |= BTF_KIND_BIT(BTF_KIND_TYPEDEF); 905 id = t->type; 906 break; 907 case BTF_KIND_ARRAY: 908 kinds |= BTF_KIND_BIT(BTF_KIND_ARRAY); 909 parens = "["; 910 if (!t) 911 return ""; 912 array = btf_type_array(t); 913 if (array_suffix > array_suffixes) 914 array_suffix -= 2; 915 id = array->type; 916 break; 917 case BTF_KIND_PTR: 918 kinds |= BTF_KIND_BIT(BTF_KIND_PTR); 919 if (ptr_suffix > ptr_suffixes) 920 ptr_suffix -= 1; 921 id = t->type; 922 break; 923 default: 924 id = 0; 925 break; 926 } 927 if (!id) 928 break; 929 t = btf_type_skip_qualifiers(show->btf, id); 930 } 931 /* We may not be able to represent this type; bail to be safe */ 932 if (i == BTF_SHOW_MAX_ITER) 933 return ""; 934 935 if (!name) 936 name = btf_name_by_offset(show->btf, t->name_off); 937 938 switch (BTF_INFO_KIND(t->info)) { 939 case BTF_KIND_STRUCT: 940 case BTF_KIND_UNION: 941 prefix = BTF_INFO_KIND(t->info) == BTF_KIND_STRUCT ? 942 "struct" : "union"; 943 /* if it's an array of struct/union, parens is already set */ 944 if (!(kinds & (BTF_KIND_BIT(BTF_KIND_ARRAY)))) 945 parens = "{"; 946 break; 947 case BTF_KIND_ENUM: 948 prefix = "enum"; 949 break; 950 default: 951 break; 952 } 953 954 /* pointer does not require parens */ 955 if (kinds & BTF_KIND_BIT(BTF_KIND_PTR)) 956 parens = ""; 957 /* typedef does not require struct/union/enum prefix */ 958 if (kinds & BTF_KIND_BIT(BTF_KIND_TYPEDEF)) 959 prefix = ""; 960 961 if (!name) 962 name = ""; 963 964 /* Even if we don't want type name info, we want parentheses etc */ 965 if (show->flags & BTF_SHOW_NONAME) 966 snprintf(show->state.name, sizeof(show->state.name), "%s", 967 parens); 968 else 969 snprintf(show->state.name, sizeof(show->state.name), 970 "%s%s%s(%s%s%s%s%s%s)%s", 971 /* first 3 strings comprise ".member = " */ 972 show_member ? "." : "", 973 show_member ? member : "", 974 show_member ? " = " : "", 975 /* ...next is our prefix (struct, enum, etc) */ 976 prefix, 977 strlen(prefix) > 0 && strlen(name) > 0 ? " " : "", 978 /* ...this is the type name itself */ 979 name, 980 /* ...suffixed by the appropriate '*', '[]' suffixes */ 981 strlen(ptr_suffix) > 0 ? " " : "", ptr_suffix, 982 array_suffix, parens); 983 984 return show->state.name; 985 } 986 987 static const char *__btf_show_indent(struct btf_show *show) 988 { 989 const char *indents = " "; 990 const char *indent = &indents[strlen(indents)]; 991 992 if ((indent - show->state.depth) >= indents) 993 return indent - show->state.depth; 994 return indents; 995 } 996 997 static const char *btf_show_indent(struct btf_show *show) 998 { 999 return show->flags & BTF_SHOW_COMPACT ? "" : __btf_show_indent(show); 1000 } 1001 1002 static const char *btf_show_newline(struct btf_show *show) 1003 { 1004 return show->flags & BTF_SHOW_COMPACT ? "" : "\n"; 1005 } 1006 1007 static const char *btf_show_delim(struct btf_show *show) 1008 { 1009 if (show->state.depth == 0) 1010 return ""; 1011 1012 if ((show->flags & BTF_SHOW_COMPACT) && show->state.type && 1013 BTF_INFO_KIND(show->state.type->info) == BTF_KIND_UNION) 1014 return "|"; 1015 1016 return ","; 1017 } 1018 1019 __printf(2, 3) static void btf_show(struct btf_show *show, const char *fmt, ...) 1020 { 1021 va_list args; 1022 1023 if (!show->state.depth_check) { 1024 va_start(args, fmt); 1025 show->showfn(show, fmt, args); 1026 va_end(args); 1027 } 1028 } 1029 1030 /* Macros are used here as btf_show_type_value[s]() prepends and appends 1031 * format specifiers to the format specifier passed in; these do the work of 1032 * adding indentation, delimiters etc while the caller simply has to specify 1033 * the type value(s) in the format specifier + value(s). 1034 */ 1035 #define btf_show_type_value(show, fmt, value) \ 1036 do { \ 1037 if ((value) != 0 || (show->flags & BTF_SHOW_ZERO) || \ 1038 show->state.depth == 0) { \ 1039 btf_show(show, "%s%s" fmt "%s%s", \ 1040 btf_show_indent(show), \ 1041 btf_show_name(show), \ 1042 value, btf_show_delim(show), \ 1043 btf_show_newline(show)); \ 1044 if (show->state.depth > show->state.depth_to_show) \ 1045 show->state.depth_to_show = show->state.depth; \ 1046 } \ 1047 } while (0) 1048 1049 #define btf_show_type_values(show, fmt, ...) \ 1050 do { \ 1051 btf_show(show, "%s%s" fmt "%s%s", btf_show_indent(show), \ 1052 btf_show_name(show), \ 1053 __VA_ARGS__, btf_show_delim(show), \ 1054 btf_show_newline(show)); \ 1055 if (show->state.depth > show->state.depth_to_show) \ 1056 show->state.depth_to_show = show->state.depth; \ 1057 } while (0) 1058 1059 /* How much is left to copy to safe buffer after @data? */ 1060 static int btf_show_obj_size_left(struct btf_show *show, void *data) 1061 { 1062 return show->obj.head + show->obj.size - data; 1063 } 1064 1065 /* Is object pointed to by @data of @size already copied to our safe buffer? */ 1066 static bool btf_show_obj_is_safe(struct btf_show *show, void *data, int size) 1067 { 1068 return data >= show->obj.data && 1069 (data + size) < (show->obj.data + BTF_SHOW_OBJ_SAFE_SIZE); 1070 } 1071 1072 /* 1073 * If object pointed to by @data of @size falls within our safe buffer, return 1074 * the equivalent pointer to the same safe data. Assumes 1075 * copy_from_kernel_nofault() has already happened and our safe buffer is 1076 * populated. 1077 */ 1078 static void *__btf_show_obj_safe(struct btf_show *show, void *data, int size) 1079 { 1080 if (btf_show_obj_is_safe(show, data, size)) 1081 return show->obj.safe + (data - show->obj.data); 1082 return NULL; 1083 } 1084 1085 /* 1086 * Return a safe-to-access version of data pointed to by @data. 1087 * We do this by copying the relevant amount of information 1088 * to the struct btf_show obj.safe buffer using copy_from_kernel_nofault(). 1089 * 1090 * If BTF_SHOW_UNSAFE is specified, just return data as-is; no 1091 * safe copy is needed. 1092 * 1093 * Otherwise we need to determine if we have the required amount 1094 * of data (determined by the @data pointer and the size of the 1095 * largest base type we can encounter (represented by 1096 * BTF_SHOW_OBJ_BASE_TYPE_SIZE). Having that much data ensures 1097 * that we will be able to print some of the current object, 1098 * and if more is needed a copy will be triggered. 1099 * Some objects such as structs will not fit into the buffer; 1100 * in such cases additional copies when we iterate over their 1101 * members may be needed. 1102 * 1103 * btf_show_obj_safe() is used to return a safe buffer for 1104 * btf_show_start_type(); this ensures that as we recurse into 1105 * nested types we always have safe data for the given type. 1106 * This approach is somewhat wasteful; it's possible for example 1107 * that when iterating over a large union we'll end up copying the 1108 * same data repeatedly, but the goal is safety not performance. 1109 * We use stack data as opposed to per-CPU buffers because the 1110 * iteration over a type can take some time, and preemption handling 1111 * would greatly complicate use of the safe buffer. 1112 */ 1113 static void *btf_show_obj_safe(struct btf_show *show, 1114 const struct btf_type *t, 1115 void *data) 1116 { 1117 const struct btf_type *rt; 1118 int size_left, size; 1119 void *safe = NULL; 1120 1121 if (show->flags & BTF_SHOW_UNSAFE) 1122 return data; 1123 1124 rt = btf_resolve_size(show->btf, t, &size); 1125 if (IS_ERR(rt)) { 1126 show->state.status = PTR_ERR(rt); 1127 return NULL; 1128 } 1129 1130 /* 1131 * Is this toplevel object? If so, set total object size and 1132 * initialize pointers. Otherwise check if we still fall within 1133 * our safe object data. 1134 */ 1135 if (show->state.depth == 0) { 1136 show->obj.size = size; 1137 show->obj.head = data; 1138 } else { 1139 /* 1140 * If the size of the current object is > our remaining 1141 * safe buffer we _may_ need to do a new copy. However 1142 * consider the case of a nested struct; it's size pushes 1143 * us over the safe buffer limit, but showing any individual 1144 * struct members does not. In such cases, we don't need 1145 * to initiate a fresh copy yet; however we definitely need 1146 * at least BTF_SHOW_OBJ_BASE_TYPE_SIZE bytes left 1147 * in our buffer, regardless of the current object size. 1148 * The logic here is that as we resolve types we will 1149 * hit a base type at some point, and we need to be sure 1150 * the next chunk of data is safely available to display 1151 * that type info safely. We cannot rely on the size of 1152 * the current object here because it may be much larger 1153 * than our current buffer (e.g. task_struct is 8k). 1154 * All we want to do here is ensure that we can print the 1155 * next basic type, which we can if either 1156 * - the current type size is within the safe buffer; or 1157 * - at least BTF_SHOW_OBJ_BASE_TYPE_SIZE bytes are left in 1158 * the safe buffer. 1159 */ 1160 safe = __btf_show_obj_safe(show, data, 1161 min(size, 1162 BTF_SHOW_OBJ_BASE_TYPE_SIZE)); 1163 } 1164 1165 /* 1166 * We need a new copy to our safe object, either because we haven't 1167 * yet copied and are initializing safe data, or because the data 1168 * we want falls outside the boundaries of the safe object. 1169 */ 1170 if (!safe) { 1171 size_left = btf_show_obj_size_left(show, data); 1172 if (size_left > BTF_SHOW_OBJ_SAFE_SIZE) 1173 size_left = BTF_SHOW_OBJ_SAFE_SIZE; 1174 show->state.status = copy_from_kernel_nofault(show->obj.safe, 1175 data, size_left); 1176 if (!show->state.status) { 1177 show->obj.data = data; 1178 safe = show->obj.safe; 1179 } 1180 } 1181 1182 return safe; 1183 } 1184 1185 /* 1186 * Set the type we are starting to show and return a safe data pointer 1187 * to be used for showing the associated data. 1188 */ 1189 static void *btf_show_start_type(struct btf_show *show, 1190 const struct btf_type *t, 1191 u32 type_id, void *data) 1192 { 1193 show->state.type = t; 1194 show->state.type_id = type_id; 1195 show->state.name[0] = '\0'; 1196 1197 return btf_show_obj_safe(show, t, data); 1198 } 1199 1200 static void btf_show_end_type(struct btf_show *show) 1201 { 1202 show->state.type = NULL; 1203 show->state.type_id = 0; 1204 show->state.name[0] = '\0'; 1205 } 1206 1207 static void *btf_show_start_aggr_type(struct btf_show *show, 1208 const struct btf_type *t, 1209 u32 type_id, void *data) 1210 { 1211 void *safe_data = btf_show_start_type(show, t, type_id, data); 1212 1213 if (!safe_data) 1214 return safe_data; 1215 1216 btf_show(show, "%s%s%s", btf_show_indent(show), 1217 btf_show_name(show), 1218 btf_show_newline(show)); 1219 show->state.depth++; 1220 return safe_data; 1221 } 1222 1223 static void btf_show_end_aggr_type(struct btf_show *show, 1224 const char *suffix) 1225 { 1226 show->state.depth--; 1227 btf_show(show, "%s%s%s%s", btf_show_indent(show), suffix, 1228 btf_show_delim(show), btf_show_newline(show)); 1229 btf_show_end_type(show); 1230 } 1231 1232 static void btf_show_start_member(struct btf_show *show, 1233 const struct btf_member *m) 1234 { 1235 show->state.member = m; 1236 } 1237 1238 static void btf_show_start_array_member(struct btf_show *show) 1239 { 1240 show->state.array_member = 1; 1241 btf_show_start_member(show, NULL); 1242 } 1243 1244 static void btf_show_end_member(struct btf_show *show) 1245 { 1246 show->state.member = NULL; 1247 } 1248 1249 static void btf_show_end_array_member(struct btf_show *show) 1250 { 1251 show->state.array_member = 0; 1252 btf_show_end_member(show); 1253 } 1254 1255 static void *btf_show_start_array_type(struct btf_show *show, 1256 const struct btf_type *t, 1257 u32 type_id, 1258 u16 array_encoding, 1259 void *data) 1260 { 1261 show->state.array_encoding = array_encoding; 1262 show->state.array_terminated = 0; 1263 return btf_show_start_aggr_type(show, t, type_id, data); 1264 } 1265 1266 static void btf_show_end_array_type(struct btf_show *show) 1267 { 1268 show->state.array_encoding = 0; 1269 show->state.array_terminated = 0; 1270 btf_show_end_aggr_type(show, "]"); 1271 } 1272 1273 static void *btf_show_start_struct_type(struct btf_show *show, 1274 const struct btf_type *t, 1275 u32 type_id, 1276 void *data) 1277 { 1278 return btf_show_start_aggr_type(show, t, type_id, data); 1279 } 1280 1281 static void btf_show_end_struct_type(struct btf_show *show) 1282 { 1283 btf_show_end_aggr_type(show, "}"); 1284 } 1285 1286 __printf(2, 3) static void __btf_verifier_log(struct bpf_verifier_log *log, 1287 const char *fmt, ...) 1288 { 1289 va_list args; 1290 1291 va_start(args, fmt); 1292 bpf_verifier_vlog(log, fmt, args); 1293 va_end(args); 1294 } 1295 1296 __printf(2, 3) static void btf_verifier_log(struct btf_verifier_env *env, 1297 const char *fmt, ...) 1298 { 1299 struct bpf_verifier_log *log = &env->log; 1300 va_list args; 1301 1302 if (!bpf_verifier_log_needed(log)) 1303 return; 1304 1305 va_start(args, fmt); 1306 bpf_verifier_vlog(log, fmt, args); 1307 va_end(args); 1308 } 1309 1310 __printf(4, 5) static void __btf_verifier_log_type(struct btf_verifier_env *env, 1311 const struct btf_type *t, 1312 bool log_details, 1313 const char *fmt, ...) 1314 { 1315 struct bpf_verifier_log *log = &env->log; 1316 u8 kind = BTF_INFO_KIND(t->info); 1317 struct btf *btf = env->btf; 1318 va_list args; 1319 1320 if (!bpf_verifier_log_needed(log)) 1321 return; 1322 1323 /* btf verifier prints all types it is processing via 1324 * btf_verifier_log_type(..., fmt = NULL). 1325 * Skip those prints for in-kernel BTF verification. 1326 */ 1327 if (log->level == BPF_LOG_KERNEL && !fmt) 1328 return; 1329 1330 __btf_verifier_log(log, "[%u] %s %s%s", 1331 env->log_type_id, 1332 btf_kind_str[kind], 1333 __btf_name_by_offset(btf, t->name_off), 1334 log_details ? " " : ""); 1335 1336 if (log_details) 1337 btf_type_ops(t)->log_details(env, t); 1338 1339 if (fmt && *fmt) { 1340 __btf_verifier_log(log, " "); 1341 va_start(args, fmt); 1342 bpf_verifier_vlog(log, fmt, args); 1343 va_end(args); 1344 } 1345 1346 __btf_verifier_log(log, "\n"); 1347 } 1348 1349 #define btf_verifier_log_type(env, t, ...) \ 1350 __btf_verifier_log_type((env), (t), true, __VA_ARGS__) 1351 #define btf_verifier_log_basic(env, t, ...) \ 1352 __btf_verifier_log_type((env), (t), false, __VA_ARGS__) 1353 1354 __printf(4, 5) 1355 static void btf_verifier_log_member(struct btf_verifier_env *env, 1356 const struct btf_type *struct_type, 1357 const struct btf_member *member, 1358 const char *fmt, ...) 1359 { 1360 struct bpf_verifier_log *log = &env->log; 1361 struct btf *btf = env->btf; 1362 va_list args; 1363 1364 if (!bpf_verifier_log_needed(log)) 1365 return; 1366 1367 if (log->level == BPF_LOG_KERNEL && !fmt) 1368 return; 1369 /* The CHECK_META phase already did a btf dump. 1370 * 1371 * If member is logged again, it must hit an error in 1372 * parsing this member. It is useful to print out which 1373 * struct this member belongs to. 1374 */ 1375 if (env->phase != CHECK_META) 1376 btf_verifier_log_type(env, struct_type, NULL); 1377 1378 if (btf_type_kflag(struct_type)) 1379 __btf_verifier_log(log, 1380 "\t%s type_id=%u bitfield_size=%u bits_offset=%u", 1381 __btf_name_by_offset(btf, member->name_off), 1382 member->type, 1383 BTF_MEMBER_BITFIELD_SIZE(member->offset), 1384 BTF_MEMBER_BIT_OFFSET(member->offset)); 1385 else 1386 __btf_verifier_log(log, "\t%s type_id=%u bits_offset=%u", 1387 __btf_name_by_offset(btf, member->name_off), 1388 member->type, member->offset); 1389 1390 if (fmt && *fmt) { 1391 __btf_verifier_log(log, " "); 1392 va_start(args, fmt); 1393 bpf_verifier_vlog(log, fmt, args); 1394 va_end(args); 1395 } 1396 1397 __btf_verifier_log(log, "\n"); 1398 } 1399 1400 __printf(4, 5) 1401 static void btf_verifier_log_vsi(struct btf_verifier_env *env, 1402 const struct btf_type *datasec_type, 1403 const struct btf_var_secinfo *vsi, 1404 const char *fmt, ...) 1405 { 1406 struct bpf_verifier_log *log = &env->log; 1407 va_list args; 1408 1409 if (!bpf_verifier_log_needed(log)) 1410 return; 1411 if (log->level == BPF_LOG_KERNEL && !fmt) 1412 return; 1413 if (env->phase != CHECK_META) 1414 btf_verifier_log_type(env, datasec_type, NULL); 1415 1416 __btf_verifier_log(log, "\t type_id=%u offset=%u size=%u", 1417 vsi->type, vsi->offset, vsi->size); 1418 if (fmt && *fmt) { 1419 __btf_verifier_log(log, " "); 1420 va_start(args, fmt); 1421 bpf_verifier_vlog(log, fmt, args); 1422 va_end(args); 1423 } 1424 1425 __btf_verifier_log(log, "\n"); 1426 } 1427 1428 static void btf_verifier_log_hdr(struct btf_verifier_env *env, 1429 u32 btf_data_size) 1430 { 1431 struct bpf_verifier_log *log = &env->log; 1432 const struct btf *btf = env->btf; 1433 const struct btf_header *hdr; 1434 1435 if (!bpf_verifier_log_needed(log)) 1436 return; 1437 1438 if (log->level == BPF_LOG_KERNEL) 1439 return; 1440 hdr = &btf->hdr; 1441 __btf_verifier_log(log, "magic: 0x%x\n", hdr->magic); 1442 __btf_verifier_log(log, "version: %u\n", hdr->version); 1443 __btf_verifier_log(log, "flags: 0x%x\n", hdr->flags); 1444 __btf_verifier_log(log, "hdr_len: %u\n", hdr->hdr_len); 1445 __btf_verifier_log(log, "type_off: %u\n", hdr->type_off); 1446 __btf_verifier_log(log, "type_len: %u\n", hdr->type_len); 1447 __btf_verifier_log(log, "str_off: %u\n", hdr->str_off); 1448 __btf_verifier_log(log, "str_len: %u\n", hdr->str_len); 1449 __btf_verifier_log(log, "btf_total_size: %u\n", btf_data_size); 1450 } 1451 1452 static int btf_add_type(struct btf_verifier_env *env, struct btf_type *t) 1453 { 1454 struct btf *btf = env->btf; 1455 1456 if (btf->types_size == btf->nr_types) { 1457 /* Expand 'types' array */ 1458 1459 struct btf_type **new_types; 1460 u32 expand_by, new_size; 1461 1462 if (btf->start_id + btf->types_size == BTF_MAX_TYPE) { 1463 btf_verifier_log(env, "Exceeded max num of types"); 1464 return -E2BIG; 1465 } 1466 1467 expand_by = max_t(u32, btf->types_size >> 2, 16); 1468 new_size = min_t(u32, BTF_MAX_TYPE, 1469 btf->types_size + expand_by); 1470 1471 new_types = kvcalloc(new_size, sizeof(*new_types), 1472 GFP_KERNEL | __GFP_NOWARN); 1473 if (!new_types) 1474 return -ENOMEM; 1475 1476 if (btf->nr_types == 0) { 1477 if (!btf->base_btf) { 1478 /* lazily init VOID type */ 1479 new_types[0] = &btf_void; 1480 btf->nr_types++; 1481 } 1482 } else { 1483 memcpy(new_types, btf->types, 1484 sizeof(*btf->types) * btf->nr_types); 1485 } 1486 1487 kvfree(btf->types); 1488 btf->types = new_types; 1489 btf->types_size = new_size; 1490 } 1491 1492 btf->types[btf->nr_types++] = t; 1493 1494 return 0; 1495 } 1496 1497 static int btf_alloc_id(struct btf *btf) 1498 { 1499 int id; 1500 1501 idr_preload(GFP_KERNEL); 1502 spin_lock_bh(&btf_idr_lock); 1503 id = idr_alloc_cyclic(&btf_idr, btf, 1, INT_MAX, GFP_ATOMIC); 1504 if (id > 0) 1505 btf->id = id; 1506 spin_unlock_bh(&btf_idr_lock); 1507 idr_preload_end(); 1508 1509 if (WARN_ON_ONCE(!id)) 1510 return -ENOSPC; 1511 1512 return id > 0 ? 0 : id; 1513 } 1514 1515 static void btf_free_id(struct btf *btf) 1516 { 1517 unsigned long flags; 1518 1519 /* 1520 * In map-in-map, calling map_delete_elem() on outer 1521 * map will call bpf_map_put on the inner map. 1522 * It will then eventually call btf_free_id() 1523 * on the inner map. Some of the map_delete_elem() 1524 * implementation may have irq disabled, so 1525 * we need to use the _irqsave() version instead 1526 * of the _bh() version. 1527 */ 1528 spin_lock_irqsave(&btf_idr_lock, flags); 1529 idr_remove(&btf_idr, btf->id); 1530 spin_unlock_irqrestore(&btf_idr_lock, flags); 1531 } 1532 1533 static void btf_free(struct btf *btf) 1534 { 1535 kvfree(btf->types); 1536 kvfree(btf->resolved_sizes); 1537 kvfree(btf->resolved_ids); 1538 kvfree(btf->data); 1539 kfree(btf); 1540 } 1541 1542 static void btf_free_rcu(struct rcu_head *rcu) 1543 { 1544 struct btf *btf = container_of(rcu, struct btf, rcu); 1545 1546 btf_free(btf); 1547 } 1548 1549 void btf_get(struct btf *btf) 1550 { 1551 refcount_inc(&btf->refcnt); 1552 } 1553 1554 void btf_put(struct btf *btf) 1555 { 1556 if (btf && refcount_dec_and_test(&btf->refcnt)) { 1557 btf_free_id(btf); 1558 call_rcu(&btf->rcu, btf_free_rcu); 1559 } 1560 } 1561 1562 static int env_resolve_init(struct btf_verifier_env *env) 1563 { 1564 struct btf *btf = env->btf; 1565 u32 nr_types = btf->nr_types; 1566 u32 *resolved_sizes = NULL; 1567 u32 *resolved_ids = NULL; 1568 u8 *visit_states = NULL; 1569 1570 resolved_sizes = kvcalloc(nr_types, sizeof(*resolved_sizes), 1571 GFP_KERNEL | __GFP_NOWARN); 1572 if (!resolved_sizes) 1573 goto nomem; 1574 1575 resolved_ids = kvcalloc(nr_types, sizeof(*resolved_ids), 1576 GFP_KERNEL | __GFP_NOWARN); 1577 if (!resolved_ids) 1578 goto nomem; 1579 1580 visit_states = kvcalloc(nr_types, sizeof(*visit_states), 1581 GFP_KERNEL | __GFP_NOWARN); 1582 if (!visit_states) 1583 goto nomem; 1584 1585 btf->resolved_sizes = resolved_sizes; 1586 btf->resolved_ids = resolved_ids; 1587 env->visit_states = visit_states; 1588 1589 return 0; 1590 1591 nomem: 1592 kvfree(resolved_sizes); 1593 kvfree(resolved_ids); 1594 kvfree(visit_states); 1595 return -ENOMEM; 1596 } 1597 1598 static void btf_verifier_env_free(struct btf_verifier_env *env) 1599 { 1600 kvfree(env->visit_states); 1601 kfree(env); 1602 } 1603 1604 static bool env_type_is_resolve_sink(const struct btf_verifier_env *env, 1605 const struct btf_type *next_type) 1606 { 1607 switch (env->resolve_mode) { 1608 case RESOLVE_TBD: 1609 /* int, enum or void is a sink */ 1610 return !btf_type_needs_resolve(next_type); 1611 case RESOLVE_PTR: 1612 /* int, enum, void, struct, array, func or func_proto is a sink 1613 * for ptr 1614 */ 1615 return !btf_type_is_modifier(next_type) && 1616 !btf_type_is_ptr(next_type); 1617 case RESOLVE_STRUCT_OR_ARRAY: 1618 /* int, enum, void, ptr, func or func_proto is a sink 1619 * for struct and array 1620 */ 1621 return !btf_type_is_modifier(next_type) && 1622 !btf_type_is_array(next_type) && 1623 !btf_type_is_struct(next_type); 1624 default: 1625 BUG(); 1626 } 1627 } 1628 1629 static bool env_type_is_resolved(const struct btf_verifier_env *env, 1630 u32 type_id) 1631 { 1632 /* base BTF types should be resolved by now */ 1633 if (type_id < env->btf->start_id) 1634 return true; 1635 1636 return env->visit_states[type_id - env->btf->start_id] == RESOLVED; 1637 } 1638 1639 static int env_stack_push(struct btf_verifier_env *env, 1640 const struct btf_type *t, u32 type_id) 1641 { 1642 const struct btf *btf = env->btf; 1643 struct resolve_vertex *v; 1644 1645 if (env->top_stack == MAX_RESOLVE_DEPTH) 1646 return -E2BIG; 1647 1648 if (type_id < btf->start_id 1649 || env->visit_states[type_id - btf->start_id] != NOT_VISITED) 1650 return -EEXIST; 1651 1652 env->visit_states[type_id - btf->start_id] = VISITED; 1653 1654 v = &env->stack[env->top_stack++]; 1655 v->t = t; 1656 v->type_id = type_id; 1657 v->next_member = 0; 1658 1659 if (env->resolve_mode == RESOLVE_TBD) { 1660 if (btf_type_is_ptr(t)) 1661 env->resolve_mode = RESOLVE_PTR; 1662 else if (btf_type_is_struct(t) || btf_type_is_array(t)) 1663 env->resolve_mode = RESOLVE_STRUCT_OR_ARRAY; 1664 } 1665 1666 return 0; 1667 } 1668 1669 static void env_stack_set_next_member(struct btf_verifier_env *env, 1670 u16 next_member) 1671 { 1672 env->stack[env->top_stack - 1].next_member = next_member; 1673 } 1674 1675 static void env_stack_pop_resolved(struct btf_verifier_env *env, 1676 u32 resolved_type_id, 1677 u32 resolved_size) 1678 { 1679 u32 type_id = env->stack[--(env->top_stack)].type_id; 1680 struct btf *btf = env->btf; 1681 1682 type_id -= btf->start_id; /* adjust to local type id */ 1683 btf->resolved_sizes[type_id] = resolved_size; 1684 btf->resolved_ids[type_id] = resolved_type_id; 1685 env->visit_states[type_id] = RESOLVED; 1686 } 1687 1688 static const struct resolve_vertex *env_stack_peak(struct btf_verifier_env *env) 1689 { 1690 return env->top_stack ? &env->stack[env->top_stack - 1] : NULL; 1691 } 1692 1693 /* Resolve the size of a passed-in "type" 1694 * 1695 * type: is an array (e.g. u32 array[x][y]) 1696 * return type: type "u32[x][y]", i.e. BTF_KIND_ARRAY, 1697 * *type_size: (x * y * sizeof(u32)). Hence, *type_size always 1698 * corresponds to the return type. 1699 * *elem_type: u32 1700 * *elem_id: id of u32 1701 * *total_nelems: (x * y). Hence, individual elem size is 1702 * (*type_size / *total_nelems) 1703 * *type_id: id of type if it's changed within the function, 0 if not 1704 * 1705 * type: is not an array (e.g. const struct X) 1706 * return type: type "struct X" 1707 * *type_size: sizeof(struct X) 1708 * *elem_type: same as return type ("struct X") 1709 * *elem_id: 0 1710 * *total_nelems: 1 1711 * *type_id: id of type if it's changed within the function, 0 if not 1712 */ 1713 static const struct btf_type * 1714 __btf_resolve_size(const struct btf *btf, const struct btf_type *type, 1715 u32 *type_size, const struct btf_type **elem_type, 1716 u32 *elem_id, u32 *total_nelems, u32 *type_id) 1717 { 1718 const struct btf_type *array_type = NULL; 1719 const struct btf_array *array = NULL; 1720 u32 i, size, nelems = 1, id = 0; 1721 1722 for (i = 0; i < MAX_RESOLVE_DEPTH; i++) { 1723 switch (BTF_INFO_KIND(type->info)) { 1724 /* type->size can be used */ 1725 case BTF_KIND_INT: 1726 case BTF_KIND_STRUCT: 1727 case BTF_KIND_UNION: 1728 case BTF_KIND_ENUM: 1729 case BTF_KIND_FLOAT: 1730 size = type->size; 1731 goto resolved; 1732 1733 case BTF_KIND_PTR: 1734 size = sizeof(void *); 1735 goto resolved; 1736 1737 /* Modifiers */ 1738 case BTF_KIND_TYPEDEF: 1739 case BTF_KIND_VOLATILE: 1740 case BTF_KIND_CONST: 1741 case BTF_KIND_RESTRICT: 1742 case BTF_KIND_TYPE_TAG: 1743 id = type->type; 1744 type = btf_type_by_id(btf, type->type); 1745 break; 1746 1747 case BTF_KIND_ARRAY: 1748 if (!array_type) 1749 array_type = type; 1750 array = btf_type_array(type); 1751 if (nelems && array->nelems > U32_MAX / nelems) 1752 return ERR_PTR(-EINVAL); 1753 nelems *= array->nelems; 1754 type = btf_type_by_id(btf, array->type); 1755 break; 1756 1757 /* type without size */ 1758 default: 1759 return ERR_PTR(-EINVAL); 1760 } 1761 } 1762 1763 return ERR_PTR(-EINVAL); 1764 1765 resolved: 1766 if (nelems && size > U32_MAX / nelems) 1767 return ERR_PTR(-EINVAL); 1768 1769 *type_size = nelems * size; 1770 if (total_nelems) 1771 *total_nelems = nelems; 1772 if (elem_type) 1773 *elem_type = type; 1774 if (elem_id) 1775 *elem_id = array ? array->type : 0; 1776 if (type_id && id) 1777 *type_id = id; 1778 1779 return array_type ? : type; 1780 } 1781 1782 const struct btf_type * 1783 btf_resolve_size(const struct btf *btf, const struct btf_type *type, 1784 u32 *type_size) 1785 { 1786 return __btf_resolve_size(btf, type, type_size, NULL, NULL, NULL, NULL); 1787 } 1788 1789 static u32 btf_resolved_type_id(const struct btf *btf, u32 type_id) 1790 { 1791 while (type_id < btf->start_id) 1792 btf = btf->base_btf; 1793 1794 return btf->resolved_ids[type_id - btf->start_id]; 1795 } 1796 1797 /* The input param "type_id" must point to a needs_resolve type */ 1798 static const struct btf_type *btf_type_id_resolve(const struct btf *btf, 1799 u32 *type_id) 1800 { 1801 *type_id = btf_resolved_type_id(btf, *type_id); 1802 return btf_type_by_id(btf, *type_id); 1803 } 1804 1805 static u32 btf_resolved_type_size(const struct btf *btf, u32 type_id) 1806 { 1807 while (type_id < btf->start_id) 1808 btf = btf->base_btf; 1809 1810 return btf->resolved_sizes[type_id - btf->start_id]; 1811 } 1812 1813 const struct btf_type *btf_type_id_size(const struct btf *btf, 1814 u32 *type_id, u32 *ret_size) 1815 { 1816 const struct btf_type *size_type; 1817 u32 size_type_id = *type_id; 1818 u32 size = 0; 1819 1820 size_type = btf_type_by_id(btf, size_type_id); 1821 if (btf_type_nosize_or_null(size_type)) 1822 return NULL; 1823 1824 if (btf_type_has_size(size_type)) { 1825 size = size_type->size; 1826 } else if (btf_type_is_array(size_type)) { 1827 size = btf_resolved_type_size(btf, size_type_id); 1828 } else if (btf_type_is_ptr(size_type)) { 1829 size = sizeof(void *); 1830 } else { 1831 if (WARN_ON_ONCE(!btf_type_is_modifier(size_type) && 1832 !btf_type_is_var(size_type))) 1833 return NULL; 1834 1835 size_type_id = btf_resolved_type_id(btf, size_type_id); 1836 size_type = btf_type_by_id(btf, size_type_id); 1837 if (btf_type_nosize_or_null(size_type)) 1838 return NULL; 1839 else if (btf_type_has_size(size_type)) 1840 size = size_type->size; 1841 else if (btf_type_is_array(size_type)) 1842 size = btf_resolved_type_size(btf, size_type_id); 1843 else if (btf_type_is_ptr(size_type)) 1844 size = sizeof(void *); 1845 else 1846 return NULL; 1847 } 1848 1849 *type_id = size_type_id; 1850 if (ret_size) 1851 *ret_size = size; 1852 1853 return size_type; 1854 } 1855 1856 static int btf_df_check_member(struct btf_verifier_env *env, 1857 const struct btf_type *struct_type, 1858 const struct btf_member *member, 1859 const struct btf_type *member_type) 1860 { 1861 btf_verifier_log_basic(env, struct_type, 1862 "Unsupported check_member"); 1863 return -EINVAL; 1864 } 1865 1866 static int btf_df_check_kflag_member(struct btf_verifier_env *env, 1867 const struct btf_type *struct_type, 1868 const struct btf_member *member, 1869 const struct btf_type *member_type) 1870 { 1871 btf_verifier_log_basic(env, struct_type, 1872 "Unsupported check_kflag_member"); 1873 return -EINVAL; 1874 } 1875 1876 /* Used for ptr, array struct/union and float type members. 1877 * int, enum and modifier types have their specific callback functions. 1878 */ 1879 static int btf_generic_check_kflag_member(struct btf_verifier_env *env, 1880 const struct btf_type *struct_type, 1881 const struct btf_member *member, 1882 const struct btf_type *member_type) 1883 { 1884 if (BTF_MEMBER_BITFIELD_SIZE(member->offset)) { 1885 btf_verifier_log_member(env, struct_type, member, 1886 "Invalid member bitfield_size"); 1887 return -EINVAL; 1888 } 1889 1890 /* bitfield size is 0, so member->offset represents bit offset only. 1891 * It is safe to call non kflag check_member variants. 1892 */ 1893 return btf_type_ops(member_type)->check_member(env, struct_type, 1894 member, 1895 member_type); 1896 } 1897 1898 static int btf_df_resolve(struct btf_verifier_env *env, 1899 const struct resolve_vertex *v) 1900 { 1901 btf_verifier_log_basic(env, v->t, "Unsupported resolve"); 1902 return -EINVAL; 1903 } 1904 1905 static void btf_df_show(const struct btf *btf, const struct btf_type *t, 1906 u32 type_id, void *data, u8 bits_offsets, 1907 struct btf_show *show) 1908 { 1909 btf_show(show, "<unsupported kind:%u>", BTF_INFO_KIND(t->info)); 1910 } 1911 1912 static int btf_int_check_member(struct btf_verifier_env *env, 1913 const struct btf_type *struct_type, 1914 const struct btf_member *member, 1915 const struct btf_type *member_type) 1916 { 1917 u32 int_data = btf_type_int(member_type); 1918 u32 struct_bits_off = member->offset; 1919 u32 struct_size = struct_type->size; 1920 u32 nr_copy_bits; 1921 u32 bytes_offset; 1922 1923 if (U32_MAX - struct_bits_off < BTF_INT_OFFSET(int_data)) { 1924 btf_verifier_log_member(env, struct_type, member, 1925 "bits_offset exceeds U32_MAX"); 1926 return -EINVAL; 1927 } 1928 1929 struct_bits_off += BTF_INT_OFFSET(int_data); 1930 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off); 1931 nr_copy_bits = BTF_INT_BITS(int_data) + 1932 BITS_PER_BYTE_MASKED(struct_bits_off); 1933 1934 if (nr_copy_bits > BITS_PER_U128) { 1935 btf_verifier_log_member(env, struct_type, member, 1936 "nr_copy_bits exceeds 128"); 1937 return -EINVAL; 1938 } 1939 1940 if (struct_size < bytes_offset || 1941 struct_size - bytes_offset < BITS_ROUNDUP_BYTES(nr_copy_bits)) { 1942 btf_verifier_log_member(env, struct_type, member, 1943 "Member exceeds struct_size"); 1944 return -EINVAL; 1945 } 1946 1947 return 0; 1948 } 1949 1950 static int btf_int_check_kflag_member(struct btf_verifier_env *env, 1951 const struct btf_type *struct_type, 1952 const struct btf_member *member, 1953 const struct btf_type *member_type) 1954 { 1955 u32 struct_bits_off, nr_bits, nr_int_data_bits, bytes_offset; 1956 u32 int_data = btf_type_int(member_type); 1957 u32 struct_size = struct_type->size; 1958 u32 nr_copy_bits; 1959 1960 /* a regular int type is required for the kflag int member */ 1961 if (!btf_type_int_is_regular(member_type)) { 1962 btf_verifier_log_member(env, struct_type, member, 1963 "Invalid member base type"); 1964 return -EINVAL; 1965 } 1966 1967 /* check sanity of bitfield size */ 1968 nr_bits = BTF_MEMBER_BITFIELD_SIZE(member->offset); 1969 struct_bits_off = BTF_MEMBER_BIT_OFFSET(member->offset); 1970 nr_int_data_bits = BTF_INT_BITS(int_data); 1971 if (!nr_bits) { 1972 /* Not a bitfield member, member offset must be at byte 1973 * boundary. 1974 */ 1975 if (BITS_PER_BYTE_MASKED(struct_bits_off)) { 1976 btf_verifier_log_member(env, struct_type, member, 1977 "Invalid member offset"); 1978 return -EINVAL; 1979 } 1980 1981 nr_bits = nr_int_data_bits; 1982 } else if (nr_bits > nr_int_data_bits) { 1983 btf_verifier_log_member(env, struct_type, member, 1984 "Invalid member bitfield_size"); 1985 return -EINVAL; 1986 } 1987 1988 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off); 1989 nr_copy_bits = nr_bits + BITS_PER_BYTE_MASKED(struct_bits_off); 1990 if (nr_copy_bits > BITS_PER_U128) { 1991 btf_verifier_log_member(env, struct_type, member, 1992 "nr_copy_bits exceeds 128"); 1993 return -EINVAL; 1994 } 1995 1996 if (struct_size < bytes_offset || 1997 struct_size - bytes_offset < BITS_ROUNDUP_BYTES(nr_copy_bits)) { 1998 btf_verifier_log_member(env, struct_type, member, 1999 "Member exceeds struct_size"); 2000 return -EINVAL; 2001 } 2002 2003 return 0; 2004 } 2005 2006 static s32 btf_int_check_meta(struct btf_verifier_env *env, 2007 const struct btf_type *t, 2008 u32 meta_left) 2009 { 2010 u32 int_data, nr_bits, meta_needed = sizeof(int_data); 2011 u16 encoding; 2012 2013 if (meta_left < meta_needed) { 2014 btf_verifier_log_basic(env, t, 2015 "meta_left:%u meta_needed:%u", 2016 meta_left, meta_needed); 2017 return -EINVAL; 2018 } 2019 2020 if (btf_type_vlen(t)) { 2021 btf_verifier_log_type(env, t, "vlen != 0"); 2022 return -EINVAL; 2023 } 2024 2025 if (btf_type_kflag(t)) { 2026 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); 2027 return -EINVAL; 2028 } 2029 2030 int_data = btf_type_int(t); 2031 if (int_data & ~BTF_INT_MASK) { 2032 btf_verifier_log_basic(env, t, "Invalid int_data:%x", 2033 int_data); 2034 return -EINVAL; 2035 } 2036 2037 nr_bits = BTF_INT_BITS(int_data) + BTF_INT_OFFSET(int_data); 2038 2039 if (nr_bits > BITS_PER_U128) { 2040 btf_verifier_log_type(env, t, "nr_bits exceeds %zu", 2041 BITS_PER_U128); 2042 return -EINVAL; 2043 } 2044 2045 if (BITS_ROUNDUP_BYTES(nr_bits) > t->size) { 2046 btf_verifier_log_type(env, t, "nr_bits exceeds type_size"); 2047 return -EINVAL; 2048 } 2049 2050 /* 2051 * Only one of the encoding bits is allowed and it 2052 * should be sufficient for the pretty print purpose (i.e. decoding). 2053 * Multiple bits can be allowed later if it is found 2054 * to be insufficient. 2055 */ 2056 encoding = BTF_INT_ENCODING(int_data); 2057 if (encoding && 2058 encoding != BTF_INT_SIGNED && 2059 encoding != BTF_INT_CHAR && 2060 encoding != BTF_INT_BOOL) { 2061 btf_verifier_log_type(env, t, "Unsupported encoding"); 2062 return -ENOTSUPP; 2063 } 2064 2065 btf_verifier_log_type(env, t, NULL); 2066 2067 return meta_needed; 2068 } 2069 2070 static void btf_int_log(struct btf_verifier_env *env, 2071 const struct btf_type *t) 2072 { 2073 int int_data = btf_type_int(t); 2074 2075 btf_verifier_log(env, 2076 "size=%u bits_offset=%u nr_bits=%u encoding=%s", 2077 t->size, BTF_INT_OFFSET(int_data), 2078 BTF_INT_BITS(int_data), 2079 btf_int_encoding_str(BTF_INT_ENCODING(int_data))); 2080 } 2081 2082 static void btf_int128_print(struct btf_show *show, void *data) 2083 { 2084 /* data points to a __int128 number. 2085 * Suppose 2086 * int128_num = *(__int128 *)data; 2087 * The below formulas shows what upper_num and lower_num represents: 2088 * upper_num = int128_num >> 64; 2089 * lower_num = int128_num & 0xffffffffFFFFFFFFULL; 2090 */ 2091 u64 upper_num, lower_num; 2092 2093 #ifdef __BIG_ENDIAN_BITFIELD 2094 upper_num = *(u64 *)data; 2095 lower_num = *(u64 *)(data + 8); 2096 #else 2097 upper_num = *(u64 *)(data + 8); 2098 lower_num = *(u64 *)data; 2099 #endif 2100 if (upper_num == 0) 2101 btf_show_type_value(show, "0x%llx", lower_num); 2102 else 2103 btf_show_type_values(show, "0x%llx%016llx", upper_num, 2104 lower_num); 2105 } 2106 2107 static void btf_int128_shift(u64 *print_num, u16 left_shift_bits, 2108 u16 right_shift_bits) 2109 { 2110 u64 upper_num, lower_num; 2111 2112 #ifdef __BIG_ENDIAN_BITFIELD 2113 upper_num = print_num[0]; 2114 lower_num = print_num[1]; 2115 #else 2116 upper_num = print_num[1]; 2117 lower_num = print_num[0]; 2118 #endif 2119 2120 /* shake out un-needed bits by shift/or operations */ 2121 if (left_shift_bits >= 64) { 2122 upper_num = lower_num << (left_shift_bits - 64); 2123 lower_num = 0; 2124 } else { 2125 upper_num = (upper_num << left_shift_bits) | 2126 (lower_num >> (64 - left_shift_bits)); 2127 lower_num = lower_num << left_shift_bits; 2128 } 2129 2130 if (right_shift_bits >= 64) { 2131 lower_num = upper_num >> (right_shift_bits - 64); 2132 upper_num = 0; 2133 } else { 2134 lower_num = (lower_num >> right_shift_bits) | 2135 (upper_num << (64 - right_shift_bits)); 2136 upper_num = upper_num >> right_shift_bits; 2137 } 2138 2139 #ifdef __BIG_ENDIAN_BITFIELD 2140 print_num[0] = upper_num; 2141 print_num[1] = lower_num; 2142 #else 2143 print_num[0] = lower_num; 2144 print_num[1] = upper_num; 2145 #endif 2146 } 2147 2148 static void btf_bitfield_show(void *data, u8 bits_offset, 2149 u8 nr_bits, struct btf_show *show) 2150 { 2151 u16 left_shift_bits, right_shift_bits; 2152 u8 nr_copy_bytes; 2153 u8 nr_copy_bits; 2154 u64 print_num[2] = {}; 2155 2156 nr_copy_bits = nr_bits + bits_offset; 2157 nr_copy_bytes = BITS_ROUNDUP_BYTES(nr_copy_bits); 2158 2159 memcpy(print_num, data, nr_copy_bytes); 2160 2161 #ifdef __BIG_ENDIAN_BITFIELD 2162 left_shift_bits = bits_offset; 2163 #else 2164 left_shift_bits = BITS_PER_U128 - nr_copy_bits; 2165 #endif 2166 right_shift_bits = BITS_PER_U128 - nr_bits; 2167 2168 btf_int128_shift(print_num, left_shift_bits, right_shift_bits); 2169 btf_int128_print(show, print_num); 2170 } 2171 2172 2173 static void btf_int_bits_show(const struct btf *btf, 2174 const struct btf_type *t, 2175 void *data, u8 bits_offset, 2176 struct btf_show *show) 2177 { 2178 u32 int_data = btf_type_int(t); 2179 u8 nr_bits = BTF_INT_BITS(int_data); 2180 u8 total_bits_offset; 2181 2182 /* 2183 * bits_offset is at most 7. 2184 * BTF_INT_OFFSET() cannot exceed 128 bits. 2185 */ 2186 total_bits_offset = bits_offset + BTF_INT_OFFSET(int_data); 2187 data += BITS_ROUNDDOWN_BYTES(total_bits_offset); 2188 bits_offset = BITS_PER_BYTE_MASKED(total_bits_offset); 2189 btf_bitfield_show(data, bits_offset, nr_bits, show); 2190 } 2191 2192 static void btf_int_show(const struct btf *btf, const struct btf_type *t, 2193 u32 type_id, void *data, u8 bits_offset, 2194 struct btf_show *show) 2195 { 2196 u32 int_data = btf_type_int(t); 2197 u8 encoding = BTF_INT_ENCODING(int_data); 2198 bool sign = encoding & BTF_INT_SIGNED; 2199 u8 nr_bits = BTF_INT_BITS(int_data); 2200 void *safe_data; 2201 2202 safe_data = btf_show_start_type(show, t, type_id, data); 2203 if (!safe_data) 2204 return; 2205 2206 if (bits_offset || BTF_INT_OFFSET(int_data) || 2207 BITS_PER_BYTE_MASKED(nr_bits)) { 2208 btf_int_bits_show(btf, t, safe_data, bits_offset, show); 2209 goto out; 2210 } 2211 2212 switch (nr_bits) { 2213 case 128: 2214 btf_int128_print(show, safe_data); 2215 break; 2216 case 64: 2217 if (sign) 2218 btf_show_type_value(show, "%lld", *(s64 *)safe_data); 2219 else 2220 btf_show_type_value(show, "%llu", *(u64 *)safe_data); 2221 break; 2222 case 32: 2223 if (sign) 2224 btf_show_type_value(show, "%d", *(s32 *)safe_data); 2225 else 2226 btf_show_type_value(show, "%u", *(u32 *)safe_data); 2227 break; 2228 case 16: 2229 if (sign) 2230 btf_show_type_value(show, "%d", *(s16 *)safe_data); 2231 else 2232 btf_show_type_value(show, "%u", *(u16 *)safe_data); 2233 break; 2234 case 8: 2235 if (show->state.array_encoding == BTF_INT_CHAR) { 2236 /* check for null terminator */ 2237 if (show->state.array_terminated) 2238 break; 2239 if (*(char *)data == '\0') { 2240 show->state.array_terminated = 1; 2241 break; 2242 } 2243 if (isprint(*(char *)data)) { 2244 btf_show_type_value(show, "'%c'", 2245 *(char *)safe_data); 2246 break; 2247 } 2248 } 2249 if (sign) 2250 btf_show_type_value(show, "%d", *(s8 *)safe_data); 2251 else 2252 btf_show_type_value(show, "%u", *(u8 *)safe_data); 2253 break; 2254 default: 2255 btf_int_bits_show(btf, t, safe_data, bits_offset, show); 2256 break; 2257 } 2258 out: 2259 btf_show_end_type(show); 2260 } 2261 2262 static const struct btf_kind_operations int_ops = { 2263 .check_meta = btf_int_check_meta, 2264 .resolve = btf_df_resolve, 2265 .check_member = btf_int_check_member, 2266 .check_kflag_member = btf_int_check_kflag_member, 2267 .log_details = btf_int_log, 2268 .show = btf_int_show, 2269 }; 2270 2271 static int btf_modifier_check_member(struct btf_verifier_env *env, 2272 const struct btf_type *struct_type, 2273 const struct btf_member *member, 2274 const struct btf_type *member_type) 2275 { 2276 const struct btf_type *resolved_type; 2277 u32 resolved_type_id = member->type; 2278 struct btf_member resolved_member; 2279 struct btf *btf = env->btf; 2280 2281 resolved_type = btf_type_id_size(btf, &resolved_type_id, NULL); 2282 if (!resolved_type) { 2283 btf_verifier_log_member(env, struct_type, member, 2284 "Invalid member"); 2285 return -EINVAL; 2286 } 2287 2288 resolved_member = *member; 2289 resolved_member.type = resolved_type_id; 2290 2291 return btf_type_ops(resolved_type)->check_member(env, struct_type, 2292 &resolved_member, 2293 resolved_type); 2294 } 2295 2296 static int btf_modifier_check_kflag_member(struct btf_verifier_env *env, 2297 const struct btf_type *struct_type, 2298 const struct btf_member *member, 2299 const struct btf_type *member_type) 2300 { 2301 const struct btf_type *resolved_type; 2302 u32 resolved_type_id = member->type; 2303 struct btf_member resolved_member; 2304 struct btf *btf = env->btf; 2305 2306 resolved_type = btf_type_id_size(btf, &resolved_type_id, NULL); 2307 if (!resolved_type) { 2308 btf_verifier_log_member(env, struct_type, member, 2309 "Invalid member"); 2310 return -EINVAL; 2311 } 2312 2313 resolved_member = *member; 2314 resolved_member.type = resolved_type_id; 2315 2316 return btf_type_ops(resolved_type)->check_kflag_member(env, struct_type, 2317 &resolved_member, 2318 resolved_type); 2319 } 2320 2321 static int btf_ptr_check_member(struct btf_verifier_env *env, 2322 const struct btf_type *struct_type, 2323 const struct btf_member *member, 2324 const struct btf_type *member_type) 2325 { 2326 u32 struct_size, struct_bits_off, bytes_offset; 2327 2328 struct_size = struct_type->size; 2329 struct_bits_off = member->offset; 2330 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off); 2331 2332 if (BITS_PER_BYTE_MASKED(struct_bits_off)) { 2333 btf_verifier_log_member(env, struct_type, member, 2334 "Member is not byte aligned"); 2335 return -EINVAL; 2336 } 2337 2338 if (struct_size - bytes_offset < sizeof(void *)) { 2339 btf_verifier_log_member(env, struct_type, member, 2340 "Member exceeds struct_size"); 2341 return -EINVAL; 2342 } 2343 2344 return 0; 2345 } 2346 2347 static int btf_ref_type_check_meta(struct btf_verifier_env *env, 2348 const struct btf_type *t, 2349 u32 meta_left) 2350 { 2351 const char *value; 2352 2353 if (btf_type_vlen(t)) { 2354 btf_verifier_log_type(env, t, "vlen != 0"); 2355 return -EINVAL; 2356 } 2357 2358 if (btf_type_kflag(t)) { 2359 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); 2360 return -EINVAL; 2361 } 2362 2363 if (!BTF_TYPE_ID_VALID(t->type)) { 2364 btf_verifier_log_type(env, t, "Invalid type_id"); 2365 return -EINVAL; 2366 } 2367 2368 /* typedef/type_tag type must have a valid name, and other ref types, 2369 * volatile, const, restrict, should have a null name. 2370 */ 2371 if (BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF) { 2372 if (!t->name_off || 2373 !btf_name_valid_identifier(env->btf, t->name_off)) { 2374 btf_verifier_log_type(env, t, "Invalid name"); 2375 return -EINVAL; 2376 } 2377 } else if (BTF_INFO_KIND(t->info) == BTF_KIND_TYPE_TAG) { 2378 value = btf_name_by_offset(env->btf, t->name_off); 2379 if (!value || !value[0]) { 2380 btf_verifier_log_type(env, t, "Invalid name"); 2381 return -EINVAL; 2382 } 2383 } else { 2384 if (t->name_off) { 2385 btf_verifier_log_type(env, t, "Invalid name"); 2386 return -EINVAL; 2387 } 2388 } 2389 2390 btf_verifier_log_type(env, t, NULL); 2391 2392 return 0; 2393 } 2394 2395 static int btf_modifier_resolve(struct btf_verifier_env *env, 2396 const struct resolve_vertex *v) 2397 { 2398 const struct btf_type *t = v->t; 2399 const struct btf_type *next_type; 2400 u32 next_type_id = t->type; 2401 struct btf *btf = env->btf; 2402 2403 next_type = btf_type_by_id(btf, next_type_id); 2404 if (!next_type || btf_type_is_resolve_source_only(next_type)) { 2405 btf_verifier_log_type(env, v->t, "Invalid type_id"); 2406 return -EINVAL; 2407 } 2408 2409 if (!env_type_is_resolve_sink(env, next_type) && 2410 !env_type_is_resolved(env, next_type_id)) 2411 return env_stack_push(env, next_type, next_type_id); 2412 2413 /* Figure out the resolved next_type_id with size. 2414 * They will be stored in the current modifier's 2415 * resolved_ids and resolved_sizes such that it can 2416 * save us a few type-following when we use it later (e.g. in 2417 * pretty print). 2418 */ 2419 if (!btf_type_id_size(btf, &next_type_id, NULL)) { 2420 if (env_type_is_resolved(env, next_type_id)) 2421 next_type = btf_type_id_resolve(btf, &next_type_id); 2422 2423 /* "typedef void new_void", "const void"...etc */ 2424 if (!btf_type_is_void(next_type) && 2425 !btf_type_is_fwd(next_type) && 2426 !btf_type_is_func_proto(next_type)) { 2427 btf_verifier_log_type(env, v->t, "Invalid type_id"); 2428 return -EINVAL; 2429 } 2430 } 2431 2432 env_stack_pop_resolved(env, next_type_id, 0); 2433 2434 return 0; 2435 } 2436 2437 static int btf_var_resolve(struct btf_verifier_env *env, 2438 const struct resolve_vertex *v) 2439 { 2440 const struct btf_type *next_type; 2441 const struct btf_type *t = v->t; 2442 u32 next_type_id = t->type; 2443 struct btf *btf = env->btf; 2444 2445 next_type = btf_type_by_id(btf, next_type_id); 2446 if (!next_type || btf_type_is_resolve_source_only(next_type)) { 2447 btf_verifier_log_type(env, v->t, "Invalid type_id"); 2448 return -EINVAL; 2449 } 2450 2451 if (!env_type_is_resolve_sink(env, next_type) && 2452 !env_type_is_resolved(env, next_type_id)) 2453 return env_stack_push(env, next_type, next_type_id); 2454 2455 if (btf_type_is_modifier(next_type)) { 2456 const struct btf_type *resolved_type; 2457 u32 resolved_type_id; 2458 2459 resolved_type_id = next_type_id; 2460 resolved_type = btf_type_id_resolve(btf, &resolved_type_id); 2461 2462 if (btf_type_is_ptr(resolved_type) && 2463 !env_type_is_resolve_sink(env, resolved_type) && 2464 !env_type_is_resolved(env, resolved_type_id)) 2465 return env_stack_push(env, resolved_type, 2466 resolved_type_id); 2467 } 2468 2469 /* We must resolve to something concrete at this point, no 2470 * forward types or similar that would resolve to size of 2471 * zero is allowed. 2472 */ 2473 if (!btf_type_id_size(btf, &next_type_id, NULL)) { 2474 btf_verifier_log_type(env, v->t, "Invalid type_id"); 2475 return -EINVAL; 2476 } 2477 2478 env_stack_pop_resolved(env, next_type_id, 0); 2479 2480 return 0; 2481 } 2482 2483 static int btf_ptr_resolve(struct btf_verifier_env *env, 2484 const struct resolve_vertex *v) 2485 { 2486 const struct btf_type *next_type; 2487 const struct btf_type *t = v->t; 2488 u32 next_type_id = t->type; 2489 struct btf *btf = env->btf; 2490 2491 next_type = btf_type_by_id(btf, next_type_id); 2492 if (!next_type || btf_type_is_resolve_source_only(next_type)) { 2493 btf_verifier_log_type(env, v->t, "Invalid type_id"); 2494 return -EINVAL; 2495 } 2496 2497 if (!env_type_is_resolve_sink(env, next_type) && 2498 !env_type_is_resolved(env, next_type_id)) 2499 return env_stack_push(env, next_type, next_type_id); 2500 2501 /* If the modifier was RESOLVED during RESOLVE_STRUCT_OR_ARRAY, 2502 * the modifier may have stopped resolving when it was resolved 2503 * to a ptr (last-resolved-ptr). 2504 * 2505 * We now need to continue from the last-resolved-ptr to 2506 * ensure the last-resolved-ptr will not referring back to 2507 * the currenct ptr (t). 2508 */ 2509 if (btf_type_is_modifier(next_type)) { 2510 const struct btf_type *resolved_type; 2511 u32 resolved_type_id; 2512 2513 resolved_type_id = next_type_id; 2514 resolved_type = btf_type_id_resolve(btf, &resolved_type_id); 2515 2516 if (btf_type_is_ptr(resolved_type) && 2517 !env_type_is_resolve_sink(env, resolved_type) && 2518 !env_type_is_resolved(env, resolved_type_id)) 2519 return env_stack_push(env, resolved_type, 2520 resolved_type_id); 2521 } 2522 2523 if (!btf_type_id_size(btf, &next_type_id, NULL)) { 2524 if (env_type_is_resolved(env, next_type_id)) 2525 next_type = btf_type_id_resolve(btf, &next_type_id); 2526 2527 if (!btf_type_is_void(next_type) && 2528 !btf_type_is_fwd(next_type) && 2529 !btf_type_is_func_proto(next_type)) { 2530 btf_verifier_log_type(env, v->t, "Invalid type_id"); 2531 return -EINVAL; 2532 } 2533 } 2534 2535 env_stack_pop_resolved(env, next_type_id, 0); 2536 2537 return 0; 2538 } 2539 2540 static void btf_modifier_show(const struct btf *btf, 2541 const struct btf_type *t, 2542 u32 type_id, void *data, 2543 u8 bits_offset, struct btf_show *show) 2544 { 2545 if (btf->resolved_ids) 2546 t = btf_type_id_resolve(btf, &type_id); 2547 else 2548 t = btf_type_skip_modifiers(btf, type_id, NULL); 2549 2550 btf_type_ops(t)->show(btf, t, type_id, data, bits_offset, show); 2551 } 2552 2553 static void btf_var_show(const struct btf *btf, const struct btf_type *t, 2554 u32 type_id, void *data, u8 bits_offset, 2555 struct btf_show *show) 2556 { 2557 t = btf_type_id_resolve(btf, &type_id); 2558 2559 btf_type_ops(t)->show(btf, t, type_id, data, bits_offset, show); 2560 } 2561 2562 static void btf_ptr_show(const struct btf *btf, const struct btf_type *t, 2563 u32 type_id, void *data, u8 bits_offset, 2564 struct btf_show *show) 2565 { 2566 void *safe_data; 2567 2568 safe_data = btf_show_start_type(show, t, type_id, data); 2569 if (!safe_data) 2570 return; 2571 2572 /* It is a hashed value unless BTF_SHOW_PTR_RAW is specified */ 2573 if (show->flags & BTF_SHOW_PTR_RAW) 2574 btf_show_type_value(show, "0x%px", *(void **)safe_data); 2575 else 2576 btf_show_type_value(show, "0x%p", *(void **)safe_data); 2577 btf_show_end_type(show); 2578 } 2579 2580 static void btf_ref_type_log(struct btf_verifier_env *env, 2581 const struct btf_type *t) 2582 { 2583 btf_verifier_log(env, "type_id=%u", t->type); 2584 } 2585 2586 static struct btf_kind_operations modifier_ops = { 2587 .check_meta = btf_ref_type_check_meta, 2588 .resolve = btf_modifier_resolve, 2589 .check_member = btf_modifier_check_member, 2590 .check_kflag_member = btf_modifier_check_kflag_member, 2591 .log_details = btf_ref_type_log, 2592 .show = btf_modifier_show, 2593 }; 2594 2595 static struct btf_kind_operations ptr_ops = { 2596 .check_meta = btf_ref_type_check_meta, 2597 .resolve = btf_ptr_resolve, 2598 .check_member = btf_ptr_check_member, 2599 .check_kflag_member = btf_generic_check_kflag_member, 2600 .log_details = btf_ref_type_log, 2601 .show = btf_ptr_show, 2602 }; 2603 2604 static s32 btf_fwd_check_meta(struct btf_verifier_env *env, 2605 const struct btf_type *t, 2606 u32 meta_left) 2607 { 2608 if (btf_type_vlen(t)) { 2609 btf_verifier_log_type(env, t, "vlen != 0"); 2610 return -EINVAL; 2611 } 2612 2613 if (t->type) { 2614 btf_verifier_log_type(env, t, "type != 0"); 2615 return -EINVAL; 2616 } 2617 2618 /* fwd type must have a valid name */ 2619 if (!t->name_off || 2620 !btf_name_valid_identifier(env->btf, t->name_off)) { 2621 btf_verifier_log_type(env, t, "Invalid name"); 2622 return -EINVAL; 2623 } 2624 2625 btf_verifier_log_type(env, t, NULL); 2626 2627 return 0; 2628 } 2629 2630 static void btf_fwd_type_log(struct btf_verifier_env *env, 2631 const struct btf_type *t) 2632 { 2633 btf_verifier_log(env, "%s", btf_type_kflag(t) ? "union" : "struct"); 2634 } 2635 2636 static struct btf_kind_operations fwd_ops = { 2637 .check_meta = btf_fwd_check_meta, 2638 .resolve = btf_df_resolve, 2639 .check_member = btf_df_check_member, 2640 .check_kflag_member = btf_df_check_kflag_member, 2641 .log_details = btf_fwd_type_log, 2642 .show = btf_df_show, 2643 }; 2644 2645 static int btf_array_check_member(struct btf_verifier_env *env, 2646 const struct btf_type *struct_type, 2647 const struct btf_member *member, 2648 const struct btf_type *member_type) 2649 { 2650 u32 struct_bits_off = member->offset; 2651 u32 struct_size, bytes_offset; 2652 u32 array_type_id, array_size; 2653 struct btf *btf = env->btf; 2654 2655 if (BITS_PER_BYTE_MASKED(struct_bits_off)) { 2656 btf_verifier_log_member(env, struct_type, member, 2657 "Member is not byte aligned"); 2658 return -EINVAL; 2659 } 2660 2661 array_type_id = member->type; 2662 btf_type_id_size(btf, &array_type_id, &array_size); 2663 struct_size = struct_type->size; 2664 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off); 2665 if (struct_size - bytes_offset < array_size) { 2666 btf_verifier_log_member(env, struct_type, member, 2667 "Member exceeds struct_size"); 2668 return -EINVAL; 2669 } 2670 2671 return 0; 2672 } 2673 2674 static s32 btf_array_check_meta(struct btf_verifier_env *env, 2675 const struct btf_type *t, 2676 u32 meta_left) 2677 { 2678 const struct btf_array *array = btf_type_array(t); 2679 u32 meta_needed = sizeof(*array); 2680 2681 if (meta_left < meta_needed) { 2682 btf_verifier_log_basic(env, t, 2683 "meta_left:%u meta_needed:%u", 2684 meta_left, meta_needed); 2685 return -EINVAL; 2686 } 2687 2688 /* array type should not have a name */ 2689 if (t->name_off) { 2690 btf_verifier_log_type(env, t, "Invalid name"); 2691 return -EINVAL; 2692 } 2693 2694 if (btf_type_vlen(t)) { 2695 btf_verifier_log_type(env, t, "vlen != 0"); 2696 return -EINVAL; 2697 } 2698 2699 if (btf_type_kflag(t)) { 2700 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); 2701 return -EINVAL; 2702 } 2703 2704 if (t->size) { 2705 btf_verifier_log_type(env, t, "size != 0"); 2706 return -EINVAL; 2707 } 2708 2709 /* Array elem type and index type cannot be in type void, 2710 * so !array->type and !array->index_type are not allowed. 2711 */ 2712 if (!array->type || !BTF_TYPE_ID_VALID(array->type)) { 2713 btf_verifier_log_type(env, t, "Invalid elem"); 2714 return -EINVAL; 2715 } 2716 2717 if (!array->index_type || !BTF_TYPE_ID_VALID(array->index_type)) { 2718 btf_verifier_log_type(env, t, "Invalid index"); 2719 return -EINVAL; 2720 } 2721 2722 btf_verifier_log_type(env, t, NULL); 2723 2724 return meta_needed; 2725 } 2726 2727 static int btf_array_resolve(struct btf_verifier_env *env, 2728 const struct resolve_vertex *v) 2729 { 2730 const struct btf_array *array = btf_type_array(v->t); 2731 const struct btf_type *elem_type, *index_type; 2732 u32 elem_type_id, index_type_id; 2733 struct btf *btf = env->btf; 2734 u32 elem_size; 2735 2736 /* Check array->index_type */ 2737 index_type_id = array->index_type; 2738 index_type = btf_type_by_id(btf, index_type_id); 2739 if (btf_type_nosize_or_null(index_type) || 2740 btf_type_is_resolve_source_only(index_type)) { 2741 btf_verifier_log_type(env, v->t, "Invalid index"); 2742 return -EINVAL; 2743 } 2744 2745 if (!env_type_is_resolve_sink(env, index_type) && 2746 !env_type_is_resolved(env, index_type_id)) 2747 return env_stack_push(env, index_type, index_type_id); 2748 2749 index_type = btf_type_id_size(btf, &index_type_id, NULL); 2750 if (!index_type || !btf_type_is_int(index_type) || 2751 !btf_type_int_is_regular(index_type)) { 2752 btf_verifier_log_type(env, v->t, "Invalid index"); 2753 return -EINVAL; 2754 } 2755 2756 /* Check array->type */ 2757 elem_type_id = array->type; 2758 elem_type = btf_type_by_id(btf, elem_type_id); 2759 if (btf_type_nosize_or_null(elem_type) || 2760 btf_type_is_resolve_source_only(elem_type)) { 2761 btf_verifier_log_type(env, v->t, 2762 "Invalid elem"); 2763 return -EINVAL; 2764 } 2765 2766 if (!env_type_is_resolve_sink(env, elem_type) && 2767 !env_type_is_resolved(env, elem_type_id)) 2768 return env_stack_push(env, elem_type, elem_type_id); 2769 2770 elem_type = btf_type_id_size(btf, &elem_type_id, &elem_size); 2771 if (!elem_type) { 2772 btf_verifier_log_type(env, v->t, "Invalid elem"); 2773 return -EINVAL; 2774 } 2775 2776 if (btf_type_is_int(elem_type) && !btf_type_int_is_regular(elem_type)) { 2777 btf_verifier_log_type(env, v->t, "Invalid array of int"); 2778 return -EINVAL; 2779 } 2780 2781 if (array->nelems && elem_size > U32_MAX / array->nelems) { 2782 btf_verifier_log_type(env, v->t, 2783 "Array size overflows U32_MAX"); 2784 return -EINVAL; 2785 } 2786 2787 env_stack_pop_resolved(env, elem_type_id, elem_size * array->nelems); 2788 2789 return 0; 2790 } 2791 2792 static void btf_array_log(struct btf_verifier_env *env, 2793 const struct btf_type *t) 2794 { 2795 const struct btf_array *array = btf_type_array(t); 2796 2797 btf_verifier_log(env, "type_id=%u index_type_id=%u nr_elems=%u", 2798 array->type, array->index_type, array->nelems); 2799 } 2800 2801 static void __btf_array_show(const struct btf *btf, const struct btf_type *t, 2802 u32 type_id, void *data, u8 bits_offset, 2803 struct btf_show *show) 2804 { 2805 const struct btf_array *array = btf_type_array(t); 2806 const struct btf_kind_operations *elem_ops; 2807 const struct btf_type *elem_type; 2808 u32 i, elem_size = 0, elem_type_id; 2809 u16 encoding = 0; 2810 2811 elem_type_id = array->type; 2812 elem_type = btf_type_skip_modifiers(btf, elem_type_id, NULL); 2813 if (elem_type && btf_type_has_size(elem_type)) 2814 elem_size = elem_type->size; 2815 2816 if (elem_type && btf_type_is_int(elem_type)) { 2817 u32 int_type = btf_type_int(elem_type); 2818 2819 encoding = BTF_INT_ENCODING(int_type); 2820 2821 /* 2822 * BTF_INT_CHAR encoding never seems to be set for 2823 * char arrays, so if size is 1 and element is 2824 * printable as a char, we'll do that. 2825 */ 2826 if (elem_size == 1) 2827 encoding = BTF_INT_CHAR; 2828 } 2829 2830 if (!btf_show_start_array_type(show, t, type_id, encoding, data)) 2831 return; 2832 2833 if (!elem_type) 2834 goto out; 2835 elem_ops = btf_type_ops(elem_type); 2836 2837 for (i = 0; i < array->nelems; i++) { 2838 2839 btf_show_start_array_member(show); 2840 2841 elem_ops->show(btf, elem_type, elem_type_id, data, 2842 bits_offset, show); 2843 data += elem_size; 2844 2845 btf_show_end_array_member(show); 2846 2847 if (show->state.array_terminated) 2848 break; 2849 } 2850 out: 2851 btf_show_end_array_type(show); 2852 } 2853 2854 static void btf_array_show(const struct btf *btf, const struct btf_type *t, 2855 u32 type_id, void *data, u8 bits_offset, 2856 struct btf_show *show) 2857 { 2858 const struct btf_member *m = show->state.member; 2859 2860 /* 2861 * First check if any members would be shown (are non-zero). 2862 * See comments above "struct btf_show" definition for more 2863 * details on how this works at a high-level. 2864 */ 2865 if (show->state.depth > 0 && !(show->flags & BTF_SHOW_ZERO)) { 2866 if (!show->state.depth_check) { 2867 show->state.depth_check = show->state.depth + 1; 2868 show->state.depth_to_show = 0; 2869 } 2870 __btf_array_show(btf, t, type_id, data, bits_offset, show); 2871 show->state.member = m; 2872 2873 if (show->state.depth_check != show->state.depth + 1) 2874 return; 2875 show->state.depth_check = 0; 2876 2877 if (show->state.depth_to_show <= show->state.depth) 2878 return; 2879 /* 2880 * Reaching here indicates we have recursed and found 2881 * non-zero array member(s). 2882 */ 2883 } 2884 __btf_array_show(btf, t, type_id, data, bits_offset, show); 2885 } 2886 2887 static struct btf_kind_operations array_ops = { 2888 .check_meta = btf_array_check_meta, 2889 .resolve = btf_array_resolve, 2890 .check_member = btf_array_check_member, 2891 .check_kflag_member = btf_generic_check_kflag_member, 2892 .log_details = btf_array_log, 2893 .show = btf_array_show, 2894 }; 2895 2896 static int btf_struct_check_member(struct btf_verifier_env *env, 2897 const struct btf_type *struct_type, 2898 const struct btf_member *member, 2899 const struct btf_type *member_type) 2900 { 2901 u32 struct_bits_off = member->offset; 2902 u32 struct_size, bytes_offset; 2903 2904 if (BITS_PER_BYTE_MASKED(struct_bits_off)) { 2905 btf_verifier_log_member(env, struct_type, member, 2906 "Member is not byte aligned"); 2907 return -EINVAL; 2908 } 2909 2910 struct_size = struct_type->size; 2911 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off); 2912 if (struct_size - bytes_offset < member_type->size) { 2913 btf_verifier_log_member(env, struct_type, member, 2914 "Member exceeds struct_size"); 2915 return -EINVAL; 2916 } 2917 2918 return 0; 2919 } 2920 2921 static s32 btf_struct_check_meta(struct btf_verifier_env *env, 2922 const struct btf_type *t, 2923 u32 meta_left) 2924 { 2925 bool is_union = BTF_INFO_KIND(t->info) == BTF_KIND_UNION; 2926 const struct btf_member *member; 2927 u32 meta_needed, last_offset; 2928 struct btf *btf = env->btf; 2929 u32 struct_size = t->size; 2930 u32 offset; 2931 u16 i; 2932 2933 meta_needed = btf_type_vlen(t) * sizeof(*member); 2934 if (meta_left < meta_needed) { 2935 btf_verifier_log_basic(env, t, 2936 "meta_left:%u meta_needed:%u", 2937 meta_left, meta_needed); 2938 return -EINVAL; 2939 } 2940 2941 /* struct type either no name or a valid one */ 2942 if (t->name_off && 2943 !btf_name_valid_identifier(env->btf, t->name_off)) { 2944 btf_verifier_log_type(env, t, "Invalid name"); 2945 return -EINVAL; 2946 } 2947 2948 btf_verifier_log_type(env, t, NULL); 2949 2950 last_offset = 0; 2951 for_each_member(i, t, member) { 2952 if (!btf_name_offset_valid(btf, member->name_off)) { 2953 btf_verifier_log_member(env, t, member, 2954 "Invalid member name_offset:%u", 2955 member->name_off); 2956 return -EINVAL; 2957 } 2958 2959 /* struct member either no name or a valid one */ 2960 if (member->name_off && 2961 !btf_name_valid_identifier(btf, member->name_off)) { 2962 btf_verifier_log_member(env, t, member, "Invalid name"); 2963 return -EINVAL; 2964 } 2965 /* A member cannot be in type void */ 2966 if (!member->type || !BTF_TYPE_ID_VALID(member->type)) { 2967 btf_verifier_log_member(env, t, member, 2968 "Invalid type_id"); 2969 return -EINVAL; 2970 } 2971 2972 offset = btf_member_bit_offset(t, member); 2973 if (is_union && offset) { 2974 btf_verifier_log_member(env, t, member, 2975 "Invalid member bits_offset"); 2976 return -EINVAL; 2977 } 2978 2979 /* 2980 * ">" instead of ">=" because the last member could be 2981 * "char a[0];" 2982 */ 2983 if (last_offset > offset) { 2984 btf_verifier_log_member(env, t, member, 2985 "Invalid member bits_offset"); 2986 return -EINVAL; 2987 } 2988 2989 if (BITS_ROUNDUP_BYTES(offset) > struct_size) { 2990 btf_verifier_log_member(env, t, member, 2991 "Member bits_offset exceeds its struct size"); 2992 return -EINVAL; 2993 } 2994 2995 btf_verifier_log_member(env, t, member, NULL); 2996 last_offset = offset; 2997 } 2998 2999 return meta_needed; 3000 } 3001 3002 static int btf_struct_resolve(struct btf_verifier_env *env, 3003 const struct resolve_vertex *v) 3004 { 3005 const struct btf_member *member; 3006 int err; 3007 u16 i; 3008 3009 /* Before continue resolving the next_member, 3010 * ensure the last member is indeed resolved to a 3011 * type with size info. 3012 */ 3013 if (v->next_member) { 3014 const struct btf_type *last_member_type; 3015 const struct btf_member *last_member; 3016 u16 last_member_type_id; 3017 3018 last_member = btf_type_member(v->t) + v->next_member - 1; 3019 last_member_type_id = last_member->type; 3020 if (WARN_ON_ONCE(!env_type_is_resolved(env, 3021 last_member_type_id))) 3022 return -EINVAL; 3023 3024 last_member_type = btf_type_by_id(env->btf, 3025 last_member_type_id); 3026 if (btf_type_kflag(v->t)) 3027 err = btf_type_ops(last_member_type)->check_kflag_member(env, v->t, 3028 last_member, 3029 last_member_type); 3030 else 3031 err = btf_type_ops(last_member_type)->check_member(env, v->t, 3032 last_member, 3033 last_member_type); 3034 if (err) 3035 return err; 3036 } 3037 3038 for_each_member_from(i, v->next_member, v->t, member) { 3039 u32 member_type_id = member->type; 3040 const struct btf_type *member_type = btf_type_by_id(env->btf, 3041 member_type_id); 3042 3043 if (btf_type_nosize_or_null(member_type) || 3044 btf_type_is_resolve_source_only(member_type)) { 3045 btf_verifier_log_member(env, v->t, member, 3046 "Invalid member"); 3047 return -EINVAL; 3048 } 3049 3050 if (!env_type_is_resolve_sink(env, member_type) && 3051 !env_type_is_resolved(env, member_type_id)) { 3052 env_stack_set_next_member(env, i + 1); 3053 return env_stack_push(env, member_type, member_type_id); 3054 } 3055 3056 if (btf_type_kflag(v->t)) 3057 err = btf_type_ops(member_type)->check_kflag_member(env, v->t, 3058 member, 3059 member_type); 3060 else 3061 err = btf_type_ops(member_type)->check_member(env, v->t, 3062 member, 3063 member_type); 3064 if (err) 3065 return err; 3066 } 3067 3068 env_stack_pop_resolved(env, 0, 0); 3069 3070 return 0; 3071 } 3072 3073 static void btf_struct_log(struct btf_verifier_env *env, 3074 const struct btf_type *t) 3075 { 3076 btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t)); 3077 } 3078 3079 static int btf_find_struct_field(const struct btf *btf, const struct btf_type *t, 3080 const char *name, int sz, int align) 3081 { 3082 const struct btf_member *member; 3083 u32 i, off = -ENOENT; 3084 3085 for_each_member(i, t, member) { 3086 const struct btf_type *member_type = btf_type_by_id(btf, 3087 member->type); 3088 if (!__btf_type_is_struct(member_type)) 3089 continue; 3090 if (member_type->size != sz) 3091 continue; 3092 if (strcmp(__btf_name_by_offset(btf, member_type->name_off), name)) 3093 continue; 3094 if (off != -ENOENT) 3095 /* only one such field is allowed */ 3096 return -E2BIG; 3097 off = btf_member_bit_offset(t, member); 3098 if (off % 8) 3099 /* valid C code cannot generate such BTF */ 3100 return -EINVAL; 3101 off /= 8; 3102 if (off % align) 3103 return -EINVAL; 3104 } 3105 return off; 3106 } 3107 3108 static int btf_find_datasec_var(const struct btf *btf, const struct btf_type *t, 3109 const char *name, int sz, int align) 3110 { 3111 const struct btf_var_secinfo *vsi; 3112 u32 i, off = -ENOENT; 3113 3114 for_each_vsi(i, t, vsi) { 3115 const struct btf_type *var = btf_type_by_id(btf, vsi->type); 3116 const struct btf_type *var_type = btf_type_by_id(btf, var->type); 3117 3118 if (!__btf_type_is_struct(var_type)) 3119 continue; 3120 if (var_type->size != sz) 3121 continue; 3122 if (vsi->size != sz) 3123 continue; 3124 if (strcmp(__btf_name_by_offset(btf, var_type->name_off), name)) 3125 continue; 3126 if (off != -ENOENT) 3127 /* only one such field is allowed */ 3128 return -E2BIG; 3129 off = vsi->offset; 3130 if (off % align) 3131 return -EINVAL; 3132 } 3133 return off; 3134 } 3135 3136 static int btf_find_field(const struct btf *btf, const struct btf_type *t, 3137 const char *name, int sz, int align) 3138 { 3139 3140 if (__btf_type_is_struct(t)) 3141 return btf_find_struct_field(btf, t, name, sz, align); 3142 else if (btf_type_is_datasec(t)) 3143 return btf_find_datasec_var(btf, t, name, sz, align); 3144 return -EINVAL; 3145 } 3146 3147 /* find 'struct bpf_spin_lock' in map value. 3148 * return >= 0 offset if found 3149 * and < 0 in case of error 3150 */ 3151 int btf_find_spin_lock(const struct btf *btf, const struct btf_type *t) 3152 { 3153 return btf_find_field(btf, t, "bpf_spin_lock", 3154 sizeof(struct bpf_spin_lock), 3155 __alignof__(struct bpf_spin_lock)); 3156 } 3157 3158 int btf_find_timer(const struct btf *btf, const struct btf_type *t) 3159 { 3160 return btf_find_field(btf, t, "bpf_timer", 3161 sizeof(struct bpf_timer), 3162 __alignof__(struct bpf_timer)); 3163 } 3164 3165 static void __btf_struct_show(const struct btf *btf, const struct btf_type *t, 3166 u32 type_id, void *data, u8 bits_offset, 3167 struct btf_show *show) 3168 { 3169 const struct btf_member *member; 3170 void *safe_data; 3171 u32 i; 3172 3173 safe_data = btf_show_start_struct_type(show, t, type_id, data); 3174 if (!safe_data) 3175 return; 3176 3177 for_each_member(i, t, member) { 3178 const struct btf_type *member_type = btf_type_by_id(btf, 3179 member->type); 3180 const struct btf_kind_operations *ops; 3181 u32 member_offset, bitfield_size; 3182 u32 bytes_offset; 3183 u8 bits8_offset; 3184 3185 btf_show_start_member(show, member); 3186 3187 member_offset = btf_member_bit_offset(t, member); 3188 bitfield_size = btf_member_bitfield_size(t, member); 3189 bytes_offset = BITS_ROUNDDOWN_BYTES(member_offset); 3190 bits8_offset = BITS_PER_BYTE_MASKED(member_offset); 3191 if (bitfield_size) { 3192 safe_data = btf_show_start_type(show, member_type, 3193 member->type, 3194 data + bytes_offset); 3195 if (safe_data) 3196 btf_bitfield_show(safe_data, 3197 bits8_offset, 3198 bitfield_size, show); 3199 btf_show_end_type(show); 3200 } else { 3201 ops = btf_type_ops(member_type); 3202 ops->show(btf, member_type, member->type, 3203 data + bytes_offset, bits8_offset, show); 3204 } 3205 3206 btf_show_end_member(show); 3207 } 3208 3209 btf_show_end_struct_type(show); 3210 } 3211 3212 static void btf_struct_show(const struct btf *btf, const struct btf_type *t, 3213 u32 type_id, void *data, u8 bits_offset, 3214 struct btf_show *show) 3215 { 3216 const struct btf_member *m = show->state.member; 3217 3218 /* 3219 * First check if any members would be shown (are non-zero). 3220 * See comments above "struct btf_show" definition for more 3221 * details on how this works at a high-level. 3222 */ 3223 if (show->state.depth > 0 && !(show->flags & BTF_SHOW_ZERO)) { 3224 if (!show->state.depth_check) { 3225 show->state.depth_check = show->state.depth + 1; 3226 show->state.depth_to_show = 0; 3227 } 3228 __btf_struct_show(btf, t, type_id, data, bits_offset, show); 3229 /* Restore saved member data here */ 3230 show->state.member = m; 3231 if (show->state.depth_check != show->state.depth + 1) 3232 return; 3233 show->state.depth_check = 0; 3234 3235 if (show->state.depth_to_show <= show->state.depth) 3236 return; 3237 /* 3238 * Reaching here indicates we have recursed and found 3239 * non-zero child values. 3240 */ 3241 } 3242 3243 __btf_struct_show(btf, t, type_id, data, bits_offset, show); 3244 } 3245 3246 static struct btf_kind_operations struct_ops = { 3247 .check_meta = btf_struct_check_meta, 3248 .resolve = btf_struct_resolve, 3249 .check_member = btf_struct_check_member, 3250 .check_kflag_member = btf_generic_check_kflag_member, 3251 .log_details = btf_struct_log, 3252 .show = btf_struct_show, 3253 }; 3254 3255 static int btf_enum_check_member(struct btf_verifier_env *env, 3256 const struct btf_type *struct_type, 3257 const struct btf_member *member, 3258 const struct btf_type *member_type) 3259 { 3260 u32 struct_bits_off = member->offset; 3261 u32 struct_size, bytes_offset; 3262 3263 if (BITS_PER_BYTE_MASKED(struct_bits_off)) { 3264 btf_verifier_log_member(env, struct_type, member, 3265 "Member is not byte aligned"); 3266 return -EINVAL; 3267 } 3268 3269 struct_size = struct_type->size; 3270 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off); 3271 if (struct_size - bytes_offset < member_type->size) { 3272 btf_verifier_log_member(env, struct_type, member, 3273 "Member exceeds struct_size"); 3274 return -EINVAL; 3275 } 3276 3277 return 0; 3278 } 3279 3280 static int btf_enum_check_kflag_member(struct btf_verifier_env *env, 3281 const struct btf_type *struct_type, 3282 const struct btf_member *member, 3283 const struct btf_type *member_type) 3284 { 3285 u32 struct_bits_off, nr_bits, bytes_end, struct_size; 3286 u32 int_bitsize = sizeof(int) * BITS_PER_BYTE; 3287 3288 struct_bits_off = BTF_MEMBER_BIT_OFFSET(member->offset); 3289 nr_bits = BTF_MEMBER_BITFIELD_SIZE(member->offset); 3290 if (!nr_bits) { 3291 if (BITS_PER_BYTE_MASKED(struct_bits_off)) { 3292 btf_verifier_log_member(env, struct_type, member, 3293 "Member is not byte aligned"); 3294 return -EINVAL; 3295 } 3296 3297 nr_bits = int_bitsize; 3298 } else if (nr_bits > int_bitsize) { 3299 btf_verifier_log_member(env, struct_type, member, 3300 "Invalid member bitfield_size"); 3301 return -EINVAL; 3302 } 3303 3304 struct_size = struct_type->size; 3305 bytes_end = BITS_ROUNDUP_BYTES(struct_bits_off + nr_bits); 3306 if (struct_size < bytes_end) { 3307 btf_verifier_log_member(env, struct_type, member, 3308 "Member exceeds struct_size"); 3309 return -EINVAL; 3310 } 3311 3312 return 0; 3313 } 3314 3315 static s32 btf_enum_check_meta(struct btf_verifier_env *env, 3316 const struct btf_type *t, 3317 u32 meta_left) 3318 { 3319 const struct btf_enum *enums = btf_type_enum(t); 3320 struct btf *btf = env->btf; 3321 u16 i, nr_enums; 3322 u32 meta_needed; 3323 3324 nr_enums = btf_type_vlen(t); 3325 meta_needed = nr_enums * sizeof(*enums); 3326 3327 if (meta_left < meta_needed) { 3328 btf_verifier_log_basic(env, t, 3329 "meta_left:%u meta_needed:%u", 3330 meta_left, meta_needed); 3331 return -EINVAL; 3332 } 3333 3334 if (btf_type_kflag(t)) { 3335 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); 3336 return -EINVAL; 3337 } 3338 3339 if (t->size > 8 || !is_power_of_2(t->size)) { 3340 btf_verifier_log_type(env, t, "Unexpected size"); 3341 return -EINVAL; 3342 } 3343 3344 /* enum type either no name or a valid one */ 3345 if (t->name_off && 3346 !btf_name_valid_identifier(env->btf, t->name_off)) { 3347 btf_verifier_log_type(env, t, "Invalid name"); 3348 return -EINVAL; 3349 } 3350 3351 btf_verifier_log_type(env, t, NULL); 3352 3353 for (i = 0; i < nr_enums; i++) { 3354 if (!btf_name_offset_valid(btf, enums[i].name_off)) { 3355 btf_verifier_log(env, "\tInvalid name_offset:%u", 3356 enums[i].name_off); 3357 return -EINVAL; 3358 } 3359 3360 /* enum member must have a valid name */ 3361 if (!enums[i].name_off || 3362 !btf_name_valid_identifier(btf, enums[i].name_off)) { 3363 btf_verifier_log_type(env, t, "Invalid name"); 3364 return -EINVAL; 3365 } 3366 3367 if (env->log.level == BPF_LOG_KERNEL) 3368 continue; 3369 btf_verifier_log(env, "\t%s val=%d\n", 3370 __btf_name_by_offset(btf, enums[i].name_off), 3371 enums[i].val); 3372 } 3373 3374 return meta_needed; 3375 } 3376 3377 static void btf_enum_log(struct btf_verifier_env *env, 3378 const struct btf_type *t) 3379 { 3380 btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t)); 3381 } 3382 3383 static void btf_enum_show(const struct btf *btf, const struct btf_type *t, 3384 u32 type_id, void *data, u8 bits_offset, 3385 struct btf_show *show) 3386 { 3387 const struct btf_enum *enums = btf_type_enum(t); 3388 u32 i, nr_enums = btf_type_vlen(t); 3389 void *safe_data; 3390 int v; 3391 3392 safe_data = btf_show_start_type(show, t, type_id, data); 3393 if (!safe_data) 3394 return; 3395 3396 v = *(int *)safe_data; 3397 3398 for (i = 0; i < nr_enums; i++) { 3399 if (v != enums[i].val) 3400 continue; 3401 3402 btf_show_type_value(show, "%s", 3403 __btf_name_by_offset(btf, 3404 enums[i].name_off)); 3405 3406 btf_show_end_type(show); 3407 return; 3408 } 3409 3410 btf_show_type_value(show, "%d", v); 3411 btf_show_end_type(show); 3412 } 3413 3414 static struct btf_kind_operations enum_ops = { 3415 .check_meta = btf_enum_check_meta, 3416 .resolve = btf_df_resolve, 3417 .check_member = btf_enum_check_member, 3418 .check_kflag_member = btf_enum_check_kflag_member, 3419 .log_details = btf_enum_log, 3420 .show = btf_enum_show, 3421 }; 3422 3423 static s32 btf_func_proto_check_meta(struct btf_verifier_env *env, 3424 const struct btf_type *t, 3425 u32 meta_left) 3426 { 3427 u32 meta_needed = btf_type_vlen(t) * sizeof(struct btf_param); 3428 3429 if (meta_left < meta_needed) { 3430 btf_verifier_log_basic(env, t, 3431 "meta_left:%u meta_needed:%u", 3432 meta_left, meta_needed); 3433 return -EINVAL; 3434 } 3435 3436 if (t->name_off) { 3437 btf_verifier_log_type(env, t, "Invalid name"); 3438 return -EINVAL; 3439 } 3440 3441 if (btf_type_kflag(t)) { 3442 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); 3443 return -EINVAL; 3444 } 3445 3446 btf_verifier_log_type(env, t, NULL); 3447 3448 return meta_needed; 3449 } 3450 3451 static void btf_func_proto_log(struct btf_verifier_env *env, 3452 const struct btf_type *t) 3453 { 3454 const struct btf_param *args = (const struct btf_param *)(t + 1); 3455 u16 nr_args = btf_type_vlen(t), i; 3456 3457 btf_verifier_log(env, "return=%u args=(", t->type); 3458 if (!nr_args) { 3459 btf_verifier_log(env, "void"); 3460 goto done; 3461 } 3462 3463 if (nr_args == 1 && !args[0].type) { 3464 /* Only one vararg */ 3465 btf_verifier_log(env, "vararg"); 3466 goto done; 3467 } 3468 3469 btf_verifier_log(env, "%u %s", args[0].type, 3470 __btf_name_by_offset(env->btf, 3471 args[0].name_off)); 3472 for (i = 1; i < nr_args - 1; i++) 3473 btf_verifier_log(env, ", %u %s", args[i].type, 3474 __btf_name_by_offset(env->btf, 3475 args[i].name_off)); 3476 3477 if (nr_args > 1) { 3478 const struct btf_param *last_arg = &args[nr_args - 1]; 3479 3480 if (last_arg->type) 3481 btf_verifier_log(env, ", %u %s", last_arg->type, 3482 __btf_name_by_offset(env->btf, 3483 last_arg->name_off)); 3484 else 3485 btf_verifier_log(env, ", vararg"); 3486 } 3487 3488 done: 3489 btf_verifier_log(env, ")"); 3490 } 3491 3492 static struct btf_kind_operations func_proto_ops = { 3493 .check_meta = btf_func_proto_check_meta, 3494 .resolve = btf_df_resolve, 3495 /* 3496 * BTF_KIND_FUNC_PROTO cannot be directly referred by 3497 * a struct's member. 3498 * 3499 * It should be a function pointer instead. 3500 * (i.e. struct's member -> BTF_KIND_PTR -> BTF_KIND_FUNC_PROTO) 3501 * 3502 * Hence, there is no btf_func_check_member(). 3503 */ 3504 .check_member = btf_df_check_member, 3505 .check_kflag_member = btf_df_check_kflag_member, 3506 .log_details = btf_func_proto_log, 3507 .show = btf_df_show, 3508 }; 3509 3510 static s32 btf_func_check_meta(struct btf_verifier_env *env, 3511 const struct btf_type *t, 3512 u32 meta_left) 3513 { 3514 if (!t->name_off || 3515 !btf_name_valid_identifier(env->btf, t->name_off)) { 3516 btf_verifier_log_type(env, t, "Invalid name"); 3517 return -EINVAL; 3518 } 3519 3520 if (btf_type_vlen(t) > BTF_FUNC_GLOBAL) { 3521 btf_verifier_log_type(env, t, "Invalid func linkage"); 3522 return -EINVAL; 3523 } 3524 3525 if (btf_type_kflag(t)) { 3526 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); 3527 return -EINVAL; 3528 } 3529 3530 btf_verifier_log_type(env, t, NULL); 3531 3532 return 0; 3533 } 3534 3535 static struct btf_kind_operations func_ops = { 3536 .check_meta = btf_func_check_meta, 3537 .resolve = btf_df_resolve, 3538 .check_member = btf_df_check_member, 3539 .check_kflag_member = btf_df_check_kflag_member, 3540 .log_details = btf_ref_type_log, 3541 .show = btf_df_show, 3542 }; 3543 3544 static s32 btf_var_check_meta(struct btf_verifier_env *env, 3545 const struct btf_type *t, 3546 u32 meta_left) 3547 { 3548 const struct btf_var *var; 3549 u32 meta_needed = sizeof(*var); 3550 3551 if (meta_left < meta_needed) { 3552 btf_verifier_log_basic(env, t, 3553 "meta_left:%u meta_needed:%u", 3554 meta_left, meta_needed); 3555 return -EINVAL; 3556 } 3557 3558 if (btf_type_vlen(t)) { 3559 btf_verifier_log_type(env, t, "vlen != 0"); 3560 return -EINVAL; 3561 } 3562 3563 if (btf_type_kflag(t)) { 3564 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); 3565 return -EINVAL; 3566 } 3567 3568 if (!t->name_off || 3569 !__btf_name_valid(env->btf, t->name_off, true)) { 3570 btf_verifier_log_type(env, t, "Invalid name"); 3571 return -EINVAL; 3572 } 3573 3574 /* A var cannot be in type void */ 3575 if (!t->type || !BTF_TYPE_ID_VALID(t->type)) { 3576 btf_verifier_log_type(env, t, "Invalid type_id"); 3577 return -EINVAL; 3578 } 3579 3580 var = btf_type_var(t); 3581 if (var->linkage != BTF_VAR_STATIC && 3582 var->linkage != BTF_VAR_GLOBAL_ALLOCATED) { 3583 btf_verifier_log_type(env, t, "Linkage not supported"); 3584 return -EINVAL; 3585 } 3586 3587 btf_verifier_log_type(env, t, NULL); 3588 3589 return meta_needed; 3590 } 3591 3592 static void btf_var_log(struct btf_verifier_env *env, const struct btf_type *t) 3593 { 3594 const struct btf_var *var = btf_type_var(t); 3595 3596 btf_verifier_log(env, "type_id=%u linkage=%u", t->type, var->linkage); 3597 } 3598 3599 static const struct btf_kind_operations var_ops = { 3600 .check_meta = btf_var_check_meta, 3601 .resolve = btf_var_resolve, 3602 .check_member = btf_df_check_member, 3603 .check_kflag_member = btf_df_check_kflag_member, 3604 .log_details = btf_var_log, 3605 .show = btf_var_show, 3606 }; 3607 3608 static s32 btf_datasec_check_meta(struct btf_verifier_env *env, 3609 const struct btf_type *t, 3610 u32 meta_left) 3611 { 3612 const struct btf_var_secinfo *vsi; 3613 u64 last_vsi_end_off = 0, sum = 0; 3614 u32 i, meta_needed; 3615 3616 meta_needed = btf_type_vlen(t) * sizeof(*vsi); 3617 if (meta_left < meta_needed) { 3618 btf_verifier_log_basic(env, t, 3619 "meta_left:%u meta_needed:%u", 3620 meta_left, meta_needed); 3621 return -EINVAL; 3622 } 3623 3624 if (!t->size) { 3625 btf_verifier_log_type(env, t, "size == 0"); 3626 return -EINVAL; 3627 } 3628 3629 if (btf_type_kflag(t)) { 3630 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); 3631 return -EINVAL; 3632 } 3633 3634 if (!t->name_off || 3635 !btf_name_valid_section(env->btf, t->name_off)) { 3636 btf_verifier_log_type(env, t, "Invalid name"); 3637 return -EINVAL; 3638 } 3639 3640 btf_verifier_log_type(env, t, NULL); 3641 3642 for_each_vsi(i, t, vsi) { 3643 /* A var cannot be in type void */ 3644 if (!vsi->type || !BTF_TYPE_ID_VALID(vsi->type)) { 3645 btf_verifier_log_vsi(env, t, vsi, 3646 "Invalid type_id"); 3647 return -EINVAL; 3648 } 3649 3650 if (vsi->offset < last_vsi_end_off || vsi->offset >= t->size) { 3651 btf_verifier_log_vsi(env, t, vsi, 3652 "Invalid offset"); 3653 return -EINVAL; 3654 } 3655 3656 if (!vsi->size || vsi->size > t->size) { 3657 btf_verifier_log_vsi(env, t, vsi, 3658 "Invalid size"); 3659 return -EINVAL; 3660 } 3661 3662 last_vsi_end_off = vsi->offset + vsi->size; 3663 if (last_vsi_end_off > t->size) { 3664 btf_verifier_log_vsi(env, t, vsi, 3665 "Invalid offset+size"); 3666 return -EINVAL; 3667 } 3668 3669 btf_verifier_log_vsi(env, t, vsi, NULL); 3670 sum += vsi->size; 3671 } 3672 3673 if (t->size < sum) { 3674 btf_verifier_log_type(env, t, "Invalid btf_info size"); 3675 return -EINVAL; 3676 } 3677 3678 return meta_needed; 3679 } 3680 3681 static int btf_datasec_resolve(struct btf_verifier_env *env, 3682 const struct resolve_vertex *v) 3683 { 3684 const struct btf_var_secinfo *vsi; 3685 struct btf *btf = env->btf; 3686 u16 i; 3687 3688 for_each_vsi_from(i, v->next_member, v->t, vsi) { 3689 u32 var_type_id = vsi->type, type_id, type_size = 0; 3690 const struct btf_type *var_type = btf_type_by_id(env->btf, 3691 var_type_id); 3692 if (!var_type || !btf_type_is_var(var_type)) { 3693 btf_verifier_log_vsi(env, v->t, vsi, 3694 "Not a VAR kind member"); 3695 return -EINVAL; 3696 } 3697 3698 if (!env_type_is_resolve_sink(env, var_type) && 3699 !env_type_is_resolved(env, var_type_id)) { 3700 env_stack_set_next_member(env, i + 1); 3701 return env_stack_push(env, var_type, var_type_id); 3702 } 3703 3704 type_id = var_type->type; 3705 if (!btf_type_id_size(btf, &type_id, &type_size)) { 3706 btf_verifier_log_vsi(env, v->t, vsi, "Invalid type"); 3707 return -EINVAL; 3708 } 3709 3710 if (vsi->size < type_size) { 3711 btf_verifier_log_vsi(env, v->t, vsi, "Invalid size"); 3712 return -EINVAL; 3713 } 3714 } 3715 3716 env_stack_pop_resolved(env, 0, 0); 3717 return 0; 3718 } 3719 3720 static void btf_datasec_log(struct btf_verifier_env *env, 3721 const struct btf_type *t) 3722 { 3723 btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t)); 3724 } 3725 3726 static void btf_datasec_show(const struct btf *btf, 3727 const struct btf_type *t, u32 type_id, 3728 void *data, u8 bits_offset, 3729 struct btf_show *show) 3730 { 3731 const struct btf_var_secinfo *vsi; 3732 const struct btf_type *var; 3733 u32 i; 3734 3735 if (!btf_show_start_type(show, t, type_id, data)) 3736 return; 3737 3738 btf_show_type_value(show, "section (\"%s\") = {", 3739 __btf_name_by_offset(btf, t->name_off)); 3740 for_each_vsi(i, t, vsi) { 3741 var = btf_type_by_id(btf, vsi->type); 3742 if (i) 3743 btf_show(show, ","); 3744 btf_type_ops(var)->show(btf, var, vsi->type, 3745 data + vsi->offset, bits_offset, show); 3746 } 3747 btf_show_end_type(show); 3748 } 3749 3750 static const struct btf_kind_operations datasec_ops = { 3751 .check_meta = btf_datasec_check_meta, 3752 .resolve = btf_datasec_resolve, 3753 .check_member = btf_df_check_member, 3754 .check_kflag_member = btf_df_check_kflag_member, 3755 .log_details = btf_datasec_log, 3756 .show = btf_datasec_show, 3757 }; 3758 3759 static s32 btf_float_check_meta(struct btf_verifier_env *env, 3760 const struct btf_type *t, 3761 u32 meta_left) 3762 { 3763 if (btf_type_vlen(t)) { 3764 btf_verifier_log_type(env, t, "vlen != 0"); 3765 return -EINVAL; 3766 } 3767 3768 if (btf_type_kflag(t)) { 3769 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); 3770 return -EINVAL; 3771 } 3772 3773 if (t->size != 2 && t->size != 4 && t->size != 8 && t->size != 12 && 3774 t->size != 16) { 3775 btf_verifier_log_type(env, t, "Invalid type_size"); 3776 return -EINVAL; 3777 } 3778 3779 btf_verifier_log_type(env, t, NULL); 3780 3781 return 0; 3782 } 3783 3784 static int btf_float_check_member(struct btf_verifier_env *env, 3785 const struct btf_type *struct_type, 3786 const struct btf_member *member, 3787 const struct btf_type *member_type) 3788 { 3789 u64 start_offset_bytes; 3790 u64 end_offset_bytes; 3791 u64 misalign_bits; 3792 u64 align_bytes; 3793 u64 align_bits; 3794 3795 /* Different architectures have different alignment requirements, so 3796 * here we check only for the reasonable minimum. This way we ensure 3797 * that types after CO-RE can pass the kernel BTF verifier. 3798 */ 3799 align_bytes = min_t(u64, sizeof(void *), member_type->size); 3800 align_bits = align_bytes * BITS_PER_BYTE; 3801 div64_u64_rem(member->offset, align_bits, &misalign_bits); 3802 if (misalign_bits) { 3803 btf_verifier_log_member(env, struct_type, member, 3804 "Member is not properly aligned"); 3805 return -EINVAL; 3806 } 3807 3808 start_offset_bytes = member->offset / BITS_PER_BYTE; 3809 end_offset_bytes = start_offset_bytes + member_type->size; 3810 if (end_offset_bytes > struct_type->size) { 3811 btf_verifier_log_member(env, struct_type, member, 3812 "Member exceeds struct_size"); 3813 return -EINVAL; 3814 } 3815 3816 return 0; 3817 } 3818 3819 static void btf_float_log(struct btf_verifier_env *env, 3820 const struct btf_type *t) 3821 { 3822 btf_verifier_log(env, "size=%u", t->size); 3823 } 3824 3825 static const struct btf_kind_operations float_ops = { 3826 .check_meta = btf_float_check_meta, 3827 .resolve = btf_df_resolve, 3828 .check_member = btf_float_check_member, 3829 .check_kflag_member = btf_generic_check_kflag_member, 3830 .log_details = btf_float_log, 3831 .show = btf_df_show, 3832 }; 3833 3834 static s32 btf_decl_tag_check_meta(struct btf_verifier_env *env, 3835 const struct btf_type *t, 3836 u32 meta_left) 3837 { 3838 const struct btf_decl_tag *tag; 3839 u32 meta_needed = sizeof(*tag); 3840 s32 component_idx; 3841 const char *value; 3842 3843 if (meta_left < meta_needed) { 3844 btf_verifier_log_basic(env, t, 3845 "meta_left:%u meta_needed:%u", 3846 meta_left, meta_needed); 3847 return -EINVAL; 3848 } 3849 3850 value = btf_name_by_offset(env->btf, t->name_off); 3851 if (!value || !value[0]) { 3852 btf_verifier_log_type(env, t, "Invalid value"); 3853 return -EINVAL; 3854 } 3855 3856 if (btf_type_vlen(t)) { 3857 btf_verifier_log_type(env, t, "vlen != 0"); 3858 return -EINVAL; 3859 } 3860 3861 if (btf_type_kflag(t)) { 3862 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); 3863 return -EINVAL; 3864 } 3865 3866 component_idx = btf_type_decl_tag(t)->component_idx; 3867 if (component_idx < -1) { 3868 btf_verifier_log_type(env, t, "Invalid component_idx"); 3869 return -EINVAL; 3870 } 3871 3872 btf_verifier_log_type(env, t, NULL); 3873 3874 return meta_needed; 3875 } 3876 3877 static int btf_decl_tag_resolve(struct btf_verifier_env *env, 3878 const struct resolve_vertex *v) 3879 { 3880 const struct btf_type *next_type; 3881 const struct btf_type *t = v->t; 3882 u32 next_type_id = t->type; 3883 struct btf *btf = env->btf; 3884 s32 component_idx; 3885 u32 vlen; 3886 3887 next_type = btf_type_by_id(btf, next_type_id); 3888 if (!next_type || !btf_type_is_decl_tag_target(next_type)) { 3889 btf_verifier_log_type(env, v->t, "Invalid type_id"); 3890 return -EINVAL; 3891 } 3892 3893 if (!env_type_is_resolve_sink(env, next_type) && 3894 !env_type_is_resolved(env, next_type_id)) 3895 return env_stack_push(env, next_type, next_type_id); 3896 3897 component_idx = btf_type_decl_tag(t)->component_idx; 3898 if (component_idx != -1) { 3899 if (btf_type_is_var(next_type) || btf_type_is_typedef(next_type)) { 3900 btf_verifier_log_type(env, v->t, "Invalid component_idx"); 3901 return -EINVAL; 3902 } 3903 3904 if (btf_type_is_struct(next_type)) { 3905 vlen = btf_type_vlen(next_type); 3906 } else { 3907 /* next_type should be a function */ 3908 next_type = btf_type_by_id(btf, next_type->type); 3909 vlen = btf_type_vlen(next_type); 3910 } 3911 3912 if ((u32)component_idx >= vlen) { 3913 btf_verifier_log_type(env, v->t, "Invalid component_idx"); 3914 return -EINVAL; 3915 } 3916 } 3917 3918 env_stack_pop_resolved(env, next_type_id, 0); 3919 3920 return 0; 3921 } 3922 3923 static void btf_decl_tag_log(struct btf_verifier_env *env, const struct btf_type *t) 3924 { 3925 btf_verifier_log(env, "type=%u component_idx=%d", t->type, 3926 btf_type_decl_tag(t)->component_idx); 3927 } 3928 3929 static const struct btf_kind_operations decl_tag_ops = { 3930 .check_meta = btf_decl_tag_check_meta, 3931 .resolve = btf_decl_tag_resolve, 3932 .check_member = btf_df_check_member, 3933 .check_kflag_member = btf_df_check_kflag_member, 3934 .log_details = btf_decl_tag_log, 3935 .show = btf_df_show, 3936 }; 3937 3938 static int btf_func_proto_check(struct btf_verifier_env *env, 3939 const struct btf_type *t) 3940 { 3941 const struct btf_type *ret_type; 3942 const struct btf_param *args; 3943 const struct btf *btf; 3944 u16 nr_args, i; 3945 int err; 3946 3947 btf = env->btf; 3948 args = (const struct btf_param *)(t + 1); 3949 nr_args = btf_type_vlen(t); 3950 3951 /* Check func return type which could be "void" (t->type == 0) */ 3952 if (t->type) { 3953 u32 ret_type_id = t->type; 3954 3955 ret_type = btf_type_by_id(btf, ret_type_id); 3956 if (!ret_type) { 3957 btf_verifier_log_type(env, t, "Invalid return type"); 3958 return -EINVAL; 3959 } 3960 3961 if (btf_type_needs_resolve(ret_type) && 3962 !env_type_is_resolved(env, ret_type_id)) { 3963 err = btf_resolve(env, ret_type, ret_type_id); 3964 if (err) 3965 return err; 3966 } 3967 3968 /* Ensure the return type is a type that has a size */ 3969 if (!btf_type_id_size(btf, &ret_type_id, NULL)) { 3970 btf_verifier_log_type(env, t, "Invalid return type"); 3971 return -EINVAL; 3972 } 3973 } 3974 3975 if (!nr_args) 3976 return 0; 3977 3978 /* Last func arg type_id could be 0 if it is a vararg */ 3979 if (!args[nr_args - 1].type) { 3980 if (args[nr_args - 1].name_off) { 3981 btf_verifier_log_type(env, t, "Invalid arg#%u", 3982 nr_args); 3983 return -EINVAL; 3984 } 3985 nr_args--; 3986 } 3987 3988 err = 0; 3989 for (i = 0; i < nr_args; i++) { 3990 const struct btf_type *arg_type; 3991 u32 arg_type_id; 3992 3993 arg_type_id = args[i].type; 3994 arg_type = btf_type_by_id(btf, arg_type_id); 3995 if (!arg_type) { 3996 btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1); 3997 err = -EINVAL; 3998 break; 3999 } 4000 4001 if (args[i].name_off && 4002 (!btf_name_offset_valid(btf, args[i].name_off) || 4003 !btf_name_valid_identifier(btf, args[i].name_off))) { 4004 btf_verifier_log_type(env, t, 4005 "Invalid arg#%u", i + 1); 4006 err = -EINVAL; 4007 break; 4008 } 4009 4010 if (btf_type_needs_resolve(arg_type) && 4011 !env_type_is_resolved(env, arg_type_id)) { 4012 err = btf_resolve(env, arg_type, arg_type_id); 4013 if (err) 4014 break; 4015 } 4016 4017 if (!btf_type_id_size(btf, &arg_type_id, NULL)) { 4018 btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1); 4019 err = -EINVAL; 4020 break; 4021 } 4022 } 4023 4024 return err; 4025 } 4026 4027 static int btf_func_check(struct btf_verifier_env *env, 4028 const struct btf_type *t) 4029 { 4030 const struct btf_type *proto_type; 4031 const struct btf_param *args; 4032 const struct btf *btf; 4033 u16 nr_args, i; 4034 4035 btf = env->btf; 4036 proto_type = btf_type_by_id(btf, t->type); 4037 4038 if (!proto_type || !btf_type_is_func_proto(proto_type)) { 4039 btf_verifier_log_type(env, t, "Invalid type_id"); 4040 return -EINVAL; 4041 } 4042 4043 args = (const struct btf_param *)(proto_type + 1); 4044 nr_args = btf_type_vlen(proto_type); 4045 for (i = 0; i < nr_args; i++) { 4046 if (!args[i].name_off && args[i].type) { 4047 btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1); 4048 return -EINVAL; 4049 } 4050 } 4051 4052 return 0; 4053 } 4054 4055 static const struct btf_kind_operations * const kind_ops[NR_BTF_KINDS] = { 4056 [BTF_KIND_INT] = &int_ops, 4057 [BTF_KIND_PTR] = &ptr_ops, 4058 [BTF_KIND_ARRAY] = &array_ops, 4059 [BTF_KIND_STRUCT] = &struct_ops, 4060 [BTF_KIND_UNION] = &struct_ops, 4061 [BTF_KIND_ENUM] = &enum_ops, 4062 [BTF_KIND_FWD] = &fwd_ops, 4063 [BTF_KIND_TYPEDEF] = &modifier_ops, 4064 [BTF_KIND_VOLATILE] = &modifier_ops, 4065 [BTF_KIND_CONST] = &modifier_ops, 4066 [BTF_KIND_RESTRICT] = &modifier_ops, 4067 [BTF_KIND_FUNC] = &func_ops, 4068 [BTF_KIND_FUNC_PROTO] = &func_proto_ops, 4069 [BTF_KIND_VAR] = &var_ops, 4070 [BTF_KIND_DATASEC] = &datasec_ops, 4071 [BTF_KIND_FLOAT] = &float_ops, 4072 [BTF_KIND_DECL_TAG] = &decl_tag_ops, 4073 [BTF_KIND_TYPE_TAG] = &modifier_ops, 4074 }; 4075 4076 static s32 btf_check_meta(struct btf_verifier_env *env, 4077 const struct btf_type *t, 4078 u32 meta_left) 4079 { 4080 u32 saved_meta_left = meta_left; 4081 s32 var_meta_size; 4082 4083 if (meta_left < sizeof(*t)) { 4084 btf_verifier_log(env, "[%u] meta_left:%u meta_needed:%zu", 4085 env->log_type_id, meta_left, sizeof(*t)); 4086 return -EINVAL; 4087 } 4088 meta_left -= sizeof(*t); 4089 4090 if (t->info & ~BTF_INFO_MASK) { 4091 btf_verifier_log(env, "[%u] Invalid btf_info:%x", 4092 env->log_type_id, t->info); 4093 return -EINVAL; 4094 } 4095 4096 if (BTF_INFO_KIND(t->info) > BTF_KIND_MAX || 4097 BTF_INFO_KIND(t->info) == BTF_KIND_UNKN) { 4098 btf_verifier_log(env, "[%u] Invalid kind:%u", 4099 env->log_type_id, BTF_INFO_KIND(t->info)); 4100 return -EINVAL; 4101 } 4102 4103 if (!btf_name_offset_valid(env->btf, t->name_off)) { 4104 btf_verifier_log(env, "[%u] Invalid name_offset:%u", 4105 env->log_type_id, t->name_off); 4106 return -EINVAL; 4107 } 4108 4109 var_meta_size = btf_type_ops(t)->check_meta(env, t, meta_left); 4110 if (var_meta_size < 0) 4111 return var_meta_size; 4112 4113 meta_left -= var_meta_size; 4114 4115 return saved_meta_left - meta_left; 4116 } 4117 4118 static int btf_check_all_metas(struct btf_verifier_env *env) 4119 { 4120 struct btf *btf = env->btf; 4121 struct btf_header *hdr; 4122 void *cur, *end; 4123 4124 hdr = &btf->hdr; 4125 cur = btf->nohdr_data + hdr->type_off; 4126 end = cur + hdr->type_len; 4127 4128 env->log_type_id = btf->base_btf ? btf->start_id : 1; 4129 while (cur < end) { 4130 struct btf_type *t = cur; 4131 s32 meta_size; 4132 4133 meta_size = btf_check_meta(env, t, end - cur); 4134 if (meta_size < 0) 4135 return meta_size; 4136 4137 btf_add_type(env, t); 4138 cur += meta_size; 4139 env->log_type_id++; 4140 } 4141 4142 return 0; 4143 } 4144 4145 static bool btf_resolve_valid(struct btf_verifier_env *env, 4146 const struct btf_type *t, 4147 u32 type_id) 4148 { 4149 struct btf *btf = env->btf; 4150 4151 if (!env_type_is_resolved(env, type_id)) 4152 return false; 4153 4154 if (btf_type_is_struct(t) || btf_type_is_datasec(t)) 4155 return !btf_resolved_type_id(btf, type_id) && 4156 !btf_resolved_type_size(btf, type_id); 4157 4158 if (btf_type_is_decl_tag(t)) 4159 return btf_resolved_type_id(btf, type_id) && 4160 !btf_resolved_type_size(btf, type_id); 4161 4162 if (btf_type_is_modifier(t) || btf_type_is_ptr(t) || 4163 btf_type_is_var(t)) { 4164 t = btf_type_id_resolve(btf, &type_id); 4165 return t && 4166 !btf_type_is_modifier(t) && 4167 !btf_type_is_var(t) && 4168 !btf_type_is_datasec(t); 4169 } 4170 4171 if (btf_type_is_array(t)) { 4172 const struct btf_array *array = btf_type_array(t); 4173 const struct btf_type *elem_type; 4174 u32 elem_type_id = array->type; 4175 u32 elem_size; 4176 4177 elem_type = btf_type_id_size(btf, &elem_type_id, &elem_size); 4178 return elem_type && !btf_type_is_modifier(elem_type) && 4179 (array->nelems * elem_size == 4180 btf_resolved_type_size(btf, type_id)); 4181 } 4182 4183 return false; 4184 } 4185 4186 static int btf_resolve(struct btf_verifier_env *env, 4187 const struct btf_type *t, u32 type_id) 4188 { 4189 u32 save_log_type_id = env->log_type_id; 4190 const struct resolve_vertex *v; 4191 int err = 0; 4192 4193 env->resolve_mode = RESOLVE_TBD; 4194 env_stack_push(env, t, type_id); 4195 while (!err && (v = env_stack_peak(env))) { 4196 env->log_type_id = v->type_id; 4197 err = btf_type_ops(v->t)->resolve(env, v); 4198 } 4199 4200 env->log_type_id = type_id; 4201 if (err == -E2BIG) { 4202 btf_verifier_log_type(env, t, 4203 "Exceeded max resolving depth:%u", 4204 MAX_RESOLVE_DEPTH); 4205 } else if (err == -EEXIST) { 4206 btf_verifier_log_type(env, t, "Loop detected"); 4207 } 4208 4209 /* Final sanity check */ 4210 if (!err && !btf_resolve_valid(env, t, type_id)) { 4211 btf_verifier_log_type(env, t, "Invalid resolve state"); 4212 err = -EINVAL; 4213 } 4214 4215 env->log_type_id = save_log_type_id; 4216 return err; 4217 } 4218 4219 static int btf_check_all_types(struct btf_verifier_env *env) 4220 { 4221 struct btf *btf = env->btf; 4222 const struct btf_type *t; 4223 u32 type_id, i; 4224 int err; 4225 4226 err = env_resolve_init(env); 4227 if (err) 4228 return err; 4229 4230 env->phase++; 4231 for (i = btf->base_btf ? 0 : 1; i < btf->nr_types; i++) { 4232 type_id = btf->start_id + i; 4233 t = btf_type_by_id(btf, type_id); 4234 4235 env->log_type_id = type_id; 4236 if (btf_type_needs_resolve(t) && 4237 !env_type_is_resolved(env, type_id)) { 4238 err = btf_resolve(env, t, type_id); 4239 if (err) 4240 return err; 4241 } 4242 4243 if (btf_type_is_func_proto(t)) { 4244 err = btf_func_proto_check(env, t); 4245 if (err) 4246 return err; 4247 } 4248 4249 if (btf_type_is_func(t)) { 4250 err = btf_func_check(env, t); 4251 if (err) 4252 return err; 4253 } 4254 } 4255 4256 return 0; 4257 } 4258 4259 static int btf_parse_type_sec(struct btf_verifier_env *env) 4260 { 4261 const struct btf_header *hdr = &env->btf->hdr; 4262 int err; 4263 4264 /* Type section must align to 4 bytes */ 4265 if (hdr->type_off & (sizeof(u32) - 1)) { 4266 btf_verifier_log(env, "Unaligned type_off"); 4267 return -EINVAL; 4268 } 4269 4270 if (!env->btf->base_btf && !hdr->type_len) { 4271 btf_verifier_log(env, "No type found"); 4272 return -EINVAL; 4273 } 4274 4275 err = btf_check_all_metas(env); 4276 if (err) 4277 return err; 4278 4279 return btf_check_all_types(env); 4280 } 4281 4282 static int btf_parse_str_sec(struct btf_verifier_env *env) 4283 { 4284 const struct btf_header *hdr; 4285 struct btf *btf = env->btf; 4286 const char *start, *end; 4287 4288 hdr = &btf->hdr; 4289 start = btf->nohdr_data + hdr->str_off; 4290 end = start + hdr->str_len; 4291 4292 if (end != btf->data + btf->data_size) { 4293 btf_verifier_log(env, "String section is not at the end"); 4294 return -EINVAL; 4295 } 4296 4297 btf->strings = start; 4298 4299 if (btf->base_btf && !hdr->str_len) 4300 return 0; 4301 if (!hdr->str_len || hdr->str_len - 1 > BTF_MAX_NAME_OFFSET || end[-1]) { 4302 btf_verifier_log(env, "Invalid string section"); 4303 return -EINVAL; 4304 } 4305 if (!btf->base_btf && start[0]) { 4306 btf_verifier_log(env, "Invalid string section"); 4307 return -EINVAL; 4308 } 4309 4310 return 0; 4311 } 4312 4313 static const size_t btf_sec_info_offset[] = { 4314 offsetof(struct btf_header, type_off), 4315 offsetof(struct btf_header, str_off), 4316 }; 4317 4318 static int btf_sec_info_cmp(const void *a, const void *b) 4319 { 4320 const struct btf_sec_info *x = a; 4321 const struct btf_sec_info *y = b; 4322 4323 return (int)(x->off - y->off) ? : (int)(x->len - y->len); 4324 } 4325 4326 static int btf_check_sec_info(struct btf_verifier_env *env, 4327 u32 btf_data_size) 4328 { 4329 struct btf_sec_info secs[ARRAY_SIZE(btf_sec_info_offset)]; 4330 u32 total, expected_total, i; 4331 const struct btf_header *hdr; 4332 const struct btf *btf; 4333 4334 btf = env->btf; 4335 hdr = &btf->hdr; 4336 4337 /* Populate the secs from hdr */ 4338 for (i = 0; i < ARRAY_SIZE(btf_sec_info_offset); i++) 4339 secs[i] = *(struct btf_sec_info *)((void *)hdr + 4340 btf_sec_info_offset[i]); 4341 4342 sort(secs, ARRAY_SIZE(btf_sec_info_offset), 4343 sizeof(struct btf_sec_info), btf_sec_info_cmp, NULL); 4344 4345 /* Check for gaps and overlap among sections */ 4346 total = 0; 4347 expected_total = btf_data_size - hdr->hdr_len; 4348 for (i = 0; i < ARRAY_SIZE(btf_sec_info_offset); i++) { 4349 if (expected_total < secs[i].off) { 4350 btf_verifier_log(env, "Invalid section offset"); 4351 return -EINVAL; 4352 } 4353 if (total < secs[i].off) { 4354 /* gap */ 4355 btf_verifier_log(env, "Unsupported section found"); 4356 return -EINVAL; 4357 } 4358 if (total > secs[i].off) { 4359 btf_verifier_log(env, "Section overlap found"); 4360 return -EINVAL; 4361 } 4362 if (expected_total - total < secs[i].len) { 4363 btf_verifier_log(env, 4364 "Total section length too long"); 4365 return -EINVAL; 4366 } 4367 total += secs[i].len; 4368 } 4369 4370 /* There is data other than hdr and known sections */ 4371 if (expected_total != total) { 4372 btf_verifier_log(env, "Unsupported section found"); 4373 return -EINVAL; 4374 } 4375 4376 return 0; 4377 } 4378 4379 static int btf_parse_hdr(struct btf_verifier_env *env) 4380 { 4381 u32 hdr_len, hdr_copy, btf_data_size; 4382 const struct btf_header *hdr; 4383 struct btf *btf; 4384 int err; 4385 4386 btf = env->btf; 4387 btf_data_size = btf->data_size; 4388 4389 if (btf_data_size < 4390 offsetof(struct btf_header, hdr_len) + sizeof(hdr->hdr_len)) { 4391 btf_verifier_log(env, "hdr_len not found"); 4392 return -EINVAL; 4393 } 4394 4395 hdr = btf->data; 4396 hdr_len = hdr->hdr_len; 4397 if (btf_data_size < hdr_len) { 4398 btf_verifier_log(env, "btf_header not found"); 4399 return -EINVAL; 4400 } 4401 4402 /* Ensure the unsupported header fields are zero */ 4403 if (hdr_len > sizeof(btf->hdr)) { 4404 u8 *expected_zero = btf->data + sizeof(btf->hdr); 4405 u8 *end = btf->data + hdr_len; 4406 4407 for (; expected_zero < end; expected_zero++) { 4408 if (*expected_zero) { 4409 btf_verifier_log(env, "Unsupported btf_header"); 4410 return -E2BIG; 4411 } 4412 } 4413 } 4414 4415 hdr_copy = min_t(u32, hdr_len, sizeof(btf->hdr)); 4416 memcpy(&btf->hdr, btf->data, hdr_copy); 4417 4418 hdr = &btf->hdr; 4419 4420 btf_verifier_log_hdr(env, btf_data_size); 4421 4422 if (hdr->magic != BTF_MAGIC) { 4423 btf_verifier_log(env, "Invalid magic"); 4424 return -EINVAL; 4425 } 4426 4427 if (hdr->version != BTF_VERSION) { 4428 btf_verifier_log(env, "Unsupported version"); 4429 return -ENOTSUPP; 4430 } 4431 4432 if (hdr->flags) { 4433 btf_verifier_log(env, "Unsupported flags"); 4434 return -ENOTSUPP; 4435 } 4436 4437 if (!btf->base_btf && btf_data_size == hdr->hdr_len) { 4438 btf_verifier_log(env, "No data"); 4439 return -EINVAL; 4440 } 4441 4442 err = btf_check_sec_info(env, btf_data_size); 4443 if (err) 4444 return err; 4445 4446 return 0; 4447 } 4448 4449 static struct btf *btf_parse(bpfptr_t btf_data, u32 btf_data_size, 4450 u32 log_level, char __user *log_ubuf, u32 log_size) 4451 { 4452 struct btf_verifier_env *env = NULL; 4453 struct bpf_verifier_log *log; 4454 struct btf *btf = NULL; 4455 u8 *data; 4456 int err; 4457 4458 if (btf_data_size > BTF_MAX_SIZE) 4459 return ERR_PTR(-E2BIG); 4460 4461 env = kzalloc(sizeof(*env), GFP_KERNEL | __GFP_NOWARN); 4462 if (!env) 4463 return ERR_PTR(-ENOMEM); 4464 4465 log = &env->log; 4466 if (log_level || log_ubuf || log_size) { 4467 /* user requested verbose verifier output 4468 * and supplied buffer to store the verification trace 4469 */ 4470 log->level = log_level; 4471 log->ubuf = log_ubuf; 4472 log->len_total = log_size; 4473 4474 /* log attributes have to be sane */ 4475 if (log->len_total < 128 || log->len_total > UINT_MAX >> 8 || 4476 !log->level || !log->ubuf) { 4477 err = -EINVAL; 4478 goto errout; 4479 } 4480 } 4481 4482 btf = kzalloc(sizeof(*btf), GFP_KERNEL | __GFP_NOWARN); 4483 if (!btf) { 4484 err = -ENOMEM; 4485 goto errout; 4486 } 4487 env->btf = btf; 4488 4489 data = kvmalloc(btf_data_size, GFP_KERNEL | __GFP_NOWARN); 4490 if (!data) { 4491 err = -ENOMEM; 4492 goto errout; 4493 } 4494 4495 btf->data = data; 4496 btf->data_size = btf_data_size; 4497 4498 if (copy_from_bpfptr(data, btf_data, btf_data_size)) { 4499 err = -EFAULT; 4500 goto errout; 4501 } 4502 4503 err = btf_parse_hdr(env); 4504 if (err) 4505 goto errout; 4506 4507 btf->nohdr_data = btf->data + btf->hdr.hdr_len; 4508 4509 err = btf_parse_str_sec(env); 4510 if (err) 4511 goto errout; 4512 4513 err = btf_parse_type_sec(env); 4514 if (err) 4515 goto errout; 4516 4517 if (log->level && bpf_verifier_log_full(log)) { 4518 err = -ENOSPC; 4519 goto errout; 4520 } 4521 4522 btf_verifier_env_free(env); 4523 refcount_set(&btf->refcnt, 1); 4524 return btf; 4525 4526 errout: 4527 btf_verifier_env_free(env); 4528 if (btf) 4529 btf_free(btf); 4530 return ERR_PTR(err); 4531 } 4532 4533 extern char __weak __start_BTF[]; 4534 extern char __weak __stop_BTF[]; 4535 extern struct btf *btf_vmlinux; 4536 4537 #define BPF_MAP_TYPE(_id, _ops) 4538 #define BPF_LINK_TYPE(_id, _name) 4539 static union { 4540 struct bpf_ctx_convert { 4541 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \ 4542 prog_ctx_type _id##_prog; \ 4543 kern_ctx_type _id##_kern; 4544 #include <linux/bpf_types.h> 4545 #undef BPF_PROG_TYPE 4546 } *__t; 4547 /* 't' is written once under lock. Read many times. */ 4548 const struct btf_type *t; 4549 } bpf_ctx_convert; 4550 enum { 4551 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \ 4552 __ctx_convert##_id, 4553 #include <linux/bpf_types.h> 4554 #undef BPF_PROG_TYPE 4555 __ctx_convert_unused, /* to avoid empty enum in extreme .config */ 4556 }; 4557 static u8 bpf_ctx_convert_map[] = { 4558 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \ 4559 [_id] = __ctx_convert##_id, 4560 #include <linux/bpf_types.h> 4561 #undef BPF_PROG_TYPE 4562 0, /* avoid empty array */ 4563 }; 4564 #undef BPF_MAP_TYPE 4565 #undef BPF_LINK_TYPE 4566 4567 static const struct btf_member * 4568 btf_get_prog_ctx_type(struct bpf_verifier_log *log, const struct btf *btf, 4569 const struct btf_type *t, enum bpf_prog_type prog_type, 4570 int arg) 4571 { 4572 const struct btf_type *conv_struct; 4573 const struct btf_type *ctx_struct; 4574 const struct btf_member *ctx_type; 4575 const char *tname, *ctx_tname; 4576 4577 conv_struct = bpf_ctx_convert.t; 4578 if (!conv_struct) { 4579 bpf_log(log, "btf_vmlinux is malformed\n"); 4580 return NULL; 4581 } 4582 t = btf_type_by_id(btf, t->type); 4583 while (btf_type_is_modifier(t)) 4584 t = btf_type_by_id(btf, t->type); 4585 if (!btf_type_is_struct(t)) { 4586 /* Only pointer to struct is supported for now. 4587 * That means that BPF_PROG_TYPE_TRACEPOINT with BTF 4588 * is not supported yet. 4589 * BPF_PROG_TYPE_RAW_TRACEPOINT is fine. 4590 */ 4591 return NULL; 4592 } 4593 tname = btf_name_by_offset(btf, t->name_off); 4594 if (!tname) { 4595 bpf_log(log, "arg#%d struct doesn't have a name\n", arg); 4596 return NULL; 4597 } 4598 /* prog_type is valid bpf program type. No need for bounds check. */ 4599 ctx_type = btf_type_member(conv_struct) + bpf_ctx_convert_map[prog_type] * 2; 4600 /* ctx_struct is a pointer to prog_ctx_type in vmlinux. 4601 * Like 'struct __sk_buff' 4602 */ 4603 ctx_struct = btf_type_by_id(btf_vmlinux, ctx_type->type); 4604 if (!ctx_struct) 4605 /* should not happen */ 4606 return NULL; 4607 ctx_tname = btf_name_by_offset(btf_vmlinux, ctx_struct->name_off); 4608 if (!ctx_tname) { 4609 /* should not happen */ 4610 bpf_log(log, "Please fix kernel include/linux/bpf_types.h\n"); 4611 return NULL; 4612 } 4613 /* only compare that prog's ctx type name is the same as 4614 * kernel expects. No need to compare field by field. 4615 * It's ok for bpf prog to do: 4616 * struct __sk_buff {}; 4617 * int socket_filter_bpf_prog(struct __sk_buff *skb) 4618 * { // no fields of skb are ever used } 4619 */ 4620 if (strcmp(ctx_tname, tname)) 4621 return NULL; 4622 return ctx_type; 4623 } 4624 4625 static const struct bpf_map_ops * const btf_vmlinux_map_ops[] = { 4626 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) 4627 #define BPF_LINK_TYPE(_id, _name) 4628 #define BPF_MAP_TYPE(_id, _ops) \ 4629 [_id] = &_ops, 4630 #include <linux/bpf_types.h> 4631 #undef BPF_PROG_TYPE 4632 #undef BPF_LINK_TYPE 4633 #undef BPF_MAP_TYPE 4634 }; 4635 4636 static int btf_vmlinux_map_ids_init(const struct btf *btf, 4637 struct bpf_verifier_log *log) 4638 { 4639 const struct bpf_map_ops *ops; 4640 int i, btf_id; 4641 4642 for (i = 0; i < ARRAY_SIZE(btf_vmlinux_map_ops); ++i) { 4643 ops = btf_vmlinux_map_ops[i]; 4644 if (!ops || (!ops->map_btf_name && !ops->map_btf_id)) 4645 continue; 4646 if (!ops->map_btf_name || !ops->map_btf_id) { 4647 bpf_log(log, "map type %d is misconfigured\n", i); 4648 return -EINVAL; 4649 } 4650 btf_id = btf_find_by_name_kind(btf, ops->map_btf_name, 4651 BTF_KIND_STRUCT); 4652 if (btf_id < 0) 4653 return btf_id; 4654 *ops->map_btf_id = btf_id; 4655 } 4656 4657 return 0; 4658 } 4659 4660 static int btf_translate_to_vmlinux(struct bpf_verifier_log *log, 4661 struct btf *btf, 4662 const struct btf_type *t, 4663 enum bpf_prog_type prog_type, 4664 int arg) 4665 { 4666 const struct btf_member *prog_ctx_type, *kern_ctx_type; 4667 4668 prog_ctx_type = btf_get_prog_ctx_type(log, btf, t, prog_type, arg); 4669 if (!prog_ctx_type) 4670 return -ENOENT; 4671 kern_ctx_type = prog_ctx_type + 1; 4672 return kern_ctx_type->type; 4673 } 4674 4675 BTF_ID_LIST(bpf_ctx_convert_btf_id) 4676 BTF_ID(struct, bpf_ctx_convert) 4677 4678 struct btf *btf_parse_vmlinux(void) 4679 { 4680 struct btf_verifier_env *env = NULL; 4681 struct bpf_verifier_log *log; 4682 struct btf *btf = NULL; 4683 int err; 4684 4685 env = kzalloc(sizeof(*env), GFP_KERNEL | __GFP_NOWARN); 4686 if (!env) 4687 return ERR_PTR(-ENOMEM); 4688 4689 log = &env->log; 4690 log->level = BPF_LOG_KERNEL; 4691 4692 btf = kzalloc(sizeof(*btf), GFP_KERNEL | __GFP_NOWARN); 4693 if (!btf) { 4694 err = -ENOMEM; 4695 goto errout; 4696 } 4697 env->btf = btf; 4698 4699 btf->data = __start_BTF; 4700 btf->data_size = __stop_BTF - __start_BTF; 4701 btf->kernel_btf = true; 4702 snprintf(btf->name, sizeof(btf->name), "vmlinux"); 4703 4704 err = btf_parse_hdr(env); 4705 if (err) 4706 goto errout; 4707 4708 btf->nohdr_data = btf->data + btf->hdr.hdr_len; 4709 4710 err = btf_parse_str_sec(env); 4711 if (err) 4712 goto errout; 4713 4714 err = btf_check_all_metas(env); 4715 if (err) 4716 goto errout; 4717 4718 /* btf_parse_vmlinux() runs under bpf_verifier_lock */ 4719 bpf_ctx_convert.t = btf_type_by_id(btf, bpf_ctx_convert_btf_id[0]); 4720 4721 /* find bpf map structs for map_ptr access checking */ 4722 err = btf_vmlinux_map_ids_init(btf, log); 4723 if (err < 0) 4724 goto errout; 4725 4726 bpf_struct_ops_init(btf, log); 4727 4728 refcount_set(&btf->refcnt, 1); 4729 4730 err = btf_alloc_id(btf); 4731 if (err) 4732 goto errout; 4733 4734 btf_verifier_env_free(env); 4735 return btf; 4736 4737 errout: 4738 btf_verifier_env_free(env); 4739 if (btf) { 4740 kvfree(btf->types); 4741 kfree(btf); 4742 } 4743 return ERR_PTR(err); 4744 } 4745 4746 #ifdef CONFIG_DEBUG_INFO_BTF_MODULES 4747 4748 static struct btf *btf_parse_module(const char *module_name, const void *data, unsigned int data_size) 4749 { 4750 struct btf_verifier_env *env = NULL; 4751 struct bpf_verifier_log *log; 4752 struct btf *btf = NULL, *base_btf; 4753 int err; 4754 4755 base_btf = bpf_get_btf_vmlinux(); 4756 if (IS_ERR(base_btf)) 4757 return base_btf; 4758 if (!base_btf) 4759 return ERR_PTR(-EINVAL); 4760 4761 env = kzalloc(sizeof(*env), GFP_KERNEL | __GFP_NOWARN); 4762 if (!env) 4763 return ERR_PTR(-ENOMEM); 4764 4765 log = &env->log; 4766 log->level = BPF_LOG_KERNEL; 4767 4768 btf = kzalloc(sizeof(*btf), GFP_KERNEL | __GFP_NOWARN); 4769 if (!btf) { 4770 err = -ENOMEM; 4771 goto errout; 4772 } 4773 env->btf = btf; 4774 4775 btf->base_btf = base_btf; 4776 btf->start_id = base_btf->nr_types; 4777 btf->start_str_off = base_btf->hdr.str_len; 4778 btf->kernel_btf = true; 4779 snprintf(btf->name, sizeof(btf->name), "%s", module_name); 4780 4781 btf->data = kvmalloc(data_size, GFP_KERNEL | __GFP_NOWARN); 4782 if (!btf->data) { 4783 err = -ENOMEM; 4784 goto errout; 4785 } 4786 memcpy(btf->data, data, data_size); 4787 btf->data_size = data_size; 4788 4789 err = btf_parse_hdr(env); 4790 if (err) 4791 goto errout; 4792 4793 btf->nohdr_data = btf->data + btf->hdr.hdr_len; 4794 4795 err = btf_parse_str_sec(env); 4796 if (err) 4797 goto errout; 4798 4799 err = btf_check_all_metas(env); 4800 if (err) 4801 goto errout; 4802 4803 btf_verifier_env_free(env); 4804 refcount_set(&btf->refcnt, 1); 4805 return btf; 4806 4807 errout: 4808 btf_verifier_env_free(env); 4809 if (btf) { 4810 kvfree(btf->data); 4811 kvfree(btf->types); 4812 kfree(btf); 4813 } 4814 return ERR_PTR(err); 4815 } 4816 4817 #endif /* CONFIG_DEBUG_INFO_BTF_MODULES */ 4818 4819 struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog) 4820 { 4821 struct bpf_prog *tgt_prog = prog->aux->dst_prog; 4822 4823 if (tgt_prog) 4824 return tgt_prog->aux->btf; 4825 else 4826 return prog->aux->attach_btf; 4827 } 4828 4829 static bool is_string_ptr(struct btf *btf, const struct btf_type *t) 4830 { 4831 /* t comes in already as a pointer */ 4832 t = btf_type_by_id(btf, t->type); 4833 4834 /* allow const */ 4835 if (BTF_INFO_KIND(t->info) == BTF_KIND_CONST) 4836 t = btf_type_by_id(btf, t->type); 4837 4838 /* char, signed char, unsigned char */ 4839 return btf_type_is_int(t) && t->size == 1; 4840 } 4841 4842 bool btf_ctx_access(int off, int size, enum bpf_access_type type, 4843 const struct bpf_prog *prog, 4844 struct bpf_insn_access_aux *info) 4845 { 4846 const struct btf_type *t = prog->aux->attach_func_proto; 4847 struct bpf_prog *tgt_prog = prog->aux->dst_prog; 4848 struct btf *btf = bpf_prog_get_target_btf(prog); 4849 const char *tname = prog->aux->attach_func_name; 4850 struct bpf_verifier_log *log = info->log; 4851 const struct btf_param *args; 4852 u32 nr_args, arg; 4853 int i, ret; 4854 4855 if (off % 8) { 4856 bpf_log(log, "func '%s' offset %d is not multiple of 8\n", 4857 tname, off); 4858 return false; 4859 } 4860 arg = off / 8; 4861 args = (const struct btf_param *)(t + 1); 4862 /* if (t == NULL) Fall back to default BPF prog with 4863 * MAX_BPF_FUNC_REG_ARGS u64 arguments. 4864 */ 4865 nr_args = t ? btf_type_vlen(t) : MAX_BPF_FUNC_REG_ARGS; 4866 if (prog->aux->attach_btf_trace) { 4867 /* skip first 'void *__data' argument in btf_trace_##name typedef */ 4868 args++; 4869 nr_args--; 4870 } 4871 4872 if (arg > nr_args) { 4873 bpf_log(log, "func '%s' doesn't have %d-th argument\n", 4874 tname, arg + 1); 4875 return false; 4876 } 4877 4878 if (arg == nr_args) { 4879 switch (prog->expected_attach_type) { 4880 case BPF_LSM_MAC: 4881 case BPF_TRACE_FEXIT: 4882 /* When LSM programs are attached to void LSM hooks 4883 * they use FEXIT trampolines and when attached to 4884 * int LSM hooks, they use MODIFY_RETURN trampolines. 4885 * 4886 * While the LSM programs are BPF_MODIFY_RETURN-like 4887 * the check: 4888 * 4889 * if (ret_type != 'int') 4890 * return -EINVAL; 4891 * 4892 * is _not_ done here. This is still safe as LSM hooks 4893 * have only void and int return types. 4894 */ 4895 if (!t) 4896 return true; 4897 t = btf_type_by_id(btf, t->type); 4898 break; 4899 case BPF_MODIFY_RETURN: 4900 /* For now the BPF_MODIFY_RETURN can only be attached to 4901 * functions that return an int. 4902 */ 4903 if (!t) 4904 return false; 4905 4906 t = btf_type_skip_modifiers(btf, t->type, NULL); 4907 if (!btf_type_is_small_int(t)) { 4908 bpf_log(log, 4909 "ret type %s not allowed for fmod_ret\n", 4910 btf_kind_str[BTF_INFO_KIND(t->info)]); 4911 return false; 4912 } 4913 break; 4914 default: 4915 bpf_log(log, "func '%s' doesn't have %d-th argument\n", 4916 tname, arg + 1); 4917 return false; 4918 } 4919 } else { 4920 if (!t) 4921 /* Default prog with MAX_BPF_FUNC_REG_ARGS args */ 4922 return true; 4923 t = btf_type_by_id(btf, args[arg].type); 4924 } 4925 4926 /* skip modifiers */ 4927 while (btf_type_is_modifier(t)) 4928 t = btf_type_by_id(btf, t->type); 4929 if (btf_type_is_small_int(t) || btf_type_is_enum(t)) 4930 /* accessing a scalar */ 4931 return true; 4932 if (!btf_type_is_ptr(t)) { 4933 bpf_log(log, 4934 "func '%s' arg%d '%s' has type %s. Only pointer access is allowed\n", 4935 tname, arg, 4936 __btf_name_by_offset(btf, t->name_off), 4937 btf_kind_str[BTF_INFO_KIND(t->info)]); 4938 return false; 4939 } 4940 4941 /* check for PTR_TO_RDONLY_BUF_OR_NULL or PTR_TO_RDWR_BUF_OR_NULL */ 4942 for (i = 0; i < prog->aux->ctx_arg_info_size; i++) { 4943 const struct bpf_ctx_arg_aux *ctx_arg_info = &prog->aux->ctx_arg_info[i]; 4944 4945 if (ctx_arg_info->offset == off && 4946 (ctx_arg_info->reg_type == PTR_TO_RDONLY_BUF_OR_NULL || 4947 ctx_arg_info->reg_type == PTR_TO_RDWR_BUF_OR_NULL)) { 4948 info->reg_type = ctx_arg_info->reg_type; 4949 return true; 4950 } 4951 } 4952 4953 if (t->type == 0) 4954 /* This is a pointer to void. 4955 * It is the same as scalar from the verifier safety pov. 4956 * No further pointer walking is allowed. 4957 */ 4958 return true; 4959 4960 if (is_string_ptr(btf, t)) 4961 return true; 4962 4963 /* this is a pointer to another type */ 4964 for (i = 0; i < prog->aux->ctx_arg_info_size; i++) { 4965 const struct bpf_ctx_arg_aux *ctx_arg_info = &prog->aux->ctx_arg_info[i]; 4966 4967 if (ctx_arg_info->offset == off) { 4968 if (!ctx_arg_info->btf_id) { 4969 bpf_log(log,"invalid btf_id for context argument offset %u\n", off); 4970 return false; 4971 } 4972 4973 info->reg_type = ctx_arg_info->reg_type; 4974 info->btf = btf_vmlinux; 4975 info->btf_id = ctx_arg_info->btf_id; 4976 return true; 4977 } 4978 } 4979 4980 info->reg_type = PTR_TO_BTF_ID; 4981 if (tgt_prog) { 4982 enum bpf_prog_type tgt_type; 4983 4984 if (tgt_prog->type == BPF_PROG_TYPE_EXT) 4985 tgt_type = tgt_prog->aux->saved_dst_prog_type; 4986 else 4987 tgt_type = tgt_prog->type; 4988 4989 ret = btf_translate_to_vmlinux(log, btf, t, tgt_type, arg); 4990 if (ret > 0) { 4991 info->btf = btf_vmlinux; 4992 info->btf_id = ret; 4993 return true; 4994 } else { 4995 return false; 4996 } 4997 } 4998 4999 info->btf = btf; 5000 info->btf_id = t->type; 5001 t = btf_type_by_id(btf, t->type); 5002 /* skip modifiers */ 5003 while (btf_type_is_modifier(t)) { 5004 info->btf_id = t->type; 5005 t = btf_type_by_id(btf, t->type); 5006 } 5007 if (!btf_type_is_struct(t)) { 5008 bpf_log(log, 5009 "func '%s' arg%d type %s is not a struct\n", 5010 tname, arg, btf_kind_str[BTF_INFO_KIND(t->info)]); 5011 return false; 5012 } 5013 bpf_log(log, "func '%s' arg%d has btf_id %d type %s '%s'\n", 5014 tname, arg, info->btf_id, btf_kind_str[BTF_INFO_KIND(t->info)], 5015 __btf_name_by_offset(btf, t->name_off)); 5016 return true; 5017 } 5018 5019 enum bpf_struct_walk_result { 5020 /* < 0 error */ 5021 WALK_SCALAR = 0, 5022 WALK_PTR, 5023 WALK_STRUCT, 5024 }; 5025 5026 static int btf_struct_walk(struct bpf_verifier_log *log, const struct btf *btf, 5027 const struct btf_type *t, int off, int size, 5028 u32 *next_btf_id) 5029 { 5030 u32 i, moff, mtrue_end, msize = 0, total_nelems = 0; 5031 const struct btf_type *mtype, *elem_type = NULL; 5032 const struct btf_member *member; 5033 const char *tname, *mname; 5034 u32 vlen, elem_id, mid; 5035 5036 again: 5037 tname = __btf_name_by_offset(btf, t->name_off); 5038 if (!btf_type_is_struct(t)) { 5039 bpf_log(log, "Type '%s' is not a struct\n", tname); 5040 return -EINVAL; 5041 } 5042 5043 vlen = btf_type_vlen(t); 5044 if (off + size > t->size) { 5045 /* If the last element is a variable size array, we may 5046 * need to relax the rule. 5047 */ 5048 struct btf_array *array_elem; 5049 5050 if (vlen == 0) 5051 goto error; 5052 5053 member = btf_type_member(t) + vlen - 1; 5054 mtype = btf_type_skip_modifiers(btf, member->type, 5055 NULL); 5056 if (!btf_type_is_array(mtype)) 5057 goto error; 5058 5059 array_elem = (struct btf_array *)(mtype + 1); 5060 if (array_elem->nelems != 0) 5061 goto error; 5062 5063 moff = btf_member_bit_offset(t, member) / 8; 5064 if (off < moff) 5065 goto error; 5066 5067 /* Only allow structure for now, can be relaxed for 5068 * other types later. 5069 */ 5070 t = btf_type_skip_modifiers(btf, array_elem->type, 5071 NULL); 5072 if (!btf_type_is_struct(t)) 5073 goto error; 5074 5075 off = (off - moff) % t->size; 5076 goto again; 5077 5078 error: 5079 bpf_log(log, "access beyond struct %s at off %u size %u\n", 5080 tname, off, size); 5081 return -EACCES; 5082 } 5083 5084 for_each_member(i, t, member) { 5085 /* offset of the field in bytes */ 5086 moff = btf_member_bit_offset(t, member) / 8; 5087 if (off + size <= moff) 5088 /* won't find anything, field is already too far */ 5089 break; 5090 5091 if (btf_member_bitfield_size(t, member)) { 5092 u32 end_bit = btf_member_bit_offset(t, member) + 5093 btf_member_bitfield_size(t, member); 5094 5095 /* off <= moff instead of off == moff because clang 5096 * does not generate a BTF member for anonymous 5097 * bitfield like the ":16" here: 5098 * struct { 5099 * int :16; 5100 * int x:8; 5101 * }; 5102 */ 5103 if (off <= moff && 5104 BITS_ROUNDUP_BYTES(end_bit) <= off + size) 5105 return WALK_SCALAR; 5106 5107 /* off may be accessing a following member 5108 * 5109 * or 5110 * 5111 * Doing partial access at either end of this 5112 * bitfield. Continue on this case also to 5113 * treat it as not accessing this bitfield 5114 * and eventually error out as field not 5115 * found to keep it simple. 5116 * It could be relaxed if there was a legit 5117 * partial access case later. 5118 */ 5119 continue; 5120 } 5121 5122 /* In case of "off" is pointing to holes of a struct */ 5123 if (off < moff) 5124 break; 5125 5126 /* type of the field */ 5127 mid = member->type; 5128 mtype = btf_type_by_id(btf, member->type); 5129 mname = __btf_name_by_offset(btf, member->name_off); 5130 5131 mtype = __btf_resolve_size(btf, mtype, &msize, 5132 &elem_type, &elem_id, &total_nelems, 5133 &mid); 5134 if (IS_ERR(mtype)) { 5135 bpf_log(log, "field %s doesn't have size\n", mname); 5136 return -EFAULT; 5137 } 5138 5139 mtrue_end = moff + msize; 5140 if (off >= mtrue_end) 5141 /* no overlap with member, keep iterating */ 5142 continue; 5143 5144 if (btf_type_is_array(mtype)) { 5145 u32 elem_idx; 5146 5147 /* __btf_resolve_size() above helps to 5148 * linearize a multi-dimensional array. 5149 * 5150 * The logic here is treating an array 5151 * in a struct as the following way: 5152 * 5153 * struct outer { 5154 * struct inner array[2][2]; 5155 * }; 5156 * 5157 * looks like: 5158 * 5159 * struct outer { 5160 * struct inner array_elem0; 5161 * struct inner array_elem1; 5162 * struct inner array_elem2; 5163 * struct inner array_elem3; 5164 * }; 5165 * 5166 * When accessing outer->array[1][0], it moves 5167 * moff to "array_elem2", set mtype to 5168 * "struct inner", and msize also becomes 5169 * sizeof(struct inner). Then most of the 5170 * remaining logic will fall through without 5171 * caring the current member is an array or 5172 * not. 5173 * 5174 * Unlike mtype/msize/moff, mtrue_end does not 5175 * change. The naming difference ("_true") tells 5176 * that it is not always corresponding to 5177 * the current mtype/msize/moff. 5178 * It is the true end of the current 5179 * member (i.e. array in this case). That 5180 * will allow an int array to be accessed like 5181 * a scratch space, 5182 * i.e. allow access beyond the size of 5183 * the array's element as long as it is 5184 * within the mtrue_end boundary. 5185 */ 5186 5187 /* skip empty array */ 5188 if (moff == mtrue_end) 5189 continue; 5190 5191 msize /= total_nelems; 5192 elem_idx = (off - moff) / msize; 5193 moff += elem_idx * msize; 5194 mtype = elem_type; 5195 mid = elem_id; 5196 } 5197 5198 /* the 'off' we're looking for is either equal to start 5199 * of this field or inside of this struct 5200 */ 5201 if (btf_type_is_struct(mtype)) { 5202 /* our field must be inside that union or struct */ 5203 t = mtype; 5204 5205 /* return if the offset matches the member offset */ 5206 if (off == moff) { 5207 *next_btf_id = mid; 5208 return WALK_STRUCT; 5209 } 5210 5211 /* adjust offset we're looking for */ 5212 off -= moff; 5213 goto again; 5214 } 5215 5216 if (btf_type_is_ptr(mtype)) { 5217 const struct btf_type *stype; 5218 u32 id; 5219 5220 if (msize != size || off != moff) { 5221 bpf_log(log, 5222 "cannot access ptr member %s with moff %u in struct %s with off %u size %u\n", 5223 mname, moff, tname, off, size); 5224 return -EACCES; 5225 } 5226 stype = btf_type_skip_modifiers(btf, mtype->type, &id); 5227 if (btf_type_is_struct(stype)) { 5228 *next_btf_id = id; 5229 return WALK_PTR; 5230 } 5231 } 5232 5233 /* Allow more flexible access within an int as long as 5234 * it is within mtrue_end. 5235 * Since mtrue_end could be the end of an array, 5236 * that also allows using an array of int as a scratch 5237 * space. e.g. skb->cb[]. 5238 */ 5239 if (off + size > mtrue_end) { 5240 bpf_log(log, 5241 "access beyond the end of member %s (mend:%u) in struct %s with off %u size %u\n", 5242 mname, mtrue_end, tname, off, size); 5243 return -EACCES; 5244 } 5245 5246 return WALK_SCALAR; 5247 } 5248 bpf_log(log, "struct %s doesn't have field at offset %d\n", tname, off); 5249 return -EINVAL; 5250 } 5251 5252 int btf_struct_access(struct bpf_verifier_log *log, const struct btf *btf, 5253 const struct btf_type *t, int off, int size, 5254 enum bpf_access_type atype __maybe_unused, 5255 u32 *next_btf_id) 5256 { 5257 int err; 5258 u32 id; 5259 5260 do { 5261 err = btf_struct_walk(log, btf, t, off, size, &id); 5262 5263 switch (err) { 5264 case WALK_PTR: 5265 /* If we found the pointer or scalar on t+off, 5266 * we're done. 5267 */ 5268 *next_btf_id = id; 5269 return PTR_TO_BTF_ID; 5270 case WALK_SCALAR: 5271 return SCALAR_VALUE; 5272 case WALK_STRUCT: 5273 /* We found nested struct, so continue the search 5274 * by diving in it. At this point the offset is 5275 * aligned with the new type, so set it to 0. 5276 */ 5277 t = btf_type_by_id(btf, id); 5278 off = 0; 5279 break; 5280 default: 5281 /* It's either error or unknown return value.. 5282 * scream and leave. 5283 */ 5284 if (WARN_ONCE(err > 0, "unknown btf_struct_walk return value")) 5285 return -EINVAL; 5286 return err; 5287 } 5288 } while (t); 5289 5290 return -EINVAL; 5291 } 5292 5293 /* Check that two BTF types, each specified as an BTF object + id, are exactly 5294 * the same. Trivial ID check is not enough due to module BTFs, because we can 5295 * end up with two different module BTFs, but IDs point to the common type in 5296 * vmlinux BTF. 5297 */ 5298 static bool btf_types_are_same(const struct btf *btf1, u32 id1, 5299 const struct btf *btf2, u32 id2) 5300 { 5301 if (id1 != id2) 5302 return false; 5303 if (btf1 == btf2) 5304 return true; 5305 return btf_type_by_id(btf1, id1) == btf_type_by_id(btf2, id2); 5306 } 5307 5308 bool btf_struct_ids_match(struct bpf_verifier_log *log, 5309 const struct btf *btf, u32 id, int off, 5310 const struct btf *need_btf, u32 need_type_id) 5311 { 5312 const struct btf_type *type; 5313 int err; 5314 5315 /* Are we already done? */ 5316 if (off == 0 && btf_types_are_same(btf, id, need_btf, need_type_id)) 5317 return true; 5318 5319 again: 5320 type = btf_type_by_id(btf, id); 5321 if (!type) 5322 return false; 5323 err = btf_struct_walk(log, btf, type, off, 1, &id); 5324 if (err != WALK_STRUCT) 5325 return false; 5326 5327 /* We found nested struct object. If it matches 5328 * the requested ID, we're done. Otherwise let's 5329 * continue the search with offset 0 in the new 5330 * type. 5331 */ 5332 if (!btf_types_are_same(btf, id, need_btf, need_type_id)) { 5333 off = 0; 5334 goto again; 5335 } 5336 5337 return true; 5338 } 5339 5340 static int __get_type_size(struct btf *btf, u32 btf_id, 5341 const struct btf_type **bad_type) 5342 { 5343 const struct btf_type *t; 5344 5345 if (!btf_id) 5346 /* void */ 5347 return 0; 5348 t = btf_type_by_id(btf, btf_id); 5349 while (t && btf_type_is_modifier(t)) 5350 t = btf_type_by_id(btf, t->type); 5351 if (!t) { 5352 *bad_type = btf_type_by_id(btf, 0); 5353 return -EINVAL; 5354 } 5355 if (btf_type_is_ptr(t)) 5356 /* kernel size of pointer. Not BPF's size of pointer*/ 5357 return sizeof(void *); 5358 if (btf_type_is_int(t) || btf_type_is_enum(t)) 5359 return t->size; 5360 *bad_type = t; 5361 return -EINVAL; 5362 } 5363 5364 int btf_distill_func_proto(struct bpf_verifier_log *log, 5365 struct btf *btf, 5366 const struct btf_type *func, 5367 const char *tname, 5368 struct btf_func_model *m) 5369 { 5370 const struct btf_param *args; 5371 const struct btf_type *t; 5372 u32 i, nargs; 5373 int ret; 5374 5375 if (!func) { 5376 /* BTF function prototype doesn't match the verifier types. 5377 * Fall back to MAX_BPF_FUNC_REG_ARGS u64 args. 5378 */ 5379 for (i = 0; i < MAX_BPF_FUNC_REG_ARGS; i++) 5380 m->arg_size[i] = 8; 5381 m->ret_size = 8; 5382 m->nr_args = MAX_BPF_FUNC_REG_ARGS; 5383 return 0; 5384 } 5385 args = (const struct btf_param *)(func + 1); 5386 nargs = btf_type_vlen(func); 5387 if (nargs >= MAX_BPF_FUNC_ARGS) { 5388 bpf_log(log, 5389 "The function %s has %d arguments. Too many.\n", 5390 tname, nargs); 5391 return -EINVAL; 5392 } 5393 ret = __get_type_size(btf, func->type, &t); 5394 if (ret < 0) { 5395 bpf_log(log, 5396 "The function %s return type %s is unsupported.\n", 5397 tname, btf_kind_str[BTF_INFO_KIND(t->info)]); 5398 return -EINVAL; 5399 } 5400 m->ret_size = ret; 5401 5402 for (i = 0; i < nargs; i++) { 5403 if (i == nargs - 1 && args[i].type == 0) { 5404 bpf_log(log, 5405 "The function %s with variable args is unsupported.\n", 5406 tname); 5407 return -EINVAL; 5408 } 5409 ret = __get_type_size(btf, args[i].type, &t); 5410 if (ret < 0) { 5411 bpf_log(log, 5412 "The function %s arg%d type %s is unsupported.\n", 5413 tname, i, btf_kind_str[BTF_INFO_KIND(t->info)]); 5414 return -EINVAL; 5415 } 5416 if (ret == 0) { 5417 bpf_log(log, 5418 "The function %s has malformed void argument.\n", 5419 tname); 5420 return -EINVAL; 5421 } 5422 m->arg_size[i] = ret; 5423 } 5424 m->nr_args = nargs; 5425 return 0; 5426 } 5427 5428 /* Compare BTFs of two functions assuming only scalars and pointers to context. 5429 * t1 points to BTF_KIND_FUNC in btf1 5430 * t2 points to BTF_KIND_FUNC in btf2 5431 * Returns: 5432 * EINVAL - function prototype mismatch 5433 * EFAULT - verifier bug 5434 * 0 - 99% match. The last 1% is validated by the verifier. 5435 */ 5436 static int btf_check_func_type_match(struct bpf_verifier_log *log, 5437 struct btf *btf1, const struct btf_type *t1, 5438 struct btf *btf2, const struct btf_type *t2) 5439 { 5440 const struct btf_param *args1, *args2; 5441 const char *fn1, *fn2, *s1, *s2; 5442 u32 nargs1, nargs2, i; 5443 5444 fn1 = btf_name_by_offset(btf1, t1->name_off); 5445 fn2 = btf_name_by_offset(btf2, t2->name_off); 5446 5447 if (btf_func_linkage(t1) != BTF_FUNC_GLOBAL) { 5448 bpf_log(log, "%s() is not a global function\n", fn1); 5449 return -EINVAL; 5450 } 5451 if (btf_func_linkage(t2) != BTF_FUNC_GLOBAL) { 5452 bpf_log(log, "%s() is not a global function\n", fn2); 5453 return -EINVAL; 5454 } 5455 5456 t1 = btf_type_by_id(btf1, t1->type); 5457 if (!t1 || !btf_type_is_func_proto(t1)) 5458 return -EFAULT; 5459 t2 = btf_type_by_id(btf2, t2->type); 5460 if (!t2 || !btf_type_is_func_proto(t2)) 5461 return -EFAULT; 5462 5463 args1 = (const struct btf_param *)(t1 + 1); 5464 nargs1 = btf_type_vlen(t1); 5465 args2 = (const struct btf_param *)(t2 + 1); 5466 nargs2 = btf_type_vlen(t2); 5467 5468 if (nargs1 != nargs2) { 5469 bpf_log(log, "%s() has %d args while %s() has %d args\n", 5470 fn1, nargs1, fn2, nargs2); 5471 return -EINVAL; 5472 } 5473 5474 t1 = btf_type_skip_modifiers(btf1, t1->type, NULL); 5475 t2 = btf_type_skip_modifiers(btf2, t2->type, NULL); 5476 if (t1->info != t2->info) { 5477 bpf_log(log, 5478 "Return type %s of %s() doesn't match type %s of %s()\n", 5479 btf_type_str(t1), fn1, 5480 btf_type_str(t2), fn2); 5481 return -EINVAL; 5482 } 5483 5484 for (i = 0; i < nargs1; i++) { 5485 t1 = btf_type_skip_modifiers(btf1, args1[i].type, NULL); 5486 t2 = btf_type_skip_modifiers(btf2, args2[i].type, NULL); 5487 5488 if (t1->info != t2->info) { 5489 bpf_log(log, "arg%d in %s() is %s while %s() has %s\n", 5490 i, fn1, btf_type_str(t1), 5491 fn2, btf_type_str(t2)); 5492 return -EINVAL; 5493 } 5494 if (btf_type_has_size(t1) && t1->size != t2->size) { 5495 bpf_log(log, 5496 "arg%d in %s() has size %d while %s() has %d\n", 5497 i, fn1, t1->size, 5498 fn2, t2->size); 5499 return -EINVAL; 5500 } 5501 5502 /* global functions are validated with scalars and pointers 5503 * to context only. And only global functions can be replaced. 5504 * Hence type check only those types. 5505 */ 5506 if (btf_type_is_int(t1) || btf_type_is_enum(t1)) 5507 continue; 5508 if (!btf_type_is_ptr(t1)) { 5509 bpf_log(log, 5510 "arg%d in %s() has unrecognized type\n", 5511 i, fn1); 5512 return -EINVAL; 5513 } 5514 t1 = btf_type_skip_modifiers(btf1, t1->type, NULL); 5515 t2 = btf_type_skip_modifiers(btf2, t2->type, NULL); 5516 if (!btf_type_is_struct(t1)) { 5517 bpf_log(log, 5518 "arg%d in %s() is not a pointer to context\n", 5519 i, fn1); 5520 return -EINVAL; 5521 } 5522 if (!btf_type_is_struct(t2)) { 5523 bpf_log(log, 5524 "arg%d in %s() is not a pointer to context\n", 5525 i, fn2); 5526 return -EINVAL; 5527 } 5528 /* This is an optional check to make program writing easier. 5529 * Compare names of structs and report an error to the user. 5530 * btf_prepare_func_args() already checked that t2 struct 5531 * is a context type. btf_prepare_func_args() will check 5532 * later that t1 struct is a context type as well. 5533 */ 5534 s1 = btf_name_by_offset(btf1, t1->name_off); 5535 s2 = btf_name_by_offset(btf2, t2->name_off); 5536 if (strcmp(s1, s2)) { 5537 bpf_log(log, 5538 "arg%d %s(struct %s *) doesn't match %s(struct %s *)\n", 5539 i, fn1, s1, fn2, s2); 5540 return -EINVAL; 5541 } 5542 } 5543 return 0; 5544 } 5545 5546 /* Compare BTFs of given program with BTF of target program */ 5547 int btf_check_type_match(struct bpf_verifier_log *log, const struct bpf_prog *prog, 5548 struct btf *btf2, const struct btf_type *t2) 5549 { 5550 struct btf *btf1 = prog->aux->btf; 5551 const struct btf_type *t1; 5552 u32 btf_id = 0; 5553 5554 if (!prog->aux->func_info) { 5555 bpf_log(log, "Program extension requires BTF\n"); 5556 return -EINVAL; 5557 } 5558 5559 btf_id = prog->aux->func_info[0].type_id; 5560 if (!btf_id) 5561 return -EFAULT; 5562 5563 t1 = btf_type_by_id(btf1, btf_id); 5564 if (!t1 || !btf_type_is_func(t1)) 5565 return -EFAULT; 5566 5567 return btf_check_func_type_match(log, btf1, t1, btf2, t2); 5568 } 5569 5570 static u32 *reg2btf_ids[__BPF_REG_TYPE_MAX] = { 5571 #ifdef CONFIG_NET 5572 [PTR_TO_SOCKET] = &btf_sock_ids[BTF_SOCK_TYPE_SOCK], 5573 [PTR_TO_SOCK_COMMON] = &btf_sock_ids[BTF_SOCK_TYPE_SOCK_COMMON], 5574 [PTR_TO_TCP_SOCK] = &btf_sock_ids[BTF_SOCK_TYPE_TCP], 5575 #endif 5576 }; 5577 5578 static int btf_check_func_arg_match(struct bpf_verifier_env *env, 5579 const struct btf *btf, u32 func_id, 5580 struct bpf_reg_state *regs, 5581 bool ptr_to_mem_ok) 5582 { 5583 struct bpf_verifier_log *log = &env->log; 5584 const char *func_name, *ref_tname; 5585 const struct btf_type *t, *ref_t; 5586 const struct btf_param *args; 5587 u32 i, nargs, ref_id; 5588 5589 t = btf_type_by_id(btf, func_id); 5590 if (!t || !btf_type_is_func(t)) { 5591 /* These checks were already done by the verifier while loading 5592 * struct bpf_func_info or in add_kfunc_call(). 5593 */ 5594 bpf_log(log, "BTF of func_id %u doesn't point to KIND_FUNC\n", 5595 func_id); 5596 return -EFAULT; 5597 } 5598 func_name = btf_name_by_offset(btf, t->name_off); 5599 5600 t = btf_type_by_id(btf, t->type); 5601 if (!t || !btf_type_is_func_proto(t)) { 5602 bpf_log(log, "Invalid BTF of func %s\n", func_name); 5603 return -EFAULT; 5604 } 5605 args = (const struct btf_param *)(t + 1); 5606 nargs = btf_type_vlen(t); 5607 if (nargs > MAX_BPF_FUNC_REG_ARGS) { 5608 bpf_log(log, "Function %s has %d > %d args\n", func_name, nargs, 5609 MAX_BPF_FUNC_REG_ARGS); 5610 return -EINVAL; 5611 } 5612 5613 /* check that BTF function arguments match actual types that the 5614 * verifier sees. 5615 */ 5616 for (i = 0; i < nargs; i++) { 5617 u32 regno = i + 1; 5618 struct bpf_reg_state *reg = ®s[regno]; 5619 5620 t = btf_type_skip_modifiers(btf, args[i].type, NULL); 5621 if (btf_type_is_scalar(t)) { 5622 if (reg->type == SCALAR_VALUE) 5623 continue; 5624 bpf_log(log, "R%d is not a scalar\n", regno); 5625 return -EINVAL; 5626 } 5627 5628 if (!btf_type_is_ptr(t)) { 5629 bpf_log(log, "Unrecognized arg#%d type %s\n", 5630 i, btf_type_str(t)); 5631 return -EINVAL; 5632 } 5633 5634 ref_t = btf_type_skip_modifiers(btf, t->type, &ref_id); 5635 ref_tname = btf_name_by_offset(btf, ref_t->name_off); 5636 if (btf_is_kernel(btf)) { 5637 const struct btf_type *reg_ref_t; 5638 const struct btf *reg_btf; 5639 const char *reg_ref_tname; 5640 u32 reg_ref_id; 5641 5642 if (!btf_type_is_struct(ref_t)) { 5643 bpf_log(log, "kernel function %s args#%d pointer type %s %s is not supported\n", 5644 func_name, i, btf_type_str(ref_t), 5645 ref_tname); 5646 return -EINVAL; 5647 } 5648 5649 if (reg->type == PTR_TO_BTF_ID) { 5650 reg_btf = reg->btf; 5651 reg_ref_id = reg->btf_id; 5652 } else if (reg2btf_ids[reg->type]) { 5653 reg_btf = btf_vmlinux; 5654 reg_ref_id = *reg2btf_ids[reg->type]; 5655 } else { 5656 bpf_log(log, "kernel function %s args#%d expected pointer to %s %s but R%d is not a pointer to btf_id\n", 5657 func_name, i, 5658 btf_type_str(ref_t), ref_tname, regno); 5659 return -EINVAL; 5660 } 5661 5662 reg_ref_t = btf_type_skip_modifiers(reg_btf, reg_ref_id, 5663 ®_ref_id); 5664 reg_ref_tname = btf_name_by_offset(reg_btf, 5665 reg_ref_t->name_off); 5666 if (!btf_struct_ids_match(log, reg_btf, reg_ref_id, 5667 reg->off, btf, ref_id)) { 5668 bpf_log(log, "kernel function %s args#%d expected pointer to %s %s but R%d has a pointer to %s %s\n", 5669 func_name, i, 5670 btf_type_str(ref_t), ref_tname, 5671 regno, btf_type_str(reg_ref_t), 5672 reg_ref_tname); 5673 return -EINVAL; 5674 } 5675 } else if (btf_get_prog_ctx_type(log, btf, t, 5676 env->prog->type, i)) { 5677 /* If function expects ctx type in BTF check that caller 5678 * is passing PTR_TO_CTX. 5679 */ 5680 if (reg->type != PTR_TO_CTX) { 5681 bpf_log(log, 5682 "arg#%d expected pointer to ctx, but got %s\n", 5683 i, btf_type_str(t)); 5684 return -EINVAL; 5685 } 5686 if (check_ctx_reg(env, reg, regno)) 5687 return -EINVAL; 5688 } else if (ptr_to_mem_ok) { 5689 const struct btf_type *resolve_ret; 5690 u32 type_size; 5691 5692 resolve_ret = btf_resolve_size(btf, ref_t, &type_size); 5693 if (IS_ERR(resolve_ret)) { 5694 bpf_log(log, 5695 "arg#%d reference type('%s %s') size cannot be determined: %ld\n", 5696 i, btf_type_str(ref_t), ref_tname, 5697 PTR_ERR(resolve_ret)); 5698 return -EINVAL; 5699 } 5700 5701 if (check_mem_reg(env, reg, regno, type_size)) 5702 return -EINVAL; 5703 } else { 5704 return -EINVAL; 5705 } 5706 } 5707 5708 return 0; 5709 } 5710 5711 /* Compare BTF of a function with given bpf_reg_state. 5712 * Returns: 5713 * EFAULT - there is a verifier bug. Abort verification. 5714 * EINVAL - there is a type mismatch or BTF is not available. 5715 * 0 - BTF matches with what bpf_reg_state expects. 5716 * Only PTR_TO_CTX and SCALAR_VALUE states are recognized. 5717 */ 5718 int btf_check_subprog_arg_match(struct bpf_verifier_env *env, int subprog, 5719 struct bpf_reg_state *regs) 5720 { 5721 struct bpf_prog *prog = env->prog; 5722 struct btf *btf = prog->aux->btf; 5723 bool is_global; 5724 u32 btf_id; 5725 int err; 5726 5727 if (!prog->aux->func_info) 5728 return -EINVAL; 5729 5730 btf_id = prog->aux->func_info[subprog].type_id; 5731 if (!btf_id) 5732 return -EFAULT; 5733 5734 if (prog->aux->func_info_aux[subprog].unreliable) 5735 return -EINVAL; 5736 5737 is_global = prog->aux->func_info_aux[subprog].linkage == BTF_FUNC_GLOBAL; 5738 err = btf_check_func_arg_match(env, btf, btf_id, regs, is_global); 5739 5740 /* Compiler optimizations can remove arguments from static functions 5741 * or mismatched type can be passed into a global function. 5742 * In such cases mark the function as unreliable from BTF point of view. 5743 */ 5744 if (err) 5745 prog->aux->func_info_aux[subprog].unreliable = true; 5746 return err; 5747 } 5748 5749 int btf_check_kfunc_arg_match(struct bpf_verifier_env *env, 5750 const struct btf *btf, u32 func_id, 5751 struct bpf_reg_state *regs) 5752 { 5753 return btf_check_func_arg_match(env, btf, func_id, regs, false); 5754 } 5755 5756 /* Convert BTF of a function into bpf_reg_state if possible 5757 * Returns: 5758 * EFAULT - there is a verifier bug. Abort verification. 5759 * EINVAL - cannot convert BTF. 5760 * 0 - Successfully converted BTF into bpf_reg_state 5761 * (either PTR_TO_CTX or SCALAR_VALUE). 5762 */ 5763 int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog, 5764 struct bpf_reg_state *regs) 5765 { 5766 struct bpf_verifier_log *log = &env->log; 5767 struct bpf_prog *prog = env->prog; 5768 enum bpf_prog_type prog_type = prog->type; 5769 struct btf *btf = prog->aux->btf; 5770 const struct btf_param *args; 5771 const struct btf_type *t, *ref_t; 5772 u32 i, nargs, btf_id; 5773 const char *tname; 5774 5775 if (!prog->aux->func_info || 5776 prog->aux->func_info_aux[subprog].linkage != BTF_FUNC_GLOBAL) { 5777 bpf_log(log, "Verifier bug\n"); 5778 return -EFAULT; 5779 } 5780 5781 btf_id = prog->aux->func_info[subprog].type_id; 5782 if (!btf_id) { 5783 bpf_log(log, "Global functions need valid BTF\n"); 5784 return -EFAULT; 5785 } 5786 5787 t = btf_type_by_id(btf, btf_id); 5788 if (!t || !btf_type_is_func(t)) { 5789 /* These checks were already done by the verifier while loading 5790 * struct bpf_func_info 5791 */ 5792 bpf_log(log, "BTF of func#%d doesn't point to KIND_FUNC\n", 5793 subprog); 5794 return -EFAULT; 5795 } 5796 tname = btf_name_by_offset(btf, t->name_off); 5797 5798 if (log->level & BPF_LOG_LEVEL) 5799 bpf_log(log, "Validating %s() func#%d...\n", 5800 tname, subprog); 5801 5802 if (prog->aux->func_info_aux[subprog].unreliable) { 5803 bpf_log(log, "Verifier bug in function %s()\n", tname); 5804 return -EFAULT; 5805 } 5806 if (prog_type == BPF_PROG_TYPE_EXT) 5807 prog_type = prog->aux->dst_prog->type; 5808 5809 t = btf_type_by_id(btf, t->type); 5810 if (!t || !btf_type_is_func_proto(t)) { 5811 bpf_log(log, "Invalid type of function %s()\n", tname); 5812 return -EFAULT; 5813 } 5814 args = (const struct btf_param *)(t + 1); 5815 nargs = btf_type_vlen(t); 5816 if (nargs > MAX_BPF_FUNC_REG_ARGS) { 5817 bpf_log(log, "Global function %s() with %d > %d args. Buggy compiler.\n", 5818 tname, nargs, MAX_BPF_FUNC_REG_ARGS); 5819 return -EINVAL; 5820 } 5821 /* check that function returns int */ 5822 t = btf_type_by_id(btf, t->type); 5823 while (btf_type_is_modifier(t)) 5824 t = btf_type_by_id(btf, t->type); 5825 if (!btf_type_is_int(t) && !btf_type_is_enum(t)) { 5826 bpf_log(log, 5827 "Global function %s() doesn't return scalar. Only those are supported.\n", 5828 tname); 5829 return -EINVAL; 5830 } 5831 /* Convert BTF function arguments into verifier types. 5832 * Only PTR_TO_CTX and SCALAR are supported atm. 5833 */ 5834 for (i = 0; i < nargs; i++) { 5835 struct bpf_reg_state *reg = ®s[i + 1]; 5836 5837 t = btf_type_by_id(btf, args[i].type); 5838 while (btf_type_is_modifier(t)) 5839 t = btf_type_by_id(btf, t->type); 5840 if (btf_type_is_int(t) || btf_type_is_enum(t)) { 5841 reg->type = SCALAR_VALUE; 5842 continue; 5843 } 5844 if (btf_type_is_ptr(t)) { 5845 if (btf_get_prog_ctx_type(log, btf, t, prog_type, i)) { 5846 reg->type = PTR_TO_CTX; 5847 continue; 5848 } 5849 5850 t = btf_type_skip_modifiers(btf, t->type, NULL); 5851 5852 ref_t = btf_resolve_size(btf, t, ®->mem_size); 5853 if (IS_ERR(ref_t)) { 5854 bpf_log(log, 5855 "arg#%d reference type('%s %s') size cannot be determined: %ld\n", 5856 i, btf_type_str(t), btf_name_by_offset(btf, t->name_off), 5857 PTR_ERR(ref_t)); 5858 return -EINVAL; 5859 } 5860 5861 reg->type = PTR_TO_MEM_OR_NULL; 5862 reg->id = ++env->id_gen; 5863 5864 continue; 5865 } 5866 bpf_log(log, "Arg#%d type %s in %s() is not supported yet.\n", 5867 i, btf_kind_str[BTF_INFO_KIND(t->info)], tname); 5868 return -EINVAL; 5869 } 5870 return 0; 5871 } 5872 5873 static void btf_type_show(const struct btf *btf, u32 type_id, void *obj, 5874 struct btf_show *show) 5875 { 5876 const struct btf_type *t = btf_type_by_id(btf, type_id); 5877 5878 show->btf = btf; 5879 memset(&show->state, 0, sizeof(show->state)); 5880 memset(&show->obj, 0, sizeof(show->obj)); 5881 5882 btf_type_ops(t)->show(btf, t, type_id, obj, 0, show); 5883 } 5884 5885 static void btf_seq_show(struct btf_show *show, const char *fmt, 5886 va_list args) 5887 { 5888 seq_vprintf((struct seq_file *)show->target, fmt, args); 5889 } 5890 5891 int btf_type_seq_show_flags(const struct btf *btf, u32 type_id, 5892 void *obj, struct seq_file *m, u64 flags) 5893 { 5894 struct btf_show sseq; 5895 5896 sseq.target = m; 5897 sseq.showfn = btf_seq_show; 5898 sseq.flags = flags; 5899 5900 btf_type_show(btf, type_id, obj, &sseq); 5901 5902 return sseq.state.status; 5903 } 5904 5905 void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj, 5906 struct seq_file *m) 5907 { 5908 (void) btf_type_seq_show_flags(btf, type_id, obj, m, 5909 BTF_SHOW_NONAME | BTF_SHOW_COMPACT | 5910 BTF_SHOW_ZERO | BTF_SHOW_UNSAFE); 5911 } 5912 5913 struct btf_show_snprintf { 5914 struct btf_show show; 5915 int len_left; /* space left in string */ 5916 int len; /* length we would have written */ 5917 }; 5918 5919 static void btf_snprintf_show(struct btf_show *show, const char *fmt, 5920 va_list args) 5921 { 5922 struct btf_show_snprintf *ssnprintf = (struct btf_show_snprintf *)show; 5923 int len; 5924 5925 len = vsnprintf(show->target, ssnprintf->len_left, fmt, args); 5926 5927 if (len < 0) { 5928 ssnprintf->len_left = 0; 5929 ssnprintf->len = len; 5930 } else if (len > ssnprintf->len_left) { 5931 /* no space, drive on to get length we would have written */ 5932 ssnprintf->len_left = 0; 5933 ssnprintf->len += len; 5934 } else { 5935 ssnprintf->len_left -= len; 5936 ssnprintf->len += len; 5937 show->target += len; 5938 } 5939 } 5940 5941 int btf_type_snprintf_show(const struct btf *btf, u32 type_id, void *obj, 5942 char *buf, int len, u64 flags) 5943 { 5944 struct btf_show_snprintf ssnprintf; 5945 5946 ssnprintf.show.target = buf; 5947 ssnprintf.show.flags = flags; 5948 ssnprintf.show.showfn = btf_snprintf_show; 5949 ssnprintf.len_left = len; 5950 ssnprintf.len = 0; 5951 5952 btf_type_show(btf, type_id, obj, (struct btf_show *)&ssnprintf); 5953 5954 /* If we encontered an error, return it. */ 5955 if (ssnprintf.show.state.status) 5956 return ssnprintf.show.state.status; 5957 5958 /* Otherwise return length we would have written */ 5959 return ssnprintf.len; 5960 } 5961 5962 #ifdef CONFIG_PROC_FS 5963 static void bpf_btf_show_fdinfo(struct seq_file *m, struct file *filp) 5964 { 5965 const struct btf *btf = filp->private_data; 5966 5967 seq_printf(m, "btf_id:\t%u\n", btf->id); 5968 } 5969 #endif 5970 5971 static int btf_release(struct inode *inode, struct file *filp) 5972 { 5973 btf_put(filp->private_data); 5974 return 0; 5975 } 5976 5977 const struct file_operations btf_fops = { 5978 #ifdef CONFIG_PROC_FS 5979 .show_fdinfo = bpf_btf_show_fdinfo, 5980 #endif 5981 .release = btf_release, 5982 }; 5983 5984 static int __btf_new_fd(struct btf *btf) 5985 { 5986 return anon_inode_getfd("btf", &btf_fops, btf, O_RDONLY | O_CLOEXEC); 5987 } 5988 5989 int btf_new_fd(const union bpf_attr *attr, bpfptr_t uattr) 5990 { 5991 struct btf *btf; 5992 int ret; 5993 5994 btf = btf_parse(make_bpfptr(attr->btf, uattr.is_kernel), 5995 attr->btf_size, attr->btf_log_level, 5996 u64_to_user_ptr(attr->btf_log_buf), 5997 attr->btf_log_size); 5998 if (IS_ERR(btf)) 5999 return PTR_ERR(btf); 6000 6001 ret = btf_alloc_id(btf); 6002 if (ret) { 6003 btf_free(btf); 6004 return ret; 6005 } 6006 6007 /* 6008 * The BTF ID is published to the userspace. 6009 * All BTF free must go through call_rcu() from 6010 * now on (i.e. free by calling btf_put()). 6011 */ 6012 6013 ret = __btf_new_fd(btf); 6014 if (ret < 0) 6015 btf_put(btf); 6016 6017 return ret; 6018 } 6019 6020 struct btf *btf_get_by_fd(int fd) 6021 { 6022 struct btf *btf; 6023 struct fd f; 6024 6025 f = fdget(fd); 6026 6027 if (!f.file) 6028 return ERR_PTR(-EBADF); 6029 6030 if (f.file->f_op != &btf_fops) { 6031 fdput(f); 6032 return ERR_PTR(-EINVAL); 6033 } 6034 6035 btf = f.file->private_data; 6036 refcount_inc(&btf->refcnt); 6037 fdput(f); 6038 6039 return btf; 6040 } 6041 6042 int btf_get_info_by_fd(const struct btf *btf, 6043 const union bpf_attr *attr, 6044 union bpf_attr __user *uattr) 6045 { 6046 struct bpf_btf_info __user *uinfo; 6047 struct bpf_btf_info info; 6048 u32 info_copy, btf_copy; 6049 void __user *ubtf; 6050 char __user *uname; 6051 u32 uinfo_len, uname_len, name_len; 6052 int ret = 0; 6053 6054 uinfo = u64_to_user_ptr(attr->info.info); 6055 uinfo_len = attr->info.info_len; 6056 6057 info_copy = min_t(u32, uinfo_len, sizeof(info)); 6058 memset(&info, 0, sizeof(info)); 6059 if (copy_from_user(&info, uinfo, info_copy)) 6060 return -EFAULT; 6061 6062 info.id = btf->id; 6063 ubtf = u64_to_user_ptr(info.btf); 6064 btf_copy = min_t(u32, btf->data_size, info.btf_size); 6065 if (copy_to_user(ubtf, btf->data, btf_copy)) 6066 return -EFAULT; 6067 info.btf_size = btf->data_size; 6068 6069 info.kernel_btf = btf->kernel_btf; 6070 6071 uname = u64_to_user_ptr(info.name); 6072 uname_len = info.name_len; 6073 if (!uname ^ !uname_len) 6074 return -EINVAL; 6075 6076 name_len = strlen(btf->name); 6077 info.name_len = name_len; 6078 6079 if (uname) { 6080 if (uname_len >= name_len + 1) { 6081 if (copy_to_user(uname, btf->name, name_len + 1)) 6082 return -EFAULT; 6083 } else { 6084 char zero = '\0'; 6085 6086 if (copy_to_user(uname, btf->name, uname_len - 1)) 6087 return -EFAULT; 6088 if (put_user(zero, uname + uname_len - 1)) 6089 return -EFAULT; 6090 /* let user-space know about too short buffer */ 6091 ret = -ENOSPC; 6092 } 6093 } 6094 6095 if (copy_to_user(uinfo, &info, info_copy) || 6096 put_user(info_copy, &uattr->info.info_len)) 6097 return -EFAULT; 6098 6099 return ret; 6100 } 6101 6102 int btf_get_fd_by_id(u32 id) 6103 { 6104 struct btf *btf; 6105 int fd; 6106 6107 rcu_read_lock(); 6108 btf = idr_find(&btf_idr, id); 6109 if (!btf || !refcount_inc_not_zero(&btf->refcnt)) 6110 btf = ERR_PTR(-ENOENT); 6111 rcu_read_unlock(); 6112 6113 if (IS_ERR(btf)) 6114 return PTR_ERR(btf); 6115 6116 fd = __btf_new_fd(btf); 6117 if (fd < 0) 6118 btf_put(btf); 6119 6120 return fd; 6121 } 6122 6123 u32 btf_obj_id(const struct btf *btf) 6124 { 6125 return btf->id; 6126 } 6127 6128 bool btf_is_kernel(const struct btf *btf) 6129 { 6130 return btf->kernel_btf; 6131 } 6132 6133 bool btf_is_module(const struct btf *btf) 6134 { 6135 return btf->kernel_btf && strcmp(btf->name, "vmlinux") != 0; 6136 } 6137 6138 static int btf_id_cmp_func(const void *a, const void *b) 6139 { 6140 const int *pa = a, *pb = b; 6141 6142 return *pa - *pb; 6143 } 6144 6145 bool btf_id_set_contains(const struct btf_id_set *set, u32 id) 6146 { 6147 return bsearch(&id, set->ids, set->cnt, sizeof(u32), btf_id_cmp_func) != NULL; 6148 } 6149 6150 #ifdef CONFIG_DEBUG_INFO_BTF_MODULES 6151 struct btf_module { 6152 struct list_head list; 6153 struct module *module; 6154 struct btf *btf; 6155 struct bin_attribute *sysfs_attr; 6156 }; 6157 6158 static LIST_HEAD(btf_modules); 6159 static DEFINE_MUTEX(btf_module_mutex); 6160 6161 static ssize_t 6162 btf_module_read(struct file *file, struct kobject *kobj, 6163 struct bin_attribute *bin_attr, 6164 char *buf, loff_t off, size_t len) 6165 { 6166 const struct btf *btf = bin_attr->private; 6167 6168 memcpy(buf, btf->data + off, len); 6169 return len; 6170 } 6171 6172 static int btf_module_notify(struct notifier_block *nb, unsigned long op, 6173 void *module) 6174 { 6175 struct btf_module *btf_mod, *tmp; 6176 struct module *mod = module; 6177 struct btf *btf; 6178 int err = 0; 6179 6180 if (mod->btf_data_size == 0 || 6181 (op != MODULE_STATE_COMING && op != MODULE_STATE_GOING)) 6182 goto out; 6183 6184 switch (op) { 6185 case MODULE_STATE_COMING: 6186 btf_mod = kzalloc(sizeof(*btf_mod), GFP_KERNEL); 6187 if (!btf_mod) { 6188 err = -ENOMEM; 6189 goto out; 6190 } 6191 btf = btf_parse_module(mod->name, mod->btf_data, mod->btf_data_size); 6192 if (IS_ERR(btf)) { 6193 pr_warn("failed to validate module [%s] BTF: %ld\n", 6194 mod->name, PTR_ERR(btf)); 6195 kfree(btf_mod); 6196 err = PTR_ERR(btf); 6197 goto out; 6198 } 6199 err = btf_alloc_id(btf); 6200 if (err) { 6201 btf_free(btf); 6202 kfree(btf_mod); 6203 goto out; 6204 } 6205 6206 mutex_lock(&btf_module_mutex); 6207 btf_mod->module = module; 6208 btf_mod->btf = btf; 6209 list_add(&btf_mod->list, &btf_modules); 6210 mutex_unlock(&btf_module_mutex); 6211 6212 if (IS_ENABLED(CONFIG_SYSFS)) { 6213 struct bin_attribute *attr; 6214 6215 attr = kzalloc(sizeof(*attr), GFP_KERNEL); 6216 if (!attr) 6217 goto out; 6218 6219 sysfs_bin_attr_init(attr); 6220 attr->attr.name = btf->name; 6221 attr->attr.mode = 0444; 6222 attr->size = btf->data_size; 6223 attr->private = btf; 6224 attr->read = btf_module_read; 6225 6226 err = sysfs_create_bin_file(btf_kobj, attr); 6227 if (err) { 6228 pr_warn("failed to register module [%s] BTF in sysfs: %d\n", 6229 mod->name, err); 6230 kfree(attr); 6231 err = 0; 6232 goto out; 6233 } 6234 6235 btf_mod->sysfs_attr = attr; 6236 } 6237 6238 break; 6239 case MODULE_STATE_GOING: 6240 mutex_lock(&btf_module_mutex); 6241 list_for_each_entry_safe(btf_mod, tmp, &btf_modules, list) { 6242 if (btf_mod->module != module) 6243 continue; 6244 6245 list_del(&btf_mod->list); 6246 if (btf_mod->sysfs_attr) 6247 sysfs_remove_bin_file(btf_kobj, btf_mod->sysfs_attr); 6248 btf_put(btf_mod->btf); 6249 kfree(btf_mod->sysfs_attr); 6250 kfree(btf_mod); 6251 break; 6252 } 6253 mutex_unlock(&btf_module_mutex); 6254 break; 6255 } 6256 out: 6257 return notifier_from_errno(err); 6258 } 6259 6260 static struct notifier_block btf_module_nb = { 6261 .notifier_call = btf_module_notify, 6262 }; 6263 6264 static int __init btf_module_init(void) 6265 { 6266 register_module_notifier(&btf_module_nb); 6267 return 0; 6268 } 6269 6270 fs_initcall(btf_module_init); 6271 #endif /* CONFIG_DEBUG_INFO_BTF_MODULES */ 6272 6273 struct module *btf_try_get_module(const struct btf *btf) 6274 { 6275 struct module *res = NULL; 6276 #ifdef CONFIG_DEBUG_INFO_BTF_MODULES 6277 struct btf_module *btf_mod, *tmp; 6278 6279 mutex_lock(&btf_module_mutex); 6280 list_for_each_entry_safe(btf_mod, tmp, &btf_modules, list) { 6281 if (btf_mod->btf != btf) 6282 continue; 6283 6284 if (try_module_get(btf_mod->module)) 6285 res = btf_mod->module; 6286 6287 break; 6288 } 6289 mutex_unlock(&btf_module_mutex); 6290 #endif 6291 6292 return res; 6293 } 6294 6295 BPF_CALL_4(bpf_btf_find_by_name_kind, char *, name, int, name_sz, u32, kind, int, flags) 6296 { 6297 struct btf *btf; 6298 long ret; 6299 6300 if (flags) 6301 return -EINVAL; 6302 6303 if (name_sz <= 1 || name[name_sz - 1]) 6304 return -EINVAL; 6305 6306 btf = bpf_get_btf_vmlinux(); 6307 if (IS_ERR(btf)) 6308 return PTR_ERR(btf); 6309 6310 ret = btf_find_by_name_kind(btf, name, kind); 6311 /* ret is never zero, since btf_find_by_name_kind returns 6312 * positive btf_id or negative error. 6313 */ 6314 if (ret < 0) { 6315 struct btf *mod_btf; 6316 int id; 6317 6318 /* If name is not found in vmlinux's BTF then search in module's BTFs */ 6319 spin_lock_bh(&btf_idr_lock); 6320 idr_for_each_entry(&btf_idr, mod_btf, id) { 6321 if (!btf_is_module(mod_btf)) 6322 continue; 6323 /* linear search could be slow hence unlock/lock 6324 * the IDR to avoiding holding it for too long 6325 */ 6326 btf_get(mod_btf); 6327 spin_unlock_bh(&btf_idr_lock); 6328 ret = btf_find_by_name_kind(mod_btf, name, kind); 6329 if (ret > 0) { 6330 int btf_obj_fd; 6331 6332 btf_obj_fd = __btf_new_fd(mod_btf); 6333 if (btf_obj_fd < 0) { 6334 btf_put(mod_btf); 6335 return btf_obj_fd; 6336 } 6337 return ret | (((u64)btf_obj_fd) << 32); 6338 } 6339 spin_lock_bh(&btf_idr_lock); 6340 btf_put(mod_btf); 6341 } 6342 spin_unlock_bh(&btf_idr_lock); 6343 } 6344 return ret; 6345 } 6346 6347 const struct bpf_func_proto bpf_btf_find_by_name_kind_proto = { 6348 .func = bpf_btf_find_by_name_kind, 6349 .gpl_only = false, 6350 .ret_type = RET_INTEGER, 6351 .arg1_type = ARG_PTR_TO_MEM, 6352 .arg2_type = ARG_CONST_SIZE, 6353 .arg3_type = ARG_ANYTHING, 6354 .arg4_type = ARG_ANYTHING, 6355 }; 6356 6357 BTF_ID_LIST_GLOBAL(btf_tracing_ids, MAX_BTF_TRACING_TYPE) 6358 #define BTF_TRACING_TYPE(name, type) BTF_ID(struct, type) 6359 BTF_TRACING_TYPE_xxx 6360 #undef BTF_TRACING_TYPE 6361 6362 /* BTF ID set registration API for modules */ 6363 6364 struct kfunc_btf_id_list { 6365 struct list_head list; 6366 struct mutex mutex; 6367 }; 6368 6369 #ifdef CONFIG_DEBUG_INFO_BTF_MODULES 6370 6371 void register_kfunc_btf_id_set(struct kfunc_btf_id_list *l, 6372 struct kfunc_btf_id_set *s) 6373 { 6374 mutex_lock(&l->mutex); 6375 list_add(&s->list, &l->list); 6376 mutex_unlock(&l->mutex); 6377 } 6378 EXPORT_SYMBOL_GPL(register_kfunc_btf_id_set); 6379 6380 void unregister_kfunc_btf_id_set(struct kfunc_btf_id_list *l, 6381 struct kfunc_btf_id_set *s) 6382 { 6383 mutex_lock(&l->mutex); 6384 list_del_init(&s->list); 6385 mutex_unlock(&l->mutex); 6386 } 6387 EXPORT_SYMBOL_GPL(unregister_kfunc_btf_id_set); 6388 6389 bool bpf_check_mod_kfunc_call(struct kfunc_btf_id_list *klist, u32 kfunc_id, 6390 struct module *owner) 6391 { 6392 struct kfunc_btf_id_set *s; 6393 6394 if (!owner) 6395 return false; 6396 mutex_lock(&klist->mutex); 6397 list_for_each_entry(s, &klist->list, list) { 6398 if (s->owner == owner && btf_id_set_contains(s->set, kfunc_id)) { 6399 mutex_unlock(&klist->mutex); 6400 return true; 6401 } 6402 } 6403 mutex_unlock(&klist->mutex); 6404 return false; 6405 } 6406 6407 #endif 6408 6409 #define DEFINE_KFUNC_BTF_ID_LIST(name) \ 6410 struct kfunc_btf_id_list name = { LIST_HEAD_INIT(name.list), \ 6411 __MUTEX_INITIALIZER(name.mutex) }; \ 6412 EXPORT_SYMBOL_GPL(name) 6413 6414 DEFINE_KFUNC_BTF_ID_LIST(bpf_tcp_ca_kfunc_list); 6415 DEFINE_KFUNC_BTF_ID_LIST(prog_test_kfunc_list); 6416