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