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