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