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