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