Linux-libre 5.7.6-gnu
[librecmc/linux-libre.git] / kernel / bpf / btf.c
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (c) 2018 Facebook */
3
4 #include <uapi/linux/btf.h>
5 #include <uapi/linux/bpf.h>
6 #include <uapi/linux/bpf_perf_event.h>
7 #include <uapi/linux/types.h>
8 #include <linux/seq_file.h>
9 #include <linux/compiler.h>
10 #include <linux/ctype.h>
11 #include <linux/errno.h>
12 #include <linux/slab.h>
13 #include <linux/anon_inodes.h>
14 #include <linux/file.h>
15 #include <linux/uaccess.h>
16 #include <linux/kernel.h>
17 #include <linux/idr.h>
18 #include <linux/sort.h>
19 #include <linux/bpf_verifier.h>
20 #include <linux/btf.h>
21 #include <linux/skmsg.h>
22 #include <linux/perf_event.h>
23 #include <net/sock.h>
24
25 /* BTF (BPF Type Format) is the meta data format which describes
26  * the data types of BPF program/map.  Hence, it basically focus
27  * on the C programming language which the modern BPF is primary
28  * using.
29  *
30  * ELF Section:
31  * ~~~~~~~~~~~
32  * The BTF data is stored under the ".BTF" ELF section
33  *
34  * struct btf_type:
35  * ~~~~~~~~~~~~~~~
36  * Each 'struct btf_type' object describes a C data type.
37  * Depending on the type it is describing, a 'struct btf_type'
38  * object may be followed by more data.  F.e.
39  * To describe an array, 'struct btf_type' is followed by
40  * 'struct btf_array'.
41  *
42  * 'struct btf_type' and any extra data following it are
43  * 4 bytes aligned.
44  *
45  * Type section:
46  * ~~~~~~~~~~~~~
47  * The BTF type section contains a list of 'struct btf_type' objects.
48  * Each one describes a C type.  Recall from the above section
49  * that a 'struct btf_type' object could be immediately followed by extra
50  * data in order to desribe some particular C types.
51  *
52  * type_id:
53  * ~~~~~~~
54  * Each btf_type object is identified by a type_id.  The type_id
55  * is implicitly implied by the location of the btf_type object in
56  * the BTF type section.  The first one has type_id 1.  The second
57  * one has type_id 2...etc.  Hence, an earlier btf_type has
58  * a smaller type_id.
59  *
60  * A btf_type object may refer to another btf_type object by using
61  * type_id (i.e. the "type" in the "struct btf_type").
62  *
63  * NOTE that we cannot assume any reference-order.
64  * A btf_type object can refer to an earlier btf_type object
65  * but it can also refer to a later btf_type object.
66  *
67  * For example, to describe "const void *".  A btf_type
68  * object describing "const" may refer to another btf_type
69  * object describing "void *".  This type-reference is done
70  * by specifying type_id:
71  *
72  * [1] CONST (anon) type_id=2
73  * [2] PTR (anon) type_id=0
74  *
75  * The above is the btf_verifier debug log:
76  *   - Each line started with "[?]" is a btf_type object
77  *   - [?] is the type_id of the btf_type object.
78  *   - CONST/PTR is the BTF_KIND_XXX
79  *   - "(anon)" is the name of the type.  It just
80  *     happens that CONST and PTR has no name.
81  *   - type_id=XXX is the 'u32 type' in btf_type
82  *
83  * NOTE: "void" has type_id 0
84  *
85  * String section:
86  * ~~~~~~~~~~~~~~
87  * The BTF string section contains the names used by the type section.
88  * Each string is referred by an "offset" from the beginning of the
89  * string section.
90  *
91  * Each string is '\0' terminated.
92  *
93  * The first character in the string section must be '\0'
94  * which is used to mean 'anonymous'. Some btf_type may not
95  * have a name.
96  */
97
98 /* BTF verification:
99  *
100  * To verify BTF data, two passes are needed.
101  *
102  * Pass #1
103  * ~~~~~~~
104  * The first pass is to collect all btf_type objects to
105  * an array: "btf->types".
106  *
107  * Depending on the C type that a btf_type is describing,
108  * a btf_type may be followed by extra data.  We don't know
109  * how many btf_type is there, and more importantly we don't
110  * know where each btf_type is located in the type section.
111  *
112  * Without knowing the location of each type_id, most verifications
113  * cannot be done.  e.g. an earlier btf_type may refer to a later
114  * btf_type (recall the "const void *" above), so we cannot
115  * check this type-reference in the first pass.
116  *
117  * In the first pass, it still does some verifications (e.g.
118  * checking the name is a valid offset to the string section).
119  *
120  * Pass #2
121  * ~~~~~~~
122  * The main focus is to resolve a btf_type that is referring
123  * to another type.
124  *
125  * We have to ensure the referring type:
126  * 1) does exist in the BTF (i.e. in btf->types[])
127  * 2) does not cause a loop:
128  *      struct A {
129  *              struct B b;
130  *      };
131  *
132  *      struct B {
133  *              struct A a;
134  *      };
135  *
136  * btf_type_needs_resolve() decides if a btf_type needs
137  * to be resolved.
138  *
139  * The needs_resolve type implements the "resolve()" ops which
140  * essentially does a DFS and detects backedge.
141  *
142  * During resolve (or DFS), different C types have different
143  * "RESOLVED" conditions.
144  *
145  * When resolving a BTF_KIND_STRUCT, we need to resolve all its
146  * members because a member is always referring to another
147  * type.  A struct's member can be treated as "RESOLVED" if
148  * it is referring to a BTF_KIND_PTR.  Otherwise, the
149  * following valid C struct would be rejected:
150  *
151  *      struct A {
152  *              int m;
153  *              struct A *a;
154  *      };
155  *
156  * When resolving a BTF_KIND_PTR, it needs to keep resolving if
157  * it is referring to another BTF_KIND_PTR.  Otherwise, we cannot
158  * detect a pointer loop, e.g.:
159  * BTF_KIND_CONST -> BTF_KIND_PTR -> BTF_KIND_CONST -> BTF_KIND_PTR +
160  *                        ^                                         |
161  *                        +-----------------------------------------+
162  *
163  */
164
165 #define BITS_PER_U128 (sizeof(u64) * BITS_PER_BYTE * 2)
166 #define BITS_PER_BYTE_MASK (BITS_PER_BYTE - 1)
167 #define BITS_PER_BYTE_MASKED(bits) ((bits) & BITS_PER_BYTE_MASK)
168 #define BITS_ROUNDDOWN_BYTES(bits) ((bits) >> 3)
169 #define BITS_ROUNDUP_BYTES(bits) \
170         (BITS_ROUNDDOWN_BYTES(bits) + !!BITS_PER_BYTE_MASKED(bits))
171
172 #define BTF_INFO_MASK 0x8f00ffff
173 #define BTF_INT_MASK 0x0fffffff
174 #define BTF_TYPE_ID_VALID(type_id) ((type_id) <= BTF_MAX_TYPE)
175 #define BTF_STR_OFFSET_VALID(name_off) ((name_off) <= BTF_MAX_NAME_OFFSET)
176
177 /* 16MB for 64k structs and each has 16 members and
178  * a few MB spaces for the string section.
179  * The hard limit is S32_MAX.
180  */
181 #define BTF_MAX_SIZE (16 * 1024 * 1024)
182
183 #define for_each_member_from(i, from, struct_type, member)              \
184         for (i = from, member = btf_type_member(struct_type) + from;    \
185              i < btf_type_vlen(struct_type);                            \
186              i++, member++)
187
188 #define for_each_vsi(i, struct_type, member)                    \
189         for (i = 0, member = btf_type_var_secinfo(struct_type); \
190              i < btf_type_vlen(struct_type);                    \
191              i++, member++)
192
193 #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
198 DEFINE_IDR(btf_idr);
199 DEFINE_SPINLOCK(btf_idr_lock);
200
201 struct btf {
202         void *data;
203         struct btf_type **types;
204         u32 *resolved_ids;
205         u32 *resolved_sizes;
206         const char *strings;
207         void *nohdr_data;
208         struct btf_header hdr;
209         u32 nr_types;
210         u32 types_size;
211         u32 data_size;
212         refcount_t refcnt;
213         u32 id;
214         struct rcu_head rcu;
215 };
216
217 enum verifier_phase {
218         CHECK_META,
219         CHECK_TYPE,
220 };
221
222 struct resolve_vertex {
223         const struct btf_type *t;
224         u32 type_id;
225         u16 next_member;
226 };
227
228 enum visit_state {
229         NOT_VISITED,
230         VISITED,
231         RESOLVED,
232 };
233
234 enum resolve_mode {
235         RESOLVE_TBD,    /* To Be Determined */
236         RESOLVE_PTR,    /* Resolving for Pointer */
237         RESOLVE_STRUCT_OR_ARRAY,        /* Resolving for struct/union
238                                          * or array
239                                          */
240 };
241
242 #define MAX_RESOLVE_DEPTH 32
243
244 struct btf_sec_info {
245         u32 off;
246         u32 len;
247 };
248
249 struct btf_verifier_env {
250         struct btf *btf;
251         u8 *visit_states;
252         struct resolve_vertex stack[MAX_RESOLVE_DEPTH];
253         struct bpf_verifier_log log;
254         u32 log_type_id;
255         u32 top_stack;
256         enum verifier_phase phase;
257         enum resolve_mode resolve_mode;
258 };
259
260 static const char * const btf_kind_str[NR_BTF_KINDS] = {
261         [BTF_KIND_UNKN]         = "UNKNOWN",
262         [BTF_KIND_INT]          = "INT",
263         [BTF_KIND_PTR]          = "PTR",
264         [BTF_KIND_ARRAY]        = "ARRAY",
265         [BTF_KIND_STRUCT]       = "STRUCT",
266         [BTF_KIND_UNION]        = "UNION",
267         [BTF_KIND_ENUM]         = "ENUM",
268         [BTF_KIND_FWD]          = "FWD",
269         [BTF_KIND_TYPEDEF]      = "TYPEDEF",
270         [BTF_KIND_VOLATILE]     = "VOLATILE",
271         [BTF_KIND_CONST]        = "CONST",
272         [BTF_KIND_RESTRICT]     = "RESTRICT",
273         [BTF_KIND_FUNC]         = "FUNC",
274         [BTF_KIND_FUNC_PROTO]   = "FUNC_PROTO",
275         [BTF_KIND_VAR]          = "VAR",
276         [BTF_KIND_DATASEC]      = "DATASEC",
277 };
278
279 static const char *btf_type_str(const struct btf_type *t)
280 {
281         return btf_kind_str[BTF_INFO_KIND(t->info)];
282 }
283
284 struct btf_kind_operations {
285         s32 (*check_meta)(struct btf_verifier_env *env,
286                           const struct btf_type *t,
287                           u32 meta_left);
288         int (*resolve)(struct btf_verifier_env *env,
289                        const struct resolve_vertex *v);
290         int (*check_member)(struct btf_verifier_env *env,
291                             const struct btf_type *struct_type,
292                             const struct btf_member *member,
293                             const struct btf_type *member_type);
294         int (*check_kflag_member)(struct btf_verifier_env *env,
295                                   const struct btf_type *struct_type,
296                                   const struct btf_member *member,
297                                   const struct btf_type *member_type);
298         void (*log_details)(struct btf_verifier_env *env,
299                             const struct btf_type *t);
300         void (*seq_show)(const struct btf *btf, const struct btf_type *t,
301                          u32 type_id, void *data, u8 bits_offsets,
302                          struct seq_file *m);
303 };
304
305 static const struct btf_kind_operations * const kind_ops[NR_BTF_KINDS];
306 static struct btf_type btf_void;
307
308 static int btf_resolve(struct btf_verifier_env *env,
309                        const struct btf_type *t, u32 type_id);
310
311 static bool btf_type_is_modifier(const struct btf_type *t)
312 {
313         /* Some of them is not strictly a C modifier
314          * but they are grouped into the same bucket
315          * for BTF concern:
316          *   A type (t) that refers to another
317          *   type through t->type AND its size cannot
318          *   be determined without following the t->type.
319          *
320          * ptr does not fall into this bucket
321          * because its size is always sizeof(void *).
322          */
323         switch (BTF_INFO_KIND(t->info)) {
324         case BTF_KIND_TYPEDEF:
325         case BTF_KIND_VOLATILE:
326         case BTF_KIND_CONST:
327         case BTF_KIND_RESTRICT:
328                 return true;
329         }
330
331         return false;
332 }
333
334 bool btf_type_is_void(const struct btf_type *t)
335 {
336         return t == &btf_void;
337 }
338
339 static bool btf_type_is_fwd(const struct btf_type *t)
340 {
341         return BTF_INFO_KIND(t->info) == BTF_KIND_FWD;
342 }
343
344 static bool btf_type_nosize(const struct btf_type *t)
345 {
346         return btf_type_is_void(t) || btf_type_is_fwd(t) ||
347                btf_type_is_func(t) || btf_type_is_func_proto(t);
348 }
349
350 static bool btf_type_nosize_or_null(const struct btf_type *t)
351 {
352         return !t || btf_type_nosize(t);
353 }
354
355 /* union is only a special case of struct:
356  * all its offsetof(member) == 0
357  */
358 static bool btf_type_is_struct(const struct btf_type *t)
359 {
360         u8 kind = BTF_INFO_KIND(t->info);
361
362         return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
363 }
364
365 static bool __btf_type_is_struct(const struct btf_type *t)
366 {
367         return BTF_INFO_KIND(t->info) == BTF_KIND_STRUCT;
368 }
369
370 static bool btf_type_is_array(const struct btf_type *t)
371 {
372         return BTF_INFO_KIND(t->info) == BTF_KIND_ARRAY;
373 }
374
375 static bool btf_type_is_var(const struct btf_type *t)
376 {
377         return BTF_INFO_KIND(t->info) == BTF_KIND_VAR;
378 }
379
380 static bool btf_type_is_datasec(const struct btf_type *t)
381 {
382         return BTF_INFO_KIND(t->info) == BTF_KIND_DATASEC;
383 }
384
385 s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind)
386 {
387         const struct btf_type *t;
388         const char *tname;
389         u32 i;
390
391         for (i = 1; i <= btf->nr_types; i++) {
392                 t = btf->types[i];
393                 if (BTF_INFO_KIND(t->info) != kind)
394                         continue;
395
396                 tname = btf_name_by_offset(btf, t->name_off);
397                 if (!strcmp(tname, name))
398                         return i;
399         }
400
401         return -ENOENT;
402 }
403
404 const struct btf_type *btf_type_skip_modifiers(const struct btf *btf,
405                                                u32 id, u32 *res_id)
406 {
407         const struct btf_type *t = btf_type_by_id(btf, id);
408
409         while (btf_type_is_modifier(t)) {
410                 id = t->type;
411                 t = btf_type_by_id(btf, t->type);
412         }
413
414         if (res_id)
415                 *res_id = id;
416
417         return t;
418 }
419
420 const struct btf_type *btf_type_resolve_ptr(const struct btf *btf,
421                                             u32 id, u32 *res_id)
422 {
423         const struct btf_type *t;
424
425         t = btf_type_skip_modifiers(btf, id, NULL);
426         if (!btf_type_is_ptr(t))
427                 return NULL;
428
429         return btf_type_skip_modifiers(btf, t->type, res_id);
430 }
431
432 const struct btf_type *btf_type_resolve_func_ptr(const struct btf *btf,
433                                                  u32 id, u32 *res_id)
434 {
435         const struct btf_type *ptype;
436
437         ptype = btf_type_resolve_ptr(btf, id, res_id);
438         if (ptype && btf_type_is_func_proto(ptype))
439                 return ptype;
440
441         return NULL;
442 }
443
444 /* Types that act only as a source, not sink or intermediate
445  * type when resolving.
446  */
447 static bool btf_type_is_resolve_source_only(const struct btf_type *t)
448 {
449         return btf_type_is_var(t) ||
450                btf_type_is_datasec(t);
451 }
452
453 /* What types need to be resolved?
454  *
455  * btf_type_is_modifier() is an obvious one.
456  *
457  * btf_type_is_struct() because its member refers to
458  * another type (through member->type).
459  *
460  * btf_type_is_var() because the variable refers to
461  * another type. btf_type_is_datasec() holds multiple
462  * btf_type_is_var() types that need resolving.
463  *
464  * btf_type_is_array() because its element (array->type)
465  * refers to another type.  Array can be thought of a
466  * special case of struct while array just has the same
467  * member-type repeated by array->nelems of times.
468  */
469 static bool btf_type_needs_resolve(const struct btf_type *t)
470 {
471         return btf_type_is_modifier(t) ||
472                btf_type_is_ptr(t) ||
473                btf_type_is_struct(t) ||
474                btf_type_is_array(t) ||
475                btf_type_is_var(t) ||
476                btf_type_is_datasec(t);
477 }
478
479 /* t->size can be used */
480 static bool btf_type_has_size(const struct btf_type *t)
481 {
482         switch (BTF_INFO_KIND(t->info)) {
483         case BTF_KIND_INT:
484         case BTF_KIND_STRUCT:
485         case BTF_KIND_UNION:
486         case BTF_KIND_ENUM:
487         case BTF_KIND_DATASEC:
488                 return true;
489         }
490
491         return false;
492 }
493
494 static const char *btf_int_encoding_str(u8 encoding)
495 {
496         if (encoding == 0)
497                 return "(none)";
498         else if (encoding == BTF_INT_SIGNED)
499                 return "SIGNED";
500         else if (encoding == BTF_INT_CHAR)
501                 return "CHAR";
502         else if (encoding == BTF_INT_BOOL)
503                 return "BOOL";
504         else
505                 return "UNKN";
506 }
507
508 static u32 btf_type_int(const struct btf_type *t)
509 {
510         return *(u32 *)(t + 1);
511 }
512
513 static const struct btf_array *btf_type_array(const struct btf_type *t)
514 {
515         return (const struct btf_array *)(t + 1);
516 }
517
518 static const struct btf_enum *btf_type_enum(const struct btf_type *t)
519 {
520         return (const struct btf_enum *)(t + 1);
521 }
522
523 static const struct btf_var *btf_type_var(const struct btf_type *t)
524 {
525         return (const struct btf_var *)(t + 1);
526 }
527
528 static const struct btf_var_secinfo *btf_type_var_secinfo(const struct btf_type *t)
529 {
530         return (const struct btf_var_secinfo *)(t + 1);
531 }
532
533 static const struct btf_kind_operations *btf_type_ops(const struct btf_type *t)
534 {
535         return kind_ops[BTF_INFO_KIND(t->info)];
536 }
537
538 static bool btf_name_offset_valid(const struct btf *btf, u32 offset)
539 {
540         return BTF_STR_OFFSET_VALID(offset) &&
541                 offset < btf->hdr.str_len;
542 }
543
544 static bool __btf_name_char_ok(char c, bool first, bool dot_ok)
545 {
546         if ((first ? !isalpha(c) :
547                      !isalnum(c)) &&
548             c != '_' &&
549             ((c == '.' && !dot_ok) ||
550               c != '.'))
551                 return false;
552         return true;
553 }
554
555 static bool __btf_name_valid(const struct btf *btf, u32 offset, bool dot_ok)
556 {
557         /* offset must be valid */
558         const char *src = &btf->strings[offset];
559         const char *src_limit;
560
561         if (!__btf_name_char_ok(*src, true, dot_ok))
562                 return false;
563
564         /* set a limit on identifier length */
565         src_limit = src + KSYM_NAME_LEN;
566         src++;
567         while (*src && src < src_limit) {
568                 if (!__btf_name_char_ok(*src, false, dot_ok))
569                         return false;
570                 src++;
571         }
572
573         return !*src;
574 }
575
576 /* Only C-style identifier is permitted. This can be relaxed if
577  * necessary.
578  */
579 static bool btf_name_valid_identifier(const struct btf *btf, u32 offset)
580 {
581         return __btf_name_valid(btf, offset, false);
582 }
583
584 static bool btf_name_valid_section(const struct btf *btf, u32 offset)
585 {
586         return __btf_name_valid(btf, offset, true);
587 }
588
589 static const char *__btf_name_by_offset(const struct btf *btf, u32 offset)
590 {
591         if (!offset)
592                 return "(anon)";
593         else if (offset < btf->hdr.str_len)
594                 return &btf->strings[offset];
595         else
596                 return "(invalid-name-offset)";
597 }
598
599 const char *btf_name_by_offset(const struct btf *btf, u32 offset)
600 {
601         if (offset < btf->hdr.str_len)
602                 return &btf->strings[offset];
603
604         return NULL;
605 }
606
607 const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id)
608 {
609         if (type_id > btf->nr_types)
610                 return NULL;
611
612         return btf->types[type_id];
613 }
614
615 /*
616  * Regular int is not a bit field and it must be either
617  * u8/u16/u32/u64 or __int128.
618  */
619 static bool btf_type_int_is_regular(const struct btf_type *t)
620 {
621         u8 nr_bits, nr_bytes;
622         u32 int_data;
623
624         int_data = btf_type_int(t);
625         nr_bits = BTF_INT_BITS(int_data);
626         nr_bytes = BITS_ROUNDUP_BYTES(nr_bits);
627         if (BITS_PER_BYTE_MASKED(nr_bits) ||
628             BTF_INT_OFFSET(int_data) ||
629             (nr_bytes != sizeof(u8) && nr_bytes != sizeof(u16) &&
630              nr_bytes != sizeof(u32) && nr_bytes != sizeof(u64) &&
631              nr_bytes != (2 * sizeof(u64)))) {
632                 return false;
633         }
634
635         return true;
636 }
637
638 /*
639  * Check that given struct member is a regular int with expected
640  * offset and size.
641  */
642 bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s,
643                            const struct btf_member *m,
644                            u32 expected_offset, u32 expected_size)
645 {
646         const struct btf_type *t;
647         u32 id, int_data;
648         u8 nr_bits;
649
650         id = m->type;
651         t = btf_type_id_size(btf, &id, NULL);
652         if (!t || !btf_type_is_int(t))
653                 return false;
654
655         int_data = btf_type_int(t);
656         nr_bits = BTF_INT_BITS(int_data);
657         if (btf_type_kflag(s)) {
658                 u32 bitfield_size = BTF_MEMBER_BITFIELD_SIZE(m->offset);
659                 u32 bit_offset = BTF_MEMBER_BIT_OFFSET(m->offset);
660
661                 /* if kflag set, int should be a regular int and
662                  * bit offset should be at byte boundary.
663                  */
664                 return !bitfield_size &&
665                        BITS_ROUNDUP_BYTES(bit_offset) == expected_offset &&
666                        BITS_ROUNDUP_BYTES(nr_bits) == expected_size;
667         }
668
669         if (BTF_INT_OFFSET(int_data) ||
670             BITS_PER_BYTE_MASKED(m->offset) ||
671             BITS_ROUNDUP_BYTES(m->offset) != expected_offset ||
672             BITS_PER_BYTE_MASKED(nr_bits) ||
673             BITS_ROUNDUP_BYTES(nr_bits) != expected_size)
674                 return false;
675
676         return true;
677 }
678
679 __printf(2, 3) static void __btf_verifier_log(struct bpf_verifier_log *log,
680                                               const char *fmt, ...)
681 {
682         va_list args;
683
684         va_start(args, fmt);
685         bpf_verifier_vlog(log, fmt, args);
686         va_end(args);
687 }
688
689 __printf(2, 3) static void btf_verifier_log(struct btf_verifier_env *env,
690                                             const char *fmt, ...)
691 {
692         struct bpf_verifier_log *log = &env->log;
693         va_list args;
694
695         if (!bpf_verifier_log_needed(log))
696                 return;
697
698         va_start(args, fmt);
699         bpf_verifier_vlog(log, fmt, args);
700         va_end(args);
701 }
702
703 __printf(4, 5) static void __btf_verifier_log_type(struct btf_verifier_env *env,
704                                                    const struct btf_type *t,
705                                                    bool log_details,
706                                                    const char *fmt, ...)
707 {
708         struct bpf_verifier_log *log = &env->log;
709         u8 kind = BTF_INFO_KIND(t->info);
710         struct btf *btf = env->btf;
711         va_list args;
712
713         if (!bpf_verifier_log_needed(log))
714                 return;
715
716         /* btf verifier prints all types it is processing via
717          * btf_verifier_log_type(..., fmt = NULL).
718          * Skip those prints for in-kernel BTF verification.
719          */
720         if (log->level == BPF_LOG_KERNEL && !fmt)
721                 return;
722
723         __btf_verifier_log(log, "[%u] %s %s%s",
724                            env->log_type_id,
725                            btf_kind_str[kind],
726                            __btf_name_by_offset(btf, t->name_off),
727                            log_details ? " " : "");
728
729         if (log_details)
730                 btf_type_ops(t)->log_details(env, t);
731
732         if (fmt && *fmt) {
733                 __btf_verifier_log(log, " ");
734                 va_start(args, fmt);
735                 bpf_verifier_vlog(log, fmt, args);
736                 va_end(args);
737         }
738
739         __btf_verifier_log(log, "\n");
740 }
741
742 #define btf_verifier_log_type(env, t, ...) \
743         __btf_verifier_log_type((env), (t), true, __VA_ARGS__)
744 #define btf_verifier_log_basic(env, t, ...) \
745         __btf_verifier_log_type((env), (t), false, __VA_ARGS__)
746
747 __printf(4, 5)
748 static void btf_verifier_log_member(struct btf_verifier_env *env,
749                                     const struct btf_type *struct_type,
750                                     const struct btf_member *member,
751                                     const char *fmt, ...)
752 {
753         struct bpf_verifier_log *log = &env->log;
754         struct btf *btf = env->btf;
755         va_list args;
756
757         if (!bpf_verifier_log_needed(log))
758                 return;
759
760         if (log->level == BPF_LOG_KERNEL && !fmt)
761                 return;
762         /* The CHECK_META phase already did a btf dump.
763          *
764          * If member is logged again, it must hit an error in
765          * parsing this member.  It is useful to print out which
766          * struct this member belongs to.
767          */
768         if (env->phase != CHECK_META)
769                 btf_verifier_log_type(env, struct_type, NULL);
770
771         if (btf_type_kflag(struct_type))
772                 __btf_verifier_log(log,
773                                    "\t%s type_id=%u bitfield_size=%u bits_offset=%u",
774                                    __btf_name_by_offset(btf, member->name_off),
775                                    member->type,
776                                    BTF_MEMBER_BITFIELD_SIZE(member->offset),
777                                    BTF_MEMBER_BIT_OFFSET(member->offset));
778         else
779                 __btf_verifier_log(log, "\t%s type_id=%u bits_offset=%u",
780                                    __btf_name_by_offset(btf, member->name_off),
781                                    member->type, member->offset);
782
783         if (fmt && *fmt) {
784                 __btf_verifier_log(log, " ");
785                 va_start(args, fmt);
786                 bpf_verifier_vlog(log, fmt, args);
787                 va_end(args);
788         }
789
790         __btf_verifier_log(log, "\n");
791 }
792
793 __printf(4, 5)
794 static void btf_verifier_log_vsi(struct btf_verifier_env *env,
795                                  const struct btf_type *datasec_type,
796                                  const struct btf_var_secinfo *vsi,
797                                  const char *fmt, ...)
798 {
799         struct bpf_verifier_log *log = &env->log;
800         va_list args;
801
802         if (!bpf_verifier_log_needed(log))
803                 return;
804         if (log->level == BPF_LOG_KERNEL && !fmt)
805                 return;
806         if (env->phase != CHECK_META)
807                 btf_verifier_log_type(env, datasec_type, NULL);
808
809         __btf_verifier_log(log, "\t type_id=%u offset=%u size=%u",
810                            vsi->type, vsi->offset, vsi->size);
811         if (fmt && *fmt) {
812                 __btf_verifier_log(log, " ");
813                 va_start(args, fmt);
814                 bpf_verifier_vlog(log, fmt, args);
815                 va_end(args);
816         }
817
818         __btf_verifier_log(log, "\n");
819 }
820
821 static void btf_verifier_log_hdr(struct btf_verifier_env *env,
822                                  u32 btf_data_size)
823 {
824         struct bpf_verifier_log *log = &env->log;
825         const struct btf *btf = env->btf;
826         const struct btf_header *hdr;
827
828         if (!bpf_verifier_log_needed(log))
829                 return;
830
831         if (log->level == BPF_LOG_KERNEL)
832                 return;
833         hdr = &btf->hdr;
834         __btf_verifier_log(log, "magic: 0x%x\n", hdr->magic);
835         __btf_verifier_log(log, "version: %u\n", hdr->version);
836         __btf_verifier_log(log, "flags: 0x%x\n", hdr->flags);
837         __btf_verifier_log(log, "hdr_len: %u\n", hdr->hdr_len);
838         __btf_verifier_log(log, "type_off: %u\n", hdr->type_off);
839         __btf_verifier_log(log, "type_len: %u\n", hdr->type_len);
840         __btf_verifier_log(log, "str_off: %u\n", hdr->str_off);
841         __btf_verifier_log(log, "str_len: %u\n", hdr->str_len);
842         __btf_verifier_log(log, "btf_total_size: %u\n", btf_data_size);
843 }
844
845 static int btf_add_type(struct btf_verifier_env *env, struct btf_type *t)
846 {
847         struct btf *btf = env->btf;
848
849         /* < 2 because +1 for btf_void which is always in btf->types[0].
850          * btf_void is not accounted in btf->nr_types because btf_void
851          * does not come from the BTF file.
852          */
853         if (btf->types_size - btf->nr_types < 2) {
854                 /* Expand 'types' array */
855
856                 struct btf_type **new_types;
857                 u32 expand_by, new_size;
858
859                 if (btf->types_size == BTF_MAX_TYPE) {
860                         btf_verifier_log(env, "Exceeded max num of types");
861                         return -E2BIG;
862                 }
863
864                 expand_by = max_t(u32, btf->types_size >> 2, 16);
865                 new_size = min_t(u32, BTF_MAX_TYPE,
866                                  btf->types_size + expand_by);
867
868                 new_types = kvcalloc(new_size, sizeof(*new_types),
869                                      GFP_KERNEL | __GFP_NOWARN);
870                 if (!new_types)
871                         return -ENOMEM;
872
873                 if (btf->nr_types == 0)
874                         new_types[0] = &btf_void;
875                 else
876                         memcpy(new_types, btf->types,
877                                sizeof(*btf->types) * (btf->nr_types + 1));
878
879                 kvfree(btf->types);
880                 btf->types = new_types;
881                 btf->types_size = new_size;
882         }
883
884         btf->types[++(btf->nr_types)] = t;
885
886         return 0;
887 }
888
889 static int btf_alloc_id(struct btf *btf)
890 {
891         int id;
892
893         idr_preload(GFP_KERNEL);
894         spin_lock_bh(&btf_idr_lock);
895         id = idr_alloc_cyclic(&btf_idr, btf, 1, INT_MAX, GFP_ATOMIC);
896         if (id > 0)
897                 btf->id = id;
898         spin_unlock_bh(&btf_idr_lock);
899         idr_preload_end();
900
901         if (WARN_ON_ONCE(!id))
902                 return -ENOSPC;
903
904         return id > 0 ? 0 : id;
905 }
906
907 static void btf_free_id(struct btf *btf)
908 {
909         unsigned long flags;
910
911         /*
912          * In map-in-map, calling map_delete_elem() on outer
913          * map will call bpf_map_put on the inner map.
914          * It will then eventually call btf_free_id()
915          * on the inner map.  Some of the map_delete_elem()
916          * implementation may have irq disabled, so
917          * we need to use the _irqsave() version instead
918          * of the _bh() version.
919          */
920         spin_lock_irqsave(&btf_idr_lock, flags);
921         idr_remove(&btf_idr, btf->id);
922         spin_unlock_irqrestore(&btf_idr_lock, flags);
923 }
924
925 static void btf_free(struct btf *btf)
926 {
927         kvfree(btf->types);
928         kvfree(btf->resolved_sizes);
929         kvfree(btf->resolved_ids);
930         kvfree(btf->data);
931         kfree(btf);
932 }
933
934 static void btf_free_rcu(struct rcu_head *rcu)
935 {
936         struct btf *btf = container_of(rcu, struct btf, rcu);
937
938         btf_free(btf);
939 }
940
941 void btf_put(struct btf *btf)
942 {
943         if (btf && refcount_dec_and_test(&btf->refcnt)) {
944                 btf_free_id(btf);
945                 call_rcu(&btf->rcu, btf_free_rcu);
946         }
947 }
948
949 static int env_resolve_init(struct btf_verifier_env *env)
950 {
951         struct btf *btf = env->btf;
952         u32 nr_types = btf->nr_types;
953         u32 *resolved_sizes = NULL;
954         u32 *resolved_ids = NULL;
955         u8 *visit_states = NULL;
956
957         /* +1 for btf_void */
958         resolved_sizes = kvcalloc(nr_types + 1, sizeof(*resolved_sizes),
959                                   GFP_KERNEL | __GFP_NOWARN);
960         if (!resolved_sizes)
961                 goto nomem;
962
963         resolved_ids = kvcalloc(nr_types + 1, sizeof(*resolved_ids),
964                                 GFP_KERNEL | __GFP_NOWARN);
965         if (!resolved_ids)
966                 goto nomem;
967
968         visit_states = kvcalloc(nr_types + 1, sizeof(*visit_states),
969                                 GFP_KERNEL | __GFP_NOWARN);
970         if (!visit_states)
971                 goto nomem;
972
973         btf->resolved_sizes = resolved_sizes;
974         btf->resolved_ids = resolved_ids;
975         env->visit_states = visit_states;
976
977         return 0;
978
979 nomem:
980         kvfree(resolved_sizes);
981         kvfree(resolved_ids);
982         kvfree(visit_states);
983         return -ENOMEM;
984 }
985
986 static void btf_verifier_env_free(struct btf_verifier_env *env)
987 {
988         kvfree(env->visit_states);
989         kfree(env);
990 }
991
992 static bool env_type_is_resolve_sink(const struct btf_verifier_env *env,
993                                      const struct btf_type *next_type)
994 {
995         switch (env->resolve_mode) {
996         case RESOLVE_TBD:
997                 /* int, enum or void is a sink */
998                 return !btf_type_needs_resolve(next_type);
999         case RESOLVE_PTR:
1000                 /* int, enum, void, struct, array, func or func_proto is a sink
1001                  * for ptr
1002                  */
1003                 return !btf_type_is_modifier(next_type) &&
1004                         !btf_type_is_ptr(next_type);
1005         case RESOLVE_STRUCT_OR_ARRAY:
1006                 /* int, enum, void, ptr, func or func_proto is a sink
1007                  * for struct and array
1008                  */
1009                 return !btf_type_is_modifier(next_type) &&
1010                         !btf_type_is_array(next_type) &&
1011                         !btf_type_is_struct(next_type);
1012         default:
1013                 BUG();
1014         }
1015 }
1016
1017 static bool env_type_is_resolved(const struct btf_verifier_env *env,
1018                                  u32 type_id)
1019 {
1020         return env->visit_states[type_id] == RESOLVED;
1021 }
1022
1023 static int env_stack_push(struct btf_verifier_env *env,
1024                           const struct btf_type *t, u32 type_id)
1025 {
1026         struct resolve_vertex *v;
1027
1028         if (env->top_stack == MAX_RESOLVE_DEPTH)
1029                 return -E2BIG;
1030
1031         if (env->visit_states[type_id] != NOT_VISITED)
1032                 return -EEXIST;
1033
1034         env->visit_states[type_id] = VISITED;
1035
1036         v = &env->stack[env->top_stack++];
1037         v->t = t;
1038         v->type_id = type_id;
1039         v->next_member = 0;
1040
1041         if (env->resolve_mode == RESOLVE_TBD) {
1042                 if (btf_type_is_ptr(t))
1043                         env->resolve_mode = RESOLVE_PTR;
1044                 else if (btf_type_is_struct(t) || btf_type_is_array(t))
1045                         env->resolve_mode = RESOLVE_STRUCT_OR_ARRAY;
1046         }
1047
1048         return 0;
1049 }
1050
1051 static void env_stack_set_next_member(struct btf_verifier_env *env,
1052                                       u16 next_member)
1053 {
1054         env->stack[env->top_stack - 1].next_member = next_member;
1055 }
1056
1057 static void env_stack_pop_resolved(struct btf_verifier_env *env,
1058                                    u32 resolved_type_id,
1059                                    u32 resolved_size)
1060 {
1061         u32 type_id = env->stack[--(env->top_stack)].type_id;
1062         struct btf *btf = env->btf;
1063
1064         btf->resolved_sizes[type_id] = resolved_size;
1065         btf->resolved_ids[type_id] = resolved_type_id;
1066         env->visit_states[type_id] = RESOLVED;
1067 }
1068
1069 static const struct resolve_vertex *env_stack_peak(struct btf_verifier_env *env)
1070 {
1071         return env->top_stack ? &env->stack[env->top_stack - 1] : NULL;
1072 }
1073
1074 /* Resolve the size of a passed-in "type"
1075  *
1076  * type: is an array (e.g. u32 array[x][y])
1077  * return type: type "u32[x][y]", i.e. BTF_KIND_ARRAY,
1078  * *type_size: (x * y * sizeof(u32)).  Hence, *type_size always
1079  *             corresponds to the return type.
1080  * *elem_type: u32
1081  * *total_nelems: (x * y).  Hence, individual elem size is
1082  *                (*type_size / *total_nelems)
1083  *
1084  * type: is not an array (e.g. const struct X)
1085  * return type: type "struct X"
1086  * *type_size: sizeof(struct X)
1087  * *elem_type: same as return type ("struct X")
1088  * *total_nelems: 1
1089  */
1090 const struct btf_type *
1091 btf_resolve_size(const struct btf *btf, const struct btf_type *type,
1092                  u32 *type_size, const struct btf_type **elem_type,
1093                  u32 *total_nelems)
1094 {
1095         const struct btf_type *array_type = NULL;
1096         const struct btf_array *array;
1097         u32 i, size, nelems = 1;
1098
1099         for (i = 0; i < MAX_RESOLVE_DEPTH; i++) {
1100                 switch (BTF_INFO_KIND(type->info)) {
1101                 /* type->size can be used */
1102                 case BTF_KIND_INT:
1103                 case BTF_KIND_STRUCT:
1104                 case BTF_KIND_UNION:
1105                 case BTF_KIND_ENUM:
1106                         size = type->size;
1107                         goto resolved;
1108
1109                 case BTF_KIND_PTR:
1110                         size = sizeof(void *);
1111                         goto resolved;
1112
1113                 /* Modifiers */
1114                 case BTF_KIND_TYPEDEF:
1115                 case BTF_KIND_VOLATILE:
1116                 case BTF_KIND_CONST:
1117                 case BTF_KIND_RESTRICT:
1118                         type = btf_type_by_id(btf, type->type);
1119                         break;
1120
1121                 case BTF_KIND_ARRAY:
1122                         if (!array_type)
1123                                 array_type = type;
1124                         array = btf_type_array(type);
1125                         if (nelems && array->nelems > U32_MAX / nelems)
1126                                 return ERR_PTR(-EINVAL);
1127                         nelems *= array->nelems;
1128                         type = btf_type_by_id(btf, array->type);
1129                         break;
1130
1131                 /* type without size */
1132                 default:
1133                         return ERR_PTR(-EINVAL);
1134                 }
1135         }
1136
1137         return ERR_PTR(-EINVAL);
1138
1139 resolved:
1140         if (nelems && size > U32_MAX / nelems)
1141                 return ERR_PTR(-EINVAL);
1142
1143         *type_size = nelems * size;
1144         if (total_nelems)
1145                 *total_nelems = nelems;
1146         if (elem_type)
1147                 *elem_type = type;
1148
1149         return array_type ? : type;
1150 }
1151
1152 /* The input param "type_id" must point to a needs_resolve type */
1153 static const struct btf_type *btf_type_id_resolve(const struct btf *btf,
1154                                                   u32 *type_id)
1155 {
1156         *type_id = btf->resolved_ids[*type_id];
1157         return btf_type_by_id(btf, *type_id);
1158 }
1159
1160 const struct btf_type *btf_type_id_size(const struct btf *btf,
1161                                         u32 *type_id, u32 *ret_size)
1162 {
1163         const struct btf_type *size_type;
1164         u32 size_type_id = *type_id;
1165         u32 size = 0;
1166
1167         size_type = btf_type_by_id(btf, size_type_id);
1168         if (btf_type_nosize_or_null(size_type))
1169                 return NULL;
1170
1171         if (btf_type_has_size(size_type)) {
1172                 size = size_type->size;
1173         } else if (btf_type_is_array(size_type)) {
1174                 size = btf->resolved_sizes[size_type_id];
1175         } else if (btf_type_is_ptr(size_type)) {
1176                 size = sizeof(void *);
1177         } else {
1178                 if (WARN_ON_ONCE(!btf_type_is_modifier(size_type) &&
1179                                  !btf_type_is_var(size_type)))
1180                         return NULL;
1181
1182                 size_type_id = btf->resolved_ids[size_type_id];
1183                 size_type = btf_type_by_id(btf, size_type_id);
1184                 if (btf_type_nosize_or_null(size_type))
1185                         return NULL;
1186                 else if (btf_type_has_size(size_type))
1187                         size = size_type->size;
1188                 else if (btf_type_is_array(size_type))
1189                         size = btf->resolved_sizes[size_type_id];
1190                 else if (btf_type_is_ptr(size_type))
1191                         size = sizeof(void *);
1192                 else
1193                         return NULL;
1194         }
1195
1196         *type_id = size_type_id;
1197         if (ret_size)
1198                 *ret_size = size;
1199
1200         return size_type;
1201 }
1202
1203 static int btf_df_check_member(struct btf_verifier_env *env,
1204                                const struct btf_type *struct_type,
1205                                const struct btf_member *member,
1206                                const struct btf_type *member_type)
1207 {
1208         btf_verifier_log_basic(env, struct_type,
1209                                "Unsupported check_member");
1210         return -EINVAL;
1211 }
1212
1213 static int btf_df_check_kflag_member(struct btf_verifier_env *env,
1214                                      const struct btf_type *struct_type,
1215                                      const struct btf_member *member,
1216                                      const struct btf_type *member_type)
1217 {
1218         btf_verifier_log_basic(env, struct_type,
1219                                "Unsupported check_kflag_member");
1220         return -EINVAL;
1221 }
1222
1223 /* Used for ptr, array and struct/union type members.
1224  * int, enum and modifier types have their specific callback functions.
1225  */
1226 static int btf_generic_check_kflag_member(struct btf_verifier_env *env,
1227                                           const struct btf_type *struct_type,
1228                                           const struct btf_member *member,
1229                                           const struct btf_type *member_type)
1230 {
1231         if (BTF_MEMBER_BITFIELD_SIZE(member->offset)) {
1232                 btf_verifier_log_member(env, struct_type, member,
1233                                         "Invalid member bitfield_size");
1234                 return -EINVAL;
1235         }
1236
1237         /* bitfield size is 0, so member->offset represents bit offset only.
1238          * It is safe to call non kflag check_member variants.
1239          */
1240         return btf_type_ops(member_type)->check_member(env, struct_type,
1241                                                        member,
1242                                                        member_type);
1243 }
1244
1245 static int btf_df_resolve(struct btf_verifier_env *env,
1246                           const struct resolve_vertex *v)
1247 {
1248         btf_verifier_log_basic(env, v->t, "Unsupported resolve");
1249         return -EINVAL;
1250 }
1251
1252 static void btf_df_seq_show(const struct btf *btf, const struct btf_type *t,
1253                             u32 type_id, void *data, u8 bits_offsets,
1254                             struct seq_file *m)
1255 {
1256         seq_printf(m, "<unsupported kind:%u>", BTF_INFO_KIND(t->info));
1257 }
1258
1259 static int btf_int_check_member(struct btf_verifier_env *env,
1260                                 const struct btf_type *struct_type,
1261                                 const struct btf_member *member,
1262                                 const struct btf_type *member_type)
1263 {
1264         u32 int_data = btf_type_int(member_type);
1265         u32 struct_bits_off = member->offset;
1266         u32 struct_size = struct_type->size;
1267         u32 nr_copy_bits;
1268         u32 bytes_offset;
1269
1270         if (U32_MAX - struct_bits_off < BTF_INT_OFFSET(int_data)) {
1271                 btf_verifier_log_member(env, struct_type, member,
1272                                         "bits_offset exceeds U32_MAX");
1273                 return -EINVAL;
1274         }
1275
1276         struct_bits_off += BTF_INT_OFFSET(int_data);
1277         bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
1278         nr_copy_bits = BTF_INT_BITS(int_data) +
1279                 BITS_PER_BYTE_MASKED(struct_bits_off);
1280
1281         if (nr_copy_bits > BITS_PER_U128) {
1282                 btf_verifier_log_member(env, struct_type, member,
1283                                         "nr_copy_bits exceeds 128");
1284                 return -EINVAL;
1285         }
1286
1287         if (struct_size < bytes_offset ||
1288             struct_size - bytes_offset < BITS_ROUNDUP_BYTES(nr_copy_bits)) {
1289                 btf_verifier_log_member(env, struct_type, member,
1290                                         "Member exceeds struct_size");
1291                 return -EINVAL;
1292         }
1293
1294         return 0;
1295 }
1296
1297 static int btf_int_check_kflag_member(struct btf_verifier_env *env,
1298                                       const struct btf_type *struct_type,
1299                                       const struct btf_member *member,
1300                                       const struct btf_type *member_type)
1301 {
1302         u32 struct_bits_off, nr_bits, nr_int_data_bits, bytes_offset;
1303         u32 int_data = btf_type_int(member_type);
1304         u32 struct_size = struct_type->size;
1305         u32 nr_copy_bits;
1306
1307         /* a regular int type is required for the kflag int member */
1308         if (!btf_type_int_is_regular(member_type)) {
1309                 btf_verifier_log_member(env, struct_type, member,
1310                                         "Invalid member base type");
1311                 return -EINVAL;
1312         }
1313
1314         /* check sanity of bitfield size */
1315         nr_bits = BTF_MEMBER_BITFIELD_SIZE(member->offset);
1316         struct_bits_off = BTF_MEMBER_BIT_OFFSET(member->offset);
1317         nr_int_data_bits = BTF_INT_BITS(int_data);
1318         if (!nr_bits) {
1319                 /* Not a bitfield member, member offset must be at byte
1320                  * boundary.
1321                  */
1322                 if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
1323                         btf_verifier_log_member(env, struct_type, member,
1324                                                 "Invalid member offset");
1325                         return -EINVAL;
1326                 }
1327
1328                 nr_bits = nr_int_data_bits;
1329         } else if (nr_bits > nr_int_data_bits) {
1330                 btf_verifier_log_member(env, struct_type, member,
1331                                         "Invalid member bitfield_size");
1332                 return -EINVAL;
1333         }
1334
1335         bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
1336         nr_copy_bits = nr_bits + BITS_PER_BYTE_MASKED(struct_bits_off);
1337         if (nr_copy_bits > BITS_PER_U128) {
1338                 btf_verifier_log_member(env, struct_type, member,
1339                                         "nr_copy_bits exceeds 128");
1340                 return -EINVAL;
1341         }
1342
1343         if (struct_size < bytes_offset ||
1344             struct_size - bytes_offset < BITS_ROUNDUP_BYTES(nr_copy_bits)) {
1345                 btf_verifier_log_member(env, struct_type, member,
1346                                         "Member exceeds struct_size");
1347                 return -EINVAL;
1348         }
1349
1350         return 0;
1351 }
1352
1353 static s32 btf_int_check_meta(struct btf_verifier_env *env,
1354                               const struct btf_type *t,
1355                               u32 meta_left)
1356 {
1357         u32 int_data, nr_bits, meta_needed = sizeof(int_data);
1358         u16 encoding;
1359
1360         if (meta_left < meta_needed) {
1361                 btf_verifier_log_basic(env, t,
1362                                        "meta_left:%u meta_needed:%u",
1363                                        meta_left, meta_needed);
1364                 return -EINVAL;
1365         }
1366
1367         if (btf_type_vlen(t)) {
1368                 btf_verifier_log_type(env, t, "vlen != 0");
1369                 return -EINVAL;
1370         }
1371
1372         if (btf_type_kflag(t)) {
1373                 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
1374                 return -EINVAL;
1375         }
1376
1377         int_data = btf_type_int(t);
1378         if (int_data & ~BTF_INT_MASK) {
1379                 btf_verifier_log_basic(env, t, "Invalid int_data:%x",
1380                                        int_data);
1381                 return -EINVAL;
1382         }
1383
1384         nr_bits = BTF_INT_BITS(int_data) + BTF_INT_OFFSET(int_data);
1385
1386         if (nr_bits > BITS_PER_U128) {
1387                 btf_verifier_log_type(env, t, "nr_bits exceeds %zu",
1388                                       BITS_PER_U128);
1389                 return -EINVAL;
1390         }
1391
1392         if (BITS_ROUNDUP_BYTES(nr_bits) > t->size) {
1393                 btf_verifier_log_type(env, t, "nr_bits exceeds type_size");
1394                 return -EINVAL;
1395         }
1396
1397         /*
1398          * Only one of the encoding bits is allowed and it
1399          * should be sufficient for the pretty print purpose (i.e. decoding).
1400          * Multiple bits can be allowed later if it is found
1401          * to be insufficient.
1402          */
1403         encoding = BTF_INT_ENCODING(int_data);
1404         if (encoding &&
1405             encoding != BTF_INT_SIGNED &&
1406             encoding != BTF_INT_CHAR &&
1407             encoding != BTF_INT_BOOL) {
1408                 btf_verifier_log_type(env, t, "Unsupported encoding");
1409                 return -ENOTSUPP;
1410         }
1411
1412         btf_verifier_log_type(env, t, NULL);
1413
1414         return meta_needed;
1415 }
1416
1417 static void btf_int_log(struct btf_verifier_env *env,
1418                         const struct btf_type *t)
1419 {
1420         int int_data = btf_type_int(t);
1421
1422         btf_verifier_log(env,
1423                          "size=%u bits_offset=%u nr_bits=%u encoding=%s",
1424                          t->size, BTF_INT_OFFSET(int_data),
1425                          BTF_INT_BITS(int_data),
1426                          btf_int_encoding_str(BTF_INT_ENCODING(int_data)));
1427 }
1428
1429 static void btf_int128_print(struct seq_file *m, void *data)
1430 {
1431         /* data points to a __int128 number.
1432          * Suppose
1433          *     int128_num = *(__int128 *)data;
1434          * The below formulas shows what upper_num and lower_num represents:
1435          *     upper_num = int128_num >> 64;
1436          *     lower_num = int128_num & 0xffffffffFFFFFFFFULL;
1437          */
1438         u64 upper_num, lower_num;
1439
1440 #ifdef __BIG_ENDIAN_BITFIELD
1441         upper_num = *(u64 *)data;
1442         lower_num = *(u64 *)(data + 8);
1443 #else
1444         upper_num = *(u64 *)(data + 8);
1445         lower_num = *(u64 *)data;
1446 #endif
1447         if (upper_num == 0)
1448                 seq_printf(m, "0x%llx", lower_num);
1449         else
1450                 seq_printf(m, "0x%llx%016llx", upper_num, lower_num);
1451 }
1452
1453 static void btf_int128_shift(u64 *print_num, u16 left_shift_bits,
1454                              u16 right_shift_bits)
1455 {
1456         u64 upper_num, lower_num;
1457
1458 #ifdef __BIG_ENDIAN_BITFIELD
1459         upper_num = print_num[0];
1460         lower_num = print_num[1];
1461 #else
1462         upper_num = print_num[1];
1463         lower_num = print_num[0];
1464 #endif
1465
1466         /* shake out un-needed bits by shift/or operations */
1467         if (left_shift_bits >= 64) {
1468                 upper_num = lower_num << (left_shift_bits - 64);
1469                 lower_num = 0;
1470         } else {
1471                 upper_num = (upper_num << left_shift_bits) |
1472                             (lower_num >> (64 - left_shift_bits));
1473                 lower_num = lower_num << left_shift_bits;
1474         }
1475
1476         if (right_shift_bits >= 64) {
1477                 lower_num = upper_num >> (right_shift_bits - 64);
1478                 upper_num = 0;
1479         } else {
1480                 lower_num = (lower_num >> right_shift_bits) |
1481                             (upper_num << (64 - right_shift_bits));
1482                 upper_num = upper_num >> right_shift_bits;
1483         }
1484
1485 #ifdef __BIG_ENDIAN_BITFIELD
1486         print_num[0] = upper_num;
1487         print_num[1] = lower_num;
1488 #else
1489         print_num[0] = lower_num;
1490         print_num[1] = upper_num;
1491 #endif
1492 }
1493
1494 static void btf_bitfield_seq_show(void *data, u8 bits_offset,
1495                                   u8 nr_bits, struct seq_file *m)
1496 {
1497         u16 left_shift_bits, right_shift_bits;
1498         u8 nr_copy_bytes;
1499         u8 nr_copy_bits;
1500         u64 print_num[2] = {};
1501
1502         nr_copy_bits = nr_bits + bits_offset;
1503         nr_copy_bytes = BITS_ROUNDUP_BYTES(nr_copy_bits);
1504
1505         memcpy(print_num, data, nr_copy_bytes);
1506
1507 #ifdef __BIG_ENDIAN_BITFIELD
1508         left_shift_bits = bits_offset;
1509 #else
1510         left_shift_bits = BITS_PER_U128 - nr_copy_bits;
1511 #endif
1512         right_shift_bits = BITS_PER_U128 - nr_bits;
1513
1514         btf_int128_shift(print_num, left_shift_bits, right_shift_bits);
1515         btf_int128_print(m, print_num);
1516 }
1517
1518
1519 static void btf_int_bits_seq_show(const struct btf *btf,
1520                                   const struct btf_type *t,
1521                                   void *data, u8 bits_offset,
1522                                   struct seq_file *m)
1523 {
1524         u32 int_data = btf_type_int(t);
1525         u8 nr_bits = BTF_INT_BITS(int_data);
1526         u8 total_bits_offset;
1527
1528         /*
1529          * bits_offset is at most 7.
1530          * BTF_INT_OFFSET() cannot exceed 128 bits.
1531          */
1532         total_bits_offset = bits_offset + BTF_INT_OFFSET(int_data);
1533         data += BITS_ROUNDDOWN_BYTES(total_bits_offset);
1534         bits_offset = BITS_PER_BYTE_MASKED(total_bits_offset);
1535         btf_bitfield_seq_show(data, bits_offset, nr_bits, m);
1536 }
1537
1538 static void btf_int_seq_show(const struct btf *btf, const struct btf_type *t,
1539                              u32 type_id, void *data, u8 bits_offset,
1540                              struct seq_file *m)
1541 {
1542         u32 int_data = btf_type_int(t);
1543         u8 encoding = BTF_INT_ENCODING(int_data);
1544         bool sign = encoding & BTF_INT_SIGNED;
1545         u8 nr_bits = BTF_INT_BITS(int_data);
1546
1547         if (bits_offset || BTF_INT_OFFSET(int_data) ||
1548             BITS_PER_BYTE_MASKED(nr_bits)) {
1549                 btf_int_bits_seq_show(btf, t, data, bits_offset, m);
1550                 return;
1551         }
1552
1553         switch (nr_bits) {
1554         case 128:
1555                 btf_int128_print(m, data);
1556                 break;
1557         case 64:
1558                 if (sign)
1559                         seq_printf(m, "%lld", *(s64 *)data);
1560                 else
1561                         seq_printf(m, "%llu", *(u64 *)data);
1562                 break;
1563         case 32:
1564                 if (sign)
1565                         seq_printf(m, "%d", *(s32 *)data);
1566                 else
1567                         seq_printf(m, "%u", *(u32 *)data);
1568                 break;
1569         case 16:
1570                 if (sign)
1571                         seq_printf(m, "%d", *(s16 *)data);
1572                 else
1573                         seq_printf(m, "%u", *(u16 *)data);
1574                 break;
1575         case 8:
1576                 if (sign)
1577                         seq_printf(m, "%d", *(s8 *)data);
1578                 else
1579                         seq_printf(m, "%u", *(u8 *)data);
1580                 break;
1581         default:
1582                 btf_int_bits_seq_show(btf, t, data, bits_offset, m);
1583         }
1584 }
1585
1586 static const struct btf_kind_operations int_ops = {
1587         .check_meta = btf_int_check_meta,
1588         .resolve = btf_df_resolve,
1589         .check_member = btf_int_check_member,
1590         .check_kflag_member = btf_int_check_kflag_member,
1591         .log_details = btf_int_log,
1592         .seq_show = btf_int_seq_show,
1593 };
1594
1595 static int btf_modifier_check_member(struct btf_verifier_env *env,
1596                                      const struct btf_type *struct_type,
1597                                      const struct btf_member *member,
1598                                      const struct btf_type *member_type)
1599 {
1600         const struct btf_type *resolved_type;
1601         u32 resolved_type_id = member->type;
1602         struct btf_member resolved_member;
1603         struct btf *btf = env->btf;
1604
1605         resolved_type = btf_type_id_size(btf, &resolved_type_id, NULL);
1606         if (!resolved_type) {
1607                 btf_verifier_log_member(env, struct_type, member,
1608                                         "Invalid member");
1609                 return -EINVAL;
1610         }
1611
1612         resolved_member = *member;
1613         resolved_member.type = resolved_type_id;
1614
1615         return btf_type_ops(resolved_type)->check_member(env, struct_type,
1616                                                          &resolved_member,
1617                                                          resolved_type);
1618 }
1619
1620 static int btf_modifier_check_kflag_member(struct btf_verifier_env *env,
1621                                            const struct btf_type *struct_type,
1622                                            const struct btf_member *member,
1623                                            const struct btf_type *member_type)
1624 {
1625         const struct btf_type *resolved_type;
1626         u32 resolved_type_id = member->type;
1627         struct btf_member resolved_member;
1628         struct btf *btf = env->btf;
1629
1630         resolved_type = btf_type_id_size(btf, &resolved_type_id, NULL);
1631         if (!resolved_type) {
1632                 btf_verifier_log_member(env, struct_type, member,
1633                                         "Invalid member");
1634                 return -EINVAL;
1635         }
1636
1637         resolved_member = *member;
1638         resolved_member.type = resolved_type_id;
1639
1640         return btf_type_ops(resolved_type)->check_kflag_member(env, struct_type,
1641                                                                &resolved_member,
1642                                                                resolved_type);
1643 }
1644
1645 static int btf_ptr_check_member(struct btf_verifier_env *env,
1646                                 const struct btf_type *struct_type,
1647                                 const struct btf_member *member,
1648                                 const struct btf_type *member_type)
1649 {
1650         u32 struct_size, struct_bits_off, bytes_offset;
1651
1652         struct_size = struct_type->size;
1653         struct_bits_off = member->offset;
1654         bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
1655
1656         if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
1657                 btf_verifier_log_member(env, struct_type, member,
1658                                         "Member is not byte aligned");
1659                 return -EINVAL;
1660         }
1661
1662         if (struct_size - bytes_offset < sizeof(void *)) {
1663                 btf_verifier_log_member(env, struct_type, member,
1664                                         "Member exceeds struct_size");
1665                 return -EINVAL;
1666         }
1667
1668         return 0;
1669 }
1670
1671 static int btf_ref_type_check_meta(struct btf_verifier_env *env,
1672                                    const struct btf_type *t,
1673                                    u32 meta_left)
1674 {
1675         if (btf_type_vlen(t)) {
1676                 btf_verifier_log_type(env, t, "vlen != 0");
1677                 return -EINVAL;
1678         }
1679
1680         if (btf_type_kflag(t)) {
1681                 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
1682                 return -EINVAL;
1683         }
1684
1685         if (!BTF_TYPE_ID_VALID(t->type)) {
1686                 btf_verifier_log_type(env, t, "Invalid type_id");
1687                 return -EINVAL;
1688         }
1689
1690         /* typedef type must have a valid name, and other ref types,
1691          * volatile, const, restrict, should have a null name.
1692          */
1693         if (BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF) {
1694                 if (!t->name_off ||
1695                     !btf_name_valid_identifier(env->btf, t->name_off)) {
1696                         btf_verifier_log_type(env, t, "Invalid name");
1697                         return -EINVAL;
1698                 }
1699         } else {
1700                 if (t->name_off) {
1701                         btf_verifier_log_type(env, t, "Invalid name");
1702                         return -EINVAL;
1703                 }
1704         }
1705
1706         btf_verifier_log_type(env, t, NULL);
1707
1708         return 0;
1709 }
1710
1711 static int btf_modifier_resolve(struct btf_verifier_env *env,
1712                                 const struct resolve_vertex *v)
1713 {
1714         const struct btf_type *t = v->t;
1715         const struct btf_type *next_type;
1716         u32 next_type_id = t->type;
1717         struct btf *btf = env->btf;
1718
1719         next_type = btf_type_by_id(btf, next_type_id);
1720         if (!next_type || btf_type_is_resolve_source_only(next_type)) {
1721                 btf_verifier_log_type(env, v->t, "Invalid type_id");
1722                 return -EINVAL;
1723         }
1724
1725         if (!env_type_is_resolve_sink(env, next_type) &&
1726             !env_type_is_resolved(env, next_type_id))
1727                 return env_stack_push(env, next_type, next_type_id);
1728
1729         /* Figure out the resolved next_type_id with size.
1730          * They will be stored in the current modifier's
1731          * resolved_ids and resolved_sizes such that it can
1732          * save us a few type-following when we use it later (e.g. in
1733          * pretty print).
1734          */
1735         if (!btf_type_id_size(btf, &next_type_id, NULL)) {
1736                 if (env_type_is_resolved(env, next_type_id))
1737                         next_type = btf_type_id_resolve(btf, &next_type_id);
1738
1739                 /* "typedef void new_void", "const void"...etc */
1740                 if (!btf_type_is_void(next_type) &&
1741                     !btf_type_is_fwd(next_type) &&
1742                     !btf_type_is_func_proto(next_type)) {
1743                         btf_verifier_log_type(env, v->t, "Invalid type_id");
1744                         return -EINVAL;
1745                 }
1746         }
1747
1748         env_stack_pop_resolved(env, next_type_id, 0);
1749
1750         return 0;
1751 }
1752
1753 static int btf_var_resolve(struct btf_verifier_env *env,
1754                            const struct resolve_vertex *v)
1755 {
1756         const struct btf_type *next_type;
1757         const struct btf_type *t = v->t;
1758         u32 next_type_id = t->type;
1759         struct btf *btf = env->btf;
1760
1761         next_type = btf_type_by_id(btf, next_type_id);
1762         if (!next_type || btf_type_is_resolve_source_only(next_type)) {
1763                 btf_verifier_log_type(env, v->t, "Invalid type_id");
1764                 return -EINVAL;
1765         }
1766
1767         if (!env_type_is_resolve_sink(env, next_type) &&
1768             !env_type_is_resolved(env, next_type_id))
1769                 return env_stack_push(env, next_type, next_type_id);
1770
1771         if (btf_type_is_modifier(next_type)) {
1772                 const struct btf_type *resolved_type;
1773                 u32 resolved_type_id;
1774
1775                 resolved_type_id = next_type_id;
1776                 resolved_type = btf_type_id_resolve(btf, &resolved_type_id);
1777
1778                 if (btf_type_is_ptr(resolved_type) &&
1779                     !env_type_is_resolve_sink(env, resolved_type) &&
1780                     !env_type_is_resolved(env, resolved_type_id))
1781                         return env_stack_push(env, resolved_type,
1782                                               resolved_type_id);
1783         }
1784
1785         /* We must resolve to something concrete at this point, no
1786          * forward types or similar that would resolve to size of
1787          * zero is allowed.
1788          */
1789         if (!btf_type_id_size(btf, &next_type_id, NULL)) {
1790                 btf_verifier_log_type(env, v->t, "Invalid type_id");
1791                 return -EINVAL;
1792         }
1793
1794         env_stack_pop_resolved(env, next_type_id, 0);
1795
1796         return 0;
1797 }
1798
1799 static int btf_ptr_resolve(struct btf_verifier_env *env,
1800                            const struct resolve_vertex *v)
1801 {
1802         const struct btf_type *next_type;
1803         const struct btf_type *t = v->t;
1804         u32 next_type_id = t->type;
1805         struct btf *btf = env->btf;
1806
1807         next_type = btf_type_by_id(btf, next_type_id);
1808         if (!next_type || btf_type_is_resolve_source_only(next_type)) {
1809                 btf_verifier_log_type(env, v->t, "Invalid type_id");
1810                 return -EINVAL;
1811         }
1812
1813         if (!env_type_is_resolve_sink(env, next_type) &&
1814             !env_type_is_resolved(env, next_type_id))
1815                 return env_stack_push(env, next_type, next_type_id);
1816
1817         /* If the modifier was RESOLVED during RESOLVE_STRUCT_OR_ARRAY,
1818          * the modifier may have stopped resolving when it was resolved
1819          * to a ptr (last-resolved-ptr).
1820          *
1821          * We now need to continue from the last-resolved-ptr to
1822          * ensure the last-resolved-ptr will not referring back to
1823          * the currenct ptr (t).
1824          */
1825         if (btf_type_is_modifier(next_type)) {
1826                 const struct btf_type *resolved_type;
1827                 u32 resolved_type_id;
1828
1829                 resolved_type_id = next_type_id;
1830                 resolved_type = btf_type_id_resolve(btf, &resolved_type_id);
1831
1832                 if (btf_type_is_ptr(resolved_type) &&
1833                     !env_type_is_resolve_sink(env, resolved_type) &&
1834                     !env_type_is_resolved(env, resolved_type_id))
1835                         return env_stack_push(env, resolved_type,
1836                                               resolved_type_id);
1837         }
1838
1839         if (!btf_type_id_size(btf, &next_type_id, NULL)) {
1840                 if (env_type_is_resolved(env, next_type_id))
1841                         next_type = btf_type_id_resolve(btf, &next_type_id);
1842
1843                 if (!btf_type_is_void(next_type) &&
1844                     !btf_type_is_fwd(next_type) &&
1845                     !btf_type_is_func_proto(next_type)) {
1846                         btf_verifier_log_type(env, v->t, "Invalid type_id");
1847                         return -EINVAL;
1848                 }
1849         }
1850
1851         env_stack_pop_resolved(env, next_type_id, 0);
1852
1853         return 0;
1854 }
1855
1856 static void btf_modifier_seq_show(const struct btf *btf,
1857                                   const struct btf_type *t,
1858                                   u32 type_id, void *data,
1859                                   u8 bits_offset, struct seq_file *m)
1860 {
1861         if (btf->resolved_ids)
1862                 t = btf_type_id_resolve(btf, &type_id);
1863         else
1864                 t = btf_type_skip_modifiers(btf, type_id, NULL);
1865
1866         btf_type_ops(t)->seq_show(btf, t, type_id, data, bits_offset, m);
1867 }
1868
1869 static void btf_var_seq_show(const struct btf *btf, const struct btf_type *t,
1870                              u32 type_id, void *data, u8 bits_offset,
1871                              struct seq_file *m)
1872 {
1873         t = btf_type_id_resolve(btf, &type_id);
1874
1875         btf_type_ops(t)->seq_show(btf, t, type_id, data, bits_offset, m);
1876 }
1877
1878 static void btf_ptr_seq_show(const struct btf *btf, const struct btf_type *t,
1879                              u32 type_id, void *data, u8 bits_offset,
1880                              struct seq_file *m)
1881 {
1882         /* It is a hashed value */
1883         seq_printf(m, "%p", *(void **)data);
1884 }
1885
1886 static void btf_ref_type_log(struct btf_verifier_env *env,
1887                              const struct btf_type *t)
1888 {
1889         btf_verifier_log(env, "type_id=%u", t->type);
1890 }
1891
1892 static struct btf_kind_operations modifier_ops = {
1893         .check_meta = btf_ref_type_check_meta,
1894         .resolve = btf_modifier_resolve,
1895         .check_member = btf_modifier_check_member,
1896         .check_kflag_member = btf_modifier_check_kflag_member,
1897         .log_details = btf_ref_type_log,
1898         .seq_show = btf_modifier_seq_show,
1899 };
1900
1901 static struct btf_kind_operations ptr_ops = {
1902         .check_meta = btf_ref_type_check_meta,
1903         .resolve = btf_ptr_resolve,
1904         .check_member = btf_ptr_check_member,
1905         .check_kflag_member = btf_generic_check_kflag_member,
1906         .log_details = btf_ref_type_log,
1907         .seq_show = btf_ptr_seq_show,
1908 };
1909
1910 static s32 btf_fwd_check_meta(struct btf_verifier_env *env,
1911                               const struct btf_type *t,
1912                               u32 meta_left)
1913 {
1914         if (btf_type_vlen(t)) {
1915                 btf_verifier_log_type(env, t, "vlen != 0");
1916                 return -EINVAL;
1917         }
1918
1919         if (t->type) {
1920                 btf_verifier_log_type(env, t, "type != 0");
1921                 return -EINVAL;
1922         }
1923
1924         /* fwd type must have a valid name */
1925         if (!t->name_off ||
1926             !btf_name_valid_identifier(env->btf, t->name_off)) {
1927                 btf_verifier_log_type(env, t, "Invalid name");
1928                 return -EINVAL;
1929         }
1930
1931         btf_verifier_log_type(env, t, NULL);
1932
1933         return 0;
1934 }
1935
1936 static void btf_fwd_type_log(struct btf_verifier_env *env,
1937                              const struct btf_type *t)
1938 {
1939         btf_verifier_log(env, "%s", btf_type_kflag(t) ? "union" : "struct");
1940 }
1941
1942 static struct btf_kind_operations fwd_ops = {
1943         .check_meta = btf_fwd_check_meta,
1944         .resolve = btf_df_resolve,
1945         .check_member = btf_df_check_member,
1946         .check_kflag_member = btf_df_check_kflag_member,
1947         .log_details = btf_fwd_type_log,
1948         .seq_show = btf_df_seq_show,
1949 };
1950
1951 static int btf_array_check_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 = member->offset;
1957         u32 struct_size, bytes_offset;
1958         u32 array_type_id, array_size;
1959         struct btf *btf = env->btf;
1960
1961         if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
1962                 btf_verifier_log_member(env, struct_type, member,
1963                                         "Member is not byte aligned");
1964                 return -EINVAL;
1965         }
1966
1967         array_type_id = member->type;
1968         btf_type_id_size(btf, &array_type_id, &array_size);
1969         struct_size = struct_type->size;
1970         bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
1971         if (struct_size - bytes_offset < array_size) {
1972                 btf_verifier_log_member(env, struct_type, member,
1973                                         "Member exceeds struct_size");
1974                 return -EINVAL;
1975         }
1976
1977         return 0;
1978 }
1979
1980 static s32 btf_array_check_meta(struct btf_verifier_env *env,
1981                                 const struct btf_type *t,
1982                                 u32 meta_left)
1983 {
1984         const struct btf_array *array = btf_type_array(t);
1985         u32 meta_needed = sizeof(*array);
1986
1987         if (meta_left < meta_needed) {
1988                 btf_verifier_log_basic(env, t,
1989                                        "meta_left:%u meta_needed:%u",
1990                                        meta_left, meta_needed);
1991                 return -EINVAL;
1992         }
1993
1994         /* array type should not have a name */
1995         if (t->name_off) {
1996                 btf_verifier_log_type(env, t, "Invalid name");
1997                 return -EINVAL;
1998         }
1999
2000         if (btf_type_vlen(t)) {
2001                 btf_verifier_log_type(env, t, "vlen != 0");
2002                 return -EINVAL;
2003         }
2004
2005         if (btf_type_kflag(t)) {
2006                 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
2007                 return -EINVAL;
2008         }
2009
2010         if (t->size) {
2011                 btf_verifier_log_type(env, t, "size != 0");
2012                 return -EINVAL;
2013         }
2014
2015         /* Array elem type and index type cannot be in type void,
2016          * so !array->type and !array->index_type are not allowed.
2017          */
2018         if (!array->type || !BTF_TYPE_ID_VALID(array->type)) {
2019                 btf_verifier_log_type(env, t, "Invalid elem");
2020                 return -EINVAL;
2021         }
2022
2023         if (!array->index_type || !BTF_TYPE_ID_VALID(array->index_type)) {
2024                 btf_verifier_log_type(env, t, "Invalid index");
2025                 return -EINVAL;
2026         }
2027
2028         btf_verifier_log_type(env, t, NULL);
2029
2030         return meta_needed;
2031 }
2032
2033 static int btf_array_resolve(struct btf_verifier_env *env,
2034                              const struct resolve_vertex *v)
2035 {
2036         const struct btf_array *array = btf_type_array(v->t);
2037         const struct btf_type *elem_type, *index_type;
2038         u32 elem_type_id, index_type_id;
2039         struct btf *btf = env->btf;
2040         u32 elem_size;
2041
2042         /* Check array->index_type */
2043         index_type_id = array->index_type;
2044         index_type = btf_type_by_id(btf, index_type_id);
2045         if (btf_type_nosize_or_null(index_type) ||
2046             btf_type_is_resolve_source_only(index_type)) {
2047                 btf_verifier_log_type(env, v->t, "Invalid index");
2048                 return -EINVAL;
2049         }
2050
2051         if (!env_type_is_resolve_sink(env, index_type) &&
2052             !env_type_is_resolved(env, index_type_id))
2053                 return env_stack_push(env, index_type, index_type_id);
2054
2055         index_type = btf_type_id_size(btf, &index_type_id, NULL);
2056         if (!index_type || !btf_type_is_int(index_type) ||
2057             !btf_type_int_is_regular(index_type)) {
2058                 btf_verifier_log_type(env, v->t, "Invalid index");
2059                 return -EINVAL;
2060         }
2061
2062         /* Check array->type */
2063         elem_type_id = array->type;
2064         elem_type = btf_type_by_id(btf, elem_type_id);
2065         if (btf_type_nosize_or_null(elem_type) ||
2066             btf_type_is_resolve_source_only(elem_type)) {
2067                 btf_verifier_log_type(env, v->t,
2068                                       "Invalid elem");
2069                 return -EINVAL;
2070         }
2071
2072         if (!env_type_is_resolve_sink(env, elem_type) &&
2073             !env_type_is_resolved(env, elem_type_id))
2074                 return env_stack_push(env, elem_type, elem_type_id);
2075
2076         elem_type = btf_type_id_size(btf, &elem_type_id, &elem_size);
2077         if (!elem_type) {
2078                 btf_verifier_log_type(env, v->t, "Invalid elem");
2079                 return -EINVAL;
2080         }
2081
2082         if (btf_type_is_int(elem_type) && !btf_type_int_is_regular(elem_type)) {
2083                 btf_verifier_log_type(env, v->t, "Invalid array of int");
2084                 return -EINVAL;
2085         }
2086
2087         if (array->nelems && elem_size > U32_MAX / array->nelems) {
2088                 btf_verifier_log_type(env, v->t,
2089                                       "Array size overflows U32_MAX");
2090                 return -EINVAL;
2091         }
2092
2093         env_stack_pop_resolved(env, elem_type_id, elem_size * array->nelems);
2094
2095         return 0;
2096 }
2097
2098 static void btf_array_log(struct btf_verifier_env *env,
2099                           const struct btf_type *t)
2100 {
2101         const struct btf_array *array = btf_type_array(t);
2102
2103         btf_verifier_log(env, "type_id=%u index_type_id=%u nr_elems=%u",
2104                          array->type, array->index_type, array->nelems);
2105 }
2106
2107 static void btf_array_seq_show(const struct btf *btf, const struct btf_type *t,
2108                                u32 type_id, void *data, u8 bits_offset,
2109                                struct seq_file *m)
2110 {
2111         const struct btf_array *array = btf_type_array(t);
2112         const struct btf_kind_operations *elem_ops;
2113         const struct btf_type *elem_type;
2114         u32 i, elem_size, elem_type_id;
2115
2116         elem_type_id = array->type;
2117         elem_type = btf_type_id_size(btf, &elem_type_id, &elem_size);
2118         elem_ops = btf_type_ops(elem_type);
2119         seq_puts(m, "[");
2120         for (i = 0; i < array->nelems; i++) {
2121                 if (i)
2122                         seq_puts(m, ",");
2123
2124                 elem_ops->seq_show(btf, elem_type, elem_type_id, data,
2125                                    bits_offset, m);
2126                 data += elem_size;
2127         }
2128         seq_puts(m, "]");
2129 }
2130
2131 static struct btf_kind_operations array_ops = {
2132         .check_meta = btf_array_check_meta,
2133         .resolve = btf_array_resolve,
2134         .check_member = btf_array_check_member,
2135         .check_kflag_member = btf_generic_check_kflag_member,
2136         .log_details = btf_array_log,
2137         .seq_show = btf_array_seq_show,
2138 };
2139
2140 static int btf_struct_check_member(struct btf_verifier_env *env,
2141                                    const struct btf_type *struct_type,
2142                                    const struct btf_member *member,
2143                                    const struct btf_type *member_type)
2144 {
2145         u32 struct_bits_off = member->offset;
2146         u32 struct_size, bytes_offset;
2147
2148         if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
2149                 btf_verifier_log_member(env, struct_type, member,
2150                                         "Member is not byte aligned");
2151                 return -EINVAL;
2152         }
2153
2154         struct_size = struct_type->size;
2155         bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
2156         if (struct_size - bytes_offset < member_type->size) {
2157                 btf_verifier_log_member(env, struct_type, member,
2158                                         "Member exceeds struct_size");
2159                 return -EINVAL;
2160         }
2161
2162         return 0;
2163 }
2164
2165 static s32 btf_struct_check_meta(struct btf_verifier_env *env,
2166                                  const struct btf_type *t,
2167                                  u32 meta_left)
2168 {
2169         bool is_union = BTF_INFO_KIND(t->info) == BTF_KIND_UNION;
2170         const struct btf_member *member;
2171         u32 meta_needed, last_offset;
2172         struct btf *btf = env->btf;
2173         u32 struct_size = t->size;
2174         u32 offset;
2175         u16 i;
2176
2177         meta_needed = btf_type_vlen(t) * sizeof(*member);
2178         if (meta_left < meta_needed) {
2179                 btf_verifier_log_basic(env, t,
2180                                        "meta_left:%u meta_needed:%u",
2181                                        meta_left, meta_needed);
2182                 return -EINVAL;
2183         }
2184
2185         /* struct type either no name or a valid one */
2186         if (t->name_off &&
2187             !btf_name_valid_identifier(env->btf, t->name_off)) {
2188                 btf_verifier_log_type(env, t, "Invalid name");
2189                 return -EINVAL;
2190         }
2191
2192         btf_verifier_log_type(env, t, NULL);
2193
2194         last_offset = 0;
2195         for_each_member(i, t, member) {
2196                 if (!btf_name_offset_valid(btf, member->name_off)) {
2197                         btf_verifier_log_member(env, t, member,
2198                                                 "Invalid member name_offset:%u",
2199                                                 member->name_off);
2200                         return -EINVAL;
2201                 }
2202
2203                 /* struct member either no name or a valid one */
2204                 if (member->name_off &&
2205                     !btf_name_valid_identifier(btf, member->name_off)) {
2206                         btf_verifier_log_member(env, t, member, "Invalid name");
2207                         return -EINVAL;
2208                 }
2209                 /* A member cannot be in type void */
2210                 if (!member->type || !BTF_TYPE_ID_VALID(member->type)) {
2211                         btf_verifier_log_member(env, t, member,
2212                                                 "Invalid type_id");
2213                         return -EINVAL;
2214                 }
2215
2216                 offset = btf_member_bit_offset(t, member);
2217                 if (is_union && offset) {
2218                         btf_verifier_log_member(env, t, member,
2219                                                 "Invalid member bits_offset");
2220                         return -EINVAL;
2221                 }
2222
2223                 /*
2224                  * ">" instead of ">=" because the last member could be
2225                  * "char a[0];"
2226                  */
2227                 if (last_offset > offset) {
2228                         btf_verifier_log_member(env, t, member,
2229                                                 "Invalid member bits_offset");
2230                         return -EINVAL;
2231                 }
2232
2233                 if (BITS_ROUNDUP_BYTES(offset) > struct_size) {
2234                         btf_verifier_log_member(env, t, member,
2235                                                 "Member bits_offset exceeds its struct size");
2236                         return -EINVAL;
2237                 }
2238
2239                 btf_verifier_log_member(env, t, member, NULL);
2240                 last_offset = offset;
2241         }
2242
2243         return meta_needed;
2244 }
2245
2246 static int btf_struct_resolve(struct btf_verifier_env *env,
2247                               const struct resolve_vertex *v)
2248 {
2249         const struct btf_member *member;
2250         int err;
2251         u16 i;
2252
2253         /* Before continue resolving the next_member,
2254          * ensure the last member is indeed resolved to a
2255          * type with size info.
2256          */
2257         if (v->next_member) {
2258                 const struct btf_type *last_member_type;
2259                 const struct btf_member *last_member;
2260                 u16 last_member_type_id;
2261
2262                 last_member = btf_type_member(v->t) + v->next_member - 1;
2263                 last_member_type_id = last_member->type;
2264                 if (WARN_ON_ONCE(!env_type_is_resolved(env,
2265                                                        last_member_type_id)))
2266                         return -EINVAL;
2267
2268                 last_member_type = btf_type_by_id(env->btf,
2269                                                   last_member_type_id);
2270                 if (btf_type_kflag(v->t))
2271                         err = btf_type_ops(last_member_type)->check_kflag_member(env, v->t,
2272                                                                 last_member,
2273                                                                 last_member_type);
2274                 else
2275                         err = btf_type_ops(last_member_type)->check_member(env, v->t,
2276                                                                 last_member,
2277                                                                 last_member_type);
2278                 if (err)
2279                         return err;
2280         }
2281
2282         for_each_member_from(i, v->next_member, v->t, member) {
2283                 u32 member_type_id = member->type;
2284                 const struct btf_type *member_type = btf_type_by_id(env->btf,
2285                                                                 member_type_id);
2286
2287                 if (btf_type_nosize_or_null(member_type) ||
2288                     btf_type_is_resolve_source_only(member_type)) {
2289                         btf_verifier_log_member(env, v->t, member,
2290                                                 "Invalid member");
2291                         return -EINVAL;
2292                 }
2293
2294                 if (!env_type_is_resolve_sink(env, member_type) &&
2295                     !env_type_is_resolved(env, member_type_id)) {
2296                         env_stack_set_next_member(env, i + 1);
2297                         return env_stack_push(env, member_type, member_type_id);
2298                 }
2299
2300                 if (btf_type_kflag(v->t))
2301                         err = btf_type_ops(member_type)->check_kflag_member(env, v->t,
2302                                                                             member,
2303                                                                             member_type);
2304                 else
2305                         err = btf_type_ops(member_type)->check_member(env, v->t,
2306                                                                       member,
2307                                                                       member_type);
2308                 if (err)
2309                         return err;
2310         }
2311
2312         env_stack_pop_resolved(env, 0, 0);
2313
2314         return 0;
2315 }
2316
2317 static void btf_struct_log(struct btf_verifier_env *env,
2318                            const struct btf_type *t)
2319 {
2320         btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t));
2321 }
2322
2323 /* find 'struct bpf_spin_lock' in map value.
2324  * return >= 0 offset if found
2325  * and < 0 in case of error
2326  */
2327 int btf_find_spin_lock(const struct btf *btf, const struct btf_type *t)
2328 {
2329         const struct btf_member *member;
2330         u32 i, off = -ENOENT;
2331
2332         if (!__btf_type_is_struct(t))
2333                 return -EINVAL;
2334
2335         for_each_member(i, t, member) {
2336                 const struct btf_type *member_type = btf_type_by_id(btf,
2337                                                                     member->type);
2338                 if (!__btf_type_is_struct(member_type))
2339                         continue;
2340                 if (member_type->size != sizeof(struct bpf_spin_lock))
2341                         continue;
2342                 if (strcmp(__btf_name_by_offset(btf, member_type->name_off),
2343                            "bpf_spin_lock"))
2344                         continue;
2345                 if (off != -ENOENT)
2346                         /* only one 'struct bpf_spin_lock' is allowed */
2347                         return -E2BIG;
2348                 off = btf_member_bit_offset(t, member);
2349                 if (off % 8)
2350                         /* valid C code cannot generate such BTF */
2351                         return -EINVAL;
2352                 off /= 8;
2353                 if (off % __alignof__(struct bpf_spin_lock))
2354                         /* valid struct bpf_spin_lock will be 4 byte aligned */
2355                         return -EINVAL;
2356         }
2357         return off;
2358 }
2359
2360 static void btf_struct_seq_show(const struct btf *btf, const struct btf_type *t,
2361                                 u32 type_id, void *data, u8 bits_offset,
2362                                 struct seq_file *m)
2363 {
2364         const char *seq = BTF_INFO_KIND(t->info) == BTF_KIND_UNION ? "|" : ",";
2365         const struct btf_member *member;
2366         u32 i;
2367
2368         seq_puts(m, "{");
2369         for_each_member(i, t, member) {
2370                 const struct btf_type *member_type = btf_type_by_id(btf,
2371                                                                 member->type);
2372                 const struct btf_kind_operations *ops;
2373                 u32 member_offset, bitfield_size;
2374                 u32 bytes_offset;
2375                 u8 bits8_offset;
2376
2377                 if (i)
2378                         seq_puts(m, seq);
2379
2380                 member_offset = btf_member_bit_offset(t, member);
2381                 bitfield_size = btf_member_bitfield_size(t, member);
2382                 bytes_offset = BITS_ROUNDDOWN_BYTES(member_offset);
2383                 bits8_offset = BITS_PER_BYTE_MASKED(member_offset);
2384                 if (bitfield_size) {
2385                         btf_bitfield_seq_show(data + bytes_offset, bits8_offset,
2386                                               bitfield_size, m);
2387                 } else {
2388                         ops = btf_type_ops(member_type);
2389                         ops->seq_show(btf, member_type, member->type,
2390                                       data + bytes_offset, bits8_offset, m);
2391                 }
2392         }
2393         seq_puts(m, "}");
2394 }
2395
2396 static struct btf_kind_operations struct_ops = {
2397         .check_meta = btf_struct_check_meta,
2398         .resolve = btf_struct_resolve,
2399         .check_member = btf_struct_check_member,
2400         .check_kflag_member = btf_generic_check_kflag_member,
2401         .log_details = btf_struct_log,
2402         .seq_show = btf_struct_seq_show,
2403 };
2404
2405 static int btf_enum_check_member(struct btf_verifier_env *env,
2406                                  const struct btf_type *struct_type,
2407                                  const struct btf_member *member,
2408                                  const struct btf_type *member_type)
2409 {
2410         u32 struct_bits_off = member->offset;
2411         u32 struct_size, bytes_offset;
2412
2413         if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
2414                 btf_verifier_log_member(env, struct_type, member,
2415                                         "Member is not byte aligned");
2416                 return -EINVAL;
2417         }
2418
2419         struct_size = struct_type->size;
2420         bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
2421         if (struct_size - bytes_offset < member_type->size) {
2422                 btf_verifier_log_member(env, struct_type, member,
2423                                         "Member exceeds struct_size");
2424                 return -EINVAL;
2425         }
2426
2427         return 0;
2428 }
2429
2430 static int btf_enum_check_kflag_member(struct btf_verifier_env *env,
2431                                        const struct btf_type *struct_type,
2432                                        const struct btf_member *member,
2433                                        const struct btf_type *member_type)
2434 {
2435         u32 struct_bits_off, nr_bits, bytes_end, struct_size;
2436         u32 int_bitsize = sizeof(int) * BITS_PER_BYTE;
2437
2438         struct_bits_off = BTF_MEMBER_BIT_OFFSET(member->offset);
2439         nr_bits = BTF_MEMBER_BITFIELD_SIZE(member->offset);
2440         if (!nr_bits) {
2441                 if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
2442                         btf_verifier_log_member(env, struct_type, member,
2443                                                 "Member is not byte aligned");
2444                         return -EINVAL;
2445                 }
2446
2447                 nr_bits = int_bitsize;
2448         } else if (nr_bits > int_bitsize) {
2449                 btf_verifier_log_member(env, struct_type, member,
2450                                         "Invalid member bitfield_size");
2451                 return -EINVAL;
2452         }
2453
2454         struct_size = struct_type->size;
2455         bytes_end = BITS_ROUNDUP_BYTES(struct_bits_off + nr_bits);
2456         if (struct_size < bytes_end) {
2457                 btf_verifier_log_member(env, struct_type, member,
2458                                         "Member exceeds struct_size");
2459                 return -EINVAL;
2460         }
2461
2462         return 0;
2463 }
2464
2465 static s32 btf_enum_check_meta(struct btf_verifier_env *env,
2466                                const struct btf_type *t,
2467                                u32 meta_left)
2468 {
2469         const struct btf_enum *enums = btf_type_enum(t);
2470         struct btf *btf = env->btf;
2471         u16 i, nr_enums;
2472         u32 meta_needed;
2473
2474         nr_enums = btf_type_vlen(t);
2475         meta_needed = nr_enums * sizeof(*enums);
2476
2477         if (meta_left < meta_needed) {
2478                 btf_verifier_log_basic(env, t,
2479                                        "meta_left:%u meta_needed:%u",
2480                                        meta_left, meta_needed);
2481                 return -EINVAL;
2482         }
2483
2484         if (btf_type_kflag(t)) {
2485                 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
2486                 return -EINVAL;
2487         }
2488
2489         if (t->size > 8 || !is_power_of_2(t->size)) {
2490                 btf_verifier_log_type(env, t, "Unexpected size");
2491                 return -EINVAL;
2492         }
2493
2494         /* enum type either no name or a valid one */
2495         if (t->name_off &&
2496             !btf_name_valid_identifier(env->btf, t->name_off)) {
2497                 btf_verifier_log_type(env, t, "Invalid name");
2498                 return -EINVAL;
2499         }
2500
2501         btf_verifier_log_type(env, t, NULL);
2502
2503         for (i = 0; i < nr_enums; i++) {
2504                 if (!btf_name_offset_valid(btf, enums[i].name_off)) {
2505                         btf_verifier_log(env, "\tInvalid name_offset:%u",
2506                                          enums[i].name_off);
2507                         return -EINVAL;
2508                 }
2509
2510                 /* enum member must have a valid name */
2511                 if (!enums[i].name_off ||
2512                     !btf_name_valid_identifier(btf, enums[i].name_off)) {
2513                         btf_verifier_log_type(env, t, "Invalid name");
2514                         return -EINVAL;
2515                 }
2516
2517                 if (env->log.level == BPF_LOG_KERNEL)
2518                         continue;
2519                 btf_verifier_log(env, "\t%s val=%d\n",
2520                                  __btf_name_by_offset(btf, enums[i].name_off),
2521                                  enums[i].val);
2522         }
2523
2524         return meta_needed;
2525 }
2526
2527 static void btf_enum_log(struct btf_verifier_env *env,
2528                          const struct btf_type *t)
2529 {
2530         btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t));
2531 }
2532
2533 static void btf_enum_seq_show(const struct btf *btf, const struct btf_type *t,
2534                               u32 type_id, void *data, u8 bits_offset,
2535                               struct seq_file *m)
2536 {
2537         const struct btf_enum *enums = btf_type_enum(t);
2538         u32 i, nr_enums = btf_type_vlen(t);
2539         int v = *(int *)data;
2540
2541         for (i = 0; i < nr_enums; i++) {
2542                 if (v == enums[i].val) {
2543                         seq_printf(m, "%s",
2544                                    __btf_name_by_offset(btf,
2545                                                         enums[i].name_off));
2546                         return;
2547                 }
2548         }
2549
2550         seq_printf(m, "%d", v);
2551 }
2552
2553 static struct btf_kind_operations enum_ops = {
2554         .check_meta = btf_enum_check_meta,
2555         .resolve = btf_df_resolve,
2556         .check_member = btf_enum_check_member,
2557         .check_kflag_member = btf_enum_check_kflag_member,
2558         .log_details = btf_enum_log,
2559         .seq_show = btf_enum_seq_show,
2560 };
2561
2562 static s32 btf_func_proto_check_meta(struct btf_verifier_env *env,
2563                                      const struct btf_type *t,
2564                                      u32 meta_left)
2565 {
2566         u32 meta_needed = btf_type_vlen(t) * sizeof(struct btf_param);
2567
2568         if (meta_left < meta_needed) {
2569                 btf_verifier_log_basic(env, t,
2570                                        "meta_left:%u meta_needed:%u",
2571                                        meta_left, meta_needed);
2572                 return -EINVAL;
2573         }
2574
2575         if (t->name_off) {
2576                 btf_verifier_log_type(env, t, "Invalid name");
2577                 return -EINVAL;
2578         }
2579
2580         if (btf_type_kflag(t)) {
2581                 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
2582                 return -EINVAL;
2583         }
2584
2585         btf_verifier_log_type(env, t, NULL);
2586
2587         return meta_needed;
2588 }
2589
2590 static void btf_func_proto_log(struct btf_verifier_env *env,
2591                                const struct btf_type *t)
2592 {
2593         const struct btf_param *args = (const struct btf_param *)(t + 1);
2594         u16 nr_args = btf_type_vlen(t), i;
2595
2596         btf_verifier_log(env, "return=%u args=(", t->type);
2597         if (!nr_args) {
2598                 btf_verifier_log(env, "void");
2599                 goto done;
2600         }
2601
2602         if (nr_args == 1 && !args[0].type) {
2603                 /* Only one vararg */
2604                 btf_verifier_log(env, "vararg");
2605                 goto done;
2606         }
2607
2608         btf_verifier_log(env, "%u %s", args[0].type,
2609                          __btf_name_by_offset(env->btf,
2610                                               args[0].name_off));
2611         for (i = 1; i < nr_args - 1; i++)
2612                 btf_verifier_log(env, ", %u %s", args[i].type,
2613                                  __btf_name_by_offset(env->btf,
2614                                                       args[i].name_off));
2615
2616         if (nr_args > 1) {
2617                 const struct btf_param *last_arg = &args[nr_args - 1];
2618
2619                 if (last_arg->type)
2620                         btf_verifier_log(env, ", %u %s", last_arg->type,
2621                                          __btf_name_by_offset(env->btf,
2622                                                               last_arg->name_off));
2623                 else
2624                         btf_verifier_log(env, ", vararg");
2625         }
2626
2627 done:
2628         btf_verifier_log(env, ")");
2629 }
2630
2631 static struct btf_kind_operations func_proto_ops = {
2632         .check_meta = btf_func_proto_check_meta,
2633         .resolve = btf_df_resolve,
2634         /*
2635          * BTF_KIND_FUNC_PROTO cannot be directly referred by
2636          * a struct's member.
2637          *
2638          * It should be a funciton pointer instead.
2639          * (i.e. struct's member -> BTF_KIND_PTR -> BTF_KIND_FUNC_PROTO)
2640          *
2641          * Hence, there is no btf_func_check_member().
2642          */
2643         .check_member = btf_df_check_member,
2644         .check_kflag_member = btf_df_check_kflag_member,
2645         .log_details = btf_func_proto_log,
2646         .seq_show = btf_df_seq_show,
2647 };
2648
2649 static s32 btf_func_check_meta(struct btf_verifier_env *env,
2650                                const struct btf_type *t,
2651                                u32 meta_left)
2652 {
2653         if (!t->name_off ||
2654             !btf_name_valid_identifier(env->btf, t->name_off)) {
2655                 btf_verifier_log_type(env, t, "Invalid name");
2656                 return -EINVAL;
2657         }
2658
2659         if (btf_type_vlen(t) > BTF_FUNC_GLOBAL) {
2660                 btf_verifier_log_type(env, t, "Invalid func linkage");
2661                 return -EINVAL;
2662         }
2663
2664         if (btf_type_kflag(t)) {
2665                 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
2666                 return -EINVAL;
2667         }
2668
2669         btf_verifier_log_type(env, t, NULL);
2670
2671         return 0;
2672 }
2673
2674 static struct btf_kind_operations func_ops = {
2675         .check_meta = btf_func_check_meta,
2676         .resolve = btf_df_resolve,
2677         .check_member = btf_df_check_member,
2678         .check_kflag_member = btf_df_check_kflag_member,
2679         .log_details = btf_ref_type_log,
2680         .seq_show = btf_df_seq_show,
2681 };
2682
2683 static s32 btf_var_check_meta(struct btf_verifier_env *env,
2684                               const struct btf_type *t,
2685                               u32 meta_left)
2686 {
2687         const struct btf_var *var;
2688         u32 meta_needed = sizeof(*var);
2689
2690         if (meta_left < meta_needed) {
2691                 btf_verifier_log_basic(env, t,
2692                                        "meta_left:%u meta_needed:%u",
2693                                        meta_left, meta_needed);
2694                 return -EINVAL;
2695         }
2696
2697         if (btf_type_vlen(t)) {
2698                 btf_verifier_log_type(env, t, "vlen != 0");
2699                 return -EINVAL;
2700         }
2701
2702         if (btf_type_kflag(t)) {
2703                 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
2704                 return -EINVAL;
2705         }
2706
2707         if (!t->name_off ||
2708             !__btf_name_valid(env->btf, t->name_off, true)) {
2709                 btf_verifier_log_type(env, t, "Invalid name");
2710                 return -EINVAL;
2711         }
2712
2713         /* A var cannot be in type void */
2714         if (!t->type || !BTF_TYPE_ID_VALID(t->type)) {
2715                 btf_verifier_log_type(env, t, "Invalid type_id");
2716                 return -EINVAL;
2717         }
2718
2719         var = btf_type_var(t);
2720         if (var->linkage != BTF_VAR_STATIC &&
2721             var->linkage != BTF_VAR_GLOBAL_ALLOCATED) {
2722                 btf_verifier_log_type(env, t, "Linkage not supported");
2723                 return -EINVAL;
2724         }
2725
2726         btf_verifier_log_type(env, t, NULL);
2727
2728         return meta_needed;
2729 }
2730
2731 static void btf_var_log(struct btf_verifier_env *env, const struct btf_type *t)
2732 {
2733         const struct btf_var *var = btf_type_var(t);
2734
2735         btf_verifier_log(env, "type_id=%u linkage=%u", t->type, var->linkage);
2736 }
2737
2738 static const struct btf_kind_operations var_ops = {
2739         .check_meta             = btf_var_check_meta,
2740         .resolve                = btf_var_resolve,
2741         .check_member           = btf_df_check_member,
2742         .check_kflag_member     = btf_df_check_kflag_member,
2743         .log_details            = btf_var_log,
2744         .seq_show               = btf_var_seq_show,
2745 };
2746
2747 static s32 btf_datasec_check_meta(struct btf_verifier_env *env,
2748                                   const struct btf_type *t,
2749                                   u32 meta_left)
2750 {
2751         const struct btf_var_secinfo *vsi;
2752         u64 last_vsi_end_off = 0, sum = 0;
2753         u32 i, meta_needed;
2754
2755         meta_needed = btf_type_vlen(t) * sizeof(*vsi);
2756         if (meta_left < meta_needed) {
2757                 btf_verifier_log_basic(env, t,
2758                                        "meta_left:%u meta_needed:%u",
2759                                        meta_left, meta_needed);
2760                 return -EINVAL;
2761         }
2762
2763         if (!btf_type_vlen(t)) {
2764                 btf_verifier_log_type(env, t, "vlen == 0");
2765                 return -EINVAL;
2766         }
2767
2768         if (!t->size) {
2769                 btf_verifier_log_type(env, t, "size == 0");
2770                 return -EINVAL;
2771         }
2772
2773         if (btf_type_kflag(t)) {
2774                 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
2775                 return -EINVAL;
2776         }
2777
2778         if (!t->name_off ||
2779             !btf_name_valid_section(env->btf, t->name_off)) {
2780                 btf_verifier_log_type(env, t, "Invalid name");
2781                 return -EINVAL;
2782         }
2783
2784         btf_verifier_log_type(env, t, NULL);
2785
2786         for_each_vsi(i, t, vsi) {
2787                 /* A var cannot be in type void */
2788                 if (!vsi->type || !BTF_TYPE_ID_VALID(vsi->type)) {
2789                         btf_verifier_log_vsi(env, t, vsi,
2790                                              "Invalid type_id");
2791                         return -EINVAL;
2792                 }
2793
2794                 if (vsi->offset < last_vsi_end_off || vsi->offset >= t->size) {
2795                         btf_verifier_log_vsi(env, t, vsi,
2796                                              "Invalid offset");
2797                         return -EINVAL;
2798                 }
2799
2800                 if (!vsi->size || vsi->size > t->size) {
2801                         btf_verifier_log_vsi(env, t, vsi,
2802                                              "Invalid size");
2803                         return -EINVAL;
2804                 }
2805
2806                 last_vsi_end_off = vsi->offset + vsi->size;
2807                 if (last_vsi_end_off > t->size) {
2808                         btf_verifier_log_vsi(env, t, vsi,
2809                                              "Invalid offset+size");
2810                         return -EINVAL;
2811                 }
2812
2813                 btf_verifier_log_vsi(env, t, vsi, NULL);
2814                 sum += vsi->size;
2815         }
2816
2817         if (t->size < sum) {
2818                 btf_verifier_log_type(env, t, "Invalid btf_info size");
2819                 return -EINVAL;
2820         }
2821
2822         return meta_needed;
2823 }
2824
2825 static int btf_datasec_resolve(struct btf_verifier_env *env,
2826                                const struct resolve_vertex *v)
2827 {
2828         const struct btf_var_secinfo *vsi;
2829         struct btf *btf = env->btf;
2830         u16 i;
2831
2832         for_each_vsi_from(i, v->next_member, v->t, vsi) {
2833                 u32 var_type_id = vsi->type, type_id, type_size = 0;
2834                 const struct btf_type *var_type = btf_type_by_id(env->btf,
2835                                                                  var_type_id);
2836                 if (!var_type || !btf_type_is_var(var_type)) {
2837                         btf_verifier_log_vsi(env, v->t, vsi,
2838                                              "Not a VAR kind member");
2839                         return -EINVAL;
2840                 }
2841
2842                 if (!env_type_is_resolve_sink(env, var_type) &&
2843                     !env_type_is_resolved(env, var_type_id)) {
2844                         env_stack_set_next_member(env, i + 1);
2845                         return env_stack_push(env, var_type, var_type_id);
2846                 }
2847
2848                 type_id = var_type->type;
2849                 if (!btf_type_id_size(btf, &type_id, &type_size)) {
2850                         btf_verifier_log_vsi(env, v->t, vsi, "Invalid type");
2851                         return -EINVAL;
2852                 }
2853
2854                 if (vsi->size < type_size) {
2855                         btf_verifier_log_vsi(env, v->t, vsi, "Invalid size");
2856                         return -EINVAL;
2857                 }
2858         }
2859
2860         env_stack_pop_resolved(env, 0, 0);
2861         return 0;
2862 }
2863
2864 static void btf_datasec_log(struct btf_verifier_env *env,
2865                             const struct btf_type *t)
2866 {
2867         btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t));
2868 }
2869
2870 static void btf_datasec_seq_show(const struct btf *btf,
2871                                  const struct btf_type *t, u32 type_id,
2872                                  void *data, u8 bits_offset,
2873                                  struct seq_file *m)
2874 {
2875         const struct btf_var_secinfo *vsi;
2876         const struct btf_type *var;
2877         u32 i;
2878
2879         seq_printf(m, "section (\"%s\") = {", __btf_name_by_offset(btf, t->name_off));
2880         for_each_vsi(i, t, vsi) {
2881                 var = btf_type_by_id(btf, vsi->type);
2882                 if (i)
2883                         seq_puts(m, ",");
2884                 btf_type_ops(var)->seq_show(btf, var, vsi->type,
2885                                             data + vsi->offset, bits_offset, m);
2886         }
2887         seq_puts(m, "}");
2888 }
2889
2890 static const struct btf_kind_operations datasec_ops = {
2891         .check_meta             = btf_datasec_check_meta,
2892         .resolve                = btf_datasec_resolve,
2893         .check_member           = btf_df_check_member,
2894         .check_kflag_member     = btf_df_check_kflag_member,
2895         .log_details            = btf_datasec_log,
2896         .seq_show               = btf_datasec_seq_show,
2897 };
2898
2899 static int btf_func_proto_check(struct btf_verifier_env *env,
2900                                 const struct btf_type *t)
2901 {
2902         const struct btf_type *ret_type;
2903         const struct btf_param *args;
2904         const struct btf *btf;
2905         u16 nr_args, i;
2906         int err;
2907
2908         btf = env->btf;
2909         args = (const struct btf_param *)(t + 1);
2910         nr_args = btf_type_vlen(t);
2911
2912         /* Check func return type which could be "void" (t->type == 0) */
2913         if (t->type) {
2914                 u32 ret_type_id = t->type;
2915
2916                 ret_type = btf_type_by_id(btf, ret_type_id);
2917                 if (!ret_type) {
2918                         btf_verifier_log_type(env, t, "Invalid return type");
2919                         return -EINVAL;
2920                 }
2921
2922                 if (btf_type_needs_resolve(ret_type) &&
2923                     !env_type_is_resolved(env, ret_type_id)) {
2924                         err = btf_resolve(env, ret_type, ret_type_id);
2925                         if (err)
2926                                 return err;
2927                 }
2928
2929                 /* Ensure the return type is a type that has a size */
2930                 if (!btf_type_id_size(btf, &ret_type_id, NULL)) {
2931                         btf_verifier_log_type(env, t, "Invalid return type");
2932                         return -EINVAL;
2933                 }
2934         }
2935
2936         if (!nr_args)
2937                 return 0;
2938
2939         /* Last func arg type_id could be 0 if it is a vararg */
2940         if (!args[nr_args - 1].type) {
2941                 if (args[nr_args - 1].name_off) {
2942                         btf_verifier_log_type(env, t, "Invalid arg#%u",
2943                                               nr_args);
2944                         return -EINVAL;
2945                 }
2946                 nr_args--;
2947         }
2948
2949         err = 0;
2950         for (i = 0; i < nr_args; i++) {
2951                 const struct btf_type *arg_type;
2952                 u32 arg_type_id;
2953
2954                 arg_type_id = args[i].type;
2955                 arg_type = btf_type_by_id(btf, arg_type_id);
2956                 if (!arg_type) {
2957                         btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1);
2958                         err = -EINVAL;
2959                         break;
2960                 }
2961
2962                 if (args[i].name_off &&
2963                     (!btf_name_offset_valid(btf, args[i].name_off) ||
2964                      !btf_name_valid_identifier(btf, args[i].name_off))) {
2965                         btf_verifier_log_type(env, t,
2966                                               "Invalid arg#%u", i + 1);
2967                         err = -EINVAL;
2968                         break;
2969                 }
2970
2971                 if (btf_type_needs_resolve(arg_type) &&
2972                     !env_type_is_resolved(env, arg_type_id)) {
2973                         err = btf_resolve(env, arg_type, arg_type_id);
2974                         if (err)
2975                                 break;
2976                 }
2977
2978                 if (!btf_type_id_size(btf, &arg_type_id, NULL)) {
2979                         btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1);
2980                         err = -EINVAL;
2981                         break;
2982                 }
2983         }
2984
2985         return err;
2986 }
2987
2988 static int btf_func_check(struct btf_verifier_env *env,
2989                           const struct btf_type *t)
2990 {
2991         const struct btf_type *proto_type;
2992         const struct btf_param *args;
2993         const struct btf *btf;
2994         u16 nr_args, i;
2995
2996         btf = env->btf;
2997         proto_type = btf_type_by_id(btf, t->type);
2998
2999         if (!proto_type || !btf_type_is_func_proto(proto_type)) {
3000                 btf_verifier_log_type(env, t, "Invalid type_id");
3001                 return -EINVAL;
3002         }
3003
3004         args = (const struct btf_param *)(proto_type + 1);
3005         nr_args = btf_type_vlen(proto_type);
3006         for (i = 0; i < nr_args; i++) {
3007                 if (!args[i].name_off && args[i].type) {
3008                         btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1);
3009                         return -EINVAL;
3010                 }
3011         }
3012
3013         return 0;
3014 }
3015
3016 static const struct btf_kind_operations * const kind_ops[NR_BTF_KINDS] = {
3017         [BTF_KIND_INT] = &int_ops,
3018         [BTF_KIND_PTR] = &ptr_ops,
3019         [BTF_KIND_ARRAY] = &array_ops,
3020         [BTF_KIND_STRUCT] = &struct_ops,
3021         [BTF_KIND_UNION] = &struct_ops,
3022         [BTF_KIND_ENUM] = &enum_ops,
3023         [BTF_KIND_FWD] = &fwd_ops,
3024         [BTF_KIND_TYPEDEF] = &modifier_ops,
3025         [BTF_KIND_VOLATILE] = &modifier_ops,
3026         [BTF_KIND_CONST] = &modifier_ops,
3027         [BTF_KIND_RESTRICT] = &modifier_ops,
3028         [BTF_KIND_FUNC] = &func_ops,
3029         [BTF_KIND_FUNC_PROTO] = &func_proto_ops,
3030         [BTF_KIND_VAR] = &var_ops,
3031         [BTF_KIND_DATASEC] = &datasec_ops,
3032 };
3033
3034 static s32 btf_check_meta(struct btf_verifier_env *env,
3035                           const struct btf_type *t,
3036                           u32 meta_left)
3037 {
3038         u32 saved_meta_left = meta_left;
3039         s32 var_meta_size;
3040
3041         if (meta_left < sizeof(*t)) {
3042                 btf_verifier_log(env, "[%u] meta_left:%u meta_needed:%zu",
3043                                  env->log_type_id, meta_left, sizeof(*t));
3044                 return -EINVAL;
3045         }
3046         meta_left -= sizeof(*t);
3047
3048         if (t->info & ~BTF_INFO_MASK) {
3049                 btf_verifier_log(env, "[%u] Invalid btf_info:%x",
3050                                  env->log_type_id, t->info);
3051                 return -EINVAL;
3052         }
3053
3054         if (BTF_INFO_KIND(t->info) > BTF_KIND_MAX ||
3055             BTF_INFO_KIND(t->info) == BTF_KIND_UNKN) {
3056                 btf_verifier_log(env, "[%u] Invalid kind:%u",
3057                                  env->log_type_id, BTF_INFO_KIND(t->info));
3058                 return -EINVAL;
3059         }
3060
3061         if (!btf_name_offset_valid(env->btf, t->name_off)) {
3062                 btf_verifier_log(env, "[%u] Invalid name_offset:%u",
3063                                  env->log_type_id, t->name_off);
3064                 return -EINVAL;
3065         }
3066
3067         var_meta_size = btf_type_ops(t)->check_meta(env, t, meta_left);
3068         if (var_meta_size < 0)
3069                 return var_meta_size;
3070
3071         meta_left -= var_meta_size;
3072
3073         return saved_meta_left - meta_left;
3074 }
3075
3076 static int btf_check_all_metas(struct btf_verifier_env *env)
3077 {
3078         struct btf *btf = env->btf;
3079         struct btf_header *hdr;
3080         void *cur, *end;
3081
3082         hdr = &btf->hdr;
3083         cur = btf->nohdr_data + hdr->type_off;
3084         end = cur + hdr->type_len;
3085
3086         env->log_type_id = 1;
3087         while (cur < end) {
3088                 struct btf_type *t = cur;
3089                 s32 meta_size;
3090
3091                 meta_size = btf_check_meta(env, t, end - cur);
3092                 if (meta_size < 0)
3093                         return meta_size;
3094
3095                 btf_add_type(env, t);
3096                 cur += meta_size;
3097                 env->log_type_id++;
3098         }
3099
3100         return 0;
3101 }
3102
3103 static bool btf_resolve_valid(struct btf_verifier_env *env,
3104                               const struct btf_type *t,
3105                               u32 type_id)
3106 {
3107         struct btf *btf = env->btf;
3108
3109         if (!env_type_is_resolved(env, type_id))
3110                 return false;
3111
3112         if (btf_type_is_struct(t) || btf_type_is_datasec(t))
3113                 return !btf->resolved_ids[type_id] &&
3114                        !btf->resolved_sizes[type_id];
3115
3116         if (btf_type_is_modifier(t) || btf_type_is_ptr(t) ||
3117             btf_type_is_var(t)) {
3118                 t = btf_type_id_resolve(btf, &type_id);
3119                 return t &&
3120                        !btf_type_is_modifier(t) &&
3121                        !btf_type_is_var(t) &&
3122                        !btf_type_is_datasec(t);
3123         }
3124
3125         if (btf_type_is_array(t)) {
3126                 const struct btf_array *array = btf_type_array(t);
3127                 const struct btf_type *elem_type;
3128                 u32 elem_type_id = array->type;
3129                 u32 elem_size;
3130
3131                 elem_type = btf_type_id_size(btf, &elem_type_id, &elem_size);
3132                 return elem_type && !btf_type_is_modifier(elem_type) &&
3133                         (array->nelems * elem_size ==
3134                          btf->resolved_sizes[type_id]);
3135         }
3136
3137         return false;
3138 }
3139
3140 static int btf_resolve(struct btf_verifier_env *env,
3141                        const struct btf_type *t, u32 type_id)
3142 {
3143         u32 save_log_type_id = env->log_type_id;
3144         const struct resolve_vertex *v;
3145         int err = 0;
3146
3147         env->resolve_mode = RESOLVE_TBD;
3148         env_stack_push(env, t, type_id);
3149         while (!err && (v = env_stack_peak(env))) {
3150                 env->log_type_id = v->type_id;
3151                 err = btf_type_ops(v->t)->resolve(env, v);
3152         }
3153
3154         env->log_type_id = type_id;
3155         if (err == -E2BIG) {
3156                 btf_verifier_log_type(env, t,
3157                                       "Exceeded max resolving depth:%u",
3158                                       MAX_RESOLVE_DEPTH);
3159         } else if (err == -EEXIST) {
3160                 btf_verifier_log_type(env, t, "Loop detected");
3161         }
3162
3163         /* Final sanity check */
3164         if (!err && !btf_resolve_valid(env, t, type_id)) {
3165                 btf_verifier_log_type(env, t, "Invalid resolve state");
3166                 err = -EINVAL;
3167         }
3168
3169         env->log_type_id = save_log_type_id;
3170         return err;
3171 }
3172
3173 static int btf_check_all_types(struct btf_verifier_env *env)
3174 {
3175         struct btf *btf = env->btf;
3176         u32 type_id;
3177         int err;
3178
3179         err = env_resolve_init(env);
3180         if (err)
3181                 return err;
3182
3183         env->phase++;
3184         for (type_id = 1; type_id <= btf->nr_types; type_id++) {
3185                 const struct btf_type *t = btf_type_by_id(btf, type_id);
3186
3187                 env->log_type_id = type_id;
3188                 if (btf_type_needs_resolve(t) &&
3189                     !env_type_is_resolved(env, type_id)) {
3190                         err = btf_resolve(env, t, type_id);
3191                         if (err)
3192                                 return err;
3193                 }
3194
3195                 if (btf_type_is_func_proto(t)) {
3196                         err = btf_func_proto_check(env, t);
3197                         if (err)
3198                                 return err;
3199                 }
3200
3201                 if (btf_type_is_func(t)) {
3202                         err = btf_func_check(env, t);
3203                         if (err)
3204                                 return err;
3205                 }
3206         }
3207
3208         return 0;
3209 }
3210
3211 static int btf_parse_type_sec(struct btf_verifier_env *env)
3212 {
3213         const struct btf_header *hdr = &env->btf->hdr;
3214         int err;
3215
3216         /* Type section must align to 4 bytes */
3217         if (hdr->type_off & (sizeof(u32) - 1)) {
3218                 btf_verifier_log(env, "Unaligned type_off");
3219                 return -EINVAL;
3220         }
3221
3222         if (!hdr->type_len) {
3223                 btf_verifier_log(env, "No type found");
3224                 return -EINVAL;
3225         }
3226
3227         err = btf_check_all_metas(env);
3228         if (err)
3229                 return err;
3230
3231         return btf_check_all_types(env);
3232 }
3233
3234 static int btf_parse_str_sec(struct btf_verifier_env *env)
3235 {
3236         const struct btf_header *hdr;
3237         struct btf *btf = env->btf;
3238         const char *start, *end;
3239
3240         hdr = &btf->hdr;
3241         start = btf->nohdr_data + hdr->str_off;
3242         end = start + hdr->str_len;
3243
3244         if (end != btf->data + btf->data_size) {
3245                 btf_verifier_log(env, "String section is not at the end");
3246                 return -EINVAL;
3247         }
3248
3249         if (!hdr->str_len || hdr->str_len - 1 > BTF_MAX_NAME_OFFSET ||
3250             start[0] || end[-1]) {
3251                 btf_verifier_log(env, "Invalid string section");
3252                 return -EINVAL;
3253         }
3254
3255         btf->strings = start;
3256
3257         return 0;
3258 }
3259
3260 static const size_t btf_sec_info_offset[] = {
3261         offsetof(struct btf_header, type_off),
3262         offsetof(struct btf_header, str_off),
3263 };
3264
3265 static int btf_sec_info_cmp(const void *a, const void *b)
3266 {
3267         const struct btf_sec_info *x = a;
3268         const struct btf_sec_info *y = b;
3269
3270         return (int)(x->off - y->off) ? : (int)(x->len - y->len);
3271 }
3272
3273 static int btf_check_sec_info(struct btf_verifier_env *env,
3274                               u32 btf_data_size)
3275 {
3276         struct btf_sec_info secs[ARRAY_SIZE(btf_sec_info_offset)];
3277         u32 total, expected_total, i;
3278         const struct btf_header *hdr;
3279         const struct btf *btf;
3280
3281         btf = env->btf;
3282         hdr = &btf->hdr;
3283
3284         /* Populate the secs from hdr */
3285         for (i = 0; i < ARRAY_SIZE(btf_sec_info_offset); i++)
3286                 secs[i] = *(struct btf_sec_info *)((void *)hdr +
3287                                                    btf_sec_info_offset[i]);
3288
3289         sort(secs, ARRAY_SIZE(btf_sec_info_offset),
3290              sizeof(struct btf_sec_info), btf_sec_info_cmp, NULL);
3291
3292         /* Check for gaps and overlap among sections */
3293         total = 0;
3294         expected_total = btf_data_size - hdr->hdr_len;
3295         for (i = 0; i < ARRAY_SIZE(btf_sec_info_offset); i++) {
3296                 if (expected_total < secs[i].off) {
3297                         btf_verifier_log(env, "Invalid section offset");
3298                         return -EINVAL;
3299                 }
3300                 if (total < secs[i].off) {
3301                         /* gap */
3302                         btf_verifier_log(env, "Unsupported section found");
3303                         return -EINVAL;
3304                 }
3305                 if (total > secs[i].off) {
3306                         btf_verifier_log(env, "Section overlap found");
3307                         return -EINVAL;
3308                 }
3309                 if (expected_total - total < secs[i].len) {
3310                         btf_verifier_log(env,
3311                                          "Total section length too long");
3312                         return -EINVAL;
3313                 }
3314                 total += secs[i].len;
3315         }
3316
3317         /* There is data other than hdr and known sections */
3318         if (expected_total != total) {
3319                 btf_verifier_log(env, "Unsupported section found");
3320                 return -EINVAL;
3321         }
3322
3323         return 0;
3324 }
3325
3326 static int btf_parse_hdr(struct btf_verifier_env *env)
3327 {
3328         u32 hdr_len, hdr_copy, btf_data_size;
3329         const struct btf_header *hdr;
3330         struct btf *btf;
3331         int err;
3332
3333         btf = env->btf;
3334         btf_data_size = btf->data_size;
3335
3336         if (btf_data_size <
3337             offsetof(struct btf_header, hdr_len) + sizeof(hdr->hdr_len)) {
3338                 btf_verifier_log(env, "hdr_len not found");
3339                 return -EINVAL;
3340         }
3341
3342         hdr = btf->data;
3343         hdr_len = hdr->hdr_len;
3344         if (btf_data_size < hdr_len) {
3345                 btf_verifier_log(env, "btf_header not found");
3346                 return -EINVAL;
3347         }
3348
3349         /* Ensure the unsupported header fields are zero */
3350         if (hdr_len > sizeof(btf->hdr)) {
3351                 u8 *expected_zero = btf->data + sizeof(btf->hdr);
3352                 u8 *end = btf->data + hdr_len;
3353
3354                 for (; expected_zero < end; expected_zero++) {
3355                         if (*expected_zero) {
3356                                 btf_verifier_log(env, "Unsupported btf_header");
3357                                 return -E2BIG;
3358                         }
3359                 }
3360         }
3361
3362         hdr_copy = min_t(u32, hdr_len, sizeof(btf->hdr));
3363         memcpy(&btf->hdr, btf->data, hdr_copy);
3364
3365         hdr = &btf->hdr;
3366
3367         btf_verifier_log_hdr(env, btf_data_size);
3368
3369         if (hdr->magic != BTF_MAGIC) {
3370                 btf_verifier_log(env, "Invalid magic");
3371                 return -EINVAL;
3372         }
3373
3374         if (hdr->version != BTF_VERSION) {
3375                 btf_verifier_log(env, "Unsupported version");
3376                 return -ENOTSUPP;
3377         }
3378
3379         if (hdr->flags) {
3380                 btf_verifier_log(env, "Unsupported flags");
3381                 return -ENOTSUPP;
3382         }
3383
3384         if (btf_data_size == hdr->hdr_len) {
3385                 btf_verifier_log(env, "No data");
3386                 return -EINVAL;
3387         }
3388
3389         err = btf_check_sec_info(env, btf_data_size);
3390         if (err)
3391                 return err;
3392
3393         return 0;
3394 }
3395
3396 static struct btf *btf_parse(void __user *btf_data, u32 btf_data_size,
3397                              u32 log_level, char __user *log_ubuf, u32 log_size)
3398 {
3399         struct btf_verifier_env *env = NULL;
3400         struct bpf_verifier_log *log;
3401         struct btf *btf = NULL;
3402         u8 *data;
3403         int err;
3404
3405         if (btf_data_size > BTF_MAX_SIZE)
3406                 return ERR_PTR(-E2BIG);
3407
3408         env = kzalloc(sizeof(*env), GFP_KERNEL | __GFP_NOWARN);
3409         if (!env)
3410                 return ERR_PTR(-ENOMEM);
3411
3412         log = &env->log;
3413         if (log_level || log_ubuf || log_size) {
3414                 /* user requested verbose verifier output
3415                  * and supplied buffer to store the verification trace
3416                  */
3417                 log->level = log_level;
3418                 log->ubuf = log_ubuf;
3419                 log->len_total = log_size;
3420
3421                 /* log attributes have to be sane */
3422                 if (log->len_total < 128 || log->len_total > UINT_MAX >> 8 ||
3423                     !log->level || !log->ubuf) {
3424                         err = -EINVAL;
3425                         goto errout;
3426                 }
3427         }
3428
3429         btf = kzalloc(sizeof(*btf), GFP_KERNEL | __GFP_NOWARN);
3430         if (!btf) {
3431                 err = -ENOMEM;
3432                 goto errout;
3433         }
3434         env->btf = btf;
3435
3436         data = kvmalloc(btf_data_size, GFP_KERNEL | __GFP_NOWARN);
3437         if (!data) {
3438                 err = -ENOMEM;
3439                 goto errout;
3440         }
3441
3442         btf->data = data;
3443         btf->data_size = btf_data_size;
3444
3445         if (copy_from_user(data, btf_data, btf_data_size)) {
3446                 err = -EFAULT;
3447                 goto errout;
3448         }
3449
3450         err = btf_parse_hdr(env);
3451         if (err)
3452                 goto errout;
3453
3454         btf->nohdr_data = btf->data + btf->hdr.hdr_len;
3455
3456         err = btf_parse_str_sec(env);
3457         if (err)
3458                 goto errout;
3459
3460         err = btf_parse_type_sec(env);
3461         if (err)
3462                 goto errout;
3463
3464         if (log->level && bpf_verifier_log_full(log)) {
3465                 err = -ENOSPC;
3466                 goto errout;
3467         }
3468
3469         btf_verifier_env_free(env);
3470         refcount_set(&btf->refcnt, 1);
3471         return btf;
3472
3473 errout:
3474         btf_verifier_env_free(env);
3475         if (btf)
3476                 btf_free(btf);
3477         return ERR_PTR(err);
3478 }
3479
3480 extern char __weak __start_BTF[];
3481 extern char __weak __stop_BTF[];
3482 extern struct btf *btf_vmlinux;
3483
3484 #define BPF_MAP_TYPE(_id, _ops)
3485 static union {
3486         struct bpf_ctx_convert {
3487 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
3488         prog_ctx_type _id##_prog; \
3489         kern_ctx_type _id##_kern;
3490 #include <linux/bpf_types.h>
3491 #undef BPF_PROG_TYPE
3492         } *__t;
3493         /* 't' is written once under lock. Read many times. */
3494         const struct btf_type *t;
3495 } bpf_ctx_convert;
3496 enum {
3497 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
3498         __ctx_convert##_id,
3499 #include <linux/bpf_types.h>
3500 #undef BPF_PROG_TYPE
3501         __ctx_convert_unused, /* to avoid empty enum in extreme .config */
3502 };
3503 static u8 bpf_ctx_convert_map[] = {
3504 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
3505         [_id] = __ctx_convert##_id,
3506 #include <linux/bpf_types.h>
3507 #undef BPF_PROG_TYPE
3508         0, /* avoid empty array */
3509 };
3510 #undef BPF_MAP_TYPE
3511
3512 static const struct btf_member *
3513 btf_get_prog_ctx_type(struct bpf_verifier_log *log, struct btf *btf,
3514                       const struct btf_type *t, enum bpf_prog_type prog_type,
3515                       int arg)
3516 {
3517         const struct btf_type *conv_struct;
3518         const struct btf_type *ctx_struct;
3519         const struct btf_member *ctx_type;
3520         const char *tname, *ctx_tname;
3521
3522         conv_struct = bpf_ctx_convert.t;
3523         if (!conv_struct) {
3524                 bpf_log(log, "btf_vmlinux is malformed\n");
3525                 return NULL;
3526         }
3527         t = btf_type_by_id(btf, t->type);
3528         while (btf_type_is_modifier(t))
3529                 t = btf_type_by_id(btf, t->type);
3530         if (!btf_type_is_struct(t)) {
3531                 /* Only pointer to struct is supported for now.
3532                  * That means that BPF_PROG_TYPE_TRACEPOINT with BTF
3533                  * is not supported yet.
3534                  * BPF_PROG_TYPE_RAW_TRACEPOINT is fine.
3535                  */
3536                 if (log->level & BPF_LOG_LEVEL)
3537                         bpf_log(log, "arg#%d type is not a struct\n", arg);
3538                 return NULL;
3539         }
3540         tname = btf_name_by_offset(btf, t->name_off);
3541         if (!tname) {
3542                 bpf_log(log, "arg#%d struct doesn't have a name\n", arg);
3543                 return NULL;
3544         }
3545         /* prog_type is valid bpf program type. No need for bounds check. */
3546         ctx_type = btf_type_member(conv_struct) + bpf_ctx_convert_map[prog_type] * 2;
3547         /* ctx_struct is a pointer to prog_ctx_type in vmlinux.
3548          * Like 'struct __sk_buff'
3549          */
3550         ctx_struct = btf_type_by_id(btf_vmlinux, ctx_type->type);
3551         if (!ctx_struct)
3552                 /* should not happen */
3553                 return NULL;
3554         ctx_tname = btf_name_by_offset(btf_vmlinux, ctx_struct->name_off);
3555         if (!ctx_tname) {
3556                 /* should not happen */
3557                 bpf_log(log, "Please fix kernel include/linux/bpf_types.h\n");
3558                 return NULL;
3559         }
3560         /* only compare that prog's ctx type name is the same as
3561          * kernel expects. No need to compare field by field.
3562          * It's ok for bpf prog to do:
3563          * struct __sk_buff {};
3564          * int socket_filter_bpf_prog(struct __sk_buff *skb)
3565          * { // no fields of skb are ever used }
3566          */
3567         if (strcmp(ctx_tname, tname))
3568                 return NULL;
3569         return ctx_type;
3570 }
3571
3572 static int btf_translate_to_vmlinux(struct bpf_verifier_log *log,
3573                                      struct btf *btf,
3574                                      const struct btf_type *t,
3575                                      enum bpf_prog_type prog_type,
3576                                      int arg)
3577 {
3578         const struct btf_member *prog_ctx_type, *kern_ctx_type;
3579
3580         prog_ctx_type = btf_get_prog_ctx_type(log, btf, t, prog_type, arg);
3581         if (!prog_ctx_type)
3582                 return -ENOENT;
3583         kern_ctx_type = prog_ctx_type + 1;
3584         return kern_ctx_type->type;
3585 }
3586
3587 struct btf *btf_parse_vmlinux(void)
3588 {
3589         struct btf_verifier_env *env = NULL;
3590         struct bpf_verifier_log *log;
3591         struct btf *btf = NULL;
3592         int err, i;
3593
3594         env = kzalloc(sizeof(*env), GFP_KERNEL | __GFP_NOWARN);
3595         if (!env)
3596                 return ERR_PTR(-ENOMEM);
3597
3598         log = &env->log;
3599         log->level = BPF_LOG_KERNEL;
3600
3601         btf = kzalloc(sizeof(*btf), GFP_KERNEL | __GFP_NOWARN);
3602         if (!btf) {
3603                 err = -ENOMEM;
3604                 goto errout;
3605         }
3606         env->btf = btf;
3607
3608         btf->data = __start_BTF;
3609         btf->data_size = __stop_BTF - __start_BTF;
3610
3611         err = btf_parse_hdr(env);
3612         if (err)
3613                 goto errout;
3614
3615         btf->nohdr_data = btf->data + btf->hdr.hdr_len;
3616
3617         err = btf_parse_str_sec(env);
3618         if (err)
3619                 goto errout;
3620
3621         err = btf_check_all_metas(env);
3622         if (err)
3623                 goto errout;
3624
3625         /* find struct bpf_ctx_convert for type checking later */
3626         for (i = 1; i <= btf->nr_types; i++) {
3627                 const struct btf_type *t;
3628                 const char *tname;
3629
3630                 t = btf_type_by_id(btf, i);
3631                 if (!__btf_type_is_struct(t))
3632                         continue;
3633                 tname = __btf_name_by_offset(btf, t->name_off);
3634                 if (!strcmp(tname, "bpf_ctx_convert")) {
3635                         /* btf_parse_vmlinux() runs under bpf_verifier_lock */
3636                         bpf_ctx_convert.t = t;
3637                         break;
3638                 }
3639         }
3640         if (i > btf->nr_types) {
3641                 err = -ENOENT;
3642                 goto errout;
3643         }
3644
3645         bpf_struct_ops_init(btf, log);
3646
3647         btf_verifier_env_free(env);
3648         refcount_set(&btf->refcnt, 1);
3649         return btf;
3650
3651 errout:
3652         btf_verifier_env_free(env);
3653         if (btf) {
3654                 kvfree(btf->types);
3655                 kfree(btf);
3656         }
3657         return ERR_PTR(err);
3658 }
3659
3660 struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog)
3661 {
3662         struct bpf_prog *tgt_prog = prog->aux->linked_prog;
3663
3664         if (tgt_prog) {
3665                 return tgt_prog->aux->btf;
3666         } else {
3667                 return btf_vmlinux;
3668         }
3669 }
3670
3671 static bool is_string_ptr(struct btf *btf, const struct btf_type *t)
3672 {
3673         /* t comes in already as a pointer */
3674         t = btf_type_by_id(btf, t->type);
3675
3676         /* allow const */
3677         if (BTF_INFO_KIND(t->info) == BTF_KIND_CONST)
3678                 t = btf_type_by_id(btf, t->type);
3679
3680         /* char, signed char, unsigned char */
3681         return btf_type_is_int(t) && t->size == 1;
3682 }
3683
3684 bool btf_ctx_access(int off, int size, enum bpf_access_type type,
3685                     const struct bpf_prog *prog,
3686                     struct bpf_insn_access_aux *info)
3687 {
3688         const struct btf_type *t = prog->aux->attach_func_proto;
3689         struct bpf_prog *tgt_prog = prog->aux->linked_prog;
3690         struct btf *btf = bpf_prog_get_target_btf(prog);
3691         const char *tname = prog->aux->attach_func_name;
3692         struct bpf_verifier_log *log = info->log;
3693         const struct btf_param *args;
3694         u32 nr_args, arg;
3695         int ret;
3696
3697         if (off % 8) {
3698                 bpf_log(log, "func '%s' offset %d is not multiple of 8\n",
3699                         tname, off);
3700                 return false;
3701         }
3702         arg = off / 8;
3703         args = (const struct btf_param *)(t + 1);
3704         /* if (t == NULL) Fall back to default BPF prog with 5 u64 arguments */
3705         nr_args = t ? btf_type_vlen(t) : 5;
3706         if (prog->aux->attach_btf_trace) {
3707                 /* skip first 'void *__data' argument in btf_trace_##name typedef */
3708                 args++;
3709                 nr_args--;
3710         }
3711
3712         if (arg > nr_args) {
3713                 bpf_log(log, "func '%s' doesn't have %d-th argument\n",
3714                         tname, arg + 1);
3715                 return false;
3716         }
3717
3718         if (arg == nr_args) {
3719                 switch (prog->expected_attach_type) {
3720                 case BPF_LSM_MAC:
3721                 case BPF_TRACE_FEXIT:
3722                         /* When LSM programs are attached to void LSM hooks
3723                          * they use FEXIT trampolines and when attached to
3724                          * int LSM hooks, they use MODIFY_RETURN trampolines.
3725                          *
3726                          * While the LSM programs are BPF_MODIFY_RETURN-like
3727                          * the check:
3728                          *
3729                          *      if (ret_type != 'int')
3730                          *              return -EINVAL;
3731                          *
3732                          * is _not_ done here. This is still safe as LSM hooks
3733                          * have only void and int return types.
3734                          */
3735                         if (!t)
3736                                 return true;
3737                         t = btf_type_by_id(btf, t->type);
3738                         break;
3739                 case BPF_MODIFY_RETURN:
3740                         /* For now the BPF_MODIFY_RETURN can only be attached to
3741                          * functions that return an int.
3742                          */
3743                         if (!t)
3744                                 return false;
3745
3746                         t = btf_type_skip_modifiers(btf, t->type, NULL);
3747                         if (!btf_type_is_int(t)) {
3748                                 bpf_log(log,
3749                                         "ret type %s not allowed for fmod_ret\n",
3750                                         btf_kind_str[BTF_INFO_KIND(t->info)]);
3751                                 return false;
3752                         }
3753                         break;
3754                 default:
3755                         bpf_log(log, "func '%s' doesn't have %d-th argument\n",
3756                                 tname, arg + 1);
3757                         return false;
3758                 }
3759         } else {
3760                 if (!t)
3761                         /* Default prog with 5 args */
3762                         return true;
3763                 t = btf_type_by_id(btf, args[arg].type);
3764         }
3765
3766         /* skip modifiers */
3767         while (btf_type_is_modifier(t))
3768                 t = btf_type_by_id(btf, t->type);
3769         if (btf_type_is_int(t) || btf_type_is_enum(t))
3770                 /* accessing a scalar */
3771                 return true;
3772         if (!btf_type_is_ptr(t)) {
3773                 bpf_log(log,
3774                         "func '%s' arg%d '%s' has type %s. Only pointer access is allowed\n",
3775                         tname, arg,
3776                         __btf_name_by_offset(btf, t->name_off),
3777                         btf_kind_str[BTF_INFO_KIND(t->info)]);
3778                 return false;
3779         }
3780         if (t->type == 0)
3781                 /* This is a pointer to void.
3782                  * It is the same as scalar from the verifier safety pov.
3783                  * No further pointer walking is allowed.
3784                  */
3785                 return true;
3786
3787         if (is_string_ptr(btf, t))
3788                 return true;
3789
3790         /* this is a pointer to another type */
3791         info->reg_type = PTR_TO_BTF_ID;
3792
3793         if (tgt_prog) {
3794                 ret = btf_translate_to_vmlinux(log, btf, t, tgt_prog->type, arg);
3795                 if (ret > 0) {
3796                         info->btf_id = ret;
3797                         return true;
3798                 } else {
3799                         return false;
3800                 }
3801         }
3802
3803         info->btf_id = t->type;
3804         t = btf_type_by_id(btf, t->type);
3805         /* skip modifiers */
3806         while (btf_type_is_modifier(t)) {
3807                 info->btf_id = t->type;
3808                 t = btf_type_by_id(btf, t->type);
3809         }
3810         if (!btf_type_is_struct(t)) {
3811                 bpf_log(log,
3812                         "func '%s' arg%d type %s is not a struct\n",
3813                         tname, arg, btf_kind_str[BTF_INFO_KIND(t->info)]);
3814                 return false;
3815         }
3816         bpf_log(log, "func '%s' arg%d has btf_id %d type %s '%s'\n",
3817                 tname, arg, info->btf_id, btf_kind_str[BTF_INFO_KIND(t->info)],
3818                 __btf_name_by_offset(btf, t->name_off));
3819         return true;
3820 }
3821
3822 int btf_struct_access(struct bpf_verifier_log *log,
3823                       const struct btf_type *t, int off, int size,
3824                       enum bpf_access_type atype,
3825                       u32 *next_btf_id)
3826 {
3827         u32 i, moff, mtrue_end, msize = 0, total_nelems = 0;
3828         const struct btf_type *mtype, *elem_type = NULL;
3829         const struct btf_member *member;
3830         const char *tname, *mname;
3831
3832 again:
3833         tname = __btf_name_by_offset(btf_vmlinux, t->name_off);
3834         if (!btf_type_is_struct(t)) {
3835                 bpf_log(log, "Type '%s' is not a struct\n", tname);
3836                 return -EINVAL;
3837         }
3838
3839         if (off + size > t->size) {
3840                 bpf_log(log, "access beyond struct %s at off %u size %u\n",
3841                         tname, off, size);
3842                 return -EACCES;
3843         }
3844
3845         for_each_member(i, t, member) {
3846                 /* offset of the field in bytes */
3847                 moff = btf_member_bit_offset(t, member) / 8;
3848                 if (off + size <= moff)
3849                         /* won't find anything, field is already too far */
3850                         break;
3851
3852                 if (btf_member_bitfield_size(t, member)) {
3853                         u32 end_bit = btf_member_bit_offset(t, member) +
3854                                 btf_member_bitfield_size(t, member);
3855
3856                         /* off <= moff instead of off == moff because clang
3857                          * does not generate a BTF member for anonymous
3858                          * bitfield like the ":16" here:
3859                          * struct {
3860                          *      int :16;
3861                          *      int x:8;
3862                          * };
3863                          */
3864                         if (off <= moff &&
3865                             BITS_ROUNDUP_BYTES(end_bit) <= off + size)
3866                                 return SCALAR_VALUE;
3867
3868                         /* off may be accessing a following member
3869                          *
3870                          * or
3871                          *
3872                          * Doing partial access at either end of this
3873                          * bitfield.  Continue on this case also to
3874                          * treat it as not accessing this bitfield
3875                          * and eventually error out as field not
3876                          * found to keep it simple.
3877                          * It could be relaxed if there was a legit
3878                          * partial access case later.
3879                          */
3880                         continue;
3881                 }
3882
3883                 /* In case of "off" is pointing to holes of a struct */
3884                 if (off < moff)
3885                         break;
3886
3887                 /* type of the field */
3888                 mtype = btf_type_by_id(btf_vmlinux, member->type);
3889                 mname = __btf_name_by_offset(btf_vmlinux, member->name_off);
3890
3891                 mtype = btf_resolve_size(btf_vmlinux, mtype, &msize,
3892                                          &elem_type, &total_nelems);
3893                 if (IS_ERR(mtype)) {
3894                         bpf_log(log, "field %s doesn't have size\n", mname);
3895                         return -EFAULT;
3896                 }
3897
3898                 mtrue_end = moff + msize;
3899                 if (off >= mtrue_end)
3900                         /* no overlap with member, keep iterating */
3901                         continue;
3902
3903                 if (btf_type_is_array(mtype)) {
3904                         u32 elem_idx;
3905
3906                         /* btf_resolve_size() above helps to
3907                          * linearize a multi-dimensional array.
3908                          *
3909                          * The logic here is treating an array
3910                          * in a struct as the following way:
3911                          *
3912                          * struct outer {
3913                          *      struct inner array[2][2];
3914                          * };
3915                          *
3916                          * looks like:
3917                          *
3918                          * struct outer {
3919                          *      struct inner array_elem0;
3920                          *      struct inner array_elem1;
3921                          *      struct inner array_elem2;
3922                          *      struct inner array_elem3;
3923                          * };
3924                          *
3925                          * When accessing outer->array[1][0], it moves
3926                          * moff to "array_elem2", set mtype to
3927                          * "struct inner", and msize also becomes
3928                          * sizeof(struct inner).  Then most of the
3929                          * remaining logic will fall through without
3930                          * caring the current member is an array or
3931                          * not.
3932                          *
3933                          * Unlike mtype/msize/moff, mtrue_end does not
3934                          * change.  The naming difference ("_true") tells
3935                          * that it is not always corresponding to
3936                          * the current mtype/msize/moff.
3937                          * It is the true end of the current
3938                          * member (i.e. array in this case).  That
3939                          * will allow an int array to be accessed like
3940                          * a scratch space,
3941                          * i.e. allow access beyond the size of
3942                          *      the array's element as long as it is
3943                          *      within the mtrue_end boundary.
3944                          */
3945
3946                         /* skip empty array */
3947                         if (moff == mtrue_end)
3948                                 continue;
3949
3950                         msize /= total_nelems;
3951                         elem_idx = (off - moff) / msize;
3952                         moff += elem_idx * msize;
3953                         mtype = elem_type;
3954                 }
3955
3956                 /* the 'off' we're looking for is either equal to start
3957                  * of this field or inside of this struct
3958                  */
3959                 if (btf_type_is_struct(mtype)) {
3960                         /* our field must be inside that union or struct */
3961                         t = mtype;
3962
3963                         /* adjust offset we're looking for */
3964                         off -= moff;
3965                         goto again;
3966                 }
3967
3968                 if (btf_type_is_ptr(mtype)) {
3969                         const struct btf_type *stype;
3970                         u32 id;
3971
3972                         if (msize != size || off != moff) {
3973                                 bpf_log(log,
3974                                         "cannot access ptr member %s with moff %u in struct %s with off %u size %u\n",
3975                                         mname, moff, tname, off, size);
3976                                 return -EACCES;
3977                         }
3978
3979                         stype = btf_type_skip_modifiers(btf_vmlinux, mtype->type, &id);
3980                         if (btf_type_is_struct(stype)) {
3981                                 *next_btf_id = id;
3982                                 return PTR_TO_BTF_ID;
3983                         }
3984                 }
3985
3986                 /* Allow more flexible access within an int as long as
3987                  * it is within mtrue_end.
3988                  * Since mtrue_end could be the end of an array,
3989                  * that also allows using an array of int as a scratch
3990                  * space. e.g. skb->cb[].
3991                  */
3992                 if (off + size > mtrue_end) {
3993                         bpf_log(log,
3994                                 "access beyond the end of member %s (mend:%u) in struct %s with off %u size %u\n",
3995                                 mname, mtrue_end, tname, off, size);
3996                         return -EACCES;
3997                 }
3998
3999                 return SCALAR_VALUE;
4000         }
4001         bpf_log(log, "struct %s doesn't have field at offset %d\n", tname, off);
4002         return -EINVAL;
4003 }
4004
4005 static int __btf_resolve_helper_id(struct bpf_verifier_log *log, void *fn,
4006                                    int arg)
4007 {
4008         char fnname[KSYM_SYMBOL_LEN + 4] = "btf_";
4009         const struct btf_param *args;
4010         const struct btf_type *t;
4011         const char *tname, *sym;
4012         u32 btf_id, i;
4013
4014         if (IS_ERR(btf_vmlinux)) {
4015                 bpf_log(log, "btf_vmlinux is malformed\n");
4016                 return -EINVAL;
4017         }
4018
4019         sym = kallsyms_lookup((long)fn, NULL, NULL, NULL, fnname + 4);
4020         if (!sym) {
4021                 bpf_log(log, "kernel doesn't have kallsyms\n");
4022                 return -EFAULT;
4023         }
4024
4025         for (i = 1; i <= btf_vmlinux->nr_types; i++) {
4026                 t = btf_type_by_id(btf_vmlinux, i);
4027                 if (BTF_INFO_KIND(t->info) != BTF_KIND_TYPEDEF)
4028                         continue;
4029                 tname = __btf_name_by_offset(btf_vmlinux, t->name_off);
4030                 if (!strcmp(tname, fnname))
4031                         break;
4032         }
4033         if (i > btf_vmlinux->nr_types) {
4034                 bpf_log(log, "helper %s type is not found\n", fnname);
4035                 return -ENOENT;
4036         }
4037
4038         t = btf_type_by_id(btf_vmlinux, t->type);
4039         if (!btf_type_is_ptr(t))
4040                 return -EFAULT;
4041         t = btf_type_by_id(btf_vmlinux, t->type);
4042         if (!btf_type_is_func_proto(t))
4043                 return -EFAULT;
4044
4045         args = (const struct btf_param *)(t + 1);
4046         if (arg >= btf_type_vlen(t)) {
4047                 bpf_log(log, "bpf helper %s doesn't have %d-th argument\n",
4048                         fnname, arg);
4049                 return -EINVAL;
4050         }
4051
4052         t = btf_type_by_id(btf_vmlinux, args[arg].type);
4053         if (!btf_type_is_ptr(t) || !t->type) {
4054                 /* anything but the pointer to struct is a helper config bug */
4055                 bpf_log(log, "ARG_PTR_TO_BTF is misconfigured\n");
4056                 return -EFAULT;
4057         }
4058         btf_id = t->type;
4059         t = btf_type_by_id(btf_vmlinux, t->type);
4060         /* skip modifiers */
4061         while (btf_type_is_modifier(t)) {
4062                 btf_id = t->type;
4063                 t = btf_type_by_id(btf_vmlinux, t->type);
4064         }
4065         if (!btf_type_is_struct(t)) {
4066                 bpf_log(log, "ARG_PTR_TO_BTF is not a struct\n");
4067                 return -EFAULT;
4068         }
4069         bpf_log(log, "helper %s arg%d has btf_id %d struct %s\n", fnname + 4,
4070                 arg, btf_id, __btf_name_by_offset(btf_vmlinux, t->name_off));
4071         return btf_id;
4072 }
4073
4074 int btf_resolve_helper_id(struct bpf_verifier_log *log,
4075                           const struct bpf_func_proto *fn, int arg)
4076 {
4077         int *btf_id = &fn->btf_id[arg];
4078         int ret;
4079
4080         if (fn->arg_type[arg] != ARG_PTR_TO_BTF_ID)
4081                 return -EINVAL;
4082
4083         ret = READ_ONCE(*btf_id);
4084         if (ret)
4085                 return ret;
4086         /* ok to race the search. The result is the same */
4087         ret = __btf_resolve_helper_id(log, fn->func, arg);
4088         if (!ret) {
4089                 /* Function argument cannot be type 'void' */
4090                 bpf_log(log, "BTF resolution bug\n");
4091                 return -EFAULT;
4092         }
4093         WRITE_ONCE(*btf_id, ret);
4094         return ret;
4095 }
4096
4097 static int __get_type_size(struct btf *btf, u32 btf_id,
4098                            const struct btf_type **bad_type)
4099 {
4100         const struct btf_type *t;
4101
4102         if (!btf_id)
4103                 /* void */
4104                 return 0;
4105         t = btf_type_by_id(btf, btf_id);
4106         while (t && btf_type_is_modifier(t))
4107                 t = btf_type_by_id(btf, t->type);
4108         if (!t) {
4109                 *bad_type = btf->types[0];
4110                 return -EINVAL;
4111         }
4112         if (btf_type_is_ptr(t))
4113                 /* kernel size of pointer. Not BPF's size of pointer*/
4114                 return sizeof(void *);
4115         if (btf_type_is_int(t) || btf_type_is_enum(t))
4116                 return t->size;
4117         *bad_type = t;
4118         return -EINVAL;
4119 }
4120
4121 int btf_distill_func_proto(struct bpf_verifier_log *log,
4122                            struct btf *btf,
4123                            const struct btf_type *func,
4124                            const char *tname,
4125                            struct btf_func_model *m)
4126 {
4127         const struct btf_param *args;
4128         const struct btf_type *t;
4129         u32 i, nargs;
4130         int ret;
4131
4132         if (!func) {
4133                 /* BTF function prototype doesn't match the verifier types.
4134                  * Fall back to 5 u64 args.
4135                  */
4136                 for (i = 0; i < 5; i++)
4137                         m->arg_size[i] = 8;
4138                 m->ret_size = 8;
4139                 m->nr_args = 5;
4140                 return 0;
4141         }
4142         args = (const struct btf_param *)(func + 1);
4143         nargs = btf_type_vlen(func);
4144         if (nargs >= MAX_BPF_FUNC_ARGS) {
4145                 bpf_log(log,
4146                         "The function %s has %d arguments. Too many.\n",
4147                         tname, nargs);
4148                 return -EINVAL;
4149         }
4150         ret = __get_type_size(btf, func->type, &t);
4151         if (ret < 0) {
4152                 bpf_log(log,
4153                         "The function %s return type %s is unsupported.\n",
4154                         tname, btf_kind_str[BTF_INFO_KIND(t->info)]);
4155                 return -EINVAL;
4156         }
4157         m->ret_size = ret;
4158
4159         for (i = 0; i < nargs; i++) {
4160                 ret = __get_type_size(btf, args[i].type, &t);
4161                 if (ret < 0) {
4162                         bpf_log(log,
4163                                 "The function %s arg%d type %s is unsupported.\n",
4164                                 tname, i, btf_kind_str[BTF_INFO_KIND(t->info)]);
4165                         return -EINVAL;
4166                 }
4167                 m->arg_size[i] = ret;
4168         }
4169         m->nr_args = nargs;
4170         return 0;
4171 }
4172
4173 /* Compare BTFs of two functions assuming only scalars and pointers to context.
4174  * t1 points to BTF_KIND_FUNC in btf1
4175  * t2 points to BTF_KIND_FUNC in btf2
4176  * Returns:
4177  * EINVAL - function prototype mismatch
4178  * EFAULT - verifier bug
4179  * 0 - 99% match. The last 1% is validated by the verifier.
4180  */
4181 static int btf_check_func_type_match(struct bpf_verifier_log *log,
4182                                      struct btf *btf1, const struct btf_type *t1,
4183                                      struct btf *btf2, const struct btf_type *t2)
4184 {
4185         const struct btf_param *args1, *args2;
4186         const char *fn1, *fn2, *s1, *s2;
4187         u32 nargs1, nargs2, i;
4188
4189         fn1 = btf_name_by_offset(btf1, t1->name_off);
4190         fn2 = btf_name_by_offset(btf2, t2->name_off);
4191
4192         if (btf_func_linkage(t1) != BTF_FUNC_GLOBAL) {
4193                 bpf_log(log, "%s() is not a global function\n", fn1);
4194                 return -EINVAL;
4195         }
4196         if (btf_func_linkage(t2) != BTF_FUNC_GLOBAL) {
4197                 bpf_log(log, "%s() is not a global function\n", fn2);
4198                 return -EINVAL;
4199         }
4200
4201         t1 = btf_type_by_id(btf1, t1->type);
4202         if (!t1 || !btf_type_is_func_proto(t1))
4203                 return -EFAULT;
4204         t2 = btf_type_by_id(btf2, t2->type);
4205         if (!t2 || !btf_type_is_func_proto(t2))
4206                 return -EFAULT;
4207
4208         args1 = (const struct btf_param *)(t1 + 1);
4209         nargs1 = btf_type_vlen(t1);
4210         args2 = (const struct btf_param *)(t2 + 1);
4211         nargs2 = btf_type_vlen(t2);
4212
4213         if (nargs1 != nargs2) {
4214                 bpf_log(log, "%s() has %d args while %s() has %d args\n",
4215                         fn1, nargs1, fn2, nargs2);
4216                 return -EINVAL;
4217         }
4218
4219         t1 = btf_type_skip_modifiers(btf1, t1->type, NULL);
4220         t2 = btf_type_skip_modifiers(btf2, t2->type, NULL);
4221         if (t1->info != t2->info) {
4222                 bpf_log(log,
4223                         "Return type %s of %s() doesn't match type %s of %s()\n",
4224                         btf_type_str(t1), fn1,
4225                         btf_type_str(t2), fn2);
4226                 return -EINVAL;
4227         }
4228
4229         for (i = 0; i < nargs1; i++) {
4230                 t1 = btf_type_skip_modifiers(btf1, args1[i].type, NULL);
4231                 t2 = btf_type_skip_modifiers(btf2, args2[i].type, NULL);
4232
4233                 if (t1->info != t2->info) {
4234                         bpf_log(log, "arg%d in %s() is %s while %s() has %s\n",
4235                                 i, fn1, btf_type_str(t1),
4236                                 fn2, btf_type_str(t2));
4237                         return -EINVAL;
4238                 }
4239                 if (btf_type_has_size(t1) && t1->size != t2->size) {
4240                         bpf_log(log,
4241                                 "arg%d in %s() has size %d while %s() has %d\n",
4242                                 i, fn1, t1->size,
4243                                 fn2, t2->size);
4244                         return -EINVAL;
4245                 }
4246
4247                 /* global functions are validated with scalars and pointers
4248                  * to context only. And only global functions can be replaced.
4249                  * Hence type check only those types.
4250                  */
4251                 if (btf_type_is_int(t1) || btf_type_is_enum(t1))
4252                         continue;
4253                 if (!btf_type_is_ptr(t1)) {
4254                         bpf_log(log,
4255                                 "arg%d in %s() has unrecognized type\n",
4256                                 i, fn1);
4257                         return -EINVAL;
4258                 }
4259                 t1 = btf_type_skip_modifiers(btf1, t1->type, NULL);
4260                 t2 = btf_type_skip_modifiers(btf2, t2->type, NULL);
4261                 if (!btf_type_is_struct(t1)) {
4262                         bpf_log(log,
4263                                 "arg%d in %s() is not a pointer to context\n",
4264                                 i, fn1);
4265                         return -EINVAL;
4266                 }
4267                 if (!btf_type_is_struct(t2)) {
4268                         bpf_log(log,
4269                                 "arg%d in %s() is not a pointer to context\n",
4270                                 i, fn2);
4271                         return -EINVAL;
4272                 }
4273                 /* This is an optional check to make program writing easier.
4274                  * Compare names of structs and report an error to the user.
4275                  * btf_prepare_func_args() already checked that t2 struct
4276                  * is a context type. btf_prepare_func_args() will check
4277                  * later that t1 struct is a context type as well.
4278                  */
4279                 s1 = btf_name_by_offset(btf1, t1->name_off);
4280                 s2 = btf_name_by_offset(btf2, t2->name_off);
4281                 if (strcmp(s1, s2)) {
4282                         bpf_log(log,
4283                                 "arg%d %s(struct %s *) doesn't match %s(struct %s *)\n",
4284                                 i, fn1, s1, fn2, s2);
4285                         return -EINVAL;
4286                 }
4287         }
4288         return 0;
4289 }
4290
4291 /* Compare BTFs of given program with BTF of target program */
4292 int btf_check_type_match(struct bpf_verifier_env *env, struct bpf_prog *prog,
4293                          struct btf *btf2, const struct btf_type *t2)
4294 {
4295         struct btf *btf1 = prog->aux->btf;
4296         const struct btf_type *t1;
4297         u32 btf_id = 0;
4298
4299         if (!prog->aux->func_info) {
4300                 bpf_log(&env->log, "Program extension requires BTF\n");
4301                 return -EINVAL;
4302         }
4303
4304         btf_id = prog->aux->func_info[0].type_id;
4305         if (!btf_id)
4306                 return -EFAULT;
4307
4308         t1 = btf_type_by_id(btf1, btf_id);
4309         if (!t1 || !btf_type_is_func(t1))
4310                 return -EFAULT;
4311
4312         return btf_check_func_type_match(&env->log, btf1, t1, btf2, t2);
4313 }
4314
4315 /* Compare BTF of a function with given bpf_reg_state.
4316  * Returns:
4317  * EFAULT - there is a verifier bug. Abort verification.
4318  * EINVAL - there is a type mismatch or BTF is not available.
4319  * 0 - BTF matches with what bpf_reg_state expects.
4320  * Only PTR_TO_CTX and SCALAR_VALUE states are recognized.
4321  */
4322 int btf_check_func_arg_match(struct bpf_verifier_env *env, int subprog,
4323                              struct bpf_reg_state *reg)
4324 {
4325         struct bpf_verifier_log *log = &env->log;
4326         struct bpf_prog *prog = env->prog;
4327         struct btf *btf = prog->aux->btf;
4328         const struct btf_param *args;
4329         const struct btf_type *t;
4330         u32 i, nargs, btf_id;
4331         const char *tname;
4332
4333         if (!prog->aux->func_info)
4334                 return -EINVAL;
4335
4336         btf_id = prog->aux->func_info[subprog].type_id;
4337         if (!btf_id)
4338                 return -EFAULT;
4339
4340         if (prog->aux->func_info_aux[subprog].unreliable)
4341                 return -EINVAL;
4342
4343         t = btf_type_by_id(btf, btf_id);
4344         if (!t || !btf_type_is_func(t)) {
4345                 /* These checks were already done by the verifier while loading
4346                  * struct bpf_func_info
4347                  */
4348                 bpf_log(log, "BTF of func#%d doesn't point to KIND_FUNC\n",
4349                         subprog);
4350                 return -EFAULT;
4351         }
4352         tname = btf_name_by_offset(btf, t->name_off);
4353
4354         t = btf_type_by_id(btf, t->type);
4355         if (!t || !btf_type_is_func_proto(t)) {
4356                 bpf_log(log, "Invalid BTF of func %s\n", tname);
4357                 return -EFAULT;
4358         }
4359         args = (const struct btf_param *)(t + 1);
4360         nargs = btf_type_vlen(t);
4361         if (nargs > 5) {
4362                 bpf_log(log, "Function %s has %d > 5 args\n", tname, nargs);
4363                 goto out;
4364         }
4365         /* check that BTF function arguments match actual types that the
4366          * verifier sees.
4367          */
4368         for (i = 0; i < nargs; i++) {
4369                 t = btf_type_by_id(btf, args[i].type);
4370                 while (btf_type_is_modifier(t))
4371                         t = btf_type_by_id(btf, t->type);
4372                 if (btf_type_is_int(t) || btf_type_is_enum(t)) {
4373                         if (reg[i + 1].type == SCALAR_VALUE)
4374                                 continue;
4375                         bpf_log(log, "R%d is not a scalar\n", i + 1);
4376                         goto out;
4377                 }
4378                 if (btf_type_is_ptr(t)) {
4379                         if (reg[i + 1].type == SCALAR_VALUE) {
4380                                 bpf_log(log, "R%d is not a pointer\n", i + 1);
4381                                 goto out;
4382                         }
4383                         /* If function expects ctx type in BTF check that caller
4384                          * is passing PTR_TO_CTX.
4385                          */
4386                         if (btf_get_prog_ctx_type(log, btf, t, prog->type, i)) {
4387                                 if (reg[i + 1].type != PTR_TO_CTX) {
4388                                         bpf_log(log,
4389                                                 "arg#%d expected pointer to ctx, but got %s\n",
4390                                                 i, btf_kind_str[BTF_INFO_KIND(t->info)]);
4391                                         goto out;
4392                                 }
4393                                 if (check_ctx_reg(env, &reg[i + 1], i + 1))
4394                                         goto out;
4395                                 continue;
4396                         }
4397                 }
4398                 bpf_log(log, "Unrecognized arg#%d type %s\n",
4399                         i, btf_kind_str[BTF_INFO_KIND(t->info)]);
4400                 goto out;
4401         }
4402         return 0;
4403 out:
4404         /* Compiler optimizations can remove arguments from static functions
4405          * or mismatched type can be passed into a global function.
4406          * In such cases mark the function as unreliable from BTF point of view.
4407          */
4408         prog->aux->func_info_aux[subprog].unreliable = true;
4409         return -EINVAL;
4410 }
4411
4412 /* Convert BTF of a function into bpf_reg_state if possible
4413  * Returns:
4414  * EFAULT - there is a verifier bug. Abort verification.
4415  * EINVAL - cannot convert BTF.
4416  * 0 - Successfully converted BTF into bpf_reg_state
4417  * (either PTR_TO_CTX or SCALAR_VALUE).
4418  */
4419 int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog,
4420                           struct bpf_reg_state *reg)
4421 {
4422         struct bpf_verifier_log *log = &env->log;
4423         struct bpf_prog *prog = env->prog;
4424         enum bpf_prog_type prog_type = prog->type;
4425         struct btf *btf = prog->aux->btf;
4426         const struct btf_param *args;
4427         const struct btf_type *t;
4428         u32 i, nargs, btf_id;
4429         const char *tname;
4430
4431         if (!prog->aux->func_info ||
4432             prog->aux->func_info_aux[subprog].linkage != BTF_FUNC_GLOBAL) {
4433                 bpf_log(log, "Verifier bug\n");
4434                 return -EFAULT;
4435         }
4436
4437         btf_id = prog->aux->func_info[subprog].type_id;
4438         if (!btf_id) {
4439                 bpf_log(log, "Global functions need valid BTF\n");
4440                 return -EFAULT;
4441         }
4442
4443         t = btf_type_by_id(btf, btf_id);
4444         if (!t || !btf_type_is_func(t)) {
4445                 /* These checks were already done by the verifier while loading
4446                  * struct bpf_func_info
4447                  */
4448                 bpf_log(log, "BTF of func#%d doesn't point to KIND_FUNC\n",
4449                         subprog);
4450                 return -EFAULT;
4451         }
4452         tname = btf_name_by_offset(btf, t->name_off);
4453
4454         if (log->level & BPF_LOG_LEVEL)
4455                 bpf_log(log, "Validating %s() func#%d...\n",
4456                         tname, subprog);
4457
4458         if (prog->aux->func_info_aux[subprog].unreliable) {
4459                 bpf_log(log, "Verifier bug in function %s()\n", tname);
4460                 return -EFAULT;
4461         }
4462         if (prog_type == BPF_PROG_TYPE_EXT)
4463                 prog_type = prog->aux->linked_prog->type;
4464
4465         t = btf_type_by_id(btf, t->type);
4466         if (!t || !btf_type_is_func_proto(t)) {
4467                 bpf_log(log, "Invalid type of function %s()\n", tname);
4468                 return -EFAULT;
4469         }
4470         args = (const struct btf_param *)(t + 1);
4471         nargs = btf_type_vlen(t);
4472         if (nargs > 5) {
4473                 bpf_log(log, "Global function %s() with %d > 5 args. Buggy compiler.\n",
4474                         tname, nargs);
4475                 return -EINVAL;
4476         }
4477         /* check that function returns int */
4478         t = btf_type_by_id(btf, t->type);
4479         while (btf_type_is_modifier(t))
4480                 t = btf_type_by_id(btf, t->type);
4481         if (!btf_type_is_int(t) && !btf_type_is_enum(t)) {
4482                 bpf_log(log,
4483                         "Global function %s() doesn't return scalar. Only those are supported.\n",
4484                         tname);
4485                 return -EINVAL;
4486         }
4487         /* Convert BTF function arguments into verifier types.
4488          * Only PTR_TO_CTX and SCALAR are supported atm.
4489          */
4490         for (i = 0; i < nargs; i++) {
4491                 t = btf_type_by_id(btf, args[i].type);
4492                 while (btf_type_is_modifier(t))
4493                         t = btf_type_by_id(btf, t->type);
4494                 if (btf_type_is_int(t) || btf_type_is_enum(t)) {
4495                         reg[i + 1].type = SCALAR_VALUE;
4496                         continue;
4497                 }
4498                 if (btf_type_is_ptr(t) &&
4499                     btf_get_prog_ctx_type(log, btf, t, prog_type, i)) {
4500                         reg[i + 1].type = PTR_TO_CTX;
4501                         continue;
4502                 }
4503                 bpf_log(log, "Arg#%d type %s in %s() is not supported yet.\n",
4504                         i, btf_kind_str[BTF_INFO_KIND(t->info)], tname);
4505                 return -EINVAL;
4506         }
4507         return 0;
4508 }
4509
4510 void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj,
4511                        struct seq_file *m)
4512 {
4513         const struct btf_type *t = btf_type_by_id(btf, type_id);
4514
4515         btf_type_ops(t)->seq_show(btf, t, type_id, obj, 0, m);
4516 }
4517
4518 #ifdef CONFIG_PROC_FS
4519 static void bpf_btf_show_fdinfo(struct seq_file *m, struct file *filp)
4520 {
4521         const struct btf *btf = filp->private_data;
4522
4523         seq_printf(m, "btf_id:\t%u\n", btf->id);
4524 }
4525 #endif
4526
4527 static int btf_release(struct inode *inode, struct file *filp)
4528 {
4529         btf_put(filp->private_data);
4530         return 0;
4531 }
4532
4533 const struct file_operations btf_fops = {
4534 #ifdef CONFIG_PROC_FS
4535         .show_fdinfo    = bpf_btf_show_fdinfo,
4536 #endif
4537         .release        = btf_release,
4538 };
4539
4540 static int __btf_new_fd(struct btf *btf)
4541 {
4542         return anon_inode_getfd("btf", &btf_fops, btf, O_RDONLY | O_CLOEXEC);
4543 }
4544
4545 int btf_new_fd(const union bpf_attr *attr)
4546 {
4547         struct btf *btf;
4548         int ret;
4549
4550         btf = btf_parse(u64_to_user_ptr(attr->btf),
4551                         attr->btf_size, attr->btf_log_level,
4552                         u64_to_user_ptr(attr->btf_log_buf),
4553                         attr->btf_log_size);
4554         if (IS_ERR(btf))
4555                 return PTR_ERR(btf);
4556
4557         ret = btf_alloc_id(btf);
4558         if (ret) {
4559                 btf_free(btf);
4560                 return ret;
4561         }
4562
4563         /*
4564          * The BTF ID is published to the userspace.
4565          * All BTF free must go through call_rcu() from
4566          * now on (i.e. free by calling btf_put()).
4567          */
4568
4569         ret = __btf_new_fd(btf);
4570         if (ret < 0)
4571                 btf_put(btf);
4572
4573         return ret;
4574 }
4575
4576 struct btf *btf_get_by_fd(int fd)
4577 {
4578         struct btf *btf;
4579         struct fd f;
4580
4581         f = fdget(fd);
4582
4583         if (!f.file)
4584                 return ERR_PTR(-EBADF);
4585
4586         if (f.file->f_op != &btf_fops) {
4587                 fdput(f);
4588                 return ERR_PTR(-EINVAL);
4589         }
4590
4591         btf = f.file->private_data;
4592         refcount_inc(&btf->refcnt);
4593         fdput(f);
4594
4595         return btf;
4596 }
4597
4598 int btf_get_info_by_fd(const struct btf *btf,
4599                        const union bpf_attr *attr,
4600                        union bpf_attr __user *uattr)
4601 {
4602         struct bpf_btf_info __user *uinfo;
4603         struct bpf_btf_info info;
4604         u32 info_copy, btf_copy;
4605         void __user *ubtf;
4606         u32 uinfo_len;
4607
4608         uinfo = u64_to_user_ptr(attr->info.info);
4609         uinfo_len = attr->info.info_len;
4610
4611         info_copy = min_t(u32, uinfo_len, sizeof(info));
4612         memset(&info, 0, sizeof(info));
4613         if (copy_from_user(&info, uinfo, info_copy))
4614                 return -EFAULT;
4615
4616         info.id = btf->id;
4617         ubtf = u64_to_user_ptr(info.btf);
4618         btf_copy = min_t(u32, btf->data_size, info.btf_size);
4619         if (copy_to_user(ubtf, btf->data, btf_copy))
4620                 return -EFAULT;
4621         info.btf_size = btf->data_size;
4622
4623         if (copy_to_user(uinfo, &info, info_copy) ||
4624             put_user(info_copy, &uattr->info.info_len))
4625                 return -EFAULT;
4626
4627         return 0;
4628 }
4629
4630 int btf_get_fd_by_id(u32 id)
4631 {
4632         struct btf *btf;
4633         int fd;
4634
4635         rcu_read_lock();
4636         btf = idr_find(&btf_idr, id);
4637         if (!btf || !refcount_inc_not_zero(&btf->refcnt))
4638                 btf = ERR_PTR(-ENOENT);
4639         rcu_read_unlock();
4640
4641         if (IS_ERR(btf))
4642                 return PTR_ERR(btf);
4643
4644         fd = __btf_new_fd(btf);
4645         if (fd < 0)
4646                 btf_put(btf);
4647
4648         return fd;
4649 }
4650
4651 u32 btf_id(const struct btf *btf)
4652 {
4653         return btf->id;
4654 }