1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (c) 2018 Facebook */
4 #include <uapi/linux/btf.h>
5 #include <uapi/linux/types.h>
6 #include <linux/seq_file.h>
7 #include <linux/compiler.h>
8 #include <linux/ctype.h>
9 #include <linux/errno.h>
10 #include <linux/slab.h>
11 #include <linux/anon_inodes.h>
12 #include <linux/file.h>
13 #include <linux/uaccess.h>
14 #include <linux/kernel.h>
15 #include <linux/idr.h>
16 #include <linux/sort.h>
17 #include <linux/bpf_verifier.h>
18 #include <linux/btf.h>
20 /* BTF (BPF Type Format) is the meta data format which describes
21 * the data types of BPF program/map. Hence, it basically focus
22 * on the C programming language which the modern BPF is primary
27 * The BTF data is stored under the ".BTF" ELF section
31 * Each 'struct btf_type' object describes a C data type.
32 * Depending on the type it is describing, a 'struct btf_type'
33 * object may be followed by more data. F.e.
34 * To describe an array, 'struct btf_type' is followed by
37 * 'struct btf_type' and any extra data following it are
42 * The BTF type section contains a list of 'struct btf_type' objects.
43 * Each one describes a C type. Recall from the above section
44 * that a 'struct btf_type' object could be immediately followed by extra
45 * data in order to desribe some particular C types.
49 * Each btf_type object is identified by a type_id. The type_id
50 * is implicitly implied by the location of the btf_type object in
51 * the BTF type section. The first one has type_id 1. The second
52 * one has type_id 2...etc. Hence, an earlier btf_type has
55 * A btf_type object may refer to another btf_type object by using
56 * type_id (i.e. the "type" in the "struct btf_type").
58 * NOTE that we cannot assume any reference-order.
59 * A btf_type object can refer to an earlier btf_type object
60 * but it can also refer to a later btf_type object.
62 * For example, to describe "const void *". A btf_type
63 * object describing "const" may refer to another btf_type
64 * object describing "void *". This type-reference is done
65 * by specifying type_id:
67 * [1] CONST (anon) type_id=2
68 * [2] PTR (anon) type_id=0
70 * The above is the btf_verifier debug log:
71 * - Each line started with "[?]" is a btf_type object
72 * - [?] is the type_id of the btf_type object.
73 * - CONST/PTR is the BTF_KIND_XXX
74 * - "(anon)" is the name of the type. It just
75 * happens that CONST and PTR has no name.
76 * - type_id=XXX is the 'u32 type' in btf_type
78 * NOTE: "void" has type_id 0
82 * The BTF string section contains the names used by the type section.
83 * Each string is referred by an "offset" from the beginning of the
86 * Each string is '\0' terminated.
88 * The first character in the string section must be '\0'
89 * which is used to mean 'anonymous'. Some btf_type may not
95 * To verify BTF data, two passes are needed.
99 * The first pass is to collect all btf_type objects to
100 * an array: "btf->types".
102 * Depending on the C type that a btf_type is describing,
103 * a btf_type may be followed by extra data. We don't know
104 * how many btf_type is there, and more importantly we don't
105 * know where each btf_type is located in the type section.
107 * Without knowing the location of each type_id, most verifications
108 * cannot be done. e.g. an earlier btf_type may refer to a later
109 * btf_type (recall the "const void *" above), so we cannot
110 * check this type-reference in the first pass.
112 * In the first pass, it still does some verifications (e.g.
113 * checking the name is a valid offset to the string section).
117 * The main focus is to resolve a btf_type that is referring
120 * We have to ensure the referring type:
121 * 1) does exist in the BTF (i.e. in btf->types[])
122 * 2) does not cause a loop:
131 * btf_type_needs_resolve() decides if a btf_type needs
134 * The needs_resolve type implements the "resolve()" ops which
135 * essentially does a DFS and detects backedge.
137 * During resolve (or DFS), different C types have different
138 * "RESOLVED" conditions.
140 * When resolving a BTF_KIND_STRUCT, we need to resolve all its
141 * members because a member is always referring to another
142 * type. A struct's member can be treated as "RESOLVED" if
143 * it is referring to a BTF_KIND_PTR. Otherwise, the
144 * following valid C struct would be rejected:
151 * When resolving a BTF_KIND_PTR, it needs to keep resolving if
152 * it is referring to another BTF_KIND_PTR. Otherwise, we cannot
153 * detect a pointer loop, e.g.:
154 * BTF_KIND_CONST -> BTF_KIND_PTR -> BTF_KIND_CONST -> BTF_KIND_PTR +
156 * +-----------------------------------------+
160 #define BITS_PER_U128 (sizeof(u64) * BITS_PER_BYTE * 2)
161 #define BITS_PER_BYTE_MASK (BITS_PER_BYTE - 1)
162 #define BITS_PER_BYTE_MASKED(bits) ((bits) & BITS_PER_BYTE_MASK)
163 #define BITS_ROUNDDOWN_BYTES(bits) ((bits) >> 3)
164 #define BITS_ROUNDUP_BYTES(bits) \
165 (BITS_ROUNDDOWN_BYTES(bits) + !!BITS_PER_BYTE_MASKED(bits))
167 #define BTF_INFO_MASK 0x8f00ffff
168 #define BTF_INT_MASK 0x0fffffff
169 #define BTF_TYPE_ID_VALID(type_id) ((type_id) <= BTF_MAX_TYPE)
170 #define BTF_STR_OFFSET_VALID(name_off) ((name_off) <= BTF_MAX_NAME_OFFSET)
172 /* 16MB for 64k structs and each has 16 members and
173 * a few MB spaces for the string section.
174 * The hard limit is S32_MAX.
176 #define BTF_MAX_SIZE (16 * 1024 * 1024)
178 #define for_each_member(i, struct_type, member) \
179 for (i = 0, member = btf_type_member(struct_type); \
180 i < btf_type_vlen(struct_type); \
183 #define for_each_member_from(i, from, struct_type, member) \
184 for (i = from, member = btf_type_member(struct_type) + from; \
185 i < btf_type_vlen(struct_type); \
188 static DEFINE_IDR(btf_idr);
189 static DEFINE_SPINLOCK(btf_idr_lock);
193 struct btf_type **types;
198 struct btf_header hdr;
207 enum verifier_phase {
212 struct resolve_vertex {
213 const struct btf_type *t;
225 RESOLVE_TBD, /* To Be Determined */
226 RESOLVE_PTR, /* Resolving for Pointer */
227 RESOLVE_STRUCT_OR_ARRAY, /* Resolving for struct/union
232 #define MAX_RESOLVE_DEPTH 32
234 struct btf_sec_info {
239 struct btf_verifier_env {
242 struct resolve_vertex stack[MAX_RESOLVE_DEPTH];
243 struct bpf_verifier_log log;
246 enum verifier_phase phase;
247 enum resolve_mode resolve_mode;
250 static const char * const btf_kind_str[NR_BTF_KINDS] = {
251 [BTF_KIND_UNKN] = "UNKNOWN",
252 [BTF_KIND_INT] = "INT",
253 [BTF_KIND_PTR] = "PTR",
254 [BTF_KIND_ARRAY] = "ARRAY",
255 [BTF_KIND_STRUCT] = "STRUCT",
256 [BTF_KIND_UNION] = "UNION",
257 [BTF_KIND_ENUM] = "ENUM",
258 [BTF_KIND_FWD] = "FWD",
259 [BTF_KIND_TYPEDEF] = "TYPEDEF",
260 [BTF_KIND_VOLATILE] = "VOLATILE",
261 [BTF_KIND_CONST] = "CONST",
262 [BTF_KIND_RESTRICT] = "RESTRICT",
263 [BTF_KIND_FUNC] = "FUNC",
264 [BTF_KIND_FUNC_PROTO] = "FUNC_PROTO",
267 struct btf_kind_operations {
268 s32 (*check_meta)(struct btf_verifier_env *env,
269 const struct btf_type *t,
271 int (*resolve)(struct btf_verifier_env *env,
272 const struct resolve_vertex *v);
273 int (*check_member)(struct btf_verifier_env *env,
274 const struct btf_type *struct_type,
275 const struct btf_member *member,
276 const struct btf_type *member_type);
277 int (*check_kflag_member)(struct btf_verifier_env *env,
278 const struct btf_type *struct_type,
279 const struct btf_member *member,
280 const struct btf_type *member_type);
281 void (*log_details)(struct btf_verifier_env *env,
282 const struct btf_type *t);
283 void (*seq_show)(const struct btf *btf, const struct btf_type *t,
284 u32 type_id, void *data, u8 bits_offsets,
288 static const struct btf_kind_operations * const kind_ops[NR_BTF_KINDS];
289 static struct btf_type btf_void;
291 static int btf_resolve(struct btf_verifier_env *env,
292 const struct btf_type *t, u32 type_id);
294 static bool btf_type_is_modifier(const struct btf_type *t)
296 /* Some of them is not strictly a C modifier
297 * but they are grouped into the same bucket
299 * A type (t) that refers to another
300 * type through t->type AND its size cannot
301 * be determined without following the t->type.
303 * ptr does not fall into this bucket
304 * because its size is always sizeof(void *).
306 switch (BTF_INFO_KIND(t->info)) {
307 case BTF_KIND_TYPEDEF:
308 case BTF_KIND_VOLATILE:
310 case BTF_KIND_RESTRICT:
317 static bool btf_type_is_void(const struct btf_type *t)
319 return t == &btf_void;
322 static bool btf_type_is_fwd(const struct btf_type *t)
324 return BTF_INFO_KIND(t->info) == BTF_KIND_FWD;
327 static bool btf_type_is_func(const struct btf_type *t)
329 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC;
332 static bool btf_type_is_func_proto(const struct btf_type *t)
334 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC_PROTO;
337 static bool btf_type_nosize(const struct btf_type *t)
339 return btf_type_is_void(t) || btf_type_is_fwd(t) ||
340 btf_type_is_func(t) || btf_type_is_func_proto(t);
343 static bool btf_type_nosize_or_null(const struct btf_type *t)
345 return !t || btf_type_nosize(t);
348 /* union is only a special case of struct:
349 * all its offsetof(member) == 0
351 static bool btf_type_is_struct(const struct btf_type *t)
353 u8 kind = BTF_INFO_KIND(t->info);
355 return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
358 static bool __btf_type_is_struct(const struct btf_type *t)
360 return BTF_INFO_KIND(t->info) == BTF_KIND_STRUCT;
363 static bool btf_type_is_array(const struct btf_type *t)
365 return BTF_INFO_KIND(t->info) == BTF_KIND_ARRAY;
368 static bool btf_type_is_ptr(const struct btf_type *t)
370 return BTF_INFO_KIND(t->info) == BTF_KIND_PTR;
373 static bool btf_type_is_int(const struct btf_type *t)
375 return BTF_INFO_KIND(t->info) == BTF_KIND_INT;
378 /* What types need to be resolved?
380 * btf_type_is_modifier() is an obvious one.
382 * btf_type_is_struct() because its member refers to
383 * another type (through member->type).
385 * btf_type_is_array() because its element (array->type)
386 * refers to another type. Array can be thought of a
387 * special case of struct while array just has the same
388 * member-type repeated by array->nelems of times.
390 static bool btf_type_needs_resolve(const struct btf_type *t)
392 return btf_type_is_modifier(t) ||
393 btf_type_is_ptr(t) ||
394 btf_type_is_struct(t) ||
395 btf_type_is_array(t);
398 /* t->size can be used */
399 static bool btf_type_has_size(const struct btf_type *t)
401 switch (BTF_INFO_KIND(t->info)) {
403 case BTF_KIND_STRUCT:
412 static const char *btf_int_encoding_str(u8 encoding)
416 else if (encoding == BTF_INT_SIGNED)
418 else if (encoding == BTF_INT_CHAR)
420 else if (encoding == BTF_INT_BOOL)
426 static u16 btf_type_vlen(const struct btf_type *t)
428 return BTF_INFO_VLEN(t->info);
431 static bool btf_type_kflag(const struct btf_type *t)
433 return BTF_INFO_KFLAG(t->info);
436 static u32 btf_member_bit_offset(const struct btf_type *struct_type,
437 const struct btf_member *member)
439 return btf_type_kflag(struct_type) ? BTF_MEMBER_BIT_OFFSET(member->offset)
443 static u32 btf_member_bitfield_size(const struct btf_type *struct_type,
444 const struct btf_member *member)
446 return btf_type_kflag(struct_type) ? BTF_MEMBER_BITFIELD_SIZE(member->offset)
450 static u32 btf_type_int(const struct btf_type *t)
452 return *(u32 *)(t + 1);
455 static const struct btf_array *btf_type_array(const struct btf_type *t)
457 return (const struct btf_array *)(t + 1);
460 static const struct btf_member *btf_type_member(const struct btf_type *t)
462 return (const struct btf_member *)(t + 1);
465 static const struct btf_enum *btf_type_enum(const struct btf_type *t)
467 return (const struct btf_enum *)(t + 1);
470 static const struct btf_kind_operations *btf_type_ops(const struct btf_type *t)
472 return kind_ops[BTF_INFO_KIND(t->info)];
475 static bool btf_name_offset_valid(const struct btf *btf, u32 offset)
477 return BTF_STR_OFFSET_VALID(offset) &&
478 offset < btf->hdr.str_len;
481 /* Only C-style identifier is permitted. This can be relaxed if
484 static bool btf_name_valid_identifier(const struct btf *btf, u32 offset)
486 /* offset must be valid */
487 const char *src = &btf->strings[offset];
488 const char *src_limit;
490 if (!isalpha(*src) && *src != '_')
493 /* set a limit on identifier length */
494 src_limit = src + KSYM_NAME_LEN;
496 while (*src && src < src_limit) {
497 if (!isalnum(*src) && *src != '_')
505 static const char *__btf_name_by_offset(const struct btf *btf, u32 offset)
509 else if (offset < btf->hdr.str_len)
510 return &btf->strings[offset];
512 return "(invalid-name-offset)";
515 const char *btf_name_by_offset(const struct btf *btf, u32 offset)
517 if (offset < btf->hdr.str_len)
518 return &btf->strings[offset];
523 const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id)
525 if (type_id > btf->nr_types)
528 return btf->types[type_id];
532 * Regular int is not a bit field and it must be either
533 * u8/u16/u32/u64 or __int128.
535 static bool btf_type_int_is_regular(const struct btf_type *t)
537 u8 nr_bits, nr_bytes;
540 int_data = btf_type_int(t);
541 nr_bits = BTF_INT_BITS(int_data);
542 nr_bytes = BITS_ROUNDUP_BYTES(nr_bits);
543 if (BITS_PER_BYTE_MASKED(nr_bits) ||
544 BTF_INT_OFFSET(int_data) ||
545 (nr_bytes != sizeof(u8) && nr_bytes != sizeof(u16) &&
546 nr_bytes != sizeof(u32) && nr_bytes != sizeof(u64) &&
547 nr_bytes != (2 * sizeof(u64)))) {
555 * Check that given struct member is a regular int with expected
558 bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s,
559 const struct btf_member *m,
560 u32 expected_offset, u32 expected_size)
562 const struct btf_type *t;
567 t = btf_type_id_size(btf, &id, NULL);
568 if (!t || !btf_type_is_int(t))
571 int_data = btf_type_int(t);
572 nr_bits = BTF_INT_BITS(int_data);
573 if (btf_type_kflag(s)) {
574 u32 bitfield_size = BTF_MEMBER_BITFIELD_SIZE(m->offset);
575 u32 bit_offset = BTF_MEMBER_BIT_OFFSET(m->offset);
577 /* if kflag set, int should be a regular int and
578 * bit offset should be at byte boundary.
580 return !bitfield_size &&
581 BITS_ROUNDUP_BYTES(bit_offset) == expected_offset &&
582 BITS_ROUNDUP_BYTES(nr_bits) == expected_size;
585 if (BTF_INT_OFFSET(int_data) ||
586 BITS_PER_BYTE_MASKED(m->offset) ||
587 BITS_ROUNDUP_BYTES(m->offset) != expected_offset ||
588 BITS_PER_BYTE_MASKED(nr_bits) ||
589 BITS_ROUNDUP_BYTES(nr_bits) != expected_size)
595 __printf(2, 3) static void __btf_verifier_log(struct bpf_verifier_log *log,
596 const char *fmt, ...)
601 bpf_verifier_vlog(log, fmt, args);
605 __printf(2, 3) static void btf_verifier_log(struct btf_verifier_env *env,
606 const char *fmt, ...)
608 struct bpf_verifier_log *log = &env->log;
611 if (!bpf_verifier_log_needed(log))
615 bpf_verifier_vlog(log, fmt, args);
619 __printf(4, 5) static void __btf_verifier_log_type(struct btf_verifier_env *env,
620 const struct btf_type *t,
622 const char *fmt, ...)
624 struct bpf_verifier_log *log = &env->log;
625 u8 kind = BTF_INFO_KIND(t->info);
626 struct btf *btf = env->btf;
629 if (!bpf_verifier_log_needed(log))
632 __btf_verifier_log(log, "[%u] %s %s%s",
635 __btf_name_by_offset(btf, t->name_off),
636 log_details ? " " : "");
639 btf_type_ops(t)->log_details(env, t);
642 __btf_verifier_log(log, " ");
644 bpf_verifier_vlog(log, fmt, args);
648 __btf_verifier_log(log, "\n");
651 #define btf_verifier_log_type(env, t, ...) \
652 __btf_verifier_log_type((env), (t), true, __VA_ARGS__)
653 #define btf_verifier_log_basic(env, t, ...) \
654 __btf_verifier_log_type((env), (t), false, __VA_ARGS__)
657 static void btf_verifier_log_member(struct btf_verifier_env *env,
658 const struct btf_type *struct_type,
659 const struct btf_member *member,
660 const char *fmt, ...)
662 struct bpf_verifier_log *log = &env->log;
663 struct btf *btf = env->btf;
666 if (!bpf_verifier_log_needed(log))
669 /* The CHECK_META phase already did a btf dump.
671 * If member is logged again, it must hit an error in
672 * parsing this member. It is useful to print out which
673 * struct this member belongs to.
675 if (env->phase != CHECK_META)
676 btf_verifier_log_type(env, struct_type, NULL);
678 if (btf_type_kflag(struct_type))
679 __btf_verifier_log(log,
680 "\t%s type_id=%u bitfield_size=%u bits_offset=%u",
681 __btf_name_by_offset(btf, member->name_off),
683 BTF_MEMBER_BITFIELD_SIZE(member->offset),
684 BTF_MEMBER_BIT_OFFSET(member->offset));
686 __btf_verifier_log(log, "\t%s type_id=%u bits_offset=%u",
687 __btf_name_by_offset(btf, member->name_off),
688 member->type, member->offset);
691 __btf_verifier_log(log, " ");
693 bpf_verifier_vlog(log, fmt, args);
697 __btf_verifier_log(log, "\n");
700 static void btf_verifier_log_hdr(struct btf_verifier_env *env,
703 struct bpf_verifier_log *log = &env->log;
704 const struct btf *btf = env->btf;
705 const struct btf_header *hdr;
707 if (!bpf_verifier_log_needed(log))
711 __btf_verifier_log(log, "magic: 0x%x\n", hdr->magic);
712 __btf_verifier_log(log, "version: %u\n", hdr->version);
713 __btf_verifier_log(log, "flags: 0x%x\n", hdr->flags);
714 __btf_verifier_log(log, "hdr_len: %u\n", hdr->hdr_len);
715 __btf_verifier_log(log, "type_off: %u\n", hdr->type_off);
716 __btf_verifier_log(log, "type_len: %u\n", hdr->type_len);
717 __btf_verifier_log(log, "str_off: %u\n", hdr->str_off);
718 __btf_verifier_log(log, "str_len: %u\n", hdr->str_len);
719 __btf_verifier_log(log, "btf_total_size: %u\n", btf_data_size);
722 static int btf_add_type(struct btf_verifier_env *env, struct btf_type *t)
724 struct btf *btf = env->btf;
726 /* < 2 because +1 for btf_void which is always in btf->types[0].
727 * btf_void is not accounted in btf->nr_types because btf_void
728 * does not come from the BTF file.
730 if (btf->types_size - btf->nr_types < 2) {
731 /* Expand 'types' array */
733 struct btf_type **new_types;
734 u32 expand_by, new_size;
736 if (btf->types_size == BTF_MAX_TYPE) {
737 btf_verifier_log(env, "Exceeded max num of types");
741 expand_by = max_t(u32, btf->types_size >> 2, 16);
742 new_size = min_t(u32, BTF_MAX_TYPE,
743 btf->types_size + expand_by);
745 new_types = kvcalloc(new_size, sizeof(*new_types),
746 GFP_KERNEL | __GFP_NOWARN);
750 if (btf->nr_types == 0)
751 new_types[0] = &btf_void;
753 memcpy(new_types, btf->types,
754 sizeof(*btf->types) * (btf->nr_types + 1));
757 btf->types = new_types;
758 btf->types_size = new_size;
761 btf->types[++(btf->nr_types)] = t;
766 static int btf_alloc_id(struct btf *btf)
770 idr_preload(GFP_KERNEL);
771 spin_lock_bh(&btf_idr_lock);
772 id = idr_alloc_cyclic(&btf_idr, btf, 1, INT_MAX, GFP_ATOMIC);
775 spin_unlock_bh(&btf_idr_lock);
778 if (WARN_ON_ONCE(!id))
781 return id > 0 ? 0 : id;
784 static void btf_free_id(struct btf *btf)
789 * In map-in-map, calling map_delete_elem() on outer
790 * map will call bpf_map_put on the inner map.
791 * It will then eventually call btf_free_id()
792 * on the inner map. Some of the map_delete_elem()
793 * implementation may have irq disabled, so
794 * we need to use the _irqsave() version instead
795 * of the _bh() version.
797 spin_lock_irqsave(&btf_idr_lock, flags);
798 idr_remove(&btf_idr, btf->id);
799 spin_unlock_irqrestore(&btf_idr_lock, flags);
802 static void btf_free(struct btf *btf)
805 kvfree(btf->resolved_sizes);
806 kvfree(btf->resolved_ids);
811 static void btf_free_rcu(struct rcu_head *rcu)
813 struct btf *btf = container_of(rcu, struct btf, rcu);
818 void btf_put(struct btf *btf)
820 if (btf && refcount_dec_and_test(&btf->refcnt)) {
822 call_rcu(&btf->rcu, btf_free_rcu);
826 static int env_resolve_init(struct btf_verifier_env *env)
828 struct btf *btf = env->btf;
829 u32 nr_types = btf->nr_types;
830 u32 *resolved_sizes = NULL;
831 u32 *resolved_ids = NULL;
832 u8 *visit_states = NULL;
834 /* +1 for btf_void */
835 resolved_sizes = kvcalloc(nr_types + 1, sizeof(*resolved_sizes),
836 GFP_KERNEL | __GFP_NOWARN);
840 resolved_ids = kvcalloc(nr_types + 1, sizeof(*resolved_ids),
841 GFP_KERNEL | __GFP_NOWARN);
845 visit_states = kvcalloc(nr_types + 1, sizeof(*visit_states),
846 GFP_KERNEL | __GFP_NOWARN);
850 btf->resolved_sizes = resolved_sizes;
851 btf->resolved_ids = resolved_ids;
852 env->visit_states = visit_states;
857 kvfree(resolved_sizes);
858 kvfree(resolved_ids);
859 kvfree(visit_states);
863 static void btf_verifier_env_free(struct btf_verifier_env *env)
865 kvfree(env->visit_states);
869 static bool env_type_is_resolve_sink(const struct btf_verifier_env *env,
870 const struct btf_type *next_type)
872 switch (env->resolve_mode) {
874 /* int, enum or void is a sink */
875 return !btf_type_needs_resolve(next_type);
877 /* int, enum, void, struct, array, func or func_proto is a sink
880 return !btf_type_is_modifier(next_type) &&
881 !btf_type_is_ptr(next_type);
882 case RESOLVE_STRUCT_OR_ARRAY:
883 /* int, enum, void, ptr, func or func_proto is a sink
884 * for struct and array
886 return !btf_type_is_modifier(next_type) &&
887 !btf_type_is_array(next_type) &&
888 !btf_type_is_struct(next_type);
894 static bool env_type_is_resolved(const struct btf_verifier_env *env,
897 return env->visit_states[type_id] == RESOLVED;
900 static int env_stack_push(struct btf_verifier_env *env,
901 const struct btf_type *t, u32 type_id)
903 struct resolve_vertex *v;
905 if (env->top_stack == MAX_RESOLVE_DEPTH)
908 if (env->visit_states[type_id] != NOT_VISITED)
911 env->visit_states[type_id] = VISITED;
913 v = &env->stack[env->top_stack++];
915 v->type_id = type_id;
918 if (env->resolve_mode == RESOLVE_TBD) {
919 if (btf_type_is_ptr(t))
920 env->resolve_mode = RESOLVE_PTR;
921 else if (btf_type_is_struct(t) || btf_type_is_array(t))
922 env->resolve_mode = RESOLVE_STRUCT_OR_ARRAY;
928 static void env_stack_set_next_member(struct btf_verifier_env *env,
931 env->stack[env->top_stack - 1].next_member = next_member;
934 static void env_stack_pop_resolved(struct btf_verifier_env *env,
935 u32 resolved_type_id,
938 u32 type_id = env->stack[--(env->top_stack)].type_id;
939 struct btf *btf = env->btf;
941 btf->resolved_sizes[type_id] = resolved_size;
942 btf->resolved_ids[type_id] = resolved_type_id;
943 env->visit_states[type_id] = RESOLVED;
946 static const struct resolve_vertex *env_stack_peak(struct btf_verifier_env *env)
948 return env->top_stack ? &env->stack[env->top_stack - 1] : NULL;
951 /* The input param "type_id" must point to a needs_resolve type */
952 static const struct btf_type *btf_type_id_resolve(const struct btf *btf,
955 *type_id = btf->resolved_ids[*type_id];
956 return btf_type_by_id(btf, *type_id);
959 const struct btf_type *btf_type_id_size(const struct btf *btf,
960 u32 *type_id, u32 *ret_size)
962 const struct btf_type *size_type;
963 u32 size_type_id = *type_id;
966 size_type = btf_type_by_id(btf, size_type_id);
967 if (btf_type_nosize_or_null(size_type))
970 if (btf_type_has_size(size_type)) {
971 size = size_type->size;
972 } else if (btf_type_is_array(size_type)) {
973 size = btf->resolved_sizes[size_type_id];
974 } else if (btf_type_is_ptr(size_type)) {
975 size = sizeof(void *);
977 if (WARN_ON_ONCE(!btf_type_is_modifier(size_type)))
980 size = btf->resolved_sizes[size_type_id];
981 size_type_id = btf->resolved_ids[size_type_id];
982 size_type = btf_type_by_id(btf, size_type_id);
983 if (btf_type_nosize_or_null(size_type))
987 *type_id = size_type_id;
994 static int btf_df_check_member(struct btf_verifier_env *env,
995 const struct btf_type *struct_type,
996 const struct btf_member *member,
997 const struct btf_type *member_type)
999 btf_verifier_log_basic(env, struct_type,
1000 "Unsupported check_member");
1004 static int btf_df_check_kflag_member(struct btf_verifier_env *env,
1005 const struct btf_type *struct_type,
1006 const struct btf_member *member,
1007 const struct btf_type *member_type)
1009 btf_verifier_log_basic(env, struct_type,
1010 "Unsupported check_kflag_member");
1014 /* Used for ptr, array and struct/union type members.
1015 * int, enum and modifier types have their specific callback functions.
1017 static int btf_generic_check_kflag_member(struct btf_verifier_env *env,
1018 const struct btf_type *struct_type,
1019 const struct btf_member *member,
1020 const struct btf_type *member_type)
1022 if (BTF_MEMBER_BITFIELD_SIZE(member->offset)) {
1023 btf_verifier_log_member(env, struct_type, member,
1024 "Invalid member bitfield_size");
1028 /* bitfield size is 0, so member->offset represents bit offset only.
1029 * It is safe to call non kflag check_member variants.
1031 return btf_type_ops(member_type)->check_member(env, struct_type,
1036 static int btf_df_resolve(struct btf_verifier_env *env,
1037 const struct resolve_vertex *v)
1039 btf_verifier_log_basic(env, v->t, "Unsupported resolve");
1043 static void btf_df_seq_show(const struct btf *btf, const struct btf_type *t,
1044 u32 type_id, void *data, u8 bits_offsets,
1047 seq_printf(m, "<unsupported kind:%u>", BTF_INFO_KIND(t->info));
1050 static int btf_int_check_member(struct btf_verifier_env *env,
1051 const struct btf_type *struct_type,
1052 const struct btf_member *member,
1053 const struct btf_type *member_type)
1055 u32 int_data = btf_type_int(member_type);
1056 u32 struct_bits_off = member->offset;
1057 u32 struct_size = struct_type->size;
1061 if (U32_MAX - struct_bits_off < BTF_INT_OFFSET(int_data)) {
1062 btf_verifier_log_member(env, struct_type, member,
1063 "bits_offset exceeds U32_MAX");
1067 struct_bits_off += BTF_INT_OFFSET(int_data);
1068 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
1069 nr_copy_bits = BTF_INT_BITS(int_data) +
1070 BITS_PER_BYTE_MASKED(struct_bits_off);
1072 if (nr_copy_bits > BITS_PER_U128) {
1073 btf_verifier_log_member(env, struct_type, member,
1074 "nr_copy_bits exceeds 128");
1078 if (struct_size < bytes_offset ||
1079 struct_size - bytes_offset < BITS_ROUNDUP_BYTES(nr_copy_bits)) {
1080 btf_verifier_log_member(env, struct_type, member,
1081 "Member exceeds struct_size");
1088 static int btf_int_check_kflag_member(struct btf_verifier_env *env,
1089 const struct btf_type *struct_type,
1090 const struct btf_member *member,
1091 const struct btf_type *member_type)
1093 u32 struct_bits_off, nr_bits, nr_int_data_bits, bytes_offset;
1094 u32 int_data = btf_type_int(member_type);
1095 u32 struct_size = struct_type->size;
1098 /* a regular int type is required for the kflag int member */
1099 if (!btf_type_int_is_regular(member_type)) {
1100 btf_verifier_log_member(env, struct_type, member,
1101 "Invalid member base type");
1105 /* check sanity of bitfield size */
1106 nr_bits = BTF_MEMBER_BITFIELD_SIZE(member->offset);
1107 struct_bits_off = BTF_MEMBER_BIT_OFFSET(member->offset);
1108 nr_int_data_bits = BTF_INT_BITS(int_data);
1110 /* Not a bitfield member, member offset must be at byte
1113 if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
1114 btf_verifier_log_member(env, struct_type, member,
1115 "Invalid member offset");
1119 nr_bits = nr_int_data_bits;
1120 } else if (nr_bits > nr_int_data_bits) {
1121 btf_verifier_log_member(env, struct_type, member,
1122 "Invalid member bitfield_size");
1126 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
1127 nr_copy_bits = nr_bits + BITS_PER_BYTE_MASKED(struct_bits_off);
1128 if (nr_copy_bits > BITS_PER_U128) {
1129 btf_verifier_log_member(env, struct_type, member,
1130 "nr_copy_bits exceeds 128");
1134 if (struct_size < bytes_offset ||
1135 struct_size - bytes_offset < BITS_ROUNDUP_BYTES(nr_copy_bits)) {
1136 btf_verifier_log_member(env, struct_type, member,
1137 "Member exceeds struct_size");
1144 static s32 btf_int_check_meta(struct btf_verifier_env *env,
1145 const struct btf_type *t,
1148 u32 int_data, nr_bits, meta_needed = sizeof(int_data);
1151 if (meta_left < meta_needed) {
1152 btf_verifier_log_basic(env, t,
1153 "meta_left:%u meta_needed:%u",
1154 meta_left, meta_needed);
1158 if (btf_type_vlen(t)) {
1159 btf_verifier_log_type(env, t, "vlen != 0");
1163 if (btf_type_kflag(t)) {
1164 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
1168 int_data = btf_type_int(t);
1169 if (int_data & ~BTF_INT_MASK) {
1170 btf_verifier_log_basic(env, t, "Invalid int_data:%x",
1175 nr_bits = BTF_INT_BITS(int_data) + BTF_INT_OFFSET(int_data);
1177 if (nr_bits > BITS_PER_U128) {
1178 btf_verifier_log_type(env, t, "nr_bits exceeds %zu",
1183 if (BITS_ROUNDUP_BYTES(nr_bits) > t->size) {
1184 btf_verifier_log_type(env, t, "nr_bits exceeds type_size");
1189 * Only one of the encoding bits is allowed and it
1190 * should be sufficient for the pretty print purpose (i.e. decoding).
1191 * Multiple bits can be allowed later if it is found
1192 * to be insufficient.
1194 encoding = BTF_INT_ENCODING(int_data);
1196 encoding != BTF_INT_SIGNED &&
1197 encoding != BTF_INT_CHAR &&
1198 encoding != BTF_INT_BOOL) {
1199 btf_verifier_log_type(env, t, "Unsupported encoding");
1203 btf_verifier_log_type(env, t, NULL);
1208 static void btf_int_log(struct btf_verifier_env *env,
1209 const struct btf_type *t)
1211 int int_data = btf_type_int(t);
1213 btf_verifier_log(env,
1214 "size=%u bits_offset=%u nr_bits=%u encoding=%s",
1215 t->size, BTF_INT_OFFSET(int_data),
1216 BTF_INT_BITS(int_data),
1217 btf_int_encoding_str(BTF_INT_ENCODING(int_data)));
1220 static void btf_int128_print(struct seq_file *m, void *data)
1222 /* data points to a __int128 number.
1224 * int128_num = *(__int128 *)data;
1225 * The below formulas shows what upper_num and lower_num represents:
1226 * upper_num = int128_num >> 64;
1227 * lower_num = int128_num & 0xffffffffFFFFFFFFULL;
1229 u64 upper_num, lower_num;
1231 #ifdef __BIG_ENDIAN_BITFIELD
1232 upper_num = *(u64 *)data;
1233 lower_num = *(u64 *)(data + 8);
1235 upper_num = *(u64 *)(data + 8);
1236 lower_num = *(u64 *)data;
1239 seq_printf(m, "0x%llx", lower_num);
1241 seq_printf(m, "0x%llx%016llx", upper_num, lower_num);
1244 static void btf_int128_shift(u64 *print_num, u16 left_shift_bits,
1245 u16 right_shift_bits)
1247 u64 upper_num, lower_num;
1249 #ifdef __BIG_ENDIAN_BITFIELD
1250 upper_num = print_num[0];
1251 lower_num = print_num[1];
1253 upper_num = print_num[1];
1254 lower_num = print_num[0];
1257 /* shake out un-needed bits by shift/or operations */
1258 if (left_shift_bits >= 64) {
1259 upper_num = lower_num << (left_shift_bits - 64);
1262 upper_num = (upper_num << left_shift_bits) |
1263 (lower_num >> (64 - left_shift_bits));
1264 lower_num = lower_num << left_shift_bits;
1267 if (right_shift_bits >= 64) {
1268 lower_num = upper_num >> (right_shift_bits - 64);
1271 lower_num = (lower_num >> right_shift_bits) |
1272 (upper_num << (64 - right_shift_bits));
1273 upper_num = upper_num >> right_shift_bits;
1276 #ifdef __BIG_ENDIAN_BITFIELD
1277 print_num[0] = upper_num;
1278 print_num[1] = lower_num;
1280 print_num[0] = lower_num;
1281 print_num[1] = upper_num;
1285 static void btf_bitfield_seq_show(void *data, u8 bits_offset,
1286 u8 nr_bits, struct seq_file *m)
1288 u16 left_shift_bits, right_shift_bits;
1291 u64 print_num[2] = {};
1293 nr_copy_bits = nr_bits + bits_offset;
1294 nr_copy_bytes = BITS_ROUNDUP_BYTES(nr_copy_bits);
1296 memcpy(print_num, data, nr_copy_bytes);
1298 #ifdef __BIG_ENDIAN_BITFIELD
1299 left_shift_bits = bits_offset;
1301 left_shift_bits = BITS_PER_U128 - nr_copy_bits;
1303 right_shift_bits = BITS_PER_U128 - nr_bits;
1305 btf_int128_shift(print_num, left_shift_bits, right_shift_bits);
1306 btf_int128_print(m, print_num);
1310 static void btf_int_bits_seq_show(const struct btf *btf,
1311 const struct btf_type *t,
1312 void *data, u8 bits_offset,
1315 u32 int_data = btf_type_int(t);
1316 u8 nr_bits = BTF_INT_BITS(int_data);
1317 u8 total_bits_offset;
1320 * bits_offset is at most 7.
1321 * BTF_INT_OFFSET() cannot exceed 128 bits.
1323 total_bits_offset = bits_offset + BTF_INT_OFFSET(int_data);
1324 data += BITS_ROUNDDOWN_BYTES(total_bits_offset);
1325 bits_offset = BITS_PER_BYTE_MASKED(total_bits_offset);
1326 btf_bitfield_seq_show(data, bits_offset, nr_bits, m);
1329 static void btf_int_seq_show(const struct btf *btf, const struct btf_type *t,
1330 u32 type_id, void *data, u8 bits_offset,
1333 u32 int_data = btf_type_int(t);
1334 u8 encoding = BTF_INT_ENCODING(int_data);
1335 bool sign = encoding & BTF_INT_SIGNED;
1336 u8 nr_bits = BTF_INT_BITS(int_data);
1338 if (bits_offset || BTF_INT_OFFSET(int_data) ||
1339 BITS_PER_BYTE_MASKED(nr_bits)) {
1340 btf_int_bits_seq_show(btf, t, data, bits_offset, m);
1346 btf_int128_print(m, data);
1350 seq_printf(m, "%lld", *(s64 *)data);
1352 seq_printf(m, "%llu", *(u64 *)data);
1356 seq_printf(m, "%d", *(s32 *)data);
1358 seq_printf(m, "%u", *(u32 *)data);
1362 seq_printf(m, "%d", *(s16 *)data);
1364 seq_printf(m, "%u", *(u16 *)data);
1368 seq_printf(m, "%d", *(s8 *)data);
1370 seq_printf(m, "%u", *(u8 *)data);
1373 btf_int_bits_seq_show(btf, t, data, bits_offset, m);
1377 static const struct btf_kind_operations int_ops = {
1378 .check_meta = btf_int_check_meta,
1379 .resolve = btf_df_resolve,
1380 .check_member = btf_int_check_member,
1381 .check_kflag_member = btf_int_check_kflag_member,
1382 .log_details = btf_int_log,
1383 .seq_show = btf_int_seq_show,
1386 static int btf_modifier_check_member(struct btf_verifier_env *env,
1387 const struct btf_type *struct_type,
1388 const struct btf_member *member,
1389 const struct btf_type *member_type)
1391 const struct btf_type *resolved_type;
1392 u32 resolved_type_id = member->type;
1393 struct btf_member resolved_member;
1394 struct btf *btf = env->btf;
1396 resolved_type = btf_type_id_size(btf, &resolved_type_id, NULL);
1397 if (!resolved_type) {
1398 btf_verifier_log_member(env, struct_type, member,
1403 resolved_member = *member;
1404 resolved_member.type = resolved_type_id;
1406 return btf_type_ops(resolved_type)->check_member(env, struct_type,
1411 static int btf_modifier_check_kflag_member(struct btf_verifier_env *env,
1412 const struct btf_type *struct_type,
1413 const struct btf_member *member,
1414 const struct btf_type *member_type)
1416 const struct btf_type *resolved_type;
1417 u32 resolved_type_id = member->type;
1418 struct btf_member resolved_member;
1419 struct btf *btf = env->btf;
1421 resolved_type = btf_type_id_size(btf, &resolved_type_id, NULL);
1422 if (!resolved_type) {
1423 btf_verifier_log_member(env, struct_type, member,
1428 resolved_member = *member;
1429 resolved_member.type = resolved_type_id;
1431 return btf_type_ops(resolved_type)->check_kflag_member(env, struct_type,
1436 static int btf_ptr_check_member(struct btf_verifier_env *env,
1437 const struct btf_type *struct_type,
1438 const struct btf_member *member,
1439 const struct btf_type *member_type)
1441 u32 struct_size, struct_bits_off, bytes_offset;
1443 struct_size = struct_type->size;
1444 struct_bits_off = member->offset;
1445 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
1447 if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
1448 btf_verifier_log_member(env, struct_type, member,
1449 "Member is not byte aligned");
1453 if (struct_size - bytes_offset < sizeof(void *)) {
1454 btf_verifier_log_member(env, struct_type, member,
1455 "Member exceeds struct_size");
1462 static int btf_ref_type_check_meta(struct btf_verifier_env *env,
1463 const struct btf_type *t,
1466 if (btf_type_vlen(t)) {
1467 btf_verifier_log_type(env, t, "vlen != 0");
1471 if (btf_type_kflag(t)) {
1472 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
1476 if (!BTF_TYPE_ID_VALID(t->type)) {
1477 btf_verifier_log_type(env, t, "Invalid type_id");
1481 /* typedef type must have a valid name, and other ref types,
1482 * volatile, const, restrict, should have a null name.
1484 if (BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF) {
1486 !btf_name_valid_identifier(env->btf, t->name_off)) {
1487 btf_verifier_log_type(env, t, "Invalid name");
1492 btf_verifier_log_type(env, t, "Invalid name");
1497 btf_verifier_log_type(env, t, NULL);
1502 static int btf_modifier_resolve(struct btf_verifier_env *env,
1503 const struct resolve_vertex *v)
1505 const struct btf_type *t = v->t;
1506 const struct btf_type *next_type;
1507 u32 next_type_id = t->type;
1508 struct btf *btf = env->btf;
1509 u32 next_type_size = 0;
1511 next_type = btf_type_by_id(btf, next_type_id);
1513 btf_verifier_log_type(env, v->t, "Invalid type_id");
1517 if (!env_type_is_resolve_sink(env, next_type) &&
1518 !env_type_is_resolved(env, next_type_id))
1519 return env_stack_push(env, next_type, next_type_id);
1521 /* Figure out the resolved next_type_id with size.
1522 * They will be stored in the current modifier's
1523 * resolved_ids and resolved_sizes such that it can
1524 * save us a few type-following when we use it later (e.g. in
1527 if (!btf_type_id_size(btf, &next_type_id, &next_type_size)) {
1528 if (env_type_is_resolved(env, next_type_id))
1529 next_type = btf_type_id_resolve(btf, &next_type_id);
1531 /* "typedef void new_void", "const void"...etc */
1532 if (!btf_type_is_void(next_type) &&
1533 !btf_type_is_fwd(next_type) &&
1534 !btf_type_is_func_proto(next_type)) {
1535 btf_verifier_log_type(env, v->t, "Invalid type_id");
1540 env_stack_pop_resolved(env, next_type_id, next_type_size);
1545 static int btf_ptr_resolve(struct btf_verifier_env *env,
1546 const struct resolve_vertex *v)
1548 const struct btf_type *next_type;
1549 const struct btf_type *t = v->t;
1550 u32 next_type_id = t->type;
1551 struct btf *btf = env->btf;
1553 next_type = btf_type_by_id(btf, next_type_id);
1555 btf_verifier_log_type(env, v->t, "Invalid type_id");
1559 if (!env_type_is_resolve_sink(env, next_type) &&
1560 !env_type_is_resolved(env, next_type_id))
1561 return env_stack_push(env, next_type, next_type_id);
1563 /* If the modifier was RESOLVED during RESOLVE_STRUCT_OR_ARRAY,
1564 * the modifier may have stopped resolving when it was resolved
1565 * to a ptr (last-resolved-ptr).
1567 * We now need to continue from the last-resolved-ptr to
1568 * ensure the last-resolved-ptr will not referring back to
1569 * the currenct ptr (t).
1571 if (btf_type_is_modifier(next_type)) {
1572 const struct btf_type *resolved_type;
1573 u32 resolved_type_id;
1575 resolved_type_id = next_type_id;
1576 resolved_type = btf_type_id_resolve(btf, &resolved_type_id);
1578 if (btf_type_is_ptr(resolved_type) &&
1579 !env_type_is_resolve_sink(env, resolved_type) &&
1580 !env_type_is_resolved(env, resolved_type_id))
1581 return env_stack_push(env, resolved_type,
1585 if (!btf_type_id_size(btf, &next_type_id, NULL)) {
1586 if (env_type_is_resolved(env, next_type_id))
1587 next_type = btf_type_id_resolve(btf, &next_type_id);
1589 if (!btf_type_is_void(next_type) &&
1590 !btf_type_is_fwd(next_type) &&
1591 !btf_type_is_func_proto(next_type)) {
1592 btf_verifier_log_type(env, v->t, "Invalid type_id");
1597 env_stack_pop_resolved(env, next_type_id, 0);
1602 static void btf_modifier_seq_show(const struct btf *btf,
1603 const struct btf_type *t,
1604 u32 type_id, void *data,
1605 u8 bits_offset, struct seq_file *m)
1607 t = btf_type_id_resolve(btf, &type_id);
1609 btf_type_ops(t)->seq_show(btf, t, type_id, data, bits_offset, m);
1612 static void btf_ptr_seq_show(const struct btf *btf, const struct btf_type *t,
1613 u32 type_id, void *data, u8 bits_offset,
1616 /* It is a hashed value */
1617 seq_printf(m, "%p", *(void **)data);
1620 static void btf_ref_type_log(struct btf_verifier_env *env,
1621 const struct btf_type *t)
1623 btf_verifier_log(env, "type_id=%u", t->type);
1626 static struct btf_kind_operations modifier_ops = {
1627 .check_meta = btf_ref_type_check_meta,
1628 .resolve = btf_modifier_resolve,
1629 .check_member = btf_modifier_check_member,
1630 .check_kflag_member = btf_modifier_check_kflag_member,
1631 .log_details = btf_ref_type_log,
1632 .seq_show = btf_modifier_seq_show,
1635 static struct btf_kind_operations ptr_ops = {
1636 .check_meta = btf_ref_type_check_meta,
1637 .resolve = btf_ptr_resolve,
1638 .check_member = btf_ptr_check_member,
1639 .check_kflag_member = btf_generic_check_kflag_member,
1640 .log_details = btf_ref_type_log,
1641 .seq_show = btf_ptr_seq_show,
1644 static s32 btf_fwd_check_meta(struct btf_verifier_env *env,
1645 const struct btf_type *t,
1648 if (btf_type_vlen(t)) {
1649 btf_verifier_log_type(env, t, "vlen != 0");
1654 btf_verifier_log_type(env, t, "type != 0");
1658 /* fwd type must have a valid name */
1660 !btf_name_valid_identifier(env->btf, t->name_off)) {
1661 btf_verifier_log_type(env, t, "Invalid name");
1665 btf_verifier_log_type(env, t, NULL);
1670 static void btf_fwd_type_log(struct btf_verifier_env *env,
1671 const struct btf_type *t)
1673 btf_verifier_log(env, "%s", btf_type_kflag(t) ? "union" : "struct");
1676 static struct btf_kind_operations fwd_ops = {
1677 .check_meta = btf_fwd_check_meta,
1678 .resolve = btf_df_resolve,
1679 .check_member = btf_df_check_member,
1680 .check_kflag_member = btf_df_check_kflag_member,
1681 .log_details = btf_fwd_type_log,
1682 .seq_show = btf_df_seq_show,
1685 static int btf_array_check_member(struct btf_verifier_env *env,
1686 const struct btf_type *struct_type,
1687 const struct btf_member *member,
1688 const struct btf_type *member_type)
1690 u32 struct_bits_off = member->offset;
1691 u32 struct_size, bytes_offset;
1692 u32 array_type_id, array_size;
1693 struct btf *btf = env->btf;
1695 if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
1696 btf_verifier_log_member(env, struct_type, member,
1697 "Member is not byte aligned");
1701 array_type_id = member->type;
1702 btf_type_id_size(btf, &array_type_id, &array_size);
1703 struct_size = struct_type->size;
1704 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
1705 if (struct_size - bytes_offset < array_size) {
1706 btf_verifier_log_member(env, struct_type, member,
1707 "Member exceeds struct_size");
1714 static s32 btf_array_check_meta(struct btf_verifier_env *env,
1715 const struct btf_type *t,
1718 const struct btf_array *array = btf_type_array(t);
1719 u32 meta_needed = sizeof(*array);
1721 if (meta_left < meta_needed) {
1722 btf_verifier_log_basic(env, t,
1723 "meta_left:%u meta_needed:%u",
1724 meta_left, meta_needed);
1728 /* array type should not have a name */
1730 btf_verifier_log_type(env, t, "Invalid name");
1734 if (btf_type_vlen(t)) {
1735 btf_verifier_log_type(env, t, "vlen != 0");
1739 if (btf_type_kflag(t)) {
1740 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
1745 btf_verifier_log_type(env, t, "size != 0");
1749 /* Array elem type and index type cannot be in type void,
1750 * so !array->type and !array->index_type are not allowed.
1752 if (!array->type || !BTF_TYPE_ID_VALID(array->type)) {
1753 btf_verifier_log_type(env, t, "Invalid elem");
1757 if (!array->index_type || !BTF_TYPE_ID_VALID(array->index_type)) {
1758 btf_verifier_log_type(env, t, "Invalid index");
1762 btf_verifier_log_type(env, t, NULL);
1767 static int btf_array_resolve(struct btf_verifier_env *env,
1768 const struct resolve_vertex *v)
1770 const struct btf_array *array = btf_type_array(v->t);
1771 const struct btf_type *elem_type, *index_type;
1772 u32 elem_type_id, index_type_id;
1773 struct btf *btf = env->btf;
1776 /* Check array->index_type */
1777 index_type_id = array->index_type;
1778 index_type = btf_type_by_id(btf, index_type_id);
1779 if (btf_type_nosize_or_null(index_type)) {
1780 btf_verifier_log_type(env, v->t, "Invalid index");
1784 if (!env_type_is_resolve_sink(env, index_type) &&
1785 !env_type_is_resolved(env, index_type_id))
1786 return env_stack_push(env, index_type, index_type_id);
1788 index_type = btf_type_id_size(btf, &index_type_id, NULL);
1789 if (!index_type || !btf_type_is_int(index_type) ||
1790 !btf_type_int_is_regular(index_type)) {
1791 btf_verifier_log_type(env, v->t, "Invalid index");
1795 /* Check array->type */
1796 elem_type_id = array->type;
1797 elem_type = btf_type_by_id(btf, elem_type_id);
1798 if (btf_type_nosize_or_null(elem_type)) {
1799 btf_verifier_log_type(env, v->t,
1804 if (!env_type_is_resolve_sink(env, elem_type) &&
1805 !env_type_is_resolved(env, elem_type_id))
1806 return env_stack_push(env, elem_type, elem_type_id);
1808 elem_type = btf_type_id_size(btf, &elem_type_id, &elem_size);
1810 btf_verifier_log_type(env, v->t, "Invalid elem");
1814 if (btf_type_is_int(elem_type) && !btf_type_int_is_regular(elem_type)) {
1815 btf_verifier_log_type(env, v->t, "Invalid array of int");
1819 if (array->nelems && elem_size > U32_MAX / array->nelems) {
1820 btf_verifier_log_type(env, v->t,
1821 "Array size overflows U32_MAX");
1825 env_stack_pop_resolved(env, elem_type_id, elem_size * array->nelems);
1830 static void btf_array_log(struct btf_verifier_env *env,
1831 const struct btf_type *t)
1833 const struct btf_array *array = btf_type_array(t);
1835 btf_verifier_log(env, "type_id=%u index_type_id=%u nr_elems=%u",
1836 array->type, array->index_type, array->nelems);
1839 static void btf_array_seq_show(const struct btf *btf, const struct btf_type *t,
1840 u32 type_id, void *data, u8 bits_offset,
1843 const struct btf_array *array = btf_type_array(t);
1844 const struct btf_kind_operations *elem_ops;
1845 const struct btf_type *elem_type;
1846 u32 i, elem_size, elem_type_id;
1848 elem_type_id = array->type;
1849 elem_type = btf_type_id_size(btf, &elem_type_id, &elem_size);
1850 elem_ops = btf_type_ops(elem_type);
1852 for (i = 0; i < array->nelems; i++) {
1856 elem_ops->seq_show(btf, elem_type, elem_type_id, data,
1863 static struct btf_kind_operations array_ops = {
1864 .check_meta = btf_array_check_meta,
1865 .resolve = btf_array_resolve,
1866 .check_member = btf_array_check_member,
1867 .check_kflag_member = btf_generic_check_kflag_member,
1868 .log_details = btf_array_log,
1869 .seq_show = btf_array_seq_show,
1872 static int btf_struct_check_member(struct btf_verifier_env *env,
1873 const struct btf_type *struct_type,
1874 const struct btf_member *member,
1875 const struct btf_type *member_type)
1877 u32 struct_bits_off = member->offset;
1878 u32 struct_size, bytes_offset;
1880 if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
1881 btf_verifier_log_member(env, struct_type, member,
1882 "Member is not byte aligned");
1886 struct_size = struct_type->size;
1887 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
1888 if (struct_size - bytes_offset < member_type->size) {
1889 btf_verifier_log_member(env, struct_type, member,
1890 "Member exceeds struct_size");
1897 static s32 btf_struct_check_meta(struct btf_verifier_env *env,
1898 const struct btf_type *t,
1901 bool is_union = BTF_INFO_KIND(t->info) == BTF_KIND_UNION;
1902 const struct btf_member *member;
1903 u32 meta_needed, last_offset;
1904 struct btf *btf = env->btf;
1905 u32 struct_size = t->size;
1909 meta_needed = btf_type_vlen(t) * sizeof(*member);
1910 if (meta_left < meta_needed) {
1911 btf_verifier_log_basic(env, t,
1912 "meta_left:%u meta_needed:%u",
1913 meta_left, meta_needed);
1917 /* struct type either no name or a valid one */
1919 !btf_name_valid_identifier(env->btf, t->name_off)) {
1920 btf_verifier_log_type(env, t, "Invalid name");
1924 btf_verifier_log_type(env, t, NULL);
1927 for_each_member(i, t, member) {
1928 if (!btf_name_offset_valid(btf, member->name_off)) {
1929 btf_verifier_log_member(env, t, member,
1930 "Invalid member name_offset:%u",
1935 /* struct member either no name or a valid one */
1936 if (member->name_off &&
1937 !btf_name_valid_identifier(btf, member->name_off)) {
1938 btf_verifier_log_member(env, t, member, "Invalid name");
1941 /* A member cannot be in type void */
1942 if (!member->type || !BTF_TYPE_ID_VALID(member->type)) {
1943 btf_verifier_log_member(env, t, member,
1948 offset = btf_member_bit_offset(t, member);
1949 if (is_union && offset) {
1950 btf_verifier_log_member(env, t, member,
1951 "Invalid member bits_offset");
1956 * ">" instead of ">=" because the last member could be
1959 if (last_offset > offset) {
1960 btf_verifier_log_member(env, t, member,
1961 "Invalid member bits_offset");
1965 if (BITS_ROUNDUP_BYTES(offset) > struct_size) {
1966 btf_verifier_log_member(env, t, member,
1967 "Member bits_offset exceeds its struct size");
1971 btf_verifier_log_member(env, t, member, NULL);
1972 last_offset = offset;
1978 static int btf_struct_resolve(struct btf_verifier_env *env,
1979 const struct resolve_vertex *v)
1981 const struct btf_member *member;
1985 /* Before continue resolving the next_member,
1986 * ensure the last member is indeed resolved to a
1987 * type with size info.
1989 if (v->next_member) {
1990 const struct btf_type *last_member_type;
1991 const struct btf_member *last_member;
1992 u16 last_member_type_id;
1994 last_member = btf_type_member(v->t) + v->next_member - 1;
1995 last_member_type_id = last_member->type;
1996 if (WARN_ON_ONCE(!env_type_is_resolved(env,
1997 last_member_type_id)))
2000 last_member_type = btf_type_by_id(env->btf,
2001 last_member_type_id);
2002 if (btf_type_kflag(v->t))
2003 err = btf_type_ops(last_member_type)->check_kflag_member(env, v->t,
2007 err = btf_type_ops(last_member_type)->check_member(env, v->t,
2014 for_each_member_from(i, v->next_member, v->t, member) {
2015 u32 member_type_id = member->type;
2016 const struct btf_type *member_type = btf_type_by_id(env->btf,
2019 if (btf_type_nosize_or_null(member_type)) {
2020 btf_verifier_log_member(env, v->t, member,
2025 if (!env_type_is_resolve_sink(env, member_type) &&
2026 !env_type_is_resolved(env, member_type_id)) {
2027 env_stack_set_next_member(env, i + 1);
2028 return env_stack_push(env, member_type, member_type_id);
2031 if (btf_type_kflag(v->t))
2032 err = btf_type_ops(member_type)->check_kflag_member(env, v->t,
2036 err = btf_type_ops(member_type)->check_member(env, v->t,
2043 env_stack_pop_resolved(env, 0, 0);
2048 static void btf_struct_log(struct btf_verifier_env *env,
2049 const struct btf_type *t)
2051 btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t));
2054 /* find 'struct bpf_spin_lock' in map value.
2055 * return >= 0 offset if found
2056 * and < 0 in case of error
2058 int btf_find_spin_lock(const struct btf *btf, const struct btf_type *t)
2060 const struct btf_member *member;
2061 u32 i, off = -ENOENT;
2063 if (!__btf_type_is_struct(t))
2066 for_each_member(i, t, member) {
2067 const struct btf_type *member_type = btf_type_by_id(btf,
2069 if (!__btf_type_is_struct(member_type))
2071 if (member_type->size != sizeof(struct bpf_spin_lock))
2073 if (strcmp(__btf_name_by_offset(btf, member_type->name_off),
2077 /* only one 'struct bpf_spin_lock' is allowed */
2079 off = btf_member_bit_offset(t, member);
2081 /* valid C code cannot generate such BTF */
2084 if (off % __alignof__(struct bpf_spin_lock))
2085 /* valid struct bpf_spin_lock will be 4 byte aligned */
2091 static void btf_struct_seq_show(const struct btf *btf, const struct btf_type *t,
2092 u32 type_id, void *data, u8 bits_offset,
2095 const char *seq = BTF_INFO_KIND(t->info) == BTF_KIND_UNION ? "|" : ",";
2096 const struct btf_member *member;
2100 for_each_member(i, t, member) {
2101 const struct btf_type *member_type = btf_type_by_id(btf,
2103 const struct btf_kind_operations *ops;
2104 u32 member_offset, bitfield_size;
2111 member_offset = btf_member_bit_offset(t, member);
2112 bitfield_size = btf_member_bitfield_size(t, member);
2113 bytes_offset = BITS_ROUNDDOWN_BYTES(member_offset);
2114 bits8_offset = BITS_PER_BYTE_MASKED(member_offset);
2115 if (bitfield_size) {
2116 btf_bitfield_seq_show(data + bytes_offset, bits8_offset,
2119 ops = btf_type_ops(member_type);
2120 ops->seq_show(btf, member_type, member->type,
2121 data + bytes_offset, bits8_offset, m);
2127 static struct btf_kind_operations struct_ops = {
2128 .check_meta = btf_struct_check_meta,
2129 .resolve = btf_struct_resolve,
2130 .check_member = btf_struct_check_member,
2131 .check_kflag_member = btf_generic_check_kflag_member,
2132 .log_details = btf_struct_log,
2133 .seq_show = btf_struct_seq_show,
2136 static int btf_enum_check_member(struct btf_verifier_env *env,
2137 const struct btf_type *struct_type,
2138 const struct btf_member *member,
2139 const struct btf_type *member_type)
2141 u32 struct_bits_off = member->offset;
2142 u32 struct_size, bytes_offset;
2144 if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
2145 btf_verifier_log_member(env, struct_type, member,
2146 "Member is not byte aligned");
2150 struct_size = struct_type->size;
2151 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
2152 if (struct_size - bytes_offset < sizeof(int)) {
2153 btf_verifier_log_member(env, struct_type, member,
2154 "Member exceeds struct_size");
2161 static int btf_enum_check_kflag_member(struct btf_verifier_env *env,
2162 const struct btf_type *struct_type,
2163 const struct btf_member *member,
2164 const struct btf_type *member_type)
2166 u32 struct_bits_off, nr_bits, bytes_end, struct_size;
2167 u32 int_bitsize = sizeof(int) * BITS_PER_BYTE;
2169 struct_bits_off = BTF_MEMBER_BIT_OFFSET(member->offset);
2170 nr_bits = BTF_MEMBER_BITFIELD_SIZE(member->offset);
2172 if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
2173 btf_verifier_log_member(env, struct_type, member,
2174 "Member is not byte aligned");
2178 nr_bits = int_bitsize;
2179 } else if (nr_bits > int_bitsize) {
2180 btf_verifier_log_member(env, struct_type, member,
2181 "Invalid member bitfield_size");
2185 struct_size = struct_type->size;
2186 bytes_end = BITS_ROUNDUP_BYTES(struct_bits_off + nr_bits);
2187 if (struct_size < bytes_end) {
2188 btf_verifier_log_member(env, struct_type, member,
2189 "Member exceeds struct_size");
2196 static s32 btf_enum_check_meta(struct btf_verifier_env *env,
2197 const struct btf_type *t,
2200 const struct btf_enum *enums = btf_type_enum(t);
2201 struct btf *btf = env->btf;
2205 nr_enums = btf_type_vlen(t);
2206 meta_needed = nr_enums * sizeof(*enums);
2208 if (meta_left < meta_needed) {
2209 btf_verifier_log_basic(env, t,
2210 "meta_left:%u meta_needed:%u",
2211 meta_left, meta_needed);
2215 if (btf_type_kflag(t)) {
2216 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
2220 if (t->size != sizeof(int)) {
2221 btf_verifier_log_type(env, t, "Expected size:%zu",
2226 /* enum type either no name or a valid one */
2228 !btf_name_valid_identifier(env->btf, t->name_off)) {
2229 btf_verifier_log_type(env, t, "Invalid name");
2233 btf_verifier_log_type(env, t, NULL);
2235 for (i = 0; i < nr_enums; i++) {
2236 if (!btf_name_offset_valid(btf, enums[i].name_off)) {
2237 btf_verifier_log(env, "\tInvalid name_offset:%u",
2242 /* enum member must have a valid name */
2243 if (!enums[i].name_off ||
2244 !btf_name_valid_identifier(btf, enums[i].name_off)) {
2245 btf_verifier_log_type(env, t, "Invalid name");
2250 btf_verifier_log(env, "\t%s val=%d\n",
2251 __btf_name_by_offset(btf, enums[i].name_off),
2258 static void btf_enum_log(struct btf_verifier_env *env,
2259 const struct btf_type *t)
2261 btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t));
2264 static void btf_enum_seq_show(const struct btf *btf, const struct btf_type *t,
2265 u32 type_id, void *data, u8 bits_offset,
2268 const struct btf_enum *enums = btf_type_enum(t);
2269 u32 i, nr_enums = btf_type_vlen(t);
2270 int v = *(int *)data;
2272 for (i = 0; i < nr_enums; i++) {
2273 if (v == enums[i].val) {
2275 __btf_name_by_offset(btf,
2276 enums[i].name_off));
2281 seq_printf(m, "%d", v);
2284 static struct btf_kind_operations enum_ops = {
2285 .check_meta = btf_enum_check_meta,
2286 .resolve = btf_df_resolve,
2287 .check_member = btf_enum_check_member,
2288 .check_kflag_member = btf_enum_check_kflag_member,
2289 .log_details = btf_enum_log,
2290 .seq_show = btf_enum_seq_show,
2293 static s32 btf_func_proto_check_meta(struct btf_verifier_env *env,
2294 const struct btf_type *t,
2297 u32 meta_needed = btf_type_vlen(t) * sizeof(struct btf_param);
2299 if (meta_left < meta_needed) {
2300 btf_verifier_log_basic(env, t,
2301 "meta_left:%u meta_needed:%u",
2302 meta_left, meta_needed);
2307 btf_verifier_log_type(env, t, "Invalid name");
2311 if (btf_type_kflag(t)) {
2312 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
2316 btf_verifier_log_type(env, t, NULL);
2321 static void btf_func_proto_log(struct btf_verifier_env *env,
2322 const struct btf_type *t)
2324 const struct btf_param *args = (const struct btf_param *)(t + 1);
2325 u16 nr_args = btf_type_vlen(t), i;
2327 btf_verifier_log(env, "return=%u args=(", t->type);
2329 btf_verifier_log(env, "void");
2333 if (nr_args == 1 && !args[0].type) {
2334 /* Only one vararg */
2335 btf_verifier_log(env, "vararg");
2339 btf_verifier_log(env, "%u %s", args[0].type,
2340 __btf_name_by_offset(env->btf,
2342 for (i = 1; i < nr_args - 1; i++)
2343 btf_verifier_log(env, ", %u %s", args[i].type,
2344 __btf_name_by_offset(env->btf,
2348 const struct btf_param *last_arg = &args[nr_args - 1];
2351 btf_verifier_log(env, ", %u %s", last_arg->type,
2352 __btf_name_by_offset(env->btf,
2353 last_arg->name_off));
2355 btf_verifier_log(env, ", vararg");
2359 btf_verifier_log(env, ")");
2362 static struct btf_kind_operations func_proto_ops = {
2363 .check_meta = btf_func_proto_check_meta,
2364 .resolve = btf_df_resolve,
2366 * BTF_KIND_FUNC_PROTO cannot be directly referred by
2367 * a struct's member.
2369 * It should be a funciton pointer instead.
2370 * (i.e. struct's member -> BTF_KIND_PTR -> BTF_KIND_FUNC_PROTO)
2372 * Hence, there is no btf_func_check_member().
2374 .check_member = btf_df_check_member,
2375 .check_kflag_member = btf_df_check_kflag_member,
2376 .log_details = btf_func_proto_log,
2377 .seq_show = btf_df_seq_show,
2380 static s32 btf_func_check_meta(struct btf_verifier_env *env,
2381 const struct btf_type *t,
2385 !btf_name_valid_identifier(env->btf, t->name_off)) {
2386 btf_verifier_log_type(env, t, "Invalid name");
2390 if (btf_type_vlen(t)) {
2391 btf_verifier_log_type(env, t, "vlen != 0");
2395 if (btf_type_kflag(t)) {
2396 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
2400 btf_verifier_log_type(env, t, NULL);
2405 static struct btf_kind_operations func_ops = {
2406 .check_meta = btf_func_check_meta,
2407 .resolve = btf_df_resolve,
2408 .check_member = btf_df_check_member,
2409 .check_kflag_member = btf_df_check_kflag_member,
2410 .log_details = btf_ref_type_log,
2411 .seq_show = btf_df_seq_show,
2414 static int btf_func_proto_check(struct btf_verifier_env *env,
2415 const struct btf_type *t)
2417 const struct btf_type *ret_type;
2418 const struct btf_param *args;
2419 const struct btf *btf;
2424 args = (const struct btf_param *)(t + 1);
2425 nr_args = btf_type_vlen(t);
2427 /* Check func return type which could be "void" (t->type == 0) */
2429 u32 ret_type_id = t->type;
2431 ret_type = btf_type_by_id(btf, ret_type_id);
2433 btf_verifier_log_type(env, t, "Invalid return type");
2437 if (btf_type_needs_resolve(ret_type) &&
2438 !env_type_is_resolved(env, ret_type_id)) {
2439 err = btf_resolve(env, ret_type, ret_type_id);
2444 /* Ensure the return type is a type that has a size */
2445 if (!btf_type_id_size(btf, &ret_type_id, NULL)) {
2446 btf_verifier_log_type(env, t, "Invalid return type");
2454 /* Last func arg type_id could be 0 if it is a vararg */
2455 if (!args[nr_args - 1].type) {
2456 if (args[nr_args - 1].name_off) {
2457 btf_verifier_log_type(env, t, "Invalid arg#%u",
2465 for (i = 0; i < nr_args; i++) {
2466 const struct btf_type *arg_type;
2469 arg_type_id = args[i].type;
2470 arg_type = btf_type_by_id(btf, arg_type_id);
2472 btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1);
2477 if (args[i].name_off &&
2478 (!btf_name_offset_valid(btf, args[i].name_off) ||
2479 !btf_name_valid_identifier(btf, args[i].name_off))) {
2480 btf_verifier_log_type(env, t,
2481 "Invalid arg#%u", i + 1);
2486 if (btf_type_needs_resolve(arg_type) &&
2487 !env_type_is_resolved(env, arg_type_id)) {
2488 err = btf_resolve(env, arg_type, arg_type_id);
2493 if (!btf_type_id_size(btf, &arg_type_id, NULL)) {
2494 btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1);
2503 static int btf_func_check(struct btf_verifier_env *env,
2504 const struct btf_type *t)
2506 const struct btf_type *proto_type;
2507 const struct btf_param *args;
2508 const struct btf *btf;
2512 proto_type = btf_type_by_id(btf, t->type);
2514 if (!proto_type || !btf_type_is_func_proto(proto_type)) {
2515 btf_verifier_log_type(env, t, "Invalid type_id");
2519 args = (const struct btf_param *)(proto_type + 1);
2520 nr_args = btf_type_vlen(proto_type);
2521 for (i = 0; i < nr_args; i++) {
2522 if (!args[i].name_off && args[i].type) {
2523 btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1);
2531 static const struct btf_kind_operations * const kind_ops[NR_BTF_KINDS] = {
2532 [BTF_KIND_INT] = &int_ops,
2533 [BTF_KIND_PTR] = &ptr_ops,
2534 [BTF_KIND_ARRAY] = &array_ops,
2535 [BTF_KIND_STRUCT] = &struct_ops,
2536 [BTF_KIND_UNION] = &struct_ops,
2537 [BTF_KIND_ENUM] = &enum_ops,
2538 [BTF_KIND_FWD] = &fwd_ops,
2539 [BTF_KIND_TYPEDEF] = &modifier_ops,
2540 [BTF_KIND_VOLATILE] = &modifier_ops,
2541 [BTF_KIND_CONST] = &modifier_ops,
2542 [BTF_KIND_RESTRICT] = &modifier_ops,
2543 [BTF_KIND_FUNC] = &func_ops,
2544 [BTF_KIND_FUNC_PROTO] = &func_proto_ops,
2547 static s32 btf_check_meta(struct btf_verifier_env *env,
2548 const struct btf_type *t,
2551 u32 saved_meta_left = meta_left;
2554 if (meta_left < sizeof(*t)) {
2555 btf_verifier_log(env, "[%u] meta_left:%u meta_needed:%zu",
2556 env->log_type_id, meta_left, sizeof(*t));
2559 meta_left -= sizeof(*t);
2561 if (t->info & ~BTF_INFO_MASK) {
2562 btf_verifier_log(env, "[%u] Invalid btf_info:%x",
2563 env->log_type_id, t->info);
2567 if (BTF_INFO_KIND(t->info) > BTF_KIND_MAX ||
2568 BTF_INFO_KIND(t->info) == BTF_KIND_UNKN) {
2569 btf_verifier_log(env, "[%u] Invalid kind:%u",
2570 env->log_type_id, BTF_INFO_KIND(t->info));
2574 if (!btf_name_offset_valid(env->btf, t->name_off)) {
2575 btf_verifier_log(env, "[%u] Invalid name_offset:%u",
2576 env->log_type_id, t->name_off);
2580 var_meta_size = btf_type_ops(t)->check_meta(env, t, meta_left);
2581 if (var_meta_size < 0)
2582 return var_meta_size;
2584 meta_left -= var_meta_size;
2586 return saved_meta_left - meta_left;
2589 static int btf_check_all_metas(struct btf_verifier_env *env)
2591 struct btf *btf = env->btf;
2592 struct btf_header *hdr;
2596 cur = btf->nohdr_data + hdr->type_off;
2597 end = cur + hdr->type_len;
2599 env->log_type_id = 1;
2601 struct btf_type *t = cur;
2604 meta_size = btf_check_meta(env, t, end - cur);
2608 btf_add_type(env, t);
2616 static bool btf_resolve_valid(struct btf_verifier_env *env,
2617 const struct btf_type *t,
2620 struct btf *btf = env->btf;
2622 if (!env_type_is_resolved(env, type_id))
2625 if (btf_type_is_struct(t))
2626 return !btf->resolved_ids[type_id] &&
2627 !btf->resolved_sizes[type_id];
2629 if (btf_type_is_modifier(t) || btf_type_is_ptr(t)) {
2630 t = btf_type_id_resolve(btf, &type_id);
2631 return t && !btf_type_is_modifier(t);
2634 if (btf_type_is_array(t)) {
2635 const struct btf_array *array = btf_type_array(t);
2636 const struct btf_type *elem_type;
2637 u32 elem_type_id = array->type;
2640 elem_type = btf_type_id_size(btf, &elem_type_id, &elem_size);
2641 return elem_type && !btf_type_is_modifier(elem_type) &&
2642 (array->nelems * elem_size ==
2643 btf->resolved_sizes[type_id]);
2649 static int btf_resolve(struct btf_verifier_env *env,
2650 const struct btf_type *t, u32 type_id)
2652 u32 save_log_type_id = env->log_type_id;
2653 const struct resolve_vertex *v;
2656 env->resolve_mode = RESOLVE_TBD;
2657 env_stack_push(env, t, type_id);
2658 while (!err && (v = env_stack_peak(env))) {
2659 env->log_type_id = v->type_id;
2660 err = btf_type_ops(v->t)->resolve(env, v);
2663 env->log_type_id = type_id;
2664 if (err == -E2BIG) {
2665 btf_verifier_log_type(env, t,
2666 "Exceeded max resolving depth:%u",
2668 } else if (err == -EEXIST) {
2669 btf_verifier_log_type(env, t, "Loop detected");
2672 /* Final sanity check */
2673 if (!err && !btf_resolve_valid(env, t, type_id)) {
2674 btf_verifier_log_type(env, t, "Invalid resolve state");
2678 env->log_type_id = save_log_type_id;
2682 static int btf_check_all_types(struct btf_verifier_env *env)
2684 struct btf *btf = env->btf;
2688 err = env_resolve_init(env);
2693 for (type_id = 1; type_id <= btf->nr_types; type_id++) {
2694 const struct btf_type *t = btf_type_by_id(btf, type_id);
2696 env->log_type_id = type_id;
2697 if (btf_type_needs_resolve(t) &&
2698 !env_type_is_resolved(env, type_id)) {
2699 err = btf_resolve(env, t, type_id);
2704 if (btf_type_is_func_proto(t)) {
2705 err = btf_func_proto_check(env, t);
2710 if (btf_type_is_func(t)) {
2711 err = btf_func_check(env, t);
2720 static int btf_parse_type_sec(struct btf_verifier_env *env)
2722 const struct btf_header *hdr = &env->btf->hdr;
2725 /* Type section must align to 4 bytes */
2726 if (hdr->type_off & (sizeof(u32) - 1)) {
2727 btf_verifier_log(env, "Unaligned type_off");
2731 if (!hdr->type_len) {
2732 btf_verifier_log(env, "No type found");
2736 err = btf_check_all_metas(env);
2740 return btf_check_all_types(env);
2743 static int btf_parse_str_sec(struct btf_verifier_env *env)
2745 const struct btf_header *hdr;
2746 struct btf *btf = env->btf;
2747 const char *start, *end;
2750 start = btf->nohdr_data + hdr->str_off;
2751 end = start + hdr->str_len;
2753 if (end != btf->data + btf->data_size) {
2754 btf_verifier_log(env, "String section is not at the end");
2758 if (!hdr->str_len || hdr->str_len - 1 > BTF_MAX_NAME_OFFSET ||
2759 start[0] || end[-1]) {
2760 btf_verifier_log(env, "Invalid string section");
2764 btf->strings = start;
2769 static const size_t btf_sec_info_offset[] = {
2770 offsetof(struct btf_header, type_off),
2771 offsetof(struct btf_header, str_off),
2774 static int btf_sec_info_cmp(const void *a, const void *b)
2776 const struct btf_sec_info *x = a;
2777 const struct btf_sec_info *y = b;
2779 return (int)(x->off - y->off) ? : (int)(x->len - y->len);
2782 static int btf_check_sec_info(struct btf_verifier_env *env,
2785 struct btf_sec_info secs[ARRAY_SIZE(btf_sec_info_offset)];
2786 u32 total, expected_total, i;
2787 const struct btf_header *hdr;
2788 const struct btf *btf;
2793 /* Populate the secs from hdr */
2794 for (i = 0; i < ARRAY_SIZE(btf_sec_info_offset); i++)
2795 secs[i] = *(struct btf_sec_info *)((void *)hdr +
2796 btf_sec_info_offset[i]);
2798 sort(secs, ARRAY_SIZE(btf_sec_info_offset),
2799 sizeof(struct btf_sec_info), btf_sec_info_cmp, NULL);
2801 /* Check for gaps and overlap among sections */
2803 expected_total = btf_data_size - hdr->hdr_len;
2804 for (i = 0; i < ARRAY_SIZE(btf_sec_info_offset); i++) {
2805 if (expected_total < secs[i].off) {
2806 btf_verifier_log(env, "Invalid section offset");
2809 if (total < secs[i].off) {
2811 btf_verifier_log(env, "Unsupported section found");
2814 if (total > secs[i].off) {
2815 btf_verifier_log(env, "Section overlap found");
2818 if (expected_total - total < secs[i].len) {
2819 btf_verifier_log(env,
2820 "Total section length too long");
2823 total += secs[i].len;
2826 /* There is data other than hdr and known sections */
2827 if (expected_total != total) {
2828 btf_verifier_log(env, "Unsupported section found");
2835 static int btf_parse_hdr(struct btf_verifier_env *env)
2837 u32 hdr_len, hdr_copy, btf_data_size;
2838 const struct btf_header *hdr;
2843 btf_data_size = btf->data_size;
2846 offsetof(struct btf_header, hdr_len) + sizeof(hdr->hdr_len)) {
2847 btf_verifier_log(env, "hdr_len not found");
2852 hdr_len = hdr->hdr_len;
2853 if (btf_data_size < hdr_len) {
2854 btf_verifier_log(env, "btf_header not found");
2858 /* Ensure the unsupported header fields are zero */
2859 if (hdr_len > sizeof(btf->hdr)) {
2860 u8 *expected_zero = btf->data + sizeof(btf->hdr);
2861 u8 *end = btf->data + hdr_len;
2863 for (; expected_zero < end; expected_zero++) {
2864 if (*expected_zero) {
2865 btf_verifier_log(env, "Unsupported btf_header");
2871 hdr_copy = min_t(u32, hdr_len, sizeof(btf->hdr));
2872 memcpy(&btf->hdr, btf->data, hdr_copy);
2876 btf_verifier_log_hdr(env, btf_data_size);
2878 if (hdr->magic != BTF_MAGIC) {
2879 btf_verifier_log(env, "Invalid magic");
2883 if (hdr->version != BTF_VERSION) {
2884 btf_verifier_log(env, "Unsupported version");
2889 btf_verifier_log(env, "Unsupported flags");
2893 if (btf_data_size == hdr->hdr_len) {
2894 btf_verifier_log(env, "No data");
2898 err = btf_check_sec_info(env, btf_data_size);
2905 static struct btf *btf_parse(void __user *btf_data, u32 btf_data_size,
2906 u32 log_level, char __user *log_ubuf, u32 log_size)
2908 struct btf_verifier_env *env = NULL;
2909 struct bpf_verifier_log *log;
2910 struct btf *btf = NULL;
2914 if (btf_data_size > BTF_MAX_SIZE)
2915 return ERR_PTR(-E2BIG);
2917 env = kzalloc(sizeof(*env), GFP_KERNEL | __GFP_NOWARN);
2919 return ERR_PTR(-ENOMEM);
2922 if (log_level || log_ubuf || log_size) {
2923 /* user requested verbose verifier output
2924 * and supplied buffer to store the verification trace
2926 log->level = log_level;
2927 log->ubuf = log_ubuf;
2928 log->len_total = log_size;
2930 /* log attributes have to be sane */
2931 if (log->len_total < 128 || log->len_total > UINT_MAX >> 8 ||
2932 !log->level || !log->ubuf) {
2938 btf = kzalloc(sizeof(*btf), GFP_KERNEL | __GFP_NOWARN);
2945 data = kvmalloc(btf_data_size, GFP_KERNEL | __GFP_NOWARN);
2952 btf->data_size = btf_data_size;
2954 if (copy_from_user(data, btf_data, btf_data_size)) {
2959 err = btf_parse_hdr(env);
2963 btf->nohdr_data = btf->data + btf->hdr.hdr_len;
2965 err = btf_parse_str_sec(env);
2969 err = btf_parse_type_sec(env);
2973 if (log->level && bpf_verifier_log_full(log)) {
2978 btf_verifier_env_free(env);
2979 refcount_set(&btf->refcnt, 1);
2983 btf_verifier_env_free(env);
2986 return ERR_PTR(err);
2989 void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj,
2992 const struct btf_type *t = btf_type_by_id(btf, type_id);
2994 btf_type_ops(t)->seq_show(btf, t, type_id, obj, 0, m);
2997 static int btf_release(struct inode *inode, struct file *filp)
2999 btf_put(filp->private_data);
3003 const struct file_operations btf_fops = {
3004 .release = btf_release,
3007 static int __btf_new_fd(struct btf *btf)
3009 return anon_inode_getfd("btf", &btf_fops, btf, O_RDONLY | O_CLOEXEC);
3012 int btf_new_fd(const union bpf_attr *attr)
3017 btf = btf_parse(u64_to_user_ptr(attr->btf),
3018 attr->btf_size, attr->btf_log_level,
3019 u64_to_user_ptr(attr->btf_log_buf),
3020 attr->btf_log_size);
3022 return PTR_ERR(btf);
3024 ret = btf_alloc_id(btf);
3031 * The BTF ID is published to the userspace.
3032 * All BTF free must go through call_rcu() from
3033 * now on (i.e. free by calling btf_put()).
3036 ret = __btf_new_fd(btf);
3043 struct btf *btf_get_by_fd(int fd)
3051 return ERR_PTR(-EBADF);
3053 if (f.file->f_op != &btf_fops) {
3055 return ERR_PTR(-EINVAL);
3058 btf = f.file->private_data;
3059 refcount_inc(&btf->refcnt);
3065 int btf_get_info_by_fd(const struct btf *btf,
3066 const union bpf_attr *attr,
3067 union bpf_attr __user *uattr)
3069 struct bpf_btf_info __user *uinfo;
3070 struct bpf_btf_info info = {};
3071 u32 info_copy, btf_copy;
3075 uinfo = u64_to_user_ptr(attr->info.info);
3076 uinfo_len = attr->info.info_len;
3078 info_copy = min_t(u32, uinfo_len, sizeof(info));
3079 if (copy_from_user(&info, uinfo, info_copy))
3083 ubtf = u64_to_user_ptr(info.btf);
3084 btf_copy = min_t(u32, btf->data_size, info.btf_size);
3085 if (copy_to_user(ubtf, btf->data, btf_copy))
3087 info.btf_size = btf->data_size;
3089 if (copy_to_user(uinfo, &info, info_copy) ||
3090 put_user(info_copy, &uattr->info.info_len))
3096 int btf_get_fd_by_id(u32 id)
3102 btf = idr_find(&btf_idr, id);
3103 if (!btf || !refcount_inc_not_zero(&btf->refcnt))
3104 btf = ERR_PTR(-ENOENT);
3108 return PTR_ERR(btf);
3110 fd = __btf_new_fd(btf);
3117 u32 btf_id(const struct btf *btf)