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