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> | |
6 | #include <linux/compiler.h> | |
7 | #include <linux/errno.h> | |
8 | #include <linux/slab.h> | |
9 | #include <linux/uaccess.h> | |
10 | #include <linux/kernel.h> | |
11 | #include <linux/bpf_verifier.h> | |
12 | #include <linux/btf.h> | |
13 | ||
14 | /* BTF (BPF Type Format) is the meta data format which describes | |
15 | * the data types of BPF program/map. Hence, it basically focus | |
16 | * on the C programming language which the modern BPF is primary | |
17 | * using. | |
18 | * | |
19 | * ELF Section: | |
20 | * ~~~~~~~~~~~ | |
21 | * The BTF data is stored under the ".BTF" ELF section | |
22 | * | |
23 | * struct btf_type: | |
24 | * ~~~~~~~~~~~~~~~ | |
25 | * Each 'struct btf_type' object describes a C data type. | |
26 | * Depending on the type it is describing, a 'struct btf_type' | |
27 | * object may be followed by more data. F.e. | |
28 | * To describe an array, 'struct btf_type' is followed by | |
29 | * 'struct btf_array'. | |
30 | * | |
31 | * 'struct btf_type' and any extra data following it are | |
32 | * 4 bytes aligned. | |
33 | * | |
34 | * Type section: | |
35 | * ~~~~~~~~~~~~~ | |
36 | * The BTF type section contains a list of 'struct btf_type' objects. | |
37 | * Each one describes a C type. Recall from the above section | |
38 | * that a 'struct btf_type' object could be immediately followed by extra | |
39 | * data in order to desribe some particular C types. | |
40 | * | |
41 | * type_id: | |
42 | * ~~~~~~~ | |
43 | * Each btf_type object is identified by a type_id. The type_id | |
44 | * is implicitly implied by the location of the btf_type object in | |
45 | * the BTF type section. The first one has type_id 1. The second | |
46 | * one has type_id 2...etc. Hence, an earlier btf_type has | |
47 | * a smaller type_id. | |
48 | * | |
49 | * A btf_type object may refer to another btf_type object by using | |
50 | * type_id (i.e. the "type" in the "struct btf_type"). | |
51 | * | |
52 | * NOTE that we cannot assume any reference-order. | |
53 | * A btf_type object can refer to an earlier btf_type object | |
54 | * but it can also refer to a later btf_type object. | |
55 | * | |
56 | * For example, to describe "const void *". A btf_type | |
57 | * object describing "const" may refer to another btf_type | |
58 | * object describing "void *". This type-reference is done | |
59 | * by specifying type_id: | |
60 | * | |
61 | * [1] CONST (anon) type_id=2 | |
62 | * [2] PTR (anon) type_id=0 | |
63 | * | |
64 | * The above is the btf_verifier debug log: | |
65 | * - Each line started with "[?]" is a btf_type object | |
66 | * - [?] is the type_id of the btf_type object. | |
67 | * - CONST/PTR is the BTF_KIND_XXX | |
68 | * - "(anon)" is the name of the type. It just | |
69 | * happens that CONST and PTR has no name. | |
70 | * - type_id=XXX is the 'u32 type' in btf_type | |
71 | * | |
72 | * NOTE: "void" has type_id 0 | |
73 | * | |
74 | * String section: | |
75 | * ~~~~~~~~~~~~~~ | |
76 | * The BTF string section contains the names used by the type section. | |
77 | * Each string is referred by an "offset" from the beginning of the | |
78 | * string section. | |
79 | * | |
80 | * Each string is '\0' terminated. | |
81 | * | |
82 | * The first character in the string section must be '\0' | |
83 | * which is used to mean 'anonymous'. Some btf_type may not | |
84 | * have a name. | |
85 | */ | |
86 | ||
87 | /* BTF verification: | |
88 | * | |
89 | * To verify BTF data, two passes are needed. | |
90 | * | |
91 | * Pass #1 | |
92 | * ~~~~~~~ | |
93 | * The first pass is to collect all btf_type objects to | |
94 | * an array: "btf->types". | |
95 | * | |
96 | * Depending on the C type that a btf_type is describing, | |
97 | * a btf_type may be followed by extra data. We don't know | |
98 | * how many btf_type is there, and more importantly we don't | |
99 | * know where each btf_type is located in the type section. | |
100 | * | |
101 | * Without knowing the location of each type_id, most verifications | |
102 | * cannot be done. e.g. an earlier btf_type may refer to a later | |
103 | * btf_type (recall the "const void *" above), so we cannot | |
104 | * check this type-reference in the first pass. | |
105 | * | |
106 | * In the first pass, it still does some verifications (e.g. | |
107 | * checking the name is a valid offset to the string section). | |
eb3f595d MKL |
108 | * |
109 | * Pass #2 | |
110 | * ~~~~~~~ | |
111 | * The main focus is to resolve a btf_type that is referring | |
112 | * to another type. | |
113 | * | |
114 | * We have to ensure the referring type: | |
115 | * 1) does exist in the BTF (i.e. in btf->types[]) | |
116 | * 2) does not cause a loop: | |
117 | * struct A { | |
118 | * struct B b; | |
119 | * }; | |
120 | * | |
121 | * struct B { | |
122 | * struct A a; | |
123 | * }; | |
124 | * | |
125 | * btf_type_needs_resolve() decides if a btf_type needs | |
126 | * to be resolved. | |
127 | * | |
128 | * The needs_resolve type implements the "resolve()" ops which | |
129 | * essentially does a DFS and detects backedge. | |
130 | * | |
131 | * During resolve (or DFS), different C types have different | |
132 | * "RESOLVED" conditions. | |
133 | * | |
134 | * When resolving a BTF_KIND_STRUCT, we need to resolve all its | |
135 | * members because a member is always referring to another | |
136 | * type. A struct's member can be treated as "RESOLVED" if | |
137 | * it is referring to a BTF_KIND_PTR. Otherwise, the | |
138 | * following valid C struct would be rejected: | |
139 | * | |
140 | * struct A { | |
141 | * int m; | |
142 | * struct A *a; | |
143 | * }; | |
144 | * | |
145 | * When resolving a BTF_KIND_PTR, it needs to keep resolving if | |
146 | * it is referring to another BTF_KIND_PTR. Otherwise, we cannot | |
147 | * detect a pointer loop, e.g.: | |
148 | * BTF_KIND_CONST -> BTF_KIND_PTR -> BTF_KIND_CONST -> BTF_KIND_PTR + | |
149 | * ^ | | |
150 | * +-----------------------------------------+ | |
151 | * | |
69b693f0 MKL |
152 | */ |
153 | ||
154 | #define BITS_PER_U64 (sizeof(u64) * BITS_PER_BYTE) | |
155 | #define BITS_PER_BYTE_MASK (BITS_PER_BYTE - 1) | |
156 | #define BITS_PER_BYTE_MASKED(bits) ((bits) & BITS_PER_BYTE_MASK) | |
157 | #define BITS_ROUNDDOWN_BYTES(bits) ((bits) >> 3) | |
158 | #define BITS_ROUNDUP_BYTES(bits) \ | |
159 | (BITS_ROUNDDOWN_BYTES(bits) + !!BITS_PER_BYTE_MASKED(bits)) | |
160 | ||
161 | /* 16MB for 64k structs and each has 16 members and | |
162 | * a few MB spaces for the string section. | |
163 | * The hard limit is S32_MAX. | |
164 | */ | |
165 | #define BTF_MAX_SIZE (16 * 1024 * 1024) | |
166 | /* 64k. We can raise it later. The hard limit is S32_MAX. */ | |
167 | #define BTF_MAX_NR_TYPES 65535 | |
168 | ||
169 | #define for_each_member(i, struct_type, member) \ | |
170 | for (i = 0, member = btf_type_member(struct_type); \ | |
171 | i < btf_type_vlen(struct_type); \ | |
172 | i++, member++) | |
173 | ||
eb3f595d MKL |
174 | #define for_each_member_from(i, from, struct_type, member) \ |
175 | for (i = from, member = btf_type_member(struct_type) + from; \ | |
176 | i < btf_type_vlen(struct_type); \ | |
177 | i++, member++) | |
178 | ||
69b693f0 MKL |
179 | struct btf { |
180 | union { | |
181 | struct btf_header *hdr; | |
182 | void *data; | |
183 | }; | |
184 | struct btf_type **types; | |
eb3f595d MKL |
185 | u32 *resolved_ids; |
186 | u32 *resolved_sizes; | |
69b693f0 MKL |
187 | const char *strings; |
188 | void *nohdr_data; | |
189 | u32 nr_types; | |
190 | u32 types_size; | |
191 | u32 data_size; | |
192 | }; | |
193 | ||
eb3f595d MKL |
194 | enum verifier_phase { |
195 | CHECK_META, | |
196 | CHECK_TYPE, | |
197 | }; | |
198 | ||
199 | struct resolve_vertex { | |
200 | const struct btf_type *t; | |
201 | u32 type_id; | |
202 | u16 next_member; | |
203 | }; | |
204 | ||
205 | enum visit_state { | |
206 | NOT_VISITED, | |
207 | VISITED, | |
208 | RESOLVED, | |
209 | }; | |
210 | ||
211 | enum resolve_mode { | |
212 | RESOLVE_TBD, /* To Be Determined */ | |
213 | RESOLVE_PTR, /* Resolving for Pointer */ | |
214 | RESOLVE_STRUCT_OR_ARRAY, /* Resolving for struct/union | |
215 | * or array | |
216 | */ | |
217 | }; | |
218 | ||
219 | #define MAX_RESOLVE_DEPTH 32 | |
220 | ||
69b693f0 MKL |
221 | struct btf_verifier_env { |
222 | struct btf *btf; | |
eb3f595d MKL |
223 | u8 *visit_states; |
224 | struct resolve_vertex stack[MAX_RESOLVE_DEPTH]; | |
69b693f0 MKL |
225 | struct bpf_verifier_log log; |
226 | u32 log_type_id; | |
eb3f595d MKL |
227 | u32 top_stack; |
228 | enum verifier_phase phase; | |
229 | enum resolve_mode resolve_mode; | |
69b693f0 MKL |
230 | }; |
231 | ||
232 | static const char * const btf_kind_str[NR_BTF_KINDS] = { | |
233 | [BTF_KIND_UNKN] = "UNKNOWN", | |
234 | [BTF_KIND_INT] = "INT", | |
235 | [BTF_KIND_PTR] = "PTR", | |
236 | [BTF_KIND_ARRAY] = "ARRAY", | |
237 | [BTF_KIND_STRUCT] = "STRUCT", | |
238 | [BTF_KIND_UNION] = "UNION", | |
239 | [BTF_KIND_ENUM] = "ENUM", | |
240 | [BTF_KIND_FWD] = "FWD", | |
241 | [BTF_KIND_TYPEDEF] = "TYPEDEF", | |
242 | [BTF_KIND_VOLATILE] = "VOLATILE", | |
243 | [BTF_KIND_CONST] = "CONST", | |
244 | [BTF_KIND_RESTRICT] = "RESTRICT", | |
245 | }; | |
246 | ||
247 | struct btf_kind_operations { | |
248 | s32 (*check_meta)(struct btf_verifier_env *env, | |
249 | const struct btf_type *t, | |
250 | u32 meta_left); | |
eb3f595d MKL |
251 | int (*resolve)(struct btf_verifier_env *env, |
252 | const struct resolve_vertex *v); | |
179cde8c MKL |
253 | int (*check_member)(struct btf_verifier_env *env, |
254 | const struct btf_type *struct_type, | |
255 | const struct btf_member *member, | |
256 | const struct btf_type *member_type); | |
69b693f0 MKL |
257 | void (*log_details)(struct btf_verifier_env *env, |
258 | const struct btf_type *t); | |
259 | }; | |
260 | ||
261 | static const struct btf_kind_operations * const kind_ops[NR_BTF_KINDS]; | |
262 | static struct btf_type btf_void; | |
263 | ||
eb3f595d MKL |
264 | static bool btf_type_is_modifier(const struct btf_type *t) |
265 | { | |
266 | /* Some of them is not strictly a C modifier | |
267 | * but they are grouped into the same bucket | |
268 | * for BTF concern: | |
269 | * A type (t) that refers to another | |
270 | * type through t->type AND its size cannot | |
271 | * be determined without following the t->type. | |
272 | * | |
273 | * ptr does not fall into this bucket | |
274 | * because its size is always sizeof(void *). | |
275 | */ | |
276 | switch (BTF_INFO_KIND(t->info)) { | |
277 | case BTF_KIND_TYPEDEF: | |
278 | case BTF_KIND_VOLATILE: | |
279 | case BTF_KIND_CONST: | |
280 | case BTF_KIND_RESTRICT: | |
281 | return true; | |
282 | } | |
283 | ||
284 | return false; | |
285 | } | |
286 | ||
287 | static bool btf_type_is_void(const struct btf_type *t) | |
288 | { | |
289 | /* void => no type and size info. | |
290 | * Hence, FWD is also treated as void. | |
291 | */ | |
292 | return t == &btf_void || BTF_INFO_KIND(t->info) == BTF_KIND_FWD; | |
293 | } | |
294 | ||
295 | static bool btf_type_is_void_or_null(const struct btf_type *t) | |
296 | { | |
297 | return !t || btf_type_is_void(t); | |
298 | } | |
299 | ||
300 | /* union is only a special case of struct: | |
301 | * all its offsetof(member) == 0 | |
302 | */ | |
303 | static bool btf_type_is_struct(const struct btf_type *t) | |
304 | { | |
305 | u8 kind = BTF_INFO_KIND(t->info); | |
306 | ||
307 | return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION; | |
308 | } | |
309 | ||
310 | static bool btf_type_is_array(const struct btf_type *t) | |
311 | { | |
312 | return BTF_INFO_KIND(t->info) == BTF_KIND_ARRAY; | |
313 | } | |
314 | ||
315 | static bool btf_type_is_ptr(const struct btf_type *t) | |
316 | { | |
317 | return BTF_INFO_KIND(t->info) == BTF_KIND_PTR; | |
318 | } | |
319 | ||
320 | static bool btf_type_is_int(const struct btf_type *t) | |
321 | { | |
322 | return BTF_INFO_KIND(t->info) == BTF_KIND_INT; | |
323 | } | |
324 | ||
325 | /* What types need to be resolved? | |
326 | * | |
327 | * btf_type_is_modifier() is an obvious one. | |
328 | * | |
329 | * btf_type_is_struct() because its member refers to | |
330 | * another type (through member->type). | |
331 | ||
332 | * btf_type_is_array() because its element (array->type) | |
333 | * refers to another type. Array can be thought of a | |
334 | * special case of struct while array just has the same | |
335 | * member-type repeated by array->nelems of times. | |
336 | */ | |
337 | static bool btf_type_needs_resolve(const struct btf_type *t) | |
338 | { | |
339 | return btf_type_is_modifier(t) || | |
340 | btf_type_is_ptr(t) || | |
341 | btf_type_is_struct(t) || | |
342 | btf_type_is_array(t); | |
343 | } | |
344 | ||
345 | /* t->size can be used */ | |
346 | static bool btf_type_has_size(const struct btf_type *t) | |
347 | { | |
348 | switch (BTF_INFO_KIND(t->info)) { | |
349 | case BTF_KIND_INT: | |
350 | case BTF_KIND_STRUCT: | |
351 | case BTF_KIND_UNION: | |
352 | case BTF_KIND_ENUM: | |
353 | return true; | |
354 | } | |
355 | ||
356 | return false; | |
357 | } | |
358 | ||
69b693f0 MKL |
359 | static const char *btf_int_encoding_str(u8 encoding) |
360 | { | |
361 | if (encoding == 0) | |
362 | return "(none)"; | |
363 | else if (encoding == BTF_INT_SIGNED) | |
364 | return "SIGNED"; | |
365 | else if (encoding == BTF_INT_CHAR) | |
366 | return "CHAR"; | |
367 | else if (encoding == BTF_INT_BOOL) | |
368 | return "BOOL"; | |
369 | else if (encoding == BTF_INT_VARARGS) | |
370 | return "VARARGS"; | |
371 | else | |
372 | return "UNKN"; | |
373 | } | |
374 | ||
375 | static u16 btf_type_vlen(const struct btf_type *t) | |
376 | { | |
377 | return BTF_INFO_VLEN(t->info); | |
378 | } | |
379 | ||
380 | static u32 btf_type_int(const struct btf_type *t) | |
381 | { | |
382 | return *(u32 *)(t + 1); | |
383 | } | |
384 | ||
385 | static const struct btf_array *btf_type_array(const struct btf_type *t) | |
386 | { | |
387 | return (const struct btf_array *)(t + 1); | |
388 | } | |
389 | ||
390 | static const struct btf_member *btf_type_member(const struct btf_type *t) | |
391 | { | |
392 | return (const struct btf_member *)(t + 1); | |
393 | } | |
394 | ||
395 | static const struct btf_enum *btf_type_enum(const struct btf_type *t) | |
396 | { | |
397 | return (const struct btf_enum *)(t + 1); | |
398 | } | |
399 | ||
400 | static const struct btf_kind_operations *btf_type_ops(const struct btf_type *t) | |
401 | { | |
402 | return kind_ops[BTF_INFO_KIND(t->info)]; | |
403 | } | |
404 | ||
405 | static bool btf_name_offset_valid(const struct btf *btf, u32 offset) | |
406 | { | |
407 | return !BTF_STR_TBL_ELF_ID(offset) && | |
408 | BTF_STR_OFFSET(offset) < btf->hdr->str_len; | |
409 | } | |
410 | ||
411 | static const char *btf_name_by_offset(const struct btf *btf, u32 offset) | |
412 | { | |
413 | if (!BTF_STR_OFFSET(offset)) | |
414 | return "(anon)"; | |
415 | else if (BTF_STR_OFFSET(offset) < btf->hdr->str_len) | |
416 | return &btf->strings[BTF_STR_OFFSET(offset)]; | |
417 | else | |
418 | return "(invalid-name-offset)"; | |
419 | } | |
420 | ||
eb3f595d MKL |
421 | static const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id) |
422 | { | |
423 | if (type_id > btf->nr_types) | |
424 | return NULL; | |
425 | ||
426 | return btf->types[type_id]; | |
427 | } | |
428 | ||
69b693f0 MKL |
429 | __printf(2, 3) static void __btf_verifier_log(struct bpf_verifier_log *log, |
430 | const char *fmt, ...) | |
431 | { | |
432 | va_list args; | |
433 | ||
434 | va_start(args, fmt); | |
435 | bpf_verifier_vlog(log, fmt, args); | |
436 | va_end(args); | |
437 | } | |
438 | ||
439 | __printf(2, 3) static void btf_verifier_log(struct btf_verifier_env *env, | |
440 | const char *fmt, ...) | |
441 | { | |
442 | struct bpf_verifier_log *log = &env->log; | |
443 | va_list args; | |
444 | ||
445 | if (!bpf_verifier_log_needed(log)) | |
446 | return; | |
447 | ||
448 | va_start(args, fmt); | |
449 | bpf_verifier_vlog(log, fmt, args); | |
450 | va_end(args); | |
451 | } | |
452 | ||
453 | __printf(4, 5) static void __btf_verifier_log_type(struct btf_verifier_env *env, | |
454 | const struct btf_type *t, | |
455 | bool log_details, | |
456 | const char *fmt, ...) | |
457 | { | |
458 | struct bpf_verifier_log *log = &env->log; | |
459 | u8 kind = BTF_INFO_KIND(t->info); | |
460 | struct btf *btf = env->btf; | |
461 | va_list args; | |
462 | ||
463 | if (!bpf_verifier_log_needed(log)) | |
464 | return; | |
465 | ||
466 | __btf_verifier_log(log, "[%u] %s %s%s", | |
467 | env->log_type_id, | |
468 | btf_kind_str[kind], | |
469 | btf_name_by_offset(btf, t->name), | |
470 | log_details ? " " : ""); | |
471 | ||
472 | if (log_details) | |
473 | btf_type_ops(t)->log_details(env, t); | |
474 | ||
475 | if (fmt && *fmt) { | |
476 | __btf_verifier_log(log, " "); | |
477 | va_start(args, fmt); | |
478 | bpf_verifier_vlog(log, fmt, args); | |
479 | va_end(args); | |
480 | } | |
481 | ||
482 | __btf_verifier_log(log, "\n"); | |
483 | } | |
484 | ||
485 | #define btf_verifier_log_type(env, t, ...) \ | |
486 | __btf_verifier_log_type((env), (t), true, __VA_ARGS__) | |
487 | #define btf_verifier_log_basic(env, t, ...) \ | |
488 | __btf_verifier_log_type((env), (t), false, __VA_ARGS__) | |
489 | ||
490 | __printf(4, 5) | |
491 | static void btf_verifier_log_member(struct btf_verifier_env *env, | |
492 | const struct btf_type *struct_type, | |
493 | const struct btf_member *member, | |
494 | const char *fmt, ...) | |
495 | { | |
496 | struct bpf_verifier_log *log = &env->log; | |
497 | struct btf *btf = env->btf; | |
498 | va_list args; | |
499 | ||
500 | if (!bpf_verifier_log_needed(log)) | |
501 | return; | |
502 | ||
eb3f595d MKL |
503 | /* The CHECK_META phase already did a btf dump. |
504 | * | |
505 | * If member is logged again, it must hit an error in | |
506 | * parsing this member. It is useful to print out which | |
507 | * struct this member belongs to. | |
508 | */ | |
509 | if (env->phase != CHECK_META) | |
510 | btf_verifier_log_type(env, struct_type, NULL); | |
511 | ||
69b693f0 MKL |
512 | __btf_verifier_log(log, "\t%s type_id=%u bits_offset=%u", |
513 | btf_name_by_offset(btf, member->name), | |
514 | member->type, member->offset); | |
515 | ||
516 | if (fmt && *fmt) { | |
517 | __btf_verifier_log(log, " "); | |
518 | va_start(args, fmt); | |
519 | bpf_verifier_vlog(log, fmt, args); | |
520 | va_end(args); | |
521 | } | |
522 | ||
523 | __btf_verifier_log(log, "\n"); | |
524 | } | |
525 | ||
526 | static void btf_verifier_log_hdr(struct btf_verifier_env *env) | |
527 | { | |
528 | struct bpf_verifier_log *log = &env->log; | |
529 | const struct btf *btf = env->btf; | |
530 | const struct btf_header *hdr; | |
531 | ||
532 | if (!bpf_verifier_log_needed(log)) | |
533 | return; | |
534 | ||
535 | hdr = btf->hdr; | |
536 | __btf_verifier_log(log, "magic: 0x%x\n", hdr->magic); | |
537 | __btf_verifier_log(log, "version: %u\n", hdr->version); | |
538 | __btf_verifier_log(log, "flags: 0x%x\n", hdr->flags); | |
539 | __btf_verifier_log(log, "parent_label: %u\n", hdr->parent_label); | |
540 | __btf_verifier_log(log, "parent_name: %u\n", hdr->parent_name); | |
541 | __btf_verifier_log(log, "label_off: %u\n", hdr->label_off); | |
542 | __btf_verifier_log(log, "object_off: %u\n", hdr->object_off); | |
543 | __btf_verifier_log(log, "func_off: %u\n", hdr->func_off); | |
544 | __btf_verifier_log(log, "type_off: %u\n", hdr->type_off); | |
545 | __btf_verifier_log(log, "str_off: %u\n", hdr->str_off); | |
546 | __btf_verifier_log(log, "str_len: %u\n", hdr->str_len); | |
547 | __btf_verifier_log(log, "btf_total_size: %u\n", btf->data_size); | |
548 | } | |
549 | ||
550 | static int btf_add_type(struct btf_verifier_env *env, struct btf_type *t) | |
551 | { | |
552 | struct btf *btf = env->btf; | |
553 | ||
554 | /* < 2 because +1 for btf_void which is always in btf->types[0]. | |
555 | * btf_void is not accounted in btf->nr_types because btf_void | |
556 | * does not come from the BTF file. | |
557 | */ | |
558 | if (btf->types_size - btf->nr_types < 2) { | |
559 | /* Expand 'types' array */ | |
560 | ||
561 | struct btf_type **new_types; | |
562 | u32 expand_by, new_size; | |
563 | ||
564 | if (btf->types_size == BTF_MAX_NR_TYPES) { | |
565 | btf_verifier_log(env, "Exceeded max num of types"); | |
566 | return -E2BIG; | |
567 | } | |
568 | ||
569 | expand_by = max_t(u32, btf->types_size >> 2, 16); | |
570 | new_size = min_t(u32, BTF_MAX_NR_TYPES, | |
571 | btf->types_size + expand_by); | |
572 | ||
573 | new_types = kvzalloc(new_size * sizeof(*new_types), | |
574 | GFP_KERNEL | __GFP_NOWARN); | |
575 | if (!new_types) | |
576 | return -ENOMEM; | |
577 | ||
578 | if (btf->nr_types == 0) | |
579 | new_types[0] = &btf_void; | |
580 | else | |
581 | memcpy(new_types, btf->types, | |
582 | sizeof(*btf->types) * (btf->nr_types + 1)); | |
583 | ||
584 | kvfree(btf->types); | |
585 | btf->types = new_types; | |
586 | btf->types_size = new_size; | |
587 | } | |
588 | ||
589 | btf->types[++(btf->nr_types)] = t; | |
590 | ||
591 | return 0; | |
592 | } | |
593 | ||
594 | static void btf_free(struct btf *btf) | |
595 | { | |
596 | kvfree(btf->types); | |
eb3f595d MKL |
597 | kvfree(btf->resolved_sizes); |
598 | kvfree(btf->resolved_ids); | |
69b693f0 MKL |
599 | kvfree(btf->data); |
600 | kfree(btf); | |
601 | } | |
602 | ||
eb3f595d MKL |
603 | static int env_resolve_init(struct btf_verifier_env *env) |
604 | { | |
605 | struct btf *btf = env->btf; | |
606 | u32 nr_types = btf->nr_types; | |
607 | u32 *resolved_sizes = NULL; | |
608 | u32 *resolved_ids = NULL; | |
609 | u8 *visit_states = NULL; | |
610 | ||
611 | /* +1 for btf_void */ | |
612 | resolved_sizes = kvzalloc((nr_types + 1) * sizeof(*resolved_sizes), | |
613 | GFP_KERNEL | __GFP_NOWARN); | |
614 | if (!resolved_sizes) | |
615 | goto nomem; | |
616 | ||
617 | resolved_ids = kvzalloc((nr_types + 1) * sizeof(*resolved_ids), | |
618 | GFP_KERNEL | __GFP_NOWARN); | |
619 | if (!resolved_ids) | |
620 | goto nomem; | |
621 | ||
622 | visit_states = kvzalloc((nr_types + 1) * sizeof(*visit_states), | |
623 | GFP_KERNEL | __GFP_NOWARN); | |
624 | if (!visit_states) | |
625 | goto nomem; | |
626 | ||
627 | btf->resolved_sizes = resolved_sizes; | |
628 | btf->resolved_ids = resolved_ids; | |
629 | env->visit_states = visit_states; | |
630 | ||
631 | return 0; | |
632 | ||
633 | nomem: | |
634 | kvfree(resolved_sizes); | |
635 | kvfree(resolved_ids); | |
636 | kvfree(visit_states); | |
637 | return -ENOMEM; | |
638 | } | |
639 | ||
69b693f0 MKL |
640 | static void btf_verifier_env_free(struct btf_verifier_env *env) |
641 | { | |
eb3f595d | 642 | kvfree(env->visit_states); |
69b693f0 MKL |
643 | kfree(env); |
644 | } | |
645 | ||
eb3f595d MKL |
646 | static bool env_type_is_resolve_sink(const struct btf_verifier_env *env, |
647 | const struct btf_type *next_type) | |
648 | { | |
649 | switch (env->resolve_mode) { | |
650 | case RESOLVE_TBD: | |
651 | /* int, enum or void is a sink */ | |
652 | return !btf_type_needs_resolve(next_type); | |
653 | case RESOLVE_PTR: | |
654 | /* int, enum, void, struct or array is a sink for ptr */ | |
655 | return !btf_type_is_modifier(next_type) && | |
656 | !btf_type_is_ptr(next_type); | |
657 | case RESOLVE_STRUCT_OR_ARRAY: | |
658 | /* int, enum, void or ptr is a sink for struct and array */ | |
659 | return !btf_type_is_modifier(next_type) && | |
660 | !btf_type_is_array(next_type) && | |
661 | !btf_type_is_struct(next_type); | |
662 | default: | |
663 | BUG_ON(1); | |
664 | } | |
665 | } | |
666 | ||
667 | static bool env_type_is_resolved(const struct btf_verifier_env *env, | |
668 | u32 type_id) | |
669 | { | |
670 | return env->visit_states[type_id] == RESOLVED; | |
671 | } | |
672 | ||
673 | static int env_stack_push(struct btf_verifier_env *env, | |
674 | const struct btf_type *t, u32 type_id) | |
675 | { | |
676 | struct resolve_vertex *v; | |
677 | ||
678 | if (env->top_stack == MAX_RESOLVE_DEPTH) | |
679 | return -E2BIG; | |
680 | ||
681 | if (env->visit_states[type_id] != NOT_VISITED) | |
682 | return -EEXIST; | |
683 | ||
684 | env->visit_states[type_id] = VISITED; | |
685 | ||
686 | v = &env->stack[env->top_stack++]; | |
687 | v->t = t; | |
688 | v->type_id = type_id; | |
689 | v->next_member = 0; | |
690 | ||
691 | if (env->resolve_mode == RESOLVE_TBD) { | |
692 | if (btf_type_is_ptr(t)) | |
693 | env->resolve_mode = RESOLVE_PTR; | |
694 | else if (btf_type_is_struct(t) || btf_type_is_array(t)) | |
695 | env->resolve_mode = RESOLVE_STRUCT_OR_ARRAY; | |
696 | } | |
697 | ||
698 | return 0; | |
699 | } | |
700 | ||
701 | static void env_stack_set_next_member(struct btf_verifier_env *env, | |
702 | u16 next_member) | |
703 | { | |
704 | env->stack[env->top_stack - 1].next_member = next_member; | |
705 | } | |
706 | ||
707 | static void env_stack_pop_resolved(struct btf_verifier_env *env, | |
708 | u32 resolved_type_id, | |
709 | u32 resolved_size) | |
710 | { | |
711 | u32 type_id = env->stack[--(env->top_stack)].type_id; | |
712 | struct btf *btf = env->btf; | |
713 | ||
714 | btf->resolved_sizes[type_id] = resolved_size; | |
715 | btf->resolved_ids[type_id] = resolved_type_id; | |
716 | env->visit_states[type_id] = RESOLVED; | |
717 | } | |
718 | ||
719 | static const struct resolve_vertex *env_stack_peak(struct btf_verifier_env *env) | |
720 | { | |
721 | return env->top_stack ? &env->stack[env->top_stack - 1] : NULL; | |
722 | } | |
723 | ||
724 | /* The input param "type_id" must point to a needs_resolve type */ | |
725 | static const struct btf_type *btf_type_id_resolve(const struct btf *btf, | |
726 | u32 *type_id) | |
727 | { | |
728 | *type_id = btf->resolved_ids[*type_id]; | |
729 | return btf_type_by_id(btf, *type_id); | |
730 | } | |
731 | ||
732 | const struct btf_type *btf_type_id_size(const struct btf *btf, | |
733 | u32 *type_id, u32 *ret_size) | |
734 | { | |
735 | const struct btf_type *size_type; | |
736 | u32 size_type_id = *type_id; | |
737 | u32 size = 0; | |
738 | ||
739 | size_type = btf_type_by_id(btf, size_type_id); | |
740 | if (btf_type_is_void_or_null(size_type)) | |
741 | return NULL; | |
742 | ||
743 | if (btf_type_has_size(size_type)) { | |
744 | size = size_type->size; | |
745 | } else if (btf_type_is_array(size_type)) { | |
746 | size = btf->resolved_sizes[size_type_id]; | |
747 | } else if (btf_type_is_ptr(size_type)) { | |
748 | size = sizeof(void *); | |
749 | } else { | |
750 | if (WARN_ON_ONCE(!btf_type_is_modifier(size_type))) | |
751 | return NULL; | |
752 | ||
753 | size = btf->resolved_sizes[size_type_id]; | |
754 | size_type_id = btf->resolved_ids[size_type_id]; | |
755 | size_type = btf_type_by_id(btf, size_type_id); | |
756 | if (btf_type_is_void(size_type)) | |
757 | return NULL; | |
758 | } | |
759 | ||
760 | *type_id = size_type_id; | |
761 | if (ret_size) | |
762 | *ret_size = size; | |
763 | ||
764 | return size_type; | |
765 | } | |
766 | ||
179cde8c MKL |
767 | static int btf_df_check_member(struct btf_verifier_env *env, |
768 | const struct btf_type *struct_type, | |
769 | const struct btf_member *member, | |
770 | const struct btf_type *member_type) | |
771 | { | |
772 | btf_verifier_log_basic(env, struct_type, | |
773 | "Unsupported check_member"); | |
774 | return -EINVAL; | |
775 | } | |
776 | ||
eb3f595d MKL |
777 | static int btf_df_resolve(struct btf_verifier_env *env, |
778 | const struct resolve_vertex *v) | |
779 | { | |
780 | btf_verifier_log_basic(env, v->t, "Unsupported resolve"); | |
781 | return -EINVAL; | |
782 | } | |
783 | ||
179cde8c MKL |
784 | static int btf_int_check_member(struct btf_verifier_env *env, |
785 | const struct btf_type *struct_type, | |
786 | const struct btf_member *member, | |
787 | const struct btf_type *member_type) | |
788 | { | |
789 | u32 int_data = btf_type_int(member_type); | |
790 | u32 struct_bits_off = member->offset; | |
791 | u32 struct_size = struct_type->size; | |
792 | u32 nr_copy_bits; | |
793 | u32 bytes_offset; | |
794 | ||
795 | if (U32_MAX - struct_bits_off < BTF_INT_OFFSET(int_data)) { | |
796 | btf_verifier_log_member(env, struct_type, member, | |
797 | "bits_offset exceeds U32_MAX"); | |
798 | return -EINVAL; | |
799 | } | |
800 | ||
801 | struct_bits_off += BTF_INT_OFFSET(int_data); | |
802 | bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off); | |
803 | nr_copy_bits = BTF_INT_BITS(int_data) + | |
804 | BITS_PER_BYTE_MASKED(struct_bits_off); | |
805 | ||
806 | if (nr_copy_bits > BITS_PER_U64) { | |
807 | btf_verifier_log_member(env, struct_type, member, | |
808 | "nr_copy_bits exceeds 64"); | |
809 | return -EINVAL; | |
810 | } | |
811 | ||
812 | if (struct_size < bytes_offset || | |
813 | struct_size - bytes_offset < BITS_ROUNDUP_BYTES(nr_copy_bits)) { | |
814 | btf_verifier_log_member(env, struct_type, member, | |
815 | "Member exceeds struct_size"); | |
816 | return -EINVAL; | |
817 | } | |
818 | ||
819 | return 0; | |
820 | } | |
821 | ||
69b693f0 MKL |
822 | static s32 btf_int_check_meta(struct btf_verifier_env *env, |
823 | const struct btf_type *t, | |
824 | u32 meta_left) | |
825 | { | |
826 | u32 int_data, nr_bits, meta_needed = sizeof(int_data); | |
827 | u16 encoding; | |
828 | ||
829 | if (meta_left < meta_needed) { | |
830 | btf_verifier_log_basic(env, t, | |
831 | "meta_left:%u meta_needed:%u", | |
832 | meta_left, meta_needed); | |
833 | return -EINVAL; | |
834 | } | |
835 | ||
836 | if (btf_type_vlen(t)) { | |
837 | btf_verifier_log_type(env, t, "vlen != 0"); | |
838 | return -EINVAL; | |
839 | } | |
840 | ||
841 | int_data = btf_type_int(t); | |
842 | nr_bits = BTF_INT_BITS(int_data) + BTF_INT_OFFSET(int_data); | |
843 | ||
844 | if (nr_bits > BITS_PER_U64) { | |
845 | btf_verifier_log_type(env, t, "nr_bits exceeds %zu", | |
846 | BITS_PER_U64); | |
847 | return -EINVAL; | |
848 | } | |
849 | ||
850 | if (BITS_ROUNDUP_BYTES(nr_bits) > t->size) { | |
851 | btf_verifier_log_type(env, t, "nr_bits exceeds type_size"); | |
852 | return -EINVAL; | |
853 | } | |
854 | ||
855 | encoding = BTF_INT_ENCODING(int_data); | |
856 | if (encoding && | |
857 | encoding != BTF_INT_SIGNED && | |
858 | encoding != BTF_INT_CHAR && | |
859 | encoding != BTF_INT_BOOL && | |
860 | encoding != BTF_INT_VARARGS) { | |
861 | btf_verifier_log_type(env, t, "Unsupported encoding"); | |
862 | return -ENOTSUPP; | |
863 | } | |
864 | ||
865 | btf_verifier_log_type(env, t, NULL); | |
866 | ||
867 | return meta_needed; | |
868 | } | |
869 | ||
870 | static void btf_int_log(struct btf_verifier_env *env, | |
871 | const struct btf_type *t) | |
872 | { | |
873 | int int_data = btf_type_int(t); | |
874 | ||
875 | btf_verifier_log(env, | |
876 | "size=%u bits_offset=%u nr_bits=%u encoding=%s", | |
877 | t->size, BTF_INT_OFFSET(int_data), | |
878 | BTF_INT_BITS(int_data), | |
879 | btf_int_encoding_str(BTF_INT_ENCODING(int_data))); | |
880 | } | |
881 | ||
882 | static const struct btf_kind_operations int_ops = { | |
883 | .check_meta = btf_int_check_meta, | |
eb3f595d | 884 | .resolve = btf_df_resolve, |
179cde8c | 885 | .check_member = btf_int_check_member, |
69b693f0 MKL |
886 | .log_details = btf_int_log, |
887 | }; | |
888 | ||
179cde8c MKL |
889 | static int btf_modifier_check_member(struct btf_verifier_env *env, |
890 | const struct btf_type *struct_type, | |
891 | const struct btf_member *member, | |
892 | const struct btf_type *member_type) | |
893 | { | |
894 | const struct btf_type *resolved_type; | |
895 | u32 resolved_type_id = member->type; | |
896 | struct btf_member resolved_member; | |
897 | struct btf *btf = env->btf; | |
898 | ||
899 | resolved_type = btf_type_id_size(btf, &resolved_type_id, NULL); | |
900 | if (!resolved_type) { | |
901 | btf_verifier_log_member(env, struct_type, member, | |
902 | "Invalid member"); | |
903 | return -EINVAL; | |
904 | } | |
905 | ||
906 | resolved_member = *member; | |
907 | resolved_member.type = resolved_type_id; | |
908 | ||
909 | return btf_type_ops(resolved_type)->check_member(env, struct_type, | |
910 | &resolved_member, | |
911 | resolved_type); | |
912 | } | |
913 | ||
914 | static int btf_ptr_check_member(struct btf_verifier_env *env, | |
915 | const struct btf_type *struct_type, | |
916 | const struct btf_member *member, | |
917 | const struct btf_type *member_type) | |
918 | { | |
919 | u32 struct_size, struct_bits_off, bytes_offset; | |
920 | ||
921 | struct_size = struct_type->size; | |
922 | struct_bits_off = member->offset; | |
923 | bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off); | |
924 | ||
925 | if (BITS_PER_BYTE_MASKED(struct_bits_off)) { | |
926 | btf_verifier_log_member(env, struct_type, member, | |
927 | "Member is not byte aligned"); | |
928 | return -EINVAL; | |
929 | } | |
930 | ||
931 | if (struct_size - bytes_offset < sizeof(void *)) { | |
932 | btf_verifier_log_member(env, struct_type, member, | |
933 | "Member exceeds struct_size"); | |
934 | return -EINVAL; | |
935 | } | |
936 | ||
937 | return 0; | |
938 | } | |
939 | ||
69b693f0 MKL |
940 | static int btf_ref_type_check_meta(struct btf_verifier_env *env, |
941 | const struct btf_type *t, | |
942 | u32 meta_left) | |
943 | { | |
944 | if (btf_type_vlen(t)) { | |
945 | btf_verifier_log_type(env, t, "vlen != 0"); | |
946 | return -EINVAL; | |
947 | } | |
948 | ||
949 | if (BTF_TYPE_PARENT(t->type)) { | |
950 | btf_verifier_log_type(env, t, "Invalid type_id"); | |
951 | return -EINVAL; | |
952 | } | |
953 | ||
954 | btf_verifier_log_type(env, t, NULL); | |
955 | ||
956 | return 0; | |
957 | } | |
958 | ||
eb3f595d MKL |
959 | static int btf_modifier_resolve(struct btf_verifier_env *env, |
960 | const struct resolve_vertex *v) | |
961 | { | |
962 | const struct btf_type *t = v->t; | |
963 | const struct btf_type *next_type; | |
964 | u32 next_type_id = t->type; | |
965 | struct btf *btf = env->btf; | |
966 | u32 next_type_size = 0; | |
967 | ||
968 | next_type = btf_type_by_id(btf, next_type_id); | |
969 | if (!next_type) { | |
970 | btf_verifier_log_type(env, v->t, "Invalid type_id"); | |
971 | return -EINVAL; | |
972 | } | |
973 | ||
974 | /* "typedef void new_void", "const void"...etc */ | |
975 | if (btf_type_is_void(next_type)) | |
976 | goto resolved; | |
977 | ||
978 | if (!env_type_is_resolve_sink(env, next_type) && | |
979 | !env_type_is_resolved(env, next_type_id)) | |
980 | return env_stack_push(env, next_type, next_type_id); | |
981 | ||
982 | /* Figure out the resolved next_type_id with size. | |
983 | * They will be stored in the current modifier's | |
984 | * resolved_ids and resolved_sizes such that it can | |
985 | * save us a few type-following when we use it later (e.g. in | |
986 | * pretty print). | |
987 | */ | |
988 | if (!btf_type_id_size(btf, &next_type_id, &next_type_size) && | |
989 | !btf_type_is_void(btf_type_id_resolve(btf, &next_type_id))) { | |
990 | btf_verifier_log_type(env, v->t, "Invalid type_id"); | |
991 | return -EINVAL; | |
992 | } | |
993 | ||
994 | resolved: | |
995 | env_stack_pop_resolved(env, next_type_id, next_type_size); | |
996 | ||
997 | return 0; | |
998 | } | |
999 | ||
1000 | static int btf_ptr_resolve(struct btf_verifier_env *env, | |
1001 | const struct resolve_vertex *v) | |
1002 | { | |
1003 | const struct btf_type *next_type; | |
1004 | const struct btf_type *t = v->t; | |
1005 | u32 next_type_id = t->type; | |
1006 | struct btf *btf = env->btf; | |
1007 | u32 next_type_size = 0; | |
1008 | ||
1009 | next_type = btf_type_by_id(btf, next_type_id); | |
1010 | if (!next_type) { | |
1011 | btf_verifier_log_type(env, v->t, "Invalid type_id"); | |
1012 | return -EINVAL; | |
1013 | } | |
1014 | ||
1015 | /* "void *" */ | |
1016 | if (btf_type_is_void(next_type)) | |
1017 | goto resolved; | |
1018 | ||
1019 | if (!env_type_is_resolve_sink(env, next_type) && | |
1020 | !env_type_is_resolved(env, next_type_id)) | |
1021 | return env_stack_push(env, next_type, next_type_id); | |
1022 | ||
1023 | /* If the modifier was RESOLVED during RESOLVE_STRUCT_OR_ARRAY, | |
1024 | * the modifier may have stopped resolving when it was resolved | |
1025 | * to a ptr (last-resolved-ptr). | |
1026 | * | |
1027 | * We now need to continue from the last-resolved-ptr to | |
1028 | * ensure the last-resolved-ptr will not referring back to | |
1029 | * the currenct ptr (t). | |
1030 | */ | |
1031 | if (btf_type_is_modifier(next_type)) { | |
1032 | const struct btf_type *resolved_type; | |
1033 | u32 resolved_type_id; | |
1034 | ||
1035 | resolved_type_id = next_type_id; | |
1036 | resolved_type = btf_type_id_resolve(btf, &resolved_type_id); | |
1037 | ||
1038 | if (btf_type_is_ptr(resolved_type) && | |
1039 | !env_type_is_resolve_sink(env, resolved_type) && | |
1040 | !env_type_is_resolved(env, resolved_type_id)) | |
1041 | return env_stack_push(env, resolved_type, | |
1042 | resolved_type_id); | |
1043 | } | |
1044 | ||
1045 | if (!btf_type_id_size(btf, &next_type_id, &next_type_size) && | |
1046 | !btf_type_is_void(btf_type_id_resolve(btf, &next_type_id))) { | |
1047 | btf_verifier_log_type(env, v->t, "Invalid type_id"); | |
1048 | return -EINVAL; | |
1049 | } | |
1050 | ||
1051 | resolved: | |
1052 | env_stack_pop_resolved(env, next_type_id, 0); | |
1053 | ||
1054 | return 0; | |
1055 | } | |
1056 | ||
69b693f0 MKL |
1057 | static void btf_ref_type_log(struct btf_verifier_env *env, |
1058 | const struct btf_type *t) | |
1059 | { | |
1060 | btf_verifier_log(env, "type_id=%u", t->type); | |
1061 | } | |
1062 | ||
1063 | static struct btf_kind_operations modifier_ops = { | |
1064 | .check_meta = btf_ref_type_check_meta, | |
eb3f595d | 1065 | .resolve = btf_modifier_resolve, |
179cde8c | 1066 | .check_member = btf_modifier_check_member, |
69b693f0 MKL |
1067 | .log_details = btf_ref_type_log, |
1068 | }; | |
1069 | ||
1070 | static struct btf_kind_operations ptr_ops = { | |
1071 | .check_meta = btf_ref_type_check_meta, | |
eb3f595d | 1072 | .resolve = btf_ptr_resolve, |
179cde8c | 1073 | .check_member = btf_ptr_check_member, |
69b693f0 MKL |
1074 | .log_details = btf_ref_type_log, |
1075 | }; | |
1076 | ||
1077 | static struct btf_kind_operations fwd_ops = { | |
1078 | .check_meta = btf_ref_type_check_meta, | |
eb3f595d | 1079 | .resolve = btf_df_resolve, |
179cde8c | 1080 | .check_member = btf_df_check_member, |
69b693f0 MKL |
1081 | .log_details = btf_ref_type_log, |
1082 | }; | |
1083 | ||
179cde8c MKL |
1084 | static int btf_array_check_member(struct btf_verifier_env *env, |
1085 | const struct btf_type *struct_type, | |
1086 | const struct btf_member *member, | |
1087 | const struct btf_type *member_type) | |
1088 | { | |
1089 | u32 struct_bits_off = member->offset; | |
1090 | u32 struct_size, bytes_offset; | |
1091 | u32 array_type_id, array_size; | |
1092 | struct btf *btf = env->btf; | |
1093 | ||
1094 | if (BITS_PER_BYTE_MASKED(struct_bits_off)) { | |
1095 | btf_verifier_log_member(env, struct_type, member, | |
1096 | "Member is not byte aligned"); | |
1097 | return -EINVAL; | |
1098 | } | |
1099 | ||
1100 | array_type_id = member->type; | |
1101 | btf_type_id_size(btf, &array_type_id, &array_size); | |
1102 | struct_size = struct_type->size; | |
1103 | bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off); | |
1104 | if (struct_size - bytes_offset < array_size) { | |
1105 | btf_verifier_log_member(env, struct_type, member, | |
1106 | "Member exceeds struct_size"); | |
1107 | return -EINVAL; | |
1108 | } | |
1109 | ||
1110 | return 0; | |
1111 | } | |
1112 | ||
69b693f0 MKL |
1113 | static s32 btf_array_check_meta(struct btf_verifier_env *env, |
1114 | const struct btf_type *t, | |
1115 | u32 meta_left) | |
1116 | { | |
1117 | const struct btf_array *array = btf_type_array(t); | |
1118 | u32 meta_needed = sizeof(*array); | |
1119 | ||
1120 | if (meta_left < meta_needed) { | |
1121 | btf_verifier_log_basic(env, t, | |
1122 | "meta_left:%u meta_needed:%u", | |
1123 | meta_left, meta_needed); | |
1124 | return -EINVAL; | |
1125 | } | |
1126 | ||
1127 | if (btf_type_vlen(t)) { | |
1128 | btf_verifier_log_type(env, t, "vlen != 0"); | |
1129 | return -EINVAL; | |
1130 | } | |
1131 | ||
1132 | /* We are a little forgiving on array->index_type since | |
1133 | * the kernel is not using it. | |
1134 | */ | |
1135 | /* Array elem cannot be in type void, | |
1136 | * so !array->type is not allowed. | |
1137 | */ | |
1138 | if (!array->type || BTF_TYPE_PARENT(array->type)) { | |
1139 | btf_verifier_log_type(env, t, "Invalid type_id"); | |
1140 | return -EINVAL; | |
1141 | } | |
1142 | ||
1143 | btf_verifier_log_type(env, t, NULL); | |
1144 | ||
1145 | return meta_needed; | |
1146 | } | |
1147 | ||
eb3f595d MKL |
1148 | static int btf_array_resolve(struct btf_verifier_env *env, |
1149 | const struct resolve_vertex *v) | |
1150 | { | |
1151 | const struct btf_array *array = btf_type_array(v->t); | |
1152 | const struct btf_type *elem_type; | |
1153 | u32 elem_type_id = array->type; | |
1154 | struct btf *btf = env->btf; | |
1155 | u32 elem_size; | |
1156 | ||
1157 | elem_type = btf_type_by_id(btf, elem_type_id); | |
1158 | if (btf_type_is_void_or_null(elem_type)) { | |
1159 | btf_verifier_log_type(env, v->t, | |
1160 | "Invalid elem"); | |
1161 | return -EINVAL; | |
1162 | } | |
1163 | ||
1164 | if (!env_type_is_resolve_sink(env, elem_type) && | |
1165 | !env_type_is_resolved(env, elem_type_id)) | |
1166 | return env_stack_push(env, elem_type, elem_type_id); | |
1167 | ||
1168 | elem_type = btf_type_id_size(btf, &elem_type_id, &elem_size); | |
1169 | if (!elem_type) { | |
1170 | btf_verifier_log_type(env, v->t, "Invalid elem"); | |
1171 | return -EINVAL; | |
1172 | } | |
1173 | ||
1174 | if (btf_type_is_int(elem_type)) { | |
1175 | int int_type_data = btf_type_int(elem_type); | |
1176 | u16 nr_bits = BTF_INT_BITS(int_type_data); | |
1177 | u16 nr_bytes = BITS_ROUNDUP_BYTES(nr_bits); | |
1178 | ||
1179 | /* Put more restriction on array of int. The int cannot | |
1180 | * be a bit field and it must be either u8/u16/u32/u64. | |
1181 | */ | |
1182 | if (BITS_PER_BYTE_MASKED(nr_bits) || | |
1183 | BTF_INT_OFFSET(int_type_data) || | |
1184 | (nr_bytes != sizeof(u8) && nr_bytes != sizeof(u16) && | |
1185 | nr_bytes != sizeof(u32) && nr_bytes != sizeof(u64))) { | |
1186 | btf_verifier_log_type(env, v->t, | |
1187 | "Invalid array of int"); | |
1188 | return -EINVAL; | |
1189 | } | |
1190 | } | |
1191 | ||
1192 | if (array->nelems && elem_size > U32_MAX / array->nelems) { | |
1193 | btf_verifier_log_type(env, v->t, | |
1194 | "Array size overflows U32_MAX"); | |
1195 | return -EINVAL; | |
1196 | } | |
1197 | ||
1198 | env_stack_pop_resolved(env, elem_type_id, elem_size * array->nelems); | |
1199 | ||
1200 | return 0; | |
1201 | } | |
1202 | ||
69b693f0 MKL |
1203 | static void btf_array_log(struct btf_verifier_env *env, |
1204 | const struct btf_type *t) | |
1205 | { | |
1206 | const struct btf_array *array = btf_type_array(t); | |
1207 | ||
1208 | btf_verifier_log(env, "type_id=%u index_type_id=%u nr_elems=%u", | |
1209 | array->type, array->index_type, array->nelems); | |
1210 | } | |
1211 | ||
1212 | static struct btf_kind_operations array_ops = { | |
1213 | .check_meta = btf_array_check_meta, | |
eb3f595d | 1214 | .resolve = btf_array_resolve, |
179cde8c | 1215 | .check_member = btf_array_check_member, |
69b693f0 MKL |
1216 | .log_details = btf_array_log, |
1217 | }; | |
1218 | ||
179cde8c MKL |
1219 | static int btf_struct_check_member(struct btf_verifier_env *env, |
1220 | const struct btf_type *struct_type, | |
1221 | const struct btf_member *member, | |
1222 | const struct btf_type *member_type) | |
1223 | { | |
1224 | u32 struct_bits_off = member->offset; | |
1225 | u32 struct_size, bytes_offset; | |
1226 | ||
1227 | if (BITS_PER_BYTE_MASKED(struct_bits_off)) { | |
1228 | btf_verifier_log_member(env, struct_type, member, | |
1229 | "Member is not byte aligned"); | |
1230 | return -EINVAL; | |
1231 | } | |
1232 | ||
1233 | struct_size = struct_type->size; | |
1234 | bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off); | |
1235 | if (struct_size - bytes_offset < member_type->size) { | |
1236 | btf_verifier_log_member(env, struct_type, member, | |
1237 | "Member exceeds struct_size"); | |
1238 | return -EINVAL; | |
1239 | } | |
1240 | ||
1241 | return 0; | |
1242 | } | |
1243 | ||
69b693f0 MKL |
1244 | static s32 btf_struct_check_meta(struct btf_verifier_env *env, |
1245 | const struct btf_type *t, | |
1246 | u32 meta_left) | |
1247 | { | |
1248 | bool is_union = BTF_INFO_KIND(t->info) == BTF_KIND_UNION; | |
1249 | const struct btf_member *member; | |
1250 | struct btf *btf = env->btf; | |
1251 | u32 struct_size = t->size; | |
1252 | u32 meta_needed; | |
1253 | u16 i; | |
1254 | ||
1255 | meta_needed = btf_type_vlen(t) * sizeof(*member); | |
1256 | if (meta_left < meta_needed) { | |
1257 | btf_verifier_log_basic(env, t, | |
1258 | "meta_left:%u meta_needed:%u", | |
1259 | meta_left, meta_needed); | |
1260 | return -EINVAL; | |
1261 | } | |
1262 | ||
1263 | btf_verifier_log_type(env, t, NULL); | |
1264 | ||
1265 | for_each_member(i, t, member) { | |
1266 | if (!btf_name_offset_valid(btf, member->name)) { | |
1267 | btf_verifier_log_member(env, t, member, | |
1268 | "Invalid member name_offset:%u", | |
1269 | member->name); | |
1270 | return -EINVAL; | |
1271 | } | |
1272 | ||
1273 | /* A member cannot be in type void */ | |
1274 | if (!member->type || BTF_TYPE_PARENT(member->type)) { | |
1275 | btf_verifier_log_member(env, t, member, | |
1276 | "Invalid type_id"); | |
1277 | return -EINVAL; | |
1278 | } | |
1279 | ||
1280 | if (is_union && member->offset) { | |
1281 | btf_verifier_log_member(env, t, member, | |
1282 | "Invalid member bits_offset"); | |
1283 | return -EINVAL; | |
1284 | } | |
1285 | ||
1286 | if (BITS_ROUNDUP_BYTES(member->offset) > struct_size) { | |
1287 | btf_verifier_log_member(env, t, member, | |
1288 | "Memmber bits_offset exceeds its struct size"); | |
1289 | return -EINVAL; | |
1290 | } | |
1291 | ||
1292 | btf_verifier_log_member(env, t, member, NULL); | |
1293 | } | |
1294 | ||
1295 | return meta_needed; | |
1296 | } | |
1297 | ||
eb3f595d MKL |
1298 | static int btf_struct_resolve(struct btf_verifier_env *env, |
1299 | const struct resolve_vertex *v) | |
1300 | { | |
1301 | const struct btf_member *member; | |
179cde8c | 1302 | int err; |
eb3f595d MKL |
1303 | u16 i; |
1304 | ||
1305 | /* Before continue resolving the next_member, | |
1306 | * ensure the last member is indeed resolved to a | |
1307 | * type with size info. | |
1308 | */ | |
1309 | if (v->next_member) { | |
179cde8c | 1310 | const struct btf_type *last_member_type; |
eb3f595d MKL |
1311 | const struct btf_member *last_member; |
1312 | u16 last_member_type_id; | |
1313 | ||
1314 | last_member = btf_type_member(v->t) + v->next_member - 1; | |
1315 | last_member_type_id = last_member->type; | |
1316 | if (WARN_ON_ONCE(!env_type_is_resolved(env, | |
1317 | last_member_type_id))) | |
1318 | return -EINVAL; | |
179cde8c MKL |
1319 | |
1320 | last_member_type = btf_type_by_id(env->btf, | |
1321 | last_member_type_id); | |
1322 | err = btf_type_ops(last_member_type)->check_member(env, v->t, | |
1323 | last_member, | |
1324 | last_member_type); | |
1325 | if (err) | |
1326 | return err; | |
eb3f595d MKL |
1327 | } |
1328 | ||
1329 | for_each_member_from(i, v->next_member, v->t, member) { | |
1330 | u32 member_type_id = member->type; | |
1331 | const struct btf_type *member_type = btf_type_by_id(env->btf, | |
1332 | member_type_id); | |
1333 | ||
1334 | if (btf_type_is_void_or_null(member_type)) { | |
1335 | btf_verifier_log_member(env, v->t, member, | |
1336 | "Invalid member"); | |
1337 | return -EINVAL; | |
1338 | } | |
1339 | ||
1340 | if (!env_type_is_resolve_sink(env, member_type) && | |
1341 | !env_type_is_resolved(env, member_type_id)) { | |
1342 | env_stack_set_next_member(env, i + 1); | |
1343 | return env_stack_push(env, member_type, member_type_id); | |
1344 | } | |
179cde8c MKL |
1345 | |
1346 | err = btf_type_ops(member_type)->check_member(env, v->t, | |
1347 | member, | |
1348 | member_type); | |
1349 | if (err) | |
1350 | return err; | |
eb3f595d MKL |
1351 | } |
1352 | ||
1353 | env_stack_pop_resolved(env, 0, 0); | |
1354 | ||
1355 | return 0; | |
1356 | } | |
1357 | ||
69b693f0 MKL |
1358 | static void btf_struct_log(struct btf_verifier_env *env, |
1359 | const struct btf_type *t) | |
1360 | { | |
1361 | btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t)); | |
1362 | } | |
1363 | ||
1364 | static struct btf_kind_operations struct_ops = { | |
1365 | .check_meta = btf_struct_check_meta, | |
eb3f595d | 1366 | .resolve = btf_struct_resolve, |
179cde8c | 1367 | .check_member = btf_struct_check_member, |
69b693f0 MKL |
1368 | .log_details = btf_struct_log, |
1369 | }; | |
1370 | ||
179cde8c MKL |
1371 | static int btf_enum_check_member(struct btf_verifier_env *env, |
1372 | const struct btf_type *struct_type, | |
1373 | const struct btf_member *member, | |
1374 | const struct btf_type *member_type) | |
1375 | { | |
1376 | u32 struct_bits_off = member->offset; | |
1377 | u32 struct_size, bytes_offset; | |
1378 | ||
1379 | if (BITS_PER_BYTE_MASKED(struct_bits_off)) { | |
1380 | btf_verifier_log_member(env, struct_type, member, | |
1381 | "Member is not byte aligned"); | |
1382 | return -EINVAL; | |
1383 | } | |
1384 | ||
1385 | struct_size = struct_type->size; | |
1386 | bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off); | |
1387 | if (struct_size - bytes_offset < sizeof(int)) { | |
1388 | btf_verifier_log_member(env, struct_type, member, | |
1389 | "Member exceeds struct_size"); | |
1390 | return -EINVAL; | |
1391 | } | |
1392 | ||
1393 | return 0; | |
1394 | } | |
1395 | ||
69b693f0 MKL |
1396 | static s32 btf_enum_check_meta(struct btf_verifier_env *env, |
1397 | const struct btf_type *t, | |
1398 | u32 meta_left) | |
1399 | { | |
1400 | const struct btf_enum *enums = btf_type_enum(t); | |
1401 | struct btf *btf = env->btf; | |
1402 | u16 i, nr_enums; | |
1403 | u32 meta_needed; | |
1404 | ||
1405 | nr_enums = btf_type_vlen(t); | |
1406 | meta_needed = nr_enums * sizeof(*enums); | |
1407 | ||
1408 | if (meta_left < meta_needed) { | |
1409 | btf_verifier_log_basic(env, t, | |
1410 | "meta_left:%u meta_needed:%u", | |
1411 | meta_left, meta_needed); | |
1412 | return -EINVAL; | |
1413 | } | |
1414 | ||
1415 | if (t->size != sizeof(int)) { | |
1416 | btf_verifier_log_type(env, t, "Expected size:%zu", | |
1417 | sizeof(int)); | |
1418 | return -EINVAL; | |
1419 | } | |
1420 | ||
1421 | btf_verifier_log_type(env, t, NULL); | |
1422 | ||
1423 | for (i = 0; i < nr_enums; i++) { | |
1424 | if (!btf_name_offset_valid(btf, enums[i].name)) { | |
1425 | btf_verifier_log(env, "\tInvalid name_offset:%u", | |
1426 | enums[i].name); | |
1427 | return -EINVAL; | |
1428 | } | |
1429 | ||
1430 | btf_verifier_log(env, "\t%s val=%d\n", | |
1431 | btf_name_by_offset(btf, enums[i].name), | |
1432 | enums[i].val); | |
1433 | } | |
1434 | ||
1435 | return meta_needed; | |
1436 | } | |
1437 | ||
1438 | static void btf_enum_log(struct btf_verifier_env *env, | |
1439 | const struct btf_type *t) | |
1440 | { | |
1441 | btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t)); | |
1442 | } | |
1443 | ||
1444 | static struct btf_kind_operations enum_ops = { | |
1445 | .check_meta = btf_enum_check_meta, | |
eb3f595d | 1446 | .resolve = btf_df_resolve, |
179cde8c | 1447 | .check_member = btf_enum_check_member, |
69b693f0 MKL |
1448 | .log_details = btf_enum_log, |
1449 | }; | |
1450 | ||
1451 | static const struct btf_kind_operations * const kind_ops[NR_BTF_KINDS] = { | |
1452 | [BTF_KIND_INT] = &int_ops, | |
1453 | [BTF_KIND_PTR] = &ptr_ops, | |
1454 | [BTF_KIND_ARRAY] = &array_ops, | |
1455 | [BTF_KIND_STRUCT] = &struct_ops, | |
1456 | [BTF_KIND_UNION] = &struct_ops, | |
1457 | [BTF_KIND_ENUM] = &enum_ops, | |
1458 | [BTF_KIND_FWD] = &fwd_ops, | |
1459 | [BTF_KIND_TYPEDEF] = &modifier_ops, | |
1460 | [BTF_KIND_VOLATILE] = &modifier_ops, | |
1461 | [BTF_KIND_CONST] = &modifier_ops, | |
1462 | [BTF_KIND_RESTRICT] = &modifier_ops, | |
1463 | }; | |
1464 | ||
1465 | static s32 btf_check_meta(struct btf_verifier_env *env, | |
1466 | const struct btf_type *t, | |
1467 | u32 meta_left) | |
1468 | { | |
1469 | u32 saved_meta_left = meta_left; | |
1470 | s32 var_meta_size; | |
1471 | ||
1472 | if (meta_left < sizeof(*t)) { | |
1473 | btf_verifier_log(env, "[%u] meta_left:%u meta_needed:%zu", | |
1474 | env->log_type_id, meta_left, sizeof(*t)); | |
1475 | return -EINVAL; | |
1476 | } | |
1477 | meta_left -= sizeof(*t); | |
1478 | ||
1479 | if (BTF_INFO_KIND(t->info) > BTF_KIND_MAX || | |
1480 | BTF_INFO_KIND(t->info) == BTF_KIND_UNKN) { | |
1481 | btf_verifier_log(env, "[%u] Invalid kind:%u", | |
1482 | env->log_type_id, BTF_INFO_KIND(t->info)); | |
1483 | return -EINVAL; | |
1484 | } | |
1485 | ||
1486 | if (!btf_name_offset_valid(env->btf, t->name)) { | |
1487 | btf_verifier_log(env, "[%u] Invalid name_offset:%u", | |
1488 | env->log_type_id, t->name); | |
1489 | return -EINVAL; | |
1490 | } | |
1491 | ||
1492 | var_meta_size = btf_type_ops(t)->check_meta(env, t, meta_left); | |
1493 | if (var_meta_size < 0) | |
1494 | return var_meta_size; | |
1495 | ||
1496 | meta_left -= var_meta_size; | |
1497 | ||
1498 | return saved_meta_left - meta_left; | |
1499 | } | |
1500 | ||
1501 | static int btf_check_all_metas(struct btf_verifier_env *env) | |
1502 | { | |
1503 | struct btf *btf = env->btf; | |
1504 | struct btf_header *hdr; | |
1505 | void *cur, *end; | |
1506 | ||
1507 | hdr = btf->hdr; | |
1508 | cur = btf->nohdr_data + hdr->type_off; | |
1509 | end = btf->nohdr_data + hdr->str_off; | |
1510 | ||
1511 | env->log_type_id = 1; | |
1512 | while (cur < end) { | |
1513 | struct btf_type *t = cur; | |
1514 | s32 meta_size; | |
1515 | ||
1516 | meta_size = btf_check_meta(env, t, end - cur); | |
1517 | if (meta_size < 0) | |
1518 | return meta_size; | |
1519 | ||
1520 | btf_add_type(env, t); | |
1521 | cur += meta_size; | |
1522 | env->log_type_id++; | |
1523 | } | |
1524 | ||
1525 | return 0; | |
1526 | } | |
1527 | ||
eb3f595d MKL |
1528 | static int btf_resolve(struct btf_verifier_env *env, |
1529 | const struct btf_type *t, u32 type_id) | |
1530 | { | |
1531 | const struct resolve_vertex *v; | |
1532 | int err = 0; | |
1533 | ||
1534 | env->resolve_mode = RESOLVE_TBD; | |
1535 | env_stack_push(env, t, type_id); | |
1536 | while (!err && (v = env_stack_peak(env))) { | |
1537 | env->log_type_id = v->type_id; | |
1538 | err = btf_type_ops(v->t)->resolve(env, v); | |
1539 | } | |
1540 | ||
1541 | env->log_type_id = type_id; | |
1542 | if (err == -E2BIG) | |
1543 | btf_verifier_log_type(env, t, | |
1544 | "Exceeded max resolving depth:%u", | |
1545 | MAX_RESOLVE_DEPTH); | |
1546 | else if (err == -EEXIST) | |
1547 | btf_verifier_log_type(env, t, "Loop detected"); | |
1548 | ||
1549 | return err; | |
1550 | } | |
1551 | ||
1552 | static bool btf_resolve_valid(struct btf_verifier_env *env, | |
1553 | const struct btf_type *t, | |
1554 | u32 type_id) | |
1555 | { | |
1556 | struct btf *btf = env->btf; | |
1557 | ||
1558 | if (!env_type_is_resolved(env, type_id)) | |
1559 | return false; | |
1560 | ||
1561 | if (btf_type_is_struct(t)) | |
1562 | return !btf->resolved_ids[type_id] && | |
1563 | !btf->resolved_sizes[type_id]; | |
1564 | ||
1565 | if (btf_type_is_modifier(t) || btf_type_is_ptr(t)) { | |
1566 | t = btf_type_id_resolve(btf, &type_id); | |
1567 | return t && !btf_type_is_modifier(t); | |
1568 | } | |
1569 | ||
1570 | if (btf_type_is_array(t)) { | |
1571 | const struct btf_array *array = btf_type_array(t); | |
1572 | const struct btf_type *elem_type; | |
1573 | u32 elem_type_id = array->type; | |
1574 | u32 elem_size; | |
1575 | ||
1576 | elem_type = btf_type_id_size(btf, &elem_type_id, &elem_size); | |
1577 | return elem_type && !btf_type_is_modifier(elem_type) && | |
1578 | (array->nelems * elem_size == | |
1579 | btf->resolved_sizes[type_id]); | |
1580 | } | |
1581 | ||
1582 | return false; | |
1583 | } | |
1584 | ||
1585 | static int btf_check_all_types(struct btf_verifier_env *env) | |
1586 | { | |
1587 | struct btf *btf = env->btf; | |
1588 | u32 type_id; | |
1589 | int err; | |
1590 | ||
1591 | err = env_resolve_init(env); | |
1592 | if (err) | |
1593 | return err; | |
1594 | ||
1595 | env->phase++; | |
1596 | for (type_id = 1; type_id <= btf->nr_types; type_id++) { | |
1597 | const struct btf_type *t = btf_type_by_id(btf, type_id); | |
1598 | ||
1599 | env->log_type_id = type_id; | |
1600 | if (btf_type_needs_resolve(t) && | |
1601 | !env_type_is_resolved(env, type_id)) { | |
1602 | err = btf_resolve(env, t, type_id); | |
1603 | if (err) | |
1604 | return err; | |
1605 | } | |
1606 | ||
1607 | if (btf_type_needs_resolve(t) && | |
1608 | !btf_resolve_valid(env, t, type_id)) { | |
1609 | btf_verifier_log_type(env, t, "Invalid resolve state"); | |
1610 | return -EINVAL; | |
1611 | } | |
1612 | } | |
1613 | ||
1614 | return 0; | |
1615 | } | |
1616 | ||
69b693f0 MKL |
1617 | static int btf_parse_type_sec(struct btf_verifier_env *env) |
1618 | { | |
eb3f595d MKL |
1619 | int err; |
1620 | ||
1621 | err = btf_check_all_metas(env); | |
1622 | if (err) | |
1623 | return err; | |
1624 | ||
1625 | return btf_check_all_types(env); | |
69b693f0 MKL |
1626 | } |
1627 | ||
1628 | static int btf_parse_str_sec(struct btf_verifier_env *env) | |
1629 | { | |
1630 | const struct btf_header *hdr; | |
1631 | struct btf *btf = env->btf; | |
1632 | const char *start, *end; | |
1633 | ||
1634 | hdr = btf->hdr; | |
1635 | start = btf->nohdr_data + hdr->str_off; | |
1636 | end = start + hdr->str_len; | |
1637 | ||
1638 | if (!hdr->str_len || hdr->str_len - 1 > BTF_MAX_NAME_OFFSET || | |
1639 | start[0] || end[-1]) { | |
1640 | btf_verifier_log(env, "Invalid string section"); | |
1641 | return -EINVAL; | |
1642 | } | |
1643 | ||
1644 | btf->strings = start; | |
1645 | ||
1646 | return 0; | |
1647 | } | |
1648 | ||
1649 | static int btf_parse_hdr(struct btf_verifier_env *env) | |
1650 | { | |
1651 | const struct btf_header *hdr; | |
1652 | struct btf *btf = env->btf; | |
1653 | u32 meta_left; | |
1654 | ||
1655 | if (btf->data_size < sizeof(*hdr)) { | |
1656 | btf_verifier_log(env, "btf_header not found"); | |
1657 | return -EINVAL; | |
1658 | } | |
1659 | ||
1660 | btf_verifier_log_hdr(env); | |
1661 | ||
1662 | hdr = btf->hdr; | |
1663 | if (hdr->magic != BTF_MAGIC) { | |
1664 | btf_verifier_log(env, "Invalid magic"); | |
1665 | return -EINVAL; | |
1666 | } | |
1667 | ||
1668 | if (hdr->version != BTF_VERSION) { | |
1669 | btf_verifier_log(env, "Unsupported version"); | |
1670 | return -ENOTSUPP; | |
1671 | } | |
1672 | ||
1673 | if (hdr->flags) { | |
1674 | btf_verifier_log(env, "Unsupported flags"); | |
1675 | return -ENOTSUPP; | |
1676 | } | |
1677 | ||
1678 | meta_left = btf->data_size - sizeof(*hdr); | |
1679 | if (!meta_left) { | |
1680 | btf_verifier_log(env, "No data"); | |
1681 | return -EINVAL; | |
1682 | } | |
1683 | ||
1684 | if (meta_left < hdr->type_off || hdr->str_off <= hdr->type_off || | |
1685 | /* Type section must align to 4 bytes */ | |
1686 | hdr->type_off & (sizeof(u32) - 1)) { | |
1687 | btf_verifier_log(env, "Invalid type_off"); | |
1688 | return -EINVAL; | |
1689 | } | |
1690 | ||
1691 | if (meta_left < hdr->str_off || | |
1692 | meta_left - hdr->str_off < hdr->str_len) { | |
1693 | btf_verifier_log(env, "Invalid str_off or str_len"); | |
1694 | return -EINVAL; | |
1695 | } | |
1696 | ||
1697 | btf->nohdr_data = btf->hdr + 1; | |
1698 | ||
1699 | return 0; | |
1700 | } | |
1701 | ||
1702 | static struct btf *btf_parse(void __user *btf_data, u32 btf_data_size, | |
1703 | u32 log_level, char __user *log_ubuf, u32 log_size) | |
1704 | { | |
1705 | struct btf_verifier_env *env = NULL; | |
1706 | struct bpf_verifier_log *log; | |
1707 | struct btf *btf = NULL; | |
1708 | u8 *data; | |
1709 | int err; | |
1710 | ||
1711 | if (btf_data_size > BTF_MAX_SIZE) | |
1712 | return ERR_PTR(-E2BIG); | |
1713 | ||
1714 | env = kzalloc(sizeof(*env), GFP_KERNEL | __GFP_NOWARN); | |
1715 | if (!env) | |
1716 | return ERR_PTR(-ENOMEM); | |
1717 | ||
1718 | log = &env->log; | |
1719 | if (log_level || log_ubuf || log_size) { | |
1720 | /* user requested verbose verifier output | |
1721 | * and supplied buffer to store the verification trace | |
1722 | */ | |
1723 | log->level = log_level; | |
1724 | log->ubuf = log_ubuf; | |
1725 | log->len_total = log_size; | |
1726 | ||
1727 | /* log attributes have to be sane */ | |
1728 | if (log->len_total < 128 || log->len_total > UINT_MAX >> 8 || | |
1729 | !log->level || !log->ubuf) { | |
1730 | err = -EINVAL; | |
1731 | goto errout; | |
1732 | } | |
1733 | } | |
1734 | ||
1735 | btf = kzalloc(sizeof(*btf), GFP_KERNEL | __GFP_NOWARN); | |
1736 | if (!btf) { | |
1737 | err = -ENOMEM; | |
1738 | goto errout; | |
1739 | } | |
1740 | ||
1741 | data = kvmalloc(btf_data_size, GFP_KERNEL | __GFP_NOWARN); | |
1742 | if (!data) { | |
1743 | err = -ENOMEM; | |
1744 | goto errout; | |
1745 | } | |
1746 | ||
1747 | btf->data = data; | |
1748 | btf->data_size = btf_data_size; | |
1749 | ||
1750 | if (copy_from_user(data, btf_data, btf_data_size)) { | |
1751 | err = -EFAULT; | |
1752 | goto errout; | |
1753 | } | |
1754 | ||
1755 | env->btf = btf; | |
1756 | ||
1757 | err = btf_parse_hdr(env); | |
1758 | if (err) | |
1759 | goto errout; | |
1760 | ||
1761 | err = btf_parse_str_sec(env); | |
1762 | if (err) | |
1763 | goto errout; | |
1764 | ||
1765 | err = btf_parse_type_sec(env); | |
1766 | if (err) | |
1767 | goto errout; | |
1768 | ||
1769 | if (!err && log->level && bpf_verifier_log_full(log)) { | |
1770 | err = -ENOSPC; | |
1771 | goto errout; | |
1772 | } | |
1773 | ||
1774 | if (!err) { | |
1775 | btf_verifier_env_free(env); | |
1776 | return btf; | |
1777 | } | |
1778 | ||
1779 | errout: | |
1780 | btf_verifier_env_free(env); | |
1781 | if (btf) | |
1782 | btf_free(btf); | |
1783 | return ERR_PTR(err); | |
1784 | } |