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