Commit | Line | Data |
---|---|---|
69b693f0 MKL |
1 | /* SPDX-License-Identifier: GPL-2.0 */ |
2 | /* Copyright (c) 2018 Facebook */ | |
3 | ||
4 | #include <uapi/linux/btf.h> | |
91cc1a99 AS |
5 | #include <uapi/linux/bpf.h> |
6 | #include <uapi/linux/bpf_perf_event.h> | |
69b693f0 | 7 | #include <uapi/linux/types.h> |
b00b8dae | 8 | #include <linux/seq_file.h> |
69b693f0 | 9 | #include <linux/compiler.h> |
2667a262 | 10 | #include <linux/ctype.h> |
69b693f0 MKL |
11 | #include <linux/errno.h> |
12 | #include <linux/slab.h> | |
f56a653c MKL |
13 | #include <linux/anon_inodes.h> |
14 | #include <linux/file.h> | |
69b693f0 MKL |
15 | #include <linux/uaccess.h> |
16 | #include <linux/kernel.h> | |
78958fca | 17 | #include <linux/idr.h> |
f80442a4 | 18 | #include <linux/sort.h> |
69b693f0 MKL |
19 | #include <linux/bpf_verifier.h> |
20 | #include <linux/btf.h> | |
91cc1a99 AS |
21 | #include <linux/skmsg.h> |
22 | #include <linux/perf_event.h> | |
23 | #include <net/sock.h> | |
69b693f0 MKL |
24 | |
25 | /* BTF (BPF Type Format) is the meta data format which describes | |
26 | * the data types of BPF program/map. Hence, it basically focus | |
27 | * on the C programming language which the modern BPF is primary | |
28 | * using. | |
29 | * | |
30 | * ELF Section: | |
31 | * ~~~~~~~~~~~ | |
32 | * The BTF data is stored under the ".BTF" ELF section | |
33 | * | |
34 | * struct btf_type: | |
35 | * ~~~~~~~~~~~~~~~ | |
36 | * Each 'struct btf_type' object describes a C data type. | |
37 | * Depending on the type it is describing, a 'struct btf_type' | |
38 | * object may be followed by more data. F.e. | |
39 | * To describe an array, 'struct btf_type' is followed by | |
40 | * 'struct btf_array'. | |
41 | * | |
42 | * 'struct btf_type' and any extra data following it are | |
43 | * 4 bytes aligned. | |
44 | * | |
45 | * Type section: | |
46 | * ~~~~~~~~~~~~~ | |
47 | * The BTF type section contains a list of 'struct btf_type' objects. | |
48 | * Each one describes a C type. Recall from the above section | |
49 | * that a 'struct btf_type' object could be immediately followed by extra | |
50 | * data in order to desribe some particular C types. | |
51 | * | |
52 | * type_id: | |
53 | * ~~~~~~~ | |
54 | * Each btf_type object is identified by a type_id. The type_id | |
55 | * is implicitly implied by the location of the btf_type object in | |
56 | * the BTF type section. The first one has type_id 1. The second | |
57 | * one has type_id 2...etc. Hence, an earlier btf_type has | |
58 | * a smaller type_id. | |
59 | * | |
60 | * A btf_type object may refer to another btf_type object by using | |
61 | * type_id (i.e. the "type" in the "struct btf_type"). | |
62 | * | |
63 | * NOTE that we cannot assume any reference-order. | |
64 | * A btf_type object can refer to an earlier btf_type object | |
65 | * but it can also refer to a later btf_type object. | |
66 | * | |
67 | * For example, to describe "const void *". A btf_type | |
68 | * object describing "const" may refer to another btf_type | |
69 | * object describing "void *". This type-reference is done | |
70 | * by specifying type_id: | |
71 | * | |
72 | * [1] CONST (anon) type_id=2 | |
73 | * [2] PTR (anon) type_id=0 | |
74 | * | |
75 | * The above is the btf_verifier debug log: | |
76 | * - Each line started with "[?]" is a btf_type object | |
77 | * - [?] is the type_id of the btf_type object. | |
78 | * - CONST/PTR is the BTF_KIND_XXX | |
79 | * - "(anon)" is the name of the type. It just | |
80 | * happens that CONST and PTR has no name. | |
81 | * - type_id=XXX is the 'u32 type' in btf_type | |
82 | * | |
83 | * NOTE: "void" has type_id 0 | |
84 | * | |
85 | * String section: | |
86 | * ~~~~~~~~~~~~~~ | |
87 | * The BTF string section contains the names used by the type section. | |
88 | * Each string is referred by an "offset" from the beginning of the | |
89 | * string section. | |
90 | * | |
91 | * Each string is '\0' terminated. | |
92 | * | |
93 | * The first character in the string section must be '\0' | |
94 | * which is used to mean 'anonymous'. Some btf_type may not | |
95 | * have a name. | |
96 | */ | |
97 | ||
98 | /* BTF verification: | |
99 | * | |
100 | * To verify BTF data, two passes are needed. | |
101 | * | |
102 | * Pass #1 | |
103 | * ~~~~~~~ | |
104 | * The first pass is to collect all btf_type objects to | |
105 | * an array: "btf->types". | |
106 | * | |
107 | * Depending on the C type that a btf_type is describing, | |
108 | * a btf_type may be followed by extra data. We don't know | |
109 | * how many btf_type is there, and more importantly we don't | |
110 | * know where each btf_type is located in the type section. | |
111 | * | |
112 | * Without knowing the location of each type_id, most verifications | |
113 | * cannot be done. e.g. an earlier btf_type may refer to a later | |
114 | * btf_type (recall the "const void *" above), so we cannot | |
115 | * check this type-reference in the first pass. | |
116 | * | |
117 | * In the first pass, it still does some verifications (e.g. | |
118 | * checking the name is a valid offset to the string section). | |
eb3f595d MKL |
119 | * |
120 | * Pass #2 | |
121 | * ~~~~~~~ | |
122 | * The main focus is to resolve a btf_type that is referring | |
123 | * to another type. | |
124 | * | |
125 | * We have to ensure the referring type: | |
126 | * 1) does exist in the BTF (i.e. in btf->types[]) | |
127 | * 2) does not cause a loop: | |
128 | * struct A { | |
129 | * struct B b; | |
130 | * }; | |
131 | * | |
132 | * struct B { | |
133 | * struct A a; | |
134 | * }; | |
135 | * | |
136 | * btf_type_needs_resolve() decides if a btf_type needs | |
137 | * to be resolved. | |
138 | * | |
139 | * The needs_resolve type implements the "resolve()" ops which | |
140 | * essentially does a DFS and detects backedge. | |
141 | * | |
142 | * During resolve (or DFS), different C types have different | |
143 | * "RESOLVED" conditions. | |
144 | * | |
145 | * When resolving a BTF_KIND_STRUCT, we need to resolve all its | |
146 | * members because a member is always referring to another | |
147 | * type. A struct's member can be treated as "RESOLVED" if | |
148 | * it is referring to a BTF_KIND_PTR. Otherwise, the | |
149 | * following valid C struct would be rejected: | |
150 | * | |
151 | * struct A { | |
152 | * int m; | |
153 | * struct A *a; | |
154 | * }; | |
155 | * | |
156 | * When resolving a BTF_KIND_PTR, it needs to keep resolving if | |
157 | * it is referring to another BTF_KIND_PTR. Otherwise, we cannot | |
158 | * detect a pointer loop, e.g.: | |
159 | * BTF_KIND_CONST -> BTF_KIND_PTR -> BTF_KIND_CONST -> BTF_KIND_PTR + | |
160 | * ^ | | |
161 | * +-----------------------------------------+ | |
162 | * | |
69b693f0 MKL |
163 | */ |
164 | ||
b1e8818c | 165 | #define BITS_PER_U128 (sizeof(u64) * BITS_PER_BYTE * 2) |
69b693f0 MKL |
166 | #define BITS_PER_BYTE_MASK (BITS_PER_BYTE - 1) |
167 | #define BITS_PER_BYTE_MASKED(bits) ((bits) & BITS_PER_BYTE_MASK) | |
168 | #define BITS_ROUNDDOWN_BYTES(bits) ((bits) >> 3) | |
169 | #define BITS_ROUNDUP_BYTES(bits) \ | |
170 | (BITS_ROUNDDOWN_BYTES(bits) + !!BITS_PER_BYTE_MASKED(bits)) | |
171 | ||
9d5f9f70 | 172 | #define BTF_INFO_MASK 0x8f00ffff |
aea2f7b8 MKL |
173 | #define BTF_INT_MASK 0x0fffffff |
174 | #define BTF_TYPE_ID_VALID(type_id) ((type_id) <= BTF_MAX_TYPE) | |
175 | #define BTF_STR_OFFSET_VALID(name_off) ((name_off) <= BTF_MAX_NAME_OFFSET) | |
176 | ||
69b693f0 MKL |
177 | /* 16MB for 64k structs and each has 16 members and |
178 | * a few MB spaces for the string section. | |
179 | * The hard limit is S32_MAX. | |
180 | */ | |
181 | #define BTF_MAX_SIZE (16 * 1024 * 1024) | |
69b693f0 | 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 | ||
1dc92851 DB |
188 | #define for_each_vsi(i, struct_type, member) \ |
189 | for (i = 0, member = btf_type_var_secinfo(struct_type); \ | |
190 | i < btf_type_vlen(struct_type); \ | |
191 | i++, member++) | |
192 | ||
193 | #define for_each_vsi_from(i, from, struct_type, member) \ | |
194 | for (i = from, member = btf_type_var_secinfo(struct_type) + from; \ | |
195 | i < btf_type_vlen(struct_type); \ | |
196 | i++, member++) | |
197 | ||
1b9ed84e QM |
198 | DEFINE_IDR(btf_idr); |
199 | DEFINE_SPINLOCK(btf_idr_lock); | |
78958fca | 200 | |
69b693f0 | 201 | struct btf { |
f80442a4 | 202 | void *data; |
69b693f0 | 203 | struct btf_type **types; |
eb3f595d MKL |
204 | u32 *resolved_ids; |
205 | u32 *resolved_sizes; | |
69b693f0 MKL |
206 | const char *strings; |
207 | void *nohdr_data; | |
f80442a4 | 208 | struct btf_header hdr; |
69b693f0 MKL |
209 | u32 nr_types; |
210 | u32 types_size; | |
211 | u32 data_size; | |
f56a653c | 212 | refcount_t refcnt; |
78958fca MKL |
213 | u32 id; |
214 | struct rcu_head rcu; | |
69b693f0 MKL |
215 | }; |
216 | ||
eb3f595d MKL |
217 | enum verifier_phase { |
218 | CHECK_META, | |
219 | CHECK_TYPE, | |
220 | }; | |
221 | ||
222 | struct resolve_vertex { | |
223 | const struct btf_type *t; | |
224 | u32 type_id; | |
225 | u16 next_member; | |
226 | }; | |
227 | ||
228 | enum visit_state { | |
229 | NOT_VISITED, | |
230 | VISITED, | |
231 | RESOLVED, | |
232 | }; | |
233 | ||
234 | enum resolve_mode { | |
235 | RESOLVE_TBD, /* To Be Determined */ | |
236 | RESOLVE_PTR, /* Resolving for Pointer */ | |
237 | RESOLVE_STRUCT_OR_ARRAY, /* Resolving for struct/union | |
238 | * or array | |
239 | */ | |
240 | }; | |
241 | ||
242 | #define MAX_RESOLVE_DEPTH 32 | |
243 | ||
f80442a4 MKL |
244 | struct btf_sec_info { |
245 | u32 off; | |
246 | u32 len; | |
247 | }; | |
248 | ||
69b693f0 MKL |
249 | struct btf_verifier_env { |
250 | struct btf *btf; | |
eb3f595d MKL |
251 | u8 *visit_states; |
252 | struct resolve_vertex stack[MAX_RESOLVE_DEPTH]; | |
69b693f0 MKL |
253 | struct bpf_verifier_log log; |
254 | u32 log_type_id; | |
eb3f595d MKL |
255 | u32 top_stack; |
256 | enum verifier_phase phase; | |
257 | enum resolve_mode resolve_mode; | |
69b693f0 MKL |
258 | }; |
259 | ||
260 | static const char * const btf_kind_str[NR_BTF_KINDS] = { | |
261 | [BTF_KIND_UNKN] = "UNKNOWN", | |
262 | [BTF_KIND_INT] = "INT", | |
263 | [BTF_KIND_PTR] = "PTR", | |
264 | [BTF_KIND_ARRAY] = "ARRAY", | |
265 | [BTF_KIND_STRUCT] = "STRUCT", | |
266 | [BTF_KIND_UNION] = "UNION", | |
267 | [BTF_KIND_ENUM] = "ENUM", | |
268 | [BTF_KIND_FWD] = "FWD", | |
269 | [BTF_KIND_TYPEDEF] = "TYPEDEF", | |
270 | [BTF_KIND_VOLATILE] = "VOLATILE", | |
271 | [BTF_KIND_CONST] = "CONST", | |
272 | [BTF_KIND_RESTRICT] = "RESTRICT", | |
2667a262 MKL |
273 | [BTF_KIND_FUNC] = "FUNC", |
274 | [BTF_KIND_FUNC_PROTO] = "FUNC_PROTO", | |
1dc92851 DB |
275 | [BTF_KIND_VAR] = "VAR", |
276 | [BTF_KIND_DATASEC] = "DATASEC", | |
69b693f0 MKL |
277 | }; |
278 | ||
be8704ff AS |
279 | static const char *btf_type_str(const struct btf_type *t) |
280 | { | |
281 | return btf_kind_str[BTF_INFO_KIND(t->info)]; | |
282 | } | |
283 | ||
69b693f0 MKL |
284 | struct btf_kind_operations { |
285 | s32 (*check_meta)(struct btf_verifier_env *env, | |
286 | const struct btf_type *t, | |
287 | u32 meta_left); | |
eb3f595d MKL |
288 | int (*resolve)(struct btf_verifier_env *env, |
289 | const struct resolve_vertex *v); | |
179cde8c MKL |
290 | int (*check_member)(struct btf_verifier_env *env, |
291 | const struct btf_type *struct_type, | |
292 | const struct btf_member *member, | |
293 | const struct btf_type *member_type); | |
9d5f9f70 YS |
294 | int (*check_kflag_member)(struct btf_verifier_env *env, |
295 | const struct btf_type *struct_type, | |
296 | const struct btf_member *member, | |
297 | const struct btf_type *member_type); | |
69b693f0 MKL |
298 | void (*log_details)(struct btf_verifier_env *env, |
299 | const struct btf_type *t); | |
b00b8dae MKL |
300 | void (*seq_show)(const struct btf *btf, const struct btf_type *t, |
301 | u32 type_id, void *data, u8 bits_offsets, | |
302 | struct seq_file *m); | |
69b693f0 MKL |
303 | }; |
304 | ||
305 | static const struct btf_kind_operations * const kind_ops[NR_BTF_KINDS]; | |
306 | static struct btf_type btf_void; | |
307 | ||
2667a262 MKL |
308 | static int btf_resolve(struct btf_verifier_env *env, |
309 | const struct btf_type *t, u32 type_id); | |
310 | ||
eb3f595d MKL |
311 | static bool btf_type_is_modifier(const struct btf_type *t) |
312 | { | |
313 | /* Some of them is not strictly a C modifier | |
314 | * but they are grouped into the same bucket | |
315 | * for BTF concern: | |
316 | * A type (t) that refers to another | |
317 | * type through t->type AND its size cannot | |
318 | * be determined without following the t->type. | |
319 | * | |
320 | * ptr does not fall into this bucket | |
321 | * because its size is always sizeof(void *). | |
322 | */ | |
323 | switch (BTF_INFO_KIND(t->info)) { | |
324 | case BTF_KIND_TYPEDEF: | |
325 | case BTF_KIND_VOLATILE: | |
326 | case BTF_KIND_CONST: | |
327 | case BTF_KIND_RESTRICT: | |
328 | return true; | |
329 | } | |
330 | ||
331 | return false; | |
332 | } | |
333 | ||
2824ecb7 | 334 | bool btf_type_is_void(const struct btf_type *t) |
eb3f595d | 335 | { |
b47a0bd2 MKL |
336 | return t == &btf_void; |
337 | } | |
338 | ||
339 | static bool btf_type_is_fwd(const struct btf_type *t) | |
340 | { | |
341 | return BTF_INFO_KIND(t->info) == BTF_KIND_FWD; | |
342 | } | |
343 | ||
344 | static bool btf_type_nosize(const struct btf_type *t) | |
345 | { | |
2667a262 MKL |
346 | return btf_type_is_void(t) || btf_type_is_fwd(t) || |
347 | btf_type_is_func(t) || btf_type_is_func_proto(t); | |
eb3f595d MKL |
348 | } |
349 | ||
b47a0bd2 | 350 | static bool btf_type_nosize_or_null(const struct btf_type *t) |
eb3f595d | 351 | { |
b47a0bd2 | 352 | return !t || btf_type_nosize(t); |
eb3f595d MKL |
353 | } |
354 | ||
355 | /* union is only a special case of struct: | |
356 | * all its offsetof(member) == 0 | |
357 | */ | |
358 | static bool btf_type_is_struct(const struct btf_type *t) | |
359 | { | |
360 | u8 kind = BTF_INFO_KIND(t->info); | |
361 | ||
362 | return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION; | |
363 | } | |
364 | ||
d83525ca AS |
365 | static bool __btf_type_is_struct(const struct btf_type *t) |
366 | { | |
367 | return BTF_INFO_KIND(t->info) == BTF_KIND_STRUCT; | |
368 | } | |
369 | ||
eb3f595d MKL |
370 | static bool btf_type_is_array(const struct btf_type *t) |
371 | { | |
372 | return BTF_INFO_KIND(t->info) == BTF_KIND_ARRAY; | |
373 | } | |
374 | ||
1dc92851 DB |
375 | static bool btf_type_is_var(const struct btf_type *t) |
376 | { | |
377 | return BTF_INFO_KIND(t->info) == BTF_KIND_VAR; | |
378 | } | |
379 | ||
380 | static bool btf_type_is_datasec(const struct btf_type *t) | |
381 | { | |
382 | return BTF_INFO_KIND(t->info) == BTF_KIND_DATASEC; | |
383 | } | |
384 | ||
27ae7997 MKL |
385 | s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind) |
386 | { | |
387 | const struct btf_type *t; | |
388 | const char *tname; | |
389 | u32 i; | |
390 | ||
391 | for (i = 1; i <= btf->nr_types; i++) { | |
392 | t = btf->types[i]; | |
393 | if (BTF_INFO_KIND(t->info) != kind) | |
394 | continue; | |
395 | ||
396 | tname = btf_name_by_offset(btf, t->name_off); | |
397 | if (!strcmp(tname, name)) | |
398 | return i; | |
399 | } | |
400 | ||
401 | return -ENOENT; | |
402 | } | |
403 | ||
404 | const struct btf_type *btf_type_skip_modifiers(const struct btf *btf, | |
405 | u32 id, u32 *res_id) | |
406 | { | |
407 | const struct btf_type *t = btf_type_by_id(btf, id); | |
408 | ||
409 | while (btf_type_is_modifier(t)) { | |
410 | id = t->type; | |
411 | t = btf_type_by_id(btf, t->type); | |
412 | } | |
413 | ||
414 | if (res_id) | |
415 | *res_id = id; | |
416 | ||
417 | return t; | |
418 | } | |
419 | ||
420 | const struct btf_type *btf_type_resolve_ptr(const struct btf *btf, | |
421 | u32 id, u32 *res_id) | |
422 | { | |
423 | const struct btf_type *t; | |
424 | ||
425 | t = btf_type_skip_modifiers(btf, id, NULL); | |
426 | if (!btf_type_is_ptr(t)) | |
427 | return NULL; | |
428 | ||
429 | return btf_type_skip_modifiers(btf, t->type, res_id); | |
430 | } | |
431 | ||
432 | const struct btf_type *btf_type_resolve_func_ptr(const struct btf *btf, | |
433 | u32 id, u32 *res_id) | |
434 | { | |
435 | const struct btf_type *ptype; | |
436 | ||
437 | ptype = btf_type_resolve_ptr(btf, id, res_id); | |
438 | if (ptype && btf_type_is_func_proto(ptype)) | |
439 | return ptype; | |
440 | ||
441 | return NULL; | |
442 | } | |
443 | ||
1dc92851 DB |
444 | /* Types that act only as a source, not sink or intermediate |
445 | * type when resolving. | |
446 | */ | |
447 | static bool btf_type_is_resolve_source_only(const struct btf_type *t) | |
448 | { | |
449 | return btf_type_is_var(t) || | |
450 | btf_type_is_datasec(t); | |
451 | } | |
452 | ||
eb3f595d MKL |
453 | /* What types need to be resolved? |
454 | * | |
455 | * btf_type_is_modifier() is an obvious one. | |
456 | * | |
457 | * btf_type_is_struct() because its member refers to | |
458 | * another type (through member->type). | |
1dc92851 DB |
459 | * |
460 | * btf_type_is_var() because the variable refers to | |
461 | * another type. btf_type_is_datasec() holds multiple | |
462 | * btf_type_is_var() types that need resolving. | |
463 | * | |
eb3f595d MKL |
464 | * btf_type_is_array() because its element (array->type) |
465 | * refers to another type. Array can be thought of a | |
466 | * special case of struct while array just has the same | |
467 | * member-type repeated by array->nelems of times. | |
468 | */ | |
469 | static bool btf_type_needs_resolve(const struct btf_type *t) | |
470 | { | |
471 | return btf_type_is_modifier(t) || | |
1dc92851 DB |
472 | btf_type_is_ptr(t) || |
473 | btf_type_is_struct(t) || | |
474 | btf_type_is_array(t) || | |
475 | btf_type_is_var(t) || | |
476 | btf_type_is_datasec(t); | |
eb3f595d MKL |
477 | } |
478 | ||
479 | /* t->size can be used */ | |
480 | static bool btf_type_has_size(const struct btf_type *t) | |
481 | { | |
482 | switch (BTF_INFO_KIND(t->info)) { | |
483 | case BTF_KIND_INT: | |
484 | case BTF_KIND_STRUCT: | |
485 | case BTF_KIND_UNION: | |
486 | case BTF_KIND_ENUM: | |
1dc92851 | 487 | case BTF_KIND_DATASEC: |
eb3f595d MKL |
488 | return true; |
489 | } | |
490 | ||
491 | return false; | |
492 | } | |
493 | ||
69b693f0 MKL |
494 | static const char *btf_int_encoding_str(u8 encoding) |
495 | { | |
496 | if (encoding == 0) | |
497 | return "(none)"; | |
498 | else if (encoding == BTF_INT_SIGNED) | |
499 | return "SIGNED"; | |
500 | else if (encoding == BTF_INT_CHAR) | |
501 | return "CHAR"; | |
502 | else if (encoding == BTF_INT_BOOL) | |
503 | return "BOOL"; | |
69b693f0 MKL |
504 | else |
505 | return "UNKN"; | |
506 | } | |
507 | ||
69b693f0 MKL |
508 | static u32 btf_type_int(const struct btf_type *t) |
509 | { | |
510 | return *(u32 *)(t + 1); | |
511 | } | |
512 | ||
513 | static const struct btf_array *btf_type_array(const struct btf_type *t) | |
514 | { | |
515 | return (const struct btf_array *)(t + 1); | |
516 | } | |
517 | ||
69b693f0 MKL |
518 | static const struct btf_enum *btf_type_enum(const struct btf_type *t) |
519 | { | |
520 | return (const struct btf_enum *)(t + 1); | |
521 | } | |
522 | ||
1dc92851 DB |
523 | static const struct btf_var *btf_type_var(const struct btf_type *t) |
524 | { | |
525 | return (const struct btf_var *)(t + 1); | |
526 | } | |
527 | ||
528 | static const struct btf_var_secinfo *btf_type_var_secinfo(const struct btf_type *t) | |
529 | { | |
530 | return (const struct btf_var_secinfo *)(t + 1); | |
531 | } | |
532 | ||
69b693f0 MKL |
533 | static const struct btf_kind_operations *btf_type_ops(const struct btf_type *t) |
534 | { | |
535 | return kind_ops[BTF_INFO_KIND(t->info)]; | |
536 | } | |
537 | ||
583c5318 | 538 | static bool btf_name_offset_valid(const struct btf *btf, u32 offset) |
69b693f0 | 539 | { |
aea2f7b8 MKL |
540 | return BTF_STR_OFFSET_VALID(offset) && |
541 | offset < btf->hdr.str_len; | |
69b693f0 MKL |
542 | } |
543 | ||
1dc92851 DB |
544 | static bool __btf_name_char_ok(char c, bool first, bool dot_ok) |
545 | { | |
546 | if ((first ? !isalpha(c) : | |
547 | !isalnum(c)) && | |
548 | c != '_' && | |
549 | ((c == '.' && !dot_ok) || | |
550 | c != '.')) | |
551 | return false; | |
552 | return true; | |
553 | } | |
554 | ||
555 | static bool __btf_name_valid(const struct btf *btf, u32 offset, bool dot_ok) | |
2667a262 MKL |
556 | { |
557 | /* offset must be valid */ | |
558 | const char *src = &btf->strings[offset]; | |
559 | const char *src_limit; | |
560 | ||
1dc92851 | 561 | if (!__btf_name_char_ok(*src, true, dot_ok)) |
2667a262 MKL |
562 | return false; |
563 | ||
564 | /* set a limit on identifier length */ | |
565 | src_limit = src + KSYM_NAME_LEN; | |
566 | src++; | |
567 | while (*src && src < src_limit) { | |
1dc92851 | 568 | if (!__btf_name_char_ok(*src, false, dot_ok)) |
2667a262 MKL |
569 | return false; |
570 | src++; | |
571 | } | |
572 | ||
573 | return !*src; | |
574 | } | |
575 | ||
1dc92851 DB |
576 | /* Only C-style identifier is permitted. This can be relaxed if |
577 | * necessary. | |
578 | */ | |
579 | static bool btf_name_valid_identifier(const struct btf *btf, u32 offset) | |
580 | { | |
581 | return __btf_name_valid(btf, offset, false); | |
582 | } | |
583 | ||
584 | static bool btf_name_valid_section(const struct btf *btf, u32 offset) | |
585 | { | |
586 | return __btf_name_valid(btf, offset, true); | |
587 | } | |
588 | ||
23127b33 | 589 | static const char *__btf_name_by_offset(const struct btf *btf, u32 offset) |
69b693f0 | 590 | { |
aea2f7b8 | 591 | if (!offset) |
69b693f0 | 592 | return "(anon)"; |
aea2f7b8 MKL |
593 | else if (offset < btf->hdr.str_len) |
594 | return &btf->strings[offset]; | |
69b693f0 MKL |
595 | else |
596 | return "(invalid-name-offset)"; | |
597 | } | |
598 | ||
23127b33 MKL |
599 | const char *btf_name_by_offset(const struct btf *btf, u32 offset) |
600 | { | |
601 | if (offset < btf->hdr.str_len) | |
602 | return &btf->strings[offset]; | |
603 | ||
604 | return NULL; | |
605 | } | |
606 | ||
838e9690 | 607 | const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id) |
eb3f595d MKL |
608 | { |
609 | if (type_id > btf->nr_types) | |
610 | return NULL; | |
611 | ||
612 | return btf->types[type_id]; | |
613 | } | |
614 | ||
4ef5f574 MKL |
615 | /* |
616 | * Regular int is not a bit field and it must be either | |
b1e8818c | 617 | * u8/u16/u32/u64 or __int128. |
4ef5f574 MKL |
618 | */ |
619 | static bool btf_type_int_is_regular(const struct btf_type *t) | |
620 | { | |
36fc3c8c | 621 | u8 nr_bits, nr_bytes; |
4ef5f574 MKL |
622 | u32 int_data; |
623 | ||
624 | int_data = btf_type_int(t); | |
625 | nr_bits = BTF_INT_BITS(int_data); | |
626 | nr_bytes = BITS_ROUNDUP_BYTES(nr_bits); | |
627 | if (BITS_PER_BYTE_MASKED(nr_bits) || | |
628 | BTF_INT_OFFSET(int_data) || | |
629 | (nr_bytes != sizeof(u8) && nr_bytes != sizeof(u16) && | |
b1e8818c YS |
630 | nr_bytes != sizeof(u32) && nr_bytes != sizeof(u64) && |
631 | nr_bytes != (2 * sizeof(u64)))) { | |
4ef5f574 MKL |
632 | return false; |
633 | } | |
634 | ||
635 | return true; | |
636 | } | |
637 | ||
9a1126b6 | 638 | /* |
ffa0c1cf YS |
639 | * Check that given struct member is a regular int with expected |
640 | * offset and size. | |
9a1126b6 | 641 | */ |
ffa0c1cf YS |
642 | bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s, |
643 | const struct btf_member *m, | |
644 | u32 expected_offset, u32 expected_size) | |
9a1126b6 | 645 | { |
ffa0c1cf YS |
646 | const struct btf_type *t; |
647 | u32 id, int_data; | |
648 | u8 nr_bits; | |
9a1126b6 | 649 | |
ffa0c1cf YS |
650 | id = m->type; |
651 | t = btf_type_id_size(btf, &id, NULL); | |
652 | if (!t || !btf_type_is_int(t)) | |
9a1126b6 RG |
653 | return false; |
654 | ||
655 | int_data = btf_type_int(t); | |
656 | nr_bits = BTF_INT_BITS(int_data); | |
ffa0c1cf YS |
657 | if (btf_type_kflag(s)) { |
658 | u32 bitfield_size = BTF_MEMBER_BITFIELD_SIZE(m->offset); | |
659 | u32 bit_offset = BTF_MEMBER_BIT_OFFSET(m->offset); | |
660 | ||
661 | /* if kflag set, int should be a regular int and | |
662 | * bit offset should be at byte boundary. | |
663 | */ | |
664 | return !bitfield_size && | |
665 | BITS_ROUNDUP_BYTES(bit_offset) == expected_offset && | |
666 | BITS_ROUNDUP_BYTES(nr_bits) == expected_size; | |
667 | } | |
668 | ||
669 | if (BTF_INT_OFFSET(int_data) || | |
670 | BITS_PER_BYTE_MASKED(m->offset) || | |
671 | BITS_ROUNDUP_BYTES(m->offset) != expected_offset || | |
672 | BITS_PER_BYTE_MASKED(nr_bits) || | |
673 | BITS_ROUNDUP_BYTES(nr_bits) != expected_size) | |
9a1126b6 RG |
674 | return false; |
675 | ||
676 | return true; | |
677 | } | |
678 | ||
69b693f0 MKL |
679 | __printf(2, 3) static void __btf_verifier_log(struct bpf_verifier_log *log, |
680 | const char *fmt, ...) | |
681 | { | |
682 | va_list args; | |
683 | ||
684 | va_start(args, fmt); | |
685 | bpf_verifier_vlog(log, fmt, args); | |
686 | va_end(args); | |
687 | } | |
688 | ||
689 | __printf(2, 3) static void btf_verifier_log(struct btf_verifier_env *env, | |
690 | const char *fmt, ...) | |
691 | { | |
692 | struct bpf_verifier_log *log = &env->log; | |
693 | va_list args; | |
694 | ||
695 | if (!bpf_verifier_log_needed(log)) | |
696 | return; | |
697 | ||
698 | va_start(args, fmt); | |
699 | bpf_verifier_vlog(log, fmt, args); | |
700 | va_end(args); | |
701 | } | |
702 | ||
703 | __printf(4, 5) static void __btf_verifier_log_type(struct btf_verifier_env *env, | |
704 | const struct btf_type *t, | |
705 | bool log_details, | |
706 | const char *fmt, ...) | |
707 | { | |
708 | struct bpf_verifier_log *log = &env->log; | |
709 | u8 kind = BTF_INFO_KIND(t->info); | |
710 | struct btf *btf = env->btf; | |
711 | va_list args; | |
712 | ||
713 | if (!bpf_verifier_log_needed(log)) | |
714 | return; | |
715 | ||
8580ac94 AS |
716 | /* btf verifier prints all types it is processing via |
717 | * btf_verifier_log_type(..., fmt = NULL). | |
718 | * Skip those prints for in-kernel BTF verification. | |
719 | */ | |
720 | if (log->level == BPF_LOG_KERNEL && !fmt) | |
721 | return; | |
722 | ||
69b693f0 MKL |
723 | __btf_verifier_log(log, "[%u] %s %s%s", |
724 | env->log_type_id, | |
725 | btf_kind_str[kind], | |
23127b33 | 726 | __btf_name_by_offset(btf, t->name_off), |
69b693f0 MKL |
727 | log_details ? " " : ""); |
728 | ||
729 | if (log_details) | |
730 | btf_type_ops(t)->log_details(env, t); | |
731 | ||
732 | if (fmt && *fmt) { | |
733 | __btf_verifier_log(log, " "); | |
734 | va_start(args, fmt); | |
735 | bpf_verifier_vlog(log, fmt, args); | |
736 | va_end(args); | |
737 | } | |
738 | ||
739 | __btf_verifier_log(log, "\n"); | |
740 | } | |
741 | ||
742 | #define btf_verifier_log_type(env, t, ...) \ | |
743 | __btf_verifier_log_type((env), (t), true, __VA_ARGS__) | |
744 | #define btf_verifier_log_basic(env, t, ...) \ | |
745 | __btf_verifier_log_type((env), (t), false, __VA_ARGS__) | |
746 | ||
747 | __printf(4, 5) | |
748 | static void btf_verifier_log_member(struct btf_verifier_env *env, | |
749 | const struct btf_type *struct_type, | |
750 | const struct btf_member *member, | |
751 | const char *fmt, ...) | |
752 | { | |
753 | struct bpf_verifier_log *log = &env->log; | |
754 | struct btf *btf = env->btf; | |
755 | va_list args; | |
756 | ||
757 | if (!bpf_verifier_log_needed(log)) | |
758 | return; | |
759 | ||
8580ac94 AS |
760 | if (log->level == BPF_LOG_KERNEL && !fmt) |
761 | return; | |
eb3f595d MKL |
762 | /* The CHECK_META phase already did a btf dump. |
763 | * | |
764 | * If member is logged again, it must hit an error in | |
765 | * parsing this member. It is useful to print out which | |
766 | * struct this member belongs to. | |
767 | */ | |
768 | if (env->phase != CHECK_META) | |
769 | btf_verifier_log_type(env, struct_type, NULL); | |
770 | ||
9d5f9f70 YS |
771 | if (btf_type_kflag(struct_type)) |
772 | __btf_verifier_log(log, | |
773 | "\t%s type_id=%u bitfield_size=%u bits_offset=%u", | |
774 | __btf_name_by_offset(btf, member->name_off), | |
775 | member->type, | |
776 | BTF_MEMBER_BITFIELD_SIZE(member->offset), | |
777 | BTF_MEMBER_BIT_OFFSET(member->offset)); | |
778 | else | |
779 | __btf_verifier_log(log, "\t%s type_id=%u bits_offset=%u", | |
780 | __btf_name_by_offset(btf, member->name_off), | |
781 | member->type, member->offset); | |
69b693f0 MKL |
782 | |
783 | if (fmt && *fmt) { | |
784 | __btf_verifier_log(log, " "); | |
785 | va_start(args, fmt); | |
786 | bpf_verifier_vlog(log, fmt, args); | |
787 | va_end(args); | |
788 | } | |
789 | ||
790 | __btf_verifier_log(log, "\n"); | |
791 | } | |
792 | ||
1dc92851 DB |
793 | __printf(4, 5) |
794 | static void btf_verifier_log_vsi(struct btf_verifier_env *env, | |
795 | const struct btf_type *datasec_type, | |
796 | const struct btf_var_secinfo *vsi, | |
797 | const char *fmt, ...) | |
798 | { | |
799 | struct bpf_verifier_log *log = &env->log; | |
800 | va_list args; | |
801 | ||
802 | if (!bpf_verifier_log_needed(log)) | |
803 | return; | |
8580ac94 AS |
804 | if (log->level == BPF_LOG_KERNEL && !fmt) |
805 | return; | |
1dc92851 DB |
806 | if (env->phase != CHECK_META) |
807 | btf_verifier_log_type(env, datasec_type, NULL); | |
808 | ||
809 | __btf_verifier_log(log, "\t type_id=%u offset=%u size=%u", | |
810 | vsi->type, vsi->offset, vsi->size); | |
811 | if (fmt && *fmt) { | |
812 | __btf_verifier_log(log, " "); | |
813 | va_start(args, fmt); | |
814 | bpf_verifier_vlog(log, fmt, args); | |
815 | va_end(args); | |
816 | } | |
817 | ||
818 | __btf_verifier_log(log, "\n"); | |
819 | } | |
820 | ||
f80442a4 MKL |
821 | static void btf_verifier_log_hdr(struct btf_verifier_env *env, |
822 | u32 btf_data_size) | |
69b693f0 MKL |
823 | { |
824 | struct bpf_verifier_log *log = &env->log; | |
825 | const struct btf *btf = env->btf; | |
826 | const struct btf_header *hdr; | |
827 | ||
828 | if (!bpf_verifier_log_needed(log)) | |
829 | return; | |
830 | ||
8580ac94 AS |
831 | if (log->level == BPF_LOG_KERNEL) |
832 | return; | |
f80442a4 | 833 | hdr = &btf->hdr; |
69b693f0 MKL |
834 | __btf_verifier_log(log, "magic: 0x%x\n", hdr->magic); |
835 | __btf_verifier_log(log, "version: %u\n", hdr->version); | |
836 | __btf_verifier_log(log, "flags: 0x%x\n", hdr->flags); | |
f80442a4 | 837 | __btf_verifier_log(log, "hdr_len: %u\n", hdr->hdr_len); |
69b693f0 | 838 | __btf_verifier_log(log, "type_off: %u\n", hdr->type_off); |
f80442a4 | 839 | __btf_verifier_log(log, "type_len: %u\n", hdr->type_len); |
69b693f0 MKL |
840 | __btf_verifier_log(log, "str_off: %u\n", hdr->str_off); |
841 | __btf_verifier_log(log, "str_len: %u\n", hdr->str_len); | |
f80442a4 | 842 | __btf_verifier_log(log, "btf_total_size: %u\n", btf_data_size); |
69b693f0 MKL |
843 | } |
844 | ||
845 | static int btf_add_type(struct btf_verifier_env *env, struct btf_type *t) | |
846 | { | |
847 | struct btf *btf = env->btf; | |
848 | ||
849 | /* < 2 because +1 for btf_void which is always in btf->types[0]. | |
850 | * btf_void is not accounted in btf->nr_types because btf_void | |
851 | * does not come from the BTF file. | |
852 | */ | |
853 | if (btf->types_size - btf->nr_types < 2) { | |
854 | /* Expand 'types' array */ | |
855 | ||
856 | struct btf_type **new_types; | |
857 | u32 expand_by, new_size; | |
858 | ||
aea2f7b8 | 859 | if (btf->types_size == BTF_MAX_TYPE) { |
69b693f0 MKL |
860 | btf_verifier_log(env, "Exceeded max num of types"); |
861 | return -E2BIG; | |
862 | } | |
863 | ||
864 | expand_by = max_t(u32, btf->types_size >> 2, 16); | |
aea2f7b8 | 865 | new_size = min_t(u32, BTF_MAX_TYPE, |
69b693f0 MKL |
866 | btf->types_size + expand_by); |
867 | ||
778e1cdd | 868 | new_types = kvcalloc(new_size, sizeof(*new_types), |
69b693f0 MKL |
869 | GFP_KERNEL | __GFP_NOWARN); |
870 | if (!new_types) | |
871 | return -ENOMEM; | |
872 | ||
873 | if (btf->nr_types == 0) | |
874 | new_types[0] = &btf_void; | |
875 | else | |
876 | memcpy(new_types, btf->types, | |
877 | sizeof(*btf->types) * (btf->nr_types + 1)); | |
878 | ||
879 | kvfree(btf->types); | |
880 | btf->types = new_types; | |
881 | btf->types_size = new_size; | |
882 | } | |
883 | ||
884 | btf->types[++(btf->nr_types)] = t; | |
885 | ||
886 | return 0; | |
887 | } | |
888 | ||
78958fca MKL |
889 | static int btf_alloc_id(struct btf *btf) |
890 | { | |
891 | int id; | |
892 | ||
893 | idr_preload(GFP_KERNEL); | |
894 | spin_lock_bh(&btf_idr_lock); | |
895 | id = idr_alloc_cyclic(&btf_idr, btf, 1, INT_MAX, GFP_ATOMIC); | |
896 | if (id > 0) | |
897 | btf->id = id; | |
898 | spin_unlock_bh(&btf_idr_lock); | |
899 | idr_preload_end(); | |
900 | ||
901 | if (WARN_ON_ONCE(!id)) | |
902 | return -ENOSPC; | |
903 | ||
904 | return id > 0 ? 0 : id; | |
905 | } | |
906 | ||
907 | static void btf_free_id(struct btf *btf) | |
908 | { | |
909 | unsigned long flags; | |
910 | ||
911 | /* | |
912 | * In map-in-map, calling map_delete_elem() on outer | |
913 | * map will call bpf_map_put on the inner map. | |
914 | * It will then eventually call btf_free_id() | |
915 | * on the inner map. Some of the map_delete_elem() | |
916 | * implementation may have irq disabled, so | |
917 | * we need to use the _irqsave() version instead | |
918 | * of the _bh() version. | |
919 | */ | |
920 | spin_lock_irqsave(&btf_idr_lock, flags); | |
921 | idr_remove(&btf_idr, btf->id); | |
922 | spin_unlock_irqrestore(&btf_idr_lock, flags); | |
923 | } | |
924 | ||
69b693f0 MKL |
925 | static void btf_free(struct btf *btf) |
926 | { | |
927 | kvfree(btf->types); | |
eb3f595d MKL |
928 | kvfree(btf->resolved_sizes); |
929 | kvfree(btf->resolved_ids); | |
69b693f0 MKL |
930 | kvfree(btf->data); |
931 | kfree(btf); | |
932 | } | |
933 | ||
78958fca | 934 | static void btf_free_rcu(struct rcu_head *rcu) |
f56a653c | 935 | { |
78958fca MKL |
936 | struct btf *btf = container_of(rcu, struct btf, rcu); |
937 | ||
938 | btf_free(btf); | |
f56a653c MKL |
939 | } |
940 | ||
941 | void btf_put(struct btf *btf) | |
942 | { | |
78958fca MKL |
943 | if (btf && refcount_dec_and_test(&btf->refcnt)) { |
944 | btf_free_id(btf); | |
945 | call_rcu(&btf->rcu, btf_free_rcu); | |
946 | } | |
f56a653c MKL |
947 | } |
948 | ||
eb3f595d MKL |
949 | static int env_resolve_init(struct btf_verifier_env *env) |
950 | { | |
951 | struct btf *btf = env->btf; | |
952 | u32 nr_types = btf->nr_types; | |
953 | u32 *resolved_sizes = NULL; | |
954 | u32 *resolved_ids = NULL; | |
955 | u8 *visit_states = NULL; | |
956 | ||
957 | /* +1 for btf_void */ | |
778e1cdd | 958 | resolved_sizes = kvcalloc(nr_types + 1, sizeof(*resolved_sizes), |
eb3f595d MKL |
959 | GFP_KERNEL | __GFP_NOWARN); |
960 | if (!resolved_sizes) | |
961 | goto nomem; | |
962 | ||
778e1cdd | 963 | resolved_ids = kvcalloc(nr_types + 1, sizeof(*resolved_ids), |
eb3f595d MKL |
964 | GFP_KERNEL | __GFP_NOWARN); |
965 | if (!resolved_ids) | |
966 | goto nomem; | |
967 | ||
778e1cdd | 968 | visit_states = kvcalloc(nr_types + 1, sizeof(*visit_states), |
eb3f595d MKL |
969 | GFP_KERNEL | __GFP_NOWARN); |
970 | if (!visit_states) | |
971 | goto nomem; | |
972 | ||
973 | btf->resolved_sizes = resolved_sizes; | |
974 | btf->resolved_ids = resolved_ids; | |
975 | env->visit_states = visit_states; | |
976 | ||
977 | return 0; | |
978 | ||
979 | nomem: | |
980 | kvfree(resolved_sizes); | |
981 | kvfree(resolved_ids); | |
982 | kvfree(visit_states); | |
983 | return -ENOMEM; | |
984 | } | |
985 | ||
69b693f0 MKL |
986 | static void btf_verifier_env_free(struct btf_verifier_env *env) |
987 | { | |
eb3f595d | 988 | kvfree(env->visit_states); |
69b693f0 MKL |
989 | kfree(env); |
990 | } | |
991 | ||
eb3f595d MKL |
992 | static bool env_type_is_resolve_sink(const struct btf_verifier_env *env, |
993 | const struct btf_type *next_type) | |
994 | { | |
995 | switch (env->resolve_mode) { | |
996 | case RESOLVE_TBD: | |
997 | /* int, enum or void is a sink */ | |
998 | return !btf_type_needs_resolve(next_type); | |
999 | case RESOLVE_PTR: | |
2667a262 MKL |
1000 | /* int, enum, void, struct, array, func or func_proto is a sink |
1001 | * for ptr | |
1002 | */ | |
eb3f595d MKL |
1003 | return !btf_type_is_modifier(next_type) && |
1004 | !btf_type_is_ptr(next_type); | |
1005 | case RESOLVE_STRUCT_OR_ARRAY: | |
2667a262 MKL |
1006 | /* int, enum, void, ptr, func or func_proto is a sink |
1007 | * for struct and array | |
1008 | */ | |
eb3f595d MKL |
1009 | return !btf_type_is_modifier(next_type) && |
1010 | !btf_type_is_array(next_type) && | |
1011 | !btf_type_is_struct(next_type); | |
1012 | default: | |
53c8036c | 1013 | BUG(); |
eb3f595d MKL |
1014 | } |
1015 | } | |
1016 | ||
1017 | static bool env_type_is_resolved(const struct btf_verifier_env *env, | |
1018 | u32 type_id) | |
1019 | { | |
1020 | return env->visit_states[type_id] == RESOLVED; | |
1021 | } | |
1022 | ||
1023 | static int env_stack_push(struct btf_verifier_env *env, | |
1024 | const struct btf_type *t, u32 type_id) | |
1025 | { | |
1026 | struct resolve_vertex *v; | |
1027 | ||
1028 | if (env->top_stack == MAX_RESOLVE_DEPTH) | |
1029 | return -E2BIG; | |
1030 | ||
1031 | if (env->visit_states[type_id] != NOT_VISITED) | |
1032 | return -EEXIST; | |
1033 | ||
1034 | env->visit_states[type_id] = VISITED; | |
1035 | ||
1036 | v = &env->stack[env->top_stack++]; | |
1037 | v->t = t; | |
1038 | v->type_id = type_id; | |
1039 | v->next_member = 0; | |
1040 | ||
1041 | if (env->resolve_mode == RESOLVE_TBD) { | |
1042 | if (btf_type_is_ptr(t)) | |
1043 | env->resolve_mode = RESOLVE_PTR; | |
1044 | else if (btf_type_is_struct(t) || btf_type_is_array(t)) | |
1045 | env->resolve_mode = RESOLVE_STRUCT_OR_ARRAY; | |
1046 | } | |
1047 | ||
1048 | return 0; | |
1049 | } | |
1050 | ||
1051 | static void env_stack_set_next_member(struct btf_verifier_env *env, | |
1052 | u16 next_member) | |
1053 | { | |
1054 | env->stack[env->top_stack - 1].next_member = next_member; | |
1055 | } | |
1056 | ||
1057 | static void env_stack_pop_resolved(struct btf_verifier_env *env, | |
1058 | u32 resolved_type_id, | |
1059 | u32 resolved_size) | |
1060 | { | |
1061 | u32 type_id = env->stack[--(env->top_stack)].type_id; | |
1062 | struct btf *btf = env->btf; | |
1063 | ||
1064 | btf->resolved_sizes[type_id] = resolved_size; | |
1065 | btf->resolved_ids[type_id] = resolved_type_id; | |
1066 | env->visit_states[type_id] = RESOLVED; | |
1067 | } | |
1068 | ||
1069 | static const struct resolve_vertex *env_stack_peak(struct btf_verifier_env *env) | |
1070 | { | |
1071 | return env->top_stack ? &env->stack[env->top_stack - 1] : NULL; | |
1072 | } | |
1073 | ||
7e3617a7 MKL |
1074 | /* Resolve the size of a passed-in "type" |
1075 | * | |
1076 | * type: is an array (e.g. u32 array[x][y]) | |
1077 | * return type: type "u32[x][y]", i.e. BTF_KIND_ARRAY, | |
1078 | * *type_size: (x * y * sizeof(u32)). Hence, *type_size always | |
1079 | * corresponds to the return type. | |
1080 | * *elem_type: u32 | |
1081 | * *total_nelems: (x * y). Hence, individual elem size is | |
1082 | * (*type_size / *total_nelems) | |
1083 | * | |
1084 | * type: is not an array (e.g. const struct X) | |
1085 | * return type: type "struct X" | |
1086 | * *type_size: sizeof(struct X) | |
1087 | * *elem_type: same as return type ("struct X") | |
1088 | * *total_nelems: 1 | |
1089 | */ | |
85d33df3 | 1090 | const struct btf_type * |
7e3617a7 MKL |
1091 | btf_resolve_size(const struct btf *btf, const struct btf_type *type, |
1092 | u32 *type_size, const struct btf_type **elem_type, | |
1093 | u32 *total_nelems) | |
1094 | { | |
1095 | const struct btf_type *array_type = NULL; | |
1096 | const struct btf_array *array; | |
1097 | u32 i, size, nelems = 1; | |
1098 | ||
1099 | for (i = 0; i < MAX_RESOLVE_DEPTH; i++) { | |
1100 | switch (BTF_INFO_KIND(type->info)) { | |
1101 | /* type->size can be used */ | |
1102 | case BTF_KIND_INT: | |
1103 | case BTF_KIND_STRUCT: | |
1104 | case BTF_KIND_UNION: | |
1105 | case BTF_KIND_ENUM: | |
1106 | size = type->size; | |
1107 | goto resolved; | |
1108 | ||
1109 | case BTF_KIND_PTR: | |
1110 | size = sizeof(void *); | |
1111 | goto resolved; | |
1112 | ||
1113 | /* Modifiers */ | |
1114 | case BTF_KIND_TYPEDEF: | |
1115 | case BTF_KIND_VOLATILE: | |
1116 | case BTF_KIND_CONST: | |
1117 | case BTF_KIND_RESTRICT: | |
1118 | type = btf_type_by_id(btf, type->type); | |
1119 | break; | |
1120 | ||
1121 | case BTF_KIND_ARRAY: | |
1122 | if (!array_type) | |
1123 | array_type = type; | |
1124 | array = btf_type_array(type); | |
1125 | if (nelems && array->nelems > U32_MAX / nelems) | |
1126 | return ERR_PTR(-EINVAL); | |
1127 | nelems *= array->nelems; | |
1128 | type = btf_type_by_id(btf, array->type); | |
1129 | break; | |
1130 | ||
1131 | /* type without size */ | |
1132 | default: | |
1133 | return ERR_PTR(-EINVAL); | |
1134 | } | |
1135 | } | |
1136 | ||
1137 | return ERR_PTR(-EINVAL); | |
1138 | ||
1139 | resolved: | |
1140 | if (nelems && size > U32_MAX / nelems) | |
1141 | return ERR_PTR(-EINVAL); | |
1142 | ||
1143 | *type_size = nelems * size; | |
85d33df3 MKL |
1144 | if (total_nelems) |
1145 | *total_nelems = nelems; | |
1146 | if (elem_type) | |
1147 | *elem_type = type; | |
7e3617a7 MKL |
1148 | |
1149 | return array_type ? : type; | |
1150 | } | |
1151 | ||
eb3f595d MKL |
1152 | /* The input param "type_id" must point to a needs_resolve type */ |
1153 | static const struct btf_type *btf_type_id_resolve(const struct btf *btf, | |
1154 | u32 *type_id) | |
1155 | { | |
1156 | *type_id = btf->resolved_ids[*type_id]; | |
1157 | return btf_type_by_id(btf, *type_id); | |
1158 | } | |
1159 | ||
1160 | const struct btf_type *btf_type_id_size(const struct btf *btf, | |
1161 | u32 *type_id, u32 *ret_size) | |
1162 | { | |
1163 | const struct btf_type *size_type; | |
1164 | u32 size_type_id = *type_id; | |
1165 | u32 size = 0; | |
1166 | ||
1167 | size_type = btf_type_by_id(btf, size_type_id); | |
b47a0bd2 | 1168 | if (btf_type_nosize_or_null(size_type)) |
eb3f595d MKL |
1169 | return NULL; |
1170 | ||
1171 | if (btf_type_has_size(size_type)) { | |
1172 | size = size_type->size; | |
1173 | } else if (btf_type_is_array(size_type)) { | |
1174 | size = btf->resolved_sizes[size_type_id]; | |
1175 | } else if (btf_type_is_ptr(size_type)) { | |
1176 | size = sizeof(void *); | |
1177 | } else { | |
1dc92851 DB |
1178 | if (WARN_ON_ONCE(!btf_type_is_modifier(size_type) && |
1179 | !btf_type_is_var(size_type))) | |
eb3f595d MKL |
1180 | return NULL; |
1181 | ||
eb3f595d MKL |
1182 | size_type_id = btf->resolved_ids[size_type_id]; |
1183 | size_type = btf_type_by_id(btf, size_type_id); | |
b47a0bd2 | 1184 | if (btf_type_nosize_or_null(size_type)) |
eb3f595d | 1185 | return NULL; |
1acc5d5c AN |
1186 | else if (btf_type_has_size(size_type)) |
1187 | size = size_type->size; | |
1188 | else if (btf_type_is_array(size_type)) | |
1189 | size = btf->resolved_sizes[size_type_id]; | |
1190 | else if (btf_type_is_ptr(size_type)) | |
1191 | size = sizeof(void *); | |
1192 | else | |
1193 | return NULL; | |
eb3f595d MKL |
1194 | } |
1195 | ||
1196 | *type_id = size_type_id; | |
1197 | if (ret_size) | |
1198 | *ret_size = size; | |
1199 | ||
1200 | return size_type; | |
1201 | } | |
1202 | ||
179cde8c MKL |
1203 | static int btf_df_check_member(struct btf_verifier_env *env, |
1204 | const struct btf_type *struct_type, | |
1205 | const struct btf_member *member, | |
1206 | const struct btf_type *member_type) | |
1207 | { | |
1208 | btf_verifier_log_basic(env, struct_type, | |
1209 | "Unsupported check_member"); | |
1210 | return -EINVAL; | |
1211 | } | |
1212 | ||
9d5f9f70 YS |
1213 | static int btf_df_check_kflag_member(struct btf_verifier_env *env, |
1214 | const struct btf_type *struct_type, | |
1215 | const struct btf_member *member, | |
1216 | const struct btf_type *member_type) | |
1217 | { | |
1218 | btf_verifier_log_basic(env, struct_type, | |
1219 | "Unsupported check_kflag_member"); | |
1220 | return -EINVAL; | |
1221 | } | |
1222 | ||
1223 | /* Used for ptr, array and struct/union type members. | |
1224 | * int, enum and modifier types have their specific callback functions. | |
1225 | */ | |
1226 | static int btf_generic_check_kflag_member(struct btf_verifier_env *env, | |
1227 | const struct btf_type *struct_type, | |
1228 | const struct btf_member *member, | |
1229 | const struct btf_type *member_type) | |
1230 | { | |
1231 | if (BTF_MEMBER_BITFIELD_SIZE(member->offset)) { | |
1232 | btf_verifier_log_member(env, struct_type, member, | |
1233 | "Invalid member bitfield_size"); | |
1234 | return -EINVAL; | |
1235 | } | |
1236 | ||
1237 | /* bitfield size is 0, so member->offset represents bit offset only. | |
1238 | * It is safe to call non kflag check_member variants. | |
1239 | */ | |
1240 | return btf_type_ops(member_type)->check_member(env, struct_type, | |
1241 | member, | |
1242 | member_type); | |
1243 | } | |
1244 | ||
eb3f595d MKL |
1245 | static int btf_df_resolve(struct btf_verifier_env *env, |
1246 | const struct resolve_vertex *v) | |
1247 | { | |
1248 | btf_verifier_log_basic(env, v->t, "Unsupported resolve"); | |
1249 | return -EINVAL; | |
1250 | } | |
1251 | ||
b00b8dae MKL |
1252 | static void btf_df_seq_show(const struct btf *btf, const struct btf_type *t, |
1253 | u32 type_id, void *data, u8 bits_offsets, | |
1254 | struct seq_file *m) | |
1255 | { | |
1256 | seq_printf(m, "<unsupported kind:%u>", BTF_INFO_KIND(t->info)); | |
1257 | } | |
1258 | ||
179cde8c MKL |
1259 | static int btf_int_check_member(struct btf_verifier_env *env, |
1260 | const struct btf_type *struct_type, | |
1261 | const struct btf_member *member, | |
1262 | const struct btf_type *member_type) | |
1263 | { | |
1264 | u32 int_data = btf_type_int(member_type); | |
1265 | u32 struct_bits_off = member->offset; | |
1266 | u32 struct_size = struct_type->size; | |
1267 | u32 nr_copy_bits; | |
1268 | u32 bytes_offset; | |
1269 | ||
1270 | if (U32_MAX - struct_bits_off < BTF_INT_OFFSET(int_data)) { | |
1271 | btf_verifier_log_member(env, struct_type, member, | |
1272 | "bits_offset exceeds U32_MAX"); | |
1273 | return -EINVAL; | |
1274 | } | |
1275 | ||
1276 | struct_bits_off += BTF_INT_OFFSET(int_data); | |
1277 | bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off); | |
1278 | nr_copy_bits = BTF_INT_BITS(int_data) + | |
1279 | BITS_PER_BYTE_MASKED(struct_bits_off); | |
1280 | ||
b1e8818c | 1281 | if (nr_copy_bits > BITS_PER_U128) { |
179cde8c | 1282 | btf_verifier_log_member(env, struct_type, member, |
b1e8818c | 1283 | "nr_copy_bits exceeds 128"); |
179cde8c MKL |
1284 | return -EINVAL; |
1285 | } | |
1286 | ||
1287 | if (struct_size < bytes_offset || | |
1288 | struct_size - bytes_offset < BITS_ROUNDUP_BYTES(nr_copy_bits)) { | |
1289 | btf_verifier_log_member(env, struct_type, member, | |
1290 | "Member exceeds struct_size"); | |
1291 | return -EINVAL; | |
1292 | } | |
1293 | ||
1294 | return 0; | |
1295 | } | |
1296 | ||
9d5f9f70 YS |
1297 | static int btf_int_check_kflag_member(struct btf_verifier_env *env, |
1298 | const struct btf_type *struct_type, | |
1299 | const struct btf_member *member, | |
1300 | const struct btf_type *member_type) | |
1301 | { | |
1302 | u32 struct_bits_off, nr_bits, nr_int_data_bits, bytes_offset; | |
1303 | u32 int_data = btf_type_int(member_type); | |
1304 | u32 struct_size = struct_type->size; | |
1305 | u32 nr_copy_bits; | |
1306 | ||
1307 | /* a regular int type is required for the kflag int member */ | |
1308 | if (!btf_type_int_is_regular(member_type)) { | |
1309 | btf_verifier_log_member(env, struct_type, member, | |
1310 | "Invalid member base type"); | |
1311 | return -EINVAL; | |
1312 | } | |
1313 | ||
1314 | /* check sanity of bitfield size */ | |
1315 | nr_bits = BTF_MEMBER_BITFIELD_SIZE(member->offset); | |
1316 | struct_bits_off = BTF_MEMBER_BIT_OFFSET(member->offset); | |
1317 | nr_int_data_bits = BTF_INT_BITS(int_data); | |
1318 | if (!nr_bits) { | |
1319 | /* Not a bitfield member, member offset must be at byte | |
1320 | * boundary. | |
1321 | */ | |
1322 | if (BITS_PER_BYTE_MASKED(struct_bits_off)) { | |
1323 | btf_verifier_log_member(env, struct_type, member, | |
1324 | "Invalid member offset"); | |
1325 | return -EINVAL; | |
1326 | } | |
1327 | ||
1328 | nr_bits = nr_int_data_bits; | |
1329 | } else if (nr_bits > nr_int_data_bits) { | |
1330 | btf_verifier_log_member(env, struct_type, member, | |
1331 | "Invalid member bitfield_size"); | |
1332 | return -EINVAL; | |
1333 | } | |
1334 | ||
1335 | bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off); | |
1336 | nr_copy_bits = nr_bits + BITS_PER_BYTE_MASKED(struct_bits_off); | |
b1e8818c | 1337 | if (nr_copy_bits > BITS_PER_U128) { |
9d5f9f70 | 1338 | btf_verifier_log_member(env, struct_type, member, |
b1e8818c | 1339 | "nr_copy_bits exceeds 128"); |
9d5f9f70 YS |
1340 | return -EINVAL; |
1341 | } | |
1342 | ||
1343 | if (struct_size < bytes_offset || | |
1344 | struct_size - bytes_offset < BITS_ROUNDUP_BYTES(nr_copy_bits)) { | |
1345 | btf_verifier_log_member(env, struct_type, member, | |
1346 | "Member exceeds struct_size"); | |
1347 | return -EINVAL; | |
1348 | } | |
1349 | ||
1350 | return 0; | |
1351 | } | |
1352 | ||
69b693f0 MKL |
1353 | static s32 btf_int_check_meta(struct btf_verifier_env *env, |
1354 | const struct btf_type *t, | |
1355 | u32 meta_left) | |
1356 | { | |
1357 | u32 int_data, nr_bits, meta_needed = sizeof(int_data); | |
1358 | u16 encoding; | |
1359 | ||
1360 | if (meta_left < meta_needed) { | |
1361 | btf_verifier_log_basic(env, t, | |
1362 | "meta_left:%u meta_needed:%u", | |
1363 | meta_left, meta_needed); | |
1364 | return -EINVAL; | |
1365 | } | |
1366 | ||
1367 | if (btf_type_vlen(t)) { | |
1368 | btf_verifier_log_type(env, t, "vlen != 0"); | |
1369 | return -EINVAL; | |
1370 | } | |
1371 | ||
9d5f9f70 YS |
1372 | if (btf_type_kflag(t)) { |
1373 | btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); | |
1374 | return -EINVAL; | |
1375 | } | |
1376 | ||
69b693f0 | 1377 | int_data = btf_type_int(t); |
aea2f7b8 MKL |
1378 | if (int_data & ~BTF_INT_MASK) { |
1379 | btf_verifier_log_basic(env, t, "Invalid int_data:%x", | |
1380 | int_data); | |
1381 | return -EINVAL; | |
1382 | } | |
1383 | ||
69b693f0 MKL |
1384 | nr_bits = BTF_INT_BITS(int_data) + BTF_INT_OFFSET(int_data); |
1385 | ||
b1e8818c | 1386 | if (nr_bits > BITS_PER_U128) { |
69b693f0 | 1387 | btf_verifier_log_type(env, t, "nr_bits exceeds %zu", |
b1e8818c | 1388 | BITS_PER_U128); |
69b693f0 MKL |
1389 | return -EINVAL; |
1390 | } | |
1391 | ||
1392 | if (BITS_ROUNDUP_BYTES(nr_bits) > t->size) { | |
1393 | btf_verifier_log_type(env, t, "nr_bits exceeds type_size"); | |
1394 | return -EINVAL; | |
1395 | } | |
1396 | ||
aea2f7b8 MKL |
1397 | /* |
1398 | * Only one of the encoding bits is allowed and it | |
1399 | * should be sufficient for the pretty print purpose (i.e. decoding). | |
1400 | * Multiple bits can be allowed later if it is found | |
1401 | * to be insufficient. | |
1402 | */ | |
69b693f0 MKL |
1403 | encoding = BTF_INT_ENCODING(int_data); |
1404 | if (encoding && | |
1405 | encoding != BTF_INT_SIGNED && | |
1406 | encoding != BTF_INT_CHAR && | |
aea2f7b8 | 1407 | encoding != BTF_INT_BOOL) { |
69b693f0 MKL |
1408 | btf_verifier_log_type(env, t, "Unsupported encoding"); |
1409 | return -ENOTSUPP; | |
1410 | } | |
1411 | ||
1412 | btf_verifier_log_type(env, t, NULL); | |
1413 | ||
1414 | return meta_needed; | |
1415 | } | |
1416 | ||
1417 | static void btf_int_log(struct btf_verifier_env *env, | |
1418 | const struct btf_type *t) | |
1419 | { | |
1420 | int int_data = btf_type_int(t); | |
1421 | ||
1422 | btf_verifier_log(env, | |
1423 | "size=%u bits_offset=%u nr_bits=%u encoding=%s", | |
1424 | t->size, BTF_INT_OFFSET(int_data), | |
1425 | BTF_INT_BITS(int_data), | |
1426 | btf_int_encoding_str(BTF_INT_ENCODING(int_data))); | |
1427 | } | |
1428 | ||
b1e8818c YS |
1429 | static void btf_int128_print(struct seq_file *m, void *data) |
1430 | { | |
1431 | /* data points to a __int128 number. | |
1432 | * Suppose | |
1433 | * int128_num = *(__int128 *)data; | |
1434 | * The below formulas shows what upper_num and lower_num represents: | |
1435 | * upper_num = int128_num >> 64; | |
1436 | * lower_num = int128_num & 0xffffffffFFFFFFFFULL; | |
1437 | */ | |
1438 | u64 upper_num, lower_num; | |
1439 | ||
1440 | #ifdef __BIG_ENDIAN_BITFIELD | |
1441 | upper_num = *(u64 *)data; | |
1442 | lower_num = *(u64 *)(data + 8); | |
1443 | #else | |
1444 | upper_num = *(u64 *)(data + 8); | |
1445 | lower_num = *(u64 *)data; | |
1446 | #endif | |
1447 | if (upper_num == 0) | |
1448 | seq_printf(m, "0x%llx", lower_num); | |
1449 | else | |
1450 | seq_printf(m, "0x%llx%016llx", upper_num, lower_num); | |
1451 | } | |
1452 | ||
1453 | static void btf_int128_shift(u64 *print_num, u16 left_shift_bits, | |
1454 | u16 right_shift_bits) | |
1455 | { | |
1456 | u64 upper_num, lower_num; | |
1457 | ||
1458 | #ifdef __BIG_ENDIAN_BITFIELD | |
1459 | upper_num = print_num[0]; | |
1460 | lower_num = print_num[1]; | |
1461 | #else | |
1462 | upper_num = print_num[1]; | |
1463 | lower_num = print_num[0]; | |
1464 | #endif | |
1465 | ||
1466 | /* shake out un-needed bits by shift/or operations */ | |
1467 | if (left_shift_bits >= 64) { | |
1468 | upper_num = lower_num << (left_shift_bits - 64); | |
1469 | lower_num = 0; | |
1470 | } else { | |
1471 | upper_num = (upper_num << left_shift_bits) | | |
1472 | (lower_num >> (64 - left_shift_bits)); | |
1473 | lower_num = lower_num << left_shift_bits; | |
1474 | } | |
1475 | ||
1476 | if (right_shift_bits >= 64) { | |
1477 | lower_num = upper_num >> (right_shift_bits - 64); | |
1478 | upper_num = 0; | |
1479 | } else { | |
1480 | lower_num = (lower_num >> right_shift_bits) | | |
1481 | (upper_num << (64 - right_shift_bits)); | |
1482 | upper_num = upper_num >> right_shift_bits; | |
1483 | } | |
1484 | ||
1485 | #ifdef __BIG_ENDIAN_BITFIELD | |
1486 | print_num[0] = upper_num; | |
1487 | print_num[1] = lower_num; | |
1488 | #else | |
1489 | print_num[0] = lower_num; | |
1490 | print_num[1] = upper_num; | |
1491 | #endif | |
1492 | } | |
1493 | ||
f97be3ab YS |
1494 | static void btf_bitfield_seq_show(void *data, u8 bits_offset, |
1495 | u8 nr_bits, struct seq_file *m) | |
b00b8dae | 1496 | { |
b65f370d | 1497 | u16 left_shift_bits, right_shift_bits; |
36fc3c8c MKL |
1498 | u8 nr_copy_bytes; |
1499 | u8 nr_copy_bits; | |
b1e8818c | 1500 | u64 print_num[2] = {}; |
b00b8dae | 1501 | |
b00b8dae MKL |
1502 | nr_copy_bits = nr_bits + bits_offset; |
1503 | nr_copy_bytes = BITS_ROUNDUP_BYTES(nr_copy_bits); | |
1504 | ||
b1e8818c | 1505 | memcpy(print_num, data, nr_copy_bytes); |
b00b8dae | 1506 | |
b65f370d OK |
1507 | #ifdef __BIG_ENDIAN_BITFIELD |
1508 | left_shift_bits = bits_offset; | |
1509 | #else | |
b1e8818c | 1510 | left_shift_bits = BITS_PER_U128 - nr_copy_bits; |
b65f370d | 1511 | #endif |
b1e8818c | 1512 | right_shift_bits = BITS_PER_U128 - nr_bits; |
b00b8dae | 1513 | |
b1e8818c YS |
1514 | btf_int128_shift(print_num, left_shift_bits, right_shift_bits); |
1515 | btf_int128_print(m, print_num); | |
b00b8dae MKL |
1516 | } |
1517 | ||
9d5f9f70 | 1518 | |
f97be3ab YS |
1519 | static void btf_int_bits_seq_show(const struct btf *btf, |
1520 | const struct btf_type *t, | |
1521 | void *data, u8 bits_offset, | |
1522 | struct seq_file *m) | |
1523 | { | |
1524 | u32 int_data = btf_type_int(t); | |
1525 | u8 nr_bits = BTF_INT_BITS(int_data); | |
1526 | u8 total_bits_offset; | |
1527 | ||
1528 | /* | |
1529 | * bits_offset is at most 7. | |
b1e8818c | 1530 | * BTF_INT_OFFSET() cannot exceed 128 bits. |
f97be3ab YS |
1531 | */ |
1532 | total_bits_offset = bits_offset + BTF_INT_OFFSET(int_data); | |
17e3ac81 YS |
1533 | data += BITS_ROUNDDOWN_BYTES(total_bits_offset); |
1534 | bits_offset = BITS_PER_BYTE_MASKED(total_bits_offset); | |
1535 | btf_bitfield_seq_show(data, bits_offset, nr_bits, m); | |
f97be3ab YS |
1536 | } |
1537 | ||
b00b8dae MKL |
1538 | static void btf_int_seq_show(const struct btf *btf, const struct btf_type *t, |
1539 | u32 type_id, void *data, u8 bits_offset, | |
1540 | struct seq_file *m) | |
1541 | { | |
1542 | u32 int_data = btf_type_int(t); | |
1543 | u8 encoding = BTF_INT_ENCODING(int_data); | |
1544 | bool sign = encoding & BTF_INT_SIGNED; | |
36fc3c8c | 1545 | u8 nr_bits = BTF_INT_BITS(int_data); |
b00b8dae MKL |
1546 | |
1547 | if (bits_offset || BTF_INT_OFFSET(int_data) || | |
1548 | BITS_PER_BYTE_MASKED(nr_bits)) { | |
1549 | btf_int_bits_seq_show(btf, t, data, bits_offset, m); | |
1550 | return; | |
1551 | } | |
1552 | ||
1553 | switch (nr_bits) { | |
b1e8818c YS |
1554 | case 128: |
1555 | btf_int128_print(m, data); | |
1556 | break; | |
b00b8dae MKL |
1557 | case 64: |
1558 | if (sign) | |
1559 | seq_printf(m, "%lld", *(s64 *)data); | |
1560 | else | |
1561 | seq_printf(m, "%llu", *(u64 *)data); | |
1562 | break; | |
1563 | case 32: | |
1564 | if (sign) | |
1565 | seq_printf(m, "%d", *(s32 *)data); | |
1566 | else | |
1567 | seq_printf(m, "%u", *(u32 *)data); | |
1568 | break; | |
1569 | case 16: | |
1570 | if (sign) | |
1571 | seq_printf(m, "%d", *(s16 *)data); | |
1572 | else | |
1573 | seq_printf(m, "%u", *(u16 *)data); | |
1574 | break; | |
1575 | case 8: | |
1576 | if (sign) | |
1577 | seq_printf(m, "%d", *(s8 *)data); | |
1578 | else | |
1579 | seq_printf(m, "%u", *(u8 *)data); | |
1580 | break; | |
1581 | default: | |
1582 | btf_int_bits_seq_show(btf, t, data, bits_offset, m); | |
1583 | } | |
1584 | } | |
1585 | ||
69b693f0 MKL |
1586 | static const struct btf_kind_operations int_ops = { |
1587 | .check_meta = btf_int_check_meta, | |
eb3f595d | 1588 | .resolve = btf_df_resolve, |
179cde8c | 1589 | .check_member = btf_int_check_member, |
9d5f9f70 | 1590 | .check_kflag_member = btf_int_check_kflag_member, |
69b693f0 | 1591 | .log_details = btf_int_log, |
b00b8dae | 1592 | .seq_show = btf_int_seq_show, |
69b693f0 MKL |
1593 | }; |
1594 | ||
179cde8c MKL |
1595 | static int btf_modifier_check_member(struct btf_verifier_env *env, |
1596 | const struct btf_type *struct_type, | |
1597 | const struct btf_member *member, | |
1598 | const struct btf_type *member_type) | |
1599 | { | |
1600 | const struct btf_type *resolved_type; | |
1601 | u32 resolved_type_id = member->type; | |
1602 | struct btf_member resolved_member; | |
1603 | struct btf *btf = env->btf; | |
1604 | ||
1605 | resolved_type = btf_type_id_size(btf, &resolved_type_id, NULL); | |
1606 | if (!resolved_type) { | |
1607 | btf_verifier_log_member(env, struct_type, member, | |
1608 | "Invalid member"); | |
1609 | return -EINVAL; | |
1610 | } | |
1611 | ||
1612 | resolved_member = *member; | |
1613 | resolved_member.type = resolved_type_id; | |
1614 | ||
1615 | return btf_type_ops(resolved_type)->check_member(env, struct_type, | |
1616 | &resolved_member, | |
1617 | resolved_type); | |
1618 | } | |
1619 | ||
9d5f9f70 YS |
1620 | static int btf_modifier_check_kflag_member(struct btf_verifier_env *env, |
1621 | const struct btf_type *struct_type, | |
1622 | const struct btf_member *member, | |
1623 | const struct btf_type *member_type) | |
1624 | { | |
1625 | const struct btf_type *resolved_type; | |
1626 | u32 resolved_type_id = member->type; | |
1627 | struct btf_member resolved_member; | |
1628 | struct btf *btf = env->btf; | |
1629 | ||
1630 | resolved_type = btf_type_id_size(btf, &resolved_type_id, NULL); | |
1631 | if (!resolved_type) { | |
1632 | btf_verifier_log_member(env, struct_type, member, | |
1633 | "Invalid member"); | |
1634 | return -EINVAL; | |
1635 | } | |
1636 | ||
1637 | resolved_member = *member; | |
1638 | resolved_member.type = resolved_type_id; | |
1639 | ||
1640 | return btf_type_ops(resolved_type)->check_kflag_member(env, struct_type, | |
1641 | &resolved_member, | |
1642 | resolved_type); | |
1643 | } | |
1644 | ||
179cde8c MKL |
1645 | static int btf_ptr_check_member(struct btf_verifier_env *env, |
1646 | const struct btf_type *struct_type, | |
1647 | const struct btf_member *member, | |
1648 | const struct btf_type *member_type) | |
1649 | { | |
1650 | u32 struct_size, struct_bits_off, bytes_offset; | |
1651 | ||
1652 | struct_size = struct_type->size; | |
1653 | struct_bits_off = member->offset; | |
1654 | bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off); | |
1655 | ||
1656 | if (BITS_PER_BYTE_MASKED(struct_bits_off)) { | |
1657 | btf_verifier_log_member(env, struct_type, member, | |
1658 | "Member is not byte aligned"); | |
1659 | return -EINVAL; | |
1660 | } | |
1661 | ||
1662 | if (struct_size - bytes_offset < sizeof(void *)) { | |
1663 | btf_verifier_log_member(env, struct_type, member, | |
1664 | "Member exceeds struct_size"); | |
1665 | return -EINVAL; | |
1666 | } | |
1667 | ||
1668 | return 0; | |
1669 | } | |
1670 | ||
69b693f0 MKL |
1671 | static int btf_ref_type_check_meta(struct btf_verifier_env *env, |
1672 | const struct btf_type *t, | |
1673 | u32 meta_left) | |
1674 | { | |
1675 | if (btf_type_vlen(t)) { | |
1676 | btf_verifier_log_type(env, t, "vlen != 0"); | |
1677 | return -EINVAL; | |
1678 | } | |
1679 | ||
9d5f9f70 YS |
1680 | if (btf_type_kflag(t)) { |
1681 | btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); | |
1682 | return -EINVAL; | |
1683 | } | |
1684 | ||
aea2f7b8 | 1685 | if (!BTF_TYPE_ID_VALID(t->type)) { |
69b693f0 MKL |
1686 | btf_verifier_log_type(env, t, "Invalid type_id"); |
1687 | return -EINVAL; | |
1688 | } | |
1689 | ||
eb04bbb6 YS |
1690 | /* typedef type must have a valid name, and other ref types, |
1691 | * volatile, const, restrict, should have a null name. | |
1692 | */ | |
1693 | if (BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF) { | |
1694 | if (!t->name_off || | |
1695 | !btf_name_valid_identifier(env->btf, t->name_off)) { | |
1696 | btf_verifier_log_type(env, t, "Invalid name"); | |
1697 | return -EINVAL; | |
1698 | } | |
1699 | } else { | |
1700 | if (t->name_off) { | |
1701 | btf_verifier_log_type(env, t, "Invalid name"); | |
1702 | return -EINVAL; | |
1703 | } | |
1704 | } | |
1705 | ||
69b693f0 MKL |
1706 | btf_verifier_log_type(env, t, NULL); |
1707 | ||
1708 | return 0; | |
1709 | } | |
1710 | ||
eb3f595d MKL |
1711 | static int btf_modifier_resolve(struct btf_verifier_env *env, |
1712 | const struct resolve_vertex *v) | |
1713 | { | |
1714 | const struct btf_type *t = v->t; | |
1715 | const struct btf_type *next_type; | |
1716 | u32 next_type_id = t->type; | |
1717 | struct btf *btf = env->btf; | |
eb3f595d MKL |
1718 | |
1719 | next_type = btf_type_by_id(btf, next_type_id); | |
1dc92851 | 1720 | if (!next_type || btf_type_is_resolve_source_only(next_type)) { |
eb3f595d MKL |
1721 | btf_verifier_log_type(env, v->t, "Invalid type_id"); |
1722 | return -EINVAL; | |
1723 | } | |
1724 | ||
eb3f595d MKL |
1725 | if (!env_type_is_resolve_sink(env, next_type) && |
1726 | !env_type_is_resolved(env, next_type_id)) | |
1727 | return env_stack_push(env, next_type, next_type_id); | |
1728 | ||
1729 | /* Figure out the resolved next_type_id with size. | |
1730 | * They will be stored in the current modifier's | |
1731 | * resolved_ids and resolved_sizes such that it can | |
1732 | * save us a few type-following when we use it later (e.g. in | |
1733 | * pretty print). | |
1734 | */ | |
1acc5d5c | 1735 | if (!btf_type_id_size(btf, &next_type_id, NULL)) { |
2667a262 MKL |
1736 | if (env_type_is_resolved(env, next_type_id)) |
1737 | next_type = btf_type_id_resolve(btf, &next_type_id); | |
1738 | ||
1739 | /* "typedef void new_void", "const void"...etc */ | |
1740 | if (!btf_type_is_void(next_type) && | |
81f5c6f5 YS |
1741 | !btf_type_is_fwd(next_type) && |
1742 | !btf_type_is_func_proto(next_type)) { | |
2667a262 MKL |
1743 | btf_verifier_log_type(env, v->t, "Invalid type_id"); |
1744 | return -EINVAL; | |
1745 | } | |
eb3f595d MKL |
1746 | } |
1747 | ||
1acc5d5c | 1748 | env_stack_pop_resolved(env, next_type_id, 0); |
eb3f595d MKL |
1749 | |
1750 | return 0; | |
1751 | } | |
1752 | ||
1dc92851 DB |
1753 | static int btf_var_resolve(struct btf_verifier_env *env, |
1754 | const struct resolve_vertex *v) | |
1755 | { | |
1756 | const struct btf_type *next_type; | |
1757 | const struct btf_type *t = v->t; | |
1758 | u32 next_type_id = t->type; | |
1759 | struct btf *btf = env->btf; | |
1dc92851 DB |
1760 | |
1761 | next_type = btf_type_by_id(btf, next_type_id); | |
1762 | if (!next_type || btf_type_is_resolve_source_only(next_type)) { | |
1763 | btf_verifier_log_type(env, v->t, "Invalid type_id"); | |
1764 | return -EINVAL; | |
1765 | } | |
1766 | ||
1767 | if (!env_type_is_resolve_sink(env, next_type) && | |
1768 | !env_type_is_resolved(env, next_type_id)) | |
1769 | return env_stack_push(env, next_type, next_type_id); | |
1770 | ||
1771 | if (btf_type_is_modifier(next_type)) { | |
1772 | const struct btf_type *resolved_type; | |
1773 | u32 resolved_type_id; | |
1774 | ||
1775 | resolved_type_id = next_type_id; | |
1776 | resolved_type = btf_type_id_resolve(btf, &resolved_type_id); | |
1777 | ||
1778 | if (btf_type_is_ptr(resolved_type) && | |
1779 | !env_type_is_resolve_sink(env, resolved_type) && | |
1780 | !env_type_is_resolved(env, resolved_type_id)) | |
1781 | return env_stack_push(env, resolved_type, | |
1782 | resolved_type_id); | |
1783 | } | |
1784 | ||
1785 | /* We must resolve to something concrete at this point, no | |
1786 | * forward types or similar that would resolve to size of | |
1787 | * zero is allowed. | |
1788 | */ | |
1acc5d5c | 1789 | if (!btf_type_id_size(btf, &next_type_id, NULL)) { |
1dc92851 DB |
1790 | btf_verifier_log_type(env, v->t, "Invalid type_id"); |
1791 | return -EINVAL; | |
1792 | } | |
1793 | ||
1acc5d5c | 1794 | env_stack_pop_resolved(env, next_type_id, 0); |
1dc92851 DB |
1795 | |
1796 | return 0; | |
1797 | } | |
1798 | ||
eb3f595d MKL |
1799 | static int btf_ptr_resolve(struct btf_verifier_env *env, |
1800 | const struct resolve_vertex *v) | |
1801 | { | |
1802 | const struct btf_type *next_type; | |
1803 | const struct btf_type *t = v->t; | |
1804 | u32 next_type_id = t->type; | |
1805 | struct btf *btf = env->btf; | |
eb3f595d MKL |
1806 | |
1807 | next_type = btf_type_by_id(btf, next_type_id); | |
1dc92851 | 1808 | if (!next_type || btf_type_is_resolve_source_only(next_type)) { |
eb3f595d MKL |
1809 | btf_verifier_log_type(env, v->t, "Invalid type_id"); |
1810 | return -EINVAL; | |
1811 | } | |
1812 | ||
eb3f595d MKL |
1813 | if (!env_type_is_resolve_sink(env, next_type) && |
1814 | !env_type_is_resolved(env, next_type_id)) | |
1815 | return env_stack_push(env, next_type, next_type_id); | |
1816 | ||
1817 | /* If the modifier was RESOLVED during RESOLVE_STRUCT_OR_ARRAY, | |
1818 | * the modifier may have stopped resolving when it was resolved | |
1819 | * to a ptr (last-resolved-ptr). | |
1820 | * | |
1821 | * We now need to continue from the last-resolved-ptr to | |
1822 | * ensure the last-resolved-ptr will not referring back to | |
1823 | * the currenct ptr (t). | |
1824 | */ | |
1825 | if (btf_type_is_modifier(next_type)) { | |
1826 | const struct btf_type *resolved_type; | |
1827 | u32 resolved_type_id; | |
1828 | ||
1829 | resolved_type_id = next_type_id; | |
1830 | resolved_type = btf_type_id_resolve(btf, &resolved_type_id); | |
1831 | ||
1832 | if (btf_type_is_ptr(resolved_type) && | |
1833 | !env_type_is_resolve_sink(env, resolved_type) && | |
1834 | !env_type_is_resolved(env, resolved_type_id)) | |
1835 | return env_stack_push(env, resolved_type, | |
1836 | resolved_type_id); | |
1837 | } | |
1838 | ||
2667a262 MKL |
1839 | if (!btf_type_id_size(btf, &next_type_id, NULL)) { |
1840 | if (env_type_is_resolved(env, next_type_id)) | |
1841 | next_type = btf_type_id_resolve(btf, &next_type_id); | |
1842 | ||
1843 | if (!btf_type_is_void(next_type) && | |
1844 | !btf_type_is_fwd(next_type) && | |
1845 | !btf_type_is_func_proto(next_type)) { | |
1846 | btf_verifier_log_type(env, v->t, "Invalid type_id"); | |
1847 | return -EINVAL; | |
1848 | } | |
eb3f595d MKL |
1849 | } |
1850 | ||
eb3f595d MKL |
1851 | env_stack_pop_resolved(env, next_type_id, 0); |
1852 | ||
1853 | return 0; | |
1854 | } | |
1855 | ||
b00b8dae MKL |
1856 | static void btf_modifier_seq_show(const struct btf *btf, |
1857 | const struct btf_type *t, | |
1858 | u32 type_id, void *data, | |
1859 | u8 bits_offset, struct seq_file *m) | |
1860 | { | |
85d33df3 MKL |
1861 | if (btf->resolved_ids) |
1862 | t = btf_type_id_resolve(btf, &type_id); | |
1863 | else | |
1864 | t = btf_type_skip_modifiers(btf, type_id, NULL); | |
b00b8dae MKL |
1865 | |
1866 | btf_type_ops(t)->seq_show(btf, t, type_id, data, bits_offset, m); | |
1867 | } | |
1868 | ||
1dc92851 DB |
1869 | static void btf_var_seq_show(const struct btf *btf, const struct btf_type *t, |
1870 | u32 type_id, void *data, u8 bits_offset, | |
1871 | struct seq_file *m) | |
1872 | { | |
1873 | t = btf_type_id_resolve(btf, &type_id); | |
1874 | ||
1875 | btf_type_ops(t)->seq_show(btf, t, type_id, data, bits_offset, m); | |
1876 | } | |
1877 | ||
b00b8dae MKL |
1878 | static void btf_ptr_seq_show(const struct btf *btf, const struct btf_type *t, |
1879 | u32 type_id, void *data, u8 bits_offset, | |
1880 | struct seq_file *m) | |
1881 | { | |
1882 | /* It is a hashed value */ | |
1883 | seq_printf(m, "%p", *(void **)data); | |
1884 | } | |
1885 | ||
69b693f0 MKL |
1886 | static void btf_ref_type_log(struct btf_verifier_env *env, |
1887 | const struct btf_type *t) | |
1888 | { | |
1889 | btf_verifier_log(env, "type_id=%u", t->type); | |
1890 | } | |
1891 | ||
1892 | static struct btf_kind_operations modifier_ops = { | |
1893 | .check_meta = btf_ref_type_check_meta, | |
eb3f595d | 1894 | .resolve = btf_modifier_resolve, |
179cde8c | 1895 | .check_member = btf_modifier_check_member, |
9d5f9f70 | 1896 | .check_kflag_member = btf_modifier_check_kflag_member, |
69b693f0 | 1897 | .log_details = btf_ref_type_log, |
b00b8dae | 1898 | .seq_show = btf_modifier_seq_show, |
69b693f0 MKL |
1899 | }; |
1900 | ||
1901 | static struct btf_kind_operations ptr_ops = { | |
1902 | .check_meta = btf_ref_type_check_meta, | |
eb3f595d | 1903 | .resolve = btf_ptr_resolve, |
179cde8c | 1904 | .check_member = btf_ptr_check_member, |
9d5f9f70 | 1905 | .check_kflag_member = btf_generic_check_kflag_member, |
69b693f0 | 1906 | .log_details = btf_ref_type_log, |
b00b8dae | 1907 | .seq_show = btf_ptr_seq_show, |
69b693f0 MKL |
1908 | }; |
1909 | ||
8175383f MKL |
1910 | static s32 btf_fwd_check_meta(struct btf_verifier_env *env, |
1911 | const struct btf_type *t, | |
1912 | u32 meta_left) | |
1913 | { | |
1914 | if (btf_type_vlen(t)) { | |
1915 | btf_verifier_log_type(env, t, "vlen != 0"); | |
1916 | return -EINVAL; | |
1917 | } | |
1918 | ||
1919 | if (t->type) { | |
1920 | btf_verifier_log_type(env, t, "type != 0"); | |
1921 | return -EINVAL; | |
1922 | } | |
1923 | ||
eb04bbb6 YS |
1924 | /* fwd type must have a valid name */ |
1925 | if (!t->name_off || | |
1926 | !btf_name_valid_identifier(env->btf, t->name_off)) { | |
1927 | btf_verifier_log_type(env, t, "Invalid name"); | |
1928 | return -EINVAL; | |
1929 | } | |
1930 | ||
8175383f MKL |
1931 | btf_verifier_log_type(env, t, NULL); |
1932 | ||
1933 | return 0; | |
1934 | } | |
1935 | ||
76c43ae8 YS |
1936 | static void btf_fwd_type_log(struct btf_verifier_env *env, |
1937 | const struct btf_type *t) | |
1938 | { | |
1939 | btf_verifier_log(env, "%s", btf_type_kflag(t) ? "union" : "struct"); | |
1940 | } | |
1941 | ||
69b693f0 | 1942 | static struct btf_kind_operations fwd_ops = { |
8175383f | 1943 | .check_meta = btf_fwd_check_meta, |
eb3f595d | 1944 | .resolve = btf_df_resolve, |
179cde8c | 1945 | .check_member = btf_df_check_member, |
9d5f9f70 | 1946 | .check_kflag_member = btf_df_check_kflag_member, |
76c43ae8 | 1947 | .log_details = btf_fwd_type_log, |
b00b8dae | 1948 | .seq_show = btf_df_seq_show, |
69b693f0 MKL |
1949 | }; |
1950 | ||
179cde8c MKL |
1951 | static int btf_array_check_member(struct btf_verifier_env *env, |
1952 | const struct btf_type *struct_type, | |
1953 | const struct btf_member *member, | |
1954 | const struct btf_type *member_type) | |
1955 | { | |
1956 | u32 struct_bits_off = member->offset; | |
1957 | u32 struct_size, bytes_offset; | |
1958 | u32 array_type_id, array_size; | |
1959 | struct btf *btf = env->btf; | |
1960 | ||
1961 | if (BITS_PER_BYTE_MASKED(struct_bits_off)) { | |
1962 | btf_verifier_log_member(env, struct_type, member, | |
1963 | "Member is not byte aligned"); | |
1964 | return -EINVAL; | |
1965 | } | |
1966 | ||
1967 | array_type_id = member->type; | |
1968 | btf_type_id_size(btf, &array_type_id, &array_size); | |
1969 | struct_size = struct_type->size; | |
1970 | bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off); | |
1971 | if (struct_size - bytes_offset < array_size) { | |
1972 | btf_verifier_log_member(env, struct_type, member, | |
1973 | "Member exceeds struct_size"); | |
1974 | return -EINVAL; | |
1975 | } | |
1976 | ||
1977 | return 0; | |
1978 | } | |
1979 | ||
69b693f0 MKL |
1980 | static s32 btf_array_check_meta(struct btf_verifier_env *env, |
1981 | const struct btf_type *t, | |
1982 | u32 meta_left) | |
1983 | { | |
1984 | const struct btf_array *array = btf_type_array(t); | |
1985 | u32 meta_needed = sizeof(*array); | |
1986 | ||
1987 | if (meta_left < meta_needed) { | |
1988 | btf_verifier_log_basic(env, t, | |
1989 | "meta_left:%u meta_needed:%u", | |
1990 | meta_left, meta_needed); | |
1991 | return -EINVAL; | |
1992 | } | |
1993 | ||
eb04bbb6 YS |
1994 | /* array type should not have a name */ |
1995 | if (t->name_off) { | |
1996 | btf_verifier_log_type(env, t, "Invalid name"); | |
1997 | return -EINVAL; | |
1998 | } | |
1999 | ||
69b693f0 MKL |
2000 | if (btf_type_vlen(t)) { |
2001 | btf_verifier_log_type(env, t, "vlen != 0"); | |
2002 | return -EINVAL; | |
2003 | } | |
2004 | ||
9d5f9f70 YS |
2005 | if (btf_type_kflag(t)) { |
2006 | btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); | |
2007 | return -EINVAL; | |
2008 | } | |
2009 | ||
b9308ae6 MKL |
2010 | if (t->size) { |
2011 | btf_verifier_log_type(env, t, "size != 0"); | |
2012 | return -EINVAL; | |
2013 | } | |
2014 | ||
4ef5f574 MKL |
2015 | /* Array elem type and index type cannot be in type void, |
2016 | * so !array->type and !array->index_type are not allowed. | |
69b693f0 | 2017 | */ |
aea2f7b8 | 2018 | if (!array->type || !BTF_TYPE_ID_VALID(array->type)) { |
4ef5f574 MKL |
2019 | btf_verifier_log_type(env, t, "Invalid elem"); |
2020 | return -EINVAL; | |
2021 | } | |
2022 | ||
aea2f7b8 | 2023 | if (!array->index_type || !BTF_TYPE_ID_VALID(array->index_type)) { |
4ef5f574 | 2024 | btf_verifier_log_type(env, t, "Invalid index"); |
69b693f0 MKL |
2025 | return -EINVAL; |
2026 | } | |
2027 | ||
2028 | btf_verifier_log_type(env, t, NULL); | |
2029 | ||
2030 | return meta_needed; | |
2031 | } | |
2032 | ||
eb3f595d MKL |
2033 | static int btf_array_resolve(struct btf_verifier_env *env, |
2034 | const struct resolve_vertex *v) | |
2035 | { | |
2036 | const struct btf_array *array = btf_type_array(v->t); | |
4ef5f574 MKL |
2037 | const struct btf_type *elem_type, *index_type; |
2038 | u32 elem_type_id, index_type_id; | |
eb3f595d MKL |
2039 | struct btf *btf = env->btf; |
2040 | u32 elem_size; | |
2041 | ||
4ef5f574 MKL |
2042 | /* Check array->index_type */ |
2043 | index_type_id = array->index_type; | |
2044 | index_type = btf_type_by_id(btf, index_type_id); | |
e4f07120 SF |
2045 | if (btf_type_nosize_or_null(index_type) || |
2046 | btf_type_is_resolve_source_only(index_type)) { | |
4ef5f574 MKL |
2047 | btf_verifier_log_type(env, v->t, "Invalid index"); |
2048 | return -EINVAL; | |
2049 | } | |
2050 | ||
2051 | if (!env_type_is_resolve_sink(env, index_type) && | |
2052 | !env_type_is_resolved(env, index_type_id)) | |
2053 | return env_stack_push(env, index_type, index_type_id); | |
2054 | ||
2055 | index_type = btf_type_id_size(btf, &index_type_id, NULL); | |
2056 | if (!index_type || !btf_type_is_int(index_type) || | |
2057 | !btf_type_int_is_regular(index_type)) { | |
2058 | btf_verifier_log_type(env, v->t, "Invalid index"); | |
2059 | return -EINVAL; | |
2060 | } | |
2061 | ||
2062 | /* Check array->type */ | |
2063 | elem_type_id = array->type; | |
eb3f595d | 2064 | elem_type = btf_type_by_id(btf, elem_type_id); |
e4f07120 SF |
2065 | if (btf_type_nosize_or_null(elem_type) || |
2066 | btf_type_is_resolve_source_only(elem_type)) { | |
eb3f595d MKL |
2067 | btf_verifier_log_type(env, v->t, |
2068 | "Invalid elem"); | |
2069 | return -EINVAL; | |
2070 | } | |
2071 | ||
2072 | if (!env_type_is_resolve_sink(env, elem_type) && | |
2073 | !env_type_is_resolved(env, elem_type_id)) | |
2074 | return env_stack_push(env, elem_type, elem_type_id); | |
2075 | ||
2076 | elem_type = btf_type_id_size(btf, &elem_type_id, &elem_size); | |
2077 | if (!elem_type) { | |
2078 | btf_verifier_log_type(env, v->t, "Invalid elem"); | |
2079 | return -EINVAL; | |
2080 | } | |
2081 | ||
4ef5f574 MKL |
2082 | if (btf_type_is_int(elem_type) && !btf_type_int_is_regular(elem_type)) { |
2083 | btf_verifier_log_type(env, v->t, "Invalid array of int"); | |
2084 | return -EINVAL; | |
eb3f595d MKL |
2085 | } |
2086 | ||
2087 | if (array->nelems && elem_size > U32_MAX / array->nelems) { | |
2088 | btf_verifier_log_type(env, v->t, | |
2089 | "Array size overflows U32_MAX"); | |
2090 | return -EINVAL; | |
2091 | } | |
2092 | ||
2093 | env_stack_pop_resolved(env, elem_type_id, elem_size * array->nelems); | |
2094 | ||
2095 | return 0; | |
2096 | } | |
2097 | ||
69b693f0 MKL |
2098 | static void btf_array_log(struct btf_verifier_env *env, |
2099 | const struct btf_type *t) | |
2100 | { | |
2101 | const struct btf_array *array = btf_type_array(t); | |
2102 | ||
2103 | btf_verifier_log(env, "type_id=%u index_type_id=%u nr_elems=%u", | |
2104 | array->type, array->index_type, array->nelems); | |
2105 | } | |
2106 | ||
b00b8dae MKL |
2107 | static void btf_array_seq_show(const struct btf *btf, const struct btf_type *t, |
2108 | u32 type_id, void *data, u8 bits_offset, | |
2109 | struct seq_file *m) | |
2110 | { | |
2111 | const struct btf_array *array = btf_type_array(t); | |
2112 | const struct btf_kind_operations *elem_ops; | |
2113 | const struct btf_type *elem_type; | |
2114 | u32 i, elem_size, elem_type_id; | |
2115 | ||
2116 | elem_type_id = array->type; | |
2117 | elem_type = btf_type_id_size(btf, &elem_type_id, &elem_size); | |
2118 | elem_ops = btf_type_ops(elem_type); | |
2119 | seq_puts(m, "["); | |
2120 | for (i = 0; i < array->nelems; i++) { | |
2121 | if (i) | |
2122 | seq_puts(m, ","); | |
2123 | ||
2124 | elem_ops->seq_show(btf, elem_type, elem_type_id, data, | |
2125 | bits_offset, m); | |
2126 | data += elem_size; | |
2127 | } | |
2128 | seq_puts(m, "]"); | |
2129 | } | |
2130 | ||
69b693f0 MKL |
2131 | static struct btf_kind_operations array_ops = { |
2132 | .check_meta = btf_array_check_meta, | |
eb3f595d | 2133 | .resolve = btf_array_resolve, |
179cde8c | 2134 | .check_member = btf_array_check_member, |
9d5f9f70 | 2135 | .check_kflag_member = btf_generic_check_kflag_member, |
69b693f0 | 2136 | .log_details = btf_array_log, |
b00b8dae | 2137 | .seq_show = btf_array_seq_show, |
69b693f0 MKL |
2138 | }; |
2139 | ||
179cde8c MKL |
2140 | static int btf_struct_check_member(struct btf_verifier_env *env, |
2141 | const struct btf_type *struct_type, | |
2142 | const struct btf_member *member, | |
2143 | const struct btf_type *member_type) | |
2144 | { | |
2145 | u32 struct_bits_off = member->offset; | |
2146 | u32 struct_size, bytes_offset; | |
2147 | ||
2148 | if (BITS_PER_BYTE_MASKED(struct_bits_off)) { | |
2149 | btf_verifier_log_member(env, struct_type, member, | |
2150 | "Member is not byte aligned"); | |
2151 | return -EINVAL; | |
2152 | } | |
2153 | ||
2154 | struct_size = struct_type->size; | |
2155 | bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off); | |
2156 | if (struct_size - bytes_offset < member_type->size) { | |
2157 | btf_verifier_log_member(env, struct_type, member, | |
2158 | "Member exceeds struct_size"); | |
2159 | return -EINVAL; | |
2160 | } | |
2161 | ||
2162 | return 0; | |
2163 | } | |
2164 | ||
69b693f0 MKL |
2165 | static s32 btf_struct_check_meta(struct btf_verifier_env *env, |
2166 | const struct btf_type *t, | |
2167 | u32 meta_left) | |
2168 | { | |
2169 | bool is_union = BTF_INFO_KIND(t->info) == BTF_KIND_UNION; | |
2170 | const struct btf_member *member; | |
6283fa38 | 2171 | u32 meta_needed, last_offset; |
69b693f0 MKL |
2172 | struct btf *btf = env->btf; |
2173 | u32 struct_size = t->size; | |
9d5f9f70 | 2174 | u32 offset; |
69b693f0 MKL |
2175 | u16 i; |
2176 | ||
2177 | meta_needed = btf_type_vlen(t) * sizeof(*member); | |
2178 | if (meta_left < meta_needed) { | |
2179 | btf_verifier_log_basic(env, t, | |
2180 | "meta_left:%u meta_needed:%u", | |
2181 | meta_left, meta_needed); | |
2182 | return -EINVAL; | |
2183 | } | |
2184 | ||
eb04bbb6 YS |
2185 | /* struct type either no name or a valid one */ |
2186 | if (t->name_off && | |
2187 | !btf_name_valid_identifier(env->btf, t->name_off)) { | |
2188 | btf_verifier_log_type(env, t, "Invalid name"); | |
2189 | return -EINVAL; | |
2190 | } | |
2191 | ||
69b693f0 MKL |
2192 | btf_verifier_log_type(env, t, NULL); |
2193 | ||
6283fa38 | 2194 | last_offset = 0; |
69b693f0 | 2195 | for_each_member(i, t, member) { |
fbcf93eb | 2196 | if (!btf_name_offset_valid(btf, member->name_off)) { |
69b693f0 MKL |
2197 | btf_verifier_log_member(env, t, member, |
2198 | "Invalid member name_offset:%u", | |
fbcf93eb | 2199 | member->name_off); |
69b693f0 MKL |
2200 | return -EINVAL; |
2201 | } | |
2202 | ||
eb04bbb6 YS |
2203 | /* struct member either no name or a valid one */ |
2204 | if (member->name_off && | |
2205 | !btf_name_valid_identifier(btf, member->name_off)) { | |
2206 | btf_verifier_log_member(env, t, member, "Invalid name"); | |
2207 | return -EINVAL; | |
2208 | } | |
69b693f0 | 2209 | /* A member cannot be in type void */ |
aea2f7b8 | 2210 | if (!member->type || !BTF_TYPE_ID_VALID(member->type)) { |
69b693f0 MKL |
2211 | btf_verifier_log_member(env, t, member, |
2212 | "Invalid type_id"); | |
2213 | return -EINVAL; | |
2214 | } | |
2215 | ||
9d5f9f70 YS |
2216 | offset = btf_member_bit_offset(t, member); |
2217 | if (is_union && offset) { | |
69b693f0 MKL |
2218 | btf_verifier_log_member(env, t, member, |
2219 | "Invalid member bits_offset"); | |
2220 | return -EINVAL; | |
2221 | } | |
2222 | ||
6283fa38 MKL |
2223 | /* |
2224 | * ">" instead of ">=" because the last member could be | |
2225 | * "char a[0];" | |
2226 | */ | |
9d5f9f70 | 2227 | if (last_offset > offset) { |
6283fa38 MKL |
2228 | btf_verifier_log_member(env, t, member, |
2229 | "Invalid member bits_offset"); | |
2230 | return -EINVAL; | |
2231 | } | |
2232 | ||
9d5f9f70 | 2233 | if (BITS_ROUNDUP_BYTES(offset) > struct_size) { |
69b693f0 | 2234 | btf_verifier_log_member(env, t, member, |
311fe1a8 | 2235 | "Member bits_offset exceeds its struct size"); |
69b693f0 MKL |
2236 | return -EINVAL; |
2237 | } | |
2238 | ||
2239 | btf_verifier_log_member(env, t, member, NULL); | |
9d5f9f70 | 2240 | last_offset = offset; |
69b693f0 MKL |
2241 | } |
2242 | ||
2243 | return meta_needed; | |
2244 | } | |
2245 | ||
eb3f595d MKL |
2246 | static int btf_struct_resolve(struct btf_verifier_env *env, |
2247 | const struct resolve_vertex *v) | |
2248 | { | |
2249 | const struct btf_member *member; | |
179cde8c | 2250 | int err; |
eb3f595d MKL |
2251 | u16 i; |
2252 | ||
2253 | /* Before continue resolving the next_member, | |
2254 | * ensure the last member is indeed resolved to a | |
2255 | * type with size info. | |
2256 | */ | |
2257 | if (v->next_member) { | |
179cde8c | 2258 | const struct btf_type *last_member_type; |
eb3f595d MKL |
2259 | const struct btf_member *last_member; |
2260 | u16 last_member_type_id; | |
2261 | ||
2262 | last_member = btf_type_member(v->t) + v->next_member - 1; | |
2263 | last_member_type_id = last_member->type; | |
2264 | if (WARN_ON_ONCE(!env_type_is_resolved(env, | |
2265 | last_member_type_id))) | |
2266 | return -EINVAL; | |
179cde8c MKL |
2267 | |
2268 | last_member_type = btf_type_by_id(env->btf, | |
2269 | last_member_type_id); | |
9d5f9f70 YS |
2270 | if (btf_type_kflag(v->t)) |
2271 | err = btf_type_ops(last_member_type)->check_kflag_member(env, v->t, | |
2272 | last_member, | |
2273 | last_member_type); | |
2274 | else | |
2275 | err = btf_type_ops(last_member_type)->check_member(env, v->t, | |
2276 | last_member, | |
2277 | last_member_type); | |
179cde8c MKL |
2278 | if (err) |
2279 | return err; | |
eb3f595d MKL |
2280 | } |
2281 | ||
2282 | for_each_member_from(i, v->next_member, v->t, member) { | |
2283 | u32 member_type_id = member->type; | |
2284 | const struct btf_type *member_type = btf_type_by_id(env->btf, | |
2285 | member_type_id); | |
2286 | ||
e4f07120 SF |
2287 | if (btf_type_nosize_or_null(member_type) || |
2288 | btf_type_is_resolve_source_only(member_type)) { | |
eb3f595d MKL |
2289 | btf_verifier_log_member(env, v->t, member, |
2290 | "Invalid member"); | |
2291 | return -EINVAL; | |
2292 | } | |
2293 | ||
2294 | if (!env_type_is_resolve_sink(env, member_type) && | |
2295 | !env_type_is_resolved(env, member_type_id)) { | |
2296 | env_stack_set_next_member(env, i + 1); | |
2297 | return env_stack_push(env, member_type, member_type_id); | |
2298 | } | |
179cde8c | 2299 | |
9d5f9f70 YS |
2300 | if (btf_type_kflag(v->t)) |
2301 | err = btf_type_ops(member_type)->check_kflag_member(env, v->t, | |
2302 | member, | |
2303 | member_type); | |
2304 | else | |
2305 | err = btf_type_ops(member_type)->check_member(env, v->t, | |
2306 | member, | |
2307 | member_type); | |
179cde8c MKL |
2308 | if (err) |
2309 | return err; | |
eb3f595d MKL |
2310 | } |
2311 | ||
2312 | env_stack_pop_resolved(env, 0, 0); | |
2313 | ||
2314 | return 0; | |
2315 | } | |
2316 | ||
69b693f0 MKL |
2317 | static void btf_struct_log(struct btf_verifier_env *env, |
2318 | const struct btf_type *t) | |
2319 | { | |
2320 | btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t)); | |
2321 | } | |
2322 | ||
d83525ca AS |
2323 | /* find 'struct bpf_spin_lock' in map value. |
2324 | * return >= 0 offset if found | |
2325 | * and < 0 in case of error | |
2326 | */ | |
2327 | int btf_find_spin_lock(const struct btf *btf, const struct btf_type *t) | |
2328 | { | |
2329 | const struct btf_member *member; | |
2330 | u32 i, off = -ENOENT; | |
2331 | ||
2332 | if (!__btf_type_is_struct(t)) | |
2333 | return -EINVAL; | |
2334 | ||
2335 | for_each_member(i, t, member) { | |
2336 | const struct btf_type *member_type = btf_type_by_id(btf, | |
2337 | member->type); | |
2338 | if (!__btf_type_is_struct(member_type)) | |
2339 | continue; | |
2340 | if (member_type->size != sizeof(struct bpf_spin_lock)) | |
2341 | continue; | |
2342 | if (strcmp(__btf_name_by_offset(btf, member_type->name_off), | |
2343 | "bpf_spin_lock")) | |
2344 | continue; | |
2345 | if (off != -ENOENT) | |
2346 | /* only one 'struct bpf_spin_lock' is allowed */ | |
2347 | return -E2BIG; | |
2348 | off = btf_member_bit_offset(t, member); | |
2349 | if (off % 8) | |
2350 | /* valid C code cannot generate such BTF */ | |
2351 | return -EINVAL; | |
2352 | off /= 8; | |
2353 | if (off % __alignof__(struct bpf_spin_lock)) | |
2354 | /* valid struct bpf_spin_lock will be 4 byte aligned */ | |
2355 | return -EINVAL; | |
2356 | } | |
2357 | return off; | |
2358 | } | |
2359 | ||
b00b8dae MKL |
2360 | static void btf_struct_seq_show(const struct btf *btf, const struct btf_type *t, |
2361 | u32 type_id, void *data, u8 bits_offset, | |
2362 | struct seq_file *m) | |
2363 | { | |
2364 | const char *seq = BTF_INFO_KIND(t->info) == BTF_KIND_UNION ? "|" : ","; | |
2365 | const struct btf_member *member; | |
2366 | u32 i; | |
2367 | ||
2368 | seq_puts(m, "{"); | |
2369 | for_each_member(i, t, member) { | |
2370 | const struct btf_type *member_type = btf_type_by_id(btf, | |
2371 | member->type); | |
b00b8dae | 2372 | const struct btf_kind_operations *ops; |
9d5f9f70 YS |
2373 | u32 member_offset, bitfield_size; |
2374 | u32 bytes_offset; | |
2375 | u8 bits8_offset; | |
b00b8dae MKL |
2376 | |
2377 | if (i) | |
2378 | seq_puts(m, seq); | |
2379 | ||
9d5f9f70 YS |
2380 | member_offset = btf_member_bit_offset(t, member); |
2381 | bitfield_size = btf_member_bitfield_size(t, member); | |
17e3ac81 YS |
2382 | bytes_offset = BITS_ROUNDDOWN_BYTES(member_offset); |
2383 | bits8_offset = BITS_PER_BYTE_MASKED(member_offset); | |
9d5f9f70 | 2384 | if (bitfield_size) { |
17e3ac81 | 2385 | btf_bitfield_seq_show(data + bytes_offset, bits8_offset, |
9d5f9f70 YS |
2386 | bitfield_size, m); |
2387 | } else { | |
9d5f9f70 YS |
2388 | ops = btf_type_ops(member_type); |
2389 | ops->seq_show(btf, member_type, member->type, | |
2390 | data + bytes_offset, bits8_offset, m); | |
2391 | } | |
b00b8dae MKL |
2392 | } |
2393 | seq_puts(m, "}"); | |
2394 | } | |
2395 | ||
69b693f0 MKL |
2396 | static struct btf_kind_operations struct_ops = { |
2397 | .check_meta = btf_struct_check_meta, | |
eb3f595d | 2398 | .resolve = btf_struct_resolve, |
179cde8c | 2399 | .check_member = btf_struct_check_member, |
9d5f9f70 | 2400 | .check_kflag_member = btf_generic_check_kflag_member, |
69b693f0 | 2401 | .log_details = btf_struct_log, |
b00b8dae | 2402 | .seq_show = btf_struct_seq_show, |
69b693f0 MKL |
2403 | }; |
2404 | ||
179cde8c MKL |
2405 | static int btf_enum_check_member(struct btf_verifier_env *env, |
2406 | const struct btf_type *struct_type, | |
2407 | const struct btf_member *member, | |
2408 | const struct btf_type *member_type) | |
2409 | { | |
2410 | u32 struct_bits_off = member->offset; | |
2411 | u32 struct_size, bytes_offset; | |
2412 | ||
2413 | if (BITS_PER_BYTE_MASKED(struct_bits_off)) { | |
2414 | btf_verifier_log_member(env, struct_type, member, | |
2415 | "Member is not byte aligned"); | |
2416 | return -EINVAL; | |
2417 | } | |
2418 | ||
2419 | struct_size = struct_type->size; | |
2420 | bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off); | |
da6c7fae | 2421 | if (struct_size - bytes_offset < member_type->size) { |
179cde8c MKL |
2422 | btf_verifier_log_member(env, struct_type, member, |
2423 | "Member exceeds struct_size"); | |
2424 | return -EINVAL; | |
2425 | } | |
2426 | ||
2427 | return 0; | |
2428 | } | |
2429 | ||
9d5f9f70 YS |
2430 | static int btf_enum_check_kflag_member(struct btf_verifier_env *env, |
2431 | const struct btf_type *struct_type, | |
2432 | const struct btf_member *member, | |
2433 | const struct btf_type *member_type) | |
2434 | { | |
2435 | u32 struct_bits_off, nr_bits, bytes_end, struct_size; | |
2436 | u32 int_bitsize = sizeof(int) * BITS_PER_BYTE; | |
2437 | ||
2438 | struct_bits_off = BTF_MEMBER_BIT_OFFSET(member->offset); | |
2439 | nr_bits = BTF_MEMBER_BITFIELD_SIZE(member->offset); | |
2440 | if (!nr_bits) { | |
2441 | if (BITS_PER_BYTE_MASKED(struct_bits_off)) { | |
2442 | btf_verifier_log_member(env, struct_type, member, | |
2443 | "Member is not byte aligned"); | |
e3439af4 | 2444 | return -EINVAL; |
9d5f9f70 YS |
2445 | } |
2446 | ||
2447 | nr_bits = int_bitsize; | |
2448 | } else if (nr_bits > int_bitsize) { | |
2449 | btf_verifier_log_member(env, struct_type, member, | |
2450 | "Invalid member bitfield_size"); | |
2451 | return -EINVAL; | |
2452 | } | |
2453 | ||
2454 | struct_size = struct_type->size; | |
2455 | bytes_end = BITS_ROUNDUP_BYTES(struct_bits_off + nr_bits); | |
2456 | if (struct_size < bytes_end) { | |
2457 | btf_verifier_log_member(env, struct_type, member, | |
2458 | "Member exceeds struct_size"); | |
2459 | return -EINVAL; | |
2460 | } | |
2461 | ||
2462 | return 0; | |
2463 | } | |
2464 | ||
69b693f0 MKL |
2465 | static s32 btf_enum_check_meta(struct btf_verifier_env *env, |
2466 | const struct btf_type *t, | |
2467 | u32 meta_left) | |
2468 | { | |
2469 | const struct btf_enum *enums = btf_type_enum(t); | |
2470 | struct btf *btf = env->btf; | |
2471 | u16 i, nr_enums; | |
2472 | u32 meta_needed; | |
2473 | ||
2474 | nr_enums = btf_type_vlen(t); | |
2475 | meta_needed = nr_enums * sizeof(*enums); | |
2476 | ||
2477 | if (meta_left < meta_needed) { | |
2478 | btf_verifier_log_basic(env, t, | |
2479 | "meta_left:%u meta_needed:%u", | |
2480 | meta_left, meta_needed); | |
2481 | return -EINVAL; | |
2482 | } | |
2483 | ||
9d5f9f70 YS |
2484 | if (btf_type_kflag(t)) { |
2485 | btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); | |
2486 | return -EINVAL; | |
2487 | } | |
2488 | ||
9eea9849 AS |
2489 | if (t->size > 8 || !is_power_of_2(t->size)) { |
2490 | btf_verifier_log_type(env, t, "Unexpected size"); | |
69b693f0 MKL |
2491 | return -EINVAL; |
2492 | } | |
2493 | ||
eb04bbb6 YS |
2494 | /* enum type either no name or a valid one */ |
2495 | if (t->name_off && | |
2496 | !btf_name_valid_identifier(env->btf, t->name_off)) { | |
2497 | btf_verifier_log_type(env, t, "Invalid name"); | |
2498 | return -EINVAL; | |
2499 | } | |
2500 | ||
69b693f0 MKL |
2501 | btf_verifier_log_type(env, t, NULL); |
2502 | ||
2503 | for (i = 0; i < nr_enums; i++) { | |
fbcf93eb | 2504 | if (!btf_name_offset_valid(btf, enums[i].name_off)) { |
69b693f0 | 2505 | btf_verifier_log(env, "\tInvalid name_offset:%u", |
fbcf93eb | 2506 | enums[i].name_off); |
69b693f0 MKL |
2507 | return -EINVAL; |
2508 | } | |
2509 | ||
eb04bbb6 YS |
2510 | /* enum member must have a valid name */ |
2511 | if (!enums[i].name_off || | |
2512 | !btf_name_valid_identifier(btf, enums[i].name_off)) { | |
2513 | btf_verifier_log_type(env, t, "Invalid name"); | |
2514 | return -EINVAL; | |
2515 | } | |
2516 | ||
8580ac94 AS |
2517 | if (env->log.level == BPF_LOG_KERNEL) |
2518 | continue; | |
69b693f0 | 2519 | btf_verifier_log(env, "\t%s val=%d\n", |
23127b33 | 2520 | __btf_name_by_offset(btf, enums[i].name_off), |
69b693f0 MKL |
2521 | enums[i].val); |
2522 | } | |
2523 | ||
2524 | return meta_needed; | |
2525 | } | |
2526 | ||
2527 | static void btf_enum_log(struct btf_verifier_env *env, | |
2528 | const struct btf_type *t) | |
2529 | { | |
2530 | btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t)); | |
2531 | } | |
2532 | ||
b00b8dae MKL |
2533 | static void btf_enum_seq_show(const struct btf *btf, const struct btf_type *t, |
2534 | u32 type_id, void *data, u8 bits_offset, | |
2535 | struct seq_file *m) | |
2536 | { | |
2537 | const struct btf_enum *enums = btf_type_enum(t); | |
2538 | u32 i, nr_enums = btf_type_vlen(t); | |
2539 | int v = *(int *)data; | |
2540 | ||
2541 | for (i = 0; i < nr_enums; i++) { | |
2542 | if (v == enums[i].val) { | |
2543 | seq_printf(m, "%s", | |
23127b33 MKL |
2544 | __btf_name_by_offset(btf, |
2545 | enums[i].name_off)); | |
b00b8dae MKL |
2546 | return; |
2547 | } | |
2548 | } | |
2549 | ||
2550 | seq_printf(m, "%d", v); | |
2551 | } | |
2552 | ||
69b693f0 MKL |
2553 | static struct btf_kind_operations enum_ops = { |
2554 | .check_meta = btf_enum_check_meta, | |
eb3f595d | 2555 | .resolve = btf_df_resolve, |
179cde8c | 2556 | .check_member = btf_enum_check_member, |
9d5f9f70 | 2557 | .check_kflag_member = btf_enum_check_kflag_member, |
69b693f0 | 2558 | .log_details = btf_enum_log, |
b00b8dae | 2559 | .seq_show = btf_enum_seq_show, |
69b693f0 MKL |
2560 | }; |
2561 | ||
2667a262 MKL |
2562 | static s32 btf_func_proto_check_meta(struct btf_verifier_env *env, |
2563 | const struct btf_type *t, | |
2564 | u32 meta_left) | |
2565 | { | |
2566 | u32 meta_needed = btf_type_vlen(t) * sizeof(struct btf_param); | |
2567 | ||
2568 | if (meta_left < meta_needed) { | |
2569 | btf_verifier_log_basic(env, t, | |
2570 | "meta_left:%u meta_needed:%u", | |
2571 | meta_left, meta_needed); | |
2572 | return -EINVAL; | |
2573 | } | |
2574 | ||
2575 | if (t->name_off) { | |
2576 | btf_verifier_log_type(env, t, "Invalid name"); | |
2577 | return -EINVAL; | |
2578 | } | |
2579 | ||
9d5f9f70 YS |
2580 | if (btf_type_kflag(t)) { |
2581 | btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); | |
2582 | return -EINVAL; | |
2583 | } | |
2584 | ||
2667a262 MKL |
2585 | btf_verifier_log_type(env, t, NULL); |
2586 | ||
2587 | return meta_needed; | |
2588 | } | |
2589 | ||
2590 | static void btf_func_proto_log(struct btf_verifier_env *env, | |
2591 | const struct btf_type *t) | |
2592 | { | |
2593 | const struct btf_param *args = (const struct btf_param *)(t + 1); | |
2594 | u16 nr_args = btf_type_vlen(t), i; | |
2595 | ||
2596 | btf_verifier_log(env, "return=%u args=(", t->type); | |
2597 | if (!nr_args) { | |
2598 | btf_verifier_log(env, "void"); | |
2599 | goto done; | |
2600 | } | |
2601 | ||
2602 | if (nr_args == 1 && !args[0].type) { | |
2603 | /* Only one vararg */ | |
2604 | btf_verifier_log(env, "vararg"); | |
2605 | goto done; | |
2606 | } | |
2607 | ||
2608 | btf_verifier_log(env, "%u %s", args[0].type, | |
23127b33 MKL |
2609 | __btf_name_by_offset(env->btf, |
2610 | args[0].name_off)); | |
2667a262 MKL |
2611 | for (i = 1; i < nr_args - 1; i++) |
2612 | btf_verifier_log(env, ", %u %s", args[i].type, | |
23127b33 MKL |
2613 | __btf_name_by_offset(env->btf, |
2614 | args[i].name_off)); | |
2667a262 MKL |
2615 | |
2616 | if (nr_args > 1) { | |
2617 | const struct btf_param *last_arg = &args[nr_args - 1]; | |
2618 | ||
2619 | if (last_arg->type) | |
2620 | btf_verifier_log(env, ", %u %s", last_arg->type, | |
23127b33 MKL |
2621 | __btf_name_by_offset(env->btf, |
2622 | last_arg->name_off)); | |
2667a262 MKL |
2623 | else |
2624 | btf_verifier_log(env, ", vararg"); | |
2625 | } | |
2626 | ||
2627 | done: | |
2628 | btf_verifier_log(env, ")"); | |
2629 | } | |
2630 | ||
2631 | static struct btf_kind_operations func_proto_ops = { | |
2632 | .check_meta = btf_func_proto_check_meta, | |
2633 | .resolve = btf_df_resolve, | |
2634 | /* | |
2635 | * BTF_KIND_FUNC_PROTO cannot be directly referred by | |
2636 | * a struct's member. | |
2637 | * | |
2638 | * It should be a funciton pointer instead. | |
2639 | * (i.e. struct's member -> BTF_KIND_PTR -> BTF_KIND_FUNC_PROTO) | |
2640 | * | |
2641 | * Hence, there is no btf_func_check_member(). | |
2642 | */ | |
2643 | .check_member = btf_df_check_member, | |
9d5f9f70 | 2644 | .check_kflag_member = btf_df_check_kflag_member, |
2667a262 MKL |
2645 | .log_details = btf_func_proto_log, |
2646 | .seq_show = btf_df_seq_show, | |
2647 | }; | |
2648 | ||
2649 | static s32 btf_func_check_meta(struct btf_verifier_env *env, | |
2650 | const struct btf_type *t, | |
2651 | u32 meta_left) | |
2652 | { | |
2653 | if (!t->name_off || | |
2654 | !btf_name_valid_identifier(env->btf, t->name_off)) { | |
2655 | btf_verifier_log_type(env, t, "Invalid name"); | |
2656 | return -EINVAL; | |
2657 | } | |
2658 | ||
51c39bb1 AS |
2659 | if (btf_type_vlen(t) > BTF_FUNC_GLOBAL) { |
2660 | btf_verifier_log_type(env, t, "Invalid func linkage"); | |
2667a262 MKL |
2661 | return -EINVAL; |
2662 | } | |
2663 | ||
9d5f9f70 YS |
2664 | if (btf_type_kflag(t)) { |
2665 | btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); | |
2666 | return -EINVAL; | |
2667 | } | |
2668 | ||
2667a262 MKL |
2669 | btf_verifier_log_type(env, t, NULL); |
2670 | ||
2671 | return 0; | |
2672 | } | |
2673 | ||
2674 | static struct btf_kind_operations func_ops = { | |
2675 | .check_meta = btf_func_check_meta, | |
2676 | .resolve = btf_df_resolve, | |
2677 | .check_member = btf_df_check_member, | |
9d5f9f70 | 2678 | .check_kflag_member = btf_df_check_kflag_member, |
2667a262 MKL |
2679 | .log_details = btf_ref_type_log, |
2680 | .seq_show = btf_df_seq_show, | |
2681 | }; | |
2682 | ||
1dc92851 DB |
2683 | static s32 btf_var_check_meta(struct btf_verifier_env *env, |
2684 | const struct btf_type *t, | |
2685 | u32 meta_left) | |
2686 | { | |
2687 | const struct btf_var *var; | |
2688 | u32 meta_needed = sizeof(*var); | |
2689 | ||
2690 | if (meta_left < meta_needed) { | |
2691 | btf_verifier_log_basic(env, t, | |
2692 | "meta_left:%u meta_needed:%u", | |
2693 | meta_left, meta_needed); | |
2694 | return -EINVAL; | |
2695 | } | |
2696 | ||
2697 | if (btf_type_vlen(t)) { | |
2698 | btf_verifier_log_type(env, t, "vlen != 0"); | |
2699 | return -EINVAL; | |
2700 | } | |
2701 | ||
2702 | if (btf_type_kflag(t)) { | |
2703 | btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); | |
2704 | return -EINVAL; | |
2705 | } | |
2706 | ||
2707 | if (!t->name_off || | |
2708 | !__btf_name_valid(env->btf, t->name_off, true)) { | |
2709 | btf_verifier_log_type(env, t, "Invalid name"); | |
2710 | return -EINVAL; | |
2711 | } | |
2712 | ||
2713 | /* A var cannot be in type void */ | |
2714 | if (!t->type || !BTF_TYPE_ID_VALID(t->type)) { | |
2715 | btf_verifier_log_type(env, t, "Invalid type_id"); | |
2716 | return -EINVAL; | |
2717 | } | |
2718 | ||
2719 | var = btf_type_var(t); | |
2720 | if (var->linkage != BTF_VAR_STATIC && | |
2721 | var->linkage != BTF_VAR_GLOBAL_ALLOCATED) { | |
2722 | btf_verifier_log_type(env, t, "Linkage not supported"); | |
2723 | return -EINVAL; | |
2724 | } | |
2725 | ||
2726 | btf_verifier_log_type(env, t, NULL); | |
2727 | ||
2728 | return meta_needed; | |
2729 | } | |
2730 | ||
2731 | static void btf_var_log(struct btf_verifier_env *env, const struct btf_type *t) | |
2732 | { | |
2733 | const struct btf_var *var = btf_type_var(t); | |
2734 | ||
2735 | btf_verifier_log(env, "type_id=%u linkage=%u", t->type, var->linkage); | |
2736 | } | |
2737 | ||
2738 | static const struct btf_kind_operations var_ops = { | |
2739 | .check_meta = btf_var_check_meta, | |
2740 | .resolve = btf_var_resolve, | |
2741 | .check_member = btf_df_check_member, | |
2742 | .check_kflag_member = btf_df_check_kflag_member, | |
2743 | .log_details = btf_var_log, | |
2744 | .seq_show = btf_var_seq_show, | |
2745 | }; | |
2746 | ||
2747 | static s32 btf_datasec_check_meta(struct btf_verifier_env *env, | |
2748 | const struct btf_type *t, | |
2749 | u32 meta_left) | |
2750 | { | |
2751 | const struct btf_var_secinfo *vsi; | |
2752 | u64 last_vsi_end_off = 0, sum = 0; | |
2753 | u32 i, meta_needed; | |
2754 | ||
2755 | meta_needed = btf_type_vlen(t) * sizeof(*vsi); | |
2756 | if (meta_left < meta_needed) { | |
2757 | btf_verifier_log_basic(env, t, | |
2758 | "meta_left:%u meta_needed:%u", | |
2759 | meta_left, meta_needed); | |
2760 | return -EINVAL; | |
2761 | } | |
2762 | ||
2763 | if (!btf_type_vlen(t)) { | |
2764 | btf_verifier_log_type(env, t, "vlen == 0"); | |
2765 | return -EINVAL; | |
2766 | } | |
2767 | ||
2768 | if (!t->size) { | |
2769 | btf_verifier_log_type(env, t, "size == 0"); | |
2770 | return -EINVAL; | |
2771 | } | |
2772 | ||
2773 | if (btf_type_kflag(t)) { | |
2774 | btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); | |
2775 | return -EINVAL; | |
2776 | } | |
2777 | ||
2778 | if (!t->name_off || | |
2779 | !btf_name_valid_section(env->btf, t->name_off)) { | |
2780 | btf_verifier_log_type(env, t, "Invalid name"); | |
2781 | return -EINVAL; | |
2782 | } | |
2783 | ||
2784 | btf_verifier_log_type(env, t, NULL); | |
2785 | ||
2786 | for_each_vsi(i, t, vsi) { | |
2787 | /* A var cannot be in type void */ | |
2788 | if (!vsi->type || !BTF_TYPE_ID_VALID(vsi->type)) { | |
2789 | btf_verifier_log_vsi(env, t, vsi, | |
2790 | "Invalid type_id"); | |
2791 | return -EINVAL; | |
2792 | } | |
2793 | ||
2794 | if (vsi->offset < last_vsi_end_off || vsi->offset >= t->size) { | |
2795 | btf_verifier_log_vsi(env, t, vsi, | |
2796 | "Invalid offset"); | |
2797 | return -EINVAL; | |
2798 | } | |
2799 | ||
2800 | if (!vsi->size || vsi->size > t->size) { | |
2801 | btf_verifier_log_vsi(env, t, vsi, | |
2802 | "Invalid size"); | |
2803 | return -EINVAL; | |
2804 | } | |
2805 | ||
2806 | last_vsi_end_off = vsi->offset + vsi->size; | |
2807 | if (last_vsi_end_off > t->size) { | |
2808 | btf_verifier_log_vsi(env, t, vsi, | |
2809 | "Invalid offset+size"); | |
2810 | return -EINVAL; | |
2811 | } | |
2812 | ||
2813 | btf_verifier_log_vsi(env, t, vsi, NULL); | |
2814 | sum += vsi->size; | |
2815 | } | |
2816 | ||
2817 | if (t->size < sum) { | |
2818 | btf_verifier_log_type(env, t, "Invalid btf_info size"); | |
2819 | return -EINVAL; | |
2820 | } | |
2821 | ||
2822 | return meta_needed; | |
2823 | } | |
2824 | ||
2825 | static int btf_datasec_resolve(struct btf_verifier_env *env, | |
2826 | const struct resolve_vertex *v) | |
2827 | { | |
2828 | const struct btf_var_secinfo *vsi; | |
2829 | struct btf *btf = env->btf; | |
2830 | u16 i; | |
2831 | ||
2832 | for_each_vsi_from(i, v->next_member, v->t, vsi) { | |
2833 | u32 var_type_id = vsi->type, type_id, type_size = 0; | |
2834 | const struct btf_type *var_type = btf_type_by_id(env->btf, | |
2835 | var_type_id); | |
2836 | if (!var_type || !btf_type_is_var(var_type)) { | |
2837 | btf_verifier_log_vsi(env, v->t, vsi, | |
2838 | "Not a VAR kind member"); | |
2839 | return -EINVAL; | |
2840 | } | |
2841 | ||
2842 | if (!env_type_is_resolve_sink(env, var_type) && | |
2843 | !env_type_is_resolved(env, var_type_id)) { | |
2844 | env_stack_set_next_member(env, i + 1); | |
2845 | return env_stack_push(env, var_type, var_type_id); | |
2846 | } | |
2847 | ||
2848 | type_id = var_type->type; | |
2849 | if (!btf_type_id_size(btf, &type_id, &type_size)) { | |
2850 | btf_verifier_log_vsi(env, v->t, vsi, "Invalid type"); | |
2851 | return -EINVAL; | |
2852 | } | |
2853 | ||
2854 | if (vsi->size < type_size) { | |
2855 | btf_verifier_log_vsi(env, v->t, vsi, "Invalid size"); | |
2856 | return -EINVAL; | |
2857 | } | |
2858 | } | |
2859 | ||
2860 | env_stack_pop_resolved(env, 0, 0); | |
2861 | return 0; | |
2862 | } | |
2863 | ||
2864 | static void btf_datasec_log(struct btf_verifier_env *env, | |
2865 | const struct btf_type *t) | |
2866 | { | |
2867 | btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t)); | |
2868 | } | |
2869 | ||
2870 | static void btf_datasec_seq_show(const struct btf *btf, | |
2871 | const struct btf_type *t, u32 type_id, | |
2872 | void *data, u8 bits_offset, | |
2873 | struct seq_file *m) | |
2874 | { | |
2875 | const struct btf_var_secinfo *vsi; | |
2876 | const struct btf_type *var; | |
2877 | u32 i; | |
2878 | ||
2879 | seq_printf(m, "section (\"%s\") = {", __btf_name_by_offset(btf, t->name_off)); | |
2880 | for_each_vsi(i, t, vsi) { | |
2881 | var = btf_type_by_id(btf, vsi->type); | |
2882 | if (i) | |
2883 | seq_puts(m, ","); | |
2884 | btf_type_ops(var)->seq_show(btf, var, vsi->type, | |
2885 | data + vsi->offset, bits_offset, m); | |
2886 | } | |
2887 | seq_puts(m, "}"); | |
2888 | } | |
2889 | ||
2890 | static const struct btf_kind_operations datasec_ops = { | |
2891 | .check_meta = btf_datasec_check_meta, | |
2892 | .resolve = btf_datasec_resolve, | |
2893 | .check_member = btf_df_check_member, | |
2894 | .check_kflag_member = btf_df_check_kflag_member, | |
2895 | .log_details = btf_datasec_log, | |
2896 | .seq_show = btf_datasec_seq_show, | |
2897 | }; | |
2898 | ||
2667a262 MKL |
2899 | static int btf_func_proto_check(struct btf_verifier_env *env, |
2900 | const struct btf_type *t) | |
2901 | { | |
2902 | const struct btf_type *ret_type; | |
2903 | const struct btf_param *args; | |
2904 | const struct btf *btf; | |
2905 | u16 nr_args, i; | |
2906 | int err; | |
2907 | ||
2908 | btf = env->btf; | |
2909 | args = (const struct btf_param *)(t + 1); | |
2910 | nr_args = btf_type_vlen(t); | |
2911 | ||
2912 | /* Check func return type which could be "void" (t->type == 0) */ | |
2913 | if (t->type) { | |
2914 | u32 ret_type_id = t->type; | |
2915 | ||
2916 | ret_type = btf_type_by_id(btf, ret_type_id); | |
2917 | if (!ret_type) { | |
2918 | btf_verifier_log_type(env, t, "Invalid return type"); | |
2919 | return -EINVAL; | |
2920 | } | |
2921 | ||
2922 | if (btf_type_needs_resolve(ret_type) && | |
2923 | !env_type_is_resolved(env, ret_type_id)) { | |
2924 | err = btf_resolve(env, ret_type, ret_type_id); | |
2925 | if (err) | |
2926 | return err; | |
2927 | } | |
2928 | ||
2929 | /* Ensure the return type is a type that has a size */ | |
2930 | if (!btf_type_id_size(btf, &ret_type_id, NULL)) { | |
2931 | btf_verifier_log_type(env, t, "Invalid return type"); | |
2932 | return -EINVAL; | |
2933 | } | |
2934 | } | |
2935 | ||
2936 | if (!nr_args) | |
2937 | return 0; | |
2938 | ||
2939 | /* Last func arg type_id could be 0 if it is a vararg */ | |
2940 | if (!args[nr_args - 1].type) { | |
2941 | if (args[nr_args - 1].name_off) { | |
2942 | btf_verifier_log_type(env, t, "Invalid arg#%u", | |
2943 | nr_args); | |
2944 | return -EINVAL; | |
2945 | } | |
2946 | nr_args--; | |
2947 | } | |
2948 | ||
2949 | err = 0; | |
2950 | for (i = 0; i < nr_args; i++) { | |
2951 | const struct btf_type *arg_type; | |
2952 | u32 arg_type_id; | |
2953 | ||
2954 | arg_type_id = args[i].type; | |
2955 | arg_type = btf_type_by_id(btf, arg_type_id); | |
2956 | if (!arg_type) { | |
2957 | btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1); | |
2958 | err = -EINVAL; | |
2959 | break; | |
2960 | } | |
2961 | ||
2962 | if (args[i].name_off && | |
2963 | (!btf_name_offset_valid(btf, args[i].name_off) || | |
2964 | !btf_name_valid_identifier(btf, args[i].name_off))) { | |
2965 | btf_verifier_log_type(env, t, | |
2966 | "Invalid arg#%u", i + 1); | |
2967 | err = -EINVAL; | |
2968 | break; | |
2969 | } | |
2970 | ||
2971 | if (btf_type_needs_resolve(arg_type) && | |
2972 | !env_type_is_resolved(env, arg_type_id)) { | |
2973 | err = btf_resolve(env, arg_type, arg_type_id); | |
2974 | if (err) | |
2975 | break; | |
2976 | } | |
2977 | ||
2978 | if (!btf_type_id_size(btf, &arg_type_id, NULL)) { | |
2979 | btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1); | |
2980 | err = -EINVAL; | |
2981 | break; | |
2982 | } | |
2983 | } | |
2984 | ||
2985 | return err; | |
2986 | } | |
2987 | ||
2988 | static int btf_func_check(struct btf_verifier_env *env, | |
2989 | const struct btf_type *t) | |
2990 | { | |
2991 | const struct btf_type *proto_type; | |
2992 | const struct btf_param *args; | |
2993 | const struct btf *btf; | |
2994 | u16 nr_args, i; | |
2995 | ||
2996 | btf = env->btf; | |
2997 | proto_type = btf_type_by_id(btf, t->type); | |
2998 | ||
2999 | if (!proto_type || !btf_type_is_func_proto(proto_type)) { | |
3000 | btf_verifier_log_type(env, t, "Invalid type_id"); | |
3001 | return -EINVAL; | |
3002 | } | |
3003 | ||
3004 | args = (const struct btf_param *)(proto_type + 1); | |
3005 | nr_args = btf_type_vlen(proto_type); | |
3006 | for (i = 0; i < nr_args; i++) { | |
3007 | if (!args[i].name_off && args[i].type) { | |
3008 | btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1); | |
3009 | return -EINVAL; | |
3010 | } | |
3011 | } | |
3012 | ||
3013 | return 0; | |
3014 | } | |
3015 | ||
69b693f0 MKL |
3016 | static const struct btf_kind_operations * const kind_ops[NR_BTF_KINDS] = { |
3017 | [BTF_KIND_INT] = &int_ops, | |
3018 | [BTF_KIND_PTR] = &ptr_ops, | |
3019 | [BTF_KIND_ARRAY] = &array_ops, | |
3020 | [BTF_KIND_STRUCT] = &struct_ops, | |
3021 | [BTF_KIND_UNION] = &struct_ops, | |
3022 | [BTF_KIND_ENUM] = &enum_ops, | |
3023 | [BTF_KIND_FWD] = &fwd_ops, | |
3024 | [BTF_KIND_TYPEDEF] = &modifier_ops, | |
3025 | [BTF_KIND_VOLATILE] = &modifier_ops, | |
3026 | [BTF_KIND_CONST] = &modifier_ops, | |
3027 | [BTF_KIND_RESTRICT] = &modifier_ops, | |
2667a262 MKL |
3028 | [BTF_KIND_FUNC] = &func_ops, |
3029 | [BTF_KIND_FUNC_PROTO] = &func_proto_ops, | |
1dc92851 DB |
3030 | [BTF_KIND_VAR] = &var_ops, |
3031 | [BTF_KIND_DATASEC] = &datasec_ops, | |
69b693f0 MKL |
3032 | }; |
3033 | ||
3034 | static s32 btf_check_meta(struct btf_verifier_env *env, | |
3035 | const struct btf_type *t, | |
3036 | u32 meta_left) | |
3037 | { | |
3038 | u32 saved_meta_left = meta_left; | |
3039 | s32 var_meta_size; | |
3040 | ||
3041 | if (meta_left < sizeof(*t)) { | |
3042 | btf_verifier_log(env, "[%u] meta_left:%u meta_needed:%zu", | |
3043 | env->log_type_id, meta_left, sizeof(*t)); | |
3044 | return -EINVAL; | |
3045 | } | |
3046 | meta_left -= sizeof(*t); | |
3047 | ||
aea2f7b8 MKL |
3048 | if (t->info & ~BTF_INFO_MASK) { |
3049 | btf_verifier_log(env, "[%u] Invalid btf_info:%x", | |
3050 | env->log_type_id, t->info); | |
3051 | return -EINVAL; | |
3052 | } | |
3053 | ||
69b693f0 MKL |
3054 | if (BTF_INFO_KIND(t->info) > BTF_KIND_MAX || |
3055 | BTF_INFO_KIND(t->info) == BTF_KIND_UNKN) { | |
3056 | btf_verifier_log(env, "[%u] Invalid kind:%u", | |
3057 | env->log_type_id, BTF_INFO_KIND(t->info)); | |
3058 | return -EINVAL; | |
3059 | } | |
3060 | ||
fbcf93eb | 3061 | if (!btf_name_offset_valid(env->btf, t->name_off)) { |
69b693f0 | 3062 | btf_verifier_log(env, "[%u] Invalid name_offset:%u", |
fbcf93eb | 3063 | env->log_type_id, t->name_off); |
69b693f0 MKL |
3064 | return -EINVAL; |
3065 | } | |
3066 | ||
3067 | var_meta_size = btf_type_ops(t)->check_meta(env, t, meta_left); | |
3068 | if (var_meta_size < 0) | |
3069 | return var_meta_size; | |
3070 | ||
3071 | meta_left -= var_meta_size; | |
3072 | ||
3073 | return saved_meta_left - meta_left; | |
3074 | } | |
3075 | ||
3076 | static int btf_check_all_metas(struct btf_verifier_env *env) | |
3077 | { | |
3078 | struct btf *btf = env->btf; | |
3079 | struct btf_header *hdr; | |
3080 | void *cur, *end; | |
3081 | ||
f80442a4 | 3082 | hdr = &btf->hdr; |
69b693f0 | 3083 | cur = btf->nohdr_data + hdr->type_off; |
4b1c5d91 | 3084 | end = cur + hdr->type_len; |
69b693f0 MKL |
3085 | |
3086 | env->log_type_id = 1; | |
3087 | while (cur < end) { | |
3088 | struct btf_type *t = cur; | |
3089 | s32 meta_size; | |
3090 | ||
3091 | meta_size = btf_check_meta(env, t, end - cur); | |
3092 | if (meta_size < 0) | |
3093 | return meta_size; | |
3094 | ||
3095 | btf_add_type(env, t); | |
3096 | cur += meta_size; | |
3097 | env->log_type_id++; | |
3098 | } | |
3099 | ||
3100 | return 0; | |
3101 | } | |
3102 | ||
eb3f595d MKL |
3103 | static bool btf_resolve_valid(struct btf_verifier_env *env, |
3104 | const struct btf_type *t, | |
3105 | u32 type_id) | |
3106 | { | |
3107 | struct btf *btf = env->btf; | |
3108 | ||
3109 | if (!env_type_is_resolved(env, type_id)) | |
3110 | return false; | |
3111 | ||
1dc92851 | 3112 | if (btf_type_is_struct(t) || btf_type_is_datasec(t)) |
eb3f595d | 3113 | return !btf->resolved_ids[type_id] && |
1dc92851 | 3114 | !btf->resolved_sizes[type_id]; |
eb3f595d | 3115 | |
1dc92851 DB |
3116 | if (btf_type_is_modifier(t) || btf_type_is_ptr(t) || |
3117 | btf_type_is_var(t)) { | |
eb3f595d | 3118 | t = btf_type_id_resolve(btf, &type_id); |
1dc92851 DB |
3119 | return t && |
3120 | !btf_type_is_modifier(t) && | |
3121 | !btf_type_is_var(t) && | |
3122 | !btf_type_is_datasec(t); | |
eb3f595d MKL |
3123 | } |
3124 | ||
3125 | if (btf_type_is_array(t)) { | |
3126 | const struct btf_array *array = btf_type_array(t); | |
3127 | const struct btf_type *elem_type; | |
3128 | u32 elem_type_id = array->type; | |
3129 | u32 elem_size; | |
3130 | ||
3131 | elem_type = btf_type_id_size(btf, &elem_type_id, &elem_size); | |
3132 | return elem_type && !btf_type_is_modifier(elem_type) && | |
3133 | (array->nelems * elem_size == | |
3134 | btf->resolved_sizes[type_id]); | |
3135 | } | |
3136 | ||
3137 | return false; | |
3138 | } | |
3139 | ||
2667a262 MKL |
3140 | static int btf_resolve(struct btf_verifier_env *env, |
3141 | const struct btf_type *t, u32 type_id) | |
3142 | { | |
3143 | u32 save_log_type_id = env->log_type_id; | |
3144 | const struct resolve_vertex *v; | |
3145 | int err = 0; | |
3146 | ||
3147 | env->resolve_mode = RESOLVE_TBD; | |
3148 | env_stack_push(env, t, type_id); | |
3149 | while (!err && (v = env_stack_peak(env))) { | |
3150 | env->log_type_id = v->type_id; | |
3151 | err = btf_type_ops(v->t)->resolve(env, v); | |
3152 | } | |
3153 | ||
3154 | env->log_type_id = type_id; | |
3155 | if (err == -E2BIG) { | |
3156 | btf_verifier_log_type(env, t, | |
3157 | "Exceeded max resolving depth:%u", | |
3158 | MAX_RESOLVE_DEPTH); | |
3159 | } else if (err == -EEXIST) { | |
3160 | btf_verifier_log_type(env, t, "Loop detected"); | |
3161 | } | |
3162 | ||
3163 | /* Final sanity check */ | |
3164 | if (!err && !btf_resolve_valid(env, t, type_id)) { | |
3165 | btf_verifier_log_type(env, t, "Invalid resolve state"); | |
3166 | err = -EINVAL; | |
3167 | } | |
3168 | ||
3169 | env->log_type_id = save_log_type_id; | |
3170 | return err; | |
3171 | } | |
3172 | ||
eb3f595d MKL |
3173 | static int btf_check_all_types(struct btf_verifier_env *env) |
3174 | { | |
3175 | struct btf *btf = env->btf; | |
3176 | u32 type_id; | |
3177 | int err; | |
3178 | ||
3179 | err = env_resolve_init(env); | |
3180 | if (err) | |
3181 | return err; | |
3182 | ||
3183 | env->phase++; | |
3184 | for (type_id = 1; type_id <= btf->nr_types; type_id++) { | |
3185 | const struct btf_type *t = btf_type_by_id(btf, type_id); | |
3186 | ||
3187 | env->log_type_id = type_id; | |
3188 | if (btf_type_needs_resolve(t) && | |
3189 | !env_type_is_resolved(env, type_id)) { | |
3190 | err = btf_resolve(env, t, type_id); | |
3191 | if (err) | |
3192 | return err; | |
3193 | } | |
3194 | ||
2667a262 MKL |
3195 | if (btf_type_is_func_proto(t)) { |
3196 | err = btf_func_proto_check(env, t); | |
3197 | if (err) | |
3198 | return err; | |
3199 | } | |
3200 | ||
3201 | if (btf_type_is_func(t)) { | |
3202 | err = btf_func_check(env, t); | |
3203 | if (err) | |
3204 | return err; | |
eb3f595d MKL |
3205 | } |
3206 | } | |
3207 | ||
3208 | return 0; | |
3209 | } | |
3210 | ||
69b693f0 MKL |
3211 | static int btf_parse_type_sec(struct btf_verifier_env *env) |
3212 | { | |
f80442a4 | 3213 | const struct btf_header *hdr = &env->btf->hdr; |
eb3f595d MKL |
3214 | int err; |
3215 | ||
f80442a4 MKL |
3216 | /* Type section must align to 4 bytes */ |
3217 | if (hdr->type_off & (sizeof(u32) - 1)) { | |
3218 | btf_verifier_log(env, "Unaligned type_off"); | |
3219 | return -EINVAL; | |
3220 | } | |
3221 | ||
3222 | if (!hdr->type_len) { | |
3223 | btf_verifier_log(env, "No type found"); | |
3224 | return -EINVAL; | |
3225 | } | |
3226 | ||
eb3f595d MKL |
3227 | err = btf_check_all_metas(env); |
3228 | if (err) | |
3229 | return err; | |
3230 | ||
3231 | return btf_check_all_types(env); | |
69b693f0 MKL |
3232 | } |
3233 | ||
3234 | static int btf_parse_str_sec(struct btf_verifier_env *env) | |
3235 | { | |
3236 | const struct btf_header *hdr; | |
3237 | struct btf *btf = env->btf; | |
3238 | const char *start, *end; | |
3239 | ||
f80442a4 | 3240 | hdr = &btf->hdr; |
69b693f0 MKL |
3241 | start = btf->nohdr_data + hdr->str_off; |
3242 | end = start + hdr->str_len; | |
3243 | ||
f80442a4 MKL |
3244 | if (end != btf->data + btf->data_size) { |
3245 | btf_verifier_log(env, "String section is not at the end"); | |
3246 | return -EINVAL; | |
3247 | } | |
3248 | ||
69b693f0 MKL |
3249 | if (!hdr->str_len || hdr->str_len - 1 > BTF_MAX_NAME_OFFSET || |
3250 | start[0] || end[-1]) { | |
3251 | btf_verifier_log(env, "Invalid string section"); | |
3252 | return -EINVAL; | |
3253 | } | |
3254 | ||
3255 | btf->strings = start; | |
3256 | ||
3257 | return 0; | |
3258 | } | |
3259 | ||
f80442a4 MKL |
3260 | static const size_t btf_sec_info_offset[] = { |
3261 | offsetof(struct btf_header, type_off), | |
3262 | offsetof(struct btf_header, str_off), | |
3263 | }; | |
3264 | ||
3265 | static int btf_sec_info_cmp(const void *a, const void *b) | |
69b693f0 | 3266 | { |
f80442a4 MKL |
3267 | const struct btf_sec_info *x = a; |
3268 | const struct btf_sec_info *y = b; | |
3269 | ||
3270 | return (int)(x->off - y->off) ? : (int)(x->len - y->len); | |
3271 | } | |
3272 | ||
3273 | static int btf_check_sec_info(struct btf_verifier_env *env, | |
3274 | u32 btf_data_size) | |
3275 | { | |
a2889a4c | 3276 | struct btf_sec_info secs[ARRAY_SIZE(btf_sec_info_offset)]; |
f80442a4 | 3277 | u32 total, expected_total, i; |
69b693f0 | 3278 | const struct btf_header *hdr; |
f80442a4 MKL |
3279 | const struct btf *btf; |
3280 | ||
3281 | btf = env->btf; | |
3282 | hdr = &btf->hdr; | |
3283 | ||
3284 | /* Populate the secs from hdr */ | |
a2889a4c | 3285 | for (i = 0; i < ARRAY_SIZE(btf_sec_info_offset); i++) |
f80442a4 MKL |
3286 | secs[i] = *(struct btf_sec_info *)((void *)hdr + |
3287 | btf_sec_info_offset[i]); | |
3288 | ||
a2889a4c MKL |
3289 | sort(secs, ARRAY_SIZE(btf_sec_info_offset), |
3290 | sizeof(struct btf_sec_info), btf_sec_info_cmp, NULL); | |
f80442a4 MKL |
3291 | |
3292 | /* Check for gaps and overlap among sections */ | |
3293 | total = 0; | |
3294 | expected_total = btf_data_size - hdr->hdr_len; | |
a2889a4c | 3295 | for (i = 0; i < ARRAY_SIZE(btf_sec_info_offset); i++) { |
f80442a4 MKL |
3296 | if (expected_total < secs[i].off) { |
3297 | btf_verifier_log(env, "Invalid section offset"); | |
3298 | return -EINVAL; | |
3299 | } | |
3300 | if (total < secs[i].off) { | |
3301 | /* gap */ | |
3302 | btf_verifier_log(env, "Unsupported section found"); | |
3303 | return -EINVAL; | |
3304 | } | |
3305 | if (total > secs[i].off) { | |
3306 | btf_verifier_log(env, "Section overlap found"); | |
3307 | return -EINVAL; | |
3308 | } | |
3309 | if (expected_total - total < secs[i].len) { | |
3310 | btf_verifier_log(env, | |
3311 | "Total section length too long"); | |
3312 | return -EINVAL; | |
3313 | } | |
3314 | total += secs[i].len; | |
3315 | } | |
3316 | ||
3317 | /* There is data other than hdr and known sections */ | |
3318 | if (expected_total != total) { | |
3319 | btf_verifier_log(env, "Unsupported section found"); | |
3320 | return -EINVAL; | |
3321 | } | |
3322 | ||
3323 | return 0; | |
3324 | } | |
3325 | ||
4a6998af | 3326 | static int btf_parse_hdr(struct btf_verifier_env *env) |
f80442a4 | 3327 | { |
4a6998af | 3328 | u32 hdr_len, hdr_copy, btf_data_size; |
f80442a4 | 3329 | const struct btf_header *hdr; |
f80442a4 MKL |
3330 | struct btf *btf; |
3331 | int err; | |
69b693f0 | 3332 | |
f80442a4 | 3333 | btf = env->btf; |
4a6998af | 3334 | btf_data_size = btf->data_size; |
f80442a4 | 3335 | |
4a6998af ML |
3336 | if (btf_data_size < |
3337 | offsetof(struct btf_header, hdr_len) + sizeof(hdr->hdr_len)) { | |
f80442a4 MKL |
3338 | btf_verifier_log(env, "hdr_len not found"); |
3339 | return -EINVAL; | |
3340 | } | |
3341 | ||
4a6998af ML |
3342 | hdr = btf->data; |
3343 | hdr_len = hdr->hdr_len; | |
f80442a4 | 3344 | if (btf_data_size < hdr_len) { |
69b693f0 MKL |
3345 | btf_verifier_log(env, "btf_header not found"); |
3346 | return -EINVAL; | |
3347 | } | |
3348 | ||
4a6998af ML |
3349 | /* Ensure the unsupported header fields are zero */ |
3350 | if (hdr_len > sizeof(btf->hdr)) { | |
3351 | u8 *expected_zero = btf->data + sizeof(btf->hdr); | |
3352 | u8 *end = btf->data + hdr_len; | |
3353 | ||
3354 | for (; expected_zero < end; expected_zero++) { | |
3355 | if (*expected_zero) { | |
3356 | btf_verifier_log(env, "Unsupported btf_header"); | |
3357 | return -E2BIG; | |
3358 | } | |
3359 | } | |
f80442a4 MKL |
3360 | } |
3361 | ||
3362 | hdr_copy = min_t(u32, hdr_len, sizeof(btf->hdr)); | |
4a6998af | 3363 | memcpy(&btf->hdr, btf->data, hdr_copy); |
f80442a4 MKL |
3364 | |
3365 | hdr = &btf->hdr; | |
3366 | ||
3367 | btf_verifier_log_hdr(env, btf_data_size); | |
69b693f0 | 3368 | |
69b693f0 MKL |
3369 | if (hdr->magic != BTF_MAGIC) { |
3370 | btf_verifier_log(env, "Invalid magic"); | |
3371 | return -EINVAL; | |
3372 | } | |
3373 | ||
3374 | if (hdr->version != BTF_VERSION) { | |
3375 | btf_verifier_log(env, "Unsupported version"); | |
3376 | return -ENOTSUPP; | |
3377 | } | |
3378 | ||
3379 | if (hdr->flags) { | |
3380 | btf_verifier_log(env, "Unsupported flags"); | |
3381 | return -ENOTSUPP; | |
3382 | } | |
3383 | ||
f80442a4 | 3384 | if (btf_data_size == hdr->hdr_len) { |
69b693f0 MKL |
3385 | btf_verifier_log(env, "No data"); |
3386 | return -EINVAL; | |
3387 | } | |
3388 | ||
f80442a4 MKL |
3389 | err = btf_check_sec_info(env, btf_data_size); |
3390 | if (err) | |
3391 | return err; | |
69b693f0 MKL |
3392 | |
3393 | return 0; | |
3394 | } | |
3395 | ||
3396 | static struct btf *btf_parse(void __user *btf_data, u32 btf_data_size, | |
3397 | u32 log_level, char __user *log_ubuf, u32 log_size) | |
3398 | { | |
3399 | struct btf_verifier_env *env = NULL; | |
3400 | struct bpf_verifier_log *log; | |
3401 | struct btf *btf = NULL; | |
3402 | u8 *data; | |
3403 | int err; | |
3404 | ||
3405 | if (btf_data_size > BTF_MAX_SIZE) | |
3406 | return ERR_PTR(-E2BIG); | |
3407 | ||
3408 | env = kzalloc(sizeof(*env), GFP_KERNEL | __GFP_NOWARN); | |
3409 | if (!env) | |
3410 | return ERR_PTR(-ENOMEM); | |
3411 | ||
3412 | log = &env->log; | |
3413 | if (log_level || log_ubuf || log_size) { | |
3414 | /* user requested verbose verifier output | |
3415 | * and supplied buffer to store the verification trace | |
3416 | */ | |
3417 | log->level = log_level; | |
3418 | log->ubuf = log_ubuf; | |
3419 | log->len_total = log_size; | |
3420 | ||
3421 | /* log attributes have to be sane */ | |
3422 | if (log->len_total < 128 || log->len_total > UINT_MAX >> 8 || | |
3423 | !log->level || !log->ubuf) { | |
3424 | err = -EINVAL; | |
3425 | goto errout; | |
3426 | } | |
3427 | } | |
3428 | ||
3429 | btf = kzalloc(sizeof(*btf), GFP_KERNEL | __GFP_NOWARN); | |
3430 | if (!btf) { | |
3431 | err = -ENOMEM; | |
3432 | goto errout; | |
3433 | } | |
f80442a4 MKL |
3434 | env->btf = btf; |
3435 | ||
69b693f0 MKL |
3436 | data = kvmalloc(btf_data_size, GFP_KERNEL | __GFP_NOWARN); |
3437 | if (!data) { | |
3438 | err = -ENOMEM; | |
3439 | goto errout; | |
3440 | } | |
3441 | ||
3442 | btf->data = data; | |
3443 | btf->data_size = btf_data_size; | |
3444 | ||
3445 | if (copy_from_user(data, btf_data, btf_data_size)) { | |
3446 | err = -EFAULT; | |
3447 | goto errout; | |
3448 | } | |
3449 | ||
4a6998af ML |
3450 | err = btf_parse_hdr(env); |
3451 | if (err) | |
3452 | goto errout; | |
3453 | ||
3454 | btf->nohdr_data = btf->data + btf->hdr.hdr_len; | |
3455 | ||
69b693f0 MKL |
3456 | err = btf_parse_str_sec(env); |
3457 | if (err) | |
3458 | goto errout; | |
3459 | ||
3460 | err = btf_parse_type_sec(env); | |
3461 | if (err) | |
3462 | goto errout; | |
3463 | ||
f80442a4 | 3464 | if (log->level && bpf_verifier_log_full(log)) { |
69b693f0 MKL |
3465 | err = -ENOSPC; |
3466 | goto errout; | |
3467 | } | |
3468 | ||
f80442a4 MKL |
3469 | btf_verifier_env_free(env); |
3470 | refcount_set(&btf->refcnt, 1); | |
3471 | return btf; | |
69b693f0 MKL |
3472 | |
3473 | errout: | |
3474 | btf_verifier_env_free(env); | |
3475 | if (btf) | |
3476 | btf_free(btf); | |
3477 | return ERR_PTR(err); | |
3478 | } | |
b00b8dae | 3479 | |
90ceddcb FS |
3480 | extern char __weak __start_BTF[]; |
3481 | extern char __weak __stop_BTF[]; | |
91cc1a99 AS |
3482 | extern struct btf *btf_vmlinux; |
3483 | ||
3484 | #define BPF_MAP_TYPE(_id, _ops) | |
f2e10bff | 3485 | #define BPF_LINK_TYPE(_id, _name) |
91cc1a99 AS |
3486 | static union { |
3487 | struct bpf_ctx_convert { | |
3488 | #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \ | |
3489 | prog_ctx_type _id##_prog; \ | |
3490 | kern_ctx_type _id##_kern; | |
3491 | #include <linux/bpf_types.h> | |
3492 | #undef BPF_PROG_TYPE | |
3493 | } *__t; | |
3494 | /* 't' is written once under lock. Read many times. */ | |
3495 | const struct btf_type *t; | |
3496 | } bpf_ctx_convert; | |
3497 | enum { | |
3498 | #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \ | |
3499 | __ctx_convert##_id, | |
3500 | #include <linux/bpf_types.h> | |
3501 | #undef BPF_PROG_TYPE | |
ce27709b | 3502 | __ctx_convert_unused, /* to avoid empty enum in extreme .config */ |
91cc1a99 AS |
3503 | }; |
3504 | static u8 bpf_ctx_convert_map[] = { | |
3505 | #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \ | |
3506 | [_id] = __ctx_convert##_id, | |
3507 | #include <linux/bpf_types.h> | |
3508 | #undef BPF_PROG_TYPE | |
4c80c7bc | 3509 | 0, /* avoid empty array */ |
91cc1a99 AS |
3510 | }; |
3511 | #undef BPF_MAP_TYPE | |
f2e10bff | 3512 | #undef BPF_LINK_TYPE |
91cc1a99 AS |
3513 | |
3514 | static const struct btf_member * | |
3515 | btf_get_prog_ctx_type(struct bpf_verifier_log *log, struct btf *btf, | |
51c39bb1 AS |
3516 | const struct btf_type *t, enum bpf_prog_type prog_type, |
3517 | int arg) | |
91cc1a99 AS |
3518 | { |
3519 | const struct btf_type *conv_struct; | |
3520 | const struct btf_type *ctx_struct; | |
3521 | const struct btf_member *ctx_type; | |
3522 | const char *tname, *ctx_tname; | |
3523 | ||
3524 | conv_struct = bpf_ctx_convert.t; | |
3525 | if (!conv_struct) { | |
3526 | bpf_log(log, "btf_vmlinux is malformed\n"); | |
3527 | return NULL; | |
3528 | } | |
3529 | t = btf_type_by_id(btf, t->type); | |
3530 | while (btf_type_is_modifier(t)) | |
3531 | t = btf_type_by_id(btf, t->type); | |
3532 | if (!btf_type_is_struct(t)) { | |
3533 | /* Only pointer to struct is supported for now. | |
3534 | * That means that BPF_PROG_TYPE_TRACEPOINT with BTF | |
3535 | * is not supported yet. | |
3536 | * BPF_PROG_TYPE_RAW_TRACEPOINT is fine. | |
3537 | */ | |
51c39bb1 AS |
3538 | if (log->level & BPF_LOG_LEVEL) |
3539 | bpf_log(log, "arg#%d type is not a struct\n", arg); | |
91cc1a99 AS |
3540 | return NULL; |
3541 | } | |
3542 | tname = btf_name_by_offset(btf, t->name_off); | |
3543 | if (!tname) { | |
51c39bb1 | 3544 | bpf_log(log, "arg#%d struct doesn't have a name\n", arg); |
91cc1a99 AS |
3545 | return NULL; |
3546 | } | |
3547 | /* prog_type is valid bpf program type. No need for bounds check. */ | |
3548 | ctx_type = btf_type_member(conv_struct) + bpf_ctx_convert_map[prog_type] * 2; | |
3549 | /* ctx_struct is a pointer to prog_ctx_type in vmlinux. | |
3550 | * Like 'struct __sk_buff' | |
3551 | */ | |
3552 | ctx_struct = btf_type_by_id(btf_vmlinux, ctx_type->type); | |
3553 | if (!ctx_struct) | |
3554 | /* should not happen */ | |
3555 | return NULL; | |
3556 | ctx_tname = btf_name_by_offset(btf_vmlinux, ctx_struct->name_off); | |
3557 | if (!ctx_tname) { | |
3558 | /* should not happen */ | |
3559 | bpf_log(log, "Please fix kernel include/linux/bpf_types.h\n"); | |
3560 | return NULL; | |
3561 | } | |
3562 | /* only compare that prog's ctx type name is the same as | |
3563 | * kernel expects. No need to compare field by field. | |
3564 | * It's ok for bpf prog to do: | |
3565 | * struct __sk_buff {}; | |
3566 | * int socket_filter_bpf_prog(struct __sk_buff *skb) | |
3567 | * { // no fields of skb are ever used } | |
3568 | */ | |
3569 | if (strcmp(ctx_tname, tname)) | |
3570 | return NULL; | |
3571 | return ctx_type; | |
3572 | } | |
8580ac94 | 3573 | |
5b92a28a AS |
3574 | static int btf_translate_to_vmlinux(struct bpf_verifier_log *log, |
3575 | struct btf *btf, | |
3576 | const struct btf_type *t, | |
51c39bb1 AS |
3577 | enum bpf_prog_type prog_type, |
3578 | int arg) | |
5b92a28a AS |
3579 | { |
3580 | const struct btf_member *prog_ctx_type, *kern_ctx_type; | |
3581 | ||
51c39bb1 | 3582 | prog_ctx_type = btf_get_prog_ctx_type(log, btf, t, prog_type, arg); |
5b92a28a AS |
3583 | if (!prog_ctx_type) |
3584 | return -ENOENT; | |
3585 | kern_ctx_type = prog_ctx_type + 1; | |
3586 | return kern_ctx_type->type; | |
3587 | } | |
3588 | ||
8580ac94 AS |
3589 | struct btf *btf_parse_vmlinux(void) |
3590 | { | |
3591 | struct btf_verifier_env *env = NULL; | |
3592 | struct bpf_verifier_log *log; | |
3593 | struct btf *btf = NULL; | |
91cc1a99 | 3594 | int err, i; |
8580ac94 AS |
3595 | |
3596 | env = kzalloc(sizeof(*env), GFP_KERNEL | __GFP_NOWARN); | |
3597 | if (!env) | |
3598 | return ERR_PTR(-ENOMEM); | |
3599 | ||
3600 | log = &env->log; | |
3601 | log->level = BPF_LOG_KERNEL; | |
3602 | ||
3603 | btf = kzalloc(sizeof(*btf), GFP_KERNEL | __GFP_NOWARN); | |
3604 | if (!btf) { | |
3605 | err = -ENOMEM; | |
3606 | goto errout; | |
3607 | } | |
3608 | env->btf = btf; | |
3609 | ||
90ceddcb FS |
3610 | btf->data = __start_BTF; |
3611 | btf->data_size = __stop_BTF - __start_BTF; | |
8580ac94 AS |
3612 | |
3613 | err = btf_parse_hdr(env); | |
3614 | if (err) | |
3615 | goto errout; | |
3616 | ||
3617 | btf->nohdr_data = btf->data + btf->hdr.hdr_len; | |
3618 | ||
3619 | err = btf_parse_str_sec(env); | |
3620 | if (err) | |
3621 | goto errout; | |
3622 | ||
3623 | err = btf_check_all_metas(env); | |
3624 | if (err) | |
3625 | goto errout; | |
3626 | ||
91cc1a99 AS |
3627 | /* find struct bpf_ctx_convert for type checking later */ |
3628 | for (i = 1; i <= btf->nr_types; i++) { | |
3629 | const struct btf_type *t; | |
3630 | const char *tname; | |
3631 | ||
3632 | t = btf_type_by_id(btf, i); | |
3633 | if (!__btf_type_is_struct(t)) | |
3634 | continue; | |
3635 | tname = __btf_name_by_offset(btf, t->name_off); | |
3636 | if (!strcmp(tname, "bpf_ctx_convert")) { | |
3637 | /* btf_parse_vmlinux() runs under bpf_verifier_lock */ | |
3638 | bpf_ctx_convert.t = t; | |
3639 | break; | |
3640 | } | |
3641 | } | |
3642 | if (i > btf->nr_types) { | |
3643 | err = -ENOENT; | |
3644 | goto errout; | |
3645 | } | |
3646 | ||
d3e42bb0 | 3647 | bpf_struct_ops_init(btf, log); |
27ae7997 | 3648 | |
8580ac94 AS |
3649 | btf_verifier_env_free(env); |
3650 | refcount_set(&btf->refcnt, 1); | |
3651 | return btf; | |
3652 | ||
3653 | errout: | |
3654 | btf_verifier_env_free(env); | |
3655 | if (btf) { | |
3656 | kvfree(btf->types); | |
3657 | kfree(btf); | |
3658 | } | |
3659 | return ERR_PTR(err); | |
3660 | } | |
3661 | ||
5b92a28a AS |
3662 | struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog) |
3663 | { | |
3664 | struct bpf_prog *tgt_prog = prog->aux->linked_prog; | |
3665 | ||
3666 | if (tgt_prog) { | |
3667 | return tgt_prog->aux->btf; | |
3668 | } else { | |
3669 | return btf_vmlinux; | |
3670 | } | |
3671 | } | |
3672 | ||
84ad7a7a JO |
3673 | static bool is_string_ptr(struct btf *btf, const struct btf_type *t) |
3674 | { | |
3675 | /* t comes in already as a pointer */ | |
3676 | t = btf_type_by_id(btf, t->type); | |
3677 | ||
3678 | /* allow const */ | |
3679 | if (BTF_INFO_KIND(t->info) == BTF_KIND_CONST) | |
3680 | t = btf_type_by_id(btf, t->type); | |
3681 | ||
3682 | /* char, signed char, unsigned char */ | |
3683 | return btf_type_is_int(t) && t->size == 1; | |
3684 | } | |
3685 | ||
9e15db66 AS |
3686 | bool btf_ctx_access(int off, int size, enum bpf_access_type type, |
3687 | const struct bpf_prog *prog, | |
3688 | struct bpf_insn_access_aux *info) | |
3689 | { | |
38207291 | 3690 | const struct btf_type *t = prog->aux->attach_func_proto; |
5b92a28a AS |
3691 | struct bpf_prog *tgt_prog = prog->aux->linked_prog; |
3692 | struct btf *btf = bpf_prog_get_target_btf(prog); | |
38207291 | 3693 | const char *tname = prog->aux->attach_func_name; |
9e15db66 | 3694 | struct bpf_verifier_log *log = info->log; |
9e15db66 | 3695 | const struct btf_param *args; |
9e15db66 | 3696 | u32 nr_args, arg; |
5b92a28a | 3697 | int ret; |
9e15db66 | 3698 | |
9e15db66 | 3699 | if (off % 8) { |
38207291 | 3700 | bpf_log(log, "func '%s' offset %d is not multiple of 8\n", |
9e15db66 AS |
3701 | tname, off); |
3702 | return false; | |
3703 | } | |
3704 | arg = off / 8; | |
3705 | args = (const struct btf_param *)(t + 1); | |
5b92a28a AS |
3706 | /* if (t == NULL) Fall back to default BPF prog with 5 u64 arguments */ |
3707 | nr_args = t ? btf_type_vlen(t) : 5; | |
38207291 MKL |
3708 | if (prog->aux->attach_btf_trace) { |
3709 | /* skip first 'void *__data' argument in btf_trace_##name typedef */ | |
3710 | args++; | |
3711 | nr_args--; | |
3712 | } | |
fec56f58 | 3713 | |
f50b49a0 KS |
3714 | if (arg > nr_args) { |
3715 | bpf_log(log, "func '%s' doesn't have %d-th argument\n", | |
3716 | tname, arg + 1); | |
3717 | return false; | |
3718 | } | |
3719 | ||
6ba43b76 | 3720 | if (arg == nr_args) { |
f50b49a0 KS |
3721 | switch (prog->expected_attach_type) { |
3722 | case BPF_LSM_MAC: | |
3723 | case BPF_TRACE_FEXIT: | |
9e4e01df KS |
3724 | /* When LSM programs are attached to void LSM hooks |
3725 | * they use FEXIT trampolines and when attached to | |
3726 | * int LSM hooks, they use MODIFY_RETURN trampolines. | |
3727 | * | |
3728 | * While the LSM programs are BPF_MODIFY_RETURN-like | |
3729 | * the check: | |
3730 | * | |
3731 | * if (ret_type != 'int') | |
3732 | * return -EINVAL; | |
3733 | * | |
3734 | * is _not_ done here. This is still safe as LSM hooks | |
3735 | * have only void and int return types. | |
3736 | */ | |
6ba43b76 KS |
3737 | if (!t) |
3738 | return true; | |
3739 | t = btf_type_by_id(btf, t->type); | |
f50b49a0 KS |
3740 | break; |
3741 | case BPF_MODIFY_RETURN: | |
6ba43b76 KS |
3742 | /* For now the BPF_MODIFY_RETURN can only be attached to |
3743 | * functions that return an int. | |
3744 | */ | |
3745 | if (!t) | |
3746 | return false; | |
3747 | ||
3748 | t = btf_type_skip_modifiers(btf, t->type, NULL); | |
3749 | if (!btf_type_is_int(t)) { | |
3750 | bpf_log(log, | |
3751 | "ret type %s not allowed for fmod_ret\n", | |
3752 | btf_kind_str[BTF_INFO_KIND(t->info)]); | |
3753 | return false; | |
3754 | } | |
f50b49a0 KS |
3755 | break; |
3756 | default: | |
3757 | bpf_log(log, "func '%s' doesn't have %d-th argument\n", | |
3758 | tname, arg + 1); | |
3759 | return false; | |
6ba43b76 | 3760 | } |
fec56f58 | 3761 | } else { |
5b92a28a AS |
3762 | if (!t) |
3763 | /* Default prog with 5 args */ | |
3764 | return true; | |
3765 | t = btf_type_by_id(btf, args[arg].type); | |
9e15db66 | 3766 | } |
f50b49a0 | 3767 | |
9e15db66 AS |
3768 | /* skip modifiers */ |
3769 | while (btf_type_is_modifier(t)) | |
5b92a28a | 3770 | t = btf_type_by_id(btf, t->type); |
218b3f65 | 3771 | if (btf_type_is_int(t) || btf_type_is_enum(t)) |
9e15db66 AS |
3772 | /* accessing a scalar */ |
3773 | return true; | |
3774 | if (!btf_type_is_ptr(t)) { | |
3775 | bpf_log(log, | |
38207291 | 3776 | "func '%s' arg%d '%s' has type %s. Only pointer access is allowed\n", |
9e15db66 | 3777 | tname, arg, |
5b92a28a | 3778 | __btf_name_by_offset(btf, t->name_off), |
9e15db66 AS |
3779 | btf_kind_str[BTF_INFO_KIND(t->info)]); |
3780 | return false; | |
3781 | } | |
3782 | if (t->type == 0) | |
3783 | /* This is a pointer to void. | |
3784 | * It is the same as scalar from the verifier safety pov. | |
3785 | * No further pointer walking is allowed. | |
3786 | */ | |
3787 | return true; | |
3788 | ||
84ad7a7a JO |
3789 | if (is_string_ptr(btf, t)) |
3790 | return true; | |
3791 | ||
9e15db66 | 3792 | /* this is a pointer to another type */ |
b121b341 YS |
3793 | if (off != 0 && prog->aux->btf_id_or_null_non0_off) |
3794 | info->reg_type = PTR_TO_BTF_ID_OR_NULL; | |
3795 | else | |
3796 | info->reg_type = PTR_TO_BTF_ID; | |
9e15db66 | 3797 | |
5b92a28a | 3798 | if (tgt_prog) { |
51c39bb1 | 3799 | ret = btf_translate_to_vmlinux(log, btf, t, tgt_prog->type, arg); |
5b92a28a AS |
3800 | if (ret > 0) { |
3801 | info->btf_id = ret; | |
3802 | return true; | |
3803 | } else { | |
3804 | return false; | |
3805 | } | |
3806 | } | |
275517ff MKL |
3807 | |
3808 | info->btf_id = t->type; | |
5b92a28a | 3809 | t = btf_type_by_id(btf, t->type); |
9e15db66 | 3810 | /* skip modifiers */ |
275517ff MKL |
3811 | while (btf_type_is_modifier(t)) { |
3812 | info->btf_id = t->type; | |
5b92a28a | 3813 | t = btf_type_by_id(btf, t->type); |
275517ff | 3814 | } |
9e15db66 AS |
3815 | if (!btf_type_is_struct(t)) { |
3816 | bpf_log(log, | |
38207291 | 3817 | "func '%s' arg%d type %s is not a struct\n", |
9e15db66 AS |
3818 | tname, arg, btf_kind_str[BTF_INFO_KIND(t->info)]); |
3819 | return false; | |
3820 | } | |
38207291 | 3821 | bpf_log(log, "func '%s' arg%d has btf_id %d type %s '%s'\n", |
9e15db66 | 3822 | tname, arg, info->btf_id, btf_kind_str[BTF_INFO_KIND(t->info)], |
5b92a28a | 3823 | __btf_name_by_offset(btf, t->name_off)); |
9e15db66 AS |
3824 | return true; |
3825 | } | |
3826 | ||
3827 | int btf_struct_access(struct bpf_verifier_log *log, | |
3828 | const struct btf_type *t, int off, int size, | |
3829 | enum bpf_access_type atype, | |
3830 | u32 *next_btf_id) | |
3831 | { | |
7e3617a7 MKL |
3832 | u32 i, moff, mtrue_end, msize = 0, total_nelems = 0; |
3833 | const struct btf_type *mtype, *elem_type = NULL; | |
9e15db66 | 3834 | const struct btf_member *member; |
9e15db66 | 3835 | const char *tname, *mname; |
9e15db66 AS |
3836 | |
3837 | again: | |
3838 | tname = __btf_name_by_offset(btf_vmlinux, t->name_off); | |
3839 | if (!btf_type_is_struct(t)) { | |
275517ff | 3840 | bpf_log(log, "Type '%s' is not a struct\n", tname); |
9e15db66 AS |
3841 | return -EINVAL; |
3842 | } | |
3843 | ||
976aba00 MKL |
3844 | if (off + size > t->size) { |
3845 | bpf_log(log, "access beyond struct %s at off %u size %u\n", | |
3846 | tname, off, size); | |
3847 | return -EACCES; | |
3848 | } | |
9e15db66 | 3849 | |
976aba00 | 3850 | for_each_member(i, t, member) { |
7e3617a7 MKL |
3851 | /* offset of the field in bytes */ |
3852 | moff = btf_member_bit_offset(t, member) / 8; | |
3853 | if (off + size <= moff) | |
9e15db66 AS |
3854 | /* won't find anything, field is already too far */ |
3855 | break; | |
976aba00 MKL |
3856 | |
3857 | if (btf_member_bitfield_size(t, member)) { | |
3858 | u32 end_bit = btf_member_bit_offset(t, member) + | |
3859 | btf_member_bitfield_size(t, member); | |
3860 | ||
3861 | /* off <= moff instead of off == moff because clang | |
3862 | * does not generate a BTF member for anonymous | |
3863 | * bitfield like the ":16" here: | |
3864 | * struct { | |
3865 | * int :16; | |
3866 | * int x:8; | |
3867 | * }; | |
3868 | */ | |
3869 | if (off <= moff && | |
3870 | BITS_ROUNDUP_BYTES(end_bit) <= off + size) | |
3871 | return SCALAR_VALUE; | |
3872 | ||
3873 | /* off may be accessing a following member | |
3874 | * | |
3875 | * or | |
3876 | * | |
3877 | * Doing partial access at either end of this | |
3878 | * bitfield. Continue on this case also to | |
3879 | * treat it as not accessing this bitfield | |
3880 | * and eventually error out as field not | |
3881 | * found to keep it simple. | |
3882 | * It could be relaxed if there was a legit | |
3883 | * partial access case later. | |
3884 | */ | |
3885 | continue; | |
3886 | } | |
3887 | ||
7e3617a7 MKL |
3888 | /* In case of "off" is pointing to holes of a struct */ |
3889 | if (off < moff) | |
976aba00 | 3890 | break; |
9e15db66 AS |
3891 | |
3892 | /* type of the field */ | |
3893 | mtype = btf_type_by_id(btf_vmlinux, member->type); | |
3894 | mname = __btf_name_by_offset(btf_vmlinux, member->name_off); | |
3895 | ||
7e3617a7 MKL |
3896 | mtype = btf_resolve_size(btf_vmlinux, mtype, &msize, |
3897 | &elem_type, &total_nelems); | |
3898 | if (IS_ERR(mtype)) { | |
9e15db66 AS |
3899 | bpf_log(log, "field %s doesn't have size\n", mname); |
3900 | return -EFAULT; | |
3901 | } | |
7e3617a7 MKL |
3902 | |
3903 | mtrue_end = moff + msize; | |
3904 | if (off >= mtrue_end) | |
9e15db66 AS |
3905 | /* no overlap with member, keep iterating */ |
3906 | continue; | |
7e3617a7 MKL |
3907 | |
3908 | if (btf_type_is_array(mtype)) { | |
3909 | u32 elem_idx; | |
3910 | ||
3911 | /* btf_resolve_size() above helps to | |
3912 | * linearize a multi-dimensional array. | |
3913 | * | |
3914 | * The logic here is treating an array | |
3915 | * in a struct as the following way: | |
3916 | * | |
3917 | * struct outer { | |
3918 | * struct inner array[2][2]; | |
3919 | * }; | |
3920 | * | |
3921 | * looks like: | |
3922 | * | |
3923 | * struct outer { | |
3924 | * struct inner array_elem0; | |
3925 | * struct inner array_elem1; | |
3926 | * struct inner array_elem2; | |
3927 | * struct inner array_elem3; | |
3928 | * }; | |
3929 | * | |
3930 | * When accessing outer->array[1][0], it moves | |
3931 | * moff to "array_elem2", set mtype to | |
3932 | * "struct inner", and msize also becomes | |
3933 | * sizeof(struct inner). Then most of the | |
3934 | * remaining logic will fall through without | |
3935 | * caring the current member is an array or | |
3936 | * not. | |
3937 | * | |
3938 | * Unlike mtype/msize/moff, mtrue_end does not | |
3939 | * change. The naming difference ("_true") tells | |
3940 | * that it is not always corresponding to | |
3941 | * the current mtype/msize/moff. | |
3942 | * It is the true end of the current | |
3943 | * member (i.e. array in this case). That | |
3944 | * will allow an int array to be accessed like | |
3945 | * a scratch space, | |
3946 | * i.e. allow access beyond the size of | |
3947 | * the array's element as long as it is | |
3948 | * within the mtrue_end boundary. | |
3949 | */ | |
3950 | ||
3951 | /* skip empty array */ | |
3952 | if (moff == mtrue_end) | |
3953 | continue; | |
3954 | ||
3955 | msize /= total_nelems; | |
3956 | elem_idx = (off - moff) / msize; | |
3957 | moff += elem_idx * msize; | |
3958 | mtype = elem_type; | |
3959 | } | |
3960 | ||
9e15db66 AS |
3961 | /* the 'off' we're looking for is either equal to start |
3962 | * of this field or inside of this struct | |
3963 | */ | |
3964 | if (btf_type_is_struct(mtype)) { | |
3965 | /* our field must be inside that union or struct */ | |
3966 | t = mtype; | |
3967 | ||
3968 | /* adjust offset we're looking for */ | |
7e3617a7 | 3969 | off -= moff; |
9e15db66 AS |
3970 | goto again; |
3971 | } | |
9e15db66 AS |
3972 | |
3973 | if (btf_type_is_ptr(mtype)) { | |
3974 | const struct btf_type *stype; | |
257af63d | 3975 | u32 id; |
9e15db66 | 3976 | |
7e3617a7 MKL |
3977 | if (msize != size || off != moff) { |
3978 | bpf_log(log, | |
3979 | "cannot access ptr member %s with moff %u in struct %s with off %u size %u\n", | |
3980 | mname, moff, tname, off, size); | |
3981 | return -EACCES; | |
3982 | } | |
3983 | ||
257af63d | 3984 | stype = btf_type_skip_modifiers(btf_vmlinux, mtype->type, &id); |
9e15db66 | 3985 | if (btf_type_is_struct(stype)) { |
257af63d | 3986 | *next_btf_id = id; |
9e15db66 AS |
3987 | return PTR_TO_BTF_ID; |
3988 | } | |
3989 | } | |
7e3617a7 MKL |
3990 | |
3991 | /* Allow more flexible access within an int as long as | |
3992 | * it is within mtrue_end. | |
3993 | * Since mtrue_end could be the end of an array, | |
3994 | * that also allows using an array of int as a scratch | |
3995 | * space. e.g. skb->cb[]. | |
3996 | */ | |
3997 | if (off + size > mtrue_end) { | |
3998 | bpf_log(log, | |
3999 | "access beyond the end of member %s (mend:%u) in struct %s with off %u size %u\n", | |
4000 | mname, mtrue_end, tname, off, size); | |
4001 | return -EACCES; | |
4002 | } | |
4003 | ||
9e15db66 AS |
4004 | return SCALAR_VALUE; |
4005 | } | |
4006 | bpf_log(log, "struct %s doesn't have field at offset %d\n", tname, off); | |
4007 | return -EINVAL; | |
4008 | } | |
4009 | ||
9cc31b3a AS |
4010 | static int __btf_resolve_helper_id(struct bpf_verifier_log *log, void *fn, |
4011 | int arg) | |
a7658e1a AS |
4012 | { |
4013 | char fnname[KSYM_SYMBOL_LEN + 4] = "btf_"; | |
4014 | const struct btf_param *args; | |
4015 | const struct btf_type *t; | |
4016 | const char *tname, *sym; | |
4017 | u32 btf_id, i; | |
4018 | ||
4019 | if (IS_ERR(btf_vmlinux)) { | |
4020 | bpf_log(log, "btf_vmlinux is malformed\n"); | |
4021 | return -EINVAL; | |
4022 | } | |
4023 | ||
4024 | sym = kallsyms_lookup((long)fn, NULL, NULL, NULL, fnname + 4); | |
4025 | if (!sym) { | |
4026 | bpf_log(log, "kernel doesn't have kallsyms\n"); | |
4027 | return -EFAULT; | |
4028 | } | |
4029 | ||
4030 | for (i = 1; i <= btf_vmlinux->nr_types; i++) { | |
4031 | t = btf_type_by_id(btf_vmlinux, i); | |
4032 | if (BTF_INFO_KIND(t->info) != BTF_KIND_TYPEDEF) | |
4033 | continue; | |
4034 | tname = __btf_name_by_offset(btf_vmlinux, t->name_off); | |
4035 | if (!strcmp(tname, fnname)) | |
4036 | break; | |
4037 | } | |
4038 | if (i > btf_vmlinux->nr_types) { | |
4039 | bpf_log(log, "helper %s type is not found\n", fnname); | |
4040 | return -ENOENT; | |
4041 | } | |
4042 | ||
4043 | t = btf_type_by_id(btf_vmlinux, t->type); | |
4044 | if (!btf_type_is_ptr(t)) | |
4045 | return -EFAULT; | |
4046 | t = btf_type_by_id(btf_vmlinux, t->type); | |
4047 | if (!btf_type_is_func_proto(t)) | |
4048 | return -EFAULT; | |
4049 | ||
4050 | args = (const struct btf_param *)(t + 1); | |
4051 | if (arg >= btf_type_vlen(t)) { | |
4052 | bpf_log(log, "bpf helper %s doesn't have %d-th argument\n", | |
4053 | fnname, arg); | |
4054 | return -EINVAL; | |
4055 | } | |
4056 | ||
4057 | t = btf_type_by_id(btf_vmlinux, args[arg].type); | |
4058 | if (!btf_type_is_ptr(t) || !t->type) { | |
4059 | /* anything but the pointer to struct is a helper config bug */ | |
4060 | bpf_log(log, "ARG_PTR_TO_BTF is misconfigured\n"); | |
4061 | return -EFAULT; | |
4062 | } | |
4063 | btf_id = t->type; | |
4064 | t = btf_type_by_id(btf_vmlinux, t->type); | |
4065 | /* skip modifiers */ | |
4066 | while (btf_type_is_modifier(t)) { | |
4067 | btf_id = t->type; | |
4068 | t = btf_type_by_id(btf_vmlinux, t->type); | |
4069 | } | |
4070 | if (!btf_type_is_struct(t)) { | |
4071 | bpf_log(log, "ARG_PTR_TO_BTF is not a struct\n"); | |
4072 | return -EFAULT; | |
4073 | } | |
4074 | bpf_log(log, "helper %s arg%d has btf_id %d struct %s\n", fnname + 4, | |
4075 | arg, btf_id, __btf_name_by_offset(btf_vmlinux, t->name_off)); | |
4076 | return btf_id; | |
4077 | } | |
4078 | ||
9cc31b3a AS |
4079 | int btf_resolve_helper_id(struct bpf_verifier_log *log, |
4080 | const struct bpf_func_proto *fn, int arg) | |
4081 | { | |
4082 | int *btf_id = &fn->btf_id[arg]; | |
4083 | int ret; | |
4084 | ||
4085 | if (fn->arg_type[arg] != ARG_PTR_TO_BTF_ID) | |
4086 | return -EINVAL; | |
4087 | ||
4088 | ret = READ_ONCE(*btf_id); | |
4089 | if (ret) | |
4090 | return ret; | |
4091 | /* ok to race the search. The result is the same */ | |
4092 | ret = __btf_resolve_helper_id(log, fn->func, arg); | |
4093 | if (!ret) { | |
4094 | /* Function argument cannot be type 'void' */ | |
4095 | bpf_log(log, "BTF resolution bug\n"); | |
4096 | return -EFAULT; | |
4097 | } | |
4098 | WRITE_ONCE(*btf_id, ret); | |
4099 | return ret; | |
4100 | } | |
4101 | ||
fec56f58 AS |
4102 | static int __get_type_size(struct btf *btf, u32 btf_id, |
4103 | const struct btf_type **bad_type) | |
4104 | { | |
4105 | const struct btf_type *t; | |
4106 | ||
4107 | if (!btf_id) | |
4108 | /* void */ | |
4109 | return 0; | |
4110 | t = btf_type_by_id(btf, btf_id); | |
4111 | while (t && btf_type_is_modifier(t)) | |
4112 | t = btf_type_by_id(btf, t->type); | |
d0f01043 AS |
4113 | if (!t) { |
4114 | *bad_type = btf->types[0]; | |
fec56f58 | 4115 | return -EINVAL; |
d0f01043 | 4116 | } |
fec56f58 AS |
4117 | if (btf_type_is_ptr(t)) |
4118 | /* kernel size of pointer. Not BPF's size of pointer*/ | |
4119 | return sizeof(void *); | |
4120 | if (btf_type_is_int(t) || btf_type_is_enum(t)) | |
4121 | return t->size; | |
4122 | *bad_type = t; | |
4123 | return -EINVAL; | |
4124 | } | |
4125 | ||
4126 | int btf_distill_func_proto(struct bpf_verifier_log *log, | |
4127 | struct btf *btf, | |
4128 | const struct btf_type *func, | |
4129 | const char *tname, | |
4130 | struct btf_func_model *m) | |
4131 | { | |
4132 | const struct btf_param *args; | |
4133 | const struct btf_type *t; | |
4134 | u32 i, nargs; | |
4135 | int ret; | |
4136 | ||
5b92a28a AS |
4137 | if (!func) { |
4138 | /* BTF function prototype doesn't match the verifier types. | |
4139 | * Fall back to 5 u64 args. | |
4140 | */ | |
4141 | for (i = 0; i < 5; i++) | |
4142 | m->arg_size[i] = 8; | |
4143 | m->ret_size = 8; | |
4144 | m->nr_args = 5; | |
4145 | return 0; | |
4146 | } | |
fec56f58 AS |
4147 | args = (const struct btf_param *)(func + 1); |
4148 | nargs = btf_type_vlen(func); | |
4149 | if (nargs >= MAX_BPF_FUNC_ARGS) { | |
4150 | bpf_log(log, | |
4151 | "The function %s has %d arguments. Too many.\n", | |
4152 | tname, nargs); | |
4153 | return -EINVAL; | |
4154 | } | |
4155 | ret = __get_type_size(btf, func->type, &t); | |
4156 | if (ret < 0) { | |
4157 | bpf_log(log, | |
4158 | "The function %s return type %s is unsupported.\n", | |
4159 | tname, btf_kind_str[BTF_INFO_KIND(t->info)]); | |
4160 | return -EINVAL; | |
4161 | } | |
4162 | m->ret_size = ret; | |
4163 | ||
4164 | for (i = 0; i < nargs; i++) { | |
4165 | ret = __get_type_size(btf, args[i].type, &t); | |
4166 | if (ret < 0) { | |
4167 | bpf_log(log, | |
4168 | "The function %s arg%d type %s is unsupported.\n", | |
4169 | tname, i, btf_kind_str[BTF_INFO_KIND(t->info)]); | |
4170 | return -EINVAL; | |
4171 | } | |
4172 | m->arg_size[i] = ret; | |
4173 | } | |
4174 | m->nr_args = nargs; | |
4175 | return 0; | |
4176 | } | |
4177 | ||
be8704ff AS |
4178 | /* Compare BTFs of two functions assuming only scalars and pointers to context. |
4179 | * t1 points to BTF_KIND_FUNC in btf1 | |
4180 | * t2 points to BTF_KIND_FUNC in btf2 | |
4181 | * Returns: | |
4182 | * EINVAL - function prototype mismatch | |
4183 | * EFAULT - verifier bug | |
4184 | * 0 - 99% match. The last 1% is validated by the verifier. | |
4185 | */ | |
2bf0eb9b HY |
4186 | static int btf_check_func_type_match(struct bpf_verifier_log *log, |
4187 | struct btf *btf1, const struct btf_type *t1, | |
4188 | struct btf *btf2, const struct btf_type *t2) | |
be8704ff AS |
4189 | { |
4190 | const struct btf_param *args1, *args2; | |
4191 | const char *fn1, *fn2, *s1, *s2; | |
4192 | u32 nargs1, nargs2, i; | |
4193 | ||
4194 | fn1 = btf_name_by_offset(btf1, t1->name_off); | |
4195 | fn2 = btf_name_by_offset(btf2, t2->name_off); | |
4196 | ||
4197 | if (btf_func_linkage(t1) != BTF_FUNC_GLOBAL) { | |
4198 | bpf_log(log, "%s() is not a global function\n", fn1); | |
4199 | return -EINVAL; | |
4200 | } | |
4201 | if (btf_func_linkage(t2) != BTF_FUNC_GLOBAL) { | |
4202 | bpf_log(log, "%s() is not a global function\n", fn2); | |
4203 | return -EINVAL; | |
4204 | } | |
4205 | ||
4206 | t1 = btf_type_by_id(btf1, t1->type); | |
4207 | if (!t1 || !btf_type_is_func_proto(t1)) | |
4208 | return -EFAULT; | |
4209 | t2 = btf_type_by_id(btf2, t2->type); | |
4210 | if (!t2 || !btf_type_is_func_proto(t2)) | |
4211 | return -EFAULT; | |
4212 | ||
4213 | args1 = (const struct btf_param *)(t1 + 1); | |
4214 | nargs1 = btf_type_vlen(t1); | |
4215 | args2 = (const struct btf_param *)(t2 + 1); | |
4216 | nargs2 = btf_type_vlen(t2); | |
4217 | ||
4218 | if (nargs1 != nargs2) { | |
4219 | bpf_log(log, "%s() has %d args while %s() has %d args\n", | |
4220 | fn1, nargs1, fn2, nargs2); | |
4221 | return -EINVAL; | |
4222 | } | |
4223 | ||
4224 | t1 = btf_type_skip_modifiers(btf1, t1->type, NULL); | |
4225 | t2 = btf_type_skip_modifiers(btf2, t2->type, NULL); | |
4226 | if (t1->info != t2->info) { | |
4227 | bpf_log(log, | |
4228 | "Return type %s of %s() doesn't match type %s of %s()\n", | |
4229 | btf_type_str(t1), fn1, | |
4230 | btf_type_str(t2), fn2); | |
4231 | return -EINVAL; | |
4232 | } | |
4233 | ||
4234 | for (i = 0; i < nargs1; i++) { | |
4235 | t1 = btf_type_skip_modifiers(btf1, args1[i].type, NULL); | |
4236 | t2 = btf_type_skip_modifiers(btf2, args2[i].type, NULL); | |
4237 | ||
4238 | if (t1->info != t2->info) { | |
4239 | bpf_log(log, "arg%d in %s() is %s while %s() has %s\n", | |
4240 | i, fn1, btf_type_str(t1), | |
4241 | fn2, btf_type_str(t2)); | |
4242 | return -EINVAL; | |
4243 | } | |
4244 | if (btf_type_has_size(t1) && t1->size != t2->size) { | |
4245 | bpf_log(log, | |
4246 | "arg%d in %s() has size %d while %s() has %d\n", | |
4247 | i, fn1, t1->size, | |
4248 | fn2, t2->size); | |
4249 | return -EINVAL; | |
4250 | } | |
4251 | ||
4252 | /* global functions are validated with scalars and pointers | |
4253 | * to context only. And only global functions can be replaced. | |
4254 | * Hence type check only those types. | |
4255 | */ | |
4256 | if (btf_type_is_int(t1) || btf_type_is_enum(t1)) | |
4257 | continue; | |
4258 | if (!btf_type_is_ptr(t1)) { | |
4259 | bpf_log(log, | |
4260 | "arg%d in %s() has unrecognized type\n", | |
4261 | i, fn1); | |
4262 | return -EINVAL; | |
4263 | } | |
4264 | t1 = btf_type_skip_modifiers(btf1, t1->type, NULL); | |
4265 | t2 = btf_type_skip_modifiers(btf2, t2->type, NULL); | |
4266 | if (!btf_type_is_struct(t1)) { | |
4267 | bpf_log(log, | |
4268 | "arg%d in %s() is not a pointer to context\n", | |
4269 | i, fn1); | |
4270 | return -EINVAL; | |
4271 | } | |
4272 | if (!btf_type_is_struct(t2)) { | |
4273 | bpf_log(log, | |
4274 | "arg%d in %s() is not a pointer to context\n", | |
4275 | i, fn2); | |
4276 | return -EINVAL; | |
4277 | } | |
4278 | /* This is an optional check to make program writing easier. | |
4279 | * Compare names of structs and report an error to the user. | |
4280 | * btf_prepare_func_args() already checked that t2 struct | |
4281 | * is a context type. btf_prepare_func_args() will check | |
4282 | * later that t1 struct is a context type as well. | |
4283 | */ | |
4284 | s1 = btf_name_by_offset(btf1, t1->name_off); | |
4285 | s2 = btf_name_by_offset(btf2, t2->name_off); | |
4286 | if (strcmp(s1, s2)) { | |
4287 | bpf_log(log, | |
4288 | "arg%d %s(struct %s *) doesn't match %s(struct %s *)\n", | |
4289 | i, fn1, s1, fn2, s2); | |
4290 | return -EINVAL; | |
4291 | } | |
4292 | } | |
4293 | return 0; | |
4294 | } | |
4295 | ||
4296 | /* Compare BTFs of given program with BTF of target program */ | |
4297 | int btf_check_type_match(struct bpf_verifier_env *env, struct bpf_prog *prog, | |
4298 | struct btf *btf2, const struct btf_type *t2) | |
4299 | { | |
4300 | struct btf *btf1 = prog->aux->btf; | |
4301 | const struct btf_type *t1; | |
4302 | u32 btf_id = 0; | |
4303 | ||
4304 | if (!prog->aux->func_info) { | |
4305 | bpf_log(&env->log, "Program extension requires BTF\n"); | |
4306 | return -EINVAL; | |
4307 | } | |
4308 | ||
4309 | btf_id = prog->aux->func_info[0].type_id; | |
4310 | if (!btf_id) | |
4311 | return -EFAULT; | |
4312 | ||
4313 | t1 = btf_type_by_id(btf1, btf_id); | |
4314 | if (!t1 || !btf_type_is_func(t1)) | |
4315 | return -EFAULT; | |
4316 | ||
4317 | return btf_check_func_type_match(&env->log, btf1, t1, btf2, t2); | |
4318 | } | |
4319 | ||
51c39bb1 AS |
4320 | /* Compare BTF of a function with given bpf_reg_state. |
4321 | * Returns: | |
4322 | * EFAULT - there is a verifier bug. Abort verification. | |
4323 | * EINVAL - there is a type mismatch or BTF is not available. | |
4324 | * 0 - BTF matches with what bpf_reg_state expects. | |
4325 | * Only PTR_TO_CTX and SCALAR_VALUE states are recognized. | |
4326 | */ | |
4327 | int btf_check_func_arg_match(struct bpf_verifier_env *env, int subprog, | |
4328 | struct bpf_reg_state *reg) | |
8c1b6e69 | 4329 | { |
8c1b6e69 AS |
4330 | struct bpf_verifier_log *log = &env->log; |
4331 | struct bpf_prog *prog = env->prog; | |
4332 | struct btf *btf = prog->aux->btf; | |
4333 | const struct btf_param *args; | |
4334 | const struct btf_type *t; | |
4335 | u32 i, nargs, btf_id; | |
4336 | const char *tname; | |
4337 | ||
4338 | if (!prog->aux->func_info) | |
51c39bb1 | 4339 | return -EINVAL; |
8c1b6e69 AS |
4340 | |
4341 | btf_id = prog->aux->func_info[subprog].type_id; | |
4342 | if (!btf_id) | |
51c39bb1 | 4343 | return -EFAULT; |
8c1b6e69 AS |
4344 | |
4345 | if (prog->aux->func_info_aux[subprog].unreliable) | |
51c39bb1 | 4346 | return -EINVAL; |
8c1b6e69 AS |
4347 | |
4348 | t = btf_type_by_id(btf, btf_id); | |
4349 | if (!t || !btf_type_is_func(t)) { | |
51c39bb1 AS |
4350 | /* These checks were already done by the verifier while loading |
4351 | * struct bpf_func_info | |
4352 | */ | |
4353 | bpf_log(log, "BTF of func#%d doesn't point to KIND_FUNC\n", | |
8c1b6e69 | 4354 | subprog); |
51c39bb1 | 4355 | return -EFAULT; |
8c1b6e69 AS |
4356 | } |
4357 | tname = btf_name_by_offset(btf, t->name_off); | |
4358 | ||
4359 | t = btf_type_by_id(btf, t->type); | |
4360 | if (!t || !btf_type_is_func_proto(t)) { | |
51c39bb1 AS |
4361 | bpf_log(log, "Invalid BTF of func %s\n", tname); |
4362 | return -EFAULT; | |
8c1b6e69 AS |
4363 | } |
4364 | args = (const struct btf_param *)(t + 1); | |
4365 | nargs = btf_type_vlen(t); | |
4366 | if (nargs > 5) { | |
4367 | bpf_log(log, "Function %s has %d > 5 args\n", tname, nargs); | |
4368 | goto out; | |
4369 | } | |
4370 | /* check that BTF function arguments match actual types that the | |
4371 | * verifier sees. | |
4372 | */ | |
4373 | for (i = 0; i < nargs; i++) { | |
4374 | t = btf_type_by_id(btf, args[i].type); | |
4375 | while (btf_type_is_modifier(t)) | |
4376 | t = btf_type_by_id(btf, t->type); | |
4377 | if (btf_type_is_int(t) || btf_type_is_enum(t)) { | |
4378 | if (reg[i + 1].type == SCALAR_VALUE) | |
4379 | continue; | |
4380 | bpf_log(log, "R%d is not a scalar\n", i + 1); | |
4381 | goto out; | |
4382 | } | |
4383 | if (btf_type_is_ptr(t)) { | |
4384 | if (reg[i + 1].type == SCALAR_VALUE) { | |
4385 | bpf_log(log, "R%d is not a pointer\n", i + 1); | |
4386 | goto out; | |
4387 | } | |
51c39bb1 AS |
4388 | /* If function expects ctx type in BTF check that caller |
4389 | * is passing PTR_TO_CTX. | |
8c1b6e69 | 4390 | */ |
51c39bb1 AS |
4391 | if (btf_get_prog_ctx_type(log, btf, t, prog->type, i)) { |
4392 | if (reg[i + 1].type != PTR_TO_CTX) { | |
4393 | bpf_log(log, | |
4394 | "arg#%d expected pointer to ctx, but got %s\n", | |
4395 | i, btf_kind_str[BTF_INFO_KIND(t->info)]); | |
4396 | goto out; | |
4397 | } | |
4398 | if (check_ctx_reg(env, ®[i + 1], i + 1)) | |
4399 | goto out; | |
4400 | continue; | |
4401 | } | |
8c1b6e69 | 4402 | } |
51c39bb1 AS |
4403 | bpf_log(log, "Unrecognized arg#%d type %s\n", |
4404 | i, btf_kind_str[BTF_INFO_KIND(t->info)]); | |
8c1b6e69 AS |
4405 | goto out; |
4406 | } | |
4407 | return 0; | |
4408 | out: | |
51c39bb1 AS |
4409 | /* Compiler optimizations can remove arguments from static functions |
4410 | * or mismatched type can be passed into a global function. | |
4411 | * In such cases mark the function as unreliable from BTF point of view. | |
4412 | */ | |
8c1b6e69 | 4413 | prog->aux->func_info_aux[subprog].unreliable = true; |
51c39bb1 AS |
4414 | return -EINVAL; |
4415 | } | |
4416 | ||
4417 | /* Convert BTF of a function into bpf_reg_state if possible | |
4418 | * Returns: | |
4419 | * EFAULT - there is a verifier bug. Abort verification. | |
4420 | * EINVAL - cannot convert BTF. | |
4421 | * 0 - Successfully converted BTF into bpf_reg_state | |
4422 | * (either PTR_TO_CTX or SCALAR_VALUE). | |
4423 | */ | |
4424 | int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog, | |
4425 | struct bpf_reg_state *reg) | |
4426 | { | |
4427 | struct bpf_verifier_log *log = &env->log; | |
4428 | struct bpf_prog *prog = env->prog; | |
be8704ff | 4429 | enum bpf_prog_type prog_type = prog->type; |
51c39bb1 AS |
4430 | struct btf *btf = prog->aux->btf; |
4431 | const struct btf_param *args; | |
4432 | const struct btf_type *t; | |
4433 | u32 i, nargs, btf_id; | |
4434 | const char *tname; | |
4435 | ||
4436 | if (!prog->aux->func_info || | |
4437 | prog->aux->func_info_aux[subprog].linkage != BTF_FUNC_GLOBAL) { | |
4438 | bpf_log(log, "Verifier bug\n"); | |
4439 | return -EFAULT; | |
4440 | } | |
4441 | ||
4442 | btf_id = prog->aux->func_info[subprog].type_id; | |
4443 | if (!btf_id) { | |
4444 | bpf_log(log, "Global functions need valid BTF\n"); | |
4445 | return -EFAULT; | |
4446 | } | |
4447 | ||
4448 | t = btf_type_by_id(btf, btf_id); | |
4449 | if (!t || !btf_type_is_func(t)) { | |
4450 | /* These checks were already done by the verifier while loading | |
4451 | * struct bpf_func_info | |
4452 | */ | |
4453 | bpf_log(log, "BTF of func#%d doesn't point to KIND_FUNC\n", | |
4454 | subprog); | |
4455 | return -EFAULT; | |
4456 | } | |
4457 | tname = btf_name_by_offset(btf, t->name_off); | |
4458 | ||
4459 | if (log->level & BPF_LOG_LEVEL) | |
4460 | bpf_log(log, "Validating %s() func#%d...\n", | |
4461 | tname, subprog); | |
4462 | ||
4463 | if (prog->aux->func_info_aux[subprog].unreliable) { | |
4464 | bpf_log(log, "Verifier bug in function %s()\n", tname); | |
4465 | return -EFAULT; | |
4466 | } | |
be8704ff AS |
4467 | if (prog_type == BPF_PROG_TYPE_EXT) |
4468 | prog_type = prog->aux->linked_prog->type; | |
51c39bb1 AS |
4469 | |
4470 | t = btf_type_by_id(btf, t->type); | |
4471 | if (!t || !btf_type_is_func_proto(t)) { | |
4472 | bpf_log(log, "Invalid type of function %s()\n", tname); | |
4473 | return -EFAULT; | |
4474 | } | |
4475 | args = (const struct btf_param *)(t + 1); | |
4476 | nargs = btf_type_vlen(t); | |
4477 | if (nargs > 5) { | |
4478 | bpf_log(log, "Global function %s() with %d > 5 args. Buggy compiler.\n", | |
4479 | tname, nargs); | |
4480 | return -EINVAL; | |
4481 | } | |
4482 | /* check that function returns int */ | |
4483 | t = btf_type_by_id(btf, t->type); | |
4484 | while (btf_type_is_modifier(t)) | |
4485 | t = btf_type_by_id(btf, t->type); | |
4486 | if (!btf_type_is_int(t) && !btf_type_is_enum(t)) { | |
4487 | bpf_log(log, | |
4488 | "Global function %s() doesn't return scalar. Only those are supported.\n", | |
4489 | tname); | |
4490 | return -EINVAL; | |
4491 | } | |
4492 | /* Convert BTF function arguments into verifier types. | |
4493 | * Only PTR_TO_CTX and SCALAR are supported atm. | |
4494 | */ | |
4495 | for (i = 0; i < nargs; i++) { | |
4496 | t = btf_type_by_id(btf, args[i].type); | |
4497 | while (btf_type_is_modifier(t)) | |
4498 | t = btf_type_by_id(btf, t->type); | |
4499 | if (btf_type_is_int(t) || btf_type_is_enum(t)) { | |
4500 | reg[i + 1].type = SCALAR_VALUE; | |
4501 | continue; | |
4502 | } | |
4503 | if (btf_type_is_ptr(t) && | |
be8704ff | 4504 | btf_get_prog_ctx_type(log, btf, t, prog_type, i)) { |
51c39bb1 AS |
4505 | reg[i + 1].type = PTR_TO_CTX; |
4506 | continue; | |
4507 | } | |
4508 | bpf_log(log, "Arg#%d type %s in %s() is not supported yet.\n", | |
4509 | i, btf_kind_str[BTF_INFO_KIND(t->info)], tname); | |
4510 | return -EINVAL; | |
4511 | } | |
8c1b6e69 AS |
4512 | return 0; |
4513 | } | |
4514 | ||
b00b8dae MKL |
4515 | void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj, |
4516 | struct seq_file *m) | |
4517 | { | |
4518 | const struct btf_type *t = btf_type_by_id(btf, type_id); | |
4519 | ||
4520 | btf_type_ops(t)->seq_show(btf, t, type_id, obj, 0, m); | |
4521 | } | |
f56a653c | 4522 | |
3481e64b QM |
4523 | #ifdef CONFIG_PROC_FS |
4524 | static void bpf_btf_show_fdinfo(struct seq_file *m, struct file *filp) | |
4525 | { | |
4526 | const struct btf *btf = filp->private_data; | |
4527 | ||
4528 | seq_printf(m, "btf_id:\t%u\n", btf->id); | |
4529 | } | |
4530 | #endif | |
4531 | ||
f56a653c MKL |
4532 | static int btf_release(struct inode *inode, struct file *filp) |
4533 | { | |
4534 | btf_put(filp->private_data); | |
4535 | return 0; | |
4536 | } | |
4537 | ||
60197cfb | 4538 | const struct file_operations btf_fops = { |
3481e64b QM |
4539 | #ifdef CONFIG_PROC_FS |
4540 | .show_fdinfo = bpf_btf_show_fdinfo, | |
4541 | #endif | |
f56a653c MKL |
4542 | .release = btf_release, |
4543 | }; | |
4544 | ||
78958fca MKL |
4545 | static int __btf_new_fd(struct btf *btf) |
4546 | { | |
4547 | return anon_inode_getfd("btf", &btf_fops, btf, O_RDONLY | O_CLOEXEC); | |
4548 | } | |
4549 | ||
f56a653c MKL |
4550 | int btf_new_fd(const union bpf_attr *attr) |
4551 | { | |
4552 | struct btf *btf; | |
78958fca | 4553 | int ret; |
f56a653c MKL |
4554 | |
4555 | btf = btf_parse(u64_to_user_ptr(attr->btf), | |
4556 | attr->btf_size, attr->btf_log_level, | |
4557 | u64_to_user_ptr(attr->btf_log_buf), | |
4558 | attr->btf_log_size); | |
4559 | if (IS_ERR(btf)) | |
4560 | return PTR_ERR(btf); | |
4561 | ||
78958fca MKL |
4562 | ret = btf_alloc_id(btf); |
4563 | if (ret) { | |
4564 | btf_free(btf); | |
4565 | return ret; | |
4566 | } | |
4567 | ||
4568 | /* | |
4569 | * The BTF ID is published to the userspace. | |
4570 | * All BTF free must go through call_rcu() from | |
4571 | * now on (i.e. free by calling btf_put()). | |
4572 | */ | |
4573 | ||
4574 | ret = __btf_new_fd(btf); | |
4575 | if (ret < 0) | |
f56a653c MKL |
4576 | btf_put(btf); |
4577 | ||
78958fca | 4578 | return ret; |
f56a653c MKL |
4579 | } |
4580 | ||
4581 | struct btf *btf_get_by_fd(int fd) | |
4582 | { | |
4583 | struct btf *btf; | |
4584 | struct fd f; | |
4585 | ||
4586 | f = fdget(fd); | |
4587 | ||
4588 | if (!f.file) | |
4589 | return ERR_PTR(-EBADF); | |
4590 | ||
4591 | if (f.file->f_op != &btf_fops) { | |
4592 | fdput(f); | |
4593 | return ERR_PTR(-EINVAL); | |
4594 | } | |
4595 | ||
4596 | btf = f.file->private_data; | |
78958fca | 4597 | refcount_inc(&btf->refcnt); |
f56a653c MKL |
4598 | fdput(f); |
4599 | ||
4600 | return btf; | |
4601 | } | |
60197cfb MKL |
4602 | |
4603 | int btf_get_info_by_fd(const struct btf *btf, | |
4604 | const union bpf_attr *attr, | |
4605 | union bpf_attr __user *uattr) | |
4606 | { | |
62dab84c | 4607 | struct bpf_btf_info __user *uinfo; |
5c6f2588 | 4608 | struct bpf_btf_info info; |
62dab84c MKL |
4609 | u32 info_copy, btf_copy; |
4610 | void __user *ubtf; | |
4611 | u32 uinfo_len; | |
60197cfb | 4612 | |
62dab84c MKL |
4613 | uinfo = u64_to_user_ptr(attr->info.info); |
4614 | uinfo_len = attr->info.info_len; | |
4615 | ||
4616 | info_copy = min_t(u32, uinfo_len, sizeof(info)); | |
5c6f2588 | 4617 | memset(&info, 0, sizeof(info)); |
62dab84c MKL |
4618 | if (copy_from_user(&info, uinfo, info_copy)) |
4619 | return -EFAULT; | |
4620 | ||
4621 | info.id = btf->id; | |
4622 | ubtf = u64_to_user_ptr(info.btf); | |
4623 | btf_copy = min_t(u32, btf->data_size, info.btf_size); | |
4624 | if (copy_to_user(ubtf, btf->data, btf_copy)) | |
4625 | return -EFAULT; | |
4626 | info.btf_size = btf->data_size; | |
4627 | ||
4628 | if (copy_to_user(uinfo, &info, info_copy) || | |
4629 | put_user(info_copy, &uattr->info.info_len)) | |
60197cfb MKL |
4630 | return -EFAULT; |
4631 | ||
4632 | return 0; | |
4633 | } | |
78958fca MKL |
4634 | |
4635 | int btf_get_fd_by_id(u32 id) | |
4636 | { | |
4637 | struct btf *btf; | |
4638 | int fd; | |
4639 | ||
4640 | rcu_read_lock(); | |
4641 | btf = idr_find(&btf_idr, id); | |
4642 | if (!btf || !refcount_inc_not_zero(&btf->refcnt)) | |
4643 | btf = ERR_PTR(-ENOENT); | |
4644 | rcu_read_unlock(); | |
4645 | ||
4646 | if (IS_ERR(btf)) | |
4647 | return PTR_ERR(btf); | |
4648 | ||
4649 | fd = __btf_new_fd(btf); | |
4650 | if (fd < 0) | |
4651 | btf_put(btf); | |
4652 | ||
4653 | return fd; | |
4654 | } | |
4655 | ||
4656 | u32 btf_id(const struct btf *btf) | |
4657 | { | |
4658 | return btf->id; | |
4659 | } |