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