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