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