1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (c) 2018 Facebook */
3
4 #include <uapi/linux/btf.h>
5 #include <uapi/linux/bpf.h>
6 #include <uapi/linux/android_fuse.h>
7 #include <uapi/linux/bpf_perf_event.h>
8 #include <uapi/linux/types.h>
9 #include <linux/seq_file.h>
10 #include <linux/compiler.h>
11 #include <linux/ctype.h>
12 #include <linux/errno.h>
13 #include <linux/slab.h>
14 #include <linux/anon_inodes.h>
15 #include <linux/file.h>
16 #include <linux/uaccess.h>
17 #include <linux/kernel.h>
18 #include <linux/idr.h>
19 #include <linux/sort.h>
20 #include <linux/bpf_verifier.h>
21 #include <linux/btf.h>
22 #include <linux/btf_ids.h>
23 #include <linux/skmsg.h>
24 #include <linux/perf_event.h>
25 #include <linux/bsearch.h>
26 #include <linux/kobject.h>
27 #include <linux/sysfs.h>
28 #include <net/sock.h>
29
30 /* BTF (BPF Type Format) is the meta data format which describes
31 * the data types of BPF program/map. Hence, it basically focus
32 * on the C programming language which the modern BPF is primary
33 * using.
34 *
35 * ELF Section:
36 * ~~~~~~~~~~~
37 * The BTF data is stored under the ".BTF" ELF section
38 *
39 * struct btf_type:
40 * ~~~~~~~~~~~~~~~
41 * Each 'struct btf_type' object describes a C data type.
42 * Depending on the type it is describing, a 'struct btf_type'
43 * object may be followed by more data. F.e.
44 * To describe an array, 'struct btf_type' is followed by
45 * 'struct btf_array'.
46 *
47 * 'struct btf_type' and any extra data following it are
48 * 4 bytes aligned.
49 *
50 * Type section:
51 * ~~~~~~~~~~~~~
52 * The BTF type section contains a list of 'struct btf_type' objects.
53 * Each one describes a C type. Recall from the above section
54 * that a 'struct btf_type' object could be immediately followed by extra
55 * data in order to describe some particular C types.
56 *
57 * type_id:
58 * ~~~~~~~
59 * Each btf_type object is identified by a type_id. The type_id
60 * is implicitly implied by the location of the btf_type object in
61 * the BTF type section. The first one has type_id 1. The second
62 * one has type_id 2...etc. Hence, an earlier btf_type has
63 * a smaller type_id.
64 *
65 * A btf_type object may refer to another btf_type object by using
66 * type_id (i.e. the "type" in the "struct btf_type").
67 *
68 * NOTE that we cannot assume any reference-order.
69 * A btf_type object can refer to an earlier btf_type object
70 * but it can also refer to a later btf_type object.
71 *
72 * For example, to describe "const void *". A btf_type
73 * object describing "const" may refer to another btf_type
74 * object describing "void *". This type-reference is done
75 * by specifying type_id:
76 *
77 * [1] CONST (anon) type_id=2
78 * [2] PTR (anon) type_id=0
79 *
80 * The above is the btf_verifier debug log:
81 * - Each line started with "[?]" is a btf_type object
82 * - [?] is the type_id of the btf_type object.
83 * - CONST/PTR is the BTF_KIND_XXX
84 * - "(anon)" is the name of the type. It just
85 * happens that CONST and PTR has no name.
86 * - type_id=XXX is the 'u32 type' in btf_type
87 *
88 * NOTE: "void" has type_id 0
89 *
90 * String section:
91 * ~~~~~~~~~~~~~~
92 * The BTF string section contains the names used by the type section.
93 * Each string is referred by an "offset" from the beginning of the
94 * string section.
95 *
96 * Each string is '\0' terminated.
97 *
98 * The first character in the string section must be '\0'
99 * which is used to mean 'anonymous'. Some btf_type may not
100 * have a name.
101 */
102
103 /* BTF verification:
104 *
105 * To verify BTF data, two passes are needed.
106 *
107 * Pass #1
108 * ~~~~~~~
109 * The first pass is to collect all btf_type objects to
110 * an array: "btf->types".
111 *
112 * Depending on the C type that a btf_type is describing,
113 * a btf_type may be followed by extra data. We don't know
114 * how many btf_type is there, and more importantly we don't
115 * know where each btf_type is located in the type section.
116 *
117 * Without knowing the location of each type_id, most verifications
118 * cannot be done. e.g. an earlier btf_type may refer to a later
119 * btf_type (recall the "const void *" above), so we cannot
120 * check this type-reference in the first pass.
121 *
122 * In the first pass, it still does some verifications (e.g.
123 * checking the name is a valid offset to the string section).
124 *
125 * Pass #2
126 * ~~~~~~~
127 * The main focus is to resolve a btf_type that is referring
128 * to another type.
129 *
130 * We have to ensure the referring type:
131 * 1) does exist in the BTF (i.e. in btf->types[])
132 * 2) does not cause a loop:
133 * struct A {
134 * struct B b;
135 * };
136 *
137 * struct B {
138 * struct A a;
139 * };
140 *
141 * btf_type_needs_resolve() decides if a btf_type needs
142 * to be resolved.
143 *
144 * The needs_resolve type implements the "resolve()" ops which
145 * essentially does a DFS and detects backedge.
146 *
147 * During resolve (or DFS), different C types have different
148 * "RESOLVED" conditions.
149 *
150 * When resolving a BTF_KIND_STRUCT, we need to resolve all its
151 * members because a member is always referring to another
152 * type. A struct's member can be treated as "RESOLVED" if
153 * it is referring to a BTF_KIND_PTR. Otherwise, the
154 * following valid C struct would be rejected:
155 *
156 * struct A {
157 * int m;
158 * struct A *a;
159 * };
160 *
161 * When resolving a BTF_KIND_PTR, it needs to keep resolving if
162 * it is referring to another BTF_KIND_PTR. Otherwise, we cannot
163 * detect a pointer loop, e.g.:
164 * BTF_KIND_CONST -> BTF_KIND_PTR -> BTF_KIND_CONST -> BTF_KIND_PTR +
165 * ^ |
166 * +-----------------------------------------+
167 *
168 */
169
170 #define BITS_PER_U128 (sizeof(u64) * BITS_PER_BYTE * 2)
171 #define BITS_PER_BYTE_MASK (BITS_PER_BYTE - 1)
172 #define BITS_PER_BYTE_MASKED(bits) ((bits) & BITS_PER_BYTE_MASK)
173 #define BITS_ROUNDDOWN_BYTES(bits) ((bits) >> 3)
174 #define BITS_ROUNDUP_BYTES(bits) \
175 (BITS_ROUNDDOWN_BYTES(bits) + !!BITS_PER_BYTE_MASKED(bits))
176
177 #define BTF_INFO_MASK 0x9f00ffff
178 #define BTF_INT_MASK 0x0fffffff
179 #define BTF_TYPE_ID_VALID(type_id) ((type_id) <= BTF_MAX_TYPE)
180 #define BTF_STR_OFFSET_VALID(name_off) ((name_off) <= BTF_MAX_NAME_OFFSET)
181
182 /* 16MB for 64k structs and each has 16 members and
183 * a few MB spaces for the string section.
184 * The hard limit is S32_MAX.
185 */
186 #define BTF_MAX_SIZE (16 * 1024 * 1024)
187
188 #define for_each_member_from(i, from, struct_type, member) \
189 for (i = from, member = btf_type_member(struct_type) + from; \
190 i < btf_type_vlen(struct_type); \
191 i++, member++)
192
193 #define for_each_vsi_from(i, from, struct_type, member) \
194 for (i = from, member = btf_type_var_secinfo(struct_type) + from; \
195 i < btf_type_vlen(struct_type); \
196 i++, member++)
197
198 DEFINE_IDR(btf_idr);
199 DEFINE_SPINLOCK(btf_idr_lock);
200
201 struct btf {
202 void *data;
203 struct btf_type **types;
204 u32 *resolved_ids;
205 u32 *resolved_sizes;
206 const char *strings;
207 void *nohdr_data;
208 struct btf_header hdr;
209 u32 nr_types; /* includes VOID for base BTF */
210 u32 types_size;
211 u32 data_size;
212 refcount_t refcnt;
213 u32 id;
214 struct rcu_head rcu;
215
216 /* split BTF support */
217 struct btf *base_btf;
218 u32 start_id; /* first type ID in this BTF (0 for base BTF) */
219 u32 start_str_off; /* first string offset (0 for base BTF) */
220 char name[MODULE_NAME_LEN];
221 bool kernel_btf;
222 };
223
224 enum verifier_phase {
225 CHECK_META,
226 CHECK_TYPE,
227 };
228
229 struct resolve_vertex {
230 const struct btf_type *t;
231 u32 type_id;
232 u16 next_member;
233 };
234
235 enum visit_state {
236 NOT_VISITED,
237 VISITED,
238 RESOLVED,
239 };
240
241 enum resolve_mode {
242 RESOLVE_TBD, /* To Be Determined */
243 RESOLVE_PTR, /* Resolving for Pointer */
244 RESOLVE_STRUCT_OR_ARRAY, /* Resolving for struct/union
245 * or array
246 */
247 };
248
249 #define MAX_RESOLVE_DEPTH 32
250
251 struct btf_sec_info {
252 u32 off;
253 u32 len;
254 };
255
256 struct btf_verifier_env {
257 struct btf *btf;
258 u8 *visit_states;
259 struct resolve_vertex stack[MAX_RESOLVE_DEPTH];
260 struct bpf_verifier_log log;
261 u32 log_type_id;
262 u32 top_stack;
263 enum verifier_phase phase;
264 enum resolve_mode resolve_mode;
265 };
266
267 static const char * const btf_kind_str[NR_BTF_KINDS] = {
268 [BTF_KIND_UNKN] = "UNKNOWN",
269 [BTF_KIND_INT] = "INT",
270 [BTF_KIND_PTR] = "PTR",
271 [BTF_KIND_ARRAY] = "ARRAY",
272 [BTF_KIND_STRUCT] = "STRUCT",
273 [BTF_KIND_UNION] = "UNION",
274 [BTF_KIND_ENUM] = "ENUM",
275 [BTF_KIND_FWD] = "FWD",
276 [BTF_KIND_TYPEDEF] = "TYPEDEF",
277 [BTF_KIND_VOLATILE] = "VOLATILE",
278 [BTF_KIND_CONST] = "CONST",
279 [BTF_KIND_RESTRICT] = "RESTRICT",
280 [BTF_KIND_FUNC] = "FUNC",
281 [BTF_KIND_FUNC_PROTO] = "FUNC_PROTO",
282 [BTF_KIND_VAR] = "VAR",
283 [BTF_KIND_DATASEC] = "DATASEC",
284 [BTF_KIND_FLOAT] = "FLOAT",
285 };
286
btf_type_str(const struct btf_type * t)287 const char *btf_type_str(const struct btf_type *t)
288 {
289 return btf_kind_str[BTF_INFO_KIND(t->info)];
290 }
291
292 /* Chunk size we use in safe copy of data to be shown. */
293 #define BTF_SHOW_OBJ_SAFE_SIZE 32
294
295 /*
296 * This is the maximum size of a base type value (equivalent to a
297 * 128-bit int); if we are at the end of our safe buffer and have
298 * less than 16 bytes space we can't be assured of being able
299 * to copy the next type safely, so in such cases we will initiate
300 * a new copy.
301 */
302 #define BTF_SHOW_OBJ_BASE_TYPE_SIZE 16
303
304 /* Type name size */
305 #define BTF_SHOW_NAME_SIZE 80
306
307 /*
308 * Common data to all BTF show operations. Private show functions can add
309 * their own data to a structure containing a struct btf_show and consult it
310 * in the show callback. See btf_type_show() below.
311 *
312 * One challenge with showing nested data is we want to skip 0-valued
313 * data, but in order to figure out whether a nested object is all zeros
314 * we need to walk through it. As a result, we need to make two passes
315 * when handling structs, unions and arrays; the first path simply looks
316 * for nonzero data, while the second actually does the display. The first
317 * pass is signalled by show->state.depth_check being set, and if we
318 * encounter a non-zero value we set show->state.depth_to_show to
319 * the depth at which we encountered it. When we have completed the
320 * first pass, we will know if anything needs to be displayed if
321 * depth_to_show > depth. See btf_[struct,array]_show() for the
322 * implementation of this.
323 *
324 * Another problem is we want to ensure the data for display is safe to
325 * access. To support this, the anonymous "struct {} obj" tracks the data
326 * object and our safe copy of it. We copy portions of the data needed
327 * to the object "copy" buffer, but because its size is limited to
328 * BTF_SHOW_OBJ_COPY_LEN bytes, multiple copies may be required as we
329 * traverse larger objects for display.
330 *
331 * The various data type show functions all start with a call to
332 * btf_show_start_type() which returns a pointer to the safe copy
333 * of the data needed (or if BTF_SHOW_UNSAFE is specified, to the
334 * raw data itself). btf_show_obj_safe() is responsible for
335 * using copy_from_kernel_nofault() to update the safe data if necessary
336 * as we traverse the object's data. skbuff-like semantics are
337 * used:
338 *
339 * - obj.head points to the start of the toplevel object for display
340 * - obj.size is the size of the toplevel object
341 * - obj.data points to the current point in the original data at
342 * which our safe data starts. obj.data will advance as we copy
343 * portions of the data.
344 *
345 * In most cases a single copy will suffice, but larger data structures
346 * such as "struct task_struct" will require many copies. The logic in
347 * btf_show_obj_safe() handles the logic that determines if a new
348 * copy_from_kernel_nofault() is needed.
349 */
350 struct btf_show {
351 u64 flags;
352 void *target; /* target of show operation (seq file, buffer) */
353 void (*showfn)(struct btf_show *show, const char *fmt, va_list args);
354 const struct btf *btf;
355 /* below are used during iteration */
356 struct {
357 u8 depth;
358 u8 depth_to_show;
359 u8 depth_check;
360 u8 array_member:1,
361 array_terminated:1;
362 u16 array_encoding;
363 u32 type_id;
364 int status; /* non-zero for error */
365 const struct btf_type *type;
366 const struct btf_member *member;
367 char name[BTF_SHOW_NAME_SIZE]; /* space for member name/type */
368 } state;
369 struct {
370 u32 size;
371 void *head;
372 void *data;
373 u8 safe[BTF_SHOW_OBJ_SAFE_SIZE];
374 } obj;
375 };
376
377 struct btf_kind_operations {
378 s32 (*check_meta)(struct btf_verifier_env *env,
379 const struct btf_type *t,
380 u32 meta_left);
381 int (*resolve)(struct btf_verifier_env *env,
382 const struct resolve_vertex *v);
383 int (*check_member)(struct btf_verifier_env *env,
384 const struct btf_type *struct_type,
385 const struct btf_member *member,
386 const struct btf_type *member_type);
387 int (*check_kflag_member)(struct btf_verifier_env *env,
388 const struct btf_type *struct_type,
389 const struct btf_member *member,
390 const struct btf_type *member_type);
391 void (*log_details)(struct btf_verifier_env *env,
392 const struct btf_type *t);
393 void (*show)(const struct btf *btf, const struct btf_type *t,
394 u32 type_id, void *data, u8 bits_offsets,
395 struct btf_show *show);
396 };
397
398 static const struct btf_kind_operations * const kind_ops[NR_BTF_KINDS];
399 static struct btf_type btf_void;
400
401 static int btf_resolve(struct btf_verifier_env *env,
402 const struct btf_type *t, u32 type_id);
403
btf_type_is_modifier(const struct btf_type * t)404 static bool btf_type_is_modifier(const struct btf_type *t)
405 {
406 /* Some of them is not strictly a C modifier
407 * but they are grouped into the same bucket
408 * for BTF concern:
409 * A type (t) that refers to another
410 * type through t->type AND its size cannot
411 * be determined without following the t->type.
412 *
413 * ptr does not fall into this bucket
414 * because its size is always sizeof(void *).
415 */
416 switch (BTF_INFO_KIND(t->info)) {
417 case BTF_KIND_TYPEDEF:
418 case BTF_KIND_VOLATILE:
419 case BTF_KIND_CONST:
420 case BTF_KIND_RESTRICT:
421 return true;
422 }
423
424 return false;
425 }
426
btf_type_is_void(const struct btf_type * t)427 bool btf_type_is_void(const struct btf_type *t)
428 {
429 return t == &btf_void;
430 }
431
btf_type_is_fwd(const struct btf_type * t)432 static bool btf_type_is_fwd(const struct btf_type *t)
433 {
434 return BTF_INFO_KIND(t->info) == BTF_KIND_FWD;
435 }
436
btf_type_nosize(const struct btf_type * t)437 static bool btf_type_nosize(const struct btf_type *t)
438 {
439 return btf_type_is_void(t) || btf_type_is_fwd(t) ||
440 btf_type_is_func(t) || btf_type_is_func_proto(t);
441 }
442
btf_type_nosize_or_null(const struct btf_type * t)443 static bool btf_type_nosize_or_null(const struct btf_type *t)
444 {
445 return !t || btf_type_nosize(t);
446 }
447
__btf_type_is_struct(const struct btf_type * t)448 static bool __btf_type_is_struct(const struct btf_type *t)
449 {
450 return BTF_INFO_KIND(t->info) == BTF_KIND_STRUCT;
451 }
452
btf_type_is_array(const struct btf_type * t)453 static bool btf_type_is_array(const struct btf_type *t)
454 {
455 return BTF_INFO_KIND(t->info) == BTF_KIND_ARRAY;
456 }
457
btf_type_is_datasec(const struct btf_type * t)458 static bool btf_type_is_datasec(const struct btf_type *t)
459 {
460 return BTF_INFO_KIND(t->info) == BTF_KIND_DATASEC;
461 }
462
btf_nr_types(const struct btf * btf)463 u32 btf_nr_types(const struct btf *btf)
464 {
465 u32 total = 0;
466
467 while (btf) {
468 total += btf->nr_types;
469 btf = btf->base_btf;
470 }
471
472 return total;
473 }
474
btf_find_by_name_kind(const struct btf * btf,const char * name,u8 kind)475 s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind)
476 {
477 const struct btf_type *t;
478 const char *tname;
479 u32 i, total;
480
481 total = btf_nr_types(btf);
482 for (i = 1; i < total; i++) {
483 t = btf_type_by_id(btf, i);
484 if (BTF_INFO_KIND(t->info) != kind)
485 continue;
486
487 tname = btf_name_by_offset(btf, t->name_off);
488 if (!strcmp(tname, name))
489 return i;
490 }
491
492 return -ENOENT;
493 }
494
btf_type_skip_modifiers(const struct btf * btf,u32 id,u32 * res_id)495 const struct btf_type *btf_type_skip_modifiers(const struct btf *btf,
496 u32 id, u32 *res_id)
497 {
498 const struct btf_type *t = btf_type_by_id(btf, id);
499
500 while (btf_type_is_modifier(t)) {
501 id = t->type;
502 t = btf_type_by_id(btf, t->type);
503 }
504
505 if (res_id)
506 *res_id = id;
507
508 return t;
509 }
510
btf_type_resolve_ptr(const struct btf * btf,u32 id,u32 * res_id)511 const struct btf_type *btf_type_resolve_ptr(const struct btf *btf,
512 u32 id, u32 *res_id)
513 {
514 const struct btf_type *t;
515
516 t = btf_type_skip_modifiers(btf, id, NULL);
517 if (!btf_type_is_ptr(t))
518 return NULL;
519
520 return btf_type_skip_modifiers(btf, t->type, res_id);
521 }
522
btf_type_resolve_func_ptr(const struct btf * btf,u32 id,u32 * res_id)523 const struct btf_type *btf_type_resolve_func_ptr(const struct btf *btf,
524 u32 id, u32 *res_id)
525 {
526 const struct btf_type *ptype;
527
528 ptype = btf_type_resolve_ptr(btf, id, res_id);
529 if (ptype && btf_type_is_func_proto(ptype))
530 return ptype;
531
532 return NULL;
533 }
534
535 /* Types that act only as a source, not sink or intermediate
536 * type when resolving.
537 */
btf_type_is_resolve_source_only(const struct btf_type * t)538 static bool btf_type_is_resolve_source_only(const struct btf_type *t)
539 {
540 return btf_type_is_var(t) ||
541 btf_type_is_datasec(t);
542 }
543
544 /* What types need to be resolved?
545 *
546 * btf_type_is_modifier() is an obvious one.
547 *
548 * btf_type_is_struct() because its member refers to
549 * another type (through member->type).
550 *
551 * btf_type_is_var() because the variable refers to
552 * another type. btf_type_is_datasec() holds multiple
553 * btf_type_is_var() types that need resolving.
554 *
555 * btf_type_is_array() because its element (array->type)
556 * refers to another type. Array can be thought of a
557 * special case of struct while array just has the same
558 * member-type repeated by array->nelems of times.
559 */
btf_type_needs_resolve(const struct btf_type * t)560 static bool btf_type_needs_resolve(const struct btf_type *t)
561 {
562 return btf_type_is_modifier(t) ||
563 btf_type_is_ptr(t) ||
564 btf_type_is_struct(t) ||
565 btf_type_is_array(t) ||
566 btf_type_is_var(t) ||
567 btf_type_is_datasec(t);
568 }
569
570 /* t->size can be used */
btf_type_has_size(const struct btf_type * t)571 static bool btf_type_has_size(const struct btf_type *t)
572 {
573 switch (BTF_INFO_KIND(t->info)) {
574 case BTF_KIND_INT:
575 case BTF_KIND_STRUCT:
576 case BTF_KIND_UNION:
577 case BTF_KIND_ENUM:
578 case BTF_KIND_DATASEC:
579 case BTF_KIND_FLOAT:
580 return true;
581 }
582
583 return false;
584 }
585
btf_int_encoding_str(u8 encoding)586 static const char *btf_int_encoding_str(u8 encoding)
587 {
588 if (encoding == 0)
589 return "(none)";
590 else if (encoding == BTF_INT_SIGNED)
591 return "SIGNED";
592 else if (encoding == BTF_INT_CHAR)
593 return "CHAR";
594 else if (encoding == BTF_INT_BOOL)
595 return "BOOL";
596 else
597 return "UNKN";
598 }
599
btf_type_int(const struct btf_type * t)600 static u32 btf_type_int(const struct btf_type *t)
601 {
602 return *(u32 *)(t + 1);
603 }
604
btf_type_array(const struct btf_type * t)605 static const struct btf_array *btf_type_array(const struct btf_type *t)
606 {
607 return (const struct btf_array *)(t + 1);
608 }
609
btf_type_enum(const struct btf_type * t)610 static const struct btf_enum *btf_type_enum(const struct btf_type *t)
611 {
612 return (const struct btf_enum *)(t + 1);
613 }
614
btf_type_var(const struct btf_type * t)615 static const struct btf_var *btf_type_var(const struct btf_type *t)
616 {
617 return (const struct btf_var *)(t + 1);
618 }
619
btf_type_ops(const struct btf_type * t)620 static const struct btf_kind_operations *btf_type_ops(const struct btf_type *t)
621 {
622 return kind_ops[BTF_INFO_KIND(t->info)];
623 }
624
btf_name_offset_valid(const struct btf * btf,u32 offset)625 static bool btf_name_offset_valid(const struct btf *btf, u32 offset)
626 {
627 if (!BTF_STR_OFFSET_VALID(offset))
628 return false;
629
630 while (offset < btf->start_str_off)
631 btf = btf->base_btf;
632
633 offset -= btf->start_str_off;
634 return offset < btf->hdr.str_len;
635 }
636
__btf_name_char_ok(char c,bool first)637 static bool __btf_name_char_ok(char c, bool first)
638 {
639 if ((first ? !isalpha(c) :
640 !isalnum(c)) &&
641 c != '_' &&
642 c != '.')
643 return false;
644 return true;
645 }
646
btf_str_by_offset(const struct btf * btf,u32 offset)647 static const char *btf_str_by_offset(const struct btf *btf, u32 offset)
648 {
649 while (offset < btf->start_str_off)
650 btf = btf->base_btf;
651
652 offset -= btf->start_str_off;
653 if (offset < btf->hdr.str_len)
654 return &btf->strings[offset];
655
656 return NULL;
657 }
658
__btf_name_valid(const struct btf * btf,u32 offset)659 static bool __btf_name_valid(const struct btf *btf, u32 offset)
660 {
661 /* offset must be valid */
662 const char *src = btf_str_by_offset(btf, offset);
663 const char *src_limit;
664
665 if (!__btf_name_char_ok(*src, true))
666 return false;
667
668 /* set a limit on identifier length */
669 src_limit = src + KSYM_NAME_LEN;
670 src++;
671 while (*src && src < src_limit) {
672 if (!__btf_name_char_ok(*src, false))
673 return false;
674 src++;
675 }
676
677 return !*src;
678 }
679
btf_name_valid_identifier(const struct btf * btf,u32 offset)680 static bool btf_name_valid_identifier(const struct btf *btf, u32 offset)
681 {
682 return __btf_name_valid(btf, offset);
683 }
684
btf_name_valid_section(const struct btf * btf,u32 offset)685 static bool btf_name_valid_section(const struct btf *btf, u32 offset)
686 {
687 return __btf_name_valid(btf, offset);
688 }
689
__btf_name_by_offset(const struct btf * btf,u32 offset)690 static const char *__btf_name_by_offset(const struct btf *btf, u32 offset)
691 {
692 const char *name;
693
694 if (!offset)
695 return "(anon)";
696
697 name = btf_str_by_offset(btf, offset);
698 return name ?: "(invalid-name-offset)";
699 }
700
btf_name_by_offset(const struct btf * btf,u32 offset)701 const char *btf_name_by_offset(const struct btf *btf, u32 offset)
702 {
703 return btf_str_by_offset(btf, offset);
704 }
705
btf_type_by_id(const struct btf * btf,u32 type_id)706 const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id)
707 {
708 while (type_id < btf->start_id)
709 btf = btf->base_btf;
710
711 type_id -= btf->start_id;
712 if (type_id >= btf->nr_types)
713 return NULL;
714 return btf->types[type_id];
715 }
716
717 /*
718 * Regular int is not a bit field and it must be either
719 * u8/u16/u32/u64 or __int128.
720 */
btf_type_int_is_regular(const struct btf_type * t)721 static bool btf_type_int_is_regular(const struct btf_type *t)
722 {
723 u8 nr_bits, nr_bytes;
724 u32 int_data;
725
726 int_data = btf_type_int(t);
727 nr_bits = BTF_INT_BITS(int_data);
728 nr_bytes = BITS_ROUNDUP_BYTES(nr_bits);
729 if (BITS_PER_BYTE_MASKED(nr_bits) ||
730 BTF_INT_OFFSET(int_data) ||
731 (nr_bytes != sizeof(u8) && nr_bytes != sizeof(u16) &&
732 nr_bytes != sizeof(u32) && nr_bytes != sizeof(u64) &&
733 nr_bytes != (2 * sizeof(u64)))) {
734 return false;
735 }
736
737 return true;
738 }
739
740 /*
741 * Check that given struct member is a regular int with expected
742 * offset and size.
743 */
btf_member_is_reg_int(const struct btf * btf,const struct btf_type * s,const struct btf_member * m,u32 expected_offset,u32 expected_size)744 bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s,
745 const struct btf_member *m,
746 u32 expected_offset, u32 expected_size)
747 {
748 const struct btf_type *t;
749 u32 id, int_data;
750 u8 nr_bits;
751
752 id = m->type;
753 t = btf_type_id_size(btf, &id, NULL);
754 if (!t || !btf_type_is_int(t))
755 return false;
756
757 int_data = btf_type_int(t);
758 nr_bits = BTF_INT_BITS(int_data);
759 if (btf_type_kflag(s)) {
760 u32 bitfield_size = BTF_MEMBER_BITFIELD_SIZE(m->offset);
761 u32 bit_offset = BTF_MEMBER_BIT_OFFSET(m->offset);
762
763 /* if kflag set, int should be a regular int and
764 * bit offset should be at byte boundary.
765 */
766 return !bitfield_size &&
767 BITS_ROUNDUP_BYTES(bit_offset) == expected_offset &&
768 BITS_ROUNDUP_BYTES(nr_bits) == expected_size;
769 }
770
771 if (BTF_INT_OFFSET(int_data) ||
772 BITS_PER_BYTE_MASKED(m->offset) ||
773 BITS_ROUNDUP_BYTES(m->offset) != expected_offset ||
774 BITS_PER_BYTE_MASKED(nr_bits) ||
775 BITS_ROUNDUP_BYTES(nr_bits) != expected_size)
776 return false;
777
778 return true;
779 }
780
781 /* Similar to btf_type_skip_modifiers() but does not skip typedefs. */
btf_type_skip_qualifiers(const struct btf * btf,u32 id)782 static const struct btf_type *btf_type_skip_qualifiers(const struct btf *btf,
783 u32 id)
784 {
785 const struct btf_type *t = btf_type_by_id(btf, id);
786
787 while (btf_type_is_modifier(t) &&
788 BTF_INFO_KIND(t->info) != BTF_KIND_TYPEDEF) {
789 t = btf_type_by_id(btf, t->type);
790 }
791
792 return t;
793 }
794
795 #define BTF_SHOW_MAX_ITER 10
796
797 #define BTF_KIND_BIT(kind) (1ULL << kind)
798
799 /*
800 * Populate show->state.name with type name information.
801 * Format of type name is
802 *
803 * [.member_name = ] (type_name)
804 */
btf_show_name(struct btf_show * show)805 static const char *btf_show_name(struct btf_show *show)
806 {
807 /* BTF_MAX_ITER array suffixes "[]" */
808 const char *array_suffixes = "[][][][][][][][][][]";
809 const char *array_suffix = &array_suffixes[strlen(array_suffixes)];
810 /* BTF_MAX_ITER pointer suffixes "*" */
811 const char *ptr_suffixes = "**********";
812 const char *ptr_suffix = &ptr_suffixes[strlen(ptr_suffixes)];
813 const char *name = NULL, *prefix = "", *parens = "";
814 const struct btf_member *m = show->state.member;
815 const struct btf_type *t = show->state.type;
816 const struct btf_array *array;
817 u32 id = show->state.type_id;
818 const char *member = NULL;
819 bool show_member = false;
820 u64 kinds = 0;
821 int i;
822
823 show->state.name[0] = '\0';
824
825 /*
826 * Don't show type name if we're showing an array member;
827 * in that case we show the array type so don't need to repeat
828 * ourselves for each member.
829 */
830 if (show->state.array_member)
831 return "";
832
833 /* Retrieve member name, if any. */
834 if (m) {
835 member = btf_name_by_offset(show->btf, m->name_off);
836 show_member = strlen(member) > 0;
837 id = m->type;
838 }
839
840 /*
841 * Start with type_id, as we have resolved the struct btf_type *
842 * via btf_modifier_show() past the parent typedef to the child
843 * struct, int etc it is defined as. In such cases, the type_id
844 * still represents the starting type while the struct btf_type *
845 * in our show->state points at the resolved type of the typedef.
846 */
847 t = btf_type_by_id(show->btf, id);
848 if (!t)
849 return "";
850
851 /*
852 * The goal here is to build up the right number of pointer and
853 * array suffixes while ensuring the type name for a typedef
854 * is represented. Along the way we accumulate a list of
855 * BTF kinds we have encountered, since these will inform later
856 * display; for example, pointer types will not require an
857 * opening "{" for struct, we will just display the pointer value.
858 *
859 * We also want to accumulate the right number of pointer or array
860 * indices in the format string while iterating until we get to
861 * the typedef/pointee/array member target type.
862 *
863 * We start by pointing at the end of pointer and array suffix
864 * strings; as we accumulate pointers and arrays we move the pointer
865 * or array string backwards so it will show the expected number of
866 * '*' or '[]' for the type. BTF_SHOW_MAX_ITER of nesting of pointers
867 * and/or arrays and typedefs are supported as a precaution.
868 *
869 * We also want to get typedef name while proceeding to resolve
870 * type it points to so that we can add parentheses if it is a
871 * "typedef struct" etc.
872 */
873 for (i = 0; i < BTF_SHOW_MAX_ITER; i++) {
874
875 switch (BTF_INFO_KIND(t->info)) {
876 case BTF_KIND_TYPEDEF:
877 if (!name)
878 name = btf_name_by_offset(show->btf,
879 t->name_off);
880 kinds |= BTF_KIND_BIT(BTF_KIND_TYPEDEF);
881 id = t->type;
882 break;
883 case BTF_KIND_ARRAY:
884 kinds |= BTF_KIND_BIT(BTF_KIND_ARRAY);
885 parens = "[";
886 if (!t)
887 return "";
888 array = btf_type_array(t);
889 if (array_suffix > array_suffixes)
890 array_suffix -= 2;
891 id = array->type;
892 break;
893 case BTF_KIND_PTR:
894 kinds |= BTF_KIND_BIT(BTF_KIND_PTR);
895 if (ptr_suffix > ptr_suffixes)
896 ptr_suffix -= 1;
897 id = t->type;
898 break;
899 default:
900 id = 0;
901 break;
902 }
903 if (!id)
904 break;
905 t = btf_type_skip_qualifiers(show->btf, id);
906 }
907 /* We may not be able to represent this type; bail to be safe */
908 if (i == BTF_SHOW_MAX_ITER)
909 return "";
910
911 if (!name)
912 name = btf_name_by_offset(show->btf, t->name_off);
913
914 switch (BTF_INFO_KIND(t->info)) {
915 case BTF_KIND_STRUCT:
916 case BTF_KIND_UNION:
917 prefix = BTF_INFO_KIND(t->info) == BTF_KIND_STRUCT ?
918 "struct" : "union";
919 /* if it's an array of struct/union, parens is already set */
920 if (!(kinds & (BTF_KIND_BIT(BTF_KIND_ARRAY))))
921 parens = "{";
922 break;
923 case BTF_KIND_ENUM:
924 prefix = "enum";
925 break;
926 default:
927 break;
928 }
929
930 /* pointer does not require parens */
931 if (kinds & BTF_KIND_BIT(BTF_KIND_PTR))
932 parens = "";
933 /* typedef does not require struct/union/enum prefix */
934 if (kinds & BTF_KIND_BIT(BTF_KIND_TYPEDEF))
935 prefix = "";
936
937 if (!name)
938 name = "";
939
940 /* Even if we don't want type name info, we want parentheses etc */
941 if (show->flags & BTF_SHOW_NONAME)
942 snprintf(show->state.name, sizeof(show->state.name), "%s",
943 parens);
944 else
945 snprintf(show->state.name, sizeof(show->state.name),
946 "%s%s%s(%s%s%s%s%s%s)%s",
947 /* first 3 strings comprise ".member = " */
948 show_member ? "." : "",
949 show_member ? member : "",
950 show_member ? " = " : "",
951 /* ...next is our prefix (struct, enum, etc) */
952 prefix,
953 strlen(prefix) > 0 && strlen(name) > 0 ? " " : "",
954 /* ...this is the type name itself */
955 name,
956 /* ...suffixed by the appropriate '*', '[]' suffixes */
957 strlen(ptr_suffix) > 0 ? " " : "", ptr_suffix,
958 array_suffix, parens);
959
960 return show->state.name;
961 }
962
__btf_show_indent(struct btf_show * show)963 static const char *__btf_show_indent(struct btf_show *show)
964 {
965 const char *indents = " ";
966 const char *indent = &indents[strlen(indents)];
967
968 if ((indent - show->state.depth) >= indents)
969 return indent - show->state.depth;
970 return indents;
971 }
972
btf_show_indent(struct btf_show * show)973 static const char *btf_show_indent(struct btf_show *show)
974 {
975 return show->flags & BTF_SHOW_COMPACT ? "" : __btf_show_indent(show);
976 }
977
btf_show_newline(struct btf_show * show)978 static const char *btf_show_newline(struct btf_show *show)
979 {
980 return show->flags & BTF_SHOW_COMPACT ? "" : "\n";
981 }
982
btf_show_delim(struct btf_show * show)983 static const char *btf_show_delim(struct btf_show *show)
984 {
985 if (show->state.depth == 0)
986 return "";
987
988 if ((show->flags & BTF_SHOW_COMPACT) && show->state.type &&
989 BTF_INFO_KIND(show->state.type->info) == BTF_KIND_UNION)
990 return "|";
991
992 return ",";
993 }
994
btf_show(struct btf_show * show,const char * fmt,...)995 __printf(2, 3) static void btf_show(struct btf_show *show, const char *fmt, ...)
996 {
997 va_list args;
998
999 if (!show->state.depth_check) {
1000 va_start(args, fmt);
1001 show->showfn(show, fmt, args);
1002 va_end(args);
1003 }
1004 }
1005
1006 /* Macros are used here as btf_show_type_value[s]() prepends and appends
1007 * format specifiers to the format specifier passed in; these do the work of
1008 * adding indentation, delimiters etc while the caller simply has to specify
1009 * the type value(s) in the format specifier + value(s).
1010 */
1011 #define btf_show_type_value(show, fmt, value) \
1012 do { \
1013 if ((value) != 0 || (show->flags & BTF_SHOW_ZERO) || \
1014 show->state.depth == 0) { \
1015 btf_show(show, "%s%s" fmt "%s%s", \
1016 btf_show_indent(show), \
1017 btf_show_name(show), \
1018 value, btf_show_delim(show), \
1019 btf_show_newline(show)); \
1020 if (show->state.depth > show->state.depth_to_show) \
1021 show->state.depth_to_show = show->state.depth; \
1022 } \
1023 } while (0)
1024
1025 #define btf_show_type_values(show, fmt, ...) \
1026 do { \
1027 btf_show(show, "%s%s" fmt "%s%s", btf_show_indent(show), \
1028 btf_show_name(show), \
1029 __VA_ARGS__, btf_show_delim(show), \
1030 btf_show_newline(show)); \
1031 if (show->state.depth > show->state.depth_to_show) \
1032 show->state.depth_to_show = show->state.depth; \
1033 } while (0)
1034
1035 /* How much is left to copy to safe buffer after @data? */
btf_show_obj_size_left(struct btf_show * show,void * data)1036 static int btf_show_obj_size_left(struct btf_show *show, void *data)
1037 {
1038 return show->obj.head + show->obj.size - data;
1039 }
1040
1041 /* Is object pointed to by @data of @size already copied to our safe buffer? */
btf_show_obj_is_safe(struct btf_show * show,void * data,int size)1042 static bool btf_show_obj_is_safe(struct btf_show *show, void *data, int size)
1043 {
1044 return data >= show->obj.data &&
1045 (data + size) < (show->obj.data + BTF_SHOW_OBJ_SAFE_SIZE);
1046 }
1047
1048 /*
1049 * If object pointed to by @data of @size falls within our safe buffer, return
1050 * the equivalent pointer to the same safe data. Assumes
1051 * copy_from_kernel_nofault() has already happened and our safe buffer is
1052 * populated.
1053 */
__btf_show_obj_safe(struct btf_show * show,void * data,int size)1054 static void *__btf_show_obj_safe(struct btf_show *show, void *data, int size)
1055 {
1056 if (btf_show_obj_is_safe(show, data, size))
1057 return show->obj.safe + (data - show->obj.data);
1058 return NULL;
1059 }
1060
1061 /*
1062 * Return a safe-to-access version of data pointed to by @data.
1063 * We do this by copying the relevant amount of information
1064 * to the struct btf_show obj.safe buffer using copy_from_kernel_nofault().
1065 *
1066 * If BTF_SHOW_UNSAFE is specified, just return data as-is; no
1067 * safe copy is needed.
1068 *
1069 * Otherwise we need to determine if we have the required amount
1070 * of data (determined by the @data pointer and the size of the
1071 * largest base type we can encounter (represented by
1072 * BTF_SHOW_OBJ_BASE_TYPE_SIZE). Having that much data ensures
1073 * that we will be able to print some of the current object,
1074 * and if more is needed a copy will be triggered.
1075 * Some objects such as structs will not fit into the buffer;
1076 * in such cases additional copies when we iterate over their
1077 * members may be needed.
1078 *
1079 * btf_show_obj_safe() is used to return a safe buffer for
1080 * btf_show_start_type(); this ensures that as we recurse into
1081 * nested types we always have safe data for the given type.
1082 * This approach is somewhat wasteful; it's possible for example
1083 * that when iterating over a large union we'll end up copying the
1084 * same data repeatedly, but the goal is safety not performance.
1085 * We use stack data as opposed to per-CPU buffers because the
1086 * iteration over a type can take some time, and preemption handling
1087 * would greatly complicate use of the safe buffer.
1088 */
btf_show_obj_safe(struct btf_show * show,const struct btf_type * t,void * data)1089 static void *btf_show_obj_safe(struct btf_show *show,
1090 const struct btf_type *t,
1091 void *data)
1092 {
1093 const struct btf_type *rt;
1094 int size_left, size;
1095 void *safe = NULL;
1096
1097 if (show->flags & BTF_SHOW_UNSAFE)
1098 return data;
1099
1100 rt = btf_resolve_size(show->btf, t, &size);
1101 if (IS_ERR(rt)) {
1102 show->state.status = PTR_ERR(rt);
1103 return NULL;
1104 }
1105
1106 /*
1107 * Is this toplevel object? If so, set total object size and
1108 * initialize pointers. Otherwise check if we still fall within
1109 * our safe object data.
1110 */
1111 if (show->state.depth == 0) {
1112 show->obj.size = size;
1113 show->obj.head = data;
1114 } else {
1115 /*
1116 * If the size of the current object is > our remaining
1117 * safe buffer we _may_ need to do a new copy. However
1118 * consider the case of a nested struct; it's size pushes
1119 * us over the safe buffer limit, but showing any individual
1120 * struct members does not. In such cases, we don't need
1121 * to initiate a fresh copy yet; however we definitely need
1122 * at least BTF_SHOW_OBJ_BASE_TYPE_SIZE bytes left
1123 * in our buffer, regardless of the current object size.
1124 * The logic here is that as we resolve types we will
1125 * hit a base type at some point, and we need to be sure
1126 * the next chunk of data is safely available to display
1127 * that type info safely. We cannot rely on the size of
1128 * the current object here because it may be much larger
1129 * than our current buffer (e.g. task_struct is 8k).
1130 * All we want to do here is ensure that we can print the
1131 * next basic type, which we can if either
1132 * - the current type size is within the safe buffer; or
1133 * - at least BTF_SHOW_OBJ_BASE_TYPE_SIZE bytes are left in
1134 * the safe buffer.
1135 */
1136 safe = __btf_show_obj_safe(show, data,
1137 min(size,
1138 BTF_SHOW_OBJ_BASE_TYPE_SIZE));
1139 }
1140
1141 /*
1142 * We need a new copy to our safe object, either because we haven't
1143 * yet copied and are initializing safe data, or because the data
1144 * we want falls outside the boundaries of the safe object.
1145 */
1146 if (!safe) {
1147 size_left = btf_show_obj_size_left(show, data);
1148 if (size_left > BTF_SHOW_OBJ_SAFE_SIZE)
1149 size_left = BTF_SHOW_OBJ_SAFE_SIZE;
1150 show->state.status = copy_from_kernel_nofault(show->obj.safe,
1151 data, size_left);
1152 if (!show->state.status) {
1153 show->obj.data = data;
1154 safe = show->obj.safe;
1155 }
1156 }
1157
1158 return safe;
1159 }
1160
1161 /*
1162 * Set the type we are starting to show and return a safe data pointer
1163 * to be used for showing the associated data.
1164 */
btf_show_start_type(struct btf_show * show,const struct btf_type * t,u32 type_id,void * data)1165 static void *btf_show_start_type(struct btf_show *show,
1166 const struct btf_type *t,
1167 u32 type_id, void *data)
1168 {
1169 show->state.type = t;
1170 show->state.type_id = type_id;
1171 show->state.name[0] = '\0';
1172
1173 return btf_show_obj_safe(show, t, data);
1174 }
1175
btf_show_end_type(struct btf_show * show)1176 static void btf_show_end_type(struct btf_show *show)
1177 {
1178 show->state.type = NULL;
1179 show->state.type_id = 0;
1180 show->state.name[0] = '\0';
1181 }
1182
btf_show_start_aggr_type(struct btf_show * show,const struct btf_type * t,u32 type_id,void * data)1183 static void *btf_show_start_aggr_type(struct btf_show *show,
1184 const struct btf_type *t,
1185 u32 type_id, void *data)
1186 {
1187 void *safe_data = btf_show_start_type(show, t, type_id, data);
1188
1189 if (!safe_data)
1190 return safe_data;
1191
1192 btf_show(show, "%s%s%s", btf_show_indent(show),
1193 btf_show_name(show),
1194 btf_show_newline(show));
1195 show->state.depth++;
1196 return safe_data;
1197 }
1198
btf_show_end_aggr_type(struct btf_show * show,const char * suffix)1199 static void btf_show_end_aggr_type(struct btf_show *show,
1200 const char *suffix)
1201 {
1202 show->state.depth--;
1203 btf_show(show, "%s%s%s%s", btf_show_indent(show), suffix,
1204 btf_show_delim(show), btf_show_newline(show));
1205 btf_show_end_type(show);
1206 }
1207
btf_show_start_member(struct btf_show * show,const struct btf_member * m)1208 static void btf_show_start_member(struct btf_show *show,
1209 const struct btf_member *m)
1210 {
1211 show->state.member = m;
1212 }
1213
btf_show_start_array_member(struct btf_show * show)1214 static void btf_show_start_array_member(struct btf_show *show)
1215 {
1216 show->state.array_member = 1;
1217 btf_show_start_member(show, NULL);
1218 }
1219
btf_show_end_member(struct btf_show * show)1220 static void btf_show_end_member(struct btf_show *show)
1221 {
1222 show->state.member = NULL;
1223 }
1224
btf_show_end_array_member(struct btf_show * show)1225 static void btf_show_end_array_member(struct btf_show *show)
1226 {
1227 show->state.array_member = 0;
1228 btf_show_end_member(show);
1229 }
1230
btf_show_start_array_type(struct btf_show * show,const struct btf_type * t,u32 type_id,u16 array_encoding,void * data)1231 static void *btf_show_start_array_type(struct btf_show *show,
1232 const struct btf_type *t,
1233 u32 type_id,
1234 u16 array_encoding,
1235 void *data)
1236 {
1237 show->state.array_encoding = array_encoding;
1238 show->state.array_terminated = 0;
1239 return btf_show_start_aggr_type(show, t, type_id, data);
1240 }
1241
btf_show_end_array_type(struct btf_show * show)1242 static void btf_show_end_array_type(struct btf_show *show)
1243 {
1244 show->state.array_encoding = 0;
1245 show->state.array_terminated = 0;
1246 btf_show_end_aggr_type(show, "]");
1247 }
1248
btf_show_start_struct_type(struct btf_show * show,const struct btf_type * t,u32 type_id,void * data)1249 static void *btf_show_start_struct_type(struct btf_show *show,
1250 const struct btf_type *t,
1251 u32 type_id,
1252 void *data)
1253 {
1254 return btf_show_start_aggr_type(show, t, type_id, data);
1255 }
1256
btf_show_end_struct_type(struct btf_show * show)1257 static void btf_show_end_struct_type(struct btf_show *show)
1258 {
1259 btf_show_end_aggr_type(show, "}");
1260 }
1261
__btf_verifier_log(struct bpf_verifier_log * log,const char * fmt,...)1262 __printf(2, 3) static void __btf_verifier_log(struct bpf_verifier_log *log,
1263 const char *fmt, ...)
1264 {
1265 va_list args;
1266
1267 va_start(args, fmt);
1268 bpf_verifier_vlog(log, fmt, args);
1269 va_end(args);
1270 }
1271
btf_verifier_log(struct btf_verifier_env * env,const char * fmt,...)1272 __printf(2, 3) static void btf_verifier_log(struct btf_verifier_env *env,
1273 const char *fmt, ...)
1274 {
1275 struct bpf_verifier_log *log = &env->log;
1276 va_list args;
1277
1278 if (!bpf_verifier_log_needed(log))
1279 return;
1280
1281 va_start(args, fmt);
1282 bpf_verifier_vlog(log, fmt, args);
1283 va_end(args);
1284 }
1285
__btf_verifier_log_type(struct btf_verifier_env * env,const struct btf_type * t,bool log_details,const char * fmt,...)1286 __printf(4, 5) static void __btf_verifier_log_type(struct btf_verifier_env *env,
1287 const struct btf_type *t,
1288 bool log_details,
1289 const char *fmt, ...)
1290 {
1291 struct bpf_verifier_log *log = &env->log;
1292 u8 kind = BTF_INFO_KIND(t->info);
1293 struct btf *btf = env->btf;
1294 va_list args;
1295
1296 if (!bpf_verifier_log_needed(log))
1297 return;
1298
1299 if (log->level == BPF_LOG_KERNEL) {
1300 /* btf verifier prints all types it is processing via
1301 * btf_verifier_log_type(..., fmt = NULL).
1302 * Skip those prints for in-kernel BTF verification.
1303 */
1304 if (!fmt)
1305 return;
1306
1307 /* Skip logging when loading module BTF with mismatches permitted */
1308 if (env->btf->base_btf && IS_ENABLED(CONFIG_MODULE_ALLOW_BTF_MISMATCH))
1309 return;
1310 }
1311
1312 __btf_verifier_log(log, "[%u] %s %s%s",
1313 env->log_type_id,
1314 btf_kind_str[kind],
1315 __btf_name_by_offset(btf, t->name_off),
1316 log_details ? " " : "");
1317
1318 if (log_details)
1319 btf_type_ops(t)->log_details(env, t);
1320
1321 if (fmt && *fmt) {
1322 __btf_verifier_log(log, " ");
1323 va_start(args, fmt);
1324 bpf_verifier_vlog(log, fmt, args);
1325 va_end(args);
1326 }
1327
1328 __btf_verifier_log(log, "\n");
1329 }
1330
1331 #define btf_verifier_log_type(env, t, ...) \
1332 __btf_verifier_log_type((env), (t), true, __VA_ARGS__)
1333 #define btf_verifier_log_basic(env, t, ...) \
1334 __btf_verifier_log_type((env), (t), false, __VA_ARGS__)
1335
1336 __printf(4, 5)
btf_verifier_log_member(struct btf_verifier_env * env,const struct btf_type * struct_type,const struct btf_member * member,const char * fmt,...)1337 static void btf_verifier_log_member(struct btf_verifier_env *env,
1338 const struct btf_type *struct_type,
1339 const struct btf_member *member,
1340 const char *fmt, ...)
1341 {
1342 struct bpf_verifier_log *log = &env->log;
1343 struct btf *btf = env->btf;
1344 va_list args;
1345
1346 if (!bpf_verifier_log_needed(log))
1347 return;
1348
1349 if (log->level == BPF_LOG_KERNEL) {
1350 if (!fmt)
1351 return;
1352
1353 /* Skip logging when loading module BTF with mismatches permitted */
1354 if (env->btf->base_btf && IS_ENABLED(CONFIG_MODULE_ALLOW_BTF_MISMATCH))
1355 return;
1356 }
1357
1358 /* The CHECK_META phase already did a btf dump.
1359 *
1360 * If member is logged again, it must hit an error in
1361 * parsing this member. It is useful to print out which
1362 * struct this member belongs to.
1363 */
1364 if (env->phase != CHECK_META)
1365 btf_verifier_log_type(env, struct_type, NULL);
1366
1367 if (btf_type_kflag(struct_type))
1368 __btf_verifier_log(log,
1369 "\t%s type_id=%u bitfield_size=%u bits_offset=%u",
1370 __btf_name_by_offset(btf, member->name_off),
1371 member->type,
1372 BTF_MEMBER_BITFIELD_SIZE(member->offset),
1373 BTF_MEMBER_BIT_OFFSET(member->offset));
1374 else
1375 __btf_verifier_log(log, "\t%s type_id=%u bits_offset=%u",
1376 __btf_name_by_offset(btf, member->name_off),
1377 member->type, member->offset);
1378
1379 if (fmt && *fmt) {
1380 __btf_verifier_log(log, " ");
1381 va_start(args, fmt);
1382 bpf_verifier_vlog(log, fmt, args);
1383 va_end(args);
1384 }
1385
1386 __btf_verifier_log(log, "\n");
1387 }
1388
1389 __printf(4, 5)
btf_verifier_log_vsi(struct btf_verifier_env * env,const struct btf_type * datasec_type,const struct btf_var_secinfo * vsi,const char * fmt,...)1390 static void btf_verifier_log_vsi(struct btf_verifier_env *env,
1391 const struct btf_type *datasec_type,
1392 const struct btf_var_secinfo *vsi,
1393 const char *fmt, ...)
1394 {
1395 struct bpf_verifier_log *log = &env->log;
1396 va_list args;
1397
1398 if (!bpf_verifier_log_needed(log))
1399 return;
1400 if (log->level == BPF_LOG_KERNEL && !fmt)
1401 return;
1402 if (env->phase != CHECK_META)
1403 btf_verifier_log_type(env, datasec_type, NULL);
1404
1405 __btf_verifier_log(log, "\t type_id=%u offset=%u size=%u",
1406 vsi->type, vsi->offset, vsi->size);
1407 if (fmt && *fmt) {
1408 __btf_verifier_log(log, " ");
1409 va_start(args, fmt);
1410 bpf_verifier_vlog(log, fmt, args);
1411 va_end(args);
1412 }
1413
1414 __btf_verifier_log(log, "\n");
1415 }
1416
btf_verifier_log_hdr(struct btf_verifier_env * env,u32 btf_data_size)1417 static void btf_verifier_log_hdr(struct btf_verifier_env *env,
1418 u32 btf_data_size)
1419 {
1420 struct bpf_verifier_log *log = &env->log;
1421 const struct btf *btf = env->btf;
1422 const struct btf_header *hdr;
1423
1424 if (!bpf_verifier_log_needed(log))
1425 return;
1426
1427 if (log->level == BPF_LOG_KERNEL)
1428 return;
1429 hdr = &btf->hdr;
1430 __btf_verifier_log(log, "magic: 0x%x\n", hdr->magic);
1431 __btf_verifier_log(log, "version: %u\n", hdr->version);
1432 __btf_verifier_log(log, "flags: 0x%x\n", hdr->flags);
1433 __btf_verifier_log(log, "hdr_len: %u\n", hdr->hdr_len);
1434 __btf_verifier_log(log, "type_off: %u\n", hdr->type_off);
1435 __btf_verifier_log(log, "type_len: %u\n", hdr->type_len);
1436 __btf_verifier_log(log, "str_off: %u\n", hdr->str_off);
1437 __btf_verifier_log(log, "str_len: %u\n", hdr->str_len);
1438 __btf_verifier_log(log, "btf_total_size: %u\n", btf_data_size);
1439 }
1440
btf_add_type(struct btf_verifier_env * env,struct btf_type * t)1441 static int btf_add_type(struct btf_verifier_env *env, struct btf_type *t)
1442 {
1443 struct btf *btf = env->btf;
1444
1445 if (btf->types_size == btf->nr_types) {
1446 /* Expand 'types' array */
1447
1448 struct btf_type **new_types;
1449 u32 expand_by, new_size;
1450
1451 if (btf->start_id + btf->types_size == BTF_MAX_TYPE) {
1452 btf_verifier_log(env, "Exceeded max num of types");
1453 return -E2BIG;
1454 }
1455
1456 expand_by = max_t(u32, btf->types_size >> 2, 16);
1457 new_size = min_t(u32, BTF_MAX_TYPE,
1458 btf->types_size + expand_by);
1459
1460 new_types = kvcalloc(new_size, sizeof(*new_types),
1461 GFP_KERNEL | __GFP_NOWARN);
1462 if (!new_types)
1463 return -ENOMEM;
1464
1465 if (btf->nr_types == 0) {
1466 if (!btf->base_btf) {
1467 /* lazily init VOID type */
1468 new_types[0] = &btf_void;
1469 btf->nr_types++;
1470 }
1471 } else {
1472 memcpy(new_types, btf->types,
1473 sizeof(*btf->types) * btf->nr_types);
1474 }
1475
1476 kvfree(btf->types);
1477 btf->types = new_types;
1478 btf->types_size = new_size;
1479 }
1480
1481 btf->types[btf->nr_types++] = t;
1482
1483 return 0;
1484 }
1485
btf_alloc_id(struct btf * btf)1486 static int btf_alloc_id(struct btf *btf)
1487 {
1488 int id;
1489
1490 idr_preload(GFP_KERNEL);
1491 spin_lock_bh(&btf_idr_lock);
1492 id = idr_alloc_cyclic(&btf_idr, btf, 1, INT_MAX, GFP_ATOMIC);
1493 if (id > 0)
1494 btf->id = id;
1495 spin_unlock_bh(&btf_idr_lock);
1496 idr_preload_end();
1497
1498 if (WARN_ON_ONCE(!id))
1499 return -ENOSPC;
1500
1501 return id > 0 ? 0 : id;
1502 }
1503
btf_free_id(struct btf * btf)1504 static void btf_free_id(struct btf *btf)
1505 {
1506 unsigned long flags;
1507
1508 /*
1509 * In map-in-map, calling map_delete_elem() on outer
1510 * map will call bpf_map_put on the inner map.
1511 * It will then eventually call btf_free_id()
1512 * on the inner map. Some of the map_delete_elem()
1513 * implementation may have irq disabled, so
1514 * we need to use the _irqsave() version instead
1515 * of the _bh() version.
1516 */
1517 spin_lock_irqsave(&btf_idr_lock, flags);
1518 idr_remove(&btf_idr, btf->id);
1519 spin_unlock_irqrestore(&btf_idr_lock, flags);
1520 }
1521
btf_free(struct btf * btf)1522 static void btf_free(struct btf *btf)
1523 {
1524 kvfree(btf->types);
1525 kvfree(btf->resolved_sizes);
1526 kvfree(btf->resolved_ids);
1527 kvfree(btf->data);
1528 kfree(btf);
1529 }
1530
btf_free_rcu(struct rcu_head * rcu)1531 static void btf_free_rcu(struct rcu_head *rcu)
1532 {
1533 struct btf *btf = container_of(rcu, struct btf, rcu);
1534
1535 btf_free(btf);
1536 }
1537
btf_get(struct btf * btf)1538 void btf_get(struct btf *btf)
1539 {
1540 refcount_inc(&btf->refcnt);
1541 }
1542
btf_put(struct btf * btf)1543 void btf_put(struct btf *btf)
1544 {
1545 if (btf && refcount_dec_and_test(&btf->refcnt)) {
1546 btf_free_id(btf);
1547 call_rcu(&btf->rcu, btf_free_rcu);
1548 }
1549 }
1550
env_resolve_init(struct btf_verifier_env * env)1551 static int env_resolve_init(struct btf_verifier_env *env)
1552 {
1553 struct btf *btf = env->btf;
1554 u32 nr_types = btf->nr_types;
1555 u32 *resolved_sizes = NULL;
1556 u32 *resolved_ids = NULL;
1557 u8 *visit_states = NULL;
1558
1559 resolved_sizes = kvcalloc(nr_types, sizeof(*resolved_sizes),
1560 GFP_KERNEL | __GFP_NOWARN);
1561 if (!resolved_sizes)
1562 goto nomem;
1563
1564 resolved_ids = kvcalloc(nr_types, sizeof(*resolved_ids),
1565 GFP_KERNEL | __GFP_NOWARN);
1566 if (!resolved_ids)
1567 goto nomem;
1568
1569 visit_states = kvcalloc(nr_types, sizeof(*visit_states),
1570 GFP_KERNEL | __GFP_NOWARN);
1571 if (!visit_states)
1572 goto nomem;
1573
1574 btf->resolved_sizes = resolved_sizes;
1575 btf->resolved_ids = resolved_ids;
1576 env->visit_states = visit_states;
1577
1578 return 0;
1579
1580 nomem:
1581 kvfree(resolved_sizes);
1582 kvfree(resolved_ids);
1583 kvfree(visit_states);
1584 return -ENOMEM;
1585 }
1586
btf_verifier_env_free(struct btf_verifier_env * env)1587 static void btf_verifier_env_free(struct btf_verifier_env *env)
1588 {
1589 kvfree(env->visit_states);
1590 kfree(env);
1591 }
1592
env_type_is_resolve_sink(const struct btf_verifier_env * env,const struct btf_type * next_type)1593 static bool env_type_is_resolve_sink(const struct btf_verifier_env *env,
1594 const struct btf_type *next_type)
1595 {
1596 switch (env->resolve_mode) {
1597 case RESOLVE_TBD:
1598 /* int, enum or void is a sink */
1599 return !btf_type_needs_resolve(next_type);
1600 case RESOLVE_PTR:
1601 /* int, enum, void, struct, array, func or func_proto is a sink
1602 * for ptr
1603 */
1604 return !btf_type_is_modifier(next_type) &&
1605 !btf_type_is_ptr(next_type);
1606 case RESOLVE_STRUCT_OR_ARRAY:
1607 /* int, enum, void, ptr, func or func_proto is a sink
1608 * for struct and array
1609 */
1610 return !btf_type_is_modifier(next_type) &&
1611 !btf_type_is_array(next_type) &&
1612 !btf_type_is_struct(next_type);
1613 default:
1614 BUG();
1615 }
1616 }
1617
env_type_is_resolved(const struct btf_verifier_env * env,u32 type_id)1618 static bool env_type_is_resolved(const struct btf_verifier_env *env,
1619 u32 type_id)
1620 {
1621 /* base BTF types should be resolved by now */
1622 if (type_id < env->btf->start_id)
1623 return true;
1624
1625 return env->visit_states[type_id - env->btf->start_id] == RESOLVED;
1626 }
1627
env_stack_push(struct btf_verifier_env * env,const struct btf_type * t,u32 type_id)1628 static int env_stack_push(struct btf_verifier_env *env,
1629 const struct btf_type *t, u32 type_id)
1630 {
1631 const struct btf *btf = env->btf;
1632 struct resolve_vertex *v;
1633
1634 if (env->top_stack == MAX_RESOLVE_DEPTH)
1635 return -E2BIG;
1636
1637 if (type_id < btf->start_id
1638 || env->visit_states[type_id - btf->start_id] != NOT_VISITED)
1639 return -EEXIST;
1640
1641 env->visit_states[type_id - btf->start_id] = VISITED;
1642
1643 v = &env->stack[env->top_stack++];
1644 v->t = t;
1645 v->type_id = type_id;
1646 v->next_member = 0;
1647
1648 if (env->resolve_mode == RESOLVE_TBD) {
1649 if (btf_type_is_ptr(t))
1650 env->resolve_mode = RESOLVE_PTR;
1651 else if (btf_type_is_struct(t) || btf_type_is_array(t))
1652 env->resolve_mode = RESOLVE_STRUCT_OR_ARRAY;
1653 }
1654
1655 return 0;
1656 }
1657
env_stack_set_next_member(struct btf_verifier_env * env,u16 next_member)1658 static void env_stack_set_next_member(struct btf_verifier_env *env,
1659 u16 next_member)
1660 {
1661 env->stack[env->top_stack - 1].next_member = next_member;
1662 }
1663
env_stack_pop_resolved(struct btf_verifier_env * env,u32 resolved_type_id,u32 resolved_size)1664 static void env_stack_pop_resolved(struct btf_verifier_env *env,
1665 u32 resolved_type_id,
1666 u32 resolved_size)
1667 {
1668 u32 type_id = env->stack[--(env->top_stack)].type_id;
1669 struct btf *btf = env->btf;
1670
1671 type_id -= btf->start_id; /* adjust to local type id */
1672 btf->resolved_sizes[type_id] = resolved_size;
1673 btf->resolved_ids[type_id] = resolved_type_id;
1674 env->visit_states[type_id] = RESOLVED;
1675 }
1676
env_stack_peak(struct btf_verifier_env * env)1677 static const struct resolve_vertex *env_stack_peak(struct btf_verifier_env *env)
1678 {
1679 return env->top_stack ? &env->stack[env->top_stack - 1] : NULL;
1680 }
1681
1682 /* Resolve the size of a passed-in "type"
1683 *
1684 * type: is an array (e.g. u32 array[x][y])
1685 * return type: type "u32[x][y]", i.e. BTF_KIND_ARRAY,
1686 * *type_size: (x * y * sizeof(u32)). Hence, *type_size always
1687 * corresponds to the return type.
1688 * *elem_type: u32
1689 * *elem_id: id of u32
1690 * *total_nelems: (x * y). Hence, individual elem size is
1691 * (*type_size / *total_nelems)
1692 * *type_id: id of type if it's changed within the function, 0 if not
1693 *
1694 * type: is not an array (e.g. const struct X)
1695 * return type: type "struct X"
1696 * *type_size: sizeof(struct X)
1697 * *elem_type: same as return type ("struct X")
1698 * *elem_id: 0
1699 * *total_nelems: 1
1700 * *type_id: id of type if it's changed within the function, 0 if not
1701 */
1702 static const struct btf_type *
__btf_resolve_size(const struct btf * btf,const struct btf_type * type,u32 * type_size,const struct btf_type ** elem_type,u32 * elem_id,u32 * total_nelems,u32 * type_id)1703 __btf_resolve_size(const struct btf *btf, const struct btf_type *type,
1704 u32 *type_size, const struct btf_type **elem_type,
1705 u32 *elem_id, u32 *total_nelems, u32 *type_id)
1706 {
1707 const struct btf_type *array_type = NULL;
1708 const struct btf_array *array = NULL;
1709 u32 i, size, nelems = 1, id = 0;
1710
1711 for (i = 0; i < MAX_RESOLVE_DEPTH; i++) {
1712 switch (BTF_INFO_KIND(type->info)) {
1713 /* type->size can be used */
1714 case BTF_KIND_INT:
1715 case BTF_KIND_STRUCT:
1716 case BTF_KIND_UNION:
1717 case BTF_KIND_ENUM:
1718 case BTF_KIND_FLOAT:
1719 size = type->size;
1720 goto resolved;
1721
1722 case BTF_KIND_PTR:
1723 size = sizeof(void *);
1724 goto resolved;
1725
1726 /* Modifiers */
1727 case BTF_KIND_TYPEDEF:
1728 case BTF_KIND_VOLATILE:
1729 case BTF_KIND_CONST:
1730 case BTF_KIND_RESTRICT:
1731 id = type->type;
1732 type = btf_type_by_id(btf, type->type);
1733 break;
1734
1735 case BTF_KIND_ARRAY:
1736 if (!array_type)
1737 array_type = type;
1738 array = btf_type_array(type);
1739 if (nelems && array->nelems > U32_MAX / nelems)
1740 return ERR_PTR(-EINVAL);
1741 nelems *= array->nelems;
1742 type = btf_type_by_id(btf, array->type);
1743 break;
1744
1745 /* type without size */
1746 default:
1747 return ERR_PTR(-EINVAL);
1748 }
1749 }
1750
1751 return ERR_PTR(-EINVAL);
1752
1753 resolved:
1754 if (nelems && size > U32_MAX / nelems)
1755 return ERR_PTR(-EINVAL);
1756
1757 *type_size = nelems * size;
1758 if (total_nelems)
1759 *total_nelems = nelems;
1760 if (elem_type)
1761 *elem_type = type;
1762 if (elem_id)
1763 *elem_id = array ? array->type : 0;
1764 if (type_id && id)
1765 *type_id = id;
1766
1767 return array_type ? : type;
1768 }
1769
1770 const struct btf_type *
btf_resolve_size(const struct btf * btf,const struct btf_type * type,u32 * type_size)1771 btf_resolve_size(const struct btf *btf, const struct btf_type *type,
1772 u32 *type_size)
1773 {
1774 return __btf_resolve_size(btf, type, type_size, NULL, NULL, NULL, NULL);
1775 }
1776
btf_resolved_type_id(const struct btf * btf,u32 type_id)1777 static u32 btf_resolved_type_id(const struct btf *btf, u32 type_id)
1778 {
1779 while (type_id < btf->start_id)
1780 btf = btf->base_btf;
1781
1782 return btf->resolved_ids[type_id - btf->start_id];
1783 }
1784
1785 /* The input param "type_id" must point to a needs_resolve type */
btf_type_id_resolve(const struct btf * btf,u32 * type_id)1786 static const struct btf_type *btf_type_id_resolve(const struct btf *btf,
1787 u32 *type_id)
1788 {
1789 *type_id = btf_resolved_type_id(btf, *type_id);
1790 return btf_type_by_id(btf, *type_id);
1791 }
1792
btf_resolved_type_size(const struct btf * btf,u32 type_id)1793 static u32 btf_resolved_type_size(const struct btf *btf, u32 type_id)
1794 {
1795 while (type_id < btf->start_id)
1796 btf = btf->base_btf;
1797
1798 return btf->resolved_sizes[type_id - btf->start_id];
1799 }
1800
btf_type_id_size(const struct btf * btf,u32 * type_id,u32 * ret_size)1801 const struct btf_type *btf_type_id_size(const struct btf *btf,
1802 u32 *type_id, u32 *ret_size)
1803 {
1804 const struct btf_type *size_type;
1805 u32 size_type_id = *type_id;
1806 u32 size = 0;
1807
1808 size_type = btf_type_by_id(btf, size_type_id);
1809 if (btf_type_nosize_or_null(size_type))
1810 return NULL;
1811
1812 if (btf_type_has_size(size_type)) {
1813 size = size_type->size;
1814 } else if (btf_type_is_array(size_type)) {
1815 size = btf_resolved_type_size(btf, size_type_id);
1816 } else if (btf_type_is_ptr(size_type)) {
1817 size = sizeof(void *);
1818 } else {
1819 if (WARN_ON_ONCE(!btf_type_is_modifier(size_type) &&
1820 !btf_type_is_var(size_type)))
1821 return NULL;
1822
1823 size_type_id = btf_resolved_type_id(btf, size_type_id);
1824 size_type = btf_type_by_id(btf, size_type_id);
1825 if (btf_type_nosize_or_null(size_type))
1826 return NULL;
1827 else if (btf_type_has_size(size_type))
1828 size = size_type->size;
1829 else if (btf_type_is_array(size_type))
1830 size = btf_resolved_type_size(btf, size_type_id);
1831 else if (btf_type_is_ptr(size_type))
1832 size = sizeof(void *);
1833 else
1834 return NULL;
1835 }
1836
1837 *type_id = size_type_id;
1838 if (ret_size)
1839 *ret_size = size;
1840
1841 return size_type;
1842 }
1843
btf_df_check_member(struct btf_verifier_env * env,const struct btf_type * struct_type,const struct btf_member * member,const struct btf_type * member_type)1844 static int btf_df_check_member(struct btf_verifier_env *env,
1845 const struct btf_type *struct_type,
1846 const struct btf_member *member,
1847 const struct btf_type *member_type)
1848 {
1849 btf_verifier_log_basic(env, struct_type,
1850 "Unsupported check_member");
1851 return -EINVAL;
1852 }
1853
btf_df_check_kflag_member(struct btf_verifier_env * env,const struct btf_type * struct_type,const struct btf_member * member,const struct btf_type * member_type)1854 static int btf_df_check_kflag_member(struct btf_verifier_env *env,
1855 const struct btf_type *struct_type,
1856 const struct btf_member *member,
1857 const struct btf_type *member_type)
1858 {
1859 btf_verifier_log_basic(env, struct_type,
1860 "Unsupported check_kflag_member");
1861 return -EINVAL;
1862 }
1863
1864 /* Used for ptr, array struct/union and float type members.
1865 * int, enum and modifier types have their specific callback functions.
1866 */
btf_generic_check_kflag_member(struct btf_verifier_env * env,const struct btf_type * struct_type,const struct btf_member * member,const struct btf_type * member_type)1867 static int btf_generic_check_kflag_member(struct btf_verifier_env *env,
1868 const struct btf_type *struct_type,
1869 const struct btf_member *member,
1870 const struct btf_type *member_type)
1871 {
1872 if (BTF_MEMBER_BITFIELD_SIZE(member->offset)) {
1873 btf_verifier_log_member(env, struct_type, member,
1874 "Invalid member bitfield_size");
1875 return -EINVAL;
1876 }
1877
1878 /* bitfield size is 0, so member->offset represents bit offset only.
1879 * It is safe to call non kflag check_member variants.
1880 */
1881 return btf_type_ops(member_type)->check_member(env, struct_type,
1882 member,
1883 member_type);
1884 }
1885
btf_df_resolve(struct btf_verifier_env * env,const struct resolve_vertex * v)1886 static int btf_df_resolve(struct btf_verifier_env *env,
1887 const struct resolve_vertex *v)
1888 {
1889 btf_verifier_log_basic(env, v->t, "Unsupported resolve");
1890 return -EINVAL;
1891 }
1892
btf_df_show(const struct btf * btf,const struct btf_type * t,u32 type_id,void * data,u8 bits_offsets,struct btf_show * show)1893 static void btf_df_show(const struct btf *btf, const struct btf_type *t,
1894 u32 type_id, void *data, u8 bits_offsets,
1895 struct btf_show *show)
1896 {
1897 btf_show(show, "<unsupported kind:%u>", BTF_INFO_KIND(t->info));
1898 }
1899
btf_int_check_member(struct btf_verifier_env * env,const struct btf_type * struct_type,const struct btf_member * member,const struct btf_type * member_type)1900 static int btf_int_check_member(struct btf_verifier_env *env,
1901 const struct btf_type *struct_type,
1902 const struct btf_member *member,
1903 const struct btf_type *member_type)
1904 {
1905 u32 int_data = btf_type_int(member_type);
1906 u32 struct_bits_off = member->offset;
1907 u32 struct_size = struct_type->size;
1908 u32 nr_copy_bits;
1909 u32 bytes_offset;
1910
1911 if (U32_MAX - struct_bits_off < BTF_INT_OFFSET(int_data)) {
1912 btf_verifier_log_member(env, struct_type, member,
1913 "bits_offset exceeds U32_MAX");
1914 return -EINVAL;
1915 }
1916
1917 struct_bits_off += BTF_INT_OFFSET(int_data);
1918 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
1919 nr_copy_bits = BTF_INT_BITS(int_data) +
1920 BITS_PER_BYTE_MASKED(struct_bits_off);
1921
1922 if (nr_copy_bits > BITS_PER_U128) {
1923 btf_verifier_log_member(env, struct_type, member,
1924 "nr_copy_bits exceeds 128");
1925 return -EINVAL;
1926 }
1927
1928 if (struct_size < bytes_offset ||
1929 struct_size - bytes_offset < BITS_ROUNDUP_BYTES(nr_copy_bits)) {
1930 btf_verifier_log_member(env, struct_type, member,
1931 "Member exceeds struct_size");
1932 return -EINVAL;
1933 }
1934
1935 return 0;
1936 }
1937
btf_int_check_kflag_member(struct btf_verifier_env * env,const struct btf_type * struct_type,const struct btf_member * member,const struct btf_type * member_type)1938 static int btf_int_check_kflag_member(struct btf_verifier_env *env,
1939 const struct btf_type *struct_type,
1940 const struct btf_member *member,
1941 const struct btf_type *member_type)
1942 {
1943 u32 struct_bits_off, nr_bits, nr_int_data_bits, bytes_offset;
1944 u32 int_data = btf_type_int(member_type);
1945 u32 struct_size = struct_type->size;
1946 u32 nr_copy_bits;
1947
1948 /* a regular int type is required for the kflag int member */
1949 if (!btf_type_int_is_regular(member_type)) {
1950 btf_verifier_log_member(env, struct_type, member,
1951 "Invalid member base type");
1952 return -EINVAL;
1953 }
1954
1955 /* check sanity of bitfield size */
1956 nr_bits = BTF_MEMBER_BITFIELD_SIZE(member->offset);
1957 struct_bits_off = BTF_MEMBER_BIT_OFFSET(member->offset);
1958 nr_int_data_bits = BTF_INT_BITS(int_data);
1959 if (!nr_bits) {
1960 /* Not a bitfield member, member offset must be at byte
1961 * boundary.
1962 */
1963 if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
1964 btf_verifier_log_member(env, struct_type, member,
1965 "Invalid member offset");
1966 return -EINVAL;
1967 }
1968
1969 nr_bits = nr_int_data_bits;
1970 } else if (nr_bits > nr_int_data_bits) {
1971 btf_verifier_log_member(env, struct_type, member,
1972 "Invalid member bitfield_size");
1973 return -EINVAL;
1974 }
1975
1976 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
1977 nr_copy_bits = nr_bits + BITS_PER_BYTE_MASKED(struct_bits_off);
1978 if (nr_copy_bits > BITS_PER_U128) {
1979 btf_verifier_log_member(env, struct_type, member,
1980 "nr_copy_bits exceeds 128");
1981 return -EINVAL;
1982 }
1983
1984 if (struct_size < bytes_offset ||
1985 struct_size - bytes_offset < BITS_ROUNDUP_BYTES(nr_copy_bits)) {
1986 btf_verifier_log_member(env, struct_type, member,
1987 "Member exceeds struct_size");
1988 return -EINVAL;
1989 }
1990
1991 return 0;
1992 }
1993
btf_int_check_meta(struct btf_verifier_env * env,const struct btf_type * t,u32 meta_left)1994 static s32 btf_int_check_meta(struct btf_verifier_env *env,
1995 const struct btf_type *t,
1996 u32 meta_left)
1997 {
1998 u32 int_data, nr_bits, meta_needed = sizeof(int_data);
1999 u16 encoding;
2000
2001 if (meta_left < meta_needed) {
2002 btf_verifier_log_basic(env, t,
2003 "meta_left:%u meta_needed:%u",
2004 meta_left, meta_needed);
2005 return -EINVAL;
2006 }
2007
2008 if (btf_type_vlen(t)) {
2009 btf_verifier_log_type(env, t, "vlen != 0");
2010 return -EINVAL;
2011 }
2012
2013 if (btf_type_kflag(t)) {
2014 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
2015 return -EINVAL;
2016 }
2017
2018 int_data = btf_type_int(t);
2019 if (int_data & ~BTF_INT_MASK) {
2020 btf_verifier_log_basic(env, t, "Invalid int_data:%x",
2021 int_data);
2022 return -EINVAL;
2023 }
2024
2025 nr_bits = BTF_INT_BITS(int_data) + BTF_INT_OFFSET(int_data);
2026
2027 if (nr_bits > BITS_PER_U128) {
2028 btf_verifier_log_type(env, t, "nr_bits exceeds %zu",
2029 BITS_PER_U128);
2030 return -EINVAL;
2031 }
2032
2033 if (BITS_ROUNDUP_BYTES(nr_bits) > t->size) {
2034 btf_verifier_log_type(env, t, "nr_bits exceeds type_size");
2035 return -EINVAL;
2036 }
2037
2038 /*
2039 * Only one of the encoding bits is allowed and it
2040 * should be sufficient for the pretty print purpose (i.e. decoding).
2041 * Multiple bits can be allowed later if it is found
2042 * to be insufficient.
2043 */
2044 encoding = BTF_INT_ENCODING(int_data);
2045 if (encoding &&
2046 encoding != BTF_INT_SIGNED &&
2047 encoding != BTF_INT_CHAR &&
2048 encoding != BTF_INT_BOOL) {
2049 btf_verifier_log_type(env, t, "Unsupported encoding");
2050 return -ENOTSUPP;
2051 }
2052
2053 btf_verifier_log_type(env, t, NULL);
2054
2055 return meta_needed;
2056 }
2057
btf_int_log(struct btf_verifier_env * env,const struct btf_type * t)2058 static void btf_int_log(struct btf_verifier_env *env,
2059 const struct btf_type *t)
2060 {
2061 int int_data = btf_type_int(t);
2062
2063 btf_verifier_log(env,
2064 "size=%u bits_offset=%u nr_bits=%u encoding=%s",
2065 t->size, BTF_INT_OFFSET(int_data),
2066 BTF_INT_BITS(int_data),
2067 btf_int_encoding_str(BTF_INT_ENCODING(int_data)));
2068 }
2069
btf_int128_print(struct btf_show * show,void * data)2070 static void btf_int128_print(struct btf_show *show, void *data)
2071 {
2072 /* data points to a __int128 number.
2073 * Suppose
2074 * int128_num = *(__int128 *)data;
2075 * The below formulas shows what upper_num and lower_num represents:
2076 * upper_num = int128_num >> 64;
2077 * lower_num = int128_num & 0xffffffffFFFFFFFFULL;
2078 */
2079 u64 upper_num, lower_num;
2080
2081 #ifdef __BIG_ENDIAN_BITFIELD
2082 upper_num = *(u64 *)data;
2083 lower_num = *(u64 *)(data + 8);
2084 #else
2085 upper_num = *(u64 *)(data + 8);
2086 lower_num = *(u64 *)data;
2087 #endif
2088 if (upper_num == 0)
2089 btf_show_type_value(show, "0x%llx", lower_num);
2090 else
2091 btf_show_type_values(show, "0x%llx%016llx", upper_num,
2092 lower_num);
2093 }
2094
btf_int128_shift(u64 * print_num,u16 left_shift_bits,u16 right_shift_bits)2095 static void btf_int128_shift(u64 *print_num, u16 left_shift_bits,
2096 u16 right_shift_bits)
2097 {
2098 u64 upper_num, lower_num;
2099
2100 #ifdef __BIG_ENDIAN_BITFIELD
2101 upper_num = print_num[0];
2102 lower_num = print_num[1];
2103 #else
2104 upper_num = print_num[1];
2105 lower_num = print_num[0];
2106 #endif
2107
2108 /* shake out un-needed bits by shift/or operations */
2109 if (left_shift_bits >= 64) {
2110 upper_num = lower_num << (left_shift_bits - 64);
2111 lower_num = 0;
2112 } else {
2113 upper_num = (upper_num << left_shift_bits) |
2114 (lower_num >> (64 - left_shift_bits));
2115 lower_num = lower_num << left_shift_bits;
2116 }
2117
2118 if (right_shift_bits >= 64) {
2119 lower_num = upper_num >> (right_shift_bits - 64);
2120 upper_num = 0;
2121 } else {
2122 lower_num = (lower_num >> right_shift_bits) |
2123 (upper_num << (64 - right_shift_bits));
2124 upper_num = upper_num >> right_shift_bits;
2125 }
2126
2127 #ifdef __BIG_ENDIAN_BITFIELD
2128 print_num[0] = upper_num;
2129 print_num[1] = lower_num;
2130 #else
2131 print_num[0] = lower_num;
2132 print_num[1] = upper_num;
2133 #endif
2134 }
2135
btf_bitfield_show(void * data,u8 bits_offset,u8 nr_bits,struct btf_show * show)2136 static void btf_bitfield_show(void *data, u8 bits_offset,
2137 u8 nr_bits, struct btf_show *show)
2138 {
2139 u16 left_shift_bits, right_shift_bits;
2140 u8 nr_copy_bytes;
2141 u8 nr_copy_bits;
2142 u64 print_num[2] = {};
2143
2144 nr_copy_bits = nr_bits + bits_offset;
2145 nr_copy_bytes = BITS_ROUNDUP_BYTES(nr_copy_bits);
2146
2147 memcpy(print_num, data, nr_copy_bytes);
2148
2149 #ifdef __BIG_ENDIAN_BITFIELD
2150 left_shift_bits = bits_offset;
2151 #else
2152 left_shift_bits = BITS_PER_U128 - nr_copy_bits;
2153 #endif
2154 right_shift_bits = BITS_PER_U128 - nr_bits;
2155
2156 btf_int128_shift(print_num, left_shift_bits, right_shift_bits);
2157 btf_int128_print(show, print_num);
2158 }
2159
2160
btf_int_bits_show(const struct btf * btf,const struct btf_type * t,void * data,u8 bits_offset,struct btf_show * show)2161 static void btf_int_bits_show(const struct btf *btf,
2162 const struct btf_type *t,
2163 void *data, u8 bits_offset,
2164 struct btf_show *show)
2165 {
2166 u32 int_data = btf_type_int(t);
2167 u8 nr_bits = BTF_INT_BITS(int_data);
2168 u8 total_bits_offset;
2169
2170 /*
2171 * bits_offset is at most 7.
2172 * BTF_INT_OFFSET() cannot exceed 128 bits.
2173 */
2174 total_bits_offset = bits_offset + BTF_INT_OFFSET(int_data);
2175 data += BITS_ROUNDDOWN_BYTES(total_bits_offset);
2176 bits_offset = BITS_PER_BYTE_MASKED(total_bits_offset);
2177 btf_bitfield_show(data, bits_offset, nr_bits, show);
2178 }
2179
btf_int_show(const struct btf * btf,const struct btf_type * t,u32 type_id,void * data,u8 bits_offset,struct btf_show * show)2180 static void btf_int_show(const struct btf *btf, const struct btf_type *t,
2181 u32 type_id, void *data, u8 bits_offset,
2182 struct btf_show *show)
2183 {
2184 u32 int_data = btf_type_int(t);
2185 u8 encoding = BTF_INT_ENCODING(int_data);
2186 bool sign = encoding & BTF_INT_SIGNED;
2187 u8 nr_bits = BTF_INT_BITS(int_data);
2188 void *safe_data;
2189
2190 safe_data = btf_show_start_type(show, t, type_id, data);
2191 if (!safe_data)
2192 return;
2193
2194 if (bits_offset || BTF_INT_OFFSET(int_data) ||
2195 BITS_PER_BYTE_MASKED(nr_bits)) {
2196 btf_int_bits_show(btf, t, safe_data, bits_offset, show);
2197 goto out;
2198 }
2199
2200 switch (nr_bits) {
2201 case 128:
2202 btf_int128_print(show, safe_data);
2203 break;
2204 case 64:
2205 if (sign)
2206 btf_show_type_value(show, "%lld", *(s64 *)safe_data);
2207 else
2208 btf_show_type_value(show, "%llu", *(u64 *)safe_data);
2209 break;
2210 case 32:
2211 if (sign)
2212 btf_show_type_value(show, "%d", *(s32 *)safe_data);
2213 else
2214 btf_show_type_value(show, "%u", *(u32 *)safe_data);
2215 break;
2216 case 16:
2217 if (sign)
2218 btf_show_type_value(show, "%d", *(s16 *)safe_data);
2219 else
2220 btf_show_type_value(show, "%u", *(u16 *)safe_data);
2221 break;
2222 case 8:
2223 if (show->state.array_encoding == BTF_INT_CHAR) {
2224 /* check for null terminator */
2225 if (show->state.array_terminated)
2226 break;
2227 if (*(char *)data == '\0') {
2228 show->state.array_terminated = 1;
2229 break;
2230 }
2231 if (isprint(*(char *)data)) {
2232 btf_show_type_value(show, "'%c'",
2233 *(char *)safe_data);
2234 break;
2235 }
2236 }
2237 if (sign)
2238 btf_show_type_value(show, "%d", *(s8 *)safe_data);
2239 else
2240 btf_show_type_value(show, "%u", *(u8 *)safe_data);
2241 break;
2242 default:
2243 btf_int_bits_show(btf, t, safe_data, bits_offset, show);
2244 break;
2245 }
2246 out:
2247 btf_show_end_type(show);
2248 }
2249
2250 static const struct btf_kind_operations int_ops = {
2251 .check_meta = btf_int_check_meta,
2252 .resolve = btf_df_resolve,
2253 .check_member = btf_int_check_member,
2254 .check_kflag_member = btf_int_check_kflag_member,
2255 .log_details = btf_int_log,
2256 .show = btf_int_show,
2257 };
2258
btf_modifier_check_member(struct btf_verifier_env * env,const struct btf_type * struct_type,const struct btf_member * member,const struct btf_type * member_type)2259 static int btf_modifier_check_member(struct btf_verifier_env *env,
2260 const struct btf_type *struct_type,
2261 const struct btf_member *member,
2262 const struct btf_type *member_type)
2263 {
2264 const struct btf_type *resolved_type;
2265 u32 resolved_type_id = member->type;
2266 struct btf_member resolved_member;
2267 struct btf *btf = env->btf;
2268
2269 resolved_type = btf_type_id_size(btf, &resolved_type_id, NULL);
2270 if (!resolved_type) {
2271 btf_verifier_log_member(env, struct_type, member,
2272 "Invalid member");
2273 return -EINVAL;
2274 }
2275
2276 resolved_member = *member;
2277 resolved_member.type = resolved_type_id;
2278
2279 return btf_type_ops(resolved_type)->check_member(env, struct_type,
2280 &resolved_member,
2281 resolved_type);
2282 }
2283
btf_modifier_check_kflag_member(struct btf_verifier_env * env,const struct btf_type * struct_type,const struct btf_member * member,const struct btf_type * member_type)2284 static int btf_modifier_check_kflag_member(struct btf_verifier_env *env,
2285 const struct btf_type *struct_type,
2286 const struct btf_member *member,
2287 const struct btf_type *member_type)
2288 {
2289 const struct btf_type *resolved_type;
2290 u32 resolved_type_id = member->type;
2291 struct btf_member resolved_member;
2292 struct btf *btf = env->btf;
2293
2294 resolved_type = btf_type_id_size(btf, &resolved_type_id, NULL);
2295 if (!resolved_type) {
2296 btf_verifier_log_member(env, struct_type, member,
2297 "Invalid member");
2298 return -EINVAL;
2299 }
2300
2301 resolved_member = *member;
2302 resolved_member.type = resolved_type_id;
2303
2304 return btf_type_ops(resolved_type)->check_kflag_member(env, struct_type,
2305 &resolved_member,
2306 resolved_type);
2307 }
2308
btf_ptr_check_member(struct btf_verifier_env * env,const struct btf_type * struct_type,const struct btf_member * member,const struct btf_type * member_type)2309 static int btf_ptr_check_member(struct btf_verifier_env *env,
2310 const struct btf_type *struct_type,
2311 const struct btf_member *member,
2312 const struct btf_type *member_type)
2313 {
2314 u32 struct_size, struct_bits_off, bytes_offset;
2315
2316 struct_size = struct_type->size;
2317 struct_bits_off = member->offset;
2318 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
2319
2320 if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
2321 btf_verifier_log_member(env, struct_type, member,
2322 "Member is not byte aligned");
2323 return -EINVAL;
2324 }
2325
2326 if (struct_size - bytes_offset < sizeof(void *)) {
2327 btf_verifier_log_member(env, struct_type, member,
2328 "Member exceeds struct_size");
2329 return -EINVAL;
2330 }
2331
2332 return 0;
2333 }
2334
btf_ref_type_check_meta(struct btf_verifier_env * env,const struct btf_type * t,u32 meta_left)2335 static int btf_ref_type_check_meta(struct btf_verifier_env *env,
2336 const struct btf_type *t,
2337 u32 meta_left)
2338 {
2339 if (btf_type_vlen(t)) {
2340 btf_verifier_log_type(env, t, "vlen != 0");
2341 return -EINVAL;
2342 }
2343
2344 if (btf_type_kflag(t)) {
2345 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
2346 return -EINVAL;
2347 }
2348
2349 if (!BTF_TYPE_ID_VALID(t->type)) {
2350 btf_verifier_log_type(env, t, "Invalid type_id");
2351 return -EINVAL;
2352 }
2353
2354 /* typedef type must have a valid name, and other ref types,
2355 * volatile, const, restrict, should have a null name.
2356 */
2357 if (BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF) {
2358 if (!t->name_off ||
2359 !btf_name_valid_identifier(env->btf, t->name_off)) {
2360 btf_verifier_log_type(env, t, "Invalid name");
2361 return -EINVAL;
2362 }
2363 } else {
2364 if (t->name_off) {
2365 btf_verifier_log_type(env, t, "Invalid name");
2366 return -EINVAL;
2367 }
2368 }
2369
2370 btf_verifier_log_type(env, t, NULL);
2371
2372 return 0;
2373 }
2374
btf_modifier_resolve(struct btf_verifier_env * env,const struct resolve_vertex * v)2375 static int btf_modifier_resolve(struct btf_verifier_env *env,
2376 const struct resolve_vertex *v)
2377 {
2378 const struct btf_type *t = v->t;
2379 const struct btf_type *next_type;
2380 u32 next_type_id = t->type;
2381 struct btf *btf = env->btf;
2382
2383 next_type = btf_type_by_id(btf, next_type_id);
2384 if (!next_type || btf_type_is_resolve_source_only(next_type)) {
2385 btf_verifier_log_type(env, v->t, "Invalid type_id");
2386 return -EINVAL;
2387 }
2388
2389 if (!env_type_is_resolve_sink(env, next_type) &&
2390 !env_type_is_resolved(env, next_type_id))
2391 return env_stack_push(env, next_type, next_type_id);
2392
2393 /* Figure out the resolved next_type_id with size.
2394 * They will be stored in the current modifier's
2395 * resolved_ids and resolved_sizes such that it can
2396 * save us a few type-following when we use it later (e.g. in
2397 * pretty print).
2398 */
2399 if (!btf_type_id_size(btf, &next_type_id, NULL)) {
2400 if (env_type_is_resolved(env, next_type_id))
2401 next_type = btf_type_id_resolve(btf, &next_type_id);
2402
2403 /* "typedef void new_void", "const void"...etc */
2404 if (!btf_type_is_void(next_type) &&
2405 !btf_type_is_fwd(next_type) &&
2406 !btf_type_is_func_proto(next_type)) {
2407 btf_verifier_log_type(env, v->t, "Invalid type_id");
2408 return -EINVAL;
2409 }
2410 }
2411
2412 env_stack_pop_resolved(env, next_type_id, 0);
2413
2414 return 0;
2415 }
2416
btf_var_resolve(struct btf_verifier_env * env,const struct resolve_vertex * v)2417 static int btf_var_resolve(struct btf_verifier_env *env,
2418 const struct resolve_vertex *v)
2419 {
2420 const struct btf_type *next_type;
2421 const struct btf_type *t = v->t;
2422 u32 next_type_id = t->type;
2423 struct btf *btf = env->btf;
2424
2425 next_type = btf_type_by_id(btf, next_type_id);
2426 if (!next_type || btf_type_is_resolve_source_only(next_type)) {
2427 btf_verifier_log_type(env, v->t, "Invalid type_id");
2428 return -EINVAL;
2429 }
2430
2431 if (!env_type_is_resolve_sink(env, next_type) &&
2432 !env_type_is_resolved(env, next_type_id))
2433 return env_stack_push(env, next_type, next_type_id);
2434
2435 if (btf_type_is_modifier(next_type)) {
2436 const struct btf_type *resolved_type;
2437 u32 resolved_type_id;
2438
2439 resolved_type_id = next_type_id;
2440 resolved_type = btf_type_id_resolve(btf, &resolved_type_id);
2441
2442 if (btf_type_is_ptr(resolved_type) &&
2443 !env_type_is_resolve_sink(env, resolved_type) &&
2444 !env_type_is_resolved(env, resolved_type_id))
2445 return env_stack_push(env, resolved_type,
2446 resolved_type_id);
2447 }
2448
2449 /* We must resolve to something concrete at this point, no
2450 * forward types or similar that would resolve to size of
2451 * zero is allowed.
2452 */
2453 if (!btf_type_id_size(btf, &next_type_id, NULL)) {
2454 btf_verifier_log_type(env, v->t, "Invalid type_id");
2455 return -EINVAL;
2456 }
2457
2458 env_stack_pop_resolved(env, next_type_id, 0);
2459
2460 return 0;
2461 }
2462
btf_ptr_resolve(struct btf_verifier_env * env,const struct resolve_vertex * v)2463 static int btf_ptr_resolve(struct btf_verifier_env *env,
2464 const struct resolve_vertex *v)
2465 {
2466 const struct btf_type *next_type;
2467 const struct btf_type *t = v->t;
2468 u32 next_type_id = t->type;
2469 struct btf *btf = env->btf;
2470
2471 next_type = btf_type_by_id(btf, next_type_id);
2472 if (!next_type || btf_type_is_resolve_source_only(next_type)) {
2473 btf_verifier_log_type(env, v->t, "Invalid type_id");
2474 return -EINVAL;
2475 }
2476
2477 if (!env_type_is_resolve_sink(env, next_type) &&
2478 !env_type_is_resolved(env, next_type_id))
2479 return env_stack_push(env, next_type, next_type_id);
2480
2481 /* If the modifier was RESOLVED during RESOLVE_STRUCT_OR_ARRAY,
2482 * the modifier may have stopped resolving when it was resolved
2483 * to a ptr (last-resolved-ptr).
2484 *
2485 * We now need to continue from the last-resolved-ptr to
2486 * ensure the last-resolved-ptr will not referring back to
2487 * the currenct ptr (t).
2488 */
2489 if (btf_type_is_modifier(next_type)) {
2490 const struct btf_type *resolved_type;
2491 u32 resolved_type_id;
2492
2493 resolved_type_id = next_type_id;
2494 resolved_type = btf_type_id_resolve(btf, &resolved_type_id);
2495
2496 if (btf_type_is_ptr(resolved_type) &&
2497 !env_type_is_resolve_sink(env, resolved_type) &&
2498 !env_type_is_resolved(env, resolved_type_id))
2499 return env_stack_push(env, resolved_type,
2500 resolved_type_id);
2501 }
2502
2503 if (!btf_type_id_size(btf, &next_type_id, NULL)) {
2504 if (env_type_is_resolved(env, next_type_id))
2505 next_type = btf_type_id_resolve(btf, &next_type_id);
2506
2507 if (!btf_type_is_void(next_type) &&
2508 !btf_type_is_fwd(next_type) &&
2509 !btf_type_is_func_proto(next_type)) {
2510 btf_verifier_log_type(env, v->t, "Invalid type_id");
2511 return -EINVAL;
2512 }
2513 }
2514
2515 env_stack_pop_resolved(env, next_type_id, 0);
2516
2517 return 0;
2518 }
2519
btf_modifier_show(const struct btf * btf,const struct btf_type * t,u32 type_id,void * data,u8 bits_offset,struct btf_show * show)2520 static void btf_modifier_show(const struct btf *btf,
2521 const struct btf_type *t,
2522 u32 type_id, void *data,
2523 u8 bits_offset, struct btf_show *show)
2524 {
2525 if (btf->resolved_ids)
2526 t = btf_type_id_resolve(btf, &type_id);
2527 else
2528 t = btf_type_skip_modifiers(btf, type_id, NULL);
2529
2530 btf_type_ops(t)->show(btf, t, type_id, data, bits_offset, show);
2531 }
2532
btf_var_show(const struct btf * btf,const struct btf_type * t,u32 type_id,void * data,u8 bits_offset,struct btf_show * show)2533 static void btf_var_show(const struct btf *btf, const struct btf_type *t,
2534 u32 type_id, void *data, u8 bits_offset,
2535 struct btf_show *show)
2536 {
2537 t = btf_type_id_resolve(btf, &type_id);
2538
2539 btf_type_ops(t)->show(btf, t, type_id, data, bits_offset, show);
2540 }
2541
btf_ptr_show(const struct btf * btf,const struct btf_type * t,u32 type_id,void * data,u8 bits_offset,struct btf_show * show)2542 static void btf_ptr_show(const struct btf *btf, const struct btf_type *t,
2543 u32 type_id, void *data, u8 bits_offset,
2544 struct btf_show *show)
2545 {
2546 void *safe_data;
2547
2548 safe_data = btf_show_start_type(show, t, type_id, data);
2549 if (!safe_data)
2550 return;
2551
2552 /* It is a hashed value unless BTF_SHOW_PTR_RAW is specified */
2553 if (show->flags & BTF_SHOW_PTR_RAW)
2554 btf_show_type_value(show, "0x%px", *(void **)safe_data);
2555 else
2556 btf_show_type_value(show, "0x%p", *(void **)safe_data);
2557 btf_show_end_type(show);
2558 }
2559
btf_ref_type_log(struct btf_verifier_env * env,const struct btf_type * t)2560 static void btf_ref_type_log(struct btf_verifier_env *env,
2561 const struct btf_type *t)
2562 {
2563 btf_verifier_log(env, "type_id=%u", t->type);
2564 }
2565
2566 static struct btf_kind_operations modifier_ops = {
2567 .check_meta = btf_ref_type_check_meta,
2568 .resolve = btf_modifier_resolve,
2569 .check_member = btf_modifier_check_member,
2570 .check_kflag_member = btf_modifier_check_kflag_member,
2571 .log_details = btf_ref_type_log,
2572 .show = btf_modifier_show,
2573 };
2574
2575 static struct btf_kind_operations ptr_ops = {
2576 .check_meta = btf_ref_type_check_meta,
2577 .resolve = btf_ptr_resolve,
2578 .check_member = btf_ptr_check_member,
2579 .check_kflag_member = btf_generic_check_kflag_member,
2580 .log_details = btf_ref_type_log,
2581 .show = btf_ptr_show,
2582 };
2583
btf_fwd_check_meta(struct btf_verifier_env * env,const struct btf_type * t,u32 meta_left)2584 static s32 btf_fwd_check_meta(struct btf_verifier_env *env,
2585 const struct btf_type *t,
2586 u32 meta_left)
2587 {
2588 if (btf_type_vlen(t)) {
2589 btf_verifier_log_type(env, t, "vlen != 0");
2590 return -EINVAL;
2591 }
2592
2593 if (t->type) {
2594 btf_verifier_log_type(env, t, "type != 0");
2595 return -EINVAL;
2596 }
2597
2598 /* fwd type must have a valid name */
2599 if (!t->name_off ||
2600 !btf_name_valid_identifier(env->btf, t->name_off)) {
2601 btf_verifier_log_type(env, t, "Invalid name");
2602 return -EINVAL;
2603 }
2604
2605 btf_verifier_log_type(env, t, NULL);
2606
2607 return 0;
2608 }
2609
btf_fwd_type_log(struct btf_verifier_env * env,const struct btf_type * t)2610 static void btf_fwd_type_log(struct btf_verifier_env *env,
2611 const struct btf_type *t)
2612 {
2613 btf_verifier_log(env, "%s", btf_type_kflag(t) ? "union" : "struct");
2614 }
2615
2616 static struct btf_kind_operations fwd_ops = {
2617 .check_meta = btf_fwd_check_meta,
2618 .resolve = btf_df_resolve,
2619 .check_member = btf_df_check_member,
2620 .check_kflag_member = btf_df_check_kflag_member,
2621 .log_details = btf_fwd_type_log,
2622 .show = btf_df_show,
2623 };
2624
btf_array_check_member(struct btf_verifier_env * env,const struct btf_type * struct_type,const struct btf_member * member,const struct btf_type * member_type)2625 static int btf_array_check_member(struct btf_verifier_env *env,
2626 const struct btf_type *struct_type,
2627 const struct btf_member *member,
2628 const struct btf_type *member_type)
2629 {
2630 u32 struct_bits_off = member->offset;
2631 u32 struct_size, bytes_offset;
2632 u32 array_type_id, array_size;
2633 struct btf *btf = env->btf;
2634
2635 if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
2636 btf_verifier_log_member(env, struct_type, member,
2637 "Member is not byte aligned");
2638 return -EINVAL;
2639 }
2640
2641 array_type_id = member->type;
2642 btf_type_id_size(btf, &array_type_id, &array_size);
2643 struct_size = struct_type->size;
2644 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
2645 if (struct_size - bytes_offset < array_size) {
2646 btf_verifier_log_member(env, struct_type, member,
2647 "Member exceeds struct_size");
2648 return -EINVAL;
2649 }
2650
2651 return 0;
2652 }
2653
btf_array_check_meta(struct btf_verifier_env * env,const struct btf_type * t,u32 meta_left)2654 static s32 btf_array_check_meta(struct btf_verifier_env *env,
2655 const struct btf_type *t,
2656 u32 meta_left)
2657 {
2658 const struct btf_array *array = btf_type_array(t);
2659 u32 meta_needed = sizeof(*array);
2660
2661 if (meta_left < meta_needed) {
2662 btf_verifier_log_basic(env, t,
2663 "meta_left:%u meta_needed:%u",
2664 meta_left, meta_needed);
2665 return -EINVAL;
2666 }
2667
2668 /* array type should not have a name */
2669 if (t->name_off) {
2670 btf_verifier_log_type(env, t, "Invalid name");
2671 return -EINVAL;
2672 }
2673
2674 if (btf_type_vlen(t)) {
2675 btf_verifier_log_type(env, t, "vlen != 0");
2676 return -EINVAL;
2677 }
2678
2679 if (btf_type_kflag(t)) {
2680 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
2681 return -EINVAL;
2682 }
2683
2684 if (t->size) {
2685 btf_verifier_log_type(env, t, "size != 0");
2686 return -EINVAL;
2687 }
2688
2689 /* Array elem type and index type cannot be in type void,
2690 * so !array->type and !array->index_type are not allowed.
2691 */
2692 if (!array->type || !BTF_TYPE_ID_VALID(array->type)) {
2693 btf_verifier_log_type(env, t, "Invalid elem");
2694 return -EINVAL;
2695 }
2696
2697 if (!array->index_type || !BTF_TYPE_ID_VALID(array->index_type)) {
2698 btf_verifier_log_type(env, t, "Invalid index");
2699 return -EINVAL;
2700 }
2701
2702 btf_verifier_log_type(env, t, NULL);
2703
2704 return meta_needed;
2705 }
2706
btf_array_resolve(struct btf_verifier_env * env,const struct resolve_vertex * v)2707 static int btf_array_resolve(struct btf_verifier_env *env,
2708 const struct resolve_vertex *v)
2709 {
2710 const struct btf_array *array = btf_type_array(v->t);
2711 const struct btf_type *elem_type, *index_type;
2712 u32 elem_type_id, index_type_id;
2713 struct btf *btf = env->btf;
2714 u32 elem_size;
2715
2716 /* Check array->index_type */
2717 index_type_id = array->index_type;
2718 index_type = btf_type_by_id(btf, index_type_id);
2719 if (btf_type_nosize_or_null(index_type) ||
2720 btf_type_is_resolve_source_only(index_type)) {
2721 btf_verifier_log_type(env, v->t, "Invalid index");
2722 return -EINVAL;
2723 }
2724
2725 if (!env_type_is_resolve_sink(env, index_type) &&
2726 !env_type_is_resolved(env, index_type_id))
2727 return env_stack_push(env, index_type, index_type_id);
2728
2729 index_type = btf_type_id_size(btf, &index_type_id, NULL);
2730 if (!index_type || !btf_type_is_int(index_type) ||
2731 !btf_type_int_is_regular(index_type)) {
2732 btf_verifier_log_type(env, v->t, "Invalid index");
2733 return -EINVAL;
2734 }
2735
2736 /* Check array->type */
2737 elem_type_id = array->type;
2738 elem_type = btf_type_by_id(btf, elem_type_id);
2739 if (btf_type_nosize_or_null(elem_type) ||
2740 btf_type_is_resolve_source_only(elem_type)) {
2741 btf_verifier_log_type(env, v->t,
2742 "Invalid elem");
2743 return -EINVAL;
2744 }
2745
2746 if (!env_type_is_resolve_sink(env, elem_type) &&
2747 !env_type_is_resolved(env, elem_type_id))
2748 return env_stack_push(env, elem_type, elem_type_id);
2749
2750 elem_type = btf_type_id_size(btf, &elem_type_id, &elem_size);
2751 if (!elem_type) {
2752 btf_verifier_log_type(env, v->t, "Invalid elem");
2753 return -EINVAL;
2754 }
2755
2756 if (btf_type_is_int(elem_type) && !btf_type_int_is_regular(elem_type)) {
2757 btf_verifier_log_type(env, v->t, "Invalid array of int");
2758 return -EINVAL;
2759 }
2760
2761 if (array->nelems && elem_size > U32_MAX / array->nelems) {
2762 btf_verifier_log_type(env, v->t,
2763 "Array size overflows U32_MAX");
2764 return -EINVAL;
2765 }
2766
2767 env_stack_pop_resolved(env, elem_type_id, elem_size * array->nelems);
2768
2769 return 0;
2770 }
2771
btf_array_log(struct btf_verifier_env * env,const struct btf_type * t)2772 static void btf_array_log(struct btf_verifier_env *env,
2773 const struct btf_type *t)
2774 {
2775 const struct btf_array *array = btf_type_array(t);
2776
2777 btf_verifier_log(env, "type_id=%u index_type_id=%u nr_elems=%u",
2778 array->type, array->index_type, array->nelems);
2779 }
2780
__btf_array_show(const struct btf * btf,const struct btf_type * t,u32 type_id,void * data,u8 bits_offset,struct btf_show * show)2781 static void __btf_array_show(const struct btf *btf, const struct btf_type *t,
2782 u32 type_id, void *data, u8 bits_offset,
2783 struct btf_show *show)
2784 {
2785 const struct btf_array *array = btf_type_array(t);
2786 const struct btf_kind_operations *elem_ops;
2787 const struct btf_type *elem_type;
2788 u32 i, elem_size = 0, elem_type_id;
2789 u16 encoding = 0;
2790
2791 elem_type_id = array->type;
2792 elem_type = btf_type_skip_modifiers(btf, elem_type_id, NULL);
2793 if (elem_type && btf_type_has_size(elem_type))
2794 elem_size = elem_type->size;
2795
2796 if (elem_type && btf_type_is_int(elem_type)) {
2797 u32 int_type = btf_type_int(elem_type);
2798
2799 encoding = BTF_INT_ENCODING(int_type);
2800
2801 /*
2802 * BTF_INT_CHAR encoding never seems to be set for
2803 * char arrays, so if size is 1 and element is
2804 * printable as a char, we'll do that.
2805 */
2806 if (elem_size == 1)
2807 encoding = BTF_INT_CHAR;
2808 }
2809
2810 if (!btf_show_start_array_type(show, t, type_id, encoding, data))
2811 return;
2812
2813 if (!elem_type)
2814 goto out;
2815 elem_ops = btf_type_ops(elem_type);
2816
2817 for (i = 0; i < array->nelems; i++) {
2818
2819 btf_show_start_array_member(show);
2820
2821 elem_ops->show(btf, elem_type, elem_type_id, data,
2822 bits_offset, show);
2823 data += elem_size;
2824
2825 btf_show_end_array_member(show);
2826
2827 if (show->state.array_terminated)
2828 break;
2829 }
2830 out:
2831 btf_show_end_array_type(show);
2832 }
2833
btf_array_show(const struct btf * btf,const struct btf_type * t,u32 type_id,void * data,u8 bits_offset,struct btf_show * show)2834 static void btf_array_show(const struct btf *btf, const struct btf_type *t,
2835 u32 type_id, void *data, u8 bits_offset,
2836 struct btf_show *show)
2837 {
2838 const struct btf_member *m = show->state.member;
2839
2840 /*
2841 * First check if any members would be shown (are non-zero).
2842 * See comments above "struct btf_show" definition for more
2843 * details on how this works at a high-level.
2844 */
2845 if (show->state.depth > 0 && !(show->flags & BTF_SHOW_ZERO)) {
2846 if (!show->state.depth_check) {
2847 show->state.depth_check = show->state.depth + 1;
2848 show->state.depth_to_show = 0;
2849 }
2850 __btf_array_show(btf, t, type_id, data, bits_offset, show);
2851 show->state.member = m;
2852
2853 if (show->state.depth_check != show->state.depth + 1)
2854 return;
2855 show->state.depth_check = 0;
2856
2857 if (show->state.depth_to_show <= show->state.depth)
2858 return;
2859 /*
2860 * Reaching here indicates we have recursed and found
2861 * non-zero array member(s).
2862 */
2863 }
2864 __btf_array_show(btf, t, type_id, data, bits_offset, show);
2865 }
2866
2867 static struct btf_kind_operations array_ops = {
2868 .check_meta = btf_array_check_meta,
2869 .resolve = btf_array_resolve,
2870 .check_member = btf_array_check_member,
2871 .check_kflag_member = btf_generic_check_kflag_member,
2872 .log_details = btf_array_log,
2873 .show = btf_array_show,
2874 };
2875
btf_struct_check_member(struct btf_verifier_env * env,const struct btf_type * struct_type,const struct btf_member * member,const struct btf_type * member_type)2876 static int btf_struct_check_member(struct btf_verifier_env *env,
2877 const struct btf_type *struct_type,
2878 const struct btf_member *member,
2879 const struct btf_type *member_type)
2880 {
2881 u32 struct_bits_off = member->offset;
2882 u32 struct_size, bytes_offset;
2883
2884 if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
2885 btf_verifier_log_member(env, struct_type, member,
2886 "Member is not byte aligned");
2887 return -EINVAL;
2888 }
2889
2890 struct_size = struct_type->size;
2891 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
2892 if (struct_size - bytes_offset < member_type->size) {
2893 btf_verifier_log_member(env, struct_type, member,
2894 "Member exceeds struct_size");
2895 return -EINVAL;
2896 }
2897
2898 return 0;
2899 }
2900
btf_struct_check_meta(struct btf_verifier_env * env,const struct btf_type * t,u32 meta_left)2901 static s32 btf_struct_check_meta(struct btf_verifier_env *env,
2902 const struct btf_type *t,
2903 u32 meta_left)
2904 {
2905 bool is_union = BTF_INFO_KIND(t->info) == BTF_KIND_UNION;
2906 const struct btf_member *member;
2907 u32 meta_needed, last_offset;
2908 struct btf *btf = env->btf;
2909 u32 struct_size = t->size;
2910 u32 offset;
2911 u16 i;
2912
2913 meta_needed = btf_type_vlen(t) * sizeof(*member);
2914 if (meta_left < meta_needed) {
2915 btf_verifier_log_basic(env, t,
2916 "meta_left:%u meta_needed:%u",
2917 meta_left, meta_needed);
2918 return -EINVAL;
2919 }
2920
2921 /* struct type either no name or a valid one */
2922 if (t->name_off &&
2923 !btf_name_valid_identifier(env->btf, t->name_off)) {
2924 btf_verifier_log_type(env, t, "Invalid name");
2925 return -EINVAL;
2926 }
2927
2928 btf_verifier_log_type(env, t, NULL);
2929
2930 last_offset = 0;
2931 for_each_member(i, t, member) {
2932 if (!btf_name_offset_valid(btf, member->name_off)) {
2933 btf_verifier_log_member(env, t, member,
2934 "Invalid member name_offset:%u",
2935 member->name_off);
2936 return -EINVAL;
2937 }
2938
2939 /* struct member either no name or a valid one */
2940 if (member->name_off &&
2941 !btf_name_valid_identifier(btf, member->name_off)) {
2942 btf_verifier_log_member(env, t, member, "Invalid name");
2943 return -EINVAL;
2944 }
2945 /* A member cannot be in type void */
2946 if (!member->type || !BTF_TYPE_ID_VALID(member->type)) {
2947 btf_verifier_log_member(env, t, member,
2948 "Invalid type_id");
2949 return -EINVAL;
2950 }
2951
2952 offset = btf_member_bit_offset(t, member);
2953 if (is_union && offset) {
2954 btf_verifier_log_member(env, t, member,
2955 "Invalid member bits_offset");
2956 return -EINVAL;
2957 }
2958
2959 /*
2960 * ">" instead of ">=" because the last member could be
2961 * "char a[0];"
2962 */
2963 if (last_offset > offset) {
2964 btf_verifier_log_member(env, t, member,
2965 "Invalid member bits_offset");
2966 return -EINVAL;
2967 }
2968
2969 if (BITS_ROUNDUP_BYTES(offset) > struct_size) {
2970 btf_verifier_log_member(env, t, member,
2971 "Member bits_offset exceeds its struct size");
2972 return -EINVAL;
2973 }
2974
2975 btf_verifier_log_member(env, t, member, NULL);
2976 last_offset = offset;
2977 }
2978
2979 return meta_needed;
2980 }
2981
btf_struct_resolve(struct btf_verifier_env * env,const struct resolve_vertex * v)2982 static int btf_struct_resolve(struct btf_verifier_env *env,
2983 const struct resolve_vertex *v)
2984 {
2985 const struct btf_member *member;
2986 int err;
2987 u16 i;
2988
2989 /* Before continue resolving the next_member,
2990 * ensure the last member is indeed resolved to a
2991 * type with size info.
2992 */
2993 if (v->next_member) {
2994 const struct btf_type *last_member_type;
2995 const struct btf_member *last_member;
2996 u32 last_member_type_id;
2997
2998 last_member = btf_type_member(v->t) + v->next_member - 1;
2999 last_member_type_id = last_member->type;
3000 if (WARN_ON_ONCE(!env_type_is_resolved(env,
3001 last_member_type_id)))
3002 return -EINVAL;
3003
3004 last_member_type = btf_type_by_id(env->btf,
3005 last_member_type_id);
3006 if (btf_type_kflag(v->t))
3007 err = btf_type_ops(last_member_type)->check_kflag_member(env, v->t,
3008 last_member,
3009 last_member_type);
3010 else
3011 err = btf_type_ops(last_member_type)->check_member(env, v->t,
3012 last_member,
3013 last_member_type);
3014 if (err)
3015 return err;
3016 }
3017
3018 for_each_member_from(i, v->next_member, v->t, member) {
3019 u32 member_type_id = member->type;
3020 const struct btf_type *member_type = btf_type_by_id(env->btf,
3021 member_type_id);
3022
3023 if (btf_type_nosize_or_null(member_type) ||
3024 btf_type_is_resolve_source_only(member_type)) {
3025 btf_verifier_log_member(env, v->t, member,
3026 "Invalid member");
3027 return -EINVAL;
3028 }
3029
3030 if (!env_type_is_resolve_sink(env, member_type) &&
3031 !env_type_is_resolved(env, member_type_id)) {
3032 env_stack_set_next_member(env, i + 1);
3033 return env_stack_push(env, member_type, member_type_id);
3034 }
3035
3036 if (btf_type_kflag(v->t))
3037 err = btf_type_ops(member_type)->check_kflag_member(env, v->t,
3038 member,
3039 member_type);
3040 else
3041 err = btf_type_ops(member_type)->check_member(env, v->t,
3042 member,
3043 member_type);
3044 if (err)
3045 return err;
3046 }
3047
3048 env_stack_pop_resolved(env, 0, 0);
3049
3050 return 0;
3051 }
3052
btf_struct_log(struct btf_verifier_env * env,const struct btf_type * t)3053 static void btf_struct_log(struct btf_verifier_env *env,
3054 const struct btf_type *t)
3055 {
3056 btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t));
3057 }
3058
btf_find_struct_field(const struct btf * btf,const struct btf_type * t,const char * name,int sz,int align)3059 static int btf_find_struct_field(const struct btf *btf, const struct btf_type *t,
3060 const char *name, int sz, int align)
3061 {
3062 const struct btf_member *member;
3063 u32 i, off = -ENOENT;
3064
3065 for_each_member(i, t, member) {
3066 const struct btf_type *member_type = btf_type_by_id(btf,
3067 member->type);
3068 if (!__btf_type_is_struct(member_type))
3069 continue;
3070 if (member_type->size != sz)
3071 continue;
3072 if (strcmp(__btf_name_by_offset(btf, member_type->name_off), name))
3073 continue;
3074 if (off != -ENOENT)
3075 /* only one such field is allowed */
3076 return -E2BIG;
3077 off = btf_member_bit_offset(t, member);
3078 if (off % 8)
3079 /* valid C code cannot generate such BTF */
3080 return -EINVAL;
3081 off /= 8;
3082 if (off % align)
3083 return -EINVAL;
3084 }
3085 return off;
3086 }
3087
btf_find_datasec_var(const struct btf * btf,const struct btf_type * t,const char * name,int sz,int align)3088 static int btf_find_datasec_var(const struct btf *btf, const struct btf_type *t,
3089 const char *name, int sz, int align)
3090 {
3091 const struct btf_var_secinfo *vsi;
3092 u32 i, off = -ENOENT;
3093
3094 for_each_vsi(i, t, vsi) {
3095 const struct btf_type *var = btf_type_by_id(btf, vsi->type);
3096 const struct btf_type *var_type = btf_type_by_id(btf, var->type);
3097
3098 if (!__btf_type_is_struct(var_type))
3099 continue;
3100 if (var_type->size != sz)
3101 continue;
3102 if (vsi->size != sz)
3103 continue;
3104 if (strcmp(__btf_name_by_offset(btf, var_type->name_off), name))
3105 continue;
3106 if (off != -ENOENT)
3107 /* only one such field is allowed */
3108 return -E2BIG;
3109 off = vsi->offset;
3110 if (off % align)
3111 return -EINVAL;
3112 }
3113 return off;
3114 }
3115
btf_find_field(const struct btf * btf,const struct btf_type * t,const char * name,int sz,int align)3116 static int btf_find_field(const struct btf *btf, const struct btf_type *t,
3117 const char *name, int sz, int align)
3118 {
3119
3120 if (__btf_type_is_struct(t))
3121 return btf_find_struct_field(btf, t, name, sz, align);
3122 else if (btf_type_is_datasec(t))
3123 return btf_find_datasec_var(btf, t, name, sz, align);
3124 return -EINVAL;
3125 }
3126
3127 /* find 'struct bpf_spin_lock' in map value.
3128 * return >= 0 offset if found
3129 * and < 0 in case of error
3130 */
btf_find_spin_lock(const struct btf * btf,const struct btf_type * t)3131 int btf_find_spin_lock(const struct btf *btf, const struct btf_type *t)
3132 {
3133 return btf_find_field(btf, t, "bpf_spin_lock",
3134 sizeof(struct bpf_spin_lock),
3135 __alignof__(struct bpf_spin_lock));
3136 }
3137
btf_find_timer(const struct btf * btf,const struct btf_type * t)3138 int btf_find_timer(const struct btf *btf, const struct btf_type *t)
3139 {
3140 return btf_find_field(btf, t, "bpf_timer",
3141 sizeof(struct bpf_timer),
3142 __alignof__(struct bpf_timer));
3143 }
3144
__btf_struct_show(const struct btf * btf,const struct btf_type * t,u32 type_id,void * data,u8 bits_offset,struct btf_show * show)3145 static void __btf_struct_show(const struct btf *btf, const struct btf_type *t,
3146 u32 type_id, void *data, u8 bits_offset,
3147 struct btf_show *show)
3148 {
3149 const struct btf_member *member;
3150 void *safe_data;
3151 u32 i;
3152
3153 safe_data = btf_show_start_struct_type(show, t, type_id, data);
3154 if (!safe_data)
3155 return;
3156
3157 for_each_member(i, t, member) {
3158 const struct btf_type *member_type = btf_type_by_id(btf,
3159 member->type);
3160 const struct btf_kind_operations *ops;
3161 u32 member_offset, bitfield_size;
3162 u32 bytes_offset;
3163 u8 bits8_offset;
3164
3165 btf_show_start_member(show, member);
3166
3167 member_offset = btf_member_bit_offset(t, member);
3168 bitfield_size = btf_member_bitfield_size(t, member);
3169 bytes_offset = BITS_ROUNDDOWN_BYTES(member_offset);
3170 bits8_offset = BITS_PER_BYTE_MASKED(member_offset);
3171 if (bitfield_size) {
3172 safe_data = btf_show_start_type(show, member_type,
3173 member->type,
3174 data + bytes_offset);
3175 if (safe_data)
3176 btf_bitfield_show(safe_data,
3177 bits8_offset,
3178 bitfield_size, show);
3179 btf_show_end_type(show);
3180 } else {
3181 ops = btf_type_ops(member_type);
3182 ops->show(btf, member_type, member->type,
3183 data + bytes_offset, bits8_offset, show);
3184 }
3185
3186 btf_show_end_member(show);
3187 }
3188
3189 btf_show_end_struct_type(show);
3190 }
3191
btf_struct_show(const struct btf * btf,const struct btf_type * t,u32 type_id,void * data,u8 bits_offset,struct btf_show * show)3192 static void btf_struct_show(const struct btf *btf, const struct btf_type *t,
3193 u32 type_id, void *data, u8 bits_offset,
3194 struct btf_show *show)
3195 {
3196 const struct btf_member *m = show->state.member;
3197
3198 /*
3199 * First check if any members would be shown (are non-zero).
3200 * See comments above "struct btf_show" definition for more
3201 * details on how this works at a high-level.
3202 */
3203 if (show->state.depth > 0 && !(show->flags & BTF_SHOW_ZERO)) {
3204 if (!show->state.depth_check) {
3205 show->state.depth_check = show->state.depth + 1;
3206 show->state.depth_to_show = 0;
3207 }
3208 __btf_struct_show(btf, t, type_id, data, bits_offset, show);
3209 /* Restore saved member data here */
3210 show->state.member = m;
3211 if (show->state.depth_check != show->state.depth + 1)
3212 return;
3213 show->state.depth_check = 0;
3214
3215 if (show->state.depth_to_show <= show->state.depth)
3216 return;
3217 /*
3218 * Reaching here indicates we have recursed and found
3219 * non-zero child values.
3220 */
3221 }
3222
3223 __btf_struct_show(btf, t, type_id, data, bits_offset, show);
3224 }
3225
3226 static struct btf_kind_operations struct_ops = {
3227 .check_meta = btf_struct_check_meta,
3228 .resolve = btf_struct_resolve,
3229 .check_member = btf_struct_check_member,
3230 .check_kflag_member = btf_generic_check_kflag_member,
3231 .log_details = btf_struct_log,
3232 .show = btf_struct_show,
3233 };
3234
btf_enum_check_member(struct btf_verifier_env * env,const struct btf_type * struct_type,const struct btf_member * member,const struct btf_type * member_type)3235 static int btf_enum_check_member(struct btf_verifier_env *env,
3236 const struct btf_type *struct_type,
3237 const struct btf_member *member,
3238 const struct btf_type *member_type)
3239 {
3240 u32 struct_bits_off = member->offset;
3241 u32 struct_size, bytes_offset;
3242
3243 if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
3244 btf_verifier_log_member(env, struct_type, member,
3245 "Member is not byte aligned");
3246 return -EINVAL;
3247 }
3248
3249 struct_size = struct_type->size;
3250 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
3251 if (struct_size - bytes_offset < member_type->size) {
3252 btf_verifier_log_member(env, struct_type, member,
3253 "Member exceeds struct_size");
3254 return -EINVAL;
3255 }
3256
3257 return 0;
3258 }
3259
btf_enum_check_kflag_member(struct btf_verifier_env * env,const struct btf_type * struct_type,const struct btf_member * member,const struct btf_type * member_type)3260 static int btf_enum_check_kflag_member(struct btf_verifier_env *env,
3261 const struct btf_type *struct_type,
3262 const struct btf_member *member,
3263 const struct btf_type *member_type)
3264 {
3265 u32 struct_bits_off, nr_bits, bytes_end, struct_size;
3266 u32 int_bitsize = sizeof(int) * BITS_PER_BYTE;
3267
3268 struct_bits_off = BTF_MEMBER_BIT_OFFSET(member->offset);
3269 nr_bits = BTF_MEMBER_BITFIELD_SIZE(member->offset);
3270 if (!nr_bits) {
3271 if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
3272 btf_verifier_log_member(env, struct_type, member,
3273 "Member is not byte aligned");
3274 return -EINVAL;
3275 }
3276
3277 nr_bits = int_bitsize;
3278 } else if (nr_bits > int_bitsize) {
3279 btf_verifier_log_member(env, struct_type, member,
3280 "Invalid member bitfield_size");
3281 return -EINVAL;
3282 }
3283
3284 struct_size = struct_type->size;
3285 bytes_end = BITS_ROUNDUP_BYTES(struct_bits_off + nr_bits);
3286 if (struct_size < bytes_end) {
3287 btf_verifier_log_member(env, struct_type, member,
3288 "Member exceeds struct_size");
3289 return -EINVAL;
3290 }
3291
3292 return 0;
3293 }
3294
btf_enum_check_meta(struct btf_verifier_env * env,const struct btf_type * t,u32 meta_left)3295 static s32 btf_enum_check_meta(struct btf_verifier_env *env,
3296 const struct btf_type *t,
3297 u32 meta_left)
3298 {
3299 const struct btf_enum *enums = btf_type_enum(t);
3300 struct btf *btf = env->btf;
3301 u16 i, nr_enums;
3302 u32 meta_needed;
3303
3304 nr_enums = btf_type_vlen(t);
3305 meta_needed = nr_enums * sizeof(*enums);
3306
3307 if (meta_left < meta_needed) {
3308 btf_verifier_log_basic(env, t,
3309 "meta_left:%u meta_needed:%u",
3310 meta_left, meta_needed);
3311 return -EINVAL;
3312 }
3313
3314 if (btf_type_kflag(t)) {
3315 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
3316 return -EINVAL;
3317 }
3318
3319 if (t->size > 8 || !is_power_of_2(t->size)) {
3320 btf_verifier_log_type(env, t, "Unexpected size");
3321 return -EINVAL;
3322 }
3323
3324 /* enum type either no name or a valid one */
3325 if (t->name_off &&
3326 !btf_name_valid_identifier(env->btf, t->name_off)) {
3327 btf_verifier_log_type(env, t, "Invalid name");
3328 return -EINVAL;
3329 }
3330
3331 btf_verifier_log_type(env, t, NULL);
3332
3333 for (i = 0; i < nr_enums; i++) {
3334 if (!btf_name_offset_valid(btf, enums[i].name_off)) {
3335 btf_verifier_log(env, "\tInvalid name_offset:%u",
3336 enums[i].name_off);
3337 return -EINVAL;
3338 }
3339
3340 /* enum member must have a valid name */
3341 if (!enums[i].name_off ||
3342 !btf_name_valid_identifier(btf, enums[i].name_off)) {
3343 btf_verifier_log_type(env, t, "Invalid name");
3344 return -EINVAL;
3345 }
3346
3347 if (env->log.level == BPF_LOG_KERNEL)
3348 continue;
3349 btf_verifier_log(env, "\t%s val=%d\n",
3350 __btf_name_by_offset(btf, enums[i].name_off),
3351 enums[i].val);
3352 }
3353
3354 return meta_needed;
3355 }
3356
btf_enum_log(struct btf_verifier_env * env,const struct btf_type * t)3357 static void btf_enum_log(struct btf_verifier_env *env,
3358 const struct btf_type *t)
3359 {
3360 btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t));
3361 }
3362
btf_enum_show(const struct btf * btf,const struct btf_type * t,u32 type_id,void * data,u8 bits_offset,struct btf_show * show)3363 static void btf_enum_show(const struct btf *btf, const struct btf_type *t,
3364 u32 type_id, void *data, u8 bits_offset,
3365 struct btf_show *show)
3366 {
3367 const struct btf_enum *enums = btf_type_enum(t);
3368 u32 i, nr_enums = btf_type_vlen(t);
3369 void *safe_data;
3370 int v;
3371
3372 safe_data = btf_show_start_type(show, t, type_id, data);
3373 if (!safe_data)
3374 return;
3375
3376 v = *(int *)safe_data;
3377
3378 for (i = 0; i < nr_enums; i++) {
3379 if (v != enums[i].val)
3380 continue;
3381
3382 btf_show_type_value(show, "%s",
3383 __btf_name_by_offset(btf,
3384 enums[i].name_off));
3385
3386 btf_show_end_type(show);
3387 return;
3388 }
3389
3390 btf_show_type_value(show, "%d", v);
3391 btf_show_end_type(show);
3392 }
3393
3394 static struct btf_kind_operations enum_ops = {
3395 .check_meta = btf_enum_check_meta,
3396 .resolve = btf_df_resolve,
3397 .check_member = btf_enum_check_member,
3398 .check_kflag_member = btf_enum_check_kflag_member,
3399 .log_details = btf_enum_log,
3400 .show = btf_enum_show,
3401 };
3402
btf_func_proto_check_meta(struct btf_verifier_env * env,const struct btf_type * t,u32 meta_left)3403 static s32 btf_func_proto_check_meta(struct btf_verifier_env *env,
3404 const struct btf_type *t,
3405 u32 meta_left)
3406 {
3407 u32 meta_needed = btf_type_vlen(t) * sizeof(struct btf_param);
3408
3409 if (meta_left < meta_needed) {
3410 btf_verifier_log_basic(env, t,
3411 "meta_left:%u meta_needed:%u",
3412 meta_left, meta_needed);
3413 return -EINVAL;
3414 }
3415
3416 if (t->name_off) {
3417 btf_verifier_log_type(env, t, "Invalid name");
3418 return -EINVAL;
3419 }
3420
3421 if (btf_type_kflag(t)) {
3422 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
3423 return -EINVAL;
3424 }
3425
3426 btf_verifier_log_type(env, t, NULL);
3427
3428 return meta_needed;
3429 }
3430
btf_func_proto_log(struct btf_verifier_env * env,const struct btf_type * t)3431 static void btf_func_proto_log(struct btf_verifier_env *env,
3432 const struct btf_type *t)
3433 {
3434 const struct btf_param *args = (const struct btf_param *)(t + 1);
3435 u16 nr_args = btf_type_vlen(t), i;
3436
3437 btf_verifier_log(env, "return=%u args=(", t->type);
3438 if (!nr_args) {
3439 btf_verifier_log(env, "void");
3440 goto done;
3441 }
3442
3443 if (nr_args == 1 && !args[0].type) {
3444 /* Only one vararg */
3445 btf_verifier_log(env, "vararg");
3446 goto done;
3447 }
3448
3449 btf_verifier_log(env, "%u %s", args[0].type,
3450 __btf_name_by_offset(env->btf,
3451 args[0].name_off));
3452 for (i = 1; i < nr_args - 1; i++)
3453 btf_verifier_log(env, ", %u %s", args[i].type,
3454 __btf_name_by_offset(env->btf,
3455 args[i].name_off));
3456
3457 if (nr_args > 1) {
3458 const struct btf_param *last_arg = &args[nr_args - 1];
3459
3460 if (last_arg->type)
3461 btf_verifier_log(env, ", %u %s", last_arg->type,
3462 __btf_name_by_offset(env->btf,
3463 last_arg->name_off));
3464 else
3465 btf_verifier_log(env, ", vararg");
3466 }
3467
3468 done:
3469 btf_verifier_log(env, ")");
3470 }
3471
3472 static struct btf_kind_operations func_proto_ops = {
3473 .check_meta = btf_func_proto_check_meta,
3474 .resolve = btf_df_resolve,
3475 /*
3476 * BTF_KIND_FUNC_PROTO cannot be directly referred by
3477 * a struct's member.
3478 *
3479 * It should be a function pointer instead.
3480 * (i.e. struct's member -> BTF_KIND_PTR -> BTF_KIND_FUNC_PROTO)
3481 *
3482 * Hence, there is no btf_func_check_member().
3483 */
3484 .check_member = btf_df_check_member,
3485 .check_kflag_member = btf_df_check_kflag_member,
3486 .log_details = btf_func_proto_log,
3487 .show = btf_df_show,
3488 };
3489
btf_func_check_meta(struct btf_verifier_env * env,const struct btf_type * t,u32 meta_left)3490 static s32 btf_func_check_meta(struct btf_verifier_env *env,
3491 const struct btf_type *t,
3492 u32 meta_left)
3493 {
3494 if (!t->name_off ||
3495 !btf_name_valid_identifier(env->btf, t->name_off)) {
3496 btf_verifier_log_type(env, t, "Invalid name");
3497 return -EINVAL;
3498 }
3499
3500 if (btf_type_vlen(t) > BTF_FUNC_GLOBAL) {
3501 btf_verifier_log_type(env, t, "Invalid func linkage");
3502 return -EINVAL;
3503 }
3504
3505 if (btf_type_kflag(t)) {
3506 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
3507 return -EINVAL;
3508 }
3509
3510 btf_verifier_log_type(env, t, NULL);
3511
3512 return 0;
3513 }
3514
3515 static struct btf_kind_operations func_ops = {
3516 .check_meta = btf_func_check_meta,
3517 .resolve = btf_df_resolve,
3518 .check_member = btf_df_check_member,
3519 .check_kflag_member = btf_df_check_kflag_member,
3520 .log_details = btf_ref_type_log,
3521 .show = btf_df_show,
3522 };
3523
btf_var_check_meta(struct btf_verifier_env * env,const struct btf_type * t,u32 meta_left)3524 static s32 btf_var_check_meta(struct btf_verifier_env *env,
3525 const struct btf_type *t,
3526 u32 meta_left)
3527 {
3528 const struct btf_var *var;
3529 u32 meta_needed = sizeof(*var);
3530
3531 if (meta_left < meta_needed) {
3532 btf_verifier_log_basic(env, t,
3533 "meta_left:%u meta_needed:%u",
3534 meta_left, meta_needed);
3535 return -EINVAL;
3536 }
3537
3538 if (btf_type_vlen(t)) {
3539 btf_verifier_log_type(env, t, "vlen != 0");
3540 return -EINVAL;
3541 }
3542
3543 if (btf_type_kflag(t)) {
3544 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
3545 return -EINVAL;
3546 }
3547
3548 if (!t->name_off ||
3549 !__btf_name_valid(env->btf, t->name_off)) {
3550 btf_verifier_log_type(env, t, "Invalid name");
3551 return -EINVAL;
3552 }
3553
3554 /* A var cannot be in type void */
3555 if (!t->type || !BTF_TYPE_ID_VALID(t->type)) {
3556 btf_verifier_log_type(env, t, "Invalid type_id");
3557 return -EINVAL;
3558 }
3559
3560 var = btf_type_var(t);
3561 if (var->linkage != BTF_VAR_STATIC &&
3562 var->linkage != BTF_VAR_GLOBAL_ALLOCATED) {
3563 btf_verifier_log_type(env, t, "Linkage not supported");
3564 return -EINVAL;
3565 }
3566
3567 btf_verifier_log_type(env, t, NULL);
3568
3569 return meta_needed;
3570 }
3571
btf_var_log(struct btf_verifier_env * env,const struct btf_type * t)3572 static void btf_var_log(struct btf_verifier_env *env, const struct btf_type *t)
3573 {
3574 const struct btf_var *var = btf_type_var(t);
3575
3576 btf_verifier_log(env, "type_id=%u linkage=%u", t->type, var->linkage);
3577 }
3578
3579 static const struct btf_kind_operations var_ops = {
3580 .check_meta = btf_var_check_meta,
3581 .resolve = btf_var_resolve,
3582 .check_member = btf_df_check_member,
3583 .check_kflag_member = btf_df_check_kflag_member,
3584 .log_details = btf_var_log,
3585 .show = btf_var_show,
3586 };
3587
btf_datasec_check_meta(struct btf_verifier_env * env,const struct btf_type * t,u32 meta_left)3588 static s32 btf_datasec_check_meta(struct btf_verifier_env *env,
3589 const struct btf_type *t,
3590 u32 meta_left)
3591 {
3592 const struct btf_var_secinfo *vsi;
3593 u64 last_vsi_end_off = 0, sum = 0;
3594 u32 i, meta_needed;
3595
3596 meta_needed = btf_type_vlen(t) * sizeof(*vsi);
3597 if (meta_left < meta_needed) {
3598 btf_verifier_log_basic(env, t,
3599 "meta_left:%u meta_needed:%u",
3600 meta_left, meta_needed);
3601 return -EINVAL;
3602 }
3603
3604 if (!t->size) {
3605 btf_verifier_log_type(env, t, "size == 0");
3606 return -EINVAL;
3607 }
3608
3609 if (btf_type_kflag(t)) {
3610 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
3611 return -EINVAL;
3612 }
3613
3614 if (!t->name_off ||
3615 !btf_name_valid_section(env->btf, t->name_off)) {
3616 btf_verifier_log_type(env, t, "Invalid name");
3617 return -EINVAL;
3618 }
3619
3620 btf_verifier_log_type(env, t, NULL);
3621
3622 for_each_vsi(i, t, vsi) {
3623 /* A var cannot be in type void */
3624 if (!vsi->type || !BTF_TYPE_ID_VALID(vsi->type)) {
3625 btf_verifier_log_vsi(env, t, vsi,
3626 "Invalid type_id");
3627 return -EINVAL;
3628 }
3629
3630 if (vsi->offset < last_vsi_end_off || vsi->offset >= t->size) {
3631 btf_verifier_log_vsi(env, t, vsi,
3632 "Invalid offset");
3633 return -EINVAL;
3634 }
3635
3636 if (!vsi->size || vsi->size > t->size) {
3637 btf_verifier_log_vsi(env, t, vsi,
3638 "Invalid size");
3639 return -EINVAL;
3640 }
3641
3642 last_vsi_end_off = vsi->offset + vsi->size;
3643 if (last_vsi_end_off > t->size) {
3644 btf_verifier_log_vsi(env, t, vsi,
3645 "Invalid offset+size");
3646 return -EINVAL;
3647 }
3648
3649 btf_verifier_log_vsi(env, t, vsi, NULL);
3650 sum += vsi->size;
3651 }
3652
3653 if (t->size < sum) {
3654 btf_verifier_log_type(env, t, "Invalid btf_info size");
3655 return -EINVAL;
3656 }
3657
3658 return meta_needed;
3659 }
3660
btf_datasec_resolve(struct btf_verifier_env * env,const struct resolve_vertex * v)3661 static int btf_datasec_resolve(struct btf_verifier_env *env,
3662 const struct resolve_vertex *v)
3663 {
3664 const struct btf_var_secinfo *vsi;
3665 struct btf *btf = env->btf;
3666 u16 i;
3667
3668 env->resolve_mode = RESOLVE_TBD;
3669 for_each_vsi_from(i, v->next_member, v->t, vsi) {
3670 u32 var_type_id = vsi->type, type_id, type_size = 0;
3671 const struct btf_type *var_type = btf_type_by_id(env->btf,
3672 var_type_id);
3673 if (!var_type || !btf_type_is_var(var_type)) {
3674 btf_verifier_log_vsi(env, v->t, vsi,
3675 "Not a VAR kind member");
3676 return -EINVAL;
3677 }
3678
3679 if (!env_type_is_resolve_sink(env, var_type) &&
3680 !env_type_is_resolved(env, var_type_id)) {
3681 env_stack_set_next_member(env, i + 1);
3682 return env_stack_push(env, var_type, var_type_id);
3683 }
3684
3685 type_id = var_type->type;
3686 if (!btf_type_id_size(btf, &type_id, &type_size)) {
3687 btf_verifier_log_vsi(env, v->t, vsi, "Invalid type");
3688 return -EINVAL;
3689 }
3690
3691 if (vsi->size < type_size) {
3692 btf_verifier_log_vsi(env, v->t, vsi, "Invalid size");
3693 return -EINVAL;
3694 }
3695 }
3696
3697 env_stack_pop_resolved(env, 0, 0);
3698 return 0;
3699 }
3700
btf_datasec_log(struct btf_verifier_env * env,const struct btf_type * t)3701 static void btf_datasec_log(struct btf_verifier_env *env,
3702 const struct btf_type *t)
3703 {
3704 btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t));
3705 }
3706
btf_datasec_show(const struct btf * btf,const struct btf_type * t,u32 type_id,void * data,u8 bits_offset,struct btf_show * show)3707 static void btf_datasec_show(const struct btf *btf,
3708 const struct btf_type *t, u32 type_id,
3709 void *data, u8 bits_offset,
3710 struct btf_show *show)
3711 {
3712 const struct btf_var_secinfo *vsi;
3713 const struct btf_type *var;
3714 u32 i;
3715
3716 if (!btf_show_start_type(show, t, type_id, data))
3717 return;
3718
3719 btf_show_type_value(show, "section (\"%s\") = {",
3720 __btf_name_by_offset(btf, t->name_off));
3721 for_each_vsi(i, t, vsi) {
3722 var = btf_type_by_id(btf, vsi->type);
3723 if (i)
3724 btf_show(show, ",");
3725 btf_type_ops(var)->show(btf, var, vsi->type,
3726 data + vsi->offset, bits_offset, show);
3727 }
3728 btf_show_end_type(show);
3729 }
3730
3731 static const struct btf_kind_operations datasec_ops = {
3732 .check_meta = btf_datasec_check_meta,
3733 .resolve = btf_datasec_resolve,
3734 .check_member = btf_df_check_member,
3735 .check_kflag_member = btf_df_check_kflag_member,
3736 .log_details = btf_datasec_log,
3737 .show = btf_datasec_show,
3738 };
3739
btf_float_check_meta(struct btf_verifier_env * env,const struct btf_type * t,u32 meta_left)3740 static s32 btf_float_check_meta(struct btf_verifier_env *env,
3741 const struct btf_type *t,
3742 u32 meta_left)
3743 {
3744 if (btf_type_vlen(t)) {
3745 btf_verifier_log_type(env, t, "vlen != 0");
3746 return -EINVAL;
3747 }
3748
3749 if (btf_type_kflag(t)) {
3750 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
3751 return -EINVAL;
3752 }
3753
3754 if (t->size != 2 && t->size != 4 && t->size != 8 && t->size != 12 &&
3755 t->size != 16) {
3756 btf_verifier_log_type(env, t, "Invalid type_size");
3757 return -EINVAL;
3758 }
3759
3760 btf_verifier_log_type(env, t, NULL);
3761
3762 return 0;
3763 }
3764
btf_float_check_member(struct btf_verifier_env * env,const struct btf_type * struct_type,const struct btf_member * member,const struct btf_type * member_type)3765 static int btf_float_check_member(struct btf_verifier_env *env,
3766 const struct btf_type *struct_type,
3767 const struct btf_member *member,
3768 const struct btf_type *member_type)
3769 {
3770 u64 start_offset_bytes;
3771 u64 end_offset_bytes;
3772 u64 misalign_bits;
3773 u64 align_bytes;
3774 u64 align_bits;
3775
3776 /* Different architectures have different alignment requirements, so
3777 * here we check only for the reasonable minimum. This way we ensure
3778 * that types after CO-RE can pass the kernel BTF verifier.
3779 */
3780 align_bytes = min_t(u64, sizeof(void *), member_type->size);
3781 align_bits = align_bytes * BITS_PER_BYTE;
3782 div64_u64_rem(member->offset, align_bits, &misalign_bits);
3783 if (misalign_bits) {
3784 btf_verifier_log_member(env, struct_type, member,
3785 "Member is not properly aligned");
3786 return -EINVAL;
3787 }
3788
3789 start_offset_bytes = member->offset / BITS_PER_BYTE;
3790 end_offset_bytes = start_offset_bytes + member_type->size;
3791 if (end_offset_bytes > struct_type->size) {
3792 btf_verifier_log_member(env, struct_type, member,
3793 "Member exceeds struct_size");
3794 return -EINVAL;
3795 }
3796
3797 return 0;
3798 }
3799
btf_float_log(struct btf_verifier_env * env,const struct btf_type * t)3800 static void btf_float_log(struct btf_verifier_env *env,
3801 const struct btf_type *t)
3802 {
3803 btf_verifier_log(env, "size=%u", t->size);
3804 }
3805
3806 static const struct btf_kind_operations float_ops = {
3807 .check_meta = btf_float_check_meta,
3808 .resolve = btf_df_resolve,
3809 .check_member = btf_float_check_member,
3810 .check_kflag_member = btf_generic_check_kflag_member,
3811 .log_details = btf_float_log,
3812 .show = btf_df_show,
3813 };
3814
btf_func_proto_check(struct btf_verifier_env * env,const struct btf_type * t)3815 static int btf_func_proto_check(struct btf_verifier_env *env,
3816 const struct btf_type *t)
3817 {
3818 const struct btf_type *ret_type;
3819 const struct btf_param *args;
3820 const struct btf *btf;
3821 u16 nr_args, i;
3822 int err;
3823
3824 btf = env->btf;
3825 args = (const struct btf_param *)(t + 1);
3826 nr_args = btf_type_vlen(t);
3827
3828 /* Check func return type which could be "void" (t->type == 0) */
3829 if (t->type) {
3830 u32 ret_type_id = t->type;
3831
3832 ret_type = btf_type_by_id(btf, ret_type_id);
3833 if (!ret_type) {
3834 btf_verifier_log_type(env, t, "Invalid return type");
3835 return -EINVAL;
3836 }
3837
3838 if (btf_type_needs_resolve(ret_type) &&
3839 !env_type_is_resolved(env, ret_type_id)) {
3840 err = btf_resolve(env, ret_type, ret_type_id);
3841 if (err)
3842 return err;
3843 }
3844
3845 /* Ensure the return type is a type that has a size */
3846 if (!btf_type_id_size(btf, &ret_type_id, NULL)) {
3847 btf_verifier_log_type(env, t, "Invalid return type");
3848 return -EINVAL;
3849 }
3850 }
3851
3852 if (!nr_args)
3853 return 0;
3854
3855 /* Last func arg type_id could be 0 if it is a vararg */
3856 if (!args[nr_args - 1].type) {
3857 if (args[nr_args - 1].name_off) {
3858 btf_verifier_log_type(env, t, "Invalid arg#%u",
3859 nr_args);
3860 return -EINVAL;
3861 }
3862 nr_args--;
3863 }
3864
3865 err = 0;
3866 for (i = 0; i < nr_args; i++) {
3867 const struct btf_type *arg_type;
3868 u32 arg_type_id;
3869
3870 arg_type_id = args[i].type;
3871 arg_type = btf_type_by_id(btf, arg_type_id);
3872 if (!arg_type) {
3873 btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1);
3874 err = -EINVAL;
3875 break;
3876 }
3877
3878 if (btf_type_is_resolve_source_only(arg_type)) {
3879 btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1);
3880 return -EINVAL;
3881 }
3882
3883 if (args[i].name_off &&
3884 (!btf_name_offset_valid(btf, args[i].name_off) ||
3885 !btf_name_valid_identifier(btf, args[i].name_off))) {
3886 btf_verifier_log_type(env, t,
3887 "Invalid arg#%u", i + 1);
3888 err = -EINVAL;
3889 break;
3890 }
3891
3892 if (btf_type_needs_resolve(arg_type) &&
3893 !env_type_is_resolved(env, arg_type_id)) {
3894 err = btf_resolve(env, arg_type, arg_type_id);
3895 if (err)
3896 break;
3897 }
3898
3899 if (!btf_type_id_size(btf, &arg_type_id, NULL)) {
3900 btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1);
3901 err = -EINVAL;
3902 break;
3903 }
3904 }
3905
3906 return err;
3907 }
3908
btf_func_check(struct btf_verifier_env * env,const struct btf_type * t)3909 static int btf_func_check(struct btf_verifier_env *env,
3910 const struct btf_type *t)
3911 {
3912 const struct btf_type *proto_type;
3913 const struct btf_param *args;
3914 const struct btf *btf;
3915 u16 nr_args, i;
3916
3917 btf = env->btf;
3918 proto_type = btf_type_by_id(btf, t->type);
3919
3920 if (!proto_type || !btf_type_is_func_proto(proto_type)) {
3921 btf_verifier_log_type(env, t, "Invalid type_id");
3922 return -EINVAL;
3923 }
3924
3925 args = (const struct btf_param *)(proto_type + 1);
3926 nr_args = btf_type_vlen(proto_type);
3927 for (i = 0; i < nr_args; i++) {
3928 if (!args[i].name_off && args[i].type) {
3929 btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1);
3930 return -EINVAL;
3931 }
3932 }
3933
3934 return 0;
3935 }
3936
3937 static const struct btf_kind_operations * const kind_ops[NR_BTF_KINDS] = {
3938 [BTF_KIND_INT] = &int_ops,
3939 [BTF_KIND_PTR] = &ptr_ops,
3940 [BTF_KIND_ARRAY] = &array_ops,
3941 [BTF_KIND_STRUCT] = &struct_ops,
3942 [BTF_KIND_UNION] = &struct_ops,
3943 [BTF_KIND_ENUM] = &enum_ops,
3944 [BTF_KIND_FWD] = &fwd_ops,
3945 [BTF_KIND_TYPEDEF] = &modifier_ops,
3946 [BTF_KIND_VOLATILE] = &modifier_ops,
3947 [BTF_KIND_CONST] = &modifier_ops,
3948 [BTF_KIND_RESTRICT] = &modifier_ops,
3949 [BTF_KIND_FUNC] = &func_ops,
3950 [BTF_KIND_FUNC_PROTO] = &func_proto_ops,
3951 [BTF_KIND_VAR] = &var_ops,
3952 [BTF_KIND_DATASEC] = &datasec_ops,
3953 [BTF_KIND_FLOAT] = &float_ops,
3954 };
3955
btf_check_meta(struct btf_verifier_env * env,const struct btf_type * t,u32 meta_left)3956 static s32 btf_check_meta(struct btf_verifier_env *env,
3957 const struct btf_type *t,
3958 u32 meta_left)
3959 {
3960 u32 saved_meta_left = meta_left;
3961 s32 var_meta_size;
3962
3963 if (meta_left < sizeof(*t)) {
3964 btf_verifier_log(env, "[%u] meta_left:%u meta_needed:%zu",
3965 env->log_type_id, meta_left, sizeof(*t));
3966 return -EINVAL;
3967 }
3968 meta_left -= sizeof(*t);
3969
3970 if (t->info & ~BTF_INFO_MASK) {
3971 btf_verifier_log(env, "[%u] Invalid btf_info:%x",
3972 env->log_type_id, t->info);
3973 return -EINVAL;
3974 }
3975
3976 if (BTF_INFO_KIND(t->info) > BTF_KIND_MAX ||
3977 BTF_INFO_KIND(t->info) == BTF_KIND_UNKN) {
3978 btf_verifier_log(env, "[%u] Invalid kind:%u",
3979 env->log_type_id, BTF_INFO_KIND(t->info));
3980 return -EINVAL;
3981 }
3982
3983 if (!btf_name_offset_valid(env->btf, t->name_off)) {
3984 btf_verifier_log(env, "[%u] Invalid name_offset:%u",
3985 env->log_type_id, t->name_off);
3986 return -EINVAL;
3987 }
3988
3989 var_meta_size = btf_type_ops(t)->check_meta(env, t, meta_left);
3990 if (var_meta_size < 0)
3991 return var_meta_size;
3992
3993 meta_left -= var_meta_size;
3994
3995 return saved_meta_left - meta_left;
3996 }
3997
btf_check_all_metas(struct btf_verifier_env * env)3998 static int btf_check_all_metas(struct btf_verifier_env *env)
3999 {
4000 struct btf *btf = env->btf;
4001 struct btf_header *hdr;
4002 void *cur, *end;
4003
4004 hdr = &btf->hdr;
4005 cur = btf->nohdr_data + hdr->type_off;
4006 end = cur + hdr->type_len;
4007
4008 env->log_type_id = btf->base_btf ? btf->start_id : 1;
4009 while (cur < end) {
4010 struct btf_type *t = cur;
4011 s32 meta_size;
4012
4013 meta_size = btf_check_meta(env, t, end - cur);
4014 if (meta_size < 0)
4015 return meta_size;
4016
4017 btf_add_type(env, t);
4018 cur += meta_size;
4019 env->log_type_id++;
4020 }
4021
4022 return 0;
4023 }
4024
btf_resolve_valid(struct btf_verifier_env * env,const struct btf_type * t,u32 type_id)4025 static bool btf_resolve_valid(struct btf_verifier_env *env,
4026 const struct btf_type *t,
4027 u32 type_id)
4028 {
4029 struct btf *btf = env->btf;
4030
4031 if (!env_type_is_resolved(env, type_id))
4032 return false;
4033
4034 if (btf_type_is_struct(t) || btf_type_is_datasec(t))
4035 return !btf_resolved_type_id(btf, type_id) &&
4036 !btf_resolved_type_size(btf, type_id);
4037
4038 if (btf_type_is_modifier(t) || btf_type_is_ptr(t) ||
4039 btf_type_is_var(t)) {
4040 t = btf_type_id_resolve(btf, &type_id);
4041 return t &&
4042 !btf_type_is_modifier(t) &&
4043 !btf_type_is_var(t) &&
4044 !btf_type_is_datasec(t);
4045 }
4046
4047 if (btf_type_is_array(t)) {
4048 const struct btf_array *array = btf_type_array(t);
4049 const struct btf_type *elem_type;
4050 u32 elem_type_id = array->type;
4051 u32 elem_size;
4052
4053 elem_type = btf_type_id_size(btf, &elem_type_id, &elem_size);
4054 return elem_type && !btf_type_is_modifier(elem_type) &&
4055 (array->nelems * elem_size ==
4056 btf_resolved_type_size(btf, type_id));
4057 }
4058
4059 return false;
4060 }
4061
btf_resolve(struct btf_verifier_env * env,const struct btf_type * t,u32 type_id)4062 static int btf_resolve(struct btf_verifier_env *env,
4063 const struct btf_type *t, u32 type_id)
4064 {
4065 u32 save_log_type_id = env->log_type_id;
4066 const struct resolve_vertex *v;
4067 int err = 0;
4068
4069 env->resolve_mode = RESOLVE_TBD;
4070 env_stack_push(env, t, type_id);
4071 while (!err && (v = env_stack_peak(env))) {
4072 env->log_type_id = v->type_id;
4073 err = btf_type_ops(v->t)->resolve(env, v);
4074 }
4075
4076 env->log_type_id = type_id;
4077 if (err == -E2BIG) {
4078 btf_verifier_log_type(env, t,
4079 "Exceeded max resolving depth:%u",
4080 MAX_RESOLVE_DEPTH);
4081 } else if (err == -EEXIST) {
4082 btf_verifier_log_type(env, t, "Loop detected");
4083 }
4084
4085 /* Final sanity check */
4086 if (!err && !btf_resolve_valid(env, t, type_id)) {
4087 btf_verifier_log_type(env, t, "Invalid resolve state");
4088 err = -EINVAL;
4089 }
4090
4091 env->log_type_id = save_log_type_id;
4092 return err;
4093 }
4094
btf_check_all_types(struct btf_verifier_env * env)4095 static int btf_check_all_types(struct btf_verifier_env *env)
4096 {
4097 struct btf *btf = env->btf;
4098 const struct btf_type *t;
4099 u32 type_id, i;
4100 int err;
4101
4102 err = env_resolve_init(env);
4103 if (err)
4104 return err;
4105
4106 env->phase++;
4107 for (i = btf->base_btf ? 0 : 1; i < btf->nr_types; i++) {
4108 type_id = btf->start_id + i;
4109 t = btf_type_by_id(btf, type_id);
4110
4111 env->log_type_id = type_id;
4112 if (btf_type_needs_resolve(t) &&
4113 !env_type_is_resolved(env, type_id)) {
4114 err = btf_resolve(env, t, type_id);
4115 if (err)
4116 return err;
4117 }
4118
4119 if (btf_type_is_func_proto(t)) {
4120 err = btf_func_proto_check(env, t);
4121 if (err)
4122 return err;
4123 }
4124
4125 if (btf_type_is_func(t)) {
4126 err = btf_func_check(env, t);
4127 if (err)
4128 return err;
4129 }
4130 }
4131
4132 return 0;
4133 }
4134
btf_parse_type_sec(struct btf_verifier_env * env)4135 static int btf_parse_type_sec(struct btf_verifier_env *env)
4136 {
4137 const struct btf_header *hdr = &env->btf->hdr;
4138 int err;
4139
4140 /* Type section must align to 4 bytes */
4141 if (hdr->type_off & (sizeof(u32) - 1)) {
4142 btf_verifier_log(env, "Unaligned type_off");
4143 return -EINVAL;
4144 }
4145
4146 if (!env->btf->base_btf && !hdr->type_len) {
4147 btf_verifier_log(env, "No type found");
4148 return -EINVAL;
4149 }
4150
4151 err = btf_check_all_metas(env);
4152 if (err)
4153 return err;
4154
4155 return btf_check_all_types(env);
4156 }
4157
btf_parse_str_sec(struct btf_verifier_env * env)4158 static int btf_parse_str_sec(struct btf_verifier_env *env)
4159 {
4160 const struct btf_header *hdr;
4161 struct btf *btf = env->btf;
4162 const char *start, *end;
4163
4164 hdr = &btf->hdr;
4165 start = btf->nohdr_data + hdr->str_off;
4166 end = start + hdr->str_len;
4167
4168 if (end != btf->data + btf->data_size) {
4169 btf_verifier_log(env, "String section is not at the end");
4170 return -EINVAL;
4171 }
4172
4173 btf->strings = start;
4174
4175 if (btf->base_btf && !hdr->str_len)
4176 return 0;
4177 if (!hdr->str_len || hdr->str_len - 1 > BTF_MAX_NAME_OFFSET || end[-1]) {
4178 btf_verifier_log(env, "Invalid string section");
4179 return -EINVAL;
4180 }
4181 if (!btf->base_btf && start[0]) {
4182 btf_verifier_log(env, "Invalid string section");
4183 return -EINVAL;
4184 }
4185
4186 return 0;
4187 }
4188
4189 static const size_t btf_sec_info_offset[] = {
4190 offsetof(struct btf_header, type_off),
4191 offsetof(struct btf_header, str_off),
4192 };
4193
btf_sec_info_cmp(const void * a,const void * b)4194 static int btf_sec_info_cmp(const void *a, const void *b)
4195 {
4196 const struct btf_sec_info *x = a;
4197 const struct btf_sec_info *y = b;
4198
4199 return (int)(x->off - y->off) ? : (int)(x->len - y->len);
4200 }
4201
btf_check_sec_info(struct btf_verifier_env * env,u32 btf_data_size)4202 static int btf_check_sec_info(struct btf_verifier_env *env,
4203 u32 btf_data_size)
4204 {
4205 struct btf_sec_info secs[ARRAY_SIZE(btf_sec_info_offset)];
4206 u32 total, expected_total, i;
4207 const struct btf_header *hdr;
4208 const struct btf *btf;
4209
4210 btf = env->btf;
4211 hdr = &btf->hdr;
4212
4213 /* Populate the secs from hdr */
4214 for (i = 0; i < ARRAY_SIZE(btf_sec_info_offset); i++)
4215 secs[i] = *(struct btf_sec_info *)((void *)hdr +
4216 btf_sec_info_offset[i]);
4217
4218 sort(secs, ARRAY_SIZE(btf_sec_info_offset),
4219 sizeof(struct btf_sec_info), btf_sec_info_cmp, NULL);
4220
4221 /* Check for gaps and overlap among sections */
4222 total = 0;
4223 expected_total = btf_data_size - hdr->hdr_len;
4224 for (i = 0; i < ARRAY_SIZE(btf_sec_info_offset); i++) {
4225 if (expected_total < secs[i].off) {
4226 btf_verifier_log(env, "Invalid section offset");
4227 return -EINVAL;
4228 }
4229 if (total < secs[i].off) {
4230 /* gap */
4231 btf_verifier_log(env, "Unsupported section found");
4232 return -EINVAL;
4233 }
4234 if (total > secs[i].off) {
4235 btf_verifier_log(env, "Section overlap found");
4236 return -EINVAL;
4237 }
4238 if (expected_total - total < secs[i].len) {
4239 btf_verifier_log(env,
4240 "Total section length too long");
4241 return -EINVAL;
4242 }
4243 total += secs[i].len;
4244 }
4245
4246 /* There is data other than hdr and known sections */
4247 if (expected_total != total) {
4248 btf_verifier_log(env, "Unsupported section found");
4249 return -EINVAL;
4250 }
4251
4252 return 0;
4253 }
4254
btf_parse_hdr(struct btf_verifier_env * env)4255 static int btf_parse_hdr(struct btf_verifier_env *env)
4256 {
4257 u32 hdr_len, hdr_copy, btf_data_size;
4258 const struct btf_header *hdr;
4259 struct btf *btf;
4260 int err;
4261
4262 btf = env->btf;
4263 btf_data_size = btf->data_size;
4264
4265 if (btf_data_size <
4266 offsetof(struct btf_header, hdr_len) + sizeof(hdr->hdr_len)) {
4267 btf_verifier_log(env, "hdr_len not found");
4268 return -EINVAL;
4269 }
4270
4271 hdr = btf->data;
4272 hdr_len = hdr->hdr_len;
4273 if (btf_data_size < hdr_len) {
4274 btf_verifier_log(env, "btf_header not found");
4275 return -EINVAL;
4276 }
4277
4278 /* Ensure the unsupported header fields are zero */
4279 if (hdr_len > sizeof(btf->hdr)) {
4280 u8 *expected_zero = btf->data + sizeof(btf->hdr);
4281 u8 *end = btf->data + hdr_len;
4282
4283 for (; expected_zero < end; expected_zero++) {
4284 if (*expected_zero) {
4285 btf_verifier_log(env, "Unsupported btf_header");
4286 return -E2BIG;
4287 }
4288 }
4289 }
4290
4291 hdr_copy = min_t(u32, hdr_len, sizeof(btf->hdr));
4292 memcpy(&btf->hdr, btf->data, hdr_copy);
4293
4294 hdr = &btf->hdr;
4295
4296 btf_verifier_log_hdr(env, btf_data_size);
4297
4298 if (hdr->magic != BTF_MAGIC) {
4299 btf_verifier_log(env, "Invalid magic");
4300 return -EINVAL;
4301 }
4302
4303 if (hdr->version != BTF_VERSION) {
4304 btf_verifier_log(env, "Unsupported version");
4305 return -ENOTSUPP;
4306 }
4307
4308 if (hdr->flags) {
4309 btf_verifier_log(env, "Unsupported flags");
4310 return -ENOTSUPP;
4311 }
4312
4313 if (!btf->base_btf && btf_data_size == hdr->hdr_len) {
4314 btf_verifier_log(env, "No data");
4315 return -EINVAL;
4316 }
4317
4318 err = btf_check_sec_info(env, btf_data_size);
4319 if (err)
4320 return err;
4321
4322 return 0;
4323 }
4324
btf_parse(bpfptr_t btf_data,u32 btf_data_size,u32 log_level,char __user * log_ubuf,u32 log_size)4325 static struct btf *btf_parse(bpfptr_t btf_data, u32 btf_data_size,
4326 u32 log_level, char __user *log_ubuf, u32 log_size)
4327 {
4328 struct btf_verifier_env *env = NULL;
4329 struct bpf_verifier_log *log;
4330 struct btf *btf = NULL;
4331 u8 *data;
4332 int err;
4333
4334 if (btf_data_size > BTF_MAX_SIZE)
4335 return ERR_PTR(-E2BIG);
4336
4337 env = kzalloc(sizeof(*env), GFP_KERNEL | __GFP_NOWARN);
4338 if (!env)
4339 return ERR_PTR(-ENOMEM);
4340
4341 log = &env->log;
4342 if (log_level || log_ubuf || log_size) {
4343 /* user requested verbose verifier output
4344 * and supplied buffer to store the verification trace
4345 */
4346 log->level = log_level;
4347 log->ubuf = log_ubuf;
4348 log->len_total = log_size;
4349
4350 /* log attributes have to be sane */
4351 if (!bpf_verifier_log_attr_valid(log)) {
4352 err = -EINVAL;
4353 goto errout;
4354 }
4355 }
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 data = kvmalloc(btf_data_size, GFP_KERNEL | __GFP_NOWARN);
4365 if (!data) {
4366 err = -ENOMEM;
4367 goto errout;
4368 }
4369
4370 btf->data = data;
4371 btf->data_size = btf_data_size;
4372
4373 if (copy_from_bpfptr(data, btf_data, btf_data_size)) {
4374 err = -EFAULT;
4375 goto errout;
4376 }
4377
4378 err = btf_parse_hdr(env);
4379 if (err)
4380 goto errout;
4381
4382 btf->nohdr_data = btf->data + btf->hdr.hdr_len;
4383
4384 err = btf_parse_str_sec(env);
4385 if (err)
4386 goto errout;
4387
4388 err = btf_parse_type_sec(env);
4389 if (err)
4390 goto errout;
4391
4392 if (log->level && bpf_verifier_log_full(log)) {
4393 err = -ENOSPC;
4394 goto errout;
4395 }
4396
4397 btf_verifier_env_free(env);
4398 refcount_set(&btf->refcnt, 1);
4399 return btf;
4400
4401 errout:
4402 btf_verifier_env_free(env);
4403 if (btf)
4404 btf_free(btf);
4405 return ERR_PTR(err);
4406 }
4407
4408 extern char __weak __start_BTF[];
4409 extern char __weak __stop_BTF[];
4410 extern struct btf *btf_vmlinux;
4411
4412 #define BPF_MAP_TYPE(_id, _ops)
4413 #define BPF_LINK_TYPE(_id, _name)
4414 static union {
4415 struct bpf_ctx_convert {
4416 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
4417 prog_ctx_type _id##_prog; \
4418 kern_ctx_type _id##_kern;
4419 #include <linux/bpf_types.h>
4420 #undef BPF_PROG_TYPE
4421 } *__t;
4422 /* 't' is written once under lock. Read many times. */
4423 const struct btf_type *t;
4424 } bpf_ctx_convert;
4425 enum {
4426 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
4427 __ctx_convert##_id,
4428 #include <linux/bpf_types.h>
4429 #undef BPF_PROG_TYPE
4430 __ctx_convert_unused, /* to avoid empty enum in extreme .config */
4431 };
4432 static u8 bpf_ctx_convert_map[] = {
4433 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
4434 [_id] = __ctx_convert##_id,
4435 #include <linux/bpf_types.h>
4436 #undef BPF_PROG_TYPE
4437 0, /* avoid empty array */
4438 };
4439 #undef BPF_MAP_TYPE
4440 #undef BPF_LINK_TYPE
4441
4442 static const struct btf_member *
btf_get_prog_ctx_type(struct bpf_verifier_log * log,const struct btf * btf,const struct btf_type * t,enum bpf_prog_type prog_type,int arg)4443 btf_get_prog_ctx_type(struct bpf_verifier_log *log, const struct btf *btf,
4444 const struct btf_type *t, enum bpf_prog_type prog_type,
4445 int arg)
4446 {
4447 const struct btf_type *conv_struct;
4448 const struct btf_type *ctx_struct;
4449 const struct btf_member *ctx_type;
4450 const char *tname, *ctx_tname;
4451
4452 conv_struct = bpf_ctx_convert.t;
4453 if (!conv_struct) {
4454 bpf_log(log, "btf_vmlinux is malformed\n");
4455 return NULL;
4456 }
4457 t = btf_type_by_id(btf, t->type);
4458 while (btf_type_is_modifier(t))
4459 t = btf_type_by_id(btf, t->type);
4460 if (!btf_type_is_struct(t)) {
4461 /* Only pointer to struct is supported for now.
4462 * That means that BPF_PROG_TYPE_TRACEPOINT with BTF
4463 * is not supported yet.
4464 * BPF_PROG_TYPE_RAW_TRACEPOINT is fine.
4465 */
4466 return NULL;
4467 }
4468 tname = btf_name_by_offset(btf, t->name_off);
4469 if (!tname) {
4470 bpf_log(log, "arg#%d struct doesn't have a name\n", arg);
4471 return NULL;
4472 }
4473 /* prog_type is valid bpf program type. No need for bounds check. */
4474 ctx_type = btf_type_member(conv_struct) + bpf_ctx_convert_map[prog_type] * 2;
4475 /* ctx_struct is a pointer to prog_ctx_type in vmlinux.
4476 * Like 'struct __sk_buff'
4477 */
4478 ctx_struct = btf_type_by_id(btf_vmlinux, ctx_type->type);
4479 if (!ctx_struct)
4480 /* should not happen */
4481 return NULL;
4482 again:
4483 ctx_tname = btf_name_by_offset(btf_vmlinux, ctx_struct->name_off);
4484 if (!ctx_tname) {
4485 /* should not happen */
4486 bpf_log(log, "Please fix kernel include/linux/bpf_types.h\n");
4487 return NULL;
4488 }
4489 /* only compare that prog's ctx type name is the same as
4490 * kernel expects. No need to compare field by field.
4491 * It's ok for bpf prog to do:
4492 * struct __sk_buff {};
4493 * int socket_filter_bpf_prog(struct __sk_buff *skb)
4494 * { // no fields of skb are ever used }
4495 */
4496 if (strcmp(ctx_tname, tname)) {
4497 /* bpf_user_pt_regs_t is a typedef, so resolve it to
4498 * underlying struct and check name again
4499 */
4500 if (!btf_type_is_modifier(ctx_struct))
4501 return NULL;
4502 while (btf_type_is_modifier(ctx_struct))
4503 ctx_struct = btf_type_by_id(btf_vmlinux, ctx_struct->type);
4504 goto again;
4505 }
4506 return ctx_type;
4507 }
4508
4509 static const struct bpf_map_ops * const btf_vmlinux_map_ops[] = {
4510 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
4511 #define BPF_LINK_TYPE(_id, _name)
4512 #define BPF_MAP_TYPE(_id, _ops) \
4513 [_id] = &_ops,
4514 #include <linux/bpf_types.h>
4515 #undef BPF_PROG_TYPE
4516 #undef BPF_LINK_TYPE
4517 #undef BPF_MAP_TYPE
4518 };
4519
btf_vmlinux_map_ids_init(const struct btf * btf,struct bpf_verifier_log * log)4520 static int btf_vmlinux_map_ids_init(const struct btf *btf,
4521 struct bpf_verifier_log *log)
4522 {
4523 const struct bpf_map_ops *ops;
4524 int i, btf_id;
4525
4526 for (i = 0; i < ARRAY_SIZE(btf_vmlinux_map_ops); ++i) {
4527 ops = btf_vmlinux_map_ops[i];
4528 if (!ops || (!ops->map_btf_name && !ops->map_btf_id))
4529 continue;
4530 if (!ops->map_btf_name || !ops->map_btf_id) {
4531 bpf_log(log, "map type %d is misconfigured\n", i);
4532 return -EINVAL;
4533 }
4534 btf_id = btf_find_by_name_kind(btf, ops->map_btf_name,
4535 BTF_KIND_STRUCT);
4536 if (btf_id < 0)
4537 return btf_id;
4538 *ops->map_btf_id = btf_id;
4539 }
4540
4541 return 0;
4542 }
4543
btf_translate_to_vmlinux(struct bpf_verifier_log * log,struct btf * btf,const struct btf_type * t,enum bpf_prog_type prog_type,int arg)4544 static int btf_translate_to_vmlinux(struct bpf_verifier_log *log,
4545 struct btf *btf,
4546 const struct btf_type *t,
4547 enum bpf_prog_type prog_type,
4548 int arg)
4549 {
4550 const struct btf_member *prog_ctx_type, *kern_ctx_type;
4551
4552 prog_ctx_type = btf_get_prog_ctx_type(log, btf, t, prog_type, arg);
4553 if (!prog_ctx_type)
4554 return -ENOENT;
4555 kern_ctx_type = prog_ctx_type + 1;
4556 return kern_ctx_type->type;
4557 }
4558
4559 BTF_ID_LIST(bpf_ctx_convert_btf_id)
BTF_ID(struct,bpf_ctx_convert)4560 BTF_ID(struct, bpf_ctx_convert)
4561
4562 struct btf *btf_parse_vmlinux(void)
4563 {
4564 struct btf_verifier_env *env = NULL;
4565 struct bpf_verifier_log *log;
4566 struct btf *btf = NULL;
4567 int err;
4568
4569 env = kzalloc(sizeof(*env), GFP_KERNEL | __GFP_NOWARN);
4570 if (!env)
4571 return ERR_PTR(-ENOMEM);
4572
4573 log = &env->log;
4574 log->level = BPF_LOG_KERNEL;
4575
4576 btf = kzalloc(sizeof(*btf), GFP_KERNEL | __GFP_NOWARN);
4577 if (!btf) {
4578 err = -ENOMEM;
4579 goto errout;
4580 }
4581 env->btf = btf;
4582
4583 btf->data = __start_BTF;
4584 btf->data_size = __stop_BTF - __start_BTF;
4585 btf->kernel_btf = true;
4586 snprintf(btf->name, sizeof(btf->name), "vmlinux");
4587
4588 err = btf_parse_hdr(env);
4589 if (err)
4590 goto errout;
4591
4592 btf->nohdr_data = btf->data + btf->hdr.hdr_len;
4593
4594 err = btf_parse_str_sec(env);
4595 if (err)
4596 goto errout;
4597
4598 err = btf_check_all_metas(env);
4599 if (err)
4600 goto errout;
4601
4602 /* btf_parse_vmlinux() runs under bpf_verifier_lock */
4603 bpf_ctx_convert.t = btf_type_by_id(btf, bpf_ctx_convert_btf_id[0]);
4604
4605 /* find bpf map structs for map_ptr access checking */
4606 err = btf_vmlinux_map_ids_init(btf, log);
4607 if (err < 0)
4608 goto errout;
4609
4610 bpf_struct_ops_init(btf, log);
4611
4612 refcount_set(&btf->refcnt, 1);
4613
4614 err = btf_alloc_id(btf);
4615 if (err)
4616 goto errout;
4617
4618 btf_verifier_env_free(env);
4619 return btf;
4620
4621 errout:
4622 btf_verifier_env_free(env);
4623 if (btf) {
4624 kvfree(btf->types);
4625 kfree(btf);
4626 }
4627 return ERR_PTR(err);
4628 }
4629
4630 #ifdef CONFIG_DEBUG_INFO_BTF_MODULES
4631
btf_parse_module(const char * module_name,const void * data,unsigned int data_size)4632 static struct btf *btf_parse_module(const char *module_name, const void *data, unsigned int data_size)
4633 {
4634 struct btf_verifier_env *env = NULL;
4635 struct bpf_verifier_log *log;
4636 struct btf *btf = NULL, *base_btf;
4637 int err;
4638
4639 base_btf = bpf_get_btf_vmlinux();
4640 if (IS_ERR(base_btf))
4641 return base_btf;
4642 if (!base_btf)
4643 return ERR_PTR(-EINVAL);
4644
4645 env = kzalloc(sizeof(*env), GFP_KERNEL | __GFP_NOWARN);
4646 if (!env)
4647 return ERR_PTR(-ENOMEM);
4648
4649 log = &env->log;
4650 log->level = BPF_LOG_KERNEL;
4651
4652 btf = kzalloc(sizeof(*btf), GFP_KERNEL | __GFP_NOWARN);
4653 if (!btf) {
4654 err = -ENOMEM;
4655 goto errout;
4656 }
4657 env->btf = btf;
4658
4659 btf->base_btf = base_btf;
4660 btf->start_id = base_btf->nr_types;
4661 btf->start_str_off = base_btf->hdr.str_len;
4662 btf->kernel_btf = true;
4663 snprintf(btf->name, sizeof(btf->name), "%s", module_name);
4664
4665 btf->data = kvmalloc(data_size, GFP_KERNEL | __GFP_NOWARN);
4666 if (!btf->data) {
4667 err = -ENOMEM;
4668 goto errout;
4669 }
4670 memcpy(btf->data, data, data_size);
4671 btf->data_size = data_size;
4672
4673 err = btf_parse_hdr(env);
4674 if (err)
4675 goto errout;
4676
4677 btf->nohdr_data = btf->data + btf->hdr.hdr_len;
4678
4679 err = btf_parse_str_sec(env);
4680 if (err)
4681 goto errout;
4682
4683 err = btf_check_all_metas(env);
4684 if (err)
4685 goto errout;
4686
4687 btf_verifier_env_free(env);
4688 refcount_set(&btf->refcnt, 1);
4689 return btf;
4690
4691 errout:
4692 btf_verifier_env_free(env);
4693 if (btf) {
4694 kvfree(btf->data);
4695 kvfree(btf->types);
4696 kfree(btf);
4697 }
4698 return ERR_PTR(err);
4699 }
4700
4701 #endif /* CONFIG_DEBUG_INFO_BTF_MODULES */
4702
bpf_prog_get_target_btf(const struct bpf_prog * prog)4703 struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog)
4704 {
4705 struct bpf_prog *tgt_prog = prog->aux->dst_prog;
4706
4707 if (tgt_prog)
4708 return tgt_prog->aux->btf;
4709 else
4710 return prog->aux->attach_btf;
4711 }
4712
is_string_ptr(struct btf * btf,const struct btf_type * t)4713 static bool is_string_ptr(struct btf *btf, const struct btf_type *t)
4714 {
4715 /* t comes in already as a pointer */
4716 t = btf_type_by_id(btf, t->type);
4717
4718 /* allow const */
4719 if (BTF_INFO_KIND(t->info) == BTF_KIND_CONST)
4720 t = btf_type_by_id(btf, t->type);
4721
4722 /* char, signed char, unsigned char */
4723 return btf_type_is_int(t) && t->size == 1;
4724 }
4725
btf_ctx_access(int off,int size,enum bpf_access_type type,const struct bpf_prog * prog,struct bpf_insn_access_aux * info)4726 bool btf_ctx_access(int off, int size, enum bpf_access_type type,
4727 const struct bpf_prog *prog,
4728 struct bpf_insn_access_aux *info)
4729 {
4730 const struct btf_type *t = prog->aux->attach_func_proto;
4731 struct bpf_prog *tgt_prog = prog->aux->dst_prog;
4732 struct btf *btf = bpf_prog_get_target_btf(prog);
4733 const char *tname = prog->aux->attach_func_name;
4734 struct bpf_verifier_log *log = info->log;
4735 const struct btf_param *args;
4736 u32 nr_args, arg;
4737 int i, ret;
4738
4739 if (off % 8) {
4740 bpf_log(log, "func '%s' offset %d is not multiple of 8\n",
4741 tname, off);
4742 return false;
4743 }
4744 arg = off / 8;
4745 args = (const struct btf_param *)(t + 1);
4746 /* if (t == NULL) Fall back to default BPF prog with
4747 * MAX_BPF_FUNC_REG_ARGS u64 arguments.
4748 */
4749 nr_args = t ? btf_type_vlen(t) : MAX_BPF_FUNC_REG_ARGS;
4750 if (prog->aux->attach_btf_trace) {
4751 /* skip first 'void *__data' argument in btf_trace_##name typedef */
4752 args++;
4753 nr_args--;
4754 }
4755
4756 if (arg > nr_args) {
4757 bpf_log(log, "func '%s' doesn't have %d-th argument\n",
4758 tname, arg + 1);
4759 return false;
4760 }
4761
4762 if (arg == nr_args) {
4763 switch (prog->expected_attach_type) {
4764 case BPF_LSM_MAC:
4765 case BPF_TRACE_FEXIT:
4766 /* When LSM programs are attached to void LSM hooks
4767 * they use FEXIT trampolines and when attached to
4768 * int LSM hooks, they use MODIFY_RETURN trampolines.
4769 *
4770 * While the LSM programs are BPF_MODIFY_RETURN-like
4771 * the check:
4772 *
4773 * if (ret_type != 'int')
4774 * return -EINVAL;
4775 *
4776 * is _not_ done here. This is still safe as LSM hooks
4777 * have only void and int return types.
4778 */
4779 if (!t)
4780 return true;
4781 t = btf_type_by_id(btf, t->type);
4782 break;
4783 case BPF_MODIFY_RETURN:
4784 /* For now the BPF_MODIFY_RETURN can only be attached to
4785 * functions that return an int.
4786 */
4787 if (!t)
4788 return false;
4789
4790 t = btf_type_skip_modifiers(btf, t->type, NULL);
4791 if (!btf_type_is_small_int(t)) {
4792 bpf_log(log,
4793 "ret type %s not allowed for fmod_ret\n",
4794 btf_kind_str[BTF_INFO_KIND(t->info)]);
4795 return false;
4796 }
4797 break;
4798 default:
4799 bpf_log(log, "func '%s' doesn't have %d-th argument\n",
4800 tname, arg + 1);
4801 return false;
4802 }
4803 } else {
4804 if (!t)
4805 /* Default prog with MAX_BPF_FUNC_REG_ARGS args */
4806 return true;
4807 t = btf_type_by_id(btf, args[arg].type);
4808 }
4809
4810 /* skip modifiers */
4811 while (btf_type_is_modifier(t))
4812 t = btf_type_by_id(btf, t->type);
4813 if (btf_type_is_small_int(t) || btf_type_is_enum(t))
4814 /* accessing a scalar */
4815 return true;
4816 if (!btf_type_is_ptr(t)) {
4817 bpf_log(log,
4818 "func '%s' arg%d '%s' has type %s. Only pointer access is allowed\n",
4819 tname, arg,
4820 __btf_name_by_offset(btf, t->name_off),
4821 btf_kind_str[BTF_INFO_KIND(t->info)]);
4822 return false;
4823 }
4824
4825 /* check for PTR_TO_RDONLY_BUF_OR_NULL or PTR_TO_RDWR_BUF_OR_NULL */
4826 for (i = 0; i < prog->aux->ctx_arg_info_size; i++) {
4827 const struct bpf_ctx_arg_aux *ctx_arg_info = &prog->aux->ctx_arg_info[i];
4828 u32 type, flag;
4829
4830 type = base_type(ctx_arg_info->reg_type);
4831 flag = type_flag(ctx_arg_info->reg_type);
4832 if (ctx_arg_info->offset == off && type == PTR_TO_BUF &&
4833 (flag & PTR_MAYBE_NULL)) {
4834 info->reg_type = ctx_arg_info->reg_type;
4835 return true;
4836 }
4837 }
4838
4839 if (t->type == 0)
4840 /* This is a pointer to void.
4841 * It is the same as scalar from the verifier safety pov.
4842 * No further pointer walking is allowed.
4843 */
4844 return true;
4845
4846 if (is_string_ptr(btf, t))
4847 return true;
4848
4849 /* this is a pointer to another type */
4850 for (i = 0; i < prog->aux->ctx_arg_info_size; i++) {
4851 const struct bpf_ctx_arg_aux *ctx_arg_info = &prog->aux->ctx_arg_info[i];
4852
4853 if (ctx_arg_info->offset == off) {
4854 if (!ctx_arg_info->btf_id) {
4855 bpf_log(log,"invalid btf_id for context argument offset %u\n", off);
4856 return false;
4857 }
4858
4859 info->reg_type = ctx_arg_info->reg_type;
4860 info->btf = btf_vmlinux;
4861 info->btf_id = ctx_arg_info->btf_id;
4862 return true;
4863 }
4864 }
4865
4866 info->reg_type = PTR_TO_BTF_ID;
4867 if (tgt_prog) {
4868 enum bpf_prog_type tgt_type;
4869
4870 if (tgt_prog->type == BPF_PROG_TYPE_EXT)
4871 tgt_type = tgt_prog->aux->saved_dst_prog_type;
4872 else
4873 tgt_type = tgt_prog->type;
4874
4875 ret = btf_translate_to_vmlinux(log, btf, t, tgt_type, arg);
4876 if (ret > 0) {
4877 info->btf = btf_vmlinux;
4878 info->btf_id = ret;
4879 return true;
4880 } else {
4881 return false;
4882 }
4883 }
4884
4885 info->btf = btf;
4886 info->btf_id = t->type;
4887 t = btf_type_by_id(btf, t->type);
4888 /* skip modifiers */
4889 while (btf_type_is_modifier(t)) {
4890 info->btf_id = t->type;
4891 t = btf_type_by_id(btf, t->type);
4892 }
4893 if (!btf_type_is_struct(t)) {
4894 bpf_log(log,
4895 "func '%s' arg%d type %s is not a struct\n",
4896 tname, arg, btf_kind_str[BTF_INFO_KIND(t->info)]);
4897 return false;
4898 }
4899 bpf_log(log, "func '%s' arg%d has btf_id %d type %s '%s'\n",
4900 tname, arg, info->btf_id, btf_kind_str[BTF_INFO_KIND(t->info)],
4901 __btf_name_by_offset(btf, t->name_off));
4902 return true;
4903 }
4904
4905 enum bpf_struct_walk_result {
4906 /* < 0 error */
4907 WALK_SCALAR = 0,
4908 WALK_PTR,
4909 WALK_STRUCT,
4910 };
4911
btf_struct_walk(struct bpf_verifier_log * log,const struct btf * btf,const struct btf_type * t,int off,int size,u32 * next_btf_id)4912 static int btf_struct_walk(struct bpf_verifier_log *log, const struct btf *btf,
4913 const struct btf_type *t, int off, int size,
4914 u32 *next_btf_id)
4915 {
4916 u32 i, moff, mtrue_end, msize = 0, total_nelems = 0;
4917 const struct btf_type *mtype, *elem_type = NULL;
4918 const struct btf_member *member;
4919 const char *tname, *mname;
4920 u32 vlen, elem_id, mid;
4921
4922 again:
4923 tname = __btf_name_by_offset(btf, t->name_off);
4924 if (!btf_type_is_struct(t)) {
4925 bpf_log(log, "Type '%s' is not a struct\n", tname);
4926 return -EINVAL;
4927 }
4928
4929 vlen = btf_type_vlen(t);
4930 if (off + size > t->size) {
4931 /* If the last element is a variable size array, we may
4932 * need to relax the rule.
4933 */
4934 struct btf_array *array_elem;
4935
4936 if (vlen == 0)
4937 goto error;
4938
4939 member = btf_type_member(t) + vlen - 1;
4940 mtype = btf_type_skip_modifiers(btf, member->type,
4941 NULL);
4942 if (!btf_type_is_array(mtype))
4943 goto error;
4944
4945 array_elem = (struct btf_array *)(mtype + 1);
4946 if (array_elem->nelems != 0)
4947 goto error;
4948
4949 moff = btf_member_bit_offset(t, member) / 8;
4950 if (off < moff)
4951 goto error;
4952
4953 /* Only allow structure for now, can be relaxed for
4954 * other types later.
4955 */
4956 t = btf_type_skip_modifiers(btf, array_elem->type,
4957 NULL);
4958 if (!btf_type_is_struct(t))
4959 goto error;
4960
4961 off = (off - moff) % t->size;
4962 goto again;
4963
4964 error:
4965 bpf_log(log, "access beyond struct %s at off %u size %u\n",
4966 tname, off, size);
4967 return -EACCES;
4968 }
4969
4970 for_each_member(i, t, member) {
4971 /* offset of the field in bytes */
4972 moff = btf_member_bit_offset(t, member) / 8;
4973 if (off + size <= moff)
4974 /* won't find anything, field is already too far */
4975 break;
4976
4977 if (btf_member_bitfield_size(t, member)) {
4978 u32 end_bit = btf_member_bit_offset(t, member) +
4979 btf_member_bitfield_size(t, member);
4980
4981 /* off <= moff instead of off == moff because clang
4982 * does not generate a BTF member for anonymous
4983 * bitfield like the ":16" here:
4984 * struct {
4985 * int :16;
4986 * int x:8;
4987 * };
4988 */
4989 if (off <= moff &&
4990 BITS_ROUNDUP_BYTES(end_bit) <= off + size)
4991 return WALK_SCALAR;
4992
4993 /* off may be accessing a following member
4994 *
4995 * or
4996 *
4997 * Doing partial access at either end of this
4998 * bitfield. Continue on this case also to
4999 * treat it as not accessing this bitfield
5000 * and eventually error out as field not
5001 * found to keep it simple.
5002 * It could be relaxed if there was a legit
5003 * partial access case later.
5004 */
5005 continue;
5006 }
5007
5008 /* In case of "off" is pointing to holes of a struct */
5009 if (off < moff)
5010 break;
5011
5012 /* type of the field */
5013 mid = member->type;
5014 mtype = btf_type_by_id(btf, member->type);
5015 mname = __btf_name_by_offset(btf, member->name_off);
5016
5017 mtype = __btf_resolve_size(btf, mtype, &msize,
5018 &elem_type, &elem_id, &total_nelems,
5019 &mid);
5020 if (IS_ERR(mtype)) {
5021 bpf_log(log, "field %s doesn't have size\n", mname);
5022 return -EFAULT;
5023 }
5024
5025 mtrue_end = moff + msize;
5026 if (off >= mtrue_end)
5027 /* no overlap with member, keep iterating */
5028 continue;
5029
5030 if (btf_type_is_array(mtype)) {
5031 u32 elem_idx;
5032
5033 /* __btf_resolve_size() above helps to
5034 * linearize a multi-dimensional array.
5035 *
5036 * The logic here is treating an array
5037 * in a struct as the following way:
5038 *
5039 * struct outer {
5040 * struct inner array[2][2];
5041 * };
5042 *
5043 * looks like:
5044 *
5045 * struct outer {
5046 * struct inner array_elem0;
5047 * struct inner array_elem1;
5048 * struct inner array_elem2;
5049 * struct inner array_elem3;
5050 * };
5051 *
5052 * When accessing outer->array[1][0], it moves
5053 * moff to "array_elem2", set mtype to
5054 * "struct inner", and msize also becomes
5055 * sizeof(struct inner). Then most of the
5056 * remaining logic will fall through without
5057 * caring the current member is an array or
5058 * not.
5059 *
5060 * Unlike mtype/msize/moff, mtrue_end does not
5061 * change. The naming difference ("_true") tells
5062 * that it is not always corresponding to
5063 * the current mtype/msize/moff.
5064 * It is the true end of the current
5065 * member (i.e. array in this case). That
5066 * will allow an int array to be accessed like
5067 * a scratch space,
5068 * i.e. allow access beyond the size of
5069 * the array's element as long as it is
5070 * within the mtrue_end boundary.
5071 */
5072
5073 /* skip empty array */
5074 if (moff == mtrue_end)
5075 continue;
5076
5077 msize /= total_nelems;
5078 elem_idx = (off - moff) / msize;
5079 moff += elem_idx * msize;
5080 mtype = elem_type;
5081 mid = elem_id;
5082 }
5083
5084 /* the 'off' we're looking for is either equal to start
5085 * of this field or inside of this struct
5086 */
5087 if (btf_type_is_struct(mtype)) {
5088 /* our field must be inside that union or struct */
5089 t = mtype;
5090
5091 /* return if the offset matches the member offset */
5092 if (off == moff) {
5093 *next_btf_id = mid;
5094 return WALK_STRUCT;
5095 }
5096
5097 /* adjust offset we're looking for */
5098 off -= moff;
5099 goto again;
5100 }
5101
5102 if (btf_type_is_ptr(mtype)) {
5103 const struct btf_type *stype;
5104 u32 id;
5105
5106 if (msize != size || off != moff) {
5107 bpf_log(log,
5108 "cannot access ptr member %s with moff %u in struct %s with off %u size %u\n",
5109 mname, moff, tname, off, size);
5110 return -EACCES;
5111 }
5112 stype = btf_type_skip_modifiers(btf, mtype->type, &id);
5113 if (btf_type_is_struct(stype)) {
5114 *next_btf_id = id;
5115 return WALK_PTR;
5116 }
5117 }
5118
5119 /* Allow more flexible access within an int as long as
5120 * it is within mtrue_end.
5121 * Since mtrue_end could be the end of an array,
5122 * that also allows using an array of int as a scratch
5123 * space. e.g. skb->cb[].
5124 */
5125 if (off + size > mtrue_end) {
5126 bpf_log(log,
5127 "access beyond the end of member %s (mend:%u) in struct %s with off %u size %u\n",
5128 mname, mtrue_end, tname, off, size);
5129 return -EACCES;
5130 }
5131
5132 return WALK_SCALAR;
5133 }
5134 bpf_log(log, "struct %s doesn't have field at offset %d\n", tname, off);
5135 return -EINVAL;
5136 }
5137
btf_struct_access(struct bpf_verifier_log * log,const struct btf * btf,const struct btf_type * t,int off,int size,enum bpf_access_type atype __maybe_unused,u32 * next_btf_id)5138 int btf_struct_access(struct bpf_verifier_log *log, const struct btf *btf,
5139 const struct btf_type *t, int off, int size,
5140 enum bpf_access_type atype __maybe_unused,
5141 u32 *next_btf_id)
5142 {
5143 int err;
5144 u32 id;
5145
5146 do {
5147 err = btf_struct_walk(log, btf, t, off, size, &id);
5148
5149 switch (err) {
5150 case WALK_PTR:
5151 /* If we found the pointer or scalar on t+off,
5152 * we're done.
5153 */
5154 *next_btf_id = id;
5155 return PTR_TO_BTF_ID;
5156 case WALK_SCALAR:
5157 return SCALAR_VALUE;
5158 case WALK_STRUCT:
5159 /* We found nested struct, so continue the search
5160 * by diving in it. At this point the offset is
5161 * aligned with the new type, so set it to 0.
5162 */
5163 t = btf_type_by_id(btf, id);
5164 off = 0;
5165 break;
5166 default:
5167 /* It's either error or unknown return value..
5168 * scream and leave.
5169 */
5170 if (WARN_ONCE(err > 0, "unknown btf_struct_walk return value"))
5171 return -EINVAL;
5172 return err;
5173 }
5174 } while (t);
5175
5176 return -EINVAL;
5177 }
5178
5179 /* Check that two BTF types, each specified as an BTF object + id, are exactly
5180 * the same. Trivial ID check is not enough due to module BTFs, because we can
5181 * end up with two different module BTFs, but IDs point to the common type in
5182 * vmlinux BTF.
5183 */
btf_types_are_same(const struct btf * btf1,u32 id1,const struct btf * btf2,u32 id2)5184 static bool btf_types_are_same(const struct btf *btf1, u32 id1,
5185 const struct btf *btf2, u32 id2)
5186 {
5187 if (id1 != id2)
5188 return false;
5189 if (btf1 == btf2)
5190 return true;
5191 return btf_type_by_id(btf1, id1) == btf_type_by_id(btf2, id2);
5192 }
5193
btf_struct_ids_match(struct bpf_verifier_log * log,const struct btf * btf,u32 id,int off,const struct btf * need_btf,u32 need_type_id)5194 bool btf_struct_ids_match(struct bpf_verifier_log *log,
5195 const struct btf *btf, u32 id, int off,
5196 const struct btf *need_btf, u32 need_type_id)
5197 {
5198 const struct btf_type *type;
5199 int err;
5200
5201 /* Are we already done? */
5202 if (off == 0 && btf_types_are_same(btf, id, need_btf, need_type_id))
5203 return true;
5204
5205 again:
5206 type = btf_type_by_id(btf, id);
5207 if (!type)
5208 return false;
5209 err = btf_struct_walk(log, btf, type, off, 1, &id);
5210 if (err != WALK_STRUCT)
5211 return false;
5212
5213 /* We found nested struct object. If it matches
5214 * the requested ID, we're done. Otherwise let's
5215 * continue the search with offset 0 in the new
5216 * type.
5217 */
5218 if (!btf_types_are_same(btf, id, need_btf, need_type_id)) {
5219 off = 0;
5220 goto again;
5221 }
5222
5223 return true;
5224 }
5225
__get_type_size(struct btf * btf,u32 btf_id,const struct btf_type ** bad_type)5226 static int __get_type_size(struct btf *btf, u32 btf_id,
5227 const struct btf_type **bad_type)
5228 {
5229 const struct btf_type *t;
5230
5231 if (!btf_id)
5232 /* void */
5233 return 0;
5234 t = btf_type_by_id(btf, btf_id);
5235 while (t && btf_type_is_modifier(t))
5236 t = btf_type_by_id(btf, t->type);
5237 if (!t) {
5238 *bad_type = btf_type_by_id(btf, 0);
5239 return -EINVAL;
5240 }
5241 if (btf_type_is_ptr(t))
5242 /* kernel size of pointer. Not BPF's size of pointer*/
5243 return sizeof(void *);
5244 if (btf_type_is_int(t) || btf_type_is_enum(t))
5245 return t->size;
5246 *bad_type = t;
5247 return -EINVAL;
5248 }
5249
btf_distill_func_proto(struct bpf_verifier_log * log,struct btf * btf,const struct btf_type * func,const char * tname,struct btf_func_model * m)5250 int btf_distill_func_proto(struct bpf_verifier_log *log,
5251 struct btf *btf,
5252 const struct btf_type *func,
5253 const char *tname,
5254 struct btf_func_model *m)
5255 {
5256 const struct btf_param *args;
5257 const struct btf_type *t;
5258 u32 i, nargs;
5259 int ret;
5260
5261 if (!func) {
5262 /* BTF function prototype doesn't match the verifier types.
5263 * Fall back to MAX_BPF_FUNC_REG_ARGS u64 args.
5264 */
5265 for (i = 0; i < MAX_BPF_FUNC_REG_ARGS; i++)
5266 m->arg_size[i] = 8;
5267 m->ret_size = 8;
5268 m->nr_args = MAX_BPF_FUNC_REG_ARGS;
5269 return 0;
5270 }
5271 args = (const struct btf_param *)(func + 1);
5272 nargs = btf_type_vlen(func);
5273 if (nargs >= MAX_BPF_FUNC_ARGS) {
5274 bpf_log(log,
5275 "The function %s has %d arguments. Too many.\n",
5276 tname, nargs);
5277 return -EINVAL;
5278 }
5279 ret = __get_type_size(btf, func->type, &t);
5280 if (ret < 0) {
5281 bpf_log(log,
5282 "The function %s return type %s is unsupported.\n",
5283 tname, btf_kind_str[BTF_INFO_KIND(t->info)]);
5284 return -EINVAL;
5285 }
5286 m->ret_size = ret;
5287
5288 for (i = 0; i < nargs; i++) {
5289 if (i == nargs - 1 && args[i].type == 0) {
5290 bpf_log(log,
5291 "The function %s with variable args is unsupported.\n",
5292 tname);
5293 return -EINVAL;
5294 }
5295 ret = __get_type_size(btf, args[i].type, &t);
5296 if (ret < 0) {
5297 bpf_log(log,
5298 "The function %s arg%d type %s is unsupported.\n",
5299 tname, i, btf_kind_str[BTF_INFO_KIND(t->info)]);
5300 return -EINVAL;
5301 }
5302 if (ret == 0) {
5303 bpf_log(log,
5304 "The function %s has malformed void argument.\n",
5305 tname);
5306 return -EINVAL;
5307 }
5308 m->arg_size[i] = ret;
5309 }
5310 m->nr_args = nargs;
5311 return 0;
5312 }
5313
5314 /* Compare BTFs of two functions assuming only scalars and pointers to context.
5315 * t1 points to BTF_KIND_FUNC in btf1
5316 * t2 points to BTF_KIND_FUNC in btf2
5317 * Returns:
5318 * EINVAL - function prototype mismatch
5319 * EFAULT - verifier bug
5320 * 0 - 99% match. The last 1% is validated by the verifier.
5321 */
btf_check_func_type_match(struct bpf_verifier_log * log,struct btf * btf1,const struct btf_type * t1,struct btf * btf2,const struct btf_type * t2)5322 static int btf_check_func_type_match(struct bpf_verifier_log *log,
5323 struct btf *btf1, const struct btf_type *t1,
5324 struct btf *btf2, const struct btf_type *t2)
5325 {
5326 const struct btf_param *args1, *args2;
5327 const char *fn1, *fn2, *s1, *s2;
5328 u32 nargs1, nargs2, i;
5329
5330 fn1 = btf_name_by_offset(btf1, t1->name_off);
5331 fn2 = btf_name_by_offset(btf2, t2->name_off);
5332
5333 if (btf_func_linkage(t1) != BTF_FUNC_GLOBAL) {
5334 bpf_log(log, "%s() is not a global function\n", fn1);
5335 return -EINVAL;
5336 }
5337 if (btf_func_linkage(t2) != BTF_FUNC_GLOBAL) {
5338 bpf_log(log, "%s() is not a global function\n", fn2);
5339 return -EINVAL;
5340 }
5341
5342 t1 = btf_type_by_id(btf1, t1->type);
5343 if (!t1 || !btf_type_is_func_proto(t1))
5344 return -EFAULT;
5345 t2 = btf_type_by_id(btf2, t2->type);
5346 if (!t2 || !btf_type_is_func_proto(t2))
5347 return -EFAULT;
5348
5349 args1 = (const struct btf_param *)(t1 + 1);
5350 nargs1 = btf_type_vlen(t1);
5351 args2 = (const struct btf_param *)(t2 + 1);
5352 nargs2 = btf_type_vlen(t2);
5353
5354 if (nargs1 != nargs2) {
5355 bpf_log(log, "%s() has %d args while %s() has %d args\n",
5356 fn1, nargs1, fn2, nargs2);
5357 return -EINVAL;
5358 }
5359
5360 t1 = btf_type_skip_modifiers(btf1, t1->type, NULL);
5361 t2 = btf_type_skip_modifiers(btf2, t2->type, NULL);
5362 if (t1->info != t2->info) {
5363 bpf_log(log,
5364 "Return type %s of %s() doesn't match type %s of %s()\n",
5365 btf_type_str(t1), fn1,
5366 btf_type_str(t2), fn2);
5367 return -EINVAL;
5368 }
5369
5370 for (i = 0; i < nargs1; i++) {
5371 t1 = btf_type_skip_modifiers(btf1, args1[i].type, NULL);
5372 t2 = btf_type_skip_modifiers(btf2, args2[i].type, NULL);
5373
5374 if (t1->info != t2->info) {
5375 bpf_log(log, "arg%d in %s() is %s while %s() has %s\n",
5376 i, fn1, btf_type_str(t1),
5377 fn2, btf_type_str(t2));
5378 return -EINVAL;
5379 }
5380 if (btf_type_has_size(t1) && t1->size != t2->size) {
5381 bpf_log(log,
5382 "arg%d in %s() has size %d while %s() has %d\n",
5383 i, fn1, t1->size,
5384 fn2, t2->size);
5385 return -EINVAL;
5386 }
5387
5388 /* global functions are validated with scalars and pointers
5389 * to context only. And only global functions can be replaced.
5390 * Hence type check only those types.
5391 */
5392 if (btf_type_is_int(t1) || btf_type_is_enum(t1))
5393 continue;
5394 if (!btf_type_is_ptr(t1)) {
5395 bpf_log(log,
5396 "arg%d in %s() has unrecognized type\n",
5397 i, fn1);
5398 return -EINVAL;
5399 }
5400 t1 = btf_type_skip_modifiers(btf1, t1->type, NULL);
5401 t2 = btf_type_skip_modifiers(btf2, t2->type, NULL);
5402 if (!btf_type_is_struct(t1)) {
5403 bpf_log(log,
5404 "arg%d in %s() is not a pointer to context\n",
5405 i, fn1);
5406 return -EINVAL;
5407 }
5408 if (!btf_type_is_struct(t2)) {
5409 bpf_log(log,
5410 "arg%d in %s() is not a pointer to context\n",
5411 i, fn2);
5412 return -EINVAL;
5413 }
5414 /* This is an optional check to make program writing easier.
5415 * Compare names of structs and report an error to the user.
5416 * btf_prepare_func_args() already checked that t2 struct
5417 * is a context type. btf_prepare_func_args() will check
5418 * later that t1 struct is a context type as well.
5419 */
5420 s1 = btf_name_by_offset(btf1, t1->name_off);
5421 s2 = btf_name_by_offset(btf2, t2->name_off);
5422 if (strcmp(s1, s2)) {
5423 bpf_log(log,
5424 "arg%d %s(struct %s *) doesn't match %s(struct %s *)\n",
5425 i, fn1, s1, fn2, s2);
5426 return -EINVAL;
5427 }
5428 }
5429 return 0;
5430 }
5431
5432 /* Compare BTFs of given program with BTF of target program */
btf_check_type_match(struct bpf_verifier_log * log,const struct bpf_prog * prog,struct btf * btf2,const struct btf_type * t2)5433 int btf_check_type_match(struct bpf_verifier_log *log, const struct bpf_prog *prog,
5434 struct btf *btf2, const struct btf_type *t2)
5435 {
5436 struct btf *btf1 = prog->aux->btf;
5437 const struct btf_type *t1;
5438 u32 btf_id = 0;
5439
5440 if (!prog->aux->func_info) {
5441 bpf_log(log, "Program extension requires BTF\n");
5442 return -EINVAL;
5443 }
5444
5445 btf_id = prog->aux->func_info[0].type_id;
5446 if (!btf_id)
5447 return -EFAULT;
5448
5449 t1 = btf_type_by_id(btf1, btf_id);
5450 if (!t1 || !btf_type_is_func(t1))
5451 return -EFAULT;
5452
5453 return btf_check_func_type_match(log, btf1, t1, btf2, t2);
5454 }
5455
5456 static u32 *reg2btf_ids[__BPF_REG_TYPE_MAX] = {
5457 #ifdef CONFIG_NET
5458 [PTR_TO_SOCKET] = &btf_sock_ids[BTF_SOCK_TYPE_SOCK],
5459 [PTR_TO_SOCK_COMMON] = &btf_sock_ids[BTF_SOCK_TYPE_SOCK_COMMON],
5460 [PTR_TO_TCP_SOCK] = &btf_sock_ids[BTF_SOCK_TYPE_TCP],
5461 #endif
5462 };
5463
btf_check_func_arg_match(struct bpf_verifier_env * env,const struct btf * btf,u32 func_id,struct bpf_reg_state * regs,bool ptr_to_mem_ok)5464 static int btf_check_func_arg_match(struct bpf_verifier_env *env,
5465 const struct btf *btf, u32 func_id,
5466 struct bpf_reg_state *regs,
5467 bool ptr_to_mem_ok)
5468 {
5469 enum bpf_prog_type prog_type = env->prog->type == BPF_PROG_TYPE_EXT ?
5470 env->prog->aux->dst_prog->type : env->prog->type;
5471 struct bpf_verifier_log *log = &env->log;
5472 const char *func_name, *ref_tname;
5473 const struct btf_type *t, *ref_t;
5474 const struct btf_param *args;
5475 u32 i, nargs, ref_id;
5476
5477 t = btf_type_by_id(btf, func_id);
5478 if (!t || !btf_type_is_func(t)) {
5479 /* These checks were already done by the verifier while loading
5480 * struct bpf_func_info or in add_kfunc_call().
5481 */
5482 bpf_log(log, "BTF of func_id %u doesn't point to KIND_FUNC\n",
5483 func_id);
5484 return -EFAULT;
5485 }
5486 func_name = btf_name_by_offset(btf, t->name_off);
5487
5488 t = btf_type_by_id(btf, t->type);
5489 if (!t || !btf_type_is_func_proto(t)) {
5490 bpf_log(log, "Invalid BTF of func %s\n", func_name);
5491 return -EFAULT;
5492 }
5493 args = (const struct btf_param *)(t + 1);
5494 nargs = btf_type_vlen(t);
5495 if (nargs > MAX_BPF_FUNC_REG_ARGS) {
5496 bpf_log(log, "Function %s has %d > %d args\n", func_name, nargs,
5497 MAX_BPF_FUNC_REG_ARGS);
5498 return -EINVAL;
5499 }
5500
5501 /* check that BTF function arguments match actual types that the
5502 * verifier sees.
5503 */
5504 for (i = 0; i < nargs; i++) {
5505 u32 regno = i + 1;
5506 struct bpf_reg_state *reg = ®s[regno];
5507
5508 t = btf_type_skip_modifiers(btf, args[i].type, NULL);
5509 if (btf_type_is_scalar(t)) {
5510 if (reg->type == SCALAR_VALUE)
5511 continue;
5512 bpf_log(log, "R%d is not a scalar\n", regno);
5513 return -EINVAL;
5514 }
5515
5516 if (!btf_type_is_ptr(t)) {
5517 bpf_log(log, "Unrecognized arg#%d type %s\n",
5518 i, btf_type_str(t));
5519 return -EINVAL;
5520 }
5521
5522 ref_t = btf_type_skip_modifiers(btf, t->type, &ref_id);
5523 ref_tname = btf_name_by_offset(btf, ref_t->name_off);
5524 if (btf_is_kernel(btf)) {
5525 const struct btf_type *reg_ref_t;
5526 const struct btf *reg_btf;
5527 const char *reg_ref_tname;
5528 u32 reg_ref_id;
5529
5530 if (!btf_type_is_struct(ref_t)) {
5531 bpf_log(log, "kernel function %s args#%d pointer type %s %s is not supported\n",
5532 func_name, i, btf_type_str(ref_t),
5533 ref_tname);
5534 return -EINVAL;
5535 }
5536
5537 if (reg->type == PTR_TO_BTF_ID) {
5538 reg_btf = reg->btf;
5539 reg_ref_id = reg->btf_id;
5540 } else if (reg2btf_ids[base_type(reg->type)]) {
5541 reg_btf = btf_vmlinux;
5542 reg_ref_id = *reg2btf_ids[base_type(reg->type)];
5543 } else {
5544 bpf_log(log, "kernel function %s args#%d expected pointer to %s %s but R%d is not a pointer to btf_id\n",
5545 func_name, i,
5546 btf_type_str(ref_t), ref_tname, regno);
5547 return -EINVAL;
5548 }
5549
5550 reg_ref_t = btf_type_skip_modifiers(reg_btf, reg_ref_id,
5551 ®_ref_id);
5552 reg_ref_tname = btf_name_by_offset(reg_btf,
5553 reg_ref_t->name_off);
5554 if (!btf_struct_ids_match(log, reg_btf, reg_ref_id,
5555 reg->off, btf, ref_id)) {
5556 bpf_log(log, "kernel function %s args#%d expected pointer to %s %s but R%d has a pointer to %s %s\n",
5557 func_name, i,
5558 btf_type_str(ref_t), ref_tname,
5559 regno, btf_type_str(reg_ref_t),
5560 reg_ref_tname);
5561 return -EINVAL;
5562 }
5563 } else if (btf_get_prog_ctx_type(log, btf, t, prog_type, i)) {
5564 /* If function expects ctx type in BTF check that caller
5565 * is passing PTR_TO_CTX.
5566 */
5567 if (reg->type != PTR_TO_CTX) {
5568 bpf_log(log,
5569 "arg#%d expected pointer to ctx, but got %s\n",
5570 i, btf_type_str(t));
5571 return -EINVAL;
5572 }
5573 if (check_ctx_reg(env, reg, regno))
5574 return -EINVAL;
5575 } else if (ptr_to_mem_ok) {
5576 const struct btf_type *resolve_ret;
5577 u32 type_size;
5578
5579 resolve_ret = btf_resolve_size(btf, ref_t, &type_size);
5580 if (IS_ERR(resolve_ret)) {
5581 bpf_log(log,
5582 "arg#%d reference type('%s %s') size cannot be determined: %ld\n",
5583 i, btf_type_str(ref_t), ref_tname,
5584 PTR_ERR(resolve_ret));
5585 return -EINVAL;
5586 }
5587
5588 if (check_mem_reg(env, reg, regno, type_size))
5589 return -EINVAL;
5590 } else {
5591 return -EINVAL;
5592 }
5593 }
5594
5595 return 0;
5596 }
5597
5598 /* Compare BTF of a function with given bpf_reg_state.
5599 * Returns:
5600 * EFAULT - there is a verifier bug. Abort verification.
5601 * EINVAL - there is a type mismatch or BTF is not available.
5602 * 0 - BTF matches with what bpf_reg_state expects.
5603 * Only PTR_TO_CTX and SCALAR_VALUE states are recognized.
5604 */
btf_check_subprog_arg_match(struct bpf_verifier_env * env,int subprog,struct bpf_reg_state * regs)5605 int btf_check_subprog_arg_match(struct bpf_verifier_env *env, int subprog,
5606 struct bpf_reg_state *regs)
5607 {
5608 struct bpf_prog *prog = env->prog;
5609 struct btf *btf = prog->aux->btf;
5610 bool is_global;
5611 u32 btf_id;
5612 int err;
5613
5614 if (!prog->aux->func_info)
5615 return -EINVAL;
5616
5617 btf_id = prog->aux->func_info[subprog].type_id;
5618 if (!btf_id)
5619 return -EFAULT;
5620
5621 if (prog->aux->func_info_aux[subprog].unreliable)
5622 return -EINVAL;
5623
5624 is_global = prog->aux->func_info_aux[subprog].linkage == BTF_FUNC_GLOBAL;
5625 err = btf_check_func_arg_match(env, btf, btf_id, regs, is_global);
5626
5627 /* Compiler optimizations can remove arguments from static functions
5628 * or mismatched type can be passed into a global function.
5629 * In such cases mark the function as unreliable from BTF point of view.
5630 */
5631 if (err)
5632 prog->aux->func_info_aux[subprog].unreliable = true;
5633 return err;
5634 }
5635
btf_check_kfunc_arg_match(struct bpf_verifier_env * env,const struct btf * btf,u32 func_id,struct bpf_reg_state * regs)5636 int btf_check_kfunc_arg_match(struct bpf_verifier_env *env,
5637 const struct btf *btf, u32 func_id,
5638 struct bpf_reg_state *regs)
5639 {
5640 return btf_check_func_arg_match(env, btf, func_id, regs, false);
5641 }
5642
5643 /* Convert BTF of a function into bpf_reg_state if possible
5644 * Returns:
5645 * EFAULT - there is a verifier bug. Abort verification.
5646 * EINVAL - cannot convert BTF.
5647 * 0 - Successfully converted BTF into bpf_reg_state
5648 * (either PTR_TO_CTX or SCALAR_VALUE).
5649 */
btf_prepare_func_args(struct bpf_verifier_env * env,int subprog,struct bpf_reg_state * regs)5650 int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog,
5651 struct bpf_reg_state *regs)
5652 {
5653 struct bpf_verifier_log *log = &env->log;
5654 struct bpf_prog *prog = env->prog;
5655 enum bpf_prog_type prog_type = prog->type;
5656 struct btf *btf = prog->aux->btf;
5657 const struct btf_param *args;
5658 const struct btf_type *t, *ref_t;
5659 u32 i, nargs, btf_id;
5660 const char *tname;
5661
5662 if (!prog->aux->func_info ||
5663 prog->aux->func_info_aux[subprog].linkage != BTF_FUNC_GLOBAL) {
5664 bpf_log(log, "Verifier bug\n");
5665 return -EFAULT;
5666 }
5667
5668 btf_id = prog->aux->func_info[subprog].type_id;
5669 if (!btf_id) {
5670 bpf_log(log, "Global functions need valid BTF\n");
5671 return -EFAULT;
5672 }
5673
5674 t = btf_type_by_id(btf, btf_id);
5675 if (!t || !btf_type_is_func(t)) {
5676 /* These checks were already done by the verifier while loading
5677 * struct bpf_func_info
5678 */
5679 bpf_log(log, "BTF of func#%d doesn't point to KIND_FUNC\n",
5680 subprog);
5681 return -EFAULT;
5682 }
5683 tname = btf_name_by_offset(btf, t->name_off);
5684
5685 if (log->level & BPF_LOG_LEVEL)
5686 bpf_log(log, "Validating %s() func#%d...\n",
5687 tname, subprog);
5688
5689 if (prog->aux->func_info_aux[subprog].unreliable) {
5690 bpf_log(log, "Verifier bug in function %s()\n", tname);
5691 return -EFAULT;
5692 }
5693 if (prog_type == BPF_PROG_TYPE_EXT)
5694 prog_type = prog->aux->dst_prog->type;
5695
5696 t = btf_type_by_id(btf, t->type);
5697 if (!t || !btf_type_is_func_proto(t)) {
5698 bpf_log(log, "Invalid type of function %s()\n", tname);
5699 return -EFAULT;
5700 }
5701 args = (const struct btf_param *)(t + 1);
5702 nargs = btf_type_vlen(t);
5703 if (nargs > MAX_BPF_FUNC_REG_ARGS) {
5704 bpf_log(log, "Global function %s() with %d > %d args. Buggy compiler.\n",
5705 tname, nargs, MAX_BPF_FUNC_REG_ARGS);
5706 return -EINVAL;
5707 }
5708 /* check that function returns int */
5709 t = btf_type_by_id(btf, t->type);
5710 while (btf_type_is_modifier(t))
5711 t = btf_type_by_id(btf, t->type);
5712 if (!btf_type_is_int(t) && !btf_type_is_enum(t)) {
5713 bpf_log(log,
5714 "Global function %s() doesn't return scalar. Only those are supported.\n",
5715 tname);
5716 return -EINVAL;
5717 }
5718 /* Convert BTF function arguments into verifier types.
5719 * Only PTR_TO_CTX and SCALAR are supported atm.
5720 */
5721 for (i = 0; i < nargs; i++) {
5722 struct bpf_reg_state *reg = ®s[i + 1];
5723
5724 t = btf_type_by_id(btf, args[i].type);
5725 while (btf_type_is_modifier(t))
5726 t = btf_type_by_id(btf, t->type);
5727 if (btf_type_is_int(t) || btf_type_is_enum(t)) {
5728 reg->type = SCALAR_VALUE;
5729 continue;
5730 }
5731 if (btf_type_is_ptr(t)) {
5732 if (btf_get_prog_ctx_type(log, btf, t, prog_type, i)) {
5733 reg->type = PTR_TO_CTX;
5734 continue;
5735 }
5736
5737 t = btf_type_skip_modifiers(btf, t->type, NULL);
5738
5739 ref_t = btf_resolve_size(btf, t, ®->mem_size);
5740 if (IS_ERR(ref_t)) {
5741 bpf_log(log,
5742 "arg#%d reference type('%s %s') size cannot be determined: %ld\n",
5743 i, btf_type_str(t), btf_name_by_offset(btf, t->name_off),
5744 PTR_ERR(ref_t));
5745 return -EINVAL;
5746 }
5747
5748 reg->type = PTR_TO_MEM | PTR_MAYBE_NULL;
5749 reg->id = ++env->id_gen;
5750
5751 continue;
5752 }
5753 bpf_log(log, "Arg#%d type %s in %s() is not supported yet.\n",
5754 i, btf_kind_str[BTF_INFO_KIND(t->info)], tname);
5755 return -EINVAL;
5756 }
5757 return 0;
5758 }
5759
btf_type_show(const struct btf * btf,u32 type_id,void * obj,struct btf_show * show)5760 static void btf_type_show(const struct btf *btf, u32 type_id, void *obj,
5761 struct btf_show *show)
5762 {
5763 const struct btf_type *t = btf_type_by_id(btf, type_id);
5764
5765 show->btf = btf;
5766 memset(&show->state, 0, sizeof(show->state));
5767 memset(&show->obj, 0, sizeof(show->obj));
5768
5769 btf_type_ops(t)->show(btf, t, type_id, obj, 0, show);
5770 }
5771
btf_seq_show(struct btf_show * show,const char * fmt,va_list args)5772 static void btf_seq_show(struct btf_show *show, const char *fmt,
5773 va_list args)
5774 {
5775 seq_vprintf((struct seq_file *)show->target, fmt, args);
5776 }
5777
btf_type_seq_show_flags(const struct btf * btf,u32 type_id,void * obj,struct seq_file * m,u64 flags)5778 int btf_type_seq_show_flags(const struct btf *btf, u32 type_id,
5779 void *obj, struct seq_file *m, u64 flags)
5780 {
5781 struct btf_show sseq;
5782
5783 sseq.target = m;
5784 sseq.showfn = btf_seq_show;
5785 sseq.flags = flags;
5786
5787 btf_type_show(btf, type_id, obj, &sseq);
5788
5789 return sseq.state.status;
5790 }
5791
btf_type_seq_show(const struct btf * btf,u32 type_id,void * obj,struct seq_file * m)5792 void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj,
5793 struct seq_file *m)
5794 {
5795 (void) btf_type_seq_show_flags(btf, type_id, obj, m,
5796 BTF_SHOW_NONAME | BTF_SHOW_COMPACT |
5797 BTF_SHOW_ZERO | BTF_SHOW_UNSAFE);
5798 }
5799
5800 struct btf_show_snprintf {
5801 struct btf_show show;
5802 int len_left; /* space left in string */
5803 int len; /* length we would have written */
5804 };
5805
btf_snprintf_show(struct btf_show * show,const char * fmt,va_list args)5806 static void btf_snprintf_show(struct btf_show *show, const char *fmt,
5807 va_list args)
5808 {
5809 struct btf_show_snprintf *ssnprintf = (struct btf_show_snprintf *)show;
5810 int len;
5811
5812 len = vsnprintf(show->target, ssnprintf->len_left, fmt, args);
5813
5814 if (len < 0) {
5815 ssnprintf->len_left = 0;
5816 ssnprintf->len = len;
5817 } else if (len > ssnprintf->len_left) {
5818 /* no space, drive on to get length we would have written */
5819 ssnprintf->len_left = 0;
5820 ssnprintf->len += len;
5821 } else {
5822 ssnprintf->len_left -= len;
5823 ssnprintf->len += len;
5824 show->target += len;
5825 }
5826 }
5827
btf_type_snprintf_show(const struct btf * btf,u32 type_id,void * obj,char * buf,int len,u64 flags)5828 int btf_type_snprintf_show(const struct btf *btf, u32 type_id, void *obj,
5829 char *buf, int len, u64 flags)
5830 {
5831 struct btf_show_snprintf ssnprintf;
5832
5833 ssnprintf.show.target = buf;
5834 ssnprintf.show.flags = flags;
5835 ssnprintf.show.showfn = btf_snprintf_show;
5836 ssnprintf.len_left = len;
5837 ssnprintf.len = 0;
5838
5839 btf_type_show(btf, type_id, obj, (struct btf_show *)&ssnprintf);
5840
5841 /* If we encontered an error, return it. */
5842 if (ssnprintf.show.state.status)
5843 return ssnprintf.show.state.status;
5844
5845 /* Otherwise return length we would have written */
5846 return ssnprintf.len;
5847 }
5848
5849 #ifdef CONFIG_PROC_FS
bpf_btf_show_fdinfo(struct seq_file * m,struct file * filp)5850 static void bpf_btf_show_fdinfo(struct seq_file *m, struct file *filp)
5851 {
5852 const struct btf *btf = filp->private_data;
5853
5854 seq_printf(m, "btf_id:\t%u\n", btf->id);
5855 }
5856 #endif
5857
btf_release(struct inode * inode,struct file * filp)5858 static int btf_release(struct inode *inode, struct file *filp)
5859 {
5860 btf_put(filp->private_data);
5861 return 0;
5862 }
5863
5864 const struct file_operations btf_fops = {
5865 #ifdef CONFIG_PROC_FS
5866 .show_fdinfo = bpf_btf_show_fdinfo,
5867 #endif
5868 .release = btf_release,
5869 };
5870
__btf_new_fd(struct btf * btf)5871 static int __btf_new_fd(struct btf *btf)
5872 {
5873 return anon_inode_getfd("btf", &btf_fops, btf, O_RDONLY | O_CLOEXEC);
5874 }
5875
btf_new_fd(const union bpf_attr * attr,bpfptr_t uattr)5876 int btf_new_fd(const union bpf_attr *attr, bpfptr_t uattr)
5877 {
5878 struct btf *btf;
5879 int ret;
5880
5881 btf = btf_parse(make_bpfptr(attr->btf, uattr.is_kernel),
5882 attr->btf_size, attr->btf_log_level,
5883 u64_to_user_ptr(attr->btf_log_buf),
5884 attr->btf_log_size);
5885 if (IS_ERR(btf))
5886 return PTR_ERR(btf);
5887
5888 ret = btf_alloc_id(btf);
5889 if (ret) {
5890 btf_free(btf);
5891 return ret;
5892 }
5893
5894 /*
5895 * The BTF ID is published to the userspace.
5896 * All BTF free must go through call_rcu() from
5897 * now on (i.e. free by calling btf_put()).
5898 */
5899
5900 ret = __btf_new_fd(btf);
5901 if (ret < 0)
5902 btf_put(btf);
5903
5904 return ret;
5905 }
5906
btf_get_by_fd(int fd)5907 struct btf *btf_get_by_fd(int fd)
5908 {
5909 struct btf *btf;
5910 struct fd f;
5911
5912 f = fdget(fd);
5913
5914 if (!f.file)
5915 return ERR_PTR(-EBADF);
5916
5917 if (f.file->f_op != &btf_fops) {
5918 fdput(f);
5919 return ERR_PTR(-EINVAL);
5920 }
5921
5922 btf = f.file->private_data;
5923 refcount_inc(&btf->refcnt);
5924 fdput(f);
5925
5926 return btf;
5927 }
5928
btf_get_info_by_fd(const struct btf * btf,const union bpf_attr * attr,union bpf_attr __user * uattr)5929 int btf_get_info_by_fd(const struct btf *btf,
5930 const union bpf_attr *attr,
5931 union bpf_attr __user *uattr)
5932 {
5933 struct bpf_btf_info __user *uinfo;
5934 struct bpf_btf_info info;
5935 u32 info_copy, btf_copy;
5936 void __user *ubtf;
5937 char __user *uname;
5938 u32 uinfo_len, uname_len, name_len;
5939 int ret = 0;
5940
5941 uinfo = u64_to_user_ptr(attr->info.info);
5942 uinfo_len = attr->info.info_len;
5943
5944 info_copy = min_t(u32, uinfo_len, sizeof(info));
5945 memset(&info, 0, sizeof(info));
5946 if (copy_from_user(&info, uinfo, info_copy))
5947 return -EFAULT;
5948
5949 info.id = btf->id;
5950 ubtf = u64_to_user_ptr(info.btf);
5951 btf_copy = min_t(u32, btf->data_size, info.btf_size);
5952 if (copy_to_user(ubtf, btf->data, btf_copy))
5953 return -EFAULT;
5954 info.btf_size = btf->data_size;
5955
5956 info.kernel_btf = btf->kernel_btf;
5957
5958 uname = u64_to_user_ptr(info.name);
5959 uname_len = info.name_len;
5960 if (!uname ^ !uname_len)
5961 return -EINVAL;
5962
5963 name_len = strlen(btf->name);
5964 info.name_len = name_len;
5965
5966 if (uname) {
5967 if (uname_len >= name_len + 1) {
5968 if (copy_to_user(uname, btf->name, name_len + 1))
5969 return -EFAULT;
5970 } else {
5971 char zero = '\0';
5972
5973 if (copy_to_user(uname, btf->name, uname_len - 1))
5974 return -EFAULT;
5975 if (put_user(zero, uname + uname_len - 1))
5976 return -EFAULT;
5977 /* let user-space know about too short buffer */
5978 ret = -ENOSPC;
5979 }
5980 }
5981
5982 if (copy_to_user(uinfo, &info, info_copy) ||
5983 put_user(info_copy, &uattr->info.info_len))
5984 return -EFAULT;
5985
5986 return ret;
5987 }
5988
btf_get_fd_by_id(u32 id)5989 int btf_get_fd_by_id(u32 id)
5990 {
5991 struct btf *btf;
5992 int fd;
5993
5994 rcu_read_lock();
5995 btf = idr_find(&btf_idr, id);
5996 if (!btf || !refcount_inc_not_zero(&btf->refcnt))
5997 btf = ERR_PTR(-ENOENT);
5998 rcu_read_unlock();
5999
6000 if (IS_ERR(btf))
6001 return PTR_ERR(btf);
6002
6003 fd = __btf_new_fd(btf);
6004 if (fd < 0)
6005 btf_put(btf);
6006
6007 return fd;
6008 }
6009
btf_obj_id(const struct btf * btf)6010 u32 btf_obj_id(const struct btf *btf)
6011 {
6012 return btf->id;
6013 }
6014
btf_is_kernel(const struct btf * btf)6015 bool btf_is_kernel(const struct btf *btf)
6016 {
6017 return btf->kernel_btf;
6018 }
6019
btf_is_module(const struct btf * btf)6020 bool btf_is_module(const struct btf *btf)
6021 {
6022 return btf->kernel_btf && strcmp(btf->name, "vmlinux") != 0;
6023 }
6024
btf_id_cmp_func(const void * a,const void * b)6025 static int btf_id_cmp_func(const void *a, const void *b)
6026 {
6027 const int *pa = a, *pb = b;
6028
6029 return *pa - *pb;
6030 }
6031
btf_id_set_contains(const struct btf_id_set * set,u32 id)6032 bool btf_id_set_contains(const struct btf_id_set *set, u32 id)
6033 {
6034 return bsearch(&id, set->ids, set->cnt, sizeof(u32), btf_id_cmp_func) != NULL;
6035 }
6036
6037 enum {
6038 BTF_MODULE_F_LIVE = (1 << 0),
6039 };
6040
6041 #ifdef CONFIG_DEBUG_INFO_BTF_MODULES
6042 struct btf_module {
6043 struct list_head list;
6044 struct module *module;
6045 struct btf *btf;
6046 struct bin_attribute *sysfs_attr;
6047 int flags;
6048 };
6049
6050 static LIST_HEAD(btf_modules);
6051 static DEFINE_MUTEX(btf_module_mutex);
6052
6053 static ssize_t
btf_module_read(struct file * file,struct kobject * kobj,struct bin_attribute * bin_attr,char * buf,loff_t off,size_t len)6054 btf_module_read(struct file *file, struct kobject *kobj,
6055 struct bin_attribute *bin_attr,
6056 char *buf, loff_t off, size_t len)
6057 {
6058 const struct btf *btf = bin_attr->private;
6059
6060 memcpy(buf, btf->data + off, len);
6061 return len;
6062 }
6063
btf_module_notify(struct notifier_block * nb,unsigned long op,void * module)6064 static int btf_module_notify(struct notifier_block *nb, unsigned long op,
6065 void *module)
6066 {
6067 struct btf_module *btf_mod, *tmp;
6068 struct module *mod = module;
6069 struct btf *btf;
6070 int err = 0;
6071
6072 if (mod->btf_data_size == 0 ||
6073 (op != MODULE_STATE_COMING && op != MODULE_STATE_LIVE &&
6074 op != MODULE_STATE_GOING))
6075 goto out;
6076
6077 switch (op) {
6078 case MODULE_STATE_COMING:
6079 btf_mod = kzalloc(sizeof(*btf_mod), GFP_KERNEL);
6080 if (!btf_mod) {
6081 err = -ENOMEM;
6082 goto out;
6083 }
6084 btf = btf_parse_module(mod->name, mod->btf_data, mod->btf_data_size);
6085 if (IS_ERR(btf)) {
6086 kfree(btf_mod);
6087 if (!IS_ENABLED(CONFIG_MODULE_ALLOW_BTF_MISMATCH)) {
6088 pr_warn("failed to validate module [%s] BTF: %ld\n",
6089 mod->name, PTR_ERR(btf));
6090 err = PTR_ERR(btf);
6091 } else {
6092 pr_warn_once("Kernel module BTF mismatch detected, BTF debug info may be unavailable for some modules\n");
6093 }
6094 goto out;
6095 }
6096 err = btf_alloc_id(btf);
6097 if (err) {
6098 btf_free(btf);
6099 kfree(btf_mod);
6100 goto out;
6101 }
6102
6103 mutex_lock(&btf_module_mutex);
6104 btf_mod->module = module;
6105 btf_mod->btf = btf;
6106 list_add(&btf_mod->list, &btf_modules);
6107 mutex_unlock(&btf_module_mutex);
6108
6109 if (IS_ENABLED(CONFIG_SYSFS)) {
6110 struct bin_attribute *attr;
6111
6112 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
6113 if (!attr)
6114 goto out;
6115
6116 sysfs_bin_attr_init(attr);
6117 attr->attr.name = btf->name;
6118 attr->attr.mode = 0444;
6119 attr->size = btf->data_size;
6120 attr->private = btf;
6121 attr->read = btf_module_read;
6122
6123 err = sysfs_create_bin_file(btf_kobj, attr);
6124 if (err) {
6125 pr_warn("failed to register module [%s] BTF in sysfs: %d\n",
6126 mod->name, err);
6127 kfree(attr);
6128 err = 0;
6129 goto out;
6130 }
6131
6132 btf_mod->sysfs_attr = attr;
6133 }
6134
6135 break;
6136 case MODULE_STATE_LIVE:
6137 mutex_lock(&btf_module_mutex);
6138 list_for_each_entry_safe(btf_mod, tmp, &btf_modules, list) {
6139 if (btf_mod->module != module)
6140 continue;
6141
6142 btf_mod->flags |= BTF_MODULE_F_LIVE;
6143 break;
6144 }
6145 mutex_unlock(&btf_module_mutex);
6146 break;
6147 case MODULE_STATE_GOING:
6148 mutex_lock(&btf_module_mutex);
6149 list_for_each_entry_safe(btf_mod, tmp, &btf_modules, list) {
6150 if (btf_mod->module != module)
6151 continue;
6152
6153 list_del(&btf_mod->list);
6154 if (btf_mod->sysfs_attr)
6155 sysfs_remove_bin_file(btf_kobj, btf_mod->sysfs_attr);
6156 btf_put(btf_mod->btf);
6157 kfree(btf_mod->sysfs_attr);
6158 kfree(btf_mod);
6159 break;
6160 }
6161 mutex_unlock(&btf_module_mutex);
6162 break;
6163 }
6164 out:
6165 return notifier_from_errno(err);
6166 }
6167
6168 static struct notifier_block btf_module_nb = {
6169 .notifier_call = btf_module_notify,
6170 };
6171
btf_module_init(void)6172 static int __init btf_module_init(void)
6173 {
6174 register_module_notifier(&btf_module_nb);
6175 return 0;
6176 }
6177
6178 fs_initcall(btf_module_init);
6179 #endif /* CONFIG_DEBUG_INFO_BTF_MODULES */
6180
btf_try_get_module(const struct btf * btf)6181 struct module *btf_try_get_module(const struct btf *btf)
6182 {
6183 struct module *res = NULL;
6184 #ifdef CONFIG_DEBUG_INFO_BTF_MODULES
6185 struct btf_module *btf_mod, *tmp;
6186
6187 mutex_lock(&btf_module_mutex);
6188 list_for_each_entry_safe(btf_mod, tmp, &btf_modules, list) {
6189 if (btf_mod->btf != btf)
6190 continue;
6191
6192 /* We must only consider module whose __init routine has
6193 * finished, hence we must check for BTF_MODULE_F_LIVE flag,
6194 * which is set from the notifier callback for
6195 * MODULE_STATE_LIVE.
6196 */
6197 if ((btf_mod->flags & BTF_MODULE_F_LIVE) && try_module_get(btf_mod->module))
6198 res = btf_mod->module;
6199
6200 break;
6201 }
6202 mutex_unlock(&btf_module_mutex);
6203 #endif
6204
6205 return res;
6206 }
6207
BPF_CALL_4(bpf_btf_find_by_name_kind,char *,name,int,name_sz,u32,kind,int,flags)6208 BPF_CALL_4(bpf_btf_find_by_name_kind, char *, name, int, name_sz, u32, kind, int, flags)
6209 {
6210 struct btf *btf;
6211 long ret;
6212
6213 if (flags)
6214 return -EINVAL;
6215
6216 if (name_sz <= 1 || name[name_sz - 1])
6217 return -EINVAL;
6218
6219 btf = bpf_get_btf_vmlinux();
6220 if (IS_ERR(btf))
6221 return PTR_ERR(btf);
6222
6223 ret = btf_find_by_name_kind(btf, name, kind);
6224 /* ret is never zero, since btf_find_by_name_kind returns
6225 * positive btf_id or negative error.
6226 */
6227 if (ret < 0) {
6228 struct btf *mod_btf;
6229 int id;
6230
6231 /* If name is not found in vmlinux's BTF then search in module's BTFs */
6232 spin_lock_bh(&btf_idr_lock);
6233 idr_for_each_entry(&btf_idr, mod_btf, id) {
6234 if (!btf_is_module(mod_btf))
6235 continue;
6236 /* linear search could be slow hence unlock/lock
6237 * the IDR to avoiding holding it for too long
6238 */
6239 btf_get(mod_btf);
6240 spin_unlock_bh(&btf_idr_lock);
6241 ret = btf_find_by_name_kind(mod_btf, name, kind);
6242 if (ret > 0) {
6243 int btf_obj_fd;
6244
6245 btf_obj_fd = __btf_new_fd(mod_btf);
6246 if (btf_obj_fd < 0) {
6247 btf_put(mod_btf);
6248 return btf_obj_fd;
6249 }
6250 return ret | (((u64)btf_obj_fd) << 32);
6251 }
6252 spin_lock_bh(&btf_idr_lock);
6253 btf_put(mod_btf);
6254 }
6255 spin_unlock_bh(&btf_idr_lock);
6256 }
6257 return ret;
6258 }
6259
6260 const struct bpf_func_proto bpf_btf_find_by_name_kind_proto = {
6261 .func = bpf_btf_find_by_name_kind,
6262 .gpl_only = false,
6263 .ret_type = RET_INTEGER,
6264 .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6265 .arg2_type = ARG_CONST_SIZE,
6266 .arg3_type = ARG_ANYTHING,
6267 .arg4_type = ARG_ANYTHING,
6268 };
6269
6270 BTF_ID_LIST_GLOBAL_SINGLE(btf_task_struct_ids, struct, task_struct)
6271