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