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