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