1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018 Facebook */
4 #include <uapi/linux/btf.h>
5 #include <uapi/linux/bpf.h>
6 #include <uapi/linux/bpf_perf_event.h>
7 #include <uapi/linux/types.h>
8 #include <linux/seq_file.h>
9 #include <linux/compiler.h>
10 #include <linux/ctype.h>
11 #include <linux/errno.h>
12 #include <linux/slab.h>
13 #include <linux/anon_inodes.h>
14 #include <linux/file.h>
15 #include <linux/uaccess.h>
16 #include <linux/kernel.h>
17 #include <linux/idr.h>
18 #include <linux/sort.h>
19 #include <linux/bpf_verifier.h>
20 #include <linux/btf.h>
21 #include <linux/btf_ids.h>
22 #include <linux/bpf.h>
23 #include <linux/bpf_lsm.h>
24 #include <linux/skmsg.h>
25 #include <linux/perf_event.h>
26 #include <linux/bsearch.h>
27 #include <linux/kobject.h>
28 #include <linux/sysfs.h>
30 #include <net/netfilter/nf_bpf_link.h>
34 #include "../tools/lib/bpf/relo_core.h"
36 /* BTF (BPF Type Format) is the meta data format which describes
37 * the data types of BPF program/map. Hence, it basically focus
38 * on the C programming language which the modern BPF is primary
43 * The BTF data is stored under the ".BTF" ELF section
47 * Each 'struct btf_type' object describes a C data type.
48 * Depending on the type it is describing, a 'struct btf_type'
49 * object may be followed by more data. F.e.
50 * To describe an array, 'struct btf_type' is followed by
53 * 'struct btf_type' and any extra data following it are
58 * The BTF type section contains a list of 'struct btf_type' objects.
59 * Each one describes a C type. Recall from the above section
60 * that a 'struct btf_type' object could be immediately followed by extra
61 * data in order to describe some particular C types.
65 * Each btf_type object is identified by a type_id. The type_id
66 * is implicitly implied by the location of the btf_type object in
67 * the BTF type section. The first one has type_id 1. The second
68 * one has type_id 2...etc. Hence, an earlier btf_type has
71 * A btf_type object may refer to another btf_type object by using
72 * type_id (i.e. the "type" in the "struct btf_type").
74 * NOTE that we cannot assume any reference-order.
75 * A btf_type object can refer to an earlier btf_type object
76 * but it can also refer to a later btf_type object.
78 * For example, to describe "const void *". A btf_type
79 * object describing "const" may refer to another btf_type
80 * object describing "void *". This type-reference is done
81 * by specifying type_id:
83 * [1] CONST (anon) type_id=2
84 * [2] PTR (anon) type_id=0
86 * The above is the btf_verifier debug log:
87 * - Each line started with "[?]" is a btf_type object
88 * - [?] is the type_id of the btf_type object.
89 * - CONST/PTR is the BTF_KIND_XXX
90 * - "(anon)" is the name of the type. It just
91 * happens that CONST and PTR has no name.
92 * - type_id=XXX is the 'u32 type' in btf_type
94 * NOTE: "void" has type_id 0
98 * The BTF string section contains the names used by the type section.
99 * Each string is referred by an "offset" from the beginning of the
102 * Each string is '\0' terminated.
104 * The first character in the string section must be '\0'
105 * which is used to mean 'anonymous'. Some btf_type may not
111 * To verify BTF data, two passes are needed.
115 * The first pass is to collect all btf_type objects to
116 * an array: "btf->types".
118 * Depending on the C type that a btf_type is describing,
119 * a btf_type may be followed by extra data. We don't know
120 * how many btf_type is there, and more importantly we don't
121 * know where each btf_type is located in the type section.
123 * Without knowing the location of each type_id, most verifications
124 * cannot be done. e.g. an earlier btf_type may refer to a later
125 * btf_type (recall the "const void *" above), so we cannot
126 * check this type-reference in the first pass.
128 * In the first pass, it still does some verifications (e.g.
129 * checking the name is a valid offset to the string section).
133 * The main focus is to resolve a btf_type that is referring
136 * We have to ensure the referring type:
137 * 1) does exist in the BTF (i.e. in btf->types[])
138 * 2) does not cause a loop:
147 * btf_type_needs_resolve() decides if a btf_type needs
150 * The needs_resolve type implements the "resolve()" ops which
151 * essentially does a DFS and detects backedge.
153 * During resolve (or DFS), different C types have different
154 * "RESOLVED" conditions.
156 * When resolving a BTF_KIND_STRUCT, we need to resolve all its
157 * members because a member is always referring to another
158 * type. A struct's member can be treated as "RESOLVED" if
159 * it is referring to a BTF_KIND_PTR. Otherwise, the
160 * following valid C struct would be rejected:
167 * When resolving a BTF_KIND_PTR, it needs to keep resolving if
168 * it is referring to another BTF_KIND_PTR. Otherwise, we cannot
169 * detect a pointer loop, e.g.:
170 * BTF_KIND_CONST -> BTF_KIND_PTR -> BTF_KIND_CONST -> BTF_KIND_PTR +
172 * +-----------------------------------------+
176 #define BITS_PER_U128 (sizeof(u64) * BITS_PER_BYTE * 2)
177 #define BITS_PER_BYTE_MASK (BITS_PER_BYTE - 1)
178 #define BITS_PER_BYTE_MASKED(bits) ((bits) & BITS_PER_BYTE_MASK)
179 #define BITS_ROUNDDOWN_BYTES(bits) ((bits) >> 3)
180 #define BITS_ROUNDUP_BYTES(bits) \
181 (BITS_ROUNDDOWN_BYTES(bits) + !!BITS_PER_BYTE_MASKED(bits))
183 #define BTF_INFO_MASK 0x9f00ffff
184 #define BTF_INT_MASK 0x0fffffff
185 #define BTF_TYPE_ID_VALID(type_id) ((type_id) <= BTF_MAX_TYPE)
186 #define BTF_STR_OFFSET_VALID(name_off) ((name_off) <= BTF_MAX_NAME_OFFSET)
188 /* 16MB for 64k structs and each has 16 members and
189 * a few MB spaces for the string section.
190 * The hard limit is S32_MAX.
192 #define BTF_MAX_SIZE (16 * 1024 * 1024)
194 #define for_each_member_from(i, from, struct_type, member) \
195 for (i = from, member = btf_type_member(struct_type) + from; \
196 i < btf_type_vlen(struct_type); \
199 #define for_each_vsi_from(i, from, struct_type, member) \
200 for (i = from, member = btf_type_var_secinfo(struct_type) + from; \
201 i < btf_type_vlen(struct_type); \
205 DEFINE_SPINLOCK(btf_idr_lock
);
207 enum btf_kfunc_hook
{
208 BTF_KFUNC_HOOK_COMMON
,
211 BTF_KFUNC_HOOK_STRUCT_OPS
,
212 BTF_KFUNC_HOOK_TRACING
,
213 BTF_KFUNC_HOOK_SYSCALL
,
214 BTF_KFUNC_HOOK_FMODRET
,
215 BTF_KFUNC_HOOK_CGROUP
,
216 BTF_KFUNC_HOOK_SCHED_ACT
,
217 BTF_KFUNC_HOOK_SK_SKB
,
218 BTF_KFUNC_HOOK_SOCKET_FILTER
,
220 BTF_KFUNC_HOOK_NETFILTER
,
221 BTF_KFUNC_HOOK_KPROBE
,
226 BTF_KFUNC_SET_MAX_CNT
= 256,
227 BTF_DTOR_KFUNC_MAX_CNT
= 256,
228 BTF_KFUNC_FILTER_MAX_CNT
= 16,
231 struct btf_kfunc_hook_filter
{
232 btf_kfunc_filter_t filters
[BTF_KFUNC_FILTER_MAX_CNT
];
236 struct btf_kfunc_set_tab
{
237 struct btf_id_set8
*sets
[BTF_KFUNC_HOOK_MAX
];
238 struct btf_kfunc_hook_filter hook_filters
[BTF_KFUNC_HOOK_MAX
];
241 struct btf_id_dtor_kfunc_tab
{
243 struct btf_id_dtor_kfunc dtors
[];
246 struct btf_struct_ops_tab
{
249 struct bpf_struct_ops_desc ops
[];
254 struct btf_type
**types
;
259 struct btf_header hdr
;
260 u32 nr_types
; /* includes VOID for base BTF */
266 struct btf_kfunc_set_tab
*kfunc_set_tab
;
267 struct btf_id_dtor_kfunc_tab
*dtor_kfunc_tab
;
268 struct btf_struct_metas
*struct_meta_tab
;
269 struct btf_struct_ops_tab
*struct_ops_tab
;
271 /* split BTF support */
272 struct btf
*base_btf
;
273 u32 start_id
; /* first type ID in this BTF (0 for base BTF) */
274 u32 start_str_off
; /* first string offset (0 for base BTF) */
275 char name
[MODULE_NAME_LEN
];
277 __u32
*base_id_map
; /* map from distilled base BTF -> vmlinux BTF ids */
280 enum verifier_phase
{
285 struct resolve_vertex
{
286 const struct btf_type
*t
;
298 RESOLVE_TBD
, /* To Be Determined */
299 RESOLVE_PTR
, /* Resolving for Pointer */
300 RESOLVE_STRUCT_OR_ARRAY
, /* Resolving for struct/union
305 #define MAX_RESOLVE_DEPTH 32
307 struct btf_sec_info
{
312 struct btf_verifier_env
{
315 struct resolve_vertex stack
[MAX_RESOLVE_DEPTH
];
316 struct bpf_verifier_log log
;
319 enum verifier_phase phase
;
320 enum resolve_mode resolve_mode
;
323 static const char * const btf_kind_str
[NR_BTF_KINDS
] = {
324 [BTF_KIND_UNKN
] = "UNKNOWN",
325 [BTF_KIND_INT
] = "INT",
326 [BTF_KIND_PTR
] = "PTR",
327 [BTF_KIND_ARRAY
] = "ARRAY",
328 [BTF_KIND_STRUCT
] = "STRUCT",
329 [BTF_KIND_UNION
] = "UNION",
330 [BTF_KIND_ENUM
] = "ENUM",
331 [BTF_KIND_FWD
] = "FWD",
332 [BTF_KIND_TYPEDEF
] = "TYPEDEF",
333 [BTF_KIND_VOLATILE
] = "VOLATILE",
334 [BTF_KIND_CONST
] = "CONST",
335 [BTF_KIND_RESTRICT
] = "RESTRICT",
336 [BTF_KIND_FUNC
] = "FUNC",
337 [BTF_KIND_FUNC_PROTO
] = "FUNC_PROTO",
338 [BTF_KIND_VAR
] = "VAR",
339 [BTF_KIND_DATASEC
] = "DATASEC",
340 [BTF_KIND_FLOAT
] = "FLOAT",
341 [BTF_KIND_DECL_TAG
] = "DECL_TAG",
342 [BTF_KIND_TYPE_TAG
] = "TYPE_TAG",
343 [BTF_KIND_ENUM64
] = "ENUM64",
346 const char *btf_type_str(const struct btf_type
*t
)
348 return btf_kind_str
[BTF_INFO_KIND(t
->info
)];
351 /* Chunk size we use in safe copy of data to be shown. */
352 #define BTF_SHOW_OBJ_SAFE_SIZE 32
355 * This is the maximum size of a base type value (equivalent to a
356 * 128-bit int); if we are at the end of our safe buffer and have
357 * less than 16 bytes space we can't be assured of being able
358 * to copy the next type safely, so in such cases we will initiate
361 #define BTF_SHOW_OBJ_BASE_TYPE_SIZE 16
364 #define BTF_SHOW_NAME_SIZE 80
367 * The suffix of a type that indicates it cannot alias another type when
368 * comparing BTF IDs for kfunc invocations.
370 #define NOCAST_ALIAS_SUFFIX "___init"
373 * Common data to all BTF show operations. Private show functions can add
374 * their own data to a structure containing a struct btf_show and consult it
375 * in the show callback. See btf_type_show() below.
377 * One challenge with showing nested data is we want to skip 0-valued
378 * data, but in order to figure out whether a nested object is all zeros
379 * we need to walk through it. As a result, we need to make two passes
380 * when handling structs, unions and arrays; the first path simply looks
381 * for nonzero data, while the second actually does the display. The first
382 * pass is signalled by show->state.depth_check being set, and if we
383 * encounter a non-zero value we set show->state.depth_to_show to
384 * the depth at which we encountered it. When we have completed the
385 * first pass, we will know if anything needs to be displayed if
386 * depth_to_show > depth. See btf_[struct,array]_show() for the
387 * implementation of this.
389 * Another problem is we want to ensure the data for display is safe to
390 * access. To support this, the anonymous "struct {} obj" tracks the data
391 * object and our safe copy of it. We copy portions of the data needed
392 * to the object "copy" buffer, but because its size is limited to
393 * BTF_SHOW_OBJ_COPY_LEN bytes, multiple copies may be required as we
394 * traverse larger objects for display.
396 * The various data type show functions all start with a call to
397 * btf_show_start_type() which returns a pointer to the safe copy
398 * of the data needed (or if BTF_SHOW_UNSAFE is specified, to the
399 * raw data itself). btf_show_obj_safe() is responsible for
400 * using copy_from_kernel_nofault() to update the safe data if necessary
401 * as we traverse the object's data. skbuff-like semantics are
404 * - obj.head points to the start of the toplevel object for display
405 * - obj.size is the size of the toplevel object
406 * - obj.data points to the current point in the original data at
407 * which our safe data starts. obj.data will advance as we copy
408 * portions of the data.
410 * In most cases a single copy will suffice, but larger data structures
411 * such as "struct task_struct" will require many copies. The logic in
412 * btf_show_obj_safe() handles the logic that determines if a new
413 * copy_from_kernel_nofault() is needed.
417 void *target
; /* target of show operation (seq file, buffer) */
418 __printf(2, 0) void (*showfn
)(struct btf_show
*show
, const char *fmt
, va_list args
);
419 const struct btf
*btf
;
420 /* below are used during iteration */
429 int status
; /* non-zero for error */
430 const struct btf_type
*type
;
431 const struct btf_member
*member
;
432 char name
[BTF_SHOW_NAME_SIZE
]; /* space for member name/type */
438 u8 safe
[BTF_SHOW_OBJ_SAFE_SIZE
];
442 struct btf_kind_operations
{
443 s32 (*check_meta
)(struct btf_verifier_env
*env
,
444 const struct btf_type
*t
,
446 int (*resolve
)(struct btf_verifier_env
*env
,
447 const struct resolve_vertex
*v
);
448 int (*check_member
)(struct btf_verifier_env
*env
,
449 const struct btf_type
*struct_type
,
450 const struct btf_member
*member
,
451 const struct btf_type
*member_type
);
452 int (*check_kflag_member
)(struct btf_verifier_env
*env
,
453 const struct btf_type
*struct_type
,
454 const struct btf_member
*member
,
455 const struct btf_type
*member_type
);
456 void (*log_details
)(struct btf_verifier_env
*env
,
457 const struct btf_type
*t
);
458 void (*show
)(const struct btf
*btf
, const struct btf_type
*t
,
459 u32 type_id
, void *data
, u8 bits_offsets
,
460 struct btf_show
*show
);
463 static const struct btf_kind_operations
* const kind_ops
[NR_BTF_KINDS
];
464 static struct btf_type btf_void
;
466 static int btf_resolve(struct btf_verifier_env
*env
,
467 const struct btf_type
*t
, u32 type_id
);
469 static int btf_func_check(struct btf_verifier_env
*env
,
470 const struct btf_type
*t
);
472 static bool btf_type_is_modifier(const struct btf_type
*t
)
474 /* Some of them is not strictly a C modifier
475 * but they are grouped into the same bucket
477 * A type (t) that refers to another
478 * type through t->type AND its size cannot
479 * be determined without following the t->type.
481 * ptr does not fall into this bucket
482 * because its size is always sizeof(void *).
484 switch (BTF_INFO_KIND(t
->info
)) {
485 case BTF_KIND_TYPEDEF
:
486 case BTF_KIND_VOLATILE
:
488 case BTF_KIND_RESTRICT
:
489 case BTF_KIND_TYPE_TAG
:
496 bool btf_type_is_void(const struct btf_type
*t
)
498 return t
== &btf_void
;
501 static bool btf_type_is_fwd(const struct btf_type
*t
)
503 return BTF_INFO_KIND(t
->info
) == BTF_KIND_FWD
;
506 static bool btf_type_is_datasec(const struct btf_type
*t
)
508 return BTF_INFO_KIND(t
->info
) == BTF_KIND_DATASEC
;
511 static bool btf_type_is_decl_tag(const struct btf_type
*t
)
513 return BTF_INFO_KIND(t
->info
) == BTF_KIND_DECL_TAG
;
516 static bool btf_type_nosize(const struct btf_type
*t
)
518 return btf_type_is_void(t
) || btf_type_is_fwd(t
) ||
519 btf_type_is_func(t
) || btf_type_is_func_proto(t
) ||
520 btf_type_is_decl_tag(t
);
523 static bool btf_type_nosize_or_null(const struct btf_type
*t
)
525 return !t
|| btf_type_nosize(t
);
528 static bool btf_type_is_decl_tag_target(const struct btf_type
*t
)
530 return btf_type_is_func(t
) || btf_type_is_struct(t
) ||
531 btf_type_is_var(t
) || btf_type_is_typedef(t
);
534 bool btf_is_vmlinux(const struct btf
*btf
)
536 return btf
->kernel_btf
&& !btf
->base_btf
;
539 u32
btf_nr_types(const struct btf
*btf
)
544 total
+= btf
->nr_types
;
551 s32
btf_find_by_name_kind(const struct btf
*btf
, const char *name
, u8 kind
)
553 const struct btf_type
*t
;
557 total
= btf_nr_types(btf
);
558 for (i
= 1; i
< total
; i
++) {
559 t
= btf_type_by_id(btf
, i
);
560 if (BTF_INFO_KIND(t
->info
) != kind
)
563 tname
= btf_name_by_offset(btf
, t
->name_off
);
564 if (!strcmp(tname
, name
))
571 s32
bpf_find_btf_id(const char *name
, u32 kind
, struct btf
**btf_p
)
577 btf
= bpf_get_btf_vmlinux();
583 ret
= btf_find_by_name_kind(btf
, name
, kind
);
584 /* ret is never zero, since btf_find_by_name_kind returns
585 * positive btf_id or negative error.
593 /* If name is not found in vmlinux's BTF then search in module's BTFs */
594 spin_lock_bh(&btf_idr_lock
);
595 idr_for_each_entry(&btf_idr
, btf
, id
) {
596 if (!btf_is_module(btf
))
598 /* linear search could be slow hence unlock/lock
599 * the IDR to avoiding holding it for too long
602 spin_unlock_bh(&btf_idr_lock
);
603 ret
= btf_find_by_name_kind(btf
, name
, kind
);
609 spin_lock_bh(&btf_idr_lock
);
611 spin_unlock_bh(&btf_idr_lock
);
615 const struct btf_type
*btf_type_skip_modifiers(const struct btf
*btf
,
618 const struct btf_type
*t
= btf_type_by_id(btf
, id
);
620 while (btf_type_is_modifier(t
)) {
622 t
= btf_type_by_id(btf
, t
->type
);
631 const struct btf_type
*btf_type_resolve_ptr(const struct btf
*btf
,
634 const struct btf_type
*t
;
636 t
= btf_type_skip_modifiers(btf
, id
, NULL
);
637 if (!btf_type_is_ptr(t
))
640 return btf_type_skip_modifiers(btf
, t
->type
, res_id
);
643 const struct btf_type
*btf_type_resolve_func_ptr(const struct btf
*btf
,
646 const struct btf_type
*ptype
;
648 ptype
= btf_type_resolve_ptr(btf
, id
, res_id
);
649 if (ptype
&& btf_type_is_func_proto(ptype
))
655 /* Types that act only as a source, not sink or intermediate
656 * type when resolving.
658 static bool btf_type_is_resolve_source_only(const struct btf_type
*t
)
660 return btf_type_is_var(t
) ||
661 btf_type_is_decl_tag(t
) ||
662 btf_type_is_datasec(t
);
665 /* What types need to be resolved?
667 * btf_type_is_modifier() is an obvious one.
669 * btf_type_is_struct() because its member refers to
670 * another type (through member->type).
672 * btf_type_is_var() because the variable refers to
673 * another type. btf_type_is_datasec() holds multiple
674 * btf_type_is_var() types that need resolving.
676 * btf_type_is_array() because its element (array->type)
677 * refers to another type. Array can be thought of a
678 * special case of struct while array just has the same
679 * member-type repeated by array->nelems of times.
681 static bool btf_type_needs_resolve(const struct btf_type
*t
)
683 return btf_type_is_modifier(t
) ||
684 btf_type_is_ptr(t
) ||
685 btf_type_is_struct(t
) ||
686 btf_type_is_array(t
) ||
687 btf_type_is_var(t
) ||
688 btf_type_is_func(t
) ||
689 btf_type_is_decl_tag(t
) ||
690 btf_type_is_datasec(t
);
693 /* t->size can be used */
694 static bool btf_type_has_size(const struct btf_type
*t
)
696 switch (BTF_INFO_KIND(t
->info
)) {
698 case BTF_KIND_STRUCT
:
701 case BTF_KIND_DATASEC
:
703 case BTF_KIND_ENUM64
:
710 static const char *btf_int_encoding_str(u8 encoding
)
714 else if (encoding
== BTF_INT_SIGNED
)
716 else if (encoding
== BTF_INT_CHAR
)
718 else if (encoding
== BTF_INT_BOOL
)
724 static u32
btf_type_int(const struct btf_type
*t
)
726 return *(u32
*)(t
+ 1);
729 static const struct btf_array
*btf_type_array(const struct btf_type
*t
)
731 return (const struct btf_array
*)(t
+ 1);
734 static const struct btf_enum
*btf_type_enum(const struct btf_type
*t
)
736 return (const struct btf_enum
*)(t
+ 1);
739 static const struct btf_var
*btf_type_var(const struct btf_type
*t
)
741 return (const struct btf_var
*)(t
+ 1);
744 static const struct btf_decl_tag
*btf_type_decl_tag(const struct btf_type
*t
)
746 return (const struct btf_decl_tag
*)(t
+ 1);
749 static const struct btf_enum64
*btf_type_enum64(const struct btf_type
*t
)
751 return (const struct btf_enum64
*)(t
+ 1);
754 static const struct btf_kind_operations
*btf_type_ops(const struct btf_type
*t
)
756 return kind_ops
[BTF_INFO_KIND(t
->info
)];
759 static bool btf_name_offset_valid(const struct btf
*btf
, u32 offset
)
761 if (!BTF_STR_OFFSET_VALID(offset
))
764 while (offset
< btf
->start_str_off
)
767 offset
-= btf
->start_str_off
;
768 return offset
< btf
->hdr
.str_len
;
771 static bool __btf_name_char_ok(char c
, bool first
)
773 if ((first
? !isalpha(c
) :
781 const char *btf_str_by_offset(const struct btf
*btf
, u32 offset
)
783 while (offset
< btf
->start_str_off
)
786 offset
-= btf
->start_str_off
;
787 if (offset
< btf
->hdr
.str_len
)
788 return &btf
->strings
[offset
];
793 static bool btf_name_valid_identifier(const struct btf
*btf
, u32 offset
)
795 /* offset must be valid */
796 const char *src
= btf_str_by_offset(btf
, offset
);
797 const char *src_limit
;
799 if (!__btf_name_char_ok(*src
, true))
802 /* set a limit on identifier length */
803 src_limit
= src
+ KSYM_NAME_LEN
;
805 while (*src
&& src
< src_limit
) {
806 if (!__btf_name_char_ok(*src
, false))
814 /* Allow any printable character in DATASEC names */
815 static bool btf_name_valid_section(const struct btf
*btf
, u32 offset
)
817 /* offset must be valid */
818 const char *src
= btf_str_by_offset(btf
, offset
);
819 const char *src_limit
;
824 /* set a limit on identifier length */
825 src_limit
= src
+ KSYM_NAME_LEN
;
826 while (*src
&& src
< src_limit
) {
835 static const char *__btf_name_by_offset(const struct btf
*btf
, u32 offset
)
842 name
= btf_str_by_offset(btf
, offset
);
843 return name
?: "(invalid-name-offset)";
846 const char *btf_name_by_offset(const struct btf
*btf
, u32 offset
)
848 return btf_str_by_offset(btf
, offset
);
851 const struct btf_type
*btf_type_by_id(const struct btf
*btf
, u32 type_id
)
853 while (type_id
< btf
->start_id
)
856 type_id
-= btf
->start_id
;
857 if (type_id
>= btf
->nr_types
)
859 return btf
->types
[type_id
];
861 EXPORT_SYMBOL_GPL(btf_type_by_id
);
864 * Regular int is not a bit field and it must be either
865 * u8/u16/u32/u64 or __int128.
867 static bool btf_type_int_is_regular(const struct btf_type
*t
)
869 u8 nr_bits
, nr_bytes
;
872 int_data
= btf_type_int(t
);
873 nr_bits
= BTF_INT_BITS(int_data
);
874 nr_bytes
= BITS_ROUNDUP_BYTES(nr_bits
);
875 if (BITS_PER_BYTE_MASKED(nr_bits
) ||
876 BTF_INT_OFFSET(int_data
) ||
877 (nr_bytes
!= sizeof(u8
) && nr_bytes
!= sizeof(u16
) &&
878 nr_bytes
!= sizeof(u32
) && nr_bytes
!= sizeof(u64
) &&
879 nr_bytes
!= (2 * sizeof(u64
)))) {
887 * Check that given struct member is a regular int with expected
890 bool btf_member_is_reg_int(const struct btf
*btf
, const struct btf_type
*s
,
891 const struct btf_member
*m
,
892 u32 expected_offset
, u32 expected_size
)
894 const struct btf_type
*t
;
899 t
= btf_type_id_size(btf
, &id
, NULL
);
900 if (!t
|| !btf_type_is_int(t
))
903 int_data
= btf_type_int(t
);
904 nr_bits
= BTF_INT_BITS(int_data
);
905 if (btf_type_kflag(s
)) {
906 u32 bitfield_size
= BTF_MEMBER_BITFIELD_SIZE(m
->offset
);
907 u32 bit_offset
= BTF_MEMBER_BIT_OFFSET(m
->offset
);
909 /* if kflag set, int should be a regular int and
910 * bit offset should be at byte boundary.
912 return !bitfield_size
&&
913 BITS_ROUNDUP_BYTES(bit_offset
) == expected_offset
&&
914 BITS_ROUNDUP_BYTES(nr_bits
) == expected_size
;
917 if (BTF_INT_OFFSET(int_data
) ||
918 BITS_PER_BYTE_MASKED(m
->offset
) ||
919 BITS_ROUNDUP_BYTES(m
->offset
) != expected_offset
||
920 BITS_PER_BYTE_MASKED(nr_bits
) ||
921 BITS_ROUNDUP_BYTES(nr_bits
) != expected_size
)
927 /* Similar to btf_type_skip_modifiers() but does not skip typedefs. */
928 static const struct btf_type
*btf_type_skip_qualifiers(const struct btf
*btf
,
931 const struct btf_type
*t
= btf_type_by_id(btf
, id
);
933 while (btf_type_is_modifier(t
) &&
934 BTF_INFO_KIND(t
->info
) != BTF_KIND_TYPEDEF
) {
935 t
= btf_type_by_id(btf
, t
->type
);
941 #define BTF_SHOW_MAX_ITER 10
943 #define BTF_KIND_BIT(kind) (1ULL << kind)
946 * Populate show->state.name with type name information.
947 * Format of type name is
949 * [.member_name = ] (type_name)
951 static const char *btf_show_name(struct btf_show
*show
)
953 /* BTF_MAX_ITER array suffixes "[]" */
954 const char *array_suffixes
= "[][][][][][][][][][]";
955 const char *array_suffix
= &array_suffixes
[strlen(array_suffixes
)];
956 /* BTF_MAX_ITER pointer suffixes "*" */
957 const char *ptr_suffixes
= "**********";
958 const char *ptr_suffix
= &ptr_suffixes
[strlen(ptr_suffixes
)];
959 const char *name
= NULL
, *prefix
= "", *parens
= "";
960 const struct btf_member
*m
= show
->state
.member
;
961 const struct btf_type
*t
;
962 const struct btf_array
*array
;
963 u32 id
= show
->state
.type_id
;
964 const char *member
= NULL
;
965 bool show_member
= false;
969 show
->state
.name
[0] = '\0';
972 * Don't show type name if we're showing an array member;
973 * in that case we show the array type so don't need to repeat
974 * ourselves for each member.
976 if (show
->state
.array_member
)
979 /* Retrieve member name, if any. */
981 member
= btf_name_by_offset(show
->btf
, m
->name_off
);
982 show_member
= strlen(member
) > 0;
987 * Start with type_id, as we have resolved the struct btf_type *
988 * via btf_modifier_show() past the parent typedef to the child
989 * struct, int etc it is defined as. In such cases, the type_id
990 * still represents the starting type while the struct btf_type *
991 * in our show->state points at the resolved type of the typedef.
993 t
= btf_type_by_id(show
->btf
, id
);
998 * The goal here is to build up the right number of pointer and
999 * array suffixes while ensuring the type name for a typedef
1000 * is represented. Along the way we accumulate a list of
1001 * BTF kinds we have encountered, since these will inform later
1002 * display; for example, pointer types will not require an
1003 * opening "{" for struct, we will just display the pointer value.
1005 * We also want to accumulate the right number of pointer or array
1006 * indices in the format string while iterating until we get to
1007 * the typedef/pointee/array member target type.
1009 * We start by pointing at the end of pointer and array suffix
1010 * strings; as we accumulate pointers and arrays we move the pointer
1011 * or array string backwards so it will show the expected number of
1012 * '*' or '[]' for the type. BTF_SHOW_MAX_ITER of nesting of pointers
1013 * and/or arrays and typedefs are supported as a precaution.
1015 * We also want to get typedef name while proceeding to resolve
1016 * type it points to so that we can add parentheses if it is a
1017 * "typedef struct" etc.
1019 for (i
= 0; i
< BTF_SHOW_MAX_ITER
; i
++) {
1021 switch (BTF_INFO_KIND(t
->info
)) {
1022 case BTF_KIND_TYPEDEF
:
1024 name
= btf_name_by_offset(show
->btf
,
1026 kinds
|= BTF_KIND_BIT(BTF_KIND_TYPEDEF
);
1029 case BTF_KIND_ARRAY
:
1030 kinds
|= BTF_KIND_BIT(BTF_KIND_ARRAY
);
1034 array
= btf_type_array(t
);
1035 if (array_suffix
> array_suffixes
)
1040 kinds
|= BTF_KIND_BIT(BTF_KIND_PTR
);
1041 if (ptr_suffix
> ptr_suffixes
)
1051 t
= btf_type_skip_qualifiers(show
->btf
, id
);
1053 /* We may not be able to represent this type; bail to be safe */
1054 if (i
== BTF_SHOW_MAX_ITER
)
1058 name
= btf_name_by_offset(show
->btf
, t
->name_off
);
1060 switch (BTF_INFO_KIND(t
->info
)) {
1061 case BTF_KIND_STRUCT
:
1062 case BTF_KIND_UNION
:
1063 prefix
= BTF_INFO_KIND(t
->info
) == BTF_KIND_STRUCT
?
1065 /* if it's an array of struct/union, parens is already set */
1066 if (!(kinds
& (BTF_KIND_BIT(BTF_KIND_ARRAY
))))
1070 case BTF_KIND_ENUM64
:
1077 /* pointer does not require parens */
1078 if (kinds
& BTF_KIND_BIT(BTF_KIND_PTR
))
1080 /* typedef does not require struct/union/enum prefix */
1081 if (kinds
& BTF_KIND_BIT(BTF_KIND_TYPEDEF
))
1087 /* Even if we don't want type name info, we want parentheses etc */
1088 if (show
->flags
& BTF_SHOW_NONAME
)
1089 snprintf(show
->state
.name
, sizeof(show
->state
.name
), "%s",
1092 snprintf(show
->state
.name
, sizeof(show
->state
.name
),
1093 "%s%s%s(%s%s%s%s%s%s)%s",
1094 /* first 3 strings comprise ".member = " */
1095 show_member
? "." : "",
1096 show_member
? member
: "",
1097 show_member
? " = " : "",
1098 /* ...next is our prefix (struct, enum, etc) */
1100 strlen(prefix
) > 0 && strlen(name
) > 0 ? " " : "",
1101 /* ...this is the type name itself */
1103 /* ...suffixed by the appropriate '*', '[]' suffixes */
1104 strlen(ptr_suffix
) > 0 ? " " : "", ptr_suffix
,
1105 array_suffix
, parens
);
1107 return show
->state
.name
;
1110 static const char *__btf_show_indent(struct btf_show
*show
)
1112 const char *indents
= " ";
1113 const char *indent
= &indents
[strlen(indents
)];
1115 if ((indent
- show
->state
.depth
) >= indents
)
1116 return indent
- show
->state
.depth
;
1120 static const char *btf_show_indent(struct btf_show
*show
)
1122 return show
->flags
& BTF_SHOW_COMPACT
? "" : __btf_show_indent(show
);
1125 static const char *btf_show_newline(struct btf_show
*show
)
1127 return show
->flags
& BTF_SHOW_COMPACT
? "" : "\n";
1130 static const char *btf_show_delim(struct btf_show
*show
)
1132 if (show
->state
.depth
== 0)
1135 if ((show
->flags
& BTF_SHOW_COMPACT
) && show
->state
.type
&&
1136 BTF_INFO_KIND(show
->state
.type
->info
) == BTF_KIND_UNION
)
1142 __printf(2, 3) static void btf_show(struct btf_show
*show
, const char *fmt
, ...)
1146 if (!show
->state
.depth_check
) {
1147 va_start(args
, fmt
);
1148 show
->showfn(show
, fmt
, args
);
1153 /* Macros are used here as btf_show_type_value[s]() prepends and appends
1154 * format specifiers to the format specifier passed in; these do the work of
1155 * adding indentation, delimiters etc while the caller simply has to specify
1156 * the type value(s) in the format specifier + value(s).
1158 #define btf_show_type_value(show, fmt, value) \
1160 if ((value) != (__typeof__(value))0 || \
1161 (show->flags & BTF_SHOW_ZERO) || \
1162 show->state.depth == 0) { \
1163 btf_show(show, "%s%s" fmt "%s%s", \
1164 btf_show_indent(show), \
1165 btf_show_name(show), \
1166 value, btf_show_delim(show), \
1167 btf_show_newline(show)); \
1168 if (show->state.depth > show->state.depth_to_show) \
1169 show->state.depth_to_show = show->state.depth; \
1173 #define btf_show_type_values(show, fmt, ...) \
1175 btf_show(show, "%s%s" fmt "%s%s", btf_show_indent(show), \
1176 btf_show_name(show), \
1177 __VA_ARGS__, btf_show_delim(show), \
1178 btf_show_newline(show)); \
1179 if (show->state.depth > show->state.depth_to_show) \
1180 show->state.depth_to_show = show->state.depth; \
1183 /* How much is left to copy to safe buffer after @data? */
1184 static int btf_show_obj_size_left(struct btf_show
*show
, void *data
)
1186 return show
->obj
.head
+ show
->obj
.size
- data
;
1189 /* Is object pointed to by @data of @size already copied to our safe buffer? */
1190 static bool btf_show_obj_is_safe(struct btf_show
*show
, void *data
, int size
)
1192 return data
>= show
->obj
.data
&&
1193 (data
+ size
) < (show
->obj
.data
+ BTF_SHOW_OBJ_SAFE_SIZE
);
1197 * If object pointed to by @data of @size falls within our safe buffer, return
1198 * the equivalent pointer to the same safe data. Assumes
1199 * copy_from_kernel_nofault() has already happened and our safe buffer is
1202 static void *__btf_show_obj_safe(struct btf_show
*show
, void *data
, int size
)
1204 if (btf_show_obj_is_safe(show
, data
, size
))
1205 return show
->obj
.safe
+ (data
- show
->obj
.data
);
1210 * Return a safe-to-access version of data pointed to by @data.
1211 * We do this by copying the relevant amount of information
1212 * to the struct btf_show obj.safe buffer using copy_from_kernel_nofault().
1214 * If BTF_SHOW_UNSAFE is specified, just return data as-is; no
1215 * safe copy is needed.
1217 * Otherwise we need to determine if we have the required amount
1218 * of data (determined by the @data pointer and the size of the
1219 * largest base type we can encounter (represented by
1220 * BTF_SHOW_OBJ_BASE_TYPE_SIZE). Having that much data ensures
1221 * that we will be able to print some of the current object,
1222 * and if more is needed a copy will be triggered.
1223 * Some objects such as structs will not fit into the buffer;
1224 * in such cases additional copies when we iterate over their
1225 * members may be needed.
1227 * btf_show_obj_safe() is used to return a safe buffer for
1228 * btf_show_start_type(); this ensures that as we recurse into
1229 * nested types we always have safe data for the given type.
1230 * This approach is somewhat wasteful; it's possible for example
1231 * that when iterating over a large union we'll end up copying the
1232 * same data repeatedly, but the goal is safety not performance.
1233 * We use stack data as opposed to per-CPU buffers because the
1234 * iteration over a type can take some time, and preemption handling
1235 * would greatly complicate use of the safe buffer.
1237 static void *btf_show_obj_safe(struct btf_show
*show
,
1238 const struct btf_type
*t
,
1241 const struct btf_type
*rt
;
1242 int size_left
, size
;
1245 if (show
->flags
& BTF_SHOW_UNSAFE
)
1248 rt
= btf_resolve_size(show
->btf
, t
, &size
);
1250 show
->state
.status
= PTR_ERR(rt
);
1255 * Is this toplevel object? If so, set total object size and
1256 * initialize pointers. Otherwise check if we still fall within
1257 * our safe object data.
1259 if (show
->state
.depth
== 0) {
1260 show
->obj
.size
= size
;
1261 show
->obj
.head
= data
;
1264 * If the size of the current object is > our remaining
1265 * safe buffer we _may_ need to do a new copy. However
1266 * consider the case of a nested struct; it's size pushes
1267 * us over the safe buffer limit, but showing any individual
1268 * struct members does not. In such cases, we don't need
1269 * to initiate a fresh copy yet; however we definitely need
1270 * at least BTF_SHOW_OBJ_BASE_TYPE_SIZE bytes left
1271 * in our buffer, regardless of the current object size.
1272 * The logic here is that as we resolve types we will
1273 * hit a base type at some point, and we need to be sure
1274 * the next chunk of data is safely available to display
1275 * that type info safely. We cannot rely on the size of
1276 * the current object here because it may be much larger
1277 * than our current buffer (e.g. task_struct is 8k).
1278 * All we want to do here is ensure that we can print the
1279 * next basic type, which we can if either
1280 * - the current type size is within the safe buffer; or
1281 * - at least BTF_SHOW_OBJ_BASE_TYPE_SIZE bytes are left in
1284 safe
= __btf_show_obj_safe(show
, data
,
1286 BTF_SHOW_OBJ_BASE_TYPE_SIZE
));
1290 * We need a new copy to our safe object, either because we haven't
1291 * yet copied and are initializing safe data, or because the data
1292 * we want falls outside the boundaries of the safe object.
1295 size_left
= btf_show_obj_size_left(show
, data
);
1296 if (size_left
> BTF_SHOW_OBJ_SAFE_SIZE
)
1297 size_left
= BTF_SHOW_OBJ_SAFE_SIZE
;
1298 show
->state
.status
= copy_from_kernel_nofault(show
->obj
.safe
,
1300 if (!show
->state
.status
) {
1301 show
->obj
.data
= data
;
1302 safe
= show
->obj
.safe
;
1310 * Set the type we are starting to show and return a safe data pointer
1311 * to be used for showing the associated data.
1313 static void *btf_show_start_type(struct btf_show
*show
,
1314 const struct btf_type
*t
,
1315 u32 type_id
, void *data
)
1317 show
->state
.type
= t
;
1318 show
->state
.type_id
= type_id
;
1319 show
->state
.name
[0] = '\0';
1321 return btf_show_obj_safe(show
, t
, data
);
1324 static void btf_show_end_type(struct btf_show
*show
)
1326 show
->state
.type
= NULL
;
1327 show
->state
.type_id
= 0;
1328 show
->state
.name
[0] = '\0';
1331 static void *btf_show_start_aggr_type(struct btf_show
*show
,
1332 const struct btf_type
*t
,
1333 u32 type_id
, void *data
)
1335 void *safe_data
= btf_show_start_type(show
, t
, type_id
, data
);
1340 btf_show(show
, "%s%s%s", btf_show_indent(show
),
1341 btf_show_name(show
),
1342 btf_show_newline(show
));
1343 show
->state
.depth
++;
1347 static void btf_show_end_aggr_type(struct btf_show
*show
,
1350 show
->state
.depth
--;
1351 btf_show(show
, "%s%s%s%s", btf_show_indent(show
), suffix
,
1352 btf_show_delim(show
), btf_show_newline(show
));
1353 btf_show_end_type(show
);
1356 static void btf_show_start_member(struct btf_show
*show
,
1357 const struct btf_member
*m
)
1359 show
->state
.member
= m
;
1362 static void btf_show_start_array_member(struct btf_show
*show
)
1364 show
->state
.array_member
= 1;
1365 btf_show_start_member(show
, NULL
);
1368 static void btf_show_end_member(struct btf_show
*show
)
1370 show
->state
.member
= NULL
;
1373 static void btf_show_end_array_member(struct btf_show
*show
)
1375 show
->state
.array_member
= 0;
1376 btf_show_end_member(show
);
1379 static void *btf_show_start_array_type(struct btf_show
*show
,
1380 const struct btf_type
*t
,
1385 show
->state
.array_encoding
= array_encoding
;
1386 show
->state
.array_terminated
= 0;
1387 return btf_show_start_aggr_type(show
, t
, type_id
, data
);
1390 static void btf_show_end_array_type(struct btf_show
*show
)
1392 show
->state
.array_encoding
= 0;
1393 show
->state
.array_terminated
= 0;
1394 btf_show_end_aggr_type(show
, "]");
1397 static void *btf_show_start_struct_type(struct btf_show
*show
,
1398 const struct btf_type
*t
,
1402 return btf_show_start_aggr_type(show
, t
, type_id
, data
);
1405 static void btf_show_end_struct_type(struct btf_show
*show
)
1407 btf_show_end_aggr_type(show
, "}");
1410 __printf(2, 3) static void __btf_verifier_log(struct bpf_verifier_log
*log
,
1411 const char *fmt
, ...)
1415 va_start(args
, fmt
);
1416 bpf_verifier_vlog(log
, fmt
, args
);
1420 __printf(2, 3) static void btf_verifier_log(struct btf_verifier_env
*env
,
1421 const char *fmt
, ...)
1423 struct bpf_verifier_log
*log
= &env
->log
;
1426 if (!bpf_verifier_log_needed(log
))
1429 va_start(args
, fmt
);
1430 bpf_verifier_vlog(log
, fmt
, args
);
1434 __printf(4, 5) static void __btf_verifier_log_type(struct btf_verifier_env
*env
,
1435 const struct btf_type
*t
,
1437 const char *fmt
, ...)
1439 struct bpf_verifier_log
*log
= &env
->log
;
1440 struct btf
*btf
= env
->btf
;
1443 if (!bpf_verifier_log_needed(log
))
1446 if (log
->level
== BPF_LOG_KERNEL
) {
1447 /* btf verifier prints all types it is processing via
1448 * btf_verifier_log_type(..., fmt = NULL).
1449 * Skip those prints for in-kernel BTF verification.
1454 /* Skip logging when loading module BTF with mismatches permitted */
1455 if (env
->btf
->base_btf
&& IS_ENABLED(CONFIG_MODULE_ALLOW_BTF_MISMATCH
))
1459 __btf_verifier_log(log
, "[%u] %s %s%s",
1462 __btf_name_by_offset(btf
, t
->name_off
),
1463 log_details
? " " : "");
1466 btf_type_ops(t
)->log_details(env
, t
);
1469 __btf_verifier_log(log
, " ");
1470 va_start(args
, fmt
);
1471 bpf_verifier_vlog(log
, fmt
, args
);
1475 __btf_verifier_log(log
, "\n");
1478 #define btf_verifier_log_type(env, t, ...) \
1479 __btf_verifier_log_type((env), (t), true, __VA_ARGS__)
1480 #define btf_verifier_log_basic(env, t, ...) \
1481 __btf_verifier_log_type((env), (t), false, __VA_ARGS__)
1484 static void btf_verifier_log_member(struct btf_verifier_env
*env
,
1485 const struct btf_type
*struct_type
,
1486 const struct btf_member
*member
,
1487 const char *fmt
, ...)
1489 struct bpf_verifier_log
*log
= &env
->log
;
1490 struct btf
*btf
= env
->btf
;
1493 if (!bpf_verifier_log_needed(log
))
1496 if (log
->level
== BPF_LOG_KERNEL
) {
1500 /* Skip logging when loading module BTF with mismatches permitted */
1501 if (env
->btf
->base_btf
&& IS_ENABLED(CONFIG_MODULE_ALLOW_BTF_MISMATCH
))
1505 /* The CHECK_META phase already did a btf dump.
1507 * If member is logged again, it must hit an error in
1508 * parsing this member. It is useful to print out which
1509 * struct this member belongs to.
1511 if (env
->phase
!= CHECK_META
)
1512 btf_verifier_log_type(env
, struct_type
, NULL
);
1514 if (btf_type_kflag(struct_type
))
1515 __btf_verifier_log(log
,
1516 "\t%s type_id=%u bitfield_size=%u bits_offset=%u",
1517 __btf_name_by_offset(btf
, member
->name_off
),
1519 BTF_MEMBER_BITFIELD_SIZE(member
->offset
),
1520 BTF_MEMBER_BIT_OFFSET(member
->offset
));
1522 __btf_verifier_log(log
, "\t%s type_id=%u bits_offset=%u",
1523 __btf_name_by_offset(btf
, member
->name_off
),
1524 member
->type
, member
->offset
);
1527 __btf_verifier_log(log
, " ");
1528 va_start(args
, fmt
);
1529 bpf_verifier_vlog(log
, fmt
, args
);
1533 __btf_verifier_log(log
, "\n");
1537 static void btf_verifier_log_vsi(struct btf_verifier_env
*env
,
1538 const struct btf_type
*datasec_type
,
1539 const struct btf_var_secinfo
*vsi
,
1540 const char *fmt
, ...)
1542 struct bpf_verifier_log
*log
= &env
->log
;
1545 if (!bpf_verifier_log_needed(log
))
1547 if (log
->level
== BPF_LOG_KERNEL
&& !fmt
)
1549 if (env
->phase
!= CHECK_META
)
1550 btf_verifier_log_type(env
, datasec_type
, NULL
);
1552 __btf_verifier_log(log
, "\t type_id=%u offset=%u size=%u",
1553 vsi
->type
, vsi
->offset
, vsi
->size
);
1555 __btf_verifier_log(log
, " ");
1556 va_start(args
, fmt
);
1557 bpf_verifier_vlog(log
, fmt
, args
);
1561 __btf_verifier_log(log
, "\n");
1564 static void btf_verifier_log_hdr(struct btf_verifier_env
*env
,
1567 struct bpf_verifier_log
*log
= &env
->log
;
1568 const struct btf
*btf
= env
->btf
;
1569 const struct btf_header
*hdr
;
1571 if (!bpf_verifier_log_needed(log
))
1574 if (log
->level
== BPF_LOG_KERNEL
)
1577 __btf_verifier_log(log
, "magic: 0x%x\n", hdr
->magic
);
1578 __btf_verifier_log(log
, "version: %u\n", hdr
->version
);
1579 __btf_verifier_log(log
, "flags: 0x%x\n", hdr
->flags
);
1580 __btf_verifier_log(log
, "hdr_len: %u\n", hdr
->hdr_len
);
1581 __btf_verifier_log(log
, "type_off: %u\n", hdr
->type_off
);
1582 __btf_verifier_log(log
, "type_len: %u\n", hdr
->type_len
);
1583 __btf_verifier_log(log
, "str_off: %u\n", hdr
->str_off
);
1584 __btf_verifier_log(log
, "str_len: %u\n", hdr
->str_len
);
1585 __btf_verifier_log(log
, "btf_total_size: %u\n", btf_data_size
);
1588 static int btf_add_type(struct btf_verifier_env
*env
, struct btf_type
*t
)
1590 struct btf
*btf
= env
->btf
;
1592 if (btf
->types_size
== btf
->nr_types
) {
1593 /* Expand 'types' array */
1595 struct btf_type
**new_types
;
1596 u32 expand_by
, new_size
;
1598 if (btf
->start_id
+ btf
->types_size
== BTF_MAX_TYPE
) {
1599 btf_verifier_log(env
, "Exceeded max num of types");
1603 expand_by
= max_t(u32
, btf
->types_size
>> 2, 16);
1604 new_size
= min_t(u32
, BTF_MAX_TYPE
,
1605 btf
->types_size
+ expand_by
);
1607 new_types
= kvcalloc(new_size
, sizeof(*new_types
),
1608 GFP_KERNEL
| __GFP_NOWARN
);
1612 if (btf
->nr_types
== 0) {
1613 if (!btf
->base_btf
) {
1614 /* lazily init VOID type */
1615 new_types
[0] = &btf_void
;
1619 memcpy(new_types
, btf
->types
,
1620 sizeof(*btf
->types
) * btf
->nr_types
);
1624 btf
->types
= new_types
;
1625 btf
->types_size
= new_size
;
1628 btf
->types
[btf
->nr_types
++] = t
;
1633 static int btf_alloc_id(struct btf
*btf
)
1637 idr_preload(GFP_KERNEL
);
1638 spin_lock_bh(&btf_idr_lock
);
1639 id
= idr_alloc_cyclic(&btf_idr
, btf
, 1, INT_MAX
, GFP_ATOMIC
);
1642 spin_unlock_bh(&btf_idr_lock
);
1645 if (WARN_ON_ONCE(!id
))
1648 return id
> 0 ? 0 : id
;
1651 static void btf_free_id(struct btf
*btf
)
1653 unsigned long flags
;
1656 * In map-in-map, calling map_delete_elem() on outer
1657 * map will call bpf_map_put on the inner map.
1658 * It will then eventually call btf_free_id()
1659 * on the inner map. Some of the map_delete_elem()
1660 * implementation may have irq disabled, so
1661 * we need to use the _irqsave() version instead
1662 * of the _bh() version.
1664 spin_lock_irqsave(&btf_idr_lock
, flags
);
1665 idr_remove(&btf_idr
, btf
->id
);
1666 spin_unlock_irqrestore(&btf_idr_lock
, flags
);
1669 static void btf_free_kfunc_set_tab(struct btf
*btf
)
1671 struct btf_kfunc_set_tab
*tab
= btf
->kfunc_set_tab
;
1676 for (hook
= 0; hook
< ARRAY_SIZE(tab
->sets
); hook
++)
1677 kfree(tab
->sets
[hook
]);
1679 btf
->kfunc_set_tab
= NULL
;
1682 static void btf_free_dtor_kfunc_tab(struct btf
*btf
)
1684 struct btf_id_dtor_kfunc_tab
*tab
= btf
->dtor_kfunc_tab
;
1689 btf
->dtor_kfunc_tab
= NULL
;
1692 static void btf_struct_metas_free(struct btf_struct_metas
*tab
)
1698 for (i
= 0; i
< tab
->cnt
; i
++)
1699 btf_record_free(tab
->types
[i
].record
);
1703 static void btf_free_struct_meta_tab(struct btf
*btf
)
1705 struct btf_struct_metas
*tab
= btf
->struct_meta_tab
;
1707 btf_struct_metas_free(tab
);
1708 btf
->struct_meta_tab
= NULL
;
1711 static void btf_free_struct_ops_tab(struct btf
*btf
)
1713 struct btf_struct_ops_tab
*tab
= btf
->struct_ops_tab
;
1719 for (i
= 0; i
< tab
->cnt
; i
++)
1720 bpf_struct_ops_desc_release(&tab
->ops
[i
]);
1723 btf
->struct_ops_tab
= NULL
;
1726 static void btf_free(struct btf
*btf
)
1728 btf_free_struct_meta_tab(btf
);
1729 btf_free_dtor_kfunc_tab(btf
);
1730 btf_free_kfunc_set_tab(btf
);
1731 btf_free_struct_ops_tab(btf
);
1733 kvfree(btf
->resolved_sizes
);
1734 kvfree(btf
->resolved_ids
);
1735 /* vmlinux does not allocate btf->data, it simply points it at
1738 if (!btf_is_vmlinux(btf
))
1740 kvfree(btf
->base_id_map
);
1744 static void btf_free_rcu(struct rcu_head
*rcu
)
1746 struct btf
*btf
= container_of(rcu
, struct btf
, rcu
);
1751 const char *btf_get_name(const struct btf
*btf
)
1756 void btf_get(struct btf
*btf
)
1758 refcount_inc(&btf
->refcnt
);
1761 void btf_put(struct btf
*btf
)
1763 if (btf
&& refcount_dec_and_test(&btf
->refcnt
)) {
1765 call_rcu(&btf
->rcu
, btf_free_rcu
);
1769 struct btf
*btf_base_btf(const struct btf
*btf
)
1771 return btf
->base_btf
;
1774 const struct btf_header
*btf_header(const struct btf
*btf
)
1779 void btf_set_base_btf(struct btf
*btf
, const struct btf
*base_btf
)
1781 btf
->base_btf
= (struct btf
*)base_btf
;
1782 btf
->start_id
= btf_nr_types(base_btf
);
1783 btf
->start_str_off
= base_btf
->hdr
.str_len
;
1786 static int env_resolve_init(struct btf_verifier_env
*env
)
1788 struct btf
*btf
= env
->btf
;
1789 u32 nr_types
= btf
->nr_types
;
1790 u32
*resolved_sizes
= NULL
;
1791 u32
*resolved_ids
= NULL
;
1792 u8
*visit_states
= NULL
;
1794 resolved_sizes
= kvcalloc(nr_types
, sizeof(*resolved_sizes
),
1795 GFP_KERNEL
| __GFP_NOWARN
);
1796 if (!resolved_sizes
)
1799 resolved_ids
= kvcalloc(nr_types
, sizeof(*resolved_ids
),
1800 GFP_KERNEL
| __GFP_NOWARN
);
1804 visit_states
= kvcalloc(nr_types
, sizeof(*visit_states
),
1805 GFP_KERNEL
| __GFP_NOWARN
);
1809 btf
->resolved_sizes
= resolved_sizes
;
1810 btf
->resolved_ids
= resolved_ids
;
1811 env
->visit_states
= visit_states
;
1816 kvfree(resolved_sizes
);
1817 kvfree(resolved_ids
);
1818 kvfree(visit_states
);
1822 static void btf_verifier_env_free(struct btf_verifier_env
*env
)
1824 kvfree(env
->visit_states
);
1828 static bool env_type_is_resolve_sink(const struct btf_verifier_env
*env
,
1829 const struct btf_type
*next_type
)
1831 switch (env
->resolve_mode
) {
1833 /* int, enum or void is a sink */
1834 return !btf_type_needs_resolve(next_type
);
1836 /* int, enum, void, struct, array, func or func_proto is a sink
1839 return !btf_type_is_modifier(next_type
) &&
1840 !btf_type_is_ptr(next_type
);
1841 case RESOLVE_STRUCT_OR_ARRAY
:
1842 /* int, enum, void, ptr, func or func_proto is a sink
1843 * for struct and array
1845 return !btf_type_is_modifier(next_type
) &&
1846 !btf_type_is_array(next_type
) &&
1847 !btf_type_is_struct(next_type
);
1853 static bool env_type_is_resolved(const struct btf_verifier_env
*env
,
1856 /* base BTF types should be resolved by now */
1857 if (type_id
< env
->btf
->start_id
)
1860 return env
->visit_states
[type_id
- env
->btf
->start_id
] == RESOLVED
;
1863 static int env_stack_push(struct btf_verifier_env
*env
,
1864 const struct btf_type
*t
, u32 type_id
)
1866 const struct btf
*btf
= env
->btf
;
1867 struct resolve_vertex
*v
;
1869 if (env
->top_stack
== MAX_RESOLVE_DEPTH
)
1872 if (type_id
< btf
->start_id
1873 || env
->visit_states
[type_id
- btf
->start_id
] != NOT_VISITED
)
1876 env
->visit_states
[type_id
- btf
->start_id
] = VISITED
;
1878 v
= &env
->stack
[env
->top_stack
++];
1880 v
->type_id
= type_id
;
1883 if (env
->resolve_mode
== RESOLVE_TBD
) {
1884 if (btf_type_is_ptr(t
))
1885 env
->resolve_mode
= RESOLVE_PTR
;
1886 else if (btf_type_is_struct(t
) || btf_type_is_array(t
))
1887 env
->resolve_mode
= RESOLVE_STRUCT_OR_ARRAY
;
1893 static void env_stack_set_next_member(struct btf_verifier_env
*env
,
1896 env
->stack
[env
->top_stack
- 1].next_member
= next_member
;
1899 static void env_stack_pop_resolved(struct btf_verifier_env
*env
,
1900 u32 resolved_type_id
,
1903 u32 type_id
= env
->stack
[--(env
->top_stack
)].type_id
;
1904 struct btf
*btf
= env
->btf
;
1906 type_id
-= btf
->start_id
; /* adjust to local type id */
1907 btf
->resolved_sizes
[type_id
] = resolved_size
;
1908 btf
->resolved_ids
[type_id
] = resolved_type_id
;
1909 env
->visit_states
[type_id
] = RESOLVED
;
1912 static const struct resolve_vertex
*env_stack_peak(struct btf_verifier_env
*env
)
1914 return env
->top_stack
? &env
->stack
[env
->top_stack
- 1] : NULL
;
1917 /* Resolve the size of a passed-in "type"
1919 * type: is an array (e.g. u32 array[x][y])
1920 * return type: type "u32[x][y]", i.e. BTF_KIND_ARRAY,
1921 * *type_size: (x * y * sizeof(u32)). Hence, *type_size always
1922 * corresponds to the return type.
1924 * *elem_id: id of u32
1925 * *total_nelems: (x * y). Hence, individual elem size is
1926 * (*type_size / *total_nelems)
1927 * *type_id: id of type if it's changed within the function, 0 if not
1929 * type: is not an array (e.g. const struct X)
1930 * return type: type "struct X"
1931 * *type_size: sizeof(struct X)
1932 * *elem_type: same as return type ("struct X")
1935 * *type_id: id of type if it's changed within the function, 0 if not
1937 static const struct btf_type
*
1938 __btf_resolve_size(const struct btf
*btf
, const struct btf_type
*type
,
1939 u32
*type_size
, const struct btf_type
**elem_type
,
1940 u32
*elem_id
, u32
*total_nelems
, u32
*type_id
)
1942 const struct btf_type
*array_type
= NULL
;
1943 const struct btf_array
*array
= NULL
;
1944 u32 i
, size
, nelems
= 1, id
= 0;
1946 for (i
= 0; i
< MAX_RESOLVE_DEPTH
; i
++) {
1947 switch (BTF_INFO_KIND(type
->info
)) {
1948 /* type->size can be used */
1950 case BTF_KIND_STRUCT
:
1951 case BTF_KIND_UNION
:
1953 case BTF_KIND_FLOAT
:
1954 case BTF_KIND_ENUM64
:
1959 size
= sizeof(void *);
1963 case BTF_KIND_TYPEDEF
:
1964 case BTF_KIND_VOLATILE
:
1965 case BTF_KIND_CONST
:
1966 case BTF_KIND_RESTRICT
:
1967 case BTF_KIND_TYPE_TAG
:
1969 type
= btf_type_by_id(btf
, type
->type
);
1972 case BTF_KIND_ARRAY
:
1975 array
= btf_type_array(type
);
1976 if (nelems
&& array
->nelems
> U32_MAX
/ nelems
)
1977 return ERR_PTR(-EINVAL
);
1978 nelems
*= array
->nelems
;
1979 type
= btf_type_by_id(btf
, array
->type
);
1982 /* type without size */
1984 return ERR_PTR(-EINVAL
);
1988 return ERR_PTR(-EINVAL
);
1991 if (nelems
&& size
> U32_MAX
/ nelems
)
1992 return ERR_PTR(-EINVAL
);
1994 *type_size
= nelems
* size
;
1996 *total_nelems
= nelems
;
2000 *elem_id
= array
? array
->type
: 0;
2004 return array_type
? : type
;
2007 const struct btf_type
*
2008 btf_resolve_size(const struct btf
*btf
, const struct btf_type
*type
,
2011 return __btf_resolve_size(btf
, type
, type_size
, NULL
, NULL
, NULL
, NULL
);
2014 static u32
btf_resolved_type_id(const struct btf
*btf
, u32 type_id
)
2016 while (type_id
< btf
->start_id
)
2017 btf
= btf
->base_btf
;
2019 return btf
->resolved_ids
[type_id
- btf
->start_id
];
2022 /* The input param "type_id" must point to a needs_resolve type */
2023 static const struct btf_type
*btf_type_id_resolve(const struct btf
*btf
,
2026 *type_id
= btf_resolved_type_id(btf
, *type_id
);
2027 return btf_type_by_id(btf
, *type_id
);
2030 static u32
btf_resolved_type_size(const struct btf
*btf
, u32 type_id
)
2032 while (type_id
< btf
->start_id
)
2033 btf
= btf
->base_btf
;
2035 return btf
->resolved_sizes
[type_id
- btf
->start_id
];
2038 const struct btf_type
*btf_type_id_size(const struct btf
*btf
,
2039 u32
*type_id
, u32
*ret_size
)
2041 const struct btf_type
*size_type
;
2042 u32 size_type_id
= *type_id
;
2045 size_type
= btf_type_by_id(btf
, size_type_id
);
2046 if (btf_type_nosize_or_null(size_type
))
2049 if (btf_type_has_size(size_type
)) {
2050 size
= size_type
->size
;
2051 } else if (btf_type_is_array(size_type
)) {
2052 size
= btf_resolved_type_size(btf
, size_type_id
);
2053 } else if (btf_type_is_ptr(size_type
)) {
2054 size
= sizeof(void *);
2056 if (WARN_ON_ONCE(!btf_type_is_modifier(size_type
) &&
2057 !btf_type_is_var(size_type
)))
2060 size_type_id
= btf_resolved_type_id(btf
, size_type_id
);
2061 size_type
= btf_type_by_id(btf
, size_type_id
);
2062 if (btf_type_nosize_or_null(size_type
))
2064 else if (btf_type_has_size(size_type
))
2065 size
= size_type
->size
;
2066 else if (btf_type_is_array(size_type
))
2067 size
= btf_resolved_type_size(btf
, size_type_id
);
2068 else if (btf_type_is_ptr(size_type
))
2069 size
= sizeof(void *);
2074 *type_id
= size_type_id
;
2081 static int btf_df_check_member(struct btf_verifier_env
*env
,
2082 const struct btf_type
*struct_type
,
2083 const struct btf_member
*member
,
2084 const struct btf_type
*member_type
)
2086 btf_verifier_log_basic(env
, struct_type
,
2087 "Unsupported check_member");
2091 static int btf_df_check_kflag_member(struct btf_verifier_env
*env
,
2092 const struct btf_type
*struct_type
,
2093 const struct btf_member
*member
,
2094 const struct btf_type
*member_type
)
2096 btf_verifier_log_basic(env
, struct_type
,
2097 "Unsupported check_kflag_member");
2101 /* Used for ptr, array struct/union and float type members.
2102 * int, enum and modifier types have their specific callback functions.
2104 static int btf_generic_check_kflag_member(struct btf_verifier_env
*env
,
2105 const struct btf_type
*struct_type
,
2106 const struct btf_member
*member
,
2107 const struct btf_type
*member_type
)
2109 if (BTF_MEMBER_BITFIELD_SIZE(member
->offset
)) {
2110 btf_verifier_log_member(env
, struct_type
, member
,
2111 "Invalid member bitfield_size");
2115 /* bitfield size is 0, so member->offset represents bit offset only.
2116 * It is safe to call non kflag check_member variants.
2118 return btf_type_ops(member_type
)->check_member(env
, struct_type
,
2123 static int btf_df_resolve(struct btf_verifier_env
*env
,
2124 const struct resolve_vertex
*v
)
2126 btf_verifier_log_basic(env
, v
->t
, "Unsupported resolve");
2130 static void btf_df_show(const struct btf
*btf
, const struct btf_type
*t
,
2131 u32 type_id
, void *data
, u8 bits_offsets
,
2132 struct btf_show
*show
)
2134 btf_show(show
, "<unsupported kind:%u>", BTF_INFO_KIND(t
->info
));
2137 static int btf_int_check_member(struct btf_verifier_env
*env
,
2138 const struct btf_type
*struct_type
,
2139 const struct btf_member
*member
,
2140 const struct btf_type
*member_type
)
2142 u32 int_data
= btf_type_int(member_type
);
2143 u32 struct_bits_off
= member
->offset
;
2144 u32 struct_size
= struct_type
->size
;
2148 if (U32_MAX
- struct_bits_off
< BTF_INT_OFFSET(int_data
)) {
2149 btf_verifier_log_member(env
, struct_type
, member
,
2150 "bits_offset exceeds U32_MAX");
2154 struct_bits_off
+= BTF_INT_OFFSET(int_data
);
2155 bytes_offset
= BITS_ROUNDDOWN_BYTES(struct_bits_off
);
2156 nr_copy_bits
= BTF_INT_BITS(int_data
) +
2157 BITS_PER_BYTE_MASKED(struct_bits_off
);
2159 if (nr_copy_bits
> BITS_PER_U128
) {
2160 btf_verifier_log_member(env
, struct_type
, member
,
2161 "nr_copy_bits exceeds 128");
2165 if (struct_size
< bytes_offset
||
2166 struct_size
- bytes_offset
< BITS_ROUNDUP_BYTES(nr_copy_bits
)) {
2167 btf_verifier_log_member(env
, struct_type
, member
,
2168 "Member exceeds struct_size");
2175 static int btf_int_check_kflag_member(struct btf_verifier_env
*env
,
2176 const struct btf_type
*struct_type
,
2177 const struct btf_member
*member
,
2178 const struct btf_type
*member_type
)
2180 u32 struct_bits_off
, nr_bits
, nr_int_data_bits
, bytes_offset
;
2181 u32 int_data
= btf_type_int(member_type
);
2182 u32 struct_size
= struct_type
->size
;
2185 /* a regular int type is required for the kflag int member */
2186 if (!btf_type_int_is_regular(member_type
)) {
2187 btf_verifier_log_member(env
, struct_type
, member
,
2188 "Invalid member base type");
2192 /* check sanity of bitfield size */
2193 nr_bits
= BTF_MEMBER_BITFIELD_SIZE(member
->offset
);
2194 struct_bits_off
= BTF_MEMBER_BIT_OFFSET(member
->offset
);
2195 nr_int_data_bits
= BTF_INT_BITS(int_data
);
2197 /* Not a bitfield member, member offset must be at byte
2200 if (BITS_PER_BYTE_MASKED(struct_bits_off
)) {
2201 btf_verifier_log_member(env
, struct_type
, member
,
2202 "Invalid member offset");
2206 nr_bits
= nr_int_data_bits
;
2207 } else if (nr_bits
> nr_int_data_bits
) {
2208 btf_verifier_log_member(env
, struct_type
, member
,
2209 "Invalid member bitfield_size");
2213 bytes_offset
= BITS_ROUNDDOWN_BYTES(struct_bits_off
);
2214 nr_copy_bits
= nr_bits
+ BITS_PER_BYTE_MASKED(struct_bits_off
);
2215 if (nr_copy_bits
> BITS_PER_U128
) {
2216 btf_verifier_log_member(env
, struct_type
, member
,
2217 "nr_copy_bits exceeds 128");
2221 if (struct_size
< bytes_offset
||
2222 struct_size
- bytes_offset
< BITS_ROUNDUP_BYTES(nr_copy_bits
)) {
2223 btf_verifier_log_member(env
, struct_type
, member
,
2224 "Member exceeds struct_size");
2231 static s32
btf_int_check_meta(struct btf_verifier_env
*env
,
2232 const struct btf_type
*t
,
2235 u32 int_data
, nr_bits
, meta_needed
= sizeof(int_data
);
2238 if (meta_left
< meta_needed
) {
2239 btf_verifier_log_basic(env
, t
,
2240 "meta_left:%u meta_needed:%u",
2241 meta_left
, meta_needed
);
2245 if (btf_type_vlen(t
)) {
2246 btf_verifier_log_type(env
, t
, "vlen != 0");
2250 if (btf_type_kflag(t
)) {
2251 btf_verifier_log_type(env
, t
, "Invalid btf_info kind_flag");
2255 int_data
= btf_type_int(t
);
2256 if (int_data
& ~BTF_INT_MASK
) {
2257 btf_verifier_log_basic(env
, t
, "Invalid int_data:%x",
2262 nr_bits
= BTF_INT_BITS(int_data
) + BTF_INT_OFFSET(int_data
);
2264 if (nr_bits
> BITS_PER_U128
) {
2265 btf_verifier_log_type(env
, t
, "nr_bits exceeds %zu",
2270 if (BITS_ROUNDUP_BYTES(nr_bits
) > t
->size
) {
2271 btf_verifier_log_type(env
, t
, "nr_bits exceeds type_size");
2276 * Only one of the encoding bits is allowed and it
2277 * should be sufficient for the pretty print purpose (i.e. decoding).
2278 * Multiple bits can be allowed later if it is found
2279 * to be insufficient.
2281 encoding
= BTF_INT_ENCODING(int_data
);
2283 encoding
!= BTF_INT_SIGNED
&&
2284 encoding
!= BTF_INT_CHAR
&&
2285 encoding
!= BTF_INT_BOOL
) {
2286 btf_verifier_log_type(env
, t
, "Unsupported encoding");
2290 btf_verifier_log_type(env
, t
, NULL
);
2295 static void btf_int_log(struct btf_verifier_env
*env
,
2296 const struct btf_type
*t
)
2298 int int_data
= btf_type_int(t
);
2300 btf_verifier_log(env
,
2301 "size=%u bits_offset=%u nr_bits=%u encoding=%s",
2302 t
->size
, BTF_INT_OFFSET(int_data
),
2303 BTF_INT_BITS(int_data
),
2304 btf_int_encoding_str(BTF_INT_ENCODING(int_data
)));
2307 static void btf_int128_print(struct btf_show
*show
, void *data
)
2309 /* data points to a __int128 number.
2311 * int128_num = *(__int128 *)data;
2312 * The below formulas shows what upper_num and lower_num represents:
2313 * upper_num = int128_num >> 64;
2314 * lower_num = int128_num & 0xffffffffFFFFFFFFULL;
2316 u64 upper_num
, lower_num
;
2318 #ifdef __BIG_ENDIAN_BITFIELD
2319 upper_num
= *(u64
*)data
;
2320 lower_num
= *(u64
*)(data
+ 8);
2322 upper_num
= *(u64
*)(data
+ 8);
2323 lower_num
= *(u64
*)data
;
2326 btf_show_type_value(show
, "0x%llx", lower_num
);
2328 btf_show_type_values(show
, "0x%llx%016llx", upper_num
,
2332 static void btf_int128_shift(u64
*print_num
, u16 left_shift_bits
,
2333 u16 right_shift_bits
)
2335 u64 upper_num
, lower_num
;
2337 #ifdef __BIG_ENDIAN_BITFIELD
2338 upper_num
= print_num
[0];
2339 lower_num
= print_num
[1];
2341 upper_num
= print_num
[1];
2342 lower_num
= print_num
[0];
2345 /* shake out un-needed bits by shift/or operations */
2346 if (left_shift_bits
>= 64) {
2347 upper_num
= lower_num
<< (left_shift_bits
- 64);
2350 upper_num
= (upper_num
<< left_shift_bits
) |
2351 (lower_num
>> (64 - left_shift_bits
));
2352 lower_num
= lower_num
<< left_shift_bits
;
2355 if (right_shift_bits
>= 64) {
2356 lower_num
= upper_num
>> (right_shift_bits
- 64);
2359 lower_num
= (lower_num
>> right_shift_bits
) |
2360 (upper_num
<< (64 - right_shift_bits
));
2361 upper_num
= upper_num
>> right_shift_bits
;
2364 #ifdef __BIG_ENDIAN_BITFIELD
2365 print_num
[0] = upper_num
;
2366 print_num
[1] = lower_num
;
2368 print_num
[0] = lower_num
;
2369 print_num
[1] = upper_num
;
2373 static void btf_bitfield_show(void *data
, u8 bits_offset
,
2374 u8 nr_bits
, struct btf_show
*show
)
2376 u16 left_shift_bits
, right_shift_bits
;
2379 u64 print_num
[2] = {};
2381 nr_copy_bits
= nr_bits
+ bits_offset
;
2382 nr_copy_bytes
= BITS_ROUNDUP_BYTES(nr_copy_bits
);
2384 memcpy(print_num
, data
, nr_copy_bytes
);
2386 #ifdef __BIG_ENDIAN_BITFIELD
2387 left_shift_bits
= bits_offset
;
2389 left_shift_bits
= BITS_PER_U128
- nr_copy_bits
;
2391 right_shift_bits
= BITS_PER_U128
- nr_bits
;
2393 btf_int128_shift(print_num
, left_shift_bits
, right_shift_bits
);
2394 btf_int128_print(show
, print_num
);
2398 static void btf_int_bits_show(const struct btf
*btf
,
2399 const struct btf_type
*t
,
2400 void *data
, u8 bits_offset
,
2401 struct btf_show
*show
)
2403 u32 int_data
= btf_type_int(t
);
2404 u8 nr_bits
= BTF_INT_BITS(int_data
);
2405 u8 total_bits_offset
;
2408 * bits_offset is at most 7.
2409 * BTF_INT_OFFSET() cannot exceed 128 bits.
2411 total_bits_offset
= bits_offset
+ BTF_INT_OFFSET(int_data
);
2412 data
+= BITS_ROUNDDOWN_BYTES(total_bits_offset
);
2413 bits_offset
= BITS_PER_BYTE_MASKED(total_bits_offset
);
2414 btf_bitfield_show(data
, bits_offset
, nr_bits
, show
);
2417 static void btf_int_show(const struct btf
*btf
, const struct btf_type
*t
,
2418 u32 type_id
, void *data
, u8 bits_offset
,
2419 struct btf_show
*show
)
2421 u32 int_data
= btf_type_int(t
);
2422 u8 encoding
= BTF_INT_ENCODING(int_data
);
2423 bool sign
= encoding
& BTF_INT_SIGNED
;
2424 u8 nr_bits
= BTF_INT_BITS(int_data
);
2427 safe_data
= btf_show_start_type(show
, t
, type_id
, data
);
2431 if (bits_offset
|| BTF_INT_OFFSET(int_data
) ||
2432 BITS_PER_BYTE_MASKED(nr_bits
)) {
2433 btf_int_bits_show(btf
, t
, safe_data
, bits_offset
, show
);
2439 btf_int128_print(show
, safe_data
);
2443 btf_show_type_value(show
, "%lld", *(s64
*)safe_data
);
2445 btf_show_type_value(show
, "%llu", *(u64
*)safe_data
);
2449 btf_show_type_value(show
, "%d", *(s32
*)safe_data
);
2451 btf_show_type_value(show
, "%u", *(u32
*)safe_data
);
2455 btf_show_type_value(show
, "%d", *(s16
*)safe_data
);
2457 btf_show_type_value(show
, "%u", *(u16
*)safe_data
);
2460 if (show
->state
.array_encoding
== BTF_INT_CHAR
) {
2461 /* check for null terminator */
2462 if (show
->state
.array_terminated
)
2464 if (*(char *)data
== '\0') {
2465 show
->state
.array_terminated
= 1;
2468 if (isprint(*(char *)data
)) {
2469 btf_show_type_value(show
, "'%c'",
2470 *(char *)safe_data
);
2475 btf_show_type_value(show
, "%d", *(s8
*)safe_data
);
2477 btf_show_type_value(show
, "%u", *(u8
*)safe_data
);
2480 btf_int_bits_show(btf
, t
, safe_data
, bits_offset
, show
);
2484 btf_show_end_type(show
);
2487 static const struct btf_kind_operations int_ops
= {
2488 .check_meta
= btf_int_check_meta
,
2489 .resolve
= btf_df_resolve
,
2490 .check_member
= btf_int_check_member
,
2491 .check_kflag_member
= btf_int_check_kflag_member
,
2492 .log_details
= btf_int_log
,
2493 .show
= btf_int_show
,
2496 static int btf_modifier_check_member(struct btf_verifier_env
*env
,
2497 const struct btf_type
*struct_type
,
2498 const struct btf_member
*member
,
2499 const struct btf_type
*member_type
)
2501 const struct btf_type
*resolved_type
;
2502 u32 resolved_type_id
= member
->type
;
2503 struct btf_member resolved_member
;
2504 struct btf
*btf
= env
->btf
;
2506 resolved_type
= btf_type_id_size(btf
, &resolved_type_id
, NULL
);
2507 if (!resolved_type
) {
2508 btf_verifier_log_member(env
, struct_type
, member
,
2513 resolved_member
= *member
;
2514 resolved_member
.type
= resolved_type_id
;
2516 return btf_type_ops(resolved_type
)->check_member(env
, struct_type
,
2521 static int btf_modifier_check_kflag_member(struct btf_verifier_env
*env
,
2522 const struct btf_type
*struct_type
,
2523 const struct btf_member
*member
,
2524 const struct btf_type
*member_type
)
2526 const struct btf_type
*resolved_type
;
2527 u32 resolved_type_id
= member
->type
;
2528 struct btf_member resolved_member
;
2529 struct btf
*btf
= env
->btf
;
2531 resolved_type
= btf_type_id_size(btf
, &resolved_type_id
, NULL
);
2532 if (!resolved_type
) {
2533 btf_verifier_log_member(env
, struct_type
, member
,
2538 resolved_member
= *member
;
2539 resolved_member
.type
= resolved_type_id
;
2541 return btf_type_ops(resolved_type
)->check_kflag_member(env
, struct_type
,
2546 static int btf_ptr_check_member(struct btf_verifier_env
*env
,
2547 const struct btf_type
*struct_type
,
2548 const struct btf_member
*member
,
2549 const struct btf_type
*member_type
)
2551 u32 struct_size
, struct_bits_off
, bytes_offset
;
2553 struct_size
= struct_type
->size
;
2554 struct_bits_off
= member
->offset
;
2555 bytes_offset
= BITS_ROUNDDOWN_BYTES(struct_bits_off
);
2557 if (BITS_PER_BYTE_MASKED(struct_bits_off
)) {
2558 btf_verifier_log_member(env
, struct_type
, member
,
2559 "Member is not byte aligned");
2563 if (struct_size
- bytes_offset
< sizeof(void *)) {
2564 btf_verifier_log_member(env
, struct_type
, member
,
2565 "Member exceeds struct_size");
2572 static int btf_ref_type_check_meta(struct btf_verifier_env
*env
,
2573 const struct btf_type
*t
,
2578 if (btf_type_vlen(t
)) {
2579 btf_verifier_log_type(env
, t
, "vlen != 0");
2583 if (btf_type_kflag(t
)) {
2584 btf_verifier_log_type(env
, t
, "Invalid btf_info kind_flag");
2588 if (!BTF_TYPE_ID_VALID(t
->type
)) {
2589 btf_verifier_log_type(env
, t
, "Invalid type_id");
2593 /* typedef/type_tag type must have a valid name, and other ref types,
2594 * volatile, const, restrict, should have a null name.
2596 if (BTF_INFO_KIND(t
->info
) == BTF_KIND_TYPEDEF
) {
2598 !btf_name_valid_identifier(env
->btf
, t
->name_off
)) {
2599 btf_verifier_log_type(env
, t
, "Invalid name");
2602 } else if (BTF_INFO_KIND(t
->info
) == BTF_KIND_TYPE_TAG
) {
2603 value
= btf_name_by_offset(env
->btf
, t
->name_off
);
2604 if (!value
|| !value
[0]) {
2605 btf_verifier_log_type(env
, t
, "Invalid name");
2610 btf_verifier_log_type(env
, t
, "Invalid name");
2615 btf_verifier_log_type(env
, t
, NULL
);
2620 static int btf_modifier_resolve(struct btf_verifier_env
*env
,
2621 const struct resolve_vertex
*v
)
2623 const struct btf_type
*t
= v
->t
;
2624 const struct btf_type
*next_type
;
2625 u32 next_type_id
= t
->type
;
2626 struct btf
*btf
= env
->btf
;
2628 next_type
= btf_type_by_id(btf
, next_type_id
);
2629 if (!next_type
|| btf_type_is_resolve_source_only(next_type
)) {
2630 btf_verifier_log_type(env
, v
->t
, "Invalid type_id");
2634 if (!env_type_is_resolve_sink(env
, next_type
) &&
2635 !env_type_is_resolved(env
, next_type_id
))
2636 return env_stack_push(env
, next_type
, next_type_id
);
2638 /* Figure out the resolved next_type_id with size.
2639 * They will be stored in the current modifier's
2640 * resolved_ids and resolved_sizes such that it can
2641 * save us a few type-following when we use it later (e.g. in
2644 if (!btf_type_id_size(btf
, &next_type_id
, NULL
)) {
2645 if (env_type_is_resolved(env
, next_type_id
))
2646 next_type
= btf_type_id_resolve(btf
, &next_type_id
);
2648 /* "typedef void new_void", "const void"...etc */
2649 if (!btf_type_is_void(next_type
) &&
2650 !btf_type_is_fwd(next_type
) &&
2651 !btf_type_is_func_proto(next_type
)) {
2652 btf_verifier_log_type(env
, v
->t
, "Invalid type_id");
2657 env_stack_pop_resolved(env
, next_type_id
, 0);
2662 static int btf_var_resolve(struct btf_verifier_env
*env
,
2663 const struct resolve_vertex
*v
)
2665 const struct btf_type
*next_type
;
2666 const struct btf_type
*t
= v
->t
;
2667 u32 next_type_id
= t
->type
;
2668 struct btf
*btf
= env
->btf
;
2670 next_type
= btf_type_by_id(btf
, next_type_id
);
2671 if (!next_type
|| btf_type_is_resolve_source_only(next_type
)) {
2672 btf_verifier_log_type(env
, v
->t
, "Invalid type_id");
2676 if (!env_type_is_resolve_sink(env
, next_type
) &&
2677 !env_type_is_resolved(env
, next_type_id
))
2678 return env_stack_push(env
, next_type
, next_type_id
);
2680 if (btf_type_is_modifier(next_type
)) {
2681 const struct btf_type
*resolved_type
;
2682 u32 resolved_type_id
;
2684 resolved_type_id
= next_type_id
;
2685 resolved_type
= btf_type_id_resolve(btf
, &resolved_type_id
);
2687 if (btf_type_is_ptr(resolved_type
) &&
2688 !env_type_is_resolve_sink(env
, resolved_type
) &&
2689 !env_type_is_resolved(env
, resolved_type_id
))
2690 return env_stack_push(env
, resolved_type
,
2694 /* We must resolve to something concrete at this point, no
2695 * forward types or similar that would resolve to size of
2698 if (!btf_type_id_size(btf
, &next_type_id
, NULL
)) {
2699 btf_verifier_log_type(env
, v
->t
, "Invalid type_id");
2703 env_stack_pop_resolved(env
, next_type_id
, 0);
2708 static int btf_ptr_resolve(struct btf_verifier_env
*env
,
2709 const struct resolve_vertex
*v
)
2711 const struct btf_type
*next_type
;
2712 const struct btf_type
*t
= v
->t
;
2713 u32 next_type_id
= t
->type
;
2714 struct btf
*btf
= env
->btf
;
2716 next_type
= btf_type_by_id(btf
, next_type_id
);
2717 if (!next_type
|| btf_type_is_resolve_source_only(next_type
)) {
2718 btf_verifier_log_type(env
, v
->t
, "Invalid type_id");
2722 if (!env_type_is_resolve_sink(env
, next_type
) &&
2723 !env_type_is_resolved(env
, next_type_id
))
2724 return env_stack_push(env
, next_type
, next_type_id
);
2726 /* If the modifier was RESOLVED during RESOLVE_STRUCT_OR_ARRAY,
2727 * the modifier may have stopped resolving when it was resolved
2728 * to a ptr (last-resolved-ptr).
2730 * We now need to continue from the last-resolved-ptr to
2731 * ensure the last-resolved-ptr will not referring back to
2732 * the current ptr (t).
2734 if (btf_type_is_modifier(next_type
)) {
2735 const struct btf_type
*resolved_type
;
2736 u32 resolved_type_id
;
2738 resolved_type_id
= next_type_id
;
2739 resolved_type
= btf_type_id_resolve(btf
, &resolved_type_id
);
2741 if (btf_type_is_ptr(resolved_type
) &&
2742 !env_type_is_resolve_sink(env
, resolved_type
) &&
2743 !env_type_is_resolved(env
, resolved_type_id
))
2744 return env_stack_push(env
, resolved_type
,
2748 if (!btf_type_id_size(btf
, &next_type_id
, NULL
)) {
2749 if (env_type_is_resolved(env
, next_type_id
))
2750 next_type
= btf_type_id_resolve(btf
, &next_type_id
);
2752 if (!btf_type_is_void(next_type
) &&
2753 !btf_type_is_fwd(next_type
) &&
2754 !btf_type_is_func_proto(next_type
)) {
2755 btf_verifier_log_type(env
, v
->t
, "Invalid type_id");
2760 env_stack_pop_resolved(env
, next_type_id
, 0);
2765 static void btf_modifier_show(const struct btf
*btf
,
2766 const struct btf_type
*t
,
2767 u32 type_id
, void *data
,
2768 u8 bits_offset
, struct btf_show
*show
)
2770 if (btf
->resolved_ids
)
2771 t
= btf_type_id_resolve(btf
, &type_id
);
2773 t
= btf_type_skip_modifiers(btf
, type_id
, NULL
);
2775 btf_type_ops(t
)->show(btf
, t
, type_id
, data
, bits_offset
, show
);
2778 static void btf_var_show(const struct btf
*btf
, const struct btf_type
*t
,
2779 u32 type_id
, void *data
, u8 bits_offset
,
2780 struct btf_show
*show
)
2782 t
= btf_type_id_resolve(btf
, &type_id
);
2784 btf_type_ops(t
)->show(btf
, t
, type_id
, data
, bits_offset
, show
);
2787 static void btf_ptr_show(const struct btf
*btf
, const struct btf_type
*t
,
2788 u32 type_id
, void *data
, u8 bits_offset
,
2789 struct btf_show
*show
)
2793 safe_data
= btf_show_start_type(show
, t
, type_id
, data
);
2797 /* It is a hashed value unless BTF_SHOW_PTR_RAW is specified */
2798 if (show
->flags
& BTF_SHOW_PTR_RAW
)
2799 btf_show_type_value(show
, "0x%px", *(void **)safe_data
);
2801 btf_show_type_value(show
, "0x%p", *(void **)safe_data
);
2802 btf_show_end_type(show
);
2805 static void btf_ref_type_log(struct btf_verifier_env
*env
,
2806 const struct btf_type
*t
)
2808 btf_verifier_log(env
, "type_id=%u", t
->type
);
2811 static const struct btf_kind_operations modifier_ops
= {
2812 .check_meta
= btf_ref_type_check_meta
,
2813 .resolve
= btf_modifier_resolve
,
2814 .check_member
= btf_modifier_check_member
,
2815 .check_kflag_member
= btf_modifier_check_kflag_member
,
2816 .log_details
= btf_ref_type_log
,
2817 .show
= btf_modifier_show
,
2820 static const struct btf_kind_operations ptr_ops
= {
2821 .check_meta
= btf_ref_type_check_meta
,
2822 .resolve
= btf_ptr_resolve
,
2823 .check_member
= btf_ptr_check_member
,
2824 .check_kflag_member
= btf_generic_check_kflag_member
,
2825 .log_details
= btf_ref_type_log
,
2826 .show
= btf_ptr_show
,
2829 static s32
btf_fwd_check_meta(struct btf_verifier_env
*env
,
2830 const struct btf_type
*t
,
2833 if (btf_type_vlen(t
)) {
2834 btf_verifier_log_type(env
, t
, "vlen != 0");
2839 btf_verifier_log_type(env
, t
, "type != 0");
2843 /* fwd type must have a valid name */
2845 !btf_name_valid_identifier(env
->btf
, t
->name_off
)) {
2846 btf_verifier_log_type(env
, t
, "Invalid name");
2850 btf_verifier_log_type(env
, t
, NULL
);
2855 static void btf_fwd_type_log(struct btf_verifier_env
*env
,
2856 const struct btf_type
*t
)
2858 btf_verifier_log(env
, "%s", btf_type_kflag(t
) ? "union" : "struct");
2861 static const struct btf_kind_operations fwd_ops
= {
2862 .check_meta
= btf_fwd_check_meta
,
2863 .resolve
= btf_df_resolve
,
2864 .check_member
= btf_df_check_member
,
2865 .check_kflag_member
= btf_df_check_kflag_member
,
2866 .log_details
= btf_fwd_type_log
,
2867 .show
= btf_df_show
,
2870 static int btf_array_check_member(struct btf_verifier_env
*env
,
2871 const struct btf_type
*struct_type
,
2872 const struct btf_member
*member
,
2873 const struct btf_type
*member_type
)
2875 u32 struct_bits_off
= member
->offset
;
2876 u32 struct_size
, bytes_offset
;
2877 u32 array_type_id
, array_size
;
2878 struct btf
*btf
= env
->btf
;
2880 if (BITS_PER_BYTE_MASKED(struct_bits_off
)) {
2881 btf_verifier_log_member(env
, struct_type
, member
,
2882 "Member is not byte aligned");
2886 array_type_id
= member
->type
;
2887 btf_type_id_size(btf
, &array_type_id
, &array_size
);
2888 struct_size
= struct_type
->size
;
2889 bytes_offset
= BITS_ROUNDDOWN_BYTES(struct_bits_off
);
2890 if (struct_size
- bytes_offset
< array_size
) {
2891 btf_verifier_log_member(env
, struct_type
, member
,
2892 "Member exceeds struct_size");
2899 static s32
btf_array_check_meta(struct btf_verifier_env
*env
,
2900 const struct btf_type
*t
,
2903 const struct btf_array
*array
= btf_type_array(t
);
2904 u32 meta_needed
= sizeof(*array
);
2906 if (meta_left
< meta_needed
) {
2907 btf_verifier_log_basic(env
, t
,
2908 "meta_left:%u meta_needed:%u",
2909 meta_left
, meta_needed
);
2913 /* array type should not have a name */
2915 btf_verifier_log_type(env
, t
, "Invalid name");
2919 if (btf_type_vlen(t
)) {
2920 btf_verifier_log_type(env
, t
, "vlen != 0");
2924 if (btf_type_kflag(t
)) {
2925 btf_verifier_log_type(env
, t
, "Invalid btf_info kind_flag");
2930 btf_verifier_log_type(env
, t
, "size != 0");
2934 /* Array elem type and index type cannot be in type void,
2935 * so !array->type and !array->index_type are not allowed.
2937 if (!array
->type
|| !BTF_TYPE_ID_VALID(array
->type
)) {
2938 btf_verifier_log_type(env
, t
, "Invalid elem");
2942 if (!array
->index_type
|| !BTF_TYPE_ID_VALID(array
->index_type
)) {
2943 btf_verifier_log_type(env
, t
, "Invalid index");
2947 btf_verifier_log_type(env
, t
, NULL
);
2952 static int btf_array_resolve(struct btf_verifier_env
*env
,
2953 const struct resolve_vertex
*v
)
2955 const struct btf_array
*array
= btf_type_array(v
->t
);
2956 const struct btf_type
*elem_type
, *index_type
;
2957 u32 elem_type_id
, index_type_id
;
2958 struct btf
*btf
= env
->btf
;
2961 /* Check array->index_type */
2962 index_type_id
= array
->index_type
;
2963 index_type
= btf_type_by_id(btf
, index_type_id
);
2964 if (btf_type_nosize_or_null(index_type
) ||
2965 btf_type_is_resolve_source_only(index_type
)) {
2966 btf_verifier_log_type(env
, v
->t
, "Invalid index");
2970 if (!env_type_is_resolve_sink(env
, index_type
) &&
2971 !env_type_is_resolved(env
, index_type_id
))
2972 return env_stack_push(env
, index_type
, index_type_id
);
2974 index_type
= btf_type_id_size(btf
, &index_type_id
, NULL
);
2975 if (!index_type
|| !btf_type_is_int(index_type
) ||
2976 !btf_type_int_is_regular(index_type
)) {
2977 btf_verifier_log_type(env
, v
->t
, "Invalid index");
2981 /* Check array->type */
2982 elem_type_id
= array
->type
;
2983 elem_type
= btf_type_by_id(btf
, elem_type_id
);
2984 if (btf_type_nosize_or_null(elem_type
) ||
2985 btf_type_is_resolve_source_only(elem_type
)) {
2986 btf_verifier_log_type(env
, v
->t
,
2991 if (!env_type_is_resolve_sink(env
, elem_type
) &&
2992 !env_type_is_resolved(env
, elem_type_id
))
2993 return env_stack_push(env
, elem_type
, elem_type_id
);
2995 elem_type
= btf_type_id_size(btf
, &elem_type_id
, &elem_size
);
2997 btf_verifier_log_type(env
, v
->t
, "Invalid elem");
3001 if (btf_type_is_int(elem_type
) && !btf_type_int_is_regular(elem_type
)) {
3002 btf_verifier_log_type(env
, v
->t
, "Invalid array of int");
3006 if (array
->nelems
&& elem_size
> U32_MAX
/ array
->nelems
) {
3007 btf_verifier_log_type(env
, v
->t
,
3008 "Array size overflows U32_MAX");
3012 env_stack_pop_resolved(env
, elem_type_id
, elem_size
* array
->nelems
);
3017 static void btf_array_log(struct btf_verifier_env
*env
,
3018 const struct btf_type
*t
)
3020 const struct btf_array
*array
= btf_type_array(t
);
3022 btf_verifier_log(env
, "type_id=%u index_type_id=%u nr_elems=%u",
3023 array
->type
, array
->index_type
, array
->nelems
);
3026 static void __btf_array_show(const struct btf
*btf
, const struct btf_type
*t
,
3027 u32 type_id
, void *data
, u8 bits_offset
,
3028 struct btf_show
*show
)
3030 const struct btf_array
*array
= btf_type_array(t
);
3031 const struct btf_kind_operations
*elem_ops
;
3032 const struct btf_type
*elem_type
;
3033 u32 i
, elem_size
= 0, elem_type_id
;
3036 elem_type_id
= array
->type
;
3037 elem_type
= btf_type_skip_modifiers(btf
, elem_type_id
, NULL
);
3038 if (elem_type
&& btf_type_has_size(elem_type
))
3039 elem_size
= elem_type
->size
;
3041 if (elem_type
&& btf_type_is_int(elem_type
)) {
3042 u32 int_type
= btf_type_int(elem_type
);
3044 encoding
= BTF_INT_ENCODING(int_type
);
3047 * BTF_INT_CHAR encoding never seems to be set for
3048 * char arrays, so if size is 1 and element is
3049 * printable as a char, we'll do that.
3052 encoding
= BTF_INT_CHAR
;
3055 if (!btf_show_start_array_type(show
, t
, type_id
, encoding
, data
))
3060 elem_ops
= btf_type_ops(elem_type
);
3062 for (i
= 0; i
< array
->nelems
; i
++) {
3064 btf_show_start_array_member(show
);
3066 elem_ops
->show(btf
, elem_type
, elem_type_id
, data
,
3070 btf_show_end_array_member(show
);
3072 if (show
->state
.array_terminated
)
3076 btf_show_end_array_type(show
);
3079 static void btf_array_show(const struct btf
*btf
, const struct btf_type
*t
,
3080 u32 type_id
, void *data
, u8 bits_offset
,
3081 struct btf_show
*show
)
3083 const struct btf_member
*m
= show
->state
.member
;
3086 * First check if any members would be shown (are non-zero).
3087 * See comments above "struct btf_show" definition for more
3088 * details on how this works at a high-level.
3090 if (show
->state
.depth
> 0 && !(show
->flags
& BTF_SHOW_ZERO
)) {
3091 if (!show
->state
.depth_check
) {
3092 show
->state
.depth_check
= show
->state
.depth
+ 1;
3093 show
->state
.depth_to_show
= 0;
3095 __btf_array_show(btf
, t
, type_id
, data
, bits_offset
, show
);
3096 show
->state
.member
= m
;
3098 if (show
->state
.depth_check
!= show
->state
.depth
+ 1)
3100 show
->state
.depth_check
= 0;
3102 if (show
->state
.depth_to_show
<= show
->state
.depth
)
3105 * Reaching here indicates we have recursed and found
3106 * non-zero array member(s).
3109 __btf_array_show(btf
, t
, type_id
, data
, bits_offset
, show
);
3112 static const struct btf_kind_operations array_ops
= {
3113 .check_meta
= btf_array_check_meta
,
3114 .resolve
= btf_array_resolve
,
3115 .check_member
= btf_array_check_member
,
3116 .check_kflag_member
= btf_generic_check_kflag_member
,
3117 .log_details
= btf_array_log
,
3118 .show
= btf_array_show
,
3121 static int btf_struct_check_member(struct btf_verifier_env
*env
,
3122 const struct btf_type
*struct_type
,
3123 const struct btf_member
*member
,
3124 const struct btf_type
*member_type
)
3126 u32 struct_bits_off
= member
->offset
;
3127 u32 struct_size
, bytes_offset
;
3129 if (BITS_PER_BYTE_MASKED(struct_bits_off
)) {
3130 btf_verifier_log_member(env
, struct_type
, member
,
3131 "Member is not byte aligned");
3135 struct_size
= struct_type
->size
;
3136 bytes_offset
= BITS_ROUNDDOWN_BYTES(struct_bits_off
);
3137 if (struct_size
- bytes_offset
< member_type
->size
) {
3138 btf_verifier_log_member(env
, struct_type
, member
,
3139 "Member exceeds struct_size");
3146 static s32
btf_struct_check_meta(struct btf_verifier_env
*env
,
3147 const struct btf_type
*t
,
3150 bool is_union
= BTF_INFO_KIND(t
->info
) == BTF_KIND_UNION
;
3151 const struct btf_member
*member
;
3152 u32 meta_needed
, last_offset
;
3153 struct btf
*btf
= env
->btf
;
3154 u32 struct_size
= t
->size
;
3158 meta_needed
= btf_type_vlen(t
) * sizeof(*member
);
3159 if (meta_left
< meta_needed
) {
3160 btf_verifier_log_basic(env
, t
,
3161 "meta_left:%u meta_needed:%u",
3162 meta_left
, meta_needed
);
3166 /* struct type either no name or a valid one */
3168 !btf_name_valid_identifier(env
->btf
, t
->name_off
)) {
3169 btf_verifier_log_type(env
, t
, "Invalid name");
3173 btf_verifier_log_type(env
, t
, NULL
);
3176 for_each_member(i
, t
, member
) {
3177 if (!btf_name_offset_valid(btf
, member
->name_off
)) {
3178 btf_verifier_log_member(env
, t
, member
,
3179 "Invalid member name_offset:%u",
3184 /* struct member either no name or a valid one */
3185 if (member
->name_off
&&
3186 !btf_name_valid_identifier(btf
, member
->name_off
)) {
3187 btf_verifier_log_member(env
, t
, member
, "Invalid name");
3190 /* A member cannot be in type void */
3191 if (!member
->type
|| !BTF_TYPE_ID_VALID(member
->type
)) {
3192 btf_verifier_log_member(env
, t
, member
,
3197 offset
= __btf_member_bit_offset(t
, member
);
3198 if (is_union
&& offset
) {
3199 btf_verifier_log_member(env
, t
, member
,
3200 "Invalid member bits_offset");
3205 * ">" instead of ">=" because the last member could be
3208 if (last_offset
> offset
) {
3209 btf_verifier_log_member(env
, t
, member
,
3210 "Invalid member bits_offset");
3214 if (BITS_ROUNDUP_BYTES(offset
) > struct_size
) {
3215 btf_verifier_log_member(env
, t
, member
,
3216 "Member bits_offset exceeds its struct size");
3220 btf_verifier_log_member(env
, t
, member
, NULL
);
3221 last_offset
= offset
;
3227 static int btf_struct_resolve(struct btf_verifier_env
*env
,
3228 const struct resolve_vertex
*v
)
3230 const struct btf_member
*member
;
3234 /* Before continue resolving the next_member,
3235 * ensure the last member is indeed resolved to a
3236 * type with size info.
3238 if (v
->next_member
) {
3239 const struct btf_type
*last_member_type
;
3240 const struct btf_member
*last_member
;
3241 u32 last_member_type_id
;
3243 last_member
= btf_type_member(v
->t
) + v
->next_member
- 1;
3244 last_member_type_id
= last_member
->type
;
3245 if (WARN_ON_ONCE(!env_type_is_resolved(env
,
3246 last_member_type_id
)))
3249 last_member_type
= btf_type_by_id(env
->btf
,
3250 last_member_type_id
);
3251 if (btf_type_kflag(v
->t
))
3252 err
= btf_type_ops(last_member_type
)->check_kflag_member(env
, v
->t
,
3256 err
= btf_type_ops(last_member_type
)->check_member(env
, v
->t
,
3263 for_each_member_from(i
, v
->next_member
, v
->t
, member
) {
3264 u32 member_type_id
= member
->type
;
3265 const struct btf_type
*member_type
= btf_type_by_id(env
->btf
,
3268 if (btf_type_nosize_or_null(member_type
) ||
3269 btf_type_is_resolve_source_only(member_type
)) {
3270 btf_verifier_log_member(env
, v
->t
, member
,
3275 if (!env_type_is_resolve_sink(env
, member_type
) &&
3276 !env_type_is_resolved(env
, member_type_id
)) {
3277 env_stack_set_next_member(env
, i
+ 1);
3278 return env_stack_push(env
, member_type
, member_type_id
);
3281 if (btf_type_kflag(v
->t
))
3282 err
= btf_type_ops(member_type
)->check_kflag_member(env
, v
->t
,
3286 err
= btf_type_ops(member_type
)->check_member(env
, v
->t
,
3293 env_stack_pop_resolved(env
, 0, 0);
3298 static void btf_struct_log(struct btf_verifier_env
*env
,
3299 const struct btf_type
*t
)
3301 btf_verifier_log(env
, "size=%u vlen=%u", t
->size
, btf_type_vlen(t
));
3305 BTF_FIELD_IGNORE
= 0,
3306 BTF_FIELD_FOUND
= 1,
3309 struct btf_field_info
{
3310 enum btf_field_type type
;
3317 const char *node_name
;
3323 static int btf_find_struct(const struct btf
*btf
, const struct btf_type
*t
,
3324 u32 off
, int sz
, enum btf_field_type field_type
,
3325 struct btf_field_info
*info
)
3327 if (!__btf_type_is_struct(t
))
3328 return BTF_FIELD_IGNORE
;
3330 return BTF_FIELD_IGNORE
;
3331 info
->type
= field_type
;
3333 return BTF_FIELD_FOUND
;
3336 static int btf_find_kptr(const struct btf
*btf
, const struct btf_type
*t
,
3337 u32 off
, int sz
, struct btf_field_info
*info
, u32 field_mask
)
3339 enum btf_field_type type
;
3342 /* Permit modifiers on the pointer itself */
3343 if (btf_type_is_volatile(t
))
3344 t
= btf_type_by_id(btf
, t
->type
);
3345 /* For PTR, sz is always == 8 */
3346 if (!btf_type_is_ptr(t
))
3347 return BTF_FIELD_IGNORE
;
3348 t
= btf_type_by_id(btf
, t
->type
);
3350 if (!btf_type_is_type_tag(t
))
3351 return BTF_FIELD_IGNORE
;
3352 /* Reject extra tags */
3353 if (btf_type_is_type_tag(btf_type_by_id(btf
, t
->type
)))
3355 if (!strcmp("kptr_untrusted", __btf_name_by_offset(btf
, t
->name_off
)))
3356 type
= BPF_KPTR_UNREF
;
3357 else if (!strcmp("kptr", __btf_name_by_offset(btf
, t
->name_off
)))
3358 type
= BPF_KPTR_REF
;
3359 else if (!strcmp("percpu_kptr", __btf_name_by_offset(btf
, t
->name_off
)))
3360 type
= BPF_KPTR_PERCPU
;
3361 else if (!strcmp("uptr", __btf_name_by_offset(btf
, t
->name_off
)))
3366 if (!(type
& field_mask
))
3367 return BTF_FIELD_IGNORE
;
3369 /* Get the base type */
3370 t
= btf_type_skip_modifiers(btf
, t
->type
, &res_id
);
3371 /* Only pointer to struct is allowed */
3372 if (!__btf_type_is_struct(t
))
3377 info
->kptr
.type_id
= res_id
;
3378 return BTF_FIELD_FOUND
;
3381 int btf_find_next_decl_tag(const struct btf
*btf
, const struct btf_type
*pt
,
3382 int comp_idx
, const char *tag_key
, int last_id
)
3384 int len
= strlen(tag_key
);
3387 for (i
= last_id
+ 1, n
= btf_nr_types(btf
); i
< n
; i
++) {
3388 const struct btf_type
*t
= btf_type_by_id(btf
, i
);
3390 if (!btf_type_is_decl_tag(t
))
3392 if (pt
!= btf_type_by_id(btf
, t
->type
))
3394 if (btf_type_decl_tag(t
)->component_idx
!= comp_idx
)
3396 if (strncmp(__btf_name_by_offset(btf
, t
->name_off
), tag_key
, len
))
3403 const char *btf_find_decl_tag_value(const struct btf
*btf
, const struct btf_type
*pt
,
3404 int comp_idx
, const char *tag_key
)
3406 const char *value
= NULL
;
3407 const struct btf_type
*t
;
3410 id
= btf_find_next_decl_tag(btf
, pt
, comp_idx
, tag_key
, 0);
3414 t
= btf_type_by_id(btf
, id
);
3415 len
= strlen(tag_key
);
3416 value
= __btf_name_by_offset(btf
, t
->name_off
) + len
;
3418 /* Prevent duplicate entries for same type */
3419 id
= btf_find_next_decl_tag(btf
, pt
, comp_idx
, tag_key
, id
);
3421 return ERR_PTR(-EEXIST
);
3427 btf_find_graph_root(const struct btf
*btf
, const struct btf_type
*pt
,
3428 const struct btf_type
*t
, int comp_idx
, u32 off
,
3429 int sz
, struct btf_field_info
*info
,
3430 enum btf_field_type head_type
)
3432 const char *node_field_name
;
3433 const char *value_type
;
3436 if (!__btf_type_is_struct(t
))
3437 return BTF_FIELD_IGNORE
;
3439 return BTF_FIELD_IGNORE
;
3440 value_type
= btf_find_decl_tag_value(btf
, pt
, comp_idx
, "contains:");
3441 if (IS_ERR(value_type
))
3443 node_field_name
= strstr(value_type
, ":");
3444 if (!node_field_name
)
3446 value_type
= kstrndup(value_type
, node_field_name
- value_type
, GFP_KERNEL
| __GFP_NOWARN
);
3449 id
= btf_find_by_name_kind(btf
, value_type
, BTF_KIND_STRUCT
);
3454 if (str_is_empty(node_field_name
))
3456 info
->type
= head_type
;
3458 info
->graph_root
.value_btf_id
= id
;
3459 info
->graph_root
.node_name
= node_field_name
;
3460 return BTF_FIELD_FOUND
;
3463 #define field_mask_test_name(field_type, field_type_str) \
3464 if (field_mask & field_type && !strcmp(name, field_type_str)) { \
3465 type = field_type; \
3469 static int btf_get_field_type(const struct btf
*btf
, const struct btf_type
*var_type
,
3470 u32 field_mask
, u32
*seen_mask
,
3471 int *align
, int *sz
)
3474 const char *name
= __btf_name_by_offset(btf
, var_type
->name_off
);
3476 if (field_mask
& BPF_SPIN_LOCK
) {
3477 if (!strcmp(name
, "bpf_spin_lock")) {
3478 if (*seen_mask
& BPF_SPIN_LOCK
)
3480 *seen_mask
|= BPF_SPIN_LOCK
;
3481 type
= BPF_SPIN_LOCK
;
3485 if (field_mask
& BPF_TIMER
) {
3486 if (!strcmp(name
, "bpf_timer")) {
3487 if (*seen_mask
& BPF_TIMER
)
3489 *seen_mask
|= BPF_TIMER
;
3494 if (field_mask
& BPF_WORKQUEUE
) {
3495 if (!strcmp(name
, "bpf_wq")) {
3496 if (*seen_mask
& BPF_WORKQUEUE
)
3498 *seen_mask
|= BPF_WORKQUEUE
;
3499 type
= BPF_WORKQUEUE
;
3503 field_mask_test_name(BPF_LIST_HEAD
, "bpf_list_head");
3504 field_mask_test_name(BPF_LIST_NODE
, "bpf_list_node");
3505 field_mask_test_name(BPF_RB_ROOT
, "bpf_rb_root");
3506 field_mask_test_name(BPF_RB_NODE
, "bpf_rb_node");
3507 field_mask_test_name(BPF_REFCOUNT
, "bpf_refcount");
3509 /* Only return BPF_KPTR when all other types with matchable names fail */
3510 if (field_mask
& (BPF_KPTR
| BPF_UPTR
) && !__btf_type_is_struct(var_type
)) {
3511 type
= BPF_KPTR_REF
;
3516 *sz
= btf_field_type_size(type
);
3517 *align
= btf_field_type_align(type
);
3521 #undef field_mask_test_name
3523 /* Repeat a number of fields for a specified number of times.
3525 * Copy the fields starting from the first field and repeat them for
3526 * repeat_cnt times. The fields are repeated by adding the offset of each
3528 * (i + 1) * elem_size
3529 * where i is the repeat index and elem_size is the size of an element.
3531 static int btf_repeat_fields(struct btf_field_info
*info
, int info_cnt
,
3532 u32 field_cnt
, u32 repeat_cnt
, u32 elem_size
)
3537 /* Ensure not repeating fields that should not be repeated. */
3538 for (i
= 0; i
< field_cnt
; i
++) {
3539 switch (info
[i
].type
) {
3540 case BPF_KPTR_UNREF
:
3542 case BPF_KPTR_PERCPU
:
3552 /* The type of struct size or variable size is u32,
3553 * so the multiplication will not overflow.
3555 if (field_cnt
* (repeat_cnt
+ 1) > info_cnt
)
3559 for (i
= 0; i
< repeat_cnt
; i
++) {
3560 memcpy(&info
[cur
], &info
[0], field_cnt
* sizeof(info
[0]));
3561 for (j
= 0; j
< field_cnt
; j
++)
3562 info
[cur
++].off
+= (i
+ 1) * elem_size
;
3568 static int btf_find_struct_field(const struct btf
*btf
,
3569 const struct btf_type
*t
, u32 field_mask
,
3570 struct btf_field_info
*info
, int info_cnt
,
3573 /* Find special fields in the struct type of a field.
3575 * This function is used to find fields of special types that is not a
3576 * global variable or a direct field of a struct type. It also handles the
3577 * repetition if it is the element type of an array.
3579 static int btf_find_nested_struct(const struct btf
*btf
, const struct btf_type
*t
,
3580 u32 off
, u32 nelems
,
3581 u32 field_mask
, struct btf_field_info
*info
,
3582 int info_cnt
, u32 level
)
3587 if (level
>= MAX_RESOLVE_DEPTH
)
3590 ret
= btf_find_struct_field(btf
, t
, field_mask
, info
, info_cnt
, level
);
3595 /* Shift the offsets of the nested struct fields to the offsets
3596 * related to the container.
3598 for (i
= 0; i
< ret
; i
++)
3602 err
= btf_repeat_fields(info
, info_cnt
, ret
, nelems
- 1, t
->size
);
3612 static int btf_find_field_one(const struct btf
*btf
,
3613 const struct btf_type
*var
,
3614 const struct btf_type
*var_type
,
3616 u32 off
, u32 expected_size
,
3617 u32 field_mask
, u32
*seen_mask
,
3618 struct btf_field_info
*info
, int info_cnt
,
3621 int ret
, align
, sz
, field_type
;
3622 struct btf_field_info tmp
;
3623 const struct btf_array
*array
;
3626 /* Walk into array types to find the element type and the number of
3627 * elements in the (flattened) array.
3629 for (i
= 0; i
< MAX_RESOLVE_DEPTH
&& btf_type_is_array(var_type
); i
++) {
3630 array
= btf_array(var_type
);
3631 nelems
*= array
->nelems
;
3632 var_type
= btf_type_by_id(btf
, array
->type
);
3634 if (i
== MAX_RESOLVE_DEPTH
)
3639 field_type
= btf_get_field_type(btf
, var_type
,
3640 field_mask
, seen_mask
, &align
, &sz
);
3641 /* Look into variables of struct types */
3642 if (!field_type
&& __btf_type_is_struct(var_type
)) {
3643 sz
= var_type
->size
;
3644 if (expected_size
&& expected_size
!= sz
* nelems
)
3646 ret
= btf_find_nested_struct(btf
, var_type
, off
, nelems
, field_mask
,
3647 &info
[0], info_cnt
, level
);
3651 if (field_type
== 0)
3656 if (expected_size
&& expected_size
!= sz
* nelems
)
3661 switch (field_type
) {
3668 ret
= btf_find_struct(btf
, var_type
, off
, sz
, field_type
,
3669 info_cnt
? &info
[0] : &tmp
);
3673 case BPF_KPTR_UNREF
:
3675 case BPF_KPTR_PERCPU
:
3677 ret
= btf_find_kptr(btf
, var_type
, off
, sz
,
3678 info_cnt
? &info
[0] : &tmp
, field_mask
);
3684 ret
= btf_find_graph_root(btf
, var
, var_type
,
3686 info_cnt
? &info
[0] : &tmp
,
3695 if (ret
== BTF_FIELD_IGNORE
)
3700 ret
= btf_repeat_fields(info
, info_cnt
, 1, nelems
- 1, sz
);
3707 static int btf_find_struct_field(const struct btf
*btf
,
3708 const struct btf_type
*t
, u32 field_mask
,
3709 struct btf_field_info
*info
, int info_cnt
,
3713 const struct btf_member
*member
;
3714 u32 i
, off
, seen_mask
= 0;
3716 for_each_member(i
, t
, member
) {
3717 const struct btf_type
*member_type
= btf_type_by_id(btf
,
3720 off
= __btf_member_bit_offset(t
, member
);
3722 /* valid C code cannot generate such BTF */
3726 ret
= btf_find_field_one(btf
, t
, member_type
, i
,
3728 field_mask
, &seen_mask
,
3729 &info
[idx
], info_cnt
- idx
, level
);
3737 static int btf_find_datasec_var(const struct btf
*btf
, const struct btf_type
*t
,
3738 u32 field_mask
, struct btf_field_info
*info
,
3739 int info_cnt
, u32 level
)
3742 const struct btf_var_secinfo
*vsi
;
3743 u32 i
, off
, seen_mask
= 0;
3745 for_each_vsi(i
, t
, vsi
) {
3746 const struct btf_type
*var
= btf_type_by_id(btf
, vsi
->type
);
3747 const struct btf_type
*var_type
= btf_type_by_id(btf
, var
->type
);
3750 ret
= btf_find_field_one(btf
, var
, var_type
, -1, off
, vsi
->size
,
3751 field_mask
, &seen_mask
,
3752 &info
[idx
], info_cnt
- idx
,
3761 static int btf_find_field(const struct btf
*btf
, const struct btf_type
*t
,
3762 u32 field_mask
, struct btf_field_info
*info
,
3765 if (__btf_type_is_struct(t
))
3766 return btf_find_struct_field(btf
, t
, field_mask
, info
, info_cnt
, 0);
3767 else if (btf_type_is_datasec(t
))
3768 return btf_find_datasec_var(btf
, t
, field_mask
, info
, info_cnt
, 0);
3772 /* Callers have to ensure the life cycle of btf if it is program BTF */
3773 static int btf_parse_kptr(const struct btf
*btf
, struct btf_field
*field
,
3774 struct btf_field_info
*info
)
3776 struct module
*mod
= NULL
;
3777 const struct btf_type
*t
;
3778 /* If a matching btf type is found in kernel or module BTFs, kptr_ref
3779 * is that BTF, otherwise it's program BTF
3781 struct btf
*kptr_btf
;
3785 /* Find type in map BTF, and use it to look up the matching type
3786 * in vmlinux or module BTFs, by name and kind.
3788 t
= btf_type_by_id(btf
, info
->kptr
.type_id
);
3789 id
= bpf_find_btf_id(__btf_name_by_offset(btf
, t
->name_off
), BTF_INFO_KIND(t
->info
),
3791 if (id
== -ENOENT
) {
3792 /* btf_parse_kptr should only be called w/ btf = program BTF */
3793 WARN_ON_ONCE(btf_is_kernel(btf
));
3795 /* Type exists only in program BTF. Assume that it's a MEM_ALLOC
3796 * kptr allocated via bpf_obj_new
3798 field
->kptr
.dtor
= NULL
;
3799 id
= info
->kptr
.type_id
;
3800 kptr_btf
= (struct btf
*)btf
;
3806 /* Find and stash the function pointer for the destruction function that
3807 * needs to be eventually invoked from the map free path.
3809 if (info
->type
== BPF_KPTR_REF
) {
3810 const struct btf_type
*dtor_func
;
3811 const char *dtor_func_name
;
3815 /* This call also serves as a whitelist of allowed objects that
3816 * can be used as a referenced pointer and be stored in a map at
3819 dtor_btf_id
= btf_find_dtor_kfunc(kptr_btf
, id
);
3820 if (dtor_btf_id
< 0) {
3825 dtor_func
= btf_type_by_id(kptr_btf
, dtor_btf_id
);
3831 if (btf_is_module(kptr_btf
)) {
3832 mod
= btf_try_get_module(kptr_btf
);
3839 /* We already verified dtor_func to be btf_type_is_func
3840 * in register_btf_id_dtor_kfuncs.
3842 dtor_func_name
= __btf_name_by_offset(kptr_btf
, dtor_func
->name_off
);
3843 addr
= kallsyms_lookup_name(dtor_func_name
);
3848 field
->kptr
.dtor
= (void *)addr
;
3852 field
->kptr
.btf_id
= id
;
3853 field
->kptr
.btf
= kptr_btf
;
3854 field
->kptr
.module
= mod
;
3863 static int btf_parse_graph_root(const struct btf
*btf
,
3864 struct btf_field
*field
,
3865 struct btf_field_info
*info
,
3866 const char *node_type_name
,
3867 size_t node_type_align
)
3869 const struct btf_type
*t
, *n
= NULL
;
3870 const struct btf_member
*member
;
3874 t
= btf_type_by_id(btf
, info
->graph_root
.value_btf_id
);
3875 /* We've already checked that value_btf_id is a struct type. We
3876 * just need to figure out the offset of the list_node, and
3879 for_each_member(i
, t
, member
) {
3880 if (strcmp(info
->graph_root
.node_name
,
3881 __btf_name_by_offset(btf
, member
->name_off
)))
3883 /* Invalid BTF, two members with same name */
3886 n
= btf_type_by_id(btf
, member
->type
);
3887 if (!__btf_type_is_struct(n
))
3889 if (strcmp(node_type_name
, __btf_name_by_offset(btf
, n
->name_off
)))
3891 offset
= __btf_member_bit_offset(n
, member
);
3895 if (offset
% node_type_align
)
3898 field
->graph_root
.btf
= (struct btf
*)btf
;
3899 field
->graph_root
.value_btf_id
= info
->graph_root
.value_btf_id
;
3900 field
->graph_root
.node_offset
= offset
;
3907 static int btf_parse_list_head(const struct btf
*btf
, struct btf_field
*field
,
3908 struct btf_field_info
*info
)
3910 return btf_parse_graph_root(btf
, field
, info
, "bpf_list_node",
3911 __alignof__(struct bpf_list_node
));
3914 static int btf_parse_rb_root(const struct btf
*btf
, struct btf_field
*field
,
3915 struct btf_field_info
*info
)
3917 return btf_parse_graph_root(btf
, field
, info
, "bpf_rb_node",
3918 __alignof__(struct bpf_rb_node
));
3921 static int btf_field_cmp(const void *_a
, const void *_b
, const void *priv
)
3923 const struct btf_field
*a
= (const struct btf_field
*)_a
;
3924 const struct btf_field
*b
= (const struct btf_field
*)_b
;
3926 if (a
->offset
< b
->offset
)
3928 else if (a
->offset
> b
->offset
)
3933 struct btf_record
*btf_parse_fields(const struct btf
*btf
, const struct btf_type
*t
,
3934 u32 field_mask
, u32 value_size
)
3936 struct btf_field_info info_arr
[BTF_FIELDS_MAX
];
3937 u32 next_off
= 0, field_type_size
;
3938 struct btf_record
*rec
;
3941 ret
= btf_find_field(btf
, t
, field_mask
, info_arr
, ARRAY_SIZE(info_arr
));
3943 return ERR_PTR(ret
);
3948 /* This needs to be kzalloc to zero out padding and unused fields, see
3949 * comment in btf_record_equal.
3951 rec
= kzalloc(offsetof(struct btf_record
, fields
[cnt
]), GFP_KERNEL
| __GFP_NOWARN
);
3953 return ERR_PTR(-ENOMEM
);
3955 rec
->spin_lock_off
= -EINVAL
;
3956 rec
->timer_off
= -EINVAL
;
3957 rec
->wq_off
= -EINVAL
;
3958 rec
->refcount_off
= -EINVAL
;
3959 for (i
= 0; i
< cnt
; i
++) {
3960 field_type_size
= btf_field_type_size(info_arr
[i
].type
);
3961 if (info_arr
[i
].off
+ field_type_size
> value_size
) {
3962 WARN_ONCE(1, "verifier bug off %d size %d", info_arr
[i
].off
, value_size
);
3966 if (info_arr
[i
].off
< next_off
) {
3970 next_off
= info_arr
[i
].off
+ field_type_size
;
3972 rec
->field_mask
|= info_arr
[i
].type
;
3973 rec
->fields
[i
].offset
= info_arr
[i
].off
;
3974 rec
->fields
[i
].type
= info_arr
[i
].type
;
3975 rec
->fields
[i
].size
= field_type_size
;
3977 switch (info_arr
[i
].type
) {
3979 WARN_ON_ONCE(rec
->spin_lock_off
>= 0);
3980 /* Cache offset for faster lookup at runtime */
3981 rec
->spin_lock_off
= rec
->fields
[i
].offset
;
3984 WARN_ON_ONCE(rec
->timer_off
>= 0);
3985 /* Cache offset for faster lookup at runtime */
3986 rec
->timer_off
= rec
->fields
[i
].offset
;
3989 WARN_ON_ONCE(rec
->wq_off
>= 0);
3990 /* Cache offset for faster lookup at runtime */
3991 rec
->wq_off
= rec
->fields
[i
].offset
;
3994 WARN_ON_ONCE(rec
->refcount_off
>= 0);
3995 /* Cache offset for faster lookup at runtime */
3996 rec
->refcount_off
= rec
->fields
[i
].offset
;
3998 case BPF_KPTR_UNREF
:
4000 case BPF_KPTR_PERCPU
:
4002 ret
= btf_parse_kptr(btf
, &rec
->fields
[i
], &info_arr
[i
]);
4007 ret
= btf_parse_list_head(btf
, &rec
->fields
[i
], &info_arr
[i
]);
4012 ret
= btf_parse_rb_root(btf
, &rec
->fields
[i
], &info_arr
[i
]);
4026 /* bpf_{list_head, rb_node} require bpf_spin_lock */
4027 if ((btf_record_has_field(rec
, BPF_LIST_HEAD
) ||
4028 btf_record_has_field(rec
, BPF_RB_ROOT
)) && rec
->spin_lock_off
< 0) {
4033 if (rec
->refcount_off
< 0 &&
4034 btf_record_has_field(rec
, BPF_LIST_NODE
) &&
4035 btf_record_has_field(rec
, BPF_RB_NODE
)) {
4040 sort_r(rec
->fields
, rec
->cnt
, sizeof(struct btf_field
), btf_field_cmp
,
4045 btf_record_free(rec
);
4046 return ERR_PTR(ret
);
4049 int btf_check_and_fixup_fields(const struct btf
*btf
, struct btf_record
*rec
)
4053 /* There are three types that signify ownership of some other type:
4054 * kptr_ref, bpf_list_head, bpf_rb_root.
4055 * kptr_ref only supports storing kernel types, which can't store
4056 * references to program allocated local types.
4058 * Hence we only need to ensure that bpf_{list_head,rb_root} ownership
4059 * does not form cycles.
4061 if (IS_ERR_OR_NULL(rec
) || !(rec
->field_mask
& (BPF_GRAPH_ROOT
| BPF_UPTR
)))
4063 for (i
= 0; i
< rec
->cnt
; i
++) {
4064 struct btf_struct_meta
*meta
;
4065 const struct btf_type
*t
;
4068 if (rec
->fields
[i
].type
== BPF_UPTR
) {
4069 /* The uptr only supports pinning one page and cannot
4070 * point to a kernel struct
4072 if (btf_is_kernel(rec
->fields
[i
].kptr
.btf
))
4074 t
= btf_type_by_id(rec
->fields
[i
].kptr
.btf
,
4075 rec
->fields
[i
].kptr
.btf_id
);
4078 if (t
->size
> PAGE_SIZE
)
4083 if (!(rec
->fields
[i
].type
& BPF_GRAPH_ROOT
))
4085 btf_id
= rec
->fields
[i
].graph_root
.value_btf_id
;
4086 meta
= btf_find_struct_meta(btf
, btf_id
);
4089 rec
->fields
[i
].graph_root
.value_rec
= meta
->record
;
4091 /* We need to set value_rec for all root types, but no need
4092 * to check ownership cycle for a type unless it's also a
4095 if (!(rec
->field_mask
& BPF_GRAPH_NODE
))
4098 /* We need to ensure ownership acyclicity among all types. The
4099 * proper way to do it would be to topologically sort all BTF
4100 * IDs based on the ownership edges, since there can be multiple
4101 * bpf_{list_head,rb_node} in a type. Instead, we use the
4102 * following resaoning:
4104 * - A type can only be owned by another type in user BTF if it
4105 * has a bpf_{list,rb}_node. Let's call these node types.
4106 * - A type can only _own_ another type in user BTF if it has a
4107 * bpf_{list_head,rb_root}. Let's call these root types.
4109 * We ensure that if a type is both a root and node, its
4110 * element types cannot be root types.
4112 * To ensure acyclicity:
4114 * When A is an root type but not a node, its ownership
4118 * - A is an root, e.g. has bpf_rb_root.
4119 * - B is both a root and node, e.g. has bpf_rb_node and
4121 * - C is only an root, e.g. has bpf_list_node
4123 * When A is both a root and node, some other type already
4124 * owns it in the BTF domain, hence it can not own
4125 * another root type through any of the ownership edges.
4128 * - A is both an root and node.
4129 * - B is only an node.
4131 if (meta
->record
->field_mask
& BPF_GRAPH_ROOT
)
4137 static void __btf_struct_show(const struct btf
*btf
, const struct btf_type
*t
,
4138 u32 type_id
, void *data
, u8 bits_offset
,
4139 struct btf_show
*show
)
4141 const struct btf_member
*member
;
4145 safe_data
= btf_show_start_struct_type(show
, t
, type_id
, data
);
4149 for_each_member(i
, t
, member
) {
4150 const struct btf_type
*member_type
= btf_type_by_id(btf
,
4152 const struct btf_kind_operations
*ops
;
4153 u32 member_offset
, bitfield_size
;
4157 btf_show_start_member(show
, member
);
4159 member_offset
= __btf_member_bit_offset(t
, member
);
4160 bitfield_size
= __btf_member_bitfield_size(t
, member
);
4161 bytes_offset
= BITS_ROUNDDOWN_BYTES(member_offset
);
4162 bits8_offset
= BITS_PER_BYTE_MASKED(member_offset
);
4163 if (bitfield_size
) {
4164 safe_data
= btf_show_start_type(show
, member_type
,
4166 data
+ bytes_offset
);
4168 btf_bitfield_show(safe_data
,
4170 bitfield_size
, show
);
4171 btf_show_end_type(show
);
4173 ops
= btf_type_ops(member_type
);
4174 ops
->show(btf
, member_type
, member
->type
,
4175 data
+ bytes_offset
, bits8_offset
, show
);
4178 btf_show_end_member(show
);
4181 btf_show_end_struct_type(show
);
4184 static void btf_struct_show(const struct btf
*btf
, const struct btf_type
*t
,
4185 u32 type_id
, void *data
, u8 bits_offset
,
4186 struct btf_show
*show
)
4188 const struct btf_member
*m
= show
->state
.member
;
4191 * First check if any members would be shown (are non-zero).
4192 * See comments above "struct btf_show" definition for more
4193 * details on how this works at a high-level.
4195 if (show
->state
.depth
> 0 && !(show
->flags
& BTF_SHOW_ZERO
)) {
4196 if (!show
->state
.depth_check
) {
4197 show
->state
.depth_check
= show
->state
.depth
+ 1;
4198 show
->state
.depth_to_show
= 0;
4200 __btf_struct_show(btf
, t
, type_id
, data
, bits_offset
, show
);
4201 /* Restore saved member data here */
4202 show
->state
.member
= m
;
4203 if (show
->state
.depth_check
!= show
->state
.depth
+ 1)
4205 show
->state
.depth_check
= 0;
4207 if (show
->state
.depth_to_show
<= show
->state
.depth
)
4210 * Reaching here indicates we have recursed and found
4211 * non-zero child values.
4215 __btf_struct_show(btf
, t
, type_id
, data
, bits_offset
, show
);
4218 static const struct btf_kind_operations struct_ops
= {
4219 .check_meta
= btf_struct_check_meta
,
4220 .resolve
= btf_struct_resolve
,
4221 .check_member
= btf_struct_check_member
,
4222 .check_kflag_member
= btf_generic_check_kflag_member
,
4223 .log_details
= btf_struct_log
,
4224 .show
= btf_struct_show
,
4227 static int btf_enum_check_member(struct btf_verifier_env
*env
,
4228 const struct btf_type
*struct_type
,
4229 const struct btf_member
*member
,
4230 const struct btf_type
*member_type
)
4232 u32 struct_bits_off
= member
->offset
;
4233 u32 struct_size
, bytes_offset
;
4235 if (BITS_PER_BYTE_MASKED(struct_bits_off
)) {
4236 btf_verifier_log_member(env
, struct_type
, member
,
4237 "Member is not byte aligned");
4241 struct_size
= struct_type
->size
;
4242 bytes_offset
= BITS_ROUNDDOWN_BYTES(struct_bits_off
);
4243 if (struct_size
- bytes_offset
< member_type
->size
) {
4244 btf_verifier_log_member(env
, struct_type
, member
,
4245 "Member exceeds struct_size");
4252 static int btf_enum_check_kflag_member(struct btf_verifier_env
*env
,
4253 const struct btf_type
*struct_type
,
4254 const struct btf_member
*member
,
4255 const struct btf_type
*member_type
)
4257 u32 struct_bits_off
, nr_bits
, bytes_end
, struct_size
;
4258 u32 int_bitsize
= sizeof(int) * BITS_PER_BYTE
;
4260 struct_bits_off
= BTF_MEMBER_BIT_OFFSET(member
->offset
);
4261 nr_bits
= BTF_MEMBER_BITFIELD_SIZE(member
->offset
);
4263 if (BITS_PER_BYTE_MASKED(struct_bits_off
)) {
4264 btf_verifier_log_member(env
, struct_type
, member
,
4265 "Member is not byte aligned");
4269 nr_bits
= int_bitsize
;
4270 } else if (nr_bits
> int_bitsize
) {
4271 btf_verifier_log_member(env
, struct_type
, member
,
4272 "Invalid member bitfield_size");
4276 struct_size
= struct_type
->size
;
4277 bytes_end
= BITS_ROUNDUP_BYTES(struct_bits_off
+ nr_bits
);
4278 if (struct_size
< bytes_end
) {
4279 btf_verifier_log_member(env
, struct_type
, member
,
4280 "Member exceeds struct_size");
4287 static s32
btf_enum_check_meta(struct btf_verifier_env
*env
,
4288 const struct btf_type
*t
,
4291 const struct btf_enum
*enums
= btf_type_enum(t
);
4292 struct btf
*btf
= env
->btf
;
4293 const char *fmt_str
;
4297 nr_enums
= btf_type_vlen(t
);
4298 meta_needed
= nr_enums
* sizeof(*enums
);
4300 if (meta_left
< meta_needed
) {
4301 btf_verifier_log_basic(env
, t
,
4302 "meta_left:%u meta_needed:%u",
4303 meta_left
, meta_needed
);
4307 if (t
->size
> 8 || !is_power_of_2(t
->size
)) {
4308 btf_verifier_log_type(env
, t
, "Unexpected size");
4312 /* enum type either no name or a valid one */
4314 !btf_name_valid_identifier(env
->btf
, t
->name_off
)) {
4315 btf_verifier_log_type(env
, t
, "Invalid name");
4319 btf_verifier_log_type(env
, t
, NULL
);
4321 for (i
= 0; i
< nr_enums
; i
++) {
4322 if (!btf_name_offset_valid(btf
, enums
[i
].name_off
)) {
4323 btf_verifier_log(env
, "\tInvalid name_offset:%u",
4328 /* enum member must have a valid name */
4329 if (!enums
[i
].name_off
||
4330 !btf_name_valid_identifier(btf
, enums
[i
].name_off
)) {
4331 btf_verifier_log_type(env
, t
, "Invalid name");
4335 if (env
->log
.level
== BPF_LOG_KERNEL
)
4337 fmt_str
= btf_type_kflag(t
) ? "\t%s val=%d\n" : "\t%s val=%u\n";
4338 btf_verifier_log(env
, fmt_str
,
4339 __btf_name_by_offset(btf
, enums
[i
].name_off
),
4346 static void btf_enum_log(struct btf_verifier_env
*env
,
4347 const struct btf_type
*t
)
4349 btf_verifier_log(env
, "size=%u vlen=%u", t
->size
, btf_type_vlen(t
));
4352 static void btf_enum_show(const struct btf
*btf
, const struct btf_type
*t
,
4353 u32 type_id
, void *data
, u8 bits_offset
,
4354 struct btf_show
*show
)
4356 const struct btf_enum
*enums
= btf_type_enum(t
);
4357 u32 i
, nr_enums
= btf_type_vlen(t
);
4361 safe_data
= btf_show_start_type(show
, t
, type_id
, data
);
4365 v
= *(int *)safe_data
;
4367 for (i
= 0; i
< nr_enums
; i
++) {
4368 if (v
!= enums
[i
].val
)
4371 btf_show_type_value(show
, "%s",
4372 __btf_name_by_offset(btf
,
4373 enums
[i
].name_off
));
4375 btf_show_end_type(show
);
4379 if (btf_type_kflag(t
))
4380 btf_show_type_value(show
, "%d", v
);
4382 btf_show_type_value(show
, "%u", v
);
4383 btf_show_end_type(show
);
4386 static const struct btf_kind_operations enum_ops
= {
4387 .check_meta
= btf_enum_check_meta
,
4388 .resolve
= btf_df_resolve
,
4389 .check_member
= btf_enum_check_member
,
4390 .check_kflag_member
= btf_enum_check_kflag_member
,
4391 .log_details
= btf_enum_log
,
4392 .show
= btf_enum_show
,
4395 static s32
btf_enum64_check_meta(struct btf_verifier_env
*env
,
4396 const struct btf_type
*t
,
4399 const struct btf_enum64
*enums
= btf_type_enum64(t
);
4400 struct btf
*btf
= env
->btf
;
4401 const char *fmt_str
;
4405 nr_enums
= btf_type_vlen(t
);
4406 meta_needed
= nr_enums
* sizeof(*enums
);
4408 if (meta_left
< meta_needed
) {
4409 btf_verifier_log_basic(env
, t
,
4410 "meta_left:%u meta_needed:%u",
4411 meta_left
, meta_needed
);
4415 if (t
->size
> 8 || !is_power_of_2(t
->size
)) {
4416 btf_verifier_log_type(env
, t
, "Unexpected size");
4420 /* enum type either no name or a valid one */
4422 !btf_name_valid_identifier(env
->btf
, t
->name_off
)) {
4423 btf_verifier_log_type(env
, t
, "Invalid name");
4427 btf_verifier_log_type(env
, t
, NULL
);
4429 for (i
= 0; i
< nr_enums
; i
++) {
4430 if (!btf_name_offset_valid(btf
, enums
[i
].name_off
)) {
4431 btf_verifier_log(env
, "\tInvalid name_offset:%u",
4436 /* enum member must have a valid name */
4437 if (!enums
[i
].name_off
||
4438 !btf_name_valid_identifier(btf
, enums
[i
].name_off
)) {
4439 btf_verifier_log_type(env
, t
, "Invalid name");
4443 if (env
->log
.level
== BPF_LOG_KERNEL
)
4446 fmt_str
= btf_type_kflag(t
) ? "\t%s val=%lld\n" : "\t%s val=%llu\n";
4447 btf_verifier_log(env
, fmt_str
,
4448 __btf_name_by_offset(btf
, enums
[i
].name_off
),
4449 btf_enum64_value(enums
+ i
));
4455 static void btf_enum64_show(const struct btf
*btf
, const struct btf_type
*t
,
4456 u32 type_id
, void *data
, u8 bits_offset
,
4457 struct btf_show
*show
)
4459 const struct btf_enum64
*enums
= btf_type_enum64(t
);
4460 u32 i
, nr_enums
= btf_type_vlen(t
);
4464 safe_data
= btf_show_start_type(show
, t
, type_id
, data
);
4468 v
= *(u64
*)safe_data
;
4470 for (i
= 0; i
< nr_enums
; i
++) {
4471 if (v
!= btf_enum64_value(enums
+ i
))
4474 btf_show_type_value(show
, "%s",
4475 __btf_name_by_offset(btf
,
4476 enums
[i
].name_off
));
4478 btf_show_end_type(show
);
4482 if (btf_type_kflag(t
))
4483 btf_show_type_value(show
, "%lld", v
);
4485 btf_show_type_value(show
, "%llu", v
);
4486 btf_show_end_type(show
);
4489 static const struct btf_kind_operations enum64_ops
= {
4490 .check_meta
= btf_enum64_check_meta
,
4491 .resolve
= btf_df_resolve
,
4492 .check_member
= btf_enum_check_member
,
4493 .check_kflag_member
= btf_enum_check_kflag_member
,
4494 .log_details
= btf_enum_log
,
4495 .show
= btf_enum64_show
,
4498 static s32
btf_func_proto_check_meta(struct btf_verifier_env
*env
,
4499 const struct btf_type
*t
,
4502 u32 meta_needed
= btf_type_vlen(t
) * sizeof(struct btf_param
);
4504 if (meta_left
< meta_needed
) {
4505 btf_verifier_log_basic(env
, t
,
4506 "meta_left:%u meta_needed:%u",
4507 meta_left
, meta_needed
);
4512 btf_verifier_log_type(env
, t
, "Invalid name");
4516 if (btf_type_kflag(t
)) {
4517 btf_verifier_log_type(env
, t
, "Invalid btf_info kind_flag");
4521 btf_verifier_log_type(env
, t
, NULL
);
4526 static void btf_func_proto_log(struct btf_verifier_env
*env
,
4527 const struct btf_type
*t
)
4529 const struct btf_param
*args
= (const struct btf_param
*)(t
+ 1);
4530 u16 nr_args
= btf_type_vlen(t
), i
;
4532 btf_verifier_log(env
, "return=%u args=(", t
->type
);
4534 btf_verifier_log(env
, "void");
4538 if (nr_args
== 1 && !args
[0].type
) {
4539 /* Only one vararg */
4540 btf_verifier_log(env
, "vararg");
4544 btf_verifier_log(env
, "%u %s", args
[0].type
,
4545 __btf_name_by_offset(env
->btf
,
4547 for (i
= 1; i
< nr_args
- 1; i
++)
4548 btf_verifier_log(env
, ", %u %s", args
[i
].type
,
4549 __btf_name_by_offset(env
->btf
,
4553 const struct btf_param
*last_arg
= &args
[nr_args
- 1];
4556 btf_verifier_log(env
, ", %u %s", last_arg
->type
,
4557 __btf_name_by_offset(env
->btf
,
4558 last_arg
->name_off
));
4560 btf_verifier_log(env
, ", vararg");
4564 btf_verifier_log(env
, ")");
4567 static const struct btf_kind_operations func_proto_ops
= {
4568 .check_meta
= btf_func_proto_check_meta
,
4569 .resolve
= btf_df_resolve
,
4571 * BTF_KIND_FUNC_PROTO cannot be directly referred by
4572 * a struct's member.
4574 * It should be a function pointer instead.
4575 * (i.e. struct's member -> BTF_KIND_PTR -> BTF_KIND_FUNC_PROTO)
4577 * Hence, there is no btf_func_check_member().
4579 .check_member
= btf_df_check_member
,
4580 .check_kflag_member
= btf_df_check_kflag_member
,
4581 .log_details
= btf_func_proto_log
,
4582 .show
= btf_df_show
,
4585 static s32
btf_func_check_meta(struct btf_verifier_env
*env
,
4586 const struct btf_type
*t
,
4590 !btf_name_valid_identifier(env
->btf
, t
->name_off
)) {
4591 btf_verifier_log_type(env
, t
, "Invalid name");
4595 if (btf_type_vlen(t
) > BTF_FUNC_GLOBAL
) {
4596 btf_verifier_log_type(env
, t
, "Invalid func linkage");
4600 if (btf_type_kflag(t
)) {
4601 btf_verifier_log_type(env
, t
, "Invalid btf_info kind_flag");
4605 btf_verifier_log_type(env
, t
, NULL
);
4610 static int btf_func_resolve(struct btf_verifier_env
*env
,
4611 const struct resolve_vertex
*v
)
4613 const struct btf_type
*t
= v
->t
;
4614 u32 next_type_id
= t
->type
;
4617 err
= btf_func_check(env
, t
);
4621 env_stack_pop_resolved(env
, next_type_id
, 0);
4625 static const struct btf_kind_operations func_ops
= {
4626 .check_meta
= btf_func_check_meta
,
4627 .resolve
= btf_func_resolve
,
4628 .check_member
= btf_df_check_member
,
4629 .check_kflag_member
= btf_df_check_kflag_member
,
4630 .log_details
= btf_ref_type_log
,
4631 .show
= btf_df_show
,
4634 static s32
btf_var_check_meta(struct btf_verifier_env
*env
,
4635 const struct btf_type
*t
,
4638 const struct btf_var
*var
;
4639 u32 meta_needed
= sizeof(*var
);
4641 if (meta_left
< meta_needed
) {
4642 btf_verifier_log_basic(env
, t
,
4643 "meta_left:%u meta_needed:%u",
4644 meta_left
, meta_needed
);
4648 if (btf_type_vlen(t
)) {
4649 btf_verifier_log_type(env
, t
, "vlen != 0");
4653 if (btf_type_kflag(t
)) {
4654 btf_verifier_log_type(env
, t
, "Invalid btf_info kind_flag");
4659 !btf_name_valid_identifier(env
->btf
, t
->name_off
)) {
4660 btf_verifier_log_type(env
, t
, "Invalid name");
4664 /* A var cannot be in type void */
4665 if (!t
->type
|| !BTF_TYPE_ID_VALID(t
->type
)) {
4666 btf_verifier_log_type(env
, t
, "Invalid type_id");
4670 var
= btf_type_var(t
);
4671 if (var
->linkage
!= BTF_VAR_STATIC
&&
4672 var
->linkage
!= BTF_VAR_GLOBAL_ALLOCATED
) {
4673 btf_verifier_log_type(env
, t
, "Linkage not supported");
4677 btf_verifier_log_type(env
, t
, NULL
);
4682 static void btf_var_log(struct btf_verifier_env
*env
, const struct btf_type
*t
)
4684 const struct btf_var
*var
= btf_type_var(t
);
4686 btf_verifier_log(env
, "type_id=%u linkage=%u", t
->type
, var
->linkage
);
4689 static const struct btf_kind_operations var_ops
= {
4690 .check_meta
= btf_var_check_meta
,
4691 .resolve
= btf_var_resolve
,
4692 .check_member
= btf_df_check_member
,
4693 .check_kflag_member
= btf_df_check_kflag_member
,
4694 .log_details
= btf_var_log
,
4695 .show
= btf_var_show
,
4698 static s32
btf_datasec_check_meta(struct btf_verifier_env
*env
,
4699 const struct btf_type
*t
,
4702 const struct btf_var_secinfo
*vsi
;
4703 u64 last_vsi_end_off
= 0, sum
= 0;
4706 meta_needed
= btf_type_vlen(t
) * sizeof(*vsi
);
4707 if (meta_left
< meta_needed
) {
4708 btf_verifier_log_basic(env
, t
,
4709 "meta_left:%u meta_needed:%u",
4710 meta_left
, meta_needed
);
4715 btf_verifier_log_type(env
, t
, "size == 0");
4719 if (btf_type_kflag(t
)) {
4720 btf_verifier_log_type(env
, t
, "Invalid btf_info kind_flag");
4725 !btf_name_valid_section(env
->btf
, t
->name_off
)) {
4726 btf_verifier_log_type(env
, t
, "Invalid name");
4730 btf_verifier_log_type(env
, t
, NULL
);
4732 for_each_vsi(i
, t
, vsi
) {
4733 /* A var cannot be in type void */
4734 if (!vsi
->type
|| !BTF_TYPE_ID_VALID(vsi
->type
)) {
4735 btf_verifier_log_vsi(env
, t
, vsi
,
4740 if (vsi
->offset
< last_vsi_end_off
|| vsi
->offset
>= t
->size
) {
4741 btf_verifier_log_vsi(env
, t
, vsi
,
4746 if (!vsi
->size
|| vsi
->size
> t
->size
) {
4747 btf_verifier_log_vsi(env
, t
, vsi
,
4752 last_vsi_end_off
= vsi
->offset
+ vsi
->size
;
4753 if (last_vsi_end_off
> t
->size
) {
4754 btf_verifier_log_vsi(env
, t
, vsi
,
4755 "Invalid offset+size");
4759 btf_verifier_log_vsi(env
, t
, vsi
, NULL
);
4763 if (t
->size
< sum
) {
4764 btf_verifier_log_type(env
, t
, "Invalid btf_info size");
4771 static int btf_datasec_resolve(struct btf_verifier_env
*env
,
4772 const struct resolve_vertex
*v
)
4774 const struct btf_var_secinfo
*vsi
;
4775 struct btf
*btf
= env
->btf
;
4778 env
->resolve_mode
= RESOLVE_TBD
;
4779 for_each_vsi_from(i
, v
->next_member
, v
->t
, vsi
) {
4780 u32 var_type_id
= vsi
->type
, type_id
, type_size
= 0;
4781 const struct btf_type
*var_type
= btf_type_by_id(env
->btf
,
4783 if (!var_type
|| !btf_type_is_var(var_type
)) {
4784 btf_verifier_log_vsi(env
, v
->t
, vsi
,
4785 "Not a VAR kind member");
4789 if (!env_type_is_resolve_sink(env
, var_type
) &&
4790 !env_type_is_resolved(env
, var_type_id
)) {
4791 env_stack_set_next_member(env
, i
+ 1);
4792 return env_stack_push(env
, var_type
, var_type_id
);
4795 type_id
= var_type
->type
;
4796 if (!btf_type_id_size(btf
, &type_id
, &type_size
)) {
4797 btf_verifier_log_vsi(env
, v
->t
, vsi
, "Invalid type");
4801 if (vsi
->size
< type_size
) {
4802 btf_verifier_log_vsi(env
, v
->t
, vsi
, "Invalid size");
4807 env_stack_pop_resolved(env
, 0, 0);
4811 static void btf_datasec_log(struct btf_verifier_env
*env
,
4812 const struct btf_type
*t
)
4814 btf_verifier_log(env
, "size=%u vlen=%u", t
->size
, btf_type_vlen(t
));
4817 static void btf_datasec_show(const struct btf
*btf
,
4818 const struct btf_type
*t
, u32 type_id
,
4819 void *data
, u8 bits_offset
,
4820 struct btf_show
*show
)
4822 const struct btf_var_secinfo
*vsi
;
4823 const struct btf_type
*var
;
4826 if (!btf_show_start_type(show
, t
, type_id
, data
))
4829 btf_show_type_value(show
, "section (\"%s\") = {",
4830 __btf_name_by_offset(btf
, t
->name_off
));
4831 for_each_vsi(i
, t
, vsi
) {
4832 var
= btf_type_by_id(btf
, vsi
->type
);
4834 btf_show(show
, ",");
4835 btf_type_ops(var
)->show(btf
, var
, vsi
->type
,
4836 data
+ vsi
->offset
, bits_offset
, show
);
4838 btf_show_end_type(show
);
4841 static const struct btf_kind_operations datasec_ops
= {
4842 .check_meta
= btf_datasec_check_meta
,
4843 .resolve
= btf_datasec_resolve
,
4844 .check_member
= btf_df_check_member
,
4845 .check_kflag_member
= btf_df_check_kflag_member
,
4846 .log_details
= btf_datasec_log
,
4847 .show
= btf_datasec_show
,
4850 static s32
btf_float_check_meta(struct btf_verifier_env
*env
,
4851 const struct btf_type
*t
,
4854 if (btf_type_vlen(t
)) {
4855 btf_verifier_log_type(env
, t
, "vlen != 0");
4859 if (btf_type_kflag(t
)) {
4860 btf_verifier_log_type(env
, t
, "Invalid btf_info kind_flag");
4864 if (t
->size
!= 2 && t
->size
!= 4 && t
->size
!= 8 && t
->size
!= 12 &&
4866 btf_verifier_log_type(env
, t
, "Invalid type_size");
4870 btf_verifier_log_type(env
, t
, NULL
);
4875 static int btf_float_check_member(struct btf_verifier_env
*env
,
4876 const struct btf_type
*struct_type
,
4877 const struct btf_member
*member
,
4878 const struct btf_type
*member_type
)
4880 u64 start_offset_bytes
;
4881 u64 end_offset_bytes
;
4886 /* Different architectures have different alignment requirements, so
4887 * here we check only for the reasonable minimum. This way we ensure
4888 * that types after CO-RE can pass the kernel BTF verifier.
4890 align_bytes
= min_t(u64
, sizeof(void *), member_type
->size
);
4891 align_bits
= align_bytes
* BITS_PER_BYTE
;
4892 div64_u64_rem(member
->offset
, align_bits
, &misalign_bits
);
4893 if (misalign_bits
) {
4894 btf_verifier_log_member(env
, struct_type
, member
,
4895 "Member is not properly aligned");
4899 start_offset_bytes
= member
->offset
/ BITS_PER_BYTE
;
4900 end_offset_bytes
= start_offset_bytes
+ member_type
->size
;
4901 if (end_offset_bytes
> struct_type
->size
) {
4902 btf_verifier_log_member(env
, struct_type
, member
,
4903 "Member exceeds struct_size");
4910 static void btf_float_log(struct btf_verifier_env
*env
,
4911 const struct btf_type
*t
)
4913 btf_verifier_log(env
, "size=%u", t
->size
);
4916 static const struct btf_kind_operations float_ops
= {
4917 .check_meta
= btf_float_check_meta
,
4918 .resolve
= btf_df_resolve
,
4919 .check_member
= btf_float_check_member
,
4920 .check_kflag_member
= btf_generic_check_kflag_member
,
4921 .log_details
= btf_float_log
,
4922 .show
= btf_df_show
,
4925 static s32
btf_decl_tag_check_meta(struct btf_verifier_env
*env
,
4926 const struct btf_type
*t
,
4929 const struct btf_decl_tag
*tag
;
4930 u32 meta_needed
= sizeof(*tag
);
4934 if (meta_left
< meta_needed
) {
4935 btf_verifier_log_basic(env
, t
,
4936 "meta_left:%u meta_needed:%u",
4937 meta_left
, meta_needed
);
4941 value
= btf_name_by_offset(env
->btf
, t
->name_off
);
4942 if (!value
|| !value
[0]) {
4943 btf_verifier_log_type(env
, t
, "Invalid value");
4947 if (btf_type_vlen(t
)) {
4948 btf_verifier_log_type(env
, t
, "vlen != 0");
4952 if (btf_type_kflag(t
)) {
4953 btf_verifier_log_type(env
, t
, "Invalid btf_info kind_flag");
4957 component_idx
= btf_type_decl_tag(t
)->component_idx
;
4958 if (component_idx
< -1) {
4959 btf_verifier_log_type(env
, t
, "Invalid component_idx");
4963 btf_verifier_log_type(env
, t
, NULL
);
4968 static int btf_decl_tag_resolve(struct btf_verifier_env
*env
,
4969 const struct resolve_vertex
*v
)
4971 const struct btf_type
*next_type
;
4972 const struct btf_type
*t
= v
->t
;
4973 u32 next_type_id
= t
->type
;
4974 struct btf
*btf
= env
->btf
;
4978 next_type
= btf_type_by_id(btf
, next_type_id
);
4979 if (!next_type
|| !btf_type_is_decl_tag_target(next_type
)) {
4980 btf_verifier_log_type(env
, v
->t
, "Invalid type_id");
4984 if (!env_type_is_resolve_sink(env
, next_type
) &&
4985 !env_type_is_resolved(env
, next_type_id
))
4986 return env_stack_push(env
, next_type
, next_type_id
);
4988 component_idx
= btf_type_decl_tag(t
)->component_idx
;
4989 if (component_idx
!= -1) {
4990 if (btf_type_is_var(next_type
) || btf_type_is_typedef(next_type
)) {
4991 btf_verifier_log_type(env
, v
->t
, "Invalid component_idx");
4995 if (btf_type_is_struct(next_type
)) {
4996 vlen
= btf_type_vlen(next_type
);
4998 /* next_type should be a function */
4999 next_type
= btf_type_by_id(btf
, next_type
->type
);
5000 vlen
= btf_type_vlen(next_type
);
5003 if ((u32
)component_idx
>= vlen
) {
5004 btf_verifier_log_type(env
, v
->t
, "Invalid component_idx");
5009 env_stack_pop_resolved(env
, next_type_id
, 0);
5014 static void btf_decl_tag_log(struct btf_verifier_env
*env
, const struct btf_type
*t
)
5016 btf_verifier_log(env
, "type=%u component_idx=%d", t
->type
,
5017 btf_type_decl_tag(t
)->component_idx
);
5020 static const struct btf_kind_operations decl_tag_ops
= {
5021 .check_meta
= btf_decl_tag_check_meta
,
5022 .resolve
= btf_decl_tag_resolve
,
5023 .check_member
= btf_df_check_member
,
5024 .check_kflag_member
= btf_df_check_kflag_member
,
5025 .log_details
= btf_decl_tag_log
,
5026 .show
= btf_df_show
,
5029 static int btf_func_proto_check(struct btf_verifier_env
*env
,
5030 const struct btf_type
*t
)
5032 const struct btf_type
*ret_type
;
5033 const struct btf_param
*args
;
5034 const struct btf
*btf
;
5039 args
= (const struct btf_param
*)(t
+ 1);
5040 nr_args
= btf_type_vlen(t
);
5042 /* Check func return type which could be "void" (t->type == 0) */
5044 u32 ret_type_id
= t
->type
;
5046 ret_type
= btf_type_by_id(btf
, ret_type_id
);
5048 btf_verifier_log_type(env
, t
, "Invalid return type");
5052 if (btf_type_is_resolve_source_only(ret_type
)) {
5053 btf_verifier_log_type(env
, t
, "Invalid return type");
5057 if (btf_type_needs_resolve(ret_type
) &&
5058 !env_type_is_resolved(env
, ret_type_id
)) {
5059 err
= btf_resolve(env
, ret_type
, ret_type_id
);
5064 /* Ensure the return type is a type that has a size */
5065 if (!btf_type_id_size(btf
, &ret_type_id
, NULL
)) {
5066 btf_verifier_log_type(env
, t
, "Invalid return type");
5074 /* Last func arg type_id could be 0 if it is a vararg */
5075 if (!args
[nr_args
- 1].type
) {
5076 if (args
[nr_args
- 1].name_off
) {
5077 btf_verifier_log_type(env
, t
, "Invalid arg#%u",
5084 for (i
= 0; i
< nr_args
; i
++) {
5085 const struct btf_type
*arg_type
;
5088 arg_type_id
= args
[i
].type
;
5089 arg_type
= btf_type_by_id(btf
, arg_type_id
);
5091 btf_verifier_log_type(env
, t
, "Invalid arg#%u", i
+ 1);
5095 if (btf_type_is_resolve_source_only(arg_type
)) {
5096 btf_verifier_log_type(env
, t
, "Invalid arg#%u", i
+ 1);
5100 if (args
[i
].name_off
&&
5101 (!btf_name_offset_valid(btf
, args
[i
].name_off
) ||
5102 !btf_name_valid_identifier(btf
, args
[i
].name_off
))) {
5103 btf_verifier_log_type(env
, t
,
5104 "Invalid arg#%u", i
+ 1);
5108 if (btf_type_needs_resolve(arg_type
) &&
5109 !env_type_is_resolved(env
, arg_type_id
)) {
5110 err
= btf_resolve(env
, arg_type
, arg_type_id
);
5115 if (!btf_type_id_size(btf
, &arg_type_id
, NULL
)) {
5116 btf_verifier_log_type(env
, t
, "Invalid arg#%u", i
+ 1);
5124 static int btf_func_check(struct btf_verifier_env
*env
,
5125 const struct btf_type
*t
)
5127 const struct btf_type
*proto_type
;
5128 const struct btf_param
*args
;
5129 const struct btf
*btf
;
5133 proto_type
= btf_type_by_id(btf
, t
->type
);
5135 if (!proto_type
|| !btf_type_is_func_proto(proto_type
)) {
5136 btf_verifier_log_type(env
, t
, "Invalid type_id");
5140 args
= (const struct btf_param
*)(proto_type
+ 1);
5141 nr_args
= btf_type_vlen(proto_type
);
5142 for (i
= 0; i
< nr_args
; i
++) {
5143 if (!args
[i
].name_off
&& args
[i
].type
) {
5144 btf_verifier_log_type(env
, t
, "Invalid arg#%u", i
+ 1);
5152 static const struct btf_kind_operations
* const kind_ops
[NR_BTF_KINDS
] = {
5153 [BTF_KIND_INT
] = &int_ops
,
5154 [BTF_KIND_PTR
] = &ptr_ops
,
5155 [BTF_KIND_ARRAY
] = &array_ops
,
5156 [BTF_KIND_STRUCT
] = &struct_ops
,
5157 [BTF_KIND_UNION
] = &struct_ops
,
5158 [BTF_KIND_ENUM
] = &enum_ops
,
5159 [BTF_KIND_FWD
] = &fwd_ops
,
5160 [BTF_KIND_TYPEDEF
] = &modifier_ops
,
5161 [BTF_KIND_VOLATILE
] = &modifier_ops
,
5162 [BTF_KIND_CONST
] = &modifier_ops
,
5163 [BTF_KIND_RESTRICT
] = &modifier_ops
,
5164 [BTF_KIND_FUNC
] = &func_ops
,
5165 [BTF_KIND_FUNC_PROTO
] = &func_proto_ops
,
5166 [BTF_KIND_VAR
] = &var_ops
,
5167 [BTF_KIND_DATASEC
] = &datasec_ops
,
5168 [BTF_KIND_FLOAT
] = &float_ops
,
5169 [BTF_KIND_DECL_TAG
] = &decl_tag_ops
,
5170 [BTF_KIND_TYPE_TAG
] = &modifier_ops
,
5171 [BTF_KIND_ENUM64
] = &enum64_ops
,
5174 static s32
btf_check_meta(struct btf_verifier_env
*env
,
5175 const struct btf_type
*t
,
5178 u32 saved_meta_left
= meta_left
;
5181 if (meta_left
< sizeof(*t
)) {
5182 btf_verifier_log(env
, "[%u] meta_left:%u meta_needed:%zu",
5183 env
->log_type_id
, meta_left
, sizeof(*t
));
5186 meta_left
-= sizeof(*t
);
5188 if (t
->info
& ~BTF_INFO_MASK
) {
5189 btf_verifier_log(env
, "[%u] Invalid btf_info:%x",
5190 env
->log_type_id
, t
->info
);
5194 if (BTF_INFO_KIND(t
->info
) > BTF_KIND_MAX
||
5195 BTF_INFO_KIND(t
->info
) == BTF_KIND_UNKN
) {
5196 btf_verifier_log(env
, "[%u] Invalid kind:%u",
5197 env
->log_type_id
, BTF_INFO_KIND(t
->info
));
5201 if (!btf_name_offset_valid(env
->btf
, t
->name_off
)) {
5202 btf_verifier_log(env
, "[%u] Invalid name_offset:%u",
5203 env
->log_type_id
, t
->name_off
);
5207 var_meta_size
= btf_type_ops(t
)->check_meta(env
, t
, meta_left
);
5208 if (var_meta_size
< 0)
5209 return var_meta_size
;
5211 meta_left
-= var_meta_size
;
5213 return saved_meta_left
- meta_left
;
5216 static int btf_check_all_metas(struct btf_verifier_env
*env
)
5218 struct btf
*btf
= env
->btf
;
5219 struct btf_header
*hdr
;
5223 cur
= btf
->nohdr_data
+ hdr
->type_off
;
5224 end
= cur
+ hdr
->type_len
;
5226 env
->log_type_id
= btf
->base_btf
? btf
->start_id
: 1;
5228 struct btf_type
*t
= cur
;
5231 meta_size
= btf_check_meta(env
, t
, end
- cur
);
5235 btf_add_type(env
, t
);
5243 static bool btf_resolve_valid(struct btf_verifier_env
*env
,
5244 const struct btf_type
*t
,
5247 struct btf
*btf
= env
->btf
;
5249 if (!env_type_is_resolved(env
, type_id
))
5252 if (btf_type_is_struct(t
) || btf_type_is_datasec(t
))
5253 return !btf_resolved_type_id(btf
, type_id
) &&
5254 !btf_resolved_type_size(btf
, type_id
);
5256 if (btf_type_is_decl_tag(t
) || btf_type_is_func(t
))
5257 return btf_resolved_type_id(btf
, type_id
) &&
5258 !btf_resolved_type_size(btf
, type_id
);
5260 if (btf_type_is_modifier(t
) || btf_type_is_ptr(t
) ||
5261 btf_type_is_var(t
)) {
5262 t
= btf_type_id_resolve(btf
, &type_id
);
5264 !btf_type_is_modifier(t
) &&
5265 !btf_type_is_var(t
) &&
5266 !btf_type_is_datasec(t
);
5269 if (btf_type_is_array(t
)) {
5270 const struct btf_array
*array
= btf_type_array(t
);
5271 const struct btf_type
*elem_type
;
5272 u32 elem_type_id
= array
->type
;
5275 elem_type
= btf_type_id_size(btf
, &elem_type_id
, &elem_size
);
5276 return elem_type
&& !btf_type_is_modifier(elem_type
) &&
5277 (array
->nelems
* elem_size
==
5278 btf_resolved_type_size(btf
, type_id
));
5284 static int btf_resolve(struct btf_verifier_env
*env
,
5285 const struct btf_type
*t
, u32 type_id
)
5287 u32 save_log_type_id
= env
->log_type_id
;
5288 const struct resolve_vertex
*v
;
5291 env
->resolve_mode
= RESOLVE_TBD
;
5292 env_stack_push(env
, t
, type_id
);
5293 while (!err
&& (v
= env_stack_peak(env
))) {
5294 env
->log_type_id
= v
->type_id
;
5295 err
= btf_type_ops(v
->t
)->resolve(env
, v
);
5298 env
->log_type_id
= type_id
;
5299 if (err
== -E2BIG
) {
5300 btf_verifier_log_type(env
, t
,
5301 "Exceeded max resolving depth:%u",
5303 } else if (err
== -EEXIST
) {
5304 btf_verifier_log_type(env
, t
, "Loop detected");
5307 /* Final sanity check */
5308 if (!err
&& !btf_resolve_valid(env
, t
, type_id
)) {
5309 btf_verifier_log_type(env
, t
, "Invalid resolve state");
5313 env
->log_type_id
= save_log_type_id
;
5317 static int btf_check_all_types(struct btf_verifier_env
*env
)
5319 struct btf
*btf
= env
->btf
;
5320 const struct btf_type
*t
;
5324 err
= env_resolve_init(env
);
5329 for (i
= btf
->base_btf
? 0 : 1; i
< btf
->nr_types
; i
++) {
5330 type_id
= btf
->start_id
+ i
;
5331 t
= btf_type_by_id(btf
, type_id
);
5333 env
->log_type_id
= type_id
;
5334 if (btf_type_needs_resolve(t
) &&
5335 !env_type_is_resolved(env
, type_id
)) {
5336 err
= btf_resolve(env
, t
, type_id
);
5341 if (btf_type_is_func_proto(t
)) {
5342 err
= btf_func_proto_check(env
, t
);
5351 static int btf_parse_type_sec(struct btf_verifier_env
*env
)
5353 const struct btf_header
*hdr
= &env
->btf
->hdr
;
5356 /* Type section must align to 4 bytes */
5357 if (hdr
->type_off
& (sizeof(u32
) - 1)) {
5358 btf_verifier_log(env
, "Unaligned type_off");
5362 if (!env
->btf
->base_btf
&& !hdr
->type_len
) {
5363 btf_verifier_log(env
, "No type found");
5367 err
= btf_check_all_metas(env
);
5371 return btf_check_all_types(env
);
5374 static int btf_parse_str_sec(struct btf_verifier_env
*env
)
5376 const struct btf_header
*hdr
;
5377 struct btf
*btf
= env
->btf
;
5378 const char *start
, *end
;
5381 start
= btf
->nohdr_data
+ hdr
->str_off
;
5382 end
= start
+ hdr
->str_len
;
5384 if (end
!= btf
->data
+ btf
->data_size
) {
5385 btf_verifier_log(env
, "String section is not at the end");
5389 btf
->strings
= start
;
5391 if (btf
->base_btf
&& !hdr
->str_len
)
5393 if (!hdr
->str_len
|| hdr
->str_len
- 1 > BTF_MAX_NAME_OFFSET
|| end
[-1]) {
5394 btf_verifier_log(env
, "Invalid string section");
5397 if (!btf
->base_btf
&& start
[0]) {
5398 btf_verifier_log(env
, "Invalid string section");
5405 static const size_t btf_sec_info_offset
[] = {
5406 offsetof(struct btf_header
, type_off
),
5407 offsetof(struct btf_header
, str_off
),
5410 static int btf_sec_info_cmp(const void *a
, const void *b
)
5412 const struct btf_sec_info
*x
= a
;
5413 const struct btf_sec_info
*y
= b
;
5415 return (int)(x
->off
- y
->off
) ? : (int)(x
->len
- y
->len
);
5418 static int btf_check_sec_info(struct btf_verifier_env
*env
,
5421 struct btf_sec_info secs
[ARRAY_SIZE(btf_sec_info_offset
)];
5422 u32 total
, expected_total
, i
;
5423 const struct btf_header
*hdr
;
5424 const struct btf
*btf
;
5429 /* Populate the secs from hdr */
5430 for (i
= 0; i
< ARRAY_SIZE(btf_sec_info_offset
); i
++)
5431 secs
[i
] = *(struct btf_sec_info
*)((void *)hdr
+
5432 btf_sec_info_offset
[i
]);
5434 sort(secs
, ARRAY_SIZE(btf_sec_info_offset
),
5435 sizeof(struct btf_sec_info
), btf_sec_info_cmp
, NULL
);
5437 /* Check for gaps and overlap among sections */
5439 expected_total
= btf_data_size
- hdr
->hdr_len
;
5440 for (i
= 0; i
< ARRAY_SIZE(btf_sec_info_offset
); i
++) {
5441 if (expected_total
< secs
[i
].off
) {
5442 btf_verifier_log(env
, "Invalid section offset");
5445 if (total
< secs
[i
].off
) {
5447 btf_verifier_log(env
, "Unsupported section found");
5450 if (total
> secs
[i
].off
) {
5451 btf_verifier_log(env
, "Section overlap found");
5454 if (expected_total
- total
< secs
[i
].len
) {
5455 btf_verifier_log(env
,
5456 "Total section length too long");
5459 total
+= secs
[i
].len
;
5462 /* There is data other than hdr and known sections */
5463 if (expected_total
!= total
) {
5464 btf_verifier_log(env
, "Unsupported section found");
5471 static int btf_parse_hdr(struct btf_verifier_env
*env
)
5473 u32 hdr_len
, hdr_copy
, btf_data_size
;
5474 const struct btf_header
*hdr
;
5478 btf_data_size
= btf
->data_size
;
5480 if (btf_data_size
< offsetofend(struct btf_header
, hdr_len
)) {
5481 btf_verifier_log(env
, "hdr_len not found");
5486 hdr_len
= hdr
->hdr_len
;
5487 if (btf_data_size
< hdr_len
) {
5488 btf_verifier_log(env
, "btf_header not found");
5492 /* Ensure the unsupported header fields are zero */
5493 if (hdr_len
> sizeof(btf
->hdr
)) {
5494 u8
*expected_zero
= btf
->data
+ sizeof(btf
->hdr
);
5495 u8
*end
= btf
->data
+ hdr_len
;
5497 for (; expected_zero
< end
; expected_zero
++) {
5498 if (*expected_zero
) {
5499 btf_verifier_log(env
, "Unsupported btf_header");
5505 hdr_copy
= min_t(u32
, hdr_len
, sizeof(btf
->hdr
));
5506 memcpy(&btf
->hdr
, btf
->data
, hdr_copy
);
5510 btf_verifier_log_hdr(env
, btf_data_size
);
5512 if (hdr
->magic
!= BTF_MAGIC
) {
5513 btf_verifier_log(env
, "Invalid magic");
5517 if (hdr
->version
!= BTF_VERSION
) {
5518 btf_verifier_log(env
, "Unsupported version");
5523 btf_verifier_log(env
, "Unsupported flags");
5527 if (!btf
->base_btf
&& btf_data_size
== hdr
->hdr_len
) {
5528 btf_verifier_log(env
, "No data");
5532 return btf_check_sec_info(env
, btf_data_size
);
5535 static const char *alloc_obj_fields
[] = {
5544 static struct btf_struct_metas
*
5545 btf_parse_struct_metas(struct bpf_verifier_log
*log
, struct btf
*btf
)
5547 struct btf_struct_metas
*tab
= NULL
;
5548 struct btf_id_set
*aof
;
5551 BUILD_BUG_ON(offsetof(struct btf_id_set
, cnt
) != 0);
5552 BUILD_BUG_ON(sizeof(struct btf_id_set
) != sizeof(u32
));
5554 aof
= kmalloc(sizeof(*aof
), GFP_KERNEL
| __GFP_NOWARN
);
5556 return ERR_PTR(-ENOMEM
);
5559 for (i
= 0; i
< ARRAY_SIZE(alloc_obj_fields
); i
++) {
5560 /* Try to find whether this special type exists in user BTF, and
5561 * if so remember its ID so we can easily find it among members
5562 * of structs that we iterate in the next loop.
5564 struct btf_id_set
*new_aof
;
5566 id
= btf_find_by_name_kind(btf
, alloc_obj_fields
[i
], BTF_KIND_STRUCT
);
5570 new_aof
= krealloc(aof
, offsetof(struct btf_id_set
, ids
[aof
->cnt
+ 1]),
5571 GFP_KERNEL
| __GFP_NOWARN
);
5577 aof
->ids
[aof
->cnt
++] = id
;
5580 n
= btf_nr_types(btf
);
5581 for (i
= 1; i
< n
; i
++) {
5582 /* Try to find if there are kptrs in user BTF and remember their ID */
5583 struct btf_id_set
*new_aof
;
5584 struct btf_field_info tmp
;
5585 const struct btf_type
*t
;
5587 t
= btf_type_by_id(btf
, i
);
5593 ret
= btf_find_kptr(btf
, t
, 0, 0, &tmp
, BPF_KPTR
);
5594 if (ret
!= BTF_FIELD_FOUND
)
5597 new_aof
= krealloc(aof
, offsetof(struct btf_id_set
, ids
[aof
->cnt
+ 1]),
5598 GFP_KERNEL
| __GFP_NOWARN
);
5604 aof
->ids
[aof
->cnt
++] = i
;
5611 sort(&aof
->ids
, aof
->cnt
, sizeof(aof
->ids
[0]), btf_id_cmp_func
, NULL
);
5613 for (i
= 1; i
< n
; i
++) {
5614 struct btf_struct_metas
*new_tab
;
5615 const struct btf_member
*member
;
5616 struct btf_struct_meta
*type
;
5617 struct btf_record
*record
;
5618 const struct btf_type
*t
;
5621 t
= btf_type_by_id(btf
, i
);
5622 if (!__btf_type_is_struct(t
))
5627 for_each_member(j
, t
, member
) {
5628 if (btf_id_set_contains(aof
, member
->type
))
5633 tab_cnt
= tab
? tab
->cnt
: 0;
5634 new_tab
= krealloc(tab
, offsetof(struct btf_struct_metas
, types
[tab_cnt
+ 1]),
5635 GFP_KERNEL
| __GFP_NOWARN
);
5644 type
= &tab
->types
[tab
->cnt
];
5646 record
= btf_parse_fields(btf
, t
, BPF_SPIN_LOCK
| BPF_LIST_HEAD
| BPF_LIST_NODE
|
5647 BPF_RB_ROOT
| BPF_RB_NODE
| BPF_REFCOUNT
|
5649 /* The record cannot be unset, treat it as an error if so */
5650 if (IS_ERR_OR_NULL(record
)) {
5651 ret
= PTR_ERR_OR_ZERO(record
) ?: -EFAULT
;
5654 type
->record
= record
;
5660 btf_struct_metas_free(tab
);
5663 return ERR_PTR(ret
);
5666 struct btf_struct_meta
*btf_find_struct_meta(const struct btf
*btf
, u32 btf_id
)
5668 struct btf_struct_metas
*tab
;
5670 BUILD_BUG_ON(offsetof(struct btf_struct_meta
, btf_id
) != 0);
5671 tab
= btf
->struct_meta_tab
;
5674 return bsearch(&btf_id
, tab
->types
, tab
->cnt
, sizeof(tab
->types
[0]), btf_id_cmp_func
);
5677 static int btf_check_type_tags(struct btf_verifier_env
*env
,
5678 struct btf
*btf
, int start_id
)
5680 int i
, n
, good_id
= start_id
- 1;
5683 n
= btf_nr_types(btf
);
5684 for (i
= start_id
; i
< n
; i
++) {
5685 const struct btf_type
*t
;
5686 int chain_limit
= 32;
5689 t
= btf_type_by_id(btf
, i
);
5692 if (!btf_type_is_modifier(t
))
5697 in_tags
= btf_type_is_type_tag(t
);
5698 while (btf_type_is_modifier(t
)) {
5699 if (!chain_limit
--) {
5700 btf_verifier_log(env
, "Max chain length or cycle detected");
5703 if (btf_type_is_type_tag(t
)) {
5705 btf_verifier_log(env
, "Type tags don't precede modifiers");
5708 } else if (in_tags
) {
5711 if (cur_id
<= good_id
)
5713 /* Move to next type */
5715 t
= btf_type_by_id(btf
, cur_id
);
5724 static int finalize_log(struct bpf_verifier_log
*log
, bpfptr_t uattr
, u32 uattr_size
)
5729 err
= bpf_vlog_finalize(log
, &log_true_size
);
5731 if (uattr_size
>= offsetofend(union bpf_attr
, btf_log_true_size
) &&
5732 copy_to_bpfptr_offset(uattr
, offsetof(union bpf_attr
, btf_log_true_size
),
5733 &log_true_size
, sizeof(log_true_size
)))
5739 static struct btf
*btf_parse(const union bpf_attr
*attr
, bpfptr_t uattr
, u32 uattr_size
)
5741 bpfptr_t btf_data
= make_bpfptr(attr
->btf
, uattr
.is_kernel
);
5742 char __user
*log_ubuf
= u64_to_user_ptr(attr
->btf_log_buf
);
5743 struct btf_struct_metas
*struct_meta_tab
;
5744 struct btf_verifier_env
*env
= NULL
;
5745 struct btf
*btf
= NULL
;
5749 if (attr
->btf_size
> BTF_MAX_SIZE
)
5750 return ERR_PTR(-E2BIG
);
5752 env
= kzalloc(sizeof(*env
), GFP_KERNEL
| __GFP_NOWARN
);
5754 return ERR_PTR(-ENOMEM
);
5756 /* user could have requested verbose verifier output
5757 * and supplied buffer to store the verification trace
5759 err
= bpf_vlog_init(&env
->log
, attr
->btf_log_level
,
5760 log_ubuf
, attr
->btf_log_size
);
5764 btf
= kzalloc(sizeof(*btf
), GFP_KERNEL
| __GFP_NOWARN
);
5771 data
= kvmalloc(attr
->btf_size
, GFP_KERNEL
| __GFP_NOWARN
);
5778 btf
->data_size
= attr
->btf_size
;
5780 if (copy_from_bpfptr(data
, btf_data
, attr
->btf_size
)) {
5785 err
= btf_parse_hdr(env
);
5789 btf
->nohdr_data
= btf
->data
+ btf
->hdr
.hdr_len
;
5791 err
= btf_parse_str_sec(env
);
5795 err
= btf_parse_type_sec(env
);
5799 err
= btf_check_type_tags(env
, btf
, 1);
5803 struct_meta_tab
= btf_parse_struct_metas(&env
->log
, btf
);
5804 if (IS_ERR(struct_meta_tab
)) {
5805 err
= PTR_ERR(struct_meta_tab
);
5808 btf
->struct_meta_tab
= struct_meta_tab
;
5810 if (struct_meta_tab
) {
5813 for (i
= 0; i
< struct_meta_tab
->cnt
; i
++) {
5814 err
= btf_check_and_fixup_fields(btf
, struct_meta_tab
->types
[i
].record
);
5820 err
= finalize_log(&env
->log
, uattr
, uattr_size
);
5824 btf_verifier_env_free(env
);
5825 refcount_set(&btf
->refcnt
, 1);
5829 btf_free_struct_meta_tab(btf
);
5831 /* overwrite err with -ENOSPC or -EFAULT */
5832 ret
= finalize_log(&env
->log
, uattr
, uattr_size
);
5836 btf_verifier_env_free(env
);
5839 return ERR_PTR(err
);
5842 extern char __start_BTF
[];
5843 extern char __stop_BTF
[];
5844 extern struct btf
*btf_vmlinux
;
5846 #define BPF_MAP_TYPE(_id, _ops)
5847 #define BPF_LINK_TYPE(_id, _name)
5849 struct bpf_ctx_convert
{
5850 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
5851 prog_ctx_type _id##_prog; \
5852 kern_ctx_type _id##_kern;
5853 #include <linux/bpf_types.h>
5854 #undef BPF_PROG_TYPE
5856 /* 't' is written once under lock. Read many times. */
5857 const struct btf_type
*t
;
5860 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
5862 #include <linux/bpf_types.h>
5863 #undef BPF_PROG_TYPE
5864 __ctx_convert_unused
, /* to avoid empty enum in extreme .config */
5866 static u8 bpf_ctx_convert_map
[] = {
5867 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
5868 [_id] = __ctx_convert##_id,
5869 #include <linux/bpf_types.h>
5870 #undef BPF_PROG_TYPE
5871 0, /* avoid empty array */
5874 #undef BPF_LINK_TYPE
5876 static const struct btf_type
*find_canonical_prog_ctx_type(enum bpf_prog_type prog_type
)
5878 const struct btf_type
*conv_struct
;
5879 const struct btf_member
*ctx_type
;
5881 conv_struct
= bpf_ctx_convert
.t
;
5884 /* prog_type is valid bpf program type. No need for bounds check. */
5885 ctx_type
= btf_type_member(conv_struct
) + bpf_ctx_convert_map
[prog_type
] * 2;
5886 /* ctx_type is a pointer to prog_ctx_type in vmlinux.
5887 * Like 'struct __sk_buff'
5889 return btf_type_by_id(btf_vmlinux
, ctx_type
->type
);
5892 static int find_kern_ctx_type_id(enum bpf_prog_type prog_type
)
5894 const struct btf_type
*conv_struct
;
5895 const struct btf_member
*ctx_type
;
5897 conv_struct
= bpf_ctx_convert
.t
;
5900 /* prog_type is valid bpf program type. No need for bounds check. */
5901 ctx_type
= btf_type_member(conv_struct
) + bpf_ctx_convert_map
[prog_type
] * 2 + 1;
5902 /* ctx_type is a pointer to prog_ctx_type in vmlinux.
5903 * Like 'struct sk_buff'
5905 return ctx_type
->type
;
5908 bool btf_is_projection_of(const char *pname
, const char *tname
)
5910 if (strcmp(pname
, "__sk_buff") == 0 && strcmp(tname
, "sk_buff") == 0)
5912 if (strcmp(pname
, "xdp_md") == 0 && strcmp(tname
, "xdp_buff") == 0)
5917 bool btf_is_prog_ctx_type(struct bpf_verifier_log
*log
, const struct btf
*btf
,
5918 const struct btf_type
*t
, enum bpf_prog_type prog_type
,
5921 const struct btf_type
*ctx_type
;
5922 const char *tname
, *ctx_tname
;
5924 t
= btf_type_by_id(btf
, t
->type
);
5926 /* KPROBE programs allow bpf_user_pt_regs_t typedef, which we need to
5927 * check before we skip all the typedef below.
5929 if (prog_type
== BPF_PROG_TYPE_KPROBE
) {
5930 while (btf_type_is_modifier(t
) && !btf_type_is_typedef(t
))
5931 t
= btf_type_by_id(btf
, t
->type
);
5933 if (btf_type_is_typedef(t
)) {
5934 tname
= btf_name_by_offset(btf
, t
->name_off
);
5935 if (tname
&& strcmp(tname
, "bpf_user_pt_regs_t") == 0)
5940 while (btf_type_is_modifier(t
))
5941 t
= btf_type_by_id(btf
, t
->type
);
5942 if (!btf_type_is_struct(t
)) {
5943 /* Only pointer to struct is supported for now.
5944 * That means that BPF_PROG_TYPE_TRACEPOINT with BTF
5945 * is not supported yet.
5946 * BPF_PROG_TYPE_RAW_TRACEPOINT is fine.
5950 tname
= btf_name_by_offset(btf
, t
->name_off
);
5952 bpf_log(log
, "arg#%d struct doesn't have a name\n", arg
);
5956 ctx_type
= find_canonical_prog_ctx_type(prog_type
);
5958 bpf_log(log
, "btf_vmlinux is malformed\n");
5959 /* should not happen */
5963 ctx_tname
= btf_name_by_offset(btf_vmlinux
, ctx_type
->name_off
);
5965 /* should not happen */
5966 bpf_log(log
, "Please fix kernel include/linux/bpf_types.h\n");
5969 /* program types without named context types work only with arg:ctx tag */
5970 if (ctx_tname
[0] == '\0')
5972 /* only compare that prog's ctx type name is the same as
5973 * kernel expects. No need to compare field by field.
5974 * It's ok for bpf prog to do:
5975 * struct __sk_buff {};
5976 * int socket_filter_bpf_prog(struct __sk_buff *skb)
5977 * { // no fields of skb are ever used }
5979 if (btf_is_projection_of(ctx_tname
, tname
))
5981 if (strcmp(ctx_tname
, tname
)) {
5982 /* bpf_user_pt_regs_t is a typedef, so resolve it to
5983 * underlying struct and check name again
5985 if (!btf_type_is_modifier(ctx_type
))
5987 while (btf_type_is_modifier(ctx_type
))
5988 ctx_type
= btf_type_by_id(btf_vmlinux
, ctx_type
->type
);
5994 /* forward declarations for arch-specific underlying types of
5995 * bpf_user_pt_regs_t; this avoids the need for arch-specific #ifdef
5996 * compilation guards below for BPF_PROG_TYPE_PERF_EVENT checks, but still
5997 * works correctly with __builtin_types_compatible_p() on respective
6000 struct user_regs_struct
;
6001 struct user_pt_regs
;
6003 static int btf_validate_prog_ctx_type(struct bpf_verifier_log
*log
, const struct btf
*btf
,
6004 const struct btf_type
*t
, int arg
,
6005 enum bpf_prog_type prog_type
,
6006 enum bpf_attach_type attach_type
)
6008 const struct btf_type
*ctx_type
;
6009 const char *tname
, *ctx_tname
;
6011 if (!btf_is_ptr(t
)) {
6012 bpf_log(log
, "arg#%d type isn't a pointer\n", arg
);
6015 t
= btf_type_by_id(btf
, t
->type
);
6017 /* KPROBE and PERF_EVENT programs allow bpf_user_pt_regs_t typedef */
6018 if (prog_type
== BPF_PROG_TYPE_KPROBE
|| prog_type
== BPF_PROG_TYPE_PERF_EVENT
) {
6019 while (btf_type_is_modifier(t
) && !btf_type_is_typedef(t
))
6020 t
= btf_type_by_id(btf
, t
->type
);
6022 if (btf_type_is_typedef(t
)) {
6023 tname
= btf_name_by_offset(btf
, t
->name_off
);
6024 if (tname
&& strcmp(tname
, "bpf_user_pt_regs_t") == 0)
6029 /* all other program types don't use typedefs for context type */
6030 while (btf_type_is_modifier(t
))
6031 t
= btf_type_by_id(btf
, t
->type
);
6033 /* `void *ctx __arg_ctx` is always valid */
6034 if (btf_type_is_void(t
))
6037 tname
= btf_name_by_offset(btf
, t
->name_off
);
6038 if (str_is_empty(tname
)) {
6039 bpf_log(log
, "arg#%d type doesn't have a name\n", arg
);
6044 switch (prog_type
) {
6045 case BPF_PROG_TYPE_KPROBE
:
6046 if (__btf_type_is_struct(t
) && strcmp(tname
, "pt_regs") == 0)
6049 case BPF_PROG_TYPE_PERF_EVENT
:
6050 if (__builtin_types_compatible_p(bpf_user_pt_regs_t
, struct pt_regs
) &&
6051 __btf_type_is_struct(t
) && strcmp(tname
, "pt_regs") == 0)
6053 if (__builtin_types_compatible_p(bpf_user_pt_regs_t
, struct user_pt_regs
) &&
6054 __btf_type_is_struct(t
) && strcmp(tname
, "user_pt_regs") == 0)
6056 if (__builtin_types_compatible_p(bpf_user_pt_regs_t
, struct user_regs_struct
) &&
6057 __btf_type_is_struct(t
) && strcmp(tname
, "user_regs_struct") == 0)
6060 case BPF_PROG_TYPE_RAW_TRACEPOINT
:
6061 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE
:
6062 /* allow u64* as ctx */
6063 if (btf_is_int(t
) && t
->size
== 8)
6066 case BPF_PROG_TYPE_TRACING
:
6067 switch (attach_type
) {
6068 case BPF_TRACE_RAW_TP
:
6069 /* tp_btf program is TRACING, so need special case here */
6070 if (__btf_type_is_struct(t
) &&
6071 strcmp(tname
, "bpf_raw_tracepoint_args") == 0)
6073 /* allow u64* as ctx */
6074 if (btf_is_int(t
) && t
->size
== 8)
6077 case BPF_TRACE_ITER
:
6078 /* allow struct bpf_iter__xxx types only */
6079 if (__btf_type_is_struct(t
) &&
6080 strncmp(tname
, "bpf_iter__", sizeof("bpf_iter__") - 1) == 0)
6083 case BPF_TRACE_FENTRY
:
6084 case BPF_TRACE_FEXIT
:
6085 case BPF_MODIFY_RETURN
:
6086 /* allow u64* as ctx */
6087 if (btf_is_int(t
) && t
->size
== 8)
6094 case BPF_PROG_TYPE_LSM
:
6095 case BPF_PROG_TYPE_STRUCT_OPS
:
6096 /* allow u64* as ctx */
6097 if (btf_is_int(t
) && t
->size
== 8)
6100 case BPF_PROG_TYPE_TRACEPOINT
:
6101 case BPF_PROG_TYPE_SYSCALL
:
6102 case BPF_PROG_TYPE_EXT
:
6103 return 0; /* anything goes */
6108 ctx_type
= find_canonical_prog_ctx_type(prog_type
);
6110 /* should not happen */
6111 bpf_log(log
, "btf_vmlinux is malformed\n");
6115 /* resolve typedefs and check that underlying structs are matching as well */
6116 while (btf_type_is_modifier(ctx_type
))
6117 ctx_type
= btf_type_by_id(btf_vmlinux
, ctx_type
->type
);
6119 /* if program type doesn't have distinctly named struct type for
6120 * context, then __arg_ctx argument can only be `void *`, which we
6121 * already checked above
6123 if (!__btf_type_is_struct(ctx_type
)) {
6124 bpf_log(log
, "arg#%d should be void pointer\n", arg
);
6128 ctx_tname
= btf_name_by_offset(btf_vmlinux
, ctx_type
->name_off
);
6129 if (!__btf_type_is_struct(t
) || strcmp(ctx_tname
, tname
) != 0) {
6130 bpf_log(log
, "arg#%d should be `struct %s *`\n", arg
, ctx_tname
);
6137 static int btf_translate_to_vmlinux(struct bpf_verifier_log
*log
,
6139 const struct btf_type
*t
,
6140 enum bpf_prog_type prog_type
,
6143 if (!btf_is_prog_ctx_type(log
, btf
, t
, prog_type
, arg
))
6145 return find_kern_ctx_type_id(prog_type
);
6148 int get_kern_ctx_btf_id(struct bpf_verifier_log
*log
, enum bpf_prog_type prog_type
)
6150 const struct btf_member
*kctx_member
;
6151 const struct btf_type
*conv_struct
;
6152 const struct btf_type
*kctx_type
;
6155 conv_struct
= bpf_ctx_convert
.t
;
6156 /* get member for kernel ctx type */
6157 kctx_member
= btf_type_member(conv_struct
) + bpf_ctx_convert_map
[prog_type
] * 2 + 1;
6158 kctx_type_id
= kctx_member
->type
;
6159 kctx_type
= btf_type_by_id(btf_vmlinux
, kctx_type_id
);
6160 if (!btf_type_is_struct(kctx_type
)) {
6161 bpf_log(log
, "kern ctx type id %u is not a struct\n", kctx_type_id
);
6165 return kctx_type_id
;
6168 BTF_ID_LIST(bpf_ctx_convert_btf_id
)
6169 BTF_ID(struct, bpf_ctx_convert
)
6171 static struct btf
*btf_parse_base(struct btf_verifier_env
*env
, const char *name
,
6172 void *data
, unsigned int data_size
)
6174 struct btf
*btf
= NULL
;
6177 if (!IS_ENABLED(CONFIG_DEBUG_INFO_BTF
))
6178 return ERR_PTR(-ENOENT
);
6180 btf
= kzalloc(sizeof(*btf
), GFP_KERNEL
| __GFP_NOWARN
);
6188 btf
->data_size
= data_size
;
6189 btf
->kernel_btf
= true;
6190 snprintf(btf
->name
, sizeof(btf
->name
), "%s", name
);
6192 err
= btf_parse_hdr(env
);
6196 btf
->nohdr_data
= btf
->data
+ btf
->hdr
.hdr_len
;
6198 err
= btf_parse_str_sec(env
);
6202 err
= btf_check_all_metas(env
);
6206 err
= btf_check_type_tags(env
, btf
, 1);
6210 refcount_set(&btf
->refcnt
, 1);
6219 return ERR_PTR(err
);
6222 struct btf
*btf_parse_vmlinux(void)
6224 struct btf_verifier_env
*env
= NULL
;
6225 struct bpf_verifier_log
*log
;
6229 env
= kzalloc(sizeof(*env
), GFP_KERNEL
| __GFP_NOWARN
);
6231 return ERR_PTR(-ENOMEM
);
6234 log
->level
= BPF_LOG_KERNEL
;
6235 btf
= btf_parse_base(env
, "vmlinux", __start_BTF
, __stop_BTF
- __start_BTF
);
6239 /* btf_parse_vmlinux() runs under bpf_verifier_lock */
6240 bpf_ctx_convert
.t
= btf_type_by_id(btf
, bpf_ctx_convert_btf_id
[0]);
6241 err
= btf_alloc_id(btf
);
6247 btf_verifier_env_free(env
);
6251 /* If .BTF_ids section was created with distilled base BTF, both base and
6252 * split BTF ids will need to be mapped to actual base/split ids for
6253 * BTF now that it has been relocated.
6255 static __u32
btf_relocate_id(const struct btf
*btf
, __u32 id
)
6257 if (!btf
->base_btf
|| !btf
->base_id_map
)
6259 return btf
->base_id_map
[id
];
6262 #ifdef CONFIG_DEBUG_INFO_BTF_MODULES
6264 static struct btf
*btf_parse_module(const char *module_name
, const void *data
,
6265 unsigned int data_size
, void *base_data
,
6266 unsigned int base_data_size
)
6268 struct btf
*btf
= NULL
, *vmlinux_btf
, *base_btf
= NULL
;
6269 struct btf_verifier_env
*env
= NULL
;
6270 struct bpf_verifier_log
*log
;
6273 vmlinux_btf
= bpf_get_btf_vmlinux();
6274 if (IS_ERR(vmlinux_btf
))
6277 return ERR_PTR(-EINVAL
);
6279 env
= kzalloc(sizeof(*env
), GFP_KERNEL
| __GFP_NOWARN
);
6281 return ERR_PTR(-ENOMEM
);
6284 log
->level
= BPF_LOG_KERNEL
;
6287 base_btf
= btf_parse_base(env
, ".BTF.base", base_data
, base_data_size
);
6288 if (IS_ERR(base_btf
)) {
6289 err
= PTR_ERR(base_btf
);
6293 base_btf
= vmlinux_btf
;
6296 btf
= kzalloc(sizeof(*btf
), GFP_KERNEL
| __GFP_NOWARN
);
6303 btf
->base_btf
= base_btf
;
6304 btf
->start_id
= base_btf
->nr_types
;
6305 btf
->start_str_off
= base_btf
->hdr
.str_len
;
6306 btf
->kernel_btf
= true;
6307 snprintf(btf
->name
, sizeof(btf
->name
), "%s", module_name
);
6309 btf
->data
= kvmemdup(data
, data_size
, GFP_KERNEL
| __GFP_NOWARN
);
6314 btf
->data_size
= data_size
;
6316 err
= btf_parse_hdr(env
);
6320 btf
->nohdr_data
= btf
->data
+ btf
->hdr
.hdr_len
;
6322 err
= btf_parse_str_sec(env
);
6326 err
= btf_check_all_metas(env
);
6330 err
= btf_check_type_tags(env
, btf
, btf_nr_types(base_btf
));
6334 if (base_btf
!= vmlinux_btf
) {
6335 err
= btf_relocate(btf
, vmlinux_btf
, &btf
->base_id_map
);
6339 base_btf
= vmlinux_btf
;
6342 btf_verifier_env_free(env
);
6343 refcount_set(&btf
->refcnt
, 1);
6347 btf_verifier_env_free(env
);
6348 if (!IS_ERR(base_btf
) && base_btf
!= vmlinux_btf
)
6355 return ERR_PTR(err
);
6358 #endif /* CONFIG_DEBUG_INFO_BTF_MODULES */
6360 struct btf
*bpf_prog_get_target_btf(const struct bpf_prog
*prog
)
6362 struct bpf_prog
*tgt_prog
= prog
->aux
->dst_prog
;
6365 return tgt_prog
->aux
->btf
;
6367 return prog
->aux
->attach_btf
;
6370 static bool is_int_ptr(struct btf
*btf
, const struct btf_type
*t
)
6372 /* skip modifiers */
6373 t
= btf_type_skip_modifiers(btf
, t
->type
, NULL
);
6375 return btf_type_is_int(t
);
6378 static u32
get_ctx_arg_idx(struct btf
*btf
, const struct btf_type
*func_proto
,
6381 const struct btf_param
*args
;
6382 const struct btf_type
*t
;
6383 u32 offset
= 0, nr_args
;
6389 nr_args
= btf_type_vlen(func_proto
);
6390 args
= (const struct btf_param
*)(func_proto
+ 1);
6391 for (i
= 0; i
< nr_args
; i
++) {
6392 t
= btf_type_skip_modifiers(btf
, args
[i
].type
, NULL
);
6393 offset
+= btf_type_is_ptr(t
) ? 8 : roundup(t
->size
, 8);
6398 t
= btf_type_skip_modifiers(btf
, func_proto
->type
, NULL
);
6399 offset
+= btf_type_is_ptr(t
) ? 8 : roundup(t
->size
, 8);
6406 static bool prog_args_trusted(const struct bpf_prog
*prog
)
6408 enum bpf_attach_type atype
= prog
->expected_attach_type
;
6410 switch (prog
->type
) {
6411 case BPF_PROG_TYPE_TRACING
:
6412 return atype
== BPF_TRACE_RAW_TP
|| atype
== BPF_TRACE_ITER
;
6413 case BPF_PROG_TYPE_LSM
:
6414 return bpf_lsm_is_trusted(prog
);
6415 case BPF_PROG_TYPE_STRUCT_OPS
:
6422 int btf_ctx_arg_offset(const struct btf
*btf
, const struct btf_type
*func_proto
,
6425 const struct btf_param
*args
;
6426 const struct btf_type
*t
;
6430 args
= btf_params(func_proto
);
6431 for (i
= 0; i
< arg_no
; i
++) {
6432 t
= btf_type_by_id(btf
, args
[i
].type
);
6433 t
= btf_resolve_size(btf
, t
, &sz
);
6436 off
+= roundup(sz
, 8);
6442 bool btf_ctx_access(int off
, int size
, enum bpf_access_type type
,
6443 const struct bpf_prog
*prog
,
6444 struct bpf_insn_access_aux
*info
)
6446 const struct btf_type
*t
= prog
->aux
->attach_func_proto
;
6447 struct bpf_prog
*tgt_prog
= prog
->aux
->dst_prog
;
6448 struct btf
*btf
= bpf_prog_get_target_btf(prog
);
6449 const char *tname
= prog
->aux
->attach_func_name
;
6450 struct bpf_verifier_log
*log
= info
->log
;
6451 const struct btf_param
*args
;
6452 const char *tag_value
;
6457 bpf_log(log
, "func '%s' offset %d is not multiple of 8\n",
6461 arg
= get_ctx_arg_idx(btf
, t
, off
);
6462 args
= (const struct btf_param
*)(t
+ 1);
6463 /* if (t == NULL) Fall back to default BPF prog with
6464 * MAX_BPF_FUNC_REG_ARGS u64 arguments.
6466 nr_args
= t
? btf_type_vlen(t
) : MAX_BPF_FUNC_REG_ARGS
;
6467 if (prog
->aux
->attach_btf_trace
) {
6468 /* skip first 'void *__data' argument in btf_trace_##name typedef */
6473 if (arg
> nr_args
) {
6474 bpf_log(log
, "func '%s' doesn't have %d-th argument\n",
6479 if (arg
== nr_args
) {
6480 switch (prog
->expected_attach_type
) {
6482 /* mark we are accessing the return value */
6483 info
->is_retval
= true;
6485 case BPF_LSM_CGROUP
:
6486 case BPF_TRACE_FEXIT
:
6487 /* When LSM programs are attached to void LSM hooks
6488 * they use FEXIT trampolines and when attached to
6489 * int LSM hooks, they use MODIFY_RETURN trampolines.
6491 * While the LSM programs are BPF_MODIFY_RETURN-like
6494 * if (ret_type != 'int')
6497 * is _not_ done here. This is still safe as LSM hooks
6498 * have only void and int return types.
6502 t
= btf_type_by_id(btf
, t
->type
);
6504 case BPF_MODIFY_RETURN
:
6505 /* For now the BPF_MODIFY_RETURN can only be attached to
6506 * functions that return an int.
6511 t
= btf_type_skip_modifiers(btf
, t
->type
, NULL
);
6512 if (!btf_type_is_small_int(t
)) {
6514 "ret type %s not allowed for fmod_ret\n",
6520 bpf_log(log
, "func '%s' doesn't have %d-th argument\n",
6526 /* Default prog with MAX_BPF_FUNC_REG_ARGS args */
6528 t
= btf_type_by_id(btf
, args
[arg
].type
);
6531 /* skip modifiers */
6532 while (btf_type_is_modifier(t
))
6533 t
= btf_type_by_id(btf
, t
->type
);
6534 if (btf_type_is_small_int(t
) || btf_is_any_enum(t
) || __btf_type_is_struct(t
))
6535 /* accessing a scalar */
6537 if (!btf_type_is_ptr(t
)) {
6539 "func '%s' arg%d '%s' has type %s. Only pointer access is allowed\n",
6541 __btf_name_by_offset(btf
, t
->name_off
),
6546 /* check for PTR_TO_RDONLY_BUF_OR_NULL or PTR_TO_RDWR_BUF_OR_NULL */
6547 for (i
= 0; i
< prog
->aux
->ctx_arg_info_size
; i
++) {
6548 const struct bpf_ctx_arg_aux
*ctx_arg_info
= &prog
->aux
->ctx_arg_info
[i
];
6551 type
= base_type(ctx_arg_info
->reg_type
);
6552 flag
= type_flag(ctx_arg_info
->reg_type
);
6553 if (ctx_arg_info
->offset
== off
&& type
== PTR_TO_BUF
&&
6554 (flag
& PTR_MAYBE_NULL
)) {
6555 info
->reg_type
= ctx_arg_info
->reg_type
;
6561 /* This is a pointer to void.
6562 * It is the same as scalar from the verifier safety pov.
6563 * No further pointer walking is allowed.
6567 if (is_int_ptr(btf
, t
))
6570 /* this is a pointer to another type */
6571 for (i
= 0; i
< prog
->aux
->ctx_arg_info_size
; i
++) {
6572 const struct bpf_ctx_arg_aux
*ctx_arg_info
= &prog
->aux
->ctx_arg_info
[i
];
6574 if (ctx_arg_info
->offset
== off
) {
6575 if (!ctx_arg_info
->btf_id
) {
6576 bpf_log(log
,"invalid btf_id for context argument offset %u\n", off
);
6580 info
->reg_type
= ctx_arg_info
->reg_type
;
6581 info
->btf
= ctx_arg_info
->btf
? : btf_vmlinux
;
6582 info
->btf_id
= ctx_arg_info
->btf_id
;
6587 info
->reg_type
= PTR_TO_BTF_ID
;
6588 if (prog_args_trusted(prog
))
6589 info
->reg_type
|= PTR_TRUSTED
;
6591 /* Raw tracepoint arguments always get marked as maybe NULL */
6592 if (bpf_prog_is_raw_tp(prog
))
6593 info
->reg_type
|= PTR_MAYBE_NULL
;
6594 else if (btf_param_match_suffix(btf
, &args
[arg
], "__nullable"))
6595 info
->reg_type
|= PTR_MAYBE_NULL
;
6598 enum bpf_prog_type tgt_type
;
6600 if (tgt_prog
->type
== BPF_PROG_TYPE_EXT
)
6601 tgt_type
= tgt_prog
->aux
->saved_dst_prog_type
;
6603 tgt_type
= tgt_prog
->type
;
6605 ret
= btf_translate_to_vmlinux(log
, btf
, t
, tgt_type
, arg
);
6607 info
->btf
= btf_vmlinux
;
6616 info
->btf_id
= t
->type
;
6617 t
= btf_type_by_id(btf
, t
->type
);
6619 if (btf_type_is_type_tag(t
)) {
6620 tag_value
= __btf_name_by_offset(btf
, t
->name_off
);
6621 if (strcmp(tag_value
, "user") == 0)
6622 info
->reg_type
|= MEM_USER
;
6623 if (strcmp(tag_value
, "percpu") == 0)
6624 info
->reg_type
|= MEM_PERCPU
;
6627 /* skip modifiers */
6628 while (btf_type_is_modifier(t
)) {
6629 info
->btf_id
= t
->type
;
6630 t
= btf_type_by_id(btf
, t
->type
);
6632 if (!btf_type_is_struct(t
)) {
6634 "func '%s' arg%d type %s is not a struct\n",
6635 tname
, arg
, btf_type_str(t
));
6638 bpf_log(log
, "func '%s' arg%d has btf_id %d type %s '%s'\n",
6639 tname
, arg
, info
->btf_id
, btf_type_str(t
),
6640 __btf_name_by_offset(btf
, t
->name_off
));
6643 EXPORT_SYMBOL_GPL(btf_ctx_access
);
6645 enum bpf_struct_walk_result
{
6652 static int btf_struct_walk(struct bpf_verifier_log
*log
, const struct btf
*btf
,
6653 const struct btf_type
*t
, int off
, int size
,
6654 u32
*next_btf_id
, enum bpf_type_flag
*flag
,
6655 const char **field_name
)
6657 u32 i
, moff
, mtrue_end
, msize
= 0, total_nelems
= 0;
6658 const struct btf_type
*mtype
, *elem_type
= NULL
;
6659 const struct btf_member
*member
;
6660 const char *tname
, *mname
, *tag_value
;
6661 u32 vlen
, elem_id
, mid
;
6664 if (btf_type_is_modifier(t
))
6665 t
= btf_type_skip_modifiers(btf
, t
->type
, NULL
);
6666 tname
= __btf_name_by_offset(btf
, t
->name_off
);
6667 if (!btf_type_is_struct(t
)) {
6668 bpf_log(log
, "Type '%s' is not a struct\n", tname
);
6672 vlen
= btf_type_vlen(t
);
6673 if (BTF_INFO_KIND(t
->info
) == BTF_KIND_UNION
&& vlen
!= 1 && !(*flag
& PTR_UNTRUSTED
))
6675 * walking unions yields untrusted pointers
6676 * with exception of __bpf_md_ptr and other
6677 * unions with a single member
6679 *flag
|= PTR_UNTRUSTED
;
6681 if (off
+ size
> t
->size
) {
6682 /* If the last element is a variable size array, we may
6683 * need to relax the rule.
6685 struct btf_array
*array_elem
;
6690 member
= btf_type_member(t
) + vlen
- 1;
6691 mtype
= btf_type_skip_modifiers(btf
, member
->type
,
6693 if (!btf_type_is_array(mtype
))
6696 array_elem
= (struct btf_array
*)(mtype
+ 1);
6697 if (array_elem
->nelems
!= 0)
6700 moff
= __btf_member_bit_offset(t
, member
) / 8;
6704 /* allow structure and integer */
6705 t
= btf_type_skip_modifiers(btf
, array_elem
->type
,
6708 if (btf_type_is_int(t
))
6711 if (!btf_type_is_struct(t
))
6714 off
= (off
- moff
) % t
->size
;
6718 bpf_log(log
, "access beyond struct %s at off %u size %u\n",
6723 for_each_member(i
, t
, member
) {
6724 /* offset of the field in bytes */
6725 moff
= __btf_member_bit_offset(t
, member
) / 8;
6726 if (off
+ size
<= moff
)
6727 /* won't find anything, field is already too far */
6730 if (__btf_member_bitfield_size(t
, member
)) {
6731 u32 end_bit
= __btf_member_bit_offset(t
, member
) +
6732 __btf_member_bitfield_size(t
, member
);
6734 /* off <= moff instead of off == moff because clang
6735 * does not generate a BTF member for anonymous
6736 * bitfield like the ":16" here:
6743 BITS_ROUNDUP_BYTES(end_bit
) <= off
+ size
)
6746 /* off may be accessing a following member
6750 * Doing partial access at either end of this
6751 * bitfield. Continue on this case also to
6752 * treat it as not accessing this bitfield
6753 * and eventually error out as field not
6754 * found to keep it simple.
6755 * It could be relaxed if there was a legit
6756 * partial access case later.
6761 /* In case of "off" is pointing to holes of a struct */
6765 /* type of the field */
6767 mtype
= btf_type_by_id(btf
, member
->type
);
6768 mname
= __btf_name_by_offset(btf
, member
->name_off
);
6770 mtype
= __btf_resolve_size(btf
, mtype
, &msize
,
6771 &elem_type
, &elem_id
, &total_nelems
,
6773 if (IS_ERR(mtype
)) {
6774 bpf_log(log
, "field %s doesn't have size\n", mname
);
6778 mtrue_end
= moff
+ msize
;
6779 if (off
>= mtrue_end
)
6780 /* no overlap with member, keep iterating */
6783 if (btf_type_is_array(mtype
)) {
6786 /* __btf_resolve_size() above helps to
6787 * linearize a multi-dimensional array.
6789 * The logic here is treating an array
6790 * in a struct as the following way:
6793 * struct inner array[2][2];
6799 * struct inner array_elem0;
6800 * struct inner array_elem1;
6801 * struct inner array_elem2;
6802 * struct inner array_elem3;
6805 * When accessing outer->array[1][0], it moves
6806 * moff to "array_elem2", set mtype to
6807 * "struct inner", and msize also becomes
6808 * sizeof(struct inner). Then most of the
6809 * remaining logic will fall through without
6810 * caring the current member is an array or
6813 * Unlike mtype/msize/moff, mtrue_end does not
6814 * change. The naming difference ("_true") tells
6815 * that it is not always corresponding to
6816 * the current mtype/msize/moff.
6817 * It is the true end of the current
6818 * member (i.e. array in this case). That
6819 * will allow an int array to be accessed like
6821 * i.e. allow access beyond the size of
6822 * the array's element as long as it is
6823 * within the mtrue_end boundary.
6826 /* skip empty array */
6827 if (moff
== mtrue_end
)
6830 msize
/= total_nelems
;
6831 elem_idx
= (off
- moff
) / msize
;
6832 moff
+= elem_idx
* msize
;
6837 /* the 'off' we're looking for is either equal to start
6838 * of this field or inside of this struct
6840 if (btf_type_is_struct(mtype
)) {
6841 /* our field must be inside that union or struct */
6844 /* return if the offset matches the member offset */
6850 /* adjust offset we're looking for */
6855 if (btf_type_is_ptr(mtype
)) {
6856 const struct btf_type
*stype
, *t
;
6857 enum bpf_type_flag tmp_flag
= 0;
6860 if (msize
!= size
|| off
!= moff
) {
6862 "cannot access ptr member %s with moff %u in struct %s with off %u size %u\n",
6863 mname
, moff
, tname
, off
, size
);
6867 /* check type tag */
6868 t
= btf_type_by_id(btf
, mtype
->type
);
6869 if (btf_type_is_type_tag(t
)) {
6870 tag_value
= __btf_name_by_offset(btf
, t
->name_off
);
6871 /* check __user tag */
6872 if (strcmp(tag_value
, "user") == 0)
6873 tmp_flag
= MEM_USER
;
6874 /* check __percpu tag */
6875 if (strcmp(tag_value
, "percpu") == 0)
6876 tmp_flag
= MEM_PERCPU
;
6877 /* check __rcu tag */
6878 if (strcmp(tag_value
, "rcu") == 0)
6882 stype
= btf_type_skip_modifiers(btf
, mtype
->type
, &id
);
6883 if (btf_type_is_struct(stype
)) {
6887 *field_name
= mname
;
6892 /* Allow more flexible access within an int as long as
6893 * it is within mtrue_end.
6894 * Since mtrue_end could be the end of an array,
6895 * that also allows using an array of int as a scratch
6896 * space. e.g. skb->cb[].
6898 if (off
+ size
> mtrue_end
&& !(*flag
& PTR_UNTRUSTED
)) {
6900 "access beyond the end of member %s (mend:%u) in struct %s with off %u size %u\n",
6901 mname
, mtrue_end
, tname
, off
, size
);
6907 bpf_log(log
, "struct %s doesn't have field at offset %d\n", tname
, off
);
6911 int btf_struct_access(struct bpf_verifier_log
*log
,
6912 const struct bpf_reg_state
*reg
,
6913 int off
, int size
, enum bpf_access_type atype __maybe_unused
,
6914 u32
*next_btf_id
, enum bpf_type_flag
*flag
,
6915 const char **field_name
)
6917 const struct btf
*btf
= reg
->btf
;
6918 enum bpf_type_flag tmp_flag
= 0;
6919 const struct btf_type
*t
;
6920 u32 id
= reg
->btf_id
;
6923 while (type_is_alloc(reg
->type
)) {
6924 struct btf_struct_meta
*meta
;
6925 struct btf_record
*rec
;
6928 meta
= btf_find_struct_meta(btf
, id
);
6932 for (i
= 0; i
< rec
->cnt
; i
++) {
6933 struct btf_field
*field
= &rec
->fields
[i
];
6934 u32 offset
= field
->offset
;
6935 if (off
< offset
+ field
->size
&& offset
< off
+ size
) {
6937 "direct access to %s is disallowed\n",
6938 btf_field_type_name(field
->type
));
6945 t
= btf_type_by_id(btf
, id
);
6947 err
= btf_struct_walk(log
, btf
, t
, off
, size
, &id
, &tmp_flag
, field_name
);
6951 /* For local types, the destination register cannot
6952 * become a pointer again.
6954 if (type_is_alloc(reg
->type
))
6955 return SCALAR_VALUE
;
6956 /* If we found the pointer or scalar on t+off,
6961 return PTR_TO_BTF_ID
;
6963 return SCALAR_VALUE
;
6965 /* We found nested struct, so continue the search
6966 * by diving in it. At this point the offset is
6967 * aligned with the new type, so set it to 0.
6969 t
= btf_type_by_id(btf
, id
);
6973 /* It's either error or unknown return value..
6976 if (WARN_ONCE(err
> 0, "unknown btf_struct_walk return value"))
6985 /* Check that two BTF types, each specified as an BTF object + id, are exactly
6986 * the same. Trivial ID check is not enough due to module BTFs, because we can
6987 * end up with two different module BTFs, but IDs point to the common type in
6990 bool btf_types_are_same(const struct btf
*btf1
, u32 id1
,
6991 const struct btf
*btf2
, u32 id2
)
6997 return btf_type_by_id(btf1
, id1
) == btf_type_by_id(btf2
, id2
);
7000 bool btf_struct_ids_match(struct bpf_verifier_log
*log
,
7001 const struct btf
*btf
, u32 id
, int off
,
7002 const struct btf
*need_btf
, u32 need_type_id
,
7005 const struct btf_type
*type
;
7006 enum bpf_type_flag flag
= 0;
7009 /* Are we already done? */
7010 if (off
== 0 && btf_types_are_same(btf
, id
, need_btf
, need_type_id
))
7012 /* In case of strict type match, we do not walk struct, the top level
7013 * type match must succeed. When strict is true, off should have already
7019 type
= btf_type_by_id(btf
, id
);
7022 err
= btf_struct_walk(log
, btf
, type
, off
, 1, &id
, &flag
, NULL
);
7023 if (err
!= WALK_STRUCT
)
7026 /* We found nested struct object. If it matches
7027 * the requested ID, we're done. Otherwise let's
7028 * continue the search with offset 0 in the new
7031 if (!btf_types_are_same(btf
, id
, need_btf
, need_type_id
)) {
7039 static int __get_type_size(struct btf
*btf
, u32 btf_id
,
7040 const struct btf_type
**ret_type
)
7042 const struct btf_type
*t
;
7044 *ret_type
= btf_type_by_id(btf
, 0);
7048 t
= btf_type_by_id(btf
, btf_id
);
7049 while (t
&& btf_type_is_modifier(t
))
7050 t
= btf_type_by_id(btf
, t
->type
);
7054 if (btf_type_is_ptr(t
))
7055 /* kernel size of pointer. Not BPF's size of pointer*/
7056 return sizeof(void *);
7057 if (btf_type_is_int(t
) || btf_is_any_enum(t
) || __btf_type_is_struct(t
))
7062 static u8
__get_type_fmodel_flags(const struct btf_type
*t
)
7066 if (__btf_type_is_struct(t
))
7067 flags
|= BTF_FMODEL_STRUCT_ARG
;
7068 if (btf_type_is_signed_int(t
))
7069 flags
|= BTF_FMODEL_SIGNED_ARG
;
7074 int btf_distill_func_proto(struct bpf_verifier_log
*log
,
7076 const struct btf_type
*func
,
7078 struct btf_func_model
*m
)
7080 const struct btf_param
*args
;
7081 const struct btf_type
*t
;
7086 /* BTF function prototype doesn't match the verifier types.
7087 * Fall back to MAX_BPF_FUNC_REG_ARGS u64 args.
7089 for (i
= 0; i
< MAX_BPF_FUNC_REG_ARGS
; i
++) {
7091 m
->arg_flags
[i
] = 0;
7095 m
->nr_args
= MAX_BPF_FUNC_REG_ARGS
;
7098 args
= (const struct btf_param
*)(func
+ 1);
7099 nargs
= btf_type_vlen(func
);
7100 if (nargs
> MAX_BPF_FUNC_ARGS
) {
7102 "The function %s has %d arguments. Too many.\n",
7106 ret
= __get_type_size(btf
, func
->type
, &t
);
7107 if (ret
< 0 || __btf_type_is_struct(t
)) {
7109 "The function %s return type %s is unsupported.\n",
7110 tname
, btf_type_str(t
));
7114 m
->ret_flags
= __get_type_fmodel_flags(t
);
7116 for (i
= 0; i
< nargs
; i
++) {
7117 if (i
== nargs
- 1 && args
[i
].type
== 0) {
7119 "The function %s with variable args is unsupported.\n",
7123 ret
= __get_type_size(btf
, args
[i
].type
, &t
);
7125 /* No support of struct argument size greater than 16 bytes */
7126 if (ret
< 0 || ret
> 16) {
7128 "The function %s arg%d type %s is unsupported.\n",
7129 tname
, i
, btf_type_str(t
));
7134 "The function %s has malformed void argument.\n",
7138 m
->arg_size
[i
] = ret
;
7139 m
->arg_flags
[i
] = __get_type_fmodel_flags(t
);
7145 /* Compare BTFs of two functions assuming only scalars and pointers to context.
7146 * t1 points to BTF_KIND_FUNC in btf1
7147 * t2 points to BTF_KIND_FUNC in btf2
7149 * EINVAL - function prototype mismatch
7150 * EFAULT - verifier bug
7151 * 0 - 99% match. The last 1% is validated by the verifier.
7153 static int btf_check_func_type_match(struct bpf_verifier_log
*log
,
7154 struct btf
*btf1
, const struct btf_type
*t1
,
7155 struct btf
*btf2
, const struct btf_type
*t2
)
7157 const struct btf_param
*args1
, *args2
;
7158 const char *fn1
, *fn2
, *s1
, *s2
;
7159 u32 nargs1
, nargs2
, i
;
7161 fn1
= btf_name_by_offset(btf1
, t1
->name_off
);
7162 fn2
= btf_name_by_offset(btf2
, t2
->name_off
);
7164 if (btf_func_linkage(t1
) != BTF_FUNC_GLOBAL
) {
7165 bpf_log(log
, "%s() is not a global function\n", fn1
);
7168 if (btf_func_linkage(t2
) != BTF_FUNC_GLOBAL
) {
7169 bpf_log(log
, "%s() is not a global function\n", fn2
);
7173 t1
= btf_type_by_id(btf1
, t1
->type
);
7174 if (!t1
|| !btf_type_is_func_proto(t1
))
7176 t2
= btf_type_by_id(btf2
, t2
->type
);
7177 if (!t2
|| !btf_type_is_func_proto(t2
))
7180 args1
= (const struct btf_param
*)(t1
+ 1);
7181 nargs1
= btf_type_vlen(t1
);
7182 args2
= (const struct btf_param
*)(t2
+ 1);
7183 nargs2
= btf_type_vlen(t2
);
7185 if (nargs1
!= nargs2
) {
7186 bpf_log(log
, "%s() has %d args while %s() has %d args\n",
7187 fn1
, nargs1
, fn2
, nargs2
);
7191 t1
= btf_type_skip_modifiers(btf1
, t1
->type
, NULL
);
7192 t2
= btf_type_skip_modifiers(btf2
, t2
->type
, NULL
);
7193 if (t1
->info
!= t2
->info
) {
7195 "Return type %s of %s() doesn't match type %s of %s()\n",
7196 btf_type_str(t1
), fn1
,
7197 btf_type_str(t2
), fn2
);
7201 for (i
= 0; i
< nargs1
; i
++) {
7202 t1
= btf_type_skip_modifiers(btf1
, args1
[i
].type
, NULL
);
7203 t2
= btf_type_skip_modifiers(btf2
, args2
[i
].type
, NULL
);
7205 if (t1
->info
!= t2
->info
) {
7206 bpf_log(log
, "arg%d in %s() is %s while %s() has %s\n",
7207 i
, fn1
, btf_type_str(t1
),
7208 fn2
, btf_type_str(t2
));
7211 if (btf_type_has_size(t1
) && t1
->size
!= t2
->size
) {
7213 "arg%d in %s() has size %d while %s() has %d\n",
7219 /* global functions are validated with scalars and pointers
7220 * to context only. And only global functions can be replaced.
7221 * Hence type check only those types.
7223 if (btf_type_is_int(t1
) || btf_is_any_enum(t1
))
7225 if (!btf_type_is_ptr(t1
)) {
7227 "arg%d in %s() has unrecognized type\n",
7231 t1
= btf_type_skip_modifiers(btf1
, t1
->type
, NULL
);
7232 t2
= btf_type_skip_modifiers(btf2
, t2
->type
, NULL
);
7233 if (!btf_type_is_struct(t1
)) {
7235 "arg%d in %s() is not a pointer to context\n",
7239 if (!btf_type_is_struct(t2
)) {
7241 "arg%d in %s() is not a pointer to context\n",
7245 /* This is an optional check to make program writing easier.
7246 * Compare names of structs and report an error to the user.
7247 * btf_prepare_func_args() already checked that t2 struct
7248 * is a context type. btf_prepare_func_args() will check
7249 * later that t1 struct is a context type as well.
7251 s1
= btf_name_by_offset(btf1
, t1
->name_off
);
7252 s2
= btf_name_by_offset(btf2
, t2
->name_off
);
7253 if (strcmp(s1
, s2
)) {
7255 "arg%d %s(struct %s *) doesn't match %s(struct %s *)\n",
7256 i
, fn1
, s1
, fn2
, s2
);
7263 /* Compare BTFs of given program with BTF of target program */
7264 int btf_check_type_match(struct bpf_verifier_log
*log
, const struct bpf_prog
*prog
,
7265 struct btf
*btf2
, const struct btf_type
*t2
)
7267 struct btf
*btf1
= prog
->aux
->btf
;
7268 const struct btf_type
*t1
;
7271 if (!prog
->aux
->func_info
) {
7272 bpf_log(log
, "Program extension requires BTF\n");
7276 btf_id
= prog
->aux
->func_info
[0].type_id
;
7280 t1
= btf_type_by_id(btf1
, btf_id
);
7281 if (!t1
|| !btf_type_is_func(t1
))
7284 return btf_check_func_type_match(log
, btf1
, t1
, btf2
, t2
);
7287 static bool btf_is_dynptr_ptr(const struct btf
*btf
, const struct btf_type
*t
)
7291 t
= btf_type_by_id(btf
, t
->type
); /* skip PTR */
7293 while (btf_type_is_modifier(t
))
7294 t
= btf_type_by_id(btf
, t
->type
);
7296 /* allow either struct or struct forward declaration */
7297 if (btf_type_is_struct(t
) ||
7298 (btf_type_is_fwd(t
) && btf_type_kflag(t
) == 0)) {
7299 name
= btf_str_by_offset(btf
, t
->name_off
);
7300 return name
&& strcmp(name
, "bpf_dynptr") == 0;
7306 struct bpf_cand_cache
{
7312 const struct btf
*btf
;
7317 static DEFINE_MUTEX(cand_cache_mutex
);
7319 static struct bpf_cand_cache
*
7320 bpf_core_find_cands(struct bpf_core_ctx
*ctx
, u32 local_type_id
);
7322 static int btf_get_ptr_to_btf_id(struct bpf_verifier_log
*log
, int arg_idx
,
7323 const struct btf
*btf
, const struct btf_type
*t
)
7325 struct bpf_cand_cache
*cc
;
7326 struct bpf_core_ctx ctx
= {
7330 u32 kern_type_id
, type_id
;
7333 /* skip PTR and modifiers */
7335 t
= btf_type_by_id(btf
, t
->type
);
7336 while (btf_type_is_modifier(t
)) {
7338 t
= btf_type_by_id(btf
, t
->type
);
7341 mutex_lock(&cand_cache_mutex
);
7342 cc
= bpf_core_find_cands(&ctx
, type_id
);
7345 bpf_log(log
, "arg#%d reference type('%s %s') candidate matching error: %d\n",
7346 arg_idx
, btf_type_str(t
), __btf_name_by_offset(btf
, t
->name_off
),
7348 goto cand_cache_unlock
;
7351 bpf_log(log
, "arg#%d reference type('%s %s') %s\n",
7352 arg_idx
, btf_type_str(t
), __btf_name_by_offset(btf
, t
->name_off
),
7353 cc
->cnt
== 0 ? "has no matches" : "is ambiguous");
7354 err
= cc
->cnt
== 0 ? -ENOENT
: -ESRCH
;
7355 goto cand_cache_unlock
;
7357 if (btf_is_module(cc
->cands
[0].btf
)) {
7358 bpf_log(log
, "arg#%d reference type('%s %s') points to kernel module type (unsupported)\n",
7359 arg_idx
, btf_type_str(t
), __btf_name_by_offset(btf
, t
->name_off
));
7361 goto cand_cache_unlock
;
7363 kern_type_id
= cc
->cands
[0].id
;
7366 mutex_unlock(&cand_cache_mutex
);
7370 return kern_type_id
;
7374 ARG_TAG_CTX
= BIT_ULL(0),
7375 ARG_TAG_NONNULL
= BIT_ULL(1),
7376 ARG_TAG_TRUSTED
= BIT_ULL(2),
7377 ARG_TAG_NULLABLE
= BIT_ULL(3),
7378 ARG_TAG_ARENA
= BIT_ULL(4),
7381 /* Process BTF of a function to produce high-level expectation of function
7382 * arguments (like ARG_PTR_TO_CTX, or ARG_PTR_TO_MEM, etc). This information
7383 * is cached in subprog info for reuse.
7385 * EFAULT - there is a verifier bug. Abort verification.
7386 * EINVAL - cannot convert BTF.
7387 * 0 - Successfully processed BTF and constructed argument expectations.
7389 int btf_prepare_func_args(struct bpf_verifier_env
*env
, int subprog
)
7391 bool is_global
= subprog_aux(env
, subprog
)->linkage
== BTF_FUNC_GLOBAL
;
7392 struct bpf_subprog_info
*sub
= subprog_info(env
, subprog
);
7393 struct bpf_verifier_log
*log
= &env
->log
;
7394 struct bpf_prog
*prog
= env
->prog
;
7395 enum bpf_prog_type prog_type
= prog
->type
;
7396 struct btf
*btf
= prog
->aux
->btf
;
7397 const struct btf_param
*args
;
7398 const struct btf_type
*t
, *ref_t
, *fn_t
;
7399 u32 i
, nargs
, btf_id
;
7402 if (sub
->args_cached
)
7405 if (!prog
->aux
->func_info
) {
7406 bpf_log(log
, "Verifier bug\n");
7410 btf_id
= prog
->aux
->func_info
[subprog
].type_id
;
7412 if (!is_global
) /* not fatal for static funcs */
7414 bpf_log(log
, "Global functions need valid BTF\n");
7418 fn_t
= btf_type_by_id(btf
, btf_id
);
7419 if (!fn_t
|| !btf_type_is_func(fn_t
)) {
7420 /* These checks were already done by the verifier while loading
7421 * struct bpf_func_info
7423 bpf_log(log
, "BTF of func#%d doesn't point to KIND_FUNC\n",
7427 tname
= btf_name_by_offset(btf
, fn_t
->name_off
);
7429 if (prog
->aux
->func_info_aux
[subprog
].unreliable
) {
7430 bpf_log(log
, "Verifier bug in function %s()\n", tname
);
7433 if (prog_type
== BPF_PROG_TYPE_EXT
)
7434 prog_type
= prog
->aux
->dst_prog
->type
;
7436 t
= btf_type_by_id(btf
, fn_t
->type
);
7437 if (!t
|| !btf_type_is_func_proto(t
)) {
7438 bpf_log(log
, "Invalid type of function %s()\n", tname
);
7441 args
= (const struct btf_param
*)(t
+ 1);
7442 nargs
= btf_type_vlen(t
);
7443 if (nargs
> MAX_BPF_FUNC_REG_ARGS
) {
7446 bpf_log(log
, "Global function %s() with %d > %d args. Buggy compiler.\n",
7447 tname
, nargs
, MAX_BPF_FUNC_REG_ARGS
);
7450 /* check that function returns int, exception cb also requires this */
7451 t
= btf_type_by_id(btf
, t
->type
);
7452 while (btf_type_is_modifier(t
))
7453 t
= btf_type_by_id(btf
, t
->type
);
7454 if (!btf_type_is_int(t
) && !btf_is_any_enum(t
)) {
7458 "Global function %s() doesn't return scalar. Only those are supported.\n",
7462 /* Convert BTF function arguments into verifier types.
7463 * Only PTR_TO_CTX and SCALAR are supported atm.
7465 for (i
= 0; i
< nargs
; i
++) {
7469 /* 'arg:<tag>' decl_tag takes precedence over derivation of
7470 * register type from BTF type itself
7472 while ((id
= btf_find_next_decl_tag(btf
, fn_t
, i
, "arg:", id
)) > 0) {
7473 const struct btf_type
*tag_t
= btf_type_by_id(btf
, id
);
7474 const char *tag
= __btf_name_by_offset(btf
, tag_t
->name_off
) + 4;
7476 /* disallow arg tags in static subprogs */
7478 bpf_log(log
, "arg#%d type tag is not supported in static functions\n", i
);
7482 if (strcmp(tag
, "ctx") == 0) {
7483 tags
|= ARG_TAG_CTX
;
7484 } else if (strcmp(tag
, "trusted") == 0) {
7485 tags
|= ARG_TAG_TRUSTED
;
7486 } else if (strcmp(tag
, "nonnull") == 0) {
7487 tags
|= ARG_TAG_NONNULL
;
7488 } else if (strcmp(tag
, "nullable") == 0) {
7489 tags
|= ARG_TAG_NULLABLE
;
7490 } else if (strcmp(tag
, "arena") == 0) {
7491 tags
|= ARG_TAG_ARENA
;
7493 bpf_log(log
, "arg#%d has unsupported set of tags\n", i
);
7497 if (id
!= -ENOENT
) {
7498 bpf_log(log
, "arg#%d type tag fetching failure: %d\n", i
, id
);
7502 t
= btf_type_by_id(btf
, args
[i
].type
);
7503 while (btf_type_is_modifier(t
))
7504 t
= btf_type_by_id(btf
, t
->type
);
7505 if (!btf_type_is_ptr(t
))
7508 if ((tags
& ARG_TAG_CTX
) || btf_is_prog_ctx_type(log
, btf
, t
, prog_type
, i
)) {
7509 if (tags
& ~ARG_TAG_CTX
) {
7510 bpf_log(log
, "arg#%d has invalid combination of tags\n", i
);
7513 if ((tags
& ARG_TAG_CTX
) &&
7514 btf_validate_prog_ctx_type(log
, btf
, t
, i
, prog_type
,
7515 prog
->expected_attach_type
))
7517 sub
->args
[i
].arg_type
= ARG_PTR_TO_CTX
;
7520 if (btf_is_dynptr_ptr(btf
, t
)) {
7522 bpf_log(log
, "arg#%d has invalid combination of tags\n", i
);
7525 sub
->args
[i
].arg_type
= ARG_PTR_TO_DYNPTR
| MEM_RDONLY
;
7528 if (tags
& ARG_TAG_TRUSTED
) {
7531 if (tags
& ARG_TAG_NONNULL
) {
7532 bpf_log(log
, "arg#%d has invalid combination of tags\n", i
);
7536 kern_type_id
= btf_get_ptr_to_btf_id(log
, i
, btf
, t
);
7537 if (kern_type_id
< 0)
7538 return kern_type_id
;
7540 sub
->args
[i
].arg_type
= ARG_PTR_TO_BTF_ID
| PTR_TRUSTED
;
7541 if (tags
& ARG_TAG_NULLABLE
)
7542 sub
->args
[i
].arg_type
|= PTR_MAYBE_NULL
;
7543 sub
->args
[i
].btf_id
= kern_type_id
;
7546 if (tags
& ARG_TAG_ARENA
) {
7547 if (tags
& ~ARG_TAG_ARENA
) {
7548 bpf_log(log
, "arg#%d arena cannot be combined with any other tags\n", i
);
7551 sub
->args
[i
].arg_type
= ARG_PTR_TO_ARENA
;
7554 if (is_global
) { /* generic user data pointer */
7557 if (tags
& ARG_TAG_NULLABLE
) {
7558 bpf_log(log
, "arg#%d has invalid combination of tags\n", i
);
7562 t
= btf_type_skip_modifiers(btf
, t
->type
, NULL
);
7563 ref_t
= btf_resolve_size(btf
, t
, &mem_size
);
7564 if (IS_ERR(ref_t
)) {
7565 bpf_log(log
, "arg#%d reference type('%s %s') size cannot be determined: %ld\n",
7566 i
, btf_type_str(t
), btf_name_by_offset(btf
, t
->name_off
),
7571 sub
->args
[i
].arg_type
= ARG_PTR_TO_MEM
| PTR_MAYBE_NULL
;
7572 if (tags
& ARG_TAG_NONNULL
)
7573 sub
->args
[i
].arg_type
&= ~PTR_MAYBE_NULL
;
7574 sub
->args
[i
].mem_size
= mem_size
;
7580 bpf_log(log
, "arg#%d has pointer tag, but is not a pointer type\n", i
);
7583 if (btf_type_is_int(t
) || btf_is_any_enum(t
)) {
7584 sub
->args
[i
].arg_type
= ARG_ANYTHING
;
7589 bpf_log(log
, "Arg#%d type %s in %s() is not supported yet.\n",
7590 i
, btf_type_str(t
), tname
);
7594 sub
->arg_cnt
= nargs
;
7595 sub
->args_cached
= true;
7600 static void btf_type_show(const struct btf
*btf
, u32 type_id
, void *obj
,
7601 struct btf_show
*show
)
7603 const struct btf_type
*t
= btf_type_by_id(btf
, type_id
);
7606 memset(&show
->state
, 0, sizeof(show
->state
));
7607 memset(&show
->obj
, 0, sizeof(show
->obj
));
7609 btf_type_ops(t
)->show(btf
, t
, type_id
, obj
, 0, show
);
7612 __printf(2, 0) static void btf_seq_show(struct btf_show
*show
, const char *fmt
,
7615 seq_vprintf((struct seq_file
*)show
->target
, fmt
, args
);
7618 int btf_type_seq_show_flags(const struct btf
*btf
, u32 type_id
,
7619 void *obj
, struct seq_file
*m
, u64 flags
)
7621 struct btf_show sseq
;
7624 sseq
.showfn
= btf_seq_show
;
7627 btf_type_show(btf
, type_id
, obj
, &sseq
);
7629 return sseq
.state
.status
;
7632 void btf_type_seq_show(const struct btf
*btf
, u32 type_id
, void *obj
,
7635 (void) btf_type_seq_show_flags(btf
, type_id
, obj
, m
,
7636 BTF_SHOW_NONAME
| BTF_SHOW_COMPACT
|
7637 BTF_SHOW_ZERO
| BTF_SHOW_UNSAFE
);
7640 struct btf_show_snprintf
{
7641 struct btf_show show
;
7642 int len_left
; /* space left in string */
7643 int len
; /* length we would have written */
7646 __printf(2, 0) static void btf_snprintf_show(struct btf_show
*show
, const char *fmt
,
7649 struct btf_show_snprintf
*ssnprintf
= (struct btf_show_snprintf
*)show
;
7652 len
= vsnprintf(show
->target
, ssnprintf
->len_left
, fmt
, args
);
7655 ssnprintf
->len_left
= 0;
7656 ssnprintf
->len
= len
;
7657 } else if (len
>= ssnprintf
->len_left
) {
7658 /* no space, drive on to get length we would have written */
7659 ssnprintf
->len_left
= 0;
7660 ssnprintf
->len
+= len
;
7662 ssnprintf
->len_left
-= len
;
7663 ssnprintf
->len
+= len
;
7664 show
->target
+= len
;
7668 int btf_type_snprintf_show(const struct btf
*btf
, u32 type_id
, void *obj
,
7669 char *buf
, int len
, u64 flags
)
7671 struct btf_show_snprintf ssnprintf
;
7673 ssnprintf
.show
.target
= buf
;
7674 ssnprintf
.show
.flags
= flags
;
7675 ssnprintf
.show
.showfn
= btf_snprintf_show
;
7676 ssnprintf
.len_left
= len
;
7679 btf_type_show(btf
, type_id
, obj
, (struct btf_show
*)&ssnprintf
);
7681 /* If we encountered an error, return it. */
7682 if (ssnprintf
.show
.state
.status
)
7683 return ssnprintf
.show
.state
.status
;
7685 /* Otherwise return length we would have written */
7686 return ssnprintf
.len
;
7689 #ifdef CONFIG_PROC_FS
7690 static void bpf_btf_show_fdinfo(struct seq_file
*m
, struct file
*filp
)
7692 const struct btf
*btf
= filp
->private_data
;
7694 seq_printf(m
, "btf_id:\t%u\n", btf
->id
);
7698 static int btf_release(struct inode
*inode
, struct file
*filp
)
7700 btf_put(filp
->private_data
);
7704 const struct file_operations btf_fops
= {
7705 #ifdef CONFIG_PROC_FS
7706 .show_fdinfo
= bpf_btf_show_fdinfo
,
7708 .release
= btf_release
,
7711 static int __btf_new_fd(struct btf
*btf
)
7713 return anon_inode_getfd("btf", &btf_fops
, btf
, O_RDONLY
| O_CLOEXEC
);
7716 int btf_new_fd(const union bpf_attr
*attr
, bpfptr_t uattr
, u32 uattr_size
)
7721 btf
= btf_parse(attr
, uattr
, uattr_size
);
7723 return PTR_ERR(btf
);
7725 ret
= btf_alloc_id(btf
);
7732 * The BTF ID is published to the userspace.
7733 * All BTF free must go through call_rcu() from
7734 * now on (i.e. free by calling btf_put()).
7737 ret
= __btf_new_fd(btf
);
7744 struct btf
*btf_get_by_fd(int fd
)
7750 return ERR_PTR(-EBADF
);
7752 if (fd_file(f
)->f_op
!= &btf_fops
)
7753 return ERR_PTR(-EINVAL
);
7755 btf
= fd_file(f
)->private_data
;
7756 refcount_inc(&btf
->refcnt
);
7761 int btf_get_info_by_fd(const struct btf
*btf
,
7762 const union bpf_attr
*attr
,
7763 union bpf_attr __user
*uattr
)
7765 struct bpf_btf_info __user
*uinfo
;
7766 struct bpf_btf_info info
;
7767 u32 info_copy
, btf_copy
;
7770 u32 uinfo_len
, uname_len
, name_len
;
7773 uinfo
= u64_to_user_ptr(attr
->info
.info
);
7774 uinfo_len
= attr
->info
.info_len
;
7776 info_copy
= min_t(u32
, uinfo_len
, sizeof(info
));
7777 memset(&info
, 0, sizeof(info
));
7778 if (copy_from_user(&info
, uinfo
, info_copy
))
7782 ubtf
= u64_to_user_ptr(info
.btf
);
7783 btf_copy
= min_t(u32
, btf
->data_size
, info
.btf_size
);
7784 if (copy_to_user(ubtf
, btf
->data
, btf_copy
))
7786 info
.btf_size
= btf
->data_size
;
7788 info
.kernel_btf
= btf
->kernel_btf
;
7790 uname
= u64_to_user_ptr(info
.name
);
7791 uname_len
= info
.name_len
;
7792 if (!uname
^ !uname_len
)
7795 name_len
= strlen(btf
->name
);
7796 info
.name_len
= name_len
;
7799 if (uname_len
>= name_len
+ 1) {
7800 if (copy_to_user(uname
, btf
->name
, name_len
+ 1))
7805 if (copy_to_user(uname
, btf
->name
, uname_len
- 1))
7807 if (put_user(zero
, uname
+ uname_len
- 1))
7809 /* let user-space know about too short buffer */
7814 if (copy_to_user(uinfo
, &info
, info_copy
) ||
7815 put_user(info_copy
, &uattr
->info
.info_len
))
7821 int btf_get_fd_by_id(u32 id
)
7827 btf
= idr_find(&btf_idr
, id
);
7828 if (!btf
|| !refcount_inc_not_zero(&btf
->refcnt
))
7829 btf
= ERR_PTR(-ENOENT
);
7833 return PTR_ERR(btf
);
7835 fd
= __btf_new_fd(btf
);
7842 u32
btf_obj_id(const struct btf
*btf
)
7847 bool btf_is_kernel(const struct btf
*btf
)
7849 return btf
->kernel_btf
;
7852 bool btf_is_module(const struct btf
*btf
)
7854 return btf
->kernel_btf
&& strcmp(btf
->name
, "vmlinux") != 0;
7858 BTF_MODULE_F_LIVE
= (1 << 0),
7861 #ifdef CONFIG_DEBUG_INFO_BTF_MODULES
7863 struct list_head list
;
7864 struct module
*module
;
7866 struct bin_attribute
*sysfs_attr
;
7870 static LIST_HEAD(btf_modules
);
7871 static DEFINE_MUTEX(btf_module_mutex
);
7874 btf_module_read(struct file
*file
, struct kobject
*kobj
,
7875 struct bin_attribute
*bin_attr
,
7876 char *buf
, loff_t off
, size_t len
)
7878 const struct btf
*btf
= bin_attr
->private;
7880 memcpy(buf
, btf
->data
+ off
, len
);
7884 static void purge_cand_cache(struct btf
*btf
);
7886 static int btf_module_notify(struct notifier_block
*nb
, unsigned long op
,
7889 struct btf_module
*btf_mod
, *tmp
;
7890 struct module
*mod
= module
;
7894 if (mod
->btf_data_size
== 0 ||
7895 (op
!= MODULE_STATE_COMING
&& op
!= MODULE_STATE_LIVE
&&
7896 op
!= MODULE_STATE_GOING
))
7900 case MODULE_STATE_COMING
:
7901 btf_mod
= kzalloc(sizeof(*btf_mod
), GFP_KERNEL
);
7906 btf
= btf_parse_module(mod
->name
, mod
->btf_data
, mod
->btf_data_size
,
7907 mod
->btf_base_data
, mod
->btf_base_data_size
);
7910 if (!IS_ENABLED(CONFIG_MODULE_ALLOW_BTF_MISMATCH
)) {
7911 pr_warn("failed to validate module [%s] BTF: %ld\n",
7912 mod
->name
, PTR_ERR(btf
));
7915 pr_warn_once("Kernel module BTF mismatch detected, BTF debug info may be unavailable for some modules\n");
7919 err
= btf_alloc_id(btf
);
7926 purge_cand_cache(NULL
);
7927 mutex_lock(&btf_module_mutex
);
7928 btf_mod
->module
= module
;
7930 list_add(&btf_mod
->list
, &btf_modules
);
7931 mutex_unlock(&btf_module_mutex
);
7933 if (IS_ENABLED(CONFIG_SYSFS
)) {
7934 struct bin_attribute
*attr
;
7936 attr
= kzalloc(sizeof(*attr
), GFP_KERNEL
);
7940 sysfs_bin_attr_init(attr
);
7941 attr
->attr
.name
= btf
->name
;
7942 attr
->attr
.mode
= 0444;
7943 attr
->size
= btf
->data_size
;
7944 attr
->private = btf
;
7945 attr
->read
= btf_module_read
;
7947 err
= sysfs_create_bin_file(btf_kobj
, attr
);
7949 pr_warn("failed to register module [%s] BTF in sysfs: %d\n",
7956 btf_mod
->sysfs_attr
= attr
;
7960 case MODULE_STATE_LIVE
:
7961 mutex_lock(&btf_module_mutex
);
7962 list_for_each_entry_safe(btf_mod
, tmp
, &btf_modules
, list
) {
7963 if (btf_mod
->module
!= module
)
7966 btf_mod
->flags
|= BTF_MODULE_F_LIVE
;
7969 mutex_unlock(&btf_module_mutex
);
7971 case MODULE_STATE_GOING
:
7972 mutex_lock(&btf_module_mutex
);
7973 list_for_each_entry_safe(btf_mod
, tmp
, &btf_modules
, list
) {
7974 if (btf_mod
->module
!= module
)
7977 list_del(&btf_mod
->list
);
7978 if (btf_mod
->sysfs_attr
)
7979 sysfs_remove_bin_file(btf_kobj
, btf_mod
->sysfs_attr
);
7980 purge_cand_cache(btf_mod
->btf
);
7981 btf_put(btf_mod
->btf
);
7982 kfree(btf_mod
->sysfs_attr
);
7986 mutex_unlock(&btf_module_mutex
);
7990 return notifier_from_errno(err
);
7993 static struct notifier_block btf_module_nb
= {
7994 .notifier_call
= btf_module_notify
,
7997 static int __init
btf_module_init(void)
7999 register_module_notifier(&btf_module_nb
);
8003 fs_initcall(btf_module_init
);
8004 #endif /* CONFIG_DEBUG_INFO_BTF_MODULES */
8006 struct module
*btf_try_get_module(const struct btf
*btf
)
8008 struct module
*res
= NULL
;
8009 #ifdef CONFIG_DEBUG_INFO_BTF_MODULES
8010 struct btf_module
*btf_mod
, *tmp
;
8012 mutex_lock(&btf_module_mutex
);
8013 list_for_each_entry_safe(btf_mod
, tmp
, &btf_modules
, list
) {
8014 if (btf_mod
->btf
!= btf
)
8017 /* We must only consider module whose __init routine has
8018 * finished, hence we must check for BTF_MODULE_F_LIVE flag,
8019 * which is set from the notifier callback for
8020 * MODULE_STATE_LIVE.
8022 if ((btf_mod
->flags
& BTF_MODULE_F_LIVE
) && try_module_get(btf_mod
->module
))
8023 res
= btf_mod
->module
;
8027 mutex_unlock(&btf_module_mutex
);
8033 /* Returns struct btf corresponding to the struct module.
8034 * This function can return NULL or ERR_PTR.
8036 static struct btf
*btf_get_module_btf(const struct module
*module
)
8038 #ifdef CONFIG_DEBUG_INFO_BTF_MODULES
8039 struct btf_module
*btf_mod
, *tmp
;
8041 struct btf
*btf
= NULL
;
8044 btf
= bpf_get_btf_vmlinux();
8045 if (!IS_ERR_OR_NULL(btf
))
8050 #ifdef CONFIG_DEBUG_INFO_BTF_MODULES
8051 mutex_lock(&btf_module_mutex
);
8052 list_for_each_entry_safe(btf_mod
, tmp
, &btf_modules
, list
) {
8053 if (btf_mod
->module
!= module
)
8056 btf_get(btf_mod
->btf
);
8060 mutex_unlock(&btf_module_mutex
);
8066 static int check_btf_kconfigs(const struct module
*module
, const char *feature
)
8068 if (!module
&& IS_ENABLED(CONFIG_DEBUG_INFO_BTF
)) {
8069 pr_err("missing vmlinux BTF, cannot register %s\n", feature
);
8072 if (module
&& IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES
))
8073 pr_warn("missing module BTF, cannot register %s\n", feature
);
8077 BPF_CALL_4(bpf_btf_find_by_name_kind
, char *, name
, int, name_sz
, u32
, kind
, int, flags
)
8079 struct btf
*btf
= NULL
;
8086 if (name_sz
<= 1 || name
[name_sz
- 1])
8089 ret
= bpf_find_btf_id(name
, kind
, &btf
);
8090 if (ret
> 0 && btf_is_module(btf
)) {
8091 btf_obj_fd
= __btf_new_fd(btf
);
8092 if (btf_obj_fd
< 0) {
8096 return ret
| (((u64
)btf_obj_fd
) << 32);
8103 const struct bpf_func_proto bpf_btf_find_by_name_kind_proto
= {
8104 .func
= bpf_btf_find_by_name_kind
,
8106 .ret_type
= RET_INTEGER
,
8107 .arg1_type
= ARG_PTR_TO_MEM
| MEM_RDONLY
,
8108 .arg2_type
= ARG_CONST_SIZE
,
8109 .arg3_type
= ARG_ANYTHING
,
8110 .arg4_type
= ARG_ANYTHING
,
8113 BTF_ID_LIST_GLOBAL(btf_tracing_ids
, MAX_BTF_TRACING_TYPE
)
8114 #define BTF_TRACING_TYPE(name, type) BTF_ID(struct, type)
8115 BTF_TRACING_TYPE_xxx
8116 #undef BTF_TRACING_TYPE
8118 /* Validate well-formedness of iter argument type.
8119 * On success, return positive BTF ID of iter state's STRUCT type.
8120 * On error, negative error is returned.
8122 int btf_check_iter_arg(struct btf
*btf
, const struct btf_type
*func
, int arg_idx
)
8124 const struct btf_param
*arg
;
8125 const struct btf_type
*t
;
8129 if (btf_type_vlen(func
) <= arg_idx
)
8132 arg
= &btf_params(func
)[arg_idx
];
8133 t
= btf_type_skip_modifiers(btf
, arg
->type
, NULL
);
8134 if (!t
|| !btf_type_is_ptr(t
))
8136 t
= btf_type_skip_modifiers(btf
, t
->type
, &btf_id
);
8137 if (!t
|| !__btf_type_is_struct(t
))
8140 name
= btf_name_by_offset(btf
, t
->name_off
);
8141 if (!name
|| strncmp(name
, ITER_PREFIX
, sizeof(ITER_PREFIX
) - 1))
8147 static int btf_check_iter_kfuncs(struct btf
*btf
, const char *func_name
,
8148 const struct btf_type
*func
, u32 func_flags
)
8150 u32 flags
= func_flags
& (KF_ITER_NEW
| KF_ITER_NEXT
| KF_ITER_DESTROY
);
8151 const char *sfx
, *iter_name
;
8152 const struct btf_type
*t
;
8157 /* exactly one of KF_ITER_{NEW,NEXT,DESTROY} can be set */
8158 if (!flags
|| (flags
& (flags
- 1)))
8161 /* any BPF iter kfunc should have `struct bpf_iter_<type> *` first arg */
8162 nr_args
= btf_type_vlen(func
);
8166 btf_id
= btf_check_iter_arg(btf
, func
, 0);
8170 /* sizeof(struct bpf_iter_<type>) should be a multiple of 8 to
8171 * fit nicely in stack slots
8173 t
= btf_type_by_id(btf
, btf_id
);
8174 if (t
->size
== 0 || (t
->size
% 8))
8177 /* validate bpf_iter_<type>_{new,next,destroy}(struct bpf_iter_<type> *)
8180 iter_name
= btf_name_by_offset(btf
, t
->name_off
) + sizeof(ITER_PREFIX
) - 1;
8181 if (flags
& KF_ITER_NEW
)
8183 else if (flags
& KF_ITER_NEXT
)
8185 else /* (flags & KF_ITER_DESTROY) */
8188 snprintf(exp_name
, sizeof(exp_name
), "bpf_iter_%s_%s", iter_name
, sfx
);
8189 if (strcmp(func_name
, exp_name
))
8192 /* only iter constructor should have extra arguments */
8193 if (!(flags
& KF_ITER_NEW
) && nr_args
!= 1)
8196 if (flags
& KF_ITER_NEXT
) {
8197 /* bpf_iter_<type>_next() should return pointer */
8198 t
= btf_type_skip_modifiers(btf
, func
->type
, NULL
);
8199 if (!t
|| !btf_type_is_ptr(t
))
8203 if (flags
& KF_ITER_DESTROY
) {
8204 /* bpf_iter_<type>_destroy() should return void */
8205 t
= btf_type_by_id(btf
, func
->type
);
8206 if (!t
|| !btf_type_is_void(t
))
8213 static int btf_check_kfunc_protos(struct btf
*btf
, u32 func_id
, u32 func_flags
)
8215 const struct btf_type
*func
;
8216 const char *func_name
;
8219 /* any kfunc should be FUNC -> FUNC_PROTO */
8220 func
= btf_type_by_id(btf
, func_id
);
8221 if (!func
|| !btf_type_is_func(func
))
8224 /* sanity check kfunc name */
8225 func_name
= btf_name_by_offset(btf
, func
->name_off
);
8226 if (!func_name
|| !func_name
[0])
8229 func
= btf_type_by_id(btf
, func
->type
);
8230 if (!func
|| !btf_type_is_func_proto(func
))
8233 if (func_flags
& (KF_ITER_NEW
| KF_ITER_NEXT
| KF_ITER_DESTROY
)) {
8234 err
= btf_check_iter_kfuncs(btf
, func_name
, func
, func_flags
);
8242 /* Kernel Function (kfunc) BTF ID set registration API */
8244 static int btf_populate_kfunc_set(struct btf
*btf
, enum btf_kfunc_hook hook
,
8245 const struct btf_kfunc_id_set
*kset
)
8247 struct btf_kfunc_hook_filter
*hook_filter
;
8248 struct btf_id_set8
*add_set
= kset
->set
;
8249 bool vmlinux_set
= !btf_is_module(btf
);
8250 bool add_filter
= !!kset
->filter
;
8251 struct btf_kfunc_set_tab
*tab
;
8252 struct btf_id_set8
*set
;
8256 if (hook
>= BTF_KFUNC_HOOK_MAX
) {
8264 tab
= btf
->kfunc_set_tab
;
8266 if (tab
&& add_filter
) {
8269 hook_filter
= &tab
->hook_filters
[hook
];
8270 for (i
= 0; i
< hook_filter
->nr_filters
; i
++) {
8271 if (hook_filter
->filters
[i
] == kset
->filter
) {
8277 if (add_filter
&& hook_filter
->nr_filters
== BTF_KFUNC_FILTER_MAX_CNT
) {
8284 tab
= kzalloc(sizeof(*tab
), GFP_KERNEL
| __GFP_NOWARN
);
8287 btf
->kfunc_set_tab
= tab
;
8290 set
= tab
->sets
[hook
];
8291 /* Warn when register_btf_kfunc_id_set is called twice for the same hook
8294 if (WARN_ON_ONCE(set
&& !vmlinux_set
)) {
8299 /* In case of vmlinux sets, there may be more than one set being
8300 * registered per hook. To create a unified set, we allocate a new set
8301 * and concatenate all individual sets being registered. While each set
8302 * is individually sorted, they may become unsorted when concatenated,
8303 * hence re-sorting the final set again is required to make binary
8304 * searching the set using btf_id_set8_contains function work.
8306 * For module sets, we need to allocate as we may need to relocate
8309 set_cnt
= set
? set
->cnt
: 0;
8311 if (set_cnt
> U32_MAX
- add_set
->cnt
) {
8316 if (set_cnt
+ add_set
->cnt
> BTF_KFUNC_SET_MAX_CNT
) {
8322 set
= krealloc(tab
->sets
[hook
],
8323 offsetof(struct btf_id_set8
, pairs
[set_cnt
+ add_set
->cnt
]),
8324 GFP_KERNEL
| __GFP_NOWARN
);
8330 /* For newly allocated set, initialize set->cnt to 0 */
8331 if (!tab
->sets
[hook
])
8333 tab
->sets
[hook
] = set
;
8335 /* Concatenate the two sets */
8336 memcpy(set
->pairs
+ set
->cnt
, add_set
->pairs
, add_set
->cnt
* sizeof(set
->pairs
[0]));
8337 /* Now that the set is copied, update with relocated BTF ids */
8338 for (i
= set
->cnt
; i
< set
->cnt
+ add_set
->cnt
; i
++)
8339 set
->pairs
[i
].id
= btf_relocate_id(btf
, set
->pairs
[i
].id
);
8341 set
->cnt
+= add_set
->cnt
;
8343 sort(set
->pairs
, set
->cnt
, sizeof(set
->pairs
[0]), btf_id_cmp_func
, NULL
);
8346 hook_filter
= &tab
->hook_filters
[hook
];
8347 hook_filter
->filters
[hook_filter
->nr_filters
++] = kset
->filter
;
8351 btf_free_kfunc_set_tab(btf
);
8355 static u32
*__btf_kfunc_id_set_contains(const struct btf
*btf
,
8356 enum btf_kfunc_hook hook
,
8358 const struct bpf_prog
*prog
)
8360 struct btf_kfunc_hook_filter
*hook_filter
;
8361 struct btf_id_set8
*set
;
8364 if (hook
>= BTF_KFUNC_HOOK_MAX
)
8366 if (!btf
->kfunc_set_tab
)
8368 hook_filter
= &btf
->kfunc_set_tab
->hook_filters
[hook
];
8369 for (i
= 0; i
< hook_filter
->nr_filters
; i
++) {
8370 if (hook_filter
->filters
[i
](prog
, kfunc_btf_id
))
8373 set
= btf
->kfunc_set_tab
->sets
[hook
];
8376 id
= btf_id_set8_contains(set
, kfunc_btf_id
);
8379 /* The flags for BTF ID are located next to it */
8383 static int bpf_prog_type_to_kfunc_hook(enum bpf_prog_type prog_type
)
8385 switch (prog_type
) {
8386 case BPF_PROG_TYPE_UNSPEC
:
8387 return BTF_KFUNC_HOOK_COMMON
;
8388 case BPF_PROG_TYPE_XDP
:
8389 return BTF_KFUNC_HOOK_XDP
;
8390 case BPF_PROG_TYPE_SCHED_CLS
:
8391 return BTF_KFUNC_HOOK_TC
;
8392 case BPF_PROG_TYPE_STRUCT_OPS
:
8393 return BTF_KFUNC_HOOK_STRUCT_OPS
;
8394 case BPF_PROG_TYPE_TRACING
:
8395 case BPF_PROG_TYPE_TRACEPOINT
:
8396 case BPF_PROG_TYPE_PERF_EVENT
:
8397 case BPF_PROG_TYPE_LSM
:
8398 return BTF_KFUNC_HOOK_TRACING
;
8399 case BPF_PROG_TYPE_SYSCALL
:
8400 return BTF_KFUNC_HOOK_SYSCALL
;
8401 case BPF_PROG_TYPE_CGROUP_SKB
:
8402 case BPF_PROG_TYPE_CGROUP_SOCK
:
8403 case BPF_PROG_TYPE_CGROUP_DEVICE
:
8404 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR
:
8405 case BPF_PROG_TYPE_CGROUP_SOCKOPT
:
8406 case BPF_PROG_TYPE_CGROUP_SYSCTL
:
8407 return BTF_KFUNC_HOOK_CGROUP
;
8408 case BPF_PROG_TYPE_SCHED_ACT
:
8409 return BTF_KFUNC_HOOK_SCHED_ACT
;
8410 case BPF_PROG_TYPE_SK_SKB
:
8411 return BTF_KFUNC_HOOK_SK_SKB
;
8412 case BPF_PROG_TYPE_SOCKET_FILTER
:
8413 return BTF_KFUNC_HOOK_SOCKET_FILTER
;
8414 case BPF_PROG_TYPE_LWT_OUT
:
8415 case BPF_PROG_TYPE_LWT_IN
:
8416 case BPF_PROG_TYPE_LWT_XMIT
:
8417 case BPF_PROG_TYPE_LWT_SEG6LOCAL
:
8418 return BTF_KFUNC_HOOK_LWT
;
8419 case BPF_PROG_TYPE_NETFILTER
:
8420 return BTF_KFUNC_HOOK_NETFILTER
;
8421 case BPF_PROG_TYPE_KPROBE
:
8422 return BTF_KFUNC_HOOK_KPROBE
;
8424 return BTF_KFUNC_HOOK_MAX
;
8429 * Reference to the module (obtained using btf_try_get_module) corresponding to
8430 * the struct btf *MUST* be held when calling this function from verifier
8431 * context. This is usually true as we stash references in prog's kfunc_btf_tab;
8432 * keeping the reference for the duration of the call provides the necessary
8433 * protection for looking up a well-formed btf->kfunc_set_tab.
8435 u32
*btf_kfunc_id_set_contains(const struct btf
*btf
,
8437 const struct bpf_prog
*prog
)
8439 enum bpf_prog_type prog_type
= resolve_prog_type(prog
);
8440 enum btf_kfunc_hook hook
;
8443 kfunc_flags
= __btf_kfunc_id_set_contains(btf
, BTF_KFUNC_HOOK_COMMON
, kfunc_btf_id
, prog
);
8447 hook
= bpf_prog_type_to_kfunc_hook(prog_type
);
8448 return __btf_kfunc_id_set_contains(btf
, hook
, kfunc_btf_id
, prog
);
8451 u32
*btf_kfunc_is_modify_return(const struct btf
*btf
, u32 kfunc_btf_id
,
8452 const struct bpf_prog
*prog
)
8454 return __btf_kfunc_id_set_contains(btf
, BTF_KFUNC_HOOK_FMODRET
, kfunc_btf_id
, prog
);
8457 static int __register_btf_kfunc_id_set(enum btf_kfunc_hook hook
,
8458 const struct btf_kfunc_id_set
*kset
)
8463 btf
= btf_get_module_btf(kset
->owner
);
8465 return check_btf_kconfigs(kset
->owner
, "kfunc");
8467 return PTR_ERR(btf
);
8469 for (i
= 0; i
< kset
->set
->cnt
; i
++) {
8470 ret
= btf_check_kfunc_protos(btf
, btf_relocate_id(btf
, kset
->set
->pairs
[i
].id
),
8471 kset
->set
->pairs
[i
].flags
);
8476 ret
= btf_populate_kfunc_set(btf
, hook
, kset
);
8483 /* This function must be invoked only from initcalls/module init functions */
8484 int register_btf_kfunc_id_set(enum bpf_prog_type prog_type
,
8485 const struct btf_kfunc_id_set
*kset
)
8487 enum btf_kfunc_hook hook
;
8489 /* All kfuncs need to be tagged as such in BTF.
8490 * WARN() for initcall registrations that do not check errors.
8492 if (!(kset
->set
->flags
& BTF_SET8_KFUNCS
)) {
8493 WARN_ON(!kset
->owner
);
8497 hook
= bpf_prog_type_to_kfunc_hook(prog_type
);
8498 return __register_btf_kfunc_id_set(hook
, kset
);
8500 EXPORT_SYMBOL_GPL(register_btf_kfunc_id_set
);
8502 /* This function must be invoked only from initcalls/module init functions */
8503 int register_btf_fmodret_id_set(const struct btf_kfunc_id_set
*kset
)
8505 return __register_btf_kfunc_id_set(BTF_KFUNC_HOOK_FMODRET
, kset
);
8507 EXPORT_SYMBOL_GPL(register_btf_fmodret_id_set
);
8509 s32
btf_find_dtor_kfunc(struct btf
*btf
, u32 btf_id
)
8511 struct btf_id_dtor_kfunc_tab
*tab
= btf
->dtor_kfunc_tab
;
8512 struct btf_id_dtor_kfunc
*dtor
;
8516 /* Even though the size of tab->dtors[0] is > sizeof(u32), we only need
8517 * to compare the first u32 with btf_id, so we can reuse btf_id_cmp_func.
8519 BUILD_BUG_ON(offsetof(struct btf_id_dtor_kfunc
, btf_id
) != 0);
8520 dtor
= bsearch(&btf_id
, tab
->dtors
, tab
->cnt
, sizeof(tab
->dtors
[0]), btf_id_cmp_func
);
8523 return dtor
->kfunc_btf_id
;
8526 static int btf_check_dtor_kfuncs(struct btf
*btf
, const struct btf_id_dtor_kfunc
*dtors
, u32 cnt
)
8528 const struct btf_type
*dtor_func
, *dtor_func_proto
, *t
;
8529 const struct btf_param
*args
;
8533 for (i
= 0; i
< cnt
; i
++) {
8534 dtor_btf_id
= btf_relocate_id(btf
, dtors
[i
].kfunc_btf_id
);
8536 dtor_func
= btf_type_by_id(btf
, dtor_btf_id
);
8537 if (!dtor_func
|| !btf_type_is_func(dtor_func
))
8540 dtor_func_proto
= btf_type_by_id(btf
, dtor_func
->type
);
8541 if (!dtor_func_proto
|| !btf_type_is_func_proto(dtor_func_proto
))
8544 /* Make sure the prototype of the destructor kfunc is 'void func(type *)' */
8545 t
= btf_type_by_id(btf
, dtor_func_proto
->type
);
8546 if (!t
|| !btf_type_is_void(t
))
8549 nr_args
= btf_type_vlen(dtor_func_proto
);
8552 args
= btf_params(dtor_func_proto
);
8553 t
= btf_type_by_id(btf
, args
[0].type
);
8554 /* Allow any pointer type, as width on targets Linux supports
8555 * will be same for all pointer types (i.e. sizeof(void *))
8557 if (!t
|| !btf_type_is_ptr(t
))
8563 /* This function must be invoked only from initcalls/module init functions */
8564 int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc
*dtors
, u32 add_cnt
,
8565 struct module
*owner
)
8567 struct btf_id_dtor_kfunc_tab
*tab
;
8572 btf
= btf_get_module_btf(owner
);
8574 return check_btf_kconfigs(owner
, "dtor kfuncs");
8576 return PTR_ERR(btf
);
8578 if (add_cnt
>= BTF_DTOR_KFUNC_MAX_CNT
) {
8579 pr_err("cannot register more than %d kfunc destructors\n", BTF_DTOR_KFUNC_MAX_CNT
);
8584 /* Ensure that the prototype of dtor kfuncs being registered is sane */
8585 ret
= btf_check_dtor_kfuncs(btf
, dtors
, add_cnt
);
8589 tab
= btf
->dtor_kfunc_tab
;
8590 /* Only one call allowed for modules */
8591 if (WARN_ON_ONCE(tab
&& btf_is_module(btf
))) {
8596 tab_cnt
= tab
? tab
->cnt
: 0;
8597 if (tab_cnt
> U32_MAX
- add_cnt
) {
8601 if (tab_cnt
+ add_cnt
>= BTF_DTOR_KFUNC_MAX_CNT
) {
8602 pr_err("cannot register more than %d kfunc destructors\n", BTF_DTOR_KFUNC_MAX_CNT
);
8607 tab
= krealloc(btf
->dtor_kfunc_tab
,
8608 offsetof(struct btf_id_dtor_kfunc_tab
, dtors
[tab_cnt
+ add_cnt
]),
8609 GFP_KERNEL
| __GFP_NOWARN
);
8615 if (!btf
->dtor_kfunc_tab
)
8617 btf
->dtor_kfunc_tab
= tab
;
8619 memcpy(tab
->dtors
+ tab
->cnt
, dtors
, add_cnt
* sizeof(tab
->dtors
[0]));
8621 /* remap BTF ids based on BTF relocation (if any) */
8622 for (i
= tab_cnt
; i
< tab_cnt
+ add_cnt
; i
++) {
8623 tab
->dtors
[i
].btf_id
= btf_relocate_id(btf
, tab
->dtors
[i
].btf_id
);
8624 tab
->dtors
[i
].kfunc_btf_id
= btf_relocate_id(btf
, tab
->dtors
[i
].kfunc_btf_id
);
8627 tab
->cnt
+= add_cnt
;
8629 sort(tab
->dtors
, tab
->cnt
, sizeof(tab
->dtors
[0]), btf_id_cmp_func
, NULL
);
8633 btf_free_dtor_kfunc_tab(btf
);
8637 EXPORT_SYMBOL_GPL(register_btf_id_dtor_kfuncs
);
8639 #define MAX_TYPES_ARE_COMPAT_DEPTH 2
8641 /* Check local and target types for compatibility. This check is used for
8642 * type-based CO-RE relocations and follow slightly different rules than
8643 * field-based relocations. This function assumes that root types were already
8644 * checked for name match. Beyond that initial root-level name check, names
8645 * are completely ignored. Compatibility rules are as follows:
8646 * - any two STRUCTs/UNIONs/FWDs/ENUMs/INTs/ENUM64s are considered compatible, but
8647 * kind should match for local and target types (i.e., STRUCT is not
8648 * compatible with UNION);
8649 * - for ENUMs/ENUM64s, the size is ignored;
8650 * - for INT, size and signedness are ignored;
8651 * - for ARRAY, dimensionality is ignored, element types are checked for
8652 * compatibility recursively;
8653 * - CONST/VOLATILE/RESTRICT modifiers are ignored;
8654 * - TYPEDEFs/PTRs are compatible if types they pointing to are compatible;
8655 * - FUNC_PROTOs are compatible if they have compatible signature: same
8656 * number of input args and compatible return and argument types.
8657 * These rules are not set in stone and probably will be adjusted as we get
8658 * more experience with using BPF CO-RE relocations.
8660 int bpf_core_types_are_compat(const struct btf
*local_btf
, __u32 local_id
,
8661 const struct btf
*targ_btf
, __u32 targ_id
)
8663 return __bpf_core_types_are_compat(local_btf
, local_id
, targ_btf
, targ_id
,
8664 MAX_TYPES_ARE_COMPAT_DEPTH
);
8667 #define MAX_TYPES_MATCH_DEPTH 2
8669 int bpf_core_types_match(const struct btf
*local_btf
, u32 local_id
,
8670 const struct btf
*targ_btf
, u32 targ_id
)
8672 return __bpf_core_types_match(local_btf
, local_id
, targ_btf
, targ_id
, false,
8673 MAX_TYPES_MATCH_DEPTH
);
8676 static bool bpf_core_is_flavor_sep(const char *s
)
8678 /* check X___Y name pattern, where X and Y are not underscores */
8679 return s
[0] != '_' && /* X */
8680 s
[1] == '_' && s
[2] == '_' && s
[3] == '_' && /* ___ */
8681 s
[4] != '_'; /* Y */
8684 size_t bpf_core_essential_name_len(const char *name
)
8686 size_t n
= strlen(name
);
8689 for (i
= n
- 5; i
>= 0; i
--) {
8690 if (bpf_core_is_flavor_sep(name
+ i
))
8696 static void bpf_free_cands(struct bpf_cand_cache
*cands
)
8699 /* empty candidate array was allocated on stack */
8704 static void bpf_free_cands_from_cache(struct bpf_cand_cache
*cands
)
8710 #define VMLINUX_CAND_CACHE_SIZE 31
8711 static struct bpf_cand_cache
*vmlinux_cand_cache
[VMLINUX_CAND_CACHE_SIZE
];
8713 #define MODULE_CAND_CACHE_SIZE 31
8714 static struct bpf_cand_cache
*module_cand_cache
[MODULE_CAND_CACHE_SIZE
];
8716 static void __print_cand_cache(struct bpf_verifier_log
*log
,
8717 struct bpf_cand_cache
**cache
,
8720 struct bpf_cand_cache
*cc
;
8723 for (i
= 0; i
< cache_size
; i
++) {
8727 bpf_log(log
, "[%d]%s(", i
, cc
->name
);
8728 for (j
= 0; j
< cc
->cnt
; j
++) {
8729 bpf_log(log
, "%d", cc
->cands
[j
].id
);
8730 if (j
< cc
->cnt
- 1)
8733 bpf_log(log
, "), ");
8737 static void print_cand_cache(struct bpf_verifier_log
*log
)
8739 mutex_lock(&cand_cache_mutex
);
8740 bpf_log(log
, "vmlinux_cand_cache:");
8741 __print_cand_cache(log
, vmlinux_cand_cache
, VMLINUX_CAND_CACHE_SIZE
);
8742 bpf_log(log
, "\nmodule_cand_cache:");
8743 __print_cand_cache(log
, module_cand_cache
, MODULE_CAND_CACHE_SIZE
);
8745 mutex_unlock(&cand_cache_mutex
);
8748 static u32
hash_cands(struct bpf_cand_cache
*cands
)
8750 return jhash(cands
->name
, cands
->name_len
, 0);
8753 static struct bpf_cand_cache
*check_cand_cache(struct bpf_cand_cache
*cands
,
8754 struct bpf_cand_cache
**cache
,
8757 struct bpf_cand_cache
*cc
= cache
[hash_cands(cands
) % cache_size
];
8759 if (cc
&& cc
->name_len
== cands
->name_len
&&
8760 !strncmp(cc
->name
, cands
->name
, cands
->name_len
))
8765 static size_t sizeof_cands(int cnt
)
8767 return offsetof(struct bpf_cand_cache
, cands
[cnt
]);
8770 static struct bpf_cand_cache
*populate_cand_cache(struct bpf_cand_cache
*cands
,
8771 struct bpf_cand_cache
**cache
,
8774 struct bpf_cand_cache
**cc
= &cache
[hash_cands(cands
) % cache_size
], *new_cands
;
8777 bpf_free_cands_from_cache(*cc
);
8780 new_cands
= kmemdup(cands
, sizeof_cands(cands
->cnt
), GFP_KERNEL
);
8782 bpf_free_cands(cands
);
8783 return ERR_PTR(-ENOMEM
);
8785 /* strdup the name, since it will stay in cache.
8786 * the cands->name points to strings in prog's BTF and the prog can be unloaded.
8788 new_cands
->name
= kmemdup_nul(cands
->name
, cands
->name_len
, GFP_KERNEL
);
8789 bpf_free_cands(cands
);
8790 if (!new_cands
->name
) {
8792 return ERR_PTR(-ENOMEM
);
8798 #ifdef CONFIG_DEBUG_INFO_BTF_MODULES
8799 static void __purge_cand_cache(struct btf
*btf
, struct bpf_cand_cache
**cache
,
8802 struct bpf_cand_cache
*cc
;
8805 for (i
= 0; i
< cache_size
; i
++) {
8810 /* when new module is loaded purge all of module_cand_cache,
8811 * since new module might have candidates with the name
8812 * that matches cached cands.
8814 bpf_free_cands_from_cache(cc
);
8818 /* when module is unloaded purge cache entries
8819 * that match module's btf
8821 for (j
= 0; j
< cc
->cnt
; j
++)
8822 if (cc
->cands
[j
].btf
== btf
) {
8823 bpf_free_cands_from_cache(cc
);
8831 static void purge_cand_cache(struct btf
*btf
)
8833 mutex_lock(&cand_cache_mutex
);
8834 __purge_cand_cache(btf
, module_cand_cache
, MODULE_CAND_CACHE_SIZE
);
8835 mutex_unlock(&cand_cache_mutex
);
8839 static struct bpf_cand_cache
*
8840 bpf_core_add_cands(struct bpf_cand_cache
*cands
, const struct btf
*targ_btf
,
8843 struct bpf_cand_cache
*new_cands
;
8844 const struct btf_type
*t
;
8845 const char *targ_name
;
8846 size_t targ_essent_len
;
8849 n
= btf_nr_types(targ_btf
);
8850 for (i
= targ_start_id
; i
< n
; i
++) {
8851 t
= btf_type_by_id(targ_btf
, i
);
8852 if (btf_kind(t
) != cands
->kind
)
8855 targ_name
= btf_name_by_offset(targ_btf
, t
->name_off
);
8859 /* the resched point is before strncmp to make sure that search
8860 * for non-existing name will have a chance to schedule().
8864 if (strncmp(cands
->name
, targ_name
, cands
->name_len
) != 0)
8867 targ_essent_len
= bpf_core_essential_name_len(targ_name
);
8868 if (targ_essent_len
!= cands
->name_len
)
8871 /* most of the time there is only one candidate for a given kind+name pair */
8872 new_cands
= kmalloc(sizeof_cands(cands
->cnt
+ 1), GFP_KERNEL
);
8874 bpf_free_cands(cands
);
8875 return ERR_PTR(-ENOMEM
);
8878 memcpy(new_cands
, cands
, sizeof_cands(cands
->cnt
));
8879 bpf_free_cands(cands
);
8881 cands
->cands
[cands
->cnt
].btf
= targ_btf
;
8882 cands
->cands
[cands
->cnt
].id
= i
;
8888 static struct bpf_cand_cache
*
8889 bpf_core_find_cands(struct bpf_core_ctx
*ctx
, u32 local_type_id
)
8891 struct bpf_cand_cache
*cands
, *cc
, local_cand
= {};
8892 const struct btf
*local_btf
= ctx
->btf
;
8893 const struct btf_type
*local_type
;
8894 const struct btf
*main_btf
;
8895 size_t local_essent_len
;
8896 struct btf
*mod_btf
;
8900 main_btf
= bpf_get_btf_vmlinux();
8901 if (IS_ERR(main_btf
))
8902 return ERR_CAST(main_btf
);
8904 return ERR_PTR(-EINVAL
);
8906 local_type
= btf_type_by_id(local_btf
, local_type_id
);
8908 return ERR_PTR(-EINVAL
);
8910 name
= btf_name_by_offset(local_btf
, local_type
->name_off
);
8911 if (str_is_empty(name
))
8912 return ERR_PTR(-EINVAL
);
8913 local_essent_len
= bpf_core_essential_name_len(name
);
8915 cands
= &local_cand
;
8917 cands
->kind
= btf_kind(local_type
);
8918 cands
->name_len
= local_essent_len
;
8920 cc
= check_cand_cache(cands
, vmlinux_cand_cache
, VMLINUX_CAND_CACHE_SIZE
);
8921 /* cands is a pointer to stack here */
8928 /* Attempt to find target candidates in vmlinux BTF first */
8929 cands
= bpf_core_add_cands(cands
, main_btf
, 1);
8931 return ERR_CAST(cands
);
8933 /* cands is a pointer to kmalloced memory here if cands->cnt > 0 */
8935 /* populate cache even when cands->cnt == 0 */
8936 cc
= populate_cand_cache(cands
, vmlinux_cand_cache
, VMLINUX_CAND_CACHE_SIZE
);
8938 return ERR_CAST(cc
);
8940 /* if vmlinux BTF has any candidate, don't go for module BTFs */
8945 /* cands is a pointer to stack here and cands->cnt == 0 */
8946 cc
= check_cand_cache(cands
, module_cand_cache
, MODULE_CAND_CACHE_SIZE
);
8948 /* if cache has it return it even if cc->cnt == 0 */
8951 /* If candidate is not found in vmlinux's BTF then search in module's BTFs */
8952 spin_lock_bh(&btf_idr_lock
);
8953 idr_for_each_entry(&btf_idr
, mod_btf
, id
) {
8954 if (!btf_is_module(mod_btf
))
8956 /* linear search could be slow hence unlock/lock
8957 * the IDR to avoiding holding it for too long
8960 spin_unlock_bh(&btf_idr_lock
);
8961 cands
= bpf_core_add_cands(cands
, mod_btf
, btf_nr_types(main_btf
));
8964 return ERR_CAST(cands
);
8965 spin_lock_bh(&btf_idr_lock
);
8967 spin_unlock_bh(&btf_idr_lock
);
8968 /* cands is a pointer to kmalloced memory here if cands->cnt > 0
8969 * or pointer to stack if cands->cnd == 0.
8970 * Copy it into the cache even when cands->cnt == 0 and
8971 * return the result.
8973 return populate_cand_cache(cands
, module_cand_cache
, MODULE_CAND_CACHE_SIZE
);
8976 int bpf_core_apply(struct bpf_core_ctx
*ctx
, const struct bpf_core_relo
*relo
,
8977 int relo_idx
, void *insn
)
8979 bool need_cands
= relo
->kind
!= BPF_CORE_TYPE_ID_LOCAL
;
8980 struct bpf_core_cand_list cands
= {};
8981 struct bpf_core_relo_res targ_res
;
8982 struct bpf_core_spec
*specs
;
8983 const struct btf_type
*type
;
8986 /* ~4k of temp memory necessary to convert LLVM spec like "0:1:0:5"
8987 * into arrays of btf_ids of struct fields and array indices.
8989 specs
= kcalloc(3, sizeof(*specs
), GFP_KERNEL
);
8993 type
= btf_type_by_id(ctx
->btf
, relo
->type_id
);
8995 bpf_log(ctx
->log
, "relo #%u: bad type id %u\n",
8996 relo_idx
, relo
->type_id
);
9002 struct bpf_cand_cache
*cc
;
9005 mutex_lock(&cand_cache_mutex
);
9006 cc
= bpf_core_find_cands(ctx
, relo
->type_id
);
9008 bpf_log(ctx
->log
, "target candidate search failed for %d\n",
9014 cands
.cands
= kcalloc(cc
->cnt
, sizeof(*cands
.cands
), GFP_KERNEL
);
9020 for (i
= 0; i
< cc
->cnt
; i
++) {
9022 "CO-RE relocating %s %s: found target candidate [%d]\n",
9023 btf_kind_str
[cc
->kind
], cc
->name
, cc
->cands
[i
].id
);
9024 cands
.cands
[i
].btf
= cc
->cands
[i
].btf
;
9025 cands
.cands
[i
].id
= cc
->cands
[i
].id
;
9027 cands
.len
= cc
->cnt
;
9028 /* cand_cache_mutex needs to span the cache lookup and
9029 * copy of btf pointer into bpf_core_cand_list,
9030 * since module can be unloaded while bpf_core_calc_relo_insn
9031 * is working with module's btf.
9035 err
= bpf_core_calc_relo_insn((void *)ctx
->log
, relo
, relo_idx
, ctx
->btf
, &cands
, specs
,
9040 err
= bpf_core_patch_insn((void *)ctx
->log
, insn
, relo
->insn_off
/ 8, relo
, relo_idx
,
9047 mutex_unlock(&cand_cache_mutex
);
9048 if (ctx
->log
->level
& BPF_LOG_LEVEL2
)
9049 print_cand_cache(ctx
->log
);
9054 bool btf_nested_type_is_trusted(struct bpf_verifier_log
*log
,
9055 const struct bpf_reg_state
*reg
,
9056 const char *field_name
, u32 btf_id
, const char *suffix
)
9058 struct btf
*btf
= reg
->btf
;
9059 const struct btf_type
*walk_type
, *safe_type
;
9061 char safe_tname
[64];
9063 const struct btf_member
*member
;
9066 walk_type
= btf_type_by_id(btf
, reg
->btf_id
);
9070 tname
= btf_name_by_offset(btf
, walk_type
->name_off
);
9072 ret
= snprintf(safe_tname
, sizeof(safe_tname
), "%s%s", tname
, suffix
);
9073 if (ret
>= sizeof(safe_tname
))
9076 safe_id
= btf_find_by_name_kind(btf
, safe_tname
, BTF_INFO_KIND(walk_type
->info
));
9080 safe_type
= btf_type_by_id(btf
, safe_id
);
9084 for_each_member(i
, safe_type
, member
) {
9085 const char *m_name
= __btf_name_by_offset(btf
, member
->name_off
);
9086 const struct btf_type
*mtype
= btf_type_by_id(btf
, member
->type
);
9089 if (!btf_type_is_ptr(mtype
))
9092 btf_type_skip_modifiers(btf
, mtype
->type
, &id
);
9093 /* If we match on both type and name, the field is considered trusted. */
9094 if (btf_id
== id
&& !strcmp(field_name
, m_name
))
9101 bool btf_type_ids_nocast_alias(struct bpf_verifier_log
*log
,
9102 const struct btf
*reg_btf
, u32 reg_id
,
9103 const struct btf
*arg_btf
, u32 arg_id
)
9105 const char *reg_name
, *arg_name
, *search_needle
;
9106 const struct btf_type
*reg_type
, *arg_type
;
9107 int reg_len
, arg_len
, cmp_len
;
9108 size_t pattern_len
= sizeof(NOCAST_ALIAS_SUFFIX
) - sizeof(char);
9110 reg_type
= btf_type_by_id(reg_btf
, reg_id
);
9114 arg_type
= btf_type_by_id(arg_btf
, arg_id
);
9118 reg_name
= btf_name_by_offset(reg_btf
, reg_type
->name_off
);
9119 arg_name
= btf_name_by_offset(arg_btf
, arg_type
->name_off
);
9121 reg_len
= strlen(reg_name
);
9122 arg_len
= strlen(arg_name
);
9124 /* Exactly one of the two type names may be suffixed with ___init, so
9125 * if the strings are the same size, they can't possibly be no-cast
9126 * aliases of one another. If you have two of the same type names, e.g.
9127 * they're both nf_conn___init, it would be improper to return true
9128 * because they are _not_ no-cast aliases, they are the same type.
9130 if (reg_len
== arg_len
)
9133 /* Either of the two names must be the other name, suffixed with ___init. */
9134 if ((reg_len
!= arg_len
+ pattern_len
) &&
9135 (arg_len
!= reg_len
+ pattern_len
))
9138 if (reg_len
< arg_len
) {
9139 search_needle
= strstr(arg_name
, NOCAST_ALIAS_SUFFIX
);
9142 search_needle
= strstr(reg_name
, NOCAST_ALIAS_SUFFIX
);
9149 /* ___init suffix must come at the end of the name */
9150 if (*(search_needle
+ pattern_len
) != '\0')
9153 return !strncmp(reg_name
, arg_name
, cmp_len
);
9156 #ifdef CONFIG_BPF_JIT
9158 btf_add_struct_ops(struct btf
*btf
, struct bpf_struct_ops
*st_ops
,
9159 struct bpf_verifier_log
*log
)
9161 struct btf_struct_ops_tab
*tab
, *new_tab
;
9164 tab
= btf
->struct_ops_tab
;
9166 tab
= kzalloc(offsetof(struct btf_struct_ops_tab
, ops
[4]),
9171 btf
->struct_ops_tab
= tab
;
9174 for (i
= 0; i
< tab
->cnt
; i
++)
9175 if (tab
->ops
[i
].st_ops
== st_ops
)
9178 if (tab
->cnt
== tab
->capacity
) {
9179 new_tab
= krealloc(tab
,
9180 offsetof(struct btf_struct_ops_tab
,
9181 ops
[tab
->capacity
* 2]),
9187 btf
->struct_ops_tab
= tab
;
9190 tab
->ops
[btf
->struct_ops_tab
->cnt
].st_ops
= st_ops
;
9192 err
= bpf_struct_ops_desc_init(&tab
->ops
[btf
->struct_ops_tab
->cnt
], btf
, log
);
9196 btf
->struct_ops_tab
->cnt
++;
9201 const struct bpf_struct_ops_desc
*
9202 bpf_struct_ops_find_value(struct btf
*btf
, u32 value_id
)
9204 const struct bpf_struct_ops_desc
*st_ops_list
;
9210 if (!btf
->struct_ops_tab
)
9213 cnt
= btf
->struct_ops_tab
->cnt
;
9214 st_ops_list
= btf
->struct_ops_tab
->ops
;
9215 for (i
= 0; i
< cnt
; i
++) {
9216 if (st_ops_list
[i
].value_id
== value_id
)
9217 return &st_ops_list
[i
];
9223 const struct bpf_struct_ops_desc
*
9224 bpf_struct_ops_find(struct btf
*btf
, u32 type_id
)
9226 const struct bpf_struct_ops_desc
*st_ops_list
;
9232 if (!btf
->struct_ops_tab
)
9235 cnt
= btf
->struct_ops_tab
->cnt
;
9236 st_ops_list
= btf
->struct_ops_tab
->ops
;
9237 for (i
= 0; i
< cnt
; i
++) {
9238 if (st_ops_list
[i
].type_id
== type_id
)
9239 return &st_ops_list
[i
];
9245 int __register_bpf_struct_ops(struct bpf_struct_ops
*st_ops
)
9247 struct bpf_verifier_log
*log
;
9251 btf
= btf_get_module_btf(st_ops
->owner
);
9253 return check_btf_kconfigs(st_ops
->owner
, "struct_ops");
9255 return PTR_ERR(btf
);
9257 log
= kzalloc(sizeof(*log
), GFP_KERNEL
| __GFP_NOWARN
);
9263 log
->level
= BPF_LOG_KERNEL
;
9265 err
= btf_add_struct_ops(btf
, st_ops
, log
);
9273 EXPORT_SYMBOL_GPL(__register_bpf_struct_ops
);
9276 bool btf_param_match_suffix(const struct btf
*btf
,
9277 const struct btf_param
*arg
,
9280 int suffix_len
= strlen(suffix
), len
;
9281 const char *param_name
;
9283 /* In the future, this can be ported to use BTF tagging */
9284 param_name
= btf_name_by_offset(btf
, arg
->name_off
);
9285 if (str_is_empty(param_name
))
9287 len
= strlen(param_name
);
9288 if (len
<= suffix_len
)
9290 param_name
+= len
- suffix_len
;
9291 return !strncmp(param_name
, suffix
, suffix_len
);