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/bpf_lsm.h> 23 #include <linux/skmsg.h> 24 #include <linux/perf_event.h> 25 #include <linux/bsearch.h> 26 #include <linux/kobject.h> 27 #include <linux/sysfs.h> 28 #include <net/sock.h> 29 #include "../tools/lib/bpf/relo_core.h" 30 31 /* BTF (BPF Type Format) is the meta data format which describes 32 * the data types of BPF program/map. Hence, it basically focus 33 * on the C programming language which the modern BPF is primary 34 * using. 35 * 36 * ELF Section: 37 * ~~~~~~~~~~~ 38 * The BTF data is stored under the ".BTF" ELF section 39 * 40 * struct btf_type: 41 * ~~~~~~~~~~~~~~~ 42 * Each 'struct btf_type' object describes a C data type. 43 * Depending on the type it is describing, a 'struct btf_type' 44 * object may be followed by more data. F.e. 45 * To describe an array, 'struct btf_type' is followed by 46 * 'struct btf_array'. 47 * 48 * 'struct btf_type' and any extra data following it are 49 * 4 bytes aligned. 50 * 51 * Type section: 52 * ~~~~~~~~~~~~~ 53 * The BTF type section contains a list of 'struct btf_type' objects. 54 * Each one describes a C type. Recall from the above section 55 * that a 'struct btf_type' object could be immediately followed by extra 56 * data in order to describe some particular C types. 57 * 58 * type_id: 59 * ~~~~~~~ 60 * Each btf_type object is identified by a type_id. The type_id 61 * is implicitly implied by the location of the btf_type object in 62 * the BTF type section. The first one has type_id 1. The second 63 * one has type_id 2...etc. Hence, an earlier btf_type has 64 * a smaller type_id. 65 * 66 * A btf_type object may refer to another btf_type object by using 67 * type_id (i.e. the "type" in the "struct btf_type"). 68 * 69 * NOTE that we cannot assume any reference-order. 70 * A btf_type object can refer to an earlier btf_type object 71 * but it can also refer to a later btf_type object. 72 * 73 * For example, to describe "const void *". A btf_type 74 * object describing "const" may refer to another btf_type 75 * object describing "void *". This type-reference is done 76 * by specifying type_id: 77 * 78 * [1] CONST (anon) type_id=2 79 * [2] PTR (anon) type_id=0 80 * 81 * The above is the btf_verifier debug log: 82 * - Each line started with "[?]" is a btf_type object 83 * - [?] is the type_id of the btf_type object. 84 * - CONST/PTR is the BTF_KIND_XXX 85 * - "(anon)" is the name of the type. It just 86 * happens that CONST and PTR has no name. 87 * - type_id=XXX is the 'u32 type' in btf_type 88 * 89 * NOTE: "void" has type_id 0 90 * 91 * String section: 92 * ~~~~~~~~~~~~~~ 93 * The BTF string section contains the names used by the type section. 94 * Each string is referred by an "offset" from the beginning of the 95 * string section. 96 * 97 * Each string is '\0' terminated. 98 * 99 * The first character in the string section must be '\0' 100 * which is used to mean 'anonymous'. Some btf_type may not 101 * have a name. 102 */ 103 104 /* BTF verification: 105 * 106 * To verify BTF data, two passes are needed. 107 * 108 * Pass #1 109 * ~~~~~~~ 110 * The first pass is to collect all btf_type objects to 111 * an array: "btf->types". 112 * 113 * Depending on the C type that a btf_type is describing, 114 * a btf_type may be followed by extra data. We don't know 115 * how many btf_type is there, and more importantly we don't 116 * know where each btf_type is located in the type section. 117 * 118 * Without knowing the location of each type_id, most verifications 119 * cannot be done. e.g. an earlier btf_type may refer to a later 120 * btf_type (recall the "const void *" above), so we cannot 121 * check this type-reference in the first pass. 122 * 123 * In the first pass, it still does some verifications (e.g. 124 * checking the name is a valid offset to the string section). 125 * 126 * Pass #2 127 * ~~~~~~~ 128 * The main focus is to resolve a btf_type that is referring 129 * to another type. 130 * 131 * We have to ensure the referring type: 132 * 1) does exist in the BTF (i.e. in btf->types[]) 133 * 2) does not cause a loop: 134 * struct A { 135 * struct B b; 136 * }; 137 * 138 * struct B { 139 * struct A a; 140 * }; 141 * 142 * btf_type_needs_resolve() decides if a btf_type needs 143 * to be resolved. 144 * 145 * The needs_resolve type implements the "resolve()" ops which 146 * essentially does a DFS and detects backedge. 147 * 148 * During resolve (or DFS), different C types have different 149 * "RESOLVED" conditions. 150 * 151 * When resolving a BTF_KIND_STRUCT, we need to resolve all its 152 * members because a member is always referring to another 153 * type. A struct's member can be treated as "RESOLVED" if 154 * it is referring to a BTF_KIND_PTR. Otherwise, the 155 * following valid C struct would be rejected: 156 * 157 * struct A { 158 * int m; 159 * struct A *a; 160 * }; 161 * 162 * When resolving a BTF_KIND_PTR, it needs to keep resolving if 163 * it is referring to another BTF_KIND_PTR. Otherwise, we cannot 164 * detect a pointer loop, e.g.: 165 * BTF_KIND_CONST -> BTF_KIND_PTR -> BTF_KIND_CONST -> BTF_KIND_PTR + 166 * ^ | 167 * +-----------------------------------------+ 168 * 169 */ 170 171 #define BITS_PER_U128 (sizeof(u64) * BITS_PER_BYTE * 2) 172 #define BITS_PER_BYTE_MASK (BITS_PER_BYTE - 1) 173 #define BITS_PER_BYTE_MASKED(bits) ((bits) & BITS_PER_BYTE_MASK) 174 #define BITS_ROUNDDOWN_BYTES(bits) ((bits) >> 3) 175 #define BITS_ROUNDUP_BYTES(bits) \ 176 (BITS_ROUNDDOWN_BYTES(bits) + !!BITS_PER_BYTE_MASKED(bits)) 177 178 #define BTF_INFO_MASK 0x9f00ffff 179 #define BTF_INT_MASK 0x0fffffff 180 #define BTF_TYPE_ID_VALID(type_id) ((type_id) <= BTF_MAX_TYPE) 181 #define BTF_STR_OFFSET_VALID(name_off) ((name_off) <= BTF_MAX_NAME_OFFSET) 182 183 /* 16MB for 64k structs and each has 16 members and 184 * a few MB spaces for the string section. 185 * The hard limit is S32_MAX. 186 */ 187 #define BTF_MAX_SIZE (16 * 1024 * 1024) 188 189 #define for_each_member_from(i, from, struct_type, member) \ 190 for (i = from, member = btf_type_member(struct_type) + from; \ 191 i < btf_type_vlen(struct_type); \ 192 i++, member++) 193 194 #define for_each_vsi_from(i, from, struct_type, member) \ 195 for (i = from, member = btf_type_var_secinfo(struct_type) + from; \ 196 i < btf_type_vlen(struct_type); \ 197 i++, member++) 198 199 DEFINE_IDR(btf_idr); 200 DEFINE_SPINLOCK(btf_idr_lock); 201 202 enum btf_kfunc_hook { 203 BTF_KFUNC_HOOK_COMMON, 204 BTF_KFUNC_HOOK_XDP, 205 BTF_KFUNC_HOOK_TC, 206 BTF_KFUNC_HOOK_STRUCT_OPS, 207 BTF_KFUNC_HOOK_TRACING, 208 BTF_KFUNC_HOOK_SYSCALL, 209 BTF_KFUNC_HOOK_FMODRET, 210 BTF_KFUNC_HOOK_MAX, 211 }; 212 213 enum { 214 BTF_KFUNC_SET_MAX_CNT = 256, 215 BTF_DTOR_KFUNC_MAX_CNT = 256, 216 }; 217 218 struct btf_kfunc_set_tab { 219 struct btf_id_set8 *sets[BTF_KFUNC_HOOK_MAX]; 220 }; 221 222 struct btf_id_dtor_kfunc_tab { 223 u32 cnt; 224 struct btf_id_dtor_kfunc dtors[]; 225 }; 226 227 struct btf { 228 void *data; 229 struct btf_type **types; 230 u32 *resolved_ids; 231 u32 *resolved_sizes; 232 const char *strings; 233 void *nohdr_data; 234 struct btf_header hdr; 235 u32 nr_types; /* includes VOID for base BTF */ 236 u32 types_size; 237 u32 data_size; 238 refcount_t refcnt; 239 u32 id; 240 struct rcu_head rcu; 241 struct btf_kfunc_set_tab *kfunc_set_tab; 242 struct btf_id_dtor_kfunc_tab *dtor_kfunc_tab; 243 struct btf_struct_metas *struct_meta_tab; 244 245 /* split BTF support */ 246 struct btf *base_btf; 247 u32 start_id; /* first type ID in this BTF (0 for base BTF) */ 248 u32 start_str_off; /* first string offset (0 for base BTF) */ 249 char name[MODULE_NAME_LEN]; 250 bool kernel_btf; 251 }; 252 253 enum verifier_phase { 254 CHECK_META, 255 CHECK_TYPE, 256 }; 257 258 struct resolve_vertex { 259 const struct btf_type *t; 260 u32 type_id; 261 u16 next_member; 262 }; 263 264 enum visit_state { 265 NOT_VISITED, 266 VISITED, 267 RESOLVED, 268 }; 269 270 enum resolve_mode { 271 RESOLVE_TBD, /* To Be Determined */ 272 RESOLVE_PTR, /* Resolving for Pointer */ 273 RESOLVE_STRUCT_OR_ARRAY, /* Resolving for struct/union 274 * or array 275 */ 276 }; 277 278 #define MAX_RESOLVE_DEPTH 32 279 280 struct btf_sec_info { 281 u32 off; 282 u32 len; 283 }; 284 285 struct btf_verifier_env { 286 struct btf *btf; 287 u8 *visit_states; 288 struct resolve_vertex stack[MAX_RESOLVE_DEPTH]; 289 struct bpf_verifier_log log; 290 u32 log_type_id; 291 u32 top_stack; 292 enum verifier_phase phase; 293 enum resolve_mode resolve_mode; 294 }; 295 296 static const char * const btf_kind_str[NR_BTF_KINDS] = { 297 [BTF_KIND_UNKN] = "UNKNOWN", 298 [BTF_KIND_INT] = "INT", 299 [BTF_KIND_PTR] = "PTR", 300 [BTF_KIND_ARRAY] = "ARRAY", 301 [BTF_KIND_STRUCT] = "STRUCT", 302 [BTF_KIND_UNION] = "UNION", 303 [BTF_KIND_ENUM] = "ENUM", 304 [BTF_KIND_FWD] = "FWD", 305 [BTF_KIND_TYPEDEF] = "TYPEDEF", 306 [BTF_KIND_VOLATILE] = "VOLATILE", 307 [BTF_KIND_CONST] = "CONST", 308 [BTF_KIND_RESTRICT] = "RESTRICT", 309 [BTF_KIND_FUNC] = "FUNC", 310 [BTF_KIND_FUNC_PROTO] = "FUNC_PROTO", 311 [BTF_KIND_VAR] = "VAR", 312 [BTF_KIND_DATASEC] = "DATASEC", 313 [BTF_KIND_FLOAT] = "FLOAT", 314 [BTF_KIND_DECL_TAG] = "DECL_TAG", 315 [BTF_KIND_TYPE_TAG] = "TYPE_TAG", 316 [BTF_KIND_ENUM64] = "ENUM64", 317 }; 318 319 const char *btf_type_str(const struct btf_type *t) 320 { 321 return btf_kind_str[BTF_INFO_KIND(t->info)]; 322 } 323 324 /* Chunk size we use in safe copy of data to be shown. */ 325 #define BTF_SHOW_OBJ_SAFE_SIZE 32 326 327 /* 328 * This is the maximum size of a base type value (equivalent to a 329 * 128-bit int); if we are at the end of our safe buffer and have 330 * less than 16 bytes space we can't be assured of being able 331 * to copy the next type safely, so in such cases we will initiate 332 * a new copy. 333 */ 334 #define BTF_SHOW_OBJ_BASE_TYPE_SIZE 16 335 336 /* Type name size */ 337 #define BTF_SHOW_NAME_SIZE 80 338 339 /* 340 * Common data to all BTF show operations. Private show functions can add 341 * their own data to a structure containing a struct btf_show and consult it 342 * in the show callback. See btf_type_show() below. 343 * 344 * One challenge with showing nested data is we want to skip 0-valued 345 * data, but in order to figure out whether a nested object is all zeros 346 * we need to walk through it. As a result, we need to make two passes 347 * when handling structs, unions and arrays; the first path simply looks 348 * for nonzero data, while the second actually does the display. The first 349 * pass is signalled by show->state.depth_check being set, and if we 350 * encounter a non-zero value we set show->state.depth_to_show to 351 * the depth at which we encountered it. When we have completed the 352 * first pass, we will know if anything needs to be displayed if 353 * depth_to_show > depth. See btf_[struct,array]_show() for the 354 * implementation of this. 355 * 356 * Another problem is we want to ensure the data for display is safe to 357 * access. To support this, the anonymous "struct {} obj" tracks the data 358 * object and our safe copy of it. We copy portions of the data needed 359 * to the object "copy" buffer, but because its size is limited to 360 * BTF_SHOW_OBJ_COPY_LEN bytes, multiple copies may be required as we 361 * traverse larger objects for display. 362 * 363 * The various data type show functions all start with a call to 364 * btf_show_start_type() which returns a pointer to the safe copy 365 * of the data needed (or if BTF_SHOW_UNSAFE is specified, to the 366 * raw data itself). btf_show_obj_safe() is responsible for 367 * using copy_from_kernel_nofault() to update the safe data if necessary 368 * as we traverse the object's data. skbuff-like semantics are 369 * used: 370 * 371 * - obj.head points to the start of the toplevel object for display 372 * - obj.size is the size of the toplevel object 373 * - obj.data points to the current point in the original data at 374 * which our safe data starts. obj.data will advance as we copy 375 * portions of the data. 376 * 377 * In most cases a single copy will suffice, but larger data structures 378 * such as "struct task_struct" will require many copies. The logic in 379 * btf_show_obj_safe() handles the logic that determines if a new 380 * copy_from_kernel_nofault() is needed. 381 */ 382 struct btf_show { 383 u64 flags; 384 void *target; /* target of show operation (seq file, buffer) */ 385 void (*showfn)(struct btf_show *show, const char *fmt, va_list args); 386 const struct btf *btf; 387 /* below are used during iteration */ 388 struct { 389 u8 depth; 390 u8 depth_to_show; 391 u8 depth_check; 392 u8 array_member:1, 393 array_terminated:1; 394 u16 array_encoding; 395 u32 type_id; 396 int status; /* non-zero for error */ 397 const struct btf_type *type; 398 const struct btf_member *member; 399 char name[BTF_SHOW_NAME_SIZE]; /* space for member name/type */ 400 } state; 401 struct { 402 u32 size; 403 void *head; 404 void *data; 405 u8 safe[BTF_SHOW_OBJ_SAFE_SIZE]; 406 } obj; 407 }; 408 409 struct btf_kind_operations { 410 s32 (*check_meta)(struct btf_verifier_env *env, 411 const struct btf_type *t, 412 u32 meta_left); 413 int (*resolve)(struct btf_verifier_env *env, 414 const struct resolve_vertex *v); 415 int (*check_member)(struct btf_verifier_env *env, 416 const struct btf_type *struct_type, 417 const struct btf_member *member, 418 const struct btf_type *member_type); 419 int (*check_kflag_member)(struct btf_verifier_env *env, 420 const struct btf_type *struct_type, 421 const struct btf_member *member, 422 const struct btf_type *member_type); 423 void (*log_details)(struct btf_verifier_env *env, 424 const struct btf_type *t); 425 void (*show)(const struct btf *btf, const struct btf_type *t, 426 u32 type_id, void *data, u8 bits_offsets, 427 struct btf_show *show); 428 }; 429 430 static const struct btf_kind_operations * const kind_ops[NR_BTF_KINDS]; 431 static struct btf_type btf_void; 432 433 static int btf_resolve(struct btf_verifier_env *env, 434 const struct btf_type *t, u32 type_id); 435 436 static int btf_func_check(struct btf_verifier_env *env, 437 const struct btf_type *t); 438 439 static bool btf_type_is_modifier(const struct btf_type *t) 440 { 441 /* Some of them is not strictly a C modifier 442 * but they are grouped into the same bucket 443 * for BTF concern: 444 * A type (t) that refers to another 445 * type through t->type AND its size cannot 446 * be determined without following the t->type. 447 * 448 * ptr does not fall into this bucket 449 * because its size is always sizeof(void *). 450 */ 451 switch (BTF_INFO_KIND(t->info)) { 452 case BTF_KIND_TYPEDEF: 453 case BTF_KIND_VOLATILE: 454 case BTF_KIND_CONST: 455 case BTF_KIND_RESTRICT: 456 case BTF_KIND_TYPE_TAG: 457 return true; 458 } 459 460 return false; 461 } 462 463 bool btf_type_is_void(const struct btf_type *t) 464 { 465 return t == &btf_void; 466 } 467 468 static bool btf_type_is_fwd(const struct btf_type *t) 469 { 470 return BTF_INFO_KIND(t->info) == BTF_KIND_FWD; 471 } 472 473 static bool btf_type_nosize(const struct btf_type *t) 474 { 475 return btf_type_is_void(t) || btf_type_is_fwd(t) || 476 btf_type_is_func(t) || btf_type_is_func_proto(t); 477 } 478 479 static bool btf_type_nosize_or_null(const struct btf_type *t) 480 { 481 return !t || btf_type_nosize(t); 482 } 483 484 static bool btf_type_is_datasec(const struct btf_type *t) 485 { 486 return BTF_INFO_KIND(t->info) == BTF_KIND_DATASEC; 487 } 488 489 static bool btf_type_is_decl_tag(const struct btf_type *t) 490 { 491 return BTF_INFO_KIND(t->info) == BTF_KIND_DECL_TAG; 492 } 493 494 static bool btf_type_is_decl_tag_target(const struct btf_type *t) 495 { 496 return btf_type_is_func(t) || btf_type_is_struct(t) || 497 btf_type_is_var(t) || btf_type_is_typedef(t); 498 } 499 500 u32 btf_nr_types(const struct btf *btf) 501 { 502 u32 total = 0; 503 504 while (btf) { 505 total += btf->nr_types; 506 btf = btf->base_btf; 507 } 508 509 return total; 510 } 511 512 s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind) 513 { 514 const struct btf_type *t; 515 const char *tname; 516 u32 i, total; 517 518 total = btf_nr_types(btf); 519 for (i = 1; i < total; i++) { 520 t = btf_type_by_id(btf, i); 521 if (BTF_INFO_KIND(t->info) != kind) 522 continue; 523 524 tname = btf_name_by_offset(btf, t->name_off); 525 if (!strcmp(tname, name)) 526 return i; 527 } 528 529 return -ENOENT; 530 } 531 532 static s32 bpf_find_btf_id(const char *name, u32 kind, struct btf **btf_p) 533 { 534 struct btf *btf; 535 s32 ret; 536 int id; 537 538 btf = bpf_get_btf_vmlinux(); 539 if (IS_ERR(btf)) 540 return PTR_ERR(btf); 541 if (!btf) 542 return -EINVAL; 543 544 ret = btf_find_by_name_kind(btf, name, kind); 545 /* ret is never zero, since btf_find_by_name_kind returns 546 * positive btf_id or negative error. 547 */ 548 if (ret > 0) { 549 btf_get(btf); 550 *btf_p = btf; 551 return ret; 552 } 553 554 /* If name is not found in vmlinux's BTF then search in module's BTFs */ 555 spin_lock_bh(&btf_idr_lock); 556 idr_for_each_entry(&btf_idr, btf, id) { 557 if (!btf_is_module(btf)) 558 continue; 559 /* linear search could be slow hence unlock/lock 560 * the IDR to avoiding holding it for too long 561 */ 562 btf_get(btf); 563 spin_unlock_bh(&btf_idr_lock); 564 ret = btf_find_by_name_kind(btf, name, kind); 565 if (ret > 0) { 566 *btf_p = btf; 567 return ret; 568 } 569 spin_lock_bh(&btf_idr_lock); 570 btf_put(btf); 571 } 572 spin_unlock_bh(&btf_idr_lock); 573 return ret; 574 } 575 576 const struct btf_type *btf_type_skip_modifiers(const struct btf *btf, 577 u32 id, u32 *res_id) 578 { 579 const struct btf_type *t = btf_type_by_id(btf, id); 580 581 while (btf_type_is_modifier(t)) { 582 id = t->type; 583 t = btf_type_by_id(btf, t->type); 584 } 585 586 if (res_id) 587 *res_id = id; 588 589 return t; 590 } 591 592 const struct btf_type *btf_type_resolve_ptr(const struct btf *btf, 593 u32 id, u32 *res_id) 594 { 595 const struct btf_type *t; 596 597 t = btf_type_skip_modifiers(btf, id, NULL); 598 if (!btf_type_is_ptr(t)) 599 return NULL; 600 601 return btf_type_skip_modifiers(btf, t->type, res_id); 602 } 603 604 const struct btf_type *btf_type_resolve_func_ptr(const struct btf *btf, 605 u32 id, u32 *res_id) 606 { 607 const struct btf_type *ptype; 608 609 ptype = btf_type_resolve_ptr(btf, id, res_id); 610 if (ptype && btf_type_is_func_proto(ptype)) 611 return ptype; 612 613 return NULL; 614 } 615 616 /* Types that act only as a source, not sink or intermediate 617 * type when resolving. 618 */ 619 static bool btf_type_is_resolve_source_only(const struct btf_type *t) 620 { 621 return btf_type_is_var(t) || 622 btf_type_is_decl_tag(t) || 623 btf_type_is_datasec(t); 624 } 625 626 /* What types need to be resolved? 627 * 628 * btf_type_is_modifier() is an obvious one. 629 * 630 * btf_type_is_struct() because its member refers to 631 * another type (through member->type). 632 * 633 * btf_type_is_var() because the variable refers to 634 * another type. btf_type_is_datasec() holds multiple 635 * btf_type_is_var() types that need resolving. 636 * 637 * btf_type_is_array() because its element (array->type) 638 * refers to another type. Array can be thought of a 639 * special case of struct while array just has the same 640 * member-type repeated by array->nelems of times. 641 */ 642 static bool btf_type_needs_resolve(const struct btf_type *t) 643 { 644 return btf_type_is_modifier(t) || 645 btf_type_is_ptr(t) || 646 btf_type_is_struct(t) || 647 btf_type_is_array(t) || 648 btf_type_is_var(t) || 649 btf_type_is_func(t) || 650 btf_type_is_decl_tag(t) || 651 btf_type_is_datasec(t); 652 } 653 654 /* t->size can be used */ 655 static bool btf_type_has_size(const struct btf_type *t) 656 { 657 switch (BTF_INFO_KIND(t->info)) { 658 case BTF_KIND_INT: 659 case BTF_KIND_STRUCT: 660 case BTF_KIND_UNION: 661 case BTF_KIND_ENUM: 662 case BTF_KIND_DATASEC: 663 case BTF_KIND_FLOAT: 664 case BTF_KIND_ENUM64: 665 return true; 666 } 667 668 return false; 669 } 670 671 static const char *btf_int_encoding_str(u8 encoding) 672 { 673 if (encoding == 0) 674 return "(none)"; 675 else if (encoding == BTF_INT_SIGNED) 676 return "SIGNED"; 677 else if (encoding == BTF_INT_CHAR) 678 return "CHAR"; 679 else if (encoding == BTF_INT_BOOL) 680 return "BOOL"; 681 else 682 return "UNKN"; 683 } 684 685 static u32 btf_type_int(const struct btf_type *t) 686 { 687 return *(u32 *)(t + 1); 688 } 689 690 static const struct btf_array *btf_type_array(const struct btf_type *t) 691 { 692 return (const struct btf_array *)(t + 1); 693 } 694 695 static const struct btf_enum *btf_type_enum(const struct btf_type *t) 696 { 697 return (const struct btf_enum *)(t + 1); 698 } 699 700 static const struct btf_var *btf_type_var(const struct btf_type *t) 701 { 702 return (const struct btf_var *)(t + 1); 703 } 704 705 static const struct btf_decl_tag *btf_type_decl_tag(const struct btf_type *t) 706 { 707 return (const struct btf_decl_tag *)(t + 1); 708 } 709 710 static const struct btf_enum64 *btf_type_enum64(const struct btf_type *t) 711 { 712 return (const struct btf_enum64 *)(t + 1); 713 } 714 715 static const struct btf_kind_operations *btf_type_ops(const struct btf_type *t) 716 { 717 return kind_ops[BTF_INFO_KIND(t->info)]; 718 } 719 720 static bool btf_name_offset_valid(const struct btf *btf, u32 offset) 721 { 722 if (!BTF_STR_OFFSET_VALID(offset)) 723 return false; 724 725 while (offset < btf->start_str_off) 726 btf = btf->base_btf; 727 728 offset -= btf->start_str_off; 729 return offset < btf->hdr.str_len; 730 } 731 732 static bool __btf_name_char_ok(char c, bool first, bool dot_ok) 733 { 734 if ((first ? !isalpha(c) : 735 !isalnum(c)) && 736 c != '_' && 737 ((c == '.' && !dot_ok) || 738 c != '.')) 739 return false; 740 return true; 741 } 742 743 static const char *btf_str_by_offset(const struct btf *btf, u32 offset) 744 { 745 while (offset < btf->start_str_off) 746 btf = btf->base_btf; 747 748 offset -= btf->start_str_off; 749 if (offset < btf->hdr.str_len) 750 return &btf->strings[offset]; 751 752 return NULL; 753 } 754 755 static bool __btf_name_valid(const struct btf *btf, u32 offset, bool dot_ok) 756 { 757 /* offset must be valid */ 758 const char *src = btf_str_by_offset(btf, offset); 759 const char *src_limit; 760 761 if (!__btf_name_char_ok(*src, true, dot_ok)) 762 return false; 763 764 /* set a limit on identifier length */ 765 src_limit = src + KSYM_NAME_LEN; 766 src++; 767 while (*src && src < src_limit) { 768 if (!__btf_name_char_ok(*src, false, dot_ok)) 769 return false; 770 src++; 771 } 772 773 return !*src; 774 } 775 776 /* Only C-style identifier is permitted. This can be relaxed if 777 * necessary. 778 */ 779 static bool btf_name_valid_identifier(const struct btf *btf, u32 offset) 780 { 781 return __btf_name_valid(btf, offset, false); 782 } 783 784 static bool btf_name_valid_section(const struct btf *btf, u32 offset) 785 { 786 return __btf_name_valid(btf, offset, true); 787 } 788 789 static const char *__btf_name_by_offset(const struct btf *btf, u32 offset) 790 { 791 const char *name; 792 793 if (!offset) 794 return "(anon)"; 795 796 name = btf_str_by_offset(btf, offset); 797 return name ?: "(invalid-name-offset)"; 798 } 799 800 const char *btf_name_by_offset(const struct btf *btf, u32 offset) 801 { 802 return btf_str_by_offset(btf, offset); 803 } 804 805 const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id) 806 { 807 while (type_id < btf->start_id) 808 btf = btf->base_btf; 809 810 type_id -= btf->start_id; 811 if (type_id >= btf->nr_types) 812 return NULL; 813 return btf->types[type_id]; 814 } 815 EXPORT_SYMBOL_GPL(btf_type_by_id); 816 817 /* 818 * Regular int is not a bit field and it must be either 819 * u8/u16/u32/u64 or __int128. 820 */ 821 static bool btf_type_int_is_regular(const struct btf_type *t) 822 { 823 u8 nr_bits, nr_bytes; 824 u32 int_data; 825 826 int_data = btf_type_int(t); 827 nr_bits = BTF_INT_BITS(int_data); 828 nr_bytes = BITS_ROUNDUP_BYTES(nr_bits); 829 if (BITS_PER_BYTE_MASKED(nr_bits) || 830 BTF_INT_OFFSET(int_data) || 831 (nr_bytes != sizeof(u8) && nr_bytes != sizeof(u16) && 832 nr_bytes != sizeof(u32) && nr_bytes != sizeof(u64) && 833 nr_bytes != (2 * sizeof(u64)))) { 834 return false; 835 } 836 837 return true; 838 } 839 840 /* 841 * Check that given struct member is a regular int with expected 842 * offset and size. 843 */ 844 bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s, 845 const struct btf_member *m, 846 u32 expected_offset, u32 expected_size) 847 { 848 const struct btf_type *t; 849 u32 id, int_data; 850 u8 nr_bits; 851 852 id = m->type; 853 t = btf_type_id_size(btf, &id, NULL); 854 if (!t || !btf_type_is_int(t)) 855 return false; 856 857 int_data = btf_type_int(t); 858 nr_bits = BTF_INT_BITS(int_data); 859 if (btf_type_kflag(s)) { 860 u32 bitfield_size = BTF_MEMBER_BITFIELD_SIZE(m->offset); 861 u32 bit_offset = BTF_MEMBER_BIT_OFFSET(m->offset); 862 863 /* if kflag set, int should be a regular int and 864 * bit offset should be at byte boundary. 865 */ 866 return !bitfield_size && 867 BITS_ROUNDUP_BYTES(bit_offset) == expected_offset && 868 BITS_ROUNDUP_BYTES(nr_bits) == expected_size; 869 } 870 871 if (BTF_INT_OFFSET(int_data) || 872 BITS_PER_BYTE_MASKED(m->offset) || 873 BITS_ROUNDUP_BYTES(m->offset) != expected_offset || 874 BITS_PER_BYTE_MASKED(nr_bits) || 875 BITS_ROUNDUP_BYTES(nr_bits) != expected_size) 876 return false; 877 878 return true; 879 } 880 881 /* Similar to btf_type_skip_modifiers() but does not skip typedefs. */ 882 static const struct btf_type *btf_type_skip_qualifiers(const struct btf *btf, 883 u32 id) 884 { 885 const struct btf_type *t = btf_type_by_id(btf, id); 886 887 while (btf_type_is_modifier(t) && 888 BTF_INFO_KIND(t->info) != BTF_KIND_TYPEDEF) { 889 t = btf_type_by_id(btf, t->type); 890 } 891 892 return t; 893 } 894 895 #define BTF_SHOW_MAX_ITER 10 896 897 #define BTF_KIND_BIT(kind) (1ULL << kind) 898 899 /* 900 * Populate show->state.name with type name information. 901 * Format of type name is 902 * 903 * [.member_name = ] (type_name) 904 */ 905 static const char *btf_show_name(struct btf_show *show) 906 { 907 /* BTF_MAX_ITER array suffixes "[]" */ 908 const char *array_suffixes = "[][][][][][][][][][]"; 909 const char *array_suffix = &array_suffixes[strlen(array_suffixes)]; 910 /* BTF_MAX_ITER pointer suffixes "*" */ 911 const char *ptr_suffixes = "**********"; 912 const char *ptr_suffix = &ptr_suffixes[strlen(ptr_suffixes)]; 913 const char *name = NULL, *prefix = "", *parens = ""; 914 const struct btf_member *m = show->state.member; 915 const struct btf_type *t; 916 const struct btf_array *array; 917 u32 id = show->state.type_id; 918 const char *member = NULL; 919 bool show_member = false; 920 u64 kinds = 0; 921 int i; 922 923 show->state.name[0] = '\0'; 924 925 /* 926 * Don't show type name if we're showing an array member; 927 * in that case we show the array type so don't need to repeat 928 * ourselves for each member. 929 */ 930 if (show->state.array_member) 931 return ""; 932 933 /* Retrieve member name, if any. */ 934 if (m) { 935 member = btf_name_by_offset(show->btf, m->name_off); 936 show_member = strlen(member) > 0; 937 id = m->type; 938 } 939 940 /* 941 * Start with type_id, as we have resolved the struct btf_type * 942 * via btf_modifier_show() past the parent typedef to the child 943 * struct, int etc it is defined as. In such cases, the type_id 944 * still represents the starting type while the struct btf_type * 945 * in our show->state points at the resolved type of the typedef. 946 */ 947 t = btf_type_by_id(show->btf, id); 948 if (!t) 949 return ""; 950 951 /* 952 * The goal here is to build up the right number of pointer and 953 * array suffixes while ensuring the type name for a typedef 954 * is represented. Along the way we accumulate a list of 955 * BTF kinds we have encountered, since these will inform later 956 * display; for example, pointer types will not require an 957 * opening "{" for struct, we will just display the pointer value. 958 * 959 * We also want to accumulate the right number of pointer or array 960 * indices in the format string while iterating until we get to 961 * the typedef/pointee/array member target type. 962 * 963 * We start by pointing at the end of pointer and array suffix 964 * strings; as we accumulate pointers and arrays we move the pointer 965 * or array string backwards so it will show the expected number of 966 * '*' or '[]' for the type. BTF_SHOW_MAX_ITER of nesting of pointers 967 * and/or arrays and typedefs are supported as a precaution. 968 * 969 * We also want to get typedef name while proceeding to resolve 970 * type it points to so that we can add parentheses if it is a 971 * "typedef struct" etc. 972 */ 973 for (i = 0; i < BTF_SHOW_MAX_ITER; i++) { 974 975 switch (BTF_INFO_KIND(t->info)) { 976 case BTF_KIND_TYPEDEF: 977 if (!name) 978 name = btf_name_by_offset(show->btf, 979 t->name_off); 980 kinds |= BTF_KIND_BIT(BTF_KIND_TYPEDEF); 981 id = t->type; 982 break; 983 case BTF_KIND_ARRAY: 984 kinds |= BTF_KIND_BIT(BTF_KIND_ARRAY); 985 parens = "["; 986 if (!t) 987 return ""; 988 array = btf_type_array(t); 989 if (array_suffix > array_suffixes) 990 array_suffix -= 2; 991 id = array->type; 992 break; 993 case BTF_KIND_PTR: 994 kinds |= BTF_KIND_BIT(BTF_KIND_PTR); 995 if (ptr_suffix > ptr_suffixes) 996 ptr_suffix -= 1; 997 id = t->type; 998 break; 999 default: 1000 id = 0; 1001 break; 1002 } 1003 if (!id) 1004 break; 1005 t = btf_type_skip_qualifiers(show->btf, id); 1006 } 1007 /* We may not be able to represent this type; bail to be safe */ 1008 if (i == BTF_SHOW_MAX_ITER) 1009 return ""; 1010 1011 if (!name) 1012 name = btf_name_by_offset(show->btf, t->name_off); 1013 1014 switch (BTF_INFO_KIND(t->info)) { 1015 case BTF_KIND_STRUCT: 1016 case BTF_KIND_UNION: 1017 prefix = BTF_INFO_KIND(t->info) == BTF_KIND_STRUCT ? 1018 "struct" : "union"; 1019 /* if it's an array of struct/union, parens is already set */ 1020 if (!(kinds & (BTF_KIND_BIT(BTF_KIND_ARRAY)))) 1021 parens = "{"; 1022 break; 1023 case BTF_KIND_ENUM: 1024 case BTF_KIND_ENUM64: 1025 prefix = "enum"; 1026 break; 1027 default: 1028 break; 1029 } 1030 1031 /* pointer does not require parens */ 1032 if (kinds & BTF_KIND_BIT(BTF_KIND_PTR)) 1033 parens = ""; 1034 /* typedef does not require struct/union/enum prefix */ 1035 if (kinds & BTF_KIND_BIT(BTF_KIND_TYPEDEF)) 1036 prefix = ""; 1037 1038 if (!name) 1039 name = ""; 1040 1041 /* Even if we don't want type name info, we want parentheses etc */ 1042 if (show->flags & BTF_SHOW_NONAME) 1043 snprintf(show->state.name, sizeof(show->state.name), "%s", 1044 parens); 1045 else 1046 snprintf(show->state.name, sizeof(show->state.name), 1047 "%s%s%s(%s%s%s%s%s%s)%s", 1048 /* first 3 strings comprise ".member = " */ 1049 show_member ? "." : "", 1050 show_member ? member : "", 1051 show_member ? " = " : "", 1052 /* ...next is our prefix (struct, enum, etc) */ 1053 prefix, 1054 strlen(prefix) > 0 && strlen(name) > 0 ? " " : "", 1055 /* ...this is the type name itself */ 1056 name, 1057 /* ...suffixed by the appropriate '*', '[]' suffixes */ 1058 strlen(ptr_suffix) > 0 ? " " : "", ptr_suffix, 1059 array_suffix, parens); 1060 1061 return show->state.name; 1062 } 1063 1064 static const char *__btf_show_indent(struct btf_show *show) 1065 { 1066 const char *indents = " "; 1067 const char *indent = &indents[strlen(indents)]; 1068 1069 if ((indent - show->state.depth) >= indents) 1070 return indent - show->state.depth; 1071 return indents; 1072 } 1073 1074 static const char *btf_show_indent(struct btf_show *show) 1075 { 1076 return show->flags & BTF_SHOW_COMPACT ? "" : __btf_show_indent(show); 1077 } 1078 1079 static const char *btf_show_newline(struct btf_show *show) 1080 { 1081 return show->flags & BTF_SHOW_COMPACT ? "" : "\n"; 1082 } 1083 1084 static const char *btf_show_delim(struct btf_show *show) 1085 { 1086 if (show->state.depth == 0) 1087 return ""; 1088 1089 if ((show->flags & BTF_SHOW_COMPACT) && show->state.type && 1090 BTF_INFO_KIND(show->state.type->info) == BTF_KIND_UNION) 1091 return "|"; 1092 1093 return ","; 1094 } 1095 1096 __printf(2, 3) static void btf_show(struct btf_show *show, const char *fmt, ...) 1097 { 1098 va_list args; 1099 1100 if (!show->state.depth_check) { 1101 va_start(args, fmt); 1102 show->showfn(show, fmt, args); 1103 va_end(args); 1104 } 1105 } 1106 1107 /* Macros are used here as btf_show_type_value[s]() prepends and appends 1108 * format specifiers to the format specifier passed in; these do the work of 1109 * adding indentation, delimiters etc while the caller simply has to specify 1110 * the type value(s) in the format specifier + value(s). 1111 */ 1112 #define btf_show_type_value(show, fmt, value) \ 1113 do { \ 1114 if ((value) != (__typeof__(value))0 || \ 1115 (show->flags & BTF_SHOW_ZERO) || \ 1116 show->state.depth == 0) { \ 1117 btf_show(show, "%s%s" fmt "%s%s", \ 1118 btf_show_indent(show), \ 1119 btf_show_name(show), \ 1120 value, btf_show_delim(show), \ 1121 btf_show_newline(show)); \ 1122 if (show->state.depth > show->state.depth_to_show) \ 1123 show->state.depth_to_show = show->state.depth; \ 1124 } \ 1125 } while (0) 1126 1127 #define btf_show_type_values(show, fmt, ...) \ 1128 do { \ 1129 btf_show(show, "%s%s" fmt "%s%s", btf_show_indent(show), \ 1130 btf_show_name(show), \ 1131 __VA_ARGS__, btf_show_delim(show), \ 1132 btf_show_newline(show)); \ 1133 if (show->state.depth > show->state.depth_to_show) \ 1134 show->state.depth_to_show = show->state.depth; \ 1135 } while (0) 1136 1137 /* How much is left to copy to safe buffer after @data? */ 1138 static int btf_show_obj_size_left(struct btf_show *show, void *data) 1139 { 1140 return show->obj.head + show->obj.size - data; 1141 } 1142 1143 /* Is object pointed to by @data of @size already copied to our safe buffer? */ 1144 static bool btf_show_obj_is_safe(struct btf_show *show, void *data, int size) 1145 { 1146 return data >= show->obj.data && 1147 (data + size) < (show->obj.data + BTF_SHOW_OBJ_SAFE_SIZE); 1148 } 1149 1150 /* 1151 * If object pointed to by @data of @size falls within our safe buffer, return 1152 * the equivalent pointer to the same safe data. Assumes 1153 * copy_from_kernel_nofault() has already happened and our safe buffer is 1154 * populated. 1155 */ 1156 static void *__btf_show_obj_safe(struct btf_show *show, void *data, int size) 1157 { 1158 if (btf_show_obj_is_safe(show, data, size)) 1159 return show->obj.safe + (data - show->obj.data); 1160 return NULL; 1161 } 1162 1163 /* 1164 * Return a safe-to-access version of data pointed to by @data. 1165 * We do this by copying the relevant amount of information 1166 * to the struct btf_show obj.safe buffer using copy_from_kernel_nofault(). 1167 * 1168 * If BTF_SHOW_UNSAFE is specified, just return data as-is; no 1169 * safe copy is needed. 1170 * 1171 * Otherwise we need to determine if we have the required amount 1172 * of data (determined by the @data pointer and the size of the 1173 * largest base type we can encounter (represented by 1174 * BTF_SHOW_OBJ_BASE_TYPE_SIZE). Having that much data ensures 1175 * that we will be able to print some of the current object, 1176 * and if more is needed a copy will be triggered. 1177 * Some objects such as structs will not fit into the buffer; 1178 * in such cases additional copies when we iterate over their 1179 * members may be needed. 1180 * 1181 * btf_show_obj_safe() is used to return a safe buffer for 1182 * btf_show_start_type(); this ensures that as we recurse into 1183 * nested types we always have safe data for the given type. 1184 * This approach is somewhat wasteful; it's possible for example 1185 * that when iterating over a large union we'll end up copying the 1186 * same data repeatedly, but the goal is safety not performance. 1187 * We use stack data as opposed to per-CPU buffers because the 1188 * iteration over a type can take some time, and preemption handling 1189 * would greatly complicate use of the safe buffer. 1190 */ 1191 static void *btf_show_obj_safe(struct btf_show *show, 1192 const struct btf_type *t, 1193 void *data) 1194 { 1195 const struct btf_type *rt; 1196 int size_left, size; 1197 void *safe = NULL; 1198 1199 if (show->flags & BTF_SHOW_UNSAFE) 1200 return data; 1201 1202 rt = btf_resolve_size(show->btf, t, &size); 1203 if (IS_ERR(rt)) { 1204 show->state.status = PTR_ERR(rt); 1205 return NULL; 1206 } 1207 1208 /* 1209 * Is this toplevel object? If so, set total object size and 1210 * initialize pointers. Otherwise check if we still fall within 1211 * our safe object data. 1212 */ 1213 if (show->state.depth == 0) { 1214 show->obj.size = size; 1215 show->obj.head = data; 1216 } else { 1217 /* 1218 * If the size of the current object is > our remaining 1219 * safe buffer we _may_ need to do a new copy. However 1220 * consider the case of a nested struct; it's size pushes 1221 * us over the safe buffer limit, but showing any individual 1222 * struct members does not. In such cases, we don't need 1223 * to initiate a fresh copy yet; however we definitely need 1224 * at least BTF_SHOW_OBJ_BASE_TYPE_SIZE bytes left 1225 * in our buffer, regardless of the current object size. 1226 * The logic here is that as we resolve types we will 1227 * hit a base type at some point, and we need to be sure 1228 * the next chunk of data is safely available to display 1229 * that type info safely. We cannot rely on the size of 1230 * the current object here because it may be much larger 1231 * than our current buffer (e.g. task_struct is 8k). 1232 * All we want to do here is ensure that we can print the 1233 * next basic type, which we can if either 1234 * - the current type size is within the safe buffer; or 1235 * - at least BTF_SHOW_OBJ_BASE_TYPE_SIZE bytes are left in 1236 * the safe buffer. 1237 */ 1238 safe = __btf_show_obj_safe(show, data, 1239 min(size, 1240 BTF_SHOW_OBJ_BASE_TYPE_SIZE)); 1241 } 1242 1243 /* 1244 * We need a new copy to our safe object, either because we haven't 1245 * yet copied and are initializing safe data, or because the data 1246 * we want falls outside the boundaries of the safe object. 1247 */ 1248 if (!safe) { 1249 size_left = btf_show_obj_size_left(show, data); 1250 if (size_left > BTF_SHOW_OBJ_SAFE_SIZE) 1251 size_left = BTF_SHOW_OBJ_SAFE_SIZE; 1252 show->state.status = copy_from_kernel_nofault(show->obj.safe, 1253 data, size_left); 1254 if (!show->state.status) { 1255 show->obj.data = data; 1256 safe = show->obj.safe; 1257 } 1258 } 1259 1260 return safe; 1261 } 1262 1263 /* 1264 * Set the type we are starting to show and return a safe data pointer 1265 * to be used for showing the associated data. 1266 */ 1267 static void *btf_show_start_type(struct btf_show *show, 1268 const struct btf_type *t, 1269 u32 type_id, void *data) 1270 { 1271 show->state.type = t; 1272 show->state.type_id = type_id; 1273 show->state.name[0] = '\0'; 1274 1275 return btf_show_obj_safe(show, t, data); 1276 } 1277 1278 static void btf_show_end_type(struct btf_show *show) 1279 { 1280 show->state.type = NULL; 1281 show->state.type_id = 0; 1282 show->state.name[0] = '\0'; 1283 } 1284 1285 static void *btf_show_start_aggr_type(struct btf_show *show, 1286 const struct btf_type *t, 1287 u32 type_id, void *data) 1288 { 1289 void *safe_data = btf_show_start_type(show, t, type_id, data); 1290 1291 if (!safe_data) 1292 return safe_data; 1293 1294 btf_show(show, "%s%s%s", btf_show_indent(show), 1295 btf_show_name(show), 1296 btf_show_newline(show)); 1297 show->state.depth++; 1298 return safe_data; 1299 } 1300 1301 static void btf_show_end_aggr_type(struct btf_show *show, 1302 const char *suffix) 1303 { 1304 show->state.depth--; 1305 btf_show(show, "%s%s%s%s", btf_show_indent(show), suffix, 1306 btf_show_delim(show), btf_show_newline(show)); 1307 btf_show_end_type(show); 1308 } 1309 1310 static void btf_show_start_member(struct btf_show *show, 1311 const struct btf_member *m) 1312 { 1313 show->state.member = m; 1314 } 1315 1316 static void btf_show_start_array_member(struct btf_show *show) 1317 { 1318 show->state.array_member = 1; 1319 btf_show_start_member(show, NULL); 1320 } 1321 1322 static void btf_show_end_member(struct btf_show *show) 1323 { 1324 show->state.member = NULL; 1325 } 1326 1327 static void btf_show_end_array_member(struct btf_show *show) 1328 { 1329 show->state.array_member = 0; 1330 btf_show_end_member(show); 1331 } 1332 1333 static void *btf_show_start_array_type(struct btf_show *show, 1334 const struct btf_type *t, 1335 u32 type_id, 1336 u16 array_encoding, 1337 void *data) 1338 { 1339 show->state.array_encoding = array_encoding; 1340 show->state.array_terminated = 0; 1341 return btf_show_start_aggr_type(show, t, type_id, data); 1342 } 1343 1344 static void btf_show_end_array_type(struct btf_show *show) 1345 { 1346 show->state.array_encoding = 0; 1347 show->state.array_terminated = 0; 1348 btf_show_end_aggr_type(show, "]"); 1349 } 1350 1351 static void *btf_show_start_struct_type(struct btf_show *show, 1352 const struct btf_type *t, 1353 u32 type_id, 1354 void *data) 1355 { 1356 return btf_show_start_aggr_type(show, t, type_id, data); 1357 } 1358 1359 static void btf_show_end_struct_type(struct btf_show *show) 1360 { 1361 btf_show_end_aggr_type(show, "}"); 1362 } 1363 1364 __printf(2, 3) static void __btf_verifier_log(struct bpf_verifier_log *log, 1365 const char *fmt, ...) 1366 { 1367 va_list args; 1368 1369 va_start(args, fmt); 1370 bpf_verifier_vlog(log, fmt, args); 1371 va_end(args); 1372 } 1373 1374 __printf(2, 3) static void btf_verifier_log(struct btf_verifier_env *env, 1375 const char *fmt, ...) 1376 { 1377 struct bpf_verifier_log *log = &env->log; 1378 va_list args; 1379 1380 if (!bpf_verifier_log_needed(log)) 1381 return; 1382 1383 va_start(args, fmt); 1384 bpf_verifier_vlog(log, fmt, args); 1385 va_end(args); 1386 } 1387 1388 __printf(4, 5) static void __btf_verifier_log_type(struct btf_verifier_env *env, 1389 const struct btf_type *t, 1390 bool log_details, 1391 const char *fmt, ...) 1392 { 1393 struct bpf_verifier_log *log = &env->log; 1394 struct btf *btf = env->btf; 1395 va_list args; 1396 1397 if (!bpf_verifier_log_needed(log)) 1398 return; 1399 1400 /* btf verifier prints all types it is processing via 1401 * btf_verifier_log_type(..., fmt = NULL). 1402 * Skip those prints for in-kernel BTF verification. 1403 */ 1404 if (log->level == BPF_LOG_KERNEL && !fmt) 1405 return; 1406 1407 __btf_verifier_log(log, "[%u] %s %s%s", 1408 env->log_type_id, 1409 btf_type_str(t), 1410 __btf_name_by_offset(btf, t->name_off), 1411 log_details ? " " : ""); 1412 1413 if (log_details) 1414 btf_type_ops(t)->log_details(env, t); 1415 1416 if (fmt && *fmt) { 1417 __btf_verifier_log(log, " "); 1418 va_start(args, fmt); 1419 bpf_verifier_vlog(log, fmt, args); 1420 va_end(args); 1421 } 1422 1423 __btf_verifier_log(log, "\n"); 1424 } 1425 1426 #define btf_verifier_log_type(env, t, ...) \ 1427 __btf_verifier_log_type((env), (t), true, __VA_ARGS__) 1428 #define btf_verifier_log_basic(env, t, ...) \ 1429 __btf_verifier_log_type((env), (t), false, __VA_ARGS__) 1430 1431 __printf(4, 5) 1432 static void btf_verifier_log_member(struct btf_verifier_env *env, 1433 const struct btf_type *struct_type, 1434 const struct btf_member *member, 1435 const char *fmt, ...) 1436 { 1437 struct bpf_verifier_log *log = &env->log; 1438 struct btf *btf = env->btf; 1439 va_list args; 1440 1441 if (!bpf_verifier_log_needed(log)) 1442 return; 1443 1444 if (log->level == BPF_LOG_KERNEL && !fmt) 1445 return; 1446 /* The CHECK_META phase already did a btf dump. 1447 * 1448 * If member is logged again, it must hit an error in 1449 * parsing this member. It is useful to print out which 1450 * struct this member belongs to. 1451 */ 1452 if (env->phase != CHECK_META) 1453 btf_verifier_log_type(env, struct_type, NULL); 1454 1455 if (btf_type_kflag(struct_type)) 1456 __btf_verifier_log(log, 1457 "\t%s type_id=%u bitfield_size=%u bits_offset=%u", 1458 __btf_name_by_offset(btf, member->name_off), 1459 member->type, 1460 BTF_MEMBER_BITFIELD_SIZE(member->offset), 1461 BTF_MEMBER_BIT_OFFSET(member->offset)); 1462 else 1463 __btf_verifier_log(log, "\t%s type_id=%u bits_offset=%u", 1464 __btf_name_by_offset(btf, member->name_off), 1465 member->type, member->offset); 1466 1467 if (fmt && *fmt) { 1468 __btf_verifier_log(log, " "); 1469 va_start(args, fmt); 1470 bpf_verifier_vlog(log, fmt, args); 1471 va_end(args); 1472 } 1473 1474 __btf_verifier_log(log, "\n"); 1475 } 1476 1477 __printf(4, 5) 1478 static void btf_verifier_log_vsi(struct btf_verifier_env *env, 1479 const struct btf_type *datasec_type, 1480 const struct btf_var_secinfo *vsi, 1481 const char *fmt, ...) 1482 { 1483 struct bpf_verifier_log *log = &env->log; 1484 va_list args; 1485 1486 if (!bpf_verifier_log_needed(log)) 1487 return; 1488 if (log->level == BPF_LOG_KERNEL && !fmt) 1489 return; 1490 if (env->phase != CHECK_META) 1491 btf_verifier_log_type(env, datasec_type, NULL); 1492 1493 __btf_verifier_log(log, "\t type_id=%u offset=%u size=%u", 1494 vsi->type, vsi->offset, vsi->size); 1495 if (fmt && *fmt) { 1496 __btf_verifier_log(log, " "); 1497 va_start(args, fmt); 1498 bpf_verifier_vlog(log, fmt, args); 1499 va_end(args); 1500 } 1501 1502 __btf_verifier_log(log, "\n"); 1503 } 1504 1505 static void btf_verifier_log_hdr(struct btf_verifier_env *env, 1506 u32 btf_data_size) 1507 { 1508 struct bpf_verifier_log *log = &env->log; 1509 const struct btf *btf = env->btf; 1510 const struct btf_header *hdr; 1511 1512 if (!bpf_verifier_log_needed(log)) 1513 return; 1514 1515 if (log->level == BPF_LOG_KERNEL) 1516 return; 1517 hdr = &btf->hdr; 1518 __btf_verifier_log(log, "magic: 0x%x\n", hdr->magic); 1519 __btf_verifier_log(log, "version: %u\n", hdr->version); 1520 __btf_verifier_log(log, "flags: 0x%x\n", hdr->flags); 1521 __btf_verifier_log(log, "hdr_len: %u\n", hdr->hdr_len); 1522 __btf_verifier_log(log, "type_off: %u\n", hdr->type_off); 1523 __btf_verifier_log(log, "type_len: %u\n", hdr->type_len); 1524 __btf_verifier_log(log, "str_off: %u\n", hdr->str_off); 1525 __btf_verifier_log(log, "str_len: %u\n", hdr->str_len); 1526 __btf_verifier_log(log, "btf_total_size: %u\n", btf_data_size); 1527 } 1528 1529 static int btf_add_type(struct btf_verifier_env *env, struct btf_type *t) 1530 { 1531 struct btf *btf = env->btf; 1532 1533 if (btf->types_size == btf->nr_types) { 1534 /* Expand 'types' array */ 1535 1536 struct btf_type **new_types; 1537 u32 expand_by, new_size; 1538 1539 if (btf->start_id + btf->types_size == BTF_MAX_TYPE) { 1540 btf_verifier_log(env, "Exceeded max num of types"); 1541 return -E2BIG; 1542 } 1543 1544 expand_by = max_t(u32, btf->types_size >> 2, 16); 1545 new_size = min_t(u32, BTF_MAX_TYPE, 1546 btf->types_size + expand_by); 1547 1548 new_types = kvcalloc(new_size, sizeof(*new_types), 1549 GFP_KERNEL | __GFP_NOWARN); 1550 if (!new_types) 1551 return -ENOMEM; 1552 1553 if (btf->nr_types == 0) { 1554 if (!btf->base_btf) { 1555 /* lazily init VOID type */ 1556 new_types[0] = &btf_void; 1557 btf->nr_types++; 1558 } 1559 } else { 1560 memcpy(new_types, btf->types, 1561 sizeof(*btf->types) * btf->nr_types); 1562 } 1563 1564 kvfree(btf->types); 1565 btf->types = new_types; 1566 btf->types_size = new_size; 1567 } 1568 1569 btf->types[btf->nr_types++] = t; 1570 1571 return 0; 1572 } 1573 1574 static int btf_alloc_id(struct btf *btf) 1575 { 1576 int id; 1577 1578 idr_preload(GFP_KERNEL); 1579 spin_lock_bh(&btf_idr_lock); 1580 id = idr_alloc_cyclic(&btf_idr, btf, 1, INT_MAX, GFP_ATOMIC); 1581 if (id > 0) 1582 btf->id = id; 1583 spin_unlock_bh(&btf_idr_lock); 1584 idr_preload_end(); 1585 1586 if (WARN_ON_ONCE(!id)) 1587 return -ENOSPC; 1588 1589 return id > 0 ? 0 : id; 1590 } 1591 1592 static void btf_free_id(struct btf *btf) 1593 { 1594 unsigned long flags; 1595 1596 /* 1597 * In map-in-map, calling map_delete_elem() on outer 1598 * map will call bpf_map_put on the inner map. 1599 * It will then eventually call btf_free_id() 1600 * on the inner map. Some of the map_delete_elem() 1601 * implementation may have irq disabled, so 1602 * we need to use the _irqsave() version instead 1603 * of the _bh() version. 1604 */ 1605 spin_lock_irqsave(&btf_idr_lock, flags); 1606 idr_remove(&btf_idr, btf->id); 1607 spin_unlock_irqrestore(&btf_idr_lock, flags); 1608 } 1609 1610 static void btf_free_kfunc_set_tab(struct btf *btf) 1611 { 1612 struct btf_kfunc_set_tab *tab = btf->kfunc_set_tab; 1613 int hook; 1614 1615 if (!tab) 1616 return; 1617 /* For module BTF, we directly assign the sets being registered, so 1618 * there is nothing to free except kfunc_set_tab. 1619 */ 1620 if (btf_is_module(btf)) 1621 goto free_tab; 1622 for (hook = 0; hook < ARRAY_SIZE(tab->sets); hook++) 1623 kfree(tab->sets[hook]); 1624 free_tab: 1625 kfree(tab); 1626 btf->kfunc_set_tab = NULL; 1627 } 1628 1629 static void btf_free_dtor_kfunc_tab(struct btf *btf) 1630 { 1631 struct btf_id_dtor_kfunc_tab *tab = btf->dtor_kfunc_tab; 1632 1633 if (!tab) 1634 return; 1635 kfree(tab); 1636 btf->dtor_kfunc_tab = NULL; 1637 } 1638 1639 static void btf_struct_metas_free(struct btf_struct_metas *tab) 1640 { 1641 int i; 1642 1643 if (!tab) 1644 return; 1645 for (i = 0; i < tab->cnt; i++) { 1646 btf_record_free(tab->types[i].record); 1647 kfree(tab->types[i].field_offs); 1648 } 1649 kfree(tab); 1650 } 1651 1652 static void btf_free_struct_meta_tab(struct btf *btf) 1653 { 1654 struct btf_struct_metas *tab = btf->struct_meta_tab; 1655 1656 btf_struct_metas_free(tab); 1657 btf->struct_meta_tab = NULL; 1658 } 1659 1660 static void btf_free(struct btf *btf) 1661 { 1662 btf_free_struct_meta_tab(btf); 1663 btf_free_dtor_kfunc_tab(btf); 1664 btf_free_kfunc_set_tab(btf); 1665 kvfree(btf->types); 1666 kvfree(btf->resolved_sizes); 1667 kvfree(btf->resolved_ids); 1668 kvfree(btf->data); 1669 kfree(btf); 1670 } 1671 1672 static void btf_free_rcu(struct rcu_head *rcu) 1673 { 1674 struct btf *btf = container_of(rcu, struct btf, rcu); 1675 1676 btf_free(btf); 1677 } 1678 1679 void btf_get(struct btf *btf) 1680 { 1681 refcount_inc(&btf->refcnt); 1682 } 1683 1684 void btf_put(struct btf *btf) 1685 { 1686 if (btf && refcount_dec_and_test(&btf->refcnt)) { 1687 btf_free_id(btf); 1688 call_rcu(&btf->rcu, btf_free_rcu); 1689 } 1690 } 1691 1692 static int env_resolve_init(struct btf_verifier_env *env) 1693 { 1694 struct btf *btf = env->btf; 1695 u32 nr_types = btf->nr_types; 1696 u32 *resolved_sizes = NULL; 1697 u32 *resolved_ids = NULL; 1698 u8 *visit_states = NULL; 1699 1700 resolved_sizes = kvcalloc(nr_types, sizeof(*resolved_sizes), 1701 GFP_KERNEL | __GFP_NOWARN); 1702 if (!resolved_sizes) 1703 goto nomem; 1704 1705 resolved_ids = kvcalloc(nr_types, sizeof(*resolved_ids), 1706 GFP_KERNEL | __GFP_NOWARN); 1707 if (!resolved_ids) 1708 goto nomem; 1709 1710 visit_states = kvcalloc(nr_types, sizeof(*visit_states), 1711 GFP_KERNEL | __GFP_NOWARN); 1712 if (!visit_states) 1713 goto nomem; 1714 1715 btf->resolved_sizes = resolved_sizes; 1716 btf->resolved_ids = resolved_ids; 1717 env->visit_states = visit_states; 1718 1719 return 0; 1720 1721 nomem: 1722 kvfree(resolved_sizes); 1723 kvfree(resolved_ids); 1724 kvfree(visit_states); 1725 return -ENOMEM; 1726 } 1727 1728 static void btf_verifier_env_free(struct btf_verifier_env *env) 1729 { 1730 kvfree(env->visit_states); 1731 kfree(env); 1732 } 1733 1734 static bool env_type_is_resolve_sink(const struct btf_verifier_env *env, 1735 const struct btf_type *next_type) 1736 { 1737 switch (env->resolve_mode) { 1738 case RESOLVE_TBD: 1739 /* int, enum or void is a sink */ 1740 return !btf_type_needs_resolve(next_type); 1741 case RESOLVE_PTR: 1742 /* int, enum, void, struct, array, func or func_proto is a sink 1743 * for ptr 1744 */ 1745 return !btf_type_is_modifier(next_type) && 1746 !btf_type_is_ptr(next_type); 1747 case RESOLVE_STRUCT_OR_ARRAY: 1748 /* int, enum, void, ptr, func or func_proto is a sink 1749 * for struct and array 1750 */ 1751 return !btf_type_is_modifier(next_type) && 1752 !btf_type_is_array(next_type) && 1753 !btf_type_is_struct(next_type); 1754 default: 1755 BUG(); 1756 } 1757 } 1758 1759 static bool env_type_is_resolved(const struct btf_verifier_env *env, 1760 u32 type_id) 1761 { 1762 /* base BTF types should be resolved by now */ 1763 if (type_id < env->btf->start_id) 1764 return true; 1765 1766 return env->visit_states[type_id - env->btf->start_id] == RESOLVED; 1767 } 1768 1769 static int env_stack_push(struct btf_verifier_env *env, 1770 const struct btf_type *t, u32 type_id) 1771 { 1772 const struct btf *btf = env->btf; 1773 struct resolve_vertex *v; 1774 1775 if (env->top_stack == MAX_RESOLVE_DEPTH) 1776 return -E2BIG; 1777 1778 if (type_id < btf->start_id 1779 || env->visit_states[type_id - btf->start_id] != NOT_VISITED) 1780 return -EEXIST; 1781 1782 env->visit_states[type_id - btf->start_id] = VISITED; 1783 1784 v = &env->stack[env->top_stack++]; 1785 v->t = t; 1786 v->type_id = type_id; 1787 v->next_member = 0; 1788 1789 if (env->resolve_mode == RESOLVE_TBD) { 1790 if (btf_type_is_ptr(t)) 1791 env->resolve_mode = RESOLVE_PTR; 1792 else if (btf_type_is_struct(t) || btf_type_is_array(t)) 1793 env->resolve_mode = RESOLVE_STRUCT_OR_ARRAY; 1794 } 1795 1796 return 0; 1797 } 1798 1799 static void env_stack_set_next_member(struct btf_verifier_env *env, 1800 u16 next_member) 1801 { 1802 env->stack[env->top_stack - 1].next_member = next_member; 1803 } 1804 1805 static void env_stack_pop_resolved(struct btf_verifier_env *env, 1806 u32 resolved_type_id, 1807 u32 resolved_size) 1808 { 1809 u32 type_id = env->stack[--(env->top_stack)].type_id; 1810 struct btf *btf = env->btf; 1811 1812 type_id -= btf->start_id; /* adjust to local type id */ 1813 btf->resolved_sizes[type_id] = resolved_size; 1814 btf->resolved_ids[type_id] = resolved_type_id; 1815 env->visit_states[type_id] = RESOLVED; 1816 } 1817 1818 static const struct resolve_vertex *env_stack_peak(struct btf_verifier_env *env) 1819 { 1820 return env->top_stack ? &env->stack[env->top_stack - 1] : NULL; 1821 } 1822 1823 /* Resolve the size of a passed-in "type" 1824 * 1825 * type: is an array (e.g. u32 array[x][y]) 1826 * return type: type "u32[x][y]", i.e. BTF_KIND_ARRAY, 1827 * *type_size: (x * y * sizeof(u32)). Hence, *type_size always 1828 * corresponds to the return type. 1829 * *elem_type: u32 1830 * *elem_id: id of u32 1831 * *total_nelems: (x * y). Hence, individual elem size is 1832 * (*type_size / *total_nelems) 1833 * *type_id: id of type if it's changed within the function, 0 if not 1834 * 1835 * type: is not an array (e.g. const struct X) 1836 * return type: type "struct X" 1837 * *type_size: sizeof(struct X) 1838 * *elem_type: same as return type ("struct X") 1839 * *elem_id: 0 1840 * *total_nelems: 1 1841 * *type_id: id of type if it's changed within the function, 0 if not 1842 */ 1843 static const struct btf_type * 1844 __btf_resolve_size(const struct btf *btf, const struct btf_type *type, 1845 u32 *type_size, const struct btf_type **elem_type, 1846 u32 *elem_id, u32 *total_nelems, u32 *type_id) 1847 { 1848 const struct btf_type *array_type = NULL; 1849 const struct btf_array *array = NULL; 1850 u32 i, size, nelems = 1, id = 0; 1851 1852 for (i = 0; i < MAX_RESOLVE_DEPTH; i++) { 1853 switch (BTF_INFO_KIND(type->info)) { 1854 /* type->size can be used */ 1855 case BTF_KIND_INT: 1856 case BTF_KIND_STRUCT: 1857 case BTF_KIND_UNION: 1858 case BTF_KIND_ENUM: 1859 case BTF_KIND_FLOAT: 1860 case BTF_KIND_ENUM64: 1861 size = type->size; 1862 goto resolved; 1863 1864 case BTF_KIND_PTR: 1865 size = sizeof(void *); 1866 goto resolved; 1867 1868 /* Modifiers */ 1869 case BTF_KIND_TYPEDEF: 1870 case BTF_KIND_VOLATILE: 1871 case BTF_KIND_CONST: 1872 case BTF_KIND_RESTRICT: 1873 case BTF_KIND_TYPE_TAG: 1874 id = type->type; 1875 type = btf_type_by_id(btf, type->type); 1876 break; 1877 1878 case BTF_KIND_ARRAY: 1879 if (!array_type) 1880 array_type = type; 1881 array = btf_type_array(type); 1882 if (nelems && array->nelems > U32_MAX / nelems) 1883 return ERR_PTR(-EINVAL); 1884 nelems *= array->nelems; 1885 type = btf_type_by_id(btf, array->type); 1886 break; 1887 1888 /* type without size */ 1889 default: 1890 return ERR_PTR(-EINVAL); 1891 } 1892 } 1893 1894 return ERR_PTR(-EINVAL); 1895 1896 resolved: 1897 if (nelems && size > U32_MAX / nelems) 1898 return ERR_PTR(-EINVAL); 1899 1900 *type_size = nelems * size; 1901 if (total_nelems) 1902 *total_nelems = nelems; 1903 if (elem_type) 1904 *elem_type = type; 1905 if (elem_id) 1906 *elem_id = array ? array->type : 0; 1907 if (type_id && id) 1908 *type_id = id; 1909 1910 return array_type ? : type; 1911 } 1912 1913 const struct btf_type * 1914 btf_resolve_size(const struct btf *btf, const struct btf_type *type, 1915 u32 *type_size) 1916 { 1917 return __btf_resolve_size(btf, type, type_size, NULL, NULL, NULL, NULL); 1918 } 1919 1920 static u32 btf_resolved_type_id(const struct btf *btf, u32 type_id) 1921 { 1922 while (type_id < btf->start_id) 1923 btf = btf->base_btf; 1924 1925 return btf->resolved_ids[type_id - btf->start_id]; 1926 } 1927 1928 /* The input param "type_id" must point to a needs_resolve type */ 1929 static const struct btf_type *btf_type_id_resolve(const struct btf *btf, 1930 u32 *type_id) 1931 { 1932 *type_id = btf_resolved_type_id(btf, *type_id); 1933 return btf_type_by_id(btf, *type_id); 1934 } 1935 1936 static u32 btf_resolved_type_size(const struct btf *btf, u32 type_id) 1937 { 1938 while (type_id < btf->start_id) 1939 btf = btf->base_btf; 1940 1941 return btf->resolved_sizes[type_id - btf->start_id]; 1942 } 1943 1944 const struct btf_type *btf_type_id_size(const struct btf *btf, 1945 u32 *type_id, u32 *ret_size) 1946 { 1947 const struct btf_type *size_type; 1948 u32 size_type_id = *type_id; 1949 u32 size = 0; 1950 1951 size_type = btf_type_by_id(btf, size_type_id); 1952 if (btf_type_nosize_or_null(size_type)) 1953 return NULL; 1954 1955 if (btf_type_has_size(size_type)) { 1956 size = size_type->size; 1957 } else if (btf_type_is_array(size_type)) { 1958 size = btf_resolved_type_size(btf, size_type_id); 1959 } else if (btf_type_is_ptr(size_type)) { 1960 size = sizeof(void *); 1961 } else { 1962 if (WARN_ON_ONCE(!btf_type_is_modifier(size_type) && 1963 !btf_type_is_var(size_type))) 1964 return NULL; 1965 1966 size_type_id = btf_resolved_type_id(btf, size_type_id); 1967 size_type = btf_type_by_id(btf, size_type_id); 1968 if (btf_type_nosize_or_null(size_type)) 1969 return NULL; 1970 else if (btf_type_has_size(size_type)) 1971 size = size_type->size; 1972 else if (btf_type_is_array(size_type)) 1973 size = btf_resolved_type_size(btf, size_type_id); 1974 else if (btf_type_is_ptr(size_type)) 1975 size = sizeof(void *); 1976 else 1977 return NULL; 1978 } 1979 1980 *type_id = size_type_id; 1981 if (ret_size) 1982 *ret_size = size; 1983 1984 return size_type; 1985 } 1986 1987 static int btf_df_check_member(struct btf_verifier_env *env, 1988 const struct btf_type *struct_type, 1989 const struct btf_member *member, 1990 const struct btf_type *member_type) 1991 { 1992 btf_verifier_log_basic(env, struct_type, 1993 "Unsupported check_member"); 1994 return -EINVAL; 1995 } 1996 1997 static int btf_df_check_kflag_member(struct btf_verifier_env *env, 1998 const struct btf_type *struct_type, 1999 const struct btf_member *member, 2000 const struct btf_type *member_type) 2001 { 2002 btf_verifier_log_basic(env, struct_type, 2003 "Unsupported check_kflag_member"); 2004 return -EINVAL; 2005 } 2006 2007 /* Used for ptr, array struct/union and float type members. 2008 * int, enum and modifier types have their specific callback functions. 2009 */ 2010 static int btf_generic_check_kflag_member(struct btf_verifier_env *env, 2011 const struct btf_type *struct_type, 2012 const struct btf_member *member, 2013 const struct btf_type *member_type) 2014 { 2015 if (BTF_MEMBER_BITFIELD_SIZE(member->offset)) { 2016 btf_verifier_log_member(env, struct_type, member, 2017 "Invalid member bitfield_size"); 2018 return -EINVAL; 2019 } 2020 2021 /* bitfield size is 0, so member->offset represents bit offset only. 2022 * It is safe to call non kflag check_member variants. 2023 */ 2024 return btf_type_ops(member_type)->check_member(env, struct_type, 2025 member, 2026 member_type); 2027 } 2028 2029 static int btf_df_resolve(struct btf_verifier_env *env, 2030 const struct resolve_vertex *v) 2031 { 2032 btf_verifier_log_basic(env, v->t, "Unsupported resolve"); 2033 return -EINVAL; 2034 } 2035 2036 static void btf_df_show(const struct btf *btf, const struct btf_type *t, 2037 u32 type_id, void *data, u8 bits_offsets, 2038 struct btf_show *show) 2039 { 2040 btf_show(show, "<unsupported kind:%u>", BTF_INFO_KIND(t->info)); 2041 } 2042 2043 static int btf_int_check_member(struct btf_verifier_env *env, 2044 const struct btf_type *struct_type, 2045 const struct btf_member *member, 2046 const struct btf_type *member_type) 2047 { 2048 u32 int_data = btf_type_int(member_type); 2049 u32 struct_bits_off = member->offset; 2050 u32 struct_size = struct_type->size; 2051 u32 nr_copy_bits; 2052 u32 bytes_offset; 2053 2054 if (U32_MAX - struct_bits_off < BTF_INT_OFFSET(int_data)) { 2055 btf_verifier_log_member(env, struct_type, member, 2056 "bits_offset exceeds U32_MAX"); 2057 return -EINVAL; 2058 } 2059 2060 struct_bits_off += BTF_INT_OFFSET(int_data); 2061 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off); 2062 nr_copy_bits = BTF_INT_BITS(int_data) + 2063 BITS_PER_BYTE_MASKED(struct_bits_off); 2064 2065 if (nr_copy_bits > BITS_PER_U128) { 2066 btf_verifier_log_member(env, struct_type, member, 2067 "nr_copy_bits exceeds 128"); 2068 return -EINVAL; 2069 } 2070 2071 if (struct_size < bytes_offset || 2072 struct_size - bytes_offset < BITS_ROUNDUP_BYTES(nr_copy_bits)) { 2073 btf_verifier_log_member(env, struct_type, member, 2074 "Member exceeds struct_size"); 2075 return -EINVAL; 2076 } 2077 2078 return 0; 2079 } 2080 2081 static int btf_int_check_kflag_member(struct btf_verifier_env *env, 2082 const struct btf_type *struct_type, 2083 const struct btf_member *member, 2084 const struct btf_type *member_type) 2085 { 2086 u32 struct_bits_off, nr_bits, nr_int_data_bits, bytes_offset; 2087 u32 int_data = btf_type_int(member_type); 2088 u32 struct_size = struct_type->size; 2089 u32 nr_copy_bits; 2090 2091 /* a regular int type is required for the kflag int member */ 2092 if (!btf_type_int_is_regular(member_type)) { 2093 btf_verifier_log_member(env, struct_type, member, 2094 "Invalid member base type"); 2095 return -EINVAL; 2096 } 2097 2098 /* check sanity of bitfield size */ 2099 nr_bits = BTF_MEMBER_BITFIELD_SIZE(member->offset); 2100 struct_bits_off = BTF_MEMBER_BIT_OFFSET(member->offset); 2101 nr_int_data_bits = BTF_INT_BITS(int_data); 2102 if (!nr_bits) { 2103 /* Not a bitfield member, member offset must be at byte 2104 * boundary. 2105 */ 2106 if (BITS_PER_BYTE_MASKED(struct_bits_off)) { 2107 btf_verifier_log_member(env, struct_type, member, 2108 "Invalid member offset"); 2109 return -EINVAL; 2110 } 2111 2112 nr_bits = nr_int_data_bits; 2113 } else if (nr_bits > nr_int_data_bits) { 2114 btf_verifier_log_member(env, struct_type, member, 2115 "Invalid member bitfield_size"); 2116 return -EINVAL; 2117 } 2118 2119 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off); 2120 nr_copy_bits = nr_bits + BITS_PER_BYTE_MASKED(struct_bits_off); 2121 if (nr_copy_bits > BITS_PER_U128) { 2122 btf_verifier_log_member(env, struct_type, member, 2123 "nr_copy_bits exceeds 128"); 2124 return -EINVAL; 2125 } 2126 2127 if (struct_size < bytes_offset || 2128 struct_size - bytes_offset < BITS_ROUNDUP_BYTES(nr_copy_bits)) { 2129 btf_verifier_log_member(env, struct_type, member, 2130 "Member exceeds struct_size"); 2131 return -EINVAL; 2132 } 2133 2134 return 0; 2135 } 2136 2137 static s32 btf_int_check_meta(struct btf_verifier_env *env, 2138 const struct btf_type *t, 2139 u32 meta_left) 2140 { 2141 u32 int_data, nr_bits, meta_needed = sizeof(int_data); 2142 u16 encoding; 2143 2144 if (meta_left < meta_needed) { 2145 btf_verifier_log_basic(env, t, 2146 "meta_left:%u meta_needed:%u", 2147 meta_left, meta_needed); 2148 return -EINVAL; 2149 } 2150 2151 if (btf_type_vlen(t)) { 2152 btf_verifier_log_type(env, t, "vlen != 0"); 2153 return -EINVAL; 2154 } 2155 2156 if (btf_type_kflag(t)) { 2157 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); 2158 return -EINVAL; 2159 } 2160 2161 int_data = btf_type_int(t); 2162 if (int_data & ~BTF_INT_MASK) { 2163 btf_verifier_log_basic(env, t, "Invalid int_data:%x", 2164 int_data); 2165 return -EINVAL; 2166 } 2167 2168 nr_bits = BTF_INT_BITS(int_data) + BTF_INT_OFFSET(int_data); 2169 2170 if (nr_bits > BITS_PER_U128) { 2171 btf_verifier_log_type(env, t, "nr_bits exceeds %zu", 2172 BITS_PER_U128); 2173 return -EINVAL; 2174 } 2175 2176 if (BITS_ROUNDUP_BYTES(nr_bits) > t->size) { 2177 btf_verifier_log_type(env, t, "nr_bits exceeds type_size"); 2178 return -EINVAL; 2179 } 2180 2181 /* 2182 * Only one of the encoding bits is allowed and it 2183 * should be sufficient for the pretty print purpose (i.e. decoding). 2184 * Multiple bits can be allowed later if it is found 2185 * to be insufficient. 2186 */ 2187 encoding = BTF_INT_ENCODING(int_data); 2188 if (encoding && 2189 encoding != BTF_INT_SIGNED && 2190 encoding != BTF_INT_CHAR && 2191 encoding != BTF_INT_BOOL) { 2192 btf_verifier_log_type(env, t, "Unsupported encoding"); 2193 return -ENOTSUPP; 2194 } 2195 2196 btf_verifier_log_type(env, t, NULL); 2197 2198 return meta_needed; 2199 } 2200 2201 static void btf_int_log(struct btf_verifier_env *env, 2202 const struct btf_type *t) 2203 { 2204 int int_data = btf_type_int(t); 2205 2206 btf_verifier_log(env, 2207 "size=%u bits_offset=%u nr_bits=%u encoding=%s", 2208 t->size, BTF_INT_OFFSET(int_data), 2209 BTF_INT_BITS(int_data), 2210 btf_int_encoding_str(BTF_INT_ENCODING(int_data))); 2211 } 2212 2213 static void btf_int128_print(struct btf_show *show, void *data) 2214 { 2215 /* data points to a __int128 number. 2216 * Suppose 2217 * int128_num = *(__int128 *)data; 2218 * The below formulas shows what upper_num and lower_num represents: 2219 * upper_num = int128_num >> 64; 2220 * lower_num = int128_num & 0xffffffffFFFFFFFFULL; 2221 */ 2222 u64 upper_num, lower_num; 2223 2224 #ifdef __BIG_ENDIAN_BITFIELD 2225 upper_num = *(u64 *)data; 2226 lower_num = *(u64 *)(data + 8); 2227 #else 2228 upper_num = *(u64 *)(data + 8); 2229 lower_num = *(u64 *)data; 2230 #endif 2231 if (upper_num == 0) 2232 btf_show_type_value(show, "0x%llx", lower_num); 2233 else 2234 btf_show_type_values(show, "0x%llx%016llx", upper_num, 2235 lower_num); 2236 } 2237 2238 static void btf_int128_shift(u64 *print_num, u16 left_shift_bits, 2239 u16 right_shift_bits) 2240 { 2241 u64 upper_num, lower_num; 2242 2243 #ifdef __BIG_ENDIAN_BITFIELD 2244 upper_num = print_num[0]; 2245 lower_num = print_num[1]; 2246 #else 2247 upper_num = print_num[1]; 2248 lower_num = print_num[0]; 2249 #endif 2250 2251 /* shake out un-needed bits by shift/or operations */ 2252 if (left_shift_bits >= 64) { 2253 upper_num = lower_num << (left_shift_bits - 64); 2254 lower_num = 0; 2255 } else { 2256 upper_num = (upper_num << left_shift_bits) | 2257 (lower_num >> (64 - left_shift_bits)); 2258 lower_num = lower_num << left_shift_bits; 2259 } 2260 2261 if (right_shift_bits >= 64) { 2262 lower_num = upper_num >> (right_shift_bits - 64); 2263 upper_num = 0; 2264 } else { 2265 lower_num = (lower_num >> right_shift_bits) | 2266 (upper_num << (64 - right_shift_bits)); 2267 upper_num = upper_num >> right_shift_bits; 2268 } 2269 2270 #ifdef __BIG_ENDIAN_BITFIELD 2271 print_num[0] = upper_num; 2272 print_num[1] = lower_num; 2273 #else 2274 print_num[0] = lower_num; 2275 print_num[1] = upper_num; 2276 #endif 2277 } 2278 2279 static void btf_bitfield_show(void *data, u8 bits_offset, 2280 u8 nr_bits, struct btf_show *show) 2281 { 2282 u16 left_shift_bits, right_shift_bits; 2283 u8 nr_copy_bytes; 2284 u8 nr_copy_bits; 2285 u64 print_num[2] = {}; 2286 2287 nr_copy_bits = nr_bits + bits_offset; 2288 nr_copy_bytes = BITS_ROUNDUP_BYTES(nr_copy_bits); 2289 2290 memcpy(print_num, data, nr_copy_bytes); 2291 2292 #ifdef __BIG_ENDIAN_BITFIELD 2293 left_shift_bits = bits_offset; 2294 #else 2295 left_shift_bits = BITS_PER_U128 - nr_copy_bits; 2296 #endif 2297 right_shift_bits = BITS_PER_U128 - nr_bits; 2298 2299 btf_int128_shift(print_num, left_shift_bits, right_shift_bits); 2300 btf_int128_print(show, print_num); 2301 } 2302 2303 2304 static void btf_int_bits_show(const struct btf *btf, 2305 const struct btf_type *t, 2306 void *data, u8 bits_offset, 2307 struct btf_show *show) 2308 { 2309 u32 int_data = btf_type_int(t); 2310 u8 nr_bits = BTF_INT_BITS(int_data); 2311 u8 total_bits_offset; 2312 2313 /* 2314 * bits_offset is at most 7. 2315 * BTF_INT_OFFSET() cannot exceed 128 bits. 2316 */ 2317 total_bits_offset = bits_offset + BTF_INT_OFFSET(int_data); 2318 data += BITS_ROUNDDOWN_BYTES(total_bits_offset); 2319 bits_offset = BITS_PER_BYTE_MASKED(total_bits_offset); 2320 btf_bitfield_show(data, bits_offset, nr_bits, show); 2321 } 2322 2323 static void btf_int_show(const struct btf *btf, const struct btf_type *t, 2324 u32 type_id, void *data, u8 bits_offset, 2325 struct btf_show *show) 2326 { 2327 u32 int_data = btf_type_int(t); 2328 u8 encoding = BTF_INT_ENCODING(int_data); 2329 bool sign = encoding & BTF_INT_SIGNED; 2330 u8 nr_bits = BTF_INT_BITS(int_data); 2331 void *safe_data; 2332 2333 safe_data = btf_show_start_type(show, t, type_id, data); 2334 if (!safe_data) 2335 return; 2336 2337 if (bits_offset || BTF_INT_OFFSET(int_data) || 2338 BITS_PER_BYTE_MASKED(nr_bits)) { 2339 btf_int_bits_show(btf, t, safe_data, bits_offset, show); 2340 goto out; 2341 } 2342 2343 switch (nr_bits) { 2344 case 128: 2345 btf_int128_print(show, safe_data); 2346 break; 2347 case 64: 2348 if (sign) 2349 btf_show_type_value(show, "%lld", *(s64 *)safe_data); 2350 else 2351 btf_show_type_value(show, "%llu", *(u64 *)safe_data); 2352 break; 2353 case 32: 2354 if (sign) 2355 btf_show_type_value(show, "%d", *(s32 *)safe_data); 2356 else 2357 btf_show_type_value(show, "%u", *(u32 *)safe_data); 2358 break; 2359 case 16: 2360 if (sign) 2361 btf_show_type_value(show, "%d", *(s16 *)safe_data); 2362 else 2363 btf_show_type_value(show, "%u", *(u16 *)safe_data); 2364 break; 2365 case 8: 2366 if (show->state.array_encoding == BTF_INT_CHAR) { 2367 /* check for null terminator */ 2368 if (show->state.array_terminated) 2369 break; 2370 if (*(char *)data == '\0') { 2371 show->state.array_terminated = 1; 2372 break; 2373 } 2374 if (isprint(*(char *)data)) { 2375 btf_show_type_value(show, "'%c'", 2376 *(char *)safe_data); 2377 break; 2378 } 2379 } 2380 if (sign) 2381 btf_show_type_value(show, "%d", *(s8 *)safe_data); 2382 else 2383 btf_show_type_value(show, "%u", *(u8 *)safe_data); 2384 break; 2385 default: 2386 btf_int_bits_show(btf, t, safe_data, bits_offset, show); 2387 break; 2388 } 2389 out: 2390 btf_show_end_type(show); 2391 } 2392 2393 static const struct btf_kind_operations int_ops = { 2394 .check_meta = btf_int_check_meta, 2395 .resolve = btf_df_resolve, 2396 .check_member = btf_int_check_member, 2397 .check_kflag_member = btf_int_check_kflag_member, 2398 .log_details = btf_int_log, 2399 .show = btf_int_show, 2400 }; 2401 2402 static int btf_modifier_check_member(struct btf_verifier_env *env, 2403 const struct btf_type *struct_type, 2404 const struct btf_member *member, 2405 const struct btf_type *member_type) 2406 { 2407 const struct btf_type *resolved_type; 2408 u32 resolved_type_id = member->type; 2409 struct btf_member resolved_member; 2410 struct btf *btf = env->btf; 2411 2412 resolved_type = btf_type_id_size(btf, &resolved_type_id, NULL); 2413 if (!resolved_type) { 2414 btf_verifier_log_member(env, struct_type, member, 2415 "Invalid member"); 2416 return -EINVAL; 2417 } 2418 2419 resolved_member = *member; 2420 resolved_member.type = resolved_type_id; 2421 2422 return btf_type_ops(resolved_type)->check_member(env, struct_type, 2423 &resolved_member, 2424 resolved_type); 2425 } 2426 2427 static int btf_modifier_check_kflag_member(struct btf_verifier_env *env, 2428 const struct btf_type *struct_type, 2429 const struct btf_member *member, 2430 const struct btf_type *member_type) 2431 { 2432 const struct btf_type *resolved_type; 2433 u32 resolved_type_id = member->type; 2434 struct btf_member resolved_member; 2435 struct btf *btf = env->btf; 2436 2437 resolved_type = btf_type_id_size(btf, &resolved_type_id, NULL); 2438 if (!resolved_type) { 2439 btf_verifier_log_member(env, struct_type, member, 2440 "Invalid member"); 2441 return -EINVAL; 2442 } 2443 2444 resolved_member = *member; 2445 resolved_member.type = resolved_type_id; 2446 2447 return btf_type_ops(resolved_type)->check_kflag_member(env, struct_type, 2448 &resolved_member, 2449 resolved_type); 2450 } 2451 2452 static int btf_ptr_check_member(struct btf_verifier_env *env, 2453 const struct btf_type *struct_type, 2454 const struct btf_member *member, 2455 const struct btf_type *member_type) 2456 { 2457 u32 struct_size, struct_bits_off, bytes_offset; 2458 2459 struct_size = struct_type->size; 2460 struct_bits_off = member->offset; 2461 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off); 2462 2463 if (BITS_PER_BYTE_MASKED(struct_bits_off)) { 2464 btf_verifier_log_member(env, struct_type, member, 2465 "Member is not byte aligned"); 2466 return -EINVAL; 2467 } 2468 2469 if (struct_size - bytes_offset < sizeof(void *)) { 2470 btf_verifier_log_member(env, struct_type, member, 2471 "Member exceeds struct_size"); 2472 return -EINVAL; 2473 } 2474 2475 return 0; 2476 } 2477 2478 static int btf_ref_type_check_meta(struct btf_verifier_env *env, 2479 const struct btf_type *t, 2480 u32 meta_left) 2481 { 2482 const char *value; 2483 2484 if (btf_type_vlen(t)) { 2485 btf_verifier_log_type(env, t, "vlen != 0"); 2486 return -EINVAL; 2487 } 2488 2489 if (btf_type_kflag(t)) { 2490 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); 2491 return -EINVAL; 2492 } 2493 2494 if (!BTF_TYPE_ID_VALID(t->type)) { 2495 btf_verifier_log_type(env, t, "Invalid type_id"); 2496 return -EINVAL; 2497 } 2498 2499 /* typedef/type_tag type must have a valid name, and other ref types, 2500 * volatile, const, restrict, should have a null name. 2501 */ 2502 if (BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF) { 2503 if (!t->name_off || 2504 !btf_name_valid_identifier(env->btf, t->name_off)) { 2505 btf_verifier_log_type(env, t, "Invalid name"); 2506 return -EINVAL; 2507 } 2508 } else if (BTF_INFO_KIND(t->info) == BTF_KIND_TYPE_TAG) { 2509 value = btf_name_by_offset(env->btf, t->name_off); 2510 if (!value || !value[0]) { 2511 btf_verifier_log_type(env, t, "Invalid name"); 2512 return -EINVAL; 2513 } 2514 } else { 2515 if (t->name_off) { 2516 btf_verifier_log_type(env, t, "Invalid name"); 2517 return -EINVAL; 2518 } 2519 } 2520 2521 btf_verifier_log_type(env, t, NULL); 2522 2523 return 0; 2524 } 2525 2526 static int btf_modifier_resolve(struct btf_verifier_env *env, 2527 const struct resolve_vertex *v) 2528 { 2529 const struct btf_type *t = v->t; 2530 const struct btf_type *next_type; 2531 u32 next_type_id = t->type; 2532 struct btf *btf = env->btf; 2533 2534 next_type = btf_type_by_id(btf, next_type_id); 2535 if (!next_type || btf_type_is_resolve_source_only(next_type)) { 2536 btf_verifier_log_type(env, v->t, "Invalid type_id"); 2537 return -EINVAL; 2538 } 2539 2540 if (!env_type_is_resolve_sink(env, next_type) && 2541 !env_type_is_resolved(env, next_type_id)) 2542 return env_stack_push(env, next_type, next_type_id); 2543 2544 /* Figure out the resolved next_type_id with size. 2545 * They will be stored in the current modifier's 2546 * resolved_ids and resolved_sizes such that it can 2547 * save us a few type-following when we use it later (e.g. in 2548 * pretty print). 2549 */ 2550 if (!btf_type_id_size(btf, &next_type_id, NULL)) { 2551 if (env_type_is_resolved(env, next_type_id)) 2552 next_type = btf_type_id_resolve(btf, &next_type_id); 2553 2554 /* "typedef void new_void", "const void"...etc */ 2555 if (!btf_type_is_void(next_type) && 2556 !btf_type_is_fwd(next_type) && 2557 !btf_type_is_func_proto(next_type)) { 2558 btf_verifier_log_type(env, v->t, "Invalid type_id"); 2559 return -EINVAL; 2560 } 2561 } 2562 2563 env_stack_pop_resolved(env, next_type_id, 0); 2564 2565 return 0; 2566 } 2567 2568 static int btf_var_resolve(struct btf_verifier_env *env, 2569 const struct resolve_vertex *v) 2570 { 2571 const struct btf_type *next_type; 2572 const struct btf_type *t = v->t; 2573 u32 next_type_id = t->type; 2574 struct btf *btf = env->btf; 2575 2576 next_type = btf_type_by_id(btf, next_type_id); 2577 if (!next_type || btf_type_is_resolve_source_only(next_type)) { 2578 btf_verifier_log_type(env, v->t, "Invalid type_id"); 2579 return -EINVAL; 2580 } 2581 2582 if (!env_type_is_resolve_sink(env, next_type) && 2583 !env_type_is_resolved(env, next_type_id)) 2584 return env_stack_push(env, next_type, next_type_id); 2585 2586 if (btf_type_is_modifier(next_type)) { 2587 const struct btf_type *resolved_type; 2588 u32 resolved_type_id; 2589 2590 resolved_type_id = next_type_id; 2591 resolved_type = btf_type_id_resolve(btf, &resolved_type_id); 2592 2593 if (btf_type_is_ptr(resolved_type) && 2594 !env_type_is_resolve_sink(env, resolved_type) && 2595 !env_type_is_resolved(env, resolved_type_id)) 2596 return env_stack_push(env, resolved_type, 2597 resolved_type_id); 2598 } 2599 2600 /* We must resolve to something concrete at this point, no 2601 * forward types or similar that would resolve to size of 2602 * zero is allowed. 2603 */ 2604 if (!btf_type_id_size(btf, &next_type_id, NULL)) { 2605 btf_verifier_log_type(env, v->t, "Invalid type_id"); 2606 return -EINVAL; 2607 } 2608 2609 env_stack_pop_resolved(env, next_type_id, 0); 2610 2611 return 0; 2612 } 2613 2614 static int btf_ptr_resolve(struct btf_verifier_env *env, 2615 const struct resolve_vertex *v) 2616 { 2617 const struct btf_type *next_type; 2618 const struct btf_type *t = v->t; 2619 u32 next_type_id = t->type; 2620 struct btf *btf = env->btf; 2621 2622 next_type = btf_type_by_id(btf, next_type_id); 2623 if (!next_type || btf_type_is_resolve_source_only(next_type)) { 2624 btf_verifier_log_type(env, v->t, "Invalid type_id"); 2625 return -EINVAL; 2626 } 2627 2628 if (!env_type_is_resolve_sink(env, next_type) && 2629 !env_type_is_resolved(env, next_type_id)) 2630 return env_stack_push(env, next_type, next_type_id); 2631 2632 /* If the modifier was RESOLVED during RESOLVE_STRUCT_OR_ARRAY, 2633 * the modifier may have stopped resolving when it was resolved 2634 * to a ptr (last-resolved-ptr). 2635 * 2636 * We now need to continue from the last-resolved-ptr to 2637 * ensure the last-resolved-ptr will not referring back to 2638 * the current ptr (t). 2639 */ 2640 if (btf_type_is_modifier(next_type)) { 2641 const struct btf_type *resolved_type; 2642 u32 resolved_type_id; 2643 2644 resolved_type_id = next_type_id; 2645 resolved_type = btf_type_id_resolve(btf, &resolved_type_id); 2646 2647 if (btf_type_is_ptr(resolved_type) && 2648 !env_type_is_resolve_sink(env, resolved_type) && 2649 !env_type_is_resolved(env, resolved_type_id)) 2650 return env_stack_push(env, resolved_type, 2651 resolved_type_id); 2652 } 2653 2654 if (!btf_type_id_size(btf, &next_type_id, NULL)) { 2655 if (env_type_is_resolved(env, next_type_id)) 2656 next_type = btf_type_id_resolve(btf, &next_type_id); 2657 2658 if (!btf_type_is_void(next_type) && 2659 !btf_type_is_fwd(next_type) && 2660 !btf_type_is_func_proto(next_type)) { 2661 btf_verifier_log_type(env, v->t, "Invalid type_id"); 2662 return -EINVAL; 2663 } 2664 } 2665 2666 env_stack_pop_resolved(env, next_type_id, 0); 2667 2668 return 0; 2669 } 2670 2671 static void btf_modifier_show(const struct btf *btf, 2672 const struct btf_type *t, 2673 u32 type_id, void *data, 2674 u8 bits_offset, struct btf_show *show) 2675 { 2676 if (btf->resolved_ids) 2677 t = btf_type_id_resolve(btf, &type_id); 2678 else 2679 t = btf_type_skip_modifiers(btf, type_id, NULL); 2680 2681 btf_type_ops(t)->show(btf, t, type_id, data, bits_offset, show); 2682 } 2683 2684 static void btf_var_show(const struct btf *btf, const struct btf_type *t, 2685 u32 type_id, void *data, u8 bits_offset, 2686 struct btf_show *show) 2687 { 2688 t = btf_type_id_resolve(btf, &type_id); 2689 2690 btf_type_ops(t)->show(btf, t, type_id, data, bits_offset, show); 2691 } 2692 2693 static void btf_ptr_show(const struct btf *btf, const struct btf_type *t, 2694 u32 type_id, void *data, u8 bits_offset, 2695 struct btf_show *show) 2696 { 2697 void *safe_data; 2698 2699 safe_data = btf_show_start_type(show, t, type_id, data); 2700 if (!safe_data) 2701 return; 2702 2703 /* It is a hashed value unless BTF_SHOW_PTR_RAW is specified */ 2704 if (show->flags & BTF_SHOW_PTR_RAW) 2705 btf_show_type_value(show, "0x%px", *(void **)safe_data); 2706 else 2707 btf_show_type_value(show, "0x%p", *(void **)safe_data); 2708 btf_show_end_type(show); 2709 } 2710 2711 static void btf_ref_type_log(struct btf_verifier_env *env, 2712 const struct btf_type *t) 2713 { 2714 btf_verifier_log(env, "type_id=%u", t->type); 2715 } 2716 2717 static struct btf_kind_operations modifier_ops = { 2718 .check_meta = btf_ref_type_check_meta, 2719 .resolve = btf_modifier_resolve, 2720 .check_member = btf_modifier_check_member, 2721 .check_kflag_member = btf_modifier_check_kflag_member, 2722 .log_details = btf_ref_type_log, 2723 .show = btf_modifier_show, 2724 }; 2725 2726 static struct btf_kind_operations ptr_ops = { 2727 .check_meta = btf_ref_type_check_meta, 2728 .resolve = btf_ptr_resolve, 2729 .check_member = btf_ptr_check_member, 2730 .check_kflag_member = btf_generic_check_kflag_member, 2731 .log_details = btf_ref_type_log, 2732 .show = btf_ptr_show, 2733 }; 2734 2735 static s32 btf_fwd_check_meta(struct btf_verifier_env *env, 2736 const struct btf_type *t, 2737 u32 meta_left) 2738 { 2739 if (btf_type_vlen(t)) { 2740 btf_verifier_log_type(env, t, "vlen != 0"); 2741 return -EINVAL; 2742 } 2743 2744 if (t->type) { 2745 btf_verifier_log_type(env, t, "type != 0"); 2746 return -EINVAL; 2747 } 2748 2749 /* fwd type must have a valid name */ 2750 if (!t->name_off || 2751 !btf_name_valid_identifier(env->btf, t->name_off)) { 2752 btf_verifier_log_type(env, t, "Invalid name"); 2753 return -EINVAL; 2754 } 2755 2756 btf_verifier_log_type(env, t, NULL); 2757 2758 return 0; 2759 } 2760 2761 static void btf_fwd_type_log(struct btf_verifier_env *env, 2762 const struct btf_type *t) 2763 { 2764 btf_verifier_log(env, "%s", btf_type_kflag(t) ? "union" : "struct"); 2765 } 2766 2767 static struct btf_kind_operations fwd_ops = { 2768 .check_meta = btf_fwd_check_meta, 2769 .resolve = btf_df_resolve, 2770 .check_member = btf_df_check_member, 2771 .check_kflag_member = btf_df_check_kflag_member, 2772 .log_details = btf_fwd_type_log, 2773 .show = btf_df_show, 2774 }; 2775 2776 static int btf_array_check_member(struct btf_verifier_env *env, 2777 const struct btf_type *struct_type, 2778 const struct btf_member *member, 2779 const struct btf_type *member_type) 2780 { 2781 u32 struct_bits_off = member->offset; 2782 u32 struct_size, bytes_offset; 2783 u32 array_type_id, array_size; 2784 struct btf *btf = env->btf; 2785 2786 if (BITS_PER_BYTE_MASKED(struct_bits_off)) { 2787 btf_verifier_log_member(env, struct_type, member, 2788 "Member is not byte aligned"); 2789 return -EINVAL; 2790 } 2791 2792 array_type_id = member->type; 2793 btf_type_id_size(btf, &array_type_id, &array_size); 2794 struct_size = struct_type->size; 2795 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off); 2796 if (struct_size - bytes_offset < array_size) { 2797 btf_verifier_log_member(env, struct_type, member, 2798 "Member exceeds struct_size"); 2799 return -EINVAL; 2800 } 2801 2802 return 0; 2803 } 2804 2805 static s32 btf_array_check_meta(struct btf_verifier_env *env, 2806 const struct btf_type *t, 2807 u32 meta_left) 2808 { 2809 const struct btf_array *array = btf_type_array(t); 2810 u32 meta_needed = sizeof(*array); 2811 2812 if (meta_left < meta_needed) { 2813 btf_verifier_log_basic(env, t, 2814 "meta_left:%u meta_needed:%u", 2815 meta_left, meta_needed); 2816 return -EINVAL; 2817 } 2818 2819 /* array type should not have a name */ 2820 if (t->name_off) { 2821 btf_verifier_log_type(env, t, "Invalid name"); 2822 return -EINVAL; 2823 } 2824 2825 if (btf_type_vlen(t)) { 2826 btf_verifier_log_type(env, t, "vlen != 0"); 2827 return -EINVAL; 2828 } 2829 2830 if (btf_type_kflag(t)) { 2831 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); 2832 return -EINVAL; 2833 } 2834 2835 if (t->size) { 2836 btf_verifier_log_type(env, t, "size != 0"); 2837 return -EINVAL; 2838 } 2839 2840 /* Array elem type and index type cannot be in type void, 2841 * so !array->type and !array->index_type are not allowed. 2842 */ 2843 if (!array->type || !BTF_TYPE_ID_VALID(array->type)) { 2844 btf_verifier_log_type(env, t, "Invalid elem"); 2845 return -EINVAL; 2846 } 2847 2848 if (!array->index_type || !BTF_TYPE_ID_VALID(array->index_type)) { 2849 btf_verifier_log_type(env, t, "Invalid index"); 2850 return -EINVAL; 2851 } 2852 2853 btf_verifier_log_type(env, t, NULL); 2854 2855 return meta_needed; 2856 } 2857 2858 static int btf_array_resolve(struct btf_verifier_env *env, 2859 const struct resolve_vertex *v) 2860 { 2861 const struct btf_array *array = btf_type_array(v->t); 2862 const struct btf_type *elem_type, *index_type; 2863 u32 elem_type_id, index_type_id; 2864 struct btf *btf = env->btf; 2865 u32 elem_size; 2866 2867 /* Check array->index_type */ 2868 index_type_id = array->index_type; 2869 index_type = btf_type_by_id(btf, index_type_id); 2870 if (btf_type_nosize_or_null(index_type) || 2871 btf_type_is_resolve_source_only(index_type)) { 2872 btf_verifier_log_type(env, v->t, "Invalid index"); 2873 return -EINVAL; 2874 } 2875 2876 if (!env_type_is_resolve_sink(env, index_type) && 2877 !env_type_is_resolved(env, index_type_id)) 2878 return env_stack_push(env, index_type, index_type_id); 2879 2880 index_type = btf_type_id_size(btf, &index_type_id, NULL); 2881 if (!index_type || !btf_type_is_int(index_type) || 2882 !btf_type_int_is_regular(index_type)) { 2883 btf_verifier_log_type(env, v->t, "Invalid index"); 2884 return -EINVAL; 2885 } 2886 2887 /* Check array->type */ 2888 elem_type_id = array->type; 2889 elem_type = btf_type_by_id(btf, elem_type_id); 2890 if (btf_type_nosize_or_null(elem_type) || 2891 btf_type_is_resolve_source_only(elem_type)) { 2892 btf_verifier_log_type(env, v->t, 2893 "Invalid elem"); 2894 return -EINVAL; 2895 } 2896 2897 if (!env_type_is_resolve_sink(env, elem_type) && 2898 !env_type_is_resolved(env, elem_type_id)) 2899 return env_stack_push(env, elem_type, elem_type_id); 2900 2901 elem_type = btf_type_id_size(btf, &elem_type_id, &elem_size); 2902 if (!elem_type) { 2903 btf_verifier_log_type(env, v->t, "Invalid elem"); 2904 return -EINVAL; 2905 } 2906 2907 if (btf_type_is_int(elem_type) && !btf_type_int_is_regular(elem_type)) { 2908 btf_verifier_log_type(env, v->t, "Invalid array of int"); 2909 return -EINVAL; 2910 } 2911 2912 if (array->nelems && elem_size > U32_MAX / array->nelems) { 2913 btf_verifier_log_type(env, v->t, 2914 "Array size overflows U32_MAX"); 2915 return -EINVAL; 2916 } 2917 2918 env_stack_pop_resolved(env, elem_type_id, elem_size * array->nelems); 2919 2920 return 0; 2921 } 2922 2923 static void btf_array_log(struct btf_verifier_env *env, 2924 const struct btf_type *t) 2925 { 2926 const struct btf_array *array = btf_type_array(t); 2927 2928 btf_verifier_log(env, "type_id=%u index_type_id=%u nr_elems=%u", 2929 array->type, array->index_type, array->nelems); 2930 } 2931 2932 static void __btf_array_show(const struct btf *btf, const struct btf_type *t, 2933 u32 type_id, void *data, u8 bits_offset, 2934 struct btf_show *show) 2935 { 2936 const struct btf_array *array = btf_type_array(t); 2937 const struct btf_kind_operations *elem_ops; 2938 const struct btf_type *elem_type; 2939 u32 i, elem_size = 0, elem_type_id; 2940 u16 encoding = 0; 2941 2942 elem_type_id = array->type; 2943 elem_type = btf_type_skip_modifiers(btf, elem_type_id, NULL); 2944 if (elem_type && btf_type_has_size(elem_type)) 2945 elem_size = elem_type->size; 2946 2947 if (elem_type && btf_type_is_int(elem_type)) { 2948 u32 int_type = btf_type_int(elem_type); 2949 2950 encoding = BTF_INT_ENCODING(int_type); 2951 2952 /* 2953 * BTF_INT_CHAR encoding never seems to be set for 2954 * char arrays, so if size is 1 and element is 2955 * printable as a char, we'll do that. 2956 */ 2957 if (elem_size == 1) 2958 encoding = BTF_INT_CHAR; 2959 } 2960 2961 if (!btf_show_start_array_type(show, t, type_id, encoding, data)) 2962 return; 2963 2964 if (!elem_type) 2965 goto out; 2966 elem_ops = btf_type_ops(elem_type); 2967 2968 for (i = 0; i < array->nelems; i++) { 2969 2970 btf_show_start_array_member(show); 2971 2972 elem_ops->show(btf, elem_type, elem_type_id, data, 2973 bits_offset, show); 2974 data += elem_size; 2975 2976 btf_show_end_array_member(show); 2977 2978 if (show->state.array_terminated) 2979 break; 2980 } 2981 out: 2982 btf_show_end_array_type(show); 2983 } 2984 2985 static void btf_array_show(const struct btf *btf, const struct btf_type *t, 2986 u32 type_id, void *data, u8 bits_offset, 2987 struct btf_show *show) 2988 { 2989 const struct btf_member *m = show->state.member; 2990 2991 /* 2992 * First check if any members would be shown (are non-zero). 2993 * See comments above "struct btf_show" definition for more 2994 * details on how this works at a high-level. 2995 */ 2996 if (show->state.depth > 0 && !(show->flags & BTF_SHOW_ZERO)) { 2997 if (!show->state.depth_check) { 2998 show->state.depth_check = show->state.depth + 1; 2999 show->state.depth_to_show = 0; 3000 } 3001 __btf_array_show(btf, t, type_id, data, bits_offset, show); 3002 show->state.member = m; 3003 3004 if (show->state.depth_check != show->state.depth + 1) 3005 return; 3006 show->state.depth_check = 0; 3007 3008 if (show->state.depth_to_show <= show->state.depth) 3009 return; 3010 /* 3011 * Reaching here indicates we have recursed and found 3012 * non-zero array member(s). 3013 */ 3014 } 3015 __btf_array_show(btf, t, type_id, data, bits_offset, show); 3016 } 3017 3018 static struct btf_kind_operations array_ops = { 3019 .check_meta = btf_array_check_meta, 3020 .resolve = btf_array_resolve, 3021 .check_member = btf_array_check_member, 3022 .check_kflag_member = btf_generic_check_kflag_member, 3023 .log_details = btf_array_log, 3024 .show = btf_array_show, 3025 }; 3026 3027 static int btf_struct_check_member(struct btf_verifier_env *env, 3028 const struct btf_type *struct_type, 3029 const struct btf_member *member, 3030 const struct btf_type *member_type) 3031 { 3032 u32 struct_bits_off = member->offset; 3033 u32 struct_size, bytes_offset; 3034 3035 if (BITS_PER_BYTE_MASKED(struct_bits_off)) { 3036 btf_verifier_log_member(env, struct_type, member, 3037 "Member is not byte aligned"); 3038 return -EINVAL; 3039 } 3040 3041 struct_size = struct_type->size; 3042 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off); 3043 if (struct_size - bytes_offset < member_type->size) { 3044 btf_verifier_log_member(env, struct_type, member, 3045 "Member exceeds struct_size"); 3046 return -EINVAL; 3047 } 3048 3049 return 0; 3050 } 3051 3052 static s32 btf_struct_check_meta(struct btf_verifier_env *env, 3053 const struct btf_type *t, 3054 u32 meta_left) 3055 { 3056 bool is_union = BTF_INFO_KIND(t->info) == BTF_KIND_UNION; 3057 const struct btf_member *member; 3058 u32 meta_needed, last_offset; 3059 struct btf *btf = env->btf; 3060 u32 struct_size = t->size; 3061 u32 offset; 3062 u16 i; 3063 3064 meta_needed = btf_type_vlen(t) * sizeof(*member); 3065 if (meta_left < meta_needed) { 3066 btf_verifier_log_basic(env, t, 3067 "meta_left:%u meta_needed:%u", 3068 meta_left, meta_needed); 3069 return -EINVAL; 3070 } 3071 3072 /* struct type either no name or a valid one */ 3073 if (t->name_off && 3074 !btf_name_valid_identifier(env->btf, t->name_off)) { 3075 btf_verifier_log_type(env, t, "Invalid name"); 3076 return -EINVAL; 3077 } 3078 3079 btf_verifier_log_type(env, t, NULL); 3080 3081 last_offset = 0; 3082 for_each_member(i, t, member) { 3083 if (!btf_name_offset_valid(btf, member->name_off)) { 3084 btf_verifier_log_member(env, t, member, 3085 "Invalid member name_offset:%u", 3086 member->name_off); 3087 return -EINVAL; 3088 } 3089 3090 /* struct member either no name or a valid one */ 3091 if (member->name_off && 3092 !btf_name_valid_identifier(btf, member->name_off)) { 3093 btf_verifier_log_member(env, t, member, "Invalid name"); 3094 return -EINVAL; 3095 } 3096 /* A member cannot be in type void */ 3097 if (!member->type || !BTF_TYPE_ID_VALID(member->type)) { 3098 btf_verifier_log_member(env, t, member, 3099 "Invalid type_id"); 3100 return -EINVAL; 3101 } 3102 3103 offset = __btf_member_bit_offset(t, member); 3104 if (is_union && offset) { 3105 btf_verifier_log_member(env, t, member, 3106 "Invalid member bits_offset"); 3107 return -EINVAL; 3108 } 3109 3110 /* 3111 * ">" instead of ">=" because the last member could be 3112 * "char a[0];" 3113 */ 3114 if (last_offset > offset) { 3115 btf_verifier_log_member(env, t, member, 3116 "Invalid member bits_offset"); 3117 return -EINVAL; 3118 } 3119 3120 if (BITS_ROUNDUP_BYTES(offset) > struct_size) { 3121 btf_verifier_log_member(env, t, member, 3122 "Member bits_offset exceeds its struct size"); 3123 return -EINVAL; 3124 } 3125 3126 btf_verifier_log_member(env, t, member, NULL); 3127 last_offset = offset; 3128 } 3129 3130 return meta_needed; 3131 } 3132 3133 static int btf_struct_resolve(struct btf_verifier_env *env, 3134 const struct resolve_vertex *v) 3135 { 3136 const struct btf_member *member; 3137 int err; 3138 u16 i; 3139 3140 /* Before continue resolving the next_member, 3141 * ensure the last member is indeed resolved to a 3142 * type with size info. 3143 */ 3144 if (v->next_member) { 3145 const struct btf_type *last_member_type; 3146 const struct btf_member *last_member; 3147 u32 last_member_type_id; 3148 3149 last_member = btf_type_member(v->t) + v->next_member - 1; 3150 last_member_type_id = last_member->type; 3151 if (WARN_ON_ONCE(!env_type_is_resolved(env, 3152 last_member_type_id))) 3153 return -EINVAL; 3154 3155 last_member_type = btf_type_by_id(env->btf, 3156 last_member_type_id); 3157 if (btf_type_kflag(v->t)) 3158 err = btf_type_ops(last_member_type)->check_kflag_member(env, v->t, 3159 last_member, 3160 last_member_type); 3161 else 3162 err = btf_type_ops(last_member_type)->check_member(env, v->t, 3163 last_member, 3164 last_member_type); 3165 if (err) 3166 return err; 3167 } 3168 3169 for_each_member_from(i, v->next_member, v->t, member) { 3170 u32 member_type_id = member->type; 3171 const struct btf_type *member_type = btf_type_by_id(env->btf, 3172 member_type_id); 3173 3174 if (btf_type_nosize_or_null(member_type) || 3175 btf_type_is_resolve_source_only(member_type)) { 3176 btf_verifier_log_member(env, v->t, member, 3177 "Invalid member"); 3178 return -EINVAL; 3179 } 3180 3181 if (!env_type_is_resolve_sink(env, member_type) && 3182 !env_type_is_resolved(env, member_type_id)) { 3183 env_stack_set_next_member(env, i + 1); 3184 return env_stack_push(env, member_type, member_type_id); 3185 } 3186 3187 if (btf_type_kflag(v->t)) 3188 err = btf_type_ops(member_type)->check_kflag_member(env, v->t, 3189 member, 3190 member_type); 3191 else 3192 err = btf_type_ops(member_type)->check_member(env, v->t, 3193 member, 3194 member_type); 3195 if (err) 3196 return err; 3197 } 3198 3199 env_stack_pop_resolved(env, 0, 0); 3200 3201 return 0; 3202 } 3203 3204 static void btf_struct_log(struct btf_verifier_env *env, 3205 const struct btf_type *t) 3206 { 3207 btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t)); 3208 } 3209 3210 enum btf_field_info_type { 3211 BTF_FIELD_SPIN_LOCK, 3212 BTF_FIELD_TIMER, 3213 BTF_FIELD_KPTR, 3214 }; 3215 3216 enum { 3217 BTF_FIELD_IGNORE = 0, 3218 BTF_FIELD_FOUND = 1, 3219 }; 3220 3221 struct btf_field_info { 3222 enum btf_field_type type; 3223 u32 off; 3224 union { 3225 struct { 3226 u32 type_id; 3227 } kptr; 3228 struct { 3229 const char *node_name; 3230 u32 value_btf_id; 3231 } graph_root; 3232 }; 3233 }; 3234 3235 static int btf_find_struct(const struct btf *btf, const struct btf_type *t, 3236 u32 off, int sz, enum btf_field_type field_type, 3237 struct btf_field_info *info) 3238 { 3239 if (!__btf_type_is_struct(t)) 3240 return BTF_FIELD_IGNORE; 3241 if (t->size != sz) 3242 return BTF_FIELD_IGNORE; 3243 info->type = field_type; 3244 info->off = off; 3245 return BTF_FIELD_FOUND; 3246 } 3247 3248 static int btf_find_kptr(const struct btf *btf, const struct btf_type *t, 3249 u32 off, int sz, struct btf_field_info *info) 3250 { 3251 enum btf_field_type type; 3252 u32 res_id; 3253 3254 /* Permit modifiers on the pointer itself */ 3255 if (btf_type_is_volatile(t)) 3256 t = btf_type_by_id(btf, t->type); 3257 /* For PTR, sz is always == 8 */ 3258 if (!btf_type_is_ptr(t)) 3259 return BTF_FIELD_IGNORE; 3260 t = btf_type_by_id(btf, t->type); 3261 3262 if (!btf_type_is_type_tag(t)) 3263 return BTF_FIELD_IGNORE; 3264 /* Reject extra tags */ 3265 if (btf_type_is_type_tag(btf_type_by_id(btf, t->type))) 3266 return -EINVAL; 3267 if (!strcmp("kptr", __btf_name_by_offset(btf, t->name_off))) 3268 type = BPF_KPTR_UNREF; 3269 else if (!strcmp("kptr_ref", __btf_name_by_offset(btf, t->name_off))) 3270 type = BPF_KPTR_REF; 3271 else 3272 return -EINVAL; 3273 3274 /* Get the base type */ 3275 t = btf_type_skip_modifiers(btf, t->type, &res_id); 3276 /* Only pointer to struct is allowed */ 3277 if (!__btf_type_is_struct(t)) 3278 return -EINVAL; 3279 3280 info->type = type; 3281 info->off = off; 3282 info->kptr.type_id = res_id; 3283 return BTF_FIELD_FOUND; 3284 } 3285 3286 static const char *btf_find_decl_tag_value(const struct btf *btf, 3287 const struct btf_type *pt, 3288 int comp_idx, const char *tag_key) 3289 { 3290 int i; 3291 3292 for (i = 1; i < btf_nr_types(btf); i++) { 3293 const struct btf_type *t = btf_type_by_id(btf, i); 3294 int len = strlen(tag_key); 3295 3296 if (!btf_type_is_decl_tag(t)) 3297 continue; 3298 if (pt != btf_type_by_id(btf, t->type) || 3299 btf_type_decl_tag(t)->component_idx != comp_idx) 3300 continue; 3301 if (strncmp(__btf_name_by_offset(btf, t->name_off), tag_key, len)) 3302 continue; 3303 return __btf_name_by_offset(btf, t->name_off) + len; 3304 } 3305 return NULL; 3306 } 3307 3308 static int btf_find_list_head(const struct btf *btf, const struct btf_type *pt, 3309 const struct btf_type *t, int comp_idx, 3310 u32 off, int sz, struct btf_field_info *info) 3311 { 3312 const char *value_type; 3313 const char *list_node; 3314 s32 id; 3315 3316 if (!__btf_type_is_struct(t)) 3317 return BTF_FIELD_IGNORE; 3318 if (t->size != sz) 3319 return BTF_FIELD_IGNORE; 3320 value_type = btf_find_decl_tag_value(btf, pt, comp_idx, "contains:"); 3321 if (!value_type) 3322 return -EINVAL; 3323 list_node = strstr(value_type, ":"); 3324 if (!list_node) 3325 return -EINVAL; 3326 value_type = kstrndup(value_type, list_node - value_type, GFP_KERNEL | __GFP_NOWARN); 3327 if (!value_type) 3328 return -ENOMEM; 3329 id = btf_find_by_name_kind(btf, value_type, BTF_KIND_STRUCT); 3330 kfree(value_type); 3331 if (id < 0) 3332 return id; 3333 list_node++; 3334 if (str_is_empty(list_node)) 3335 return -EINVAL; 3336 info->type = BPF_LIST_HEAD; 3337 info->off = off; 3338 info->graph_root.value_btf_id = id; 3339 info->graph_root.node_name = list_node; 3340 return BTF_FIELD_FOUND; 3341 } 3342 3343 static int btf_get_field_type(const char *name, u32 field_mask, u32 *seen_mask, 3344 int *align, int *sz) 3345 { 3346 int type = 0; 3347 3348 if (field_mask & BPF_SPIN_LOCK) { 3349 if (!strcmp(name, "bpf_spin_lock")) { 3350 if (*seen_mask & BPF_SPIN_LOCK) 3351 return -E2BIG; 3352 *seen_mask |= BPF_SPIN_LOCK; 3353 type = BPF_SPIN_LOCK; 3354 goto end; 3355 } 3356 } 3357 if (field_mask & BPF_TIMER) { 3358 if (!strcmp(name, "bpf_timer")) { 3359 if (*seen_mask & BPF_TIMER) 3360 return -E2BIG; 3361 *seen_mask |= BPF_TIMER; 3362 type = BPF_TIMER; 3363 goto end; 3364 } 3365 } 3366 if (field_mask & BPF_LIST_HEAD) { 3367 if (!strcmp(name, "bpf_list_head")) { 3368 type = BPF_LIST_HEAD; 3369 goto end; 3370 } 3371 } 3372 if (field_mask & BPF_LIST_NODE) { 3373 if (!strcmp(name, "bpf_list_node")) { 3374 type = BPF_LIST_NODE; 3375 goto end; 3376 } 3377 } 3378 /* Only return BPF_KPTR when all other types with matchable names fail */ 3379 if (field_mask & BPF_KPTR) { 3380 type = BPF_KPTR_REF; 3381 goto end; 3382 } 3383 return 0; 3384 end: 3385 *sz = btf_field_type_size(type); 3386 *align = btf_field_type_align(type); 3387 return type; 3388 } 3389 3390 static int btf_find_struct_field(const struct btf *btf, 3391 const struct btf_type *t, u32 field_mask, 3392 struct btf_field_info *info, int info_cnt) 3393 { 3394 int ret, idx = 0, align, sz, field_type; 3395 const struct btf_member *member; 3396 struct btf_field_info tmp; 3397 u32 i, off, seen_mask = 0; 3398 3399 for_each_member(i, t, member) { 3400 const struct btf_type *member_type = btf_type_by_id(btf, 3401 member->type); 3402 3403 field_type = btf_get_field_type(__btf_name_by_offset(btf, member_type->name_off), 3404 field_mask, &seen_mask, &align, &sz); 3405 if (field_type == 0) 3406 continue; 3407 if (field_type < 0) 3408 return field_type; 3409 3410 off = __btf_member_bit_offset(t, member); 3411 if (off % 8) 3412 /* valid C code cannot generate such BTF */ 3413 return -EINVAL; 3414 off /= 8; 3415 if (off % align) 3416 continue; 3417 3418 switch (field_type) { 3419 case BPF_SPIN_LOCK: 3420 case BPF_TIMER: 3421 case BPF_LIST_NODE: 3422 ret = btf_find_struct(btf, member_type, off, sz, field_type, 3423 idx < info_cnt ? &info[idx] : &tmp); 3424 if (ret < 0) 3425 return ret; 3426 break; 3427 case BPF_KPTR_UNREF: 3428 case BPF_KPTR_REF: 3429 ret = btf_find_kptr(btf, member_type, off, sz, 3430 idx < info_cnt ? &info[idx] : &tmp); 3431 if (ret < 0) 3432 return ret; 3433 break; 3434 case BPF_LIST_HEAD: 3435 ret = btf_find_list_head(btf, t, member_type, i, off, sz, 3436 idx < info_cnt ? &info[idx] : &tmp); 3437 if (ret < 0) 3438 return ret; 3439 break; 3440 default: 3441 return -EFAULT; 3442 } 3443 3444 if (ret == BTF_FIELD_IGNORE) 3445 continue; 3446 if (idx >= info_cnt) 3447 return -E2BIG; 3448 ++idx; 3449 } 3450 return idx; 3451 } 3452 3453 static int btf_find_datasec_var(const struct btf *btf, const struct btf_type *t, 3454 u32 field_mask, struct btf_field_info *info, 3455 int info_cnt) 3456 { 3457 int ret, idx = 0, align, sz, field_type; 3458 const struct btf_var_secinfo *vsi; 3459 struct btf_field_info tmp; 3460 u32 i, off, seen_mask = 0; 3461 3462 for_each_vsi(i, t, vsi) { 3463 const struct btf_type *var = btf_type_by_id(btf, vsi->type); 3464 const struct btf_type *var_type = btf_type_by_id(btf, var->type); 3465 3466 field_type = btf_get_field_type(__btf_name_by_offset(btf, var_type->name_off), 3467 field_mask, &seen_mask, &align, &sz); 3468 if (field_type == 0) 3469 continue; 3470 if (field_type < 0) 3471 return field_type; 3472 3473 off = vsi->offset; 3474 if (vsi->size != sz) 3475 continue; 3476 if (off % align) 3477 continue; 3478 3479 switch (field_type) { 3480 case BPF_SPIN_LOCK: 3481 case BPF_TIMER: 3482 case BPF_LIST_NODE: 3483 ret = btf_find_struct(btf, var_type, off, sz, field_type, 3484 idx < info_cnt ? &info[idx] : &tmp); 3485 if (ret < 0) 3486 return ret; 3487 break; 3488 case BPF_KPTR_UNREF: 3489 case BPF_KPTR_REF: 3490 ret = btf_find_kptr(btf, var_type, off, sz, 3491 idx < info_cnt ? &info[idx] : &tmp); 3492 if (ret < 0) 3493 return ret; 3494 break; 3495 case BPF_LIST_HEAD: 3496 ret = btf_find_list_head(btf, var, var_type, -1, off, sz, 3497 idx < info_cnt ? &info[idx] : &tmp); 3498 if (ret < 0) 3499 return ret; 3500 break; 3501 default: 3502 return -EFAULT; 3503 } 3504 3505 if (ret == BTF_FIELD_IGNORE) 3506 continue; 3507 if (idx >= info_cnt) 3508 return -E2BIG; 3509 ++idx; 3510 } 3511 return idx; 3512 } 3513 3514 static int btf_find_field(const struct btf *btf, const struct btf_type *t, 3515 u32 field_mask, struct btf_field_info *info, 3516 int info_cnt) 3517 { 3518 if (__btf_type_is_struct(t)) 3519 return btf_find_struct_field(btf, t, field_mask, info, info_cnt); 3520 else if (btf_type_is_datasec(t)) 3521 return btf_find_datasec_var(btf, t, field_mask, info, info_cnt); 3522 return -EINVAL; 3523 } 3524 3525 static int btf_parse_kptr(const struct btf *btf, struct btf_field *field, 3526 struct btf_field_info *info) 3527 { 3528 struct module *mod = NULL; 3529 const struct btf_type *t; 3530 struct btf *kernel_btf; 3531 int ret; 3532 s32 id; 3533 3534 /* Find type in map BTF, and use it to look up the matching type 3535 * in vmlinux or module BTFs, by name and kind. 3536 */ 3537 t = btf_type_by_id(btf, info->kptr.type_id); 3538 id = bpf_find_btf_id(__btf_name_by_offset(btf, t->name_off), BTF_INFO_KIND(t->info), 3539 &kernel_btf); 3540 if (id < 0) 3541 return id; 3542 3543 /* Find and stash the function pointer for the destruction function that 3544 * needs to be eventually invoked from the map free path. 3545 */ 3546 if (info->type == BPF_KPTR_REF) { 3547 const struct btf_type *dtor_func; 3548 const char *dtor_func_name; 3549 unsigned long addr; 3550 s32 dtor_btf_id; 3551 3552 /* This call also serves as a whitelist of allowed objects that 3553 * can be used as a referenced pointer and be stored in a map at 3554 * the same time. 3555 */ 3556 dtor_btf_id = btf_find_dtor_kfunc(kernel_btf, id); 3557 if (dtor_btf_id < 0) { 3558 ret = dtor_btf_id; 3559 goto end_btf; 3560 } 3561 3562 dtor_func = btf_type_by_id(kernel_btf, dtor_btf_id); 3563 if (!dtor_func) { 3564 ret = -ENOENT; 3565 goto end_btf; 3566 } 3567 3568 if (btf_is_module(kernel_btf)) { 3569 mod = btf_try_get_module(kernel_btf); 3570 if (!mod) { 3571 ret = -ENXIO; 3572 goto end_btf; 3573 } 3574 } 3575 3576 /* We already verified dtor_func to be btf_type_is_func 3577 * in register_btf_id_dtor_kfuncs. 3578 */ 3579 dtor_func_name = __btf_name_by_offset(kernel_btf, dtor_func->name_off); 3580 addr = kallsyms_lookup_name(dtor_func_name); 3581 if (!addr) { 3582 ret = -EINVAL; 3583 goto end_mod; 3584 } 3585 field->kptr.dtor = (void *)addr; 3586 } 3587 3588 field->kptr.btf_id = id; 3589 field->kptr.btf = kernel_btf; 3590 field->kptr.module = mod; 3591 return 0; 3592 end_mod: 3593 module_put(mod); 3594 end_btf: 3595 btf_put(kernel_btf); 3596 return ret; 3597 } 3598 3599 static int btf_parse_list_head(const struct btf *btf, struct btf_field *field, 3600 struct btf_field_info *info) 3601 { 3602 const struct btf_type *t, *n = NULL; 3603 const struct btf_member *member; 3604 u32 offset; 3605 int i; 3606 3607 t = btf_type_by_id(btf, info->graph_root.value_btf_id); 3608 /* We've already checked that value_btf_id is a struct type. We 3609 * just need to figure out the offset of the list_node, and 3610 * verify its type. 3611 */ 3612 for_each_member(i, t, member) { 3613 if (strcmp(info->graph_root.node_name, 3614 __btf_name_by_offset(btf, member->name_off))) 3615 continue; 3616 /* Invalid BTF, two members with same name */ 3617 if (n) 3618 return -EINVAL; 3619 n = btf_type_by_id(btf, member->type); 3620 if (!__btf_type_is_struct(n)) 3621 return -EINVAL; 3622 if (strcmp("bpf_list_node", __btf_name_by_offset(btf, n->name_off))) 3623 return -EINVAL; 3624 offset = __btf_member_bit_offset(n, member); 3625 if (offset % 8) 3626 return -EINVAL; 3627 offset /= 8; 3628 if (offset % __alignof__(struct bpf_list_node)) 3629 return -EINVAL; 3630 3631 field->graph_root.btf = (struct btf *)btf; 3632 field->graph_root.value_btf_id = info->graph_root.value_btf_id; 3633 field->graph_root.node_offset = offset; 3634 } 3635 if (!n) 3636 return -ENOENT; 3637 return 0; 3638 } 3639 3640 struct btf_record *btf_parse_fields(const struct btf *btf, const struct btf_type *t, 3641 u32 field_mask, u32 value_size) 3642 { 3643 struct btf_field_info info_arr[BTF_FIELDS_MAX]; 3644 struct btf_record *rec; 3645 u32 next_off = 0; 3646 int ret, i, cnt; 3647 3648 ret = btf_find_field(btf, t, field_mask, info_arr, ARRAY_SIZE(info_arr)); 3649 if (ret < 0) 3650 return ERR_PTR(ret); 3651 if (!ret) 3652 return NULL; 3653 3654 cnt = ret; 3655 /* This needs to be kzalloc to zero out padding and unused fields, see 3656 * comment in btf_record_equal. 3657 */ 3658 rec = kzalloc(offsetof(struct btf_record, fields[cnt]), GFP_KERNEL | __GFP_NOWARN); 3659 if (!rec) 3660 return ERR_PTR(-ENOMEM); 3661 3662 rec->spin_lock_off = -EINVAL; 3663 rec->timer_off = -EINVAL; 3664 for (i = 0; i < cnt; i++) { 3665 if (info_arr[i].off + btf_field_type_size(info_arr[i].type) > value_size) { 3666 WARN_ONCE(1, "verifier bug off %d size %d", info_arr[i].off, value_size); 3667 ret = -EFAULT; 3668 goto end; 3669 } 3670 if (info_arr[i].off < next_off) { 3671 ret = -EEXIST; 3672 goto end; 3673 } 3674 next_off = info_arr[i].off + btf_field_type_size(info_arr[i].type); 3675 3676 rec->field_mask |= info_arr[i].type; 3677 rec->fields[i].offset = info_arr[i].off; 3678 rec->fields[i].type = info_arr[i].type; 3679 3680 switch (info_arr[i].type) { 3681 case BPF_SPIN_LOCK: 3682 WARN_ON_ONCE(rec->spin_lock_off >= 0); 3683 /* Cache offset for faster lookup at runtime */ 3684 rec->spin_lock_off = rec->fields[i].offset; 3685 break; 3686 case BPF_TIMER: 3687 WARN_ON_ONCE(rec->timer_off >= 0); 3688 /* Cache offset for faster lookup at runtime */ 3689 rec->timer_off = rec->fields[i].offset; 3690 break; 3691 case BPF_KPTR_UNREF: 3692 case BPF_KPTR_REF: 3693 ret = btf_parse_kptr(btf, &rec->fields[i], &info_arr[i]); 3694 if (ret < 0) 3695 goto end; 3696 break; 3697 case BPF_LIST_HEAD: 3698 ret = btf_parse_list_head(btf, &rec->fields[i], &info_arr[i]); 3699 if (ret < 0) 3700 goto end; 3701 break; 3702 case BPF_LIST_NODE: 3703 break; 3704 default: 3705 ret = -EFAULT; 3706 goto end; 3707 } 3708 rec->cnt++; 3709 } 3710 3711 /* bpf_list_head requires bpf_spin_lock */ 3712 if (btf_record_has_field(rec, BPF_LIST_HEAD) && rec->spin_lock_off < 0) { 3713 ret = -EINVAL; 3714 goto end; 3715 } 3716 3717 return rec; 3718 end: 3719 btf_record_free(rec); 3720 return ERR_PTR(ret); 3721 } 3722 3723 int btf_check_and_fixup_fields(const struct btf *btf, struct btf_record *rec) 3724 { 3725 int i; 3726 3727 /* There are two owning types, kptr_ref and bpf_list_head. The former 3728 * only supports storing kernel types, which can never store references 3729 * to program allocated local types, atleast not yet. Hence we only need 3730 * to ensure that bpf_list_head ownership does not form cycles. 3731 */ 3732 if (IS_ERR_OR_NULL(rec) || !(rec->field_mask & BPF_LIST_HEAD)) 3733 return 0; 3734 for (i = 0; i < rec->cnt; i++) { 3735 struct btf_struct_meta *meta; 3736 u32 btf_id; 3737 3738 if (!(rec->fields[i].type & BPF_LIST_HEAD)) 3739 continue; 3740 btf_id = rec->fields[i].graph_root.value_btf_id; 3741 meta = btf_find_struct_meta(btf, btf_id); 3742 if (!meta) 3743 return -EFAULT; 3744 rec->fields[i].graph_root.value_rec = meta->record; 3745 3746 if (!(rec->field_mask & BPF_LIST_NODE)) 3747 continue; 3748 3749 /* We need to ensure ownership acyclicity among all types. The 3750 * proper way to do it would be to topologically sort all BTF 3751 * IDs based on the ownership edges, since there can be multiple 3752 * bpf_list_head in a type. Instead, we use the following 3753 * reasoning: 3754 * 3755 * - A type can only be owned by another type in user BTF if it 3756 * has a bpf_list_node. 3757 * - A type can only _own_ another type in user BTF if it has a 3758 * bpf_list_head. 3759 * 3760 * We ensure that if a type has both bpf_list_head and 3761 * bpf_list_node, its element types cannot be owning types. 3762 * 3763 * To ensure acyclicity: 3764 * 3765 * When A only has bpf_list_head, ownership chain can be: 3766 * A -> B -> C 3767 * Where: 3768 * - B has both bpf_list_head and bpf_list_node. 3769 * - C only has bpf_list_node. 3770 * 3771 * When A has both bpf_list_head and bpf_list_node, some other 3772 * type already owns it in the BTF domain, hence it can not own 3773 * another owning type through any of the bpf_list_head edges. 3774 * A -> B 3775 * Where: 3776 * - B only has bpf_list_node. 3777 */ 3778 if (meta->record->field_mask & BPF_LIST_HEAD) 3779 return -ELOOP; 3780 } 3781 return 0; 3782 } 3783 3784 static int btf_field_offs_cmp(const void *_a, const void *_b, const void *priv) 3785 { 3786 const u32 a = *(const u32 *)_a; 3787 const u32 b = *(const u32 *)_b; 3788 3789 if (a < b) 3790 return -1; 3791 else if (a > b) 3792 return 1; 3793 return 0; 3794 } 3795 3796 static void btf_field_offs_swap(void *_a, void *_b, int size, const void *priv) 3797 { 3798 struct btf_field_offs *foffs = (void *)priv; 3799 u32 *off_base = foffs->field_off; 3800 u32 *a = _a, *b = _b; 3801 u8 *sz_a, *sz_b; 3802 3803 sz_a = foffs->field_sz + (a - off_base); 3804 sz_b = foffs->field_sz + (b - off_base); 3805 3806 swap(*a, *b); 3807 swap(*sz_a, *sz_b); 3808 } 3809 3810 struct btf_field_offs *btf_parse_field_offs(struct btf_record *rec) 3811 { 3812 struct btf_field_offs *foffs; 3813 u32 i, *off; 3814 u8 *sz; 3815 3816 BUILD_BUG_ON(ARRAY_SIZE(foffs->field_off) != ARRAY_SIZE(foffs->field_sz)); 3817 if (IS_ERR_OR_NULL(rec)) 3818 return NULL; 3819 3820 foffs = kzalloc(sizeof(*foffs), GFP_KERNEL | __GFP_NOWARN); 3821 if (!foffs) 3822 return ERR_PTR(-ENOMEM); 3823 3824 off = foffs->field_off; 3825 sz = foffs->field_sz; 3826 for (i = 0; i < rec->cnt; i++) { 3827 off[i] = rec->fields[i].offset; 3828 sz[i] = btf_field_type_size(rec->fields[i].type); 3829 } 3830 foffs->cnt = rec->cnt; 3831 3832 if (foffs->cnt == 1) 3833 return foffs; 3834 sort_r(foffs->field_off, foffs->cnt, sizeof(foffs->field_off[0]), 3835 btf_field_offs_cmp, btf_field_offs_swap, foffs); 3836 return foffs; 3837 } 3838 3839 static void __btf_struct_show(const struct btf *btf, const struct btf_type *t, 3840 u32 type_id, void *data, u8 bits_offset, 3841 struct btf_show *show) 3842 { 3843 const struct btf_member *member; 3844 void *safe_data; 3845 u32 i; 3846 3847 safe_data = btf_show_start_struct_type(show, t, type_id, data); 3848 if (!safe_data) 3849 return; 3850 3851 for_each_member(i, t, member) { 3852 const struct btf_type *member_type = btf_type_by_id(btf, 3853 member->type); 3854 const struct btf_kind_operations *ops; 3855 u32 member_offset, bitfield_size; 3856 u32 bytes_offset; 3857 u8 bits8_offset; 3858 3859 btf_show_start_member(show, member); 3860 3861 member_offset = __btf_member_bit_offset(t, member); 3862 bitfield_size = __btf_member_bitfield_size(t, member); 3863 bytes_offset = BITS_ROUNDDOWN_BYTES(member_offset); 3864 bits8_offset = BITS_PER_BYTE_MASKED(member_offset); 3865 if (bitfield_size) { 3866 safe_data = btf_show_start_type(show, member_type, 3867 member->type, 3868 data + bytes_offset); 3869 if (safe_data) 3870 btf_bitfield_show(safe_data, 3871 bits8_offset, 3872 bitfield_size, show); 3873 btf_show_end_type(show); 3874 } else { 3875 ops = btf_type_ops(member_type); 3876 ops->show(btf, member_type, member->type, 3877 data + bytes_offset, bits8_offset, show); 3878 } 3879 3880 btf_show_end_member(show); 3881 } 3882 3883 btf_show_end_struct_type(show); 3884 } 3885 3886 static void btf_struct_show(const struct btf *btf, const struct btf_type *t, 3887 u32 type_id, void *data, u8 bits_offset, 3888 struct btf_show *show) 3889 { 3890 const struct btf_member *m = show->state.member; 3891 3892 /* 3893 * First check if any members would be shown (are non-zero). 3894 * See comments above "struct btf_show" definition for more 3895 * details on how this works at a high-level. 3896 */ 3897 if (show->state.depth > 0 && !(show->flags & BTF_SHOW_ZERO)) { 3898 if (!show->state.depth_check) { 3899 show->state.depth_check = show->state.depth + 1; 3900 show->state.depth_to_show = 0; 3901 } 3902 __btf_struct_show(btf, t, type_id, data, bits_offset, show); 3903 /* Restore saved member data here */ 3904 show->state.member = m; 3905 if (show->state.depth_check != show->state.depth + 1) 3906 return; 3907 show->state.depth_check = 0; 3908 3909 if (show->state.depth_to_show <= show->state.depth) 3910 return; 3911 /* 3912 * Reaching here indicates we have recursed and found 3913 * non-zero child values. 3914 */ 3915 } 3916 3917 __btf_struct_show(btf, t, type_id, data, bits_offset, show); 3918 } 3919 3920 static struct btf_kind_operations struct_ops = { 3921 .check_meta = btf_struct_check_meta, 3922 .resolve = btf_struct_resolve, 3923 .check_member = btf_struct_check_member, 3924 .check_kflag_member = btf_generic_check_kflag_member, 3925 .log_details = btf_struct_log, 3926 .show = btf_struct_show, 3927 }; 3928 3929 static int btf_enum_check_member(struct btf_verifier_env *env, 3930 const struct btf_type *struct_type, 3931 const struct btf_member *member, 3932 const struct btf_type *member_type) 3933 { 3934 u32 struct_bits_off = member->offset; 3935 u32 struct_size, bytes_offset; 3936 3937 if (BITS_PER_BYTE_MASKED(struct_bits_off)) { 3938 btf_verifier_log_member(env, struct_type, member, 3939 "Member is not byte aligned"); 3940 return -EINVAL; 3941 } 3942 3943 struct_size = struct_type->size; 3944 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off); 3945 if (struct_size - bytes_offset < member_type->size) { 3946 btf_verifier_log_member(env, struct_type, member, 3947 "Member exceeds struct_size"); 3948 return -EINVAL; 3949 } 3950 3951 return 0; 3952 } 3953 3954 static int btf_enum_check_kflag_member(struct btf_verifier_env *env, 3955 const struct btf_type *struct_type, 3956 const struct btf_member *member, 3957 const struct btf_type *member_type) 3958 { 3959 u32 struct_bits_off, nr_bits, bytes_end, struct_size; 3960 u32 int_bitsize = sizeof(int) * BITS_PER_BYTE; 3961 3962 struct_bits_off = BTF_MEMBER_BIT_OFFSET(member->offset); 3963 nr_bits = BTF_MEMBER_BITFIELD_SIZE(member->offset); 3964 if (!nr_bits) { 3965 if (BITS_PER_BYTE_MASKED(struct_bits_off)) { 3966 btf_verifier_log_member(env, struct_type, member, 3967 "Member is not byte aligned"); 3968 return -EINVAL; 3969 } 3970 3971 nr_bits = int_bitsize; 3972 } else if (nr_bits > int_bitsize) { 3973 btf_verifier_log_member(env, struct_type, member, 3974 "Invalid member bitfield_size"); 3975 return -EINVAL; 3976 } 3977 3978 struct_size = struct_type->size; 3979 bytes_end = BITS_ROUNDUP_BYTES(struct_bits_off + nr_bits); 3980 if (struct_size < bytes_end) { 3981 btf_verifier_log_member(env, struct_type, member, 3982 "Member exceeds struct_size"); 3983 return -EINVAL; 3984 } 3985 3986 return 0; 3987 } 3988 3989 static s32 btf_enum_check_meta(struct btf_verifier_env *env, 3990 const struct btf_type *t, 3991 u32 meta_left) 3992 { 3993 const struct btf_enum *enums = btf_type_enum(t); 3994 struct btf *btf = env->btf; 3995 const char *fmt_str; 3996 u16 i, nr_enums; 3997 u32 meta_needed; 3998 3999 nr_enums = btf_type_vlen(t); 4000 meta_needed = nr_enums * sizeof(*enums); 4001 4002 if (meta_left < meta_needed) { 4003 btf_verifier_log_basic(env, t, 4004 "meta_left:%u meta_needed:%u", 4005 meta_left, meta_needed); 4006 return -EINVAL; 4007 } 4008 4009 if (t->size > 8 || !is_power_of_2(t->size)) { 4010 btf_verifier_log_type(env, t, "Unexpected size"); 4011 return -EINVAL; 4012 } 4013 4014 /* enum type either no name or a valid one */ 4015 if (t->name_off && 4016 !btf_name_valid_identifier(env->btf, t->name_off)) { 4017 btf_verifier_log_type(env, t, "Invalid name"); 4018 return -EINVAL; 4019 } 4020 4021 btf_verifier_log_type(env, t, NULL); 4022 4023 for (i = 0; i < nr_enums; i++) { 4024 if (!btf_name_offset_valid(btf, enums[i].name_off)) { 4025 btf_verifier_log(env, "\tInvalid name_offset:%u", 4026 enums[i].name_off); 4027 return -EINVAL; 4028 } 4029 4030 /* enum member must have a valid name */ 4031 if (!enums[i].name_off || 4032 !btf_name_valid_identifier(btf, enums[i].name_off)) { 4033 btf_verifier_log_type(env, t, "Invalid name"); 4034 return -EINVAL; 4035 } 4036 4037 if (env->log.level == BPF_LOG_KERNEL) 4038 continue; 4039 fmt_str = btf_type_kflag(t) ? "\t%s val=%d\n" : "\t%s val=%u\n"; 4040 btf_verifier_log(env, fmt_str, 4041 __btf_name_by_offset(btf, enums[i].name_off), 4042 enums[i].val); 4043 } 4044 4045 return meta_needed; 4046 } 4047 4048 static void btf_enum_log(struct btf_verifier_env *env, 4049 const struct btf_type *t) 4050 { 4051 btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t)); 4052 } 4053 4054 static void btf_enum_show(const struct btf *btf, const struct btf_type *t, 4055 u32 type_id, void *data, u8 bits_offset, 4056 struct btf_show *show) 4057 { 4058 const struct btf_enum *enums = btf_type_enum(t); 4059 u32 i, nr_enums = btf_type_vlen(t); 4060 void *safe_data; 4061 int v; 4062 4063 safe_data = btf_show_start_type(show, t, type_id, data); 4064 if (!safe_data) 4065 return; 4066 4067 v = *(int *)safe_data; 4068 4069 for (i = 0; i < nr_enums; i++) { 4070 if (v != enums[i].val) 4071 continue; 4072 4073 btf_show_type_value(show, "%s", 4074 __btf_name_by_offset(btf, 4075 enums[i].name_off)); 4076 4077 btf_show_end_type(show); 4078 return; 4079 } 4080 4081 if (btf_type_kflag(t)) 4082 btf_show_type_value(show, "%d", v); 4083 else 4084 btf_show_type_value(show, "%u", v); 4085 btf_show_end_type(show); 4086 } 4087 4088 static struct btf_kind_operations enum_ops = { 4089 .check_meta = btf_enum_check_meta, 4090 .resolve = btf_df_resolve, 4091 .check_member = btf_enum_check_member, 4092 .check_kflag_member = btf_enum_check_kflag_member, 4093 .log_details = btf_enum_log, 4094 .show = btf_enum_show, 4095 }; 4096 4097 static s32 btf_enum64_check_meta(struct btf_verifier_env *env, 4098 const struct btf_type *t, 4099 u32 meta_left) 4100 { 4101 const struct btf_enum64 *enums = btf_type_enum64(t); 4102 struct btf *btf = env->btf; 4103 const char *fmt_str; 4104 u16 i, nr_enums; 4105 u32 meta_needed; 4106 4107 nr_enums = btf_type_vlen(t); 4108 meta_needed = nr_enums * sizeof(*enums); 4109 4110 if (meta_left < meta_needed) { 4111 btf_verifier_log_basic(env, t, 4112 "meta_left:%u meta_needed:%u", 4113 meta_left, meta_needed); 4114 return -EINVAL; 4115 } 4116 4117 if (t->size > 8 || !is_power_of_2(t->size)) { 4118 btf_verifier_log_type(env, t, "Unexpected size"); 4119 return -EINVAL; 4120 } 4121 4122 /* enum type either no name or a valid one */ 4123 if (t->name_off && 4124 !btf_name_valid_identifier(env->btf, t->name_off)) { 4125 btf_verifier_log_type(env, t, "Invalid name"); 4126 return -EINVAL; 4127 } 4128 4129 btf_verifier_log_type(env, t, NULL); 4130 4131 for (i = 0; i < nr_enums; i++) { 4132 if (!btf_name_offset_valid(btf, enums[i].name_off)) { 4133 btf_verifier_log(env, "\tInvalid name_offset:%u", 4134 enums[i].name_off); 4135 return -EINVAL; 4136 } 4137 4138 /* enum member must have a valid name */ 4139 if (!enums[i].name_off || 4140 !btf_name_valid_identifier(btf, enums[i].name_off)) { 4141 btf_verifier_log_type(env, t, "Invalid name"); 4142 return -EINVAL; 4143 } 4144 4145 if (env->log.level == BPF_LOG_KERNEL) 4146 continue; 4147 4148 fmt_str = btf_type_kflag(t) ? "\t%s val=%lld\n" : "\t%s val=%llu\n"; 4149 btf_verifier_log(env, fmt_str, 4150 __btf_name_by_offset(btf, enums[i].name_off), 4151 btf_enum64_value(enums + i)); 4152 } 4153 4154 return meta_needed; 4155 } 4156 4157 static void btf_enum64_show(const struct btf *btf, const struct btf_type *t, 4158 u32 type_id, void *data, u8 bits_offset, 4159 struct btf_show *show) 4160 { 4161 const struct btf_enum64 *enums = btf_type_enum64(t); 4162 u32 i, nr_enums = btf_type_vlen(t); 4163 void *safe_data; 4164 s64 v; 4165 4166 safe_data = btf_show_start_type(show, t, type_id, data); 4167 if (!safe_data) 4168 return; 4169 4170 v = *(u64 *)safe_data; 4171 4172 for (i = 0; i < nr_enums; i++) { 4173 if (v != btf_enum64_value(enums + i)) 4174 continue; 4175 4176 btf_show_type_value(show, "%s", 4177 __btf_name_by_offset(btf, 4178 enums[i].name_off)); 4179 4180 btf_show_end_type(show); 4181 return; 4182 } 4183 4184 if (btf_type_kflag(t)) 4185 btf_show_type_value(show, "%lld", v); 4186 else 4187 btf_show_type_value(show, "%llu", v); 4188 btf_show_end_type(show); 4189 } 4190 4191 static struct btf_kind_operations enum64_ops = { 4192 .check_meta = btf_enum64_check_meta, 4193 .resolve = btf_df_resolve, 4194 .check_member = btf_enum_check_member, 4195 .check_kflag_member = btf_enum_check_kflag_member, 4196 .log_details = btf_enum_log, 4197 .show = btf_enum64_show, 4198 }; 4199 4200 static s32 btf_func_proto_check_meta(struct btf_verifier_env *env, 4201 const struct btf_type *t, 4202 u32 meta_left) 4203 { 4204 u32 meta_needed = btf_type_vlen(t) * sizeof(struct btf_param); 4205 4206 if (meta_left < meta_needed) { 4207 btf_verifier_log_basic(env, t, 4208 "meta_left:%u meta_needed:%u", 4209 meta_left, meta_needed); 4210 return -EINVAL; 4211 } 4212 4213 if (t->name_off) { 4214 btf_verifier_log_type(env, t, "Invalid name"); 4215 return -EINVAL; 4216 } 4217 4218 if (btf_type_kflag(t)) { 4219 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); 4220 return -EINVAL; 4221 } 4222 4223 btf_verifier_log_type(env, t, NULL); 4224 4225 return meta_needed; 4226 } 4227 4228 static void btf_func_proto_log(struct btf_verifier_env *env, 4229 const struct btf_type *t) 4230 { 4231 const struct btf_param *args = (const struct btf_param *)(t + 1); 4232 u16 nr_args = btf_type_vlen(t), i; 4233 4234 btf_verifier_log(env, "return=%u args=(", t->type); 4235 if (!nr_args) { 4236 btf_verifier_log(env, "void"); 4237 goto done; 4238 } 4239 4240 if (nr_args == 1 && !args[0].type) { 4241 /* Only one vararg */ 4242 btf_verifier_log(env, "vararg"); 4243 goto done; 4244 } 4245 4246 btf_verifier_log(env, "%u %s", args[0].type, 4247 __btf_name_by_offset(env->btf, 4248 args[0].name_off)); 4249 for (i = 1; i < nr_args - 1; i++) 4250 btf_verifier_log(env, ", %u %s", args[i].type, 4251 __btf_name_by_offset(env->btf, 4252 args[i].name_off)); 4253 4254 if (nr_args > 1) { 4255 const struct btf_param *last_arg = &args[nr_args - 1]; 4256 4257 if (last_arg->type) 4258 btf_verifier_log(env, ", %u %s", last_arg->type, 4259 __btf_name_by_offset(env->btf, 4260 last_arg->name_off)); 4261 else 4262 btf_verifier_log(env, ", vararg"); 4263 } 4264 4265 done: 4266 btf_verifier_log(env, ")"); 4267 } 4268 4269 static struct btf_kind_operations func_proto_ops = { 4270 .check_meta = btf_func_proto_check_meta, 4271 .resolve = btf_df_resolve, 4272 /* 4273 * BTF_KIND_FUNC_PROTO cannot be directly referred by 4274 * a struct's member. 4275 * 4276 * It should be a function pointer instead. 4277 * (i.e. struct's member -> BTF_KIND_PTR -> BTF_KIND_FUNC_PROTO) 4278 * 4279 * Hence, there is no btf_func_check_member(). 4280 */ 4281 .check_member = btf_df_check_member, 4282 .check_kflag_member = btf_df_check_kflag_member, 4283 .log_details = btf_func_proto_log, 4284 .show = btf_df_show, 4285 }; 4286 4287 static s32 btf_func_check_meta(struct btf_verifier_env *env, 4288 const struct btf_type *t, 4289 u32 meta_left) 4290 { 4291 if (!t->name_off || 4292 !btf_name_valid_identifier(env->btf, t->name_off)) { 4293 btf_verifier_log_type(env, t, "Invalid name"); 4294 return -EINVAL; 4295 } 4296 4297 if (btf_type_vlen(t) > BTF_FUNC_GLOBAL) { 4298 btf_verifier_log_type(env, t, "Invalid func linkage"); 4299 return -EINVAL; 4300 } 4301 4302 if (btf_type_kflag(t)) { 4303 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); 4304 return -EINVAL; 4305 } 4306 4307 btf_verifier_log_type(env, t, NULL); 4308 4309 return 0; 4310 } 4311 4312 static int btf_func_resolve(struct btf_verifier_env *env, 4313 const struct resolve_vertex *v) 4314 { 4315 const struct btf_type *t = v->t; 4316 u32 next_type_id = t->type; 4317 int err; 4318 4319 err = btf_func_check(env, t); 4320 if (err) 4321 return err; 4322 4323 env_stack_pop_resolved(env, next_type_id, 0); 4324 return 0; 4325 } 4326 4327 static struct btf_kind_operations func_ops = { 4328 .check_meta = btf_func_check_meta, 4329 .resolve = btf_func_resolve, 4330 .check_member = btf_df_check_member, 4331 .check_kflag_member = btf_df_check_kflag_member, 4332 .log_details = btf_ref_type_log, 4333 .show = btf_df_show, 4334 }; 4335 4336 static s32 btf_var_check_meta(struct btf_verifier_env *env, 4337 const struct btf_type *t, 4338 u32 meta_left) 4339 { 4340 const struct btf_var *var; 4341 u32 meta_needed = sizeof(*var); 4342 4343 if (meta_left < meta_needed) { 4344 btf_verifier_log_basic(env, t, 4345 "meta_left:%u meta_needed:%u", 4346 meta_left, meta_needed); 4347 return -EINVAL; 4348 } 4349 4350 if (btf_type_vlen(t)) { 4351 btf_verifier_log_type(env, t, "vlen != 0"); 4352 return -EINVAL; 4353 } 4354 4355 if (btf_type_kflag(t)) { 4356 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); 4357 return -EINVAL; 4358 } 4359 4360 if (!t->name_off || 4361 !__btf_name_valid(env->btf, t->name_off, true)) { 4362 btf_verifier_log_type(env, t, "Invalid name"); 4363 return -EINVAL; 4364 } 4365 4366 /* A var cannot be in type void */ 4367 if (!t->type || !BTF_TYPE_ID_VALID(t->type)) { 4368 btf_verifier_log_type(env, t, "Invalid type_id"); 4369 return -EINVAL; 4370 } 4371 4372 var = btf_type_var(t); 4373 if (var->linkage != BTF_VAR_STATIC && 4374 var->linkage != BTF_VAR_GLOBAL_ALLOCATED) { 4375 btf_verifier_log_type(env, t, "Linkage not supported"); 4376 return -EINVAL; 4377 } 4378 4379 btf_verifier_log_type(env, t, NULL); 4380 4381 return meta_needed; 4382 } 4383 4384 static void btf_var_log(struct btf_verifier_env *env, const struct btf_type *t) 4385 { 4386 const struct btf_var *var = btf_type_var(t); 4387 4388 btf_verifier_log(env, "type_id=%u linkage=%u", t->type, var->linkage); 4389 } 4390 4391 static const struct btf_kind_operations var_ops = { 4392 .check_meta = btf_var_check_meta, 4393 .resolve = btf_var_resolve, 4394 .check_member = btf_df_check_member, 4395 .check_kflag_member = btf_df_check_kflag_member, 4396 .log_details = btf_var_log, 4397 .show = btf_var_show, 4398 }; 4399 4400 static s32 btf_datasec_check_meta(struct btf_verifier_env *env, 4401 const struct btf_type *t, 4402 u32 meta_left) 4403 { 4404 const struct btf_var_secinfo *vsi; 4405 u64 last_vsi_end_off = 0, sum = 0; 4406 u32 i, meta_needed; 4407 4408 meta_needed = btf_type_vlen(t) * sizeof(*vsi); 4409 if (meta_left < meta_needed) { 4410 btf_verifier_log_basic(env, t, 4411 "meta_left:%u meta_needed:%u", 4412 meta_left, meta_needed); 4413 return -EINVAL; 4414 } 4415 4416 if (!t->size) { 4417 btf_verifier_log_type(env, t, "size == 0"); 4418 return -EINVAL; 4419 } 4420 4421 if (btf_type_kflag(t)) { 4422 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); 4423 return -EINVAL; 4424 } 4425 4426 if (!t->name_off || 4427 !btf_name_valid_section(env->btf, t->name_off)) { 4428 btf_verifier_log_type(env, t, "Invalid name"); 4429 return -EINVAL; 4430 } 4431 4432 btf_verifier_log_type(env, t, NULL); 4433 4434 for_each_vsi(i, t, vsi) { 4435 /* A var cannot be in type void */ 4436 if (!vsi->type || !BTF_TYPE_ID_VALID(vsi->type)) { 4437 btf_verifier_log_vsi(env, t, vsi, 4438 "Invalid type_id"); 4439 return -EINVAL; 4440 } 4441 4442 if (vsi->offset < last_vsi_end_off || vsi->offset >= t->size) { 4443 btf_verifier_log_vsi(env, t, vsi, 4444 "Invalid offset"); 4445 return -EINVAL; 4446 } 4447 4448 if (!vsi->size || vsi->size > t->size) { 4449 btf_verifier_log_vsi(env, t, vsi, 4450 "Invalid size"); 4451 return -EINVAL; 4452 } 4453 4454 last_vsi_end_off = vsi->offset + vsi->size; 4455 if (last_vsi_end_off > t->size) { 4456 btf_verifier_log_vsi(env, t, vsi, 4457 "Invalid offset+size"); 4458 return -EINVAL; 4459 } 4460 4461 btf_verifier_log_vsi(env, t, vsi, NULL); 4462 sum += vsi->size; 4463 } 4464 4465 if (t->size < sum) { 4466 btf_verifier_log_type(env, t, "Invalid btf_info size"); 4467 return -EINVAL; 4468 } 4469 4470 return meta_needed; 4471 } 4472 4473 static int btf_datasec_resolve(struct btf_verifier_env *env, 4474 const struct resolve_vertex *v) 4475 { 4476 const struct btf_var_secinfo *vsi; 4477 struct btf *btf = env->btf; 4478 u16 i; 4479 4480 for_each_vsi_from(i, v->next_member, v->t, vsi) { 4481 u32 var_type_id = vsi->type, type_id, type_size = 0; 4482 const struct btf_type *var_type = btf_type_by_id(env->btf, 4483 var_type_id); 4484 if (!var_type || !btf_type_is_var(var_type)) { 4485 btf_verifier_log_vsi(env, v->t, vsi, 4486 "Not a VAR kind member"); 4487 return -EINVAL; 4488 } 4489 4490 if (!env_type_is_resolve_sink(env, var_type) && 4491 !env_type_is_resolved(env, var_type_id)) { 4492 env_stack_set_next_member(env, i + 1); 4493 return env_stack_push(env, var_type, var_type_id); 4494 } 4495 4496 type_id = var_type->type; 4497 if (!btf_type_id_size(btf, &type_id, &type_size)) { 4498 btf_verifier_log_vsi(env, v->t, vsi, "Invalid type"); 4499 return -EINVAL; 4500 } 4501 4502 if (vsi->size < type_size) { 4503 btf_verifier_log_vsi(env, v->t, vsi, "Invalid size"); 4504 return -EINVAL; 4505 } 4506 } 4507 4508 env_stack_pop_resolved(env, 0, 0); 4509 return 0; 4510 } 4511 4512 static void btf_datasec_log(struct btf_verifier_env *env, 4513 const struct btf_type *t) 4514 { 4515 btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t)); 4516 } 4517 4518 static void btf_datasec_show(const struct btf *btf, 4519 const struct btf_type *t, u32 type_id, 4520 void *data, u8 bits_offset, 4521 struct btf_show *show) 4522 { 4523 const struct btf_var_secinfo *vsi; 4524 const struct btf_type *var; 4525 u32 i; 4526 4527 if (!btf_show_start_type(show, t, type_id, data)) 4528 return; 4529 4530 btf_show_type_value(show, "section (\"%s\") = {", 4531 __btf_name_by_offset(btf, t->name_off)); 4532 for_each_vsi(i, t, vsi) { 4533 var = btf_type_by_id(btf, vsi->type); 4534 if (i) 4535 btf_show(show, ","); 4536 btf_type_ops(var)->show(btf, var, vsi->type, 4537 data + vsi->offset, bits_offset, show); 4538 } 4539 btf_show_end_type(show); 4540 } 4541 4542 static const struct btf_kind_operations datasec_ops = { 4543 .check_meta = btf_datasec_check_meta, 4544 .resolve = btf_datasec_resolve, 4545 .check_member = btf_df_check_member, 4546 .check_kflag_member = btf_df_check_kflag_member, 4547 .log_details = btf_datasec_log, 4548 .show = btf_datasec_show, 4549 }; 4550 4551 static s32 btf_float_check_meta(struct btf_verifier_env *env, 4552 const struct btf_type *t, 4553 u32 meta_left) 4554 { 4555 if (btf_type_vlen(t)) { 4556 btf_verifier_log_type(env, t, "vlen != 0"); 4557 return -EINVAL; 4558 } 4559 4560 if (btf_type_kflag(t)) { 4561 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); 4562 return -EINVAL; 4563 } 4564 4565 if (t->size != 2 && t->size != 4 && t->size != 8 && t->size != 12 && 4566 t->size != 16) { 4567 btf_verifier_log_type(env, t, "Invalid type_size"); 4568 return -EINVAL; 4569 } 4570 4571 btf_verifier_log_type(env, t, NULL); 4572 4573 return 0; 4574 } 4575 4576 static int btf_float_check_member(struct btf_verifier_env *env, 4577 const struct btf_type *struct_type, 4578 const struct btf_member *member, 4579 const struct btf_type *member_type) 4580 { 4581 u64 start_offset_bytes; 4582 u64 end_offset_bytes; 4583 u64 misalign_bits; 4584 u64 align_bytes; 4585 u64 align_bits; 4586 4587 /* Different architectures have different alignment requirements, so 4588 * here we check only for the reasonable minimum. This way we ensure 4589 * that types after CO-RE can pass the kernel BTF verifier. 4590 */ 4591 align_bytes = min_t(u64, sizeof(void *), member_type->size); 4592 align_bits = align_bytes * BITS_PER_BYTE; 4593 div64_u64_rem(member->offset, align_bits, &misalign_bits); 4594 if (misalign_bits) { 4595 btf_verifier_log_member(env, struct_type, member, 4596 "Member is not properly aligned"); 4597 return -EINVAL; 4598 } 4599 4600 start_offset_bytes = member->offset / BITS_PER_BYTE; 4601 end_offset_bytes = start_offset_bytes + member_type->size; 4602 if (end_offset_bytes > struct_type->size) { 4603 btf_verifier_log_member(env, struct_type, member, 4604 "Member exceeds struct_size"); 4605 return -EINVAL; 4606 } 4607 4608 return 0; 4609 } 4610 4611 static void btf_float_log(struct btf_verifier_env *env, 4612 const struct btf_type *t) 4613 { 4614 btf_verifier_log(env, "size=%u", t->size); 4615 } 4616 4617 static const struct btf_kind_operations float_ops = { 4618 .check_meta = btf_float_check_meta, 4619 .resolve = btf_df_resolve, 4620 .check_member = btf_float_check_member, 4621 .check_kflag_member = btf_generic_check_kflag_member, 4622 .log_details = btf_float_log, 4623 .show = btf_df_show, 4624 }; 4625 4626 static s32 btf_decl_tag_check_meta(struct btf_verifier_env *env, 4627 const struct btf_type *t, 4628 u32 meta_left) 4629 { 4630 const struct btf_decl_tag *tag; 4631 u32 meta_needed = sizeof(*tag); 4632 s32 component_idx; 4633 const char *value; 4634 4635 if (meta_left < meta_needed) { 4636 btf_verifier_log_basic(env, t, 4637 "meta_left:%u meta_needed:%u", 4638 meta_left, meta_needed); 4639 return -EINVAL; 4640 } 4641 4642 value = btf_name_by_offset(env->btf, t->name_off); 4643 if (!value || !value[0]) { 4644 btf_verifier_log_type(env, t, "Invalid value"); 4645 return -EINVAL; 4646 } 4647 4648 if (btf_type_vlen(t)) { 4649 btf_verifier_log_type(env, t, "vlen != 0"); 4650 return -EINVAL; 4651 } 4652 4653 if (btf_type_kflag(t)) { 4654 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); 4655 return -EINVAL; 4656 } 4657 4658 component_idx = btf_type_decl_tag(t)->component_idx; 4659 if (component_idx < -1) { 4660 btf_verifier_log_type(env, t, "Invalid component_idx"); 4661 return -EINVAL; 4662 } 4663 4664 btf_verifier_log_type(env, t, NULL); 4665 4666 return meta_needed; 4667 } 4668 4669 static int btf_decl_tag_resolve(struct btf_verifier_env *env, 4670 const struct resolve_vertex *v) 4671 { 4672 const struct btf_type *next_type; 4673 const struct btf_type *t = v->t; 4674 u32 next_type_id = t->type; 4675 struct btf *btf = env->btf; 4676 s32 component_idx; 4677 u32 vlen; 4678 4679 next_type = btf_type_by_id(btf, next_type_id); 4680 if (!next_type || !btf_type_is_decl_tag_target(next_type)) { 4681 btf_verifier_log_type(env, v->t, "Invalid type_id"); 4682 return -EINVAL; 4683 } 4684 4685 if (!env_type_is_resolve_sink(env, next_type) && 4686 !env_type_is_resolved(env, next_type_id)) 4687 return env_stack_push(env, next_type, next_type_id); 4688 4689 component_idx = btf_type_decl_tag(t)->component_idx; 4690 if (component_idx != -1) { 4691 if (btf_type_is_var(next_type) || btf_type_is_typedef(next_type)) { 4692 btf_verifier_log_type(env, v->t, "Invalid component_idx"); 4693 return -EINVAL; 4694 } 4695 4696 if (btf_type_is_struct(next_type)) { 4697 vlen = btf_type_vlen(next_type); 4698 } else { 4699 /* next_type should be a function */ 4700 next_type = btf_type_by_id(btf, next_type->type); 4701 vlen = btf_type_vlen(next_type); 4702 } 4703 4704 if ((u32)component_idx >= vlen) { 4705 btf_verifier_log_type(env, v->t, "Invalid component_idx"); 4706 return -EINVAL; 4707 } 4708 } 4709 4710 env_stack_pop_resolved(env, next_type_id, 0); 4711 4712 return 0; 4713 } 4714 4715 static void btf_decl_tag_log(struct btf_verifier_env *env, const struct btf_type *t) 4716 { 4717 btf_verifier_log(env, "type=%u component_idx=%d", t->type, 4718 btf_type_decl_tag(t)->component_idx); 4719 } 4720 4721 static const struct btf_kind_operations decl_tag_ops = { 4722 .check_meta = btf_decl_tag_check_meta, 4723 .resolve = btf_decl_tag_resolve, 4724 .check_member = btf_df_check_member, 4725 .check_kflag_member = btf_df_check_kflag_member, 4726 .log_details = btf_decl_tag_log, 4727 .show = btf_df_show, 4728 }; 4729 4730 static int btf_func_proto_check(struct btf_verifier_env *env, 4731 const struct btf_type *t) 4732 { 4733 const struct btf_type *ret_type; 4734 const struct btf_param *args; 4735 const struct btf *btf; 4736 u16 nr_args, i; 4737 int err; 4738 4739 btf = env->btf; 4740 args = (const struct btf_param *)(t + 1); 4741 nr_args = btf_type_vlen(t); 4742 4743 /* Check func return type which could be "void" (t->type == 0) */ 4744 if (t->type) { 4745 u32 ret_type_id = t->type; 4746 4747 ret_type = btf_type_by_id(btf, ret_type_id); 4748 if (!ret_type) { 4749 btf_verifier_log_type(env, t, "Invalid return type"); 4750 return -EINVAL; 4751 } 4752 4753 if (btf_type_is_resolve_source_only(ret_type)) { 4754 btf_verifier_log_type(env, t, "Invalid return type"); 4755 return -EINVAL; 4756 } 4757 4758 if (btf_type_needs_resolve(ret_type) && 4759 !env_type_is_resolved(env, ret_type_id)) { 4760 err = btf_resolve(env, ret_type, ret_type_id); 4761 if (err) 4762 return err; 4763 } 4764 4765 /* Ensure the return type is a type that has a size */ 4766 if (!btf_type_id_size(btf, &ret_type_id, NULL)) { 4767 btf_verifier_log_type(env, t, "Invalid return type"); 4768 return -EINVAL; 4769 } 4770 } 4771 4772 if (!nr_args) 4773 return 0; 4774 4775 /* Last func arg type_id could be 0 if it is a vararg */ 4776 if (!args[nr_args - 1].type) { 4777 if (args[nr_args - 1].name_off) { 4778 btf_verifier_log_type(env, t, "Invalid arg#%u", 4779 nr_args); 4780 return -EINVAL; 4781 } 4782 nr_args--; 4783 } 4784 4785 for (i = 0; i < nr_args; i++) { 4786 const struct btf_type *arg_type; 4787 u32 arg_type_id; 4788 4789 arg_type_id = args[i].type; 4790 arg_type = btf_type_by_id(btf, arg_type_id); 4791 if (!arg_type) { 4792 btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1); 4793 return -EINVAL; 4794 } 4795 4796 if (btf_type_is_resolve_source_only(arg_type)) { 4797 btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1); 4798 return -EINVAL; 4799 } 4800 4801 if (args[i].name_off && 4802 (!btf_name_offset_valid(btf, args[i].name_off) || 4803 !btf_name_valid_identifier(btf, args[i].name_off))) { 4804 btf_verifier_log_type(env, t, 4805 "Invalid arg#%u", i + 1); 4806 return -EINVAL; 4807 } 4808 4809 if (btf_type_needs_resolve(arg_type) && 4810 !env_type_is_resolved(env, arg_type_id)) { 4811 err = btf_resolve(env, arg_type, arg_type_id); 4812 if (err) 4813 return err; 4814 } 4815 4816 if (!btf_type_id_size(btf, &arg_type_id, NULL)) { 4817 btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1); 4818 return -EINVAL; 4819 } 4820 } 4821 4822 return 0; 4823 } 4824 4825 static int btf_func_check(struct btf_verifier_env *env, 4826 const struct btf_type *t) 4827 { 4828 const struct btf_type *proto_type; 4829 const struct btf_param *args; 4830 const struct btf *btf; 4831 u16 nr_args, i; 4832 4833 btf = env->btf; 4834 proto_type = btf_type_by_id(btf, t->type); 4835 4836 if (!proto_type || !btf_type_is_func_proto(proto_type)) { 4837 btf_verifier_log_type(env, t, "Invalid type_id"); 4838 return -EINVAL; 4839 } 4840 4841 args = (const struct btf_param *)(proto_type + 1); 4842 nr_args = btf_type_vlen(proto_type); 4843 for (i = 0; i < nr_args; i++) { 4844 if (!args[i].name_off && args[i].type) { 4845 btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1); 4846 return -EINVAL; 4847 } 4848 } 4849 4850 return 0; 4851 } 4852 4853 static const struct btf_kind_operations * const kind_ops[NR_BTF_KINDS] = { 4854 [BTF_KIND_INT] = &int_ops, 4855 [BTF_KIND_PTR] = &ptr_ops, 4856 [BTF_KIND_ARRAY] = &array_ops, 4857 [BTF_KIND_STRUCT] = &struct_ops, 4858 [BTF_KIND_UNION] = &struct_ops, 4859 [BTF_KIND_ENUM] = &enum_ops, 4860 [BTF_KIND_FWD] = &fwd_ops, 4861 [BTF_KIND_TYPEDEF] = &modifier_ops, 4862 [BTF_KIND_VOLATILE] = &modifier_ops, 4863 [BTF_KIND_CONST] = &modifier_ops, 4864 [BTF_KIND_RESTRICT] = &modifier_ops, 4865 [BTF_KIND_FUNC] = &func_ops, 4866 [BTF_KIND_FUNC_PROTO] = &func_proto_ops, 4867 [BTF_KIND_VAR] = &var_ops, 4868 [BTF_KIND_DATASEC] = &datasec_ops, 4869 [BTF_KIND_FLOAT] = &float_ops, 4870 [BTF_KIND_DECL_TAG] = &decl_tag_ops, 4871 [BTF_KIND_TYPE_TAG] = &modifier_ops, 4872 [BTF_KIND_ENUM64] = &enum64_ops, 4873 }; 4874 4875 static s32 btf_check_meta(struct btf_verifier_env *env, 4876 const struct btf_type *t, 4877 u32 meta_left) 4878 { 4879 u32 saved_meta_left = meta_left; 4880 s32 var_meta_size; 4881 4882 if (meta_left < sizeof(*t)) { 4883 btf_verifier_log(env, "[%u] meta_left:%u meta_needed:%zu", 4884 env->log_type_id, meta_left, sizeof(*t)); 4885 return -EINVAL; 4886 } 4887 meta_left -= sizeof(*t); 4888 4889 if (t->info & ~BTF_INFO_MASK) { 4890 btf_verifier_log(env, "[%u] Invalid btf_info:%x", 4891 env->log_type_id, t->info); 4892 return -EINVAL; 4893 } 4894 4895 if (BTF_INFO_KIND(t->info) > BTF_KIND_MAX || 4896 BTF_INFO_KIND(t->info) == BTF_KIND_UNKN) { 4897 btf_verifier_log(env, "[%u] Invalid kind:%u", 4898 env->log_type_id, BTF_INFO_KIND(t->info)); 4899 return -EINVAL; 4900 } 4901 4902 if (!btf_name_offset_valid(env->btf, t->name_off)) { 4903 btf_verifier_log(env, "[%u] Invalid name_offset:%u", 4904 env->log_type_id, t->name_off); 4905 return -EINVAL; 4906 } 4907 4908 var_meta_size = btf_type_ops(t)->check_meta(env, t, meta_left); 4909 if (var_meta_size < 0) 4910 return var_meta_size; 4911 4912 meta_left -= var_meta_size; 4913 4914 return saved_meta_left - meta_left; 4915 } 4916 4917 static int btf_check_all_metas(struct btf_verifier_env *env) 4918 { 4919 struct btf *btf = env->btf; 4920 struct btf_header *hdr; 4921 void *cur, *end; 4922 4923 hdr = &btf->hdr; 4924 cur = btf->nohdr_data + hdr->type_off; 4925 end = cur + hdr->type_len; 4926 4927 env->log_type_id = btf->base_btf ? btf->start_id : 1; 4928 while (cur < end) { 4929 struct btf_type *t = cur; 4930 s32 meta_size; 4931 4932 meta_size = btf_check_meta(env, t, end - cur); 4933 if (meta_size < 0) 4934 return meta_size; 4935 4936 btf_add_type(env, t); 4937 cur += meta_size; 4938 env->log_type_id++; 4939 } 4940 4941 return 0; 4942 } 4943 4944 static bool btf_resolve_valid(struct btf_verifier_env *env, 4945 const struct btf_type *t, 4946 u32 type_id) 4947 { 4948 struct btf *btf = env->btf; 4949 4950 if (!env_type_is_resolved(env, type_id)) 4951 return false; 4952 4953 if (btf_type_is_struct(t) || btf_type_is_datasec(t)) 4954 return !btf_resolved_type_id(btf, type_id) && 4955 !btf_resolved_type_size(btf, type_id); 4956 4957 if (btf_type_is_decl_tag(t) || btf_type_is_func(t)) 4958 return btf_resolved_type_id(btf, type_id) && 4959 !btf_resolved_type_size(btf, type_id); 4960 4961 if (btf_type_is_modifier(t) || btf_type_is_ptr(t) || 4962 btf_type_is_var(t)) { 4963 t = btf_type_id_resolve(btf, &type_id); 4964 return t && 4965 !btf_type_is_modifier(t) && 4966 !btf_type_is_var(t) && 4967 !btf_type_is_datasec(t); 4968 } 4969 4970 if (btf_type_is_array(t)) { 4971 const struct btf_array *array = btf_type_array(t); 4972 const struct btf_type *elem_type; 4973 u32 elem_type_id = array->type; 4974 u32 elem_size; 4975 4976 elem_type = btf_type_id_size(btf, &elem_type_id, &elem_size); 4977 return elem_type && !btf_type_is_modifier(elem_type) && 4978 (array->nelems * elem_size == 4979 btf_resolved_type_size(btf, type_id)); 4980 } 4981 4982 return false; 4983 } 4984 4985 static int btf_resolve(struct btf_verifier_env *env, 4986 const struct btf_type *t, u32 type_id) 4987 { 4988 u32 save_log_type_id = env->log_type_id; 4989 const struct resolve_vertex *v; 4990 int err = 0; 4991 4992 env->resolve_mode = RESOLVE_TBD; 4993 env_stack_push(env, t, type_id); 4994 while (!err && (v = env_stack_peak(env))) { 4995 env->log_type_id = v->type_id; 4996 err = btf_type_ops(v->t)->resolve(env, v); 4997 } 4998 4999 env->log_type_id = type_id; 5000 if (err == -E2BIG) { 5001 btf_verifier_log_type(env, t, 5002 "Exceeded max resolving depth:%u", 5003 MAX_RESOLVE_DEPTH); 5004 } else if (err == -EEXIST) { 5005 btf_verifier_log_type(env, t, "Loop detected"); 5006 } 5007 5008 /* Final sanity check */ 5009 if (!err && !btf_resolve_valid(env, t, type_id)) { 5010 btf_verifier_log_type(env, t, "Invalid resolve state"); 5011 err = -EINVAL; 5012 } 5013 5014 env->log_type_id = save_log_type_id; 5015 return err; 5016 } 5017 5018 static int btf_check_all_types(struct btf_verifier_env *env) 5019 { 5020 struct btf *btf = env->btf; 5021 const struct btf_type *t; 5022 u32 type_id, i; 5023 int err; 5024 5025 err = env_resolve_init(env); 5026 if (err) 5027 return err; 5028 5029 env->phase++; 5030 for (i = btf->base_btf ? 0 : 1; i < btf->nr_types; i++) { 5031 type_id = btf->start_id + i; 5032 t = btf_type_by_id(btf, type_id); 5033 5034 env->log_type_id = type_id; 5035 if (btf_type_needs_resolve(t) && 5036 !env_type_is_resolved(env, type_id)) { 5037 err = btf_resolve(env, t, type_id); 5038 if (err) 5039 return err; 5040 } 5041 5042 if (btf_type_is_func_proto(t)) { 5043 err = btf_func_proto_check(env, t); 5044 if (err) 5045 return err; 5046 } 5047 } 5048 5049 return 0; 5050 } 5051 5052 static int btf_parse_type_sec(struct btf_verifier_env *env) 5053 { 5054 const struct btf_header *hdr = &env->btf->hdr; 5055 int err; 5056 5057 /* Type section must align to 4 bytes */ 5058 if (hdr->type_off & (sizeof(u32) - 1)) { 5059 btf_verifier_log(env, "Unaligned type_off"); 5060 return -EINVAL; 5061 } 5062 5063 if (!env->btf->base_btf && !hdr->type_len) { 5064 btf_verifier_log(env, "No type found"); 5065 return -EINVAL; 5066 } 5067 5068 err = btf_check_all_metas(env); 5069 if (err) 5070 return err; 5071 5072 return btf_check_all_types(env); 5073 } 5074 5075 static int btf_parse_str_sec(struct btf_verifier_env *env) 5076 { 5077 const struct btf_header *hdr; 5078 struct btf *btf = env->btf; 5079 const char *start, *end; 5080 5081 hdr = &btf->hdr; 5082 start = btf->nohdr_data + hdr->str_off; 5083 end = start + hdr->str_len; 5084 5085 if (end != btf->data + btf->data_size) { 5086 btf_verifier_log(env, "String section is not at the end"); 5087 return -EINVAL; 5088 } 5089 5090 btf->strings = start; 5091 5092 if (btf->base_btf && !hdr->str_len) 5093 return 0; 5094 if (!hdr->str_len || hdr->str_len - 1 > BTF_MAX_NAME_OFFSET || end[-1]) { 5095 btf_verifier_log(env, "Invalid string section"); 5096 return -EINVAL; 5097 } 5098 if (!btf->base_btf && start[0]) { 5099 btf_verifier_log(env, "Invalid string section"); 5100 return -EINVAL; 5101 } 5102 5103 return 0; 5104 } 5105 5106 static const size_t btf_sec_info_offset[] = { 5107 offsetof(struct btf_header, type_off), 5108 offsetof(struct btf_header, str_off), 5109 }; 5110 5111 static int btf_sec_info_cmp(const void *a, const void *b) 5112 { 5113 const struct btf_sec_info *x = a; 5114 const struct btf_sec_info *y = b; 5115 5116 return (int)(x->off - y->off) ? : (int)(x->len - y->len); 5117 } 5118 5119 static int btf_check_sec_info(struct btf_verifier_env *env, 5120 u32 btf_data_size) 5121 { 5122 struct btf_sec_info secs[ARRAY_SIZE(btf_sec_info_offset)]; 5123 u32 total, expected_total, i; 5124 const struct btf_header *hdr; 5125 const struct btf *btf; 5126 5127 btf = env->btf; 5128 hdr = &btf->hdr; 5129 5130 /* Populate the secs from hdr */ 5131 for (i = 0; i < ARRAY_SIZE(btf_sec_info_offset); i++) 5132 secs[i] = *(struct btf_sec_info *)((void *)hdr + 5133 btf_sec_info_offset[i]); 5134 5135 sort(secs, ARRAY_SIZE(btf_sec_info_offset), 5136 sizeof(struct btf_sec_info), btf_sec_info_cmp, NULL); 5137 5138 /* Check for gaps and overlap among sections */ 5139 total = 0; 5140 expected_total = btf_data_size - hdr->hdr_len; 5141 for (i = 0; i < ARRAY_SIZE(btf_sec_info_offset); i++) { 5142 if (expected_total < secs[i].off) { 5143 btf_verifier_log(env, "Invalid section offset"); 5144 return -EINVAL; 5145 } 5146 if (total < secs[i].off) { 5147 /* gap */ 5148 btf_verifier_log(env, "Unsupported section found"); 5149 return -EINVAL; 5150 } 5151 if (total > secs[i].off) { 5152 btf_verifier_log(env, "Section overlap found"); 5153 return -EINVAL; 5154 } 5155 if (expected_total - total < secs[i].len) { 5156 btf_verifier_log(env, 5157 "Total section length too long"); 5158 return -EINVAL; 5159 } 5160 total += secs[i].len; 5161 } 5162 5163 /* There is data other than hdr and known sections */ 5164 if (expected_total != total) { 5165 btf_verifier_log(env, "Unsupported section found"); 5166 return -EINVAL; 5167 } 5168 5169 return 0; 5170 } 5171 5172 static int btf_parse_hdr(struct btf_verifier_env *env) 5173 { 5174 u32 hdr_len, hdr_copy, btf_data_size; 5175 const struct btf_header *hdr; 5176 struct btf *btf; 5177 5178 btf = env->btf; 5179 btf_data_size = btf->data_size; 5180 5181 if (btf_data_size < offsetofend(struct btf_header, hdr_len)) { 5182 btf_verifier_log(env, "hdr_len not found"); 5183 return -EINVAL; 5184 } 5185 5186 hdr = btf->data; 5187 hdr_len = hdr->hdr_len; 5188 if (btf_data_size < hdr_len) { 5189 btf_verifier_log(env, "btf_header not found"); 5190 return -EINVAL; 5191 } 5192 5193 /* Ensure the unsupported header fields are zero */ 5194 if (hdr_len > sizeof(btf->hdr)) { 5195 u8 *expected_zero = btf->data + sizeof(btf->hdr); 5196 u8 *end = btf->data + hdr_len; 5197 5198 for (; expected_zero < end; expected_zero++) { 5199 if (*expected_zero) { 5200 btf_verifier_log(env, "Unsupported btf_header"); 5201 return -E2BIG; 5202 } 5203 } 5204 } 5205 5206 hdr_copy = min_t(u32, hdr_len, sizeof(btf->hdr)); 5207 memcpy(&btf->hdr, btf->data, hdr_copy); 5208 5209 hdr = &btf->hdr; 5210 5211 btf_verifier_log_hdr(env, btf_data_size); 5212 5213 if (hdr->magic != BTF_MAGIC) { 5214 btf_verifier_log(env, "Invalid magic"); 5215 return -EINVAL; 5216 } 5217 5218 if (hdr->version != BTF_VERSION) { 5219 btf_verifier_log(env, "Unsupported version"); 5220 return -ENOTSUPP; 5221 } 5222 5223 if (hdr->flags) { 5224 btf_verifier_log(env, "Unsupported flags"); 5225 return -ENOTSUPP; 5226 } 5227 5228 if (!btf->base_btf && btf_data_size == hdr->hdr_len) { 5229 btf_verifier_log(env, "No data"); 5230 return -EINVAL; 5231 } 5232 5233 return btf_check_sec_info(env, btf_data_size); 5234 } 5235 5236 static const char *alloc_obj_fields[] = { 5237 "bpf_spin_lock", 5238 "bpf_list_head", 5239 "bpf_list_node", 5240 }; 5241 5242 static struct btf_struct_metas * 5243 btf_parse_struct_metas(struct bpf_verifier_log *log, struct btf *btf) 5244 { 5245 union { 5246 struct btf_id_set set; 5247 struct { 5248 u32 _cnt; 5249 u32 _ids[ARRAY_SIZE(alloc_obj_fields)]; 5250 } _arr; 5251 } aof; 5252 struct btf_struct_metas *tab = NULL; 5253 int i, n, id, ret; 5254 5255 BUILD_BUG_ON(offsetof(struct btf_id_set, cnt) != 0); 5256 BUILD_BUG_ON(sizeof(struct btf_id_set) != sizeof(u32)); 5257 5258 memset(&aof, 0, sizeof(aof)); 5259 for (i = 0; i < ARRAY_SIZE(alloc_obj_fields); i++) { 5260 /* Try to find whether this special type exists in user BTF, and 5261 * if so remember its ID so we can easily find it among members 5262 * of structs that we iterate in the next loop. 5263 */ 5264 id = btf_find_by_name_kind(btf, alloc_obj_fields[i], BTF_KIND_STRUCT); 5265 if (id < 0) 5266 continue; 5267 aof.set.ids[aof.set.cnt++] = id; 5268 } 5269 5270 if (!aof.set.cnt) 5271 return NULL; 5272 sort(&aof.set.ids, aof.set.cnt, sizeof(aof.set.ids[0]), btf_id_cmp_func, NULL); 5273 5274 n = btf_nr_types(btf); 5275 for (i = 1; i < n; i++) { 5276 struct btf_struct_metas *new_tab; 5277 const struct btf_member *member; 5278 struct btf_field_offs *foffs; 5279 struct btf_struct_meta *type; 5280 struct btf_record *record; 5281 const struct btf_type *t; 5282 int j, tab_cnt; 5283 5284 t = btf_type_by_id(btf, i); 5285 if (!t) { 5286 ret = -EINVAL; 5287 goto free; 5288 } 5289 if (!__btf_type_is_struct(t)) 5290 continue; 5291 5292 cond_resched(); 5293 5294 for_each_member(j, t, member) { 5295 if (btf_id_set_contains(&aof.set, member->type)) 5296 goto parse; 5297 } 5298 continue; 5299 parse: 5300 tab_cnt = tab ? tab->cnt : 0; 5301 new_tab = krealloc(tab, offsetof(struct btf_struct_metas, types[tab_cnt + 1]), 5302 GFP_KERNEL | __GFP_NOWARN); 5303 if (!new_tab) { 5304 ret = -ENOMEM; 5305 goto free; 5306 } 5307 if (!tab) 5308 new_tab->cnt = 0; 5309 tab = new_tab; 5310 5311 type = &tab->types[tab->cnt]; 5312 type->btf_id = i; 5313 record = btf_parse_fields(btf, t, BPF_SPIN_LOCK | BPF_LIST_HEAD | BPF_LIST_NODE, t->size); 5314 /* The record cannot be unset, treat it as an error if so */ 5315 if (IS_ERR_OR_NULL(record)) { 5316 ret = PTR_ERR_OR_ZERO(record) ?: -EFAULT; 5317 goto free; 5318 } 5319 foffs = btf_parse_field_offs(record); 5320 /* We need the field_offs to be valid for a valid record, 5321 * either both should be set or both should be unset. 5322 */ 5323 if (IS_ERR_OR_NULL(foffs)) { 5324 btf_record_free(record); 5325 ret = -EFAULT; 5326 goto free; 5327 } 5328 type->record = record; 5329 type->field_offs = foffs; 5330 tab->cnt++; 5331 } 5332 return tab; 5333 free: 5334 btf_struct_metas_free(tab); 5335 return ERR_PTR(ret); 5336 } 5337 5338 struct btf_struct_meta *btf_find_struct_meta(const struct btf *btf, u32 btf_id) 5339 { 5340 struct btf_struct_metas *tab; 5341 5342 BUILD_BUG_ON(offsetof(struct btf_struct_meta, btf_id) != 0); 5343 tab = btf->struct_meta_tab; 5344 if (!tab) 5345 return NULL; 5346 return bsearch(&btf_id, tab->types, tab->cnt, sizeof(tab->types[0]), btf_id_cmp_func); 5347 } 5348 5349 static int btf_check_type_tags(struct btf_verifier_env *env, 5350 struct btf *btf, int start_id) 5351 { 5352 int i, n, good_id = start_id - 1; 5353 bool in_tags; 5354 5355 n = btf_nr_types(btf); 5356 for (i = start_id; i < n; i++) { 5357 const struct btf_type *t; 5358 int chain_limit = 32; 5359 u32 cur_id = i; 5360 5361 t = btf_type_by_id(btf, i); 5362 if (!t) 5363 return -EINVAL; 5364 if (!btf_type_is_modifier(t)) 5365 continue; 5366 5367 cond_resched(); 5368 5369 in_tags = btf_type_is_type_tag(t); 5370 while (btf_type_is_modifier(t)) { 5371 if (!chain_limit--) { 5372 btf_verifier_log(env, "Max chain length or cycle detected"); 5373 return -ELOOP; 5374 } 5375 if (btf_type_is_type_tag(t)) { 5376 if (!in_tags) { 5377 btf_verifier_log(env, "Type tags don't precede modifiers"); 5378 return -EINVAL; 5379 } 5380 } else if (in_tags) { 5381 in_tags = false; 5382 } 5383 if (cur_id <= good_id) 5384 break; 5385 /* Move to next type */ 5386 cur_id = t->type; 5387 t = btf_type_by_id(btf, cur_id); 5388 if (!t) 5389 return -EINVAL; 5390 } 5391 good_id = i; 5392 } 5393 return 0; 5394 } 5395 5396 static struct btf *btf_parse(bpfptr_t btf_data, u32 btf_data_size, 5397 u32 log_level, char __user *log_ubuf, u32 log_size) 5398 { 5399 struct btf_struct_metas *struct_meta_tab; 5400 struct btf_verifier_env *env = NULL; 5401 struct bpf_verifier_log *log; 5402 struct btf *btf = NULL; 5403 u8 *data; 5404 int err; 5405 5406 if (btf_data_size > BTF_MAX_SIZE) 5407 return ERR_PTR(-E2BIG); 5408 5409 env = kzalloc(sizeof(*env), GFP_KERNEL | __GFP_NOWARN); 5410 if (!env) 5411 return ERR_PTR(-ENOMEM); 5412 5413 log = &env->log; 5414 if (log_level || log_ubuf || log_size) { 5415 /* user requested verbose verifier output 5416 * and supplied buffer to store the verification trace 5417 */ 5418 log->level = log_level; 5419 log->ubuf = log_ubuf; 5420 log->len_total = log_size; 5421 5422 /* log attributes have to be sane */ 5423 if (!bpf_verifier_log_attr_valid(log)) { 5424 err = -EINVAL; 5425 goto errout; 5426 } 5427 } 5428 5429 btf = kzalloc(sizeof(*btf), GFP_KERNEL | __GFP_NOWARN); 5430 if (!btf) { 5431 err = -ENOMEM; 5432 goto errout; 5433 } 5434 env->btf = btf; 5435 5436 data = kvmalloc(btf_data_size, GFP_KERNEL | __GFP_NOWARN); 5437 if (!data) { 5438 err = -ENOMEM; 5439 goto errout; 5440 } 5441 5442 btf->data = data; 5443 btf->data_size = btf_data_size; 5444 5445 if (copy_from_bpfptr(data, btf_data, btf_data_size)) { 5446 err = -EFAULT; 5447 goto errout; 5448 } 5449 5450 err = btf_parse_hdr(env); 5451 if (err) 5452 goto errout; 5453 5454 btf->nohdr_data = btf->data + btf->hdr.hdr_len; 5455 5456 err = btf_parse_str_sec(env); 5457 if (err) 5458 goto errout; 5459 5460 err = btf_parse_type_sec(env); 5461 if (err) 5462 goto errout; 5463 5464 err = btf_check_type_tags(env, btf, 1); 5465 if (err) 5466 goto errout; 5467 5468 struct_meta_tab = btf_parse_struct_metas(log, btf); 5469 if (IS_ERR(struct_meta_tab)) { 5470 err = PTR_ERR(struct_meta_tab); 5471 goto errout; 5472 } 5473 btf->struct_meta_tab = struct_meta_tab; 5474 5475 if (struct_meta_tab) { 5476 int i; 5477 5478 for (i = 0; i < struct_meta_tab->cnt; i++) { 5479 err = btf_check_and_fixup_fields(btf, struct_meta_tab->types[i].record); 5480 if (err < 0) 5481 goto errout_meta; 5482 } 5483 } 5484 5485 if (log->level && bpf_verifier_log_full(log)) { 5486 err = -ENOSPC; 5487 goto errout_meta; 5488 } 5489 5490 btf_verifier_env_free(env); 5491 refcount_set(&btf->refcnt, 1); 5492 return btf; 5493 5494 errout_meta: 5495 btf_free_struct_meta_tab(btf); 5496 errout: 5497 btf_verifier_env_free(env); 5498 if (btf) 5499 btf_free(btf); 5500 return ERR_PTR(err); 5501 } 5502 5503 extern char __weak __start_BTF[]; 5504 extern char __weak __stop_BTF[]; 5505 extern struct btf *btf_vmlinux; 5506 5507 #define BPF_MAP_TYPE(_id, _ops) 5508 #define BPF_LINK_TYPE(_id, _name) 5509 static union { 5510 struct bpf_ctx_convert { 5511 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \ 5512 prog_ctx_type _id##_prog; \ 5513 kern_ctx_type _id##_kern; 5514 #include <linux/bpf_types.h> 5515 #undef BPF_PROG_TYPE 5516 } *__t; 5517 /* 't' is written once under lock. Read many times. */ 5518 const struct btf_type *t; 5519 } bpf_ctx_convert; 5520 enum { 5521 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \ 5522 __ctx_convert##_id, 5523 #include <linux/bpf_types.h> 5524 #undef BPF_PROG_TYPE 5525 __ctx_convert_unused, /* to avoid empty enum in extreme .config */ 5526 }; 5527 static u8 bpf_ctx_convert_map[] = { 5528 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \ 5529 [_id] = __ctx_convert##_id, 5530 #include <linux/bpf_types.h> 5531 #undef BPF_PROG_TYPE 5532 0, /* avoid empty array */ 5533 }; 5534 #undef BPF_MAP_TYPE 5535 #undef BPF_LINK_TYPE 5536 5537 const struct btf_member * 5538 btf_get_prog_ctx_type(struct bpf_verifier_log *log, const struct btf *btf, 5539 const struct btf_type *t, enum bpf_prog_type prog_type, 5540 int arg) 5541 { 5542 const struct btf_type *conv_struct; 5543 const struct btf_type *ctx_struct; 5544 const struct btf_member *ctx_type; 5545 const char *tname, *ctx_tname; 5546 5547 conv_struct = bpf_ctx_convert.t; 5548 if (!conv_struct) { 5549 bpf_log(log, "btf_vmlinux is malformed\n"); 5550 return NULL; 5551 } 5552 t = btf_type_by_id(btf, t->type); 5553 while (btf_type_is_modifier(t)) 5554 t = btf_type_by_id(btf, t->type); 5555 if (!btf_type_is_struct(t)) { 5556 /* Only pointer to struct is supported for now. 5557 * That means that BPF_PROG_TYPE_TRACEPOINT with BTF 5558 * is not supported yet. 5559 * BPF_PROG_TYPE_RAW_TRACEPOINT is fine. 5560 */ 5561 return NULL; 5562 } 5563 tname = btf_name_by_offset(btf, t->name_off); 5564 if (!tname) { 5565 bpf_log(log, "arg#%d struct doesn't have a name\n", arg); 5566 return NULL; 5567 } 5568 /* prog_type is valid bpf program type. No need for bounds check. */ 5569 ctx_type = btf_type_member(conv_struct) + bpf_ctx_convert_map[prog_type] * 2; 5570 /* ctx_struct is a pointer to prog_ctx_type in vmlinux. 5571 * Like 'struct __sk_buff' 5572 */ 5573 ctx_struct = btf_type_by_id(btf_vmlinux, ctx_type->type); 5574 if (!ctx_struct) 5575 /* should not happen */ 5576 return NULL; 5577 ctx_tname = btf_name_by_offset(btf_vmlinux, ctx_struct->name_off); 5578 if (!ctx_tname) { 5579 /* should not happen */ 5580 bpf_log(log, "Please fix kernel include/linux/bpf_types.h\n"); 5581 return NULL; 5582 } 5583 /* only compare that prog's ctx type name is the same as 5584 * kernel expects. No need to compare field by field. 5585 * It's ok for bpf prog to do: 5586 * struct __sk_buff {}; 5587 * int socket_filter_bpf_prog(struct __sk_buff *skb) 5588 * { // no fields of skb are ever used } 5589 */ 5590 if (strcmp(ctx_tname, tname)) 5591 return NULL; 5592 return ctx_type; 5593 } 5594 5595 static int btf_translate_to_vmlinux(struct bpf_verifier_log *log, 5596 struct btf *btf, 5597 const struct btf_type *t, 5598 enum bpf_prog_type prog_type, 5599 int arg) 5600 { 5601 const struct btf_member *prog_ctx_type, *kern_ctx_type; 5602 5603 prog_ctx_type = btf_get_prog_ctx_type(log, btf, t, prog_type, arg); 5604 if (!prog_ctx_type) 5605 return -ENOENT; 5606 kern_ctx_type = prog_ctx_type + 1; 5607 return kern_ctx_type->type; 5608 } 5609 5610 int get_kern_ctx_btf_id(struct bpf_verifier_log *log, enum bpf_prog_type prog_type) 5611 { 5612 const struct btf_member *kctx_member; 5613 const struct btf_type *conv_struct; 5614 const struct btf_type *kctx_type; 5615 u32 kctx_type_id; 5616 5617 conv_struct = bpf_ctx_convert.t; 5618 /* get member for kernel ctx type */ 5619 kctx_member = btf_type_member(conv_struct) + bpf_ctx_convert_map[prog_type] * 2 + 1; 5620 kctx_type_id = kctx_member->type; 5621 kctx_type = btf_type_by_id(btf_vmlinux, kctx_type_id); 5622 if (!btf_type_is_struct(kctx_type)) { 5623 bpf_log(log, "kern ctx type id %u is not a struct\n", kctx_type_id); 5624 return -EINVAL; 5625 } 5626 5627 return kctx_type_id; 5628 } 5629 5630 BTF_ID_LIST(bpf_ctx_convert_btf_id) 5631 BTF_ID(struct, bpf_ctx_convert) 5632 5633 struct btf *btf_parse_vmlinux(void) 5634 { 5635 struct btf_verifier_env *env = NULL; 5636 struct bpf_verifier_log *log; 5637 struct btf *btf = NULL; 5638 int err; 5639 5640 env = kzalloc(sizeof(*env), GFP_KERNEL | __GFP_NOWARN); 5641 if (!env) 5642 return ERR_PTR(-ENOMEM); 5643 5644 log = &env->log; 5645 log->level = BPF_LOG_KERNEL; 5646 5647 btf = kzalloc(sizeof(*btf), GFP_KERNEL | __GFP_NOWARN); 5648 if (!btf) { 5649 err = -ENOMEM; 5650 goto errout; 5651 } 5652 env->btf = btf; 5653 5654 btf->data = __start_BTF; 5655 btf->data_size = __stop_BTF - __start_BTF; 5656 btf->kernel_btf = true; 5657 snprintf(btf->name, sizeof(btf->name), "vmlinux"); 5658 5659 err = btf_parse_hdr(env); 5660 if (err) 5661 goto errout; 5662 5663 btf->nohdr_data = btf->data + btf->hdr.hdr_len; 5664 5665 err = btf_parse_str_sec(env); 5666 if (err) 5667 goto errout; 5668 5669 err = btf_check_all_metas(env); 5670 if (err) 5671 goto errout; 5672 5673 err = btf_check_type_tags(env, btf, 1); 5674 if (err) 5675 goto errout; 5676 5677 /* btf_parse_vmlinux() runs under bpf_verifier_lock */ 5678 bpf_ctx_convert.t = btf_type_by_id(btf, bpf_ctx_convert_btf_id[0]); 5679 5680 bpf_struct_ops_init(btf, log); 5681 5682 refcount_set(&btf->refcnt, 1); 5683 5684 err = btf_alloc_id(btf); 5685 if (err) 5686 goto errout; 5687 5688 btf_verifier_env_free(env); 5689 return btf; 5690 5691 errout: 5692 btf_verifier_env_free(env); 5693 if (btf) { 5694 kvfree(btf->types); 5695 kfree(btf); 5696 } 5697 return ERR_PTR(err); 5698 } 5699 5700 #ifdef CONFIG_DEBUG_INFO_BTF_MODULES 5701 5702 static struct btf *btf_parse_module(const char *module_name, const void *data, unsigned int data_size) 5703 { 5704 struct btf_verifier_env *env = NULL; 5705 struct bpf_verifier_log *log; 5706 struct btf *btf = NULL, *base_btf; 5707 int err; 5708 5709 base_btf = bpf_get_btf_vmlinux(); 5710 if (IS_ERR(base_btf)) 5711 return base_btf; 5712 if (!base_btf) 5713 return ERR_PTR(-EINVAL); 5714 5715 env = kzalloc(sizeof(*env), GFP_KERNEL | __GFP_NOWARN); 5716 if (!env) 5717 return ERR_PTR(-ENOMEM); 5718 5719 log = &env->log; 5720 log->level = BPF_LOG_KERNEL; 5721 5722 btf = kzalloc(sizeof(*btf), GFP_KERNEL | __GFP_NOWARN); 5723 if (!btf) { 5724 err = -ENOMEM; 5725 goto errout; 5726 } 5727 env->btf = btf; 5728 5729 btf->base_btf = base_btf; 5730 btf->start_id = base_btf->nr_types; 5731 btf->start_str_off = base_btf->hdr.str_len; 5732 btf->kernel_btf = true; 5733 snprintf(btf->name, sizeof(btf->name), "%s", module_name); 5734 5735 btf->data = kvmalloc(data_size, GFP_KERNEL | __GFP_NOWARN); 5736 if (!btf->data) { 5737 err = -ENOMEM; 5738 goto errout; 5739 } 5740 memcpy(btf->data, data, data_size); 5741 btf->data_size = data_size; 5742 5743 err = btf_parse_hdr(env); 5744 if (err) 5745 goto errout; 5746 5747 btf->nohdr_data = btf->data + btf->hdr.hdr_len; 5748 5749 err = btf_parse_str_sec(env); 5750 if (err) 5751 goto errout; 5752 5753 err = btf_check_all_metas(env); 5754 if (err) 5755 goto errout; 5756 5757 err = btf_check_type_tags(env, btf, btf_nr_types(base_btf)); 5758 if (err) 5759 goto errout; 5760 5761 btf_verifier_env_free(env); 5762 refcount_set(&btf->refcnt, 1); 5763 return btf; 5764 5765 errout: 5766 btf_verifier_env_free(env); 5767 if (btf) { 5768 kvfree(btf->data); 5769 kvfree(btf->types); 5770 kfree(btf); 5771 } 5772 return ERR_PTR(err); 5773 } 5774 5775 #endif /* CONFIG_DEBUG_INFO_BTF_MODULES */ 5776 5777 struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog) 5778 { 5779 struct bpf_prog *tgt_prog = prog->aux->dst_prog; 5780 5781 if (tgt_prog) 5782 return tgt_prog->aux->btf; 5783 else 5784 return prog->aux->attach_btf; 5785 } 5786 5787 static bool is_int_ptr(struct btf *btf, const struct btf_type *t) 5788 { 5789 /* t comes in already as a pointer */ 5790 t = btf_type_by_id(btf, t->type); 5791 5792 /* allow const */ 5793 if (BTF_INFO_KIND(t->info) == BTF_KIND_CONST) 5794 t = btf_type_by_id(btf, t->type); 5795 5796 return btf_type_is_int(t); 5797 } 5798 5799 static u32 get_ctx_arg_idx(struct btf *btf, const struct btf_type *func_proto, 5800 int off) 5801 { 5802 const struct btf_param *args; 5803 const struct btf_type *t; 5804 u32 offset = 0, nr_args; 5805 int i; 5806 5807 if (!func_proto) 5808 return off / 8; 5809 5810 nr_args = btf_type_vlen(func_proto); 5811 args = (const struct btf_param *)(func_proto + 1); 5812 for (i = 0; i < nr_args; i++) { 5813 t = btf_type_skip_modifiers(btf, args[i].type, NULL); 5814 offset += btf_type_is_ptr(t) ? 8 : roundup(t->size, 8); 5815 if (off < offset) 5816 return i; 5817 } 5818 5819 t = btf_type_skip_modifiers(btf, func_proto->type, NULL); 5820 offset += btf_type_is_ptr(t) ? 8 : roundup(t->size, 8); 5821 if (off < offset) 5822 return nr_args; 5823 5824 return nr_args + 1; 5825 } 5826 5827 static bool prog_args_trusted(const struct bpf_prog *prog) 5828 { 5829 enum bpf_attach_type atype = prog->expected_attach_type; 5830 5831 switch (prog->type) { 5832 case BPF_PROG_TYPE_TRACING: 5833 return atype == BPF_TRACE_RAW_TP || atype == BPF_TRACE_ITER; 5834 case BPF_PROG_TYPE_LSM: 5835 return bpf_lsm_is_trusted(prog); 5836 case BPF_PROG_TYPE_STRUCT_OPS: 5837 return true; 5838 default: 5839 return false; 5840 } 5841 } 5842 5843 bool btf_ctx_access(int off, int size, enum bpf_access_type type, 5844 const struct bpf_prog *prog, 5845 struct bpf_insn_access_aux *info) 5846 { 5847 const struct btf_type *t = prog->aux->attach_func_proto; 5848 struct bpf_prog *tgt_prog = prog->aux->dst_prog; 5849 struct btf *btf = bpf_prog_get_target_btf(prog); 5850 const char *tname = prog->aux->attach_func_name; 5851 struct bpf_verifier_log *log = info->log; 5852 const struct btf_param *args; 5853 const char *tag_value; 5854 u32 nr_args, arg; 5855 int i, ret; 5856 5857 if (off % 8) { 5858 bpf_log(log, "func '%s' offset %d is not multiple of 8\n", 5859 tname, off); 5860 return false; 5861 } 5862 arg = get_ctx_arg_idx(btf, t, off); 5863 args = (const struct btf_param *)(t + 1); 5864 /* if (t == NULL) Fall back to default BPF prog with 5865 * MAX_BPF_FUNC_REG_ARGS u64 arguments. 5866 */ 5867 nr_args = t ? btf_type_vlen(t) : MAX_BPF_FUNC_REG_ARGS; 5868 if (prog->aux->attach_btf_trace) { 5869 /* skip first 'void *__data' argument in btf_trace_##name typedef */ 5870 args++; 5871 nr_args--; 5872 } 5873 5874 if (arg > nr_args) { 5875 bpf_log(log, "func '%s' doesn't have %d-th argument\n", 5876 tname, arg + 1); 5877 return false; 5878 } 5879 5880 if (arg == nr_args) { 5881 switch (prog->expected_attach_type) { 5882 case BPF_LSM_CGROUP: 5883 case BPF_LSM_MAC: 5884 case BPF_TRACE_FEXIT: 5885 /* When LSM programs are attached to void LSM hooks 5886 * they use FEXIT trampolines and when attached to 5887 * int LSM hooks, they use MODIFY_RETURN trampolines. 5888 * 5889 * While the LSM programs are BPF_MODIFY_RETURN-like 5890 * the check: 5891 * 5892 * if (ret_type != 'int') 5893 * return -EINVAL; 5894 * 5895 * is _not_ done here. This is still safe as LSM hooks 5896 * have only void and int return types. 5897 */ 5898 if (!t) 5899 return true; 5900 t = btf_type_by_id(btf, t->type); 5901 break; 5902 case BPF_MODIFY_RETURN: 5903 /* For now the BPF_MODIFY_RETURN can only be attached to 5904 * functions that return an int. 5905 */ 5906 if (!t) 5907 return false; 5908 5909 t = btf_type_skip_modifiers(btf, t->type, NULL); 5910 if (!btf_type_is_small_int(t)) { 5911 bpf_log(log, 5912 "ret type %s not allowed for fmod_ret\n", 5913 btf_type_str(t)); 5914 return false; 5915 } 5916 break; 5917 default: 5918 bpf_log(log, "func '%s' doesn't have %d-th argument\n", 5919 tname, arg + 1); 5920 return false; 5921 } 5922 } else { 5923 if (!t) 5924 /* Default prog with MAX_BPF_FUNC_REG_ARGS args */ 5925 return true; 5926 t = btf_type_by_id(btf, args[arg].type); 5927 } 5928 5929 /* skip modifiers */ 5930 while (btf_type_is_modifier(t)) 5931 t = btf_type_by_id(btf, t->type); 5932 if (btf_type_is_small_int(t) || btf_is_any_enum(t) || __btf_type_is_struct(t)) 5933 /* accessing a scalar */ 5934 return true; 5935 if (!btf_type_is_ptr(t)) { 5936 bpf_log(log, 5937 "func '%s' arg%d '%s' has type %s. Only pointer access is allowed\n", 5938 tname, arg, 5939 __btf_name_by_offset(btf, t->name_off), 5940 btf_type_str(t)); 5941 return false; 5942 } 5943 5944 /* check for PTR_TO_RDONLY_BUF_OR_NULL or PTR_TO_RDWR_BUF_OR_NULL */ 5945 for (i = 0; i < prog->aux->ctx_arg_info_size; i++) { 5946 const struct bpf_ctx_arg_aux *ctx_arg_info = &prog->aux->ctx_arg_info[i]; 5947 u32 type, flag; 5948 5949 type = base_type(ctx_arg_info->reg_type); 5950 flag = type_flag(ctx_arg_info->reg_type); 5951 if (ctx_arg_info->offset == off && type == PTR_TO_BUF && 5952 (flag & PTR_MAYBE_NULL)) { 5953 info->reg_type = ctx_arg_info->reg_type; 5954 return true; 5955 } 5956 } 5957 5958 if (t->type == 0) 5959 /* This is a pointer to void. 5960 * It is the same as scalar from the verifier safety pov. 5961 * No further pointer walking is allowed. 5962 */ 5963 return true; 5964 5965 if (is_int_ptr(btf, t)) 5966 return true; 5967 5968 /* this is a pointer to another type */ 5969 for (i = 0; i < prog->aux->ctx_arg_info_size; i++) { 5970 const struct bpf_ctx_arg_aux *ctx_arg_info = &prog->aux->ctx_arg_info[i]; 5971 5972 if (ctx_arg_info->offset == off) { 5973 if (!ctx_arg_info->btf_id) { 5974 bpf_log(log,"invalid btf_id for context argument offset %u\n", off); 5975 return false; 5976 } 5977 5978 info->reg_type = ctx_arg_info->reg_type; 5979 info->btf = btf_vmlinux; 5980 info->btf_id = ctx_arg_info->btf_id; 5981 return true; 5982 } 5983 } 5984 5985 info->reg_type = PTR_TO_BTF_ID; 5986 if (prog_args_trusted(prog)) 5987 info->reg_type |= PTR_TRUSTED; 5988 5989 if (tgt_prog) { 5990 enum bpf_prog_type tgt_type; 5991 5992 if (tgt_prog->type == BPF_PROG_TYPE_EXT) 5993 tgt_type = tgt_prog->aux->saved_dst_prog_type; 5994 else 5995 tgt_type = tgt_prog->type; 5996 5997 ret = btf_translate_to_vmlinux(log, btf, t, tgt_type, arg); 5998 if (ret > 0) { 5999 info->btf = btf_vmlinux; 6000 info->btf_id = ret; 6001 return true; 6002 } else { 6003 return false; 6004 } 6005 } 6006 6007 info->btf = btf; 6008 info->btf_id = t->type; 6009 t = btf_type_by_id(btf, t->type); 6010 6011 if (btf_type_is_type_tag(t)) { 6012 tag_value = __btf_name_by_offset(btf, t->name_off); 6013 if (strcmp(tag_value, "user") == 0) 6014 info->reg_type |= MEM_USER; 6015 if (strcmp(tag_value, "percpu") == 0) 6016 info->reg_type |= MEM_PERCPU; 6017 } 6018 6019 /* skip modifiers */ 6020 while (btf_type_is_modifier(t)) { 6021 info->btf_id = t->type; 6022 t = btf_type_by_id(btf, t->type); 6023 } 6024 if (!btf_type_is_struct(t)) { 6025 bpf_log(log, 6026 "func '%s' arg%d type %s is not a struct\n", 6027 tname, arg, btf_type_str(t)); 6028 return false; 6029 } 6030 bpf_log(log, "func '%s' arg%d has btf_id %d type %s '%s'\n", 6031 tname, arg, info->btf_id, btf_type_str(t), 6032 __btf_name_by_offset(btf, t->name_off)); 6033 return true; 6034 } 6035 6036 enum bpf_struct_walk_result { 6037 /* < 0 error */ 6038 WALK_SCALAR = 0, 6039 WALK_PTR, 6040 WALK_STRUCT, 6041 }; 6042 6043 static int btf_struct_walk(struct bpf_verifier_log *log, const struct btf *btf, 6044 const struct btf_type *t, int off, int size, 6045 u32 *next_btf_id, enum bpf_type_flag *flag) 6046 { 6047 u32 i, moff, mtrue_end, msize = 0, total_nelems = 0; 6048 const struct btf_type *mtype, *elem_type = NULL; 6049 const struct btf_member *member; 6050 const char *tname, *mname, *tag_value; 6051 u32 vlen, elem_id, mid; 6052 6053 again: 6054 tname = __btf_name_by_offset(btf, t->name_off); 6055 if (!btf_type_is_struct(t)) { 6056 bpf_log(log, "Type '%s' is not a struct\n", tname); 6057 return -EINVAL; 6058 } 6059 6060 vlen = btf_type_vlen(t); 6061 if (off + size > t->size) { 6062 /* If the last element is a variable size array, we may 6063 * need to relax the rule. 6064 */ 6065 struct btf_array *array_elem; 6066 6067 if (vlen == 0) 6068 goto error; 6069 6070 member = btf_type_member(t) + vlen - 1; 6071 mtype = btf_type_skip_modifiers(btf, member->type, 6072 NULL); 6073 if (!btf_type_is_array(mtype)) 6074 goto error; 6075 6076 array_elem = (struct btf_array *)(mtype + 1); 6077 if (array_elem->nelems != 0) 6078 goto error; 6079 6080 moff = __btf_member_bit_offset(t, member) / 8; 6081 if (off < moff) 6082 goto error; 6083 6084 /* Only allow structure for now, can be relaxed for 6085 * other types later. 6086 */ 6087 t = btf_type_skip_modifiers(btf, array_elem->type, 6088 NULL); 6089 if (!btf_type_is_struct(t)) 6090 goto error; 6091 6092 off = (off - moff) % t->size; 6093 goto again; 6094 6095 error: 6096 bpf_log(log, "access beyond struct %s at off %u size %u\n", 6097 tname, off, size); 6098 return -EACCES; 6099 } 6100 6101 for_each_member(i, t, member) { 6102 /* offset of the field in bytes */ 6103 moff = __btf_member_bit_offset(t, member) / 8; 6104 if (off + size <= moff) 6105 /* won't find anything, field is already too far */ 6106 break; 6107 6108 if (__btf_member_bitfield_size(t, member)) { 6109 u32 end_bit = __btf_member_bit_offset(t, member) + 6110 __btf_member_bitfield_size(t, member); 6111 6112 /* off <= moff instead of off == moff because clang 6113 * does not generate a BTF member for anonymous 6114 * bitfield like the ":16" here: 6115 * struct { 6116 * int :16; 6117 * int x:8; 6118 * }; 6119 */ 6120 if (off <= moff && 6121 BITS_ROUNDUP_BYTES(end_bit) <= off + size) 6122 return WALK_SCALAR; 6123 6124 /* off may be accessing a following member 6125 * 6126 * or 6127 * 6128 * Doing partial access at either end of this 6129 * bitfield. Continue on this case also to 6130 * treat it as not accessing this bitfield 6131 * and eventually error out as field not 6132 * found to keep it simple. 6133 * It could be relaxed if there was a legit 6134 * partial access case later. 6135 */ 6136 continue; 6137 } 6138 6139 /* In case of "off" is pointing to holes of a struct */ 6140 if (off < moff) 6141 break; 6142 6143 /* type of the field */ 6144 mid = member->type; 6145 mtype = btf_type_by_id(btf, member->type); 6146 mname = __btf_name_by_offset(btf, member->name_off); 6147 6148 mtype = __btf_resolve_size(btf, mtype, &msize, 6149 &elem_type, &elem_id, &total_nelems, 6150 &mid); 6151 if (IS_ERR(mtype)) { 6152 bpf_log(log, "field %s doesn't have size\n", mname); 6153 return -EFAULT; 6154 } 6155 6156 mtrue_end = moff + msize; 6157 if (off >= mtrue_end) 6158 /* no overlap with member, keep iterating */ 6159 continue; 6160 6161 if (btf_type_is_array(mtype)) { 6162 u32 elem_idx; 6163 6164 /* __btf_resolve_size() above helps to 6165 * linearize a multi-dimensional array. 6166 * 6167 * The logic here is treating an array 6168 * in a struct as the following way: 6169 * 6170 * struct outer { 6171 * struct inner array[2][2]; 6172 * }; 6173 * 6174 * looks like: 6175 * 6176 * struct outer { 6177 * struct inner array_elem0; 6178 * struct inner array_elem1; 6179 * struct inner array_elem2; 6180 * struct inner array_elem3; 6181 * }; 6182 * 6183 * When accessing outer->array[1][0], it moves 6184 * moff to "array_elem2", set mtype to 6185 * "struct inner", and msize also becomes 6186 * sizeof(struct inner). Then most of the 6187 * remaining logic will fall through without 6188 * caring the current member is an array or 6189 * not. 6190 * 6191 * Unlike mtype/msize/moff, mtrue_end does not 6192 * change. The naming difference ("_true") tells 6193 * that it is not always corresponding to 6194 * the current mtype/msize/moff. 6195 * It is the true end of the current 6196 * member (i.e. array in this case). That 6197 * will allow an int array to be accessed like 6198 * a scratch space, 6199 * i.e. allow access beyond the size of 6200 * the array's element as long as it is 6201 * within the mtrue_end boundary. 6202 */ 6203 6204 /* skip empty array */ 6205 if (moff == mtrue_end) 6206 continue; 6207 6208 msize /= total_nelems; 6209 elem_idx = (off - moff) / msize; 6210 moff += elem_idx * msize; 6211 mtype = elem_type; 6212 mid = elem_id; 6213 } 6214 6215 /* the 'off' we're looking for is either equal to start 6216 * of this field or inside of this struct 6217 */ 6218 if (btf_type_is_struct(mtype)) { 6219 /* our field must be inside that union or struct */ 6220 t = mtype; 6221 6222 /* return if the offset matches the member offset */ 6223 if (off == moff) { 6224 *next_btf_id = mid; 6225 return WALK_STRUCT; 6226 } 6227 6228 /* adjust offset we're looking for */ 6229 off -= moff; 6230 goto again; 6231 } 6232 6233 if (btf_type_is_ptr(mtype)) { 6234 const struct btf_type *stype, *t; 6235 enum bpf_type_flag tmp_flag = 0; 6236 u32 id; 6237 6238 if (msize != size || off != moff) { 6239 bpf_log(log, 6240 "cannot access ptr member %s with moff %u in struct %s with off %u size %u\n", 6241 mname, moff, tname, off, size); 6242 return -EACCES; 6243 } 6244 6245 /* check type tag */ 6246 t = btf_type_by_id(btf, mtype->type); 6247 if (btf_type_is_type_tag(t)) { 6248 tag_value = __btf_name_by_offset(btf, t->name_off); 6249 /* check __user tag */ 6250 if (strcmp(tag_value, "user") == 0) 6251 tmp_flag = MEM_USER; 6252 /* check __percpu tag */ 6253 if (strcmp(tag_value, "percpu") == 0) 6254 tmp_flag = MEM_PERCPU; 6255 /* check __rcu tag */ 6256 if (strcmp(tag_value, "rcu") == 0) 6257 tmp_flag = MEM_RCU; 6258 } 6259 6260 stype = btf_type_skip_modifiers(btf, mtype->type, &id); 6261 if (btf_type_is_struct(stype)) { 6262 *next_btf_id = id; 6263 *flag = tmp_flag; 6264 return WALK_PTR; 6265 } 6266 } 6267 6268 /* Allow more flexible access within an int as long as 6269 * it is within mtrue_end. 6270 * Since mtrue_end could be the end of an array, 6271 * that also allows using an array of int as a scratch 6272 * space. e.g. skb->cb[]. 6273 */ 6274 if (off + size > mtrue_end) { 6275 bpf_log(log, 6276 "access beyond the end of member %s (mend:%u) in struct %s with off %u size %u\n", 6277 mname, mtrue_end, tname, off, size); 6278 return -EACCES; 6279 } 6280 6281 return WALK_SCALAR; 6282 } 6283 bpf_log(log, "struct %s doesn't have field at offset %d\n", tname, off); 6284 return -EINVAL; 6285 } 6286 6287 int btf_struct_access(struct bpf_verifier_log *log, 6288 const struct bpf_reg_state *reg, 6289 int off, int size, enum bpf_access_type atype __maybe_unused, 6290 u32 *next_btf_id, enum bpf_type_flag *flag) 6291 { 6292 const struct btf *btf = reg->btf; 6293 enum bpf_type_flag tmp_flag = 0; 6294 const struct btf_type *t; 6295 u32 id = reg->btf_id; 6296 int err; 6297 6298 while (type_is_alloc(reg->type)) { 6299 struct btf_struct_meta *meta; 6300 struct btf_record *rec; 6301 int i; 6302 6303 meta = btf_find_struct_meta(btf, id); 6304 if (!meta) 6305 break; 6306 rec = meta->record; 6307 for (i = 0; i < rec->cnt; i++) { 6308 struct btf_field *field = &rec->fields[i]; 6309 u32 offset = field->offset; 6310 if (off < offset + btf_field_type_size(field->type) && offset < off + size) { 6311 bpf_log(log, 6312 "direct access to %s is disallowed\n", 6313 btf_field_type_name(field->type)); 6314 return -EACCES; 6315 } 6316 } 6317 break; 6318 } 6319 6320 t = btf_type_by_id(btf, id); 6321 do { 6322 err = btf_struct_walk(log, btf, t, off, size, &id, &tmp_flag); 6323 6324 switch (err) { 6325 case WALK_PTR: 6326 /* For local types, the destination register cannot 6327 * become a pointer again. 6328 */ 6329 if (type_is_alloc(reg->type)) 6330 return SCALAR_VALUE; 6331 /* If we found the pointer or scalar on t+off, 6332 * we're done. 6333 */ 6334 *next_btf_id = id; 6335 *flag = tmp_flag; 6336 return PTR_TO_BTF_ID; 6337 case WALK_SCALAR: 6338 return SCALAR_VALUE; 6339 case WALK_STRUCT: 6340 /* We found nested struct, so continue the search 6341 * by diving in it. At this point the offset is 6342 * aligned with the new type, so set it to 0. 6343 */ 6344 t = btf_type_by_id(btf, id); 6345 off = 0; 6346 break; 6347 default: 6348 /* It's either error or unknown return value.. 6349 * scream and leave. 6350 */ 6351 if (WARN_ONCE(err > 0, "unknown btf_struct_walk return value")) 6352 return -EINVAL; 6353 return err; 6354 } 6355 } while (t); 6356 6357 return -EINVAL; 6358 } 6359 6360 /* Check that two BTF types, each specified as an BTF object + id, are exactly 6361 * the same. Trivial ID check is not enough due to module BTFs, because we can 6362 * end up with two different module BTFs, but IDs point to the common type in 6363 * vmlinux BTF. 6364 */ 6365 bool btf_types_are_same(const struct btf *btf1, u32 id1, 6366 const struct btf *btf2, u32 id2) 6367 { 6368 if (id1 != id2) 6369 return false; 6370 if (btf1 == btf2) 6371 return true; 6372 return btf_type_by_id(btf1, id1) == btf_type_by_id(btf2, id2); 6373 } 6374 6375 bool btf_struct_ids_match(struct bpf_verifier_log *log, 6376 const struct btf *btf, u32 id, int off, 6377 const struct btf *need_btf, u32 need_type_id, 6378 bool strict) 6379 { 6380 const struct btf_type *type; 6381 enum bpf_type_flag flag; 6382 int err; 6383 6384 /* Are we already done? */ 6385 if (off == 0 && btf_types_are_same(btf, id, need_btf, need_type_id)) 6386 return true; 6387 /* In case of strict type match, we do not walk struct, the top level 6388 * type match must succeed. When strict is true, off should have already 6389 * been 0. 6390 */ 6391 if (strict) 6392 return false; 6393 again: 6394 type = btf_type_by_id(btf, id); 6395 if (!type) 6396 return false; 6397 err = btf_struct_walk(log, btf, type, off, 1, &id, &flag); 6398 if (err != WALK_STRUCT) 6399 return false; 6400 6401 /* We found nested struct object. If it matches 6402 * the requested ID, we're done. Otherwise let's 6403 * continue the search with offset 0 in the new 6404 * type. 6405 */ 6406 if (!btf_types_are_same(btf, id, need_btf, need_type_id)) { 6407 off = 0; 6408 goto again; 6409 } 6410 6411 return true; 6412 } 6413 6414 static int __get_type_size(struct btf *btf, u32 btf_id, 6415 const struct btf_type **ret_type) 6416 { 6417 const struct btf_type *t; 6418 6419 *ret_type = btf_type_by_id(btf, 0); 6420 if (!btf_id) 6421 /* void */ 6422 return 0; 6423 t = btf_type_by_id(btf, btf_id); 6424 while (t && btf_type_is_modifier(t)) 6425 t = btf_type_by_id(btf, t->type); 6426 if (!t) 6427 return -EINVAL; 6428 *ret_type = t; 6429 if (btf_type_is_ptr(t)) 6430 /* kernel size of pointer. Not BPF's size of pointer*/ 6431 return sizeof(void *); 6432 if (btf_type_is_int(t) || btf_is_any_enum(t) || __btf_type_is_struct(t)) 6433 return t->size; 6434 return -EINVAL; 6435 } 6436 6437 int btf_distill_func_proto(struct bpf_verifier_log *log, 6438 struct btf *btf, 6439 const struct btf_type *func, 6440 const char *tname, 6441 struct btf_func_model *m) 6442 { 6443 const struct btf_param *args; 6444 const struct btf_type *t; 6445 u32 i, nargs; 6446 int ret; 6447 6448 if (!func) { 6449 /* BTF function prototype doesn't match the verifier types. 6450 * Fall back to MAX_BPF_FUNC_REG_ARGS u64 args. 6451 */ 6452 for (i = 0; i < MAX_BPF_FUNC_REG_ARGS; i++) { 6453 m->arg_size[i] = 8; 6454 m->arg_flags[i] = 0; 6455 } 6456 m->ret_size = 8; 6457 m->nr_args = MAX_BPF_FUNC_REG_ARGS; 6458 return 0; 6459 } 6460 args = (const struct btf_param *)(func + 1); 6461 nargs = btf_type_vlen(func); 6462 if (nargs > MAX_BPF_FUNC_ARGS) { 6463 bpf_log(log, 6464 "The function %s has %d arguments. Too many.\n", 6465 tname, nargs); 6466 return -EINVAL; 6467 } 6468 ret = __get_type_size(btf, func->type, &t); 6469 if (ret < 0 || __btf_type_is_struct(t)) { 6470 bpf_log(log, 6471 "The function %s return type %s is unsupported.\n", 6472 tname, btf_type_str(t)); 6473 return -EINVAL; 6474 } 6475 m->ret_size = ret; 6476 6477 for (i = 0; i < nargs; i++) { 6478 if (i == nargs - 1 && args[i].type == 0) { 6479 bpf_log(log, 6480 "The function %s with variable args is unsupported.\n", 6481 tname); 6482 return -EINVAL; 6483 } 6484 ret = __get_type_size(btf, args[i].type, &t); 6485 6486 /* No support of struct argument size greater than 16 bytes */ 6487 if (ret < 0 || ret > 16) { 6488 bpf_log(log, 6489 "The function %s arg%d type %s is unsupported.\n", 6490 tname, i, btf_type_str(t)); 6491 return -EINVAL; 6492 } 6493 if (ret == 0) { 6494 bpf_log(log, 6495 "The function %s has malformed void argument.\n", 6496 tname); 6497 return -EINVAL; 6498 } 6499 m->arg_size[i] = ret; 6500 m->arg_flags[i] = __btf_type_is_struct(t) ? BTF_FMODEL_STRUCT_ARG : 0; 6501 } 6502 m->nr_args = nargs; 6503 return 0; 6504 } 6505 6506 /* Compare BTFs of two functions assuming only scalars and pointers to context. 6507 * t1 points to BTF_KIND_FUNC in btf1 6508 * t2 points to BTF_KIND_FUNC in btf2 6509 * Returns: 6510 * EINVAL - function prototype mismatch 6511 * EFAULT - verifier bug 6512 * 0 - 99% match. The last 1% is validated by the verifier. 6513 */ 6514 static int btf_check_func_type_match(struct bpf_verifier_log *log, 6515 struct btf *btf1, const struct btf_type *t1, 6516 struct btf *btf2, const struct btf_type *t2) 6517 { 6518 const struct btf_param *args1, *args2; 6519 const char *fn1, *fn2, *s1, *s2; 6520 u32 nargs1, nargs2, i; 6521 6522 fn1 = btf_name_by_offset(btf1, t1->name_off); 6523 fn2 = btf_name_by_offset(btf2, t2->name_off); 6524 6525 if (btf_func_linkage(t1) != BTF_FUNC_GLOBAL) { 6526 bpf_log(log, "%s() is not a global function\n", fn1); 6527 return -EINVAL; 6528 } 6529 if (btf_func_linkage(t2) != BTF_FUNC_GLOBAL) { 6530 bpf_log(log, "%s() is not a global function\n", fn2); 6531 return -EINVAL; 6532 } 6533 6534 t1 = btf_type_by_id(btf1, t1->type); 6535 if (!t1 || !btf_type_is_func_proto(t1)) 6536 return -EFAULT; 6537 t2 = btf_type_by_id(btf2, t2->type); 6538 if (!t2 || !btf_type_is_func_proto(t2)) 6539 return -EFAULT; 6540 6541 args1 = (const struct btf_param *)(t1 + 1); 6542 nargs1 = btf_type_vlen(t1); 6543 args2 = (const struct btf_param *)(t2 + 1); 6544 nargs2 = btf_type_vlen(t2); 6545 6546 if (nargs1 != nargs2) { 6547 bpf_log(log, "%s() has %d args while %s() has %d args\n", 6548 fn1, nargs1, fn2, nargs2); 6549 return -EINVAL; 6550 } 6551 6552 t1 = btf_type_skip_modifiers(btf1, t1->type, NULL); 6553 t2 = btf_type_skip_modifiers(btf2, t2->type, NULL); 6554 if (t1->info != t2->info) { 6555 bpf_log(log, 6556 "Return type %s of %s() doesn't match type %s of %s()\n", 6557 btf_type_str(t1), fn1, 6558 btf_type_str(t2), fn2); 6559 return -EINVAL; 6560 } 6561 6562 for (i = 0; i < nargs1; i++) { 6563 t1 = btf_type_skip_modifiers(btf1, args1[i].type, NULL); 6564 t2 = btf_type_skip_modifiers(btf2, args2[i].type, NULL); 6565 6566 if (t1->info != t2->info) { 6567 bpf_log(log, "arg%d in %s() is %s while %s() has %s\n", 6568 i, fn1, btf_type_str(t1), 6569 fn2, btf_type_str(t2)); 6570 return -EINVAL; 6571 } 6572 if (btf_type_has_size(t1) && t1->size != t2->size) { 6573 bpf_log(log, 6574 "arg%d in %s() has size %d while %s() has %d\n", 6575 i, fn1, t1->size, 6576 fn2, t2->size); 6577 return -EINVAL; 6578 } 6579 6580 /* global functions are validated with scalars and pointers 6581 * to context only. And only global functions can be replaced. 6582 * Hence type check only those types. 6583 */ 6584 if (btf_type_is_int(t1) || btf_is_any_enum(t1)) 6585 continue; 6586 if (!btf_type_is_ptr(t1)) { 6587 bpf_log(log, 6588 "arg%d in %s() has unrecognized type\n", 6589 i, fn1); 6590 return -EINVAL; 6591 } 6592 t1 = btf_type_skip_modifiers(btf1, t1->type, NULL); 6593 t2 = btf_type_skip_modifiers(btf2, t2->type, NULL); 6594 if (!btf_type_is_struct(t1)) { 6595 bpf_log(log, 6596 "arg%d in %s() is not a pointer to context\n", 6597 i, fn1); 6598 return -EINVAL; 6599 } 6600 if (!btf_type_is_struct(t2)) { 6601 bpf_log(log, 6602 "arg%d in %s() is not a pointer to context\n", 6603 i, fn2); 6604 return -EINVAL; 6605 } 6606 /* This is an optional check to make program writing easier. 6607 * Compare names of structs and report an error to the user. 6608 * btf_prepare_func_args() already checked that t2 struct 6609 * is a context type. btf_prepare_func_args() will check 6610 * later that t1 struct is a context type as well. 6611 */ 6612 s1 = btf_name_by_offset(btf1, t1->name_off); 6613 s2 = btf_name_by_offset(btf2, t2->name_off); 6614 if (strcmp(s1, s2)) { 6615 bpf_log(log, 6616 "arg%d %s(struct %s *) doesn't match %s(struct %s *)\n", 6617 i, fn1, s1, fn2, s2); 6618 return -EINVAL; 6619 } 6620 } 6621 return 0; 6622 } 6623 6624 /* Compare BTFs of given program with BTF of target program */ 6625 int btf_check_type_match(struct bpf_verifier_log *log, const struct bpf_prog *prog, 6626 struct btf *btf2, const struct btf_type *t2) 6627 { 6628 struct btf *btf1 = prog->aux->btf; 6629 const struct btf_type *t1; 6630 u32 btf_id = 0; 6631 6632 if (!prog->aux->func_info) { 6633 bpf_log(log, "Program extension requires BTF\n"); 6634 return -EINVAL; 6635 } 6636 6637 btf_id = prog->aux->func_info[0].type_id; 6638 if (!btf_id) 6639 return -EFAULT; 6640 6641 t1 = btf_type_by_id(btf1, btf_id); 6642 if (!t1 || !btf_type_is_func(t1)) 6643 return -EFAULT; 6644 6645 return btf_check_func_type_match(log, btf1, t1, btf2, t2); 6646 } 6647 6648 static int btf_check_func_arg_match(struct bpf_verifier_env *env, 6649 const struct btf *btf, u32 func_id, 6650 struct bpf_reg_state *regs, 6651 bool ptr_to_mem_ok, 6652 bool processing_call) 6653 { 6654 enum bpf_prog_type prog_type = resolve_prog_type(env->prog); 6655 struct bpf_verifier_log *log = &env->log; 6656 const char *func_name, *ref_tname; 6657 const struct btf_type *t, *ref_t; 6658 const struct btf_param *args; 6659 u32 i, nargs, ref_id; 6660 int ret; 6661 6662 t = btf_type_by_id(btf, func_id); 6663 if (!t || !btf_type_is_func(t)) { 6664 /* These checks were already done by the verifier while loading 6665 * struct bpf_func_info or in add_kfunc_call(). 6666 */ 6667 bpf_log(log, "BTF of func_id %u doesn't point to KIND_FUNC\n", 6668 func_id); 6669 return -EFAULT; 6670 } 6671 func_name = btf_name_by_offset(btf, t->name_off); 6672 6673 t = btf_type_by_id(btf, t->type); 6674 if (!t || !btf_type_is_func_proto(t)) { 6675 bpf_log(log, "Invalid BTF of func %s\n", func_name); 6676 return -EFAULT; 6677 } 6678 args = (const struct btf_param *)(t + 1); 6679 nargs = btf_type_vlen(t); 6680 if (nargs > MAX_BPF_FUNC_REG_ARGS) { 6681 bpf_log(log, "Function %s has %d > %d args\n", func_name, nargs, 6682 MAX_BPF_FUNC_REG_ARGS); 6683 return -EINVAL; 6684 } 6685 6686 /* check that BTF function arguments match actual types that the 6687 * verifier sees. 6688 */ 6689 for (i = 0; i < nargs; i++) { 6690 enum bpf_arg_type arg_type = ARG_DONTCARE; 6691 u32 regno = i + 1; 6692 struct bpf_reg_state *reg = ®s[regno]; 6693 6694 t = btf_type_skip_modifiers(btf, args[i].type, NULL); 6695 if (btf_type_is_scalar(t)) { 6696 if (reg->type == SCALAR_VALUE) 6697 continue; 6698 bpf_log(log, "R%d is not a scalar\n", regno); 6699 return -EINVAL; 6700 } 6701 6702 if (!btf_type_is_ptr(t)) { 6703 bpf_log(log, "Unrecognized arg#%d type %s\n", 6704 i, btf_type_str(t)); 6705 return -EINVAL; 6706 } 6707 6708 ref_t = btf_type_skip_modifiers(btf, t->type, &ref_id); 6709 ref_tname = btf_name_by_offset(btf, ref_t->name_off); 6710 6711 ret = check_func_arg_reg_off(env, reg, regno, arg_type); 6712 if (ret < 0) 6713 return ret; 6714 6715 if (btf_get_prog_ctx_type(log, btf, t, prog_type, i)) { 6716 /* If function expects ctx type in BTF check that caller 6717 * is passing PTR_TO_CTX. 6718 */ 6719 if (reg->type != PTR_TO_CTX) { 6720 bpf_log(log, 6721 "arg#%d expected pointer to ctx, but got %s\n", 6722 i, btf_type_str(t)); 6723 return -EINVAL; 6724 } 6725 } else if (ptr_to_mem_ok && processing_call) { 6726 const struct btf_type *resolve_ret; 6727 u32 type_size; 6728 6729 resolve_ret = btf_resolve_size(btf, ref_t, &type_size); 6730 if (IS_ERR(resolve_ret)) { 6731 bpf_log(log, 6732 "arg#%d reference type('%s %s') size cannot be determined: %ld\n", 6733 i, btf_type_str(ref_t), ref_tname, 6734 PTR_ERR(resolve_ret)); 6735 return -EINVAL; 6736 } 6737 6738 if (check_mem_reg(env, reg, regno, type_size)) 6739 return -EINVAL; 6740 } else { 6741 bpf_log(log, "reg type unsupported for arg#%d function %s#%d\n", i, 6742 func_name, func_id); 6743 return -EINVAL; 6744 } 6745 } 6746 6747 return 0; 6748 } 6749 6750 /* Compare BTF of a function declaration with given bpf_reg_state. 6751 * Returns: 6752 * EFAULT - there is a verifier bug. Abort verification. 6753 * EINVAL - there is a type mismatch or BTF is not available. 6754 * 0 - BTF matches with what bpf_reg_state expects. 6755 * Only PTR_TO_CTX and SCALAR_VALUE states are recognized. 6756 */ 6757 int btf_check_subprog_arg_match(struct bpf_verifier_env *env, int subprog, 6758 struct bpf_reg_state *regs) 6759 { 6760 struct bpf_prog *prog = env->prog; 6761 struct btf *btf = prog->aux->btf; 6762 bool is_global; 6763 u32 btf_id; 6764 int err; 6765 6766 if (!prog->aux->func_info) 6767 return -EINVAL; 6768 6769 btf_id = prog->aux->func_info[subprog].type_id; 6770 if (!btf_id) 6771 return -EFAULT; 6772 6773 if (prog->aux->func_info_aux[subprog].unreliable) 6774 return -EINVAL; 6775 6776 is_global = prog->aux->func_info_aux[subprog].linkage == BTF_FUNC_GLOBAL; 6777 err = btf_check_func_arg_match(env, btf, btf_id, regs, is_global, false); 6778 6779 /* Compiler optimizations can remove arguments from static functions 6780 * or mismatched type can be passed into a global function. 6781 * In such cases mark the function as unreliable from BTF point of view. 6782 */ 6783 if (err) 6784 prog->aux->func_info_aux[subprog].unreliable = true; 6785 return err; 6786 } 6787 6788 /* Compare BTF of a function call with given bpf_reg_state. 6789 * Returns: 6790 * EFAULT - there is a verifier bug. Abort verification. 6791 * EINVAL - there is a type mismatch or BTF is not available. 6792 * 0 - BTF matches with what bpf_reg_state expects. 6793 * Only PTR_TO_CTX and SCALAR_VALUE states are recognized. 6794 * 6795 * NOTE: the code is duplicated from btf_check_subprog_arg_match() 6796 * because btf_check_func_arg_match() is still doing both. Once that 6797 * function is split in 2, we can call from here btf_check_subprog_arg_match() 6798 * first, and then treat the calling part in a new code path. 6799 */ 6800 int btf_check_subprog_call(struct bpf_verifier_env *env, int subprog, 6801 struct bpf_reg_state *regs) 6802 { 6803 struct bpf_prog *prog = env->prog; 6804 struct btf *btf = prog->aux->btf; 6805 bool is_global; 6806 u32 btf_id; 6807 int err; 6808 6809 if (!prog->aux->func_info) 6810 return -EINVAL; 6811 6812 btf_id = prog->aux->func_info[subprog].type_id; 6813 if (!btf_id) 6814 return -EFAULT; 6815 6816 if (prog->aux->func_info_aux[subprog].unreliable) 6817 return -EINVAL; 6818 6819 is_global = prog->aux->func_info_aux[subprog].linkage == BTF_FUNC_GLOBAL; 6820 err = btf_check_func_arg_match(env, btf, btf_id, regs, is_global, true); 6821 6822 /* Compiler optimizations can remove arguments from static functions 6823 * or mismatched type can be passed into a global function. 6824 * In such cases mark the function as unreliable from BTF point of view. 6825 */ 6826 if (err) 6827 prog->aux->func_info_aux[subprog].unreliable = true; 6828 return err; 6829 } 6830 6831 /* Convert BTF of a function into bpf_reg_state if possible 6832 * Returns: 6833 * EFAULT - there is a verifier bug. Abort verification. 6834 * EINVAL - cannot convert BTF. 6835 * 0 - Successfully converted BTF into bpf_reg_state 6836 * (either PTR_TO_CTX or SCALAR_VALUE). 6837 */ 6838 int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog, 6839 struct bpf_reg_state *regs) 6840 { 6841 struct bpf_verifier_log *log = &env->log; 6842 struct bpf_prog *prog = env->prog; 6843 enum bpf_prog_type prog_type = prog->type; 6844 struct btf *btf = prog->aux->btf; 6845 const struct btf_param *args; 6846 const struct btf_type *t, *ref_t; 6847 u32 i, nargs, btf_id; 6848 const char *tname; 6849 6850 if (!prog->aux->func_info || 6851 prog->aux->func_info_aux[subprog].linkage != BTF_FUNC_GLOBAL) { 6852 bpf_log(log, "Verifier bug\n"); 6853 return -EFAULT; 6854 } 6855 6856 btf_id = prog->aux->func_info[subprog].type_id; 6857 if (!btf_id) { 6858 bpf_log(log, "Global functions need valid BTF\n"); 6859 return -EFAULT; 6860 } 6861 6862 t = btf_type_by_id(btf, btf_id); 6863 if (!t || !btf_type_is_func(t)) { 6864 /* These checks were already done by the verifier while loading 6865 * struct bpf_func_info 6866 */ 6867 bpf_log(log, "BTF of func#%d doesn't point to KIND_FUNC\n", 6868 subprog); 6869 return -EFAULT; 6870 } 6871 tname = btf_name_by_offset(btf, t->name_off); 6872 6873 if (log->level & BPF_LOG_LEVEL) 6874 bpf_log(log, "Validating %s() func#%d...\n", 6875 tname, subprog); 6876 6877 if (prog->aux->func_info_aux[subprog].unreliable) { 6878 bpf_log(log, "Verifier bug in function %s()\n", tname); 6879 return -EFAULT; 6880 } 6881 if (prog_type == BPF_PROG_TYPE_EXT) 6882 prog_type = prog->aux->dst_prog->type; 6883 6884 t = btf_type_by_id(btf, t->type); 6885 if (!t || !btf_type_is_func_proto(t)) { 6886 bpf_log(log, "Invalid type of function %s()\n", tname); 6887 return -EFAULT; 6888 } 6889 args = (const struct btf_param *)(t + 1); 6890 nargs = btf_type_vlen(t); 6891 if (nargs > MAX_BPF_FUNC_REG_ARGS) { 6892 bpf_log(log, "Global function %s() with %d > %d args. Buggy compiler.\n", 6893 tname, nargs, MAX_BPF_FUNC_REG_ARGS); 6894 return -EINVAL; 6895 } 6896 /* check that function returns int */ 6897 t = btf_type_by_id(btf, t->type); 6898 while (btf_type_is_modifier(t)) 6899 t = btf_type_by_id(btf, t->type); 6900 if (!btf_type_is_int(t) && !btf_is_any_enum(t)) { 6901 bpf_log(log, 6902 "Global function %s() doesn't return scalar. Only those are supported.\n", 6903 tname); 6904 return -EINVAL; 6905 } 6906 /* Convert BTF function arguments into verifier types. 6907 * Only PTR_TO_CTX and SCALAR are supported atm. 6908 */ 6909 for (i = 0; i < nargs; i++) { 6910 struct bpf_reg_state *reg = ®s[i + 1]; 6911 6912 t = btf_type_by_id(btf, args[i].type); 6913 while (btf_type_is_modifier(t)) 6914 t = btf_type_by_id(btf, t->type); 6915 if (btf_type_is_int(t) || btf_is_any_enum(t)) { 6916 reg->type = SCALAR_VALUE; 6917 continue; 6918 } 6919 if (btf_type_is_ptr(t)) { 6920 if (btf_get_prog_ctx_type(log, btf, t, prog_type, i)) { 6921 reg->type = PTR_TO_CTX; 6922 continue; 6923 } 6924 6925 t = btf_type_skip_modifiers(btf, t->type, NULL); 6926 6927 ref_t = btf_resolve_size(btf, t, ®->mem_size); 6928 if (IS_ERR(ref_t)) { 6929 bpf_log(log, 6930 "arg#%d reference type('%s %s') size cannot be determined: %ld\n", 6931 i, btf_type_str(t), btf_name_by_offset(btf, t->name_off), 6932 PTR_ERR(ref_t)); 6933 return -EINVAL; 6934 } 6935 6936 reg->type = PTR_TO_MEM | PTR_MAYBE_NULL; 6937 reg->id = ++env->id_gen; 6938 6939 continue; 6940 } 6941 bpf_log(log, "Arg#%d type %s in %s() is not supported yet.\n", 6942 i, btf_type_str(t), tname); 6943 return -EINVAL; 6944 } 6945 return 0; 6946 } 6947 6948 static void btf_type_show(const struct btf *btf, u32 type_id, void *obj, 6949 struct btf_show *show) 6950 { 6951 const struct btf_type *t = btf_type_by_id(btf, type_id); 6952 6953 show->btf = btf; 6954 memset(&show->state, 0, sizeof(show->state)); 6955 memset(&show->obj, 0, sizeof(show->obj)); 6956 6957 btf_type_ops(t)->show(btf, t, type_id, obj, 0, show); 6958 } 6959 6960 static void btf_seq_show(struct btf_show *show, const char *fmt, 6961 va_list args) 6962 { 6963 seq_vprintf((struct seq_file *)show->target, fmt, args); 6964 } 6965 6966 int btf_type_seq_show_flags(const struct btf *btf, u32 type_id, 6967 void *obj, struct seq_file *m, u64 flags) 6968 { 6969 struct btf_show sseq; 6970 6971 sseq.target = m; 6972 sseq.showfn = btf_seq_show; 6973 sseq.flags = flags; 6974 6975 btf_type_show(btf, type_id, obj, &sseq); 6976 6977 return sseq.state.status; 6978 } 6979 6980 void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj, 6981 struct seq_file *m) 6982 { 6983 (void) btf_type_seq_show_flags(btf, type_id, obj, m, 6984 BTF_SHOW_NONAME | BTF_SHOW_COMPACT | 6985 BTF_SHOW_ZERO | BTF_SHOW_UNSAFE); 6986 } 6987 6988 struct btf_show_snprintf { 6989 struct btf_show show; 6990 int len_left; /* space left in string */ 6991 int len; /* length we would have written */ 6992 }; 6993 6994 static void btf_snprintf_show(struct btf_show *show, const char *fmt, 6995 va_list args) 6996 { 6997 struct btf_show_snprintf *ssnprintf = (struct btf_show_snprintf *)show; 6998 int len; 6999 7000 len = vsnprintf(show->target, ssnprintf->len_left, fmt, args); 7001 7002 if (len < 0) { 7003 ssnprintf->len_left = 0; 7004 ssnprintf->len = len; 7005 } else if (len >= ssnprintf->len_left) { 7006 /* no space, drive on to get length we would have written */ 7007 ssnprintf->len_left = 0; 7008 ssnprintf->len += len; 7009 } else { 7010 ssnprintf->len_left -= len; 7011 ssnprintf->len += len; 7012 show->target += len; 7013 } 7014 } 7015 7016 int btf_type_snprintf_show(const struct btf *btf, u32 type_id, void *obj, 7017 char *buf, int len, u64 flags) 7018 { 7019 struct btf_show_snprintf ssnprintf; 7020 7021 ssnprintf.show.target = buf; 7022 ssnprintf.show.flags = flags; 7023 ssnprintf.show.showfn = btf_snprintf_show; 7024 ssnprintf.len_left = len; 7025 ssnprintf.len = 0; 7026 7027 btf_type_show(btf, type_id, obj, (struct btf_show *)&ssnprintf); 7028 7029 /* If we encountered an error, return it. */ 7030 if (ssnprintf.show.state.status) 7031 return ssnprintf.show.state.status; 7032 7033 /* Otherwise return length we would have written */ 7034 return ssnprintf.len; 7035 } 7036 7037 #ifdef CONFIG_PROC_FS 7038 static void bpf_btf_show_fdinfo(struct seq_file *m, struct file *filp) 7039 { 7040 const struct btf *btf = filp->private_data; 7041 7042 seq_printf(m, "btf_id:\t%u\n", btf->id); 7043 } 7044 #endif 7045 7046 static int btf_release(struct inode *inode, struct file *filp) 7047 { 7048 btf_put(filp->private_data); 7049 return 0; 7050 } 7051 7052 const struct file_operations btf_fops = { 7053 #ifdef CONFIG_PROC_FS 7054 .show_fdinfo = bpf_btf_show_fdinfo, 7055 #endif 7056 .release = btf_release, 7057 }; 7058 7059 static int __btf_new_fd(struct btf *btf) 7060 { 7061 return anon_inode_getfd("btf", &btf_fops, btf, O_RDONLY | O_CLOEXEC); 7062 } 7063 7064 int btf_new_fd(const union bpf_attr *attr, bpfptr_t uattr) 7065 { 7066 struct btf *btf; 7067 int ret; 7068 7069 btf = btf_parse(make_bpfptr(attr->btf, uattr.is_kernel), 7070 attr->btf_size, attr->btf_log_level, 7071 u64_to_user_ptr(attr->btf_log_buf), 7072 attr->btf_log_size); 7073 if (IS_ERR(btf)) 7074 return PTR_ERR(btf); 7075 7076 ret = btf_alloc_id(btf); 7077 if (ret) { 7078 btf_free(btf); 7079 return ret; 7080 } 7081 7082 /* 7083 * The BTF ID is published to the userspace. 7084 * All BTF free must go through call_rcu() from 7085 * now on (i.e. free by calling btf_put()). 7086 */ 7087 7088 ret = __btf_new_fd(btf); 7089 if (ret < 0) 7090 btf_put(btf); 7091 7092 return ret; 7093 } 7094 7095 struct btf *btf_get_by_fd(int fd) 7096 { 7097 struct btf *btf; 7098 struct fd f; 7099 7100 f = fdget(fd); 7101 7102 if (!f.file) 7103 return ERR_PTR(-EBADF); 7104 7105 if (f.file->f_op != &btf_fops) { 7106 fdput(f); 7107 return ERR_PTR(-EINVAL); 7108 } 7109 7110 btf = f.file->private_data; 7111 refcount_inc(&btf->refcnt); 7112 fdput(f); 7113 7114 return btf; 7115 } 7116 7117 int btf_get_info_by_fd(const struct btf *btf, 7118 const union bpf_attr *attr, 7119 union bpf_attr __user *uattr) 7120 { 7121 struct bpf_btf_info __user *uinfo; 7122 struct bpf_btf_info info; 7123 u32 info_copy, btf_copy; 7124 void __user *ubtf; 7125 char __user *uname; 7126 u32 uinfo_len, uname_len, name_len; 7127 int ret = 0; 7128 7129 uinfo = u64_to_user_ptr(attr->info.info); 7130 uinfo_len = attr->info.info_len; 7131 7132 info_copy = min_t(u32, uinfo_len, sizeof(info)); 7133 memset(&info, 0, sizeof(info)); 7134 if (copy_from_user(&info, uinfo, info_copy)) 7135 return -EFAULT; 7136 7137 info.id = btf->id; 7138 ubtf = u64_to_user_ptr(info.btf); 7139 btf_copy = min_t(u32, btf->data_size, info.btf_size); 7140 if (copy_to_user(ubtf, btf->data, btf_copy)) 7141 return -EFAULT; 7142 info.btf_size = btf->data_size; 7143 7144 info.kernel_btf = btf->kernel_btf; 7145 7146 uname = u64_to_user_ptr(info.name); 7147 uname_len = info.name_len; 7148 if (!uname ^ !uname_len) 7149 return -EINVAL; 7150 7151 name_len = strlen(btf->name); 7152 info.name_len = name_len; 7153 7154 if (uname) { 7155 if (uname_len >= name_len + 1) { 7156 if (copy_to_user(uname, btf->name, name_len + 1)) 7157 return -EFAULT; 7158 } else { 7159 char zero = '\0'; 7160 7161 if (copy_to_user(uname, btf->name, uname_len - 1)) 7162 return -EFAULT; 7163 if (put_user(zero, uname + uname_len - 1)) 7164 return -EFAULT; 7165 /* let user-space know about too short buffer */ 7166 ret = -ENOSPC; 7167 } 7168 } 7169 7170 if (copy_to_user(uinfo, &info, info_copy) || 7171 put_user(info_copy, &uattr->info.info_len)) 7172 return -EFAULT; 7173 7174 return ret; 7175 } 7176 7177 int btf_get_fd_by_id(u32 id) 7178 { 7179 struct btf *btf; 7180 int fd; 7181 7182 rcu_read_lock(); 7183 btf = idr_find(&btf_idr, id); 7184 if (!btf || !refcount_inc_not_zero(&btf->refcnt)) 7185 btf = ERR_PTR(-ENOENT); 7186 rcu_read_unlock(); 7187 7188 if (IS_ERR(btf)) 7189 return PTR_ERR(btf); 7190 7191 fd = __btf_new_fd(btf); 7192 if (fd < 0) 7193 btf_put(btf); 7194 7195 return fd; 7196 } 7197 7198 u32 btf_obj_id(const struct btf *btf) 7199 { 7200 return btf->id; 7201 } 7202 7203 bool btf_is_kernel(const struct btf *btf) 7204 { 7205 return btf->kernel_btf; 7206 } 7207 7208 bool btf_is_module(const struct btf *btf) 7209 { 7210 return btf->kernel_btf && strcmp(btf->name, "vmlinux") != 0; 7211 } 7212 7213 enum { 7214 BTF_MODULE_F_LIVE = (1 << 0), 7215 }; 7216 7217 #ifdef CONFIG_DEBUG_INFO_BTF_MODULES 7218 struct btf_module { 7219 struct list_head list; 7220 struct module *module; 7221 struct btf *btf; 7222 struct bin_attribute *sysfs_attr; 7223 int flags; 7224 }; 7225 7226 static LIST_HEAD(btf_modules); 7227 static DEFINE_MUTEX(btf_module_mutex); 7228 7229 static ssize_t 7230 btf_module_read(struct file *file, struct kobject *kobj, 7231 struct bin_attribute *bin_attr, 7232 char *buf, loff_t off, size_t len) 7233 { 7234 const struct btf *btf = bin_attr->private; 7235 7236 memcpy(buf, btf->data + off, len); 7237 return len; 7238 } 7239 7240 static void purge_cand_cache(struct btf *btf); 7241 7242 static int btf_module_notify(struct notifier_block *nb, unsigned long op, 7243 void *module) 7244 { 7245 struct btf_module *btf_mod, *tmp; 7246 struct module *mod = module; 7247 struct btf *btf; 7248 int err = 0; 7249 7250 if (mod->btf_data_size == 0 || 7251 (op != MODULE_STATE_COMING && op != MODULE_STATE_LIVE && 7252 op != MODULE_STATE_GOING)) 7253 goto out; 7254 7255 switch (op) { 7256 case MODULE_STATE_COMING: 7257 btf_mod = kzalloc(sizeof(*btf_mod), GFP_KERNEL); 7258 if (!btf_mod) { 7259 err = -ENOMEM; 7260 goto out; 7261 } 7262 btf = btf_parse_module(mod->name, mod->btf_data, mod->btf_data_size); 7263 if (IS_ERR(btf)) { 7264 pr_warn("failed to validate module [%s] BTF: %ld\n", 7265 mod->name, PTR_ERR(btf)); 7266 kfree(btf_mod); 7267 if (!IS_ENABLED(CONFIG_MODULE_ALLOW_BTF_MISMATCH)) 7268 err = PTR_ERR(btf); 7269 goto out; 7270 } 7271 err = btf_alloc_id(btf); 7272 if (err) { 7273 btf_free(btf); 7274 kfree(btf_mod); 7275 goto out; 7276 } 7277 7278 purge_cand_cache(NULL); 7279 mutex_lock(&btf_module_mutex); 7280 btf_mod->module = module; 7281 btf_mod->btf = btf; 7282 list_add(&btf_mod->list, &btf_modules); 7283 mutex_unlock(&btf_module_mutex); 7284 7285 if (IS_ENABLED(CONFIG_SYSFS)) { 7286 struct bin_attribute *attr; 7287 7288 attr = kzalloc(sizeof(*attr), GFP_KERNEL); 7289 if (!attr) 7290 goto out; 7291 7292 sysfs_bin_attr_init(attr); 7293 attr->attr.name = btf->name; 7294 attr->attr.mode = 0444; 7295 attr->size = btf->data_size; 7296 attr->private = btf; 7297 attr->read = btf_module_read; 7298 7299 err = sysfs_create_bin_file(btf_kobj, attr); 7300 if (err) { 7301 pr_warn("failed to register module [%s] BTF in sysfs: %d\n", 7302 mod->name, err); 7303 kfree(attr); 7304 err = 0; 7305 goto out; 7306 } 7307 7308 btf_mod->sysfs_attr = attr; 7309 } 7310 7311 break; 7312 case MODULE_STATE_LIVE: 7313 mutex_lock(&btf_module_mutex); 7314 list_for_each_entry_safe(btf_mod, tmp, &btf_modules, list) { 7315 if (btf_mod->module != module) 7316 continue; 7317 7318 btf_mod->flags |= BTF_MODULE_F_LIVE; 7319 break; 7320 } 7321 mutex_unlock(&btf_module_mutex); 7322 break; 7323 case MODULE_STATE_GOING: 7324 mutex_lock(&btf_module_mutex); 7325 list_for_each_entry_safe(btf_mod, tmp, &btf_modules, list) { 7326 if (btf_mod->module != module) 7327 continue; 7328 7329 list_del(&btf_mod->list); 7330 if (btf_mod->sysfs_attr) 7331 sysfs_remove_bin_file(btf_kobj, btf_mod->sysfs_attr); 7332 purge_cand_cache(btf_mod->btf); 7333 btf_put(btf_mod->btf); 7334 kfree(btf_mod->sysfs_attr); 7335 kfree(btf_mod); 7336 break; 7337 } 7338 mutex_unlock(&btf_module_mutex); 7339 break; 7340 } 7341 out: 7342 return notifier_from_errno(err); 7343 } 7344 7345 static struct notifier_block btf_module_nb = { 7346 .notifier_call = btf_module_notify, 7347 }; 7348 7349 static int __init btf_module_init(void) 7350 { 7351 register_module_notifier(&btf_module_nb); 7352 return 0; 7353 } 7354 7355 fs_initcall(btf_module_init); 7356 #endif /* CONFIG_DEBUG_INFO_BTF_MODULES */ 7357 7358 struct module *btf_try_get_module(const struct btf *btf) 7359 { 7360 struct module *res = NULL; 7361 #ifdef CONFIG_DEBUG_INFO_BTF_MODULES 7362 struct btf_module *btf_mod, *tmp; 7363 7364 mutex_lock(&btf_module_mutex); 7365 list_for_each_entry_safe(btf_mod, tmp, &btf_modules, list) { 7366 if (btf_mod->btf != btf) 7367 continue; 7368 7369 /* We must only consider module whose __init routine has 7370 * finished, hence we must check for BTF_MODULE_F_LIVE flag, 7371 * which is set from the notifier callback for 7372 * MODULE_STATE_LIVE. 7373 */ 7374 if ((btf_mod->flags & BTF_MODULE_F_LIVE) && try_module_get(btf_mod->module)) 7375 res = btf_mod->module; 7376 7377 break; 7378 } 7379 mutex_unlock(&btf_module_mutex); 7380 #endif 7381 7382 return res; 7383 } 7384 7385 /* Returns struct btf corresponding to the struct module. 7386 * This function can return NULL or ERR_PTR. 7387 */ 7388 static struct btf *btf_get_module_btf(const struct module *module) 7389 { 7390 #ifdef CONFIG_DEBUG_INFO_BTF_MODULES 7391 struct btf_module *btf_mod, *tmp; 7392 #endif 7393 struct btf *btf = NULL; 7394 7395 if (!module) { 7396 btf = bpf_get_btf_vmlinux(); 7397 if (!IS_ERR_OR_NULL(btf)) 7398 btf_get(btf); 7399 return btf; 7400 } 7401 7402 #ifdef CONFIG_DEBUG_INFO_BTF_MODULES 7403 mutex_lock(&btf_module_mutex); 7404 list_for_each_entry_safe(btf_mod, tmp, &btf_modules, list) { 7405 if (btf_mod->module != module) 7406 continue; 7407 7408 btf_get(btf_mod->btf); 7409 btf = btf_mod->btf; 7410 break; 7411 } 7412 mutex_unlock(&btf_module_mutex); 7413 #endif 7414 7415 return btf; 7416 } 7417 7418 BPF_CALL_4(bpf_btf_find_by_name_kind, char *, name, int, name_sz, u32, kind, int, flags) 7419 { 7420 struct btf *btf = NULL; 7421 int btf_obj_fd = 0; 7422 long ret; 7423 7424 if (flags) 7425 return -EINVAL; 7426 7427 if (name_sz <= 1 || name[name_sz - 1]) 7428 return -EINVAL; 7429 7430 ret = bpf_find_btf_id(name, kind, &btf); 7431 if (ret > 0 && btf_is_module(btf)) { 7432 btf_obj_fd = __btf_new_fd(btf); 7433 if (btf_obj_fd < 0) { 7434 btf_put(btf); 7435 return btf_obj_fd; 7436 } 7437 return ret | (((u64)btf_obj_fd) << 32); 7438 } 7439 if (ret > 0) 7440 btf_put(btf); 7441 return ret; 7442 } 7443 7444 const struct bpf_func_proto bpf_btf_find_by_name_kind_proto = { 7445 .func = bpf_btf_find_by_name_kind, 7446 .gpl_only = false, 7447 .ret_type = RET_INTEGER, 7448 .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY, 7449 .arg2_type = ARG_CONST_SIZE, 7450 .arg3_type = ARG_ANYTHING, 7451 .arg4_type = ARG_ANYTHING, 7452 }; 7453 7454 BTF_ID_LIST_GLOBAL(btf_tracing_ids, MAX_BTF_TRACING_TYPE) 7455 #define BTF_TRACING_TYPE(name, type) BTF_ID(struct, type) 7456 BTF_TRACING_TYPE_xxx 7457 #undef BTF_TRACING_TYPE 7458 7459 /* Kernel Function (kfunc) BTF ID set registration API */ 7460 7461 static int btf_populate_kfunc_set(struct btf *btf, enum btf_kfunc_hook hook, 7462 struct btf_id_set8 *add_set) 7463 { 7464 bool vmlinux_set = !btf_is_module(btf); 7465 struct btf_kfunc_set_tab *tab; 7466 struct btf_id_set8 *set; 7467 u32 set_cnt; 7468 int ret; 7469 7470 if (hook >= BTF_KFUNC_HOOK_MAX) { 7471 ret = -EINVAL; 7472 goto end; 7473 } 7474 7475 if (!add_set->cnt) 7476 return 0; 7477 7478 tab = btf->kfunc_set_tab; 7479 if (!tab) { 7480 tab = kzalloc(sizeof(*tab), GFP_KERNEL | __GFP_NOWARN); 7481 if (!tab) 7482 return -ENOMEM; 7483 btf->kfunc_set_tab = tab; 7484 } 7485 7486 set = tab->sets[hook]; 7487 /* Warn when register_btf_kfunc_id_set is called twice for the same hook 7488 * for module sets. 7489 */ 7490 if (WARN_ON_ONCE(set && !vmlinux_set)) { 7491 ret = -EINVAL; 7492 goto end; 7493 } 7494 7495 /* We don't need to allocate, concatenate, and sort module sets, because 7496 * only one is allowed per hook. Hence, we can directly assign the 7497 * pointer and return. 7498 */ 7499 if (!vmlinux_set) { 7500 tab->sets[hook] = add_set; 7501 return 0; 7502 } 7503 7504 /* In case of vmlinux sets, there may be more than one set being 7505 * registered per hook. To create a unified set, we allocate a new set 7506 * and concatenate all individual sets being registered. While each set 7507 * is individually sorted, they may become unsorted when concatenated, 7508 * hence re-sorting the final set again is required to make binary 7509 * searching the set using btf_id_set8_contains function work. 7510 */ 7511 set_cnt = set ? set->cnt : 0; 7512 7513 if (set_cnt > U32_MAX - add_set->cnt) { 7514 ret = -EOVERFLOW; 7515 goto end; 7516 } 7517 7518 if (set_cnt + add_set->cnt > BTF_KFUNC_SET_MAX_CNT) { 7519 ret = -E2BIG; 7520 goto end; 7521 } 7522 7523 /* Grow set */ 7524 set = krealloc(tab->sets[hook], 7525 offsetof(struct btf_id_set8, pairs[set_cnt + add_set->cnt]), 7526 GFP_KERNEL | __GFP_NOWARN); 7527 if (!set) { 7528 ret = -ENOMEM; 7529 goto end; 7530 } 7531 7532 /* For newly allocated set, initialize set->cnt to 0 */ 7533 if (!tab->sets[hook]) 7534 set->cnt = 0; 7535 tab->sets[hook] = set; 7536 7537 /* Concatenate the two sets */ 7538 memcpy(set->pairs + set->cnt, add_set->pairs, add_set->cnt * sizeof(set->pairs[0])); 7539 set->cnt += add_set->cnt; 7540 7541 sort(set->pairs, set->cnt, sizeof(set->pairs[0]), btf_id_cmp_func, NULL); 7542 7543 return 0; 7544 end: 7545 btf_free_kfunc_set_tab(btf); 7546 return ret; 7547 } 7548 7549 static u32 *__btf_kfunc_id_set_contains(const struct btf *btf, 7550 enum btf_kfunc_hook hook, 7551 u32 kfunc_btf_id) 7552 { 7553 struct btf_id_set8 *set; 7554 u32 *id; 7555 7556 if (hook >= BTF_KFUNC_HOOK_MAX) 7557 return NULL; 7558 if (!btf->kfunc_set_tab) 7559 return NULL; 7560 set = btf->kfunc_set_tab->sets[hook]; 7561 if (!set) 7562 return NULL; 7563 id = btf_id_set8_contains(set, kfunc_btf_id); 7564 if (!id) 7565 return NULL; 7566 /* The flags for BTF ID are located next to it */ 7567 return id + 1; 7568 } 7569 7570 static int bpf_prog_type_to_kfunc_hook(enum bpf_prog_type prog_type) 7571 { 7572 switch (prog_type) { 7573 case BPF_PROG_TYPE_UNSPEC: 7574 return BTF_KFUNC_HOOK_COMMON; 7575 case BPF_PROG_TYPE_XDP: 7576 return BTF_KFUNC_HOOK_XDP; 7577 case BPF_PROG_TYPE_SCHED_CLS: 7578 return BTF_KFUNC_HOOK_TC; 7579 case BPF_PROG_TYPE_STRUCT_OPS: 7580 return BTF_KFUNC_HOOK_STRUCT_OPS; 7581 case BPF_PROG_TYPE_TRACING: 7582 case BPF_PROG_TYPE_LSM: 7583 return BTF_KFUNC_HOOK_TRACING; 7584 case BPF_PROG_TYPE_SYSCALL: 7585 return BTF_KFUNC_HOOK_SYSCALL; 7586 default: 7587 return BTF_KFUNC_HOOK_MAX; 7588 } 7589 } 7590 7591 /* Caution: 7592 * Reference to the module (obtained using btf_try_get_module) corresponding to 7593 * the struct btf *MUST* be held when calling this function from verifier 7594 * context. This is usually true as we stash references in prog's kfunc_btf_tab; 7595 * keeping the reference for the duration of the call provides the necessary 7596 * protection for looking up a well-formed btf->kfunc_set_tab. 7597 */ 7598 u32 *btf_kfunc_id_set_contains(const struct btf *btf, 7599 enum bpf_prog_type prog_type, 7600 u32 kfunc_btf_id) 7601 { 7602 enum btf_kfunc_hook hook; 7603 u32 *kfunc_flags; 7604 7605 kfunc_flags = __btf_kfunc_id_set_contains(btf, BTF_KFUNC_HOOK_COMMON, kfunc_btf_id); 7606 if (kfunc_flags) 7607 return kfunc_flags; 7608 7609 hook = bpf_prog_type_to_kfunc_hook(prog_type); 7610 return __btf_kfunc_id_set_contains(btf, hook, kfunc_btf_id); 7611 } 7612 7613 u32 *btf_kfunc_is_modify_return(const struct btf *btf, u32 kfunc_btf_id) 7614 { 7615 return __btf_kfunc_id_set_contains(btf, BTF_KFUNC_HOOK_FMODRET, kfunc_btf_id); 7616 } 7617 7618 static int __register_btf_kfunc_id_set(enum btf_kfunc_hook hook, 7619 const struct btf_kfunc_id_set *kset) 7620 { 7621 struct btf *btf; 7622 int ret; 7623 7624 btf = btf_get_module_btf(kset->owner); 7625 if (!btf) { 7626 if (!kset->owner && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) { 7627 pr_err("missing vmlinux BTF, cannot register kfuncs\n"); 7628 return -ENOENT; 7629 } 7630 if (kset->owner && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES)) { 7631 pr_err("missing module BTF, cannot register kfuncs\n"); 7632 return -ENOENT; 7633 } 7634 return 0; 7635 } 7636 if (IS_ERR(btf)) 7637 return PTR_ERR(btf); 7638 7639 ret = btf_populate_kfunc_set(btf, hook, kset->set); 7640 btf_put(btf); 7641 return ret; 7642 } 7643 7644 /* This function must be invoked only from initcalls/module init functions */ 7645 int register_btf_kfunc_id_set(enum bpf_prog_type prog_type, 7646 const struct btf_kfunc_id_set *kset) 7647 { 7648 enum btf_kfunc_hook hook; 7649 7650 hook = bpf_prog_type_to_kfunc_hook(prog_type); 7651 return __register_btf_kfunc_id_set(hook, kset); 7652 } 7653 EXPORT_SYMBOL_GPL(register_btf_kfunc_id_set); 7654 7655 /* This function must be invoked only from initcalls/module init functions */ 7656 int register_btf_fmodret_id_set(const struct btf_kfunc_id_set *kset) 7657 { 7658 return __register_btf_kfunc_id_set(BTF_KFUNC_HOOK_FMODRET, kset); 7659 } 7660 EXPORT_SYMBOL_GPL(register_btf_fmodret_id_set); 7661 7662 s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id) 7663 { 7664 struct btf_id_dtor_kfunc_tab *tab = btf->dtor_kfunc_tab; 7665 struct btf_id_dtor_kfunc *dtor; 7666 7667 if (!tab) 7668 return -ENOENT; 7669 /* Even though the size of tab->dtors[0] is > sizeof(u32), we only need 7670 * to compare the first u32 with btf_id, so we can reuse btf_id_cmp_func. 7671 */ 7672 BUILD_BUG_ON(offsetof(struct btf_id_dtor_kfunc, btf_id) != 0); 7673 dtor = bsearch(&btf_id, tab->dtors, tab->cnt, sizeof(tab->dtors[0]), btf_id_cmp_func); 7674 if (!dtor) 7675 return -ENOENT; 7676 return dtor->kfunc_btf_id; 7677 } 7678 7679 static int btf_check_dtor_kfuncs(struct btf *btf, const struct btf_id_dtor_kfunc *dtors, u32 cnt) 7680 { 7681 const struct btf_type *dtor_func, *dtor_func_proto, *t; 7682 const struct btf_param *args; 7683 s32 dtor_btf_id; 7684 u32 nr_args, i; 7685 7686 for (i = 0; i < cnt; i++) { 7687 dtor_btf_id = dtors[i].kfunc_btf_id; 7688 7689 dtor_func = btf_type_by_id(btf, dtor_btf_id); 7690 if (!dtor_func || !btf_type_is_func(dtor_func)) 7691 return -EINVAL; 7692 7693 dtor_func_proto = btf_type_by_id(btf, dtor_func->type); 7694 if (!dtor_func_proto || !btf_type_is_func_proto(dtor_func_proto)) 7695 return -EINVAL; 7696 7697 /* Make sure the prototype of the destructor kfunc is 'void func(type *)' */ 7698 t = btf_type_by_id(btf, dtor_func_proto->type); 7699 if (!t || !btf_type_is_void(t)) 7700 return -EINVAL; 7701 7702 nr_args = btf_type_vlen(dtor_func_proto); 7703 if (nr_args != 1) 7704 return -EINVAL; 7705 args = btf_params(dtor_func_proto); 7706 t = btf_type_by_id(btf, args[0].type); 7707 /* Allow any pointer type, as width on targets Linux supports 7708 * will be same for all pointer types (i.e. sizeof(void *)) 7709 */ 7710 if (!t || !btf_type_is_ptr(t)) 7711 return -EINVAL; 7712 } 7713 return 0; 7714 } 7715 7716 /* This function must be invoked only from initcalls/module init functions */ 7717 int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors, u32 add_cnt, 7718 struct module *owner) 7719 { 7720 struct btf_id_dtor_kfunc_tab *tab; 7721 struct btf *btf; 7722 u32 tab_cnt; 7723 int ret; 7724 7725 btf = btf_get_module_btf(owner); 7726 if (!btf) { 7727 if (!owner && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) { 7728 pr_err("missing vmlinux BTF, cannot register dtor kfuncs\n"); 7729 return -ENOENT; 7730 } 7731 if (owner && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES)) { 7732 pr_err("missing module BTF, cannot register dtor kfuncs\n"); 7733 return -ENOENT; 7734 } 7735 return 0; 7736 } 7737 if (IS_ERR(btf)) 7738 return PTR_ERR(btf); 7739 7740 if (add_cnt >= BTF_DTOR_KFUNC_MAX_CNT) { 7741 pr_err("cannot register more than %d kfunc destructors\n", BTF_DTOR_KFUNC_MAX_CNT); 7742 ret = -E2BIG; 7743 goto end; 7744 } 7745 7746 /* Ensure that the prototype of dtor kfuncs being registered is sane */ 7747 ret = btf_check_dtor_kfuncs(btf, dtors, add_cnt); 7748 if (ret < 0) 7749 goto end; 7750 7751 tab = btf->dtor_kfunc_tab; 7752 /* Only one call allowed for modules */ 7753 if (WARN_ON_ONCE(tab && btf_is_module(btf))) { 7754 ret = -EINVAL; 7755 goto end; 7756 } 7757 7758 tab_cnt = tab ? tab->cnt : 0; 7759 if (tab_cnt > U32_MAX - add_cnt) { 7760 ret = -EOVERFLOW; 7761 goto end; 7762 } 7763 if (tab_cnt + add_cnt >= BTF_DTOR_KFUNC_MAX_CNT) { 7764 pr_err("cannot register more than %d kfunc destructors\n", BTF_DTOR_KFUNC_MAX_CNT); 7765 ret = -E2BIG; 7766 goto end; 7767 } 7768 7769 tab = krealloc(btf->dtor_kfunc_tab, 7770 offsetof(struct btf_id_dtor_kfunc_tab, dtors[tab_cnt + add_cnt]), 7771 GFP_KERNEL | __GFP_NOWARN); 7772 if (!tab) { 7773 ret = -ENOMEM; 7774 goto end; 7775 } 7776 7777 if (!btf->dtor_kfunc_tab) 7778 tab->cnt = 0; 7779 btf->dtor_kfunc_tab = tab; 7780 7781 memcpy(tab->dtors + tab->cnt, dtors, add_cnt * sizeof(tab->dtors[0])); 7782 tab->cnt += add_cnt; 7783 7784 sort(tab->dtors, tab->cnt, sizeof(tab->dtors[0]), btf_id_cmp_func, NULL); 7785 7786 return 0; 7787 end: 7788 btf_free_dtor_kfunc_tab(btf); 7789 btf_put(btf); 7790 return ret; 7791 } 7792 EXPORT_SYMBOL_GPL(register_btf_id_dtor_kfuncs); 7793 7794 #define MAX_TYPES_ARE_COMPAT_DEPTH 2 7795 7796 /* Check local and target types for compatibility. This check is used for 7797 * type-based CO-RE relocations and follow slightly different rules than 7798 * field-based relocations. This function assumes that root types were already 7799 * checked for name match. Beyond that initial root-level name check, names 7800 * are completely ignored. Compatibility rules are as follows: 7801 * - any two STRUCTs/UNIONs/FWDs/ENUMs/INTs/ENUM64s are considered compatible, but 7802 * kind should match for local and target types (i.e., STRUCT is not 7803 * compatible with UNION); 7804 * - for ENUMs/ENUM64s, the size is ignored; 7805 * - for INT, size and signedness are ignored; 7806 * - for ARRAY, dimensionality is ignored, element types are checked for 7807 * compatibility recursively; 7808 * - CONST/VOLATILE/RESTRICT modifiers are ignored; 7809 * - TYPEDEFs/PTRs are compatible if types they pointing to are compatible; 7810 * - FUNC_PROTOs are compatible if they have compatible signature: same 7811 * number of input args and compatible return and argument types. 7812 * These rules are not set in stone and probably will be adjusted as we get 7813 * more experience with using BPF CO-RE relocations. 7814 */ 7815 int bpf_core_types_are_compat(const struct btf *local_btf, __u32 local_id, 7816 const struct btf *targ_btf, __u32 targ_id) 7817 { 7818 return __bpf_core_types_are_compat(local_btf, local_id, targ_btf, targ_id, 7819 MAX_TYPES_ARE_COMPAT_DEPTH); 7820 } 7821 7822 #define MAX_TYPES_MATCH_DEPTH 2 7823 7824 int bpf_core_types_match(const struct btf *local_btf, u32 local_id, 7825 const struct btf *targ_btf, u32 targ_id) 7826 { 7827 return __bpf_core_types_match(local_btf, local_id, targ_btf, targ_id, false, 7828 MAX_TYPES_MATCH_DEPTH); 7829 } 7830 7831 static bool bpf_core_is_flavor_sep(const char *s) 7832 { 7833 /* check X___Y name pattern, where X and Y are not underscores */ 7834 return s[0] != '_' && /* X */ 7835 s[1] == '_' && s[2] == '_' && s[3] == '_' && /* ___ */ 7836 s[4] != '_'; /* Y */ 7837 } 7838 7839 size_t bpf_core_essential_name_len(const char *name) 7840 { 7841 size_t n = strlen(name); 7842 int i; 7843 7844 for (i = n - 5; i >= 0; i--) { 7845 if (bpf_core_is_flavor_sep(name + i)) 7846 return i + 1; 7847 } 7848 return n; 7849 } 7850 7851 struct bpf_cand_cache { 7852 const char *name; 7853 u32 name_len; 7854 u16 kind; 7855 u16 cnt; 7856 struct { 7857 const struct btf *btf; 7858 u32 id; 7859 } cands[]; 7860 }; 7861 7862 static void bpf_free_cands(struct bpf_cand_cache *cands) 7863 { 7864 if (!cands->cnt) 7865 /* empty candidate array was allocated on stack */ 7866 return; 7867 kfree(cands); 7868 } 7869 7870 static void bpf_free_cands_from_cache(struct bpf_cand_cache *cands) 7871 { 7872 kfree(cands->name); 7873 kfree(cands); 7874 } 7875 7876 #define VMLINUX_CAND_CACHE_SIZE 31 7877 static struct bpf_cand_cache *vmlinux_cand_cache[VMLINUX_CAND_CACHE_SIZE]; 7878 7879 #define MODULE_CAND_CACHE_SIZE 31 7880 static struct bpf_cand_cache *module_cand_cache[MODULE_CAND_CACHE_SIZE]; 7881 7882 static DEFINE_MUTEX(cand_cache_mutex); 7883 7884 static void __print_cand_cache(struct bpf_verifier_log *log, 7885 struct bpf_cand_cache **cache, 7886 int cache_size) 7887 { 7888 struct bpf_cand_cache *cc; 7889 int i, j; 7890 7891 for (i = 0; i < cache_size; i++) { 7892 cc = cache[i]; 7893 if (!cc) 7894 continue; 7895 bpf_log(log, "[%d]%s(", i, cc->name); 7896 for (j = 0; j < cc->cnt; j++) { 7897 bpf_log(log, "%d", cc->cands[j].id); 7898 if (j < cc->cnt - 1) 7899 bpf_log(log, " "); 7900 } 7901 bpf_log(log, "), "); 7902 } 7903 } 7904 7905 static void print_cand_cache(struct bpf_verifier_log *log) 7906 { 7907 mutex_lock(&cand_cache_mutex); 7908 bpf_log(log, "vmlinux_cand_cache:"); 7909 __print_cand_cache(log, vmlinux_cand_cache, VMLINUX_CAND_CACHE_SIZE); 7910 bpf_log(log, "\nmodule_cand_cache:"); 7911 __print_cand_cache(log, module_cand_cache, MODULE_CAND_CACHE_SIZE); 7912 bpf_log(log, "\n"); 7913 mutex_unlock(&cand_cache_mutex); 7914 } 7915 7916 static u32 hash_cands(struct bpf_cand_cache *cands) 7917 { 7918 return jhash(cands->name, cands->name_len, 0); 7919 } 7920 7921 static struct bpf_cand_cache *check_cand_cache(struct bpf_cand_cache *cands, 7922 struct bpf_cand_cache **cache, 7923 int cache_size) 7924 { 7925 struct bpf_cand_cache *cc = cache[hash_cands(cands) % cache_size]; 7926 7927 if (cc && cc->name_len == cands->name_len && 7928 !strncmp(cc->name, cands->name, cands->name_len)) 7929 return cc; 7930 return NULL; 7931 } 7932 7933 static size_t sizeof_cands(int cnt) 7934 { 7935 return offsetof(struct bpf_cand_cache, cands[cnt]); 7936 } 7937 7938 static struct bpf_cand_cache *populate_cand_cache(struct bpf_cand_cache *cands, 7939 struct bpf_cand_cache **cache, 7940 int cache_size) 7941 { 7942 struct bpf_cand_cache **cc = &cache[hash_cands(cands) % cache_size], *new_cands; 7943 7944 if (*cc) { 7945 bpf_free_cands_from_cache(*cc); 7946 *cc = NULL; 7947 } 7948 new_cands = kmemdup(cands, sizeof_cands(cands->cnt), GFP_KERNEL); 7949 if (!new_cands) { 7950 bpf_free_cands(cands); 7951 return ERR_PTR(-ENOMEM); 7952 } 7953 /* strdup the name, since it will stay in cache. 7954 * the cands->name points to strings in prog's BTF and the prog can be unloaded. 7955 */ 7956 new_cands->name = kmemdup_nul(cands->name, cands->name_len, GFP_KERNEL); 7957 bpf_free_cands(cands); 7958 if (!new_cands->name) { 7959 kfree(new_cands); 7960 return ERR_PTR(-ENOMEM); 7961 } 7962 *cc = new_cands; 7963 return new_cands; 7964 } 7965 7966 #ifdef CONFIG_DEBUG_INFO_BTF_MODULES 7967 static void __purge_cand_cache(struct btf *btf, struct bpf_cand_cache **cache, 7968 int cache_size) 7969 { 7970 struct bpf_cand_cache *cc; 7971 int i, j; 7972 7973 for (i = 0; i < cache_size; i++) { 7974 cc = cache[i]; 7975 if (!cc) 7976 continue; 7977 if (!btf) { 7978 /* when new module is loaded purge all of module_cand_cache, 7979 * since new module might have candidates with the name 7980 * that matches cached cands. 7981 */ 7982 bpf_free_cands_from_cache(cc); 7983 cache[i] = NULL; 7984 continue; 7985 } 7986 /* when module is unloaded purge cache entries 7987 * that match module's btf 7988 */ 7989 for (j = 0; j < cc->cnt; j++) 7990 if (cc->cands[j].btf == btf) { 7991 bpf_free_cands_from_cache(cc); 7992 cache[i] = NULL; 7993 break; 7994 } 7995 } 7996 7997 } 7998 7999 static void purge_cand_cache(struct btf *btf) 8000 { 8001 mutex_lock(&cand_cache_mutex); 8002 __purge_cand_cache(btf, module_cand_cache, MODULE_CAND_CACHE_SIZE); 8003 mutex_unlock(&cand_cache_mutex); 8004 } 8005 #endif 8006 8007 static struct bpf_cand_cache * 8008 bpf_core_add_cands(struct bpf_cand_cache *cands, const struct btf *targ_btf, 8009 int targ_start_id) 8010 { 8011 struct bpf_cand_cache *new_cands; 8012 const struct btf_type *t; 8013 const char *targ_name; 8014 size_t targ_essent_len; 8015 int n, i; 8016 8017 n = btf_nr_types(targ_btf); 8018 for (i = targ_start_id; i < n; i++) { 8019 t = btf_type_by_id(targ_btf, i); 8020 if (btf_kind(t) != cands->kind) 8021 continue; 8022 8023 targ_name = btf_name_by_offset(targ_btf, t->name_off); 8024 if (!targ_name) 8025 continue; 8026 8027 /* the resched point is before strncmp to make sure that search 8028 * for non-existing name will have a chance to schedule(). 8029 */ 8030 cond_resched(); 8031 8032 if (strncmp(cands->name, targ_name, cands->name_len) != 0) 8033 continue; 8034 8035 targ_essent_len = bpf_core_essential_name_len(targ_name); 8036 if (targ_essent_len != cands->name_len) 8037 continue; 8038 8039 /* most of the time there is only one candidate for a given kind+name pair */ 8040 new_cands = kmalloc(sizeof_cands(cands->cnt + 1), GFP_KERNEL); 8041 if (!new_cands) { 8042 bpf_free_cands(cands); 8043 return ERR_PTR(-ENOMEM); 8044 } 8045 8046 memcpy(new_cands, cands, sizeof_cands(cands->cnt)); 8047 bpf_free_cands(cands); 8048 cands = new_cands; 8049 cands->cands[cands->cnt].btf = targ_btf; 8050 cands->cands[cands->cnt].id = i; 8051 cands->cnt++; 8052 } 8053 return cands; 8054 } 8055 8056 static struct bpf_cand_cache * 8057 bpf_core_find_cands(struct bpf_core_ctx *ctx, u32 local_type_id) 8058 { 8059 struct bpf_cand_cache *cands, *cc, local_cand = {}; 8060 const struct btf *local_btf = ctx->btf; 8061 const struct btf_type *local_type; 8062 const struct btf *main_btf; 8063 size_t local_essent_len; 8064 struct btf *mod_btf; 8065 const char *name; 8066 int id; 8067 8068 main_btf = bpf_get_btf_vmlinux(); 8069 if (IS_ERR(main_btf)) 8070 return ERR_CAST(main_btf); 8071 if (!main_btf) 8072 return ERR_PTR(-EINVAL); 8073 8074 local_type = btf_type_by_id(local_btf, local_type_id); 8075 if (!local_type) 8076 return ERR_PTR(-EINVAL); 8077 8078 name = btf_name_by_offset(local_btf, local_type->name_off); 8079 if (str_is_empty(name)) 8080 return ERR_PTR(-EINVAL); 8081 local_essent_len = bpf_core_essential_name_len(name); 8082 8083 cands = &local_cand; 8084 cands->name = name; 8085 cands->kind = btf_kind(local_type); 8086 cands->name_len = local_essent_len; 8087 8088 cc = check_cand_cache(cands, vmlinux_cand_cache, VMLINUX_CAND_CACHE_SIZE); 8089 /* cands is a pointer to stack here */ 8090 if (cc) { 8091 if (cc->cnt) 8092 return cc; 8093 goto check_modules; 8094 } 8095 8096 /* Attempt to find target candidates in vmlinux BTF first */ 8097 cands = bpf_core_add_cands(cands, main_btf, 1); 8098 if (IS_ERR(cands)) 8099 return ERR_CAST(cands); 8100 8101 /* cands is a pointer to kmalloced memory here if cands->cnt > 0 */ 8102 8103 /* populate cache even when cands->cnt == 0 */ 8104 cc = populate_cand_cache(cands, vmlinux_cand_cache, VMLINUX_CAND_CACHE_SIZE); 8105 if (IS_ERR(cc)) 8106 return ERR_CAST(cc); 8107 8108 /* if vmlinux BTF has any candidate, don't go for module BTFs */ 8109 if (cc->cnt) 8110 return cc; 8111 8112 check_modules: 8113 /* cands is a pointer to stack here and cands->cnt == 0 */ 8114 cc = check_cand_cache(cands, module_cand_cache, MODULE_CAND_CACHE_SIZE); 8115 if (cc) 8116 /* if cache has it return it even if cc->cnt == 0 */ 8117 return cc; 8118 8119 /* If candidate is not found in vmlinux's BTF then search in module's BTFs */ 8120 spin_lock_bh(&btf_idr_lock); 8121 idr_for_each_entry(&btf_idr, mod_btf, id) { 8122 if (!btf_is_module(mod_btf)) 8123 continue; 8124 /* linear search could be slow hence unlock/lock 8125 * the IDR to avoiding holding it for too long 8126 */ 8127 btf_get(mod_btf); 8128 spin_unlock_bh(&btf_idr_lock); 8129 cands = bpf_core_add_cands(cands, mod_btf, btf_nr_types(main_btf)); 8130 if (IS_ERR(cands)) { 8131 btf_put(mod_btf); 8132 return ERR_CAST(cands); 8133 } 8134 spin_lock_bh(&btf_idr_lock); 8135 btf_put(mod_btf); 8136 } 8137 spin_unlock_bh(&btf_idr_lock); 8138 /* cands is a pointer to kmalloced memory here if cands->cnt > 0 8139 * or pointer to stack if cands->cnd == 0. 8140 * Copy it into the cache even when cands->cnt == 0 and 8141 * return the result. 8142 */ 8143 return populate_cand_cache(cands, module_cand_cache, MODULE_CAND_CACHE_SIZE); 8144 } 8145 8146 int bpf_core_apply(struct bpf_core_ctx *ctx, const struct bpf_core_relo *relo, 8147 int relo_idx, void *insn) 8148 { 8149 bool need_cands = relo->kind != BPF_CORE_TYPE_ID_LOCAL; 8150 struct bpf_core_cand_list cands = {}; 8151 struct bpf_core_relo_res targ_res; 8152 struct bpf_core_spec *specs; 8153 int err; 8154 8155 /* ~4k of temp memory necessary to convert LLVM spec like "0:1:0:5" 8156 * into arrays of btf_ids of struct fields and array indices. 8157 */ 8158 specs = kcalloc(3, sizeof(*specs), GFP_KERNEL); 8159 if (!specs) 8160 return -ENOMEM; 8161 8162 if (need_cands) { 8163 struct bpf_cand_cache *cc; 8164 int i; 8165 8166 mutex_lock(&cand_cache_mutex); 8167 cc = bpf_core_find_cands(ctx, relo->type_id); 8168 if (IS_ERR(cc)) { 8169 bpf_log(ctx->log, "target candidate search failed for %d\n", 8170 relo->type_id); 8171 err = PTR_ERR(cc); 8172 goto out; 8173 } 8174 if (cc->cnt) { 8175 cands.cands = kcalloc(cc->cnt, sizeof(*cands.cands), GFP_KERNEL); 8176 if (!cands.cands) { 8177 err = -ENOMEM; 8178 goto out; 8179 } 8180 } 8181 for (i = 0; i < cc->cnt; i++) { 8182 bpf_log(ctx->log, 8183 "CO-RE relocating %s %s: found target candidate [%d]\n", 8184 btf_kind_str[cc->kind], cc->name, cc->cands[i].id); 8185 cands.cands[i].btf = cc->cands[i].btf; 8186 cands.cands[i].id = cc->cands[i].id; 8187 } 8188 cands.len = cc->cnt; 8189 /* cand_cache_mutex needs to span the cache lookup and 8190 * copy of btf pointer into bpf_core_cand_list, 8191 * since module can be unloaded while bpf_core_calc_relo_insn 8192 * is working with module's btf. 8193 */ 8194 } 8195 8196 err = bpf_core_calc_relo_insn((void *)ctx->log, relo, relo_idx, ctx->btf, &cands, specs, 8197 &targ_res); 8198 if (err) 8199 goto out; 8200 8201 err = bpf_core_patch_insn((void *)ctx->log, insn, relo->insn_off / 8, relo, relo_idx, 8202 &targ_res); 8203 8204 out: 8205 kfree(specs); 8206 if (need_cands) { 8207 kfree(cands.cands); 8208 mutex_unlock(&cand_cache_mutex); 8209 if (ctx->log->level & BPF_LOG_LEVEL2) 8210 print_cand_cache(ctx->log); 8211 } 8212 return err; 8213 } 8214