blob: 72caa799e82fe4543a46a85bacfa3eef7b654b34 [file] [log] [blame]
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001/* SPDX-License-Identifier: GPL-2.0 */
2/* Copyright (c) 2018 Facebook */
3
4#include <uapi/linux/btf.h>
5#include <uapi/linux/types.h>
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07006#include <linux/seq_file.h>
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07007#include <linux/compiler.h>
Martin KaFai Lau2667a262018-11-19 15:29:08 -08008#include <linux/ctype.h>
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07009#include <linux/errno.h>
10#include <linux/slab.h>
Martin KaFai Lauf56a6532018-04-18 15:56:01 -070011#include <linux/anon_inodes.h>
12#include <linux/file.h>
Martin KaFai Lau69b693f2018-04-18 15:55:57 -070013#include <linux/uaccess.h>
14#include <linux/kernel.h>
Martin KaFai Lau78958fc2018-05-04 14:49:51 -070015#include <linux/idr.h>
Martin KaFai Lauf80442a2018-05-22 14:57:18 -070016#include <linux/sort.h>
Martin KaFai Lau69b693f2018-04-18 15:55:57 -070017#include <linux/bpf_verifier.h>
18#include <linux/btf.h>
19
20/* BTF (BPF Type Format) is the meta data format which describes
21 * the data types of BPF program/map. Hence, it basically focus
22 * on the C programming language which the modern BPF is primary
23 * using.
24 *
25 * ELF Section:
26 * ~~~~~~~~~~~
27 * The BTF data is stored under the ".BTF" ELF section
28 *
29 * struct btf_type:
30 * ~~~~~~~~~~~~~~~
31 * Each 'struct btf_type' object describes a C data type.
32 * Depending on the type it is describing, a 'struct btf_type'
33 * object may be followed by more data. F.e.
34 * To describe an array, 'struct btf_type' is followed by
35 * 'struct btf_array'.
36 *
37 * 'struct btf_type' and any extra data following it are
38 * 4 bytes aligned.
39 *
40 * Type section:
41 * ~~~~~~~~~~~~~
42 * The BTF type section contains a list of 'struct btf_type' objects.
43 * Each one describes a C type. Recall from the above section
44 * that a 'struct btf_type' object could be immediately followed by extra
45 * data in order to desribe some particular C types.
46 *
47 * type_id:
48 * ~~~~~~~
49 * Each btf_type object is identified by a type_id. The type_id
50 * is implicitly implied by the location of the btf_type object in
51 * the BTF type section. The first one has type_id 1. The second
52 * one has type_id 2...etc. Hence, an earlier btf_type has
53 * a smaller type_id.
54 *
55 * A btf_type object may refer to another btf_type object by using
56 * type_id (i.e. the "type" in the "struct btf_type").
57 *
58 * NOTE that we cannot assume any reference-order.
59 * A btf_type object can refer to an earlier btf_type object
60 * but it can also refer to a later btf_type object.
61 *
62 * For example, to describe "const void *". A btf_type
63 * object describing "const" may refer to another btf_type
64 * object describing "void *". This type-reference is done
65 * by specifying type_id:
66 *
67 * [1] CONST (anon) type_id=2
68 * [2] PTR (anon) type_id=0
69 *
70 * The above is the btf_verifier debug log:
71 * - Each line started with "[?]" is a btf_type object
72 * - [?] is the type_id of the btf_type object.
73 * - CONST/PTR is the BTF_KIND_XXX
74 * - "(anon)" is the name of the type. It just
75 * happens that CONST and PTR has no name.
76 * - type_id=XXX is the 'u32 type' in btf_type
77 *
78 * NOTE: "void" has type_id 0
79 *
80 * String section:
81 * ~~~~~~~~~~~~~~
82 * The BTF string section contains the names used by the type section.
83 * Each string is referred by an "offset" from the beginning of the
84 * string section.
85 *
86 * Each string is '\0' terminated.
87 *
88 * The first character in the string section must be '\0'
89 * which is used to mean 'anonymous'. Some btf_type may not
90 * have a name.
91 */
92
93/* BTF verification:
94 *
95 * To verify BTF data, two passes are needed.
96 *
97 * Pass #1
98 * ~~~~~~~
99 * The first pass is to collect all btf_type objects to
100 * an array: "btf->types".
101 *
102 * Depending on the C type that a btf_type is describing,
103 * a btf_type may be followed by extra data. We don't know
104 * how many btf_type is there, and more importantly we don't
105 * know where each btf_type is located in the type section.
106 *
107 * Without knowing the location of each type_id, most verifications
108 * cannot be done. e.g. an earlier btf_type may refer to a later
109 * btf_type (recall the "const void *" above), so we cannot
110 * check this type-reference in the first pass.
111 *
112 * In the first pass, it still does some verifications (e.g.
113 * checking the name is a valid offset to the string section).
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700114 *
115 * Pass #2
116 * ~~~~~~~
117 * The main focus is to resolve a btf_type that is referring
118 * to another type.
119 *
120 * We have to ensure the referring type:
121 * 1) does exist in the BTF (i.e. in btf->types[])
122 * 2) does not cause a loop:
123 * struct A {
124 * struct B b;
125 * };
126 *
127 * struct B {
128 * struct A a;
129 * };
130 *
131 * btf_type_needs_resolve() decides if a btf_type needs
132 * to be resolved.
133 *
134 * The needs_resolve type implements the "resolve()" ops which
135 * essentially does a DFS and detects backedge.
136 *
137 * During resolve (or DFS), different C types have different
138 * "RESOLVED" conditions.
139 *
140 * When resolving a BTF_KIND_STRUCT, we need to resolve all its
141 * members because a member is always referring to another
142 * type. A struct's member can be treated as "RESOLVED" if
143 * it is referring to a BTF_KIND_PTR. Otherwise, the
144 * following valid C struct would be rejected:
145 *
146 * struct A {
147 * int m;
148 * struct A *a;
149 * };
150 *
151 * When resolving a BTF_KIND_PTR, it needs to keep resolving if
152 * it is referring to another BTF_KIND_PTR. Otherwise, we cannot
153 * detect a pointer loop, e.g.:
154 * BTF_KIND_CONST -> BTF_KIND_PTR -> BTF_KIND_CONST -> BTF_KIND_PTR +
155 * ^ |
156 * +-----------------------------------------+
157 *
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700158 */
159
160#define BITS_PER_U64 (sizeof(u64) * BITS_PER_BYTE)
161#define BITS_PER_BYTE_MASK (BITS_PER_BYTE - 1)
162#define BITS_PER_BYTE_MASKED(bits) ((bits) & BITS_PER_BYTE_MASK)
163#define BITS_ROUNDDOWN_BYTES(bits) ((bits) >> 3)
164#define BITS_ROUNDUP_BYTES(bits) \
165 (BITS_ROUNDDOWN_BYTES(bits) + !!BITS_PER_BYTE_MASKED(bits))
166
Martin KaFai Lauaea2f7b82018-05-22 14:57:20 -0700167#define BTF_INFO_MASK 0x0f00ffff
168#define BTF_INT_MASK 0x0fffffff
169#define BTF_TYPE_ID_VALID(type_id) ((type_id) <= BTF_MAX_TYPE)
170#define BTF_STR_OFFSET_VALID(name_off) ((name_off) <= BTF_MAX_NAME_OFFSET)
171
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700172/* 16MB for 64k structs and each has 16 members and
173 * a few MB spaces for the string section.
174 * The hard limit is S32_MAX.
175 */
176#define BTF_MAX_SIZE (16 * 1024 * 1024)
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700177
178#define for_each_member(i, struct_type, member) \
179 for (i = 0, member = btf_type_member(struct_type); \
180 i < btf_type_vlen(struct_type); \
181 i++, member++)
182
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700183#define for_each_member_from(i, from, struct_type, member) \
184 for (i = from, member = btf_type_member(struct_type) + from; \
185 i < btf_type_vlen(struct_type); \
186 i++, member++)
187
Martin KaFai Lau78958fc2018-05-04 14:49:51 -0700188static DEFINE_IDR(btf_idr);
189static DEFINE_SPINLOCK(btf_idr_lock);
190
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700191struct btf {
Martin KaFai Lauf80442a2018-05-22 14:57:18 -0700192 void *data;
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700193 struct btf_type **types;
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700194 u32 *resolved_ids;
195 u32 *resolved_sizes;
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700196 const char *strings;
197 void *nohdr_data;
Martin KaFai Lauf80442a2018-05-22 14:57:18 -0700198 struct btf_header hdr;
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700199 u32 nr_types;
200 u32 types_size;
201 u32 data_size;
Martin KaFai Lauf56a6532018-04-18 15:56:01 -0700202 refcount_t refcnt;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -0700203 u32 id;
204 struct rcu_head rcu;
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700205};
206
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700207enum verifier_phase {
208 CHECK_META,
209 CHECK_TYPE,
210};
211
212struct resolve_vertex {
213 const struct btf_type *t;
214 u32 type_id;
215 u16 next_member;
216};
217
218enum visit_state {
219 NOT_VISITED,
220 VISITED,
221 RESOLVED,
222};
223
224enum resolve_mode {
225 RESOLVE_TBD, /* To Be Determined */
226 RESOLVE_PTR, /* Resolving for Pointer */
227 RESOLVE_STRUCT_OR_ARRAY, /* Resolving for struct/union
228 * or array
229 */
230};
231
232#define MAX_RESOLVE_DEPTH 32
233
Martin KaFai Lauf80442a2018-05-22 14:57:18 -0700234struct btf_sec_info {
235 u32 off;
236 u32 len;
237};
238
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700239struct btf_verifier_env {
240 struct btf *btf;
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700241 u8 *visit_states;
242 struct resolve_vertex stack[MAX_RESOLVE_DEPTH];
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700243 struct bpf_verifier_log log;
244 u32 log_type_id;
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700245 u32 top_stack;
246 enum verifier_phase phase;
247 enum resolve_mode resolve_mode;
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700248};
249
250static const char * const btf_kind_str[NR_BTF_KINDS] = {
251 [BTF_KIND_UNKN] = "UNKNOWN",
252 [BTF_KIND_INT] = "INT",
253 [BTF_KIND_PTR] = "PTR",
254 [BTF_KIND_ARRAY] = "ARRAY",
255 [BTF_KIND_STRUCT] = "STRUCT",
256 [BTF_KIND_UNION] = "UNION",
257 [BTF_KIND_ENUM] = "ENUM",
258 [BTF_KIND_FWD] = "FWD",
259 [BTF_KIND_TYPEDEF] = "TYPEDEF",
260 [BTF_KIND_VOLATILE] = "VOLATILE",
261 [BTF_KIND_CONST] = "CONST",
262 [BTF_KIND_RESTRICT] = "RESTRICT",
Martin KaFai Lau2667a262018-11-19 15:29:08 -0800263 [BTF_KIND_FUNC] = "FUNC",
264 [BTF_KIND_FUNC_PROTO] = "FUNC_PROTO",
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700265};
266
267struct btf_kind_operations {
268 s32 (*check_meta)(struct btf_verifier_env *env,
269 const struct btf_type *t,
270 u32 meta_left);
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700271 int (*resolve)(struct btf_verifier_env *env,
272 const struct resolve_vertex *v);
Martin KaFai Lau179cde82018-04-18 15:55:59 -0700273 int (*check_member)(struct btf_verifier_env *env,
274 const struct btf_type *struct_type,
275 const struct btf_member *member,
276 const struct btf_type *member_type);
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700277 void (*log_details)(struct btf_verifier_env *env,
278 const struct btf_type *t);
Martin KaFai Laub00b8da2018-04-18 15:56:00 -0700279 void (*seq_show)(const struct btf *btf, const struct btf_type *t,
280 u32 type_id, void *data, u8 bits_offsets,
281 struct seq_file *m);
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700282};
283
284static const struct btf_kind_operations * const kind_ops[NR_BTF_KINDS];
285static struct btf_type btf_void;
286
Martin KaFai Lau2667a262018-11-19 15:29:08 -0800287static int btf_resolve(struct btf_verifier_env *env,
288 const struct btf_type *t, u32 type_id);
289
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700290static bool btf_type_is_modifier(const struct btf_type *t)
291{
292 /* Some of them is not strictly a C modifier
293 * but they are grouped into the same bucket
294 * for BTF concern:
295 * A type (t) that refers to another
296 * type through t->type AND its size cannot
297 * be determined without following the t->type.
298 *
299 * ptr does not fall into this bucket
300 * because its size is always sizeof(void *).
301 */
302 switch (BTF_INFO_KIND(t->info)) {
303 case BTF_KIND_TYPEDEF:
304 case BTF_KIND_VOLATILE:
305 case BTF_KIND_CONST:
306 case BTF_KIND_RESTRICT:
307 return true;
308 }
309
310 return false;
311}
312
313static bool btf_type_is_void(const struct btf_type *t)
314{
Martin KaFai Laub47a0bd2018-11-19 15:29:06 -0800315 return t == &btf_void;
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700316}
317
Martin KaFai Laub47a0bd2018-11-19 15:29:06 -0800318static bool btf_type_is_fwd(const struct btf_type *t)
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700319{
Martin KaFai Laub47a0bd2018-11-19 15:29:06 -0800320 return BTF_INFO_KIND(t->info) == BTF_KIND_FWD;
321}
322
Martin KaFai Lau2667a262018-11-19 15:29:08 -0800323static bool btf_type_is_func(const struct btf_type *t)
324{
325 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC;
326}
327
328static bool btf_type_is_func_proto(const struct btf_type *t)
329{
330 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC_PROTO;
331}
332
Martin KaFai Laub47a0bd2018-11-19 15:29:06 -0800333static bool btf_type_nosize(const struct btf_type *t)
334{
Martin KaFai Lau2667a262018-11-19 15:29:08 -0800335 return btf_type_is_void(t) || btf_type_is_fwd(t) ||
336 btf_type_is_func(t) || btf_type_is_func_proto(t);
Martin KaFai Laub47a0bd2018-11-19 15:29:06 -0800337}
338
339static bool btf_type_nosize_or_null(const struct btf_type *t)
340{
341 return !t || btf_type_nosize(t);
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700342}
343
344/* union is only a special case of struct:
345 * all its offsetof(member) == 0
346 */
347static bool btf_type_is_struct(const struct btf_type *t)
348{
349 u8 kind = BTF_INFO_KIND(t->info);
350
351 return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
352}
353
354static bool btf_type_is_array(const struct btf_type *t)
355{
356 return BTF_INFO_KIND(t->info) == BTF_KIND_ARRAY;
357}
358
359static bool btf_type_is_ptr(const struct btf_type *t)
360{
361 return BTF_INFO_KIND(t->info) == BTF_KIND_PTR;
362}
363
364static bool btf_type_is_int(const struct btf_type *t)
365{
366 return BTF_INFO_KIND(t->info) == BTF_KIND_INT;
367}
368
369/* What types need to be resolved?
370 *
371 * btf_type_is_modifier() is an obvious one.
372 *
373 * btf_type_is_struct() because its member refers to
374 * another type (through member->type).
375
376 * btf_type_is_array() because its element (array->type)
377 * refers to another type. Array can be thought of a
378 * special case of struct while array just has the same
379 * member-type repeated by array->nelems of times.
380 */
381static bool btf_type_needs_resolve(const struct btf_type *t)
382{
383 return btf_type_is_modifier(t) ||
384 btf_type_is_ptr(t) ||
385 btf_type_is_struct(t) ||
386 btf_type_is_array(t);
387}
388
389/* t->size can be used */
390static bool btf_type_has_size(const struct btf_type *t)
391{
392 switch (BTF_INFO_KIND(t->info)) {
393 case BTF_KIND_INT:
394 case BTF_KIND_STRUCT:
395 case BTF_KIND_UNION:
396 case BTF_KIND_ENUM:
397 return true;
398 }
399
400 return false;
401}
402
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700403static const char *btf_int_encoding_str(u8 encoding)
404{
405 if (encoding == 0)
406 return "(none)";
407 else if (encoding == BTF_INT_SIGNED)
408 return "SIGNED";
409 else if (encoding == BTF_INT_CHAR)
410 return "CHAR";
411 else if (encoding == BTF_INT_BOOL)
412 return "BOOL";
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700413 else
414 return "UNKN";
415}
416
417static u16 btf_type_vlen(const struct btf_type *t)
418{
419 return BTF_INFO_VLEN(t->info);
420}
421
422static u32 btf_type_int(const struct btf_type *t)
423{
424 return *(u32 *)(t + 1);
425}
426
427static const struct btf_array *btf_type_array(const struct btf_type *t)
428{
429 return (const struct btf_array *)(t + 1);
430}
431
432static const struct btf_member *btf_type_member(const struct btf_type *t)
433{
434 return (const struct btf_member *)(t + 1);
435}
436
437static const struct btf_enum *btf_type_enum(const struct btf_type *t)
438{
439 return (const struct btf_enum *)(t + 1);
440}
441
442static const struct btf_kind_operations *btf_type_ops(const struct btf_type *t)
443{
444 return kind_ops[BTF_INFO_KIND(t->info)];
445}
446
Martin KaFai Lauc454a462018-12-07 16:42:25 -0800447bool btf_name_offset_valid(const struct btf *btf, u32 offset)
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700448{
Martin KaFai Lauaea2f7b82018-05-22 14:57:20 -0700449 return BTF_STR_OFFSET_VALID(offset) &&
450 offset < btf->hdr.str_len;
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700451}
452
Martin KaFai Lau2667a262018-11-19 15:29:08 -0800453/* Only C-style identifier is permitted. This can be relaxed if
454 * necessary.
455 */
456static bool btf_name_valid_identifier(const struct btf *btf, u32 offset)
457{
458 /* offset must be valid */
459 const char *src = &btf->strings[offset];
460 const char *src_limit;
461
462 if (!isalpha(*src) && *src != '_')
463 return false;
464
465 /* set a limit on identifier length */
466 src_limit = src + KSYM_NAME_LEN;
467 src++;
468 while (*src && src < src_limit) {
469 if (!isalnum(*src) && *src != '_')
470 return false;
471 src++;
472 }
473
474 return !*src;
475}
476
Martin KaFai Lau23127b32018-12-13 10:41:46 -0800477static const char *__btf_name_by_offset(const struct btf *btf, u32 offset)
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700478{
Martin KaFai Lauaea2f7b82018-05-22 14:57:20 -0700479 if (!offset)
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700480 return "(anon)";
Martin KaFai Lauaea2f7b82018-05-22 14:57:20 -0700481 else if (offset < btf->hdr.str_len)
482 return &btf->strings[offset];
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700483 else
484 return "(invalid-name-offset)";
485}
486
Martin KaFai Lau23127b32018-12-13 10:41:46 -0800487const char *btf_name_by_offset(const struct btf *btf, u32 offset)
488{
489 if (offset < btf->hdr.str_len)
490 return &btf->strings[offset];
491
492 return NULL;
493}
494
Yonghong Song838e9692018-11-19 15:29:11 -0800495const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id)
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700496{
497 if (type_id > btf->nr_types)
498 return NULL;
499
500 return btf->types[type_id];
501}
502
Martin KaFai Lau4ef5f572018-05-22 14:57:19 -0700503/*
504 * Regular int is not a bit field and it must be either
505 * u8/u16/u32/u64.
506 */
507static bool btf_type_int_is_regular(const struct btf_type *t)
508{
Martin KaFai Lau36fc3c82018-07-19 22:14:31 -0700509 u8 nr_bits, nr_bytes;
Martin KaFai Lau4ef5f572018-05-22 14:57:19 -0700510 u32 int_data;
511
512 int_data = btf_type_int(t);
513 nr_bits = BTF_INT_BITS(int_data);
514 nr_bytes = BITS_ROUNDUP_BYTES(nr_bits);
515 if (BITS_PER_BYTE_MASKED(nr_bits) ||
516 BTF_INT_OFFSET(int_data) ||
517 (nr_bytes != sizeof(u8) && nr_bytes != sizeof(u16) &&
518 nr_bytes != sizeof(u32) && nr_bytes != sizeof(u64))) {
519 return false;
520 }
521
522 return true;
523}
524
Roman Gushchin9a1126b2018-12-10 15:43:01 -0800525/*
526 * Check that given type is a regular int and has the expected size.
527 */
528bool btf_type_is_reg_int(const struct btf_type *t, u32 expected_size)
529{
530 u8 nr_bits, nr_bytes;
531 u32 int_data;
532
533 if (!btf_type_is_int(t))
534 return false;
535
536 int_data = btf_type_int(t);
537 nr_bits = BTF_INT_BITS(int_data);
538 nr_bytes = BITS_ROUNDUP_BYTES(nr_bits);
539 if (BITS_PER_BYTE_MASKED(nr_bits) ||
540 BTF_INT_OFFSET(int_data) ||
541 nr_bytes != expected_size)
542 return false;
543
544 return true;
545}
546
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700547__printf(2, 3) static void __btf_verifier_log(struct bpf_verifier_log *log,
548 const char *fmt, ...)
549{
550 va_list args;
551
552 va_start(args, fmt);
553 bpf_verifier_vlog(log, fmt, args);
554 va_end(args);
555}
556
557__printf(2, 3) static void btf_verifier_log(struct btf_verifier_env *env,
558 const char *fmt, ...)
559{
560 struct bpf_verifier_log *log = &env->log;
561 va_list args;
562
563 if (!bpf_verifier_log_needed(log))
564 return;
565
566 va_start(args, fmt);
567 bpf_verifier_vlog(log, fmt, args);
568 va_end(args);
569}
570
571__printf(4, 5) static void __btf_verifier_log_type(struct btf_verifier_env *env,
572 const struct btf_type *t,
573 bool log_details,
574 const char *fmt, ...)
575{
576 struct bpf_verifier_log *log = &env->log;
577 u8 kind = BTF_INFO_KIND(t->info);
578 struct btf *btf = env->btf;
579 va_list args;
580
581 if (!bpf_verifier_log_needed(log))
582 return;
583
584 __btf_verifier_log(log, "[%u] %s %s%s",
585 env->log_type_id,
586 btf_kind_str[kind],
Martin KaFai Lau23127b32018-12-13 10:41:46 -0800587 __btf_name_by_offset(btf, t->name_off),
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700588 log_details ? " " : "");
589
590 if (log_details)
591 btf_type_ops(t)->log_details(env, t);
592
593 if (fmt && *fmt) {
594 __btf_verifier_log(log, " ");
595 va_start(args, fmt);
596 bpf_verifier_vlog(log, fmt, args);
597 va_end(args);
598 }
599
600 __btf_verifier_log(log, "\n");
601}
602
603#define btf_verifier_log_type(env, t, ...) \
604 __btf_verifier_log_type((env), (t), true, __VA_ARGS__)
605#define btf_verifier_log_basic(env, t, ...) \
606 __btf_verifier_log_type((env), (t), false, __VA_ARGS__)
607
608__printf(4, 5)
609static void btf_verifier_log_member(struct btf_verifier_env *env,
610 const struct btf_type *struct_type,
611 const struct btf_member *member,
612 const char *fmt, ...)
613{
614 struct bpf_verifier_log *log = &env->log;
615 struct btf *btf = env->btf;
616 va_list args;
617
618 if (!bpf_verifier_log_needed(log))
619 return;
620
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700621 /* The CHECK_META phase already did a btf dump.
622 *
623 * If member is logged again, it must hit an error in
624 * parsing this member. It is useful to print out which
625 * struct this member belongs to.
626 */
627 if (env->phase != CHECK_META)
628 btf_verifier_log_type(env, struct_type, NULL);
629
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700630 __btf_verifier_log(log, "\t%s type_id=%u bits_offset=%u",
Martin KaFai Lau23127b32018-12-13 10:41:46 -0800631 __btf_name_by_offset(btf, member->name_off),
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700632 member->type, member->offset);
633
634 if (fmt && *fmt) {
635 __btf_verifier_log(log, " ");
636 va_start(args, fmt);
637 bpf_verifier_vlog(log, fmt, args);
638 va_end(args);
639 }
640
641 __btf_verifier_log(log, "\n");
642}
643
Martin KaFai Lauf80442a2018-05-22 14:57:18 -0700644static void btf_verifier_log_hdr(struct btf_verifier_env *env,
645 u32 btf_data_size)
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700646{
647 struct bpf_verifier_log *log = &env->log;
648 const struct btf *btf = env->btf;
649 const struct btf_header *hdr;
650
651 if (!bpf_verifier_log_needed(log))
652 return;
653
Martin KaFai Lauf80442a2018-05-22 14:57:18 -0700654 hdr = &btf->hdr;
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700655 __btf_verifier_log(log, "magic: 0x%x\n", hdr->magic);
656 __btf_verifier_log(log, "version: %u\n", hdr->version);
657 __btf_verifier_log(log, "flags: 0x%x\n", hdr->flags);
Martin KaFai Lauf80442a2018-05-22 14:57:18 -0700658 __btf_verifier_log(log, "hdr_len: %u\n", hdr->hdr_len);
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700659 __btf_verifier_log(log, "type_off: %u\n", hdr->type_off);
Martin KaFai Lauf80442a2018-05-22 14:57:18 -0700660 __btf_verifier_log(log, "type_len: %u\n", hdr->type_len);
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700661 __btf_verifier_log(log, "str_off: %u\n", hdr->str_off);
662 __btf_verifier_log(log, "str_len: %u\n", hdr->str_len);
Martin KaFai Lauf80442a2018-05-22 14:57:18 -0700663 __btf_verifier_log(log, "btf_total_size: %u\n", btf_data_size);
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700664}
665
666static int btf_add_type(struct btf_verifier_env *env, struct btf_type *t)
667{
668 struct btf *btf = env->btf;
669
670 /* < 2 because +1 for btf_void which is always in btf->types[0].
671 * btf_void is not accounted in btf->nr_types because btf_void
672 * does not come from the BTF file.
673 */
674 if (btf->types_size - btf->nr_types < 2) {
675 /* Expand 'types' array */
676
677 struct btf_type **new_types;
678 u32 expand_by, new_size;
679
Martin KaFai Lauaea2f7b82018-05-22 14:57:20 -0700680 if (btf->types_size == BTF_MAX_TYPE) {
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700681 btf_verifier_log(env, "Exceeded max num of types");
682 return -E2BIG;
683 }
684
685 expand_by = max_t(u32, btf->types_size >> 2, 16);
Martin KaFai Lauaea2f7b82018-05-22 14:57:20 -0700686 new_size = min_t(u32, BTF_MAX_TYPE,
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700687 btf->types_size + expand_by);
688
Kees Cook778e1cd2018-06-12 14:04:48 -0700689 new_types = kvcalloc(new_size, sizeof(*new_types),
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700690 GFP_KERNEL | __GFP_NOWARN);
691 if (!new_types)
692 return -ENOMEM;
693
694 if (btf->nr_types == 0)
695 new_types[0] = &btf_void;
696 else
697 memcpy(new_types, btf->types,
698 sizeof(*btf->types) * (btf->nr_types + 1));
699
700 kvfree(btf->types);
701 btf->types = new_types;
702 btf->types_size = new_size;
703 }
704
705 btf->types[++(btf->nr_types)] = t;
706
707 return 0;
708}
709
Martin KaFai Lau78958fc2018-05-04 14:49:51 -0700710static int btf_alloc_id(struct btf *btf)
711{
712 int id;
713
714 idr_preload(GFP_KERNEL);
715 spin_lock_bh(&btf_idr_lock);
716 id = idr_alloc_cyclic(&btf_idr, btf, 1, INT_MAX, GFP_ATOMIC);
717 if (id > 0)
718 btf->id = id;
719 spin_unlock_bh(&btf_idr_lock);
720 idr_preload_end();
721
722 if (WARN_ON_ONCE(!id))
723 return -ENOSPC;
724
725 return id > 0 ? 0 : id;
726}
727
728static void btf_free_id(struct btf *btf)
729{
730 unsigned long flags;
731
732 /*
733 * In map-in-map, calling map_delete_elem() on outer
734 * map will call bpf_map_put on the inner map.
735 * It will then eventually call btf_free_id()
736 * on the inner map. Some of the map_delete_elem()
737 * implementation may have irq disabled, so
738 * we need to use the _irqsave() version instead
739 * of the _bh() version.
740 */
741 spin_lock_irqsave(&btf_idr_lock, flags);
742 idr_remove(&btf_idr, btf->id);
743 spin_unlock_irqrestore(&btf_idr_lock, flags);
744}
745
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700746static void btf_free(struct btf *btf)
747{
748 kvfree(btf->types);
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700749 kvfree(btf->resolved_sizes);
750 kvfree(btf->resolved_ids);
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700751 kvfree(btf->data);
752 kfree(btf);
753}
754
Martin KaFai Lau78958fc2018-05-04 14:49:51 -0700755static void btf_free_rcu(struct rcu_head *rcu)
Martin KaFai Lauf56a6532018-04-18 15:56:01 -0700756{
Martin KaFai Lau78958fc2018-05-04 14:49:51 -0700757 struct btf *btf = container_of(rcu, struct btf, rcu);
758
759 btf_free(btf);
Martin KaFai Lauf56a6532018-04-18 15:56:01 -0700760}
761
762void btf_put(struct btf *btf)
763{
Martin KaFai Lau78958fc2018-05-04 14:49:51 -0700764 if (btf && refcount_dec_and_test(&btf->refcnt)) {
765 btf_free_id(btf);
766 call_rcu(&btf->rcu, btf_free_rcu);
767 }
Martin KaFai Lauf56a6532018-04-18 15:56:01 -0700768}
769
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700770static int env_resolve_init(struct btf_verifier_env *env)
771{
772 struct btf *btf = env->btf;
773 u32 nr_types = btf->nr_types;
774 u32 *resolved_sizes = NULL;
775 u32 *resolved_ids = NULL;
776 u8 *visit_states = NULL;
777
778 /* +1 for btf_void */
Kees Cook778e1cd2018-06-12 14:04:48 -0700779 resolved_sizes = kvcalloc(nr_types + 1, sizeof(*resolved_sizes),
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700780 GFP_KERNEL | __GFP_NOWARN);
781 if (!resolved_sizes)
782 goto nomem;
783
Kees Cook778e1cd2018-06-12 14:04:48 -0700784 resolved_ids = kvcalloc(nr_types + 1, sizeof(*resolved_ids),
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700785 GFP_KERNEL | __GFP_NOWARN);
786 if (!resolved_ids)
787 goto nomem;
788
Kees Cook778e1cd2018-06-12 14:04:48 -0700789 visit_states = kvcalloc(nr_types + 1, sizeof(*visit_states),
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700790 GFP_KERNEL | __GFP_NOWARN);
791 if (!visit_states)
792 goto nomem;
793
794 btf->resolved_sizes = resolved_sizes;
795 btf->resolved_ids = resolved_ids;
796 env->visit_states = visit_states;
797
798 return 0;
799
800nomem:
801 kvfree(resolved_sizes);
802 kvfree(resolved_ids);
803 kvfree(visit_states);
804 return -ENOMEM;
805}
806
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700807static void btf_verifier_env_free(struct btf_verifier_env *env)
808{
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700809 kvfree(env->visit_states);
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700810 kfree(env);
811}
812
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700813static bool env_type_is_resolve_sink(const struct btf_verifier_env *env,
814 const struct btf_type *next_type)
815{
816 switch (env->resolve_mode) {
817 case RESOLVE_TBD:
818 /* int, enum or void is a sink */
819 return !btf_type_needs_resolve(next_type);
820 case RESOLVE_PTR:
Martin KaFai Lau2667a262018-11-19 15:29:08 -0800821 /* int, enum, void, struct, array, func or func_proto is a sink
822 * for ptr
823 */
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700824 return !btf_type_is_modifier(next_type) &&
825 !btf_type_is_ptr(next_type);
826 case RESOLVE_STRUCT_OR_ARRAY:
Martin KaFai Lau2667a262018-11-19 15:29:08 -0800827 /* int, enum, void, ptr, func or func_proto is a sink
828 * for struct and array
829 */
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700830 return !btf_type_is_modifier(next_type) &&
831 !btf_type_is_array(next_type) &&
832 !btf_type_is_struct(next_type);
833 default:
Arnd Bergmann53c80362018-05-25 23:33:19 +0200834 BUG();
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700835 }
836}
837
838static bool env_type_is_resolved(const struct btf_verifier_env *env,
839 u32 type_id)
840{
841 return env->visit_states[type_id] == RESOLVED;
842}
843
844static int env_stack_push(struct btf_verifier_env *env,
845 const struct btf_type *t, u32 type_id)
846{
847 struct resolve_vertex *v;
848
849 if (env->top_stack == MAX_RESOLVE_DEPTH)
850 return -E2BIG;
851
852 if (env->visit_states[type_id] != NOT_VISITED)
853 return -EEXIST;
854
855 env->visit_states[type_id] = VISITED;
856
857 v = &env->stack[env->top_stack++];
858 v->t = t;
859 v->type_id = type_id;
860 v->next_member = 0;
861
862 if (env->resolve_mode == RESOLVE_TBD) {
863 if (btf_type_is_ptr(t))
864 env->resolve_mode = RESOLVE_PTR;
865 else if (btf_type_is_struct(t) || btf_type_is_array(t))
866 env->resolve_mode = RESOLVE_STRUCT_OR_ARRAY;
867 }
868
869 return 0;
870}
871
872static void env_stack_set_next_member(struct btf_verifier_env *env,
873 u16 next_member)
874{
875 env->stack[env->top_stack - 1].next_member = next_member;
876}
877
878static void env_stack_pop_resolved(struct btf_verifier_env *env,
879 u32 resolved_type_id,
880 u32 resolved_size)
881{
882 u32 type_id = env->stack[--(env->top_stack)].type_id;
883 struct btf *btf = env->btf;
884
885 btf->resolved_sizes[type_id] = resolved_size;
886 btf->resolved_ids[type_id] = resolved_type_id;
887 env->visit_states[type_id] = RESOLVED;
888}
889
890static const struct resolve_vertex *env_stack_peak(struct btf_verifier_env *env)
891{
892 return env->top_stack ? &env->stack[env->top_stack - 1] : NULL;
893}
894
895/* The input param "type_id" must point to a needs_resolve type */
896static const struct btf_type *btf_type_id_resolve(const struct btf *btf,
897 u32 *type_id)
898{
899 *type_id = btf->resolved_ids[*type_id];
900 return btf_type_by_id(btf, *type_id);
901}
902
903const struct btf_type *btf_type_id_size(const struct btf *btf,
904 u32 *type_id, u32 *ret_size)
905{
906 const struct btf_type *size_type;
907 u32 size_type_id = *type_id;
908 u32 size = 0;
909
910 size_type = btf_type_by_id(btf, size_type_id);
Martin KaFai Laub47a0bd2018-11-19 15:29:06 -0800911 if (btf_type_nosize_or_null(size_type))
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700912 return NULL;
913
914 if (btf_type_has_size(size_type)) {
915 size = size_type->size;
916 } else if (btf_type_is_array(size_type)) {
917 size = btf->resolved_sizes[size_type_id];
918 } else if (btf_type_is_ptr(size_type)) {
919 size = sizeof(void *);
920 } else {
921 if (WARN_ON_ONCE(!btf_type_is_modifier(size_type)))
922 return NULL;
923
924 size = btf->resolved_sizes[size_type_id];
925 size_type_id = btf->resolved_ids[size_type_id];
926 size_type = btf_type_by_id(btf, size_type_id);
Martin KaFai Laub47a0bd2018-11-19 15:29:06 -0800927 if (btf_type_nosize_or_null(size_type))
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700928 return NULL;
929 }
930
931 *type_id = size_type_id;
932 if (ret_size)
933 *ret_size = size;
934
935 return size_type;
936}
937
Martin KaFai Lau179cde82018-04-18 15:55:59 -0700938static int btf_df_check_member(struct btf_verifier_env *env,
939 const struct btf_type *struct_type,
940 const struct btf_member *member,
941 const struct btf_type *member_type)
942{
943 btf_verifier_log_basic(env, struct_type,
944 "Unsupported check_member");
945 return -EINVAL;
946}
947
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700948static int btf_df_resolve(struct btf_verifier_env *env,
949 const struct resolve_vertex *v)
950{
951 btf_verifier_log_basic(env, v->t, "Unsupported resolve");
952 return -EINVAL;
953}
954
Martin KaFai Laub00b8da2018-04-18 15:56:00 -0700955static void btf_df_seq_show(const struct btf *btf, const struct btf_type *t,
956 u32 type_id, void *data, u8 bits_offsets,
957 struct seq_file *m)
958{
959 seq_printf(m, "<unsupported kind:%u>", BTF_INFO_KIND(t->info));
960}
961
Martin KaFai Lau179cde82018-04-18 15:55:59 -0700962static int btf_int_check_member(struct btf_verifier_env *env,
963 const struct btf_type *struct_type,
964 const struct btf_member *member,
965 const struct btf_type *member_type)
966{
967 u32 int_data = btf_type_int(member_type);
968 u32 struct_bits_off = member->offset;
969 u32 struct_size = struct_type->size;
970 u32 nr_copy_bits;
971 u32 bytes_offset;
972
973 if (U32_MAX - struct_bits_off < BTF_INT_OFFSET(int_data)) {
974 btf_verifier_log_member(env, struct_type, member,
975 "bits_offset exceeds U32_MAX");
976 return -EINVAL;
977 }
978
979 struct_bits_off += BTF_INT_OFFSET(int_data);
980 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
981 nr_copy_bits = BTF_INT_BITS(int_data) +
982 BITS_PER_BYTE_MASKED(struct_bits_off);
983
984 if (nr_copy_bits > BITS_PER_U64) {
985 btf_verifier_log_member(env, struct_type, member,
986 "nr_copy_bits exceeds 64");
987 return -EINVAL;
988 }
989
990 if (struct_size < bytes_offset ||
991 struct_size - bytes_offset < BITS_ROUNDUP_BYTES(nr_copy_bits)) {
992 btf_verifier_log_member(env, struct_type, member,
993 "Member exceeds struct_size");
994 return -EINVAL;
995 }
996
997 return 0;
998}
999
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001000static s32 btf_int_check_meta(struct btf_verifier_env *env,
1001 const struct btf_type *t,
1002 u32 meta_left)
1003{
1004 u32 int_data, nr_bits, meta_needed = sizeof(int_data);
1005 u16 encoding;
1006
1007 if (meta_left < meta_needed) {
1008 btf_verifier_log_basic(env, t,
1009 "meta_left:%u meta_needed:%u",
1010 meta_left, meta_needed);
1011 return -EINVAL;
1012 }
1013
1014 if (btf_type_vlen(t)) {
1015 btf_verifier_log_type(env, t, "vlen != 0");
1016 return -EINVAL;
1017 }
1018
1019 int_data = btf_type_int(t);
Martin KaFai Lauaea2f7b82018-05-22 14:57:20 -07001020 if (int_data & ~BTF_INT_MASK) {
1021 btf_verifier_log_basic(env, t, "Invalid int_data:%x",
1022 int_data);
1023 return -EINVAL;
1024 }
1025
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001026 nr_bits = BTF_INT_BITS(int_data) + BTF_INT_OFFSET(int_data);
1027
1028 if (nr_bits > BITS_PER_U64) {
1029 btf_verifier_log_type(env, t, "nr_bits exceeds %zu",
1030 BITS_PER_U64);
1031 return -EINVAL;
1032 }
1033
1034 if (BITS_ROUNDUP_BYTES(nr_bits) > t->size) {
1035 btf_verifier_log_type(env, t, "nr_bits exceeds type_size");
1036 return -EINVAL;
1037 }
1038
Martin KaFai Lauaea2f7b82018-05-22 14:57:20 -07001039 /*
1040 * Only one of the encoding bits is allowed and it
1041 * should be sufficient for the pretty print purpose (i.e. decoding).
1042 * Multiple bits can be allowed later if it is found
1043 * to be insufficient.
1044 */
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001045 encoding = BTF_INT_ENCODING(int_data);
1046 if (encoding &&
1047 encoding != BTF_INT_SIGNED &&
1048 encoding != BTF_INT_CHAR &&
Martin KaFai Lauaea2f7b82018-05-22 14:57:20 -07001049 encoding != BTF_INT_BOOL) {
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001050 btf_verifier_log_type(env, t, "Unsupported encoding");
1051 return -ENOTSUPP;
1052 }
1053
1054 btf_verifier_log_type(env, t, NULL);
1055
1056 return meta_needed;
1057}
1058
1059static void btf_int_log(struct btf_verifier_env *env,
1060 const struct btf_type *t)
1061{
1062 int int_data = btf_type_int(t);
1063
1064 btf_verifier_log(env,
1065 "size=%u bits_offset=%u nr_bits=%u encoding=%s",
1066 t->size, BTF_INT_OFFSET(int_data),
1067 BTF_INT_BITS(int_data),
1068 btf_int_encoding_str(BTF_INT_ENCODING(int_data)));
1069}
1070
Yonghong Songf97be3a2018-12-15 22:13:50 -08001071static void btf_bitfield_seq_show(void *data, u8 bits_offset,
1072 u8 nr_bits, struct seq_file *m)
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07001073{
Okash Khawajab65f3702018-07-10 14:33:07 -07001074 u16 left_shift_bits, right_shift_bits;
Martin KaFai Lau36fc3c82018-07-19 22:14:31 -07001075 u8 nr_copy_bytes;
1076 u8 nr_copy_bits;
Okash Khawajab65f3702018-07-10 14:33:07 -07001077 u64 print_num;
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07001078
Yonghong Songf97be3a2018-12-15 22:13:50 -08001079 data += BITS_ROUNDDOWN_BYTES(bits_offset);
1080 bits_offset = BITS_PER_BYTE_MASKED(bits_offset);
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07001081 nr_copy_bits = nr_bits + bits_offset;
1082 nr_copy_bytes = BITS_ROUNDUP_BYTES(nr_copy_bits);
1083
Okash Khawajab65f3702018-07-10 14:33:07 -07001084 print_num = 0;
1085 memcpy(&print_num, data, nr_copy_bytes);
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07001086
Okash Khawajab65f3702018-07-10 14:33:07 -07001087#ifdef __BIG_ENDIAN_BITFIELD
1088 left_shift_bits = bits_offset;
1089#else
1090 left_shift_bits = BITS_PER_U64 - nr_copy_bits;
1091#endif
1092 right_shift_bits = BITS_PER_U64 - nr_bits;
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07001093
Okash Khawajab65f3702018-07-10 14:33:07 -07001094 print_num <<= left_shift_bits;
1095 print_num >>= right_shift_bits;
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07001096
Okash Khawajab65f3702018-07-10 14:33:07 -07001097 seq_printf(m, "0x%llx", print_num);
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07001098}
1099
Yonghong Songf97be3a2018-12-15 22:13:50 -08001100static void btf_int_bits_seq_show(const struct btf *btf,
1101 const struct btf_type *t,
1102 void *data, u8 bits_offset,
1103 struct seq_file *m)
1104{
1105 u32 int_data = btf_type_int(t);
1106 u8 nr_bits = BTF_INT_BITS(int_data);
1107 u8 total_bits_offset;
1108
1109 /*
1110 * bits_offset is at most 7.
1111 * BTF_INT_OFFSET() cannot exceed 64 bits.
1112 */
1113 total_bits_offset = bits_offset + BTF_INT_OFFSET(int_data);
1114 btf_bitfield_seq_show(data, total_bits_offset, nr_bits, m);
1115}
1116
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07001117static void btf_int_seq_show(const struct btf *btf, const struct btf_type *t,
1118 u32 type_id, void *data, u8 bits_offset,
1119 struct seq_file *m)
1120{
1121 u32 int_data = btf_type_int(t);
1122 u8 encoding = BTF_INT_ENCODING(int_data);
1123 bool sign = encoding & BTF_INT_SIGNED;
Martin KaFai Lau36fc3c82018-07-19 22:14:31 -07001124 u8 nr_bits = BTF_INT_BITS(int_data);
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07001125
1126 if (bits_offset || BTF_INT_OFFSET(int_data) ||
1127 BITS_PER_BYTE_MASKED(nr_bits)) {
1128 btf_int_bits_seq_show(btf, t, data, bits_offset, m);
1129 return;
1130 }
1131
1132 switch (nr_bits) {
1133 case 64:
1134 if (sign)
1135 seq_printf(m, "%lld", *(s64 *)data);
1136 else
1137 seq_printf(m, "%llu", *(u64 *)data);
1138 break;
1139 case 32:
1140 if (sign)
1141 seq_printf(m, "%d", *(s32 *)data);
1142 else
1143 seq_printf(m, "%u", *(u32 *)data);
1144 break;
1145 case 16:
1146 if (sign)
1147 seq_printf(m, "%d", *(s16 *)data);
1148 else
1149 seq_printf(m, "%u", *(u16 *)data);
1150 break;
1151 case 8:
1152 if (sign)
1153 seq_printf(m, "%d", *(s8 *)data);
1154 else
1155 seq_printf(m, "%u", *(u8 *)data);
1156 break;
1157 default:
1158 btf_int_bits_seq_show(btf, t, data, bits_offset, m);
1159 }
1160}
1161
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001162static const struct btf_kind_operations int_ops = {
1163 .check_meta = btf_int_check_meta,
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001164 .resolve = btf_df_resolve,
Martin KaFai Lau179cde82018-04-18 15:55:59 -07001165 .check_member = btf_int_check_member,
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001166 .log_details = btf_int_log,
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07001167 .seq_show = btf_int_seq_show,
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001168};
1169
Martin KaFai Lau179cde82018-04-18 15:55:59 -07001170static int btf_modifier_check_member(struct btf_verifier_env *env,
1171 const struct btf_type *struct_type,
1172 const struct btf_member *member,
1173 const struct btf_type *member_type)
1174{
1175 const struct btf_type *resolved_type;
1176 u32 resolved_type_id = member->type;
1177 struct btf_member resolved_member;
1178 struct btf *btf = env->btf;
1179
1180 resolved_type = btf_type_id_size(btf, &resolved_type_id, NULL);
1181 if (!resolved_type) {
1182 btf_verifier_log_member(env, struct_type, member,
1183 "Invalid member");
1184 return -EINVAL;
1185 }
1186
1187 resolved_member = *member;
1188 resolved_member.type = resolved_type_id;
1189
1190 return btf_type_ops(resolved_type)->check_member(env, struct_type,
1191 &resolved_member,
1192 resolved_type);
1193}
1194
1195static int btf_ptr_check_member(struct btf_verifier_env *env,
1196 const struct btf_type *struct_type,
1197 const struct btf_member *member,
1198 const struct btf_type *member_type)
1199{
1200 u32 struct_size, struct_bits_off, bytes_offset;
1201
1202 struct_size = struct_type->size;
1203 struct_bits_off = member->offset;
1204 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
1205
1206 if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
1207 btf_verifier_log_member(env, struct_type, member,
1208 "Member is not byte aligned");
1209 return -EINVAL;
1210 }
1211
1212 if (struct_size - bytes_offset < sizeof(void *)) {
1213 btf_verifier_log_member(env, struct_type, member,
1214 "Member exceeds struct_size");
1215 return -EINVAL;
1216 }
1217
1218 return 0;
1219}
1220
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001221static int btf_ref_type_check_meta(struct btf_verifier_env *env,
1222 const struct btf_type *t,
1223 u32 meta_left)
1224{
1225 if (btf_type_vlen(t)) {
1226 btf_verifier_log_type(env, t, "vlen != 0");
1227 return -EINVAL;
1228 }
1229
Martin KaFai Lauaea2f7b82018-05-22 14:57:20 -07001230 if (!BTF_TYPE_ID_VALID(t->type)) {
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001231 btf_verifier_log_type(env, t, "Invalid type_id");
1232 return -EINVAL;
1233 }
1234
Yonghong Songeb04bbb2018-11-27 13:23:28 -08001235 /* typedef type must have a valid name, and other ref types,
1236 * volatile, const, restrict, should have a null name.
1237 */
1238 if (BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF) {
1239 if (!t->name_off ||
1240 !btf_name_valid_identifier(env->btf, t->name_off)) {
1241 btf_verifier_log_type(env, t, "Invalid name");
1242 return -EINVAL;
1243 }
1244 } else {
1245 if (t->name_off) {
1246 btf_verifier_log_type(env, t, "Invalid name");
1247 return -EINVAL;
1248 }
1249 }
1250
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001251 btf_verifier_log_type(env, t, NULL);
1252
1253 return 0;
1254}
1255
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001256static int btf_modifier_resolve(struct btf_verifier_env *env,
1257 const struct resolve_vertex *v)
1258{
1259 const struct btf_type *t = v->t;
1260 const struct btf_type *next_type;
1261 u32 next_type_id = t->type;
1262 struct btf *btf = env->btf;
1263 u32 next_type_size = 0;
1264
1265 next_type = btf_type_by_id(btf, next_type_id);
1266 if (!next_type) {
1267 btf_verifier_log_type(env, v->t, "Invalid type_id");
1268 return -EINVAL;
1269 }
1270
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001271 if (!env_type_is_resolve_sink(env, next_type) &&
1272 !env_type_is_resolved(env, next_type_id))
1273 return env_stack_push(env, next_type, next_type_id);
1274
1275 /* Figure out the resolved next_type_id with size.
1276 * They will be stored in the current modifier's
1277 * resolved_ids and resolved_sizes such that it can
1278 * save us a few type-following when we use it later (e.g. in
1279 * pretty print).
1280 */
Martin KaFai Lau2667a262018-11-19 15:29:08 -08001281 if (!btf_type_id_size(btf, &next_type_id, &next_type_size)) {
1282 if (env_type_is_resolved(env, next_type_id))
1283 next_type = btf_type_id_resolve(btf, &next_type_id);
1284
1285 /* "typedef void new_void", "const void"...etc */
1286 if (!btf_type_is_void(next_type) &&
1287 !btf_type_is_fwd(next_type)) {
1288 btf_verifier_log_type(env, v->t, "Invalid type_id");
1289 return -EINVAL;
1290 }
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001291 }
1292
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001293 env_stack_pop_resolved(env, next_type_id, next_type_size);
1294
1295 return 0;
1296}
1297
1298static int btf_ptr_resolve(struct btf_verifier_env *env,
1299 const struct resolve_vertex *v)
1300{
1301 const struct btf_type *next_type;
1302 const struct btf_type *t = v->t;
1303 u32 next_type_id = t->type;
1304 struct btf *btf = env->btf;
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001305
1306 next_type = btf_type_by_id(btf, next_type_id);
1307 if (!next_type) {
1308 btf_verifier_log_type(env, v->t, "Invalid type_id");
1309 return -EINVAL;
1310 }
1311
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001312 if (!env_type_is_resolve_sink(env, next_type) &&
1313 !env_type_is_resolved(env, next_type_id))
1314 return env_stack_push(env, next_type, next_type_id);
1315
1316 /* If the modifier was RESOLVED during RESOLVE_STRUCT_OR_ARRAY,
1317 * the modifier may have stopped resolving when it was resolved
1318 * to a ptr (last-resolved-ptr).
1319 *
1320 * We now need to continue from the last-resolved-ptr to
1321 * ensure the last-resolved-ptr will not referring back to
1322 * the currenct ptr (t).
1323 */
1324 if (btf_type_is_modifier(next_type)) {
1325 const struct btf_type *resolved_type;
1326 u32 resolved_type_id;
1327
1328 resolved_type_id = next_type_id;
1329 resolved_type = btf_type_id_resolve(btf, &resolved_type_id);
1330
1331 if (btf_type_is_ptr(resolved_type) &&
1332 !env_type_is_resolve_sink(env, resolved_type) &&
1333 !env_type_is_resolved(env, resolved_type_id))
1334 return env_stack_push(env, resolved_type,
1335 resolved_type_id);
1336 }
1337
Martin KaFai Lau2667a262018-11-19 15:29:08 -08001338 if (!btf_type_id_size(btf, &next_type_id, NULL)) {
1339 if (env_type_is_resolved(env, next_type_id))
1340 next_type = btf_type_id_resolve(btf, &next_type_id);
1341
1342 if (!btf_type_is_void(next_type) &&
1343 !btf_type_is_fwd(next_type) &&
1344 !btf_type_is_func_proto(next_type)) {
1345 btf_verifier_log_type(env, v->t, "Invalid type_id");
1346 return -EINVAL;
1347 }
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001348 }
1349
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001350 env_stack_pop_resolved(env, next_type_id, 0);
1351
1352 return 0;
1353}
1354
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07001355static void btf_modifier_seq_show(const struct btf *btf,
1356 const struct btf_type *t,
1357 u32 type_id, void *data,
1358 u8 bits_offset, struct seq_file *m)
1359{
1360 t = btf_type_id_resolve(btf, &type_id);
1361
1362 btf_type_ops(t)->seq_show(btf, t, type_id, data, bits_offset, m);
1363}
1364
1365static void btf_ptr_seq_show(const struct btf *btf, const struct btf_type *t,
1366 u32 type_id, void *data, u8 bits_offset,
1367 struct seq_file *m)
1368{
1369 /* It is a hashed value */
1370 seq_printf(m, "%p", *(void **)data);
1371}
1372
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001373static void btf_ref_type_log(struct btf_verifier_env *env,
1374 const struct btf_type *t)
1375{
1376 btf_verifier_log(env, "type_id=%u", t->type);
1377}
1378
1379static struct btf_kind_operations modifier_ops = {
1380 .check_meta = btf_ref_type_check_meta,
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001381 .resolve = btf_modifier_resolve,
Martin KaFai Lau179cde82018-04-18 15:55:59 -07001382 .check_member = btf_modifier_check_member,
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001383 .log_details = btf_ref_type_log,
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07001384 .seq_show = btf_modifier_seq_show,
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001385};
1386
1387static struct btf_kind_operations ptr_ops = {
1388 .check_meta = btf_ref_type_check_meta,
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001389 .resolve = btf_ptr_resolve,
Martin KaFai Lau179cde82018-04-18 15:55:59 -07001390 .check_member = btf_ptr_check_member,
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001391 .log_details = btf_ref_type_log,
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07001392 .seq_show = btf_ptr_seq_show,
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001393};
1394
Martin KaFai Lau81753832018-06-02 09:06:51 -07001395static s32 btf_fwd_check_meta(struct btf_verifier_env *env,
1396 const struct btf_type *t,
1397 u32 meta_left)
1398{
1399 if (btf_type_vlen(t)) {
1400 btf_verifier_log_type(env, t, "vlen != 0");
1401 return -EINVAL;
1402 }
1403
1404 if (t->type) {
1405 btf_verifier_log_type(env, t, "type != 0");
1406 return -EINVAL;
1407 }
1408
Yonghong Songeb04bbb2018-11-27 13:23:28 -08001409 /* fwd type must have a valid name */
1410 if (!t->name_off ||
1411 !btf_name_valid_identifier(env->btf, t->name_off)) {
1412 btf_verifier_log_type(env, t, "Invalid name");
1413 return -EINVAL;
1414 }
1415
Martin KaFai Lau81753832018-06-02 09:06:51 -07001416 btf_verifier_log_type(env, t, NULL);
1417
1418 return 0;
1419}
1420
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001421static struct btf_kind_operations fwd_ops = {
Martin KaFai Lau81753832018-06-02 09:06:51 -07001422 .check_meta = btf_fwd_check_meta,
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001423 .resolve = btf_df_resolve,
Martin KaFai Lau179cde82018-04-18 15:55:59 -07001424 .check_member = btf_df_check_member,
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001425 .log_details = btf_ref_type_log,
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07001426 .seq_show = btf_df_seq_show,
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001427};
1428
Martin KaFai Lau179cde82018-04-18 15:55:59 -07001429static int btf_array_check_member(struct btf_verifier_env *env,
1430 const struct btf_type *struct_type,
1431 const struct btf_member *member,
1432 const struct btf_type *member_type)
1433{
1434 u32 struct_bits_off = member->offset;
1435 u32 struct_size, bytes_offset;
1436 u32 array_type_id, array_size;
1437 struct btf *btf = env->btf;
1438
1439 if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
1440 btf_verifier_log_member(env, struct_type, member,
1441 "Member is not byte aligned");
1442 return -EINVAL;
1443 }
1444
1445 array_type_id = member->type;
1446 btf_type_id_size(btf, &array_type_id, &array_size);
1447 struct_size = struct_type->size;
1448 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
1449 if (struct_size - bytes_offset < array_size) {
1450 btf_verifier_log_member(env, struct_type, member,
1451 "Member exceeds struct_size");
1452 return -EINVAL;
1453 }
1454
1455 return 0;
1456}
1457
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001458static s32 btf_array_check_meta(struct btf_verifier_env *env,
1459 const struct btf_type *t,
1460 u32 meta_left)
1461{
1462 const struct btf_array *array = btf_type_array(t);
1463 u32 meta_needed = sizeof(*array);
1464
1465 if (meta_left < meta_needed) {
1466 btf_verifier_log_basic(env, t,
1467 "meta_left:%u meta_needed:%u",
1468 meta_left, meta_needed);
1469 return -EINVAL;
1470 }
1471
Yonghong Songeb04bbb2018-11-27 13:23:28 -08001472 /* array type should not have a name */
1473 if (t->name_off) {
1474 btf_verifier_log_type(env, t, "Invalid name");
1475 return -EINVAL;
1476 }
1477
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001478 if (btf_type_vlen(t)) {
1479 btf_verifier_log_type(env, t, "vlen != 0");
1480 return -EINVAL;
1481 }
1482
Martin KaFai Laub9308ae2018-06-02 09:06:50 -07001483 if (t->size) {
1484 btf_verifier_log_type(env, t, "size != 0");
1485 return -EINVAL;
1486 }
1487
Martin KaFai Lau4ef5f572018-05-22 14:57:19 -07001488 /* Array elem type and index type cannot be in type void,
1489 * so !array->type and !array->index_type are not allowed.
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001490 */
Martin KaFai Lauaea2f7b82018-05-22 14:57:20 -07001491 if (!array->type || !BTF_TYPE_ID_VALID(array->type)) {
Martin KaFai Lau4ef5f572018-05-22 14:57:19 -07001492 btf_verifier_log_type(env, t, "Invalid elem");
1493 return -EINVAL;
1494 }
1495
Martin KaFai Lauaea2f7b82018-05-22 14:57:20 -07001496 if (!array->index_type || !BTF_TYPE_ID_VALID(array->index_type)) {
Martin KaFai Lau4ef5f572018-05-22 14:57:19 -07001497 btf_verifier_log_type(env, t, "Invalid index");
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001498 return -EINVAL;
1499 }
1500
1501 btf_verifier_log_type(env, t, NULL);
1502
1503 return meta_needed;
1504}
1505
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001506static int btf_array_resolve(struct btf_verifier_env *env,
1507 const struct resolve_vertex *v)
1508{
1509 const struct btf_array *array = btf_type_array(v->t);
Martin KaFai Lau4ef5f572018-05-22 14:57:19 -07001510 const struct btf_type *elem_type, *index_type;
1511 u32 elem_type_id, index_type_id;
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001512 struct btf *btf = env->btf;
1513 u32 elem_size;
1514
Martin KaFai Lau4ef5f572018-05-22 14:57:19 -07001515 /* Check array->index_type */
1516 index_type_id = array->index_type;
1517 index_type = btf_type_by_id(btf, index_type_id);
Martin KaFai Laub47a0bd2018-11-19 15:29:06 -08001518 if (btf_type_nosize_or_null(index_type)) {
Martin KaFai Lau4ef5f572018-05-22 14:57:19 -07001519 btf_verifier_log_type(env, v->t, "Invalid index");
1520 return -EINVAL;
1521 }
1522
1523 if (!env_type_is_resolve_sink(env, index_type) &&
1524 !env_type_is_resolved(env, index_type_id))
1525 return env_stack_push(env, index_type, index_type_id);
1526
1527 index_type = btf_type_id_size(btf, &index_type_id, NULL);
1528 if (!index_type || !btf_type_is_int(index_type) ||
1529 !btf_type_int_is_regular(index_type)) {
1530 btf_verifier_log_type(env, v->t, "Invalid index");
1531 return -EINVAL;
1532 }
1533
1534 /* Check array->type */
1535 elem_type_id = array->type;
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001536 elem_type = btf_type_by_id(btf, elem_type_id);
Martin KaFai Laub47a0bd2018-11-19 15:29:06 -08001537 if (btf_type_nosize_or_null(elem_type)) {
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001538 btf_verifier_log_type(env, v->t,
1539 "Invalid elem");
1540 return -EINVAL;
1541 }
1542
1543 if (!env_type_is_resolve_sink(env, elem_type) &&
1544 !env_type_is_resolved(env, elem_type_id))
1545 return env_stack_push(env, elem_type, elem_type_id);
1546
1547 elem_type = btf_type_id_size(btf, &elem_type_id, &elem_size);
1548 if (!elem_type) {
1549 btf_verifier_log_type(env, v->t, "Invalid elem");
1550 return -EINVAL;
1551 }
1552
Martin KaFai Lau4ef5f572018-05-22 14:57:19 -07001553 if (btf_type_is_int(elem_type) && !btf_type_int_is_regular(elem_type)) {
1554 btf_verifier_log_type(env, v->t, "Invalid array of int");
1555 return -EINVAL;
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001556 }
1557
1558 if (array->nelems && elem_size > U32_MAX / array->nelems) {
1559 btf_verifier_log_type(env, v->t,
1560 "Array size overflows U32_MAX");
1561 return -EINVAL;
1562 }
1563
1564 env_stack_pop_resolved(env, elem_type_id, elem_size * array->nelems);
1565
1566 return 0;
1567}
1568
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001569static void btf_array_log(struct btf_verifier_env *env,
1570 const struct btf_type *t)
1571{
1572 const struct btf_array *array = btf_type_array(t);
1573
1574 btf_verifier_log(env, "type_id=%u index_type_id=%u nr_elems=%u",
1575 array->type, array->index_type, array->nelems);
1576}
1577
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07001578static void btf_array_seq_show(const struct btf *btf, const struct btf_type *t,
1579 u32 type_id, void *data, u8 bits_offset,
1580 struct seq_file *m)
1581{
1582 const struct btf_array *array = btf_type_array(t);
1583 const struct btf_kind_operations *elem_ops;
1584 const struct btf_type *elem_type;
1585 u32 i, elem_size, elem_type_id;
1586
1587 elem_type_id = array->type;
1588 elem_type = btf_type_id_size(btf, &elem_type_id, &elem_size);
1589 elem_ops = btf_type_ops(elem_type);
1590 seq_puts(m, "[");
1591 for (i = 0; i < array->nelems; i++) {
1592 if (i)
1593 seq_puts(m, ",");
1594
1595 elem_ops->seq_show(btf, elem_type, elem_type_id, data,
1596 bits_offset, m);
1597 data += elem_size;
1598 }
1599 seq_puts(m, "]");
1600}
1601
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001602static struct btf_kind_operations array_ops = {
1603 .check_meta = btf_array_check_meta,
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001604 .resolve = btf_array_resolve,
Martin KaFai Lau179cde82018-04-18 15:55:59 -07001605 .check_member = btf_array_check_member,
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001606 .log_details = btf_array_log,
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07001607 .seq_show = btf_array_seq_show,
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001608};
1609
Martin KaFai Lau179cde82018-04-18 15:55:59 -07001610static int btf_struct_check_member(struct btf_verifier_env *env,
1611 const struct btf_type *struct_type,
1612 const struct btf_member *member,
1613 const struct btf_type *member_type)
1614{
1615 u32 struct_bits_off = member->offset;
1616 u32 struct_size, bytes_offset;
1617
1618 if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
1619 btf_verifier_log_member(env, struct_type, member,
1620 "Member is not byte aligned");
1621 return -EINVAL;
1622 }
1623
1624 struct_size = struct_type->size;
1625 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
1626 if (struct_size - bytes_offset < member_type->size) {
1627 btf_verifier_log_member(env, struct_type, member,
1628 "Member exceeds struct_size");
1629 return -EINVAL;
1630 }
1631
1632 return 0;
1633}
1634
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001635static s32 btf_struct_check_meta(struct btf_verifier_env *env,
1636 const struct btf_type *t,
1637 u32 meta_left)
1638{
1639 bool is_union = BTF_INFO_KIND(t->info) == BTF_KIND_UNION;
1640 const struct btf_member *member;
Martin KaFai Lau6283fa32018-07-20 17:38:37 -07001641 u32 meta_needed, last_offset;
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001642 struct btf *btf = env->btf;
1643 u32 struct_size = t->size;
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001644 u16 i;
1645
1646 meta_needed = btf_type_vlen(t) * sizeof(*member);
1647 if (meta_left < meta_needed) {
1648 btf_verifier_log_basic(env, t,
1649 "meta_left:%u meta_needed:%u",
1650 meta_left, meta_needed);
1651 return -EINVAL;
1652 }
1653
Yonghong Songeb04bbb2018-11-27 13:23:28 -08001654 /* struct type either no name or a valid one */
1655 if (t->name_off &&
1656 !btf_name_valid_identifier(env->btf, t->name_off)) {
1657 btf_verifier_log_type(env, t, "Invalid name");
1658 return -EINVAL;
1659 }
1660
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001661 btf_verifier_log_type(env, t, NULL);
1662
Martin KaFai Lau6283fa32018-07-20 17:38:37 -07001663 last_offset = 0;
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001664 for_each_member(i, t, member) {
Martin KaFai Laufbcf93e2018-04-21 09:48:23 -07001665 if (!btf_name_offset_valid(btf, member->name_off)) {
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001666 btf_verifier_log_member(env, t, member,
1667 "Invalid member name_offset:%u",
Martin KaFai Laufbcf93e2018-04-21 09:48:23 -07001668 member->name_off);
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001669 return -EINVAL;
1670 }
1671
Yonghong Songeb04bbb2018-11-27 13:23:28 -08001672 /* struct member either no name or a valid one */
1673 if (member->name_off &&
1674 !btf_name_valid_identifier(btf, member->name_off)) {
1675 btf_verifier_log_member(env, t, member, "Invalid name");
1676 return -EINVAL;
1677 }
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001678 /* A member cannot be in type void */
Martin KaFai Lauaea2f7b82018-05-22 14:57:20 -07001679 if (!member->type || !BTF_TYPE_ID_VALID(member->type)) {
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001680 btf_verifier_log_member(env, t, member,
1681 "Invalid type_id");
1682 return -EINVAL;
1683 }
1684
1685 if (is_union && member->offset) {
1686 btf_verifier_log_member(env, t, member,
1687 "Invalid member bits_offset");
1688 return -EINVAL;
1689 }
1690
Martin KaFai Lau6283fa32018-07-20 17:38:37 -07001691 /*
1692 * ">" instead of ">=" because the last member could be
1693 * "char a[0];"
1694 */
1695 if (last_offset > member->offset) {
1696 btf_verifier_log_member(env, t, member,
1697 "Invalid member bits_offset");
1698 return -EINVAL;
1699 }
1700
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001701 if (BITS_ROUNDUP_BYTES(member->offset) > struct_size) {
1702 btf_verifier_log_member(env, t, member,
Colin Ian King311fe1a2018-11-25 23:32:51 +00001703 "Member bits_offset exceeds its struct size");
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001704 return -EINVAL;
1705 }
1706
1707 btf_verifier_log_member(env, t, member, NULL);
Martin KaFai Lau6283fa32018-07-20 17:38:37 -07001708 last_offset = member->offset;
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001709 }
1710
1711 return meta_needed;
1712}
1713
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001714static int btf_struct_resolve(struct btf_verifier_env *env,
1715 const struct resolve_vertex *v)
1716{
1717 const struct btf_member *member;
Martin KaFai Lau179cde82018-04-18 15:55:59 -07001718 int err;
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001719 u16 i;
1720
1721 /* Before continue resolving the next_member,
1722 * ensure the last member is indeed resolved to a
1723 * type with size info.
1724 */
1725 if (v->next_member) {
Martin KaFai Lau179cde82018-04-18 15:55:59 -07001726 const struct btf_type *last_member_type;
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001727 const struct btf_member *last_member;
1728 u16 last_member_type_id;
1729
1730 last_member = btf_type_member(v->t) + v->next_member - 1;
1731 last_member_type_id = last_member->type;
1732 if (WARN_ON_ONCE(!env_type_is_resolved(env,
1733 last_member_type_id)))
1734 return -EINVAL;
Martin KaFai Lau179cde82018-04-18 15:55:59 -07001735
1736 last_member_type = btf_type_by_id(env->btf,
1737 last_member_type_id);
1738 err = btf_type_ops(last_member_type)->check_member(env, v->t,
1739 last_member,
1740 last_member_type);
1741 if (err)
1742 return err;
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001743 }
1744
1745 for_each_member_from(i, v->next_member, v->t, member) {
1746 u32 member_type_id = member->type;
1747 const struct btf_type *member_type = btf_type_by_id(env->btf,
1748 member_type_id);
1749
Martin KaFai Laub47a0bd2018-11-19 15:29:06 -08001750 if (btf_type_nosize_or_null(member_type)) {
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001751 btf_verifier_log_member(env, v->t, member,
1752 "Invalid member");
1753 return -EINVAL;
1754 }
1755
1756 if (!env_type_is_resolve_sink(env, member_type) &&
1757 !env_type_is_resolved(env, member_type_id)) {
1758 env_stack_set_next_member(env, i + 1);
1759 return env_stack_push(env, member_type, member_type_id);
1760 }
Martin KaFai Lau179cde82018-04-18 15:55:59 -07001761
1762 err = btf_type_ops(member_type)->check_member(env, v->t,
1763 member,
1764 member_type);
1765 if (err)
1766 return err;
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001767 }
1768
1769 env_stack_pop_resolved(env, 0, 0);
1770
1771 return 0;
1772}
1773
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001774static void btf_struct_log(struct btf_verifier_env *env,
1775 const struct btf_type *t)
1776{
1777 btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t));
1778}
1779
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07001780static void btf_struct_seq_show(const struct btf *btf, const struct btf_type *t,
1781 u32 type_id, void *data, u8 bits_offset,
1782 struct seq_file *m)
1783{
1784 const char *seq = BTF_INFO_KIND(t->info) == BTF_KIND_UNION ? "|" : ",";
1785 const struct btf_member *member;
1786 u32 i;
1787
1788 seq_puts(m, "{");
1789 for_each_member(i, t, member) {
1790 const struct btf_type *member_type = btf_type_by_id(btf,
1791 member->type);
1792 u32 member_offset = member->offset;
1793 u32 bytes_offset = BITS_ROUNDDOWN_BYTES(member_offset);
1794 u8 bits8_offset = BITS_PER_BYTE_MASKED(member_offset);
1795 const struct btf_kind_operations *ops;
1796
1797 if (i)
1798 seq_puts(m, seq);
1799
1800 ops = btf_type_ops(member_type);
1801 ops->seq_show(btf, member_type, member->type,
1802 data + bytes_offset, bits8_offset, m);
1803 }
1804 seq_puts(m, "}");
1805}
1806
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001807static struct btf_kind_operations struct_ops = {
1808 .check_meta = btf_struct_check_meta,
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001809 .resolve = btf_struct_resolve,
Martin KaFai Lau179cde82018-04-18 15:55:59 -07001810 .check_member = btf_struct_check_member,
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001811 .log_details = btf_struct_log,
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07001812 .seq_show = btf_struct_seq_show,
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001813};
1814
Martin KaFai Lau179cde82018-04-18 15:55:59 -07001815static int btf_enum_check_member(struct btf_verifier_env *env,
1816 const struct btf_type *struct_type,
1817 const struct btf_member *member,
1818 const struct btf_type *member_type)
1819{
1820 u32 struct_bits_off = member->offset;
1821 u32 struct_size, bytes_offset;
1822
1823 if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
1824 btf_verifier_log_member(env, struct_type, member,
1825 "Member is not byte aligned");
1826 return -EINVAL;
1827 }
1828
1829 struct_size = struct_type->size;
1830 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
1831 if (struct_size - bytes_offset < sizeof(int)) {
1832 btf_verifier_log_member(env, struct_type, member,
1833 "Member exceeds struct_size");
1834 return -EINVAL;
1835 }
1836
1837 return 0;
1838}
1839
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001840static s32 btf_enum_check_meta(struct btf_verifier_env *env,
1841 const struct btf_type *t,
1842 u32 meta_left)
1843{
1844 const struct btf_enum *enums = btf_type_enum(t);
1845 struct btf *btf = env->btf;
1846 u16 i, nr_enums;
1847 u32 meta_needed;
1848
1849 nr_enums = btf_type_vlen(t);
1850 meta_needed = nr_enums * sizeof(*enums);
1851
1852 if (meta_left < meta_needed) {
1853 btf_verifier_log_basic(env, t,
1854 "meta_left:%u meta_needed:%u",
1855 meta_left, meta_needed);
1856 return -EINVAL;
1857 }
1858
1859 if (t->size != sizeof(int)) {
1860 btf_verifier_log_type(env, t, "Expected size:%zu",
1861 sizeof(int));
1862 return -EINVAL;
1863 }
1864
Yonghong Songeb04bbb2018-11-27 13:23:28 -08001865 /* enum type either no name or a valid one */
1866 if (t->name_off &&
1867 !btf_name_valid_identifier(env->btf, t->name_off)) {
1868 btf_verifier_log_type(env, t, "Invalid name");
1869 return -EINVAL;
1870 }
1871
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001872 btf_verifier_log_type(env, t, NULL);
1873
1874 for (i = 0; i < nr_enums; i++) {
Martin KaFai Laufbcf93e2018-04-21 09:48:23 -07001875 if (!btf_name_offset_valid(btf, enums[i].name_off)) {
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001876 btf_verifier_log(env, "\tInvalid name_offset:%u",
Martin KaFai Laufbcf93e2018-04-21 09:48:23 -07001877 enums[i].name_off);
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001878 return -EINVAL;
1879 }
1880
Yonghong Songeb04bbb2018-11-27 13:23:28 -08001881 /* enum member must have a valid name */
1882 if (!enums[i].name_off ||
1883 !btf_name_valid_identifier(btf, enums[i].name_off)) {
1884 btf_verifier_log_type(env, t, "Invalid name");
1885 return -EINVAL;
1886 }
1887
1888
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001889 btf_verifier_log(env, "\t%s val=%d\n",
Martin KaFai Lau23127b32018-12-13 10:41:46 -08001890 __btf_name_by_offset(btf, enums[i].name_off),
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001891 enums[i].val);
1892 }
1893
1894 return meta_needed;
1895}
1896
1897static void btf_enum_log(struct btf_verifier_env *env,
1898 const struct btf_type *t)
1899{
1900 btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t));
1901}
1902
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07001903static void btf_enum_seq_show(const struct btf *btf, const struct btf_type *t,
1904 u32 type_id, void *data, u8 bits_offset,
1905 struct seq_file *m)
1906{
1907 const struct btf_enum *enums = btf_type_enum(t);
1908 u32 i, nr_enums = btf_type_vlen(t);
1909 int v = *(int *)data;
1910
1911 for (i = 0; i < nr_enums; i++) {
1912 if (v == enums[i].val) {
1913 seq_printf(m, "%s",
Martin KaFai Lau23127b32018-12-13 10:41:46 -08001914 __btf_name_by_offset(btf,
1915 enums[i].name_off));
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07001916 return;
1917 }
1918 }
1919
1920 seq_printf(m, "%d", v);
1921}
1922
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001923static struct btf_kind_operations enum_ops = {
1924 .check_meta = btf_enum_check_meta,
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001925 .resolve = btf_df_resolve,
Martin KaFai Lau179cde82018-04-18 15:55:59 -07001926 .check_member = btf_enum_check_member,
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001927 .log_details = btf_enum_log,
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07001928 .seq_show = btf_enum_seq_show,
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001929};
1930
Martin KaFai Lau2667a262018-11-19 15:29:08 -08001931static s32 btf_func_proto_check_meta(struct btf_verifier_env *env,
1932 const struct btf_type *t,
1933 u32 meta_left)
1934{
1935 u32 meta_needed = btf_type_vlen(t) * sizeof(struct btf_param);
1936
1937 if (meta_left < meta_needed) {
1938 btf_verifier_log_basic(env, t,
1939 "meta_left:%u meta_needed:%u",
1940 meta_left, meta_needed);
1941 return -EINVAL;
1942 }
1943
1944 if (t->name_off) {
1945 btf_verifier_log_type(env, t, "Invalid name");
1946 return -EINVAL;
1947 }
1948
1949 btf_verifier_log_type(env, t, NULL);
1950
1951 return meta_needed;
1952}
1953
1954static void btf_func_proto_log(struct btf_verifier_env *env,
1955 const struct btf_type *t)
1956{
1957 const struct btf_param *args = (const struct btf_param *)(t + 1);
1958 u16 nr_args = btf_type_vlen(t), i;
1959
1960 btf_verifier_log(env, "return=%u args=(", t->type);
1961 if (!nr_args) {
1962 btf_verifier_log(env, "void");
1963 goto done;
1964 }
1965
1966 if (nr_args == 1 && !args[0].type) {
1967 /* Only one vararg */
1968 btf_verifier_log(env, "vararg");
1969 goto done;
1970 }
1971
1972 btf_verifier_log(env, "%u %s", args[0].type,
Martin KaFai Lau23127b32018-12-13 10:41:46 -08001973 __btf_name_by_offset(env->btf,
1974 args[0].name_off));
Martin KaFai Lau2667a262018-11-19 15:29:08 -08001975 for (i = 1; i < nr_args - 1; i++)
1976 btf_verifier_log(env, ", %u %s", args[i].type,
Martin KaFai Lau23127b32018-12-13 10:41:46 -08001977 __btf_name_by_offset(env->btf,
1978 args[i].name_off));
Martin KaFai Lau2667a262018-11-19 15:29:08 -08001979
1980 if (nr_args > 1) {
1981 const struct btf_param *last_arg = &args[nr_args - 1];
1982
1983 if (last_arg->type)
1984 btf_verifier_log(env, ", %u %s", last_arg->type,
Martin KaFai Lau23127b32018-12-13 10:41:46 -08001985 __btf_name_by_offset(env->btf,
1986 last_arg->name_off));
Martin KaFai Lau2667a262018-11-19 15:29:08 -08001987 else
1988 btf_verifier_log(env, ", vararg");
1989 }
1990
1991done:
1992 btf_verifier_log(env, ")");
1993}
1994
1995static struct btf_kind_operations func_proto_ops = {
1996 .check_meta = btf_func_proto_check_meta,
1997 .resolve = btf_df_resolve,
1998 /*
1999 * BTF_KIND_FUNC_PROTO cannot be directly referred by
2000 * a struct's member.
2001 *
2002 * It should be a funciton pointer instead.
2003 * (i.e. struct's member -> BTF_KIND_PTR -> BTF_KIND_FUNC_PROTO)
2004 *
2005 * Hence, there is no btf_func_check_member().
2006 */
2007 .check_member = btf_df_check_member,
2008 .log_details = btf_func_proto_log,
2009 .seq_show = btf_df_seq_show,
2010};
2011
2012static s32 btf_func_check_meta(struct btf_verifier_env *env,
2013 const struct btf_type *t,
2014 u32 meta_left)
2015{
2016 if (!t->name_off ||
2017 !btf_name_valid_identifier(env->btf, t->name_off)) {
2018 btf_verifier_log_type(env, t, "Invalid name");
2019 return -EINVAL;
2020 }
2021
2022 if (btf_type_vlen(t)) {
2023 btf_verifier_log_type(env, t, "vlen != 0");
2024 return -EINVAL;
2025 }
2026
2027 btf_verifier_log_type(env, t, NULL);
2028
2029 return 0;
2030}
2031
2032static struct btf_kind_operations func_ops = {
2033 .check_meta = btf_func_check_meta,
2034 .resolve = btf_df_resolve,
2035 .check_member = btf_df_check_member,
2036 .log_details = btf_ref_type_log,
2037 .seq_show = btf_df_seq_show,
2038};
2039
2040static int btf_func_proto_check(struct btf_verifier_env *env,
2041 const struct btf_type *t)
2042{
2043 const struct btf_type *ret_type;
2044 const struct btf_param *args;
2045 const struct btf *btf;
2046 u16 nr_args, i;
2047 int err;
2048
2049 btf = env->btf;
2050 args = (const struct btf_param *)(t + 1);
2051 nr_args = btf_type_vlen(t);
2052
2053 /* Check func return type which could be "void" (t->type == 0) */
2054 if (t->type) {
2055 u32 ret_type_id = t->type;
2056
2057 ret_type = btf_type_by_id(btf, ret_type_id);
2058 if (!ret_type) {
2059 btf_verifier_log_type(env, t, "Invalid return type");
2060 return -EINVAL;
2061 }
2062
2063 if (btf_type_needs_resolve(ret_type) &&
2064 !env_type_is_resolved(env, ret_type_id)) {
2065 err = btf_resolve(env, ret_type, ret_type_id);
2066 if (err)
2067 return err;
2068 }
2069
2070 /* Ensure the return type is a type that has a size */
2071 if (!btf_type_id_size(btf, &ret_type_id, NULL)) {
2072 btf_verifier_log_type(env, t, "Invalid return type");
2073 return -EINVAL;
2074 }
2075 }
2076
2077 if (!nr_args)
2078 return 0;
2079
2080 /* Last func arg type_id could be 0 if it is a vararg */
2081 if (!args[nr_args - 1].type) {
2082 if (args[nr_args - 1].name_off) {
2083 btf_verifier_log_type(env, t, "Invalid arg#%u",
2084 nr_args);
2085 return -EINVAL;
2086 }
2087 nr_args--;
2088 }
2089
2090 err = 0;
2091 for (i = 0; i < nr_args; i++) {
2092 const struct btf_type *arg_type;
2093 u32 arg_type_id;
2094
2095 arg_type_id = args[i].type;
2096 arg_type = btf_type_by_id(btf, arg_type_id);
2097 if (!arg_type) {
2098 btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1);
2099 err = -EINVAL;
2100 break;
2101 }
2102
2103 if (args[i].name_off &&
2104 (!btf_name_offset_valid(btf, args[i].name_off) ||
2105 !btf_name_valid_identifier(btf, args[i].name_off))) {
2106 btf_verifier_log_type(env, t,
2107 "Invalid arg#%u", i + 1);
2108 err = -EINVAL;
2109 break;
2110 }
2111
2112 if (btf_type_needs_resolve(arg_type) &&
2113 !env_type_is_resolved(env, arg_type_id)) {
2114 err = btf_resolve(env, arg_type, arg_type_id);
2115 if (err)
2116 break;
2117 }
2118
2119 if (!btf_type_id_size(btf, &arg_type_id, NULL)) {
2120 btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1);
2121 err = -EINVAL;
2122 break;
2123 }
2124 }
2125
2126 return err;
2127}
2128
2129static int btf_func_check(struct btf_verifier_env *env,
2130 const struct btf_type *t)
2131{
2132 const struct btf_type *proto_type;
2133 const struct btf_param *args;
2134 const struct btf *btf;
2135 u16 nr_args, i;
2136
2137 btf = env->btf;
2138 proto_type = btf_type_by_id(btf, t->type);
2139
2140 if (!proto_type || !btf_type_is_func_proto(proto_type)) {
2141 btf_verifier_log_type(env, t, "Invalid type_id");
2142 return -EINVAL;
2143 }
2144
2145 args = (const struct btf_param *)(proto_type + 1);
2146 nr_args = btf_type_vlen(proto_type);
2147 for (i = 0; i < nr_args; i++) {
2148 if (!args[i].name_off && args[i].type) {
2149 btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1);
2150 return -EINVAL;
2151 }
2152 }
2153
2154 return 0;
2155}
2156
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002157static const struct btf_kind_operations * const kind_ops[NR_BTF_KINDS] = {
2158 [BTF_KIND_INT] = &int_ops,
2159 [BTF_KIND_PTR] = &ptr_ops,
2160 [BTF_KIND_ARRAY] = &array_ops,
2161 [BTF_KIND_STRUCT] = &struct_ops,
2162 [BTF_KIND_UNION] = &struct_ops,
2163 [BTF_KIND_ENUM] = &enum_ops,
2164 [BTF_KIND_FWD] = &fwd_ops,
2165 [BTF_KIND_TYPEDEF] = &modifier_ops,
2166 [BTF_KIND_VOLATILE] = &modifier_ops,
2167 [BTF_KIND_CONST] = &modifier_ops,
2168 [BTF_KIND_RESTRICT] = &modifier_ops,
Martin KaFai Lau2667a262018-11-19 15:29:08 -08002169 [BTF_KIND_FUNC] = &func_ops,
2170 [BTF_KIND_FUNC_PROTO] = &func_proto_ops,
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002171};
2172
2173static s32 btf_check_meta(struct btf_verifier_env *env,
2174 const struct btf_type *t,
2175 u32 meta_left)
2176{
2177 u32 saved_meta_left = meta_left;
2178 s32 var_meta_size;
2179
2180 if (meta_left < sizeof(*t)) {
2181 btf_verifier_log(env, "[%u] meta_left:%u meta_needed:%zu",
2182 env->log_type_id, meta_left, sizeof(*t));
2183 return -EINVAL;
2184 }
2185 meta_left -= sizeof(*t);
2186
Martin KaFai Lauaea2f7b82018-05-22 14:57:20 -07002187 if (t->info & ~BTF_INFO_MASK) {
2188 btf_verifier_log(env, "[%u] Invalid btf_info:%x",
2189 env->log_type_id, t->info);
2190 return -EINVAL;
2191 }
2192
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002193 if (BTF_INFO_KIND(t->info) > BTF_KIND_MAX ||
2194 BTF_INFO_KIND(t->info) == BTF_KIND_UNKN) {
2195 btf_verifier_log(env, "[%u] Invalid kind:%u",
2196 env->log_type_id, BTF_INFO_KIND(t->info));
2197 return -EINVAL;
2198 }
2199
Martin KaFai Laufbcf93e2018-04-21 09:48:23 -07002200 if (!btf_name_offset_valid(env->btf, t->name_off)) {
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002201 btf_verifier_log(env, "[%u] Invalid name_offset:%u",
Martin KaFai Laufbcf93e2018-04-21 09:48:23 -07002202 env->log_type_id, t->name_off);
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002203 return -EINVAL;
2204 }
2205
2206 var_meta_size = btf_type_ops(t)->check_meta(env, t, meta_left);
2207 if (var_meta_size < 0)
2208 return var_meta_size;
2209
2210 meta_left -= var_meta_size;
2211
2212 return saved_meta_left - meta_left;
2213}
2214
2215static int btf_check_all_metas(struct btf_verifier_env *env)
2216{
2217 struct btf *btf = env->btf;
2218 struct btf_header *hdr;
2219 void *cur, *end;
2220
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07002221 hdr = &btf->hdr;
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002222 cur = btf->nohdr_data + hdr->type_off;
Martin KaFai Lau4b1c5d92018-09-12 10:29:11 -07002223 end = cur + hdr->type_len;
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002224
2225 env->log_type_id = 1;
2226 while (cur < end) {
2227 struct btf_type *t = cur;
2228 s32 meta_size;
2229
2230 meta_size = btf_check_meta(env, t, end - cur);
2231 if (meta_size < 0)
2232 return meta_size;
2233
2234 btf_add_type(env, t);
2235 cur += meta_size;
2236 env->log_type_id++;
2237 }
2238
2239 return 0;
2240}
2241
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07002242static bool btf_resolve_valid(struct btf_verifier_env *env,
2243 const struct btf_type *t,
2244 u32 type_id)
2245{
2246 struct btf *btf = env->btf;
2247
2248 if (!env_type_is_resolved(env, type_id))
2249 return false;
2250
2251 if (btf_type_is_struct(t))
2252 return !btf->resolved_ids[type_id] &&
2253 !btf->resolved_sizes[type_id];
2254
2255 if (btf_type_is_modifier(t) || btf_type_is_ptr(t)) {
2256 t = btf_type_id_resolve(btf, &type_id);
2257 return t && !btf_type_is_modifier(t);
2258 }
2259
2260 if (btf_type_is_array(t)) {
2261 const struct btf_array *array = btf_type_array(t);
2262 const struct btf_type *elem_type;
2263 u32 elem_type_id = array->type;
2264 u32 elem_size;
2265
2266 elem_type = btf_type_id_size(btf, &elem_type_id, &elem_size);
2267 return elem_type && !btf_type_is_modifier(elem_type) &&
2268 (array->nelems * elem_size ==
2269 btf->resolved_sizes[type_id]);
2270 }
2271
2272 return false;
2273}
2274
Martin KaFai Lau2667a262018-11-19 15:29:08 -08002275static int btf_resolve(struct btf_verifier_env *env,
2276 const struct btf_type *t, u32 type_id)
2277{
2278 u32 save_log_type_id = env->log_type_id;
2279 const struct resolve_vertex *v;
2280 int err = 0;
2281
2282 env->resolve_mode = RESOLVE_TBD;
2283 env_stack_push(env, t, type_id);
2284 while (!err && (v = env_stack_peak(env))) {
2285 env->log_type_id = v->type_id;
2286 err = btf_type_ops(v->t)->resolve(env, v);
2287 }
2288
2289 env->log_type_id = type_id;
2290 if (err == -E2BIG) {
2291 btf_verifier_log_type(env, t,
2292 "Exceeded max resolving depth:%u",
2293 MAX_RESOLVE_DEPTH);
2294 } else if (err == -EEXIST) {
2295 btf_verifier_log_type(env, t, "Loop detected");
2296 }
2297
2298 /* Final sanity check */
2299 if (!err && !btf_resolve_valid(env, t, type_id)) {
2300 btf_verifier_log_type(env, t, "Invalid resolve state");
2301 err = -EINVAL;
2302 }
2303
2304 env->log_type_id = save_log_type_id;
2305 return err;
2306}
2307
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07002308static int btf_check_all_types(struct btf_verifier_env *env)
2309{
2310 struct btf *btf = env->btf;
2311 u32 type_id;
2312 int err;
2313
2314 err = env_resolve_init(env);
2315 if (err)
2316 return err;
2317
2318 env->phase++;
2319 for (type_id = 1; type_id <= btf->nr_types; type_id++) {
2320 const struct btf_type *t = btf_type_by_id(btf, type_id);
2321
2322 env->log_type_id = type_id;
2323 if (btf_type_needs_resolve(t) &&
2324 !env_type_is_resolved(env, type_id)) {
2325 err = btf_resolve(env, t, type_id);
2326 if (err)
2327 return err;
2328 }
2329
Martin KaFai Lau2667a262018-11-19 15:29:08 -08002330 if (btf_type_is_func_proto(t)) {
2331 err = btf_func_proto_check(env, t);
2332 if (err)
2333 return err;
2334 }
2335
2336 if (btf_type_is_func(t)) {
2337 err = btf_func_check(env, t);
2338 if (err)
2339 return err;
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07002340 }
2341 }
2342
2343 return 0;
2344}
2345
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002346static int btf_parse_type_sec(struct btf_verifier_env *env)
2347{
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07002348 const struct btf_header *hdr = &env->btf->hdr;
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07002349 int err;
2350
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07002351 /* Type section must align to 4 bytes */
2352 if (hdr->type_off & (sizeof(u32) - 1)) {
2353 btf_verifier_log(env, "Unaligned type_off");
2354 return -EINVAL;
2355 }
2356
2357 if (!hdr->type_len) {
2358 btf_verifier_log(env, "No type found");
2359 return -EINVAL;
2360 }
2361
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07002362 err = btf_check_all_metas(env);
2363 if (err)
2364 return err;
2365
2366 return btf_check_all_types(env);
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002367}
2368
2369static int btf_parse_str_sec(struct btf_verifier_env *env)
2370{
2371 const struct btf_header *hdr;
2372 struct btf *btf = env->btf;
2373 const char *start, *end;
2374
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07002375 hdr = &btf->hdr;
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002376 start = btf->nohdr_data + hdr->str_off;
2377 end = start + hdr->str_len;
2378
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07002379 if (end != btf->data + btf->data_size) {
2380 btf_verifier_log(env, "String section is not at the end");
2381 return -EINVAL;
2382 }
2383
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002384 if (!hdr->str_len || hdr->str_len - 1 > BTF_MAX_NAME_OFFSET ||
2385 start[0] || end[-1]) {
2386 btf_verifier_log(env, "Invalid string section");
2387 return -EINVAL;
2388 }
2389
2390 btf->strings = start;
2391
2392 return 0;
2393}
2394
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07002395static const size_t btf_sec_info_offset[] = {
2396 offsetof(struct btf_header, type_off),
2397 offsetof(struct btf_header, str_off),
2398};
2399
2400static int btf_sec_info_cmp(const void *a, const void *b)
2401{
2402 const struct btf_sec_info *x = a;
2403 const struct btf_sec_info *y = b;
2404
2405 return (int)(x->off - y->off) ? : (int)(x->len - y->len);
2406}
2407
2408static int btf_check_sec_info(struct btf_verifier_env *env,
2409 u32 btf_data_size)
2410{
Martin KaFai Laua2889a42018-05-23 11:32:36 -07002411 struct btf_sec_info secs[ARRAY_SIZE(btf_sec_info_offset)];
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07002412 u32 total, expected_total, i;
2413 const struct btf_header *hdr;
2414 const struct btf *btf;
2415
2416 btf = env->btf;
2417 hdr = &btf->hdr;
2418
2419 /* Populate the secs from hdr */
Martin KaFai Laua2889a42018-05-23 11:32:36 -07002420 for (i = 0; i < ARRAY_SIZE(btf_sec_info_offset); i++)
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07002421 secs[i] = *(struct btf_sec_info *)((void *)hdr +
2422 btf_sec_info_offset[i]);
2423
Martin KaFai Laua2889a42018-05-23 11:32:36 -07002424 sort(secs, ARRAY_SIZE(btf_sec_info_offset),
2425 sizeof(struct btf_sec_info), btf_sec_info_cmp, NULL);
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07002426
2427 /* Check for gaps and overlap among sections */
2428 total = 0;
2429 expected_total = btf_data_size - hdr->hdr_len;
Martin KaFai Laua2889a42018-05-23 11:32:36 -07002430 for (i = 0; i < ARRAY_SIZE(btf_sec_info_offset); i++) {
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07002431 if (expected_total < secs[i].off) {
2432 btf_verifier_log(env, "Invalid section offset");
2433 return -EINVAL;
2434 }
2435 if (total < secs[i].off) {
2436 /* gap */
2437 btf_verifier_log(env, "Unsupported section found");
2438 return -EINVAL;
2439 }
2440 if (total > secs[i].off) {
2441 btf_verifier_log(env, "Section overlap found");
2442 return -EINVAL;
2443 }
2444 if (expected_total - total < secs[i].len) {
2445 btf_verifier_log(env,
2446 "Total section length too long");
2447 return -EINVAL;
2448 }
2449 total += secs[i].len;
2450 }
2451
2452 /* There is data other than hdr and known sections */
2453 if (expected_total != total) {
2454 btf_verifier_log(env, "Unsupported section found");
2455 return -EINVAL;
2456 }
2457
2458 return 0;
2459}
2460
Martin Lau4a6998a2018-10-24 20:42:25 +00002461static int btf_parse_hdr(struct btf_verifier_env *env)
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002462{
Martin Lau4a6998a2018-10-24 20:42:25 +00002463 u32 hdr_len, hdr_copy, btf_data_size;
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002464 const struct btf_header *hdr;
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07002465 struct btf *btf;
2466 int err;
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002467
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07002468 btf = env->btf;
Martin Lau4a6998a2018-10-24 20:42:25 +00002469 btf_data_size = btf->data_size;
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07002470
Martin Lau4a6998a2018-10-24 20:42:25 +00002471 if (btf_data_size <
2472 offsetof(struct btf_header, hdr_len) + sizeof(hdr->hdr_len)) {
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07002473 btf_verifier_log(env, "hdr_len not found");
2474 return -EINVAL;
2475 }
2476
Martin Lau4a6998a2018-10-24 20:42:25 +00002477 hdr = btf->data;
2478 hdr_len = hdr->hdr_len;
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07002479 if (btf_data_size < hdr_len) {
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002480 btf_verifier_log(env, "btf_header not found");
2481 return -EINVAL;
2482 }
2483
Martin Lau4a6998a2018-10-24 20:42:25 +00002484 /* Ensure the unsupported header fields are zero */
2485 if (hdr_len > sizeof(btf->hdr)) {
2486 u8 *expected_zero = btf->data + sizeof(btf->hdr);
2487 u8 *end = btf->data + hdr_len;
2488
2489 for (; expected_zero < end; expected_zero++) {
2490 if (*expected_zero) {
2491 btf_verifier_log(env, "Unsupported btf_header");
2492 return -E2BIG;
2493 }
2494 }
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07002495 }
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002496
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07002497 hdr_copy = min_t(u32, hdr_len, sizeof(btf->hdr));
Martin Lau4a6998a2018-10-24 20:42:25 +00002498 memcpy(&btf->hdr, btf->data, hdr_copy);
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07002499
2500 hdr = &btf->hdr;
2501
2502 btf_verifier_log_hdr(env, btf_data_size);
2503
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002504 if (hdr->magic != BTF_MAGIC) {
2505 btf_verifier_log(env, "Invalid magic");
2506 return -EINVAL;
2507 }
2508
2509 if (hdr->version != BTF_VERSION) {
2510 btf_verifier_log(env, "Unsupported version");
2511 return -ENOTSUPP;
2512 }
2513
2514 if (hdr->flags) {
2515 btf_verifier_log(env, "Unsupported flags");
2516 return -ENOTSUPP;
2517 }
2518
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07002519 if (btf_data_size == hdr->hdr_len) {
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002520 btf_verifier_log(env, "No data");
2521 return -EINVAL;
2522 }
2523
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07002524 err = btf_check_sec_info(env, btf_data_size);
2525 if (err)
2526 return err;
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002527
2528 return 0;
2529}
2530
2531static struct btf *btf_parse(void __user *btf_data, u32 btf_data_size,
2532 u32 log_level, char __user *log_ubuf, u32 log_size)
2533{
2534 struct btf_verifier_env *env = NULL;
2535 struct bpf_verifier_log *log;
2536 struct btf *btf = NULL;
2537 u8 *data;
2538 int err;
2539
2540 if (btf_data_size > BTF_MAX_SIZE)
2541 return ERR_PTR(-E2BIG);
2542
2543 env = kzalloc(sizeof(*env), GFP_KERNEL | __GFP_NOWARN);
2544 if (!env)
2545 return ERR_PTR(-ENOMEM);
2546
2547 log = &env->log;
2548 if (log_level || log_ubuf || log_size) {
2549 /* user requested verbose verifier output
2550 * and supplied buffer to store the verification trace
2551 */
2552 log->level = log_level;
2553 log->ubuf = log_ubuf;
2554 log->len_total = log_size;
2555
2556 /* log attributes have to be sane */
2557 if (log->len_total < 128 || log->len_total > UINT_MAX >> 8 ||
2558 !log->level || !log->ubuf) {
2559 err = -EINVAL;
2560 goto errout;
2561 }
2562 }
2563
2564 btf = kzalloc(sizeof(*btf), GFP_KERNEL | __GFP_NOWARN);
2565 if (!btf) {
2566 err = -ENOMEM;
2567 goto errout;
2568 }
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07002569 env->btf = btf;
2570
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002571 data = kvmalloc(btf_data_size, GFP_KERNEL | __GFP_NOWARN);
2572 if (!data) {
2573 err = -ENOMEM;
2574 goto errout;
2575 }
2576
2577 btf->data = data;
2578 btf->data_size = btf_data_size;
2579
2580 if (copy_from_user(data, btf_data, btf_data_size)) {
2581 err = -EFAULT;
2582 goto errout;
2583 }
2584
Martin Lau4a6998a2018-10-24 20:42:25 +00002585 err = btf_parse_hdr(env);
2586 if (err)
2587 goto errout;
2588
2589 btf->nohdr_data = btf->data + btf->hdr.hdr_len;
2590
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002591 err = btf_parse_str_sec(env);
2592 if (err)
2593 goto errout;
2594
2595 err = btf_parse_type_sec(env);
2596 if (err)
2597 goto errout;
2598
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07002599 if (log->level && bpf_verifier_log_full(log)) {
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002600 err = -ENOSPC;
2601 goto errout;
2602 }
2603
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07002604 btf_verifier_env_free(env);
2605 refcount_set(&btf->refcnt, 1);
2606 return btf;
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002607
2608errout:
2609 btf_verifier_env_free(env);
2610 if (btf)
2611 btf_free(btf);
2612 return ERR_PTR(err);
2613}
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07002614
2615void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj,
2616 struct seq_file *m)
2617{
2618 const struct btf_type *t = btf_type_by_id(btf, type_id);
2619
2620 btf_type_ops(t)->seq_show(btf, t, type_id, obj, 0, m);
2621}
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07002622
2623static int btf_release(struct inode *inode, struct file *filp)
2624{
2625 btf_put(filp->private_data);
2626 return 0;
2627}
2628
Martin KaFai Lau60197cf2018-04-18 15:56:02 -07002629const struct file_operations btf_fops = {
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07002630 .release = btf_release,
2631};
2632
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07002633static int __btf_new_fd(struct btf *btf)
2634{
2635 return anon_inode_getfd("btf", &btf_fops, btf, O_RDONLY | O_CLOEXEC);
2636}
2637
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07002638int btf_new_fd(const union bpf_attr *attr)
2639{
2640 struct btf *btf;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07002641 int ret;
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07002642
2643 btf = btf_parse(u64_to_user_ptr(attr->btf),
2644 attr->btf_size, attr->btf_log_level,
2645 u64_to_user_ptr(attr->btf_log_buf),
2646 attr->btf_log_size);
2647 if (IS_ERR(btf))
2648 return PTR_ERR(btf);
2649
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07002650 ret = btf_alloc_id(btf);
2651 if (ret) {
2652 btf_free(btf);
2653 return ret;
2654 }
2655
2656 /*
2657 * The BTF ID is published to the userspace.
2658 * All BTF free must go through call_rcu() from
2659 * now on (i.e. free by calling btf_put()).
2660 */
2661
2662 ret = __btf_new_fd(btf);
2663 if (ret < 0)
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07002664 btf_put(btf);
2665
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07002666 return ret;
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07002667}
2668
2669struct btf *btf_get_by_fd(int fd)
2670{
2671 struct btf *btf;
2672 struct fd f;
2673
2674 f = fdget(fd);
2675
2676 if (!f.file)
2677 return ERR_PTR(-EBADF);
2678
2679 if (f.file->f_op != &btf_fops) {
2680 fdput(f);
2681 return ERR_PTR(-EINVAL);
2682 }
2683
2684 btf = f.file->private_data;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07002685 refcount_inc(&btf->refcnt);
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07002686 fdput(f);
2687
2688 return btf;
2689}
Martin KaFai Lau60197cf2018-04-18 15:56:02 -07002690
2691int btf_get_info_by_fd(const struct btf *btf,
2692 const union bpf_attr *attr,
2693 union bpf_attr __user *uattr)
2694{
Martin KaFai Lau62dab842018-05-04 14:49:52 -07002695 struct bpf_btf_info __user *uinfo;
2696 struct bpf_btf_info info = {};
2697 u32 info_copy, btf_copy;
2698 void __user *ubtf;
2699 u32 uinfo_len;
Martin KaFai Lau60197cf2018-04-18 15:56:02 -07002700
Martin KaFai Lau62dab842018-05-04 14:49:52 -07002701 uinfo = u64_to_user_ptr(attr->info.info);
2702 uinfo_len = attr->info.info_len;
2703
2704 info_copy = min_t(u32, uinfo_len, sizeof(info));
2705 if (copy_from_user(&info, uinfo, info_copy))
2706 return -EFAULT;
2707
2708 info.id = btf->id;
2709 ubtf = u64_to_user_ptr(info.btf);
2710 btf_copy = min_t(u32, btf->data_size, info.btf_size);
2711 if (copy_to_user(ubtf, btf->data, btf_copy))
2712 return -EFAULT;
2713 info.btf_size = btf->data_size;
2714
2715 if (copy_to_user(uinfo, &info, info_copy) ||
2716 put_user(info_copy, &uattr->info.info_len))
Martin KaFai Lau60197cf2018-04-18 15:56:02 -07002717 return -EFAULT;
2718
2719 return 0;
2720}
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07002721
2722int btf_get_fd_by_id(u32 id)
2723{
2724 struct btf *btf;
2725 int fd;
2726
2727 rcu_read_lock();
2728 btf = idr_find(&btf_idr, id);
2729 if (!btf || !refcount_inc_not_zero(&btf->refcnt))
2730 btf = ERR_PTR(-ENOENT);
2731 rcu_read_unlock();
2732
2733 if (IS_ERR(btf))
2734 return PTR_ERR(btf);
2735
2736 fd = __btf_new_fd(btf);
2737 if (fd < 0)
2738 btf_put(btf);
2739
2740 return fd;
2741}
2742
2743u32 btf_id(const struct btf *btf)
2744{
2745 return btf->id;
2746}