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