blob: e804b26a0506a64e75ab36f38f9b063e8c6a9041 [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
Yonghong Song9d5f9f72018-12-15 22:13:51 -0800167#define BTF_INFO_MASK 0x8f00ffff
Martin KaFai Lauaea2f7b82018-05-22 14:57:20 -0700168#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);
Yonghong Song9d5f9f72018-12-15 22:13:51 -0800277 int (*check_kflag_member)(struct btf_verifier_env *env,
278 const struct btf_type *struct_type,
279 const struct btf_member *member,
280 const struct btf_type *member_type);
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700281 void (*log_details)(struct btf_verifier_env *env,
282 const struct btf_type *t);
Martin KaFai Laub00b8da2018-04-18 15:56:00 -0700283 void (*seq_show)(const struct btf *btf, const struct btf_type *t,
284 u32 type_id, void *data, u8 bits_offsets,
285 struct seq_file *m);
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700286};
287
288static const struct btf_kind_operations * const kind_ops[NR_BTF_KINDS];
289static struct btf_type btf_void;
290
Martin KaFai Lau2667a262018-11-19 15:29:08 -0800291static int btf_resolve(struct btf_verifier_env *env,
292 const struct btf_type *t, u32 type_id);
293
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700294static bool btf_type_is_modifier(const struct btf_type *t)
295{
296 /* Some of them is not strictly a C modifier
297 * but they are grouped into the same bucket
298 * for BTF concern:
299 * A type (t) that refers to another
300 * type through t->type AND its size cannot
301 * be determined without following the t->type.
302 *
303 * ptr does not fall into this bucket
304 * because its size is always sizeof(void *).
305 */
306 switch (BTF_INFO_KIND(t->info)) {
307 case BTF_KIND_TYPEDEF:
308 case BTF_KIND_VOLATILE:
309 case BTF_KIND_CONST:
310 case BTF_KIND_RESTRICT:
311 return true;
312 }
313
314 return false;
315}
316
317static bool btf_type_is_void(const struct btf_type *t)
318{
Martin KaFai Laub47a0bd2018-11-19 15:29:06 -0800319 return t == &btf_void;
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700320}
321
Martin KaFai Laub47a0bd2018-11-19 15:29:06 -0800322static bool btf_type_is_fwd(const struct btf_type *t)
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700323{
Martin KaFai Laub47a0bd2018-11-19 15:29:06 -0800324 return BTF_INFO_KIND(t->info) == BTF_KIND_FWD;
325}
326
Martin KaFai Lau2667a262018-11-19 15:29:08 -0800327static bool btf_type_is_func(const struct btf_type *t)
328{
329 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC;
330}
331
332static bool btf_type_is_func_proto(const struct btf_type *t)
333{
334 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC_PROTO;
335}
336
Martin KaFai Laub47a0bd2018-11-19 15:29:06 -0800337static bool btf_type_nosize(const struct btf_type *t)
338{
Martin KaFai Lau2667a262018-11-19 15:29:08 -0800339 return btf_type_is_void(t) || btf_type_is_fwd(t) ||
340 btf_type_is_func(t) || btf_type_is_func_proto(t);
Martin KaFai Laub47a0bd2018-11-19 15:29:06 -0800341}
342
343static bool btf_type_nosize_or_null(const struct btf_type *t)
344{
345 return !t || btf_type_nosize(t);
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700346}
347
348/* union is only a special case of struct:
349 * all its offsetof(member) == 0
350 */
351static bool btf_type_is_struct(const struct btf_type *t)
352{
353 u8 kind = BTF_INFO_KIND(t->info);
354
355 return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
356}
357
358static bool btf_type_is_array(const struct btf_type *t)
359{
360 return BTF_INFO_KIND(t->info) == BTF_KIND_ARRAY;
361}
362
363static bool btf_type_is_ptr(const struct btf_type *t)
364{
365 return BTF_INFO_KIND(t->info) == BTF_KIND_PTR;
366}
367
368static bool btf_type_is_int(const struct btf_type *t)
369{
370 return BTF_INFO_KIND(t->info) == BTF_KIND_INT;
371}
372
373/* What types need to be resolved?
374 *
375 * btf_type_is_modifier() is an obvious one.
376 *
377 * btf_type_is_struct() because its member refers to
378 * another type (through member->type).
379
380 * btf_type_is_array() because its element (array->type)
381 * refers to another type. Array can be thought of a
382 * special case of struct while array just has the same
383 * member-type repeated by array->nelems of times.
384 */
385static bool btf_type_needs_resolve(const struct btf_type *t)
386{
387 return btf_type_is_modifier(t) ||
388 btf_type_is_ptr(t) ||
389 btf_type_is_struct(t) ||
390 btf_type_is_array(t);
391}
392
393/* t->size can be used */
394static bool btf_type_has_size(const struct btf_type *t)
395{
396 switch (BTF_INFO_KIND(t->info)) {
397 case BTF_KIND_INT:
398 case BTF_KIND_STRUCT:
399 case BTF_KIND_UNION:
400 case BTF_KIND_ENUM:
401 return true;
402 }
403
404 return false;
405}
406
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700407static const char *btf_int_encoding_str(u8 encoding)
408{
409 if (encoding == 0)
410 return "(none)";
411 else if (encoding == BTF_INT_SIGNED)
412 return "SIGNED";
413 else if (encoding == BTF_INT_CHAR)
414 return "CHAR";
415 else if (encoding == BTF_INT_BOOL)
416 return "BOOL";
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700417 else
418 return "UNKN";
419}
420
421static u16 btf_type_vlen(const struct btf_type *t)
422{
423 return BTF_INFO_VLEN(t->info);
424}
425
Yonghong Song9d5f9f72018-12-15 22:13:51 -0800426static bool btf_type_kflag(const struct btf_type *t)
427{
428 return BTF_INFO_KFLAG(t->info);
429}
430
431static u32 btf_member_bit_offset(const struct btf_type *struct_type,
432 const struct btf_member *member)
433{
434 return btf_type_kflag(struct_type) ? BTF_MEMBER_BIT_OFFSET(member->offset)
435 : member->offset;
436}
437
438static u32 btf_member_bitfield_size(const struct btf_type *struct_type,
439 const struct btf_member *member)
440{
441 return btf_type_kflag(struct_type) ? BTF_MEMBER_BITFIELD_SIZE(member->offset)
442 : 0;
443}
444
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700445static u32 btf_type_int(const struct btf_type *t)
446{
447 return *(u32 *)(t + 1);
448}
449
450static const struct btf_array *btf_type_array(const struct btf_type *t)
451{
452 return (const struct btf_array *)(t + 1);
453}
454
455static const struct btf_member *btf_type_member(const struct btf_type *t)
456{
457 return (const struct btf_member *)(t + 1);
458}
459
460static const struct btf_enum *btf_type_enum(const struct btf_type *t)
461{
462 return (const struct btf_enum *)(t + 1);
463}
464
465static const struct btf_kind_operations *btf_type_ops(const struct btf_type *t)
466{
467 return kind_ops[BTF_INFO_KIND(t->info)];
468}
469
Martin KaFai Lauc454a462018-12-07 16:42:25 -0800470bool btf_name_offset_valid(const struct btf *btf, u32 offset)
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700471{
Martin KaFai Lauaea2f7b82018-05-22 14:57:20 -0700472 return BTF_STR_OFFSET_VALID(offset) &&
473 offset < btf->hdr.str_len;
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700474}
475
Martin KaFai Lau2667a262018-11-19 15:29:08 -0800476/* Only C-style identifier is permitted. This can be relaxed if
477 * necessary.
478 */
479static bool btf_name_valid_identifier(const struct btf *btf, u32 offset)
480{
481 /* offset must be valid */
482 const char *src = &btf->strings[offset];
483 const char *src_limit;
484
485 if (!isalpha(*src) && *src != '_')
486 return false;
487
488 /* set a limit on identifier length */
489 src_limit = src + KSYM_NAME_LEN;
490 src++;
491 while (*src && src < src_limit) {
492 if (!isalnum(*src) && *src != '_')
493 return false;
494 src++;
495 }
496
497 return !*src;
498}
499
Martin KaFai Lau23127b32018-12-13 10:41:46 -0800500static const char *__btf_name_by_offset(const struct btf *btf, u32 offset)
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700501{
Martin KaFai Lauaea2f7b82018-05-22 14:57:20 -0700502 if (!offset)
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700503 return "(anon)";
Martin KaFai Lauaea2f7b82018-05-22 14:57:20 -0700504 else if (offset < btf->hdr.str_len)
505 return &btf->strings[offset];
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700506 else
507 return "(invalid-name-offset)";
508}
509
Martin KaFai Lau23127b32018-12-13 10:41:46 -0800510const char *btf_name_by_offset(const struct btf *btf, u32 offset)
511{
512 if (offset < btf->hdr.str_len)
513 return &btf->strings[offset];
514
515 return NULL;
516}
517
Yonghong Song838e9692018-11-19 15:29:11 -0800518const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id)
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700519{
520 if (type_id > btf->nr_types)
521 return NULL;
522
523 return btf->types[type_id];
524}
525
Martin KaFai Lau4ef5f572018-05-22 14:57:19 -0700526/*
527 * Regular int is not a bit field and it must be either
528 * u8/u16/u32/u64.
529 */
530static bool btf_type_int_is_regular(const struct btf_type *t)
531{
Martin KaFai Lau36fc3c82018-07-19 22:14:31 -0700532 u8 nr_bits, nr_bytes;
Martin KaFai Lau4ef5f572018-05-22 14:57:19 -0700533 u32 int_data;
534
535 int_data = btf_type_int(t);
536 nr_bits = BTF_INT_BITS(int_data);
537 nr_bytes = BITS_ROUNDUP_BYTES(nr_bits);
538 if (BITS_PER_BYTE_MASKED(nr_bits) ||
539 BTF_INT_OFFSET(int_data) ||
540 (nr_bytes != sizeof(u8) && nr_bytes != sizeof(u16) &&
541 nr_bytes != sizeof(u32) && nr_bytes != sizeof(u64))) {
542 return false;
543 }
544
545 return true;
546}
547
Roman Gushchin9a1126b2018-12-10 15:43:01 -0800548/*
Yonghong Songffa0c1c2018-12-15 22:13:52 -0800549 * Check that given struct member is a regular int with expected
550 * offset and size.
Roman Gushchin9a1126b2018-12-10 15:43:01 -0800551 */
Yonghong Songffa0c1c2018-12-15 22:13:52 -0800552bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s,
553 const struct btf_member *m,
554 u32 expected_offset, u32 expected_size)
Roman Gushchin9a1126b2018-12-10 15:43:01 -0800555{
Yonghong Songffa0c1c2018-12-15 22:13:52 -0800556 const struct btf_type *t;
557 u32 id, int_data;
558 u8 nr_bits;
Roman Gushchin9a1126b2018-12-10 15:43:01 -0800559
Yonghong Songffa0c1c2018-12-15 22:13:52 -0800560 id = m->type;
561 t = btf_type_id_size(btf, &id, NULL);
562 if (!t || !btf_type_is_int(t))
Roman Gushchin9a1126b2018-12-10 15:43:01 -0800563 return false;
564
565 int_data = btf_type_int(t);
566 nr_bits = BTF_INT_BITS(int_data);
Yonghong Songffa0c1c2018-12-15 22:13:52 -0800567 if (btf_type_kflag(s)) {
568 u32 bitfield_size = BTF_MEMBER_BITFIELD_SIZE(m->offset);
569 u32 bit_offset = BTF_MEMBER_BIT_OFFSET(m->offset);
570
571 /* if kflag set, int should be a regular int and
572 * bit offset should be at byte boundary.
573 */
574 return !bitfield_size &&
575 BITS_ROUNDUP_BYTES(bit_offset) == expected_offset &&
576 BITS_ROUNDUP_BYTES(nr_bits) == expected_size;
577 }
578
579 if (BTF_INT_OFFSET(int_data) ||
580 BITS_PER_BYTE_MASKED(m->offset) ||
581 BITS_ROUNDUP_BYTES(m->offset) != expected_offset ||
582 BITS_PER_BYTE_MASKED(nr_bits) ||
583 BITS_ROUNDUP_BYTES(nr_bits) != expected_size)
Roman Gushchin9a1126b2018-12-10 15:43:01 -0800584 return false;
585
586 return true;
587}
588
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700589__printf(2, 3) static void __btf_verifier_log(struct bpf_verifier_log *log,
590 const char *fmt, ...)
591{
592 va_list args;
593
594 va_start(args, fmt);
595 bpf_verifier_vlog(log, fmt, args);
596 va_end(args);
597}
598
599__printf(2, 3) static void btf_verifier_log(struct btf_verifier_env *env,
600 const char *fmt, ...)
601{
602 struct bpf_verifier_log *log = &env->log;
603 va_list args;
604
605 if (!bpf_verifier_log_needed(log))
606 return;
607
608 va_start(args, fmt);
609 bpf_verifier_vlog(log, fmt, args);
610 va_end(args);
611}
612
613__printf(4, 5) static void __btf_verifier_log_type(struct btf_verifier_env *env,
614 const struct btf_type *t,
615 bool log_details,
616 const char *fmt, ...)
617{
618 struct bpf_verifier_log *log = &env->log;
619 u8 kind = BTF_INFO_KIND(t->info);
620 struct btf *btf = env->btf;
621 va_list args;
622
623 if (!bpf_verifier_log_needed(log))
624 return;
625
626 __btf_verifier_log(log, "[%u] %s %s%s",
627 env->log_type_id,
628 btf_kind_str[kind],
Martin KaFai Lau23127b32018-12-13 10:41:46 -0800629 __btf_name_by_offset(btf, t->name_off),
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700630 log_details ? " " : "");
631
632 if (log_details)
633 btf_type_ops(t)->log_details(env, t);
634
635 if (fmt && *fmt) {
636 __btf_verifier_log(log, " ");
637 va_start(args, fmt);
638 bpf_verifier_vlog(log, fmt, args);
639 va_end(args);
640 }
641
642 __btf_verifier_log(log, "\n");
643}
644
645#define btf_verifier_log_type(env, t, ...) \
646 __btf_verifier_log_type((env), (t), true, __VA_ARGS__)
647#define btf_verifier_log_basic(env, t, ...) \
648 __btf_verifier_log_type((env), (t), false, __VA_ARGS__)
649
650__printf(4, 5)
651static void btf_verifier_log_member(struct btf_verifier_env *env,
652 const struct btf_type *struct_type,
653 const struct btf_member *member,
654 const char *fmt, ...)
655{
656 struct bpf_verifier_log *log = &env->log;
657 struct btf *btf = env->btf;
658 va_list args;
659
660 if (!bpf_verifier_log_needed(log))
661 return;
662
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700663 /* The CHECK_META phase already did a btf dump.
664 *
665 * If member is logged again, it must hit an error in
666 * parsing this member. It is useful to print out which
667 * struct this member belongs to.
668 */
669 if (env->phase != CHECK_META)
670 btf_verifier_log_type(env, struct_type, NULL);
671
Yonghong Song9d5f9f72018-12-15 22:13:51 -0800672 if (btf_type_kflag(struct_type))
673 __btf_verifier_log(log,
674 "\t%s type_id=%u bitfield_size=%u bits_offset=%u",
675 __btf_name_by_offset(btf, member->name_off),
676 member->type,
677 BTF_MEMBER_BITFIELD_SIZE(member->offset),
678 BTF_MEMBER_BIT_OFFSET(member->offset));
679 else
680 __btf_verifier_log(log, "\t%s type_id=%u bits_offset=%u",
681 __btf_name_by_offset(btf, member->name_off),
682 member->type, member->offset);
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700683
684 if (fmt && *fmt) {
685 __btf_verifier_log(log, " ");
686 va_start(args, fmt);
687 bpf_verifier_vlog(log, fmt, args);
688 va_end(args);
689 }
690
691 __btf_verifier_log(log, "\n");
692}
693
Martin KaFai Lauf80442a2018-05-22 14:57:18 -0700694static void btf_verifier_log_hdr(struct btf_verifier_env *env,
695 u32 btf_data_size)
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700696{
697 struct bpf_verifier_log *log = &env->log;
698 const struct btf *btf = env->btf;
699 const struct btf_header *hdr;
700
701 if (!bpf_verifier_log_needed(log))
702 return;
703
Martin KaFai Lauf80442a2018-05-22 14:57:18 -0700704 hdr = &btf->hdr;
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700705 __btf_verifier_log(log, "magic: 0x%x\n", hdr->magic);
706 __btf_verifier_log(log, "version: %u\n", hdr->version);
707 __btf_verifier_log(log, "flags: 0x%x\n", hdr->flags);
Martin KaFai Lauf80442a2018-05-22 14:57:18 -0700708 __btf_verifier_log(log, "hdr_len: %u\n", hdr->hdr_len);
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700709 __btf_verifier_log(log, "type_off: %u\n", hdr->type_off);
Martin KaFai Lauf80442a2018-05-22 14:57:18 -0700710 __btf_verifier_log(log, "type_len: %u\n", hdr->type_len);
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700711 __btf_verifier_log(log, "str_off: %u\n", hdr->str_off);
712 __btf_verifier_log(log, "str_len: %u\n", hdr->str_len);
Martin KaFai Lauf80442a2018-05-22 14:57:18 -0700713 __btf_verifier_log(log, "btf_total_size: %u\n", btf_data_size);
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700714}
715
716static int btf_add_type(struct btf_verifier_env *env, struct btf_type *t)
717{
718 struct btf *btf = env->btf;
719
720 /* < 2 because +1 for btf_void which is always in btf->types[0].
721 * btf_void is not accounted in btf->nr_types because btf_void
722 * does not come from the BTF file.
723 */
724 if (btf->types_size - btf->nr_types < 2) {
725 /* Expand 'types' array */
726
727 struct btf_type **new_types;
728 u32 expand_by, new_size;
729
Martin KaFai Lauaea2f7b82018-05-22 14:57:20 -0700730 if (btf->types_size == BTF_MAX_TYPE) {
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700731 btf_verifier_log(env, "Exceeded max num of types");
732 return -E2BIG;
733 }
734
735 expand_by = max_t(u32, btf->types_size >> 2, 16);
Martin KaFai Lauaea2f7b82018-05-22 14:57:20 -0700736 new_size = min_t(u32, BTF_MAX_TYPE,
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700737 btf->types_size + expand_by);
738
Kees Cook778e1cd2018-06-12 14:04:48 -0700739 new_types = kvcalloc(new_size, sizeof(*new_types),
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700740 GFP_KERNEL | __GFP_NOWARN);
741 if (!new_types)
742 return -ENOMEM;
743
744 if (btf->nr_types == 0)
745 new_types[0] = &btf_void;
746 else
747 memcpy(new_types, btf->types,
748 sizeof(*btf->types) * (btf->nr_types + 1));
749
750 kvfree(btf->types);
751 btf->types = new_types;
752 btf->types_size = new_size;
753 }
754
755 btf->types[++(btf->nr_types)] = t;
756
757 return 0;
758}
759
Martin KaFai Lau78958fc2018-05-04 14:49:51 -0700760static int btf_alloc_id(struct btf *btf)
761{
762 int id;
763
764 idr_preload(GFP_KERNEL);
765 spin_lock_bh(&btf_idr_lock);
766 id = idr_alloc_cyclic(&btf_idr, btf, 1, INT_MAX, GFP_ATOMIC);
767 if (id > 0)
768 btf->id = id;
769 spin_unlock_bh(&btf_idr_lock);
770 idr_preload_end();
771
772 if (WARN_ON_ONCE(!id))
773 return -ENOSPC;
774
775 return id > 0 ? 0 : id;
776}
777
778static void btf_free_id(struct btf *btf)
779{
780 unsigned long flags;
781
782 /*
783 * In map-in-map, calling map_delete_elem() on outer
784 * map will call bpf_map_put on the inner map.
785 * It will then eventually call btf_free_id()
786 * on the inner map. Some of the map_delete_elem()
787 * implementation may have irq disabled, so
788 * we need to use the _irqsave() version instead
789 * of the _bh() version.
790 */
791 spin_lock_irqsave(&btf_idr_lock, flags);
792 idr_remove(&btf_idr, btf->id);
793 spin_unlock_irqrestore(&btf_idr_lock, flags);
794}
795
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700796static void btf_free(struct btf *btf)
797{
798 kvfree(btf->types);
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700799 kvfree(btf->resolved_sizes);
800 kvfree(btf->resolved_ids);
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700801 kvfree(btf->data);
802 kfree(btf);
803}
804
Martin KaFai Lau78958fc2018-05-04 14:49:51 -0700805static void btf_free_rcu(struct rcu_head *rcu)
Martin KaFai Lauf56a6532018-04-18 15:56:01 -0700806{
Martin KaFai Lau78958fc2018-05-04 14:49:51 -0700807 struct btf *btf = container_of(rcu, struct btf, rcu);
808
809 btf_free(btf);
Martin KaFai Lauf56a6532018-04-18 15:56:01 -0700810}
811
812void btf_put(struct btf *btf)
813{
Martin KaFai Lau78958fc2018-05-04 14:49:51 -0700814 if (btf && refcount_dec_and_test(&btf->refcnt)) {
815 btf_free_id(btf);
816 call_rcu(&btf->rcu, btf_free_rcu);
817 }
Martin KaFai Lauf56a6532018-04-18 15:56:01 -0700818}
819
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700820static int env_resolve_init(struct btf_verifier_env *env)
821{
822 struct btf *btf = env->btf;
823 u32 nr_types = btf->nr_types;
824 u32 *resolved_sizes = NULL;
825 u32 *resolved_ids = NULL;
826 u8 *visit_states = NULL;
827
828 /* +1 for btf_void */
Kees Cook778e1cd2018-06-12 14:04:48 -0700829 resolved_sizes = kvcalloc(nr_types + 1, sizeof(*resolved_sizes),
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700830 GFP_KERNEL | __GFP_NOWARN);
831 if (!resolved_sizes)
832 goto nomem;
833
Kees Cook778e1cd2018-06-12 14:04:48 -0700834 resolved_ids = kvcalloc(nr_types + 1, sizeof(*resolved_ids),
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700835 GFP_KERNEL | __GFP_NOWARN);
836 if (!resolved_ids)
837 goto nomem;
838
Kees Cook778e1cd2018-06-12 14:04:48 -0700839 visit_states = kvcalloc(nr_types + 1, sizeof(*visit_states),
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700840 GFP_KERNEL | __GFP_NOWARN);
841 if (!visit_states)
842 goto nomem;
843
844 btf->resolved_sizes = resolved_sizes;
845 btf->resolved_ids = resolved_ids;
846 env->visit_states = visit_states;
847
848 return 0;
849
850nomem:
851 kvfree(resolved_sizes);
852 kvfree(resolved_ids);
853 kvfree(visit_states);
854 return -ENOMEM;
855}
856
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700857static void btf_verifier_env_free(struct btf_verifier_env *env)
858{
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700859 kvfree(env->visit_states);
Martin KaFai Lau69b693f2018-04-18 15:55:57 -0700860 kfree(env);
861}
862
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700863static bool env_type_is_resolve_sink(const struct btf_verifier_env *env,
864 const struct btf_type *next_type)
865{
866 switch (env->resolve_mode) {
867 case RESOLVE_TBD:
868 /* int, enum or void is a sink */
869 return !btf_type_needs_resolve(next_type);
870 case RESOLVE_PTR:
Martin KaFai Lau2667a262018-11-19 15:29:08 -0800871 /* int, enum, void, struct, array, func or func_proto is a sink
872 * for ptr
873 */
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700874 return !btf_type_is_modifier(next_type) &&
875 !btf_type_is_ptr(next_type);
876 case RESOLVE_STRUCT_OR_ARRAY:
Martin KaFai Lau2667a262018-11-19 15:29:08 -0800877 /* int, enum, void, ptr, func or func_proto is a sink
878 * for struct and array
879 */
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700880 return !btf_type_is_modifier(next_type) &&
881 !btf_type_is_array(next_type) &&
882 !btf_type_is_struct(next_type);
883 default:
Arnd Bergmann53c80362018-05-25 23:33:19 +0200884 BUG();
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700885 }
886}
887
888static bool env_type_is_resolved(const struct btf_verifier_env *env,
889 u32 type_id)
890{
891 return env->visit_states[type_id] == RESOLVED;
892}
893
894static int env_stack_push(struct btf_verifier_env *env,
895 const struct btf_type *t, u32 type_id)
896{
897 struct resolve_vertex *v;
898
899 if (env->top_stack == MAX_RESOLVE_DEPTH)
900 return -E2BIG;
901
902 if (env->visit_states[type_id] != NOT_VISITED)
903 return -EEXIST;
904
905 env->visit_states[type_id] = VISITED;
906
907 v = &env->stack[env->top_stack++];
908 v->t = t;
909 v->type_id = type_id;
910 v->next_member = 0;
911
912 if (env->resolve_mode == RESOLVE_TBD) {
913 if (btf_type_is_ptr(t))
914 env->resolve_mode = RESOLVE_PTR;
915 else if (btf_type_is_struct(t) || btf_type_is_array(t))
916 env->resolve_mode = RESOLVE_STRUCT_OR_ARRAY;
917 }
918
919 return 0;
920}
921
922static void env_stack_set_next_member(struct btf_verifier_env *env,
923 u16 next_member)
924{
925 env->stack[env->top_stack - 1].next_member = next_member;
926}
927
928static void env_stack_pop_resolved(struct btf_verifier_env *env,
929 u32 resolved_type_id,
930 u32 resolved_size)
931{
932 u32 type_id = env->stack[--(env->top_stack)].type_id;
933 struct btf *btf = env->btf;
934
935 btf->resolved_sizes[type_id] = resolved_size;
936 btf->resolved_ids[type_id] = resolved_type_id;
937 env->visit_states[type_id] = RESOLVED;
938}
939
940static const struct resolve_vertex *env_stack_peak(struct btf_verifier_env *env)
941{
942 return env->top_stack ? &env->stack[env->top_stack - 1] : NULL;
943}
944
945/* The input param "type_id" must point to a needs_resolve type */
946static const struct btf_type *btf_type_id_resolve(const struct btf *btf,
947 u32 *type_id)
948{
949 *type_id = btf->resolved_ids[*type_id];
950 return btf_type_by_id(btf, *type_id);
951}
952
953const struct btf_type *btf_type_id_size(const struct btf *btf,
954 u32 *type_id, u32 *ret_size)
955{
956 const struct btf_type *size_type;
957 u32 size_type_id = *type_id;
958 u32 size = 0;
959
960 size_type = btf_type_by_id(btf, size_type_id);
Martin KaFai Laub47a0bd2018-11-19 15:29:06 -0800961 if (btf_type_nosize_or_null(size_type))
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700962 return NULL;
963
964 if (btf_type_has_size(size_type)) {
965 size = size_type->size;
966 } else if (btf_type_is_array(size_type)) {
967 size = btf->resolved_sizes[size_type_id];
968 } else if (btf_type_is_ptr(size_type)) {
969 size = sizeof(void *);
970 } else {
971 if (WARN_ON_ONCE(!btf_type_is_modifier(size_type)))
972 return NULL;
973
974 size = btf->resolved_sizes[size_type_id];
975 size_type_id = btf->resolved_ids[size_type_id];
976 size_type = btf_type_by_id(btf, size_type_id);
Martin KaFai Laub47a0bd2018-11-19 15:29:06 -0800977 if (btf_type_nosize_or_null(size_type))
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700978 return NULL;
979 }
980
981 *type_id = size_type_id;
982 if (ret_size)
983 *ret_size = size;
984
985 return size_type;
986}
987
Martin KaFai Lau179cde82018-04-18 15:55:59 -0700988static int btf_df_check_member(struct btf_verifier_env *env,
989 const struct btf_type *struct_type,
990 const struct btf_member *member,
991 const struct btf_type *member_type)
992{
993 btf_verifier_log_basic(env, struct_type,
994 "Unsupported check_member");
995 return -EINVAL;
996}
997
Yonghong Song9d5f9f72018-12-15 22:13:51 -0800998static int btf_df_check_kflag_member(struct btf_verifier_env *env,
999 const struct btf_type *struct_type,
1000 const struct btf_member *member,
1001 const struct btf_type *member_type)
1002{
1003 btf_verifier_log_basic(env, struct_type,
1004 "Unsupported check_kflag_member");
1005 return -EINVAL;
1006}
1007
1008/* Used for ptr, array and struct/union type members.
1009 * int, enum and modifier types have their specific callback functions.
1010 */
1011static int btf_generic_check_kflag_member(struct btf_verifier_env *env,
1012 const struct btf_type *struct_type,
1013 const struct btf_member *member,
1014 const struct btf_type *member_type)
1015{
1016 if (BTF_MEMBER_BITFIELD_SIZE(member->offset)) {
1017 btf_verifier_log_member(env, struct_type, member,
1018 "Invalid member bitfield_size");
1019 return -EINVAL;
1020 }
1021
1022 /* bitfield size is 0, so member->offset represents bit offset only.
1023 * It is safe to call non kflag check_member variants.
1024 */
1025 return btf_type_ops(member_type)->check_member(env, struct_type,
1026 member,
1027 member_type);
1028}
1029
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001030static int btf_df_resolve(struct btf_verifier_env *env,
1031 const struct resolve_vertex *v)
1032{
1033 btf_verifier_log_basic(env, v->t, "Unsupported resolve");
1034 return -EINVAL;
1035}
1036
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07001037static void btf_df_seq_show(const struct btf *btf, const struct btf_type *t,
1038 u32 type_id, void *data, u8 bits_offsets,
1039 struct seq_file *m)
1040{
1041 seq_printf(m, "<unsupported kind:%u>", BTF_INFO_KIND(t->info));
1042}
1043
Martin KaFai Lau179cde82018-04-18 15:55:59 -07001044static int btf_int_check_member(struct btf_verifier_env *env,
1045 const struct btf_type *struct_type,
1046 const struct btf_member *member,
1047 const struct btf_type *member_type)
1048{
1049 u32 int_data = btf_type_int(member_type);
1050 u32 struct_bits_off = member->offset;
1051 u32 struct_size = struct_type->size;
1052 u32 nr_copy_bits;
1053 u32 bytes_offset;
1054
1055 if (U32_MAX - struct_bits_off < BTF_INT_OFFSET(int_data)) {
1056 btf_verifier_log_member(env, struct_type, member,
1057 "bits_offset exceeds U32_MAX");
1058 return -EINVAL;
1059 }
1060
1061 struct_bits_off += BTF_INT_OFFSET(int_data);
1062 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
1063 nr_copy_bits = BTF_INT_BITS(int_data) +
1064 BITS_PER_BYTE_MASKED(struct_bits_off);
1065
1066 if (nr_copy_bits > BITS_PER_U64) {
1067 btf_verifier_log_member(env, struct_type, member,
1068 "nr_copy_bits exceeds 64");
1069 return -EINVAL;
1070 }
1071
1072 if (struct_size < bytes_offset ||
1073 struct_size - bytes_offset < BITS_ROUNDUP_BYTES(nr_copy_bits)) {
1074 btf_verifier_log_member(env, struct_type, member,
1075 "Member exceeds struct_size");
1076 return -EINVAL;
1077 }
1078
1079 return 0;
1080}
1081
Yonghong Song9d5f9f72018-12-15 22:13:51 -08001082static int btf_int_check_kflag_member(struct btf_verifier_env *env,
1083 const struct btf_type *struct_type,
1084 const struct btf_member *member,
1085 const struct btf_type *member_type)
1086{
1087 u32 struct_bits_off, nr_bits, nr_int_data_bits, bytes_offset;
1088 u32 int_data = btf_type_int(member_type);
1089 u32 struct_size = struct_type->size;
1090 u32 nr_copy_bits;
1091
1092 /* a regular int type is required for the kflag int member */
1093 if (!btf_type_int_is_regular(member_type)) {
1094 btf_verifier_log_member(env, struct_type, member,
1095 "Invalid member base type");
1096 return -EINVAL;
1097 }
1098
1099 /* check sanity of bitfield size */
1100 nr_bits = BTF_MEMBER_BITFIELD_SIZE(member->offset);
1101 struct_bits_off = BTF_MEMBER_BIT_OFFSET(member->offset);
1102 nr_int_data_bits = BTF_INT_BITS(int_data);
1103 if (!nr_bits) {
1104 /* Not a bitfield member, member offset must be at byte
1105 * boundary.
1106 */
1107 if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
1108 btf_verifier_log_member(env, struct_type, member,
1109 "Invalid member offset");
1110 return -EINVAL;
1111 }
1112
1113 nr_bits = nr_int_data_bits;
1114 } else if (nr_bits > nr_int_data_bits) {
1115 btf_verifier_log_member(env, struct_type, member,
1116 "Invalid member bitfield_size");
1117 return -EINVAL;
1118 }
1119
1120 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
1121 nr_copy_bits = nr_bits + BITS_PER_BYTE_MASKED(struct_bits_off);
1122 if (nr_copy_bits > BITS_PER_U64) {
1123 btf_verifier_log_member(env, struct_type, member,
1124 "nr_copy_bits exceeds 64");
1125 return -EINVAL;
1126 }
1127
1128 if (struct_size < bytes_offset ||
1129 struct_size - bytes_offset < BITS_ROUNDUP_BYTES(nr_copy_bits)) {
1130 btf_verifier_log_member(env, struct_type, member,
1131 "Member exceeds struct_size");
1132 return -EINVAL;
1133 }
1134
1135 return 0;
1136}
1137
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001138static s32 btf_int_check_meta(struct btf_verifier_env *env,
1139 const struct btf_type *t,
1140 u32 meta_left)
1141{
1142 u32 int_data, nr_bits, meta_needed = sizeof(int_data);
1143 u16 encoding;
1144
1145 if (meta_left < meta_needed) {
1146 btf_verifier_log_basic(env, t,
1147 "meta_left:%u meta_needed:%u",
1148 meta_left, meta_needed);
1149 return -EINVAL;
1150 }
1151
1152 if (btf_type_vlen(t)) {
1153 btf_verifier_log_type(env, t, "vlen != 0");
1154 return -EINVAL;
1155 }
1156
Yonghong Song9d5f9f72018-12-15 22:13:51 -08001157 if (btf_type_kflag(t)) {
1158 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
1159 return -EINVAL;
1160 }
1161
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001162 int_data = btf_type_int(t);
Martin KaFai Lauaea2f7b82018-05-22 14:57:20 -07001163 if (int_data & ~BTF_INT_MASK) {
1164 btf_verifier_log_basic(env, t, "Invalid int_data:%x",
1165 int_data);
1166 return -EINVAL;
1167 }
1168
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001169 nr_bits = BTF_INT_BITS(int_data) + BTF_INT_OFFSET(int_data);
1170
1171 if (nr_bits > BITS_PER_U64) {
1172 btf_verifier_log_type(env, t, "nr_bits exceeds %zu",
1173 BITS_PER_U64);
1174 return -EINVAL;
1175 }
1176
1177 if (BITS_ROUNDUP_BYTES(nr_bits) > t->size) {
1178 btf_verifier_log_type(env, t, "nr_bits exceeds type_size");
1179 return -EINVAL;
1180 }
1181
Martin KaFai Lauaea2f7b82018-05-22 14:57:20 -07001182 /*
1183 * Only one of the encoding bits is allowed and it
1184 * should be sufficient for the pretty print purpose (i.e. decoding).
1185 * Multiple bits can be allowed later if it is found
1186 * to be insufficient.
1187 */
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001188 encoding = BTF_INT_ENCODING(int_data);
1189 if (encoding &&
1190 encoding != BTF_INT_SIGNED &&
1191 encoding != BTF_INT_CHAR &&
Martin KaFai Lauaea2f7b82018-05-22 14:57:20 -07001192 encoding != BTF_INT_BOOL) {
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001193 btf_verifier_log_type(env, t, "Unsupported encoding");
1194 return -ENOTSUPP;
1195 }
1196
1197 btf_verifier_log_type(env, t, NULL);
1198
1199 return meta_needed;
1200}
1201
1202static void btf_int_log(struct btf_verifier_env *env,
1203 const struct btf_type *t)
1204{
1205 int int_data = btf_type_int(t);
1206
1207 btf_verifier_log(env,
1208 "size=%u bits_offset=%u nr_bits=%u encoding=%s",
1209 t->size, BTF_INT_OFFSET(int_data),
1210 BTF_INT_BITS(int_data),
1211 btf_int_encoding_str(BTF_INT_ENCODING(int_data)));
1212}
1213
Yonghong Songf97be3a2018-12-15 22:13:50 -08001214static void btf_bitfield_seq_show(void *data, u8 bits_offset,
1215 u8 nr_bits, struct seq_file *m)
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07001216{
Okash Khawajab65f3702018-07-10 14:33:07 -07001217 u16 left_shift_bits, right_shift_bits;
Martin KaFai Lau36fc3c82018-07-19 22:14:31 -07001218 u8 nr_copy_bytes;
1219 u8 nr_copy_bits;
Okash Khawajab65f3702018-07-10 14:33:07 -07001220 u64 print_num;
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07001221
Yonghong Songf97be3a2018-12-15 22:13:50 -08001222 data += BITS_ROUNDDOWN_BYTES(bits_offset);
1223 bits_offset = BITS_PER_BYTE_MASKED(bits_offset);
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07001224 nr_copy_bits = nr_bits + bits_offset;
1225 nr_copy_bytes = BITS_ROUNDUP_BYTES(nr_copy_bits);
1226
Okash Khawajab65f3702018-07-10 14:33:07 -07001227 print_num = 0;
1228 memcpy(&print_num, data, nr_copy_bytes);
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07001229
Okash Khawajab65f3702018-07-10 14:33:07 -07001230#ifdef __BIG_ENDIAN_BITFIELD
1231 left_shift_bits = bits_offset;
1232#else
1233 left_shift_bits = BITS_PER_U64 - nr_copy_bits;
1234#endif
1235 right_shift_bits = BITS_PER_U64 - nr_bits;
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07001236
Okash Khawajab65f3702018-07-10 14:33:07 -07001237 print_num <<= left_shift_bits;
1238 print_num >>= right_shift_bits;
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07001239
Okash Khawajab65f3702018-07-10 14:33:07 -07001240 seq_printf(m, "0x%llx", print_num);
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07001241}
1242
Yonghong Song9d5f9f72018-12-15 22:13:51 -08001243
Yonghong Songf97be3a2018-12-15 22:13:50 -08001244static void btf_int_bits_seq_show(const struct btf *btf,
1245 const struct btf_type *t,
1246 void *data, u8 bits_offset,
1247 struct seq_file *m)
1248{
1249 u32 int_data = btf_type_int(t);
1250 u8 nr_bits = BTF_INT_BITS(int_data);
1251 u8 total_bits_offset;
1252
1253 /*
1254 * bits_offset is at most 7.
1255 * BTF_INT_OFFSET() cannot exceed 64 bits.
1256 */
1257 total_bits_offset = bits_offset + BTF_INT_OFFSET(int_data);
1258 btf_bitfield_seq_show(data, total_bits_offset, nr_bits, m);
1259}
1260
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07001261static void btf_int_seq_show(const struct btf *btf, const struct btf_type *t,
1262 u32 type_id, void *data, u8 bits_offset,
1263 struct seq_file *m)
1264{
1265 u32 int_data = btf_type_int(t);
1266 u8 encoding = BTF_INT_ENCODING(int_data);
1267 bool sign = encoding & BTF_INT_SIGNED;
Martin KaFai Lau36fc3c82018-07-19 22:14:31 -07001268 u8 nr_bits = BTF_INT_BITS(int_data);
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07001269
1270 if (bits_offset || BTF_INT_OFFSET(int_data) ||
1271 BITS_PER_BYTE_MASKED(nr_bits)) {
1272 btf_int_bits_seq_show(btf, t, data, bits_offset, m);
1273 return;
1274 }
1275
1276 switch (nr_bits) {
1277 case 64:
1278 if (sign)
1279 seq_printf(m, "%lld", *(s64 *)data);
1280 else
1281 seq_printf(m, "%llu", *(u64 *)data);
1282 break;
1283 case 32:
1284 if (sign)
1285 seq_printf(m, "%d", *(s32 *)data);
1286 else
1287 seq_printf(m, "%u", *(u32 *)data);
1288 break;
1289 case 16:
1290 if (sign)
1291 seq_printf(m, "%d", *(s16 *)data);
1292 else
1293 seq_printf(m, "%u", *(u16 *)data);
1294 break;
1295 case 8:
1296 if (sign)
1297 seq_printf(m, "%d", *(s8 *)data);
1298 else
1299 seq_printf(m, "%u", *(u8 *)data);
1300 break;
1301 default:
1302 btf_int_bits_seq_show(btf, t, data, bits_offset, m);
1303 }
1304}
1305
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001306static const struct btf_kind_operations int_ops = {
1307 .check_meta = btf_int_check_meta,
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001308 .resolve = btf_df_resolve,
Martin KaFai Lau179cde82018-04-18 15:55:59 -07001309 .check_member = btf_int_check_member,
Yonghong Song9d5f9f72018-12-15 22:13:51 -08001310 .check_kflag_member = btf_int_check_kflag_member,
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001311 .log_details = btf_int_log,
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07001312 .seq_show = btf_int_seq_show,
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001313};
1314
Martin KaFai Lau179cde82018-04-18 15:55:59 -07001315static int btf_modifier_check_member(struct btf_verifier_env *env,
1316 const struct btf_type *struct_type,
1317 const struct btf_member *member,
1318 const struct btf_type *member_type)
1319{
1320 const struct btf_type *resolved_type;
1321 u32 resolved_type_id = member->type;
1322 struct btf_member resolved_member;
1323 struct btf *btf = env->btf;
1324
1325 resolved_type = btf_type_id_size(btf, &resolved_type_id, NULL);
1326 if (!resolved_type) {
1327 btf_verifier_log_member(env, struct_type, member,
1328 "Invalid member");
1329 return -EINVAL;
1330 }
1331
1332 resolved_member = *member;
1333 resolved_member.type = resolved_type_id;
1334
1335 return btf_type_ops(resolved_type)->check_member(env, struct_type,
1336 &resolved_member,
1337 resolved_type);
1338}
1339
Yonghong Song9d5f9f72018-12-15 22:13:51 -08001340static int btf_modifier_check_kflag_member(struct btf_verifier_env *env,
1341 const struct btf_type *struct_type,
1342 const struct btf_member *member,
1343 const struct btf_type *member_type)
1344{
1345 const struct btf_type *resolved_type;
1346 u32 resolved_type_id = member->type;
1347 struct btf_member resolved_member;
1348 struct btf *btf = env->btf;
1349
1350 resolved_type = btf_type_id_size(btf, &resolved_type_id, NULL);
1351 if (!resolved_type) {
1352 btf_verifier_log_member(env, struct_type, member,
1353 "Invalid member");
1354 return -EINVAL;
1355 }
1356
1357 resolved_member = *member;
1358 resolved_member.type = resolved_type_id;
1359
1360 return btf_type_ops(resolved_type)->check_kflag_member(env, struct_type,
1361 &resolved_member,
1362 resolved_type);
1363}
1364
Martin KaFai Lau179cde82018-04-18 15:55:59 -07001365static int btf_ptr_check_member(struct btf_verifier_env *env,
1366 const struct btf_type *struct_type,
1367 const struct btf_member *member,
1368 const struct btf_type *member_type)
1369{
1370 u32 struct_size, struct_bits_off, bytes_offset;
1371
1372 struct_size = struct_type->size;
1373 struct_bits_off = member->offset;
1374 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
1375
1376 if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
1377 btf_verifier_log_member(env, struct_type, member,
1378 "Member is not byte aligned");
1379 return -EINVAL;
1380 }
1381
1382 if (struct_size - bytes_offset < sizeof(void *)) {
1383 btf_verifier_log_member(env, struct_type, member,
1384 "Member exceeds struct_size");
1385 return -EINVAL;
1386 }
1387
1388 return 0;
1389}
1390
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001391static int btf_ref_type_check_meta(struct btf_verifier_env *env,
1392 const struct btf_type *t,
1393 u32 meta_left)
1394{
1395 if (btf_type_vlen(t)) {
1396 btf_verifier_log_type(env, t, "vlen != 0");
1397 return -EINVAL;
1398 }
1399
Yonghong Song9d5f9f72018-12-15 22:13:51 -08001400 if (btf_type_kflag(t)) {
1401 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
1402 return -EINVAL;
1403 }
1404
Martin KaFai Lauaea2f7b82018-05-22 14:57:20 -07001405 if (!BTF_TYPE_ID_VALID(t->type)) {
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001406 btf_verifier_log_type(env, t, "Invalid type_id");
1407 return -EINVAL;
1408 }
1409
Yonghong Songeb04bbb2018-11-27 13:23:28 -08001410 /* typedef type must have a valid name, and other ref types,
1411 * volatile, const, restrict, should have a null name.
1412 */
1413 if (BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF) {
1414 if (!t->name_off ||
1415 !btf_name_valid_identifier(env->btf, t->name_off)) {
1416 btf_verifier_log_type(env, t, "Invalid name");
1417 return -EINVAL;
1418 }
1419 } else {
1420 if (t->name_off) {
1421 btf_verifier_log_type(env, t, "Invalid name");
1422 return -EINVAL;
1423 }
1424 }
1425
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001426 btf_verifier_log_type(env, t, NULL);
1427
1428 return 0;
1429}
1430
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001431static int btf_modifier_resolve(struct btf_verifier_env *env,
1432 const struct resolve_vertex *v)
1433{
1434 const struct btf_type *t = v->t;
1435 const struct btf_type *next_type;
1436 u32 next_type_id = t->type;
1437 struct btf *btf = env->btf;
1438 u32 next_type_size = 0;
1439
1440 next_type = btf_type_by_id(btf, next_type_id);
1441 if (!next_type) {
1442 btf_verifier_log_type(env, v->t, "Invalid type_id");
1443 return -EINVAL;
1444 }
1445
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001446 if (!env_type_is_resolve_sink(env, next_type) &&
1447 !env_type_is_resolved(env, next_type_id))
1448 return env_stack_push(env, next_type, next_type_id);
1449
1450 /* Figure out the resolved next_type_id with size.
1451 * They will be stored in the current modifier's
1452 * resolved_ids and resolved_sizes such that it can
1453 * save us a few type-following when we use it later (e.g. in
1454 * pretty print).
1455 */
Martin KaFai Lau2667a262018-11-19 15:29:08 -08001456 if (!btf_type_id_size(btf, &next_type_id, &next_type_size)) {
1457 if (env_type_is_resolved(env, next_type_id))
1458 next_type = btf_type_id_resolve(btf, &next_type_id);
1459
1460 /* "typedef void new_void", "const void"...etc */
1461 if (!btf_type_is_void(next_type) &&
1462 !btf_type_is_fwd(next_type)) {
1463 btf_verifier_log_type(env, v->t, "Invalid type_id");
1464 return -EINVAL;
1465 }
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001466 }
1467
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001468 env_stack_pop_resolved(env, next_type_id, next_type_size);
1469
1470 return 0;
1471}
1472
1473static int btf_ptr_resolve(struct btf_verifier_env *env,
1474 const struct resolve_vertex *v)
1475{
1476 const struct btf_type *next_type;
1477 const struct btf_type *t = v->t;
1478 u32 next_type_id = t->type;
1479 struct btf *btf = env->btf;
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001480
1481 next_type = btf_type_by_id(btf, next_type_id);
1482 if (!next_type) {
1483 btf_verifier_log_type(env, v->t, "Invalid type_id");
1484 return -EINVAL;
1485 }
1486
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001487 if (!env_type_is_resolve_sink(env, next_type) &&
1488 !env_type_is_resolved(env, next_type_id))
1489 return env_stack_push(env, next_type, next_type_id);
1490
1491 /* If the modifier was RESOLVED during RESOLVE_STRUCT_OR_ARRAY,
1492 * the modifier may have stopped resolving when it was resolved
1493 * to a ptr (last-resolved-ptr).
1494 *
1495 * We now need to continue from the last-resolved-ptr to
1496 * ensure the last-resolved-ptr will not referring back to
1497 * the currenct ptr (t).
1498 */
1499 if (btf_type_is_modifier(next_type)) {
1500 const struct btf_type *resolved_type;
1501 u32 resolved_type_id;
1502
1503 resolved_type_id = next_type_id;
1504 resolved_type = btf_type_id_resolve(btf, &resolved_type_id);
1505
1506 if (btf_type_is_ptr(resolved_type) &&
1507 !env_type_is_resolve_sink(env, resolved_type) &&
1508 !env_type_is_resolved(env, resolved_type_id))
1509 return env_stack_push(env, resolved_type,
1510 resolved_type_id);
1511 }
1512
Martin KaFai Lau2667a262018-11-19 15:29:08 -08001513 if (!btf_type_id_size(btf, &next_type_id, NULL)) {
1514 if (env_type_is_resolved(env, next_type_id))
1515 next_type = btf_type_id_resolve(btf, &next_type_id);
1516
1517 if (!btf_type_is_void(next_type) &&
1518 !btf_type_is_fwd(next_type) &&
1519 !btf_type_is_func_proto(next_type)) {
1520 btf_verifier_log_type(env, v->t, "Invalid type_id");
1521 return -EINVAL;
1522 }
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001523 }
1524
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001525 env_stack_pop_resolved(env, next_type_id, 0);
1526
1527 return 0;
1528}
1529
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07001530static void btf_modifier_seq_show(const struct btf *btf,
1531 const struct btf_type *t,
1532 u32 type_id, void *data,
1533 u8 bits_offset, struct seq_file *m)
1534{
1535 t = btf_type_id_resolve(btf, &type_id);
1536
1537 btf_type_ops(t)->seq_show(btf, t, type_id, data, bits_offset, m);
1538}
1539
1540static void btf_ptr_seq_show(const struct btf *btf, const struct btf_type *t,
1541 u32 type_id, void *data, u8 bits_offset,
1542 struct seq_file *m)
1543{
1544 /* It is a hashed value */
1545 seq_printf(m, "%p", *(void **)data);
1546}
1547
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001548static void btf_ref_type_log(struct btf_verifier_env *env,
1549 const struct btf_type *t)
1550{
1551 btf_verifier_log(env, "type_id=%u", t->type);
1552}
1553
1554static struct btf_kind_operations modifier_ops = {
1555 .check_meta = btf_ref_type_check_meta,
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001556 .resolve = btf_modifier_resolve,
Martin KaFai Lau179cde82018-04-18 15:55:59 -07001557 .check_member = btf_modifier_check_member,
Yonghong Song9d5f9f72018-12-15 22:13:51 -08001558 .check_kflag_member = btf_modifier_check_kflag_member,
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001559 .log_details = btf_ref_type_log,
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07001560 .seq_show = btf_modifier_seq_show,
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001561};
1562
1563static struct btf_kind_operations ptr_ops = {
1564 .check_meta = btf_ref_type_check_meta,
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001565 .resolve = btf_ptr_resolve,
Martin KaFai Lau179cde82018-04-18 15:55:59 -07001566 .check_member = btf_ptr_check_member,
Yonghong Song9d5f9f72018-12-15 22:13:51 -08001567 .check_kflag_member = btf_generic_check_kflag_member,
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001568 .log_details = btf_ref_type_log,
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07001569 .seq_show = btf_ptr_seq_show,
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001570};
1571
Martin KaFai Lau81753832018-06-02 09:06:51 -07001572static s32 btf_fwd_check_meta(struct btf_verifier_env *env,
1573 const struct btf_type *t,
1574 u32 meta_left)
1575{
1576 if (btf_type_vlen(t)) {
1577 btf_verifier_log_type(env, t, "vlen != 0");
1578 return -EINVAL;
1579 }
1580
1581 if (t->type) {
1582 btf_verifier_log_type(env, t, "type != 0");
1583 return -EINVAL;
1584 }
1585
Yonghong Songeb04bbb2018-11-27 13:23:28 -08001586 /* fwd type must have a valid name */
1587 if (!t->name_off ||
1588 !btf_name_valid_identifier(env->btf, t->name_off)) {
1589 btf_verifier_log_type(env, t, "Invalid name");
1590 return -EINVAL;
1591 }
1592
Martin KaFai Lau81753832018-06-02 09:06:51 -07001593 btf_verifier_log_type(env, t, NULL);
1594
1595 return 0;
1596}
1597
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001598static struct btf_kind_operations fwd_ops = {
Martin KaFai Lau81753832018-06-02 09:06:51 -07001599 .check_meta = btf_fwd_check_meta,
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001600 .resolve = btf_df_resolve,
Martin KaFai Lau179cde82018-04-18 15:55:59 -07001601 .check_member = btf_df_check_member,
Yonghong Song9d5f9f72018-12-15 22:13:51 -08001602 .check_kflag_member = btf_df_check_kflag_member,
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001603 .log_details = btf_ref_type_log,
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07001604 .seq_show = btf_df_seq_show,
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001605};
1606
Martin KaFai Lau179cde82018-04-18 15:55:59 -07001607static int btf_array_check_member(struct btf_verifier_env *env,
1608 const struct btf_type *struct_type,
1609 const struct btf_member *member,
1610 const struct btf_type *member_type)
1611{
1612 u32 struct_bits_off = member->offset;
1613 u32 struct_size, bytes_offset;
1614 u32 array_type_id, array_size;
1615 struct btf *btf = env->btf;
1616
1617 if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
1618 btf_verifier_log_member(env, struct_type, member,
1619 "Member is not byte aligned");
1620 return -EINVAL;
1621 }
1622
1623 array_type_id = member->type;
1624 btf_type_id_size(btf, &array_type_id, &array_size);
1625 struct_size = struct_type->size;
1626 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
1627 if (struct_size - bytes_offset < array_size) {
1628 btf_verifier_log_member(env, struct_type, member,
1629 "Member exceeds struct_size");
1630 return -EINVAL;
1631 }
1632
1633 return 0;
1634}
1635
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001636static s32 btf_array_check_meta(struct btf_verifier_env *env,
1637 const struct btf_type *t,
1638 u32 meta_left)
1639{
1640 const struct btf_array *array = btf_type_array(t);
1641 u32 meta_needed = sizeof(*array);
1642
1643 if (meta_left < meta_needed) {
1644 btf_verifier_log_basic(env, t,
1645 "meta_left:%u meta_needed:%u",
1646 meta_left, meta_needed);
1647 return -EINVAL;
1648 }
1649
Yonghong Songeb04bbb2018-11-27 13:23:28 -08001650 /* array type should not have a name */
1651 if (t->name_off) {
1652 btf_verifier_log_type(env, t, "Invalid name");
1653 return -EINVAL;
1654 }
1655
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001656 if (btf_type_vlen(t)) {
1657 btf_verifier_log_type(env, t, "vlen != 0");
1658 return -EINVAL;
1659 }
1660
Yonghong Song9d5f9f72018-12-15 22:13:51 -08001661 if (btf_type_kflag(t)) {
1662 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
1663 return -EINVAL;
1664 }
1665
Martin KaFai Laub9308ae2018-06-02 09:06:50 -07001666 if (t->size) {
1667 btf_verifier_log_type(env, t, "size != 0");
1668 return -EINVAL;
1669 }
1670
Martin KaFai Lau4ef5f572018-05-22 14:57:19 -07001671 /* Array elem type and index type cannot be in type void,
1672 * so !array->type and !array->index_type are not allowed.
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001673 */
Martin KaFai Lauaea2f7b82018-05-22 14:57:20 -07001674 if (!array->type || !BTF_TYPE_ID_VALID(array->type)) {
Martin KaFai Lau4ef5f572018-05-22 14:57:19 -07001675 btf_verifier_log_type(env, t, "Invalid elem");
1676 return -EINVAL;
1677 }
1678
Martin KaFai Lauaea2f7b82018-05-22 14:57:20 -07001679 if (!array->index_type || !BTF_TYPE_ID_VALID(array->index_type)) {
Martin KaFai Lau4ef5f572018-05-22 14:57:19 -07001680 btf_verifier_log_type(env, t, "Invalid index");
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001681 return -EINVAL;
1682 }
1683
1684 btf_verifier_log_type(env, t, NULL);
1685
1686 return meta_needed;
1687}
1688
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001689static int btf_array_resolve(struct btf_verifier_env *env,
1690 const struct resolve_vertex *v)
1691{
1692 const struct btf_array *array = btf_type_array(v->t);
Martin KaFai Lau4ef5f572018-05-22 14:57:19 -07001693 const struct btf_type *elem_type, *index_type;
1694 u32 elem_type_id, index_type_id;
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001695 struct btf *btf = env->btf;
1696 u32 elem_size;
1697
Martin KaFai Lau4ef5f572018-05-22 14:57:19 -07001698 /* Check array->index_type */
1699 index_type_id = array->index_type;
1700 index_type = btf_type_by_id(btf, index_type_id);
Martin KaFai Laub47a0bd2018-11-19 15:29:06 -08001701 if (btf_type_nosize_or_null(index_type)) {
Martin KaFai Lau4ef5f572018-05-22 14:57:19 -07001702 btf_verifier_log_type(env, v->t, "Invalid index");
1703 return -EINVAL;
1704 }
1705
1706 if (!env_type_is_resolve_sink(env, index_type) &&
1707 !env_type_is_resolved(env, index_type_id))
1708 return env_stack_push(env, index_type, index_type_id);
1709
1710 index_type = btf_type_id_size(btf, &index_type_id, NULL);
1711 if (!index_type || !btf_type_is_int(index_type) ||
1712 !btf_type_int_is_regular(index_type)) {
1713 btf_verifier_log_type(env, v->t, "Invalid index");
1714 return -EINVAL;
1715 }
1716
1717 /* Check array->type */
1718 elem_type_id = array->type;
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001719 elem_type = btf_type_by_id(btf, elem_type_id);
Martin KaFai Laub47a0bd2018-11-19 15:29:06 -08001720 if (btf_type_nosize_or_null(elem_type)) {
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001721 btf_verifier_log_type(env, v->t,
1722 "Invalid elem");
1723 return -EINVAL;
1724 }
1725
1726 if (!env_type_is_resolve_sink(env, elem_type) &&
1727 !env_type_is_resolved(env, elem_type_id))
1728 return env_stack_push(env, elem_type, elem_type_id);
1729
1730 elem_type = btf_type_id_size(btf, &elem_type_id, &elem_size);
1731 if (!elem_type) {
1732 btf_verifier_log_type(env, v->t, "Invalid elem");
1733 return -EINVAL;
1734 }
1735
Martin KaFai Lau4ef5f572018-05-22 14:57:19 -07001736 if (btf_type_is_int(elem_type) && !btf_type_int_is_regular(elem_type)) {
1737 btf_verifier_log_type(env, v->t, "Invalid array of int");
1738 return -EINVAL;
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001739 }
1740
1741 if (array->nelems && elem_size > U32_MAX / array->nelems) {
1742 btf_verifier_log_type(env, v->t,
1743 "Array size overflows U32_MAX");
1744 return -EINVAL;
1745 }
1746
1747 env_stack_pop_resolved(env, elem_type_id, elem_size * array->nelems);
1748
1749 return 0;
1750}
1751
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001752static void btf_array_log(struct btf_verifier_env *env,
1753 const struct btf_type *t)
1754{
1755 const struct btf_array *array = btf_type_array(t);
1756
1757 btf_verifier_log(env, "type_id=%u index_type_id=%u nr_elems=%u",
1758 array->type, array->index_type, array->nelems);
1759}
1760
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07001761static void btf_array_seq_show(const struct btf *btf, const struct btf_type *t,
1762 u32 type_id, void *data, u8 bits_offset,
1763 struct seq_file *m)
1764{
1765 const struct btf_array *array = btf_type_array(t);
1766 const struct btf_kind_operations *elem_ops;
1767 const struct btf_type *elem_type;
1768 u32 i, elem_size, elem_type_id;
1769
1770 elem_type_id = array->type;
1771 elem_type = btf_type_id_size(btf, &elem_type_id, &elem_size);
1772 elem_ops = btf_type_ops(elem_type);
1773 seq_puts(m, "[");
1774 for (i = 0; i < array->nelems; i++) {
1775 if (i)
1776 seq_puts(m, ",");
1777
1778 elem_ops->seq_show(btf, elem_type, elem_type_id, data,
1779 bits_offset, m);
1780 data += elem_size;
1781 }
1782 seq_puts(m, "]");
1783}
1784
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001785static struct btf_kind_operations array_ops = {
1786 .check_meta = btf_array_check_meta,
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001787 .resolve = btf_array_resolve,
Martin KaFai Lau179cde82018-04-18 15:55:59 -07001788 .check_member = btf_array_check_member,
Yonghong Song9d5f9f72018-12-15 22:13:51 -08001789 .check_kflag_member = btf_generic_check_kflag_member,
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001790 .log_details = btf_array_log,
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07001791 .seq_show = btf_array_seq_show,
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001792};
1793
Martin KaFai Lau179cde82018-04-18 15:55:59 -07001794static int btf_struct_check_member(struct btf_verifier_env *env,
1795 const struct btf_type *struct_type,
1796 const struct btf_member *member,
1797 const struct btf_type *member_type)
1798{
1799 u32 struct_bits_off = member->offset;
1800 u32 struct_size, bytes_offset;
1801
1802 if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
1803 btf_verifier_log_member(env, struct_type, member,
1804 "Member is not byte aligned");
1805 return -EINVAL;
1806 }
1807
1808 struct_size = struct_type->size;
1809 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
1810 if (struct_size - bytes_offset < member_type->size) {
1811 btf_verifier_log_member(env, struct_type, member,
1812 "Member exceeds struct_size");
1813 return -EINVAL;
1814 }
1815
1816 return 0;
1817}
1818
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001819static s32 btf_struct_check_meta(struct btf_verifier_env *env,
1820 const struct btf_type *t,
1821 u32 meta_left)
1822{
1823 bool is_union = BTF_INFO_KIND(t->info) == BTF_KIND_UNION;
1824 const struct btf_member *member;
Martin KaFai Lau6283fa32018-07-20 17:38:37 -07001825 u32 meta_needed, last_offset;
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001826 struct btf *btf = env->btf;
1827 u32 struct_size = t->size;
Yonghong Song9d5f9f72018-12-15 22:13:51 -08001828 u32 offset;
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001829 u16 i;
1830
1831 meta_needed = btf_type_vlen(t) * sizeof(*member);
1832 if (meta_left < meta_needed) {
1833 btf_verifier_log_basic(env, t,
1834 "meta_left:%u meta_needed:%u",
1835 meta_left, meta_needed);
1836 return -EINVAL;
1837 }
1838
Yonghong Songeb04bbb2018-11-27 13:23:28 -08001839 /* struct type either no name or a valid one */
1840 if (t->name_off &&
1841 !btf_name_valid_identifier(env->btf, t->name_off)) {
1842 btf_verifier_log_type(env, t, "Invalid name");
1843 return -EINVAL;
1844 }
1845
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001846 btf_verifier_log_type(env, t, NULL);
1847
Martin KaFai Lau6283fa32018-07-20 17:38:37 -07001848 last_offset = 0;
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001849 for_each_member(i, t, member) {
Martin KaFai Laufbcf93e2018-04-21 09:48:23 -07001850 if (!btf_name_offset_valid(btf, member->name_off)) {
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001851 btf_verifier_log_member(env, t, member,
1852 "Invalid member name_offset:%u",
Martin KaFai Laufbcf93e2018-04-21 09:48:23 -07001853 member->name_off);
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001854 return -EINVAL;
1855 }
1856
Yonghong Songeb04bbb2018-11-27 13:23:28 -08001857 /* struct member either no name or a valid one */
1858 if (member->name_off &&
1859 !btf_name_valid_identifier(btf, member->name_off)) {
1860 btf_verifier_log_member(env, t, member, "Invalid name");
1861 return -EINVAL;
1862 }
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001863 /* A member cannot be in type void */
Martin KaFai Lauaea2f7b82018-05-22 14:57:20 -07001864 if (!member->type || !BTF_TYPE_ID_VALID(member->type)) {
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001865 btf_verifier_log_member(env, t, member,
1866 "Invalid type_id");
1867 return -EINVAL;
1868 }
1869
Yonghong Song9d5f9f72018-12-15 22:13:51 -08001870 offset = btf_member_bit_offset(t, member);
1871 if (is_union && offset) {
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001872 btf_verifier_log_member(env, t, member,
1873 "Invalid member bits_offset");
1874 return -EINVAL;
1875 }
1876
Martin KaFai Lau6283fa32018-07-20 17:38:37 -07001877 /*
1878 * ">" instead of ">=" because the last member could be
1879 * "char a[0];"
1880 */
Yonghong Song9d5f9f72018-12-15 22:13:51 -08001881 if (last_offset > offset) {
Martin KaFai Lau6283fa32018-07-20 17:38:37 -07001882 btf_verifier_log_member(env, t, member,
1883 "Invalid member bits_offset");
1884 return -EINVAL;
1885 }
1886
Yonghong Song9d5f9f72018-12-15 22:13:51 -08001887 if (BITS_ROUNDUP_BYTES(offset) > struct_size) {
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001888 btf_verifier_log_member(env, t, member,
Colin Ian King311fe1a2018-11-25 23:32:51 +00001889 "Member bits_offset exceeds its struct size");
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001890 return -EINVAL;
1891 }
1892
1893 btf_verifier_log_member(env, t, member, NULL);
Yonghong Song9d5f9f72018-12-15 22:13:51 -08001894 last_offset = offset;
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001895 }
1896
1897 return meta_needed;
1898}
1899
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001900static int btf_struct_resolve(struct btf_verifier_env *env,
1901 const struct resolve_vertex *v)
1902{
1903 const struct btf_member *member;
Martin KaFai Lau179cde82018-04-18 15:55:59 -07001904 int err;
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001905 u16 i;
1906
1907 /* Before continue resolving the next_member,
1908 * ensure the last member is indeed resolved to a
1909 * type with size info.
1910 */
1911 if (v->next_member) {
Martin KaFai Lau179cde82018-04-18 15:55:59 -07001912 const struct btf_type *last_member_type;
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001913 const struct btf_member *last_member;
1914 u16 last_member_type_id;
1915
1916 last_member = btf_type_member(v->t) + v->next_member - 1;
1917 last_member_type_id = last_member->type;
1918 if (WARN_ON_ONCE(!env_type_is_resolved(env,
1919 last_member_type_id)))
1920 return -EINVAL;
Martin KaFai Lau179cde82018-04-18 15:55:59 -07001921
1922 last_member_type = btf_type_by_id(env->btf,
1923 last_member_type_id);
Yonghong Song9d5f9f72018-12-15 22:13:51 -08001924 if (btf_type_kflag(v->t))
1925 err = btf_type_ops(last_member_type)->check_kflag_member(env, v->t,
1926 last_member,
1927 last_member_type);
1928 else
1929 err = btf_type_ops(last_member_type)->check_member(env, v->t,
1930 last_member,
1931 last_member_type);
Martin KaFai Lau179cde82018-04-18 15:55:59 -07001932 if (err)
1933 return err;
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001934 }
1935
1936 for_each_member_from(i, v->next_member, v->t, member) {
1937 u32 member_type_id = member->type;
1938 const struct btf_type *member_type = btf_type_by_id(env->btf,
1939 member_type_id);
1940
Martin KaFai Laub47a0bd2018-11-19 15:29:06 -08001941 if (btf_type_nosize_or_null(member_type)) {
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001942 btf_verifier_log_member(env, v->t, member,
1943 "Invalid member");
1944 return -EINVAL;
1945 }
1946
1947 if (!env_type_is_resolve_sink(env, member_type) &&
1948 !env_type_is_resolved(env, member_type_id)) {
1949 env_stack_set_next_member(env, i + 1);
1950 return env_stack_push(env, member_type, member_type_id);
1951 }
Martin KaFai Lau179cde82018-04-18 15:55:59 -07001952
Yonghong Song9d5f9f72018-12-15 22:13:51 -08001953 if (btf_type_kflag(v->t))
1954 err = btf_type_ops(member_type)->check_kflag_member(env, v->t,
1955 member,
1956 member_type);
1957 else
1958 err = btf_type_ops(member_type)->check_member(env, v->t,
1959 member,
1960 member_type);
Martin KaFai Lau179cde82018-04-18 15:55:59 -07001961 if (err)
1962 return err;
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001963 }
1964
1965 env_stack_pop_resolved(env, 0, 0);
1966
1967 return 0;
1968}
1969
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07001970static void btf_struct_log(struct btf_verifier_env *env,
1971 const struct btf_type *t)
1972{
1973 btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t));
1974}
1975
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07001976static void btf_struct_seq_show(const struct btf *btf, const struct btf_type *t,
1977 u32 type_id, void *data, u8 bits_offset,
1978 struct seq_file *m)
1979{
1980 const char *seq = BTF_INFO_KIND(t->info) == BTF_KIND_UNION ? "|" : ",";
1981 const struct btf_member *member;
1982 u32 i;
1983
1984 seq_puts(m, "{");
1985 for_each_member(i, t, member) {
1986 const struct btf_type *member_type = btf_type_by_id(btf,
1987 member->type);
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07001988 const struct btf_kind_operations *ops;
Yonghong Song9d5f9f72018-12-15 22:13:51 -08001989 u32 member_offset, bitfield_size;
1990 u32 bytes_offset;
1991 u8 bits8_offset;
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07001992
1993 if (i)
1994 seq_puts(m, seq);
1995
Yonghong Song9d5f9f72018-12-15 22:13:51 -08001996 member_offset = btf_member_bit_offset(t, member);
1997 bitfield_size = btf_member_bitfield_size(t, member);
1998 if (bitfield_size) {
1999 btf_bitfield_seq_show(data, member_offset,
2000 bitfield_size, m);
2001 } else {
2002 bytes_offset = BITS_ROUNDDOWN_BYTES(member_offset);
2003 bits8_offset = BITS_PER_BYTE_MASKED(member_offset);
2004 ops = btf_type_ops(member_type);
2005 ops->seq_show(btf, member_type, member->type,
2006 data + bytes_offset, bits8_offset, m);
2007 }
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07002008 }
2009 seq_puts(m, "}");
2010}
2011
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002012static struct btf_kind_operations struct_ops = {
2013 .check_meta = btf_struct_check_meta,
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07002014 .resolve = btf_struct_resolve,
Martin KaFai Lau179cde82018-04-18 15:55:59 -07002015 .check_member = btf_struct_check_member,
Yonghong Song9d5f9f72018-12-15 22:13:51 -08002016 .check_kflag_member = btf_generic_check_kflag_member,
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002017 .log_details = btf_struct_log,
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07002018 .seq_show = btf_struct_seq_show,
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002019};
2020
Martin KaFai Lau179cde82018-04-18 15:55:59 -07002021static int btf_enum_check_member(struct btf_verifier_env *env,
2022 const struct btf_type *struct_type,
2023 const struct btf_member *member,
2024 const struct btf_type *member_type)
2025{
2026 u32 struct_bits_off = member->offset;
2027 u32 struct_size, bytes_offset;
2028
2029 if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
2030 btf_verifier_log_member(env, struct_type, member,
2031 "Member is not byte aligned");
2032 return -EINVAL;
2033 }
2034
2035 struct_size = struct_type->size;
2036 bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
2037 if (struct_size - bytes_offset < sizeof(int)) {
2038 btf_verifier_log_member(env, struct_type, member,
2039 "Member exceeds struct_size");
2040 return -EINVAL;
2041 }
2042
2043 return 0;
2044}
2045
Yonghong Song9d5f9f72018-12-15 22:13:51 -08002046static int btf_enum_check_kflag_member(struct btf_verifier_env *env,
2047 const struct btf_type *struct_type,
2048 const struct btf_member *member,
2049 const struct btf_type *member_type)
2050{
2051 u32 struct_bits_off, nr_bits, bytes_end, struct_size;
2052 u32 int_bitsize = sizeof(int) * BITS_PER_BYTE;
2053
2054 struct_bits_off = BTF_MEMBER_BIT_OFFSET(member->offset);
2055 nr_bits = BTF_MEMBER_BITFIELD_SIZE(member->offset);
2056 if (!nr_bits) {
2057 if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
2058 btf_verifier_log_member(env, struct_type, member,
2059 "Member is not byte aligned");
2060 return -EINVAL;
2061 }
2062
2063 nr_bits = int_bitsize;
2064 } else if (nr_bits > int_bitsize) {
2065 btf_verifier_log_member(env, struct_type, member,
2066 "Invalid member bitfield_size");
2067 return -EINVAL;
2068 }
2069
2070 struct_size = struct_type->size;
2071 bytes_end = BITS_ROUNDUP_BYTES(struct_bits_off + nr_bits);
2072 if (struct_size < bytes_end) {
2073 btf_verifier_log_member(env, struct_type, member,
2074 "Member exceeds struct_size");
2075 return -EINVAL;
2076 }
2077
2078 return 0;
2079}
2080
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002081static s32 btf_enum_check_meta(struct btf_verifier_env *env,
2082 const struct btf_type *t,
2083 u32 meta_left)
2084{
2085 const struct btf_enum *enums = btf_type_enum(t);
2086 struct btf *btf = env->btf;
2087 u16 i, nr_enums;
2088 u32 meta_needed;
2089
2090 nr_enums = btf_type_vlen(t);
2091 meta_needed = nr_enums * sizeof(*enums);
2092
2093 if (meta_left < meta_needed) {
2094 btf_verifier_log_basic(env, t,
2095 "meta_left:%u meta_needed:%u",
2096 meta_left, meta_needed);
2097 return -EINVAL;
2098 }
2099
Yonghong Song9d5f9f72018-12-15 22:13:51 -08002100 if (btf_type_kflag(t)) {
2101 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
2102 return -EINVAL;
2103 }
2104
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002105 if (t->size != sizeof(int)) {
2106 btf_verifier_log_type(env, t, "Expected size:%zu",
2107 sizeof(int));
2108 return -EINVAL;
2109 }
2110
Yonghong Songeb04bbb2018-11-27 13:23:28 -08002111 /* enum type either no name or a valid one */
2112 if (t->name_off &&
2113 !btf_name_valid_identifier(env->btf, t->name_off)) {
2114 btf_verifier_log_type(env, t, "Invalid name");
2115 return -EINVAL;
2116 }
2117
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002118 btf_verifier_log_type(env, t, NULL);
2119
2120 for (i = 0; i < nr_enums; i++) {
Martin KaFai Laufbcf93e2018-04-21 09:48:23 -07002121 if (!btf_name_offset_valid(btf, enums[i].name_off)) {
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002122 btf_verifier_log(env, "\tInvalid name_offset:%u",
Martin KaFai Laufbcf93e2018-04-21 09:48:23 -07002123 enums[i].name_off);
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002124 return -EINVAL;
2125 }
2126
Yonghong Songeb04bbb2018-11-27 13:23:28 -08002127 /* enum member must have a valid name */
2128 if (!enums[i].name_off ||
2129 !btf_name_valid_identifier(btf, enums[i].name_off)) {
2130 btf_verifier_log_type(env, t, "Invalid name");
2131 return -EINVAL;
2132 }
2133
2134
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002135 btf_verifier_log(env, "\t%s val=%d\n",
Martin KaFai Lau23127b32018-12-13 10:41:46 -08002136 __btf_name_by_offset(btf, enums[i].name_off),
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002137 enums[i].val);
2138 }
2139
2140 return meta_needed;
2141}
2142
2143static void btf_enum_log(struct btf_verifier_env *env,
2144 const struct btf_type *t)
2145{
2146 btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t));
2147}
2148
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07002149static void btf_enum_seq_show(const struct btf *btf, const struct btf_type *t,
2150 u32 type_id, void *data, u8 bits_offset,
2151 struct seq_file *m)
2152{
2153 const struct btf_enum *enums = btf_type_enum(t);
2154 u32 i, nr_enums = btf_type_vlen(t);
2155 int v = *(int *)data;
2156
2157 for (i = 0; i < nr_enums; i++) {
2158 if (v == enums[i].val) {
2159 seq_printf(m, "%s",
Martin KaFai Lau23127b32018-12-13 10:41:46 -08002160 __btf_name_by_offset(btf,
2161 enums[i].name_off));
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07002162 return;
2163 }
2164 }
2165
2166 seq_printf(m, "%d", v);
2167}
2168
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002169static struct btf_kind_operations enum_ops = {
2170 .check_meta = btf_enum_check_meta,
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07002171 .resolve = btf_df_resolve,
Martin KaFai Lau179cde82018-04-18 15:55:59 -07002172 .check_member = btf_enum_check_member,
Yonghong Song9d5f9f72018-12-15 22:13:51 -08002173 .check_kflag_member = btf_enum_check_kflag_member,
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002174 .log_details = btf_enum_log,
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07002175 .seq_show = btf_enum_seq_show,
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002176};
2177
Martin KaFai Lau2667a262018-11-19 15:29:08 -08002178static s32 btf_func_proto_check_meta(struct btf_verifier_env *env,
2179 const struct btf_type *t,
2180 u32 meta_left)
2181{
2182 u32 meta_needed = btf_type_vlen(t) * sizeof(struct btf_param);
2183
2184 if (meta_left < meta_needed) {
2185 btf_verifier_log_basic(env, t,
2186 "meta_left:%u meta_needed:%u",
2187 meta_left, meta_needed);
2188 return -EINVAL;
2189 }
2190
2191 if (t->name_off) {
2192 btf_verifier_log_type(env, t, "Invalid name");
2193 return -EINVAL;
2194 }
2195
Yonghong Song9d5f9f72018-12-15 22:13:51 -08002196 if (btf_type_kflag(t)) {
2197 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
2198 return -EINVAL;
2199 }
2200
Martin KaFai Lau2667a262018-11-19 15:29:08 -08002201 btf_verifier_log_type(env, t, NULL);
2202
2203 return meta_needed;
2204}
2205
2206static void btf_func_proto_log(struct btf_verifier_env *env,
2207 const struct btf_type *t)
2208{
2209 const struct btf_param *args = (const struct btf_param *)(t + 1);
2210 u16 nr_args = btf_type_vlen(t), i;
2211
2212 btf_verifier_log(env, "return=%u args=(", t->type);
2213 if (!nr_args) {
2214 btf_verifier_log(env, "void");
2215 goto done;
2216 }
2217
2218 if (nr_args == 1 && !args[0].type) {
2219 /* Only one vararg */
2220 btf_verifier_log(env, "vararg");
2221 goto done;
2222 }
2223
2224 btf_verifier_log(env, "%u %s", args[0].type,
Martin KaFai Lau23127b32018-12-13 10:41:46 -08002225 __btf_name_by_offset(env->btf,
2226 args[0].name_off));
Martin KaFai Lau2667a262018-11-19 15:29:08 -08002227 for (i = 1; i < nr_args - 1; i++)
2228 btf_verifier_log(env, ", %u %s", args[i].type,
Martin KaFai Lau23127b32018-12-13 10:41:46 -08002229 __btf_name_by_offset(env->btf,
2230 args[i].name_off));
Martin KaFai Lau2667a262018-11-19 15:29:08 -08002231
2232 if (nr_args > 1) {
2233 const struct btf_param *last_arg = &args[nr_args - 1];
2234
2235 if (last_arg->type)
2236 btf_verifier_log(env, ", %u %s", last_arg->type,
Martin KaFai Lau23127b32018-12-13 10:41:46 -08002237 __btf_name_by_offset(env->btf,
2238 last_arg->name_off));
Martin KaFai Lau2667a262018-11-19 15:29:08 -08002239 else
2240 btf_verifier_log(env, ", vararg");
2241 }
2242
2243done:
2244 btf_verifier_log(env, ")");
2245}
2246
2247static struct btf_kind_operations func_proto_ops = {
2248 .check_meta = btf_func_proto_check_meta,
2249 .resolve = btf_df_resolve,
2250 /*
2251 * BTF_KIND_FUNC_PROTO cannot be directly referred by
2252 * a struct's member.
2253 *
2254 * It should be a funciton pointer instead.
2255 * (i.e. struct's member -> BTF_KIND_PTR -> BTF_KIND_FUNC_PROTO)
2256 *
2257 * Hence, there is no btf_func_check_member().
2258 */
2259 .check_member = btf_df_check_member,
Yonghong Song9d5f9f72018-12-15 22:13:51 -08002260 .check_kflag_member = btf_df_check_kflag_member,
Martin KaFai Lau2667a262018-11-19 15:29:08 -08002261 .log_details = btf_func_proto_log,
2262 .seq_show = btf_df_seq_show,
2263};
2264
2265static s32 btf_func_check_meta(struct btf_verifier_env *env,
2266 const struct btf_type *t,
2267 u32 meta_left)
2268{
2269 if (!t->name_off ||
2270 !btf_name_valid_identifier(env->btf, t->name_off)) {
2271 btf_verifier_log_type(env, t, "Invalid name");
2272 return -EINVAL;
2273 }
2274
2275 if (btf_type_vlen(t)) {
2276 btf_verifier_log_type(env, t, "vlen != 0");
2277 return -EINVAL;
2278 }
2279
Yonghong Song9d5f9f72018-12-15 22:13:51 -08002280 if (btf_type_kflag(t)) {
2281 btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
2282 return -EINVAL;
2283 }
2284
Martin KaFai Lau2667a262018-11-19 15:29:08 -08002285 btf_verifier_log_type(env, t, NULL);
2286
2287 return 0;
2288}
2289
2290static struct btf_kind_operations func_ops = {
2291 .check_meta = btf_func_check_meta,
2292 .resolve = btf_df_resolve,
2293 .check_member = btf_df_check_member,
Yonghong Song9d5f9f72018-12-15 22:13:51 -08002294 .check_kflag_member = btf_df_check_kflag_member,
Martin KaFai Lau2667a262018-11-19 15:29:08 -08002295 .log_details = btf_ref_type_log,
2296 .seq_show = btf_df_seq_show,
2297};
2298
2299static int btf_func_proto_check(struct btf_verifier_env *env,
2300 const struct btf_type *t)
2301{
2302 const struct btf_type *ret_type;
2303 const struct btf_param *args;
2304 const struct btf *btf;
2305 u16 nr_args, i;
2306 int err;
2307
2308 btf = env->btf;
2309 args = (const struct btf_param *)(t + 1);
2310 nr_args = btf_type_vlen(t);
2311
2312 /* Check func return type which could be "void" (t->type == 0) */
2313 if (t->type) {
2314 u32 ret_type_id = t->type;
2315
2316 ret_type = btf_type_by_id(btf, ret_type_id);
2317 if (!ret_type) {
2318 btf_verifier_log_type(env, t, "Invalid return type");
2319 return -EINVAL;
2320 }
2321
2322 if (btf_type_needs_resolve(ret_type) &&
2323 !env_type_is_resolved(env, ret_type_id)) {
2324 err = btf_resolve(env, ret_type, ret_type_id);
2325 if (err)
2326 return err;
2327 }
2328
2329 /* Ensure the return type is a type that has a size */
2330 if (!btf_type_id_size(btf, &ret_type_id, NULL)) {
2331 btf_verifier_log_type(env, t, "Invalid return type");
2332 return -EINVAL;
2333 }
2334 }
2335
2336 if (!nr_args)
2337 return 0;
2338
2339 /* Last func arg type_id could be 0 if it is a vararg */
2340 if (!args[nr_args - 1].type) {
2341 if (args[nr_args - 1].name_off) {
2342 btf_verifier_log_type(env, t, "Invalid arg#%u",
2343 nr_args);
2344 return -EINVAL;
2345 }
2346 nr_args--;
2347 }
2348
2349 err = 0;
2350 for (i = 0; i < nr_args; i++) {
2351 const struct btf_type *arg_type;
2352 u32 arg_type_id;
2353
2354 arg_type_id = args[i].type;
2355 arg_type = btf_type_by_id(btf, arg_type_id);
2356 if (!arg_type) {
2357 btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1);
2358 err = -EINVAL;
2359 break;
2360 }
2361
2362 if (args[i].name_off &&
2363 (!btf_name_offset_valid(btf, args[i].name_off) ||
2364 !btf_name_valid_identifier(btf, args[i].name_off))) {
2365 btf_verifier_log_type(env, t,
2366 "Invalid arg#%u", i + 1);
2367 err = -EINVAL;
2368 break;
2369 }
2370
2371 if (btf_type_needs_resolve(arg_type) &&
2372 !env_type_is_resolved(env, arg_type_id)) {
2373 err = btf_resolve(env, arg_type, arg_type_id);
2374 if (err)
2375 break;
2376 }
2377
2378 if (!btf_type_id_size(btf, &arg_type_id, NULL)) {
2379 btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1);
2380 err = -EINVAL;
2381 break;
2382 }
2383 }
2384
2385 return err;
2386}
2387
2388static int btf_func_check(struct btf_verifier_env *env,
2389 const struct btf_type *t)
2390{
2391 const struct btf_type *proto_type;
2392 const struct btf_param *args;
2393 const struct btf *btf;
2394 u16 nr_args, i;
2395
2396 btf = env->btf;
2397 proto_type = btf_type_by_id(btf, t->type);
2398
2399 if (!proto_type || !btf_type_is_func_proto(proto_type)) {
2400 btf_verifier_log_type(env, t, "Invalid type_id");
2401 return -EINVAL;
2402 }
2403
2404 args = (const struct btf_param *)(proto_type + 1);
2405 nr_args = btf_type_vlen(proto_type);
2406 for (i = 0; i < nr_args; i++) {
2407 if (!args[i].name_off && args[i].type) {
2408 btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1);
2409 return -EINVAL;
2410 }
2411 }
2412
2413 return 0;
2414}
2415
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002416static const struct btf_kind_operations * const kind_ops[NR_BTF_KINDS] = {
2417 [BTF_KIND_INT] = &int_ops,
2418 [BTF_KIND_PTR] = &ptr_ops,
2419 [BTF_KIND_ARRAY] = &array_ops,
2420 [BTF_KIND_STRUCT] = &struct_ops,
2421 [BTF_KIND_UNION] = &struct_ops,
2422 [BTF_KIND_ENUM] = &enum_ops,
2423 [BTF_KIND_FWD] = &fwd_ops,
2424 [BTF_KIND_TYPEDEF] = &modifier_ops,
2425 [BTF_KIND_VOLATILE] = &modifier_ops,
2426 [BTF_KIND_CONST] = &modifier_ops,
2427 [BTF_KIND_RESTRICT] = &modifier_ops,
Martin KaFai Lau2667a262018-11-19 15:29:08 -08002428 [BTF_KIND_FUNC] = &func_ops,
2429 [BTF_KIND_FUNC_PROTO] = &func_proto_ops,
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002430};
2431
2432static s32 btf_check_meta(struct btf_verifier_env *env,
2433 const struct btf_type *t,
2434 u32 meta_left)
2435{
2436 u32 saved_meta_left = meta_left;
2437 s32 var_meta_size;
2438
2439 if (meta_left < sizeof(*t)) {
2440 btf_verifier_log(env, "[%u] meta_left:%u meta_needed:%zu",
2441 env->log_type_id, meta_left, sizeof(*t));
2442 return -EINVAL;
2443 }
2444 meta_left -= sizeof(*t);
2445
Martin KaFai Lauaea2f7b82018-05-22 14:57:20 -07002446 if (t->info & ~BTF_INFO_MASK) {
2447 btf_verifier_log(env, "[%u] Invalid btf_info:%x",
2448 env->log_type_id, t->info);
2449 return -EINVAL;
2450 }
2451
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002452 if (BTF_INFO_KIND(t->info) > BTF_KIND_MAX ||
2453 BTF_INFO_KIND(t->info) == BTF_KIND_UNKN) {
2454 btf_verifier_log(env, "[%u] Invalid kind:%u",
2455 env->log_type_id, BTF_INFO_KIND(t->info));
2456 return -EINVAL;
2457 }
2458
Martin KaFai Laufbcf93e2018-04-21 09:48:23 -07002459 if (!btf_name_offset_valid(env->btf, t->name_off)) {
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002460 btf_verifier_log(env, "[%u] Invalid name_offset:%u",
Martin KaFai Laufbcf93e2018-04-21 09:48:23 -07002461 env->log_type_id, t->name_off);
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002462 return -EINVAL;
2463 }
2464
2465 var_meta_size = btf_type_ops(t)->check_meta(env, t, meta_left);
2466 if (var_meta_size < 0)
2467 return var_meta_size;
2468
2469 meta_left -= var_meta_size;
2470
2471 return saved_meta_left - meta_left;
2472}
2473
2474static int btf_check_all_metas(struct btf_verifier_env *env)
2475{
2476 struct btf *btf = env->btf;
2477 struct btf_header *hdr;
2478 void *cur, *end;
2479
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07002480 hdr = &btf->hdr;
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002481 cur = btf->nohdr_data + hdr->type_off;
Martin KaFai Lau4b1c5d92018-09-12 10:29:11 -07002482 end = cur + hdr->type_len;
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002483
2484 env->log_type_id = 1;
2485 while (cur < end) {
2486 struct btf_type *t = cur;
2487 s32 meta_size;
2488
2489 meta_size = btf_check_meta(env, t, end - cur);
2490 if (meta_size < 0)
2491 return meta_size;
2492
2493 btf_add_type(env, t);
2494 cur += meta_size;
2495 env->log_type_id++;
2496 }
2497
2498 return 0;
2499}
2500
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07002501static bool btf_resolve_valid(struct btf_verifier_env *env,
2502 const struct btf_type *t,
2503 u32 type_id)
2504{
2505 struct btf *btf = env->btf;
2506
2507 if (!env_type_is_resolved(env, type_id))
2508 return false;
2509
2510 if (btf_type_is_struct(t))
2511 return !btf->resolved_ids[type_id] &&
2512 !btf->resolved_sizes[type_id];
2513
2514 if (btf_type_is_modifier(t) || btf_type_is_ptr(t)) {
2515 t = btf_type_id_resolve(btf, &type_id);
2516 return t && !btf_type_is_modifier(t);
2517 }
2518
2519 if (btf_type_is_array(t)) {
2520 const struct btf_array *array = btf_type_array(t);
2521 const struct btf_type *elem_type;
2522 u32 elem_type_id = array->type;
2523 u32 elem_size;
2524
2525 elem_type = btf_type_id_size(btf, &elem_type_id, &elem_size);
2526 return elem_type && !btf_type_is_modifier(elem_type) &&
2527 (array->nelems * elem_size ==
2528 btf->resolved_sizes[type_id]);
2529 }
2530
2531 return false;
2532}
2533
Martin KaFai Lau2667a262018-11-19 15:29:08 -08002534static int btf_resolve(struct btf_verifier_env *env,
2535 const struct btf_type *t, u32 type_id)
2536{
2537 u32 save_log_type_id = env->log_type_id;
2538 const struct resolve_vertex *v;
2539 int err = 0;
2540
2541 env->resolve_mode = RESOLVE_TBD;
2542 env_stack_push(env, t, type_id);
2543 while (!err && (v = env_stack_peak(env))) {
2544 env->log_type_id = v->type_id;
2545 err = btf_type_ops(v->t)->resolve(env, v);
2546 }
2547
2548 env->log_type_id = type_id;
2549 if (err == -E2BIG) {
2550 btf_verifier_log_type(env, t,
2551 "Exceeded max resolving depth:%u",
2552 MAX_RESOLVE_DEPTH);
2553 } else if (err == -EEXIST) {
2554 btf_verifier_log_type(env, t, "Loop detected");
2555 }
2556
2557 /* Final sanity check */
2558 if (!err && !btf_resolve_valid(env, t, type_id)) {
2559 btf_verifier_log_type(env, t, "Invalid resolve state");
2560 err = -EINVAL;
2561 }
2562
2563 env->log_type_id = save_log_type_id;
2564 return err;
2565}
2566
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07002567static int btf_check_all_types(struct btf_verifier_env *env)
2568{
2569 struct btf *btf = env->btf;
2570 u32 type_id;
2571 int err;
2572
2573 err = env_resolve_init(env);
2574 if (err)
2575 return err;
2576
2577 env->phase++;
2578 for (type_id = 1; type_id <= btf->nr_types; type_id++) {
2579 const struct btf_type *t = btf_type_by_id(btf, type_id);
2580
2581 env->log_type_id = type_id;
2582 if (btf_type_needs_resolve(t) &&
2583 !env_type_is_resolved(env, type_id)) {
2584 err = btf_resolve(env, t, type_id);
2585 if (err)
2586 return err;
2587 }
2588
Martin KaFai Lau2667a262018-11-19 15:29:08 -08002589 if (btf_type_is_func_proto(t)) {
2590 err = btf_func_proto_check(env, t);
2591 if (err)
2592 return err;
2593 }
2594
2595 if (btf_type_is_func(t)) {
2596 err = btf_func_check(env, t);
2597 if (err)
2598 return err;
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07002599 }
2600 }
2601
2602 return 0;
2603}
2604
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002605static int btf_parse_type_sec(struct btf_verifier_env *env)
2606{
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07002607 const struct btf_header *hdr = &env->btf->hdr;
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07002608 int err;
2609
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07002610 /* Type section must align to 4 bytes */
2611 if (hdr->type_off & (sizeof(u32) - 1)) {
2612 btf_verifier_log(env, "Unaligned type_off");
2613 return -EINVAL;
2614 }
2615
2616 if (!hdr->type_len) {
2617 btf_verifier_log(env, "No type found");
2618 return -EINVAL;
2619 }
2620
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07002621 err = btf_check_all_metas(env);
2622 if (err)
2623 return err;
2624
2625 return btf_check_all_types(env);
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002626}
2627
2628static int btf_parse_str_sec(struct btf_verifier_env *env)
2629{
2630 const struct btf_header *hdr;
2631 struct btf *btf = env->btf;
2632 const char *start, *end;
2633
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07002634 hdr = &btf->hdr;
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002635 start = btf->nohdr_data + hdr->str_off;
2636 end = start + hdr->str_len;
2637
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07002638 if (end != btf->data + btf->data_size) {
2639 btf_verifier_log(env, "String section is not at the end");
2640 return -EINVAL;
2641 }
2642
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002643 if (!hdr->str_len || hdr->str_len - 1 > BTF_MAX_NAME_OFFSET ||
2644 start[0] || end[-1]) {
2645 btf_verifier_log(env, "Invalid string section");
2646 return -EINVAL;
2647 }
2648
2649 btf->strings = start;
2650
2651 return 0;
2652}
2653
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07002654static const size_t btf_sec_info_offset[] = {
2655 offsetof(struct btf_header, type_off),
2656 offsetof(struct btf_header, str_off),
2657};
2658
2659static int btf_sec_info_cmp(const void *a, const void *b)
2660{
2661 const struct btf_sec_info *x = a;
2662 const struct btf_sec_info *y = b;
2663
2664 return (int)(x->off - y->off) ? : (int)(x->len - y->len);
2665}
2666
2667static int btf_check_sec_info(struct btf_verifier_env *env,
2668 u32 btf_data_size)
2669{
Martin KaFai Laua2889a42018-05-23 11:32:36 -07002670 struct btf_sec_info secs[ARRAY_SIZE(btf_sec_info_offset)];
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07002671 u32 total, expected_total, i;
2672 const struct btf_header *hdr;
2673 const struct btf *btf;
2674
2675 btf = env->btf;
2676 hdr = &btf->hdr;
2677
2678 /* Populate the secs from hdr */
Martin KaFai Laua2889a42018-05-23 11:32:36 -07002679 for (i = 0; i < ARRAY_SIZE(btf_sec_info_offset); i++)
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07002680 secs[i] = *(struct btf_sec_info *)((void *)hdr +
2681 btf_sec_info_offset[i]);
2682
Martin KaFai Laua2889a42018-05-23 11:32:36 -07002683 sort(secs, ARRAY_SIZE(btf_sec_info_offset),
2684 sizeof(struct btf_sec_info), btf_sec_info_cmp, NULL);
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07002685
2686 /* Check for gaps and overlap among sections */
2687 total = 0;
2688 expected_total = btf_data_size - hdr->hdr_len;
Martin KaFai Laua2889a42018-05-23 11:32:36 -07002689 for (i = 0; i < ARRAY_SIZE(btf_sec_info_offset); i++) {
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07002690 if (expected_total < secs[i].off) {
2691 btf_verifier_log(env, "Invalid section offset");
2692 return -EINVAL;
2693 }
2694 if (total < secs[i].off) {
2695 /* gap */
2696 btf_verifier_log(env, "Unsupported section found");
2697 return -EINVAL;
2698 }
2699 if (total > secs[i].off) {
2700 btf_verifier_log(env, "Section overlap found");
2701 return -EINVAL;
2702 }
2703 if (expected_total - total < secs[i].len) {
2704 btf_verifier_log(env,
2705 "Total section length too long");
2706 return -EINVAL;
2707 }
2708 total += secs[i].len;
2709 }
2710
2711 /* There is data other than hdr and known sections */
2712 if (expected_total != total) {
2713 btf_verifier_log(env, "Unsupported section found");
2714 return -EINVAL;
2715 }
2716
2717 return 0;
2718}
2719
Martin Lau4a6998a2018-10-24 20:42:25 +00002720static int btf_parse_hdr(struct btf_verifier_env *env)
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002721{
Martin Lau4a6998a2018-10-24 20:42:25 +00002722 u32 hdr_len, hdr_copy, btf_data_size;
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002723 const struct btf_header *hdr;
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07002724 struct btf *btf;
2725 int err;
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002726
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07002727 btf = env->btf;
Martin Lau4a6998a2018-10-24 20:42:25 +00002728 btf_data_size = btf->data_size;
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07002729
Martin Lau4a6998a2018-10-24 20:42:25 +00002730 if (btf_data_size <
2731 offsetof(struct btf_header, hdr_len) + sizeof(hdr->hdr_len)) {
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07002732 btf_verifier_log(env, "hdr_len not found");
2733 return -EINVAL;
2734 }
2735
Martin Lau4a6998a2018-10-24 20:42:25 +00002736 hdr = btf->data;
2737 hdr_len = hdr->hdr_len;
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07002738 if (btf_data_size < hdr_len) {
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002739 btf_verifier_log(env, "btf_header not found");
2740 return -EINVAL;
2741 }
2742
Martin Lau4a6998a2018-10-24 20:42:25 +00002743 /* Ensure the unsupported header fields are zero */
2744 if (hdr_len > sizeof(btf->hdr)) {
2745 u8 *expected_zero = btf->data + sizeof(btf->hdr);
2746 u8 *end = btf->data + hdr_len;
2747
2748 for (; expected_zero < end; expected_zero++) {
2749 if (*expected_zero) {
2750 btf_verifier_log(env, "Unsupported btf_header");
2751 return -E2BIG;
2752 }
2753 }
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07002754 }
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002755
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07002756 hdr_copy = min_t(u32, hdr_len, sizeof(btf->hdr));
Martin Lau4a6998a2018-10-24 20:42:25 +00002757 memcpy(&btf->hdr, btf->data, hdr_copy);
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07002758
2759 hdr = &btf->hdr;
2760
2761 btf_verifier_log_hdr(env, btf_data_size);
2762
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002763 if (hdr->magic != BTF_MAGIC) {
2764 btf_verifier_log(env, "Invalid magic");
2765 return -EINVAL;
2766 }
2767
2768 if (hdr->version != BTF_VERSION) {
2769 btf_verifier_log(env, "Unsupported version");
2770 return -ENOTSUPP;
2771 }
2772
2773 if (hdr->flags) {
2774 btf_verifier_log(env, "Unsupported flags");
2775 return -ENOTSUPP;
2776 }
2777
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07002778 if (btf_data_size == hdr->hdr_len) {
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002779 btf_verifier_log(env, "No data");
2780 return -EINVAL;
2781 }
2782
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07002783 err = btf_check_sec_info(env, btf_data_size);
2784 if (err)
2785 return err;
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002786
2787 return 0;
2788}
2789
2790static struct btf *btf_parse(void __user *btf_data, u32 btf_data_size,
2791 u32 log_level, char __user *log_ubuf, u32 log_size)
2792{
2793 struct btf_verifier_env *env = NULL;
2794 struct bpf_verifier_log *log;
2795 struct btf *btf = NULL;
2796 u8 *data;
2797 int err;
2798
2799 if (btf_data_size > BTF_MAX_SIZE)
2800 return ERR_PTR(-E2BIG);
2801
2802 env = kzalloc(sizeof(*env), GFP_KERNEL | __GFP_NOWARN);
2803 if (!env)
2804 return ERR_PTR(-ENOMEM);
2805
2806 log = &env->log;
2807 if (log_level || log_ubuf || log_size) {
2808 /* user requested verbose verifier output
2809 * and supplied buffer to store the verification trace
2810 */
2811 log->level = log_level;
2812 log->ubuf = log_ubuf;
2813 log->len_total = log_size;
2814
2815 /* log attributes have to be sane */
2816 if (log->len_total < 128 || log->len_total > UINT_MAX >> 8 ||
2817 !log->level || !log->ubuf) {
2818 err = -EINVAL;
2819 goto errout;
2820 }
2821 }
2822
2823 btf = kzalloc(sizeof(*btf), GFP_KERNEL | __GFP_NOWARN);
2824 if (!btf) {
2825 err = -ENOMEM;
2826 goto errout;
2827 }
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07002828 env->btf = btf;
2829
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002830 data = kvmalloc(btf_data_size, GFP_KERNEL | __GFP_NOWARN);
2831 if (!data) {
2832 err = -ENOMEM;
2833 goto errout;
2834 }
2835
2836 btf->data = data;
2837 btf->data_size = btf_data_size;
2838
2839 if (copy_from_user(data, btf_data, btf_data_size)) {
2840 err = -EFAULT;
2841 goto errout;
2842 }
2843
Martin Lau4a6998a2018-10-24 20:42:25 +00002844 err = btf_parse_hdr(env);
2845 if (err)
2846 goto errout;
2847
2848 btf->nohdr_data = btf->data + btf->hdr.hdr_len;
2849
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002850 err = btf_parse_str_sec(env);
2851 if (err)
2852 goto errout;
2853
2854 err = btf_parse_type_sec(env);
2855 if (err)
2856 goto errout;
2857
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07002858 if (log->level && bpf_verifier_log_full(log)) {
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002859 err = -ENOSPC;
2860 goto errout;
2861 }
2862
Martin KaFai Lauf80442a2018-05-22 14:57:18 -07002863 btf_verifier_env_free(env);
2864 refcount_set(&btf->refcnt, 1);
2865 return btf;
Martin KaFai Lau69b693f2018-04-18 15:55:57 -07002866
2867errout:
2868 btf_verifier_env_free(env);
2869 if (btf)
2870 btf_free(btf);
2871 return ERR_PTR(err);
2872}
Martin KaFai Laub00b8da2018-04-18 15:56:00 -07002873
2874void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj,
2875 struct seq_file *m)
2876{
2877 const struct btf_type *t = btf_type_by_id(btf, type_id);
2878
2879 btf_type_ops(t)->seq_show(btf, t, type_id, obj, 0, m);
2880}
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07002881
2882static int btf_release(struct inode *inode, struct file *filp)
2883{
2884 btf_put(filp->private_data);
2885 return 0;
2886}
2887
Martin KaFai Lau60197cf2018-04-18 15:56:02 -07002888const struct file_operations btf_fops = {
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07002889 .release = btf_release,
2890};
2891
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07002892static int __btf_new_fd(struct btf *btf)
2893{
2894 return anon_inode_getfd("btf", &btf_fops, btf, O_RDONLY | O_CLOEXEC);
2895}
2896
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07002897int btf_new_fd(const union bpf_attr *attr)
2898{
2899 struct btf *btf;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07002900 int ret;
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07002901
2902 btf = btf_parse(u64_to_user_ptr(attr->btf),
2903 attr->btf_size, attr->btf_log_level,
2904 u64_to_user_ptr(attr->btf_log_buf),
2905 attr->btf_log_size);
2906 if (IS_ERR(btf))
2907 return PTR_ERR(btf);
2908
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07002909 ret = btf_alloc_id(btf);
2910 if (ret) {
2911 btf_free(btf);
2912 return ret;
2913 }
2914
2915 /*
2916 * The BTF ID is published to the userspace.
2917 * All BTF free must go through call_rcu() from
2918 * now on (i.e. free by calling btf_put()).
2919 */
2920
2921 ret = __btf_new_fd(btf);
2922 if (ret < 0)
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07002923 btf_put(btf);
2924
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07002925 return ret;
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07002926}
2927
2928struct btf *btf_get_by_fd(int fd)
2929{
2930 struct btf *btf;
2931 struct fd f;
2932
2933 f = fdget(fd);
2934
2935 if (!f.file)
2936 return ERR_PTR(-EBADF);
2937
2938 if (f.file->f_op != &btf_fops) {
2939 fdput(f);
2940 return ERR_PTR(-EINVAL);
2941 }
2942
2943 btf = f.file->private_data;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07002944 refcount_inc(&btf->refcnt);
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07002945 fdput(f);
2946
2947 return btf;
2948}
Martin KaFai Lau60197cf2018-04-18 15:56:02 -07002949
2950int btf_get_info_by_fd(const struct btf *btf,
2951 const union bpf_attr *attr,
2952 union bpf_attr __user *uattr)
2953{
Martin KaFai Lau62dab842018-05-04 14:49:52 -07002954 struct bpf_btf_info __user *uinfo;
2955 struct bpf_btf_info info = {};
2956 u32 info_copy, btf_copy;
2957 void __user *ubtf;
2958 u32 uinfo_len;
Martin KaFai Lau60197cf2018-04-18 15:56:02 -07002959
Martin KaFai Lau62dab842018-05-04 14:49:52 -07002960 uinfo = u64_to_user_ptr(attr->info.info);
2961 uinfo_len = attr->info.info_len;
2962
2963 info_copy = min_t(u32, uinfo_len, sizeof(info));
2964 if (copy_from_user(&info, uinfo, info_copy))
2965 return -EFAULT;
2966
2967 info.id = btf->id;
2968 ubtf = u64_to_user_ptr(info.btf);
2969 btf_copy = min_t(u32, btf->data_size, info.btf_size);
2970 if (copy_to_user(ubtf, btf->data, btf_copy))
2971 return -EFAULT;
2972 info.btf_size = btf->data_size;
2973
2974 if (copy_to_user(uinfo, &info, info_copy) ||
2975 put_user(info_copy, &uattr->info.info_len))
Martin KaFai Lau60197cf2018-04-18 15:56:02 -07002976 return -EFAULT;
2977
2978 return 0;
2979}
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07002980
2981int btf_get_fd_by_id(u32 id)
2982{
2983 struct btf *btf;
2984 int fd;
2985
2986 rcu_read_lock();
2987 btf = idr_find(&btf_idr, id);
2988 if (!btf || !refcount_inc_not_zero(&btf->refcnt))
2989 btf = ERR_PTR(-ENOENT);
2990 rcu_read_unlock();
2991
2992 if (IS_ERR(btf))
2993 return PTR_ERR(btf);
2994
2995 fd = __btf_new_fd(btf);
2996 if (fd < 0)
2997 btf_put(btf);
2998
2999 return fd;
3000}
3001
3002u32 btf_id(const struct btf *btf)
3003{
3004 return btf->id;
3005}