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