Commit | Line | Data |
---|---|---|
69b693f0 MKL |
1 | /* SPDX-License-Identifier: GPL-2.0 */ |
2 | /* Copyright (c) 2018 Facebook */ | |
3 | ||
4 | #include <uapi/linux/btf.h> | |
5 | #include <uapi/linux/types.h> | |
b00b8dae | 6 | #include <linux/seq_file.h> |
69b693f0 | 7 | #include <linux/compiler.h> |
2667a262 | 8 | #include <linux/ctype.h> |
69b693f0 MKL |
9 | #include <linux/errno.h> |
10 | #include <linux/slab.h> | |
f56a653c MKL |
11 | #include <linux/anon_inodes.h> |
12 | #include <linux/file.h> | |
69b693f0 MKL |
13 | #include <linux/uaccess.h> |
14 | #include <linux/kernel.h> | |
78958fca | 15 | #include <linux/idr.h> |
f80442a4 | 16 | #include <linux/sort.h> |
69b693f0 MKL |
17 | #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). | |
eb3f595d MKL |
114 | * |
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 | * | |
69b693f0 MKL |
158 | */ |
159 | ||
b1e8818c | 160 | #define BITS_PER_U128 (sizeof(u64) * BITS_PER_BYTE * 2) |
69b693f0 MKL |
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 | ||
9d5f9f70 | 167 | #define BTF_INFO_MASK 0x8f00ffff |
aea2f7b8 MKL |
168 | #define BTF_INT_MASK 0x0fffffff |
169 | #define BTF_TYPE_ID_VALID(type_id) ((type_id) <= BTF_MAX_TYPE) | |
170 | #define BTF_STR_OFFSET_VALID(name_off) ((name_off) <= BTF_MAX_NAME_OFFSET) | |
171 | ||
69b693f0 MKL |
172 | /* 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) | |
69b693f0 MKL |
177 | |
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 | ||
eb3f595d MKL |
183 | #define for_each_member_from(i, from, struct_type, member) \ |
184 | for (i = from, member = btf_type_member(struct_type) + from; \ | |
185 | i < btf_type_vlen(struct_type); \ | |
186 | i++, member++) | |
187 | ||
78958fca MKL |
188 | static DEFINE_IDR(btf_idr); |
189 | static DEFINE_SPINLOCK(btf_idr_lock); | |
190 | ||
69b693f0 | 191 | struct btf { |
f80442a4 | 192 | void *data; |
69b693f0 | 193 | struct btf_type **types; |
eb3f595d MKL |
194 | u32 *resolved_ids; |
195 | u32 *resolved_sizes; | |
69b693f0 MKL |
196 | const char *strings; |
197 | void *nohdr_data; | |
f80442a4 | 198 | struct btf_header hdr; |
69b693f0 MKL |
199 | u32 nr_types; |
200 | u32 types_size; | |
201 | u32 data_size; | |
f56a653c | 202 | refcount_t refcnt; |
78958fca MKL |
203 | u32 id; |
204 | struct rcu_head rcu; | |
69b693f0 MKL |
205 | }; |
206 | ||
eb3f595d MKL |
207 | enum verifier_phase { |
208 | CHECK_META, | |
209 | CHECK_TYPE, | |
210 | }; | |
211 | ||
212 | struct resolve_vertex { | |
213 | const struct btf_type *t; | |
214 | u32 type_id; | |
215 | u16 next_member; | |
216 | }; | |
217 | ||
218 | enum visit_state { | |
219 | NOT_VISITED, | |
220 | VISITED, | |
221 | RESOLVED, | |
222 | }; | |
223 | ||
224 | enum 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 | ||
f80442a4 MKL |
234 | struct btf_sec_info { |
235 | u32 off; | |
236 | u32 len; | |
237 | }; | |
238 | ||
69b693f0 MKL |
239 | struct btf_verifier_env { |
240 | struct btf *btf; | |
eb3f595d MKL |
241 | u8 *visit_states; |
242 | struct resolve_vertex stack[MAX_RESOLVE_DEPTH]; | |
69b693f0 MKL |
243 | struct bpf_verifier_log log; |
244 | u32 log_type_id; | |
eb3f595d MKL |
245 | u32 top_stack; |
246 | enum verifier_phase phase; | |
247 | enum resolve_mode resolve_mode; | |
69b693f0 MKL |
248 | }; |
249 | ||
250 | static 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", | |
2667a262 MKL |
263 | [BTF_KIND_FUNC] = "FUNC", |
264 | [BTF_KIND_FUNC_PROTO] = "FUNC_PROTO", | |
69b693f0 MKL |
265 | }; |
266 | ||
267 | struct btf_kind_operations { | |
268 | s32 (*check_meta)(struct btf_verifier_env *env, | |
269 | const struct btf_type *t, | |
270 | u32 meta_left); | |
eb3f595d MKL |
271 | int (*resolve)(struct btf_verifier_env *env, |
272 | const struct resolve_vertex *v); | |
179cde8c MKL |
273 | 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); | |
9d5f9f70 YS |
277 | 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); | |
69b693f0 MKL |
281 | void (*log_details)(struct btf_verifier_env *env, |
282 | const struct btf_type *t); | |
b00b8dae MKL |
283 | 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); | |
69b693f0 MKL |
286 | }; |
287 | ||
288 | static const struct btf_kind_operations * const kind_ops[NR_BTF_KINDS]; | |
289 | static struct btf_type btf_void; | |
290 | ||
2667a262 MKL |
291 | static int btf_resolve(struct btf_verifier_env *env, |
292 | const struct btf_type *t, u32 type_id); | |
293 | ||
eb3f595d MKL |
294 | static 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 | ||
317 | static bool btf_type_is_void(const struct btf_type *t) | |
318 | { | |
b47a0bd2 MKL |
319 | return t == &btf_void; |
320 | } | |
321 | ||
322 | static bool btf_type_is_fwd(const struct btf_type *t) | |
323 | { | |
324 | return BTF_INFO_KIND(t->info) == BTF_KIND_FWD; | |
325 | } | |
326 | ||
2667a262 MKL |
327 | static bool btf_type_is_func(const struct btf_type *t) |
328 | { | |
329 | return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC; | |
330 | } | |
331 | ||
332 | static 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 | ||
b47a0bd2 MKL |
337 | static bool btf_type_nosize(const struct btf_type *t) |
338 | { | |
2667a262 MKL |
339 | return btf_type_is_void(t) || btf_type_is_fwd(t) || |
340 | btf_type_is_func(t) || btf_type_is_func_proto(t); | |
eb3f595d MKL |
341 | } |
342 | ||
b47a0bd2 | 343 | static bool btf_type_nosize_or_null(const struct btf_type *t) |
eb3f595d | 344 | { |
b47a0bd2 | 345 | return !t || btf_type_nosize(t); |
eb3f595d MKL |
346 | } |
347 | ||
348 | /* union is only a special case of struct: | |
349 | * all its offsetof(member) == 0 | |
350 | */ | |
351 | static 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 | ||
d83525ca AS |
358 | static bool __btf_type_is_struct(const struct btf_type *t) |
359 | { | |
360 | return BTF_INFO_KIND(t->info) == BTF_KIND_STRUCT; | |
361 | } | |
362 | ||
eb3f595d MKL |
363 | static bool btf_type_is_array(const struct btf_type *t) |
364 | { | |
365 | return BTF_INFO_KIND(t->info) == BTF_KIND_ARRAY; | |
366 | } | |
367 | ||
368 | static bool btf_type_is_ptr(const struct btf_type *t) | |
369 | { | |
370 | return BTF_INFO_KIND(t->info) == BTF_KIND_PTR; | |
371 | } | |
372 | ||
373 | static bool btf_type_is_int(const struct btf_type *t) | |
374 | { | |
375 | return BTF_INFO_KIND(t->info) == BTF_KIND_INT; | |
376 | } | |
377 | ||
378 | /* What types need to be resolved? | |
379 | * | |
380 | * btf_type_is_modifier() is an obvious one. | |
381 | * | |
382 | * btf_type_is_struct() because its member refers to | |
383 | * another type (through member->type). | |
384 | ||
385 | * btf_type_is_array() because its element (array->type) | |
386 | * refers to another type. Array can be thought of a | |
387 | * special case of struct while array just has the same | |
388 | * member-type repeated by array->nelems of times. | |
389 | */ | |
390 | static bool btf_type_needs_resolve(const struct btf_type *t) | |
391 | { | |
392 | return btf_type_is_modifier(t) || | |
393 | btf_type_is_ptr(t) || | |
394 | btf_type_is_struct(t) || | |
395 | btf_type_is_array(t); | |
396 | } | |
397 | ||
398 | /* t->size can be used */ | |
399 | static bool btf_type_has_size(const struct btf_type *t) | |
400 | { | |
401 | switch (BTF_INFO_KIND(t->info)) { | |
402 | case BTF_KIND_INT: | |
403 | case BTF_KIND_STRUCT: | |
404 | case BTF_KIND_UNION: | |
405 | case BTF_KIND_ENUM: | |
406 | return true; | |
407 | } | |
408 | ||
409 | return false; | |
410 | } | |
411 | ||
69b693f0 MKL |
412 | static const char *btf_int_encoding_str(u8 encoding) |
413 | { | |
414 | if (encoding == 0) | |
415 | return "(none)"; | |
416 | else if (encoding == BTF_INT_SIGNED) | |
417 | return "SIGNED"; | |
418 | else if (encoding == BTF_INT_CHAR) | |
419 | return "CHAR"; | |
420 | else if (encoding == BTF_INT_BOOL) | |
421 | return "BOOL"; | |
69b693f0 MKL |
422 | else |
423 | return "UNKN"; | |
424 | } | |
425 | ||
426 | static u16 btf_type_vlen(const struct btf_type *t) | |
427 | { | |
428 | return BTF_INFO_VLEN(t->info); | |
429 | } | |
430 | ||
9d5f9f70 YS |
431 | static bool btf_type_kflag(const struct btf_type *t) |
432 | { | |
433 | return BTF_INFO_KFLAG(t->info); | |
434 | } | |
435 | ||
436 | static u32 btf_member_bit_offset(const struct btf_type *struct_type, | |
437 | const struct btf_member *member) | |
438 | { | |
439 | return btf_type_kflag(struct_type) ? BTF_MEMBER_BIT_OFFSET(member->offset) | |
440 | : member->offset; | |
441 | } | |
442 | ||
443 | static u32 btf_member_bitfield_size(const struct btf_type *struct_type, | |
444 | const struct btf_member *member) | |
445 | { | |
446 | return btf_type_kflag(struct_type) ? BTF_MEMBER_BITFIELD_SIZE(member->offset) | |
447 | : 0; | |
448 | } | |
449 | ||
69b693f0 MKL |
450 | static u32 btf_type_int(const struct btf_type *t) |
451 | { | |
452 | return *(u32 *)(t + 1); | |
453 | } | |
454 | ||
455 | static const struct btf_array *btf_type_array(const struct btf_type *t) | |
456 | { | |
457 | return (const struct btf_array *)(t + 1); | |
458 | } | |
459 | ||
460 | static const struct btf_member *btf_type_member(const struct btf_type *t) | |
461 | { | |
462 | return (const struct btf_member *)(t + 1); | |
463 | } | |
464 | ||
465 | static const struct btf_enum *btf_type_enum(const struct btf_type *t) | |
466 | { | |
467 | return (const struct btf_enum *)(t + 1); | |
468 | } | |
469 | ||
470 | static const struct btf_kind_operations *btf_type_ops(const struct btf_type *t) | |
471 | { | |
472 | return kind_ops[BTF_INFO_KIND(t->info)]; | |
473 | } | |
474 | ||
583c5318 | 475 | static bool btf_name_offset_valid(const struct btf *btf, u32 offset) |
69b693f0 | 476 | { |
aea2f7b8 MKL |
477 | return BTF_STR_OFFSET_VALID(offset) && |
478 | offset < btf->hdr.str_len; | |
69b693f0 MKL |
479 | } |
480 | ||
2667a262 MKL |
481 | /* Only C-style identifier is permitted. This can be relaxed if |
482 | * necessary. | |
483 | */ | |
484 | static bool btf_name_valid_identifier(const struct btf *btf, u32 offset) | |
485 | { | |
486 | /* offset must be valid */ | |
487 | const char *src = &btf->strings[offset]; | |
488 | const char *src_limit; | |
489 | ||
490 | if (!isalpha(*src) && *src != '_') | |
491 | return false; | |
492 | ||
493 | /* set a limit on identifier length */ | |
494 | src_limit = src + KSYM_NAME_LEN; | |
495 | src++; | |
496 | while (*src && src < src_limit) { | |
497 | if (!isalnum(*src) && *src != '_') | |
498 | return false; | |
499 | src++; | |
500 | } | |
501 | ||
502 | return !*src; | |
503 | } | |
504 | ||
23127b33 | 505 | static const char *__btf_name_by_offset(const struct btf *btf, u32 offset) |
69b693f0 | 506 | { |
aea2f7b8 | 507 | if (!offset) |
69b693f0 | 508 | return "(anon)"; |
aea2f7b8 MKL |
509 | else if (offset < btf->hdr.str_len) |
510 | return &btf->strings[offset]; | |
69b693f0 MKL |
511 | else |
512 | return "(invalid-name-offset)"; | |
513 | } | |
514 | ||
23127b33 MKL |
515 | const char *btf_name_by_offset(const struct btf *btf, u32 offset) |
516 | { | |
517 | if (offset < btf->hdr.str_len) | |
518 | return &btf->strings[offset]; | |
519 | ||
520 | return NULL; | |
521 | } | |
522 | ||
838e9690 | 523 | const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id) |
eb3f595d MKL |
524 | { |
525 | if (type_id > btf->nr_types) | |
526 | return NULL; | |
527 | ||
528 | return btf->types[type_id]; | |
529 | } | |
530 | ||
4ef5f574 MKL |
531 | /* |
532 | * Regular int is not a bit field and it must be either | |
b1e8818c | 533 | * u8/u16/u32/u64 or __int128. |
4ef5f574 MKL |
534 | */ |
535 | static bool btf_type_int_is_regular(const struct btf_type *t) | |
536 | { | |
36fc3c8c | 537 | u8 nr_bits, nr_bytes; |
4ef5f574 MKL |
538 | u32 int_data; |
539 | ||
540 | int_data = btf_type_int(t); | |
541 | nr_bits = BTF_INT_BITS(int_data); | |
542 | nr_bytes = BITS_ROUNDUP_BYTES(nr_bits); | |
543 | if (BITS_PER_BYTE_MASKED(nr_bits) || | |
544 | BTF_INT_OFFSET(int_data) || | |
545 | (nr_bytes != sizeof(u8) && nr_bytes != sizeof(u16) && | |
b1e8818c YS |
546 | nr_bytes != sizeof(u32) && nr_bytes != sizeof(u64) && |
547 | nr_bytes != (2 * sizeof(u64)))) { | |
4ef5f574 MKL |
548 | return false; |
549 | } | |
550 | ||
551 | return true; | |
552 | } | |
553 | ||
9a1126b6 | 554 | /* |
ffa0c1cf YS |
555 | * Check that given struct member is a regular int with expected |
556 | * offset and size. | |
9a1126b6 | 557 | */ |
ffa0c1cf YS |
558 | bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s, |
559 | const struct btf_member *m, | |
560 | u32 expected_offset, u32 expected_size) | |
9a1126b6 | 561 | { |
ffa0c1cf YS |
562 | const struct btf_type *t; |
563 | u32 id, int_data; | |
564 | u8 nr_bits; | |
9a1126b6 | 565 | |
ffa0c1cf YS |
566 | id = m->type; |
567 | t = btf_type_id_size(btf, &id, NULL); | |
568 | if (!t || !btf_type_is_int(t)) | |
9a1126b6 RG |
569 | return false; |
570 | ||
571 | int_data = btf_type_int(t); | |
572 | nr_bits = BTF_INT_BITS(int_data); | |
ffa0c1cf YS |
573 | if (btf_type_kflag(s)) { |
574 | u32 bitfield_size = BTF_MEMBER_BITFIELD_SIZE(m->offset); | |
575 | u32 bit_offset = BTF_MEMBER_BIT_OFFSET(m->offset); | |
576 | ||
577 | /* if kflag set, int should be a regular int and | |
578 | * bit offset should be at byte boundary. | |
579 | */ | |
580 | return !bitfield_size && | |
581 | BITS_ROUNDUP_BYTES(bit_offset) == expected_offset && | |
582 | BITS_ROUNDUP_BYTES(nr_bits) == expected_size; | |
583 | } | |
584 | ||
585 | if (BTF_INT_OFFSET(int_data) || | |
586 | BITS_PER_BYTE_MASKED(m->offset) || | |
587 | BITS_ROUNDUP_BYTES(m->offset) != expected_offset || | |
588 | BITS_PER_BYTE_MASKED(nr_bits) || | |
589 | BITS_ROUNDUP_BYTES(nr_bits) != expected_size) | |
9a1126b6 RG |
590 | return false; |
591 | ||
592 | return true; | |
593 | } | |
594 | ||
69b693f0 MKL |
595 | __printf(2, 3) static void __btf_verifier_log(struct bpf_verifier_log *log, |
596 | const char *fmt, ...) | |
597 | { | |
598 | va_list args; | |
599 | ||
600 | va_start(args, fmt); | |
601 | bpf_verifier_vlog(log, fmt, args); | |
602 | va_end(args); | |
603 | } | |
604 | ||
605 | __printf(2, 3) static void btf_verifier_log(struct btf_verifier_env *env, | |
606 | const char *fmt, ...) | |
607 | { | |
608 | struct bpf_verifier_log *log = &env->log; | |
609 | va_list args; | |
610 | ||
611 | if (!bpf_verifier_log_needed(log)) | |
612 | return; | |
613 | ||
614 | va_start(args, fmt); | |
615 | bpf_verifier_vlog(log, fmt, args); | |
616 | va_end(args); | |
617 | } | |
618 | ||
619 | __printf(4, 5) static void __btf_verifier_log_type(struct btf_verifier_env *env, | |
620 | const struct btf_type *t, | |
621 | bool log_details, | |
622 | const char *fmt, ...) | |
623 | { | |
624 | struct bpf_verifier_log *log = &env->log; | |
625 | u8 kind = BTF_INFO_KIND(t->info); | |
626 | struct btf *btf = env->btf; | |
627 | va_list args; | |
628 | ||
629 | if (!bpf_verifier_log_needed(log)) | |
630 | return; | |
631 | ||
632 | __btf_verifier_log(log, "[%u] %s %s%s", | |
633 | env->log_type_id, | |
634 | btf_kind_str[kind], | |
23127b33 | 635 | __btf_name_by_offset(btf, t->name_off), |
69b693f0 MKL |
636 | log_details ? " " : ""); |
637 | ||
638 | if (log_details) | |
639 | btf_type_ops(t)->log_details(env, t); | |
640 | ||
641 | if (fmt && *fmt) { | |
642 | __btf_verifier_log(log, " "); | |
643 | va_start(args, fmt); | |
644 | bpf_verifier_vlog(log, fmt, args); | |
645 | va_end(args); | |
646 | } | |
647 | ||
648 | __btf_verifier_log(log, "\n"); | |
649 | } | |
650 | ||
651 | #define btf_verifier_log_type(env, t, ...) \ | |
652 | __btf_verifier_log_type((env), (t), true, __VA_ARGS__) | |
653 | #define btf_verifier_log_basic(env, t, ...) \ | |
654 | __btf_verifier_log_type((env), (t), false, __VA_ARGS__) | |
655 | ||
656 | __printf(4, 5) | |
657 | static void btf_verifier_log_member(struct btf_verifier_env *env, | |
658 | const struct btf_type *struct_type, | |
659 | const struct btf_member *member, | |
660 | const char *fmt, ...) | |
661 | { | |
662 | struct bpf_verifier_log *log = &env->log; | |
663 | struct btf *btf = env->btf; | |
664 | va_list args; | |
665 | ||
666 | if (!bpf_verifier_log_needed(log)) | |
667 | return; | |
668 | ||
eb3f595d MKL |
669 | /* The CHECK_META phase already did a btf dump. |
670 | * | |
671 | * If member is logged again, it must hit an error in | |
672 | * parsing this member. It is useful to print out which | |
673 | * struct this member belongs to. | |
674 | */ | |
675 | if (env->phase != CHECK_META) | |
676 | btf_verifier_log_type(env, struct_type, NULL); | |
677 | ||
9d5f9f70 YS |
678 | if (btf_type_kflag(struct_type)) |
679 | __btf_verifier_log(log, | |
680 | "\t%s type_id=%u bitfield_size=%u bits_offset=%u", | |
681 | __btf_name_by_offset(btf, member->name_off), | |
682 | member->type, | |
683 | BTF_MEMBER_BITFIELD_SIZE(member->offset), | |
684 | BTF_MEMBER_BIT_OFFSET(member->offset)); | |
685 | else | |
686 | __btf_verifier_log(log, "\t%s type_id=%u bits_offset=%u", | |
687 | __btf_name_by_offset(btf, member->name_off), | |
688 | member->type, member->offset); | |
69b693f0 MKL |
689 | |
690 | if (fmt && *fmt) { | |
691 | __btf_verifier_log(log, " "); | |
692 | va_start(args, fmt); | |
693 | bpf_verifier_vlog(log, fmt, args); | |
694 | va_end(args); | |
695 | } | |
696 | ||
697 | __btf_verifier_log(log, "\n"); | |
698 | } | |
699 | ||
f80442a4 MKL |
700 | static void btf_verifier_log_hdr(struct btf_verifier_env *env, |
701 | u32 btf_data_size) | |
69b693f0 MKL |
702 | { |
703 | struct bpf_verifier_log *log = &env->log; | |
704 | const struct btf *btf = env->btf; | |
705 | const struct btf_header *hdr; | |
706 | ||
707 | if (!bpf_verifier_log_needed(log)) | |
708 | return; | |
709 | ||
f80442a4 | 710 | hdr = &btf->hdr; |
69b693f0 MKL |
711 | __btf_verifier_log(log, "magic: 0x%x\n", hdr->magic); |
712 | __btf_verifier_log(log, "version: %u\n", hdr->version); | |
713 | __btf_verifier_log(log, "flags: 0x%x\n", hdr->flags); | |
f80442a4 | 714 | __btf_verifier_log(log, "hdr_len: %u\n", hdr->hdr_len); |
69b693f0 | 715 | __btf_verifier_log(log, "type_off: %u\n", hdr->type_off); |
f80442a4 | 716 | __btf_verifier_log(log, "type_len: %u\n", hdr->type_len); |
69b693f0 MKL |
717 | __btf_verifier_log(log, "str_off: %u\n", hdr->str_off); |
718 | __btf_verifier_log(log, "str_len: %u\n", hdr->str_len); | |
f80442a4 | 719 | __btf_verifier_log(log, "btf_total_size: %u\n", btf_data_size); |
69b693f0 MKL |
720 | } |
721 | ||
722 | static int btf_add_type(struct btf_verifier_env *env, struct btf_type *t) | |
723 | { | |
724 | struct btf *btf = env->btf; | |
725 | ||
726 | /* < 2 because +1 for btf_void which is always in btf->types[0]. | |
727 | * btf_void is not accounted in btf->nr_types because btf_void | |
728 | * does not come from the BTF file. | |
729 | */ | |
730 | if (btf->types_size - btf->nr_types < 2) { | |
731 | /* Expand 'types' array */ | |
732 | ||
733 | struct btf_type **new_types; | |
734 | u32 expand_by, new_size; | |
735 | ||
aea2f7b8 | 736 | if (btf->types_size == BTF_MAX_TYPE) { |
69b693f0 MKL |
737 | btf_verifier_log(env, "Exceeded max num of types"); |
738 | return -E2BIG; | |
739 | } | |
740 | ||
741 | expand_by = max_t(u32, btf->types_size >> 2, 16); | |
aea2f7b8 | 742 | new_size = min_t(u32, BTF_MAX_TYPE, |
69b693f0 MKL |
743 | btf->types_size + expand_by); |
744 | ||
778e1cdd | 745 | new_types = kvcalloc(new_size, sizeof(*new_types), |
69b693f0 MKL |
746 | GFP_KERNEL | __GFP_NOWARN); |
747 | if (!new_types) | |
748 | return -ENOMEM; | |
749 | ||
750 | if (btf->nr_types == 0) | |
751 | new_types[0] = &btf_void; | |
752 | else | |
753 | memcpy(new_types, btf->types, | |
754 | sizeof(*btf->types) * (btf->nr_types + 1)); | |
755 | ||
756 | kvfree(btf->types); | |
757 | btf->types = new_types; | |
758 | btf->types_size = new_size; | |
759 | } | |
760 | ||
761 | btf->types[++(btf->nr_types)] = t; | |
762 | ||
763 | return 0; | |
764 | } | |
765 | ||
78958fca MKL |
766 | static int btf_alloc_id(struct btf *btf) |
767 | { | |
768 | int id; | |
769 | ||
770 | idr_preload(GFP_KERNEL); | |
771 | spin_lock_bh(&btf_idr_lock); | |
772 | id = idr_alloc_cyclic(&btf_idr, btf, 1, INT_MAX, GFP_ATOMIC); | |
773 | if (id > 0) | |
774 | btf->id = id; | |
775 | spin_unlock_bh(&btf_idr_lock); | |
776 | idr_preload_end(); | |
777 | ||
778 | if (WARN_ON_ONCE(!id)) | |
779 | return -ENOSPC; | |
780 | ||
781 | return id > 0 ? 0 : id; | |
782 | } | |
783 | ||
784 | static void btf_free_id(struct btf *btf) | |
785 | { | |
786 | unsigned long flags; | |
787 | ||
788 | /* | |
789 | * In map-in-map, calling map_delete_elem() on outer | |
790 | * map will call bpf_map_put on the inner map. | |
791 | * It will then eventually call btf_free_id() | |
792 | * on the inner map. Some of the map_delete_elem() | |
793 | * implementation may have irq disabled, so | |
794 | * we need to use the _irqsave() version instead | |
795 | * of the _bh() version. | |
796 | */ | |
797 | spin_lock_irqsave(&btf_idr_lock, flags); | |
798 | idr_remove(&btf_idr, btf->id); | |
799 | spin_unlock_irqrestore(&btf_idr_lock, flags); | |
800 | } | |
801 | ||
69b693f0 MKL |
802 | static void btf_free(struct btf *btf) |
803 | { | |
804 | kvfree(btf->types); | |
eb3f595d MKL |
805 | kvfree(btf->resolved_sizes); |
806 | kvfree(btf->resolved_ids); | |
69b693f0 MKL |
807 | kvfree(btf->data); |
808 | kfree(btf); | |
809 | } | |
810 | ||
78958fca | 811 | static void btf_free_rcu(struct rcu_head *rcu) |
f56a653c | 812 | { |
78958fca MKL |
813 | struct btf *btf = container_of(rcu, struct btf, rcu); |
814 | ||
815 | btf_free(btf); | |
f56a653c MKL |
816 | } |
817 | ||
818 | void btf_put(struct btf *btf) | |
819 | { | |
78958fca MKL |
820 | if (btf && refcount_dec_and_test(&btf->refcnt)) { |
821 | btf_free_id(btf); | |
822 | call_rcu(&btf->rcu, btf_free_rcu); | |
823 | } | |
f56a653c MKL |
824 | } |
825 | ||
eb3f595d MKL |
826 | static int env_resolve_init(struct btf_verifier_env *env) |
827 | { | |
828 | struct btf *btf = env->btf; | |
829 | u32 nr_types = btf->nr_types; | |
830 | u32 *resolved_sizes = NULL; | |
831 | u32 *resolved_ids = NULL; | |
832 | u8 *visit_states = NULL; | |
833 | ||
834 | /* +1 for btf_void */ | |
778e1cdd | 835 | resolved_sizes = kvcalloc(nr_types + 1, sizeof(*resolved_sizes), |
eb3f595d MKL |
836 | GFP_KERNEL | __GFP_NOWARN); |
837 | if (!resolved_sizes) | |
838 | goto nomem; | |
839 | ||
778e1cdd | 840 | resolved_ids = kvcalloc(nr_types + 1, sizeof(*resolved_ids), |
eb3f595d MKL |
841 | GFP_KERNEL | __GFP_NOWARN); |
842 | if (!resolved_ids) | |
843 | goto nomem; | |
844 | ||
778e1cdd | 845 | visit_states = kvcalloc(nr_types + 1, sizeof(*visit_states), |
eb3f595d MKL |
846 | GFP_KERNEL | __GFP_NOWARN); |
847 | if (!visit_states) | |
848 | goto nomem; | |
849 | ||
850 | btf->resolved_sizes = resolved_sizes; | |
851 | btf->resolved_ids = resolved_ids; | |
852 | env->visit_states = visit_states; | |
853 | ||
854 | return 0; | |
855 | ||
856 | nomem: | |
857 | kvfree(resolved_sizes); | |
858 | kvfree(resolved_ids); | |
859 | kvfree(visit_states); | |
860 | return -ENOMEM; | |
861 | } | |
862 | ||
69b693f0 MKL |
863 | static void btf_verifier_env_free(struct btf_verifier_env *env) |
864 | { | |
eb3f595d | 865 | kvfree(env->visit_states); |
69b693f0 MKL |
866 | kfree(env); |
867 | } | |
868 | ||
eb3f595d MKL |
869 | static bool env_type_is_resolve_sink(const struct btf_verifier_env *env, |
870 | const struct btf_type *next_type) | |
871 | { | |
872 | switch (env->resolve_mode) { | |
873 | case RESOLVE_TBD: | |
874 | /* int, enum or void is a sink */ | |
875 | return !btf_type_needs_resolve(next_type); | |
876 | case RESOLVE_PTR: | |
2667a262 MKL |
877 | /* int, enum, void, struct, array, func or func_proto is a sink |
878 | * for ptr | |
879 | */ | |
eb3f595d MKL |
880 | return !btf_type_is_modifier(next_type) && |
881 | !btf_type_is_ptr(next_type); | |
882 | case RESOLVE_STRUCT_OR_ARRAY: | |
2667a262 MKL |
883 | /* int, enum, void, ptr, func or func_proto is a sink |
884 | * for struct and array | |
885 | */ | |
eb3f595d MKL |
886 | return !btf_type_is_modifier(next_type) && |
887 | !btf_type_is_array(next_type) && | |
888 | !btf_type_is_struct(next_type); | |
889 | default: | |
53c8036c | 890 | BUG(); |
eb3f595d MKL |
891 | } |
892 | } | |
893 | ||
894 | static bool env_type_is_resolved(const struct btf_verifier_env *env, | |
895 | u32 type_id) | |
896 | { | |
897 | return env->visit_states[type_id] == RESOLVED; | |
898 | } | |
899 | ||
900 | static int env_stack_push(struct btf_verifier_env *env, | |
901 | const struct btf_type *t, u32 type_id) | |
902 | { | |
903 | struct resolve_vertex *v; | |
904 | ||
905 | if (env->top_stack == MAX_RESOLVE_DEPTH) | |
906 | return -E2BIG; | |
907 | ||
908 | if (env->visit_states[type_id] != NOT_VISITED) | |
909 | return -EEXIST; | |
910 | ||
911 | env->visit_states[type_id] = VISITED; | |
912 | ||
913 | v = &env->stack[env->top_stack++]; | |
914 | v->t = t; | |
915 | v->type_id = type_id; | |
916 | v->next_member = 0; | |
917 | ||
918 | if (env->resolve_mode == RESOLVE_TBD) { | |
919 | if (btf_type_is_ptr(t)) | |
920 | env->resolve_mode = RESOLVE_PTR; | |
921 | else if (btf_type_is_struct(t) || btf_type_is_array(t)) | |
922 | env->resolve_mode = RESOLVE_STRUCT_OR_ARRAY; | |
923 | } | |
924 | ||
925 | return 0; | |
926 | } | |
927 | ||
928 | static void env_stack_set_next_member(struct btf_verifier_env *env, | |
929 | u16 next_member) | |
930 | { | |
931 | env->stack[env->top_stack - 1].next_member = next_member; | |
932 | } | |
933 | ||
934 | static void env_stack_pop_resolved(struct btf_verifier_env *env, | |
935 | u32 resolved_type_id, | |
936 | u32 resolved_size) | |
937 | { | |
938 | u32 type_id = env->stack[--(env->top_stack)].type_id; | |
939 | struct btf *btf = env->btf; | |
940 | ||
941 | btf->resolved_sizes[type_id] = resolved_size; | |
942 | btf->resolved_ids[type_id] = resolved_type_id; | |
943 | env->visit_states[type_id] = RESOLVED; | |
944 | } | |
945 | ||
946 | static const struct resolve_vertex *env_stack_peak(struct btf_verifier_env *env) | |
947 | { | |
948 | return env->top_stack ? &env->stack[env->top_stack - 1] : NULL; | |
949 | } | |
950 | ||
951 | /* The input param "type_id" must point to a needs_resolve type */ | |
952 | static const struct btf_type *btf_type_id_resolve(const struct btf *btf, | |
953 | u32 *type_id) | |
954 | { | |
955 | *type_id = btf->resolved_ids[*type_id]; | |
956 | return btf_type_by_id(btf, *type_id); | |
957 | } | |
958 | ||
959 | const struct btf_type *btf_type_id_size(const struct btf *btf, | |
960 | u32 *type_id, u32 *ret_size) | |
961 | { | |
962 | const struct btf_type *size_type; | |
963 | u32 size_type_id = *type_id; | |
964 | u32 size = 0; | |
965 | ||
966 | size_type = btf_type_by_id(btf, size_type_id); | |
b47a0bd2 | 967 | if (btf_type_nosize_or_null(size_type)) |
eb3f595d MKL |
968 | return NULL; |
969 | ||
970 | if (btf_type_has_size(size_type)) { | |
971 | size = size_type->size; | |
972 | } else if (btf_type_is_array(size_type)) { | |
973 | size = btf->resolved_sizes[size_type_id]; | |
974 | } else if (btf_type_is_ptr(size_type)) { | |
975 | size = sizeof(void *); | |
976 | } else { | |
977 | if (WARN_ON_ONCE(!btf_type_is_modifier(size_type))) | |
978 | return NULL; | |
979 | ||
980 | size = btf->resolved_sizes[size_type_id]; | |
981 | size_type_id = btf->resolved_ids[size_type_id]; | |
982 | size_type = btf_type_by_id(btf, size_type_id); | |
b47a0bd2 | 983 | if (btf_type_nosize_or_null(size_type)) |
eb3f595d MKL |
984 | return NULL; |
985 | } | |
986 | ||
987 | *type_id = size_type_id; | |
988 | if (ret_size) | |
989 | *ret_size = size; | |
990 | ||
991 | return size_type; | |
992 | } | |
993 | ||
179cde8c MKL |
994 | static int btf_df_check_member(struct btf_verifier_env *env, |
995 | const struct btf_type *struct_type, | |
996 | const struct btf_member *member, | |
997 | const struct btf_type *member_type) | |
998 | { | |
999 | btf_verifier_log_basic(env, struct_type, | |
1000 | "Unsupported check_member"); | |
1001 | return -EINVAL; | |
1002 | } | |
1003 | ||
9d5f9f70 YS |
1004 | static int btf_df_check_kflag_member(struct btf_verifier_env *env, |
1005 | const struct btf_type *struct_type, | |
1006 | const struct btf_member *member, | |
1007 | const struct btf_type *member_type) | |
1008 | { | |
1009 | btf_verifier_log_basic(env, struct_type, | |
1010 | "Unsupported check_kflag_member"); | |
1011 | return -EINVAL; | |
1012 | } | |
1013 | ||
1014 | /* Used for ptr, array and struct/union type members. | |
1015 | * int, enum and modifier types have their specific callback functions. | |
1016 | */ | |
1017 | static int btf_generic_check_kflag_member(struct btf_verifier_env *env, | |
1018 | const struct btf_type *struct_type, | |
1019 | const struct btf_member *member, | |
1020 | const struct btf_type *member_type) | |
1021 | { | |
1022 | if (BTF_MEMBER_BITFIELD_SIZE(member->offset)) { | |
1023 | btf_verifier_log_member(env, struct_type, member, | |
1024 | "Invalid member bitfield_size"); | |
1025 | return -EINVAL; | |
1026 | } | |
1027 | ||
1028 | /* bitfield size is 0, so member->offset represents bit offset only. | |
1029 | * It is safe to call non kflag check_member variants. | |
1030 | */ | |
1031 | return btf_type_ops(member_type)->check_member(env, struct_type, | |
1032 | member, | |
1033 | member_type); | |
1034 | } | |
1035 | ||
eb3f595d MKL |
1036 | static int btf_df_resolve(struct btf_verifier_env *env, |
1037 | const struct resolve_vertex *v) | |
1038 | { | |
1039 | btf_verifier_log_basic(env, v->t, "Unsupported resolve"); | |
1040 | return -EINVAL; | |
1041 | } | |
1042 | ||
b00b8dae MKL |
1043 | static void btf_df_seq_show(const struct btf *btf, const struct btf_type *t, |
1044 | u32 type_id, void *data, u8 bits_offsets, | |
1045 | struct seq_file *m) | |
1046 | { | |
1047 | seq_printf(m, "<unsupported kind:%u>", BTF_INFO_KIND(t->info)); | |
1048 | } | |
1049 | ||
179cde8c MKL |
1050 | static int btf_int_check_member(struct btf_verifier_env *env, |
1051 | const struct btf_type *struct_type, | |
1052 | const struct btf_member *member, | |
1053 | const struct btf_type *member_type) | |
1054 | { | |
1055 | u32 int_data = btf_type_int(member_type); | |
1056 | u32 struct_bits_off = member->offset; | |
1057 | u32 struct_size = struct_type->size; | |
1058 | u32 nr_copy_bits; | |
1059 | u32 bytes_offset; | |
1060 | ||
1061 | if (U32_MAX - struct_bits_off < BTF_INT_OFFSET(int_data)) { | |
1062 | btf_verifier_log_member(env, struct_type, member, | |
1063 | "bits_offset exceeds U32_MAX"); | |
1064 | return -EINVAL; | |
1065 | } | |
1066 | ||
1067 | struct_bits_off += BTF_INT_OFFSET(int_data); | |
1068 | bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off); | |
1069 | nr_copy_bits = BTF_INT_BITS(int_data) + | |
1070 | BITS_PER_BYTE_MASKED(struct_bits_off); | |
1071 | ||
b1e8818c | 1072 | if (nr_copy_bits > BITS_PER_U128) { |
179cde8c | 1073 | btf_verifier_log_member(env, struct_type, member, |
b1e8818c | 1074 | "nr_copy_bits exceeds 128"); |
179cde8c MKL |
1075 | return -EINVAL; |
1076 | } | |
1077 | ||
1078 | if (struct_size < bytes_offset || | |
1079 | struct_size - bytes_offset < BITS_ROUNDUP_BYTES(nr_copy_bits)) { | |
1080 | btf_verifier_log_member(env, struct_type, member, | |
1081 | "Member exceeds struct_size"); | |
1082 | return -EINVAL; | |
1083 | } | |
1084 | ||
1085 | return 0; | |
1086 | } | |
1087 | ||
9d5f9f70 YS |
1088 | static int btf_int_check_kflag_member(struct btf_verifier_env *env, |
1089 | const struct btf_type *struct_type, | |
1090 | const struct btf_member *member, | |
1091 | const struct btf_type *member_type) | |
1092 | { | |
1093 | u32 struct_bits_off, nr_bits, nr_int_data_bits, bytes_offset; | |
1094 | u32 int_data = btf_type_int(member_type); | |
1095 | u32 struct_size = struct_type->size; | |
1096 | u32 nr_copy_bits; | |
1097 | ||
1098 | /* a regular int type is required for the kflag int member */ | |
1099 | if (!btf_type_int_is_regular(member_type)) { | |
1100 | btf_verifier_log_member(env, struct_type, member, | |
1101 | "Invalid member base type"); | |
1102 | return -EINVAL; | |
1103 | } | |
1104 | ||
1105 | /* check sanity of bitfield size */ | |
1106 | nr_bits = BTF_MEMBER_BITFIELD_SIZE(member->offset); | |
1107 | struct_bits_off = BTF_MEMBER_BIT_OFFSET(member->offset); | |
1108 | nr_int_data_bits = BTF_INT_BITS(int_data); | |
1109 | if (!nr_bits) { | |
1110 | /* Not a bitfield member, member offset must be at byte | |
1111 | * boundary. | |
1112 | */ | |
1113 | if (BITS_PER_BYTE_MASKED(struct_bits_off)) { | |
1114 | btf_verifier_log_member(env, struct_type, member, | |
1115 | "Invalid member offset"); | |
1116 | return -EINVAL; | |
1117 | } | |
1118 | ||
1119 | nr_bits = nr_int_data_bits; | |
1120 | } else if (nr_bits > nr_int_data_bits) { | |
1121 | btf_verifier_log_member(env, struct_type, member, | |
1122 | "Invalid member bitfield_size"); | |
1123 | return -EINVAL; | |
1124 | } | |
1125 | ||
1126 | bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off); | |
1127 | nr_copy_bits = nr_bits + BITS_PER_BYTE_MASKED(struct_bits_off); | |
b1e8818c | 1128 | if (nr_copy_bits > BITS_PER_U128) { |
9d5f9f70 | 1129 | btf_verifier_log_member(env, struct_type, member, |
b1e8818c | 1130 | "nr_copy_bits exceeds 128"); |
9d5f9f70 YS |
1131 | return -EINVAL; |
1132 | } | |
1133 | ||
1134 | if (struct_size < bytes_offset || | |
1135 | struct_size - bytes_offset < BITS_ROUNDUP_BYTES(nr_copy_bits)) { | |
1136 | btf_verifier_log_member(env, struct_type, member, | |
1137 | "Member exceeds struct_size"); | |
1138 | return -EINVAL; | |
1139 | } | |
1140 | ||
1141 | return 0; | |
1142 | } | |
1143 | ||
69b693f0 MKL |
1144 | static s32 btf_int_check_meta(struct btf_verifier_env *env, |
1145 | const struct btf_type *t, | |
1146 | u32 meta_left) | |
1147 | { | |
1148 | u32 int_data, nr_bits, meta_needed = sizeof(int_data); | |
1149 | u16 encoding; | |
1150 | ||
1151 | if (meta_left < meta_needed) { | |
1152 | btf_verifier_log_basic(env, t, | |
1153 | "meta_left:%u meta_needed:%u", | |
1154 | meta_left, meta_needed); | |
1155 | return -EINVAL; | |
1156 | } | |
1157 | ||
1158 | if (btf_type_vlen(t)) { | |
1159 | btf_verifier_log_type(env, t, "vlen != 0"); | |
1160 | return -EINVAL; | |
1161 | } | |
1162 | ||
9d5f9f70 YS |
1163 | if (btf_type_kflag(t)) { |
1164 | btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); | |
1165 | return -EINVAL; | |
1166 | } | |
1167 | ||
69b693f0 | 1168 | int_data = btf_type_int(t); |
aea2f7b8 MKL |
1169 | if (int_data & ~BTF_INT_MASK) { |
1170 | btf_verifier_log_basic(env, t, "Invalid int_data:%x", | |
1171 | int_data); | |
1172 | return -EINVAL; | |
1173 | } | |
1174 | ||
69b693f0 MKL |
1175 | nr_bits = BTF_INT_BITS(int_data) + BTF_INT_OFFSET(int_data); |
1176 | ||
b1e8818c | 1177 | if (nr_bits > BITS_PER_U128) { |
69b693f0 | 1178 | btf_verifier_log_type(env, t, "nr_bits exceeds %zu", |
b1e8818c | 1179 | BITS_PER_U128); |
69b693f0 MKL |
1180 | return -EINVAL; |
1181 | } | |
1182 | ||
1183 | if (BITS_ROUNDUP_BYTES(nr_bits) > t->size) { | |
1184 | btf_verifier_log_type(env, t, "nr_bits exceeds type_size"); | |
1185 | return -EINVAL; | |
1186 | } | |
1187 | ||
aea2f7b8 MKL |
1188 | /* |
1189 | * Only one of the encoding bits is allowed and it | |
1190 | * should be sufficient for the pretty print purpose (i.e. decoding). | |
1191 | * Multiple bits can be allowed later if it is found | |
1192 | * to be insufficient. | |
1193 | */ | |
69b693f0 MKL |
1194 | encoding = BTF_INT_ENCODING(int_data); |
1195 | if (encoding && | |
1196 | encoding != BTF_INT_SIGNED && | |
1197 | encoding != BTF_INT_CHAR && | |
aea2f7b8 | 1198 | encoding != BTF_INT_BOOL) { |
69b693f0 MKL |
1199 | btf_verifier_log_type(env, t, "Unsupported encoding"); |
1200 | return -ENOTSUPP; | |
1201 | } | |
1202 | ||
1203 | btf_verifier_log_type(env, t, NULL); | |
1204 | ||
1205 | return meta_needed; | |
1206 | } | |
1207 | ||
1208 | static void btf_int_log(struct btf_verifier_env *env, | |
1209 | const struct btf_type *t) | |
1210 | { | |
1211 | int int_data = btf_type_int(t); | |
1212 | ||
1213 | btf_verifier_log(env, | |
1214 | "size=%u bits_offset=%u nr_bits=%u encoding=%s", | |
1215 | t->size, BTF_INT_OFFSET(int_data), | |
1216 | BTF_INT_BITS(int_data), | |
1217 | btf_int_encoding_str(BTF_INT_ENCODING(int_data))); | |
1218 | } | |
1219 | ||
b1e8818c YS |
1220 | static void btf_int128_print(struct seq_file *m, void *data) |
1221 | { | |
1222 | /* data points to a __int128 number. | |
1223 | * Suppose | |
1224 | * int128_num = *(__int128 *)data; | |
1225 | * The below formulas shows what upper_num and lower_num represents: | |
1226 | * upper_num = int128_num >> 64; | |
1227 | * lower_num = int128_num & 0xffffffffFFFFFFFFULL; | |
1228 | */ | |
1229 | u64 upper_num, lower_num; | |
1230 | ||
1231 | #ifdef __BIG_ENDIAN_BITFIELD | |
1232 | upper_num = *(u64 *)data; | |
1233 | lower_num = *(u64 *)(data + 8); | |
1234 | #else | |
1235 | upper_num = *(u64 *)(data + 8); | |
1236 | lower_num = *(u64 *)data; | |
1237 | #endif | |
1238 | if (upper_num == 0) | |
1239 | seq_printf(m, "0x%llx", lower_num); | |
1240 | else | |
1241 | seq_printf(m, "0x%llx%016llx", upper_num, lower_num); | |
1242 | } | |
1243 | ||
1244 | static void btf_int128_shift(u64 *print_num, u16 left_shift_bits, | |
1245 | u16 right_shift_bits) | |
1246 | { | |
1247 | u64 upper_num, lower_num; | |
1248 | ||
1249 | #ifdef __BIG_ENDIAN_BITFIELD | |
1250 | upper_num = print_num[0]; | |
1251 | lower_num = print_num[1]; | |
1252 | #else | |
1253 | upper_num = print_num[1]; | |
1254 | lower_num = print_num[0]; | |
1255 | #endif | |
1256 | ||
1257 | /* shake out un-needed bits by shift/or operations */ | |
1258 | if (left_shift_bits >= 64) { | |
1259 | upper_num = lower_num << (left_shift_bits - 64); | |
1260 | lower_num = 0; | |
1261 | } else { | |
1262 | upper_num = (upper_num << left_shift_bits) | | |
1263 | (lower_num >> (64 - left_shift_bits)); | |
1264 | lower_num = lower_num << left_shift_bits; | |
1265 | } | |
1266 | ||
1267 | if (right_shift_bits >= 64) { | |
1268 | lower_num = upper_num >> (right_shift_bits - 64); | |
1269 | upper_num = 0; | |
1270 | } else { | |
1271 | lower_num = (lower_num >> right_shift_bits) | | |
1272 | (upper_num << (64 - right_shift_bits)); | |
1273 | upper_num = upper_num >> right_shift_bits; | |
1274 | } | |
1275 | ||
1276 | #ifdef __BIG_ENDIAN_BITFIELD | |
1277 | print_num[0] = upper_num; | |
1278 | print_num[1] = lower_num; | |
1279 | #else | |
1280 | print_num[0] = lower_num; | |
1281 | print_num[1] = upper_num; | |
1282 | #endif | |
1283 | } | |
1284 | ||
f97be3ab YS |
1285 | static void btf_bitfield_seq_show(void *data, u8 bits_offset, |
1286 | u8 nr_bits, struct seq_file *m) | |
b00b8dae | 1287 | { |
b65f370d | 1288 | u16 left_shift_bits, right_shift_bits; |
36fc3c8c MKL |
1289 | u8 nr_copy_bytes; |
1290 | u8 nr_copy_bits; | |
b1e8818c | 1291 | u64 print_num[2] = {}; |
b00b8dae | 1292 | |
b00b8dae MKL |
1293 | nr_copy_bits = nr_bits + bits_offset; |
1294 | nr_copy_bytes = BITS_ROUNDUP_BYTES(nr_copy_bits); | |
1295 | ||
b1e8818c | 1296 | memcpy(print_num, data, nr_copy_bytes); |
b00b8dae | 1297 | |
b65f370d OK |
1298 | #ifdef __BIG_ENDIAN_BITFIELD |
1299 | left_shift_bits = bits_offset; | |
1300 | #else | |
b1e8818c | 1301 | left_shift_bits = BITS_PER_U128 - nr_copy_bits; |
b65f370d | 1302 | #endif |
b1e8818c | 1303 | right_shift_bits = BITS_PER_U128 - nr_bits; |
b00b8dae | 1304 | |
b1e8818c YS |
1305 | btf_int128_shift(print_num, left_shift_bits, right_shift_bits); |
1306 | btf_int128_print(m, print_num); | |
b00b8dae MKL |
1307 | } |
1308 | ||
9d5f9f70 | 1309 | |
f97be3ab YS |
1310 | static void btf_int_bits_seq_show(const struct btf *btf, |
1311 | const struct btf_type *t, | |
1312 | void *data, u8 bits_offset, | |
1313 | struct seq_file *m) | |
1314 | { | |
1315 | u32 int_data = btf_type_int(t); | |
1316 | u8 nr_bits = BTF_INT_BITS(int_data); | |
1317 | u8 total_bits_offset; | |
1318 | ||
1319 | /* | |
1320 | * bits_offset is at most 7. | |
b1e8818c | 1321 | * BTF_INT_OFFSET() cannot exceed 128 bits. |
f97be3ab YS |
1322 | */ |
1323 | total_bits_offset = bits_offset + BTF_INT_OFFSET(int_data); | |
17e3ac81 YS |
1324 | data += BITS_ROUNDDOWN_BYTES(total_bits_offset); |
1325 | bits_offset = BITS_PER_BYTE_MASKED(total_bits_offset); | |
1326 | btf_bitfield_seq_show(data, bits_offset, nr_bits, m); | |
f97be3ab YS |
1327 | } |
1328 | ||
b00b8dae MKL |
1329 | static void btf_int_seq_show(const struct btf *btf, const struct btf_type *t, |
1330 | u32 type_id, void *data, u8 bits_offset, | |
1331 | struct seq_file *m) | |
1332 | { | |
1333 | u32 int_data = btf_type_int(t); | |
1334 | u8 encoding = BTF_INT_ENCODING(int_data); | |
1335 | bool sign = encoding & BTF_INT_SIGNED; | |
36fc3c8c | 1336 | u8 nr_bits = BTF_INT_BITS(int_data); |
b00b8dae MKL |
1337 | |
1338 | if (bits_offset || BTF_INT_OFFSET(int_data) || | |
1339 | BITS_PER_BYTE_MASKED(nr_bits)) { | |
1340 | btf_int_bits_seq_show(btf, t, data, bits_offset, m); | |
1341 | return; | |
1342 | } | |
1343 | ||
1344 | switch (nr_bits) { | |
b1e8818c YS |
1345 | case 128: |
1346 | btf_int128_print(m, data); | |
1347 | break; | |
b00b8dae MKL |
1348 | case 64: |
1349 | if (sign) | |
1350 | seq_printf(m, "%lld", *(s64 *)data); | |
1351 | else | |
1352 | seq_printf(m, "%llu", *(u64 *)data); | |
1353 | break; | |
1354 | case 32: | |
1355 | if (sign) | |
1356 | seq_printf(m, "%d", *(s32 *)data); | |
1357 | else | |
1358 | seq_printf(m, "%u", *(u32 *)data); | |
1359 | break; | |
1360 | case 16: | |
1361 | if (sign) | |
1362 | seq_printf(m, "%d", *(s16 *)data); | |
1363 | else | |
1364 | seq_printf(m, "%u", *(u16 *)data); | |
1365 | break; | |
1366 | case 8: | |
1367 | if (sign) | |
1368 | seq_printf(m, "%d", *(s8 *)data); | |
1369 | else | |
1370 | seq_printf(m, "%u", *(u8 *)data); | |
1371 | break; | |
1372 | default: | |
1373 | btf_int_bits_seq_show(btf, t, data, bits_offset, m); | |
1374 | } | |
1375 | } | |
1376 | ||
69b693f0 MKL |
1377 | static const struct btf_kind_operations int_ops = { |
1378 | .check_meta = btf_int_check_meta, | |
eb3f595d | 1379 | .resolve = btf_df_resolve, |
179cde8c | 1380 | .check_member = btf_int_check_member, |
9d5f9f70 | 1381 | .check_kflag_member = btf_int_check_kflag_member, |
69b693f0 | 1382 | .log_details = btf_int_log, |
b00b8dae | 1383 | .seq_show = btf_int_seq_show, |
69b693f0 MKL |
1384 | }; |
1385 | ||
179cde8c MKL |
1386 | static int btf_modifier_check_member(struct btf_verifier_env *env, |
1387 | const struct btf_type *struct_type, | |
1388 | const struct btf_member *member, | |
1389 | const struct btf_type *member_type) | |
1390 | { | |
1391 | const struct btf_type *resolved_type; | |
1392 | u32 resolved_type_id = member->type; | |
1393 | struct btf_member resolved_member; | |
1394 | struct btf *btf = env->btf; | |
1395 | ||
1396 | resolved_type = btf_type_id_size(btf, &resolved_type_id, NULL); | |
1397 | if (!resolved_type) { | |
1398 | btf_verifier_log_member(env, struct_type, member, | |
1399 | "Invalid member"); | |
1400 | return -EINVAL; | |
1401 | } | |
1402 | ||
1403 | resolved_member = *member; | |
1404 | resolved_member.type = resolved_type_id; | |
1405 | ||
1406 | return btf_type_ops(resolved_type)->check_member(env, struct_type, | |
1407 | &resolved_member, | |
1408 | resolved_type); | |
1409 | } | |
1410 | ||
9d5f9f70 YS |
1411 | static int btf_modifier_check_kflag_member(struct btf_verifier_env *env, |
1412 | const struct btf_type *struct_type, | |
1413 | const struct btf_member *member, | |
1414 | const struct btf_type *member_type) | |
1415 | { | |
1416 | const struct btf_type *resolved_type; | |
1417 | u32 resolved_type_id = member->type; | |
1418 | struct btf_member resolved_member; | |
1419 | struct btf *btf = env->btf; | |
1420 | ||
1421 | resolved_type = btf_type_id_size(btf, &resolved_type_id, NULL); | |
1422 | if (!resolved_type) { | |
1423 | btf_verifier_log_member(env, struct_type, member, | |
1424 | "Invalid member"); | |
1425 | return -EINVAL; | |
1426 | } | |
1427 | ||
1428 | resolved_member = *member; | |
1429 | resolved_member.type = resolved_type_id; | |
1430 | ||
1431 | return btf_type_ops(resolved_type)->check_kflag_member(env, struct_type, | |
1432 | &resolved_member, | |
1433 | resolved_type); | |
1434 | } | |
1435 | ||
179cde8c MKL |
1436 | static int btf_ptr_check_member(struct btf_verifier_env *env, |
1437 | const struct btf_type *struct_type, | |
1438 | const struct btf_member *member, | |
1439 | const struct btf_type *member_type) | |
1440 | { | |
1441 | u32 struct_size, struct_bits_off, bytes_offset; | |
1442 | ||
1443 | struct_size = struct_type->size; | |
1444 | struct_bits_off = member->offset; | |
1445 | bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off); | |
1446 | ||
1447 | if (BITS_PER_BYTE_MASKED(struct_bits_off)) { | |
1448 | btf_verifier_log_member(env, struct_type, member, | |
1449 | "Member is not byte aligned"); | |
1450 | return -EINVAL; | |
1451 | } | |
1452 | ||
1453 | if (struct_size - bytes_offset < sizeof(void *)) { | |
1454 | btf_verifier_log_member(env, struct_type, member, | |
1455 | "Member exceeds struct_size"); | |
1456 | return -EINVAL; | |
1457 | } | |
1458 | ||
1459 | return 0; | |
1460 | } | |
1461 | ||
69b693f0 MKL |
1462 | static int btf_ref_type_check_meta(struct btf_verifier_env *env, |
1463 | const struct btf_type *t, | |
1464 | u32 meta_left) | |
1465 | { | |
1466 | if (btf_type_vlen(t)) { | |
1467 | btf_verifier_log_type(env, t, "vlen != 0"); | |
1468 | return -EINVAL; | |
1469 | } | |
1470 | ||
9d5f9f70 YS |
1471 | if (btf_type_kflag(t)) { |
1472 | btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); | |
1473 | return -EINVAL; | |
1474 | } | |
1475 | ||
aea2f7b8 | 1476 | if (!BTF_TYPE_ID_VALID(t->type)) { |
69b693f0 MKL |
1477 | btf_verifier_log_type(env, t, "Invalid type_id"); |
1478 | return -EINVAL; | |
1479 | } | |
1480 | ||
eb04bbb6 YS |
1481 | /* typedef type must have a valid name, and other ref types, |
1482 | * volatile, const, restrict, should have a null name. | |
1483 | */ | |
1484 | if (BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF) { | |
1485 | if (!t->name_off || | |
1486 | !btf_name_valid_identifier(env->btf, t->name_off)) { | |
1487 | btf_verifier_log_type(env, t, "Invalid name"); | |
1488 | return -EINVAL; | |
1489 | } | |
1490 | } else { | |
1491 | if (t->name_off) { | |
1492 | btf_verifier_log_type(env, t, "Invalid name"); | |
1493 | return -EINVAL; | |
1494 | } | |
1495 | } | |
1496 | ||
69b693f0 MKL |
1497 | btf_verifier_log_type(env, t, NULL); |
1498 | ||
1499 | return 0; | |
1500 | } | |
1501 | ||
eb3f595d MKL |
1502 | static int btf_modifier_resolve(struct btf_verifier_env *env, |
1503 | const struct resolve_vertex *v) | |
1504 | { | |
1505 | const struct btf_type *t = v->t; | |
1506 | const struct btf_type *next_type; | |
1507 | u32 next_type_id = t->type; | |
1508 | struct btf *btf = env->btf; | |
1509 | u32 next_type_size = 0; | |
1510 | ||
1511 | next_type = btf_type_by_id(btf, next_type_id); | |
1512 | if (!next_type) { | |
1513 | btf_verifier_log_type(env, v->t, "Invalid type_id"); | |
1514 | return -EINVAL; | |
1515 | } | |
1516 | ||
eb3f595d MKL |
1517 | if (!env_type_is_resolve_sink(env, next_type) && |
1518 | !env_type_is_resolved(env, next_type_id)) | |
1519 | return env_stack_push(env, next_type, next_type_id); | |
1520 | ||
1521 | /* Figure out the resolved next_type_id with size. | |
1522 | * They will be stored in the current modifier's | |
1523 | * resolved_ids and resolved_sizes such that it can | |
1524 | * save us a few type-following when we use it later (e.g. in | |
1525 | * pretty print). | |
1526 | */ | |
2667a262 MKL |
1527 | if (!btf_type_id_size(btf, &next_type_id, &next_type_size)) { |
1528 | if (env_type_is_resolved(env, next_type_id)) | |
1529 | next_type = btf_type_id_resolve(btf, &next_type_id); | |
1530 | ||
1531 | /* "typedef void new_void", "const void"...etc */ | |
1532 | if (!btf_type_is_void(next_type) && | |
81f5c6f5 YS |
1533 | !btf_type_is_fwd(next_type) && |
1534 | !btf_type_is_func_proto(next_type)) { | |
2667a262 MKL |
1535 | btf_verifier_log_type(env, v->t, "Invalid type_id"); |
1536 | return -EINVAL; | |
1537 | } | |
eb3f595d MKL |
1538 | } |
1539 | ||
eb3f595d MKL |
1540 | env_stack_pop_resolved(env, next_type_id, next_type_size); |
1541 | ||
1542 | return 0; | |
1543 | } | |
1544 | ||
1545 | static int btf_ptr_resolve(struct btf_verifier_env *env, | |
1546 | const struct resolve_vertex *v) | |
1547 | { | |
1548 | const struct btf_type *next_type; | |
1549 | const struct btf_type *t = v->t; | |
1550 | u32 next_type_id = t->type; | |
1551 | struct btf *btf = env->btf; | |
eb3f595d MKL |
1552 | |
1553 | next_type = btf_type_by_id(btf, next_type_id); | |
1554 | if (!next_type) { | |
1555 | btf_verifier_log_type(env, v->t, "Invalid type_id"); | |
1556 | return -EINVAL; | |
1557 | } | |
1558 | ||
eb3f595d MKL |
1559 | if (!env_type_is_resolve_sink(env, next_type) && |
1560 | !env_type_is_resolved(env, next_type_id)) | |
1561 | return env_stack_push(env, next_type, next_type_id); | |
1562 | ||
1563 | /* If the modifier was RESOLVED during RESOLVE_STRUCT_OR_ARRAY, | |
1564 | * the modifier may have stopped resolving when it was resolved | |
1565 | * to a ptr (last-resolved-ptr). | |
1566 | * | |
1567 | * We now need to continue from the last-resolved-ptr to | |
1568 | * ensure the last-resolved-ptr will not referring back to | |
1569 | * the currenct ptr (t). | |
1570 | */ | |
1571 | if (btf_type_is_modifier(next_type)) { | |
1572 | const struct btf_type *resolved_type; | |
1573 | u32 resolved_type_id; | |
1574 | ||
1575 | resolved_type_id = next_type_id; | |
1576 | resolved_type = btf_type_id_resolve(btf, &resolved_type_id); | |
1577 | ||
1578 | if (btf_type_is_ptr(resolved_type) && | |
1579 | !env_type_is_resolve_sink(env, resolved_type) && | |
1580 | !env_type_is_resolved(env, resolved_type_id)) | |
1581 | return env_stack_push(env, resolved_type, | |
1582 | resolved_type_id); | |
1583 | } | |
1584 | ||
2667a262 MKL |
1585 | if (!btf_type_id_size(btf, &next_type_id, NULL)) { |
1586 | if (env_type_is_resolved(env, next_type_id)) | |
1587 | next_type = btf_type_id_resolve(btf, &next_type_id); | |
1588 | ||
1589 | if (!btf_type_is_void(next_type) && | |
1590 | !btf_type_is_fwd(next_type) && | |
1591 | !btf_type_is_func_proto(next_type)) { | |
1592 | btf_verifier_log_type(env, v->t, "Invalid type_id"); | |
1593 | return -EINVAL; | |
1594 | } | |
eb3f595d MKL |
1595 | } |
1596 | ||
eb3f595d MKL |
1597 | env_stack_pop_resolved(env, next_type_id, 0); |
1598 | ||
1599 | return 0; | |
1600 | } | |
1601 | ||
b00b8dae MKL |
1602 | static void btf_modifier_seq_show(const struct btf *btf, |
1603 | const struct btf_type *t, | |
1604 | u32 type_id, void *data, | |
1605 | u8 bits_offset, struct seq_file *m) | |
1606 | { | |
1607 | t = btf_type_id_resolve(btf, &type_id); | |
1608 | ||
1609 | btf_type_ops(t)->seq_show(btf, t, type_id, data, bits_offset, m); | |
1610 | } | |
1611 | ||
1612 | static void btf_ptr_seq_show(const struct btf *btf, const struct btf_type *t, | |
1613 | u32 type_id, void *data, u8 bits_offset, | |
1614 | struct seq_file *m) | |
1615 | { | |
1616 | /* It is a hashed value */ | |
1617 | seq_printf(m, "%p", *(void **)data); | |
1618 | } | |
1619 | ||
69b693f0 MKL |
1620 | static void btf_ref_type_log(struct btf_verifier_env *env, |
1621 | const struct btf_type *t) | |
1622 | { | |
1623 | btf_verifier_log(env, "type_id=%u", t->type); | |
1624 | } | |
1625 | ||
1626 | static struct btf_kind_operations modifier_ops = { | |
1627 | .check_meta = btf_ref_type_check_meta, | |
eb3f595d | 1628 | .resolve = btf_modifier_resolve, |
179cde8c | 1629 | .check_member = btf_modifier_check_member, |
9d5f9f70 | 1630 | .check_kflag_member = btf_modifier_check_kflag_member, |
69b693f0 | 1631 | .log_details = btf_ref_type_log, |
b00b8dae | 1632 | .seq_show = btf_modifier_seq_show, |
69b693f0 MKL |
1633 | }; |
1634 | ||
1635 | static struct btf_kind_operations ptr_ops = { | |
1636 | .check_meta = btf_ref_type_check_meta, | |
eb3f595d | 1637 | .resolve = btf_ptr_resolve, |
179cde8c | 1638 | .check_member = btf_ptr_check_member, |
9d5f9f70 | 1639 | .check_kflag_member = btf_generic_check_kflag_member, |
69b693f0 | 1640 | .log_details = btf_ref_type_log, |
b00b8dae | 1641 | .seq_show = btf_ptr_seq_show, |
69b693f0 MKL |
1642 | }; |
1643 | ||
8175383f MKL |
1644 | static s32 btf_fwd_check_meta(struct btf_verifier_env *env, |
1645 | const struct btf_type *t, | |
1646 | u32 meta_left) | |
1647 | { | |
1648 | if (btf_type_vlen(t)) { | |
1649 | btf_verifier_log_type(env, t, "vlen != 0"); | |
1650 | return -EINVAL; | |
1651 | } | |
1652 | ||
1653 | if (t->type) { | |
1654 | btf_verifier_log_type(env, t, "type != 0"); | |
1655 | return -EINVAL; | |
1656 | } | |
1657 | ||
eb04bbb6 YS |
1658 | /* fwd type must have a valid name */ |
1659 | if (!t->name_off || | |
1660 | !btf_name_valid_identifier(env->btf, t->name_off)) { | |
1661 | btf_verifier_log_type(env, t, "Invalid name"); | |
1662 | return -EINVAL; | |
1663 | } | |
1664 | ||
8175383f MKL |
1665 | btf_verifier_log_type(env, t, NULL); |
1666 | ||
1667 | return 0; | |
1668 | } | |
1669 | ||
76c43ae8 YS |
1670 | static void btf_fwd_type_log(struct btf_verifier_env *env, |
1671 | const struct btf_type *t) | |
1672 | { | |
1673 | btf_verifier_log(env, "%s", btf_type_kflag(t) ? "union" : "struct"); | |
1674 | } | |
1675 | ||
69b693f0 | 1676 | static struct btf_kind_operations fwd_ops = { |
8175383f | 1677 | .check_meta = btf_fwd_check_meta, |
eb3f595d | 1678 | .resolve = btf_df_resolve, |
179cde8c | 1679 | .check_member = btf_df_check_member, |
9d5f9f70 | 1680 | .check_kflag_member = btf_df_check_kflag_member, |
76c43ae8 | 1681 | .log_details = btf_fwd_type_log, |
b00b8dae | 1682 | .seq_show = btf_df_seq_show, |
69b693f0 MKL |
1683 | }; |
1684 | ||
179cde8c MKL |
1685 | static int btf_array_check_member(struct btf_verifier_env *env, |
1686 | const struct btf_type *struct_type, | |
1687 | const struct btf_member *member, | |
1688 | const struct btf_type *member_type) | |
1689 | { | |
1690 | u32 struct_bits_off = member->offset; | |
1691 | u32 struct_size, bytes_offset; | |
1692 | u32 array_type_id, array_size; | |
1693 | struct btf *btf = env->btf; | |
1694 | ||
1695 | if (BITS_PER_BYTE_MASKED(struct_bits_off)) { | |
1696 | btf_verifier_log_member(env, struct_type, member, | |
1697 | "Member is not byte aligned"); | |
1698 | return -EINVAL; | |
1699 | } | |
1700 | ||
1701 | array_type_id = member->type; | |
1702 | btf_type_id_size(btf, &array_type_id, &array_size); | |
1703 | struct_size = struct_type->size; | |
1704 | bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off); | |
1705 | if (struct_size - bytes_offset < array_size) { | |
1706 | btf_verifier_log_member(env, struct_type, member, | |
1707 | "Member exceeds struct_size"); | |
1708 | return -EINVAL; | |
1709 | } | |
1710 | ||
1711 | return 0; | |
1712 | } | |
1713 | ||
69b693f0 MKL |
1714 | static s32 btf_array_check_meta(struct btf_verifier_env *env, |
1715 | const struct btf_type *t, | |
1716 | u32 meta_left) | |
1717 | { | |
1718 | const struct btf_array *array = btf_type_array(t); | |
1719 | u32 meta_needed = sizeof(*array); | |
1720 | ||
1721 | if (meta_left < meta_needed) { | |
1722 | btf_verifier_log_basic(env, t, | |
1723 | "meta_left:%u meta_needed:%u", | |
1724 | meta_left, meta_needed); | |
1725 | return -EINVAL; | |
1726 | } | |
1727 | ||
eb04bbb6 YS |
1728 | /* array type should not have a name */ |
1729 | if (t->name_off) { | |
1730 | btf_verifier_log_type(env, t, "Invalid name"); | |
1731 | return -EINVAL; | |
1732 | } | |
1733 | ||
69b693f0 MKL |
1734 | if (btf_type_vlen(t)) { |
1735 | btf_verifier_log_type(env, t, "vlen != 0"); | |
1736 | return -EINVAL; | |
1737 | } | |
1738 | ||
9d5f9f70 YS |
1739 | if (btf_type_kflag(t)) { |
1740 | btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); | |
1741 | return -EINVAL; | |
1742 | } | |
1743 | ||
b9308ae6 MKL |
1744 | if (t->size) { |
1745 | btf_verifier_log_type(env, t, "size != 0"); | |
1746 | return -EINVAL; | |
1747 | } | |
1748 | ||
4ef5f574 MKL |
1749 | /* Array elem type and index type cannot be in type void, |
1750 | * so !array->type and !array->index_type are not allowed. | |
69b693f0 | 1751 | */ |
aea2f7b8 | 1752 | if (!array->type || !BTF_TYPE_ID_VALID(array->type)) { |
4ef5f574 MKL |
1753 | btf_verifier_log_type(env, t, "Invalid elem"); |
1754 | return -EINVAL; | |
1755 | } | |
1756 | ||
aea2f7b8 | 1757 | if (!array->index_type || !BTF_TYPE_ID_VALID(array->index_type)) { |
4ef5f574 | 1758 | btf_verifier_log_type(env, t, "Invalid index"); |
69b693f0 MKL |
1759 | return -EINVAL; |
1760 | } | |
1761 | ||
1762 | btf_verifier_log_type(env, t, NULL); | |
1763 | ||
1764 | return meta_needed; | |
1765 | } | |
1766 | ||
eb3f595d MKL |
1767 | static int btf_array_resolve(struct btf_verifier_env *env, |
1768 | const struct resolve_vertex *v) | |
1769 | { | |
1770 | const struct btf_array *array = btf_type_array(v->t); | |
4ef5f574 MKL |
1771 | const struct btf_type *elem_type, *index_type; |
1772 | u32 elem_type_id, index_type_id; | |
eb3f595d MKL |
1773 | struct btf *btf = env->btf; |
1774 | u32 elem_size; | |
1775 | ||
4ef5f574 MKL |
1776 | /* Check array->index_type */ |
1777 | index_type_id = array->index_type; | |
1778 | index_type = btf_type_by_id(btf, index_type_id); | |
b47a0bd2 | 1779 | if (btf_type_nosize_or_null(index_type)) { |
4ef5f574 MKL |
1780 | btf_verifier_log_type(env, v->t, "Invalid index"); |
1781 | return -EINVAL; | |
1782 | } | |
1783 | ||
1784 | if (!env_type_is_resolve_sink(env, index_type) && | |
1785 | !env_type_is_resolved(env, index_type_id)) | |
1786 | return env_stack_push(env, index_type, index_type_id); | |
1787 | ||
1788 | index_type = btf_type_id_size(btf, &index_type_id, NULL); | |
1789 | if (!index_type || !btf_type_is_int(index_type) || | |
1790 | !btf_type_int_is_regular(index_type)) { | |
1791 | btf_verifier_log_type(env, v->t, "Invalid index"); | |
1792 | return -EINVAL; | |
1793 | } | |
1794 | ||
1795 | /* Check array->type */ | |
1796 | elem_type_id = array->type; | |
eb3f595d | 1797 | elem_type = btf_type_by_id(btf, elem_type_id); |
b47a0bd2 | 1798 | if (btf_type_nosize_or_null(elem_type)) { |
eb3f595d MKL |
1799 | btf_verifier_log_type(env, v->t, |
1800 | "Invalid elem"); | |
1801 | return -EINVAL; | |
1802 | } | |
1803 | ||
1804 | if (!env_type_is_resolve_sink(env, elem_type) && | |
1805 | !env_type_is_resolved(env, elem_type_id)) | |
1806 | return env_stack_push(env, elem_type, elem_type_id); | |
1807 | ||
1808 | elem_type = btf_type_id_size(btf, &elem_type_id, &elem_size); | |
1809 | if (!elem_type) { | |
1810 | btf_verifier_log_type(env, v->t, "Invalid elem"); | |
1811 | return -EINVAL; | |
1812 | } | |
1813 | ||
4ef5f574 MKL |
1814 | if (btf_type_is_int(elem_type) && !btf_type_int_is_regular(elem_type)) { |
1815 | btf_verifier_log_type(env, v->t, "Invalid array of int"); | |
1816 | return -EINVAL; | |
eb3f595d MKL |
1817 | } |
1818 | ||
1819 | if (array->nelems && elem_size > U32_MAX / array->nelems) { | |
1820 | btf_verifier_log_type(env, v->t, | |
1821 | "Array size overflows U32_MAX"); | |
1822 | return -EINVAL; | |
1823 | } | |
1824 | ||
1825 | env_stack_pop_resolved(env, elem_type_id, elem_size * array->nelems); | |
1826 | ||
1827 | return 0; | |
1828 | } | |
1829 | ||
69b693f0 MKL |
1830 | static void btf_array_log(struct btf_verifier_env *env, |
1831 | const struct btf_type *t) | |
1832 | { | |
1833 | const struct btf_array *array = btf_type_array(t); | |
1834 | ||
1835 | btf_verifier_log(env, "type_id=%u index_type_id=%u nr_elems=%u", | |
1836 | array->type, array->index_type, array->nelems); | |
1837 | } | |
1838 | ||
b00b8dae MKL |
1839 | static void btf_array_seq_show(const struct btf *btf, const struct btf_type *t, |
1840 | u32 type_id, void *data, u8 bits_offset, | |
1841 | struct seq_file *m) | |
1842 | { | |
1843 | const struct btf_array *array = btf_type_array(t); | |
1844 | const struct btf_kind_operations *elem_ops; | |
1845 | const struct btf_type *elem_type; | |
1846 | u32 i, elem_size, elem_type_id; | |
1847 | ||
1848 | elem_type_id = array->type; | |
1849 | elem_type = btf_type_id_size(btf, &elem_type_id, &elem_size); | |
1850 | elem_ops = btf_type_ops(elem_type); | |
1851 | seq_puts(m, "["); | |
1852 | for (i = 0; i < array->nelems; i++) { | |
1853 | if (i) | |
1854 | seq_puts(m, ","); | |
1855 | ||
1856 | elem_ops->seq_show(btf, elem_type, elem_type_id, data, | |
1857 | bits_offset, m); | |
1858 | data += elem_size; | |
1859 | } | |
1860 | seq_puts(m, "]"); | |
1861 | } | |
1862 | ||
69b693f0 MKL |
1863 | static struct btf_kind_operations array_ops = { |
1864 | .check_meta = btf_array_check_meta, | |
eb3f595d | 1865 | .resolve = btf_array_resolve, |
179cde8c | 1866 | .check_member = btf_array_check_member, |
9d5f9f70 | 1867 | .check_kflag_member = btf_generic_check_kflag_member, |
69b693f0 | 1868 | .log_details = btf_array_log, |
b00b8dae | 1869 | .seq_show = btf_array_seq_show, |
69b693f0 MKL |
1870 | }; |
1871 | ||
179cde8c MKL |
1872 | static int btf_struct_check_member(struct btf_verifier_env *env, |
1873 | const struct btf_type *struct_type, | |
1874 | const struct btf_member *member, | |
1875 | const struct btf_type *member_type) | |
1876 | { | |
1877 | u32 struct_bits_off = member->offset; | |
1878 | u32 struct_size, bytes_offset; | |
1879 | ||
1880 | if (BITS_PER_BYTE_MASKED(struct_bits_off)) { | |
1881 | btf_verifier_log_member(env, struct_type, member, | |
1882 | "Member is not byte aligned"); | |
1883 | return -EINVAL; | |
1884 | } | |
1885 | ||
1886 | struct_size = struct_type->size; | |
1887 | bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off); | |
1888 | if (struct_size - bytes_offset < member_type->size) { | |
1889 | btf_verifier_log_member(env, struct_type, member, | |
1890 | "Member exceeds struct_size"); | |
1891 | return -EINVAL; | |
1892 | } | |
1893 | ||
1894 | return 0; | |
1895 | } | |
1896 | ||
69b693f0 MKL |
1897 | static s32 btf_struct_check_meta(struct btf_verifier_env *env, |
1898 | const struct btf_type *t, | |
1899 | u32 meta_left) | |
1900 | { | |
1901 | bool is_union = BTF_INFO_KIND(t->info) == BTF_KIND_UNION; | |
1902 | const struct btf_member *member; | |
6283fa38 | 1903 | u32 meta_needed, last_offset; |
69b693f0 MKL |
1904 | struct btf *btf = env->btf; |
1905 | u32 struct_size = t->size; | |
9d5f9f70 | 1906 | u32 offset; |
69b693f0 MKL |
1907 | u16 i; |
1908 | ||
1909 | meta_needed = btf_type_vlen(t) * sizeof(*member); | |
1910 | if (meta_left < meta_needed) { | |
1911 | btf_verifier_log_basic(env, t, | |
1912 | "meta_left:%u meta_needed:%u", | |
1913 | meta_left, meta_needed); | |
1914 | return -EINVAL; | |
1915 | } | |
1916 | ||
eb04bbb6 YS |
1917 | /* struct type either no name or a valid one */ |
1918 | if (t->name_off && | |
1919 | !btf_name_valid_identifier(env->btf, t->name_off)) { | |
1920 | btf_verifier_log_type(env, t, "Invalid name"); | |
1921 | return -EINVAL; | |
1922 | } | |
1923 | ||
69b693f0 MKL |
1924 | btf_verifier_log_type(env, t, NULL); |
1925 | ||
6283fa38 | 1926 | last_offset = 0; |
69b693f0 | 1927 | for_each_member(i, t, member) { |
fbcf93eb | 1928 | if (!btf_name_offset_valid(btf, member->name_off)) { |
69b693f0 MKL |
1929 | btf_verifier_log_member(env, t, member, |
1930 | "Invalid member name_offset:%u", | |
fbcf93eb | 1931 | member->name_off); |
69b693f0 MKL |
1932 | return -EINVAL; |
1933 | } | |
1934 | ||
eb04bbb6 YS |
1935 | /* struct member either no name or a valid one */ |
1936 | if (member->name_off && | |
1937 | !btf_name_valid_identifier(btf, member->name_off)) { | |
1938 | btf_verifier_log_member(env, t, member, "Invalid name"); | |
1939 | return -EINVAL; | |
1940 | } | |
69b693f0 | 1941 | /* A member cannot be in type void */ |
aea2f7b8 | 1942 | if (!member->type || !BTF_TYPE_ID_VALID(member->type)) { |
69b693f0 MKL |
1943 | btf_verifier_log_member(env, t, member, |
1944 | "Invalid type_id"); | |
1945 | return -EINVAL; | |
1946 | } | |
1947 | ||
9d5f9f70 YS |
1948 | offset = btf_member_bit_offset(t, member); |
1949 | if (is_union && offset) { | |
69b693f0 MKL |
1950 | btf_verifier_log_member(env, t, member, |
1951 | "Invalid member bits_offset"); | |
1952 | return -EINVAL; | |
1953 | } | |
1954 | ||
6283fa38 MKL |
1955 | /* |
1956 | * ">" instead of ">=" because the last member could be | |
1957 | * "char a[0];" | |
1958 | */ | |
9d5f9f70 | 1959 | if (last_offset > offset) { |
6283fa38 MKL |
1960 | btf_verifier_log_member(env, t, member, |
1961 | "Invalid member bits_offset"); | |
1962 | return -EINVAL; | |
1963 | } | |
1964 | ||
9d5f9f70 | 1965 | if (BITS_ROUNDUP_BYTES(offset) > struct_size) { |
69b693f0 | 1966 | btf_verifier_log_member(env, t, member, |
311fe1a8 | 1967 | "Member bits_offset exceeds its struct size"); |
69b693f0 MKL |
1968 | return -EINVAL; |
1969 | } | |
1970 | ||
1971 | btf_verifier_log_member(env, t, member, NULL); | |
9d5f9f70 | 1972 | last_offset = offset; |
69b693f0 MKL |
1973 | } |
1974 | ||
1975 | return meta_needed; | |
1976 | } | |
1977 | ||
eb3f595d MKL |
1978 | static int btf_struct_resolve(struct btf_verifier_env *env, |
1979 | const struct resolve_vertex *v) | |
1980 | { | |
1981 | const struct btf_member *member; | |
179cde8c | 1982 | int err; |
eb3f595d MKL |
1983 | u16 i; |
1984 | ||
1985 | /* Before continue resolving the next_member, | |
1986 | * ensure the last member is indeed resolved to a | |
1987 | * type with size info. | |
1988 | */ | |
1989 | if (v->next_member) { | |
179cde8c | 1990 | const struct btf_type *last_member_type; |
eb3f595d MKL |
1991 | const struct btf_member *last_member; |
1992 | u16 last_member_type_id; | |
1993 | ||
1994 | last_member = btf_type_member(v->t) + v->next_member - 1; | |
1995 | last_member_type_id = last_member->type; | |
1996 | if (WARN_ON_ONCE(!env_type_is_resolved(env, | |
1997 | last_member_type_id))) | |
1998 | return -EINVAL; | |
179cde8c MKL |
1999 | |
2000 | last_member_type = btf_type_by_id(env->btf, | |
2001 | last_member_type_id); | |
9d5f9f70 YS |
2002 | if (btf_type_kflag(v->t)) |
2003 | err = btf_type_ops(last_member_type)->check_kflag_member(env, v->t, | |
2004 | last_member, | |
2005 | last_member_type); | |
2006 | else | |
2007 | err = btf_type_ops(last_member_type)->check_member(env, v->t, | |
2008 | last_member, | |
2009 | last_member_type); | |
179cde8c MKL |
2010 | if (err) |
2011 | return err; | |
eb3f595d MKL |
2012 | } |
2013 | ||
2014 | for_each_member_from(i, v->next_member, v->t, member) { | |
2015 | u32 member_type_id = member->type; | |
2016 | const struct btf_type *member_type = btf_type_by_id(env->btf, | |
2017 | member_type_id); | |
2018 | ||
b47a0bd2 | 2019 | if (btf_type_nosize_or_null(member_type)) { |
eb3f595d MKL |
2020 | btf_verifier_log_member(env, v->t, member, |
2021 | "Invalid member"); | |
2022 | return -EINVAL; | |
2023 | } | |
2024 | ||
2025 | if (!env_type_is_resolve_sink(env, member_type) && | |
2026 | !env_type_is_resolved(env, member_type_id)) { | |
2027 | env_stack_set_next_member(env, i + 1); | |
2028 | return env_stack_push(env, member_type, member_type_id); | |
2029 | } | |
179cde8c | 2030 | |
9d5f9f70 YS |
2031 | if (btf_type_kflag(v->t)) |
2032 | err = btf_type_ops(member_type)->check_kflag_member(env, v->t, | |
2033 | member, | |
2034 | member_type); | |
2035 | else | |
2036 | err = btf_type_ops(member_type)->check_member(env, v->t, | |
2037 | member, | |
2038 | member_type); | |
179cde8c MKL |
2039 | if (err) |
2040 | return err; | |
eb3f595d MKL |
2041 | } |
2042 | ||
2043 | env_stack_pop_resolved(env, 0, 0); | |
2044 | ||
2045 | return 0; | |
2046 | } | |
2047 | ||
69b693f0 MKL |
2048 | static void btf_struct_log(struct btf_verifier_env *env, |
2049 | const struct btf_type *t) | |
2050 | { | |
2051 | btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t)); | |
2052 | } | |
2053 | ||
d83525ca AS |
2054 | /* find 'struct bpf_spin_lock' in map value. |
2055 | * return >= 0 offset if found | |
2056 | * and < 0 in case of error | |
2057 | */ | |
2058 | int btf_find_spin_lock(const struct btf *btf, const struct btf_type *t) | |
2059 | { | |
2060 | const struct btf_member *member; | |
2061 | u32 i, off = -ENOENT; | |
2062 | ||
2063 | if (!__btf_type_is_struct(t)) | |
2064 | return -EINVAL; | |
2065 | ||
2066 | for_each_member(i, t, member) { | |
2067 | const struct btf_type *member_type = btf_type_by_id(btf, | |
2068 | member->type); | |
2069 | if (!__btf_type_is_struct(member_type)) | |
2070 | continue; | |
2071 | if (member_type->size != sizeof(struct bpf_spin_lock)) | |
2072 | continue; | |
2073 | if (strcmp(__btf_name_by_offset(btf, member_type->name_off), | |
2074 | "bpf_spin_lock")) | |
2075 | continue; | |
2076 | if (off != -ENOENT) | |
2077 | /* only one 'struct bpf_spin_lock' is allowed */ | |
2078 | return -E2BIG; | |
2079 | off = btf_member_bit_offset(t, member); | |
2080 | if (off % 8) | |
2081 | /* valid C code cannot generate such BTF */ | |
2082 | return -EINVAL; | |
2083 | off /= 8; | |
2084 | if (off % __alignof__(struct bpf_spin_lock)) | |
2085 | /* valid struct bpf_spin_lock will be 4 byte aligned */ | |
2086 | return -EINVAL; | |
2087 | } | |
2088 | return off; | |
2089 | } | |
2090 | ||
b00b8dae MKL |
2091 | static void btf_struct_seq_show(const struct btf *btf, const struct btf_type *t, |
2092 | u32 type_id, void *data, u8 bits_offset, | |
2093 | struct seq_file *m) | |
2094 | { | |
2095 | const char *seq = BTF_INFO_KIND(t->info) == BTF_KIND_UNION ? "|" : ","; | |
2096 | const struct btf_member *member; | |
2097 | u32 i; | |
2098 | ||
2099 | seq_puts(m, "{"); | |
2100 | for_each_member(i, t, member) { | |
2101 | const struct btf_type *member_type = btf_type_by_id(btf, | |
2102 | member->type); | |
b00b8dae | 2103 | const struct btf_kind_operations *ops; |
9d5f9f70 YS |
2104 | u32 member_offset, bitfield_size; |
2105 | u32 bytes_offset; | |
2106 | u8 bits8_offset; | |
b00b8dae MKL |
2107 | |
2108 | if (i) | |
2109 | seq_puts(m, seq); | |
2110 | ||
9d5f9f70 YS |
2111 | member_offset = btf_member_bit_offset(t, member); |
2112 | bitfield_size = btf_member_bitfield_size(t, member); | |
17e3ac81 YS |
2113 | bytes_offset = BITS_ROUNDDOWN_BYTES(member_offset); |
2114 | bits8_offset = BITS_PER_BYTE_MASKED(member_offset); | |
9d5f9f70 | 2115 | if (bitfield_size) { |
17e3ac81 | 2116 | btf_bitfield_seq_show(data + bytes_offset, bits8_offset, |
9d5f9f70 YS |
2117 | bitfield_size, m); |
2118 | } else { | |
9d5f9f70 YS |
2119 | ops = btf_type_ops(member_type); |
2120 | ops->seq_show(btf, member_type, member->type, | |
2121 | data + bytes_offset, bits8_offset, m); | |
2122 | } | |
b00b8dae MKL |
2123 | } |
2124 | seq_puts(m, "}"); | |
2125 | } | |
2126 | ||
69b693f0 MKL |
2127 | static struct btf_kind_operations struct_ops = { |
2128 | .check_meta = btf_struct_check_meta, | |
eb3f595d | 2129 | .resolve = btf_struct_resolve, |
179cde8c | 2130 | .check_member = btf_struct_check_member, |
9d5f9f70 | 2131 | .check_kflag_member = btf_generic_check_kflag_member, |
69b693f0 | 2132 | .log_details = btf_struct_log, |
b00b8dae | 2133 | .seq_show = btf_struct_seq_show, |
69b693f0 MKL |
2134 | }; |
2135 | ||
179cde8c MKL |
2136 | static int btf_enum_check_member(struct btf_verifier_env *env, |
2137 | const struct btf_type *struct_type, | |
2138 | const struct btf_member *member, | |
2139 | const struct btf_type *member_type) | |
2140 | { | |
2141 | u32 struct_bits_off = member->offset; | |
2142 | u32 struct_size, bytes_offset; | |
2143 | ||
2144 | if (BITS_PER_BYTE_MASKED(struct_bits_off)) { | |
2145 | btf_verifier_log_member(env, struct_type, member, | |
2146 | "Member is not byte aligned"); | |
2147 | return -EINVAL; | |
2148 | } | |
2149 | ||
2150 | struct_size = struct_type->size; | |
2151 | bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off); | |
2152 | if (struct_size - bytes_offset < sizeof(int)) { | |
2153 | btf_verifier_log_member(env, struct_type, member, | |
2154 | "Member exceeds struct_size"); | |
2155 | return -EINVAL; | |
2156 | } | |
2157 | ||
2158 | return 0; | |
2159 | } | |
2160 | ||
9d5f9f70 YS |
2161 | static int btf_enum_check_kflag_member(struct btf_verifier_env *env, |
2162 | const struct btf_type *struct_type, | |
2163 | const struct btf_member *member, | |
2164 | const struct btf_type *member_type) | |
2165 | { | |
2166 | u32 struct_bits_off, nr_bits, bytes_end, struct_size; | |
2167 | u32 int_bitsize = sizeof(int) * BITS_PER_BYTE; | |
2168 | ||
2169 | struct_bits_off = BTF_MEMBER_BIT_OFFSET(member->offset); | |
2170 | nr_bits = BTF_MEMBER_BITFIELD_SIZE(member->offset); | |
2171 | if (!nr_bits) { | |
2172 | if (BITS_PER_BYTE_MASKED(struct_bits_off)) { | |
2173 | btf_verifier_log_member(env, struct_type, member, | |
2174 | "Member is not byte aligned"); | |
2175 | return -EINVAL; | |
2176 | } | |
2177 | ||
2178 | nr_bits = int_bitsize; | |
2179 | } else if (nr_bits > int_bitsize) { | |
2180 | btf_verifier_log_member(env, struct_type, member, | |
2181 | "Invalid member bitfield_size"); | |
2182 | return -EINVAL; | |
2183 | } | |
2184 | ||
2185 | struct_size = struct_type->size; | |
2186 | bytes_end = BITS_ROUNDUP_BYTES(struct_bits_off + nr_bits); | |
2187 | if (struct_size < bytes_end) { | |
2188 | btf_verifier_log_member(env, struct_type, member, | |
2189 | "Member exceeds struct_size"); | |
2190 | return -EINVAL; | |
2191 | } | |
2192 | ||
2193 | return 0; | |
2194 | } | |
2195 | ||
69b693f0 MKL |
2196 | static s32 btf_enum_check_meta(struct btf_verifier_env *env, |
2197 | const struct btf_type *t, | |
2198 | u32 meta_left) | |
2199 | { | |
2200 | const struct btf_enum *enums = btf_type_enum(t); | |
2201 | struct btf *btf = env->btf; | |
2202 | u16 i, nr_enums; | |
2203 | u32 meta_needed; | |
2204 | ||
2205 | nr_enums = btf_type_vlen(t); | |
2206 | meta_needed = nr_enums * sizeof(*enums); | |
2207 | ||
2208 | if (meta_left < meta_needed) { | |
2209 | btf_verifier_log_basic(env, t, | |
2210 | "meta_left:%u meta_needed:%u", | |
2211 | meta_left, meta_needed); | |
2212 | return -EINVAL; | |
2213 | } | |
2214 | ||
9d5f9f70 YS |
2215 | if (btf_type_kflag(t)) { |
2216 | btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); | |
2217 | return -EINVAL; | |
2218 | } | |
2219 | ||
69b693f0 MKL |
2220 | if (t->size != sizeof(int)) { |
2221 | btf_verifier_log_type(env, t, "Expected size:%zu", | |
2222 | sizeof(int)); | |
2223 | return -EINVAL; | |
2224 | } | |
2225 | ||
eb04bbb6 YS |
2226 | /* enum type either no name or a valid one */ |
2227 | if (t->name_off && | |
2228 | !btf_name_valid_identifier(env->btf, t->name_off)) { | |
2229 | btf_verifier_log_type(env, t, "Invalid name"); | |
2230 | return -EINVAL; | |
2231 | } | |
2232 | ||
69b693f0 MKL |
2233 | btf_verifier_log_type(env, t, NULL); |
2234 | ||
2235 | for (i = 0; i < nr_enums; i++) { | |
fbcf93eb | 2236 | if (!btf_name_offset_valid(btf, enums[i].name_off)) { |
69b693f0 | 2237 | btf_verifier_log(env, "\tInvalid name_offset:%u", |
fbcf93eb | 2238 | enums[i].name_off); |
69b693f0 MKL |
2239 | return -EINVAL; |
2240 | } | |
2241 | ||
eb04bbb6 YS |
2242 | /* enum member must have a valid name */ |
2243 | if (!enums[i].name_off || | |
2244 | !btf_name_valid_identifier(btf, enums[i].name_off)) { | |
2245 | btf_verifier_log_type(env, t, "Invalid name"); | |
2246 | return -EINVAL; | |
2247 | } | |
2248 | ||
2249 | ||
69b693f0 | 2250 | btf_verifier_log(env, "\t%s val=%d\n", |
23127b33 | 2251 | __btf_name_by_offset(btf, enums[i].name_off), |
69b693f0 MKL |
2252 | enums[i].val); |
2253 | } | |
2254 | ||
2255 | return meta_needed; | |
2256 | } | |
2257 | ||
2258 | static void btf_enum_log(struct btf_verifier_env *env, | |
2259 | const struct btf_type *t) | |
2260 | { | |
2261 | btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t)); | |
2262 | } | |
2263 | ||
b00b8dae MKL |
2264 | static void btf_enum_seq_show(const struct btf *btf, const struct btf_type *t, |
2265 | u32 type_id, void *data, u8 bits_offset, | |
2266 | struct seq_file *m) | |
2267 | { | |
2268 | const struct btf_enum *enums = btf_type_enum(t); | |
2269 | u32 i, nr_enums = btf_type_vlen(t); | |
2270 | int v = *(int *)data; | |
2271 | ||
2272 | for (i = 0; i < nr_enums; i++) { | |
2273 | if (v == enums[i].val) { | |
2274 | seq_printf(m, "%s", | |
23127b33 MKL |
2275 | __btf_name_by_offset(btf, |
2276 | enums[i].name_off)); | |
b00b8dae MKL |
2277 | return; |
2278 | } | |
2279 | } | |
2280 | ||
2281 | seq_printf(m, "%d", v); | |
2282 | } | |
2283 | ||
69b693f0 MKL |
2284 | static struct btf_kind_operations enum_ops = { |
2285 | .check_meta = btf_enum_check_meta, | |
eb3f595d | 2286 | .resolve = btf_df_resolve, |
179cde8c | 2287 | .check_member = btf_enum_check_member, |
9d5f9f70 | 2288 | .check_kflag_member = btf_enum_check_kflag_member, |
69b693f0 | 2289 | .log_details = btf_enum_log, |
b00b8dae | 2290 | .seq_show = btf_enum_seq_show, |
69b693f0 MKL |
2291 | }; |
2292 | ||
2667a262 MKL |
2293 | static s32 btf_func_proto_check_meta(struct btf_verifier_env *env, |
2294 | const struct btf_type *t, | |
2295 | u32 meta_left) | |
2296 | { | |
2297 | u32 meta_needed = btf_type_vlen(t) * sizeof(struct btf_param); | |
2298 | ||
2299 | if (meta_left < meta_needed) { | |
2300 | btf_verifier_log_basic(env, t, | |
2301 | "meta_left:%u meta_needed:%u", | |
2302 | meta_left, meta_needed); | |
2303 | return -EINVAL; | |
2304 | } | |
2305 | ||
2306 | if (t->name_off) { | |
2307 | btf_verifier_log_type(env, t, "Invalid name"); | |
2308 | return -EINVAL; | |
2309 | } | |
2310 | ||
9d5f9f70 YS |
2311 | if (btf_type_kflag(t)) { |
2312 | btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); | |
2313 | return -EINVAL; | |
2314 | } | |
2315 | ||
2667a262 MKL |
2316 | btf_verifier_log_type(env, t, NULL); |
2317 | ||
2318 | return meta_needed; | |
2319 | } | |
2320 | ||
2321 | static void btf_func_proto_log(struct btf_verifier_env *env, | |
2322 | const struct btf_type *t) | |
2323 | { | |
2324 | const struct btf_param *args = (const struct btf_param *)(t + 1); | |
2325 | u16 nr_args = btf_type_vlen(t), i; | |
2326 | ||
2327 | btf_verifier_log(env, "return=%u args=(", t->type); | |
2328 | if (!nr_args) { | |
2329 | btf_verifier_log(env, "void"); | |
2330 | goto done; | |
2331 | } | |
2332 | ||
2333 | if (nr_args == 1 && !args[0].type) { | |
2334 | /* Only one vararg */ | |
2335 | btf_verifier_log(env, "vararg"); | |
2336 | goto done; | |
2337 | } | |
2338 | ||
2339 | btf_verifier_log(env, "%u %s", args[0].type, | |
23127b33 MKL |
2340 | __btf_name_by_offset(env->btf, |
2341 | args[0].name_off)); | |
2667a262 MKL |
2342 | for (i = 1; i < nr_args - 1; i++) |
2343 | btf_verifier_log(env, ", %u %s", args[i].type, | |
23127b33 MKL |
2344 | __btf_name_by_offset(env->btf, |
2345 | args[i].name_off)); | |
2667a262 MKL |
2346 | |
2347 | if (nr_args > 1) { | |
2348 | const struct btf_param *last_arg = &args[nr_args - 1]; | |
2349 | ||
2350 | if (last_arg->type) | |
2351 | btf_verifier_log(env, ", %u %s", last_arg->type, | |
23127b33 MKL |
2352 | __btf_name_by_offset(env->btf, |
2353 | last_arg->name_off)); | |
2667a262 MKL |
2354 | else |
2355 | btf_verifier_log(env, ", vararg"); | |
2356 | } | |
2357 | ||
2358 | done: | |
2359 | btf_verifier_log(env, ")"); | |
2360 | } | |
2361 | ||
2362 | static struct btf_kind_operations func_proto_ops = { | |
2363 | .check_meta = btf_func_proto_check_meta, | |
2364 | .resolve = btf_df_resolve, | |
2365 | /* | |
2366 | * BTF_KIND_FUNC_PROTO cannot be directly referred by | |
2367 | * a struct's member. | |
2368 | * | |
2369 | * It should be a funciton pointer instead. | |
2370 | * (i.e. struct's member -> BTF_KIND_PTR -> BTF_KIND_FUNC_PROTO) | |
2371 | * | |
2372 | * Hence, there is no btf_func_check_member(). | |
2373 | */ | |
2374 | .check_member = btf_df_check_member, | |
9d5f9f70 | 2375 | .check_kflag_member = btf_df_check_kflag_member, |
2667a262 MKL |
2376 | .log_details = btf_func_proto_log, |
2377 | .seq_show = btf_df_seq_show, | |
2378 | }; | |
2379 | ||
2380 | static s32 btf_func_check_meta(struct btf_verifier_env *env, | |
2381 | const struct btf_type *t, | |
2382 | u32 meta_left) | |
2383 | { | |
2384 | if (!t->name_off || | |
2385 | !btf_name_valid_identifier(env->btf, t->name_off)) { | |
2386 | btf_verifier_log_type(env, t, "Invalid name"); | |
2387 | return -EINVAL; | |
2388 | } | |
2389 | ||
2390 | if (btf_type_vlen(t)) { | |
2391 | btf_verifier_log_type(env, t, "vlen != 0"); | |
2392 | return -EINVAL; | |
2393 | } | |
2394 | ||
9d5f9f70 YS |
2395 | if (btf_type_kflag(t)) { |
2396 | btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); | |
2397 | return -EINVAL; | |
2398 | } | |
2399 | ||
2667a262 MKL |
2400 | btf_verifier_log_type(env, t, NULL); |
2401 | ||
2402 | return 0; | |
2403 | } | |
2404 | ||
2405 | static struct btf_kind_operations func_ops = { | |
2406 | .check_meta = btf_func_check_meta, | |
2407 | .resolve = btf_df_resolve, | |
2408 | .check_member = btf_df_check_member, | |
9d5f9f70 | 2409 | .check_kflag_member = btf_df_check_kflag_member, |
2667a262 MKL |
2410 | .log_details = btf_ref_type_log, |
2411 | .seq_show = btf_df_seq_show, | |
2412 | }; | |
2413 | ||
2414 | static int btf_func_proto_check(struct btf_verifier_env *env, | |
2415 | const struct btf_type *t) | |
2416 | { | |
2417 | const struct btf_type *ret_type; | |
2418 | const struct btf_param *args; | |
2419 | const struct btf *btf; | |
2420 | u16 nr_args, i; | |
2421 | int err; | |
2422 | ||
2423 | btf = env->btf; | |
2424 | args = (const struct btf_param *)(t + 1); | |
2425 | nr_args = btf_type_vlen(t); | |
2426 | ||
2427 | /* Check func return type which could be "void" (t->type == 0) */ | |
2428 | if (t->type) { | |
2429 | u32 ret_type_id = t->type; | |
2430 | ||
2431 | ret_type = btf_type_by_id(btf, ret_type_id); | |
2432 | if (!ret_type) { | |
2433 | btf_verifier_log_type(env, t, "Invalid return type"); | |
2434 | return -EINVAL; | |
2435 | } | |
2436 | ||
2437 | if (btf_type_needs_resolve(ret_type) && | |
2438 | !env_type_is_resolved(env, ret_type_id)) { | |
2439 | err = btf_resolve(env, ret_type, ret_type_id); | |
2440 | if (err) | |
2441 | return err; | |
2442 | } | |
2443 | ||
2444 | /* Ensure the return type is a type that has a size */ | |
2445 | if (!btf_type_id_size(btf, &ret_type_id, NULL)) { | |
2446 | btf_verifier_log_type(env, t, "Invalid return type"); | |
2447 | return -EINVAL; | |
2448 | } | |
2449 | } | |
2450 | ||
2451 | if (!nr_args) | |
2452 | return 0; | |
2453 | ||
2454 | /* Last func arg type_id could be 0 if it is a vararg */ | |
2455 | if (!args[nr_args - 1].type) { | |
2456 | if (args[nr_args - 1].name_off) { | |
2457 | btf_verifier_log_type(env, t, "Invalid arg#%u", | |
2458 | nr_args); | |
2459 | return -EINVAL; | |
2460 | } | |
2461 | nr_args--; | |
2462 | } | |
2463 | ||
2464 | err = 0; | |
2465 | for (i = 0; i < nr_args; i++) { | |
2466 | const struct btf_type *arg_type; | |
2467 | u32 arg_type_id; | |
2468 | ||
2469 | arg_type_id = args[i].type; | |
2470 | arg_type = btf_type_by_id(btf, arg_type_id); | |
2471 | if (!arg_type) { | |
2472 | btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1); | |
2473 | err = -EINVAL; | |
2474 | break; | |
2475 | } | |
2476 | ||
2477 | if (args[i].name_off && | |
2478 | (!btf_name_offset_valid(btf, args[i].name_off) || | |
2479 | !btf_name_valid_identifier(btf, args[i].name_off))) { | |
2480 | btf_verifier_log_type(env, t, | |
2481 | "Invalid arg#%u", i + 1); | |
2482 | err = -EINVAL; | |
2483 | break; | |
2484 | } | |
2485 | ||
2486 | if (btf_type_needs_resolve(arg_type) && | |
2487 | !env_type_is_resolved(env, arg_type_id)) { | |
2488 | err = btf_resolve(env, arg_type, arg_type_id); | |
2489 | if (err) | |
2490 | break; | |
2491 | } | |
2492 | ||
2493 | if (!btf_type_id_size(btf, &arg_type_id, NULL)) { | |
2494 | btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1); | |
2495 | err = -EINVAL; | |
2496 | break; | |
2497 | } | |
2498 | } | |
2499 | ||
2500 | return err; | |
2501 | } | |
2502 | ||
2503 | static int btf_func_check(struct btf_verifier_env *env, | |
2504 | const struct btf_type *t) | |
2505 | { | |
2506 | const struct btf_type *proto_type; | |
2507 | const struct btf_param *args; | |
2508 | const struct btf *btf; | |
2509 | u16 nr_args, i; | |
2510 | ||
2511 | btf = env->btf; | |
2512 | proto_type = btf_type_by_id(btf, t->type); | |
2513 | ||
2514 | if (!proto_type || !btf_type_is_func_proto(proto_type)) { | |
2515 | btf_verifier_log_type(env, t, "Invalid type_id"); | |
2516 | return -EINVAL; | |
2517 | } | |
2518 | ||
2519 | args = (const struct btf_param *)(proto_type + 1); | |
2520 | nr_args = btf_type_vlen(proto_type); | |
2521 | for (i = 0; i < nr_args; i++) { | |
2522 | if (!args[i].name_off && args[i].type) { | |
2523 | btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1); | |
2524 | return -EINVAL; | |
2525 | } | |
2526 | } | |
2527 | ||
2528 | return 0; | |
2529 | } | |
2530 | ||
69b693f0 MKL |
2531 | static const struct btf_kind_operations * const kind_ops[NR_BTF_KINDS] = { |
2532 | [BTF_KIND_INT] = &int_ops, | |
2533 | [BTF_KIND_PTR] = &ptr_ops, | |
2534 | [BTF_KIND_ARRAY] = &array_ops, | |
2535 | [BTF_KIND_STRUCT] = &struct_ops, | |
2536 | [BTF_KIND_UNION] = &struct_ops, | |
2537 | [BTF_KIND_ENUM] = &enum_ops, | |
2538 | [BTF_KIND_FWD] = &fwd_ops, | |
2539 | [BTF_KIND_TYPEDEF] = &modifier_ops, | |
2540 | [BTF_KIND_VOLATILE] = &modifier_ops, | |
2541 | [BTF_KIND_CONST] = &modifier_ops, | |
2542 | [BTF_KIND_RESTRICT] = &modifier_ops, | |
2667a262 MKL |
2543 | [BTF_KIND_FUNC] = &func_ops, |
2544 | [BTF_KIND_FUNC_PROTO] = &func_proto_ops, | |
69b693f0 MKL |
2545 | }; |
2546 | ||
2547 | static s32 btf_check_meta(struct btf_verifier_env *env, | |
2548 | const struct btf_type *t, | |
2549 | u32 meta_left) | |
2550 | { | |
2551 | u32 saved_meta_left = meta_left; | |
2552 | s32 var_meta_size; | |
2553 | ||
2554 | if (meta_left < sizeof(*t)) { | |
2555 | btf_verifier_log(env, "[%u] meta_left:%u meta_needed:%zu", | |
2556 | env->log_type_id, meta_left, sizeof(*t)); | |
2557 | return -EINVAL; | |
2558 | } | |
2559 | meta_left -= sizeof(*t); | |
2560 | ||
aea2f7b8 MKL |
2561 | if (t->info & ~BTF_INFO_MASK) { |
2562 | btf_verifier_log(env, "[%u] Invalid btf_info:%x", | |
2563 | env->log_type_id, t->info); | |
2564 | return -EINVAL; | |
2565 | } | |
2566 | ||
69b693f0 MKL |
2567 | if (BTF_INFO_KIND(t->info) > BTF_KIND_MAX || |
2568 | BTF_INFO_KIND(t->info) == BTF_KIND_UNKN) { | |
2569 | btf_verifier_log(env, "[%u] Invalid kind:%u", | |
2570 | env->log_type_id, BTF_INFO_KIND(t->info)); | |
2571 | return -EINVAL; | |
2572 | } | |
2573 | ||
fbcf93eb | 2574 | if (!btf_name_offset_valid(env->btf, t->name_off)) { |
69b693f0 | 2575 | btf_verifier_log(env, "[%u] Invalid name_offset:%u", |
fbcf93eb | 2576 | env->log_type_id, t->name_off); |
69b693f0 MKL |
2577 | return -EINVAL; |
2578 | } | |
2579 | ||
2580 | var_meta_size = btf_type_ops(t)->check_meta(env, t, meta_left); | |
2581 | if (var_meta_size < 0) | |
2582 | return var_meta_size; | |
2583 | ||
2584 | meta_left -= var_meta_size; | |
2585 | ||
2586 | return saved_meta_left - meta_left; | |
2587 | } | |
2588 | ||
2589 | static int btf_check_all_metas(struct btf_verifier_env *env) | |
2590 | { | |
2591 | struct btf *btf = env->btf; | |
2592 | struct btf_header *hdr; | |
2593 | void *cur, *end; | |
2594 | ||
f80442a4 | 2595 | hdr = &btf->hdr; |
69b693f0 | 2596 | cur = btf->nohdr_data + hdr->type_off; |
4b1c5d91 | 2597 | end = cur + hdr->type_len; |
69b693f0 MKL |
2598 | |
2599 | env->log_type_id = 1; | |
2600 | while (cur < end) { | |
2601 | struct btf_type *t = cur; | |
2602 | s32 meta_size; | |
2603 | ||
2604 | meta_size = btf_check_meta(env, t, end - cur); | |
2605 | if (meta_size < 0) | |
2606 | return meta_size; | |
2607 | ||
2608 | btf_add_type(env, t); | |
2609 | cur += meta_size; | |
2610 | env->log_type_id++; | |
2611 | } | |
2612 | ||
2613 | return 0; | |
2614 | } | |
2615 | ||
eb3f595d MKL |
2616 | static bool btf_resolve_valid(struct btf_verifier_env *env, |
2617 | const struct btf_type *t, | |
2618 | u32 type_id) | |
2619 | { | |
2620 | struct btf *btf = env->btf; | |
2621 | ||
2622 | if (!env_type_is_resolved(env, type_id)) | |
2623 | return false; | |
2624 | ||
2625 | if (btf_type_is_struct(t)) | |
2626 | return !btf->resolved_ids[type_id] && | |
2627 | !btf->resolved_sizes[type_id]; | |
2628 | ||
2629 | if (btf_type_is_modifier(t) || btf_type_is_ptr(t)) { | |
2630 | t = btf_type_id_resolve(btf, &type_id); | |
2631 | return t && !btf_type_is_modifier(t); | |
2632 | } | |
2633 | ||
2634 | if (btf_type_is_array(t)) { | |
2635 | const struct btf_array *array = btf_type_array(t); | |
2636 | const struct btf_type *elem_type; | |
2637 | u32 elem_type_id = array->type; | |
2638 | u32 elem_size; | |
2639 | ||
2640 | elem_type = btf_type_id_size(btf, &elem_type_id, &elem_size); | |
2641 | return elem_type && !btf_type_is_modifier(elem_type) && | |
2642 | (array->nelems * elem_size == | |
2643 | btf->resolved_sizes[type_id]); | |
2644 | } | |
2645 | ||
2646 | return false; | |
2647 | } | |
2648 | ||
2667a262 MKL |
2649 | static int btf_resolve(struct btf_verifier_env *env, |
2650 | const struct btf_type *t, u32 type_id) | |
2651 | { | |
2652 | u32 save_log_type_id = env->log_type_id; | |
2653 | const struct resolve_vertex *v; | |
2654 | int err = 0; | |
2655 | ||
2656 | env->resolve_mode = RESOLVE_TBD; | |
2657 | env_stack_push(env, t, type_id); | |
2658 | while (!err && (v = env_stack_peak(env))) { | |
2659 | env->log_type_id = v->type_id; | |
2660 | err = btf_type_ops(v->t)->resolve(env, v); | |
2661 | } | |
2662 | ||
2663 | env->log_type_id = type_id; | |
2664 | if (err == -E2BIG) { | |
2665 | btf_verifier_log_type(env, t, | |
2666 | "Exceeded max resolving depth:%u", | |
2667 | MAX_RESOLVE_DEPTH); | |
2668 | } else if (err == -EEXIST) { | |
2669 | btf_verifier_log_type(env, t, "Loop detected"); | |
2670 | } | |
2671 | ||
2672 | /* Final sanity check */ | |
2673 | if (!err && !btf_resolve_valid(env, t, type_id)) { | |
2674 | btf_verifier_log_type(env, t, "Invalid resolve state"); | |
2675 | err = -EINVAL; | |
2676 | } | |
2677 | ||
2678 | env->log_type_id = save_log_type_id; | |
2679 | return err; | |
2680 | } | |
2681 | ||
eb3f595d MKL |
2682 | static int btf_check_all_types(struct btf_verifier_env *env) |
2683 | { | |
2684 | struct btf *btf = env->btf; | |
2685 | u32 type_id; | |
2686 | int err; | |
2687 | ||
2688 | err = env_resolve_init(env); | |
2689 | if (err) | |
2690 | return err; | |
2691 | ||
2692 | env->phase++; | |
2693 | for (type_id = 1; type_id <= btf->nr_types; type_id++) { | |
2694 | const struct btf_type *t = btf_type_by_id(btf, type_id); | |
2695 | ||
2696 | env->log_type_id = type_id; | |
2697 | if (btf_type_needs_resolve(t) && | |
2698 | !env_type_is_resolved(env, type_id)) { | |
2699 | err = btf_resolve(env, t, type_id); | |
2700 | if (err) | |
2701 | return err; | |
2702 | } | |
2703 | ||
2667a262 MKL |
2704 | if (btf_type_is_func_proto(t)) { |
2705 | err = btf_func_proto_check(env, t); | |
2706 | if (err) | |
2707 | return err; | |
2708 | } | |
2709 | ||
2710 | if (btf_type_is_func(t)) { | |
2711 | err = btf_func_check(env, t); | |
2712 | if (err) | |
2713 | return err; | |
eb3f595d MKL |
2714 | } |
2715 | } | |
2716 | ||
2717 | return 0; | |
2718 | } | |
2719 | ||
69b693f0 MKL |
2720 | static int btf_parse_type_sec(struct btf_verifier_env *env) |
2721 | { | |
f80442a4 | 2722 | const struct btf_header *hdr = &env->btf->hdr; |
eb3f595d MKL |
2723 | int err; |
2724 | ||
f80442a4 MKL |
2725 | /* Type section must align to 4 bytes */ |
2726 | if (hdr->type_off & (sizeof(u32) - 1)) { | |
2727 | btf_verifier_log(env, "Unaligned type_off"); | |
2728 | return -EINVAL; | |
2729 | } | |
2730 | ||
2731 | if (!hdr->type_len) { | |
2732 | btf_verifier_log(env, "No type found"); | |
2733 | return -EINVAL; | |
2734 | } | |
2735 | ||
eb3f595d MKL |
2736 | err = btf_check_all_metas(env); |
2737 | if (err) | |
2738 | return err; | |
2739 | ||
2740 | return btf_check_all_types(env); | |
69b693f0 MKL |
2741 | } |
2742 | ||
2743 | static int btf_parse_str_sec(struct btf_verifier_env *env) | |
2744 | { | |
2745 | const struct btf_header *hdr; | |
2746 | struct btf *btf = env->btf; | |
2747 | const char *start, *end; | |
2748 | ||
f80442a4 | 2749 | hdr = &btf->hdr; |
69b693f0 MKL |
2750 | start = btf->nohdr_data + hdr->str_off; |
2751 | end = start + hdr->str_len; | |
2752 | ||
f80442a4 MKL |
2753 | if (end != btf->data + btf->data_size) { |
2754 | btf_verifier_log(env, "String section is not at the end"); | |
2755 | return -EINVAL; | |
2756 | } | |
2757 | ||
69b693f0 MKL |
2758 | if (!hdr->str_len || hdr->str_len - 1 > BTF_MAX_NAME_OFFSET || |
2759 | start[0] || end[-1]) { | |
2760 | btf_verifier_log(env, "Invalid string section"); | |
2761 | return -EINVAL; | |
2762 | } | |
2763 | ||
2764 | btf->strings = start; | |
2765 | ||
2766 | return 0; | |
2767 | } | |
2768 | ||
f80442a4 MKL |
2769 | static const size_t btf_sec_info_offset[] = { |
2770 | offsetof(struct btf_header, type_off), | |
2771 | offsetof(struct btf_header, str_off), | |
2772 | }; | |
2773 | ||
2774 | static int btf_sec_info_cmp(const void *a, const void *b) | |
69b693f0 | 2775 | { |
f80442a4 MKL |
2776 | const struct btf_sec_info *x = a; |
2777 | const struct btf_sec_info *y = b; | |
2778 | ||
2779 | return (int)(x->off - y->off) ? : (int)(x->len - y->len); | |
2780 | } | |
2781 | ||
2782 | static int btf_check_sec_info(struct btf_verifier_env *env, | |
2783 | u32 btf_data_size) | |
2784 | { | |
a2889a4c | 2785 | struct btf_sec_info secs[ARRAY_SIZE(btf_sec_info_offset)]; |
f80442a4 | 2786 | u32 total, expected_total, i; |
69b693f0 | 2787 | const struct btf_header *hdr; |
f80442a4 MKL |
2788 | const struct btf *btf; |
2789 | ||
2790 | btf = env->btf; | |
2791 | hdr = &btf->hdr; | |
2792 | ||
2793 | /* Populate the secs from hdr */ | |
a2889a4c | 2794 | for (i = 0; i < ARRAY_SIZE(btf_sec_info_offset); i++) |
f80442a4 MKL |
2795 | secs[i] = *(struct btf_sec_info *)((void *)hdr + |
2796 | btf_sec_info_offset[i]); | |
2797 | ||
a2889a4c MKL |
2798 | sort(secs, ARRAY_SIZE(btf_sec_info_offset), |
2799 | sizeof(struct btf_sec_info), btf_sec_info_cmp, NULL); | |
f80442a4 MKL |
2800 | |
2801 | /* Check for gaps and overlap among sections */ | |
2802 | total = 0; | |
2803 | expected_total = btf_data_size - hdr->hdr_len; | |
a2889a4c | 2804 | for (i = 0; i < ARRAY_SIZE(btf_sec_info_offset); i++) { |
f80442a4 MKL |
2805 | if (expected_total < secs[i].off) { |
2806 | btf_verifier_log(env, "Invalid section offset"); | |
2807 | return -EINVAL; | |
2808 | } | |
2809 | if (total < secs[i].off) { | |
2810 | /* gap */ | |
2811 | btf_verifier_log(env, "Unsupported section found"); | |
2812 | return -EINVAL; | |
2813 | } | |
2814 | if (total > secs[i].off) { | |
2815 | btf_verifier_log(env, "Section overlap found"); | |
2816 | return -EINVAL; | |
2817 | } | |
2818 | if (expected_total - total < secs[i].len) { | |
2819 | btf_verifier_log(env, | |
2820 | "Total section length too long"); | |
2821 | return -EINVAL; | |
2822 | } | |
2823 | total += secs[i].len; | |
2824 | } | |
2825 | ||
2826 | /* There is data other than hdr and known sections */ | |
2827 | if (expected_total != total) { | |
2828 | btf_verifier_log(env, "Unsupported section found"); | |
2829 | return -EINVAL; | |
2830 | } | |
2831 | ||
2832 | return 0; | |
2833 | } | |
2834 | ||
4a6998af | 2835 | static int btf_parse_hdr(struct btf_verifier_env *env) |
f80442a4 | 2836 | { |
4a6998af | 2837 | u32 hdr_len, hdr_copy, btf_data_size; |
f80442a4 | 2838 | const struct btf_header *hdr; |
f80442a4 MKL |
2839 | struct btf *btf; |
2840 | int err; | |
69b693f0 | 2841 | |
f80442a4 | 2842 | btf = env->btf; |
4a6998af | 2843 | btf_data_size = btf->data_size; |
f80442a4 | 2844 | |
4a6998af ML |
2845 | if (btf_data_size < |
2846 | offsetof(struct btf_header, hdr_len) + sizeof(hdr->hdr_len)) { | |
f80442a4 MKL |
2847 | btf_verifier_log(env, "hdr_len not found"); |
2848 | return -EINVAL; | |
2849 | } | |
2850 | ||
4a6998af ML |
2851 | hdr = btf->data; |
2852 | hdr_len = hdr->hdr_len; | |
f80442a4 | 2853 | if (btf_data_size < hdr_len) { |
69b693f0 MKL |
2854 | btf_verifier_log(env, "btf_header not found"); |
2855 | return -EINVAL; | |
2856 | } | |
2857 | ||
4a6998af ML |
2858 | /* Ensure the unsupported header fields are zero */ |
2859 | if (hdr_len > sizeof(btf->hdr)) { | |
2860 | u8 *expected_zero = btf->data + sizeof(btf->hdr); | |
2861 | u8 *end = btf->data + hdr_len; | |
2862 | ||
2863 | for (; expected_zero < end; expected_zero++) { | |
2864 | if (*expected_zero) { | |
2865 | btf_verifier_log(env, "Unsupported btf_header"); | |
2866 | return -E2BIG; | |
2867 | } | |
2868 | } | |
f80442a4 MKL |
2869 | } |
2870 | ||
2871 | hdr_copy = min_t(u32, hdr_len, sizeof(btf->hdr)); | |
4a6998af | 2872 | memcpy(&btf->hdr, btf->data, hdr_copy); |
f80442a4 MKL |
2873 | |
2874 | hdr = &btf->hdr; | |
2875 | ||
2876 | btf_verifier_log_hdr(env, btf_data_size); | |
69b693f0 | 2877 | |
69b693f0 MKL |
2878 | if (hdr->magic != BTF_MAGIC) { |
2879 | btf_verifier_log(env, "Invalid magic"); | |
2880 | return -EINVAL; | |
2881 | } | |
2882 | ||
2883 | if (hdr->version != BTF_VERSION) { | |
2884 | btf_verifier_log(env, "Unsupported version"); | |
2885 | return -ENOTSUPP; | |
2886 | } | |
2887 | ||
2888 | if (hdr->flags) { | |
2889 | btf_verifier_log(env, "Unsupported flags"); | |
2890 | return -ENOTSUPP; | |
2891 | } | |
2892 | ||
f80442a4 | 2893 | if (btf_data_size == hdr->hdr_len) { |
69b693f0 MKL |
2894 | btf_verifier_log(env, "No data"); |
2895 | return -EINVAL; | |
2896 | } | |
2897 | ||
f80442a4 MKL |
2898 | err = btf_check_sec_info(env, btf_data_size); |
2899 | if (err) | |
2900 | return err; | |
69b693f0 MKL |
2901 | |
2902 | return 0; | |
2903 | } | |
2904 | ||
2905 | static struct btf *btf_parse(void __user *btf_data, u32 btf_data_size, | |
2906 | u32 log_level, char __user *log_ubuf, u32 log_size) | |
2907 | { | |
2908 | struct btf_verifier_env *env = NULL; | |
2909 | struct bpf_verifier_log *log; | |
2910 | struct btf *btf = NULL; | |
2911 | u8 *data; | |
2912 | int err; | |
2913 | ||
2914 | if (btf_data_size > BTF_MAX_SIZE) | |
2915 | return ERR_PTR(-E2BIG); | |
2916 | ||
2917 | env = kzalloc(sizeof(*env), GFP_KERNEL | __GFP_NOWARN); | |
2918 | if (!env) | |
2919 | return ERR_PTR(-ENOMEM); | |
2920 | ||
2921 | log = &env->log; | |
2922 | if (log_level || log_ubuf || log_size) { | |
2923 | /* user requested verbose verifier output | |
2924 | * and supplied buffer to store the verification trace | |
2925 | */ | |
2926 | log->level = log_level; | |
2927 | log->ubuf = log_ubuf; | |
2928 | log->len_total = log_size; | |
2929 | ||
2930 | /* log attributes have to be sane */ | |
2931 | if (log->len_total < 128 || log->len_total > UINT_MAX >> 8 || | |
2932 | !log->level || !log->ubuf) { | |
2933 | err = -EINVAL; | |
2934 | goto errout; | |
2935 | } | |
2936 | } | |
2937 | ||
2938 | btf = kzalloc(sizeof(*btf), GFP_KERNEL | __GFP_NOWARN); | |
2939 | if (!btf) { | |
2940 | err = -ENOMEM; | |
2941 | goto errout; | |
2942 | } | |
f80442a4 MKL |
2943 | env->btf = btf; |
2944 | ||
69b693f0 MKL |
2945 | data = kvmalloc(btf_data_size, GFP_KERNEL | __GFP_NOWARN); |
2946 | if (!data) { | |
2947 | err = -ENOMEM; | |
2948 | goto errout; | |
2949 | } | |
2950 | ||
2951 | btf->data = data; | |
2952 | btf->data_size = btf_data_size; | |
2953 | ||
2954 | if (copy_from_user(data, btf_data, btf_data_size)) { | |
2955 | err = -EFAULT; | |
2956 | goto errout; | |
2957 | } | |
2958 | ||
4a6998af ML |
2959 | err = btf_parse_hdr(env); |
2960 | if (err) | |
2961 | goto errout; | |
2962 | ||
2963 | btf->nohdr_data = btf->data + btf->hdr.hdr_len; | |
2964 | ||
69b693f0 MKL |
2965 | err = btf_parse_str_sec(env); |
2966 | if (err) | |
2967 | goto errout; | |
2968 | ||
2969 | err = btf_parse_type_sec(env); | |
2970 | if (err) | |
2971 | goto errout; | |
2972 | ||
f80442a4 | 2973 | if (log->level && bpf_verifier_log_full(log)) { |
69b693f0 MKL |
2974 | err = -ENOSPC; |
2975 | goto errout; | |
2976 | } | |
2977 | ||
f80442a4 MKL |
2978 | btf_verifier_env_free(env); |
2979 | refcount_set(&btf->refcnt, 1); | |
2980 | return btf; | |
69b693f0 MKL |
2981 | |
2982 | errout: | |
2983 | btf_verifier_env_free(env); | |
2984 | if (btf) | |
2985 | btf_free(btf); | |
2986 | return ERR_PTR(err); | |
2987 | } | |
b00b8dae MKL |
2988 | |
2989 | void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj, | |
2990 | struct seq_file *m) | |
2991 | { | |
2992 | const struct btf_type *t = btf_type_by_id(btf, type_id); | |
2993 | ||
2994 | btf_type_ops(t)->seq_show(btf, t, type_id, obj, 0, m); | |
2995 | } | |
f56a653c MKL |
2996 | |
2997 | static int btf_release(struct inode *inode, struct file *filp) | |
2998 | { | |
2999 | btf_put(filp->private_data); | |
3000 | return 0; | |
3001 | } | |
3002 | ||
60197cfb | 3003 | const struct file_operations btf_fops = { |
f56a653c MKL |
3004 | .release = btf_release, |
3005 | }; | |
3006 | ||
78958fca MKL |
3007 | static int __btf_new_fd(struct btf *btf) |
3008 | { | |
3009 | return anon_inode_getfd("btf", &btf_fops, btf, O_RDONLY | O_CLOEXEC); | |
3010 | } | |
3011 | ||
f56a653c MKL |
3012 | int btf_new_fd(const union bpf_attr *attr) |
3013 | { | |
3014 | struct btf *btf; | |
78958fca | 3015 | int ret; |
f56a653c MKL |
3016 | |
3017 | btf = btf_parse(u64_to_user_ptr(attr->btf), | |
3018 | attr->btf_size, attr->btf_log_level, | |
3019 | u64_to_user_ptr(attr->btf_log_buf), | |
3020 | attr->btf_log_size); | |
3021 | if (IS_ERR(btf)) | |
3022 | return PTR_ERR(btf); | |
3023 | ||
78958fca MKL |
3024 | ret = btf_alloc_id(btf); |
3025 | if (ret) { | |
3026 | btf_free(btf); | |
3027 | return ret; | |
3028 | } | |
3029 | ||
3030 | /* | |
3031 | * The BTF ID is published to the userspace. | |
3032 | * All BTF free must go through call_rcu() from | |
3033 | * now on (i.e. free by calling btf_put()). | |
3034 | */ | |
3035 | ||
3036 | ret = __btf_new_fd(btf); | |
3037 | if (ret < 0) | |
f56a653c MKL |
3038 | btf_put(btf); |
3039 | ||
78958fca | 3040 | return ret; |
f56a653c MKL |
3041 | } |
3042 | ||
3043 | struct btf *btf_get_by_fd(int fd) | |
3044 | { | |
3045 | struct btf *btf; | |
3046 | struct fd f; | |
3047 | ||
3048 | f = fdget(fd); | |
3049 | ||
3050 | if (!f.file) | |
3051 | return ERR_PTR(-EBADF); | |
3052 | ||
3053 | if (f.file->f_op != &btf_fops) { | |
3054 | fdput(f); | |
3055 | return ERR_PTR(-EINVAL); | |
3056 | } | |
3057 | ||
3058 | btf = f.file->private_data; | |
78958fca | 3059 | refcount_inc(&btf->refcnt); |
f56a653c MKL |
3060 | fdput(f); |
3061 | ||
3062 | return btf; | |
3063 | } | |
60197cfb MKL |
3064 | |
3065 | int btf_get_info_by_fd(const struct btf *btf, | |
3066 | const union bpf_attr *attr, | |
3067 | union bpf_attr __user *uattr) | |
3068 | { | |
62dab84c MKL |
3069 | struct bpf_btf_info __user *uinfo; |
3070 | struct bpf_btf_info info = {}; | |
3071 | u32 info_copy, btf_copy; | |
3072 | void __user *ubtf; | |
3073 | u32 uinfo_len; | |
60197cfb | 3074 | |
62dab84c MKL |
3075 | uinfo = u64_to_user_ptr(attr->info.info); |
3076 | uinfo_len = attr->info.info_len; | |
3077 | ||
3078 | info_copy = min_t(u32, uinfo_len, sizeof(info)); | |
3079 | if (copy_from_user(&info, uinfo, info_copy)) | |
3080 | return -EFAULT; | |
3081 | ||
3082 | info.id = btf->id; | |
3083 | ubtf = u64_to_user_ptr(info.btf); | |
3084 | btf_copy = min_t(u32, btf->data_size, info.btf_size); | |
3085 | if (copy_to_user(ubtf, btf->data, btf_copy)) | |
3086 | return -EFAULT; | |
3087 | info.btf_size = btf->data_size; | |
3088 | ||
3089 | if (copy_to_user(uinfo, &info, info_copy) || | |
3090 | put_user(info_copy, &uattr->info.info_len)) | |
60197cfb MKL |
3091 | return -EFAULT; |
3092 | ||
3093 | return 0; | |
3094 | } | |
78958fca MKL |
3095 | |
3096 | int btf_get_fd_by_id(u32 id) | |
3097 | { | |
3098 | struct btf *btf; | |
3099 | int fd; | |
3100 | ||
3101 | rcu_read_lock(); | |
3102 | btf = idr_find(&btf_idr, id); | |
3103 | if (!btf || !refcount_inc_not_zero(&btf->refcnt)) | |
3104 | btf = ERR_PTR(-ENOENT); | |
3105 | rcu_read_unlock(); | |
3106 | ||
3107 | if (IS_ERR(btf)) | |
3108 | return PTR_ERR(btf); | |
3109 | ||
3110 | fd = __btf_new_fd(btf); | |
3111 | if (fd < 0) | |
3112 | btf_put(btf); | |
3113 | ||
3114 | return fd; | |
3115 | } | |
3116 | ||
3117 | u32 btf_id(const struct btf *btf) | |
3118 | { | |
3119 | return btf->id; | |
3120 | } |