xref: /openbmc/linux/kernel/bpf/btf.c (revision 16f6ccde74a6f8538c62f127f17207c75f4dba7a)
1c561d110STom Rix // SPDX-License-Identifier: GPL-2.0
269b693f0SMartin KaFai Lau /* Copyright (c) 2018 Facebook */
369b693f0SMartin KaFai Lau 
469b693f0SMartin KaFai Lau #include <uapi/linux/btf.h>
591cc1a99SAlexei Starovoitov #include <uapi/linux/bpf.h>
691cc1a99SAlexei Starovoitov #include <uapi/linux/bpf_perf_event.h>
769b693f0SMartin KaFai Lau #include <uapi/linux/types.h>
8b00b8daeSMartin KaFai Lau #include <linux/seq_file.h>
969b693f0SMartin KaFai Lau #include <linux/compiler.h>
102667a262SMartin KaFai Lau #include <linux/ctype.h>
1169b693f0SMartin KaFai Lau #include <linux/errno.h>
1269b693f0SMartin KaFai Lau #include <linux/slab.h>
13f56a653cSMartin KaFai Lau #include <linux/anon_inodes.h>
14f56a653cSMartin KaFai Lau #include <linux/file.h>
1569b693f0SMartin KaFai Lau #include <linux/uaccess.h>
1669b693f0SMartin KaFai Lau #include <linux/kernel.h>
1778958fcaSMartin KaFai Lau #include <linux/idr.h>
18f80442a4SMartin KaFai Lau #include <linux/sort.h>
1969b693f0SMartin KaFai Lau #include <linux/bpf_verifier.h>
2069b693f0SMartin KaFai Lau #include <linux/btf.h>
2149f4e672SJiri Olsa #include <linux/btf_ids.h>
22c0c852ddSYonghong Song #include <linux/bpf_lsm.h>
2391cc1a99SAlexei Starovoitov #include <linux/skmsg.h>
2491cc1a99SAlexei Starovoitov #include <linux/perf_event.h>
25eae2e83eSJiri Olsa #include <linux/bsearch.h>
2636e68442SAndrii Nakryiko #include <linux/kobject.h>
2736e68442SAndrii Nakryiko #include <linux/sysfs.h>
28fd9c663bSFlorian Westphal 
29fd9c663bSFlorian Westphal #include <net/netfilter/nf_bpf_link.h>
30fd9c663bSFlorian Westphal 
3191cc1a99SAlexei Starovoitov #include <net/sock.h>
32680ee045SJakub Kicinski #include <net/xdp.h>
331e89106dSAlexei Starovoitov #include "../tools/lib/bpf/relo_core.h"
3469b693f0SMartin KaFai Lau 
3569b693f0SMartin KaFai Lau /* BTF (BPF Type Format) is the meta data format which describes
3669b693f0SMartin KaFai Lau  * the data types of BPF program/map.  Hence, it basically focus
3769b693f0SMartin KaFai Lau  * on the C programming language which the modern BPF is primary
3869b693f0SMartin KaFai Lau  * using.
3969b693f0SMartin KaFai Lau  *
4069b693f0SMartin KaFai Lau  * ELF Section:
4169b693f0SMartin KaFai Lau  * ~~~~~~~~~~~
4269b693f0SMartin KaFai Lau  * The BTF data is stored under the ".BTF" ELF section
4369b693f0SMartin KaFai Lau  *
4469b693f0SMartin KaFai Lau  * struct btf_type:
4569b693f0SMartin KaFai Lau  * ~~~~~~~~~~~~~~~
4669b693f0SMartin KaFai Lau  * Each 'struct btf_type' object describes a C data type.
4769b693f0SMartin KaFai Lau  * Depending on the type it is describing, a 'struct btf_type'
4869b693f0SMartin KaFai Lau  * object may be followed by more data.  F.e.
4969b693f0SMartin KaFai Lau  * To describe an array, 'struct btf_type' is followed by
5069b693f0SMartin KaFai Lau  * 'struct btf_array'.
5169b693f0SMartin KaFai Lau  *
5269b693f0SMartin KaFai Lau  * 'struct btf_type' and any extra data following it are
5369b693f0SMartin KaFai Lau  * 4 bytes aligned.
5469b693f0SMartin KaFai Lau  *
5569b693f0SMartin KaFai Lau  * Type section:
5669b693f0SMartin KaFai Lau  * ~~~~~~~~~~~~~
5769b693f0SMartin KaFai Lau  * The BTF type section contains a list of 'struct btf_type' objects.
5869b693f0SMartin KaFai Lau  * Each one describes a C type.  Recall from the above section
5969b693f0SMartin KaFai Lau  * that a 'struct btf_type' object could be immediately followed by extra
608fb33b60SZhen Lei  * data in order to describe some particular C types.
6169b693f0SMartin KaFai Lau  *
6269b693f0SMartin KaFai Lau  * type_id:
6369b693f0SMartin KaFai Lau  * ~~~~~~~
6469b693f0SMartin KaFai Lau  * Each btf_type object is identified by a type_id.  The type_id
6569b693f0SMartin KaFai Lau  * is implicitly implied by the location of the btf_type object in
6669b693f0SMartin KaFai Lau  * the BTF type section.  The first one has type_id 1.  The second
6769b693f0SMartin KaFai Lau  * one has type_id 2...etc.  Hence, an earlier btf_type has
6869b693f0SMartin KaFai Lau  * a smaller type_id.
6969b693f0SMartin KaFai Lau  *
7069b693f0SMartin KaFai Lau  * A btf_type object may refer to another btf_type object by using
7169b693f0SMartin KaFai Lau  * type_id (i.e. the "type" in the "struct btf_type").
7269b693f0SMartin KaFai Lau  *
7369b693f0SMartin KaFai Lau  * NOTE that we cannot assume any reference-order.
7469b693f0SMartin KaFai Lau  * A btf_type object can refer to an earlier btf_type object
7569b693f0SMartin KaFai Lau  * but it can also refer to a later btf_type object.
7669b693f0SMartin KaFai Lau  *
7769b693f0SMartin KaFai Lau  * For example, to describe "const void *".  A btf_type
7869b693f0SMartin KaFai Lau  * object describing "const" may refer to another btf_type
7969b693f0SMartin KaFai Lau  * object describing "void *".  This type-reference is done
8069b693f0SMartin KaFai Lau  * by specifying type_id:
8169b693f0SMartin KaFai Lau  *
8269b693f0SMartin KaFai Lau  * [1] CONST (anon) type_id=2
8369b693f0SMartin KaFai Lau  * [2] PTR (anon) type_id=0
8469b693f0SMartin KaFai Lau  *
8569b693f0SMartin KaFai Lau  * The above is the btf_verifier debug log:
8669b693f0SMartin KaFai Lau  *   - Each line started with "[?]" is a btf_type object
8769b693f0SMartin KaFai Lau  *   - [?] is the type_id of the btf_type object.
8869b693f0SMartin KaFai Lau  *   - CONST/PTR is the BTF_KIND_XXX
8969b693f0SMartin KaFai Lau  *   - "(anon)" is the name of the type.  It just
9069b693f0SMartin KaFai Lau  *     happens that CONST and PTR has no name.
9169b693f0SMartin KaFai Lau  *   - type_id=XXX is the 'u32 type' in btf_type
9269b693f0SMartin KaFai Lau  *
9369b693f0SMartin KaFai Lau  * NOTE: "void" has type_id 0
9469b693f0SMartin KaFai Lau  *
9569b693f0SMartin KaFai Lau  * String section:
9669b693f0SMartin KaFai Lau  * ~~~~~~~~~~~~~~
9769b693f0SMartin KaFai Lau  * The BTF string section contains the names used by the type section.
9869b693f0SMartin KaFai Lau  * Each string is referred by an "offset" from the beginning of the
9969b693f0SMartin KaFai Lau  * string section.
10069b693f0SMartin KaFai Lau  *
10169b693f0SMartin KaFai Lau  * Each string is '\0' terminated.
10269b693f0SMartin KaFai Lau  *
10369b693f0SMartin KaFai Lau  * The first character in the string section must be '\0'
10469b693f0SMartin KaFai Lau  * which is used to mean 'anonymous'. Some btf_type may not
10569b693f0SMartin KaFai Lau  * have a name.
10669b693f0SMartin KaFai Lau  */
10769b693f0SMartin KaFai Lau 
10869b693f0SMartin KaFai Lau /* BTF verification:
10969b693f0SMartin KaFai Lau  *
11069b693f0SMartin KaFai Lau  * To verify BTF data, two passes are needed.
11169b693f0SMartin KaFai Lau  *
11269b693f0SMartin KaFai Lau  * Pass #1
11369b693f0SMartin KaFai Lau  * ~~~~~~~
11469b693f0SMartin KaFai Lau  * The first pass is to collect all btf_type objects to
11569b693f0SMartin KaFai Lau  * an array: "btf->types".
11669b693f0SMartin KaFai Lau  *
11769b693f0SMartin KaFai Lau  * Depending on the C type that a btf_type is describing,
11869b693f0SMartin KaFai Lau  * a btf_type may be followed by extra data.  We don't know
11969b693f0SMartin KaFai Lau  * how many btf_type is there, and more importantly we don't
12069b693f0SMartin KaFai Lau  * know where each btf_type is located in the type section.
12169b693f0SMartin KaFai Lau  *
12269b693f0SMartin KaFai Lau  * Without knowing the location of each type_id, most verifications
12369b693f0SMartin KaFai Lau  * cannot be done.  e.g. an earlier btf_type may refer to a later
12469b693f0SMartin KaFai Lau  * btf_type (recall the "const void *" above), so we cannot
12569b693f0SMartin KaFai Lau  * check this type-reference in the first pass.
12669b693f0SMartin KaFai Lau  *
12769b693f0SMartin KaFai Lau  * In the first pass, it still does some verifications (e.g.
12869b693f0SMartin KaFai Lau  * checking the name is a valid offset to the string section).
129eb3f595dSMartin KaFai Lau  *
130eb3f595dSMartin KaFai Lau  * Pass #2
131eb3f595dSMartin KaFai Lau  * ~~~~~~~
132eb3f595dSMartin KaFai Lau  * The main focus is to resolve a btf_type that is referring
133eb3f595dSMartin KaFai Lau  * to another type.
134eb3f595dSMartin KaFai Lau  *
135eb3f595dSMartin KaFai Lau  * We have to ensure the referring type:
136eb3f595dSMartin KaFai Lau  * 1) does exist in the BTF (i.e. in btf->types[])
137eb3f595dSMartin KaFai Lau  * 2) does not cause a loop:
138eb3f595dSMartin KaFai Lau  *	struct A {
139eb3f595dSMartin KaFai Lau  *		struct B b;
140eb3f595dSMartin KaFai Lau  *	};
141eb3f595dSMartin KaFai Lau  *
142eb3f595dSMartin KaFai Lau  *	struct B {
143eb3f595dSMartin KaFai Lau  *		struct A a;
144eb3f595dSMartin KaFai Lau  *	};
145eb3f595dSMartin KaFai Lau  *
146eb3f595dSMartin KaFai Lau  * btf_type_needs_resolve() decides if a btf_type needs
147eb3f595dSMartin KaFai Lau  * to be resolved.
148eb3f595dSMartin KaFai Lau  *
149eb3f595dSMartin KaFai Lau  * The needs_resolve type implements the "resolve()" ops which
150eb3f595dSMartin KaFai Lau  * essentially does a DFS and detects backedge.
151eb3f595dSMartin KaFai Lau  *
152eb3f595dSMartin KaFai Lau  * During resolve (or DFS), different C types have different
153eb3f595dSMartin KaFai Lau  * "RESOLVED" conditions.
154eb3f595dSMartin KaFai Lau  *
155eb3f595dSMartin KaFai Lau  * When resolving a BTF_KIND_STRUCT, we need to resolve all its
156eb3f595dSMartin KaFai Lau  * members because a member is always referring to another
157eb3f595dSMartin KaFai Lau  * type.  A struct's member can be treated as "RESOLVED" if
158eb3f595dSMartin KaFai Lau  * it is referring to a BTF_KIND_PTR.  Otherwise, the
159eb3f595dSMartin KaFai Lau  * following valid C struct would be rejected:
160eb3f595dSMartin KaFai Lau  *
161eb3f595dSMartin KaFai Lau  *	struct A {
162eb3f595dSMartin KaFai Lau  *		int m;
163eb3f595dSMartin KaFai Lau  *		struct A *a;
164eb3f595dSMartin KaFai Lau  *	};
165eb3f595dSMartin KaFai Lau  *
166eb3f595dSMartin KaFai Lau  * When resolving a BTF_KIND_PTR, it needs to keep resolving if
167eb3f595dSMartin KaFai Lau  * it is referring to another BTF_KIND_PTR.  Otherwise, we cannot
168eb3f595dSMartin KaFai Lau  * detect a pointer loop, e.g.:
169eb3f595dSMartin KaFai Lau  * BTF_KIND_CONST -> BTF_KIND_PTR -> BTF_KIND_CONST -> BTF_KIND_PTR +
170eb3f595dSMartin KaFai Lau  *                        ^                                         |
171eb3f595dSMartin KaFai Lau  *                        +-----------------------------------------+
172eb3f595dSMartin KaFai Lau  *
17369b693f0SMartin KaFai Lau  */
17469b693f0SMartin KaFai Lau 
175b1e8818cSYonghong Song #define BITS_PER_U128 (sizeof(u64) * BITS_PER_BYTE * 2)
17669b693f0SMartin KaFai Lau #define BITS_PER_BYTE_MASK (BITS_PER_BYTE - 1)
17769b693f0SMartin KaFai Lau #define BITS_PER_BYTE_MASKED(bits) ((bits) & BITS_PER_BYTE_MASK)
17869b693f0SMartin KaFai Lau #define BITS_ROUNDDOWN_BYTES(bits) ((bits) >> 3)
17969b693f0SMartin KaFai Lau #define BITS_ROUNDUP_BYTES(bits) \
18069b693f0SMartin KaFai Lau 	(BITS_ROUNDDOWN_BYTES(bits) + !!BITS_PER_BYTE_MASKED(bits))
18169b693f0SMartin KaFai Lau 
182b1828f0bSIlya Leoshkevich #define BTF_INFO_MASK 0x9f00ffff
183aea2f7b8SMartin KaFai Lau #define BTF_INT_MASK 0x0fffffff
184aea2f7b8SMartin KaFai Lau #define BTF_TYPE_ID_VALID(type_id) ((type_id) <= BTF_MAX_TYPE)
185aea2f7b8SMartin KaFai Lau #define BTF_STR_OFFSET_VALID(name_off) ((name_off) <= BTF_MAX_NAME_OFFSET)
186aea2f7b8SMartin KaFai Lau 
18769b693f0SMartin KaFai Lau /* 16MB for 64k structs and each has 16 members and
18869b693f0SMartin KaFai Lau  * a few MB spaces for the string section.
18969b693f0SMartin KaFai Lau  * The hard limit is S32_MAX.
19069b693f0SMartin KaFai Lau  */
19169b693f0SMartin KaFai Lau #define BTF_MAX_SIZE (16 * 1024 * 1024)
19269b693f0SMartin KaFai Lau 
193eb3f595dSMartin KaFai Lau #define for_each_member_from(i, from, struct_type, member)		\
194eb3f595dSMartin KaFai Lau 	for (i = from, member = btf_type_member(struct_type) + from;	\
195eb3f595dSMartin KaFai Lau 	     i < btf_type_vlen(struct_type);				\
196eb3f595dSMartin KaFai Lau 	     i++, member++)
197eb3f595dSMartin KaFai Lau 
1981dc92851SDaniel Borkmann #define for_each_vsi_from(i, from, struct_type, member)				\
1991dc92851SDaniel Borkmann 	for (i = from, member = btf_type_var_secinfo(struct_type) + from;	\
2001dc92851SDaniel Borkmann 	     i < btf_type_vlen(struct_type);					\
2011dc92851SDaniel Borkmann 	     i++, member++)
2021dc92851SDaniel Borkmann 
2031b9ed84eSQuentin Monnet DEFINE_IDR(btf_idr);
2041b9ed84eSQuentin Monnet DEFINE_SPINLOCK(btf_idr_lock);
20578958fcaSMartin KaFai Lau 
206dee872e1SKumar Kartikeya Dwivedi enum btf_kfunc_hook {
207cfe14564SYonghong Song 	BTF_KFUNC_HOOK_COMMON,
208dee872e1SKumar Kartikeya Dwivedi 	BTF_KFUNC_HOOK_XDP,
209dee872e1SKumar Kartikeya Dwivedi 	BTF_KFUNC_HOOK_TC,
210dee872e1SKumar Kartikeya Dwivedi 	BTF_KFUNC_HOOK_STRUCT_OPS,
21197949767SBenjamin Tissoires 	BTF_KFUNC_HOOK_TRACING,
21297949767SBenjamin Tissoires 	BTF_KFUNC_HOOK_SYSCALL,
2135b481acaSBenjamin Tissoires 	BTF_KFUNC_HOOK_FMODRET,
214b5964b96SJoanne Koong 	BTF_KFUNC_HOOK_CGROUP_SKB,
215b5964b96SJoanne Koong 	BTF_KFUNC_HOOK_SCHED_ACT,
216b5964b96SJoanne Koong 	BTF_KFUNC_HOOK_SK_SKB,
217b5964b96SJoanne Koong 	BTF_KFUNC_HOOK_SOCKET_FILTER,
218b5964b96SJoanne Koong 	BTF_KFUNC_HOOK_LWT,
219fd9c663bSFlorian Westphal 	BTF_KFUNC_HOOK_NETFILTER,
220dee872e1SKumar Kartikeya Dwivedi 	BTF_KFUNC_HOOK_MAX,
221dee872e1SKumar Kartikeya Dwivedi };
222dee872e1SKumar Kartikeya Dwivedi 
223dee872e1SKumar Kartikeya Dwivedi enum {
224f9b34818SBenjamin Tissoires 	BTF_KFUNC_SET_MAX_CNT = 256,
2255ce937d6SKumar Kartikeya Dwivedi 	BTF_DTOR_KFUNC_MAX_CNT = 256,
226e924e80eSAditi Ghag 	BTF_KFUNC_FILTER_MAX_CNT = 16,
227e924e80eSAditi Ghag };
228e924e80eSAditi Ghag 
229e924e80eSAditi Ghag struct btf_kfunc_hook_filter {
230e924e80eSAditi Ghag 	btf_kfunc_filter_t filters[BTF_KFUNC_FILTER_MAX_CNT];
231e924e80eSAditi Ghag 	u32 nr_filters;
232dee872e1SKumar Kartikeya Dwivedi };
233dee872e1SKumar Kartikeya Dwivedi 
234dee872e1SKumar Kartikeya Dwivedi struct btf_kfunc_set_tab {
235a4703e31SKumar Kartikeya Dwivedi 	struct btf_id_set8 *sets[BTF_KFUNC_HOOK_MAX];
236e924e80eSAditi Ghag 	struct btf_kfunc_hook_filter hook_filters[BTF_KFUNC_HOOK_MAX];
237dee872e1SKumar Kartikeya Dwivedi };
238dee872e1SKumar Kartikeya Dwivedi 
2395ce937d6SKumar Kartikeya Dwivedi struct btf_id_dtor_kfunc_tab {
2405ce937d6SKumar Kartikeya Dwivedi 	u32 cnt;
2415ce937d6SKumar Kartikeya Dwivedi 	struct btf_id_dtor_kfunc dtors[];
2425ce937d6SKumar Kartikeya Dwivedi };
2435ce937d6SKumar Kartikeya Dwivedi 
24469b693f0SMartin KaFai Lau struct btf {
24569b693f0SMartin KaFai Lau 	void *data;
24669b693f0SMartin KaFai Lau 	struct btf_type **types;
247eb3f595dSMartin KaFai Lau 	u32 *resolved_ids;
248eb3f595dSMartin KaFai Lau 	u32 *resolved_sizes;
24969b693f0SMartin KaFai Lau 	const char *strings;
25069b693f0SMartin KaFai Lau 	void *nohdr_data;
251f80442a4SMartin KaFai Lau 	struct btf_header hdr;
252951bb646SAndrii Nakryiko 	u32 nr_types; /* includes VOID for base BTF */
25369b693f0SMartin KaFai Lau 	u32 types_size;
25469b693f0SMartin KaFai Lau 	u32 data_size;
255f56a653cSMartin KaFai Lau 	refcount_t refcnt;
25678958fcaSMartin KaFai Lau 	u32 id;
25778958fcaSMartin KaFai Lau 	struct rcu_head rcu;
258dee872e1SKumar Kartikeya Dwivedi 	struct btf_kfunc_set_tab *kfunc_set_tab;
2595ce937d6SKumar Kartikeya Dwivedi 	struct btf_id_dtor_kfunc_tab *dtor_kfunc_tab;
2608ffa5cc1SKumar Kartikeya Dwivedi 	struct btf_struct_metas *struct_meta_tab;
261951bb646SAndrii Nakryiko 
262951bb646SAndrii Nakryiko 	/* split BTF support */
263951bb646SAndrii Nakryiko 	struct btf *base_btf;
264951bb646SAndrii Nakryiko 	u32 start_id; /* first type ID in this BTF (0 for base BTF) */
265951bb646SAndrii Nakryiko 	u32 start_str_off; /* first string offset (0 for base BTF) */
26653297220SAndrii Nakryiko 	char name[MODULE_NAME_LEN];
26753297220SAndrii Nakryiko 	bool kernel_btf;
26869b693f0SMartin KaFai Lau };
26969b693f0SMartin KaFai Lau 
270eb3f595dSMartin KaFai Lau enum verifier_phase {
271eb3f595dSMartin KaFai Lau 	CHECK_META,
272eb3f595dSMartin KaFai Lau 	CHECK_TYPE,
273eb3f595dSMartin KaFai Lau };
274eb3f595dSMartin KaFai Lau 
275eb3f595dSMartin KaFai Lau struct resolve_vertex {
276eb3f595dSMartin KaFai Lau 	const struct btf_type *t;
277eb3f595dSMartin KaFai Lau 	u32 type_id;
278eb3f595dSMartin KaFai Lau 	u16 next_member;
279eb3f595dSMartin KaFai Lau };
280eb3f595dSMartin KaFai Lau 
281eb3f595dSMartin KaFai Lau enum visit_state {
282eb3f595dSMartin KaFai Lau 	NOT_VISITED,
283eb3f595dSMartin KaFai Lau 	VISITED,
284eb3f595dSMartin KaFai Lau 	RESOLVED,
285eb3f595dSMartin KaFai Lau };
286eb3f595dSMartin KaFai Lau 
287eb3f595dSMartin KaFai Lau enum resolve_mode {
288eb3f595dSMartin KaFai Lau 	RESOLVE_TBD,	/* To Be Determined */
289eb3f595dSMartin KaFai Lau 	RESOLVE_PTR,	/* Resolving for Pointer */
290eb3f595dSMartin KaFai Lau 	RESOLVE_STRUCT_OR_ARRAY,	/* Resolving for struct/union
291eb3f595dSMartin KaFai Lau 					 * or array
292eb3f595dSMartin KaFai Lau 					 */
293eb3f595dSMartin KaFai Lau };
294eb3f595dSMartin KaFai Lau 
295eb3f595dSMartin KaFai Lau #define MAX_RESOLVE_DEPTH 32
296eb3f595dSMartin KaFai Lau 
297f80442a4SMartin KaFai Lau struct btf_sec_info {
298f80442a4SMartin KaFai Lau 	u32 off;
299f80442a4SMartin KaFai Lau 	u32 len;
300f80442a4SMartin KaFai Lau };
301f80442a4SMartin KaFai Lau 
30269b693f0SMartin KaFai Lau struct btf_verifier_env {
30369b693f0SMartin KaFai Lau 	struct btf *btf;
304eb3f595dSMartin KaFai Lau 	u8 *visit_states;
305eb3f595dSMartin KaFai Lau 	struct resolve_vertex stack[MAX_RESOLVE_DEPTH];
30669b693f0SMartin KaFai Lau 	struct bpf_verifier_log log;
30769b693f0SMartin KaFai Lau 	u32 log_type_id;
308eb3f595dSMartin KaFai Lau 	u32 top_stack;
309eb3f595dSMartin KaFai Lau 	enum verifier_phase phase;
310eb3f595dSMartin KaFai Lau 	enum resolve_mode resolve_mode;
31169b693f0SMartin KaFai Lau };
31269b693f0SMartin KaFai Lau 
31369b693f0SMartin KaFai Lau static const char * const btf_kind_str[NR_BTF_KINDS] = {
31469b693f0SMartin KaFai Lau 	[BTF_KIND_UNKN]		= "UNKNOWN",
31569b693f0SMartin KaFai Lau 	[BTF_KIND_INT]		= "INT",
31669b693f0SMartin KaFai Lau 	[BTF_KIND_PTR]		= "PTR",
31769b693f0SMartin KaFai Lau 	[BTF_KIND_ARRAY]	= "ARRAY",
31869b693f0SMartin KaFai Lau 	[BTF_KIND_STRUCT]	= "STRUCT",
31969b693f0SMartin KaFai Lau 	[BTF_KIND_UNION]	= "UNION",
32069b693f0SMartin KaFai Lau 	[BTF_KIND_ENUM]		= "ENUM",
32169b693f0SMartin KaFai Lau 	[BTF_KIND_FWD]		= "FWD",
32269b693f0SMartin KaFai Lau 	[BTF_KIND_TYPEDEF]	= "TYPEDEF",
32369b693f0SMartin KaFai Lau 	[BTF_KIND_VOLATILE]	= "VOLATILE",
32469b693f0SMartin KaFai Lau 	[BTF_KIND_CONST]	= "CONST",
32569b693f0SMartin KaFai Lau 	[BTF_KIND_RESTRICT]	= "RESTRICT",
3262667a262SMartin KaFai Lau 	[BTF_KIND_FUNC]		= "FUNC",
3272667a262SMartin KaFai Lau 	[BTF_KIND_FUNC_PROTO]	= "FUNC_PROTO",
3281dc92851SDaniel Borkmann 	[BTF_KIND_VAR]		= "VAR",
3291dc92851SDaniel Borkmann 	[BTF_KIND_DATASEC]	= "DATASEC",
330b1828f0bSIlya Leoshkevich 	[BTF_KIND_FLOAT]	= "FLOAT",
331223f903eSYonghong Song 	[BTF_KIND_DECL_TAG]	= "DECL_TAG",
3328c42d2faSYonghong Song 	[BTF_KIND_TYPE_TAG]	= "TYPE_TAG",
3336089fb32SYonghong Song 	[BTF_KIND_ENUM64]	= "ENUM64",
33469b693f0SMartin KaFai Lau };
33569b693f0SMartin KaFai Lau 
btf_type_str(const struct btf_type * t)336e6ac2450SMartin KaFai Lau const char *btf_type_str(const struct btf_type *t)
337be8704ffSAlexei Starovoitov {
338be8704ffSAlexei Starovoitov 	return btf_kind_str[BTF_INFO_KIND(t->info)];
339be8704ffSAlexei Starovoitov }
340be8704ffSAlexei Starovoitov 
34131d0bc81SAlan Maguire /* Chunk size we use in safe copy of data to be shown. */
34231d0bc81SAlan Maguire #define BTF_SHOW_OBJ_SAFE_SIZE		32
34331d0bc81SAlan Maguire 
34431d0bc81SAlan Maguire /*
34531d0bc81SAlan Maguire  * This is the maximum size of a base type value (equivalent to a
34631d0bc81SAlan Maguire  * 128-bit int); if we are at the end of our safe buffer and have
34731d0bc81SAlan Maguire  * less than 16 bytes space we can't be assured of being able
34831d0bc81SAlan Maguire  * to copy the next type safely, so in such cases we will initiate
34931d0bc81SAlan Maguire  * a new copy.
35031d0bc81SAlan Maguire  */
35131d0bc81SAlan Maguire #define BTF_SHOW_OBJ_BASE_TYPE_SIZE	16
35231d0bc81SAlan Maguire 
35331d0bc81SAlan Maguire /* Type name size */
35431d0bc81SAlan Maguire #define BTF_SHOW_NAME_SIZE		80
35531d0bc81SAlan Maguire 
35631d0bc81SAlan Maguire /*
357b613d335SDavid Vernet  * The suffix of a type that indicates it cannot alias another type when
358b613d335SDavid Vernet  * comparing BTF IDs for kfunc invocations.
359b613d335SDavid Vernet  */
360b613d335SDavid Vernet #define NOCAST_ALIAS_SUFFIX		"___init"
361b613d335SDavid Vernet 
362b613d335SDavid Vernet /*
36331d0bc81SAlan Maguire  * Common data to all BTF show operations. Private show functions can add
36431d0bc81SAlan Maguire  * their own data to a structure containing a struct btf_show and consult it
36531d0bc81SAlan Maguire  * in the show callback.  See btf_type_show() below.
36631d0bc81SAlan Maguire  *
36731d0bc81SAlan Maguire  * One challenge with showing nested data is we want to skip 0-valued
36831d0bc81SAlan Maguire  * data, but in order to figure out whether a nested object is all zeros
36931d0bc81SAlan Maguire  * we need to walk through it.  As a result, we need to make two passes
37031d0bc81SAlan Maguire  * when handling structs, unions and arrays; the first path simply looks
37131d0bc81SAlan Maguire  * for nonzero data, while the second actually does the display.  The first
37231d0bc81SAlan Maguire  * pass is signalled by show->state.depth_check being set, and if we
37331d0bc81SAlan Maguire  * encounter a non-zero value we set show->state.depth_to_show to
37431d0bc81SAlan Maguire  * the depth at which we encountered it.  When we have completed the
37531d0bc81SAlan Maguire  * first pass, we will know if anything needs to be displayed if
37631d0bc81SAlan Maguire  * depth_to_show > depth.  See btf_[struct,array]_show() for the
37731d0bc81SAlan Maguire  * implementation of this.
37831d0bc81SAlan Maguire  *
37931d0bc81SAlan Maguire  * Another problem is we want to ensure the data for display is safe to
38031d0bc81SAlan Maguire  * access.  To support this, the anonymous "struct {} obj" tracks the data
38131d0bc81SAlan Maguire  * object and our safe copy of it.  We copy portions of the data needed
38231d0bc81SAlan Maguire  * to the object "copy" buffer, but because its size is limited to
38331d0bc81SAlan Maguire  * BTF_SHOW_OBJ_COPY_LEN bytes, multiple copies may be required as we
38431d0bc81SAlan Maguire  * traverse larger objects for display.
38531d0bc81SAlan Maguire  *
38631d0bc81SAlan Maguire  * The various data type show functions all start with a call to
38731d0bc81SAlan Maguire  * btf_show_start_type() which returns a pointer to the safe copy
38831d0bc81SAlan Maguire  * of the data needed (or if BTF_SHOW_UNSAFE is specified, to the
38931d0bc81SAlan Maguire  * raw data itself).  btf_show_obj_safe() is responsible for
39031d0bc81SAlan Maguire  * using copy_from_kernel_nofault() to update the safe data if necessary
39131d0bc81SAlan Maguire  * as we traverse the object's data.  skbuff-like semantics are
39231d0bc81SAlan Maguire  * used:
39331d0bc81SAlan Maguire  *
39431d0bc81SAlan Maguire  * - obj.head points to the start of the toplevel object for display
39531d0bc81SAlan Maguire  * - obj.size is the size of the toplevel object
39631d0bc81SAlan Maguire  * - obj.data points to the current point in the original data at
39731d0bc81SAlan Maguire  *   which our safe data starts.  obj.data will advance as we copy
39831d0bc81SAlan Maguire  *   portions of the data.
39931d0bc81SAlan Maguire  *
40031d0bc81SAlan Maguire  * In most cases a single copy will suffice, but larger data structures
40131d0bc81SAlan Maguire  * such as "struct task_struct" will require many copies.  The logic in
40231d0bc81SAlan Maguire  * btf_show_obj_safe() handles the logic that determines if a new
40331d0bc81SAlan Maguire  * copy_from_kernel_nofault() is needed.
40431d0bc81SAlan Maguire  */
40531d0bc81SAlan Maguire struct btf_show {
40631d0bc81SAlan Maguire 	u64 flags;
40731d0bc81SAlan Maguire 	void *target;	/* target of show operation (seq file, buffer) */
40833a1321fSAlan Maguire 	__printf(2, 0) void (*showfn)(struct btf_show *show, const char *fmt, va_list args);
40931d0bc81SAlan Maguire 	const struct btf *btf;
41031d0bc81SAlan Maguire 	/* below are used during iteration */
41131d0bc81SAlan Maguire 	struct {
41231d0bc81SAlan Maguire 		u8 depth;
41331d0bc81SAlan Maguire 		u8 depth_to_show;
41431d0bc81SAlan Maguire 		u8 depth_check;
41531d0bc81SAlan Maguire 		u8 array_member:1,
41631d0bc81SAlan Maguire 		   array_terminated:1;
41731d0bc81SAlan Maguire 		u16 array_encoding;
41831d0bc81SAlan Maguire 		u32 type_id;
41931d0bc81SAlan Maguire 		int status;			/* non-zero for error */
42031d0bc81SAlan Maguire 		const struct btf_type *type;
42131d0bc81SAlan Maguire 		const struct btf_member *member;
42231d0bc81SAlan Maguire 		char name[BTF_SHOW_NAME_SIZE];	/* space for member name/type */
42331d0bc81SAlan Maguire 	} state;
42431d0bc81SAlan Maguire 	struct {
42531d0bc81SAlan Maguire 		u32 size;
42631d0bc81SAlan Maguire 		void *head;
42731d0bc81SAlan Maguire 		void *data;
42831d0bc81SAlan Maguire 		u8 safe[BTF_SHOW_OBJ_SAFE_SIZE];
42931d0bc81SAlan Maguire 	} obj;
43031d0bc81SAlan Maguire };
43131d0bc81SAlan Maguire 
43269b693f0SMartin KaFai Lau struct btf_kind_operations {
43369b693f0SMartin KaFai Lau 	s32 (*check_meta)(struct btf_verifier_env *env,
43469b693f0SMartin KaFai Lau 			  const struct btf_type *t,
43569b693f0SMartin KaFai Lau 			  u32 meta_left);
436eb3f595dSMartin KaFai Lau 	int (*resolve)(struct btf_verifier_env *env,
437eb3f595dSMartin KaFai Lau 		       const struct resolve_vertex *v);
438179cde8cSMartin KaFai Lau 	int (*check_member)(struct btf_verifier_env *env,
439179cde8cSMartin KaFai Lau 			    const struct btf_type *struct_type,
440179cde8cSMartin KaFai Lau 			    const struct btf_member *member,
441179cde8cSMartin KaFai Lau 			    const struct btf_type *member_type);
4429d5f9f70SYonghong Song 	int (*check_kflag_member)(struct btf_verifier_env *env,
4439d5f9f70SYonghong Song 				  const struct btf_type *struct_type,
4449d5f9f70SYonghong Song 				  const struct btf_member *member,
4459d5f9f70SYonghong Song 				  const struct btf_type *member_type);
44669b693f0SMartin KaFai Lau 	void (*log_details)(struct btf_verifier_env *env,
44769b693f0SMartin KaFai Lau 			    const struct btf_type *t);
44831d0bc81SAlan Maguire 	void (*show)(const struct btf *btf, const struct btf_type *t,
449b00b8daeSMartin KaFai Lau 			 u32 type_id, void *data, u8 bits_offsets,
45031d0bc81SAlan Maguire 			 struct btf_show *show);
45169b693f0SMartin KaFai Lau };
45269b693f0SMartin KaFai Lau 
45369b693f0SMartin KaFai Lau static const struct btf_kind_operations * const kind_ops[NR_BTF_KINDS];
45469b693f0SMartin KaFai Lau static struct btf_type btf_void;
45569b693f0SMartin KaFai Lau 
4562667a262SMartin KaFai Lau static int btf_resolve(struct btf_verifier_env *env,
4572667a262SMartin KaFai Lau 		       const struct btf_type *t, u32 type_id);
4582667a262SMartin KaFai Lau 
459d7e7b42fSYonghong Song static int btf_func_check(struct btf_verifier_env *env,
460d7e7b42fSYonghong Song 			  const struct btf_type *t);
461d7e7b42fSYonghong Song 
btf_type_is_modifier(const struct btf_type * t)462eb3f595dSMartin KaFai Lau static bool btf_type_is_modifier(const struct btf_type *t)
463eb3f595dSMartin KaFai Lau {
464eb3f595dSMartin KaFai Lau 	/* Some of them is not strictly a C modifier
465eb3f595dSMartin KaFai Lau 	 * but they are grouped into the same bucket
466eb3f595dSMartin KaFai Lau 	 * for BTF concern:
467eb3f595dSMartin KaFai Lau 	 *   A type (t) that refers to another
468eb3f595dSMartin KaFai Lau 	 *   type through t->type AND its size cannot
469eb3f595dSMartin KaFai Lau 	 *   be determined without following the t->type.
470eb3f595dSMartin KaFai Lau 	 *
471eb3f595dSMartin KaFai Lau 	 * ptr does not fall into this bucket
472eb3f595dSMartin KaFai Lau 	 * because its size is always sizeof(void *).
473eb3f595dSMartin KaFai Lau 	 */
474eb3f595dSMartin KaFai Lau 	switch (BTF_INFO_KIND(t->info)) {
475eb3f595dSMartin KaFai Lau 	case BTF_KIND_TYPEDEF:
476eb3f595dSMartin KaFai Lau 	case BTF_KIND_VOLATILE:
477eb3f595dSMartin KaFai Lau 	case BTF_KIND_CONST:
478eb3f595dSMartin KaFai Lau 	case BTF_KIND_RESTRICT:
4798c42d2faSYonghong Song 	case BTF_KIND_TYPE_TAG:
480eb3f595dSMartin KaFai Lau 		return true;
481eb3f595dSMartin KaFai Lau 	}
482eb3f595dSMartin KaFai Lau 
483eb3f595dSMartin KaFai Lau 	return false;
484eb3f595dSMartin KaFai Lau }
485eb3f595dSMartin KaFai Lau 
btf_type_is_void(const struct btf_type * t)4862824ecb7SDaniel Borkmann bool btf_type_is_void(const struct btf_type *t)
487eb3f595dSMartin KaFai Lau {
488b47a0bd2SMartin KaFai Lau 	return t == &btf_void;
489eb3f595dSMartin KaFai Lau }
490eb3f595dSMartin KaFai Lau 
btf_type_is_fwd(const struct btf_type * t)491b47a0bd2SMartin KaFai Lau static bool btf_type_is_fwd(const struct btf_type *t)
492eb3f595dSMartin KaFai Lau {
493b47a0bd2SMartin KaFai Lau 	return BTF_INFO_KIND(t->info) == BTF_KIND_FWD;
494b47a0bd2SMartin KaFai Lau }
495b47a0bd2SMartin KaFai Lau 
btf_type_is_datasec(const struct btf_type * t)4961dc92851SDaniel Borkmann static bool btf_type_is_datasec(const struct btf_type *t)
4971dc92851SDaniel Borkmann {
4981dc92851SDaniel Borkmann 	return BTF_INFO_KIND(t->info) == BTF_KIND_DATASEC;
4991dc92851SDaniel Borkmann }
5001dc92851SDaniel Borkmann 
btf_type_is_decl_tag(const struct btf_type * t)501223f903eSYonghong Song static bool btf_type_is_decl_tag(const struct btf_type *t)
502b5ea834dSYonghong Song {
503223f903eSYonghong Song 	return BTF_INFO_KIND(t->info) == BTF_KIND_DECL_TAG;
504b5ea834dSYonghong Song }
505b5ea834dSYonghong Song 
btf_type_nosize(const struct btf_type * t)506e6c2f594SYonghong Song static bool btf_type_nosize(const struct btf_type *t)
507e6c2f594SYonghong Song {
508e6c2f594SYonghong Song 	return btf_type_is_void(t) || btf_type_is_fwd(t) ||
509e6c2f594SYonghong Song 	       btf_type_is_func(t) || btf_type_is_func_proto(t) ||
510e6c2f594SYonghong Song 	       btf_type_is_decl_tag(t);
511e6c2f594SYonghong Song }
512e6c2f594SYonghong Song 
btf_type_nosize_or_null(const struct btf_type * t)513e6c2f594SYonghong Song static bool btf_type_nosize_or_null(const struct btf_type *t)
514e6c2f594SYonghong Song {
515e6c2f594SYonghong Song 	return !t || btf_type_nosize(t);
516e6c2f594SYonghong Song }
517e6c2f594SYonghong Song 
btf_type_is_decl_tag_target(const struct btf_type * t)518223f903eSYonghong Song static bool btf_type_is_decl_tag_target(const struct btf_type *t)
519b5ea834dSYonghong Song {
520b5ea834dSYonghong Song 	return btf_type_is_func(t) || btf_type_is_struct(t) ||
521bd16dee6SYonghong Song 	       btf_type_is_var(t) || btf_type_is_typedef(t);
522b5ea834dSYonghong Song }
523b5ea834dSYonghong Song 
btf_nr_types(const struct btf * btf)524541c3badSAndrii Nakryiko u32 btf_nr_types(const struct btf *btf)
525951bb646SAndrii Nakryiko {
526951bb646SAndrii Nakryiko 	u32 total = 0;
527951bb646SAndrii Nakryiko 
528951bb646SAndrii Nakryiko 	while (btf) {
529951bb646SAndrii Nakryiko 		total += btf->nr_types;
530951bb646SAndrii Nakryiko 		btf = btf->base_btf;
531951bb646SAndrii Nakryiko 	}
532951bb646SAndrii Nakryiko 
533951bb646SAndrii Nakryiko 	return total;
534951bb646SAndrii Nakryiko }
535951bb646SAndrii Nakryiko 
btf_find_by_name_kind(const struct btf * btf,const char * name,u8 kind)53627ae7997SMartin KaFai Lau s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind)
53727ae7997SMartin KaFai Lau {
53827ae7997SMartin KaFai Lau 	const struct btf_type *t;
53927ae7997SMartin KaFai Lau 	const char *tname;
540951bb646SAndrii Nakryiko 	u32 i, total;
54127ae7997SMartin KaFai Lau 
542541c3badSAndrii Nakryiko 	total = btf_nr_types(btf);
543951bb646SAndrii Nakryiko 	for (i = 1; i < total; i++) {
544951bb646SAndrii Nakryiko 		t = btf_type_by_id(btf, i);
54527ae7997SMartin KaFai Lau 		if (BTF_INFO_KIND(t->info) != kind)
54627ae7997SMartin KaFai Lau 			continue;
54727ae7997SMartin KaFai Lau 
54827ae7997SMartin KaFai Lau 		tname = btf_name_by_offset(btf, t->name_off);
54927ae7997SMartin KaFai Lau 		if (!strcmp(tname, name))
55027ae7997SMartin KaFai Lau 			return i;
55127ae7997SMartin KaFai Lau 	}
55227ae7997SMartin KaFai Lau 
55327ae7997SMartin KaFai Lau 	return -ENOENT;
55427ae7997SMartin KaFai Lau }
55527ae7997SMartin KaFai Lau 
bpf_find_btf_id(const char * name,u32 kind,struct btf ** btf_p)556b1d1e904SMasami Hiramatsu (Google) s32 bpf_find_btf_id(const char *name, u32 kind, struct btf **btf_p)
557edc3ec09SKumar Kartikeya Dwivedi {
558edc3ec09SKumar Kartikeya Dwivedi 	struct btf *btf;
559edc3ec09SKumar Kartikeya Dwivedi 	s32 ret;
560edc3ec09SKumar Kartikeya Dwivedi 	int id;
561edc3ec09SKumar Kartikeya Dwivedi 
562edc3ec09SKumar Kartikeya Dwivedi 	btf = bpf_get_btf_vmlinux();
563edc3ec09SKumar Kartikeya Dwivedi 	if (IS_ERR(btf))
564edc3ec09SKumar Kartikeya Dwivedi 		return PTR_ERR(btf);
5657ada3787SKumar Kartikeya Dwivedi 	if (!btf)
5667ada3787SKumar Kartikeya Dwivedi 		return -EINVAL;
567edc3ec09SKumar Kartikeya Dwivedi 
568edc3ec09SKumar Kartikeya Dwivedi 	ret = btf_find_by_name_kind(btf, name, kind);
569edc3ec09SKumar Kartikeya Dwivedi 	/* ret is never zero, since btf_find_by_name_kind returns
570edc3ec09SKumar Kartikeya Dwivedi 	 * positive btf_id or negative error.
571edc3ec09SKumar Kartikeya Dwivedi 	 */
572edc3ec09SKumar Kartikeya Dwivedi 	if (ret > 0) {
573edc3ec09SKumar Kartikeya Dwivedi 		btf_get(btf);
574edc3ec09SKumar Kartikeya Dwivedi 		*btf_p = btf;
575edc3ec09SKumar Kartikeya Dwivedi 		return ret;
576edc3ec09SKumar Kartikeya Dwivedi 	}
577edc3ec09SKumar Kartikeya Dwivedi 
578edc3ec09SKumar Kartikeya Dwivedi 	/* If name is not found in vmlinux's BTF then search in module's BTFs */
579edc3ec09SKumar Kartikeya Dwivedi 	spin_lock_bh(&btf_idr_lock);
580edc3ec09SKumar Kartikeya Dwivedi 	idr_for_each_entry(&btf_idr, btf, id) {
581edc3ec09SKumar Kartikeya Dwivedi 		if (!btf_is_module(btf))
582edc3ec09SKumar Kartikeya Dwivedi 			continue;
583edc3ec09SKumar Kartikeya Dwivedi 		/* linear search could be slow hence unlock/lock
584edc3ec09SKumar Kartikeya Dwivedi 		 * the IDR to avoiding holding it for too long
585edc3ec09SKumar Kartikeya Dwivedi 		 */
586edc3ec09SKumar Kartikeya Dwivedi 		btf_get(btf);
587edc3ec09SKumar Kartikeya Dwivedi 		spin_unlock_bh(&btf_idr_lock);
588edc3ec09SKumar Kartikeya Dwivedi 		ret = btf_find_by_name_kind(btf, name, kind);
589edc3ec09SKumar Kartikeya Dwivedi 		if (ret > 0) {
590edc3ec09SKumar Kartikeya Dwivedi 			*btf_p = btf;
591edc3ec09SKumar Kartikeya Dwivedi 			return ret;
592edc3ec09SKumar Kartikeya Dwivedi 		}
593edc3ec09SKumar Kartikeya Dwivedi 		btf_put(btf);
594acf1c3d6SAlexei Starovoitov 		spin_lock_bh(&btf_idr_lock);
595edc3ec09SKumar Kartikeya Dwivedi 	}
596edc3ec09SKumar Kartikeya Dwivedi 	spin_unlock_bh(&btf_idr_lock);
597edc3ec09SKumar Kartikeya Dwivedi 	return ret;
598edc3ec09SKumar Kartikeya Dwivedi }
599edc3ec09SKumar Kartikeya Dwivedi 
btf_type_skip_modifiers(const struct btf * btf,u32 id,u32 * res_id)60027ae7997SMartin KaFai Lau const struct btf_type *btf_type_skip_modifiers(const struct btf *btf,
60127ae7997SMartin KaFai Lau 					       u32 id, u32 *res_id)
60227ae7997SMartin KaFai Lau {
60327ae7997SMartin KaFai Lau 	const struct btf_type *t = btf_type_by_id(btf, id);
60427ae7997SMartin KaFai Lau 
60527ae7997SMartin KaFai Lau 	while (btf_type_is_modifier(t)) {
60627ae7997SMartin KaFai Lau 		id = t->type;
60727ae7997SMartin KaFai Lau 		t = btf_type_by_id(btf, t->type);
60827ae7997SMartin KaFai Lau 	}
60927ae7997SMartin KaFai Lau 
61027ae7997SMartin KaFai Lau 	if (res_id)
61127ae7997SMartin KaFai Lau 		*res_id = id;
61227ae7997SMartin KaFai Lau 
61327ae7997SMartin KaFai Lau 	return t;
61427ae7997SMartin KaFai Lau }
61527ae7997SMartin KaFai Lau 
btf_type_resolve_ptr(const struct btf * btf,u32 id,u32 * res_id)61627ae7997SMartin KaFai Lau const struct btf_type *btf_type_resolve_ptr(const struct btf *btf,
61727ae7997SMartin KaFai Lau 					    u32 id, u32 *res_id)
61827ae7997SMartin KaFai Lau {
61927ae7997SMartin KaFai Lau 	const struct btf_type *t;
62027ae7997SMartin KaFai Lau 
62127ae7997SMartin KaFai Lau 	t = btf_type_skip_modifiers(btf, id, NULL);
62227ae7997SMartin KaFai Lau 	if (!btf_type_is_ptr(t))
62327ae7997SMartin KaFai Lau 		return NULL;
62427ae7997SMartin KaFai Lau 
62527ae7997SMartin KaFai Lau 	return btf_type_skip_modifiers(btf, t->type, res_id);
62627ae7997SMartin KaFai Lau }
62727ae7997SMartin KaFai Lau 
btf_type_resolve_func_ptr(const struct btf * btf,u32 id,u32 * res_id)62827ae7997SMartin KaFai Lau const struct btf_type *btf_type_resolve_func_ptr(const struct btf *btf,
62927ae7997SMartin KaFai Lau 						 u32 id, u32 *res_id)
63027ae7997SMartin KaFai Lau {
63127ae7997SMartin KaFai Lau 	const struct btf_type *ptype;
63227ae7997SMartin KaFai Lau 
63327ae7997SMartin KaFai Lau 	ptype = btf_type_resolve_ptr(btf, id, res_id);
63427ae7997SMartin KaFai Lau 	if (ptype && btf_type_is_func_proto(ptype))
63527ae7997SMartin KaFai Lau 		return ptype;
63627ae7997SMartin KaFai Lau 
63727ae7997SMartin KaFai Lau 	return NULL;
63827ae7997SMartin KaFai Lau }
63927ae7997SMartin KaFai Lau 
6401dc92851SDaniel Borkmann /* Types that act only as a source, not sink or intermediate
6411dc92851SDaniel Borkmann  * type when resolving.
6421dc92851SDaniel Borkmann  */
btf_type_is_resolve_source_only(const struct btf_type * t)6431dc92851SDaniel Borkmann static bool btf_type_is_resolve_source_only(const struct btf_type *t)
6441dc92851SDaniel Borkmann {
6451dc92851SDaniel Borkmann 	return btf_type_is_var(t) ||
646223f903eSYonghong Song 	       btf_type_is_decl_tag(t) ||
6471dc92851SDaniel Borkmann 	       btf_type_is_datasec(t);
6481dc92851SDaniel Borkmann }
6491dc92851SDaniel Borkmann 
650eb3f595dSMartin KaFai Lau /* What types need to be resolved?
651eb3f595dSMartin KaFai Lau  *
652eb3f595dSMartin KaFai Lau  * btf_type_is_modifier() is an obvious one.
653eb3f595dSMartin KaFai Lau  *
654eb3f595dSMartin KaFai Lau  * btf_type_is_struct() because its member refers to
655eb3f595dSMartin KaFai Lau  * another type (through member->type).
6561dc92851SDaniel Borkmann  *
6571dc92851SDaniel Borkmann  * btf_type_is_var() because the variable refers to
6581dc92851SDaniel Borkmann  * another type. btf_type_is_datasec() holds multiple
6591dc92851SDaniel Borkmann  * btf_type_is_var() types that need resolving.
6601dc92851SDaniel Borkmann  *
661eb3f595dSMartin KaFai Lau  * btf_type_is_array() because its element (array->type)
662eb3f595dSMartin KaFai Lau  * refers to another type.  Array can be thought of a
663eb3f595dSMartin KaFai Lau  * special case of struct while array just has the same
664eb3f595dSMartin KaFai Lau  * member-type repeated by array->nelems of times.
665eb3f595dSMartin KaFai Lau  */
btf_type_needs_resolve(const struct btf_type * t)666eb3f595dSMartin KaFai Lau static bool btf_type_needs_resolve(const struct btf_type *t)
667eb3f595dSMartin KaFai Lau {
668eb3f595dSMartin KaFai Lau 	return btf_type_is_modifier(t) ||
669eb3f595dSMartin KaFai Lau 	       btf_type_is_ptr(t) ||
670eb3f595dSMartin KaFai Lau 	       btf_type_is_struct(t) ||
6711dc92851SDaniel Borkmann 	       btf_type_is_array(t) ||
6721dc92851SDaniel Borkmann 	       btf_type_is_var(t) ||
673d7e7b42fSYonghong Song 	       btf_type_is_func(t) ||
674223f903eSYonghong Song 	       btf_type_is_decl_tag(t) ||
6751dc92851SDaniel Borkmann 	       btf_type_is_datasec(t);
676eb3f595dSMartin KaFai Lau }
677eb3f595dSMartin KaFai Lau 
678eb3f595dSMartin KaFai Lau /* t->size can be used */
btf_type_has_size(const struct btf_type * t)679eb3f595dSMartin KaFai Lau static bool btf_type_has_size(const struct btf_type *t)
680eb3f595dSMartin KaFai Lau {
681eb3f595dSMartin KaFai Lau 	switch (BTF_INFO_KIND(t->info)) {
682eb3f595dSMartin KaFai Lau 	case BTF_KIND_INT:
683eb3f595dSMartin KaFai Lau 	case BTF_KIND_STRUCT:
684eb3f595dSMartin KaFai Lau 	case BTF_KIND_UNION:
685eb3f595dSMartin KaFai Lau 	case BTF_KIND_ENUM:
6861dc92851SDaniel Borkmann 	case BTF_KIND_DATASEC:
687b1828f0bSIlya Leoshkevich 	case BTF_KIND_FLOAT:
6886089fb32SYonghong Song 	case BTF_KIND_ENUM64:
689eb3f595dSMartin KaFai Lau 		return true;
690eb3f595dSMartin KaFai Lau 	}
691eb3f595dSMartin KaFai Lau 
692eb3f595dSMartin KaFai Lau 	return false;
693eb3f595dSMartin KaFai Lau }
694eb3f595dSMartin KaFai Lau 
btf_int_encoding_str(u8 encoding)69569b693f0SMartin KaFai Lau static const char *btf_int_encoding_str(u8 encoding)
69669b693f0SMartin KaFai Lau {
69769b693f0SMartin KaFai Lau 	if (encoding == 0)
69869b693f0SMartin KaFai Lau 		return "(none)";
69969b693f0SMartin KaFai Lau 	else if (encoding == BTF_INT_SIGNED)
70069b693f0SMartin KaFai Lau 		return "SIGNED";
70169b693f0SMartin KaFai Lau 	else if (encoding == BTF_INT_CHAR)
70269b693f0SMartin KaFai Lau 		return "CHAR";
70369b693f0SMartin KaFai Lau 	else if (encoding == BTF_INT_BOOL)
70469b693f0SMartin KaFai Lau 		return "BOOL";
70569b693f0SMartin KaFai Lau 	else
70669b693f0SMartin KaFai Lau 		return "UNKN";
70769b693f0SMartin KaFai Lau }
70869b693f0SMartin KaFai Lau 
btf_type_int(const struct btf_type * t)70969b693f0SMartin KaFai Lau static u32 btf_type_int(const struct btf_type *t)
71069b693f0SMartin KaFai Lau {
71169b693f0SMartin KaFai Lau 	return *(u32 *)(t + 1);
71269b693f0SMartin KaFai Lau }
71369b693f0SMartin KaFai Lau 
btf_type_array(const struct btf_type * t)71469b693f0SMartin KaFai Lau static const struct btf_array *btf_type_array(const struct btf_type *t)
71569b693f0SMartin KaFai Lau {
71669b693f0SMartin KaFai Lau 	return (const struct btf_array *)(t + 1);
71769b693f0SMartin KaFai Lau }
71869b693f0SMartin KaFai Lau 
btf_type_enum(const struct btf_type * t)71969b693f0SMartin KaFai Lau static const struct btf_enum *btf_type_enum(const struct btf_type *t)
72069b693f0SMartin KaFai Lau {
72169b693f0SMartin KaFai Lau 	return (const struct btf_enum *)(t + 1);
72269b693f0SMartin KaFai Lau }
72369b693f0SMartin KaFai Lau 
btf_type_var(const struct btf_type * t)7241dc92851SDaniel Borkmann static const struct btf_var *btf_type_var(const struct btf_type *t)
7251dc92851SDaniel Borkmann {
7261dc92851SDaniel Borkmann 	return (const struct btf_var *)(t + 1);
7271dc92851SDaniel Borkmann }
7281dc92851SDaniel Borkmann 
btf_type_decl_tag(const struct btf_type * t)729223f903eSYonghong Song static const struct btf_decl_tag *btf_type_decl_tag(const struct btf_type *t)
730b5ea834dSYonghong Song {
731223f903eSYonghong Song 	return (const struct btf_decl_tag *)(t + 1);
732b5ea834dSYonghong Song }
733b5ea834dSYonghong Song 
btf_type_enum64(const struct btf_type * t)7346089fb32SYonghong Song static const struct btf_enum64 *btf_type_enum64(const struct btf_type *t)
7356089fb32SYonghong Song {
7366089fb32SYonghong Song 	return (const struct btf_enum64 *)(t + 1);
7376089fb32SYonghong Song }
7386089fb32SYonghong Song 
btf_type_ops(const struct btf_type * t)73969b693f0SMartin KaFai Lau static const struct btf_kind_operations *btf_type_ops(const struct btf_type *t)
74069b693f0SMartin KaFai Lau {
74169b693f0SMartin KaFai Lau 	return kind_ops[BTF_INFO_KIND(t->info)];
74269b693f0SMartin KaFai Lau }
74369b693f0SMartin KaFai Lau 
btf_name_offset_valid(const struct btf * btf,u32 offset)744583c5318SMathieu Malaterre static bool btf_name_offset_valid(const struct btf *btf, u32 offset)
74569b693f0SMartin KaFai Lau {
746951bb646SAndrii Nakryiko 	if (!BTF_STR_OFFSET_VALID(offset))
747951bb646SAndrii Nakryiko 		return false;
748951bb646SAndrii Nakryiko 
749951bb646SAndrii Nakryiko 	while (offset < btf->start_str_off)
750951bb646SAndrii Nakryiko 		btf = btf->base_btf;
751951bb646SAndrii Nakryiko 
752951bb646SAndrii Nakryiko 	offset -= btf->start_str_off;
753951bb646SAndrii Nakryiko 	return offset < btf->hdr.str_len;
75469b693f0SMartin KaFai Lau }
75569b693f0SMartin KaFai Lau 
__btf_name_char_ok(char c,bool first)7569724160bSFlorent Revest static bool __btf_name_char_ok(char c, bool first)
7571dc92851SDaniel Borkmann {
7581dc92851SDaniel Borkmann 	if ((first ? !isalpha(c) :
7591dc92851SDaniel Borkmann 		     !isalnum(c)) &&
7601dc92851SDaniel Borkmann 	    c != '_' &&
7619724160bSFlorent Revest 	    c != '.')
7621dc92851SDaniel Borkmann 		return false;
7631dc92851SDaniel Borkmann 	return true;
7641dc92851SDaniel Borkmann }
7651dc92851SDaniel Borkmann 
btf_str_by_offset(const struct btf * btf,u32 offset)766951bb646SAndrii Nakryiko static const char *btf_str_by_offset(const struct btf *btf, u32 offset)
767951bb646SAndrii Nakryiko {
768951bb646SAndrii Nakryiko 	while (offset < btf->start_str_off)
769951bb646SAndrii Nakryiko 		btf = btf->base_btf;
770951bb646SAndrii Nakryiko 
771951bb646SAndrii Nakryiko 	offset -= btf->start_str_off;
772951bb646SAndrii Nakryiko 	if (offset < btf->hdr.str_len)
773951bb646SAndrii Nakryiko 		return &btf->strings[offset];
774951bb646SAndrii Nakryiko 
775951bb646SAndrii Nakryiko 	return NULL;
776951bb646SAndrii Nakryiko }
777951bb646SAndrii Nakryiko 
__btf_name_valid(const struct btf * btf,u32 offset)7789724160bSFlorent Revest static bool __btf_name_valid(const struct btf *btf, u32 offset)
7792667a262SMartin KaFai Lau {
7802667a262SMartin KaFai Lau 	/* offset must be valid */
781951bb646SAndrii Nakryiko 	const char *src = btf_str_by_offset(btf, offset);
7822667a262SMartin KaFai Lau 	const char *src_limit;
7832667a262SMartin KaFai Lau 
7849724160bSFlorent Revest 	if (!__btf_name_char_ok(*src, true))
7852667a262SMartin KaFai Lau 		return false;
7862667a262SMartin KaFai Lau 
7872667a262SMartin KaFai Lau 	/* set a limit on identifier length */
7882667a262SMartin KaFai Lau 	src_limit = src + KSYM_NAME_LEN;
7892667a262SMartin KaFai Lau 	src++;
7902667a262SMartin KaFai Lau 	while (*src && src < src_limit) {
7919724160bSFlorent Revest 		if (!__btf_name_char_ok(*src, false))
7922667a262SMartin KaFai Lau 			return false;
7932667a262SMartin KaFai Lau 		src++;
7942667a262SMartin KaFai Lau 	}
7952667a262SMartin KaFai Lau 
7962667a262SMartin KaFai Lau 	return !*src;
7972667a262SMartin KaFai Lau }
7982667a262SMartin KaFai Lau 
btf_name_valid_identifier(const struct btf * btf,u32 offset)7991dc92851SDaniel Borkmann static bool btf_name_valid_identifier(const struct btf *btf, u32 offset)
8001dc92851SDaniel Borkmann {
8019724160bSFlorent Revest 	return __btf_name_valid(btf, offset);
8021dc92851SDaniel Borkmann }
8031dc92851SDaniel Borkmann 
btf_name_valid_section(const struct btf * btf,u32 offset)8041dc92851SDaniel Borkmann static bool btf_name_valid_section(const struct btf *btf, u32 offset)
8051dc92851SDaniel Borkmann {
8069724160bSFlorent Revest 	return __btf_name_valid(btf, offset);
8071dc92851SDaniel Borkmann }
8081dc92851SDaniel Borkmann 
__btf_name_by_offset(const struct btf * btf,u32 offset)80923127b33SMartin KaFai Lau static const char *__btf_name_by_offset(const struct btf *btf, u32 offset)
81069b693f0SMartin KaFai Lau {
811951bb646SAndrii Nakryiko 	const char *name;
812951bb646SAndrii Nakryiko 
813aea2f7b8SMartin KaFai Lau 	if (!offset)
81469b693f0SMartin KaFai Lau 		return "(anon)";
815951bb646SAndrii Nakryiko 
816951bb646SAndrii Nakryiko 	name = btf_str_by_offset(btf, offset);
817951bb646SAndrii Nakryiko 	return name ?: "(invalid-name-offset)";
81869b693f0SMartin KaFai Lau }
81969b693f0SMartin KaFai Lau 
btf_name_by_offset(const struct btf * btf,u32 offset)82023127b33SMartin KaFai Lau const char *btf_name_by_offset(const struct btf *btf, u32 offset)
82123127b33SMartin KaFai Lau {
822951bb646SAndrii Nakryiko 	return btf_str_by_offset(btf, offset);
82323127b33SMartin KaFai Lau }
82423127b33SMartin KaFai Lau 
btf_type_by_id(const struct btf * btf,u32 type_id)825838e9690SYonghong Song const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id)
826eb3f595dSMartin KaFai Lau {
827951bb646SAndrii Nakryiko 	while (type_id < btf->start_id)
828951bb646SAndrii Nakryiko 		btf = btf->base_btf;
829eb3f595dSMartin KaFai Lau 
830951bb646SAndrii Nakryiko 	type_id -= btf->start_id;
831951bb646SAndrii Nakryiko 	if (type_id >= btf->nr_types)
832951bb646SAndrii Nakryiko 		return NULL;
833eb3f595dSMartin KaFai Lau 	return btf->types[type_id];
834eb3f595dSMartin KaFai Lau }
83584c6ac41SDaniel Xu EXPORT_SYMBOL_GPL(btf_type_by_id);
836eb3f595dSMartin KaFai Lau 
8374ef5f574SMartin KaFai Lau /*
8384ef5f574SMartin KaFai Lau  * Regular int is not a bit field and it must be either
839b1e8818cSYonghong Song  * u8/u16/u32/u64 or __int128.
8404ef5f574SMartin KaFai Lau  */
btf_type_int_is_regular(const struct btf_type * t)8414ef5f574SMartin KaFai Lau static bool btf_type_int_is_regular(const struct btf_type *t)
8424ef5f574SMartin KaFai Lau {
84336fc3c8cSMartin KaFai Lau 	u8 nr_bits, nr_bytes;
8444ef5f574SMartin KaFai Lau 	u32 int_data;
8454ef5f574SMartin KaFai Lau 
8464ef5f574SMartin KaFai Lau 	int_data = btf_type_int(t);
8474ef5f574SMartin KaFai Lau 	nr_bits = BTF_INT_BITS(int_data);
8484ef5f574SMartin KaFai Lau 	nr_bytes = BITS_ROUNDUP_BYTES(nr_bits);
8494ef5f574SMartin KaFai Lau 	if (BITS_PER_BYTE_MASKED(nr_bits) ||
8504ef5f574SMartin KaFai Lau 	    BTF_INT_OFFSET(int_data) ||
8514ef5f574SMartin KaFai Lau 	    (nr_bytes != sizeof(u8) && nr_bytes != sizeof(u16) &&
852b1e8818cSYonghong Song 	     nr_bytes != sizeof(u32) && nr_bytes != sizeof(u64) &&
853b1e8818cSYonghong Song 	     nr_bytes != (2 * sizeof(u64)))) {
8544ef5f574SMartin KaFai Lau 		return false;
8554ef5f574SMartin KaFai Lau 	}
8564ef5f574SMartin KaFai Lau 
8574ef5f574SMartin KaFai Lau 	return true;
8584ef5f574SMartin KaFai Lau }
8594ef5f574SMartin KaFai Lau 
8609a1126b6SRoman Gushchin /*
861ffa0c1cfSYonghong Song  * Check that given struct member is a regular int with expected
862ffa0c1cfSYonghong Song  * offset and size.
8639a1126b6SRoman Gushchin  */
btf_member_is_reg_int(const struct btf * btf,const struct btf_type * s,const struct btf_member * m,u32 expected_offset,u32 expected_size)864ffa0c1cfSYonghong Song bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s,
865ffa0c1cfSYonghong Song 			   const struct btf_member *m,
866ffa0c1cfSYonghong Song 			   u32 expected_offset, u32 expected_size)
8679a1126b6SRoman Gushchin {
868ffa0c1cfSYonghong Song 	const struct btf_type *t;
869ffa0c1cfSYonghong Song 	u32 id, int_data;
870ffa0c1cfSYonghong Song 	u8 nr_bits;
8719a1126b6SRoman Gushchin 
872ffa0c1cfSYonghong Song 	id = m->type;
873ffa0c1cfSYonghong Song 	t = btf_type_id_size(btf, &id, NULL);
874ffa0c1cfSYonghong Song 	if (!t || !btf_type_is_int(t))
8759a1126b6SRoman Gushchin 		return false;
8769a1126b6SRoman Gushchin 
8779a1126b6SRoman Gushchin 	int_data = btf_type_int(t);
8789a1126b6SRoman Gushchin 	nr_bits = BTF_INT_BITS(int_data);
879ffa0c1cfSYonghong Song 	if (btf_type_kflag(s)) {
880ffa0c1cfSYonghong Song 		u32 bitfield_size = BTF_MEMBER_BITFIELD_SIZE(m->offset);
881ffa0c1cfSYonghong Song 		u32 bit_offset = BTF_MEMBER_BIT_OFFSET(m->offset);
882ffa0c1cfSYonghong Song 
883ffa0c1cfSYonghong Song 		/* if kflag set, int should be a regular int and
884ffa0c1cfSYonghong Song 		 * bit offset should be at byte boundary.
885ffa0c1cfSYonghong Song 		 */
886ffa0c1cfSYonghong Song 		return !bitfield_size &&
887ffa0c1cfSYonghong Song 		       BITS_ROUNDUP_BYTES(bit_offset) == expected_offset &&
888ffa0c1cfSYonghong Song 		       BITS_ROUNDUP_BYTES(nr_bits) == expected_size;
889ffa0c1cfSYonghong Song 	}
890ffa0c1cfSYonghong Song 
891ffa0c1cfSYonghong Song 	if (BTF_INT_OFFSET(int_data) ||
892ffa0c1cfSYonghong Song 	    BITS_PER_BYTE_MASKED(m->offset) ||
893ffa0c1cfSYonghong Song 	    BITS_ROUNDUP_BYTES(m->offset) != expected_offset ||
894ffa0c1cfSYonghong Song 	    BITS_PER_BYTE_MASKED(nr_bits) ||
895ffa0c1cfSYonghong Song 	    BITS_ROUNDUP_BYTES(nr_bits) != expected_size)
8969a1126b6SRoman Gushchin 		return false;
8979a1126b6SRoman Gushchin 
8989a1126b6SRoman Gushchin 	return true;
8999a1126b6SRoman Gushchin }
9009a1126b6SRoman Gushchin 
90131d0bc81SAlan Maguire /* Similar to btf_type_skip_modifiers() but does not skip typedefs. */
btf_type_skip_qualifiers(const struct btf * btf,u32 id)90231d0bc81SAlan Maguire static const struct btf_type *btf_type_skip_qualifiers(const struct btf *btf,
90331d0bc81SAlan Maguire 						       u32 id)
90431d0bc81SAlan Maguire {
90531d0bc81SAlan Maguire 	const struct btf_type *t = btf_type_by_id(btf, id);
90631d0bc81SAlan Maguire 
90731d0bc81SAlan Maguire 	while (btf_type_is_modifier(t) &&
90831d0bc81SAlan Maguire 	       BTF_INFO_KIND(t->info) != BTF_KIND_TYPEDEF) {
90931d0bc81SAlan Maguire 		t = btf_type_by_id(btf, t->type);
91031d0bc81SAlan Maguire 	}
91131d0bc81SAlan Maguire 
91231d0bc81SAlan Maguire 	return t;
91331d0bc81SAlan Maguire }
91431d0bc81SAlan Maguire 
91531d0bc81SAlan Maguire #define BTF_SHOW_MAX_ITER	10
91631d0bc81SAlan Maguire 
91731d0bc81SAlan Maguire #define BTF_KIND_BIT(kind)	(1ULL << kind)
91831d0bc81SAlan Maguire 
91931d0bc81SAlan Maguire /*
92031d0bc81SAlan Maguire  * Populate show->state.name with type name information.
92131d0bc81SAlan Maguire  * Format of type name is
92231d0bc81SAlan Maguire  *
92331d0bc81SAlan Maguire  * [.member_name = ] (type_name)
92431d0bc81SAlan Maguire  */
btf_show_name(struct btf_show * show)92531d0bc81SAlan Maguire static const char *btf_show_name(struct btf_show *show)
92631d0bc81SAlan Maguire {
92731d0bc81SAlan Maguire 	/* BTF_MAX_ITER array suffixes "[]" */
92831d0bc81SAlan Maguire 	const char *array_suffixes = "[][][][][][][][][][]";
92931d0bc81SAlan Maguire 	const char *array_suffix = &array_suffixes[strlen(array_suffixes)];
93031d0bc81SAlan Maguire 	/* BTF_MAX_ITER pointer suffixes "*" */
93131d0bc81SAlan Maguire 	const char *ptr_suffixes = "**********";
93231d0bc81SAlan Maguire 	const char *ptr_suffix = &ptr_suffixes[strlen(ptr_suffixes)];
93331d0bc81SAlan Maguire 	const char *name = NULL, *prefix = "", *parens = "";
93431d0bc81SAlan Maguire 	const struct btf_member *m = show->state.member;
93573b6eae5SColin Ian King 	const struct btf_type *t;
93631d0bc81SAlan Maguire 	const struct btf_array *array;
93731d0bc81SAlan Maguire 	u32 id = show->state.type_id;
93831d0bc81SAlan Maguire 	const char *member = NULL;
93931d0bc81SAlan Maguire 	bool show_member = false;
94031d0bc81SAlan Maguire 	u64 kinds = 0;
94131d0bc81SAlan Maguire 	int i;
94231d0bc81SAlan Maguire 
94331d0bc81SAlan Maguire 	show->state.name[0] = '\0';
94431d0bc81SAlan Maguire 
94531d0bc81SAlan Maguire 	/*
94631d0bc81SAlan Maguire 	 * Don't show type name if we're showing an array member;
94731d0bc81SAlan Maguire 	 * in that case we show the array type so don't need to repeat
94831d0bc81SAlan Maguire 	 * ourselves for each member.
94931d0bc81SAlan Maguire 	 */
95031d0bc81SAlan Maguire 	if (show->state.array_member)
95131d0bc81SAlan Maguire 		return "";
95231d0bc81SAlan Maguire 
95331d0bc81SAlan Maguire 	/* Retrieve member name, if any. */
95431d0bc81SAlan Maguire 	if (m) {
95531d0bc81SAlan Maguire 		member = btf_name_by_offset(show->btf, m->name_off);
95631d0bc81SAlan Maguire 		show_member = strlen(member) > 0;
95731d0bc81SAlan Maguire 		id = m->type;
95831d0bc81SAlan Maguire 	}
95931d0bc81SAlan Maguire 
96031d0bc81SAlan Maguire 	/*
96131d0bc81SAlan Maguire 	 * Start with type_id, as we have resolved the struct btf_type *
96231d0bc81SAlan Maguire 	 * via btf_modifier_show() past the parent typedef to the child
96331d0bc81SAlan Maguire 	 * struct, int etc it is defined as.  In such cases, the type_id
96431d0bc81SAlan Maguire 	 * still represents the starting type while the struct btf_type *
96531d0bc81SAlan Maguire 	 * in our show->state points at the resolved type of the typedef.
96631d0bc81SAlan Maguire 	 */
96731d0bc81SAlan Maguire 	t = btf_type_by_id(show->btf, id);
96831d0bc81SAlan Maguire 	if (!t)
96931d0bc81SAlan Maguire 		return "";
97031d0bc81SAlan Maguire 
97131d0bc81SAlan Maguire 	/*
97231d0bc81SAlan Maguire 	 * The goal here is to build up the right number of pointer and
97331d0bc81SAlan Maguire 	 * array suffixes while ensuring the type name for a typedef
97431d0bc81SAlan Maguire 	 * is represented.  Along the way we accumulate a list of
97531d0bc81SAlan Maguire 	 * BTF kinds we have encountered, since these will inform later
97631d0bc81SAlan Maguire 	 * display; for example, pointer types will not require an
97731d0bc81SAlan Maguire 	 * opening "{" for struct, we will just display the pointer value.
97831d0bc81SAlan Maguire 	 *
97931d0bc81SAlan Maguire 	 * We also want to accumulate the right number of pointer or array
98031d0bc81SAlan Maguire 	 * indices in the format string while iterating until we get to
98131d0bc81SAlan Maguire 	 * the typedef/pointee/array member target type.
98231d0bc81SAlan Maguire 	 *
98331d0bc81SAlan Maguire 	 * We start by pointing at the end of pointer and array suffix
98431d0bc81SAlan Maguire 	 * strings; as we accumulate pointers and arrays we move the pointer
98531d0bc81SAlan Maguire 	 * or array string backwards so it will show the expected number of
98631d0bc81SAlan Maguire 	 * '*' or '[]' for the type.  BTF_SHOW_MAX_ITER of nesting of pointers
98731d0bc81SAlan Maguire 	 * and/or arrays and typedefs are supported as a precaution.
98831d0bc81SAlan Maguire 	 *
98931d0bc81SAlan Maguire 	 * We also want to get typedef name while proceeding to resolve
99031d0bc81SAlan Maguire 	 * type it points to so that we can add parentheses if it is a
99131d0bc81SAlan Maguire 	 * "typedef struct" etc.
99231d0bc81SAlan Maguire 	 */
99331d0bc81SAlan Maguire 	for (i = 0; i < BTF_SHOW_MAX_ITER; i++) {
99431d0bc81SAlan Maguire 
99531d0bc81SAlan Maguire 		switch (BTF_INFO_KIND(t->info)) {
99631d0bc81SAlan Maguire 		case BTF_KIND_TYPEDEF:
99731d0bc81SAlan Maguire 			if (!name)
99831d0bc81SAlan Maguire 				name = btf_name_by_offset(show->btf,
99931d0bc81SAlan Maguire 							       t->name_off);
100031d0bc81SAlan Maguire 			kinds |= BTF_KIND_BIT(BTF_KIND_TYPEDEF);
100131d0bc81SAlan Maguire 			id = t->type;
100231d0bc81SAlan Maguire 			break;
100331d0bc81SAlan Maguire 		case BTF_KIND_ARRAY:
100431d0bc81SAlan Maguire 			kinds |= BTF_KIND_BIT(BTF_KIND_ARRAY);
100531d0bc81SAlan Maguire 			parens = "[";
100631d0bc81SAlan Maguire 			if (!t)
100731d0bc81SAlan Maguire 				return "";
100831d0bc81SAlan Maguire 			array = btf_type_array(t);
100931d0bc81SAlan Maguire 			if (array_suffix > array_suffixes)
101031d0bc81SAlan Maguire 				array_suffix -= 2;
101131d0bc81SAlan Maguire 			id = array->type;
101231d0bc81SAlan Maguire 			break;
101331d0bc81SAlan Maguire 		case BTF_KIND_PTR:
101431d0bc81SAlan Maguire 			kinds |= BTF_KIND_BIT(BTF_KIND_PTR);
101531d0bc81SAlan Maguire 			if (ptr_suffix > ptr_suffixes)
101631d0bc81SAlan Maguire 				ptr_suffix -= 1;
101731d0bc81SAlan Maguire 			id = t->type;
101831d0bc81SAlan Maguire 			break;
101931d0bc81SAlan Maguire 		default:
102031d0bc81SAlan Maguire 			id = 0;
102131d0bc81SAlan Maguire 			break;
102231d0bc81SAlan Maguire 		}
102331d0bc81SAlan Maguire 		if (!id)
102431d0bc81SAlan Maguire 			break;
102531d0bc81SAlan Maguire 		t = btf_type_skip_qualifiers(show->btf, id);
102631d0bc81SAlan Maguire 	}
102731d0bc81SAlan Maguire 	/* We may not be able to represent this type; bail to be safe */
102831d0bc81SAlan Maguire 	if (i == BTF_SHOW_MAX_ITER)
102931d0bc81SAlan Maguire 		return "";
103031d0bc81SAlan Maguire 
103131d0bc81SAlan Maguire 	if (!name)
103231d0bc81SAlan Maguire 		name = btf_name_by_offset(show->btf, t->name_off);
103331d0bc81SAlan Maguire 
103431d0bc81SAlan Maguire 	switch (BTF_INFO_KIND(t->info)) {
103531d0bc81SAlan Maguire 	case BTF_KIND_STRUCT:
103631d0bc81SAlan Maguire 	case BTF_KIND_UNION:
103731d0bc81SAlan Maguire 		prefix = BTF_INFO_KIND(t->info) == BTF_KIND_STRUCT ?
103831d0bc81SAlan Maguire 			 "struct" : "union";
103931d0bc81SAlan Maguire 		/* if it's an array of struct/union, parens is already set */
104031d0bc81SAlan Maguire 		if (!(kinds & (BTF_KIND_BIT(BTF_KIND_ARRAY))))
104131d0bc81SAlan Maguire 			parens = "{";
104231d0bc81SAlan Maguire 		break;
104331d0bc81SAlan Maguire 	case BTF_KIND_ENUM:
10446089fb32SYonghong Song 	case BTF_KIND_ENUM64:
104531d0bc81SAlan Maguire 		prefix = "enum";
104631d0bc81SAlan Maguire 		break;
104731d0bc81SAlan Maguire 	default:
104831d0bc81SAlan Maguire 		break;
104931d0bc81SAlan Maguire 	}
105031d0bc81SAlan Maguire 
105131d0bc81SAlan Maguire 	/* pointer does not require parens */
105231d0bc81SAlan Maguire 	if (kinds & BTF_KIND_BIT(BTF_KIND_PTR))
105331d0bc81SAlan Maguire 		parens = "";
105431d0bc81SAlan Maguire 	/* typedef does not require struct/union/enum prefix */
105531d0bc81SAlan Maguire 	if (kinds & BTF_KIND_BIT(BTF_KIND_TYPEDEF))
105631d0bc81SAlan Maguire 		prefix = "";
105731d0bc81SAlan Maguire 
105831d0bc81SAlan Maguire 	if (!name)
105931d0bc81SAlan Maguire 		name = "";
106031d0bc81SAlan Maguire 
106131d0bc81SAlan Maguire 	/* Even if we don't want type name info, we want parentheses etc */
106231d0bc81SAlan Maguire 	if (show->flags & BTF_SHOW_NONAME)
106331d0bc81SAlan Maguire 		snprintf(show->state.name, sizeof(show->state.name), "%s",
106431d0bc81SAlan Maguire 			 parens);
106531d0bc81SAlan Maguire 	else
106631d0bc81SAlan Maguire 		snprintf(show->state.name, sizeof(show->state.name),
106731d0bc81SAlan Maguire 			 "%s%s%s(%s%s%s%s%s%s)%s",
106831d0bc81SAlan Maguire 			 /* first 3 strings comprise ".member = " */
106931d0bc81SAlan Maguire 			 show_member ? "." : "",
107031d0bc81SAlan Maguire 			 show_member ? member : "",
107131d0bc81SAlan Maguire 			 show_member ? " = " : "",
107231d0bc81SAlan Maguire 			 /* ...next is our prefix (struct, enum, etc) */
107331d0bc81SAlan Maguire 			 prefix,
107431d0bc81SAlan Maguire 			 strlen(prefix) > 0 && strlen(name) > 0 ? " " : "",
107531d0bc81SAlan Maguire 			 /* ...this is the type name itself */
107631d0bc81SAlan Maguire 			 name,
107731d0bc81SAlan Maguire 			 /* ...suffixed by the appropriate '*', '[]' suffixes */
107831d0bc81SAlan Maguire 			 strlen(ptr_suffix) > 0 ? " " : "", ptr_suffix,
107931d0bc81SAlan Maguire 			 array_suffix, parens);
108031d0bc81SAlan Maguire 
108131d0bc81SAlan Maguire 	return show->state.name;
108231d0bc81SAlan Maguire }
108331d0bc81SAlan Maguire 
__btf_show_indent(struct btf_show * show)108431d0bc81SAlan Maguire static const char *__btf_show_indent(struct btf_show *show)
108531d0bc81SAlan Maguire {
108631d0bc81SAlan Maguire 	const char *indents = "                                ";
108731d0bc81SAlan Maguire 	const char *indent = &indents[strlen(indents)];
108831d0bc81SAlan Maguire 
108931d0bc81SAlan Maguire 	if ((indent - show->state.depth) >= indents)
109031d0bc81SAlan Maguire 		return indent - show->state.depth;
109131d0bc81SAlan Maguire 	return indents;
109231d0bc81SAlan Maguire }
109331d0bc81SAlan Maguire 
btf_show_indent(struct btf_show * show)109431d0bc81SAlan Maguire static const char *btf_show_indent(struct btf_show *show)
109531d0bc81SAlan Maguire {
109631d0bc81SAlan Maguire 	return show->flags & BTF_SHOW_COMPACT ? "" : __btf_show_indent(show);
109731d0bc81SAlan Maguire }
109831d0bc81SAlan Maguire 
btf_show_newline(struct btf_show * show)109931d0bc81SAlan Maguire static const char *btf_show_newline(struct btf_show *show)
110031d0bc81SAlan Maguire {
110131d0bc81SAlan Maguire 	return show->flags & BTF_SHOW_COMPACT ? "" : "\n";
110231d0bc81SAlan Maguire }
110331d0bc81SAlan Maguire 
btf_show_delim(struct btf_show * show)110431d0bc81SAlan Maguire static const char *btf_show_delim(struct btf_show *show)
110531d0bc81SAlan Maguire {
110631d0bc81SAlan Maguire 	if (show->state.depth == 0)
110731d0bc81SAlan Maguire 		return "";
110831d0bc81SAlan Maguire 
110931d0bc81SAlan Maguire 	if ((show->flags & BTF_SHOW_COMPACT) && show->state.type &&
111031d0bc81SAlan Maguire 		BTF_INFO_KIND(show->state.type->info) == BTF_KIND_UNION)
111131d0bc81SAlan Maguire 		return "|";
111231d0bc81SAlan Maguire 
111331d0bc81SAlan Maguire 	return ",";
111431d0bc81SAlan Maguire }
111531d0bc81SAlan Maguire 
btf_show(struct btf_show * show,const char * fmt,...)111631d0bc81SAlan Maguire __printf(2, 3) static void btf_show(struct btf_show *show, const char *fmt, ...)
111731d0bc81SAlan Maguire {
111831d0bc81SAlan Maguire 	va_list args;
111931d0bc81SAlan Maguire 
112031d0bc81SAlan Maguire 	if (!show->state.depth_check) {
112131d0bc81SAlan Maguire 		va_start(args, fmt);
112231d0bc81SAlan Maguire 		show->showfn(show, fmt, args);
112331d0bc81SAlan Maguire 		va_end(args);
112431d0bc81SAlan Maguire 	}
112531d0bc81SAlan Maguire }
112631d0bc81SAlan Maguire 
112731d0bc81SAlan Maguire /* Macros are used here as btf_show_type_value[s]() prepends and appends
112831d0bc81SAlan Maguire  * format specifiers to the format specifier passed in; these do the work of
112931d0bc81SAlan Maguire  * adding indentation, delimiters etc while the caller simply has to specify
113031d0bc81SAlan Maguire  * the type value(s) in the format specifier + value(s).
113131d0bc81SAlan Maguire  */
113231d0bc81SAlan Maguire #define btf_show_type_value(show, fmt, value)				       \
113331d0bc81SAlan Maguire 	do {								       \
1134a2a5580fSBen Dooks 		if ((value) != (__typeof__(value))0 ||			       \
1135a2a5580fSBen Dooks 		    (show->flags & BTF_SHOW_ZERO) ||			       \
113631d0bc81SAlan Maguire 		    show->state.depth == 0) {				       \
113731d0bc81SAlan Maguire 			btf_show(show, "%s%s" fmt "%s%s",		       \
113831d0bc81SAlan Maguire 				 btf_show_indent(show),			       \
113931d0bc81SAlan Maguire 				 btf_show_name(show),			       \
114031d0bc81SAlan Maguire 				 value, btf_show_delim(show),		       \
114131d0bc81SAlan Maguire 				 btf_show_newline(show));		       \
114231d0bc81SAlan Maguire 			if (show->state.depth > show->state.depth_to_show)     \
114331d0bc81SAlan Maguire 				show->state.depth_to_show = show->state.depth; \
114431d0bc81SAlan Maguire 		}							       \
114531d0bc81SAlan Maguire 	} while (0)
114631d0bc81SAlan Maguire 
114731d0bc81SAlan Maguire #define btf_show_type_values(show, fmt, ...)				       \
114831d0bc81SAlan Maguire 	do {								       \
114931d0bc81SAlan Maguire 		btf_show(show, "%s%s" fmt "%s%s", btf_show_indent(show),       \
115031d0bc81SAlan Maguire 			 btf_show_name(show),				       \
115131d0bc81SAlan Maguire 			 __VA_ARGS__, btf_show_delim(show),		       \
115231d0bc81SAlan Maguire 			 btf_show_newline(show));			       \
115331d0bc81SAlan Maguire 		if (show->state.depth > show->state.depth_to_show)	       \
115431d0bc81SAlan Maguire 			show->state.depth_to_show = show->state.depth;	       \
115531d0bc81SAlan Maguire 	} while (0)
115631d0bc81SAlan Maguire 
115731d0bc81SAlan Maguire /* How much is left to copy to safe buffer after @data? */
btf_show_obj_size_left(struct btf_show * show,void * data)115831d0bc81SAlan Maguire static int btf_show_obj_size_left(struct btf_show *show, void *data)
115931d0bc81SAlan Maguire {
116031d0bc81SAlan Maguire 	return show->obj.head + show->obj.size - data;
116131d0bc81SAlan Maguire }
116231d0bc81SAlan Maguire 
116331d0bc81SAlan Maguire /* Is object pointed to by @data of @size already copied to our safe buffer? */
btf_show_obj_is_safe(struct btf_show * show,void * data,int size)116431d0bc81SAlan Maguire static bool btf_show_obj_is_safe(struct btf_show *show, void *data, int size)
116531d0bc81SAlan Maguire {
116631d0bc81SAlan Maguire 	return data >= show->obj.data &&
116731d0bc81SAlan Maguire 	       (data + size) < (show->obj.data + BTF_SHOW_OBJ_SAFE_SIZE);
116831d0bc81SAlan Maguire }
116931d0bc81SAlan Maguire 
117031d0bc81SAlan Maguire /*
117131d0bc81SAlan Maguire  * If object pointed to by @data of @size falls within our safe buffer, return
117231d0bc81SAlan Maguire  * the equivalent pointer to the same safe data.  Assumes
117331d0bc81SAlan Maguire  * copy_from_kernel_nofault() has already happened and our safe buffer is
117431d0bc81SAlan Maguire  * populated.
117531d0bc81SAlan Maguire  */
__btf_show_obj_safe(struct btf_show * show,void * data,int size)117631d0bc81SAlan Maguire static void *__btf_show_obj_safe(struct btf_show *show, void *data, int size)
117731d0bc81SAlan Maguire {
117831d0bc81SAlan Maguire 	if (btf_show_obj_is_safe(show, data, size))
117931d0bc81SAlan Maguire 		return show->obj.safe + (data - show->obj.data);
118031d0bc81SAlan Maguire 	return NULL;
118131d0bc81SAlan Maguire }
118231d0bc81SAlan Maguire 
118331d0bc81SAlan Maguire /*
118431d0bc81SAlan Maguire  * Return a safe-to-access version of data pointed to by @data.
118531d0bc81SAlan Maguire  * We do this by copying the relevant amount of information
118631d0bc81SAlan Maguire  * to the struct btf_show obj.safe buffer using copy_from_kernel_nofault().
118731d0bc81SAlan Maguire  *
118831d0bc81SAlan Maguire  * If BTF_SHOW_UNSAFE is specified, just return data as-is; no
118931d0bc81SAlan Maguire  * safe copy is needed.
119031d0bc81SAlan Maguire  *
119131d0bc81SAlan Maguire  * Otherwise we need to determine if we have the required amount
119231d0bc81SAlan Maguire  * of data (determined by the @data pointer and the size of the
119331d0bc81SAlan Maguire  * largest base type we can encounter (represented by
119431d0bc81SAlan Maguire  * BTF_SHOW_OBJ_BASE_TYPE_SIZE). Having that much data ensures
119531d0bc81SAlan Maguire  * that we will be able to print some of the current object,
119631d0bc81SAlan Maguire  * and if more is needed a copy will be triggered.
119731d0bc81SAlan Maguire  * Some objects such as structs will not fit into the buffer;
119831d0bc81SAlan Maguire  * in such cases additional copies when we iterate over their
119931d0bc81SAlan Maguire  * members may be needed.
120031d0bc81SAlan Maguire  *
120131d0bc81SAlan Maguire  * btf_show_obj_safe() is used to return a safe buffer for
120231d0bc81SAlan Maguire  * btf_show_start_type(); this ensures that as we recurse into
120331d0bc81SAlan Maguire  * nested types we always have safe data for the given type.
120431d0bc81SAlan Maguire  * This approach is somewhat wasteful; it's possible for example
120531d0bc81SAlan Maguire  * that when iterating over a large union we'll end up copying the
120631d0bc81SAlan Maguire  * same data repeatedly, but the goal is safety not performance.
120731d0bc81SAlan Maguire  * We use stack data as opposed to per-CPU buffers because the
120831d0bc81SAlan Maguire  * iteration over a type can take some time, and preemption handling
120931d0bc81SAlan Maguire  * would greatly complicate use of the safe buffer.
121031d0bc81SAlan Maguire  */
btf_show_obj_safe(struct btf_show * show,const struct btf_type * t,void * data)121131d0bc81SAlan Maguire static void *btf_show_obj_safe(struct btf_show *show,
121231d0bc81SAlan Maguire 			       const struct btf_type *t,
121331d0bc81SAlan Maguire 			       void *data)
121431d0bc81SAlan Maguire {
121531d0bc81SAlan Maguire 	const struct btf_type *rt;
121631d0bc81SAlan Maguire 	int size_left, size;
121731d0bc81SAlan Maguire 	void *safe = NULL;
121831d0bc81SAlan Maguire 
121931d0bc81SAlan Maguire 	if (show->flags & BTF_SHOW_UNSAFE)
122031d0bc81SAlan Maguire 		return data;
122131d0bc81SAlan Maguire 
122231d0bc81SAlan Maguire 	rt = btf_resolve_size(show->btf, t, &size);
122331d0bc81SAlan Maguire 	if (IS_ERR(rt)) {
122431d0bc81SAlan Maguire 		show->state.status = PTR_ERR(rt);
122531d0bc81SAlan Maguire 		return NULL;
122631d0bc81SAlan Maguire 	}
122731d0bc81SAlan Maguire 
122831d0bc81SAlan Maguire 	/*
122931d0bc81SAlan Maguire 	 * Is this toplevel object? If so, set total object size and
123031d0bc81SAlan Maguire 	 * initialize pointers.  Otherwise check if we still fall within
123131d0bc81SAlan Maguire 	 * our safe object data.
123231d0bc81SAlan Maguire 	 */
123331d0bc81SAlan Maguire 	if (show->state.depth == 0) {
123431d0bc81SAlan Maguire 		show->obj.size = size;
123531d0bc81SAlan Maguire 		show->obj.head = data;
123631d0bc81SAlan Maguire 	} else {
123731d0bc81SAlan Maguire 		/*
123831d0bc81SAlan Maguire 		 * If the size of the current object is > our remaining
123931d0bc81SAlan Maguire 		 * safe buffer we _may_ need to do a new copy.  However
124031d0bc81SAlan Maguire 		 * consider the case of a nested struct; it's size pushes
124131d0bc81SAlan Maguire 		 * us over the safe buffer limit, but showing any individual
124231d0bc81SAlan Maguire 		 * struct members does not.  In such cases, we don't need
124331d0bc81SAlan Maguire 		 * to initiate a fresh copy yet; however we definitely need
124431d0bc81SAlan Maguire 		 * at least BTF_SHOW_OBJ_BASE_TYPE_SIZE bytes left
124531d0bc81SAlan Maguire 		 * in our buffer, regardless of the current object size.
124631d0bc81SAlan Maguire 		 * The logic here is that as we resolve types we will
124731d0bc81SAlan Maguire 		 * hit a base type at some point, and we need to be sure
124831d0bc81SAlan Maguire 		 * the next chunk of data is safely available to display
124931d0bc81SAlan Maguire 		 * that type info safely.  We cannot rely on the size of
125031d0bc81SAlan Maguire 		 * the current object here because it may be much larger
125131d0bc81SAlan Maguire 		 * than our current buffer (e.g. task_struct is 8k).
125231d0bc81SAlan Maguire 		 * All we want to do here is ensure that we can print the
125331d0bc81SAlan Maguire 		 * next basic type, which we can if either
125431d0bc81SAlan Maguire 		 * - the current type size is within the safe buffer; or
125531d0bc81SAlan Maguire 		 * - at least BTF_SHOW_OBJ_BASE_TYPE_SIZE bytes are left in
125631d0bc81SAlan Maguire 		 *   the safe buffer.
125731d0bc81SAlan Maguire 		 */
125831d0bc81SAlan Maguire 		safe = __btf_show_obj_safe(show, data,
125931d0bc81SAlan Maguire 					   min(size,
126031d0bc81SAlan Maguire 					       BTF_SHOW_OBJ_BASE_TYPE_SIZE));
126131d0bc81SAlan Maguire 	}
126231d0bc81SAlan Maguire 
126331d0bc81SAlan Maguire 	/*
126431d0bc81SAlan Maguire 	 * We need a new copy to our safe object, either because we haven't
12658fb33b60SZhen Lei 	 * yet copied and are initializing safe data, or because the data
126631d0bc81SAlan Maguire 	 * we want falls outside the boundaries of the safe object.
126731d0bc81SAlan Maguire 	 */
126831d0bc81SAlan Maguire 	if (!safe) {
126931d0bc81SAlan Maguire 		size_left = btf_show_obj_size_left(show, data);
127031d0bc81SAlan Maguire 		if (size_left > BTF_SHOW_OBJ_SAFE_SIZE)
127131d0bc81SAlan Maguire 			size_left = BTF_SHOW_OBJ_SAFE_SIZE;
127231d0bc81SAlan Maguire 		show->state.status = copy_from_kernel_nofault(show->obj.safe,
127331d0bc81SAlan Maguire 							      data, size_left);
127431d0bc81SAlan Maguire 		if (!show->state.status) {
127531d0bc81SAlan Maguire 			show->obj.data = data;
127631d0bc81SAlan Maguire 			safe = show->obj.safe;
127731d0bc81SAlan Maguire 		}
127831d0bc81SAlan Maguire 	}
127931d0bc81SAlan Maguire 
128031d0bc81SAlan Maguire 	return safe;
128131d0bc81SAlan Maguire }
128231d0bc81SAlan Maguire 
128331d0bc81SAlan Maguire /*
128431d0bc81SAlan Maguire  * Set the type we are starting to show and return a safe data pointer
128531d0bc81SAlan Maguire  * to be used for showing the associated data.
128631d0bc81SAlan Maguire  */
btf_show_start_type(struct btf_show * show,const struct btf_type * t,u32 type_id,void * data)128731d0bc81SAlan Maguire static void *btf_show_start_type(struct btf_show *show,
128831d0bc81SAlan Maguire 				 const struct btf_type *t,
128931d0bc81SAlan Maguire 				 u32 type_id, void *data)
129031d0bc81SAlan Maguire {
129131d0bc81SAlan Maguire 	show->state.type = t;
129231d0bc81SAlan Maguire 	show->state.type_id = type_id;
129331d0bc81SAlan Maguire 	show->state.name[0] = '\0';
129431d0bc81SAlan Maguire 
129531d0bc81SAlan Maguire 	return btf_show_obj_safe(show, t, data);
129631d0bc81SAlan Maguire }
129731d0bc81SAlan Maguire 
btf_show_end_type(struct btf_show * show)129831d0bc81SAlan Maguire static void btf_show_end_type(struct btf_show *show)
129931d0bc81SAlan Maguire {
130031d0bc81SAlan Maguire 	show->state.type = NULL;
130131d0bc81SAlan Maguire 	show->state.type_id = 0;
130231d0bc81SAlan Maguire 	show->state.name[0] = '\0';
130331d0bc81SAlan Maguire }
130431d0bc81SAlan Maguire 
btf_show_start_aggr_type(struct btf_show * show,const struct btf_type * t,u32 type_id,void * data)130531d0bc81SAlan Maguire static void *btf_show_start_aggr_type(struct btf_show *show,
130631d0bc81SAlan Maguire 				      const struct btf_type *t,
130731d0bc81SAlan Maguire 				      u32 type_id, void *data)
130831d0bc81SAlan Maguire {
130931d0bc81SAlan Maguire 	void *safe_data = btf_show_start_type(show, t, type_id, data);
131031d0bc81SAlan Maguire 
131131d0bc81SAlan Maguire 	if (!safe_data)
131231d0bc81SAlan Maguire 		return safe_data;
131331d0bc81SAlan Maguire 
131431d0bc81SAlan Maguire 	btf_show(show, "%s%s%s", btf_show_indent(show),
131531d0bc81SAlan Maguire 		 btf_show_name(show),
131631d0bc81SAlan Maguire 		 btf_show_newline(show));
131731d0bc81SAlan Maguire 	show->state.depth++;
131831d0bc81SAlan Maguire 	return safe_data;
131931d0bc81SAlan Maguire }
132031d0bc81SAlan Maguire 
btf_show_end_aggr_type(struct btf_show * show,const char * suffix)132131d0bc81SAlan Maguire static void btf_show_end_aggr_type(struct btf_show *show,
132231d0bc81SAlan Maguire 				   const char *suffix)
132331d0bc81SAlan Maguire {
132431d0bc81SAlan Maguire 	show->state.depth--;
132531d0bc81SAlan Maguire 	btf_show(show, "%s%s%s%s", btf_show_indent(show), suffix,
132631d0bc81SAlan Maguire 		 btf_show_delim(show), btf_show_newline(show));
132731d0bc81SAlan Maguire 	btf_show_end_type(show);
132831d0bc81SAlan Maguire }
132931d0bc81SAlan Maguire 
btf_show_start_member(struct btf_show * show,const struct btf_member * m)133031d0bc81SAlan Maguire static void btf_show_start_member(struct btf_show *show,
133131d0bc81SAlan Maguire 				  const struct btf_member *m)
133231d0bc81SAlan Maguire {
133331d0bc81SAlan Maguire 	show->state.member = m;
133431d0bc81SAlan Maguire }
133531d0bc81SAlan Maguire 
btf_show_start_array_member(struct btf_show * show)133631d0bc81SAlan Maguire static void btf_show_start_array_member(struct btf_show *show)
133731d0bc81SAlan Maguire {
133831d0bc81SAlan Maguire 	show->state.array_member = 1;
133931d0bc81SAlan Maguire 	btf_show_start_member(show, NULL);
134031d0bc81SAlan Maguire }
134131d0bc81SAlan Maguire 
btf_show_end_member(struct btf_show * show)134231d0bc81SAlan Maguire static void btf_show_end_member(struct btf_show *show)
134331d0bc81SAlan Maguire {
134431d0bc81SAlan Maguire 	show->state.member = NULL;
134531d0bc81SAlan Maguire }
134631d0bc81SAlan Maguire 
btf_show_end_array_member(struct btf_show * show)134731d0bc81SAlan Maguire static void btf_show_end_array_member(struct btf_show *show)
134831d0bc81SAlan Maguire {
134931d0bc81SAlan Maguire 	show->state.array_member = 0;
135031d0bc81SAlan Maguire 	btf_show_end_member(show);
135131d0bc81SAlan Maguire }
135231d0bc81SAlan Maguire 
btf_show_start_array_type(struct btf_show * show,const struct btf_type * t,u32 type_id,u16 array_encoding,void * data)135331d0bc81SAlan Maguire static void *btf_show_start_array_type(struct btf_show *show,
135431d0bc81SAlan Maguire 				       const struct btf_type *t,
135531d0bc81SAlan Maguire 				       u32 type_id,
135631d0bc81SAlan Maguire 				       u16 array_encoding,
135731d0bc81SAlan Maguire 				       void *data)
135831d0bc81SAlan Maguire {
135931d0bc81SAlan Maguire 	show->state.array_encoding = array_encoding;
136031d0bc81SAlan Maguire 	show->state.array_terminated = 0;
136131d0bc81SAlan Maguire 	return btf_show_start_aggr_type(show, t, type_id, data);
136231d0bc81SAlan Maguire }
136331d0bc81SAlan Maguire 
btf_show_end_array_type(struct btf_show * show)136431d0bc81SAlan Maguire static void btf_show_end_array_type(struct btf_show *show)
136531d0bc81SAlan Maguire {
136631d0bc81SAlan Maguire 	show->state.array_encoding = 0;
136731d0bc81SAlan Maguire 	show->state.array_terminated = 0;
136831d0bc81SAlan Maguire 	btf_show_end_aggr_type(show, "]");
136931d0bc81SAlan Maguire }
137031d0bc81SAlan Maguire 
btf_show_start_struct_type(struct btf_show * show,const struct btf_type * t,u32 type_id,void * data)137131d0bc81SAlan Maguire static void *btf_show_start_struct_type(struct btf_show *show,
137231d0bc81SAlan Maguire 					const struct btf_type *t,
137331d0bc81SAlan Maguire 					u32 type_id,
137431d0bc81SAlan Maguire 					void *data)
137531d0bc81SAlan Maguire {
137631d0bc81SAlan Maguire 	return btf_show_start_aggr_type(show, t, type_id, data);
137731d0bc81SAlan Maguire }
137831d0bc81SAlan Maguire 
btf_show_end_struct_type(struct btf_show * show)137931d0bc81SAlan Maguire static void btf_show_end_struct_type(struct btf_show *show)
138031d0bc81SAlan Maguire {
138131d0bc81SAlan Maguire 	btf_show_end_aggr_type(show, "}");
138231d0bc81SAlan Maguire }
138331d0bc81SAlan Maguire 
__btf_verifier_log(struct bpf_verifier_log * log,const char * fmt,...)138469b693f0SMartin KaFai Lau __printf(2, 3) static void __btf_verifier_log(struct bpf_verifier_log *log,
138569b693f0SMartin KaFai Lau 					      const char *fmt, ...)
138669b693f0SMartin KaFai Lau {
138769b693f0SMartin KaFai Lau 	va_list args;
138869b693f0SMartin KaFai Lau 
138969b693f0SMartin KaFai Lau 	va_start(args, fmt);
139069b693f0SMartin KaFai Lau 	bpf_verifier_vlog(log, fmt, args);
139169b693f0SMartin KaFai Lau 	va_end(args);
139269b693f0SMartin KaFai Lau }
139369b693f0SMartin KaFai Lau 
btf_verifier_log(struct btf_verifier_env * env,const char * fmt,...)139469b693f0SMartin KaFai Lau __printf(2, 3) static void btf_verifier_log(struct btf_verifier_env *env,
139569b693f0SMartin KaFai Lau 					    const char *fmt, ...)
139669b693f0SMartin KaFai Lau {
139769b693f0SMartin KaFai Lau 	struct bpf_verifier_log *log = &env->log;
139869b693f0SMartin KaFai Lau 	va_list args;
139969b693f0SMartin KaFai Lau 
140069b693f0SMartin KaFai Lau 	if (!bpf_verifier_log_needed(log))
140169b693f0SMartin KaFai Lau 		return;
140269b693f0SMartin KaFai Lau 
140369b693f0SMartin KaFai Lau 	va_start(args, fmt);
140469b693f0SMartin KaFai Lau 	bpf_verifier_vlog(log, fmt, args);
140569b693f0SMartin KaFai Lau 	va_end(args);
140669b693f0SMartin KaFai Lau }
140769b693f0SMartin KaFai Lau 
__btf_verifier_log_type(struct btf_verifier_env * env,const struct btf_type * t,bool log_details,const char * fmt,...)140869b693f0SMartin KaFai Lau __printf(4, 5) static void __btf_verifier_log_type(struct btf_verifier_env *env,
140969b693f0SMartin KaFai Lau 						   const struct btf_type *t,
141069b693f0SMartin KaFai Lau 						   bool log_details,
141169b693f0SMartin KaFai Lau 						   const char *fmt, ...)
141269b693f0SMartin KaFai Lau {
141369b693f0SMartin KaFai Lau 	struct bpf_verifier_log *log = &env->log;
141469b693f0SMartin KaFai Lau 	struct btf *btf = env->btf;
141569b693f0SMartin KaFai Lau 	va_list args;
141669b693f0SMartin KaFai Lau 
141769b693f0SMartin KaFai Lau 	if (!bpf_verifier_log_needed(log))
141869b693f0SMartin KaFai Lau 		return;
141969b693f0SMartin KaFai Lau 
14209cb61e50SConnor O'Brien 	if (log->level == BPF_LOG_KERNEL) {
14218580ac94SAlexei Starovoitov 		/* btf verifier prints all types it is processing via
14228580ac94SAlexei Starovoitov 		 * btf_verifier_log_type(..., fmt = NULL).
14238580ac94SAlexei Starovoitov 		 * Skip those prints for in-kernel BTF verification.
14248580ac94SAlexei Starovoitov 		 */
14259cb61e50SConnor O'Brien 		if (!fmt)
14268580ac94SAlexei Starovoitov 			return;
14278580ac94SAlexei Starovoitov 
14289cb61e50SConnor O'Brien 		/* Skip logging when loading module BTF with mismatches permitted */
14299cb61e50SConnor O'Brien 		if (env->btf->base_btf && IS_ENABLED(CONFIG_MODULE_ALLOW_BTF_MISMATCH))
14309cb61e50SConnor O'Brien 			return;
14319cb61e50SConnor O'Brien 	}
14329cb61e50SConnor O'Brien 
143369b693f0SMartin KaFai Lau 	__btf_verifier_log(log, "[%u] %s %s%s",
143469b693f0SMartin KaFai Lau 			   env->log_type_id,
1435571f9738SPeilin Ye 			   btf_type_str(t),
143623127b33SMartin KaFai Lau 			   __btf_name_by_offset(btf, t->name_off),
143769b693f0SMartin KaFai Lau 			   log_details ? " " : "");
143869b693f0SMartin KaFai Lau 
143969b693f0SMartin KaFai Lau 	if (log_details)
144069b693f0SMartin KaFai Lau 		btf_type_ops(t)->log_details(env, t);
144169b693f0SMartin KaFai Lau 
144269b693f0SMartin KaFai Lau 	if (fmt && *fmt) {
144369b693f0SMartin KaFai Lau 		__btf_verifier_log(log, " ");
144469b693f0SMartin KaFai Lau 		va_start(args, fmt);
144569b693f0SMartin KaFai Lau 		bpf_verifier_vlog(log, fmt, args);
144669b693f0SMartin KaFai Lau 		va_end(args);
144769b693f0SMartin KaFai Lau 	}
144869b693f0SMartin KaFai Lau 
144969b693f0SMartin KaFai Lau 	__btf_verifier_log(log, "\n");
145069b693f0SMartin KaFai Lau }
145169b693f0SMartin KaFai Lau 
145269b693f0SMartin KaFai Lau #define btf_verifier_log_type(env, t, ...) \
145369b693f0SMartin KaFai Lau 	__btf_verifier_log_type((env), (t), true, __VA_ARGS__)
145469b693f0SMartin KaFai Lau #define btf_verifier_log_basic(env, t, ...) \
145569b693f0SMartin KaFai Lau 	__btf_verifier_log_type((env), (t), false, __VA_ARGS__)
145669b693f0SMartin KaFai Lau 
145769b693f0SMartin KaFai Lau __printf(4, 5)
btf_verifier_log_member(struct btf_verifier_env * env,const struct btf_type * struct_type,const struct btf_member * member,const char * fmt,...)145869b693f0SMartin KaFai Lau static void btf_verifier_log_member(struct btf_verifier_env *env,
145969b693f0SMartin KaFai Lau 				    const struct btf_type *struct_type,
146069b693f0SMartin KaFai Lau 				    const struct btf_member *member,
146169b693f0SMartin KaFai Lau 				    const char *fmt, ...)
146269b693f0SMartin KaFai Lau {
146369b693f0SMartin KaFai Lau 	struct bpf_verifier_log *log = &env->log;
146469b693f0SMartin KaFai Lau 	struct btf *btf = env->btf;
146569b693f0SMartin KaFai Lau 	va_list args;
146669b693f0SMartin KaFai Lau 
146769b693f0SMartin KaFai Lau 	if (!bpf_verifier_log_needed(log))
146869b693f0SMartin KaFai Lau 		return;
146969b693f0SMartin KaFai Lau 
14709cb61e50SConnor O'Brien 	if (log->level == BPF_LOG_KERNEL) {
14719cb61e50SConnor O'Brien 		if (!fmt)
14728580ac94SAlexei Starovoitov 			return;
14739cb61e50SConnor O'Brien 
14749cb61e50SConnor O'Brien 		/* Skip logging when loading module BTF with mismatches permitted */
14759cb61e50SConnor O'Brien 		if (env->btf->base_btf && IS_ENABLED(CONFIG_MODULE_ALLOW_BTF_MISMATCH))
14769cb61e50SConnor O'Brien 			return;
14779cb61e50SConnor O'Brien 	}
14789cb61e50SConnor O'Brien 
1479eb3f595dSMartin KaFai Lau 	/* The CHECK_META phase already did a btf dump.
1480eb3f595dSMartin KaFai Lau 	 *
1481eb3f595dSMartin KaFai Lau 	 * If member is logged again, it must hit an error in
1482eb3f595dSMartin KaFai Lau 	 * parsing this member.  It is useful to print out which
1483eb3f595dSMartin KaFai Lau 	 * struct this member belongs to.
1484eb3f595dSMartin KaFai Lau 	 */
1485eb3f595dSMartin KaFai Lau 	if (env->phase != CHECK_META)
1486eb3f595dSMartin KaFai Lau 		btf_verifier_log_type(env, struct_type, NULL);
1487eb3f595dSMartin KaFai Lau 
14889d5f9f70SYonghong Song 	if (btf_type_kflag(struct_type))
14899d5f9f70SYonghong Song 		__btf_verifier_log(log,
14909d5f9f70SYonghong Song 				   "\t%s type_id=%u bitfield_size=%u bits_offset=%u",
14919d5f9f70SYonghong Song 				   __btf_name_by_offset(btf, member->name_off),
14929d5f9f70SYonghong Song 				   member->type,
14939d5f9f70SYonghong Song 				   BTF_MEMBER_BITFIELD_SIZE(member->offset),
14949d5f9f70SYonghong Song 				   BTF_MEMBER_BIT_OFFSET(member->offset));
14959d5f9f70SYonghong Song 	else
149669b693f0SMartin KaFai Lau 		__btf_verifier_log(log, "\t%s type_id=%u bits_offset=%u",
149723127b33SMartin KaFai Lau 				   __btf_name_by_offset(btf, member->name_off),
149869b693f0SMartin KaFai Lau 				   member->type, member->offset);
149969b693f0SMartin KaFai Lau 
150069b693f0SMartin KaFai Lau 	if (fmt && *fmt) {
150169b693f0SMartin KaFai Lau 		__btf_verifier_log(log, " ");
150269b693f0SMartin KaFai Lau 		va_start(args, fmt);
150369b693f0SMartin KaFai Lau 		bpf_verifier_vlog(log, fmt, args);
150469b693f0SMartin KaFai Lau 		va_end(args);
150569b693f0SMartin KaFai Lau 	}
150669b693f0SMartin KaFai Lau 
150769b693f0SMartin KaFai Lau 	__btf_verifier_log(log, "\n");
150869b693f0SMartin KaFai Lau }
150969b693f0SMartin KaFai Lau 
15101dc92851SDaniel Borkmann __printf(4, 5)
btf_verifier_log_vsi(struct btf_verifier_env * env,const struct btf_type * datasec_type,const struct btf_var_secinfo * vsi,const char * fmt,...)15111dc92851SDaniel Borkmann static void btf_verifier_log_vsi(struct btf_verifier_env *env,
15121dc92851SDaniel Borkmann 				 const struct btf_type *datasec_type,
15131dc92851SDaniel Borkmann 				 const struct btf_var_secinfo *vsi,
15141dc92851SDaniel Borkmann 				 const char *fmt, ...)
15151dc92851SDaniel Borkmann {
15161dc92851SDaniel Borkmann 	struct bpf_verifier_log *log = &env->log;
15171dc92851SDaniel Borkmann 	va_list args;
15181dc92851SDaniel Borkmann 
15191dc92851SDaniel Borkmann 	if (!bpf_verifier_log_needed(log))
15201dc92851SDaniel Borkmann 		return;
15218580ac94SAlexei Starovoitov 	if (log->level == BPF_LOG_KERNEL && !fmt)
15228580ac94SAlexei Starovoitov 		return;
15231dc92851SDaniel Borkmann 	if (env->phase != CHECK_META)
15241dc92851SDaniel Borkmann 		btf_verifier_log_type(env, datasec_type, NULL);
15251dc92851SDaniel Borkmann 
15261dc92851SDaniel Borkmann 	__btf_verifier_log(log, "\t type_id=%u offset=%u size=%u",
15271dc92851SDaniel Borkmann 			   vsi->type, vsi->offset, vsi->size);
15281dc92851SDaniel Borkmann 	if (fmt && *fmt) {
15291dc92851SDaniel Borkmann 		__btf_verifier_log(log, " ");
15301dc92851SDaniel Borkmann 		va_start(args, fmt);
15311dc92851SDaniel Borkmann 		bpf_verifier_vlog(log, fmt, args);
15321dc92851SDaniel Borkmann 		va_end(args);
15331dc92851SDaniel Borkmann 	}
15341dc92851SDaniel Borkmann 
15351dc92851SDaniel Borkmann 	__btf_verifier_log(log, "\n");
15361dc92851SDaniel Borkmann }
15371dc92851SDaniel Borkmann 
btf_verifier_log_hdr(struct btf_verifier_env * env,u32 btf_data_size)1538f80442a4SMartin KaFai Lau static void btf_verifier_log_hdr(struct btf_verifier_env *env,
1539f80442a4SMartin KaFai Lau 				 u32 btf_data_size)
154069b693f0SMartin KaFai Lau {
154169b693f0SMartin KaFai Lau 	struct bpf_verifier_log *log = &env->log;
154269b693f0SMartin KaFai Lau 	const struct btf *btf = env->btf;
154369b693f0SMartin KaFai Lau 	const struct btf_header *hdr;
154469b693f0SMartin KaFai Lau 
154569b693f0SMartin KaFai Lau 	if (!bpf_verifier_log_needed(log))
154669b693f0SMartin KaFai Lau 		return;
154769b693f0SMartin KaFai Lau 
15488580ac94SAlexei Starovoitov 	if (log->level == BPF_LOG_KERNEL)
15498580ac94SAlexei Starovoitov 		return;
1550f80442a4SMartin KaFai Lau 	hdr = &btf->hdr;
155169b693f0SMartin KaFai Lau 	__btf_verifier_log(log, "magic: 0x%x\n", hdr->magic);
155269b693f0SMartin KaFai Lau 	__btf_verifier_log(log, "version: %u\n", hdr->version);
155369b693f0SMartin KaFai Lau 	__btf_verifier_log(log, "flags: 0x%x\n", hdr->flags);
1554f80442a4SMartin KaFai Lau 	__btf_verifier_log(log, "hdr_len: %u\n", hdr->hdr_len);
155569b693f0SMartin KaFai Lau 	__btf_verifier_log(log, "type_off: %u\n", hdr->type_off);
1556f80442a4SMartin KaFai Lau 	__btf_verifier_log(log, "type_len: %u\n", hdr->type_len);
155769b693f0SMartin KaFai Lau 	__btf_verifier_log(log, "str_off: %u\n", hdr->str_off);
155869b693f0SMartin KaFai Lau 	__btf_verifier_log(log, "str_len: %u\n", hdr->str_len);
1559f80442a4SMartin KaFai Lau 	__btf_verifier_log(log, "btf_total_size: %u\n", btf_data_size);
156069b693f0SMartin KaFai Lau }
156169b693f0SMartin KaFai Lau 
btf_add_type(struct btf_verifier_env * env,struct btf_type * t)156269b693f0SMartin KaFai Lau static int btf_add_type(struct btf_verifier_env *env, struct btf_type *t)
156369b693f0SMartin KaFai Lau {
156469b693f0SMartin KaFai Lau 	struct btf *btf = env->btf;
156569b693f0SMartin KaFai Lau 
1566951bb646SAndrii Nakryiko 	if (btf->types_size == btf->nr_types) {
156769b693f0SMartin KaFai Lau 		/* Expand 'types' array */
156869b693f0SMartin KaFai Lau 
156969b693f0SMartin KaFai Lau 		struct btf_type **new_types;
157069b693f0SMartin KaFai Lau 		u32 expand_by, new_size;
157169b693f0SMartin KaFai Lau 
1572951bb646SAndrii Nakryiko 		if (btf->start_id + btf->types_size == BTF_MAX_TYPE) {
157369b693f0SMartin KaFai Lau 			btf_verifier_log(env, "Exceeded max num of types");
157469b693f0SMartin KaFai Lau 			return -E2BIG;
157569b693f0SMartin KaFai Lau 		}
157669b693f0SMartin KaFai Lau 
157769b693f0SMartin KaFai Lau 		expand_by = max_t(u32, btf->types_size >> 2, 16);
1578aea2f7b8SMartin KaFai Lau 		new_size = min_t(u32, BTF_MAX_TYPE,
157969b693f0SMartin KaFai Lau 				 btf->types_size + expand_by);
158069b693f0SMartin KaFai Lau 
1581778e1cddSKees Cook 		new_types = kvcalloc(new_size, sizeof(*new_types),
158269b693f0SMartin KaFai Lau 				     GFP_KERNEL | __GFP_NOWARN);
158369b693f0SMartin KaFai Lau 		if (!new_types)
158469b693f0SMartin KaFai Lau 			return -ENOMEM;
158569b693f0SMartin KaFai Lau 
1586951bb646SAndrii Nakryiko 		if (btf->nr_types == 0) {
1587951bb646SAndrii Nakryiko 			if (!btf->base_btf) {
1588951bb646SAndrii Nakryiko 				/* lazily init VOID type */
158969b693f0SMartin KaFai Lau 				new_types[0] = &btf_void;
1590951bb646SAndrii Nakryiko 				btf->nr_types++;
1591951bb646SAndrii Nakryiko 			}
1592951bb646SAndrii Nakryiko 		} else {
159369b693f0SMartin KaFai Lau 			memcpy(new_types, btf->types,
1594951bb646SAndrii Nakryiko 			       sizeof(*btf->types) * btf->nr_types);
1595951bb646SAndrii Nakryiko 		}
159669b693f0SMartin KaFai Lau 
159769b693f0SMartin KaFai Lau 		kvfree(btf->types);
159869b693f0SMartin KaFai Lau 		btf->types = new_types;
159969b693f0SMartin KaFai Lau 		btf->types_size = new_size;
160069b693f0SMartin KaFai Lau 	}
160169b693f0SMartin KaFai Lau 
1602951bb646SAndrii Nakryiko 	btf->types[btf->nr_types++] = t;
160369b693f0SMartin KaFai Lau 
160469b693f0SMartin KaFai Lau 	return 0;
160569b693f0SMartin KaFai Lau }
160669b693f0SMartin KaFai Lau 
btf_alloc_id(struct btf * btf)160778958fcaSMartin KaFai Lau static int btf_alloc_id(struct btf *btf)
160878958fcaSMartin KaFai Lau {
160978958fcaSMartin KaFai Lau 	int id;
161078958fcaSMartin KaFai Lau 
161178958fcaSMartin KaFai Lau 	idr_preload(GFP_KERNEL);
161278958fcaSMartin KaFai Lau 	spin_lock_bh(&btf_idr_lock);
161378958fcaSMartin KaFai Lau 	id = idr_alloc_cyclic(&btf_idr, btf, 1, INT_MAX, GFP_ATOMIC);
161478958fcaSMartin KaFai Lau 	if (id > 0)
161578958fcaSMartin KaFai Lau 		btf->id = id;
161678958fcaSMartin KaFai Lau 	spin_unlock_bh(&btf_idr_lock);
161778958fcaSMartin KaFai Lau 	idr_preload_end();
161878958fcaSMartin KaFai Lau 
161978958fcaSMartin KaFai Lau 	if (WARN_ON_ONCE(!id))
162078958fcaSMartin KaFai Lau 		return -ENOSPC;
162178958fcaSMartin KaFai Lau 
162278958fcaSMartin KaFai Lau 	return id > 0 ? 0 : id;
162378958fcaSMartin KaFai Lau }
162478958fcaSMartin KaFai Lau 
btf_free_id(struct btf * btf)162578958fcaSMartin KaFai Lau static void btf_free_id(struct btf *btf)
162678958fcaSMartin KaFai Lau {
162778958fcaSMartin KaFai Lau 	unsigned long flags;
162878958fcaSMartin KaFai Lau 
162978958fcaSMartin KaFai Lau 	/*
163078958fcaSMartin KaFai Lau 	 * In map-in-map, calling map_delete_elem() on outer
163178958fcaSMartin KaFai Lau 	 * map will call bpf_map_put on the inner map.
163278958fcaSMartin KaFai Lau 	 * It will then eventually call btf_free_id()
163378958fcaSMartin KaFai Lau 	 * on the inner map.  Some of the map_delete_elem()
163478958fcaSMartin KaFai Lau 	 * implementation may have irq disabled, so
163578958fcaSMartin KaFai Lau 	 * we need to use the _irqsave() version instead
163678958fcaSMartin KaFai Lau 	 * of the _bh() version.
163778958fcaSMartin KaFai Lau 	 */
163878958fcaSMartin KaFai Lau 	spin_lock_irqsave(&btf_idr_lock, flags);
163978958fcaSMartin KaFai Lau 	idr_remove(&btf_idr, btf->id);
164078958fcaSMartin KaFai Lau 	spin_unlock_irqrestore(&btf_idr_lock, flags);
164178958fcaSMartin KaFai Lau }
164278958fcaSMartin KaFai Lau 
btf_free_kfunc_set_tab(struct btf * btf)1643dee872e1SKumar Kartikeya Dwivedi static void btf_free_kfunc_set_tab(struct btf *btf)
1644dee872e1SKumar Kartikeya Dwivedi {
1645dee872e1SKumar Kartikeya Dwivedi 	struct btf_kfunc_set_tab *tab = btf->kfunc_set_tab;
1646a4703e31SKumar Kartikeya Dwivedi 	int hook;
1647dee872e1SKumar Kartikeya Dwivedi 
1648dee872e1SKumar Kartikeya Dwivedi 	if (!tab)
1649dee872e1SKumar Kartikeya Dwivedi 		return;
1650dee872e1SKumar Kartikeya Dwivedi 	/* For module BTF, we directly assign the sets being registered, so
1651dee872e1SKumar Kartikeya Dwivedi 	 * there is nothing to free except kfunc_set_tab.
1652dee872e1SKumar Kartikeya Dwivedi 	 */
1653dee872e1SKumar Kartikeya Dwivedi 	if (btf_is_module(btf))
1654dee872e1SKumar Kartikeya Dwivedi 		goto free_tab;
1655a4703e31SKumar Kartikeya Dwivedi 	for (hook = 0; hook < ARRAY_SIZE(tab->sets); hook++)
1656a4703e31SKumar Kartikeya Dwivedi 		kfree(tab->sets[hook]);
1657dee872e1SKumar Kartikeya Dwivedi free_tab:
1658dee872e1SKumar Kartikeya Dwivedi 	kfree(tab);
1659dee872e1SKumar Kartikeya Dwivedi 	btf->kfunc_set_tab = NULL;
1660dee872e1SKumar Kartikeya Dwivedi }
1661dee872e1SKumar Kartikeya Dwivedi 
btf_free_dtor_kfunc_tab(struct btf * btf)16625ce937d6SKumar Kartikeya Dwivedi static void btf_free_dtor_kfunc_tab(struct btf *btf)
16635ce937d6SKumar Kartikeya Dwivedi {
16645ce937d6SKumar Kartikeya Dwivedi 	struct btf_id_dtor_kfunc_tab *tab = btf->dtor_kfunc_tab;
16655ce937d6SKumar Kartikeya Dwivedi 
16665ce937d6SKumar Kartikeya Dwivedi 	if (!tab)
16675ce937d6SKumar Kartikeya Dwivedi 		return;
16685ce937d6SKumar Kartikeya Dwivedi 	kfree(tab);
16695ce937d6SKumar Kartikeya Dwivedi 	btf->dtor_kfunc_tab = NULL;
16705ce937d6SKumar Kartikeya Dwivedi }
16715ce937d6SKumar Kartikeya Dwivedi 
btf_struct_metas_free(struct btf_struct_metas * tab)16728ffa5cc1SKumar Kartikeya Dwivedi static void btf_struct_metas_free(struct btf_struct_metas *tab)
16738ffa5cc1SKumar Kartikeya Dwivedi {
16748ffa5cc1SKumar Kartikeya Dwivedi 	int i;
16758ffa5cc1SKumar Kartikeya Dwivedi 
16768ffa5cc1SKumar Kartikeya Dwivedi 	if (!tab)
16778ffa5cc1SKumar Kartikeya Dwivedi 		return;
1678cd2a8079SDave Marchevsky 	for (i = 0; i < tab->cnt; i++)
16798ffa5cc1SKumar Kartikeya Dwivedi 		btf_record_free(tab->types[i].record);
16808ffa5cc1SKumar Kartikeya Dwivedi 	kfree(tab);
16818ffa5cc1SKumar Kartikeya Dwivedi }
16828ffa5cc1SKumar Kartikeya Dwivedi 
btf_free_struct_meta_tab(struct btf * btf)16838ffa5cc1SKumar Kartikeya Dwivedi static void btf_free_struct_meta_tab(struct btf *btf)
16848ffa5cc1SKumar Kartikeya Dwivedi {
16858ffa5cc1SKumar Kartikeya Dwivedi 	struct btf_struct_metas *tab = btf->struct_meta_tab;
16868ffa5cc1SKumar Kartikeya Dwivedi 
16878ffa5cc1SKumar Kartikeya Dwivedi 	btf_struct_metas_free(tab);
16888ffa5cc1SKumar Kartikeya Dwivedi 	btf->struct_meta_tab = NULL;
16898ffa5cc1SKumar Kartikeya Dwivedi }
16908ffa5cc1SKumar Kartikeya Dwivedi 
btf_free(struct btf * btf)169169b693f0SMartin KaFai Lau static void btf_free(struct btf *btf)
169269b693f0SMartin KaFai Lau {
16938ffa5cc1SKumar Kartikeya Dwivedi 	btf_free_struct_meta_tab(btf);
16945ce937d6SKumar Kartikeya Dwivedi 	btf_free_dtor_kfunc_tab(btf);
1695dee872e1SKumar Kartikeya Dwivedi 	btf_free_kfunc_set_tab(btf);
169669b693f0SMartin KaFai Lau 	kvfree(btf->types);
1697eb3f595dSMartin KaFai Lau 	kvfree(btf->resolved_sizes);
1698eb3f595dSMartin KaFai Lau 	kvfree(btf->resolved_ids);
169969b693f0SMartin KaFai Lau 	kvfree(btf->data);
170069b693f0SMartin KaFai Lau 	kfree(btf);
170169b693f0SMartin KaFai Lau }
170269b693f0SMartin KaFai Lau 
btf_free_rcu(struct rcu_head * rcu)170378958fcaSMartin KaFai Lau static void btf_free_rcu(struct rcu_head *rcu)
1704f56a653cSMartin KaFai Lau {
170578958fcaSMartin KaFai Lau 	struct btf *btf = container_of(rcu, struct btf, rcu);
170678958fcaSMartin KaFai Lau 
170778958fcaSMartin KaFai Lau 	btf_free(btf);
1708f56a653cSMartin KaFai Lau }
1709f56a653cSMartin KaFai Lau 
btf_get(struct btf * btf)171022dc4a0fSAndrii Nakryiko void btf_get(struct btf *btf)
171122dc4a0fSAndrii Nakryiko {
171222dc4a0fSAndrii Nakryiko 	refcount_inc(&btf->refcnt);
171322dc4a0fSAndrii Nakryiko }
171422dc4a0fSAndrii Nakryiko 
btf_put(struct btf * btf)1715f56a653cSMartin KaFai Lau void btf_put(struct btf *btf)
1716f56a653cSMartin KaFai Lau {
171778958fcaSMartin KaFai Lau 	if (btf && refcount_dec_and_test(&btf->refcnt)) {
171878958fcaSMartin KaFai Lau 		btf_free_id(btf);
171978958fcaSMartin KaFai Lau 		call_rcu(&btf->rcu, btf_free_rcu);
172078958fcaSMartin KaFai Lau 	}
1721f56a653cSMartin KaFai Lau }
1722f56a653cSMartin KaFai Lau 
env_resolve_init(struct btf_verifier_env * env)1723eb3f595dSMartin KaFai Lau static int env_resolve_init(struct btf_verifier_env *env)
1724eb3f595dSMartin KaFai Lau {
1725eb3f595dSMartin KaFai Lau 	struct btf *btf = env->btf;
1726eb3f595dSMartin KaFai Lau 	u32 nr_types = btf->nr_types;
1727eb3f595dSMartin KaFai Lau 	u32 *resolved_sizes = NULL;
1728eb3f595dSMartin KaFai Lau 	u32 *resolved_ids = NULL;
1729eb3f595dSMartin KaFai Lau 	u8 *visit_states = NULL;
1730eb3f595dSMartin KaFai Lau 
1731951bb646SAndrii Nakryiko 	resolved_sizes = kvcalloc(nr_types, sizeof(*resolved_sizes),
1732eb3f595dSMartin KaFai Lau 				  GFP_KERNEL | __GFP_NOWARN);
1733eb3f595dSMartin KaFai Lau 	if (!resolved_sizes)
1734eb3f595dSMartin KaFai Lau 		goto nomem;
1735eb3f595dSMartin KaFai Lau 
1736951bb646SAndrii Nakryiko 	resolved_ids = kvcalloc(nr_types, sizeof(*resolved_ids),
1737eb3f595dSMartin KaFai Lau 				GFP_KERNEL | __GFP_NOWARN);
1738eb3f595dSMartin KaFai Lau 	if (!resolved_ids)
1739eb3f595dSMartin KaFai Lau 		goto nomem;
1740eb3f595dSMartin KaFai Lau 
1741951bb646SAndrii Nakryiko 	visit_states = kvcalloc(nr_types, sizeof(*visit_states),
1742eb3f595dSMartin KaFai Lau 				GFP_KERNEL | __GFP_NOWARN);
1743eb3f595dSMartin KaFai Lau 	if (!visit_states)
1744eb3f595dSMartin KaFai Lau 		goto nomem;
1745eb3f595dSMartin KaFai Lau 
1746eb3f595dSMartin KaFai Lau 	btf->resolved_sizes = resolved_sizes;
1747eb3f595dSMartin KaFai Lau 	btf->resolved_ids = resolved_ids;
1748eb3f595dSMartin KaFai Lau 	env->visit_states = visit_states;
1749eb3f595dSMartin KaFai Lau 
1750eb3f595dSMartin KaFai Lau 	return 0;
1751eb3f595dSMartin KaFai Lau 
1752eb3f595dSMartin KaFai Lau nomem:
1753eb3f595dSMartin KaFai Lau 	kvfree(resolved_sizes);
1754eb3f595dSMartin KaFai Lau 	kvfree(resolved_ids);
1755eb3f595dSMartin KaFai Lau 	kvfree(visit_states);
1756eb3f595dSMartin KaFai Lau 	return -ENOMEM;
1757eb3f595dSMartin KaFai Lau }
1758eb3f595dSMartin KaFai Lau 
btf_verifier_env_free(struct btf_verifier_env * env)175969b693f0SMartin KaFai Lau static void btf_verifier_env_free(struct btf_verifier_env *env)
176069b693f0SMartin KaFai Lau {
1761eb3f595dSMartin KaFai Lau 	kvfree(env->visit_states);
176269b693f0SMartin KaFai Lau 	kfree(env);
176369b693f0SMartin KaFai Lau }
176469b693f0SMartin KaFai Lau 
env_type_is_resolve_sink(const struct btf_verifier_env * env,const struct btf_type * next_type)1765eb3f595dSMartin KaFai Lau static bool env_type_is_resolve_sink(const struct btf_verifier_env *env,
1766eb3f595dSMartin KaFai Lau 				     const struct btf_type *next_type)
1767eb3f595dSMartin KaFai Lau {
1768eb3f595dSMartin KaFai Lau 	switch (env->resolve_mode) {
1769eb3f595dSMartin KaFai Lau 	case RESOLVE_TBD:
1770eb3f595dSMartin KaFai Lau 		/* int, enum or void is a sink */
1771eb3f595dSMartin KaFai Lau 		return !btf_type_needs_resolve(next_type);
1772eb3f595dSMartin KaFai Lau 	case RESOLVE_PTR:
17732667a262SMartin KaFai Lau 		/* int, enum, void, struct, array, func or func_proto is a sink
17742667a262SMartin KaFai Lau 		 * for ptr
17752667a262SMartin KaFai Lau 		 */
1776eb3f595dSMartin KaFai Lau 		return !btf_type_is_modifier(next_type) &&
1777eb3f595dSMartin KaFai Lau 			!btf_type_is_ptr(next_type);
1778eb3f595dSMartin KaFai Lau 	case RESOLVE_STRUCT_OR_ARRAY:
17792667a262SMartin KaFai Lau 		/* int, enum, void, ptr, func or func_proto is a sink
17802667a262SMartin KaFai Lau 		 * for struct and array
17812667a262SMartin KaFai Lau 		 */
1782eb3f595dSMartin KaFai Lau 		return !btf_type_is_modifier(next_type) &&
1783eb3f595dSMartin KaFai Lau 			!btf_type_is_array(next_type) &&
1784eb3f595dSMartin KaFai Lau 			!btf_type_is_struct(next_type);
1785eb3f595dSMartin KaFai Lau 	default:
178653c8036cSArnd Bergmann 		BUG();
1787eb3f595dSMartin KaFai Lau 	}
1788eb3f595dSMartin KaFai Lau }
1789eb3f595dSMartin KaFai Lau 
env_type_is_resolved(const struct btf_verifier_env * env,u32 type_id)1790eb3f595dSMartin KaFai Lau static bool env_type_is_resolved(const struct btf_verifier_env *env,
1791eb3f595dSMartin KaFai Lau 				 u32 type_id)
1792eb3f595dSMartin KaFai Lau {
1793951bb646SAndrii Nakryiko 	/* base BTF types should be resolved by now */
1794951bb646SAndrii Nakryiko 	if (type_id < env->btf->start_id)
1795951bb646SAndrii Nakryiko 		return true;
1796951bb646SAndrii Nakryiko 
1797951bb646SAndrii Nakryiko 	return env->visit_states[type_id - env->btf->start_id] == RESOLVED;
1798eb3f595dSMartin KaFai Lau }
1799eb3f595dSMartin KaFai Lau 
env_stack_push(struct btf_verifier_env * env,const struct btf_type * t,u32 type_id)1800eb3f595dSMartin KaFai Lau static int env_stack_push(struct btf_verifier_env *env,
1801eb3f595dSMartin KaFai Lau 			  const struct btf_type *t, u32 type_id)
1802eb3f595dSMartin KaFai Lau {
1803951bb646SAndrii Nakryiko 	const struct btf *btf = env->btf;
1804eb3f595dSMartin KaFai Lau 	struct resolve_vertex *v;
1805eb3f595dSMartin KaFai Lau 
1806eb3f595dSMartin KaFai Lau 	if (env->top_stack == MAX_RESOLVE_DEPTH)
1807eb3f595dSMartin KaFai Lau 		return -E2BIG;
1808eb3f595dSMartin KaFai Lau 
1809951bb646SAndrii Nakryiko 	if (type_id < btf->start_id
1810951bb646SAndrii Nakryiko 	    || env->visit_states[type_id - btf->start_id] != NOT_VISITED)
1811eb3f595dSMartin KaFai Lau 		return -EEXIST;
1812eb3f595dSMartin KaFai Lau 
1813951bb646SAndrii Nakryiko 	env->visit_states[type_id - btf->start_id] = VISITED;
1814eb3f595dSMartin KaFai Lau 
1815eb3f595dSMartin KaFai Lau 	v = &env->stack[env->top_stack++];
1816eb3f595dSMartin KaFai Lau 	v->t = t;
1817eb3f595dSMartin KaFai Lau 	v->type_id = type_id;
1818eb3f595dSMartin KaFai Lau 	v->next_member = 0;
1819eb3f595dSMartin KaFai Lau 
1820eb3f595dSMartin KaFai Lau 	if (env->resolve_mode == RESOLVE_TBD) {
1821eb3f595dSMartin KaFai Lau 		if (btf_type_is_ptr(t))
1822eb3f595dSMartin KaFai Lau 			env->resolve_mode = RESOLVE_PTR;
1823eb3f595dSMartin KaFai Lau 		else if (btf_type_is_struct(t) || btf_type_is_array(t))
1824eb3f595dSMartin KaFai Lau 			env->resolve_mode = RESOLVE_STRUCT_OR_ARRAY;
1825eb3f595dSMartin KaFai Lau 	}
1826eb3f595dSMartin KaFai Lau 
1827eb3f595dSMartin KaFai Lau 	return 0;
1828eb3f595dSMartin KaFai Lau }
1829eb3f595dSMartin KaFai Lau 
env_stack_set_next_member(struct btf_verifier_env * env,u16 next_member)1830eb3f595dSMartin KaFai Lau static void env_stack_set_next_member(struct btf_verifier_env *env,
1831eb3f595dSMartin KaFai Lau 				      u16 next_member)
1832eb3f595dSMartin KaFai Lau {
1833eb3f595dSMartin KaFai Lau 	env->stack[env->top_stack - 1].next_member = next_member;
1834eb3f595dSMartin KaFai Lau }
1835eb3f595dSMartin KaFai Lau 
env_stack_pop_resolved(struct btf_verifier_env * env,u32 resolved_type_id,u32 resolved_size)1836eb3f595dSMartin KaFai Lau static void env_stack_pop_resolved(struct btf_verifier_env *env,
1837eb3f595dSMartin KaFai Lau 				   u32 resolved_type_id,
1838eb3f595dSMartin KaFai Lau 				   u32 resolved_size)
1839eb3f595dSMartin KaFai Lau {
1840eb3f595dSMartin KaFai Lau 	u32 type_id = env->stack[--(env->top_stack)].type_id;
1841eb3f595dSMartin KaFai Lau 	struct btf *btf = env->btf;
1842eb3f595dSMartin KaFai Lau 
1843951bb646SAndrii Nakryiko 	type_id -= btf->start_id; /* adjust to local type id */
1844eb3f595dSMartin KaFai Lau 	btf->resolved_sizes[type_id] = resolved_size;
1845eb3f595dSMartin KaFai Lau 	btf->resolved_ids[type_id] = resolved_type_id;
1846eb3f595dSMartin KaFai Lau 	env->visit_states[type_id] = RESOLVED;
1847eb3f595dSMartin KaFai Lau }
1848eb3f595dSMartin KaFai Lau 
env_stack_peak(struct btf_verifier_env * env)1849eb3f595dSMartin KaFai Lau static const struct resolve_vertex *env_stack_peak(struct btf_verifier_env *env)
1850eb3f595dSMartin KaFai Lau {
1851eb3f595dSMartin KaFai Lau 	return env->top_stack ? &env->stack[env->top_stack - 1] : NULL;
1852eb3f595dSMartin KaFai Lau }
1853eb3f595dSMartin KaFai Lau 
18547e3617a7SMartin KaFai Lau /* Resolve the size of a passed-in "type"
18557e3617a7SMartin KaFai Lau  *
18567e3617a7SMartin KaFai Lau  * type: is an array (e.g. u32 array[x][y])
18577e3617a7SMartin KaFai Lau  * return type: type "u32[x][y]", i.e. BTF_KIND_ARRAY,
18587e3617a7SMartin KaFai Lau  * *type_size: (x * y * sizeof(u32)).  Hence, *type_size always
18597e3617a7SMartin KaFai Lau  *             corresponds to the return type.
18607e3617a7SMartin KaFai Lau  * *elem_type: u32
186169ff3047SJiri Olsa  * *elem_id: id of u32
18627e3617a7SMartin KaFai Lau  * *total_nelems: (x * y).  Hence, individual elem size is
18637e3617a7SMartin KaFai Lau  *                (*type_size / *total_nelems)
1864887c31a3SJiri Olsa  * *type_id: id of type if it's changed within the function, 0 if not
18657e3617a7SMartin KaFai Lau  *
18667e3617a7SMartin KaFai Lau  * type: is not an array (e.g. const struct X)
18677e3617a7SMartin KaFai Lau  * return type: type "struct X"
18687e3617a7SMartin KaFai Lau  * *type_size: sizeof(struct X)
18697e3617a7SMartin KaFai Lau  * *elem_type: same as return type ("struct X")
187069ff3047SJiri Olsa  * *elem_id: 0
18717e3617a7SMartin KaFai Lau  * *total_nelems: 1
1872887c31a3SJiri Olsa  * *type_id: id of type if it's changed within the function, 0 if not
18737e3617a7SMartin KaFai Lau  */
18746298399bSJiri Olsa static const struct btf_type *
__btf_resolve_size(const struct btf * btf,const struct btf_type * type,u32 * type_size,const struct btf_type ** elem_type,u32 * elem_id,u32 * total_nelems,u32 * type_id)18756298399bSJiri Olsa __btf_resolve_size(const struct btf *btf, const struct btf_type *type,
18767e3617a7SMartin KaFai Lau 		   u32 *type_size, const struct btf_type **elem_type,
1877887c31a3SJiri Olsa 		   u32 *elem_id, u32 *total_nelems, u32 *type_id)
18787e3617a7SMartin KaFai Lau {
18797e3617a7SMartin KaFai Lau 	const struct btf_type *array_type = NULL;
188069ff3047SJiri Olsa 	const struct btf_array *array = NULL;
1881887c31a3SJiri Olsa 	u32 i, size, nelems = 1, id = 0;
18827e3617a7SMartin KaFai Lau 
18837e3617a7SMartin KaFai Lau 	for (i = 0; i < MAX_RESOLVE_DEPTH; i++) {
18847e3617a7SMartin KaFai Lau 		switch (BTF_INFO_KIND(type->info)) {
18857e3617a7SMartin KaFai Lau 		/* type->size can be used */
18867e3617a7SMartin KaFai Lau 		case BTF_KIND_INT:
18877e3617a7SMartin KaFai Lau 		case BTF_KIND_STRUCT:
18887e3617a7SMartin KaFai Lau 		case BTF_KIND_UNION:
18897e3617a7SMartin KaFai Lau 		case BTF_KIND_ENUM:
1890b1828f0bSIlya Leoshkevich 		case BTF_KIND_FLOAT:
18916089fb32SYonghong Song 		case BTF_KIND_ENUM64:
18927e3617a7SMartin KaFai Lau 			size = type->size;
18937e3617a7SMartin KaFai Lau 			goto resolved;
18947e3617a7SMartin KaFai Lau 
18957e3617a7SMartin KaFai Lau 		case BTF_KIND_PTR:
18967e3617a7SMartin KaFai Lau 			size = sizeof(void *);
18977e3617a7SMartin KaFai Lau 			goto resolved;
18987e3617a7SMartin KaFai Lau 
18997e3617a7SMartin KaFai Lau 		/* Modifiers */
19007e3617a7SMartin KaFai Lau 		case BTF_KIND_TYPEDEF:
19017e3617a7SMartin KaFai Lau 		case BTF_KIND_VOLATILE:
19027e3617a7SMartin KaFai Lau 		case BTF_KIND_CONST:
19037e3617a7SMartin KaFai Lau 		case BTF_KIND_RESTRICT:
19048c42d2faSYonghong Song 		case BTF_KIND_TYPE_TAG:
1905887c31a3SJiri Olsa 			id = type->type;
19067e3617a7SMartin KaFai Lau 			type = btf_type_by_id(btf, type->type);
19077e3617a7SMartin KaFai Lau 			break;
19087e3617a7SMartin KaFai Lau 
19097e3617a7SMartin KaFai Lau 		case BTF_KIND_ARRAY:
19107e3617a7SMartin KaFai Lau 			if (!array_type)
19117e3617a7SMartin KaFai Lau 				array_type = type;
19127e3617a7SMartin KaFai Lau 			array = btf_type_array(type);
19137e3617a7SMartin KaFai Lau 			if (nelems && array->nelems > U32_MAX / nelems)
19147e3617a7SMartin KaFai Lau 				return ERR_PTR(-EINVAL);
19157e3617a7SMartin KaFai Lau 			nelems *= array->nelems;
19167e3617a7SMartin KaFai Lau 			type = btf_type_by_id(btf, array->type);
19177e3617a7SMartin KaFai Lau 			break;
19187e3617a7SMartin KaFai Lau 
19197e3617a7SMartin KaFai Lau 		/* type without size */
19207e3617a7SMartin KaFai Lau 		default:
19217e3617a7SMartin KaFai Lau 			return ERR_PTR(-EINVAL);
19227e3617a7SMartin KaFai Lau 		}
19237e3617a7SMartin KaFai Lau 	}
19247e3617a7SMartin KaFai Lau 
19257e3617a7SMartin KaFai Lau 	return ERR_PTR(-EINVAL);
19267e3617a7SMartin KaFai Lau 
19277e3617a7SMartin KaFai Lau resolved:
19287e3617a7SMartin KaFai Lau 	if (nelems && size > U32_MAX / nelems)
19297e3617a7SMartin KaFai Lau 		return ERR_PTR(-EINVAL);
19307e3617a7SMartin KaFai Lau 
19317e3617a7SMartin KaFai Lau 	*type_size = nelems * size;
193285d33df3SMartin KaFai Lau 	if (total_nelems)
19337e3617a7SMartin KaFai Lau 		*total_nelems = nelems;
193485d33df3SMartin KaFai Lau 	if (elem_type)
19357e3617a7SMartin KaFai Lau 		*elem_type = type;
193669ff3047SJiri Olsa 	if (elem_id)
193769ff3047SJiri Olsa 		*elem_id = array ? array->type : 0;
1938887c31a3SJiri Olsa 	if (type_id && id)
1939887c31a3SJiri Olsa 		*type_id = id;
19407e3617a7SMartin KaFai Lau 
19417e3617a7SMartin KaFai Lau 	return array_type ? : type;
19427e3617a7SMartin KaFai Lau }
19437e3617a7SMartin KaFai Lau 
19446298399bSJiri Olsa const struct btf_type *
btf_resolve_size(const struct btf * btf,const struct btf_type * type,u32 * type_size)19456298399bSJiri Olsa btf_resolve_size(const struct btf *btf, const struct btf_type *type,
19466298399bSJiri Olsa 		 u32 *type_size)
19476298399bSJiri Olsa {
1948887c31a3SJiri Olsa 	return __btf_resolve_size(btf, type, type_size, NULL, NULL, NULL, NULL);
19496298399bSJiri Olsa }
19506298399bSJiri Olsa 
btf_resolved_type_id(const struct btf * btf,u32 type_id)1951951bb646SAndrii Nakryiko static u32 btf_resolved_type_id(const struct btf *btf, u32 type_id)
1952951bb646SAndrii Nakryiko {
1953951bb646SAndrii Nakryiko 	while (type_id < btf->start_id)
1954951bb646SAndrii Nakryiko 		btf = btf->base_btf;
1955951bb646SAndrii Nakryiko 
1956951bb646SAndrii Nakryiko 	return btf->resolved_ids[type_id - btf->start_id];
1957951bb646SAndrii Nakryiko }
1958951bb646SAndrii Nakryiko 
1959eb3f595dSMartin KaFai Lau /* The input param "type_id" must point to a needs_resolve type */
btf_type_id_resolve(const struct btf * btf,u32 * type_id)1960eb3f595dSMartin KaFai Lau static const struct btf_type *btf_type_id_resolve(const struct btf *btf,
1961eb3f595dSMartin KaFai Lau 						  u32 *type_id)
1962eb3f595dSMartin KaFai Lau {
1963951bb646SAndrii Nakryiko 	*type_id = btf_resolved_type_id(btf, *type_id);
1964eb3f595dSMartin KaFai Lau 	return btf_type_by_id(btf, *type_id);
1965eb3f595dSMartin KaFai Lau }
1966eb3f595dSMartin KaFai Lau 
btf_resolved_type_size(const struct btf * btf,u32 type_id)1967951bb646SAndrii Nakryiko static u32 btf_resolved_type_size(const struct btf *btf, u32 type_id)
1968951bb646SAndrii Nakryiko {
1969951bb646SAndrii Nakryiko 	while (type_id < btf->start_id)
1970951bb646SAndrii Nakryiko 		btf = btf->base_btf;
1971951bb646SAndrii Nakryiko 
1972951bb646SAndrii Nakryiko 	return btf->resolved_sizes[type_id - btf->start_id];
1973951bb646SAndrii Nakryiko }
1974951bb646SAndrii Nakryiko 
btf_type_id_size(const struct btf * btf,u32 * type_id,u32 * ret_size)1975eb3f595dSMartin KaFai Lau const struct btf_type *btf_type_id_size(const struct btf *btf,
1976eb3f595dSMartin KaFai Lau 					u32 *type_id, u32 *ret_size)
1977eb3f595dSMartin KaFai Lau {
1978eb3f595dSMartin KaFai Lau 	const struct btf_type *size_type;
1979eb3f595dSMartin KaFai Lau 	u32 size_type_id = *type_id;
1980eb3f595dSMartin KaFai Lau 	u32 size = 0;
1981eb3f595dSMartin KaFai Lau 
1982eb3f595dSMartin KaFai Lau 	size_type = btf_type_by_id(btf, size_type_id);
1983b47a0bd2SMartin KaFai Lau 	if (btf_type_nosize_or_null(size_type))
1984eb3f595dSMartin KaFai Lau 		return NULL;
1985eb3f595dSMartin KaFai Lau 
1986eb3f595dSMartin KaFai Lau 	if (btf_type_has_size(size_type)) {
1987eb3f595dSMartin KaFai Lau 		size = size_type->size;
1988eb3f595dSMartin KaFai Lau 	} else if (btf_type_is_array(size_type)) {
1989951bb646SAndrii Nakryiko 		size = btf_resolved_type_size(btf, size_type_id);
1990eb3f595dSMartin KaFai Lau 	} else if (btf_type_is_ptr(size_type)) {
1991eb3f595dSMartin KaFai Lau 		size = sizeof(void *);
1992eb3f595dSMartin KaFai Lau 	} else {
19931dc92851SDaniel Borkmann 		if (WARN_ON_ONCE(!btf_type_is_modifier(size_type) &&
19941dc92851SDaniel Borkmann 				 !btf_type_is_var(size_type)))
1995eb3f595dSMartin KaFai Lau 			return NULL;
1996eb3f595dSMartin KaFai Lau 
1997951bb646SAndrii Nakryiko 		size_type_id = btf_resolved_type_id(btf, size_type_id);
1998eb3f595dSMartin KaFai Lau 		size_type = btf_type_by_id(btf, size_type_id);
1999b47a0bd2SMartin KaFai Lau 		if (btf_type_nosize_or_null(size_type))
2000eb3f595dSMartin KaFai Lau 			return NULL;
20011acc5d5cSAndrii Nakryiko 		else if (btf_type_has_size(size_type))
20021acc5d5cSAndrii Nakryiko 			size = size_type->size;
20031acc5d5cSAndrii Nakryiko 		else if (btf_type_is_array(size_type))
2004951bb646SAndrii Nakryiko 			size = btf_resolved_type_size(btf, size_type_id);
20051acc5d5cSAndrii Nakryiko 		else if (btf_type_is_ptr(size_type))
20061acc5d5cSAndrii Nakryiko 			size = sizeof(void *);
20071acc5d5cSAndrii Nakryiko 		else
20081acc5d5cSAndrii Nakryiko 			return NULL;
2009eb3f595dSMartin KaFai Lau 	}
2010eb3f595dSMartin KaFai Lau 
2011eb3f595dSMartin KaFai Lau 	*type_id = size_type_id;
2012eb3f595dSMartin KaFai Lau 	if (ret_size)
2013eb3f595dSMartin KaFai Lau 		*ret_size = size;
2014eb3f595dSMartin KaFai Lau 
2015eb3f595dSMartin KaFai Lau 	return size_type;
2016eb3f595dSMartin KaFai Lau }
2017eb3f595dSMartin KaFai Lau 
btf_df_check_member(struct btf_verifier_env * env,const struct btf_type * struct_type,const struct btf_member * member,const struct btf_type * member_type)2018179cde8cSMartin KaFai Lau static int btf_df_check_member(struct btf_verifier_env *env,
2019179cde8cSMartin KaFai Lau 			       const struct btf_type *struct_type,
2020179cde8cSMartin KaFai Lau 			       const struct btf_member *member,
2021179cde8cSMartin KaFai Lau 			       const struct btf_type *member_type)
2022179cde8cSMartin KaFai Lau {
2023179cde8cSMartin KaFai Lau 	btf_verifier_log_basic(env, struct_type,
2024179cde8cSMartin KaFai Lau 			       "Unsupported check_member");
2025179cde8cSMartin KaFai Lau 	return -EINVAL;
2026179cde8cSMartin KaFai Lau }
2027179cde8cSMartin KaFai Lau 
btf_df_check_kflag_member(struct btf_verifier_env * env,const struct btf_type * struct_type,const struct btf_member * member,const struct btf_type * member_type)20289d5f9f70SYonghong Song static int btf_df_check_kflag_member(struct btf_verifier_env *env,
20299d5f9f70SYonghong Song 				     const struct btf_type *struct_type,
20309d5f9f70SYonghong Song 				     const struct btf_member *member,
20319d5f9f70SYonghong Song 				     const struct btf_type *member_type)
20329d5f9f70SYonghong Song {
20339d5f9f70SYonghong Song 	btf_verifier_log_basic(env, struct_type,
20349d5f9f70SYonghong Song 			       "Unsupported check_kflag_member");
20359d5f9f70SYonghong Song 	return -EINVAL;
20369d5f9f70SYonghong Song }
20379d5f9f70SYonghong Song 
2038b1828f0bSIlya Leoshkevich /* Used for ptr, array struct/union and float type members.
20399d5f9f70SYonghong Song  * int, enum and modifier types have their specific callback functions.
20409d5f9f70SYonghong Song  */
btf_generic_check_kflag_member(struct btf_verifier_env * env,const struct btf_type * struct_type,const struct btf_member * member,const struct btf_type * member_type)20419d5f9f70SYonghong Song static int btf_generic_check_kflag_member(struct btf_verifier_env *env,
20429d5f9f70SYonghong Song 					  const struct btf_type *struct_type,
20439d5f9f70SYonghong Song 					  const struct btf_member *member,
20449d5f9f70SYonghong Song 					  const struct btf_type *member_type)
20459d5f9f70SYonghong Song {
20469d5f9f70SYonghong Song 	if (BTF_MEMBER_BITFIELD_SIZE(member->offset)) {
20479d5f9f70SYonghong Song 		btf_verifier_log_member(env, struct_type, member,
20489d5f9f70SYonghong Song 					"Invalid member bitfield_size");
20499d5f9f70SYonghong Song 		return -EINVAL;
20509d5f9f70SYonghong Song 	}
20519d5f9f70SYonghong Song 
20529d5f9f70SYonghong Song 	/* bitfield size is 0, so member->offset represents bit offset only.
20539d5f9f70SYonghong Song 	 * It is safe to call non kflag check_member variants.
20549d5f9f70SYonghong Song 	 */
20559d5f9f70SYonghong Song 	return btf_type_ops(member_type)->check_member(env, struct_type,
20569d5f9f70SYonghong Song 						       member,
20579d5f9f70SYonghong Song 						       member_type);
20589d5f9f70SYonghong Song }
20599d5f9f70SYonghong Song 
btf_df_resolve(struct btf_verifier_env * env,const struct resolve_vertex * v)2060eb3f595dSMartin KaFai Lau static int btf_df_resolve(struct btf_verifier_env *env,
2061eb3f595dSMartin KaFai Lau 			  const struct resolve_vertex *v)
2062eb3f595dSMartin KaFai Lau {
2063eb3f595dSMartin KaFai Lau 	btf_verifier_log_basic(env, v->t, "Unsupported resolve");
2064eb3f595dSMartin KaFai Lau 	return -EINVAL;
2065eb3f595dSMartin KaFai Lau }
2066eb3f595dSMartin KaFai Lau 
btf_df_show(const struct btf * btf,const struct btf_type * t,u32 type_id,void * data,u8 bits_offsets,struct btf_show * show)206731d0bc81SAlan Maguire static void btf_df_show(const struct btf *btf, const struct btf_type *t,
2068b00b8daeSMartin KaFai Lau 			u32 type_id, void *data, u8 bits_offsets,
206931d0bc81SAlan Maguire 			struct btf_show *show)
2070b00b8daeSMartin KaFai Lau {
207131d0bc81SAlan Maguire 	btf_show(show, "<unsupported kind:%u>", BTF_INFO_KIND(t->info));
2072b00b8daeSMartin KaFai Lau }
2073b00b8daeSMartin KaFai Lau 
btf_int_check_member(struct btf_verifier_env * env,const struct btf_type * struct_type,const struct btf_member * member,const struct btf_type * member_type)2074179cde8cSMartin KaFai Lau static int btf_int_check_member(struct btf_verifier_env *env,
2075179cde8cSMartin KaFai Lau 				const struct btf_type *struct_type,
2076179cde8cSMartin KaFai Lau 				const struct btf_member *member,
2077179cde8cSMartin KaFai Lau 				const struct btf_type *member_type)
2078179cde8cSMartin KaFai Lau {
2079179cde8cSMartin KaFai Lau 	u32 int_data = btf_type_int(member_type);
2080179cde8cSMartin KaFai Lau 	u32 struct_bits_off = member->offset;
2081179cde8cSMartin KaFai Lau 	u32 struct_size = struct_type->size;
2082179cde8cSMartin KaFai Lau 	u32 nr_copy_bits;
2083179cde8cSMartin KaFai Lau 	u32 bytes_offset;
2084179cde8cSMartin KaFai Lau 
2085179cde8cSMartin KaFai Lau 	if (U32_MAX - struct_bits_off < BTF_INT_OFFSET(int_data)) {
2086179cde8cSMartin KaFai Lau 		btf_verifier_log_member(env, struct_type, member,
2087179cde8cSMartin KaFai Lau 					"bits_offset exceeds U32_MAX");
2088179cde8cSMartin KaFai Lau 		return -EINVAL;
2089179cde8cSMartin KaFai Lau 	}
2090179cde8cSMartin KaFai Lau 
2091179cde8cSMartin KaFai Lau 	struct_bits_off += BTF_INT_OFFSET(int_data);
2092179cde8cSMartin KaFai Lau 	bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
2093179cde8cSMartin KaFai Lau 	nr_copy_bits = BTF_INT_BITS(int_data) +
2094179cde8cSMartin KaFai Lau 		BITS_PER_BYTE_MASKED(struct_bits_off);
2095179cde8cSMartin KaFai Lau 
2096b1e8818cSYonghong Song 	if (nr_copy_bits > BITS_PER_U128) {
2097179cde8cSMartin KaFai Lau 		btf_verifier_log_member(env, struct_type, member,
2098b1e8818cSYonghong Song 					"nr_copy_bits exceeds 128");
2099179cde8cSMartin KaFai Lau 		return -EINVAL;
2100179cde8cSMartin KaFai Lau 	}
2101179cde8cSMartin KaFai Lau 
2102179cde8cSMartin KaFai Lau 	if (struct_size < bytes_offset ||
2103179cde8cSMartin KaFai Lau 	    struct_size - bytes_offset < BITS_ROUNDUP_BYTES(nr_copy_bits)) {
2104179cde8cSMartin KaFai Lau 		btf_verifier_log_member(env, struct_type, member,
2105179cde8cSMartin KaFai Lau 					"Member exceeds struct_size");
2106179cde8cSMartin KaFai Lau 		return -EINVAL;
2107179cde8cSMartin KaFai Lau 	}
2108179cde8cSMartin KaFai Lau 
2109179cde8cSMartin KaFai Lau 	return 0;
2110179cde8cSMartin KaFai Lau }
2111179cde8cSMartin KaFai Lau 
btf_int_check_kflag_member(struct btf_verifier_env * env,const struct btf_type * struct_type,const struct btf_member * member,const struct btf_type * member_type)21129d5f9f70SYonghong Song static int btf_int_check_kflag_member(struct btf_verifier_env *env,
21139d5f9f70SYonghong Song 				      const struct btf_type *struct_type,
21149d5f9f70SYonghong Song 				      const struct btf_member *member,
21159d5f9f70SYonghong Song 				      const struct btf_type *member_type)
21169d5f9f70SYonghong Song {
21179d5f9f70SYonghong Song 	u32 struct_bits_off, nr_bits, nr_int_data_bits, bytes_offset;
21189d5f9f70SYonghong Song 	u32 int_data = btf_type_int(member_type);
21199d5f9f70SYonghong Song 	u32 struct_size = struct_type->size;
21209d5f9f70SYonghong Song 	u32 nr_copy_bits;
21219d5f9f70SYonghong Song 
21229d5f9f70SYonghong Song 	/* a regular int type is required for the kflag int member */
21239d5f9f70SYonghong Song 	if (!btf_type_int_is_regular(member_type)) {
21249d5f9f70SYonghong Song 		btf_verifier_log_member(env, struct_type, member,
21259d5f9f70SYonghong Song 					"Invalid member base type");
21269d5f9f70SYonghong Song 		return -EINVAL;
21279d5f9f70SYonghong Song 	}
21289d5f9f70SYonghong Song 
21299d5f9f70SYonghong Song 	/* check sanity of bitfield size */
21309d5f9f70SYonghong Song 	nr_bits = BTF_MEMBER_BITFIELD_SIZE(member->offset);
21319d5f9f70SYonghong Song 	struct_bits_off = BTF_MEMBER_BIT_OFFSET(member->offset);
21329d5f9f70SYonghong Song 	nr_int_data_bits = BTF_INT_BITS(int_data);
21339d5f9f70SYonghong Song 	if (!nr_bits) {
21349d5f9f70SYonghong Song 		/* Not a bitfield member, member offset must be at byte
21359d5f9f70SYonghong Song 		 * boundary.
21369d5f9f70SYonghong Song 		 */
21379d5f9f70SYonghong Song 		if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
21389d5f9f70SYonghong Song 			btf_verifier_log_member(env, struct_type, member,
21399d5f9f70SYonghong Song 						"Invalid member offset");
21409d5f9f70SYonghong Song 			return -EINVAL;
21419d5f9f70SYonghong Song 		}
21429d5f9f70SYonghong Song 
21439d5f9f70SYonghong Song 		nr_bits = nr_int_data_bits;
21449d5f9f70SYonghong Song 	} else if (nr_bits > nr_int_data_bits) {
21459d5f9f70SYonghong Song 		btf_verifier_log_member(env, struct_type, member,
21469d5f9f70SYonghong Song 					"Invalid member bitfield_size");
21479d5f9f70SYonghong Song 		return -EINVAL;
21489d5f9f70SYonghong Song 	}
21499d5f9f70SYonghong Song 
21509d5f9f70SYonghong Song 	bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
21519d5f9f70SYonghong Song 	nr_copy_bits = nr_bits + BITS_PER_BYTE_MASKED(struct_bits_off);
2152b1e8818cSYonghong Song 	if (nr_copy_bits > BITS_PER_U128) {
21539d5f9f70SYonghong Song 		btf_verifier_log_member(env, struct_type, member,
2154b1e8818cSYonghong Song 					"nr_copy_bits exceeds 128");
21559d5f9f70SYonghong Song 		return -EINVAL;
21569d5f9f70SYonghong Song 	}
21579d5f9f70SYonghong Song 
21589d5f9f70SYonghong Song 	if (struct_size < bytes_offset ||
21599d5f9f70SYonghong Song 	    struct_size - bytes_offset < BITS_ROUNDUP_BYTES(nr_copy_bits)) {
21609d5f9f70SYonghong Song 		btf_verifier_log_member(env, struct_type, member,
21619d5f9f70SYonghong Song 					"Member exceeds struct_size");
21629d5f9f70SYonghong Song 		return -EINVAL;
21639d5f9f70SYonghong Song 	}
21649d5f9f70SYonghong Song 
21659d5f9f70SYonghong Song 	return 0;
21669d5f9f70SYonghong Song }
21679d5f9f70SYonghong Song 
btf_int_check_meta(struct btf_verifier_env * env,const struct btf_type * t,u32 meta_left)216869b693f0SMartin KaFai Lau static s32 btf_int_check_meta(struct btf_verifier_env *env,
216969b693f0SMartin KaFai Lau 			      const struct btf_type *t,
217069b693f0SMartin KaFai Lau 			      u32 meta_left)
217169b693f0SMartin KaFai Lau {
217269b693f0SMartin KaFai Lau 	u32 int_data, nr_bits, meta_needed = sizeof(int_data);
217369b693f0SMartin KaFai Lau 	u16 encoding;
217469b693f0SMartin KaFai Lau 
217569b693f0SMartin KaFai Lau 	if (meta_left < meta_needed) {
217669b693f0SMartin KaFai Lau 		btf_verifier_log_basic(env, t,
217769b693f0SMartin KaFai Lau 				       "meta_left:%u meta_needed:%u",
217869b693f0SMartin KaFai Lau 				       meta_left, meta_needed);
217969b693f0SMartin KaFai Lau 		return -EINVAL;
218069b693f0SMartin KaFai Lau 	}
218169b693f0SMartin KaFai Lau 
218269b693f0SMartin KaFai Lau 	if (btf_type_vlen(t)) {
218369b693f0SMartin KaFai Lau 		btf_verifier_log_type(env, t, "vlen != 0");
218469b693f0SMartin KaFai Lau 		return -EINVAL;
218569b693f0SMartin KaFai Lau 	}
218669b693f0SMartin KaFai Lau 
21879d5f9f70SYonghong Song 	if (btf_type_kflag(t)) {
21889d5f9f70SYonghong Song 		btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
21899d5f9f70SYonghong Song 		return -EINVAL;
21909d5f9f70SYonghong Song 	}
21919d5f9f70SYonghong Song 
219269b693f0SMartin KaFai Lau 	int_data = btf_type_int(t);
2193aea2f7b8SMartin KaFai Lau 	if (int_data & ~BTF_INT_MASK) {
2194aea2f7b8SMartin KaFai Lau 		btf_verifier_log_basic(env, t, "Invalid int_data:%x",
2195aea2f7b8SMartin KaFai Lau 				       int_data);
2196aea2f7b8SMartin KaFai Lau 		return -EINVAL;
2197aea2f7b8SMartin KaFai Lau 	}
2198aea2f7b8SMartin KaFai Lau 
219969b693f0SMartin KaFai Lau 	nr_bits = BTF_INT_BITS(int_data) + BTF_INT_OFFSET(int_data);
220069b693f0SMartin KaFai Lau 
2201b1e8818cSYonghong Song 	if (nr_bits > BITS_PER_U128) {
220269b693f0SMartin KaFai Lau 		btf_verifier_log_type(env, t, "nr_bits exceeds %zu",
2203b1e8818cSYonghong Song 				      BITS_PER_U128);
220469b693f0SMartin KaFai Lau 		return -EINVAL;
220569b693f0SMartin KaFai Lau 	}
220669b693f0SMartin KaFai Lau 
220769b693f0SMartin KaFai Lau 	if (BITS_ROUNDUP_BYTES(nr_bits) > t->size) {
220869b693f0SMartin KaFai Lau 		btf_verifier_log_type(env, t, "nr_bits exceeds type_size");
220969b693f0SMartin KaFai Lau 		return -EINVAL;
221069b693f0SMartin KaFai Lau 	}
221169b693f0SMartin KaFai Lau 
2212aea2f7b8SMartin KaFai Lau 	/*
2213aea2f7b8SMartin KaFai Lau 	 * Only one of the encoding bits is allowed and it
2214aea2f7b8SMartin KaFai Lau 	 * should be sufficient for the pretty print purpose (i.e. decoding).
2215aea2f7b8SMartin KaFai Lau 	 * Multiple bits can be allowed later if it is found
2216aea2f7b8SMartin KaFai Lau 	 * to be insufficient.
2217aea2f7b8SMartin KaFai Lau 	 */
221869b693f0SMartin KaFai Lau 	encoding = BTF_INT_ENCODING(int_data);
221969b693f0SMartin KaFai Lau 	if (encoding &&
222069b693f0SMartin KaFai Lau 	    encoding != BTF_INT_SIGNED &&
222169b693f0SMartin KaFai Lau 	    encoding != BTF_INT_CHAR &&
2222aea2f7b8SMartin KaFai Lau 	    encoding != BTF_INT_BOOL) {
222369b693f0SMartin KaFai Lau 		btf_verifier_log_type(env, t, "Unsupported encoding");
222469b693f0SMartin KaFai Lau 		return -ENOTSUPP;
222569b693f0SMartin KaFai Lau 	}
222669b693f0SMartin KaFai Lau 
222769b693f0SMartin KaFai Lau 	btf_verifier_log_type(env, t, NULL);
222869b693f0SMartin KaFai Lau 
222969b693f0SMartin KaFai Lau 	return meta_needed;
223069b693f0SMartin KaFai Lau }
223169b693f0SMartin KaFai Lau 
btf_int_log(struct btf_verifier_env * env,const struct btf_type * t)223269b693f0SMartin KaFai Lau static void btf_int_log(struct btf_verifier_env *env,
223369b693f0SMartin KaFai Lau 			const struct btf_type *t)
223469b693f0SMartin KaFai Lau {
223569b693f0SMartin KaFai Lau 	int int_data = btf_type_int(t);
223669b693f0SMartin KaFai Lau 
223769b693f0SMartin KaFai Lau 	btf_verifier_log(env,
223869b693f0SMartin KaFai Lau 			 "size=%u bits_offset=%u nr_bits=%u encoding=%s",
223969b693f0SMartin KaFai Lau 			 t->size, BTF_INT_OFFSET(int_data),
224069b693f0SMartin KaFai Lau 			 BTF_INT_BITS(int_data),
224169b693f0SMartin KaFai Lau 			 btf_int_encoding_str(BTF_INT_ENCODING(int_data)));
224269b693f0SMartin KaFai Lau }
224369b693f0SMartin KaFai Lau 
btf_int128_print(struct btf_show * show,void * data)224431d0bc81SAlan Maguire static void btf_int128_print(struct btf_show *show, void *data)
2245b1e8818cSYonghong Song {
2246b1e8818cSYonghong Song 	/* data points to a __int128 number.
2247b1e8818cSYonghong Song 	 * Suppose
2248b1e8818cSYonghong Song 	 *     int128_num = *(__int128 *)data;
2249b1e8818cSYonghong Song 	 * The below formulas shows what upper_num and lower_num represents:
2250b1e8818cSYonghong Song 	 *     upper_num = int128_num >> 64;
2251b1e8818cSYonghong Song 	 *     lower_num = int128_num & 0xffffffffFFFFFFFFULL;
2252b1e8818cSYonghong Song 	 */
2253b1e8818cSYonghong Song 	u64 upper_num, lower_num;
2254b1e8818cSYonghong Song 
2255b1e8818cSYonghong Song #ifdef __BIG_ENDIAN_BITFIELD
2256b1e8818cSYonghong Song 	upper_num = *(u64 *)data;
2257b1e8818cSYonghong Song 	lower_num = *(u64 *)(data + 8);
2258b1e8818cSYonghong Song #else
2259b1e8818cSYonghong Song 	upper_num = *(u64 *)(data + 8);
2260b1e8818cSYonghong Song 	lower_num = *(u64 *)data;
2261b1e8818cSYonghong Song #endif
2262b1e8818cSYonghong Song 	if (upper_num == 0)
226331d0bc81SAlan Maguire 		btf_show_type_value(show, "0x%llx", lower_num);
2264b1e8818cSYonghong Song 	else
226531d0bc81SAlan Maguire 		btf_show_type_values(show, "0x%llx%016llx", upper_num,
226631d0bc81SAlan Maguire 				     lower_num);
2267b1e8818cSYonghong Song }
2268b1e8818cSYonghong Song 
btf_int128_shift(u64 * print_num,u16 left_shift_bits,u16 right_shift_bits)2269b1e8818cSYonghong Song static void btf_int128_shift(u64 *print_num, u16 left_shift_bits,
2270b1e8818cSYonghong Song 			     u16 right_shift_bits)
2271b1e8818cSYonghong Song {
2272b1e8818cSYonghong Song 	u64 upper_num, lower_num;
2273b1e8818cSYonghong Song 
2274b1e8818cSYonghong Song #ifdef __BIG_ENDIAN_BITFIELD
2275b1e8818cSYonghong Song 	upper_num = print_num[0];
2276b1e8818cSYonghong Song 	lower_num = print_num[1];
2277b1e8818cSYonghong Song #else
2278b1e8818cSYonghong Song 	upper_num = print_num[1];
2279b1e8818cSYonghong Song 	lower_num = print_num[0];
2280b1e8818cSYonghong Song #endif
2281b1e8818cSYonghong Song 
2282b1e8818cSYonghong Song 	/* shake out un-needed bits by shift/or operations */
2283b1e8818cSYonghong Song 	if (left_shift_bits >= 64) {
2284b1e8818cSYonghong Song 		upper_num = lower_num << (left_shift_bits - 64);
2285b1e8818cSYonghong Song 		lower_num = 0;
2286b1e8818cSYonghong Song 	} else {
2287b1e8818cSYonghong Song 		upper_num = (upper_num << left_shift_bits) |
2288b1e8818cSYonghong Song 			    (lower_num >> (64 - left_shift_bits));
2289b1e8818cSYonghong Song 		lower_num = lower_num << left_shift_bits;
2290b1e8818cSYonghong Song 	}
2291b1e8818cSYonghong Song 
2292b1e8818cSYonghong Song 	if (right_shift_bits >= 64) {
2293b1e8818cSYonghong Song 		lower_num = upper_num >> (right_shift_bits - 64);
2294b1e8818cSYonghong Song 		upper_num = 0;
2295b1e8818cSYonghong Song 	} else {
2296b1e8818cSYonghong Song 		lower_num = (lower_num >> right_shift_bits) |
2297b1e8818cSYonghong Song 			    (upper_num << (64 - right_shift_bits));
2298b1e8818cSYonghong Song 		upper_num = upper_num >> right_shift_bits;
2299b1e8818cSYonghong Song 	}
2300b1e8818cSYonghong Song 
2301b1e8818cSYonghong Song #ifdef __BIG_ENDIAN_BITFIELD
2302b1e8818cSYonghong Song 	print_num[0] = upper_num;
2303b1e8818cSYonghong Song 	print_num[1] = lower_num;
2304b1e8818cSYonghong Song #else
2305b1e8818cSYonghong Song 	print_num[0] = lower_num;
2306b1e8818cSYonghong Song 	print_num[1] = upper_num;
2307b1e8818cSYonghong Song #endif
2308b1e8818cSYonghong Song }
2309b1e8818cSYonghong Song 
btf_bitfield_show(void * data,u8 bits_offset,u8 nr_bits,struct btf_show * show)231031d0bc81SAlan Maguire static void btf_bitfield_show(void *data, u8 bits_offset,
231131d0bc81SAlan Maguire 			      u8 nr_bits, struct btf_show *show)
2312b00b8daeSMartin KaFai Lau {
2313b65f370dSOkash Khawaja 	u16 left_shift_bits, right_shift_bits;
231436fc3c8cSMartin KaFai Lau 	u8 nr_copy_bytes;
231536fc3c8cSMartin KaFai Lau 	u8 nr_copy_bits;
2316b1e8818cSYonghong Song 	u64 print_num[2] = {};
2317b00b8daeSMartin KaFai Lau 
2318b00b8daeSMartin KaFai Lau 	nr_copy_bits = nr_bits + bits_offset;
2319b00b8daeSMartin KaFai Lau 	nr_copy_bytes = BITS_ROUNDUP_BYTES(nr_copy_bits);
2320b00b8daeSMartin KaFai Lau 
2321b1e8818cSYonghong Song 	memcpy(print_num, data, nr_copy_bytes);
2322b00b8daeSMartin KaFai Lau 
2323b65f370dSOkash Khawaja #ifdef __BIG_ENDIAN_BITFIELD
2324b65f370dSOkash Khawaja 	left_shift_bits = bits_offset;
2325b65f370dSOkash Khawaja #else
2326b1e8818cSYonghong Song 	left_shift_bits = BITS_PER_U128 - nr_copy_bits;
2327b65f370dSOkash Khawaja #endif
2328b1e8818cSYonghong Song 	right_shift_bits = BITS_PER_U128 - nr_bits;
2329b00b8daeSMartin KaFai Lau 
2330b1e8818cSYonghong Song 	btf_int128_shift(print_num, left_shift_bits, right_shift_bits);
233131d0bc81SAlan Maguire 	btf_int128_print(show, print_num);
2332b00b8daeSMartin KaFai Lau }
2333b00b8daeSMartin KaFai Lau 
23349d5f9f70SYonghong Song 
btf_int_bits_show(const struct btf * btf,const struct btf_type * t,void * data,u8 bits_offset,struct btf_show * show)233531d0bc81SAlan Maguire static void btf_int_bits_show(const struct btf *btf,
2336f97be3abSYonghong Song 			      const struct btf_type *t,
2337f97be3abSYonghong Song 			      void *data, u8 bits_offset,
233831d0bc81SAlan Maguire 			      struct btf_show *show)
2339f97be3abSYonghong Song {
2340f97be3abSYonghong Song 	u32 int_data = btf_type_int(t);
2341f97be3abSYonghong Song 	u8 nr_bits = BTF_INT_BITS(int_data);
2342f97be3abSYonghong Song 	u8 total_bits_offset;
2343f97be3abSYonghong Song 
2344f97be3abSYonghong Song 	/*
2345f97be3abSYonghong Song 	 * bits_offset is at most 7.
2346b1e8818cSYonghong Song 	 * BTF_INT_OFFSET() cannot exceed 128 bits.
2347f97be3abSYonghong Song 	 */
2348f97be3abSYonghong Song 	total_bits_offset = bits_offset + BTF_INT_OFFSET(int_data);
234917e3ac81SYonghong Song 	data += BITS_ROUNDDOWN_BYTES(total_bits_offset);
235017e3ac81SYonghong Song 	bits_offset = BITS_PER_BYTE_MASKED(total_bits_offset);
235131d0bc81SAlan Maguire 	btf_bitfield_show(data, bits_offset, nr_bits, show);
2352f97be3abSYonghong Song }
2353f97be3abSYonghong Song 
btf_int_show(const struct btf * btf,const struct btf_type * t,u32 type_id,void * data,u8 bits_offset,struct btf_show * show)235431d0bc81SAlan Maguire static void btf_int_show(const struct btf *btf, const struct btf_type *t,
2355b00b8daeSMartin KaFai Lau 			 u32 type_id, void *data, u8 bits_offset,
235631d0bc81SAlan Maguire 			 struct btf_show *show)
2357b00b8daeSMartin KaFai Lau {
2358b00b8daeSMartin KaFai Lau 	u32 int_data = btf_type_int(t);
2359b00b8daeSMartin KaFai Lau 	u8 encoding = BTF_INT_ENCODING(int_data);
2360b00b8daeSMartin KaFai Lau 	bool sign = encoding & BTF_INT_SIGNED;
236136fc3c8cSMartin KaFai Lau 	u8 nr_bits = BTF_INT_BITS(int_data);
236231d0bc81SAlan Maguire 	void *safe_data;
236331d0bc81SAlan Maguire 
236431d0bc81SAlan Maguire 	safe_data = btf_show_start_type(show, t, type_id, data);
236531d0bc81SAlan Maguire 	if (!safe_data)
236631d0bc81SAlan Maguire 		return;
2367b00b8daeSMartin KaFai Lau 
2368b00b8daeSMartin KaFai Lau 	if (bits_offset || BTF_INT_OFFSET(int_data) ||
2369b00b8daeSMartin KaFai Lau 	    BITS_PER_BYTE_MASKED(nr_bits)) {
237031d0bc81SAlan Maguire 		btf_int_bits_show(btf, t, safe_data, bits_offset, show);
237131d0bc81SAlan Maguire 		goto out;
2372b00b8daeSMartin KaFai Lau 	}
2373b00b8daeSMartin KaFai Lau 
2374b00b8daeSMartin KaFai Lau 	switch (nr_bits) {
2375b1e8818cSYonghong Song 	case 128:
237631d0bc81SAlan Maguire 		btf_int128_print(show, safe_data);
2377b1e8818cSYonghong Song 		break;
2378b00b8daeSMartin KaFai Lau 	case 64:
2379b00b8daeSMartin KaFai Lau 		if (sign)
238031d0bc81SAlan Maguire 			btf_show_type_value(show, "%lld", *(s64 *)safe_data);
2381b00b8daeSMartin KaFai Lau 		else
238231d0bc81SAlan Maguire 			btf_show_type_value(show, "%llu", *(u64 *)safe_data);
2383b00b8daeSMartin KaFai Lau 		break;
2384b00b8daeSMartin KaFai Lau 	case 32:
2385b00b8daeSMartin KaFai Lau 		if (sign)
238631d0bc81SAlan Maguire 			btf_show_type_value(show, "%d", *(s32 *)safe_data);
2387b00b8daeSMartin KaFai Lau 		else
238831d0bc81SAlan Maguire 			btf_show_type_value(show, "%u", *(u32 *)safe_data);
2389b00b8daeSMartin KaFai Lau 		break;
2390b00b8daeSMartin KaFai Lau 	case 16:
2391b00b8daeSMartin KaFai Lau 		if (sign)
239231d0bc81SAlan Maguire 			btf_show_type_value(show, "%d", *(s16 *)safe_data);
2393b00b8daeSMartin KaFai Lau 		else
239431d0bc81SAlan Maguire 			btf_show_type_value(show, "%u", *(u16 *)safe_data);
2395b00b8daeSMartin KaFai Lau 		break;
2396b00b8daeSMartin KaFai Lau 	case 8:
239731d0bc81SAlan Maguire 		if (show->state.array_encoding == BTF_INT_CHAR) {
239831d0bc81SAlan Maguire 			/* check for null terminator */
239931d0bc81SAlan Maguire 			if (show->state.array_terminated)
240031d0bc81SAlan Maguire 				break;
240131d0bc81SAlan Maguire 			if (*(char *)data == '\0') {
240231d0bc81SAlan Maguire 				show->state.array_terminated = 1;
240331d0bc81SAlan Maguire 				break;
240431d0bc81SAlan Maguire 			}
240531d0bc81SAlan Maguire 			if (isprint(*(char *)data)) {
240631d0bc81SAlan Maguire 				btf_show_type_value(show, "'%c'",
240731d0bc81SAlan Maguire 						    *(char *)safe_data);
240831d0bc81SAlan Maguire 				break;
240931d0bc81SAlan Maguire 			}
241031d0bc81SAlan Maguire 		}
2411b00b8daeSMartin KaFai Lau 		if (sign)
241231d0bc81SAlan Maguire 			btf_show_type_value(show, "%d", *(s8 *)safe_data);
2413b00b8daeSMartin KaFai Lau 		else
241431d0bc81SAlan Maguire 			btf_show_type_value(show, "%u", *(u8 *)safe_data);
2415b00b8daeSMartin KaFai Lau 		break;
2416b00b8daeSMartin KaFai Lau 	default:
241731d0bc81SAlan Maguire 		btf_int_bits_show(btf, t, safe_data, bits_offset, show);
241831d0bc81SAlan Maguire 		break;
2419b00b8daeSMartin KaFai Lau 	}
242031d0bc81SAlan Maguire out:
242131d0bc81SAlan Maguire 	btf_show_end_type(show);
2422b00b8daeSMartin KaFai Lau }
2423b00b8daeSMartin KaFai Lau 
242469b693f0SMartin KaFai Lau static const struct btf_kind_operations int_ops = {
242569b693f0SMartin KaFai Lau 	.check_meta = btf_int_check_meta,
2426eb3f595dSMartin KaFai Lau 	.resolve = btf_df_resolve,
2427179cde8cSMartin KaFai Lau 	.check_member = btf_int_check_member,
24289d5f9f70SYonghong Song 	.check_kflag_member = btf_int_check_kflag_member,
242969b693f0SMartin KaFai Lau 	.log_details = btf_int_log,
243031d0bc81SAlan Maguire 	.show = btf_int_show,
243169b693f0SMartin KaFai Lau };
243269b693f0SMartin KaFai Lau 
btf_modifier_check_member(struct btf_verifier_env * env,const struct btf_type * struct_type,const struct btf_member * member,const struct btf_type * member_type)2433179cde8cSMartin KaFai Lau static int btf_modifier_check_member(struct btf_verifier_env *env,
2434179cde8cSMartin KaFai Lau 				     const struct btf_type *struct_type,
2435179cde8cSMartin KaFai Lau 				     const struct btf_member *member,
2436179cde8cSMartin KaFai Lau 				     const struct btf_type *member_type)
2437179cde8cSMartin KaFai Lau {
2438179cde8cSMartin KaFai Lau 	const struct btf_type *resolved_type;
2439179cde8cSMartin KaFai Lau 	u32 resolved_type_id = member->type;
2440179cde8cSMartin KaFai Lau 	struct btf_member resolved_member;
2441179cde8cSMartin KaFai Lau 	struct btf *btf = env->btf;
2442179cde8cSMartin KaFai Lau 
2443179cde8cSMartin KaFai Lau 	resolved_type = btf_type_id_size(btf, &resolved_type_id, NULL);
2444179cde8cSMartin KaFai Lau 	if (!resolved_type) {
2445179cde8cSMartin KaFai Lau 		btf_verifier_log_member(env, struct_type, member,
2446179cde8cSMartin KaFai Lau 					"Invalid member");
2447179cde8cSMartin KaFai Lau 		return -EINVAL;
2448179cde8cSMartin KaFai Lau 	}
2449179cde8cSMartin KaFai Lau 
2450179cde8cSMartin KaFai Lau 	resolved_member = *member;
2451179cde8cSMartin KaFai Lau 	resolved_member.type = resolved_type_id;
2452179cde8cSMartin KaFai Lau 
2453179cde8cSMartin KaFai Lau 	return btf_type_ops(resolved_type)->check_member(env, struct_type,
2454179cde8cSMartin KaFai Lau 							 &resolved_member,
2455179cde8cSMartin KaFai Lau 							 resolved_type);
2456179cde8cSMartin KaFai Lau }
2457179cde8cSMartin KaFai Lau 
btf_modifier_check_kflag_member(struct btf_verifier_env * env,const struct btf_type * struct_type,const struct btf_member * member,const struct btf_type * member_type)24589d5f9f70SYonghong Song static int btf_modifier_check_kflag_member(struct btf_verifier_env *env,
24599d5f9f70SYonghong Song 					   const struct btf_type *struct_type,
24609d5f9f70SYonghong Song 					   const struct btf_member *member,
24619d5f9f70SYonghong Song 					   const struct btf_type *member_type)
24629d5f9f70SYonghong Song {
24639d5f9f70SYonghong Song 	const struct btf_type *resolved_type;
24649d5f9f70SYonghong Song 	u32 resolved_type_id = member->type;
24659d5f9f70SYonghong Song 	struct btf_member resolved_member;
24669d5f9f70SYonghong Song 	struct btf *btf = env->btf;
24679d5f9f70SYonghong Song 
24689d5f9f70SYonghong Song 	resolved_type = btf_type_id_size(btf, &resolved_type_id, NULL);
24699d5f9f70SYonghong Song 	if (!resolved_type) {
24709d5f9f70SYonghong Song 		btf_verifier_log_member(env, struct_type, member,
24719d5f9f70SYonghong Song 					"Invalid member");
24729d5f9f70SYonghong Song 		return -EINVAL;
24739d5f9f70SYonghong Song 	}
24749d5f9f70SYonghong Song 
24759d5f9f70SYonghong Song 	resolved_member = *member;
24769d5f9f70SYonghong Song 	resolved_member.type = resolved_type_id;
24779d5f9f70SYonghong Song 
24789d5f9f70SYonghong Song 	return btf_type_ops(resolved_type)->check_kflag_member(env, struct_type,
24799d5f9f70SYonghong Song 							       &resolved_member,
24809d5f9f70SYonghong Song 							       resolved_type);
24819d5f9f70SYonghong Song }
24829d5f9f70SYonghong Song 
btf_ptr_check_member(struct btf_verifier_env * env,const struct btf_type * struct_type,const struct btf_member * member,const struct btf_type * member_type)2483179cde8cSMartin KaFai Lau static int btf_ptr_check_member(struct btf_verifier_env *env,
2484179cde8cSMartin KaFai Lau 				const struct btf_type *struct_type,
2485179cde8cSMartin KaFai Lau 				const struct btf_member *member,
2486179cde8cSMartin KaFai Lau 				const struct btf_type *member_type)
2487179cde8cSMartin KaFai Lau {
2488179cde8cSMartin KaFai Lau 	u32 struct_size, struct_bits_off, bytes_offset;
2489179cde8cSMartin KaFai Lau 
2490179cde8cSMartin KaFai Lau 	struct_size = struct_type->size;
2491179cde8cSMartin KaFai Lau 	struct_bits_off = member->offset;
2492179cde8cSMartin KaFai Lau 	bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
2493179cde8cSMartin KaFai Lau 
2494179cde8cSMartin KaFai Lau 	if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
2495179cde8cSMartin KaFai Lau 		btf_verifier_log_member(env, struct_type, member,
2496179cde8cSMartin KaFai Lau 					"Member is not byte aligned");
2497179cde8cSMartin KaFai Lau 		return -EINVAL;
2498179cde8cSMartin KaFai Lau 	}
2499179cde8cSMartin KaFai Lau 
2500179cde8cSMartin KaFai Lau 	if (struct_size - bytes_offset < sizeof(void *)) {
2501179cde8cSMartin KaFai Lau 		btf_verifier_log_member(env, struct_type, member,
2502179cde8cSMartin KaFai Lau 					"Member exceeds struct_size");
2503179cde8cSMartin KaFai Lau 		return -EINVAL;
2504179cde8cSMartin KaFai Lau 	}
2505179cde8cSMartin KaFai Lau 
2506179cde8cSMartin KaFai Lau 	return 0;
2507179cde8cSMartin KaFai Lau }
2508179cde8cSMartin KaFai Lau 
btf_ref_type_check_meta(struct btf_verifier_env * env,const struct btf_type * t,u32 meta_left)250969b693f0SMartin KaFai Lau static int btf_ref_type_check_meta(struct btf_verifier_env *env,
251069b693f0SMartin KaFai Lau 				   const struct btf_type *t,
251169b693f0SMartin KaFai Lau 				   u32 meta_left)
251269b693f0SMartin KaFai Lau {
25138c42d2faSYonghong Song 	const char *value;
25148c42d2faSYonghong Song 
251569b693f0SMartin KaFai Lau 	if (btf_type_vlen(t)) {
251669b693f0SMartin KaFai Lau 		btf_verifier_log_type(env, t, "vlen != 0");
251769b693f0SMartin KaFai Lau 		return -EINVAL;
251869b693f0SMartin KaFai Lau 	}
251969b693f0SMartin KaFai Lau 
25209d5f9f70SYonghong Song 	if (btf_type_kflag(t)) {
25219d5f9f70SYonghong Song 		btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
25229d5f9f70SYonghong Song 		return -EINVAL;
25239d5f9f70SYonghong Song 	}
25249d5f9f70SYonghong Song 
2525aea2f7b8SMartin KaFai Lau 	if (!BTF_TYPE_ID_VALID(t->type)) {
252669b693f0SMartin KaFai Lau 		btf_verifier_log_type(env, t, "Invalid type_id");
252769b693f0SMartin KaFai Lau 		return -EINVAL;
252869b693f0SMartin KaFai Lau 	}
252969b693f0SMartin KaFai Lau 
25308c42d2faSYonghong Song 	/* typedef/type_tag type must have a valid name, and other ref types,
2531eb04bbb6SYonghong Song 	 * volatile, const, restrict, should have a null name.
2532eb04bbb6SYonghong Song 	 */
2533eb04bbb6SYonghong Song 	if (BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF) {
2534eb04bbb6SYonghong Song 		if (!t->name_off ||
2535eb04bbb6SYonghong Song 		    !btf_name_valid_identifier(env->btf, t->name_off)) {
2536eb04bbb6SYonghong Song 			btf_verifier_log_type(env, t, "Invalid name");
2537eb04bbb6SYonghong Song 			return -EINVAL;
2538eb04bbb6SYonghong Song 		}
25398c42d2faSYonghong Song 	} else if (BTF_INFO_KIND(t->info) == BTF_KIND_TYPE_TAG) {
25408c42d2faSYonghong Song 		value = btf_name_by_offset(env->btf, t->name_off);
25418c42d2faSYonghong Song 		if (!value || !value[0]) {
25428c42d2faSYonghong Song 			btf_verifier_log_type(env, t, "Invalid name");
25438c42d2faSYonghong Song 			return -EINVAL;
25448c42d2faSYonghong Song 		}
2545eb04bbb6SYonghong Song 	} else {
2546eb04bbb6SYonghong Song 		if (t->name_off) {
2547eb04bbb6SYonghong Song 			btf_verifier_log_type(env, t, "Invalid name");
2548eb04bbb6SYonghong Song 			return -EINVAL;
2549eb04bbb6SYonghong Song 		}
2550eb04bbb6SYonghong Song 	}
2551eb04bbb6SYonghong Song 
255269b693f0SMartin KaFai Lau 	btf_verifier_log_type(env, t, NULL);
255369b693f0SMartin KaFai Lau 
255469b693f0SMartin KaFai Lau 	return 0;
255569b693f0SMartin KaFai Lau }
255669b693f0SMartin KaFai Lau 
btf_modifier_resolve(struct btf_verifier_env * env,const struct resolve_vertex * v)2557eb3f595dSMartin KaFai Lau static int btf_modifier_resolve(struct btf_verifier_env *env,
2558eb3f595dSMartin KaFai Lau 				const struct resolve_vertex *v)
2559eb3f595dSMartin KaFai Lau {
2560eb3f595dSMartin KaFai Lau 	const struct btf_type *t = v->t;
2561eb3f595dSMartin KaFai Lau 	const struct btf_type *next_type;
2562eb3f595dSMartin KaFai Lau 	u32 next_type_id = t->type;
2563eb3f595dSMartin KaFai Lau 	struct btf *btf = env->btf;
2564eb3f595dSMartin KaFai Lau 
2565eb3f595dSMartin KaFai Lau 	next_type = btf_type_by_id(btf, next_type_id);
25661dc92851SDaniel Borkmann 	if (!next_type || btf_type_is_resolve_source_only(next_type)) {
2567eb3f595dSMartin KaFai Lau 		btf_verifier_log_type(env, v->t, "Invalid type_id");
2568eb3f595dSMartin KaFai Lau 		return -EINVAL;
2569eb3f595dSMartin KaFai Lau 	}
2570eb3f595dSMartin KaFai Lau 
2571eb3f595dSMartin KaFai Lau 	if (!env_type_is_resolve_sink(env, next_type) &&
2572eb3f595dSMartin KaFai Lau 	    !env_type_is_resolved(env, next_type_id))
2573eb3f595dSMartin KaFai Lau 		return env_stack_push(env, next_type, next_type_id);
2574eb3f595dSMartin KaFai Lau 
2575eb3f595dSMartin KaFai Lau 	/* Figure out the resolved next_type_id with size.
2576eb3f595dSMartin KaFai Lau 	 * They will be stored in the current modifier's
2577eb3f595dSMartin KaFai Lau 	 * resolved_ids and resolved_sizes such that it can
2578eb3f595dSMartin KaFai Lau 	 * save us a few type-following when we use it later (e.g. in
2579eb3f595dSMartin KaFai Lau 	 * pretty print).
2580eb3f595dSMartin KaFai Lau 	 */
25811acc5d5cSAndrii Nakryiko 	if (!btf_type_id_size(btf, &next_type_id, NULL)) {
25822667a262SMartin KaFai Lau 		if (env_type_is_resolved(env, next_type_id))
25832667a262SMartin KaFai Lau 			next_type = btf_type_id_resolve(btf, &next_type_id);
25842667a262SMartin KaFai Lau 
25852667a262SMartin KaFai Lau 		/* "typedef void new_void", "const void"...etc */
25862667a262SMartin KaFai Lau 		if (!btf_type_is_void(next_type) &&
258781f5c6f5SYonghong Song 		    !btf_type_is_fwd(next_type) &&
258881f5c6f5SYonghong Song 		    !btf_type_is_func_proto(next_type)) {
2589eb3f595dSMartin KaFai Lau 			btf_verifier_log_type(env, v->t, "Invalid type_id");
2590eb3f595dSMartin KaFai Lau 			return -EINVAL;
2591eb3f595dSMartin KaFai Lau 		}
25922667a262SMartin KaFai Lau 	}
2593eb3f595dSMartin KaFai Lau 
25941acc5d5cSAndrii Nakryiko 	env_stack_pop_resolved(env, next_type_id, 0);
2595eb3f595dSMartin KaFai Lau 
2596eb3f595dSMartin KaFai Lau 	return 0;
2597eb3f595dSMartin KaFai Lau }
2598eb3f595dSMartin KaFai Lau 
btf_var_resolve(struct btf_verifier_env * env,const struct resolve_vertex * v)25991dc92851SDaniel Borkmann static int btf_var_resolve(struct btf_verifier_env *env,
26001dc92851SDaniel Borkmann 			   const struct resolve_vertex *v)
26011dc92851SDaniel Borkmann {
26021dc92851SDaniel Borkmann 	const struct btf_type *next_type;
26031dc92851SDaniel Borkmann 	const struct btf_type *t = v->t;
26041dc92851SDaniel Borkmann 	u32 next_type_id = t->type;
26051dc92851SDaniel Borkmann 	struct btf *btf = env->btf;
26061dc92851SDaniel Borkmann 
26071dc92851SDaniel Borkmann 	next_type = btf_type_by_id(btf, next_type_id);
26081dc92851SDaniel Borkmann 	if (!next_type || btf_type_is_resolve_source_only(next_type)) {
26091dc92851SDaniel Borkmann 		btf_verifier_log_type(env, v->t, "Invalid type_id");
26101dc92851SDaniel Borkmann 		return -EINVAL;
26111dc92851SDaniel Borkmann 	}
26121dc92851SDaniel Borkmann 
26131dc92851SDaniel Borkmann 	if (!env_type_is_resolve_sink(env, next_type) &&
26141dc92851SDaniel Borkmann 	    !env_type_is_resolved(env, next_type_id))
26151dc92851SDaniel Borkmann 		return env_stack_push(env, next_type, next_type_id);
26161dc92851SDaniel Borkmann 
26171dc92851SDaniel Borkmann 	if (btf_type_is_modifier(next_type)) {
26181dc92851SDaniel Borkmann 		const struct btf_type *resolved_type;
26191dc92851SDaniel Borkmann 		u32 resolved_type_id;
26201dc92851SDaniel Borkmann 
26211dc92851SDaniel Borkmann 		resolved_type_id = next_type_id;
26221dc92851SDaniel Borkmann 		resolved_type = btf_type_id_resolve(btf, &resolved_type_id);
26231dc92851SDaniel Borkmann 
26241dc92851SDaniel Borkmann 		if (btf_type_is_ptr(resolved_type) &&
26251dc92851SDaniel Borkmann 		    !env_type_is_resolve_sink(env, resolved_type) &&
26261dc92851SDaniel Borkmann 		    !env_type_is_resolved(env, resolved_type_id))
26271dc92851SDaniel Borkmann 			return env_stack_push(env, resolved_type,
26281dc92851SDaniel Borkmann 					      resolved_type_id);
26291dc92851SDaniel Borkmann 	}
26301dc92851SDaniel Borkmann 
26311dc92851SDaniel Borkmann 	/* We must resolve to something concrete at this point, no
26321dc92851SDaniel Borkmann 	 * forward types or similar that would resolve to size of
26331dc92851SDaniel Borkmann 	 * zero is allowed.
26341dc92851SDaniel Borkmann 	 */
26351acc5d5cSAndrii Nakryiko 	if (!btf_type_id_size(btf, &next_type_id, NULL)) {
26361dc92851SDaniel Borkmann 		btf_verifier_log_type(env, v->t, "Invalid type_id");
26371dc92851SDaniel Borkmann 		return -EINVAL;
26381dc92851SDaniel Borkmann 	}
26391dc92851SDaniel Borkmann 
26401acc5d5cSAndrii Nakryiko 	env_stack_pop_resolved(env, next_type_id, 0);
26411dc92851SDaniel Borkmann 
26421dc92851SDaniel Borkmann 	return 0;
26431dc92851SDaniel Borkmann }
26441dc92851SDaniel Borkmann 
btf_ptr_resolve(struct btf_verifier_env * env,const struct resolve_vertex * v)2645eb3f595dSMartin KaFai Lau static int btf_ptr_resolve(struct btf_verifier_env *env,
2646eb3f595dSMartin KaFai Lau 			   const struct resolve_vertex *v)
2647eb3f595dSMartin KaFai Lau {
2648eb3f595dSMartin KaFai Lau 	const struct btf_type *next_type;
2649eb3f595dSMartin KaFai Lau 	const struct btf_type *t = v->t;
2650eb3f595dSMartin KaFai Lau 	u32 next_type_id = t->type;
2651eb3f595dSMartin KaFai Lau 	struct btf *btf = env->btf;
2652eb3f595dSMartin KaFai Lau 
2653eb3f595dSMartin KaFai Lau 	next_type = btf_type_by_id(btf, next_type_id);
26541dc92851SDaniel Borkmann 	if (!next_type || btf_type_is_resolve_source_only(next_type)) {
2655eb3f595dSMartin KaFai Lau 		btf_verifier_log_type(env, v->t, "Invalid type_id");
2656eb3f595dSMartin KaFai Lau 		return -EINVAL;
2657eb3f595dSMartin KaFai Lau 	}
2658eb3f595dSMartin KaFai Lau 
2659eb3f595dSMartin KaFai Lau 	if (!env_type_is_resolve_sink(env, next_type) &&
2660eb3f595dSMartin KaFai Lau 	    !env_type_is_resolved(env, next_type_id))
2661eb3f595dSMartin KaFai Lau 		return env_stack_push(env, next_type, next_type_id);
2662eb3f595dSMartin KaFai Lau 
2663eb3f595dSMartin KaFai Lau 	/* If the modifier was RESOLVED during RESOLVE_STRUCT_OR_ARRAY,
2664eb3f595dSMartin KaFai Lau 	 * the modifier may have stopped resolving when it was resolved
2665eb3f595dSMartin KaFai Lau 	 * to a ptr (last-resolved-ptr).
2666eb3f595dSMartin KaFai Lau 	 *
2667eb3f595dSMartin KaFai Lau 	 * We now need to continue from the last-resolved-ptr to
2668eb3f595dSMartin KaFai Lau 	 * ensure the last-resolved-ptr will not referring back to
2669c561d110STom Rix 	 * the current ptr (t).
2670eb3f595dSMartin KaFai Lau 	 */
2671eb3f595dSMartin KaFai Lau 	if (btf_type_is_modifier(next_type)) {
2672eb3f595dSMartin KaFai Lau 		const struct btf_type *resolved_type;
2673eb3f595dSMartin KaFai Lau 		u32 resolved_type_id;
2674eb3f595dSMartin KaFai Lau 
2675eb3f595dSMartin KaFai Lau 		resolved_type_id = next_type_id;
2676eb3f595dSMartin KaFai Lau 		resolved_type = btf_type_id_resolve(btf, &resolved_type_id);
2677eb3f595dSMartin KaFai Lau 
2678eb3f595dSMartin KaFai Lau 		if (btf_type_is_ptr(resolved_type) &&
2679eb3f595dSMartin KaFai Lau 		    !env_type_is_resolve_sink(env, resolved_type) &&
2680eb3f595dSMartin KaFai Lau 		    !env_type_is_resolved(env, resolved_type_id))
2681eb3f595dSMartin KaFai Lau 			return env_stack_push(env, resolved_type,
2682eb3f595dSMartin KaFai Lau 					      resolved_type_id);
2683eb3f595dSMartin KaFai Lau 	}
2684eb3f595dSMartin KaFai Lau 
26852667a262SMartin KaFai Lau 	if (!btf_type_id_size(btf, &next_type_id, NULL)) {
26862667a262SMartin KaFai Lau 		if (env_type_is_resolved(env, next_type_id))
26872667a262SMartin KaFai Lau 			next_type = btf_type_id_resolve(btf, &next_type_id);
26882667a262SMartin KaFai Lau 
26892667a262SMartin KaFai Lau 		if (!btf_type_is_void(next_type) &&
26902667a262SMartin KaFai Lau 		    !btf_type_is_fwd(next_type) &&
26912667a262SMartin KaFai Lau 		    !btf_type_is_func_proto(next_type)) {
2692eb3f595dSMartin KaFai Lau 			btf_verifier_log_type(env, v->t, "Invalid type_id");
2693eb3f595dSMartin KaFai Lau 			return -EINVAL;
2694eb3f595dSMartin KaFai Lau 		}
26952667a262SMartin KaFai Lau 	}
2696eb3f595dSMartin KaFai Lau 
2697eb3f595dSMartin KaFai Lau 	env_stack_pop_resolved(env, next_type_id, 0);
2698eb3f595dSMartin KaFai Lau 
2699eb3f595dSMartin KaFai Lau 	return 0;
2700eb3f595dSMartin KaFai Lau }
2701eb3f595dSMartin KaFai Lau 
btf_modifier_show(const struct btf * btf,const struct btf_type * t,u32 type_id,void * data,u8 bits_offset,struct btf_show * show)270231d0bc81SAlan Maguire static void btf_modifier_show(const struct btf *btf,
2703b00b8daeSMartin KaFai Lau 			      const struct btf_type *t,
2704b00b8daeSMartin KaFai Lau 			      u32 type_id, void *data,
270531d0bc81SAlan Maguire 			      u8 bits_offset, struct btf_show *show)
2706b00b8daeSMartin KaFai Lau {
270785d33df3SMartin KaFai Lau 	if (btf->resolved_ids)
2708b00b8daeSMartin KaFai Lau 		t = btf_type_id_resolve(btf, &type_id);
270985d33df3SMartin KaFai Lau 	else
271085d33df3SMartin KaFai Lau 		t = btf_type_skip_modifiers(btf, type_id, NULL);
2711b00b8daeSMartin KaFai Lau 
271231d0bc81SAlan Maguire 	btf_type_ops(t)->show(btf, t, type_id, data, bits_offset, show);
2713b00b8daeSMartin KaFai Lau }
2714b00b8daeSMartin KaFai Lau 
btf_var_show(const struct btf * btf,const struct btf_type * t,u32 type_id,void * data,u8 bits_offset,struct btf_show * show)271531d0bc81SAlan Maguire static void btf_var_show(const struct btf *btf, const struct btf_type *t,
27161dc92851SDaniel Borkmann 			 u32 type_id, void *data, u8 bits_offset,
271731d0bc81SAlan Maguire 			 struct btf_show *show)
27181dc92851SDaniel Borkmann {
27191dc92851SDaniel Borkmann 	t = btf_type_id_resolve(btf, &type_id);
27201dc92851SDaniel Borkmann 
272131d0bc81SAlan Maguire 	btf_type_ops(t)->show(btf, t, type_id, data, bits_offset, show);
27221dc92851SDaniel Borkmann }
27231dc92851SDaniel Borkmann 
btf_ptr_show(const struct btf * btf,const struct btf_type * t,u32 type_id,void * data,u8 bits_offset,struct btf_show * show)272431d0bc81SAlan Maguire static void btf_ptr_show(const struct btf *btf, const struct btf_type *t,
2725b00b8daeSMartin KaFai Lau 			 u32 type_id, void *data, u8 bits_offset,
272631d0bc81SAlan Maguire 			 struct btf_show *show)
2727b00b8daeSMartin KaFai Lau {
272831d0bc81SAlan Maguire 	void *safe_data;
272931d0bc81SAlan Maguire 
273031d0bc81SAlan Maguire 	safe_data = btf_show_start_type(show, t, type_id, data);
273131d0bc81SAlan Maguire 	if (!safe_data)
273231d0bc81SAlan Maguire 		return;
273331d0bc81SAlan Maguire 
273431d0bc81SAlan Maguire 	/* It is a hashed value unless BTF_SHOW_PTR_RAW is specified */
273531d0bc81SAlan Maguire 	if (show->flags & BTF_SHOW_PTR_RAW)
273631d0bc81SAlan Maguire 		btf_show_type_value(show, "0x%px", *(void **)safe_data);
273731d0bc81SAlan Maguire 	else
273831d0bc81SAlan Maguire 		btf_show_type_value(show, "0x%p", *(void **)safe_data);
273931d0bc81SAlan Maguire 	btf_show_end_type(show);
2740b00b8daeSMartin KaFai Lau }
2741b00b8daeSMartin KaFai Lau 
btf_ref_type_log(struct btf_verifier_env * env,const struct btf_type * t)274269b693f0SMartin KaFai Lau static void btf_ref_type_log(struct btf_verifier_env *env,
274369b693f0SMartin KaFai Lau 			     const struct btf_type *t)
274469b693f0SMartin KaFai Lau {
274569b693f0SMartin KaFai Lau 	btf_verifier_log(env, "type_id=%u", t->type);
274669b693f0SMartin KaFai Lau }
274769b693f0SMartin KaFai Lau 
274869b693f0SMartin KaFai Lau static struct btf_kind_operations modifier_ops = {
274969b693f0SMartin KaFai Lau 	.check_meta = btf_ref_type_check_meta,
2750eb3f595dSMartin KaFai Lau 	.resolve = btf_modifier_resolve,
2751179cde8cSMartin KaFai Lau 	.check_member = btf_modifier_check_member,
27529d5f9f70SYonghong Song 	.check_kflag_member = btf_modifier_check_kflag_member,
275369b693f0SMartin KaFai Lau 	.log_details = btf_ref_type_log,
275431d0bc81SAlan Maguire 	.show = btf_modifier_show,
275569b693f0SMartin KaFai Lau };
275669b693f0SMartin KaFai Lau 
275769b693f0SMartin KaFai Lau static struct btf_kind_operations ptr_ops = {
275869b693f0SMartin KaFai Lau 	.check_meta = btf_ref_type_check_meta,
2759eb3f595dSMartin KaFai Lau 	.resolve = btf_ptr_resolve,
2760179cde8cSMartin KaFai Lau 	.check_member = btf_ptr_check_member,
27619d5f9f70SYonghong Song 	.check_kflag_member = btf_generic_check_kflag_member,
276269b693f0SMartin KaFai Lau 	.log_details = btf_ref_type_log,
276331d0bc81SAlan Maguire 	.show = btf_ptr_show,
276469b693f0SMartin KaFai Lau };
276569b693f0SMartin KaFai Lau 
btf_fwd_check_meta(struct btf_verifier_env * env,const struct btf_type * t,u32 meta_left)27668175383fSMartin KaFai Lau static s32 btf_fwd_check_meta(struct btf_verifier_env *env,
27678175383fSMartin KaFai Lau 			      const struct btf_type *t,
27688175383fSMartin KaFai Lau 			      u32 meta_left)
27698175383fSMartin KaFai Lau {
27708175383fSMartin KaFai Lau 	if (btf_type_vlen(t)) {
27718175383fSMartin KaFai Lau 		btf_verifier_log_type(env, t, "vlen != 0");
27728175383fSMartin KaFai Lau 		return -EINVAL;
27738175383fSMartin KaFai Lau 	}
27748175383fSMartin KaFai Lau 
27758175383fSMartin KaFai Lau 	if (t->type) {
27768175383fSMartin KaFai Lau 		btf_verifier_log_type(env, t, "type != 0");
27778175383fSMartin KaFai Lau 		return -EINVAL;
27788175383fSMartin KaFai Lau 	}
27798175383fSMartin KaFai Lau 
2780eb04bbb6SYonghong Song 	/* fwd type must have a valid name */
2781eb04bbb6SYonghong Song 	if (!t->name_off ||
2782eb04bbb6SYonghong Song 	    !btf_name_valid_identifier(env->btf, t->name_off)) {
2783eb04bbb6SYonghong Song 		btf_verifier_log_type(env, t, "Invalid name");
2784eb04bbb6SYonghong Song 		return -EINVAL;
2785eb04bbb6SYonghong Song 	}
2786eb04bbb6SYonghong Song 
27878175383fSMartin KaFai Lau 	btf_verifier_log_type(env, t, NULL);
27888175383fSMartin KaFai Lau 
27898175383fSMartin KaFai Lau 	return 0;
27908175383fSMartin KaFai Lau }
27918175383fSMartin KaFai Lau 
btf_fwd_type_log(struct btf_verifier_env * env,const struct btf_type * t)279276c43ae8SYonghong Song static void btf_fwd_type_log(struct btf_verifier_env *env,
279376c43ae8SYonghong Song 			     const struct btf_type *t)
279476c43ae8SYonghong Song {
279576c43ae8SYonghong Song 	btf_verifier_log(env, "%s", btf_type_kflag(t) ? "union" : "struct");
279676c43ae8SYonghong Song }
279776c43ae8SYonghong Song 
279869b693f0SMartin KaFai Lau static struct btf_kind_operations fwd_ops = {
27998175383fSMartin KaFai Lau 	.check_meta = btf_fwd_check_meta,
2800eb3f595dSMartin KaFai Lau 	.resolve = btf_df_resolve,
2801179cde8cSMartin KaFai Lau 	.check_member = btf_df_check_member,
28029d5f9f70SYonghong Song 	.check_kflag_member = btf_df_check_kflag_member,
280376c43ae8SYonghong Song 	.log_details = btf_fwd_type_log,
280431d0bc81SAlan Maguire 	.show = btf_df_show,
280569b693f0SMartin KaFai Lau };
280669b693f0SMartin KaFai Lau 
btf_array_check_member(struct btf_verifier_env * env,const struct btf_type * struct_type,const struct btf_member * member,const struct btf_type * member_type)2807179cde8cSMartin KaFai Lau static int btf_array_check_member(struct btf_verifier_env *env,
2808179cde8cSMartin KaFai Lau 				  const struct btf_type *struct_type,
2809179cde8cSMartin KaFai Lau 				  const struct btf_member *member,
2810179cde8cSMartin KaFai Lau 				  const struct btf_type *member_type)
2811179cde8cSMartin KaFai Lau {
2812179cde8cSMartin KaFai Lau 	u32 struct_bits_off = member->offset;
2813179cde8cSMartin KaFai Lau 	u32 struct_size, bytes_offset;
2814179cde8cSMartin KaFai Lau 	u32 array_type_id, array_size;
2815179cde8cSMartin KaFai Lau 	struct btf *btf = env->btf;
2816179cde8cSMartin KaFai Lau 
2817179cde8cSMartin KaFai Lau 	if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
2818179cde8cSMartin KaFai Lau 		btf_verifier_log_member(env, struct_type, member,
2819179cde8cSMartin KaFai Lau 					"Member is not byte aligned");
2820179cde8cSMartin KaFai Lau 		return -EINVAL;
2821179cde8cSMartin KaFai Lau 	}
2822179cde8cSMartin KaFai Lau 
2823179cde8cSMartin KaFai Lau 	array_type_id = member->type;
2824179cde8cSMartin KaFai Lau 	btf_type_id_size(btf, &array_type_id, &array_size);
2825179cde8cSMartin KaFai Lau 	struct_size = struct_type->size;
2826179cde8cSMartin KaFai Lau 	bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
2827179cde8cSMartin KaFai Lau 	if (struct_size - bytes_offset < array_size) {
2828179cde8cSMartin KaFai Lau 		btf_verifier_log_member(env, struct_type, member,
2829179cde8cSMartin KaFai Lau 					"Member exceeds struct_size");
2830179cde8cSMartin KaFai Lau 		return -EINVAL;
2831179cde8cSMartin KaFai Lau 	}
2832179cde8cSMartin KaFai Lau 
2833179cde8cSMartin KaFai Lau 	return 0;
2834179cde8cSMartin KaFai Lau }
2835179cde8cSMartin KaFai Lau 
btf_array_check_meta(struct btf_verifier_env * env,const struct btf_type * t,u32 meta_left)283669b693f0SMartin KaFai Lau static s32 btf_array_check_meta(struct btf_verifier_env *env,
283769b693f0SMartin KaFai Lau 				const struct btf_type *t,
283869b693f0SMartin KaFai Lau 				u32 meta_left)
283969b693f0SMartin KaFai Lau {
284069b693f0SMartin KaFai Lau 	const struct btf_array *array = btf_type_array(t);
284169b693f0SMartin KaFai Lau 	u32 meta_needed = sizeof(*array);
284269b693f0SMartin KaFai Lau 
284369b693f0SMartin KaFai Lau 	if (meta_left < meta_needed) {
284469b693f0SMartin KaFai Lau 		btf_verifier_log_basic(env, t,
284569b693f0SMartin KaFai Lau 				       "meta_left:%u meta_needed:%u",
284669b693f0SMartin KaFai Lau 				       meta_left, meta_needed);
284769b693f0SMartin KaFai Lau 		return -EINVAL;
284869b693f0SMartin KaFai Lau 	}
284969b693f0SMartin KaFai Lau 
2850eb04bbb6SYonghong Song 	/* array type should not have a name */
2851eb04bbb6SYonghong Song 	if (t->name_off) {
2852eb04bbb6SYonghong Song 		btf_verifier_log_type(env, t, "Invalid name");
2853eb04bbb6SYonghong Song 		return -EINVAL;
2854eb04bbb6SYonghong Song 	}
2855eb04bbb6SYonghong Song 
285669b693f0SMartin KaFai Lau 	if (btf_type_vlen(t)) {
285769b693f0SMartin KaFai Lau 		btf_verifier_log_type(env, t, "vlen != 0");
285869b693f0SMartin KaFai Lau 		return -EINVAL;
285969b693f0SMartin KaFai Lau 	}
286069b693f0SMartin KaFai Lau 
28619d5f9f70SYonghong Song 	if (btf_type_kflag(t)) {
28629d5f9f70SYonghong Song 		btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
28639d5f9f70SYonghong Song 		return -EINVAL;
28649d5f9f70SYonghong Song 	}
28659d5f9f70SYonghong Song 
2866b9308ae6SMartin KaFai Lau 	if (t->size) {
2867b9308ae6SMartin KaFai Lau 		btf_verifier_log_type(env, t, "size != 0");
2868b9308ae6SMartin KaFai Lau 		return -EINVAL;
2869b9308ae6SMartin KaFai Lau 	}
2870b9308ae6SMartin KaFai Lau 
28714ef5f574SMartin KaFai Lau 	/* Array elem type and index type cannot be in type void,
28724ef5f574SMartin KaFai Lau 	 * so !array->type and !array->index_type are not allowed.
287369b693f0SMartin KaFai Lau 	 */
2874aea2f7b8SMartin KaFai Lau 	if (!array->type || !BTF_TYPE_ID_VALID(array->type)) {
28754ef5f574SMartin KaFai Lau 		btf_verifier_log_type(env, t, "Invalid elem");
28764ef5f574SMartin KaFai Lau 		return -EINVAL;
28774ef5f574SMartin KaFai Lau 	}
28784ef5f574SMartin KaFai Lau 
2879aea2f7b8SMartin KaFai Lau 	if (!array->index_type || !BTF_TYPE_ID_VALID(array->index_type)) {
28804ef5f574SMartin KaFai Lau 		btf_verifier_log_type(env, t, "Invalid index");
288169b693f0SMartin KaFai Lau 		return -EINVAL;
288269b693f0SMartin KaFai Lau 	}
288369b693f0SMartin KaFai Lau 
288469b693f0SMartin KaFai Lau 	btf_verifier_log_type(env, t, NULL);
288569b693f0SMartin KaFai Lau 
288669b693f0SMartin KaFai Lau 	return meta_needed;
288769b693f0SMartin KaFai Lau }
288869b693f0SMartin KaFai Lau 
btf_array_resolve(struct btf_verifier_env * env,const struct resolve_vertex * v)2889eb3f595dSMartin KaFai Lau static int btf_array_resolve(struct btf_verifier_env *env,
2890eb3f595dSMartin KaFai Lau 			     const struct resolve_vertex *v)
2891eb3f595dSMartin KaFai Lau {
2892eb3f595dSMartin KaFai Lau 	const struct btf_array *array = btf_type_array(v->t);
28934ef5f574SMartin KaFai Lau 	const struct btf_type *elem_type, *index_type;
28944ef5f574SMartin KaFai Lau 	u32 elem_type_id, index_type_id;
2895eb3f595dSMartin KaFai Lau 	struct btf *btf = env->btf;
2896eb3f595dSMartin KaFai Lau 	u32 elem_size;
2897eb3f595dSMartin KaFai Lau 
28984ef5f574SMartin KaFai Lau 	/* Check array->index_type */
28994ef5f574SMartin KaFai Lau 	index_type_id = array->index_type;
29004ef5f574SMartin KaFai Lau 	index_type = btf_type_by_id(btf, index_type_id);
2901e4f07120SStanislav Fomichev 	if (btf_type_nosize_or_null(index_type) ||
2902e4f07120SStanislav Fomichev 	    btf_type_is_resolve_source_only(index_type)) {
29034ef5f574SMartin KaFai Lau 		btf_verifier_log_type(env, v->t, "Invalid index");
29044ef5f574SMartin KaFai Lau 		return -EINVAL;
29054ef5f574SMartin KaFai Lau 	}
29064ef5f574SMartin KaFai Lau 
29074ef5f574SMartin KaFai Lau 	if (!env_type_is_resolve_sink(env, index_type) &&
29084ef5f574SMartin KaFai Lau 	    !env_type_is_resolved(env, index_type_id))
29094ef5f574SMartin KaFai Lau 		return env_stack_push(env, index_type, index_type_id);
29104ef5f574SMartin KaFai Lau 
29114ef5f574SMartin KaFai Lau 	index_type = btf_type_id_size(btf, &index_type_id, NULL);
29124ef5f574SMartin KaFai Lau 	if (!index_type || !btf_type_is_int(index_type) ||
29134ef5f574SMartin KaFai Lau 	    !btf_type_int_is_regular(index_type)) {
29144ef5f574SMartin KaFai Lau 		btf_verifier_log_type(env, v->t, "Invalid index");
29154ef5f574SMartin KaFai Lau 		return -EINVAL;
29164ef5f574SMartin KaFai Lau 	}
29174ef5f574SMartin KaFai Lau 
29184ef5f574SMartin KaFai Lau 	/* Check array->type */
29194ef5f574SMartin KaFai Lau 	elem_type_id = array->type;
2920eb3f595dSMartin KaFai Lau 	elem_type = btf_type_by_id(btf, elem_type_id);
2921e4f07120SStanislav Fomichev 	if (btf_type_nosize_or_null(elem_type) ||
2922e4f07120SStanislav Fomichev 	    btf_type_is_resolve_source_only(elem_type)) {
2923eb3f595dSMartin KaFai Lau 		btf_verifier_log_type(env, v->t,
2924eb3f595dSMartin KaFai Lau 				      "Invalid elem");
2925eb3f595dSMartin KaFai Lau 		return -EINVAL;
2926eb3f595dSMartin KaFai Lau 	}
2927eb3f595dSMartin KaFai Lau 
2928eb3f595dSMartin KaFai Lau 	if (!env_type_is_resolve_sink(env, elem_type) &&
2929eb3f595dSMartin KaFai Lau 	    !env_type_is_resolved(env, elem_type_id))
2930eb3f595dSMartin KaFai Lau 		return env_stack_push(env, elem_type, elem_type_id);
2931eb3f595dSMartin KaFai Lau 
2932eb3f595dSMartin KaFai Lau 	elem_type = btf_type_id_size(btf, &elem_type_id, &elem_size);
2933eb3f595dSMartin KaFai Lau 	if (!elem_type) {
2934eb3f595dSMartin KaFai Lau 		btf_verifier_log_type(env, v->t, "Invalid elem");
2935eb3f595dSMartin KaFai Lau 		return -EINVAL;
2936eb3f595dSMartin KaFai Lau 	}
2937eb3f595dSMartin KaFai Lau 
29384ef5f574SMartin KaFai Lau 	if (btf_type_is_int(elem_type) && !btf_type_int_is_regular(elem_type)) {
29394ef5f574SMartin KaFai Lau 		btf_verifier_log_type(env, v->t, "Invalid array of int");
2940eb3f595dSMartin KaFai Lau 		return -EINVAL;
2941eb3f595dSMartin KaFai Lau 	}
2942eb3f595dSMartin KaFai Lau 
2943eb3f595dSMartin KaFai Lau 	if (array->nelems && elem_size > U32_MAX / array->nelems) {
2944eb3f595dSMartin KaFai Lau 		btf_verifier_log_type(env, v->t,
2945eb3f595dSMartin KaFai Lau 				      "Array size overflows U32_MAX");
2946eb3f595dSMartin KaFai Lau 		return -EINVAL;
2947eb3f595dSMartin KaFai Lau 	}
2948eb3f595dSMartin KaFai Lau 
2949eb3f595dSMartin KaFai Lau 	env_stack_pop_resolved(env, elem_type_id, elem_size * array->nelems);
2950eb3f595dSMartin KaFai Lau 
2951eb3f595dSMartin KaFai Lau 	return 0;
2952eb3f595dSMartin KaFai Lau }
2953eb3f595dSMartin KaFai Lau 
btf_array_log(struct btf_verifier_env * env,const struct btf_type * t)295469b693f0SMartin KaFai Lau static void btf_array_log(struct btf_verifier_env *env,
295569b693f0SMartin KaFai Lau 			  const struct btf_type *t)
295669b693f0SMartin KaFai Lau {
295769b693f0SMartin KaFai Lau 	const struct btf_array *array = btf_type_array(t);
295869b693f0SMartin KaFai Lau 
295969b693f0SMartin KaFai Lau 	btf_verifier_log(env, "type_id=%u index_type_id=%u nr_elems=%u",
296069b693f0SMartin KaFai Lau 			 array->type, array->index_type, array->nelems);
296169b693f0SMartin KaFai Lau }
296269b693f0SMartin KaFai Lau 
__btf_array_show(const struct btf * btf,const struct btf_type * t,u32 type_id,void * data,u8 bits_offset,struct btf_show * show)296331d0bc81SAlan Maguire static void __btf_array_show(const struct btf *btf, const struct btf_type *t,
2964b00b8daeSMartin KaFai Lau 			     u32 type_id, void *data, u8 bits_offset,
296531d0bc81SAlan Maguire 			     struct btf_show *show)
2966b00b8daeSMartin KaFai Lau {
2967b00b8daeSMartin KaFai Lau 	const struct btf_array *array = btf_type_array(t);
2968b00b8daeSMartin KaFai Lau 	const struct btf_kind_operations *elem_ops;
2969b00b8daeSMartin KaFai Lau 	const struct btf_type *elem_type;
297031d0bc81SAlan Maguire 	u32 i, elem_size = 0, elem_type_id;
297131d0bc81SAlan Maguire 	u16 encoding = 0;
2972b00b8daeSMartin KaFai Lau 
2973b00b8daeSMartin KaFai Lau 	elem_type_id = array->type;
297431d0bc81SAlan Maguire 	elem_type = btf_type_skip_modifiers(btf, elem_type_id, NULL);
297531d0bc81SAlan Maguire 	if (elem_type && btf_type_has_size(elem_type))
297631d0bc81SAlan Maguire 		elem_size = elem_type->size;
2977b00b8daeSMartin KaFai Lau 
297831d0bc81SAlan Maguire 	if (elem_type && btf_type_is_int(elem_type)) {
297931d0bc81SAlan Maguire 		u32 int_type = btf_type_int(elem_type);
298031d0bc81SAlan Maguire 
298131d0bc81SAlan Maguire 		encoding = BTF_INT_ENCODING(int_type);
298231d0bc81SAlan Maguire 
298331d0bc81SAlan Maguire 		/*
298431d0bc81SAlan Maguire 		 * BTF_INT_CHAR encoding never seems to be set for
298531d0bc81SAlan Maguire 		 * char arrays, so if size is 1 and element is
298631d0bc81SAlan Maguire 		 * printable as a char, we'll do that.
298731d0bc81SAlan Maguire 		 */
298831d0bc81SAlan Maguire 		if (elem_size == 1)
298931d0bc81SAlan Maguire 			encoding = BTF_INT_CHAR;
2990b00b8daeSMartin KaFai Lau 	}
299131d0bc81SAlan Maguire 
299231d0bc81SAlan Maguire 	if (!btf_show_start_array_type(show, t, type_id, encoding, data))
299331d0bc81SAlan Maguire 		return;
299431d0bc81SAlan Maguire 
299531d0bc81SAlan Maguire 	if (!elem_type)
299631d0bc81SAlan Maguire 		goto out;
299731d0bc81SAlan Maguire 	elem_ops = btf_type_ops(elem_type);
299831d0bc81SAlan Maguire 
299931d0bc81SAlan Maguire 	for (i = 0; i < array->nelems; i++) {
300031d0bc81SAlan Maguire 
300131d0bc81SAlan Maguire 		btf_show_start_array_member(show);
300231d0bc81SAlan Maguire 
300331d0bc81SAlan Maguire 		elem_ops->show(btf, elem_type, elem_type_id, data,
300431d0bc81SAlan Maguire 			       bits_offset, show);
300531d0bc81SAlan Maguire 		data += elem_size;
300631d0bc81SAlan Maguire 
300731d0bc81SAlan Maguire 		btf_show_end_array_member(show);
300831d0bc81SAlan Maguire 
300931d0bc81SAlan Maguire 		if (show->state.array_terminated)
301031d0bc81SAlan Maguire 			break;
301131d0bc81SAlan Maguire 	}
301231d0bc81SAlan Maguire out:
301331d0bc81SAlan Maguire 	btf_show_end_array_type(show);
301431d0bc81SAlan Maguire }
301531d0bc81SAlan Maguire 
btf_array_show(const struct btf * btf,const struct btf_type * t,u32 type_id,void * data,u8 bits_offset,struct btf_show * show)301631d0bc81SAlan Maguire static void btf_array_show(const struct btf *btf, const struct btf_type *t,
301731d0bc81SAlan Maguire 			   u32 type_id, void *data, u8 bits_offset,
301831d0bc81SAlan Maguire 			   struct btf_show *show)
301931d0bc81SAlan Maguire {
302031d0bc81SAlan Maguire 	const struct btf_member *m = show->state.member;
302131d0bc81SAlan Maguire 
302231d0bc81SAlan Maguire 	/*
302331d0bc81SAlan Maguire 	 * First check if any members would be shown (are non-zero).
302431d0bc81SAlan Maguire 	 * See comments above "struct btf_show" definition for more
302531d0bc81SAlan Maguire 	 * details on how this works at a high-level.
302631d0bc81SAlan Maguire 	 */
302731d0bc81SAlan Maguire 	if (show->state.depth > 0 && !(show->flags & BTF_SHOW_ZERO)) {
302831d0bc81SAlan Maguire 		if (!show->state.depth_check) {
302931d0bc81SAlan Maguire 			show->state.depth_check = show->state.depth + 1;
303031d0bc81SAlan Maguire 			show->state.depth_to_show = 0;
303131d0bc81SAlan Maguire 		}
303231d0bc81SAlan Maguire 		__btf_array_show(btf, t, type_id, data, bits_offset, show);
303331d0bc81SAlan Maguire 		show->state.member = m;
303431d0bc81SAlan Maguire 
303531d0bc81SAlan Maguire 		if (show->state.depth_check != show->state.depth + 1)
303631d0bc81SAlan Maguire 			return;
303731d0bc81SAlan Maguire 		show->state.depth_check = 0;
303831d0bc81SAlan Maguire 
303931d0bc81SAlan Maguire 		if (show->state.depth_to_show <= show->state.depth)
304031d0bc81SAlan Maguire 			return;
304131d0bc81SAlan Maguire 		/*
304231d0bc81SAlan Maguire 		 * Reaching here indicates we have recursed and found
304331d0bc81SAlan Maguire 		 * non-zero array member(s).
304431d0bc81SAlan Maguire 		 */
304531d0bc81SAlan Maguire 	}
304631d0bc81SAlan Maguire 	__btf_array_show(btf, t, type_id, data, bits_offset, show);
3047b00b8daeSMartin KaFai Lau }
3048b00b8daeSMartin KaFai Lau 
304969b693f0SMartin KaFai Lau static struct btf_kind_operations array_ops = {
305069b693f0SMartin KaFai Lau 	.check_meta = btf_array_check_meta,
3051eb3f595dSMartin KaFai Lau 	.resolve = btf_array_resolve,
3052179cde8cSMartin KaFai Lau 	.check_member = btf_array_check_member,
30539d5f9f70SYonghong Song 	.check_kflag_member = btf_generic_check_kflag_member,
305469b693f0SMartin KaFai Lau 	.log_details = btf_array_log,
305531d0bc81SAlan Maguire 	.show = btf_array_show,
305669b693f0SMartin KaFai Lau };
305769b693f0SMartin KaFai Lau 
btf_struct_check_member(struct btf_verifier_env * env,const struct btf_type * struct_type,const struct btf_member * member,const struct btf_type * member_type)3058179cde8cSMartin KaFai Lau static int btf_struct_check_member(struct btf_verifier_env *env,
3059179cde8cSMartin KaFai Lau 				   const struct btf_type *struct_type,
3060179cde8cSMartin KaFai Lau 				   const struct btf_member *member,
3061179cde8cSMartin KaFai Lau 				   const struct btf_type *member_type)
3062179cde8cSMartin KaFai Lau {
3063179cde8cSMartin KaFai Lau 	u32 struct_bits_off = member->offset;
3064179cde8cSMartin KaFai Lau 	u32 struct_size, bytes_offset;
3065179cde8cSMartin KaFai Lau 
3066179cde8cSMartin KaFai Lau 	if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
3067179cde8cSMartin KaFai Lau 		btf_verifier_log_member(env, struct_type, member,
3068179cde8cSMartin KaFai Lau 					"Member is not byte aligned");
3069179cde8cSMartin KaFai Lau 		return -EINVAL;
3070179cde8cSMartin KaFai Lau 	}
3071179cde8cSMartin KaFai Lau 
3072179cde8cSMartin KaFai Lau 	struct_size = struct_type->size;
3073179cde8cSMartin KaFai Lau 	bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
3074179cde8cSMartin KaFai Lau 	if (struct_size - bytes_offset < member_type->size) {
3075179cde8cSMartin KaFai Lau 		btf_verifier_log_member(env, struct_type, member,
3076179cde8cSMartin KaFai Lau 					"Member exceeds struct_size");
3077179cde8cSMartin KaFai Lau 		return -EINVAL;
3078179cde8cSMartin KaFai Lau 	}
3079179cde8cSMartin KaFai Lau 
3080179cde8cSMartin KaFai Lau 	return 0;
3081179cde8cSMartin KaFai Lau }
3082179cde8cSMartin KaFai Lau 
btf_struct_check_meta(struct btf_verifier_env * env,const struct btf_type * t,u32 meta_left)308369b693f0SMartin KaFai Lau static s32 btf_struct_check_meta(struct btf_verifier_env *env,
308469b693f0SMartin KaFai Lau 				 const struct btf_type *t,
308569b693f0SMartin KaFai Lau 				 u32 meta_left)
308669b693f0SMartin KaFai Lau {
308769b693f0SMartin KaFai Lau 	bool is_union = BTF_INFO_KIND(t->info) == BTF_KIND_UNION;
308869b693f0SMartin KaFai Lau 	const struct btf_member *member;
30896283fa38SMartin KaFai Lau 	u32 meta_needed, last_offset;
309069b693f0SMartin KaFai Lau 	struct btf *btf = env->btf;
309169b693f0SMartin KaFai Lau 	u32 struct_size = t->size;
30929d5f9f70SYonghong Song 	u32 offset;
309369b693f0SMartin KaFai Lau 	u16 i;
309469b693f0SMartin KaFai Lau 
309569b693f0SMartin KaFai Lau 	meta_needed = btf_type_vlen(t) * sizeof(*member);
309669b693f0SMartin KaFai Lau 	if (meta_left < meta_needed) {
309769b693f0SMartin KaFai Lau 		btf_verifier_log_basic(env, t,
309869b693f0SMartin KaFai Lau 				       "meta_left:%u meta_needed:%u",
309969b693f0SMartin KaFai Lau 				       meta_left, meta_needed);
310069b693f0SMartin KaFai Lau 		return -EINVAL;
310169b693f0SMartin KaFai Lau 	}
310269b693f0SMartin KaFai Lau 
3103eb04bbb6SYonghong Song 	/* struct type either no name or a valid one */
3104eb04bbb6SYonghong Song 	if (t->name_off &&
3105eb04bbb6SYonghong Song 	    !btf_name_valid_identifier(env->btf, t->name_off)) {
3106eb04bbb6SYonghong Song 		btf_verifier_log_type(env, t, "Invalid name");
3107eb04bbb6SYonghong Song 		return -EINVAL;
3108eb04bbb6SYonghong Song 	}
3109eb04bbb6SYonghong Song 
311069b693f0SMartin KaFai Lau 	btf_verifier_log_type(env, t, NULL);
311169b693f0SMartin KaFai Lau 
31126283fa38SMartin KaFai Lau 	last_offset = 0;
311369b693f0SMartin KaFai Lau 	for_each_member(i, t, member) {
3114fbcf93ebSMartin KaFai Lau 		if (!btf_name_offset_valid(btf, member->name_off)) {
311569b693f0SMartin KaFai Lau 			btf_verifier_log_member(env, t, member,
311669b693f0SMartin KaFai Lau 						"Invalid member name_offset:%u",
3117fbcf93ebSMartin KaFai Lau 						member->name_off);
311869b693f0SMartin KaFai Lau 			return -EINVAL;
311969b693f0SMartin KaFai Lau 		}
312069b693f0SMartin KaFai Lau 
3121eb04bbb6SYonghong Song 		/* struct member either no name or a valid one */
3122eb04bbb6SYonghong Song 		if (member->name_off &&
3123eb04bbb6SYonghong Song 		    !btf_name_valid_identifier(btf, member->name_off)) {
3124eb04bbb6SYonghong Song 			btf_verifier_log_member(env, t, member, "Invalid name");
3125eb04bbb6SYonghong Song 			return -EINVAL;
3126eb04bbb6SYonghong Song 		}
312769b693f0SMartin KaFai Lau 		/* A member cannot be in type void */
3128aea2f7b8SMartin KaFai Lau 		if (!member->type || !BTF_TYPE_ID_VALID(member->type)) {
312969b693f0SMartin KaFai Lau 			btf_verifier_log_member(env, t, member,
313069b693f0SMartin KaFai Lau 						"Invalid type_id");
313169b693f0SMartin KaFai Lau 			return -EINVAL;
313269b693f0SMartin KaFai Lau 		}
313369b693f0SMartin KaFai Lau 
31348293eb99SAlexei Starovoitov 		offset = __btf_member_bit_offset(t, member);
31359d5f9f70SYonghong Song 		if (is_union && offset) {
313669b693f0SMartin KaFai Lau 			btf_verifier_log_member(env, t, member,
313769b693f0SMartin KaFai Lau 						"Invalid member bits_offset");
313869b693f0SMartin KaFai Lau 			return -EINVAL;
313969b693f0SMartin KaFai Lau 		}
314069b693f0SMartin KaFai Lau 
31416283fa38SMartin KaFai Lau 		/*
31426283fa38SMartin KaFai Lau 		 * ">" instead of ">=" because the last member could be
31436283fa38SMartin KaFai Lau 		 * "char a[0];"
31446283fa38SMartin KaFai Lau 		 */
31459d5f9f70SYonghong Song 		if (last_offset > offset) {
31466283fa38SMartin KaFai Lau 			btf_verifier_log_member(env, t, member,
31476283fa38SMartin KaFai Lau 						"Invalid member bits_offset");
31486283fa38SMartin KaFai Lau 			return -EINVAL;
31496283fa38SMartin KaFai Lau 		}
31506283fa38SMartin KaFai Lau 
31519d5f9f70SYonghong Song 		if (BITS_ROUNDUP_BYTES(offset) > struct_size) {
315269b693f0SMartin KaFai Lau 			btf_verifier_log_member(env, t, member,
3153311fe1a8SColin Ian King 						"Member bits_offset exceeds its struct size");
315469b693f0SMartin KaFai Lau 			return -EINVAL;
315569b693f0SMartin KaFai Lau 		}
315669b693f0SMartin KaFai Lau 
315769b693f0SMartin KaFai Lau 		btf_verifier_log_member(env, t, member, NULL);
31589d5f9f70SYonghong Song 		last_offset = offset;
315969b693f0SMartin KaFai Lau 	}
316069b693f0SMartin KaFai Lau 
316169b693f0SMartin KaFai Lau 	return meta_needed;
316269b693f0SMartin KaFai Lau }
316369b693f0SMartin KaFai Lau 
btf_struct_resolve(struct btf_verifier_env * env,const struct resolve_vertex * v)3164eb3f595dSMartin KaFai Lau static int btf_struct_resolve(struct btf_verifier_env *env,
3165eb3f595dSMartin KaFai Lau 			      const struct resolve_vertex *v)
3166eb3f595dSMartin KaFai Lau {
3167eb3f595dSMartin KaFai Lau 	const struct btf_member *member;
3168179cde8cSMartin KaFai Lau 	int err;
3169eb3f595dSMartin KaFai Lau 	u16 i;
3170eb3f595dSMartin KaFai Lau 
3171eb3f595dSMartin KaFai Lau 	/* Before continue resolving the next_member,
3172eb3f595dSMartin KaFai Lau 	 * ensure the last member is indeed resolved to a
3173eb3f595dSMartin KaFai Lau 	 * type with size info.
3174eb3f595dSMartin KaFai Lau 	 */
3175eb3f595dSMartin KaFai Lau 	if (v->next_member) {
3176179cde8cSMartin KaFai Lau 		const struct btf_type *last_member_type;
3177eb3f595dSMartin KaFai Lau 		const struct btf_member *last_member;
3178a37a3258SLorenz Bauer 		u32 last_member_type_id;
3179eb3f595dSMartin KaFai Lau 
3180eb3f595dSMartin KaFai Lau 		last_member = btf_type_member(v->t) + v->next_member - 1;
3181eb3f595dSMartin KaFai Lau 		last_member_type_id = last_member->type;
3182eb3f595dSMartin KaFai Lau 		if (WARN_ON_ONCE(!env_type_is_resolved(env,
3183eb3f595dSMartin KaFai Lau 						       last_member_type_id)))
3184eb3f595dSMartin KaFai Lau 			return -EINVAL;
3185179cde8cSMartin KaFai Lau 
3186179cde8cSMartin KaFai Lau 		last_member_type = btf_type_by_id(env->btf,
3187179cde8cSMartin KaFai Lau 						  last_member_type_id);
31889d5f9f70SYonghong Song 		if (btf_type_kflag(v->t))
31899d5f9f70SYonghong Song 			err = btf_type_ops(last_member_type)->check_kflag_member(env, v->t,
31909d5f9f70SYonghong Song 								last_member,
31919d5f9f70SYonghong Song 								last_member_type);
31929d5f9f70SYonghong Song 		else
3193179cde8cSMartin KaFai Lau 			err = btf_type_ops(last_member_type)->check_member(env, v->t,
3194179cde8cSMartin KaFai Lau 								last_member,
3195179cde8cSMartin KaFai Lau 								last_member_type);
3196179cde8cSMartin KaFai Lau 		if (err)
3197179cde8cSMartin KaFai Lau 			return err;
3198eb3f595dSMartin KaFai Lau 	}
3199eb3f595dSMartin KaFai Lau 
3200eb3f595dSMartin KaFai Lau 	for_each_member_from(i, v->next_member, v->t, member) {
3201eb3f595dSMartin KaFai Lau 		u32 member_type_id = member->type;
3202eb3f595dSMartin KaFai Lau 		const struct btf_type *member_type = btf_type_by_id(env->btf,
3203eb3f595dSMartin KaFai Lau 								member_type_id);
3204eb3f595dSMartin KaFai Lau 
3205e4f07120SStanislav Fomichev 		if (btf_type_nosize_or_null(member_type) ||
3206e4f07120SStanislav Fomichev 		    btf_type_is_resolve_source_only(member_type)) {
3207eb3f595dSMartin KaFai Lau 			btf_verifier_log_member(env, v->t, member,
3208eb3f595dSMartin KaFai Lau 						"Invalid member");
3209eb3f595dSMartin KaFai Lau 			return -EINVAL;
3210eb3f595dSMartin KaFai Lau 		}
3211eb3f595dSMartin KaFai Lau 
3212eb3f595dSMartin KaFai Lau 		if (!env_type_is_resolve_sink(env, member_type) &&
3213eb3f595dSMartin KaFai Lau 		    !env_type_is_resolved(env, member_type_id)) {
3214eb3f595dSMartin KaFai Lau 			env_stack_set_next_member(env, i + 1);
3215eb3f595dSMartin KaFai Lau 			return env_stack_push(env, member_type, member_type_id);
3216eb3f595dSMartin KaFai Lau 		}
3217179cde8cSMartin KaFai Lau 
32189d5f9f70SYonghong Song 		if (btf_type_kflag(v->t))
32199d5f9f70SYonghong Song 			err = btf_type_ops(member_type)->check_kflag_member(env, v->t,
32209d5f9f70SYonghong Song 									    member,
32219d5f9f70SYonghong Song 									    member_type);
32229d5f9f70SYonghong Song 		else
3223179cde8cSMartin KaFai Lau 			err = btf_type_ops(member_type)->check_member(env, v->t,
3224179cde8cSMartin KaFai Lau 								      member,
3225179cde8cSMartin KaFai Lau 								      member_type);
3226179cde8cSMartin KaFai Lau 		if (err)
3227179cde8cSMartin KaFai Lau 			return err;
3228eb3f595dSMartin KaFai Lau 	}
3229eb3f595dSMartin KaFai Lau 
3230eb3f595dSMartin KaFai Lau 	env_stack_pop_resolved(env, 0, 0);
3231eb3f595dSMartin KaFai Lau 
3232eb3f595dSMartin KaFai Lau 	return 0;
3233eb3f595dSMartin KaFai Lau }
3234eb3f595dSMartin KaFai Lau 
btf_struct_log(struct btf_verifier_env * env,const struct btf_type * t)323569b693f0SMartin KaFai Lau static void btf_struct_log(struct btf_verifier_env *env,
323669b693f0SMartin KaFai Lau 			   const struct btf_type *t)
323769b693f0SMartin KaFai Lau {
323869b693f0SMartin KaFai Lau 	btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t));
323969b693f0SMartin KaFai Lau }
324069b693f0SMartin KaFai Lau 
324161df10c7SKumar Kartikeya Dwivedi enum {
324261df10c7SKumar Kartikeya Dwivedi 	BTF_FIELD_IGNORE = 0,
324361df10c7SKumar Kartikeya Dwivedi 	BTF_FIELD_FOUND  = 1,
324442ba1308SKumar Kartikeya Dwivedi };
324542ba1308SKumar Kartikeya Dwivedi 
324642ba1308SKumar Kartikeya Dwivedi struct btf_field_info {
3247aa3496acSKumar Kartikeya Dwivedi 	enum btf_field_type type;
324842ba1308SKumar Kartikeya Dwivedi 	u32 off;
3249f0c5941fSKumar Kartikeya Dwivedi 	union {
3250db559117SKumar Kartikeya Dwivedi 		struct {
3251aa3496acSKumar Kartikeya Dwivedi 			u32 type_id;
3252db559117SKumar Kartikeya Dwivedi 		} kptr;
3253f0c5941fSKumar Kartikeya Dwivedi 		struct {
3254f0c5941fSKumar Kartikeya Dwivedi 			const char *node_name;
3255f0c5941fSKumar Kartikeya Dwivedi 			u32 value_btf_id;
325630465003SDave Marchevsky 		} graph_root;
3257f0c5941fSKumar Kartikeya Dwivedi 	};
325842ba1308SKumar Kartikeya Dwivedi };
325942ba1308SKumar Kartikeya Dwivedi 
btf_find_struct(const struct btf * btf,const struct btf_type * t,u32 off,int sz,enum btf_field_type field_type,struct btf_field_info * info)326042ba1308SKumar Kartikeya Dwivedi static int btf_find_struct(const struct btf *btf, const struct btf_type *t,
3261db559117SKumar Kartikeya Dwivedi 			   u32 off, int sz, enum btf_field_type field_type,
3262db559117SKumar Kartikeya Dwivedi 			   struct btf_field_info *info)
326342ba1308SKumar Kartikeya Dwivedi {
326442ba1308SKumar Kartikeya Dwivedi 	if (!__btf_type_is_struct(t))
326561df10c7SKumar Kartikeya Dwivedi 		return BTF_FIELD_IGNORE;
326642ba1308SKumar Kartikeya Dwivedi 	if (t->size != sz)
326761df10c7SKumar Kartikeya Dwivedi 		return BTF_FIELD_IGNORE;
3268db559117SKumar Kartikeya Dwivedi 	info->type = field_type;
326942ba1308SKumar Kartikeya Dwivedi 	info->off = off;
327061df10c7SKumar Kartikeya Dwivedi 	return BTF_FIELD_FOUND;
327161df10c7SKumar Kartikeya Dwivedi }
327261df10c7SKumar Kartikeya Dwivedi 
btf_find_kptr(const struct btf * btf,const struct btf_type * t,u32 off,int sz,struct btf_field_info * info)327361df10c7SKumar Kartikeya Dwivedi static int btf_find_kptr(const struct btf *btf, const struct btf_type *t,
327461df10c7SKumar Kartikeya Dwivedi 			 u32 off, int sz, struct btf_field_info *info)
327561df10c7SKumar Kartikeya Dwivedi {
3276aa3496acSKumar Kartikeya Dwivedi 	enum btf_field_type type;
327761df10c7SKumar Kartikeya Dwivedi 	u32 res_id;
327861df10c7SKumar Kartikeya Dwivedi 
327923da464dSKumar Kartikeya Dwivedi 	/* Permit modifiers on the pointer itself */
328023da464dSKumar Kartikeya Dwivedi 	if (btf_type_is_volatile(t))
328123da464dSKumar Kartikeya Dwivedi 		t = btf_type_by_id(btf, t->type);
328261df10c7SKumar Kartikeya Dwivedi 	/* For PTR, sz is always == 8 */
328361df10c7SKumar Kartikeya Dwivedi 	if (!btf_type_is_ptr(t))
328461df10c7SKumar Kartikeya Dwivedi 		return BTF_FIELD_IGNORE;
328561df10c7SKumar Kartikeya Dwivedi 	t = btf_type_by_id(btf, t->type);
328661df10c7SKumar Kartikeya Dwivedi 
328761df10c7SKumar Kartikeya Dwivedi 	if (!btf_type_is_type_tag(t))
328861df10c7SKumar Kartikeya Dwivedi 		return BTF_FIELD_IGNORE;
328961df10c7SKumar Kartikeya Dwivedi 	/* Reject extra tags */
329061df10c7SKumar Kartikeya Dwivedi 	if (btf_type_is_type_tag(btf_type_by_id(btf, t->type)))
329161df10c7SKumar Kartikeya Dwivedi 		return -EINVAL;
329203b77e17SAlexei Starovoitov 	if (!strcmp("kptr_untrusted", __btf_name_by_offset(btf, t->name_off)))
3293c0a5a21cSKumar Kartikeya Dwivedi 		type = BPF_KPTR_UNREF;
329403b77e17SAlexei Starovoitov 	else if (!strcmp("kptr", __btf_name_by_offset(btf, t->name_off)))
3295c0a5a21cSKumar Kartikeya Dwivedi 		type = BPF_KPTR_REF;
3296c0a5a21cSKumar Kartikeya Dwivedi 	else
329761df10c7SKumar Kartikeya Dwivedi 		return -EINVAL;
329861df10c7SKumar Kartikeya Dwivedi 
329961df10c7SKumar Kartikeya Dwivedi 	/* Get the base type */
330061df10c7SKumar Kartikeya Dwivedi 	t = btf_type_skip_modifiers(btf, t->type, &res_id);
330161df10c7SKumar Kartikeya Dwivedi 	/* Only pointer to struct is allowed */
330261df10c7SKumar Kartikeya Dwivedi 	if (!__btf_type_is_struct(t))
330361df10c7SKumar Kartikeya Dwivedi 		return -EINVAL;
330461df10c7SKumar Kartikeya Dwivedi 
3305c0a5a21cSKumar Kartikeya Dwivedi 	info->type = type;
3306db559117SKumar Kartikeya Dwivedi 	info->off = off;
3307db559117SKumar Kartikeya Dwivedi 	info->kptr.type_id = res_id;
330861df10c7SKumar Kartikeya Dwivedi 	return BTF_FIELD_FOUND;
330942ba1308SKumar Kartikeya Dwivedi }
331042ba1308SKumar Kartikeya Dwivedi 
btf_find_decl_tag_value(const struct btf * btf,const struct btf_type * pt,int comp_idx,const char * tag_key)3311f0c5941fSKumar Kartikeya Dwivedi static const char *btf_find_decl_tag_value(const struct btf *btf,
3312f0c5941fSKumar Kartikeya Dwivedi 					   const struct btf_type *pt,
3313f0c5941fSKumar Kartikeya Dwivedi 					   int comp_idx, const char *tag_key)
3314f0c5941fSKumar Kartikeya Dwivedi {
3315f0c5941fSKumar Kartikeya Dwivedi 	int i;
3316f0c5941fSKumar Kartikeya Dwivedi 
3317f0c5941fSKumar Kartikeya Dwivedi 	for (i = 1; i < btf_nr_types(btf); i++) {
3318f0c5941fSKumar Kartikeya Dwivedi 		const struct btf_type *t = btf_type_by_id(btf, i);
3319f0c5941fSKumar Kartikeya Dwivedi 		int len = strlen(tag_key);
3320f0c5941fSKumar Kartikeya Dwivedi 
3321f0c5941fSKumar Kartikeya Dwivedi 		if (!btf_type_is_decl_tag(t))
3322f0c5941fSKumar Kartikeya Dwivedi 			continue;
3323f0c5941fSKumar Kartikeya Dwivedi 		if (pt != btf_type_by_id(btf, t->type) ||
3324f0c5941fSKumar Kartikeya Dwivedi 		    btf_type_decl_tag(t)->component_idx != comp_idx)
3325f0c5941fSKumar Kartikeya Dwivedi 			continue;
3326f0c5941fSKumar Kartikeya Dwivedi 		if (strncmp(__btf_name_by_offset(btf, t->name_off), tag_key, len))
3327f0c5941fSKumar Kartikeya Dwivedi 			continue;
3328f0c5941fSKumar Kartikeya Dwivedi 		return __btf_name_by_offset(btf, t->name_off) + len;
3329f0c5941fSKumar Kartikeya Dwivedi 	}
3330f0c5941fSKumar Kartikeya Dwivedi 	return NULL;
3331f0c5941fSKumar Kartikeya Dwivedi }
3332f0c5941fSKumar Kartikeya Dwivedi 
33339c395c1bSDave Marchevsky static int
btf_find_graph_root(const struct btf * btf,const struct btf_type * pt,const struct btf_type * t,int comp_idx,u32 off,int sz,struct btf_field_info * info,enum btf_field_type head_type)33349c395c1bSDave Marchevsky btf_find_graph_root(const struct btf *btf, const struct btf_type *pt,
33359c395c1bSDave Marchevsky 		    const struct btf_type *t, int comp_idx, u32 off,
33369c395c1bSDave Marchevsky 		    int sz, struct btf_field_info *info,
33379c395c1bSDave Marchevsky 		    enum btf_field_type head_type)
3338f0c5941fSKumar Kartikeya Dwivedi {
33399c395c1bSDave Marchevsky 	const char *node_field_name;
3340f0c5941fSKumar Kartikeya Dwivedi 	const char *value_type;
3341f0c5941fSKumar Kartikeya Dwivedi 	s32 id;
3342f0c5941fSKumar Kartikeya Dwivedi 
3343f0c5941fSKumar Kartikeya Dwivedi 	if (!__btf_type_is_struct(t))
3344f0c5941fSKumar Kartikeya Dwivedi 		return BTF_FIELD_IGNORE;
3345f0c5941fSKumar Kartikeya Dwivedi 	if (t->size != sz)
3346f0c5941fSKumar Kartikeya Dwivedi 		return BTF_FIELD_IGNORE;
3347f0c5941fSKumar Kartikeya Dwivedi 	value_type = btf_find_decl_tag_value(btf, pt, comp_idx, "contains:");
3348f0c5941fSKumar Kartikeya Dwivedi 	if (!value_type)
3349f0c5941fSKumar Kartikeya Dwivedi 		return -EINVAL;
33509c395c1bSDave Marchevsky 	node_field_name = strstr(value_type, ":");
33519c395c1bSDave Marchevsky 	if (!node_field_name)
3352f0c5941fSKumar Kartikeya Dwivedi 		return -EINVAL;
33539c395c1bSDave Marchevsky 	value_type = kstrndup(value_type, node_field_name - value_type, GFP_KERNEL | __GFP_NOWARN);
3354f0c5941fSKumar Kartikeya Dwivedi 	if (!value_type)
3355f0c5941fSKumar Kartikeya Dwivedi 		return -ENOMEM;
3356f0c5941fSKumar Kartikeya Dwivedi 	id = btf_find_by_name_kind(btf, value_type, BTF_KIND_STRUCT);
3357f0c5941fSKumar Kartikeya Dwivedi 	kfree(value_type);
3358f0c5941fSKumar Kartikeya Dwivedi 	if (id < 0)
3359f0c5941fSKumar Kartikeya Dwivedi 		return id;
33609c395c1bSDave Marchevsky 	node_field_name++;
33619c395c1bSDave Marchevsky 	if (str_is_empty(node_field_name))
3362f0c5941fSKumar Kartikeya Dwivedi 		return -EINVAL;
33639c395c1bSDave Marchevsky 	info->type = head_type;
3364f0c5941fSKumar Kartikeya Dwivedi 	info->off = off;
336530465003SDave Marchevsky 	info->graph_root.value_btf_id = id;
33669c395c1bSDave Marchevsky 	info->graph_root.node_name = node_field_name;
3367f0c5941fSKumar Kartikeya Dwivedi 	return BTF_FIELD_FOUND;
3368f0c5941fSKumar Kartikeya Dwivedi }
3369f0c5941fSKumar Kartikeya Dwivedi 
33709c395c1bSDave Marchevsky #define field_mask_test_name(field_type, field_type_str) \
33719c395c1bSDave Marchevsky 	if (field_mask & field_type && !strcmp(name, field_type_str)) { \
33729c395c1bSDave Marchevsky 		type = field_type;					\
33739c395c1bSDave Marchevsky 		goto end;						\
33749c395c1bSDave Marchevsky 	}
33759c395c1bSDave Marchevsky 
btf_get_field_type(const char * name,u32 field_mask,u32 * seen_mask,int * align,int * sz)3376db559117SKumar Kartikeya Dwivedi static int btf_get_field_type(const char *name, u32 field_mask, u32 *seen_mask,
3377db559117SKumar Kartikeya Dwivedi 			      int *align, int *sz)
3378db559117SKumar Kartikeya Dwivedi {
3379db559117SKumar Kartikeya Dwivedi 	int type = 0;
3380db559117SKumar Kartikeya Dwivedi 
3381db559117SKumar Kartikeya Dwivedi 	if (field_mask & BPF_SPIN_LOCK) {
3382db559117SKumar Kartikeya Dwivedi 		if (!strcmp(name, "bpf_spin_lock")) {
3383db559117SKumar Kartikeya Dwivedi 			if (*seen_mask & BPF_SPIN_LOCK)
3384db559117SKumar Kartikeya Dwivedi 				return -E2BIG;
3385db559117SKumar Kartikeya Dwivedi 			*seen_mask |= BPF_SPIN_LOCK;
3386db559117SKumar Kartikeya Dwivedi 			type = BPF_SPIN_LOCK;
3387db559117SKumar Kartikeya Dwivedi 			goto end;
3388db559117SKumar Kartikeya Dwivedi 		}
3389db559117SKumar Kartikeya Dwivedi 	}
3390db559117SKumar Kartikeya Dwivedi 	if (field_mask & BPF_TIMER) {
3391db559117SKumar Kartikeya Dwivedi 		if (!strcmp(name, "bpf_timer")) {
3392db559117SKumar Kartikeya Dwivedi 			if (*seen_mask & BPF_TIMER)
3393db559117SKumar Kartikeya Dwivedi 				return -E2BIG;
3394db559117SKumar Kartikeya Dwivedi 			*seen_mask |= BPF_TIMER;
3395db559117SKumar Kartikeya Dwivedi 			type = BPF_TIMER;
3396db559117SKumar Kartikeya Dwivedi 			goto end;
3397db559117SKumar Kartikeya Dwivedi 		}
3398db559117SKumar Kartikeya Dwivedi 	}
33999c395c1bSDave Marchevsky 	field_mask_test_name(BPF_LIST_HEAD, "bpf_list_head");
34009c395c1bSDave Marchevsky 	field_mask_test_name(BPF_LIST_NODE, "bpf_list_node");
34019c395c1bSDave Marchevsky 	field_mask_test_name(BPF_RB_ROOT,   "bpf_rb_root");
34029c395c1bSDave Marchevsky 	field_mask_test_name(BPF_RB_NODE,   "bpf_rb_node");
3403d54730b5SDave Marchevsky 	field_mask_test_name(BPF_REFCOUNT,  "bpf_refcount");
34049c395c1bSDave Marchevsky 
3405db559117SKumar Kartikeya Dwivedi 	/* Only return BPF_KPTR when all other types with matchable names fail */
3406db559117SKumar Kartikeya Dwivedi 	if (field_mask & BPF_KPTR) {
3407db559117SKumar Kartikeya Dwivedi 		type = BPF_KPTR_REF;
3408db559117SKumar Kartikeya Dwivedi 		goto end;
3409db559117SKumar Kartikeya Dwivedi 	}
3410db559117SKumar Kartikeya Dwivedi 	return 0;
3411db559117SKumar Kartikeya Dwivedi end:
3412db559117SKumar Kartikeya Dwivedi 	*sz = btf_field_type_size(type);
3413db559117SKumar Kartikeya Dwivedi 	*align = btf_field_type_align(type);
3414db559117SKumar Kartikeya Dwivedi 	return type;
3415db559117SKumar Kartikeya Dwivedi }
3416db559117SKumar Kartikeya Dwivedi 
34179c395c1bSDave Marchevsky #undef field_mask_test_name
34189c395c1bSDave Marchevsky 
btf_find_struct_field(const struct btf * btf,const struct btf_type * t,u32 field_mask,struct btf_field_info * info,int info_cnt)3419db559117SKumar Kartikeya Dwivedi static int btf_find_struct_field(const struct btf *btf,
3420db559117SKumar Kartikeya Dwivedi 				 const struct btf_type *t, u32 field_mask,
342161df10c7SKumar Kartikeya Dwivedi 				 struct btf_field_info *info, int info_cnt)
3422d83525caSAlexei Starovoitov {
3423db559117SKumar Kartikeya Dwivedi 	int ret, idx = 0, align, sz, field_type;
3424d83525caSAlexei Starovoitov 	const struct btf_member *member;
342561df10c7SKumar Kartikeya Dwivedi 	struct btf_field_info tmp;
3426db559117SKumar Kartikeya Dwivedi 	u32 i, off, seen_mask = 0;
3427d83525caSAlexei Starovoitov 
3428d83525caSAlexei Starovoitov 	for_each_member(i, t, member) {
3429d83525caSAlexei Starovoitov 		const struct btf_type *member_type = btf_type_by_id(btf,
3430d83525caSAlexei Starovoitov 								    member->type);
343142ba1308SKumar Kartikeya Dwivedi 
3432db559117SKumar Kartikeya Dwivedi 		field_type = btf_get_field_type(__btf_name_by_offset(btf, member_type->name_off),
3433db559117SKumar Kartikeya Dwivedi 						field_mask, &seen_mask, &align, &sz);
3434db559117SKumar Kartikeya Dwivedi 		if (field_type == 0)
3435d83525caSAlexei Starovoitov 			continue;
3436db559117SKumar Kartikeya Dwivedi 		if (field_type < 0)
3437db559117SKumar Kartikeya Dwivedi 			return field_type;
343842ba1308SKumar Kartikeya Dwivedi 
34398293eb99SAlexei Starovoitov 		off = __btf_member_bit_offset(t, member);
3440d83525caSAlexei Starovoitov 		if (off % 8)
3441d83525caSAlexei Starovoitov 			/* valid C code cannot generate such BTF */
3442d83525caSAlexei Starovoitov 			return -EINVAL;
3443d83525caSAlexei Starovoitov 		off /= 8;
344468134668SAlexei Starovoitov 		if (off % align)
3445db559117SKumar Kartikeya Dwivedi 			continue;
344642ba1308SKumar Kartikeya Dwivedi 
344742ba1308SKumar Kartikeya Dwivedi 		switch (field_type) {
3448db559117SKumar Kartikeya Dwivedi 		case BPF_SPIN_LOCK:
3449db559117SKumar Kartikeya Dwivedi 		case BPF_TIMER:
34508ffa5cc1SKumar Kartikeya Dwivedi 		case BPF_LIST_NODE:
34519c395c1bSDave Marchevsky 		case BPF_RB_NODE:
3452d54730b5SDave Marchevsky 		case BPF_REFCOUNT:
3453db559117SKumar Kartikeya Dwivedi 			ret = btf_find_struct(btf, member_type, off, sz, field_type,
345461df10c7SKumar Kartikeya Dwivedi 					      idx < info_cnt ? &info[idx] : &tmp);
345561df10c7SKumar Kartikeya Dwivedi 			if (ret < 0)
345661df10c7SKumar Kartikeya Dwivedi 				return ret;
345761df10c7SKumar Kartikeya Dwivedi 			break;
3458db559117SKumar Kartikeya Dwivedi 		case BPF_KPTR_UNREF:
3459db559117SKumar Kartikeya Dwivedi 		case BPF_KPTR_REF:
346061df10c7SKumar Kartikeya Dwivedi 			ret = btf_find_kptr(btf, member_type, off, sz,
346161df10c7SKumar Kartikeya Dwivedi 					    idx < info_cnt ? &info[idx] : &tmp);
346261df10c7SKumar Kartikeya Dwivedi 			if (ret < 0)
346361df10c7SKumar Kartikeya Dwivedi 				return ret;
346461df10c7SKumar Kartikeya Dwivedi 			break;
3465f0c5941fSKumar Kartikeya Dwivedi 		case BPF_LIST_HEAD:
34669c395c1bSDave Marchevsky 		case BPF_RB_ROOT:
34679c395c1bSDave Marchevsky 			ret = btf_find_graph_root(btf, t, member_type,
34689c395c1bSDave Marchevsky 						  i, off, sz,
34699c395c1bSDave Marchevsky 						  idx < info_cnt ? &info[idx] : &tmp,
34709c395c1bSDave Marchevsky 						  field_type);
3471f0c5941fSKumar Kartikeya Dwivedi 			if (ret < 0)
3472f0c5941fSKumar Kartikeya Dwivedi 				return ret;
3473f0c5941fSKumar Kartikeya Dwivedi 			break;
347442ba1308SKumar Kartikeya Dwivedi 		default:
347542ba1308SKumar Kartikeya Dwivedi 			return -EFAULT;
3476d83525caSAlexei Starovoitov 		}
347761df10c7SKumar Kartikeya Dwivedi 
347861df10c7SKumar Kartikeya Dwivedi 		if (ret == BTF_FIELD_IGNORE)
347961df10c7SKumar Kartikeya Dwivedi 			continue;
348061df10c7SKumar Kartikeya Dwivedi 		if (idx >= info_cnt)
348161df10c7SKumar Kartikeya Dwivedi 			return -E2BIG;
348261df10c7SKumar Kartikeya Dwivedi 		++idx;
348342ba1308SKumar Kartikeya Dwivedi 	}
348461df10c7SKumar Kartikeya Dwivedi 	return idx;
3485d83525caSAlexei Starovoitov }
3486d83525caSAlexei Starovoitov 
btf_find_datasec_var(const struct btf * btf,const struct btf_type * t,u32 field_mask,struct btf_field_info * info,int info_cnt)348768134668SAlexei Starovoitov static int btf_find_datasec_var(const struct btf *btf, const struct btf_type *t,
3488db559117SKumar Kartikeya Dwivedi 				u32 field_mask, struct btf_field_info *info,
3489db559117SKumar Kartikeya Dwivedi 				int info_cnt)
349068134668SAlexei Starovoitov {
3491db559117SKumar Kartikeya Dwivedi 	int ret, idx = 0, align, sz, field_type;
349268134668SAlexei Starovoitov 	const struct btf_var_secinfo *vsi;
349361df10c7SKumar Kartikeya Dwivedi 	struct btf_field_info tmp;
3494db559117SKumar Kartikeya Dwivedi 	u32 i, off, seen_mask = 0;
349568134668SAlexei Starovoitov 
349668134668SAlexei Starovoitov 	for_each_vsi(i, t, vsi) {
349768134668SAlexei Starovoitov 		const struct btf_type *var = btf_type_by_id(btf, vsi->type);
349868134668SAlexei Starovoitov 		const struct btf_type *var_type = btf_type_by_id(btf, var->type);
349968134668SAlexei Starovoitov 
3500db559117SKumar Kartikeya Dwivedi 		field_type = btf_get_field_type(__btf_name_by_offset(btf, var_type->name_off),
3501db559117SKumar Kartikeya Dwivedi 						field_mask, &seen_mask, &align, &sz);
3502db559117SKumar Kartikeya Dwivedi 		if (field_type == 0)
350368134668SAlexei Starovoitov 			continue;
3504db559117SKumar Kartikeya Dwivedi 		if (field_type < 0)
3505db559117SKumar Kartikeya Dwivedi 			return field_type;
3506db559117SKumar Kartikeya Dwivedi 
3507db559117SKumar Kartikeya Dwivedi 		off = vsi->offset;
350868134668SAlexei Starovoitov 		if (vsi->size != sz)
350968134668SAlexei Starovoitov 			continue;
351068134668SAlexei Starovoitov 		if (off % align)
3511db559117SKumar Kartikeya Dwivedi 			continue;
351242ba1308SKumar Kartikeya Dwivedi 
351342ba1308SKumar Kartikeya Dwivedi 		switch (field_type) {
3514db559117SKumar Kartikeya Dwivedi 		case BPF_SPIN_LOCK:
3515db559117SKumar Kartikeya Dwivedi 		case BPF_TIMER:
35168ffa5cc1SKumar Kartikeya Dwivedi 		case BPF_LIST_NODE:
35179c395c1bSDave Marchevsky 		case BPF_RB_NODE:
3518d54730b5SDave Marchevsky 		case BPF_REFCOUNT:
3519db559117SKumar Kartikeya Dwivedi 			ret = btf_find_struct(btf, var_type, off, sz, field_type,
352061df10c7SKumar Kartikeya Dwivedi 					      idx < info_cnt ? &info[idx] : &tmp);
352161df10c7SKumar Kartikeya Dwivedi 			if (ret < 0)
352261df10c7SKumar Kartikeya Dwivedi 				return ret;
352361df10c7SKumar Kartikeya Dwivedi 			break;
3524db559117SKumar Kartikeya Dwivedi 		case BPF_KPTR_UNREF:
3525db559117SKumar Kartikeya Dwivedi 		case BPF_KPTR_REF:
352661df10c7SKumar Kartikeya Dwivedi 			ret = btf_find_kptr(btf, var_type, off, sz,
352761df10c7SKumar Kartikeya Dwivedi 					    idx < info_cnt ? &info[idx] : &tmp);
352861df10c7SKumar Kartikeya Dwivedi 			if (ret < 0)
352961df10c7SKumar Kartikeya Dwivedi 				return ret;
353061df10c7SKumar Kartikeya Dwivedi 			break;
3531f0c5941fSKumar Kartikeya Dwivedi 		case BPF_LIST_HEAD:
35329c395c1bSDave Marchevsky 		case BPF_RB_ROOT:
35339c395c1bSDave Marchevsky 			ret = btf_find_graph_root(btf, var, var_type,
35349c395c1bSDave Marchevsky 						  -1, off, sz,
35359c395c1bSDave Marchevsky 						  idx < info_cnt ? &info[idx] : &tmp,
35369c395c1bSDave Marchevsky 						  field_type);
3537f0c5941fSKumar Kartikeya Dwivedi 			if (ret < 0)
3538f0c5941fSKumar Kartikeya Dwivedi 				return ret;
3539f0c5941fSKumar Kartikeya Dwivedi 			break;
354042ba1308SKumar Kartikeya Dwivedi 		default:
354142ba1308SKumar Kartikeya Dwivedi 			return -EFAULT;
354268134668SAlexei Starovoitov 		}
354361df10c7SKumar Kartikeya Dwivedi 
354461df10c7SKumar Kartikeya Dwivedi 		if (ret == BTF_FIELD_IGNORE)
354561df10c7SKumar Kartikeya Dwivedi 			continue;
354661df10c7SKumar Kartikeya Dwivedi 		if (idx >= info_cnt)
354761df10c7SKumar Kartikeya Dwivedi 			return -E2BIG;
354861df10c7SKumar Kartikeya Dwivedi 		++idx;
354942ba1308SKumar Kartikeya Dwivedi 	}
355061df10c7SKumar Kartikeya Dwivedi 	return idx;
355168134668SAlexei Starovoitov }
355268134668SAlexei Starovoitov 
btf_find_field(const struct btf * btf,const struct btf_type * t,u32 field_mask,struct btf_field_info * info,int info_cnt)355368134668SAlexei Starovoitov static int btf_find_field(const struct btf *btf, const struct btf_type *t,
3554db559117SKumar Kartikeya Dwivedi 			  u32 field_mask, struct btf_field_info *info,
3555db559117SKumar Kartikeya Dwivedi 			  int info_cnt)
355668134668SAlexei Starovoitov {
355768134668SAlexei Starovoitov 	if (__btf_type_is_struct(t))
3558db559117SKumar Kartikeya Dwivedi 		return btf_find_struct_field(btf, t, field_mask, info, info_cnt);
355968134668SAlexei Starovoitov 	else if (btf_type_is_datasec(t))
3560db559117SKumar Kartikeya Dwivedi 		return btf_find_datasec_var(btf, t, field_mask, info, info_cnt);
356168134668SAlexei Starovoitov 	return -EINVAL;
356268134668SAlexei Starovoitov }
356368134668SAlexei Starovoitov 
btf_parse_kptr(const struct btf * btf,struct btf_field * field,struct btf_field_info * info)3564db559117SKumar Kartikeya Dwivedi static int btf_parse_kptr(const struct btf *btf, struct btf_field *field,
3565db559117SKumar Kartikeya Dwivedi 			  struct btf_field_info *info)
356668134668SAlexei Starovoitov {
356714a324f6SKumar Kartikeya Dwivedi 	struct module *mod = NULL;
356861df10c7SKumar Kartikeya Dwivedi 	const struct btf_type *t;
3569c8e18754SDave Marchevsky 	/* If a matching btf type is found in kernel or module BTFs, kptr_ref
3570c8e18754SDave Marchevsky 	 * is that BTF, otherwise it's program BTF
3571c8e18754SDave Marchevsky 	 */
3572c8e18754SDave Marchevsky 	struct btf *kptr_btf;
3573db559117SKumar Kartikeya Dwivedi 	int ret;
357461df10c7SKumar Kartikeya Dwivedi 	s32 id;
357561df10c7SKumar Kartikeya Dwivedi 
357661df10c7SKumar Kartikeya Dwivedi 	/* Find type in map BTF, and use it to look up the matching type
357761df10c7SKumar Kartikeya Dwivedi 	 * in vmlinux or module BTFs, by name and kind.
357861df10c7SKumar Kartikeya Dwivedi 	 */
3579db559117SKumar Kartikeya Dwivedi 	t = btf_type_by_id(btf, info->kptr.type_id);
358061df10c7SKumar Kartikeya Dwivedi 	id = bpf_find_btf_id(__btf_name_by_offset(btf, t->name_off), BTF_INFO_KIND(t->info),
3581c8e18754SDave Marchevsky 			     &kptr_btf);
3582c8e18754SDave Marchevsky 	if (id == -ENOENT) {
3583c8e18754SDave Marchevsky 		/* btf_parse_kptr should only be called w/ btf = program BTF */
3584c8e18754SDave Marchevsky 		WARN_ON_ONCE(btf_is_kernel(btf));
3585c8e18754SDave Marchevsky 
3586c8e18754SDave Marchevsky 		/* Type exists only in program BTF. Assume that it's a MEM_ALLOC
3587c8e18754SDave Marchevsky 		 * kptr allocated via bpf_obj_new
3588c8e18754SDave Marchevsky 		 */
35899e36a204SDave Marchevsky 		field->kptr.dtor = NULL;
3590c8e18754SDave Marchevsky 		id = info->kptr.type_id;
3591c8e18754SDave Marchevsky 		kptr_btf = (struct btf *)btf;
3592c8e18754SDave Marchevsky 		btf_get(kptr_btf);
3593c8e18754SDave Marchevsky 		goto found_dtor;
3594c8e18754SDave Marchevsky 	}
3595db559117SKumar Kartikeya Dwivedi 	if (id < 0)
3596db559117SKumar Kartikeya Dwivedi 		return id;
359761df10c7SKumar Kartikeya Dwivedi 
359814a324f6SKumar Kartikeya Dwivedi 	/* Find and stash the function pointer for the destruction function that
359914a324f6SKumar Kartikeya Dwivedi 	 * needs to be eventually invoked from the map free path.
360014a324f6SKumar Kartikeya Dwivedi 	 */
3601db559117SKumar Kartikeya Dwivedi 	if (info->type == BPF_KPTR_REF) {
360214a324f6SKumar Kartikeya Dwivedi 		const struct btf_type *dtor_func;
360314a324f6SKumar Kartikeya Dwivedi 		const char *dtor_func_name;
360414a324f6SKumar Kartikeya Dwivedi 		unsigned long addr;
360514a324f6SKumar Kartikeya Dwivedi 		s32 dtor_btf_id;
360614a324f6SKumar Kartikeya Dwivedi 
360714a324f6SKumar Kartikeya Dwivedi 		/* This call also serves as a whitelist of allowed objects that
360814a324f6SKumar Kartikeya Dwivedi 		 * can be used as a referenced pointer and be stored in a map at
360914a324f6SKumar Kartikeya Dwivedi 		 * the same time.
361014a324f6SKumar Kartikeya Dwivedi 		 */
3611c8e18754SDave Marchevsky 		dtor_btf_id = btf_find_dtor_kfunc(kptr_btf, id);
361214a324f6SKumar Kartikeya Dwivedi 		if (dtor_btf_id < 0) {
361314a324f6SKumar Kartikeya Dwivedi 			ret = dtor_btf_id;
361414a324f6SKumar Kartikeya Dwivedi 			goto end_btf;
361514a324f6SKumar Kartikeya Dwivedi 		}
361614a324f6SKumar Kartikeya Dwivedi 
3617c8e18754SDave Marchevsky 		dtor_func = btf_type_by_id(kptr_btf, dtor_btf_id);
361814a324f6SKumar Kartikeya Dwivedi 		if (!dtor_func) {
361914a324f6SKumar Kartikeya Dwivedi 			ret = -ENOENT;
362014a324f6SKumar Kartikeya Dwivedi 			goto end_btf;
362114a324f6SKumar Kartikeya Dwivedi 		}
362214a324f6SKumar Kartikeya Dwivedi 
3623c8e18754SDave Marchevsky 		if (btf_is_module(kptr_btf)) {
3624c8e18754SDave Marchevsky 			mod = btf_try_get_module(kptr_btf);
362514a324f6SKumar Kartikeya Dwivedi 			if (!mod) {
362614a324f6SKumar Kartikeya Dwivedi 				ret = -ENXIO;
362714a324f6SKumar Kartikeya Dwivedi 				goto end_btf;
362814a324f6SKumar Kartikeya Dwivedi 			}
362914a324f6SKumar Kartikeya Dwivedi 		}
363014a324f6SKumar Kartikeya Dwivedi 
363114a324f6SKumar Kartikeya Dwivedi 		/* We already verified dtor_func to be btf_type_is_func
363214a324f6SKumar Kartikeya Dwivedi 		 * in register_btf_id_dtor_kfuncs.
363314a324f6SKumar Kartikeya Dwivedi 		 */
3634c8e18754SDave Marchevsky 		dtor_func_name = __btf_name_by_offset(kptr_btf, dtor_func->name_off);
363514a324f6SKumar Kartikeya Dwivedi 		addr = kallsyms_lookup_name(dtor_func_name);
363614a324f6SKumar Kartikeya Dwivedi 		if (!addr) {
363714a324f6SKumar Kartikeya Dwivedi 			ret = -EINVAL;
363814a324f6SKumar Kartikeya Dwivedi 			goto end_mod;
363914a324f6SKumar Kartikeya Dwivedi 		}
3640db559117SKumar Kartikeya Dwivedi 		field->kptr.dtor = (void *)addr;
3641db559117SKumar Kartikeya Dwivedi 	}
3642db559117SKumar Kartikeya Dwivedi 
3643c8e18754SDave Marchevsky found_dtor:
3644db559117SKumar Kartikeya Dwivedi 	field->kptr.btf_id = id;
3645c8e18754SDave Marchevsky 	field->kptr.btf = kptr_btf;
3646db559117SKumar Kartikeya Dwivedi 	field->kptr.module = mod;
3647db559117SKumar Kartikeya Dwivedi 	return 0;
3648db559117SKumar Kartikeya Dwivedi end_mod:
3649db559117SKumar Kartikeya Dwivedi 	module_put(mod);
3650db559117SKumar Kartikeya Dwivedi end_btf:
3651c8e18754SDave Marchevsky 	btf_put(kptr_btf);
3652db559117SKumar Kartikeya Dwivedi 	return ret;
3653db559117SKumar Kartikeya Dwivedi }
3654db559117SKumar Kartikeya Dwivedi 
btf_parse_graph_root(const struct btf * btf,struct btf_field * field,struct btf_field_info * info,const char * node_type_name,size_t node_type_align)36559c395c1bSDave Marchevsky static int btf_parse_graph_root(const struct btf *btf,
36569c395c1bSDave Marchevsky 				struct btf_field *field,
36579c395c1bSDave Marchevsky 				struct btf_field_info *info,
36589c395c1bSDave Marchevsky 				const char *node_type_name,
36599c395c1bSDave Marchevsky 				size_t node_type_align)
3660f0c5941fSKumar Kartikeya Dwivedi {
3661f0c5941fSKumar Kartikeya Dwivedi 	const struct btf_type *t, *n = NULL;
3662f0c5941fSKumar Kartikeya Dwivedi 	const struct btf_member *member;
3663f0c5941fSKumar Kartikeya Dwivedi 	u32 offset;
3664f0c5941fSKumar Kartikeya Dwivedi 	int i;
3665f0c5941fSKumar Kartikeya Dwivedi 
366630465003SDave Marchevsky 	t = btf_type_by_id(btf, info->graph_root.value_btf_id);
3667f0c5941fSKumar Kartikeya Dwivedi 	/* We've already checked that value_btf_id is a struct type. We
3668f0c5941fSKumar Kartikeya Dwivedi 	 * just need to figure out the offset of the list_node, and
3669f0c5941fSKumar Kartikeya Dwivedi 	 * verify its type.
3670f0c5941fSKumar Kartikeya Dwivedi 	 */
3671f0c5941fSKumar Kartikeya Dwivedi 	for_each_member(i, t, member) {
367230465003SDave Marchevsky 		if (strcmp(info->graph_root.node_name,
367330465003SDave Marchevsky 			   __btf_name_by_offset(btf, member->name_off)))
3674f0c5941fSKumar Kartikeya Dwivedi 			continue;
3675f0c5941fSKumar Kartikeya Dwivedi 		/* Invalid BTF, two members with same name */
3676f0c5941fSKumar Kartikeya Dwivedi 		if (n)
3677f0c5941fSKumar Kartikeya Dwivedi 			return -EINVAL;
3678f0c5941fSKumar Kartikeya Dwivedi 		n = btf_type_by_id(btf, member->type);
3679f0c5941fSKumar Kartikeya Dwivedi 		if (!__btf_type_is_struct(n))
3680f0c5941fSKumar Kartikeya Dwivedi 			return -EINVAL;
36819c395c1bSDave Marchevsky 		if (strcmp(node_type_name, __btf_name_by_offset(btf, n->name_off)))
3682f0c5941fSKumar Kartikeya Dwivedi 			return -EINVAL;
3683f0c5941fSKumar Kartikeya Dwivedi 		offset = __btf_member_bit_offset(n, member);
3684f0c5941fSKumar Kartikeya Dwivedi 		if (offset % 8)
3685f0c5941fSKumar Kartikeya Dwivedi 			return -EINVAL;
3686f0c5941fSKumar Kartikeya Dwivedi 		offset /= 8;
36879c395c1bSDave Marchevsky 		if (offset % node_type_align)
3688f0c5941fSKumar Kartikeya Dwivedi 			return -EINVAL;
3689f0c5941fSKumar Kartikeya Dwivedi 
369030465003SDave Marchevsky 		field->graph_root.btf = (struct btf *)btf;
369130465003SDave Marchevsky 		field->graph_root.value_btf_id = info->graph_root.value_btf_id;
369230465003SDave Marchevsky 		field->graph_root.node_offset = offset;
3693f0c5941fSKumar Kartikeya Dwivedi 	}
3694f0c5941fSKumar Kartikeya Dwivedi 	if (!n)
3695f0c5941fSKumar Kartikeya Dwivedi 		return -ENOENT;
3696f0c5941fSKumar Kartikeya Dwivedi 	return 0;
3697f0c5941fSKumar Kartikeya Dwivedi }
3698f0c5941fSKumar Kartikeya Dwivedi 
btf_parse_list_head(const struct btf * btf,struct btf_field * field,struct btf_field_info * info)36999c395c1bSDave Marchevsky static int btf_parse_list_head(const struct btf *btf, struct btf_field *field,
37009c395c1bSDave Marchevsky 			       struct btf_field_info *info)
37019c395c1bSDave Marchevsky {
37029c395c1bSDave Marchevsky 	return btf_parse_graph_root(btf, field, info, "bpf_list_node",
37039c395c1bSDave Marchevsky 					    __alignof__(struct bpf_list_node));
37049c395c1bSDave Marchevsky }
37059c395c1bSDave Marchevsky 
btf_parse_rb_root(const struct btf * btf,struct btf_field * field,struct btf_field_info * info)37069c395c1bSDave Marchevsky static int btf_parse_rb_root(const struct btf *btf, struct btf_field *field,
37079c395c1bSDave Marchevsky 			     struct btf_field_info *info)
37089c395c1bSDave Marchevsky {
37099c395c1bSDave Marchevsky 	return btf_parse_graph_root(btf, field, info, "bpf_rb_node",
37109c395c1bSDave Marchevsky 					    __alignof__(struct bpf_rb_node));
37119c395c1bSDave Marchevsky }
37129c395c1bSDave Marchevsky 
btf_field_cmp(const void * _a,const void * _b,const void * priv)3713cd2a8079SDave Marchevsky static int btf_field_cmp(const void *_a, const void *_b, const void *priv)
3714cd2a8079SDave Marchevsky {
3715cd2a8079SDave Marchevsky 	const struct btf_field *a = (const struct btf_field *)_a;
3716cd2a8079SDave Marchevsky 	const struct btf_field *b = (const struct btf_field *)_b;
3717cd2a8079SDave Marchevsky 
3718cd2a8079SDave Marchevsky 	if (a->offset < b->offset)
3719cd2a8079SDave Marchevsky 		return -1;
3720cd2a8079SDave Marchevsky 	else if (a->offset > b->offset)
3721cd2a8079SDave Marchevsky 		return 1;
3722cd2a8079SDave Marchevsky 	return 0;
3723cd2a8079SDave Marchevsky }
3724cd2a8079SDave Marchevsky 
btf_parse_fields(const struct btf * btf,const struct btf_type * t,u32 field_mask,u32 value_size)3725db559117SKumar Kartikeya Dwivedi struct btf_record *btf_parse_fields(const struct btf *btf, const struct btf_type *t,
3726db559117SKumar Kartikeya Dwivedi 				    u32 field_mask, u32 value_size)
3727db559117SKumar Kartikeya Dwivedi {
3728db559117SKumar Kartikeya Dwivedi 	struct btf_field_info info_arr[BTF_FIELDS_MAX];
3729cd2a8079SDave Marchevsky 	u32 next_off = 0, field_type_size;
3730db559117SKumar Kartikeya Dwivedi 	struct btf_record *rec;
3731db559117SKumar Kartikeya Dwivedi 	int ret, i, cnt;
3732db559117SKumar Kartikeya Dwivedi 
3733db559117SKumar Kartikeya Dwivedi 	ret = btf_find_field(btf, t, field_mask, info_arr, ARRAY_SIZE(info_arr));
3734db559117SKumar Kartikeya Dwivedi 	if (ret < 0)
3735db559117SKumar Kartikeya Dwivedi 		return ERR_PTR(ret);
3736db559117SKumar Kartikeya Dwivedi 	if (!ret)
3737db559117SKumar Kartikeya Dwivedi 		return NULL;
3738db559117SKumar Kartikeya Dwivedi 
3739db559117SKumar Kartikeya Dwivedi 	cnt = ret;
3740c22dfdd2SKumar Kartikeya Dwivedi 	/* This needs to be kzalloc to zero out padding and unused fields, see
3741c22dfdd2SKumar Kartikeya Dwivedi 	 * comment in btf_record_equal.
3742c22dfdd2SKumar Kartikeya Dwivedi 	 */
3743db559117SKumar Kartikeya Dwivedi 	rec = kzalloc(offsetof(struct btf_record, fields[cnt]), GFP_KERNEL | __GFP_NOWARN);
3744db559117SKumar Kartikeya Dwivedi 	if (!rec)
3745db559117SKumar Kartikeya Dwivedi 		return ERR_PTR(-ENOMEM);
3746db559117SKumar Kartikeya Dwivedi 
3747db559117SKumar Kartikeya Dwivedi 	rec->spin_lock_off = -EINVAL;
3748db559117SKumar Kartikeya Dwivedi 	rec->timer_off = -EINVAL;
3749d54730b5SDave Marchevsky 	rec->refcount_off = -EINVAL;
3750db559117SKumar Kartikeya Dwivedi 	for (i = 0; i < cnt; i++) {
3751cd2a8079SDave Marchevsky 		field_type_size = btf_field_type_size(info_arr[i].type);
3752cd2a8079SDave Marchevsky 		if (info_arr[i].off + field_type_size > value_size) {
3753db559117SKumar Kartikeya Dwivedi 			WARN_ONCE(1, "verifier bug off %d size %d", info_arr[i].off, value_size);
3754db559117SKumar Kartikeya Dwivedi 			ret = -EFAULT;
3755db559117SKumar Kartikeya Dwivedi 			goto end;
375614a324f6SKumar Kartikeya Dwivedi 		}
3757f0c5941fSKumar Kartikeya Dwivedi 		if (info_arr[i].off < next_off) {
3758f0c5941fSKumar Kartikeya Dwivedi 			ret = -EEXIST;
3759f0c5941fSKumar Kartikeya Dwivedi 			goto end;
3760f0c5941fSKumar Kartikeya Dwivedi 		}
3761cd2a8079SDave Marchevsky 		next_off = info_arr[i].off + field_type_size;
376214a324f6SKumar Kartikeya Dwivedi 
3763aa3496acSKumar Kartikeya Dwivedi 		rec->field_mask |= info_arr[i].type;
3764aa3496acSKumar Kartikeya Dwivedi 		rec->fields[i].offset = info_arr[i].off;
3765aa3496acSKumar Kartikeya Dwivedi 		rec->fields[i].type = info_arr[i].type;
3766cd2a8079SDave Marchevsky 		rec->fields[i].size = field_type_size;
3767db559117SKumar Kartikeya Dwivedi 
3768db559117SKumar Kartikeya Dwivedi 		switch (info_arr[i].type) {
3769db559117SKumar Kartikeya Dwivedi 		case BPF_SPIN_LOCK:
3770db559117SKumar Kartikeya Dwivedi 			WARN_ON_ONCE(rec->spin_lock_off >= 0);
3771db559117SKumar Kartikeya Dwivedi 			/* Cache offset for faster lookup at runtime */
3772db559117SKumar Kartikeya Dwivedi 			rec->spin_lock_off = rec->fields[i].offset;
3773db559117SKumar Kartikeya Dwivedi 			break;
3774db559117SKumar Kartikeya Dwivedi 		case BPF_TIMER:
3775db559117SKumar Kartikeya Dwivedi 			WARN_ON_ONCE(rec->timer_off >= 0);
3776db559117SKumar Kartikeya Dwivedi 			/* Cache offset for faster lookup at runtime */
3777db559117SKumar Kartikeya Dwivedi 			rec->timer_off = rec->fields[i].offset;
3778db559117SKumar Kartikeya Dwivedi 			break;
3779d54730b5SDave Marchevsky 		case BPF_REFCOUNT:
3780d54730b5SDave Marchevsky 			WARN_ON_ONCE(rec->refcount_off >= 0);
3781d54730b5SDave Marchevsky 			/* Cache offset for faster lookup at runtime */
3782d54730b5SDave Marchevsky 			rec->refcount_off = rec->fields[i].offset;
3783d54730b5SDave Marchevsky 			break;
3784db559117SKumar Kartikeya Dwivedi 		case BPF_KPTR_UNREF:
3785db559117SKumar Kartikeya Dwivedi 		case BPF_KPTR_REF:
3786db559117SKumar Kartikeya Dwivedi 			ret = btf_parse_kptr(btf, &rec->fields[i], &info_arr[i]);
3787db559117SKumar Kartikeya Dwivedi 			if (ret < 0)
3788db559117SKumar Kartikeya Dwivedi 				goto end;
3789db559117SKumar Kartikeya Dwivedi 			break;
3790f0c5941fSKumar Kartikeya Dwivedi 		case BPF_LIST_HEAD:
3791f0c5941fSKumar Kartikeya Dwivedi 			ret = btf_parse_list_head(btf, &rec->fields[i], &info_arr[i]);
3792f0c5941fSKumar Kartikeya Dwivedi 			if (ret < 0)
3793f0c5941fSKumar Kartikeya Dwivedi 				goto end;
3794f0c5941fSKumar Kartikeya Dwivedi 			break;
37959c395c1bSDave Marchevsky 		case BPF_RB_ROOT:
37969c395c1bSDave Marchevsky 			ret = btf_parse_rb_root(btf, &rec->fields[i], &info_arr[i]);
37979c395c1bSDave Marchevsky 			if (ret < 0)
37989c395c1bSDave Marchevsky 				goto end;
37999c395c1bSDave Marchevsky 			break;
38008ffa5cc1SKumar Kartikeya Dwivedi 		case BPF_LIST_NODE:
38019c395c1bSDave Marchevsky 		case BPF_RB_NODE:
38028ffa5cc1SKumar Kartikeya Dwivedi 			break;
3803db559117SKumar Kartikeya Dwivedi 		default:
3804db559117SKumar Kartikeya Dwivedi 			ret = -EFAULT;
3805db559117SKumar Kartikeya Dwivedi 			goto end;
3806db559117SKumar Kartikeya Dwivedi 		}
3807aa3496acSKumar Kartikeya Dwivedi 		rec->cnt++;
380861df10c7SKumar Kartikeya Dwivedi 	}
3809f0c5941fSKumar Kartikeya Dwivedi 
38109c395c1bSDave Marchevsky 	/* bpf_{list_head, rb_node} require bpf_spin_lock */
38119c395c1bSDave Marchevsky 	if ((btf_record_has_field(rec, BPF_LIST_HEAD) ||
38129c395c1bSDave Marchevsky 	     btf_record_has_field(rec, BPF_RB_ROOT)) && rec->spin_lock_off < 0) {
3813f0c5941fSKumar Kartikeya Dwivedi 		ret = -EINVAL;
3814f0c5941fSKumar Kartikeya Dwivedi 		goto end;
3815f0c5941fSKumar Kartikeya Dwivedi 	}
3816f0c5941fSKumar Kartikeya Dwivedi 
3817404ad75aSDave Marchevsky 	if (rec->refcount_off < 0 &&
3818404ad75aSDave Marchevsky 	    btf_record_has_field(rec, BPF_LIST_NODE) &&
3819a40d3632SDave Marchevsky 	    btf_record_has_field(rec, BPF_RB_NODE)) {
3820a40d3632SDave Marchevsky 		ret = -EINVAL;
3821a40d3632SDave Marchevsky 		goto end;
3822a40d3632SDave Marchevsky 	}
3823a40d3632SDave Marchevsky 
3824cd2a8079SDave Marchevsky 	sort_r(rec->fields, rec->cnt, sizeof(struct btf_field), btf_field_cmp,
3825cd2a8079SDave Marchevsky 	       NULL, rec);
3826cd2a8079SDave Marchevsky 
3827aa3496acSKumar Kartikeya Dwivedi 	return rec;
382861df10c7SKumar Kartikeya Dwivedi end:
3829aa3496acSKumar Kartikeya Dwivedi 	btf_record_free(rec);
383061df10c7SKumar Kartikeya Dwivedi 	return ERR_PTR(ret);
383161df10c7SKumar Kartikeya Dwivedi }
383261df10c7SKumar Kartikeya Dwivedi 
38339c395c1bSDave Marchevsky #define GRAPH_ROOT_MASK (BPF_LIST_HEAD | BPF_RB_ROOT)
38349c395c1bSDave Marchevsky #define GRAPH_NODE_MASK (BPF_LIST_NODE | BPF_RB_NODE)
38359c395c1bSDave Marchevsky 
btf_check_and_fixup_fields(const struct btf * btf,struct btf_record * rec)3836865ce09aSKumar Kartikeya Dwivedi int btf_check_and_fixup_fields(const struct btf *btf, struct btf_record *rec)
3837865ce09aSKumar Kartikeya Dwivedi {
3838865ce09aSKumar Kartikeya Dwivedi 	int i;
3839865ce09aSKumar Kartikeya Dwivedi 
38409c395c1bSDave Marchevsky 	/* There are three types that signify ownership of some other type:
38419c395c1bSDave Marchevsky 	 *  kptr_ref, bpf_list_head, bpf_rb_root.
38429c395c1bSDave Marchevsky 	 * kptr_ref only supports storing kernel types, which can't store
38439c395c1bSDave Marchevsky 	 * references to program allocated local types.
38449c395c1bSDave Marchevsky 	 *
38459c395c1bSDave Marchevsky 	 * Hence we only need to ensure that bpf_{list_head,rb_root} ownership
38469c395c1bSDave Marchevsky 	 * does not form cycles.
3847865ce09aSKumar Kartikeya Dwivedi 	 */
38489c395c1bSDave Marchevsky 	if (IS_ERR_OR_NULL(rec) || !(rec->field_mask & GRAPH_ROOT_MASK))
3849865ce09aSKumar Kartikeya Dwivedi 		return 0;
3850865ce09aSKumar Kartikeya Dwivedi 	for (i = 0; i < rec->cnt; i++) {
3851865ce09aSKumar Kartikeya Dwivedi 		struct btf_struct_meta *meta;
3852865ce09aSKumar Kartikeya Dwivedi 		u32 btf_id;
3853865ce09aSKumar Kartikeya Dwivedi 
38549c395c1bSDave Marchevsky 		if (!(rec->fields[i].type & GRAPH_ROOT_MASK))
3855865ce09aSKumar Kartikeya Dwivedi 			continue;
385630465003SDave Marchevsky 		btf_id = rec->fields[i].graph_root.value_btf_id;
3857865ce09aSKumar Kartikeya Dwivedi 		meta = btf_find_struct_meta(btf, btf_id);
3858865ce09aSKumar Kartikeya Dwivedi 		if (!meta)
3859865ce09aSKumar Kartikeya Dwivedi 			return -EFAULT;
386030465003SDave Marchevsky 		rec->fields[i].graph_root.value_rec = meta->record;
3861865ce09aSKumar Kartikeya Dwivedi 
38629c395c1bSDave Marchevsky 		/* We need to set value_rec for all root types, but no need
38639c395c1bSDave Marchevsky 		 * to check ownership cycle for a type unless it's also a
38649c395c1bSDave Marchevsky 		 * node type.
38659c395c1bSDave Marchevsky 		 */
38669c395c1bSDave Marchevsky 		if (!(rec->field_mask & GRAPH_NODE_MASK))
3867865ce09aSKumar Kartikeya Dwivedi 			continue;
3868865ce09aSKumar Kartikeya Dwivedi 
3869865ce09aSKumar Kartikeya Dwivedi 		/* We need to ensure ownership acyclicity among all types. The
3870865ce09aSKumar Kartikeya Dwivedi 		 * proper way to do it would be to topologically sort all BTF
3871865ce09aSKumar Kartikeya Dwivedi 		 * IDs based on the ownership edges, since there can be multiple
38729c395c1bSDave Marchevsky 		 * bpf_{list_head,rb_node} in a type. Instead, we use the
38739c395c1bSDave Marchevsky 		 * following resaoning:
3874865ce09aSKumar Kartikeya Dwivedi 		 *
3875865ce09aSKumar Kartikeya Dwivedi 		 * - A type can only be owned by another type in user BTF if it
38769c395c1bSDave Marchevsky 		 *   has a bpf_{list,rb}_node. Let's call these node types.
3877865ce09aSKumar Kartikeya Dwivedi 		 * - A type can only _own_ another type in user BTF if it has a
38789c395c1bSDave Marchevsky 		 *   bpf_{list_head,rb_root}. Let's call these root types.
3879865ce09aSKumar Kartikeya Dwivedi 		 *
38809c395c1bSDave Marchevsky 		 * We ensure that if a type is both a root and node, its
38819c395c1bSDave Marchevsky 		 * element types cannot be root types.
3882865ce09aSKumar Kartikeya Dwivedi 		 *
3883865ce09aSKumar Kartikeya Dwivedi 		 * To ensure acyclicity:
3884865ce09aSKumar Kartikeya Dwivedi 		 *
38859c395c1bSDave Marchevsky 		 * When A is an root type but not a node, its ownership
38869c395c1bSDave Marchevsky 		 * chain can be:
3887865ce09aSKumar Kartikeya Dwivedi 		 *	A -> B -> C
3888865ce09aSKumar Kartikeya Dwivedi 		 * Where:
38899c395c1bSDave Marchevsky 		 * - A is an root, e.g. has bpf_rb_root.
38909c395c1bSDave Marchevsky 		 * - B is both a root and node, e.g. has bpf_rb_node and
38919c395c1bSDave Marchevsky 		 *   bpf_list_head.
38929c395c1bSDave Marchevsky 		 * - C is only an root, e.g. has bpf_list_node
3893865ce09aSKumar Kartikeya Dwivedi 		 *
38949c395c1bSDave Marchevsky 		 * When A is both a root and node, some other type already
38959c395c1bSDave Marchevsky 		 * owns it in the BTF domain, hence it can not own
38969c395c1bSDave Marchevsky 		 * another root type through any of the ownership edges.
3897865ce09aSKumar Kartikeya Dwivedi 		 *	A -> B
3898865ce09aSKumar Kartikeya Dwivedi 		 * Where:
38999c395c1bSDave Marchevsky 		 * - A is both an root and node.
39009c395c1bSDave Marchevsky 		 * - B is only an node.
3901865ce09aSKumar Kartikeya Dwivedi 		 */
39029c395c1bSDave Marchevsky 		if (meta->record->field_mask & GRAPH_ROOT_MASK)
3903865ce09aSKumar Kartikeya Dwivedi 			return -ELOOP;
3904865ce09aSKumar Kartikeya Dwivedi 	}
3905865ce09aSKumar Kartikeya Dwivedi 	return 0;
3906865ce09aSKumar Kartikeya Dwivedi }
3907865ce09aSKumar Kartikeya Dwivedi 
__btf_struct_show(const struct btf * btf,const struct btf_type * t,u32 type_id,void * data,u8 bits_offset,struct btf_show * show)390831d0bc81SAlan Maguire static void __btf_struct_show(const struct btf *btf, const struct btf_type *t,
3909b00b8daeSMartin KaFai Lau 			      u32 type_id, void *data, u8 bits_offset,
391031d0bc81SAlan Maguire 			      struct btf_show *show)
3911b00b8daeSMartin KaFai Lau {
3912b00b8daeSMartin KaFai Lau 	const struct btf_member *member;
391331d0bc81SAlan Maguire 	void *safe_data;
3914b00b8daeSMartin KaFai Lau 	u32 i;
3915b00b8daeSMartin KaFai Lau 
391631d0bc81SAlan Maguire 	safe_data = btf_show_start_struct_type(show, t, type_id, data);
391731d0bc81SAlan Maguire 	if (!safe_data)
391831d0bc81SAlan Maguire 		return;
391931d0bc81SAlan Maguire 
3920b00b8daeSMartin KaFai Lau 	for_each_member(i, t, member) {
3921b00b8daeSMartin KaFai Lau 		const struct btf_type *member_type = btf_type_by_id(btf,
3922b00b8daeSMartin KaFai Lau 								member->type);
3923b00b8daeSMartin KaFai Lau 		const struct btf_kind_operations *ops;
39249d5f9f70SYonghong Song 		u32 member_offset, bitfield_size;
39259d5f9f70SYonghong Song 		u32 bytes_offset;
39269d5f9f70SYonghong Song 		u8 bits8_offset;
3927b00b8daeSMartin KaFai Lau 
392831d0bc81SAlan Maguire 		btf_show_start_member(show, member);
3929b00b8daeSMartin KaFai Lau 
39308293eb99SAlexei Starovoitov 		member_offset = __btf_member_bit_offset(t, member);
39318293eb99SAlexei Starovoitov 		bitfield_size = __btf_member_bitfield_size(t, member);
39329d5f9f70SYonghong Song 		bytes_offset = BITS_ROUNDDOWN_BYTES(member_offset);
39339d5f9f70SYonghong Song 		bits8_offset = BITS_PER_BYTE_MASKED(member_offset);
393417e3ac81SYonghong Song 		if (bitfield_size) {
393531d0bc81SAlan Maguire 			safe_data = btf_show_start_type(show, member_type,
393631d0bc81SAlan Maguire 							member->type,
393731d0bc81SAlan Maguire 							data + bytes_offset);
393831d0bc81SAlan Maguire 			if (safe_data)
393931d0bc81SAlan Maguire 				btf_bitfield_show(safe_data,
394031d0bc81SAlan Maguire 						  bits8_offset,
394131d0bc81SAlan Maguire 						  bitfield_size, show);
394231d0bc81SAlan Maguire 			btf_show_end_type(show);
394317e3ac81SYonghong Song 		} else {
3944b00b8daeSMartin KaFai Lau 			ops = btf_type_ops(member_type);
394531d0bc81SAlan Maguire 			ops->show(btf, member_type, member->type,
394631d0bc81SAlan Maguire 				  data + bytes_offset, bits8_offset, show);
3947b00b8daeSMartin KaFai Lau 		}
394831d0bc81SAlan Maguire 
394931d0bc81SAlan Maguire 		btf_show_end_member(show);
39509d5f9f70SYonghong Song 	}
395131d0bc81SAlan Maguire 
395231d0bc81SAlan Maguire 	btf_show_end_struct_type(show);
395331d0bc81SAlan Maguire }
395431d0bc81SAlan Maguire 
btf_struct_show(const struct btf * btf,const struct btf_type * t,u32 type_id,void * data,u8 bits_offset,struct btf_show * show)395531d0bc81SAlan Maguire static void btf_struct_show(const struct btf *btf, const struct btf_type *t,
395631d0bc81SAlan Maguire 			    u32 type_id, void *data, u8 bits_offset,
395731d0bc81SAlan Maguire 			    struct btf_show *show)
395831d0bc81SAlan Maguire {
395931d0bc81SAlan Maguire 	const struct btf_member *m = show->state.member;
396031d0bc81SAlan Maguire 
396131d0bc81SAlan Maguire 	/*
396231d0bc81SAlan Maguire 	 * First check if any members would be shown (are non-zero).
396331d0bc81SAlan Maguire 	 * See comments above "struct btf_show" definition for more
396431d0bc81SAlan Maguire 	 * details on how this works at a high-level.
396531d0bc81SAlan Maguire 	 */
396631d0bc81SAlan Maguire 	if (show->state.depth > 0 && !(show->flags & BTF_SHOW_ZERO)) {
396731d0bc81SAlan Maguire 		if (!show->state.depth_check) {
396831d0bc81SAlan Maguire 			show->state.depth_check = show->state.depth + 1;
396931d0bc81SAlan Maguire 			show->state.depth_to_show = 0;
397031d0bc81SAlan Maguire 		}
397131d0bc81SAlan Maguire 		__btf_struct_show(btf, t, type_id, data, bits_offset, show);
397231d0bc81SAlan Maguire 		/* Restore saved member data here */
397331d0bc81SAlan Maguire 		show->state.member = m;
397431d0bc81SAlan Maguire 		if (show->state.depth_check != show->state.depth + 1)
397531d0bc81SAlan Maguire 			return;
397631d0bc81SAlan Maguire 		show->state.depth_check = 0;
397731d0bc81SAlan Maguire 
397831d0bc81SAlan Maguire 		if (show->state.depth_to_show <= show->state.depth)
397931d0bc81SAlan Maguire 			return;
398031d0bc81SAlan Maguire 		/*
398131d0bc81SAlan Maguire 		 * Reaching here indicates we have recursed and found
398231d0bc81SAlan Maguire 		 * non-zero child values.
398331d0bc81SAlan Maguire 		 */
398431d0bc81SAlan Maguire 	}
398531d0bc81SAlan Maguire 
398631d0bc81SAlan Maguire 	__btf_struct_show(btf, t, type_id, data, bits_offset, show);
3987b00b8daeSMartin KaFai Lau }
3988b00b8daeSMartin KaFai Lau 
398969b693f0SMartin KaFai Lau static struct btf_kind_operations struct_ops = {
399069b693f0SMartin KaFai Lau 	.check_meta = btf_struct_check_meta,
3991eb3f595dSMartin KaFai Lau 	.resolve = btf_struct_resolve,
3992179cde8cSMartin KaFai Lau 	.check_member = btf_struct_check_member,
39939d5f9f70SYonghong Song 	.check_kflag_member = btf_generic_check_kflag_member,
399469b693f0SMartin KaFai Lau 	.log_details = btf_struct_log,
399531d0bc81SAlan Maguire 	.show = btf_struct_show,
399669b693f0SMartin KaFai Lau };
399769b693f0SMartin KaFai Lau 
btf_enum_check_member(struct btf_verifier_env * env,const struct btf_type * struct_type,const struct btf_member * member,const struct btf_type * member_type)3998179cde8cSMartin KaFai Lau static int btf_enum_check_member(struct btf_verifier_env *env,
3999179cde8cSMartin KaFai Lau 				 const struct btf_type *struct_type,
4000179cde8cSMartin KaFai Lau 				 const struct btf_member *member,
4001179cde8cSMartin KaFai Lau 				 const struct btf_type *member_type)
4002179cde8cSMartin KaFai Lau {
4003179cde8cSMartin KaFai Lau 	u32 struct_bits_off = member->offset;
4004179cde8cSMartin KaFai Lau 	u32 struct_size, bytes_offset;
4005179cde8cSMartin KaFai Lau 
4006179cde8cSMartin KaFai Lau 	if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
4007179cde8cSMartin KaFai Lau 		btf_verifier_log_member(env, struct_type, member,
4008179cde8cSMartin KaFai Lau 					"Member is not byte aligned");
4009179cde8cSMartin KaFai Lau 		return -EINVAL;
4010179cde8cSMartin KaFai Lau 	}
4011179cde8cSMartin KaFai Lau 
4012179cde8cSMartin KaFai Lau 	struct_size = struct_type->size;
4013179cde8cSMartin KaFai Lau 	bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
4014da6c7faeSYoshiki Komachi 	if (struct_size - bytes_offset < member_type->size) {
4015179cde8cSMartin KaFai Lau 		btf_verifier_log_member(env, struct_type, member,
4016179cde8cSMartin KaFai Lau 					"Member exceeds struct_size");
4017179cde8cSMartin KaFai Lau 		return -EINVAL;
4018179cde8cSMartin KaFai Lau 	}
4019179cde8cSMartin KaFai Lau 
4020179cde8cSMartin KaFai Lau 	return 0;
4021179cde8cSMartin KaFai Lau }
4022179cde8cSMartin KaFai Lau 
btf_enum_check_kflag_member(struct btf_verifier_env * env,const struct btf_type * struct_type,const struct btf_member * member,const struct btf_type * member_type)40239d5f9f70SYonghong Song static int btf_enum_check_kflag_member(struct btf_verifier_env *env,
40249d5f9f70SYonghong Song 				       const struct btf_type *struct_type,
40259d5f9f70SYonghong Song 				       const struct btf_member *member,
40269d5f9f70SYonghong Song 				       const struct btf_type *member_type)
40279d5f9f70SYonghong Song {
40289d5f9f70SYonghong Song 	u32 struct_bits_off, nr_bits, bytes_end, struct_size;
40299d5f9f70SYonghong Song 	u32 int_bitsize = sizeof(int) * BITS_PER_BYTE;
40309d5f9f70SYonghong Song 
40319d5f9f70SYonghong Song 	struct_bits_off = BTF_MEMBER_BIT_OFFSET(member->offset);
40329d5f9f70SYonghong Song 	nr_bits = BTF_MEMBER_BITFIELD_SIZE(member->offset);
40339d5f9f70SYonghong Song 	if (!nr_bits) {
40349d5f9f70SYonghong Song 		if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
40359d5f9f70SYonghong Song 			btf_verifier_log_member(env, struct_type, member,
40369d5f9f70SYonghong Song 						"Member is not byte aligned");
40379d5f9f70SYonghong Song 			return -EINVAL;
40389d5f9f70SYonghong Song 		}
40399d5f9f70SYonghong Song 
40409d5f9f70SYonghong Song 		nr_bits = int_bitsize;
40419d5f9f70SYonghong Song 	} else if (nr_bits > int_bitsize) {
40429d5f9f70SYonghong Song 		btf_verifier_log_member(env, struct_type, member,
40439d5f9f70SYonghong Song 					"Invalid member bitfield_size");
40449d5f9f70SYonghong Song 		return -EINVAL;
40459d5f9f70SYonghong Song 	}
40469d5f9f70SYonghong Song 
40479d5f9f70SYonghong Song 	struct_size = struct_type->size;
40489d5f9f70SYonghong Song 	bytes_end = BITS_ROUNDUP_BYTES(struct_bits_off + nr_bits);
40499d5f9f70SYonghong Song 	if (struct_size < bytes_end) {
40509d5f9f70SYonghong Song 		btf_verifier_log_member(env, struct_type, member,
40519d5f9f70SYonghong Song 					"Member exceeds struct_size");
40529d5f9f70SYonghong Song 		return -EINVAL;
40539d5f9f70SYonghong Song 	}
40549d5f9f70SYonghong Song 
40559d5f9f70SYonghong Song 	return 0;
40569d5f9f70SYonghong Song }
40579d5f9f70SYonghong Song 
btf_enum_check_meta(struct btf_verifier_env * env,const struct btf_type * t,u32 meta_left)405869b693f0SMartin KaFai Lau static s32 btf_enum_check_meta(struct btf_verifier_env *env,
405969b693f0SMartin KaFai Lau 			       const struct btf_type *t,
406069b693f0SMartin KaFai Lau 			       u32 meta_left)
406169b693f0SMartin KaFai Lau {
406269b693f0SMartin KaFai Lau 	const struct btf_enum *enums = btf_type_enum(t);
406369b693f0SMartin KaFai Lau 	struct btf *btf = env->btf;
40646089fb32SYonghong Song 	const char *fmt_str;
406569b693f0SMartin KaFai Lau 	u16 i, nr_enums;
406669b693f0SMartin KaFai Lau 	u32 meta_needed;
406769b693f0SMartin KaFai Lau 
406869b693f0SMartin KaFai Lau 	nr_enums = btf_type_vlen(t);
406969b693f0SMartin KaFai Lau 	meta_needed = nr_enums * sizeof(*enums);
407069b693f0SMartin KaFai Lau 
407169b693f0SMartin KaFai Lau 	if (meta_left < meta_needed) {
407269b693f0SMartin KaFai Lau 		btf_verifier_log_basic(env, t,
407369b693f0SMartin KaFai Lau 				       "meta_left:%u meta_needed:%u",
407469b693f0SMartin KaFai Lau 				       meta_left, meta_needed);
407569b693f0SMartin KaFai Lau 		return -EINVAL;
407669b693f0SMartin KaFai Lau 	}
407769b693f0SMartin KaFai Lau 
40789eea9849SAlexei Starovoitov 	if (t->size > 8 || !is_power_of_2(t->size)) {
40799eea9849SAlexei Starovoitov 		btf_verifier_log_type(env, t, "Unexpected size");
408069b693f0SMartin KaFai Lau 		return -EINVAL;
408169b693f0SMartin KaFai Lau 	}
408269b693f0SMartin KaFai Lau 
4083eb04bbb6SYonghong Song 	/* enum type either no name or a valid one */
4084eb04bbb6SYonghong Song 	if (t->name_off &&
4085eb04bbb6SYonghong Song 	    !btf_name_valid_identifier(env->btf, t->name_off)) {
4086eb04bbb6SYonghong Song 		btf_verifier_log_type(env, t, "Invalid name");
4087eb04bbb6SYonghong Song 		return -EINVAL;
4088eb04bbb6SYonghong Song 	}
4089eb04bbb6SYonghong Song 
409069b693f0SMartin KaFai Lau 	btf_verifier_log_type(env, t, NULL);
409169b693f0SMartin KaFai Lau 
409269b693f0SMartin KaFai Lau 	for (i = 0; i < nr_enums; i++) {
4093fbcf93ebSMartin KaFai Lau 		if (!btf_name_offset_valid(btf, enums[i].name_off)) {
409469b693f0SMartin KaFai Lau 			btf_verifier_log(env, "\tInvalid name_offset:%u",
4095fbcf93ebSMartin KaFai Lau 					 enums[i].name_off);
409669b693f0SMartin KaFai Lau 			return -EINVAL;
409769b693f0SMartin KaFai Lau 		}
409869b693f0SMartin KaFai Lau 
4099eb04bbb6SYonghong Song 		/* enum member must have a valid name */
4100eb04bbb6SYonghong Song 		if (!enums[i].name_off ||
4101eb04bbb6SYonghong Song 		    !btf_name_valid_identifier(btf, enums[i].name_off)) {
4102eb04bbb6SYonghong Song 			btf_verifier_log_type(env, t, "Invalid name");
4103eb04bbb6SYonghong Song 			return -EINVAL;
4104eb04bbb6SYonghong Song 		}
4105eb04bbb6SYonghong Song 
41068580ac94SAlexei Starovoitov 		if (env->log.level == BPF_LOG_KERNEL)
41078580ac94SAlexei Starovoitov 			continue;
41086089fb32SYonghong Song 		fmt_str = btf_type_kflag(t) ? "\t%s val=%d\n" : "\t%s val=%u\n";
41096089fb32SYonghong Song 		btf_verifier_log(env, fmt_str,
411023127b33SMartin KaFai Lau 				 __btf_name_by_offset(btf, enums[i].name_off),
411169b693f0SMartin KaFai Lau 				 enums[i].val);
411269b693f0SMartin KaFai Lau 	}
411369b693f0SMartin KaFai Lau 
411469b693f0SMartin KaFai Lau 	return meta_needed;
411569b693f0SMartin KaFai Lau }
411669b693f0SMartin KaFai Lau 
btf_enum_log(struct btf_verifier_env * env,const struct btf_type * t)411769b693f0SMartin KaFai Lau static void btf_enum_log(struct btf_verifier_env *env,
411869b693f0SMartin KaFai Lau 			 const struct btf_type *t)
411969b693f0SMartin KaFai Lau {
412069b693f0SMartin KaFai Lau 	btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t));
412169b693f0SMartin KaFai Lau }
412269b693f0SMartin KaFai Lau 
btf_enum_show(const struct btf * btf,const struct btf_type * t,u32 type_id,void * data,u8 bits_offset,struct btf_show * show)412331d0bc81SAlan Maguire static void btf_enum_show(const struct btf *btf, const struct btf_type *t,
4124b00b8daeSMartin KaFai Lau 			  u32 type_id, void *data, u8 bits_offset,
412531d0bc81SAlan Maguire 			  struct btf_show *show)
4126b00b8daeSMartin KaFai Lau {
4127b00b8daeSMartin KaFai Lau 	const struct btf_enum *enums = btf_type_enum(t);
4128b00b8daeSMartin KaFai Lau 	u32 i, nr_enums = btf_type_vlen(t);
412931d0bc81SAlan Maguire 	void *safe_data;
413031d0bc81SAlan Maguire 	int v;
413131d0bc81SAlan Maguire 
413231d0bc81SAlan Maguire 	safe_data = btf_show_start_type(show, t, type_id, data);
413331d0bc81SAlan Maguire 	if (!safe_data)
413431d0bc81SAlan Maguire 		return;
413531d0bc81SAlan Maguire 
413631d0bc81SAlan Maguire 	v = *(int *)safe_data;
4137b00b8daeSMartin KaFai Lau 
4138b00b8daeSMartin KaFai Lau 	for (i = 0; i < nr_enums; i++) {
413931d0bc81SAlan Maguire 		if (v != enums[i].val)
414031d0bc81SAlan Maguire 			continue;
414131d0bc81SAlan Maguire 
414231d0bc81SAlan Maguire 		btf_show_type_value(show, "%s",
414323127b33SMartin KaFai Lau 				    __btf_name_by_offset(btf,
414423127b33SMartin KaFai Lau 							 enums[i].name_off));
414531d0bc81SAlan Maguire 
414631d0bc81SAlan Maguire 		btf_show_end_type(show);
4147b00b8daeSMartin KaFai Lau 		return;
4148b00b8daeSMartin KaFai Lau 	}
4149b00b8daeSMartin KaFai Lau 
41506089fb32SYonghong Song 	if (btf_type_kflag(t))
415131d0bc81SAlan Maguire 		btf_show_type_value(show, "%d", v);
41526089fb32SYonghong Song 	else
41536089fb32SYonghong Song 		btf_show_type_value(show, "%u", v);
415431d0bc81SAlan Maguire 	btf_show_end_type(show);
4155b00b8daeSMartin KaFai Lau }
4156b00b8daeSMartin KaFai Lau 
415769b693f0SMartin KaFai Lau static struct btf_kind_operations enum_ops = {
415869b693f0SMartin KaFai Lau 	.check_meta = btf_enum_check_meta,
4159eb3f595dSMartin KaFai Lau 	.resolve = btf_df_resolve,
4160179cde8cSMartin KaFai Lau 	.check_member = btf_enum_check_member,
41619d5f9f70SYonghong Song 	.check_kflag_member = btf_enum_check_kflag_member,
416269b693f0SMartin KaFai Lau 	.log_details = btf_enum_log,
416331d0bc81SAlan Maguire 	.show = btf_enum_show,
416469b693f0SMartin KaFai Lau };
416569b693f0SMartin KaFai Lau 
btf_enum64_check_meta(struct btf_verifier_env * env,const struct btf_type * t,u32 meta_left)41666089fb32SYonghong Song static s32 btf_enum64_check_meta(struct btf_verifier_env *env,
41676089fb32SYonghong Song 				 const struct btf_type *t,
41686089fb32SYonghong Song 				 u32 meta_left)
41696089fb32SYonghong Song {
41706089fb32SYonghong Song 	const struct btf_enum64 *enums = btf_type_enum64(t);
41716089fb32SYonghong Song 	struct btf *btf = env->btf;
41726089fb32SYonghong Song 	const char *fmt_str;
41736089fb32SYonghong Song 	u16 i, nr_enums;
41746089fb32SYonghong Song 	u32 meta_needed;
41756089fb32SYonghong Song 
41766089fb32SYonghong Song 	nr_enums = btf_type_vlen(t);
41776089fb32SYonghong Song 	meta_needed = nr_enums * sizeof(*enums);
41786089fb32SYonghong Song 
41796089fb32SYonghong Song 	if (meta_left < meta_needed) {
41806089fb32SYonghong Song 		btf_verifier_log_basic(env, t,
41816089fb32SYonghong Song 				       "meta_left:%u meta_needed:%u",
41826089fb32SYonghong Song 				       meta_left, meta_needed);
41836089fb32SYonghong Song 		return -EINVAL;
41846089fb32SYonghong Song 	}
41856089fb32SYonghong Song 
41866089fb32SYonghong Song 	if (t->size > 8 || !is_power_of_2(t->size)) {
41876089fb32SYonghong Song 		btf_verifier_log_type(env, t, "Unexpected size");
41886089fb32SYonghong Song 		return -EINVAL;
41896089fb32SYonghong Song 	}
41906089fb32SYonghong Song 
41916089fb32SYonghong Song 	/* enum type either no name or a valid one */
41926089fb32SYonghong Song 	if (t->name_off &&
41936089fb32SYonghong Song 	    !btf_name_valid_identifier(env->btf, t->name_off)) {
41946089fb32SYonghong Song 		btf_verifier_log_type(env, t, "Invalid name");
41956089fb32SYonghong Song 		return -EINVAL;
41966089fb32SYonghong Song 	}
41976089fb32SYonghong Song 
41986089fb32SYonghong Song 	btf_verifier_log_type(env, t, NULL);
41996089fb32SYonghong Song 
42006089fb32SYonghong Song 	for (i = 0; i < nr_enums; i++) {
42016089fb32SYonghong Song 		if (!btf_name_offset_valid(btf, enums[i].name_off)) {
42026089fb32SYonghong Song 			btf_verifier_log(env, "\tInvalid name_offset:%u",
42036089fb32SYonghong Song 					 enums[i].name_off);
42046089fb32SYonghong Song 			return -EINVAL;
42056089fb32SYonghong Song 		}
42066089fb32SYonghong Song 
42076089fb32SYonghong Song 		/* enum member must have a valid name */
42086089fb32SYonghong Song 		if (!enums[i].name_off ||
42096089fb32SYonghong Song 		    !btf_name_valid_identifier(btf, enums[i].name_off)) {
42106089fb32SYonghong Song 			btf_verifier_log_type(env, t, "Invalid name");
42116089fb32SYonghong Song 			return -EINVAL;
42126089fb32SYonghong Song 		}
42136089fb32SYonghong Song 
42146089fb32SYonghong Song 		if (env->log.level == BPF_LOG_KERNEL)
42156089fb32SYonghong Song 			continue;
42166089fb32SYonghong Song 
42176089fb32SYonghong Song 		fmt_str = btf_type_kflag(t) ? "\t%s val=%lld\n" : "\t%s val=%llu\n";
42186089fb32SYonghong Song 		btf_verifier_log(env, fmt_str,
42196089fb32SYonghong Song 				 __btf_name_by_offset(btf, enums[i].name_off),
42206089fb32SYonghong Song 				 btf_enum64_value(enums + i));
42216089fb32SYonghong Song 	}
42226089fb32SYonghong Song 
42236089fb32SYonghong Song 	return meta_needed;
42246089fb32SYonghong Song }
42256089fb32SYonghong Song 
btf_enum64_show(const struct btf * btf,const struct btf_type * t,u32 type_id,void * data,u8 bits_offset,struct btf_show * show)42266089fb32SYonghong Song static void btf_enum64_show(const struct btf *btf, const struct btf_type *t,
42276089fb32SYonghong Song 			    u32 type_id, void *data, u8 bits_offset,
42286089fb32SYonghong Song 			    struct btf_show *show)
42296089fb32SYonghong Song {
42306089fb32SYonghong Song 	const struct btf_enum64 *enums = btf_type_enum64(t);
42316089fb32SYonghong Song 	u32 i, nr_enums = btf_type_vlen(t);
42326089fb32SYonghong Song 	void *safe_data;
42336089fb32SYonghong Song 	s64 v;
42346089fb32SYonghong Song 
42356089fb32SYonghong Song 	safe_data = btf_show_start_type(show, t, type_id, data);
42366089fb32SYonghong Song 	if (!safe_data)
42376089fb32SYonghong Song 		return;
42386089fb32SYonghong Song 
42396089fb32SYonghong Song 	v = *(u64 *)safe_data;
42406089fb32SYonghong Song 
42416089fb32SYonghong Song 	for (i = 0; i < nr_enums; i++) {
42426089fb32SYonghong Song 		if (v != btf_enum64_value(enums + i))
42436089fb32SYonghong Song 			continue;
42446089fb32SYonghong Song 
42456089fb32SYonghong Song 		btf_show_type_value(show, "%s",
42466089fb32SYonghong Song 				    __btf_name_by_offset(btf,
42476089fb32SYonghong Song 							 enums[i].name_off));
42486089fb32SYonghong Song 
42496089fb32SYonghong Song 		btf_show_end_type(show);
42506089fb32SYonghong Song 		return;
42516089fb32SYonghong Song 	}
42526089fb32SYonghong Song 
42536089fb32SYonghong Song 	if (btf_type_kflag(t))
42546089fb32SYonghong Song 		btf_show_type_value(show, "%lld", v);
42556089fb32SYonghong Song 	else
42566089fb32SYonghong Song 		btf_show_type_value(show, "%llu", v);
42576089fb32SYonghong Song 	btf_show_end_type(show);
42586089fb32SYonghong Song }
42596089fb32SYonghong Song 
42606089fb32SYonghong Song static struct btf_kind_operations enum64_ops = {
42616089fb32SYonghong Song 	.check_meta = btf_enum64_check_meta,
42626089fb32SYonghong Song 	.resolve = btf_df_resolve,
42636089fb32SYonghong Song 	.check_member = btf_enum_check_member,
42646089fb32SYonghong Song 	.check_kflag_member = btf_enum_check_kflag_member,
42656089fb32SYonghong Song 	.log_details = btf_enum_log,
42666089fb32SYonghong Song 	.show = btf_enum64_show,
42676089fb32SYonghong Song };
42686089fb32SYonghong Song 
btf_func_proto_check_meta(struct btf_verifier_env * env,const struct btf_type * t,u32 meta_left)42692667a262SMartin KaFai Lau static s32 btf_func_proto_check_meta(struct btf_verifier_env *env,
42702667a262SMartin KaFai Lau 				     const struct btf_type *t,
42712667a262SMartin KaFai Lau 				     u32 meta_left)
42722667a262SMartin KaFai Lau {
42732667a262SMartin KaFai Lau 	u32 meta_needed = btf_type_vlen(t) * sizeof(struct btf_param);
42742667a262SMartin KaFai Lau 
42752667a262SMartin KaFai Lau 	if (meta_left < meta_needed) {
42762667a262SMartin KaFai Lau 		btf_verifier_log_basic(env, t,
42772667a262SMartin KaFai Lau 				       "meta_left:%u meta_needed:%u",
42782667a262SMartin KaFai Lau 				       meta_left, meta_needed);
42792667a262SMartin KaFai Lau 		return -EINVAL;
42802667a262SMartin KaFai Lau 	}
42812667a262SMartin KaFai Lau 
42822667a262SMartin KaFai Lau 	if (t->name_off) {
42832667a262SMartin KaFai Lau 		btf_verifier_log_type(env, t, "Invalid name");
42842667a262SMartin KaFai Lau 		return -EINVAL;
42852667a262SMartin KaFai Lau 	}
42862667a262SMartin KaFai Lau 
42879d5f9f70SYonghong Song 	if (btf_type_kflag(t)) {
42889d5f9f70SYonghong Song 		btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
42899d5f9f70SYonghong Song 		return -EINVAL;
42909d5f9f70SYonghong Song 	}
42919d5f9f70SYonghong Song 
42922667a262SMartin KaFai Lau 	btf_verifier_log_type(env, t, NULL);
42932667a262SMartin KaFai Lau 
42942667a262SMartin KaFai Lau 	return meta_needed;
42952667a262SMartin KaFai Lau }
42962667a262SMartin KaFai Lau 
btf_func_proto_log(struct btf_verifier_env * env,const struct btf_type * t)42972667a262SMartin KaFai Lau static void btf_func_proto_log(struct btf_verifier_env *env,
42982667a262SMartin KaFai Lau 			       const struct btf_type *t)
42992667a262SMartin KaFai Lau {
43002667a262SMartin KaFai Lau 	const struct btf_param *args = (const struct btf_param *)(t + 1);
43012667a262SMartin KaFai Lau 	u16 nr_args = btf_type_vlen(t), i;
43022667a262SMartin KaFai Lau 
43032667a262SMartin KaFai Lau 	btf_verifier_log(env, "return=%u args=(", t->type);
43042667a262SMartin KaFai Lau 	if (!nr_args) {
43052667a262SMartin KaFai Lau 		btf_verifier_log(env, "void");
43062667a262SMartin KaFai Lau 		goto done;
43072667a262SMartin KaFai Lau 	}
43082667a262SMartin KaFai Lau 
43092667a262SMartin KaFai Lau 	if (nr_args == 1 && !args[0].type) {
43102667a262SMartin KaFai Lau 		/* Only one vararg */
43112667a262SMartin KaFai Lau 		btf_verifier_log(env, "vararg");
43122667a262SMartin KaFai Lau 		goto done;
43132667a262SMartin KaFai Lau 	}
43142667a262SMartin KaFai Lau 
43152667a262SMartin KaFai Lau 	btf_verifier_log(env, "%u %s", args[0].type,
431623127b33SMartin KaFai Lau 			 __btf_name_by_offset(env->btf,
43172667a262SMartin KaFai Lau 					      args[0].name_off));
43182667a262SMartin KaFai Lau 	for (i = 1; i < nr_args - 1; i++)
43192667a262SMartin KaFai Lau 		btf_verifier_log(env, ", %u %s", args[i].type,
432023127b33SMartin KaFai Lau 				 __btf_name_by_offset(env->btf,
43212667a262SMartin KaFai Lau 						      args[i].name_off));
43222667a262SMartin KaFai Lau 
43232667a262SMartin KaFai Lau 	if (nr_args > 1) {
43242667a262SMartin KaFai Lau 		const struct btf_param *last_arg = &args[nr_args - 1];
43252667a262SMartin KaFai Lau 
43262667a262SMartin KaFai Lau 		if (last_arg->type)
43272667a262SMartin KaFai Lau 			btf_verifier_log(env, ", %u %s", last_arg->type,
432823127b33SMartin KaFai Lau 					 __btf_name_by_offset(env->btf,
43292667a262SMartin KaFai Lau 							      last_arg->name_off));
43302667a262SMartin KaFai Lau 		else
43312667a262SMartin KaFai Lau 			btf_verifier_log(env, ", vararg");
43322667a262SMartin KaFai Lau 	}
43332667a262SMartin KaFai Lau 
43342667a262SMartin KaFai Lau done:
43352667a262SMartin KaFai Lau 	btf_verifier_log(env, ")");
43362667a262SMartin KaFai Lau }
43372667a262SMartin KaFai Lau 
43382667a262SMartin KaFai Lau static struct btf_kind_operations func_proto_ops = {
43392667a262SMartin KaFai Lau 	.check_meta = btf_func_proto_check_meta,
43402667a262SMartin KaFai Lau 	.resolve = btf_df_resolve,
43412667a262SMartin KaFai Lau 	/*
43422667a262SMartin KaFai Lau 	 * BTF_KIND_FUNC_PROTO cannot be directly referred by
43432667a262SMartin KaFai Lau 	 * a struct's member.
43442667a262SMartin KaFai Lau 	 *
43458fb33b60SZhen Lei 	 * It should be a function pointer instead.
43462667a262SMartin KaFai Lau 	 * (i.e. struct's member -> BTF_KIND_PTR -> BTF_KIND_FUNC_PROTO)
43472667a262SMartin KaFai Lau 	 *
43482667a262SMartin KaFai Lau 	 * Hence, there is no btf_func_check_member().
43492667a262SMartin KaFai Lau 	 */
43502667a262SMartin KaFai Lau 	.check_member = btf_df_check_member,
43519d5f9f70SYonghong Song 	.check_kflag_member = btf_df_check_kflag_member,
43522667a262SMartin KaFai Lau 	.log_details = btf_func_proto_log,
435331d0bc81SAlan Maguire 	.show = btf_df_show,
43542667a262SMartin KaFai Lau };
43552667a262SMartin KaFai Lau 
btf_func_check_meta(struct btf_verifier_env * env,const struct btf_type * t,u32 meta_left)43562667a262SMartin KaFai Lau static s32 btf_func_check_meta(struct btf_verifier_env *env,
43572667a262SMartin KaFai Lau 			       const struct btf_type *t,
43582667a262SMartin KaFai Lau 			       u32 meta_left)
43592667a262SMartin KaFai Lau {
43602667a262SMartin KaFai Lau 	if (!t->name_off ||
43612667a262SMartin KaFai Lau 	    !btf_name_valid_identifier(env->btf, t->name_off)) {
43622667a262SMartin KaFai Lau 		btf_verifier_log_type(env, t, "Invalid name");
43632667a262SMartin KaFai Lau 		return -EINVAL;
43642667a262SMartin KaFai Lau 	}
43652667a262SMartin KaFai Lau 
436651c39bb1SAlexei Starovoitov 	if (btf_type_vlen(t) > BTF_FUNC_GLOBAL) {
436751c39bb1SAlexei Starovoitov 		btf_verifier_log_type(env, t, "Invalid func linkage");
43682667a262SMartin KaFai Lau 		return -EINVAL;
43692667a262SMartin KaFai Lau 	}
43702667a262SMartin KaFai Lau 
43719d5f9f70SYonghong Song 	if (btf_type_kflag(t)) {
43729d5f9f70SYonghong Song 		btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
43739d5f9f70SYonghong Song 		return -EINVAL;
43749d5f9f70SYonghong Song 	}
43759d5f9f70SYonghong Song 
43762667a262SMartin KaFai Lau 	btf_verifier_log_type(env, t, NULL);
43772667a262SMartin KaFai Lau 
43782667a262SMartin KaFai Lau 	return 0;
43792667a262SMartin KaFai Lau }
43802667a262SMartin KaFai Lau 
btf_func_resolve(struct btf_verifier_env * env,const struct resolve_vertex * v)4381d7e7b42fSYonghong Song static int btf_func_resolve(struct btf_verifier_env *env,
4382d7e7b42fSYonghong Song 			    const struct resolve_vertex *v)
4383d7e7b42fSYonghong Song {
4384d7e7b42fSYonghong Song 	const struct btf_type *t = v->t;
4385d7e7b42fSYonghong Song 	u32 next_type_id = t->type;
4386d7e7b42fSYonghong Song 	int err;
4387d7e7b42fSYonghong Song 
4388d7e7b42fSYonghong Song 	err = btf_func_check(env, t);
4389d7e7b42fSYonghong Song 	if (err)
4390d7e7b42fSYonghong Song 		return err;
4391d7e7b42fSYonghong Song 
4392d7e7b42fSYonghong Song 	env_stack_pop_resolved(env, next_type_id, 0);
4393d7e7b42fSYonghong Song 	return 0;
4394d7e7b42fSYonghong Song }
4395d7e7b42fSYonghong Song 
43962667a262SMartin KaFai Lau static struct btf_kind_operations func_ops = {
43972667a262SMartin KaFai Lau 	.check_meta = btf_func_check_meta,
4398d7e7b42fSYonghong Song 	.resolve = btf_func_resolve,
43992667a262SMartin KaFai Lau 	.check_member = btf_df_check_member,
44009d5f9f70SYonghong Song 	.check_kflag_member = btf_df_check_kflag_member,
44012667a262SMartin KaFai Lau 	.log_details = btf_ref_type_log,
440231d0bc81SAlan Maguire 	.show = btf_df_show,
44032667a262SMartin KaFai Lau };
44042667a262SMartin KaFai Lau 
btf_var_check_meta(struct btf_verifier_env * env,const struct btf_type * t,u32 meta_left)44051dc92851SDaniel Borkmann static s32 btf_var_check_meta(struct btf_verifier_env *env,
44061dc92851SDaniel Borkmann 			      const struct btf_type *t,
44071dc92851SDaniel Borkmann 			      u32 meta_left)
44081dc92851SDaniel Borkmann {
44091dc92851SDaniel Borkmann 	const struct btf_var *var;
44101dc92851SDaniel Borkmann 	u32 meta_needed = sizeof(*var);
44111dc92851SDaniel Borkmann 
44121dc92851SDaniel Borkmann 	if (meta_left < meta_needed) {
44131dc92851SDaniel Borkmann 		btf_verifier_log_basic(env, t,
44141dc92851SDaniel Borkmann 				       "meta_left:%u meta_needed:%u",
44151dc92851SDaniel Borkmann 				       meta_left, meta_needed);
44161dc92851SDaniel Borkmann 		return -EINVAL;
44171dc92851SDaniel Borkmann 	}
44181dc92851SDaniel Borkmann 
44191dc92851SDaniel Borkmann 	if (btf_type_vlen(t)) {
44201dc92851SDaniel Borkmann 		btf_verifier_log_type(env, t, "vlen != 0");
44211dc92851SDaniel Borkmann 		return -EINVAL;
44221dc92851SDaniel Borkmann 	}
44231dc92851SDaniel Borkmann 
44241dc92851SDaniel Borkmann 	if (btf_type_kflag(t)) {
44251dc92851SDaniel Borkmann 		btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
44261dc92851SDaniel Borkmann 		return -EINVAL;
44271dc92851SDaniel Borkmann 	}
44281dc92851SDaniel Borkmann 
44291dc92851SDaniel Borkmann 	if (!t->name_off ||
44309724160bSFlorent Revest 	    !__btf_name_valid(env->btf, t->name_off)) {
44311dc92851SDaniel Borkmann 		btf_verifier_log_type(env, t, "Invalid name");
44321dc92851SDaniel Borkmann 		return -EINVAL;
44331dc92851SDaniel Borkmann 	}
44341dc92851SDaniel Borkmann 
44351dc92851SDaniel Borkmann 	/* A var cannot be in type void */
44361dc92851SDaniel Borkmann 	if (!t->type || !BTF_TYPE_ID_VALID(t->type)) {
44371dc92851SDaniel Borkmann 		btf_verifier_log_type(env, t, "Invalid type_id");
44381dc92851SDaniel Borkmann 		return -EINVAL;
44391dc92851SDaniel Borkmann 	}
44401dc92851SDaniel Borkmann 
44411dc92851SDaniel Borkmann 	var = btf_type_var(t);
44421dc92851SDaniel Borkmann 	if (var->linkage != BTF_VAR_STATIC &&
44431dc92851SDaniel Borkmann 	    var->linkage != BTF_VAR_GLOBAL_ALLOCATED) {
44441dc92851SDaniel Borkmann 		btf_verifier_log_type(env, t, "Linkage not supported");
44451dc92851SDaniel Borkmann 		return -EINVAL;
44461dc92851SDaniel Borkmann 	}
44471dc92851SDaniel Borkmann 
44481dc92851SDaniel Borkmann 	btf_verifier_log_type(env, t, NULL);
44491dc92851SDaniel Borkmann 
44501dc92851SDaniel Borkmann 	return meta_needed;
44511dc92851SDaniel Borkmann }
44521dc92851SDaniel Borkmann 
btf_var_log(struct btf_verifier_env * env,const struct btf_type * t)44531dc92851SDaniel Borkmann static void btf_var_log(struct btf_verifier_env *env, const struct btf_type *t)
44541dc92851SDaniel Borkmann {
44551dc92851SDaniel Borkmann 	const struct btf_var *var = btf_type_var(t);
44561dc92851SDaniel Borkmann 
44571dc92851SDaniel Borkmann 	btf_verifier_log(env, "type_id=%u linkage=%u", t->type, var->linkage);
44581dc92851SDaniel Borkmann }
44591dc92851SDaniel Borkmann 
44601dc92851SDaniel Borkmann static const struct btf_kind_operations var_ops = {
44611dc92851SDaniel Borkmann 	.check_meta		= btf_var_check_meta,
44621dc92851SDaniel Borkmann 	.resolve		= btf_var_resolve,
44631dc92851SDaniel Borkmann 	.check_member		= btf_df_check_member,
44641dc92851SDaniel Borkmann 	.check_kflag_member	= btf_df_check_kflag_member,
44651dc92851SDaniel Borkmann 	.log_details		= btf_var_log,
446631d0bc81SAlan Maguire 	.show			= btf_var_show,
44671dc92851SDaniel Borkmann };
44681dc92851SDaniel Borkmann 
btf_datasec_check_meta(struct btf_verifier_env * env,const struct btf_type * t,u32 meta_left)44691dc92851SDaniel Borkmann static s32 btf_datasec_check_meta(struct btf_verifier_env *env,
44701dc92851SDaniel Borkmann 				  const struct btf_type *t,
44711dc92851SDaniel Borkmann 				  u32 meta_left)
44721dc92851SDaniel Borkmann {
44731dc92851SDaniel Borkmann 	const struct btf_var_secinfo *vsi;
44741dc92851SDaniel Borkmann 	u64 last_vsi_end_off = 0, sum = 0;
44751dc92851SDaniel Borkmann 	u32 i, meta_needed;
44761dc92851SDaniel Borkmann 
44771dc92851SDaniel Borkmann 	meta_needed = btf_type_vlen(t) * sizeof(*vsi);
44781dc92851SDaniel Borkmann 	if (meta_left < meta_needed) {
44791dc92851SDaniel Borkmann 		btf_verifier_log_basic(env, t,
44801dc92851SDaniel Borkmann 				       "meta_left:%u meta_needed:%u",
44811dc92851SDaniel Borkmann 				       meta_left, meta_needed);
44821dc92851SDaniel Borkmann 		return -EINVAL;
44831dc92851SDaniel Borkmann 	}
44841dc92851SDaniel Borkmann 
44851dc92851SDaniel Borkmann 	if (!t->size) {
44861dc92851SDaniel Borkmann 		btf_verifier_log_type(env, t, "size == 0");
44871dc92851SDaniel Borkmann 		return -EINVAL;
44881dc92851SDaniel Borkmann 	}
44891dc92851SDaniel Borkmann 
44901dc92851SDaniel Borkmann 	if (btf_type_kflag(t)) {
44911dc92851SDaniel Borkmann 		btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
44921dc92851SDaniel Borkmann 		return -EINVAL;
44931dc92851SDaniel Borkmann 	}
44941dc92851SDaniel Borkmann 
44951dc92851SDaniel Borkmann 	if (!t->name_off ||
44961dc92851SDaniel Borkmann 	    !btf_name_valid_section(env->btf, t->name_off)) {
44971dc92851SDaniel Borkmann 		btf_verifier_log_type(env, t, "Invalid name");
44981dc92851SDaniel Borkmann 		return -EINVAL;
44991dc92851SDaniel Borkmann 	}
45001dc92851SDaniel Borkmann 
45011dc92851SDaniel Borkmann 	btf_verifier_log_type(env, t, NULL);
45021dc92851SDaniel Borkmann 
45031dc92851SDaniel Borkmann 	for_each_vsi(i, t, vsi) {
45041dc92851SDaniel Borkmann 		/* A var cannot be in type void */
45051dc92851SDaniel Borkmann 		if (!vsi->type || !BTF_TYPE_ID_VALID(vsi->type)) {
45061dc92851SDaniel Borkmann 			btf_verifier_log_vsi(env, t, vsi,
45071dc92851SDaniel Borkmann 					     "Invalid type_id");
45081dc92851SDaniel Borkmann 			return -EINVAL;
45091dc92851SDaniel Borkmann 		}
45101dc92851SDaniel Borkmann 
45111dc92851SDaniel Borkmann 		if (vsi->offset < last_vsi_end_off || vsi->offset >= t->size) {
45121dc92851SDaniel Borkmann 			btf_verifier_log_vsi(env, t, vsi,
45131dc92851SDaniel Borkmann 					     "Invalid offset");
45141dc92851SDaniel Borkmann 			return -EINVAL;
45151dc92851SDaniel Borkmann 		}
45161dc92851SDaniel Borkmann 
45171dc92851SDaniel Borkmann 		if (!vsi->size || vsi->size > t->size) {
45181dc92851SDaniel Borkmann 			btf_verifier_log_vsi(env, t, vsi,
45191dc92851SDaniel Borkmann 					     "Invalid size");
45201dc92851SDaniel Borkmann 			return -EINVAL;
45211dc92851SDaniel Borkmann 		}
45221dc92851SDaniel Borkmann 
45231dc92851SDaniel Borkmann 		last_vsi_end_off = vsi->offset + vsi->size;
45241dc92851SDaniel Borkmann 		if (last_vsi_end_off > t->size) {
45251dc92851SDaniel Borkmann 			btf_verifier_log_vsi(env, t, vsi,
45261dc92851SDaniel Borkmann 					     "Invalid offset+size");
45271dc92851SDaniel Borkmann 			return -EINVAL;
45281dc92851SDaniel Borkmann 		}
45291dc92851SDaniel Borkmann 
45301dc92851SDaniel Borkmann 		btf_verifier_log_vsi(env, t, vsi, NULL);
45311dc92851SDaniel Borkmann 		sum += vsi->size;
45321dc92851SDaniel Borkmann 	}
45331dc92851SDaniel Borkmann 
45341dc92851SDaniel Borkmann 	if (t->size < sum) {
45351dc92851SDaniel Borkmann 		btf_verifier_log_type(env, t, "Invalid btf_info size");
45361dc92851SDaniel Borkmann 		return -EINVAL;
45371dc92851SDaniel Borkmann 	}
45381dc92851SDaniel Borkmann 
45391dc92851SDaniel Borkmann 	return meta_needed;
45401dc92851SDaniel Borkmann }
45411dc92851SDaniel Borkmann 
btf_datasec_resolve(struct btf_verifier_env * env,const struct resolve_vertex * v)45421dc92851SDaniel Borkmann static int btf_datasec_resolve(struct btf_verifier_env *env,
45431dc92851SDaniel Borkmann 			       const struct resolve_vertex *v)
45441dc92851SDaniel Borkmann {
45451dc92851SDaniel Borkmann 	const struct btf_var_secinfo *vsi;
45461dc92851SDaniel Borkmann 	struct btf *btf = env->btf;
45471dc92851SDaniel Borkmann 	u16 i;
45481dc92851SDaniel Borkmann 
45499b459804SLorenz Bauer 	env->resolve_mode = RESOLVE_TBD;
45501dc92851SDaniel Borkmann 	for_each_vsi_from(i, v->next_member, v->t, vsi) {
45511dc92851SDaniel Borkmann 		u32 var_type_id = vsi->type, type_id, type_size = 0;
45521dc92851SDaniel Borkmann 		const struct btf_type *var_type = btf_type_by_id(env->btf,
45531dc92851SDaniel Borkmann 								 var_type_id);
45541dc92851SDaniel Borkmann 		if (!var_type || !btf_type_is_var(var_type)) {
45551dc92851SDaniel Borkmann 			btf_verifier_log_vsi(env, v->t, vsi,
45561dc92851SDaniel Borkmann 					     "Not a VAR kind member");
45571dc92851SDaniel Borkmann 			return -EINVAL;
45581dc92851SDaniel Borkmann 		}
45591dc92851SDaniel Borkmann 
45601dc92851SDaniel Borkmann 		if (!env_type_is_resolve_sink(env, var_type) &&
45611dc92851SDaniel Borkmann 		    !env_type_is_resolved(env, var_type_id)) {
45621dc92851SDaniel Borkmann 			env_stack_set_next_member(env, i + 1);
45631dc92851SDaniel Borkmann 			return env_stack_push(env, var_type, var_type_id);
45641dc92851SDaniel Borkmann 		}
45651dc92851SDaniel Borkmann 
45661dc92851SDaniel Borkmann 		type_id = var_type->type;
45671dc92851SDaniel Borkmann 		if (!btf_type_id_size(btf, &type_id, &type_size)) {
45681dc92851SDaniel Borkmann 			btf_verifier_log_vsi(env, v->t, vsi, "Invalid type");
45691dc92851SDaniel Borkmann 			return -EINVAL;
45701dc92851SDaniel Borkmann 		}
45711dc92851SDaniel Borkmann 
45721dc92851SDaniel Borkmann 		if (vsi->size < type_size) {
45731dc92851SDaniel Borkmann 			btf_verifier_log_vsi(env, v->t, vsi, "Invalid size");
45741dc92851SDaniel Borkmann 			return -EINVAL;
45751dc92851SDaniel Borkmann 		}
45761dc92851SDaniel Borkmann 	}
45771dc92851SDaniel Borkmann 
45781dc92851SDaniel Borkmann 	env_stack_pop_resolved(env, 0, 0);
45791dc92851SDaniel Borkmann 	return 0;
45801dc92851SDaniel Borkmann }
45811dc92851SDaniel Borkmann 
btf_datasec_log(struct btf_verifier_env * env,const struct btf_type * t)45821dc92851SDaniel Borkmann static void btf_datasec_log(struct btf_verifier_env *env,
45831dc92851SDaniel Borkmann 			    const struct btf_type *t)
45841dc92851SDaniel Borkmann {
45851dc92851SDaniel Borkmann 	btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t));
45861dc92851SDaniel Borkmann }
45871dc92851SDaniel Borkmann 
btf_datasec_show(const struct btf * btf,const struct btf_type * t,u32 type_id,void * data,u8 bits_offset,struct btf_show * show)458831d0bc81SAlan Maguire static void btf_datasec_show(const struct btf *btf,
45891dc92851SDaniel Borkmann 			     const struct btf_type *t, u32 type_id,
45901dc92851SDaniel Borkmann 			     void *data, u8 bits_offset,
459131d0bc81SAlan Maguire 			     struct btf_show *show)
45921dc92851SDaniel Borkmann {
45931dc92851SDaniel Borkmann 	const struct btf_var_secinfo *vsi;
45941dc92851SDaniel Borkmann 	const struct btf_type *var;
45951dc92851SDaniel Borkmann 	u32 i;
45961dc92851SDaniel Borkmann 
459731d0bc81SAlan Maguire 	if (!btf_show_start_type(show, t, type_id, data))
459831d0bc81SAlan Maguire 		return;
459931d0bc81SAlan Maguire 
460031d0bc81SAlan Maguire 	btf_show_type_value(show, "section (\"%s\") = {",
460131d0bc81SAlan Maguire 			    __btf_name_by_offset(btf, t->name_off));
46021dc92851SDaniel Borkmann 	for_each_vsi(i, t, vsi) {
46031dc92851SDaniel Borkmann 		var = btf_type_by_id(btf, vsi->type);
46041dc92851SDaniel Borkmann 		if (i)
460531d0bc81SAlan Maguire 			btf_show(show, ",");
460631d0bc81SAlan Maguire 		btf_type_ops(var)->show(btf, var, vsi->type,
460731d0bc81SAlan Maguire 					data + vsi->offset, bits_offset, show);
46081dc92851SDaniel Borkmann 	}
460931d0bc81SAlan Maguire 	btf_show_end_type(show);
46101dc92851SDaniel Borkmann }
46111dc92851SDaniel Borkmann 
46121dc92851SDaniel Borkmann static const struct btf_kind_operations datasec_ops = {
46131dc92851SDaniel Borkmann 	.check_meta		= btf_datasec_check_meta,
46141dc92851SDaniel Borkmann 	.resolve		= btf_datasec_resolve,
46151dc92851SDaniel Borkmann 	.check_member		= btf_df_check_member,
46161dc92851SDaniel Borkmann 	.check_kflag_member	= btf_df_check_kflag_member,
46171dc92851SDaniel Borkmann 	.log_details		= btf_datasec_log,
461831d0bc81SAlan Maguire 	.show			= btf_datasec_show,
46191dc92851SDaniel Borkmann };
46201dc92851SDaniel Borkmann 
btf_float_check_meta(struct btf_verifier_env * env,const struct btf_type * t,u32 meta_left)4621b1828f0bSIlya Leoshkevich static s32 btf_float_check_meta(struct btf_verifier_env *env,
4622b1828f0bSIlya Leoshkevich 				const struct btf_type *t,
4623b1828f0bSIlya Leoshkevich 				u32 meta_left)
4624b1828f0bSIlya Leoshkevich {
4625b1828f0bSIlya Leoshkevich 	if (btf_type_vlen(t)) {
4626b1828f0bSIlya Leoshkevich 		btf_verifier_log_type(env, t, "vlen != 0");
4627b1828f0bSIlya Leoshkevich 		return -EINVAL;
4628b1828f0bSIlya Leoshkevich 	}
4629b1828f0bSIlya Leoshkevich 
4630b1828f0bSIlya Leoshkevich 	if (btf_type_kflag(t)) {
4631b1828f0bSIlya Leoshkevich 		btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
4632b1828f0bSIlya Leoshkevich 		return -EINVAL;
4633b1828f0bSIlya Leoshkevich 	}
4634b1828f0bSIlya Leoshkevich 
4635b1828f0bSIlya Leoshkevich 	if (t->size != 2 && t->size != 4 && t->size != 8 && t->size != 12 &&
4636b1828f0bSIlya Leoshkevich 	    t->size != 16) {
4637b1828f0bSIlya Leoshkevich 		btf_verifier_log_type(env, t, "Invalid type_size");
4638b1828f0bSIlya Leoshkevich 		return -EINVAL;
4639b1828f0bSIlya Leoshkevich 	}
4640b1828f0bSIlya Leoshkevich 
4641b1828f0bSIlya Leoshkevich 	btf_verifier_log_type(env, t, NULL);
4642b1828f0bSIlya Leoshkevich 
4643b1828f0bSIlya Leoshkevich 	return 0;
4644b1828f0bSIlya Leoshkevich }
4645b1828f0bSIlya Leoshkevich 
btf_float_check_member(struct btf_verifier_env * env,const struct btf_type * struct_type,const struct btf_member * member,const struct btf_type * member_type)4646b1828f0bSIlya Leoshkevich static int btf_float_check_member(struct btf_verifier_env *env,
4647b1828f0bSIlya Leoshkevich 				  const struct btf_type *struct_type,
4648b1828f0bSIlya Leoshkevich 				  const struct btf_member *member,
4649b1828f0bSIlya Leoshkevich 				  const struct btf_type *member_type)
4650b1828f0bSIlya Leoshkevich {
4651b1828f0bSIlya Leoshkevich 	u64 start_offset_bytes;
4652b1828f0bSIlya Leoshkevich 	u64 end_offset_bytes;
4653b1828f0bSIlya Leoshkevich 	u64 misalign_bits;
4654b1828f0bSIlya Leoshkevich 	u64 align_bytes;
4655b1828f0bSIlya Leoshkevich 	u64 align_bits;
4656b1828f0bSIlya Leoshkevich 
4657b1828f0bSIlya Leoshkevich 	/* Different architectures have different alignment requirements, so
4658b1828f0bSIlya Leoshkevich 	 * here we check only for the reasonable minimum. This way we ensure
4659b1828f0bSIlya Leoshkevich 	 * that types after CO-RE can pass the kernel BTF verifier.
4660b1828f0bSIlya Leoshkevich 	 */
4661b1828f0bSIlya Leoshkevich 	align_bytes = min_t(u64, sizeof(void *), member_type->size);
4662b1828f0bSIlya Leoshkevich 	align_bits = align_bytes * BITS_PER_BYTE;
4663b1828f0bSIlya Leoshkevich 	div64_u64_rem(member->offset, align_bits, &misalign_bits);
4664b1828f0bSIlya Leoshkevich 	if (misalign_bits) {
4665b1828f0bSIlya Leoshkevich 		btf_verifier_log_member(env, struct_type, member,
4666b1828f0bSIlya Leoshkevich 					"Member is not properly aligned");
4667b1828f0bSIlya Leoshkevich 		return -EINVAL;
4668b1828f0bSIlya Leoshkevich 	}
4669b1828f0bSIlya Leoshkevich 
4670b1828f0bSIlya Leoshkevich 	start_offset_bytes = member->offset / BITS_PER_BYTE;
4671b1828f0bSIlya Leoshkevich 	end_offset_bytes = start_offset_bytes + member_type->size;
4672b1828f0bSIlya Leoshkevich 	if (end_offset_bytes > struct_type->size) {
4673b1828f0bSIlya Leoshkevich 		btf_verifier_log_member(env, struct_type, member,
4674b1828f0bSIlya Leoshkevich 					"Member exceeds struct_size");
4675b1828f0bSIlya Leoshkevich 		return -EINVAL;
4676b1828f0bSIlya Leoshkevich 	}
4677b1828f0bSIlya Leoshkevich 
4678b1828f0bSIlya Leoshkevich 	return 0;
4679b1828f0bSIlya Leoshkevich }
4680b1828f0bSIlya Leoshkevich 
btf_float_log(struct btf_verifier_env * env,const struct btf_type * t)4681b1828f0bSIlya Leoshkevich static void btf_float_log(struct btf_verifier_env *env,
4682b1828f0bSIlya Leoshkevich 			  const struct btf_type *t)
4683b1828f0bSIlya Leoshkevich {
4684b1828f0bSIlya Leoshkevich 	btf_verifier_log(env, "size=%u", t->size);
4685b1828f0bSIlya Leoshkevich }
4686b1828f0bSIlya Leoshkevich 
4687b1828f0bSIlya Leoshkevich static const struct btf_kind_operations float_ops = {
4688b1828f0bSIlya Leoshkevich 	.check_meta = btf_float_check_meta,
4689b1828f0bSIlya Leoshkevich 	.resolve = btf_df_resolve,
4690b1828f0bSIlya Leoshkevich 	.check_member = btf_float_check_member,
4691b1828f0bSIlya Leoshkevich 	.check_kflag_member = btf_generic_check_kflag_member,
4692b1828f0bSIlya Leoshkevich 	.log_details = btf_float_log,
4693b1828f0bSIlya Leoshkevich 	.show = btf_df_show,
4694b1828f0bSIlya Leoshkevich };
4695b1828f0bSIlya Leoshkevich 
btf_decl_tag_check_meta(struct btf_verifier_env * env,const struct btf_type * t,u32 meta_left)4696223f903eSYonghong Song static s32 btf_decl_tag_check_meta(struct btf_verifier_env *env,
4697b5ea834dSYonghong Song 			      const struct btf_type *t,
4698b5ea834dSYonghong Song 			      u32 meta_left)
4699b5ea834dSYonghong Song {
4700223f903eSYonghong Song 	const struct btf_decl_tag *tag;
4701b5ea834dSYonghong Song 	u32 meta_needed = sizeof(*tag);
4702b5ea834dSYonghong Song 	s32 component_idx;
4703b5ea834dSYonghong Song 	const char *value;
4704b5ea834dSYonghong Song 
4705b5ea834dSYonghong Song 	if (meta_left < meta_needed) {
4706b5ea834dSYonghong Song 		btf_verifier_log_basic(env, t,
4707b5ea834dSYonghong Song 				       "meta_left:%u meta_needed:%u",
4708b5ea834dSYonghong Song 				       meta_left, meta_needed);
4709b5ea834dSYonghong Song 		return -EINVAL;
4710b5ea834dSYonghong Song 	}
4711b5ea834dSYonghong Song 
4712b5ea834dSYonghong Song 	value = btf_name_by_offset(env->btf, t->name_off);
4713b5ea834dSYonghong Song 	if (!value || !value[0]) {
4714b5ea834dSYonghong Song 		btf_verifier_log_type(env, t, "Invalid value");
4715b5ea834dSYonghong Song 		return -EINVAL;
4716b5ea834dSYonghong Song 	}
4717b5ea834dSYonghong Song 
4718b5ea834dSYonghong Song 	if (btf_type_vlen(t)) {
4719b5ea834dSYonghong Song 		btf_verifier_log_type(env, t, "vlen != 0");
4720b5ea834dSYonghong Song 		return -EINVAL;
4721b5ea834dSYonghong Song 	}
4722b5ea834dSYonghong Song 
4723b5ea834dSYonghong Song 	if (btf_type_kflag(t)) {
4724b5ea834dSYonghong Song 		btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
4725b5ea834dSYonghong Song 		return -EINVAL;
4726b5ea834dSYonghong Song 	}
4727b5ea834dSYonghong Song 
4728223f903eSYonghong Song 	component_idx = btf_type_decl_tag(t)->component_idx;
4729b5ea834dSYonghong Song 	if (component_idx < -1) {
4730b5ea834dSYonghong Song 		btf_verifier_log_type(env, t, "Invalid component_idx");
4731b5ea834dSYonghong Song 		return -EINVAL;
4732b5ea834dSYonghong Song 	}
4733b5ea834dSYonghong Song 
4734b5ea834dSYonghong Song 	btf_verifier_log_type(env, t, NULL);
4735b5ea834dSYonghong Song 
4736b5ea834dSYonghong Song 	return meta_needed;
4737b5ea834dSYonghong Song }
4738b5ea834dSYonghong Song 
btf_decl_tag_resolve(struct btf_verifier_env * env,const struct resolve_vertex * v)4739223f903eSYonghong Song static int btf_decl_tag_resolve(struct btf_verifier_env *env,
4740b5ea834dSYonghong Song 			   const struct resolve_vertex *v)
4741b5ea834dSYonghong Song {
4742b5ea834dSYonghong Song 	const struct btf_type *next_type;
4743b5ea834dSYonghong Song 	const struct btf_type *t = v->t;
4744b5ea834dSYonghong Song 	u32 next_type_id = t->type;
4745b5ea834dSYonghong Song 	struct btf *btf = env->btf;
4746b5ea834dSYonghong Song 	s32 component_idx;
4747b5ea834dSYonghong Song 	u32 vlen;
4748b5ea834dSYonghong Song 
4749b5ea834dSYonghong Song 	next_type = btf_type_by_id(btf, next_type_id);
4750223f903eSYonghong Song 	if (!next_type || !btf_type_is_decl_tag_target(next_type)) {
4751b5ea834dSYonghong Song 		btf_verifier_log_type(env, v->t, "Invalid type_id");
4752b5ea834dSYonghong Song 		return -EINVAL;
4753b5ea834dSYonghong Song 	}
4754b5ea834dSYonghong Song 
4755b5ea834dSYonghong Song 	if (!env_type_is_resolve_sink(env, next_type) &&
4756b5ea834dSYonghong Song 	    !env_type_is_resolved(env, next_type_id))
4757b5ea834dSYonghong Song 		return env_stack_push(env, next_type, next_type_id);
4758b5ea834dSYonghong Song 
4759223f903eSYonghong Song 	component_idx = btf_type_decl_tag(t)->component_idx;
4760b5ea834dSYonghong Song 	if (component_idx != -1) {
4761bd16dee6SYonghong Song 		if (btf_type_is_var(next_type) || btf_type_is_typedef(next_type)) {
4762b5ea834dSYonghong Song 			btf_verifier_log_type(env, v->t, "Invalid component_idx");
4763b5ea834dSYonghong Song 			return -EINVAL;
4764b5ea834dSYonghong Song 		}
4765b5ea834dSYonghong Song 
4766b5ea834dSYonghong Song 		if (btf_type_is_struct(next_type)) {
4767b5ea834dSYonghong Song 			vlen = btf_type_vlen(next_type);
4768b5ea834dSYonghong Song 		} else {
4769b5ea834dSYonghong Song 			/* next_type should be a function */
4770b5ea834dSYonghong Song 			next_type = btf_type_by_id(btf, next_type->type);
4771b5ea834dSYonghong Song 			vlen = btf_type_vlen(next_type);
4772b5ea834dSYonghong Song 		}
4773b5ea834dSYonghong Song 
4774b5ea834dSYonghong Song 		if ((u32)component_idx >= vlen) {
4775b5ea834dSYonghong Song 			btf_verifier_log_type(env, v->t, "Invalid component_idx");
4776b5ea834dSYonghong Song 			return -EINVAL;
4777b5ea834dSYonghong Song 		}
4778b5ea834dSYonghong Song 	}
4779b5ea834dSYonghong Song 
4780b5ea834dSYonghong Song 	env_stack_pop_resolved(env, next_type_id, 0);
4781b5ea834dSYonghong Song 
4782b5ea834dSYonghong Song 	return 0;
4783b5ea834dSYonghong Song }
4784b5ea834dSYonghong Song 
btf_decl_tag_log(struct btf_verifier_env * env,const struct btf_type * t)4785223f903eSYonghong Song static void btf_decl_tag_log(struct btf_verifier_env *env, const struct btf_type *t)
4786b5ea834dSYonghong Song {
4787b5ea834dSYonghong Song 	btf_verifier_log(env, "type=%u component_idx=%d", t->type,
4788223f903eSYonghong Song 			 btf_type_decl_tag(t)->component_idx);
4789b5ea834dSYonghong Song }
4790b5ea834dSYonghong Song 
4791223f903eSYonghong Song static const struct btf_kind_operations decl_tag_ops = {
4792223f903eSYonghong Song 	.check_meta = btf_decl_tag_check_meta,
4793223f903eSYonghong Song 	.resolve = btf_decl_tag_resolve,
4794b5ea834dSYonghong Song 	.check_member = btf_df_check_member,
4795b5ea834dSYonghong Song 	.check_kflag_member = btf_df_check_kflag_member,
4796223f903eSYonghong Song 	.log_details = btf_decl_tag_log,
4797b5ea834dSYonghong Song 	.show = btf_df_show,
4798b5ea834dSYonghong Song };
4799b5ea834dSYonghong Song 
btf_func_proto_check(struct btf_verifier_env * env,const struct btf_type * t)48002667a262SMartin KaFai Lau static int btf_func_proto_check(struct btf_verifier_env *env,
48012667a262SMartin KaFai Lau 				const struct btf_type *t)
48022667a262SMartin KaFai Lau {
48032667a262SMartin KaFai Lau 	const struct btf_type *ret_type;
48042667a262SMartin KaFai Lau 	const struct btf_param *args;
48052667a262SMartin KaFai Lau 	const struct btf *btf;
48062667a262SMartin KaFai Lau 	u16 nr_args, i;
48072667a262SMartin KaFai Lau 	int err;
48082667a262SMartin KaFai Lau 
48092667a262SMartin KaFai Lau 	btf = env->btf;
48102667a262SMartin KaFai Lau 	args = (const struct btf_param *)(t + 1);
48112667a262SMartin KaFai Lau 	nr_args = btf_type_vlen(t);
48122667a262SMartin KaFai Lau 
48132667a262SMartin KaFai Lau 	/* Check func return type which could be "void" (t->type == 0) */
48142667a262SMartin KaFai Lau 	if (t->type) {
48152667a262SMartin KaFai Lau 		u32 ret_type_id = t->type;
48162667a262SMartin KaFai Lau 
48172667a262SMartin KaFai Lau 		ret_type = btf_type_by_id(btf, ret_type_id);
48182667a262SMartin KaFai Lau 		if (!ret_type) {
48192667a262SMartin KaFai Lau 			btf_verifier_log_type(env, t, "Invalid return type");
48202667a262SMartin KaFai Lau 			return -EINVAL;
48212667a262SMartin KaFai Lau 		}
48222667a262SMartin KaFai Lau 
4823ea68376cSStanislav Fomichev 		if (btf_type_is_resolve_source_only(ret_type)) {
4824ea68376cSStanislav Fomichev 			btf_verifier_log_type(env, t, "Invalid return type");
4825ea68376cSStanislav Fomichev 			return -EINVAL;
4826ea68376cSStanislav Fomichev 		}
4827ea68376cSStanislav Fomichev 
48282667a262SMartin KaFai Lau 		if (btf_type_needs_resolve(ret_type) &&
48292667a262SMartin KaFai Lau 		    !env_type_is_resolved(env, ret_type_id)) {
48302667a262SMartin KaFai Lau 			err = btf_resolve(env, ret_type, ret_type_id);
48312667a262SMartin KaFai Lau 			if (err)
48322667a262SMartin KaFai Lau 				return err;
48332667a262SMartin KaFai Lau 		}
48342667a262SMartin KaFai Lau 
48352667a262SMartin KaFai Lau 		/* Ensure the return type is a type that has a size */
48362667a262SMartin KaFai Lau 		if (!btf_type_id_size(btf, &ret_type_id, NULL)) {
48372667a262SMartin KaFai Lau 			btf_verifier_log_type(env, t, "Invalid return type");
48382667a262SMartin KaFai Lau 			return -EINVAL;
48392667a262SMartin KaFai Lau 		}
48402667a262SMartin KaFai Lau 	}
48412667a262SMartin KaFai Lau 
48422667a262SMartin KaFai Lau 	if (!nr_args)
48432667a262SMartin KaFai Lau 		return 0;
48442667a262SMartin KaFai Lau 
48452667a262SMartin KaFai Lau 	/* Last func arg type_id could be 0 if it is a vararg */
48462667a262SMartin KaFai Lau 	if (!args[nr_args - 1].type) {
48472667a262SMartin KaFai Lau 		if (args[nr_args - 1].name_off) {
48482667a262SMartin KaFai Lau 			btf_verifier_log_type(env, t, "Invalid arg#%u",
48492667a262SMartin KaFai Lau 					      nr_args);
48502667a262SMartin KaFai Lau 			return -EINVAL;
48512667a262SMartin KaFai Lau 		}
48522667a262SMartin KaFai Lau 		nr_args--;
48532667a262SMartin KaFai Lau 	}
48542667a262SMartin KaFai Lau 
48552667a262SMartin KaFai Lau 	for (i = 0; i < nr_args; i++) {
48562667a262SMartin KaFai Lau 		const struct btf_type *arg_type;
48572667a262SMartin KaFai Lau 		u32 arg_type_id;
48582667a262SMartin KaFai Lau 
48592667a262SMartin KaFai Lau 		arg_type_id = args[i].type;
48602667a262SMartin KaFai Lau 		arg_type = btf_type_by_id(btf, arg_type_id);
48612667a262SMartin KaFai Lau 		if (!arg_type) {
48622667a262SMartin KaFai Lau 			btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1);
48635bad3587SStanislav Fomichev 			return -EINVAL;
48642667a262SMartin KaFai Lau 		}
48652667a262SMartin KaFai Lau 
4866f17472d4SStanislav Fomichev 		if (btf_type_is_resolve_source_only(arg_type)) {
4867f17472d4SStanislav Fomichev 			btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1);
4868f17472d4SStanislav Fomichev 			return -EINVAL;
4869f17472d4SStanislav Fomichev 		}
4870f17472d4SStanislav Fomichev 
48712667a262SMartin KaFai Lau 		if (args[i].name_off &&
48722667a262SMartin KaFai Lau 		    (!btf_name_offset_valid(btf, args[i].name_off) ||
48732667a262SMartin KaFai Lau 		     !btf_name_valid_identifier(btf, args[i].name_off))) {
48742667a262SMartin KaFai Lau 			btf_verifier_log_type(env, t,
48752667a262SMartin KaFai Lau 					      "Invalid arg#%u", i + 1);
48765bad3587SStanislav Fomichev 			return -EINVAL;
48772667a262SMartin KaFai Lau 		}
48782667a262SMartin KaFai Lau 
48792667a262SMartin KaFai Lau 		if (btf_type_needs_resolve(arg_type) &&
48802667a262SMartin KaFai Lau 		    !env_type_is_resolved(env, arg_type_id)) {
48812667a262SMartin KaFai Lau 			err = btf_resolve(env, arg_type, arg_type_id);
48822667a262SMartin KaFai Lau 			if (err)
48835bad3587SStanislav Fomichev 				return err;
48842667a262SMartin KaFai Lau 		}
48852667a262SMartin KaFai Lau 
48862667a262SMartin KaFai Lau 		if (!btf_type_id_size(btf, &arg_type_id, NULL)) {
48872667a262SMartin KaFai Lau 			btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1);
48885bad3587SStanislav Fomichev 			return -EINVAL;
48892667a262SMartin KaFai Lau 		}
48902667a262SMartin KaFai Lau 	}
48912667a262SMartin KaFai Lau 
48925bad3587SStanislav Fomichev 	return 0;
48932667a262SMartin KaFai Lau }
48942667a262SMartin KaFai Lau 
btf_func_check(struct btf_verifier_env * env,const struct btf_type * t)48952667a262SMartin KaFai Lau static int btf_func_check(struct btf_verifier_env *env,
48962667a262SMartin KaFai Lau 			  const struct btf_type *t)
48972667a262SMartin KaFai Lau {
48982667a262SMartin KaFai Lau 	const struct btf_type *proto_type;
48992667a262SMartin KaFai Lau 	const struct btf_param *args;
49002667a262SMartin KaFai Lau 	const struct btf *btf;
49012667a262SMartin KaFai Lau 	u16 nr_args, i;
49022667a262SMartin KaFai Lau 
49032667a262SMartin KaFai Lau 	btf = env->btf;
49042667a262SMartin KaFai Lau 	proto_type = btf_type_by_id(btf, t->type);
49052667a262SMartin KaFai Lau 
49062667a262SMartin KaFai Lau 	if (!proto_type || !btf_type_is_func_proto(proto_type)) {
49072667a262SMartin KaFai Lau 		btf_verifier_log_type(env, t, "Invalid type_id");
49082667a262SMartin KaFai Lau 		return -EINVAL;
49092667a262SMartin KaFai Lau 	}
49102667a262SMartin KaFai Lau 
49112667a262SMartin KaFai Lau 	args = (const struct btf_param *)(proto_type + 1);
49122667a262SMartin KaFai Lau 	nr_args = btf_type_vlen(proto_type);
49132667a262SMartin KaFai Lau 	for (i = 0; i < nr_args; i++) {
49142667a262SMartin KaFai Lau 		if (!args[i].name_off && args[i].type) {
49152667a262SMartin KaFai Lau 			btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1);
49162667a262SMartin KaFai Lau 			return -EINVAL;
49172667a262SMartin KaFai Lau 		}
49182667a262SMartin KaFai Lau 	}
49192667a262SMartin KaFai Lau 
49202667a262SMartin KaFai Lau 	return 0;
49212667a262SMartin KaFai Lau }
49222667a262SMartin KaFai Lau 
492369b693f0SMartin KaFai Lau static const struct btf_kind_operations * const kind_ops[NR_BTF_KINDS] = {
492469b693f0SMartin KaFai Lau 	[BTF_KIND_INT] = &int_ops,
492569b693f0SMartin KaFai Lau 	[BTF_KIND_PTR] = &ptr_ops,
492669b693f0SMartin KaFai Lau 	[BTF_KIND_ARRAY] = &array_ops,
492769b693f0SMartin KaFai Lau 	[BTF_KIND_STRUCT] = &struct_ops,
492869b693f0SMartin KaFai Lau 	[BTF_KIND_UNION] = &struct_ops,
492969b693f0SMartin KaFai Lau 	[BTF_KIND_ENUM] = &enum_ops,
493069b693f0SMartin KaFai Lau 	[BTF_KIND_FWD] = &fwd_ops,
493169b693f0SMartin KaFai Lau 	[BTF_KIND_TYPEDEF] = &modifier_ops,
493269b693f0SMartin KaFai Lau 	[BTF_KIND_VOLATILE] = &modifier_ops,
493369b693f0SMartin KaFai Lau 	[BTF_KIND_CONST] = &modifier_ops,
493469b693f0SMartin KaFai Lau 	[BTF_KIND_RESTRICT] = &modifier_ops,
49352667a262SMartin KaFai Lau 	[BTF_KIND_FUNC] = &func_ops,
49362667a262SMartin KaFai Lau 	[BTF_KIND_FUNC_PROTO] = &func_proto_ops,
49371dc92851SDaniel Borkmann 	[BTF_KIND_VAR] = &var_ops,
49381dc92851SDaniel Borkmann 	[BTF_KIND_DATASEC] = &datasec_ops,
4939b1828f0bSIlya Leoshkevich 	[BTF_KIND_FLOAT] = &float_ops,
4940223f903eSYonghong Song 	[BTF_KIND_DECL_TAG] = &decl_tag_ops,
49418c42d2faSYonghong Song 	[BTF_KIND_TYPE_TAG] = &modifier_ops,
49426089fb32SYonghong Song 	[BTF_KIND_ENUM64] = &enum64_ops,
494369b693f0SMartin KaFai Lau };
494469b693f0SMartin KaFai Lau 
btf_check_meta(struct btf_verifier_env * env,const struct btf_type * t,u32 meta_left)494569b693f0SMartin KaFai Lau static s32 btf_check_meta(struct btf_verifier_env *env,
494669b693f0SMartin KaFai Lau 			  const struct btf_type *t,
494769b693f0SMartin KaFai Lau 			  u32 meta_left)
494869b693f0SMartin KaFai Lau {
494969b693f0SMartin KaFai Lau 	u32 saved_meta_left = meta_left;
495069b693f0SMartin KaFai Lau 	s32 var_meta_size;
495169b693f0SMartin KaFai Lau 
495269b693f0SMartin KaFai Lau 	if (meta_left < sizeof(*t)) {
495369b693f0SMartin KaFai Lau 		btf_verifier_log(env, "[%u] meta_left:%u meta_needed:%zu",
495469b693f0SMartin KaFai Lau 				 env->log_type_id, meta_left, sizeof(*t));
495569b693f0SMartin KaFai Lau 		return -EINVAL;
495669b693f0SMartin KaFai Lau 	}
495769b693f0SMartin KaFai Lau 	meta_left -= sizeof(*t);
495869b693f0SMartin KaFai Lau 
4959aea2f7b8SMartin KaFai Lau 	if (t->info & ~BTF_INFO_MASK) {
4960aea2f7b8SMartin KaFai Lau 		btf_verifier_log(env, "[%u] Invalid btf_info:%x",
4961aea2f7b8SMartin KaFai Lau 				 env->log_type_id, t->info);
4962aea2f7b8SMartin KaFai Lau 		return -EINVAL;
4963aea2f7b8SMartin KaFai Lau 	}
4964aea2f7b8SMartin KaFai Lau 
496569b693f0SMartin KaFai Lau 	if (BTF_INFO_KIND(t->info) > BTF_KIND_MAX ||
496669b693f0SMartin KaFai Lau 	    BTF_INFO_KIND(t->info) == BTF_KIND_UNKN) {
496769b693f0SMartin KaFai Lau 		btf_verifier_log(env, "[%u] Invalid kind:%u",
496869b693f0SMartin KaFai Lau 				 env->log_type_id, BTF_INFO_KIND(t->info));
496969b693f0SMartin KaFai Lau 		return -EINVAL;
497069b693f0SMartin KaFai Lau 	}
497169b693f0SMartin KaFai Lau 
4972fbcf93ebSMartin KaFai Lau 	if (!btf_name_offset_valid(env->btf, t->name_off)) {
497369b693f0SMartin KaFai Lau 		btf_verifier_log(env, "[%u] Invalid name_offset:%u",
4974fbcf93ebSMartin KaFai Lau 				 env->log_type_id, t->name_off);
497569b693f0SMartin KaFai Lau 		return -EINVAL;
497669b693f0SMartin KaFai Lau 	}
497769b693f0SMartin KaFai Lau 
497869b693f0SMartin KaFai Lau 	var_meta_size = btf_type_ops(t)->check_meta(env, t, meta_left);
497969b693f0SMartin KaFai Lau 	if (var_meta_size < 0)
498069b693f0SMartin KaFai Lau 		return var_meta_size;
498169b693f0SMartin KaFai Lau 
498269b693f0SMartin KaFai Lau 	meta_left -= var_meta_size;
498369b693f0SMartin KaFai Lau 
498469b693f0SMartin KaFai Lau 	return saved_meta_left - meta_left;
498569b693f0SMartin KaFai Lau }
498669b693f0SMartin KaFai Lau 
btf_check_all_metas(struct btf_verifier_env * env)498769b693f0SMartin KaFai Lau static int btf_check_all_metas(struct btf_verifier_env *env)
498869b693f0SMartin KaFai Lau {
498969b693f0SMartin KaFai Lau 	struct btf *btf = env->btf;
499069b693f0SMartin KaFai Lau 	struct btf_header *hdr;
499169b693f0SMartin KaFai Lau 	void *cur, *end;
499269b693f0SMartin KaFai Lau 
4993f80442a4SMartin KaFai Lau 	hdr = &btf->hdr;
499469b693f0SMartin KaFai Lau 	cur = btf->nohdr_data + hdr->type_off;
49954b1c5d91SMartin KaFai Lau 	end = cur + hdr->type_len;
499669b693f0SMartin KaFai Lau 
4997951bb646SAndrii Nakryiko 	env->log_type_id = btf->base_btf ? btf->start_id : 1;
499869b693f0SMartin KaFai Lau 	while (cur < end) {
499969b693f0SMartin KaFai Lau 		struct btf_type *t = cur;
500069b693f0SMartin KaFai Lau 		s32 meta_size;
500169b693f0SMartin KaFai Lau 
500269b693f0SMartin KaFai Lau 		meta_size = btf_check_meta(env, t, end - cur);
500369b693f0SMartin KaFai Lau 		if (meta_size < 0)
500469b693f0SMartin KaFai Lau 			return meta_size;
500569b693f0SMartin KaFai Lau 
500669b693f0SMartin KaFai Lau 		btf_add_type(env, t);
500769b693f0SMartin KaFai Lau 		cur += meta_size;
500869b693f0SMartin KaFai Lau 		env->log_type_id++;
500969b693f0SMartin KaFai Lau 	}
501069b693f0SMartin KaFai Lau 
501169b693f0SMartin KaFai Lau 	return 0;
501269b693f0SMartin KaFai Lau }
501369b693f0SMartin KaFai Lau 
btf_resolve_valid(struct btf_verifier_env * env,const struct btf_type * t,u32 type_id)5014eb3f595dSMartin KaFai Lau static bool btf_resolve_valid(struct btf_verifier_env *env,
5015eb3f595dSMartin KaFai Lau 			      const struct btf_type *t,
5016eb3f595dSMartin KaFai Lau 			      u32 type_id)
5017eb3f595dSMartin KaFai Lau {
5018eb3f595dSMartin KaFai Lau 	struct btf *btf = env->btf;
5019eb3f595dSMartin KaFai Lau 
5020eb3f595dSMartin KaFai Lau 	if (!env_type_is_resolved(env, type_id))
5021eb3f595dSMartin KaFai Lau 		return false;
5022eb3f595dSMartin KaFai Lau 
50231dc92851SDaniel Borkmann 	if (btf_type_is_struct(t) || btf_type_is_datasec(t))
5024951bb646SAndrii Nakryiko 		return !btf_resolved_type_id(btf, type_id) &&
5025951bb646SAndrii Nakryiko 		       !btf_resolved_type_size(btf, type_id);
5026eb3f595dSMartin KaFai Lau 
5027d7e7b42fSYonghong Song 	if (btf_type_is_decl_tag(t) || btf_type_is_func(t))
5028b5ea834dSYonghong Song 		return btf_resolved_type_id(btf, type_id) &&
5029b5ea834dSYonghong Song 		       !btf_resolved_type_size(btf, type_id);
5030b5ea834dSYonghong Song 
50311dc92851SDaniel Borkmann 	if (btf_type_is_modifier(t) || btf_type_is_ptr(t) ||
50321dc92851SDaniel Borkmann 	    btf_type_is_var(t)) {
5033eb3f595dSMartin KaFai Lau 		t = btf_type_id_resolve(btf, &type_id);
50341dc92851SDaniel Borkmann 		return t &&
50351dc92851SDaniel Borkmann 		       !btf_type_is_modifier(t) &&
50361dc92851SDaniel Borkmann 		       !btf_type_is_var(t) &&
50371dc92851SDaniel Borkmann 		       !btf_type_is_datasec(t);
5038eb3f595dSMartin KaFai Lau 	}
5039eb3f595dSMartin KaFai Lau 
5040eb3f595dSMartin KaFai Lau 	if (btf_type_is_array(t)) {
5041eb3f595dSMartin KaFai Lau 		const struct btf_array *array = btf_type_array(t);
5042eb3f595dSMartin KaFai Lau 		const struct btf_type *elem_type;
5043eb3f595dSMartin KaFai Lau 		u32 elem_type_id = array->type;
5044eb3f595dSMartin KaFai Lau 		u32 elem_size;
5045eb3f595dSMartin KaFai Lau 
5046eb3f595dSMartin KaFai Lau 		elem_type = btf_type_id_size(btf, &elem_type_id, &elem_size);
5047eb3f595dSMartin KaFai Lau 		return elem_type && !btf_type_is_modifier(elem_type) &&
5048eb3f595dSMartin KaFai Lau 			(array->nelems * elem_size ==
5049951bb646SAndrii Nakryiko 			 btf_resolved_type_size(btf, type_id));
5050eb3f595dSMartin KaFai Lau 	}
5051eb3f595dSMartin KaFai Lau 
5052eb3f595dSMartin KaFai Lau 	return false;
5053eb3f595dSMartin KaFai Lau }
5054eb3f595dSMartin KaFai Lau 
btf_resolve(struct btf_verifier_env * env,const struct btf_type * t,u32 type_id)50552667a262SMartin KaFai Lau static int btf_resolve(struct btf_verifier_env *env,
50562667a262SMartin KaFai Lau 		       const struct btf_type *t, u32 type_id)
50572667a262SMartin KaFai Lau {
50582667a262SMartin KaFai Lau 	u32 save_log_type_id = env->log_type_id;
50592667a262SMartin KaFai Lau 	const struct resolve_vertex *v;
50602667a262SMartin KaFai Lau 	int err = 0;
50612667a262SMartin KaFai Lau 
50622667a262SMartin KaFai Lau 	env->resolve_mode = RESOLVE_TBD;
50632667a262SMartin KaFai Lau 	env_stack_push(env, t, type_id);
50642667a262SMartin KaFai Lau 	while (!err && (v = env_stack_peak(env))) {
50652667a262SMartin KaFai Lau 		env->log_type_id = v->type_id;
50662667a262SMartin KaFai Lau 		err = btf_type_ops(v->t)->resolve(env, v);
50672667a262SMartin KaFai Lau 	}
50682667a262SMartin KaFai Lau 
50692667a262SMartin KaFai Lau 	env->log_type_id = type_id;
50702667a262SMartin KaFai Lau 	if (err == -E2BIG) {
50712667a262SMartin KaFai Lau 		btf_verifier_log_type(env, t,
50722667a262SMartin KaFai Lau 				      "Exceeded max resolving depth:%u",
50732667a262SMartin KaFai Lau 				      MAX_RESOLVE_DEPTH);
50742667a262SMartin KaFai Lau 	} else if (err == -EEXIST) {
50752667a262SMartin KaFai Lau 		btf_verifier_log_type(env, t, "Loop detected");
50762667a262SMartin KaFai Lau 	}
50772667a262SMartin KaFai Lau 
50782667a262SMartin KaFai Lau 	/* Final sanity check */
50792667a262SMartin KaFai Lau 	if (!err && !btf_resolve_valid(env, t, type_id)) {
50802667a262SMartin KaFai Lau 		btf_verifier_log_type(env, t, "Invalid resolve state");
50812667a262SMartin KaFai Lau 		err = -EINVAL;
50822667a262SMartin KaFai Lau 	}
50832667a262SMartin KaFai Lau 
50842667a262SMartin KaFai Lau 	env->log_type_id = save_log_type_id;
50852667a262SMartin KaFai Lau 	return err;
50862667a262SMartin KaFai Lau }
50872667a262SMartin KaFai Lau 
btf_check_all_types(struct btf_verifier_env * env)5088eb3f595dSMartin KaFai Lau static int btf_check_all_types(struct btf_verifier_env *env)
5089eb3f595dSMartin KaFai Lau {
5090eb3f595dSMartin KaFai Lau 	struct btf *btf = env->btf;
5091951bb646SAndrii Nakryiko 	const struct btf_type *t;
5092951bb646SAndrii Nakryiko 	u32 type_id, i;
5093eb3f595dSMartin KaFai Lau 	int err;
5094eb3f595dSMartin KaFai Lau 
5095eb3f595dSMartin KaFai Lau 	err = env_resolve_init(env);
5096eb3f595dSMartin KaFai Lau 	if (err)
5097eb3f595dSMartin KaFai Lau 		return err;
5098eb3f595dSMartin KaFai Lau 
5099eb3f595dSMartin KaFai Lau 	env->phase++;
5100951bb646SAndrii Nakryiko 	for (i = btf->base_btf ? 0 : 1; i < btf->nr_types; i++) {
5101951bb646SAndrii Nakryiko 		type_id = btf->start_id + i;
5102951bb646SAndrii Nakryiko 		t = btf_type_by_id(btf, type_id);
5103eb3f595dSMartin KaFai Lau 
5104eb3f595dSMartin KaFai Lau 		env->log_type_id = type_id;
5105eb3f595dSMartin KaFai Lau 		if (btf_type_needs_resolve(t) &&
5106eb3f595dSMartin KaFai Lau 		    !env_type_is_resolved(env, type_id)) {
5107eb3f595dSMartin KaFai Lau 			err = btf_resolve(env, t, type_id);
5108eb3f595dSMartin KaFai Lau 			if (err)
5109eb3f595dSMartin KaFai Lau 				return err;
5110eb3f595dSMartin KaFai Lau 		}
5111eb3f595dSMartin KaFai Lau 
51122667a262SMartin KaFai Lau 		if (btf_type_is_func_proto(t)) {
51132667a262SMartin KaFai Lau 			err = btf_func_proto_check(env, t);
51142667a262SMartin KaFai Lau 			if (err)
51152667a262SMartin KaFai Lau 				return err;
51162667a262SMartin KaFai Lau 		}
5117eb3f595dSMartin KaFai Lau 	}
5118eb3f595dSMartin KaFai Lau 
5119eb3f595dSMartin KaFai Lau 	return 0;
5120eb3f595dSMartin KaFai Lau }
5121eb3f595dSMartin KaFai Lau 
btf_parse_type_sec(struct btf_verifier_env * env)512269b693f0SMartin KaFai Lau static int btf_parse_type_sec(struct btf_verifier_env *env)
512369b693f0SMartin KaFai Lau {
5124f80442a4SMartin KaFai Lau 	const struct btf_header *hdr = &env->btf->hdr;
5125eb3f595dSMartin KaFai Lau 	int err;
5126eb3f595dSMartin KaFai Lau 
5127f80442a4SMartin KaFai Lau 	/* Type section must align to 4 bytes */
5128f80442a4SMartin KaFai Lau 	if (hdr->type_off & (sizeof(u32) - 1)) {
5129f80442a4SMartin KaFai Lau 		btf_verifier_log(env, "Unaligned type_off");
5130f80442a4SMartin KaFai Lau 		return -EINVAL;
5131f80442a4SMartin KaFai Lau 	}
5132f80442a4SMartin KaFai Lau 
5133951bb646SAndrii Nakryiko 	if (!env->btf->base_btf && !hdr->type_len) {
5134f80442a4SMartin KaFai Lau 		btf_verifier_log(env, "No type found");
5135f80442a4SMartin KaFai Lau 		return -EINVAL;
5136f80442a4SMartin KaFai Lau 	}
5137f80442a4SMartin KaFai Lau 
5138eb3f595dSMartin KaFai Lau 	err = btf_check_all_metas(env);
5139eb3f595dSMartin KaFai Lau 	if (err)
5140eb3f595dSMartin KaFai Lau 		return err;
5141eb3f595dSMartin KaFai Lau 
5142eb3f595dSMartin KaFai Lau 	return btf_check_all_types(env);
514369b693f0SMartin KaFai Lau }
514469b693f0SMartin KaFai Lau 
btf_parse_str_sec(struct btf_verifier_env * env)514569b693f0SMartin KaFai Lau static int btf_parse_str_sec(struct btf_verifier_env *env)
514669b693f0SMartin KaFai Lau {
514769b693f0SMartin KaFai Lau 	const struct btf_header *hdr;
514869b693f0SMartin KaFai Lau 	struct btf *btf = env->btf;
514969b693f0SMartin KaFai Lau 	const char *start, *end;
515069b693f0SMartin KaFai Lau 
5151f80442a4SMartin KaFai Lau 	hdr = &btf->hdr;
515269b693f0SMartin KaFai Lau 	start = btf->nohdr_data + hdr->str_off;
515369b693f0SMartin KaFai Lau 	end = start + hdr->str_len;
515469b693f0SMartin KaFai Lau 
5155f80442a4SMartin KaFai Lau 	if (end != btf->data + btf->data_size) {
5156f80442a4SMartin KaFai Lau 		btf_verifier_log(env, "String section is not at the end");
5157f80442a4SMartin KaFai Lau 		return -EINVAL;
5158f80442a4SMartin KaFai Lau 	}
5159f80442a4SMartin KaFai Lau 
5160951bb646SAndrii Nakryiko 	btf->strings = start;
5161951bb646SAndrii Nakryiko 
5162951bb646SAndrii Nakryiko 	if (btf->base_btf && !hdr->str_len)
5163951bb646SAndrii Nakryiko 		return 0;
5164951bb646SAndrii Nakryiko 	if (!hdr->str_len || hdr->str_len - 1 > BTF_MAX_NAME_OFFSET || end[-1]) {
516569b693f0SMartin KaFai Lau 		btf_verifier_log(env, "Invalid string section");
516669b693f0SMartin KaFai Lau 		return -EINVAL;
516769b693f0SMartin KaFai Lau 	}
5168951bb646SAndrii Nakryiko 	if (!btf->base_btf && start[0]) {
5169951bb646SAndrii Nakryiko 		btf_verifier_log(env, "Invalid string section");
5170951bb646SAndrii Nakryiko 		return -EINVAL;
5171951bb646SAndrii Nakryiko 	}
517269b693f0SMartin KaFai Lau 
517369b693f0SMartin KaFai Lau 	return 0;
517469b693f0SMartin KaFai Lau }
517569b693f0SMartin KaFai Lau 
5176f80442a4SMartin KaFai Lau static const size_t btf_sec_info_offset[] = {
5177f80442a4SMartin KaFai Lau 	offsetof(struct btf_header, type_off),
5178f80442a4SMartin KaFai Lau 	offsetof(struct btf_header, str_off),
5179f80442a4SMartin KaFai Lau };
5180f80442a4SMartin KaFai Lau 
btf_sec_info_cmp(const void * a,const void * b)5181f80442a4SMartin KaFai Lau static int btf_sec_info_cmp(const void *a, const void *b)
5182f80442a4SMartin KaFai Lau {
5183f80442a4SMartin KaFai Lau 	const struct btf_sec_info *x = a;
5184f80442a4SMartin KaFai Lau 	const struct btf_sec_info *y = b;
5185f80442a4SMartin KaFai Lau 
5186f80442a4SMartin KaFai Lau 	return (int)(x->off - y->off) ? : (int)(x->len - y->len);
5187f80442a4SMartin KaFai Lau }
5188f80442a4SMartin KaFai Lau 
btf_check_sec_info(struct btf_verifier_env * env,u32 btf_data_size)5189f80442a4SMartin KaFai Lau static int btf_check_sec_info(struct btf_verifier_env *env,
5190f80442a4SMartin KaFai Lau 			      u32 btf_data_size)
5191f80442a4SMartin KaFai Lau {
5192a2889a4cSMartin KaFai Lau 	struct btf_sec_info secs[ARRAY_SIZE(btf_sec_info_offset)];
5193f80442a4SMartin KaFai Lau 	u32 total, expected_total, i;
5194f80442a4SMartin KaFai Lau 	const struct btf_header *hdr;
5195f80442a4SMartin KaFai Lau 	const struct btf *btf;
5196f80442a4SMartin KaFai Lau 
5197f80442a4SMartin KaFai Lau 	btf = env->btf;
5198f80442a4SMartin KaFai Lau 	hdr = &btf->hdr;
5199f80442a4SMartin KaFai Lau 
5200f80442a4SMartin KaFai Lau 	/* Populate the secs from hdr */
5201a2889a4cSMartin KaFai Lau 	for (i = 0; i < ARRAY_SIZE(btf_sec_info_offset); i++)
5202f80442a4SMartin KaFai Lau 		secs[i] = *(struct btf_sec_info *)((void *)hdr +
5203f80442a4SMartin KaFai Lau 						   btf_sec_info_offset[i]);
5204f80442a4SMartin KaFai Lau 
5205a2889a4cSMartin KaFai Lau 	sort(secs, ARRAY_SIZE(btf_sec_info_offset),
5206a2889a4cSMartin KaFai Lau 	     sizeof(struct btf_sec_info), btf_sec_info_cmp, NULL);
5207f80442a4SMartin KaFai Lau 
5208f80442a4SMartin KaFai Lau 	/* Check for gaps and overlap among sections */
5209f80442a4SMartin KaFai Lau 	total = 0;
5210f80442a4SMartin KaFai Lau 	expected_total = btf_data_size - hdr->hdr_len;
5211a2889a4cSMartin KaFai Lau 	for (i = 0; i < ARRAY_SIZE(btf_sec_info_offset); i++) {
5212f80442a4SMartin KaFai Lau 		if (expected_total < secs[i].off) {
5213f80442a4SMartin KaFai Lau 			btf_verifier_log(env, "Invalid section offset");
5214f80442a4SMartin KaFai Lau 			return -EINVAL;
5215f80442a4SMartin KaFai Lau 		}
5216f80442a4SMartin KaFai Lau 		if (total < secs[i].off) {
5217f80442a4SMartin KaFai Lau 			/* gap */
5218f80442a4SMartin KaFai Lau 			btf_verifier_log(env, "Unsupported section found");
5219f80442a4SMartin KaFai Lau 			return -EINVAL;
5220f80442a4SMartin KaFai Lau 		}
5221f80442a4SMartin KaFai Lau 		if (total > secs[i].off) {
5222f80442a4SMartin KaFai Lau 			btf_verifier_log(env, "Section overlap found");
5223f80442a4SMartin KaFai Lau 			return -EINVAL;
5224f80442a4SMartin KaFai Lau 		}
5225f80442a4SMartin KaFai Lau 		if (expected_total - total < secs[i].len) {
5226f80442a4SMartin KaFai Lau 			btf_verifier_log(env,
5227f80442a4SMartin KaFai Lau 					 "Total section length too long");
5228f80442a4SMartin KaFai Lau 			return -EINVAL;
5229f80442a4SMartin KaFai Lau 		}
5230f80442a4SMartin KaFai Lau 		total += secs[i].len;
5231f80442a4SMartin KaFai Lau 	}
5232f80442a4SMartin KaFai Lau 
5233f80442a4SMartin KaFai Lau 	/* There is data other than hdr and known sections */
5234f80442a4SMartin KaFai Lau 	if (expected_total != total) {
5235f80442a4SMartin KaFai Lau 		btf_verifier_log(env, "Unsupported section found");
5236f80442a4SMartin KaFai Lau 		return -EINVAL;
5237f80442a4SMartin KaFai Lau 	}
5238f80442a4SMartin KaFai Lau 
5239f80442a4SMartin KaFai Lau 	return 0;
5240f80442a4SMartin KaFai Lau }
5241f80442a4SMartin KaFai Lau 
btf_parse_hdr(struct btf_verifier_env * env)52424a6998afSMartin Lau static int btf_parse_hdr(struct btf_verifier_env *env)
524369b693f0SMartin KaFai Lau {
52444a6998afSMartin Lau 	u32 hdr_len, hdr_copy, btf_data_size;
524569b693f0SMartin KaFai Lau 	const struct btf_header *hdr;
5246f80442a4SMartin KaFai Lau 	struct btf *btf;
524769b693f0SMartin KaFai Lau 
5248f80442a4SMartin KaFai Lau 	btf = env->btf;
52494a6998afSMartin Lau 	btf_data_size = btf->data_size;
5250f80442a4SMartin KaFai Lau 
5251583669abSYuntao Wang 	if (btf_data_size < offsetofend(struct btf_header, hdr_len)) {
5252f80442a4SMartin KaFai Lau 		btf_verifier_log(env, "hdr_len not found");
5253f80442a4SMartin KaFai Lau 		return -EINVAL;
5254f80442a4SMartin KaFai Lau 	}
5255f80442a4SMartin KaFai Lau 
52564a6998afSMartin Lau 	hdr = btf->data;
52574a6998afSMartin Lau 	hdr_len = hdr->hdr_len;
5258f80442a4SMartin KaFai Lau 	if (btf_data_size < hdr_len) {
525969b693f0SMartin KaFai Lau 		btf_verifier_log(env, "btf_header not found");
526069b693f0SMartin KaFai Lau 		return -EINVAL;
526169b693f0SMartin KaFai Lau 	}
526269b693f0SMartin KaFai Lau 
52634a6998afSMartin Lau 	/* Ensure the unsupported header fields are zero */
52644a6998afSMartin Lau 	if (hdr_len > sizeof(btf->hdr)) {
52654a6998afSMartin Lau 		u8 *expected_zero = btf->data + sizeof(btf->hdr);
52664a6998afSMartin Lau 		u8 *end = btf->data + hdr_len;
52674a6998afSMartin Lau 
52684a6998afSMartin Lau 		for (; expected_zero < end; expected_zero++) {
52694a6998afSMartin Lau 			if (*expected_zero) {
5270f80442a4SMartin KaFai Lau 				btf_verifier_log(env, "Unsupported btf_header");
52714a6998afSMartin Lau 				return -E2BIG;
52724a6998afSMartin Lau 			}
52734a6998afSMartin Lau 		}
5274f80442a4SMartin KaFai Lau 	}
527569b693f0SMartin KaFai Lau 
5276f80442a4SMartin KaFai Lau 	hdr_copy = min_t(u32, hdr_len, sizeof(btf->hdr));
52774a6998afSMartin Lau 	memcpy(&btf->hdr, btf->data, hdr_copy);
5278f80442a4SMartin KaFai Lau 
5279f80442a4SMartin KaFai Lau 	hdr = &btf->hdr;
5280f80442a4SMartin KaFai Lau 
5281f80442a4SMartin KaFai Lau 	btf_verifier_log_hdr(env, btf_data_size);
5282f80442a4SMartin KaFai Lau 
528369b693f0SMartin KaFai Lau 	if (hdr->magic != BTF_MAGIC) {
528469b693f0SMartin KaFai Lau 		btf_verifier_log(env, "Invalid magic");
528569b693f0SMartin KaFai Lau 		return -EINVAL;
528669b693f0SMartin KaFai Lau 	}
528769b693f0SMartin KaFai Lau 
528869b693f0SMartin KaFai Lau 	if (hdr->version != BTF_VERSION) {
528969b693f0SMartin KaFai Lau 		btf_verifier_log(env, "Unsupported version");
529069b693f0SMartin KaFai Lau 		return -ENOTSUPP;
529169b693f0SMartin KaFai Lau 	}
529269b693f0SMartin KaFai Lau 
529369b693f0SMartin KaFai Lau 	if (hdr->flags) {
529469b693f0SMartin KaFai Lau 		btf_verifier_log(env, "Unsupported flags");
529569b693f0SMartin KaFai Lau 		return -ENOTSUPP;
529669b693f0SMartin KaFai Lau 	}
529769b693f0SMartin KaFai Lau 
5298bcc5e616SAndrii Nakryiko 	if (!btf->base_btf && btf_data_size == hdr->hdr_len) {
529969b693f0SMartin KaFai Lau 		btf_verifier_log(env, "No data");
530069b693f0SMartin KaFai Lau 		return -EINVAL;
530169b693f0SMartin KaFai Lau 	}
530269b693f0SMartin KaFai Lau 
53033a74904cSWilliam Dean 	return btf_check_sec_info(env, btf_data_size);
530469b693f0SMartin KaFai Lau }
530569b693f0SMartin KaFai Lau 
53068ffa5cc1SKumar Kartikeya Dwivedi static const char *alloc_obj_fields[] = {
53078ffa5cc1SKumar Kartikeya Dwivedi 	"bpf_spin_lock",
53088ffa5cc1SKumar Kartikeya Dwivedi 	"bpf_list_head",
53098ffa5cc1SKumar Kartikeya Dwivedi 	"bpf_list_node",
53109c395c1bSDave Marchevsky 	"bpf_rb_root",
53119c395c1bSDave Marchevsky 	"bpf_rb_node",
5312d54730b5SDave Marchevsky 	"bpf_refcount",
53138ffa5cc1SKumar Kartikeya Dwivedi };
53148ffa5cc1SKumar Kartikeya Dwivedi 
53158ffa5cc1SKumar Kartikeya Dwivedi static struct btf_struct_metas *
btf_parse_struct_metas(struct bpf_verifier_log * log,struct btf * btf)53168ffa5cc1SKumar Kartikeya Dwivedi btf_parse_struct_metas(struct bpf_verifier_log *log, struct btf *btf)
53178ffa5cc1SKumar Kartikeya Dwivedi {
53188ffa5cc1SKumar Kartikeya Dwivedi 	union {
53198ffa5cc1SKumar Kartikeya Dwivedi 		struct btf_id_set set;
53208ffa5cc1SKumar Kartikeya Dwivedi 		struct {
53218ffa5cc1SKumar Kartikeya Dwivedi 			u32 _cnt;
53228ffa5cc1SKumar Kartikeya Dwivedi 			u32 _ids[ARRAY_SIZE(alloc_obj_fields)];
53238ffa5cc1SKumar Kartikeya Dwivedi 		} _arr;
53248ffa5cc1SKumar Kartikeya Dwivedi 	} aof;
53258ffa5cc1SKumar Kartikeya Dwivedi 	struct btf_struct_metas *tab = NULL;
53268ffa5cc1SKumar Kartikeya Dwivedi 	int i, n, id, ret;
53278ffa5cc1SKumar Kartikeya Dwivedi 
53288ffa5cc1SKumar Kartikeya Dwivedi 	BUILD_BUG_ON(offsetof(struct btf_id_set, cnt) != 0);
53298ffa5cc1SKumar Kartikeya Dwivedi 	BUILD_BUG_ON(sizeof(struct btf_id_set) != sizeof(u32));
53308ffa5cc1SKumar Kartikeya Dwivedi 
53318ffa5cc1SKumar Kartikeya Dwivedi 	memset(&aof, 0, sizeof(aof));
53328ffa5cc1SKumar Kartikeya Dwivedi 	for (i = 0; i < ARRAY_SIZE(alloc_obj_fields); i++) {
53338ffa5cc1SKumar Kartikeya Dwivedi 		/* Try to find whether this special type exists in user BTF, and
53348ffa5cc1SKumar Kartikeya Dwivedi 		 * if so remember its ID so we can easily find it among members
53358ffa5cc1SKumar Kartikeya Dwivedi 		 * of structs that we iterate in the next loop.
53368ffa5cc1SKumar Kartikeya Dwivedi 		 */
53378ffa5cc1SKumar Kartikeya Dwivedi 		id = btf_find_by_name_kind(btf, alloc_obj_fields[i], BTF_KIND_STRUCT);
53388ffa5cc1SKumar Kartikeya Dwivedi 		if (id < 0)
53398ffa5cc1SKumar Kartikeya Dwivedi 			continue;
53408ffa5cc1SKumar Kartikeya Dwivedi 		aof.set.ids[aof.set.cnt++] = id;
53418ffa5cc1SKumar Kartikeya Dwivedi 	}
53428ffa5cc1SKumar Kartikeya Dwivedi 
53438ffa5cc1SKumar Kartikeya Dwivedi 	if (!aof.set.cnt)
53448ffa5cc1SKumar Kartikeya Dwivedi 		return NULL;
53458ffa5cc1SKumar Kartikeya Dwivedi 	sort(&aof.set.ids, aof.set.cnt, sizeof(aof.set.ids[0]), btf_id_cmp_func, NULL);
53468ffa5cc1SKumar Kartikeya Dwivedi 
53478ffa5cc1SKumar Kartikeya Dwivedi 	n = btf_nr_types(btf);
53488ffa5cc1SKumar Kartikeya Dwivedi 	for (i = 1; i < n; i++) {
53498ffa5cc1SKumar Kartikeya Dwivedi 		struct btf_struct_metas *new_tab;
53508ffa5cc1SKumar Kartikeya Dwivedi 		const struct btf_member *member;
53518ffa5cc1SKumar Kartikeya Dwivedi 		struct btf_struct_meta *type;
53528ffa5cc1SKumar Kartikeya Dwivedi 		struct btf_record *record;
53538ffa5cc1SKumar Kartikeya Dwivedi 		const struct btf_type *t;
53548ffa5cc1SKumar Kartikeya Dwivedi 		int j, tab_cnt;
53558ffa5cc1SKumar Kartikeya Dwivedi 
53568ffa5cc1SKumar Kartikeya Dwivedi 		t = btf_type_by_id(btf, i);
53578ffa5cc1SKumar Kartikeya Dwivedi 		if (!t) {
53588ffa5cc1SKumar Kartikeya Dwivedi 			ret = -EINVAL;
53598ffa5cc1SKumar Kartikeya Dwivedi 			goto free;
53608ffa5cc1SKumar Kartikeya Dwivedi 		}
53618ffa5cc1SKumar Kartikeya Dwivedi 		if (!__btf_type_is_struct(t))
53628ffa5cc1SKumar Kartikeya Dwivedi 			continue;
53638ffa5cc1SKumar Kartikeya Dwivedi 
53648ffa5cc1SKumar Kartikeya Dwivedi 		cond_resched();
53658ffa5cc1SKumar Kartikeya Dwivedi 
53668ffa5cc1SKumar Kartikeya Dwivedi 		for_each_member(j, t, member) {
53678ffa5cc1SKumar Kartikeya Dwivedi 			if (btf_id_set_contains(&aof.set, member->type))
53688ffa5cc1SKumar Kartikeya Dwivedi 				goto parse;
53698ffa5cc1SKumar Kartikeya Dwivedi 		}
53708ffa5cc1SKumar Kartikeya Dwivedi 		continue;
53718ffa5cc1SKumar Kartikeya Dwivedi 	parse:
53728ffa5cc1SKumar Kartikeya Dwivedi 		tab_cnt = tab ? tab->cnt : 0;
53738ffa5cc1SKumar Kartikeya Dwivedi 		new_tab = krealloc(tab, offsetof(struct btf_struct_metas, types[tab_cnt + 1]),
53748ffa5cc1SKumar Kartikeya Dwivedi 				   GFP_KERNEL | __GFP_NOWARN);
53758ffa5cc1SKumar Kartikeya Dwivedi 		if (!new_tab) {
53768ffa5cc1SKumar Kartikeya Dwivedi 			ret = -ENOMEM;
53778ffa5cc1SKumar Kartikeya Dwivedi 			goto free;
53788ffa5cc1SKumar Kartikeya Dwivedi 		}
53798ffa5cc1SKumar Kartikeya Dwivedi 		if (!tab)
53808ffa5cc1SKumar Kartikeya Dwivedi 			new_tab->cnt = 0;
53818ffa5cc1SKumar Kartikeya Dwivedi 		tab = new_tab;
53828ffa5cc1SKumar Kartikeya Dwivedi 
53838ffa5cc1SKumar Kartikeya Dwivedi 		type = &tab->types[tab->cnt];
53848ffa5cc1SKumar Kartikeya Dwivedi 		type->btf_id = i;
53859c395c1bSDave Marchevsky 		record = btf_parse_fields(btf, t, BPF_SPIN_LOCK | BPF_LIST_HEAD | BPF_LIST_NODE |
5386d54730b5SDave Marchevsky 						  BPF_RB_ROOT | BPF_RB_NODE | BPF_REFCOUNT, t->size);
53878ffa5cc1SKumar Kartikeya Dwivedi 		/* The record cannot be unset, treat it as an error if so */
53888ffa5cc1SKumar Kartikeya Dwivedi 		if (IS_ERR_OR_NULL(record)) {
53898ffa5cc1SKumar Kartikeya Dwivedi 			ret = PTR_ERR_OR_ZERO(record) ?: -EFAULT;
53908ffa5cc1SKumar Kartikeya Dwivedi 			goto free;
53918ffa5cc1SKumar Kartikeya Dwivedi 		}
53928ffa5cc1SKumar Kartikeya Dwivedi 		type->record = record;
53938ffa5cc1SKumar Kartikeya Dwivedi 		tab->cnt++;
53948ffa5cc1SKumar Kartikeya Dwivedi 	}
53958ffa5cc1SKumar Kartikeya Dwivedi 	return tab;
53968ffa5cc1SKumar Kartikeya Dwivedi free:
53978ffa5cc1SKumar Kartikeya Dwivedi 	btf_struct_metas_free(tab);
53988ffa5cc1SKumar Kartikeya Dwivedi 	return ERR_PTR(ret);
53998ffa5cc1SKumar Kartikeya Dwivedi }
54008ffa5cc1SKumar Kartikeya Dwivedi 
btf_find_struct_meta(const struct btf * btf,u32 btf_id)54018ffa5cc1SKumar Kartikeya Dwivedi struct btf_struct_meta *btf_find_struct_meta(const struct btf *btf, u32 btf_id)
54028ffa5cc1SKumar Kartikeya Dwivedi {
54038ffa5cc1SKumar Kartikeya Dwivedi 	struct btf_struct_metas *tab;
54048ffa5cc1SKumar Kartikeya Dwivedi 
54058ffa5cc1SKumar Kartikeya Dwivedi 	BUILD_BUG_ON(offsetof(struct btf_struct_meta, btf_id) != 0);
54068ffa5cc1SKumar Kartikeya Dwivedi 	tab = btf->struct_meta_tab;
54078ffa5cc1SKumar Kartikeya Dwivedi 	if (!tab)
54088ffa5cc1SKumar Kartikeya Dwivedi 		return NULL;
54098ffa5cc1SKumar Kartikeya Dwivedi 	return bsearch(&btf_id, tab->types, tab->cnt, sizeof(tab->types[0]), btf_id_cmp_func);
54108ffa5cc1SKumar Kartikeya Dwivedi }
54118ffa5cc1SKumar Kartikeya Dwivedi 
btf_check_type_tags(struct btf_verifier_env * env,struct btf * btf,int start_id)5412eb596b09SKumar Kartikeya Dwivedi static int btf_check_type_tags(struct btf_verifier_env *env,
5413eb596b09SKumar Kartikeya Dwivedi 			       struct btf *btf, int start_id)
5414eb596b09SKumar Kartikeya Dwivedi {
5415eb596b09SKumar Kartikeya Dwivedi 	int i, n, good_id = start_id - 1;
5416eb596b09SKumar Kartikeya Dwivedi 	bool in_tags;
5417eb596b09SKumar Kartikeya Dwivedi 
5418eb596b09SKumar Kartikeya Dwivedi 	n = btf_nr_types(btf);
5419eb596b09SKumar Kartikeya Dwivedi 	for (i = start_id; i < n; i++) {
5420eb596b09SKumar Kartikeya Dwivedi 		const struct btf_type *t;
5421d1a374a1SKumar Kartikeya Dwivedi 		int chain_limit = 32;
5422eb596b09SKumar Kartikeya Dwivedi 		u32 cur_id = i;
5423eb596b09SKumar Kartikeya Dwivedi 
5424eb596b09SKumar Kartikeya Dwivedi 		t = btf_type_by_id(btf, i);
5425eb596b09SKumar Kartikeya Dwivedi 		if (!t)
5426eb596b09SKumar Kartikeya Dwivedi 			return -EINVAL;
5427eb596b09SKumar Kartikeya Dwivedi 		if (!btf_type_is_modifier(t))
5428eb596b09SKumar Kartikeya Dwivedi 			continue;
5429eb596b09SKumar Kartikeya Dwivedi 
5430eb596b09SKumar Kartikeya Dwivedi 		cond_resched();
5431eb596b09SKumar Kartikeya Dwivedi 
5432eb596b09SKumar Kartikeya Dwivedi 		in_tags = btf_type_is_type_tag(t);
5433eb596b09SKumar Kartikeya Dwivedi 		while (btf_type_is_modifier(t)) {
5434d1a374a1SKumar Kartikeya Dwivedi 			if (!chain_limit--) {
5435d1a374a1SKumar Kartikeya Dwivedi 				btf_verifier_log(env, "Max chain length or cycle detected");
5436d1a374a1SKumar Kartikeya Dwivedi 				return -ELOOP;
5437d1a374a1SKumar Kartikeya Dwivedi 			}
5438eb596b09SKumar Kartikeya Dwivedi 			if (btf_type_is_type_tag(t)) {
5439eb596b09SKumar Kartikeya Dwivedi 				if (!in_tags) {
5440eb596b09SKumar Kartikeya Dwivedi 					btf_verifier_log(env, "Type tags don't precede modifiers");
5441eb596b09SKumar Kartikeya Dwivedi 					return -EINVAL;
5442eb596b09SKumar Kartikeya Dwivedi 				}
5443eb596b09SKumar Kartikeya Dwivedi 			} else if (in_tags) {
5444eb596b09SKumar Kartikeya Dwivedi 				in_tags = false;
5445eb596b09SKumar Kartikeya Dwivedi 			}
5446eb596b09SKumar Kartikeya Dwivedi 			if (cur_id <= good_id)
5447eb596b09SKumar Kartikeya Dwivedi 				break;
5448eb596b09SKumar Kartikeya Dwivedi 			/* Move to next type */
5449eb596b09SKumar Kartikeya Dwivedi 			cur_id = t->type;
5450eb596b09SKumar Kartikeya Dwivedi 			t = btf_type_by_id(btf, cur_id);
5451eb596b09SKumar Kartikeya Dwivedi 			if (!t)
5452eb596b09SKumar Kartikeya Dwivedi 				return -EINVAL;
5453eb596b09SKumar Kartikeya Dwivedi 		}
5454eb596b09SKumar Kartikeya Dwivedi 		good_id = i;
5455eb596b09SKumar Kartikeya Dwivedi 	}
5456eb596b09SKumar Kartikeya Dwivedi 	return 0;
5457eb596b09SKumar Kartikeya Dwivedi }
5458eb596b09SKumar Kartikeya Dwivedi 
finalize_log(struct bpf_verifier_log * log,bpfptr_t uattr,u32 uattr_size)5459bdcab414SAndrii Nakryiko static int finalize_log(struct bpf_verifier_log *log, bpfptr_t uattr, u32 uattr_size)
546069b693f0SMartin KaFai Lau {
5461bdcab414SAndrii Nakryiko 	u32 log_true_size;
546269b693f0SMartin KaFai Lau 	int err;
546369b693f0SMartin KaFai Lau 
5464bdcab414SAndrii Nakryiko 	err = bpf_vlog_finalize(log, &log_true_size);
5465bdcab414SAndrii Nakryiko 
5466bdcab414SAndrii Nakryiko 	if (uattr_size >= offsetofend(union bpf_attr, btf_log_true_size) &&
5467bdcab414SAndrii Nakryiko 	    copy_to_bpfptr_offset(uattr, offsetof(union bpf_attr, btf_log_true_size),
5468bdcab414SAndrii Nakryiko 				  &log_true_size, sizeof(log_true_size)))
5469bdcab414SAndrii Nakryiko 		err = -EFAULT;
5470bdcab414SAndrii Nakryiko 
5471bdcab414SAndrii Nakryiko 	return err;
5472bdcab414SAndrii Nakryiko }
5473bdcab414SAndrii Nakryiko 
btf_parse(const union bpf_attr * attr,bpfptr_t uattr,u32 uattr_size)547447a71c1fSAndrii Nakryiko static struct btf *btf_parse(const union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size)
547569b693f0SMartin KaFai Lau {
547647a71c1fSAndrii Nakryiko 	bpfptr_t btf_data = make_bpfptr(attr->btf, uattr.is_kernel);
547747a71c1fSAndrii Nakryiko 	char __user *log_ubuf = u64_to_user_ptr(attr->btf_log_buf);
547869b693f0SMartin KaFai Lau 	struct btf_struct_metas *struct_meta_tab;
547969b693f0SMartin KaFai Lau 	struct btf_verifier_env *env = NULL;
548069b693f0SMartin KaFai Lau 	struct btf *btf = NULL;
548169b693f0SMartin KaFai Lau 	u8 *data;
5482bdcab414SAndrii Nakryiko 	int err, ret;
548369b693f0SMartin KaFai Lau 
548447a71c1fSAndrii Nakryiko 	if (attr->btf_size > BTF_MAX_SIZE)
548569b693f0SMartin KaFai Lau 		return ERR_PTR(-E2BIG);
548669b693f0SMartin KaFai Lau 
548769b693f0SMartin KaFai Lau 	env = kzalloc(sizeof(*env), GFP_KERNEL | __GFP_NOWARN);
548869b693f0SMartin KaFai Lau 	if (!env)
548969b693f0SMartin KaFai Lau 		return ERR_PTR(-ENOMEM);
549069b693f0SMartin KaFai Lau 
5491bdcab414SAndrii Nakryiko 	/* user could have requested verbose verifier output
549269b693f0SMartin KaFai Lau 	 * and supplied buffer to store the verification trace
549369b693f0SMartin KaFai Lau 	 */
5494bdcab414SAndrii Nakryiko 	err = bpf_vlog_init(&env->log, attr->btf_log_level,
5495bdcab414SAndrii Nakryiko 			    log_ubuf, attr->btf_log_size);
5496bdcab414SAndrii Nakryiko 	if (err)
5497bdcab414SAndrii Nakryiko 		goto errout_free;
549869b693f0SMartin KaFai Lau 
549969b693f0SMartin KaFai Lau 	btf = kzalloc(sizeof(*btf), GFP_KERNEL | __GFP_NOWARN);
550069b693f0SMartin KaFai Lau 	if (!btf) {
550169b693f0SMartin KaFai Lau 		err = -ENOMEM;
550269b693f0SMartin KaFai Lau 		goto errout;
550369b693f0SMartin KaFai Lau 	}
5504f80442a4SMartin KaFai Lau 	env->btf = btf;
5505f80442a4SMartin KaFai Lau 
550647a71c1fSAndrii Nakryiko 	data = kvmalloc(attr->btf_size, GFP_KERNEL | __GFP_NOWARN);
550769b693f0SMartin KaFai Lau 	if (!data) {
550869b693f0SMartin KaFai Lau 		err = -ENOMEM;
550969b693f0SMartin KaFai Lau 		goto errout;
551069b693f0SMartin KaFai Lau 	}
551169b693f0SMartin KaFai Lau 
551269b693f0SMartin KaFai Lau 	btf->data = data;
551347a71c1fSAndrii Nakryiko 	btf->data_size = attr->btf_size;
551469b693f0SMartin KaFai Lau 
551547a71c1fSAndrii Nakryiko 	if (copy_from_bpfptr(data, btf_data, attr->btf_size)) {
551669b693f0SMartin KaFai Lau 		err = -EFAULT;
551769b693f0SMartin KaFai Lau 		goto errout;
551869b693f0SMartin KaFai Lau 	}
551969b693f0SMartin KaFai Lau 
55204a6998afSMartin Lau 	err = btf_parse_hdr(env);
55214a6998afSMartin Lau 	if (err)
55224a6998afSMartin Lau 		goto errout;
55234a6998afSMartin Lau 
55244a6998afSMartin Lau 	btf->nohdr_data = btf->data + btf->hdr.hdr_len;
55254a6998afSMartin Lau 
552669b693f0SMartin KaFai Lau 	err = btf_parse_str_sec(env);
552769b693f0SMartin KaFai Lau 	if (err)
552869b693f0SMartin KaFai Lau 		goto errout;
552969b693f0SMartin KaFai Lau 
553069b693f0SMartin KaFai Lau 	err = btf_parse_type_sec(env);
553169b693f0SMartin KaFai Lau 	if (err)
553269b693f0SMartin KaFai Lau 		goto errout;
553369b693f0SMartin KaFai Lau 
5534eb596b09SKumar Kartikeya Dwivedi 	err = btf_check_type_tags(env, btf, 1);
5535eb596b09SKumar Kartikeya Dwivedi 	if (err)
5536eb596b09SKumar Kartikeya Dwivedi 		goto errout;
5537eb596b09SKumar Kartikeya Dwivedi 
5538bdcab414SAndrii Nakryiko 	struct_meta_tab = btf_parse_struct_metas(&env->log, btf);
55398ffa5cc1SKumar Kartikeya Dwivedi 	if (IS_ERR(struct_meta_tab)) {
55408ffa5cc1SKumar Kartikeya Dwivedi 		err = PTR_ERR(struct_meta_tab);
55418ffa5cc1SKumar Kartikeya Dwivedi 		goto errout;
55428ffa5cc1SKumar Kartikeya Dwivedi 	}
55438ffa5cc1SKumar Kartikeya Dwivedi 	btf->struct_meta_tab = struct_meta_tab;
55448ffa5cc1SKumar Kartikeya Dwivedi 
5545865ce09aSKumar Kartikeya Dwivedi 	if (struct_meta_tab) {
5546865ce09aSKumar Kartikeya Dwivedi 		int i;
5547865ce09aSKumar Kartikeya Dwivedi 
5548865ce09aSKumar Kartikeya Dwivedi 		for (i = 0; i < struct_meta_tab->cnt; i++) {
5549865ce09aSKumar Kartikeya Dwivedi 			err = btf_check_and_fixup_fields(btf, struct_meta_tab->types[i].record);
5550865ce09aSKumar Kartikeya Dwivedi 			if (err < 0)
5551865ce09aSKumar Kartikeya Dwivedi 				goto errout_meta;
5552865ce09aSKumar Kartikeya Dwivedi 		}
5553865ce09aSKumar Kartikeya Dwivedi 	}
5554865ce09aSKumar Kartikeya Dwivedi 
5555bdcab414SAndrii Nakryiko 	err = finalize_log(&env->log, uattr, uattr_size);
5556bdcab414SAndrii Nakryiko 	if (err)
5557bdcab414SAndrii Nakryiko 		goto errout_free;
555869b693f0SMartin KaFai Lau 
555969b693f0SMartin KaFai Lau 	btf_verifier_env_free(env);
556082e96972SMartin KaFai Lau 	refcount_set(&btf->refcnt, 1);
556169b693f0SMartin KaFai Lau 	return btf;
556269b693f0SMartin KaFai Lau 
55638ffa5cc1SKumar Kartikeya Dwivedi errout_meta:
55648ffa5cc1SKumar Kartikeya Dwivedi 	btf_free_struct_meta_tab(btf);
556569b693f0SMartin KaFai Lau errout:
5566bdcab414SAndrii Nakryiko 	/* overwrite err with -ENOSPC or -EFAULT */
5567bdcab414SAndrii Nakryiko 	ret = finalize_log(&env->log, uattr, uattr_size);
5568bdcab414SAndrii Nakryiko 	if (ret)
5569bdcab414SAndrii Nakryiko 		err = ret;
5570bdcab414SAndrii Nakryiko errout_free:
557169b693f0SMartin KaFai Lau 	btf_verifier_env_free(env);
557269b693f0SMartin KaFai Lau 	if (btf)
557369b693f0SMartin KaFai Lau 		btf_free(btf);
557469b693f0SMartin KaFai Lau 	return ERR_PTR(err);
557569b693f0SMartin KaFai Lau }
5576b00b8daeSMartin KaFai Lau 
557790ceddcbSFangrui Song extern char __weak __start_BTF[];
557890ceddcbSFangrui Song extern char __weak __stop_BTF[];
557991cc1a99SAlexei Starovoitov extern struct btf *btf_vmlinux;
558091cc1a99SAlexei Starovoitov 
558191cc1a99SAlexei Starovoitov #define BPF_MAP_TYPE(_id, _ops)
5582f2e10bffSAndrii Nakryiko #define BPF_LINK_TYPE(_id, _name)
558391cc1a99SAlexei Starovoitov static union {
558491cc1a99SAlexei Starovoitov 	struct bpf_ctx_convert {
558591cc1a99SAlexei Starovoitov #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
558691cc1a99SAlexei Starovoitov 	prog_ctx_type _id##_prog; \
558791cc1a99SAlexei Starovoitov 	kern_ctx_type _id##_kern;
558891cc1a99SAlexei Starovoitov #include <linux/bpf_types.h>
558991cc1a99SAlexei Starovoitov #undef BPF_PROG_TYPE
559091cc1a99SAlexei Starovoitov 	} *__t;
559191cc1a99SAlexei Starovoitov 	/* 't' is written once under lock. Read many times. */
559291cc1a99SAlexei Starovoitov 	const struct btf_type *t;
559391cc1a99SAlexei Starovoitov } bpf_ctx_convert;
559491cc1a99SAlexei Starovoitov enum {
559591cc1a99SAlexei Starovoitov #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
559691cc1a99SAlexei Starovoitov 	__ctx_convert##_id,
559791cc1a99SAlexei Starovoitov #include <linux/bpf_types.h>
559891cc1a99SAlexei Starovoitov #undef BPF_PROG_TYPE
5599ce27709bSAlexei Starovoitov 	__ctx_convert_unused, /* to avoid empty enum in extreme .config */
560091cc1a99SAlexei Starovoitov };
560191cc1a99SAlexei Starovoitov static u8 bpf_ctx_convert_map[] = {
560291cc1a99SAlexei Starovoitov #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
560391cc1a99SAlexei Starovoitov 	[_id] = __ctx_convert##_id,
560491cc1a99SAlexei Starovoitov #include <linux/bpf_types.h>
560591cc1a99SAlexei Starovoitov #undef BPF_PROG_TYPE
56064c80c7bcSArnd Bergmann 	0, /* avoid empty array */
560791cc1a99SAlexei Starovoitov };
560891cc1a99SAlexei Starovoitov #undef BPF_MAP_TYPE
5609f2e10bffSAndrii Nakryiko #undef BPF_LINK_TYPE
561091cc1a99SAlexei Starovoitov 
561100b85860SKumar Kartikeya Dwivedi const struct btf_member *
btf_get_prog_ctx_type(struct bpf_verifier_log * log,const struct btf * btf,const struct btf_type * t,enum bpf_prog_type prog_type,int arg)561234747c41SMartin KaFai Lau btf_get_prog_ctx_type(struct bpf_verifier_log *log, const struct btf *btf,
561351c39bb1SAlexei Starovoitov 		      const struct btf_type *t, enum bpf_prog_type prog_type,
561451c39bb1SAlexei Starovoitov 		      int arg)
561591cc1a99SAlexei Starovoitov {
561691cc1a99SAlexei Starovoitov 	const struct btf_type *conv_struct;
561791cc1a99SAlexei Starovoitov 	const struct btf_type *ctx_struct;
561891cc1a99SAlexei Starovoitov 	const struct btf_member *ctx_type;
561991cc1a99SAlexei Starovoitov 	const char *tname, *ctx_tname;
562091cc1a99SAlexei Starovoitov 
562191cc1a99SAlexei Starovoitov 	conv_struct = bpf_ctx_convert.t;
562291cc1a99SAlexei Starovoitov 	if (!conv_struct) {
562391cc1a99SAlexei Starovoitov 		bpf_log(log, "btf_vmlinux is malformed\n");
562491cc1a99SAlexei Starovoitov 		return NULL;
562591cc1a99SAlexei Starovoitov 	}
562691cc1a99SAlexei Starovoitov 	t = btf_type_by_id(btf, t->type);
562791cc1a99SAlexei Starovoitov 	while (btf_type_is_modifier(t))
562891cc1a99SAlexei Starovoitov 		t = btf_type_by_id(btf, t->type);
562991cc1a99SAlexei Starovoitov 	if (!btf_type_is_struct(t)) {
563091cc1a99SAlexei Starovoitov 		/* Only pointer to struct is supported for now.
563191cc1a99SAlexei Starovoitov 		 * That means that BPF_PROG_TYPE_TRACEPOINT with BTF
563291cc1a99SAlexei Starovoitov 		 * is not supported yet.
563391cc1a99SAlexei Starovoitov 		 * BPF_PROG_TYPE_RAW_TRACEPOINT is fine.
563491cc1a99SAlexei Starovoitov 		 */
563591cc1a99SAlexei Starovoitov 		return NULL;
563691cc1a99SAlexei Starovoitov 	}
563791cc1a99SAlexei Starovoitov 	tname = btf_name_by_offset(btf, t->name_off);
563891cc1a99SAlexei Starovoitov 	if (!tname) {
563951c39bb1SAlexei Starovoitov 		bpf_log(log, "arg#%d struct doesn't have a name\n", arg);
564091cc1a99SAlexei Starovoitov 		return NULL;
564191cc1a99SAlexei Starovoitov 	}
564291cc1a99SAlexei Starovoitov 	/* prog_type is valid bpf program type. No need for bounds check. */
564391cc1a99SAlexei Starovoitov 	ctx_type = btf_type_member(conv_struct) + bpf_ctx_convert_map[prog_type] * 2;
564491cc1a99SAlexei Starovoitov 	/* ctx_struct is a pointer to prog_ctx_type in vmlinux.
564591cc1a99SAlexei Starovoitov 	 * Like 'struct __sk_buff'
564691cc1a99SAlexei Starovoitov 	 */
564791cc1a99SAlexei Starovoitov 	ctx_struct = btf_type_by_id(btf_vmlinux, ctx_type->type);
564891cc1a99SAlexei Starovoitov 	if (!ctx_struct)
564991cc1a99SAlexei Starovoitov 		/* should not happen */
565091cc1a99SAlexei Starovoitov 		return NULL;
5651d384dce2SAndrii Nakryiko again:
565291cc1a99SAlexei Starovoitov 	ctx_tname = btf_name_by_offset(btf_vmlinux, ctx_struct->name_off);
565391cc1a99SAlexei Starovoitov 	if (!ctx_tname) {
565491cc1a99SAlexei Starovoitov 		/* should not happen */
565591cc1a99SAlexei Starovoitov 		bpf_log(log, "Please fix kernel include/linux/bpf_types.h\n");
565691cc1a99SAlexei Starovoitov 		return NULL;
565791cc1a99SAlexei Starovoitov 	}
565891cc1a99SAlexei Starovoitov 	/* only compare that prog's ctx type name is the same as
565991cc1a99SAlexei Starovoitov 	 * kernel expects. No need to compare field by field.
566091cc1a99SAlexei Starovoitov 	 * It's ok for bpf prog to do:
566191cc1a99SAlexei Starovoitov 	 * struct __sk_buff {};
566291cc1a99SAlexei Starovoitov 	 * int socket_filter_bpf_prog(struct __sk_buff *skb)
566391cc1a99SAlexei Starovoitov 	 * { // no fields of skb are ever used }
566491cc1a99SAlexei Starovoitov 	 */
56652f464393SJoanne Koong 	if (strcmp(ctx_tname, "__sk_buff") == 0 && strcmp(tname, "sk_buff") == 0)
56662f464393SJoanne Koong 		return ctx_type;
56672f464393SJoanne Koong 	if (strcmp(ctx_tname, "xdp_md") == 0 && strcmp(tname, "xdp_buff") == 0)
56682f464393SJoanne Koong 		return ctx_type;
5669d384dce2SAndrii Nakryiko 	if (strcmp(ctx_tname, tname)) {
5670d384dce2SAndrii Nakryiko 		/* bpf_user_pt_regs_t is a typedef, so resolve it to
5671d384dce2SAndrii Nakryiko 		 * underlying struct and check name again
5672d384dce2SAndrii Nakryiko 		 */
5673d384dce2SAndrii Nakryiko 		if (!btf_type_is_modifier(ctx_struct))
567491cc1a99SAlexei Starovoitov 			return NULL;
5675d384dce2SAndrii Nakryiko 		while (btf_type_is_modifier(ctx_struct))
5676d384dce2SAndrii Nakryiko 			ctx_struct = btf_type_by_id(btf_vmlinux, ctx_struct->type);
5677d384dce2SAndrii Nakryiko 		goto again;
5678d384dce2SAndrii Nakryiko 	}
567991cc1a99SAlexei Starovoitov 	return ctx_type;
568091cc1a99SAlexei Starovoitov }
56818580ac94SAlexei Starovoitov 
btf_translate_to_vmlinux(struct bpf_verifier_log * log,struct btf * btf,const struct btf_type * t,enum bpf_prog_type prog_type,int arg)56825b92a28aSAlexei Starovoitov static int btf_translate_to_vmlinux(struct bpf_verifier_log *log,
56835b92a28aSAlexei Starovoitov 				     struct btf *btf,
56845b92a28aSAlexei Starovoitov 				     const struct btf_type *t,
568551c39bb1SAlexei Starovoitov 				     enum bpf_prog_type prog_type,
568651c39bb1SAlexei Starovoitov 				     int arg)
56875b92a28aSAlexei Starovoitov {
56885b92a28aSAlexei Starovoitov 	const struct btf_member *prog_ctx_type, *kern_ctx_type;
56895b92a28aSAlexei Starovoitov 
569051c39bb1SAlexei Starovoitov 	prog_ctx_type = btf_get_prog_ctx_type(log, btf, t, prog_type, arg);
56915b92a28aSAlexei Starovoitov 	if (!prog_ctx_type)
56925b92a28aSAlexei Starovoitov 		return -ENOENT;
56935b92a28aSAlexei Starovoitov 	kern_ctx_type = prog_ctx_type + 1;
56945b92a28aSAlexei Starovoitov 	return kern_ctx_type->type;
56955b92a28aSAlexei Starovoitov }
56965b92a28aSAlexei Starovoitov 
get_kern_ctx_btf_id(struct bpf_verifier_log * log,enum bpf_prog_type prog_type)5697fd264ca0SYonghong Song int get_kern_ctx_btf_id(struct bpf_verifier_log *log, enum bpf_prog_type prog_type)
5698fd264ca0SYonghong Song {
5699fd264ca0SYonghong Song 	const struct btf_member *kctx_member;
5700fd264ca0SYonghong Song 	const struct btf_type *conv_struct;
5701fd264ca0SYonghong Song 	const struct btf_type *kctx_type;
5702fd264ca0SYonghong Song 	u32 kctx_type_id;
5703fd264ca0SYonghong Song 
5704fd264ca0SYonghong Song 	conv_struct = bpf_ctx_convert.t;
5705fd264ca0SYonghong Song 	/* get member for kernel ctx type */
5706fd264ca0SYonghong Song 	kctx_member = btf_type_member(conv_struct) + bpf_ctx_convert_map[prog_type] * 2 + 1;
5707fd264ca0SYonghong Song 	kctx_type_id = kctx_member->type;
5708fd264ca0SYonghong Song 	kctx_type = btf_type_by_id(btf_vmlinux, kctx_type_id);
5709fd264ca0SYonghong Song 	if (!btf_type_is_struct(kctx_type)) {
5710fd264ca0SYonghong Song 		bpf_log(log, "kern ctx type id %u is not a struct\n", kctx_type_id);
5711fd264ca0SYonghong Song 		return -EINVAL;
5712fd264ca0SYonghong Song 	}
5713fd264ca0SYonghong Song 
5714fd264ca0SYonghong Song 	return kctx_type_id;
5715fd264ca0SYonghong Song }
5716fd264ca0SYonghong Song 
571749f4e672SJiri Olsa BTF_ID_LIST(bpf_ctx_convert_btf_id)
BTF_ID(struct,bpf_ctx_convert)571849f4e672SJiri Olsa BTF_ID(struct, bpf_ctx_convert)
571949f4e672SJiri Olsa 
57208580ac94SAlexei Starovoitov struct btf *btf_parse_vmlinux(void)
57218580ac94SAlexei Starovoitov {
57228580ac94SAlexei Starovoitov 	struct btf_verifier_env *env = NULL;
57238580ac94SAlexei Starovoitov 	struct bpf_verifier_log *log;
57248580ac94SAlexei Starovoitov 	struct btf *btf = NULL;
572549f4e672SJiri Olsa 	int err;
57268580ac94SAlexei Starovoitov 
57278580ac94SAlexei Starovoitov 	env = kzalloc(sizeof(*env), GFP_KERNEL | __GFP_NOWARN);
57288580ac94SAlexei Starovoitov 	if (!env)
57298580ac94SAlexei Starovoitov 		return ERR_PTR(-ENOMEM);
57308580ac94SAlexei Starovoitov 
57318580ac94SAlexei Starovoitov 	log = &env->log;
57328580ac94SAlexei Starovoitov 	log->level = BPF_LOG_KERNEL;
57338580ac94SAlexei Starovoitov 
57348580ac94SAlexei Starovoitov 	btf = kzalloc(sizeof(*btf), GFP_KERNEL | __GFP_NOWARN);
57358580ac94SAlexei Starovoitov 	if (!btf) {
57368580ac94SAlexei Starovoitov 		err = -ENOMEM;
57378580ac94SAlexei Starovoitov 		goto errout;
57388580ac94SAlexei Starovoitov 	}
57398580ac94SAlexei Starovoitov 	env->btf = btf;
57408580ac94SAlexei Starovoitov 
574190ceddcbSFangrui Song 	btf->data = __start_BTF;
574290ceddcbSFangrui Song 	btf->data_size = __stop_BTF - __start_BTF;
574353297220SAndrii Nakryiko 	btf->kernel_btf = true;
574453297220SAndrii Nakryiko 	snprintf(btf->name, sizeof(btf->name), "vmlinux");
57458580ac94SAlexei Starovoitov 
57468580ac94SAlexei Starovoitov 	err = btf_parse_hdr(env);
57478580ac94SAlexei Starovoitov 	if (err)
57488580ac94SAlexei Starovoitov 		goto errout;
57498580ac94SAlexei Starovoitov 
57508580ac94SAlexei Starovoitov 	btf->nohdr_data = btf->data + btf->hdr.hdr_len;
57518580ac94SAlexei Starovoitov 
57528580ac94SAlexei Starovoitov 	err = btf_parse_str_sec(env);
57538580ac94SAlexei Starovoitov 	if (err)
57548580ac94SAlexei Starovoitov 		goto errout;
57558580ac94SAlexei Starovoitov 
57568580ac94SAlexei Starovoitov 	err = btf_check_all_metas(env);
57578580ac94SAlexei Starovoitov 	if (err)
57588580ac94SAlexei Starovoitov 		goto errout;
57598580ac94SAlexei Starovoitov 
5760eb596b09SKumar Kartikeya Dwivedi 	err = btf_check_type_tags(env, btf, 1);
5761eb596b09SKumar Kartikeya Dwivedi 	if (err)
5762eb596b09SKumar Kartikeya Dwivedi 		goto errout;
5763eb596b09SKumar Kartikeya Dwivedi 
5764a2d0d62fSAndrey Ignatov 	/* btf_parse_vmlinux() runs under bpf_verifier_lock */
576549f4e672SJiri Olsa 	bpf_ctx_convert.t = btf_type_by_id(btf, bpf_ctx_convert_btf_id[0]);
576691cc1a99SAlexei Starovoitov 
5767d3e42bb0SMartin KaFai Lau 	bpf_struct_ops_init(btf, log);
576827ae7997SMartin KaFai Lau 
57698580ac94SAlexei Starovoitov 	refcount_set(&btf->refcnt, 1);
577053297220SAndrii Nakryiko 
577153297220SAndrii Nakryiko 	err = btf_alloc_id(btf);
577253297220SAndrii Nakryiko 	if (err)
577353297220SAndrii Nakryiko 		goto errout;
577453297220SAndrii Nakryiko 
577553297220SAndrii Nakryiko 	btf_verifier_env_free(env);
57768580ac94SAlexei Starovoitov 	return btf;
57778580ac94SAlexei Starovoitov 
57788580ac94SAlexei Starovoitov errout:
57798580ac94SAlexei Starovoitov 	btf_verifier_env_free(env);
57808580ac94SAlexei Starovoitov 	if (btf) {
57818580ac94SAlexei Starovoitov 		kvfree(btf->types);
57828580ac94SAlexei Starovoitov 		kfree(btf);
57838580ac94SAlexei Starovoitov 	}
57848580ac94SAlexei Starovoitov 	return ERR_PTR(err);
57858580ac94SAlexei Starovoitov }
57868580ac94SAlexei Starovoitov 
57877112d127SAndrii Nakryiko #ifdef CONFIG_DEBUG_INFO_BTF_MODULES
57887112d127SAndrii Nakryiko 
btf_parse_module(const char * module_name,const void * data,unsigned int data_size)578936e68442SAndrii Nakryiko static struct btf *btf_parse_module(const char *module_name, const void *data, unsigned int data_size)
579036e68442SAndrii Nakryiko {
579136e68442SAndrii Nakryiko 	struct btf_verifier_env *env = NULL;
579236e68442SAndrii Nakryiko 	struct bpf_verifier_log *log;
579336e68442SAndrii Nakryiko 	struct btf *btf = NULL, *base_btf;
579436e68442SAndrii Nakryiko 	int err;
579536e68442SAndrii Nakryiko 
579636e68442SAndrii Nakryiko 	base_btf = bpf_get_btf_vmlinux();
579736e68442SAndrii Nakryiko 	if (IS_ERR(base_btf))
579836e68442SAndrii Nakryiko 		return base_btf;
579936e68442SAndrii Nakryiko 	if (!base_btf)
580036e68442SAndrii Nakryiko 		return ERR_PTR(-EINVAL);
580136e68442SAndrii Nakryiko 
580236e68442SAndrii Nakryiko 	env = kzalloc(sizeof(*env), GFP_KERNEL | __GFP_NOWARN);
580336e68442SAndrii Nakryiko 	if (!env)
580436e68442SAndrii Nakryiko 		return ERR_PTR(-ENOMEM);
580536e68442SAndrii Nakryiko 
580636e68442SAndrii Nakryiko 	log = &env->log;
580736e68442SAndrii Nakryiko 	log->level = BPF_LOG_KERNEL;
580836e68442SAndrii Nakryiko 
580936e68442SAndrii Nakryiko 	btf = kzalloc(sizeof(*btf), GFP_KERNEL | __GFP_NOWARN);
581036e68442SAndrii Nakryiko 	if (!btf) {
581136e68442SAndrii Nakryiko 		err = -ENOMEM;
581236e68442SAndrii Nakryiko 		goto errout;
581336e68442SAndrii Nakryiko 	}
581436e68442SAndrii Nakryiko 	env->btf = btf;
581536e68442SAndrii Nakryiko 
581636e68442SAndrii Nakryiko 	btf->base_btf = base_btf;
581736e68442SAndrii Nakryiko 	btf->start_id = base_btf->nr_types;
581836e68442SAndrii Nakryiko 	btf->start_str_off = base_btf->hdr.str_len;
581936e68442SAndrii Nakryiko 	btf->kernel_btf = true;
582036e68442SAndrii Nakryiko 	snprintf(btf->name, sizeof(btf->name), "%s", module_name);
582136e68442SAndrii Nakryiko 
582236e68442SAndrii Nakryiko 	btf->data = kvmalloc(data_size, GFP_KERNEL | __GFP_NOWARN);
582336e68442SAndrii Nakryiko 	if (!btf->data) {
582436e68442SAndrii Nakryiko 		err = -ENOMEM;
582536e68442SAndrii Nakryiko 		goto errout;
582636e68442SAndrii Nakryiko 	}
582736e68442SAndrii Nakryiko 	memcpy(btf->data, data, data_size);
582836e68442SAndrii Nakryiko 	btf->data_size = data_size;
582936e68442SAndrii Nakryiko 
583036e68442SAndrii Nakryiko 	err = btf_parse_hdr(env);
583136e68442SAndrii Nakryiko 	if (err)
583236e68442SAndrii Nakryiko 		goto errout;
583336e68442SAndrii Nakryiko 
583436e68442SAndrii Nakryiko 	btf->nohdr_data = btf->data + btf->hdr.hdr_len;
583536e68442SAndrii Nakryiko 
583636e68442SAndrii Nakryiko 	err = btf_parse_str_sec(env);
583736e68442SAndrii Nakryiko 	if (err)
583836e68442SAndrii Nakryiko 		goto errout;
583936e68442SAndrii Nakryiko 
584036e68442SAndrii Nakryiko 	err = btf_check_all_metas(env);
584136e68442SAndrii Nakryiko 	if (err)
584236e68442SAndrii Nakryiko 		goto errout;
584336e68442SAndrii Nakryiko 
5844eb596b09SKumar Kartikeya Dwivedi 	err = btf_check_type_tags(env, btf, btf_nr_types(base_btf));
5845eb596b09SKumar Kartikeya Dwivedi 	if (err)
5846eb596b09SKumar Kartikeya Dwivedi 		goto errout;
5847eb596b09SKumar Kartikeya Dwivedi 
584836e68442SAndrii Nakryiko 	btf_verifier_env_free(env);
584936e68442SAndrii Nakryiko 	refcount_set(&btf->refcnt, 1);
585036e68442SAndrii Nakryiko 	return btf;
585136e68442SAndrii Nakryiko 
585236e68442SAndrii Nakryiko errout:
585336e68442SAndrii Nakryiko 	btf_verifier_env_free(env);
585436e68442SAndrii Nakryiko 	if (btf) {
585536e68442SAndrii Nakryiko 		kvfree(btf->data);
585636e68442SAndrii Nakryiko 		kvfree(btf->types);
585736e68442SAndrii Nakryiko 		kfree(btf);
585836e68442SAndrii Nakryiko 	}
585936e68442SAndrii Nakryiko 	return ERR_PTR(err);
586036e68442SAndrii Nakryiko }
586136e68442SAndrii Nakryiko 
58627112d127SAndrii Nakryiko #endif /* CONFIG_DEBUG_INFO_BTF_MODULES */
58637112d127SAndrii Nakryiko 
bpf_prog_get_target_btf(const struct bpf_prog * prog)58645b92a28aSAlexei Starovoitov struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog)
58655b92a28aSAlexei Starovoitov {
58663aac1eadSToke Høiland-Jørgensen 	struct bpf_prog *tgt_prog = prog->aux->dst_prog;
58675b92a28aSAlexei Starovoitov 
586822dc4a0fSAndrii Nakryiko 	if (tgt_prog)
58695b92a28aSAlexei Starovoitov 		return tgt_prog->aux->btf;
587022dc4a0fSAndrii Nakryiko 	else
587122dc4a0fSAndrii Nakryiko 		return prog->aux->attach_btf;
58725b92a28aSAlexei Starovoitov }
58735b92a28aSAlexei Starovoitov 
is_int_ptr(struct btf * btf,const struct btf_type * t)5874bb6728d7SJiri Olsa static bool is_int_ptr(struct btf *btf, const struct btf_type *t)
587584ad7a7aSJiri Olsa {
587691f2dc68SFeng Zhou 	/* skip modifiers */
587791f2dc68SFeng Zhou 	t = btf_type_skip_modifiers(btf, t->type, NULL);
587884ad7a7aSJiri Olsa 
5879bb6728d7SJiri Olsa 	return btf_type_is_int(t);
588084ad7a7aSJiri Olsa }
588184ad7a7aSJiri Olsa 
get_ctx_arg_idx(struct btf * btf,const struct btf_type * func_proto,int off)5882720e6a43SYonghong Song static u32 get_ctx_arg_idx(struct btf *btf, const struct btf_type *func_proto,
5883720e6a43SYonghong Song 			   int off)
5884720e6a43SYonghong Song {
5885720e6a43SYonghong Song 	const struct btf_param *args;
5886720e6a43SYonghong Song 	const struct btf_type *t;
5887720e6a43SYonghong Song 	u32 offset = 0, nr_args;
5888720e6a43SYonghong Song 	int i;
5889720e6a43SYonghong Song 
5890720e6a43SYonghong Song 	if (!func_proto)
5891720e6a43SYonghong Song 		return off / 8;
5892720e6a43SYonghong Song 
5893720e6a43SYonghong Song 	nr_args = btf_type_vlen(func_proto);
5894720e6a43SYonghong Song 	args = (const struct btf_param *)(func_proto + 1);
5895720e6a43SYonghong Song 	for (i = 0; i < nr_args; i++) {
5896720e6a43SYonghong Song 		t = btf_type_skip_modifiers(btf, args[i].type, NULL);
5897720e6a43SYonghong Song 		offset += btf_type_is_ptr(t) ? 8 : roundup(t->size, 8);
5898720e6a43SYonghong Song 		if (off < offset)
5899720e6a43SYonghong Song 			return i;
5900720e6a43SYonghong Song 	}
5901720e6a43SYonghong Song 
5902720e6a43SYonghong Song 	t = btf_type_skip_modifiers(btf, func_proto->type, NULL);
5903720e6a43SYonghong Song 	offset += btf_type_is_ptr(t) ? 8 : roundup(t->size, 8);
5904720e6a43SYonghong Song 	if (off < offset)
5905720e6a43SYonghong Song 		return nr_args;
5906720e6a43SYonghong Song 
5907720e6a43SYonghong Song 	return nr_args + 1;
5908720e6a43SYonghong Song }
5909720e6a43SYonghong Song 
prog_args_trusted(const struct bpf_prog * prog)5910c6b0337fSAlexei Starovoitov static bool prog_args_trusted(const struct bpf_prog *prog)
59113f00c523SDavid Vernet {
5912c6b0337fSAlexei Starovoitov 	enum bpf_attach_type atype = prog->expected_attach_type;
5913c6b0337fSAlexei Starovoitov 
5914c6b0337fSAlexei Starovoitov 	switch (prog->type) {
5915c6b0337fSAlexei Starovoitov 	case BPF_PROG_TYPE_TRACING:
5916c6b0337fSAlexei Starovoitov 		return atype == BPF_TRACE_RAW_TP || atype == BPF_TRACE_ITER;
5917c6b0337fSAlexei Starovoitov 	case BPF_PROG_TYPE_LSM:
5918c0c852ddSYonghong Song 		return bpf_lsm_is_trusted(prog);
5919c6b0337fSAlexei Starovoitov 	case BPF_PROG_TYPE_STRUCT_OPS:
5920c6b0337fSAlexei Starovoitov 		return true;
5921c6b0337fSAlexei Starovoitov 	default:
5922c6b0337fSAlexei Starovoitov 		return false;
5923c6b0337fSAlexei Starovoitov 	}
59243f00c523SDavid Vernet }
59253f00c523SDavid Vernet 
btf_ctx_access(int off,int size,enum bpf_access_type type,const struct bpf_prog * prog,struct bpf_insn_access_aux * info)59269e15db66SAlexei Starovoitov bool btf_ctx_access(int off, int size, enum bpf_access_type type,
59279e15db66SAlexei Starovoitov 		    const struct bpf_prog *prog,
59289e15db66SAlexei Starovoitov 		    struct bpf_insn_access_aux *info)
59299e15db66SAlexei Starovoitov {
593038207291SMartin KaFai Lau 	const struct btf_type *t = prog->aux->attach_func_proto;
59313aac1eadSToke Høiland-Jørgensen 	struct bpf_prog *tgt_prog = prog->aux->dst_prog;
59325b92a28aSAlexei Starovoitov 	struct btf *btf = bpf_prog_get_target_btf(prog);
593338207291SMartin KaFai Lau 	const char *tname = prog->aux->attach_func_name;
59349e15db66SAlexei Starovoitov 	struct bpf_verifier_log *log = info->log;
59359e15db66SAlexei Starovoitov 	const struct btf_param *args;
5936c6f1bfe8SYonghong Song 	const char *tag_value;
59379e15db66SAlexei Starovoitov 	u32 nr_args, arg;
59383c32cc1bSYonghong Song 	int i, ret;
59399e15db66SAlexei Starovoitov 
59409e15db66SAlexei Starovoitov 	if (off % 8) {
594138207291SMartin KaFai Lau 		bpf_log(log, "func '%s' offset %d is not multiple of 8\n",
59429e15db66SAlexei Starovoitov 			tname, off);
59439e15db66SAlexei Starovoitov 		return false;
59449e15db66SAlexei Starovoitov 	}
5945720e6a43SYonghong Song 	arg = get_ctx_arg_idx(btf, t, off);
59469e15db66SAlexei Starovoitov 	args = (const struct btf_param *)(t + 1);
5947523a4cf4SDmitrii Banshchikov 	/* if (t == NULL) Fall back to default BPF prog with
5948523a4cf4SDmitrii Banshchikov 	 * MAX_BPF_FUNC_REG_ARGS u64 arguments.
5949523a4cf4SDmitrii Banshchikov 	 */
5950523a4cf4SDmitrii Banshchikov 	nr_args = t ? btf_type_vlen(t) : MAX_BPF_FUNC_REG_ARGS;
595138207291SMartin KaFai Lau 	if (prog->aux->attach_btf_trace) {
59529e15db66SAlexei Starovoitov 		/* skip first 'void *__data' argument in btf_trace_##name typedef */
59539e15db66SAlexei Starovoitov 		args++;
595438207291SMartin KaFai Lau 		nr_args--;
595538207291SMartin KaFai Lau 	}
59569e15db66SAlexei Starovoitov 
5957f50b49a0SKP Singh 	if (arg > nr_args) {
5958f50b49a0SKP Singh 		bpf_log(log, "func '%s' doesn't have %d-th argument\n",
5959f50b49a0SKP Singh 			tname, arg + 1);
5960f50b49a0SKP Singh 		return false;
5961f50b49a0SKP Singh 	}
5962f50b49a0SKP Singh 
59636ba43b76SKP Singh 	if (arg == nr_args) {
5964f50b49a0SKP Singh 		switch (prog->expected_attach_type) {
596569fd337aSStanislav Fomichev 		case BPF_LSM_CGROUP:
5966f50b49a0SKP Singh 		case BPF_LSM_MAC:
5967f50b49a0SKP Singh 		case BPF_TRACE_FEXIT:
59689e4e01dfSKP Singh 			/* When LSM programs are attached to void LSM hooks
59699e4e01dfSKP Singh 			 * they use FEXIT trampolines and when attached to
59709e4e01dfSKP Singh 			 * int LSM hooks, they use MODIFY_RETURN trampolines.
59719e4e01dfSKP Singh 			 *
59729e4e01dfSKP Singh 			 * While the LSM programs are BPF_MODIFY_RETURN-like
59739e4e01dfSKP Singh 			 * the check:
59749e4e01dfSKP Singh 			 *
59759e4e01dfSKP Singh 			 *	if (ret_type != 'int')
59769e4e01dfSKP Singh 			 *		return -EINVAL;
59779e4e01dfSKP Singh 			 *
59789e4e01dfSKP Singh 			 * is _not_ done here. This is still safe as LSM hooks
59799e4e01dfSKP Singh 			 * have only void and int return types.
59809e4e01dfSKP Singh 			 */
59815b92a28aSAlexei Starovoitov 			if (!t)
59825b92a28aSAlexei Starovoitov 				return true;
59835b92a28aSAlexei Starovoitov 			t = btf_type_by_id(btf, t->type);
5984f50b49a0SKP Singh 			break;
5985f50b49a0SKP Singh 		case BPF_MODIFY_RETURN:
59866ba43b76SKP Singh 			/* For now the BPF_MODIFY_RETURN can only be attached to
59876ba43b76SKP Singh 			 * functions that return an int.
59886ba43b76SKP Singh 			 */
59896ba43b76SKP Singh 			if (!t)
59906ba43b76SKP Singh 				return false;
59916ba43b76SKP Singh 
59926ba43b76SKP Singh 			t = btf_type_skip_modifiers(btf, t->type, NULL);
5993a9b59159SJohn Fastabend 			if (!btf_type_is_small_int(t)) {
59946ba43b76SKP Singh 				bpf_log(log,
59956ba43b76SKP Singh 					"ret type %s not allowed for fmod_ret\n",
5996571f9738SPeilin Ye 					btf_type_str(t));
59976ba43b76SKP Singh 				return false;
59986ba43b76SKP Singh 			}
5999f50b49a0SKP Singh 			break;
6000f50b49a0SKP Singh 		default:
6001fec56f58SAlexei Starovoitov 			bpf_log(log, "func '%s' doesn't have %d-th argument\n",
6002fec56f58SAlexei Starovoitov 				tname, arg + 1);
6003fec56f58SAlexei Starovoitov 			return false;
6004f50b49a0SKP Singh 		}
6005fec56f58SAlexei Starovoitov 	} else {
60065b92a28aSAlexei Starovoitov 		if (!t)
6007523a4cf4SDmitrii Banshchikov 			/* Default prog with MAX_BPF_FUNC_REG_ARGS args */
60085b92a28aSAlexei Starovoitov 			return true;
60095b92a28aSAlexei Starovoitov 		t = btf_type_by_id(btf, args[arg].type);
6010fec56f58SAlexei Starovoitov 	}
6011f50b49a0SKP Singh 
60129e15db66SAlexei Starovoitov 	/* skip modifiers */
60139e15db66SAlexei Starovoitov 	while (btf_type_is_modifier(t))
60145b92a28aSAlexei Starovoitov 		t = btf_type_by_id(btf, t->type);
6015720e6a43SYonghong Song 	if (btf_type_is_small_int(t) || btf_is_any_enum(t) || __btf_type_is_struct(t))
60169e15db66SAlexei Starovoitov 		/* accessing a scalar */
60179e15db66SAlexei Starovoitov 		return true;
60189e15db66SAlexei Starovoitov 	if (!btf_type_is_ptr(t)) {
60199e15db66SAlexei Starovoitov 		bpf_log(log,
602038207291SMartin KaFai Lau 			"func '%s' arg%d '%s' has type %s. Only pointer access is allowed\n",
60219e15db66SAlexei Starovoitov 			tname, arg,
60225b92a28aSAlexei Starovoitov 			__btf_name_by_offset(btf, t->name_off),
6023571f9738SPeilin Ye 			btf_type_str(t));
60249e15db66SAlexei Starovoitov 		return false;
60259e15db66SAlexei Starovoitov 	}
6026afbf21dcSYonghong Song 
6027*c7e1962aSKumar Kartikeya Dwivedi 	if (size != sizeof(u64)) {
6028*c7e1962aSKumar Kartikeya Dwivedi 		bpf_log(log, "func '%s' size %d must be 8\n",
6029*c7e1962aSKumar Kartikeya Dwivedi 			tname, size);
6030*c7e1962aSKumar Kartikeya Dwivedi 		return false;
6031*c7e1962aSKumar Kartikeya Dwivedi 	}
6032*c7e1962aSKumar Kartikeya Dwivedi 
6033afbf21dcSYonghong Song 	/* check for PTR_TO_RDONLY_BUF_OR_NULL or PTR_TO_RDWR_BUF_OR_NULL */
6034afbf21dcSYonghong Song 	for (i = 0; i < prog->aux->ctx_arg_info_size; i++) {
6035afbf21dcSYonghong Song 		const struct bpf_ctx_arg_aux *ctx_arg_info = &prog->aux->ctx_arg_info[i];
6036c25b2ae1SHao Luo 		u32 type, flag;
6037afbf21dcSYonghong Song 
6038c25b2ae1SHao Luo 		type = base_type(ctx_arg_info->reg_type);
6039c25b2ae1SHao Luo 		flag = type_flag(ctx_arg_info->reg_type);
604020b2aff4SHao Luo 		if (ctx_arg_info->offset == off && type == PTR_TO_BUF &&
6041c25b2ae1SHao Luo 		    (flag & PTR_MAYBE_NULL)) {
6042afbf21dcSYonghong Song 			info->reg_type = ctx_arg_info->reg_type;
6043afbf21dcSYonghong Song 			return true;
6044afbf21dcSYonghong Song 		}
6045afbf21dcSYonghong Song 	}
6046afbf21dcSYonghong Song 
60479e15db66SAlexei Starovoitov 	if (t->type == 0)
60489e15db66SAlexei Starovoitov 		/* This is a pointer to void.
60499e15db66SAlexei Starovoitov 		 * It is the same as scalar from the verifier safety pov.
60509e15db66SAlexei Starovoitov 		 * No further pointer walking is allowed.
60519e15db66SAlexei Starovoitov 		 */
60529e15db66SAlexei Starovoitov 		return true;
60539e15db66SAlexei Starovoitov 
6054bb6728d7SJiri Olsa 	if (is_int_ptr(btf, t))
605584ad7a7aSJiri Olsa 		return true;
605684ad7a7aSJiri Olsa 
60579e15db66SAlexei Starovoitov 	/* this is a pointer to another type */
60583c32cc1bSYonghong Song 	for (i = 0; i < prog->aux->ctx_arg_info_size; i++) {
60593c32cc1bSYonghong Song 		const struct bpf_ctx_arg_aux *ctx_arg_info = &prog->aux->ctx_arg_info[i];
60603c32cc1bSYonghong Song 
60613c32cc1bSYonghong Song 		if (ctx_arg_info->offset == off) {
6062d3621642SYonghong Song 			if (!ctx_arg_info->btf_id) {
6063d3621642SYonghong Song 				bpf_log(log,"invalid btf_id for context argument offset %u\n", off);
6064d3621642SYonghong Song 				return false;
6065d3621642SYonghong Song 			}
6066d3621642SYonghong Song 
60673c32cc1bSYonghong Song 			info->reg_type = ctx_arg_info->reg_type;
606822dc4a0fSAndrii Nakryiko 			info->btf = btf_vmlinux;
6069951cf368SYonghong Song 			info->btf_id = ctx_arg_info->btf_id;
6070951cf368SYonghong Song 			return true;
60713c32cc1bSYonghong Song 		}
60723c32cc1bSYonghong Song 	}
60739e15db66SAlexei Starovoitov 
6074951cf368SYonghong Song 	info->reg_type = PTR_TO_BTF_ID;
6075c6b0337fSAlexei Starovoitov 	if (prog_args_trusted(prog))
60763f00c523SDavid Vernet 		info->reg_type |= PTR_TRUSTED;
60773f00c523SDavid Vernet 
60785b92a28aSAlexei Starovoitov 	if (tgt_prog) {
607943bc2874SToke Høiland-Jørgensen 		enum bpf_prog_type tgt_type;
608043bc2874SToke Høiland-Jørgensen 
608143bc2874SToke Høiland-Jørgensen 		if (tgt_prog->type == BPF_PROG_TYPE_EXT)
608243bc2874SToke Høiland-Jørgensen 			tgt_type = tgt_prog->aux->saved_dst_prog_type;
608343bc2874SToke Høiland-Jørgensen 		else
608443bc2874SToke Høiland-Jørgensen 			tgt_type = tgt_prog->type;
608543bc2874SToke Høiland-Jørgensen 
608643bc2874SToke Høiland-Jørgensen 		ret = btf_translate_to_vmlinux(log, btf, t, tgt_type, arg);
60875b92a28aSAlexei Starovoitov 		if (ret > 0) {
608822dc4a0fSAndrii Nakryiko 			info->btf = btf_vmlinux;
60895b92a28aSAlexei Starovoitov 			info->btf_id = ret;
60905b92a28aSAlexei Starovoitov 			return true;
60915b92a28aSAlexei Starovoitov 		} else {
60925b92a28aSAlexei Starovoitov 			return false;
60935b92a28aSAlexei Starovoitov 		}
60945b92a28aSAlexei Starovoitov 	}
6095275517ffSMartin KaFai Lau 
609622dc4a0fSAndrii Nakryiko 	info->btf = btf;
6097275517ffSMartin KaFai Lau 	info->btf_id = t->type;
60985b92a28aSAlexei Starovoitov 	t = btf_type_by_id(btf, t->type);
6099c6f1bfe8SYonghong Song 
6100c6f1bfe8SYonghong Song 	if (btf_type_is_type_tag(t)) {
6101c6f1bfe8SYonghong Song 		tag_value = __btf_name_by_offset(btf, t->name_off);
6102c6f1bfe8SYonghong Song 		if (strcmp(tag_value, "user") == 0)
6103c6f1bfe8SYonghong Song 			info->reg_type |= MEM_USER;
61045844101aSHao Luo 		if (strcmp(tag_value, "percpu") == 0)
61055844101aSHao Luo 			info->reg_type |= MEM_PERCPU;
6106c6f1bfe8SYonghong Song 	}
6107c6f1bfe8SYonghong Song 
61089e15db66SAlexei Starovoitov 	/* skip modifiers */
6109275517ffSMartin KaFai Lau 	while (btf_type_is_modifier(t)) {
6110275517ffSMartin KaFai Lau 		info->btf_id = t->type;
61115b92a28aSAlexei Starovoitov 		t = btf_type_by_id(btf, t->type);
6112275517ffSMartin KaFai Lau 	}
61139e15db66SAlexei Starovoitov 	if (!btf_type_is_struct(t)) {
61149e15db66SAlexei Starovoitov 		bpf_log(log,
611538207291SMartin KaFai Lau 			"func '%s' arg%d type %s is not a struct\n",
6116571f9738SPeilin Ye 			tname, arg, btf_type_str(t));
61179e15db66SAlexei Starovoitov 		return false;
61189e15db66SAlexei Starovoitov 	}
611938207291SMartin KaFai Lau 	bpf_log(log, "func '%s' arg%d has btf_id %d type %s '%s'\n",
6120571f9738SPeilin Ye 		tname, arg, info->btf_id, btf_type_str(t),
61215b92a28aSAlexei Starovoitov 		__btf_name_by_offset(btf, t->name_off));
61229e15db66SAlexei Starovoitov 	return true;
61239e15db66SAlexei Starovoitov }
61249e15db66SAlexei Starovoitov 
61251c6d28a6SJiri Olsa enum bpf_struct_walk_result {
61261c6d28a6SJiri Olsa 	/* < 0 error */
61271c6d28a6SJiri Olsa 	WALK_SCALAR = 0,
61281c6d28a6SJiri Olsa 	WALK_PTR,
61291c6d28a6SJiri Olsa 	WALK_STRUCT,
61301c6d28a6SJiri Olsa };
61311c6d28a6SJiri Olsa 
btf_struct_walk(struct bpf_verifier_log * log,const struct btf * btf,const struct btf_type * t,int off,int size,u32 * next_btf_id,enum bpf_type_flag * flag,const char ** field_name)613222dc4a0fSAndrii Nakryiko static int btf_struct_walk(struct bpf_verifier_log *log, const struct btf *btf,
61339e15db66SAlexei Starovoitov 			   const struct btf_type *t, int off, int size,
613463260df1SAlexei Starovoitov 			   u32 *next_btf_id, enum bpf_type_flag *flag,
613563260df1SAlexei Starovoitov 			   const char **field_name)
61369e15db66SAlexei Starovoitov {
61377e3617a7SMartin KaFai Lau 	u32 i, moff, mtrue_end, msize = 0, total_nelems = 0;
61387e3617a7SMartin KaFai Lau 	const struct btf_type *mtype, *elem_type = NULL;
61399e15db66SAlexei Starovoitov 	const struct btf_member *member;
6140c6f1bfe8SYonghong Song 	const char *tname, *mname, *tag_value;
61411c6d28a6SJiri Olsa 	u32 vlen, elem_id, mid;
61429e15db66SAlexei Starovoitov 
61439e15db66SAlexei Starovoitov again:
6144819d4342SStanislav Fomichev 	if (btf_type_is_modifier(t))
6145819d4342SStanislav Fomichev 		t = btf_type_skip_modifiers(btf, t->type, NULL);
614622dc4a0fSAndrii Nakryiko 	tname = __btf_name_by_offset(btf, t->name_off);
61479e15db66SAlexei Starovoitov 	if (!btf_type_is_struct(t)) {
6148275517ffSMartin KaFai Lau 		bpf_log(log, "Type '%s' is not a struct\n", tname);
61499e15db66SAlexei Starovoitov 		return -EINVAL;
61509e15db66SAlexei Starovoitov 	}
61519e15db66SAlexei Starovoitov 
61529c5f8a10SYonghong Song 	vlen = btf_type_vlen(t);
61537ce4dc3eSYafang Shao 	if (BTF_INFO_KIND(t->info) == BTF_KIND_UNION && vlen != 1 && !(*flag & PTR_UNTRUSTED))
61547ce4dc3eSYafang Shao 		/*
61557ce4dc3eSYafang Shao 		 * walking unions yields untrusted pointers
61567ce4dc3eSYafang Shao 		 * with exception of __bpf_md_ptr and other
61577ce4dc3eSYafang Shao 		 * unions with a single member
61587ce4dc3eSYafang Shao 		 */
61597ce4dc3eSYafang Shao 		*flag |= PTR_UNTRUSTED;
61607ce4dc3eSYafang Shao 
6161976aba00SMartin KaFai Lau 	if (off + size > t->size) {
61629c5f8a10SYonghong Song 		/* If the last element is a variable size array, we may
61639c5f8a10SYonghong Song 		 * need to relax the rule.
61649c5f8a10SYonghong Song 		 */
61659c5f8a10SYonghong Song 		struct btf_array *array_elem;
61669c5f8a10SYonghong Song 
61679c5f8a10SYonghong Song 		if (vlen == 0)
61689c5f8a10SYonghong Song 			goto error;
61699c5f8a10SYonghong Song 
61709c5f8a10SYonghong Song 		member = btf_type_member(t) + vlen - 1;
617122dc4a0fSAndrii Nakryiko 		mtype = btf_type_skip_modifiers(btf, member->type,
61729c5f8a10SYonghong Song 						NULL);
61739c5f8a10SYonghong Song 		if (!btf_type_is_array(mtype))
61749c5f8a10SYonghong Song 			goto error;
61759c5f8a10SYonghong Song 
61769c5f8a10SYonghong Song 		array_elem = (struct btf_array *)(mtype + 1);
61779c5f8a10SYonghong Song 		if (array_elem->nelems != 0)
61789c5f8a10SYonghong Song 			goto error;
61799c5f8a10SYonghong Song 
61808293eb99SAlexei Starovoitov 		moff = __btf_member_bit_offset(t, member) / 8;
61819c5f8a10SYonghong Song 		if (off < moff)
61829c5f8a10SYonghong Song 			goto error;
61839c5f8a10SYonghong Song 
61842569c7b8SFeng Zhou 		/* allow structure and integer */
618522dc4a0fSAndrii Nakryiko 		t = btf_type_skip_modifiers(btf, array_elem->type,
6186dafe58fcSJiri Olsa 					    NULL);
61872569c7b8SFeng Zhou 
61882569c7b8SFeng Zhou 		if (btf_type_is_int(t))
61892569c7b8SFeng Zhou 			return WALK_SCALAR;
61902569c7b8SFeng Zhou 
6191dafe58fcSJiri Olsa 		if (!btf_type_is_struct(t))
61929c5f8a10SYonghong Song 			goto error;
61939c5f8a10SYonghong Song 
6194dafe58fcSJiri Olsa 		off = (off - moff) % t->size;
6195dafe58fcSJiri Olsa 		goto again;
61969c5f8a10SYonghong Song 
61979c5f8a10SYonghong Song error:
6198976aba00SMartin KaFai Lau 		bpf_log(log, "access beyond struct %s at off %u size %u\n",
6199976aba00SMartin KaFai Lau 			tname, off, size);
6200976aba00SMartin KaFai Lau 		return -EACCES;
6201976aba00SMartin KaFai Lau 	}
62029e15db66SAlexei Starovoitov 
6203976aba00SMartin KaFai Lau 	for_each_member(i, t, member) {
62047e3617a7SMartin KaFai Lau 		/* offset of the field in bytes */
62058293eb99SAlexei Starovoitov 		moff = __btf_member_bit_offset(t, member) / 8;
62067e3617a7SMartin KaFai Lau 		if (off + size <= moff)
62079e15db66SAlexei Starovoitov 			/* won't find anything, field is already too far */
62089e15db66SAlexei Starovoitov 			break;
6209976aba00SMartin KaFai Lau 
62108293eb99SAlexei Starovoitov 		if (__btf_member_bitfield_size(t, member)) {
62118293eb99SAlexei Starovoitov 			u32 end_bit = __btf_member_bit_offset(t, member) +
62128293eb99SAlexei Starovoitov 				__btf_member_bitfield_size(t, member);
6213976aba00SMartin KaFai Lau 
6214976aba00SMartin KaFai Lau 			/* off <= moff instead of off == moff because clang
6215976aba00SMartin KaFai Lau 			 * does not generate a BTF member for anonymous
6216976aba00SMartin KaFai Lau 			 * bitfield like the ":16" here:
6217976aba00SMartin KaFai Lau 			 * struct {
6218976aba00SMartin KaFai Lau 			 *	int :16;
6219976aba00SMartin KaFai Lau 			 *	int x:8;
6220976aba00SMartin KaFai Lau 			 * };
6221976aba00SMartin KaFai Lau 			 */
6222976aba00SMartin KaFai Lau 			if (off <= moff &&
6223976aba00SMartin KaFai Lau 			    BITS_ROUNDUP_BYTES(end_bit) <= off + size)
62241c6d28a6SJiri Olsa 				return WALK_SCALAR;
6225976aba00SMartin KaFai Lau 
6226976aba00SMartin KaFai Lau 			/* off may be accessing a following member
6227976aba00SMartin KaFai Lau 			 *
6228976aba00SMartin KaFai Lau 			 * or
6229976aba00SMartin KaFai Lau 			 *
6230976aba00SMartin KaFai Lau 			 * Doing partial access at either end of this
6231976aba00SMartin KaFai Lau 			 * bitfield.  Continue on this case also to
6232976aba00SMartin KaFai Lau 			 * treat it as not accessing this bitfield
6233976aba00SMartin KaFai Lau 			 * and eventually error out as field not
6234976aba00SMartin KaFai Lau 			 * found to keep it simple.
6235976aba00SMartin KaFai Lau 			 * It could be relaxed if there was a legit
6236976aba00SMartin KaFai Lau 			 * partial access case later.
6237976aba00SMartin KaFai Lau 			 */
6238976aba00SMartin KaFai Lau 			continue;
6239976aba00SMartin KaFai Lau 		}
6240976aba00SMartin KaFai Lau 
62417e3617a7SMartin KaFai Lau 		/* In case of "off" is pointing to holes of a struct */
62427e3617a7SMartin KaFai Lau 		if (off < moff)
6243976aba00SMartin KaFai Lau 			break;
62449e15db66SAlexei Starovoitov 
62459e15db66SAlexei Starovoitov 		/* type of the field */
62461c6d28a6SJiri Olsa 		mid = member->type;
624722dc4a0fSAndrii Nakryiko 		mtype = btf_type_by_id(btf, member->type);
624822dc4a0fSAndrii Nakryiko 		mname = __btf_name_by_offset(btf, member->name_off);
62499e15db66SAlexei Starovoitov 
625022dc4a0fSAndrii Nakryiko 		mtype = __btf_resolve_size(btf, mtype, &msize,
62511c6d28a6SJiri Olsa 					   &elem_type, &elem_id, &total_nelems,
62521c6d28a6SJiri Olsa 					   &mid);
62537e3617a7SMartin KaFai Lau 		if (IS_ERR(mtype)) {
62549e15db66SAlexei Starovoitov 			bpf_log(log, "field %s doesn't have size\n", mname);
62559e15db66SAlexei Starovoitov 			return -EFAULT;
62569e15db66SAlexei Starovoitov 		}
62577e3617a7SMartin KaFai Lau 
62587e3617a7SMartin KaFai Lau 		mtrue_end = moff + msize;
62597e3617a7SMartin KaFai Lau 		if (off >= mtrue_end)
62609e15db66SAlexei Starovoitov 			/* no overlap with member, keep iterating */
62619e15db66SAlexei Starovoitov 			continue;
62627e3617a7SMartin KaFai Lau 
62637e3617a7SMartin KaFai Lau 		if (btf_type_is_array(mtype)) {
62647e3617a7SMartin KaFai Lau 			u32 elem_idx;
62657e3617a7SMartin KaFai Lau 
62666298399bSJiri Olsa 			/* __btf_resolve_size() above helps to
62677e3617a7SMartin KaFai Lau 			 * linearize a multi-dimensional array.
62687e3617a7SMartin KaFai Lau 			 *
62697e3617a7SMartin KaFai Lau 			 * The logic here is treating an array
62707e3617a7SMartin KaFai Lau 			 * in a struct as the following way:
62717e3617a7SMartin KaFai Lau 			 *
62727e3617a7SMartin KaFai Lau 			 * struct outer {
62737e3617a7SMartin KaFai Lau 			 *	struct inner array[2][2];
62747e3617a7SMartin KaFai Lau 			 * };
62757e3617a7SMartin KaFai Lau 			 *
62767e3617a7SMartin KaFai Lau 			 * looks like:
62777e3617a7SMartin KaFai Lau 			 *
62787e3617a7SMartin KaFai Lau 			 * struct outer {
62797e3617a7SMartin KaFai Lau 			 *	struct inner array_elem0;
62807e3617a7SMartin KaFai Lau 			 *	struct inner array_elem1;
62817e3617a7SMartin KaFai Lau 			 *	struct inner array_elem2;
62827e3617a7SMartin KaFai Lau 			 *	struct inner array_elem3;
62837e3617a7SMartin KaFai Lau 			 * };
62847e3617a7SMartin KaFai Lau 			 *
62857e3617a7SMartin KaFai Lau 			 * When accessing outer->array[1][0], it moves
62867e3617a7SMartin KaFai Lau 			 * moff to "array_elem2", set mtype to
62877e3617a7SMartin KaFai Lau 			 * "struct inner", and msize also becomes
62887e3617a7SMartin KaFai Lau 			 * sizeof(struct inner).  Then most of the
62897e3617a7SMartin KaFai Lau 			 * remaining logic will fall through without
62907e3617a7SMartin KaFai Lau 			 * caring the current member is an array or
62917e3617a7SMartin KaFai Lau 			 * not.
62927e3617a7SMartin KaFai Lau 			 *
62937e3617a7SMartin KaFai Lau 			 * Unlike mtype/msize/moff, mtrue_end does not
62947e3617a7SMartin KaFai Lau 			 * change.  The naming difference ("_true") tells
62957e3617a7SMartin KaFai Lau 			 * that it is not always corresponding to
62967e3617a7SMartin KaFai Lau 			 * the current mtype/msize/moff.
62977e3617a7SMartin KaFai Lau 			 * It is the true end of the current
62987e3617a7SMartin KaFai Lau 			 * member (i.e. array in this case).  That
62997e3617a7SMartin KaFai Lau 			 * will allow an int array to be accessed like
63007e3617a7SMartin KaFai Lau 			 * a scratch space,
63017e3617a7SMartin KaFai Lau 			 * i.e. allow access beyond the size of
63027e3617a7SMartin KaFai Lau 			 *      the array's element as long as it is
63037e3617a7SMartin KaFai Lau 			 *      within the mtrue_end boundary.
63047e3617a7SMartin KaFai Lau 			 */
63057e3617a7SMartin KaFai Lau 
63067e3617a7SMartin KaFai Lau 			/* skip empty array */
63077e3617a7SMartin KaFai Lau 			if (moff == mtrue_end)
63087e3617a7SMartin KaFai Lau 				continue;
63097e3617a7SMartin KaFai Lau 
63107e3617a7SMartin KaFai Lau 			msize /= total_nelems;
63117e3617a7SMartin KaFai Lau 			elem_idx = (off - moff) / msize;
63127e3617a7SMartin KaFai Lau 			moff += elem_idx * msize;
63137e3617a7SMartin KaFai Lau 			mtype = elem_type;
63141c6d28a6SJiri Olsa 			mid = elem_id;
63157e3617a7SMartin KaFai Lau 		}
63167e3617a7SMartin KaFai Lau 
63179e15db66SAlexei Starovoitov 		/* the 'off' we're looking for is either equal to start
63189e15db66SAlexei Starovoitov 		 * of this field or inside of this struct
63199e15db66SAlexei Starovoitov 		 */
63209e15db66SAlexei Starovoitov 		if (btf_type_is_struct(mtype)) {
63219e15db66SAlexei Starovoitov 			/* our field must be inside that union or struct */
63229e15db66SAlexei Starovoitov 			t = mtype;
63239e15db66SAlexei Starovoitov 
63241c6d28a6SJiri Olsa 			/* return if the offset matches the member offset */
63251c6d28a6SJiri Olsa 			if (off == moff) {
63261c6d28a6SJiri Olsa 				*next_btf_id = mid;
63271c6d28a6SJiri Olsa 				return WALK_STRUCT;
63281c6d28a6SJiri Olsa 			}
63291c6d28a6SJiri Olsa 
63309e15db66SAlexei Starovoitov 			/* adjust offset we're looking for */
63317e3617a7SMartin KaFai Lau 			off -= moff;
63329e15db66SAlexei Starovoitov 			goto again;
63339e15db66SAlexei Starovoitov 		}
63349e15db66SAlexei Starovoitov 
63359e15db66SAlexei Starovoitov 		if (btf_type_is_ptr(mtype)) {
6336c6f1bfe8SYonghong Song 			const struct btf_type *stype, *t;
6337c6f1bfe8SYonghong Song 			enum bpf_type_flag tmp_flag = 0;
6338257af63dSAlexei Starovoitov 			u32 id;
63399e15db66SAlexei Starovoitov 
63407e3617a7SMartin KaFai Lau 			if (msize != size || off != moff) {
63417e3617a7SMartin KaFai Lau 				bpf_log(log,
63427e3617a7SMartin KaFai Lau 					"cannot access ptr member %s with moff %u in struct %s with off %u size %u\n",
63437e3617a7SMartin KaFai Lau 					mname, moff, tname, off, size);
63447e3617a7SMartin KaFai Lau 				return -EACCES;
63457e3617a7SMartin KaFai Lau 			}
6346c6f1bfe8SYonghong Song 
63475844101aSHao Luo 			/* check type tag */
6348c6f1bfe8SYonghong Song 			t = btf_type_by_id(btf, mtype->type);
6349c6f1bfe8SYonghong Song 			if (btf_type_is_type_tag(t)) {
6350c6f1bfe8SYonghong Song 				tag_value = __btf_name_by_offset(btf, t->name_off);
63515844101aSHao Luo 				/* check __user tag */
6352c6f1bfe8SYonghong Song 				if (strcmp(tag_value, "user") == 0)
6353c6f1bfe8SYonghong Song 					tmp_flag = MEM_USER;
63545844101aSHao Luo 				/* check __percpu tag */
63555844101aSHao Luo 				if (strcmp(tag_value, "percpu") == 0)
63565844101aSHao Luo 					tmp_flag = MEM_PERCPU;
63579bb00b28SYonghong Song 				/* check __rcu tag */
63589bb00b28SYonghong Song 				if (strcmp(tag_value, "rcu") == 0)
63599bb00b28SYonghong Song 					tmp_flag = MEM_RCU;
6360c6f1bfe8SYonghong Song 			}
6361c6f1bfe8SYonghong Song 
636222dc4a0fSAndrii Nakryiko 			stype = btf_type_skip_modifiers(btf, mtype->type, &id);
63639e15db66SAlexei Starovoitov 			if (btf_type_is_struct(stype)) {
6364257af63dSAlexei Starovoitov 				*next_btf_id = id;
63656fcd486bSAlexei Starovoitov 				*flag |= tmp_flag;
636663260df1SAlexei Starovoitov 				if (field_name)
636763260df1SAlexei Starovoitov 					*field_name = mname;
63681c6d28a6SJiri Olsa 				return WALK_PTR;
63699e15db66SAlexei Starovoitov 			}
63709e15db66SAlexei Starovoitov 		}
63717e3617a7SMartin KaFai Lau 
63727e3617a7SMartin KaFai Lau 		/* Allow more flexible access within an int as long as
63737e3617a7SMartin KaFai Lau 		 * it is within mtrue_end.
63747e3617a7SMartin KaFai Lau 		 * Since mtrue_end could be the end of an array,
63757e3617a7SMartin KaFai Lau 		 * that also allows using an array of int as a scratch
63767e3617a7SMartin KaFai Lau 		 * space. e.g. skb->cb[].
63777e3617a7SMartin KaFai Lau 		 */
637833937607SYafang Shao 		if (off + size > mtrue_end && !(*flag & PTR_UNTRUSTED)) {
63797e3617a7SMartin KaFai Lau 			bpf_log(log,
63807e3617a7SMartin KaFai Lau 				"access beyond the end of member %s (mend:%u) in struct %s with off %u size %u\n",
63817e3617a7SMartin KaFai Lau 				mname, mtrue_end, tname, off, size);
63827e3617a7SMartin KaFai Lau 			return -EACCES;
63837e3617a7SMartin KaFai Lau 		}
63847e3617a7SMartin KaFai Lau 
63851c6d28a6SJiri Olsa 		return WALK_SCALAR;
63869e15db66SAlexei Starovoitov 	}
63879e15db66SAlexei Starovoitov 	bpf_log(log, "struct %s doesn't have field at offset %d\n", tname, off);
63889e15db66SAlexei Starovoitov 	return -EINVAL;
63899e15db66SAlexei Starovoitov }
63909e15db66SAlexei Starovoitov 
btf_struct_access(struct bpf_verifier_log * log,const struct bpf_reg_state * reg,int off,int size,enum bpf_access_type atype __maybe_unused,u32 * next_btf_id,enum bpf_type_flag * flag,const char ** field_name)63916728aea7SKumar Kartikeya Dwivedi int btf_struct_access(struct bpf_verifier_log *log,
63926728aea7SKumar Kartikeya Dwivedi 		      const struct bpf_reg_state *reg,
63936728aea7SKumar Kartikeya Dwivedi 		      int off, int size, enum bpf_access_type atype __maybe_unused,
639463260df1SAlexei Starovoitov 		      u32 *next_btf_id, enum bpf_type_flag *flag,
639563260df1SAlexei Starovoitov 		      const char **field_name)
63961c6d28a6SJiri Olsa {
63976728aea7SKumar Kartikeya Dwivedi 	const struct btf *btf = reg->btf;
6398c6f1bfe8SYonghong Song 	enum bpf_type_flag tmp_flag = 0;
63996728aea7SKumar Kartikeya Dwivedi 	const struct btf_type *t;
64006728aea7SKumar Kartikeya Dwivedi 	u32 id = reg->btf_id;
64011c6d28a6SJiri Olsa 	int err;
64021c6d28a6SJiri Olsa 
64038ffa5cc1SKumar Kartikeya Dwivedi 	while (type_is_alloc(reg->type)) {
64048ffa5cc1SKumar Kartikeya Dwivedi 		struct btf_struct_meta *meta;
64058ffa5cc1SKumar Kartikeya Dwivedi 		struct btf_record *rec;
64068ffa5cc1SKumar Kartikeya Dwivedi 		int i;
64078ffa5cc1SKumar Kartikeya Dwivedi 
64088ffa5cc1SKumar Kartikeya Dwivedi 		meta = btf_find_struct_meta(btf, id);
64098ffa5cc1SKumar Kartikeya Dwivedi 		if (!meta)
64108ffa5cc1SKumar Kartikeya Dwivedi 			break;
64118ffa5cc1SKumar Kartikeya Dwivedi 		rec = meta->record;
64128ffa5cc1SKumar Kartikeya Dwivedi 		for (i = 0; i < rec->cnt; i++) {
64138ffa5cc1SKumar Kartikeya Dwivedi 			struct btf_field *field = &rec->fields[i];
64148ffa5cc1SKumar Kartikeya Dwivedi 			u32 offset = field->offset;
64158ffa5cc1SKumar Kartikeya Dwivedi 			if (off < offset + btf_field_type_size(field->type) && offset < off + size) {
64168ffa5cc1SKumar Kartikeya Dwivedi 				bpf_log(log,
64178ffa5cc1SKumar Kartikeya Dwivedi 					"direct access to %s is disallowed\n",
64188ffa5cc1SKumar Kartikeya Dwivedi 					btf_field_type_name(field->type));
64198ffa5cc1SKumar Kartikeya Dwivedi 				return -EACCES;
64208ffa5cc1SKumar Kartikeya Dwivedi 			}
64218ffa5cc1SKumar Kartikeya Dwivedi 		}
64228ffa5cc1SKumar Kartikeya Dwivedi 		break;
64238ffa5cc1SKumar Kartikeya Dwivedi 	}
64248ffa5cc1SKumar Kartikeya Dwivedi 
64256728aea7SKumar Kartikeya Dwivedi 	t = btf_type_by_id(btf, id);
64261c6d28a6SJiri Olsa 	do {
642763260df1SAlexei Starovoitov 		err = btf_struct_walk(log, btf, t, off, size, &id, &tmp_flag, field_name);
64281c6d28a6SJiri Olsa 
64291c6d28a6SJiri Olsa 		switch (err) {
64301c6d28a6SJiri Olsa 		case WALK_PTR:
6431282de143SKumar Kartikeya Dwivedi 			/* For local types, the destination register cannot
6432282de143SKumar Kartikeya Dwivedi 			 * become a pointer again.
6433282de143SKumar Kartikeya Dwivedi 			 */
6434282de143SKumar Kartikeya Dwivedi 			if (type_is_alloc(reg->type))
6435282de143SKumar Kartikeya Dwivedi 				return SCALAR_VALUE;
64361c6d28a6SJiri Olsa 			/* If we found the pointer or scalar on t+off,
64371c6d28a6SJiri Olsa 			 * we're done.
64381c6d28a6SJiri Olsa 			 */
64391c6d28a6SJiri Olsa 			*next_btf_id = id;
6440c6f1bfe8SYonghong Song 			*flag = tmp_flag;
64411c6d28a6SJiri Olsa 			return PTR_TO_BTF_ID;
64421c6d28a6SJiri Olsa 		case WALK_SCALAR:
64431c6d28a6SJiri Olsa 			return SCALAR_VALUE;
64441c6d28a6SJiri Olsa 		case WALK_STRUCT:
64451c6d28a6SJiri Olsa 			/* We found nested struct, so continue the search
64461c6d28a6SJiri Olsa 			 * by diving in it. At this point the offset is
64471c6d28a6SJiri Olsa 			 * aligned with the new type, so set it to 0.
64481c6d28a6SJiri Olsa 			 */
644922dc4a0fSAndrii Nakryiko 			t = btf_type_by_id(btf, id);
64501c6d28a6SJiri Olsa 			off = 0;
64511c6d28a6SJiri Olsa 			break;
64521c6d28a6SJiri Olsa 		default:
64531c6d28a6SJiri Olsa 			/* It's either error or unknown return value..
64541c6d28a6SJiri Olsa 			 * scream and leave.
64551c6d28a6SJiri Olsa 			 */
64561c6d28a6SJiri Olsa 			if (WARN_ONCE(err > 0, "unknown btf_struct_walk return value"))
64571c6d28a6SJiri Olsa 				return -EINVAL;
64581c6d28a6SJiri Olsa 			return err;
64591c6d28a6SJiri Olsa 		}
64601c6d28a6SJiri Olsa 	} while (t);
64611c6d28a6SJiri Olsa 
64621c6d28a6SJiri Olsa 	return -EINVAL;
64631c6d28a6SJiri Olsa }
64641c6d28a6SJiri Olsa 
646522dc4a0fSAndrii Nakryiko /* Check that two BTF types, each specified as an BTF object + id, are exactly
646622dc4a0fSAndrii Nakryiko  * the same. Trivial ID check is not enough due to module BTFs, because we can
646722dc4a0fSAndrii Nakryiko  * end up with two different module BTFs, but IDs point to the common type in
646822dc4a0fSAndrii Nakryiko  * vmlinux BTF.
646922dc4a0fSAndrii Nakryiko  */
btf_types_are_same(const struct btf * btf1,u32 id1,const struct btf * btf2,u32 id2)647000b85860SKumar Kartikeya Dwivedi bool btf_types_are_same(const struct btf *btf1, u32 id1,
647122dc4a0fSAndrii Nakryiko 			const struct btf *btf2, u32 id2)
647222dc4a0fSAndrii Nakryiko {
647322dc4a0fSAndrii Nakryiko 	if (id1 != id2)
647422dc4a0fSAndrii Nakryiko 		return false;
647522dc4a0fSAndrii Nakryiko 	if (btf1 == btf2)
647622dc4a0fSAndrii Nakryiko 		return true;
647722dc4a0fSAndrii Nakryiko 	return btf_type_by_id(btf1, id1) == btf_type_by_id(btf2, id2);
647822dc4a0fSAndrii Nakryiko }
647922dc4a0fSAndrii Nakryiko 
btf_struct_ids_match(struct bpf_verifier_log * log,const struct btf * btf,u32 id,int off,const struct btf * need_btf,u32 need_type_id,bool strict)6480faaf4a79SJiri Olsa bool btf_struct_ids_match(struct bpf_verifier_log *log,
648122dc4a0fSAndrii Nakryiko 			  const struct btf *btf, u32 id, int off,
64822ab3b380SKumar Kartikeya Dwivedi 			  const struct btf *need_btf, u32 need_type_id,
64832ab3b380SKumar Kartikeya Dwivedi 			  bool strict)
6484faaf4a79SJiri Olsa {
6485faaf4a79SJiri Olsa 	const struct btf_type *type;
64867ce4dc3eSYafang Shao 	enum bpf_type_flag flag = 0;
6487faaf4a79SJiri Olsa 	int err;
6488faaf4a79SJiri Olsa 
6489faaf4a79SJiri Olsa 	/* Are we already done? */
649022dc4a0fSAndrii Nakryiko 	if (off == 0 && btf_types_are_same(btf, id, need_btf, need_type_id))
6491faaf4a79SJiri Olsa 		return true;
64922ab3b380SKumar Kartikeya Dwivedi 	/* In case of strict type match, we do not walk struct, the top level
64932ab3b380SKumar Kartikeya Dwivedi 	 * type match must succeed. When strict is true, off should have already
64942ab3b380SKumar Kartikeya Dwivedi 	 * been 0.
64952ab3b380SKumar Kartikeya Dwivedi 	 */
64962ab3b380SKumar Kartikeya Dwivedi 	if (strict)
64972ab3b380SKumar Kartikeya Dwivedi 		return false;
6498faaf4a79SJiri Olsa again:
649922dc4a0fSAndrii Nakryiko 	type = btf_type_by_id(btf, id);
6500faaf4a79SJiri Olsa 	if (!type)
6501faaf4a79SJiri Olsa 		return false;
650263260df1SAlexei Starovoitov 	err = btf_struct_walk(log, btf, type, off, 1, &id, &flag, NULL);
6503faaf4a79SJiri Olsa 	if (err != WALK_STRUCT)
6504faaf4a79SJiri Olsa 		return false;
6505faaf4a79SJiri Olsa 
6506faaf4a79SJiri Olsa 	/* We found nested struct object. If it matches
6507faaf4a79SJiri Olsa 	 * the requested ID, we're done. Otherwise let's
6508faaf4a79SJiri Olsa 	 * continue the search with offset 0 in the new
6509faaf4a79SJiri Olsa 	 * type.
6510faaf4a79SJiri Olsa 	 */
651122dc4a0fSAndrii Nakryiko 	if (!btf_types_are_same(btf, id, need_btf, need_type_id)) {
6512faaf4a79SJiri Olsa 		off = 0;
6513faaf4a79SJiri Olsa 		goto again;
6514faaf4a79SJiri Olsa 	}
6515faaf4a79SJiri Olsa 
6516faaf4a79SJiri Olsa 	return true;
6517faaf4a79SJiri Olsa }
6518faaf4a79SJiri Olsa 
__get_type_size(struct btf * btf,u32 btf_id,const struct btf_type ** ret_type)6519fec56f58SAlexei Starovoitov static int __get_type_size(struct btf *btf, u32 btf_id,
6520a00ed843SYonghong Song 			   const struct btf_type **ret_type)
6521fec56f58SAlexei Starovoitov {
6522fec56f58SAlexei Starovoitov 	const struct btf_type *t;
6523fec56f58SAlexei Starovoitov 
6524a00ed843SYonghong Song 	*ret_type = btf_type_by_id(btf, 0);
6525fec56f58SAlexei Starovoitov 	if (!btf_id)
6526fec56f58SAlexei Starovoitov 		/* void */
6527fec56f58SAlexei Starovoitov 		return 0;
6528fec56f58SAlexei Starovoitov 	t = btf_type_by_id(btf, btf_id);
6529fec56f58SAlexei Starovoitov 	while (t && btf_type_is_modifier(t))
6530fec56f58SAlexei Starovoitov 		t = btf_type_by_id(btf, t->type);
6531a00ed843SYonghong Song 	if (!t)
6532fec56f58SAlexei Starovoitov 		return -EINVAL;
6533a00ed843SYonghong Song 	*ret_type = t;
6534fec56f58SAlexei Starovoitov 	if (btf_type_is_ptr(t))
6535fec56f58SAlexei Starovoitov 		/* kernel size of pointer. Not BPF's size of pointer*/
6536fec56f58SAlexei Starovoitov 		return sizeof(void *);
6537720e6a43SYonghong Song 	if (btf_type_is_int(t) || btf_is_any_enum(t) || __btf_type_is_struct(t))
6538fec56f58SAlexei Starovoitov 		return t->size;
6539fec56f58SAlexei Starovoitov 	return -EINVAL;
6540fec56f58SAlexei Starovoitov }
6541fec56f58SAlexei Starovoitov 
__get_type_fmodel_flags(const struct btf_type * t)654249f67f39SIlya Leoshkevich static u8 __get_type_fmodel_flags(const struct btf_type *t)
654349f67f39SIlya Leoshkevich {
654449f67f39SIlya Leoshkevich 	u8 flags = 0;
654549f67f39SIlya Leoshkevich 
654649f67f39SIlya Leoshkevich 	if (__btf_type_is_struct(t))
654749f67f39SIlya Leoshkevich 		flags |= BTF_FMODEL_STRUCT_ARG;
654849f67f39SIlya Leoshkevich 	if (btf_type_is_signed_int(t))
654949f67f39SIlya Leoshkevich 		flags |= BTF_FMODEL_SIGNED_ARG;
655049f67f39SIlya Leoshkevich 
655149f67f39SIlya Leoshkevich 	return flags;
655249f67f39SIlya Leoshkevich }
655349f67f39SIlya Leoshkevich 
btf_distill_func_proto(struct bpf_verifier_log * log,struct btf * btf,const struct btf_type * func,const char * tname,struct btf_func_model * m)6554fec56f58SAlexei Starovoitov int btf_distill_func_proto(struct bpf_verifier_log *log,
6555fec56f58SAlexei Starovoitov 			   struct btf *btf,
6556fec56f58SAlexei Starovoitov 			   const struct btf_type *func,
6557fec56f58SAlexei Starovoitov 			   const char *tname,
6558fec56f58SAlexei Starovoitov 			   struct btf_func_model *m)
6559fec56f58SAlexei Starovoitov {
6560fec56f58SAlexei Starovoitov 	const struct btf_param *args;
6561fec56f58SAlexei Starovoitov 	const struct btf_type *t;
6562fec56f58SAlexei Starovoitov 	u32 i, nargs;
6563fec56f58SAlexei Starovoitov 	int ret;
6564fec56f58SAlexei Starovoitov 
65655b92a28aSAlexei Starovoitov 	if (!func) {
65665b92a28aSAlexei Starovoitov 		/* BTF function prototype doesn't match the verifier types.
6567523a4cf4SDmitrii Banshchikov 		 * Fall back to MAX_BPF_FUNC_REG_ARGS u64 args.
65685b92a28aSAlexei Starovoitov 		 */
6569720e6a43SYonghong Song 		for (i = 0; i < MAX_BPF_FUNC_REG_ARGS; i++) {
65705b92a28aSAlexei Starovoitov 			m->arg_size[i] = 8;
6571720e6a43SYonghong Song 			m->arg_flags[i] = 0;
6572720e6a43SYonghong Song 		}
65735b92a28aSAlexei Starovoitov 		m->ret_size = 8;
657449f67f39SIlya Leoshkevich 		m->ret_flags = 0;
6575523a4cf4SDmitrii Banshchikov 		m->nr_args = MAX_BPF_FUNC_REG_ARGS;
65765b92a28aSAlexei Starovoitov 		return 0;
65775b92a28aSAlexei Starovoitov 	}
6578fec56f58SAlexei Starovoitov 	args = (const struct btf_param *)(func + 1);
6579fec56f58SAlexei Starovoitov 	nargs = btf_type_vlen(func);
6580c29a4920SYuntao Wang 	if (nargs > MAX_BPF_FUNC_ARGS) {
6581fec56f58SAlexei Starovoitov 		bpf_log(log,
6582fec56f58SAlexei Starovoitov 			"The function %s has %d arguments. Too many.\n",
6583fec56f58SAlexei Starovoitov 			tname, nargs);
6584fec56f58SAlexei Starovoitov 		return -EINVAL;
6585fec56f58SAlexei Starovoitov 	}
6586fec56f58SAlexei Starovoitov 	ret = __get_type_size(btf, func->type, &t);
6587720e6a43SYonghong Song 	if (ret < 0 || __btf_type_is_struct(t)) {
6588fec56f58SAlexei Starovoitov 		bpf_log(log,
6589fec56f58SAlexei Starovoitov 			"The function %s return type %s is unsupported.\n",
6590571f9738SPeilin Ye 			tname, btf_type_str(t));
6591fec56f58SAlexei Starovoitov 		return -EINVAL;
6592fec56f58SAlexei Starovoitov 	}
6593fec56f58SAlexei Starovoitov 	m->ret_size = ret;
659449f67f39SIlya Leoshkevich 	m->ret_flags = __get_type_fmodel_flags(t);
6595fec56f58SAlexei Starovoitov 
6596fec56f58SAlexei Starovoitov 	for (i = 0; i < nargs; i++) {
659731379397SJiri Olsa 		if (i == nargs - 1 && args[i].type == 0) {
659831379397SJiri Olsa 			bpf_log(log,
659931379397SJiri Olsa 				"The function %s with variable args is unsupported.\n",
660031379397SJiri Olsa 				tname);
660131379397SJiri Olsa 			return -EINVAL;
660231379397SJiri Olsa 		}
6603fec56f58SAlexei Starovoitov 		ret = __get_type_size(btf, args[i].type, &t);
6604720e6a43SYonghong Song 
6605720e6a43SYonghong Song 		/* No support of struct argument size greater than 16 bytes */
6606720e6a43SYonghong Song 		if (ret < 0 || ret > 16) {
6607fec56f58SAlexei Starovoitov 			bpf_log(log,
6608fec56f58SAlexei Starovoitov 				"The function %s arg%d type %s is unsupported.\n",
6609571f9738SPeilin Ye 				tname, i, btf_type_str(t));
6610fec56f58SAlexei Starovoitov 			return -EINVAL;
6611fec56f58SAlexei Starovoitov 		}
661231379397SJiri Olsa 		if (ret == 0) {
661331379397SJiri Olsa 			bpf_log(log,
661431379397SJiri Olsa 				"The function %s has malformed void argument.\n",
661531379397SJiri Olsa 				tname);
661631379397SJiri Olsa 			return -EINVAL;
661731379397SJiri Olsa 		}
6618fec56f58SAlexei Starovoitov 		m->arg_size[i] = ret;
661949f67f39SIlya Leoshkevich 		m->arg_flags[i] = __get_type_fmodel_flags(t);
6620fec56f58SAlexei Starovoitov 	}
6621fec56f58SAlexei Starovoitov 	m->nr_args = nargs;
6622fec56f58SAlexei Starovoitov 	return 0;
6623fec56f58SAlexei Starovoitov }
6624fec56f58SAlexei Starovoitov 
6625be8704ffSAlexei Starovoitov /* Compare BTFs of two functions assuming only scalars and pointers to context.
6626be8704ffSAlexei Starovoitov  * t1 points to BTF_KIND_FUNC in btf1
6627be8704ffSAlexei Starovoitov  * t2 points to BTF_KIND_FUNC in btf2
6628be8704ffSAlexei Starovoitov  * Returns:
6629be8704ffSAlexei Starovoitov  * EINVAL - function prototype mismatch
6630be8704ffSAlexei Starovoitov  * EFAULT - verifier bug
6631be8704ffSAlexei Starovoitov  * 0 - 99% match. The last 1% is validated by the verifier.
6632be8704ffSAlexei Starovoitov  */
btf_check_func_type_match(struct bpf_verifier_log * log,struct btf * btf1,const struct btf_type * t1,struct btf * btf2,const struct btf_type * t2)66332bf0eb9bSHongbo Yao static int btf_check_func_type_match(struct bpf_verifier_log *log,
6634be8704ffSAlexei Starovoitov 				     struct btf *btf1, const struct btf_type *t1,
6635be8704ffSAlexei Starovoitov 				     struct btf *btf2, const struct btf_type *t2)
6636be8704ffSAlexei Starovoitov {
6637be8704ffSAlexei Starovoitov 	const struct btf_param *args1, *args2;
6638be8704ffSAlexei Starovoitov 	const char *fn1, *fn2, *s1, *s2;
6639be8704ffSAlexei Starovoitov 	u32 nargs1, nargs2, i;
6640be8704ffSAlexei Starovoitov 
6641be8704ffSAlexei Starovoitov 	fn1 = btf_name_by_offset(btf1, t1->name_off);
6642be8704ffSAlexei Starovoitov 	fn2 = btf_name_by_offset(btf2, t2->name_off);
6643be8704ffSAlexei Starovoitov 
6644be8704ffSAlexei Starovoitov 	if (btf_func_linkage(t1) != BTF_FUNC_GLOBAL) {
6645be8704ffSAlexei Starovoitov 		bpf_log(log, "%s() is not a global function\n", fn1);
6646be8704ffSAlexei Starovoitov 		return -EINVAL;
6647be8704ffSAlexei Starovoitov 	}
6648be8704ffSAlexei Starovoitov 	if (btf_func_linkage(t2) != BTF_FUNC_GLOBAL) {
6649be8704ffSAlexei Starovoitov 		bpf_log(log, "%s() is not a global function\n", fn2);
6650be8704ffSAlexei Starovoitov 		return -EINVAL;
6651be8704ffSAlexei Starovoitov 	}
6652be8704ffSAlexei Starovoitov 
6653be8704ffSAlexei Starovoitov 	t1 = btf_type_by_id(btf1, t1->type);
6654be8704ffSAlexei Starovoitov 	if (!t1 || !btf_type_is_func_proto(t1))
6655be8704ffSAlexei Starovoitov 		return -EFAULT;
6656be8704ffSAlexei Starovoitov 	t2 = btf_type_by_id(btf2, t2->type);
6657be8704ffSAlexei Starovoitov 	if (!t2 || !btf_type_is_func_proto(t2))
6658be8704ffSAlexei Starovoitov 		return -EFAULT;
6659be8704ffSAlexei Starovoitov 
6660be8704ffSAlexei Starovoitov 	args1 = (const struct btf_param *)(t1 + 1);
6661be8704ffSAlexei Starovoitov 	nargs1 = btf_type_vlen(t1);
6662be8704ffSAlexei Starovoitov 	args2 = (const struct btf_param *)(t2 + 1);
6663be8704ffSAlexei Starovoitov 	nargs2 = btf_type_vlen(t2);
6664be8704ffSAlexei Starovoitov 
6665be8704ffSAlexei Starovoitov 	if (nargs1 != nargs2) {
6666be8704ffSAlexei Starovoitov 		bpf_log(log, "%s() has %d args while %s() has %d args\n",
6667be8704ffSAlexei Starovoitov 			fn1, nargs1, fn2, nargs2);
6668be8704ffSAlexei Starovoitov 		return -EINVAL;
6669be8704ffSAlexei Starovoitov 	}
6670be8704ffSAlexei Starovoitov 
6671be8704ffSAlexei Starovoitov 	t1 = btf_type_skip_modifiers(btf1, t1->type, NULL);
6672be8704ffSAlexei Starovoitov 	t2 = btf_type_skip_modifiers(btf2, t2->type, NULL);
6673be8704ffSAlexei Starovoitov 	if (t1->info != t2->info) {
6674be8704ffSAlexei Starovoitov 		bpf_log(log,
6675be8704ffSAlexei Starovoitov 			"Return type %s of %s() doesn't match type %s of %s()\n",
6676be8704ffSAlexei Starovoitov 			btf_type_str(t1), fn1,
6677be8704ffSAlexei Starovoitov 			btf_type_str(t2), fn2);
6678be8704ffSAlexei Starovoitov 		return -EINVAL;
6679be8704ffSAlexei Starovoitov 	}
6680be8704ffSAlexei Starovoitov 
6681be8704ffSAlexei Starovoitov 	for (i = 0; i < nargs1; i++) {
6682be8704ffSAlexei Starovoitov 		t1 = btf_type_skip_modifiers(btf1, args1[i].type, NULL);
6683be8704ffSAlexei Starovoitov 		t2 = btf_type_skip_modifiers(btf2, args2[i].type, NULL);
6684be8704ffSAlexei Starovoitov 
6685be8704ffSAlexei Starovoitov 		if (t1->info != t2->info) {
6686be8704ffSAlexei Starovoitov 			bpf_log(log, "arg%d in %s() is %s while %s() has %s\n",
6687be8704ffSAlexei Starovoitov 				i, fn1, btf_type_str(t1),
6688be8704ffSAlexei Starovoitov 				fn2, btf_type_str(t2));
6689be8704ffSAlexei Starovoitov 			return -EINVAL;
6690be8704ffSAlexei Starovoitov 		}
6691be8704ffSAlexei Starovoitov 		if (btf_type_has_size(t1) && t1->size != t2->size) {
6692be8704ffSAlexei Starovoitov 			bpf_log(log,
6693be8704ffSAlexei Starovoitov 				"arg%d in %s() has size %d while %s() has %d\n",
6694be8704ffSAlexei Starovoitov 				i, fn1, t1->size,
6695be8704ffSAlexei Starovoitov 				fn2, t2->size);
6696be8704ffSAlexei Starovoitov 			return -EINVAL;
6697be8704ffSAlexei Starovoitov 		}
6698be8704ffSAlexei Starovoitov 
6699be8704ffSAlexei Starovoitov 		/* global functions are validated with scalars and pointers
6700be8704ffSAlexei Starovoitov 		 * to context only. And only global functions can be replaced.
6701be8704ffSAlexei Starovoitov 		 * Hence type check only those types.
6702be8704ffSAlexei Starovoitov 		 */
67036089fb32SYonghong Song 		if (btf_type_is_int(t1) || btf_is_any_enum(t1))
6704be8704ffSAlexei Starovoitov 			continue;
6705be8704ffSAlexei Starovoitov 		if (!btf_type_is_ptr(t1)) {
6706be8704ffSAlexei Starovoitov 			bpf_log(log,
6707be8704ffSAlexei Starovoitov 				"arg%d in %s() has unrecognized type\n",
6708be8704ffSAlexei Starovoitov 				i, fn1);
6709be8704ffSAlexei Starovoitov 			return -EINVAL;
6710be8704ffSAlexei Starovoitov 		}
6711be8704ffSAlexei Starovoitov 		t1 = btf_type_skip_modifiers(btf1, t1->type, NULL);
6712be8704ffSAlexei Starovoitov 		t2 = btf_type_skip_modifiers(btf2, t2->type, NULL);
6713be8704ffSAlexei Starovoitov 		if (!btf_type_is_struct(t1)) {
6714be8704ffSAlexei Starovoitov 			bpf_log(log,
6715be8704ffSAlexei Starovoitov 				"arg%d in %s() is not a pointer to context\n",
6716be8704ffSAlexei Starovoitov 				i, fn1);
6717be8704ffSAlexei Starovoitov 			return -EINVAL;
6718be8704ffSAlexei Starovoitov 		}
6719be8704ffSAlexei Starovoitov 		if (!btf_type_is_struct(t2)) {
6720be8704ffSAlexei Starovoitov 			bpf_log(log,
6721be8704ffSAlexei Starovoitov 				"arg%d in %s() is not a pointer to context\n",
6722be8704ffSAlexei Starovoitov 				i, fn2);
6723be8704ffSAlexei Starovoitov 			return -EINVAL;
6724be8704ffSAlexei Starovoitov 		}
6725be8704ffSAlexei Starovoitov 		/* This is an optional check to make program writing easier.
6726be8704ffSAlexei Starovoitov 		 * Compare names of structs and report an error to the user.
6727be8704ffSAlexei Starovoitov 		 * btf_prepare_func_args() already checked that t2 struct
6728be8704ffSAlexei Starovoitov 		 * is a context type. btf_prepare_func_args() will check
6729be8704ffSAlexei Starovoitov 		 * later that t1 struct is a context type as well.
6730be8704ffSAlexei Starovoitov 		 */
6731be8704ffSAlexei Starovoitov 		s1 = btf_name_by_offset(btf1, t1->name_off);
6732be8704ffSAlexei Starovoitov 		s2 = btf_name_by_offset(btf2, t2->name_off);
6733be8704ffSAlexei Starovoitov 		if (strcmp(s1, s2)) {
6734be8704ffSAlexei Starovoitov 			bpf_log(log,
6735be8704ffSAlexei Starovoitov 				"arg%d %s(struct %s *) doesn't match %s(struct %s *)\n",
6736be8704ffSAlexei Starovoitov 				i, fn1, s1, fn2, s2);
6737be8704ffSAlexei Starovoitov 			return -EINVAL;
6738be8704ffSAlexei Starovoitov 		}
6739be8704ffSAlexei Starovoitov 	}
6740be8704ffSAlexei Starovoitov 	return 0;
6741be8704ffSAlexei Starovoitov }
6742be8704ffSAlexei Starovoitov 
6743be8704ffSAlexei Starovoitov /* Compare BTFs of given program with BTF of target program */
btf_check_type_match(struct bpf_verifier_log * log,const struct bpf_prog * prog,struct btf * btf2,const struct btf_type * t2)6744efc68158SToke Høiland-Jørgensen int btf_check_type_match(struct bpf_verifier_log *log, const struct bpf_prog *prog,
6745be8704ffSAlexei Starovoitov 			 struct btf *btf2, const struct btf_type *t2)
6746be8704ffSAlexei Starovoitov {
6747be8704ffSAlexei Starovoitov 	struct btf *btf1 = prog->aux->btf;
6748be8704ffSAlexei Starovoitov 	const struct btf_type *t1;
6749be8704ffSAlexei Starovoitov 	u32 btf_id = 0;
6750be8704ffSAlexei Starovoitov 
6751be8704ffSAlexei Starovoitov 	if (!prog->aux->func_info) {
6752efc68158SToke Høiland-Jørgensen 		bpf_log(log, "Program extension requires BTF\n");
6753be8704ffSAlexei Starovoitov 		return -EINVAL;
6754be8704ffSAlexei Starovoitov 	}
6755be8704ffSAlexei Starovoitov 
6756be8704ffSAlexei Starovoitov 	btf_id = prog->aux->func_info[0].type_id;
6757be8704ffSAlexei Starovoitov 	if (!btf_id)
6758be8704ffSAlexei Starovoitov 		return -EFAULT;
6759be8704ffSAlexei Starovoitov 
6760be8704ffSAlexei Starovoitov 	t1 = btf_type_by_id(btf1, btf_id);
6761be8704ffSAlexei Starovoitov 	if (!t1 || !btf_type_is_func(t1))
6762be8704ffSAlexei Starovoitov 		return -EFAULT;
6763be8704ffSAlexei Starovoitov 
6764efc68158SToke Høiland-Jørgensen 	return btf_check_func_type_match(log, btf1, t1, btf2, t2);
6765be8704ffSAlexei Starovoitov }
6766be8704ffSAlexei Starovoitov 
btf_check_func_arg_match(struct bpf_verifier_env * env,const struct btf * btf,u32 func_id,struct bpf_reg_state * regs,bool ptr_to_mem_ok,bool processing_call)676734747c41SMartin KaFai Lau static int btf_check_func_arg_match(struct bpf_verifier_env *env,
676834747c41SMartin KaFai Lau 				    const struct btf *btf, u32 func_id,
676934747c41SMartin KaFai Lau 				    struct bpf_reg_state *regs,
6770a4703e31SKumar Kartikeya Dwivedi 				    bool ptr_to_mem_ok,
677195f2f26fSBenjamin Tissoires 				    bool processing_call)
677234747c41SMartin KaFai Lau {
6773f858c2b2SToke Høiland-Jørgensen 	enum bpf_prog_type prog_type = resolve_prog_type(env->prog);
677434747c41SMartin KaFai Lau 	struct bpf_verifier_log *log = &env->log;
677534747c41SMartin KaFai Lau 	const char *func_name, *ref_tname;
677634747c41SMartin KaFai Lau 	const struct btf_type *t, *ref_t;
677734747c41SMartin KaFai Lau 	const struct btf_param *args;
677800b85860SKumar Kartikeya Dwivedi 	u32 i, nargs, ref_id;
677900b85860SKumar Kartikeya Dwivedi 	int ret;
678034747c41SMartin KaFai Lau 
678134747c41SMartin KaFai Lau 	t = btf_type_by_id(btf, func_id);
678234747c41SMartin KaFai Lau 	if (!t || !btf_type_is_func(t)) {
678334747c41SMartin KaFai Lau 		/* These checks were already done by the verifier while loading
6784e6ac2450SMartin KaFai Lau 		 * struct bpf_func_info or in add_kfunc_call().
678534747c41SMartin KaFai Lau 		 */
678634747c41SMartin KaFai Lau 		bpf_log(log, "BTF of func_id %u doesn't point to KIND_FUNC\n",
678734747c41SMartin KaFai Lau 			func_id);
678834747c41SMartin KaFai Lau 		return -EFAULT;
678934747c41SMartin KaFai Lau 	}
679034747c41SMartin KaFai Lau 	func_name = btf_name_by_offset(btf, t->name_off);
679134747c41SMartin KaFai Lau 
679234747c41SMartin KaFai Lau 	t = btf_type_by_id(btf, t->type);
679334747c41SMartin KaFai Lau 	if (!t || !btf_type_is_func_proto(t)) {
679434747c41SMartin KaFai Lau 		bpf_log(log, "Invalid BTF of func %s\n", func_name);
679534747c41SMartin KaFai Lau 		return -EFAULT;
679634747c41SMartin KaFai Lau 	}
679734747c41SMartin KaFai Lau 	args = (const struct btf_param *)(t + 1);
679834747c41SMartin KaFai Lau 	nargs = btf_type_vlen(t);
679934747c41SMartin KaFai Lau 	if (nargs > MAX_BPF_FUNC_REG_ARGS) {
680034747c41SMartin KaFai Lau 		bpf_log(log, "Function %s has %d > %d args\n", func_name, nargs,
680134747c41SMartin KaFai Lau 			MAX_BPF_FUNC_REG_ARGS);
680234747c41SMartin KaFai Lau 		return -EINVAL;
680334747c41SMartin KaFai Lau 	}
680434747c41SMartin KaFai Lau 
680534747c41SMartin KaFai Lau 	/* check that BTF function arguments match actual types that the
680634747c41SMartin KaFai Lau 	 * verifier sees.
680734747c41SMartin KaFai Lau 	 */
680834747c41SMartin KaFai Lau 	for (i = 0; i < nargs; i++) {
68098f14852eSKumar Kartikeya Dwivedi 		enum bpf_arg_type arg_type = ARG_DONTCARE;
681034747c41SMartin KaFai Lau 		u32 regno = i + 1;
681134747c41SMartin KaFai Lau 		struct bpf_reg_state *reg = &regs[regno];
681234747c41SMartin KaFai Lau 
681334747c41SMartin KaFai Lau 		t = btf_type_skip_modifiers(btf, args[i].type, NULL);
681434747c41SMartin KaFai Lau 		if (btf_type_is_scalar(t)) {
681534747c41SMartin KaFai Lau 			if (reg->type == SCALAR_VALUE)
681634747c41SMartin KaFai Lau 				continue;
681734747c41SMartin KaFai Lau 			bpf_log(log, "R%d is not a scalar\n", regno);
681834747c41SMartin KaFai Lau 			return -EINVAL;
681934747c41SMartin KaFai Lau 		}
682034747c41SMartin KaFai Lau 
682134747c41SMartin KaFai Lau 		if (!btf_type_is_ptr(t)) {
682234747c41SMartin KaFai Lau 			bpf_log(log, "Unrecognized arg#%d type %s\n",
682334747c41SMartin KaFai Lau 				i, btf_type_str(t));
682434747c41SMartin KaFai Lau 			return -EINVAL;
682534747c41SMartin KaFai Lau 		}
682634747c41SMartin KaFai Lau 
6827e6ac2450SMartin KaFai Lau 		ref_t = btf_type_skip_modifiers(btf, t->type, &ref_id);
682834747c41SMartin KaFai Lau 		ref_tname = btf_name_by_offset(btf, ref_t->name_off);
6829655efe50SKumar Kartikeya Dwivedi 
68308f14852eSKumar Kartikeya Dwivedi 		ret = check_func_arg_reg_off(env, reg, regno, arg_type);
6831655efe50SKumar Kartikeya Dwivedi 		if (ret < 0)
6832655efe50SKumar Kartikeya Dwivedi 			return ret;
6833655efe50SKumar Kartikeya Dwivedi 
683400b85860SKumar Kartikeya Dwivedi 		if (btf_get_prog_ctx_type(log, btf, t, prog_type, i)) {
68353363bd0cSKumar Kartikeya Dwivedi 			/* If function expects ctx type in BTF check that caller
68363363bd0cSKumar Kartikeya Dwivedi 			 * is passing PTR_TO_CTX.
68373363bd0cSKumar Kartikeya Dwivedi 			 */
68383363bd0cSKumar Kartikeya Dwivedi 			if (reg->type != PTR_TO_CTX) {
68393363bd0cSKumar Kartikeya Dwivedi 				bpf_log(log,
68403363bd0cSKumar Kartikeya Dwivedi 					"arg#%d expected pointer to ctx, but got %s\n",
68413363bd0cSKumar Kartikeya Dwivedi 					i, btf_type_str(t));
68423363bd0cSKumar Kartikeya Dwivedi 				return -EINVAL;
68433363bd0cSKumar Kartikeya Dwivedi 			}
684495f2f26fSBenjamin Tissoires 		} else if (ptr_to_mem_ok && processing_call) {
684534747c41SMartin KaFai Lau 			const struct btf_type *resolve_ret;
684634747c41SMartin KaFai Lau 			u32 type_size;
684734747c41SMartin KaFai Lau 
684834747c41SMartin KaFai Lau 			resolve_ret = btf_resolve_size(btf, ref_t, &type_size);
684934747c41SMartin KaFai Lau 			if (IS_ERR(resolve_ret)) {
685034747c41SMartin KaFai Lau 				bpf_log(log,
685134747c41SMartin KaFai Lau 					"arg#%d reference type('%s %s') size cannot be determined: %ld\n",
685234747c41SMartin KaFai Lau 					i, btf_type_str(ref_t), ref_tname,
685334747c41SMartin KaFai Lau 					PTR_ERR(resolve_ret));
685434747c41SMartin KaFai Lau 				return -EINVAL;
685534747c41SMartin KaFai Lau 			}
685634747c41SMartin KaFai Lau 
685734747c41SMartin KaFai Lau 			if (check_mem_reg(env, reg, regno, type_size))
685834747c41SMartin KaFai Lau 				return -EINVAL;
685934747c41SMartin KaFai Lau 		} else {
686000b85860SKumar Kartikeya Dwivedi 			bpf_log(log, "reg type unsupported for arg#%d function %s#%d\n", i,
686100b85860SKumar Kartikeya Dwivedi 				func_name, func_id);
686234747c41SMartin KaFai Lau 			return -EINVAL;
686334747c41SMartin KaFai Lau 		}
686434747c41SMartin KaFai Lau 	}
686534747c41SMartin KaFai Lau 
686600b85860SKumar Kartikeya Dwivedi 	return 0;
686734747c41SMartin KaFai Lau }
686834747c41SMartin KaFai Lau 
686995f2f26fSBenjamin Tissoires /* Compare BTF of a function declaration with given bpf_reg_state.
687051c39bb1SAlexei Starovoitov  * Returns:
687151c39bb1SAlexei Starovoitov  * EFAULT - there is a verifier bug. Abort verification.
687251c39bb1SAlexei Starovoitov  * EINVAL - there is a type mismatch or BTF is not available.
687351c39bb1SAlexei Starovoitov  * 0 - BTF matches with what bpf_reg_state expects.
687451c39bb1SAlexei Starovoitov  * Only PTR_TO_CTX and SCALAR_VALUE states are recognized.
687551c39bb1SAlexei Starovoitov  */
btf_check_subprog_arg_match(struct bpf_verifier_env * env,int subprog,struct bpf_reg_state * regs)687634747c41SMartin KaFai Lau int btf_check_subprog_arg_match(struct bpf_verifier_env *env, int subprog,
6877feb4adfaSDmitrii Banshchikov 				struct bpf_reg_state *regs)
68788c1b6e69SAlexei Starovoitov {
68798c1b6e69SAlexei Starovoitov 	struct bpf_prog *prog = env->prog;
68808c1b6e69SAlexei Starovoitov 	struct btf *btf = prog->aux->btf;
6881e5069b9cSDmitrii Banshchikov 	bool is_global;
688234747c41SMartin KaFai Lau 	u32 btf_id;
688334747c41SMartin KaFai Lau 	int err;
68848c1b6e69SAlexei Starovoitov 
68858c1b6e69SAlexei Starovoitov 	if (!prog->aux->func_info)
688651c39bb1SAlexei Starovoitov 		return -EINVAL;
68878c1b6e69SAlexei Starovoitov 
68888c1b6e69SAlexei Starovoitov 	btf_id = prog->aux->func_info[subprog].type_id;
68898c1b6e69SAlexei Starovoitov 	if (!btf_id)
689051c39bb1SAlexei Starovoitov 		return -EFAULT;
68918c1b6e69SAlexei Starovoitov 
68928c1b6e69SAlexei Starovoitov 	if (prog->aux->func_info_aux[subprog].unreliable)
689351c39bb1SAlexei Starovoitov 		return -EINVAL;
68948c1b6e69SAlexei Starovoitov 
6895e5069b9cSDmitrii Banshchikov 	is_global = prog->aux->func_info_aux[subprog].linkage == BTF_FUNC_GLOBAL;
689600b85860SKumar Kartikeya Dwivedi 	err = btf_check_func_arg_match(env, btf, btf_id, regs, is_global, false);
689795f2f26fSBenjamin Tissoires 
689895f2f26fSBenjamin Tissoires 	/* Compiler optimizations can remove arguments from static functions
689995f2f26fSBenjamin Tissoires 	 * or mismatched type can be passed into a global function.
690095f2f26fSBenjamin Tissoires 	 * In such cases mark the function as unreliable from BTF point of view.
690195f2f26fSBenjamin Tissoires 	 */
690295f2f26fSBenjamin Tissoires 	if (err)
690395f2f26fSBenjamin Tissoires 		prog->aux->func_info_aux[subprog].unreliable = true;
690495f2f26fSBenjamin Tissoires 	return err;
690595f2f26fSBenjamin Tissoires }
690695f2f26fSBenjamin Tissoires 
690795f2f26fSBenjamin Tissoires /* Compare BTF of a function call with given bpf_reg_state.
690895f2f26fSBenjamin Tissoires  * Returns:
690995f2f26fSBenjamin Tissoires  * EFAULT - there is a verifier bug. Abort verification.
691095f2f26fSBenjamin Tissoires  * EINVAL - there is a type mismatch or BTF is not available.
691195f2f26fSBenjamin Tissoires  * 0 - BTF matches with what bpf_reg_state expects.
691295f2f26fSBenjamin Tissoires  * Only PTR_TO_CTX and SCALAR_VALUE states are recognized.
691395f2f26fSBenjamin Tissoires  *
691495f2f26fSBenjamin Tissoires  * NOTE: the code is duplicated from btf_check_subprog_arg_match()
691595f2f26fSBenjamin Tissoires  * because btf_check_func_arg_match() is still doing both. Once that
691695f2f26fSBenjamin Tissoires  * function is split in 2, we can call from here btf_check_subprog_arg_match()
691795f2f26fSBenjamin Tissoires  * first, and then treat the calling part in a new code path.
691895f2f26fSBenjamin Tissoires  */
btf_check_subprog_call(struct bpf_verifier_env * env,int subprog,struct bpf_reg_state * regs)691995f2f26fSBenjamin Tissoires int btf_check_subprog_call(struct bpf_verifier_env *env, int subprog,
692095f2f26fSBenjamin Tissoires 			   struct bpf_reg_state *regs)
692195f2f26fSBenjamin Tissoires {
692295f2f26fSBenjamin Tissoires 	struct bpf_prog *prog = env->prog;
692395f2f26fSBenjamin Tissoires 	struct btf *btf = prog->aux->btf;
692495f2f26fSBenjamin Tissoires 	bool is_global;
692595f2f26fSBenjamin Tissoires 	u32 btf_id;
692695f2f26fSBenjamin Tissoires 	int err;
692795f2f26fSBenjamin Tissoires 
692895f2f26fSBenjamin Tissoires 	if (!prog->aux->func_info)
692995f2f26fSBenjamin Tissoires 		return -EINVAL;
693095f2f26fSBenjamin Tissoires 
693195f2f26fSBenjamin Tissoires 	btf_id = prog->aux->func_info[subprog].type_id;
693295f2f26fSBenjamin Tissoires 	if (!btf_id)
693395f2f26fSBenjamin Tissoires 		return -EFAULT;
693495f2f26fSBenjamin Tissoires 
693595f2f26fSBenjamin Tissoires 	if (prog->aux->func_info_aux[subprog].unreliable)
693695f2f26fSBenjamin Tissoires 		return -EINVAL;
693795f2f26fSBenjamin Tissoires 
693895f2f26fSBenjamin Tissoires 	is_global = prog->aux->func_info_aux[subprog].linkage == BTF_FUNC_GLOBAL;
693900b85860SKumar Kartikeya Dwivedi 	err = btf_check_func_arg_match(env, btf, btf_id, regs, is_global, true);
6940feb4adfaSDmitrii Banshchikov 
694151c39bb1SAlexei Starovoitov 	/* Compiler optimizations can remove arguments from static functions
694251c39bb1SAlexei Starovoitov 	 * or mismatched type can be passed into a global function.
694351c39bb1SAlexei Starovoitov 	 * In such cases mark the function as unreliable from BTF point of view.
694451c39bb1SAlexei Starovoitov 	 */
694534747c41SMartin KaFai Lau 	if (err)
69468c1b6e69SAlexei Starovoitov 		prog->aux->func_info_aux[subprog].unreliable = true;
694734747c41SMartin KaFai Lau 	return err;
694851c39bb1SAlexei Starovoitov }
694951c39bb1SAlexei Starovoitov 
695051c39bb1SAlexei Starovoitov /* Convert BTF of a function into bpf_reg_state if possible
695151c39bb1SAlexei Starovoitov  * Returns:
695251c39bb1SAlexei Starovoitov  * EFAULT - there is a verifier bug. Abort verification.
695351c39bb1SAlexei Starovoitov  * EINVAL - cannot convert BTF.
695451c39bb1SAlexei Starovoitov  * 0 - Successfully converted BTF into bpf_reg_state
695551c39bb1SAlexei Starovoitov  * (either PTR_TO_CTX or SCALAR_VALUE).
695651c39bb1SAlexei Starovoitov  */
btf_prepare_func_args(struct bpf_verifier_env * env,int subprog,struct bpf_reg_state * regs)695751c39bb1SAlexei Starovoitov int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog,
6958feb4adfaSDmitrii Banshchikov 			  struct bpf_reg_state *regs)
695951c39bb1SAlexei Starovoitov {
696051c39bb1SAlexei Starovoitov 	struct bpf_verifier_log *log = &env->log;
696151c39bb1SAlexei Starovoitov 	struct bpf_prog *prog = env->prog;
6962be8704ffSAlexei Starovoitov 	enum bpf_prog_type prog_type = prog->type;
696351c39bb1SAlexei Starovoitov 	struct btf *btf = prog->aux->btf;
696451c39bb1SAlexei Starovoitov 	const struct btf_param *args;
6965e5069b9cSDmitrii Banshchikov 	const struct btf_type *t, *ref_t;
696651c39bb1SAlexei Starovoitov 	u32 i, nargs, btf_id;
696751c39bb1SAlexei Starovoitov 	const char *tname;
696851c39bb1SAlexei Starovoitov 
696951c39bb1SAlexei Starovoitov 	if (!prog->aux->func_info ||
697051c39bb1SAlexei Starovoitov 	    prog->aux->func_info_aux[subprog].linkage != BTF_FUNC_GLOBAL) {
697151c39bb1SAlexei Starovoitov 		bpf_log(log, "Verifier bug\n");
697251c39bb1SAlexei Starovoitov 		return -EFAULT;
697351c39bb1SAlexei Starovoitov 	}
697451c39bb1SAlexei Starovoitov 
697551c39bb1SAlexei Starovoitov 	btf_id = prog->aux->func_info[subprog].type_id;
697651c39bb1SAlexei Starovoitov 	if (!btf_id) {
697751c39bb1SAlexei Starovoitov 		bpf_log(log, "Global functions need valid BTF\n");
697851c39bb1SAlexei Starovoitov 		return -EFAULT;
697951c39bb1SAlexei Starovoitov 	}
698051c39bb1SAlexei Starovoitov 
698151c39bb1SAlexei Starovoitov 	t = btf_type_by_id(btf, btf_id);
698251c39bb1SAlexei Starovoitov 	if (!t || !btf_type_is_func(t)) {
698351c39bb1SAlexei Starovoitov 		/* These checks were already done by the verifier while loading
698451c39bb1SAlexei Starovoitov 		 * struct bpf_func_info
698551c39bb1SAlexei Starovoitov 		 */
698651c39bb1SAlexei Starovoitov 		bpf_log(log, "BTF of func#%d doesn't point to KIND_FUNC\n",
698751c39bb1SAlexei Starovoitov 			subprog);
698851c39bb1SAlexei Starovoitov 		return -EFAULT;
698951c39bb1SAlexei Starovoitov 	}
699051c39bb1SAlexei Starovoitov 	tname = btf_name_by_offset(btf, t->name_off);
699151c39bb1SAlexei Starovoitov 
699251c39bb1SAlexei Starovoitov 	if (log->level & BPF_LOG_LEVEL)
699351c39bb1SAlexei Starovoitov 		bpf_log(log, "Validating %s() func#%d...\n",
699451c39bb1SAlexei Starovoitov 			tname, subprog);
699551c39bb1SAlexei Starovoitov 
699651c39bb1SAlexei Starovoitov 	if (prog->aux->func_info_aux[subprog].unreliable) {
699751c39bb1SAlexei Starovoitov 		bpf_log(log, "Verifier bug in function %s()\n", tname);
699851c39bb1SAlexei Starovoitov 		return -EFAULT;
699951c39bb1SAlexei Starovoitov 	}
7000be8704ffSAlexei Starovoitov 	if (prog_type == BPF_PROG_TYPE_EXT)
70013aac1eadSToke Høiland-Jørgensen 		prog_type = prog->aux->dst_prog->type;
700251c39bb1SAlexei Starovoitov 
700351c39bb1SAlexei Starovoitov 	t = btf_type_by_id(btf, t->type);
700451c39bb1SAlexei Starovoitov 	if (!t || !btf_type_is_func_proto(t)) {
700551c39bb1SAlexei Starovoitov 		bpf_log(log, "Invalid type of function %s()\n", tname);
700651c39bb1SAlexei Starovoitov 		return -EFAULT;
700751c39bb1SAlexei Starovoitov 	}
700851c39bb1SAlexei Starovoitov 	args = (const struct btf_param *)(t + 1);
700951c39bb1SAlexei Starovoitov 	nargs = btf_type_vlen(t);
7010523a4cf4SDmitrii Banshchikov 	if (nargs > MAX_BPF_FUNC_REG_ARGS) {
7011523a4cf4SDmitrii Banshchikov 		bpf_log(log, "Global function %s() with %d > %d args. Buggy compiler.\n",
7012523a4cf4SDmitrii Banshchikov 			tname, nargs, MAX_BPF_FUNC_REG_ARGS);
701351c39bb1SAlexei Starovoitov 		return -EINVAL;
701451c39bb1SAlexei Starovoitov 	}
701551c39bb1SAlexei Starovoitov 	/* check that function returns int */
701651c39bb1SAlexei Starovoitov 	t = btf_type_by_id(btf, t->type);
701751c39bb1SAlexei Starovoitov 	while (btf_type_is_modifier(t))
701851c39bb1SAlexei Starovoitov 		t = btf_type_by_id(btf, t->type);
70196089fb32SYonghong Song 	if (!btf_type_is_int(t) && !btf_is_any_enum(t)) {
702051c39bb1SAlexei Starovoitov 		bpf_log(log,
702151c39bb1SAlexei Starovoitov 			"Global function %s() doesn't return scalar. Only those are supported.\n",
702251c39bb1SAlexei Starovoitov 			tname);
702351c39bb1SAlexei Starovoitov 		return -EINVAL;
702451c39bb1SAlexei Starovoitov 	}
702551c39bb1SAlexei Starovoitov 	/* Convert BTF function arguments into verifier types.
702651c39bb1SAlexei Starovoitov 	 * Only PTR_TO_CTX and SCALAR are supported atm.
702751c39bb1SAlexei Starovoitov 	 */
702851c39bb1SAlexei Starovoitov 	for (i = 0; i < nargs; i++) {
7029feb4adfaSDmitrii Banshchikov 		struct bpf_reg_state *reg = &regs[i + 1];
7030feb4adfaSDmitrii Banshchikov 
703151c39bb1SAlexei Starovoitov 		t = btf_type_by_id(btf, args[i].type);
703251c39bb1SAlexei Starovoitov 		while (btf_type_is_modifier(t))
703351c39bb1SAlexei Starovoitov 			t = btf_type_by_id(btf, t->type);
70346089fb32SYonghong Song 		if (btf_type_is_int(t) || btf_is_any_enum(t)) {
7035feb4adfaSDmitrii Banshchikov 			reg->type = SCALAR_VALUE;
703651c39bb1SAlexei Starovoitov 			continue;
703751c39bb1SAlexei Starovoitov 		}
7038e5069b9cSDmitrii Banshchikov 		if (btf_type_is_ptr(t)) {
7039e5069b9cSDmitrii Banshchikov 			if (btf_get_prog_ctx_type(log, btf, t, prog_type, i)) {
7040feb4adfaSDmitrii Banshchikov 				reg->type = PTR_TO_CTX;
704151c39bb1SAlexei Starovoitov 				continue;
704251c39bb1SAlexei Starovoitov 			}
7043e5069b9cSDmitrii Banshchikov 
7044e5069b9cSDmitrii Banshchikov 			t = btf_type_skip_modifiers(btf, t->type, NULL);
7045e5069b9cSDmitrii Banshchikov 
7046e5069b9cSDmitrii Banshchikov 			ref_t = btf_resolve_size(btf, t, &reg->mem_size);
7047e5069b9cSDmitrii Banshchikov 			if (IS_ERR(ref_t)) {
7048e5069b9cSDmitrii Banshchikov 				bpf_log(log,
7049e5069b9cSDmitrii Banshchikov 				    "arg#%d reference type('%s %s') size cannot be determined: %ld\n",
7050e5069b9cSDmitrii Banshchikov 				    i, btf_type_str(t), btf_name_by_offset(btf, t->name_off),
7051e5069b9cSDmitrii Banshchikov 					PTR_ERR(ref_t));
7052e5069b9cSDmitrii Banshchikov 				return -EINVAL;
7053e5069b9cSDmitrii Banshchikov 			}
7054e5069b9cSDmitrii Banshchikov 
7055cf9f2f8dSHao Luo 			reg->type = PTR_TO_MEM | PTR_MAYBE_NULL;
7056e5069b9cSDmitrii Banshchikov 			reg->id = ++env->id_gen;
7057e5069b9cSDmitrii Banshchikov 
7058e5069b9cSDmitrii Banshchikov 			continue;
7059e5069b9cSDmitrii Banshchikov 		}
706051c39bb1SAlexei Starovoitov 		bpf_log(log, "Arg#%d type %s in %s() is not supported yet.\n",
7061571f9738SPeilin Ye 			i, btf_type_str(t), tname);
706251c39bb1SAlexei Starovoitov 		return -EINVAL;
706351c39bb1SAlexei Starovoitov 	}
70648c1b6e69SAlexei Starovoitov 	return 0;
70658c1b6e69SAlexei Starovoitov }
70668c1b6e69SAlexei Starovoitov 
btf_type_show(const struct btf * btf,u32 type_id,void * obj,struct btf_show * show)706731d0bc81SAlan Maguire static void btf_type_show(const struct btf *btf, u32 type_id, void *obj,
706831d0bc81SAlan Maguire 			  struct btf_show *show)
7069b00b8daeSMartin KaFai Lau {
7070b00b8daeSMartin KaFai Lau 	const struct btf_type *t = btf_type_by_id(btf, type_id);
7071b00b8daeSMartin KaFai Lau 
707231d0bc81SAlan Maguire 	show->btf = btf;
707331d0bc81SAlan Maguire 	memset(&show->state, 0, sizeof(show->state));
707431d0bc81SAlan Maguire 	memset(&show->obj, 0, sizeof(show->obj));
707531d0bc81SAlan Maguire 
707631d0bc81SAlan Maguire 	btf_type_ops(t)->show(btf, t, type_id, obj, 0, show);
707731d0bc81SAlan Maguire }
707831d0bc81SAlan Maguire 
btf_seq_show(struct btf_show * show,const char * fmt,va_list args)70795306d9a5SAlan Maguire __printf(2, 0) static void btf_seq_show(struct btf_show *show, const char *fmt,
708031d0bc81SAlan Maguire 					va_list args)
708131d0bc81SAlan Maguire {
708231d0bc81SAlan Maguire 	seq_vprintf((struct seq_file *)show->target, fmt, args);
708331d0bc81SAlan Maguire }
708431d0bc81SAlan Maguire 
btf_type_seq_show_flags(const struct btf * btf,u32 type_id,void * obj,struct seq_file * m,u64 flags)7085eb411377SAlan Maguire int btf_type_seq_show_flags(const struct btf *btf, u32 type_id,
708631d0bc81SAlan Maguire 			    void *obj, struct seq_file *m, u64 flags)
708731d0bc81SAlan Maguire {
708831d0bc81SAlan Maguire 	struct btf_show sseq;
708931d0bc81SAlan Maguire 
709031d0bc81SAlan Maguire 	sseq.target = m;
709131d0bc81SAlan Maguire 	sseq.showfn = btf_seq_show;
709231d0bc81SAlan Maguire 	sseq.flags = flags;
709331d0bc81SAlan Maguire 
709431d0bc81SAlan Maguire 	btf_type_show(btf, type_id, obj, &sseq);
709531d0bc81SAlan Maguire 
709631d0bc81SAlan Maguire 	return sseq.state.status;
709731d0bc81SAlan Maguire }
709831d0bc81SAlan Maguire 
btf_type_seq_show(const struct btf * btf,u32 type_id,void * obj,struct seq_file * m)709931d0bc81SAlan Maguire void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj,
710031d0bc81SAlan Maguire 		       struct seq_file *m)
710131d0bc81SAlan Maguire {
710231d0bc81SAlan Maguire 	(void) btf_type_seq_show_flags(btf, type_id, obj, m,
710331d0bc81SAlan Maguire 				       BTF_SHOW_NONAME | BTF_SHOW_COMPACT |
710431d0bc81SAlan Maguire 				       BTF_SHOW_ZERO | BTF_SHOW_UNSAFE);
710531d0bc81SAlan Maguire }
710631d0bc81SAlan Maguire 
710731d0bc81SAlan Maguire struct btf_show_snprintf {
710831d0bc81SAlan Maguire 	struct btf_show show;
710931d0bc81SAlan Maguire 	int len_left;		/* space left in string */
711031d0bc81SAlan Maguire 	int len;		/* length we would have written */
711131d0bc81SAlan Maguire };
711231d0bc81SAlan Maguire 
btf_snprintf_show(struct btf_show * show,const char * fmt,va_list args)71135306d9a5SAlan Maguire __printf(2, 0) static void btf_snprintf_show(struct btf_show *show, const char *fmt,
711431d0bc81SAlan Maguire 					     va_list args)
711531d0bc81SAlan Maguire {
711631d0bc81SAlan Maguire 	struct btf_show_snprintf *ssnprintf = (struct btf_show_snprintf *)show;
711731d0bc81SAlan Maguire 	int len;
711831d0bc81SAlan Maguire 
711931d0bc81SAlan Maguire 	len = vsnprintf(show->target, ssnprintf->len_left, fmt, args);
712031d0bc81SAlan Maguire 
712131d0bc81SAlan Maguire 	if (len < 0) {
712231d0bc81SAlan Maguire 		ssnprintf->len_left = 0;
712331d0bc81SAlan Maguire 		ssnprintf->len = len;
712458250ae3SFedor Tokarev 	} else if (len >= ssnprintf->len_left) {
712531d0bc81SAlan Maguire 		/* no space, drive on to get length we would have written */
712631d0bc81SAlan Maguire 		ssnprintf->len_left = 0;
712731d0bc81SAlan Maguire 		ssnprintf->len += len;
712831d0bc81SAlan Maguire 	} else {
712931d0bc81SAlan Maguire 		ssnprintf->len_left -= len;
713031d0bc81SAlan Maguire 		ssnprintf->len += len;
713131d0bc81SAlan Maguire 		show->target += len;
713231d0bc81SAlan Maguire 	}
713331d0bc81SAlan Maguire }
713431d0bc81SAlan Maguire 
btf_type_snprintf_show(const struct btf * btf,u32 type_id,void * obj,char * buf,int len,u64 flags)713531d0bc81SAlan Maguire int btf_type_snprintf_show(const struct btf *btf, u32 type_id, void *obj,
713631d0bc81SAlan Maguire 			   char *buf, int len, u64 flags)
713731d0bc81SAlan Maguire {
713831d0bc81SAlan Maguire 	struct btf_show_snprintf ssnprintf;
713931d0bc81SAlan Maguire 
714031d0bc81SAlan Maguire 	ssnprintf.show.target = buf;
714131d0bc81SAlan Maguire 	ssnprintf.show.flags = flags;
714231d0bc81SAlan Maguire 	ssnprintf.show.showfn = btf_snprintf_show;
714331d0bc81SAlan Maguire 	ssnprintf.len_left = len;
714431d0bc81SAlan Maguire 	ssnprintf.len = 0;
714531d0bc81SAlan Maguire 
714631d0bc81SAlan Maguire 	btf_type_show(btf, type_id, obj, (struct btf_show *)&ssnprintf);
714731d0bc81SAlan Maguire 
7148c561d110STom Rix 	/* If we encountered an error, return it. */
714931d0bc81SAlan Maguire 	if (ssnprintf.show.state.status)
715031d0bc81SAlan Maguire 		return ssnprintf.show.state.status;
715131d0bc81SAlan Maguire 
715231d0bc81SAlan Maguire 	/* Otherwise return length we would have written */
715331d0bc81SAlan Maguire 	return ssnprintf.len;
7154b00b8daeSMartin KaFai Lau }
7155f56a653cSMartin KaFai Lau 
71563481e64bSQuentin Monnet #ifdef CONFIG_PROC_FS
bpf_btf_show_fdinfo(struct seq_file * m,struct file * filp)71573481e64bSQuentin Monnet static void bpf_btf_show_fdinfo(struct seq_file *m, struct file *filp)
71583481e64bSQuentin Monnet {
71593481e64bSQuentin Monnet 	const struct btf *btf = filp->private_data;
71603481e64bSQuentin Monnet 
71613481e64bSQuentin Monnet 	seq_printf(m, "btf_id:\t%u\n", btf->id);
71623481e64bSQuentin Monnet }
71633481e64bSQuentin Monnet #endif
71643481e64bSQuentin Monnet 
btf_release(struct inode * inode,struct file * filp)7165f56a653cSMartin KaFai Lau static int btf_release(struct inode *inode, struct file *filp)
7166f56a653cSMartin KaFai Lau {
7167f56a653cSMartin KaFai Lau 	btf_put(filp->private_data);
7168f56a653cSMartin KaFai Lau 	return 0;
7169f56a653cSMartin KaFai Lau }
7170f56a653cSMartin KaFai Lau 
717160197cfbSMartin KaFai Lau const struct file_operations btf_fops = {
71723481e64bSQuentin Monnet #ifdef CONFIG_PROC_FS
71733481e64bSQuentin Monnet 	.show_fdinfo	= bpf_btf_show_fdinfo,
71743481e64bSQuentin Monnet #endif
7175f56a653cSMartin KaFai Lau 	.release	= btf_release,
7176f56a653cSMartin KaFai Lau };
7177f56a653cSMartin KaFai Lau 
__btf_new_fd(struct btf * btf)717878958fcaSMartin KaFai Lau static int __btf_new_fd(struct btf *btf)
717978958fcaSMartin KaFai Lau {
718078958fcaSMartin KaFai Lau 	return anon_inode_getfd("btf", &btf_fops, btf, O_RDONLY | O_CLOEXEC);
718178958fcaSMartin KaFai Lau }
718278958fcaSMartin KaFai Lau 
btf_new_fd(const union bpf_attr * attr,bpfptr_t uattr,u32 uattr_size)718347a71c1fSAndrii Nakryiko int btf_new_fd(const union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size)
7184f56a653cSMartin KaFai Lau {
7185f56a653cSMartin KaFai Lau 	struct btf *btf;
718678958fcaSMartin KaFai Lau 	int ret;
7187f56a653cSMartin KaFai Lau 
718847a71c1fSAndrii Nakryiko 	btf = btf_parse(attr, uattr, uattr_size);
7189f56a653cSMartin KaFai Lau 	if (IS_ERR(btf))
7190f56a653cSMartin KaFai Lau 		return PTR_ERR(btf);
7191f56a653cSMartin KaFai Lau 
719278958fcaSMartin KaFai Lau 	ret = btf_alloc_id(btf);
719378958fcaSMartin KaFai Lau 	if (ret) {
719478958fcaSMartin KaFai Lau 		btf_free(btf);
719578958fcaSMartin KaFai Lau 		return ret;
719678958fcaSMartin KaFai Lau 	}
719778958fcaSMartin KaFai Lau 
719878958fcaSMartin KaFai Lau 	/*
719978958fcaSMartin KaFai Lau 	 * The BTF ID is published to the userspace.
720078958fcaSMartin KaFai Lau 	 * All BTF free must go through call_rcu() from
720178958fcaSMartin KaFai Lau 	 * now on (i.e. free by calling btf_put()).
720278958fcaSMartin KaFai Lau 	 */
720378958fcaSMartin KaFai Lau 
720478958fcaSMartin KaFai Lau 	ret = __btf_new_fd(btf);
720578958fcaSMartin KaFai Lau 	if (ret < 0)
7206f56a653cSMartin KaFai Lau 		btf_put(btf);
7207f56a653cSMartin KaFai Lau 
720878958fcaSMartin KaFai Lau 	return ret;
7209f56a653cSMartin KaFai Lau }
7210f56a653cSMartin KaFai Lau 
btf_get_by_fd(int fd)7211f56a653cSMartin KaFai Lau struct btf *btf_get_by_fd(int fd)
7212f56a653cSMartin KaFai Lau {
7213f56a653cSMartin KaFai Lau 	struct btf *btf;
7214f56a653cSMartin KaFai Lau 	struct fd f;
7215f56a653cSMartin KaFai Lau 
7216f56a653cSMartin KaFai Lau 	f = fdget(fd);
7217f56a653cSMartin KaFai Lau 
7218f56a653cSMartin KaFai Lau 	if (!f.file)
7219f56a653cSMartin KaFai Lau 		return ERR_PTR(-EBADF);
7220f56a653cSMartin KaFai Lau 
7221f56a653cSMartin KaFai Lau 	if (f.file->f_op != &btf_fops) {
7222f56a653cSMartin KaFai Lau 		fdput(f);
7223f56a653cSMartin KaFai Lau 		return ERR_PTR(-EINVAL);
7224f56a653cSMartin KaFai Lau 	}
7225f56a653cSMartin KaFai Lau 
7226f56a653cSMartin KaFai Lau 	btf = f.file->private_data;
722778958fcaSMartin KaFai Lau 	refcount_inc(&btf->refcnt);
7228f56a653cSMartin KaFai Lau 	fdput(f);
7229f56a653cSMartin KaFai Lau 
7230f56a653cSMartin KaFai Lau 	return btf;
7231f56a653cSMartin KaFai Lau }
723260197cfbSMartin KaFai Lau 
btf_get_info_by_fd(const struct btf * btf,const union bpf_attr * attr,union bpf_attr __user * uattr)723360197cfbSMartin KaFai Lau int btf_get_info_by_fd(const struct btf *btf,
723460197cfbSMartin KaFai Lau 		       const union bpf_attr *attr,
723560197cfbSMartin KaFai Lau 		       union bpf_attr __user *uattr)
723660197cfbSMartin KaFai Lau {
723762dab84cSMartin KaFai Lau 	struct bpf_btf_info __user *uinfo;
72385c6f2588SGreg Kroah-Hartman 	struct bpf_btf_info info;
723962dab84cSMartin KaFai Lau 	u32 info_copy, btf_copy;
724062dab84cSMartin KaFai Lau 	void __user *ubtf;
724153297220SAndrii Nakryiko 	char __user *uname;
724253297220SAndrii Nakryiko 	u32 uinfo_len, uname_len, name_len;
724353297220SAndrii Nakryiko 	int ret = 0;
724460197cfbSMartin KaFai Lau 
724562dab84cSMartin KaFai Lau 	uinfo = u64_to_user_ptr(attr->info.info);
724662dab84cSMartin KaFai Lau 	uinfo_len = attr->info.info_len;
724762dab84cSMartin KaFai Lau 
724862dab84cSMartin KaFai Lau 	info_copy = min_t(u32, uinfo_len, sizeof(info));
72495c6f2588SGreg Kroah-Hartman 	memset(&info, 0, sizeof(info));
725062dab84cSMartin KaFai Lau 	if (copy_from_user(&info, uinfo, info_copy))
725162dab84cSMartin KaFai Lau 		return -EFAULT;
725262dab84cSMartin KaFai Lau 
725362dab84cSMartin KaFai Lau 	info.id = btf->id;
725462dab84cSMartin KaFai Lau 	ubtf = u64_to_user_ptr(info.btf);
725562dab84cSMartin KaFai Lau 	btf_copy = min_t(u32, btf->data_size, info.btf_size);
725662dab84cSMartin KaFai Lau 	if (copy_to_user(ubtf, btf->data, btf_copy))
725762dab84cSMartin KaFai Lau 		return -EFAULT;
725862dab84cSMartin KaFai Lau 	info.btf_size = btf->data_size;
725962dab84cSMartin KaFai Lau 
726053297220SAndrii Nakryiko 	info.kernel_btf = btf->kernel_btf;
726153297220SAndrii Nakryiko 
726253297220SAndrii Nakryiko 	uname = u64_to_user_ptr(info.name);
726353297220SAndrii Nakryiko 	uname_len = info.name_len;
726453297220SAndrii Nakryiko 	if (!uname ^ !uname_len)
726553297220SAndrii Nakryiko 		return -EINVAL;
726653297220SAndrii Nakryiko 
726753297220SAndrii Nakryiko 	name_len = strlen(btf->name);
726853297220SAndrii Nakryiko 	info.name_len = name_len;
726953297220SAndrii Nakryiko 
727053297220SAndrii Nakryiko 	if (uname) {
727153297220SAndrii Nakryiko 		if (uname_len >= name_len + 1) {
727253297220SAndrii Nakryiko 			if (copy_to_user(uname, btf->name, name_len + 1))
727353297220SAndrii Nakryiko 				return -EFAULT;
727453297220SAndrii Nakryiko 		} else {
727553297220SAndrii Nakryiko 			char zero = '\0';
727653297220SAndrii Nakryiko 
727753297220SAndrii Nakryiko 			if (copy_to_user(uname, btf->name, uname_len - 1))
727853297220SAndrii Nakryiko 				return -EFAULT;
727953297220SAndrii Nakryiko 			if (put_user(zero, uname + uname_len - 1))
728053297220SAndrii Nakryiko 				return -EFAULT;
728153297220SAndrii Nakryiko 			/* let user-space know about too short buffer */
728253297220SAndrii Nakryiko 			ret = -ENOSPC;
728353297220SAndrii Nakryiko 		}
728453297220SAndrii Nakryiko 	}
728553297220SAndrii Nakryiko 
728662dab84cSMartin KaFai Lau 	if (copy_to_user(uinfo, &info, info_copy) ||
728762dab84cSMartin KaFai Lau 	    put_user(info_copy, &uattr->info.info_len))
728860197cfbSMartin KaFai Lau 		return -EFAULT;
728960197cfbSMartin KaFai Lau 
729053297220SAndrii Nakryiko 	return ret;
729160197cfbSMartin KaFai Lau }
729278958fcaSMartin KaFai Lau 
btf_get_fd_by_id(u32 id)729378958fcaSMartin KaFai Lau int btf_get_fd_by_id(u32 id)
729478958fcaSMartin KaFai Lau {
729578958fcaSMartin KaFai Lau 	struct btf *btf;
729678958fcaSMartin KaFai Lau 	int fd;
729778958fcaSMartin KaFai Lau 
729878958fcaSMartin KaFai Lau 	rcu_read_lock();
729978958fcaSMartin KaFai Lau 	btf = idr_find(&btf_idr, id);
730078958fcaSMartin KaFai Lau 	if (!btf || !refcount_inc_not_zero(&btf->refcnt))
730178958fcaSMartin KaFai Lau 		btf = ERR_PTR(-ENOENT);
730278958fcaSMartin KaFai Lau 	rcu_read_unlock();
730378958fcaSMartin KaFai Lau 
730478958fcaSMartin KaFai Lau 	if (IS_ERR(btf))
730578958fcaSMartin KaFai Lau 		return PTR_ERR(btf);
730678958fcaSMartin KaFai Lau 
730778958fcaSMartin KaFai Lau 	fd = __btf_new_fd(btf);
730878958fcaSMartin KaFai Lau 	if (fd < 0)
730978958fcaSMartin KaFai Lau 		btf_put(btf);
731078958fcaSMartin KaFai Lau 
731178958fcaSMartin KaFai Lau 	return fd;
731278958fcaSMartin KaFai Lau }
731378958fcaSMartin KaFai Lau 
btf_obj_id(const struct btf * btf)731422dc4a0fSAndrii Nakryiko u32 btf_obj_id(const struct btf *btf)
731578958fcaSMartin KaFai Lau {
731678958fcaSMartin KaFai Lau 	return btf->id;
731778958fcaSMartin KaFai Lau }
7318eae2e83eSJiri Olsa 
btf_is_kernel(const struct btf * btf)7319290248a5SAndrii Nakryiko bool btf_is_kernel(const struct btf *btf)
7320290248a5SAndrii Nakryiko {
7321290248a5SAndrii Nakryiko 	return btf->kernel_btf;
7322290248a5SAndrii Nakryiko }
7323290248a5SAndrii Nakryiko 
btf_is_module(const struct btf * btf)7324541c3badSAndrii Nakryiko bool btf_is_module(const struct btf *btf)
7325541c3badSAndrii Nakryiko {
7326541c3badSAndrii Nakryiko 	return btf->kernel_btf && strcmp(btf->name, "vmlinux") != 0;
7327541c3badSAndrii Nakryiko }
7328541c3badSAndrii Nakryiko 
732918688de2SKumar Kartikeya Dwivedi enum {
733018688de2SKumar Kartikeya Dwivedi 	BTF_MODULE_F_LIVE = (1 << 0),
733118688de2SKumar Kartikeya Dwivedi };
733218688de2SKumar Kartikeya Dwivedi 
733336e68442SAndrii Nakryiko #ifdef CONFIG_DEBUG_INFO_BTF_MODULES
733436e68442SAndrii Nakryiko struct btf_module {
733536e68442SAndrii Nakryiko 	struct list_head list;
733636e68442SAndrii Nakryiko 	struct module *module;
733736e68442SAndrii Nakryiko 	struct btf *btf;
733836e68442SAndrii Nakryiko 	struct bin_attribute *sysfs_attr;
733918688de2SKumar Kartikeya Dwivedi 	int flags;
734036e68442SAndrii Nakryiko };
734136e68442SAndrii Nakryiko 
734236e68442SAndrii Nakryiko static LIST_HEAD(btf_modules);
734336e68442SAndrii Nakryiko static DEFINE_MUTEX(btf_module_mutex);
734436e68442SAndrii Nakryiko 
734536e68442SAndrii Nakryiko static ssize_t
btf_module_read(struct file * file,struct kobject * kobj,struct bin_attribute * bin_attr,char * buf,loff_t off,size_t len)734636e68442SAndrii Nakryiko btf_module_read(struct file *file, struct kobject *kobj,
734736e68442SAndrii Nakryiko 		struct bin_attribute *bin_attr,
734836e68442SAndrii Nakryiko 		char *buf, loff_t off, size_t len)
734936e68442SAndrii Nakryiko {
735036e68442SAndrii Nakryiko 	const struct btf *btf = bin_attr->private;
735136e68442SAndrii Nakryiko 
735236e68442SAndrii Nakryiko 	memcpy(buf, btf->data + off, len);
735336e68442SAndrii Nakryiko 	return len;
735436e68442SAndrii Nakryiko }
735536e68442SAndrii Nakryiko 
73561e89106dSAlexei Starovoitov static void purge_cand_cache(struct btf *btf);
73571e89106dSAlexei Starovoitov 
btf_module_notify(struct notifier_block * nb,unsigned long op,void * module)735836e68442SAndrii Nakryiko static int btf_module_notify(struct notifier_block *nb, unsigned long op,
735936e68442SAndrii Nakryiko 			     void *module)
736036e68442SAndrii Nakryiko {
736136e68442SAndrii Nakryiko 	struct btf_module *btf_mod, *tmp;
736236e68442SAndrii Nakryiko 	struct module *mod = module;
736336e68442SAndrii Nakryiko 	struct btf *btf;
736436e68442SAndrii Nakryiko 	int err = 0;
736536e68442SAndrii Nakryiko 
736636e68442SAndrii Nakryiko 	if (mod->btf_data_size == 0 ||
736718688de2SKumar Kartikeya Dwivedi 	    (op != MODULE_STATE_COMING && op != MODULE_STATE_LIVE &&
736818688de2SKumar Kartikeya Dwivedi 	     op != MODULE_STATE_GOING))
736936e68442SAndrii Nakryiko 		goto out;
737036e68442SAndrii Nakryiko 
737136e68442SAndrii Nakryiko 	switch (op) {
737236e68442SAndrii Nakryiko 	case MODULE_STATE_COMING:
737336e68442SAndrii Nakryiko 		btf_mod = kzalloc(sizeof(*btf_mod), GFP_KERNEL);
737436e68442SAndrii Nakryiko 		if (!btf_mod) {
737536e68442SAndrii Nakryiko 			err = -ENOMEM;
737636e68442SAndrii Nakryiko 			goto out;
737736e68442SAndrii Nakryiko 		}
737836e68442SAndrii Nakryiko 		btf = btf_parse_module(mod->name, mod->btf_data, mod->btf_data_size);
737936e68442SAndrii Nakryiko 		if (IS_ERR(btf)) {
73809cb61e50SConnor O'Brien 			kfree(btf_mod);
73819cb61e50SConnor O'Brien 			if (!IS_ENABLED(CONFIG_MODULE_ALLOW_BTF_MISMATCH)) {
738236e68442SAndrii Nakryiko 				pr_warn("failed to validate module [%s] BTF: %ld\n",
738336e68442SAndrii Nakryiko 					mod->name, PTR_ERR(btf));
738436e68442SAndrii Nakryiko 				err = PTR_ERR(btf);
73859cb61e50SConnor O'Brien 			} else {
73869cb61e50SConnor O'Brien 				pr_warn_once("Kernel module BTF mismatch detected, BTF debug info may be unavailable for some modules\n");
73879cb61e50SConnor O'Brien 			}
738836e68442SAndrii Nakryiko 			goto out;
738936e68442SAndrii Nakryiko 		}
739036e68442SAndrii Nakryiko 		err = btf_alloc_id(btf);
739136e68442SAndrii Nakryiko 		if (err) {
739236e68442SAndrii Nakryiko 			btf_free(btf);
739336e68442SAndrii Nakryiko 			kfree(btf_mod);
739436e68442SAndrii Nakryiko 			goto out;
739536e68442SAndrii Nakryiko 		}
739636e68442SAndrii Nakryiko 
73971e89106dSAlexei Starovoitov 		purge_cand_cache(NULL);
739836e68442SAndrii Nakryiko 		mutex_lock(&btf_module_mutex);
739936e68442SAndrii Nakryiko 		btf_mod->module = module;
740036e68442SAndrii Nakryiko 		btf_mod->btf = btf;
740136e68442SAndrii Nakryiko 		list_add(&btf_mod->list, &btf_modules);
740236e68442SAndrii Nakryiko 		mutex_unlock(&btf_module_mutex);
740336e68442SAndrii Nakryiko 
740436e68442SAndrii Nakryiko 		if (IS_ENABLED(CONFIG_SYSFS)) {
740536e68442SAndrii Nakryiko 			struct bin_attribute *attr;
740636e68442SAndrii Nakryiko 
740736e68442SAndrii Nakryiko 			attr = kzalloc(sizeof(*attr), GFP_KERNEL);
740836e68442SAndrii Nakryiko 			if (!attr)
740936e68442SAndrii Nakryiko 				goto out;
741036e68442SAndrii Nakryiko 
741136e68442SAndrii Nakryiko 			sysfs_bin_attr_init(attr);
741236e68442SAndrii Nakryiko 			attr->attr.name = btf->name;
741336e68442SAndrii Nakryiko 			attr->attr.mode = 0444;
741436e68442SAndrii Nakryiko 			attr->size = btf->data_size;
741536e68442SAndrii Nakryiko 			attr->private = btf;
741636e68442SAndrii Nakryiko 			attr->read = btf_module_read;
741736e68442SAndrii Nakryiko 
741836e68442SAndrii Nakryiko 			err = sysfs_create_bin_file(btf_kobj, attr);
741936e68442SAndrii Nakryiko 			if (err) {
742036e68442SAndrii Nakryiko 				pr_warn("failed to register module [%s] BTF in sysfs: %d\n",
742136e68442SAndrii Nakryiko 					mod->name, err);
742236e68442SAndrii Nakryiko 				kfree(attr);
742336e68442SAndrii Nakryiko 				err = 0;
742436e68442SAndrii Nakryiko 				goto out;
742536e68442SAndrii Nakryiko 			}
742636e68442SAndrii Nakryiko 
742736e68442SAndrii Nakryiko 			btf_mod->sysfs_attr = attr;
742836e68442SAndrii Nakryiko 		}
742936e68442SAndrii Nakryiko 
743036e68442SAndrii Nakryiko 		break;
743118688de2SKumar Kartikeya Dwivedi 	case MODULE_STATE_LIVE:
743218688de2SKumar Kartikeya Dwivedi 		mutex_lock(&btf_module_mutex);
743318688de2SKumar Kartikeya Dwivedi 		list_for_each_entry_safe(btf_mod, tmp, &btf_modules, list) {
743418688de2SKumar Kartikeya Dwivedi 			if (btf_mod->module != module)
743518688de2SKumar Kartikeya Dwivedi 				continue;
743618688de2SKumar Kartikeya Dwivedi 
743718688de2SKumar Kartikeya Dwivedi 			btf_mod->flags |= BTF_MODULE_F_LIVE;
743818688de2SKumar Kartikeya Dwivedi 			break;
743918688de2SKumar Kartikeya Dwivedi 		}
744018688de2SKumar Kartikeya Dwivedi 		mutex_unlock(&btf_module_mutex);
744118688de2SKumar Kartikeya Dwivedi 		break;
744236e68442SAndrii Nakryiko 	case MODULE_STATE_GOING:
744336e68442SAndrii Nakryiko 		mutex_lock(&btf_module_mutex);
744436e68442SAndrii Nakryiko 		list_for_each_entry_safe(btf_mod, tmp, &btf_modules, list) {
744536e68442SAndrii Nakryiko 			if (btf_mod->module != module)
744636e68442SAndrii Nakryiko 				continue;
744736e68442SAndrii Nakryiko 
744836e68442SAndrii Nakryiko 			list_del(&btf_mod->list);
744936e68442SAndrii Nakryiko 			if (btf_mod->sysfs_attr)
745036e68442SAndrii Nakryiko 				sysfs_remove_bin_file(btf_kobj, btf_mod->sysfs_attr);
74511e89106dSAlexei Starovoitov 			purge_cand_cache(btf_mod->btf);
745236e68442SAndrii Nakryiko 			btf_put(btf_mod->btf);
745336e68442SAndrii Nakryiko 			kfree(btf_mod->sysfs_attr);
745436e68442SAndrii Nakryiko 			kfree(btf_mod);
745536e68442SAndrii Nakryiko 			break;
745636e68442SAndrii Nakryiko 		}
745736e68442SAndrii Nakryiko 		mutex_unlock(&btf_module_mutex);
745836e68442SAndrii Nakryiko 		break;
745936e68442SAndrii Nakryiko 	}
746036e68442SAndrii Nakryiko out:
746136e68442SAndrii Nakryiko 	return notifier_from_errno(err);
746236e68442SAndrii Nakryiko }
746336e68442SAndrii Nakryiko 
746436e68442SAndrii Nakryiko static struct notifier_block btf_module_nb = {
746536e68442SAndrii Nakryiko 	.notifier_call = btf_module_notify,
746636e68442SAndrii Nakryiko };
746736e68442SAndrii Nakryiko 
btf_module_init(void)746836e68442SAndrii Nakryiko static int __init btf_module_init(void)
746936e68442SAndrii Nakryiko {
747036e68442SAndrii Nakryiko 	register_module_notifier(&btf_module_nb);
747136e68442SAndrii Nakryiko 	return 0;
747236e68442SAndrii Nakryiko }
747336e68442SAndrii Nakryiko 
747436e68442SAndrii Nakryiko fs_initcall(btf_module_init);
747536e68442SAndrii Nakryiko #endif /* CONFIG_DEBUG_INFO_BTF_MODULES */
7476541c3badSAndrii Nakryiko 
btf_try_get_module(const struct btf * btf)7477541c3badSAndrii Nakryiko struct module *btf_try_get_module(const struct btf *btf)
7478541c3badSAndrii Nakryiko {
7479541c3badSAndrii Nakryiko 	struct module *res = NULL;
7480541c3badSAndrii Nakryiko #ifdef CONFIG_DEBUG_INFO_BTF_MODULES
7481541c3badSAndrii Nakryiko 	struct btf_module *btf_mod, *tmp;
7482541c3badSAndrii Nakryiko 
7483541c3badSAndrii Nakryiko 	mutex_lock(&btf_module_mutex);
7484541c3badSAndrii Nakryiko 	list_for_each_entry_safe(btf_mod, tmp, &btf_modules, list) {
7485541c3badSAndrii Nakryiko 		if (btf_mod->btf != btf)
7486541c3badSAndrii Nakryiko 			continue;
7487541c3badSAndrii Nakryiko 
748818688de2SKumar Kartikeya Dwivedi 		/* We must only consider module whose __init routine has
748918688de2SKumar Kartikeya Dwivedi 		 * finished, hence we must check for BTF_MODULE_F_LIVE flag,
749018688de2SKumar Kartikeya Dwivedi 		 * which is set from the notifier callback for
749118688de2SKumar Kartikeya Dwivedi 		 * MODULE_STATE_LIVE.
749218688de2SKumar Kartikeya Dwivedi 		 */
749318688de2SKumar Kartikeya Dwivedi 		if ((btf_mod->flags & BTF_MODULE_F_LIVE) && try_module_get(btf_mod->module))
7494541c3badSAndrii Nakryiko 			res = btf_mod->module;
7495541c3badSAndrii Nakryiko 
7496541c3badSAndrii Nakryiko 		break;
7497541c3badSAndrii Nakryiko 	}
7498541c3badSAndrii Nakryiko 	mutex_unlock(&btf_module_mutex);
7499541c3badSAndrii Nakryiko #endif
7500541c3badSAndrii Nakryiko 
7501541c3badSAndrii Nakryiko 	return res;
7502541c3badSAndrii Nakryiko }
75033d78417bSAlexei Starovoitov 
75049492450fSKumar Kartikeya Dwivedi /* Returns struct btf corresponding to the struct module.
75059492450fSKumar Kartikeya Dwivedi  * This function can return NULL or ERR_PTR.
7506dee872e1SKumar Kartikeya Dwivedi  */
btf_get_module_btf(const struct module * module)7507dee872e1SKumar Kartikeya Dwivedi static struct btf *btf_get_module_btf(const struct module *module)
7508dee872e1SKumar Kartikeya Dwivedi {
7509dee872e1SKumar Kartikeya Dwivedi #ifdef CONFIG_DEBUG_INFO_BTF_MODULES
7510dee872e1SKumar Kartikeya Dwivedi 	struct btf_module *btf_mod, *tmp;
7511dee872e1SKumar Kartikeya Dwivedi #endif
75129492450fSKumar Kartikeya Dwivedi 	struct btf *btf = NULL;
7513dee872e1SKumar Kartikeya Dwivedi 
75149492450fSKumar Kartikeya Dwivedi 	if (!module) {
75159492450fSKumar Kartikeya Dwivedi 		btf = bpf_get_btf_vmlinux();
75167ada3787SKumar Kartikeya Dwivedi 		if (!IS_ERR_OR_NULL(btf))
75179492450fSKumar Kartikeya Dwivedi 			btf_get(btf);
75189492450fSKumar Kartikeya Dwivedi 		return btf;
75199492450fSKumar Kartikeya Dwivedi 	}
75209492450fSKumar Kartikeya Dwivedi 
7521dee872e1SKumar Kartikeya Dwivedi #ifdef CONFIG_DEBUG_INFO_BTF_MODULES
7522dee872e1SKumar Kartikeya Dwivedi 	mutex_lock(&btf_module_mutex);
7523dee872e1SKumar Kartikeya Dwivedi 	list_for_each_entry_safe(btf_mod, tmp, &btf_modules, list) {
7524dee872e1SKumar Kartikeya Dwivedi 		if (btf_mod->module != module)
7525dee872e1SKumar Kartikeya Dwivedi 			continue;
7526dee872e1SKumar Kartikeya Dwivedi 
7527dee872e1SKumar Kartikeya Dwivedi 		btf_get(btf_mod->btf);
7528dee872e1SKumar Kartikeya Dwivedi 		btf = btf_mod->btf;
7529dee872e1SKumar Kartikeya Dwivedi 		break;
7530dee872e1SKumar Kartikeya Dwivedi 	}
7531dee872e1SKumar Kartikeya Dwivedi 	mutex_unlock(&btf_module_mutex);
7532dee872e1SKumar Kartikeya Dwivedi #endif
7533dee872e1SKumar Kartikeya Dwivedi 
7534dee872e1SKumar Kartikeya Dwivedi 	return btf;
7535dee872e1SKumar Kartikeya Dwivedi }
7536dee872e1SKumar Kartikeya Dwivedi 
BPF_CALL_4(bpf_btf_find_by_name_kind,char *,name,int,name_sz,u32,kind,int,flags)75373d78417bSAlexei Starovoitov BPF_CALL_4(bpf_btf_find_by_name_kind, char *, name, int, name_sz, u32, kind, int, flags)
75383d78417bSAlexei Starovoitov {
7539edc3ec09SKumar Kartikeya Dwivedi 	struct btf *btf = NULL;
7540edc3ec09SKumar Kartikeya Dwivedi 	int btf_obj_fd = 0;
75413d78417bSAlexei Starovoitov 	long ret;
75423d78417bSAlexei Starovoitov 
75433d78417bSAlexei Starovoitov 	if (flags)
75443d78417bSAlexei Starovoitov 		return -EINVAL;
75453d78417bSAlexei Starovoitov 
75463d78417bSAlexei Starovoitov 	if (name_sz <= 1 || name[name_sz - 1])
75473d78417bSAlexei Starovoitov 		return -EINVAL;
75483d78417bSAlexei Starovoitov 
7549edc3ec09SKumar Kartikeya Dwivedi 	ret = bpf_find_btf_id(name, kind, &btf);
7550edc3ec09SKumar Kartikeya Dwivedi 	if (ret > 0 && btf_is_module(btf)) {
7551edc3ec09SKumar Kartikeya Dwivedi 		btf_obj_fd = __btf_new_fd(btf);
75523d78417bSAlexei Starovoitov 		if (btf_obj_fd < 0) {
7553edc3ec09SKumar Kartikeya Dwivedi 			btf_put(btf);
75543d78417bSAlexei Starovoitov 			return btf_obj_fd;
75553d78417bSAlexei Starovoitov 		}
75563d78417bSAlexei Starovoitov 		return ret | (((u64)btf_obj_fd) << 32);
75573d78417bSAlexei Starovoitov 	}
7558edc3ec09SKumar Kartikeya Dwivedi 	if (ret > 0)
7559edc3ec09SKumar Kartikeya Dwivedi 		btf_put(btf);
75603d78417bSAlexei Starovoitov 	return ret;
75613d78417bSAlexei Starovoitov }
75623d78417bSAlexei Starovoitov 
75633d78417bSAlexei Starovoitov const struct bpf_func_proto bpf_btf_find_by_name_kind_proto = {
75643d78417bSAlexei Starovoitov 	.func		= bpf_btf_find_by_name_kind,
75653d78417bSAlexei Starovoitov 	.gpl_only	= false,
75663d78417bSAlexei Starovoitov 	.ret_type	= RET_INTEGER,
7567216e3cd2SHao Luo 	.arg1_type	= ARG_PTR_TO_MEM | MEM_RDONLY,
75683d78417bSAlexei Starovoitov 	.arg2_type	= ARG_CONST_SIZE,
75693d78417bSAlexei Starovoitov 	.arg3_type	= ARG_ANYTHING,
75703d78417bSAlexei Starovoitov 	.arg4_type	= ARG_ANYTHING,
75713d78417bSAlexei Starovoitov };
7572eb529c5bSDaniel Xu 
BTF_ID_LIST_GLOBAL(btf_tracing_ids,MAX_BTF_TRACING_TYPE)7573d19ddb47SSong Liu BTF_ID_LIST_GLOBAL(btf_tracing_ids, MAX_BTF_TRACING_TYPE)
7574d19ddb47SSong Liu #define BTF_TRACING_TYPE(name, type) BTF_ID(struct, type)
7575d19ddb47SSong Liu BTF_TRACING_TYPE_xxx
7576d19ddb47SSong Liu #undef BTF_TRACING_TYPE
757714f267d9SKumar Kartikeya Dwivedi 
7578215bf496SAndrii Nakryiko static int btf_check_iter_kfuncs(struct btf *btf, const char *func_name,
7579215bf496SAndrii Nakryiko 				 const struct btf_type *func, u32 func_flags)
7580215bf496SAndrii Nakryiko {
7581215bf496SAndrii Nakryiko 	u32 flags = func_flags & (KF_ITER_NEW | KF_ITER_NEXT | KF_ITER_DESTROY);
7582215bf496SAndrii Nakryiko 	const char *name, *sfx, *iter_name;
7583215bf496SAndrii Nakryiko 	const struct btf_param *arg;
7584215bf496SAndrii Nakryiko 	const struct btf_type *t;
7585215bf496SAndrii Nakryiko 	char exp_name[128];
7586215bf496SAndrii Nakryiko 	u32 nr_args;
7587215bf496SAndrii Nakryiko 
7588215bf496SAndrii Nakryiko 	/* exactly one of KF_ITER_{NEW,NEXT,DESTROY} can be set */
7589215bf496SAndrii Nakryiko 	if (!flags || (flags & (flags - 1)))
7590215bf496SAndrii Nakryiko 		return -EINVAL;
7591215bf496SAndrii Nakryiko 
7592215bf496SAndrii Nakryiko 	/* any BPF iter kfunc should have `struct bpf_iter_<type> *` first arg */
7593215bf496SAndrii Nakryiko 	nr_args = btf_type_vlen(func);
7594215bf496SAndrii Nakryiko 	if (nr_args < 1)
7595215bf496SAndrii Nakryiko 		return -EINVAL;
7596215bf496SAndrii Nakryiko 
7597215bf496SAndrii Nakryiko 	arg = &btf_params(func)[0];
7598215bf496SAndrii Nakryiko 	t = btf_type_skip_modifiers(btf, arg->type, NULL);
7599215bf496SAndrii Nakryiko 	if (!t || !btf_type_is_ptr(t))
7600215bf496SAndrii Nakryiko 		return -EINVAL;
7601215bf496SAndrii Nakryiko 	t = btf_type_skip_modifiers(btf, t->type, NULL);
7602215bf496SAndrii Nakryiko 	if (!t || !__btf_type_is_struct(t))
7603215bf496SAndrii Nakryiko 		return -EINVAL;
7604215bf496SAndrii Nakryiko 
7605215bf496SAndrii Nakryiko 	name = btf_name_by_offset(btf, t->name_off);
7606215bf496SAndrii Nakryiko 	if (!name || strncmp(name, ITER_PREFIX, sizeof(ITER_PREFIX) - 1))
7607215bf496SAndrii Nakryiko 		return -EINVAL;
7608215bf496SAndrii Nakryiko 
7609215bf496SAndrii Nakryiko 	/* sizeof(struct bpf_iter_<type>) should be a multiple of 8 to
7610215bf496SAndrii Nakryiko 	 * fit nicely in stack slots
7611215bf496SAndrii Nakryiko 	 */
7612215bf496SAndrii Nakryiko 	if (t->size == 0 || (t->size % 8))
7613215bf496SAndrii Nakryiko 		return -EINVAL;
7614215bf496SAndrii Nakryiko 
7615215bf496SAndrii Nakryiko 	/* validate bpf_iter_<type>_{new,next,destroy}(struct bpf_iter_<type> *)
7616215bf496SAndrii Nakryiko 	 * naming pattern
7617215bf496SAndrii Nakryiko 	 */
7618215bf496SAndrii Nakryiko 	iter_name = name + sizeof(ITER_PREFIX) - 1;
7619215bf496SAndrii Nakryiko 	if (flags & KF_ITER_NEW)
7620215bf496SAndrii Nakryiko 		sfx = "new";
7621215bf496SAndrii Nakryiko 	else if (flags & KF_ITER_NEXT)
7622215bf496SAndrii Nakryiko 		sfx = "next";
7623215bf496SAndrii Nakryiko 	else /* (flags & KF_ITER_DESTROY) */
7624215bf496SAndrii Nakryiko 		sfx = "destroy";
7625215bf496SAndrii Nakryiko 
7626215bf496SAndrii Nakryiko 	snprintf(exp_name, sizeof(exp_name), "bpf_iter_%s_%s", iter_name, sfx);
7627215bf496SAndrii Nakryiko 	if (strcmp(func_name, exp_name))
7628215bf496SAndrii Nakryiko 		return -EINVAL;
7629215bf496SAndrii Nakryiko 
7630215bf496SAndrii Nakryiko 	/* only iter constructor should have extra arguments */
7631215bf496SAndrii Nakryiko 	if (!(flags & KF_ITER_NEW) && nr_args != 1)
7632215bf496SAndrii Nakryiko 		return -EINVAL;
7633215bf496SAndrii Nakryiko 
7634215bf496SAndrii Nakryiko 	if (flags & KF_ITER_NEXT) {
7635215bf496SAndrii Nakryiko 		/* bpf_iter_<type>_next() should return pointer */
7636215bf496SAndrii Nakryiko 		t = btf_type_skip_modifiers(btf, func->type, NULL);
7637215bf496SAndrii Nakryiko 		if (!t || !btf_type_is_ptr(t))
7638215bf496SAndrii Nakryiko 			return -EINVAL;
7639215bf496SAndrii Nakryiko 	}
7640215bf496SAndrii Nakryiko 
7641215bf496SAndrii Nakryiko 	if (flags & KF_ITER_DESTROY) {
7642215bf496SAndrii Nakryiko 		/* bpf_iter_<type>_destroy() should return void */
7643215bf496SAndrii Nakryiko 		t = btf_type_by_id(btf, func->type);
7644215bf496SAndrii Nakryiko 		if (!t || !btf_type_is_void(t))
7645215bf496SAndrii Nakryiko 			return -EINVAL;
7646215bf496SAndrii Nakryiko 	}
7647215bf496SAndrii Nakryiko 
7648215bf496SAndrii Nakryiko 	return 0;
7649215bf496SAndrii Nakryiko }
7650215bf496SAndrii Nakryiko 
btf_check_kfunc_protos(struct btf * btf,u32 func_id,u32 func_flags)7651215bf496SAndrii Nakryiko static int btf_check_kfunc_protos(struct btf *btf, u32 func_id, u32 func_flags)
7652215bf496SAndrii Nakryiko {
7653215bf496SAndrii Nakryiko 	const struct btf_type *func;
7654215bf496SAndrii Nakryiko 	const char *func_name;
7655215bf496SAndrii Nakryiko 	int err;
7656215bf496SAndrii Nakryiko 
7657215bf496SAndrii Nakryiko 	/* any kfunc should be FUNC -> FUNC_PROTO */
7658215bf496SAndrii Nakryiko 	func = btf_type_by_id(btf, func_id);
7659215bf496SAndrii Nakryiko 	if (!func || !btf_type_is_func(func))
7660215bf496SAndrii Nakryiko 		return -EINVAL;
7661215bf496SAndrii Nakryiko 
7662215bf496SAndrii Nakryiko 	/* sanity check kfunc name */
7663215bf496SAndrii Nakryiko 	func_name = btf_name_by_offset(btf, func->name_off);
7664215bf496SAndrii Nakryiko 	if (!func_name || !func_name[0])
7665215bf496SAndrii Nakryiko 		return -EINVAL;
7666215bf496SAndrii Nakryiko 
7667215bf496SAndrii Nakryiko 	func = btf_type_by_id(btf, func->type);
7668215bf496SAndrii Nakryiko 	if (!func || !btf_type_is_func_proto(func))
7669215bf496SAndrii Nakryiko 		return -EINVAL;
7670215bf496SAndrii Nakryiko 
7671215bf496SAndrii Nakryiko 	if (func_flags & (KF_ITER_NEW | KF_ITER_NEXT | KF_ITER_DESTROY)) {
7672215bf496SAndrii Nakryiko 		err = btf_check_iter_kfuncs(btf, func_name, func, func_flags);
7673215bf496SAndrii Nakryiko 		if (err)
7674215bf496SAndrii Nakryiko 			return err;
7675215bf496SAndrii Nakryiko 	}
7676215bf496SAndrii Nakryiko 
7677215bf496SAndrii Nakryiko 	return 0;
7678215bf496SAndrii Nakryiko }
7679215bf496SAndrii Nakryiko 
7680dee872e1SKumar Kartikeya Dwivedi /* Kernel Function (kfunc) BTF ID set registration API */
768114f267d9SKumar Kartikeya Dwivedi 
btf_populate_kfunc_set(struct btf * btf,enum btf_kfunc_hook hook,const struct btf_kfunc_id_set * kset)7682a4703e31SKumar Kartikeya Dwivedi static int btf_populate_kfunc_set(struct btf *btf, enum btf_kfunc_hook hook,
7683e924e80eSAditi Ghag 				  const struct btf_kfunc_id_set *kset)
768414f267d9SKumar Kartikeya Dwivedi {
7685e924e80eSAditi Ghag 	struct btf_kfunc_hook_filter *hook_filter;
7686e924e80eSAditi Ghag 	struct btf_id_set8 *add_set = kset->set;
7687a4703e31SKumar Kartikeya Dwivedi 	bool vmlinux_set = !btf_is_module(btf);
7688e924e80eSAditi Ghag 	bool add_filter = !!kset->filter;
7689dee872e1SKumar Kartikeya Dwivedi 	struct btf_kfunc_set_tab *tab;
7690a4703e31SKumar Kartikeya Dwivedi 	struct btf_id_set8 *set;
7691dee872e1SKumar Kartikeya Dwivedi 	u32 set_cnt;
7692dee872e1SKumar Kartikeya Dwivedi 	int ret;
769314f267d9SKumar Kartikeya Dwivedi 
7694a4703e31SKumar Kartikeya Dwivedi 	if (hook >= BTF_KFUNC_HOOK_MAX) {
7695dee872e1SKumar Kartikeya Dwivedi 		ret = -EINVAL;
7696dee872e1SKumar Kartikeya Dwivedi 		goto end;
7697dee872e1SKumar Kartikeya Dwivedi 	}
7698dee872e1SKumar Kartikeya Dwivedi 
7699dee872e1SKumar Kartikeya Dwivedi 	if (!add_set->cnt)
7700dee872e1SKumar Kartikeya Dwivedi 		return 0;
7701dee872e1SKumar Kartikeya Dwivedi 
7702dee872e1SKumar Kartikeya Dwivedi 	tab = btf->kfunc_set_tab;
7703e924e80eSAditi Ghag 
7704e924e80eSAditi Ghag 	if (tab && add_filter) {
7705e924e80eSAditi Ghag 		u32 i;
7706e924e80eSAditi Ghag 
7707e924e80eSAditi Ghag 		hook_filter = &tab->hook_filters[hook];
7708e924e80eSAditi Ghag 		for (i = 0; i < hook_filter->nr_filters; i++) {
7709e924e80eSAditi Ghag 			if (hook_filter->filters[i] == kset->filter) {
7710e924e80eSAditi Ghag 				add_filter = false;
7711e924e80eSAditi Ghag 				break;
7712e924e80eSAditi Ghag 			}
7713e924e80eSAditi Ghag 		}
7714e924e80eSAditi Ghag 
7715e924e80eSAditi Ghag 		if (add_filter && hook_filter->nr_filters == BTF_KFUNC_FILTER_MAX_CNT) {
7716e924e80eSAditi Ghag 			ret = -E2BIG;
7717e924e80eSAditi Ghag 			goto end;
7718e924e80eSAditi Ghag 		}
7719e924e80eSAditi Ghag 	}
7720e924e80eSAditi Ghag 
7721dee872e1SKumar Kartikeya Dwivedi 	if (!tab) {
7722dee872e1SKumar Kartikeya Dwivedi 		tab = kzalloc(sizeof(*tab), GFP_KERNEL | __GFP_NOWARN);
7723dee872e1SKumar Kartikeya Dwivedi 		if (!tab)
7724dee872e1SKumar Kartikeya Dwivedi 			return -ENOMEM;
7725dee872e1SKumar Kartikeya Dwivedi 		btf->kfunc_set_tab = tab;
7726dee872e1SKumar Kartikeya Dwivedi 	}
7727dee872e1SKumar Kartikeya Dwivedi 
7728a4703e31SKumar Kartikeya Dwivedi 	set = tab->sets[hook];
7729dee872e1SKumar Kartikeya Dwivedi 	/* Warn when register_btf_kfunc_id_set is called twice for the same hook
7730dee872e1SKumar Kartikeya Dwivedi 	 * for module sets.
7731dee872e1SKumar Kartikeya Dwivedi 	 */
7732dee872e1SKumar Kartikeya Dwivedi 	if (WARN_ON_ONCE(set && !vmlinux_set)) {
7733dee872e1SKumar Kartikeya Dwivedi 		ret = -EINVAL;
7734dee872e1SKumar Kartikeya Dwivedi 		goto end;
7735dee872e1SKumar Kartikeya Dwivedi 	}
7736dee872e1SKumar Kartikeya Dwivedi 
7737dee872e1SKumar Kartikeya Dwivedi 	/* We don't need to allocate, concatenate, and sort module sets, because
7738dee872e1SKumar Kartikeya Dwivedi 	 * only one is allowed per hook. Hence, we can directly assign the
7739dee872e1SKumar Kartikeya Dwivedi 	 * pointer and return.
7740dee872e1SKumar Kartikeya Dwivedi 	 */
7741dee872e1SKumar Kartikeya Dwivedi 	if (!vmlinux_set) {
7742a4703e31SKumar Kartikeya Dwivedi 		tab->sets[hook] = add_set;
7743e924e80eSAditi Ghag 		goto do_add_filter;
7744dee872e1SKumar Kartikeya Dwivedi 	}
7745dee872e1SKumar Kartikeya Dwivedi 
7746dee872e1SKumar Kartikeya Dwivedi 	/* In case of vmlinux sets, there may be more than one set being
7747dee872e1SKumar Kartikeya Dwivedi 	 * registered per hook. To create a unified set, we allocate a new set
7748dee872e1SKumar Kartikeya Dwivedi 	 * and concatenate all individual sets being registered. While each set
7749dee872e1SKumar Kartikeya Dwivedi 	 * is individually sorted, they may become unsorted when concatenated,
7750dee872e1SKumar Kartikeya Dwivedi 	 * hence re-sorting the final set again is required to make binary
7751a4703e31SKumar Kartikeya Dwivedi 	 * searching the set using btf_id_set8_contains function work.
7752dee872e1SKumar Kartikeya Dwivedi 	 */
7753dee872e1SKumar Kartikeya Dwivedi 	set_cnt = set ? set->cnt : 0;
7754dee872e1SKumar Kartikeya Dwivedi 
7755dee872e1SKumar Kartikeya Dwivedi 	if (set_cnt > U32_MAX - add_set->cnt) {
7756dee872e1SKumar Kartikeya Dwivedi 		ret = -EOVERFLOW;
7757dee872e1SKumar Kartikeya Dwivedi 		goto end;
7758dee872e1SKumar Kartikeya Dwivedi 	}
7759dee872e1SKumar Kartikeya Dwivedi 
7760dee872e1SKumar Kartikeya Dwivedi 	if (set_cnt + add_set->cnt > BTF_KFUNC_SET_MAX_CNT) {
7761dee872e1SKumar Kartikeya Dwivedi 		ret = -E2BIG;
7762dee872e1SKumar Kartikeya Dwivedi 		goto end;
7763dee872e1SKumar Kartikeya Dwivedi 	}
7764dee872e1SKumar Kartikeya Dwivedi 
7765dee872e1SKumar Kartikeya Dwivedi 	/* Grow set */
7766a4703e31SKumar Kartikeya Dwivedi 	set = krealloc(tab->sets[hook],
7767a4703e31SKumar Kartikeya Dwivedi 		       offsetof(struct btf_id_set8, pairs[set_cnt + add_set->cnt]),
7768dee872e1SKumar Kartikeya Dwivedi 		       GFP_KERNEL | __GFP_NOWARN);
7769dee872e1SKumar Kartikeya Dwivedi 	if (!set) {
7770dee872e1SKumar Kartikeya Dwivedi 		ret = -ENOMEM;
7771dee872e1SKumar Kartikeya Dwivedi 		goto end;
7772dee872e1SKumar Kartikeya Dwivedi 	}
7773dee872e1SKumar Kartikeya Dwivedi 
7774dee872e1SKumar Kartikeya Dwivedi 	/* For newly allocated set, initialize set->cnt to 0 */
7775a4703e31SKumar Kartikeya Dwivedi 	if (!tab->sets[hook])
7776dee872e1SKumar Kartikeya Dwivedi 		set->cnt = 0;
7777a4703e31SKumar Kartikeya Dwivedi 	tab->sets[hook] = set;
7778dee872e1SKumar Kartikeya Dwivedi 
7779dee872e1SKumar Kartikeya Dwivedi 	/* Concatenate the two sets */
7780a4703e31SKumar Kartikeya Dwivedi 	memcpy(set->pairs + set->cnt, add_set->pairs, add_set->cnt * sizeof(set->pairs[0]));
7781dee872e1SKumar Kartikeya Dwivedi 	set->cnt += add_set->cnt;
7782dee872e1SKumar Kartikeya Dwivedi 
7783a4703e31SKumar Kartikeya Dwivedi 	sort(set->pairs, set->cnt, sizeof(set->pairs[0]), btf_id_cmp_func, NULL);
7784dee872e1SKumar Kartikeya Dwivedi 
7785e924e80eSAditi Ghag do_add_filter:
7786e924e80eSAditi Ghag 	if (add_filter) {
7787e924e80eSAditi Ghag 		hook_filter = &tab->hook_filters[hook];
7788e924e80eSAditi Ghag 		hook_filter->filters[hook_filter->nr_filters++] = kset->filter;
7789e924e80eSAditi Ghag 	}
7790dee872e1SKumar Kartikeya Dwivedi 	return 0;
7791dee872e1SKumar Kartikeya Dwivedi end:
7792dee872e1SKumar Kartikeya Dwivedi 	btf_free_kfunc_set_tab(btf);
7793dee872e1SKumar Kartikeya Dwivedi 	return ret;
7794dee872e1SKumar Kartikeya Dwivedi }
7795dee872e1SKumar Kartikeya Dwivedi 
__btf_kfunc_id_set_contains(const struct btf * btf,enum btf_kfunc_hook hook,u32 kfunc_btf_id,const struct bpf_prog * prog)7796a4703e31SKumar Kartikeya Dwivedi static u32 *__btf_kfunc_id_set_contains(const struct btf *btf,
7797dee872e1SKumar Kartikeya Dwivedi 					enum btf_kfunc_hook hook,
7798e924e80eSAditi Ghag 					u32 kfunc_btf_id,
7799e924e80eSAditi Ghag 					const struct bpf_prog *prog)
780014f267d9SKumar Kartikeya Dwivedi {
7801e924e80eSAditi Ghag 	struct btf_kfunc_hook_filter *hook_filter;
7802a4703e31SKumar Kartikeya Dwivedi 	struct btf_id_set8 *set;
7803e924e80eSAditi Ghag 	u32 *id, i;
780414f267d9SKumar Kartikeya Dwivedi 
7805a4703e31SKumar Kartikeya Dwivedi 	if (hook >= BTF_KFUNC_HOOK_MAX)
7806a4703e31SKumar Kartikeya Dwivedi 		return NULL;
7807dee872e1SKumar Kartikeya Dwivedi 	if (!btf->kfunc_set_tab)
7808a4703e31SKumar Kartikeya Dwivedi 		return NULL;
7809e924e80eSAditi Ghag 	hook_filter = &btf->kfunc_set_tab->hook_filters[hook];
7810e924e80eSAditi Ghag 	for (i = 0; i < hook_filter->nr_filters; i++) {
7811e924e80eSAditi Ghag 		if (hook_filter->filters[i](prog, kfunc_btf_id))
7812e924e80eSAditi Ghag 			return NULL;
7813e924e80eSAditi Ghag 	}
7814a4703e31SKumar Kartikeya Dwivedi 	set = btf->kfunc_set_tab->sets[hook];
7815dee872e1SKumar Kartikeya Dwivedi 	if (!set)
7816a4703e31SKumar Kartikeya Dwivedi 		return NULL;
7817a4703e31SKumar Kartikeya Dwivedi 	id = btf_id_set8_contains(set, kfunc_btf_id);
7818a4703e31SKumar Kartikeya Dwivedi 	if (!id)
7819a4703e31SKumar Kartikeya Dwivedi 		return NULL;
7820a4703e31SKumar Kartikeya Dwivedi 	/* The flags for BTF ID are located next to it */
7821a4703e31SKumar Kartikeya Dwivedi 	return id + 1;
782214f267d9SKumar Kartikeya Dwivedi }
782314f267d9SKumar Kartikeya Dwivedi 
bpf_prog_type_to_kfunc_hook(enum bpf_prog_type prog_type)7824dee872e1SKumar Kartikeya Dwivedi static int bpf_prog_type_to_kfunc_hook(enum bpf_prog_type prog_type)
7825dee872e1SKumar Kartikeya Dwivedi {
7826dee872e1SKumar Kartikeya Dwivedi 	switch (prog_type) {
7827cfe14564SYonghong Song 	case BPF_PROG_TYPE_UNSPEC:
7828cfe14564SYonghong Song 		return BTF_KFUNC_HOOK_COMMON;
7829dee872e1SKumar Kartikeya Dwivedi 	case BPF_PROG_TYPE_XDP:
7830dee872e1SKumar Kartikeya Dwivedi 		return BTF_KFUNC_HOOK_XDP;
7831dee872e1SKumar Kartikeya Dwivedi 	case BPF_PROG_TYPE_SCHED_CLS:
7832dee872e1SKumar Kartikeya Dwivedi 		return BTF_KFUNC_HOOK_TC;
7833dee872e1SKumar Kartikeya Dwivedi 	case BPF_PROG_TYPE_STRUCT_OPS:
7834dee872e1SKumar Kartikeya Dwivedi 		return BTF_KFUNC_HOOK_STRUCT_OPS;
783597949767SBenjamin Tissoires 	case BPF_PROG_TYPE_TRACING:
7836d15bf150SKP Singh 	case BPF_PROG_TYPE_LSM:
783797949767SBenjamin Tissoires 		return BTF_KFUNC_HOOK_TRACING;
783897949767SBenjamin Tissoires 	case BPF_PROG_TYPE_SYSCALL:
783997949767SBenjamin Tissoires 		return BTF_KFUNC_HOOK_SYSCALL;
7840b5964b96SJoanne Koong 	case BPF_PROG_TYPE_CGROUP_SKB:
78411474a8afSDaan De Meyer 	case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
7842b5964b96SJoanne Koong 		return BTF_KFUNC_HOOK_CGROUP_SKB;
7843b5964b96SJoanne Koong 	case BPF_PROG_TYPE_SCHED_ACT:
7844b5964b96SJoanne Koong 		return BTF_KFUNC_HOOK_SCHED_ACT;
7845b5964b96SJoanne Koong 	case BPF_PROG_TYPE_SK_SKB:
7846b5964b96SJoanne Koong 		return BTF_KFUNC_HOOK_SK_SKB;
7847b5964b96SJoanne Koong 	case BPF_PROG_TYPE_SOCKET_FILTER:
7848b5964b96SJoanne Koong 		return BTF_KFUNC_HOOK_SOCKET_FILTER;
7849b5964b96SJoanne Koong 	case BPF_PROG_TYPE_LWT_OUT:
7850b5964b96SJoanne Koong 	case BPF_PROG_TYPE_LWT_IN:
7851b5964b96SJoanne Koong 	case BPF_PROG_TYPE_LWT_XMIT:
7852b5964b96SJoanne Koong 	case BPF_PROG_TYPE_LWT_SEG6LOCAL:
7853b5964b96SJoanne Koong 		return BTF_KFUNC_HOOK_LWT;
7854fd9c663bSFlorian Westphal 	case BPF_PROG_TYPE_NETFILTER:
7855fd9c663bSFlorian Westphal 		return BTF_KFUNC_HOOK_NETFILTER;
7856dee872e1SKumar Kartikeya Dwivedi 	default:
7857dee872e1SKumar Kartikeya Dwivedi 		return BTF_KFUNC_HOOK_MAX;
7858dee872e1SKumar Kartikeya Dwivedi 	}
7859dee872e1SKumar Kartikeya Dwivedi }
78600e32dfc8SKumar Kartikeya Dwivedi 
7861dee872e1SKumar Kartikeya Dwivedi /* Caution:
7862dee872e1SKumar Kartikeya Dwivedi  * Reference to the module (obtained using btf_try_get_module) corresponding to
7863dee872e1SKumar Kartikeya Dwivedi  * the struct btf *MUST* be held when calling this function from verifier
7864dee872e1SKumar Kartikeya Dwivedi  * context. This is usually true as we stash references in prog's kfunc_btf_tab;
7865dee872e1SKumar Kartikeya Dwivedi  * keeping the reference for the duration of the call provides the necessary
7866dee872e1SKumar Kartikeya Dwivedi  * protection for looking up a well-formed btf->kfunc_set_tab.
7867dee872e1SKumar Kartikeya Dwivedi  */
btf_kfunc_id_set_contains(const struct btf * btf,u32 kfunc_btf_id,const struct bpf_prog * prog)7868a4703e31SKumar Kartikeya Dwivedi u32 *btf_kfunc_id_set_contains(const struct btf *btf,
7869e924e80eSAditi Ghag 			       u32 kfunc_btf_id,
7870e924e80eSAditi Ghag 			       const struct bpf_prog *prog)
7871dee872e1SKumar Kartikeya Dwivedi {
7872e924e80eSAditi Ghag 	enum bpf_prog_type prog_type = resolve_prog_type(prog);
7873dee872e1SKumar Kartikeya Dwivedi 	enum btf_kfunc_hook hook;
7874cfe14564SYonghong Song 	u32 *kfunc_flags;
7875cfe14564SYonghong Song 
7876e924e80eSAditi Ghag 	kfunc_flags = __btf_kfunc_id_set_contains(btf, BTF_KFUNC_HOOK_COMMON, kfunc_btf_id, prog);
7877cfe14564SYonghong Song 	if (kfunc_flags)
7878cfe14564SYonghong Song 		return kfunc_flags;
7879d9847eb8SKumar Kartikeya Dwivedi 
7880dee872e1SKumar Kartikeya Dwivedi 	hook = bpf_prog_type_to_kfunc_hook(prog_type);
7881e924e80eSAditi Ghag 	return __btf_kfunc_id_set_contains(btf, hook, kfunc_btf_id, prog);
7882dee872e1SKumar Kartikeya Dwivedi }
7883dee872e1SKumar Kartikeya Dwivedi 
btf_kfunc_is_modify_return(const struct btf * btf,u32 kfunc_btf_id,const struct bpf_prog * prog)7884e924e80eSAditi Ghag u32 *btf_kfunc_is_modify_return(const struct btf *btf, u32 kfunc_btf_id,
7885e924e80eSAditi Ghag 				const struct bpf_prog *prog)
78865b481acaSBenjamin Tissoires {
7887e924e80eSAditi Ghag 	return __btf_kfunc_id_set_contains(btf, BTF_KFUNC_HOOK_FMODRET, kfunc_btf_id, prog);
78885b481acaSBenjamin Tissoires }
78895b481acaSBenjamin Tissoires 
__register_btf_kfunc_id_set(enum btf_kfunc_hook hook,const struct btf_kfunc_id_set * kset)78905b481acaSBenjamin Tissoires static int __register_btf_kfunc_id_set(enum btf_kfunc_hook hook,
7891dee872e1SKumar Kartikeya Dwivedi 				       const struct btf_kfunc_id_set *kset)
7892dee872e1SKumar Kartikeya Dwivedi {
7893dee872e1SKumar Kartikeya Dwivedi 	struct btf *btf;
7894215bf496SAndrii Nakryiko 	int ret, i;
7895dee872e1SKumar Kartikeya Dwivedi 
7896dee872e1SKumar Kartikeya Dwivedi 	btf = btf_get_module_btf(kset->owner);
7897c446fdacSStanislav Fomichev 	if (!btf) {
7898c446fdacSStanislav Fomichev 		if (!kset->owner && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) {
7899c446fdacSStanislav Fomichev 			pr_err("missing vmlinux BTF, cannot register kfuncs\n");
7900c446fdacSStanislav Fomichev 			return -ENOENT;
7901c446fdacSStanislav Fomichev 		}
79023de4d22cSSeongJae Park 		if (kset->owner && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES))
79033de4d22cSSeongJae Park 			pr_warn("missing module BTF, cannot register kfuncs\n");
7904c446fdacSStanislav Fomichev 		return 0;
7905c446fdacSStanislav Fomichev 	}
7906c446fdacSStanislav Fomichev 	if (IS_ERR(btf))
7907c446fdacSStanislav Fomichev 		return PTR_ERR(btf);
7908dee872e1SKumar Kartikeya Dwivedi 
7909215bf496SAndrii Nakryiko 	for (i = 0; i < kset->set->cnt; i++) {
7910215bf496SAndrii Nakryiko 		ret = btf_check_kfunc_protos(btf, kset->set->pairs[i].id,
7911215bf496SAndrii Nakryiko 					     kset->set->pairs[i].flags);
7912215bf496SAndrii Nakryiko 		if (ret)
7913215bf496SAndrii Nakryiko 			goto err_out;
7914215bf496SAndrii Nakryiko 	}
7915215bf496SAndrii Nakryiko 
7916e924e80eSAditi Ghag 	ret = btf_populate_kfunc_set(btf, hook, kset);
7917e924e80eSAditi Ghag 
7918215bf496SAndrii Nakryiko err_out:
7919dee872e1SKumar Kartikeya Dwivedi 	btf_put(btf);
7920dee872e1SKumar Kartikeya Dwivedi 	return ret;
7921dee872e1SKumar Kartikeya Dwivedi }
79225b481acaSBenjamin Tissoires 
79235b481acaSBenjamin Tissoires /* This function must be invoked only from initcalls/module init functions */
register_btf_kfunc_id_set(enum bpf_prog_type prog_type,const struct btf_kfunc_id_set * kset)79245b481acaSBenjamin Tissoires int register_btf_kfunc_id_set(enum bpf_prog_type prog_type,
79255b481acaSBenjamin Tissoires 			      const struct btf_kfunc_id_set *kset)
79265b481acaSBenjamin Tissoires {
79275b481acaSBenjamin Tissoires 	enum btf_kfunc_hook hook;
79285b481acaSBenjamin Tissoires 
79295b481acaSBenjamin Tissoires 	hook = bpf_prog_type_to_kfunc_hook(prog_type);
79305b481acaSBenjamin Tissoires 	return __register_btf_kfunc_id_set(hook, kset);
79315b481acaSBenjamin Tissoires }
7932dee872e1SKumar Kartikeya Dwivedi EXPORT_SYMBOL_GPL(register_btf_kfunc_id_set);
7933be315829SJakub Kicinski 
79345b481acaSBenjamin Tissoires /* This function must be invoked only from initcalls/module init functions */
register_btf_fmodret_id_set(const struct btf_kfunc_id_set * kset)79355b481acaSBenjamin Tissoires int register_btf_fmodret_id_set(const struct btf_kfunc_id_set *kset)
79365b481acaSBenjamin Tissoires {
79375b481acaSBenjamin Tissoires 	return __register_btf_kfunc_id_set(BTF_KFUNC_HOOK_FMODRET, kset);
79385b481acaSBenjamin Tissoires }
79395b481acaSBenjamin Tissoires EXPORT_SYMBOL_GPL(register_btf_fmodret_id_set);
79405b481acaSBenjamin Tissoires 
btf_find_dtor_kfunc(struct btf * btf,u32 btf_id)79415ce937d6SKumar Kartikeya Dwivedi s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id)
79425ce937d6SKumar Kartikeya Dwivedi {
79435ce937d6SKumar Kartikeya Dwivedi 	struct btf_id_dtor_kfunc_tab *tab = btf->dtor_kfunc_tab;
79445ce937d6SKumar Kartikeya Dwivedi 	struct btf_id_dtor_kfunc *dtor;
79455ce937d6SKumar Kartikeya Dwivedi 
79465ce937d6SKumar Kartikeya Dwivedi 	if (!tab)
79475ce937d6SKumar Kartikeya Dwivedi 		return -ENOENT;
79485ce937d6SKumar Kartikeya Dwivedi 	/* Even though the size of tab->dtors[0] is > sizeof(u32), we only need
79495ce937d6SKumar Kartikeya Dwivedi 	 * to compare the first u32 with btf_id, so we can reuse btf_id_cmp_func.
79505ce937d6SKumar Kartikeya Dwivedi 	 */
79515ce937d6SKumar Kartikeya Dwivedi 	BUILD_BUG_ON(offsetof(struct btf_id_dtor_kfunc, btf_id) != 0);
79525ce937d6SKumar Kartikeya Dwivedi 	dtor = bsearch(&btf_id, tab->dtors, tab->cnt, sizeof(tab->dtors[0]), btf_id_cmp_func);
79535ce937d6SKumar Kartikeya Dwivedi 	if (!dtor)
79545ce937d6SKumar Kartikeya Dwivedi 		return -ENOENT;
79555ce937d6SKumar Kartikeya Dwivedi 	return dtor->kfunc_btf_id;
79565ce937d6SKumar Kartikeya Dwivedi }
79575ce937d6SKumar Kartikeya Dwivedi 
btf_check_dtor_kfuncs(struct btf * btf,const struct btf_id_dtor_kfunc * dtors,u32 cnt)795814a324f6SKumar Kartikeya Dwivedi static int btf_check_dtor_kfuncs(struct btf *btf, const struct btf_id_dtor_kfunc *dtors, u32 cnt)
795914a324f6SKumar Kartikeya Dwivedi {
796014a324f6SKumar Kartikeya Dwivedi 	const struct btf_type *dtor_func, *dtor_func_proto, *t;
796114a324f6SKumar Kartikeya Dwivedi 	const struct btf_param *args;
796214a324f6SKumar Kartikeya Dwivedi 	s32 dtor_btf_id;
796314a324f6SKumar Kartikeya Dwivedi 	u32 nr_args, i;
796414a324f6SKumar Kartikeya Dwivedi 
796514a324f6SKumar Kartikeya Dwivedi 	for (i = 0; i < cnt; i++) {
796614a324f6SKumar Kartikeya Dwivedi 		dtor_btf_id = dtors[i].kfunc_btf_id;
796714a324f6SKumar Kartikeya Dwivedi 
796814a324f6SKumar Kartikeya Dwivedi 		dtor_func = btf_type_by_id(btf, dtor_btf_id);
796914a324f6SKumar Kartikeya Dwivedi 		if (!dtor_func || !btf_type_is_func(dtor_func))
797014a324f6SKumar Kartikeya Dwivedi 			return -EINVAL;
797114a324f6SKumar Kartikeya Dwivedi 
797214a324f6SKumar Kartikeya Dwivedi 		dtor_func_proto = btf_type_by_id(btf, dtor_func->type);
797314a324f6SKumar Kartikeya Dwivedi 		if (!dtor_func_proto || !btf_type_is_func_proto(dtor_func_proto))
797414a324f6SKumar Kartikeya Dwivedi 			return -EINVAL;
797514a324f6SKumar Kartikeya Dwivedi 
797614a324f6SKumar Kartikeya Dwivedi 		/* Make sure the prototype of the destructor kfunc is 'void func(type *)' */
797714a324f6SKumar Kartikeya Dwivedi 		t = btf_type_by_id(btf, dtor_func_proto->type);
797814a324f6SKumar Kartikeya Dwivedi 		if (!t || !btf_type_is_void(t))
797914a324f6SKumar Kartikeya Dwivedi 			return -EINVAL;
798014a324f6SKumar Kartikeya Dwivedi 
798114a324f6SKumar Kartikeya Dwivedi 		nr_args = btf_type_vlen(dtor_func_proto);
798214a324f6SKumar Kartikeya Dwivedi 		if (nr_args != 1)
798314a324f6SKumar Kartikeya Dwivedi 			return -EINVAL;
798414a324f6SKumar Kartikeya Dwivedi 		args = btf_params(dtor_func_proto);
798514a324f6SKumar Kartikeya Dwivedi 		t = btf_type_by_id(btf, args[0].type);
798614a324f6SKumar Kartikeya Dwivedi 		/* Allow any pointer type, as width on targets Linux supports
798714a324f6SKumar Kartikeya Dwivedi 		 * will be same for all pointer types (i.e. sizeof(void *))
798814a324f6SKumar Kartikeya Dwivedi 		 */
798914a324f6SKumar Kartikeya Dwivedi 		if (!t || !btf_type_is_ptr(t))
799014a324f6SKumar Kartikeya Dwivedi 			return -EINVAL;
799114a324f6SKumar Kartikeya Dwivedi 	}
799214a324f6SKumar Kartikeya Dwivedi 	return 0;
799314a324f6SKumar Kartikeya Dwivedi }
799414a324f6SKumar Kartikeya Dwivedi 
79955ce937d6SKumar Kartikeya Dwivedi /* This function must be invoked only from initcalls/module init functions */
register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc * dtors,u32 add_cnt,struct module * owner)79965ce937d6SKumar Kartikeya Dwivedi int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors, u32 add_cnt,
79975ce937d6SKumar Kartikeya Dwivedi 				struct module *owner)
79985ce937d6SKumar Kartikeya Dwivedi {
79995ce937d6SKumar Kartikeya Dwivedi 	struct btf_id_dtor_kfunc_tab *tab;
80005ce937d6SKumar Kartikeya Dwivedi 	struct btf *btf;
80015ce937d6SKumar Kartikeya Dwivedi 	u32 tab_cnt;
80025ce937d6SKumar Kartikeya Dwivedi 	int ret;
80035ce937d6SKumar Kartikeya Dwivedi 
80045ce937d6SKumar Kartikeya Dwivedi 	btf = btf_get_module_btf(owner);
80055ce937d6SKumar Kartikeya Dwivedi 	if (!btf) {
80065ce937d6SKumar Kartikeya Dwivedi 		if (!owner && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) {
80075ce937d6SKumar Kartikeya Dwivedi 			pr_err("missing vmlinux BTF, cannot register dtor kfuncs\n");
80085ce937d6SKumar Kartikeya Dwivedi 			return -ENOENT;
80095ce937d6SKumar Kartikeya Dwivedi 		}
80105ce937d6SKumar Kartikeya Dwivedi 		if (owner && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES)) {
80115ce937d6SKumar Kartikeya Dwivedi 			pr_err("missing module BTF, cannot register dtor kfuncs\n");
80125ce937d6SKumar Kartikeya Dwivedi 			return -ENOENT;
80135ce937d6SKumar Kartikeya Dwivedi 		}
80145ce937d6SKumar Kartikeya Dwivedi 		return 0;
80155ce937d6SKumar Kartikeya Dwivedi 	}
80165ce937d6SKumar Kartikeya Dwivedi 	if (IS_ERR(btf))
80175ce937d6SKumar Kartikeya Dwivedi 		return PTR_ERR(btf);
80185ce937d6SKumar Kartikeya Dwivedi 
80195ce937d6SKumar Kartikeya Dwivedi 	if (add_cnt >= BTF_DTOR_KFUNC_MAX_CNT) {
80205ce937d6SKumar Kartikeya Dwivedi 		pr_err("cannot register more than %d kfunc destructors\n", BTF_DTOR_KFUNC_MAX_CNT);
80215ce937d6SKumar Kartikeya Dwivedi 		ret = -E2BIG;
80225ce937d6SKumar Kartikeya Dwivedi 		goto end;
80235ce937d6SKumar Kartikeya Dwivedi 	}
80245ce937d6SKumar Kartikeya Dwivedi 
802514a324f6SKumar Kartikeya Dwivedi 	/* Ensure that the prototype of dtor kfuncs being registered is sane */
802614a324f6SKumar Kartikeya Dwivedi 	ret = btf_check_dtor_kfuncs(btf, dtors, add_cnt);
802714a324f6SKumar Kartikeya Dwivedi 	if (ret < 0)
802814a324f6SKumar Kartikeya Dwivedi 		goto end;
802914a324f6SKumar Kartikeya Dwivedi 
80305ce937d6SKumar Kartikeya Dwivedi 	tab = btf->dtor_kfunc_tab;
80315ce937d6SKumar Kartikeya Dwivedi 	/* Only one call allowed for modules */
80325ce937d6SKumar Kartikeya Dwivedi 	if (WARN_ON_ONCE(tab && btf_is_module(btf))) {
80335ce937d6SKumar Kartikeya Dwivedi 		ret = -EINVAL;
80345ce937d6SKumar Kartikeya Dwivedi 		goto end;
80355ce937d6SKumar Kartikeya Dwivedi 	}
80365ce937d6SKumar Kartikeya Dwivedi 
80375ce937d6SKumar Kartikeya Dwivedi 	tab_cnt = tab ? tab->cnt : 0;
80385ce937d6SKumar Kartikeya Dwivedi 	if (tab_cnt > U32_MAX - add_cnt) {
80395ce937d6SKumar Kartikeya Dwivedi 		ret = -EOVERFLOW;
80405ce937d6SKumar Kartikeya Dwivedi 		goto end;
80415ce937d6SKumar Kartikeya Dwivedi 	}
80425ce937d6SKumar Kartikeya Dwivedi 	if (tab_cnt + add_cnt >= BTF_DTOR_KFUNC_MAX_CNT) {
80435ce937d6SKumar Kartikeya Dwivedi 		pr_err("cannot register more than %d kfunc destructors\n", BTF_DTOR_KFUNC_MAX_CNT);
80445ce937d6SKumar Kartikeya Dwivedi 		ret = -E2BIG;
80455ce937d6SKumar Kartikeya Dwivedi 		goto end;
80465ce937d6SKumar Kartikeya Dwivedi 	}
80475ce937d6SKumar Kartikeya Dwivedi 
80485ce937d6SKumar Kartikeya Dwivedi 	tab = krealloc(btf->dtor_kfunc_tab,
80495ce937d6SKumar Kartikeya Dwivedi 		       offsetof(struct btf_id_dtor_kfunc_tab, dtors[tab_cnt + add_cnt]),
80505ce937d6SKumar Kartikeya Dwivedi 		       GFP_KERNEL | __GFP_NOWARN);
80515ce937d6SKumar Kartikeya Dwivedi 	if (!tab) {
80525ce937d6SKumar Kartikeya Dwivedi 		ret = -ENOMEM;
80535ce937d6SKumar Kartikeya Dwivedi 		goto end;
80545ce937d6SKumar Kartikeya Dwivedi 	}
80555ce937d6SKumar Kartikeya Dwivedi 
80565ce937d6SKumar Kartikeya Dwivedi 	if (!btf->dtor_kfunc_tab)
80575ce937d6SKumar Kartikeya Dwivedi 		tab->cnt = 0;
80585ce937d6SKumar Kartikeya Dwivedi 	btf->dtor_kfunc_tab = tab;
80595ce937d6SKumar Kartikeya Dwivedi 
80605ce937d6SKumar Kartikeya Dwivedi 	memcpy(tab->dtors + tab->cnt, dtors, add_cnt * sizeof(tab->dtors[0]));
80615ce937d6SKumar Kartikeya Dwivedi 	tab->cnt += add_cnt;
80625ce937d6SKumar Kartikeya Dwivedi 
80635ce937d6SKumar Kartikeya Dwivedi 	sort(tab->dtors, tab->cnt, sizeof(tab->dtors[0]), btf_id_cmp_func, NULL);
80645ce937d6SKumar Kartikeya Dwivedi 
80655ce937d6SKumar Kartikeya Dwivedi end:
806674bc3a5aSJiri Olsa 	if (ret)
80675ce937d6SKumar Kartikeya Dwivedi 		btf_free_dtor_kfunc_tab(btf);
80685ce937d6SKumar Kartikeya Dwivedi 	btf_put(btf);
80695ce937d6SKumar Kartikeya Dwivedi 	return ret;
80705ce937d6SKumar Kartikeya Dwivedi }
80715ce937d6SKumar Kartikeya Dwivedi EXPORT_SYMBOL_GPL(register_btf_id_dtor_kfuncs);
80725ce937d6SKumar Kartikeya Dwivedi 
8073e70e13e7SMatteo Croce #define MAX_TYPES_ARE_COMPAT_DEPTH 2
8074e70e13e7SMatteo Croce 
8075e70e13e7SMatteo Croce /* Check local and target types for compatibility. This check is used for
8076e70e13e7SMatteo Croce  * type-based CO-RE relocations and follow slightly different rules than
8077e70e13e7SMatteo Croce  * field-based relocations. This function assumes that root types were already
8078e70e13e7SMatteo Croce  * checked for name match. Beyond that initial root-level name check, names
8079e70e13e7SMatteo Croce  * are completely ignored. Compatibility rules are as follows:
80806089fb32SYonghong Song  *   - any two STRUCTs/UNIONs/FWDs/ENUMs/INTs/ENUM64s are considered compatible, but
8081e70e13e7SMatteo Croce  *     kind should match for local and target types (i.e., STRUCT is not
8082e70e13e7SMatteo Croce  *     compatible with UNION);
80836089fb32SYonghong Song  *   - for ENUMs/ENUM64s, the size is ignored;
8084e70e13e7SMatteo Croce  *   - for INT, size and signedness are ignored;
8085e70e13e7SMatteo Croce  *   - for ARRAY, dimensionality is ignored, element types are checked for
8086e70e13e7SMatteo Croce  *     compatibility recursively;
8087e70e13e7SMatteo Croce  *   - CONST/VOLATILE/RESTRICT modifiers are ignored;
8088e70e13e7SMatteo Croce  *   - TYPEDEFs/PTRs are compatible if types they pointing to are compatible;
8089e70e13e7SMatteo Croce  *   - FUNC_PROTOs are compatible if they have compatible signature: same
8090e70e13e7SMatteo Croce  *     number of input args and compatible return and argument types.
8091e70e13e7SMatteo Croce  * These rules are not set in stone and probably will be adjusted as we get
8092e70e13e7SMatteo Croce  * more experience with using BPF CO-RE relocations.
8093e70e13e7SMatteo Croce  */
bpf_core_types_are_compat(const struct btf * local_btf,__u32 local_id,const struct btf * targ_btf,__u32 targ_id)809429db4beaSAlexei Starovoitov int bpf_core_types_are_compat(const struct btf *local_btf, __u32 local_id,
809529db4beaSAlexei Starovoitov 			      const struct btf *targ_btf, __u32 targ_id)
809629db4beaSAlexei Starovoitov {
8097fd75733dSDaniel Müller 	return __bpf_core_types_are_compat(local_btf, local_id, targ_btf, targ_id,
8098e70e13e7SMatteo Croce 					   MAX_TYPES_ARE_COMPAT_DEPTH);
809929db4beaSAlexei Starovoitov }
810029db4beaSAlexei Starovoitov 
8101ec6209c8SDaniel Müller #define MAX_TYPES_MATCH_DEPTH 2
8102ec6209c8SDaniel Müller 
bpf_core_types_match(const struct btf * local_btf,u32 local_id,const struct btf * targ_btf,u32 targ_id)8103ec6209c8SDaniel Müller int bpf_core_types_match(const struct btf *local_btf, u32 local_id,
8104ec6209c8SDaniel Müller 			 const struct btf *targ_btf, u32 targ_id)
8105ec6209c8SDaniel Müller {
8106ec6209c8SDaniel Müller 	return __bpf_core_types_match(local_btf, local_id, targ_btf, targ_id, false,
8107ec6209c8SDaniel Müller 				      MAX_TYPES_MATCH_DEPTH);
8108ec6209c8SDaniel Müller }
8109ec6209c8SDaniel Müller 
bpf_core_is_flavor_sep(const char * s)811029db4beaSAlexei Starovoitov static bool bpf_core_is_flavor_sep(const char *s)
811129db4beaSAlexei Starovoitov {
811229db4beaSAlexei Starovoitov 	/* check X___Y name pattern, where X and Y are not underscores */
811329db4beaSAlexei Starovoitov 	return s[0] != '_' &&				      /* X */
811429db4beaSAlexei Starovoitov 	       s[1] == '_' && s[2] == '_' && s[3] == '_' &&   /* ___ */
811529db4beaSAlexei Starovoitov 	       s[4] != '_';				      /* Y */
811629db4beaSAlexei Starovoitov }
811729db4beaSAlexei Starovoitov 
bpf_core_essential_name_len(const char * name)811829db4beaSAlexei Starovoitov size_t bpf_core_essential_name_len(const char *name)
811929db4beaSAlexei Starovoitov {
812029db4beaSAlexei Starovoitov 	size_t n = strlen(name);
812129db4beaSAlexei Starovoitov 	int i;
812229db4beaSAlexei Starovoitov 
812329db4beaSAlexei Starovoitov 	for (i = n - 5; i >= 0; i--) {
812429db4beaSAlexei Starovoitov 		if (bpf_core_is_flavor_sep(name + i))
812529db4beaSAlexei Starovoitov 			return i + 1;
812629db4beaSAlexei Starovoitov 	}
812729db4beaSAlexei Starovoitov 	return n;
812829db4beaSAlexei Starovoitov }
8129fbd94c7aSAlexei Starovoitov 
81301e89106dSAlexei Starovoitov struct bpf_cand_cache {
81311e89106dSAlexei Starovoitov 	const char *name;
81321e89106dSAlexei Starovoitov 	u32 name_len;
81331e89106dSAlexei Starovoitov 	u16 kind;
81341e89106dSAlexei Starovoitov 	u16 cnt;
81351e89106dSAlexei Starovoitov 	struct {
81361e89106dSAlexei Starovoitov 		const struct btf *btf;
81371e89106dSAlexei Starovoitov 		u32 id;
81381e89106dSAlexei Starovoitov 	} cands[];
81391e89106dSAlexei Starovoitov };
81401e89106dSAlexei Starovoitov 
bpf_free_cands(struct bpf_cand_cache * cands)81411e89106dSAlexei Starovoitov static void bpf_free_cands(struct bpf_cand_cache *cands)
81421e89106dSAlexei Starovoitov {
81431e89106dSAlexei Starovoitov 	if (!cands->cnt)
81441e89106dSAlexei Starovoitov 		/* empty candidate array was allocated on stack */
81451e89106dSAlexei Starovoitov 		return;
81461e89106dSAlexei Starovoitov 	kfree(cands);
81471e89106dSAlexei Starovoitov }
81481e89106dSAlexei Starovoitov 
bpf_free_cands_from_cache(struct bpf_cand_cache * cands)81491e89106dSAlexei Starovoitov static void bpf_free_cands_from_cache(struct bpf_cand_cache *cands)
81501e89106dSAlexei Starovoitov {
81511e89106dSAlexei Starovoitov 	kfree(cands->name);
81521e89106dSAlexei Starovoitov 	kfree(cands);
81531e89106dSAlexei Starovoitov }
81541e89106dSAlexei Starovoitov 
81551e89106dSAlexei Starovoitov #define VMLINUX_CAND_CACHE_SIZE 31
81561e89106dSAlexei Starovoitov static struct bpf_cand_cache *vmlinux_cand_cache[VMLINUX_CAND_CACHE_SIZE];
81571e89106dSAlexei Starovoitov 
81581e89106dSAlexei Starovoitov #define MODULE_CAND_CACHE_SIZE 31
81591e89106dSAlexei Starovoitov static struct bpf_cand_cache *module_cand_cache[MODULE_CAND_CACHE_SIZE];
81601e89106dSAlexei Starovoitov 
81611e89106dSAlexei Starovoitov static DEFINE_MUTEX(cand_cache_mutex);
81621e89106dSAlexei Starovoitov 
__print_cand_cache(struct bpf_verifier_log * log,struct bpf_cand_cache ** cache,int cache_size)81631e89106dSAlexei Starovoitov static void __print_cand_cache(struct bpf_verifier_log *log,
81641e89106dSAlexei Starovoitov 			       struct bpf_cand_cache **cache,
81651e89106dSAlexei Starovoitov 			       int cache_size)
81661e89106dSAlexei Starovoitov {
81671e89106dSAlexei Starovoitov 	struct bpf_cand_cache *cc;
81681e89106dSAlexei Starovoitov 	int i, j;
81691e89106dSAlexei Starovoitov 
81701e89106dSAlexei Starovoitov 	for (i = 0; i < cache_size; i++) {
81711e89106dSAlexei Starovoitov 		cc = cache[i];
81721e89106dSAlexei Starovoitov 		if (!cc)
81731e89106dSAlexei Starovoitov 			continue;
81741e89106dSAlexei Starovoitov 		bpf_log(log, "[%d]%s(", i, cc->name);
81751e89106dSAlexei Starovoitov 		for (j = 0; j < cc->cnt; j++) {
81761e89106dSAlexei Starovoitov 			bpf_log(log, "%d", cc->cands[j].id);
81771e89106dSAlexei Starovoitov 			if (j < cc->cnt - 1)
81781e89106dSAlexei Starovoitov 				bpf_log(log, " ");
81791e89106dSAlexei Starovoitov 		}
81801e89106dSAlexei Starovoitov 		bpf_log(log, "), ");
81811e89106dSAlexei Starovoitov 	}
81821e89106dSAlexei Starovoitov }
81831e89106dSAlexei Starovoitov 
print_cand_cache(struct bpf_verifier_log * log)81841e89106dSAlexei Starovoitov static void print_cand_cache(struct bpf_verifier_log *log)
81851e89106dSAlexei Starovoitov {
81861e89106dSAlexei Starovoitov 	mutex_lock(&cand_cache_mutex);
81871e89106dSAlexei Starovoitov 	bpf_log(log, "vmlinux_cand_cache:");
81881e89106dSAlexei Starovoitov 	__print_cand_cache(log, vmlinux_cand_cache, VMLINUX_CAND_CACHE_SIZE);
81891e89106dSAlexei Starovoitov 	bpf_log(log, "\nmodule_cand_cache:");
81901e89106dSAlexei Starovoitov 	__print_cand_cache(log, module_cand_cache, MODULE_CAND_CACHE_SIZE);
81911e89106dSAlexei Starovoitov 	bpf_log(log, "\n");
81921e89106dSAlexei Starovoitov 	mutex_unlock(&cand_cache_mutex);
81931e89106dSAlexei Starovoitov }
81941e89106dSAlexei Starovoitov 
hash_cands(struct bpf_cand_cache * cands)81951e89106dSAlexei Starovoitov static u32 hash_cands(struct bpf_cand_cache *cands)
81961e89106dSAlexei Starovoitov {
81971e89106dSAlexei Starovoitov 	return jhash(cands->name, cands->name_len, 0);
81981e89106dSAlexei Starovoitov }
81991e89106dSAlexei Starovoitov 
check_cand_cache(struct bpf_cand_cache * cands,struct bpf_cand_cache ** cache,int cache_size)82001e89106dSAlexei Starovoitov static struct bpf_cand_cache *check_cand_cache(struct bpf_cand_cache *cands,
82011e89106dSAlexei Starovoitov 					       struct bpf_cand_cache **cache,
82021e89106dSAlexei Starovoitov 					       int cache_size)
82031e89106dSAlexei Starovoitov {
82041e89106dSAlexei Starovoitov 	struct bpf_cand_cache *cc = cache[hash_cands(cands) % cache_size];
82051e89106dSAlexei Starovoitov 
82061e89106dSAlexei Starovoitov 	if (cc && cc->name_len == cands->name_len &&
82071e89106dSAlexei Starovoitov 	    !strncmp(cc->name, cands->name, cands->name_len))
82081e89106dSAlexei Starovoitov 		return cc;
82091e89106dSAlexei Starovoitov 	return NULL;
82101e89106dSAlexei Starovoitov }
82111e89106dSAlexei Starovoitov 
sizeof_cands(int cnt)82121e89106dSAlexei Starovoitov static size_t sizeof_cands(int cnt)
82131e89106dSAlexei Starovoitov {
82141e89106dSAlexei Starovoitov 	return offsetof(struct bpf_cand_cache, cands[cnt]);
82151e89106dSAlexei Starovoitov }
82161e89106dSAlexei Starovoitov 
populate_cand_cache(struct bpf_cand_cache * cands,struct bpf_cand_cache ** cache,int cache_size)82171e89106dSAlexei Starovoitov static struct bpf_cand_cache *populate_cand_cache(struct bpf_cand_cache *cands,
82181e89106dSAlexei Starovoitov 						  struct bpf_cand_cache **cache,
82191e89106dSAlexei Starovoitov 						  int cache_size)
82201e89106dSAlexei Starovoitov {
82211e89106dSAlexei Starovoitov 	struct bpf_cand_cache **cc = &cache[hash_cands(cands) % cache_size], *new_cands;
82221e89106dSAlexei Starovoitov 
82231e89106dSAlexei Starovoitov 	if (*cc) {
82241e89106dSAlexei Starovoitov 		bpf_free_cands_from_cache(*cc);
82251e89106dSAlexei Starovoitov 		*cc = NULL;
82261e89106dSAlexei Starovoitov 	}
82274674f210SJiapeng Chong 	new_cands = kmemdup(cands, sizeof_cands(cands->cnt), GFP_KERNEL);
82281e89106dSAlexei Starovoitov 	if (!new_cands) {
82291e89106dSAlexei Starovoitov 		bpf_free_cands(cands);
82301e89106dSAlexei Starovoitov 		return ERR_PTR(-ENOMEM);
82311e89106dSAlexei Starovoitov 	}
82321e89106dSAlexei Starovoitov 	/* strdup the name, since it will stay in cache.
82331e89106dSAlexei Starovoitov 	 * the cands->name points to strings in prog's BTF and the prog can be unloaded.
82341e89106dSAlexei Starovoitov 	 */
82351e89106dSAlexei Starovoitov 	new_cands->name = kmemdup_nul(cands->name, cands->name_len, GFP_KERNEL);
82361e89106dSAlexei Starovoitov 	bpf_free_cands(cands);
82371e89106dSAlexei Starovoitov 	if (!new_cands->name) {
82381e89106dSAlexei Starovoitov 		kfree(new_cands);
82391e89106dSAlexei Starovoitov 		return ERR_PTR(-ENOMEM);
82401e89106dSAlexei Starovoitov 	}
82411e89106dSAlexei Starovoitov 	*cc = new_cands;
82421e89106dSAlexei Starovoitov 	return new_cands;
82431e89106dSAlexei Starovoitov }
82441e89106dSAlexei Starovoitov 
824529f2e5bdSAlexei Starovoitov #ifdef CONFIG_DEBUG_INFO_BTF_MODULES
__purge_cand_cache(struct btf * btf,struct bpf_cand_cache ** cache,int cache_size)82461e89106dSAlexei Starovoitov static void __purge_cand_cache(struct btf *btf, struct bpf_cand_cache **cache,
82471e89106dSAlexei Starovoitov 			       int cache_size)
82481e89106dSAlexei Starovoitov {
82491e89106dSAlexei Starovoitov 	struct bpf_cand_cache *cc;
82501e89106dSAlexei Starovoitov 	int i, j;
82511e89106dSAlexei Starovoitov 
82521e89106dSAlexei Starovoitov 	for (i = 0; i < cache_size; i++) {
82531e89106dSAlexei Starovoitov 		cc = cache[i];
82541e89106dSAlexei Starovoitov 		if (!cc)
82551e89106dSAlexei Starovoitov 			continue;
82561e89106dSAlexei Starovoitov 		if (!btf) {
82571e89106dSAlexei Starovoitov 			/* when new module is loaded purge all of module_cand_cache,
82581e89106dSAlexei Starovoitov 			 * since new module might have candidates with the name
82591e89106dSAlexei Starovoitov 			 * that matches cached cands.
82601e89106dSAlexei Starovoitov 			 */
82611e89106dSAlexei Starovoitov 			bpf_free_cands_from_cache(cc);
82621e89106dSAlexei Starovoitov 			cache[i] = NULL;
82631e89106dSAlexei Starovoitov 			continue;
82641e89106dSAlexei Starovoitov 		}
82651e89106dSAlexei Starovoitov 		/* when module is unloaded purge cache entries
82661e89106dSAlexei Starovoitov 		 * that match module's btf
82671e89106dSAlexei Starovoitov 		 */
82681e89106dSAlexei Starovoitov 		for (j = 0; j < cc->cnt; j++)
82691e89106dSAlexei Starovoitov 			if (cc->cands[j].btf == btf) {
82701e89106dSAlexei Starovoitov 				bpf_free_cands_from_cache(cc);
82711e89106dSAlexei Starovoitov 				cache[i] = NULL;
82721e89106dSAlexei Starovoitov 				break;
82731e89106dSAlexei Starovoitov 			}
82741e89106dSAlexei Starovoitov 	}
82751e89106dSAlexei Starovoitov 
82761e89106dSAlexei Starovoitov }
82771e89106dSAlexei Starovoitov 
purge_cand_cache(struct btf * btf)82781e89106dSAlexei Starovoitov static void purge_cand_cache(struct btf *btf)
82791e89106dSAlexei Starovoitov {
82801e89106dSAlexei Starovoitov 	mutex_lock(&cand_cache_mutex);
82811e89106dSAlexei Starovoitov 	__purge_cand_cache(btf, module_cand_cache, MODULE_CAND_CACHE_SIZE);
82821e89106dSAlexei Starovoitov 	mutex_unlock(&cand_cache_mutex);
82831e89106dSAlexei Starovoitov }
828429f2e5bdSAlexei Starovoitov #endif
82851e89106dSAlexei Starovoitov 
82861e89106dSAlexei Starovoitov static struct bpf_cand_cache *
bpf_core_add_cands(struct bpf_cand_cache * cands,const struct btf * targ_btf,int targ_start_id)82871e89106dSAlexei Starovoitov bpf_core_add_cands(struct bpf_cand_cache *cands, const struct btf *targ_btf,
82881e89106dSAlexei Starovoitov 		   int targ_start_id)
82891e89106dSAlexei Starovoitov {
82901e89106dSAlexei Starovoitov 	struct bpf_cand_cache *new_cands;
82911e89106dSAlexei Starovoitov 	const struct btf_type *t;
82921e89106dSAlexei Starovoitov 	const char *targ_name;
82931e89106dSAlexei Starovoitov 	size_t targ_essent_len;
82941e89106dSAlexei Starovoitov 	int n, i;
82951e89106dSAlexei Starovoitov 
82961e89106dSAlexei Starovoitov 	n = btf_nr_types(targ_btf);
82971e89106dSAlexei Starovoitov 	for (i = targ_start_id; i < n; i++) {
82981e89106dSAlexei Starovoitov 		t = btf_type_by_id(targ_btf, i);
82991e89106dSAlexei Starovoitov 		if (btf_kind(t) != cands->kind)
83001e89106dSAlexei Starovoitov 			continue;
83011e89106dSAlexei Starovoitov 
83021e89106dSAlexei Starovoitov 		targ_name = btf_name_by_offset(targ_btf, t->name_off);
83031e89106dSAlexei Starovoitov 		if (!targ_name)
83041e89106dSAlexei Starovoitov 			continue;
83051e89106dSAlexei Starovoitov 
83061e89106dSAlexei Starovoitov 		/* the resched point is before strncmp to make sure that search
83071e89106dSAlexei Starovoitov 		 * for non-existing name will have a chance to schedule().
83081e89106dSAlexei Starovoitov 		 */
83091e89106dSAlexei Starovoitov 		cond_resched();
83101e89106dSAlexei Starovoitov 
83111e89106dSAlexei Starovoitov 		if (strncmp(cands->name, targ_name, cands->name_len) != 0)
83121e89106dSAlexei Starovoitov 			continue;
83131e89106dSAlexei Starovoitov 
83141e89106dSAlexei Starovoitov 		targ_essent_len = bpf_core_essential_name_len(targ_name);
83151e89106dSAlexei Starovoitov 		if (targ_essent_len != cands->name_len)
83161e89106dSAlexei Starovoitov 			continue;
83171e89106dSAlexei Starovoitov 
83181e89106dSAlexei Starovoitov 		/* most of the time there is only one candidate for a given kind+name pair */
83191e89106dSAlexei Starovoitov 		new_cands = kmalloc(sizeof_cands(cands->cnt + 1), GFP_KERNEL);
83201e89106dSAlexei Starovoitov 		if (!new_cands) {
83211e89106dSAlexei Starovoitov 			bpf_free_cands(cands);
83221e89106dSAlexei Starovoitov 			return ERR_PTR(-ENOMEM);
83231e89106dSAlexei Starovoitov 		}
83241e89106dSAlexei Starovoitov 
83251e89106dSAlexei Starovoitov 		memcpy(new_cands, cands, sizeof_cands(cands->cnt));
83261e89106dSAlexei Starovoitov 		bpf_free_cands(cands);
83271e89106dSAlexei Starovoitov 		cands = new_cands;
83281e89106dSAlexei Starovoitov 		cands->cands[cands->cnt].btf = targ_btf;
83291e89106dSAlexei Starovoitov 		cands->cands[cands->cnt].id = i;
83301e89106dSAlexei Starovoitov 		cands->cnt++;
83311e89106dSAlexei Starovoitov 	}
83321e89106dSAlexei Starovoitov 	return cands;
83331e89106dSAlexei Starovoitov }
83341e89106dSAlexei Starovoitov 
83351e89106dSAlexei Starovoitov static struct bpf_cand_cache *
bpf_core_find_cands(struct bpf_core_ctx * ctx,u32 local_type_id)83361e89106dSAlexei Starovoitov bpf_core_find_cands(struct bpf_core_ctx *ctx, u32 local_type_id)
83371e89106dSAlexei Starovoitov {
83381e89106dSAlexei Starovoitov 	struct bpf_cand_cache *cands, *cc, local_cand = {};
83391e89106dSAlexei Starovoitov 	const struct btf *local_btf = ctx->btf;
83401e89106dSAlexei Starovoitov 	const struct btf_type *local_type;
83411e89106dSAlexei Starovoitov 	const struct btf *main_btf;
83421e89106dSAlexei Starovoitov 	size_t local_essent_len;
83431e89106dSAlexei Starovoitov 	struct btf *mod_btf;
83441e89106dSAlexei Starovoitov 	const char *name;
83451e89106dSAlexei Starovoitov 	int id;
83461e89106dSAlexei Starovoitov 
83471e89106dSAlexei Starovoitov 	main_btf = bpf_get_btf_vmlinux();
83481e89106dSAlexei Starovoitov 	if (IS_ERR(main_btf))
8349f18a4997SAlexei Starovoitov 		return ERR_CAST(main_btf);
83507ada3787SKumar Kartikeya Dwivedi 	if (!main_btf)
83517ada3787SKumar Kartikeya Dwivedi 		return ERR_PTR(-EINVAL);
83521e89106dSAlexei Starovoitov 
83531e89106dSAlexei Starovoitov 	local_type = btf_type_by_id(local_btf, local_type_id);
83541e89106dSAlexei Starovoitov 	if (!local_type)
83551e89106dSAlexei Starovoitov 		return ERR_PTR(-EINVAL);
83561e89106dSAlexei Starovoitov 
83571e89106dSAlexei Starovoitov 	name = btf_name_by_offset(local_btf, local_type->name_off);
83581e89106dSAlexei Starovoitov 	if (str_is_empty(name))
83591e89106dSAlexei Starovoitov 		return ERR_PTR(-EINVAL);
83601e89106dSAlexei Starovoitov 	local_essent_len = bpf_core_essential_name_len(name);
83611e89106dSAlexei Starovoitov 
83621e89106dSAlexei Starovoitov 	cands = &local_cand;
83631e89106dSAlexei Starovoitov 	cands->name = name;
83641e89106dSAlexei Starovoitov 	cands->kind = btf_kind(local_type);
83651e89106dSAlexei Starovoitov 	cands->name_len = local_essent_len;
83661e89106dSAlexei Starovoitov 
83671e89106dSAlexei Starovoitov 	cc = check_cand_cache(cands, vmlinux_cand_cache, VMLINUX_CAND_CACHE_SIZE);
83681e89106dSAlexei Starovoitov 	/* cands is a pointer to stack here */
83691e89106dSAlexei Starovoitov 	if (cc) {
83701e89106dSAlexei Starovoitov 		if (cc->cnt)
83711e89106dSAlexei Starovoitov 			return cc;
83721e89106dSAlexei Starovoitov 		goto check_modules;
83731e89106dSAlexei Starovoitov 	}
83741e89106dSAlexei Starovoitov 
83751e89106dSAlexei Starovoitov 	/* Attempt to find target candidates in vmlinux BTF first */
83761e89106dSAlexei Starovoitov 	cands = bpf_core_add_cands(cands, main_btf, 1);
83771e89106dSAlexei Starovoitov 	if (IS_ERR(cands))
8378f18a4997SAlexei Starovoitov 		return ERR_CAST(cands);
83791e89106dSAlexei Starovoitov 
83801e89106dSAlexei Starovoitov 	/* cands is a pointer to kmalloced memory here if cands->cnt > 0 */
83811e89106dSAlexei Starovoitov 
83821e89106dSAlexei Starovoitov 	/* populate cache even when cands->cnt == 0 */
83831e89106dSAlexei Starovoitov 	cc = populate_cand_cache(cands, vmlinux_cand_cache, VMLINUX_CAND_CACHE_SIZE);
83841e89106dSAlexei Starovoitov 	if (IS_ERR(cc))
8385f18a4997SAlexei Starovoitov 		return ERR_CAST(cc);
83861e89106dSAlexei Starovoitov 
83871e89106dSAlexei Starovoitov 	/* if vmlinux BTF has any candidate, don't go for module BTFs */
83881e89106dSAlexei Starovoitov 	if (cc->cnt)
83891e89106dSAlexei Starovoitov 		return cc;
83901e89106dSAlexei Starovoitov 
83911e89106dSAlexei Starovoitov check_modules:
83921e89106dSAlexei Starovoitov 	/* cands is a pointer to stack here and cands->cnt == 0 */
83931e89106dSAlexei Starovoitov 	cc = check_cand_cache(cands, module_cand_cache, MODULE_CAND_CACHE_SIZE);
83941e89106dSAlexei Starovoitov 	if (cc)
83951e89106dSAlexei Starovoitov 		/* if cache has it return it even if cc->cnt == 0 */
83961e89106dSAlexei Starovoitov 		return cc;
83971e89106dSAlexei Starovoitov 
83981e89106dSAlexei Starovoitov 	/* If candidate is not found in vmlinux's BTF then search in module's BTFs */
83991e89106dSAlexei Starovoitov 	spin_lock_bh(&btf_idr_lock);
84001e89106dSAlexei Starovoitov 	idr_for_each_entry(&btf_idr, mod_btf, id) {
84011e89106dSAlexei Starovoitov 		if (!btf_is_module(mod_btf))
84021e89106dSAlexei Starovoitov 			continue;
84031e89106dSAlexei Starovoitov 		/* linear search could be slow hence unlock/lock
84041e89106dSAlexei Starovoitov 		 * the IDR to avoiding holding it for too long
84051e89106dSAlexei Starovoitov 		 */
84061e89106dSAlexei Starovoitov 		btf_get(mod_btf);
84071e89106dSAlexei Starovoitov 		spin_unlock_bh(&btf_idr_lock);
84081e89106dSAlexei Starovoitov 		cands = bpf_core_add_cands(cands, mod_btf, btf_nr_types(main_btf));
84091e89106dSAlexei Starovoitov 		btf_put(mod_btf);
8410acf1c3d6SAlexei Starovoitov 		if (IS_ERR(cands))
8411f18a4997SAlexei Starovoitov 			return ERR_CAST(cands);
84121e89106dSAlexei Starovoitov 		spin_lock_bh(&btf_idr_lock);
84131e89106dSAlexei Starovoitov 	}
84141e89106dSAlexei Starovoitov 	spin_unlock_bh(&btf_idr_lock);
84151e89106dSAlexei Starovoitov 	/* cands is a pointer to kmalloced memory here if cands->cnt > 0
84161e89106dSAlexei Starovoitov 	 * or pointer to stack if cands->cnd == 0.
84171e89106dSAlexei Starovoitov 	 * Copy it into the cache even when cands->cnt == 0 and
84181e89106dSAlexei Starovoitov 	 * return the result.
84191e89106dSAlexei Starovoitov 	 */
84201e89106dSAlexei Starovoitov 	return populate_cand_cache(cands, module_cand_cache, MODULE_CAND_CACHE_SIZE);
84211e89106dSAlexei Starovoitov }
84221e89106dSAlexei Starovoitov 
bpf_core_apply(struct bpf_core_ctx * ctx,const struct bpf_core_relo * relo,int relo_idx,void * insn)8423fbd94c7aSAlexei Starovoitov int bpf_core_apply(struct bpf_core_ctx *ctx, const struct bpf_core_relo *relo,
8424fbd94c7aSAlexei Starovoitov 		   int relo_idx, void *insn)
8425fbd94c7aSAlexei Starovoitov {
84261e89106dSAlexei Starovoitov 	bool need_cands = relo->kind != BPF_CORE_TYPE_ID_LOCAL;
84271e89106dSAlexei Starovoitov 	struct bpf_core_cand_list cands = {};
8428adb8fa19SMauricio Vásquez 	struct bpf_core_relo_res targ_res;
842978c1f8d0SAlexei Starovoitov 	struct bpf_core_spec *specs;
84302288b54bSEduard Zingerman 	const struct btf_type *type;
84311e89106dSAlexei Starovoitov 	int err;
84321e89106dSAlexei Starovoitov 
843378c1f8d0SAlexei Starovoitov 	/* ~4k of temp memory necessary to convert LLVM spec like "0:1:0:5"
843478c1f8d0SAlexei Starovoitov 	 * into arrays of btf_ids of struct fields and array indices.
843578c1f8d0SAlexei Starovoitov 	 */
843678c1f8d0SAlexei Starovoitov 	specs = kcalloc(3, sizeof(*specs), GFP_KERNEL);
843778c1f8d0SAlexei Starovoitov 	if (!specs)
843878c1f8d0SAlexei Starovoitov 		return -ENOMEM;
843978c1f8d0SAlexei Starovoitov 
84402288b54bSEduard Zingerman 	type = btf_type_by_id(ctx->btf, relo->type_id);
84412288b54bSEduard Zingerman 	if (!type) {
84422288b54bSEduard Zingerman 		bpf_log(ctx->log, "relo #%u: bad type id %u\n",
84432288b54bSEduard Zingerman 			relo_idx, relo->type_id);
844490ad4e2fSJiri Olsa 		kfree(specs);
84452288b54bSEduard Zingerman 		return -EINVAL;
84462288b54bSEduard Zingerman 	}
84472288b54bSEduard Zingerman 
84481e89106dSAlexei Starovoitov 	if (need_cands) {
84491e89106dSAlexei Starovoitov 		struct bpf_cand_cache *cc;
84501e89106dSAlexei Starovoitov 		int i;
84511e89106dSAlexei Starovoitov 
84521e89106dSAlexei Starovoitov 		mutex_lock(&cand_cache_mutex);
84531e89106dSAlexei Starovoitov 		cc = bpf_core_find_cands(ctx, relo->type_id);
84541e89106dSAlexei Starovoitov 		if (IS_ERR(cc)) {
84551e89106dSAlexei Starovoitov 			bpf_log(ctx->log, "target candidate search failed for %d\n",
84561e89106dSAlexei Starovoitov 				relo->type_id);
84571e89106dSAlexei Starovoitov 			err = PTR_ERR(cc);
84581e89106dSAlexei Starovoitov 			goto out;
84591e89106dSAlexei Starovoitov 		}
84601e89106dSAlexei Starovoitov 		if (cc->cnt) {
84611e89106dSAlexei Starovoitov 			cands.cands = kcalloc(cc->cnt, sizeof(*cands.cands), GFP_KERNEL);
84621e89106dSAlexei Starovoitov 			if (!cands.cands) {
84631e89106dSAlexei Starovoitov 				err = -ENOMEM;
84641e89106dSAlexei Starovoitov 				goto out;
84651e89106dSAlexei Starovoitov 			}
84661e89106dSAlexei Starovoitov 		}
84671e89106dSAlexei Starovoitov 		for (i = 0; i < cc->cnt; i++) {
84681e89106dSAlexei Starovoitov 			bpf_log(ctx->log,
84691e89106dSAlexei Starovoitov 				"CO-RE relocating %s %s: found target candidate [%d]\n",
84701e89106dSAlexei Starovoitov 				btf_kind_str[cc->kind], cc->name, cc->cands[i].id);
84711e89106dSAlexei Starovoitov 			cands.cands[i].btf = cc->cands[i].btf;
84721e89106dSAlexei Starovoitov 			cands.cands[i].id = cc->cands[i].id;
84731e89106dSAlexei Starovoitov 		}
84741e89106dSAlexei Starovoitov 		cands.len = cc->cnt;
84751e89106dSAlexei Starovoitov 		/* cand_cache_mutex needs to span the cache lookup and
84761e89106dSAlexei Starovoitov 		 * copy of btf pointer into bpf_core_cand_list,
8477adb8fa19SMauricio Vásquez 		 * since module can be unloaded while bpf_core_calc_relo_insn
84781e89106dSAlexei Starovoitov 		 * is working with module's btf.
84791e89106dSAlexei Starovoitov 		 */
84801e89106dSAlexei Starovoitov 	}
84811e89106dSAlexei Starovoitov 
8482adb8fa19SMauricio Vásquez 	err = bpf_core_calc_relo_insn((void *)ctx->log, relo, relo_idx, ctx->btf, &cands, specs,
8483adb8fa19SMauricio Vásquez 				      &targ_res);
8484adb8fa19SMauricio Vásquez 	if (err)
8485adb8fa19SMauricio Vásquez 		goto out;
8486adb8fa19SMauricio Vásquez 
8487adb8fa19SMauricio Vásquez 	err = bpf_core_patch_insn((void *)ctx->log, insn, relo->insn_off / 8, relo, relo_idx,
8488adb8fa19SMauricio Vásquez 				  &targ_res);
8489adb8fa19SMauricio Vásquez 
84901e89106dSAlexei Starovoitov out:
849178c1f8d0SAlexei Starovoitov 	kfree(specs);
84921e89106dSAlexei Starovoitov 	if (need_cands) {
84931e89106dSAlexei Starovoitov 		kfree(cands.cands);
84941e89106dSAlexei Starovoitov 		mutex_unlock(&cand_cache_mutex);
84951e89106dSAlexei Starovoitov 		if (ctx->log->level & BPF_LOG_LEVEL2)
84961e89106dSAlexei Starovoitov 			print_cand_cache(ctx->log);
84971e89106dSAlexei Starovoitov 	}
84981e89106dSAlexei Starovoitov 	return err;
8499fbd94c7aSAlexei Starovoitov }
850057539b1cSDavid Vernet 
btf_nested_type_is_trusted(struct bpf_verifier_log * log,const struct bpf_reg_state * reg,const char * field_name,u32 btf_id,const char * suffix)850157539b1cSDavid Vernet bool btf_nested_type_is_trusted(struct bpf_verifier_log *log,
850257539b1cSDavid Vernet 				const struct bpf_reg_state *reg,
850363260df1SAlexei Starovoitov 				const char *field_name, u32 btf_id, const char *suffix)
850457539b1cSDavid Vernet {
850557539b1cSDavid Vernet 	struct btf *btf = reg->btf;
850657539b1cSDavid Vernet 	const struct btf_type *walk_type, *safe_type;
850757539b1cSDavid Vernet 	const char *tname;
850857539b1cSDavid Vernet 	char safe_tname[64];
850957539b1cSDavid Vernet 	long ret, safe_id;
851063260df1SAlexei Starovoitov 	const struct btf_member *member;
851157539b1cSDavid Vernet 	u32 i;
851257539b1cSDavid Vernet 
851357539b1cSDavid Vernet 	walk_type = btf_type_by_id(btf, reg->btf_id);
851457539b1cSDavid Vernet 	if (!walk_type)
851557539b1cSDavid Vernet 		return false;
851657539b1cSDavid Vernet 
851757539b1cSDavid Vernet 	tname = btf_name_by_offset(btf, walk_type->name_off);
851857539b1cSDavid Vernet 
85196fcd486bSAlexei Starovoitov 	ret = snprintf(safe_tname, sizeof(safe_tname), "%s%s", tname, suffix);
8520a8f12572SChristophe JAILLET 	if (ret >= sizeof(safe_tname))
852157539b1cSDavid Vernet 		return false;
852257539b1cSDavid Vernet 
852357539b1cSDavid Vernet 	safe_id = btf_find_by_name_kind(btf, safe_tname, BTF_INFO_KIND(walk_type->info));
852457539b1cSDavid Vernet 	if (safe_id < 0)
852557539b1cSDavid Vernet 		return false;
852657539b1cSDavid Vernet 
852757539b1cSDavid Vernet 	safe_type = btf_type_by_id(btf, safe_id);
852857539b1cSDavid Vernet 	if (!safe_type)
852957539b1cSDavid Vernet 		return false;
853057539b1cSDavid Vernet 
853157539b1cSDavid Vernet 	for_each_member(i, safe_type, member) {
853257539b1cSDavid Vernet 		const char *m_name = __btf_name_by_offset(btf, member->name_off);
853363260df1SAlexei Starovoitov 		const struct btf_type *mtype = btf_type_by_id(btf, member->type);
853463260df1SAlexei Starovoitov 		u32 id;
853557539b1cSDavid Vernet 
853663260df1SAlexei Starovoitov 		if (!btf_type_is_ptr(mtype))
853763260df1SAlexei Starovoitov 			continue;
853863260df1SAlexei Starovoitov 
853963260df1SAlexei Starovoitov 		btf_type_skip_modifiers(btf, mtype->type, &id);
854057539b1cSDavid Vernet 		/* If we match on both type and name, the field is considered trusted. */
854163260df1SAlexei Starovoitov 		if (btf_id == id && !strcmp(field_name, m_name))
854257539b1cSDavid Vernet 			return true;
854357539b1cSDavid Vernet 	}
854457539b1cSDavid Vernet 
854557539b1cSDavid Vernet 	return false;
854657539b1cSDavid Vernet }
8547b613d335SDavid Vernet 
btf_type_ids_nocast_alias(struct bpf_verifier_log * log,const struct btf * reg_btf,u32 reg_id,const struct btf * arg_btf,u32 arg_id)8548b613d335SDavid Vernet bool btf_type_ids_nocast_alias(struct bpf_verifier_log *log,
8549b613d335SDavid Vernet 			       const struct btf *reg_btf, u32 reg_id,
8550b613d335SDavid Vernet 			       const struct btf *arg_btf, u32 arg_id)
8551b613d335SDavid Vernet {
8552b613d335SDavid Vernet 	const char *reg_name, *arg_name, *search_needle;
8553b613d335SDavid Vernet 	const struct btf_type *reg_type, *arg_type;
8554b613d335SDavid Vernet 	int reg_len, arg_len, cmp_len;
8555b613d335SDavid Vernet 	size_t pattern_len = sizeof(NOCAST_ALIAS_SUFFIX) - sizeof(char);
8556b613d335SDavid Vernet 
8557b613d335SDavid Vernet 	reg_type = btf_type_by_id(reg_btf, reg_id);
8558b613d335SDavid Vernet 	if (!reg_type)
8559b613d335SDavid Vernet 		return false;
8560b613d335SDavid Vernet 
8561b613d335SDavid Vernet 	arg_type = btf_type_by_id(arg_btf, arg_id);
8562b613d335SDavid Vernet 	if (!arg_type)
8563b613d335SDavid Vernet 		return false;
8564b613d335SDavid Vernet 
8565b613d335SDavid Vernet 	reg_name = btf_name_by_offset(reg_btf, reg_type->name_off);
8566b613d335SDavid Vernet 	arg_name = btf_name_by_offset(arg_btf, arg_type->name_off);
8567b613d335SDavid Vernet 
8568b613d335SDavid Vernet 	reg_len = strlen(reg_name);
8569b613d335SDavid Vernet 	arg_len = strlen(arg_name);
8570b613d335SDavid Vernet 
8571b613d335SDavid Vernet 	/* Exactly one of the two type names may be suffixed with ___init, so
8572b613d335SDavid Vernet 	 * if the strings are the same size, they can't possibly be no-cast
8573b613d335SDavid Vernet 	 * aliases of one another. If you have two of the same type names, e.g.
8574b613d335SDavid Vernet 	 * they're both nf_conn___init, it would be improper to return true
8575b613d335SDavid Vernet 	 * because they are _not_ no-cast aliases, they are the same type.
8576b613d335SDavid Vernet 	 */
8577b613d335SDavid Vernet 	if (reg_len == arg_len)
8578b613d335SDavid Vernet 		return false;
8579b613d335SDavid Vernet 
8580b613d335SDavid Vernet 	/* Either of the two names must be the other name, suffixed with ___init. */
8581b613d335SDavid Vernet 	if ((reg_len != arg_len + pattern_len) &&
8582b613d335SDavid Vernet 	    (arg_len != reg_len + pattern_len))
8583b613d335SDavid Vernet 		return false;
8584b613d335SDavid Vernet 
8585b613d335SDavid Vernet 	if (reg_len < arg_len) {
8586b613d335SDavid Vernet 		search_needle = strstr(arg_name, NOCAST_ALIAS_SUFFIX);
8587b613d335SDavid Vernet 		cmp_len = reg_len;
8588b613d335SDavid Vernet 	} else {
8589b613d335SDavid Vernet 		search_needle = strstr(reg_name, NOCAST_ALIAS_SUFFIX);
8590b613d335SDavid Vernet 		cmp_len = arg_len;
8591b613d335SDavid Vernet 	}
8592b613d335SDavid Vernet 
8593b613d335SDavid Vernet 	if (!search_needle)
8594b613d335SDavid Vernet 		return false;
8595b613d335SDavid Vernet 
8596b613d335SDavid Vernet 	/* ___init suffix must come at the end of the name */
8597b613d335SDavid Vernet 	if (*(search_needle + pattern_len) != '\0')
8598b613d335SDavid Vernet 		return false;
8599b613d335SDavid Vernet 
8600b613d335SDavid Vernet 	return !strncmp(reg_name, arg_name, cmp_len);
8601b613d335SDavid Vernet }
8602