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