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