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/skmsg.h>
23 #include <linux/perf_event.h>
24 #include <linux/bsearch.h>
25 #include <linux/kobject.h>
26 #include <linux/sysfs.h>
29 /* BTF (BPF Type Format) is the meta data format which describes
30 * the data types of BPF program/map. Hence, it basically focus
31 * on the C programming language which the modern BPF is primary
36 * The BTF data is stored under the ".BTF" ELF section
40 * Each 'struct btf_type' object describes a C data type.
41 * Depending on the type it is describing, a 'struct btf_type'
42 * object may be followed by more data. F.e.
43 * To describe an array, 'struct btf_type' is followed by
46 * 'struct btf_type' and any extra data following it are
51 * The BTF type section contains a list of 'struct btf_type' objects.
52 * Each one describes a C type. Recall from the above section
53 * that a 'struct btf_type' object could be immediately followed by extra
54 * data in order to desribe some particular C types.
58 * Each btf_type object is identified by a type_id. The type_id
59 * is implicitly implied by the location of the btf_type object in
60 * the BTF type section. The first one has type_id 1. The second
61 * one has type_id 2...etc. Hence, an earlier btf_type has
64 * A btf_type object may refer to another btf_type object by using
65 * type_id (i.e. the "type" in the "struct btf_type").
67 * NOTE that we cannot assume any reference-order.
68 * A btf_type object can refer to an earlier btf_type object
69 * but it can also refer to a later btf_type object.
71 * For example, to describe "const void *". A btf_type
72 * object describing "const" may refer to another btf_type
73 * object describing "void *". This type-reference is done
74 * by specifying type_id:
76 * [1] CONST (anon) type_id=2
77 * [2] PTR (anon) type_id=0
79 * The above is the btf_verifier debug log:
80 * - Each line started with "[?]" is a btf_type object
81 * - [?] is the type_id of the btf_type object.
82 * - CONST/PTR is the BTF_KIND_XXX
83 * - "(anon)" is the name of the type. It just
84 * happens that CONST and PTR has no name.
85 * - type_id=XXX is the 'u32 type' in btf_type
87 * NOTE: "void" has type_id 0
91 * The BTF string section contains the names used by the type section.
92 * Each string is referred by an "offset" from the beginning of the
95 * Each string is '\0' terminated.
97 * The first character in the string section must be '\0'
98 * which is used to mean 'anonymous'. Some btf_type may not
104 * To verify BTF data, two passes are needed.
108 * The first pass is to collect all btf_type objects to
109 * an array: "btf->types".
111 * Depending on the C type that a btf_type is describing,
112 * a btf_type may be followed by extra data. We don't know
113 * how many btf_type is there, and more importantly we don't
114 * know where each btf_type is located in the type section.
116 * Without knowing the location of each type_id, most verifications
117 * cannot be done. e.g. an earlier btf_type may refer to a later
118 * btf_type (recall the "const void *" above), so we cannot
119 * check this type-reference in the first pass.
121 * In the first pass, it still does some verifications (e.g.
122 * checking the name is a valid offset to the string section).
126 * The main focus is to resolve a btf_type that is referring
129 * We have to ensure the referring type:
130 * 1) does exist in the BTF (i.e. in btf->types[])
131 * 2) does not cause a loop:
140 * btf_type_needs_resolve() decides if a btf_type needs
143 * The needs_resolve type implements the "resolve()" ops which
144 * essentially does a DFS and detects backedge.
146 * During resolve (or DFS), different C types have different
147 * "RESOLVED" conditions.
149 * When resolving a BTF_KIND_STRUCT, we need to resolve all its
150 * members because a member is always referring to another
151 * type. A struct's member can be treated as "RESOLVED" if
152 * it is referring to a BTF_KIND_PTR. Otherwise, the
153 * following valid C struct would be rejected:
160 * When resolving a BTF_KIND_PTR, it needs to keep resolving if
161 * it is referring to another BTF_KIND_PTR. Otherwise, we cannot
162 * detect a pointer loop, e.g.:
163 * BTF_KIND_CONST -> BTF_KIND_PTR -> BTF_KIND_CONST -> BTF_KIND_PTR +
165 * +-----------------------------------------+
169 #define BITS_PER_U128 (sizeof(u64) * BITS_PER_BYTE * 2)
170 #define BITS_PER_BYTE_MASK (BITS_PER_BYTE - 1)
171 #define BITS_PER_BYTE_MASKED(bits) ((bits) & BITS_PER_BYTE_MASK)
172 #define BITS_ROUNDDOWN_BYTES(bits) ((bits) >> 3)
173 #define BITS_ROUNDUP_BYTES(bits) \
174 (BITS_ROUNDDOWN_BYTES(bits) + !!BITS_PER_BYTE_MASKED(bits))
176 #define BTF_INFO_MASK 0x8f00ffff
177 #define BTF_INT_MASK 0x0fffffff
178 #define BTF_TYPE_ID_VALID(type_id) ((type_id) <= BTF_MAX_TYPE)
179 #define BTF_STR_OFFSET_VALID(name_off) ((name_off) <= BTF_MAX_NAME_OFFSET)
181 /* 16MB for 64k structs and each has 16 members and
182 * a few MB spaces for the string section.
183 * The hard limit is S32_MAX.
185 #define BTF_MAX_SIZE (16 * 1024 * 1024)
187 #define for_each_member_from(i, from, struct_type, member) \
188 for (i = from, member = btf_type_member(struct_type) + from; \
189 i < btf_type_vlen(struct_type); \
192 #define for_each_vsi_from(i, from, struct_type, member) \
193 for (i = from, member = btf_type_var_secinfo(struct_type) + from; \
194 i < btf_type_vlen(struct_type); \
198 DEFINE_SPINLOCK(btf_idr_lock
);
202 struct btf_type
**types
;
207 struct btf_header hdr
;
208 u32 nr_types
; /* includes VOID for base BTF */
215 /* split BTF support */
216 struct btf
*base_btf
;
217 u32 start_id
; /* first type ID in this BTF (0 for base BTF) */
218 u32 start_str_off
; /* first string offset (0 for base BTF) */
219 char name
[MODULE_NAME_LEN
];
223 enum verifier_phase
{
228 struct resolve_vertex
{
229 const struct btf_type
*t
;
241 RESOLVE_TBD
, /* To Be Determined */
242 RESOLVE_PTR
, /* Resolving for Pointer */
243 RESOLVE_STRUCT_OR_ARRAY
, /* Resolving for struct/union
248 #define MAX_RESOLVE_DEPTH 32
250 struct btf_sec_info
{
255 struct btf_verifier_env
{
258 struct resolve_vertex stack
[MAX_RESOLVE_DEPTH
];
259 struct bpf_verifier_log log
;
262 enum verifier_phase phase
;
263 enum resolve_mode resolve_mode
;
266 static const char * const btf_kind_str
[NR_BTF_KINDS
] = {
267 [BTF_KIND_UNKN
] = "UNKNOWN",
268 [BTF_KIND_INT
] = "INT",
269 [BTF_KIND_PTR
] = "PTR",
270 [BTF_KIND_ARRAY
] = "ARRAY",
271 [BTF_KIND_STRUCT
] = "STRUCT",
272 [BTF_KIND_UNION
] = "UNION",
273 [BTF_KIND_ENUM
] = "ENUM",
274 [BTF_KIND_FWD
] = "FWD",
275 [BTF_KIND_TYPEDEF
] = "TYPEDEF",
276 [BTF_KIND_VOLATILE
] = "VOLATILE",
277 [BTF_KIND_CONST
] = "CONST",
278 [BTF_KIND_RESTRICT
] = "RESTRICT",
279 [BTF_KIND_FUNC
] = "FUNC",
280 [BTF_KIND_FUNC_PROTO
] = "FUNC_PROTO",
281 [BTF_KIND_VAR
] = "VAR",
282 [BTF_KIND_DATASEC
] = "DATASEC",
285 static const char *btf_type_str(const struct btf_type
*t
)
287 return btf_kind_str
[BTF_INFO_KIND(t
->info
)];
290 /* Chunk size we use in safe copy of data to be shown. */
291 #define BTF_SHOW_OBJ_SAFE_SIZE 32
294 * This is the maximum size of a base type value (equivalent to a
295 * 128-bit int); if we are at the end of our safe buffer and have
296 * less than 16 bytes space we can't be assured of being able
297 * to copy the next type safely, so in such cases we will initiate
300 #define BTF_SHOW_OBJ_BASE_TYPE_SIZE 16
303 #define BTF_SHOW_NAME_SIZE 80
306 * Common data to all BTF show operations. Private show functions can add
307 * their own data to a structure containing a struct btf_show and consult it
308 * in the show callback. See btf_type_show() below.
310 * One challenge with showing nested data is we want to skip 0-valued
311 * data, but in order to figure out whether a nested object is all zeros
312 * we need to walk through it. As a result, we need to make two passes
313 * when handling structs, unions and arrays; the first path simply looks
314 * for nonzero data, while the second actually does the display. The first
315 * pass is signalled by show->state.depth_check being set, and if we
316 * encounter a non-zero value we set show->state.depth_to_show to
317 * the depth at which we encountered it. When we have completed the
318 * first pass, we will know if anything needs to be displayed if
319 * depth_to_show > depth. See btf_[struct,array]_show() for the
320 * implementation of this.
322 * Another problem is we want to ensure the data for display is safe to
323 * access. To support this, the anonymous "struct {} obj" tracks the data
324 * object and our safe copy of it. We copy portions of the data needed
325 * to the object "copy" buffer, but because its size is limited to
326 * BTF_SHOW_OBJ_COPY_LEN bytes, multiple copies may be required as we
327 * traverse larger objects for display.
329 * The various data type show functions all start with a call to
330 * btf_show_start_type() which returns a pointer to the safe copy
331 * of the data needed (or if BTF_SHOW_UNSAFE is specified, to the
332 * raw data itself). btf_show_obj_safe() is responsible for
333 * using copy_from_kernel_nofault() to update the safe data if necessary
334 * as we traverse the object's data. skbuff-like semantics are
337 * - obj.head points to the start of the toplevel object for display
338 * - obj.size is the size of the toplevel object
339 * - obj.data points to the current point in the original data at
340 * which our safe data starts. obj.data will advance as we copy
341 * portions of the data.
343 * In most cases a single copy will suffice, but larger data structures
344 * such as "struct task_struct" will require many copies. The logic in
345 * btf_show_obj_safe() handles the logic that determines if a new
346 * copy_from_kernel_nofault() is needed.
350 void *target
; /* target of show operation (seq file, buffer) */
351 void (*showfn
)(struct btf_show
*show
, const char *fmt
, va_list args
);
352 const struct btf
*btf
;
353 /* below are used during iteration */
362 int status
; /* non-zero for error */
363 const struct btf_type
*type
;
364 const struct btf_member
*member
;
365 char name
[BTF_SHOW_NAME_SIZE
]; /* space for member name/type */
371 u8 safe
[BTF_SHOW_OBJ_SAFE_SIZE
];
375 struct btf_kind_operations
{
376 s32 (*check_meta
)(struct btf_verifier_env
*env
,
377 const struct btf_type
*t
,
379 int (*resolve
)(struct btf_verifier_env
*env
,
380 const struct resolve_vertex
*v
);
381 int (*check_member
)(struct btf_verifier_env
*env
,
382 const struct btf_type
*struct_type
,
383 const struct btf_member
*member
,
384 const struct btf_type
*member_type
);
385 int (*check_kflag_member
)(struct btf_verifier_env
*env
,
386 const struct btf_type
*struct_type
,
387 const struct btf_member
*member
,
388 const struct btf_type
*member_type
);
389 void (*log_details
)(struct btf_verifier_env
*env
,
390 const struct btf_type
*t
);
391 void (*show
)(const struct btf
*btf
, const struct btf_type
*t
,
392 u32 type_id
, void *data
, u8 bits_offsets
,
393 struct btf_show
*show
);
396 static const struct btf_kind_operations
* const kind_ops
[NR_BTF_KINDS
];
397 static struct btf_type btf_void
;
399 static int btf_resolve(struct btf_verifier_env
*env
,
400 const struct btf_type
*t
, u32 type_id
);
402 static bool btf_type_is_modifier(const struct btf_type
*t
)
404 /* Some of them is not strictly a C modifier
405 * but they are grouped into the same bucket
407 * A type (t) that refers to another
408 * type through t->type AND its size cannot
409 * be determined without following the t->type.
411 * ptr does not fall into this bucket
412 * because its size is always sizeof(void *).
414 switch (BTF_INFO_KIND(t
->info
)) {
415 case BTF_KIND_TYPEDEF
:
416 case BTF_KIND_VOLATILE
:
418 case BTF_KIND_RESTRICT
:
425 bool btf_type_is_void(const struct btf_type
*t
)
427 return t
== &btf_void
;
430 static bool btf_type_is_fwd(const struct btf_type
*t
)
432 return BTF_INFO_KIND(t
->info
) == BTF_KIND_FWD
;
435 static bool btf_type_nosize(const struct btf_type
*t
)
437 return btf_type_is_void(t
) || btf_type_is_fwd(t
) ||
438 btf_type_is_func(t
) || btf_type_is_func_proto(t
);
441 static bool btf_type_nosize_or_null(const struct btf_type
*t
)
443 return !t
|| btf_type_nosize(t
);
446 static bool __btf_type_is_struct(const struct btf_type
*t
)
448 return BTF_INFO_KIND(t
->info
) == BTF_KIND_STRUCT
;
451 static bool btf_type_is_array(const struct btf_type
*t
)
453 return BTF_INFO_KIND(t
->info
) == BTF_KIND_ARRAY
;
456 static bool btf_type_is_datasec(const struct btf_type
*t
)
458 return BTF_INFO_KIND(t
->info
) == BTF_KIND_DATASEC
;
461 static u32
btf_nr_types_total(const struct btf
*btf
)
466 total
+= btf
->nr_types
;
473 s32
btf_find_by_name_kind(const struct btf
*btf
, const char *name
, u8 kind
)
475 const struct btf_type
*t
;
479 total
= btf_nr_types_total(btf
);
480 for (i
= 1; i
< total
; i
++) {
481 t
= btf_type_by_id(btf
, i
);
482 if (BTF_INFO_KIND(t
->info
) != kind
)
485 tname
= btf_name_by_offset(btf
, t
->name_off
);
486 if (!strcmp(tname
, name
))
493 const struct btf_type
*btf_type_skip_modifiers(const struct btf
*btf
,
496 const struct btf_type
*t
= btf_type_by_id(btf
, id
);
498 while (btf_type_is_modifier(t
)) {
500 t
= btf_type_by_id(btf
, t
->type
);
509 const struct btf_type
*btf_type_resolve_ptr(const struct btf
*btf
,
512 const struct btf_type
*t
;
514 t
= btf_type_skip_modifiers(btf
, id
, NULL
);
515 if (!btf_type_is_ptr(t
))
518 return btf_type_skip_modifiers(btf
, t
->type
, res_id
);
521 const struct btf_type
*btf_type_resolve_func_ptr(const struct btf
*btf
,
524 const struct btf_type
*ptype
;
526 ptype
= btf_type_resolve_ptr(btf
, id
, res_id
);
527 if (ptype
&& btf_type_is_func_proto(ptype
))
533 /* Types that act only as a source, not sink or intermediate
534 * type when resolving.
536 static bool btf_type_is_resolve_source_only(const struct btf_type
*t
)
538 return btf_type_is_var(t
) ||
539 btf_type_is_datasec(t
);
542 /* What types need to be resolved?
544 * btf_type_is_modifier() is an obvious one.
546 * btf_type_is_struct() because its member refers to
547 * another type (through member->type).
549 * btf_type_is_var() because the variable refers to
550 * another type. btf_type_is_datasec() holds multiple
551 * btf_type_is_var() types that need resolving.
553 * btf_type_is_array() because its element (array->type)
554 * refers to another type. Array can be thought of a
555 * special case of struct while array just has the same
556 * member-type repeated by array->nelems of times.
558 static bool btf_type_needs_resolve(const struct btf_type
*t
)
560 return btf_type_is_modifier(t
) ||
561 btf_type_is_ptr(t
) ||
562 btf_type_is_struct(t
) ||
563 btf_type_is_array(t
) ||
564 btf_type_is_var(t
) ||
565 btf_type_is_datasec(t
);
568 /* t->size can be used */
569 static bool btf_type_has_size(const struct btf_type
*t
)
571 switch (BTF_INFO_KIND(t
->info
)) {
573 case BTF_KIND_STRUCT
:
576 case BTF_KIND_DATASEC
:
583 static const char *btf_int_encoding_str(u8 encoding
)
587 else if (encoding
== BTF_INT_SIGNED
)
589 else if (encoding
== BTF_INT_CHAR
)
591 else if (encoding
== BTF_INT_BOOL
)
597 static u32
btf_type_int(const struct btf_type
*t
)
599 return *(u32
*)(t
+ 1);
602 static const struct btf_array
*btf_type_array(const struct btf_type
*t
)
604 return (const struct btf_array
*)(t
+ 1);
607 static const struct btf_enum
*btf_type_enum(const struct btf_type
*t
)
609 return (const struct btf_enum
*)(t
+ 1);
612 static const struct btf_var
*btf_type_var(const struct btf_type
*t
)
614 return (const struct btf_var
*)(t
+ 1);
617 static const struct btf_kind_operations
*btf_type_ops(const struct btf_type
*t
)
619 return kind_ops
[BTF_INFO_KIND(t
->info
)];
622 static bool btf_name_offset_valid(const struct btf
*btf
, u32 offset
)
624 if (!BTF_STR_OFFSET_VALID(offset
))
627 while (offset
< btf
->start_str_off
)
630 offset
-= btf
->start_str_off
;
631 return offset
< btf
->hdr
.str_len
;
634 static bool __btf_name_char_ok(char c
, bool first
, bool dot_ok
)
636 if ((first
? !isalpha(c
) :
639 ((c
== '.' && !dot_ok
) ||
645 static const char *btf_str_by_offset(const struct btf
*btf
, u32 offset
)
647 while (offset
< btf
->start_str_off
)
650 offset
-= btf
->start_str_off
;
651 if (offset
< btf
->hdr
.str_len
)
652 return &btf
->strings
[offset
];
657 static bool __btf_name_valid(const struct btf
*btf
, u32 offset
, bool dot_ok
)
659 /* offset must be valid */
660 const char *src
= btf_str_by_offset(btf
, offset
);
661 const char *src_limit
;
663 if (!__btf_name_char_ok(*src
, true, dot_ok
))
666 /* set a limit on identifier length */
667 src_limit
= src
+ KSYM_NAME_LEN
;
669 while (*src
&& src
< src_limit
) {
670 if (!__btf_name_char_ok(*src
, false, dot_ok
))
678 /* Only C-style identifier is permitted. This can be relaxed if
681 static bool btf_name_valid_identifier(const struct btf
*btf
, u32 offset
)
683 return __btf_name_valid(btf
, offset
, false);
686 static bool btf_name_valid_section(const struct btf
*btf
, u32 offset
)
688 return __btf_name_valid(btf
, offset
, true);
691 static const char *__btf_name_by_offset(const struct btf
*btf
, u32 offset
)
698 name
= btf_str_by_offset(btf
, offset
);
699 return name
?: "(invalid-name-offset)";
702 const char *btf_name_by_offset(const struct btf
*btf
, u32 offset
)
704 return btf_str_by_offset(btf
, offset
);
707 const struct btf_type
*btf_type_by_id(const struct btf
*btf
, u32 type_id
)
709 while (type_id
< btf
->start_id
)
712 type_id
-= btf
->start_id
;
713 if (type_id
>= btf
->nr_types
)
715 return btf
->types
[type_id
];
719 * Regular int is not a bit field and it must be either
720 * u8/u16/u32/u64 or __int128.
722 static bool btf_type_int_is_regular(const struct btf_type
*t
)
724 u8 nr_bits
, nr_bytes
;
727 int_data
= btf_type_int(t
);
728 nr_bits
= BTF_INT_BITS(int_data
);
729 nr_bytes
= BITS_ROUNDUP_BYTES(nr_bits
);
730 if (BITS_PER_BYTE_MASKED(nr_bits
) ||
731 BTF_INT_OFFSET(int_data
) ||
732 (nr_bytes
!= sizeof(u8
) && nr_bytes
!= sizeof(u16
) &&
733 nr_bytes
!= sizeof(u32
) && nr_bytes
!= sizeof(u64
) &&
734 nr_bytes
!= (2 * sizeof(u64
)))) {
742 * Check that given struct member is a regular int with expected
745 bool btf_member_is_reg_int(const struct btf
*btf
, const struct btf_type
*s
,
746 const struct btf_member
*m
,
747 u32 expected_offset
, u32 expected_size
)
749 const struct btf_type
*t
;
754 t
= btf_type_id_size(btf
, &id
, NULL
);
755 if (!t
|| !btf_type_is_int(t
))
758 int_data
= btf_type_int(t
);
759 nr_bits
= BTF_INT_BITS(int_data
);
760 if (btf_type_kflag(s
)) {
761 u32 bitfield_size
= BTF_MEMBER_BITFIELD_SIZE(m
->offset
);
762 u32 bit_offset
= BTF_MEMBER_BIT_OFFSET(m
->offset
);
764 /* if kflag set, int should be a regular int and
765 * bit offset should be at byte boundary.
767 return !bitfield_size
&&
768 BITS_ROUNDUP_BYTES(bit_offset
) == expected_offset
&&
769 BITS_ROUNDUP_BYTES(nr_bits
) == expected_size
;
772 if (BTF_INT_OFFSET(int_data
) ||
773 BITS_PER_BYTE_MASKED(m
->offset
) ||
774 BITS_ROUNDUP_BYTES(m
->offset
) != expected_offset
||
775 BITS_PER_BYTE_MASKED(nr_bits
) ||
776 BITS_ROUNDUP_BYTES(nr_bits
) != expected_size
)
782 /* Similar to btf_type_skip_modifiers() but does not skip typedefs. */
783 static const struct btf_type
*btf_type_skip_qualifiers(const struct btf
*btf
,
786 const struct btf_type
*t
= btf_type_by_id(btf
, id
);
788 while (btf_type_is_modifier(t
) &&
789 BTF_INFO_KIND(t
->info
) != BTF_KIND_TYPEDEF
) {
791 t
= btf_type_by_id(btf
, t
->type
);
797 #define BTF_SHOW_MAX_ITER 10
799 #define BTF_KIND_BIT(kind) (1ULL << kind)
802 * Populate show->state.name with type name information.
803 * Format of type name is
805 * [.member_name = ] (type_name)
807 static const char *btf_show_name(struct btf_show
*show
)
809 /* BTF_MAX_ITER array suffixes "[]" */
810 const char *array_suffixes
= "[][][][][][][][][][]";
811 const char *array_suffix
= &array_suffixes
[strlen(array_suffixes
)];
812 /* BTF_MAX_ITER pointer suffixes "*" */
813 const char *ptr_suffixes
= "**********";
814 const char *ptr_suffix
= &ptr_suffixes
[strlen(ptr_suffixes
)];
815 const char *name
= NULL
, *prefix
= "", *parens
= "";
816 const struct btf_member
*m
= show
->state
.member
;
817 const struct btf_type
*t
= show
->state
.type
;
818 const struct btf_array
*array
;
819 u32 id
= show
->state
.type_id
;
820 const char *member
= NULL
;
821 bool show_member
= false;
825 show
->state
.name
[0] = '\0';
828 * Don't show type name if we're showing an array member;
829 * in that case we show the array type so don't need to repeat
830 * ourselves for each member.
832 if (show
->state
.array_member
)
835 /* Retrieve member name, if any. */
837 member
= btf_name_by_offset(show
->btf
, m
->name_off
);
838 show_member
= strlen(member
) > 0;
843 * Start with type_id, as we have resolved the struct btf_type *
844 * via btf_modifier_show() past the parent typedef to the child
845 * struct, int etc it is defined as. In such cases, the type_id
846 * still represents the starting type while the struct btf_type *
847 * in our show->state points at the resolved type of the typedef.
849 t
= btf_type_by_id(show
->btf
, id
);
854 * The goal here is to build up the right number of pointer and
855 * array suffixes while ensuring the type name for a typedef
856 * is represented. Along the way we accumulate a list of
857 * BTF kinds we have encountered, since these will inform later
858 * display; for example, pointer types will not require an
859 * opening "{" for struct, we will just display the pointer value.
861 * We also want to accumulate the right number of pointer or array
862 * indices in the format string while iterating until we get to
863 * the typedef/pointee/array member target type.
865 * We start by pointing at the end of pointer and array suffix
866 * strings; as we accumulate pointers and arrays we move the pointer
867 * or array string backwards so it will show the expected number of
868 * '*' or '[]' for the type. BTF_SHOW_MAX_ITER of nesting of pointers
869 * and/or arrays and typedefs are supported as a precaution.
871 * We also want to get typedef name while proceeding to resolve
872 * type it points to so that we can add parentheses if it is a
873 * "typedef struct" etc.
875 for (i
= 0; i
< BTF_SHOW_MAX_ITER
; i
++) {
877 switch (BTF_INFO_KIND(t
->info
)) {
878 case BTF_KIND_TYPEDEF
:
880 name
= btf_name_by_offset(show
->btf
,
882 kinds
|= BTF_KIND_BIT(BTF_KIND_TYPEDEF
);
886 kinds
|= BTF_KIND_BIT(BTF_KIND_ARRAY
);
890 array
= btf_type_array(t
);
891 if (array_suffix
> array_suffixes
)
896 kinds
|= BTF_KIND_BIT(BTF_KIND_PTR
);
897 if (ptr_suffix
> ptr_suffixes
)
907 t
= btf_type_skip_qualifiers(show
->btf
, id
);
909 /* We may not be able to represent this type; bail to be safe */
910 if (i
== BTF_SHOW_MAX_ITER
)
914 name
= btf_name_by_offset(show
->btf
, t
->name_off
);
916 switch (BTF_INFO_KIND(t
->info
)) {
917 case BTF_KIND_STRUCT
:
919 prefix
= BTF_INFO_KIND(t
->info
) == BTF_KIND_STRUCT
?
921 /* if it's an array of struct/union, parens is already set */
922 if (!(kinds
& (BTF_KIND_BIT(BTF_KIND_ARRAY
))))
932 /* pointer does not require parens */
933 if (kinds
& BTF_KIND_BIT(BTF_KIND_PTR
))
935 /* typedef does not require struct/union/enum prefix */
936 if (kinds
& BTF_KIND_BIT(BTF_KIND_TYPEDEF
))
942 /* Even if we don't want type name info, we want parentheses etc */
943 if (show
->flags
& BTF_SHOW_NONAME
)
944 snprintf(show
->state
.name
, sizeof(show
->state
.name
), "%s",
947 snprintf(show
->state
.name
, sizeof(show
->state
.name
),
948 "%s%s%s(%s%s%s%s%s%s)%s",
949 /* first 3 strings comprise ".member = " */
950 show_member
? "." : "",
951 show_member
? member
: "",
952 show_member
? " = " : "",
953 /* ...next is our prefix (struct, enum, etc) */
955 strlen(prefix
) > 0 && strlen(name
) > 0 ? " " : "",
956 /* ...this is the type name itself */
958 /* ...suffixed by the appropriate '*', '[]' suffixes */
959 strlen(ptr_suffix
) > 0 ? " " : "", ptr_suffix
,
960 array_suffix
, parens
);
962 return show
->state
.name
;
965 static const char *__btf_show_indent(struct btf_show
*show
)
967 const char *indents
= " ";
968 const char *indent
= &indents
[strlen(indents
)];
970 if ((indent
- show
->state
.depth
) >= indents
)
971 return indent
- show
->state
.depth
;
975 static const char *btf_show_indent(struct btf_show
*show
)
977 return show
->flags
& BTF_SHOW_COMPACT
? "" : __btf_show_indent(show
);
980 static const char *btf_show_newline(struct btf_show
*show
)
982 return show
->flags
& BTF_SHOW_COMPACT
? "" : "\n";
985 static const char *btf_show_delim(struct btf_show
*show
)
987 if (show
->state
.depth
== 0)
990 if ((show
->flags
& BTF_SHOW_COMPACT
) && show
->state
.type
&&
991 BTF_INFO_KIND(show
->state
.type
->info
) == BTF_KIND_UNION
)
997 __printf(2, 3) static void btf_show(struct btf_show
*show
, const char *fmt
, ...)
1001 if (!show
->state
.depth_check
) {
1002 va_start(args
, fmt
);
1003 show
->showfn(show
, fmt
, args
);
1008 /* Macros are used here as btf_show_type_value[s]() prepends and appends
1009 * format specifiers to the format specifier passed in; these do the work of
1010 * adding indentation, delimiters etc while the caller simply has to specify
1011 * the type value(s) in the format specifier + value(s).
1013 #define btf_show_type_value(show, fmt, value) \
1015 if ((value) != 0 || (show->flags & BTF_SHOW_ZERO) || \
1016 show->state.depth == 0) { \
1017 btf_show(show, "%s%s" fmt "%s%s", \
1018 btf_show_indent(show), \
1019 btf_show_name(show), \
1020 value, btf_show_delim(show), \
1021 btf_show_newline(show)); \
1022 if (show->state.depth > show->state.depth_to_show) \
1023 show->state.depth_to_show = show->state.depth; \
1027 #define btf_show_type_values(show, fmt, ...) \
1029 btf_show(show, "%s%s" fmt "%s%s", btf_show_indent(show), \
1030 btf_show_name(show), \
1031 __VA_ARGS__, btf_show_delim(show), \
1032 btf_show_newline(show)); \
1033 if (show->state.depth > show->state.depth_to_show) \
1034 show->state.depth_to_show = show->state.depth; \
1037 /* How much is left to copy to safe buffer after @data? */
1038 static int btf_show_obj_size_left(struct btf_show
*show
, void *data
)
1040 return show
->obj
.head
+ show
->obj
.size
- data
;
1043 /* Is object pointed to by @data of @size already copied to our safe buffer? */
1044 static bool btf_show_obj_is_safe(struct btf_show
*show
, void *data
, int size
)
1046 return data
>= show
->obj
.data
&&
1047 (data
+ size
) < (show
->obj
.data
+ BTF_SHOW_OBJ_SAFE_SIZE
);
1051 * If object pointed to by @data of @size falls within our safe buffer, return
1052 * the equivalent pointer to the same safe data. Assumes
1053 * copy_from_kernel_nofault() has already happened and our safe buffer is
1056 static void *__btf_show_obj_safe(struct btf_show
*show
, void *data
, int size
)
1058 if (btf_show_obj_is_safe(show
, data
, size
))
1059 return show
->obj
.safe
+ (data
- show
->obj
.data
);
1064 * Return a safe-to-access version of data pointed to by @data.
1065 * We do this by copying the relevant amount of information
1066 * to the struct btf_show obj.safe buffer using copy_from_kernel_nofault().
1068 * If BTF_SHOW_UNSAFE is specified, just return data as-is; no
1069 * safe copy is needed.
1071 * Otherwise we need to determine if we have the required amount
1072 * of data (determined by the @data pointer and the size of the
1073 * largest base type we can encounter (represented by
1074 * BTF_SHOW_OBJ_BASE_TYPE_SIZE). Having that much data ensures
1075 * that we will be able to print some of the current object,
1076 * and if more is needed a copy will be triggered.
1077 * Some objects such as structs will not fit into the buffer;
1078 * in such cases additional copies when we iterate over their
1079 * members may be needed.
1081 * btf_show_obj_safe() is used to return a safe buffer for
1082 * btf_show_start_type(); this ensures that as we recurse into
1083 * nested types we always have safe data for the given type.
1084 * This approach is somewhat wasteful; it's possible for example
1085 * that when iterating over a large union we'll end up copying the
1086 * same data repeatedly, but the goal is safety not performance.
1087 * We use stack data as opposed to per-CPU buffers because the
1088 * iteration over a type can take some time, and preemption handling
1089 * would greatly complicate use of the safe buffer.
1091 static void *btf_show_obj_safe(struct btf_show
*show
,
1092 const struct btf_type
*t
,
1095 const struct btf_type
*rt
;
1096 int size_left
, size
;
1099 if (show
->flags
& BTF_SHOW_UNSAFE
)
1102 rt
= btf_resolve_size(show
->btf
, t
, &size
);
1104 show
->state
.status
= PTR_ERR(rt
);
1109 * Is this toplevel object? If so, set total object size and
1110 * initialize pointers. Otherwise check if we still fall within
1111 * our safe object data.
1113 if (show
->state
.depth
== 0) {
1114 show
->obj
.size
= size
;
1115 show
->obj
.head
= data
;
1118 * If the size of the current object is > our remaining
1119 * safe buffer we _may_ need to do a new copy. However
1120 * consider the case of a nested struct; it's size pushes
1121 * us over the safe buffer limit, but showing any individual
1122 * struct members does not. In such cases, we don't need
1123 * to initiate a fresh copy yet; however we definitely need
1124 * at least BTF_SHOW_OBJ_BASE_TYPE_SIZE bytes left
1125 * in our buffer, regardless of the current object size.
1126 * The logic here is that as we resolve types we will
1127 * hit a base type at some point, and we need to be sure
1128 * the next chunk of data is safely available to display
1129 * that type info safely. We cannot rely on the size of
1130 * the current object here because it may be much larger
1131 * than our current buffer (e.g. task_struct is 8k).
1132 * All we want to do here is ensure that we can print the
1133 * next basic type, which we can if either
1134 * - the current type size is within the safe buffer; or
1135 * - at least BTF_SHOW_OBJ_BASE_TYPE_SIZE bytes are left in
1138 safe
= __btf_show_obj_safe(show
, data
,
1140 BTF_SHOW_OBJ_BASE_TYPE_SIZE
));
1144 * We need a new copy to our safe object, either because we haven't
1145 * yet copied and are intializing safe data, or because the data
1146 * we want falls outside the boundaries of the safe object.
1149 size_left
= btf_show_obj_size_left(show
, data
);
1150 if (size_left
> BTF_SHOW_OBJ_SAFE_SIZE
)
1151 size_left
= BTF_SHOW_OBJ_SAFE_SIZE
;
1152 show
->state
.status
= copy_from_kernel_nofault(show
->obj
.safe
,
1154 if (!show
->state
.status
) {
1155 show
->obj
.data
= data
;
1156 safe
= show
->obj
.safe
;
1164 * Set the type we are starting to show and return a safe data pointer
1165 * to be used for showing the associated data.
1167 static void *btf_show_start_type(struct btf_show
*show
,
1168 const struct btf_type
*t
,
1169 u32 type_id
, void *data
)
1171 show
->state
.type
= t
;
1172 show
->state
.type_id
= type_id
;
1173 show
->state
.name
[0] = '\0';
1175 return btf_show_obj_safe(show
, t
, data
);
1178 static void btf_show_end_type(struct btf_show
*show
)
1180 show
->state
.type
= NULL
;
1181 show
->state
.type_id
= 0;
1182 show
->state
.name
[0] = '\0';
1185 static void *btf_show_start_aggr_type(struct btf_show
*show
,
1186 const struct btf_type
*t
,
1187 u32 type_id
, void *data
)
1189 void *safe_data
= btf_show_start_type(show
, t
, type_id
, data
);
1194 btf_show(show
, "%s%s%s", btf_show_indent(show
),
1195 btf_show_name(show
),
1196 btf_show_newline(show
));
1197 show
->state
.depth
++;
1201 static void btf_show_end_aggr_type(struct btf_show
*show
,
1204 show
->state
.depth
--;
1205 btf_show(show
, "%s%s%s%s", btf_show_indent(show
), suffix
,
1206 btf_show_delim(show
), btf_show_newline(show
));
1207 btf_show_end_type(show
);
1210 static void btf_show_start_member(struct btf_show
*show
,
1211 const struct btf_member
*m
)
1213 show
->state
.member
= m
;
1216 static void btf_show_start_array_member(struct btf_show
*show
)
1218 show
->state
.array_member
= 1;
1219 btf_show_start_member(show
, NULL
);
1222 static void btf_show_end_member(struct btf_show
*show
)
1224 show
->state
.member
= NULL
;
1227 static void btf_show_end_array_member(struct btf_show
*show
)
1229 show
->state
.array_member
= 0;
1230 btf_show_end_member(show
);
1233 static void *btf_show_start_array_type(struct btf_show
*show
,
1234 const struct btf_type
*t
,
1239 show
->state
.array_encoding
= array_encoding
;
1240 show
->state
.array_terminated
= 0;
1241 return btf_show_start_aggr_type(show
, t
, type_id
, data
);
1244 static void btf_show_end_array_type(struct btf_show
*show
)
1246 show
->state
.array_encoding
= 0;
1247 show
->state
.array_terminated
= 0;
1248 btf_show_end_aggr_type(show
, "]");
1251 static void *btf_show_start_struct_type(struct btf_show
*show
,
1252 const struct btf_type
*t
,
1256 return btf_show_start_aggr_type(show
, t
, type_id
, data
);
1259 static void btf_show_end_struct_type(struct btf_show
*show
)
1261 btf_show_end_aggr_type(show
, "}");
1264 __printf(2, 3) static void __btf_verifier_log(struct bpf_verifier_log
*log
,
1265 const char *fmt
, ...)
1269 va_start(args
, fmt
);
1270 bpf_verifier_vlog(log
, fmt
, args
);
1274 __printf(2, 3) static void btf_verifier_log(struct btf_verifier_env
*env
,
1275 const char *fmt
, ...)
1277 struct bpf_verifier_log
*log
= &env
->log
;
1280 if (!bpf_verifier_log_needed(log
))
1283 va_start(args
, fmt
);
1284 bpf_verifier_vlog(log
, fmt
, args
);
1288 __printf(4, 5) static void __btf_verifier_log_type(struct btf_verifier_env
*env
,
1289 const struct btf_type
*t
,
1291 const char *fmt
, ...)
1293 struct bpf_verifier_log
*log
= &env
->log
;
1294 u8 kind
= BTF_INFO_KIND(t
->info
);
1295 struct btf
*btf
= env
->btf
;
1298 if (!bpf_verifier_log_needed(log
))
1301 /* btf verifier prints all types it is processing via
1302 * btf_verifier_log_type(..., fmt = NULL).
1303 * Skip those prints for in-kernel BTF verification.
1305 if (log
->level
== BPF_LOG_KERNEL
&& !fmt
)
1308 __btf_verifier_log(log
, "[%u] %s %s%s",
1311 __btf_name_by_offset(btf
, t
->name_off
),
1312 log_details
? " " : "");
1315 btf_type_ops(t
)->log_details(env
, t
);
1318 __btf_verifier_log(log
, " ");
1319 va_start(args
, fmt
);
1320 bpf_verifier_vlog(log
, fmt
, args
);
1324 __btf_verifier_log(log
, "\n");
1327 #define btf_verifier_log_type(env, t, ...) \
1328 __btf_verifier_log_type((env), (t), true, __VA_ARGS__)
1329 #define btf_verifier_log_basic(env, t, ...) \
1330 __btf_verifier_log_type((env), (t), false, __VA_ARGS__)
1333 static void btf_verifier_log_member(struct btf_verifier_env
*env
,
1334 const struct btf_type
*struct_type
,
1335 const struct btf_member
*member
,
1336 const char *fmt
, ...)
1338 struct bpf_verifier_log
*log
= &env
->log
;
1339 struct btf
*btf
= env
->btf
;
1342 if (!bpf_verifier_log_needed(log
))
1345 if (log
->level
== BPF_LOG_KERNEL
&& !fmt
)
1347 /* The CHECK_META phase already did a btf dump.
1349 * If member is logged again, it must hit an error in
1350 * parsing this member. It is useful to print out which
1351 * struct this member belongs to.
1353 if (env
->phase
!= CHECK_META
)
1354 btf_verifier_log_type(env
, struct_type
, NULL
);
1356 if (btf_type_kflag(struct_type
))
1357 __btf_verifier_log(log
,
1358 "\t%s type_id=%u bitfield_size=%u bits_offset=%u",
1359 __btf_name_by_offset(btf
, member
->name_off
),
1361 BTF_MEMBER_BITFIELD_SIZE(member
->offset
),
1362 BTF_MEMBER_BIT_OFFSET(member
->offset
));
1364 __btf_verifier_log(log
, "\t%s type_id=%u bits_offset=%u",
1365 __btf_name_by_offset(btf
, member
->name_off
),
1366 member
->type
, member
->offset
);
1369 __btf_verifier_log(log
, " ");
1370 va_start(args
, fmt
);
1371 bpf_verifier_vlog(log
, fmt
, args
);
1375 __btf_verifier_log(log
, "\n");
1379 static void btf_verifier_log_vsi(struct btf_verifier_env
*env
,
1380 const struct btf_type
*datasec_type
,
1381 const struct btf_var_secinfo
*vsi
,
1382 const char *fmt
, ...)
1384 struct bpf_verifier_log
*log
= &env
->log
;
1387 if (!bpf_verifier_log_needed(log
))
1389 if (log
->level
== BPF_LOG_KERNEL
&& !fmt
)
1391 if (env
->phase
!= CHECK_META
)
1392 btf_verifier_log_type(env
, datasec_type
, NULL
);
1394 __btf_verifier_log(log
, "\t type_id=%u offset=%u size=%u",
1395 vsi
->type
, vsi
->offset
, vsi
->size
);
1397 __btf_verifier_log(log
, " ");
1398 va_start(args
, fmt
);
1399 bpf_verifier_vlog(log
, fmt
, args
);
1403 __btf_verifier_log(log
, "\n");
1406 static void btf_verifier_log_hdr(struct btf_verifier_env
*env
,
1409 struct bpf_verifier_log
*log
= &env
->log
;
1410 const struct btf
*btf
= env
->btf
;
1411 const struct btf_header
*hdr
;
1413 if (!bpf_verifier_log_needed(log
))
1416 if (log
->level
== BPF_LOG_KERNEL
)
1419 __btf_verifier_log(log
, "magic: 0x%x\n", hdr
->magic
);
1420 __btf_verifier_log(log
, "version: %u\n", hdr
->version
);
1421 __btf_verifier_log(log
, "flags: 0x%x\n", hdr
->flags
);
1422 __btf_verifier_log(log
, "hdr_len: %u\n", hdr
->hdr_len
);
1423 __btf_verifier_log(log
, "type_off: %u\n", hdr
->type_off
);
1424 __btf_verifier_log(log
, "type_len: %u\n", hdr
->type_len
);
1425 __btf_verifier_log(log
, "str_off: %u\n", hdr
->str_off
);
1426 __btf_verifier_log(log
, "str_len: %u\n", hdr
->str_len
);
1427 __btf_verifier_log(log
, "btf_total_size: %u\n", btf_data_size
);
1430 static int btf_add_type(struct btf_verifier_env
*env
, struct btf_type
*t
)
1432 struct btf
*btf
= env
->btf
;
1434 if (btf
->types_size
== btf
->nr_types
) {
1435 /* Expand 'types' array */
1437 struct btf_type
**new_types
;
1438 u32 expand_by
, new_size
;
1440 if (btf
->start_id
+ btf
->types_size
== BTF_MAX_TYPE
) {
1441 btf_verifier_log(env
, "Exceeded max num of types");
1445 expand_by
= max_t(u32
, btf
->types_size
>> 2, 16);
1446 new_size
= min_t(u32
, BTF_MAX_TYPE
,
1447 btf
->types_size
+ expand_by
);
1449 new_types
= kvcalloc(new_size
, sizeof(*new_types
),
1450 GFP_KERNEL
| __GFP_NOWARN
);
1454 if (btf
->nr_types
== 0) {
1455 if (!btf
->base_btf
) {
1456 /* lazily init VOID type */
1457 new_types
[0] = &btf_void
;
1461 memcpy(new_types
, btf
->types
,
1462 sizeof(*btf
->types
) * btf
->nr_types
);
1466 btf
->types
= new_types
;
1467 btf
->types_size
= new_size
;
1470 btf
->types
[btf
->nr_types
++] = t
;
1475 static int btf_alloc_id(struct btf
*btf
)
1479 idr_preload(GFP_KERNEL
);
1480 spin_lock_bh(&btf_idr_lock
);
1481 id
= idr_alloc_cyclic(&btf_idr
, btf
, 1, INT_MAX
, GFP_ATOMIC
);
1484 spin_unlock_bh(&btf_idr_lock
);
1487 if (WARN_ON_ONCE(!id
))
1490 return id
> 0 ? 0 : id
;
1493 static void btf_free_id(struct btf
*btf
)
1495 unsigned long flags
;
1498 * In map-in-map, calling map_delete_elem() on outer
1499 * map will call bpf_map_put on the inner map.
1500 * It will then eventually call btf_free_id()
1501 * on the inner map. Some of the map_delete_elem()
1502 * implementation may have irq disabled, so
1503 * we need to use the _irqsave() version instead
1504 * of the _bh() version.
1506 spin_lock_irqsave(&btf_idr_lock
, flags
);
1507 idr_remove(&btf_idr
, btf
->id
);
1508 spin_unlock_irqrestore(&btf_idr_lock
, flags
);
1511 static void btf_free(struct btf
*btf
)
1514 kvfree(btf
->resolved_sizes
);
1515 kvfree(btf
->resolved_ids
);
1520 static void btf_free_rcu(struct rcu_head
*rcu
)
1522 struct btf
*btf
= container_of(rcu
, struct btf
, rcu
);
1527 void btf_get(struct btf
*btf
)
1529 refcount_inc(&btf
->refcnt
);
1532 void btf_put(struct btf
*btf
)
1534 if (btf
&& refcount_dec_and_test(&btf
->refcnt
)) {
1536 call_rcu(&btf
->rcu
, btf_free_rcu
);
1540 static int env_resolve_init(struct btf_verifier_env
*env
)
1542 struct btf
*btf
= env
->btf
;
1543 u32 nr_types
= btf
->nr_types
;
1544 u32
*resolved_sizes
= NULL
;
1545 u32
*resolved_ids
= NULL
;
1546 u8
*visit_states
= NULL
;
1548 resolved_sizes
= kvcalloc(nr_types
, sizeof(*resolved_sizes
),
1549 GFP_KERNEL
| __GFP_NOWARN
);
1550 if (!resolved_sizes
)
1553 resolved_ids
= kvcalloc(nr_types
, sizeof(*resolved_ids
),
1554 GFP_KERNEL
| __GFP_NOWARN
);
1558 visit_states
= kvcalloc(nr_types
, sizeof(*visit_states
),
1559 GFP_KERNEL
| __GFP_NOWARN
);
1563 btf
->resolved_sizes
= resolved_sizes
;
1564 btf
->resolved_ids
= resolved_ids
;
1565 env
->visit_states
= visit_states
;
1570 kvfree(resolved_sizes
);
1571 kvfree(resolved_ids
);
1572 kvfree(visit_states
);
1576 static void btf_verifier_env_free(struct btf_verifier_env
*env
)
1578 kvfree(env
->visit_states
);
1582 static bool env_type_is_resolve_sink(const struct btf_verifier_env
*env
,
1583 const struct btf_type
*next_type
)
1585 switch (env
->resolve_mode
) {
1587 /* int, enum or void is a sink */
1588 return !btf_type_needs_resolve(next_type
);
1590 /* int, enum, void, struct, array, func or func_proto is a sink
1593 return !btf_type_is_modifier(next_type
) &&
1594 !btf_type_is_ptr(next_type
);
1595 case RESOLVE_STRUCT_OR_ARRAY
:
1596 /* int, enum, void, ptr, func or func_proto is a sink
1597 * for struct and array
1599 return !btf_type_is_modifier(next_type
) &&
1600 !btf_type_is_array(next_type
) &&
1601 !btf_type_is_struct(next_type
);
1607 static bool env_type_is_resolved(const struct btf_verifier_env
*env
,
1610 /* base BTF types should be resolved by now */
1611 if (type_id
< env
->btf
->start_id
)
1614 return env
->visit_states
[type_id
- env
->btf
->start_id
] == RESOLVED
;
1617 static int env_stack_push(struct btf_verifier_env
*env
,
1618 const struct btf_type
*t
, u32 type_id
)
1620 const struct btf
*btf
= env
->btf
;
1621 struct resolve_vertex
*v
;
1623 if (env
->top_stack
== MAX_RESOLVE_DEPTH
)
1626 if (type_id
< btf
->start_id
1627 || env
->visit_states
[type_id
- btf
->start_id
] != NOT_VISITED
)
1630 env
->visit_states
[type_id
- btf
->start_id
] = VISITED
;
1632 v
= &env
->stack
[env
->top_stack
++];
1634 v
->type_id
= type_id
;
1637 if (env
->resolve_mode
== RESOLVE_TBD
) {
1638 if (btf_type_is_ptr(t
))
1639 env
->resolve_mode
= RESOLVE_PTR
;
1640 else if (btf_type_is_struct(t
) || btf_type_is_array(t
))
1641 env
->resolve_mode
= RESOLVE_STRUCT_OR_ARRAY
;
1647 static void env_stack_set_next_member(struct btf_verifier_env
*env
,
1650 env
->stack
[env
->top_stack
- 1].next_member
= next_member
;
1653 static void env_stack_pop_resolved(struct btf_verifier_env
*env
,
1654 u32 resolved_type_id
,
1657 u32 type_id
= env
->stack
[--(env
->top_stack
)].type_id
;
1658 struct btf
*btf
= env
->btf
;
1660 type_id
-= btf
->start_id
; /* adjust to local type id */
1661 btf
->resolved_sizes
[type_id
] = resolved_size
;
1662 btf
->resolved_ids
[type_id
] = resolved_type_id
;
1663 env
->visit_states
[type_id
] = RESOLVED
;
1666 static const struct resolve_vertex
*env_stack_peak(struct btf_verifier_env
*env
)
1668 return env
->top_stack
? &env
->stack
[env
->top_stack
- 1] : NULL
;
1671 /* Resolve the size of a passed-in "type"
1673 * type: is an array (e.g. u32 array[x][y])
1674 * return type: type "u32[x][y]", i.e. BTF_KIND_ARRAY,
1675 * *type_size: (x * y * sizeof(u32)). Hence, *type_size always
1676 * corresponds to the return type.
1678 * *elem_id: id of u32
1679 * *total_nelems: (x * y). Hence, individual elem size is
1680 * (*type_size / *total_nelems)
1681 * *type_id: id of type if it's changed within the function, 0 if not
1683 * type: is not an array (e.g. const struct X)
1684 * return type: type "struct X"
1685 * *type_size: sizeof(struct X)
1686 * *elem_type: same as return type ("struct X")
1689 * *type_id: id of type if it's changed within the function, 0 if not
1691 static const struct btf_type
*
1692 __btf_resolve_size(const struct btf
*btf
, const struct btf_type
*type
,
1693 u32
*type_size
, const struct btf_type
**elem_type
,
1694 u32
*elem_id
, u32
*total_nelems
, u32
*type_id
)
1696 const struct btf_type
*array_type
= NULL
;
1697 const struct btf_array
*array
= NULL
;
1698 u32 i
, size
, nelems
= 1, id
= 0;
1700 for (i
= 0; i
< MAX_RESOLVE_DEPTH
; i
++) {
1701 switch (BTF_INFO_KIND(type
->info
)) {
1702 /* type->size can be used */
1704 case BTF_KIND_STRUCT
:
1705 case BTF_KIND_UNION
:
1711 size
= sizeof(void *);
1715 case BTF_KIND_TYPEDEF
:
1716 case BTF_KIND_VOLATILE
:
1717 case BTF_KIND_CONST
:
1718 case BTF_KIND_RESTRICT
:
1720 type
= btf_type_by_id(btf
, type
->type
);
1723 case BTF_KIND_ARRAY
:
1726 array
= btf_type_array(type
);
1727 if (nelems
&& array
->nelems
> U32_MAX
/ nelems
)
1728 return ERR_PTR(-EINVAL
);
1729 nelems
*= array
->nelems
;
1730 type
= btf_type_by_id(btf
, array
->type
);
1733 /* type without size */
1735 return ERR_PTR(-EINVAL
);
1739 return ERR_PTR(-EINVAL
);
1742 if (nelems
&& size
> U32_MAX
/ nelems
)
1743 return ERR_PTR(-EINVAL
);
1745 *type_size
= nelems
* size
;
1747 *total_nelems
= nelems
;
1751 *elem_id
= array
? array
->type
: 0;
1755 return array_type
? : type
;
1758 const struct btf_type
*
1759 btf_resolve_size(const struct btf
*btf
, const struct btf_type
*type
,
1762 return __btf_resolve_size(btf
, type
, type_size
, NULL
, NULL
, NULL
, NULL
);
1765 static u32
btf_resolved_type_id(const struct btf
*btf
, u32 type_id
)
1767 while (type_id
< btf
->start_id
)
1768 btf
= btf
->base_btf
;
1770 return btf
->resolved_ids
[type_id
- btf
->start_id
];
1773 /* The input param "type_id" must point to a needs_resolve type */
1774 static const struct btf_type
*btf_type_id_resolve(const struct btf
*btf
,
1777 *type_id
= btf_resolved_type_id(btf
, *type_id
);
1778 return btf_type_by_id(btf
, *type_id
);
1781 static u32
btf_resolved_type_size(const struct btf
*btf
, u32 type_id
)
1783 while (type_id
< btf
->start_id
)
1784 btf
= btf
->base_btf
;
1786 return btf
->resolved_sizes
[type_id
- btf
->start_id
];
1789 const struct btf_type
*btf_type_id_size(const struct btf
*btf
,
1790 u32
*type_id
, u32
*ret_size
)
1792 const struct btf_type
*size_type
;
1793 u32 size_type_id
= *type_id
;
1796 size_type
= btf_type_by_id(btf
, size_type_id
);
1797 if (btf_type_nosize_or_null(size_type
))
1800 if (btf_type_has_size(size_type
)) {
1801 size
= size_type
->size
;
1802 } else if (btf_type_is_array(size_type
)) {
1803 size
= btf_resolved_type_size(btf
, size_type_id
);
1804 } else if (btf_type_is_ptr(size_type
)) {
1805 size
= sizeof(void *);
1807 if (WARN_ON_ONCE(!btf_type_is_modifier(size_type
) &&
1808 !btf_type_is_var(size_type
)))
1811 size_type_id
= btf_resolved_type_id(btf
, size_type_id
);
1812 size_type
= btf_type_by_id(btf
, size_type_id
);
1813 if (btf_type_nosize_or_null(size_type
))
1815 else if (btf_type_has_size(size_type
))
1816 size
= size_type
->size
;
1817 else if (btf_type_is_array(size_type
))
1818 size
= btf_resolved_type_size(btf
, size_type_id
);
1819 else if (btf_type_is_ptr(size_type
))
1820 size
= sizeof(void *);
1825 *type_id
= size_type_id
;
1832 static int btf_df_check_member(struct btf_verifier_env
*env
,
1833 const struct btf_type
*struct_type
,
1834 const struct btf_member
*member
,
1835 const struct btf_type
*member_type
)
1837 btf_verifier_log_basic(env
, struct_type
,
1838 "Unsupported check_member");
1842 static int btf_df_check_kflag_member(struct btf_verifier_env
*env
,
1843 const struct btf_type
*struct_type
,
1844 const struct btf_member
*member
,
1845 const struct btf_type
*member_type
)
1847 btf_verifier_log_basic(env
, struct_type
,
1848 "Unsupported check_kflag_member");
1852 /* Used for ptr, array and struct/union type members.
1853 * int, enum and modifier types have their specific callback functions.
1855 static int btf_generic_check_kflag_member(struct btf_verifier_env
*env
,
1856 const struct btf_type
*struct_type
,
1857 const struct btf_member
*member
,
1858 const struct btf_type
*member_type
)
1860 if (BTF_MEMBER_BITFIELD_SIZE(member
->offset
)) {
1861 btf_verifier_log_member(env
, struct_type
, member
,
1862 "Invalid member bitfield_size");
1866 /* bitfield size is 0, so member->offset represents bit offset only.
1867 * It is safe to call non kflag check_member variants.
1869 return btf_type_ops(member_type
)->check_member(env
, struct_type
,
1874 static int btf_df_resolve(struct btf_verifier_env
*env
,
1875 const struct resolve_vertex
*v
)
1877 btf_verifier_log_basic(env
, v
->t
, "Unsupported resolve");
1881 static void btf_df_show(const struct btf
*btf
, const struct btf_type
*t
,
1882 u32 type_id
, void *data
, u8 bits_offsets
,
1883 struct btf_show
*show
)
1885 btf_show(show
, "<unsupported kind:%u>", BTF_INFO_KIND(t
->info
));
1888 static int btf_int_check_member(struct btf_verifier_env
*env
,
1889 const struct btf_type
*struct_type
,
1890 const struct btf_member
*member
,
1891 const struct btf_type
*member_type
)
1893 u32 int_data
= btf_type_int(member_type
);
1894 u32 struct_bits_off
= member
->offset
;
1895 u32 struct_size
= struct_type
->size
;
1899 if (U32_MAX
- struct_bits_off
< BTF_INT_OFFSET(int_data
)) {
1900 btf_verifier_log_member(env
, struct_type
, member
,
1901 "bits_offset exceeds U32_MAX");
1905 struct_bits_off
+= BTF_INT_OFFSET(int_data
);
1906 bytes_offset
= BITS_ROUNDDOWN_BYTES(struct_bits_off
);
1907 nr_copy_bits
= BTF_INT_BITS(int_data
) +
1908 BITS_PER_BYTE_MASKED(struct_bits_off
);
1910 if (nr_copy_bits
> BITS_PER_U128
) {
1911 btf_verifier_log_member(env
, struct_type
, member
,
1912 "nr_copy_bits exceeds 128");
1916 if (struct_size
< bytes_offset
||
1917 struct_size
- bytes_offset
< BITS_ROUNDUP_BYTES(nr_copy_bits
)) {
1918 btf_verifier_log_member(env
, struct_type
, member
,
1919 "Member exceeds struct_size");
1926 static int btf_int_check_kflag_member(struct btf_verifier_env
*env
,
1927 const struct btf_type
*struct_type
,
1928 const struct btf_member
*member
,
1929 const struct btf_type
*member_type
)
1931 u32 struct_bits_off
, nr_bits
, nr_int_data_bits
, bytes_offset
;
1932 u32 int_data
= btf_type_int(member_type
);
1933 u32 struct_size
= struct_type
->size
;
1936 /* a regular int type is required for the kflag int member */
1937 if (!btf_type_int_is_regular(member_type
)) {
1938 btf_verifier_log_member(env
, struct_type
, member
,
1939 "Invalid member base type");
1943 /* check sanity of bitfield size */
1944 nr_bits
= BTF_MEMBER_BITFIELD_SIZE(member
->offset
);
1945 struct_bits_off
= BTF_MEMBER_BIT_OFFSET(member
->offset
);
1946 nr_int_data_bits
= BTF_INT_BITS(int_data
);
1948 /* Not a bitfield member, member offset must be at byte
1951 if (BITS_PER_BYTE_MASKED(struct_bits_off
)) {
1952 btf_verifier_log_member(env
, struct_type
, member
,
1953 "Invalid member offset");
1957 nr_bits
= nr_int_data_bits
;
1958 } else if (nr_bits
> nr_int_data_bits
) {
1959 btf_verifier_log_member(env
, struct_type
, member
,
1960 "Invalid member bitfield_size");
1964 bytes_offset
= BITS_ROUNDDOWN_BYTES(struct_bits_off
);
1965 nr_copy_bits
= nr_bits
+ BITS_PER_BYTE_MASKED(struct_bits_off
);
1966 if (nr_copy_bits
> BITS_PER_U128
) {
1967 btf_verifier_log_member(env
, struct_type
, member
,
1968 "nr_copy_bits exceeds 128");
1972 if (struct_size
< bytes_offset
||
1973 struct_size
- bytes_offset
< BITS_ROUNDUP_BYTES(nr_copy_bits
)) {
1974 btf_verifier_log_member(env
, struct_type
, member
,
1975 "Member exceeds struct_size");
1982 static s32
btf_int_check_meta(struct btf_verifier_env
*env
,
1983 const struct btf_type
*t
,
1986 u32 int_data
, nr_bits
, meta_needed
= sizeof(int_data
);
1989 if (meta_left
< meta_needed
) {
1990 btf_verifier_log_basic(env
, t
,
1991 "meta_left:%u meta_needed:%u",
1992 meta_left
, meta_needed
);
1996 if (btf_type_vlen(t
)) {
1997 btf_verifier_log_type(env
, t
, "vlen != 0");
2001 if (btf_type_kflag(t
)) {
2002 btf_verifier_log_type(env
, t
, "Invalid btf_info kind_flag");
2006 int_data
= btf_type_int(t
);
2007 if (int_data
& ~BTF_INT_MASK
) {
2008 btf_verifier_log_basic(env
, t
, "Invalid int_data:%x",
2013 nr_bits
= BTF_INT_BITS(int_data
) + BTF_INT_OFFSET(int_data
);
2015 if (nr_bits
> BITS_PER_U128
) {
2016 btf_verifier_log_type(env
, t
, "nr_bits exceeds %zu",
2021 if (BITS_ROUNDUP_BYTES(nr_bits
) > t
->size
) {
2022 btf_verifier_log_type(env
, t
, "nr_bits exceeds type_size");
2027 * Only one of the encoding bits is allowed and it
2028 * should be sufficient for the pretty print purpose (i.e. decoding).
2029 * Multiple bits can be allowed later if it is found
2030 * to be insufficient.
2032 encoding
= BTF_INT_ENCODING(int_data
);
2034 encoding
!= BTF_INT_SIGNED
&&
2035 encoding
!= BTF_INT_CHAR
&&
2036 encoding
!= BTF_INT_BOOL
) {
2037 btf_verifier_log_type(env
, t
, "Unsupported encoding");
2041 btf_verifier_log_type(env
, t
, NULL
);
2046 static void btf_int_log(struct btf_verifier_env
*env
,
2047 const struct btf_type
*t
)
2049 int int_data
= btf_type_int(t
);
2051 btf_verifier_log(env
,
2052 "size=%u bits_offset=%u nr_bits=%u encoding=%s",
2053 t
->size
, BTF_INT_OFFSET(int_data
),
2054 BTF_INT_BITS(int_data
),
2055 btf_int_encoding_str(BTF_INT_ENCODING(int_data
)));
2058 static void btf_int128_print(struct btf_show
*show
, void *data
)
2060 /* data points to a __int128 number.
2062 * int128_num = *(__int128 *)data;
2063 * The below formulas shows what upper_num and lower_num represents:
2064 * upper_num = int128_num >> 64;
2065 * lower_num = int128_num & 0xffffffffFFFFFFFFULL;
2067 u64 upper_num
, lower_num
;
2069 #ifdef __BIG_ENDIAN_BITFIELD
2070 upper_num
= *(u64
*)data
;
2071 lower_num
= *(u64
*)(data
+ 8);
2073 upper_num
= *(u64
*)(data
+ 8);
2074 lower_num
= *(u64
*)data
;
2077 btf_show_type_value(show
, "0x%llx", lower_num
);
2079 btf_show_type_values(show
, "0x%llx%016llx", upper_num
,
2083 static void btf_int128_shift(u64
*print_num
, u16 left_shift_bits
,
2084 u16 right_shift_bits
)
2086 u64 upper_num
, lower_num
;
2088 #ifdef __BIG_ENDIAN_BITFIELD
2089 upper_num
= print_num
[0];
2090 lower_num
= print_num
[1];
2092 upper_num
= print_num
[1];
2093 lower_num
= print_num
[0];
2096 /* shake out un-needed bits by shift/or operations */
2097 if (left_shift_bits
>= 64) {
2098 upper_num
= lower_num
<< (left_shift_bits
- 64);
2101 upper_num
= (upper_num
<< left_shift_bits
) |
2102 (lower_num
>> (64 - left_shift_bits
));
2103 lower_num
= lower_num
<< left_shift_bits
;
2106 if (right_shift_bits
>= 64) {
2107 lower_num
= upper_num
>> (right_shift_bits
- 64);
2110 lower_num
= (lower_num
>> right_shift_bits
) |
2111 (upper_num
<< (64 - right_shift_bits
));
2112 upper_num
= upper_num
>> right_shift_bits
;
2115 #ifdef __BIG_ENDIAN_BITFIELD
2116 print_num
[0] = upper_num
;
2117 print_num
[1] = lower_num
;
2119 print_num
[0] = lower_num
;
2120 print_num
[1] = upper_num
;
2124 static void btf_bitfield_show(void *data
, u8 bits_offset
,
2125 u8 nr_bits
, struct btf_show
*show
)
2127 u16 left_shift_bits
, right_shift_bits
;
2130 u64 print_num
[2] = {};
2132 nr_copy_bits
= nr_bits
+ bits_offset
;
2133 nr_copy_bytes
= BITS_ROUNDUP_BYTES(nr_copy_bits
);
2135 memcpy(print_num
, data
, nr_copy_bytes
);
2137 #ifdef __BIG_ENDIAN_BITFIELD
2138 left_shift_bits
= bits_offset
;
2140 left_shift_bits
= BITS_PER_U128
- nr_copy_bits
;
2142 right_shift_bits
= BITS_PER_U128
- nr_bits
;
2144 btf_int128_shift(print_num
, left_shift_bits
, right_shift_bits
);
2145 btf_int128_print(show
, print_num
);
2149 static void btf_int_bits_show(const struct btf
*btf
,
2150 const struct btf_type
*t
,
2151 void *data
, u8 bits_offset
,
2152 struct btf_show
*show
)
2154 u32 int_data
= btf_type_int(t
);
2155 u8 nr_bits
= BTF_INT_BITS(int_data
);
2156 u8 total_bits_offset
;
2159 * bits_offset is at most 7.
2160 * BTF_INT_OFFSET() cannot exceed 128 bits.
2162 total_bits_offset
= bits_offset
+ BTF_INT_OFFSET(int_data
);
2163 data
+= BITS_ROUNDDOWN_BYTES(total_bits_offset
);
2164 bits_offset
= BITS_PER_BYTE_MASKED(total_bits_offset
);
2165 btf_bitfield_show(data
, bits_offset
, nr_bits
, show
);
2168 static void btf_int_show(const struct btf
*btf
, const struct btf_type
*t
,
2169 u32 type_id
, void *data
, u8 bits_offset
,
2170 struct btf_show
*show
)
2172 u32 int_data
= btf_type_int(t
);
2173 u8 encoding
= BTF_INT_ENCODING(int_data
);
2174 bool sign
= encoding
& BTF_INT_SIGNED
;
2175 u8 nr_bits
= BTF_INT_BITS(int_data
);
2178 safe_data
= btf_show_start_type(show
, t
, type_id
, data
);
2182 if (bits_offset
|| BTF_INT_OFFSET(int_data
) ||
2183 BITS_PER_BYTE_MASKED(nr_bits
)) {
2184 btf_int_bits_show(btf
, t
, safe_data
, bits_offset
, show
);
2190 btf_int128_print(show
, safe_data
);
2194 btf_show_type_value(show
, "%lld", *(s64
*)safe_data
);
2196 btf_show_type_value(show
, "%llu", *(u64
*)safe_data
);
2200 btf_show_type_value(show
, "%d", *(s32
*)safe_data
);
2202 btf_show_type_value(show
, "%u", *(u32
*)safe_data
);
2206 btf_show_type_value(show
, "%d", *(s16
*)safe_data
);
2208 btf_show_type_value(show
, "%u", *(u16
*)safe_data
);
2211 if (show
->state
.array_encoding
== BTF_INT_CHAR
) {
2212 /* check for null terminator */
2213 if (show
->state
.array_terminated
)
2215 if (*(char *)data
== '\0') {
2216 show
->state
.array_terminated
= 1;
2219 if (isprint(*(char *)data
)) {
2220 btf_show_type_value(show
, "'%c'",
2221 *(char *)safe_data
);
2226 btf_show_type_value(show
, "%d", *(s8
*)safe_data
);
2228 btf_show_type_value(show
, "%u", *(u8
*)safe_data
);
2231 btf_int_bits_show(btf
, t
, safe_data
, bits_offset
, show
);
2235 btf_show_end_type(show
);
2238 static const struct btf_kind_operations int_ops
= {
2239 .check_meta
= btf_int_check_meta
,
2240 .resolve
= btf_df_resolve
,
2241 .check_member
= btf_int_check_member
,
2242 .check_kflag_member
= btf_int_check_kflag_member
,
2243 .log_details
= btf_int_log
,
2244 .show
= btf_int_show
,
2247 static int btf_modifier_check_member(struct btf_verifier_env
*env
,
2248 const struct btf_type
*struct_type
,
2249 const struct btf_member
*member
,
2250 const struct btf_type
*member_type
)
2252 const struct btf_type
*resolved_type
;
2253 u32 resolved_type_id
= member
->type
;
2254 struct btf_member resolved_member
;
2255 struct btf
*btf
= env
->btf
;
2257 resolved_type
= btf_type_id_size(btf
, &resolved_type_id
, NULL
);
2258 if (!resolved_type
) {
2259 btf_verifier_log_member(env
, struct_type
, member
,
2264 resolved_member
= *member
;
2265 resolved_member
.type
= resolved_type_id
;
2267 return btf_type_ops(resolved_type
)->check_member(env
, struct_type
,
2272 static int btf_modifier_check_kflag_member(struct btf_verifier_env
*env
,
2273 const struct btf_type
*struct_type
,
2274 const struct btf_member
*member
,
2275 const struct btf_type
*member_type
)
2277 const struct btf_type
*resolved_type
;
2278 u32 resolved_type_id
= member
->type
;
2279 struct btf_member resolved_member
;
2280 struct btf
*btf
= env
->btf
;
2282 resolved_type
= btf_type_id_size(btf
, &resolved_type_id
, NULL
);
2283 if (!resolved_type
) {
2284 btf_verifier_log_member(env
, struct_type
, member
,
2289 resolved_member
= *member
;
2290 resolved_member
.type
= resolved_type_id
;
2292 return btf_type_ops(resolved_type
)->check_kflag_member(env
, struct_type
,
2297 static int btf_ptr_check_member(struct btf_verifier_env
*env
,
2298 const struct btf_type
*struct_type
,
2299 const struct btf_member
*member
,
2300 const struct btf_type
*member_type
)
2302 u32 struct_size
, struct_bits_off
, bytes_offset
;
2304 struct_size
= struct_type
->size
;
2305 struct_bits_off
= member
->offset
;
2306 bytes_offset
= BITS_ROUNDDOWN_BYTES(struct_bits_off
);
2308 if (BITS_PER_BYTE_MASKED(struct_bits_off
)) {
2309 btf_verifier_log_member(env
, struct_type
, member
,
2310 "Member is not byte aligned");
2314 if (struct_size
- bytes_offset
< sizeof(void *)) {
2315 btf_verifier_log_member(env
, struct_type
, member
,
2316 "Member exceeds struct_size");
2323 static int btf_ref_type_check_meta(struct btf_verifier_env
*env
,
2324 const struct btf_type
*t
,
2327 if (btf_type_vlen(t
)) {
2328 btf_verifier_log_type(env
, t
, "vlen != 0");
2332 if (btf_type_kflag(t
)) {
2333 btf_verifier_log_type(env
, t
, "Invalid btf_info kind_flag");
2337 if (!BTF_TYPE_ID_VALID(t
->type
)) {
2338 btf_verifier_log_type(env
, t
, "Invalid type_id");
2342 /* typedef type must have a valid name, and other ref types,
2343 * volatile, const, restrict, should have a null name.
2345 if (BTF_INFO_KIND(t
->info
) == BTF_KIND_TYPEDEF
) {
2347 !btf_name_valid_identifier(env
->btf
, t
->name_off
)) {
2348 btf_verifier_log_type(env
, t
, "Invalid name");
2353 btf_verifier_log_type(env
, t
, "Invalid name");
2358 btf_verifier_log_type(env
, t
, NULL
);
2363 static int btf_modifier_resolve(struct btf_verifier_env
*env
,
2364 const struct resolve_vertex
*v
)
2366 const struct btf_type
*t
= v
->t
;
2367 const struct btf_type
*next_type
;
2368 u32 next_type_id
= t
->type
;
2369 struct btf
*btf
= env
->btf
;
2371 next_type
= btf_type_by_id(btf
, next_type_id
);
2372 if (!next_type
|| btf_type_is_resolve_source_only(next_type
)) {
2373 btf_verifier_log_type(env
, v
->t
, "Invalid type_id");
2377 if (!env_type_is_resolve_sink(env
, next_type
) &&
2378 !env_type_is_resolved(env
, next_type_id
))
2379 return env_stack_push(env
, next_type
, next_type_id
);
2381 /* Figure out the resolved next_type_id with size.
2382 * They will be stored in the current modifier's
2383 * resolved_ids and resolved_sizes such that it can
2384 * save us a few type-following when we use it later (e.g. in
2387 if (!btf_type_id_size(btf
, &next_type_id
, NULL
)) {
2388 if (env_type_is_resolved(env
, next_type_id
))
2389 next_type
= btf_type_id_resolve(btf
, &next_type_id
);
2391 /* "typedef void new_void", "const void"...etc */
2392 if (!btf_type_is_void(next_type
) &&
2393 !btf_type_is_fwd(next_type
) &&
2394 !btf_type_is_func_proto(next_type
)) {
2395 btf_verifier_log_type(env
, v
->t
, "Invalid type_id");
2400 env_stack_pop_resolved(env
, next_type_id
, 0);
2405 static int btf_var_resolve(struct btf_verifier_env
*env
,
2406 const struct resolve_vertex
*v
)
2408 const struct btf_type
*next_type
;
2409 const struct btf_type
*t
= v
->t
;
2410 u32 next_type_id
= t
->type
;
2411 struct btf
*btf
= env
->btf
;
2413 next_type
= btf_type_by_id(btf
, next_type_id
);
2414 if (!next_type
|| btf_type_is_resolve_source_only(next_type
)) {
2415 btf_verifier_log_type(env
, v
->t
, "Invalid type_id");
2419 if (!env_type_is_resolve_sink(env
, next_type
) &&
2420 !env_type_is_resolved(env
, next_type_id
))
2421 return env_stack_push(env
, next_type
, next_type_id
);
2423 if (btf_type_is_modifier(next_type
)) {
2424 const struct btf_type
*resolved_type
;
2425 u32 resolved_type_id
;
2427 resolved_type_id
= next_type_id
;
2428 resolved_type
= btf_type_id_resolve(btf
, &resolved_type_id
);
2430 if (btf_type_is_ptr(resolved_type
) &&
2431 !env_type_is_resolve_sink(env
, resolved_type
) &&
2432 !env_type_is_resolved(env
, resolved_type_id
))
2433 return env_stack_push(env
, resolved_type
,
2437 /* We must resolve to something concrete at this point, no
2438 * forward types or similar that would resolve to size of
2441 if (!btf_type_id_size(btf
, &next_type_id
, NULL
)) {
2442 btf_verifier_log_type(env
, v
->t
, "Invalid type_id");
2446 env_stack_pop_resolved(env
, next_type_id
, 0);
2451 static int btf_ptr_resolve(struct btf_verifier_env
*env
,
2452 const struct resolve_vertex
*v
)
2454 const struct btf_type
*next_type
;
2455 const struct btf_type
*t
= v
->t
;
2456 u32 next_type_id
= t
->type
;
2457 struct btf
*btf
= env
->btf
;
2459 next_type
= btf_type_by_id(btf
, next_type_id
);
2460 if (!next_type
|| btf_type_is_resolve_source_only(next_type
)) {
2461 btf_verifier_log_type(env
, v
->t
, "Invalid type_id");
2465 if (!env_type_is_resolve_sink(env
, next_type
) &&
2466 !env_type_is_resolved(env
, next_type_id
))
2467 return env_stack_push(env
, next_type
, next_type_id
);
2469 /* If the modifier was RESOLVED during RESOLVE_STRUCT_OR_ARRAY,
2470 * the modifier may have stopped resolving when it was resolved
2471 * to a ptr (last-resolved-ptr).
2473 * We now need to continue from the last-resolved-ptr to
2474 * ensure the last-resolved-ptr will not referring back to
2475 * the currenct ptr (t).
2477 if (btf_type_is_modifier(next_type
)) {
2478 const struct btf_type
*resolved_type
;
2479 u32 resolved_type_id
;
2481 resolved_type_id
= next_type_id
;
2482 resolved_type
= btf_type_id_resolve(btf
, &resolved_type_id
);
2484 if (btf_type_is_ptr(resolved_type
) &&
2485 !env_type_is_resolve_sink(env
, resolved_type
) &&
2486 !env_type_is_resolved(env
, resolved_type_id
))
2487 return env_stack_push(env
, resolved_type
,
2491 if (!btf_type_id_size(btf
, &next_type_id
, NULL
)) {
2492 if (env_type_is_resolved(env
, next_type_id
))
2493 next_type
= btf_type_id_resolve(btf
, &next_type_id
);
2495 if (!btf_type_is_void(next_type
) &&
2496 !btf_type_is_fwd(next_type
) &&
2497 !btf_type_is_func_proto(next_type
)) {
2498 btf_verifier_log_type(env
, v
->t
, "Invalid type_id");
2503 env_stack_pop_resolved(env
, next_type_id
, 0);
2508 static void btf_modifier_show(const struct btf
*btf
,
2509 const struct btf_type
*t
,
2510 u32 type_id
, void *data
,
2511 u8 bits_offset
, struct btf_show
*show
)
2513 if (btf
->resolved_ids
)
2514 t
= btf_type_id_resolve(btf
, &type_id
);
2516 t
= btf_type_skip_modifiers(btf
, type_id
, NULL
);
2518 btf_type_ops(t
)->show(btf
, t
, type_id
, data
, bits_offset
, show
);
2521 static void btf_var_show(const struct btf
*btf
, const struct btf_type
*t
,
2522 u32 type_id
, void *data
, u8 bits_offset
,
2523 struct btf_show
*show
)
2525 t
= btf_type_id_resolve(btf
, &type_id
);
2527 btf_type_ops(t
)->show(btf
, t
, type_id
, data
, bits_offset
, show
);
2530 static void btf_ptr_show(const struct btf
*btf
, const struct btf_type
*t
,
2531 u32 type_id
, void *data
, u8 bits_offset
,
2532 struct btf_show
*show
)
2536 safe_data
= btf_show_start_type(show
, t
, type_id
, data
);
2540 /* It is a hashed value unless BTF_SHOW_PTR_RAW is specified */
2541 if (show
->flags
& BTF_SHOW_PTR_RAW
)
2542 btf_show_type_value(show
, "0x%px", *(void **)safe_data
);
2544 btf_show_type_value(show
, "0x%p", *(void **)safe_data
);
2545 btf_show_end_type(show
);
2548 static void btf_ref_type_log(struct btf_verifier_env
*env
,
2549 const struct btf_type
*t
)
2551 btf_verifier_log(env
, "type_id=%u", t
->type
);
2554 static struct btf_kind_operations modifier_ops
= {
2555 .check_meta
= btf_ref_type_check_meta
,
2556 .resolve
= btf_modifier_resolve
,
2557 .check_member
= btf_modifier_check_member
,
2558 .check_kflag_member
= btf_modifier_check_kflag_member
,
2559 .log_details
= btf_ref_type_log
,
2560 .show
= btf_modifier_show
,
2563 static struct btf_kind_operations ptr_ops
= {
2564 .check_meta
= btf_ref_type_check_meta
,
2565 .resolve
= btf_ptr_resolve
,
2566 .check_member
= btf_ptr_check_member
,
2567 .check_kflag_member
= btf_generic_check_kflag_member
,
2568 .log_details
= btf_ref_type_log
,
2569 .show
= btf_ptr_show
,
2572 static s32
btf_fwd_check_meta(struct btf_verifier_env
*env
,
2573 const struct btf_type
*t
,
2576 if (btf_type_vlen(t
)) {
2577 btf_verifier_log_type(env
, t
, "vlen != 0");
2582 btf_verifier_log_type(env
, t
, "type != 0");
2586 /* fwd type must have a valid name */
2588 !btf_name_valid_identifier(env
->btf
, t
->name_off
)) {
2589 btf_verifier_log_type(env
, t
, "Invalid name");
2593 btf_verifier_log_type(env
, t
, NULL
);
2598 static void btf_fwd_type_log(struct btf_verifier_env
*env
,
2599 const struct btf_type
*t
)
2601 btf_verifier_log(env
, "%s", btf_type_kflag(t
) ? "union" : "struct");
2604 static struct btf_kind_operations fwd_ops
= {
2605 .check_meta
= btf_fwd_check_meta
,
2606 .resolve
= btf_df_resolve
,
2607 .check_member
= btf_df_check_member
,
2608 .check_kflag_member
= btf_df_check_kflag_member
,
2609 .log_details
= btf_fwd_type_log
,
2610 .show
= btf_df_show
,
2613 static int btf_array_check_member(struct btf_verifier_env
*env
,
2614 const struct btf_type
*struct_type
,
2615 const struct btf_member
*member
,
2616 const struct btf_type
*member_type
)
2618 u32 struct_bits_off
= member
->offset
;
2619 u32 struct_size
, bytes_offset
;
2620 u32 array_type_id
, array_size
;
2621 struct btf
*btf
= env
->btf
;
2623 if (BITS_PER_BYTE_MASKED(struct_bits_off
)) {
2624 btf_verifier_log_member(env
, struct_type
, member
,
2625 "Member is not byte aligned");
2629 array_type_id
= member
->type
;
2630 btf_type_id_size(btf
, &array_type_id
, &array_size
);
2631 struct_size
= struct_type
->size
;
2632 bytes_offset
= BITS_ROUNDDOWN_BYTES(struct_bits_off
);
2633 if (struct_size
- bytes_offset
< array_size
) {
2634 btf_verifier_log_member(env
, struct_type
, member
,
2635 "Member exceeds struct_size");
2642 static s32
btf_array_check_meta(struct btf_verifier_env
*env
,
2643 const struct btf_type
*t
,
2646 const struct btf_array
*array
= btf_type_array(t
);
2647 u32 meta_needed
= sizeof(*array
);
2649 if (meta_left
< meta_needed
) {
2650 btf_verifier_log_basic(env
, t
,
2651 "meta_left:%u meta_needed:%u",
2652 meta_left
, meta_needed
);
2656 /* array type should not have a name */
2658 btf_verifier_log_type(env
, t
, "Invalid name");
2662 if (btf_type_vlen(t
)) {
2663 btf_verifier_log_type(env
, t
, "vlen != 0");
2667 if (btf_type_kflag(t
)) {
2668 btf_verifier_log_type(env
, t
, "Invalid btf_info kind_flag");
2673 btf_verifier_log_type(env
, t
, "size != 0");
2677 /* Array elem type and index type cannot be in type void,
2678 * so !array->type and !array->index_type are not allowed.
2680 if (!array
->type
|| !BTF_TYPE_ID_VALID(array
->type
)) {
2681 btf_verifier_log_type(env
, t
, "Invalid elem");
2685 if (!array
->index_type
|| !BTF_TYPE_ID_VALID(array
->index_type
)) {
2686 btf_verifier_log_type(env
, t
, "Invalid index");
2690 btf_verifier_log_type(env
, t
, NULL
);
2695 static int btf_array_resolve(struct btf_verifier_env
*env
,
2696 const struct resolve_vertex
*v
)
2698 const struct btf_array
*array
= btf_type_array(v
->t
);
2699 const struct btf_type
*elem_type
, *index_type
;
2700 u32 elem_type_id
, index_type_id
;
2701 struct btf
*btf
= env
->btf
;
2704 /* Check array->index_type */
2705 index_type_id
= array
->index_type
;
2706 index_type
= btf_type_by_id(btf
, index_type_id
);
2707 if (btf_type_nosize_or_null(index_type
) ||
2708 btf_type_is_resolve_source_only(index_type
)) {
2709 btf_verifier_log_type(env
, v
->t
, "Invalid index");
2713 if (!env_type_is_resolve_sink(env
, index_type
) &&
2714 !env_type_is_resolved(env
, index_type_id
))
2715 return env_stack_push(env
, index_type
, index_type_id
);
2717 index_type
= btf_type_id_size(btf
, &index_type_id
, NULL
);
2718 if (!index_type
|| !btf_type_is_int(index_type
) ||
2719 !btf_type_int_is_regular(index_type
)) {
2720 btf_verifier_log_type(env
, v
->t
, "Invalid index");
2724 /* Check array->type */
2725 elem_type_id
= array
->type
;
2726 elem_type
= btf_type_by_id(btf
, elem_type_id
);
2727 if (btf_type_nosize_or_null(elem_type
) ||
2728 btf_type_is_resolve_source_only(elem_type
)) {
2729 btf_verifier_log_type(env
, v
->t
,
2734 if (!env_type_is_resolve_sink(env
, elem_type
) &&
2735 !env_type_is_resolved(env
, elem_type_id
))
2736 return env_stack_push(env
, elem_type
, elem_type_id
);
2738 elem_type
= btf_type_id_size(btf
, &elem_type_id
, &elem_size
);
2740 btf_verifier_log_type(env
, v
->t
, "Invalid elem");
2744 if (btf_type_is_int(elem_type
) && !btf_type_int_is_regular(elem_type
)) {
2745 btf_verifier_log_type(env
, v
->t
, "Invalid array of int");
2749 if (array
->nelems
&& elem_size
> U32_MAX
/ array
->nelems
) {
2750 btf_verifier_log_type(env
, v
->t
,
2751 "Array size overflows U32_MAX");
2755 env_stack_pop_resolved(env
, elem_type_id
, elem_size
* array
->nelems
);
2760 static void btf_array_log(struct btf_verifier_env
*env
,
2761 const struct btf_type
*t
)
2763 const struct btf_array
*array
= btf_type_array(t
);
2765 btf_verifier_log(env
, "type_id=%u index_type_id=%u nr_elems=%u",
2766 array
->type
, array
->index_type
, array
->nelems
);
2769 static void __btf_array_show(const struct btf
*btf
, const struct btf_type
*t
,
2770 u32 type_id
, void *data
, u8 bits_offset
,
2771 struct btf_show
*show
)
2773 const struct btf_array
*array
= btf_type_array(t
);
2774 const struct btf_kind_operations
*elem_ops
;
2775 const struct btf_type
*elem_type
;
2776 u32 i
, elem_size
= 0, elem_type_id
;
2779 elem_type_id
= array
->type
;
2780 elem_type
= btf_type_skip_modifiers(btf
, elem_type_id
, NULL
);
2781 if (elem_type
&& btf_type_has_size(elem_type
))
2782 elem_size
= elem_type
->size
;
2784 if (elem_type
&& btf_type_is_int(elem_type
)) {
2785 u32 int_type
= btf_type_int(elem_type
);
2787 encoding
= BTF_INT_ENCODING(int_type
);
2790 * BTF_INT_CHAR encoding never seems to be set for
2791 * char arrays, so if size is 1 and element is
2792 * printable as a char, we'll do that.
2795 encoding
= BTF_INT_CHAR
;
2798 if (!btf_show_start_array_type(show
, t
, type_id
, encoding
, data
))
2803 elem_ops
= btf_type_ops(elem_type
);
2805 for (i
= 0; i
< array
->nelems
; i
++) {
2807 btf_show_start_array_member(show
);
2809 elem_ops
->show(btf
, elem_type
, elem_type_id
, data
,
2813 btf_show_end_array_member(show
);
2815 if (show
->state
.array_terminated
)
2819 btf_show_end_array_type(show
);
2822 static void btf_array_show(const struct btf
*btf
, const struct btf_type
*t
,
2823 u32 type_id
, void *data
, u8 bits_offset
,
2824 struct btf_show
*show
)
2826 const struct btf_member
*m
= show
->state
.member
;
2829 * First check if any members would be shown (are non-zero).
2830 * See comments above "struct btf_show" definition for more
2831 * details on how this works at a high-level.
2833 if (show
->state
.depth
> 0 && !(show
->flags
& BTF_SHOW_ZERO
)) {
2834 if (!show
->state
.depth_check
) {
2835 show
->state
.depth_check
= show
->state
.depth
+ 1;
2836 show
->state
.depth_to_show
= 0;
2838 __btf_array_show(btf
, t
, type_id
, data
, bits_offset
, show
);
2839 show
->state
.member
= m
;
2841 if (show
->state
.depth_check
!= show
->state
.depth
+ 1)
2843 show
->state
.depth_check
= 0;
2845 if (show
->state
.depth_to_show
<= show
->state
.depth
)
2848 * Reaching here indicates we have recursed and found
2849 * non-zero array member(s).
2852 __btf_array_show(btf
, t
, type_id
, data
, bits_offset
, show
);
2855 static struct btf_kind_operations array_ops
= {
2856 .check_meta
= btf_array_check_meta
,
2857 .resolve
= btf_array_resolve
,
2858 .check_member
= btf_array_check_member
,
2859 .check_kflag_member
= btf_generic_check_kflag_member
,
2860 .log_details
= btf_array_log
,
2861 .show
= btf_array_show
,
2864 static int btf_struct_check_member(struct btf_verifier_env
*env
,
2865 const struct btf_type
*struct_type
,
2866 const struct btf_member
*member
,
2867 const struct btf_type
*member_type
)
2869 u32 struct_bits_off
= member
->offset
;
2870 u32 struct_size
, bytes_offset
;
2872 if (BITS_PER_BYTE_MASKED(struct_bits_off
)) {
2873 btf_verifier_log_member(env
, struct_type
, member
,
2874 "Member is not byte aligned");
2878 struct_size
= struct_type
->size
;
2879 bytes_offset
= BITS_ROUNDDOWN_BYTES(struct_bits_off
);
2880 if (struct_size
- bytes_offset
< member_type
->size
) {
2881 btf_verifier_log_member(env
, struct_type
, member
,
2882 "Member exceeds struct_size");
2889 static s32
btf_struct_check_meta(struct btf_verifier_env
*env
,
2890 const struct btf_type
*t
,
2893 bool is_union
= BTF_INFO_KIND(t
->info
) == BTF_KIND_UNION
;
2894 const struct btf_member
*member
;
2895 u32 meta_needed
, last_offset
;
2896 struct btf
*btf
= env
->btf
;
2897 u32 struct_size
= t
->size
;
2901 meta_needed
= btf_type_vlen(t
) * sizeof(*member
);
2902 if (meta_left
< meta_needed
) {
2903 btf_verifier_log_basic(env
, t
,
2904 "meta_left:%u meta_needed:%u",
2905 meta_left
, meta_needed
);
2909 /* struct type either no name or a valid one */
2911 !btf_name_valid_identifier(env
->btf
, t
->name_off
)) {
2912 btf_verifier_log_type(env
, t
, "Invalid name");
2916 btf_verifier_log_type(env
, t
, NULL
);
2919 for_each_member(i
, t
, member
) {
2920 if (!btf_name_offset_valid(btf
, member
->name_off
)) {
2921 btf_verifier_log_member(env
, t
, member
,
2922 "Invalid member name_offset:%u",
2927 /* struct member either no name or a valid one */
2928 if (member
->name_off
&&
2929 !btf_name_valid_identifier(btf
, member
->name_off
)) {
2930 btf_verifier_log_member(env
, t
, member
, "Invalid name");
2933 /* A member cannot be in type void */
2934 if (!member
->type
|| !BTF_TYPE_ID_VALID(member
->type
)) {
2935 btf_verifier_log_member(env
, t
, member
,
2940 offset
= btf_member_bit_offset(t
, member
);
2941 if (is_union
&& offset
) {
2942 btf_verifier_log_member(env
, t
, member
,
2943 "Invalid member bits_offset");
2948 * ">" instead of ">=" because the last member could be
2951 if (last_offset
> offset
) {
2952 btf_verifier_log_member(env
, t
, member
,
2953 "Invalid member bits_offset");
2957 if (BITS_ROUNDUP_BYTES(offset
) > struct_size
) {
2958 btf_verifier_log_member(env
, t
, member
,
2959 "Member bits_offset exceeds its struct size");
2963 btf_verifier_log_member(env
, t
, member
, NULL
);
2964 last_offset
= offset
;
2970 static int btf_struct_resolve(struct btf_verifier_env
*env
,
2971 const struct resolve_vertex
*v
)
2973 const struct btf_member
*member
;
2977 /* Before continue resolving the next_member,
2978 * ensure the last member is indeed resolved to a
2979 * type with size info.
2981 if (v
->next_member
) {
2982 const struct btf_type
*last_member_type
;
2983 const struct btf_member
*last_member
;
2984 u16 last_member_type_id
;
2986 last_member
= btf_type_member(v
->t
) + v
->next_member
- 1;
2987 last_member_type_id
= last_member
->type
;
2988 if (WARN_ON_ONCE(!env_type_is_resolved(env
,
2989 last_member_type_id
)))
2992 last_member_type
= btf_type_by_id(env
->btf
,
2993 last_member_type_id
);
2994 if (btf_type_kflag(v
->t
))
2995 err
= btf_type_ops(last_member_type
)->check_kflag_member(env
, v
->t
,
2999 err
= btf_type_ops(last_member_type
)->check_member(env
, v
->t
,
3006 for_each_member_from(i
, v
->next_member
, v
->t
, member
) {
3007 u32 member_type_id
= member
->type
;
3008 const struct btf_type
*member_type
= btf_type_by_id(env
->btf
,
3011 if (btf_type_nosize_or_null(member_type
) ||
3012 btf_type_is_resolve_source_only(member_type
)) {
3013 btf_verifier_log_member(env
, v
->t
, member
,
3018 if (!env_type_is_resolve_sink(env
, member_type
) &&
3019 !env_type_is_resolved(env
, member_type_id
)) {
3020 env_stack_set_next_member(env
, i
+ 1);
3021 return env_stack_push(env
, member_type
, member_type_id
);
3024 if (btf_type_kflag(v
->t
))
3025 err
= btf_type_ops(member_type
)->check_kflag_member(env
, v
->t
,
3029 err
= btf_type_ops(member_type
)->check_member(env
, v
->t
,
3036 env_stack_pop_resolved(env
, 0, 0);
3041 static void btf_struct_log(struct btf_verifier_env
*env
,
3042 const struct btf_type
*t
)
3044 btf_verifier_log(env
, "size=%u vlen=%u", t
->size
, btf_type_vlen(t
));
3047 /* find 'struct bpf_spin_lock' in map value.
3048 * return >= 0 offset if found
3049 * and < 0 in case of error
3051 int btf_find_spin_lock(const struct btf
*btf
, const struct btf_type
*t
)
3053 const struct btf_member
*member
;
3054 u32 i
, off
= -ENOENT
;
3056 if (!__btf_type_is_struct(t
))
3059 for_each_member(i
, t
, member
) {
3060 const struct btf_type
*member_type
= btf_type_by_id(btf
,
3062 if (!__btf_type_is_struct(member_type
))
3064 if (member_type
->size
!= sizeof(struct bpf_spin_lock
))
3066 if (strcmp(__btf_name_by_offset(btf
, member_type
->name_off
),
3070 /* only one 'struct bpf_spin_lock' is allowed */
3072 off
= btf_member_bit_offset(t
, member
);
3074 /* valid C code cannot generate such BTF */
3077 if (off
% __alignof__(struct bpf_spin_lock
))
3078 /* valid struct bpf_spin_lock will be 4 byte aligned */
3084 static void __btf_struct_show(const struct btf
*btf
, const struct btf_type
*t
,
3085 u32 type_id
, void *data
, u8 bits_offset
,
3086 struct btf_show
*show
)
3088 const struct btf_member
*member
;
3092 safe_data
= btf_show_start_struct_type(show
, t
, type_id
, data
);
3096 for_each_member(i
, t
, member
) {
3097 const struct btf_type
*member_type
= btf_type_by_id(btf
,
3099 const struct btf_kind_operations
*ops
;
3100 u32 member_offset
, bitfield_size
;
3104 btf_show_start_member(show
, member
);
3106 member_offset
= btf_member_bit_offset(t
, member
);
3107 bitfield_size
= btf_member_bitfield_size(t
, member
);
3108 bytes_offset
= BITS_ROUNDDOWN_BYTES(member_offset
);
3109 bits8_offset
= BITS_PER_BYTE_MASKED(member_offset
);
3110 if (bitfield_size
) {
3111 safe_data
= btf_show_start_type(show
, member_type
,
3113 data
+ bytes_offset
);
3115 btf_bitfield_show(safe_data
,
3117 bitfield_size
, show
);
3118 btf_show_end_type(show
);
3120 ops
= btf_type_ops(member_type
);
3121 ops
->show(btf
, member_type
, member
->type
,
3122 data
+ bytes_offset
, bits8_offset
, show
);
3125 btf_show_end_member(show
);
3128 btf_show_end_struct_type(show
);
3131 static void btf_struct_show(const struct btf
*btf
, const struct btf_type
*t
,
3132 u32 type_id
, void *data
, u8 bits_offset
,
3133 struct btf_show
*show
)
3135 const struct btf_member
*m
= show
->state
.member
;
3138 * First check if any members would be shown (are non-zero).
3139 * See comments above "struct btf_show" definition for more
3140 * details on how this works at a high-level.
3142 if (show
->state
.depth
> 0 && !(show
->flags
& BTF_SHOW_ZERO
)) {
3143 if (!show
->state
.depth_check
) {
3144 show
->state
.depth_check
= show
->state
.depth
+ 1;
3145 show
->state
.depth_to_show
= 0;
3147 __btf_struct_show(btf
, t
, type_id
, data
, bits_offset
, show
);
3148 /* Restore saved member data here */
3149 show
->state
.member
= m
;
3150 if (show
->state
.depth_check
!= show
->state
.depth
+ 1)
3152 show
->state
.depth_check
= 0;
3154 if (show
->state
.depth_to_show
<= show
->state
.depth
)
3157 * Reaching here indicates we have recursed and found
3158 * non-zero child values.
3162 __btf_struct_show(btf
, t
, type_id
, data
, bits_offset
, show
);
3165 static struct btf_kind_operations struct_ops
= {
3166 .check_meta
= btf_struct_check_meta
,
3167 .resolve
= btf_struct_resolve
,
3168 .check_member
= btf_struct_check_member
,
3169 .check_kflag_member
= btf_generic_check_kflag_member
,
3170 .log_details
= btf_struct_log
,
3171 .show
= btf_struct_show
,
3174 static int btf_enum_check_member(struct btf_verifier_env
*env
,
3175 const struct btf_type
*struct_type
,
3176 const struct btf_member
*member
,
3177 const struct btf_type
*member_type
)
3179 u32 struct_bits_off
= member
->offset
;
3180 u32 struct_size
, bytes_offset
;
3182 if (BITS_PER_BYTE_MASKED(struct_bits_off
)) {
3183 btf_verifier_log_member(env
, struct_type
, member
,
3184 "Member is not byte aligned");
3188 struct_size
= struct_type
->size
;
3189 bytes_offset
= BITS_ROUNDDOWN_BYTES(struct_bits_off
);
3190 if (struct_size
- bytes_offset
< member_type
->size
) {
3191 btf_verifier_log_member(env
, struct_type
, member
,
3192 "Member exceeds struct_size");
3199 static int btf_enum_check_kflag_member(struct btf_verifier_env
*env
,
3200 const struct btf_type
*struct_type
,
3201 const struct btf_member
*member
,
3202 const struct btf_type
*member_type
)
3204 u32 struct_bits_off
, nr_bits
, bytes_end
, struct_size
;
3205 u32 int_bitsize
= sizeof(int) * BITS_PER_BYTE
;
3207 struct_bits_off
= BTF_MEMBER_BIT_OFFSET(member
->offset
);
3208 nr_bits
= BTF_MEMBER_BITFIELD_SIZE(member
->offset
);
3210 if (BITS_PER_BYTE_MASKED(struct_bits_off
)) {
3211 btf_verifier_log_member(env
, struct_type
, member
,
3212 "Member is not byte aligned");
3216 nr_bits
= int_bitsize
;
3217 } else if (nr_bits
> int_bitsize
) {
3218 btf_verifier_log_member(env
, struct_type
, member
,
3219 "Invalid member bitfield_size");
3223 struct_size
= struct_type
->size
;
3224 bytes_end
= BITS_ROUNDUP_BYTES(struct_bits_off
+ nr_bits
);
3225 if (struct_size
< bytes_end
) {
3226 btf_verifier_log_member(env
, struct_type
, member
,
3227 "Member exceeds struct_size");
3234 static s32
btf_enum_check_meta(struct btf_verifier_env
*env
,
3235 const struct btf_type
*t
,
3238 const struct btf_enum
*enums
= btf_type_enum(t
);
3239 struct btf
*btf
= env
->btf
;
3243 nr_enums
= btf_type_vlen(t
);
3244 meta_needed
= nr_enums
* sizeof(*enums
);
3246 if (meta_left
< meta_needed
) {
3247 btf_verifier_log_basic(env
, t
,
3248 "meta_left:%u meta_needed:%u",
3249 meta_left
, meta_needed
);
3253 if (btf_type_kflag(t
)) {
3254 btf_verifier_log_type(env
, t
, "Invalid btf_info kind_flag");
3258 if (t
->size
> 8 || !is_power_of_2(t
->size
)) {
3259 btf_verifier_log_type(env
, t
, "Unexpected size");
3263 /* enum type either no name or a valid one */
3265 !btf_name_valid_identifier(env
->btf
, t
->name_off
)) {
3266 btf_verifier_log_type(env
, t
, "Invalid name");
3270 btf_verifier_log_type(env
, t
, NULL
);
3272 for (i
= 0; i
< nr_enums
; i
++) {
3273 if (!btf_name_offset_valid(btf
, enums
[i
].name_off
)) {
3274 btf_verifier_log(env
, "\tInvalid name_offset:%u",
3279 /* enum member must have a valid name */
3280 if (!enums
[i
].name_off
||
3281 !btf_name_valid_identifier(btf
, enums
[i
].name_off
)) {
3282 btf_verifier_log_type(env
, t
, "Invalid name");
3286 if (env
->log
.level
== BPF_LOG_KERNEL
)
3288 btf_verifier_log(env
, "\t%s val=%d\n",
3289 __btf_name_by_offset(btf
, enums
[i
].name_off
),
3296 static void btf_enum_log(struct btf_verifier_env
*env
,
3297 const struct btf_type
*t
)
3299 btf_verifier_log(env
, "size=%u vlen=%u", t
->size
, btf_type_vlen(t
));
3302 static void btf_enum_show(const struct btf
*btf
, const struct btf_type
*t
,
3303 u32 type_id
, void *data
, u8 bits_offset
,
3304 struct btf_show
*show
)
3306 const struct btf_enum
*enums
= btf_type_enum(t
);
3307 u32 i
, nr_enums
= btf_type_vlen(t
);
3311 safe_data
= btf_show_start_type(show
, t
, type_id
, data
);
3315 v
= *(int *)safe_data
;
3317 for (i
= 0; i
< nr_enums
; i
++) {
3318 if (v
!= enums
[i
].val
)
3321 btf_show_type_value(show
, "%s",
3322 __btf_name_by_offset(btf
,
3323 enums
[i
].name_off
));
3325 btf_show_end_type(show
);
3329 btf_show_type_value(show
, "%d", v
);
3330 btf_show_end_type(show
);
3333 static struct btf_kind_operations enum_ops
= {
3334 .check_meta
= btf_enum_check_meta
,
3335 .resolve
= btf_df_resolve
,
3336 .check_member
= btf_enum_check_member
,
3337 .check_kflag_member
= btf_enum_check_kflag_member
,
3338 .log_details
= btf_enum_log
,
3339 .show
= btf_enum_show
,
3342 static s32
btf_func_proto_check_meta(struct btf_verifier_env
*env
,
3343 const struct btf_type
*t
,
3346 u32 meta_needed
= btf_type_vlen(t
) * sizeof(struct btf_param
);
3348 if (meta_left
< meta_needed
) {
3349 btf_verifier_log_basic(env
, t
,
3350 "meta_left:%u meta_needed:%u",
3351 meta_left
, meta_needed
);
3356 btf_verifier_log_type(env
, t
, "Invalid name");
3360 if (btf_type_kflag(t
)) {
3361 btf_verifier_log_type(env
, t
, "Invalid btf_info kind_flag");
3365 btf_verifier_log_type(env
, t
, NULL
);
3370 static void btf_func_proto_log(struct btf_verifier_env
*env
,
3371 const struct btf_type
*t
)
3373 const struct btf_param
*args
= (const struct btf_param
*)(t
+ 1);
3374 u16 nr_args
= btf_type_vlen(t
), i
;
3376 btf_verifier_log(env
, "return=%u args=(", t
->type
);
3378 btf_verifier_log(env
, "void");
3382 if (nr_args
== 1 && !args
[0].type
) {
3383 /* Only one vararg */
3384 btf_verifier_log(env
, "vararg");
3388 btf_verifier_log(env
, "%u %s", args
[0].type
,
3389 __btf_name_by_offset(env
->btf
,
3391 for (i
= 1; i
< nr_args
- 1; i
++)
3392 btf_verifier_log(env
, ", %u %s", args
[i
].type
,
3393 __btf_name_by_offset(env
->btf
,
3397 const struct btf_param
*last_arg
= &args
[nr_args
- 1];
3400 btf_verifier_log(env
, ", %u %s", last_arg
->type
,
3401 __btf_name_by_offset(env
->btf
,
3402 last_arg
->name_off
));
3404 btf_verifier_log(env
, ", vararg");
3408 btf_verifier_log(env
, ")");
3411 static struct btf_kind_operations func_proto_ops
= {
3412 .check_meta
= btf_func_proto_check_meta
,
3413 .resolve
= btf_df_resolve
,
3415 * BTF_KIND_FUNC_PROTO cannot be directly referred by
3416 * a struct's member.
3418 * It should be a funciton pointer instead.
3419 * (i.e. struct's member -> BTF_KIND_PTR -> BTF_KIND_FUNC_PROTO)
3421 * Hence, there is no btf_func_check_member().
3423 .check_member
= btf_df_check_member
,
3424 .check_kflag_member
= btf_df_check_kflag_member
,
3425 .log_details
= btf_func_proto_log
,
3426 .show
= btf_df_show
,
3429 static s32
btf_func_check_meta(struct btf_verifier_env
*env
,
3430 const struct btf_type
*t
,
3434 !btf_name_valid_identifier(env
->btf
, t
->name_off
)) {
3435 btf_verifier_log_type(env
, t
, "Invalid name");
3439 if (btf_type_vlen(t
) > BTF_FUNC_GLOBAL
) {
3440 btf_verifier_log_type(env
, t
, "Invalid func linkage");
3444 if (btf_type_kflag(t
)) {
3445 btf_verifier_log_type(env
, t
, "Invalid btf_info kind_flag");
3449 btf_verifier_log_type(env
, t
, NULL
);
3454 static struct btf_kind_operations func_ops
= {
3455 .check_meta
= btf_func_check_meta
,
3456 .resolve
= btf_df_resolve
,
3457 .check_member
= btf_df_check_member
,
3458 .check_kflag_member
= btf_df_check_kflag_member
,
3459 .log_details
= btf_ref_type_log
,
3460 .show
= btf_df_show
,
3463 static s32
btf_var_check_meta(struct btf_verifier_env
*env
,
3464 const struct btf_type
*t
,
3467 const struct btf_var
*var
;
3468 u32 meta_needed
= sizeof(*var
);
3470 if (meta_left
< meta_needed
) {
3471 btf_verifier_log_basic(env
, t
,
3472 "meta_left:%u meta_needed:%u",
3473 meta_left
, meta_needed
);
3477 if (btf_type_vlen(t
)) {
3478 btf_verifier_log_type(env
, t
, "vlen != 0");
3482 if (btf_type_kflag(t
)) {
3483 btf_verifier_log_type(env
, t
, "Invalid btf_info kind_flag");
3488 !__btf_name_valid(env
->btf
, t
->name_off
, true)) {
3489 btf_verifier_log_type(env
, t
, "Invalid name");
3493 /* A var cannot be in type void */
3494 if (!t
->type
|| !BTF_TYPE_ID_VALID(t
->type
)) {
3495 btf_verifier_log_type(env
, t
, "Invalid type_id");
3499 var
= btf_type_var(t
);
3500 if (var
->linkage
!= BTF_VAR_STATIC
&&
3501 var
->linkage
!= BTF_VAR_GLOBAL_ALLOCATED
) {
3502 btf_verifier_log_type(env
, t
, "Linkage not supported");
3506 btf_verifier_log_type(env
, t
, NULL
);
3511 static void btf_var_log(struct btf_verifier_env
*env
, const struct btf_type
*t
)
3513 const struct btf_var
*var
= btf_type_var(t
);
3515 btf_verifier_log(env
, "type_id=%u linkage=%u", t
->type
, var
->linkage
);
3518 static const struct btf_kind_operations var_ops
= {
3519 .check_meta
= btf_var_check_meta
,
3520 .resolve
= btf_var_resolve
,
3521 .check_member
= btf_df_check_member
,
3522 .check_kflag_member
= btf_df_check_kflag_member
,
3523 .log_details
= btf_var_log
,
3524 .show
= btf_var_show
,
3527 static s32
btf_datasec_check_meta(struct btf_verifier_env
*env
,
3528 const struct btf_type
*t
,
3531 const struct btf_var_secinfo
*vsi
;
3532 u64 last_vsi_end_off
= 0, sum
= 0;
3535 meta_needed
= btf_type_vlen(t
) * sizeof(*vsi
);
3536 if (meta_left
< meta_needed
) {
3537 btf_verifier_log_basic(env
, t
,
3538 "meta_left:%u meta_needed:%u",
3539 meta_left
, meta_needed
);
3543 if (!btf_type_vlen(t
)) {
3544 btf_verifier_log_type(env
, t
, "vlen == 0");
3549 btf_verifier_log_type(env
, t
, "size == 0");
3553 if (btf_type_kflag(t
)) {
3554 btf_verifier_log_type(env
, t
, "Invalid btf_info kind_flag");
3559 !btf_name_valid_section(env
->btf
, t
->name_off
)) {
3560 btf_verifier_log_type(env
, t
, "Invalid name");
3564 btf_verifier_log_type(env
, t
, NULL
);
3566 for_each_vsi(i
, t
, vsi
) {
3567 /* A var cannot be in type void */
3568 if (!vsi
->type
|| !BTF_TYPE_ID_VALID(vsi
->type
)) {
3569 btf_verifier_log_vsi(env
, t
, vsi
,
3574 if (vsi
->offset
< last_vsi_end_off
|| vsi
->offset
>= t
->size
) {
3575 btf_verifier_log_vsi(env
, t
, vsi
,
3580 if (!vsi
->size
|| vsi
->size
> t
->size
) {
3581 btf_verifier_log_vsi(env
, t
, vsi
,
3586 last_vsi_end_off
= vsi
->offset
+ vsi
->size
;
3587 if (last_vsi_end_off
> t
->size
) {
3588 btf_verifier_log_vsi(env
, t
, vsi
,
3589 "Invalid offset+size");
3593 btf_verifier_log_vsi(env
, t
, vsi
, NULL
);
3597 if (t
->size
< sum
) {
3598 btf_verifier_log_type(env
, t
, "Invalid btf_info size");
3605 static int btf_datasec_resolve(struct btf_verifier_env
*env
,
3606 const struct resolve_vertex
*v
)
3608 const struct btf_var_secinfo
*vsi
;
3609 struct btf
*btf
= env
->btf
;
3612 for_each_vsi_from(i
, v
->next_member
, v
->t
, vsi
) {
3613 u32 var_type_id
= vsi
->type
, type_id
, type_size
= 0;
3614 const struct btf_type
*var_type
= btf_type_by_id(env
->btf
,
3616 if (!var_type
|| !btf_type_is_var(var_type
)) {
3617 btf_verifier_log_vsi(env
, v
->t
, vsi
,
3618 "Not a VAR kind member");
3622 if (!env_type_is_resolve_sink(env
, var_type
) &&
3623 !env_type_is_resolved(env
, var_type_id
)) {
3624 env_stack_set_next_member(env
, i
+ 1);
3625 return env_stack_push(env
, var_type
, var_type_id
);
3628 type_id
= var_type
->type
;
3629 if (!btf_type_id_size(btf
, &type_id
, &type_size
)) {
3630 btf_verifier_log_vsi(env
, v
->t
, vsi
, "Invalid type");
3634 if (vsi
->size
< type_size
) {
3635 btf_verifier_log_vsi(env
, v
->t
, vsi
, "Invalid size");
3640 env_stack_pop_resolved(env
, 0, 0);
3644 static void btf_datasec_log(struct btf_verifier_env
*env
,
3645 const struct btf_type
*t
)
3647 btf_verifier_log(env
, "size=%u vlen=%u", t
->size
, btf_type_vlen(t
));
3650 static void btf_datasec_show(const struct btf
*btf
,
3651 const struct btf_type
*t
, u32 type_id
,
3652 void *data
, u8 bits_offset
,
3653 struct btf_show
*show
)
3655 const struct btf_var_secinfo
*vsi
;
3656 const struct btf_type
*var
;
3659 if (!btf_show_start_type(show
, t
, type_id
, data
))
3662 btf_show_type_value(show
, "section (\"%s\") = {",
3663 __btf_name_by_offset(btf
, t
->name_off
));
3664 for_each_vsi(i
, t
, vsi
) {
3665 var
= btf_type_by_id(btf
, vsi
->type
);
3667 btf_show(show
, ",");
3668 btf_type_ops(var
)->show(btf
, var
, vsi
->type
,
3669 data
+ vsi
->offset
, bits_offset
, show
);
3671 btf_show_end_type(show
);
3674 static const struct btf_kind_operations datasec_ops
= {
3675 .check_meta
= btf_datasec_check_meta
,
3676 .resolve
= btf_datasec_resolve
,
3677 .check_member
= btf_df_check_member
,
3678 .check_kflag_member
= btf_df_check_kflag_member
,
3679 .log_details
= btf_datasec_log
,
3680 .show
= btf_datasec_show
,
3683 static int btf_func_proto_check(struct btf_verifier_env
*env
,
3684 const struct btf_type
*t
)
3686 const struct btf_type
*ret_type
;
3687 const struct btf_param
*args
;
3688 const struct btf
*btf
;
3693 args
= (const struct btf_param
*)(t
+ 1);
3694 nr_args
= btf_type_vlen(t
);
3696 /* Check func return type which could be "void" (t->type == 0) */
3698 u32 ret_type_id
= t
->type
;
3700 ret_type
= btf_type_by_id(btf
, ret_type_id
);
3702 btf_verifier_log_type(env
, t
, "Invalid return type");
3706 if (btf_type_needs_resolve(ret_type
) &&
3707 !env_type_is_resolved(env
, ret_type_id
)) {
3708 err
= btf_resolve(env
, ret_type
, ret_type_id
);
3713 /* Ensure the return type is a type that has a size */
3714 if (!btf_type_id_size(btf
, &ret_type_id
, NULL
)) {
3715 btf_verifier_log_type(env
, t
, "Invalid return type");
3723 /* Last func arg type_id could be 0 if it is a vararg */
3724 if (!args
[nr_args
- 1].type
) {
3725 if (args
[nr_args
- 1].name_off
) {
3726 btf_verifier_log_type(env
, t
, "Invalid arg#%u",
3734 for (i
= 0; i
< nr_args
; i
++) {
3735 const struct btf_type
*arg_type
;
3738 arg_type_id
= args
[i
].type
;
3739 arg_type
= btf_type_by_id(btf
, arg_type_id
);
3741 btf_verifier_log_type(env
, t
, "Invalid arg#%u", i
+ 1);
3746 if (args
[i
].name_off
&&
3747 (!btf_name_offset_valid(btf
, args
[i
].name_off
) ||
3748 !btf_name_valid_identifier(btf
, args
[i
].name_off
))) {
3749 btf_verifier_log_type(env
, t
,
3750 "Invalid arg#%u", i
+ 1);
3755 if (btf_type_needs_resolve(arg_type
) &&
3756 !env_type_is_resolved(env
, arg_type_id
)) {
3757 err
= btf_resolve(env
, arg_type
, arg_type_id
);
3762 if (!btf_type_id_size(btf
, &arg_type_id
, NULL
)) {
3763 btf_verifier_log_type(env
, t
, "Invalid arg#%u", i
+ 1);
3772 static int btf_func_check(struct btf_verifier_env
*env
,
3773 const struct btf_type
*t
)
3775 const struct btf_type
*proto_type
;
3776 const struct btf_param
*args
;
3777 const struct btf
*btf
;
3781 proto_type
= btf_type_by_id(btf
, t
->type
);
3783 if (!proto_type
|| !btf_type_is_func_proto(proto_type
)) {
3784 btf_verifier_log_type(env
, t
, "Invalid type_id");
3788 args
= (const struct btf_param
*)(proto_type
+ 1);
3789 nr_args
= btf_type_vlen(proto_type
);
3790 for (i
= 0; i
< nr_args
; i
++) {
3791 if (!args
[i
].name_off
&& args
[i
].type
) {
3792 btf_verifier_log_type(env
, t
, "Invalid arg#%u", i
+ 1);
3800 static const struct btf_kind_operations
* const kind_ops
[NR_BTF_KINDS
] = {
3801 [BTF_KIND_INT
] = &int_ops
,
3802 [BTF_KIND_PTR
] = &ptr_ops
,
3803 [BTF_KIND_ARRAY
] = &array_ops
,
3804 [BTF_KIND_STRUCT
] = &struct_ops
,
3805 [BTF_KIND_UNION
] = &struct_ops
,
3806 [BTF_KIND_ENUM
] = &enum_ops
,
3807 [BTF_KIND_FWD
] = &fwd_ops
,
3808 [BTF_KIND_TYPEDEF
] = &modifier_ops
,
3809 [BTF_KIND_VOLATILE
] = &modifier_ops
,
3810 [BTF_KIND_CONST
] = &modifier_ops
,
3811 [BTF_KIND_RESTRICT
] = &modifier_ops
,
3812 [BTF_KIND_FUNC
] = &func_ops
,
3813 [BTF_KIND_FUNC_PROTO
] = &func_proto_ops
,
3814 [BTF_KIND_VAR
] = &var_ops
,
3815 [BTF_KIND_DATASEC
] = &datasec_ops
,
3818 static s32
btf_check_meta(struct btf_verifier_env
*env
,
3819 const struct btf_type
*t
,
3822 u32 saved_meta_left
= meta_left
;
3825 if (meta_left
< sizeof(*t
)) {
3826 btf_verifier_log(env
, "[%u] meta_left:%u meta_needed:%zu",
3827 env
->log_type_id
, meta_left
, sizeof(*t
));
3830 meta_left
-= sizeof(*t
);
3832 if (t
->info
& ~BTF_INFO_MASK
) {
3833 btf_verifier_log(env
, "[%u] Invalid btf_info:%x",
3834 env
->log_type_id
, t
->info
);
3838 if (BTF_INFO_KIND(t
->info
) > BTF_KIND_MAX
||
3839 BTF_INFO_KIND(t
->info
) == BTF_KIND_UNKN
) {
3840 btf_verifier_log(env
, "[%u] Invalid kind:%u",
3841 env
->log_type_id
, BTF_INFO_KIND(t
->info
));
3845 if (!btf_name_offset_valid(env
->btf
, t
->name_off
)) {
3846 btf_verifier_log(env
, "[%u] Invalid name_offset:%u",
3847 env
->log_type_id
, t
->name_off
);
3851 var_meta_size
= btf_type_ops(t
)->check_meta(env
, t
, meta_left
);
3852 if (var_meta_size
< 0)
3853 return var_meta_size
;
3855 meta_left
-= var_meta_size
;
3857 return saved_meta_left
- meta_left
;
3860 static int btf_check_all_metas(struct btf_verifier_env
*env
)
3862 struct btf
*btf
= env
->btf
;
3863 struct btf_header
*hdr
;
3867 cur
= btf
->nohdr_data
+ hdr
->type_off
;
3868 end
= cur
+ hdr
->type_len
;
3870 env
->log_type_id
= btf
->base_btf
? btf
->start_id
: 1;
3872 struct btf_type
*t
= cur
;
3875 meta_size
= btf_check_meta(env
, t
, end
- cur
);
3879 btf_add_type(env
, t
);
3887 static bool btf_resolve_valid(struct btf_verifier_env
*env
,
3888 const struct btf_type
*t
,
3891 struct btf
*btf
= env
->btf
;
3893 if (!env_type_is_resolved(env
, type_id
))
3896 if (btf_type_is_struct(t
) || btf_type_is_datasec(t
))
3897 return !btf_resolved_type_id(btf
, type_id
) &&
3898 !btf_resolved_type_size(btf
, type_id
);
3900 if (btf_type_is_modifier(t
) || btf_type_is_ptr(t
) ||
3901 btf_type_is_var(t
)) {
3902 t
= btf_type_id_resolve(btf
, &type_id
);
3904 !btf_type_is_modifier(t
) &&
3905 !btf_type_is_var(t
) &&
3906 !btf_type_is_datasec(t
);
3909 if (btf_type_is_array(t
)) {
3910 const struct btf_array
*array
= btf_type_array(t
);
3911 const struct btf_type
*elem_type
;
3912 u32 elem_type_id
= array
->type
;
3915 elem_type
= btf_type_id_size(btf
, &elem_type_id
, &elem_size
);
3916 return elem_type
&& !btf_type_is_modifier(elem_type
) &&
3917 (array
->nelems
* elem_size
==
3918 btf_resolved_type_size(btf
, type_id
));
3924 static int btf_resolve(struct btf_verifier_env
*env
,
3925 const struct btf_type
*t
, u32 type_id
)
3927 u32 save_log_type_id
= env
->log_type_id
;
3928 const struct resolve_vertex
*v
;
3931 env
->resolve_mode
= RESOLVE_TBD
;
3932 env_stack_push(env
, t
, type_id
);
3933 while (!err
&& (v
= env_stack_peak(env
))) {
3934 env
->log_type_id
= v
->type_id
;
3935 err
= btf_type_ops(v
->t
)->resolve(env
, v
);
3938 env
->log_type_id
= type_id
;
3939 if (err
== -E2BIG
) {
3940 btf_verifier_log_type(env
, t
,
3941 "Exceeded max resolving depth:%u",
3943 } else if (err
== -EEXIST
) {
3944 btf_verifier_log_type(env
, t
, "Loop detected");
3947 /* Final sanity check */
3948 if (!err
&& !btf_resolve_valid(env
, t
, type_id
)) {
3949 btf_verifier_log_type(env
, t
, "Invalid resolve state");
3953 env
->log_type_id
= save_log_type_id
;
3957 static int btf_check_all_types(struct btf_verifier_env
*env
)
3959 struct btf
*btf
= env
->btf
;
3960 const struct btf_type
*t
;
3964 err
= env_resolve_init(env
);
3969 for (i
= btf
->base_btf
? 0 : 1; i
< btf
->nr_types
; i
++) {
3970 type_id
= btf
->start_id
+ i
;
3971 t
= btf_type_by_id(btf
, type_id
);
3973 env
->log_type_id
= type_id
;
3974 if (btf_type_needs_resolve(t
) &&
3975 !env_type_is_resolved(env
, type_id
)) {
3976 err
= btf_resolve(env
, t
, type_id
);
3981 if (btf_type_is_func_proto(t
)) {
3982 err
= btf_func_proto_check(env
, t
);
3987 if (btf_type_is_func(t
)) {
3988 err
= btf_func_check(env
, t
);
3997 static int btf_parse_type_sec(struct btf_verifier_env
*env
)
3999 const struct btf_header
*hdr
= &env
->btf
->hdr
;
4002 /* Type section must align to 4 bytes */
4003 if (hdr
->type_off
& (sizeof(u32
) - 1)) {
4004 btf_verifier_log(env
, "Unaligned type_off");
4008 if (!env
->btf
->base_btf
&& !hdr
->type_len
) {
4009 btf_verifier_log(env
, "No type found");
4013 err
= btf_check_all_metas(env
);
4017 return btf_check_all_types(env
);
4020 static int btf_parse_str_sec(struct btf_verifier_env
*env
)
4022 const struct btf_header
*hdr
;
4023 struct btf
*btf
= env
->btf
;
4024 const char *start
, *end
;
4027 start
= btf
->nohdr_data
+ hdr
->str_off
;
4028 end
= start
+ hdr
->str_len
;
4030 if (end
!= btf
->data
+ btf
->data_size
) {
4031 btf_verifier_log(env
, "String section is not at the end");
4035 btf
->strings
= start
;
4037 if (btf
->base_btf
&& !hdr
->str_len
)
4039 if (!hdr
->str_len
|| hdr
->str_len
- 1 > BTF_MAX_NAME_OFFSET
|| end
[-1]) {
4040 btf_verifier_log(env
, "Invalid string section");
4043 if (!btf
->base_btf
&& start
[0]) {
4044 btf_verifier_log(env
, "Invalid string section");
4051 static const size_t btf_sec_info_offset
[] = {
4052 offsetof(struct btf_header
, type_off
),
4053 offsetof(struct btf_header
, str_off
),
4056 static int btf_sec_info_cmp(const void *a
, const void *b
)
4058 const struct btf_sec_info
*x
= a
;
4059 const struct btf_sec_info
*y
= b
;
4061 return (int)(x
->off
- y
->off
) ? : (int)(x
->len
- y
->len
);
4064 static int btf_check_sec_info(struct btf_verifier_env
*env
,
4067 struct btf_sec_info secs
[ARRAY_SIZE(btf_sec_info_offset
)];
4068 u32 total
, expected_total
, i
;
4069 const struct btf_header
*hdr
;
4070 const struct btf
*btf
;
4075 /* Populate the secs from hdr */
4076 for (i
= 0; i
< ARRAY_SIZE(btf_sec_info_offset
); i
++)
4077 secs
[i
] = *(struct btf_sec_info
*)((void *)hdr
+
4078 btf_sec_info_offset
[i
]);
4080 sort(secs
, ARRAY_SIZE(btf_sec_info_offset
),
4081 sizeof(struct btf_sec_info
), btf_sec_info_cmp
, NULL
);
4083 /* Check for gaps and overlap among sections */
4085 expected_total
= btf_data_size
- hdr
->hdr_len
;
4086 for (i
= 0; i
< ARRAY_SIZE(btf_sec_info_offset
); i
++) {
4087 if (expected_total
< secs
[i
].off
) {
4088 btf_verifier_log(env
, "Invalid section offset");
4091 if (total
< secs
[i
].off
) {
4093 btf_verifier_log(env
, "Unsupported section found");
4096 if (total
> secs
[i
].off
) {
4097 btf_verifier_log(env
, "Section overlap found");
4100 if (expected_total
- total
< secs
[i
].len
) {
4101 btf_verifier_log(env
,
4102 "Total section length too long");
4105 total
+= secs
[i
].len
;
4108 /* There is data other than hdr and known sections */
4109 if (expected_total
!= total
) {
4110 btf_verifier_log(env
, "Unsupported section found");
4117 static int btf_parse_hdr(struct btf_verifier_env
*env
)
4119 u32 hdr_len
, hdr_copy
, btf_data_size
;
4120 const struct btf_header
*hdr
;
4125 btf_data_size
= btf
->data_size
;
4128 offsetof(struct btf_header
, hdr_len
) + sizeof(hdr
->hdr_len
)) {
4129 btf_verifier_log(env
, "hdr_len not found");
4134 hdr_len
= hdr
->hdr_len
;
4135 if (btf_data_size
< hdr_len
) {
4136 btf_verifier_log(env
, "btf_header not found");
4140 /* Ensure the unsupported header fields are zero */
4141 if (hdr_len
> sizeof(btf
->hdr
)) {
4142 u8
*expected_zero
= btf
->data
+ sizeof(btf
->hdr
);
4143 u8
*end
= btf
->data
+ hdr_len
;
4145 for (; expected_zero
< end
; expected_zero
++) {
4146 if (*expected_zero
) {
4147 btf_verifier_log(env
, "Unsupported btf_header");
4153 hdr_copy
= min_t(u32
, hdr_len
, sizeof(btf
->hdr
));
4154 memcpy(&btf
->hdr
, btf
->data
, hdr_copy
);
4158 btf_verifier_log_hdr(env
, btf_data_size
);
4160 if (hdr
->magic
!= BTF_MAGIC
) {
4161 btf_verifier_log(env
, "Invalid magic");
4165 if (hdr
->version
!= BTF_VERSION
) {
4166 btf_verifier_log(env
, "Unsupported version");
4171 btf_verifier_log(env
, "Unsupported flags");
4175 if (btf_data_size
== hdr
->hdr_len
) {
4176 btf_verifier_log(env
, "No data");
4180 err
= btf_check_sec_info(env
, btf_data_size
);
4187 static struct btf
*btf_parse(void __user
*btf_data
, u32 btf_data_size
,
4188 u32 log_level
, char __user
*log_ubuf
, u32 log_size
)
4190 struct btf_verifier_env
*env
= NULL
;
4191 struct bpf_verifier_log
*log
;
4192 struct btf
*btf
= NULL
;
4196 if (btf_data_size
> BTF_MAX_SIZE
)
4197 return ERR_PTR(-E2BIG
);
4199 env
= kzalloc(sizeof(*env
), GFP_KERNEL
| __GFP_NOWARN
);
4201 return ERR_PTR(-ENOMEM
);
4204 if (log_level
|| log_ubuf
|| log_size
) {
4205 /* user requested verbose verifier output
4206 * and supplied buffer to store the verification trace
4208 log
->level
= log_level
;
4209 log
->ubuf
= log_ubuf
;
4210 log
->len_total
= log_size
;
4212 /* log attributes have to be sane */
4213 if (log
->len_total
< 128 || log
->len_total
> UINT_MAX
>> 8 ||
4214 !log
->level
|| !log
->ubuf
) {
4220 btf
= kzalloc(sizeof(*btf
), GFP_KERNEL
| __GFP_NOWARN
);
4227 data
= kvmalloc(btf_data_size
, GFP_KERNEL
| __GFP_NOWARN
);
4234 btf
->data_size
= btf_data_size
;
4236 if (copy_from_user(data
, btf_data
, btf_data_size
)) {
4241 err
= btf_parse_hdr(env
);
4245 btf
->nohdr_data
= btf
->data
+ btf
->hdr
.hdr_len
;
4247 err
= btf_parse_str_sec(env
);
4251 err
= btf_parse_type_sec(env
);
4255 if (log
->level
&& bpf_verifier_log_full(log
)) {
4260 btf_verifier_env_free(env
);
4261 refcount_set(&btf
->refcnt
, 1);
4265 btf_verifier_env_free(env
);
4268 return ERR_PTR(err
);
4271 extern char __weak __start_BTF
[];
4272 extern char __weak __stop_BTF
[];
4273 extern struct btf
*btf_vmlinux
;
4275 #define BPF_MAP_TYPE(_id, _ops)
4276 #define BPF_LINK_TYPE(_id, _name)
4278 struct bpf_ctx_convert
{
4279 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
4280 prog_ctx_type _id##_prog; \
4281 kern_ctx_type _id##_kern;
4282 #include <linux/bpf_types.h>
4283 #undef BPF_PROG_TYPE
4285 /* 't' is written once under lock. Read many times. */
4286 const struct btf_type
*t
;
4289 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
4291 #include <linux/bpf_types.h>
4292 #undef BPF_PROG_TYPE
4293 __ctx_convert_unused
, /* to avoid empty enum in extreme .config */
4295 static u8 bpf_ctx_convert_map
[] = {
4296 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
4297 [_id] = __ctx_convert##_id,
4298 #include <linux/bpf_types.h>
4299 #undef BPF_PROG_TYPE
4300 0, /* avoid empty array */
4303 #undef BPF_LINK_TYPE
4305 static const struct btf_member
*
4306 btf_get_prog_ctx_type(struct bpf_verifier_log
*log
, struct btf
*btf
,
4307 const struct btf_type
*t
, enum bpf_prog_type prog_type
,
4310 const struct btf_type
*conv_struct
;
4311 const struct btf_type
*ctx_struct
;
4312 const struct btf_member
*ctx_type
;
4313 const char *tname
, *ctx_tname
;
4315 conv_struct
= bpf_ctx_convert
.t
;
4317 bpf_log(log
, "btf_vmlinux is malformed\n");
4320 t
= btf_type_by_id(btf
, t
->type
);
4321 while (btf_type_is_modifier(t
))
4322 t
= btf_type_by_id(btf
, t
->type
);
4323 if (!btf_type_is_struct(t
)) {
4324 /* Only pointer to struct is supported for now.
4325 * That means that BPF_PROG_TYPE_TRACEPOINT with BTF
4326 * is not supported yet.
4327 * BPF_PROG_TYPE_RAW_TRACEPOINT is fine.
4329 if (log
->level
& BPF_LOG_LEVEL
)
4330 bpf_log(log
, "arg#%d type is not a struct\n", arg
);
4333 tname
= btf_name_by_offset(btf
, t
->name_off
);
4335 bpf_log(log
, "arg#%d struct doesn't have a name\n", arg
);
4338 /* prog_type is valid bpf program type. No need for bounds check. */
4339 ctx_type
= btf_type_member(conv_struct
) + bpf_ctx_convert_map
[prog_type
] * 2;
4340 /* ctx_struct is a pointer to prog_ctx_type in vmlinux.
4341 * Like 'struct __sk_buff'
4343 ctx_struct
= btf_type_by_id(btf_vmlinux
, ctx_type
->type
);
4345 /* should not happen */
4347 ctx_tname
= btf_name_by_offset(btf_vmlinux
, ctx_struct
->name_off
);
4349 /* should not happen */
4350 bpf_log(log
, "Please fix kernel include/linux/bpf_types.h\n");
4353 /* only compare that prog's ctx type name is the same as
4354 * kernel expects. No need to compare field by field.
4355 * It's ok for bpf prog to do:
4356 * struct __sk_buff {};
4357 * int socket_filter_bpf_prog(struct __sk_buff *skb)
4358 * { // no fields of skb are ever used }
4360 if (strcmp(ctx_tname
, tname
))
4365 static const struct bpf_map_ops
* const btf_vmlinux_map_ops
[] = {
4366 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
4367 #define BPF_LINK_TYPE(_id, _name)
4368 #define BPF_MAP_TYPE(_id, _ops) \
4370 #include <linux/bpf_types.h>
4371 #undef BPF_PROG_TYPE
4372 #undef BPF_LINK_TYPE
4376 static int btf_vmlinux_map_ids_init(const struct btf
*btf
,
4377 struct bpf_verifier_log
*log
)
4379 const struct bpf_map_ops
*ops
;
4382 for (i
= 0; i
< ARRAY_SIZE(btf_vmlinux_map_ops
); ++i
) {
4383 ops
= btf_vmlinux_map_ops
[i
];
4384 if (!ops
|| (!ops
->map_btf_name
&& !ops
->map_btf_id
))
4386 if (!ops
->map_btf_name
|| !ops
->map_btf_id
) {
4387 bpf_log(log
, "map type %d is misconfigured\n", i
);
4390 btf_id
= btf_find_by_name_kind(btf
, ops
->map_btf_name
,
4394 *ops
->map_btf_id
= btf_id
;
4400 static int btf_translate_to_vmlinux(struct bpf_verifier_log
*log
,
4402 const struct btf_type
*t
,
4403 enum bpf_prog_type prog_type
,
4406 const struct btf_member
*prog_ctx_type
, *kern_ctx_type
;
4408 prog_ctx_type
= btf_get_prog_ctx_type(log
, btf
, t
, prog_type
, arg
);
4411 kern_ctx_type
= prog_ctx_type
+ 1;
4412 return kern_ctx_type
->type
;
4415 BTF_ID_LIST(bpf_ctx_convert_btf_id
)
4416 BTF_ID(struct, bpf_ctx_convert
)
4418 struct btf
*btf_parse_vmlinux(void)
4420 struct btf_verifier_env
*env
= NULL
;
4421 struct bpf_verifier_log
*log
;
4422 struct btf
*btf
= NULL
;
4425 env
= kzalloc(sizeof(*env
), GFP_KERNEL
| __GFP_NOWARN
);
4427 return ERR_PTR(-ENOMEM
);
4430 log
->level
= BPF_LOG_KERNEL
;
4432 btf
= kzalloc(sizeof(*btf
), GFP_KERNEL
| __GFP_NOWARN
);
4439 btf
->data
= __start_BTF
;
4440 btf
->data_size
= __stop_BTF
- __start_BTF
;
4441 btf
->kernel_btf
= true;
4442 snprintf(btf
->name
, sizeof(btf
->name
), "vmlinux");
4444 err
= btf_parse_hdr(env
);
4448 btf
->nohdr_data
= btf
->data
+ btf
->hdr
.hdr_len
;
4450 err
= btf_parse_str_sec(env
);
4454 err
= btf_check_all_metas(env
);
4458 /* btf_parse_vmlinux() runs under bpf_verifier_lock */
4459 bpf_ctx_convert
.t
= btf_type_by_id(btf
, bpf_ctx_convert_btf_id
[0]);
4461 /* find bpf map structs for map_ptr access checking */
4462 err
= btf_vmlinux_map_ids_init(btf
, log
);
4466 bpf_struct_ops_init(btf
, log
);
4468 refcount_set(&btf
->refcnt
, 1);
4470 err
= btf_alloc_id(btf
);
4474 btf_verifier_env_free(env
);
4478 btf_verifier_env_free(env
);
4483 return ERR_PTR(err
);
4486 #ifdef CONFIG_DEBUG_INFO_BTF_MODULES
4488 static struct btf
*btf_parse_module(const char *module_name
, const void *data
, unsigned int data_size
)
4490 struct btf_verifier_env
*env
= NULL
;
4491 struct bpf_verifier_log
*log
;
4492 struct btf
*btf
= NULL
, *base_btf
;
4495 base_btf
= bpf_get_btf_vmlinux();
4496 if (IS_ERR(base_btf
))
4499 return ERR_PTR(-EINVAL
);
4501 env
= kzalloc(sizeof(*env
), GFP_KERNEL
| __GFP_NOWARN
);
4503 return ERR_PTR(-ENOMEM
);
4506 log
->level
= BPF_LOG_KERNEL
;
4508 btf
= kzalloc(sizeof(*btf
), GFP_KERNEL
| __GFP_NOWARN
);
4515 btf
->base_btf
= base_btf
;
4516 btf
->start_id
= base_btf
->nr_types
;
4517 btf
->start_str_off
= base_btf
->hdr
.str_len
;
4518 btf
->kernel_btf
= true;
4519 snprintf(btf
->name
, sizeof(btf
->name
), "%s", module_name
);
4521 btf
->data
= kvmalloc(data_size
, GFP_KERNEL
| __GFP_NOWARN
);
4526 memcpy(btf
->data
, data
, data_size
);
4527 btf
->data_size
= data_size
;
4529 err
= btf_parse_hdr(env
);
4533 btf
->nohdr_data
= btf
->data
+ btf
->hdr
.hdr_len
;
4535 err
= btf_parse_str_sec(env
);
4539 err
= btf_check_all_metas(env
);
4543 btf_verifier_env_free(env
);
4544 refcount_set(&btf
->refcnt
, 1);
4548 btf_verifier_env_free(env
);
4554 return ERR_PTR(err
);
4557 #endif /* CONFIG_DEBUG_INFO_BTF_MODULES */
4559 struct btf
*bpf_prog_get_target_btf(const struct bpf_prog
*prog
)
4561 struct bpf_prog
*tgt_prog
= prog
->aux
->dst_prog
;
4564 return tgt_prog
->aux
->btf
;
4566 return prog
->aux
->attach_btf
;
4569 static bool is_string_ptr(struct btf
*btf
, const struct btf_type
*t
)
4571 /* t comes in already as a pointer */
4572 t
= btf_type_by_id(btf
, t
->type
);
4575 if (BTF_INFO_KIND(t
->info
) == BTF_KIND_CONST
)
4576 t
= btf_type_by_id(btf
, t
->type
);
4578 /* char, signed char, unsigned char */
4579 return btf_type_is_int(t
) && t
->size
== 1;
4582 bool btf_ctx_access(int off
, int size
, enum bpf_access_type type
,
4583 const struct bpf_prog
*prog
,
4584 struct bpf_insn_access_aux
*info
)
4586 const struct btf_type
*t
= prog
->aux
->attach_func_proto
;
4587 struct bpf_prog
*tgt_prog
= prog
->aux
->dst_prog
;
4588 struct btf
*btf
= bpf_prog_get_target_btf(prog
);
4589 const char *tname
= prog
->aux
->attach_func_name
;
4590 struct bpf_verifier_log
*log
= info
->log
;
4591 const struct btf_param
*args
;
4596 bpf_log(log
, "func '%s' offset %d is not multiple of 8\n",
4601 args
= (const struct btf_param
*)(t
+ 1);
4602 /* if (t == NULL) Fall back to default BPF prog with 5 u64 arguments */
4603 nr_args
= t
? btf_type_vlen(t
) : 5;
4604 if (prog
->aux
->attach_btf_trace
) {
4605 /* skip first 'void *__data' argument in btf_trace_##name typedef */
4610 if (arg
> nr_args
) {
4611 bpf_log(log
, "func '%s' doesn't have %d-th argument\n",
4616 if (arg
== nr_args
) {
4617 switch (prog
->expected_attach_type
) {
4619 case BPF_TRACE_FEXIT
:
4620 /* When LSM programs are attached to void LSM hooks
4621 * they use FEXIT trampolines and when attached to
4622 * int LSM hooks, they use MODIFY_RETURN trampolines.
4624 * While the LSM programs are BPF_MODIFY_RETURN-like
4627 * if (ret_type != 'int')
4630 * is _not_ done here. This is still safe as LSM hooks
4631 * have only void and int return types.
4635 t
= btf_type_by_id(btf
, t
->type
);
4637 case BPF_MODIFY_RETURN
:
4638 /* For now the BPF_MODIFY_RETURN can only be attached to
4639 * functions that return an int.
4644 t
= btf_type_skip_modifiers(btf
, t
->type
, NULL
);
4645 if (!btf_type_is_small_int(t
)) {
4647 "ret type %s not allowed for fmod_ret\n",
4648 btf_kind_str
[BTF_INFO_KIND(t
->info
)]);
4653 bpf_log(log
, "func '%s' doesn't have %d-th argument\n",
4659 /* Default prog with 5 args */
4661 t
= btf_type_by_id(btf
, args
[arg
].type
);
4664 /* skip modifiers */
4665 while (btf_type_is_modifier(t
))
4666 t
= btf_type_by_id(btf
, t
->type
);
4667 if (btf_type_is_small_int(t
) || btf_type_is_enum(t
))
4668 /* accessing a scalar */
4670 if (!btf_type_is_ptr(t
)) {
4672 "func '%s' arg%d '%s' has type %s. Only pointer access is allowed\n",
4674 __btf_name_by_offset(btf
, t
->name_off
),
4675 btf_kind_str
[BTF_INFO_KIND(t
->info
)]);
4679 /* check for PTR_TO_RDONLY_BUF_OR_NULL or PTR_TO_RDWR_BUF_OR_NULL */
4680 for (i
= 0; i
< prog
->aux
->ctx_arg_info_size
; i
++) {
4681 const struct bpf_ctx_arg_aux
*ctx_arg_info
= &prog
->aux
->ctx_arg_info
[i
];
4683 if (ctx_arg_info
->offset
== off
&&
4684 (ctx_arg_info
->reg_type
== PTR_TO_RDONLY_BUF_OR_NULL
||
4685 ctx_arg_info
->reg_type
== PTR_TO_RDWR_BUF_OR_NULL
)) {
4686 info
->reg_type
= ctx_arg_info
->reg_type
;
4692 /* This is a pointer to void.
4693 * It is the same as scalar from the verifier safety pov.
4694 * No further pointer walking is allowed.
4698 if (is_string_ptr(btf
, t
))
4701 /* this is a pointer to another type */
4702 for (i
= 0; i
< prog
->aux
->ctx_arg_info_size
; i
++) {
4703 const struct bpf_ctx_arg_aux
*ctx_arg_info
= &prog
->aux
->ctx_arg_info
[i
];
4705 if (ctx_arg_info
->offset
== off
) {
4706 info
->reg_type
= ctx_arg_info
->reg_type
;
4707 info
->btf
= btf_vmlinux
;
4708 info
->btf_id
= ctx_arg_info
->btf_id
;
4713 info
->reg_type
= PTR_TO_BTF_ID
;
4715 enum bpf_prog_type tgt_type
;
4717 if (tgt_prog
->type
== BPF_PROG_TYPE_EXT
)
4718 tgt_type
= tgt_prog
->aux
->saved_dst_prog_type
;
4720 tgt_type
= tgt_prog
->type
;
4722 ret
= btf_translate_to_vmlinux(log
, btf
, t
, tgt_type
, arg
);
4724 info
->btf
= btf_vmlinux
;
4733 info
->btf_id
= t
->type
;
4734 t
= btf_type_by_id(btf
, t
->type
);
4735 /* skip modifiers */
4736 while (btf_type_is_modifier(t
)) {
4737 info
->btf_id
= t
->type
;
4738 t
= btf_type_by_id(btf
, t
->type
);
4740 if (!btf_type_is_struct(t
)) {
4742 "func '%s' arg%d type %s is not a struct\n",
4743 tname
, arg
, btf_kind_str
[BTF_INFO_KIND(t
->info
)]);
4746 bpf_log(log
, "func '%s' arg%d has btf_id %d type %s '%s'\n",
4747 tname
, arg
, info
->btf_id
, btf_kind_str
[BTF_INFO_KIND(t
->info
)],
4748 __btf_name_by_offset(btf
, t
->name_off
));
4752 enum bpf_struct_walk_result
{
4759 static int btf_struct_walk(struct bpf_verifier_log
*log
, const struct btf
*btf
,
4760 const struct btf_type
*t
, int off
, int size
,
4763 u32 i
, moff
, mtrue_end
, msize
= 0, total_nelems
= 0;
4764 const struct btf_type
*mtype
, *elem_type
= NULL
;
4765 const struct btf_member
*member
;
4766 const char *tname
, *mname
;
4767 u32 vlen
, elem_id
, mid
;
4770 tname
= __btf_name_by_offset(btf
, t
->name_off
);
4771 if (!btf_type_is_struct(t
)) {
4772 bpf_log(log
, "Type '%s' is not a struct\n", tname
);
4776 vlen
= btf_type_vlen(t
);
4777 if (off
+ size
> t
->size
) {
4778 /* If the last element is a variable size array, we may
4779 * need to relax the rule.
4781 struct btf_array
*array_elem
;
4786 member
= btf_type_member(t
) + vlen
- 1;
4787 mtype
= btf_type_skip_modifiers(btf
, member
->type
,
4789 if (!btf_type_is_array(mtype
))
4792 array_elem
= (struct btf_array
*)(mtype
+ 1);
4793 if (array_elem
->nelems
!= 0)
4796 moff
= btf_member_bit_offset(t
, member
) / 8;
4800 /* Only allow structure for now, can be relaxed for
4801 * other types later.
4803 t
= btf_type_skip_modifiers(btf
, array_elem
->type
,
4805 if (!btf_type_is_struct(t
))
4808 off
= (off
- moff
) % t
->size
;
4812 bpf_log(log
, "access beyond struct %s at off %u size %u\n",
4817 for_each_member(i
, t
, member
) {
4818 /* offset of the field in bytes */
4819 moff
= btf_member_bit_offset(t
, member
) / 8;
4820 if (off
+ size
<= moff
)
4821 /* won't find anything, field is already too far */
4824 if (btf_member_bitfield_size(t
, member
)) {
4825 u32 end_bit
= btf_member_bit_offset(t
, member
) +
4826 btf_member_bitfield_size(t
, member
);
4828 /* off <= moff instead of off == moff because clang
4829 * does not generate a BTF member for anonymous
4830 * bitfield like the ":16" here:
4837 BITS_ROUNDUP_BYTES(end_bit
) <= off
+ size
)
4840 /* off may be accessing a following member
4844 * Doing partial access at either end of this
4845 * bitfield. Continue on this case also to
4846 * treat it as not accessing this bitfield
4847 * and eventually error out as field not
4848 * found to keep it simple.
4849 * It could be relaxed if there was a legit
4850 * partial access case later.
4855 /* In case of "off" is pointing to holes of a struct */
4859 /* type of the field */
4861 mtype
= btf_type_by_id(btf
, member
->type
);
4862 mname
= __btf_name_by_offset(btf
, member
->name_off
);
4864 mtype
= __btf_resolve_size(btf
, mtype
, &msize
,
4865 &elem_type
, &elem_id
, &total_nelems
,
4867 if (IS_ERR(mtype
)) {
4868 bpf_log(log
, "field %s doesn't have size\n", mname
);
4872 mtrue_end
= moff
+ msize
;
4873 if (off
>= mtrue_end
)
4874 /* no overlap with member, keep iterating */
4877 if (btf_type_is_array(mtype
)) {
4880 /* __btf_resolve_size() above helps to
4881 * linearize a multi-dimensional array.
4883 * The logic here is treating an array
4884 * in a struct as the following way:
4887 * struct inner array[2][2];
4893 * struct inner array_elem0;
4894 * struct inner array_elem1;
4895 * struct inner array_elem2;
4896 * struct inner array_elem3;
4899 * When accessing outer->array[1][0], it moves
4900 * moff to "array_elem2", set mtype to
4901 * "struct inner", and msize also becomes
4902 * sizeof(struct inner). Then most of the
4903 * remaining logic will fall through without
4904 * caring the current member is an array or
4907 * Unlike mtype/msize/moff, mtrue_end does not
4908 * change. The naming difference ("_true") tells
4909 * that it is not always corresponding to
4910 * the current mtype/msize/moff.
4911 * It is the true end of the current
4912 * member (i.e. array in this case). That
4913 * will allow an int array to be accessed like
4915 * i.e. allow access beyond the size of
4916 * the array's element as long as it is
4917 * within the mtrue_end boundary.
4920 /* skip empty array */
4921 if (moff
== mtrue_end
)
4924 msize
/= total_nelems
;
4925 elem_idx
= (off
- moff
) / msize
;
4926 moff
+= elem_idx
* msize
;
4931 /* the 'off' we're looking for is either equal to start
4932 * of this field or inside of this struct
4934 if (btf_type_is_struct(mtype
)) {
4935 /* our field must be inside that union or struct */
4938 /* return if the offset matches the member offset */
4944 /* adjust offset we're looking for */
4949 if (btf_type_is_ptr(mtype
)) {
4950 const struct btf_type
*stype
;
4953 if (msize
!= size
|| off
!= moff
) {
4955 "cannot access ptr member %s with moff %u in struct %s with off %u size %u\n",
4956 mname
, moff
, tname
, off
, size
);
4959 stype
= btf_type_skip_modifiers(btf
, mtype
->type
, &id
);
4960 if (btf_type_is_struct(stype
)) {
4966 /* Allow more flexible access within an int as long as
4967 * it is within mtrue_end.
4968 * Since mtrue_end could be the end of an array,
4969 * that also allows using an array of int as a scratch
4970 * space. e.g. skb->cb[].
4972 if (off
+ size
> mtrue_end
) {
4974 "access beyond the end of member %s (mend:%u) in struct %s with off %u size %u\n",
4975 mname
, mtrue_end
, tname
, off
, size
);
4981 bpf_log(log
, "struct %s doesn't have field at offset %d\n", tname
, off
);
4985 int btf_struct_access(struct bpf_verifier_log
*log
, const struct btf
*btf
,
4986 const struct btf_type
*t
, int off
, int size
,
4987 enum bpf_access_type atype __maybe_unused
,
4994 err
= btf_struct_walk(log
, btf
, t
, off
, size
, &id
);
4998 /* If we found the pointer or scalar on t+off,
5002 return PTR_TO_BTF_ID
;
5004 return SCALAR_VALUE
;
5006 /* We found nested struct, so continue the search
5007 * by diving in it. At this point the offset is
5008 * aligned with the new type, so set it to 0.
5010 t
= btf_type_by_id(btf
, id
);
5014 /* It's either error or unknown return value..
5017 if (WARN_ONCE(err
> 0, "unknown btf_struct_walk return value"))
5026 /* Check that two BTF types, each specified as an BTF object + id, are exactly
5027 * the same. Trivial ID check is not enough due to module BTFs, because we can
5028 * end up with two different module BTFs, but IDs point to the common type in
5031 static bool btf_types_are_same(const struct btf
*btf1
, u32 id1
,
5032 const struct btf
*btf2
, u32 id2
)
5038 return btf_type_by_id(btf1
, id1
) == btf_type_by_id(btf2
, id2
);
5041 bool btf_struct_ids_match(struct bpf_verifier_log
*log
,
5042 const struct btf
*btf
, u32 id
, int off
,
5043 const struct btf
*need_btf
, u32 need_type_id
)
5045 const struct btf_type
*type
;
5048 /* Are we already done? */
5049 if (off
== 0 && btf_types_are_same(btf
, id
, need_btf
, need_type_id
))
5053 type
= btf_type_by_id(btf
, id
);
5056 err
= btf_struct_walk(log
, btf
, type
, off
, 1, &id
);
5057 if (err
!= WALK_STRUCT
)
5060 /* We found nested struct object. If it matches
5061 * the requested ID, we're done. Otherwise let's
5062 * continue the search with offset 0 in the new
5065 if (!btf_types_are_same(btf
, id
, need_btf
, need_type_id
)) {
5073 static int __get_type_size(struct btf
*btf
, u32 btf_id
,
5074 const struct btf_type
**bad_type
)
5076 const struct btf_type
*t
;
5081 t
= btf_type_by_id(btf
, btf_id
);
5082 while (t
&& btf_type_is_modifier(t
))
5083 t
= btf_type_by_id(btf
, t
->type
);
5085 *bad_type
= btf_type_by_id(btf
, 0);
5088 if (btf_type_is_ptr(t
))
5089 /* kernel size of pointer. Not BPF's size of pointer*/
5090 return sizeof(void *);
5091 if (btf_type_is_int(t
) || btf_type_is_enum(t
))
5097 int btf_distill_func_proto(struct bpf_verifier_log
*log
,
5099 const struct btf_type
*func
,
5101 struct btf_func_model
*m
)
5103 const struct btf_param
*args
;
5104 const struct btf_type
*t
;
5109 /* BTF function prototype doesn't match the verifier types.
5110 * Fall back to 5 u64 args.
5112 for (i
= 0; i
< 5; i
++)
5118 args
= (const struct btf_param
*)(func
+ 1);
5119 nargs
= btf_type_vlen(func
);
5120 if (nargs
>= MAX_BPF_FUNC_ARGS
) {
5122 "The function %s has %d arguments. Too many.\n",
5126 ret
= __get_type_size(btf
, func
->type
, &t
);
5129 "The function %s return type %s is unsupported.\n",
5130 tname
, btf_kind_str
[BTF_INFO_KIND(t
->info
)]);
5135 for (i
= 0; i
< nargs
; i
++) {
5136 ret
= __get_type_size(btf
, args
[i
].type
, &t
);
5139 "The function %s arg%d type %s is unsupported.\n",
5140 tname
, i
, btf_kind_str
[BTF_INFO_KIND(t
->info
)]);
5143 m
->arg_size
[i
] = ret
;
5149 /* Compare BTFs of two functions assuming only scalars and pointers to context.
5150 * t1 points to BTF_KIND_FUNC in btf1
5151 * t2 points to BTF_KIND_FUNC in btf2
5153 * EINVAL - function prototype mismatch
5154 * EFAULT - verifier bug
5155 * 0 - 99% match. The last 1% is validated by the verifier.
5157 static int btf_check_func_type_match(struct bpf_verifier_log
*log
,
5158 struct btf
*btf1
, const struct btf_type
*t1
,
5159 struct btf
*btf2
, const struct btf_type
*t2
)
5161 const struct btf_param
*args1
, *args2
;
5162 const char *fn1
, *fn2
, *s1
, *s2
;
5163 u32 nargs1
, nargs2
, i
;
5165 fn1
= btf_name_by_offset(btf1
, t1
->name_off
);
5166 fn2
= btf_name_by_offset(btf2
, t2
->name_off
);
5168 if (btf_func_linkage(t1
) != BTF_FUNC_GLOBAL
) {
5169 bpf_log(log
, "%s() is not a global function\n", fn1
);
5172 if (btf_func_linkage(t2
) != BTF_FUNC_GLOBAL
) {
5173 bpf_log(log
, "%s() is not a global function\n", fn2
);
5177 t1
= btf_type_by_id(btf1
, t1
->type
);
5178 if (!t1
|| !btf_type_is_func_proto(t1
))
5180 t2
= btf_type_by_id(btf2
, t2
->type
);
5181 if (!t2
|| !btf_type_is_func_proto(t2
))
5184 args1
= (const struct btf_param
*)(t1
+ 1);
5185 nargs1
= btf_type_vlen(t1
);
5186 args2
= (const struct btf_param
*)(t2
+ 1);
5187 nargs2
= btf_type_vlen(t2
);
5189 if (nargs1
!= nargs2
) {
5190 bpf_log(log
, "%s() has %d args while %s() has %d args\n",
5191 fn1
, nargs1
, fn2
, nargs2
);
5195 t1
= btf_type_skip_modifiers(btf1
, t1
->type
, NULL
);
5196 t2
= btf_type_skip_modifiers(btf2
, t2
->type
, NULL
);
5197 if (t1
->info
!= t2
->info
) {
5199 "Return type %s of %s() doesn't match type %s of %s()\n",
5200 btf_type_str(t1
), fn1
,
5201 btf_type_str(t2
), fn2
);
5205 for (i
= 0; i
< nargs1
; i
++) {
5206 t1
= btf_type_skip_modifiers(btf1
, args1
[i
].type
, NULL
);
5207 t2
= btf_type_skip_modifiers(btf2
, args2
[i
].type
, NULL
);
5209 if (t1
->info
!= t2
->info
) {
5210 bpf_log(log
, "arg%d in %s() is %s while %s() has %s\n",
5211 i
, fn1
, btf_type_str(t1
),
5212 fn2
, btf_type_str(t2
));
5215 if (btf_type_has_size(t1
) && t1
->size
!= t2
->size
) {
5217 "arg%d in %s() has size %d while %s() has %d\n",
5223 /* global functions are validated with scalars and pointers
5224 * to context only. And only global functions can be replaced.
5225 * Hence type check only those types.
5227 if (btf_type_is_int(t1
) || btf_type_is_enum(t1
))
5229 if (!btf_type_is_ptr(t1
)) {
5231 "arg%d in %s() has unrecognized type\n",
5235 t1
= btf_type_skip_modifiers(btf1
, t1
->type
, NULL
);
5236 t2
= btf_type_skip_modifiers(btf2
, t2
->type
, NULL
);
5237 if (!btf_type_is_struct(t1
)) {
5239 "arg%d in %s() is not a pointer to context\n",
5243 if (!btf_type_is_struct(t2
)) {
5245 "arg%d in %s() is not a pointer to context\n",
5249 /* This is an optional check to make program writing easier.
5250 * Compare names of structs and report an error to the user.
5251 * btf_prepare_func_args() already checked that t2 struct
5252 * is a context type. btf_prepare_func_args() will check
5253 * later that t1 struct is a context type as well.
5255 s1
= btf_name_by_offset(btf1
, t1
->name_off
);
5256 s2
= btf_name_by_offset(btf2
, t2
->name_off
);
5257 if (strcmp(s1
, s2
)) {
5259 "arg%d %s(struct %s *) doesn't match %s(struct %s *)\n",
5260 i
, fn1
, s1
, fn2
, s2
);
5267 /* Compare BTFs of given program with BTF of target program */
5268 int btf_check_type_match(struct bpf_verifier_log
*log
, const struct bpf_prog
*prog
,
5269 struct btf
*btf2
, const struct btf_type
*t2
)
5271 struct btf
*btf1
= prog
->aux
->btf
;
5272 const struct btf_type
*t1
;
5275 if (!prog
->aux
->func_info
) {
5276 bpf_log(log
, "Program extension requires BTF\n");
5280 btf_id
= prog
->aux
->func_info
[0].type_id
;
5284 t1
= btf_type_by_id(btf1
, btf_id
);
5285 if (!t1
|| !btf_type_is_func(t1
))
5288 return btf_check_func_type_match(log
, btf1
, t1
, btf2
, t2
);
5291 /* Compare BTF of a function with given bpf_reg_state.
5293 * EFAULT - there is a verifier bug. Abort verification.
5294 * EINVAL - there is a type mismatch or BTF is not available.
5295 * 0 - BTF matches with what bpf_reg_state expects.
5296 * Only PTR_TO_CTX and SCALAR_VALUE states are recognized.
5298 int btf_check_func_arg_match(struct bpf_verifier_env
*env
, int subprog
,
5299 struct bpf_reg_state
*reg
)
5301 struct bpf_verifier_log
*log
= &env
->log
;
5302 struct bpf_prog
*prog
= env
->prog
;
5303 struct btf
*btf
= prog
->aux
->btf
;
5304 const struct btf_param
*args
;
5305 const struct btf_type
*t
;
5306 u32 i
, nargs
, btf_id
;
5309 if (!prog
->aux
->func_info
)
5312 btf_id
= prog
->aux
->func_info
[subprog
].type_id
;
5316 if (prog
->aux
->func_info_aux
[subprog
].unreliable
)
5319 t
= btf_type_by_id(btf
, btf_id
);
5320 if (!t
|| !btf_type_is_func(t
)) {
5321 /* These checks were already done by the verifier while loading
5322 * struct bpf_func_info
5324 bpf_log(log
, "BTF of func#%d doesn't point to KIND_FUNC\n",
5328 tname
= btf_name_by_offset(btf
, t
->name_off
);
5330 t
= btf_type_by_id(btf
, t
->type
);
5331 if (!t
|| !btf_type_is_func_proto(t
)) {
5332 bpf_log(log
, "Invalid BTF of func %s\n", tname
);
5335 args
= (const struct btf_param
*)(t
+ 1);
5336 nargs
= btf_type_vlen(t
);
5338 bpf_log(log
, "Function %s has %d > 5 args\n", tname
, nargs
);
5341 /* check that BTF function arguments match actual types that the
5344 for (i
= 0; i
< nargs
; i
++) {
5345 t
= btf_type_by_id(btf
, args
[i
].type
);
5346 while (btf_type_is_modifier(t
))
5347 t
= btf_type_by_id(btf
, t
->type
);
5348 if (btf_type_is_int(t
) || btf_type_is_enum(t
)) {
5349 if (reg
[i
+ 1].type
== SCALAR_VALUE
)
5351 bpf_log(log
, "R%d is not a scalar\n", i
+ 1);
5354 if (btf_type_is_ptr(t
)) {
5355 if (reg
[i
+ 1].type
== SCALAR_VALUE
) {
5356 bpf_log(log
, "R%d is not a pointer\n", i
+ 1);
5359 /* If function expects ctx type in BTF check that caller
5360 * is passing PTR_TO_CTX.
5362 if (btf_get_prog_ctx_type(log
, btf
, t
, prog
->type
, i
)) {
5363 if (reg
[i
+ 1].type
!= PTR_TO_CTX
) {
5365 "arg#%d expected pointer to ctx, but got %s\n",
5366 i
, btf_kind_str
[BTF_INFO_KIND(t
->info
)]);
5369 if (check_ctx_reg(env
, ®
[i
+ 1], i
+ 1))
5374 bpf_log(log
, "Unrecognized arg#%d type %s\n",
5375 i
, btf_kind_str
[BTF_INFO_KIND(t
->info
)]);
5380 /* Compiler optimizations can remove arguments from static functions
5381 * or mismatched type can be passed into a global function.
5382 * In such cases mark the function as unreliable from BTF point of view.
5384 prog
->aux
->func_info_aux
[subprog
].unreliable
= true;
5388 /* Convert BTF of a function into bpf_reg_state if possible
5390 * EFAULT - there is a verifier bug. Abort verification.
5391 * EINVAL - cannot convert BTF.
5392 * 0 - Successfully converted BTF into bpf_reg_state
5393 * (either PTR_TO_CTX or SCALAR_VALUE).
5395 int btf_prepare_func_args(struct bpf_verifier_env
*env
, int subprog
,
5396 struct bpf_reg_state
*reg
)
5398 struct bpf_verifier_log
*log
= &env
->log
;
5399 struct bpf_prog
*prog
= env
->prog
;
5400 enum bpf_prog_type prog_type
= prog
->type
;
5401 struct btf
*btf
= prog
->aux
->btf
;
5402 const struct btf_param
*args
;
5403 const struct btf_type
*t
;
5404 u32 i
, nargs
, btf_id
;
5407 if (!prog
->aux
->func_info
||
5408 prog
->aux
->func_info_aux
[subprog
].linkage
!= BTF_FUNC_GLOBAL
) {
5409 bpf_log(log
, "Verifier bug\n");
5413 btf_id
= prog
->aux
->func_info
[subprog
].type_id
;
5415 bpf_log(log
, "Global functions need valid BTF\n");
5419 t
= btf_type_by_id(btf
, btf_id
);
5420 if (!t
|| !btf_type_is_func(t
)) {
5421 /* These checks were already done by the verifier while loading
5422 * struct bpf_func_info
5424 bpf_log(log
, "BTF of func#%d doesn't point to KIND_FUNC\n",
5428 tname
= btf_name_by_offset(btf
, t
->name_off
);
5430 if (log
->level
& BPF_LOG_LEVEL
)
5431 bpf_log(log
, "Validating %s() func#%d...\n",
5434 if (prog
->aux
->func_info_aux
[subprog
].unreliable
) {
5435 bpf_log(log
, "Verifier bug in function %s()\n", tname
);
5438 if (prog_type
== BPF_PROG_TYPE_EXT
)
5439 prog_type
= prog
->aux
->dst_prog
->type
;
5441 t
= btf_type_by_id(btf
, t
->type
);
5442 if (!t
|| !btf_type_is_func_proto(t
)) {
5443 bpf_log(log
, "Invalid type of function %s()\n", tname
);
5446 args
= (const struct btf_param
*)(t
+ 1);
5447 nargs
= btf_type_vlen(t
);
5449 bpf_log(log
, "Global function %s() with %d > 5 args. Buggy compiler.\n",
5453 /* check that function returns int */
5454 t
= btf_type_by_id(btf
, t
->type
);
5455 while (btf_type_is_modifier(t
))
5456 t
= btf_type_by_id(btf
, t
->type
);
5457 if (!btf_type_is_int(t
) && !btf_type_is_enum(t
)) {
5459 "Global function %s() doesn't return scalar. Only those are supported.\n",
5463 /* Convert BTF function arguments into verifier types.
5464 * Only PTR_TO_CTX and SCALAR are supported atm.
5466 for (i
= 0; i
< nargs
; i
++) {
5467 t
= btf_type_by_id(btf
, args
[i
].type
);
5468 while (btf_type_is_modifier(t
))
5469 t
= btf_type_by_id(btf
, t
->type
);
5470 if (btf_type_is_int(t
) || btf_type_is_enum(t
)) {
5471 reg
[i
+ 1].type
= SCALAR_VALUE
;
5474 if (btf_type_is_ptr(t
) &&
5475 btf_get_prog_ctx_type(log
, btf
, t
, prog_type
, i
)) {
5476 reg
[i
+ 1].type
= PTR_TO_CTX
;
5479 bpf_log(log
, "Arg#%d type %s in %s() is not supported yet.\n",
5480 i
, btf_kind_str
[BTF_INFO_KIND(t
->info
)], tname
);
5486 static void btf_type_show(const struct btf
*btf
, u32 type_id
, void *obj
,
5487 struct btf_show
*show
)
5489 const struct btf_type
*t
= btf_type_by_id(btf
, type_id
);
5492 memset(&show
->state
, 0, sizeof(show
->state
));
5493 memset(&show
->obj
, 0, sizeof(show
->obj
));
5495 btf_type_ops(t
)->show(btf
, t
, type_id
, obj
, 0, show
);
5498 static void btf_seq_show(struct btf_show
*show
, const char *fmt
,
5501 seq_vprintf((struct seq_file
*)show
->target
, fmt
, args
);
5504 int btf_type_seq_show_flags(const struct btf
*btf
, u32 type_id
,
5505 void *obj
, struct seq_file
*m
, u64 flags
)
5507 struct btf_show sseq
;
5510 sseq
.showfn
= btf_seq_show
;
5513 btf_type_show(btf
, type_id
, obj
, &sseq
);
5515 return sseq
.state
.status
;
5518 void btf_type_seq_show(const struct btf
*btf
, u32 type_id
, void *obj
,
5521 (void) btf_type_seq_show_flags(btf
, type_id
, obj
, m
,
5522 BTF_SHOW_NONAME
| BTF_SHOW_COMPACT
|
5523 BTF_SHOW_ZERO
| BTF_SHOW_UNSAFE
);
5526 struct btf_show_snprintf
{
5527 struct btf_show show
;
5528 int len_left
; /* space left in string */
5529 int len
; /* length we would have written */
5532 static void btf_snprintf_show(struct btf_show
*show
, const char *fmt
,
5535 struct btf_show_snprintf
*ssnprintf
= (struct btf_show_snprintf
*)show
;
5538 len
= vsnprintf(show
->target
, ssnprintf
->len_left
, fmt
, args
);
5541 ssnprintf
->len_left
= 0;
5542 ssnprintf
->len
= len
;
5543 } else if (len
> ssnprintf
->len_left
) {
5544 /* no space, drive on to get length we would have written */
5545 ssnprintf
->len_left
= 0;
5546 ssnprintf
->len
+= len
;
5548 ssnprintf
->len_left
-= len
;
5549 ssnprintf
->len
+= len
;
5550 show
->target
+= len
;
5554 int btf_type_snprintf_show(const struct btf
*btf
, u32 type_id
, void *obj
,
5555 char *buf
, int len
, u64 flags
)
5557 struct btf_show_snprintf ssnprintf
;
5559 ssnprintf
.show
.target
= buf
;
5560 ssnprintf
.show
.flags
= flags
;
5561 ssnprintf
.show
.showfn
= btf_snprintf_show
;
5562 ssnprintf
.len_left
= len
;
5565 btf_type_show(btf
, type_id
, obj
, (struct btf_show
*)&ssnprintf
);
5567 /* If we encontered an error, return it. */
5568 if (ssnprintf
.show
.state
.status
)
5569 return ssnprintf
.show
.state
.status
;
5571 /* Otherwise return length we would have written */
5572 return ssnprintf
.len
;
5575 #ifdef CONFIG_PROC_FS
5576 static void bpf_btf_show_fdinfo(struct seq_file
*m
, struct file
*filp
)
5578 const struct btf
*btf
= filp
->private_data
;
5580 seq_printf(m
, "btf_id:\t%u\n", btf
->id
);
5584 static int btf_release(struct inode
*inode
, struct file
*filp
)
5586 btf_put(filp
->private_data
);
5590 const struct file_operations btf_fops
= {
5591 #ifdef CONFIG_PROC_FS
5592 .show_fdinfo
= bpf_btf_show_fdinfo
,
5594 .release
= btf_release
,
5597 static int __btf_new_fd(struct btf
*btf
)
5599 return anon_inode_getfd("btf", &btf_fops
, btf
, O_RDONLY
| O_CLOEXEC
);
5602 int btf_new_fd(const union bpf_attr
*attr
)
5607 btf
= btf_parse(u64_to_user_ptr(attr
->btf
),
5608 attr
->btf_size
, attr
->btf_log_level
,
5609 u64_to_user_ptr(attr
->btf_log_buf
),
5610 attr
->btf_log_size
);
5612 return PTR_ERR(btf
);
5614 ret
= btf_alloc_id(btf
);
5621 * The BTF ID is published to the userspace.
5622 * All BTF free must go through call_rcu() from
5623 * now on (i.e. free by calling btf_put()).
5626 ret
= __btf_new_fd(btf
);
5633 struct btf
*btf_get_by_fd(int fd
)
5641 return ERR_PTR(-EBADF
);
5643 if (f
.file
->f_op
!= &btf_fops
) {
5645 return ERR_PTR(-EINVAL
);
5648 btf
= f
.file
->private_data
;
5649 refcount_inc(&btf
->refcnt
);
5655 int btf_get_info_by_fd(const struct btf
*btf
,
5656 const union bpf_attr
*attr
,
5657 union bpf_attr __user
*uattr
)
5659 struct bpf_btf_info __user
*uinfo
;
5660 struct bpf_btf_info info
;
5661 u32 info_copy
, btf_copy
;
5664 u32 uinfo_len
, uname_len
, name_len
;
5667 uinfo
= u64_to_user_ptr(attr
->info
.info
);
5668 uinfo_len
= attr
->info
.info_len
;
5670 info_copy
= min_t(u32
, uinfo_len
, sizeof(info
));
5671 memset(&info
, 0, sizeof(info
));
5672 if (copy_from_user(&info
, uinfo
, info_copy
))
5676 ubtf
= u64_to_user_ptr(info
.btf
);
5677 btf_copy
= min_t(u32
, btf
->data_size
, info
.btf_size
);
5678 if (copy_to_user(ubtf
, btf
->data
, btf_copy
))
5680 info
.btf_size
= btf
->data_size
;
5682 info
.kernel_btf
= btf
->kernel_btf
;
5684 uname
= u64_to_user_ptr(info
.name
);
5685 uname_len
= info
.name_len
;
5686 if (!uname
^ !uname_len
)
5689 name_len
= strlen(btf
->name
);
5690 info
.name_len
= name_len
;
5693 if (uname_len
>= name_len
+ 1) {
5694 if (copy_to_user(uname
, btf
->name
, name_len
+ 1))
5699 if (copy_to_user(uname
, btf
->name
, uname_len
- 1))
5701 if (put_user(zero
, uname
+ uname_len
- 1))
5703 /* let user-space know about too short buffer */
5708 if (copy_to_user(uinfo
, &info
, info_copy
) ||
5709 put_user(info_copy
, &uattr
->info
.info_len
))
5715 int btf_get_fd_by_id(u32 id
)
5721 btf
= idr_find(&btf_idr
, id
);
5722 if (!btf
|| !refcount_inc_not_zero(&btf
->refcnt
))
5723 btf
= ERR_PTR(-ENOENT
);
5727 return PTR_ERR(btf
);
5729 fd
= __btf_new_fd(btf
);
5736 u32
btf_obj_id(const struct btf
*btf
)
5741 bool btf_is_kernel(const struct btf
*btf
)
5743 return btf
->kernel_btf
;
5746 static int btf_id_cmp_func(const void *a
, const void *b
)
5748 const int *pa
= a
, *pb
= b
;
5753 bool btf_id_set_contains(const struct btf_id_set
*set
, u32 id
)
5755 return bsearch(&id
, set
->ids
, set
->cnt
, sizeof(u32
), btf_id_cmp_func
) != NULL
;
5758 #ifdef CONFIG_DEBUG_INFO_BTF_MODULES
5760 struct list_head list
;
5761 struct module
*module
;
5763 struct bin_attribute
*sysfs_attr
;
5766 static LIST_HEAD(btf_modules
);
5767 static DEFINE_MUTEX(btf_module_mutex
);
5770 btf_module_read(struct file
*file
, struct kobject
*kobj
,
5771 struct bin_attribute
*bin_attr
,
5772 char *buf
, loff_t off
, size_t len
)
5774 const struct btf
*btf
= bin_attr
->private;
5776 memcpy(buf
, btf
->data
+ off
, len
);
5780 static int btf_module_notify(struct notifier_block
*nb
, unsigned long op
,
5783 struct btf_module
*btf_mod
, *tmp
;
5784 struct module
*mod
= module
;
5788 if (mod
->btf_data_size
== 0 ||
5789 (op
!= MODULE_STATE_COMING
&& op
!= MODULE_STATE_GOING
))
5793 case MODULE_STATE_COMING
:
5794 btf_mod
= kzalloc(sizeof(*btf_mod
), GFP_KERNEL
);
5799 btf
= btf_parse_module(mod
->name
, mod
->btf_data
, mod
->btf_data_size
);
5801 pr_warn("failed to validate module [%s] BTF: %ld\n",
5802 mod
->name
, PTR_ERR(btf
));
5807 err
= btf_alloc_id(btf
);
5814 mutex_lock(&btf_module_mutex
);
5815 btf_mod
->module
= module
;
5817 list_add(&btf_mod
->list
, &btf_modules
);
5818 mutex_unlock(&btf_module_mutex
);
5820 if (IS_ENABLED(CONFIG_SYSFS
)) {
5821 struct bin_attribute
*attr
;
5823 attr
= kzalloc(sizeof(*attr
), GFP_KERNEL
);
5827 sysfs_bin_attr_init(attr
);
5828 attr
->attr
.name
= btf
->name
;
5829 attr
->attr
.mode
= 0444;
5830 attr
->size
= btf
->data_size
;
5831 attr
->private = btf
;
5832 attr
->read
= btf_module_read
;
5834 err
= sysfs_create_bin_file(btf_kobj
, attr
);
5836 pr_warn("failed to register module [%s] BTF in sysfs: %d\n",
5843 btf_mod
->sysfs_attr
= attr
;
5847 case MODULE_STATE_GOING
:
5848 mutex_lock(&btf_module_mutex
);
5849 list_for_each_entry_safe(btf_mod
, tmp
, &btf_modules
, list
) {
5850 if (btf_mod
->module
!= module
)
5853 list_del(&btf_mod
->list
);
5854 if (btf_mod
->sysfs_attr
)
5855 sysfs_remove_bin_file(btf_kobj
, btf_mod
->sysfs_attr
);
5856 btf_put(btf_mod
->btf
);
5857 kfree(btf_mod
->sysfs_attr
);
5861 mutex_unlock(&btf_module_mutex
);
5865 return notifier_from_errno(err
);
5868 static struct notifier_block btf_module_nb
= {
5869 .notifier_call
= btf_module_notify
,
5872 static int __init
btf_module_init(void)
5874 register_module_notifier(&btf_module_nb
);
5878 fs_initcall(btf_module_init
);
5879 #endif /* CONFIG_DEBUG_INFO_BTF_MODULES */