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