1
2
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/btf_ids.h>
22#include <linux/skmsg.h>
23#include <linux/perf_event.h>
24#include <linux/bsearch.h>
25#include <linux/btf_ids.h>
26#include <net/sock.h>
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168#define BITS_PER_U128 (sizeof(u64) * BITS_PER_BYTE * 2)
169#define BITS_PER_BYTE_MASK (BITS_PER_BYTE - 1)
170#define BITS_PER_BYTE_MASKED(bits) ((bits) & BITS_PER_BYTE_MASK)
171#define BITS_ROUNDDOWN_BYTES(bits) ((bits) >> 3)
172#define BITS_ROUNDUP_BYTES(bits) \
173 (BITS_ROUNDDOWN_BYTES(bits) + !!BITS_PER_BYTE_MASKED(bits))
174
175#define BTF_INFO_MASK 0x8f00ffff
176#define BTF_INT_MASK 0x0fffffff
177#define BTF_TYPE_ID_VALID(type_id) ((type_id) <= BTF_MAX_TYPE)
178#define BTF_STR_OFFSET_VALID(name_off) ((name_off) <= BTF_MAX_NAME_OFFSET)
179
180
181
182
183
184#define BTF_MAX_SIZE (16 * 1024 * 1024)
185
186#define for_each_member_from(i, from, struct_type, member) \
187 for (i = from, member = btf_type_member(struct_type) + from; \
188 i < btf_type_vlen(struct_type); \
189 i++, member++)
190
191#define for_each_vsi_from(i, from, struct_type, member) \
192 for (i = from, member = btf_type_var_secinfo(struct_type) + from; \
193 i < btf_type_vlen(struct_type); \
194 i++, member++)
195
196DEFINE_IDR(btf_idr);
197DEFINE_SPINLOCK(btf_idr_lock);
198
199struct btf {
200 void *data;
201 struct btf_type **types;
202 u32 *resolved_ids;
203 u32 *resolved_sizes;
204 const char *strings;
205 void *nohdr_data;
206 struct btf_header hdr;
207 u32 nr_types;
208 u32 types_size;
209 u32 data_size;
210 refcount_t refcnt;
211 u32 id;
212 struct rcu_head rcu;
213};
214
215enum verifier_phase {
216 CHECK_META,
217 CHECK_TYPE,
218};
219
220struct resolve_vertex {
221 const struct btf_type *t;
222 u32 type_id;
223 u16 next_member;
224};
225
226enum visit_state {
227 NOT_VISITED,
228 VISITED,
229 RESOLVED,
230};
231
232enum resolve_mode {
233 RESOLVE_TBD,
234 RESOLVE_PTR,
235 RESOLVE_STRUCT_OR_ARRAY,
236
237
238};
239
240#define MAX_RESOLVE_DEPTH 32
241
242struct btf_sec_info {
243 u32 off;
244 u32 len;
245};
246
247struct btf_verifier_env {
248 struct btf *btf;
249 u8 *visit_states;
250 struct resolve_vertex stack[MAX_RESOLVE_DEPTH];
251 struct bpf_verifier_log log;
252 u32 log_type_id;
253 u32 top_stack;
254 enum verifier_phase phase;
255 enum resolve_mode resolve_mode;
256};
257
258static const char * const btf_kind_str[NR_BTF_KINDS] = {
259 [BTF_KIND_UNKN] = "UNKNOWN",
260 [BTF_KIND_INT] = "INT",
261 [BTF_KIND_PTR] = "PTR",
262 [BTF_KIND_ARRAY] = "ARRAY",
263 [BTF_KIND_STRUCT] = "STRUCT",
264 [BTF_KIND_UNION] = "UNION",
265 [BTF_KIND_ENUM] = "ENUM",
266 [BTF_KIND_FWD] = "FWD",
267 [BTF_KIND_TYPEDEF] = "TYPEDEF",
268 [BTF_KIND_VOLATILE] = "VOLATILE",
269 [BTF_KIND_CONST] = "CONST",
270 [BTF_KIND_RESTRICT] = "RESTRICT",
271 [BTF_KIND_FUNC] = "FUNC",
272 [BTF_KIND_FUNC_PROTO] = "FUNC_PROTO",
273 [BTF_KIND_VAR] = "VAR",
274 [BTF_KIND_DATASEC] = "DATASEC",
275};
276
277static const char *btf_type_str(const struct btf_type *t)
278{
279 return btf_kind_str[BTF_INFO_KIND(t->info)];
280}
281
282
283#define BTF_SHOW_OBJ_SAFE_SIZE 32
284
285
286
287
288
289
290
291
292#define BTF_SHOW_OBJ_BASE_TYPE_SIZE 16
293
294
295#define BTF_SHOW_NAME_SIZE 80
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340struct btf_show {
341 u64 flags;
342 void *target;
343 void (*showfn)(struct btf_show *show, const char *fmt, va_list args);
344 const struct btf *btf;
345
346 struct {
347 u8 depth;
348 u8 depth_to_show;
349 u8 depth_check;
350 u8 array_member:1,
351 array_terminated:1;
352 u16 array_encoding;
353 u32 type_id;
354 int status;
355 const struct btf_type *type;
356 const struct btf_member *member;
357 char name[BTF_SHOW_NAME_SIZE];
358 } state;
359 struct {
360 u32 size;
361 void *head;
362 void *data;
363 u8 safe[BTF_SHOW_OBJ_SAFE_SIZE];
364 } obj;
365};
366
367struct btf_kind_operations {
368 s32 (*check_meta)(struct btf_verifier_env *env,
369 const struct btf_type *t,
370 u32 meta_left);
371 int (*resolve)(struct btf_verifier_env *env,
372 const struct resolve_vertex *v);
373 int (*check_member)(struct btf_verifier_env *env,
374 const struct btf_type *struct_type,
375 const struct btf_member *member,
376 const struct btf_type *member_type);
377 int (*check_kflag_member)(struct btf_verifier_env *env,
378 const struct btf_type *struct_type,
379 const struct btf_member *member,
380 const struct btf_type *member_type);
381 void (*log_details)(struct btf_verifier_env *env,
382 const struct btf_type *t);
383 void (*show)(const struct btf *btf, const struct btf_type *t,
384 u32 type_id, void *data, u8 bits_offsets,
385 struct btf_show *show);
386};
387
388static const struct btf_kind_operations * const kind_ops[NR_BTF_KINDS];
389static struct btf_type btf_void;
390
391static int btf_resolve(struct btf_verifier_env *env,
392 const struct btf_type *t, u32 type_id);
393
394static bool btf_type_is_modifier(const struct btf_type *t)
395{
396
397
398
399
400
401
402
403
404
405
406 switch (BTF_INFO_KIND(t->info)) {
407 case BTF_KIND_TYPEDEF:
408 case BTF_KIND_VOLATILE:
409 case BTF_KIND_CONST:
410 case BTF_KIND_RESTRICT:
411 return true;
412 }
413
414 return false;
415}
416
417bool btf_type_is_void(const struct btf_type *t)
418{
419 return t == &btf_void;
420}
421
422static bool btf_type_is_fwd(const struct btf_type *t)
423{
424 return BTF_INFO_KIND(t->info) == BTF_KIND_FWD;
425}
426
427static bool btf_type_nosize(const struct btf_type *t)
428{
429 return btf_type_is_void(t) || btf_type_is_fwd(t) ||
430 btf_type_is_func(t) || btf_type_is_func_proto(t);
431}
432
433static bool btf_type_nosize_or_null(const struct btf_type *t)
434{
435 return !t || btf_type_nosize(t);
436}
437
438static bool __btf_type_is_struct(const struct btf_type *t)
439{
440 return BTF_INFO_KIND(t->info) == BTF_KIND_STRUCT;
441}
442
443static bool btf_type_is_array(const struct btf_type *t)
444{
445 return BTF_INFO_KIND(t->info) == BTF_KIND_ARRAY;
446}
447
448static bool btf_type_is_datasec(const struct btf_type *t)
449{
450 return BTF_INFO_KIND(t->info) == BTF_KIND_DATASEC;
451}
452
453s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind)
454{
455 const struct btf_type *t;
456 const char *tname;
457 u32 i;
458
459 for (i = 1; i <= btf->nr_types; i++) {
460 t = btf->types[i];
461 if (BTF_INFO_KIND(t->info) != kind)
462 continue;
463
464 tname = btf_name_by_offset(btf, t->name_off);
465 if (!strcmp(tname, name))
466 return i;
467 }
468
469 return -ENOENT;
470}
471
472const struct btf_type *btf_type_skip_modifiers(const struct btf *btf,
473 u32 id, u32 *res_id)
474{
475 const struct btf_type *t = btf_type_by_id(btf, id);
476
477 while (btf_type_is_modifier(t)) {
478 id = t->type;
479 t = btf_type_by_id(btf, t->type);
480 }
481
482 if (res_id)
483 *res_id = id;
484
485 return t;
486}
487
488const struct btf_type *btf_type_resolve_ptr(const struct btf *btf,
489 u32 id, u32 *res_id)
490{
491 const struct btf_type *t;
492
493 t = btf_type_skip_modifiers(btf, id, NULL);
494 if (!btf_type_is_ptr(t))
495 return NULL;
496
497 return btf_type_skip_modifiers(btf, t->type, res_id);
498}
499
500const struct btf_type *btf_type_resolve_func_ptr(const struct btf *btf,
501 u32 id, u32 *res_id)
502{
503 const struct btf_type *ptype;
504
505 ptype = btf_type_resolve_ptr(btf, id, res_id);
506 if (ptype && btf_type_is_func_proto(ptype))
507 return ptype;
508
509 return NULL;
510}
511
512
513
514
515static bool btf_type_is_resolve_source_only(const struct btf_type *t)
516{
517 return btf_type_is_var(t) ||
518 btf_type_is_datasec(t);
519}
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537static bool btf_type_needs_resolve(const struct btf_type *t)
538{
539 return btf_type_is_modifier(t) ||
540 btf_type_is_ptr(t) ||
541 btf_type_is_struct(t) ||
542 btf_type_is_array(t) ||
543 btf_type_is_var(t) ||
544 btf_type_is_datasec(t);
545}
546
547
548static bool btf_type_has_size(const struct btf_type *t)
549{
550 switch (BTF_INFO_KIND(t->info)) {
551 case BTF_KIND_INT:
552 case BTF_KIND_STRUCT:
553 case BTF_KIND_UNION:
554 case BTF_KIND_ENUM:
555 case BTF_KIND_DATASEC:
556 return true;
557 }
558
559 return false;
560}
561
562static const char *btf_int_encoding_str(u8 encoding)
563{
564 if (encoding == 0)
565 return "(none)";
566 else if (encoding == BTF_INT_SIGNED)
567 return "SIGNED";
568 else if (encoding == BTF_INT_CHAR)
569 return "CHAR";
570 else if (encoding == BTF_INT_BOOL)
571 return "BOOL";
572 else
573 return "UNKN";
574}
575
576static u32 btf_type_int(const struct btf_type *t)
577{
578 return *(u32 *)(t + 1);
579}
580
581static const struct btf_array *btf_type_array(const struct btf_type *t)
582{
583 return (const struct btf_array *)(t + 1);
584}
585
586static const struct btf_enum *btf_type_enum(const struct btf_type *t)
587{
588 return (const struct btf_enum *)(t + 1);
589}
590
591static const struct btf_var *btf_type_var(const struct btf_type *t)
592{
593 return (const struct btf_var *)(t + 1);
594}
595
596static const struct btf_kind_operations *btf_type_ops(const struct btf_type *t)
597{
598 return kind_ops[BTF_INFO_KIND(t->info)];
599}
600
601static bool btf_name_offset_valid(const struct btf *btf, u32 offset)
602{
603 return BTF_STR_OFFSET_VALID(offset) &&
604 offset < btf->hdr.str_len;
605}
606
607static bool __btf_name_char_ok(char c, bool first, bool dot_ok)
608{
609 if ((first ? !isalpha(c) :
610 !isalnum(c)) &&
611 c != '_' &&
612 ((c == '.' && !dot_ok) ||
613 c != '.'))
614 return false;
615 return true;
616}
617
618static bool __btf_name_valid(const struct btf *btf, u32 offset, bool dot_ok)
619{
620
621 const char *src = &btf->strings[offset];
622 const char *src_limit;
623
624 if (!__btf_name_char_ok(*src, true, dot_ok))
625 return false;
626
627
628 src_limit = src + KSYM_NAME_LEN;
629 src++;
630 while (*src && src < src_limit) {
631 if (!__btf_name_char_ok(*src, false, dot_ok))
632 return false;
633 src++;
634 }
635
636 return !*src;
637}
638
639
640
641
642static bool btf_name_valid_identifier(const struct btf *btf, u32 offset)
643{
644 return __btf_name_valid(btf, offset, false);
645}
646
647static bool btf_name_valid_section(const struct btf *btf, u32 offset)
648{
649 return __btf_name_valid(btf, offset, true);
650}
651
652static const char *__btf_name_by_offset(const struct btf *btf, u32 offset)
653{
654 if (!offset)
655 return "(anon)";
656 else if (offset < btf->hdr.str_len)
657 return &btf->strings[offset];
658 else
659 return "(invalid-name-offset)";
660}
661
662const char *btf_name_by_offset(const struct btf *btf, u32 offset)
663{
664 if (offset < btf->hdr.str_len)
665 return &btf->strings[offset];
666
667 return NULL;
668}
669
670const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id)
671{
672 if (type_id > btf->nr_types)
673 return NULL;
674
675 return btf->types[type_id];
676}
677
678
679
680
681
682static bool btf_type_int_is_regular(const struct btf_type *t)
683{
684 u8 nr_bits, nr_bytes;
685 u32 int_data;
686
687 int_data = btf_type_int(t);
688 nr_bits = BTF_INT_BITS(int_data);
689 nr_bytes = BITS_ROUNDUP_BYTES(nr_bits);
690 if (BITS_PER_BYTE_MASKED(nr_bits) ||
691 BTF_INT_OFFSET(int_data) ||
692 (nr_bytes != sizeof(u8) && nr_bytes != sizeof(u16) &&
693 nr_bytes != sizeof(u32) && nr_bytes != sizeof(u64) &&
694 nr_bytes != (2 * sizeof(u64)))) {
695 return false;
696 }
697
698 return true;
699}
700
701
702
703
704
705bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s,
706 const struct btf_member *m,
707 u32 expected_offset, u32 expected_size)
708{
709 const struct btf_type *t;
710 u32 id, int_data;
711 u8 nr_bits;
712
713 id = m->type;
714 t = btf_type_id_size(btf, &id, NULL);
715 if (!t || !btf_type_is_int(t))
716 return false;
717
718 int_data = btf_type_int(t);
719 nr_bits = BTF_INT_BITS(int_data);
720 if (btf_type_kflag(s)) {
721 u32 bitfield_size = BTF_MEMBER_BITFIELD_SIZE(m->offset);
722 u32 bit_offset = BTF_MEMBER_BIT_OFFSET(m->offset);
723
724
725
726
727 return !bitfield_size &&
728 BITS_ROUNDUP_BYTES(bit_offset) == expected_offset &&
729 BITS_ROUNDUP_BYTES(nr_bits) == expected_size;
730 }
731
732 if (BTF_INT_OFFSET(int_data) ||
733 BITS_PER_BYTE_MASKED(m->offset) ||
734 BITS_ROUNDUP_BYTES(m->offset) != expected_offset ||
735 BITS_PER_BYTE_MASKED(nr_bits) ||
736 BITS_ROUNDUP_BYTES(nr_bits) != expected_size)
737 return false;
738
739 return true;
740}
741
742
743static const struct btf_type *btf_type_skip_qualifiers(const struct btf *btf,
744 u32 id)
745{
746 const struct btf_type *t = btf_type_by_id(btf, id);
747
748 while (btf_type_is_modifier(t) &&
749 BTF_INFO_KIND(t->info) != BTF_KIND_TYPEDEF) {
750 id = t->type;
751 t = btf_type_by_id(btf, t->type);
752 }
753
754 return t;
755}
756
757#define BTF_SHOW_MAX_ITER 10
758
759#define BTF_KIND_BIT(kind) (1ULL << kind)
760
761
762
763
764
765
766
767static const char *btf_show_name(struct btf_show *show)
768{
769
770 const char *array_suffixes = "[][][][][][][][][][]";
771 const char *array_suffix = &array_suffixes[strlen(array_suffixes)];
772
773 const char *ptr_suffixes = "**********";
774 const char *ptr_suffix = &ptr_suffixes[strlen(ptr_suffixes)];
775 const char *name = NULL, *prefix = "", *parens = "";
776 const struct btf_member *m = show->state.member;
777 const struct btf_type *t = show->state.type;
778 const struct btf_array *array;
779 u32 id = show->state.type_id;
780 const char *member = NULL;
781 bool show_member = false;
782 u64 kinds = 0;
783 int i;
784
785 show->state.name[0] = '\0';
786
787
788
789
790
791
792 if (show->state.array_member)
793 return "";
794
795
796 if (m) {
797 member = btf_name_by_offset(show->btf, m->name_off);
798 show_member = strlen(member) > 0;
799 id = m->type;
800 }
801
802
803
804
805
806
807
808
809 t = btf_type_by_id(show->btf, id);
810 if (!t)
811 return "";
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835 for (i = 0; i < BTF_SHOW_MAX_ITER; i++) {
836
837 switch (BTF_INFO_KIND(t->info)) {
838 case BTF_KIND_TYPEDEF:
839 if (!name)
840 name = btf_name_by_offset(show->btf,
841 t->name_off);
842 kinds |= BTF_KIND_BIT(BTF_KIND_TYPEDEF);
843 id = t->type;
844 break;
845 case BTF_KIND_ARRAY:
846 kinds |= BTF_KIND_BIT(BTF_KIND_ARRAY);
847 parens = "[";
848 if (!t)
849 return "";
850 array = btf_type_array(t);
851 if (array_suffix > array_suffixes)
852 array_suffix -= 2;
853 id = array->type;
854 break;
855 case BTF_KIND_PTR:
856 kinds |= BTF_KIND_BIT(BTF_KIND_PTR);
857 if (ptr_suffix > ptr_suffixes)
858 ptr_suffix -= 1;
859 id = t->type;
860 break;
861 default:
862 id = 0;
863 break;
864 }
865 if (!id)
866 break;
867 t = btf_type_skip_qualifiers(show->btf, id);
868 }
869
870 if (i == BTF_SHOW_MAX_ITER)
871 return "";
872
873 if (!name)
874 name = btf_name_by_offset(show->btf, t->name_off);
875
876 switch (BTF_INFO_KIND(t->info)) {
877 case BTF_KIND_STRUCT:
878 case BTF_KIND_UNION:
879 prefix = BTF_INFO_KIND(t->info) == BTF_KIND_STRUCT ?
880 "struct" : "union";
881
882 if (!(kinds & (BTF_KIND_BIT(BTF_KIND_ARRAY))))
883 parens = "{";
884 break;
885 case BTF_KIND_ENUM:
886 prefix = "enum";
887 break;
888 default:
889 break;
890 }
891
892
893 if (kinds & BTF_KIND_BIT(BTF_KIND_PTR))
894 parens = "";
895
896 if (kinds & BTF_KIND_BIT(BTF_KIND_TYPEDEF))
897 prefix = "";
898
899 if (!name)
900 name = "";
901
902
903 if (show->flags & BTF_SHOW_NONAME)
904 snprintf(show->state.name, sizeof(show->state.name), "%s",
905 parens);
906 else
907 snprintf(show->state.name, sizeof(show->state.name),
908 "%s%s%s(%s%s%s%s%s%s)%s",
909
910 show_member ? "." : "",
911 show_member ? member : "",
912 show_member ? " = " : "",
913
914 prefix,
915 strlen(prefix) > 0 && strlen(name) > 0 ? " " : "",
916
917 name,
918
919 strlen(ptr_suffix) > 0 ? " " : "", ptr_suffix,
920 array_suffix, parens);
921
922 return show->state.name;
923}
924
925static const char *__btf_show_indent(struct btf_show *show)
926{
927 const char *indents = " ";
928 const char *indent = &indents[strlen(indents)];
929
930 if ((indent - show->state.depth) >= indents)
931 return indent - show->state.depth;
932 return indents;
933}
934
935static const char *btf_show_indent(struct btf_show *show)
936{
937 return show->flags & BTF_SHOW_COMPACT ? "" : __btf_show_indent(show);
938}
939
940static const char *btf_show_newline(struct btf_show *show)
941{
942 return show->flags & BTF_SHOW_COMPACT ? "" : "\n";
943}
944
945static const char *btf_show_delim(struct btf_show *show)
946{
947 if (show->state.depth == 0)
948 return "";
949
950 if ((show->flags & BTF_SHOW_COMPACT) && show->state.type &&
951 BTF_INFO_KIND(show->state.type->info) == BTF_KIND_UNION)
952 return "|";
953
954 return ",";
955}
956
957__printf(2, 3) static void btf_show(struct btf_show *show, const char *fmt, ...)
958{
959 va_list args;
960
961 if (!show->state.depth_check) {
962 va_start(args, fmt);
963 show->showfn(show, fmt, args);
964 va_end(args);
965 }
966}
967
968
969
970
971
972
973#define btf_show_type_value(show, fmt, value) \
974 do { \
975 if ((value) != 0 || (show->flags & BTF_SHOW_ZERO) || \
976 show->state.depth == 0) { \
977 btf_show(show, "%s%s" fmt "%s%s", \
978 btf_show_indent(show), \
979 btf_show_name(show), \
980 value, btf_show_delim(show), \
981 btf_show_newline(show)); \
982 if (show->state.depth > show->state.depth_to_show) \
983 show->state.depth_to_show = show->state.depth; \
984 } \
985 } while (0)
986
987#define btf_show_type_values(show, fmt, ...) \
988 do { \
989 btf_show(show, "%s%s" fmt "%s%s", btf_show_indent(show), \
990 btf_show_name(show), \
991 __VA_ARGS__, btf_show_delim(show), \
992 btf_show_newline(show)); \
993 if (show->state.depth > show->state.depth_to_show) \
994 show->state.depth_to_show = show->state.depth; \
995 } while (0)
996
997
998static int btf_show_obj_size_left(struct btf_show *show, void *data)
999{
1000 return show->obj.head + show->obj.size - data;
1001}
1002
1003
1004static bool btf_show_obj_is_safe(struct btf_show *show, void *data, int size)
1005{
1006 return data >= show->obj.data &&
1007 (data + size) < (show->obj.data + BTF_SHOW_OBJ_SAFE_SIZE);
1008}
1009
1010
1011
1012
1013
1014
1015
1016static void *__btf_show_obj_safe(struct btf_show *show, void *data, int size)
1017{
1018 if (btf_show_obj_is_safe(show, data, size))
1019 return show->obj.safe + (data - show->obj.data);
1020 return NULL;
1021}
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051static void *btf_show_obj_safe(struct btf_show *show,
1052 const struct btf_type *t,
1053 void *data)
1054{
1055 const struct btf_type *rt;
1056 int size_left, size;
1057 void *safe = NULL;
1058
1059 if (show->flags & BTF_SHOW_UNSAFE)
1060 return data;
1061
1062 rt = btf_resolve_size(show->btf, t, &size);
1063 if (IS_ERR(rt)) {
1064 show->state.status = PTR_ERR(rt);
1065 return NULL;
1066 }
1067
1068
1069
1070
1071
1072
1073 if (show->state.depth == 0) {
1074 show->obj.size = size;
1075 show->obj.head = data;
1076 } else {
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098 safe = __btf_show_obj_safe(show, data,
1099 min(size,
1100 BTF_SHOW_OBJ_BASE_TYPE_SIZE));
1101 }
1102
1103
1104
1105
1106
1107
1108 if (!safe) {
1109 size_left = btf_show_obj_size_left(show, data);
1110 if (size_left > BTF_SHOW_OBJ_SAFE_SIZE)
1111 size_left = BTF_SHOW_OBJ_SAFE_SIZE;
1112 show->state.status = copy_from_kernel_nofault(show->obj.safe,
1113 data, size_left);
1114 if (!show->state.status) {
1115 show->obj.data = data;
1116 safe = show->obj.safe;
1117 }
1118 }
1119
1120 return safe;
1121}
1122
1123
1124
1125
1126
1127static void *btf_show_start_type(struct btf_show *show,
1128 const struct btf_type *t,
1129 u32 type_id, void *data)
1130{
1131 show->state.type = t;
1132 show->state.type_id = type_id;
1133 show->state.name[0] = '\0';
1134
1135 return btf_show_obj_safe(show, t, data);
1136}
1137
1138static void btf_show_end_type(struct btf_show *show)
1139{
1140 show->state.type = NULL;
1141 show->state.type_id = 0;
1142 show->state.name[0] = '\0';
1143}
1144
1145static void *btf_show_start_aggr_type(struct btf_show *show,
1146 const struct btf_type *t,
1147 u32 type_id, void *data)
1148{
1149 void *safe_data = btf_show_start_type(show, t, type_id, data);
1150
1151 if (!safe_data)
1152 return safe_data;
1153
1154 btf_show(show, "%s%s%s", btf_show_indent(show),
1155 btf_show_name(show),
1156 btf_show_newline(show));
1157 show->state.depth++;
1158 return safe_data;
1159}
1160
1161static void btf_show_end_aggr_type(struct btf_show *show,
1162 const char *suffix)
1163{
1164 show->state.depth--;
1165 btf_show(show, "%s%s%s%s", btf_show_indent(show), suffix,
1166 btf_show_delim(show), btf_show_newline(show));
1167 btf_show_end_type(show);
1168}
1169
1170static void btf_show_start_member(struct btf_show *show,
1171 const struct btf_member *m)
1172{
1173 show->state.member = m;
1174}
1175
1176static void btf_show_start_array_member(struct btf_show *show)
1177{
1178 show->state.array_member = 1;
1179 btf_show_start_member(show, NULL);
1180}
1181
1182static void btf_show_end_member(struct btf_show *show)
1183{
1184 show->state.member = NULL;
1185}
1186
1187static void btf_show_end_array_member(struct btf_show *show)
1188{
1189 show->state.array_member = 0;
1190 btf_show_end_member(show);
1191}
1192
1193static void *btf_show_start_array_type(struct btf_show *show,
1194 const struct btf_type *t,
1195 u32 type_id,
1196 u16 array_encoding,
1197 void *data)
1198{
1199 show->state.array_encoding = array_encoding;
1200 show->state.array_terminated = 0;
1201 return btf_show_start_aggr_type(show, t, type_id, data);
1202}
1203
1204static void btf_show_end_array_type(struct btf_show *show)
1205{
1206 show->state.array_encoding = 0;
1207 show->state.array_terminated = 0;
1208 btf_show_end_aggr_type(show, "]");
1209}
1210
1211static void *btf_show_start_struct_type(struct btf_show *show,
1212 const struct btf_type *t,
1213 u32 type_id,
1214 void *data)
1215{
1216 return btf_show_start_aggr_type(show, t, type_id, data);
1217}
1218
1219static void btf_show_end_struct_type(struct btf_show *show)
1220{
1221 btf_show_end_aggr_type(show, "}");
1222}
1223
1224__printf(2, 3) static void __btf_verifier_log(struct bpf_verifier_log *log,
1225 const char *fmt, ...)
1226{
1227 va_list args;
1228
1229 va_start(args, fmt);
1230 bpf_verifier_vlog(log, fmt, args);
1231 va_end(args);
1232}
1233
1234__printf(2, 3) static void btf_verifier_log(struct btf_verifier_env *env,
1235 const char *fmt, ...)
1236{
1237 struct bpf_verifier_log *log = &env->log;
1238 va_list args;
1239
1240 if (!bpf_verifier_log_needed(log))
1241 return;
1242
1243 va_start(args, fmt);
1244 bpf_verifier_vlog(log, fmt, args);
1245 va_end(args);
1246}
1247
1248__printf(4, 5) static void __btf_verifier_log_type(struct btf_verifier_env *env,
1249 const struct btf_type *t,
1250 bool log_details,
1251 const char *fmt, ...)
1252{
1253 struct bpf_verifier_log *log = &env->log;
1254 u8 kind = BTF_INFO_KIND(t->info);
1255 struct btf *btf = env->btf;
1256 va_list args;
1257
1258 if (!bpf_verifier_log_needed(log))
1259 return;
1260
1261
1262
1263
1264
1265 if (log->level == BPF_LOG_KERNEL && !fmt)
1266 return;
1267
1268 __btf_verifier_log(log, "[%u] %s %s%s",
1269 env->log_type_id,
1270 btf_kind_str[kind],
1271 __btf_name_by_offset(btf, t->name_off),
1272 log_details ? " " : "");
1273
1274 if (log_details)
1275 btf_type_ops(t)->log_details(env, t);
1276
1277 if (fmt && *fmt) {
1278 __btf_verifier_log(log, " ");
1279 va_start(args, fmt);
1280 bpf_verifier_vlog(log, fmt, args);
1281 va_end(args);
1282 }
1283
1284 __btf_verifier_log(log, "\n");
1285}
1286
1287#define btf_verifier_log_type(env, t, ...) \
1288 __btf_verifier_log_type((env), (t), true, __VA_ARGS__)
1289#define btf_verifier_log_basic(env, t, ...) \
1290 __btf_verifier_log_type((env), (t), false, __VA_ARGS__)
1291
1292__printf(4, 5)
1293static void btf_verifier_log_member(struct btf_verifier_env *env,
1294 const struct btf_type *struct_type,
1295 const struct btf_member *member,
1296 const char *fmt, ...)
1297{
1298 struct bpf_verifier_log *log = &env->log;
1299 struct btf *btf = env->btf;
1300 va_list args;
1301
1302 if (!bpf_verifier_log_needed(log))
1303 return;
1304
1305 if (log->level == BPF_LOG_KERNEL && !fmt)
1306 return;
1307
1308
1309
1310
1311
1312
1313 if (env->phase != CHECK_META)
1314 btf_verifier_log_type(env, struct_type, NULL);
1315
1316 if (btf_type_kflag(struct_type))
1317 __btf_verifier_log(log,
1318 "\t%s type_id=%u bitfield_size=%u bits_offset=%u",
1319 __btf_name_by_offset(btf, member->name_off),
1320 member->type,
1321 BTF_MEMBER_BITFIELD_SIZE(member->offset),
1322 BTF_MEMBER_BIT_OFFSET(member->offset));
1323 else
1324 __btf_verifier_log(log, "\t%s type_id=%u bits_offset=%u",
1325 __btf_name_by_offset(btf, member->name_off),
1326 member->type, member->offset);
1327
1328 if (fmt && *fmt) {
1329 __btf_verifier_log(log, " ");
1330 va_start(args, fmt);
1331 bpf_verifier_vlog(log, fmt, args);
1332 va_end(args);
1333 }
1334
1335 __btf_verifier_log(log, "\n");
1336}
1337
1338__printf(4, 5)
1339static void btf_verifier_log_vsi(struct btf_verifier_env *env,
1340 const struct btf_type *datasec_type,
1341 const struct btf_var_secinfo *vsi,
1342 const char *fmt, ...)
1343{
1344 struct bpf_verifier_log *log = &env->log;
1345 va_list args;
1346
1347 if (!bpf_verifier_log_needed(log))
1348 return;
1349 if (log->level == BPF_LOG_KERNEL && !fmt)
1350 return;
1351 if (env->phase != CHECK_META)
1352 btf_verifier_log_type(env, datasec_type, NULL);
1353
1354 __btf_verifier_log(log, "\t type_id=%u offset=%u size=%u",
1355 vsi->type, vsi->offset, vsi->size);
1356 if (fmt && *fmt) {
1357 __btf_verifier_log(log, " ");
1358 va_start(args, fmt);
1359 bpf_verifier_vlog(log, fmt, args);
1360 va_end(args);
1361 }
1362
1363 __btf_verifier_log(log, "\n");
1364}
1365
1366static void btf_verifier_log_hdr(struct btf_verifier_env *env,
1367 u32 btf_data_size)
1368{
1369 struct bpf_verifier_log *log = &env->log;
1370 const struct btf *btf = env->btf;
1371 const struct btf_header *hdr;
1372
1373 if (!bpf_verifier_log_needed(log))
1374 return;
1375
1376 if (log->level == BPF_LOG_KERNEL)
1377 return;
1378 hdr = &btf->hdr;
1379 __btf_verifier_log(log, "magic: 0x%x\n", hdr->magic);
1380 __btf_verifier_log(log, "version: %u\n", hdr->version);
1381 __btf_verifier_log(log, "flags: 0x%x\n", hdr->flags);
1382 __btf_verifier_log(log, "hdr_len: %u\n", hdr->hdr_len);
1383 __btf_verifier_log(log, "type_off: %u\n", hdr->type_off);
1384 __btf_verifier_log(log, "type_len: %u\n", hdr->type_len);
1385 __btf_verifier_log(log, "str_off: %u\n", hdr->str_off);
1386 __btf_verifier_log(log, "str_len: %u\n", hdr->str_len);
1387 __btf_verifier_log(log, "btf_total_size: %u\n", btf_data_size);
1388}
1389
1390static int btf_add_type(struct btf_verifier_env *env, struct btf_type *t)
1391{
1392 struct btf *btf = env->btf;
1393
1394
1395
1396
1397
1398 if (btf->types_size - btf->nr_types < 2) {
1399
1400
1401 struct btf_type **new_types;
1402 u32 expand_by, new_size;
1403
1404 if (btf->types_size == BTF_MAX_TYPE) {
1405 btf_verifier_log(env, "Exceeded max num of types");
1406 return -E2BIG;
1407 }
1408
1409 expand_by = max_t(u32, btf->types_size >> 2, 16);
1410 new_size = min_t(u32, BTF_MAX_TYPE,
1411 btf->types_size + expand_by);
1412
1413 new_types = kvcalloc(new_size, sizeof(*new_types),
1414 GFP_KERNEL | __GFP_NOWARN);
1415 if (!new_types)
1416 return -ENOMEM;
1417
1418 if (btf->nr_types == 0)
1419 new_types[0] = &btf_void;
1420 else
1421 memcpy(new_types, btf->types,
1422 sizeof(*btf->types) * (btf->nr_types + 1));
1423
1424 kvfree(btf->types);
1425 btf->types = new_types;
1426 btf->types_size = new_size;
1427 }
1428
1429 btf->types[++(btf->nr_types)] = t;
1430
1431 return 0;
1432}
1433
1434static int btf_alloc_id(struct btf *btf)
1435{
1436 int id;
1437
1438 idr_preload(GFP_KERNEL);
1439 spin_lock_bh(&btf_idr_lock);
1440 id = idr_alloc_cyclic(&btf_idr, btf, 1, INT_MAX, GFP_ATOMIC);
1441 if (id > 0)
1442 btf->id = id;
1443 spin_unlock_bh(&btf_idr_lock);
1444 idr_preload_end();
1445
1446 if (WARN_ON_ONCE(!id))
1447 return -ENOSPC;
1448
1449 return id > 0 ? 0 : id;
1450}
1451
1452static void btf_free_id(struct btf *btf)
1453{
1454 unsigned long flags;
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465 spin_lock_irqsave(&btf_idr_lock, flags);
1466 idr_remove(&btf_idr, btf->id);
1467 spin_unlock_irqrestore(&btf_idr_lock, flags);
1468}
1469
1470static void btf_free(struct btf *btf)
1471{
1472 kvfree(btf->types);
1473 kvfree(btf->resolved_sizes);
1474 kvfree(btf->resolved_ids);
1475 kvfree(btf->data);
1476 kfree(btf);
1477}
1478
1479static void btf_free_rcu(struct rcu_head *rcu)
1480{
1481 struct btf *btf = container_of(rcu, struct btf, rcu);
1482
1483 btf_free(btf);
1484}
1485
1486void btf_put(struct btf *btf)
1487{
1488 if (btf && refcount_dec_and_test(&btf->refcnt)) {
1489 btf_free_id(btf);
1490 call_rcu(&btf->rcu, btf_free_rcu);
1491 }
1492}
1493
1494static int env_resolve_init(struct btf_verifier_env *env)
1495{
1496 struct btf *btf = env->btf;
1497 u32 nr_types = btf->nr_types;
1498 u32 *resolved_sizes = NULL;
1499 u32 *resolved_ids = NULL;
1500 u8 *visit_states = NULL;
1501
1502
1503 resolved_sizes = kvcalloc(nr_types + 1, sizeof(*resolved_sizes),
1504 GFP_KERNEL | __GFP_NOWARN);
1505 if (!resolved_sizes)
1506 goto nomem;
1507
1508 resolved_ids = kvcalloc(nr_types + 1, sizeof(*resolved_ids),
1509 GFP_KERNEL | __GFP_NOWARN);
1510 if (!resolved_ids)
1511 goto nomem;
1512
1513 visit_states = kvcalloc(nr_types + 1, sizeof(*visit_states),
1514 GFP_KERNEL | __GFP_NOWARN);
1515 if (!visit_states)
1516 goto nomem;
1517
1518 btf->resolved_sizes = resolved_sizes;
1519 btf->resolved_ids = resolved_ids;
1520 env->visit_states = visit_states;
1521
1522 return 0;
1523
1524nomem:
1525 kvfree(resolved_sizes);
1526 kvfree(resolved_ids);
1527 kvfree(visit_states);
1528 return -ENOMEM;
1529}
1530
1531static void btf_verifier_env_free(struct btf_verifier_env *env)
1532{
1533 kvfree(env->visit_states);
1534 kfree(env);
1535}
1536
1537static bool env_type_is_resolve_sink(const struct btf_verifier_env *env,
1538 const struct btf_type *next_type)
1539{
1540 switch (env->resolve_mode) {
1541 case RESOLVE_TBD:
1542
1543 return !btf_type_needs_resolve(next_type);
1544 case RESOLVE_PTR:
1545
1546
1547
1548 return !btf_type_is_modifier(next_type) &&
1549 !btf_type_is_ptr(next_type);
1550 case RESOLVE_STRUCT_OR_ARRAY:
1551
1552
1553
1554 return !btf_type_is_modifier(next_type) &&
1555 !btf_type_is_array(next_type) &&
1556 !btf_type_is_struct(next_type);
1557 default:
1558 BUG();
1559 }
1560}
1561
1562static bool env_type_is_resolved(const struct btf_verifier_env *env,
1563 u32 type_id)
1564{
1565 return env->visit_states[type_id] == RESOLVED;
1566}
1567
1568static int env_stack_push(struct btf_verifier_env *env,
1569 const struct btf_type *t, u32 type_id)
1570{
1571 struct resolve_vertex *v;
1572
1573 if (env->top_stack == MAX_RESOLVE_DEPTH)
1574 return -E2BIG;
1575
1576 if (env->visit_states[type_id] != NOT_VISITED)
1577 return -EEXIST;
1578
1579 env->visit_states[type_id] = VISITED;
1580
1581 v = &env->stack[env->top_stack++];
1582 v->t = t;
1583 v->type_id = type_id;
1584 v->next_member = 0;
1585
1586 if (env->resolve_mode == RESOLVE_TBD) {
1587 if (btf_type_is_ptr(t))
1588 env->resolve_mode = RESOLVE_PTR;
1589 else if (btf_type_is_struct(t) || btf_type_is_array(t))
1590 env->resolve_mode = RESOLVE_STRUCT_OR_ARRAY;
1591 }
1592
1593 return 0;
1594}
1595
1596static void env_stack_set_next_member(struct btf_verifier_env *env,
1597 u16 next_member)
1598{
1599 env->stack[env->top_stack - 1].next_member = next_member;
1600}
1601
1602static void env_stack_pop_resolved(struct btf_verifier_env *env,
1603 u32 resolved_type_id,
1604 u32 resolved_size)
1605{
1606 u32 type_id = env->stack[--(env->top_stack)].type_id;
1607 struct btf *btf = env->btf;
1608
1609 btf->resolved_sizes[type_id] = resolved_size;
1610 btf->resolved_ids[type_id] = resolved_type_id;
1611 env->visit_states[type_id] = RESOLVED;
1612}
1613
1614static const struct resolve_vertex *env_stack_peak(struct btf_verifier_env *env)
1615{
1616 return env->top_stack ? &env->stack[env->top_stack - 1] : NULL;
1617}
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639static const struct btf_type *
1640__btf_resolve_size(const struct btf *btf, const struct btf_type *type,
1641 u32 *type_size, const struct btf_type **elem_type,
1642 u32 *elem_id, u32 *total_nelems, u32 *type_id)
1643{
1644 const struct btf_type *array_type = NULL;
1645 const struct btf_array *array = NULL;
1646 u32 i, size, nelems = 1, id = 0;
1647
1648 for (i = 0; i < MAX_RESOLVE_DEPTH; i++) {
1649 switch (BTF_INFO_KIND(type->info)) {
1650
1651 case BTF_KIND_INT:
1652 case BTF_KIND_STRUCT:
1653 case BTF_KIND_UNION:
1654 case BTF_KIND_ENUM:
1655 size = type->size;
1656 goto resolved;
1657
1658 case BTF_KIND_PTR:
1659 size = sizeof(void *);
1660 goto resolved;
1661
1662
1663 case BTF_KIND_TYPEDEF:
1664 case BTF_KIND_VOLATILE:
1665 case BTF_KIND_CONST:
1666 case BTF_KIND_RESTRICT:
1667 id = type->type;
1668 type = btf_type_by_id(btf, type->type);
1669 break;
1670
1671 case BTF_KIND_ARRAY:
1672 if (!array_type)
1673 array_type = type;
1674 array = btf_type_array(type);
1675 if (nelems && array->nelems > U32_MAX / nelems)
1676 return ERR_PTR(-EINVAL);
1677 nelems *= array->nelems;
1678 type = btf_type_by_id(btf, array->type);
1679 break;
1680
1681
1682 default:
1683 return ERR_PTR(-EINVAL);
1684 }
1685 }
1686
1687 return ERR_PTR(-EINVAL);
1688
1689resolved:
1690 if (nelems && size > U32_MAX / nelems)
1691 return ERR_PTR(-EINVAL);
1692
1693 *type_size = nelems * size;
1694 if (total_nelems)
1695 *total_nelems = nelems;
1696 if (elem_type)
1697 *elem_type = type;
1698 if (elem_id)
1699 *elem_id = array ? array->type : 0;
1700 if (type_id && id)
1701 *type_id = id;
1702
1703 return array_type ? : type;
1704}
1705
1706const struct btf_type *
1707btf_resolve_size(const struct btf *btf, const struct btf_type *type,
1708 u32 *type_size)
1709{
1710 return __btf_resolve_size(btf, type, type_size, NULL, NULL, NULL, NULL);
1711}
1712
1713
1714static const struct btf_type *btf_type_id_resolve(const struct btf *btf,
1715 u32 *type_id)
1716{
1717 *type_id = btf->resolved_ids[*type_id];
1718 return btf_type_by_id(btf, *type_id);
1719}
1720
1721const struct btf_type *btf_type_id_size(const struct btf *btf,
1722 u32 *type_id, u32 *ret_size)
1723{
1724 const struct btf_type *size_type;
1725 u32 size_type_id = *type_id;
1726 u32 size = 0;
1727
1728 size_type = btf_type_by_id(btf, size_type_id);
1729 if (btf_type_nosize_or_null(size_type))
1730 return NULL;
1731
1732 if (btf_type_has_size(size_type)) {
1733 size = size_type->size;
1734 } else if (btf_type_is_array(size_type)) {
1735 size = btf->resolved_sizes[size_type_id];
1736 } else if (btf_type_is_ptr(size_type)) {
1737 size = sizeof(void *);
1738 } else {
1739 if (WARN_ON_ONCE(!btf_type_is_modifier(size_type) &&
1740 !btf_type_is_var(size_type)))
1741 return NULL;
1742
1743 size_type_id = btf->resolved_ids[size_type_id];
1744 size_type = btf_type_by_id(btf, size_type_id);
1745 if (btf_type_nosize_or_null(size_type))
1746 return NULL;
1747 else if (btf_type_has_size(size_type))
1748 size = size_type->size;
1749 else if (btf_type_is_array(size_type))
1750 size = btf->resolved_sizes[size_type_id];
1751 else if (btf_type_is_ptr(size_type))
1752 size = sizeof(void *);
1753 else
1754 return NULL;
1755 }
1756
1757 *type_id = size_type_id;
1758 if (ret_size)
1759 *ret_size = size;
1760
1761 return size_type;
1762}
1763
1764static int btf_df_check_member(struct btf_verifier_env *env,
1765 const struct btf_type *struct_type,
1766 const struct btf_member *member,
1767 const struct btf_type *member_type)
1768{
1769 btf_verifier_log_basic(env, struct_type,
1770 "Unsupported check_member");
1771 return -EINVAL;
1772}
1773
1774static int btf_df_check_kflag_member(struct btf_verifier_env *env,
1775 const struct btf_type *struct_type,
1776 const struct btf_member *member,
1777 const struct btf_type *member_type)
1778{
1779 btf_verifier_log_basic(env, struct_type,
1780 "Unsupported check_kflag_member");
1781 return -EINVAL;
1782}
1783
1784
1785
1786
1787static int btf_generic_check_kflag_member(struct btf_verifier_env *env,
1788 const struct btf_type *struct_type,
1789 const struct btf_member *member,
1790 const struct btf_type *member_type)
1791{
1792 if (BTF_MEMBER_BITFIELD_SIZE(member->offset)) {
1793 btf_verifier_log_member(env, struct_type, member,
1794 "Invalid member bitfield_size");
1795 return -EINVAL;
1796 }
1797
1798
1799
1800
1801 return btf_type_ops(member_type)->check_member(env, struct_type,
1802 member,
1803 member_type);
1804}
1805
1806static int btf_df_resolve(struct btf_verifier_env *env,
1807 const struct resolve_vertex *v)
1808{
1809 btf_verifier_log_basic(env, v->t, "Unsupported resolve");
1810 return -EINVAL;
1811}
1812
1813static void btf_df_show(const struct btf *btf, const struct btf_type *t,
1814 u32 type_id, void *data, u8 bits_offsets,
1815 struct btf_show *show)
1816{
1817 btf_show(show, "<unsupported kind:%u>", BTF_INFO_KIND(t->info));
1818}
1819
1820static int btf_int_check_member(struct btf_verifier_env *env,
1821 const struct btf_type *struct_type,
1822 const struct btf_member *member,
1823 const struct btf_type *member_type)
1824{
1825 u32 int_data = btf_type_int(member_type);
1826 u32 struct_bits_off = member->offset;
1827 u32 struct_size = struct_type->size;
1828 u32 nr_copy_bits;
1829 u32 bytes_offset;
1830
1831 if (U32_MAX - struct_bits_off < BTF_INT_OFFSET(int_data)) {
1832 btf_verifier_log_member(env, struct_type, member,
1833 "bits_offset exceeds U32_MAX");
1834 return -EINVAL;
1835 }
1836
1837 struct_bits_off += BTF_INT_OFFSET(int_data);
1838 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
1839 nr_copy_bits = BTF_INT_BITS(int_data) +
1840 BITS_PER_BYTE_MASKED(struct_bits_off);
1841
1842 if (nr_copy_bits > BITS_PER_U128) {
1843 btf_verifier_log_member(env, struct_type, member,
1844 "nr_copy_bits exceeds 128");
1845 return -EINVAL;
1846 }
1847
1848 if (struct_size < bytes_offset ||
1849 struct_size - bytes_offset < BITS_ROUNDUP_BYTES(nr_copy_bits)) {
1850 btf_verifier_log_member(env, struct_type, member,
1851 "Member exceeds struct_size");
1852 return -EINVAL;
1853 }
1854
1855 return 0;
1856}
1857
1858static int btf_int_check_kflag_member(struct btf_verifier_env *env,
1859 const struct btf_type *struct_type,
1860 const struct btf_member *member,
1861 const struct btf_type *member_type)
1862{
1863 u32 struct_bits_off, nr_bits, nr_int_data_bits, bytes_offset;
1864 u32 int_data = btf_type_int(member_type);
1865 u32 struct_size = struct_type->size;
1866 u32 nr_copy_bits;
1867
1868
1869 if (!btf_type_int_is_regular(member_type)) {
1870 btf_verifier_log_member(env, struct_type, member,
1871 "Invalid member base type");
1872 return -EINVAL;
1873 }
1874
1875
1876 nr_bits = BTF_MEMBER_BITFIELD_SIZE(member->offset);
1877 struct_bits_off = BTF_MEMBER_BIT_OFFSET(member->offset);
1878 nr_int_data_bits = BTF_INT_BITS(int_data);
1879 if (!nr_bits) {
1880
1881
1882
1883 if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
1884 btf_verifier_log_member(env, struct_type, member,
1885 "Invalid member offset");
1886 return -EINVAL;
1887 }
1888
1889 nr_bits = nr_int_data_bits;
1890 } else if (nr_bits > nr_int_data_bits) {
1891 btf_verifier_log_member(env, struct_type, member,
1892 "Invalid member bitfield_size");
1893 return -EINVAL;
1894 }
1895
1896 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
1897 nr_copy_bits = nr_bits + BITS_PER_BYTE_MASKED(struct_bits_off);
1898 if (nr_copy_bits > BITS_PER_U128) {
1899 btf_verifier_log_member(env, struct_type, member,
1900 "nr_copy_bits exceeds 128");
1901 return -EINVAL;
1902 }
1903
1904 if (struct_size < bytes_offset ||
1905 struct_size - bytes_offset < BITS_ROUNDUP_BYTES(nr_copy_bits)) {
1906 btf_verifier_log_member(env, struct_type, member,
1907 "Member exceeds struct_size");
1908 return -EINVAL;
1909 }
1910
1911 return 0;
1912}
1913
1914static s32 btf_int_check_meta(struct btf_verifier_env *env,
1915 const struct btf_type *t,
1916 u32 meta_left)
1917{
1918 u32 int_data, nr_bits, meta_needed = sizeof(int_data);
1919 u16 encoding;
1920
1921 if (meta_left < meta_needed) {
1922 btf_verifier_log_basic(env, t,
1923 "meta_left:%u meta_needed:%u",
1924 meta_left, meta_needed);
1925 return -EINVAL;
1926 }
1927
1928 if (btf_type_vlen(t)) {
1929 btf_verifier_log_type(env, t, "vlen != 0");
1930 return -EINVAL;
1931 }
1932
1933 if (btf_type_kflag(t)) {
1934 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
1935 return -EINVAL;
1936 }
1937
1938 int_data = btf_type_int(t);
1939 if (int_data & ~BTF_INT_MASK) {
1940 btf_verifier_log_basic(env, t, "Invalid int_data:%x",
1941 int_data);
1942 return -EINVAL;
1943 }
1944
1945 nr_bits = BTF_INT_BITS(int_data) + BTF_INT_OFFSET(int_data);
1946
1947 if (nr_bits > BITS_PER_U128) {
1948 btf_verifier_log_type(env, t, "nr_bits exceeds %zu",
1949 BITS_PER_U128);
1950 return -EINVAL;
1951 }
1952
1953 if (BITS_ROUNDUP_BYTES(nr_bits) > t->size) {
1954 btf_verifier_log_type(env, t, "nr_bits exceeds type_size");
1955 return -EINVAL;
1956 }
1957
1958
1959
1960
1961
1962
1963
1964 encoding = BTF_INT_ENCODING(int_data);
1965 if (encoding &&
1966 encoding != BTF_INT_SIGNED &&
1967 encoding != BTF_INT_CHAR &&
1968 encoding != BTF_INT_BOOL) {
1969 btf_verifier_log_type(env, t, "Unsupported encoding");
1970 return -ENOTSUPP;
1971 }
1972
1973 btf_verifier_log_type(env, t, NULL);
1974
1975 return meta_needed;
1976}
1977
1978static void btf_int_log(struct btf_verifier_env *env,
1979 const struct btf_type *t)
1980{
1981 int int_data = btf_type_int(t);
1982
1983 btf_verifier_log(env,
1984 "size=%u bits_offset=%u nr_bits=%u encoding=%s",
1985 t->size, BTF_INT_OFFSET(int_data),
1986 BTF_INT_BITS(int_data),
1987 btf_int_encoding_str(BTF_INT_ENCODING(int_data)));
1988}
1989
1990static void btf_int128_print(struct btf_show *show, void *data)
1991{
1992
1993
1994
1995
1996
1997
1998
1999 u64 upper_num, lower_num;
2000
2001#ifdef __BIG_ENDIAN_BITFIELD
2002 upper_num = *(u64 *)data;
2003 lower_num = *(u64 *)(data + 8);
2004#else
2005 upper_num = *(u64 *)(data + 8);
2006 lower_num = *(u64 *)data;
2007#endif
2008 if (upper_num == 0)
2009 btf_show_type_value(show, "0x%llx", lower_num);
2010 else
2011 btf_show_type_values(show, "0x%llx%016llx", upper_num,
2012 lower_num);
2013}
2014
2015static void btf_int128_shift(u64 *print_num, u16 left_shift_bits,
2016 u16 right_shift_bits)
2017{
2018 u64 upper_num, lower_num;
2019
2020#ifdef __BIG_ENDIAN_BITFIELD
2021 upper_num = print_num[0];
2022 lower_num = print_num[1];
2023#else
2024 upper_num = print_num[1];
2025 lower_num = print_num[0];
2026#endif
2027
2028
2029 if (left_shift_bits >= 64) {
2030 upper_num = lower_num << (left_shift_bits - 64);
2031 lower_num = 0;
2032 } else {
2033 upper_num = (upper_num << left_shift_bits) |
2034 (lower_num >> (64 - left_shift_bits));
2035 lower_num = lower_num << left_shift_bits;
2036 }
2037
2038 if (right_shift_bits >= 64) {
2039 lower_num = upper_num >> (right_shift_bits - 64);
2040 upper_num = 0;
2041 } else {
2042 lower_num = (lower_num >> right_shift_bits) |
2043 (upper_num << (64 - right_shift_bits));
2044 upper_num = upper_num >> right_shift_bits;
2045 }
2046
2047#ifdef __BIG_ENDIAN_BITFIELD
2048 print_num[0] = upper_num;
2049 print_num[1] = lower_num;
2050#else
2051 print_num[0] = lower_num;
2052 print_num[1] = upper_num;
2053#endif
2054}
2055
2056static void btf_bitfield_show(void *data, u8 bits_offset,
2057 u8 nr_bits, struct btf_show *show)
2058{
2059 u16 left_shift_bits, right_shift_bits;
2060 u8 nr_copy_bytes;
2061 u8 nr_copy_bits;
2062 u64 print_num[2] = {};
2063
2064 nr_copy_bits = nr_bits + bits_offset;
2065 nr_copy_bytes = BITS_ROUNDUP_BYTES(nr_copy_bits);
2066
2067 memcpy(print_num, data, nr_copy_bytes);
2068
2069#ifdef __BIG_ENDIAN_BITFIELD
2070 left_shift_bits = bits_offset;
2071#else
2072 left_shift_bits = BITS_PER_U128 - nr_copy_bits;
2073#endif
2074 right_shift_bits = BITS_PER_U128 - nr_bits;
2075
2076 btf_int128_shift(print_num, left_shift_bits, right_shift_bits);
2077 btf_int128_print(show, print_num);
2078}
2079
2080
2081static void btf_int_bits_show(const struct btf *btf,
2082 const struct btf_type *t,
2083 void *data, u8 bits_offset,
2084 struct btf_show *show)
2085{
2086 u32 int_data = btf_type_int(t);
2087 u8 nr_bits = BTF_INT_BITS(int_data);
2088 u8 total_bits_offset;
2089
2090
2091
2092
2093
2094 total_bits_offset = bits_offset + BTF_INT_OFFSET(int_data);
2095 data += BITS_ROUNDDOWN_BYTES(total_bits_offset);
2096 bits_offset = BITS_PER_BYTE_MASKED(total_bits_offset);
2097 btf_bitfield_show(data, bits_offset, nr_bits, show);
2098}
2099
2100static void btf_int_show(const struct btf *btf, const struct btf_type *t,
2101 u32 type_id, void *data, u8 bits_offset,
2102 struct btf_show *show)
2103{
2104 u32 int_data = btf_type_int(t);
2105 u8 encoding = BTF_INT_ENCODING(int_data);
2106 bool sign = encoding & BTF_INT_SIGNED;
2107 u8 nr_bits = BTF_INT_BITS(int_data);
2108 void *safe_data;
2109
2110 safe_data = btf_show_start_type(show, t, type_id, data);
2111 if (!safe_data)
2112 return;
2113
2114 if (bits_offset || BTF_INT_OFFSET(int_data) ||
2115 BITS_PER_BYTE_MASKED(nr_bits)) {
2116 btf_int_bits_show(btf, t, safe_data, bits_offset, show);
2117 goto out;
2118 }
2119
2120 switch (nr_bits) {
2121 case 128:
2122 btf_int128_print(show, safe_data);
2123 break;
2124 case 64:
2125 if (sign)
2126 btf_show_type_value(show, "%lld", *(s64 *)safe_data);
2127 else
2128 btf_show_type_value(show, "%llu", *(u64 *)safe_data);
2129 break;
2130 case 32:
2131 if (sign)
2132 btf_show_type_value(show, "%d", *(s32 *)safe_data);
2133 else
2134 btf_show_type_value(show, "%u", *(u32 *)safe_data);
2135 break;
2136 case 16:
2137 if (sign)
2138 btf_show_type_value(show, "%d", *(s16 *)safe_data);
2139 else
2140 btf_show_type_value(show, "%u", *(u16 *)safe_data);
2141 break;
2142 case 8:
2143 if (show->state.array_encoding == BTF_INT_CHAR) {
2144
2145 if (show->state.array_terminated)
2146 break;
2147 if (*(char *)data == '\0') {
2148 show->state.array_terminated = 1;
2149 break;
2150 }
2151 if (isprint(*(char *)data)) {
2152 btf_show_type_value(show, "'%c'",
2153 *(char *)safe_data);
2154 break;
2155 }
2156 }
2157 if (sign)
2158 btf_show_type_value(show, "%d", *(s8 *)safe_data);
2159 else
2160 btf_show_type_value(show, "%u", *(u8 *)safe_data);
2161 break;
2162 default:
2163 btf_int_bits_show(btf, t, safe_data, bits_offset, show);
2164 break;
2165 }
2166out:
2167 btf_show_end_type(show);
2168}
2169
2170static const struct btf_kind_operations int_ops = {
2171 .check_meta = btf_int_check_meta,
2172 .resolve = btf_df_resolve,
2173 .check_member = btf_int_check_member,
2174 .check_kflag_member = btf_int_check_kflag_member,
2175 .log_details = btf_int_log,
2176 .show = btf_int_show,
2177};
2178
2179static int btf_modifier_check_member(struct btf_verifier_env *env,
2180 const struct btf_type *struct_type,
2181 const struct btf_member *member,
2182 const struct btf_type *member_type)
2183{
2184 const struct btf_type *resolved_type;
2185 u32 resolved_type_id = member->type;
2186 struct btf_member resolved_member;
2187 struct btf *btf = env->btf;
2188
2189 resolved_type = btf_type_id_size(btf, &resolved_type_id, NULL);
2190 if (!resolved_type) {
2191 btf_verifier_log_member(env, struct_type, member,
2192 "Invalid member");
2193 return -EINVAL;
2194 }
2195
2196 resolved_member = *member;
2197 resolved_member.type = resolved_type_id;
2198
2199 return btf_type_ops(resolved_type)->check_member(env, struct_type,
2200 &resolved_member,
2201 resolved_type);
2202}
2203
2204static int btf_modifier_check_kflag_member(struct btf_verifier_env *env,
2205 const struct btf_type *struct_type,
2206 const struct btf_member *member,
2207 const struct btf_type *member_type)
2208{
2209 const struct btf_type *resolved_type;
2210 u32 resolved_type_id = member->type;
2211 struct btf_member resolved_member;
2212 struct btf *btf = env->btf;
2213
2214 resolved_type = btf_type_id_size(btf, &resolved_type_id, NULL);
2215 if (!resolved_type) {
2216 btf_verifier_log_member(env, struct_type, member,
2217 "Invalid member");
2218 return -EINVAL;
2219 }
2220
2221 resolved_member = *member;
2222 resolved_member.type = resolved_type_id;
2223
2224 return btf_type_ops(resolved_type)->check_kflag_member(env, struct_type,
2225 &resolved_member,
2226 resolved_type);
2227}
2228
2229static int btf_ptr_check_member(struct btf_verifier_env *env,
2230 const struct btf_type *struct_type,
2231 const struct btf_member *member,
2232 const struct btf_type *member_type)
2233{
2234 u32 struct_size, struct_bits_off, bytes_offset;
2235
2236 struct_size = struct_type->size;
2237 struct_bits_off = member->offset;
2238 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
2239
2240 if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
2241 btf_verifier_log_member(env, struct_type, member,
2242 "Member is not byte aligned");
2243 return -EINVAL;
2244 }
2245
2246 if (struct_size - bytes_offset < sizeof(void *)) {
2247 btf_verifier_log_member(env, struct_type, member,
2248 "Member exceeds struct_size");
2249 return -EINVAL;
2250 }
2251
2252 return 0;
2253}
2254
2255static int btf_ref_type_check_meta(struct btf_verifier_env *env,
2256 const struct btf_type *t,
2257 u32 meta_left)
2258{
2259 if (btf_type_vlen(t)) {
2260 btf_verifier_log_type(env, t, "vlen != 0");
2261 return -EINVAL;
2262 }
2263
2264 if (btf_type_kflag(t)) {
2265 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
2266 return -EINVAL;
2267 }
2268
2269 if (!BTF_TYPE_ID_VALID(t->type)) {
2270 btf_verifier_log_type(env, t, "Invalid type_id");
2271 return -EINVAL;
2272 }
2273
2274
2275
2276
2277 if (BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF) {
2278 if (!t->name_off ||
2279 !btf_name_valid_identifier(env->btf, t->name_off)) {
2280 btf_verifier_log_type(env, t, "Invalid name");
2281 return -EINVAL;
2282 }
2283 } else {
2284 if (t->name_off) {
2285 btf_verifier_log_type(env, t, "Invalid name");
2286 return -EINVAL;
2287 }
2288 }
2289
2290 btf_verifier_log_type(env, t, NULL);
2291
2292 return 0;
2293}
2294
2295static int btf_modifier_resolve(struct btf_verifier_env *env,
2296 const struct resolve_vertex *v)
2297{
2298 const struct btf_type *t = v->t;
2299 const struct btf_type *next_type;
2300 u32 next_type_id = t->type;
2301 struct btf *btf = env->btf;
2302
2303 next_type = btf_type_by_id(btf, next_type_id);
2304 if (!next_type || btf_type_is_resolve_source_only(next_type)) {
2305 btf_verifier_log_type(env, v->t, "Invalid type_id");
2306 return -EINVAL;
2307 }
2308
2309 if (!env_type_is_resolve_sink(env, next_type) &&
2310 !env_type_is_resolved(env, next_type_id))
2311 return env_stack_push(env, next_type, next_type_id);
2312
2313
2314
2315
2316
2317
2318
2319 if (!btf_type_id_size(btf, &next_type_id, NULL)) {
2320 if (env_type_is_resolved(env, next_type_id))
2321 next_type = btf_type_id_resolve(btf, &next_type_id);
2322
2323
2324 if (!btf_type_is_void(next_type) &&
2325 !btf_type_is_fwd(next_type) &&
2326 !btf_type_is_func_proto(next_type)) {
2327 btf_verifier_log_type(env, v->t, "Invalid type_id");
2328 return -EINVAL;
2329 }
2330 }
2331
2332 env_stack_pop_resolved(env, next_type_id, 0);
2333
2334 return 0;
2335}
2336
2337static int btf_var_resolve(struct btf_verifier_env *env,
2338 const struct resolve_vertex *v)
2339{
2340 const struct btf_type *next_type;
2341 const struct btf_type *t = v->t;
2342 u32 next_type_id = t->type;
2343 struct btf *btf = env->btf;
2344
2345 next_type = btf_type_by_id(btf, next_type_id);
2346 if (!next_type || btf_type_is_resolve_source_only(next_type)) {
2347 btf_verifier_log_type(env, v->t, "Invalid type_id");
2348 return -EINVAL;
2349 }
2350
2351 if (!env_type_is_resolve_sink(env, next_type) &&
2352 !env_type_is_resolved(env, next_type_id))
2353 return env_stack_push(env, next_type, next_type_id);
2354
2355 if (btf_type_is_modifier(next_type)) {
2356 const struct btf_type *resolved_type;
2357 u32 resolved_type_id;
2358
2359 resolved_type_id = next_type_id;
2360 resolved_type = btf_type_id_resolve(btf, &resolved_type_id);
2361
2362 if (btf_type_is_ptr(resolved_type) &&
2363 !env_type_is_resolve_sink(env, resolved_type) &&
2364 !env_type_is_resolved(env, resolved_type_id))
2365 return env_stack_push(env, resolved_type,
2366 resolved_type_id);
2367 }
2368
2369
2370
2371
2372
2373 if (!btf_type_id_size(btf, &next_type_id, NULL)) {
2374 btf_verifier_log_type(env, v->t, "Invalid type_id");
2375 return -EINVAL;
2376 }
2377
2378 env_stack_pop_resolved(env, next_type_id, 0);
2379
2380 return 0;
2381}
2382
2383static int btf_ptr_resolve(struct btf_verifier_env *env,
2384 const struct resolve_vertex *v)
2385{
2386 const struct btf_type *next_type;
2387 const struct btf_type *t = v->t;
2388 u32 next_type_id = t->type;
2389 struct btf *btf = env->btf;
2390
2391 next_type = btf_type_by_id(btf, next_type_id);
2392 if (!next_type || btf_type_is_resolve_source_only(next_type)) {
2393 btf_verifier_log_type(env, v->t, "Invalid type_id");
2394 return -EINVAL;
2395 }
2396
2397 if (!env_type_is_resolve_sink(env, next_type) &&
2398 !env_type_is_resolved(env, next_type_id))
2399 return env_stack_push(env, next_type, next_type_id);
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409 if (btf_type_is_modifier(next_type)) {
2410 const struct btf_type *resolved_type;
2411 u32 resolved_type_id;
2412
2413 resolved_type_id = next_type_id;
2414 resolved_type = btf_type_id_resolve(btf, &resolved_type_id);
2415
2416 if (btf_type_is_ptr(resolved_type) &&
2417 !env_type_is_resolve_sink(env, resolved_type) &&
2418 !env_type_is_resolved(env, resolved_type_id))
2419 return env_stack_push(env, resolved_type,
2420 resolved_type_id);
2421 }
2422
2423 if (!btf_type_id_size(btf, &next_type_id, NULL)) {
2424 if (env_type_is_resolved(env, next_type_id))
2425 next_type = btf_type_id_resolve(btf, &next_type_id);
2426
2427 if (!btf_type_is_void(next_type) &&
2428 !btf_type_is_fwd(next_type) &&
2429 !btf_type_is_func_proto(next_type)) {
2430 btf_verifier_log_type(env, v->t, "Invalid type_id");
2431 return -EINVAL;
2432 }
2433 }
2434
2435 env_stack_pop_resolved(env, next_type_id, 0);
2436
2437 return 0;
2438}
2439
2440static void btf_modifier_show(const struct btf *btf,
2441 const struct btf_type *t,
2442 u32 type_id, void *data,
2443 u8 bits_offset, struct btf_show *show)
2444{
2445 if (btf->resolved_ids)
2446 t = btf_type_id_resolve(btf, &type_id);
2447 else
2448 t = btf_type_skip_modifiers(btf, type_id, NULL);
2449
2450 btf_type_ops(t)->show(btf, t, type_id, data, bits_offset, show);
2451}
2452
2453static void btf_var_show(const struct btf *btf, const struct btf_type *t,
2454 u32 type_id, void *data, u8 bits_offset,
2455 struct btf_show *show)
2456{
2457 t = btf_type_id_resolve(btf, &type_id);
2458
2459 btf_type_ops(t)->show(btf, t, type_id, data, bits_offset, show);
2460}
2461
2462static void btf_ptr_show(const struct btf *btf, const struct btf_type *t,
2463 u32 type_id, void *data, u8 bits_offset,
2464 struct btf_show *show)
2465{
2466 void *safe_data;
2467
2468 safe_data = btf_show_start_type(show, t, type_id, data);
2469 if (!safe_data)
2470 return;
2471
2472
2473 if (show->flags & BTF_SHOW_PTR_RAW)
2474 btf_show_type_value(show, "0x%px", *(void **)safe_data);
2475 else
2476 btf_show_type_value(show, "0x%p", *(void **)safe_data);
2477 btf_show_end_type(show);
2478}
2479
2480static void btf_ref_type_log(struct btf_verifier_env *env,
2481 const struct btf_type *t)
2482{
2483 btf_verifier_log(env, "type_id=%u", t->type);
2484}
2485
2486static struct btf_kind_operations modifier_ops = {
2487 .check_meta = btf_ref_type_check_meta,
2488 .resolve = btf_modifier_resolve,
2489 .check_member = btf_modifier_check_member,
2490 .check_kflag_member = btf_modifier_check_kflag_member,
2491 .log_details = btf_ref_type_log,
2492 .show = btf_modifier_show,
2493};
2494
2495static struct btf_kind_operations ptr_ops = {
2496 .check_meta = btf_ref_type_check_meta,
2497 .resolve = btf_ptr_resolve,
2498 .check_member = btf_ptr_check_member,
2499 .check_kflag_member = btf_generic_check_kflag_member,
2500 .log_details = btf_ref_type_log,
2501 .show = btf_ptr_show,
2502};
2503
2504static s32 btf_fwd_check_meta(struct btf_verifier_env *env,
2505 const struct btf_type *t,
2506 u32 meta_left)
2507{
2508 if (btf_type_vlen(t)) {
2509 btf_verifier_log_type(env, t, "vlen != 0");
2510 return -EINVAL;
2511 }
2512
2513 if (t->type) {
2514 btf_verifier_log_type(env, t, "type != 0");
2515 return -EINVAL;
2516 }
2517
2518
2519 if (!t->name_off ||
2520 !btf_name_valid_identifier(env->btf, t->name_off)) {
2521 btf_verifier_log_type(env, t, "Invalid name");
2522 return -EINVAL;
2523 }
2524
2525 btf_verifier_log_type(env, t, NULL);
2526
2527 return 0;
2528}
2529
2530static void btf_fwd_type_log(struct btf_verifier_env *env,
2531 const struct btf_type *t)
2532{
2533 btf_verifier_log(env, "%s", btf_type_kflag(t) ? "union" : "struct");
2534}
2535
2536static struct btf_kind_operations fwd_ops = {
2537 .check_meta = btf_fwd_check_meta,
2538 .resolve = btf_df_resolve,
2539 .check_member = btf_df_check_member,
2540 .check_kflag_member = btf_df_check_kflag_member,
2541 .log_details = btf_fwd_type_log,
2542 .show = btf_df_show,
2543};
2544
2545static int btf_array_check_member(struct btf_verifier_env *env,
2546 const struct btf_type *struct_type,
2547 const struct btf_member *member,
2548 const struct btf_type *member_type)
2549{
2550 u32 struct_bits_off = member->offset;
2551 u32 struct_size, bytes_offset;
2552 u32 array_type_id, array_size;
2553 struct btf *btf = env->btf;
2554
2555 if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
2556 btf_verifier_log_member(env, struct_type, member,
2557 "Member is not byte aligned");
2558 return -EINVAL;
2559 }
2560
2561 array_type_id = member->type;
2562 btf_type_id_size(btf, &array_type_id, &array_size);
2563 struct_size = struct_type->size;
2564 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
2565 if (struct_size - bytes_offset < array_size) {
2566 btf_verifier_log_member(env, struct_type, member,
2567 "Member exceeds struct_size");
2568 return -EINVAL;
2569 }
2570
2571 return 0;
2572}
2573
2574static s32 btf_array_check_meta(struct btf_verifier_env *env,
2575 const struct btf_type *t,
2576 u32 meta_left)
2577{
2578 const struct btf_array *array = btf_type_array(t);
2579 u32 meta_needed = sizeof(*array);
2580
2581 if (meta_left < meta_needed) {
2582 btf_verifier_log_basic(env, t,
2583 "meta_left:%u meta_needed:%u",
2584 meta_left, meta_needed);
2585 return -EINVAL;
2586 }
2587
2588
2589 if (t->name_off) {
2590 btf_verifier_log_type(env, t, "Invalid name");
2591 return -EINVAL;
2592 }
2593
2594 if (btf_type_vlen(t)) {
2595 btf_verifier_log_type(env, t, "vlen != 0");
2596 return -EINVAL;
2597 }
2598
2599 if (btf_type_kflag(t)) {
2600 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
2601 return -EINVAL;
2602 }
2603
2604 if (t->size) {
2605 btf_verifier_log_type(env, t, "size != 0");
2606 return -EINVAL;
2607 }
2608
2609
2610
2611
2612 if (!array->type || !BTF_TYPE_ID_VALID(array->type)) {
2613 btf_verifier_log_type(env, t, "Invalid elem");
2614 return -EINVAL;
2615 }
2616
2617 if (!array->index_type || !BTF_TYPE_ID_VALID(array->index_type)) {
2618 btf_verifier_log_type(env, t, "Invalid index");
2619 return -EINVAL;
2620 }
2621
2622 btf_verifier_log_type(env, t, NULL);
2623
2624 return meta_needed;
2625}
2626
2627static int btf_array_resolve(struct btf_verifier_env *env,
2628 const struct resolve_vertex *v)
2629{
2630 const struct btf_array *array = btf_type_array(v->t);
2631 const struct btf_type *elem_type, *index_type;
2632 u32 elem_type_id, index_type_id;
2633 struct btf *btf = env->btf;
2634 u32 elem_size;
2635
2636
2637 index_type_id = array->index_type;
2638 index_type = btf_type_by_id(btf, index_type_id);
2639 if (btf_type_nosize_or_null(index_type) ||
2640 btf_type_is_resolve_source_only(index_type)) {
2641 btf_verifier_log_type(env, v->t, "Invalid index");
2642 return -EINVAL;
2643 }
2644
2645 if (!env_type_is_resolve_sink(env, index_type) &&
2646 !env_type_is_resolved(env, index_type_id))
2647 return env_stack_push(env, index_type, index_type_id);
2648
2649 index_type = btf_type_id_size(btf, &index_type_id, NULL);
2650 if (!index_type || !btf_type_is_int(index_type) ||
2651 !btf_type_int_is_regular(index_type)) {
2652 btf_verifier_log_type(env, v->t, "Invalid index");
2653 return -EINVAL;
2654 }
2655
2656
2657 elem_type_id = array->type;
2658 elem_type = btf_type_by_id(btf, elem_type_id);
2659 if (btf_type_nosize_or_null(elem_type) ||
2660 btf_type_is_resolve_source_only(elem_type)) {
2661 btf_verifier_log_type(env, v->t,
2662 "Invalid elem");
2663 return -EINVAL;
2664 }
2665
2666 if (!env_type_is_resolve_sink(env, elem_type) &&
2667 !env_type_is_resolved(env, elem_type_id))
2668 return env_stack_push(env, elem_type, elem_type_id);
2669
2670 elem_type = btf_type_id_size(btf, &elem_type_id, &elem_size);
2671 if (!elem_type) {
2672 btf_verifier_log_type(env, v->t, "Invalid elem");
2673 return -EINVAL;
2674 }
2675
2676 if (btf_type_is_int(elem_type) && !btf_type_int_is_regular(elem_type)) {
2677 btf_verifier_log_type(env, v->t, "Invalid array of int");
2678 return -EINVAL;
2679 }
2680
2681 if (array->nelems && elem_size > U32_MAX / array->nelems) {
2682 btf_verifier_log_type(env, v->t,
2683 "Array size overflows U32_MAX");
2684 return -EINVAL;
2685 }
2686
2687 env_stack_pop_resolved(env, elem_type_id, elem_size * array->nelems);
2688
2689 return 0;
2690}
2691
2692static void btf_array_log(struct btf_verifier_env *env,
2693 const struct btf_type *t)
2694{
2695 const struct btf_array *array = btf_type_array(t);
2696
2697 btf_verifier_log(env, "type_id=%u index_type_id=%u nr_elems=%u",
2698 array->type, array->index_type, array->nelems);
2699}
2700
2701static void __btf_array_show(const struct btf *btf, const struct btf_type *t,
2702 u32 type_id, void *data, u8 bits_offset,
2703 struct btf_show *show)
2704{
2705 const struct btf_array *array = btf_type_array(t);
2706 const struct btf_kind_operations *elem_ops;
2707 const struct btf_type *elem_type;
2708 u32 i, elem_size = 0, elem_type_id;
2709 u16 encoding = 0;
2710
2711 elem_type_id = array->type;
2712 elem_type = btf_type_skip_modifiers(btf, elem_type_id, NULL);
2713 if (elem_type && btf_type_has_size(elem_type))
2714 elem_size = elem_type->size;
2715
2716 if (elem_type && btf_type_is_int(elem_type)) {
2717 u32 int_type = btf_type_int(elem_type);
2718
2719 encoding = BTF_INT_ENCODING(int_type);
2720
2721
2722
2723
2724
2725
2726 if (elem_size == 1)
2727 encoding = BTF_INT_CHAR;
2728 }
2729
2730 if (!btf_show_start_array_type(show, t, type_id, encoding, data))
2731 return;
2732
2733 if (!elem_type)
2734 goto out;
2735 elem_ops = btf_type_ops(elem_type);
2736
2737 for (i = 0; i < array->nelems; i++) {
2738
2739 btf_show_start_array_member(show);
2740
2741 elem_ops->show(btf, elem_type, elem_type_id, data,
2742 bits_offset, show);
2743 data += elem_size;
2744
2745 btf_show_end_array_member(show);
2746
2747 if (show->state.array_terminated)
2748 break;
2749 }
2750out:
2751 btf_show_end_array_type(show);
2752}
2753
2754static void btf_array_show(const struct btf *btf, const struct btf_type *t,
2755 u32 type_id, void *data, u8 bits_offset,
2756 struct btf_show *show)
2757{
2758 const struct btf_member *m = show->state.member;
2759
2760
2761
2762
2763
2764
2765 if (show->state.depth > 0 && !(show->flags & BTF_SHOW_ZERO)) {
2766 if (!show->state.depth_check) {
2767 show->state.depth_check = show->state.depth + 1;
2768 show->state.depth_to_show = 0;
2769 }
2770 __btf_array_show(btf, t, type_id, data, bits_offset, show);
2771 show->state.member = m;
2772
2773 if (show->state.depth_check != show->state.depth + 1)
2774 return;
2775 show->state.depth_check = 0;
2776
2777 if (show->state.depth_to_show <= show->state.depth)
2778 return;
2779
2780
2781
2782
2783 }
2784 __btf_array_show(btf, t, type_id, data, bits_offset, show);
2785}
2786
2787static struct btf_kind_operations array_ops = {
2788 .check_meta = btf_array_check_meta,
2789 .resolve = btf_array_resolve,
2790 .check_member = btf_array_check_member,
2791 .check_kflag_member = btf_generic_check_kflag_member,
2792 .log_details = btf_array_log,
2793 .show = btf_array_show,
2794};
2795
2796static int btf_struct_check_member(struct btf_verifier_env *env,
2797 const struct btf_type *struct_type,
2798 const struct btf_member *member,
2799 const struct btf_type *member_type)
2800{
2801 u32 struct_bits_off = member->offset;
2802 u32 struct_size, bytes_offset;
2803
2804 if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
2805 btf_verifier_log_member(env, struct_type, member,
2806 "Member is not byte aligned");
2807 return -EINVAL;
2808 }
2809
2810 struct_size = struct_type->size;
2811 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
2812 if (struct_size - bytes_offset < member_type->size) {
2813 btf_verifier_log_member(env, struct_type, member,
2814 "Member exceeds struct_size");
2815 return -EINVAL;
2816 }
2817
2818 return 0;
2819}
2820
2821static s32 btf_struct_check_meta(struct btf_verifier_env *env,
2822 const struct btf_type *t,
2823 u32 meta_left)
2824{
2825 bool is_union = BTF_INFO_KIND(t->info) == BTF_KIND_UNION;
2826 const struct btf_member *member;
2827 u32 meta_needed, last_offset;
2828 struct btf *btf = env->btf;
2829 u32 struct_size = t->size;
2830 u32 offset;
2831 u16 i;
2832
2833 meta_needed = btf_type_vlen(t) * sizeof(*member);
2834 if (meta_left < meta_needed) {
2835 btf_verifier_log_basic(env, t,
2836 "meta_left:%u meta_needed:%u",
2837 meta_left, meta_needed);
2838 return -EINVAL;
2839 }
2840
2841
2842 if (t->name_off &&
2843 !btf_name_valid_identifier(env->btf, t->name_off)) {
2844 btf_verifier_log_type(env, t, "Invalid name");
2845 return -EINVAL;
2846 }
2847
2848 btf_verifier_log_type(env, t, NULL);
2849
2850 last_offset = 0;
2851 for_each_member(i, t, member) {
2852 if (!btf_name_offset_valid(btf, member->name_off)) {
2853 btf_verifier_log_member(env, t, member,
2854 "Invalid member name_offset:%u",
2855 member->name_off);
2856 return -EINVAL;
2857 }
2858
2859
2860 if (member->name_off &&
2861 !btf_name_valid_identifier(btf, member->name_off)) {
2862 btf_verifier_log_member(env, t, member, "Invalid name");
2863 return -EINVAL;
2864 }
2865
2866 if (!member->type || !BTF_TYPE_ID_VALID(member->type)) {
2867 btf_verifier_log_member(env, t, member,
2868 "Invalid type_id");
2869 return -EINVAL;
2870 }
2871
2872 offset = btf_member_bit_offset(t, member);
2873 if (is_union && offset) {
2874 btf_verifier_log_member(env, t, member,
2875 "Invalid member bits_offset");
2876 return -EINVAL;
2877 }
2878
2879
2880
2881
2882
2883 if (last_offset > offset) {
2884 btf_verifier_log_member(env, t, member,
2885 "Invalid member bits_offset");
2886 return -EINVAL;
2887 }
2888
2889 if (BITS_ROUNDUP_BYTES(offset) > struct_size) {
2890 btf_verifier_log_member(env, t, member,
2891 "Member bits_offset exceeds its struct size");
2892 return -EINVAL;
2893 }
2894
2895 btf_verifier_log_member(env, t, member, NULL);
2896 last_offset = offset;
2897 }
2898
2899 return meta_needed;
2900}
2901
2902static int btf_struct_resolve(struct btf_verifier_env *env,
2903 const struct resolve_vertex *v)
2904{
2905 const struct btf_member *member;
2906 int err;
2907 u16 i;
2908
2909
2910
2911
2912
2913 if (v->next_member) {
2914 const struct btf_type *last_member_type;
2915 const struct btf_member *last_member;
2916 u16 last_member_type_id;
2917
2918 last_member = btf_type_member(v->t) + v->next_member - 1;
2919 last_member_type_id = last_member->type;
2920 if (WARN_ON_ONCE(!env_type_is_resolved(env,
2921 last_member_type_id)))
2922 return -EINVAL;
2923
2924 last_member_type = btf_type_by_id(env->btf,
2925 last_member_type_id);
2926 if (btf_type_kflag(v->t))
2927 err = btf_type_ops(last_member_type)->check_kflag_member(env, v->t,
2928 last_member,
2929 last_member_type);
2930 else
2931 err = btf_type_ops(last_member_type)->check_member(env, v->t,
2932 last_member,
2933 last_member_type);
2934 if (err)
2935 return err;
2936 }
2937
2938 for_each_member_from(i, v->next_member, v->t, member) {
2939 u32 member_type_id = member->type;
2940 const struct btf_type *member_type = btf_type_by_id(env->btf,
2941 member_type_id);
2942
2943 if (btf_type_nosize_or_null(member_type) ||
2944 btf_type_is_resolve_source_only(member_type)) {
2945 btf_verifier_log_member(env, v->t, member,
2946 "Invalid member");
2947 return -EINVAL;
2948 }
2949
2950 if (!env_type_is_resolve_sink(env, member_type) &&
2951 !env_type_is_resolved(env, member_type_id)) {
2952 env_stack_set_next_member(env, i + 1);
2953 return env_stack_push(env, member_type, member_type_id);
2954 }
2955
2956 if (btf_type_kflag(v->t))
2957 err = btf_type_ops(member_type)->check_kflag_member(env, v->t,
2958 member,
2959 member_type);
2960 else
2961 err = btf_type_ops(member_type)->check_member(env, v->t,
2962 member,
2963 member_type);
2964 if (err)
2965 return err;
2966 }
2967
2968 env_stack_pop_resolved(env, 0, 0);
2969
2970 return 0;
2971}
2972
2973static void btf_struct_log(struct btf_verifier_env *env,
2974 const struct btf_type *t)
2975{
2976 btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t));
2977}
2978
2979
2980
2981
2982
2983int btf_find_spin_lock(const struct btf *btf, const struct btf_type *t)
2984{
2985 const struct btf_member *member;
2986 u32 i, off = -ENOENT;
2987
2988 if (!__btf_type_is_struct(t))
2989 return -EINVAL;
2990
2991 for_each_member(i, t, member) {
2992 const struct btf_type *member_type = btf_type_by_id(btf,
2993 member->type);
2994 if (!__btf_type_is_struct(member_type))
2995 continue;
2996 if (member_type->size != sizeof(struct bpf_spin_lock))
2997 continue;
2998 if (strcmp(__btf_name_by_offset(btf, member_type->name_off),
2999 "bpf_spin_lock"))
3000 continue;
3001 if (off != -ENOENT)
3002
3003 return -E2BIG;
3004 off = btf_member_bit_offset(t, member);
3005 if (off % 8)
3006
3007 return -EINVAL;
3008 off /= 8;
3009 if (off % __alignof__(struct bpf_spin_lock))
3010
3011 return -EINVAL;
3012 }
3013 return off;
3014}
3015
3016static void __btf_struct_show(const struct btf *btf, const struct btf_type *t,
3017 u32 type_id, void *data, u8 bits_offset,
3018 struct btf_show *show)
3019{
3020 const struct btf_member *member;
3021 void *safe_data;
3022 u32 i;
3023
3024 safe_data = btf_show_start_struct_type(show, t, type_id, data);
3025 if (!safe_data)
3026 return;
3027
3028 for_each_member(i, t, member) {
3029 const struct btf_type *member_type = btf_type_by_id(btf,
3030 member->type);
3031 const struct btf_kind_operations *ops;
3032 u32 member_offset, bitfield_size;
3033 u32 bytes_offset;
3034 u8 bits8_offset;
3035
3036 btf_show_start_member(show, member);
3037
3038 member_offset = btf_member_bit_offset(t, member);
3039 bitfield_size = btf_member_bitfield_size(t, member);
3040 bytes_offset = BITS_ROUNDDOWN_BYTES(member_offset);
3041 bits8_offset = BITS_PER_BYTE_MASKED(member_offset);
3042 if (bitfield_size) {
3043 safe_data = btf_show_start_type(show, member_type,
3044 member->type,
3045 data + bytes_offset);
3046 if (safe_data)
3047 btf_bitfield_show(safe_data,
3048 bits8_offset,
3049 bitfield_size, show);
3050 btf_show_end_type(show);
3051 } else {
3052 ops = btf_type_ops(member_type);
3053 ops->show(btf, member_type, member->type,
3054 data + bytes_offset, bits8_offset, show);
3055 }
3056
3057 btf_show_end_member(show);
3058 }
3059
3060 btf_show_end_struct_type(show);
3061}
3062
3063static void btf_struct_show(const struct btf *btf, const struct btf_type *t,
3064 u32 type_id, void *data, u8 bits_offset,
3065 struct btf_show *show)
3066{
3067 const struct btf_member *m = show->state.member;
3068
3069
3070
3071
3072
3073
3074 if (show->state.depth > 0 && !(show->flags & BTF_SHOW_ZERO)) {
3075 if (!show->state.depth_check) {
3076 show->state.depth_check = show->state.depth + 1;
3077 show->state.depth_to_show = 0;
3078 }
3079 __btf_struct_show(btf, t, type_id, data, bits_offset, show);
3080
3081 show->state.member = m;
3082 if (show->state.depth_check != show->state.depth + 1)
3083 return;
3084 show->state.depth_check = 0;
3085
3086 if (show->state.depth_to_show <= show->state.depth)
3087 return;
3088
3089
3090
3091
3092 }
3093
3094 __btf_struct_show(btf, t, type_id, data, bits_offset, show);
3095}
3096
3097static struct btf_kind_operations struct_ops = {
3098 .check_meta = btf_struct_check_meta,
3099 .resolve = btf_struct_resolve,
3100 .check_member = btf_struct_check_member,
3101 .check_kflag_member = btf_generic_check_kflag_member,
3102 .log_details = btf_struct_log,
3103 .show = btf_struct_show,
3104};
3105
3106static int btf_enum_check_member(struct btf_verifier_env *env,
3107 const struct btf_type *struct_type,
3108 const struct btf_member *member,
3109 const struct btf_type *member_type)
3110{
3111 u32 struct_bits_off = member->offset;
3112 u32 struct_size, bytes_offset;
3113
3114 if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
3115 btf_verifier_log_member(env, struct_type, member,
3116 "Member is not byte aligned");
3117 return -EINVAL;
3118 }
3119
3120 struct_size = struct_type->size;
3121 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
3122 if (struct_size - bytes_offset < member_type->size) {
3123 btf_verifier_log_member(env, struct_type, member,
3124 "Member exceeds struct_size");
3125 return -EINVAL;
3126 }
3127
3128 return 0;
3129}
3130
3131static int btf_enum_check_kflag_member(struct btf_verifier_env *env,
3132 const struct btf_type *struct_type,
3133 const struct btf_member *member,
3134 const struct btf_type *member_type)
3135{
3136 u32 struct_bits_off, nr_bits, bytes_end, struct_size;
3137 u32 int_bitsize = sizeof(int) * BITS_PER_BYTE;
3138
3139 struct_bits_off = BTF_MEMBER_BIT_OFFSET(member->offset);
3140 nr_bits = BTF_MEMBER_BITFIELD_SIZE(member->offset);
3141 if (!nr_bits) {
3142 if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
3143 btf_verifier_log_member(env, struct_type, member,
3144 "Member is not byte aligned");
3145 return -EINVAL;
3146 }
3147
3148 nr_bits = int_bitsize;
3149 } else if (nr_bits > int_bitsize) {
3150 btf_verifier_log_member(env, struct_type, member,
3151 "Invalid member bitfield_size");
3152 return -EINVAL;
3153 }
3154
3155 struct_size = struct_type->size;
3156 bytes_end = BITS_ROUNDUP_BYTES(struct_bits_off + nr_bits);
3157 if (struct_size < bytes_end) {
3158 btf_verifier_log_member(env, struct_type, member,
3159 "Member exceeds struct_size");
3160 return -EINVAL;
3161 }
3162
3163 return 0;
3164}
3165
3166static s32 btf_enum_check_meta(struct btf_verifier_env *env,
3167 const struct btf_type *t,
3168 u32 meta_left)
3169{
3170 const struct btf_enum *enums = btf_type_enum(t);
3171 struct btf *btf = env->btf;
3172 u16 i, nr_enums;
3173 u32 meta_needed;
3174
3175 nr_enums = btf_type_vlen(t);
3176 meta_needed = nr_enums * sizeof(*enums);
3177
3178 if (meta_left < meta_needed) {
3179 btf_verifier_log_basic(env, t,
3180 "meta_left:%u meta_needed:%u",
3181 meta_left, meta_needed);
3182 return -EINVAL;
3183 }
3184
3185 if (btf_type_kflag(t)) {
3186 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
3187 return -EINVAL;
3188 }
3189
3190 if (t->size > 8 || !is_power_of_2(t->size)) {
3191 btf_verifier_log_type(env, t, "Unexpected size");
3192 return -EINVAL;
3193 }
3194
3195
3196 if (t->name_off &&
3197 !btf_name_valid_identifier(env->btf, t->name_off)) {
3198 btf_verifier_log_type(env, t, "Invalid name");
3199 return -EINVAL;
3200 }
3201
3202 btf_verifier_log_type(env, t, NULL);
3203
3204 for (i = 0; i < nr_enums; i++) {
3205 if (!btf_name_offset_valid(btf, enums[i].name_off)) {
3206 btf_verifier_log(env, "\tInvalid name_offset:%u",
3207 enums[i].name_off);
3208 return -EINVAL;
3209 }
3210
3211
3212 if (!enums[i].name_off ||
3213 !btf_name_valid_identifier(btf, enums[i].name_off)) {
3214 btf_verifier_log_type(env, t, "Invalid name");
3215 return -EINVAL;
3216 }
3217
3218 if (env->log.level == BPF_LOG_KERNEL)
3219 continue;
3220 btf_verifier_log(env, "\t%s val=%d\n",
3221 __btf_name_by_offset(btf, enums[i].name_off),
3222 enums[i].val);
3223 }
3224
3225 return meta_needed;
3226}
3227
3228static void btf_enum_log(struct btf_verifier_env *env,
3229 const struct btf_type *t)
3230{
3231 btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t));
3232}
3233
3234static void btf_enum_show(const struct btf *btf, const struct btf_type *t,
3235 u32 type_id, void *data, u8 bits_offset,
3236 struct btf_show *show)
3237{
3238 const struct btf_enum *enums = btf_type_enum(t);
3239 u32 i, nr_enums = btf_type_vlen(t);
3240 void *safe_data;
3241 int v;
3242
3243 safe_data = btf_show_start_type(show, t, type_id, data);
3244 if (!safe_data)
3245 return;
3246
3247 v = *(int *)safe_data;
3248
3249 for (i = 0; i < nr_enums; i++) {
3250 if (v != enums[i].val)
3251 continue;
3252
3253 btf_show_type_value(show, "%s",
3254 __btf_name_by_offset(btf,
3255 enums[i].name_off));
3256
3257 btf_show_end_type(show);
3258 return;
3259 }
3260
3261 btf_show_type_value(show, "%d", v);
3262 btf_show_end_type(show);
3263}
3264
3265static struct btf_kind_operations enum_ops = {
3266 .check_meta = btf_enum_check_meta,
3267 .resolve = btf_df_resolve,
3268 .check_member = btf_enum_check_member,
3269 .check_kflag_member = btf_enum_check_kflag_member,
3270 .log_details = btf_enum_log,
3271 .show = btf_enum_show,
3272};
3273
3274static s32 btf_func_proto_check_meta(struct btf_verifier_env *env,
3275 const struct btf_type *t,
3276 u32 meta_left)
3277{
3278 u32 meta_needed = btf_type_vlen(t) * sizeof(struct btf_param);
3279
3280 if (meta_left < meta_needed) {
3281 btf_verifier_log_basic(env, t,
3282 "meta_left:%u meta_needed:%u",
3283 meta_left, meta_needed);
3284 return -EINVAL;
3285 }
3286
3287 if (t->name_off) {
3288 btf_verifier_log_type(env, t, "Invalid name");
3289 return -EINVAL;
3290 }
3291
3292 if (btf_type_kflag(t)) {
3293 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
3294 return -EINVAL;
3295 }
3296
3297 btf_verifier_log_type(env, t, NULL);
3298
3299 return meta_needed;
3300}
3301
3302static void btf_func_proto_log(struct btf_verifier_env *env,
3303 const struct btf_type *t)
3304{
3305 const struct btf_param *args = (const struct btf_param *)(t + 1);
3306 u16 nr_args = btf_type_vlen(t), i;
3307
3308 btf_verifier_log(env, "return=%u args=(", t->type);
3309 if (!nr_args) {
3310 btf_verifier_log(env, "void");
3311 goto done;
3312 }
3313
3314 if (nr_args == 1 && !args[0].type) {
3315
3316 btf_verifier_log(env, "vararg");
3317 goto done;
3318 }
3319
3320 btf_verifier_log(env, "%u %s", args[0].type,
3321 __btf_name_by_offset(env->btf,
3322 args[0].name_off));
3323 for (i = 1; i < nr_args - 1; i++)
3324 btf_verifier_log(env, ", %u %s", args[i].type,
3325 __btf_name_by_offset(env->btf,
3326 args[i].name_off));
3327
3328 if (nr_args > 1) {
3329 const struct btf_param *last_arg = &args[nr_args - 1];
3330
3331 if (last_arg->type)
3332 btf_verifier_log(env, ", %u %s", last_arg->type,
3333 __btf_name_by_offset(env->btf,
3334 last_arg->name_off));
3335 else
3336 btf_verifier_log(env, ", vararg");
3337 }
3338
3339done:
3340 btf_verifier_log(env, ")");
3341}
3342
3343static struct btf_kind_operations func_proto_ops = {
3344 .check_meta = btf_func_proto_check_meta,
3345 .resolve = btf_df_resolve,
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355 .check_member = btf_df_check_member,
3356 .check_kflag_member = btf_df_check_kflag_member,
3357 .log_details = btf_func_proto_log,
3358 .show = btf_df_show,
3359};
3360
3361static s32 btf_func_check_meta(struct btf_verifier_env *env,
3362 const struct btf_type *t,
3363 u32 meta_left)
3364{
3365 if (!t->name_off ||
3366 !btf_name_valid_identifier(env->btf, t->name_off)) {
3367 btf_verifier_log_type(env, t, "Invalid name");
3368 return -EINVAL;
3369 }
3370
3371 if (btf_type_vlen(t) > BTF_FUNC_GLOBAL) {
3372 btf_verifier_log_type(env, t, "Invalid func linkage");
3373 return -EINVAL;
3374 }
3375
3376 if (btf_type_kflag(t)) {
3377 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
3378 return -EINVAL;
3379 }
3380
3381 btf_verifier_log_type(env, t, NULL);
3382
3383 return 0;
3384}
3385
3386static struct btf_kind_operations func_ops = {
3387 .check_meta = btf_func_check_meta,
3388 .resolve = btf_df_resolve,
3389 .check_member = btf_df_check_member,
3390 .check_kflag_member = btf_df_check_kflag_member,
3391 .log_details = btf_ref_type_log,
3392 .show = btf_df_show,
3393};
3394
3395static s32 btf_var_check_meta(struct btf_verifier_env *env,
3396 const struct btf_type *t,
3397 u32 meta_left)
3398{
3399 const struct btf_var *var;
3400 u32 meta_needed = sizeof(*var);
3401
3402 if (meta_left < meta_needed) {
3403 btf_verifier_log_basic(env, t,
3404 "meta_left:%u meta_needed:%u",
3405 meta_left, meta_needed);
3406 return -EINVAL;
3407 }
3408
3409 if (btf_type_vlen(t)) {
3410 btf_verifier_log_type(env, t, "vlen != 0");
3411 return -EINVAL;
3412 }
3413
3414 if (btf_type_kflag(t)) {
3415 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
3416 return -EINVAL;
3417 }
3418
3419 if (!t->name_off ||
3420 !__btf_name_valid(env->btf, t->name_off, true)) {
3421 btf_verifier_log_type(env, t, "Invalid name");
3422 return -EINVAL;
3423 }
3424
3425
3426 if (!t->type || !BTF_TYPE_ID_VALID(t->type)) {
3427 btf_verifier_log_type(env, t, "Invalid type_id");
3428 return -EINVAL;
3429 }
3430
3431 var = btf_type_var(t);
3432 if (var->linkage != BTF_VAR_STATIC &&
3433 var->linkage != BTF_VAR_GLOBAL_ALLOCATED) {
3434 btf_verifier_log_type(env, t, "Linkage not supported");
3435 return -EINVAL;
3436 }
3437
3438 btf_verifier_log_type(env, t, NULL);
3439
3440 return meta_needed;
3441}
3442
3443static void btf_var_log(struct btf_verifier_env *env, const struct btf_type *t)
3444{
3445 const struct btf_var *var = btf_type_var(t);
3446
3447 btf_verifier_log(env, "type_id=%u linkage=%u", t->type, var->linkage);
3448}
3449
3450static const struct btf_kind_operations var_ops = {
3451 .check_meta = btf_var_check_meta,
3452 .resolve = btf_var_resolve,
3453 .check_member = btf_df_check_member,
3454 .check_kflag_member = btf_df_check_kflag_member,
3455 .log_details = btf_var_log,
3456 .show = btf_var_show,
3457};
3458
3459static s32 btf_datasec_check_meta(struct btf_verifier_env *env,
3460 const struct btf_type *t,
3461 u32 meta_left)
3462{
3463 const struct btf_var_secinfo *vsi;
3464 u64 last_vsi_end_off = 0, sum = 0;
3465 u32 i, meta_needed;
3466
3467 meta_needed = btf_type_vlen(t) * sizeof(*vsi);
3468 if (meta_left < meta_needed) {
3469 btf_verifier_log_basic(env, t,
3470 "meta_left:%u meta_needed:%u",
3471 meta_left, meta_needed);
3472 return -EINVAL;
3473 }
3474
3475 if (!btf_type_vlen(t)) {
3476 btf_verifier_log_type(env, t, "vlen == 0");
3477 return -EINVAL;
3478 }
3479
3480 if (!t->size) {
3481 btf_verifier_log_type(env, t, "size == 0");
3482 return -EINVAL;
3483 }
3484
3485 if (btf_type_kflag(t)) {
3486 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
3487 return -EINVAL;
3488 }
3489
3490 if (!t->name_off ||
3491 !btf_name_valid_section(env->btf, t->name_off)) {
3492 btf_verifier_log_type(env, t, "Invalid name");
3493 return -EINVAL;
3494 }
3495
3496 btf_verifier_log_type(env, t, NULL);
3497
3498 for_each_vsi(i, t, vsi) {
3499
3500 if (!vsi->type || !BTF_TYPE_ID_VALID(vsi->type)) {
3501 btf_verifier_log_vsi(env, t, vsi,
3502 "Invalid type_id");
3503 return -EINVAL;
3504 }
3505
3506 if (vsi->offset < last_vsi_end_off || vsi->offset >= t->size) {
3507 btf_verifier_log_vsi(env, t, vsi,
3508 "Invalid offset");
3509 return -EINVAL;
3510 }
3511
3512 if (!vsi->size || vsi->size > t->size) {
3513 btf_verifier_log_vsi(env, t, vsi,
3514 "Invalid size");
3515 return -EINVAL;
3516 }
3517
3518 last_vsi_end_off = vsi->offset + vsi->size;
3519 if (last_vsi_end_off > t->size) {
3520 btf_verifier_log_vsi(env, t, vsi,
3521 "Invalid offset+size");
3522 return -EINVAL;
3523 }
3524
3525 btf_verifier_log_vsi(env, t, vsi, NULL);
3526 sum += vsi->size;
3527 }
3528
3529 if (t->size < sum) {
3530 btf_verifier_log_type(env, t, "Invalid btf_info size");
3531 return -EINVAL;
3532 }
3533
3534 return meta_needed;
3535}
3536
3537static int btf_datasec_resolve(struct btf_verifier_env *env,
3538 const struct resolve_vertex *v)
3539{
3540 const struct btf_var_secinfo *vsi;
3541 struct btf *btf = env->btf;
3542 u16 i;
3543
3544 for_each_vsi_from(i, v->next_member, v->t, vsi) {
3545 u32 var_type_id = vsi->type, type_id, type_size = 0;
3546 const struct btf_type *var_type = btf_type_by_id(env->btf,
3547 var_type_id);
3548 if (!var_type || !btf_type_is_var(var_type)) {
3549 btf_verifier_log_vsi(env, v->t, vsi,
3550 "Not a VAR kind member");
3551 return -EINVAL;
3552 }
3553
3554 if (!env_type_is_resolve_sink(env, var_type) &&
3555 !env_type_is_resolved(env, var_type_id)) {
3556 env_stack_set_next_member(env, i + 1);
3557 return env_stack_push(env, var_type, var_type_id);
3558 }
3559
3560 type_id = var_type->type;
3561 if (!btf_type_id_size(btf, &type_id, &type_size)) {
3562 btf_verifier_log_vsi(env, v->t, vsi, "Invalid type");
3563 return -EINVAL;
3564 }
3565
3566 if (vsi->size < type_size) {
3567 btf_verifier_log_vsi(env, v->t, vsi, "Invalid size");
3568 return -EINVAL;
3569 }
3570 }
3571
3572 env_stack_pop_resolved(env, 0, 0);
3573 return 0;
3574}
3575
3576static void btf_datasec_log(struct btf_verifier_env *env,
3577 const struct btf_type *t)
3578{
3579 btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t));
3580}
3581
3582static void btf_datasec_show(const struct btf *btf,
3583 const struct btf_type *t, u32 type_id,
3584 void *data, u8 bits_offset,
3585 struct btf_show *show)
3586{
3587 const struct btf_var_secinfo *vsi;
3588 const struct btf_type *var;
3589 u32 i;
3590
3591 if (!btf_show_start_type(show, t, type_id, data))
3592 return;
3593
3594 btf_show_type_value(show, "section (\"%s\") = {",
3595 __btf_name_by_offset(btf, t->name_off));
3596 for_each_vsi(i, t, vsi) {
3597 var = btf_type_by_id(btf, vsi->type);
3598 if (i)
3599 btf_show(show, ",");
3600 btf_type_ops(var)->show(btf, var, vsi->type,
3601 data + vsi->offset, bits_offset, show);
3602 }
3603 btf_show_end_type(show);
3604}
3605
3606static const struct btf_kind_operations datasec_ops = {
3607 .check_meta = btf_datasec_check_meta,
3608 .resolve = btf_datasec_resolve,
3609 .check_member = btf_df_check_member,
3610 .check_kflag_member = btf_df_check_kflag_member,
3611 .log_details = btf_datasec_log,
3612 .show = btf_datasec_show,
3613};
3614
3615static int btf_func_proto_check(struct btf_verifier_env *env,
3616 const struct btf_type *t)
3617{
3618 const struct btf_type *ret_type;
3619 const struct btf_param *args;
3620 const struct btf *btf;
3621 u16 nr_args, i;
3622 int err;
3623
3624 btf = env->btf;
3625 args = (const struct btf_param *)(t + 1);
3626 nr_args = btf_type_vlen(t);
3627
3628
3629 if (t->type) {
3630 u32 ret_type_id = t->type;
3631
3632 ret_type = btf_type_by_id(btf, ret_type_id);
3633 if (!ret_type) {
3634 btf_verifier_log_type(env, t, "Invalid return type");
3635 return -EINVAL;
3636 }
3637
3638 if (btf_type_needs_resolve(ret_type) &&
3639 !env_type_is_resolved(env, ret_type_id)) {
3640 err = btf_resolve(env, ret_type, ret_type_id);
3641 if (err)
3642 return err;
3643 }
3644
3645
3646 if (!btf_type_id_size(btf, &ret_type_id, NULL)) {
3647 btf_verifier_log_type(env, t, "Invalid return type");
3648 return -EINVAL;
3649 }
3650 }
3651
3652 if (!nr_args)
3653 return 0;
3654
3655
3656 if (!args[nr_args - 1].type) {
3657 if (args[nr_args - 1].name_off) {
3658 btf_verifier_log_type(env, t, "Invalid arg#%u",
3659 nr_args);
3660 return -EINVAL;
3661 }
3662 nr_args--;
3663 }
3664
3665 err = 0;
3666 for (i = 0; i < nr_args; i++) {
3667 const struct btf_type *arg_type;
3668 u32 arg_type_id;
3669
3670 arg_type_id = args[i].type;
3671 arg_type = btf_type_by_id(btf, arg_type_id);
3672 if (!arg_type) {
3673 btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1);
3674 err = -EINVAL;
3675 break;
3676 }
3677
3678 if (args[i].name_off &&
3679 (!btf_name_offset_valid(btf, args[i].name_off) ||
3680 !btf_name_valid_identifier(btf, args[i].name_off))) {
3681 btf_verifier_log_type(env, t,
3682 "Invalid arg#%u", i + 1);
3683 err = -EINVAL;
3684 break;
3685 }
3686
3687 if (btf_type_needs_resolve(arg_type) &&
3688 !env_type_is_resolved(env, arg_type_id)) {
3689 err = btf_resolve(env, arg_type, arg_type_id);
3690 if (err)
3691 break;
3692 }
3693
3694 if (!btf_type_id_size(btf, &arg_type_id, NULL)) {
3695 btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1);
3696 err = -EINVAL;
3697 break;
3698 }
3699 }
3700
3701 return err;
3702}
3703
3704static int btf_func_check(struct btf_verifier_env *env,
3705 const struct btf_type *t)
3706{
3707 const struct btf_type *proto_type;
3708 const struct btf_param *args;
3709 const struct btf *btf;
3710 u16 nr_args, i;
3711
3712 btf = env->btf;
3713 proto_type = btf_type_by_id(btf, t->type);
3714
3715 if (!proto_type || !btf_type_is_func_proto(proto_type)) {
3716 btf_verifier_log_type(env, t, "Invalid type_id");
3717 return -EINVAL;
3718 }
3719
3720 args = (const struct btf_param *)(proto_type + 1);
3721 nr_args = btf_type_vlen(proto_type);
3722 for (i = 0; i < nr_args; i++) {
3723 if (!args[i].name_off && args[i].type) {
3724 btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1);
3725 return -EINVAL;
3726 }
3727 }
3728
3729 return 0;
3730}
3731
3732static const struct btf_kind_operations * const kind_ops[NR_BTF_KINDS] = {
3733 [BTF_KIND_INT] = &int_ops,
3734 [BTF_KIND_PTR] = &ptr_ops,
3735 [BTF_KIND_ARRAY] = &array_ops,
3736 [BTF_KIND_STRUCT] = &struct_ops,
3737 [BTF_KIND_UNION] = &struct_ops,
3738 [BTF_KIND_ENUM] = &enum_ops,
3739 [BTF_KIND_FWD] = &fwd_ops,
3740 [BTF_KIND_TYPEDEF] = &modifier_ops,
3741 [BTF_KIND_VOLATILE] = &modifier_ops,
3742 [BTF_KIND_CONST] = &modifier_ops,
3743 [BTF_KIND_RESTRICT] = &modifier_ops,
3744 [BTF_KIND_FUNC] = &func_ops,
3745 [BTF_KIND_FUNC_PROTO] = &func_proto_ops,
3746 [BTF_KIND_VAR] = &var_ops,
3747 [BTF_KIND_DATASEC] = &datasec_ops,
3748};
3749
3750static s32 btf_check_meta(struct btf_verifier_env *env,
3751 const struct btf_type *t,
3752 u32 meta_left)
3753{
3754 u32 saved_meta_left = meta_left;
3755 s32 var_meta_size;
3756
3757 if (meta_left < sizeof(*t)) {
3758 btf_verifier_log(env, "[%u] meta_left:%u meta_needed:%zu",
3759 env->log_type_id, meta_left, sizeof(*t));
3760 return -EINVAL;
3761 }
3762 meta_left -= sizeof(*t);
3763
3764 if (t->info & ~BTF_INFO_MASK) {
3765 btf_verifier_log(env, "[%u] Invalid btf_info:%x",
3766 env->log_type_id, t->info);
3767 return -EINVAL;
3768 }
3769
3770 if (BTF_INFO_KIND(t->info) > BTF_KIND_MAX ||
3771 BTF_INFO_KIND(t->info) == BTF_KIND_UNKN) {
3772 btf_verifier_log(env, "[%u] Invalid kind:%u",
3773 env->log_type_id, BTF_INFO_KIND(t->info));
3774 return -EINVAL;
3775 }
3776
3777 if (!btf_name_offset_valid(env->btf, t->name_off)) {
3778 btf_verifier_log(env, "[%u] Invalid name_offset:%u",
3779 env->log_type_id, t->name_off);
3780 return -EINVAL;
3781 }
3782
3783 var_meta_size = btf_type_ops(t)->check_meta(env, t, meta_left);
3784 if (var_meta_size < 0)
3785 return var_meta_size;
3786
3787 meta_left -= var_meta_size;
3788
3789 return saved_meta_left - meta_left;
3790}
3791
3792static int btf_check_all_metas(struct btf_verifier_env *env)
3793{
3794 struct btf *btf = env->btf;
3795 struct btf_header *hdr;
3796 void *cur, *end;
3797
3798 hdr = &btf->hdr;
3799 cur = btf->nohdr_data + hdr->type_off;
3800 end = cur + hdr->type_len;
3801
3802 env->log_type_id = 1;
3803 while (cur < end) {
3804 struct btf_type *t = cur;
3805 s32 meta_size;
3806
3807 meta_size = btf_check_meta(env, t, end - cur);
3808 if (meta_size < 0)
3809 return meta_size;
3810
3811 btf_add_type(env, t);
3812 cur += meta_size;
3813 env->log_type_id++;
3814 }
3815
3816 return 0;
3817}
3818
3819static bool btf_resolve_valid(struct btf_verifier_env *env,
3820 const struct btf_type *t,
3821 u32 type_id)
3822{
3823 struct btf *btf = env->btf;
3824
3825 if (!env_type_is_resolved(env, type_id))
3826 return false;
3827
3828 if (btf_type_is_struct(t) || btf_type_is_datasec(t))
3829 return !btf->resolved_ids[type_id] &&
3830 !btf->resolved_sizes[type_id];
3831
3832 if (btf_type_is_modifier(t) || btf_type_is_ptr(t) ||
3833 btf_type_is_var(t)) {
3834 t = btf_type_id_resolve(btf, &type_id);
3835 return t &&
3836 !btf_type_is_modifier(t) &&
3837 !btf_type_is_var(t) &&
3838 !btf_type_is_datasec(t);
3839 }
3840
3841 if (btf_type_is_array(t)) {
3842 const struct btf_array *array = btf_type_array(t);
3843 const struct btf_type *elem_type;
3844 u32 elem_type_id = array->type;
3845 u32 elem_size;
3846
3847 elem_type = btf_type_id_size(btf, &elem_type_id, &elem_size);
3848 return elem_type && !btf_type_is_modifier(elem_type) &&
3849 (array->nelems * elem_size ==
3850 btf->resolved_sizes[type_id]);
3851 }
3852
3853 return false;
3854}
3855
3856static int btf_resolve(struct btf_verifier_env *env,
3857 const struct btf_type *t, u32 type_id)
3858{
3859 u32 save_log_type_id = env->log_type_id;
3860 const struct resolve_vertex *v;
3861 int err = 0;
3862
3863 env->resolve_mode = RESOLVE_TBD;
3864 env_stack_push(env, t, type_id);
3865 while (!err && (v = env_stack_peak(env))) {
3866 env->log_type_id = v->type_id;
3867 err = btf_type_ops(v->t)->resolve(env, v);
3868 }
3869
3870 env->log_type_id = type_id;
3871 if (err == -E2BIG) {
3872 btf_verifier_log_type(env, t,
3873 "Exceeded max resolving depth:%u",
3874 MAX_RESOLVE_DEPTH);
3875 } else if (err == -EEXIST) {
3876 btf_verifier_log_type(env, t, "Loop detected");
3877 }
3878
3879
3880 if (!err && !btf_resolve_valid(env, t, type_id)) {
3881 btf_verifier_log_type(env, t, "Invalid resolve state");
3882 err = -EINVAL;
3883 }
3884
3885 env->log_type_id = save_log_type_id;
3886 return err;
3887}
3888
3889static int btf_check_all_types(struct btf_verifier_env *env)
3890{
3891 struct btf *btf = env->btf;
3892 u32 type_id;
3893 int err;
3894
3895 err = env_resolve_init(env);
3896 if (err)
3897 return err;
3898
3899 env->phase++;
3900 for (type_id = 1; type_id <= btf->nr_types; type_id++) {
3901 const struct btf_type *t = btf_type_by_id(btf, type_id);
3902
3903 env->log_type_id = type_id;
3904 if (btf_type_needs_resolve(t) &&
3905 !env_type_is_resolved(env, type_id)) {
3906 err = btf_resolve(env, t, type_id);
3907 if (err)
3908 return err;
3909 }
3910
3911 if (btf_type_is_func_proto(t)) {
3912 err = btf_func_proto_check(env, t);
3913 if (err)
3914 return err;
3915 }
3916
3917 if (btf_type_is_func(t)) {
3918 err = btf_func_check(env, t);
3919 if (err)
3920 return err;
3921 }
3922 }
3923
3924 return 0;
3925}
3926
3927static int btf_parse_type_sec(struct btf_verifier_env *env)
3928{
3929 const struct btf_header *hdr = &env->btf->hdr;
3930 int err;
3931
3932
3933 if (hdr->type_off & (sizeof(u32) - 1)) {
3934 btf_verifier_log(env, "Unaligned type_off");
3935 return -EINVAL;
3936 }
3937
3938 if (!hdr->type_len) {
3939 btf_verifier_log(env, "No type found");
3940 return -EINVAL;
3941 }
3942
3943 err = btf_check_all_metas(env);
3944 if (err)
3945 return err;
3946
3947 return btf_check_all_types(env);
3948}
3949
3950static int btf_parse_str_sec(struct btf_verifier_env *env)
3951{
3952 const struct btf_header *hdr;
3953 struct btf *btf = env->btf;
3954 const char *start, *end;
3955
3956 hdr = &btf->hdr;
3957 start = btf->nohdr_data + hdr->str_off;
3958 end = start + hdr->str_len;
3959
3960 if (end != btf->data + btf->data_size) {
3961 btf_verifier_log(env, "String section is not at the end");
3962 return -EINVAL;
3963 }
3964
3965 if (!hdr->str_len || hdr->str_len - 1 > BTF_MAX_NAME_OFFSET ||
3966 start[0] || end[-1]) {
3967 btf_verifier_log(env, "Invalid string section");
3968 return -EINVAL;
3969 }
3970
3971 btf->strings = start;
3972
3973 return 0;
3974}
3975
3976static const size_t btf_sec_info_offset[] = {
3977 offsetof(struct btf_header, type_off),
3978 offsetof(struct btf_header, str_off),
3979};
3980
3981static int btf_sec_info_cmp(const void *a, const void *b)
3982{
3983 const struct btf_sec_info *x = a;
3984 const struct btf_sec_info *y = b;
3985
3986 return (int)(x->off - y->off) ? : (int)(x->len - y->len);
3987}
3988
3989static int btf_check_sec_info(struct btf_verifier_env *env,
3990 u32 btf_data_size)
3991{
3992 struct btf_sec_info secs[ARRAY_SIZE(btf_sec_info_offset)];
3993 u32 total, expected_total, i;
3994 const struct btf_header *hdr;
3995 const struct btf *btf;
3996
3997 btf = env->btf;
3998 hdr = &btf->hdr;
3999
4000
4001 for (i = 0; i < ARRAY_SIZE(btf_sec_info_offset); i++)
4002 secs[i] = *(struct btf_sec_info *)((void *)hdr +
4003 btf_sec_info_offset[i]);
4004
4005 sort(secs, ARRAY_SIZE(btf_sec_info_offset),
4006 sizeof(struct btf_sec_info), btf_sec_info_cmp, NULL);
4007
4008
4009 total = 0;
4010 expected_total = btf_data_size - hdr->hdr_len;
4011 for (i = 0; i < ARRAY_SIZE(btf_sec_info_offset); i++) {
4012 if (expected_total < secs[i].off) {
4013 btf_verifier_log(env, "Invalid section offset");
4014 return -EINVAL;
4015 }
4016 if (total < secs[i].off) {
4017
4018 btf_verifier_log(env, "Unsupported section found");
4019 return -EINVAL;
4020 }
4021 if (total > secs[i].off) {
4022 btf_verifier_log(env, "Section overlap found");
4023 return -EINVAL;
4024 }
4025 if (expected_total - total < secs[i].len) {
4026 btf_verifier_log(env,
4027 "Total section length too long");
4028 return -EINVAL;
4029 }
4030 total += secs[i].len;
4031 }
4032
4033
4034 if (expected_total != total) {
4035 btf_verifier_log(env, "Unsupported section found");
4036 return -EINVAL;
4037 }
4038
4039 return 0;
4040}
4041
4042static int btf_parse_hdr(struct btf_verifier_env *env)
4043{
4044 u32 hdr_len, hdr_copy, btf_data_size;
4045 const struct btf_header *hdr;
4046 struct btf *btf;
4047 int err;
4048
4049 btf = env->btf;
4050 btf_data_size = btf->data_size;
4051
4052 if (btf_data_size <
4053 offsetof(struct btf_header, hdr_len) + sizeof(hdr->hdr_len)) {
4054 btf_verifier_log(env, "hdr_len not found");
4055 return -EINVAL;
4056 }
4057
4058 hdr = btf->data;
4059 hdr_len = hdr->hdr_len;
4060 if (btf_data_size < hdr_len) {
4061 btf_verifier_log(env, "btf_header not found");
4062 return -EINVAL;
4063 }
4064
4065
4066 if (hdr_len > sizeof(btf->hdr)) {
4067 u8 *expected_zero = btf->data + sizeof(btf->hdr);
4068 u8 *end = btf->data + hdr_len;
4069
4070 for (; expected_zero < end; expected_zero++) {
4071 if (*expected_zero) {
4072 btf_verifier_log(env, "Unsupported btf_header");
4073 return -E2BIG;
4074 }
4075 }
4076 }
4077
4078 hdr_copy = min_t(u32, hdr_len, sizeof(btf->hdr));
4079 memcpy(&btf->hdr, btf->data, hdr_copy);
4080
4081 hdr = &btf->hdr;
4082
4083 btf_verifier_log_hdr(env, btf_data_size);
4084
4085 if (hdr->magic != BTF_MAGIC) {
4086 btf_verifier_log(env, "Invalid magic");
4087 return -EINVAL;
4088 }
4089
4090 if (hdr->version != BTF_VERSION) {
4091 btf_verifier_log(env, "Unsupported version");
4092 return -ENOTSUPP;
4093 }
4094
4095 if (hdr->flags) {
4096 btf_verifier_log(env, "Unsupported flags");
4097 return -ENOTSUPP;
4098 }
4099
4100 if (btf_data_size == hdr->hdr_len) {
4101 btf_verifier_log(env, "No data");
4102 return -EINVAL;
4103 }
4104
4105 err = btf_check_sec_info(env, btf_data_size);
4106 if (err)
4107 return err;
4108
4109 return 0;
4110}
4111
4112static struct btf *btf_parse(void __user *btf_data, u32 btf_data_size,
4113 u32 log_level, char __user *log_ubuf, u32 log_size)
4114{
4115 struct btf_verifier_env *env = NULL;
4116 struct bpf_verifier_log *log;
4117 struct btf *btf = NULL;
4118 u8 *data;
4119 int err;
4120
4121 if (btf_data_size > BTF_MAX_SIZE)
4122 return ERR_PTR(-E2BIG);
4123
4124 env = kzalloc(sizeof(*env), GFP_KERNEL | __GFP_NOWARN);
4125 if (!env)
4126 return ERR_PTR(-ENOMEM);
4127
4128 log = &env->log;
4129 if (log_level || log_ubuf || log_size) {
4130
4131
4132
4133 log->level = log_level;
4134 log->ubuf = log_ubuf;
4135 log->len_total = log_size;
4136
4137
4138 if (log->len_total < 128 || log->len_total > UINT_MAX >> 8 ||
4139 !log->level || !log->ubuf) {
4140 err = -EINVAL;
4141 goto errout;
4142 }
4143 }
4144
4145 btf = kzalloc(sizeof(*btf), GFP_KERNEL | __GFP_NOWARN);
4146 if (!btf) {
4147 err = -ENOMEM;
4148 goto errout;
4149 }
4150 env->btf = btf;
4151
4152 data = kvmalloc(btf_data_size, GFP_KERNEL | __GFP_NOWARN);
4153 if (!data) {
4154 err = -ENOMEM;
4155 goto errout;
4156 }
4157
4158 btf->data = data;
4159 btf->data_size = btf_data_size;
4160
4161 if (copy_from_user(data, btf_data, btf_data_size)) {
4162 err = -EFAULT;
4163 goto errout;
4164 }
4165
4166 err = btf_parse_hdr(env);
4167 if (err)
4168 goto errout;
4169
4170 btf->nohdr_data = btf->data + btf->hdr.hdr_len;
4171
4172 err = btf_parse_str_sec(env);
4173 if (err)
4174 goto errout;
4175
4176 err = btf_parse_type_sec(env);
4177 if (err)
4178 goto errout;
4179
4180 if (log->level && bpf_verifier_log_full(log)) {
4181 err = -ENOSPC;
4182 goto errout;
4183 }
4184
4185 btf_verifier_env_free(env);
4186 refcount_set(&btf->refcnt, 1);
4187 return btf;
4188
4189errout:
4190 btf_verifier_env_free(env);
4191 if (btf)
4192 btf_free(btf);
4193 return ERR_PTR(err);
4194}
4195
4196extern char __weak __start_BTF[];
4197extern char __weak __stop_BTF[];
4198extern struct btf *btf_vmlinux;
4199
4200#define BPF_MAP_TYPE(_id, _ops)
4201#define BPF_LINK_TYPE(_id, _name)
4202static union {
4203 struct bpf_ctx_convert {
4204#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
4205 prog_ctx_type _id##_prog; \
4206 kern_ctx_type _id##_kern;
4207#include <linux/bpf_types.h>
4208#undef BPF_PROG_TYPE
4209 } *__t;
4210
4211 const struct btf_type *t;
4212} bpf_ctx_convert;
4213enum {
4214#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
4215 __ctx_convert##_id,
4216#include <linux/bpf_types.h>
4217#undef BPF_PROG_TYPE
4218 __ctx_convert_unused,
4219};
4220static u8 bpf_ctx_convert_map[] = {
4221#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
4222 [_id] = __ctx_convert##_id,
4223#include <linux/bpf_types.h>
4224#undef BPF_PROG_TYPE
4225 0,
4226};
4227#undef BPF_MAP_TYPE
4228#undef BPF_LINK_TYPE
4229
4230static const struct btf_member *
4231btf_get_prog_ctx_type(struct bpf_verifier_log *log, struct btf *btf,
4232 const struct btf_type *t, enum bpf_prog_type prog_type,
4233 int arg)
4234{
4235 const struct btf_type *conv_struct;
4236 const struct btf_type *ctx_struct;
4237 const struct btf_member *ctx_type;
4238 const char *tname, *ctx_tname;
4239
4240 conv_struct = bpf_ctx_convert.t;
4241 if (!conv_struct) {
4242 bpf_log(log, "btf_vmlinux is malformed\n");
4243 return NULL;
4244 }
4245 t = btf_type_by_id(btf, t->type);
4246 while (btf_type_is_modifier(t))
4247 t = btf_type_by_id(btf, t->type);
4248 if (!btf_type_is_struct(t)) {
4249
4250
4251
4252
4253
4254 if (log->level & BPF_LOG_LEVEL)
4255 bpf_log(log, "arg#%d type is not a struct\n", arg);
4256 return NULL;
4257 }
4258 tname = btf_name_by_offset(btf, t->name_off);
4259 if (!tname) {
4260 bpf_log(log, "arg#%d struct doesn't have a name\n", arg);
4261 return NULL;
4262 }
4263
4264 ctx_type = btf_type_member(conv_struct) + bpf_ctx_convert_map[prog_type] * 2;
4265
4266
4267
4268 ctx_struct = btf_type_by_id(btf_vmlinux, ctx_type->type);
4269 if (!ctx_struct)
4270
4271 return NULL;
4272 ctx_tname = btf_name_by_offset(btf_vmlinux, ctx_struct->name_off);
4273 if (!ctx_tname) {
4274
4275 bpf_log(log, "Please fix kernel include/linux/bpf_types.h\n");
4276 return NULL;
4277 }
4278
4279
4280
4281
4282
4283
4284
4285 if (strcmp(ctx_tname, tname))
4286 return NULL;
4287 return ctx_type;
4288}
4289
4290static const struct bpf_map_ops * const btf_vmlinux_map_ops[] = {
4291#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
4292#define BPF_LINK_TYPE(_id, _name)
4293#define BPF_MAP_TYPE(_id, _ops) \
4294 [_id] = &_ops,
4295#include <linux/bpf_types.h>
4296#undef BPF_PROG_TYPE
4297#undef BPF_LINK_TYPE
4298#undef BPF_MAP_TYPE
4299};
4300
4301static int btf_vmlinux_map_ids_init(const struct btf *btf,
4302 struct bpf_verifier_log *log)
4303{
4304 const struct bpf_map_ops *ops;
4305 int i, btf_id;
4306
4307 for (i = 0; i < ARRAY_SIZE(btf_vmlinux_map_ops); ++i) {
4308 ops = btf_vmlinux_map_ops[i];
4309 if (!ops || (!ops->map_btf_name && !ops->map_btf_id))
4310 continue;
4311 if (!ops->map_btf_name || !ops->map_btf_id) {
4312 bpf_log(log, "map type %d is misconfigured\n", i);
4313 return -EINVAL;
4314 }
4315 btf_id = btf_find_by_name_kind(btf, ops->map_btf_name,
4316 BTF_KIND_STRUCT);
4317 if (btf_id < 0)
4318 return btf_id;
4319 *ops->map_btf_id = btf_id;
4320 }
4321
4322 return 0;
4323}
4324
4325static int btf_translate_to_vmlinux(struct bpf_verifier_log *log,
4326 struct btf *btf,
4327 const struct btf_type *t,
4328 enum bpf_prog_type prog_type,
4329 int arg)
4330{
4331 const struct btf_member *prog_ctx_type, *kern_ctx_type;
4332
4333 prog_ctx_type = btf_get_prog_ctx_type(log, btf, t, prog_type, arg);
4334 if (!prog_ctx_type)
4335 return -ENOENT;
4336 kern_ctx_type = prog_ctx_type + 1;
4337 return kern_ctx_type->type;
4338}
4339
4340BTF_ID_LIST(bpf_ctx_convert_btf_id)
4341BTF_ID(struct, bpf_ctx_convert)
4342
4343struct btf *btf_parse_vmlinux(void)
4344{
4345 struct btf_verifier_env *env = NULL;
4346 struct bpf_verifier_log *log;
4347 struct btf *btf = NULL;
4348 int err;
4349
4350 env = kzalloc(sizeof(*env), GFP_KERNEL | __GFP_NOWARN);
4351 if (!env)
4352 return ERR_PTR(-ENOMEM);
4353
4354 log = &env->log;
4355 log->level = BPF_LOG_KERNEL;
4356
4357 btf = kzalloc(sizeof(*btf), GFP_KERNEL | __GFP_NOWARN);
4358 if (!btf) {
4359 err = -ENOMEM;
4360 goto errout;
4361 }
4362 env->btf = btf;
4363
4364 btf->data = __start_BTF;
4365 btf->data_size = __stop_BTF - __start_BTF;
4366
4367 err = btf_parse_hdr(env);
4368 if (err)
4369 goto errout;
4370
4371 btf->nohdr_data = btf->data + btf->hdr.hdr_len;
4372
4373 err = btf_parse_str_sec(env);
4374 if (err)
4375 goto errout;
4376
4377 err = btf_check_all_metas(env);
4378 if (err)
4379 goto errout;
4380
4381
4382 bpf_ctx_convert.t = btf_type_by_id(btf, bpf_ctx_convert_btf_id[0]);
4383
4384
4385 err = btf_vmlinux_map_ids_init(btf, log);
4386 if (err < 0)
4387 goto errout;
4388
4389 bpf_struct_ops_init(btf, log);
4390
4391 btf_verifier_env_free(env);
4392 refcount_set(&btf->refcnt, 1);
4393 return btf;
4394
4395errout:
4396 btf_verifier_env_free(env);
4397 if (btf) {
4398 kvfree(btf->types);
4399 kfree(btf);
4400 }
4401 return ERR_PTR(err);
4402}
4403
4404struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog)
4405{
4406 struct bpf_prog *tgt_prog = prog->aux->dst_prog;
4407
4408 if (tgt_prog) {
4409 return tgt_prog->aux->btf;
4410 } else {
4411 return btf_vmlinux;
4412 }
4413}
4414
4415static bool is_string_ptr(struct btf *btf, const struct btf_type *t)
4416{
4417
4418 t = btf_type_by_id(btf, t->type);
4419
4420
4421 if (BTF_INFO_KIND(t->info) == BTF_KIND_CONST)
4422 t = btf_type_by_id(btf, t->type);
4423
4424
4425 return btf_type_is_int(t) && t->size == 1;
4426}
4427
4428bool btf_ctx_access(int off, int size, enum bpf_access_type type,
4429 const struct bpf_prog *prog,
4430 struct bpf_insn_access_aux *info)
4431{
4432 const struct btf_type *t = prog->aux->attach_func_proto;
4433 struct bpf_prog *tgt_prog = prog->aux->dst_prog;
4434 struct btf *btf = bpf_prog_get_target_btf(prog);
4435 const char *tname = prog->aux->attach_func_name;
4436 struct bpf_verifier_log *log = info->log;
4437 const struct btf_param *args;
4438 u32 nr_args, arg;
4439 int i, ret;
4440
4441 if (off % 8) {
4442 bpf_log(log, "func '%s' offset %d is not multiple of 8\n",
4443 tname, off);
4444 return false;
4445 }
4446 arg = off / 8;
4447 args = (const struct btf_param *)(t + 1);
4448
4449 nr_args = t ? btf_type_vlen(t) : 5;
4450 if (prog->aux->attach_btf_trace) {
4451
4452 args++;
4453 nr_args--;
4454 }
4455
4456 if (arg > nr_args) {
4457 bpf_log(log, "func '%s' doesn't have %d-th argument\n",
4458 tname, arg + 1);
4459 return false;
4460 }
4461
4462 if (arg == nr_args) {
4463 switch (prog->expected_attach_type) {
4464 case BPF_LSM_MAC:
4465 case BPF_TRACE_FEXIT:
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479 if (!t)
4480 return true;
4481 t = btf_type_by_id(btf, t->type);
4482 break;
4483 case BPF_MODIFY_RETURN:
4484
4485
4486
4487 if (!t)
4488 return false;
4489
4490 t = btf_type_skip_modifiers(btf, t->type, NULL);
4491 if (!btf_type_is_small_int(t)) {
4492 bpf_log(log,
4493 "ret type %s not allowed for fmod_ret\n",
4494 btf_kind_str[BTF_INFO_KIND(t->info)]);
4495 return false;
4496 }
4497 break;
4498 default:
4499 bpf_log(log, "func '%s' doesn't have %d-th argument\n",
4500 tname, arg + 1);
4501 return false;
4502 }
4503 } else {
4504 if (!t)
4505
4506 return true;
4507 t = btf_type_by_id(btf, args[arg].type);
4508 }
4509
4510
4511 while (btf_type_is_modifier(t))
4512 t = btf_type_by_id(btf, t->type);
4513 if (btf_type_is_small_int(t) || btf_type_is_enum(t))
4514
4515 return true;
4516 if (!btf_type_is_ptr(t)) {
4517 bpf_log(log,
4518 "func '%s' arg%d '%s' has type %s. Only pointer access is allowed\n",
4519 tname, arg,
4520 __btf_name_by_offset(btf, t->name_off),
4521 btf_kind_str[BTF_INFO_KIND(t->info)]);
4522 return false;
4523 }
4524
4525
4526 for (i = 0; i < prog->aux->ctx_arg_info_size; i++) {
4527 const struct bpf_ctx_arg_aux *ctx_arg_info = &prog->aux->ctx_arg_info[i];
4528
4529 if (ctx_arg_info->offset == off &&
4530 (ctx_arg_info->reg_type == PTR_TO_RDONLY_BUF_OR_NULL ||
4531 ctx_arg_info->reg_type == PTR_TO_RDWR_BUF_OR_NULL)) {
4532 info->reg_type = ctx_arg_info->reg_type;
4533 return true;
4534 }
4535 }
4536
4537 if (t->type == 0)
4538
4539
4540
4541
4542 return true;
4543
4544 if (is_string_ptr(btf, t))
4545 return true;
4546
4547
4548 for (i = 0; i < prog->aux->ctx_arg_info_size; i++) {
4549 const struct bpf_ctx_arg_aux *ctx_arg_info = &prog->aux->ctx_arg_info[i];
4550
4551 if (ctx_arg_info->offset == off) {
4552 info->reg_type = ctx_arg_info->reg_type;
4553 info->btf_id = ctx_arg_info->btf_id;
4554 return true;
4555 }
4556 }
4557
4558 info->reg_type = PTR_TO_BTF_ID;
4559 if (tgt_prog) {
4560 enum bpf_prog_type tgt_type;
4561
4562 if (tgt_prog->type == BPF_PROG_TYPE_EXT)
4563 tgt_type = tgt_prog->aux->saved_dst_prog_type;
4564 else
4565 tgt_type = tgt_prog->type;
4566
4567 ret = btf_translate_to_vmlinux(log, btf, t, tgt_type, arg);
4568 if (ret > 0) {
4569 info->btf_id = ret;
4570 return true;
4571 } else {
4572 return false;
4573 }
4574 }
4575
4576 info->btf_id = t->type;
4577 t = btf_type_by_id(btf, t->type);
4578
4579 while (btf_type_is_modifier(t)) {
4580 info->btf_id = t->type;
4581 t = btf_type_by_id(btf, t->type);
4582 }
4583 if (!btf_type_is_struct(t)) {
4584 bpf_log(log,
4585 "func '%s' arg%d type %s is not a struct\n",
4586 tname, arg, btf_kind_str[BTF_INFO_KIND(t->info)]);
4587 return false;
4588 }
4589 bpf_log(log, "func '%s' arg%d has btf_id %d type %s '%s'\n",
4590 tname, arg, info->btf_id, btf_kind_str[BTF_INFO_KIND(t->info)],
4591 __btf_name_by_offset(btf, t->name_off));
4592 return true;
4593}
4594
4595enum bpf_struct_walk_result {
4596
4597 WALK_SCALAR = 0,
4598 WALK_PTR,
4599 WALK_STRUCT,
4600};
4601
4602static int btf_struct_walk(struct bpf_verifier_log *log,
4603 const struct btf_type *t, int off, int size,
4604 u32 *next_btf_id)
4605{
4606 u32 i, moff, mtrue_end, msize = 0, total_nelems = 0;
4607 const struct btf_type *mtype, *elem_type = NULL;
4608 const struct btf_member *member;
4609 const char *tname, *mname;
4610 u32 vlen, elem_id, mid;
4611
4612again:
4613 tname = __btf_name_by_offset(btf_vmlinux, t->name_off);
4614 if (!btf_type_is_struct(t)) {
4615 bpf_log(log, "Type '%s' is not a struct\n", tname);
4616 return -EINVAL;
4617 }
4618
4619 vlen = btf_type_vlen(t);
4620 if (off + size > t->size) {
4621
4622
4623
4624 struct btf_array *array_elem;
4625
4626 if (vlen == 0)
4627 goto error;
4628
4629 member = btf_type_member(t) + vlen - 1;
4630 mtype = btf_type_skip_modifiers(btf_vmlinux, member->type,
4631 NULL);
4632 if (!btf_type_is_array(mtype))
4633 goto error;
4634
4635 array_elem = (struct btf_array *)(mtype + 1);
4636 if (array_elem->nelems != 0)
4637 goto error;
4638
4639 moff = btf_member_bit_offset(t, member) / 8;
4640 if (off < moff)
4641 goto error;
4642
4643
4644
4645
4646 t = btf_type_skip_modifiers(btf_vmlinux, array_elem->type,
4647 NULL);
4648 if (!btf_type_is_struct(t))
4649 goto error;
4650
4651 off = (off - moff) % t->size;
4652 goto again;
4653
4654error:
4655 bpf_log(log, "access beyond struct %s at off %u size %u\n",
4656 tname, off, size);
4657 return -EACCES;
4658 }
4659
4660 for_each_member(i, t, member) {
4661
4662 moff = btf_member_bit_offset(t, member) / 8;
4663 if (off + size <= moff)
4664
4665 break;
4666
4667 if (btf_member_bitfield_size(t, member)) {
4668 u32 end_bit = btf_member_bit_offset(t, member) +
4669 btf_member_bitfield_size(t, member);
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679 if (off <= moff &&
4680 BITS_ROUNDUP_BYTES(end_bit) <= off + size)
4681 return WALK_SCALAR;
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695 continue;
4696 }
4697
4698
4699 if (off < moff)
4700 break;
4701
4702
4703 mid = member->type;
4704 mtype = btf_type_by_id(btf_vmlinux, member->type);
4705 mname = __btf_name_by_offset(btf_vmlinux, member->name_off);
4706
4707 mtype = __btf_resolve_size(btf_vmlinux, mtype, &msize,
4708 &elem_type, &elem_id, &total_nelems,
4709 &mid);
4710 if (IS_ERR(mtype)) {
4711 bpf_log(log, "field %s doesn't have size\n", mname);
4712 return -EFAULT;
4713 }
4714
4715 mtrue_end = moff + msize;
4716 if (off >= mtrue_end)
4717
4718 continue;
4719
4720 if (btf_type_is_array(mtype)) {
4721 u32 elem_idx;
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764 if (moff == mtrue_end)
4765 continue;
4766
4767 msize /= total_nelems;
4768 elem_idx = (off - moff) / msize;
4769 moff += elem_idx * msize;
4770 mtype = elem_type;
4771 mid = elem_id;
4772 }
4773
4774
4775
4776
4777 if (btf_type_is_struct(mtype)) {
4778
4779 t = mtype;
4780
4781
4782 if (off == moff) {
4783 *next_btf_id = mid;
4784 return WALK_STRUCT;
4785 }
4786
4787
4788 off -= moff;
4789 goto again;
4790 }
4791
4792 if (btf_type_is_ptr(mtype)) {
4793 const struct btf_type *stype;
4794 u32 id;
4795
4796 if (msize != size || off != moff) {
4797 bpf_log(log,
4798 "cannot access ptr member %s with moff %u in struct %s with off %u size %u\n",
4799 mname, moff, tname, off, size);
4800 return -EACCES;
4801 }
4802 stype = btf_type_skip_modifiers(btf_vmlinux, mtype->type, &id);
4803 if (btf_type_is_struct(stype)) {
4804 *next_btf_id = id;
4805 return WALK_PTR;
4806 }
4807 }
4808
4809
4810
4811
4812
4813
4814
4815 if (off + size > mtrue_end) {
4816 bpf_log(log,
4817 "access beyond the end of member %s (mend:%u) in struct %s with off %u size %u\n",
4818 mname, mtrue_end, tname, off, size);
4819 return -EACCES;
4820 }
4821
4822 return WALK_SCALAR;
4823 }
4824 bpf_log(log, "struct %s doesn't have field at offset %d\n", tname, off);
4825 return -EINVAL;
4826}
4827
4828int btf_struct_access(struct bpf_verifier_log *log,
4829 const struct btf_type *t, int off, int size,
4830 enum bpf_access_type atype __maybe_unused,
4831 u32 *next_btf_id)
4832{
4833 int err;
4834 u32 id;
4835
4836 do {
4837 err = btf_struct_walk(log, t, off, size, &id);
4838
4839 switch (err) {
4840 case WALK_PTR:
4841
4842
4843
4844 *next_btf_id = id;
4845 return PTR_TO_BTF_ID;
4846 case WALK_SCALAR:
4847 return SCALAR_VALUE;
4848 case WALK_STRUCT:
4849
4850
4851
4852
4853 t = btf_type_by_id(btf_vmlinux, id);
4854 off = 0;
4855 break;
4856 default:
4857
4858
4859
4860 if (WARN_ONCE(err > 0, "unknown btf_struct_walk return value"))
4861 return -EINVAL;
4862 return err;
4863 }
4864 } while (t);
4865
4866 return -EINVAL;
4867}
4868
4869bool btf_struct_ids_match(struct bpf_verifier_log *log,
4870 int off, u32 id, u32 need_type_id)
4871{
4872 const struct btf_type *type;
4873 int err;
4874
4875
4876 if (need_type_id == id && off == 0)
4877 return true;
4878
4879again:
4880 type = btf_type_by_id(btf_vmlinux, id);
4881 if (!type)
4882 return false;
4883 err = btf_struct_walk(log, type, off, 1, &id);
4884 if (err != WALK_STRUCT)
4885 return false;
4886
4887
4888
4889
4890
4891
4892 if (need_type_id != id) {
4893 off = 0;
4894 goto again;
4895 }
4896
4897 return true;
4898}
4899
4900static int __get_type_size(struct btf *btf, u32 btf_id,
4901 const struct btf_type **bad_type)
4902{
4903 const struct btf_type *t;
4904
4905 if (!btf_id)
4906
4907 return 0;
4908 t = btf_type_by_id(btf, btf_id);
4909 while (t && btf_type_is_modifier(t))
4910 t = btf_type_by_id(btf, t->type);
4911 if (!t) {
4912 *bad_type = btf->types[0];
4913 return -EINVAL;
4914 }
4915 if (btf_type_is_ptr(t))
4916
4917 return sizeof(void *);
4918 if (btf_type_is_int(t) || btf_type_is_enum(t))
4919 return t->size;
4920 *bad_type = t;
4921 return -EINVAL;
4922}
4923
4924int btf_distill_func_proto(struct bpf_verifier_log *log,
4925 struct btf *btf,
4926 const struct btf_type *func,
4927 const char *tname,
4928 struct btf_func_model *m)
4929{
4930 const struct btf_param *args;
4931 const struct btf_type *t;
4932 u32 i, nargs;
4933 int ret;
4934
4935 if (!func) {
4936
4937
4938
4939 for (i = 0; i < 5; i++)
4940 m->arg_size[i] = 8;
4941 m->ret_size = 8;
4942 m->nr_args = 5;
4943 return 0;
4944 }
4945 args = (const struct btf_param *)(func + 1);
4946 nargs = btf_type_vlen(func);
4947 if (nargs >= MAX_BPF_FUNC_ARGS) {
4948 bpf_log(log,
4949 "The function %s has %d arguments. Too many.\n",
4950 tname, nargs);
4951 return -EINVAL;
4952 }
4953 ret = __get_type_size(btf, func->type, &t);
4954 if (ret < 0) {
4955 bpf_log(log,
4956 "The function %s return type %s is unsupported.\n",
4957 tname, btf_kind_str[BTF_INFO_KIND(t->info)]);
4958 return -EINVAL;
4959 }
4960 m->ret_size = ret;
4961
4962 for (i = 0; i < nargs; i++) {
4963 ret = __get_type_size(btf, args[i].type, &t);
4964 if (ret < 0) {
4965 bpf_log(log,
4966 "The function %s arg%d type %s is unsupported.\n",
4967 tname, i, btf_kind_str[BTF_INFO_KIND(t->info)]);
4968 return -EINVAL;
4969 }
4970 m->arg_size[i] = ret;
4971 }
4972 m->nr_args = nargs;
4973 return 0;
4974}
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984static int btf_check_func_type_match(struct bpf_verifier_log *log,
4985 struct btf *btf1, const struct btf_type *t1,
4986 struct btf *btf2, const struct btf_type *t2)
4987{
4988 const struct btf_param *args1, *args2;
4989 const char *fn1, *fn2, *s1, *s2;
4990 u32 nargs1, nargs2, i;
4991
4992 fn1 = btf_name_by_offset(btf1, t1->name_off);
4993 fn2 = btf_name_by_offset(btf2, t2->name_off);
4994
4995 if (btf_func_linkage(t1) != BTF_FUNC_GLOBAL) {
4996 bpf_log(log, "%s() is not a global function\n", fn1);
4997 return -EINVAL;
4998 }
4999 if (btf_func_linkage(t2) != BTF_FUNC_GLOBAL) {
5000 bpf_log(log, "%s() is not a global function\n", fn2);
5001 return -EINVAL;
5002 }
5003
5004 t1 = btf_type_by_id(btf1, t1->type);
5005 if (!t1 || !btf_type_is_func_proto(t1))
5006 return -EFAULT;
5007 t2 = btf_type_by_id(btf2, t2->type);
5008 if (!t2 || !btf_type_is_func_proto(t2))
5009 return -EFAULT;
5010
5011 args1 = (const struct btf_param *)(t1 + 1);
5012 nargs1 = btf_type_vlen(t1);
5013 args2 = (const struct btf_param *)(t2 + 1);
5014 nargs2 = btf_type_vlen(t2);
5015
5016 if (nargs1 != nargs2) {
5017 bpf_log(log, "%s() has %d args while %s() has %d args\n",
5018 fn1, nargs1, fn2, nargs2);
5019 return -EINVAL;
5020 }
5021
5022 t1 = btf_type_skip_modifiers(btf1, t1->type, NULL);
5023 t2 = btf_type_skip_modifiers(btf2, t2->type, NULL);
5024 if (t1->info != t2->info) {
5025 bpf_log(log,
5026 "Return type %s of %s() doesn't match type %s of %s()\n",
5027 btf_type_str(t1), fn1,
5028 btf_type_str(t2), fn2);
5029 return -EINVAL;
5030 }
5031
5032 for (i = 0; i < nargs1; i++) {
5033 t1 = btf_type_skip_modifiers(btf1, args1[i].type, NULL);
5034 t2 = btf_type_skip_modifiers(btf2, args2[i].type, NULL);
5035
5036 if (t1->info != t2->info) {
5037 bpf_log(log, "arg%d in %s() is %s while %s() has %s\n",
5038 i, fn1, btf_type_str(t1),
5039 fn2, btf_type_str(t2));
5040 return -EINVAL;
5041 }
5042 if (btf_type_has_size(t1) && t1->size != t2->size) {
5043 bpf_log(log,
5044 "arg%d in %s() has size %d while %s() has %d\n",
5045 i, fn1, t1->size,
5046 fn2, t2->size);
5047 return -EINVAL;
5048 }
5049
5050
5051
5052
5053
5054 if (btf_type_is_int(t1) || btf_type_is_enum(t1))
5055 continue;
5056 if (!btf_type_is_ptr(t1)) {
5057 bpf_log(log,
5058 "arg%d in %s() has unrecognized type\n",
5059 i, fn1);
5060 return -EINVAL;
5061 }
5062 t1 = btf_type_skip_modifiers(btf1, t1->type, NULL);
5063 t2 = btf_type_skip_modifiers(btf2, t2->type, NULL);
5064 if (!btf_type_is_struct(t1)) {
5065 bpf_log(log,
5066 "arg%d in %s() is not a pointer to context\n",
5067 i, fn1);
5068 return -EINVAL;
5069 }
5070 if (!btf_type_is_struct(t2)) {
5071 bpf_log(log,
5072 "arg%d in %s() is not a pointer to context\n",
5073 i, fn2);
5074 return -EINVAL;
5075 }
5076
5077
5078
5079
5080
5081
5082 s1 = btf_name_by_offset(btf1, t1->name_off);
5083 s2 = btf_name_by_offset(btf2, t2->name_off);
5084 if (strcmp(s1, s2)) {
5085 bpf_log(log,
5086 "arg%d %s(struct %s *) doesn't match %s(struct %s *)\n",
5087 i, fn1, s1, fn2, s2);
5088 return -EINVAL;
5089 }
5090 }
5091 return 0;
5092}
5093
5094
5095int btf_check_type_match(struct bpf_verifier_log *log, const struct bpf_prog *prog,
5096 struct btf *btf2, const struct btf_type *t2)
5097{
5098 struct btf *btf1 = prog->aux->btf;
5099 const struct btf_type *t1;
5100 u32 btf_id = 0;
5101
5102 if (!prog->aux->func_info) {
5103 bpf_log(log, "Program extension requires BTF\n");
5104 return -EINVAL;
5105 }
5106
5107 btf_id = prog->aux->func_info[0].type_id;
5108 if (!btf_id)
5109 return -EFAULT;
5110
5111 t1 = btf_type_by_id(btf1, btf_id);
5112 if (!t1 || !btf_type_is_func(t1))
5113 return -EFAULT;
5114
5115 return btf_check_func_type_match(log, btf1, t1, btf2, t2);
5116}
5117
5118
5119
5120
5121
5122
5123
5124
5125int btf_check_func_arg_match(struct bpf_verifier_env *env, int subprog,
5126 struct bpf_reg_state *reg)
5127{
5128 struct bpf_verifier_log *log = &env->log;
5129 struct bpf_prog *prog = env->prog;
5130 struct btf *btf = prog->aux->btf;
5131 const struct btf_param *args;
5132 const struct btf_type *t;
5133 u32 i, nargs, btf_id;
5134 const char *tname;
5135
5136 if (!prog->aux->func_info)
5137 return -EINVAL;
5138
5139 btf_id = prog->aux->func_info[subprog].type_id;
5140 if (!btf_id)
5141 return -EFAULT;
5142
5143 if (prog->aux->func_info_aux[subprog].unreliable)
5144 return -EINVAL;
5145
5146 t = btf_type_by_id(btf, btf_id);
5147 if (!t || !btf_type_is_func(t)) {
5148
5149
5150
5151 bpf_log(log, "BTF of func#%d doesn't point to KIND_FUNC\n",
5152 subprog);
5153 return -EFAULT;
5154 }
5155 tname = btf_name_by_offset(btf, t->name_off);
5156
5157 t = btf_type_by_id(btf, t->type);
5158 if (!t || !btf_type_is_func_proto(t)) {
5159 bpf_log(log, "Invalid BTF of func %s\n", tname);
5160 return -EFAULT;
5161 }
5162 args = (const struct btf_param *)(t + 1);
5163 nargs = btf_type_vlen(t);
5164 if (nargs > 5) {
5165 bpf_log(log, "Function %s has %d > 5 args\n", tname, nargs);
5166 goto out;
5167 }
5168
5169
5170
5171 for (i = 0; i < nargs; i++) {
5172 t = btf_type_by_id(btf, args[i].type);
5173 while (btf_type_is_modifier(t))
5174 t = btf_type_by_id(btf, t->type);
5175 if (btf_type_is_int(t) || btf_type_is_enum(t)) {
5176 if (reg[i + 1].type == SCALAR_VALUE)
5177 continue;
5178 bpf_log(log, "R%d is not a scalar\n", i + 1);
5179 goto out;
5180 }
5181 if (btf_type_is_ptr(t)) {
5182 if (reg[i + 1].type == SCALAR_VALUE) {
5183 bpf_log(log, "R%d is not a pointer\n", i + 1);
5184 goto out;
5185 }
5186
5187
5188
5189 if (btf_get_prog_ctx_type(log, btf, t, prog->type, i)) {
5190 if (reg[i + 1].type != PTR_TO_CTX) {
5191 bpf_log(log,
5192 "arg#%d expected pointer to ctx, but got %s\n",
5193 i, btf_kind_str[BTF_INFO_KIND(t->info)]);
5194 goto out;
5195 }
5196 if (check_ctx_reg(env, ®[i + 1], i + 1))
5197 goto out;
5198 continue;
5199 }
5200 }
5201 bpf_log(log, "Unrecognized arg#%d type %s\n",
5202 i, btf_kind_str[BTF_INFO_KIND(t->info)]);
5203 goto out;
5204 }
5205 return 0;
5206out:
5207
5208
5209
5210
5211 prog->aux->func_info_aux[subprog].unreliable = true;
5212 return -EINVAL;
5213}
5214
5215
5216
5217
5218
5219
5220
5221
5222int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog,
5223 struct bpf_reg_state *reg)
5224{
5225 struct bpf_verifier_log *log = &env->log;
5226 struct bpf_prog *prog = env->prog;
5227 enum bpf_prog_type prog_type = prog->type;
5228 struct btf *btf = prog->aux->btf;
5229 const struct btf_param *args;
5230 const struct btf_type *t;
5231 u32 i, nargs, btf_id;
5232 const char *tname;
5233
5234 if (!prog->aux->func_info ||
5235 prog->aux->func_info_aux[subprog].linkage != BTF_FUNC_GLOBAL) {
5236 bpf_log(log, "Verifier bug\n");
5237 return -EFAULT;
5238 }
5239
5240 btf_id = prog->aux->func_info[subprog].type_id;
5241 if (!btf_id) {
5242 bpf_log(log, "Global functions need valid BTF\n");
5243 return -EFAULT;
5244 }
5245
5246 t = btf_type_by_id(btf, btf_id);
5247 if (!t || !btf_type_is_func(t)) {
5248
5249
5250
5251 bpf_log(log, "BTF of func#%d doesn't point to KIND_FUNC\n",
5252 subprog);
5253 return -EFAULT;
5254 }
5255 tname = btf_name_by_offset(btf, t->name_off);
5256
5257 if (log->level & BPF_LOG_LEVEL)
5258 bpf_log(log, "Validating %s() func#%d...\n",
5259 tname, subprog);
5260
5261 if (prog->aux->func_info_aux[subprog].unreliable) {
5262 bpf_log(log, "Verifier bug in function %s()\n", tname);
5263 return -EFAULT;
5264 }
5265 if (prog_type == BPF_PROG_TYPE_EXT)
5266 prog_type = prog->aux->dst_prog->type;
5267
5268 t = btf_type_by_id(btf, t->type);
5269 if (!t || !btf_type_is_func_proto(t)) {
5270 bpf_log(log, "Invalid type of function %s()\n", tname);
5271 return -EFAULT;
5272 }
5273 args = (const struct btf_param *)(t + 1);
5274 nargs = btf_type_vlen(t);
5275 if (nargs > 5) {
5276 bpf_log(log, "Global function %s() with %d > 5 args. Buggy compiler.\n",
5277 tname, nargs);
5278 return -EINVAL;
5279 }
5280
5281 t = btf_type_by_id(btf, t->type);
5282 while (btf_type_is_modifier(t))
5283 t = btf_type_by_id(btf, t->type);
5284 if (!btf_type_is_int(t) && !btf_type_is_enum(t)) {
5285 bpf_log(log,
5286 "Global function %s() doesn't return scalar. Only those are supported.\n",
5287 tname);
5288 return -EINVAL;
5289 }
5290
5291
5292
5293 for (i = 0; i < nargs; i++) {
5294 t = btf_type_by_id(btf, args[i].type);
5295 while (btf_type_is_modifier(t))
5296 t = btf_type_by_id(btf, t->type);
5297 if (btf_type_is_int(t) || btf_type_is_enum(t)) {
5298 reg[i + 1].type = SCALAR_VALUE;
5299 continue;
5300 }
5301 if (btf_type_is_ptr(t) &&
5302 btf_get_prog_ctx_type(log, btf, t, prog_type, i)) {
5303 reg[i + 1].type = PTR_TO_CTX;
5304 continue;
5305 }
5306 bpf_log(log, "Arg#%d type %s in %s() is not supported yet.\n",
5307 i, btf_kind_str[BTF_INFO_KIND(t->info)], tname);
5308 return -EINVAL;
5309 }
5310 return 0;
5311}
5312
5313static void btf_type_show(const struct btf *btf, u32 type_id, void *obj,
5314 struct btf_show *show)
5315{
5316 const struct btf_type *t = btf_type_by_id(btf, type_id);
5317
5318 show->btf = btf;
5319 memset(&show->state, 0, sizeof(show->state));
5320 memset(&show->obj, 0, sizeof(show->obj));
5321
5322 btf_type_ops(t)->show(btf, t, type_id, obj, 0, show);
5323}
5324
5325static void btf_seq_show(struct btf_show *show, const char *fmt,
5326 va_list args)
5327{
5328 seq_vprintf((struct seq_file *)show->target, fmt, args);
5329}
5330
5331int btf_type_seq_show_flags(const struct btf *btf, u32 type_id,
5332 void *obj, struct seq_file *m, u64 flags)
5333{
5334 struct btf_show sseq;
5335
5336 sseq.target = m;
5337 sseq.showfn = btf_seq_show;
5338 sseq.flags = flags;
5339
5340 btf_type_show(btf, type_id, obj, &sseq);
5341
5342 return sseq.state.status;
5343}
5344
5345void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj,
5346 struct seq_file *m)
5347{
5348 (void) btf_type_seq_show_flags(btf, type_id, obj, m,
5349 BTF_SHOW_NONAME | BTF_SHOW_COMPACT |
5350 BTF_SHOW_ZERO | BTF_SHOW_UNSAFE);
5351}
5352
5353struct btf_show_snprintf {
5354 struct btf_show show;
5355 int len_left;
5356 int len;
5357};
5358
5359static void btf_snprintf_show(struct btf_show *show, const char *fmt,
5360 va_list args)
5361{
5362 struct btf_show_snprintf *ssnprintf = (struct btf_show_snprintf *)show;
5363 int len;
5364
5365 len = vsnprintf(show->target, ssnprintf->len_left, fmt, args);
5366
5367 if (len < 0) {
5368 ssnprintf->len_left = 0;
5369 ssnprintf->len = len;
5370 } else if (len > ssnprintf->len_left) {
5371
5372 ssnprintf->len_left = 0;
5373 ssnprintf->len += len;
5374 } else {
5375 ssnprintf->len_left -= len;
5376 ssnprintf->len += len;
5377 show->target += len;
5378 }
5379}
5380
5381int btf_type_snprintf_show(const struct btf *btf, u32 type_id, void *obj,
5382 char *buf, int len, u64 flags)
5383{
5384 struct btf_show_snprintf ssnprintf;
5385
5386 ssnprintf.show.target = buf;
5387 ssnprintf.show.flags = flags;
5388 ssnprintf.show.showfn = btf_snprintf_show;
5389 ssnprintf.len_left = len;
5390 ssnprintf.len = 0;
5391
5392 btf_type_show(btf, type_id, obj, (struct btf_show *)&ssnprintf);
5393
5394
5395 if (ssnprintf.show.state.status)
5396 return ssnprintf.show.state.status;
5397
5398
5399 return ssnprintf.len;
5400}
5401
5402#ifdef CONFIG_PROC_FS
5403static void bpf_btf_show_fdinfo(struct seq_file *m, struct file *filp)
5404{
5405 const struct btf *btf = filp->private_data;
5406
5407 seq_printf(m, "btf_id:\t%u\n", btf->id);
5408}
5409#endif
5410
5411static int btf_release(struct inode *inode, struct file *filp)
5412{
5413 btf_put(filp->private_data);
5414 return 0;
5415}
5416
5417const struct file_operations btf_fops = {
5418#ifdef CONFIG_PROC_FS
5419 .show_fdinfo = bpf_btf_show_fdinfo,
5420#endif
5421 .release = btf_release,
5422};
5423
5424static int __btf_new_fd(struct btf *btf)
5425{
5426 return anon_inode_getfd("btf", &btf_fops, btf, O_RDONLY | O_CLOEXEC);
5427}
5428
5429int btf_new_fd(const union bpf_attr *attr)
5430{
5431 struct btf *btf;
5432 int ret;
5433
5434 btf = btf_parse(u64_to_user_ptr(attr->btf),
5435 attr->btf_size, attr->btf_log_level,
5436 u64_to_user_ptr(attr->btf_log_buf),
5437 attr->btf_log_size);
5438 if (IS_ERR(btf))
5439 return PTR_ERR(btf);
5440
5441 ret = btf_alloc_id(btf);
5442 if (ret) {
5443 btf_free(btf);
5444 return ret;
5445 }
5446
5447
5448
5449
5450
5451
5452
5453 ret = __btf_new_fd(btf);
5454 if (ret < 0)
5455 btf_put(btf);
5456
5457 return ret;
5458}
5459
5460struct btf *btf_get_by_fd(int fd)
5461{
5462 struct btf *btf;
5463 struct fd f;
5464
5465 f = fdget(fd);
5466
5467 if (!f.file)
5468 return ERR_PTR(-EBADF);
5469
5470 if (f.file->f_op != &btf_fops) {
5471 fdput(f);
5472 return ERR_PTR(-EINVAL);
5473 }
5474
5475 btf = f.file->private_data;
5476 refcount_inc(&btf->refcnt);
5477 fdput(f);
5478
5479 return btf;
5480}
5481
5482int btf_get_info_by_fd(const struct btf *btf,
5483 const union bpf_attr *attr,
5484 union bpf_attr __user *uattr)
5485{
5486 struct bpf_btf_info __user *uinfo;
5487 struct bpf_btf_info info;
5488 u32 info_copy, btf_copy;
5489 void __user *ubtf;
5490 u32 uinfo_len;
5491
5492 uinfo = u64_to_user_ptr(attr->info.info);
5493 uinfo_len = attr->info.info_len;
5494
5495 info_copy = min_t(u32, uinfo_len, sizeof(info));
5496 memset(&info, 0, sizeof(info));
5497 if (copy_from_user(&info, uinfo, info_copy))
5498 return -EFAULT;
5499
5500 info.id = btf->id;
5501 ubtf = u64_to_user_ptr(info.btf);
5502 btf_copy = min_t(u32, btf->data_size, info.btf_size);
5503 if (copy_to_user(ubtf, btf->data, btf_copy))
5504 return -EFAULT;
5505 info.btf_size = btf->data_size;
5506
5507 if (copy_to_user(uinfo, &info, info_copy) ||
5508 put_user(info_copy, &uattr->info.info_len))
5509 return -EFAULT;
5510
5511 return 0;
5512}
5513
5514int btf_get_fd_by_id(u32 id)
5515{
5516 struct btf *btf;
5517 int fd;
5518
5519 rcu_read_lock();
5520 btf = idr_find(&btf_idr, id);
5521 if (!btf || !refcount_inc_not_zero(&btf->refcnt))
5522 btf = ERR_PTR(-ENOENT);
5523 rcu_read_unlock();
5524
5525 if (IS_ERR(btf))
5526 return PTR_ERR(btf);
5527
5528 fd = __btf_new_fd(btf);
5529 if (fd < 0)
5530 btf_put(btf);
5531
5532 return fd;
5533}
5534
5535u32 btf_id(const struct btf *btf)
5536{
5537 return btf->id;
5538}
5539
5540static int btf_id_cmp_func(const void *a, const void *b)
5541{
5542 const int *pa = a, *pb = b;
5543
5544 return *pa - *pb;
5545}
5546
5547bool btf_id_set_contains(const struct btf_id_set *set, u32 id)
5548{
5549 return bsearch(&id, set->ids, set->cnt, sizeof(u32), btf_id_cmp_func) != NULL;
5550}
5551