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