blob: e16dafeb24504a307c40d1e37c9da6e6ede1a076 [file] [log] [blame]
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001/* SPDX-License-Identifier: GPL-2.0 */
2/* Copyright (c) 2018 Facebook */
3
4#include <uapi/linux/btf.h>
Alexei Starovoitov91cc1a92019-11-14 10:57:15 -08005#include <uapi/linux/bpf.h>
6#include <uapi/linux/bpf_perf_event.h>
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07007#include <uapi/linux/types.h>
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07008#include <linux/seq_file.h>
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07009#include <linux/compiler.h>
Martin KaFai Lau2667a262018-11-19 15:29:08 -080010#include <linux/ctype.h>
Martin KaFai Lau69b693f2018-04-18 15:55:57 -070011#include <linux/errno.h>
12#include <linux/slab.h>
Martin KaFai Lauf56a6532018-04-18 15:56:01 -070013#include <linux/anon_inodes.h>
14#include <linux/file.h>
Martin KaFai Lau69b693f2018-04-18 15:55:57 -070015#include <linux/uaccess.h>
16#include <linux/kernel.h>
Martin KaFai Lau78958fc2018-05-04 14:49:51 -070017#include <linux/idr.h>
Martin KaFai Lauf80442a2018-05-22 14:57:18 -070018#include <linux/sort.h>
Martin KaFai Lau69b693f2018-04-18 15:55:57 -070019#include <linux/bpf_verifier.h>
20#include <linux/btf.h>
Jiri Olsa49f4e672020-07-11 23:53:26 +020021#include <linux/btf_ids.h>
Alexei Starovoitov91cc1a92019-11-14 10:57:15 -080022#include <linux/skmsg.h>
23#include <linux/perf_event.h>
Jiri Olsaeae2e832020-08-25 21:21:19 +020024#include <linux/bsearch.h>
Andrii Nakryiko36e68442020-11-09 17:19:31 -080025#include <linux/kobject.h>
26#include <linux/sysfs.h>
Alexei Starovoitov91cc1a92019-11-14 10:57:15 -080027#include <net/sock.h>
Alexei Starovoitov1e891062021-12-01 10:10:31 -080028#include "../tools/lib/bpf/relo_core.h"
Martin KaFai Lau69b693f2018-04-18 15:55:57 -070029
30/* BTF (BPF Type Format) is the meta data format which describes
31 * the data types of BPF program/map. Hence, it basically focus
32 * on the C programming language which the modern BPF is primary
33 * using.
34 *
35 * ELF Section:
36 * ~~~~~~~~~~~
37 * The BTF data is stored under the ".BTF" ELF section
38 *
39 * struct btf_type:
40 * ~~~~~~~~~~~~~~~
41 * Each 'struct btf_type' object describes a C data type.
42 * Depending on the type it is describing, a 'struct btf_type'
43 * object may be followed by more data. F.e.
44 * To describe an array, 'struct btf_type' is followed by
45 * 'struct btf_array'.
46 *
47 * 'struct btf_type' and any extra data following it are
48 * 4 bytes aligned.
49 *
50 * Type section:
51 * ~~~~~~~~~~~~~
52 * The BTF type section contains a list of 'struct btf_type' objects.
53 * Each one describes a C type. Recall from the above section
54 * that a 'struct btf_type' object could be immediately followed by extra
Zhen Lei8fb33b62021-05-25 10:56:59 +080055 * data in order to describe some particular C types.
Martin KaFai Lau69b693f2018-04-18 15:55:57 -070056 *
57 * type_id:
58 * ~~~~~~~
59 * Each btf_type object is identified by a type_id. The type_id
60 * is implicitly implied by the location of the btf_type object in
61 * the BTF type section. The first one has type_id 1. The second
62 * one has type_id 2...etc. Hence, an earlier btf_type has
63 * a smaller type_id.
64 *
65 * A btf_type object may refer to another btf_type object by using
66 * type_id (i.e. the "type" in the "struct btf_type").
67 *
68 * NOTE that we cannot assume any reference-order.
69 * A btf_type object can refer to an earlier btf_type object
70 * but it can also refer to a later btf_type object.
71 *
72 * For example, to describe "const void *". A btf_type
73 * object describing "const" may refer to another btf_type
74 * object describing "void *". This type-reference is done
75 * by specifying type_id:
76 *
77 * [1] CONST (anon) type_id=2
78 * [2] PTR (anon) type_id=0
79 *
80 * The above is the btf_verifier debug log:
81 * - Each line started with "[?]" is a btf_type object
82 * - [?] is the type_id of the btf_type object.
83 * - CONST/PTR is the BTF_KIND_XXX
84 * - "(anon)" is the name of the type. It just
85 * happens that CONST and PTR has no name.
86 * - type_id=XXX is the 'u32 type' in btf_type
87 *
88 * NOTE: "void" has type_id 0
89 *
90 * String section:
91 * ~~~~~~~~~~~~~~
92 * The BTF string section contains the names used by the type section.
93 * Each string is referred by an "offset" from the beginning of the
94 * string section.
95 *
96 * Each string is '\0' terminated.
97 *
98 * The first character in the string section must be '\0'
99 * which is used to mean 'anonymous'. Some btf_type may not
100 * have a name.
101 */
102
103/* BTF verification:
104 *
105 * To verify BTF data, two passes are needed.
106 *
107 * Pass #1
108 * ~~~~~~~
109 * The first pass is to collect all btf_type objects to
110 * an array: "btf->types".
111 *
112 * Depending on the C type that a btf_type is describing,
113 * a btf_type may be followed by extra data. We don't know
114 * how many btf_type is there, and more importantly we don't
115 * know where each btf_type is located in the type section.
116 *
117 * Without knowing the location of each type_id, most verifications
118 * cannot be done. e.g. an earlier btf_type may refer to a later
119 * btf_type (recall the "const void *" above), so we cannot
120 * check this type-reference in the first pass.
121 *
122 * In the first pass, it still does some verifications (e.g.
123 * checking the name is a valid offset to the string section).
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700124 *
125 * Pass #2
126 * ~~~~~~~
127 * The main focus is to resolve a btf_type that is referring
128 * to another type.
129 *
130 * We have to ensure the referring type:
131 * 1) does exist in the BTF (i.e. in btf->types[])
132 * 2) does not cause a loop:
133 * struct A {
134 * struct B b;
135 * };
136 *
137 * struct B {
138 * struct A a;
139 * };
140 *
141 * btf_type_needs_resolve() decides if a btf_type needs
142 * to be resolved.
143 *
144 * The needs_resolve type implements the "resolve()" ops which
145 * essentially does a DFS and detects backedge.
146 *
147 * During resolve (or DFS), different C types have different
148 * "RESOLVED" conditions.
149 *
150 * When resolving a BTF_KIND_STRUCT, we need to resolve all its
151 * members because a member is always referring to another
152 * type. A struct's member can be treated as "RESOLVED" if
153 * it is referring to a BTF_KIND_PTR. Otherwise, the
154 * following valid C struct would be rejected:
155 *
156 * struct A {
157 * int m;
158 * struct A *a;
159 * };
160 *
161 * When resolving a BTF_KIND_PTR, it needs to keep resolving if
162 * it is referring to another BTF_KIND_PTR. Otherwise, we cannot
163 * detect a pointer loop, e.g.:
164 * BTF_KIND_CONST -> BTF_KIND_PTR -> BTF_KIND_CONST -> BTF_KIND_PTR +
165 * ^ |
166 * +-----------------------------------------+
167 *
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700168 */
169
Yonghong Songb1e88182019-01-15 17:07:47 -0800170#define BITS_PER_U128 (sizeof(u64) * BITS_PER_BYTE * 2)
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700171#define BITS_PER_BYTE_MASK (BITS_PER_BYTE - 1)
172#define BITS_PER_BYTE_MASKED(bits) ((bits) & BITS_PER_BYTE_MASK)
173#define BITS_ROUNDDOWN_BYTES(bits) ((bits) >> 3)
174#define BITS_ROUNDUP_BYTES(bits) \
175 (BITS_ROUNDDOWN_BYTES(bits) + !!BITS_PER_BYTE_MASKED(bits))
176
Ilya Leoshkevichb1828f02021-02-26 21:22:52 +0100177#define BTF_INFO_MASK 0x9f00ffff
Martin KaFai Lauaea2f7b82018-05-22 14:57:20 -0700178#define BTF_INT_MASK 0x0fffffff
179#define BTF_TYPE_ID_VALID(type_id) ((type_id) <= BTF_MAX_TYPE)
180#define BTF_STR_OFFSET_VALID(name_off) ((name_off) <= BTF_MAX_NAME_OFFSET)
181
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700182/* 16MB for 64k structs and each has 16 members and
183 * a few MB spaces for the string section.
184 * The hard limit is S32_MAX.
185 */
186#define BTF_MAX_SIZE (16 * 1024 * 1024)
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700187
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700188#define for_each_member_from(i, from, struct_type, member) \
189 for (i = from, member = btf_type_member(struct_type) + from; \
190 i < btf_type_vlen(struct_type); \
191 i++, member++)
192
Daniel Borkmann1dc92852019-04-09 23:20:09 +0200193#define for_each_vsi_from(i, from, struct_type, member) \
194 for (i = from, member = btf_type_var_secinfo(struct_type) + from; \
195 i < btf_type_vlen(struct_type); \
196 i++, member++)
197
Quentin Monnet1b9ed842019-08-20 10:31:50 +0100198DEFINE_IDR(btf_idr);
199DEFINE_SPINLOCK(btf_idr_lock);
Martin KaFai Lau78958fc2018-05-04 14:49:51 -0700200
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700201struct btf {
Martin KaFai Lauf80442a2018-05-22 14:57:18 -0700202 void *data;
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700203 struct btf_type **types;
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700204 u32 *resolved_ids;
205 u32 *resolved_sizes;
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700206 const char *strings;
207 void *nohdr_data;
Martin KaFai Lauf80442a2018-05-22 14:57:18 -0700208 struct btf_header hdr;
Andrii Nakryiko951bb642020-11-09 17:19:28 -0800209 u32 nr_types; /* includes VOID for base BTF */
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700210 u32 types_size;
211 u32 data_size;
Martin KaFai Lauf56a6532018-04-18 15:56:01 -0700212 refcount_t refcnt;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -0700213 u32 id;
214 struct rcu_head rcu;
Andrii Nakryiko951bb642020-11-09 17:19:28 -0800215
216 /* split BTF support */
217 struct btf *base_btf;
218 u32 start_id; /* first type ID in this BTF (0 for base BTF) */
219 u32 start_str_off; /* first string offset (0 for base BTF) */
Andrii Nakryiko53297222020-11-09 17:19:29 -0800220 char name[MODULE_NAME_LEN];
221 bool kernel_btf;
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700222};
223
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700224enum verifier_phase {
225 CHECK_META,
226 CHECK_TYPE,
227};
228
229struct resolve_vertex {
230 const struct btf_type *t;
231 u32 type_id;
232 u16 next_member;
233};
234
235enum visit_state {
236 NOT_VISITED,
237 VISITED,
238 RESOLVED,
239};
240
241enum resolve_mode {
242 RESOLVE_TBD, /* To Be Determined */
243 RESOLVE_PTR, /* Resolving for Pointer */
244 RESOLVE_STRUCT_OR_ARRAY, /* Resolving for struct/union
245 * or array
246 */
247};
248
249#define MAX_RESOLVE_DEPTH 32
250
Martin KaFai Lauf80442a2018-05-22 14:57:18 -0700251struct btf_sec_info {
252 u32 off;
253 u32 len;
254};
255
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700256struct btf_verifier_env {
257 struct btf *btf;
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700258 u8 *visit_states;
259 struct resolve_vertex stack[MAX_RESOLVE_DEPTH];
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700260 struct bpf_verifier_log log;
261 u32 log_type_id;
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700262 u32 top_stack;
263 enum verifier_phase phase;
264 enum resolve_mode resolve_mode;
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700265};
266
267static const char * const btf_kind_str[NR_BTF_KINDS] = {
268 [BTF_KIND_UNKN] = "UNKNOWN",
269 [BTF_KIND_INT] = "INT",
270 [BTF_KIND_PTR] = "PTR",
271 [BTF_KIND_ARRAY] = "ARRAY",
272 [BTF_KIND_STRUCT] = "STRUCT",
273 [BTF_KIND_UNION] = "UNION",
274 [BTF_KIND_ENUM] = "ENUM",
275 [BTF_KIND_FWD] = "FWD",
276 [BTF_KIND_TYPEDEF] = "TYPEDEF",
277 [BTF_KIND_VOLATILE] = "VOLATILE",
278 [BTF_KIND_CONST] = "CONST",
279 [BTF_KIND_RESTRICT] = "RESTRICT",
Martin KaFai Lau2667a262018-11-19 15:29:08 -0800280 [BTF_KIND_FUNC] = "FUNC",
281 [BTF_KIND_FUNC_PROTO] = "FUNC_PROTO",
Daniel Borkmann1dc92852019-04-09 23:20:09 +0200282 [BTF_KIND_VAR] = "VAR",
283 [BTF_KIND_DATASEC] = "DATASEC",
Ilya Leoshkevichb1828f02021-02-26 21:22:52 +0100284 [BTF_KIND_FLOAT] = "FLOAT",
Yonghong Song223f9032021-10-12 09:48:38 -0700285 [BTF_KIND_DECL_TAG] = "DECL_TAG",
Yonghong Song8c42d2f2021-11-11 17:26:09 -0800286 [BTF_KIND_TYPE_TAG] = "TYPE_TAG",
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700287};
288
Martin KaFai Laue6ac2452021-03-24 18:51:42 -0700289const char *btf_type_str(const struct btf_type *t)
Alexei Starovoitovbe8704f2020-01-20 16:53:46 -0800290{
291 return btf_kind_str[BTF_INFO_KIND(t->info)];
292}
293
Alan Maguire31d0bc82020-09-28 12:31:04 +0100294/* Chunk size we use in safe copy of data to be shown. */
295#define BTF_SHOW_OBJ_SAFE_SIZE 32
296
297/*
298 * This is the maximum size of a base type value (equivalent to a
299 * 128-bit int); if we are at the end of our safe buffer and have
300 * less than 16 bytes space we can't be assured of being able
301 * to copy the next type safely, so in such cases we will initiate
302 * a new copy.
303 */
304#define BTF_SHOW_OBJ_BASE_TYPE_SIZE 16
305
306/* Type name size */
307#define BTF_SHOW_NAME_SIZE 80
308
309/*
310 * Common data to all BTF show operations. Private show functions can add
311 * their own data to a structure containing a struct btf_show and consult it
312 * in the show callback. See btf_type_show() below.
313 *
314 * One challenge with showing nested data is we want to skip 0-valued
315 * data, but in order to figure out whether a nested object is all zeros
316 * we need to walk through it. As a result, we need to make two passes
317 * when handling structs, unions and arrays; the first path simply looks
318 * for nonzero data, while the second actually does the display. The first
319 * pass is signalled by show->state.depth_check being set, and if we
320 * encounter a non-zero value we set show->state.depth_to_show to
321 * the depth at which we encountered it. When we have completed the
322 * first pass, we will know if anything needs to be displayed if
323 * depth_to_show > depth. See btf_[struct,array]_show() for the
324 * implementation of this.
325 *
326 * Another problem is we want to ensure the data for display is safe to
327 * access. To support this, the anonymous "struct {} obj" tracks the data
328 * object and our safe copy of it. We copy portions of the data needed
329 * to the object "copy" buffer, but because its size is limited to
330 * BTF_SHOW_OBJ_COPY_LEN bytes, multiple copies may be required as we
331 * traverse larger objects for display.
332 *
333 * The various data type show functions all start with a call to
334 * btf_show_start_type() which returns a pointer to the safe copy
335 * of the data needed (or if BTF_SHOW_UNSAFE is specified, to the
336 * raw data itself). btf_show_obj_safe() is responsible for
337 * using copy_from_kernel_nofault() to update the safe data if necessary
338 * as we traverse the object's data. skbuff-like semantics are
339 * used:
340 *
341 * - obj.head points to the start of the toplevel object for display
342 * - obj.size is the size of the toplevel object
343 * - obj.data points to the current point in the original data at
344 * which our safe data starts. obj.data will advance as we copy
345 * portions of the data.
346 *
347 * In most cases a single copy will suffice, but larger data structures
348 * such as "struct task_struct" will require many copies. The logic in
349 * btf_show_obj_safe() handles the logic that determines if a new
350 * copy_from_kernel_nofault() is needed.
351 */
352struct btf_show {
353 u64 flags;
354 void *target; /* target of show operation (seq file, buffer) */
355 void (*showfn)(struct btf_show *show, const char *fmt, va_list args);
356 const struct btf *btf;
357 /* below are used during iteration */
358 struct {
359 u8 depth;
360 u8 depth_to_show;
361 u8 depth_check;
362 u8 array_member:1,
363 array_terminated:1;
364 u16 array_encoding;
365 u32 type_id;
366 int status; /* non-zero for error */
367 const struct btf_type *type;
368 const struct btf_member *member;
369 char name[BTF_SHOW_NAME_SIZE]; /* space for member name/type */
370 } state;
371 struct {
372 u32 size;
373 void *head;
374 void *data;
375 u8 safe[BTF_SHOW_OBJ_SAFE_SIZE];
376 } obj;
377};
378
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700379struct btf_kind_operations {
380 s32 (*check_meta)(struct btf_verifier_env *env,
381 const struct btf_type *t,
382 u32 meta_left);
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700383 int (*resolve)(struct btf_verifier_env *env,
384 const struct resolve_vertex *v);
Martin KaFai Lau179cde82018-04-18 15:55:59 -0700385 int (*check_member)(struct btf_verifier_env *env,
386 const struct btf_type *struct_type,
387 const struct btf_member *member,
388 const struct btf_type *member_type);
Yonghong Song9d5f9f72018-12-15 22:13:51 -0800389 int (*check_kflag_member)(struct btf_verifier_env *env,
390 const struct btf_type *struct_type,
391 const struct btf_member *member,
392 const struct btf_type *member_type);
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700393 void (*log_details)(struct btf_verifier_env *env,
394 const struct btf_type *t);
Alan Maguire31d0bc82020-09-28 12:31:04 +0100395 void (*show)(const struct btf *btf, const struct btf_type *t,
Martin KaFai Laub00b8da2018-04-18 15:56:00 -0700396 u32 type_id, void *data, u8 bits_offsets,
Alan Maguire31d0bc82020-09-28 12:31:04 +0100397 struct btf_show *show);
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700398};
399
400static const struct btf_kind_operations * const kind_ops[NR_BTF_KINDS];
401static struct btf_type btf_void;
402
Martin KaFai Lau2667a262018-11-19 15:29:08 -0800403static int btf_resolve(struct btf_verifier_env *env,
404 const struct btf_type *t, u32 type_id);
405
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700406static bool btf_type_is_modifier(const struct btf_type *t)
407{
408 /* Some of them is not strictly a C modifier
409 * but they are grouped into the same bucket
410 * for BTF concern:
411 * A type (t) that refers to another
412 * type through t->type AND its size cannot
413 * be determined without following the t->type.
414 *
415 * ptr does not fall into this bucket
416 * because its size is always sizeof(void *).
417 */
418 switch (BTF_INFO_KIND(t->info)) {
419 case BTF_KIND_TYPEDEF:
420 case BTF_KIND_VOLATILE:
421 case BTF_KIND_CONST:
422 case BTF_KIND_RESTRICT:
Yonghong Song8c42d2f2021-11-11 17:26:09 -0800423 case BTF_KIND_TYPE_TAG:
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700424 return true;
425 }
426
427 return false;
428}
429
Daniel Borkmann2824ecb2019-04-09 23:20:10 +0200430bool btf_type_is_void(const struct btf_type *t)
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700431{
Martin KaFai Laub47a0bd2018-11-19 15:29:06 -0800432 return t == &btf_void;
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700433}
434
Martin KaFai Laub47a0bd2018-11-19 15:29:06 -0800435static bool btf_type_is_fwd(const struct btf_type *t)
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700436{
Martin KaFai Laub47a0bd2018-11-19 15:29:06 -0800437 return BTF_INFO_KIND(t->info) == BTF_KIND_FWD;
438}
439
440static bool btf_type_nosize(const struct btf_type *t)
441{
Martin KaFai Lau2667a262018-11-19 15:29:08 -0800442 return btf_type_is_void(t) || btf_type_is_fwd(t) ||
443 btf_type_is_func(t) || btf_type_is_func_proto(t);
Martin KaFai Laub47a0bd2018-11-19 15:29:06 -0800444}
445
446static bool btf_type_nosize_or_null(const struct btf_type *t)
447{
448 return !t || btf_type_nosize(t);
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700449}
450
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800451static bool __btf_type_is_struct(const struct btf_type *t)
452{
453 return BTF_INFO_KIND(t->info) == BTF_KIND_STRUCT;
454}
455
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700456static bool btf_type_is_array(const struct btf_type *t)
457{
458 return BTF_INFO_KIND(t->info) == BTF_KIND_ARRAY;
459}
460
Daniel Borkmann1dc92852019-04-09 23:20:09 +0200461static bool btf_type_is_datasec(const struct btf_type *t)
462{
463 return BTF_INFO_KIND(t->info) == BTF_KIND_DATASEC;
464}
465
Yonghong Song223f9032021-10-12 09:48:38 -0700466static bool btf_type_is_decl_tag(const struct btf_type *t)
Yonghong Songb5ea8342021-09-14 15:30:15 -0700467{
Yonghong Song223f9032021-10-12 09:48:38 -0700468 return BTF_INFO_KIND(t->info) == BTF_KIND_DECL_TAG;
Yonghong Songb5ea8342021-09-14 15:30:15 -0700469}
470
Yonghong Song223f9032021-10-12 09:48:38 -0700471static bool btf_type_is_decl_tag_target(const struct btf_type *t)
Yonghong Songb5ea8342021-09-14 15:30:15 -0700472{
473 return btf_type_is_func(t) || btf_type_is_struct(t) ||
Yonghong Songbd16dee2021-10-21 12:56:28 -0700474 btf_type_is_var(t) || btf_type_is_typedef(t);
Yonghong Songb5ea8342021-09-14 15:30:15 -0700475}
476
Andrii Nakryiko541c3ba2021-01-11 23:55:18 -0800477u32 btf_nr_types(const struct btf *btf)
Andrii Nakryiko951bb642020-11-09 17:19:28 -0800478{
479 u32 total = 0;
480
481 while (btf) {
482 total += btf->nr_types;
483 btf = btf->base_btf;
484 }
485
486 return total;
487}
488
Martin KaFai Lau27ae79972020-01-08 16:35:03 -0800489s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind)
490{
491 const struct btf_type *t;
492 const char *tname;
Andrii Nakryiko951bb642020-11-09 17:19:28 -0800493 u32 i, total;
Martin KaFai Lau27ae79972020-01-08 16:35:03 -0800494
Andrii Nakryiko541c3ba2021-01-11 23:55:18 -0800495 total = btf_nr_types(btf);
Andrii Nakryiko951bb642020-11-09 17:19:28 -0800496 for (i = 1; i < total; i++) {
497 t = btf_type_by_id(btf, i);
Martin KaFai Lau27ae79972020-01-08 16:35:03 -0800498 if (BTF_INFO_KIND(t->info) != kind)
499 continue;
500
501 tname = btf_name_by_offset(btf, t->name_off);
502 if (!strcmp(tname, name))
503 return i;
504 }
505
506 return -ENOENT;
507}
508
509const struct btf_type *btf_type_skip_modifiers(const struct btf *btf,
510 u32 id, u32 *res_id)
511{
512 const struct btf_type *t = btf_type_by_id(btf, id);
513
514 while (btf_type_is_modifier(t)) {
515 id = t->type;
516 t = btf_type_by_id(btf, t->type);
517 }
518
519 if (res_id)
520 *res_id = id;
521
522 return t;
523}
524
525const struct btf_type *btf_type_resolve_ptr(const struct btf *btf,
526 u32 id, u32 *res_id)
527{
528 const struct btf_type *t;
529
530 t = btf_type_skip_modifiers(btf, id, NULL);
531 if (!btf_type_is_ptr(t))
532 return NULL;
533
534 return btf_type_skip_modifiers(btf, t->type, res_id);
535}
536
537const struct btf_type *btf_type_resolve_func_ptr(const struct btf *btf,
538 u32 id, u32 *res_id)
539{
540 const struct btf_type *ptype;
541
542 ptype = btf_type_resolve_ptr(btf, id, res_id);
543 if (ptype && btf_type_is_func_proto(ptype))
544 return ptype;
545
546 return NULL;
547}
548
Daniel Borkmann1dc92852019-04-09 23:20:09 +0200549/* Types that act only as a source, not sink or intermediate
550 * type when resolving.
551 */
552static bool btf_type_is_resolve_source_only(const struct btf_type *t)
553{
554 return btf_type_is_var(t) ||
Yonghong Song223f9032021-10-12 09:48:38 -0700555 btf_type_is_decl_tag(t) ||
Daniel Borkmann1dc92852019-04-09 23:20:09 +0200556 btf_type_is_datasec(t);
557}
558
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700559/* What types need to be resolved?
560 *
561 * btf_type_is_modifier() is an obvious one.
562 *
563 * btf_type_is_struct() because its member refers to
564 * another type (through member->type).
Daniel Borkmann1dc92852019-04-09 23:20:09 +0200565 *
566 * btf_type_is_var() because the variable refers to
567 * another type. btf_type_is_datasec() holds multiple
568 * btf_type_is_var() types that need resolving.
569 *
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700570 * btf_type_is_array() because its element (array->type)
571 * refers to another type. Array can be thought of a
572 * special case of struct while array just has the same
573 * member-type repeated by array->nelems of times.
574 */
575static bool btf_type_needs_resolve(const struct btf_type *t)
576{
577 return btf_type_is_modifier(t) ||
Daniel Borkmann1dc92852019-04-09 23:20:09 +0200578 btf_type_is_ptr(t) ||
579 btf_type_is_struct(t) ||
580 btf_type_is_array(t) ||
581 btf_type_is_var(t) ||
Yonghong Song223f9032021-10-12 09:48:38 -0700582 btf_type_is_decl_tag(t) ||
Daniel Borkmann1dc92852019-04-09 23:20:09 +0200583 btf_type_is_datasec(t);
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700584}
585
586/* t->size can be used */
587static bool btf_type_has_size(const struct btf_type *t)
588{
589 switch (BTF_INFO_KIND(t->info)) {
590 case BTF_KIND_INT:
591 case BTF_KIND_STRUCT:
592 case BTF_KIND_UNION:
593 case BTF_KIND_ENUM:
Daniel Borkmann1dc92852019-04-09 23:20:09 +0200594 case BTF_KIND_DATASEC:
Ilya Leoshkevichb1828f02021-02-26 21:22:52 +0100595 case BTF_KIND_FLOAT:
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700596 return true;
597 }
598
599 return false;
600}
601
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700602static const char *btf_int_encoding_str(u8 encoding)
603{
604 if (encoding == 0)
605 return "(none)";
606 else if (encoding == BTF_INT_SIGNED)
607 return "SIGNED";
608 else if (encoding == BTF_INT_CHAR)
609 return "CHAR";
610 else if (encoding == BTF_INT_BOOL)
611 return "BOOL";
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700612 else
613 return "UNKN";
614}
615
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700616static u32 btf_type_int(const struct btf_type *t)
617{
618 return *(u32 *)(t + 1);
619}
620
621static const struct btf_array *btf_type_array(const struct btf_type *t)
622{
623 return (const struct btf_array *)(t + 1);
624}
625
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700626static const struct btf_enum *btf_type_enum(const struct btf_type *t)
627{
628 return (const struct btf_enum *)(t + 1);
629}
630
Daniel Borkmann1dc92852019-04-09 23:20:09 +0200631static const struct btf_var *btf_type_var(const struct btf_type *t)
632{
633 return (const struct btf_var *)(t + 1);
634}
635
Yonghong Song223f9032021-10-12 09:48:38 -0700636static const struct btf_decl_tag *btf_type_decl_tag(const struct btf_type *t)
Yonghong Songb5ea8342021-09-14 15:30:15 -0700637{
Yonghong Song223f9032021-10-12 09:48:38 -0700638 return (const struct btf_decl_tag *)(t + 1);
Yonghong Songb5ea8342021-09-14 15:30:15 -0700639}
640
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700641static const struct btf_kind_operations *btf_type_ops(const struct btf_type *t)
642{
643 return kind_ops[BTF_INFO_KIND(t->info)];
644}
645
Mathieu Malaterre583c5312019-01-16 20:29:40 +0100646static bool btf_name_offset_valid(const struct btf *btf, u32 offset)
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700647{
Andrii Nakryiko951bb642020-11-09 17:19:28 -0800648 if (!BTF_STR_OFFSET_VALID(offset))
649 return false;
650
651 while (offset < btf->start_str_off)
652 btf = btf->base_btf;
653
654 offset -= btf->start_str_off;
655 return offset < btf->hdr.str_len;
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700656}
657
Daniel Borkmann1dc92852019-04-09 23:20:09 +0200658static bool __btf_name_char_ok(char c, bool first, bool dot_ok)
659{
660 if ((first ? !isalpha(c) :
661 !isalnum(c)) &&
662 c != '_' &&
663 ((c == '.' && !dot_ok) ||
664 c != '.'))
665 return false;
666 return true;
667}
668
Andrii Nakryiko951bb642020-11-09 17:19:28 -0800669static const char *btf_str_by_offset(const struct btf *btf, u32 offset)
670{
671 while (offset < btf->start_str_off)
672 btf = btf->base_btf;
673
674 offset -= btf->start_str_off;
675 if (offset < btf->hdr.str_len)
676 return &btf->strings[offset];
677
678 return NULL;
679}
680
Daniel Borkmann1dc92852019-04-09 23:20:09 +0200681static bool __btf_name_valid(const struct btf *btf, u32 offset, bool dot_ok)
Martin KaFai Lau2667a262018-11-19 15:29:08 -0800682{
683 /* offset must be valid */
Andrii Nakryiko951bb642020-11-09 17:19:28 -0800684 const char *src = btf_str_by_offset(btf, offset);
Martin KaFai Lau2667a262018-11-19 15:29:08 -0800685 const char *src_limit;
686
Daniel Borkmann1dc92852019-04-09 23:20:09 +0200687 if (!__btf_name_char_ok(*src, true, dot_ok))
Martin KaFai Lau2667a262018-11-19 15:29:08 -0800688 return false;
689
690 /* set a limit on identifier length */
691 src_limit = src + KSYM_NAME_LEN;
692 src++;
693 while (*src && src < src_limit) {
Daniel Borkmann1dc92852019-04-09 23:20:09 +0200694 if (!__btf_name_char_ok(*src, false, dot_ok))
Martin KaFai Lau2667a262018-11-19 15:29:08 -0800695 return false;
696 src++;
697 }
698
699 return !*src;
700}
701
Daniel Borkmann1dc92852019-04-09 23:20:09 +0200702/* Only C-style identifier is permitted. This can be relaxed if
703 * necessary.
704 */
705static bool btf_name_valid_identifier(const struct btf *btf, u32 offset)
706{
707 return __btf_name_valid(btf, offset, false);
708}
709
710static bool btf_name_valid_section(const struct btf *btf, u32 offset)
711{
712 return __btf_name_valid(btf, offset, true);
713}
714
Martin KaFai Lau23127b32018-12-13 10:41:46 -0800715static const char *__btf_name_by_offset(const struct btf *btf, u32 offset)
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700716{
Andrii Nakryiko951bb642020-11-09 17:19:28 -0800717 const char *name;
718
Martin KaFai Lauaea2f7b82018-05-22 14:57:20 -0700719 if (!offset)
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700720 return "(anon)";
Andrii Nakryiko951bb642020-11-09 17:19:28 -0800721
722 name = btf_str_by_offset(btf, offset);
723 return name ?: "(invalid-name-offset)";
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700724}
725
Martin KaFai Lau23127b32018-12-13 10:41:46 -0800726const char *btf_name_by_offset(const struct btf *btf, u32 offset)
727{
Andrii Nakryiko951bb642020-11-09 17:19:28 -0800728 return btf_str_by_offset(btf, offset);
Martin KaFai Lau23127b32018-12-13 10:41:46 -0800729}
730
Yonghong Song838e9692018-11-19 15:29:11 -0800731const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id)
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700732{
Andrii Nakryiko951bb642020-11-09 17:19:28 -0800733 while (type_id < btf->start_id)
734 btf = btf->base_btf;
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700735
Andrii Nakryiko951bb642020-11-09 17:19:28 -0800736 type_id -= btf->start_id;
737 if (type_id >= btf->nr_types)
738 return NULL;
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700739 return btf->types[type_id];
740}
741
Martin KaFai Lau4ef5f572018-05-22 14:57:19 -0700742/*
743 * Regular int is not a bit field and it must be either
Yonghong Songb1e88182019-01-15 17:07:47 -0800744 * u8/u16/u32/u64 or __int128.
Martin KaFai Lau4ef5f572018-05-22 14:57:19 -0700745 */
746static bool btf_type_int_is_regular(const struct btf_type *t)
747{
Martin KaFai Lau36fc3c82018-07-19 22:14:31 -0700748 u8 nr_bits, nr_bytes;
Martin KaFai Lau4ef5f572018-05-22 14:57:19 -0700749 u32 int_data;
750
751 int_data = btf_type_int(t);
752 nr_bits = BTF_INT_BITS(int_data);
753 nr_bytes = BITS_ROUNDUP_BYTES(nr_bits);
754 if (BITS_PER_BYTE_MASKED(nr_bits) ||
755 BTF_INT_OFFSET(int_data) ||
756 (nr_bytes != sizeof(u8) && nr_bytes != sizeof(u16) &&
Yonghong Songb1e88182019-01-15 17:07:47 -0800757 nr_bytes != sizeof(u32) && nr_bytes != sizeof(u64) &&
758 nr_bytes != (2 * sizeof(u64)))) {
Martin KaFai Lau4ef5f572018-05-22 14:57:19 -0700759 return false;
760 }
761
762 return true;
763}
764
Roman Gushchin9a1126b2018-12-10 15:43:01 -0800765/*
Yonghong Songffa0c1c2018-12-15 22:13:52 -0800766 * Check that given struct member is a regular int with expected
767 * offset and size.
Roman Gushchin9a1126b2018-12-10 15:43:01 -0800768 */
Yonghong Songffa0c1c2018-12-15 22:13:52 -0800769bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s,
770 const struct btf_member *m,
771 u32 expected_offset, u32 expected_size)
Roman Gushchin9a1126b2018-12-10 15:43:01 -0800772{
Yonghong Songffa0c1c2018-12-15 22:13:52 -0800773 const struct btf_type *t;
774 u32 id, int_data;
775 u8 nr_bits;
Roman Gushchin9a1126b2018-12-10 15:43:01 -0800776
Yonghong Songffa0c1c2018-12-15 22:13:52 -0800777 id = m->type;
778 t = btf_type_id_size(btf, &id, NULL);
779 if (!t || !btf_type_is_int(t))
Roman Gushchin9a1126b2018-12-10 15:43:01 -0800780 return false;
781
782 int_data = btf_type_int(t);
783 nr_bits = BTF_INT_BITS(int_data);
Yonghong Songffa0c1c2018-12-15 22:13:52 -0800784 if (btf_type_kflag(s)) {
785 u32 bitfield_size = BTF_MEMBER_BITFIELD_SIZE(m->offset);
786 u32 bit_offset = BTF_MEMBER_BIT_OFFSET(m->offset);
787
788 /* if kflag set, int should be a regular int and
789 * bit offset should be at byte boundary.
790 */
791 return !bitfield_size &&
792 BITS_ROUNDUP_BYTES(bit_offset) == expected_offset &&
793 BITS_ROUNDUP_BYTES(nr_bits) == expected_size;
794 }
795
796 if (BTF_INT_OFFSET(int_data) ||
797 BITS_PER_BYTE_MASKED(m->offset) ||
798 BITS_ROUNDUP_BYTES(m->offset) != expected_offset ||
799 BITS_PER_BYTE_MASKED(nr_bits) ||
800 BITS_ROUNDUP_BYTES(nr_bits) != expected_size)
Roman Gushchin9a1126b2018-12-10 15:43:01 -0800801 return false;
802
803 return true;
804}
805
Alan Maguire31d0bc82020-09-28 12:31:04 +0100806/* Similar to btf_type_skip_modifiers() but does not skip typedefs. */
807static const struct btf_type *btf_type_skip_qualifiers(const struct btf *btf,
808 u32 id)
809{
810 const struct btf_type *t = btf_type_by_id(btf, id);
811
812 while (btf_type_is_modifier(t) &&
813 BTF_INFO_KIND(t->info) != BTF_KIND_TYPEDEF) {
Alan Maguire31d0bc82020-09-28 12:31:04 +0100814 t = btf_type_by_id(btf, t->type);
815 }
816
817 return t;
818}
819
820#define BTF_SHOW_MAX_ITER 10
821
822#define BTF_KIND_BIT(kind) (1ULL << kind)
823
824/*
825 * Populate show->state.name with type name information.
826 * Format of type name is
827 *
828 * [.member_name = ] (type_name)
829 */
830static const char *btf_show_name(struct btf_show *show)
831{
832 /* BTF_MAX_ITER array suffixes "[]" */
833 const char *array_suffixes = "[][][][][][][][][][]";
834 const char *array_suffix = &array_suffixes[strlen(array_suffixes)];
835 /* BTF_MAX_ITER pointer suffixes "*" */
836 const char *ptr_suffixes = "**********";
837 const char *ptr_suffix = &ptr_suffixes[strlen(ptr_suffixes)];
838 const char *name = NULL, *prefix = "", *parens = "";
839 const struct btf_member *m = show->state.member;
Colin Ian King73b6eae2021-12-07 22:47:18 +0000840 const struct btf_type *t;
Alan Maguire31d0bc82020-09-28 12:31:04 +0100841 const struct btf_array *array;
842 u32 id = show->state.type_id;
843 const char *member = NULL;
844 bool show_member = false;
845 u64 kinds = 0;
846 int i;
847
848 show->state.name[0] = '\0';
849
850 /*
851 * Don't show type name if we're showing an array member;
852 * in that case we show the array type so don't need to repeat
853 * ourselves for each member.
854 */
855 if (show->state.array_member)
856 return "";
857
858 /* Retrieve member name, if any. */
859 if (m) {
860 member = btf_name_by_offset(show->btf, m->name_off);
861 show_member = strlen(member) > 0;
862 id = m->type;
863 }
864
865 /*
866 * Start with type_id, as we have resolved the struct btf_type *
867 * via btf_modifier_show() past the parent typedef to the child
868 * struct, int etc it is defined as. In such cases, the type_id
869 * still represents the starting type while the struct btf_type *
870 * in our show->state points at the resolved type of the typedef.
871 */
872 t = btf_type_by_id(show->btf, id);
873 if (!t)
874 return "";
875
876 /*
877 * The goal here is to build up the right number of pointer and
878 * array suffixes while ensuring the type name for a typedef
879 * is represented. Along the way we accumulate a list of
880 * BTF kinds we have encountered, since these will inform later
881 * display; for example, pointer types will not require an
882 * opening "{" for struct, we will just display the pointer value.
883 *
884 * We also want to accumulate the right number of pointer or array
885 * indices in the format string while iterating until we get to
886 * the typedef/pointee/array member target type.
887 *
888 * We start by pointing at the end of pointer and array suffix
889 * strings; as we accumulate pointers and arrays we move the pointer
890 * or array string backwards so it will show the expected number of
891 * '*' or '[]' for the type. BTF_SHOW_MAX_ITER of nesting of pointers
892 * and/or arrays and typedefs are supported as a precaution.
893 *
894 * We also want to get typedef name while proceeding to resolve
895 * type it points to so that we can add parentheses if it is a
896 * "typedef struct" etc.
897 */
898 for (i = 0; i < BTF_SHOW_MAX_ITER; i++) {
899
900 switch (BTF_INFO_KIND(t->info)) {
901 case BTF_KIND_TYPEDEF:
902 if (!name)
903 name = btf_name_by_offset(show->btf,
904 t->name_off);
905 kinds |= BTF_KIND_BIT(BTF_KIND_TYPEDEF);
906 id = t->type;
907 break;
908 case BTF_KIND_ARRAY:
909 kinds |= BTF_KIND_BIT(BTF_KIND_ARRAY);
910 parens = "[";
911 if (!t)
912 return "";
913 array = btf_type_array(t);
914 if (array_suffix > array_suffixes)
915 array_suffix -= 2;
916 id = array->type;
917 break;
918 case BTF_KIND_PTR:
919 kinds |= BTF_KIND_BIT(BTF_KIND_PTR);
920 if (ptr_suffix > ptr_suffixes)
921 ptr_suffix -= 1;
922 id = t->type;
923 break;
924 default:
925 id = 0;
926 break;
927 }
928 if (!id)
929 break;
930 t = btf_type_skip_qualifiers(show->btf, id);
931 }
932 /* We may not be able to represent this type; bail to be safe */
933 if (i == BTF_SHOW_MAX_ITER)
934 return "";
935
936 if (!name)
937 name = btf_name_by_offset(show->btf, t->name_off);
938
939 switch (BTF_INFO_KIND(t->info)) {
940 case BTF_KIND_STRUCT:
941 case BTF_KIND_UNION:
942 prefix = BTF_INFO_KIND(t->info) == BTF_KIND_STRUCT ?
943 "struct" : "union";
944 /* if it's an array of struct/union, parens is already set */
945 if (!(kinds & (BTF_KIND_BIT(BTF_KIND_ARRAY))))
946 parens = "{";
947 break;
948 case BTF_KIND_ENUM:
949 prefix = "enum";
950 break;
951 default:
952 break;
953 }
954
955 /* pointer does not require parens */
956 if (kinds & BTF_KIND_BIT(BTF_KIND_PTR))
957 parens = "";
958 /* typedef does not require struct/union/enum prefix */
959 if (kinds & BTF_KIND_BIT(BTF_KIND_TYPEDEF))
960 prefix = "";
961
962 if (!name)
963 name = "";
964
965 /* Even if we don't want type name info, we want parentheses etc */
966 if (show->flags & BTF_SHOW_NONAME)
967 snprintf(show->state.name, sizeof(show->state.name), "%s",
968 parens);
969 else
970 snprintf(show->state.name, sizeof(show->state.name),
971 "%s%s%s(%s%s%s%s%s%s)%s",
972 /* first 3 strings comprise ".member = " */
973 show_member ? "." : "",
974 show_member ? member : "",
975 show_member ? " = " : "",
976 /* ...next is our prefix (struct, enum, etc) */
977 prefix,
978 strlen(prefix) > 0 && strlen(name) > 0 ? " " : "",
979 /* ...this is the type name itself */
980 name,
981 /* ...suffixed by the appropriate '*', '[]' suffixes */
982 strlen(ptr_suffix) > 0 ? " " : "", ptr_suffix,
983 array_suffix, parens);
984
985 return show->state.name;
986}
987
988static const char *__btf_show_indent(struct btf_show *show)
989{
990 const char *indents = " ";
991 const char *indent = &indents[strlen(indents)];
992
993 if ((indent - show->state.depth) >= indents)
994 return indent - show->state.depth;
995 return indents;
996}
997
998static const char *btf_show_indent(struct btf_show *show)
999{
1000 return show->flags & BTF_SHOW_COMPACT ? "" : __btf_show_indent(show);
1001}
1002
1003static const char *btf_show_newline(struct btf_show *show)
1004{
1005 return show->flags & BTF_SHOW_COMPACT ? "" : "\n";
1006}
1007
1008static const char *btf_show_delim(struct btf_show *show)
1009{
1010 if (show->state.depth == 0)
1011 return "";
1012
1013 if ((show->flags & BTF_SHOW_COMPACT) && show->state.type &&
1014 BTF_INFO_KIND(show->state.type->info) == BTF_KIND_UNION)
1015 return "|";
1016
1017 return ",";
1018}
1019
1020__printf(2, 3) static void btf_show(struct btf_show *show, const char *fmt, ...)
1021{
1022 va_list args;
1023
1024 if (!show->state.depth_check) {
1025 va_start(args, fmt);
1026 show->showfn(show, fmt, args);
1027 va_end(args);
1028 }
1029}
1030
1031/* Macros are used here as btf_show_type_value[s]() prepends and appends
1032 * format specifiers to the format specifier passed in; these do the work of
1033 * adding indentation, delimiters etc while the caller simply has to specify
1034 * the type value(s) in the format specifier + value(s).
1035 */
1036#define btf_show_type_value(show, fmt, value) \
1037 do { \
1038 if ((value) != 0 || (show->flags & BTF_SHOW_ZERO) || \
1039 show->state.depth == 0) { \
1040 btf_show(show, "%s%s" fmt "%s%s", \
1041 btf_show_indent(show), \
1042 btf_show_name(show), \
1043 value, btf_show_delim(show), \
1044 btf_show_newline(show)); \
1045 if (show->state.depth > show->state.depth_to_show) \
1046 show->state.depth_to_show = show->state.depth; \
1047 } \
1048 } while (0)
1049
1050#define btf_show_type_values(show, fmt, ...) \
1051 do { \
1052 btf_show(show, "%s%s" fmt "%s%s", btf_show_indent(show), \
1053 btf_show_name(show), \
1054 __VA_ARGS__, btf_show_delim(show), \
1055 btf_show_newline(show)); \
1056 if (show->state.depth > show->state.depth_to_show) \
1057 show->state.depth_to_show = show->state.depth; \
1058 } while (0)
1059
1060/* How much is left to copy to safe buffer after @data? */
1061static int btf_show_obj_size_left(struct btf_show *show, void *data)
1062{
1063 return show->obj.head + show->obj.size - data;
1064}
1065
1066/* Is object pointed to by @data of @size already copied to our safe buffer? */
1067static bool btf_show_obj_is_safe(struct btf_show *show, void *data, int size)
1068{
1069 return data >= show->obj.data &&
1070 (data + size) < (show->obj.data + BTF_SHOW_OBJ_SAFE_SIZE);
1071}
1072
1073/*
1074 * If object pointed to by @data of @size falls within our safe buffer, return
1075 * the equivalent pointer to the same safe data. Assumes
1076 * copy_from_kernel_nofault() has already happened and our safe buffer is
1077 * populated.
1078 */
1079static void *__btf_show_obj_safe(struct btf_show *show, void *data, int size)
1080{
1081 if (btf_show_obj_is_safe(show, data, size))
1082 return show->obj.safe + (data - show->obj.data);
1083 return NULL;
1084}
1085
1086/*
1087 * Return a safe-to-access version of data pointed to by @data.
1088 * We do this by copying the relevant amount of information
1089 * to the struct btf_show obj.safe buffer using copy_from_kernel_nofault().
1090 *
1091 * If BTF_SHOW_UNSAFE is specified, just return data as-is; no
1092 * safe copy is needed.
1093 *
1094 * Otherwise we need to determine if we have the required amount
1095 * of data (determined by the @data pointer and the size of the
1096 * largest base type we can encounter (represented by
1097 * BTF_SHOW_OBJ_BASE_TYPE_SIZE). Having that much data ensures
1098 * that we will be able to print some of the current object,
1099 * and if more is needed a copy will be triggered.
1100 * Some objects such as structs will not fit into the buffer;
1101 * in such cases additional copies when we iterate over their
1102 * members may be needed.
1103 *
1104 * btf_show_obj_safe() is used to return a safe buffer for
1105 * btf_show_start_type(); this ensures that as we recurse into
1106 * nested types we always have safe data for the given type.
1107 * This approach is somewhat wasteful; it's possible for example
1108 * that when iterating over a large union we'll end up copying the
1109 * same data repeatedly, but the goal is safety not performance.
1110 * We use stack data as opposed to per-CPU buffers because the
1111 * iteration over a type can take some time, and preemption handling
1112 * would greatly complicate use of the safe buffer.
1113 */
1114static void *btf_show_obj_safe(struct btf_show *show,
1115 const struct btf_type *t,
1116 void *data)
1117{
1118 const struct btf_type *rt;
1119 int size_left, size;
1120 void *safe = NULL;
1121
1122 if (show->flags & BTF_SHOW_UNSAFE)
1123 return data;
1124
1125 rt = btf_resolve_size(show->btf, t, &size);
1126 if (IS_ERR(rt)) {
1127 show->state.status = PTR_ERR(rt);
1128 return NULL;
1129 }
1130
1131 /*
1132 * Is this toplevel object? If so, set total object size and
1133 * initialize pointers. Otherwise check if we still fall within
1134 * our safe object data.
1135 */
1136 if (show->state.depth == 0) {
1137 show->obj.size = size;
1138 show->obj.head = data;
1139 } else {
1140 /*
1141 * If the size of the current object is > our remaining
1142 * safe buffer we _may_ need to do a new copy. However
1143 * consider the case of a nested struct; it's size pushes
1144 * us over the safe buffer limit, but showing any individual
1145 * struct members does not. In such cases, we don't need
1146 * to initiate a fresh copy yet; however we definitely need
1147 * at least BTF_SHOW_OBJ_BASE_TYPE_SIZE bytes left
1148 * in our buffer, regardless of the current object size.
1149 * The logic here is that as we resolve types we will
1150 * hit a base type at some point, and we need to be sure
1151 * the next chunk of data is safely available to display
1152 * that type info safely. We cannot rely on the size of
1153 * the current object here because it may be much larger
1154 * than our current buffer (e.g. task_struct is 8k).
1155 * All we want to do here is ensure that we can print the
1156 * next basic type, which we can if either
1157 * - the current type size is within the safe buffer; or
1158 * - at least BTF_SHOW_OBJ_BASE_TYPE_SIZE bytes are left in
1159 * the safe buffer.
1160 */
1161 safe = __btf_show_obj_safe(show, data,
1162 min(size,
1163 BTF_SHOW_OBJ_BASE_TYPE_SIZE));
1164 }
1165
1166 /*
1167 * We need a new copy to our safe object, either because we haven't
Zhen Lei8fb33b62021-05-25 10:56:59 +08001168 * yet copied and are initializing safe data, or because the data
Alan Maguire31d0bc82020-09-28 12:31:04 +01001169 * we want falls outside the boundaries of the safe object.
1170 */
1171 if (!safe) {
1172 size_left = btf_show_obj_size_left(show, data);
1173 if (size_left > BTF_SHOW_OBJ_SAFE_SIZE)
1174 size_left = BTF_SHOW_OBJ_SAFE_SIZE;
1175 show->state.status = copy_from_kernel_nofault(show->obj.safe,
1176 data, size_left);
1177 if (!show->state.status) {
1178 show->obj.data = data;
1179 safe = show->obj.safe;
1180 }
1181 }
1182
1183 return safe;
1184}
1185
1186/*
1187 * Set the type we are starting to show and return a safe data pointer
1188 * to be used for showing the associated data.
1189 */
1190static void *btf_show_start_type(struct btf_show *show,
1191 const struct btf_type *t,
1192 u32 type_id, void *data)
1193{
1194 show->state.type = t;
1195 show->state.type_id = type_id;
1196 show->state.name[0] = '\0';
1197
1198 return btf_show_obj_safe(show, t, data);
1199}
1200
1201static void btf_show_end_type(struct btf_show *show)
1202{
1203 show->state.type = NULL;
1204 show->state.type_id = 0;
1205 show->state.name[0] = '\0';
1206}
1207
1208static void *btf_show_start_aggr_type(struct btf_show *show,
1209 const struct btf_type *t,
1210 u32 type_id, void *data)
1211{
1212 void *safe_data = btf_show_start_type(show, t, type_id, data);
1213
1214 if (!safe_data)
1215 return safe_data;
1216
1217 btf_show(show, "%s%s%s", btf_show_indent(show),
1218 btf_show_name(show),
1219 btf_show_newline(show));
1220 show->state.depth++;
1221 return safe_data;
1222}
1223
1224static void btf_show_end_aggr_type(struct btf_show *show,
1225 const char *suffix)
1226{
1227 show->state.depth--;
1228 btf_show(show, "%s%s%s%s", btf_show_indent(show), suffix,
1229 btf_show_delim(show), btf_show_newline(show));
1230 btf_show_end_type(show);
1231}
1232
1233static void btf_show_start_member(struct btf_show *show,
1234 const struct btf_member *m)
1235{
1236 show->state.member = m;
1237}
1238
1239static void btf_show_start_array_member(struct btf_show *show)
1240{
1241 show->state.array_member = 1;
1242 btf_show_start_member(show, NULL);
1243}
1244
1245static void btf_show_end_member(struct btf_show *show)
1246{
1247 show->state.member = NULL;
1248}
1249
1250static void btf_show_end_array_member(struct btf_show *show)
1251{
1252 show->state.array_member = 0;
1253 btf_show_end_member(show);
1254}
1255
1256static void *btf_show_start_array_type(struct btf_show *show,
1257 const struct btf_type *t,
1258 u32 type_id,
1259 u16 array_encoding,
1260 void *data)
1261{
1262 show->state.array_encoding = array_encoding;
1263 show->state.array_terminated = 0;
1264 return btf_show_start_aggr_type(show, t, type_id, data);
1265}
1266
1267static void btf_show_end_array_type(struct btf_show *show)
1268{
1269 show->state.array_encoding = 0;
1270 show->state.array_terminated = 0;
1271 btf_show_end_aggr_type(show, "]");
1272}
1273
1274static void *btf_show_start_struct_type(struct btf_show *show,
1275 const struct btf_type *t,
1276 u32 type_id,
1277 void *data)
1278{
1279 return btf_show_start_aggr_type(show, t, type_id, data);
1280}
1281
1282static void btf_show_end_struct_type(struct btf_show *show)
1283{
1284 btf_show_end_aggr_type(show, "}");
1285}
1286
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001287__printf(2, 3) static void __btf_verifier_log(struct bpf_verifier_log *log,
1288 const char *fmt, ...)
1289{
1290 va_list args;
1291
1292 va_start(args, fmt);
1293 bpf_verifier_vlog(log, fmt, args);
1294 va_end(args);
1295}
1296
1297__printf(2, 3) static void btf_verifier_log(struct btf_verifier_env *env,
1298 const char *fmt, ...)
1299{
1300 struct bpf_verifier_log *log = &env->log;
1301 va_list args;
1302
1303 if (!bpf_verifier_log_needed(log))
1304 return;
1305
1306 va_start(args, fmt);
1307 bpf_verifier_vlog(log, fmt, args);
1308 va_end(args);
1309}
1310
1311__printf(4, 5) static void __btf_verifier_log_type(struct btf_verifier_env *env,
1312 const struct btf_type *t,
1313 bool log_details,
1314 const char *fmt, ...)
1315{
1316 struct bpf_verifier_log *log = &env->log;
1317 u8 kind = BTF_INFO_KIND(t->info);
1318 struct btf *btf = env->btf;
1319 va_list args;
1320
1321 if (!bpf_verifier_log_needed(log))
1322 return;
1323
Alexei Starovoitov8580ac92019-10-15 20:24:57 -07001324 /* btf verifier prints all types it is processing via
1325 * btf_verifier_log_type(..., fmt = NULL).
1326 * Skip those prints for in-kernel BTF verification.
1327 */
1328 if (log->level == BPF_LOG_KERNEL && !fmt)
1329 return;
1330
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001331 __btf_verifier_log(log, "[%u] %s %s%s",
1332 env->log_type_id,
1333 btf_kind_str[kind],
Martin KaFai Lau23127b32018-12-13 10:41:46 -08001334 __btf_name_by_offset(btf, t->name_off),
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001335 log_details ? " " : "");
1336
1337 if (log_details)
1338 btf_type_ops(t)->log_details(env, t);
1339
1340 if (fmt && *fmt) {
1341 __btf_verifier_log(log, " ");
1342 va_start(args, fmt);
1343 bpf_verifier_vlog(log, fmt, args);
1344 va_end(args);
1345 }
1346
1347 __btf_verifier_log(log, "\n");
1348}
1349
1350#define btf_verifier_log_type(env, t, ...) \
1351 __btf_verifier_log_type((env), (t), true, __VA_ARGS__)
1352#define btf_verifier_log_basic(env, t, ...) \
1353 __btf_verifier_log_type((env), (t), false, __VA_ARGS__)
1354
1355__printf(4, 5)
1356static void btf_verifier_log_member(struct btf_verifier_env *env,
1357 const struct btf_type *struct_type,
1358 const struct btf_member *member,
1359 const char *fmt, ...)
1360{
1361 struct bpf_verifier_log *log = &env->log;
1362 struct btf *btf = env->btf;
1363 va_list args;
1364
1365 if (!bpf_verifier_log_needed(log))
1366 return;
1367
Alexei Starovoitov8580ac92019-10-15 20:24:57 -07001368 if (log->level == BPF_LOG_KERNEL && !fmt)
1369 return;
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001370 /* The CHECK_META phase already did a btf dump.
1371 *
1372 * If member is logged again, it must hit an error in
1373 * parsing this member. It is useful to print out which
1374 * struct this member belongs to.
1375 */
1376 if (env->phase != CHECK_META)
1377 btf_verifier_log_type(env, struct_type, NULL);
1378
Yonghong Song9d5f9f72018-12-15 22:13:51 -08001379 if (btf_type_kflag(struct_type))
1380 __btf_verifier_log(log,
1381 "\t%s type_id=%u bitfield_size=%u bits_offset=%u",
1382 __btf_name_by_offset(btf, member->name_off),
1383 member->type,
1384 BTF_MEMBER_BITFIELD_SIZE(member->offset),
1385 BTF_MEMBER_BIT_OFFSET(member->offset));
1386 else
1387 __btf_verifier_log(log, "\t%s type_id=%u bits_offset=%u",
1388 __btf_name_by_offset(btf, member->name_off),
1389 member->type, member->offset);
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001390
1391 if (fmt && *fmt) {
1392 __btf_verifier_log(log, " ");
1393 va_start(args, fmt);
1394 bpf_verifier_vlog(log, fmt, args);
1395 va_end(args);
1396 }
1397
1398 __btf_verifier_log(log, "\n");
1399}
1400
Daniel Borkmann1dc92852019-04-09 23:20:09 +02001401__printf(4, 5)
1402static void btf_verifier_log_vsi(struct btf_verifier_env *env,
1403 const struct btf_type *datasec_type,
1404 const struct btf_var_secinfo *vsi,
1405 const char *fmt, ...)
1406{
1407 struct bpf_verifier_log *log = &env->log;
1408 va_list args;
1409
1410 if (!bpf_verifier_log_needed(log))
1411 return;
Alexei Starovoitov8580ac92019-10-15 20:24:57 -07001412 if (log->level == BPF_LOG_KERNEL && !fmt)
1413 return;
Daniel Borkmann1dc92852019-04-09 23:20:09 +02001414 if (env->phase != CHECK_META)
1415 btf_verifier_log_type(env, datasec_type, NULL);
1416
1417 __btf_verifier_log(log, "\t type_id=%u offset=%u size=%u",
1418 vsi->type, vsi->offset, vsi->size);
1419 if (fmt && *fmt) {
1420 __btf_verifier_log(log, " ");
1421 va_start(args, fmt);
1422 bpf_verifier_vlog(log, fmt, args);
1423 va_end(args);
1424 }
1425
1426 __btf_verifier_log(log, "\n");
1427}
1428
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07001429static void btf_verifier_log_hdr(struct btf_verifier_env *env,
1430 u32 btf_data_size)
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001431{
1432 struct bpf_verifier_log *log = &env->log;
1433 const struct btf *btf = env->btf;
1434 const struct btf_header *hdr;
1435
1436 if (!bpf_verifier_log_needed(log))
1437 return;
1438
Alexei Starovoitov8580ac92019-10-15 20:24:57 -07001439 if (log->level == BPF_LOG_KERNEL)
1440 return;
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07001441 hdr = &btf->hdr;
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001442 __btf_verifier_log(log, "magic: 0x%x\n", hdr->magic);
1443 __btf_verifier_log(log, "version: %u\n", hdr->version);
1444 __btf_verifier_log(log, "flags: 0x%x\n", hdr->flags);
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07001445 __btf_verifier_log(log, "hdr_len: %u\n", hdr->hdr_len);
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001446 __btf_verifier_log(log, "type_off: %u\n", hdr->type_off);
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07001447 __btf_verifier_log(log, "type_len: %u\n", hdr->type_len);
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001448 __btf_verifier_log(log, "str_off: %u\n", hdr->str_off);
1449 __btf_verifier_log(log, "str_len: %u\n", hdr->str_len);
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07001450 __btf_verifier_log(log, "btf_total_size: %u\n", btf_data_size);
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001451}
1452
1453static int btf_add_type(struct btf_verifier_env *env, struct btf_type *t)
1454{
1455 struct btf *btf = env->btf;
1456
Andrii Nakryiko951bb642020-11-09 17:19:28 -08001457 if (btf->types_size == btf->nr_types) {
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001458 /* Expand 'types' array */
1459
1460 struct btf_type **new_types;
1461 u32 expand_by, new_size;
1462
Andrii Nakryiko951bb642020-11-09 17:19:28 -08001463 if (btf->start_id + btf->types_size == BTF_MAX_TYPE) {
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001464 btf_verifier_log(env, "Exceeded max num of types");
1465 return -E2BIG;
1466 }
1467
1468 expand_by = max_t(u32, btf->types_size >> 2, 16);
Martin KaFai Lauaea2f7b82018-05-22 14:57:20 -07001469 new_size = min_t(u32, BTF_MAX_TYPE,
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001470 btf->types_size + expand_by);
1471
Kees Cook778e1cd2018-06-12 14:04:48 -07001472 new_types = kvcalloc(new_size, sizeof(*new_types),
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001473 GFP_KERNEL | __GFP_NOWARN);
1474 if (!new_types)
1475 return -ENOMEM;
1476
Andrii Nakryiko951bb642020-11-09 17:19:28 -08001477 if (btf->nr_types == 0) {
1478 if (!btf->base_btf) {
1479 /* lazily init VOID type */
1480 new_types[0] = &btf_void;
1481 btf->nr_types++;
1482 }
1483 } else {
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001484 memcpy(new_types, btf->types,
Andrii Nakryiko951bb642020-11-09 17:19:28 -08001485 sizeof(*btf->types) * btf->nr_types);
1486 }
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001487
1488 kvfree(btf->types);
1489 btf->types = new_types;
1490 btf->types_size = new_size;
1491 }
1492
Andrii Nakryiko951bb642020-11-09 17:19:28 -08001493 btf->types[btf->nr_types++] = t;
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001494
1495 return 0;
1496}
1497
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07001498static int btf_alloc_id(struct btf *btf)
1499{
1500 int id;
1501
1502 idr_preload(GFP_KERNEL);
1503 spin_lock_bh(&btf_idr_lock);
1504 id = idr_alloc_cyclic(&btf_idr, btf, 1, INT_MAX, GFP_ATOMIC);
1505 if (id > 0)
1506 btf->id = id;
1507 spin_unlock_bh(&btf_idr_lock);
1508 idr_preload_end();
1509
1510 if (WARN_ON_ONCE(!id))
1511 return -ENOSPC;
1512
1513 return id > 0 ? 0 : id;
1514}
1515
1516static void btf_free_id(struct btf *btf)
1517{
1518 unsigned long flags;
1519
1520 /*
1521 * In map-in-map, calling map_delete_elem() on outer
1522 * map will call bpf_map_put on the inner map.
1523 * It will then eventually call btf_free_id()
1524 * on the inner map. Some of the map_delete_elem()
1525 * implementation may have irq disabled, so
1526 * we need to use the _irqsave() version instead
1527 * of the _bh() version.
1528 */
1529 spin_lock_irqsave(&btf_idr_lock, flags);
1530 idr_remove(&btf_idr, btf->id);
1531 spin_unlock_irqrestore(&btf_idr_lock, flags);
1532}
1533
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001534static void btf_free(struct btf *btf)
1535{
1536 kvfree(btf->types);
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001537 kvfree(btf->resolved_sizes);
1538 kvfree(btf->resolved_ids);
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001539 kvfree(btf->data);
1540 kfree(btf);
1541}
1542
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07001543static void btf_free_rcu(struct rcu_head *rcu)
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07001544{
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07001545 struct btf *btf = container_of(rcu, struct btf, rcu);
1546
1547 btf_free(btf);
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07001548}
1549
Andrii Nakryiko22dc4a02020-12-03 12:46:29 -08001550void btf_get(struct btf *btf)
1551{
1552 refcount_inc(&btf->refcnt);
1553}
1554
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07001555void btf_put(struct btf *btf)
1556{
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07001557 if (btf && refcount_dec_and_test(&btf->refcnt)) {
1558 btf_free_id(btf);
1559 call_rcu(&btf->rcu, btf_free_rcu);
1560 }
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07001561}
1562
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001563static int env_resolve_init(struct btf_verifier_env *env)
1564{
1565 struct btf *btf = env->btf;
1566 u32 nr_types = btf->nr_types;
1567 u32 *resolved_sizes = NULL;
1568 u32 *resolved_ids = NULL;
1569 u8 *visit_states = NULL;
1570
Andrii Nakryiko951bb642020-11-09 17:19:28 -08001571 resolved_sizes = kvcalloc(nr_types, sizeof(*resolved_sizes),
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001572 GFP_KERNEL | __GFP_NOWARN);
1573 if (!resolved_sizes)
1574 goto nomem;
1575
Andrii Nakryiko951bb642020-11-09 17:19:28 -08001576 resolved_ids = kvcalloc(nr_types, sizeof(*resolved_ids),
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001577 GFP_KERNEL | __GFP_NOWARN);
1578 if (!resolved_ids)
1579 goto nomem;
1580
Andrii Nakryiko951bb642020-11-09 17:19:28 -08001581 visit_states = kvcalloc(nr_types, sizeof(*visit_states),
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001582 GFP_KERNEL | __GFP_NOWARN);
1583 if (!visit_states)
1584 goto nomem;
1585
1586 btf->resolved_sizes = resolved_sizes;
1587 btf->resolved_ids = resolved_ids;
1588 env->visit_states = visit_states;
1589
1590 return 0;
1591
1592nomem:
1593 kvfree(resolved_sizes);
1594 kvfree(resolved_ids);
1595 kvfree(visit_states);
1596 return -ENOMEM;
1597}
1598
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001599static void btf_verifier_env_free(struct btf_verifier_env *env)
1600{
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001601 kvfree(env->visit_states);
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001602 kfree(env);
1603}
1604
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001605static bool env_type_is_resolve_sink(const struct btf_verifier_env *env,
1606 const struct btf_type *next_type)
1607{
1608 switch (env->resolve_mode) {
1609 case RESOLVE_TBD:
1610 /* int, enum or void is a sink */
1611 return !btf_type_needs_resolve(next_type);
1612 case RESOLVE_PTR:
Martin KaFai Lau2667a262018-11-19 15:29:08 -08001613 /* int, enum, void, struct, array, func or func_proto is a sink
1614 * for ptr
1615 */
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001616 return !btf_type_is_modifier(next_type) &&
1617 !btf_type_is_ptr(next_type);
1618 case RESOLVE_STRUCT_OR_ARRAY:
Martin KaFai Lau2667a262018-11-19 15:29:08 -08001619 /* int, enum, void, ptr, func or func_proto is a sink
1620 * for struct and array
1621 */
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001622 return !btf_type_is_modifier(next_type) &&
1623 !btf_type_is_array(next_type) &&
1624 !btf_type_is_struct(next_type);
1625 default:
Arnd Bergmann53c80362018-05-25 23:33:19 +02001626 BUG();
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001627 }
1628}
1629
1630static bool env_type_is_resolved(const struct btf_verifier_env *env,
1631 u32 type_id)
1632{
Andrii Nakryiko951bb642020-11-09 17:19:28 -08001633 /* base BTF types should be resolved by now */
1634 if (type_id < env->btf->start_id)
1635 return true;
1636
1637 return env->visit_states[type_id - env->btf->start_id] == RESOLVED;
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001638}
1639
1640static int env_stack_push(struct btf_verifier_env *env,
1641 const struct btf_type *t, u32 type_id)
1642{
Andrii Nakryiko951bb642020-11-09 17:19:28 -08001643 const struct btf *btf = env->btf;
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001644 struct resolve_vertex *v;
1645
1646 if (env->top_stack == MAX_RESOLVE_DEPTH)
1647 return -E2BIG;
1648
Andrii Nakryiko951bb642020-11-09 17:19:28 -08001649 if (type_id < btf->start_id
1650 || env->visit_states[type_id - btf->start_id] != NOT_VISITED)
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001651 return -EEXIST;
1652
Andrii Nakryiko951bb642020-11-09 17:19:28 -08001653 env->visit_states[type_id - btf->start_id] = VISITED;
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001654
1655 v = &env->stack[env->top_stack++];
1656 v->t = t;
1657 v->type_id = type_id;
1658 v->next_member = 0;
1659
1660 if (env->resolve_mode == RESOLVE_TBD) {
1661 if (btf_type_is_ptr(t))
1662 env->resolve_mode = RESOLVE_PTR;
1663 else if (btf_type_is_struct(t) || btf_type_is_array(t))
1664 env->resolve_mode = RESOLVE_STRUCT_OR_ARRAY;
1665 }
1666
1667 return 0;
1668}
1669
1670static void env_stack_set_next_member(struct btf_verifier_env *env,
1671 u16 next_member)
1672{
1673 env->stack[env->top_stack - 1].next_member = next_member;
1674}
1675
1676static void env_stack_pop_resolved(struct btf_verifier_env *env,
1677 u32 resolved_type_id,
1678 u32 resolved_size)
1679{
1680 u32 type_id = env->stack[--(env->top_stack)].type_id;
1681 struct btf *btf = env->btf;
1682
Andrii Nakryiko951bb642020-11-09 17:19:28 -08001683 type_id -= btf->start_id; /* adjust to local type id */
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001684 btf->resolved_sizes[type_id] = resolved_size;
1685 btf->resolved_ids[type_id] = resolved_type_id;
1686 env->visit_states[type_id] = RESOLVED;
1687}
1688
1689static const struct resolve_vertex *env_stack_peak(struct btf_verifier_env *env)
1690{
1691 return env->top_stack ? &env->stack[env->top_stack - 1] : NULL;
1692}
1693
Martin KaFai Lau7e3617a2019-11-07 10:09:03 -08001694/* Resolve the size of a passed-in "type"
1695 *
1696 * type: is an array (e.g. u32 array[x][y])
1697 * return type: type "u32[x][y]", i.e. BTF_KIND_ARRAY,
1698 * *type_size: (x * y * sizeof(u32)). Hence, *type_size always
1699 * corresponds to the return type.
1700 * *elem_type: u32
Jiri Olsa69ff3042020-08-25 21:21:14 +02001701 * *elem_id: id of u32
Martin KaFai Lau7e3617a2019-11-07 10:09:03 -08001702 * *total_nelems: (x * y). Hence, individual elem size is
1703 * (*type_size / *total_nelems)
Jiri Olsa887c31a2020-08-25 21:21:15 +02001704 * *type_id: id of type if it's changed within the function, 0 if not
Martin KaFai Lau7e3617a2019-11-07 10:09:03 -08001705 *
1706 * type: is not an array (e.g. const struct X)
1707 * return type: type "struct X"
1708 * *type_size: sizeof(struct X)
1709 * *elem_type: same as return type ("struct X")
Jiri Olsa69ff3042020-08-25 21:21:14 +02001710 * *elem_id: 0
Martin KaFai Lau7e3617a2019-11-07 10:09:03 -08001711 * *total_nelems: 1
Jiri Olsa887c31a2020-08-25 21:21:15 +02001712 * *type_id: id of type if it's changed within the function, 0 if not
Martin KaFai Lau7e3617a2019-11-07 10:09:03 -08001713 */
Jiri Olsa62983992020-08-25 21:21:13 +02001714static const struct btf_type *
1715__btf_resolve_size(const struct btf *btf, const struct btf_type *type,
1716 u32 *type_size, const struct btf_type **elem_type,
Jiri Olsa887c31a2020-08-25 21:21:15 +02001717 u32 *elem_id, u32 *total_nelems, u32 *type_id)
Martin KaFai Lau7e3617a2019-11-07 10:09:03 -08001718{
1719 const struct btf_type *array_type = NULL;
Jiri Olsa69ff3042020-08-25 21:21:14 +02001720 const struct btf_array *array = NULL;
Jiri Olsa887c31a2020-08-25 21:21:15 +02001721 u32 i, size, nelems = 1, id = 0;
Martin KaFai Lau7e3617a2019-11-07 10:09:03 -08001722
1723 for (i = 0; i < MAX_RESOLVE_DEPTH; i++) {
1724 switch (BTF_INFO_KIND(type->info)) {
1725 /* type->size can be used */
1726 case BTF_KIND_INT:
1727 case BTF_KIND_STRUCT:
1728 case BTF_KIND_UNION:
1729 case BTF_KIND_ENUM:
Ilya Leoshkevichb1828f02021-02-26 21:22:52 +01001730 case BTF_KIND_FLOAT:
Martin KaFai Lau7e3617a2019-11-07 10:09:03 -08001731 size = type->size;
1732 goto resolved;
1733
1734 case BTF_KIND_PTR:
1735 size = sizeof(void *);
1736 goto resolved;
1737
1738 /* Modifiers */
1739 case BTF_KIND_TYPEDEF:
1740 case BTF_KIND_VOLATILE:
1741 case BTF_KIND_CONST:
1742 case BTF_KIND_RESTRICT:
Yonghong Song8c42d2f2021-11-11 17:26:09 -08001743 case BTF_KIND_TYPE_TAG:
Jiri Olsa887c31a2020-08-25 21:21:15 +02001744 id = type->type;
Martin KaFai Lau7e3617a2019-11-07 10:09:03 -08001745 type = btf_type_by_id(btf, type->type);
1746 break;
1747
1748 case BTF_KIND_ARRAY:
1749 if (!array_type)
1750 array_type = type;
1751 array = btf_type_array(type);
1752 if (nelems && array->nelems > U32_MAX / nelems)
1753 return ERR_PTR(-EINVAL);
1754 nelems *= array->nelems;
1755 type = btf_type_by_id(btf, array->type);
1756 break;
1757
1758 /* type without size */
1759 default:
1760 return ERR_PTR(-EINVAL);
1761 }
1762 }
1763
1764 return ERR_PTR(-EINVAL);
1765
1766resolved:
1767 if (nelems && size > U32_MAX / nelems)
1768 return ERR_PTR(-EINVAL);
1769
1770 *type_size = nelems * size;
Martin KaFai Lau85d33df2020-01-08 16:35:05 -08001771 if (total_nelems)
1772 *total_nelems = nelems;
1773 if (elem_type)
1774 *elem_type = type;
Jiri Olsa69ff3042020-08-25 21:21:14 +02001775 if (elem_id)
1776 *elem_id = array ? array->type : 0;
Jiri Olsa887c31a2020-08-25 21:21:15 +02001777 if (type_id && id)
1778 *type_id = id;
Martin KaFai Lau7e3617a2019-11-07 10:09:03 -08001779
1780 return array_type ? : type;
1781}
1782
Jiri Olsa62983992020-08-25 21:21:13 +02001783const struct btf_type *
1784btf_resolve_size(const struct btf *btf, const struct btf_type *type,
1785 u32 *type_size)
1786{
Jiri Olsa887c31a2020-08-25 21:21:15 +02001787 return __btf_resolve_size(btf, type, type_size, NULL, NULL, NULL, NULL);
Jiri Olsa62983992020-08-25 21:21:13 +02001788}
1789
Andrii Nakryiko951bb642020-11-09 17:19:28 -08001790static u32 btf_resolved_type_id(const struct btf *btf, u32 type_id)
1791{
1792 while (type_id < btf->start_id)
1793 btf = btf->base_btf;
1794
1795 return btf->resolved_ids[type_id - btf->start_id];
1796}
1797
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001798/* The input param "type_id" must point to a needs_resolve type */
1799static const struct btf_type *btf_type_id_resolve(const struct btf *btf,
1800 u32 *type_id)
1801{
Andrii Nakryiko951bb642020-11-09 17:19:28 -08001802 *type_id = btf_resolved_type_id(btf, *type_id);
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001803 return btf_type_by_id(btf, *type_id);
1804}
1805
Andrii Nakryiko951bb642020-11-09 17:19:28 -08001806static u32 btf_resolved_type_size(const struct btf *btf, u32 type_id)
1807{
1808 while (type_id < btf->start_id)
1809 btf = btf->base_btf;
1810
1811 return btf->resolved_sizes[type_id - btf->start_id];
1812}
1813
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001814const struct btf_type *btf_type_id_size(const struct btf *btf,
1815 u32 *type_id, u32 *ret_size)
1816{
1817 const struct btf_type *size_type;
1818 u32 size_type_id = *type_id;
1819 u32 size = 0;
1820
1821 size_type = btf_type_by_id(btf, size_type_id);
Martin KaFai Laub47a0bd2018-11-19 15:29:06 -08001822 if (btf_type_nosize_or_null(size_type))
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001823 return NULL;
1824
1825 if (btf_type_has_size(size_type)) {
1826 size = size_type->size;
1827 } else if (btf_type_is_array(size_type)) {
Andrii Nakryiko951bb642020-11-09 17:19:28 -08001828 size = btf_resolved_type_size(btf, size_type_id);
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001829 } else if (btf_type_is_ptr(size_type)) {
1830 size = sizeof(void *);
1831 } else {
Daniel Borkmann1dc92852019-04-09 23:20:09 +02001832 if (WARN_ON_ONCE(!btf_type_is_modifier(size_type) &&
1833 !btf_type_is_var(size_type)))
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001834 return NULL;
1835
Andrii Nakryiko951bb642020-11-09 17:19:28 -08001836 size_type_id = btf_resolved_type_id(btf, size_type_id);
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001837 size_type = btf_type_by_id(btf, size_type_id);
Martin KaFai Laub47a0bd2018-11-19 15:29:06 -08001838 if (btf_type_nosize_or_null(size_type))
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001839 return NULL;
Andrii Nakryiko1acc5d52019-07-12 10:25:55 -07001840 else if (btf_type_has_size(size_type))
1841 size = size_type->size;
1842 else if (btf_type_is_array(size_type))
Andrii Nakryiko951bb642020-11-09 17:19:28 -08001843 size = btf_resolved_type_size(btf, size_type_id);
Andrii Nakryiko1acc5d52019-07-12 10:25:55 -07001844 else if (btf_type_is_ptr(size_type))
1845 size = sizeof(void *);
1846 else
1847 return NULL;
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001848 }
1849
1850 *type_id = size_type_id;
1851 if (ret_size)
1852 *ret_size = size;
1853
1854 return size_type;
1855}
1856
Martin KaFai Lau179cde82018-04-18 15:55:59 -07001857static int btf_df_check_member(struct btf_verifier_env *env,
1858 const struct btf_type *struct_type,
1859 const struct btf_member *member,
1860 const struct btf_type *member_type)
1861{
1862 btf_verifier_log_basic(env, struct_type,
1863 "Unsupported check_member");
1864 return -EINVAL;
1865}
1866
Yonghong Song9d5f9f72018-12-15 22:13:51 -08001867static int btf_df_check_kflag_member(struct btf_verifier_env *env,
1868 const struct btf_type *struct_type,
1869 const struct btf_member *member,
1870 const struct btf_type *member_type)
1871{
1872 btf_verifier_log_basic(env, struct_type,
1873 "Unsupported check_kflag_member");
1874 return -EINVAL;
1875}
1876
Ilya Leoshkevichb1828f02021-02-26 21:22:52 +01001877/* Used for ptr, array struct/union and float type members.
Yonghong Song9d5f9f72018-12-15 22:13:51 -08001878 * int, enum and modifier types have their specific callback functions.
1879 */
1880static int btf_generic_check_kflag_member(struct btf_verifier_env *env,
1881 const struct btf_type *struct_type,
1882 const struct btf_member *member,
1883 const struct btf_type *member_type)
1884{
1885 if (BTF_MEMBER_BITFIELD_SIZE(member->offset)) {
1886 btf_verifier_log_member(env, struct_type, member,
1887 "Invalid member bitfield_size");
1888 return -EINVAL;
1889 }
1890
1891 /* bitfield size is 0, so member->offset represents bit offset only.
1892 * It is safe to call non kflag check_member variants.
1893 */
1894 return btf_type_ops(member_type)->check_member(env, struct_type,
1895 member,
1896 member_type);
1897}
1898
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001899static int btf_df_resolve(struct btf_verifier_env *env,
1900 const struct resolve_vertex *v)
1901{
1902 btf_verifier_log_basic(env, v->t, "Unsupported resolve");
1903 return -EINVAL;
1904}
1905
Alan Maguire31d0bc82020-09-28 12:31:04 +01001906static void btf_df_show(const struct btf *btf, const struct btf_type *t,
1907 u32 type_id, void *data, u8 bits_offsets,
1908 struct btf_show *show)
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07001909{
Alan Maguire31d0bc82020-09-28 12:31:04 +01001910 btf_show(show, "<unsupported kind:%u>", BTF_INFO_KIND(t->info));
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07001911}
1912
Martin KaFai Lau179cde82018-04-18 15:55:59 -07001913static int btf_int_check_member(struct btf_verifier_env *env,
1914 const struct btf_type *struct_type,
1915 const struct btf_member *member,
1916 const struct btf_type *member_type)
1917{
1918 u32 int_data = btf_type_int(member_type);
1919 u32 struct_bits_off = member->offset;
1920 u32 struct_size = struct_type->size;
1921 u32 nr_copy_bits;
1922 u32 bytes_offset;
1923
1924 if (U32_MAX - struct_bits_off < BTF_INT_OFFSET(int_data)) {
1925 btf_verifier_log_member(env, struct_type, member,
1926 "bits_offset exceeds U32_MAX");
1927 return -EINVAL;
1928 }
1929
1930 struct_bits_off += BTF_INT_OFFSET(int_data);
1931 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
1932 nr_copy_bits = BTF_INT_BITS(int_data) +
1933 BITS_PER_BYTE_MASKED(struct_bits_off);
1934
Yonghong Songb1e88182019-01-15 17:07:47 -08001935 if (nr_copy_bits > BITS_PER_U128) {
Martin KaFai Lau179cde82018-04-18 15:55:59 -07001936 btf_verifier_log_member(env, struct_type, member,
Yonghong Songb1e88182019-01-15 17:07:47 -08001937 "nr_copy_bits exceeds 128");
Martin KaFai Lau179cde82018-04-18 15:55:59 -07001938 return -EINVAL;
1939 }
1940
1941 if (struct_size < bytes_offset ||
1942 struct_size - bytes_offset < BITS_ROUNDUP_BYTES(nr_copy_bits)) {
1943 btf_verifier_log_member(env, struct_type, member,
1944 "Member exceeds struct_size");
1945 return -EINVAL;
1946 }
1947
1948 return 0;
1949}
1950
Yonghong Song9d5f9f72018-12-15 22:13:51 -08001951static int btf_int_check_kflag_member(struct btf_verifier_env *env,
1952 const struct btf_type *struct_type,
1953 const struct btf_member *member,
1954 const struct btf_type *member_type)
1955{
1956 u32 struct_bits_off, nr_bits, nr_int_data_bits, bytes_offset;
1957 u32 int_data = btf_type_int(member_type);
1958 u32 struct_size = struct_type->size;
1959 u32 nr_copy_bits;
1960
1961 /* a regular int type is required for the kflag int member */
1962 if (!btf_type_int_is_regular(member_type)) {
1963 btf_verifier_log_member(env, struct_type, member,
1964 "Invalid member base type");
1965 return -EINVAL;
1966 }
1967
1968 /* check sanity of bitfield size */
1969 nr_bits = BTF_MEMBER_BITFIELD_SIZE(member->offset);
1970 struct_bits_off = BTF_MEMBER_BIT_OFFSET(member->offset);
1971 nr_int_data_bits = BTF_INT_BITS(int_data);
1972 if (!nr_bits) {
1973 /* Not a bitfield member, member offset must be at byte
1974 * boundary.
1975 */
1976 if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
1977 btf_verifier_log_member(env, struct_type, member,
1978 "Invalid member offset");
1979 return -EINVAL;
1980 }
1981
1982 nr_bits = nr_int_data_bits;
1983 } else if (nr_bits > nr_int_data_bits) {
1984 btf_verifier_log_member(env, struct_type, member,
1985 "Invalid member bitfield_size");
1986 return -EINVAL;
1987 }
1988
1989 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
1990 nr_copy_bits = nr_bits + BITS_PER_BYTE_MASKED(struct_bits_off);
Yonghong Songb1e88182019-01-15 17:07:47 -08001991 if (nr_copy_bits > BITS_PER_U128) {
Yonghong Song9d5f9f72018-12-15 22:13:51 -08001992 btf_verifier_log_member(env, struct_type, member,
Yonghong Songb1e88182019-01-15 17:07:47 -08001993 "nr_copy_bits exceeds 128");
Yonghong Song9d5f9f72018-12-15 22:13:51 -08001994 return -EINVAL;
1995 }
1996
1997 if (struct_size < bytes_offset ||
1998 struct_size - bytes_offset < BITS_ROUNDUP_BYTES(nr_copy_bits)) {
1999 btf_verifier_log_member(env, struct_type, member,
2000 "Member exceeds struct_size");
2001 return -EINVAL;
2002 }
2003
2004 return 0;
2005}
2006
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002007static s32 btf_int_check_meta(struct btf_verifier_env *env,
2008 const struct btf_type *t,
2009 u32 meta_left)
2010{
2011 u32 int_data, nr_bits, meta_needed = sizeof(int_data);
2012 u16 encoding;
2013
2014 if (meta_left < meta_needed) {
2015 btf_verifier_log_basic(env, t,
2016 "meta_left:%u meta_needed:%u",
2017 meta_left, meta_needed);
2018 return -EINVAL;
2019 }
2020
2021 if (btf_type_vlen(t)) {
2022 btf_verifier_log_type(env, t, "vlen != 0");
2023 return -EINVAL;
2024 }
2025
Yonghong Song9d5f9f72018-12-15 22:13:51 -08002026 if (btf_type_kflag(t)) {
2027 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
2028 return -EINVAL;
2029 }
2030
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002031 int_data = btf_type_int(t);
Martin KaFai Lauaea2f7b82018-05-22 14:57:20 -07002032 if (int_data & ~BTF_INT_MASK) {
2033 btf_verifier_log_basic(env, t, "Invalid int_data:%x",
2034 int_data);
2035 return -EINVAL;
2036 }
2037
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002038 nr_bits = BTF_INT_BITS(int_data) + BTF_INT_OFFSET(int_data);
2039
Yonghong Songb1e88182019-01-15 17:07:47 -08002040 if (nr_bits > BITS_PER_U128) {
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002041 btf_verifier_log_type(env, t, "nr_bits exceeds %zu",
Yonghong Songb1e88182019-01-15 17:07:47 -08002042 BITS_PER_U128);
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002043 return -EINVAL;
2044 }
2045
2046 if (BITS_ROUNDUP_BYTES(nr_bits) > t->size) {
2047 btf_verifier_log_type(env, t, "nr_bits exceeds type_size");
2048 return -EINVAL;
2049 }
2050
Martin KaFai Lauaea2f7b82018-05-22 14:57:20 -07002051 /*
2052 * Only one of the encoding bits is allowed and it
2053 * should be sufficient for the pretty print purpose (i.e. decoding).
2054 * Multiple bits can be allowed later if it is found
2055 * to be insufficient.
2056 */
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002057 encoding = BTF_INT_ENCODING(int_data);
2058 if (encoding &&
2059 encoding != BTF_INT_SIGNED &&
2060 encoding != BTF_INT_CHAR &&
Martin KaFai Lauaea2f7b82018-05-22 14:57:20 -07002061 encoding != BTF_INT_BOOL) {
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002062 btf_verifier_log_type(env, t, "Unsupported encoding");
2063 return -ENOTSUPP;
2064 }
2065
2066 btf_verifier_log_type(env, t, NULL);
2067
2068 return meta_needed;
2069}
2070
2071static void btf_int_log(struct btf_verifier_env *env,
2072 const struct btf_type *t)
2073{
2074 int int_data = btf_type_int(t);
2075
2076 btf_verifier_log(env,
2077 "size=%u bits_offset=%u nr_bits=%u encoding=%s",
2078 t->size, BTF_INT_OFFSET(int_data),
2079 BTF_INT_BITS(int_data),
2080 btf_int_encoding_str(BTF_INT_ENCODING(int_data)));
2081}
2082
Alan Maguire31d0bc82020-09-28 12:31:04 +01002083static void btf_int128_print(struct btf_show *show, void *data)
Yonghong Songb1e88182019-01-15 17:07:47 -08002084{
2085 /* data points to a __int128 number.
2086 * Suppose
2087 * int128_num = *(__int128 *)data;
2088 * The below formulas shows what upper_num and lower_num represents:
2089 * upper_num = int128_num >> 64;
2090 * lower_num = int128_num & 0xffffffffFFFFFFFFULL;
2091 */
2092 u64 upper_num, lower_num;
2093
2094#ifdef __BIG_ENDIAN_BITFIELD
2095 upper_num = *(u64 *)data;
2096 lower_num = *(u64 *)(data + 8);
2097#else
2098 upper_num = *(u64 *)(data + 8);
2099 lower_num = *(u64 *)data;
2100#endif
2101 if (upper_num == 0)
Alan Maguire31d0bc82020-09-28 12:31:04 +01002102 btf_show_type_value(show, "0x%llx", lower_num);
Yonghong Songb1e88182019-01-15 17:07:47 -08002103 else
Alan Maguire31d0bc82020-09-28 12:31:04 +01002104 btf_show_type_values(show, "0x%llx%016llx", upper_num,
2105 lower_num);
Yonghong Songb1e88182019-01-15 17:07:47 -08002106}
2107
2108static void btf_int128_shift(u64 *print_num, u16 left_shift_bits,
2109 u16 right_shift_bits)
2110{
2111 u64 upper_num, lower_num;
2112
2113#ifdef __BIG_ENDIAN_BITFIELD
2114 upper_num = print_num[0];
2115 lower_num = print_num[1];
2116#else
2117 upper_num = print_num[1];
2118 lower_num = print_num[0];
2119#endif
2120
2121 /* shake out un-needed bits by shift/or operations */
2122 if (left_shift_bits >= 64) {
2123 upper_num = lower_num << (left_shift_bits - 64);
2124 lower_num = 0;
2125 } else {
2126 upper_num = (upper_num << left_shift_bits) |
2127 (lower_num >> (64 - left_shift_bits));
2128 lower_num = lower_num << left_shift_bits;
2129 }
2130
2131 if (right_shift_bits >= 64) {
2132 lower_num = upper_num >> (right_shift_bits - 64);
2133 upper_num = 0;
2134 } else {
2135 lower_num = (lower_num >> right_shift_bits) |
2136 (upper_num << (64 - right_shift_bits));
2137 upper_num = upper_num >> right_shift_bits;
2138 }
2139
2140#ifdef __BIG_ENDIAN_BITFIELD
2141 print_num[0] = upper_num;
2142 print_num[1] = lower_num;
2143#else
2144 print_num[0] = lower_num;
2145 print_num[1] = upper_num;
2146#endif
2147}
2148
Alan Maguire31d0bc82020-09-28 12:31:04 +01002149static void btf_bitfield_show(void *data, u8 bits_offset,
2150 u8 nr_bits, struct btf_show *show)
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07002151{
Okash Khawajab65f3702018-07-10 14:33:07 -07002152 u16 left_shift_bits, right_shift_bits;
Martin KaFai Lau36fc3c82018-07-19 22:14:31 -07002153 u8 nr_copy_bytes;
2154 u8 nr_copy_bits;
Yonghong Songb1e88182019-01-15 17:07:47 -08002155 u64 print_num[2] = {};
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07002156
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07002157 nr_copy_bits = nr_bits + bits_offset;
2158 nr_copy_bytes = BITS_ROUNDUP_BYTES(nr_copy_bits);
2159
Yonghong Songb1e88182019-01-15 17:07:47 -08002160 memcpy(print_num, data, nr_copy_bytes);
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07002161
Okash Khawajab65f3702018-07-10 14:33:07 -07002162#ifdef __BIG_ENDIAN_BITFIELD
2163 left_shift_bits = bits_offset;
2164#else
Yonghong Songb1e88182019-01-15 17:07:47 -08002165 left_shift_bits = BITS_PER_U128 - nr_copy_bits;
Okash Khawajab65f3702018-07-10 14:33:07 -07002166#endif
Yonghong Songb1e88182019-01-15 17:07:47 -08002167 right_shift_bits = BITS_PER_U128 - nr_bits;
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07002168
Yonghong Songb1e88182019-01-15 17:07:47 -08002169 btf_int128_shift(print_num, left_shift_bits, right_shift_bits);
Alan Maguire31d0bc82020-09-28 12:31:04 +01002170 btf_int128_print(show, print_num);
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07002171}
2172
Yonghong Song9d5f9f72018-12-15 22:13:51 -08002173
Alan Maguire31d0bc82020-09-28 12:31:04 +01002174static void btf_int_bits_show(const struct btf *btf,
2175 const struct btf_type *t,
2176 void *data, u8 bits_offset,
2177 struct btf_show *show)
Yonghong Songf97be3a2018-12-15 22:13:50 -08002178{
2179 u32 int_data = btf_type_int(t);
2180 u8 nr_bits = BTF_INT_BITS(int_data);
2181 u8 total_bits_offset;
2182
2183 /*
2184 * bits_offset is at most 7.
Yonghong Songb1e88182019-01-15 17:07:47 -08002185 * BTF_INT_OFFSET() cannot exceed 128 bits.
Yonghong Songf97be3a2018-12-15 22:13:50 -08002186 */
2187 total_bits_offset = bits_offset + BTF_INT_OFFSET(int_data);
Yonghong Song17e3ac82019-01-10 11:14:00 -08002188 data += BITS_ROUNDDOWN_BYTES(total_bits_offset);
2189 bits_offset = BITS_PER_BYTE_MASKED(total_bits_offset);
Alan Maguire31d0bc82020-09-28 12:31:04 +01002190 btf_bitfield_show(data, bits_offset, nr_bits, show);
Yonghong Songf97be3a2018-12-15 22:13:50 -08002191}
2192
Alan Maguire31d0bc82020-09-28 12:31:04 +01002193static void btf_int_show(const struct btf *btf, const struct btf_type *t,
2194 u32 type_id, void *data, u8 bits_offset,
2195 struct btf_show *show)
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07002196{
2197 u32 int_data = btf_type_int(t);
2198 u8 encoding = BTF_INT_ENCODING(int_data);
2199 bool sign = encoding & BTF_INT_SIGNED;
Martin KaFai Lau36fc3c82018-07-19 22:14:31 -07002200 u8 nr_bits = BTF_INT_BITS(int_data);
Alan Maguire31d0bc82020-09-28 12:31:04 +01002201 void *safe_data;
2202
2203 safe_data = btf_show_start_type(show, t, type_id, data);
2204 if (!safe_data)
2205 return;
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07002206
2207 if (bits_offset || BTF_INT_OFFSET(int_data) ||
2208 BITS_PER_BYTE_MASKED(nr_bits)) {
Alan Maguire31d0bc82020-09-28 12:31:04 +01002209 btf_int_bits_show(btf, t, safe_data, bits_offset, show);
2210 goto out;
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07002211 }
2212
2213 switch (nr_bits) {
Yonghong Songb1e88182019-01-15 17:07:47 -08002214 case 128:
Alan Maguire31d0bc82020-09-28 12:31:04 +01002215 btf_int128_print(show, safe_data);
Yonghong Songb1e88182019-01-15 17:07:47 -08002216 break;
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07002217 case 64:
2218 if (sign)
Alan Maguire31d0bc82020-09-28 12:31:04 +01002219 btf_show_type_value(show, "%lld", *(s64 *)safe_data);
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07002220 else
Alan Maguire31d0bc82020-09-28 12:31:04 +01002221 btf_show_type_value(show, "%llu", *(u64 *)safe_data);
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07002222 break;
2223 case 32:
2224 if (sign)
Alan Maguire31d0bc82020-09-28 12:31:04 +01002225 btf_show_type_value(show, "%d", *(s32 *)safe_data);
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07002226 else
Alan Maguire31d0bc82020-09-28 12:31:04 +01002227 btf_show_type_value(show, "%u", *(u32 *)safe_data);
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07002228 break;
2229 case 16:
2230 if (sign)
Alan Maguire31d0bc82020-09-28 12:31:04 +01002231 btf_show_type_value(show, "%d", *(s16 *)safe_data);
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07002232 else
Alan Maguire31d0bc82020-09-28 12:31:04 +01002233 btf_show_type_value(show, "%u", *(u16 *)safe_data);
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07002234 break;
2235 case 8:
Alan Maguire31d0bc82020-09-28 12:31:04 +01002236 if (show->state.array_encoding == BTF_INT_CHAR) {
2237 /* check for null terminator */
2238 if (show->state.array_terminated)
2239 break;
2240 if (*(char *)data == '\0') {
2241 show->state.array_terminated = 1;
2242 break;
2243 }
2244 if (isprint(*(char *)data)) {
2245 btf_show_type_value(show, "'%c'",
2246 *(char *)safe_data);
2247 break;
2248 }
2249 }
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07002250 if (sign)
Alan Maguire31d0bc82020-09-28 12:31:04 +01002251 btf_show_type_value(show, "%d", *(s8 *)safe_data);
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07002252 else
Alan Maguire31d0bc82020-09-28 12:31:04 +01002253 btf_show_type_value(show, "%u", *(u8 *)safe_data);
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07002254 break;
2255 default:
Alan Maguire31d0bc82020-09-28 12:31:04 +01002256 btf_int_bits_show(btf, t, safe_data, bits_offset, show);
2257 break;
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07002258 }
Alan Maguire31d0bc82020-09-28 12:31:04 +01002259out:
2260 btf_show_end_type(show);
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07002261}
2262
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002263static const struct btf_kind_operations int_ops = {
2264 .check_meta = btf_int_check_meta,
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07002265 .resolve = btf_df_resolve,
Martin KaFai Lau179cde82018-04-18 15:55:59 -07002266 .check_member = btf_int_check_member,
Yonghong Song9d5f9f72018-12-15 22:13:51 -08002267 .check_kflag_member = btf_int_check_kflag_member,
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002268 .log_details = btf_int_log,
Alan Maguire31d0bc82020-09-28 12:31:04 +01002269 .show = btf_int_show,
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002270};
2271
Martin KaFai Lau179cde82018-04-18 15:55:59 -07002272static int btf_modifier_check_member(struct btf_verifier_env *env,
2273 const struct btf_type *struct_type,
2274 const struct btf_member *member,
2275 const struct btf_type *member_type)
2276{
2277 const struct btf_type *resolved_type;
2278 u32 resolved_type_id = member->type;
2279 struct btf_member resolved_member;
2280 struct btf *btf = env->btf;
2281
2282 resolved_type = btf_type_id_size(btf, &resolved_type_id, NULL);
2283 if (!resolved_type) {
2284 btf_verifier_log_member(env, struct_type, member,
2285 "Invalid member");
2286 return -EINVAL;
2287 }
2288
2289 resolved_member = *member;
2290 resolved_member.type = resolved_type_id;
2291
2292 return btf_type_ops(resolved_type)->check_member(env, struct_type,
2293 &resolved_member,
2294 resolved_type);
2295}
2296
Yonghong Song9d5f9f72018-12-15 22:13:51 -08002297static int btf_modifier_check_kflag_member(struct btf_verifier_env *env,
2298 const struct btf_type *struct_type,
2299 const struct btf_member *member,
2300 const struct btf_type *member_type)
2301{
2302 const struct btf_type *resolved_type;
2303 u32 resolved_type_id = member->type;
2304 struct btf_member resolved_member;
2305 struct btf *btf = env->btf;
2306
2307 resolved_type = btf_type_id_size(btf, &resolved_type_id, NULL);
2308 if (!resolved_type) {
2309 btf_verifier_log_member(env, struct_type, member,
2310 "Invalid member");
2311 return -EINVAL;
2312 }
2313
2314 resolved_member = *member;
2315 resolved_member.type = resolved_type_id;
2316
2317 return btf_type_ops(resolved_type)->check_kflag_member(env, struct_type,
2318 &resolved_member,
2319 resolved_type);
2320}
2321
Martin KaFai Lau179cde82018-04-18 15:55:59 -07002322static int btf_ptr_check_member(struct btf_verifier_env *env,
2323 const struct btf_type *struct_type,
2324 const struct btf_member *member,
2325 const struct btf_type *member_type)
2326{
2327 u32 struct_size, struct_bits_off, bytes_offset;
2328
2329 struct_size = struct_type->size;
2330 struct_bits_off = member->offset;
2331 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
2332
2333 if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
2334 btf_verifier_log_member(env, struct_type, member,
2335 "Member is not byte aligned");
2336 return -EINVAL;
2337 }
2338
2339 if (struct_size - bytes_offset < sizeof(void *)) {
2340 btf_verifier_log_member(env, struct_type, member,
2341 "Member exceeds struct_size");
2342 return -EINVAL;
2343 }
2344
2345 return 0;
2346}
2347
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002348static int btf_ref_type_check_meta(struct btf_verifier_env *env,
2349 const struct btf_type *t,
2350 u32 meta_left)
2351{
Yonghong Song8c42d2f2021-11-11 17:26:09 -08002352 const char *value;
2353
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002354 if (btf_type_vlen(t)) {
2355 btf_verifier_log_type(env, t, "vlen != 0");
2356 return -EINVAL;
2357 }
2358
Yonghong Song9d5f9f72018-12-15 22:13:51 -08002359 if (btf_type_kflag(t)) {
2360 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
2361 return -EINVAL;
2362 }
2363
Martin KaFai Lauaea2f7b82018-05-22 14:57:20 -07002364 if (!BTF_TYPE_ID_VALID(t->type)) {
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002365 btf_verifier_log_type(env, t, "Invalid type_id");
2366 return -EINVAL;
2367 }
2368
Yonghong Song8c42d2f2021-11-11 17:26:09 -08002369 /* typedef/type_tag type must have a valid name, and other ref types,
Yonghong Songeb04bbb2018-11-27 13:23:28 -08002370 * volatile, const, restrict, should have a null name.
2371 */
2372 if (BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF) {
2373 if (!t->name_off ||
2374 !btf_name_valid_identifier(env->btf, t->name_off)) {
2375 btf_verifier_log_type(env, t, "Invalid name");
2376 return -EINVAL;
2377 }
Yonghong Song8c42d2f2021-11-11 17:26:09 -08002378 } else if (BTF_INFO_KIND(t->info) == BTF_KIND_TYPE_TAG) {
2379 value = btf_name_by_offset(env->btf, t->name_off);
2380 if (!value || !value[0]) {
2381 btf_verifier_log_type(env, t, "Invalid name");
2382 return -EINVAL;
2383 }
Yonghong Songeb04bbb2018-11-27 13:23:28 -08002384 } else {
2385 if (t->name_off) {
2386 btf_verifier_log_type(env, t, "Invalid name");
2387 return -EINVAL;
2388 }
2389 }
2390
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002391 btf_verifier_log_type(env, t, NULL);
2392
2393 return 0;
2394}
2395
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07002396static int btf_modifier_resolve(struct btf_verifier_env *env,
2397 const struct resolve_vertex *v)
2398{
2399 const struct btf_type *t = v->t;
2400 const struct btf_type *next_type;
2401 u32 next_type_id = t->type;
2402 struct btf *btf = env->btf;
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07002403
2404 next_type = btf_type_by_id(btf, next_type_id);
Daniel Borkmann1dc92852019-04-09 23:20:09 +02002405 if (!next_type || btf_type_is_resolve_source_only(next_type)) {
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07002406 btf_verifier_log_type(env, v->t, "Invalid type_id");
2407 return -EINVAL;
2408 }
2409
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07002410 if (!env_type_is_resolve_sink(env, next_type) &&
2411 !env_type_is_resolved(env, next_type_id))
2412 return env_stack_push(env, next_type, next_type_id);
2413
2414 /* Figure out the resolved next_type_id with size.
2415 * They will be stored in the current modifier's
2416 * resolved_ids and resolved_sizes such that it can
2417 * save us a few type-following when we use it later (e.g. in
2418 * pretty print).
2419 */
Andrii Nakryiko1acc5d52019-07-12 10:25:55 -07002420 if (!btf_type_id_size(btf, &next_type_id, NULL)) {
Martin KaFai Lau2667a262018-11-19 15:29:08 -08002421 if (env_type_is_resolved(env, next_type_id))
2422 next_type = btf_type_id_resolve(btf, &next_type_id);
2423
2424 /* "typedef void new_void", "const void"...etc */
2425 if (!btf_type_is_void(next_type) &&
Yonghong Song81f5c6f2019-01-29 16:38:16 -08002426 !btf_type_is_fwd(next_type) &&
2427 !btf_type_is_func_proto(next_type)) {
Martin KaFai Lau2667a262018-11-19 15:29:08 -08002428 btf_verifier_log_type(env, v->t, "Invalid type_id");
2429 return -EINVAL;
2430 }
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07002431 }
2432
Andrii Nakryiko1acc5d52019-07-12 10:25:55 -07002433 env_stack_pop_resolved(env, next_type_id, 0);
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07002434
2435 return 0;
2436}
2437
Daniel Borkmann1dc92852019-04-09 23:20:09 +02002438static int btf_var_resolve(struct btf_verifier_env *env,
2439 const struct resolve_vertex *v)
2440{
2441 const struct btf_type *next_type;
2442 const struct btf_type *t = v->t;
2443 u32 next_type_id = t->type;
2444 struct btf *btf = env->btf;
Daniel Borkmann1dc92852019-04-09 23:20:09 +02002445
2446 next_type = btf_type_by_id(btf, next_type_id);
2447 if (!next_type || btf_type_is_resolve_source_only(next_type)) {
2448 btf_verifier_log_type(env, v->t, "Invalid type_id");
2449 return -EINVAL;
2450 }
2451
2452 if (!env_type_is_resolve_sink(env, next_type) &&
2453 !env_type_is_resolved(env, next_type_id))
2454 return env_stack_push(env, next_type, next_type_id);
2455
2456 if (btf_type_is_modifier(next_type)) {
2457 const struct btf_type *resolved_type;
2458 u32 resolved_type_id;
2459
2460 resolved_type_id = next_type_id;
2461 resolved_type = btf_type_id_resolve(btf, &resolved_type_id);
2462
2463 if (btf_type_is_ptr(resolved_type) &&
2464 !env_type_is_resolve_sink(env, resolved_type) &&
2465 !env_type_is_resolved(env, resolved_type_id))
2466 return env_stack_push(env, resolved_type,
2467 resolved_type_id);
2468 }
2469
2470 /* We must resolve to something concrete at this point, no
2471 * forward types or similar that would resolve to size of
2472 * zero is allowed.
2473 */
Andrii Nakryiko1acc5d52019-07-12 10:25:55 -07002474 if (!btf_type_id_size(btf, &next_type_id, NULL)) {
Daniel Borkmann1dc92852019-04-09 23:20:09 +02002475 btf_verifier_log_type(env, v->t, "Invalid type_id");
2476 return -EINVAL;
2477 }
2478
Andrii Nakryiko1acc5d52019-07-12 10:25:55 -07002479 env_stack_pop_resolved(env, next_type_id, 0);
Daniel Borkmann1dc92852019-04-09 23:20:09 +02002480
2481 return 0;
2482}
2483
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07002484static int btf_ptr_resolve(struct btf_verifier_env *env,
2485 const struct resolve_vertex *v)
2486{
2487 const struct btf_type *next_type;
2488 const struct btf_type *t = v->t;
2489 u32 next_type_id = t->type;
2490 struct btf *btf = env->btf;
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07002491
2492 next_type = btf_type_by_id(btf, next_type_id);
Daniel Borkmann1dc92852019-04-09 23:20:09 +02002493 if (!next_type || btf_type_is_resolve_source_only(next_type)) {
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07002494 btf_verifier_log_type(env, v->t, "Invalid type_id");
2495 return -EINVAL;
2496 }
2497
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07002498 if (!env_type_is_resolve_sink(env, next_type) &&
2499 !env_type_is_resolved(env, next_type_id))
2500 return env_stack_push(env, next_type, next_type_id);
2501
2502 /* If the modifier was RESOLVED during RESOLVE_STRUCT_OR_ARRAY,
2503 * the modifier may have stopped resolving when it was resolved
2504 * to a ptr (last-resolved-ptr).
2505 *
2506 * We now need to continue from the last-resolved-ptr to
2507 * ensure the last-resolved-ptr will not referring back to
2508 * the currenct ptr (t).
2509 */
2510 if (btf_type_is_modifier(next_type)) {
2511 const struct btf_type *resolved_type;
2512 u32 resolved_type_id;
2513
2514 resolved_type_id = next_type_id;
2515 resolved_type = btf_type_id_resolve(btf, &resolved_type_id);
2516
2517 if (btf_type_is_ptr(resolved_type) &&
2518 !env_type_is_resolve_sink(env, resolved_type) &&
2519 !env_type_is_resolved(env, resolved_type_id))
2520 return env_stack_push(env, resolved_type,
2521 resolved_type_id);
2522 }
2523
Martin KaFai Lau2667a262018-11-19 15:29:08 -08002524 if (!btf_type_id_size(btf, &next_type_id, NULL)) {
2525 if (env_type_is_resolved(env, next_type_id))
2526 next_type = btf_type_id_resolve(btf, &next_type_id);
2527
2528 if (!btf_type_is_void(next_type) &&
2529 !btf_type_is_fwd(next_type) &&
2530 !btf_type_is_func_proto(next_type)) {
2531 btf_verifier_log_type(env, v->t, "Invalid type_id");
2532 return -EINVAL;
2533 }
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07002534 }
2535
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07002536 env_stack_pop_resolved(env, next_type_id, 0);
2537
2538 return 0;
2539}
2540
Alan Maguire31d0bc82020-09-28 12:31:04 +01002541static void btf_modifier_show(const struct btf *btf,
2542 const struct btf_type *t,
2543 u32 type_id, void *data,
2544 u8 bits_offset, struct btf_show *show)
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07002545{
Martin KaFai Lau85d33df2020-01-08 16:35:05 -08002546 if (btf->resolved_ids)
2547 t = btf_type_id_resolve(btf, &type_id);
2548 else
2549 t = btf_type_skip_modifiers(btf, type_id, NULL);
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07002550
Alan Maguire31d0bc82020-09-28 12:31:04 +01002551 btf_type_ops(t)->show(btf, t, type_id, data, bits_offset, show);
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07002552}
2553
Alan Maguire31d0bc82020-09-28 12:31:04 +01002554static void btf_var_show(const struct btf *btf, const struct btf_type *t,
2555 u32 type_id, void *data, u8 bits_offset,
2556 struct btf_show *show)
Daniel Borkmann1dc92852019-04-09 23:20:09 +02002557{
2558 t = btf_type_id_resolve(btf, &type_id);
2559
Alan Maguire31d0bc82020-09-28 12:31:04 +01002560 btf_type_ops(t)->show(btf, t, type_id, data, bits_offset, show);
Daniel Borkmann1dc92852019-04-09 23:20:09 +02002561}
2562
Alan Maguire31d0bc82020-09-28 12:31:04 +01002563static void btf_ptr_show(const struct btf *btf, const struct btf_type *t,
2564 u32 type_id, void *data, u8 bits_offset,
2565 struct btf_show *show)
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07002566{
Alan Maguire31d0bc82020-09-28 12:31:04 +01002567 void *safe_data;
2568
2569 safe_data = btf_show_start_type(show, t, type_id, data);
2570 if (!safe_data)
2571 return;
2572
2573 /* It is a hashed value unless BTF_SHOW_PTR_RAW is specified */
2574 if (show->flags & BTF_SHOW_PTR_RAW)
2575 btf_show_type_value(show, "0x%px", *(void **)safe_data);
2576 else
2577 btf_show_type_value(show, "0x%p", *(void **)safe_data);
2578 btf_show_end_type(show);
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07002579}
2580
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002581static void btf_ref_type_log(struct btf_verifier_env *env,
2582 const struct btf_type *t)
2583{
2584 btf_verifier_log(env, "type_id=%u", t->type);
2585}
2586
2587static struct btf_kind_operations modifier_ops = {
2588 .check_meta = btf_ref_type_check_meta,
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07002589 .resolve = btf_modifier_resolve,
Martin KaFai Lau179cde82018-04-18 15:55:59 -07002590 .check_member = btf_modifier_check_member,
Yonghong Song9d5f9f72018-12-15 22:13:51 -08002591 .check_kflag_member = btf_modifier_check_kflag_member,
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002592 .log_details = btf_ref_type_log,
Alan Maguire31d0bc82020-09-28 12:31:04 +01002593 .show = btf_modifier_show,
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002594};
2595
2596static struct btf_kind_operations ptr_ops = {
2597 .check_meta = btf_ref_type_check_meta,
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07002598 .resolve = btf_ptr_resolve,
Martin KaFai Lau179cde82018-04-18 15:55:59 -07002599 .check_member = btf_ptr_check_member,
Yonghong Song9d5f9f72018-12-15 22:13:51 -08002600 .check_kflag_member = btf_generic_check_kflag_member,
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002601 .log_details = btf_ref_type_log,
Alan Maguire31d0bc82020-09-28 12:31:04 +01002602 .show = btf_ptr_show,
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002603};
2604
Martin KaFai Lau81753832018-06-02 09:06:51 -07002605static s32 btf_fwd_check_meta(struct btf_verifier_env *env,
2606 const struct btf_type *t,
2607 u32 meta_left)
2608{
2609 if (btf_type_vlen(t)) {
2610 btf_verifier_log_type(env, t, "vlen != 0");
2611 return -EINVAL;
2612 }
2613
2614 if (t->type) {
2615 btf_verifier_log_type(env, t, "type != 0");
2616 return -EINVAL;
2617 }
2618
Yonghong Songeb04bbb2018-11-27 13:23:28 -08002619 /* fwd type must have a valid name */
2620 if (!t->name_off ||
2621 !btf_name_valid_identifier(env->btf, t->name_off)) {
2622 btf_verifier_log_type(env, t, "Invalid name");
2623 return -EINVAL;
2624 }
2625
Martin KaFai Lau81753832018-06-02 09:06:51 -07002626 btf_verifier_log_type(env, t, NULL);
2627
2628 return 0;
2629}
2630
Yonghong Song76c43ae2018-12-18 13:43:58 -08002631static void btf_fwd_type_log(struct btf_verifier_env *env,
2632 const struct btf_type *t)
2633{
2634 btf_verifier_log(env, "%s", btf_type_kflag(t) ? "union" : "struct");
2635}
2636
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002637static struct btf_kind_operations fwd_ops = {
Martin KaFai Lau81753832018-06-02 09:06:51 -07002638 .check_meta = btf_fwd_check_meta,
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07002639 .resolve = btf_df_resolve,
Martin KaFai Lau179cde82018-04-18 15:55:59 -07002640 .check_member = btf_df_check_member,
Yonghong Song9d5f9f72018-12-15 22:13:51 -08002641 .check_kflag_member = btf_df_check_kflag_member,
Yonghong Song76c43ae2018-12-18 13:43:58 -08002642 .log_details = btf_fwd_type_log,
Alan Maguire31d0bc82020-09-28 12:31:04 +01002643 .show = btf_df_show,
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002644};
2645
Martin KaFai Lau179cde82018-04-18 15:55:59 -07002646static int btf_array_check_member(struct btf_verifier_env *env,
2647 const struct btf_type *struct_type,
2648 const struct btf_member *member,
2649 const struct btf_type *member_type)
2650{
2651 u32 struct_bits_off = member->offset;
2652 u32 struct_size, bytes_offset;
2653 u32 array_type_id, array_size;
2654 struct btf *btf = env->btf;
2655
2656 if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
2657 btf_verifier_log_member(env, struct_type, member,
2658 "Member is not byte aligned");
2659 return -EINVAL;
2660 }
2661
2662 array_type_id = member->type;
2663 btf_type_id_size(btf, &array_type_id, &array_size);
2664 struct_size = struct_type->size;
2665 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
2666 if (struct_size - bytes_offset < array_size) {
2667 btf_verifier_log_member(env, struct_type, member,
2668 "Member exceeds struct_size");
2669 return -EINVAL;
2670 }
2671
2672 return 0;
2673}
2674
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002675static s32 btf_array_check_meta(struct btf_verifier_env *env,
2676 const struct btf_type *t,
2677 u32 meta_left)
2678{
2679 const struct btf_array *array = btf_type_array(t);
2680 u32 meta_needed = sizeof(*array);
2681
2682 if (meta_left < meta_needed) {
2683 btf_verifier_log_basic(env, t,
2684 "meta_left:%u meta_needed:%u",
2685 meta_left, meta_needed);
2686 return -EINVAL;
2687 }
2688
Yonghong Songeb04bbb2018-11-27 13:23:28 -08002689 /* array type should not have a name */
2690 if (t->name_off) {
2691 btf_verifier_log_type(env, t, "Invalid name");
2692 return -EINVAL;
2693 }
2694
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002695 if (btf_type_vlen(t)) {
2696 btf_verifier_log_type(env, t, "vlen != 0");
2697 return -EINVAL;
2698 }
2699
Yonghong Song9d5f9f72018-12-15 22:13:51 -08002700 if (btf_type_kflag(t)) {
2701 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
2702 return -EINVAL;
2703 }
2704
Martin KaFai Laub9308ae2018-06-02 09:06:50 -07002705 if (t->size) {
2706 btf_verifier_log_type(env, t, "size != 0");
2707 return -EINVAL;
2708 }
2709
Martin KaFai Lau4ef5f572018-05-22 14:57:19 -07002710 /* Array elem type and index type cannot be in type void,
2711 * so !array->type and !array->index_type are not allowed.
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002712 */
Martin KaFai Lauaea2f7b82018-05-22 14:57:20 -07002713 if (!array->type || !BTF_TYPE_ID_VALID(array->type)) {
Martin KaFai Lau4ef5f572018-05-22 14:57:19 -07002714 btf_verifier_log_type(env, t, "Invalid elem");
2715 return -EINVAL;
2716 }
2717
Martin KaFai Lauaea2f7b82018-05-22 14:57:20 -07002718 if (!array->index_type || !BTF_TYPE_ID_VALID(array->index_type)) {
Martin KaFai Lau4ef5f572018-05-22 14:57:19 -07002719 btf_verifier_log_type(env, t, "Invalid index");
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002720 return -EINVAL;
2721 }
2722
2723 btf_verifier_log_type(env, t, NULL);
2724
2725 return meta_needed;
2726}
2727
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07002728static int btf_array_resolve(struct btf_verifier_env *env,
2729 const struct resolve_vertex *v)
2730{
2731 const struct btf_array *array = btf_type_array(v->t);
Martin KaFai Lau4ef5f572018-05-22 14:57:19 -07002732 const struct btf_type *elem_type, *index_type;
2733 u32 elem_type_id, index_type_id;
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07002734 struct btf *btf = env->btf;
2735 u32 elem_size;
2736
Martin KaFai Lau4ef5f572018-05-22 14:57:19 -07002737 /* Check array->index_type */
2738 index_type_id = array->index_type;
2739 index_type = btf_type_by_id(btf, index_type_id);
Stanislav Fomicheve4f07122019-06-19 12:01:05 -07002740 if (btf_type_nosize_or_null(index_type) ||
2741 btf_type_is_resolve_source_only(index_type)) {
Martin KaFai Lau4ef5f572018-05-22 14:57:19 -07002742 btf_verifier_log_type(env, v->t, "Invalid index");
2743 return -EINVAL;
2744 }
2745
2746 if (!env_type_is_resolve_sink(env, index_type) &&
2747 !env_type_is_resolved(env, index_type_id))
2748 return env_stack_push(env, index_type, index_type_id);
2749
2750 index_type = btf_type_id_size(btf, &index_type_id, NULL);
2751 if (!index_type || !btf_type_is_int(index_type) ||
2752 !btf_type_int_is_regular(index_type)) {
2753 btf_verifier_log_type(env, v->t, "Invalid index");
2754 return -EINVAL;
2755 }
2756
2757 /* Check array->type */
2758 elem_type_id = array->type;
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07002759 elem_type = btf_type_by_id(btf, elem_type_id);
Stanislav Fomicheve4f07122019-06-19 12:01:05 -07002760 if (btf_type_nosize_or_null(elem_type) ||
2761 btf_type_is_resolve_source_only(elem_type)) {
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07002762 btf_verifier_log_type(env, v->t,
2763 "Invalid elem");
2764 return -EINVAL;
2765 }
2766
2767 if (!env_type_is_resolve_sink(env, elem_type) &&
2768 !env_type_is_resolved(env, elem_type_id))
2769 return env_stack_push(env, elem_type, elem_type_id);
2770
2771 elem_type = btf_type_id_size(btf, &elem_type_id, &elem_size);
2772 if (!elem_type) {
2773 btf_verifier_log_type(env, v->t, "Invalid elem");
2774 return -EINVAL;
2775 }
2776
Martin KaFai Lau4ef5f572018-05-22 14:57:19 -07002777 if (btf_type_is_int(elem_type) && !btf_type_int_is_regular(elem_type)) {
2778 btf_verifier_log_type(env, v->t, "Invalid array of int");
2779 return -EINVAL;
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07002780 }
2781
2782 if (array->nelems && elem_size > U32_MAX / array->nelems) {
2783 btf_verifier_log_type(env, v->t,
2784 "Array size overflows U32_MAX");
2785 return -EINVAL;
2786 }
2787
2788 env_stack_pop_resolved(env, elem_type_id, elem_size * array->nelems);
2789
2790 return 0;
2791}
2792
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002793static void btf_array_log(struct btf_verifier_env *env,
2794 const struct btf_type *t)
2795{
2796 const struct btf_array *array = btf_type_array(t);
2797
2798 btf_verifier_log(env, "type_id=%u index_type_id=%u nr_elems=%u",
2799 array->type, array->index_type, array->nelems);
2800}
2801
Alan Maguire31d0bc82020-09-28 12:31:04 +01002802static void __btf_array_show(const struct btf *btf, const struct btf_type *t,
2803 u32 type_id, void *data, u8 bits_offset,
2804 struct btf_show *show)
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07002805{
2806 const struct btf_array *array = btf_type_array(t);
2807 const struct btf_kind_operations *elem_ops;
2808 const struct btf_type *elem_type;
Alan Maguire31d0bc82020-09-28 12:31:04 +01002809 u32 i, elem_size = 0, elem_type_id;
2810 u16 encoding = 0;
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07002811
2812 elem_type_id = array->type;
Alan Maguire31d0bc82020-09-28 12:31:04 +01002813 elem_type = btf_type_skip_modifiers(btf, elem_type_id, NULL);
2814 if (elem_type && btf_type_has_size(elem_type))
2815 elem_size = elem_type->size;
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07002816
Alan Maguire31d0bc82020-09-28 12:31:04 +01002817 if (elem_type && btf_type_is_int(elem_type)) {
2818 u32 int_type = btf_type_int(elem_type);
2819
2820 encoding = BTF_INT_ENCODING(int_type);
2821
2822 /*
2823 * BTF_INT_CHAR encoding never seems to be set for
2824 * char arrays, so if size is 1 and element is
2825 * printable as a char, we'll do that.
2826 */
2827 if (elem_size == 1)
2828 encoding = BTF_INT_CHAR;
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07002829 }
Alan Maguire31d0bc82020-09-28 12:31:04 +01002830
2831 if (!btf_show_start_array_type(show, t, type_id, encoding, data))
2832 return;
2833
2834 if (!elem_type)
2835 goto out;
2836 elem_ops = btf_type_ops(elem_type);
2837
2838 for (i = 0; i < array->nelems; i++) {
2839
2840 btf_show_start_array_member(show);
2841
2842 elem_ops->show(btf, elem_type, elem_type_id, data,
2843 bits_offset, show);
2844 data += elem_size;
2845
2846 btf_show_end_array_member(show);
2847
2848 if (show->state.array_terminated)
2849 break;
2850 }
2851out:
2852 btf_show_end_array_type(show);
2853}
2854
2855static void btf_array_show(const struct btf *btf, const struct btf_type *t,
2856 u32 type_id, void *data, u8 bits_offset,
2857 struct btf_show *show)
2858{
2859 const struct btf_member *m = show->state.member;
2860
2861 /*
2862 * First check if any members would be shown (are non-zero).
2863 * See comments above "struct btf_show" definition for more
2864 * details on how this works at a high-level.
2865 */
2866 if (show->state.depth > 0 && !(show->flags & BTF_SHOW_ZERO)) {
2867 if (!show->state.depth_check) {
2868 show->state.depth_check = show->state.depth + 1;
2869 show->state.depth_to_show = 0;
2870 }
2871 __btf_array_show(btf, t, type_id, data, bits_offset, show);
2872 show->state.member = m;
2873
2874 if (show->state.depth_check != show->state.depth + 1)
2875 return;
2876 show->state.depth_check = 0;
2877
2878 if (show->state.depth_to_show <= show->state.depth)
2879 return;
2880 /*
2881 * Reaching here indicates we have recursed and found
2882 * non-zero array member(s).
2883 */
2884 }
2885 __btf_array_show(btf, t, type_id, data, bits_offset, show);
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07002886}
2887
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002888static struct btf_kind_operations array_ops = {
2889 .check_meta = btf_array_check_meta,
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07002890 .resolve = btf_array_resolve,
Martin KaFai Lau179cde82018-04-18 15:55:59 -07002891 .check_member = btf_array_check_member,
Yonghong Song9d5f9f72018-12-15 22:13:51 -08002892 .check_kflag_member = btf_generic_check_kflag_member,
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002893 .log_details = btf_array_log,
Alan Maguire31d0bc82020-09-28 12:31:04 +01002894 .show = btf_array_show,
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002895};
2896
Martin KaFai Lau179cde82018-04-18 15:55:59 -07002897static int btf_struct_check_member(struct btf_verifier_env *env,
2898 const struct btf_type *struct_type,
2899 const struct btf_member *member,
2900 const struct btf_type *member_type)
2901{
2902 u32 struct_bits_off = member->offset;
2903 u32 struct_size, bytes_offset;
2904
2905 if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
2906 btf_verifier_log_member(env, struct_type, member,
2907 "Member is not byte aligned");
2908 return -EINVAL;
2909 }
2910
2911 struct_size = struct_type->size;
2912 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
2913 if (struct_size - bytes_offset < member_type->size) {
2914 btf_verifier_log_member(env, struct_type, member,
2915 "Member exceeds struct_size");
2916 return -EINVAL;
2917 }
2918
2919 return 0;
2920}
2921
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002922static s32 btf_struct_check_meta(struct btf_verifier_env *env,
2923 const struct btf_type *t,
2924 u32 meta_left)
2925{
2926 bool is_union = BTF_INFO_KIND(t->info) == BTF_KIND_UNION;
2927 const struct btf_member *member;
Martin KaFai Lau6283fa32018-07-20 17:38:37 -07002928 u32 meta_needed, last_offset;
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002929 struct btf *btf = env->btf;
2930 u32 struct_size = t->size;
Yonghong Song9d5f9f72018-12-15 22:13:51 -08002931 u32 offset;
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002932 u16 i;
2933
2934 meta_needed = btf_type_vlen(t) * sizeof(*member);
2935 if (meta_left < meta_needed) {
2936 btf_verifier_log_basic(env, t,
2937 "meta_left:%u meta_needed:%u",
2938 meta_left, meta_needed);
2939 return -EINVAL;
2940 }
2941
Yonghong Songeb04bbb2018-11-27 13:23:28 -08002942 /* struct type either no name or a valid one */
2943 if (t->name_off &&
2944 !btf_name_valid_identifier(env->btf, t->name_off)) {
2945 btf_verifier_log_type(env, t, "Invalid name");
2946 return -EINVAL;
2947 }
2948
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002949 btf_verifier_log_type(env, t, NULL);
2950
Martin KaFai Lau6283fa32018-07-20 17:38:37 -07002951 last_offset = 0;
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002952 for_each_member(i, t, member) {
Martin KaFai Laufbcf93e2018-04-21 09:48:23 -07002953 if (!btf_name_offset_valid(btf, member->name_off)) {
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002954 btf_verifier_log_member(env, t, member,
2955 "Invalid member name_offset:%u",
Martin KaFai Laufbcf93e2018-04-21 09:48:23 -07002956 member->name_off);
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002957 return -EINVAL;
2958 }
2959
Yonghong Songeb04bbb2018-11-27 13:23:28 -08002960 /* struct member either no name or a valid one */
2961 if (member->name_off &&
2962 !btf_name_valid_identifier(btf, member->name_off)) {
2963 btf_verifier_log_member(env, t, member, "Invalid name");
2964 return -EINVAL;
2965 }
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002966 /* A member cannot be in type void */
Martin KaFai Lauaea2f7b82018-05-22 14:57:20 -07002967 if (!member->type || !BTF_TYPE_ID_VALID(member->type)) {
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002968 btf_verifier_log_member(env, t, member,
2969 "Invalid type_id");
2970 return -EINVAL;
2971 }
2972
Alexei Starovoitov8293eb92021-12-01 10:10:25 -08002973 offset = __btf_member_bit_offset(t, member);
Yonghong Song9d5f9f72018-12-15 22:13:51 -08002974 if (is_union && offset) {
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002975 btf_verifier_log_member(env, t, member,
2976 "Invalid member bits_offset");
2977 return -EINVAL;
2978 }
2979
Martin KaFai Lau6283fa32018-07-20 17:38:37 -07002980 /*
2981 * ">" instead of ">=" because the last member could be
2982 * "char a[0];"
2983 */
Yonghong Song9d5f9f72018-12-15 22:13:51 -08002984 if (last_offset > offset) {
Martin KaFai Lau6283fa32018-07-20 17:38:37 -07002985 btf_verifier_log_member(env, t, member,
2986 "Invalid member bits_offset");
2987 return -EINVAL;
2988 }
2989
Yonghong Song9d5f9f72018-12-15 22:13:51 -08002990 if (BITS_ROUNDUP_BYTES(offset) > struct_size) {
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002991 btf_verifier_log_member(env, t, member,
Colin Ian King311fe1a2018-11-25 23:32:51 +00002992 "Member bits_offset exceeds its struct size");
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002993 return -EINVAL;
2994 }
2995
2996 btf_verifier_log_member(env, t, member, NULL);
Yonghong Song9d5f9f72018-12-15 22:13:51 -08002997 last_offset = offset;
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002998 }
2999
3000 return meta_needed;
3001}
3002
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07003003static int btf_struct_resolve(struct btf_verifier_env *env,
3004 const struct resolve_vertex *v)
3005{
3006 const struct btf_member *member;
Martin KaFai Lau179cde82018-04-18 15:55:59 -07003007 int err;
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07003008 u16 i;
3009
3010 /* Before continue resolving the next_member,
3011 * ensure the last member is indeed resolved to a
3012 * type with size info.
3013 */
3014 if (v->next_member) {
Martin KaFai Lau179cde82018-04-18 15:55:59 -07003015 const struct btf_type *last_member_type;
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07003016 const struct btf_member *last_member;
3017 u16 last_member_type_id;
3018
3019 last_member = btf_type_member(v->t) + v->next_member - 1;
3020 last_member_type_id = last_member->type;
3021 if (WARN_ON_ONCE(!env_type_is_resolved(env,
3022 last_member_type_id)))
3023 return -EINVAL;
Martin KaFai Lau179cde82018-04-18 15:55:59 -07003024
3025 last_member_type = btf_type_by_id(env->btf,
3026 last_member_type_id);
Yonghong Song9d5f9f72018-12-15 22:13:51 -08003027 if (btf_type_kflag(v->t))
3028 err = btf_type_ops(last_member_type)->check_kflag_member(env, v->t,
3029 last_member,
3030 last_member_type);
3031 else
3032 err = btf_type_ops(last_member_type)->check_member(env, v->t,
3033 last_member,
3034 last_member_type);
Martin KaFai Lau179cde82018-04-18 15:55:59 -07003035 if (err)
3036 return err;
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07003037 }
3038
3039 for_each_member_from(i, v->next_member, v->t, member) {
3040 u32 member_type_id = member->type;
3041 const struct btf_type *member_type = btf_type_by_id(env->btf,
3042 member_type_id);
3043
Stanislav Fomicheve4f07122019-06-19 12:01:05 -07003044 if (btf_type_nosize_or_null(member_type) ||
3045 btf_type_is_resolve_source_only(member_type)) {
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07003046 btf_verifier_log_member(env, v->t, member,
3047 "Invalid member");
3048 return -EINVAL;
3049 }
3050
3051 if (!env_type_is_resolve_sink(env, member_type) &&
3052 !env_type_is_resolved(env, member_type_id)) {
3053 env_stack_set_next_member(env, i + 1);
3054 return env_stack_push(env, member_type, member_type_id);
3055 }
Martin KaFai Lau179cde82018-04-18 15:55:59 -07003056
Yonghong Song9d5f9f72018-12-15 22:13:51 -08003057 if (btf_type_kflag(v->t))
3058 err = btf_type_ops(member_type)->check_kflag_member(env, v->t,
3059 member,
3060 member_type);
3061 else
3062 err = btf_type_ops(member_type)->check_member(env, v->t,
3063 member,
3064 member_type);
Martin KaFai Lau179cde82018-04-18 15:55:59 -07003065 if (err)
3066 return err;
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07003067 }
3068
3069 env_stack_pop_resolved(env, 0, 0);
3070
3071 return 0;
3072}
3073
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07003074static void btf_struct_log(struct btf_verifier_env *env,
3075 const struct btf_type *t)
3076{
3077 btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t));
3078}
3079
Alexei Starovoitov68134662021-07-14 17:54:10 -07003080static int btf_find_struct_field(const struct btf *btf, const struct btf_type *t,
3081 const char *name, int sz, int align)
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08003082{
3083 const struct btf_member *member;
3084 u32 i, off = -ENOENT;
3085
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08003086 for_each_member(i, t, member) {
3087 const struct btf_type *member_type = btf_type_by_id(btf,
3088 member->type);
3089 if (!__btf_type_is_struct(member_type))
3090 continue;
Alexei Starovoitov68134662021-07-14 17:54:10 -07003091 if (member_type->size != sz)
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08003092 continue;
Alexei Starovoitov68134662021-07-14 17:54:10 -07003093 if (strcmp(__btf_name_by_offset(btf, member_type->name_off), name))
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08003094 continue;
3095 if (off != -ENOENT)
Alexei Starovoitov68134662021-07-14 17:54:10 -07003096 /* only one such field is allowed */
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08003097 return -E2BIG;
Alexei Starovoitov8293eb92021-12-01 10:10:25 -08003098 off = __btf_member_bit_offset(t, member);
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08003099 if (off % 8)
3100 /* valid C code cannot generate such BTF */
3101 return -EINVAL;
3102 off /= 8;
Alexei Starovoitov68134662021-07-14 17:54:10 -07003103 if (off % align)
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08003104 return -EINVAL;
3105 }
3106 return off;
3107}
3108
Alexei Starovoitov68134662021-07-14 17:54:10 -07003109static int btf_find_datasec_var(const struct btf *btf, const struct btf_type *t,
3110 const char *name, int sz, int align)
3111{
3112 const struct btf_var_secinfo *vsi;
3113 u32 i, off = -ENOENT;
3114
3115 for_each_vsi(i, t, vsi) {
3116 const struct btf_type *var = btf_type_by_id(btf, vsi->type);
3117 const struct btf_type *var_type = btf_type_by_id(btf, var->type);
3118
3119 if (!__btf_type_is_struct(var_type))
3120 continue;
3121 if (var_type->size != sz)
3122 continue;
3123 if (vsi->size != sz)
3124 continue;
3125 if (strcmp(__btf_name_by_offset(btf, var_type->name_off), name))
3126 continue;
3127 if (off != -ENOENT)
3128 /* only one such field is allowed */
3129 return -E2BIG;
3130 off = vsi->offset;
3131 if (off % align)
3132 return -EINVAL;
3133 }
3134 return off;
3135}
3136
3137static int btf_find_field(const struct btf *btf, const struct btf_type *t,
3138 const char *name, int sz, int align)
3139{
3140
3141 if (__btf_type_is_struct(t))
3142 return btf_find_struct_field(btf, t, name, sz, align);
3143 else if (btf_type_is_datasec(t))
3144 return btf_find_datasec_var(btf, t, name, sz, align);
3145 return -EINVAL;
3146}
3147
3148/* find 'struct bpf_spin_lock' in map value.
3149 * return >= 0 offset if found
3150 * and < 0 in case of error
3151 */
3152int btf_find_spin_lock(const struct btf *btf, const struct btf_type *t)
3153{
3154 return btf_find_field(btf, t, "bpf_spin_lock",
3155 sizeof(struct bpf_spin_lock),
3156 __alignof__(struct bpf_spin_lock));
3157}
3158
3159int btf_find_timer(const struct btf *btf, const struct btf_type *t)
3160{
3161 return btf_find_field(btf, t, "bpf_timer",
3162 sizeof(struct bpf_timer),
3163 __alignof__(struct bpf_timer));
3164}
3165
Alan Maguire31d0bc82020-09-28 12:31:04 +01003166static void __btf_struct_show(const struct btf *btf, const struct btf_type *t,
3167 u32 type_id, void *data, u8 bits_offset,
3168 struct btf_show *show)
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07003169{
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07003170 const struct btf_member *member;
Alan Maguire31d0bc82020-09-28 12:31:04 +01003171 void *safe_data;
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07003172 u32 i;
3173
Alan Maguire31d0bc82020-09-28 12:31:04 +01003174 safe_data = btf_show_start_struct_type(show, t, type_id, data);
3175 if (!safe_data)
3176 return;
3177
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07003178 for_each_member(i, t, member) {
3179 const struct btf_type *member_type = btf_type_by_id(btf,
3180 member->type);
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07003181 const struct btf_kind_operations *ops;
Yonghong Song9d5f9f72018-12-15 22:13:51 -08003182 u32 member_offset, bitfield_size;
3183 u32 bytes_offset;
3184 u8 bits8_offset;
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07003185
Alan Maguire31d0bc82020-09-28 12:31:04 +01003186 btf_show_start_member(show, member);
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07003187
Alexei Starovoitov8293eb92021-12-01 10:10:25 -08003188 member_offset = __btf_member_bit_offset(t, member);
3189 bitfield_size = __btf_member_bitfield_size(t, member);
Yonghong Song17e3ac82019-01-10 11:14:00 -08003190 bytes_offset = BITS_ROUNDDOWN_BYTES(member_offset);
3191 bits8_offset = BITS_PER_BYTE_MASKED(member_offset);
Yonghong Song9d5f9f72018-12-15 22:13:51 -08003192 if (bitfield_size) {
Alan Maguire31d0bc82020-09-28 12:31:04 +01003193 safe_data = btf_show_start_type(show, member_type,
3194 member->type,
3195 data + bytes_offset);
3196 if (safe_data)
3197 btf_bitfield_show(safe_data,
3198 bits8_offset,
3199 bitfield_size, show);
3200 btf_show_end_type(show);
Yonghong Song9d5f9f72018-12-15 22:13:51 -08003201 } else {
Yonghong Song9d5f9f72018-12-15 22:13:51 -08003202 ops = btf_type_ops(member_type);
Alan Maguire31d0bc82020-09-28 12:31:04 +01003203 ops->show(btf, member_type, member->type,
3204 data + bytes_offset, bits8_offset, show);
Yonghong Song9d5f9f72018-12-15 22:13:51 -08003205 }
Alan Maguire31d0bc82020-09-28 12:31:04 +01003206
3207 btf_show_end_member(show);
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07003208 }
Alan Maguire31d0bc82020-09-28 12:31:04 +01003209
3210 btf_show_end_struct_type(show);
3211}
3212
3213static void btf_struct_show(const struct btf *btf, const struct btf_type *t,
3214 u32 type_id, void *data, u8 bits_offset,
3215 struct btf_show *show)
3216{
3217 const struct btf_member *m = show->state.member;
3218
3219 /*
3220 * First check if any members would be shown (are non-zero).
3221 * See comments above "struct btf_show" definition for more
3222 * details on how this works at a high-level.
3223 */
3224 if (show->state.depth > 0 && !(show->flags & BTF_SHOW_ZERO)) {
3225 if (!show->state.depth_check) {
3226 show->state.depth_check = show->state.depth + 1;
3227 show->state.depth_to_show = 0;
3228 }
3229 __btf_struct_show(btf, t, type_id, data, bits_offset, show);
3230 /* Restore saved member data here */
3231 show->state.member = m;
3232 if (show->state.depth_check != show->state.depth + 1)
3233 return;
3234 show->state.depth_check = 0;
3235
3236 if (show->state.depth_to_show <= show->state.depth)
3237 return;
3238 /*
3239 * Reaching here indicates we have recursed and found
3240 * non-zero child values.
3241 */
3242 }
3243
3244 __btf_struct_show(btf, t, type_id, data, bits_offset, show);
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07003245}
3246
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07003247static struct btf_kind_operations struct_ops = {
3248 .check_meta = btf_struct_check_meta,
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07003249 .resolve = btf_struct_resolve,
Martin KaFai Lau179cde82018-04-18 15:55:59 -07003250 .check_member = btf_struct_check_member,
Yonghong Song9d5f9f72018-12-15 22:13:51 -08003251 .check_kflag_member = btf_generic_check_kflag_member,
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07003252 .log_details = btf_struct_log,
Alan Maguire31d0bc82020-09-28 12:31:04 +01003253 .show = btf_struct_show,
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07003254};
3255
Martin KaFai Lau179cde82018-04-18 15:55:59 -07003256static int btf_enum_check_member(struct btf_verifier_env *env,
3257 const struct btf_type *struct_type,
3258 const struct btf_member *member,
3259 const struct btf_type *member_type)
3260{
3261 u32 struct_bits_off = member->offset;
3262 u32 struct_size, bytes_offset;
3263
3264 if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
3265 btf_verifier_log_member(env, struct_type, member,
3266 "Member is not byte aligned");
3267 return -EINVAL;
3268 }
3269
3270 struct_size = struct_type->size;
3271 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
Yoshiki Komachida6c7fa2020-03-10 16:32:29 +09003272 if (struct_size - bytes_offset < member_type->size) {
Martin KaFai Lau179cde82018-04-18 15:55:59 -07003273 btf_verifier_log_member(env, struct_type, member,
3274 "Member exceeds struct_size");
3275 return -EINVAL;
3276 }
3277
3278 return 0;
3279}
3280
Yonghong Song9d5f9f72018-12-15 22:13:51 -08003281static int btf_enum_check_kflag_member(struct btf_verifier_env *env,
3282 const struct btf_type *struct_type,
3283 const struct btf_member *member,
3284 const struct btf_type *member_type)
3285{
3286 u32 struct_bits_off, nr_bits, bytes_end, struct_size;
3287 u32 int_bitsize = sizeof(int) * BITS_PER_BYTE;
3288
3289 struct_bits_off = BTF_MEMBER_BIT_OFFSET(member->offset);
3290 nr_bits = BTF_MEMBER_BITFIELD_SIZE(member->offset);
3291 if (!nr_bits) {
3292 if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
3293 btf_verifier_log_member(env, struct_type, member,
3294 "Member is not byte aligned");
Colin Ian Kinge3439af2019-09-25 10:38:35 +01003295 return -EINVAL;
Yonghong Song9d5f9f72018-12-15 22:13:51 -08003296 }
3297
3298 nr_bits = int_bitsize;
3299 } else if (nr_bits > int_bitsize) {
3300 btf_verifier_log_member(env, struct_type, member,
3301 "Invalid member bitfield_size");
3302 return -EINVAL;
3303 }
3304
3305 struct_size = struct_type->size;
3306 bytes_end = BITS_ROUNDUP_BYTES(struct_bits_off + nr_bits);
3307 if (struct_size < bytes_end) {
3308 btf_verifier_log_member(env, struct_type, member,
3309 "Member exceeds struct_size");
3310 return -EINVAL;
3311 }
3312
3313 return 0;
3314}
3315
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07003316static s32 btf_enum_check_meta(struct btf_verifier_env *env,
3317 const struct btf_type *t,
3318 u32 meta_left)
3319{
3320 const struct btf_enum *enums = btf_type_enum(t);
3321 struct btf *btf = env->btf;
3322 u16 i, nr_enums;
3323 u32 meta_needed;
3324
3325 nr_enums = btf_type_vlen(t);
3326 meta_needed = nr_enums * sizeof(*enums);
3327
3328 if (meta_left < meta_needed) {
3329 btf_verifier_log_basic(env, t,
3330 "meta_left:%u meta_needed:%u",
3331 meta_left, meta_needed);
3332 return -EINVAL;
3333 }
3334
Yonghong Song9d5f9f72018-12-15 22:13:51 -08003335 if (btf_type_kflag(t)) {
3336 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
3337 return -EINVAL;
3338 }
3339
Alexei Starovoitov9eea9842019-09-17 10:45:37 -07003340 if (t->size > 8 || !is_power_of_2(t->size)) {
3341 btf_verifier_log_type(env, t, "Unexpected size");
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07003342 return -EINVAL;
3343 }
3344
Yonghong Songeb04bbb2018-11-27 13:23:28 -08003345 /* enum type either no name or a valid one */
3346 if (t->name_off &&
3347 !btf_name_valid_identifier(env->btf, t->name_off)) {
3348 btf_verifier_log_type(env, t, "Invalid name");
3349 return -EINVAL;
3350 }
3351
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07003352 btf_verifier_log_type(env, t, NULL);
3353
3354 for (i = 0; i < nr_enums; i++) {
Martin KaFai Laufbcf93e2018-04-21 09:48:23 -07003355 if (!btf_name_offset_valid(btf, enums[i].name_off)) {
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07003356 btf_verifier_log(env, "\tInvalid name_offset:%u",
Martin KaFai Laufbcf93e2018-04-21 09:48:23 -07003357 enums[i].name_off);
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07003358 return -EINVAL;
3359 }
3360
Yonghong Songeb04bbb2018-11-27 13:23:28 -08003361 /* enum member must have a valid name */
3362 if (!enums[i].name_off ||
3363 !btf_name_valid_identifier(btf, enums[i].name_off)) {
3364 btf_verifier_log_type(env, t, "Invalid name");
3365 return -EINVAL;
3366 }
3367
Alexei Starovoitov8580ac92019-10-15 20:24:57 -07003368 if (env->log.level == BPF_LOG_KERNEL)
3369 continue;
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07003370 btf_verifier_log(env, "\t%s val=%d\n",
Martin KaFai Lau23127b32018-12-13 10:41:46 -08003371 __btf_name_by_offset(btf, enums[i].name_off),
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07003372 enums[i].val);
3373 }
3374
3375 return meta_needed;
3376}
3377
3378static void btf_enum_log(struct btf_verifier_env *env,
3379 const struct btf_type *t)
3380{
3381 btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t));
3382}
3383
Alan Maguire31d0bc82020-09-28 12:31:04 +01003384static void btf_enum_show(const struct btf *btf, const struct btf_type *t,
3385 u32 type_id, void *data, u8 bits_offset,
3386 struct btf_show *show)
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07003387{
3388 const struct btf_enum *enums = btf_type_enum(t);
3389 u32 i, nr_enums = btf_type_vlen(t);
Alan Maguire31d0bc82020-09-28 12:31:04 +01003390 void *safe_data;
3391 int v;
3392
3393 safe_data = btf_show_start_type(show, t, type_id, data);
3394 if (!safe_data)
3395 return;
3396
3397 v = *(int *)safe_data;
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07003398
3399 for (i = 0; i < nr_enums; i++) {
Alan Maguire31d0bc82020-09-28 12:31:04 +01003400 if (v != enums[i].val)
3401 continue;
3402
3403 btf_show_type_value(show, "%s",
3404 __btf_name_by_offset(btf,
3405 enums[i].name_off));
3406
3407 btf_show_end_type(show);
3408 return;
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07003409 }
3410
Alan Maguire31d0bc82020-09-28 12:31:04 +01003411 btf_show_type_value(show, "%d", v);
3412 btf_show_end_type(show);
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07003413}
3414
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07003415static struct btf_kind_operations enum_ops = {
3416 .check_meta = btf_enum_check_meta,
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07003417 .resolve = btf_df_resolve,
Martin KaFai Lau179cde82018-04-18 15:55:59 -07003418 .check_member = btf_enum_check_member,
Yonghong Song9d5f9f72018-12-15 22:13:51 -08003419 .check_kflag_member = btf_enum_check_kflag_member,
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07003420 .log_details = btf_enum_log,
Alan Maguire31d0bc82020-09-28 12:31:04 +01003421 .show = btf_enum_show,
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07003422};
3423
Martin KaFai Lau2667a262018-11-19 15:29:08 -08003424static s32 btf_func_proto_check_meta(struct btf_verifier_env *env,
3425 const struct btf_type *t,
3426 u32 meta_left)
3427{
3428 u32 meta_needed = btf_type_vlen(t) * sizeof(struct btf_param);
3429
3430 if (meta_left < meta_needed) {
3431 btf_verifier_log_basic(env, t,
3432 "meta_left:%u meta_needed:%u",
3433 meta_left, meta_needed);
3434 return -EINVAL;
3435 }
3436
3437 if (t->name_off) {
3438 btf_verifier_log_type(env, t, "Invalid name");
3439 return -EINVAL;
3440 }
3441
Yonghong Song9d5f9f72018-12-15 22:13:51 -08003442 if (btf_type_kflag(t)) {
3443 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
3444 return -EINVAL;
3445 }
3446
Martin KaFai Lau2667a262018-11-19 15:29:08 -08003447 btf_verifier_log_type(env, t, NULL);
3448
3449 return meta_needed;
3450}
3451
3452static void btf_func_proto_log(struct btf_verifier_env *env,
3453 const struct btf_type *t)
3454{
3455 const struct btf_param *args = (const struct btf_param *)(t + 1);
3456 u16 nr_args = btf_type_vlen(t), i;
3457
3458 btf_verifier_log(env, "return=%u args=(", t->type);
3459 if (!nr_args) {
3460 btf_verifier_log(env, "void");
3461 goto done;
3462 }
3463
3464 if (nr_args == 1 && !args[0].type) {
3465 /* Only one vararg */
3466 btf_verifier_log(env, "vararg");
3467 goto done;
3468 }
3469
3470 btf_verifier_log(env, "%u %s", args[0].type,
Martin KaFai Lau23127b32018-12-13 10:41:46 -08003471 __btf_name_by_offset(env->btf,
3472 args[0].name_off));
Martin KaFai Lau2667a262018-11-19 15:29:08 -08003473 for (i = 1; i < nr_args - 1; i++)
3474 btf_verifier_log(env, ", %u %s", args[i].type,
Martin KaFai Lau23127b32018-12-13 10:41:46 -08003475 __btf_name_by_offset(env->btf,
3476 args[i].name_off));
Martin KaFai Lau2667a262018-11-19 15:29:08 -08003477
3478 if (nr_args > 1) {
3479 const struct btf_param *last_arg = &args[nr_args - 1];
3480
3481 if (last_arg->type)
3482 btf_verifier_log(env, ", %u %s", last_arg->type,
Martin KaFai Lau23127b32018-12-13 10:41:46 -08003483 __btf_name_by_offset(env->btf,
3484 last_arg->name_off));
Martin KaFai Lau2667a262018-11-19 15:29:08 -08003485 else
3486 btf_verifier_log(env, ", vararg");
3487 }
3488
3489done:
3490 btf_verifier_log(env, ")");
3491}
3492
3493static struct btf_kind_operations func_proto_ops = {
3494 .check_meta = btf_func_proto_check_meta,
3495 .resolve = btf_df_resolve,
3496 /*
3497 * BTF_KIND_FUNC_PROTO cannot be directly referred by
3498 * a struct's member.
3499 *
Zhen Lei8fb33b62021-05-25 10:56:59 +08003500 * It should be a function pointer instead.
Martin KaFai Lau2667a262018-11-19 15:29:08 -08003501 * (i.e. struct's member -> BTF_KIND_PTR -> BTF_KIND_FUNC_PROTO)
3502 *
3503 * Hence, there is no btf_func_check_member().
3504 */
3505 .check_member = btf_df_check_member,
Yonghong Song9d5f9f72018-12-15 22:13:51 -08003506 .check_kflag_member = btf_df_check_kflag_member,
Martin KaFai Lau2667a262018-11-19 15:29:08 -08003507 .log_details = btf_func_proto_log,
Alan Maguire31d0bc82020-09-28 12:31:04 +01003508 .show = btf_df_show,
Martin KaFai Lau2667a262018-11-19 15:29:08 -08003509};
3510
3511static s32 btf_func_check_meta(struct btf_verifier_env *env,
3512 const struct btf_type *t,
3513 u32 meta_left)
3514{
3515 if (!t->name_off ||
3516 !btf_name_valid_identifier(env->btf, t->name_off)) {
3517 btf_verifier_log_type(env, t, "Invalid name");
3518 return -EINVAL;
3519 }
3520
Alexei Starovoitov51c39bb2020-01-09 22:41:20 -08003521 if (btf_type_vlen(t) > BTF_FUNC_GLOBAL) {
3522 btf_verifier_log_type(env, t, "Invalid func linkage");
Martin KaFai Lau2667a262018-11-19 15:29:08 -08003523 return -EINVAL;
3524 }
3525
Yonghong Song9d5f9f72018-12-15 22:13:51 -08003526 if (btf_type_kflag(t)) {
3527 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
3528 return -EINVAL;
3529 }
3530
Martin KaFai Lau2667a262018-11-19 15:29:08 -08003531 btf_verifier_log_type(env, t, NULL);
3532
3533 return 0;
3534}
3535
3536static struct btf_kind_operations func_ops = {
3537 .check_meta = btf_func_check_meta,
3538 .resolve = btf_df_resolve,
3539 .check_member = btf_df_check_member,
Yonghong Song9d5f9f72018-12-15 22:13:51 -08003540 .check_kflag_member = btf_df_check_kflag_member,
Martin KaFai Lau2667a262018-11-19 15:29:08 -08003541 .log_details = btf_ref_type_log,
Alan Maguire31d0bc82020-09-28 12:31:04 +01003542 .show = btf_df_show,
Martin KaFai Lau2667a262018-11-19 15:29:08 -08003543};
3544
Daniel Borkmann1dc92852019-04-09 23:20:09 +02003545static s32 btf_var_check_meta(struct btf_verifier_env *env,
3546 const struct btf_type *t,
3547 u32 meta_left)
3548{
3549 const struct btf_var *var;
3550 u32 meta_needed = sizeof(*var);
3551
3552 if (meta_left < meta_needed) {
3553 btf_verifier_log_basic(env, t,
3554 "meta_left:%u meta_needed:%u",
3555 meta_left, meta_needed);
3556 return -EINVAL;
3557 }
3558
3559 if (btf_type_vlen(t)) {
3560 btf_verifier_log_type(env, t, "vlen != 0");
3561 return -EINVAL;
3562 }
3563
3564 if (btf_type_kflag(t)) {
3565 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
3566 return -EINVAL;
3567 }
3568
3569 if (!t->name_off ||
3570 !__btf_name_valid(env->btf, t->name_off, true)) {
3571 btf_verifier_log_type(env, t, "Invalid name");
3572 return -EINVAL;
3573 }
3574
3575 /* A var cannot be in type void */
3576 if (!t->type || !BTF_TYPE_ID_VALID(t->type)) {
3577 btf_verifier_log_type(env, t, "Invalid type_id");
3578 return -EINVAL;
3579 }
3580
3581 var = btf_type_var(t);
3582 if (var->linkage != BTF_VAR_STATIC &&
3583 var->linkage != BTF_VAR_GLOBAL_ALLOCATED) {
3584 btf_verifier_log_type(env, t, "Linkage not supported");
3585 return -EINVAL;
3586 }
3587
3588 btf_verifier_log_type(env, t, NULL);
3589
3590 return meta_needed;
3591}
3592
3593static void btf_var_log(struct btf_verifier_env *env, const struct btf_type *t)
3594{
3595 const struct btf_var *var = btf_type_var(t);
3596
3597 btf_verifier_log(env, "type_id=%u linkage=%u", t->type, var->linkage);
3598}
3599
3600static const struct btf_kind_operations var_ops = {
3601 .check_meta = btf_var_check_meta,
3602 .resolve = btf_var_resolve,
3603 .check_member = btf_df_check_member,
3604 .check_kflag_member = btf_df_check_kflag_member,
3605 .log_details = btf_var_log,
Alan Maguire31d0bc82020-09-28 12:31:04 +01003606 .show = btf_var_show,
Daniel Borkmann1dc92852019-04-09 23:20:09 +02003607};
3608
3609static s32 btf_datasec_check_meta(struct btf_verifier_env *env,
3610 const struct btf_type *t,
3611 u32 meta_left)
3612{
3613 const struct btf_var_secinfo *vsi;
3614 u64 last_vsi_end_off = 0, sum = 0;
3615 u32 i, meta_needed;
3616
3617 meta_needed = btf_type_vlen(t) * sizeof(*vsi);
3618 if (meta_left < meta_needed) {
3619 btf_verifier_log_basic(env, t,
3620 "meta_left:%u meta_needed:%u",
3621 meta_left, meta_needed);
3622 return -EINVAL;
3623 }
3624
Daniel Borkmann1dc92852019-04-09 23:20:09 +02003625 if (!t->size) {
3626 btf_verifier_log_type(env, t, "size == 0");
3627 return -EINVAL;
3628 }
3629
3630 if (btf_type_kflag(t)) {
3631 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
3632 return -EINVAL;
3633 }
3634
3635 if (!t->name_off ||
3636 !btf_name_valid_section(env->btf, t->name_off)) {
3637 btf_verifier_log_type(env, t, "Invalid name");
3638 return -EINVAL;
3639 }
3640
3641 btf_verifier_log_type(env, t, NULL);
3642
3643 for_each_vsi(i, t, vsi) {
3644 /* A var cannot be in type void */
3645 if (!vsi->type || !BTF_TYPE_ID_VALID(vsi->type)) {
3646 btf_verifier_log_vsi(env, t, vsi,
3647 "Invalid type_id");
3648 return -EINVAL;
3649 }
3650
3651 if (vsi->offset < last_vsi_end_off || vsi->offset >= t->size) {
3652 btf_verifier_log_vsi(env, t, vsi,
3653 "Invalid offset");
3654 return -EINVAL;
3655 }
3656
3657 if (!vsi->size || vsi->size > t->size) {
3658 btf_verifier_log_vsi(env, t, vsi,
3659 "Invalid size");
3660 return -EINVAL;
3661 }
3662
3663 last_vsi_end_off = vsi->offset + vsi->size;
3664 if (last_vsi_end_off > t->size) {
3665 btf_verifier_log_vsi(env, t, vsi,
3666 "Invalid offset+size");
3667 return -EINVAL;
3668 }
3669
3670 btf_verifier_log_vsi(env, t, vsi, NULL);
3671 sum += vsi->size;
3672 }
3673
3674 if (t->size < sum) {
3675 btf_verifier_log_type(env, t, "Invalid btf_info size");
3676 return -EINVAL;
3677 }
3678
3679 return meta_needed;
3680}
3681
3682static int btf_datasec_resolve(struct btf_verifier_env *env,
3683 const struct resolve_vertex *v)
3684{
3685 const struct btf_var_secinfo *vsi;
3686 struct btf *btf = env->btf;
3687 u16 i;
3688
3689 for_each_vsi_from(i, v->next_member, v->t, vsi) {
3690 u32 var_type_id = vsi->type, type_id, type_size = 0;
3691 const struct btf_type *var_type = btf_type_by_id(env->btf,
3692 var_type_id);
3693 if (!var_type || !btf_type_is_var(var_type)) {
3694 btf_verifier_log_vsi(env, v->t, vsi,
3695 "Not a VAR kind member");
3696 return -EINVAL;
3697 }
3698
3699 if (!env_type_is_resolve_sink(env, var_type) &&
3700 !env_type_is_resolved(env, var_type_id)) {
3701 env_stack_set_next_member(env, i + 1);
3702 return env_stack_push(env, var_type, var_type_id);
3703 }
3704
3705 type_id = var_type->type;
3706 if (!btf_type_id_size(btf, &type_id, &type_size)) {
3707 btf_verifier_log_vsi(env, v->t, vsi, "Invalid type");
3708 return -EINVAL;
3709 }
3710
3711 if (vsi->size < type_size) {
3712 btf_verifier_log_vsi(env, v->t, vsi, "Invalid size");
3713 return -EINVAL;
3714 }
3715 }
3716
3717 env_stack_pop_resolved(env, 0, 0);
3718 return 0;
3719}
3720
3721static void btf_datasec_log(struct btf_verifier_env *env,
3722 const struct btf_type *t)
3723{
3724 btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t));
3725}
3726
Alan Maguire31d0bc82020-09-28 12:31:04 +01003727static void btf_datasec_show(const struct btf *btf,
3728 const struct btf_type *t, u32 type_id,
3729 void *data, u8 bits_offset,
3730 struct btf_show *show)
Daniel Borkmann1dc92852019-04-09 23:20:09 +02003731{
3732 const struct btf_var_secinfo *vsi;
3733 const struct btf_type *var;
3734 u32 i;
3735
Alan Maguire31d0bc82020-09-28 12:31:04 +01003736 if (!btf_show_start_type(show, t, type_id, data))
3737 return;
3738
3739 btf_show_type_value(show, "section (\"%s\") = {",
3740 __btf_name_by_offset(btf, t->name_off));
Daniel Borkmann1dc92852019-04-09 23:20:09 +02003741 for_each_vsi(i, t, vsi) {
3742 var = btf_type_by_id(btf, vsi->type);
3743 if (i)
Alan Maguire31d0bc82020-09-28 12:31:04 +01003744 btf_show(show, ",");
3745 btf_type_ops(var)->show(btf, var, vsi->type,
3746 data + vsi->offset, bits_offset, show);
Daniel Borkmann1dc92852019-04-09 23:20:09 +02003747 }
Alan Maguire31d0bc82020-09-28 12:31:04 +01003748 btf_show_end_type(show);
Daniel Borkmann1dc92852019-04-09 23:20:09 +02003749}
3750
3751static const struct btf_kind_operations datasec_ops = {
3752 .check_meta = btf_datasec_check_meta,
3753 .resolve = btf_datasec_resolve,
3754 .check_member = btf_df_check_member,
3755 .check_kflag_member = btf_df_check_kflag_member,
3756 .log_details = btf_datasec_log,
Alan Maguire31d0bc82020-09-28 12:31:04 +01003757 .show = btf_datasec_show,
Daniel Borkmann1dc92852019-04-09 23:20:09 +02003758};
3759
Ilya Leoshkevichb1828f02021-02-26 21:22:52 +01003760static s32 btf_float_check_meta(struct btf_verifier_env *env,
3761 const struct btf_type *t,
3762 u32 meta_left)
3763{
3764 if (btf_type_vlen(t)) {
3765 btf_verifier_log_type(env, t, "vlen != 0");
3766 return -EINVAL;
3767 }
3768
3769 if (btf_type_kflag(t)) {
3770 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
3771 return -EINVAL;
3772 }
3773
3774 if (t->size != 2 && t->size != 4 && t->size != 8 && t->size != 12 &&
3775 t->size != 16) {
3776 btf_verifier_log_type(env, t, "Invalid type_size");
3777 return -EINVAL;
3778 }
3779
3780 btf_verifier_log_type(env, t, NULL);
3781
3782 return 0;
3783}
3784
3785static int btf_float_check_member(struct btf_verifier_env *env,
3786 const struct btf_type *struct_type,
3787 const struct btf_member *member,
3788 const struct btf_type *member_type)
3789{
3790 u64 start_offset_bytes;
3791 u64 end_offset_bytes;
3792 u64 misalign_bits;
3793 u64 align_bytes;
3794 u64 align_bits;
3795
3796 /* Different architectures have different alignment requirements, so
3797 * here we check only for the reasonable minimum. This way we ensure
3798 * that types after CO-RE can pass the kernel BTF verifier.
3799 */
3800 align_bytes = min_t(u64, sizeof(void *), member_type->size);
3801 align_bits = align_bytes * BITS_PER_BYTE;
3802 div64_u64_rem(member->offset, align_bits, &misalign_bits);
3803 if (misalign_bits) {
3804 btf_verifier_log_member(env, struct_type, member,
3805 "Member is not properly aligned");
3806 return -EINVAL;
3807 }
3808
3809 start_offset_bytes = member->offset / BITS_PER_BYTE;
3810 end_offset_bytes = start_offset_bytes + member_type->size;
3811 if (end_offset_bytes > struct_type->size) {
3812 btf_verifier_log_member(env, struct_type, member,
3813 "Member exceeds struct_size");
3814 return -EINVAL;
3815 }
3816
3817 return 0;
3818}
3819
3820static void btf_float_log(struct btf_verifier_env *env,
3821 const struct btf_type *t)
3822{
3823 btf_verifier_log(env, "size=%u", t->size);
3824}
3825
3826static const struct btf_kind_operations float_ops = {
3827 .check_meta = btf_float_check_meta,
3828 .resolve = btf_df_resolve,
3829 .check_member = btf_float_check_member,
3830 .check_kflag_member = btf_generic_check_kflag_member,
3831 .log_details = btf_float_log,
3832 .show = btf_df_show,
3833};
3834
Yonghong Song223f9032021-10-12 09:48:38 -07003835static s32 btf_decl_tag_check_meta(struct btf_verifier_env *env,
Yonghong Songb5ea8342021-09-14 15:30:15 -07003836 const struct btf_type *t,
3837 u32 meta_left)
3838{
Yonghong Song223f9032021-10-12 09:48:38 -07003839 const struct btf_decl_tag *tag;
Yonghong Songb5ea8342021-09-14 15:30:15 -07003840 u32 meta_needed = sizeof(*tag);
3841 s32 component_idx;
3842 const char *value;
3843
3844 if (meta_left < meta_needed) {
3845 btf_verifier_log_basic(env, t,
3846 "meta_left:%u meta_needed:%u",
3847 meta_left, meta_needed);
3848 return -EINVAL;
3849 }
3850
3851 value = btf_name_by_offset(env->btf, t->name_off);
3852 if (!value || !value[0]) {
3853 btf_verifier_log_type(env, t, "Invalid value");
3854 return -EINVAL;
3855 }
3856
3857 if (btf_type_vlen(t)) {
3858 btf_verifier_log_type(env, t, "vlen != 0");
3859 return -EINVAL;
3860 }
3861
3862 if (btf_type_kflag(t)) {
3863 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
3864 return -EINVAL;
3865 }
3866
Yonghong Song223f9032021-10-12 09:48:38 -07003867 component_idx = btf_type_decl_tag(t)->component_idx;
Yonghong Songb5ea8342021-09-14 15:30:15 -07003868 if (component_idx < -1) {
3869 btf_verifier_log_type(env, t, "Invalid component_idx");
3870 return -EINVAL;
3871 }
3872
3873 btf_verifier_log_type(env, t, NULL);
3874
3875 return meta_needed;
3876}
3877
Yonghong Song223f9032021-10-12 09:48:38 -07003878static int btf_decl_tag_resolve(struct btf_verifier_env *env,
Yonghong Songb5ea8342021-09-14 15:30:15 -07003879 const struct resolve_vertex *v)
3880{
3881 const struct btf_type *next_type;
3882 const struct btf_type *t = v->t;
3883 u32 next_type_id = t->type;
3884 struct btf *btf = env->btf;
3885 s32 component_idx;
3886 u32 vlen;
3887
3888 next_type = btf_type_by_id(btf, next_type_id);
Yonghong Song223f9032021-10-12 09:48:38 -07003889 if (!next_type || !btf_type_is_decl_tag_target(next_type)) {
Yonghong Songb5ea8342021-09-14 15:30:15 -07003890 btf_verifier_log_type(env, v->t, "Invalid type_id");
3891 return -EINVAL;
3892 }
3893
3894 if (!env_type_is_resolve_sink(env, next_type) &&
3895 !env_type_is_resolved(env, next_type_id))
3896 return env_stack_push(env, next_type, next_type_id);
3897
Yonghong Song223f9032021-10-12 09:48:38 -07003898 component_idx = btf_type_decl_tag(t)->component_idx;
Yonghong Songb5ea8342021-09-14 15:30:15 -07003899 if (component_idx != -1) {
Yonghong Songbd16dee2021-10-21 12:56:28 -07003900 if (btf_type_is_var(next_type) || btf_type_is_typedef(next_type)) {
Yonghong Songb5ea8342021-09-14 15:30:15 -07003901 btf_verifier_log_type(env, v->t, "Invalid component_idx");
3902 return -EINVAL;
3903 }
3904
3905 if (btf_type_is_struct(next_type)) {
3906 vlen = btf_type_vlen(next_type);
3907 } else {
3908 /* next_type should be a function */
3909 next_type = btf_type_by_id(btf, next_type->type);
3910 vlen = btf_type_vlen(next_type);
3911 }
3912
3913 if ((u32)component_idx >= vlen) {
3914 btf_verifier_log_type(env, v->t, "Invalid component_idx");
3915 return -EINVAL;
3916 }
3917 }
3918
3919 env_stack_pop_resolved(env, next_type_id, 0);
3920
3921 return 0;
3922}
3923
Yonghong Song223f9032021-10-12 09:48:38 -07003924static void btf_decl_tag_log(struct btf_verifier_env *env, const struct btf_type *t)
Yonghong Songb5ea8342021-09-14 15:30:15 -07003925{
3926 btf_verifier_log(env, "type=%u component_idx=%d", t->type,
Yonghong Song223f9032021-10-12 09:48:38 -07003927 btf_type_decl_tag(t)->component_idx);
Yonghong Songb5ea8342021-09-14 15:30:15 -07003928}
3929
Yonghong Song223f9032021-10-12 09:48:38 -07003930static const struct btf_kind_operations decl_tag_ops = {
3931 .check_meta = btf_decl_tag_check_meta,
3932 .resolve = btf_decl_tag_resolve,
Yonghong Songb5ea8342021-09-14 15:30:15 -07003933 .check_member = btf_df_check_member,
3934 .check_kflag_member = btf_df_check_kflag_member,
Yonghong Song223f9032021-10-12 09:48:38 -07003935 .log_details = btf_decl_tag_log,
Yonghong Songb5ea8342021-09-14 15:30:15 -07003936 .show = btf_df_show,
3937};
3938
Martin KaFai Lau2667a262018-11-19 15:29:08 -08003939static int btf_func_proto_check(struct btf_verifier_env *env,
3940 const struct btf_type *t)
3941{
3942 const struct btf_type *ret_type;
3943 const struct btf_param *args;
3944 const struct btf *btf;
3945 u16 nr_args, i;
3946 int err;
3947
3948 btf = env->btf;
3949 args = (const struct btf_param *)(t + 1);
3950 nr_args = btf_type_vlen(t);
3951
3952 /* Check func return type which could be "void" (t->type == 0) */
3953 if (t->type) {
3954 u32 ret_type_id = t->type;
3955
3956 ret_type = btf_type_by_id(btf, ret_type_id);
3957 if (!ret_type) {
3958 btf_verifier_log_type(env, t, "Invalid return type");
3959 return -EINVAL;
3960 }
3961
3962 if (btf_type_needs_resolve(ret_type) &&
3963 !env_type_is_resolved(env, ret_type_id)) {
3964 err = btf_resolve(env, ret_type, ret_type_id);
3965 if (err)
3966 return err;
3967 }
3968
3969 /* Ensure the return type is a type that has a size */
3970 if (!btf_type_id_size(btf, &ret_type_id, NULL)) {
3971 btf_verifier_log_type(env, t, "Invalid return type");
3972 return -EINVAL;
3973 }
3974 }
3975
3976 if (!nr_args)
3977 return 0;
3978
3979 /* Last func arg type_id could be 0 if it is a vararg */
3980 if (!args[nr_args - 1].type) {
3981 if (args[nr_args - 1].name_off) {
3982 btf_verifier_log_type(env, t, "Invalid arg#%u",
3983 nr_args);
3984 return -EINVAL;
3985 }
3986 nr_args--;
3987 }
3988
3989 err = 0;
3990 for (i = 0; i < nr_args; i++) {
3991 const struct btf_type *arg_type;
3992 u32 arg_type_id;
3993
3994 arg_type_id = args[i].type;
3995 arg_type = btf_type_by_id(btf, arg_type_id);
3996 if (!arg_type) {
3997 btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1);
3998 err = -EINVAL;
3999 break;
4000 }
4001
4002 if (args[i].name_off &&
4003 (!btf_name_offset_valid(btf, args[i].name_off) ||
4004 !btf_name_valid_identifier(btf, args[i].name_off))) {
4005 btf_verifier_log_type(env, t,
4006 "Invalid arg#%u", i + 1);
4007 err = -EINVAL;
4008 break;
4009 }
4010
4011 if (btf_type_needs_resolve(arg_type) &&
4012 !env_type_is_resolved(env, arg_type_id)) {
4013 err = btf_resolve(env, arg_type, arg_type_id);
4014 if (err)
4015 break;
4016 }
4017
4018 if (!btf_type_id_size(btf, &arg_type_id, NULL)) {
4019 btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1);
4020 err = -EINVAL;
4021 break;
4022 }
4023 }
4024
4025 return err;
4026}
4027
4028static int btf_func_check(struct btf_verifier_env *env,
4029 const struct btf_type *t)
4030{
4031 const struct btf_type *proto_type;
4032 const struct btf_param *args;
4033 const struct btf *btf;
4034 u16 nr_args, i;
4035
4036 btf = env->btf;
4037 proto_type = btf_type_by_id(btf, t->type);
4038
4039 if (!proto_type || !btf_type_is_func_proto(proto_type)) {
4040 btf_verifier_log_type(env, t, "Invalid type_id");
4041 return -EINVAL;
4042 }
4043
4044 args = (const struct btf_param *)(proto_type + 1);
4045 nr_args = btf_type_vlen(proto_type);
4046 for (i = 0; i < nr_args; i++) {
4047 if (!args[i].name_off && args[i].type) {
4048 btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1);
4049 return -EINVAL;
4050 }
4051 }
4052
4053 return 0;
4054}
4055
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07004056static const struct btf_kind_operations * const kind_ops[NR_BTF_KINDS] = {
4057 [BTF_KIND_INT] = &int_ops,
4058 [BTF_KIND_PTR] = &ptr_ops,
4059 [BTF_KIND_ARRAY] = &array_ops,
4060 [BTF_KIND_STRUCT] = &struct_ops,
4061 [BTF_KIND_UNION] = &struct_ops,
4062 [BTF_KIND_ENUM] = &enum_ops,
4063 [BTF_KIND_FWD] = &fwd_ops,
4064 [BTF_KIND_TYPEDEF] = &modifier_ops,
4065 [BTF_KIND_VOLATILE] = &modifier_ops,
4066 [BTF_KIND_CONST] = &modifier_ops,
4067 [BTF_KIND_RESTRICT] = &modifier_ops,
Martin KaFai Lau2667a262018-11-19 15:29:08 -08004068 [BTF_KIND_FUNC] = &func_ops,
4069 [BTF_KIND_FUNC_PROTO] = &func_proto_ops,
Daniel Borkmann1dc92852019-04-09 23:20:09 +02004070 [BTF_KIND_VAR] = &var_ops,
4071 [BTF_KIND_DATASEC] = &datasec_ops,
Ilya Leoshkevichb1828f02021-02-26 21:22:52 +01004072 [BTF_KIND_FLOAT] = &float_ops,
Yonghong Song223f9032021-10-12 09:48:38 -07004073 [BTF_KIND_DECL_TAG] = &decl_tag_ops,
Yonghong Song8c42d2f2021-11-11 17:26:09 -08004074 [BTF_KIND_TYPE_TAG] = &modifier_ops,
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07004075};
4076
4077static s32 btf_check_meta(struct btf_verifier_env *env,
4078 const struct btf_type *t,
4079 u32 meta_left)
4080{
4081 u32 saved_meta_left = meta_left;
4082 s32 var_meta_size;
4083
4084 if (meta_left < sizeof(*t)) {
4085 btf_verifier_log(env, "[%u] meta_left:%u meta_needed:%zu",
4086 env->log_type_id, meta_left, sizeof(*t));
4087 return -EINVAL;
4088 }
4089 meta_left -= sizeof(*t);
4090
Martin KaFai Lauaea2f7b82018-05-22 14:57:20 -07004091 if (t->info & ~BTF_INFO_MASK) {
4092 btf_verifier_log(env, "[%u] Invalid btf_info:%x",
4093 env->log_type_id, t->info);
4094 return -EINVAL;
4095 }
4096
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07004097 if (BTF_INFO_KIND(t->info) > BTF_KIND_MAX ||
4098 BTF_INFO_KIND(t->info) == BTF_KIND_UNKN) {
4099 btf_verifier_log(env, "[%u] Invalid kind:%u",
4100 env->log_type_id, BTF_INFO_KIND(t->info));
4101 return -EINVAL;
4102 }
4103
Martin KaFai Laufbcf93e2018-04-21 09:48:23 -07004104 if (!btf_name_offset_valid(env->btf, t->name_off)) {
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07004105 btf_verifier_log(env, "[%u] Invalid name_offset:%u",
Martin KaFai Laufbcf93e2018-04-21 09:48:23 -07004106 env->log_type_id, t->name_off);
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07004107 return -EINVAL;
4108 }
4109
4110 var_meta_size = btf_type_ops(t)->check_meta(env, t, meta_left);
4111 if (var_meta_size < 0)
4112 return var_meta_size;
4113
4114 meta_left -= var_meta_size;
4115
4116 return saved_meta_left - meta_left;
4117}
4118
4119static int btf_check_all_metas(struct btf_verifier_env *env)
4120{
4121 struct btf *btf = env->btf;
4122 struct btf_header *hdr;
4123 void *cur, *end;
4124
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07004125 hdr = &btf->hdr;
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07004126 cur = btf->nohdr_data + hdr->type_off;
Martin KaFai Lau4b1c5d92018-09-12 10:29:11 -07004127 end = cur + hdr->type_len;
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07004128
Andrii Nakryiko951bb642020-11-09 17:19:28 -08004129 env->log_type_id = btf->base_btf ? btf->start_id : 1;
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07004130 while (cur < end) {
4131 struct btf_type *t = cur;
4132 s32 meta_size;
4133
4134 meta_size = btf_check_meta(env, t, end - cur);
4135 if (meta_size < 0)
4136 return meta_size;
4137
4138 btf_add_type(env, t);
4139 cur += meta_size;
4140 env->log_type_id++;
4141 }
4142
4143 return 0;
4144}
4145
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07004146static bool btf_resolve_valid(struct btf_verifier_env *env,
4147 const struct btf_type *t,
4148 u32 type_id)
4149{
4150 struct btf *btf = env->btf;
4151
4152 if (!env_type_is_resolved(env, type_id))
4153 return false;
4154
Daniel Borkmann1dc92852019-04-09 23:20:09 +02004155 if (btf_type_is_struct(t) || btf_type_is_datasec(t))
Andrii Nakryiko951bb642020-11-09 17:19:28 -08004156 return !btf_resolved_type_id(btf, type_id) &&
4157 !btf_resolved_type_size(btf, type_id);
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07004158
Yonghong Song223f9032021-10-12 09:48:38 -07004159 if (btf_type_is_decl_tag(t))
Yonghong Songb5ea8342021-09-14 15:30:15 -07004160 return btf_resolved_type_id(btf, type_id) &&
4161 !btf_resolved_type_size(btf, type_id);
4162
Daniel Borkmann1dc92852019-04-09 23:20:09 +02004163 if (btf_type_is_modifier(t) || btf_type_is_ptr(t) ||
4164 btf_type_is_var(t)) {
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07004165 t = btf_type_id_resolve(btf, &type_id);
Daniel Borkmann1dc92852019-04-09 23:20:09 +02004166 return t &&
4167 !btf_type_is_modifier(t) &&
4168 !btf_type_is_var(t) &&
4169 !btf_type_is_datasec(t);
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07004170 }
4171
4172 if (btf_type_is_array(t)) {
4173 const struct btf_array *array = btf_type_array(t);
4174 const struct btf_type *elem_type;
4175 u32 elem_type_id = array->type;
4176 u32 elem_size;
4177
4178 elem_type = btf_type_id_size(btf, &elem_type_id, &elem_size);
4179 return elem_type && !btf_type_is_modifier(elem_type) &&
4180 (array->nelems * elem_size ==
Andrii Nakryiko951bb642020-11-09 17:19:28 -08004181 btf_resolved_type_size(btf, type_id));
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07004182 }
4183
4184 return false;
4185}
4186
Martin KaFai Lau2667a262018-11-19 15:29:08 -08004187static int btf_resolve(struct btf_verifier_env *env,
4188 const struct btf_type *t, u32 type_id)
4189{
4190 u32 save_log_type_id = env->log_type_id;
4191 const struct resolve_vertex *v;
4192 int err = 0;
4193
4194 env->resolve_mode = RESOLVE_TBD;
4195 env_stack_push(env, t, type_id);
4196 while (!err && (v = env_stack_peak(env))) {
4197 env->log_type_id = v->type_id;
4198 err = btf_type_ops(v->t)->resolve(env, v);
4199 }
4200
4201 env->log_type_id = type_id;
4202 if (err == -E2BIG) {
4203 btf_verifier_log_type(env, t,
4204 "Exceeded max resolving depth:%u",
4205 MAX_RESOLVE_DEPTH);
4206 } else if (err == -EEXIST) {
4207 btf_verifier_log_type(env, t, "Loop detected");
4208 }
4209
4210 /* Final sanity check */
4211 if (!err && !btf_resolve_valid(env, t, type_id)) {
4212 btf_verifier_log_type(env, t, "Invalid resolve state");
4213 err = -EINVAL;
4214 }
4215
4216 env->log_type_id = save_log_type_id;
4217 return err;
4218}
4219
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07004220static int btf_check_all_types(struct btf_verifier_env *env)
4221{
4222 struct btf *btf = env->btf;
Andrii Nakryiko951bb642020-11-09 17:19:28 -08004223 const struct btf_type *t;
4224 u32 type_id, i;
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07004225 int err;
4226
4227 err = env_resolve_init(env);
4228 if (err)
4229 return err;
4230
4231 env->phase++;
Andrii Nakryiko951bb642020-11-09 17:19:28 -08004232 for (i = btf->base_btf ? 0 : 1; i < btf->nr_types; i++) {
4233 type_id = btf->start_id + i;
4234 t = btf_type_by_id(btf, type_id);
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07004235
4236 env->log_type_id = type_id;
4237 if (btf_type_needs_resolve(t) &&
4238 !env_type_is_resolved(env, type_id)) {
4239 err = btf_resolve(env, t, type_id);
4240 if (err)
4241 return err;
4242 }
4243
Martin KaFai Lau2667a262018-11-19 15:29:08 -08004244 if (btf_type_is_func_proto(t)) {
4245 err = btf_func_proto_check(env, t);
4246 if (err)
4247 return err;
4248 }
4249
4250 if (btf_type_is_func(t)) {
4251 err = btf_func_check(env, t);
4252 if (err)
4253 return err;
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07004254 }
4255 }
4256
4257 return 0;
4258}
4259
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07004260static int btf_parse_type_sec(struct btf_verifier_env *env)
4261{
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07004262 const struct btf_header *hdr = &env->btf->hdr;
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07004263 int err;
4264
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07004265 /* Type section must align to 4 bytes */
4266 if (hdr->type_off & (sizeof(u32) - 1)) {
4267 btf_verifier_log(env, "Unaligned type_off");
4268 return -EINVAL;
4269 }
4270
Andrii Nakryiko951bb642020-11-09 17:19:28 -08004271 if (!env->btf->base_btf && !hdr->type_len) {
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07004272 btf_verifier_log(env, "No type found");
4273 return -EINVAL;
4274 }
4275
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07004276 err = btf_check_all_metas(env);
4277 if (err)
4278 return err;
4279
4280 return btf_check_all_types(env);
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07004281}
4282
4283static int btf_parse_str_sec(struct btf_verifier_env *env)
4284{
4285 const struct btf_header *hdr;
4286 struct btf *btf = env->btf;
4287 const char *start, *end;
4288
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07004289 hdr = &btf->hdr;
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07004290 start = btf->nohdr_data + hdr->str_off;
4291 end = start + hdr->str_len;
4292
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07004293 if (end != btf->data + btf->data_size) {
4294 btf_verifier_log(env, "String section is not at the end");
4295 return -EINVAL;
4296 }
4297
Andrii Nakryiko951bb642020-11-09 17:19:28 -08004298 btf->strings = start;
4299
4300 if (btf->base_btf && !hdr->str_len)
4301 return 0;
4302 if (!hdr->str_len || hdr->str_len - 1 > BTF_MAX_NAME_OFFSET || end[-1]) {
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07004303 btf_verifier_log(env, "Invalid string section");
4304 return -EINVAL;
4305 }
Andrii Nakryiko951bb642020-11-09 17:19:28 -08004306 if (!btf->base_btf && start[0]) {
4307 btf_verifier_log(env, "Invalid string section");
4308 return -EINVAL;
4309 }
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07004310
4311 return 0;
4312}
4313
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07004314static const size_t btf_sec_info_offset[] = {
4315 offsetof(struct btf_header, type_off),
4316 offsetof(struct btf_header, str_off),
4317};
4318
4319static int btf_sec_info_cmp(const void *a, const void *b)
4320{
4321 const struct btf_sec_info *x = a;
4322 const struct btf_sec_info *y = b;
4323
4324 return (int)(x->off - y->off) ? : (int)(x->len - y->len);
4325}
4326
4327static int btf_check_sec_info(struct btf_verifier_env *env,
4328 u32 btf_data_size)
4329{
Martin KaFai Laua2889a42018-05-23 11:32:36 -07004330 struct btf_sec_info secs[ARRAY_SIZE(btf_sec_info_offset)];
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07004331 u32 total, expected_total, i;
4332 const struct btf_header *hdr;
4333 const struct btf *btf;
4334
4335 btf = env->btf;
4336 hdr = &btf->hdr;
4337
4338 /* Populate the secs from hdr */
Martin KaFai Laua2889a42018-05-23 11:32:36 -07004339 for (i = 0; i < ARRAY_SIZE(btf_sec_info_offset); i++)
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07004340 secs[i] = *(struct btf_sec_info *)((void *)hdr +
4341 btf_sec_info_offset[i]);
4342
Martin KaFai Laua2889a42018-05-23 11:32:36 -07004343 sort(secs, ARRAY_SIZE(btf_sec_info_offset),
4344 sizeof(struct btf_sec_info), btf_sec_info_cmp, NULL);
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07004345
4346 /* Check for gaps and overlap among sections */
4347 total = 0;
4348 expected_total = btf_data_size - hdr->hdr_len;
Martin KaFai Laua2889a42018-05-23 11:32:36 -07004349 for (i = 0; i < ARRAY_SIZE(btf_sec_info_offset); i++) {
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07004350 if (expected_total < secs[i].off) {
4351 btf_verifier_log(env, "Invalid section offset");
4352 return -EINVAL;
4353 }
4354 if (total < secs[i].off) {
4355 /* gap */
4356 btf_verifier_log(env, "Unsupported section found");
4357 return -EINVAL;
4358 }
4359 if (total > secs[i].off) {
4360 btf_verifier_log(env, "Section overlap found");
4361 return -EINVAL;
4362 }
4363 if (expected_total - total < secs[i].len) {
4364 btf_verifier_log(env,
4365 "Total section length too long");
4366 return -EINVAL;
4367 }
4368 total += secs[i].len;
4369 }
4370
4371 /* There is data other than hdr and known sections */
4372 if (expected_total != total) {
4373 btf_verifier_log(env, "Unsupported section found");
4374 return -EINVAL;
4375 }
4376
4377 return 0;
4378}
4379
Martin Lau4a6998a2018-10-24 20:42:25 +00004380static int btf_parse_hdr(struct btf_verifier_env *env)
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07004381{
Martin Lau4a6998a2018-10-24 20:42:25 +00004382 u32 hdr_len, hdr_copy, btf_data_size;
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07004383 const struct btf_header *hdr;
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07004384 struct btf *btf;
4385 int err;
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07004386
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07004387 btf = env->btf;
Martin Lau4a6998a2018-10-24 20:42:25 +00004388 btf_data_size = btf->data_size;
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07004389
Martin Lau4a6998a2018-10-24 20:42:25 +00004390 if (btf_data_size <
4391 offsetof(struct btf_header, hdr_len) + sizeof(hdr->hdr_len)) {
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07004392 btf_verifier_log(env, "hdr_len not found");
4393 return -EINVAL;
4394 }
4395
Martin Lau4a6998a2018-10-24 20:42:25 +00004396 hdr = btf->data;
4397 hdr_len = hdr->hdr_len;
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07004398 if (btf_data_size < hdr_len) {
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07004399 btf_verifier_log(env, "btf_header not found");
4400 return -EINVAL;
4401 }
4402
Martin Lau4a6998a2018-10-24 20:42:25 +00004403 /* Ensure the unsupported header fields are zero */
4404 if (hdr_len > sizeof(btf->hdr)) {
4405 u8 *expected_zero = btf->data + sizeof(btf->hdr);
4406 u8 *end = btf->data + hdr_len;
4407
4408 for (; expected_zero < end; expected_zero++) {
4409 if (*expected_zero) {
4410 btf_verifier_log(env, "Unsupported btf_header");
4411 return -E2BIG;
4412 }
4413 }
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07004414 }
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07004415
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07004416 hdr_copy = min_t(u32, hdr_len, sizeof(btf->hdr));
Martin Lau4a6998a2018-10-24 20:42:25 +00004417 memcpy(&btf->hdr, btf->data, hdr_copy);
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07004418
4419 hdr = &btf->hdr;
4420
4421 btf_verifier_log_hdr(env, btf_data_size);
4422
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07004423 if (hdr->magic != BTF_MAGIC) {
4424 btf_verifier_log(env, "Invalid magic");
4425 return -EINVAL;
4426 }
4427
4428 if (hdr->version != BTF_VERSION) {
4429 btf_verifier_log(env, "Unsupported version");
4430 return -ENOTSUPP;
4431 }
4432
4433 if (hdr->flags) {
4434 btf_verifier_log(env, "Unsupported flags");
4435 return -ENOTSUPP;
4436 }
4437
Andrii Nakryikobcc5e612021-01-09 23:03:40 -08004438 if (!btf->base_btf && btf_data_size == hdr->hdr_len) {
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07004439 btf_verifier_log(env, "No data");
4440 return -EINVAL;
4441 }
4442
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07004443 err = btf_check_sec_info(env, btf_data_size);
4444 if (err)
4445 return err;
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07004446
4447 return 0;
4448}
4449
Alexei Starovoitovc571bd72021-05-13 17:36:08 -07004450static struct btf *btf_parse(bpfptr_t btf_data, u32 btf_data_size,
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07004451 u32 log_level, char __user *log_ubuf, u32 log_size)
4452{
4453 struct btf_verifier_env *env = NULL;
4454 struct bpf_verifier_log *log;
4455 struct btf *btf = NULL;
4456 u8 *data;
4457 int err;
4458
4459 if (btf_data_size > BTF_MAX_SIZE)
4460 return ERR_PTR(-E2BIG);
4461
4462 env = kzalloc(sizeof(*env), GFP_KERNEL | __GFP_NOWARN);
4463 if (!env)
4464 return ERR_PTR(-ENOMEM);
4465
4466 log = &env->log;
4467 if (log_level || log_ubuf || log_size) {
4468 /* user requested verbose verifier output
4469 * and supplied buffer to store the verification trace
4470 */
4471 log->level = log_level;
4472 log->ubuf = log_ubuf;
4473 log->len_total = log_size;
4474
4475 /* log attributes have to be sane */
Hou Tao866de402021-12-03 13:30:01 +08004476 if (!bpf_verifier_log_attr_valid(log)) {
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07004477 err = -EINVAL;
4478 goto errout;
4479 }
4480 }
4481
4482 btf = kzalloc(sizeof(*btf), GFP_KERNEL | __GFP_NOWARN);
4483 if (!btf) {
4484 err = -ENOMEM;
4485 goto errout;
4486 }
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07004487 env->btf = btf;
4488
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07004489 data = kvmalloc(btf_data_size, GFP_KERNEL | __GFP_NOWARN);
4490 if (!data) {
4491 err = -ENOMEM;
4492 goto errout;
4493 }
4494
4495 btf->data = data;
4496 btf->data_size = btf_data_size;
4497
Alexei Starovoitovc571bd72021-05-13 17:36:08 -07004498 if (copy_from_bpfptr(data, btf_data, btf_data_size)) {
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07004499 err = -EFAULT;
4500 goto errout;
4501 }
4502
Martin Lau4a6998a2018-10-24 20:42:25 +00004503 err = btf_parse_hdr(env);
4504 if (err)
4505 goto errout;
4506
4507 btf->nohdr_data = btf->data + btf->hdr.hdr_len;
4508
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07004509 err = btf_parse_str_sec(env);
4510 if (err)
4511 goto errout;
4512
4513 err = btf_parse_type_sec(env);
4514 if (err)
4515 goto errout;
4516
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07004517 if (log->level && bpf_verifier_log_full(log)) {
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07004518 err = -ENOSPC;
4519 goto errout;
4520 }
4521
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07004522 btf_verifier_env_free(env);
4523 refcount_set(&btf->refcnt, 1);
4524 return btf;
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07004525
4526errout:
4527 btf_verifier_env_free(env);
4528 if (btf)
4529 btf_free(btf);
4530 return ERR_PTR(err);
4531}
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07004532
Fangrui Song90ceddc2020-03-18 15:27:46 -07004533extern char __weak __start_BTF[];
4534extern char __weak __stop_BTF[];
Alexei Starovoitov91cc1a92019-11-14 10:57:15 -08004535extern struct btf *btf_vmlinux;
4536
4537#define BPF_MAP_TYPE(_id, _ops)
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07004538#define BPF_LINK_TYPE(_id, _name)
Alexei Starovoitov91cc1a92019-11-14 10:57:15 -08004539static union {
4540 struct bpf_ctx_convert {
4541#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
4542 prog_ctx_type _id##_prog; \
4543 kern_ctx_type _id##_kern;
4544#include <linux/bpf_types.h>
4545#undef BPF_PROG_TYPE
4546 } *__t;
4547 /* 't' is written once under lock. Read many times. */
4548 const struct btf_type *t;
4549} bpf_ctx_convert;
4550enum {
4551#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
4552 __ctx_convert##_id,
4553#include <linux/bpf_types.h>
4554#undef BPF_PROG_TYPE
Alexei Starovoitovce277092019-11-27 20:35:08 -08004555 __ctx_convert_unused, /* to avoid empty enum in extreme .config */
Alexei Starovoitov91cc1a92019-11-14 10:57:15 -08004556};
4557static u8 bpf_ctx_convert_map[] = {
4558#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
4559 [_id] = __ctx_convert##_id,
4560#include <linux/bpf_types.h>
4561#undef BPF_PROG_TYPE
Arnd Bergmann4c80c7bc2019-12-10 21:35:46 +01004562 0, /* avoid empty array */
Alexei Starovoitov91cc1a92019-11-14 10:57:15 -08004563};
4564#undef BPF_MAP_TYPE
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07004565#undef BPF_LINK_TYPE
Alexei Starovoitov91cc1a92019-11-14 10:57:15 -08004566
4567static const struct btf_member *
Martin KaFai Lau34747c42021-03-24 18:51:36 -07004568btf_get_prog_ctx_type(struct bpf_verifier_log *log, const struct btf *btf,
Alexei Starovoitov51c39bb2020-01-09 22:41:20 -08004569 const struct btf_type *t, enum bpf_prog_type prog_type,
4570 int arg)
Alexei Starovoitov91cc1a92019-11-14 10:57:15 -08004571{
4572 const struct btf_type *conv_struct;
4573 const struct btf_type *ctx_struct;
4574 const struct btf_member *ctx_type;
4575 const char *tname, *ctx_tname;
4576
4577 conv_struct = bpf_ctx_convert.t;
4578 if (!conv_struct) {
4579 bpf_log(log, "btf_vmlinux is malformed\n");
4580 return NULL;
4581 }
4582 t = btf_type_by_id(btf, t->type);
4583 while (btf_type_is_modifier(t))
4584 t = btf_type_by_id(btf, t->type);
4585 if (!btf_type_is_struct(t)) {
4586 /* Only pointer to struct is supported for now.
4587 * That means that BPF_PROG_TYPE_TRACEPOINT with BTF
4588 * is not supported yet.
4589 * BPF_PROG_TYPE_RAW_TRACEPOINT is fine.
4590 */
Alexei Starovoitov91cc1a92019-11-14 10:57:15 -08004591 return NULL;
4592 }
4593 tname = btf_name_by_offset(btf, t->name_off);
4594 if (!tname) {
Alexei Starovoitov51c39bb2020-01-09 22:41:20 -08004595 bpf_log(log, "arg#%d struct doesn't have a name\n", arg);
Alexei Starovoitov91cc1a92019-11-14 10:57:15 -08004596 return NULL;
4597 }
4598 /* prog_type is valid bpf program type. No need for bounds check. */
4599 ctx_type = btf_type_member(conv_struct) + bpf_ctx_convert_map[prog_type] * 2;
4600 /* ctx_struct is a pointer to prog_ctx_type in vmlinux.
4601 * Like 'struct __sk_buff'
4602 */
4603 ctx_struct = btf_type_by_id(btf_vmlinux, ctx_type->type);
4604 if (!ctx_struct)
4605 /* should not happen */
4606 return NULL;
4607 ctx_tname = btf_name_by_offset(btf_vmlinux, ctx_struct->name_off);
4608 if (!ctx_tname) {
4609 /* should not happen */
4610 bpf_log(log, "Please fix kernel include/linux/bpf_types.h\n");
4611 return NULL;
4612 }
4613 /* only compare that prog's ctx type name is the same as
4614 * kernel expects. No need to compare field by field.
4615 * It's ok for bpf prog to do:
4616 * struct __sk_buff {};
4617 * int socket_filter_bpf_prog(struct __sk_buff *skb)
4618 * { // no fields of skb are ever used }
4619 */
4620 if (strcmp(ctx_tname, tname))
4621 return NULL;
4622 return ctx_type;
4623}
Alexei Starovoitov8580ac92019-10-15 20:24:57 -07004624
Andrey Ignatov41c48f32020-06-19 14:11:43 -07004625static const struct bpf_map_ops * const btf_vmlinux_map_ops[] = {
4626#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
4627#define BPF_LINK_TYPE(_id, _name)
4628#define BPF_MAP_TYPE(_id, _ops) \
4629 [_id] = &_ops,
4630#include <linux/bpf_types.h>
4631#undef BPF_PROG_TYPE
4632#undef BPF_LINK_TYPE
4633#undef BPF_MAP_TYPE
4634};
4635
4636static int btf_vmlinux_map_ids_init(const struct btf *btf,
4637 struct bpf_verifier_log *log)
4638{
4639 const struct bpf_map_ops *ops;
4640 int i, btf_id;
4641
4642 for (i = 0; i < ARRAY_SIZE(btf_vmlinux_map_ops); ++i) {
4643 ops = btf_vmlinux_map_ops[i];
4644 if (!ops || (!ops->map_btf_name && !ops->map_btf_id))
4645 continue;
4646 if (!ops->map_btf_name || !ops->map_btf_id) {
4647 bpf_log(log, "map type %d is misconfigured\n", i);
4648 return -EINVAL;
4649 }
4650 btf_id = btf_find_by_name_kind(btf, ops->map_btf_name,
4651 BTF_KIND_STRUCT);
4652 if (btf_id < 0)
4653 return btf_id;
4654 *ops->map_btf_id = btf_id;
4655 }
4656
4657 return 0;
4658}
4659
Alexei Starovoitov5b92a282019-11-14 10:57:17 -08004660static int btf_translate_to_vmlinux(struct bpf_verifier_log *log,
4661 struct btf *btf,
4662 const struct btf_type *t,
Alexei Starovoitov51c39bb2020-01-09 22:41:20 -08004663 enum bpf_prog_type prog_type,
4664 int arg)
Alexei Starovoitov5b92a282019-11-14 10:57:17 -08004665{
4666 const struct btf_member *prog_ctx_type, *kern_ctx_type;
4667
Alexei Starovoitov51c39bb2020-01-09 22:41:20 -08004668 prog_ctx_type = btf_get_prog_ctx_type(log, btf, t, prog_type, arg);
Alexei Starovoitov5b92a282019-11-14 10:57:17 -08004669 if (!prog_ctx_type)
4670 return -ENOENT;
4671 kern_ctx_type = prog_ctx_type + 1;
4672 return kern_ctx_type->type;
4673}
4674
Jiri Olsa49f4e672020-07-11 23:53:26 +02004675BTF_ID_LIST(bpf_ctx_convert_btf_id)
4676BTF_ID(struct, bpf_ctx_convert)
4677
Alexei Starovoitov8580ac92019-10-15 20:24:57 -07004678struct btf *btf_parse_vmlinux(void)
4679{
4680 struct btf_verifier_env *env = NULL;
4681 struct bpf_verifier_log *log;
4682 struct btf *btf = NULL;
Jiri Olsa49f4e672020-07-11 23:53:26 +02004683 int err;
Alexei Starovoitov8580ac92019-10-15 20:24:57 -07004684
4685 env = kzalloc(sizeof(*env), GFP_KERNEL | __GFP_NOWARN);
4686 if (!env)
4687 return ERR_PTR(-ENOMEM);
4688
4689 log = &env->log;
4690 log->level = BPF_LOG_KERNEL;
4691
4692 btf = kzalloc(sizeof(*btf), GFP_KERNEL | __GFP_NOWARN);
4693 if (!btf) {
4694 err = -ENOMEM;
4695 goto errout;
4696 }
4697 env->btf = btf;
4698
Fangrui Song90ceddc2020-03-18 15:27:46 -07004699 btf->data = __start_BTF;
4700 btf->data_size = __stop_BTF - __start_BTF;
Andrii Nakryiko53297222020-11-09 17:19:29 -08004701 btf->kernel_btf = true;
4702 snprintf(btf->name, sizeof(btf->name), "vmlinux");
Alexei Starovoitov8580ac92019-10-15 20:24:57 -07004703
4704 err = btf_parse_hdr(env);
4705 if (err)
4706 goto errout;
4707
4708 btf->nohdr_data = btf->data + btf->hdr.hdr_len;
4709
4710 err = btf_parse_str_sec(env);
4711 if (err)
4712 goto errout;
4713
4714 err = btf_check_all_metas(env);
4715 if (err)
4716 goto errout;
4717
Andrey Ignatova2d0d622020-06-19 14:11:41 -07004718 /* btf_parse_vmlinux() runs under bpf_verifier_lock */
Jiri Olsa49f4e672020-07-11 23:53:26 +02004719 bpf_ctx_convert.t = btf_type_by_id(btf, bpf_ctx_convert_btf_id[0]);
Alexei Starovoitov91cc1a92019-11-14 10:57:15 -08004720
Andrey Ignatov41c48f32020-06-19 14:11:43 -07004721 /* find bpf map structs for map_ptr access checking */
4722 err = btf_vmlinux_map_ids_init(btf, log);
4723 if (err < 0)
4724 goto errout;
4725
Martin KaFai Laud3e42bb2020-01-27 09:51:45 -08004726 bpf_struct_ops_init(btf, log);
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08004727
Alexei Starovoitov8580ac92019-10-15 20:24:57 -07004728 refcount_set(&btf->refcnt, 1);
Andrii Nakryiko53297222020-11-09 17:19:29 -08004729
4730 err = btf_alloc_id(btf);
4731 if (err)
4732 goto errout;
4733
4734 btf_verifier_env_free(env);
Alexei Starovoitov8580ac92019-10-15 20:24:57 -07004735 return btf;
4736
4737errout:
4738 btf_verifier_env_free(env);
4739 if (btf) {
4740 kvfree(btf->types);
4741 kfree(btf);
4742 }
4743 return ERR_PTR(err);
4744}
4745
Andrii Nakryiko7112d122020-11-10 20:06:45 -08004746#ifdef CONFIG_DEBUG_INFO_BTF_MODULES
4747
Andrii Nakryiko36e68442020-11-09 17:19:31 -08004748static struct btf *btf_parse_module(const char *module_name, const void *data, unsigned int data_size)
4749{
4750 struct btf_verifier_env *env = NULL;
4751 struct bpf_verifier_log *log;
4752 struct btf *btf = NULL, *base_btf;
4753 int err;
4754
4755 base_btf = bpf_get_btf_vmlinux();
4756 if (IS_ERR(base_btf))
4757 return base_btf;
4758 if (!base_btf)
4759 return ERR_PTR(-EINVAL);
4760
4761 env = kzalloc(sizeof(*env), GFP_KERNEL | __GFP_NOWARN);
4762 if (!env)
4763 return ERR_PTR(-ENOMEM);
4764
4765 log = &env->log;
4766 log->level = BPF_LOG_KERNEL;
4767
4768 btf = kzalloc(sizeof(*btf), GFP_KERNEL | __GFP_NOWARN);
4769 if (!btf) {
4770 err = -ENOMEM;
4771 goto errout;
4772 }
4773 env->btf = btf;
4774
4775 btf->base_btf = base_btf;
4776 btf->start_id = base_btf->nr_types;
4777 btf->start_str_off = base_btf->hdr.str_len;
4778 btf->kernel_btf = true;
4779 snprintf(btf->name, sizeof(btf->name), "%s", module_name);
4780
4781 btf->data = kvmalloc(data_size, GFP_KERNEL | __GFP_NOWARN);
4782 if (!btf->data) {
4783 err = -ENOMEM;
4784 goto errout;
4785 }
4786 memcpy(btf->data, data, data_size);
4787 btf->data_size = data_size;
4788
4789 err = btf_parse_hdr(env);
4790 if (err)
4791 goto errout;
4792
4793 btf->nohdr_data = btf->data + btf->hdr.hdr_len;
4794
4795 err = btf_parse_str_sec(env);
4796 if (err)
4797 goto errout;
4798
4799 err = btf_check_all_metas(env);
4800 if (err)
4801 goto errout;
4802
4803 btf_verifier_env_free(env);
4804 refcount_set(&btf->refcnt, 1);
4805 return btf;
4806
4807errout:
4808 btf_verifier_env_free(env);
4809 if (btf) {
4810 kvfree(btf->data);
4811 kvfree(btf->types);
4812 kfree(btf);
4813 }
4814 return ERR_PTR(err);
4815}
4816
Andrii Nakryiko7112d122020-11-10 20:06:45 -08004817#endif /* CONFIG_DEBUG_INFO_BTF_MODULES */
4818
Alexei Starovoitov5b92a282019-11-14 10:57:17 -08004819struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog)
4820{
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02004821 struct bpf_prog *tgt_prog = prog->aux->dst_prog;
Alexei Starovoitov5b92a282019-11-14 10:57:17 -08004822
Andrii Nakryiko22dc4a02020-12-03 12:46:29 -08004823 if (tgt_prog)
Alexei Starovoitov5b92a282019-11-14 10:57:17 -08004824 return tgt_prog->aux->btf;
Andrii Nakryiko22dc4a02020-12-03 12:46:29 -08004825 else
4826 return prog->aux->attach_btf;
Alexei Starovoitov5b92a282019-11-14 10:57:17 -08004827}
4828
Jiri Olsabb6728d2021-12-08 20:32:41 +01004829static bool is_int_ptr(struct btf *btf, const struct btf_type *t)
Jiri Olsa84ad7a72020-01-23 17:15:06 +01004830{
4831 /* t comes in already as a pointer */
4832 t = btf_type_by_id(btf, t->type);
4833
4834 /* allow const */
4835 if (BTF_INFO_KIND(t->info) == BTF_KIND_CONST)
4836 t = btf_type_by_id(btf, t->type);
4837
Jiri Olsabb6728d2021-12-08 20:32:41 +01004838 return btf_type_is_int(t);
Jiri Olsa84ad7a72020-01-23 17:15:06 +01004839}
4840
Alexei Starovoitov9e15db62019-10-15 20:25:00 -07004841bool btf_ctx_access(int off, int size, enum bpf_access_type type,
4842 const struct bpf_prog *prog,
4843 struct bpf_insn_access_aux *info)
4844{
Martin KaFai Lau38207292019-10-24 17:18:11 -07004845 const struct btf_type *t = prog->aux->attach_func_proto;
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02004846 struct bpf_prog *tgt_prog = prog->aux->dst_prog;
Alexei Starovoitov5b92a282019-11-14 10:57:17 -08004847 struct btf *btf = bpf_prog_get_target_btf(prog);
Martin KaFai Lau38207292019-10-24 17:18:11 -07004848 const char *tname = prog->aux->attach_func_name;
Alexei Starovoitov9e15db62019-10-15 20:25:00 -07004849 struct bpf_verifier_log *log = info->log;
Alexei Starovoitov9e15db62019-10-15 20:25:00 -07004850 const struct btf_param *args;
Alexei Starovoitov9e15db62019-10-15 20:25:00 -07004851 u32 nr_args, arg;
Yonghong Song3c32cc12020-05-13 11:02:21 -07004852 int i, ret;
Alexei Starovoitov9e15db62019-10-15 20:25:00 -07004853
Alexei Starovoitov9e15db62019-10-15 20:25:00 -07004854 if (off % 8) {
Martin KaFai Lau38207292019-10-24 17:18:11 -07004855 bpf_log(log, "func '%s' offset %d is not multiple of 8\n",
Alexei Starovoitov9e15db62019-10-15 20:25:00 -07004856 tname, off);
4857 return false;
4858 }
4859 arg = off / 8;
4860 args = (const struct btf_param *)(t + 1);
Dmitrii Banshchikov523a4cf2021-02-26 00:26:29 +04004861 /* if (t == NULL) Fall back to default BPF prog with
4862 * MAX_BPF_FUNC_REG_ARGS u64 arguments.
4863 */
4864 nr_args = t ? btf_type_vlen(t) : MAX_BPF_FUNC_REG_ARGS;
Martin KaFai Lau38207292019-10-24 17:18:11 -07004865 if (prog->aux->attach_btf_trace) {
4866 /* skip first 'void *__data' argument in btf_trace_##name typedef */
4867 args++;
4868 nr_args--;
4869 }
Alexei Starovoitov9e15db62019-10-15 20:25:00 -07004870
KP Singhf50b49a2020-03-30 16:42:46 +02004871 if (arg > nr_args) {
4872 bpf_log(log, "func '%s' doesn't have %d-th argument\n",
4873 tname, arg + 1);
4874 return false;
4875 }
4876
KP Singh6ba43b72020-03-04 20:18:50 +01004877 if (arg == nr_args) {
KP Singhf50b49a2020-03-30 16:42:46 +02004878 switch (prog->expected_attach_type) {
4879 case BPF_LSM_MAC:
4880 case BPF_TRACE_FEXIT:
KP Singh9e4e01d2020-03-29 01:43:52 +01004881 /* When LSM programs are attached to void LSM hooks
4882 * they use FEXIT trampolines and when attached to
4883 * int LSM hooks, they use MODIFY_RETURN trampolines.
4884 *
4885 * While the LSM programs are BPF_MODIFY_RETURN-like
4886 * the check:
4887 *
4888 * if (ret_type != 'int')
4889 * return -EINVAL;
4890 *
4891 * is _not_ done here. This is still safe as LSM hooks
4892 * have only void and int return types.
4893 */
KP Singh6ba43b72020-03-04 20:18:50 +01004894 if (!t)
4895 return true;
4896 t = btf_type_by_id(btf, t->type);
KP Singhf50b49a2020-03-30 16:42:46 +02004897 break;
4898 case BPF_MODIFY_RETURN:
KP Singh6ba43b72020-03-04 20:18:50 +01004899 /* For now the BPF_MODIFY_RETURN can only be attached to
4900 * functions that return an int.
4901 */
4902 if (!t)
4903 return false;
4904
4905 t = btf_type_skip_modifiers(btf, t->type, NULL);
John Fastabenda9b59152020-06-24 15:20:39 -07004906 if (!btf_type_is_small_int(t)) {
KP Singh6ba43b72020-03-04 20:18:50 +01004907 bpf_log(log,
4908 "ret type %s not allowed for fmod_ret\n",
4909 btf_kind_str[BTF_INFO_KIND(t->info)]);
4910 return false;
4911 }
KP Singhf50b49a2020-03-30 16:42:46 +02004912 break;
4913 default:
4914 bpf_log(log, "func '%s' doesn't have %d-th argument\n",
4915 tname, arg + 1);
4916 return false;
KP Singh6ba43b72020-03-04 20:18:50 +01004917 }
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08004918 } else {
Alexei Starovoitov5b92a282019-11-14 10:57:17 -08004919 if (!t)
Dmitrii Banshchikov523a4cf2021-02-26 00:26:29 +04004920 /* Default prog with MAX_BPF_FUNC_REG_ARGS args */
Alexei Starovoitov5b92a282019-11-14 10:57:17 -08004921 return true;
4922 t = btf_type_by_id(btf, args[arg].type);
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08004923 }
KP Singhf50b49a2020-03-30 16:42:46 +02004924
Alexei Starovoitov9e15db62019-10-15 20:25:00 -07004925 /* skip modifiers */
4926 while (btf_type_is_modifier(t))
Alexei Starovoitov5b92a282019-11-14 10:57:17 -08004927 t = btf_type_by_id(btf, t->type);
John Fastabenda9b59152020-06-24 15:20:39 -07004928 if (btf_type_is_small_int(t) || btf_type_is_enum(t))
Alexei Starovoitov9e15db62019-10-15 20:25:00 -07004929 /* accessing a scalar */
4930 return true;
4931 if (!btf_type_is_ptr(t)) {
4932 bpf_log(log,
Martin KaFai Lau38207292019-10-24 17:18:11 -07004933 "func '%s' arg%d '%s' has type %s. Only pointer access is allowed\n",
Alexei Starovoitov9e15db62019-10-15 20:25:00 -07004934 tname, arg,
Alexei Starovoitov5b92a282019-11-14 10:57:17 -08004935 __btf_name_by_offset(btf, t->name_off),
Alexei Starovoitov9e15db62019-10-15 20:25:00 -07004936 btf_kind_str[BTF_INFO_KIND(t->info)]);
4937 return false;
4938 }
Yonghong Songafbf21d2020-07-23 11:41:11 -07004939
4940 /* check for PTR_TO_RDONLY_BUF_OR_NULL or PTR_TO_RDWR_BUF_OR_NULL */
4941 for (i = 0; i < prog->aux->ctx_arg_info_size; i++) {
4942 const struct bpf_ctx_arg_aux *ctx_arg_info = &prog->aux->ctx_arg_info[i];
Hao Luoc25b2ae2021-12-16 16:31:47 -08004943 u32 type, flag;
Yonghong Songafbf21d2020-07-23 11:41:11 -07004944
Hao Luoc25b2ae2021-12-16 16:31:47 -08004945 type = base_type(ctx_arg_info->reg_type);
4946 flag = type_flag(ctx_arg_info->reg_type);
Hao Luo20b2aff2021-12-16 16:31:48 -08004947 if (ctx_arg_info->offset == off && type == PTR_TO_BUF &&
Hao Luoc25b2ae2021-12-16 16:31:47 -08004948 (flag & PTR_MAYBE_NULL)) {
Yonghong Songafbf21d2020-07-23 11:41:11 -07004949 info->reg_type = ctx_arg_info->reg_type;
4950 return true;
4951 }
4952 }
4953
Alexei Starovoitov9e15db62019-10-15 20:25:00 -07004954 if (t->type == 0)
4955 /* This is a pointer to void.
4956 * It is the same as scalar from the verifier safety pov.
4957 * No further pointer walking is allowed.
4958 */
4959 return true;
4960
Jiri Olsabb6728d2021-12-08 20:32:41 +01004961 if (is_int_ptr(btf, t))
Jiri Olsa84ad7a72020-01-23 17:15:06 +01004962 return true;
4963
Alexei Starovoitov9e15db62019-10-15 20:25:00 -07004964 /* this is a pointer to another type */
Yonghong Song3c32cc12020-05-13 11:02:21 -07004965 for (i = 0; i < prog->aux->ctx_arg_info_size; i++) {
4966 const struct bpf_ctx_arg_aux *ctx_arg_info = &prog->aux->ctx_arg_info[i];
4967
4968 if (ctx_arg_info->offset == off) {
Yonghong Songd3621642021-07-28 11:30:25 -07004969 if (!ctx_arg_info->btf_id) {
4970 bpf_log(log,"invalid btf_id for context argument offset %u\n", off);
4971 return false;
4972 }
4973
Yonghong Song3c32cc12020-05-13 11:02:21 -07004974 info->reg_type = ctx_arg_info->reg_type;
Andrii Nakryiko22dc4a02020-12-03 12:46:29 -08004975 info->btf = btf_vmlinux;
Yonghong Song951cf362020-07-20 09:34:03 -07004976 info->btf_id = ctx_arg_info->btf_id;
4977 return true;
Yonghong Song3c32cc12020-05-13 11:02:21 -07004978 }
4979 }
Alexei Starovoitov9e15db62019-10-15 20:25:00 -07004980
Yonghong Song951cf362020-07-20 09:34:03 -07004981 info->reg_type = PTR_TO_BTF_ID;
Alexei Starovoitov5b92a282019-11-14 10:57:17 -08004982 if (tgt_prog) {
Toke Høiland-Jørgensen43bc2872020-09-29 14:45:52 +02004983 enum bpf_prog_type tgt_type;
4984
4985 if (tgt_prog->type == BPF_PROG_TYPE_EXT)
4986 tgt_type = tgt_prog->aux->saved_dst_prog_type;
4987 else
4988 tgt_type = tgt_prog->type;
4989
4990 ret = btf_translate_to_vmlinux(log, btf, t, tgt_type, arg);
Alexei Starovoitov5b92a282019-11-14 10:57:17 -08004991 if (ret > 0) {
Andrii Nakryiko22dc4a02020-12-03 12:46:29 -08004992 info->btf = btf_vmlinux;
Alexei Starovoitov5b92a282019-11-14 10:57:17 -08004993 info->btf_id = ret;
4994 return true;
4995 } else {
4996 return false;
4997 }
4998 }
Martin KaFai Lau275517f2020-01-08 16:34:56 -08004999
Andrii Nakryiko22dc4a02020-12-03 12:46:29 -08005000 info->btf = btf;
Martin KaFai Lau275517f2020-01-08 16:34:56 -08005001 info->btf_id = t->type;
Alexei Starovoitov5b92a282019-11-14 10:57:17 -08005002 t = btf_type_by_id(btf, t->type);
Alexei Starovoitov9e15db62019-10-15 20:25:00 -07005003 /* skip modifiers */
Martin KaFai Lau275517f2020-01-08 16:34:56 -08005004 while (btf_type_is_modifier(t)) {
5005 info->btf_id = t->type;
Alexei Starovoitov5b92a282019-11-14 10:57:17 -08005006 t = btf_type_by_id(btf, t->type);
Martin KaFai Lau275517f2020-01-08 16:34:56 -08005007 }
Alexei Starovoitov9e15db62019-10-15 20:25:00 -07005008 if (!btf_type_is_struct(t)) {
5009 bpf_log(log,
Martin KaFai Lau38207292019-10-24 17:18:11 -07005010 "func '%s' arg%d type %s is not a struct\n",
Alexei Starovoitov9e15db62019-10-15 20:25:00 -07005011 tname, arg, btf_kind_str[BTF_INFO_KIND(t->info)]);
5012 return false;
5013 }
Martin KaFai Lau38207292019-10-24 17:18:11 -07005014 bpf_log(log, "func '%s' arg%d has btf_id %d type %s '%s'\n",
Alexei Starovoitov9e15db62019-10-15 20:25:00 -07005015 tname, arg, info->btf_id, btf_kind_str[BTF_INFO_KIND(t->info)],
Alexei Starovoitov5b92a282019-11-14 10:57:17 -08005016 __btf_name_by_offset(btf, t->name_off));
Alexei Starovoitov9e15db62019-10-15 20:25:00 -07005017 return true;
5018}
5019
Jiri Olsa1c6d28a2020-08-25 21:21:17 +02005020enum bpf_struct_walk_result {
5021 /* < 0 error */
5022 WALK_SCALAR = 0,
5023 WALK_PTR,
5024 WALK_STRUCT,
5025};
5026
Andrii Nakryiko22dc4a02020-12-03 12:46:29 -08005027static int btf_struct_walk(struct bpf_verifier_log *log, const struct btf *btf,
Jiri Olsa1c6d28a2020-08-25 21:21:17 +02005028 const struct btf_type *t, int off, int size,
5029 u32 *next_btf_id)
Alexei Starovoitov9e15db62019-10-15 20:25:00 -07005030{
Martin KaFai Lau7e3617a2019-11-07 10:09:03 -08005031 u32 i, moff, mtrue_end, msize = 0, total_nelems = 0;
5032 const struct btf_type *mtype, *elem_type = NULL;
Alexei Starovoitov9e15db62019-10-15 20:25:00 -07005033 const struct btf_member *member;
Alexei Starovoitov9e15db62019-10-15 20:25:00 -07005034 const char *tname, *mname;
Jiri Olsa1c6d28a2020-08-25 21:21:17 +02005035 u32 vlen, elem_id, mid;
Alexei Starovoitov9e15db62019-10-15 20:25:00 -07005036
5037again:
Andrii Nakryiko22dc4a02020-12-03 12:46:29 -08005038 tname = __btf_name_by_offset(btf, t->name_off);
Alexei Starovoitov9e15db62019-10-15 20:25:00 -07005039 if (!btf_type_is_struct(t)) {
Martin KaFai Lau275517f2020-01-08 16:34:56 -08005040 bpf_log(log, "Type '%s' is not a struct\n", tname);
Alexei Starovoitov9e15db62019-10-15 20:25:00 -07005041 return -EINVAL;
5042 }
5043
Yonghong Song9c5f8a102020-05-09 10:59:16 -07005044 vlen = btf_type_vlen(t);
Martin KaFai Lau976aba02020-01-08 16:35:01 -08005045 if (off + size > t->size) {
Yonghong Song9c5f8a102020-05-09 10:59:16 -07005046 /* If the last element is a variable size array, we may
5047 * need to relax the rule.
5048 */
5049 struct btf_array *array_elem;
5050
5051 if (vlen == 0)
5052 goto error;
5053
5054 member = btf_type_member(t) + vlen - 1;
Andrii Nakryiko22dc4a02020-12-03 12:46:29 -08005055 mtype = btf_type_skip_modifiers(btf, member->type,
Yonghong Song9c5f8a102020-05-09 10:59:16 -07005056 NULL);
5057 if (!btf_type_is_array(mtype))
5058 goto error;
5059
5060 array_elem = (struct btf_array *)(mtype + 1);
5061 if (array_elem->nelems != 0)
5062 goto error;
5063
Alexei Starovoitov8293eb92021-12-01 10:10:25 -08005064 moff = __btf_member_bit_offset(t, member) / 8;
Yonghong Song9c5f8a102020-05-09 10:59:16 -07005065 if (off < moff)
5066 goto error;
5067
5068 /* Only allow structure for now, can be relaxed for
5069 * other types later.
5070 */
Andrii Nakryiko22dc4a02020-12-03 12:46:29 -08005071 t = btf_type_skip_modifiers(btf, array_elem->type,
Jiri Olsadafe58f2020-08-25 21:21:16 +02005072 NULL);
5073 if (!btf_type_is_struct(t))
Yonghong Song9c5f8a102020-05-09 10:59:16 -07005074 goto error;
5075
Jiri Olsadafe58f2020-08-25 21:21:16 +02005076 off = (off - moff) % t->size;
5077 goto again;
Yonghong Song9c5f8a102020-05-09 10:59:16 -07005078
5079error:
Martin KaFai Lau976aba02020-01-08 16:35:01 -08005080 bpf_log(log, "access beyond struct %s at off %u size %u\n",
5081 tname, off, size);
5082 return -EACCES;
5083 }
Alexei Starovoitov9e15db62019-10-15 20:25:00 -07005084
Martin KaFai Lau976aba02020-01-08 16:35:01 -08005085 for_each_member(i, t, member) {
Martin KaFai Lau7e3617a2019-11-07 10:09:03 -08005086 /* offset of the field in bytes */
Alexei Starovoitov8293eb92021-12-01 10:10:25 -08005087 moff = __btf_member_bit_offset(t, member) / 8;
Martin KaFai Lau7e3617a2019-11-07 10:09:03 -08005088 if (off + size <= moff)
Alexei Starovoitov9e15db62019-10-15 20:25:00 -07005089 /* won't find anything, field is already too far */
5090 break;
Martin KaFai Lau976aba02020-01-08 16:35:01 -08005091
Alexei Starovoitov8293eb92021-12-01 10:10:25 -08005092 if (__btf_member_bitfield_size(t, member)) {
5093 u32 end_bit = __btf_member_bit_offset(t, member) +
5094 __btf_member_bitfield_size(t, member);
Martin KaFai Lau976aba02020-01-08 16:35:01 -08005095
5096 /* off <= moff instead of off == moff because clang
5097 * does not generate a BTF member for anonymous
5098 * bitfield like the ":16" here:
5099 * struct {
5100 * int :16;
5101 * int x:8;
5102 * };
5103 */
5104 if (off <= moff &&
5105 BITS_ROUNDUP_BYTES(end_bit) <= off + size)
Jiri Olsa1c6d28a2020-08-25 21:21:17 +02005106 return WALK_SCALAR;
Martin KaFai Lau976aba02020-01-08 16:35:01 -08005107
5108 /* off may be accessing a following member
5109 *
5110 * or
5111 *
5112 * Doing partial access at either end of this
5113 * bitfield. Continue on this case also to
5114 * treat it as not accessing this bitfield
5115 * and eventually error out as field not
5116 * found to keep it simple.
5117 * It could be relaxed if there was a legit
5118 * partial access case later.
5119 */
5120 continue;
5121 }
5122
Martin KaFai Lau7e3617a2019-11-07 10:09:03 -08005123 /* In case of "off" is pointing to holes of a struct */
5124 if (off < moff)
Martin KaFai Lau976aba02020-01-08 16:35:01 -08005125 break;
Alexei Starovoitov9e15db62019-10-15 20:25:00 -07005126
5127 /* type of the field */
Jiri Olsa1c6d28a2020-08-25 21:21:17 +02005128 mid = member->type;
Andrii Nakryiko22dc4a02020-12-03 12:46:29 -08005129 mtype = btf_type_by_id(btf, member->type);
5130 mname = __btf_name_by_offset(btf, member->name_off);
Alexei Starovoitov9e15db62019-10-15 20:25:00 -07005131
Andrii Nakryiko22dc4a02020-12-03 12:46:29 -08005132 mtype = __btf_resolve_size(btf, mtype, &msize,
Jiri Olsa1c6d28a2020-08-25 21:21:17 +02005133 &elem_type, &elem_id, &total_nelems,
5134 &mid);
Martin KaFai Lau7e3617a2019-11-07 10:09:03 -08005135 if (IS_ERR(mtype)) {
Alexei Starovoitov9e15db62019-10-15 20:25:00 -07005136 bpf_log(log, "field %s doesn't have size\n", mname);
5137 return -EFAULT;
5138 }
Martin KaFai Lau7e3617a2019-11-07 10:09:03 -08005139
5140 mtrue_end = moff + msize;
5141 if (off >= mtrue_end)
Alexei Starovoitov9e15db62019-10-15 20:25:00 -07005142 /* no overlap with member, keep iterating */
5143 continue;
Martin KaFai Lau7e3617a2019-11-07 10:09:03 -08005144
5145 if (btf_type_is_array(mtype)) {
5146 u32 elem_idx;
5147
Jiri Olsa62983992020-08-25 21:21:13 +02005148 /* __btf_resolve_size() above helps to
Martin KaFai Lau7e3617a2019-11-07 10:09:03 -08005149 * linearize a multi-dimensional array.
5150 *
5151 * The logic here is treating an array
5152 * in a struct as the following way:
5153 *
5154 * struct outer {
5155 * struct inner array[2][2];
5156 * };
5157 *
5158 * looks like:
5159 *
5160 * struct outer {
5161 * struct inner array_elem0;
5162 * struct inner array_elem1;
5163 * struct inner array_elem2;
5164 * struct inner array_elem3;
5165 * };
5166 *
5167 * When accessing outer->array[1][0], it moves
5168 * moff to "array_elem2", set mtype to
5169 * "struct inner", and msize also becomes
5170 * sizeof(struct inner). Then most of the
5171 * remaining logic will fall through without
5172 * caring the current member is an array or
5173 * not.
5174 *
5175 * Unlike mtype/msize/moff, mtrue_end does not
5176 * change. The naming difference ("_true") tells
5177 * that it is not always corresponding to
5178 * the current mtype/msize/moff.
5179 * It is the true end of the current
5180 * member (i.e. array in this case). That
5181 * will allow an int array to be accessed like
5182 * a scratch space,
5183 * i.e. allow access beyond the size of
5184 * the array's element as long as it is
5185 * within the mtrue_end boundary.
5186 */
5187
5188 /* skip empty array */
5189 if (moff == mtrue_end)
5190 continue;
5191
5192 msize /= total_nelems;
5193 elem_idx = (off - moff) / msize;
5194 moff += elem_idx * msize;
5195 mtype = elem_type;
Jiri Olsa1c6d28a2020-08-25 21:21:17 +02005196 mid = elem_id;
Martin KaFai Lau7e3617a2019-11-07 10:09:03 -08005197 }
5198
Alexei Starovoitov9e15db62019-10-15 20:25:00 -07005199 /* the 'off' we're looking for is either equal to start
5200 * of this field or inside of this struct
5201 */
5202 if (btf_type_is_struct(mtype)) {
5203 /* our field must be inside that union or struct */
5204 t = mtype;
5205
Jiri Olsa1c6d28a2020-08-25 21:21:17 +02005206 /* return if the offset matches the member offset */
5207 if (off == moff) {
5208 *next_btf_id = mid;
5209 return WALK_STRUCT;
5210 }
5211
Alexei Starovoitov9e15db62019-10-15 20:25:00 -07005212 /* adjust offset we're looking for */
Martin KaFai Lau7e3617a2019-11-07 10:09:03 -08005213 off -= moff;
Alexei Starovoitov9e15db62019-10-15 20:25:00 -07005214 goto again;
5215 }
Alexei Starovoitov9e15db62019-10-15 20:25:00 -07005216
5217 if (btf_type_is_ptr(mtype)) {
5218 const struct btf_type *stype;
Alexei Starovoitov257af632020-01-31 16:03:14 -08005219 u32 id;
Alexei Starovoitov9e15db62019-10-15 20:25:00 -07005220
Martin KaFai Lau7e3617a2019-11-07 10:09:03 -08005221 if (msize != size || off != moff) {
5222 bpf_log(log,
5223 "cannot access ptr member %s with moff %u in struct %s with off %u size %u\n",
5224 mname, moff, tname, off, size);
5225 return -EACCES;
5226 }
Andrii Nakryiko22dc4a02020-12-03 12:46:29 -08005227 stype = btf_type_skip_modifiers(btf, mtype->type, &id);
Alexei Starovoitov9e15db62019-10-15 20:25:00 -07005228 if (btf_type_is_struct(stype)) {
Alexei Starovoitov257af632020-01-31 16:03:14 -08005229 *next_btf_id = id;
Jiri Olsa1c6d28a2020-08-25 21:21:17 +02005230 return WALK_PTR;
Alexei Starovoitov9e15db62019-10-15 20:25:00 -07005231 }
5232 }
Martin KaFai Lau7e3617a2019-11-07 10:09:03 -08005233
5234 /* Allow more flexible access within an int as long as
5235 * it is within mtrue_end.
5236 * Since mtrue_end could be the end of an array,
5237 * that also allows using an array of int as a scratch
5238 * space. e.g. skb->cb[].
5239 */
5240 if (off + size > mtrue_end) {
5241 bpf_log(log,
5242 "access beyond the end of member %s (mend:%u) in struct %s with off %u size %u\n",
5243 mname, mtrue_end, tname, off, size);
5244 return -EACCES;
5245 }
5246
Jiri Olsa1c6d28a2020-08-25 21:21:17 +02005247 return WALK_SCALAR;
Alexei Starovoitov9e15db62019-10-15 20:25:00 -07005248 }
5249 bpf_log(log, "struct %s doesn't have field at offset %d\n", tname, off);
5250 return -EINVAL;
5251}
5252
Andrii Nakryiko22dc4a02020-12-03 12:46:29 -08005253int btf_struct_access(struct bpf_verifier_log *log, const struct btf *btf,
Jiri Olsa1c6d28a2020-08-25 21:21:17 +02005254 const struct btf_type *t, int off, int size,
5255 enum bpf_access_type atype __maybe_unused,
5256 u32 *next_btf_id)
5257{
5258 int err;
5259 u32 id;
5260
5261 do {
Andrii Nakryiko22dc4a02020-12-03 12:46:29 -08005262 err = btf_struct_walk(log, btf, t, off, size, &id);
Jiri Olsa1c6d28a2020-08-25 21:21:17 +02005263
5264 switch (err) {
5265 case WALK_PTR:
5266 /* If we found the pointer or scalar on t+off,
5267 * we're done.
5268 */
5269 *next_btf_id = id;
5270 return PTR_TO_BTF_ID;
5271 case WALK_SCALAR:
5272 return SCALAR_VALUE;
5273 case WALK_STRUCT:
5274 /* We found nested struct, so continue the search
5275 * by diving in it. At this point the offset is
5276 * aligned with the new type, so set it to 0.
5277 */
Andrii Nakryiko22dc4a02020-12-03 12:46:29 -08005278 t = btf_type_by_id(btf, id);
Jiri Olsa1c6d28a2020-08-25 21:21:17 +02005279 off = 0;
5280 break;
5281 default:
5282 /* It's either error or unknown return value..
5283 * scream and leave.
5284 */
5285 if (WARN_ONCE(err > 0, "unknown btf_struct_walk return value"))
5286 return -EINVAL;
5287 return err;
5288 }
5289 } while (t);
5290
5291 return -EINVAL;
5292}
5293
Andrii Nakryiko22dc4a02020-12-03 12:46:29 -08005294/* Check that two BTF types, each specified as an BTF object + id, are exactly
5295 * the same. Trivial ID check is not enough due to module BTFs, because we can
5296 * end up with two different module BTFs, but IDs point to the common type in
5297 * vmlinux BTF.
5298 */
5299static bool btf_types_are_same(const struct btf *btf1, u32 id1,
5300 const struct btf *btf2, u32 id2)
5301{
5302 if (id1 != id2)
5303 return false;
5304 if (btf1 == btf2)
5305 return true;
5306 return btf_type_by_id(btf1, id1) == btf_type_by_id(btf2, id2);
5307}
5308
Jiri Olsafaaf4a72020-08-25 21:21:18 +02005309bool btf_struct_ids_match(struct bpf_verifier_log *log,
Andrii Nakryiko22dc4a02020-12-03 12:46:29 -08005310 const struct btf *btf, u32 id, int off,
5311 const struct btf *need_btf, u32 need_type_id)
Jiri Olsafaaf4a72020-08-25 21:21:18 +02005312{
5313 const struct btf_type *type;
5314 int err;
5315
5316 /* Are we already done? */
Andrii Nakryiko22dc4a02020-12-03 12:46:29 -08005317 if (off == 0 && btf_types_are_same(btf, id, need_btf, need_type_id))
Jiri Olsafaaf4a72020-08-25 21:21:18 +02005318 return true;
5319
5320again:
Andrii Nakryiko22dc4a02020-12-03 12:46:29 -08005321 type = btf_type_by_id(btf, id);
Jiri Olsafaaf4a72020-08-25 21:21:18 +02005322 if (!type)
5323 return false;
Andrii Nakryiko22dc4a02020-12-03 12:46:29 -08005324 err = btf_struct_walk(log, btf, type, off, 1, &id);
Jiri Olsafaaf4a72020-08-25 21:21:18 +02005325 if (err != WALK_STRUCT)
5326 return false;
5327
5328 /* We found nested struct object. If it matches
5329 * the requested ID, we're done. Otherwise let's
5330 * continue the search with offset 0 in the new
5331 * type.
5332 */
Andrii Nakryiko22dc4a02020-12-03 12:46:29 -08005333 if (!btf_types_are_same(btf, id, need_btf, need_type_id)) {
Jiri Olsafaaf4a72020-08-25 21:21:18 +02005334 off = 0;
5335 goto again;
5336 }
5337
5338 return true;
5339}
5340
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08005341static int __get_type_size(struct btf *btf, u32 btf_id,
5342 const struct btf_type **bad_type)
5343{
5344 const struct btf_type *t;
5345
5346 if (!btf_id)
5347 /* void */
5348 return 0;
5349 t = btf_type_by_id(btf, btf_id);
5350 while (t && btf_type_is_modifier(t))
5351 t = btf_type_by_id(btf, t->type);
Alexei Starovoitovd0f01042019-11-26 15:01:06 -08005352 if (!t) {
Andrii Nakryiko951bb642020-11-09 17:19:28 -08005353 *bad_type = btf_type_by_id(btf, 0);
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08005354 return -EINVAL;
Alexei Starovoitovd0f01042019-11-26 15:01:06 -08005355 }
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08005356 if (btf_type_is_ptr(t))
5357 /* kernel size of pointer. Not BPF's size of pointer*/
5358 return sizeof(void *);
5359 if (btf_type_is_int(t) || btf_type_is_enum(t))
5360 return t->size;
5361 *bad_type = t;
5362 return -EINVAL;
5363}
5364
5365int btf_distill_func_proto(struct bpf_verifier_log *log,
5366 struct btf *btf,
5367 const struct btf_type *func,
5368 const char *tname,
5369 struct btf_func_model *m)
5370{
5371 const struct btf_param *args;
5372 const struct btf_type *t;
5373 u32 i, nargs;
5374 int ret;
5375
Alexei Starovoitov5b92a282019-11-14 10:57:17 -08005376 if (!func) {
5377 /* BTF function prototype doesn't match the verifier types.
Dmitrii Banshchikov523a4cf2021-02-26 00:26:29 +04005378 * Fall back to MAX_BPF_FUNC_REG_ARGS u64 args.
Alexei Starovoitov5b92a282019-11-14 10:57:17 -08005379 */
Dmitrii Banshchikov523a4cf2021-02-26 00:26:29 +04005380 for (i = 0; i < MAX_BPF_FUNC_REG_ARGS; i++)
Alexei Starovoitov5b92a282019-11-14 10:57:17 -08005381 m->arg_size[i] = 8;
5382 m->ret_size = 8;
Dmitrii Banshchikov523a4cf2021-02-26 00:26:29 +04005383 m->nr_args = MAX_BPF_FUNC_REG_ARGS;
Alexei Starovoitov5b92a282019-11-14 10:57:17 -08005384 return 0;
5385 }
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08005386 args = (const struct btf_param *)(func + 1);
5387 nargs = btf_type_vlen(func);
5388 if (nargs >= MAX_BPF_FUNC_ARGS) {
5389 bpf_log(log,
5390 "The function %s has %d arguments. Too many.\n",
5391 tname, nargs);
5392 return -EINVAL;
5393 }
5394 ret = __get_type_size(btf, func->type, &t);
5395 if (ret < 0) {
5396 bpf_log(log,
5397 "The function %s return type %s is unsupported.\n",
5398 tname, btf_kind_str[BTF_INFO_KIND(t->info)]);
5399 return -EINVAL;
5400 }
5401 m->ret_size = ret;
5402
5403 for (i = 0; i < nargs; i++) {
Jiri Olsa31379392021-05-05 15:25:29 +02005404 if (i == nargs - 1 && args[i].type == 0) {
5405 bpf_log(log,
5406 "The function %s with variable args is unsupported.\n",
5407 tname);
5408 return -EINVAL;
5409 }
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08005410 ret = __get_type_size(btf, args[i].type, &t);
5411 if (ret < 0) {
5412 bpf_log(log,
5413 "The function %s arg%d type %s is unsupported.\n",
5414 tname, i, btf_kind_str[BTF_INFO_KIND(t->info)]);
5415 return -EINVAL;
5416 }
Jiri Olsa31379392021-05-05 15:25:29 +02005417 if (ret == 0) {
5418 bpf_log(log,
5419 "The function %s has malformed void argument.\n",
5420 tname);
5421 return -EINVAL;
5422 }
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08005423 m->arg_size[i] = ret;
5424 }
5425 m->nr_args = nargs;
5426 return 0;
5427}
5428
Alexei Starovoitovbe8704f2020-01-20 16:53:46 -08005429/* Compare BTFs of two functions assuming only scalars and pointers to context.
5430 * t1 points to BTF_KIND_FUNC in btf1
5431 * t2 points to BTF_KIND_FUNC in btf2
5432 * Returns:
5433 * EINVAL - function prototype mismatch
5434 * EFAULT - verifier bug
5435 * 0 - 99% match. The last 1% is validated by the verifier.
5436 */
Hongbo Yao2bf0eb92020-02-10 09:14:41 +08005437static int btf_check_func_type_match(struct bpf_verifier_log *log,
5438 struct btf *btf1, const struct btf_type *t1,
5439 struct btf *btf2, const struct btf_type *t2)
Alexei Starovoitovbe8704f2020-01-20 16:53:46 -08005440{
5441 const struct btf_param *args1, *args2;
5442 const char *fn1, *fn2, *s1, *s2;
5443 u32 nargs1, nargs2, i;
5444
5445 fn1 = btf_name_by_offset(btf1, t1->name_off);
5446 fn2 = btf_name_by_offset(btf2, t2->name_off);
5447
5448 if (btf_func_linkage(t1) != BTF_FUNC_GLOBAL) {
5449 bpf_log(log, "%s() is not a global function\n", fn1);
5450 return -EINVAL;
5451 }
5452 if (btf_func_linkage(t2) != BTF_FUNC_GLOBAL) {
5453 bpf_log(log, "%s() is not a global function\n", fn2);
5454 return -EINVAL;
5455 }
5456
5457 t1 = btf_type_by_id(btf1, t1->type);
5458 if (!t1 || !btf_type_is_func_proto(t1))
5459 return -EFAULT;
5460 t2 = btf_type_by_id(btf2, t2->type);
5461 if (!t2 || !btf_type_is_func_proto(t2))
5462 return -EFAULT;
5463
5464 args1 = (const struct btf_param *)(t1 + 1);
5465 nargs1 = btf_type_vlen(t1);
5466 args2 = (const struct btf_param *)(t2 + 1);
5467 nargs2 = btf_type_vlen(t2);
5468
5469 if (nargs1 != nargs2) {
5470 bpf_log(log, "%s() has %d args while %s() has %d args\n",
5471 fn1, nargs1, fn2, nargs2);
5472 return -EINVAL;
5473 }
5474
5475 t1 = btf_type_skip_modifiers(btf1, t1->type, NULL);
5476 t2 = btf_type_skip_modifiers(btf2, t2->type, NULL);
5477 if (t1->info != t2->info) {
5478 bpf_log(log,
5479 "Return type %s of %s() doesn't match type %s of %s()\n",
5480 btf_type_str(t1), fn1,
5481 btf_type_str(t2), fn2);
5482 return -EINVAL;
5483 }
5484
5485 for (i = 0; i < nargs1; i++) {
5486 t1 = btf_type_skip_modifiers(btf1, args1[i].type, NULL);
5487 t2 = btf_type_skip_modifiers(btf2, args2[i].type, NULL);
5488
5489 if (t1->info != t2->info) {
5490 bpf_log(log, "arg%d in %s() is %s while %s() has %s\n",
5491 i, fn1, btf_type_str(t1),
5492 fn2, btf_type_str(t2));
5493 return -EINVAL;
5494 }
5495 if (btf_type_has_size(t1) && t1->size != t2->size) {
5496 bpf_log(log,
5497 "arg%d in %s() has size %d while %s() has %d\n",
5498 i, fn1, t1->size,
5499 fn2, t2->size);
5500 return -EINVAL;
5501 }
5502
5503 /* global functions are validated with scalars and pointers
5504 * to context only. And only global functions can be replaced.
5505 * Hence type check only those types.
5506 */
5507 if (btf_type_is_int(t1) || btf_type_is_enum(t1))
5508 continue;
5509 if (!btf_type_is_ptr(t1)) {
5510 bpf_log(log,
5511 "arg%d in %s() has unrecognized type\n",
5512 i, fn1);
5513 return -EINVAL;
5514 }
5515 t1 = btf_type_skip_modifiers(btf1, t1->type, NULL);
5516 t2 = btf_type_skip_modifiers(btf2, t2->type, NULL);
5517 if (!btf_type_is_struct(t1)) {
5518 bpf_log(log,
5519 "arg%d in %s() is not a pointer to context\n",
5520 i, fn1);
5521 return -EINVAL;
5522 }
5523 if (!btf_type_is_struct(t2)) {
5524 bpf_log(log,
5525 "arg%d in %s() is not a pointer to context\n",
5526 i, fn2);
5527 return -EINVAL;
5528 }
5529 /* This is an optional check to make program writing easier.
5530 * Compare names of structs and report an error to the user.
5531 * btf_prepare_func_args() already checked that t2 struct
5532 * is a context type. btf_prepare_func_args() will check
5533 * later that t1 struct is a context type as well.
5534 */
5535 s1 = btf_name_by_offset(btf1, t1->name_off);
5536 s2 = btf_name_by_offset(btf2, t2->name_off);
5537 if (strcmp(s1, s2)) {
5538 bpf_log(log,
5539 "arg%d %s(struct %s *) doesn't match %s(struct %s *)\n",
5540 i, fn1, s1, fn2, s2);
5541 return -EINVAL;
5542 }
5543 }
5544 return 0;
5545}
5546
5547/* Compare BTFs of given program with BTF of target program */
Toke Høiland-Jørgensenefc68152020-09-25 23:25:01 +02005548int btf_check_type_match(struct bpf_verifier_log *log, const struct bpf_prog *prog,
Alexei Starovoitovbe8704f2020-01-20 16:53:46 -08005549 struct btf *btf2, const struct btf_type *t2)
5550{
5551 struct btf *btf1 = prog->aux->btf;
5552 const struct btf_type *t1;
5553 u32 btf_id = 0;
5554
5555 if (!prog->aux->func_info) {
Toke Høiland-Jørgensenefc68152020-09-25 23:25:01 +02005556 bpf_log(log, "Program extension requires BTF\n");
Alexei Starovoitovbe8704f2020-01-20 16:53:46 -08005557 return -EINVAL;
5558 }
5559
5560 btf_id = prog->aux->func_info[0].type_id;
5561 if (!btf_id)
5562 return -EFAULT;
5563
5564 t1 = btf_type_by_id(btf1, btf_id);
5565 if (!t1 || !btf_type_is_func(t1))
5566 return -EFAULT;
5567
Toke Høiland-Jørgensenefc68152020-09-25 23:25:01 +02005568 return btf_check_func_type_match(log, btf1, t1, btf2, t2);
Alexei Starovoitovbe8704f2020-01-20 16:53:46 -08005569}
5570
Martin KaFai Laue6ac2452021-03-24 18:51:42 -07005571static u32 *reg2btf_ids[__BPF_REG_TYPE_MAX] = {
5572#ifdef CONFIG_NET
5573 [PTR_TO_SOCKET] = &btf_sock_ids[BTF_SOCK_TYPE_SOCK],
5574 [PTR_TO_SOCK_COMMON] = &btf_sock_ids[BTF_SOCK_TYPE_SOCK_COMMON],
5575 [PTR_TO_TCP_SOCK] = &btf_sock_ids[BTF_SOCK_TYPE_TCP],
5576#endif
5577};
5578
Kumar Kartikeya Dwivedi3363bd02021-12-17 07:20:24 +05305579/* Returns true if struct is composed of scalars, 4 levels of nesting allowed */
5580static bool __btf_type_is_scalar_struct(struct bpf_verifier_log *log,
5581 const struct btf *btf,
5582 const struct btf_type *t, int rec)
5583{
5584 const struct btf_type *member_type;
5585 const struct btf_member *member;
5586 u32 i;
5587
5588 if (!btf_type_is_struct(t))
5589 return false;
5590
5591 for_each_member(i, t, member) {
5592 const struct btf_array *array;
5593
5594 member_type = btf_type_skip_modifiers(btf, member->type, NULL);
5595 if (btf_type_is_struct(member_type)) {
5596 if (rec >= 3) {
5597 bpf_log(log, "max struct nesting depth exceeded\n");
5598 return false;
5599 }
5600 if (!__btf_type_is_scalar_struct(log, btf, member_type, rec + 1))
5601 return false;
5602 continue;
5603 }
5604 if (btf_type_is_array(member_type)) {
5605 array = btf_type_array(member_type);
5606 if (!array->nelems)
5607 return false;
5608 member_type = btf_type_skip_modifiers(btf, array->type, NULL);
5609 if (!btf_type_is_scalar(member_type))
5610 return false;
5611 continue;
5612 }
5613 if (!btf_type_is_scalar(member_type))
5614 return false;
5615 }
5616 return true;
5617}
5618
Martin KaFai Lau34747c42021-03-24 18:51:36 -07005619static int btf_check_func_arg_match(struct bpf_verifier_env *env,
5620 const struct btf *btf, u32 func_id,
5621 struct bpf_reg_state *regs,
5622 bool ptr_to_mem_ok)
5623{
5624 struct bpf_verifier_log *log = &env->log;
Kumar Kartikeya Dwivedi3363bd02021-12-17 07:20:24 +05305625 bool is_kfunc = btf_is_kernel(btf);
Martin KaFai Lau34747c42021-03-24 18:51:36 -07005626 const char *func_name, *ref_tname;
5627 const struct btf_type *t, *ref_t;
5628 const struct btf_param *args;
Martin KaFai Laue6ac2452021-03-24 18:51:42 -07005629 u32 i, nargs, ref_id;
Martin KaFai Lau34747c42021-03-24 18:51:36 -07005630
5631 t = btf_type_by_id(btf, func_id);
5632 if (!t || !btf_type_is_func(t)) {
5633 /* These checks were already done by the verifier while loading
Martin KaFai Laue6ac2452021-03-24 18:51:42 -07005634 * struct bpf_func_info or in add_kfunc_call().
Martin KaFai Lau34747c42021-03-24 18:51:36 -07005635 */
5636 bpf_log(log, "BTF of func_id %u doesn't point to KIND_FUNC\n",
5637 func_id);
5638 return -EFAULT;
5639 }
5640 func_name = btf_name_by_offset(btf, t->name_off);
5641
5642 t = btf_type_by_id(btf, t->type);
5643 if (!t || !btf_type_is_func_proto(t)) {
5644 bpf_log(log, "Invalid BTF of func %s\n", func_name);
5645 return -EFAULT;
5646 }
5647 args = (const struct btf_param *)(t + 1);
5648 nargs = btf_type_vlen(t);
5649 if (nargs > MAX_BPF_FUNC_REG_ARGS) {
5650 bpf_log(log, "Function %s has %d > %d args\n", func_name, nargs,
5651 MAX_BPF_FUNC_REG_ARGS);
5652 return -EINVAL;
5653 }
5654
5655 /* check that BTF function arguments match actual types that the
5656 * verifier sees.
5657 */
5658 for (i = 0; i < nargs; i++) {
5659 u32 regno = i + 1;
5660 struct bpf_reg_state *reg = &regs[regno];
5661
5662 t = btf_type_skip_modifiers(btf, args[i].type, NULL);
5663 if (btf_type_is_scalar(t)) {
5664 if (reg->type == SCALAR_VALUE)
5665 continue;
5666 bpf_log(log, "R%d is not a scalar\n", regno);
5667 return -EINVAL;
5668 }
5669
5670 if (!btf_type_is_ptr(t)) {
5671 bpf_log(log, "Unrecognized arg#%d type %s\n",
5672 i, btf_type_str(t));
5673 return -EINVAL;
5674 }
5675
Martin KaFai Laue6ac2452021-03-24 18:51:42 -07005676 ref_t = btf_type_skip_modifiers(btf, t->type, &ref_id);
Martin KaFai Lau34747c42021-03-24 18:51:36 -07005677 ref_tname = btf_name_by_offset(btf, ref_t->name_off);
Kumar Kartikeya Dwivedi3363bd02021-12-17 07:20:24 +05305678 if (btf_get_prog_ctx_type(log, btf, t,
5679 env->prog->type, i)) {
5680 /* If function expects ctx type in BTF check that caller
5681 * is passing PTR_TO_CTX.
5682 */
5683 if (reg->type != PTR_TO_CTX) {
5684 bpf_log(log,
5685 "arg#%d expected pointer to ctx, but got %s\n",
5686 i, btf_type_str(t));
5687 return -EINVAL;
5688 }
Daniel Borkmannbe80a1d2022-01-10 14:05:49 +00005689 if (check_ptr_off_reg(env, reg, regno))
Kumar Kartikeya Dwivedi3363bd02021-12-17 07:20:24 +05305690 return -EINVAL;
5691 } else if (is_kfunc && (reg->type == PTR_TO_BTF_ID || reg2btf_ids[reg->type])) {
Martin KaFai Laue6ac2452021-03-24 18:51:42 -07005692 const struct btf_type *reg_ref_t;
5693 const struct btf *reg_btf;
5694 const char *reg_ref_tname;
5695 u32 reg_ref_id;
5696
5697 if (!btf_type_is_struct(ref_t)) {
5698 bpf_log(log, "kernel function %s args#%d pointer type %s %s is not supported\n",
5699 func_name, i, btf_type_str(ref_t),
5700 ref_tname);
5701 return -EINVAL;
5702 }
5703
5704 if (reg->type == PTR_TO_BTF_ID) {
5705 reg_btf = reg->btf;
5706 reg_ref_id = reg->btf_id;
Kumar Kartikeya Dwivedi3363bd02021-12-17 07:20:24 +05305707 } else {
Martin KaFai Laue6ac2452021-03-24 18:51:42 -07005708 reg_btf = btf_vmlinux;
5709 reg_ref_id = *reg2btf_ids[reg->type];
Martin KaFai Laue6ac2452021-03-24 18:51:42 -07005710 }
5711
5712 reg_ref_t = btf_type_skip_modifiers(reg_btf, reg_ref_id,
5713 &reg_ref_id);
5714 reg_ref_tname = btf_name_by_offset(reg_btf,
5715 reg_ref_t->name_off);
5716 if (!btf_struct_ids_match(log, reg_btf, reg_ref_id,
5717 reg->off, btf, ref_id)) {
5718 bpf_log(log, "kernel function %s args#%d expected pointer to %s %s but R%d has a pointer to %s %s\n",
5719 func_name, i,
5720 btf_type_str(ref_t), ref_tname,
5721 regno, btf_type_str(reg_ref_t),
5722 reg_ref_tname);
5723 return -EINVAL;
5724 }
Martin KaFai Lau34747c42021-03-24 18:51:36 -07005725 } else if (ptr_to_mem_ok) {
5726 const struct btf_type *resolve_ret;
5727 u32 type_size;
5728
Kumar Kartikeya Dwivedi3363bd02021-12-17 07:20:24 +05305729 if (is_kfunc) {
5730 /* Permit pointer to mem, but only when argument
5731 * type is pointer to scalar, or struct composed
5732 * (recursively) of scalars.
5733 */
5734 if (!btf_type_is_scalar(ref_t) &&
5735 !__btf_type_is_scalar_struct(log, btf, ref_t, 0)) {
5736 bpf_log(log,
5737 "arg#%d pointer type %s %s must point to scalar or struct with scalar\n",
5738 i, btf_type_str(ref_t), ref_tname);
5739 return -EINVAL;
5740 }
5741 }
5742
Martin KaFai Lau34747c42021-03-24 18:51:36 -07005743 resolve_ret = btf_resolve_size(btf, ref_t, &type_size);
5744 if (IS_ERR(resolve_ret)) {
5745 bpf_log(log,
5746 "arg#%d reference type('%s %s') size cannot be determined: %ld\n",
5747 i, btf_type_str(ref_t), ref_tname,
5748 PTR_ERR(resolve_ret));
5749 return -EINVAL;
5750 }
5751
5752 if (check_mem_reg(env, reg, regno, type_size))
5753 return -EINVAL;
5754 } else {
Kumar Kartikeya Dwivedi3363bd02021-12-17 07:20:24 +05305755 bpf_log(log, "reg type unsupported for arg#%d %sfunction %s#%d\n", i,
5756 is_kfunc ? "kernel " : "", func_name, func_id);
Martin KaFai Lau34747c42021-03-24 18:51:36 -07005757 return -EINVAL;
5758 }
5759 }
5760
5761 return 0;
5762}
5763
Alexei Starovoitov51c39bb2020-01-09 22:41:20 -08005764/* Compare BTF of a function with given bpf_reg_state.
5765 * Returns:
5766 * EFAULT - there is a verifier bug. Abort verification.
5767 * EINVAL - there is a type mismatch or BTF is not available.
5768 * 0 - BTF matches with what bpf_reg_state expects.
5769 * Only PTR_TO_CTX and SCALAR_VALUE states are recognized.
5770 */
Martin KaFai Lau34747c42021-03-24 18:51:36 -07005771int btf_check_subprog_arg_match(struct bpf_verifier_env *env, int subprog,
5772 struct bpf_reg_state *regs)
Alexei Starovoitov8c1b6e62019-11-14 10:57:16 -08005773{
Alexei Starovoitov8c1b6e62019-11-14 10:57:16 -08005774 struct bpf_prog *prog = env->prog;
5775 struct btf *btf = prog->aux->btf;
Dmitrii Banshchikove5069b9c2021-02-13 00:56:41 +04005776 bool is_global;
Martin KaFai Lau34747c42021-03-24 18:51:36 -07005777 u32 btf_id;
5778 int err;
Alexei Starovoitov8c1b6e62019-11-14 10:57:16 -08005779
5780 if (!prog->aux->func_info)
Alexei Starovoitov51c39bb2020-01-09 22:41:20 -08005781 return -EINVAL;
Alexei Starovoitov8c1b6e62019-11-14 10:57:16 -08005782
5783 btf_id = prog->aux->func_info[subprog].type_id;
5784 if (!btf_id)
Alexei Starovoitov51c39bb2020-01-09 22:41:20 -08005785 return -EFAULT;
Alexei Starovoitov8c1b6e62019-11-14 10:57:16 -08005786
5787 if (prog->aux->func_info_aux[subprog].unreliable)
Alexei Starovoitov51c39bb2020-01-09 22:41:20 -08005788 return -EINVAL;
Alexei Starovoitov8c1b6e62019-11-14 10:57:16 -08005789
Dmitrii Banshchikove5069b9c2021-02-13 00:56:41 +04005790 is_global = prog->aux->func_info_aux[subprog].linkage == BTF_FUNC_GLOBAL;
Martin KaFai Lau34747c42021-03-24 18:51:36 -07005791 err = btf_check_func_arg_match(env, btf, btf_id, regs, is_global);
Dmitrii Banshchikovfeb4adf2021-02-13 00:56:39 +04005792
Alexei Starovoitov51c39bb2020-01-09 22:41:20 -08005793 /* Compiler optimizations can remove arguments from static functions
5794 * or mismatched type can be passed into a global function.
5795 * In such cases mark the function as unreliable from BTF point of view.
5796 */
Martin KaFai Lau34747c42021-03-24 18:51:36 -07005797 if (err)
5798 prog->aux->func_info_aux[subprog].unreliable = true;
5799 return err;
Alexei Starovoitov51c39bb2020-01-09 22:41:20 -08005800}
5801
Martin KaFai Laue6ac2452021-03-24 18:51:42 -07005802int btf_check_kfunc_arg_match(struct bpf_verifier_env *env,
5803 const struct btf *btf, u32 func_id,
5804 struct bpf_reg_state *regs)
5805{
Kumar Kartikeya Dwivedi3363bd02021-12-17 07:20:24 +05305806 return btf_check_func_arg_match(env, btf, func_id, regs, true);
Martin KaFai Laue6ac2452021-03-24 18:51:42 -07005807}
5808
Alexei Starovoitov51c39bb2020-01-09 22:41:20 -08005809/* Convert BTF of a function into bpf_reg_state if possible
5810 * Returns:
5811 * EFAULT - there is a verifier bug. Abort verification.
5812 * EINVAL - cannot convert BTF.
5813 * 0 - Successfully converted BTF into bpf_reg_state
5814 * (either PTR_TO_CTX or SCALAR_VALUE).
5815 */
5816int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog,
Dmitrii Banshchikovfeb4adf2021-02-13 00:56:39 +04005817 struct bpf_reg_state *regs)
Alexei Starovoitov51c39bb2020-01-09 22:41:20 -08005818{
5819 struct bpf_verifier_log *log = &env->log;
5820 struct bpf_prog *prog = env->prog;
Alexei Starovoitovbe8704f2020-01-20 16:53:46 -08005821 enum bpf_prog_type prog_type = prog->type;
Alexei Starovoitov51c39bb2020-01-09 22:41:20 -08005822 struct btf *btf = prog->aux->btf;
5823 const struct btf_param *args;
Dmitrii Banshchikove5069b9c2021-02-13 00:56:41 +04005824 const struct btf_type *t, *ref_t;
Alexei Starovoitov51c39bb2020-01-09 22:41:20 -08005825 u32 i, nargs, btf_id;
5826 const char *tname;
5827
5828 if (!prog->aux->func_info ||
5829 prog->aux->func_info_aux[subprog].linkage != BTF_FUNC_GLOBAL) {
5830 bpf_log(log, "Verifier bug\n");
5831 return -EFAULT;
5832 }
5833
5834 btf_id = prog->aux->func_info[subprog].type_id;
5835 if (!btf_id) {
5836 bpf_log(log, "Global functions need valid BTF\n");
5837 return -EFAULT;
5838 }
5839
5840 t = btf_type_by_id(btf, btf_id);
5841 if (!t || !btf_type_is_func(t)) {
5842 /* These checks were already done by the verifier while loading
5843 * struct bpf_func_info
5844 */
5845 bpf_log(log, "BTF of func#%d doesn't point to KIND_FUNC\n",
5846 subprog);
5847 return -EFAULT;
5848 }
5849 tname = btf_name_by_offset(btf, t->name_off);
5850
5851 if (log->level & BPF_LOG_LEVEL)
5852 bpf_log(log, "Validating %s() func#%d...\n",
5853 tname, subprog);
5854
5855 if (prog->aux->func_info_aux[subprog].unreliable) {
5856 bpf_log(log, "Verifier bug in function %s()\n", tname);
5857 return -EFAULT;
5858 }
Alexei Starovoitovbe8704f2020-01-20 16:53:46 -08005859 if (prog_type == BPF_PROG_TYPE_EXT)
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02005860 prog_type = prog->aux->dst_prog->type;
Alexei Starovoitov51c39bb2020-01-09 22:41:20 -08005861
5862 t = btf_type_by_id(btf, t->type);
5863 if (!t || !btf_type_is_func_proto(t)) {
5864 bpf_log(log, "Invalid type of function %s()\n", tname);
5865 return -EFAULT;
5866 }
5867 args = (const struct btf_param *)(t + 1);
5868 nargs = btf_type_vlen(t);
Dmitrii Banshchikov523a4cf2021-02-26 00:26:29 +04005869 if (nargs > MAX_BPF_FUNC_REG_ARGS) {
5870 bpf_log(log, "Global function %s() with %d > %d args. Buggy compiler.\n",
5871 tname, nargs, MAX_BPF_FUNC_REG_ARGS);
Alexei Starovoitov51c39bb2020-01-09 22:41:20 -08005872 return -EINVAL;
5873 }
5874 /* check that function returns int */
5875 t = btf_type_by_id(btf, t->type);
5876 while (btf_type_is_modifier(t))
5877 t = btf_type_by_id(btf, t->type);
5878 if (!btf_type_is_int(t) && !btf_type_is_enum(t)) {
5879 bpf_log(log,
5880 "Global function %s() doesn't return scalar. Only those are supported.\n",
5881 tname);
5882 return -EINVAL;
5883 }
5884 /* Convert BTF function arguments into verifier types.
5885 * Only PTR_TO_CTX and SCALAR are supported atm.
5886 */
5887 for (i = 0; i < nargs; i++) {
Dmitrii Banshchikovfeb4adf2021-02-13 00:56:39 +04005888 struct bpf_reg_state *reg = &regs[i + 1];
5889
Alexei Starovoitov51c39bb2020-01-09 22:41:20 -08005890 t = btf_type_by_id(btf, args[i].type);
5891 while (btf_type_is_modifier(t))
5892 t = btf_type_by_id(btf, t->type);
5893 if (btf_type_is_int(t) || btf_type_is_enum(t)) {
Dmitrii Banshchikovfeb4adf2021-02-13 00:56:39 +04005894 reg->type = SCALAR_VALUE;
Alexei Starovoitov51c39bb2020-01-09 22:41:20 -08005895 continue;
5896 }
Dmitrii Banshchikove5069b9c2021-02-13 00:56:41 +04005897 if (btf_type_is_ptr(t)) {
5898 if (btf_get_prog_ctx_type(log, btf, t, prog_type, i)) {
5899 reg->type = PTR_TO_CTX;
5900 continue;
5901 }
5902
5903 t = btf_type_skip_modifiers(btf, t->type, NULL);
5904
5905 ref_t = btf_resolve_size(btf, t, &reg->mem_size);
5906 if (IS_ERR(ref_t)) {
5907 bpf_log(log,
5908 "arg#%d reference type('%s %s') size cannot be determined: %ld\n",
5909 i, btf_type_str(t), btf_name_by_offset(btf, t->name_off),
5910 PTR_ERR(ref_t));
5911 return -EINVAL;
5912 }
5913
Hao Luocf9f2f82021-12-16 16:31:49 -08005914 reg->type = PTR_TO_MEM | PTR_MAYBE_NULL;
Dmitrii Banshchikove5069b9c2021-02-13 00:56:41 +04005915 reg->id = ++env->id_gen;
5916
Alexei Starovoitov51c39bb2020-01-09 22:41:20 -08005917 continue;
5918 }
5919 bpf_log(log, "Arg#%d type %s in %s() is not supported yet.\n",
5920 i, btf_kind_str[BTF_INFO_KIND(t->info)], tname);
5921 return -EINVAL;
5922 }
Alexei Starovoitov8c1b6e62019-11-14 10:57:16 -08005923 return 0;
5924}
5925
Alan Maguire31d0bc82020-09-28 12:31:04 +01005926static void btf_type_show(const struct btf *btf, u32 type_id, void *obj,
5927 struct btf_show *show)
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07005928{
5929 const struct btf_type *t = btf_type_by_id(btf, type_id);
5930
Alan Maguire31d0bc82020-09-28 12:31:04 +01005931 show->btf = btf;
5932 memset(&show->state, 0, sizeof(show->state));
5933 memset(&show->obj, 0, sizeof(show->obj));
5934
5935 btf_type_ops(t)->show(btf, t, type_id, obj, 0, show);
5936}
5937
5938static void btf_seq_show(struct btf_show *show, const char *fmt,
5939 va_list args)
5940{
5941 seq_vprintf((struct seq_file *)show->target, fmt, args);
5942}
5943
Alan Maguireeb411372020-09-28 12:31:09 +01005944int btf_type_seq_show_flags(const struct btf *btf, u32 type_id,
5945 void *obj, struct seq_file *m, u64 flags)
Alan Maguire31d0bc82020-09-28 12:31:04 +01005946{
5947 struct btf_show sseq;
5948
5949 sseq.target = m;
5950 sseq.showfn = btf_seq_show;
5951 sseq.flags = flags;
5952
5953 btf_type_show(btf, type_id, obj, &sseq);
5954
5955 return sseq.state.status;
5956}
5957
5958void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj,
5959 struct seq_file *m)
5960{
5961 (void) btf_type_seq_show_flags(btf, type_id, obj, m,
5962 BTF_SHOW_NONAME | BTF_SHOW_COMPACT |
5963 BTF_SHOW_ZERO | BTF_SHOW_UNSAFE);
5964}
5965
5966struct btf_show_snprintf {
5967 struct btf_show show;
5968 int len_left; /* space left in string */
5969 int len; /* length we would have written */
5970};
5971
5972static void btf_snprintf_show(struct btf_show *show, const char *fmt,
5973 va_list args)
5974{
5975 struct btf_show_snprintf *ssnprintf = (struct btf_show_snprintf *)show;
5976 int len;
5977
5978 len = vsnprintf(show->target, ssnprintf->len_left, fmt, args);
5979
5980 if (len < 0) {
5981 ssnprintf->len_left = 0;
5982 ssnprintf->len = len;
5983 } else if (len > ssnprintf->len_left) {
5984 /* no space, drive on to get length we would have written */
5985 ssnprintf->len_left = 0;
5986 ssnprintf->len += len;
5987 } else {
5988 ssnprintf->len_left -= len;
5989 ssnprintf->len += len;
5990 show->target += len;
5991 }
5992}
5993
5994int btf_type_snprintf_show(const struct btf *btf, u32 type_id, void *obj,
5995 char *buf, int len, u64 flags)
5996{
5997 struct btf_show_snprintf ssnprintf;
5998
5999 ssnprintf.show.target = buf;
6000 ssnprintf.show.flags = flags;
6001 ssnprintf.show.showfn = btf_snprintf_show;
6002 ssnprintf.len_left = len;
6003 ssnprintf.len = 0;
6004
6005 btf_type_show(btf, type_id, obj, (struct btf_show *)&ssnprintf);
6006
6007 /* If we encontered an error, return it. */
6008 if (ssnprintf.show.state.status)
6009 return ssnprintf.show.state.status;
6010
6011 /* Otherwise return length we would have written */
6012 return ssnprintf.len;
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07006013}
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07006014
Quentin Monnet3481e642019-08-20 14:53:46 +01006015#ifdef CONFIG_PROC_FS
6016static void bpf_btf_show_fdinfo(struct seq_file *m, struct file *filp)
6017{
6018 const struct btf *btf = filp->private_data;
6019
6020 seq_printf(m, "btf_id:\t%u\n", btf->id);
6021}
6022#endif
6023
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07006024static int btf_release(struct inode *inode, struct file *filp)
6025{
6026 btf_put(filp->private_data);
6027 return 0;
6028}
6029
Martin KaFai Lau60197cf2018-04-18 15:56:02 -07006030const struct file_operations btf_fops = {
Quentin Monnet3481e642019-08-20 14:53:46 +01006031#ifdef CONFIG_PROC_FS
6032 .show_fdinfo = bpf_btf_show_fdinfo,
6033#endif
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07006034 .release = btf_release,
6035};
6036
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07006037static int __btf_new_fd(struct btf *btf)
6038{
6039 return anon_inode_getfd("btf", &btf_fops, btf, O_RDONLY | O_CLOEXEC);
6040}
6041
Alexei Starovoitovc571bd72021-05-13 17:36:08 -07006042int btf_new_fd(const union bpf_attr *attr, bpfptr_t uattr)
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07006043{
6044 struct btf *btf;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07006045 int ret;
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07006046
Alexei Starovoitovc571bd72021-05-13 17:36:08 -07006047 btf = btf_parse(make_bpfptr(attr->btf, uattr.is_kernel),
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07006048 attr->btf_size, attr->btf_log_level,
6049 u64_to_user_ptr(attr->btf_log_buf),
6050 attr->btf_log_size);
6051 if (IS_ERR(btf))
6052 return PTR_ERR(btf);
6053
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07006054 ret = btf_alloc_id(btf);
6055 if (ret) {
6056 btf_free(btf);
6057 return ret;
6058 }
6059
6060 /*
6061 * The BTF ID is published to the userspace.
6062 * All BTF free must go through call_rcu() from
6063 * now on (i.e. free by calling btf_put()).
6064 */
6065
6066 ret = __btf_new_fd(btf);
6067 if (ret < 0)
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07006068 btf_put(btf);
6069
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07006070 return ret;
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07006071}
6072
6073struct btf *btf_get_by_fd(int fd)
6074{
6075 struct btf *btf;
6076 struct fd f;
6077
6078 f = fdget(fd);
6079
6080 if (!f.file)
6081 return ERR_PTR(-EBADF);
6082
6083 if (f.file->f_op != &btf_fops) {
6084 fdput(f);
6085 return ERR_PTR(-EINVAL);
6086 }
6087
6088 btf = f.file->private_data;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07006089 refcount_inc(&btf->refcnt);
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07006090 fdput(f);
6091
6092 return btf;
6093}
Martin KaFai Lau60197cf2018-04-18 15:56:02 -07006094
6095int btf_get_info_by_fd(const struct btf *btf,
6096 const union bpf_attr *attr,
6097 union bpf_attr __user *uattr)
6098{
Martin KaFai Lau62dab842018-05-04 14:49:52 -07006099 struct bpf_btf_info __user *uinfo;
Greg Kroah-Hartman5c6f2582020-03-20 17:22:58 +01006100 struct bpf_btf_info info;
Martin KaFai Lau62dab842018-05-04 14:49:52 -07006101 u32 info_copy, btf_copy;
6102 void __user *ubtf;
Andrii Nakryiko53297222020-11-09 17:19:29 -08006103 char __user *uname;
6104 u32 uinfo_len, uname_len, name_len;
6105 int ret = 0;
Martin KaFai Lau60197cf2018-04-18 15:56:02 -07006106
Martin KaFai Lau62dab842018-05-04 14:49:52 -07006107 uinfo = u64_to_user_ptr(attr->info.info);
6108 uinfo_len = attr->info.info_len;
6109
6110 info_copy = min_t(u32, uinfo_len, sizeof(info));
Greg Kroah-Hartman5c6f2582020-03-20 17:22:58 +01006111 memset(&info, 0, sizeof(info));
Martin KaFai Lau62dab842018-05-04 14:49:52 -07006112 if (copy_from_user(&info, uinfo, info_copy))
6113 return -EFAULT;
6114
6115 info.id = btf->id;
6116 ubtf = u64_to_user_ptr(info.btf);
6117 btf_copy = min_t(u32, btf->data_size, info.btf_size);
6118 if (copy_to_user(ubtf, btf->data, btf_copy))
6119 return -EFAULT;
6120 info.btf_size = btf->data_size;
6121
Andrii Nakryiko53297222020-11-09 17:19:29 -08006122 info.kernel_btf = btf->kernel_btf;
6123
6124 uname = u64_to_user_ptr(info.name);
6125 uname_len = info.name_len;
6126 if (!uname ^ !uname_len)
6127 return -EINVAL;
6128
6129 name_len = strlen(btf->name);
6130 info.name_len = name_len;
6131
6132 if (uname) {
6133 if (uname_len >= name_len + 1) {
6134 if (copy_to_user(uname, btf->name, name_len + 1))
6135 return -EFAULT;
6136 } else {
6137 char zero = '\0';
6138
6139 if (copy_to_user(uname, btf->name, uname_len - 1))
6140 return -EFAULT;
6141 if (put_user(zero, uname + uname_len - 1))
6142 return -EFAULT;
6143 /* let user-space know about too short buffer */
6144 ret = -ENOSPC;
6145 }
6146 }
6147
Martin KaFai Lau62dab842018-05-04 14:49:52 -07006148 if (copy_to_user(uinfo, &info, info_copy) ||
6149 put_user(info_copy, &uattr->info.info_len))
Martin KaFai Lau60197cf2018-04-18 15:56:02 -07006150 return -EFAULT;
6151
Andrii Nakryiko53297222020-11-09 17:19:29 -08006152 return ret;
Martin KaFai Lau60197cf2018-04-18 15:56:02 -07006153}
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07006154
6155int btf_get_fd_by_id(u32 id)
6156{
6157 struct btf *btf;
6158 int fd;
6159
6160 rcu_read_lock();
6161 btf = idr_find(&btf_idr, id);
6162 if (!btf || !refcount_inc_not_zero(&btf->refcnt))
6163 btf = ERR_PTR(-ENOENT);
6164 rcu_read_unlock();
6165
6166 if (IS_ERR(btf))
6167 return PTR_ERR(btf);
6168
6169 fd = __btf_new_fd(btf);
6170 if (fd < 0)
6171 btf_put(btf);
6172
6173 return fd;
6174}
6175
Andrii Nakryiko22dc4a02020-12-03 12:46:29 -08006176u32 btf_obj_id(const struct btf *btf)
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07006177{
6178 return btf->id;
6179}
Jiri Olsaeae2e832020-08-25 21:21:19 +02006180
Andrii Nakryiko290248a2020-12-03 12:46:30 -08006181bool btf_is_kernel(const struct btf *btf)
6182{
6183 return btf->kernel_btf;
6184}
6185
Andrii Nakryiko541c3ba2021-01-11 23:55:18 -08006186bool btf_is_module(const struct btf *btf)
6187{
6188 return btf->kernel_btf && strcmp(btf->name, "vmlinux") != 0;
6189}
6190
Jiri Olsaeae2e832020-08-25 21:21:19 +02006191static int btf_id_cmp_func(const void *a, const void *b)
6192{
6193 const int *pa = a, *pb = b;
6194
6195 return *pa - *pb;
6196}
6197
Lorenz Bauer2af30f12020-09-21 13:12:17 +01006198bool btf_id_set_contains(const struct btf_id_set *set, u32 id)
Jiri Olsaeae2e832020-08-25 21:21:19 +02006199{
6200 return bsearch(&id, set->ids, set->cnt, sizeof(u32), btf_id_cmp_func) != NULL;
6201}
Andrii Nakryiko36e68442020-11-09 17:19:31 -08006202
6203#ifdef CONFIG_DEBUG_INFO_BTF_MODULES
6204struct btf_module {
6205 struct list_head list;
6206 struct module *module;
6207 struct btf *btf;
6208 struct bin_attribute *sysfs_attr;
6209};
6210
6211static LIST_HEAD(btf_modules);
6212static DEFINE_MUTEX(btf_module_mutex);
6213
6214static ssize_t
6215btf_module_read(struct file *file, struct kobject *kobj,
6216 struct bin_attribute *bin_attr,
6217 char *buf, loff_t off, size_t len)
6218{
6219 const struct btf *btf = bin_attr->private;
6220
6221 memcpy(buf, btf->data + off, len);
6222 return len;
6223}
6224
Alexei Starovoitov1e891062021-12-01 10:10:31 -08006225static void purge_cand_cache(struct btf *btf);
6226
Andrii Nakryiko36e68442020-11-09 17:19:31 -08006227static int btf_module_notify(struct notifier_block *nb, unsigned long op,
6228 void *module)
6229{
6230 struct btf_module *btf_mod, *tmp;
6231 struct module *mod = module;
6232 struct btf *btf;
6233 int err = 0;
6234
6235 if (mod->btf_data_size == 0 ||
6236 (op != MODULE_STATE_COMING && op != MODULE_STATE_GOING))
6237 goto out;
6238
6239 switch (op) {
6240 case MODULE_STATE_COMING:
6241 btf_mod = kzalloc(sizeof(*btf_mod), GFP_KERNEL);
6242 if (!btf_mod) {
6243 err = -ENOMEM;
6244 goto out;
6245 }
6246 btf = btf_parse_module(mod->name, mod->btf_data, mod->btf_data_size);
6247 if (IS_ERR(btf)) {
6248 pr_warn("failed to validate module [%s] BTF: %ld\n",
6249 mod->name, PTR_ERR(btf));
6250 kfree(btf_mod);
6251 err = PTR_ERR(btf);
6252 goto out;
6253 }
6254 err = btf_alloc_id(btf);
6255 if (err) {
6256 btf_free(btf);
6257 kfree(btf_mod);
6258 goto out;
6259 }
6260
Alexei Starovoitov1e891062021-12-01 10:10:31 -08006261 purge_cand_cache(NULL);
Andrii Nakryiko36e68442020-11-09 17:19:31 -08006262 mutex_lock(&btf_module_mutex);
6263 btf_mod->module = module;
6264 btf_mod->btf = btf;
6265 list_add(&btf_mod->list, &btf_modules);
6266 mutex_unlock(&btf_module_mutex);
6267
6268 if (IS_ENABLED(CONFIG_SYSFS)) {
6269 struct bin_attribute *attr;
6270
6271 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
6272 if (!attr)
6273 goto out;
6274
6275 sysfs_bin_attr_init(attr);
6276 attr->attr.name = btf->name;
6277 attr->attr.mode = 0444;
6278 attr->size = btf->data_size;
6279 attr->private = btf;
6280 attr->read = btf_module_read;
6281
6282 err = sysfs_create_bin_file(btf_kobj, attr);
6283 if (err) {
6284 pr_warn("failed to register module [%s] BTF in sysfs: %d\n",
6285 mod->name, err);
6286 kfree(attr);
6287 err = 0;
6288 goto out;
6289 }
6290
6291 btf_mod->sysfs_attr = attr;
6292 }
6293
6294 break;
6295 case MODULE_STATE_GOING:
6296 mutex_lock(&btf_module_mutex);
6297 list_for_each_entry_safe(btf_mod, tmp, &btf_modules, list) {
6298 if (btf_mod->module != module)
6299 continue;
6300
6301 list_del(&btf_mod->list);
6302 if (btf_mod->sysfs_attr)
6303 sysfs_remove_bin_file(btf_kobj, btf_mod->sysfs_attr);
Alexei Starovoitov1e891062021-12-01 10:10:31 -08006304 purge_cand_cache(btf_mod->btf);
Andrii Nakryiko36e68442020-11-09 17:19:31 -08006305 btf_put(btf_mod->btf);
6306 kfree(btf_mod->sysfs_attr);
6307 kfree(btf_mod);
6308 break;
6309 }
6310 mutex_unlock(&btf_module_mutex);
6311 break;
6312 }
6313out:
6314 return notifier_from_errno(err);
6315}
6316
6317static struct notifier_block btf_module_nb = {
6318 .notifier_call = btf_module_notify,
6319};
6320
6321static int __init btf_module_init(void)
6322{
6323 register_module_notifier(&btf_module_nb);
6324 return 0;
6325}
6326
6327fs_initcall(btf_module_init);
6328#endif /* CONFIG_DEBUG_INFO_BTF_MODULES */
Andrii Nakryiko541c3ba2021-01-11 23:55:18 -08006329
6330struct module *btf_try_get_module(const struct btf *btf)
6331{
6332 struct module *res = NULL;
6333#ifdef CONFIG_DEBUG_INFO_BTF_MODULES
6334 struct btf_module *btf_mod, *tmp;
6335
6336 mutex_lock(&btf_module_mutex);
6337 list_for_each_entry_safe(btf_mod, tmp, &btf_modules, list) {
6338 if (btf_mod->btf != btf)
6339 continue;
6340
6341 if (try_module_get(btf_mod->module))
6342 res = btf_mod->module;
6343
6344 break;
6345 }
6346 mutex_unlock(&btf_module_mutex);
6347#endif
6348
6349 return res;
6350}
Alexei Starovoitov3d784172021-05-13 17:36:11 -07006351
6352BPF_CALL_4(bpf_btf_find_by_name_kind, char *, name, int, name_sz, u32, kind, int, flags)
6353{
6354 struct btf *btf;
6355 long ret;
6356
6357 if (flags)
6358 return -EINVAL;
6359
6360 if (name_sz <= 1 || name[name_sz - 1])
6361 return -EINVAL;
6362
6363 btf = bpf_get_btf_vmlinux();
6364 if (IS_ERR(btf))
6365 return PTR_ERR(btf);
6366
6367 ret = btf_find_by_name_kind(btf, name, kind);
6368 /* ret is never zero, since btf_find_by_name_kind returns
6369 * positive btf_id or negative error.
6370 */
6371 if (ret < 0) {
6372 struct btf *mod_btf;
6373 int id;
6374
6375 /* If name is not found in vmlinux's BTF then search in module's BTFs */
6376 spin_lock_bh(&btf_idr_lock);
6377 idr_for_each_entry(&btf_idr, mod_btf, id) {
6378 if (!btf_is_module(mod_btf))
6379 continue;
6380 /* linear search could be slow hence unlock/lock
6381 * the IDR to avoiding holding it for too long
6382 */
6383 btf_get(mod_btf);
6384 spin_unlock_bh(&btf_idr_lock);
6385 ret = btf_find_by_name_kind(mod_btf, name, kind);
6386 if (ret > 0) {
6387 int btf_obj_fd;
6388
6389 btf_obj_fd = __btf_new_fd(mod_btf);
6390 if (btf_obj_fd < 0) {
6391 btf_put(mod_btf);
6392 return btf_obj_fd;
6393 }
6394 return ret | (((u64)btf_obj_fd) << 32);
6395 }
6396 spin_lock_bh(&btf_idr_lock);
6397 btf_put(mod_btf);
6398 }
6399 spin_unlock_bh(&btf_idr_lock);
6400 }
6401 return ret;
6402}
6403
6404const struct bpf_func_proto bpf_btf_find_by_name_kind_proto = {
6405 .func = bpf_btf_find_by_name_kind,
6406 .gpl_only = false,
6407 .ret_type = RET_INTEGER,
Hao Luo216e3cd2021-12-16 16:31:51 -08006408 .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY,
Alexei Starovoitov3d784172021-05-13 17:36:11 -07006409 .arg2_type = ARG_CONST_SIZE,
6410 .arg3_type = ARG_ANYTHING,
6411 .arg4_type = ARG_ANYTHING,
6412};
Daniel Xueb529c52021-08-25 18:48:31 -07006413
Song Liud19ddb42021-11-12 07:02:43 -08006414BTF_ID_LIST_GLOBAL(btf_tracing_ids, MAX_BTF_TRACING_TYPE)
6415#define BTF_TRACING_TYPE(name, type) BTF_ID(struct, type)
6416BTF_TRACING_TYPE_xxx
6417#undef BTF_TRACING_TYPE
Kumar Kartikeya Dwivedi14f267d2021-10-02 06:47:51 +05306418
6419/* BTF ID set registration API for modules */
6420
Kumar Kartikeya Dwivedi14f267d2021-10-02 06:47:51 +05306421#ifdef CONFIG_DEBUG_INFO_BTF_MODULES
6422
6423void register_kfunc_btf_id_set(struct kfunc_btf_id_list *l,
6424 struct kfunc_btf_id_set *s)
6425{
6426 mutex_lock(&l->mutex);
6427 list_add(&s->list, &l->list);
6428 mutex_unlock(&l->mutex);
6429}
6430EXPORT_SYMBOL_GPL(register_kfunc_btf_id_set);
6431
6432void unregister_kfunc_btf_id_set(struct kfunc_btf_id_list *l,
6433 struct kfunc_btf_id_set *s)
6434{
6435 mutex_lock(&l->mutex);
6436 list_del_init(&s->list);
6437 mutex_unlock(&l->mutex);
6438}
6439EXPORT_SYMBOL_GPL(unregister_kfunc_btf_id_set);
6440
6441bool bpf_check_mod_kfunc_call(struct kfunc_btf_id_list *klist, u32 kfunc_id,
6442 struct module *owner)
6443{
6444 struct kfunc_btf_id_set *s;
6445
Kumar Kartikeya Dwivedi14f267d2021-10-02 06:47:51 +05306446 mutex_lock(&klist->mutex);
6447 list_for_each_entry(s, &klist->list, list) {
6448 if (s->owner == owner && btf_id_set_contains(s->set, kfunc_id)) {
6449 mutex_unlock(&klist->mutex);
6450 return true;
6451 }
6452 }
6453 mutex_unlock(&klist->mutex);
6454 return false;
6455}
6456
Kumar Kartikeya Dwivedi14f267d2021-10-02 06:47:51 +05306457#define DEFINE_KFUNC_BTF_ID_LIST(name) \
6458 struct kfunc_btf_id_list name = { LIST_HEAD_INIT(name.list), \
6459 __MUTEX_INITIALIZER(name.mutex) }; \
6460 EXPORT_SYMBOL_GPL(name)
Kumar Kartikeya Dwivedi0e32dfc2021-10-02 06:47:53 +05306461
6462DEFINE_KFUNC_BTF_ID_LIST(bpf_tcp_ca_kfunc_list);
Kumar Kartikeya Dwivedic48e51c2021-10-02 06:47:57 +05306463DEFINE_KFUNC_BTF_ID_LIST(prog_test_kfunc_list);
Kumar Kartikeya Dwivedid9847eb2021-11-22 20:17:40 +05306464
6465#endif
Jakub Kicinskibe315822021-12-10 15:56:10 -08006466
Alexei Starovoitov29db4be2021-12-01 10:10:26 -08006467int bpf_core_types_are_compat(const struct btf *local_btf, __u32 local_id,
6468 const struct btf *targ_btf, __u32 targ_id)
6469{
6470 return -EOPNOTSUPP;
6471}
6472
6473static bool bpf_core_is_flavor_sep(const char *s)
6474{
6475 /* check X___Y name pattern, where X and Y are not underscores */
6476 return s[0] != '_' && /* X */
6477 s[1] == '_' && s[2] == '_' && s[3] == '_' && /* ___ */
6478 s[4] != '_'; /* Y */
6479}
6480
6481size_t bpf_core_essential_name_len(const char *name)
6482{
6483 size_t n = strlen(name);
6484 int i;
6485
6486 for (i = n - 5; i >= 0; i--) {
6487 if (bpf_core_is_flavor_sep(name + i))
6488 return i + 1;
6489 }
6490 return n;
6491}
Alexei Starovoitovfbd94c72021-12-01 10:10:28 -08006492
Alexei Starovoitov1e891062021-12-01 10:10:31 -08006493struct bpf_cand_cache {
6494 const char *name;
6495 u32 name_len;
6496 u16 kind;
6497 u16 cnt;
6498 struct {
6499 const struct btf *btf;
6500 u32 id;
6501 } cands[];
6502};
6503
6504static void bpf_free_cands(struct bpf_cand_cache *cands)
6505{
6506 if (!cands->cnt)
6507 /* empty candidate array was allocated on stack */
6508 return;
6509 kfree(cands);
6510}
6511
6512static void bpf_free_cands_from_cache(struct bpf_cand_cache *cands)
6513{
6514 kfree(cands->name);
6515 kfree(cands);
6516}
6517
6518#define VMLINUX_CAND_CACHE_SIZE 31
6519static struct bpf_cand_cache *vmlinux_cand_cache[VMLINUX_CAND_CACHE_SIZE];
6520
6521#define MODULE_CAND_CACHE_SIZE 31
6522static struct bpf_cand_cache *module_cand_cache[MODULE_CAND_CACHE_SIZE];
6523
6524static DEFINE_MUTEX(cand_cache_mutex);
6525
6526static void __print_cand_cache(struct bpf_verifier_log *log,
6527 struct bpf_cand_cache **cache,
6528 int cache_size)
6529{
6530 struct bpf_cand_cache *cc;
6531 int i, j;
6532
6533 for (i = 0; i < cache_size; i++) {
6534 cc = cache[i];
6535 if (!cc)
6536 continue;
6537 bpf_log(log, "[%d]%s(", i, cc->name);
6538 for (j = 0; j < cc->cnt; j++) {
6539 bpf_log(log, "%d", cc->cands[j].id);
6540 if (j < cc->cnt - 1)
6541 bpf_log(log, " ");
6542 }
6543 bpf_log(log, "), ");
6544 }
6545}
6546
6547static void print_cand_cache(struct bpf_verifier_log *log)
6548{
6549 mutex_lock(&cand_cache_mutex);
6550 bpf_log(log, "vmlinux_cand_cache:");
6551 __print_cand_cache(log, vmlinux_cand_cache, VMLINUX_CAND_CACHE_SIZE);
6552 bpf_log(log, "\nmodule_cand_cache:");
6553 __print_cand_cache(log, module_cand_cache, MODULE_CAND_CACHE_SIZE);
6554 bpf_log(log, "\n");
6555 mutex_unlock(&cand_cache_mutex);
6556}
6557
6558static u32 hash_cands(struct bpf_cand_cache *cands)
6559{
6560 return jhash(cands->name, cands->name_len, 0);
6561}
6562
6563static struct bpf_cand_cache *check_cand_cache(struct bpf_cand_cache *cands,
6564 struct bpf_cand_cache **cache,
6565 int cache_size)
6566{
6567 struct bpf_cand_cache *cc = cache[hash_cands(cands) % cache_size];
6568
6569 if (cc && cc->name_len == cands->name_len &&
6570 !strncmp(cc->name, cands->name, cands->name_len))
6571 return cc;
6572 return NULL;
6573}
6574
6575static size_t sizeof_cands(int cnt)
6576{
6577 return offsetof(struct bpf_cand_cache, cands[cnt]);
6578}
6579
6580static struct bpf_cand_cache *populate_cand_cache(struct bpf_cand_cache *cands,
6581 struct bpf_cand_cache **cache,
6582 int cache_size)
6583{
6584 struct bpf_cand_cache **cc = &cache[hash_cands(cands) % cache_size], *new_cands;
6585
6586 if (*cc) {
6587 bpf_free_cands_from_cache(*cc);
6588 *cc = NULL;
6589 }
Jiapeng Chong4674f212021-12-09 14:21:22 +08006590 new_cands = kmemdup(cands, sizeof_cands(cands->cnt), GFP_KERNEL);
Alexei Starovoitov1e891062021-12-01 10:10:31 -08006591 if (!new_cands) {
6592 bpf_free_cands(cands);
6593 return ERR_PTR(-ENOMEM);
6594 }
Alexei Starovoitov1e891062021-12-01 10:10:31 -08006595 /* strdup the name, since it will stay in cache.
6596 * the cands->name points to strings in prog's BTF and the prog can be unloaded.
6597 */
6598 new_cands->name = kmemdup_nul(cands->name, cands->name_len, GFP_KERNEL);
6599 bpf_free_cands(cands);
6600 if (!new_cands->name) {
6601 kfree(new_cands);
6602 return ERR_PTR(-ENOMEM);
6603 }
6604 *cc = new_cands;
6605 return new_cands;
6606}
6607
Alexei Starovoitov29f2e5b2021-12-06 17:48:39 -08006608#ifdef CONFIG_DEBUG_INFO_BTF_MODULES
Alexei Starovoitov1e891062021-12-01 10:10:31 -08006609static void __purge_cand_cache(struct btf *btf, struct bpf_cand_cache **cache,
6610 int cache_size)
6611{
6612 struct bpf_cand_cache *cc;
6613 int i, j;
6614
6615 for (i = 0; i < cache_size; i++) {
6616 cc = cache[i];
6617 if (!cc)
6618 continue;
6619 if (!btf) {
6620 /* when new module is loaded purge all of module_cand_cache,
6621 * since new module might have candidates with the name
6622 * that matches cached cands.
6623 */
6624 bpf_free_cands_from_cache(cc);
6625 cache[i] = NULL;
6626 continue;
6627 }
6628 /* when module is unloaded purge cache entries
6629 * that match module's btf
6630 */
6631 for (j = 0; j < cc->cnt; j++)
6632 if (cc->cands[j].btf == btf) {
6633 bpf_free_cands_from_cache(cc);
6634 cache[i] = NULL;
6635 break;
6636 }
6637 }
6638
6639}
6640
6641static void purge_cand_cache(struct btf *btf)
6642{
6643 mutex_lock(&cand_cache_mutex);
6644 __purge_cand_cache(btf, module_cand_cache, MODULE_CAND_CACHE_SIZE);
6645 mutex_unlock(&cand_cache_mutex);
6646}
Alexei Starovoitov29f2e5b2021-12-06 17:48:39 -08006647#endif
Alexei Starovoitov1e891062021-12-01 10:10:31 -08006648
6649static struct bpf_cand_cache *
6650bpf_core_add_cands(struct bpf_cand_cache *cands, const struct btf *targ_btf,
6651 int targ_start_id)
6652{
6653 struct bpf_cand_cache *new_cands;
6654 const struct btf_type *t;
6655 const char *targ_name;
6656 size_t targ_essent_len;
6657 int n, i;
6658
6659 n = btf_nr_types(targ_btf);
6660 for (i = targ_start_id; i < n; i++) {
6661 t = btf_type_by_id(targ_btf, i);
6662 if (btf_kind(t) != cands->kind)
6663 continue;
6664
6665 targ_name = btf_name_by_offset(targ_btf, t->name_off);
6666 if (!targ_name)
6667 continue;
6668
6669 /* the resched point is before strncmp to make sure that search
6670 * for non-existing name will have a chance to schedule().
6671 */
6672 cond_resched();
6673
6674 if (strncmp(cands->name, targ_name, cands->name_len) != 0)
6675 continue;
6676
6677 targ_essent_len = bpf_core_essential_name_len(targ_name);
6678 if (targ_essent_len != cands->name_len)
6679 continue;
6680
6681 /* most of the time there is only one candidate for a given kind+name pair */
6682 new_cands = kmalloc(sizeof_cands(cands->cnt + 1), GFP_KERNEL);
6683 if (!new_cands) {
6684 bpf_free_cands(cands);
6685 return ERR_PTR(-ENOMEM);
6686 }
6687
6688 memcpy(new_cands, cands, sizeof_cands(cands->cnt));
6689 bpf_free_cands(cands);
6690 cands = new_cands;
6691 cands->cands[cands->cnt].btf = targ_btf;
6692 cands->cands[cands->cnt].id = i;
6693 cands->cnt++;
6694 }
6695 return cands;
6696}
6697
6698static struct bpf_cand_cache *
6699bpf_core_find_cands(struct bpf_core_ctx *ctx, u32 local_type_id)
6700{
6701 struct bpf_cand_cache *cands, *cc, local_cand = {};
6702 const struct btf *local_btf = ctx->btf;
6703 const struct btf_type *local_type;
6704 const struct btf *main_btf;
6705 size_t local_essent_len;
6706 struct btf *mod_btf;
6707 const char *name;
6708 int id;
6709
6710 main_btf = bpf_get_btf_vmlinux();
6711 if (IS_ERR(main_btf))
Alexei Starovoitovf18a4992021-12-11 18:08:19 -08006712 return ERR_CAST(main_btf);
Alexei Starovoitov1e891062021-12-01 10:10:31 -08006713
6714 local_type = btf_type_by_id(local_btf, local_type_id);
6715 if (!local_type)
6716 return ERR_PTR(-EINVAL);
6717
6718 name = btf_name_by_offset(local_btf, local_type->name_off);
6719 if (str_is_empty(name))
6720 return ERR_PTR(-EINVAL);
6721 local_essent_len = bpf_core_essential_name_len(name);
6722
6723 cands = &local_cand;
6724 cands->name = name;
6725 cands->kind = btf_kind(local_type);
6726 cands->name_len = local_essent_len;
6727
6728 cc = check_cand_cache(cands, vmlinux_cand_cache, VMLINUX_CAND_CACHE_SIZE);
6729 /* cands is a pointer to stack here */
6730 if (cc) {
6731 if (cc->cnt)
6732 return cc;
6733 goto check_modules;
6734 }
6735
6736 /* Attempt to find target candidates in vmlinux BTF first */
6737 cands = bpf_core_add_cands(cands, main_btf, 1);
6738 if (IS_ERR(cands))
Alexei Starovoitovf18a4992021-12-11 18:08:19 -08006739 return ERR_CAST(cands);
Alexei Starovoitov1e891062021-12-01 10:10:31 -08006740
6741 /* cands is a pointer to kmalloced memory here if cands->cnt > 0 */
6742
6743 /* populate cache even when cands->cnt == 0 */
6744 cc = populate_cand_cache(cands, vmlinux_cand_cache, VMLINUX_CAND_CACHE_SIZE);
6745 if (IS_ERR(cc))
Alexei Starovoitovf18a4992021-12-11 18:08:19 -08006746 return ERR_CAST(cc);
Alexei Starovoitov1e891062021-12-01 10:10:31 -08006747
6748 /* if vmlinux BTF has any candidate, don't go for module BTFs */
6749 if (cc->cnt)
6750 return cc;
6751
6752check_modules:
6753 /* cands is a pointer to stack here and cands->cnt == 0 */
6754 cc = check_cand_cache(cands, module_cand_cache, MODULE_CAND_CACHE_SIZE);
6755 if (cc)
6756 /* if cache has it return it even if cc->cnt == 0 */
6757 return cc;
6758
6759 /* If candidate is not found in vmlinux's BTF then search in module's BTFs */
6760 spin_lock_bh(&btf_idr_lock);
6761 idr_for_each_entry(&btf_idr, mod_btf, id) {
6762 if (!btf_is_module(mod_btf))
6763 continue;
6764 /* linear search could be slow hence unlock/lock
6765 * the IDR to avoiding holding it for too long
6766 */
6767 btf_get(mod_btf);
6768 spin_unlock_bh(&btf_idr_lock);
6769 cands = bpf_core_add_cands(cands, mod_btf, btf_nr_types(main_btf));
6770 if (IS_ERR(cands)) {
6771 btf_put(mod_btf);
Alexei Starovoitovf18a4992021-12-11 18:08:19 -08006772 return ERR_CAST(cands);
Alexei Starovoitov1e891062021-12-01 10:10:31 -08006773 }
6774 spin_lock_bh(&btf_idr_lock);
6775 btf_put(mod_btf);
6776 }
6777 spin_unlock_bh(&btf_idr_lock);
6778 /* cands is a pointer to kmalloced memory here if cands->cnt > 0
6779 * or pointer to stack if cands->cnd == 0.
6780 * Copy it into the cache even when cands->cnt == 0 and
6781 * return the result.
6782 */
6783 return populate_cand_cache(cands, module_cand_cache, MODULE_CAND_CACHE_SIZE);
6784}
6785
Alexei Starovoitovfbd94c72021-12-01 10:10:28 -08006786int bpf_core_apply(struct bpf_core_ctx *ctx, const struct bpf_core_relo *relo,
6787 int relo_idx, void *insn)
6788{
Alexei Starovoitov1e891062021-12-01 10:10:31 -08006789 bool need_cands = relo->kind != BPF_CORE_TYPE_ID_LOCAL;
6790 struct bpf_core_cand_list cands = {};
Alexei Starovoitov78c1f8d2021-12-03 10:28:36 -08006791 struct bpf_core_spec *specs;
Alexei Starovoitov1e891062021-12-01 10:10:31 -08006792 int err;
6793
Alexei Starovoitov78c1f8d2021-12-03 10:28:36 -08006794 /* ~4k of temp memory necessary to convert LLVM spec like "0:1:0:5"
6795 * into arrays of btf_ids of struct fields and array indices.
6796 */
6797 specs = kcalloc(3, sizeof(*specs), GFP_KERNEL);
6798 if (!specs)
6799 return -ENOMEM;
6800
Alexei Starovoitov1e891062021-12-01 10:10:31 -08006801 if (need_cands) {
6802 struct bpf_cand_cache *cc;
6803 int i;
6804
6805 mutex_lock(&cand_cache_mutex);
6806 cc = bpf_core_find_cands(ctx, relo->type_id);
6807 if (IS_ERR(cc)) {
6808 bpf_log(ctx->log, "target candidate search failed for %d\n",
6809 relo->type_id);
6810 err = PTR_ERR(cc);
6811 goto out;
6812 }
6813 if (cc->cnt) {
6814 cands.cands = kcalloc(cc->cnt, sizeof(*cands.cands), GFP_KERNEL);
6815 if (!cands.cands) {
6816 err = -ENOMEM;
6817 goto out;
6818 }
6819 }
6820 for (i = 0; i < cc->cnt; i++) {
6821 bpf_log(ctx->log,
6822 "CO-RE relocating %s %s: found target candidate [%d]\n",
6823 btf_kind_str[cc->kind], cc->name, cc->cands[i].id);
6824 cands.cands[i].btf = cc->cands[i].btf;
6825 cands.cands[i].id = cc->cands[i].id;
6826 }
6827 cands.len = cc->cnt;
6828 /* cand_cache_mutex needs to span the cache lookup and
6829 * copy of btf pointer into bpf_core_cand_list,
6830 * since module can be unloaded while bpf_core_apply_relo_insn
6831 * is working with module's btf.
6832 */
6833 }
6834
6835 err = bpf_core_apply_relo_insn((void *)ctx->log, insn, relo->insn_off / 8,
Alexei Starovoitov78c1f8d2021-12-03 10:28:36 -08006836 relo, relo_idx, ctx->btf, &cands, specs);
Alexei Starovoitov1e891062021-12-01 10:10:31 -08006837out:
Alexei Starovoitov78c1f8d2021-12-03 10:28:36 -08006838 kfree(specs);
Alexei Starovoitov1e891062021-12-01 10:10:31 -08006839 if (need_cands) {
6840 kfree(cands.cands);
6841 mutex_unlock(&cand_cache_mutex);
6842 if (ctx->log->level & BPF_LOG_LEVEL2)
6843 print_cand_cache(ctx->log);
6844 }
6845 return err;
Alexei Starovoitovfbd94c72021-12-01 10:10:28 -08006846}