Commit | Line | Data |
---|---|---|
c561d110 | 1 | // SPDX-License-Identifier: GPL-2.0 |
69b693f0 MKL |
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> |
1e89106d | 28 | #include "../tools/lib/bpf/relo_core.h" |
69b693f0 MKL |
29 | |
30 | /* BTF (BPF Type Format) is the meta data format which describes | |
31 | * the data types of BPF program/map. Hence, it basically focus | |
32 | * on the C programming language which the modern BPF is primary | |
33 | * using. | |
34 | * | |
35 | * ELF Section: | |
36 | * ~~~~~~~~~~~ | |
37 | * The BTF data is stored under the ".BTF" ELF section | |
38 | * | |
39 | * struct btf_type: | |
40 | * ~~~~~~~~~~~~~~~ | |
41 | * Each 'struct btf_type' object describes a C data type. | |
42 | * Depending on the type it is describing, a 'struct btf_type' | |
43 | * object may be followed by more data. F.e. | |
44 | * To describe an array, 'struct btf_type' is followed by | |
45 | * 'struct btf_array'. | |
46 | * | |
47 | * 'struct btf_type' and any extra data following it are | |
48 | * 4 bytes aligned. | |
49 | * | |
50 | * Type section: | |
51 | * ~~~~~~~~~~~~~ | |
52 | * The BTF type section contains a list of 'struct btf_type' objects. | |
53 | * Each one describes a C type. Recall from the above section | |
54 | * that a 'struct btf_type' object could be immediately followed by extra | |
8fb33b60 | 55 | * data in order to describe some particular C types. |
69b693f0 MKL |
56 | * |
57 | * type_id: | |
58 | * ~~~~~~~ | |
59 | * Each btf_type object is identified by a type_id. The type_id | |
60 | * is implicitly implied by the location of the btf_type object in | |
61 | * the BTF type section. The first one has type_id 1. The second | |
62 | * one has type_id 2...etc. Hence, an earlier btf_type has | |
63 | * a smaller type_id. | |
64 | * | |
65 | * A btf_type object may refer to another btf_type object by using | |
66 | * type_id (i.e. the "type" in the "struct btf_type"). | |
67 | * | |
68 | * NOTE that we cannot assume any reference-order. | |
69 | * A btf_type object can refer to an earlier btf_type object | |
70 | * but it can also refer to a later btf_type object. | |
71 | * | |
72 | * For example, to describe "const void *". A btf_type | |
73 | * object describing "const" may refer to another btf_type | |
74 | * object describing "void *". This type-reference is done | |
75 | * by specifying type_id: | |
76 | * | |
77 | * [1] CONST (anon) type_id=2 | |
78 | * [2] PTR (anon) type_id=0 | |
79 | * | |
80 | * The above is the btf_verifier debug log: | |
81 | * - Each line started with "[?]" is a btf_type object | |
82 | * - [?] is the type_id of the btf_type object. | |
83 | * - CONST/PTR is the BTF_KIND_XXX | |
84 | * - "(anon)" is the name of the type. It just | |
85 | * happens that CONST and PTR has no name. | |
86 | * - type_id=XXX is the 'u32 type' in btf_type | |
87 | * | |
88 | * NOTE: "void" has type_id 0 | |
89 | * | |
90 | * String section: | |
91 | * ~~~~~~~~~~~~~~ | |
92 | * The BTF string section contains the names used by the type section. | |
93 | * Each string is referred by an "offset" from the beginning of the | |
94 | * string section. | |
95 | * | |
96 | * Each string is '\0' terminated. | |
97 | * | |
98 | * The first character in the string section must be '\0' | |
99 | * which is used to mean 'anonymous'. Some btf_type may not | |
100 | * have a name. | |
101 | */ | |
102 | ||
103 | /* BTF verification: | |
104 | * | |
105 | * To verify BTF data, two passes are needed. | |
106 | * | |
107 | * Pass #1 | |
108 | * ~~~~~~~ | |
109 | * The first pass is to collect all btf_type objects to | |
110 | * an array: "btf->types". | |
111 | * | |
112 | * Depending on the C type that a btf_type is describing, | |
113 | * a btf_type may be followed by extra data. We don't know | |
114 | * how many btf_type is there, and more importantly we don't | |
115 | * know where each btf_type is located in the type section. | |
116 | * | |
117 | * Without knowing the location of each type_id, most verifications | |
118 | * cannot be done. e.g. an earlier btf_type may refer to a later | |
119 | * btf_type (recall the "const void *" above), so we cannot | |
120 | * check this type-reference in the first pass. | |
121 | * | |
122 | * In the first pass, it still does some verifications (e.g. | |
123 | * checking the name is a valid offset to the string section). | |
eb3f595d MKL |
124 | * |
125 | * Pass #2 | |
126 | * ~~~~~~~ | |
127 | * The main focus is to resolve a btf_type that is referring | |
128 | * to another type. | |
129 | * | |
130 | * We have to ensure the referring type: | |
131 | * 1) does exist in the BTF (i.e. in btf->types[]) | |
132 | * 2) does not cause a loop: | |
133 | * struct A { | |
134 | * struct B b; | |
135 | * }; | |
136 | * | |
137 | * struct B { | |
138 | * struct A a; | |
139 | * }; | |
140 | * | |
141 | * btf_type_needs_resolve() decides if a btf_type needs | |
142 | * to be resolved. | |
143 | * | |
144 | * The needs_resolve type implements the "resolve()" ops which | |
145 | * essentially does a DFS and detects backedge. | |
146 | * | |
147 | * During resolve (or DFS), different C types have different | |
148 | * "RESOLVED" conditions. | |
149 | * | |
150 | * When resolving a BTF_KIND_STRUCT, we need to resolve all its | |
151 | * members because a member is always referring to another | |
152 | * type. A struct's member can be treated as "RESOLVED" if | |
153 | * it is referring to a BTF_KIND_PTR. Otherwise, the | |
154 | * following valid C struct would be rejected: | |
155 | * | |
156 | * struct A { | |
157 | * int m; | |
158 | * struct A *a; | |
159 | * }; | |
160 | * | |
161 | * When resolving a BTF_KIND_PTR, it needs to keep resolving if | |
162 | * it is referring to another BTF_KIND_PTR. Otherwise, we cannot | |
163 | * detect a pointer loop, e.g.: | |
164 | * BTF_KIND_CONST -> BTF_KIND_PTR -> BTF_KIND_CONST -> BTF_KIND_PTR + | |
165 | * ^ | | |
166 | * +-----------------------------------------+ | |
167 | * | |
69b693f0 MKL |
168 | */ |
169 | ||
b1e8818c | 170 | #define BITS_PER_U128 (sizeof(u64) * BITS_PER_BYTE * 2) |
69b693f0 MKL |
171 | #define BITS_PER_BYTE_MASK (BITS_PER_BYTE - 1) |
172 | #define BITS_PER_BYTE_MASKED(bits) ((bits) & BITS_PER_BYTE_MASK) | |
173 | #define BITS_ROUNDDOWN_BYTES(bits) ((bits) >> 3) | |
174 | #define BITS_ROUNDUP_BYTES(bits) \ | |
175 | (BITS_ROUNDDOWN_BYTES(bits) + !!BITS_PER_BYTE_MASKED(bits)) | |
176 | ||
b1828f0b | 177 | #define BTF_INFO_MASK 0x9f00ffff |
aea2f7b8 MKL |
178 | #define BTF_INT_MASK 0x0fffffff |
179 | #define BTF_TYPE_ID_VALID(type_id) ((type_id) <= BTF_MAX_TYPE) | |
180 | #define BTF_STR_OFFSET_VALID(name_off) ((name_off) <= BTF_MAX_NAME_OFFSET) | |
181 | ||
69b693f0 MKL |
182 | /* 16MB for 64k structs and each has 16 members and |
183 | * a few MB spaces for the string section. | |
184 | * The hard limit is S32_MAX. | |
185 | */ | |
186 | #define BTF_MAX_SIZE (16 * 1024 * 1024) | |
69b693f0 | 187 | |
eb3f595d MKL |
188 | #define for_each_member_from(i, from, struct_type, member) \ |
189 | for (i = from, member = btf_type_member(struct_type) + from; \ | |
190 | i < btf_type_vlen(struct_type); \ | |
191 | i++, member++) | |
192 | ||
1dc92851 DB |
193 | #define for_each_vsi_from(i, from, struct_type, member) \ |
194 | for (i = from, member = btf_type_var_secinfo(struct_type) + from; \ | |
195 | i < btf_type_vlen(struct_type); \ | |
196 | i++, member++) | |
197 | ||
1b9ed84e QM |
198 | DEFINE_IDR(btf_idr); |
199 | DEFINE_SPINLOCK(btf_idr_lock); | |
78958fca | 200 | |
dee872e1 KKD |
201 | enum btf_kfunc_hook { |
202 | BTF_KFUNC_HOOK_XDP, | |
203 | BTF_KFUNC_HOOK_TC, | |
204 | BTF_KFUNC_HOOK_STRUCT_OPS, | |
97949767 BT |
205 | BTF_KFUNC_HOOK_TRACING, |
206 | BTF_KFUNC_HOOK_SYSCALL, | |
dee872e1 KKD |
207 | BTF_KFUNC_HOOK_MAX, |
208 | }; | |
209 | ||
210 | enum { | |
f9b34818 | 211 | BTF_KFUNC_SET_MAX_CNT = 256, |
5ce937d6 | 212 | BTF_DTOR_KFUNC_MAX_CNT = 256, |
dee872e1 KKD |
213 | }; |
214 | ||
215 | struct btf_kfunc_set_tab { | |
a4703e31 | 216 | struct btf_id_set8 *sets[BTF_KFUNC_HOOK_MAX]; |
dee872e1 KKD |
217 | }; |
218 | ||
5ce937d6 KKD |
219 | struct btf_id_dtor_kfunc_tab { |
220 | u32 cnt; | |
221 | struct btf_id_dtor_kfunc dtors[]; | |
222 | }; | |
223 | ||
69b693f0 | 224 | struct btf { |
f80442a4 | 225 | void *data; |
69b693f0 | 226 | struct btf_type **types; |
eb3f595d MKL |
227 | u32 *resolved_ids; |
228 | u32 *resolved_sizes; | |
69b693f0 MKL |
229 | const char *strings; |
230 | void *nohdr_data; | |
f80442a4 | 231 | struct btf_header hdr; |
951bb646 | 232 | u32 nr_types; /* includes VOID for base BTF */ |
69b693f0 MKL |
233 | u32 types_size; |
234 | u32 data_size; | |
f56a653c | 235 | refcount_t refcnt; |
78958fca MKL |
236 | u32 id; |
237 | struct rcu_head rcu; | |
dee872e1 | 238 | struct btf_kfunc_set_tab *kfunc_set_tab; |
5ce937d6 | 239 | struct btf_id_dtor_kfunc_tab *dtor_kfunc_tab; |
951bb646 AN |
240 | |
241 | /* split BTF support */ | |
242 | struct btf *base_btf; | |
243 | u32 start_id; /* first type ID in this BTF (0 for base BTF) */ | |
244 | u32 start_str_off; /* first string offset (0 for base BTF) */ | |
53297220 AN |
245 | char name[MODULE_NAME_LEN]; |
246 | bool kernel_btf; | |
69b693f0 MKL |
247 | }; |
248 | ||
eb3f595d MKL |
249 | enum verifier_phase { |
250 | CHECK_META, | |
251 | CHECK_TYPE, | |
252 | }; | |
253 | ||
254 | struct resolve_vertex { | |
255 | const struct btf_type *t; | |
256 | u32 type_id; | |
257 | u16 next_member; | |
258 | }; | |
259 | ||
260 | enum visit_state { | |
261 | NOT_VISITED, | |
262 | VISITED, | |
263 | RESOLVED, | |
264 | }; | |
265 | ||
266 | enum resolve_mode { | |
267 | RESOLVE_TBD, /* To Be Determined */ | |
268 | RESOLVE_PTR, /* Resolving for Pointer */ | |
269 | RESOLVE_STRUCT_OR_ARRAY, /* Resolving for struct/union | |
270 | * or array | |
271 | */ | |
272 | }; | |
273 | ||
274 | #define MAX_RESOLVE_DEPTH 32 | |
275 | ||
f80442a4 MKL |
276 | struct btf_sec_info { |
277 | u32 off; | |
278 | u32 len; | |
279 | }; | |
280 | ||
69b693f0 MKL |
281 | struct btf_verifier_env { |
282 | struct btf *btf; | |
eb3f595d MKL |
283 | u8 *visit_states; |
284 | struct resolve_vertex stack[MAX_RESOLVE_DEPTH]; | |
69b693f0 MKL |
285 | struct bpf_verifier_log log; |
286 | u32 log_type_id; | |
eb3f595d MKL |
287 | u32 top_stack; |
288 | enum verifier_phase phase; | |
289 | enum resolve_mode resolve_mode; | |
69b693f0 MKL |
290 | }; |
291 | ||
292 | static const char * const btf_kind_str[NR_BTF_KINDS] = { | |
293 | [BTF_KIND_UNKN] = "UNKNOWN", | |
294 | [BTF_KIND_INT] = "INT", | |
295 | [BTF_KIND_PTR] = "PTR", | |
296 | [BTF_KIND_ARRAY] = "ARRAY", | |
297 | [BTF_KIND_STRUCT] = "STRUCT", | |
298 | [BTF_KIND_UNION] = "UNION", | |
299 | [BTF_KIND_ENUM] = "ENUM", | |
300 | [BTF_KIND_FWD] = "FWD", | |
301 | [BTF_KIND_TYPEDEF] = "TYPEDEF", | |
302 | [BTF_KIND_VOLATILE] = "VOLATILE", | |
303 | [BTF_KIND_CONST] = "CONST", | |
304 | [BTF_KIND_RESTRICT] = "RESTRICT", | |
2667a262 MKL |
305 | [BTF_KIND_FUNC] = "FUNC", |
306 | [BTF_KIND_FUNC_PROTO] = "FUNC_PROTO", | |
1dc92851 DB |
307 | [BTF_KIND_VAR] = "VAR", |
308 | [BTF_KIND_DATASEC] = "DATASEC", | |
b1828f0b | 309 | [BTF_KIND_FLOAT] = "FLOAT", |
223f903e | 310 | [BTF_KIND_DECL_TAG] = "DECL_TAG", |
8c42d2fa | 311 | [BTF_KIND_TYPE_TAG] = "TYPE_TAG", |
6089fb32 | 312 | [BTF_KIND_ENUM64] = "ENUM64", |
69b693f0 MKL |
313 | }; |
314 | ||
e6ac2450 | 315 | const char *btf_type_str(const struct btf_type *t) |
be8704ff AS |
316 | { |
317 | return btf_kind_str[BTF_INFO_KIND(t->info)]; | |
318 | } | |
319 | ||
31d0bc81 AM |
320 | /* Chunk size we use in safe copy of data to be shown. */ |
321 | #define BTF_SHOW_OBJ_SAFE_SIZE 32 | |
322 | ||
323 | /* | |
324 | * This is the maximum size of a base type value (equivalent to a | |
325 | * 128-bit int); if we are at the end of our safe buffer and have | |
326 | * less than 16 bytes space we can't be assured of being able | |
327 | * to copy the next type safely, so in such cases we will initiate | |
328 | * a new copy. | |
329 | */ | |
330 | #define BTF_SHOW_OBJ_BASE_TYPE_SIZE 16 | |
331 | ||
332 | /* Type name size */ | |
333 | #define BTF_SHOW_NAME_SIZE 80 | |
334 | ||
335 | /* | |
336 | * Common data to all BTF show operations. Private show functions can add | |
337 | * their own data to a structure containing a struct btf_show and consult it | |
338 | * in the show callback. See btf_type_show() below. | |
339 | * | |
340 | * One challenge with showing nested data is we want to skip 0-valued | |
341 | * data, but in order to figure out whether a nested object is all zeros | |
342 | * we need to walk through it. As a result, we need to make two passes | |
343 | * when handling structs, unions and arrays; the first path simply looks | |
344 | * for nonzero data, while the second actually does the display. The first | |
345 | * pass is signalled by show->state.depth_check being set, and if we | |
346 | * encounter a non-zero value we set show->state.depth_to_show to | |
347 | * the depth at which we encountered it. When we have completed the | |
348 | * first pass, we will know if anything needs to be displayed if | |
349 | * depth_to_show > depth. See btf_[struct,array]_show() for the | |
350 | * implementation of this. | |
351 | * | |
352 | * Another problem is we want to ensure the data for display is safe to | |
353 | * access. To support this, the anonymous "struct {} obj" tracks the data | |
354 | * object and our safe copy of it. We copy portions of the data needed | |
355 | * to the object "copy" buffer, but because its size is limited to | |
356 | * BTF_SHOW_OBJ_COPY_LEN bytes, multiple copies may be required as we | |
357 | * traverse larger objects for display. | |
358 | * | |
359 | * The various data type show functions all start with a call to | |
360 | * btf_show_start_type() which returns a pointer to the safe copy | |
361 | * of the data needed (or if BTF_SHOW_UNSAFE is specified, to the | |
362 | * raw data itself). btf_show_obj_safe() is responsible for | |
363 | * using copy_from_kernel_nofault() to update the safe data if necessary | |
364 | * as we traverse the object's data. skbuff-like semantics are | |
365 | * used: | |
366 | * | |
367 | * - obj.head points to the start of the toplevel object for display | |
368 | * - obj.size is the size of the toplevel object | |
369 | * - obj.data points to the current point in the original data at | |
370 | * which our safe data starts. obj.data will advance as we copy | |
371 | * portions of the data. | |
372 | * | |
373 | * In most cases a single copy will suffice, but larger data structures | |
374 | * such as "struct task_struct" will require many copies. The logic in | |
375 | * btf_show_obj_safe() handles the logic that determines if a new | |
376 | * copy_from_kernel_nofault() is needed. | |
377 | */ | |
378 | struct btf_show { | |
379 | u64 flags; | |
380 | void *target; /* target of show operation (seq file, buffer) */ | |
381 | void (*showfn)(struct btf_show *show, const char *fmt, va_list args); | |
382 | const struct btf *btf; | |
383 | /* below are used during iteration */ | |
384 | struct { | |
385 | u8 depth; | |
386 | u8 depth_to_show; | |
387 | u8 depth_check; | |
388 | u8 array_member:1, | |
389 | array_terminated:1; | |
390 | u16 array_encoding; | |
391 | u32 type_id; | |
392 | int status; /* non-zero for error */ | |
393 | const struct btf_type *type; | |
394 | const struct btf_member *member; | |
395 | char name[BTF_SHOW_NAME_SIZE]; /* space for member name/type */ | |
396 | } state; | |
397 | struct { | |
398 | u32 size; | |
399 | void *head; | |
400 | void *data; | |
401 | u8 safe[BTF_SHOW_OBJ_SAFE_SIZE]; | |
402 | } obj; | |
403 | }; | |
404 | ||
69b693f0 MKL |
405 | struct btf_kind_operations { |
406 | s32 (*check_meta)(struct btf_verifier_env *env, | |
407 | const struct btf_type *t, | |
408 | u32 meta_left); | |
eb3f595d MKL |
409 | int (*resolve)(struct btf_verifier_env *env, |
410 | const struct resolve_vertex *v); | |
179cde8c MKL |
411 | int (*check_member)(struct btf_verifier_env *env, |
412 | const struct btf_type *struct_type, | |
413 | const struct btf_member *member, | |
414 | const struct btf_type *member_type); | |
9d5f9f70 YS |
415 | int (*check_kflag_member)(struct btf_verifier_env *env, |
416 | const struct btf_type *struct_type, | |
417 | const struct btf_member *member, | |
418 | const struct btf_type *member_type); | |
69b693f0 MKL |
419 | void (*log_details)(struct btf_verifier_env *env, |
420 | const struct btf_type *t); | |
31d0bc81 | 421 | void (*show)(const struct btf *btf, const struct btf_type *t, |
b00b8dae | 422 | u32 type_id, void *data, u8 bits_offsets, |
31d0bc81 | 423 | struct btf_show *show); |
69b693f0 MKL |
424 | }; |
425 | ||
426 | static const struct btf_kind_operations * const kind_ops[NR_BTF_KINDS]; | |
427 | static struct btf_type btf_void; | |
428 | ||
2667a262 MKL |
429 | static int btf_resolve(struct btf_verifier_env *env, |
430 | const struct btf_type *t, u32 type_id); | |
431 | ||
d7e7b42f YS |
432 | static int btf_func_check(struct btf_verifier_env *env, |
433 | const struct btf_type *t); | |
434 | ||
eb3f595d MKL |
435 | static bool btf_type_is_modifier(const struct btf_type *t) |
436 | { | |
437 | /* Some of them is not strictly a C modifier | |
438 | * but they are grouped into the same bucket | |
439 | * for BTF concern: | |
440 | * A type (t) that refers to another | |
441 | * type through t->type AND its size cannot | |
442 | * be determined without following the t->type. | |
443 | * | |
444 | * ptr does not fall into this bucket | |
445 | * because its size is always sizeof(void *). | |
446 | */ | |
447 | switch (BTF_INFO_KIND(t->info)) { | |
448 | case BTF_KIND_TYPEDEF: | |
449 | case BTF_KIND_VOLATILE: | |
450 | case BTF_KIND_CONST: | |
451 | case BTF_KIND_RESTRICT: | |
8c42d2fa | 452 | case BTF_KIND_TYPE_TAG: |
eb3f595d MKL |
453 | return true; |
454 | } | |
455 | ||
456 | return false; | |
457 | } | |
458 | ||
2824ecb7 | 459 | bool btf_type_is_void(const struct btf_type *t) |
eb3f595d | 460 | { |
b47a0bd2 MKL |
461 | return t == &btf_void; |
462 | } | |
463 | ||
464 | static bool btf_type_is_fwd(const struct btf_type *t) | |
465 | { | |
466 | return BTF_INFO_KIND(t->info) == BTF_KIND_FWD; | |
467 | } | |
468 | ||
469 | static bool btf_type_nosize(const struct btf_type *t) | |
470 | { | |
2667a262 MKL |
471 | return btf_type_is_void(t) || btf_type_is_fwd(t) || |
472 | btf_type_is_func(t) || btf_type_is_func_proto(t); | |
eb3f595d MKL |
473 | } |
474 | ||
b47a0bd2 | 475 | static bool btf_type_nosize_or_null(const struct btf_type *t) |
eb3f595d | 476 | { |
b47a0bd2 | 477 | return !t || btf_type_nosize(t); |
eb3f595d MKL |
478 | } |
479 | ||
d83525ca AS |
480 | static bool __btf_type_is_struct(const struct btf_type *t) |
481 | { | |
482 | return BTF_INFO_KIND(t->info) == BTF_KIND_STRUCT; | |
483 | } | |
484 | ||
eb3f595d MKL |
485 | static bool btf_type_is_array(const struct btf_type *t) |
486 | { | |
487 | return BTF_INFO_KIND(t->info) == BTF_KIND_ARRAY; | |
488 | } | |
489 | ||
1dc92851 DB |
490 | static bool btf_type_is_datasec(const struct btf_type *t) |
491 | { | |
492 | return BTF_INFO_KIND(t->info) == BTF_KIND_DATASEC; | |
493 | } | |
494 | ||
223f903e | 495 | static bool btf_type_is_decl_tag(const struct btf_type *t) |
b5ea834d | 496 | { |
223f903e | 497 | return BTF_INFO_KIND(t->info) == BTF_KIND_DECL_TAG; |
b5ea834d YS |
498 | } |
499 | ||
223f903e | 500 | static bool btf_type_is_decl_tag_target(const struct btf_type *t) |
b5ea834d YS |
501 | { |
502 | return btf_type_is_func(t) || btf_type_is_struct(t) || | |
bd16dee6 | 503 | btf_type_is_var(t) || btf_type_is_typedef(t); |
b5ea834d YS |
504 | } |
505 | ||
541c3bad | 506 | u32 btf_nr_types(const struct btf *btf) |
951bb646 AN |
507 | { |
508 | u32 total = 0; | |
509 | ||
510 | while (btf) { | |
511 | total += btf->nr_types; | |
512 | btf = btf->base_btf; | |
513 | } | |
514 | ||
515 | return total; | |
516 | } | |
517 | ||
27ae7997 MKL |
518 | s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind) |
519 | { | |
520 | const struct btf_type *t; | |
521 | const char *tname; | |
951bb646 | 522 | u32 i, total; |
27ae7997 | 523 | |
541c3bad | 524 | total = btf_nr_types(btf); |
951bb646 AN |
525 | for (i = 1; i < total; i++) { |
526 | t = btf_type_by_id(btf, i); | |
27ae7997 MKL |
527 | if (BTF_INFO_KIND(t->info) != kind) |
528 | continue; | |
529 | ||
530 | tname = btf_name_by_offset(btf, t->name_off); | |
531 | if (!strcmp(tname, name)) | |
532 | return i; | |
533 | } | |
534 | ||
535 | return -ENOENT; | |
536 | } | |
537 | ||
edc3ec09 KKD |
538 | static s32 bpf_find_btf_id(const char *name, u32 kind, struct btf **btf_p) |
539 | { | |
540 | struct btf *btf; | |
541 | s32 ret; | |
542 | int id; | |
543 | ||
544 | btf = bpf_get_btf_vmlinux(); | |
545 | if (IS_ERR(btf)) | |
546 | return PTR_ERR(btf); | |
7ada3787 KKD |
547 | if (!btf) |
548 | return -EINVAL; | |
edc3ec09 KKD |
549 | |
550 | ret = btf_find_by_name_kind(btf, name, kind); | |
551 | /* ret is never zero, since btf_find_by_name_kind returns | |
552 | * positive btf_id or negative error. | |
553 | */ | |
554 | if (ret > 0) { | |
555 | btf_get(btf); | |
556 | *btf_p = btf; | |
557 | return ret; | |
558 | } | |
559 | ||
560 | /* If name is not found in vmlinux's BTF then search in module's BTFs */ | |
561 | spin_lock_bh(&btf_idr_lock); | |
562 | idr_for_each_entry(&btf_idr, btf, id) { | |
563 | if (!btf_is_module(btf)) | |
564 | continue; | |
565 | /* linear search could be slow hence unlock/lock | |
566 | * the IDR to avoiding holding it for too long | |
567 | */ | |
568 | btf_get(btf); | |
569 | spin_unlock_bh(&btf_idr_lock); | |
570 | ret = btf_find_by_name_kind(btf, name, kind); | |
571 | if (ret > 0) { | |
572 | *btf_p = btf; | |
573 | return ret; | |
574 | } | |
575 | spin_lock_bh(&btf_idr_lock); | |
576 | btf_put(btf); | |
577 | } | |
578 | spin_unlock_bh(&btf_idr_lock); | |
579 | return ret; | |
580 | } | |
581 | ||
27ae7997 MKL |
582 | const struct btf_type *btf_type_skip_modifiers(const struct btf *btf, |
583 | u32 id, u32 *res_id) | |
584 | { | |
585 | const struct btf_type *t = btf_type_by_id(btf, id); | |
586 | ||
587 | while (btf_type_is_modifier(t)) { | |
588 | id = t->type; | |
589 | t = btf_type_by_id(btf, t->type); | |
590 | } | |
591 | ||
592 | if (res_id) | |
593 | *res_id = id; | |
594 | ||
595 | return t; | |
596 | } | |
597 | ||
598 | const struct btf_type *btf_type_resolve_ptr(const struct btf *btf, | |
599 | u32 id, u32 *res_id) | |
600 | { | |
601 | const struct btf_type *t; | |
602 | ||
603 | t = btf_type_skip_modifiers(btf, id, NULL); | |
604 | if (!btf_type_is_ptr(t)) | |
605 | return NULL; | |
606 | ||
607 | return btf_type_skip_modifiers(btf, t->type, res_id); | |
608 | } | |
609 | ||
610 | const struct btf_type *btf_type_resolve_func_ptr(const struct btf *btf, | |
611 | u32 id, u32 *res_id) | |
612 | { | |
613 | const struct btf_type *ptype; | |
614 | ||
615 | ptype = btf_type_resolve_ptr(btf, id, res_id); | |
616 | if (ptype && btf_type_is_func_proto(ptype)) | |
617 | return ptype; | |
618 | ||
619 | return NULL; | |
620 | } | |
621 | ||
1dc92851 DB |
622 | /* Types that act only as a source, not sink or intermediate |
623 | * type when resolving. | |
624 | */ | |
625 | static bool btf_type_is_resolve_source_only(const struct btf_type *t) | |
626 | { | |
627 | return btf_type_is_var(t) || | |
223f903e | 628 | btf_type_is_decl_tag(t) || |
1dc92851 DB |
629 | btf_type_is_datasec(t); |
630 | } | |
631 | ||
eb3f595d MKL |
632 | /* What types need to be resolved? |
633 | * | |
634 | * btf_type_is_modifier() is an obvious one. | |
635 | * | |
636 | * btf_type_is_struct() because its member refers to | |
637 | * another type (through member->type). | |
1dc92851 DB |
638 | * |
639 | * btf_type_is_var() because the variable refers to | |
640 | * another type. btf_type_is_datasec() holds multiple | |
641 | * btf_type_is_var() types that need resolving. | |
642 | * | |
eb3f595d MKL |
643 | * btf_type_is_array() because its element (array->type) |
644 | * refers to another type. Array can be thought of a | |
645 | * special case of struct while array just has the same | |
646 | * member-type repeated by array->nelems of times. | |
647 | */ | |
648 | static bool btf_type_needs_resolve(const struct btf_type *t) | |
649 | { | |
650 | return btf_type_is_modifier(t) || | |
1dc92851 DB |
651 | btf_type_is_ptr(t) || |
652 | btf_type_is_struct(t) || | |
653 | btf_type_is_array(t) || | |
654 | btf_type_is_var(t) || | |
d7e7b42f | 655 | btf_type_is_func(t) || |
223f903e | 656 | btf_type_is_decl_tag(t) || |
1dc92851 | 657 | btf_type_is_datasec(t); |
eb3f595d MKL |
658 | } |
659 | ||
660 | /* t->size can be used */ | |
661 | static bool btf_type_has_size(const struct btf_type *t) | |
662 | { | |
663 | switch (BTF_INFO_KIND(t->info)) { | |
664 | case BTF_KIND_INT: | |
665 | case BTF_KIND_STRUCT: | |
666 | case BTF_KIND_UNION: | |
667 | case BTF_KIND_ENUM: | |
1dc92851 | 668 | case BTF_KIND_DATASEC: |
b1828f0b | 669 | case BTF_KIND_FLOAT: |
6089fb32 | 670 | case BTF_KIND_ENUM64: |
eb3f595d MKL |
671 | return true; |
672 | } | |
673 | ||
674 | return false; | |
675 | } | |
676 | ||
69b693f0 MKL |
677 | static const char *btf_int_encoding_str(u8 encoding) |
678 | { | |
679 | if (encoding == 0) | |
680 | return "(none)"; | |
681 | else if (encoding == BTF_INT_SIGNED) | |
682 | return "SIGNED"; | |
683 | else if (encoding == BTF_INT_CHAR) | |
684 | return "CHAR"; | |
685 | else if (encoding == BTF_INT_BOOL) | |
686 | return "BOOL"; | |
69b693f0 MKL |
687 | else |
688 | return "UNKN"; | |
689 | } | |
690 | ||
69b693f0 MKL |
691 | static u32 btf_type_int(const struct btf_type *t) |
692 | { | |
693 | return *(u32 *)(t + 1); | |
694 | } | |
695 | ||
696 | static const struct btf_array *btf_type_array(const struct btf_type *t) | |
697 | { | |
698 | return (const struct btf_array *)(t + 1); | |
699 | } | |
700 | ||
69b693f0 MKL |
701 | static const struct btf_enum *btf_type_enum(const struct btf_type *t) |
702 | { | |
703 | return (const struct btf_enum *)(t + 1); | |
704 | } | |
705 | ||
1dc92851 DB |
706 | static const struct btf_var *btf_type_var(const struct btf_type *t) |
707 | { | |
708 | return (const struct btf_var *)(t + 1); | |
709 | } | |
710 | ||
223f903e | 711 | static const struct btf_decl_tag *btf_type_decl_tag(const struct btf_type *t) |
b5ea834d | 712 | { |
223f903e | 713 | return (const struct btf_decl_tag *)(t + 1); |
b5ea834d YS |
714 | } |
715 | ||
6089fb32 YS |
716 | static const struct btf_enum64 *btf_type_enum64(const struct btf_type *t) |
717 | { | |
718 | return (const struct btf_enum64 *)(t + 1); | |
719 | } | |
720 | ||
69b693f0 MKL |
721 | static const struct btf_kind_operations *btf_type_ops(const struct btf_type *t) |
722 | { | |
723 | return kind_ops[BTF_INFO_KIND(t->info)]; | |
724 | } | |
725 | ||
583c5318 | 726 | static bool btf_name_offset_valid(const struct btf *btf, u32 offset) |
69b693f0 | 727 | { |
951bb646 AN |
728 | if (!BTF_STR_OFFSET_VALID(offset)) |
729 | return false; | |
730 | ||
731 | while (offset < btf->start_str_off) | |
732 | btf = btf->base_btf; | |
733 | ||
734 | offset -= btf->start_str_off; | |
735 | return offset < btf->hdr.str_len; | |
69b693f0 MKL |
736 | } |
737 | ||
1dc92851 DB |
738 | static bool __btf_name_char_ok(char c, bool first, bool dot_ok) |
739 | { | |
740 | if ((first ? !isalpha(c) : | |
741 | !isalnum(c)) && | |
742 | c != '_' && | |
743 | ((c == '.' && !dot_ok) || | |
744 | c != '.')) | |
745 | return false; | |
746 | return true; | |
747 | } | |
748 | ||
951bb646 AN |
749 | static const char *btf_str_by_offset(const struct btf *btf, u32 offset) |
750 | { | |
751 | while (offset < btf->start_str_off) | |
752 | btf = btf->base_btf; | |
753 | ||
754 | offset -= btf->start_str_off; | |
755 | if (offset < btf->hdr.str_len) | |
756 | return &btf->strings[offset]; | |
757 | ||
758 | return NULL; | |
759 | } | |
760 | ||
1dc92851 | 761 | static bool __btf_name_valid(const struct btf *btf, u32 offset, bool dot_ok) |
2667a262 MKL |
762 | { |
763 | /* offset must be valid */ | |
951bb646 | 764 | const char *src = btf_str_by_offset(btf, offset); |
2667a262 MKL |
765 | const char *src_limit; |
766 | ||
1dc92851 | 767 | if (!__btf_name_char_ok(*src, true, dot_ok)) |
2667a262 MKL |
768 | return false; |
769 | ||
770 | /* set a limit on identifier length */ | |
771 | src_limit = src + KSYM_NAME_LEN; | |
772 | src++; | |
773 | while (*src && src < src_limit) { | |
1dc92851 | 774 | if (!__btf_name_char_ok(*src, false, dot_ok)) |
2667a262 MKL |
775 | return false; |
776 | src++; | |
777 | } | |
778 | ||
779 | return !*src; | |
780 | } | |
781 | ||
1dc92851 DB |
782 | /* Only C-style identifier is permitted. This can be relaxed if |
783 | * necessary. | |
784 | */ | |
785 | static bool btf_name_valid_identifier(const struct btf *btf, u32 offset) | |
786 | { | |
787 | return __btf_name_valid(btf, offset, false); | |
788 | } | |
789 | ||
790 | static bool btf_name_valid_section(const struct btf *btf, u32 offset) | |
791 | { | |
792 | return __btf_name_valid(btf, offset, true); | |
793 | } | |
794 | ||
23127b33 | 795 | static const char *__btf_name_by_offset(const struct btf *btf, u32 offset) |
69b693f0 | 796 | { |
951bb646 AN |
797 | const char *name; |
798 | ||
aea2f7b8 | 799 | if (!offset) |
69b693f0 | 800 | return "(anon)"; |
951bb646 AN |
801 | |
802 | name = btf_str_by_offset(btf, offset); | |
803 | return name ?: "(invalid-name-offset)"; | |
69b693f0 MKL |
804 | } |
805 | ||
23127b33 MKL |
806 | const char *btf_name_by_offset(const struct btf *btf, u32 offset) |
807 | { | |
951bb646 | 808 | return btf_str_by_offset(btf, offset); |
23127b33 MKL |
809 | } |
810 | ||
838e9690 | 811 | const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id) |
eb3f595d | 812 | { |
951bb646 AN |
813 | while (type_id < btf->start_id) |
814 | btf = btf->base_btf; | |
eb3f595d | 815 | |
951bb646 AN |
816 | type_id -= btf->start_id; |
817 | if (type_id >= btf->nr_types) | |
818 | return NULL; | |
eb3f595d MKL |
819 | return btf->types[type_id]; |
820 | } | |
84c6ac41 | 821 | EXPORT_SYMBOL_GPL(btf_type_by_id); |
eb3f595d | 822 | |
4ef5f574 MKL |
823 | /* |
824 | * Regular int is not a bit field and it must be either | |
b1e8818c | 825 | * u8/u16/u32/u64 or __int128. |
4ef5f574 MKL |
826 | */ |
827 | static bool btf_type_int_is_regular(const struct btf_type *t) | |
828 | { | |
36fc3c8c | 829 | u8 nr_bits, nr_bytes; |
4ef5f574 MKL |
830 | u32 int_data; |
831 | ||
832 | int_data = btf_type_int(t); | |
833 | nr_bits = BTF_INT_BITS(int_data); | |
834 | nr_bytes = BITS_ROUNDUP_BYTES(nr_bits); | |
835 | if (BITS_PER_BYTE_MASKED(nr_bits) || | |
836 | BTF_INT_OFFSET(int_data) || | |
837 | (nr_bytes != sizeof(u8) && nr_bytes != sizeof(u16) && | |
b1e8818c YS |
838 | nr_bytes != sizeof(u32) && nr_bytes != sizeof(u64) && |
839 | nr_bytes != (2 * sizeof(u64)))) { | |
4ef5f574 MKL |
840 | return false; |
841 | } | |
842 | ||
843 | return true; | |
844 | } | |
845 | ||
9a1126b6 | 846 | /* |
ffa0c1cf YS |
847 | * Check that given struct member is a regular int with expected |
848 | * offset and size. | |
9a1126b6 | 849 | */ |
ffa0c1cf YS |
850 | bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s, |
851 | const struct btf_member *m, | |
852 | u32 expected_offset, u32 expected_size) | |
9a1126b6 | 853 | { |
ffa0c1cf YS |
854 | const struct btf_type *t; |
855 | u32 id, int_data; | |
856 | u8 nr_bits; | |
9a1126b6 | 857 | |
ffa0c1cf YS |
858 | id = m->type; |
859 | t = btf_type_id_size(btf, &id, NULL); | |
860 | if (!t || !btf_type_is_int(t)) | |
9a1126b6 RG |
861 | return false; |
862 | ||
863 | int_data = btf_type_int(t); | |
864 | nr_bits = BTF_INT_BITS(int_data); | |
ffa0c1cf YS |
865 | if (btf_type_kflag(s)) { |
866 | u32 bitfield_size = BTF_MEMBER_BITFIELD_SIZE(m->offset); | |
867 | u32 bit_offset = BTF_MEMBER_BIT_OFFSET(m->offset); | |
868 | ||
869 | /* if kflag set, int should be a regular int and | |
870 | * bit offset should be at byte boundary. | |
871 | */ | |
872 | return !bitfield_size && | |
873 | BITS_ROUNDUP_BYTES(bit_offset) == expected_offset && | |
874 | BITS_ROUNDUP_BYTES(nr_bits) == expected_size; | |
875 | } | |
876 | ||
877 | if (BTF_INT_OFFSET(int_data) || | |
878 | BITS_PER_BYTE_MASKED(m->offset) || | |
879 | BITS_ROUNDUP_BYTES(m->offset) != expected_offset || | |
880 | BITS_PER_BYTE_MASKED(nr_bits) || | |
881 | BITS_ROUNDUP_BYTES(nr_bits) != expected_size) | |
9a1126b6 RG |
882 | return false; |
883 | ||
884 | return true; | |
885 | } | |
886 | ||
31d0bc81 AM |
887 | /* Similar to btf_type_skip_modifiers() but does not skip typedefs. */ |
888 | static const struct btf_type *btf_type_skip_qualifiers(const struct btf *btf, | |
889 | u32 id) | |
890 | { | |
891 | const struct btf_type *t = btf_type_by_id(btf, id); | |
892 | ||
893 | while (btf_type_is_modifier(t) && | |
894 | BTF_INFO_KIND(t->info) != BTF_KIND_TYPEDEF) { | |
31d0bc81 AM |
895 | t = btf_type_by_id(btf, t->type); |
896 | } | |
897 | ||
898 | return t; | |
899 | } | |
900 | ||
901 | #define BTF_SHOW_MAX_ITER 10 | |
902 | ||
903 | #define BTF_KIND_BIT(kind) (1ULL << kind) | |
904 | ||
905 | /* | |
906 | * Populate show->state.name with type name information. | |
907 | * Format of type name is | |
908 | * | |
909 | * [.member_name = ] (type_name) | |
910 | */ | |
911 | static const char *btf_show_name(struct btf_show *show) | |
912 | { | |
913 | /* BTF_MAX_ITER array suffixes "[]" */ | |
914 | const char *array_suffixes = "[][][][][][][][][][]"; | |
915 | const char *array_suffix = &array_suffixes[strlen(array_suffixes)]; | |
916 | /* BTF_MAX_ITER pointer suffixes "*" */ | |
917 | const char *ptr_suffixes = "**********"; | |
918 | const char *ptr_suffix = &ptr_suffixes[strlen(ptr_suffixes)]; | |
919 | const char *name = NULL, *prefix = "", *parens = ""; | |
920 | const struct btf_member *m = show->state.member; | |
73b6eae5 | 921 | const struct btf_type *t; |
31d0bc81 AM |
922 | const struct btf_array *array; |
923 | u32 id = show->state.type_id; | |
924 | const char *member = NULL; | |
925 | bool show_member = false; | |
926 | u64 kinds = 0; | |
927 | int i; | |
928 | ||
929 | show->state.name[0] = '\0'; | |
930 | ||
931 | /* | |
932 | * Don't show type name if we're showing an array member; | |
933 | * in that case we show the array type so don't need to repeat | |
934 | * ourselves for each member. | |
935 | */ | |
936 | if (show->state.array_member) | |
937 | return ""; | |
938 | ||
939 | /* Retrieve member name, if any. */ | |
940 | if (m) { | |
941 | member = btf_name_by_offset(show->btf, m->name_off); | |
942 | show_member = strlen(member) > 0; | |
943 | id = m->type; | |
944 | } | |
945 | ||
946 | /* | |
947 | * Start with type_id, as we have resolved the struct btf_type * | |
948 | * via btf_modifier_show() past the parent typedef to the child | |
949 | * struct, int etc it is defined as. In such cases, the type_id | |
950 | * still represents the starting type while the struct btf_type * | |
951 | * in our show->state points at the resolved type of the typedef. | |
952 | */ | |
953 | t = btf_type_by_id(show->btf, id); | |
954 | if (!t) | |
955 | return ""; | |
956 | ||
957 | /* | |
958 | * The goal here is to build up the right number of pointer and | |
959 | * array suffixes while ensuring the type name for a typedef | |
960 | * is represented. Along the way we accumulate a list of | |
961 | * BTF kinds we have encountered, since these will inform later | |
962 | * display; for example, pointer types will not require an | |
963 | * opening "{" for struct, we will just display the pointer value. | |
964 | * | |
965 | * We also want to accumulate the right number of pointer or array | |
966 | * indices in the format string while iterating until we get to | |
967 | * the typedef/pointee/array member target type. | |
968 | * | |
969 | * We start by pointing at the end of pointer and array suffix | |
970 | * strings; as we accumulate pointers and arrays we move the pointer | |
971 | * or array string backwards so it will show the expected number of | |
972 | * '*' or '[]' for the type. BTF_SHOW_MAX_ITER of nesting of pointers | |
973 | * and/or arrays and typedefs are supported as a precaution. | |
974 | * | |
975 | * We also want to get typedef name while proceeding to resolve | |
976 | * type it points to so that we can add parentheses if it is a | |
977 | * "typedef struct" etc. | |
978 | */ | |
979 | for (i = 0; i < BTF_SHOW_MAX_ITER; i++) { | |
980 | ||
981 | switch (BTF_INFO_KIND(t->info)) { | |
982 | case BTF_KIND_TYPEDEF: | |
983 | if (!name) | |
984 | name = btf_name_by_offset(show->btf, | |
985 | t->name_off); | |
986 | kinds |= BTF_KIND_BIT(BTF_KIND_TYPEDEF); | |
987 | id = t->type; | |
988 | break; | |
989 | case BTF_KIND_ARRAY: | |
990 | kinds |= BTF_KIND_BIT(BTF_KIND_ARRAY); | |
991 | parens = "["; | |
992 | if (!t) | |
993 | return ""; | |
994 | array = btf_type_array(t); | |
995 | if (array_suffix > array_suffixes) | |
996 | array_suffix -= 2; | |
997 | id = array->type; | |
998 | break; | |
999 | case BTF_KIND_PTR: | |
1000 | kinds |= BTF_KIND_BIT(BTF_KIND_PTR); | |
1001 | if (ptr_suffix > ptr_suffixes) | |
1002 | ptr_suffix -= 1; | |
1003 | id = t->type; | |
1004 | break; | |
1005 | default: | |
1006 | id = 0; | |
1007 | break; | |
1008 | } | |
1009 | if (!id) | |
1010 | break; | |
1011 | t = btf_type_skip_qualifiers(show->btf, id); | |
1012 | } | |
1013 | /* We may not be able to represent this type; bail to be safe */ | |
1014 | if (i == BTF_SHOW_MAX_ITER) | |
1015 | return ""; | |
1016 | ||
1017 | if (!name) | |
1018 | name = btf_name_by_offset(show->btf, t->name_off); | |
1019 | ||
1020 | switch (BTF_INFO_KIND(t->info)) { | |
1021 | case BTF_KIND_STRUCT: | |
1022 | case BTF_KIND_UNION: | |
1023 | prefix = BTF_INFO_KIND(t->info) == BTF_KIND_STRUCT ? | |
1024 | "struct" : "union"; | |
1025 | /* if it's an array of struct/union, parens is already set */ | |
1026 | if (!(kinds & (BTF_KIND_BIT(BTF_KIND_ARRAY)))) | |
1027 | parens = "{"; | |
1028 | break; | |
1029 | case BTF_KIND_ENUM: | |
6089fb32 | 1030 | case BTF_KIND_ENUM64: |
31d0bc81 AM |
1031 | prefix = "enum"; |
1032 | break; | |
1033 | default: | |
1034 | break; | |
1035 | } | |
1036 | ||
1037 | /* pointer does not require parens */ | |
1038 | if (kinds & BTF_KIND_BIT(BTF_KIND_PTR)) | |
1039 | parens = ""; | |
1040 | /* typedef does not require struct/union/enum prefix */ | |
1041 | if (kinds & BTF_KIND_BIT(BTF_KIND_TYPEDEF)) | |
1042 | prefix = ""; | |
1043 | ||
1044 | if (!name) | |
1045 | name = ""; | |
1046 | ||
1047 | /* Even if we don't want type name info, we want parentheses etc */ | |
1048 | if (show->flags & BTF_SHOW_NONAME) | |
1049 | snprintf(show->state.name, sizeof(show->state.name), "%s", | |
1050 | parens); | |
1051 | else | |
1052 | snprintf(show->state.name, sizeof(show->state.name), | |
1053 | "%s%s%s(%s%s%s%s%s%s)%s", | |
1054 | /* first 3 strings comprise ".member = " */ | |
1055 | show_member ? "." : "", | |
1056 | show_member ? member : "", | |
1057 | show_member ? " = " : "", | |
1058 | /* ...next is our prefix (struct, enum, etc) */ | |
1059 | prefix, | |
1060 | strlen(prefix) > 0 && strlen(name) > 0 ? " " : "", | |
1061 | /* ...this is the type name itself */ | |
1062 | name, | |
1063 | /* ...suffixed by the appropriate '*', '[]' suffixes */ | |
1064 | strlen(ptr_suffix) > 0 ? " " : "", ptr_suffix, | |
1065 | array_suffix, parens); | |
1066 | ||
1067 | return show->state.name; | |
1068 | } | |
1069 | ||
1070 | static const char *__btf_show_indent(struct btf_show *show) | |
1071 | { | |
1072 | const char *indents = " "; | |
1073 | const char *indent = &indents[strlen(indents)]; | |
1074 | ||
1075 | if ((indent - show->state.depth) >= indents) | |
1076 | return indent - show->state.depth; | |
1077 | return indents; | |
1078 | } | |
1079 | ||
1080 | static const char *btf_show_indent(struct btf_show *show) | |
1081 | { | |
1082 | return show->flags & BTF_SHOW_COMPACT ? "" : __btf_show_indent(show); | |
1083 | } | |
1084 | ||
1085 | static const char *btf_show_newline(struct btf_show *show) | |
1086 | { | |
1087 | return show->flags & BTF_SHOW_COMPACT ? "" : "\n"; | |
1088 | } | |
1089 | ||
1090 | static const char *btf_show_delim(struct btf_show *show) | |
1091 | { | |
1092 | if (show->state.depth == 0) | |
1093 | return ""; | |
1094 | ||
1095 | if ((show->flags & BTF_SHOW_COMPACT) && show->state.type && | |
1096 | BTF_INFO_KIND(show->state.type->info) == BTF_KIND_UNION) | |
1097 | return "|"; | |
1098 | ||
1099 | return ","; | |
1100 | } | |
1101 | ||
1102 | __printf(2, 3) static void btf_show(struct btf_show *show, const char *fmt, ...) | |
1103 | { | |
1104 | va_list args; | |
1105 | ||
1106 | if (!show->state.depth_check) { | |
1107 | va_start(args, fmt); | |
1108 | show->showfn(show, fmt, args); | |
1109 | va_end(args); | |
1110 | } | |
1111 | } | |
1112 | ||
1113 | /* Macros are used here as btf_show_type_value[s]() prepends and appends | |
1114 | * format specifiers to the format specifier passed in; these do the work of | |
1115 | * adding indentation, delimiters etc while the caller simply has to specify | |
1116 | * the type value(s) in the format specifier + value(s). | |
1117 | */ | |
1118 | #define btf_show_type_value(show, fmt, value) \ | |
1119 | do { \ | |
a2a5580f BD |
1120 | if ((value) != (__typeof__(value))0 || \ |
1121 | (show->flags & BTF_SHOW_ZERO) || \ | |
31d0bc81 AM |
1122 | show->state.depth == 0) { \ |
1123 | btf_show(show, "%s%s" fmt "%s%s", \ | |
1124 | btf_show_indent(show), \ | |
1125 | btf_show_name(show), \ | |
1126 | value, btf_show_delim(show), \ | |
1127 | btf_show_newline(show)); \ | |
1128 | if (show->state.depth > show->state.depth_to_show) \ | |
1129 | show->state.depth_to_show = show->state.depth; \ | |
1130 | } \ | |
1131 | } while (0) | |
1132 | ||
1133 | #define btf_show_type_values(show, fmt, ...) \ | |
1134 | do { \ | |
1135 | btf_show(show, "%s%s" fmt "%s%s", btf_show_indent(show), \ | |
1136 | btf_show_name(show), \ | |
1137 | __VA_ARGS__, btf_show_delim(show), \ | |
1138 | btf_show_newline(show)); \ | |
1139 | if (show->state.depth > show->state.depth_to_show) \ | |
1140 | show->state.depth_to_show = show->state.depth; \ | |
1141 | } while (0) | |
1142 | ||
1143 | /* How much is left to copy to safe buffer after @data? */ | |
1144 | static int btf_show_obj_size_left(struct btf_show *show, void *data) | |
1145 | { | |
1146 | return show->obj.head + show->obj.size - data; | |
1147 | } | |
1148 | ||
1149 | /* Is object pointed to by @data of @size already copied to our safe buffer? */ | |
1150 | static bool btf_show_obj_is_safe(struct btf_show *show, void *data, int size) | |
1151 | { | |
1152 | return data >= show->obj.data && | |
1153 | (data + size) < (show->obj.data + BTF_SHOW_OBJ_SAFE_SIZE); | |
1154 | } | |
1155 | ||
1156 | /* | |
1157 | * If object pointed to by @data of @size falls within our safe buffer, return | |
1158 | * the equivalent pointer to the same safe data. Assumes | |
1159 | * copy_from_kernel_nofault() has already happened and our safe buffer is | |
1160 | * populated. | |
1161 | */ | |
1162 | static void *__btf_show_obj_safe(struct btf_show *show, void *data, int size) | |
1163 | { | |
1164 | if (btf_show_obj_is_safe(show, data, size)) | |
1165 | return show->obj.safe + (data - show->obj.data); | |
1166 | return NULL; | |
1167 | } | |
1168 | ||
1169 | /* | |
1170 | * Return a safe-to-access version of data pointed to by @data. | |
1171 | * We do this by copying the relevant amount of information | |
1172 | * to the struct btf_show obj.safe buffer using copy_from_kernel_nofault(). | |
1173 | * | |
1174 | * If BTF_SHOW_UNSAFE is specified, just return data as-is; no | |
1175 | * safe copy is needed. | |
1176 | * | |
1177 | * Otherwise we need to determine if we have the required amount | |
1178 | * of data (determined by the @data pointer and the size of the | |
1179 | * largest base type we can encounter (represented by | |
1180 | * BTF_SHOW_OBJ_BASE_TYPE_SIZE). Having that much data ensures | |
1181 | * that we will be able to print some of the current object, | |
1182 | * and if more is needed a copy will be triggered. | |
1183 | * Some objects such as structs will not fit into the buffer; | |
1184 | * in such cases additional copies when we iterate over their | |
1185 | * members may be needed. | |
1186 | * | |
1187 | * btf_show_obj_safe() is used to return a safe buffer for | |
1188 | * btf_show_start_type(); this ensures that as we recurse into | |
1189 | * nested types we always have safe data for the given type. | |
1190 | * This approach is somewhat wasteful; it's possible for example | |
1191 | * that when iterating over a large union we'll end up copying the | |
1192 | * same data repeatedly, but the goal is safety not performance. | |
1193 | * We use stack data as opposed to per-CPU buffers because the | |
1194 | * iteration over a type can take some time, and preemption handling | |
1195 | * would greatly complicate use of the safe buffer. | |
1196 | */ | |
1197 | static void *btf_show_obj_safe(struct btf_show *show, | |
1198 | const struct btf_type *t, | |
1199 | void *data) | |
1200 | { | |
1201 | const struct btf_type *rt; | |
1202 | int size_left, size; | |
1203 | void *safe = NULL; | |
1204 | ||
1205 | if (show->flags & BTF_SHOW_UNSAFE) | |
1206 | return data; | |
1207 | ||
1208 | rt = btf_resolve_size(show->btf, t, &size); | |
1209 | if (IS_ERR(rt)) { | |
1210 | show->state.status = PTR_ERR(rt); | |
1211 | return NULL; | |
1212 | } | |
1213 | ||
1214 | /* | |
1215 | * Is this toplevel object? If so, set total object size and | |
1216 | * initialize pointers. Otherwise check if we still fall within | |
1217 | * our safe object data. | |
1218 | */ | |
1219 | if (show->state.depth == 0) { | |
1220 | show->obj.size = size; | |
1221 | show->obj.head = data; | |
1222 | } else { | |
1223 | /* | |
1224 | * If the size of the current object is > our remaining | |
1225 | * safe buffer we _may_ need to do a new copy. However | |
1226 | * consider the case of a nested struct; it's size pushes | |
1227 | * us over the safe buffer limit, but showing any individual | |
1228 | * struct members does not. In such cases, we don't need | |
1229 | * to initiate a fresh copy yet; however we definitely need | |
1230 | * at least BTF_SHOW_OBJ_BASE_TYPE_SIZE bytes left | |
1231 | * in our buffer, regardless of the current object size. | |
1232 | * The logic here is that as we resolve types we will | |
1233 | * hit a base type at some point, and we need to be sure | |
1234 | * the next chunk of data is safely available to display | |
1235 | * that type info safely. We cannot rely on the size of | |
1236 | * the current object here because it may be much larger | |
1237 | * than our current buffer (e.g. task_struct is 8k). | |
1238 | * All we want to do here is ensure that we can print the | |
1239 | * next basic type, which we can if either | |
1240 | * - the current type size is within the safe buffer; or | |
1241 | * - at least BTF_SHOW_OBJ_BASE_TYPE_SIZE bytes are left in | |
1242 | * the safe buffer. | |
1243 | */ | |
1244 | safe = __btf_show_obj_safe(show, data, | |
1245 | min(size, | |
1246 | BTF_SHOW_OBJ_BASE_TYPE_SIZE)); | |
1247 | } | |
1248 | ||
1249 | /* | |
1250 | * We need a new copy to our safe object, either because we haven't | |
8fb33b60 | 1251 | * yet copied and are initializing safe data, or because the data |
31d0bc81 AM |
1252 | * we want falls outside the boundaries of the safe object. |
1253 | */ | |
1254 | if (!safe) { | |
1255 | size_left = btf_show_obj_size_left(show, data); | |
1256 | if (size_left > BTF_SHOW_OBJ_SAFE_SIZE) | |
1257 | size_left = BTF_SHOW_OBJ_SAFE_SIZE; | |
1258 | show->state.status = copy_from_kernel_nofault(show->obj.safe, | |
1259 | data, size_left); | |
1260 | if (!show->state.status) { | |
1261 | show->obj.data = data; | |
1262 | safe = show->obj.safe; | |
1263 | } | |
1264 | } | |
1265 | ||
1266 | return safe; | |
1267 | } | |
1268 | ||
1269 | /* | |
1270 | * Set the type we are starting to show and return a safe data pointer | |
1271 | * to be used for showing the associated data. | |
1272 | */ | |
1273 | static void *btf_show_start_type(struct btf_show *show, | |
1274 | const struct btf_type *t, | |
1275 | u32 type_id, void *data) | |
1276 | { | |
1277 | show->state.type = t; | |
1278 | show->state.type_id = type_id; | |
1279 | show->state.name[0] = '\0'; | |
1280 | ||
1281 | return btf_show_obj_safe(show, t, data); | |
1282 | } | |
1283 | ||
1284 | static void btf_show_end_type(struct btf_show *show) | |
1285 | { | |
1286 | show->state.type = NULL; | |
1287 | show->state.type_id = 0; | |
1288 | show->state.name[0] = '\0'; | |
1289 | } | |
1290 | ||
1291 | static void *btf_show_start_aggr_type(struct btf_show *show, | |
1292 | const struct btf_type *t, | |
1293 | u32 type_id, void *data) | |
1294 | { | |
1295 | void *safe_data = btf_show_start_type(show, t, type_id, data); | |
1296 | ||
1297 | if (!safe_data) | |
1298 | return safe_data; | |
1299 | ||
1300 | btf_show(show, "%s%s%s", btf_show_indent(show), | |
1301 | btf_show_name(show), | |
1302 | btf_show_newline(show)); | |
1303 | show->state.depth++; | |
1304 | return safe_data; | |
1305 | } | |
1306 | ||
1307 | static void btf_show_end_aggr_type(struct btf_show *show, | |
1308 | const char *suffix) | |
1309 | { | |
1310 | show->state.depth--; | |
1311 | btf_show(show, "%s%s%s%s", btf_show_indent(show), suffix, | |
1312 | btf_show_delim(show), btf_show_newline(show)); | |
1313 | btf_show_end_type(show); | |
1314 | } | |
1315 | ||
1316 | static void btf_show_start_member(struct btf_show *show, | |
1317 | const struct btf_member *m) | |
1318 | { | |
1319 | show->state.member = m; | |
1320 | } | |
1321 | ||
1322 | static void btf_show_start_array_member(struct btf_show *show) | |
1323 | { | |
1324 | show->state.array_member = 1; | |
1325 | btf_show_start_member(show, NULL); | |
1326 | } | |
1327 | ||
1328 | static void btf_show_end_member(struct btf_show *show) | |
1329 | { | |
1330 | show->state.member = NULL; | |
1331 | } | |
1332 | ||
1333 | static void btf_show_end_array_member(struct btf_show *show) | |
1334 | { | |
1335 | show->state.array_member = 0; | |
1336 | btf_show_end_member(show); | |
1337 | } | |
1338 | ||
1339 | static void *btf_show_start_array_type(struct btf_show *show, | |
1340 | const struct btf_type *t, | |
1341 | u32 type_id, | |
1342 | u16 array_encoding, | |
1343 | void *data) | |
1344 | { | |
1345 | show->state.array_encoding = array_encoding; | |
1346 | show->state.array_terminated = 0; | |
1347 | return btf_show_start_aggr_type(show, t, type_id, data); | |
1348 | } | |
1349 | ||
1350 | static void btf_show_end_array_type(struct btf_show *show) | |
1351 | { | |
1352 | show->state.array_encoding = 0; | |
1353 | show->state.array_terminated = 0; | |
1354 | btf_show_end_aggr_type(show, "]"); | |
1355 | } | |
1356 | ||
1357 | static void *btf_show_start_struct_type(struct btf_show *show, | |
1358 | const struct btf_type *t, | |
1359 | u32 type_id, | |
1360 | void *data) | |
1361 | { | |
1362 | return btf_show_start_aggr_type(show, t, type_id, data); | |
1363 | } | |
1364 | ||
1365 | static void btf_show_end_struct_type(struct btf_show *show) | |
1366 | { | |
1367 | btf_show_end_aggr_type(show, "}"); | |
1368 | } | |
1369 | ||
69b693f0 MKL |
1370 | __printf(2, 3) static void __btf_verifier_log(struct bpf_verifier_log *log, |
1371 | const char *fmt, ...) | |
1372 | { | |
1373 | va_list args; | |
1374 | ||
1375 | va_start(args, fmt); | |
1376 | bpf_verifier_vlog(log, fmt, args); | |
1377 | va_end(args); | |
1378 | } | |
1379 | ||
1380 | __printf(2, 3) static void btf_verifier_log(struct btf_verifier_env *env, | |
1381 | const char *fmt, ...) | |
1382 | { | |
1383 | struct bpf_verifier_log *log = &env->log; | |
1384 | va_list args; | |
1385 | ||
1386 | if (!bpf_verifier_log_needed(log)) | |
1387 | return; | |
1388 | ||
1389 | va_start(args, fmt); | |
1390 | bpf_verifier_vlog(log, fmt, args); | |
1391 | va_end(args); | |
1392 | } | |
1393 | ||
1394 | __printf(4, 5) static void __btf_verifier_log_type(struct btf_verifier_env *env, | |
1395 | const struct btf_type *t, | |
1396 | bool log_details, | |
1397 | const char *fmt, ...) | |
1398 | { | |
1399 | struct bpf_verifier_log *log = &env->log; | |
1400 | u8 kind = BTF_INFO_KIND(t->info); | |
1401 | struct btf *btf = env->btf; | |
1402 | va_list args; | |
1403 | ||
1404 | if (!bpf_verifier_log_needed(log)) | |
1405 | return; | |
1406 | ||
8580ac94 AS |
1407 | /* btf verifier prints all types it is processing via |
1408 | * btf_verifier_log_type(..., fmt = NULL). | |
1409 | * Skip those prints for in-kernel BTF verification. | |
1410 | */ | |
1411 | if (log->level == BPF_LOG_KERNEL && !fmt) | |
1412 | return; | |
1413 | ||
69b693f0 MKL |
1414 | __btf_verifier_log(log, "[%u] %s %s%s", |
1415 | env->log_type_id, | |
1416 | btf_kind_str[kind], | |
23127b33 | 1417 | __btf_name_by_offset(btf, t->name_off), |
69b693f0 MKL |
1418 | log_details ? " " : ""); |
1419 | ||
1420 | if (log_details) | |
1421 | btf_type_ops(t)->log_details(env, t); | |
1422 | ||
1423 | if (fmt && *fmt) { | |
1424 | __btf_verifier_log(log, " "); | |
1425 | va_start(args, fmt); | |
1426 | bpf_verifier_vlog(log, fmt, args); | |
1427 | va_end(args); | |
1428 | } | |
1429 | ||
1430 | __btf_verifier_log(log, "\n"); | |
1431 | } | |
1432 | ||
1433 | #define btf_verifier_log_type(env, t, ...) \ | |
1434 | __btf_verifier_log_type((env), (t), true, __VA_ARGS__) | |
1435 | #define btf_verifier_log_basic(env, t, ...) \ | |
1436 | __btf_verifier_log_type((env), (t), false, __VA_ARGS__) | |
1437 | ||
1438 | __printf(4, 5) | |
1439 | static void btf_verifier_log_member(struct btf_verifier_env *env, | |
1440 | const struct btf_type *struct_type, | |
1441 | const struct btf_member *member, | |
1442 | const char *fmt, ...) | |
1443 | { | |
1444 | struct bpf_verifier_log *log = &env->log; | |
1445 | struct btf *btf = env->btf; | |
1446 | va_list args; | |
1447 | ||
1448 | if (!bpf_verifier_log_needed(log)) | |
1449 | return; | |
1450 | ||
8580ac94 AS |
1451 | if (log->level == BPF_LOG_KERNEL && !fmt) |
1452 | return; | |
eb3f595d MKL |
1453 | /* The CHECK_META phase already did a btf dump. |
1454 | * | |
1455 | * If member is logged again, it must hit an error in | |
1456 | * parsing this member. It is useful to print out which | |
1457 | * struct this member belongs to. | |
1458 | */ | |
1459 | if (env->phase != CHECK_META) | |
1460 | btf_verifier_log_type(env, struct_type, NULL); | |
1461 | ||
9d5f9f70 YS |
1462 | if (btf_type_kflag(struct_type)) |
1463 | __btf_verifier_log(log, | |
1464 | "\t%s type_id=%u bitfield_size=%u bits_offset=%u", | |
1465 | __btf_name_by_offset(btf, member->name_off), | |
1466 | member->type, | |
1467 | BTF_MEMBER_BITFIELD_SIZE(member->offset), | |
1468 | BTF_MEMBER_BIT_OFFSET(member->offset)); | |
1469 | else | |
1470 | __btf_verifier_log(log, "\t%s type_id=%u bits_offset=%u", | |
1471 | __btf_name_by_offset(btf, member->name_off), | |
1472 | member->type, member->offset); | |
69b693f0 MKL |
1473 | |
1474 | if (fmt && *fmt) { | |
1475 | __btf_verifier_log(log, " "); | |
1476 | va_start(args, fmt); | |
1477 | bpf_verifier_vlog(log, fmt, args); | |
1478 | va_end(args); | |
1479 | } | |
1480 | ||
1481 | __btf_verifier_log(log, "\n"); | |
1482 | } | |
1483 | ||
1dc92851 DB |
1484 | __printf(4, 5) |
1485 | static void btf_verifier_log_vsi(struct btf_verifier_env *env, | |
1486 | const struct btf_type *datasec_type, | |
1487 | const struct btf_var_secinfo *vsi, | |
1488 | const char *fmt, ...) | |
1489 | { | |
1490 | struct bpf_verifier_log *log = &env->log; | |
1491 | va_list args; | |
1492 | ||
1493 | if (!bpf_verifier_log_needed(log)) | |
1494 | return; | |
8580ac94 AS |
1495 | if (log->level == BPF_LOG_KERNEL && !fmt) |
1496 | return; | |
1dc92851 DB |
1497 | if (env->phase != CHECK_META) |
1498 | btf_verifier_log_type(env, datasec_type, NULL); | |
1499 | ||
1500 | __btf_verifier_log(log, "\t type_id=%u offset=%u size=%u", | |
1501 | vsi->type, vsi->offset, vsi->size); | |
1502 | if (fmt && *fmt) { | |
1503 | __btf_verifier_log(log, " "); | |
1504 | va_start(args, fmt); | |
1505 | bpf_verifier_vlog(log, fmt, args); | |
1506 | va_end(args); | |
1507 | } | |
1508 | ||
1509 | __btf_verifier_log(log, "\n"); | |
1510 | } | |
1511 | ||
f80442a4 MKL |
1512 | static void btf_verifier_log_hdr(struct btf_verifier_env *env, |
1513 | u32 btf_data_size) | |
69b693f0 MKL |
1514 | { |
1515 | struct bpf_verifier_log *log = &env->log; | |
1516 | const struct btf *btf = env->btf; | |
1517 | const struct btf_header *hdr; | |
1518 | ||
1519 | if (!bpf_verifier_log_needed(log)) | |
1520 | return; | |
1521 | ||
8580ac94 AS |
1522 | if (log->level == BPF_LOG_KERNEL) |
1523 | return; | |
f80442a4 | 1524 | hdr = &btf->hdr; |
69b693f0 MKL |
1525 | __btf_verifier_log(log, "magic: 0x%x\n", hdr->magic); |
1526 | __btf_verifier_log(log, "version: %u\n", hdr->version); | |
1527 | __btf_verifier_log(log, "flags: 0x%x\n", hdr->flags); | |
f80442a4 | 1528 | __btf_verifier_log(log, "hdr_len: %u\n", hdr->hdr_len); |
69b693f0 | 1529 | __btf_verifier_log(log, "type_off: %u\n", hdr->type_off); |
f80442a4 | 1530 | __btf_verifier_log(log, "type_len: %u\n", hdr->type_len); |
69b693f0 MKL |
1531 | __btf_verifier_log(log, "str_off: %u\n", hdr->str_off); |
1532 | __btf_verifier_log(log, "str_len: %u\n", hdr->str_len); | |
f80442a4 | 1533 | __btf_verifier_log(log, "btf_total_size: %u\n", btf_data_size); |
69b693f0 MKL |
1534 | } |
1535 | ||
1536 | static int btf_add_type(struct btf_verifier_env *env, struct btf_type *t) | |
1537 | { | |
1538 | struct btf *btf = env->btf; | |
1539 | ||
951bb646 | 1540 | if (btf->types_size == btf->nr_types) { |
69b693f0 MKL |
1541 | /* Expand 'types' array */ |
1542 | ||
1543 | struct btf_type **new_types; | |
1544 | u32 expand_by, new_size; | |
1545 | ||
951bb646 | 1546 | if (btf->start_id + btf->types_size == BTF_MAX_TYPE) { |
69b693f0 MKL |
1547 | btf_verifier_log(env, "Exceeded max num of types"); |
1548 | return -E2BIG; | |
1549 | } | |
1550 | ||
1551 | expand_by = max_t(u32, btf->types_size >> 2, 16); | |
aea2f7b8 | 1552 | new_size = min_t(u32, BTF_MAX_TYPE, |
69b693f0 MKL |
1553 | btf->types_size + expand_by); |
1554 | ||
778e1cdd | 1555 | new_types = kvcalloc(new_size, sizeof(*new_types), |
69b693f0 MKL |
1556 | GFP_KERNEL | __GFP_NOWARN); |
1557 | if (!new_types) | |
1558 | return -ENOMEM; | |
1559 | ||
951bb646 AN |
1560 | if (btf->nr_types == 0) { |
1561 | if (!btf->base_btf) { | |
1562 | /* lazily init VOID type */ | |
1563 | new_types[0] = &btf_void; | |
1564 | btf->nr_types++; | |
1565 | } | |
1566 | } else { | |
69b693f0 | 1567 | memcpy(new_types, btf->types, |
951bb646 AN |
1568 | sizeof(*btf->types) * btf->nr_types); |
1569 | } | |
69b693f0 MKL |
1570 | |
1571 | kvfree(btf->types); | |
1572 | btf->types = new_types; | |
1573 | btf->types_size = new_size; | |
1574 | } | |
1575 | ||
951bb646 | 1576 | btf->types[btf->nr_types++] = t; |
69b693f0 MKL |
1577 | |
1578 | return 0; | |
1579 | } | |
1580 | ||
78958fca MKL |
1581 | static int btf_alloc_id(struct btf *btf) |
1582 | { | |
1583 | int id; | |
1584 | ||
1585 | idr_preload(GFP_KERNEL); | |
1586 | spin_lock_bh(&btf_idr_lock); | |
1587 | id = idr_alloc_cyclic(&btf_idr, btf, 1, INT_MAX, GFP_ATOMIC); | |
1588 | if (id > 0) | |
1589 | btf->id = id; | |
1590 | spin_unlock_bh(&btf_idr_lock); | |
1591 | idr_preload_end(); | |
1592 | ||
1593 | if (WARN_ON_ONCE(!id)) | |
1594 | return -ENOSPC; | |
1595 | ||
1596 | return id > 0 ? 0 : id; | |
1597 | } | |
1598 | ||
1599 | static void btf_free_id(struct btf *btf) | |
1600 | { | |
1601 | unsigned long flags; | |
1602 | ||
1603 | /* | |
1604 | * In map-in-map, calling map_delete_elem() on outer | |
1605 | * map will call bpf_map_put on the inner map. | |
1606 | * It will then eventually call btf_free_id() | |
1607 | * on the inner map. Some of the map_delete_elem() | |
1608 | * implementation may have irq disabled, so | |
1609 | * we need to use the _irqsave() version instead | |
1610 | * of the _bh() version. | |
1611 | */ | |
1612 | spin_lock_irqsave(&btf_idr_lock, flags); | |
1613 | idr_remove(&btf_idr, btf->id); | |
1614 | spin_unlock_irqrestore(&btf_idr_lock, flags); | |
1615 | } | |
1616 | ||
dee872e1 KKD |
1617 | static void btf_free_kfunc_set_tab(struct btf *btf) |
1618 | { | |
1619 | struct btf_kfunc_set_tab *tab = btf->kfunc_set_tab; | |
a4703e31 | 1620 | int hook; |
dee872e1 KKD |
1621 | |
1622 | if (!tab) | |
1623 | return; | |
1624 | /* For module BTF, we directly assign the sets being registered, so | |
1625 | * there is nothing to free except kfunc_set_tab. | |
1626 | */ | |
1627 | if (btf_is_module(btf)) | |
1628 | goto free_tab; | |
a4703e31 KKD |
1629 | for (hook = 0; hook < ARRAY_SIZE(tab->sets); hook++) |
1630 | kfree(tab->sets[hook]); | |
dee872e1 KKD |
1631 | free_tab: |
1632 | kfree(tab); | |
1633 | btf->kfunc_set_tab = NULL; | |
1634 | } | |
1635 | ||
5ce937d6 KKD |
1636 | static void btf_free_dtor_kfunc_tab(struct btf *btf) |
1637 | { | |
1638 | struct btf_id_dtor_kfunc_tab *tab = btf->dtor_kfunc_tab; | |
1639 | ||
1640 | if (!tab) | |
1641 | return; | |
1642 | kfree(tab); | |
1643 | btf->dtor_kfunc_tab = NULL; | |
1644 | } | |
1645 | ||
69b693f0 MKL |
1646 | static void btf_free(struct btf *btf) |
1647 | { | |
5ce937d6 | 1648 | btf_free_dtor_kfunc_tab(btf); |
dee872e1 | 1649 | btf_free_kfunc_set_tab(btf); |
69b693f0 | 1650 | kvfree(btf->types); |
eb3f595d MKL |
1651 | kvfree(btf->resolved_sizes); |
1652 | kvfree(btf->resolved_ids); | |
69b693f0 MKL |
1653 | kvfree(btf->data); |
1654 | kfree(btf); | |
1655 | } | |
1656 | ||
78958fca | 1657 | static void btf_free_rcu(struct rcu_head *rcu) |
f56a653c | 1658 | { |
78958fca MKL |
1659 | struct btf *btf = container_of(rcu, struct btf, rcu); |
1660 | ||
1661 | btf_free(btf); | |
f56a653c MKL |
1662 | } |
1663 | ||
22dc4a0f AN |
1664 | void btf_get(struct btf *btf) |
1665 | { | |
1666 | refcount_inc(&btf->refcnt); | |
1667 | } | |
1668 | ||
f56a653c MKL |
1669 | void btf_put(struct btf *btf) |
1670 | { | |
78958fca MKL |
1671 | if (btf && refcount_dec_and_test(&btf->refcnt)) { |
1672 | btf_free_id(btf); | |
1673 | call_rcu(&btf->rcu, btf_free_rcu); | |
1674 | } | |
f56a653c MKL |
1675 | } |
1676 | ||
eb3f595d MKL |
1677 | static int env_resolve_init(struct btf_verifier_env *env) |
1678 | { | |
1679 | struct btf *btf = env->btf; | |
1680 | u32 nr_types = btf->nr_types; | |
1681 | u32 *resolved_sizes = NULL; | |
1682 | u32 *resolved_ids = NULL; | |
1683 | u8 *visit_states = NULL; | |
1684 | ||
951bb646 | 1685 | resolved_sizes = kvcalloc(nr_types, sizeof(*resolved_sizes), |
eb3f595d MKL |
1686 | GFP_KERNEL | __GFP_NOWARN); |
1687 | if (!resolved_sizes) | |
1688 | goto nomem; | |
1689 | ||
951bb646 | 1690 | resolved_ids = kvcalloc(nr_types, sizeof(*resolved_ids), |
eb3f595d MKL |
1691 | GFP_KERNEL | __GFP_NOWARN); |
1692 | if (!resolved_ids) | |
1693 | goto nomem; | |
1694 | ||
951bb646 | 1695 | visit_states = kvcalloc(nr_types, sizeof(*visit_states), |
eb3f595d MKL |
1696 | GFP_KERNEL | __GFP_NOWARN); |
1697 | if (!visit_states) | |
1698 | goto nomem; | |
1699 | ||
1700 | btf->resolved_sizes = resolved_sizes; | |
1701 | btf->resolved_ids = resolved_ids; | |
1702 | env->visit_states = visit_states; | |
1703 | ||
1704 | return 0; | |
1705 | ||
1706 | nomem: | |
1707 | kvfree(resolved_sizes); | |
1708 | kvfree(resolved_ids); | |
1709 | kvfree(visit_states); | |
1710 | return -ENOMEM; | |
1711 | } | |
1712 | ||
69b693f0 MKL |
1713 | static void btf_verifier_env_free(struct btf_verifier_env *env) |
1714 | { | |
eb3f595d | 1715 | kvfree(env->visit_states); |
69b693f0 MKL |
1716 | kfree(env); |
1717 | } | |
1718 | ||
eb3f595d MKL |
1719 | static bool env_type_is_resolve_sink(const struct btf_verifier_env *env, |
1720 | const struct btf_type *next_type) | |
1721 | { | |
1722 | switch (env->resolve_mode) { | |
1723 | case RESOLVE_TBD: | |
1724 | /* int, enum or void is a sink */ | |
1725 | return !btf_type_needs_resolve(next_type); | |
1726 | case RESOLVE_PTR: | |
2667a262 MKL |
1727 | /* int, enum, void, struct, array, func or func_proto is a sink |
1728 | * for ptr | |
1729 | */ | |
eb3f595d MKL |
1730 | return !btf_type_is_modifier(next_type) && |
1731 | !btf_type_is_ptr(next_type); | |
1732 | case RESOLVE_STRUCT_OR_ARRAY: | |
2667a262 MKL |
1733 | /* int, enum, void, ptr, func or func_proto is a sink |
1734 | * for struct and array | |
1735 | */ | |
eb3f595d MKL |
1736 | return !btf_type_is_modifier(next_type) && |
1737 | !btf_type_is_array(next_type) && | |
1738 | !btf_type_is_struct(next_type); | |
1739 | default: | |
53c8036c | 1740 | BUG(); |
eb3f595d MKL |
1741 | } |
1742 | } | |
1743 | ||
1744 | static bool env_type_is_resolved(const struct btf_verifier_env *env, | |
1745 | u32 type_id) | |
1746 | { | |
951bb646 AN |
1747 | /* base BTF types should be resolved by now */ |
1748 | if (type_id < env->btf->start_id) | |
1749 | return true; | |
1750 | ||
1751 | return env->visit_states[type_id - env->btf->start_id] == RESOLVED; | |
eb3f595d MKL |
1752 | } |
1753 | ||
1754 | static int env_stack_push(struct btf_verifier_env *env, | |
1755 | const struct btf_type *t, u32 type_id) | |
1756 | { | |
951bb646 | 1757 | const struct btf *btf = env->btf; |
eb3f595d MKL |
1758 | struct resolve_vertex *v; |
1759 | ||
1760 | if (env->top_stack == MAX_RESOLVE_DEPTH) | |
1761 | return -E2BIG; | |
1762 | ||
951bb646 AN |
1763 | if (type_id < btf->start_id |
1764 | || env->visit_states[type_id - btf->start_id] != NOT_VISITED) | |
eb3f595d MKL |
1765 | return -EEXIST; |
1766 | ||
951bb646 | 1767 | env->visit_states[type_id - btf->start_id] = VISITED; |
eb3f595d MKL |
1768 | |
1769 | v = &env->stack[env->top_stack++]; | |
1770 | v->t = t; | |
1771 | v->type_id = type_id; | |
1772 | v->next_member = 0; | |
1773 | ||
1774 | if (env->resolve_mode == RESOLVE_TBD) { | |
1775 | if (btf_type_is_ptr(t)) | |
1776 | env->resolve_mode = RESOLVE_PTR; | |
1777 | else if (btf_type_is_struct(t) || btf_type_is_array(t)) | |
1778 | env->resolve_mode = RESOLVE_STRUCT_OR_ARRAY; | |
1779 | } | |
1780 | ||
1781 | return 0; | |
1782 | } | |
1783 | ||
1784 | static void env_stack_set_next_member(struct btf_verifier_env *env, | |
1785 | u16 next_member) | |
1786 | { | |
1787 | env->stack[env->top_stack - 1].next_member = next_member; | |
1788 | } | |
1789 | ||
1790 | static void env_stack_pop_resolved(struct btf_verifier_env *env, | |
1791 | u32 resolved_type_id, | |
1792 | u32 resolved_size) | |
1793 | { | |
1794 | u32 type_id = env->stack[--(env->top_stack)].type_id; | |
1795 | struct btf *btf = env->btf; | |
1796 | ||
951bb646 | 1797 | type_id -= btf->start_id; /* adjust to local type id */ |
eb3f595d MKL |
1798 | btf->resolved_sizes[type_id] = resolved_size; |
1799 | btf->resolved_ids[type_id] = resolved_type_id; | |
1800 | env->visit_states[type_id] = RESOLVED; | |
1801 | } | |
1802 | ||
1803 | static const struct resolve_vertex *env_stack_peak(struct btf_verifier_env *env) | |
1804 | { | |
1805 | return env->top_stack ? &env->stack[env->top_stack - 1] : NULL; | |
1806 | } | |
1807 | ||
7e3617a7 MKL |
1808 | /* Resolve the size of a passed-in "type" |
1809 | * | |
1810 | * type: is an array (e.g. u32 array[x][y]) | |
1811 | * return type: type "u32[x][y]", i.e. BTF_KIND_ARRAY, | |
1812 | * *type_size: (x * y * sizeof(u32)). Hence, *type_size always | |
1813 | * corresponds to the return type. | |
1814 | * *elem_type: u32 | |
69ff3047 | 1815 | * *elem_id: id of u32 |
7e3617a7 MKL |
1816 | * *total_nelems: (x * y). Hence, individual elem size is |
1817 | * (*type_size / *total_nelems) | |
887c31a3 | 1818 | * *type_id: id of type if it's changed within the function, 0 if not |
7e3617a7 MKL |
1819 | * |
1820 | * type: is not an array (e.g. const struct X) | |
1821 | * return type: type "struct X" | |
1822 | * *type_size: sizeof(struct X) | |
1823 | * *elem_type: same as return type ("struct X") | |
69ff3047 | 1824 | * *elem_id: 0 |
7e3617a7 | 1825 | * *total_nelems: 1 |
887c31a3 | 1826 | * *type_id: id of type if it's changed within the function, 0 if not |
7e3617a7 | 1827 | */ |
6298399b JO |
1828 | static const struct btf_type * |
1829 | __btf_resolve_size(const struct btf *btf, const struct btf_type *type, | |
1830 | u32 *type_size, const struct btf_type **elem_type, | |
887c31a3 | 1831 | u32 *elem_id, u32 *total_nelems, u32 *type_id) |
7e3617a7 MKL |
1832 | { |
1833 | const struct btf_type *array_type = NULL; | |
69ff3047 | 1834 | const struct btf_array *array = NULL; |
887c31a3 | 1835 | u32 i, size, nelems = 1, id = 0; |
7e3617a7 MKL |
1836 | |
1837 | for (i = 0; i < MAX_RESOLVE_DEPTH; i++) { | |
1838 | switch (BTF_INFO_KIND(type->info)) { | |
1839 | /* type->size can be used */ | |
1840 | case BTF_KIND_INT: | |
1841 | case BTF_KIND_STRUCT: | |
1842 | case BTF_KIND_UNION: | |
1843 | case BTF_KIND_ENUM: | |
b1828f0b | 1844 | case BTF_KIND_FLOAT: |
6089fb32 | 1845 | case BTF_KIND_ENUM64: |
7e3617a7 MKL |
1846 | size = type->size; |
1847 | goto resolved; | |
1848 | ||
1849 | case BTF_KIND_PTR: | |
1850 | size = sizeof(void *); | |
1851 | goto resolved; | |
1852 | ||
1853 | /* Modifiers */ | |
1854 | case BTF_KIND_TYPEDEF: | |
1855 | case BTF_KIND_VOLATILE: | |
1856 | case BTF_KIND_CONST: | |
1857 | case BTF_KIND_RESTRICT: | |
8c42d2fa | 1858 | case BTF_KIND_TYPE_TAG: |
887c31a3 | 1859 | id = type->type; |
7e3617a7 MKL |
1860 | type = btf_type_by_id(btf, type->type); |
1861 | break; | |
1862 | ||
1863 | case BTF_KIND_ARRAY: | |
1864 | if (!array_type) | |
1865 | array_type = type; | |
1866 | array = btf_type_array(type); | |
1867 | if (nelems && array->nelems > U32_MAX / nelems) | |
1868 | return ERR_PTR(-EINVAL); | |
1869 | nelems *= array->nelems; | |
1870 | type = btf_type_by_id(btf, array->type); | |
1871 | break; | |
1872 | ||
1873 | /* type without size */ | |
1874 | default: | |
1875 | return ERR_PTR(-EINVAL); | |
1876 | } | |
1877 | } | |
1878 | ||
1879 | return ERR_PTR(-EINVAL); | |
1880 | ||
1881 | resolved: | |
1882 | if (nelems && size > U32_MAX / nelems) | |
1883 | return ERR_PTR(-EINVAL); | |
1884 | ||
1885 | *type_size = nelems * size; | |
85d33df3 MKL |
1886 | if (total_nelems) |
1887 | *total_nelems = nelems; | |
1888 | if (elem_type) | |
1889 | *elem_type = type; | |
69ff3047 JO |
1890 | if (elem_id) |
1891 | *elem_id = array ? array->type : 0; | |
887c31a3 JO |
1892 | if (type_id && id) |
1893 | *type_id = id; | |
7e3617a7 MKL |
1894 | |
1895 | return array_type ? : type; | |
1896 | } | |
1897 | ||
6298399b JO |
1898 | const struct btf_type * |
1899 | btf_resolve_size(const struct btf *btf, const struct btf_type *type, | |
1900 | u32 *type_size) | |
1901 | { | |
887c31a3 | 1902 | return __btf_resolve_size(btf, type, type_size, NULL, NULL, NULL, NULL); |
6298399b JO |
1903 | } |
1904 | ||
951bb646 AN |
1905 | static u32 btf_resolved_type_id(const struct btf *btf, u32 type_id) |
1906 | { | |
1907 | while (type_id < btf->start_id) | |
1908 | btf = btf->base_btf; | |
1909 | ||
1910 | return btf->resolved_ids[type_id - btf->start_id]; | |
1911 | } | |
1912 | ||
eb3f595d MKL |
1913 | /* The input param "type_id" must point to a needs_resolve type */ |
1914 | static const struct btf_type *btf_type_id_resolve(const struct btf *btf, | |
1915 | u32 *type_id) | |
1916 | { | |
951bb646 | 1917 | *type_id = btf_resolved_type_id(btf, *type_id); |
eb3f595d MKL |
1918 | return btf_type_by_id(btf, *type_id); |
1919 | } | |
1920 | ||
951bb646 AN |
1921 | static u32 btf_resolved_type_size(const struct btf *btf, u32 type_id) |
1922 | { | |
1923 | while (type_id < btf->start_id) | |
1924 | btf = btf->base_btf; | |
1925 | ||
1926 | return btf->resolved_sizes[type_id - btf->start_id]; | |
1927 | } | |
1928 | ||
eb3f595d MKL |
1929 | const struct btf_type *btf_type_id_size(const struct btf *btf, |
1930 | u32 *type_id, u32 *ret_size) | |
1931 | { | |
1932 | const struct btf_type *size_type; | |
1933 | u32 size_type_id = *type_id; | |
1934 | u32 size = 0; | |
1935 | ||
1936 | size_type = btf_type_by_id(btf, size_type_id); | |
b47a0bd2 | 1937 | if (btf_type_nosize_or_null(size_type)) |
eb3f595d MKL |
1938 | return NULL; |
1939 | ||
1940 | if (btf_type_has_size(size_type)) { | |
1941 | size = size_type->size; | |
1942 | } else if (btf_type_is_array(size_type)) { | |
951bb646 | 1943 | size = btf_resolved_type_size(btf, size_type_id); |
eb3f595d MKL |
1944 | } else if (btf_type_is_ptr(size_type)) { |
1945 | size = sizeof(void *); | |
1946 | } else { | |
1dc92851 DB |
1947 | if (WARN_ON_ONCE(!btf_type_is_modifier(size_type) && |
1948 | !btf_type_is_var(size_type))) | |
eb3f595d MKL |
1949 | return NULL; |
1950 | ||
951bb646 | 1951 | size_type_id = btf_resolved_type_id(btf, size_type_id); |
eb3f595d | 1952 | size_type = btf_type_by_id(btf, size_type_id); |
b47a0bd2 | 1953 | if (btf_type_nosize_or_null(size_type)) |
eb3f595d | 1954 | return NULL; |
1acc5d5c AN |
1955 | else if (btf_type_has_size(size_type)) |
1956 | size = size_type->size; | |
1957 | else if (btf_type_is_array(size_type)) | |
951bb646 | 1958 | size = btf_resolved_type_size(btf, size_type_id); |
1acc5d5c AN |
1959 | else if (btf_type_is_ptr(size_type)) |
1960 | size = sizeof(void *); | |
1961 | else | |
1962 | return NULL; | |
eb3f595d MKL |
1963 | } |
1964 | ||
1965 | *type_id = size_type_id; | |
1966 | if (ret_size) | |
1967 | *ret_size = size; | |
1968 | ||
1969 | return size_type; | |
1970 | } | |
1971 | ||
179cde8c MKL |
1972 | static int btf_df_check_member(struct btf_verifier_env *env, |
1973 | const struct btf_type *struct_type, | |
1974 | const struct btf_member *member, | |
1975 | const struct btf_type *member_type) | |
1976 | { | |
1977 | btf_verifier_log_basic(env, struct_type, | |
1978 | "Unsupported check_member"); | |
1979 | return -EINVAL; | |
1980 | } | |
1981 | ||
9d5f9f70 YS |
1982 | static int btf_df_check_kflag_member(struct btf_verifier_env *env, |
1983 | const struct btf_type *struct_type, | |
1984 | const struct btf_member *member, | |
1985 | const struct btf_type *member_type) | |
1986 | { | |
1987 | btf_verifier_log_basic(env, struct_type, | |
1988 | "Unsupported check_kflag_member"); | |
1989 | return -EINVAL; | |
1990 | } | |
1991 | ||
b1828f0b | 1992 | /* Used for ptr, array struct/union and float type members. |
9d5f9f70 YS |
1993 | * int, enum and modifier types have their specific callback functions. |
1994 | */ | |
1995 | static int btf_generic_check_kflag_member(struct btf_verifier_env *env, | |
1996 | const struct btf_type *struct_type, | |
1997 | const struct btf_member *member, | |
1998 | const struct btf_type *member_type) | |
1999 | { | |
2000 | if (BTF_MEMBER_BITFIELD_SIZE(member->offset)) { | |
2001 | btf_verifier_log_member(env, struct_type, member, | |
2002 | "Invalid member bitfield_size"); | |
2003 | return -EINVAL; | |
2004 | } | |
2005 | ||
2006 | /* bitfield size is 0, so member->offset represents bit offset only. | |
2007 | * It is safe to call non kflag check_member variants. | |
2008 | */ | |
2009 | return btf_type_ops(member_type)->check_member(env, struct_type, | |
2010 | member, | |
2011 | member_type); | |
2012 | } | |
2013 | ||
eb3f595d MKL |
2014 | static int btf_df_resolve(struct btf_verifier_env *env, |
2015 | const struct resolve_vertex *v) | |
2016 | { | |
2017 | btf_verifier_log_basic(env, v->t, "Unsupported resolve"); | |
2018 | return -EINVAL; | |
2019 | } | |
2020 | ||
31d0bc81 AM |
2021 | static void btf_df_show(const struct btf *btf, const struct btf_type *t, |
2022 | u32 type_id, void *data, u8 bits_offsets, | |
2023 | struct btf_show *show) | |
b00b8dae | 2024 | { |
31d0bc81 | 2025 | btf_show(show, "<unsupported kind:%u>", BTF_INFO_KIND(t->info)); |
b00b8dae MKL |
2026 | } |
2027 | ||
179cde8c MKL |
2028 | static int btf_int_check_member(struct btf_verifier_env *env, |
2029 | const struct btf_type *struct_type, | |
2030 | const struct btf_member *member, | |
2031 | const struct btf_type *member_type) | |
2032 | { | |
2033 | u32 int_data = btf_type_int(member_type); | |
2034 | u32 struct_bits_off = member->offset; | |
2035 | u32 struct_size = struct_type->size; | |
2036 | u32 nr_copy_bits; | |
2037 | u32 bytes_offset; | |
2038 | ||
2039 | if (U32_MAX - struct_bits_off < BTF_INT_OFFSET(int_data)) { | |
2040 | btf_verifier_log_member(env, struct_type, member, | |
2041 | "bits_offset exceeds U32_MAX"); | |
2042 | return -EINVAL; | |
2043 | } | |
2044 | ||
2045 | struct_bits_off += BTF_INT_OFFSET(int_data); | |
2046 | bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off); | |
2047 | nr_copy_bits = BTF_INT_BITS(int_data) + | |
2048 | BITS_PER_BYTE_MASKED(struct_bits_off); | |
2049 | ||
b1e8818c | 2050 | if (nr_copy_bits > BITS_PER_U128) { |
179cde8c | 2051 | btf_verifier_log_member(env, struct_type, member, |
b1e8818c | 2052 | "nr_copy_bits exceeds 128"); |
179cde8c MKL |
2053 | return -EINVAL; |
2054 | } | |
2055 | ||
2056 | if (struct_size < bytes_offset || | |
2057 | struct_size - bytes_offset < BITS_ROUNDUP_BYTES(nr_copy_bits)) { | |
2058 | btf_verifier_log_member(env, struct_type, member, | |
2059 | "Member exceeds struct_size"); | |
2060 | return -EINVAL; | |
2061 | } | |
2062 | ||
2063 | return 0; | |
2064 | } | |
2065 | ||
9d5f9f70 YS |
2066 | static int btf_int_check_kflag_member(struct btf_verifier_env *env, |
2067 | const struct btf_type *struct_type, | |
2068 | const struct btf_member *member, | |
2069 | const struct btf_type *member_type) | |
2070 | { | |
2071 | u32 struct_bits_off, nr_bits, nr_int_data_bits, bytes_offset; | |
2072 | u32 int_data = btf_type_int(member_type); | |
2073 | u32 struct_size = struct_type->size; | |
2074 | u32 nr_copy_bits; | |
2075 | ||
2076 | /* a regular int type is required for the kflag int member */ | |
2077 | if (!btf_type_int_is_regular(member_type)) { | |
2078 | btf_verifier_log_member(env, struct_type, member, | |
2079 | "Invalid member base type"); | |
2080 | return -EINVAL; | |
2081 | } | |
2082 | ||
2083 | /* check sanity of bitfield size */ | |
2084 | nr_bits = BTF_MEMBER_BITFIELD_SIZE(member->offset); | |
2085 | struct_bits_off = BTF_MEMBER_BIT_OFFSET(member->offset); | |
2086 | nr_int_data_bits = BTF_INT_BITS(int_data); | |
2087 | if (!nr_bits) { | |
2088 | /* Not a bitfield member, member offset must be at byte | |
2089 | * boundary. | |
2090 | */ | |
2091 | if (BITS_PER_BYTE_MASKED(struct_bits_off)) { | |
2092 | btf_verifier_log_member(env, struct_type, member, | |
2093 | "Invalid member offset"); | |
2094 | return -EINVAL; | |
2095 | } | |
2096 | ||
2097 | nr_bits = nr_int_data_bits; | |
2098 | } else if (nr_bits > nr_int_data_bits) { | |
2099 | btf_verifier_log_member(env, struct_type, member, | |
2100 | "Invalid member bitfield_size"); | |
2101 | return -EINVAL; | |
2102 | } | |
2103 | ||
2104 | bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off); | |
2105 | nr_copy_bits = nr_bits + BITS_PER_BYTE_MASKED(struct_bits_off); | |
b1e8818c | 2106 | if (nr_copy_bits > BITS_PER_U128) { |
9d5f9f70 | 2107 | btf_verifier_log_member(env, struct_type, member, |
b1e8818c | 2108 | "nr_copy_bits exceeds 128"); |
9d5f9f70 YS |
2109 | return -EINVAL; |
2110 | } | |
2111 | ||
2112 | if (struct_size < bytes_offset || | |
2113 | struct_size - bytes_offset < BITS_ROUNDUP_BYTES(nr_copy_bits)) { | |
2114 | btf_verifier_log_member(env, struct_type, member, | |
2115 | "Member exceeds struct_size"); | |
2116 | return -EINVAL; | |
2117 | } | |
2118 | ||
2119 | return 0; | |
2120 | } | |
2121 | ||
69b693f0 MKL |
2122 | static s32 btf_int_check_meta(struct btf_verifier_env *env, |
2123 | const struct btf_type *t, | |
2124 | u32 meta_left) | |
2125 | { | |
2126 | u32 int_data, nr_bits, meta_needed = sizeof(int_data); | |
2127 | u16 encoding; | |
2128 | ||
2129 | if (meta_left < meta_needed) { | |
2130 | btf_verifier_log_basic(env, t, | |
2131 | "meta_left:%u meta_needed:%u", | |
2132 | meta_left, meta_needed); | |
2133 | return -EINVAL; | |
2134 | } | |
2135 | ||
2136 | if (btf_type_vlen(t)) { | |
2137 | btf_verifier_log_type(env, t, "vlen != 0"); | |
2138 | return -EINVAL; | |
2139 | } | |
2140 | ||
9d5f9f70 YS |
2141 | if (btf_type_kflag(t)) { |
2142 | btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); | |
2143 | return -EINVAL; | |
2144 | } | |
2145 | ||
69b693f0 | 2146 | int_data = btf_type_int(t); |
aea2f7b8 MKL |
2147 | if (int_data & ~BTF_INT_MASK) { |
2148 | btf_verifier_log_basic(env, t, "Invalid int_data:%x", | |
2149 | int_data); | |
2150 | return -EINVAL; | |
2151 | } | |
2152 | ||
69b693f0 MKL |
2153 | nr_bits = BTF_INT_BITS(int_data) + BTF_INT_OFFSET(int_data); |
2154 | ||
b1e8818c | 2155 | if (nr_bits > BITS_PER_U128) { |
69b693f0 | 2156 | btf_verifier_log_type(env, t, "nr_bits exceeds %zu", |
b1e8818c | 2157 | BITS_PER_U128); |
69b693f0 MKL |
2158 | return -EINVAL; |
2159 | } | |
2160 | ||
2161 | if (BITS_ROUNDUP_BYTES(nr_bits) > t->size) { | |
2162 | btf_verifier_log_type(env, t, "nr_bits exceeds type_size"); | |
2163 | return -EINVAL; | |
2164 | } | |
2165 | ||
aea2f7b8 MKL |
2166 | /* |
2167 | * Only one of the encoding bits is allowed and it | |
2168 | * should be sufficient for the pretty print purpose (i.e. decoding). | |
2169 | * Multiple bits can be allowed later if it is found | |
2170 | * to be insufficient. | |
2171 | */ | |
69b693f0 MKL |
2172 | encoding = BTF_INT_ENCODING(int_data); |
2173 | if (encoding && | |
2174 | encoding != BTF_INT_SIGNED && | |
2175 | encoding != BTF_INT_CHAR && | |
aea2f7b8 | 2176 | encoding != BTF_INT_BOOL) { |
69b693f0 MKL |
2177 | btf_verifier_log_type(env, t, "Unsupported encoding"); |
2178 | return -ENOTSUPP; | |
2179 | } | |
2180 | ||
2181 | btf_verifier_log_type(env, t, NULL); | |
2182 | ||
2183 | return meta_needed; | |
2184 | } | |
2185 | ||
2186 | static void btf_int_log(struct btf_verifier_env *env, | |
2187 | const struct btf_type *t) | |
2188 | { | |
2189 | int int_data = btf_type_int(t); | |
2190 | ||
2191 | btf_verifier_log(env, | |
2192 | "size=%u bits_offset=%u nr_bits=%u encoding=%s", | |
2193 | t->size, BTF_INT_OFFSET(int_data), | |
2194 | BTF_INT_BITS(int_data), | |
2195 | btf_int_encoding_str(BTF_INT_ENCODING(int_data))); | |
2196 | } | |
2197 | ||
31d0bc81 | 2198 | static void btf_int128_print(struct btf_show *show, void *data) |
b1e8818c YS |
2199 | { |
2200 | /* data points to a __int128 number. | |
2201 | * Suppose | |
2202 | * int128_num = *(__int128 *)data; | |
2203 | * The below formulas shows what upper_num and lower_num represents: | |
2204 | * upper_num = int128_num >> 64; | |
2205 | * lower_num = int128_num & 0xffffffffFFFFFFFFULL; | |
2206 | */ | |
2207 | u64 upper_num, lower_num; | |
2208 | ||
2209 | #ifdef __BIG_ENDIAN_BITFIELD | |
2210 | upper_num = *(u64 *)data; | |
2211 | lower_num = *(u64 *)(data + 8); | |
2212 | #else | |
2213 | upper_num = *(u64 *)(data + 8); | |
2214 | lower_num = *(u64 *)data; | |
2215 | #endif | |
2216 | if (upper_num == 0) | |
31d0bc81 | 2217 | btf_show_type_value(show, "0x%llx", lower_num); |
b1e8818c | 2218 | else |
31d0bc81 AM |
2219 | btf_show_type_values(show, "0x%llx%016llx", upper_num, |
2220 | lower_num); | |
b1e8818c YS |
2221 | } |
2222 | ||
2223 | static void btf_int128_shift(u64 *print_num, u16 left_shift_bits, | |
2224 | u16 right_shift_bits) | |
2225 | { | |
2226 | u64 upper_num, lower_num; | |
2227 | ||
2228 | #ifdef __BIG_ENDIAN_BITFIELD | |
2229 | upper_num = print_num[0]; | |
2230 | lower_num = print_num[1]; | |
2231 | #else | |
2232 | upper_num = print_num[1]; | |
2233 | lower_num = print_num[0]; | |
2234 | #endif | |
2235 | ||
2236 | /* shake out un-needed bits by shift/or operations */ | |
2237 | if (left_shift_bits >= 64) { | |
2238 | upper_num = lower_num << (left_shift_bits - 64); | |
2239 | lower_num = 0; | |
2240 | } else { | |
2241 | upper_num = (upper_num << left_shift_bits) | | |
2242 | (lower_num >> (64 - left_shift_bits)); | |
2243 | lower_num = lower_num << left_shift_bits; | |
2244 | } | |
2245 | ||
2246 | if (right_shift_bits >= 64) { | |
2247 | lower_num = upper_num >> (right_shift_bits - 64); | |
2248 | upper_num = 0; | |
2249 | } else { | |
2250 | lower_num = (lower_num >> right_shift_bits) | | |
2251 | (upper_num << (64 - right_shift_bits)); | |
2252 | upper_num = upper_num >> right_shift_bits; | |
2253 | } | |
2254 | ||
2255 | #ifdef __BIG_ENDIAN_BITFIELD | |
2256 | print_num[0] = upper_num; | |
2257 | print_num[1] = lower_num; | |
2258 | #else | |
2259 | print_num[0] = lower_num; | |
2260 | print_num[1] = upper_num; | |
2261 | #endif | |
2262 | } | |
2263 | ||
31d0bc81 AM |
2264 | static void btf_bitfield_show(void *data, u8 bits_offset, |
2265 | u8 nr_bits, struct btf_show *show) | |
b00b8dae | 2266 | { |
b65f370d | 2267 | u16 left_shift_bits, right_shift_bits; |
36fc3c8c MKL |
2268 | u8 nr_copy_bytes; |
2269 | u8 nr_copy_bits; | |
b1e8818c | 2270 | u64 print_num[2] = {}; |
b00b8dae | 2271 | |
b00b8dae MKL |
2272 | nr_copy_bits = nr_bits + bits_offset; |
2273 | nr_copy_bytes = BITS_ROUNDUP_BYTES(nr_copy_bits); | |
2274 | ||
b1e8818c | 2275 | memcpy(print_num, data, nr_copy_bytes); |
b00b8dae | 2276 | |
b65f370d OK |
2277 | #ifdef __BIG_ENDIAN_BITFIELD |
2278 | left_shift_bits = bits_offset; | |
2279 | #else | |
b1e8818c | 2280 | left_shift_bits = BITS_PER_U128 - nr_copy_bits; |
b65f370d | 2281 | #endif |
b1e8818c | 2282 | right_shift_bits = BITS_PER_U128 - nr_bits; |
b00b8dae | 2283 | |
b1e8818c | 2284 | btf_int128_shift(print_num, left_shift_bits, right_shift_bits); |
31d0bc81 | 2285 | btf_int128_print(show, print_num); |
b00b8dae MKL |
2286 | } |
2287 | ||
9d5f9f70 | 2288 | |
31d0bc81 AM |
2289 | static void btf_int_bits_show(const struct btf *btf, |
2290 | const struct btf_type *t, | |
2291 | void *data, u8 bits_offset, | |
2292 | struct btf_show *show) | |
f97be3ab YS |
2293 | { |
2294 | u32 int_data = btf_type_int(t); | |
2295 | u8 nr_bits = BTF_INT_BITS(int_data); | |
2296 | u8 total_bits_offset; | |
2297 | ||
2298 | /* | |
2299 | * bits_offset is at most 7. | |
b1e8818c | 2300 | * BTF_INT_OFFSET() cannot exceed 128 bits. |
f97be3ab YS |
2301 | */ |
2302 | total_bits_offset = bits_offset + BTF_INT_OFFSET(int_data); | |
17e3ac81 YS |
2303 | data += BITS_ROUNDDOWN_BYTES(total_bits_offset); |
2304 | bits_offset = BITS_PER_BYTE_MASKED(total_bits_offset); | |
31d0bc81 | 2305 | btf_bitfield_show(data, bits_offset, nr_bits, show); |
f97be3ab YS |
2306 | } |
2307 | ||
31d0bc81 AM |
2308 | static void btf_int_show(const struct btf *btf, const struct btf_type *t, |
2309 | u32 type_id, void *data, u8 bits_offset, | |
2310 | struct btf_show *show) | |
b00b8dae MKL |
2311 | { |
2312 | u32 int_data = btf_type_int(t); | |
2313 | u8 encoding = BTF_INT_ENCODING(int_data); | |
2314 | bool sign = encoding & BTF_INT_SIGNED; | |
36fc3c8c | 2315 | u8 nr_bits = BTF_INT_BITS(int_data); |
31d0bc81 AM |
2316 | void *safe_data; |
2317 | ||
2318 | safe_data = btf_show_start_type(show, t, type_id, data); | |
2319 | if (!safe_data) | |
2320 | return; | |
b00b8dae MKL |
2321 | |
2322 | if (bits_offset || BTF_INT_OFFSET(int_data) || | |
2323 | BITS_PER_BYTE_MASKED(nr_bits)) { | |
31d0bc81 AM |
2324 | btf_int_bits_show(btf, t, safe_data, bits_offset, show); |
2325 | goto out; | |
b00b8dae MKL |
2326 | } |
2327 | ||
2328 | switch (nr_bits) { | |
b1e8818c | 2329 | case 128: |
31d0bc81 | 2330 | btf_int128_print(show, safe_data); |
b1e8818c | 2331 | break; |
b00b8dae MKL |
2332 | case 64: |
2333 | if (sign) | |
31d0bc81 | 2334 | btf_show_type_value(show, "%lld", *(s64 *)safe_data); |
b00b8dae | 2335 | else |
31d0bc81 | 2336 | btf_show_type_value(show, "%llu", *(u64 *)safe_data); |
b00b8dae MKL |
2337 | break; |
2338 | case 32: | |
2339 | if (sign) | |
31d0bc81 | 2340 | btf_show_type_value(show, "%d", *(s32 *)safe_data); |
b00b8dae | 2341 | else |
31d0bc81 | 2342 | btf_show_type_value(show, "%u", *(u32 *)safe_data); |
b00b8dae MKL |
2343 | break; |
2344 | case 16: | |
2345 | if (sign) | |
31d0bc81 | 2346 | btf_show_type_value(show, "%d", *(s16 *)safe_data); |
b00b8dae | 2347 | else |
31d0bc81 | 2348 | btf_show_type_value(show, "%u", *(u16 *)safe_data); |
b00b8dae MKL |
2349 | break; |
2350 | case 8: | |
31d0bc81 AM |
2351 | if (show->state.array_encoding == BTF_INT_CHAR) { |
2352 | /* check for null terminator */ | |
2353 | if (show->state.array_terminated) | |
2354 | break; | |
2355 | if (*(char *)data == '\0') { | |
2356 | show->state.array_terminated = 1; | |
2357 | break; | |
2358 | } | |
2359 | if (isprint(*(char *)data)) { | |
2360 | btf_show_type_value(show, "'%c'", | |
2361 | *(char *)safe_data); | |
2362 | break; | |
2363 | } | |
2364 | } | |
b00b8dae | 2365 | if (sign) |
31d0bc81 | 2366 | btf_show_type_value(show, "%d", *(s8 *)safe_data); |
b00b8dae | 2367 | else |
31d0bc81 | 2368 | btf_show_type_value(show, "%u", *(u8 *)safe_data); |
b00b8dae MKL |
2369 | break; |
2370 | default: | |
31d0bc81 AM |
2371 | btf_int_bits_show(btf, t, safe_data, bits_offset, show); |
2372 | break; | |
b00b8dae | 2373 | } |
31d0bc81 AM |
2374 | out: |
2375 | btf_show_end_type(show); | |
b00b8dae MKL |
2376 | } |
2377 | ||
69b693f0 MKL |
2378 | static const struct btf_kind_operations int_ops = { |
2379 | .check_meta = btf_int_check_meta, | |
eb3f595d | 2380 | .resolve = btf_df_resolve, |
179cde8c | 2381 | .check_member = btf_int_check_member, |
9d5f9f70 | 2382 | .check_kflag_member = btf_int_check_kflag_member, |
69b693f0 | 2383 | .log_details = btf_int_log, |
31d0bc81 | 2384 | .show = btf_int_show, |
69b693f0 MKL |
2385 | }; |
2386 | ||
179cde8c MKL |
2387 | static int btf_modifier_check_member(struct btf_verifier_env *env, |
2388 | const struct btf_type *struct_type, | |
2389 | const struct btf_member *member, | |
2390 | const struct btf_type *member_type) | |
2391 | { | |
2392 | const struct btf_type *resolved_type; | |
2393 | u32 resolved_type_id = member->type; | |
2394 | struct btf_member resolved_member; | |
2395 | struct btf *btf = env->btf; | |
2396 | ||
2397 | resolved_type = btf_type_id_size(btf, &resolved_type_id, NULL); | |
2398 | if (!resolved_type) { | |
2399 | btf_verifier_log_member(env, struct_type, member, | |
2400 | "Invalid member"); | |
2401 | return -EINVAL; | |
2402 | } | |
2403 | ||
2404 | resolved_member = *member; | |
2405 | resolved_member.type = resolved_type_id; | |
2406 | ||
2407 | return btf_type_ops(resolved_type)->check_member(env, struct_type, | |
2408 | &resolved_member, | |
2409 | resolved_type); | |
2410 | } | |
2411 | ||
9d5f9f70 YS |
2412 | static int btf_modifier_check_kflag_member(struct btf_verifier_env *env, |
2413 | const struct btf_type *struct_type, | |
2414 | const struct btf_member *member, | |
2415 | const struct btf_type *member_type) | |
2416 | { | |
2417 | const struct btf_type *resolved_type; | |
2418 | u32 resolved_type_id = member->type; | |
2419 | struct btf_member resolved_member; | |
2420 | struct btf *btf = env->btf; | |
2421 | ||
2422 | resolved_type = btf_type_id_size(btf, &resolved_type_id, NULL); | |
2423 | if (!resolved_type) { | |
2424 | btf_verifier_log_member(env, struct_type, member, | |
2425 | "Invalid member"); | |
2426 | return -EINVAL; | |
2427 | } | |
2428 | ||
2429 | resolved_member = *member; | |
2430 | resolved_member.type = resolved_type_id; | |
2431 | ||
2432 | return btf_type_ops(resolved_type)->check_kflag_member(env, struct_type, | |
2433 | &resolved_member, | |
2434 | resolved_type); | |
2435 | } | |
2436 | ||
179cde8c MKL |
2437 | static int btf_ptr_check_member(struct btf_verifier_env *env, |
2438 | const struct btf_type *struct_type, | |
2439 | const struct btf_member *member, | |
2440 | const struct btf_type *member_type) | |
2441 | { | |
2442 | u32 struct_size, struct_bits_off, bytes_offset; | |
2443 | ||
2444 | struct_size = struct_type->size; | |
2445 | struct_bits_off = member->offset; | |
2446 | bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off); | |
2447 | ||
2448 | if (BITS_PER_BYTE_MASKED(struct_bits_off)) { | |
2449 | btf_verifier_log_member(env, struct_type, member, | |
2450 | "Member is not byte aligned"); | |
2451 | return -EINVAL; | |
2452 | } | |
2453 | ||
2454 | if (struct_size - bytes_offset < sizeof(void *)) { | |
2455 | btf_verifier_log_member(env, struct_type, member, | |
2456 | "Member exceeds struct_size"); | |
2457 | return -EINVAL; | |
2458 | } | |
2459 | ||
2460 | return 0; | |
2461 | } | |
2462 | ||
69b693f0 MKL |
2463 | static int btf_ref_type_check_meta(struct btf_verifier_env *env, |
2464 | const struct btf_type *t, | |
2465 | u32 meta_left) | |
2466 | { | |
8c42d2fa YS |
2467 | const char *value; |
2468 | ||
69b693f0 MKL |
2469 | if (btf_type_vlen(t)) { |
2470 | btf_verifier_log_type(env, t, "vlen != 0"); | |
2471 | return -EINVAL; | |
2472 | } | |
2473 | ||
9d5f9f70 YS |
2474 | if (btf_type_kflag(t)) { |
2475 | btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); | |
2476 | return -EINVAL; | |
2477 | } | |
2478 | ||
aea2f7b8 | 2479 | if (!BTF_TYPE_ID_VALID(t->type)) { |
69b693f0 MKL |
2480 | btf_verifier_log_type(env, t, "Invalid type_id"); |
2481 | return -EINVAL; | |
2482 | } | |
2483 | ||
8c42d2fa | 2484 | /* typedef/type_tag type must have a valid name, and other ref types, |
eb04bbb6 YS |
2485 | * volatile, const, restrict, should have a null name. |
2486 | */ | |
2487 | if (BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF) { | |
2488 | if (!t->name_off || | |
2489 | !btf_name_valid_identifier(env->btf, t->name_off)) { | |
2490 | btf_verifier_log_type(env, t, "Invalid name"); | |
2491 | return -EINVAL; | |
2492 | } | |
8c42d2fa YS |
2493 | } else if (BTF_INFO_KIND(t->info) == BTF_KIND_TYPE_TAG) { |
2494 | value = btf_name_by_offset(env->btf, t->name_off); | |
2495 | if (!value || !value[0]) { | |
2496 | btf_verifier_log_type(env, t, "Invalid name"); | |
2497 | return -EINVAL; | |
2498 | } | |
eb04bbb6 YS |
2499 | } else { |
2500 | if (t->name_off) { | |
2501 | btf_verifier_log_type(env, t, "Invalid name"); | |
2502 | return -EINVAL; | |
2503 | } | |
2504 | } | |
2505 | ||
69b693f0 MKL |
2506 | btf_verifier_log_type(env, t, NULL); |
2507 | ||
2508 | return 0; | |
2509 | } | |
2510 | ||
eb3f595d MKL |
2511 | static int btf_modifier_resolve(struct btf_verifier_env *env, |
2512 | const struct resolve_vertex *v) | |
2513 | { | |
2514 | const struct btf_type *t = v->t; | |
2515 | const struct btf_type *next_type; | |
2516 | u32 next_type_id = t->type; | |
2517 | struct btf *btf = env->btf; | |
eb3f595d MKL |
2518 | |
2519 | next_type = btf_type_by_id(btf, next_type_id); | |
1dc92851 | 2520 | if (!next_type || btf_type_is_resolve_source_only(next_type)) { |
eb3f595d MKL |
2521 | btf_verifier_log_type(env, v->t, "Invalid type_id"); |
2522 | return -EINVAL; | |
2523 | } | |
2524 | ||
eb3f595d MKL |
2525 | if (!env_type_is_resolve_sink(env, next_type) && |
2526 | !env_type_is_resolved(env, next_type_id)) | |
2527 | return env_stack_push(env, next_type, next_type_id); | |
2528 | ||
2529 | /* Figure out the resolved next_type_id with size. | |
2530 | * They will be stored in the current modifier's | |
2531 | * resolved_ids and resolved_sizes such that it can | |
2532 | * save us a few type-following when we use it later (e.g. in | |
2533 | * pretty print). | |
2534 | */ | |
1acc5d5c | 2535 | if (!btf_type_id_size(btf, &next_type_id, NULL)) { |
2667a262 MKL |
2536 | if (env_type_is_resolved(env, next_type_id)) |
2537 | next_type = btf_type_id_resolve(btf, &next_type_id); | |
2538 | ||
2539 | /* "typedef void new_void", "const void"...etc */ | |
2540 | if (!btf_type_is_void(next_type) && | |
81f5c6f5 YS |
2541 | !btf_type_is_fwd(next_type) && |
2542 | !btf_type_is_func_proto(next_type)) { | |
2667a262 MKL |
2543 | btf_verifier_log_type(env, v->t, "Invalid type_id"); |
2544 | return -EINVAL; | |
2545 | } | |
eb3f595d MKL |
2546 | } |
2547 | ||
1acc5d5c | 2548 | env_stack_pop_resolved(env, next_type_id, 0); |
eb3f595d MKL |
2549 | |
2550 | return 0; | |
2551 | } | |
2552 | ||
1dc92851 DB |
2553 | static int btf_var_resolve(struct btf_verifier_env *env, |
2554 | const struct resolve_vertex *v) | |
2555 | { | |
2556 | const struct btf_type *next_type; | |
2557 | const struct btf_type *t = v->t; | |
2558 | u32 next_type_id = t->type; | |
2559 | struct btf *btf = env->btf; | |
1dc92851 DB |
2560 | |
2561 | next_type = btf_type_by_id(btf, next_type_id); | |
2562 | if (!next_type || btf_type_is_resolve_source_only(next_type)) { | |
2563 | btf_verifier_log_type(env, v->t, "Invalid type_id"); | |
2564 | return -EINVAL; | |
2565 | } | |
2566 | ||
2567 | if (!env_type_is_resolve_sink(env, next_type) && | |
2568 | !env_type_is_resolved(env, next_type_id)) | |
2569 | return env_stack_push(env, next_type, next_type_id); | |
2570 | ||
2571 | if (btf_type_is_modifier(next_type)) { | |
2572 | const struct btf_type *resolved_type; | |
2573 | u32 resolved_type_id; | |
2574 | ||
2575 | resolved_type_id = next_type_id; | |
2576 | resolved_type = btf_type_id_resolve(btf, &resolved_type_id); | |
2577 | ||
2578 | if (btf_type_is_ptr(resolved_type) && | |
2579 | !env_type_is_resolve_sink(env, resolved_type) && | |
2580 | !env_type_is_resolved(env, resolved_type_id)) | |
2581 | return env_stack_push(env, resolved_type, | |
2582 | resolved_type_id); | |
2583 | } | |
2584 | ||
2585 | /* We must resolve to something concrete at this point, no | |
2586 | * forward types or similar that would resolve to size of | |
2587 | * zero is allowed. | |
2588 | */ | |
1acc5d5c | 2589 | if (!btf_type_id_size(btf, &next_type_id, NULL)) { |
1dc92851 DB |
2590 | btf_verifier_log_type(env, v->t, "Invalid type_id"); |
2591 | return -EINVAL; | |
2592 | } | |
2593 | ||
1acc5d5c | 2594 | env_stack_pop_resolved(env, next_type_id, 0); |
1dc92851 DB |
2595 | |
2596 | return 0; | |
2597 | } | |
2598 | ||
eb3f595d MKL |
2599 | static int btf_ptr_resolve(struct btf_verifier_env *env, |
2600 | const struct resolve_vertex *v) | |
2601 | { | |
2602 | const struct btf_type *next_type; | |
2603 | const struct btf_type *t = v->t; | |
2604 | u32 next_type_id = t->type; | |
2605 | struct btf *btf = env->btf; | |
eb3f595d MKL |
2606 | |
2607 | next_type = btf_type_by_id(btf, next_type_id); | |
1dc92851 | 2608 | if (!next_type || btf_type_is_resolve_source_only(next_type)) { |
eb3f595d MKL |
2609 | btf_verifier_log_type(env, v->t, "Invalid type_id"); |
2610 | return -EINVAL; | |
2611 | } | |
2612 | ||
eb3f595d MKL |
2613 | if (!env_type_is_resolve_sink(env, next_type) && |
2614 | !env_type_is_resolved(env, next_type_id)) | |
2615 | return env_stack_push(env, next_type, next_type_id); | |
2616 | ||
2617 | /* If the modifier was RESOLVED during RESOLVE_STRUCT_OR_ARRAY, | |
2618 | * the modifier may have stopped resolving when it was resolved | |
2619 | * to a ptr (last-resolved-ptr). | |
2620 | * | |
2621 | * We now need to continue from the last-resolved-ptr to | |
2622 | * ensure the last-resolved-ptr will not referring back to | |
c561d110 | 2623 | * the current ptr (t). |
eb3f595d MKL |
2624 | */ |
2625 | if (btf_type_is_modifier(next_type)) { | |
2626 | const struct btf_type *resolved_type; | |
2627 | u32 resolved_type_id; | |
2628 | ||
2629 | resolved_type_id = next_type_id; | |
2630 | resolved_type = btf_type_id_resolve(btf, &resolved_type_id); | |
2631 | ||
2632 | if (btf_type_is_ptr(resolved_type) && | |
2633 | !env_type_is_resolve_sink(env, resolved_type) && | |
2634 | !env_type_is_resolved(env, resolved_type_id)) | |
2635 | return env_stack_push(env, resolved_type, | |
2636 | resolved_type_id); | |
2637 | } | |
2638 | ||
2667a262 MKL |
2639 | if (!btf_type_id_size(btf, &next_type_id, NULL)) { |
2640 | if (env_type_is_resolved(env, next_type_id)) | |
2641 | next_type = btf_type_id_resolve(btf, &next_type_id); | |
2642 | ||
2643 | if (!btf_type_is_void(next_type) && | |
2644 | !btf_type_is_fwd(next_type) && | |
2645 | !btf_type_is_func_proto(next_type)) { | |
2646 | btf_verifier_log_type(env, v->t, "Invalid type_id"); | |
2647 | return -EINVAL; | |
2648 | } | |
eb3f595d MKL |
2649 | } |
2650 | ||
eb3f595d MKL |
2651 | env_stack_pop_resolved(env, next_type_id, 0); |
2652 | ||
2653 | return 0; | |
2654 | } | |
2655 | ||
31d0bc81 AM |
2656 | static void btf_modifier_show(const struct btf *btf, |
2657 | const struct btf_type *t, | |
2658 | u32 type_id, void *data, | |
2659 | u8 bits_offset, struct btf_show *show) | |
b00b8dae | 2660 | { |
85d33df3 MKL |
2661 | if (btf->resolved_ids) |
2662 | t = btf_type_id_resolve(btf, &type_id); | |
2663 | else | |
2664 | t = btf_type_skip_modifiers(btf, type_id, NULL); | |
b00b8dae | 2665 | |
31d0bc81 | 2666 | btf_type_ops(t)->show(btf, t, type_id, data, bits_offset, show); |
b00b8dae MKL |
2667 | } |
2668 | ||
31d0bc81 AM |
2669 | static void btf_var_show(const struct btf *btf, const struct btf_type *t, |
2670 | u32 type_id, void *data, u8 bits_offset, | |
2671 | struct btf_show *show) | |
1dc92851 DB |
2672 | { |
2673 | t = btf_type_id_resolve(btf, &type_id); | |
2674 | ||
31d0bc81 | 2675 | btf_type_ops(t)->show(btf, t, type_id, data, bits_offset, show); |
1dc92851 DB |
2676 | } |
2677 | ||
31d0bc81 AM |
2678 | static void btf_ptr_show(const struct btf *btf, const struct btf_type *t, |
2679 | u32 type_id, void *data, u8 bits_offset, | |
2680 | struct btf_show *show) | |
b00b8dae | 2681 | { |
31d0bc81 AM |
2682 | void *safe_data; |
2683 | ||
2684 | safe_data = btf_show_start_type(show, t, type_id, data); | |
2685 | if (!safe_data) | |
2686 | return; | |
2687 | ||
2688 | /* It is a hashed value unless BTF_SHOW_PTR_RAW is specified */ | |
2689 | if (show->flags & BTF_SHOW_PTR_RAW) | |
2690 | btf_show_type_value(show, "0x%px", *(void **)safe_data); | |
2691 | else | |
2692 | btf_show_type_value(show, "0x%p", *(void **)safe_data); | |
2693 | btf_show_end_type(show); | |
b00b8dae MKL |
2694 | } |
2695 | ||
69b693f0 MKL |
2696 | static void btf_ref_type_log(struct btf_verifier_env *env, |
2697 | const struct btf_type *t) | |
2698 | { | |
2699 | btf_verifier_log(env, "type_id=%u", t->type); | |
2700 | } | |
2701 | ||
2702 | static struct btf_kind_operations modifier_ops = { | |
2703 | .check_meta = btf_ref_type_check_meta, | |
eb3f595d | 2704 | .resolve = btf_modifier_resolve, |
179cde8c | 2705 | .check_member = btf_modifier_check_member, |
9d5f9f70 | 2706 | .check_kflag_member = btf_modifier_check_kflag_member, |
69b693f0 | 2707 | .log_details = btf_ref_type_log, |
31d0bc81 | 2708 | .show = btf_modifier_show, |
69b693f0 MKL |
2709 | }; |
2710 | ||
2711 | static struct btf_kind_operations ptr_ops = { | |
2712 | .check_meta = btf_ref_type_check_meta, | |
eb3f595d | 2713 | .resolve = btf_ptr_resolve, |
179cde8c | 2714 | .check_member = btf_ptr_check_member, |
9d5f9f70 | 2715 | .check_kflag_member = btf_generic_check_kflag_member, |
69b693f0 | 2716 | .log_details = btf_ref_type_log, |
31d0bc81 | 2717 | .show = btf_ptr_show, |
69b693f0 MKL |
2718 | }; |
2719 | ||
8175383f MKL |
2720 | static s32 btf_fwd_check_meta(struct btf_verifier_env *env, |
2721 | const struct btf_type *t, | |
2722 | u32 meta_left) | |
2723 | { | |
2724 | if (btf_type_vlen(t)) { | |
2725 | btf_verifier_log_type(env, t, "vlen != 0"); | |
2726 | return -EINVAL; | |
2727 | } | |
2728 | ||
2729 | if (t->type) { | |
2730 | btf_verifier_log_type(env, t, "type != 0"); | |
2731 | return -EINVAL; | |
2732 | } | |
2733 | ||
eb04bbb6 YS |
2734 | /* fwd type must have a valid name */ |
2735 | if (!t->name_off || | |
2736 | !btf_name_valid_identifier(env->btf, t->name_off)) { | |
2737 | btf_verifier_log_type(env, t, "Invalid name"); | |
2738 | return -EINVAL; | |
2739 | } | |
2740 | ||
8175383f MKL |
2741 | btf_verifier_log_type(env, t, NULL); |
2742 | ||
2743 | return 0; | |
2744 | } | |
2745 | ||
76c43ae8 YS |
2746 | static void btf_fwd_type_log(struct btf_verifier_env *env, |
2747 | const struct btf_type *t) | |
2748 | { | |
2749 | btf_verifier_log(env, "%s", btf_type_kflag(t) ? "union" : "struct"); | |
2750 | } | |
2751 | ||
69b693f0 | 2752 | static struct btf_kind_operations fwd_ops = { |
8175383f | 2753 | .check_meta = btf_fwd_check_meta, |
eb3f595d | 2754 | .resolve = btf_df_resolve, |
179cde8c | 2755 | .check_member = btf_df_check_member, |
9d5f9f70 | 2756 | .check_kflag_member = btf_df_check_kflag_member, |
76c43ae8 | 2757 | .log_details = btf_fwd_type_log, |
31d0bc81 | 2758 | .show = btf_df_show, |
69b693f0 MKL |
2759 | }; |
2760 | ||
179cde8c MKL |
2761 | static int btf_array_check_member(struct btf_verifier_env *env, |
2762 | const struct btf_type *struct_type, | |
2763 | const struct btf_member *member, | |
2764 | const struct btf_type *member_type) | |
2765 | { | |
2766 | u32 struct_bits_off = member->offset; | |
2767 | u32 struct_size, bytes_offset; | |
2768 | u32 array_type_id, array_size; | |
2769 | struct btf *btf = env->btf; | |
2770 | ||
2771 | if (BITS_PER_BYTE_MASKED(struct_bits_off)) { | |
2772 | btf_verifier_log_member(env, struct_type, member, | |
2773 | "Member is not byte aligned"); | |
2774 | return -EINVAL; | |
2775 | } | |
2776 | ||
2777 | array_type_id = member->type; | |
2778 | btf_type_id_size(btf, &array_type_id, &array_size); | |
2779 | struct_size = struct_type->size; | |
2780 | bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off); | |
2781 | if (struct_size - bytes_offset < array_size) { | |
2782 | btf_verifier_log_member(env, struct_type, member, | |
2783 | "Member exceeds struct_size"); | |
2784 | return -EINVAL; | |
2785 | } | |
2786 | ||
2787 | return 0; | |
2788 | } | |
2789 | ||
69b693f0 MKL |
2790 | static s32 btf_array_check_meta(struct btf_verifier_env *env, |
2791 | const struct btf_type *t, | |
2792 | u32 meta_left) | |
2793 | { | |
2794 | const struct btf_array *array = btf_type_array(t); | |
2795 | u32 meta_needed = sizeof(*array); | |
2796 | ||
2797 | if (meta_left < meta_needed) { | |
2798 | btf_verifier_log_basic(env, t, | |
2799 | "meta_left:%u meta_needed:%u", | |
2800 | meta_left, meta_needed); | |
2801 | return -EINVAL; | |
2802 | } | |
2803 | ||
eb04bbb6 YS |
2804 | /* array type should not have a name */ |
2805 | if (t->name_off) { | |
2806 | btf_verifier_log_type(env, t, "Invalid name"); | |
2807 | return -EINVAL; | |
2808 | } | |
2809 | ||
69b693f0 MKL |
2810 | if (btf_type_vlen(t)) { |
2811 | btf_verifier_log_type(env, t, "vlen != 0"); | |
2812 | return -EINVAL; | |
2813 | } | |
2814 | ||
9d5f9f70 YS |
2815 | if (btf_type_kflag(t)) { |
2816 | btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); | |
2817 | return -EINVAL; | |
2818 | } | |
2819 | ||
b9308ae6 MKL |
2820 | if (t->size) { |
2821 | btf_verifier_log_type(env, t, "size != 0"); | |
2822 | return -EINVAL; | |
2823 | } | |
2824 | ||
4ef5f574 MKL |
2825 | /* Array elem type and index type cannot be in type void, |
2826 | * so !array->type and !array->index_type are not allowed. | |
69b693f0 | 2827 | */ |
aea2f7b8 | 2828 | if (!array->type || !BTF_TYPE_ID_VALID(array->type)) { |
4ef5f574 MKL |
2829 | btf_verifier_log_type(env, t, "Invalid elem"); |
2830 | return -EINVAL; | |
2831 | } | |
2832 | ||
aea2f7b8 | 2833 | if (!array->index_type || !BTF_TYPE_ID_VALID(array->index_type)) { |
4ef5f574 | 2834 | btf_verifier_log_type(env, t, "Invalid index"); |
69b693f0 MKL |
2835 | return -EINVAL; |
2836 | } | |
2837 | ||
2838 | btf_verifier_log_type(env, t, NULL); | |
2839 | ||
2840 | return meta_needed; | |
2841 | } | |
2842 | ||
eb3f595d MKL |
2843 | static int btf_array_resolve(struct btf_verifier_env *env, |
2844 | const struct resolve_vertex *v) | |
2845 | { | |
2846 | const struct btf_array *array = btf_type_array(v->t); | |
4ef5f574 MKL |
2847 | const struct btf_type *elem_type, *index_type; |
2848 | u32 elem_type_id, index_type_id; | |
eb3f595d MKL |
2849 | struct btf *btf = env->btf; |
2850 | u32 elem_size; | |
2851 | ||
4ef5f574 MKL |
2852 | /* Check array->index_type */ |
2853 | index_type_id = array->index_type; | |
2854 | index_type = btf_type_by_id(btf, index_type_id); | |
e4f07120 SF |
2855 | if (btf_type_nosize_or_null(index_type) || |
2856 | btf_type_is_resolve_source_only(index_type)) { | |
4ef5f574 MKL |
2857 | btf_verifier_log_type(env, v->t, "Invalid index"); |
2858 | return -EINVAL; | |
2859 | } | |
2860 | ||
2861 | if (!env_type_is_resolve_sink(env, index_type) && | |
2862 | !env_type_is_resolved(env, index_type_id)) | |
2863 | return env_stack_push(env, index_type, index_type_id); | |
2864 | ||
2865 | index_type = btf_type_id_size(btf, &index_type_id, NULL); | |
2866 | if (!index_type || !btf_type_is_int(index_type) || | |
2867 | !btf_type_int_is_regular(index_type)) { | |
2868 | btf_verifier_log_type(env, v->t, "Invalid index"); | |
2869 | return -EINVAL; | |
2870 | } | |
2871 | ||
2872 | /* Check array->type */ | |
2873 | elem_type_id = array->type; | |
eb3f595d | 2874 | elem_type = btf_type_by_id(btf, elem_type_id); |
e4f07120 SF |
2875 | if (btf_type_nosize_or_null(elem_type) || |
2876 | btf_type_is_resolve_source_only(elem_type)) { | |
eb3f595d MKL |
2877 | btf_verifier_log_type(env, v->t, |
2878 | "Invalid elem"); | |
2879 | return -EINVAL; | |
2880 | } | |
2881 | ||
2882 | if (!env_type_is_resolve_sink(env, elem_type) && | |
2883 | !env_type_is_resolved(env, elem_type_id)) | |
2884 | return env_stack_push(env, elem_type, elem_type_id); | |
2885 | ||
2886 | elem_type = btf_type_id_size(btf, &elem_type_id, &elem_size); | |
2887 | if (!elem_type) { | |
2888 | btf_verifier_log_type(env, v->t, "Invalid elem"); | |
2889 | return -EINVAL; | |
2890 | } | |
2891 | ||
4ef5f574 MKL |
2892 | if (btf_type_is_int(elem_type) && !btf_type_int_is_regular(elem_type)) { |
2893 | btf_verifier_log_type(env, v->t, "Invalid array of int"); | |
2894 | return -EINVAL; | |
eb3f595d MKL |
2895 | } |
2896 | ||
2897 | if (array->nelems && elem_size > U32_MAX / array->nelems) { | |
2898 | btf_verifier_log_type(env, v->t, | |
2899 | "Array size overflows U32_MAX"); | |
2900 | return -EINVAL; | |
2901 | } | |
2902 | ||
2903 | env_stack_pop_resolved(env, elem_type_id, elem_size * array->nelems); | |
2904 | ||
2905 | return 0; | |
2906 | } | |
2907 | ||
69b693f0 MKL |
2908 | static void btf_array_log(struct btf_verifier_env *env, |
2909 | const struct btf_type *t) | |
2910 | { | |
2911 | const struct btf_array *array = btf_type_array(t); | |
2912 | ||
2913 | btf_verifier_log(env, "type_id=%u index_type_id=%u nr_elems=%u", | |
2914 | array->type, array->index_type, array->nelems); | |
2915 | } | |
2916 | ||
31d0bc81 AM |
2917 | static void __btf_array_show(const struct btf *btf, const struct btf_type *t, |
2918 | u32 type_id, void *data, u8 bits_offset, | |
2919 | struct btf_show *show) | |
b00b8dae MKL |
2920 | { |
2921 | const struct btf_array *array = btf_type_array(t); | |
2922 | const struct btf_kind_operations *elem_ops; | |
2923 | const struct btf_type *elem_type; | |
31d0bc81 AM |
2924 | u32 i, elem_size = 0, elem_type_id; |
2925 | u16 encoding = 0; | |
b00b8dae MKL |
2926 | |
2927 | elem_type_id = array->type; | |
31d0bc81 AM |
2928 | elem_type = btf_type_skip_modifiers(btf, elem_type_id, NULL); |
2929 | if (elem_type && btf_type_has_size(elem_type)) | |
2930 | elem_size = elem_type->size; | |
2931 | ||
2932 | if (elem_type && btf_type_is_int(elem_type)) { | |
2933 | u32 int_type = btf_type_int(elem_type); | |
2934 | ||
2935 | encoding = BTF_INT_ENCODING(int_type); | |
2936 | ||
2937 | /* | |
2938 | * BTF_INT_CHAR encoding never seems to be set for | |
2939 | * char arrays, so if size is 1 and element is | |
2940 | * printable as a char, we'll do that. | |
2941 | */ | |
2942 | if (elem_size == 1) | |
2943 | encoding = BTF_INT_CHAR; | |
2944 | } | |
2945 | ||
2946 | if (!btf_show_start_array_type(show, t, type_id, encoding, data)) | |
2947 | return; | |
2948 | ||
2949 | if (!elem_type) | |
2950 | goto out; | |
b00b8dae | 2951 | elem_ops = btf_type_ops(elem_type); |
31d0bc81 | 2952 | |
b00b8dae | 2953 | for (i = 0; i < array->nelems; i++) { |
b00b8dae | 2954 | |
31d0bc81 AM |
2955 | btf_show_start_array_member(show); |
2956 | ||
2957 | elem_ops->show(btf, elem_type, elem_type_id, data, | |
2958 | bits_offset, show); | |
b00b8dae | 2959 | data += elem_size; |
31d0bc81 AM |
2960 | |
2961 | btf_show_end_array_member(show); | |
2962 | ||
2963 | if (show->state.array_terminated) | |
2964 | break; | |
b00b8dae | 2965 | } |
31d0bc81 AM |
2966 | out: |
2967 | btf_show_end_array_type(show); | |
2968 | } | |
2969 | ||
2970 | static void btf_array_show(const struct btf *btf, const struct btf_type *t, | |
2971 | u32 type_id, void *data, u8 bits_offset, | |
2972 | struct btf_show *show) | |
2973 | { | |
2974 | const struct btf_member *m = show->state.member; | |
2975 | ||
2976 | /* | |
2977 | * First check if any members would be shown (are non-zero). | |
2978 | * See comments above "struct btf_show" definition for more | |
2979 | * details on how this works at a high-level. | |
2980 | */ | |
2981 | if (show->state.depth > 0 && !(show->flags & BTF_SHOW_ZERO)) { | |
2982 | if (!show->state.depth_check) { | |
2983 | show->state.depth_check = show->state.depth + 1; | |
2984 | show->state.depth_to_show = 0; | |
2985 | } | |
2986 | __btf_array_show(btf, t, type_id, data, bits_offset, show); | |
2987 | show->state.member = m; | |
2988 | ||
2989 | if (show->state.depth_check != show->state.depth + 1) | |
2990 | return; | |
2991 | show->state.depth_check = 0; | |
2992 | ||
2993 | if (show->state.depth_to_show <= show->state.depth) | |
2994 | return; | |
2995 | /* | |
2996 | * Reaching here indicates we have recursed and found | |
2997 | * non-zero array member(s). | |
2998 | */ | |
2999 | } | |
3000 | __btf_array_show(btf, t, type_id, data, bits_offset, show); | |
b00b8dae MKL |
3001 | } |
3002 | ||
69b693f0 MKL |
3003 | static struct btf_kind_operations array_ops = { |
3004 | .check_meta = btf_array_check_meta, | |
eb3f595d | 3005 | .resolve = btf_array_resolve, |
179cde8c | 3006 | .check_member = btf_array_check_member, |
9d5f9f70 | 3007 | .check_kflag_member = btf_generic_check_kflag_member, |
69b693f0 | 3008 | .log_details = btf_array_log, |
31d0bc81 | 3009 | .show = btf_array_show, |
69b693f0 MKL |
3010 | }; |
3011 | ||
179cde8c MKL |
3012 | static int btf_struct_check_member(struct btf_verifier_env *env, |
3013 | const struct btf_type *struct_type, | |
3014 | const struct btf_member *member, | |
3015 | const struct btf_type *member_type) | |
3016 | { | |
3017 | u32 struct_bits_off = member->offset; | |
3018 | u32 struct_size, bytes_offset; | |
3019 | ||
3020 | if (BITS_PER_BYTE_MASKED(struct_bits_off)) { | |
3021 | btf_verifier_log_member(env, struct_type, member, | |
3022 | "Member is not byte aligned"); | |
3023 | return -EINVAL; | |
3024 | } | |
3025 | ||
3026 | struct_size = struct_type->size; | |
3027 | bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off); | |
3028 | if (struct_size - bytes_offset < member_type->size) { | |
3029 | btf_verifier_log_member(env, struct_type, member, | |
3030 | "Member exceeds struct_size"); | |
3031 | return -EINVAL; | |
3032 | } | |
3033 | ||
3034 | return 0; | |
3035 | } | |
3036 | ||
69b693f0 MKL |
3037 | static s32 btf_struct_check_meta(struct btf_verifier_env *env, |
3038 | const struct btf_type *t, | |
3039 | u32 meta_left) | |
3040 | { | |
3041 | bool is_union = BTF_INFO_KIND(t->info) == BTF_KIND_UNION; | |
3042 | const struct btf_member *member; | |
6283fa38 | 3043 | u32 meta_needed, last_offset; |
69b693f0 MKL |
3044 | struct btf *btf = env->btf; |
3045 | u32 struct_size = t->size; | |
9d5f9f70 | 3046 | u32 offset; |
69b693f0 MKL |
3047 | u16 i; |
3048 | ||
3049 | meta_needed = btf_type_vlen(t) * sizeof(*member); | |
3050 | if (meta_left < meta_needed) { | |
3051 | btf_verifier_log_basic(env, t, | |
3052 | "meta_left:%u meta_needed:%u", | |
3053 | meta_left, meta_needed); | |
3054 | return -EINVAL; | |
3055 | } | |
3056 | ||
eb04bbb6 YS |
3057 | /* struct type either no name or a valid one */ |
3058 | if (t->name_off && | |
3059 | !btf_name_valid_identifier(env->btf, t->name_off)) { | |
3060 | btf_verifier_log_type(env, t, "Invalid name"); | |
3061 | return -EINVAL; | |
3062 | } | |
3063 | ||
69b693f0 MKL |
3064 | btf_verifier_log_type(env, t, NULL); |
3065 | ||
6283fa38 | 3066 | last_offset = 0; |
69b693f0 | 3067 | for_each_member(i, t, member) { |
fbcf93eb | 3068 | if (!btf_name_offset_valid(btf, member->name_off)) { |
69b693f0 MKL |
3069 | btf_verifier_log_member(env, t, member, |
3070 | "Invalid member name_offset:%u", | |
fbcf93eb | 3071 | member->name_off); |
69b693f0 MKL |
3072 | return -EINVAL; |
3073 | } | |
3074 | ||
eb04bbb6 YS |
3075 | /* struct member either no name or a valid one */ |
3076 | if (member->name_off && | |
3077 | !btf_name_valid_identifier(btf, member->name_off)) { | |
3078 | btf_verifier_log_member(env, t, member, "Invalid name"); | |
3079 | return -EINVAL; | |
3080 | } | |
69b693f0 | 3081 | /* A member cannot be in type void */ |
aea2f7b8 | 3082 | if (!member->type || !BTF_TYPE_ID_VALID(member->type)) { |
69b693f0 MKL |
3083 | btf_verifier_log_member(env, t, member, |
3084 | "Invalid type_id"); | |
3085 | return -EINVAL; | |
3086 | } | |
3087 | ||
8293eb99 | 3088 | offset = __btf_member_bit_offset(t, member); |
9d5f9f70 | 3089 | if (is_union && offset) { |
69b693f0 MKL |
3090 | btf_verifier_log_member(env, t, member, |
3091 | "Invalid member bits_offset"); | |
3092 | return -EINVAL; | |
3093 | } | |
3094 | ||
6283fa38 MKL |
3095 | /* |
3096 | * ">" instead of ">=" because the last member could be | |
3097 | * "char a[0];" | |
3098 | */ | |
9d5f9f70 | 3099 | if (last_offset > offset) { |
6283fa38 MKL |
3100 | btf_verifier_log_member(env, t, member, |
3101 | "Invalid member bits_offset"); | |
3102 | return -EINVAL; | |
3103 | } | |
3104 | ||
9d5f9f70 | 3105 | if (BITS_ROUNDUP_BYTES(offset) > struct_size) { |
69b693f0 | 3106 | btf_verifier_log_member(env, t, member, |
311fe1a8 | 3107 | "Member bits_offset exceeds its struct size"); |
69b693f0 MKL |
3108 | return -EINVAL; |
3109 | } | |
3110 | ||
3111 | btf_verifier_log_member(env, t, member, NULL); | |
9d5f9f70 | 3112 | last_offset = offset; |
69b693f0 MKL |
3113 | } |
3114 | ||
3115 | return meta_needed; | |
3116 | } | |
3117 | ||
eb3f595d MKL |
3118 | static int btf_struct_resolve(struct btf_verifier_env *env, |
3119 | const struct resolve_vertex *v) | |
3120 | { | |
3121 | const struct btf_member *member; | |
179cde8c | 3122 | int err; |
eb3f595d MKL |
3123 | u16 i; |
3124 | ||
3125 | /* Before continue resolving the next_member, | |
3126 | * ensure the last member is indeed resolved to a | |
3127 | * type with size info. | |
3128 | */ | |
3129 | if (v->next_member) { | |
179cde8c | 3130 | const struct btf_type *last_member_type; |
eb3f595d MKL |
3131 | const struct btf_member *last_member; |
3132 | u16 last_member_type_id; | |
3133 | ||
3134 | last_member = btf_type_member(v->t) + v->next_member - 1; | |
3135 | last_member_type_id = last_member->type; | |
3136 | if (WARN_ON_ONCE(!env_type_is_resolved(env, | |
3137 | last_member_type_id))) | |
3138 | return -EINVAL; | |
179cde8c MKL |
3139 | |
3140 | last_member_type = btf_type_by_id(env->btf, | |
3141 | last_member_type_id); | |
9d5f9f70 YS |
3142 | if (btf_type_kflag(v->t)) |
3143 | err = btf_type_ops(last_member_type)->check_kflag_member(env, v->t, | |
3144 | last_member, | |
3145 | last_member_type); | |
3146 | else | |
3147 | err = btf_type_ops(last_member_type)->check_member(env, v->t, | |
3148 | last_member, | |
3149 | last_member_type); | |
179cde8c MKL |
3150 | if (err) |
3151 | return err; | |
eb3f595d MKL |
3152 | } |
3153 | ||
3154 | for_each_member_from(i, v->next_member, v->t, member) { | |
3155 | u32 member_type_id = member->type; | |
3156 | const struct btf_type *member_type = btf_type_by_id(env->btf, | |
3157 | member_type_id); | |
3158 | ||
e4f07120 SF |
3159 | if (btf_type_nosize_or_null(member_type) || |
3160 | btf_type_is_resolve_source_only(member_type)) { | |
eb3f595d MKL |
3161 | btf_verifier_log_member(env, v->t, member, |
3162 | "Invalid member"); | |
3163 | return -EINVAL; | |
3164 | } | |
3165 | ||
3166 | if (!env_type_is_resolve_sink(env, member_type) && | |
3167 | !env_type_is_resolved(env, member_type_id)) { | |
3168 | env_stack_set_next_member(env, i + 1); | |
3169 | return env_stack_push(env, member_type, member_type_id); | |
3170 | } | |
179cde8c | 3171 | |
9d5f9f70 YS |
3172 | if (btf_type_kflag(v->t)) |
3173 | err = btf_type_ops(member_type)->check_kflag_member(env, v->t, | |
3174 | member, | |
3175 | member_type); | |
3176 | else | |
3177 | err = btf_type_ops(member_type)->check_member(env, v->t, | |
3178 | member, | |
3179 | member_type); | |
179cde8c MKL |
3180 | if (err) |
3181 | return err; | |
eb3f595d MKL |
3182 | } |
3183 | ||
3184 | env_stack_pop_resolved(env, 0, 0); | |
3185 | ||
3186 | return 0; | |
3187 | } | |
3188 | ||
69b693f0 MKL |
3189 | static void btf_struct_log(struct btf_verifier_env *env, |
3190 | const struct btf_type *t) | |
3191 | { | |
3192 | btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t)); | |
3193 | } | |
3194 | ||
42ba1308 KKD |
3195 | enum btf_field_type { |
3196 | BTF_FIELD_SPIN_LOCK, | |
3197 | BTF_FIELD_TIMER, | |
61df10c7 KKD |
3198 | BTF_FIELD_KPTR, |
3199 | }; | |
3200 | ||
3201 | enum { | |
3202 | BTF_FIELD_IGNORE = 0, | |
3203 | BTF_FIELD_FOUND = 1, | |
42ba1308 KKD |
3204 | }; |
3205 | ||
3206 | struct btf_field_info { | |
61df10c7 | 3207 | u32 type_id; |
42ba1308 | 3208 | u32 off; |
c0a5a21c | 3209 | enum bpf_kptr_type type; |
42ba1308 KKD |
3210 | }; |
3211 | ||
3212 | static int btf_find_struct(const struct btf *btf, const struct btf_type *t, | |
3213 | u32 off, int sz, struct btf_field_info *info) | |
3214 | { | |
3215 | if (!__btf_type_is_struct(t)) | |
61df10c7 | 3216 | return BTF_FIELD_IGNORE; |
42ba1308 | 3217 | if (t->size != sz) |
61df10c7 | 3218 | return BTF_FIELD_IGNORE; |
42ba1308 | 3219 | info->off = off; |
61df10c7 KKD |
3220 | return BTF_FIELD_FOUND; |
3221 | } | |
3222 | ||
3223 | static int btf_find_kptr(const struct btf *btf, const struct btf_type *t, | |
3224 | u32 off, int sz, struct btf_field_info *info) | |
3225 | { | |
c0a5a21c | 3226 | enum bpf_kptr_type type; |
61df10c7 KKD |
3227 | u32 res_id; |
3228 | ||
3229 | /* For PTR, sz is always == 8 */ | |
3230 | if (!btf_type_is_ptr(t)) | |
3231 | return BTF_FIELD_IGNORE; | |
3232 | t = btf_type_by_id(btf, t->type); | |
3233 | ||
3234 | if (!btf_type_is_type_tag(t)) | |
3235 | return BTF_FIELD_IGNORE; | |
3236 | /* Reject extra tags */ | |
3237 | if (btf_type_is_type_tag(btf_type_by_id(btf, t->type))) | |
3238 | return -EINVAL; | |
c0a5a21c KKD |
3239 | if (!strcmp("kptr", __btf_name_by_offset(btf, t->name_off))) |
3240 | type = BPF_KPTR_UNREF; | |
3241 | else if (!strcmp("kptr_ref", __btf_name_by_offset(btf, t->name_off))) | |
3242 | type = BPF_KPTR_REF; | |
3243 | else | |
61df10c7 KKD |
3244 | return -EINVAL; |
3245 | ||
3246 | /* Get the base type */ | |
3247 | t = btf_type_skip_modifiers(btf, t->type, &res_id); | |
3248 | /* Only pointer to struct is allowed */ | |
3249 | if (!__btf_type_is_struct(t)) | |
3250 | return -EINVAL; | |
3251 | ||
3252 | info->type_id = res_id; | |
3253 | info->off = off; | |
c0a5a21c | 3254 | info->type = type; |
61df10c7 | 3255 | return BTF_FIELD_FOUND; |
42ba1308 KKD |
3256 | } |
3257 | ||
68134668 | 3258 | static int btf_find_struct_field(const struct btf *btf, const struct btf_type *t, |
42ba1308 KKD |
3259 | const char *name, int sz, int align, |
3260 | enum btf_field_type field_type, | |
61df10c7 | 3261 | struct btf_field_info *info, int info_cnt) |
d83525ca AS |
3262 | { |
3263 | const struct btf_member *member; | |
61df10c7 KKD |
3264 | struct btf_field_info tmp; |
3265 | int ret, idx = 0; | |
42ba1308 | 3266 | u32 i, off; |
d83525ca | 3267 | |
d83525ca AS |
3268 | for_each_member(i, t, member) { |
3269 | const struct btf_type *member_type = btf_type_by_id(btf, | |
3270 | member->type); | |
42ba1308 | 3271 | |
61df10c7 | 3272 | if (name && strcmp(__btf_name_by_offset(btf, member_type->name_off), name)) |
d83525ca | 3273 | continue; |
42ba1308 | 3274 | |
8293eb99 | 3275 | off = __btf_member_bit_offset(t, member); |
d83525ca AS |
3276 | if (off % 8) |
3277 | /* valid C code cannot generate such BTF */ | |
3278 | return -EINVAL; | |
3279 | off /= 8; | |
68134668 AS |
3280 | if (off % align) |
3281 | return -EINVAL; | |
42ba1308 KKD |
3282 | |
3283 | switch (field_type) { | |
3284 | case BTF_FIELD_SPIN_LOCK: | |
3285 | case BTF_FIELD_TIMER: | |
61df10c7 KKD |
3286 | ret = btf_find_struct(btf, member_type, off, sz, |
3287 | idx < info_cnt ? &info[idx] : &tmp); | |
3288 | if (ret < 0) | |
3289 | return ret; | |
3290 | break; | |
3291 | case BTF_FIELD_KPTR: | |
3292 | ret = btf_find_kptr(btf, member_type, off, sz, | |
3293 | idx < info_cnt ? &info[idx] : &tmp); | |
3294 | if (ret < 0) | |
3295 | return ret; | |
3296 | break; | |
42ba1308 KKD |
3297 | default: |
3298 | return -EFAULT; | |
3299 | } | |
61df10c7 KKD |
3300 | |
3301 | if (ret == BTF_FIELD_IGNORE) | |
3302 | continue; | |
3303 | if (idx >= info_cnt) | |
3304 | return -E2BIG; | |
3305 | ++idx; | |
68134668 | 3306 | } |
61df10c7 | 3307 | return idx; |
68134668 AS |
3308 | } |
3309 | ||
3310 | static int btf_find_datasec_var(const struct btf *btf, const struct btf_type *t, | |
42ba1308 KKD |
3311 | const char *name, int sz, int align, |
3312 | enum btf_field_type field_type, | |
61df10c7 | 3313 | struct btf_field_info *info, int info_cnt) |
68134668 AS |
3314 | { |
3315 | const struct btf_var_secinfo *vsi; | |
61df10c7 KKD |
3316 | struct btf_field_info tmp; |
3317 | int ret, idx = 0; | |
42ba1308 | 3318 | u32 i, off; |
68134668 AS |
3319 | |
3320 | for_each_vsi(i, t, vsi) { | |
3321 | const struct btf_type *var = btf_type_by_id(btf, vsi->type); | |
3322 | const struct btf_type *var_type = btf_type_by_id(btf, var->type); | |
3323 | ||
42ba1308 KKD |
3324 | off = vsi->offset; |
3325 | ||
61df10c7 | 3326 | if (name && strcmp(__btf_name_by_offset(btf, var_type->name_off), name)) |
68134668 AS |
3327 | continue; |
3328 | if (vsi->size != sz) | |
3329 | continue; | |
68134668 | 3330 | if (off % align) |
d83525ca | 3331 | return -EINVAL; |
42ba1308 KKD |
3332 | |
3333 | switch (field_type) { | |
3334 | case BTF_FIELD_SPIN_LOCK: | |
3335 | case BTF_FIELD_TIMER: | |
61df10c7 KKD |
3336 | ret = btf_find_struct(btf, var_type, off, sz, |
3337 | idx < info_cnt ? &info[idx] : &tmp); | |
3338 | if (ret < 0) | |
3339 | return ret; | |
3340 | break; | |
3341 | case BTF_FIELD_KPTR: | |
3342 | ret = btf_find_kptr(btf, var_type, off, sz, | |
3343 | idx < info_cnt ? &info[idx] : &tmp); | |
3344 | if (ret < 0) | |
3345 | return ret; | |
3346 | break; | |
42ba1308 KKD |
3347 | default: |
3348 | return -EFAULT; | |
3349 | } | |
61df10c7 KKD |
3350 | |
3351 | if (ret == BTF_FIELD_IGNORE) | |
3352 | continue; | |
3353 | if (idx >= info_cnt) | |
3354 | return -E2BIG; | |
3355 | ++idx; | |
d83525ca | 3356 | } |
61df10c7 | 3357 | return idx; |
d83525ca AS |
3358 | } |
3359 | ||
68134668 | 3360 | static int btf_find_field(const struct btf *btf, const struct btf_type *t, |
42ba1308 | 3361 | enum btf_field_type field_type, |
61df10c7 | 3362 | struct btf_field_info *info, int info_cnt) |
68134668 | 3363 | { |
42ba1308 KKD |
3364 | const char *name; |
3365 | int sz, align; | |
3366 | ||
3367 | switch (field_type) { | |
3368 | case BTF_FIELD_SPIN_LOCK: | |
3369 | name = "bpf_spin_lock"; | |
3370 | sz = sizeof(struct bpf_spin_lock); | |
3371 | align = __alignof__(struct bpf_spin_lock); | |
3372 | break; | |
3373 | case BTF_FIELD_TIMER: | |
3374 | name = "bpf_timer"; | |
3375 | sz = sizeof(struct bpf_timer); | |
3376 | align = __alignof__(struct bpf_timer); | |
3377 | break; | |
61df10c7 KKD |
3378 | case BTF_FIELD_KPTR: |
3379 | name = NULL; | |
3380 | sz = sizeof(u64); | |
3381 | align = 8; | |
3382 | break; | |
42ba1308 KKD |
3383 | default: |
3384 | return -EFAULT; | |
3385 | } | |
68134668 AS |
3386 | |
3387 | if (__btf_type_is_struct(t)) | |
61df10c7 | 3388 | return btf_find_struct_field(btf, t, name, sz, align, field_type, info, info_cnt); |
68134668 | 3389 | else if (btf_type_is_datasec(t)) |
61df10c7 | 3390 | return btf_find_datasec_var(btf, t, name, sz, align, field_type, info, info_cnt); |
68134668 AS |
3391 | return -EINVAL; |
3392 | } | |
3393 | ||
3394 | /* find 'struct bpf_spin_lock' in map value. | |
3395 | * return >= 0 offset if found | |
3396 | * and < 0 in case of error | |
3397 | */ | |
3398 | int btf_find_spin_lock(const struct btf *btf, const struct btf_type *t) | |
3399 | { | |
61df10c7 | 3400 | struct btf_field_info info; |
42ba1308 KKD |
3401 | int ret; |
3402 | ||
61df10c7 | 3403 | ret = btf_find_field(btf, t, BTF_FIELD_SPIN_LOCK, &info, 1); |
42ba1308 KKD |
3404 | if (ret < 0) |
3405 | return ret; | |
61df10c7 KKD |
3406 | if (!ret) |
3407 | return -ENOENT; | |
42ba1308 | 3408 | return info.off; |
68134668 AS |
3409 | } |
3410 | ||
3411 | int btf_find_timer(const struct btf *btf, const struct btf_type *t) | |
3412 | { | |
61df10c7 | 3413 | struct btf_field_info info; |
42ba1308 KKD |
3414 | int ret; |
3415 | ||
61df10c7 | 3416 | ret = btf_find_field(btf, t, BTF_FIELD_TIMER, &info, 1); |
42ba1308 KKD |
3417 | if (ret < 0) |
3418 | return ret; | |
61df10c7 KKD |
3419 | if (!ret) |
3420 | return -ENOENT; | |
42ba1308 | 3421 | return info.off; |
68134668 AS |
3422 | } |
3423 | ||
61df10c7 KKD |
3424 | struct bpf_map_value_off *btf_parse_kptrs(const struct btf *btf, |
3425 | const struct btf_type *t) | |
3426 | { | |
3427 | struct btf_field_info info_arr[BPF_MAP_VALUE_OFF_MAX]; | |
3428 | struct bpf_map_value_off *tab; | |
3429 | struct btf *kernel_btf = NULL; | |
14a324f6 | 3430 | struct module *mod = NULL; |
61df10c7 KKD |
3431 | int ret, i, nr_off; |
3432 | ||
3433 | ret = btf_find_field(btf, t, BTF_FIELD_KPTR, info_arr, ARRAY_SIZE(info_arr)); | |
3434 | if (ret < 0) | |
3435 | return ERR_PTR(ret); | |
3436 | if (!ret) | |
3437 | return NULL; | |
3438 | ||
3439 | nr_off = ret; | |
3440 | tab = kzalloc(offsetof(struct bpf_map_value_off, off[nr_off]), GFP_KERNEL | __GFP_NOWARN); | |
3441 | if (!tab) | |
3442 | return ERR_PTR(-ENOMEM); | |
3443 | ||
3444 | for (i = 0; i < nr_off; i++) { | |
3445 | const struct btf_type *t; | |
3446 | s32 id; | |
3447 | ||
3448 | /* Find type in map BTF, and use it to look up the matching type | |
3449 | * in vmlinux or module BTFs, by name and kind. | |
3450 | */ | |
3451 | t = btf_type_by_id(btf, info_arr[i].type_id); | |
3452 | id = bpf_find_btf_id(__btf_name_by_offset(btf, t->name_off), BTF_INFO_KIND(t->info), | |
3453 | &kernel_btf); | |
3454 | if (id < 0) { | |
3455 | ret = id; | |
3456 | goto end; | |
3457 | } | |
3458 | ||
14a324f6 KKD |
3459 | /* Find and stash the function pointer for the destruction function that |
3460 | * needs to be eventually invoked from the map free path. | |
3461 | */ | |
3462 | if (info_arr[i].type == BPF_KPTR_REF) { | |
3463 | const struct btf_type *dtor_func; | |
3464 | const char *dtor_func_name; | |
3465 | unsigned long addr; | |
3466 | s32 dtor_btf_id; | |
3467 | ||
3468 | /* This call also serves as a whitelist of allowed objects that | |
3469 | * can be used as a referenced pointer and be stored in a map at | |
3470 | * the same time. | |
3471 | */ | |
3472 | dtor_btf_id = btf_find_dtor_kfunc(kernel_btf, id); | |
3473 | if (dtor_btf_id < 0) { | |
3474 | ret = dtor_btf_id; | |
3475 | goto end_btf; | |
3476 | } | |
3477 | ||
3478 | dtor_func = btf_type_by_id(kernel_btf, dtor_btf_id); | |
3479 | if (!dtor_func) { | |
3480 | ret = -ENOENT; | |
3481 | goto end_btf; | |
3482 | } | |
3483 | ||
3484 | if (btf_is_module(kernel_btf)) { | |
3485 | mod = btf_try_get_module(kernel_btf); | |
3486 | if (!mod) { | |
3487 | ret = -ENXIO; | |
3488 | goto end_btf; | |
3489 | } | |
3490 | } | |
3491 | ||
3492 | /* We already verified dtor_func to be btf_type_is_func | |
3493 | * in register_btf_id_dtor_kfuncs. | |
3494 | */ | |
3495 | dtor_func_name = __btf_name_by_offset(kernel_btf, dtor_func->name_off); | |
3496 | addr = kallsyms_lookup_name(dtor_func_name); | |
3497 | if (!addr) { | |
3498 | ret = -EINVAL; | |
3499 | goto end_mod; | |
3500 | } | |
3501 | tab->off[i].kptr.dtor = (void *)addr; | |
3502 | } | |
3503 | ||
61df10c7 | 3504 | tab->off[i].offset = info_arr[i].off; |
c0a5a21c | 3505 | tab->off[i].type = info_arr[i].type; |
61df10c7 KKD |
3506 | tab->off[i].kptr.btf_id = id; |
3507 | tab->off[i].kptr.btf = kernel_btf; | |
14a324f6 | 3508 | tab->off[i].kptr.module = mod; |
61df10c7 KKD |
3509 | } |
3510 | tab->nr_off = nr_off; | |
3511 | return tab; | |
14a324f6 KKD |
3512 | end_mod: |
3513 | module_put(mod); | |
3514 | end_btf: | |
3515 | btf_put(kernel_btf); | |
61df10c7 | 3516 | end: |
14a324f6 | 3517 | while (i--) { |
61df10c7 | 3518 | btf_put(tab->off[i].kptr.btf); |
14a324f6 KKD |
3519 | if (tab->off[i].kptr.module) |
3520 | module_put(tab->off[i].kptr.module); | |
3521 | } | |
61df10c7 KKD |
3522 | kfree(tab); |
3523 | return ERR_PTR(ret); | |
3524 | } | |
3525 | ||
31d0bc81 AM |
3526 | static void __btf_struct_show(const struct btf *btf, const struct btf_type *t, |
3527 | u32 type_id, void *data, u8 bits_offset, | |
3528 | struct btf_show *show) | |
b00b8dae | 3529 | { |
b00b8dae | 3530 | const struct btf_member *member; |
31d0bc81 | 3531 | void *safe_data; |
b00b8dae MKL |
3532 | u32 i; |
3533 | ||
31d0bc81 AM |
3534 | safe_data = btf_show_start_struct_type(show, t, type_id, data); |
3535 | if (!safe_data) | |
3536 | return; | |
3537 | ||
b00b8dae MKL |
3538 | for_each_member(i, t, member) { |
3539 | const struct btf_type *member_type = btf_type_by_id(btf, | |
3540 | member->type); | |
b00b8dae | 3541 | const struct btf_kind_operations *ops; |
9d5f9f70 YS |
3542 | u32 member_offset, bitfield_size; |
3543 | u32 bytes_offset; | |
3544 | u8 bits8_offset; | |
b00b8dae | 3545 | |
31d0bc81 | 3546 | btf_show_start_member(show, member); |
b00b8dae | 3547 | |
8293eb99 AS |
3548 | member_offset = __btf_member_bit_offset(t, member); |
3549 | bitfield_size = __btf_member_bitfield_size(t, member); | |
17e3ac81 YS |
3550 | bytes_offset = BITS_ROUNDDOWN_BYTES(member_offset); |
3551 | bits8_offset = BITS_PER_BYTE_MASKED(member_offset); | |
9d5f9f70 | 3552 | if (bitfield_size) { |
31d0bc81 AM |
3553 | safe_data = btf_show_start_type(show, member_type, |
3554 | member->type, | |
3555 | data + bytes_offset); | |
3556 | if (safe_data) | |
3557 | btf_bitfield_show(safe_data, | |
3558 | bits8_offset, | |
3559 | bitfield_size, show); | |
3560 | btf_show_end_type(show); | |
9d5f9f70 | 3561 | } else { |
9d5f9f70 | 3562 | ops = btf_type_ops(member_type); |
31d0bc81 AM |
3563 | ops->show(btf, member_type, member->type, |
3564 | data + bytes_offset, bits8_offset, show); | |
9d5f9f70 | 3565 | } |
31d0bc81 AM |
3566 | |
3567 | btf_show_end_member(show); | |
b00b8dae | 3568 | } |
31d0bc81 AM |
3569 | |
3570 | btf_show_end_struct_type(show); | |
3571 | } | |
3572 | ||
3573 | static void btf_struct_show(const struct btf *btf, const struct btf_type *t, | |
3574 | u32 type_id, void *data, u8 bits_offset, | |
3575 | struct btf_show *show) | |
3576 | { | |
3577 | const struct btf_member *m = show->state.member; | |
3578 | ||
3579 | /* | |
3580 | * First check if any members would be shown (are non-zero). | |
3581 | * See comments above "struct btf_show" definition for more | |
3582 | * details on how this works at a high-level. | |
3583 | */ | |
3584 | if (show->state.depth > 0 && !(show->flags & BTF_SHOW_ZERO)) { | |
3585 | if (!show->state.depth_check) { | |
3586 | show->state.depth_check = show->state.depth + 1; | |
3587 | show->state.depth_to_show = 0; | |
3588 | } | |
3589 | __btf_struct_show(btf, t, type_id, data, bits_offset, show); | |
3590 | /* Restore saved member data here */ | |
3591 | show->state.member = m; | |
3592 | if (show->state.depth_check != show->state.depth + 1) | |
3593 | return; | |
3594 | show->state.depth_check = 0; | |
3595 | ||
3596 | if (show->state.depth_to_show <= show->state.depth) | |
3597 | return; | |
3598 | /* | |
3599 | * Reaching here indicates we have recursed and found | |
3600 | * non-zero child values. | |
3601 | */ | |
3602 | } | |
3603 | ||
3604 | __btf_struct_show(btf, t, type_id, data, bits_offset, show); | |
b00b8dae MKL |
3605 | } |
3606 | ||
69b693f0 MKL |
3607 | static struct btf_kind_operations struct_ops = { |
3608 | .check_meta = btf_struct_check_meta, | |
eb3f595d | 3609 | .resolve = btf_struct_resolve, |
179cde8c | 3610 | .check_member = btf_struct_check_member, |
9d5f9f70 | 3611 | .check_kflag_member = btf_generic_check_kflag_member, |
69b693f0 | 3612 | .log_details = btf_struct_log, |
31d0bc81 | 3613 | .show = btf_struct_show, |
69b693f0 MKL |
3614 | }; |
3615 | ||
179cde8c MKL |
3616 | static int btf_enum_check_member(struct btf_verifier_env *env, |
3617 | const struct btf_type *struct_type, | |
3618 | const struct btf_member *member, | |
3619 | const struct btf_type *member_type) | |
3620 | { | |
3621 | u32 struct_bits_off = member->offset; | |
3622 | u32 struct_size, bytes_offset; | |
3623 | ||
3624 | if (BITS_PER_BYTE_MASKED(struct_bits_off)) { | |
3625 | btf_verifier_log_member(env, struct_type, member, | |
3626 | "Member is not byte aligned"); | |
3627 | return -EINVAL; | |
3628 | } | |
3629 | ||
3630 | struct_size = struct_type->size; | |
3631 | bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off); | |
da6c7fae | 3632 | if (struct_size - bytes_offset < member_type->size) { |
179cde8c MKL |
3633 | btf_verifier_log_member(env, struct_type, member, |
3634 | "Member exceeds struct_size"); | |
3635 | return -EINVAL; | |
3636 | } | |
3637 | ||
3638 | return 0; | |
3639 | } | |
3640 | ||
9d5f9f70 YS |
3641 | static int btf_enum_check_kflag_member(struct btf_verifier_env *env, |
3642 | const struct btf_type *struct_type, | |
3643 | const struct btf_member *member, | |
3644 | const struct btf_type *member_type) | |
3645 | { | |
3646 | u32 struct_bits_off, nr_bits, bytes_end, struct_size; | |
3647 | u32 int_bitsize = sizeof(int) * BITS_PER_BYTE; | |
3648 | ||
3649 | struct_bits_off = BTF_MEMBER_BIT_OFFSET(member->offset); | |
3650 | nr_bits = BTF_MEMBER_BITFIELD_SIZE(member->offset); | |
3651 | if (!nr_bits) { | |
3652 | if (BITS_PER_BYTE_MASKED(struct_bits_off)) { | |
3653 | btf_verifier_log_member(env, struct_type, member, | |
3654 | "Member is not byte aligned"); | |
e3439af4 | 3655 | return -EINVAL; |
9d5f9f70 YS |
3656 | } |
3657 | ||
3658 | nr_bits = int_bitsize; | |
3659 | } else if (nr_bits > int_bitsize) { | |
3660 | btf_verifier_log_member(env, struct_type, member, | |
3661 | "Invalid member bitfield_size"); | |
3662 | return -EINVAL; | |
3663 | } | |
3664 | ||
3665 | struct_size = struct_type->size; | |
3666 | bytes_end = BITS_ROUNDUP_BYTES(struct_bits_off + nr_bits); | |
3667 | if (struct_size < bytes_end) { | |
3668 | btf_verifier_log_member(env, struct_type, member, | |
3669 | "Member exceeds struct_size"); | |
3670 | return -EINVAL; | |
3671 | } | |
3672 | ||
3673 | return 0; | |
3674 | } | |
3675 | ||
69b693f0 MKL |
3676 | static s32 btf_enum_check_meta(struct btf_verifier_env *env, |
3677 | const struct btf_type *t, | |
3678 | u32 meta_left) | |
3679 | { | |
3680 | const struct btf_enum *enums = btf_type_enum(t); | |
3681 | struct btf *btf = env->btf; | |
6089fb32 | 3682 | const char *fmt_str; |
69b693f0 MKL |
3683 | u16 i, nr_enums; |
3684 | u32 meta_needed; | |
3685 | ||
3686 | nr_enums = btf_type_vlen(t); | |
3687 | meta_needed = nr_enums * sizeof(*enums); | |
3688 | ||
3689 | if (meta_left < meta_needed) { | |
3690 | btf_verifier_log_basic(env, t, | |
3691 | "meta_left:%u meta_needed:%u", | |
3692 | meta_left, meta_needed); | |
3693 | return -EINVAL; | |
3694 | } | |
3695 | ||
9eea9849 AS |
3696 | if (t->size > 8 || !is_power_of_2(t->size)) { |
3697 | btf_verifier_log_type(env, t, "Unexpected size"); | |
69b693f0 MKL |
3698 | return -EINVAL; |
3699 | } | |
3700 | ||
eb04bbb6 YS |
3701 | /* enum type either no name or a valid one */ |
3702 | if (t->name_off && | |
3703 | !btf_name_valid_identifier(env->btf, t->name_off)) { | |
3704 | btf_verifier_log_type(env, t, "Invalid name"); | |
3705 | return -EINVAL; | |
3706 | } | |
3707 | ||
69b693f0 MKL |
3708 | btf_verifier_log_type(env, t, NULL); |
3709 | ||
3710 | for (i = 0; i < nr_enums; i++) { | |
fbcf93eb | 3711 | if (!btf_name_offset_valid(btf, enums[i].name_off)) { |
69b693f0 | 3712 | btf_verifier_log(env, "\tInvalid name_offset:%u", |
fbcf93eb | 3713 | enums[i].name_off); |
69b693f0 MKL |
3714 | return -EINVAL; |
3715 | } | |
3716 | ||
eb04bbb6 YS |
3717 | /* enum member must have a valid name */ |
3718 | if (!enums[i].name_off || | |
3719 | !btf_name_valid_identifier(btf, enums[i].name_off)) { | |
3720 | btf_verifier_log_type(env, t, "Invalid name"); | |
3721 | return -EINVAL; | |
3722 | } | |
3723 | ||
8580ac94 AS |
3724 | if (env->log.level == BPF_LOG_KERNEL) |
3725 | continue; | |
6089fb32 YS |
3726 | fmt_str = btf_type_kflag(t) ? "\t%s val=%d\n" : "\t%s val=%u\n"; |
3727 | btf_verifier_log(env, fmt_str, | |
23127b33 | 3728 | __btf_name_by_offset(btf, enums[i].name_off), |
69b693f0 MKL |
3729 | enums[i].val); |
3730 | } | |
3731 | ||
3732 | return meta_needed; | |
3733 | } | |
3734 | ||
3735 | static void btf_enum_log(struct btf_verifier_env *env, | |
3736 | const struct btf_type *t) | |
3737 | { | |
3738 | btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t)); | |
3739 | } | |
3740 | ||
31d0bc81 AM |
3741 | static void btf_enum_show(const struct btf *btf, const struct btf_type *t, |
3742 | u32 type_id, void *data, u8 bits_offset, | |
3743 | struct btf_show *show) | |
b00b8dae MKL |
3744 | { |
3745 | const struct btf_enum *enums = btf_type_enum(t); | |
3746 | u32 i, nr_enums = btf_type_vlen(t); | |
31d0bc81 AM |
3747 | void *safe_data; |
3748 | int v; | |
3749 | ||
3750 | safe_data = btf_show_start_type(show, t, type_id, data); | |
3751 | if (!safe_data) | |
3752 | return; | |
3753 | ||
3754 | v = *(int *)safe_data; | |
b00b8dae MKL |
3755 | |
3756 | for (i = 0; i < nr_enums; i++) { | |
31d0bc81 AM |
3757 | if (v != enums[i].val) |
3758 | continue; | |
3759 | ||
3760 | btf_show_type_value(show, "%s", | |
3761 | __btf_name_by_offset(btf, | |
3762 | enums[i].name_off)); | |
3763 | ||
3764 | btf_show_end_type(show); | |
3765 | return; | |
b00b8dae MKL |
3766 | } |
3767 | ||
6089fb32 YS |
3768 | if (btf_type_kflag(t)) |
3769 | btf_show_type_value(show, "%d", v); | |
3770 | else | |
3771 | btf_show_type_value(show, "%u", v); | |
31d0bc81 | 3772 | btf_show_end_type(show); |
b00b8dae MKL |
3773 | } |
3774 | ||
69b693f0 MKL |
3775 | static struct btf_kind_operations enum_ops = { |
3776 | .check_meta = btf_enum_check_meta, | |
eb3f595d | 3777 | .resolve = btf_df_resolve, |
179cde8c | 3778 | .check_member = btf_enum_check_member, |
9d5f9f70 | 3779 | .check_kflag_member = btf_enum_check_kflag_member, |
69b693f0 | 3780 | .log_details = btf_enum_log, |
31d0bc81 | 3781 | .show = btf_enum_show, |
69b693f0 MKL |
3782 | }; |
3783 | ||
6089fb32 YS |
3784 | static s32 btf_enum64_check_meta(struct btf_verifier_env *env, |
3785 | const struct btf_type *t, | |
3786 | u32 meta_left) | |
3787 | { | |
3788 | const struct btf_enum64 *enums = btf_type_enum64(t); | |
3789 | struct btf *btf = env->btf; | |
3790 | const char *fmt_str; | |
3791 | u16 i, nr_enums; | |
3792 | u32 meta_needed; | |
3793 | ||
3794 | nr_enums = btf_type_vlen(t); | |
3795 | meta_needed = nr_enums * sizeof(*enums); | |
3796 | ||
3797 | if (meta_left < meta_needed) { | |
3798 | btf_verifier_log_basic(env, t, | |
3799 | "meta_left:%u meta_needed:%u", | |
3800 | meta_left, meta_needed); | |
3801 | return -EINVAL; | |
3802 | } | |
3803 | ||
3804 | if (t->size > 8 || !is_power_of_2(t->size)) { | |
3805 | btf_verifier_log_type(env, t, "Unexpected size"); | |
3806 | return -EINVAL; | |
3807 | } | |
3808 | ||
3809 | /* enum type either no name or a valid one */ | |
3810 | if (t->name_off && | |
3811 | !btf_name_valid_identifier(env->btf, t->name_off)) { | |
3812 | btf_verifier_log_type(env, t, "Invalid name"); | |
3813 | return -EINVAL; | |
3814 | } | |
3815 | ||
3816 | btf_verifier_log_type(env, t, NULL); | |
3817 | ||
3818 | for (i = 0; i < nr_enums; i++) { | |
3819 | if (!btf_name_offset_valid(btf, enums[i].name_off)) { | |
3820 | btf_verifier_log(env, "\tInvalid name_offset:%u", | |
3821 | enums[i].name_off); | |
3822 | return -EINVAL; | |
3823 | } | |
3824 | ||
3825 | /* enum member must have a valid name */ | |
3826 | if (!enums[i].name_off || | |
3827 | !btf_name_valid_identifier(btf, enums[i].name_off)) { | |
3828 | btf_verifier_log_type(env, t, "Invalid name"); | |
3829 | return -EINVAL; | |
3830 | } | |
3831 | ||
3832 | if (env->log.level == BPF_LOG_KERNEL) | |
3833 | continue; | |
3834 | ||
3835 | fmt_str = btf_type_kflag(t) ? "\t%s val=%lld\n" : "\t%s val=%llu\n"; | |
3836 | btf_verifier_log(env, fmt_str, | |
3837 | __btf_name_by_offset(btf, enums[i].name_off), | |
3838 | btf_enum64_value(enums + i)); | |
3839 | } | |
3840 | ||
3841 | return meta_needed; | |
3842 | } | |
3843 | ||
3844 | static void btf_enum64_show(const struct btf *btf, const struct btf_type *t, | |
3845 | u32 type_id, void *data, u8 bits_offset, | |
3846 | struct btf_show *show) | |
3847 | { | |
3848 | const struct btf_enum64 *enums = btf_type_enum64(t); | |
3849 | u32 i, nr_enums = btf_type_vlen(t); | |
3850 | void *safe_data; | |
3851 | s64 v; | |
3852 | ||
3853 | safe_data = btf_show_start_type(show, t, type_id, data); | |
3854 | if (!safe_data) | |
3855 | return; | |
3856 | ||
3857 | v = *(u64 *)safe_data; | |
3858 | ||
3859 | for (i = 0; i < nr_enums; i++) { | |
3860 | if (v != btf_enum64_value(enums + i)) | |
3861 | continue; | |
3862 | ||
3863 | btf_show_type_value(show, "%s", | |
3864 | __btf_name_by_offset(btf, | |
3865 | enums[i].name_off)); | |
3866 | ||
3867 | btf_show_end_type(show); | |
3868 | return; | |
3869 | } | |
3870 | ||
3871 | if (btf_type_kflag(t)) | |
3872 | btf_show_type_value(show, "%lld", v); | |
3873 | else | |
3874 | btf_show_type_value(show, "%llu", v); | |
3875 | btf_show_end_type(show); | |
3876 | } | |
3877 | ||
3878 | static struct btf_kind_operations enum64_ops = { | |
3879 | .check_meta = btf_enum64_check_meta, | |
3880 | .resolve = btf_df_resolve, | |
3881 | .check_member = btf_enum_check_member, | |
3882 | .check_kflag_member = btf_enum_check_kflag_member, | |
3883 | .log_details = btf_enum_log, | |
3884 | .show = btf_enum64_show, | |
3885 | }; | |
3886 | ||
2667a262 MKL |
3887 | static s32 btf_func_proto_check_meta(struct btf_verifier_env *env, |
3888 | const struct btf_type *t, | |
3889 | u32 meta_left) | |
3890 | { | |
3891 | u32 meta_needed = btf_type_vlen(t) * sizeof(struct btf_param); | |
3892 | ||
3893 | if (meta_left < meta_needed) { | |
3894 | btf_verifier_log_basic(env, t, | |
3895 | "meta_left:%u meta_needed:%u", | |
3896 | meta_left, meta_needed); | |
3897 | return -EINVAL; | |
3898 | } | |
3899 | ||
3900 | if (t->name_off) { | |
3901 | btf_verifier_log_type(env, t, "Invalid name"); | |
3902 | return -EINVAL; | |
3903 | } | |
3904 | ||
9d5f9f70 YS |
3905 | if (btf_type_kflag(t)) { |
3906 | btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); | |
3907 | return -EINVAL; | |
3908 | } | |
3909 | ||
2667a262 MKL |
3910 | btf_verifier_log_type(env, t, NULL); |
3911 | ||
3912 | return meta_needed; | |
3913 | } | |
3914 | ||
3915 | static void btf_func_proto_log(struct btf_verifier_env *env, | |
3916 | const struct btf_type *t) | |
3917 | { | |
3918 | const struct btf_param *args = (const struct btf_param *)(t + 1); | |
3919 | u16 nr_args = btf_type_vlen(t), i; | |
3920 | ||
3921 | btf_verifier_log(env, "return=%u args=(", t->type); | |
3922 | if (!nr_args) { | |
3923 | btf_verifier_log(env, "void"); | |
3924 | goto done; | |
3925 | } | |
3926 | ||
3927 | if (nr_args == 1 && !args[0].type) { | |
3928 | /* Only one vararg */ | |
3929 | btf_verifier_log(env, "vararg"); | |
3930 | goto done; | |
3931 | } | |
3932 | ||
3933 | btf_verifier_log(env, "%u %s", args[0].type, | |
23127b33 MKL |
3934 | __btf_name_by_offset(env->btf, |
3935 | args[0].name_off)); | |
2667a262 MKL |
3936 | for (i = 1; i < nr_args - 1; i++) |
3937 | btf_verifier_log(env, ", %u %s", args[i].type, | |
23127b33 MKL |
3938 | __btf_name_by_offset(env->btf, |
3939 | args[i].name_off)); | |
2667a262 MKL |
3940 | |
3941 | if (nr_args > 1) { | |
3942 | const struct btf_param *last_arg = &args[nr_args - 1]; | |
3943 | ||
3944 | if (last_arg->type) | |
3945 | btf_verifier_log(env, ", %u %s", last_arg->type, | |
23127b33 MKL |
3946 | __btf_name_by_offset(env->btf, |
3947 | last_arg->name_off)); | |
2667a262 MKL |
3948 | else |
3949 | btf_verifier_log(env, ", vararg"); | |
3950 | } | |
3951 | ||
3952 | done: | |
3953 | btf_verifier_log(env, ")"); | |
3954 | } | |
3955 | ||
3956 | static struct btf_kind_operations func_proto_ops = { | |
3957 | .check_meta = btf_func_proto_check_meta, | |
3958 | .resolve = btf_df_resolve, | |
3959 | /* | |
3960 | * BTF_KIND_FUNC_PROTO cannot be directly referred by | |
3961 | * a struct's member. | |
3962 | * | |
8fb33b60 | 3963 | * It should be a function pointer instead. |
2667a262 MKL |
3964 | * (i.e. struct's member -> BTF_KIND_PTR -> BTF_KIND_FUNC_PROTO) |
3965 | * | |
3966 | * Hence, there is no btf_func_check_member(). | |
3967 | */ | |
3968 | .check_member = btf_df_check_member, | |
9d5f9f70 | 3969 | .check_kflag_member = btf_df_check_kflag_member, |
2667a262 | 3970 | .log_details = btf_func_proto_log, |
31d0bc81 | 3971 | .show = btf_df_show, |
2667a262 MKL |
3972 | }; |
3973 | ||
3974 | static s32 btf_func_check_meta(struct btf_verifier_env *env, | |
3975 | const struct btf_type *t, | |
3976 | u32 meta_left) | |
3977 | { | |
3978 | if (!t->name_off || | |
3979 | !btf_name_valid_identifier(env->btf, t->name_off)) { | |
3980 | btf_verifier_log_type(env, t, "Invalid name"); | |
3981 | return -EINVAL; | |
3982 | } | |
3983 | ||
51c39bb1 AS |
3984 | if (btf_type_vlen(t) > BTF_FUNC_GLOBAL) { |
3985 | btf_verifier_log_type(env, t, "Invalid func linkage"); | |
2667a262 MKL |
3986 | return -EINVAL; |
3987 | } | |
3988 | ||
9d5f9f70 YS |
3989 | if (btf_type_kflag(t)) { |
3990 | btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); | |
3991 | return -EINVAL; | |
3992 | } | |
3993 | ||
2667a262 MKL |
3994 | btf_verifier_log_type(env, t, NULL); |
3995 | ||
3996 | return 0; | |
3997 | } | |
3998 | ||
d7e7b42f YS |
3999 | static int btf_func_resolve(struct btf_verifier_env *env, |
4000 | const struct resolve_vertex *v) | |
4001 | { | |
4002 | const struct btf_type *t = v->t; | |
4003 | u32 next_type_id = t->type; | |
4004 | int err; | |
4005 | ||
4006 | err = btf_func_check(env, t); | |
4007 | if (err) | |
4008 | return err; | |
4009 | ||
4010 | env_stack_pop_resolved(env, next_type_id, 0); | |
4011 | return 0; | |
4012 | } | |
4013 | ||
2667a262 MKL |
4014 | static struct btf_kind_operations func_ops = { |
4015 | .check_meta = btf_func_check_meta, | |
d7e7b42f | 4016 | .resolve = btf_func_resolve, |
2667a262 | 4017 | .check_member = btf_df_check_member, |
9d5f9f70 | 4018 | .check_kflag_member = btf_df_check_kflag_member, |
2667a262 | 4019 | .log_details = btf_ref_type_log, |
31d0bc81 | 4020 | .show = btf_df_show, |
2667a262 MKL |
4021 | }; |
4022 | ||
1dc92851 DB |
4023 | static s32 btf_var_check_meta(struct btf_verifier_env *env, |
4024 | const struct btf_type *t, | |
4025 | u32 meta_left) | |
4026 | { | |
4027 | const struct btf_var *var; | |
4028 | u32 meta_needed = sizeof(*var); | |
4029 | ||
4030 | if (meta_left < meta_needed) { | |
4031 | btf_verifier_log_basic(env, t, | |
4032 | "meta_left:%u meta_needed:%u", | |
4033 | meta_left, meta_needed); | |
4034 | return -EINVAL; | |
4035 | } | |
4036 | ||
4037 | if (btf_type_vlen(t)) { | |
4038 | btf_verifier_log_type(env, t, "vlen != 0"); | |
4039 | return -EINVAL; | |
4040 | } | |
4041 | ||
4042 | if (btf_type_kflag(t)) { | |
4043 | btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); | |
4044 | return -EINVAL; | |
4045 | } | |
4046 | ||
4047 | if (!t->name_off || | |
4048 | !__btf_name_valid(env->btf, t->name_off, true)) { | |
4049 | btf_verifier_log_type(env, t, "Invalid name"); | |
4050 | return -EINVAL; | |
4051 | } | |
4052 | ||
4053 | /* A var cannot be in type void */ | |
4054 | if (!t->type || !BTF_TYPE_ID_VALID(t->type)) { | |
4055 | btf_verifier_log_type(env, t, "Invalid type_id"); | |
4056 | return -EINVAL; | |
4057 | } | |
4058 | ||
4059 | var = btf_type_var(t); | |
4060 | if (var->linkage != BTF_VAR_STATIC && | |
4061 | var->linkage != BTF_VAR_GLOBAL_ALLOCATED) { | |
4062 | btf_verifier_log_type(env, t, "Linkage not supported"); | |
4063 | return -EINVAL; | |
4064 | } | |
4065 | ||
4066 | btf_verifier_log_type(env, t, NULL); | |
4067 | ||
4068 | return meta_needed; | |
4069 | } | |
4070 | ||
4071 | static void btf_var_log(struct btf_verifier_env *env, const struct btf_type *t) | |
4072 | { | |
4073 | const struct btf_var *var = btf_type_var(t); | |
4074 | ||
4075 | btf_verifier_log(env, "type_id=%u linkage=%u", t->type, var->linkage); | |
4076 | } | |
4077 | ||
4078 | static const struct btf_kind_operations var_ops = { | |
4079 | .check_meta = btf_var_check_meta, | |
4080 | .resolve = btf_var_resolve, | |
4081 | .check_member = btf_df_check_member, | |
4082 | .check_kflag_member = btf_df_check_kflag_member, | |
4083 | .log_details = btf_var_log, | |
31d0bc81 | 4084 | .show = btf_var_show, |
1dc92851 DB |
4085 | }; |
4086 | ||
4087 | static s32 btf_datasec_check_meta(struct btf_verifier_env *env, | |
4088 | const struct btf_type *t, | |
4089 | u32 meta_left) | |
4090 | { | |
4091 | const struct btf_var_secinfo *vsi; | |
4092 | u64 last_vsi_end_off = 0, sum = 0; | |
4093 | u32 i, meta_needed; | |
4094 | ||
4095 | meta_needed = btf_type_vlen(t) * sizeof(*vsi); | |
4096 | if (meta_left < meta_needed) { | |
4097 | btf_verifier_log_basic(env, t, | |
4098 | "meta_left:%u meta_needed:%u", | |
4099 | meta_left, meta_needed); | |
4100 | return -EINVAL; | |
4101 | } | |
4102 | ||
1dc92851 DB |
4103 | if (!t->size) { |
4104 | btf_verifier_log_type(env, t, "size == 0"); | |
4105 | return -EINVAL; | |
4106 | } | |
4107 | ||
4108 | if (btf_type_kflag(t)) { | |
4109 | btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); | |
4110 | return -EINVAL; | |
4111 | } | |
4112 | ||
4113 | if (!t->name_off || | |
4114 | !btf_name_valid_section(env->btf, t->name_off)) { | |
4115 | btf_verifier_log_type(env, t, "Invalid name"); | |
4116 | return -EINVAL; | |
4117 | } | |
4118 | ||
4119 | btf_verifier_log_type(env, t, NULL); | |
4120 | ||
4121 | for_each_vsi(i, t, vsi) { | |
4122 | /* A var cannot be in type void */ | |
4123 | if (!vsi->type || !BTF_TYPE_ID_VALID(vsi->type)) { | |
4124 | btf_verifier_log_vsi(env, t, vsi, | |
4125 | "Invalid type_id"); | |
4126 | return -EINVAL; | |
4127 | } | |
4128 | ||
4129 | if (vsi->offset < last_vsi_end_off || vsi->offset >= t->size) { | |
4130 | btf_verifier_log_vsi(env, t, vsi, | |
4131 | "Invalid offset"); | |
4132 | return -EINVAL; | |
4133 | } | |
4134 | ||
4135 | if (!vsi->size || vsi->size > t->size) { | |
4136 | btf_verifier_log_vsi(env, t, vsi, | |
4137 | "Invalid size"); | |
4138 | return -EINVAL; | |
4139 | } | |
4140 | ||
4141 | last_vsi_end_off = vsi->offset + vsi->size; | |
4142 | if (last_vsi_end_off > t->size) { | |
4143 | btf_verifier_log_vsi(env, t, vsi, | |
4144 | "Invalid offset+size"); | |
4145 | return -EINVAL; | |
4146 | } | |
4147 | ||
4148 | btf_verifier_log_vsi(env, t, vsi, NULL); | |
4149 | sum += vsi->size; | |
4150 | } | |
4151 | ||
4152 | if (t->size < sum) { | |
4153 | btf_verifier_log_type(env, t, "Invalid btf_info size"); | |
4154 | return -EINVAL; | |
4155 | } | |
4156 | ||
4157 | return meta_needed; | |
4158 | } | |
4159 | ||
4160 | static int btf_datasec_resolve(struct btf_verifier_env *env, | |
4161 | const struct resolve_vertex *v) | |
4162 | { | |
4163 | const struct btf_var_secinfo *vsi; | |
4164 | struct btf *btf = env->btf; | |
4165 | u16 i; | |
4166 | ||
4167 | for_each_vsi_from(i, v->next_member, v->t, vsi) { | |
4168 | u32 var_type_id = vsi->type, type_id, type_size = 0; | |
4169 | const struct btf_type *var_type = btf_type_by_id(env->btf, | |
4170 | var_type_id); | |
4171 | if (!var_type || !btf_type_is_var(var_type)) { | |
4172 | btf_verifier_log_vsi(env, v->t, vsi, | |
4173 | "Not a VAR kind member"); | |
4174 | return -EINVAL; | |
4175 | } | |
4176 | ||
4177 | if (!env_type_is_resolve_sink(env, var_type) && | |
4178 | !env_type_is_resolved(env, var_type_id)) { | |
4179 | env_stack_set_next_member(env, i + 1); | |
4180 | return env_stack_push(env, var_type, var_type_id); | |
4181 | } | |
4182 | ||
4183 | type_id = var_type->type; | |
4184 | if (!btf_type_id_size(btf, &type_id, &type_size)) { | |
4185 | btf_verifier_log_vsi(env, v->t, vsi, "Invalid type"); | |
4186 | return -EINVAL; | |
4187 | } | |
4188 | ||
4189 | if (vsi->size < type_size) { | |
4190 | btf_verifier_log_vsi(env, v->t, vsi, "Invalid size"); | |
4191 | return -EINVAL; | |
4192 | } | |
4193 | } | |
4194 | ||
4195 | env_stack_pop_resolved(env, 0, 0); | |
4196 | return 0; | |
4197 | } | |
4198 | ||
4199 | static void btf_datasec_log(struct btf_verifier_env *env, | |
4200 | const struct btf_type *t) | |
4201 | { | |
4202 | btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t)); | |
4203 | } | |
4204 | ||
31d0bc81 AM |
4205 | static void btf_datasec_show(const struct btf *btf, |
4206 | const struct btf_type *t, u32 type_id, | |
4207 | void *data, u8 bits_offset, | |
4208 | struct btf_show *show) | |
1dc92851 DB |
4209 | { |
4210 | const struct btf_var_secinfo *vsi; | |
4211 | const struct btf_type *var; | |
4212 | u32 i; | |
4213 | ||
31d0bc81 AM |
4214 | if (!btf_show_start_type(show, t, type_id, data)) |
4215 | return; | |
4216 | ||
4217 | btf_show_type_value(show, "section (\"%s\") = {", | |
4218 | __btf_name_by_offset(btf, t->name_off)); | |
1dc92851 DB |
4219 | for_each_vsi(i, t, vsi) { |
4220 | var = btf_type_by_id(btf, vsi->type); | |
4221 | if (i) | |
31d0bc81 AM |
4222 | btf_show(show, ","); |
4223 | btf_type_ops(var)->show(btf, var, vsi->type, | |
4224 | data + vsi->offset, bits_offset, show); | |
1dc92851 | 4225 | } |
31d0bc81 | 4226 | btf_show_end_type(show); |
1dc92851 DB |
4227 | } |
4228 | ||
4229 | static const struct btf_kind_operations datasec_ops = { | |
4230 | .check_meta = btf_datasec_check_meta, | |
4231 | .resolve = btf_datasec_resolve, | |
4232 | .check_member = btf_df_check_member, | |
4233 | .check_kflag_member = btf_df_check_kflag_member, | |
4234 | .log_details = btf_datasec_log, | |
31d0bc81 | 4235 | .show = btf_datasec_show, |
1dc92851 DB |
4236 | }; |
4237 | ||
b1828f0b IL |
4238 | static s32 btf_float_check_meta(struct btf_verifier_env *env, |
4239 | const struct btf_type *t, | |
4240 | u32 meta_left) | |
4241 | { | |
4242 | if (btf_type_vlen(t)) { | |
4243 | btf_verifier_log_type(env, t, "vlen != 0"); | |
4244 | return -EINVAL; | |
4245 | } | |
4246 | ||
4247 | if (btf_type_kflag(t)) { | |
4248 | btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); | |
4249 | return -EINVAL; | |
4250 | } | |
4251 | ||
4252 | if (t->size != 2 && t->size != 4 && t->size != 8 && t->size != 12 && | |
4253 | t->size != 16) { | |
4254 | btf_verifier_log_type(env, t, "Invalid type_size"); | |
4255 | return -EINVAL; | |
4256 | } | |
4257 | ||
4258 | btf_verifier_log_type(env, t, NULL); | |
4259 | ||
4260 | return 0; | |
4261 | } | |
4262 | ||
4263 | static int btf_float_check_member(struct btf_verifier_env *env, | |
4264 | const struct btf_type *struct_type, | |
4265 | const struct btf_member *member, | |
4266 | const struct btf_type *member_type) | |
4267 | { | |
4268 | u64 start_offset_bytes; | |
4269 | u64 end_offset_bytes; | |
4270 | u64 misalign_bits; | |
4271 | u64 align_bytes; | |
4272 | u64 align_bits; | |
4273 | ||
4274 | /* Different architectures have different alignment requirements, so | |
4275 | * here we check only for the reasonable minimum. This way we ensure | |
4276 | * that types after CO-RE can pass the kernel BTF verifier. | |
4277 | */ | |
4278 | align_bytes = min_t(u64, sizeof(void *), member_type->size); | |
4279 | align_bits = align_bytes * BITS_PER_BYTE; | |
4280 | div64_u64_rem(member->offset, align_bits, &misalign_bits); | |
4281 | if (misalign_bits) { | |
4282 | btf_verifier_log_member(env, struct_type, member, | |
4283 | "Member is not properly aligned"); | |
4284 | return -EINVAL; | |
4285 | } | |
4286 | ||
4287 | start_offset_bytes = member->offset / BITS_PER_BYTE; | |
4288 | end_offset_bytes = start_offset_bytes + member_type->size; | |
4289 | if (end_offset_bytes > struct_type->size) { | |
4290 | btf_verifier_log_member(env, struct_type, member, | |
4291 | "Member exceeds struct_size"); | |
4292 | return -EINVAL; | |
4293 | } | |
4294 | ||
4295 | return 0; | |
4296 | } | |
4297 | ||
4298 | static void btf_float_log(struct btf_verifier_env *env, | |
4299 | const struct btf_type *t) | |
4300 | { | |
4301 | btf_verifier_log(env, "size=%u", t->size); | |
4302 | } | |
4303 | ||
4304 | static const struct btf_kind_operations float_ops = { | |
4305 | .check_meta = btf_float_check_meta, | |
4306 | .resolve = btf_df_resolve, | |
4307 | .check_member = btf_float_check_member, | |
4308 | .check_kflag_member = btf_generic_check_kflag_member, | |
4309 | .log_details = btf_float_log, | |
4310 | .show = btf_df_show, | |
4311 | }; | |
4312 | ||
223f903e | 4313 | static s32 btf_decl_tag_check_meta(struct btf_verifier_env *env, |
b5ea834d YS |
4314 | const struct btf_type *t, |
4315 | u32 meta_left) | |
4316 | { | |
223f903e | 4317 | const struct btf_decl_tag *tag; |
b5ea834d YS |
4318 | u32 meta_needed = sizeof(*tag); |
4319 | s32 component_idx; | |
4320 | const char *value; | |
4321 | ||
4322 | if (meta_left < meta_needed) { | |
4323 | btf_verifier_log_basic(env, t, | |
4324 | "meta_left:%u meta_needed:%u", | |
4325 | meta_left, meta_needed); | |
4326 | return -EINVAL; | |
4327 | } | |
4328 | ||
4329 | value = btf_name_by_offset(env->btf, t->name_off); | |
4330 | if (!value || !value[0]) { | |
4331 | btf_verifier_log_type(env, t, "Invalid value"); | |
4332 | return -EINVAL; | |
4333 | } | |
4334 | ||
4335 | if (btf_type_vlen(t)) { | |
4336 | btf_verifier_log_type(env, t, "vlen != 0"); | |
4337 | return -EINVAL; | |
4338 | } | |
4339 | ||
4340 | if (btf_type_kflag(t)) { | |
4341 | btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); | |
4342 | return -EINVAL; | |
4343 | } | |
4344 | ||
223f903e | 4345 | component_idx = btf_type_decl_tag(t)->component_idx; |
b5ea834d YS |
4346 | if (component_idx < -1) { |
4347 | btf_verifier_log_type(env, t, "Invalid component_idx"); | |
4348 | return -EINVAL; | |
4349 | } | |
4350 | ||
4351 | btf_verifier_log_type(env, t, NULL); | |
4352 | ||
4353 | return meta_needed; | |
4354 | } | |
4355 | ||
223f903e | 4356 | static int btf_decl_tag_resolve(struct btf_verifier_env *env, |
b5ea834d YS |
4357 | const struct resolve_vertex *v) |
4358 | { | |
4359 | const struct btf_type *next_type; | |
4360 | const struct btf_type *t = v->t; | |
4361 | u32 next_type_id = t->type; | |
4362 | struct btf *btf = env->btf; | |
4363 | s32 component_idx; | |
4364 | u32 vlen; | |
4365 | ||
4366 | next_type = btf_type_by_id(btf, next_type_id); | |
223f903e | 4367 | if (!next_type || !btf_type_is_decl_tag_target(next_type)) { |
b5ea834d YS |
4368 | btf_verifier_log_type(env, v->t, "Invalid type_id"); |
4369 | return -EINVAL; | |
4370 | } | |
4371 | ||
4372 | if (!env_type_is_resolve_sink(env, next_type) && | |
4373 | !env_type_is_resolved(env, next_type_id)) | |
4374 | return env_stack_push(env, next_type, next_type_id); | |
4375 | ||
223f903e | 4376 | component_idx = btf_type_decl_tag(t)->component_idx; |
b5ea834d | 4377 | if (component_idx != -1) { |
bd16dee6 | 4378 | if (btf_type_is_var(next_type) || btf_type_is_typedef(next_type)) { |
b5ea834d YS |
4379 | btf_verifier_log_type(env, v->t, "Invalid component_idx"); |
4380 | return -EINVAL; | |
4381 | } | |
4382 | ||
4383 | if (btf_type_is_struct(next_type)) { | |
4384 | vlen = btf_type_vlen(next_type); | |
4385 | } else { | |
4386 | /* next_type should be a function */ | |
4387 | next_type = btf_type_by_id(btf, next_type->type); | |
4388 | vlen = btf_type_vlen(next_type); | |
4389 | } | |
4390 | ||
4391 | if ((u32)component_idx >= vlen) { | |
4392 | btf_verifier_log_type(env, v->t, "Invalid component_idx"); | |
4393 | return -EINVAL; | |
4394 | } | |
4395 | } | |
4396 | ||
4397 | env_stack_pop_resolved(env, next_type_id, 0); | |
4398 | ||
4399 | return 0; | |
4400 | } | |
4401 | ||
223f903e | 4402 | static void btf_decl_tag_log(struct btf_verifier_env *env, const struct btf_type *t) |
b5ea834d YS |
4403 | { |
4404 | btf_verifier_log(env, "type=%u component_idx=%d", t->type, | |
223f903e | 4405 | btf_type_decl_tag(t)->component_idx); |
b5ea834d YS |
4406 | } |
4407 | ||
223f903e YS |
4408 | static const struct btf_kind_operations decl_tag_ops = { |
4409 | .check_meta = btf_decl_tag_check_meta, | |
4410 | .resolve = btf_decl_tag_resolve, | |
b5ea834d YS |
4411 | .check_member = btf_df_check_member, |
4412 | .check_kflag_member = btf_df_check_kflag_member, | |
223f903e | 4413 | .log_details = btf_decl_tag_log, |
b5ea834d YS |
4414 | .show = btf_df_show, |
4415 | }; | |
4416 | ||
2667a262 MKL |
4417 | static int btf_func_proto_check(struct btf_verifier_env *env, |
4418 | const struct btf_type *t) | |
4419 | { | |
4420 | const struct btf_type *ret_type; | |
4421 | const struct btf_param *args; | |
4422 | const struct btf *btf; | |
4423 | u16 nr_args, i; | |
4424 | int err; | |
4425 | ||
4426 | btf = env->btf; | |
4427 | args = (const struct btf_param *)(t + 1); | |
4428 | nr_args = btf_type_vlen(t); | |
4429 | ||
4430 | /* Check func return type which could be "void" (t->type == 0) */ | |
4431 | if (t->type) { | |
4432 | u32 ret_type_id = t->type; | |
4433 | ||
4434 | ret_type = btf_type_by_id(btf, ret_type_id); | |
4435 | if (!ret_type) { | |
4436 | btf_verifier_log_type(env, t, "Invalid return type"); | |
4437 | return -EINVAL; | |
4438 | } | |
4439 | ||
4440 | if (btf_type_needs_resolve(ret_type) && | |
4441 | !env_type_is_resolved(env, ret_type_id)) { | |
4442 | err = btf_resolve(env, ret_type, ret_type_id); | |
4443 | if (err) | |
4444 | return err; | |
4445 | } | |
4446 | ||
4447 | /* Ensure the return type is a type that has a size */ | |
4448 | if (!btf_type_id_size(btf, &ret_type_id, NULL)) { | |
4449 | btf_verifier_log_type(env, t, "Invalid return type"); | |
4450 | return -EINVAL; | |
4451 | } | |
4452 | } | |
4453 | ||
4454 | if (!nr_args) | |
4455 | return 0; | |
4456 | ||
4457 | /* Last func arg type_id could be 0 if it is a vararg */ | |
4458 | if (!args[nr_args - 1].type) { | |
4459 | if (args[nr_args - 1].name_off) { | |
4460 | btf_verifier_log_type(env, t, "Invalid arg#%u", | |
4461 | nr_args); | |
4462 | return -EINVAL; | |
4463 | } | |
4464 | nr_args--; | |
4465 | } | |
4466 | ||
4467 | err = 0; | |
4468 | for (i = 0; i < nr_args; i++) { | |
4469 | const struct btf_type *arg_type; | |
4470 | u32 arg_type_id; | |
4471 | ||
4472 | arg_type_id = args[i].type; | |
4473 | arg_type = btf_type_by_id(btf, arg_type_id); | |
4474 | if (!arg_type) { | |
4475 | btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1); | |
4476 | err = -EINVAL; | |
4477 | break; | |
4478 | } | |
4479 | ||
4480 | if (args[i].name_off && | |
4481 | (!btf_name_offset_valid(btf, args[i].name_off) || | |
4482 | !btf_name_valid_identifier(btf, args[i].name_off))) { | |
4483 | btf_verifier_log_type(env, t, | |
4484 | "Invalid arg#%u", i + 1); | |
4485 | err = -EINVAL; | |
4486 | break; | |
4487 | } | |
4488 | ||
4489 | if (btf_type_needs_resolve(arg_type) && | |
4490 | !env_type_is_resolved(env, arg_type_id)) { | |
4491 | err = btf_resolve(env, arg_type, arg_type_id); | |
4492 | if (err) | |
4493 | break; | |
4494 | } | |
4495 | ||
4496 | if (!btf_type_id_size(btf, &arg_type_id, NULL)) { | |
4497 | btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1); | |
4498 | err = -EINVAL; | |
4499 | break; | |
4500 | } | |
4501 | } | |
4502 | ||
4503 | return err; | |
4504 | } | |
4505 | ||
4506 | static int btf_func_check(struct btf_verifier_env *env, | |
4507 | const struct btf_type *t) | |
4508 | { | |
4509 | const struct btf_type *proto_type; | |
4510 | const struct btf_param *args; | |
4511 | const struct btf *btf; | |
4512 | u16 nr_args, i; | |
4513 | ||
4514 | btf = env->btf; | |
4515 | proto_type = btf_type_by_id(btf, t->type); | |
4516 | ||
4517 | if (!proto_type || !btf_type_is_func_proto(proto_type)) { | |
4518 | btf_verifier_log_type(env, t, "Invalid type_id"); | |
4519 | return -EINVAL; | |
4520 | } | |
4521 | ||
4522 | args = (const struct btf_param *)(proto_type + 1); | |
4523 | nr_args = btf_type_vlen(proto_type); | |
4524 | for (i = 0; i < nr_args; i++) { | |
4525 | if (!args[i].name_off && args[i].type) { | |
4526 | btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1); | |
4527 | return -EINVAL; | |
4528 | } | |
4529 | } | |
4530 | ||
4531 | return 0; | |
4532 | } | |
4533 | ||
69b693f0 MKL |
4534 | static const struct btf_kind_operations * const kind_ops[NR_BTF_KINDS] = { |
4535 | [BTF_KIND_INT] = &int_ops, | |
4536 | [BTF_KIND_PTR] = &ptr_ops, | |
4537 | [BTF_KIND_ARRAY] = &array_ops, | |
4538 | [BTF_KIND_STRUCT] = &struct_ops, | |
4539 | [BTF_KIND_UNION] = &struct_ops, | |
4540 | [BTF_KIND_ENUM] = &enum_ops, | |
4541 | [BTF_KIND_FWD] = &fwd_ops, | |
4542 | [BTF_KIND_TYPEDEF] = &modifier_ops, | |
4543 | [BTF_KIND_VOLATILE] = &modifier_ops, | |
4544 | [BTF_KIND_CONST] = &modifier_ops, | |
4545 | [BTF_KIND_RESTRICT] = &modifier_ops, | |
2667a262 MKL |
4546 | [BTF_KIND_FUNC] = &func_ops, |
4547 | [BTF_KIND_FUNC_PROTO] = &func_proto_ops, | |
1dc92851 DB |
4548 | [BTF_KIND_VAR] = &var_ops, |
4549 | [BTF_KIND_DATASEC] = &datasec_ops, | |
b1828f0b | 4550 | [BTF_KIND_FLOAT] = &float_ops, |
223f903e | 4551 | [BTF_KIND_DECL_TAG] = &decl_tag_ops, |
8c42d2fa | 4552 | [BTF_KIND_TYPE_TAG] = &modifier_ops, |
6089fb32 | 4553 | [BTF_KIND_ENUM64] = &enum64_ops, |
69b693f0 MKL |
4554 | }; |
4555 | ||
4556 | static s32 btf_check_meta(struct btf_verifier_env *env, | |
4557 | const struct btf_type *t, | |
4558 | u32 meta_left) | |
4559 | { | |
4560 | u32 saved_meta_left = meta_left; | |
4561 | s32 var_meta_size; | |
4562 | ||
4563 | if (meta_left < sizeof(*t)) { | |
4564 | btf_verifier_log(env, "[%u] meta_left:%u meta_needed:%zu", | |
4565 | env->log_type_id, meta_left, sizeof(*t)); | |
4566 | return -EINVAL; | |
4567 | } | |
4568 | meta_left -= sizeof(*t); | |
4569 | ||
aea2f7b8 MKL |
4570 | if (t->info & ~BTF_INFO_MASK) { |
4571 | btf_verifier_log(env, "[%u] Invalid btf_info:%x", | |
4572 | env->log_type_id, t->info); | |
4573 | return -EINVAL; | |
4574 | } | |
4575 | ||
69b693f0 MKL |
4576 | if (BTF_INFO_KIND(t->info) > BTF_KIND_MAX || |
4577 | BTF_INFO_KIND(t->info) == BTF_KIND_UNKN) { | |
4578 | btf_verifier_log(env, "[%u] Invalid kind:%u", | |
4579 | env->log_type_id, BTF_INFO_KIND(t->info)); | |
4580 | return -EINVAL; | |
4581 | } | |
4582 | ||
fbcf93eb | 4583 | if (!btf_name_offset_valid(env->btf, t->name_off)) { |
69b693f0 | 4584 | btf_verifier_log(env, "[%u] Invalid name_offset:%u", |
fbcf93eb | 4585 | env->log_type_id, t->name_off); |
69b693f0 MKL |
4586 | return -EINVAL; |
4587 | } | |
4588 | ||
4589 | var_meta_size = btf_type_ops(t)->check_meta(env, t, meta_left); | |
4590 | if (var_meta_size < 0) | |
4591 | return var_meta_size; | |
4592 | ||
4593 | meta_left -= var_meta_size; | |
4594 | ||
4595 | return saved_meta_left - meta_left; | |
4596 | } | |
4597 | ||
4598 | static int btf_check_all_metas(struct btf_verifier_env *env) | |
4599 | { | |
4600 | struct btf *btf = env->btf; | |
4601 | struct btf_header *hdr; | |
4602 | void *cur, *end; | |
4603 | ||
f80442a4 | 4604 | hdr = &btf->hdr; |
69b693f0 | 4605 | cur = btf->nohdr_data + hdr->type_off; |
4b1c5d91 | 4606 | end = cur + hdr->type_len; |
69b693f0 | 4607 | |
951bb646 | 4608 | env->log_type_id = btf->base_btf ? btf->start_id : 1; |
69b693f0 MKL |
4609 | while (cur < end) { |
4610 | struct btf_type *t = cur; | |
4611 | s32 meta_size; | |
4612 | ||
4613 | meta_size = btf_check_meta(env, t, end - cur); | |
4614 | if (meta_size < 0) | |
4615 | return meta_size; | |
4616 | ||
4617 | btf_add_type(env, t); | |
4618 | cur += meta_size; | |
4619 | env->log_type_id++; | |
4620 | } | |
4621 | ||
4622 | return 0; | |
4623 | } | |
4624 | ||
eb3f595d MKL |
4625 | static bool btf_resolve_valid(struct btf_verifier_env *env, |
4626 | const struct btf_type *t, | |
4627 | u32 type_id) | |
4628 | { | |
4629 | struct btf *btf = env->btf; | |
4630 | ||
4631 | if (!env_type_is_resolved(env, type_id)) | |
4632 | return false; | |
4633 | ||
1dc92851 | 4634 | if (btf_type_is_struct(t) || btf_type_is_datasec(t)) |
951bb646 AN |
4635 | return !btf_resolved_type_id(btf, type_id) && |
4636 | !btf_resolved_type_size(btf, type_id); | |
eb3f595d | 4637 | |
d7e7b42f | 4638 | if (btf_type_is_decl_tag(t) || btf_type_is_func(t)) |
b5ea834d YS |
4639 | return btf_resolved_type_id(btf, type_id) && |
4640 | !btf_resolved_type_size(btf, type_id); | |
4641 | ||
1dc92851 DB |
4642 | if (btf_type_is_modifier(t) || btf_type_is_ptr(t) || |
4643 | btf_type_is_var(t)) { | |
eb3f595d | 4644 | t = btf_type_id_resolve(btf, &type_id); |
1dc92851 DB |
4645 | return t && |
4646 | !btf_type_is_modifier(t) && | |
4647 | !btf_type_is_var(t) && | |
4648 | !btf_type_is_datasec(t); | |
eb3f595d MKL |
4649 | } |
4650 | ||
4651 | if (btf_type_is_array(t)) { | |
4652 | const struct btf_array *array = btf_type_array(t); | |
4653 | const struct btf_type *elem_type; | |
4654 | u32 elem_type_id = array->type; | |
4655 | u32 elem_size; | |
4656 | ||
4657 | elem_type = btf_type_id_size(btf, &elem_type_id, &elem_size); | |
4658 | return elem_type && !btf_type_is_modifier(elem_type) && | |
4659 | (array->nelems * elem_size == | |
951bb646 | 4660 | btf_resolved_type_size(btf, type_id)); |
eb3f595d MKL |
4661 | } |
4662 | ||
4663 | return false; | |
4664 | } | |
4665 | ||
2667a262 MKL |
4666 | static int btf_resolve(struct btf_verifier_env *env, |
4667 | const struct btf_type *t, u32 type_id) | |
4668 | { | |
4669 | u32 save_log_type_id = env->log_type_id; | |
4670 | const struct resolve_vertex *v; | |
4671 | int err = 0; | |
4672 | ||
4673 | env->resolve_mode = RESOLVE_TBD; | |
4674 | env_stack_push(env, t, type_id); | |
4675 | while (!err && (v = env_stack_peak(env))) { | |
4676 | env->log_type_id = v->type_id; | |
4677 | err = btf_type_ops(v->t)->resolve(env, v); | |
4678 | } | |
4679 | ||
4680 | env->log_type_id = type_id; | |
4681 | if (err == -E2BIG) { | |
4682 | btf_verifier_log_type(env, t, | |
4683 | "Exceeded max resolving depth:%u", | |
4684 | MAX_RESOLVE_DEPTH); | |
4685 | } else if (err == -EEXIST) { | |
4686 | btf_verifier_log_type(env, t, "Loop detected"); | |
4687 | } | |
4688 | ||
4689 | /* Final sanity check */ | |
4690 | if (!err && !btf_resolve_valid(env, t, type_id)) { | |
4691 | btf_verifier_log_type(env, t, "Invalid resolve state"); | |
4692 | err = -EINVAL; | |
4693 | } | |
4694 | ||
4695 | env->log_type_id = save_log_type_id; | |
4696 | return err; | |
4697 | } | |
4698 | ||
eb3f595d MKL |
4699 | static int btf_check_all_types(struct btf_verifier_env *env) |
4700 | { | |
4701 | struct btf *btf = env->btf; | |
951bb646 AN |
4702 | const struct btf_type *t; |
4703 | u32 type_id, i; | |
eb3f595d MKL |
4704 | int err; |
4705 | ||
4706 | err = env_resolve_init(env); | |
4707 | if (err) | |
4708 | return err; | |
4709 | ||
4710 | env->phase++; | |
951bb646 AN |
4711 | for (i = btf->base_btf ? 0 : 1; i < btf->nr_types; i++) { |
4712 | type_id = btf->start_id + i; | |
4713 | t = btf_type_by_id(btf, type_id); | |
eb3f595d MKL |
4714 | |
4715 | env->log_type_id = type_id; | |
4716 | if (btf_type_needs_resolve(t) && | |
4717 | !env_type_is_resolved(env, type_id)) { | |
4718 | err = btf_resolve(env, t, type_id); | |
4719 | if (err) | |
4720 | return err; | |
4721 | } | |
4722 | ||
2667a262 MKL |
4723 | if (btf_type_is_func_proto(t)) { |
4724 | err = btf_func_proto_check(env, t); | |
4725 | if (err) | |
4726 | return err; | |
4727 | } | |
eb3f595d MKL |
4728 | } |
4729 | ||
4730 | return 0; | |
4731 | } | |
4732 | ||
69b693f0 MKL |
4733 | static int btf_parse_type_sec(struct btf_verifier_env *env) |
4734 | { | |
f80442a4 | 4735 | const struct btf_header *hdr = &env->btf->hdr; |
eb3f595d MKL |
4736 | int err; |
4737 | ||
f80442a4 MKL |
4738 | /* Type section must align to 4 bytes */ |
4739 | if (hdr->type_off & (sizeof(u32) - 1)) { | |
4740 | btf_verifier_log(env, "Unaligned type_off"); | |
4741 | return -EINVAL; | |
4742 | } | |
4743 | ||
951bb646 | 4744 | if (!env->btf->base_btf && !hdr->type_len) { |
f80442a4 MKL |
4745 | btf_verifier_log(env, "No type found"); |
4746 | return -EINVAL; | |
4747 | } | |
4748 | ||
eb3f595d MKL |
4749 | err = btf_check_all_metas(env); |
4750 | if (err) | |
4751 | return err; | |
4752 | ||
4753 | return btf_check_all_types(env); | |
69b693f0 MKL |
4754 | } |
4755 | ||
4756 | static int btf_parse_str_sec(struct btf_verifier_env *env) | |
4757 | { | |
4758 | const struct btf_header *hdr; | |
4759 | struct btf *btf = env->btf; | |
4760 | const char *start, *end; | |
4761 | ||
f80442a4 | 4762 | hdr = &btf->hdr; |
69b693f0 MKL |
4763 | start = btf->nohdr_data + hdr->str_off; |
4764 | end = start + hdr->str_len; | |
4765 | ||
f80442a4 MKL |
4766 | if (end != btf->data + btf->data_size) { |
4767 | btf_verifier_log(env, "String section is not at the end"); | |
4768 | return -EINVAL; | |
4769 | } | |
4770 | ||
951bb646 AN |
4771 | btf->strings = start; |
4772 | ||
4773 | if (btf->base_btf && !hdr->str_len) | |
4774 | return 0; | |
4775 | if (!hdr->str_len || hdr->str_len - 1 > BTF_MAX_NAME_OFFSET || end[-1]) { | |
4776 | btf_verifier_log(env, "Invalid string section"); | |
4777 | return -EINVAL; | |
4778 | } | |
4779 | if (!btf->base_btf && start[0]) { | |
69b693f0 MKL |
4780 | btf_verifier_log(env, "Invalid string section"); |
4781 | return -EINVAL; | |
4782 | } | |
69b693f0 MKL |
4783 | |
4784 | return 0; | |
4785 | } | |
4786 | ||
f80442a4 MKL |
4787 | static const size_t btf_sec_info_offset[] = { |
4788 | offsetof(struct btf_header, type_off), | |
4789 | offsetof(struct btf_header, str_off), | |
4790 | }; | |
4791 | ||
4792 | static int btf_sec_info_cmp(const void *a, const void *b) | |
69b693f0 | 4793 | { |
f80442a4 MKL |
4794 | const struct btf_sec_info *x = a; |
4795 | const struct btf_sec_info *y = b; | |
4796 | ||
4797 | return (int)(x->off - y->off) ? : (int)(x->len - y->len); | |
4798 | } | |
4799 | ||
4800 | static int btf_check_sec_info(struct btf_verifier_env *env, | |
4801 | u32 btf_data_size) | |
4802 | { | |
a2889a4c | 4803 | struct btf_sec_info secs[ARRAY_SIZE(btf_sec_info_offset)]; |
f80442a4 | 4804 | u32 total, expected_total, i; |
69b693f0 | 4805 | const struct btf_header *hdr; |
f80442a4 MKL |
4806 | const struct btf *btf; |
4807 | ||
4808 | btf = env->btf; | |
4809 | hdr = &btf->hdr; | |
4810 | ||
4811 | /* Populate the secs from hdr */ | |
a2889a4c | 4812 | for (i = 0; i < ARRAY_SIZE(btf_sec_info_offset); i++) |
f80442a4 MKL |
4813 | secs[i] = *(struct btf_sec_info *)((void *)hdr + |
4814 | btf_sec_info_offset[i]); | |
4815 | ||
a2889a4c MKL |
4816 | sort(secs, ARRAY_SIZE(btf_sec_info_offset), |
4817 | sizeof(struct btf_sec_info), btf_sec_info_cmp, NULL); | |
f80442a4 MKL |
4818 | |
4819 | /* Check for gaps and overlap among sections */ | |
4820 | total = 0; | |
4821 | expected_total = btf_data_size - hdr->hdr_len; | |
a2889a4c | 4822 | for (i = 0; i < ARRAY_SIZE(btf_sec_info_offset); i++) { |
f80442a4 MKL |
4823 | if (expected_total < secs[i].off) { |
4824 | btf_verifier_log(env, "Invalid section offset"); | |
4825 | return -EINVAL; | |
4826 | } | |
4827 | if (total < secs[i].off) { | |
4828 | /* gap */ | |
4829 | btf_verifier_log(env, "Unsupported section found"); | |
4830 | return -EINVAL; | |
4831 | } | |
4832 | if (total > secs[i].off) { | |
4833 | btf_verifier_log(env, "Section overlap found"); | |
4834 | return -EINVAL; | |
4835 | } | |
4836 | if (expected_total - total < secs[i].len) { | |
4837 | btf_verifier_log(env, | |
4838 | "Total section length too long"); | |
4839 | return -EINVAL; | |
4840 | } | |
4841 | total += secs[i].len; | |
4842 | } | |
4843 | ||
4844 | /* There is data other than hdr and known sections */ | |
4845 | if (expected_total != total) { | |
4846 | btf_verifier_log(env, "Unsupported section found"); | |
4847 | return -EINVAL; | |
4848 | } | |
4849 | ||
4850 | return 0; | |
4851 | } | |
4852 | ||
4a6998af | 4853 | static int btf_parse_hdr(struct btf_verifier_env *env) |
f80442a4 | 4854 | { |
4a6998af | 4855 | u32 hdr_len, hdr_copy, btf_data_size; |
f80442a4 | 4856 | const struct btf_header *hdr; |
f80442a4 MKL |
4857 | struct btf *btf; |
4858 | int err; | |
69b693f0 | 4859 | |
f80442a4 | 4860 | btf = env->btf; |
4a6998af | 4861 | btf_data_size = btf->data_size; |
f80442a4 | 4862 | |
583669ab | 4863 | if (btf_data_size < offsetofend(struct btf_header, hdr_len)) { |
f80442a4 MKL |
4864 | btf_verifier_log(env, "hdr_len not found"); |
4865 | return -EINVAL; | |
4866 | } | |
4867 | ||
4a6998af ML |
4868 | hdr = btf->data; |
4869 | hdr_len = hdr->hdr_len; | |
f80442a4 | 4870 | if (btf_data_size < hdr_len) { |
69b693f0 MKL |
4871 | btf_verifier_log(env, "btf_header not found"); |
4872 | return -EINVAL; | |
4873 | } | |
4874 | ||
4a6998af ML |
4875 | /* Ensure the unsupported header fields are zero */ |
4876 | if (hdr_len > sizeof(btf->hdr)) { | |
4877 | u8 *expected_zero = btf->data + sizeof(btf->hdr); | |
4878 | u8 *end = btf->data + hdr_len; | |
4879 | ||
4880 | for (; expected_zero < end; expected_zero++) { | |
4881 | if (*expected_zero) { | |
4882 | btf_verifier_log(env, "Unsupported btf_header"); | |
4883 | return -E2BIG; | |
4884 | } | |
4885 | } | |
f80442a4 MKL |
4886 | } |
4887 | ||
4888 | hdr_copy = min_t(u32, hdr_len, sizeof(btf->hdr)); | |
4a6998af | 4889 | memcpy(&btf->hdr, btf->data, hdr_copy); |
f80442a4 MKL |
4890 | |
4891 | hdr = &btf->hdr; | |
4892 | ||
4893 | btf_verifier_log_hdr(env, btf_data_size); | |
69b693f0 | 4894 | |
69b693f0 MKL |
4895 | if (hdr->magic != BTF_MAGIC) { |
4896 | btf_verifier_log(env, "Invalid magic"); | |
4897 | return -EINVAL; | |
4898 | } | |
4899 | ||
4900 | if (hdr->version != BTF_VERSION) { | |
4901 | btf_verifier_log(env, "Unsupported version"); | |
4902 | return -ENOTSUPP; | |
4903 | } | |
4904 | ||
4905 | if (hdr->flags) { | |
4906 | btf_verifier_log(env, "Unsupported flags"); | |
4907 | return -ENOTSUPP; | |
4908 | } | |
4909 | ||
bcc5e616 | 4910 | if (!btf->base_btf && btf_data_size == hdr->hdr_len) { |
69b693f0 MKL |
4911 | btf_verifier_log(env, "No data"); |
4912 | return -EINVAL; | |
4913 | } | |
4914 | ||
f80442a4 MKL |
4915 | err = btf_check_sec_info(env, btf_data_size); |
4916 | if (err) | |
4917 | return err; | |
69b693f0 MKL |
4918 | |
4919 | return 0; | |
4920 | } | |
4921 | ||
eb596b09 KKD |
4922 | static int btf_check_type_tags(struct btf_verifier_env *env, |
4923 | struct btf *btf, int start_id) | |
4924 | { | |
4925 | int i, n, good_id = start_id - 1; | |
4926 | bool in_tags; | |
4927 | ||
4928 | n = btf_nr_types(btf); | |
4929 | for (i = start_id; i < n; i++) { | |
4930 | const struct btf_type *t; | |
d1a374a1 | 4931 | int chain_limit = 32; |
eb596b09 KKD |
4932 | u32 cur_id = i; |
4933 | ||
4934 | t = btf_type_by_id(btf, i); | |
4935 | if (!t) | |
4936 | return -EINVAL; | |
4937 | if (!btf_type_is_modifier(t)) | |
4938 | continue; | |
4939 | ||
4940 | cond_resched(); | |
4941 | ||
4942 | in_tags = btf_type_is_type_tag(t); | |
4943 | while (btf_type_is_modifier(t)) { | |
d1a374a1 KKD |
4944 | if (!chain_limit--) { |
4945 | btf_verifier_log(env, "Max chain length or cycle detected"); | |
4946 | return -ELOOP; | |
4947 | } | |
eb596b09 KKD |
4948 | if (btf_type_is_type_tag(t)) { |
4949 | if (!in_tags) { | |
4950 | btf_verifier_log(env, "Type tags don't precede modifiers"); | |
4951 | return -EINVAL; | |
4952 | } | |
4953 | } else if (in_tags) { | |
4954 | in_tags = false; | |
4955 | } | |
4956 | if (cur_id <= good_id) | |
4957 | break; | |
4958 | /* Move to next type */ | |
4959 | cur_id = t->type; | |
4960 | t = btf_type_by_id(btf, cur_id); | |
4961 | if (!t) | |
4962 | return -EINVAL; | |
4963 | } | |
4964 | good_id = i; | |
4965 | } | |
4966 | return 0; | |
4967 | } | |
4968 | ||
c571bd75 | 4969 | static struct btf *btf_parse(bpfptr_t btf_data, u32 btf_data_size, |
69b693f0 MKL |
4970 | u32 log_level, char __user *log_ubuf, u32 log_size) |
4971 | { | |
4972 | struct btf_verifier_env *env = NULL; | |
4973 | struct bpf_verifier_log *log; | |
4974 | struct btf *btf = NULL; | |
4975 | u8 *data; | |
4976 | int err; | |
4977 | ||
4978 | if (btf_data_size > BTF_MAX_SIZE) | |
4979 | return ERR_PTR(-E2BIG); | |
4980 | ||
4981 | env = kzalloc(sizeof(*env), GFP_KERNEL | __GFP_NOWARN); | |
4982 | if (!env) | |
4983 | return ERR_PTR(-ENOMEM); | |
4984 | ||
4985 | log = &env->log; | |
4986 | if (log_level || log_ubuf || log_size) { | |
4987 | /* user requested verbose verifier output | |
4988 | * and supplied buffer to store the verification trace | |
4989 | */ | |
4990 | log->level = log_level; | |
4991 | log->ubuf = log_ubuf; | |
4992 | log->len_total = log_size; | |
4993 | ||
4994 | /* log attributes have to be sane */ | |
866de407 | 4995 | if (!bpf_verifier_log_attr_valid(log)) { |
69b693f0 MKL |
4996 | err = -EINVAL; |
4997 | goto errout; | |
4998 | } | |
4999 | } | |
5000 | ||
5001 | btf = kzalloc(sizeof(*btf), GFP_KERNEL | __GFP_NOWARN); | |
5002 | if (!btf) { | |
5003 | err = -ENOMEM; | |
5004 | goto errout; | |
5005 | } | |
f80442a4 MKL |
5006 | env->btf = btf; |
5007 | ||
69b693f0 MKL |
5008 | data = kvmalloc(btf_data_size, GFP_KERNEL | __GFP_NOWARN); |
5009 | if (!data) { | |
5010 | err = -ENOMEM; | |
5011 | goto errout; | |
5012 | } | |
5013 | ||
5014 | btf->data = data; | |
5015 | btf->data_size = btf_data_size; | |
5016 | ||
c571bd75 | 5017 | if (copy_from_bpfptr(data, btf_data, btf_data_size)) { |
69b693f0 MKL |
5018 | err = -EFAULT; |
5019 | goto errout; | |
5020 | } | |
5021 | ||
4a6998af ML |
5022 | err = btf_parse_hdr(env); |
5023 | if (err) | |
5024 | goto errout; | |
5025 | ||
5026 | btf->nohdr_data = btf->data + btf->hdr.hdr_len; | |
5027 | ||
69b693f0 MKL |
5028 | err = btf_parse_str_sec(env); |
5029 | if (err) | |
5030 | goto errout; | |
5031 | ||
5032 | err = btf_parse_type_sec(env); | |
5033 | if (err) | |
5034 | goto errout; | |
5035 | ||
eb596b09 KKD |
5036 | err = btf_check_type_tags(env, btf, 1); |
5037 | if (err) | |
5038 | goto errout; | |
5039 | ||
f80442a4 | 5040 | if (log->level && bpf_verifier_log_full(log)) { |
69b693f0 MKL |
5041 | err = -ENOSPC; |
5042 | goto errout; | |
5043 | } | |
5044 | ||
f80442a4 MKL |
5045 | btf_verifier_env_free(env); |
5046 | refcount_set(&btf->refcnt, 1); | |
5047 | return btf; | |
69b693f0 MKL |
5048 | |
5049 | errout: | |
5050 | btf_verifier_env_free(env); | |
5051 | if (btf) | |
5052 | btf_free(btf); | |
5053 | return ERR_PTR(err); | |
5054 | } | |
b00b8dae | 5055 | |
90ceddcb FS |
5056 | extern char __weak __start_BTF[]; |
5057 | extern char __weak __stop_BTF[]; | |
91cc1a99 AS |
5058 | extern struct btf *btf_vmlinux; |
5059 | ||
5060 | #define BPF_MAP_TYPE(_id, _ops) | |
f2e10bff | 5061 | #define BPF_LINK_TYPE(_id, _name) |
91cc1a99 AS |
5062 | static union { |
5063 | struct bpf_ctx_convert { | |
5064 | #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \ | |
5065 | prog_ctx_type _id##_prog; \ | |
5066 | kern_ctx_type _id##_kern; | |
5067 | #include <linux/bpf_types.h> | |
5068 | #undef BPF_PROG_TYPE | |
5069 | } *__t; | |
5070 | /* 't' is written once under lock. Read many times. */ | |
5071 | const struct btf_type *t; | |
5072 | } bpf_ctx_convert; | |
5073 | enum { | |
5074 | #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \ | |
5075 | __ctx_convert##_id, | |
5076 | #include <linux/bpf_types.h> | |
5077 | #undef BPF_PROG_TYPE | |
ce27709b | 5078 | __ctx_convert_unused, /* to avoid empty enum in extreme .config */ |
91cc1a99 AS |
5079 | }; |
5080 | static u8 bpf_ctx_convert_map[] = { | |
5081 | #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \ | |
5082 | [_id] = __ctx_convert##_id, | |
5083 | #include <linux/bpf_types.h> | |
5084 | #undef BPF_PROG_TYPE | |
4c80c7bc | 5085 | 0, /* avoid empty array */ |
91cc1a99 AS |
5086 | }; |
5087 | #undef BPF_MAP_TYPE | |
f2e10bff | 5088 | #undef BPF_LINK_TYPE |
91cc1a99 AS |
5089 | |
5090 | static const struct btf_member * | |
34747c41 | 5091 | btf_get_prog_ctx_type(struct bpf_verifier_log *log, const struct btf *btf, |
51c39bb1 AS |
5092 | const struct btf_type *t, enum bpf_prog_type prog_type, |
5093 | int arg) | |
91cc1a99 AS |
5094 | { |
5095 | const struct btf_type *conv_struct; | |
5096 | const struct btf_type *ctx_struct; | |
5097 | const struct btf_member *ctx_type; | |
5098 | const char *tname, *ctx_tname; | |
5099 | ||
5100 | conv_struct = bpf_ctx_convert.t; | |
5101 | if (!conv_struct) { | |
5102 | bpf_log(log, "btf_vmlinux is malformed\n"); | |
5103 | return NULL; | |
5104 | } | |
5105 | t = btf_type_by_id(btf, t->type); | |
5106 | while (btf_type_is_modifier(t)) | |
5107 | t = btf_type_by_id(btf, t->type); | |
5108 | if (!btf_type_is_struct(t)) { | |
5109 | /* Only pointer to struct is supported for now. | |
5110 | * That means that BPF_PROG_TYPE_TRACEPOINT with BTF | |
5111 | * is not supported yet. | |
5112 | * BPF_PROG_TYPE_RAW_TRACEPOINT is fine. | |
5113 | */ | |
91cc1a99 AS |
5114 | return NULL; |
5115 | } | |
5116 | tname = btf_name_by_offset(btf, t->name_off); | |
5117 | if (!tname) { | |
51c39bb1 | 5118 | bpf_log(log, "arg#%d struct doesn't have a name\n", arg); |
91cc1a99 AS |
5119 | return NULL; |
5120 | } | |
5121 | /* prog_type is valid bpf program type. No need for bounds check. */ | |
5122 | ctx_type = btf_type_member(conv_struct) + bpf_ctx_convert_map[prog_type] * 2; | |
5123 | /* ctx_struct is a pointer to prog_ctx_type in vmlinux. | |
5124 | * Like 'struct __sk_buff' | |
5125 | */ | |
5126 | ctx_struct = btf_type_by_id(btf_vmlinux, ctx_type->type); | |
5127 | if (!ctx_struct) | |
5128 | /* should not happen */ | |
5129 | return NULL; | |
5130 | ctx_tname = btf_name_by_offset(btf_vmlinux, ctx_struct->name_off); | |
5131 | if (!ctx_tname) { | |
5132 | /* should not happen */ | |
5133 | bpf_log(log, "Please fix kernel include/linux/bpf_types.h\n"); | |
5134 | return NULL; | |
5135 | } | |
5136 | /* only compare that prog's ctx type name is the same as | |
5137 | * kernel expects. No need to compare field by field. | |
5138 | * It's ok for bpf prog to do: | |
5139 | * struct __sk_buff {}; | |
5140 | * int socket_filter_bpf_prog(struct __sk_buff *skb) | |
5141 | * { // no fields of skb are ever used } | |
5142 | */ | |
5143 | if (strcmp(ctx_tname, tname)) | |
5144 | return NULL; | |
5145 | return ctx_type; | |
5146 | } | |
8580ac94 | 5147 | |
5b92a28a AS |
5148 | static int btf_translate_to_vmlinux(struct bpf_verifier_log *log, |
5149 | struct btf *btf, | |
5150 | const struct btf_type *t, | |
51c39bb1 AS |
5151 | enum bpf_prog_type prog_type, |
5152 | int arg) | |
5b92a28a AS |
5153 | { |
5154 | const struct btf_member *prog_ctx_type, *kern_ctx_type; | |
5155 | ||
51c39bb1 | 5156 | prog_ctx_type = btf_get_prog_ctx_type(log, btf, t, prog_type, arg); |
5b92a28a AS |
5157 | if (!prog_ctx_type) |
5158 | return -ENOENT; | |
5159 | kern_ctx_type = prog_ctx_type + 1; | |
5160 | return kern_ctx_type->type; | |
5161 | } | |
5162 | ||
49f4e672 JO |
5163 | BTF_ID_LIST(bpf_ctx_convert_btf_id) |
5164 | BTF_ID(struct, bpf_ctx_convert) | |
5165 | ||
8580ac94 AS |
5166 | struct btf *btf_parse_vmlinux(void) |
5167 | { | |
5168 | struct btf_verifier_env *env = NULL; | |
5169 | struct bpf_verifier_log *log; | |
5170 | struct btf *btf = NULL; | |
49f4e672 | 5171 | int err; |
8580ac94 AS |
5172 | |
5173 | env = kzalloc(sizeof(*env), GFP_KERNEL | __GFP_NOWARN); | |
5174 | if (!env) | |
5175 | return ERR_PTR(-ENOMEM); | |
5176 | ||
5177 | log = &env->log; | |
5178 | log->level = BPF_LOG_KERNEL; | |
5179 | ||
5180 | btf = kzalloc(sizeof(*btf), GFP_KERNEL | __GFP_NOWARN); | |
5181 | if (!btf) { | |
5182 | err = -ENOMEM; | |
5183 | goto errout; | |
5184 | } | |
5185 | env->btf = btf; | |
5186 | ||
90ceddcb FS |
5187 | btf->data = __start_BTF; |
5188 | btf->data_size = __stop_BTF - __start_BTF; | |
53297220 AN |
5189 | btf->kernel_btf = true; |
5190 | snprintf(btf->name, sizeof(btf->name), "vmlinux"); | |
8580ac94 AS |
5191 | |
5192 | err = btf_parse_hdr(env); | |
5193 | if (err) | |
5194 | goto errout; | |
5195 | ||
5196 | btf->nohdr_data = btf->data + btf->hdr.hdr_len; | |
5197 | ||
5198 | err = btf_parse_str_sec(env); | |
5199 | if (err) | |
5200 | goto errout; | |
5201 | ||
5202 | err = btf_check_all_metas(env); | |
5203 | if (err) | |
5204 | goto errout; | |
5205 | ||
eb596b09 KKD |
5206 | err = btf_check_type_tags(env, btf, 1); |
5207 | if (err) | |
5208 | goto errout; | |
5209 | ||
a2d0d62f | 5210 | /* btf_parse_vmlinux() runs under bpf_verifier_lock */ |
49f4e672 | 5211 | bpf_ctx_convert.t = btf_type_by_id(btf, bpf_ctx_convert_btf_id[0]); |
91cc1a99 | 5212 | |
d3e42bb0 | 5213 | bpf_struct_ops_init(btf, log); |
27ae7997 | 5214 | |
8580ac94 | 5215 | refcount_set(&btf->refcnt, 1); |
53297220 AN |
5216 | |
5217 | err = btf_alloc_id(btf); | |
5218 | if (err) | |
5219 | goto errout; | |
5220 | ||
5221 | btf_verifier_env_free(env); | |
8580ac94 AS |
5222 | return btf; |
5223 | ||
5224 | errout: | |
5225 | btf_verifier_env_free(env); | |
5226 | if (btf) { | |
5227 | kvfree(btf->types); | |
5228 | kfree(btf); | |
5229 | } | |
5230 | return ERR_PTR(err); | |
5231 | } | |
5232 | ||
7112d127 AN |
5233 | #ifdef CONFIG_DEBUG_INFO_BTF_MODULES |
5234 | ||
36e68442 AN |
5235 | static struct btf *btf_parse_module(const char *module_name, const void *data, unsigned int data_size) |
5236 | { | |
5237 | struct btf_verifier_env *env = NULL; | |
5238 | struct bpf_verifier_log *log; | |
5239 | struct btf *btf = NULL, *base_btf; | |
5240 | int err; | |
5241 | ||
5242 | base_btf = bpf_get_btf_vmlinux(); | |
5243 | if (IS_ERR(base_btf)) | |
5244 | return base_btf; | |
5245 | if (!base_btf) | |
5246 | return ERR_PTR(-EINVAL); | |
5247 | ||
5248 | env = kzalloc(sizeof(*env), GFP_KERNEL | __GFP_NOWARN); | |
5249 | if (!env) | |
5250 | return ERR_PTR(-ENOMEM); | |
5251 | ||
5252 | log = &env->log; | |
5253 | log->level = BPF_LOG_KERNEL; | |
5254 | ||
5255 | btf = kzalloc(sizeof(*btf), GFP_KERNEL | __GFP_NOWARN); | |
5256 | if (!btf) { | |
5257 | err = -ENOMEM; | |
5258 | goto errout; | |
5259 | } | |
5260 | env->btf = btf; | |
5261 | ||
5262 | btf->base_btf = base_btf; | |
5263 | btf->start_id = base_btf->nr_types; | |
5264 | btf->start_str_off = base_btf->hdr.str_len; | |
5265 | btf->kernel_btf = true; | |
5266 | snprintf(btf->name, sizeof(btf->name), "%s", module_name); | |
5267 | ||
5268 | btf->data = kvmalloc(data_size, GFP_KERNEL | __GFP_NOWARN); | |
5269 | if (!btf->data) { | |
5270 | err = -ENOMEM; | |
5271 | goto errout; | |
5272 | } | |
5273 | memcpy(btf->data, data, data_size); | |
5274 | btf->data_size = data_size; | |
5275 | ||
5276 | err = btf_parse_hdr(env); | |
5277 | if (err) | |
5278 | goto errout; | |
5279 | ||
5280 | btf->nohdr_data = btf->data + btf->hdr.hdr_len; | |
5281 | ||
5282 | err = btf_parse_str_sec(env); | |
5283 | if (err) | |
5284 | goto errout; | |
5285 | ||
5286 | err = btf_check_all_metas(env); | |
5287 | if (err) | |
5288 | goto errout; | |
5289 | ||
eb596b09 KKD |
5290 | err = btf_check_type_tags(env, btf, btf_nr_types(base_btf)); |
5291 | if (err) | |
5292 | goto errout; | |
5293 | ||
36e68442 AN |
5294 | btf_verifier_env_free(env); |
5295 | refcount_set(&btf->refcnt, 1); | |
5296 | return btf; | |
5297 | ||
5298 | errout: | |
5299 | btf_verifier_env_free(env); | |
5300 | if (btf) { | |
5301 | kvfree(btf->data); | |
5302 | kvfree(btf->types); | |
5303 | kfree(btf); | |
5304 | } | |
5305 | return ERR_PTR(err); | |
5306 | } | |
5307 | ||
7112d127 AN |
5308 | #endif /* CONFIG_DEBUG_INFO_BTF_MODULES */ |
5309 | ||
5b92a28a AS |
5310 | struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog) |
5311 | { | |
3aac1ead | 5312 | struct bpf_prog *tgt_prog = prog->aux->dst_prog; |
5b92a28a | 5313 | |
22dc4a0f | 5314 | if (tgt_prog) |
5b92a28a | 5315 | return tgt_prog->aux->btf; |
22dc4a0f AN |
5316 | else |
5317 | return prog->aux->attach_btf; | |
5b92a28a AS |
5318 | } |
5319 | ||
bb6728d7 | 5320 | static bool is_int_ptr(struct btf *btf, const struct btf_type *t) |
84ad7a7a JO |
5321 | { |
5322 | /* t comes in already as a pointer */ | |
5323 | t = btf_type_by_id(btf, t->type); | |
5324 | ||
5325 | /* allow const */ | |
5326 | if (BTF_INFO_KIND(t->info) == BTF_KIND_CONST) | |
5327 | t = btf_type_by_id(btf, t->type); | |
5328 | ||
bb6728d7 | 5329 | return btf_type_is_int(t); |
84ad7a7a JO |
5330 | } |
5331 | ||
720e6a43 YS |
5332 | static u32 get_ctx_arg_idx(struct btf *btf, const struct btf_type *func_proto, |
5333 | int off) | |
5334 | { | |
5335 | const struct btf_param *args; | |
5336 | const struct btf_type *t; | |
5337 | u32 offset = 0, nr_args; | |
5338 | int i; | |
5339 | ||
5340 | if (!func_proto) | |
5341 | return off / 8; | |
5342 | ||
5343 | nr_args = btf_type_vlen(func_proto); | |
5344 | args = (const struct btf_param *)(func_proto + 1); | |
5345 | for (i = 0; i < nr_args; i++) { | |
5346 | t = btf_type_skip_modifiers(btf, args[i].type, NULL); | |
5347 | offset += btf_type_is_ptr(t) ? 8 : roundup(t->size, 8); | |
5348 | if (off < offset) | |
5349 | return i; | |
5350 | } | |
5351 | ||
5352 | t = btf_type_skip_modifiers(btf, func_proto->type, NULL); | |
5353 | offset += btf_type_is_ptr(t) ? 8 : roundup(t->size, 8); | |
5354 | if (off < offset) | |
5355 | return nr_args; | |
5356 | ||
5357 | return nr_args + 1; | |
5358 | } | |
5359 | ||
9e15db66 AS |
5360 | bool btf_ctx_access(int off, int size, enum bpf_access_type type, |
5361 | const struct bpf_prog *prog, | |
5362 | struct bpf_insn_access_aux *info) | |
5363 | { | |
38207291 | 5364 | const struct btf_type *t = prog->aux->attach_func_proto; |
3aac1ead | 5365 | struct bpf_prog *tgt_prog = prog->aux->dst_prog; |
5b92a28a | 5366 | struct btf *btf = bpf_prog_get_target_btf(prog); |
38207291 | 5367 | const char *tname = prog->aux->attach_func_name; |
9e15db66 | 5368 | struct bpf_verifier_log *log = info->log; |
9e15db66 | 5369 | const struct btf_param *args; |
c6f1bfe8 | 5370 | const char *tag_value; |
9e15db66 | 5371 | u32 nr_args, arg; |
3c32cc1b | 5372 | int i, ret; |
9e15db66 | 5373 | |
9e15db66 | 5374 | if (off % 8) { |
38207291 | 5375 | bpf_log(log, "func '%s' offset %d is not multiple of 8\n", |
9e15db66 AS |
5376 | tname, off); |
5377 | return false; | |
5378 | } | |
720e6a43 | 5379 | arg = get_ctx_arg_idx(btf, t, off); |
9e15db66 | 5380 | args = (const struct btf_param *)(t + 1); |
523a4cf4 DB |
5381 | /* if (t == NULL) Fall back to default BPF prog with |
5382 | * MAX_BPF_FUNC_REG_ARGS u64 arguments. | |
5383 | */ | |
5384 | nr_args = t ? btf_type_vlen(t) : MAX_BPF_FUNC_REG_ARGS; | |
38207291 MKL |
5385 | if (prog->aux->attach_btf_trace) { |
5386 | /* skip first 'void *__data' argument in btf_trace_##name typedef */ | |
5387 | args++; | |
5388 | nr_args--; | |
5389 | } | |
fec56f58 | 5390 | |
f50b49a0 KS |
5391 | if (arg > nr_args) { |
5392 | bpf_log(log, "func '%s' doesn't have %d-th argument\n", | |
5393 | tname, arg + 1); | |
5394 | return false; | |
5395 | } | |
5396 | ||
6ba43b76 | 5397 | if (arg == nr_args) { |
f50b49a0 | 5398 | switch (prog->expected_attach_type) { |
69fd337a | 5399 | case BPF_LSM_CGROUP: |
f50b49a0 KS |
5400 | case BPF_LSM_MAC: |
5401 | case BPF_TRACE_FEXIT: | |
9e4e01df KS |
5402 | /* When LSM programs are attached to void LSM hooks |
5403 | * they use FEXIT trampolines and when attached to | |
5404 | * int LSM hooks, they use MODIFY_RETURN trampolines. | |
5405 | * | |
5406 | * While the LSM programs are BPF_MODIFY_RETURN-like | |
5407 | * the check: | |
5408 | * | |
5409 | * if (ret_type != 'int') | |
5410 | * return -EINVAL; | |
5411 | * | |
5412 | * is _not_ done here. This is still safe as LSM hooks | |
5413 | * have only void and int return types. | |
5414 | */ | |
6ba43b76 KS |
5415 | if (!t) |
5416 | return true; | |
5417 | t = btf_type_by_id(btf, t->type); | |
f50b49a0 KS |
5418 | break; |
5419 | case BPF_MODIFY_RETURN: | |
6ba43b76 KS |
5420 | /* For now the BPF_MODIFY_RETURN can only be attached to |
5421 | * functions that return an int. | |
5422 | */ | |
5423 | if (!t) | |
5424 | return false; | |
5425 | ||
5426 | t = btf_type_skip_modifiers(btf, t->type, NULL); | |
a9b59159 | 5427 | if (!btf_type_is_small_int(t)) { |
6ba43b76 KS |
5428 | bpf_log(log, |
5429 | "ret type %s not allowed for fmod_ret\n", | |
5430 | btf_kind_str[BTF_INFO_KIND(t->info)]); | |
5431 | return false; | |
5432 | } | |
f50b49a0 KS |
5433 | break; |
5434 | default: | |
5435 | bpf_log(log, "func '%s' doesn't have %d-th argument\n", | |
5436 | tname, arg + 1); | |
5437 | return false; | |
6ba43b76 | 5438 | } |
fec56f58 | 5439 | } else { |
5b92a28a | 5440 | if (!t) |
523a4cf4 | 5441 | /* Default prog with MAX_BPF_FUNC_REG_ARGS args */ |
5b92a28a AS |
5442 | return true; |
5443 | t = btf_type_by_id(btf, args[arg].type); | |
9e15db66 | 5444 | } |
f50b49a0 | 5445 | |
9e15db66 AS |
5446 | /* skip modifiers */ |
5447 | while (btf_type_is_modifier(t)) | |
5b92a28a | 5448 | t = btf_type_by_id(btf, t->type); |
720e6a43 | 5449 | if (btf_type_is_small_int(t) || btf_is_any_enum(t) || __btf_type_is_struct(t)) |
9e15db66 AS |
5450 | /* accessing a scalar */ |
5451 | return true; | |
5452 | if (!btf_type_is_ptr(t)) { | |
5453 | bpf_log(log, | |
38207291 | 5454 | "func '%s' arg%d '%s' has type %s. Only pointer access is allowed\n", |
9e15db66 | 5455 | tname, arg, |
5b92a28a | 5456 | __btf_name_by_offset(btf, t->name_off), |
9e15db66 AS |
5457 | btf_kind_str[BTF_INFO_KIND(t->info)]); |
5458 | return false; | |
5459 | } | |
afbf21dc YS |
5460 | |
5461 | /* check for PTR_TO_RDONLY_BUF_OR_NULL or PTR_TO_RDWR_BUF_OR_NULL */ | |
5462 | for (i = 0; i < prog->aux->ctx_arg_info_size; i++) { | |
5463 | const struct bpf_ctx_arg_aux *ctx_arg_info = &prog->aux->ctx_arg_info[i]; | |
c25b2ae1 | 5464 | u32 type, flag; |
afbf21dc | 5465 | |
c25b2ae1 HL |
5466 | type = base_type(ctx_arg_info->reg_type); |
5467 | flag = type_flag(ctx_arg_info->reg_type); | |
20b2aff4 | 5468 | if (ctx_arg_info->offset == off && type == PTR_TO_BUF && |
c25b2ae1 | 5469 | (flag & PTR_MAYBE_NULL)) { |
afbf21dc YS |
5470 | info->reg_type = ctx_arg_info->reg_type; |
5471 | return true; | |
5472 | } | |
5473 | } | |
5474 | ||
9e15db66 AS |
5475 | if (t->type == 0) |
5476 | /* This is a pointer to void. | |
5477 | * It is the same as scalar from the verifier safety pov. | |
5478 | * No further pointer walking is allowed. | |
5479 | */ | |
5480 | return true; | |
5481 | ||
bb6728d7 | 5482 | if (is_int_ptr(btf, t)) |
84ad7a7a JO |
5483 | return true; |
5484 | ||
9e15db66 | 5485 | /* this is a pointer to another type */ |
3c32cc1b YS |
5486 | for (i = 0; i < prog->aux->ctx_arg_info_size; i++) { |
5487 | const struct bpf_ctx_arg_aux *ctx_arg_info = &prog->aux->ctx_arg_info[i]; | |
5488 | ||
5489 | if (ctx_arg_info->offset == off) { | |
d3621642 YS |
5490 | if (!ctx_arg_info->btf_id) { |
5491 | bpf_log(log,"invalid btf_id for context argument offset %u\n", off); | |
5492 | return false; | |
5493 | } | |
5494 | ||
3c32cc1b | 5495 | info->reg_type = ctx_arg_info->reg_type; |
22dc4a0f | 5496 | info->btf = btf_vmlinux; |
951cf368 YS |
5497 | info->btf_id = ctx_arg_info->btf_id; |
5498 | return true; | |
3c32cc1b YS |
5499 | } |
5500 | } | |
9e15db66 | 5501 | |
951cf368 | 5502 | info->reg_type = PTR_TO_BTF_ID; |
5b92a28a | 5503 | if (tgt_prog) { |
43bc2874 THJ |
5504 | enum bpf_prog_type tgt_type; |
5505 | ||
5506 | if (tgt_prog->type == BPF_PROG_TYPE_EXT) | |
5507 | tgt_type = tgt_prog->aux->saved_dst_prog_type; | |
5508 | else | |
5509 | tgt_type = tgt_prog->type; | |
5510 | ||
5511 | ret = btf_translate_to_vmlinux(log, btf, t, tgt_type, arg); | |
5b92a28a | 5512 | if (ret > 0) { |
22dc4a0f | 5513 | info->btf = btf_vmlinux; |
5b92a28a AS |
5514 | info->btf_id = ret; |
5515 | return true; | |
5516 | } else { | |
5517 | return false; | |
5518 | } | |
5519 | } | |
275517ff | 5520 | |
22dc4a0f | 5521 | info->btf = btf; |
275517ff | 5522 | info->btf_id = t->type; |
5b92a28a | 5523 | t = btf_type_by_id(btf, t->type); |
c6f1bfe8 YS |
5524 | |
5525 | if (btf_type_is_type_tag(t)) { | |
5526 | tag_value = __btf_name_by_offset(btf, t->name_off); | |
5527 | if (strcmp(tag_value, "user") == 0) | |
5528 | info->reg_type |= MEM_USER; | |
5844101a HL |
5529 | if (strcmp(tag_value, "percpu") == 0) |
5530 | info->reg_type |= MEM_PERCPU; | |
c6f1bfe8 YS |
5531 | } |
5532 | ||
9e15db66 | 5533 | /* skip modifiers */ |
275517ff MKL |
5534 | while (btf_type_is_modifier(t)) { |
5535 | info->btf_id = t->type; | |
5b92a28a | 5536 | t = btf_type_by_id(btf, t->type); |
275517ff | 5537 | } |
9e15db66 AS |
5538 | if (!btf_type_is_struct(t)) { |
5539 | bpf_log(log, | |
38207291 | 5540 | "func '%s' arg%d type %s is not a struct\n", |
9e15db66 AS |
5541 | tname, arg, btf_kind_str[BTF_INFO_KIND(t->info)]); |
5542 | return false; | |
5543 | } | |
38207291 | 5544 | bpf_log(log, "func '%s' arg%d has btf_id %d type %s '%s'\n", |
9e15db66 | 5545 | tname, arg, info->btf_id, btf_kind_str[BTF_INFO_KIND(t->info)], |
5b92a28a | 5546 | __btf_name_by_offset(btf, t->name_off)); |
9e15db66 AS |
5547 | return true; |
5548 | } | |
5549 | ||
1c6d28a6 JO |
5550 | enum bpf_struct_walk_result { |
5551 | /* < 0 error */ | |
5552 | WALK_SCALAR = 0, | |
5553 | WALK_PTR, | |
5554 | WALK_STRUCT, | |
5555 | }; | |
5556 | ||
22dc4a0f | 5557 | static int btf_struct_walk(struct bpf_verifier_log *log, const struct btf *btf, |
1c6d28a6 | 5558 | const struct btf_type *t, int off, int size, |
c6f1bfe8 | 5559 | u32 *next_btf_id, enum bpf_type_flag *flag) |
9e15db66 | 5560 | { |
7e3617a7 MKL |
5561 | u32 i, moff, mtrue_end, msize = 0, total_nelems = 0; |
5562 | const struct btf_type *mtype, *elem_type = NULL; | |
9e15db66 | 5563 | const struct btf_member *member; |
c6f1bfe8 | 5564 | const char *tname, *mname, *tag_value; |
1c6d28a6 | 5565 | u32 vlen, elem_id, mid; |
9e15db66 AS |
5566 | |
5567 | again: | |
22dc4a0f | 5568 | tname = __btf_name_by_offset(btf, t->name_off); |
9e15db66 | 5569 | if (!btf_type_is_struct(t)) { |
275517ff | 5570 | bpf_log(log, "Type '%s' is not a struct\n", tname); |
9e15db66 AS |
5571 | return -EINVAL; |
5572 | } | |
5573 | ||
9c5f8a10 | 5574 | vlen = btf_type_vlen(t); |
976aba00 | 5575 | if (off + size > t->size) { |
9c5f8a10 YS |
5576 | /* If the last element is a variable size array, we may |
5577 | * need to relax the rule. | |
5578 | */ | |
5579 | struct btf_array *array_elem; | |
5580 | ||
5581 | if (vlen == 0) | |
5582 | goto error; | |
5583 | ||
5584 | member = btf_type_member(t) + vlen - 1; | |
22dc4a0f | 5585 | mtype = btf_type_skip_modifiers(btf, member->type, |
9c5f8a10 YS |
5586 | NULL); |
5587 | if (!btf_type_is_array(mtype)) | |
5588 | goto error; | |
5589 | ||
5590 | array_elem = (struct btf_array *)(mtype + 1); | |
5591 | if (array_elem->nelems != 0) | |
5592 | goto error; | |
5593 | ||
8293eb99 | 5594 | moff = __btf_member_bit_offset(t, member) / 8; |
9c5f8a10 YS |
5595 | if (off < moff) |
5596 | goto error; | |
5597 | ||
5598 | /* Only allow structure for now, can be relaxed for | |
5599 | * other types later. | |
5600 | */ | |
22dc4a0f | 5601 | t = btf_type_skip_modifiers(btf, array_elem->type, |
dafe58fc JO |
5602 | NULL); |
5603 | if (!btf_type_is_struct(t)) | |
9c5f8a10 YS |
5604 | goto error; |
5605 | ||
dafe58fc JO |
5606 | off = (off - moff) % t->size; |
5607 | goto again; | |
9c5f8a10 YS |
5608 | |
5609 | error: | |
976aba00 MKL |
5610 | bpf_log(log, "access beyond struct %s at off %u size %u\n", |
5611 | tname, off, size); | |
5612 | return -EACCES; | |
5613 | } | |
9e15db66 | 5614 | |
976aba00 | 5615 | for_each_member(i, t, member) { |
7e3617a7 | 5616 | /* offset of the field in bytes */ |
8293eb99 | 5617 | moff = __btf_member_bit_offset(t, member) / 8; |
7e3617a7 | 5618 | if (off + size <= moff) |
9e15db66 AS |
5619 | /* won't find anything, field is already too far */ |
5620 | break; | |
976aba00 | 5621 | |
8293eb99 AS |
5622 | if (__btf_member_bitfield_size(t, member)) { |
5623 | u32 end_bit = __btf_member_bit_offset(t, member) + | |
5624 | __btf_member_bitfield_size(t, member); | |
976aba00 MKL |
5625 | |
5626 | /* off <= moff instead of off == moff because clang | |
5627 | * does not generate a BTF member for anonymous | |
5628 | * bitfield like the ":16" here: | |
5629 | * struct { | |
5630 | * int :16; | |
5631 | * int x:8; | |
5632 | * }; | |
5633 | */ | |
5634 | if (off <= moff && | |
5635 | BITS_ROUNDUP_BYTES(end_bit) <= off + size) | |
1c6d28a6 | 5636 | return WALK_SCALAR; |
976aba00 MKL |
5637 | |
5638 | /* off may be accessing a following member | |
5639 | * | |
5640 | * or | |
5641 | * | |
5642 | * Doing partial access at either end of this | |
5643 | * bitfield. Continue on this case also to | |
5644 | * treat it as not accessing this bitfield | |
5645 | * and eventually error out as field not | |
5646 | * found to keep it simple. | |
5647 | * It could be relaxed if there was a legit | |
5648 | * partial access case later. | |
5649 | */ | |
5650 | continue; | |
5651 | } | |
5652 | ||
7e3617a7 MKL |
5653 | /* In case of "off" is pointing to holes of a struct */ |
5654 | if (off < moff) | |
976aba00 | 5655 | break; |
9e15db66 AS |
5656 | |
5657 | /* type of the field */ | |
1c6d28a6 | 5658 | mid = member->type; |
22dc4a0f AN |
5659 | mtype = btf_type_by_id(btf, member->type); |
5660 | mname = __btf_name_by_offset(btf, member->name_off); | |
9e15db66 | 5661 | |
22dc4a0f | 5662 | mtype = __btf_resolve_size(btf, mtype, &msize, |
1c6d28a6 JO |
5663 | &elem_type, &elem_id, &total_nelems, |
5664 | &mid); | |
7e3617a7 | 5665 | if (IS_ERR(mtype)) { |
9e15db66 AS |
5666 | bpf_log(log, "field %s doesn't have size\n", mname); |
5667 | return -EFAULT; | |
5668 | } | |
7e3617a7 MKL |
5669 | |
5670 | mtrue_end = moff + msize; | |
5671 | if (off >= mtrue_end) | |
9e15db66 AS |
5672 | /* no overlap with member, keep iterating */ |
5673 | continue; | |
7e3617a7 MKL |
5674 | |
5675 | if (btf_type_is_array(mtype)) { | |
5676 | u32 elem_idx; | |
5677 | ||
6298399b | 5678 | /* __btf_resolve_size() above helps to |
7e3617a7 MKL |
5679 | * linearize a multi-dimensional array. |
5680 | * | |
5681 | * The logic here is treating an array | |
5682 | * in a struct as the following way: | |
5683 | * | |
5684 | * struct outer { | |
5685 | * struct inner array[2][2]; | |
5686 | * }; | |
5687 | * | |
5688 | * looks like: | |
5689 | * | |
5690 | * struct outer { | |
5691 | * struct inner array_elem0; | |
5692 | * struct inner array_elem1; | |
5693 | * struct inner array_elem2; | |
5694 | * struct inner array_elem3; | |
5695 | * }; | |
5696 | * | |
5697 | * When accessing outer->array[1][0], it moves | |
5698 | * moff to "array_elem2", set mtype to | |
5699 | * "struct inner", and msize also becomes | |
5700 | * sizeof(struct inner). Then most of the | |
5701 | * remaining logic will fall through without | |
5702 | * caring the current member is an array or | |
5703 | * not. | |
5704 | * | |
5705 | * Unlike mtype/msize/moff, mtrue_end does not | |
5706 | * change. The naming difference ("_true") tells | |
5707 | * that it is not always corresponding to | |
5708 | * the current mtype/msize/moff. | |
5709 | * It is the true end of the current | |
5710 | * member (i.e. array in this case). That | |
5711 | * will allow an int array to be accessed like | |
5712 | * a scratch space, | |
5713 | * i.e. allow access beyond the size of | |
5714 | * the array's element as long as it is | |
5715 | * within the mtrue_end boundary. | |
5716 | */ | |
5717 | ||
5718 | /* skip empty array */ | |
5719 | if (moff == mtrue_end) | |
5720 | continue; | |
5721 | ||
5722 | msize /= total_nelems; | |
5723 | elem_idx = (off - moff) / msize; | |
5724 | moff += elem_idx * msize; | |
5725 | mtype = elem_type; | |
1c6d28a6 | 5726 | mid = elem_id; |
7e3617a7 MKL |
5727 | } |
5728 | ||
9e15db66 AS |
5729 | /* the 'off' we're looking for is either equal to start |
5730 | * of this field or inside of this struct | |
5731 | */ | |
5732 | if (btf_type_is_struct(mtype)) { | |
5733 | /* our field must be inside that union or struct */ | |
5734 | t = mtype; | |
5735 | ||
1c6d28a6 JO |
5736 | /* return if the offset matches the member offset */ |
5737 | if (off == moff) { | |
5738 | *next_btf_id = mid; | |
5739 | return WALK_STRUCT; | |
5740 | } | |
5741 | ||
9e15db66 | 5742 | /* adjust offset we're looking for */ |
7e3617a7 | 5743 | off -= moff; |
9e15db66 AS |
5744 | goto again; |
5745 | } | |
9e15db66 AS |
5746 | |
5747 | if (btf_type_is_ptr(mtype)) { | |
c6f1bfe8 YS |
5748 | const struct btf_type *stype, *t; |
5749 | enum bpf_type_flag tmp_flag = 0; | |
257af63d | 5750 | u32 id; |
9e15db66 | 5751 | |
7e3617a7 MKL |
5752 | if (msize != size || off != moff) { |
5753 | bpf_log(log, | |
5754 | "cannot access ptr member %s with moff %u in struct %s with off %u size %u\n", | |
5755 | mname, moff, tname, off, size); | |
5756 | return -EACCES; | |
5757 | } | |
c6f1bfe8 | 5758 | |
5844101a | 5759 | /* check type tag */ |
c6f1bfe8 YS |
5760 | t = btf_type_by_id(btf, mtype->type); |
5761 | if (btf_type_is_type_tag(t)) { | |
5762 | tag_value = __btf_name_by_offset(btf, t->name_off); | |
5844101a | 5763 | /* check __user tag */ |
c6f1bfe8 YS |
5764 | if (strcmp(tag_value, "user") == 0) |
5765 | tmp_flag = MEM_USER; | |
5844101a HL |
5766 | /* check __percpu tag */ |
5767 | if (strcmp(tag_value, "percpu") == 0) | |
5768 | tmp_flag = MEM_PERCPU; | |
c6f1bfe8 YS |
5769 | } |
5770 | ||
22dc4a0f | 5771 | stype = btf_type_skip_modifiers(btf, mtype->type, &id); |
9e15db66 | 5772 | if (btf_type_is_struct(stype)) { |
257af63d | 5773 | *next_btf_id = id; |
c6f1bfe8 | 5774 | *flag = tmp_flag; |
1c6d28a6 | 5775 | return WALK_PTR; |
9e15db66 AS |
5776 | } |
5777 | } | |
7e3617a7 MKL |
5778 | |
5779 | /* Allow more flexible access within an int as long as | |
5780 | * it is within mtrue_end. | |
5781 | * Since mtrue_end could be the end of an array, | |
5782 | * that also allows using an array of int as a scratch | |
5783 | * space. e.g. skb->cb[]. | |
5784 | */ | |
5785 | if (off + size > mtrue_end) { | |
5786 | bpf_log(log, | |
5787 | "access beyond the end of member %s (mend:%u) in struct %s with off %u size %u\n", | |
5788 | mname, mtrue_end, tname, off, size); | |
5789 | return -EACCES; | |
5790 | } | |
5791 | ||
1c6d28a6 | 5792 | return WALK_SCALAR; |
9e15db66 AS |
5793 | } |
5794 | bpf_log(log, "struct %s doesn't have field at offset %d\n", tname, off); | |
5795 | return -EINVAL; | |
5796 | } | |
5797 | ||
22dc4a0f | 5798 | int btf_struct_access(struct bpf_verifier_log *log, const struct btf *btf, |
1c6d28a6 JO |
5799 | const struct btf_type *t, int off, int size, |
5800 | enum bpf_access_type atype __maybe_unused, | |
c6f1bfe8 | 5801 | u32 *next_btf_id, enum bpf_type_flag *flag) |
1c6d28a6 | 5802 | { |
c6f1bfe8 | 5803 | enum bpf_type_flag tmp_flag = 0; |
1c6d28a6 JO |
5804 | int err; |
5805 | u32 id; | |
5806 | ||
5807 | do { | |
c6f1bfe8 | 5808 | err = btf_struct_walk(log, btf, t, off, size, &id, &tmp_flag); |
1c6d28a6 JO |
5809 | |
5810 | switch (err) { | |
5811 | case WALK_PTR: | |
5812 | /* If we found the pointer or scalar on t+off, | |
5813 | * we're done. | |
5814 | */ | |
5815 | *next_btf_id = id; | |
c6f1bfe8 | 5816 | *flag = tmp_flag; |
1c6d28a6 JO |
5817 | return PTR_TO_BTF_ID; |
5818 | case WALK_SCALAR: | |
5819 | return SCALAR_VALUE; | |
5820 | case WALK_STRUCT: | |
5821 | /* We found nested struct, so continue the search | |
5822 | * by diving in it. At this point the offset is | |
5823 | * aligned with the new type, so set it to 0. | |
5824 | */ | |
22dc4a0f | 5825 | t = btf_type_by_id(btf, id); |
1c6d28a6 JO |
5826 | off = 0; |
5827 | break; | |
5828 | default: | |
5829 | /* It's either error or unknown return value.. | |
5830 | * scream and leave. | |
5831 | */ | |
5832 | if (WARN_ONCE(err > 0, "unknown btf_struct_walk return value")) | |
5833 | return -EINVAL; | |
5834 | return err; | |
5835 | } | |
5836 | } while (t); | |
5837 | ||
5838 | return -EINVAL; | |
5839 | } | |
5840 | ||
22dc4a0f AN |
5841 | /* Check that two BTF types, each specified as an BTF object + id, are exactly |
5842 | * the same. Trivial ID check is not enough due to module BTFs, because we can | |
5843 | * end up with two different module BTFs, but IDs point to the common type in | |
5844 | * vmlinux BTF. | |
5845 | */ | |
5846 | static bool btf_types_are_same(const struct btf *btf1, u32 id1, | |
5847 | const struct btf *btf2, u32 id2) | |
5848 | { | |
5849 | if (id1 != id2) | |
5850 | return false; | |
5851 | if (btf1 == btf2) | |
5852 | return true; | |
5853 | return btf_type_by_id(btf1, id1) == btf_type_by_id(btf2, id2); | |
5854 | } | |
5855 | ||
faaf4a79 | 5856 | bool btf_struct_ids_match(struct bpf_verifier_log *log, |
22dc4a0f | 5857 | const struct btf *btf, u32 id, int off, |
2ab3b380 KKD |
5858 | const struct btf *need_btf, u32 need_type_id, |
5859 | bool strict) | |
faaf4a79 JO |
5860 | { |
5861 | const struct btf_type *type; | |
c6f1bfe8 | 5862 | enum bpf_type_flag flag; |
faaf4a79 JO |
5863 | int err; |
5864 | ||
5865 | /* Are we already done? */ | |
22dc4a0f | 5866 | if (off == 0 && btf_types_are_same(btf, id, need_btf, need_type_id)) |
faaf4a79 | 5867 | return true; |
2ab3b380 KKD |
5868 | /* In case of strict type match, we do not walk struct, the top level |
5869 | * type match must succeed. When strict is true, off should have already | |
5870 | * been 0. | |
5871 | */ | |
5872 | if (strict) | |
5873 | return false; | |
faaf4a79 | 5874 | again: |
22dc4a0f | 5875 | type = btf_type_by_id(btf, id); |
faaf4a79 JO |
5876 | if (!type) |
5877 | return false; | |
c6f1bfe8 | 5878 | err = btf_struct_walk(log, btf, type, off, 1, &id, &flag); |
faaf4a79 JO |
5879 | if (err != WALK_STRUCT) |
5880 | return false; | |
5881 | ||
5882 | /* We found nested struct object. If it matches | |
5883 | * the requested ID, we're done. Otherwise let's | |
5884 | * continue the search with offset 0 in the new | |
5885 | * type. | |
5886 | */ | |
22dc4a0f | 5887 | if (!btf_types_are_same(btf, id, need_btf, need_type_id)) { |
faaf4a79 JO |
5888 | off = 0; |
5889 | goto again; | |
5890 | } | |
5891 | ||
5892 | return true; | |
5893 | } | |
5894 | ||
fec56f58 | 5895 | static int __get_type_size(struct btf *btf, u32 btf_id, |
a00ed843 | 5896 | const struct btf_type **ret_type) |
fec56f58 AS |
5897 | { |
5898 | const struct btf_type *t; | |
5899 | ||
a00ed843 | 5900 | *ret_type = btf_type_by_id(btf, 0); |
fec56f58 AS |
5901 | if (!btf_id) |
5902 | /* void */ | |
5903 | return 0; | |
5904 | t = btf_type_by_id(btf, btf_id); | |
5905 | while (t && btf_type_is_modifier(t)) | |
5906 | t = btf_type_by_id(btf, t->type); | |
a00ed843 | 5907 | if (!t) |
fec56f58 | 5908 | return -EINVAL; |
a00ed843 | 5909 | *ret_type = t; |
fec56f58 AS |
5910 | if (btf_type_is_ptr(t)) |
5911 | /* kernel size of pointer. Not BPF's size of pointer*/ | |
5912 | return sizeof(void *); | |
720e6a43 | 5913 | if (btf_type_is_int(t) || btf_is_any_enum(t) || __btf_type_is_struct(t)) |
fec56f58 | 5914 | return t->size; |
fec56f58 AS |
5915 | return -EINVAL; |
5916 | } | |
5917 | ||
5918 | int btf_distill_func_proto(struct bpf_verifier_log *log, | |
5919 | struct btf *btf, | |
5920 | const struct btf_type *func, | |
5921 | const char *tname, | |
5922 | struct btf_func_model *m) | |
5923 | { | |
5924 | const struct btf_param *args; | |
5925 | const struct btf_type *t; | |
5926 | u32 i, nargs; | |
5927 | int ret; | |
5928 | ||
5b92a28a AS |
5929 | if (!func) { |
5930 | /* BTF function prototype doesn't match the verifier types. | |
523a4cf4 | 5931 | * Fall back to MAX_BPF_FUNC_REG_ARGS u64 args. |
5b92a28a | 5932 | */ |
720e6a43 | 5933 | for (i = 0; i < MAX_BPF_FUNC_REG_ARGS; i++) { |
5b92a28a | 5934 | m->arg_size[i] = 8; |
720e6a43 YS |
5935 | m->arg_flags[i] = 0; |
5936 | } | |
5b92a28a | 5937 | m->ret_size = 8; |
523a4cf4 | 5938 | m->nr_args = MAX_BPF_FUNC_REG_ARGS; |
5b92a28a AS |
5939 | return 0; |
5940 | } | |
fec56f58 AS |
5941 | args = (const struct btf_param *)(func + 1); |
5942 | nargs = btf_type_vlen(func); | |
c29a4920 | 5943 | if (nargs > MAX_BPF_FUNC_ARGS) { |
fec56f58 AS |
5944 | bpf_log(log, |
5945 | "The function %s has %d arguments. Too many.\n", | |
5946 | tname, nargs); | |
5947 | return -EINVAL; | |
5948 | } | |
5949 | ret = __get_type_size(btf, func->type, &t); | |
720e6a43 | 5950 | if (ret < 0 || __btf_type_is_struct(t)) { |
fec56f58 AS |
5951 | bpf_log(log, |
5952 | "The function %s return type %s is unsupported.\n", | |
5953 | tname, btf_kind_str[BTF_INFO_KIND(t->info)]); | |
5954 | return -EINVAL; | |
5955 | } | |
5956 | m->ret_size = ret; | |
5957 | ||
5958 | for (i = 0; i < nargs; i++) { | |
31379397 JO |
5959 | if (i == nargs - 1 && args[i].type == 0) { |
5960 | bpf_log(log, | |
5961 | "The function %s with variable args is unsupported.\n", | |
5962 | tname); | |
5963 | return -EINVAL; | |
5964 | } | |
fec56f58 | 5965 | ret = __get_type_size(btf, args[i].type, &t); |
720e6a43 YS |
5966 | |
5967 | /* No support of struct argument size greater than 16 bytes */ | |
5968 | if (ret < 0 || ret > 16) { | |
fec56f58 AS |
5969 | bpf_log(log, |
5970 | "The function %s arg%d type %s is unsupported.\n", | |
5971 | tname, i, btf_kind_str[BTF_INFO_KIND(t->info)]); | |
5972 | return -EINVAL; | |
5973 | } | |
31379397 JO |
5974 | if (ret == 0) { |
5975 | bpf_log(log, | |
5976 | "The function %s has malformed void argument.\n", | |
5977 | tname); | |
5978 | return -EINVAL; | |
5979 | } | |
fec56f58 | 5980 | m->arg_size[i] = ret; |
720e6a43 | 5981 | m->arg_flags[i] = __btf_type_is_struct(t) ? BTF_FMODEL_STRUCT_ARG : 0; |
fec56f58 AS |
5982 | } |
5983 | m->nr_args = nargs; | |
5984 | return 0; | |
5985 | } | |
5986 | ||
be8704ff AS |
5987 | /* Compare BTFs of two functions assuming only scalars and pointers to context. |
5988 | * t1 points to BTF_KIND_FUNC in btf1 | |
5989 | * t2 points to BTF_KIND_FUNC in btf2 | |
5990 | * Returns: | |
5991 | * EINVAL - function prototype mismatch | |
5992 | * EFAULT - verifier bug | |
5993 | * 0 - 99% match. The last 1% is validated by the verifier. | |
5994 | */ | |
2bf0eb9b HY |
5995 | static int btf_check_func_type_match(struct bpf_verifier_log *log, |
5996 | struct btf *btf1, const struct btf_type *t1, | |
5997 | struct btf *btf2, const struct btf_type *t2) | |
be8704ff AS |
5998 | { |
5999 | const struct btf_param *args1, *args2; | |
6000 | const char *fn1, *fn2, *s1, *s2; | |
6001 | u32 nargs1, nargs2, i; | |
6002 | ||
6003 | fn1 = btf_name_by_offset(btf1, t1->name_off); | |
6004 | fn2 = btf_name_by_offset(btf2, t2->name_off); | |
6005 | ||
6006 | if (btf_func_linkage(t1) != BTF_FUNC_GLOBAL) { | |
6007 | bpf_log(log, "%s() is not a global function\n", fn1); | |
6008 | return -EINVAL; | |
6009 | } | |
6010 | if (btf_func_linkage(t2) != BTF_FUNC_GLOBAL) { | |
6011 | bpf_log(log, "%s() is not a global function\n", fn2); | |
6012 | return -EINVAL; | |
6013 | } | |
6014 | ||
6015 | t1 = btf_type_by_id(btf1, t1->type); | |
6016 | if (!t1 || !btf_type_is_func_proto(t1)) | |
6017 | return -EFAULT; | |
6018 | t2 = btf_type_by_id(btf2, t2->type); | |
6019 | if (!t2 || !btf_type_is_func_proto(t2)) | |
6020 | return -EFAULT; | |
6021 | ||
6022 | args1 = (const struct btf_param *)(t1 + 1); | |
6023 | nargs1 = btf_type_vlen(t1); | |
6024 | args2 = (const struct btf_param *)(t2 + 1); | |
6025 | nargs2 = btf_type_vlen(t2); | |
6026 | ||
6027 | if (nargs1 != nargs2) { | |
6028 | bpf_log(log, "%s() has %d args while %s() has %d args\n", | |
6029 | fn1, nargs1, fn2, nargs2); | |
6030 | return -EINVAL; | |
6031 | } | |
6032 | ||
6033 | t1 = btf_type_skip_modifiers(btf1, t1->type, NULL); | |
6034 | t2 = btf_type_skip_modifiers(btf2, t2->type, NULL); | |
6035 | if (t1->info != t2->info) { | |
6036 | bpf_log(log, | |
6037 | "Return type %s of %s() doesn't match type %s of %s()\n", | |
6038 | btf_type_str(t1), fn1, | |
6039 | btf_type_str(t2), fn2); | |
6040 | return -EINVAL; | |
6041 | } | |
6042 | ||
6043 | for (i = 0; i < nargs1; i++) { | |
6044 | t1 = btf_type_skip_modifiers(btf1, args1[i].type, NULL); | |
6045 | t2 = btf_type_skip_modifiers(btf2, args2[i].type, NULL); | |
6046 | ||
6047 | if (t1->info != t2->info) { | |
6048 | bpf_log(log, "arg%d in %s() is %s while %s() has %s\n", | |
6049 | i, fn1, btf_type_str(t1), | |
6050 | fn2, btf_type_str(t2)); | |
6051 | return -EINVAL; | |
6052 | } | |
6053 | if (btf_type_has_size(t1) && t1->size != t2->size) { | |
6054 | bpf_log(log, | |
6055 | "arg%d in %s() has size %d while %s() has %d\n", | |
6056 | i, fn1, t1->size, | |
6057 | fn2, t2->size); | |
6058 | return -EINVAL; | |
6059 | } | |
6060 | ||
6061 | /* global functions are validated with scalars and pointers | |
6062 | * to context only. And only global functions can be replaced. | |
6063 | * Hence type check only those types. | |
6064 | */ | |
6089fb32 | 6065 | if (btf_type_is_int(t1) || btf_is_any_enum(t1)) |
be8704ff AS |
6066 | continue; |
6067 | if (!btf_type_is_ptr(t1)) { | |
6068 | bpf_log(log, | |
6069 | "arg%d in %s() has unrecognized type\n", | |
6070 | i, fn1); | |
6071 | return -EINVAL; | |
6072 | } | |
6073 | t1 = btf_type_skip_modifiers(btf1, t1->type, NULL); | |
6074 | t2 = btf_type_skip_modifiers(btf2, t2->type, NULL); | |
6075 | if (!btf_type_is_struct(t1)) { | |
6076 | bpf_log(log, | |
6077 | "arg%d in %s() is not a pointer to context\n", | |
6078 | i, fn1); | |
6079 | return -EINVAL; | |
6080 | } | |
6081 | if (!btf_type_is_struct(t2)) { | |
6082 | bpf_log(log, | |
6083 | "arg%d in %s() is not a pointer to context\n", | |
6084 | i, fn2); | |
6085 | return -EINVAL; | |
6086 | } | |
6087 | /* This is an optional check to make program writing easier. | |
6088 | * Compare names of structs and report an error to the user. | |
6089 | * btf_prepare_func_args() already checked that t2 struct | |
6090 | * is a context type. btf_prepare_func_args() will check | |
6091 | * later that t1 struct is a context type as well. | |
6092 | */ | |
6093 | s1 = btf_name_by_offset(btf1, t1->name_off); | |
6094 | s2 = btf_name_by_offset(btf2, t2->name_off); | |
6095 | if (strcmp(s1, s2)) { | |
6096 | bpf_log(log, | |
6097 | "arg%d %s(struct %s *) doesn't match %s(struct %s *)\n", | |
6098 | i, fn1, s1, fn2, s2); | |
6099 | return -EINVAL; | |
6100 | } | |
6101 | } | |
6102 | return 0; | |
6103 | } | |
6104 | ||
6105 | /* Compare BTFs of given program with BTF of target program */ | |
efc68158 | 6106 | int btf_check_type_match(struct bpf_verifier_log *log, const struct bpf_prog *prog, |
be8704ff AS |
6107 | struct btf *btf2, const struct btf_type *t2) |
6108 | { | |
6109 | struct btf *btf1 = prog->aux->btf; | |
6110 | const struct btf_type *t1; | |
6111 | u32 btf_id = 0; | |
6112 | ||
6113 | if (!prog->aux->func_info) { | |
efc68158 | 6114 | bpf_log(log, "Program extension requires BTF\n"); |
be8704ff AS |
6115 | return -EINVAL; |
6116 | } | |
6117 | ||
6118 | btf_id = prog->aux->func_info[0].type_id; | |
6119 | if (!btf_id) | |
6120 | return -EFAULT; | |
6121 | ||
6122 | t1 = btf_type_by_id(btf1, btf_id); | |
6123 | if (!t1 || !btf_type_is_func(t1)) | |
6124 | return -EFAULT; | |
6125 | ||
efc68158 | 6126 | return btf_check_func_type_match(log, btf1, t1, btf2, t2); |
be8704ff AS |
6127 | } |
6128 | ||
e6ac2450 MKL |
6129 | static u32 *reg2btf_ids[__BPF_REG_TYPE_MAX] = { |
6130 | #ifdef CONFIG_NET | |
6131 | [PTR_TO_SOCKET] = &btf_sock_ids[BTF_SOCK_TYPE_SOCK], | |
6132 | [PTR_TO_SOCK_COMMON] = &btf_sock_ids[BTF_SOCK_TYPE_SOCK_COMMON], | |
6133 | [PTR_TO_TCP_SOCK] = &btf_sock_ids[BTF_SOCK_TYPE_TCP], | |
6134 | #endif | |
6135 | }; | |
6136 | ||
3363bd0c KKD |
6137 | /* Returns true if struct is composed of scalars, 4 levels of nesting allowed */ |
6138 | static bool __btf_type_is_scalar_struct(struct bpf_verifier_log *log, | |
6139 | const struct btf *btf, | |
6140 | const struct btf_type *t, int rec) | |
6141 | { | |
6142 | const struct btf_type *member_type; | |
6143 | const struct btf_member *member; | |
6144 | u32 i; | |
6145 | ||
6146 | if (!btf_type_is_struct(t)) | |
6147 | return false; | |
6148 | ||
6149 | for_each_member(i, t, member) { | |
6150 | const struct btf_array *array; | |
6151 | ||
6152 | member_type = btf_type_skip_modifiers(btf, member->type, NULL); | |
6153 | if (btf_type_is_struct(member_type)) { | |
6154 | if (rec >= 3) { | |
6155 | bpf_log(log, "max struct nesting depth exceeded\n"); | |
6156 | return false; | |
6157 | } | |
6158 | if (!__btf_type_is_scalar_struct(log, btf, member_type, rec + 1)) | |
6159 | return false; | |
6160 | continue; | |
6161 | } | |
6162 | if (btf_type_is_array(member_type)) { | |
6163 | array = btf_type_array(member_type); | |
6164 | if (!array->nelems) | |
6165 | return false; | |
6166 | member_type = btf_type_skip_modifiers(btf, array->type, NULL); | |
6167 | if (!btf_type_is_scalar(member_type)) | |
6168 | return false; | |
6169 | continue; | |
6170 | } | |
6171 | if (!btf_type_is_scalar(member_type)) | |
6172 | return false; | |
6173 | } | |
6174 | return true; | |
6175 | } | |
6176 | ||
d583691c KKD |
6177 | static bool is_kfunc_arg_mem_size(const struct btf *btf, |
6178 | const struct btf_param *arg, | |
6179 | const struct bpf_reg_state *reg) | |
6180 | { | |
6181 | int len, sfx_len = sizeof("__sz") - 1; | |
6182 | const struct btf_type *t; | |
6183 | const char *param_name; | |
6184 | ||
6185 | t = btf_type_skip_modifiers(btf, arg->type, NULL); | |
6186 | if (!btf_type_is_scalar(t) || reg->type != SCALAR_VALUE) | |
6187 | return false; | |
6188 | ||
6189 | /* In the future, this can be ported to use BTF tagging */ | |
6190 | param_name = btf_name_by_offset(btf, arg->name_off); | |
6191 | if (str_is_empty(param_name)) | |
6192 | return false; | |
6193 | len = strlen(param_name); | |
6194 | if (len < sfx_len) | |
6195 | return false; | |
6196 | param_name += len - sfx_len; | |
6197 | if (strncmp(param_name, "__sz", sfx_len)) | |
6198 | return false; | |
6199 | ||
6200 | return true; | |
6201 | } | |
6202 | ||
eb1f7f71 BT |
6203 | static bool btf_is_kfunc_arg_mem_size(const struct btf *btf, |
6204 | const struct btf_param *arg, | |
6205 | const struct bpf_reg_state *reg, | |
6206 | const char *name) | |
6207 | { | |
6208 | int len, target_len = strlen(name); | |
6209 | const struct btf_type *t; | |
6210 | const char *param_name; | |
6211 | ||
6212 | t = btf_type_skip_modifiers(btf, arg->type, NULL); | |
6213 | if (!btf_type_is_scalar(t) || reg->type != SCALAR_VALUE) | |
6214 | return false; | |
6215 | ||
6216 | param_name = btf_name_by_offset(btf, arg->name_off); | |
6217 | if (str_is_empty(param_name)) | |
6218 | return false; | |
6219 | len = strlen(param_name); | |
6220 | if (len != target_len) | |
6221 | return false; | |
6222 | if (strcmp(param_name, name)) | |
6223 | return false; | |
6224 | ||
6225 | return true; | |
6226 | } | |
6227 | ||
34747c41 MKL |
6228 | static int btf_check_func_arg_match(struct bpf_verifier_env *env, |
6229 | const struct btf *btf, u32 func_id, | |
6230 | struct bpf_reg_state *regs, | |
a4703e31 | 6231 | bool ptr_to_mem_ok, |
eb1f7f71 | 6232 | struct bpf_kfunc_arg_meta *kfunc_meta, |
95f2f26f | 6233 | bool processing_call) |
8c1b6e69 | 6234 | { |
f858c2b2 | 6235 | enum bpf_prog_type prog_type = resolve_prog_type(env->prog); |
56e948ff | 6236 | bool rel = false, kptr_get = false, trusted_arg = false; |
fa96b242 | 6237 | bool sleepable = false; |
8c1b6e69 | 6238 | struct bpf_verifier_log *log = &env->log; |
5c073f26 | 6239 | u32 i, nargs, ref_id, ref_obj_id = 0; |
3363bd0c | 6240 | bool is_kfunc = btf_is_kernel(btf); |
34747c41 | 6241 | const char *func_name, *ref_tname; |
e5069b9c | 6242 | const struct btf_type *t, *ref_t; |
34747c41 | 6243 | const struct btf_param *args; |
655efe50 | 6244 | int ref_regno = 0, ret; |
8c1b6e69 | 6245 | |
34747c41 | 6246 | t = btf_type_by_id(btf, func_id); |
8c1b6e69 | 6247 | if (!t || !btf_type_is_func(t)) { |
51c39bb1 | 6248 | /* These checks were already done by the verifier while loading |
e6ac2450 | 6249 | * struct bpf_func_info or in add_kfunc_call(). |
51c39bb1 | 6250 | */ |
34747c41 MKL |
6251 | bpf_log(log, "BTF of func_id %u doesn't point to KIND_FUNC\n", |
6252 | func_id); | |
51c39bb1 | 6253 | return -EFAULT; |
8c1b6e69 | 6254 | } |
34747c41 | 6255 | func_name = btf_name_by_offset(btf, t->name_off); |
8c1b6e69 AS |
6256 | |
6257 | t = btf_type_by_id(btf, t->type); | |
6258 | if (!t || !btf_type_is_func_proto(t)) { | |
34747c41 | 6259 | bpf_log(log, "Invalid BTF of func %s\n", func_name); |
51c39bb1 | 6260 | return -EFAULT; |
8c1b6e69 AS |
6261 | } |
6262 | args = (const struct btf_param *)(t + 1); | |
6263 | nargs = btf_type_vlen(t); | |
523a4cf4 | 6264 | if (nargs > MAX_BPF_FUNC_REG_ARGS) { |
34747c41 | 6265 | bpf_log(log, "Function %s has %d > %d args\n", func_name, nargs, |
523a4cf4 | 6266 | MAX_BPF_FUNC_REG_ARGS); |
34747c41 | 6267 | return -EINVAL; |
8c1b6e69 | 6268 | } |
e5069b9c | 6269 | |
eb1f7f71 | 6270 | if (is_kfunc && kfunc_meta) { |
a1ef1959 | 6271 | /* Only kfunc can be release func */ |
eb1f7f71 BT |
6272 | rel = kfunc_meta->flags & KF_RELEASE; |
6273 | kptr_get = kfunc_meta->flags & KF_KPTR_GET; | |
6274 | trusted_arg = kfunc_meta->flags & KF_TRUSTED_ARGS; | |
6275 | sleepable = kfunc_meta->flags & KF_SLEEPABLE; | |
a1ef1959 KKD |
6276 | } |
6277 | ||
8c1b6e69 AS |
6278 | /* check that BTF function arguments match actual types that the |
6279 | * verifier sees. | |
6280 | */ | |
6281 | for (i = 0; i < nargs; i++) { | |
8f14852e | 6282 | enum bpf_arg_type arg_type = ARG_DONTCARE; |
34747c41 MKL |
6283 | u32 regno = i + 1; |
6284 | struct bpf_reg_state *reg = ®s[regno]; | |
feb4adfa | 6285 | |
34747c41 MKL |
6286 | t = btf_type_skip_modifiers(btf, args[i].type, NULL); |
6287 | if (btf_type_is_scalar(t)) { | |
eb1f7f71 BT |
6288 | if (is_kfunc && kfunc_meta) { |
6289 | bool is_buf_size = false; | |
6290 | ||
6291 | /* check for any const scalar parameter of name "rdonly_buf_size" | |
6292 | * or "rdwr_buf_size" | |
6293 | */ | |
6294 | if (btf_is_kfunc_arg_mem_size(btf, &args[i], reg, | |
6295 | "rdonly_buf_size")) { | |
6296 | kfunc_meta->r0_rdonly = true; | |
6297 | is_buf_size = true; | |
6298 | } else if (btf_is_kfunc_arg_mem_size(btf, &args[i], reg, | |
6299 | "rdwr_buf_size")) | |
6300 | is_buf_size = true; | |
6301 | ||
6302 | if (is_buf_size) { | |
6303 | if (kfunc_meta->r0_size) { | |
6304 | bpf_log(log, "2 or more rdonly/rdwr_buf_size parameters for kfunc"); | |
6305 | return -EINVAL; | |
6306 | } | |
6307 | ||
6308 | if (!tnum_is_const(reg->var_off)) { | |
6309 | bpf_log(log, "R%d is not a const\n", regno); | |
6310 | return -EINVAL; | |
6311 | } | |
6312 | ||
6313 | kfunc_meta->r0_size = reg->var_off.value; | |
6314 | ret = mark_chain_precision(env, regno); | |
6315 | if (ret) | |
6316 | return ret; | |
6317 | } | |
6318 | } | |
6319 | ||
feb4adfa | 6320 | if (reg->type == SCALAR_VALUE) |
8c1b6e69 | 6321 | continue; |
34747c41 MKL |
6322 | bpf_log(log, "R%d is not a scalar\n", regno); |
6323 | return -EINVAL; | |
8c1b6e69 | 6324 | } |
34747c41 MKL |
6325 | |
6326 | if (!btf_type_is_ptr(t)) { | |
6327 | bpf_log(log, "Unrecognized arg#%d type %s\n", | |
6328 | i, btf_type_str(t)); | |
6329 | return -EINVAL; | |
6330 | } | |
6331 | ||
56e948ff KKD |
6332 | /* Check if argument must be a referenced pointer, args + i has |
6333 | * been verified to be a pointer (after skipping modifiers). | |
6334 | */ | |
6335 | if (is_kfunc && trusted_arg && !reg->ref_obj_id) { | |
6336 | bpf_log(log, "R%d must be referenced\n", regno); | |
6337 | return -EINVAL; | |
6338 | } | |
6339 | ||
e6ac2450 | 6340 | ref_t = btf_type_skip_modifiers(btf, t->type, &ref_id); |
34747c41 | 6341 | ref_tname = btf_name_by_offset(btf, ref_t->name_off); |
655efe50 | 6342 | |
56e948ff KKD |
6343 | /* Trusted args have the same offset checks as release arguments */ |
6344 | if (trusted_arg || (rel && reg->ref_obj_id)) | |
8f14852e KKD |
6345 | arg_type |= OBJ_RELEASE; |
6346 | ret = check_func_arg_reg_off(env, reg, regno, arg_type); | |
655efe50 KKD |
6347 | if (ret < 0) |
6348 | return ret; | |
6349 | ||
eb1f7f71 BT |
6350 | if (is_kfunc && reg->ref_obj_id) { |
6351 | /* Ensure only one argument is referenced PTR_TO_BTF_ID */ | |
6352 | if (ref_obj_id) { | |
6353 | bpf_log(log, "verifier internal error: more than one arg with ref_obj_id R%d %u %u\n", | |
6354 | regno, reg->ref_obj_id, ref_obj_id); | |
6355 | return -EFAULT; | |
6356 | } | |
6357 | ref_regno = regno; | |
6358 | ref_obj_id = reg->ref_obj_id; | |
6359 | } | |
6360 | ||
a1ef1959 KKD |
6361 | /* kptr_get is only true for kfunc */ |
6362 | if (i == 0 && kptr_get) { | |
6363 | struct bpf_map_value_off_desc *off_desc; | |
6364 | ||
6365 | if (reg->type != PTR_TO_MAP_VALUE) { | |
6366 | bpf_log(log, "arg#0 expected pointer to map value\n"); | |
6367 | return -EINVAL; | |
6368 | } | |
6369 | ||
6370 | /* check_func_arg_reg_off allows var_off for | |
6371 | * PTR_TO_MAP_VALUE, but we need fixed offset to find | |
6372 | * off_desc. | |
6373 | */ | |
6374 | if (!tnum_is_const(reg->var_off)) { | |
6375 | bpf_log(log, "arg#0 must have constant offset\n"); | |
6376 | return -EINVAL; | |
6377 | } | |
6378 | ||
6379 | off_desc = bpf_map_kptr_off_contains(reg->map_ptr, reg->off + reg->var_off.value); | |
6380 | if (!off_desc || off_desc->type != BPF_KPTR_REF) { | |
6381 | bpf_log(log, "arg#0 no referenced kptr at map value offset=%llu\n", | |
6382 | reg->off + reg->var_off.value); | |
6383 | return -EINVAL; | |
6384 | } | |
6385 | ||
6386 | if (!btf_type_is_ptr(ref_t)) { | |
6387 | bpf_log(log, "arg#0 BTF type must be a double pointer\n"); | |
6388 | return -EINVAL; | |
6389 | } | |
6390 | ||
6391 | ref_t = btf_type_skip_modifiers(btf, ref_t->type, &ref_id); | |
6392 | ref_tname = btf_name_by_offset(btf, ref_t->name_off); | |
6393 | ||
6394 | if (!btf_type_is_struct(ref_t)) { | |
6395 | bpf_log(log, "kernel function %s args#%d pointer type %s %s is not supported\n", | |
6396 | func_name, i, btf_type_str(ref_t), ref_tname); | |
6397 | return -EINVAL; | |
6398 | } | |
6399 | if (!btf_struct_ids_match(log, btf, ref_id, 0, off_desc->kptr.btf, | |
2ab3b380 | 6400 | off_desc->kptr.btf_id, true)) { |
a1ef1959 KKD |
6401 | bpf_log(log, "kernel function %s args#%d expected pointer to %s %s\n", |
6402 | func_name, i, btf_type_str(ref_t), ref_tname); | |
6403 | return -EINVAL; | |
6404 | } | |
6405 | /* rest of the arguments can be anything, like normal kfunc */ | |
f858c2b2 | 6406 | } else if (btf_get_prog_ctx_type(log, btf, t, prog_type, i)) { |
3363bd0c KKD |
6407 | /* If function expects ctx type in BTF check that caller |
6408 | * is passing PTR_TO_CTX. | |
6409 | */ | |
6410 | if (reg->type != PTR_TO_CTX) { | |
6411 | bpf_log(log, | |
6412 | "arg#%d expected pointer to ctx, but got %s\n", | |
6413 | i, btf_type_str(t)); | |
6414 | return -EINVAL; | |
6415 | } | |
45ce4b4f KKD |
6416 | } else if (is_kfunc && (reg->type == PTR_TO_BTF_ID || |
6417 | (reg2btf_ids[base_type(reg->type)] && !type_flag(reg->type)))) { | |
e6ac2450 MKL |
6418 | const struct btf_type *reg_ref_t; |
6419 | const struct btf *reg_btf; | |
6420 | const char *reg_ref_tname; | |
6421 | u32 reg_ref_id; | |
6422 | ||
6423 | if (!btf_type_is_struct(ref_t)) { | |
6424 | bpf_log(log, "kernel function %s args#%d pointer type %s %s is not supported\n", | |
6425 | func_name, i, btf_type_str(ref_t), | |
6426 | ref_tname); | |
6427 | return -EINVAL; | |
6428 | } | |
6429 | ||
6430 | if (reg->type == PTR_TO_BTF_ID) { | |
6431 | reg_btf = reg->btf; | |
6432 | reg_ref_id = reg->btf_id; | |
3363bd0c | 6433 | } else { |
e6ac2450 | 6434 | reg_btf = btf_vmlinux; |
45ce4b4f | 6435 | reg_ref_id = *reg2btf_ids[base_type(reg->type)]; |
e6ac2450 MKL |
6436 | } |
6437 | ||
6438 | reg_ref_t = btf_type_skip_modifiers(reg_btf, reg_ref_id, | |
6439 | ®_ref_id); | |
6440 | reg_ref_tname = btf_name_by_offset(reg_btf, | |
6441 | reg_ref_t->name_off); | |
6442 | if (!btf_struct_ids_match(log, reg_btf, reg_ref_id, | |
56e948ff KKD |
6443 | reg->off, btf, ref_id, |
6444 | trusted_arg || (rel && reg->ref_obj_id))) { | |
e6ac2450 MKL |
6445 | bpf_log(log, "kernel function %s args#%d expected pointer to %s %s but R%d has a pointer to %s %s\n", |
6446 | func_name, i, | |
6447 | btf_type_str(ref_t), ref_tname, | |
6448 | regno, btf_type_str(reg_ref_t), | |
6449 | reg_ref_tname); | |
6450 | return -EINVAL; | |
6451 | } | |
95f2f26f | 6452 | } else if (ptr_to_mem_ok && processing_call) { |
34747c41 MKL |
6453 | const struct btf_type *resolve_ret; |
6454 | u32 type_size; | |
e5069b9c | 6455 | |
3363bd0c | 6456 | if (is_kfunc) { |
d583691c KKD |
6457 | bool arg_mem_size = i + 1 < nargs && is_kfunc_arg_mem_size(btf, &args[i + 1], ®s[regno + 1]); |
6458 | ||
3363bd0c KKD |
6459 | /* Permit pointer to mem, but only when argument |
6460 | * type is pointer to scalar, or struct composed | |
6461 | * (recursively) of scalars. | |
d583691c KKD |
6462 | * When arg_mem_size is true, the pointer can be |
6463 | * void *. | |
3363bd0c KKD |
6464 | */ |
6465 | if (!btf_type_is_scalar(ref_t) && | |
d583691c KKD |
6466 | !__btf_type_is_scalar_struct(log, btf, ref_t, 0) && |
6467 | (arg_mem_size ? !btf_type_is_void(ref_t) : 1)) { | |
3363bd0c | 6468 | bpf_log(log, |
d583691c KKD |
6469 | "arg#%d pointer type %s %s must point to %sscalar, or struct with scalar\n", |
6470 | i, btf_type_str(ref_t), ref_tname, arg_mem_size ? "void, " : ""); | |
3363bd0c KKD |
6471 | return -EINVAL; |
6472 | } | |
d583691c KKD |
6473 | |
6474 | /* Check for mem, len pair */ | |
6475 | if (arg_mem_size) { | |
6476 | if (check_kfunc_mem_size_reg(env, ®s[regno + 1], regno + 1)) { | |
6477 | bpf_log(log, "arg#%d arg#%d memory, len pair leads to invalid memory access\n", | |
6478 | i, i + 1); | |
6479 | return -EINVAL; | |
6480 | } | |
6481 | i++; | |
6482 | continue; | |
6483 | } | |
3363bd0c KKD |
6484 | } |
6485 | ||
34747c41 MKL |
6486 | resolve_ret = btf_resolve_size(btf, ref_t, &type_size); |
6487 | if (IS_ERR(resolve_ret)) { | |
e5069b9c | 6488 | bpf_log(log, |
34747c41 MKL |
6489 | "arg#%d reference type('%s %s') size cannot be determined: %ld\n", |
6490 | i, btf_type_str(ref_t), ref_tname, | |
6491 | PTR_ERR(resolve_ret)); | |
6492 | return -EINVAL; | |
e5069b9c DB |
6493 | } |
6494 | ||
34747c41 MKL |
6495 | if (check_mem_reg(env, reg, regno, type_size)) |
6496 | return -EINVAL; | |
6497 | } else { | |
3363bd0c KKD |
6498 | bpf_log(log, "reg type unsupported for arg#%d %sfunction %s#%d\n", i, |
6499 | is_kfunc ? "kernel " : "", func_name, func_id); | |
34747c41 | 6500 | return -EINVAL; |
8c1b6e69 | 6501 | } |
8c1b6e69 | 6502 | } |
34747c41 | 6503 | |
5c073f26 KKD |
6504 | /* Either both are set, or neither */ |
6505 | WARN_ON_ONCE((ref_obj_id && !ref_regno) || (!ref_obj_id && ref_regno)); | |
24d5bb80 KKD |
6506 | /* We already made sure ref_obj_id is set only for one argument. We do |
6507 | * allow (!rel && ref_obj_id), so that passing such referenced | |
6508 | * PTR_TO_BTF_ID to other kfuncs works. Note that rel is only true when | |
6509 | * is_kfunc is true. | |
6510 | */ | |
6511 | if (rel && !ref_obj_id) { | |
6512 | bpf_log(log, "release kernel function %s expects refcounted PTR_TO_BTF_ID\n", | |
6513 | func_name); | |
6514 | return -EINVAL; | |
5c073f26 | 6515 | } |
fa96b242 BT |
6516 | |
6517 | if (sleepable && !env->prog->aux->sleepable) { | |
6518 | bpf_log(log, "kernel function %s is sleepable but the program is not\n", | |
6519 | func_name); | |
6520 | return -EINVAL; | |
6521 | } | |
6522 | ||
eb1f7f71 BT |
6523 | if (kfunc_meta && ref_obj_id) |
6524 | kfunc_meta->ref_obj_id = ref_obj_id; | |
6525 | ||
5c073f26 KKD |
6526 | /* returns argument register number > 0 in case of reference release kfunc */ |
6527 | return rel ? ref_regno : 0; | |
34747c41 MKL |
6528 | } |
6529 | ||
95f2f26f | 6530 | /* Compare BTF of a function declaration with given bpf_reg_state. |
34747c41 MKL |
6531 | * Returns: |
6532 | * EFAULT - there is a verifier bug. Abort verification. | |
6533 | * EINVAL - there is a type mismatch or BTF is not available. | |
6534 | * 0 - BTF matches with what bpf_reg_state expects. | |
6535 | * Only PTR_TO_CTX and SCALAR_VALUE states are recognized. | |
6536 | */ | |
6537 | int btf_check_subprog_arg_match(struct bpf_verifier_env *env, int subprog, | |
6538 | struct bpf_reg_state *regs) | |
6539 | { | |
6540 | struct bpf_prog *prog = env->prog; | |
6541 | struct btf *btf = prog->aux->btf; | |
6542 | bool is_global; | |
6543 | u32 btf_id; | |
6544 | int err; | |
6545 | ||
6546 | if (!prog->aux->func_info) | |
6547 | return -EINVAL; | |
6548 | ||
6549 | btf_id = prog->aux->func_info[subprog].type_id; | |
6550 | if (!btf_id) | |
6551 | return -EFAULT; | |
6552 | ||
6553 | if (prog->aux->func_info_aux[subprog].unreliable) | |
6554 | return -EINVAL; | |
6555 | ||
6556 | is_global = prog->aux->func_info_aux[subprog].linkage == BTF_FUNC_GLOBAL; | |
eb1f7f71 | 6557 | err = btf_check_func_arg_match(env, btf, btf_id, regs, is_global, NULL, false); |
95f2f26f BT |
6558 | |
6559 | /* Compiler optimizations can remove arguments from static functions | |
6560 | * or mismatched type can be passed into a global function. | |
6561 | * In such cases mark the function as unreliable from BTF point of view. | |
6562 | */ | |
6563 | if (err) | |
6564 | prog->aux->func_info_aux[subprog].unreliable = true; | |
6565 | return err; | |
6566 | } | |
6567 | ||
6568 | /* Compare BTF of a function call with given bpf_reg_state. | |
6569 | * Returns: | |
6570 | * EFAULT - there is a verifier bug. Abort verification. | |
6571 | * EINVAL - there is a type mismatch or BTF is not available. | |
6572 | * 0 - BTF matches with what bpf_reg_state expects. | |
6573 | * Only PTR_TO_CTX and SCALAR_VALUE states are recognized. | |
6574 | * | |
6575 | * NOTE: the code is duplicated from btf_check_subprog_arg_match() | |
6576 | * because btf_check_func_arg_match() is still doing both. Once that | |
6577 | * function is split in 2, we can call from here btf_check_subprog_arg_match() | |
6578 | * first, and then treat the calling part in a new code path. | |
6579 | */ | |
6580 | int btf_check_subprog_call(struct bpf_verifier_env *env, int subprog, | |
6581 | struct bpf_reg_state *regs) | |
6582 | { | |
6583 | struct bpf_prog *prog = env->prog; | |
6584 | struct btf *btf = prog->aux->btf; | |
6585 | bool is_global; | |
6586 | u32 btf_id; | |
6587 | int err; | |
6588 | ||
6589 | if (!prog->aux->func_info) | |
6590 | return -EINVAL; | |
6591 | ||
6592 | btf_id = prog->aux->func_info[subprog].type_id; | |
6593 | if (!btf_id) | |
6594 | return -EFAULT; | |
6595 | ||
6596 | if (prog->aux->func_info_aux[subprog].unreliable) | |
6597 | return -EINVAL; | |
6598 | ||
6599 | is_global = prog->aux->func_info_aux[subprog].linkage == BTF_FUNC_GLOBAL; | |
eb1f7f71 | 6600 | err = btf_check_func_arg_match(env, btf, btf_id, regs, is_global, NULL, true); |
34747c41 | 6601 | |
51c39bb1 AS |
6602 | /* Compiler optimizations can remove arguments from static functions |
6603 | * or mismatched type can be passed into a global function. | |
6604 | * In such cases mark the function as unreliable from BTF point of view. | |
6605 | */ | |
34747c41 MKL |
6606 | if (err) |
6607 | prog->aux->func_info_aux[subprog].unreliable = true; | |
6608 | return err; | |
51c39bb1 AS |
6609 | } |
6610 | ||
e6ac2450 MKL |
6611 | int btf_check_kfunc_arg_match(struct bpf_verifier_env *env, |
6612 | const struct btf *btf, u32 func_id, | |
a4703e31 | 6613 | struct bpf_reg_state *regs, |
eb1f7f71 | 6614 | struct bpf_kfunc_arg_meta *meta) |
e6ac2450 | 6615 | { |
eb1f7f71 | 6616 | return btf_check_func_arg_match(env, btf, func_id, regs, true, meta, true); |
e6ac2450 MKL |
6617 | } |
6618 | ||
51c39bb1 AS |
6619 | /* Convert BTF of a function into bpf_reg_state if possible |
6620 | * Returns: | |
6621 | * EFAULT - there is a verifier bug. Abort verification. | |
6622 | * EINVAL - cannot convert BTF. | |
6623 | * 0 - Successfully converted BTF into bpf_reg_state | |
6624 | * (either PTR_TO_CTX or SCALAR_VALUE). | |
6625 | */ | |
6626 | int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog, | |
feb4adfa | 6627 | struct bpf_reg_state *regs) |
51c39bb1 AS |
6628 | { |
6629 | struct bpf_verifier_log *log = &env->log; | |
6630 | struct bpf_prog *prog = env->prog; | |
be8704ff | 6631 | enum bpf_prog_type prog_type = prog->type; |
51c39bb1 AS |
6632 | struct btf *btf = prog->aux->btf; |
6633 | const struct btf_param *args; | |
e5069b9c | 6634 | const struct btf_type *t, *ref_t; |
51c39bb1 AS |
6635 | u32 i, nargs, btf_id; |
6636 | const char *tname; | |
6637 | ||
6638 | if (!prog->aux->func_info || | |
6639 | prog->aux->func_info_aux[subprog].linkage != BTF_FUNC_GLOBAL) { | |
6640 | bpf_log(log, "Verifier bug\n"); | |
6641 | return -EFAULT; | |
6642 | } | |
6643 | ||
6644 | btf_id = prog->aux->func_info[subprog].type_id; | |
6645 | if (!btf_id) { | |
6646 | bpf_log(log, "Global functions need valid BTF\n"); | |
6647 | return -EFAULT; | |
6648 | } | |
6649 | ||
6650 | t = btf_type_by_id(btf, btf_id); | |
6651 | if (!t || !btf_type_is_func(t)) { | |
6652 | /* These checks were already done by the verifier while loading | |
6653 | * struct bpf_func_info | |
6654 | */ | |
6655 | bpf_log(log, "BTF of func#%d doesn't point to KIND_FUNC\n", | |
6656 | subprog); | |
6657 | return -EFAULT; | |
6658 | } | |
6659 | tname = btf_name_by_offset(btf, t->name_off); | |
6660 | ||
6661 | if (log->level & BPF_LOG_LEVEL) | |
6662 | bpf_log(log, "Validating %s() func#%d...\n", | |
6663 | tname, subprog); | |
6664 | ||
6665 | if (prog->aux->func_info_aux[subprog].unreliable) { | |
6666 | bpf_log(log, "Verifier bug in function %s()\n", tname); | |
6667 | return -EFAULT; | |
6668 | } | |
be8704ff | 6669 | if (prog_type == BPF_PROG_TYPE_EXT) |
3aac1ead | 6670 | prog_type = prog->aux->dst_prog->type; |
51c39bb1 AS |
6671 | |
6672 | t = btf_type_by_id(btf, t->type); | |
6673 | if (!t || !btf_type_is_func_proto(t)) { | |
6674 | bpf_log(log, "Invalid type of function %s()\n", tname); | |
6675 | return -EFAULT; | |
6676 | } | |
6677 | args = (const struct btf_param *)(t + 1); | |
6678 | nargs = btf_type_vlen(t); | |
523a4cf4 DB |
6679 | if (nargs > MAX_BPF_FUNC_REG_ARGS) { |
6680 | bpf_log(log, "Global function %s() with %d > %d args. Buggy compiler.\n", | |
6681 | tname, nargs, MAX_BPF_FUNC_REG_ARGS); | |
51c39bb1 AS |
6682 | return -EINVAL; |
6683 | } | |
6684 | /* check that function returns int */ | |
6685 | t = btf_type_by_id(btf, t->type); | |
6686 | while (btf_type_is_modifier(t)) | |
6687 | t = btf_type_by_id(btf, t->type); | |
6089fb32 | 6688 | if (!btf_type_is_int(t) && !btf_is_any_enum(t)) { |
51c39bb1 AS |
6689 | bpf_log(log, |
6690 | "Global function %s() doesn't return scalar. Only those are supported.\n", | |
6691 | tname); | |
6692 | return -EINVAL; | |
6693 | } | |
6694 | /* Convert BTF function arguments into verifier types. | |
6695 | * Only PTR_TO_CTX and SCALAR are supported atm. | |
6696 | */ | |
6697 | for (i = 0; i < nargs; i++) { | |
feb4adfa DB |
6698 | struct bpf_reg_state *reg = ®s[i + 1]; |
6699 | ||
51c39bb1 AS |
6700 | t = btf_type_by_id(btf, args[i].type); |
6701 | while (btf_type_is_modifier(t)) | |
6702 | t = btf_type_by_id(btf, t->type); | |
6089fb32 | 6703 | if (btf_type_is_int(t) || btf_is_any_enum(t)) { |
feb4adfa | 6704 | reg->type = SCALAR_VALUE; |
51c39bb1 AS |
6705 | continue; |
6706 | } | |
e5069b9c DB |
6707 | if (btf_type_is_ptr(t)) { |
6708 | if (btf_get_prog_ctx_type(log, btf, t, prog_type, i)) { | |
6709 | reg->type = PTR_TO_CTX; | |
6710 | continue; | |
6711 | } | |
6712 | ||
6713 | t = btf_type_skip_modifiers(btf, t->type, NULL); | |
6714 | ||
6715 | ref_t = btf_resolve_size(btf, t, ®->mem_size); | |
6716 | if (IS_ERR(ref_t)) { | |
6717 | bpf_log(log, | |
6718 | "arg#%d reference type('%s %s') size cannot be determined: %ld\n", | |
6719 | i, btf_type_str(t), btf_name_by_offset(btf, t->name_off), | |
6720 | PTR_ERR(ref_t)); | |
6721 | return -EINVAL; | |
6722 | } | |
6723 | ||
cf9f2f8d | 6724 | reg->type = PTR_TO_MEM | PTR_MAYBE_NULL; |
e5069b9c DB |
6725 | reg->id = ++env->id_gen; |
6726 | ||
51c39bb1 AS |
6727 | continue; |
6728 | } | |
6729 | bpf_log(log, "Arg#%d type %s in %s() is not supported yet.\n", | |
6730 | i, btf_kind_str[BTF_INFO_KIND(t->info)], tname); | |
6731 | return -EINVAL; | |
6732 | } | |
8c1b6e69 AS |
6733 | return 0; |
6734 | } | |
6735 | ||
31d0bc81 AM |
6736 | static void btf_type_show(const struct btf *btf, u32 type_id, void *obj, |
6737 | struct btf_show *show) | |
6738 | { | |
6739 | const struct btf_type *t = btf_type_by_id(btf, type_id); | |
6740 | ||
6741 | show->btf = btf; | |
6742 | memset(&show->state, 0, sizeof(show->state)); | |
6743 | memset(&show->obj, 0, sizeof(show->obj)); | |
6744 | ||
6745 | btf_type_ops(t)->show(btf, t, type_id, obj, 0, show); | |
6746 | } | |
6747 | ||
6748 | static void btf_seq_show(struct btf_show *show, const char *fmt, | |
6749 | va_list args) | |
6750 | { | |
6751 | seq_vprintf((struct seq_file *)show->target, fmt, args); | |
6752 | } | |
6753 | ||
eb411377 AM |
6754 | int btf_type_seq_show_flags(const struct btf *btf, u32 type_id, |
6755 | void *obj, struct seq_file *m, u64 flags) | |
31d0bc81 AM |
6756 | { |
6757 | struct btf_show sseq; | |
6758 | ||
6759 | sseq.target = m; | |
6760 | sseq.showfn = btf_seq_show; | |
6761 | sseq.flags = flags; | |
6762 | ||
6763 | btf_type_show(btf, type_id, obj, &sseq); | |
6764 | ||
6765 | return sseq.state.status; | |
6766 | } | |
6767 | ||
b00b8dae MKL |
6768 | void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj, |
6769 | struct seq_file *m) | |
6770 | { | |
31d0bc81 AM |
6771 | (void) btf_type_seq_show_flags(btf, type_id, obj, m, |
6772 | BTF_SHOW_NONAME | BTF_SHOW_COMPACT | | |
6773 | BTF_SHOW_ZERO | BTF_SHOW_UNSAFE); | |
6774 | } | |
6775 | ||
6776 | struct btf_show_snprintf { | |
6777 | struct btf_show show; | |
6778 | int len_left; /* space left in string */ | |
6779 | int len; /* length we would have written */ | |
6780 | }; | |
6781 | ||
6782 | static void btf_snprintf_show(struct btf_show *show, const char *fmt, | |
6783 | va_list args) | |
6784 | { | |
6785 | struct btf_show_snprintf *ssnprintf = (struct btf_show_snprintf *)show; | |
6786 | int len; | |
6787 | ||
6788 | len = vsnprintf(show->target, ssnprintf->len_left, fmt, args); | |
6789 | ||
6790 | if (len < 0) { | |
6791 | ssnprintf->len_left = 0; | |
6792 | ssnprintf->len = len; | |
58250ae3 | 6793 | } else if (len >= ssnprintf->len_left) { |
31d0bc81 AM |
6794 | /* no space, drive on to get length we would have written */ |
6795 | ssnprintf->len_left = 0; | |
6796 | ssnprintf->len += len; | |
6797 | } else { | |
6798 | ssnprintf->len_left -= len; | |
6799 | ssnprintf->len += len; | |
6800 | show->target += len; | |
6801 | } | |
6802 | } | |
6803 | ||
6804 | int btf_type_snprintf_show(const struct btf *btf, u32 type_id, void *obj, | |
6805 | char *buf, int len, u64 flags) | |
6806 | { | |
6807 | struct btf_show_snprintf ssnprintf; | |
6808 | ||
6809 | ssnprintf.show.target = buf; | |
6810 | ssnprintf.show.flags = flags; | |
6811 | ssnprintf.show.showfn = btf_snprintf_show; | |
6812 | ssnprintf.len_left = len; | |
6813 | ssnprintf.len = 0; | |
6814 | ||
6815 | btf_type_show(btf, type_id, obj, (struct btf_show *)&ssnprintf); | |
6816 | ||
c561d110 | 6817 | /* If we encountered an error, return it. */ |
31d0bc81 AM |
6818 | if (ssnprintf.show.state.status) |
6819 | return ssnprintf.show.state.status; | |
b00b8dae | 6820 | |
31d0bc81 AM |
6821 | /* Otherwise return length we would have written */ |
6822 | return ssnprintf.len; | |
b00b8dae | 6823 | } |
f56a653c | 6824 | |
3481e64b QM |
6825 | #ifdef CONFIG_PROC_FS |
6826 | static void bpf_btf_show_fdinfo(struct seq_file *m, struct file *filp) | |
6827 | { | |
6828 | const struct btf *btf = filp->private_data; | |
6829 | ||
6830 | seq_printf(m, "btf_id:\t%u\n", btf->id); | |
6831 | } | |
6832 | #endif | |
6833 | ||
f56a653c MKL |
6834 | static int btf_release(struct inode *inode, struct file *filp) |
6835 | { | |
6836 | btf_put(filp->private_data); | |
6837 | return 0; | |
6838 | } | |
6839 | ||
60197cfb | 6840 | const struct file_operations btf_fops = { |
3481e64b QM |
6841 | #ifdef CONFIG_PROC_FS |
6842 | .show_fdinfo = bpf_btf_show_fdinfo, | |
6843 | #endif | |
f56a653c MKL |
6844 | .release = btf_release, |
6845 | }; | |
6846 | ||
78958fca MKL |
6847 | static int __btf_new_fd(struct btf *btf) |
6848 | { | |
6849 | return anon_inode_getfd("btf", &btf_fops, btf, O_RDONLY | O_CLOEXEC); | |
6850 | } | |
6851 | ||
c571bd75 | 6852 | int btf_new_fd(const union bpf_attr *attr, bpfptr_t uattr) |
f56a653c MKL |
6853 | { |
6854 | struct btf *btf; | |
78958fca | 6855 | int ret; |
f56a653c | 6856 | |
c571bd75 | 6857 | btf = btf_parse(make_bpfptr(attr->btf, uattr.is_kernel), |
f56a653c MKL |
6858 | attr->btf_size, attr->btf_log_level, |
6859 | u64_to_user_ptr(attr->btf_log_buf), | |
6860 | attr->btf_log_size); | |
6861 | if (IS_ERR(btf)) | |
6862 | return PTR_ERR(btf); | |
6863 | ||
78958fca MKL |
6864 | ret = btf_alloc_id(btf); |
6865 | if (ret) { | |
6866 | btf_free(btf); | |
6867 | return ret; | |
6868 | } | |
6869 | ||
6870 | /* | |
6871 | * The BTF ID is published to the userspace. | |
6872 | * All BTF free must go through call_rcu() from | |
6873 | * now on (i.e. free by calling btf_put()). | |
6874 | */ | |
6875 | ||
6876 | ret = __btf_new_fd(btf); | |
6877 | if (ret < 0) | |
f56a653c MKL |
6878 | btf_put(btf); |
6879 | ||
78958fca | 6880 | return ret; |
f56a653c MKL |
6881 | } |
6882 | ||
6883 | struct btf *btf_get_by_fd(int fd) | |
6884 | { | |
6885 | struct btf *btf; | |
6886 | struct fd f; | |
6887 | ||
6888 | f = fdget(fd); | |
6889 | ||
6890 | if (!f.file) | |
6891 | return ERR_PTR(-EBADF); | |
6892 | ||
6893 | if (f.file->f_op != &btf_fops) { | |
6894 | fdput(f); | |
6895 | return ERR_PTR(-EINVAL); | |
6896 | } | |
6897 | ||
6898 | btf = f.file->private_data; | |
78958fca | 6899 | refcount_inc(&btf->refcnt); |
f56a653c MKL |
6900 | fdput(f); |
6901 | ||
6902 | return btf; | |
6903 | } | |
60197cfb MKL |
6904 | |
6905 | int btf_get_info_by_fd(const struct btf *btf, | |
6906 | const union bpf_attr *attr, | |
6907 | union bpf_attr __user *uattr) | |
6908 | { | |
62dab84c | 6909 | struct bpf_btf_info __user *uinfo; |
5c6f2588 | 6910 | struct bpf_btf_info info; |
62dab84c MKL |
6911 | u32 info_copy, btf_copy; |
6912 | void __user *ubtf; | |
53297220 AN |
6913 | char __user *uname; |
6914 | u32 uinfo_len, uname_len, name_len; | |
6915 | int ret = 0; | |
60197cfb | 6916 | |
62dab84c MKL |
6917 | uinfo = u64_to_user_ptr(attr->info.info); |
6918 | uinfo_len = attr->info.info_len; | |
6919 | ||
6920 | info_copy = min_t(u32, uinfo_len, sizeof(info)); | |
5c6f2588 | 6921 | memset(&info, 0, sizeof(info)); |
62dab84c MKL |
6922 | if (copy_from_user(&info, uinfo, info_copy)) |
6923 | return -EFAULT; | |
6924 | ||
6925 | info.id = btf->id; | |
6926 | ubtf = u64_to_user_ptr(info.btf); | |
6927 | btf_copy = min_t(u32, btf->data_size, info.btf_size); | |
6928 | if (copy_to_user(ubtf, btf->data, btf_copy)) | |
6929 | return -EFAULT; | |
6930 | info.btf_size = btf->data_size; | |
6931 | ||
53297220 AN |
6932 | info.kernel_btf = btf->kernel_btf; |
6933 | ||
6934 | uname = u64_to_user_ptr(info.name); | |
6935 | uname_len = info.name_len; | |
6936 | if (!uname ^ !uname_len) | |
6937 | return -EINVAL; | |
6938 | ||
6939 | name_len = strlen(btf->name); | |
6940 | info.name_len = name_len; | |
6941 | ||
6942 | if (uname) { | |
6943 | if (uname_len >= name_len + 1) { | |
6944 | if (copy_to_user(uname, btf->name, name_len + 1)) | |
6945 | return -EFAULT; | |
6946 | } else { | |
6947 | char zero = '\0'; | |
6948 | ||
6949 | if (copy_to_user(uname, btf->name, uname_len - 1)) | |
6950 | return -EFAULT; | |
6951 | if (put_user(zero, uname + uname_len - 1)) | |
6952 | return -EFAULT; | |
6953 | /* let user-space know about too short buffer */ | |
6954 | ret = -ENOSPC; | |
6955 | } | |
6956 | } | |
6957 | ||
62dab84c MKL |
6958 | if (copy_to_user(uinfo, &info, info_copy) || |
6959 | put_user(info_copy, &uattr->info.info_len)) | |
60197cfb MKL |
6960 | return -EFAULT; |
6961 | ||
53297220 | 6962 | return ret; |
60197cfb | 6963 | } |
78958fca MKL |
6964 | |
6965 | int btf_get_fd_by_id(u32 id) | |
6966 | { | |
6967 | struct btf *btf; | |
6968 | int fd; | |
6969 | ||
6970 | rcu_read_lock(); | |
6971 | btf = idr_find(&btf_idr, id); | |
6972 | if (!btf || !refcount_inc_not_zero(&btf->refcnt)) | |
6973 | btf = ERR_PTR(-ENOENT); | |
6974 | rcu_read_unlock(); | |
6975 | ||
6976 | if (IS_ERR(btf)) | |
6977 | return PTR_ERR(btf); | |
6978 | ||
6979 | fd = __btf_new_fd(btf); | |
6980 | if (fd < 0) | |
6981 | btf_put(btf); | |
6982 | ||
6983 | return fd; | |
6984 | } | |
6985 | ||
22dc4a0f | 6986 | u32 btf_obj_id(const struct btf *btf) |
78958fca MKL |
6987 | { |
6988 | return btf->id; | |
6989 | } | |
eae2e83e | 6990 | |
290248a5 AN |
6991 | bool btf_is_kernel(const struct btf *btf) |
6992 | { | |
6993 | return btf->kernel_btf; | |
6994 | } | |
6995 | ||
541c3bad AN |
6996 | bool btf_is_module(const struct btf *btf) |
6997 | { | |
6998 | return btf->kernel_btf && strcmp(btf->name, "vmlinux") != 0; | |
6999 | } | |
7000 | ||
eae2e83e JO |
7001 | static int btf_id_cmp_func(const void *a, const void *b) |
7002 | { | |
7003 | const int *pa = a, *pb = b; | |
7004 | ||
7005 | return *pa - *pb; | |
7006 | } | |
7007 | ||
2af30f11 | 7008 | bool btf_id_set_contains(const struct btf_id_set *set, u32 id) |
eae2e83e JO |
7009 | { |
7010 | return bsearch(&id, set->ids, set->cnt, sizeof(u32), btf_id_cmp_func) != NULL; | |
7011 | } | |
36e68442 | 7012 | |
a4703e31 KKD |
7013 | static void *btf_id_set8_contains(const struct btf_id_set8 *set, u32 id) |
7014 | { | |
7015 | return bsearch(&id, set->pairs, set->cnt, sizeof(set->pairs[0]), btf_id_cmp_func); | |
7016 | } | |
7017 | ||
18688de2 KKD |
7018 | enum { |
7019 | BTF_MODULE_F_LIVE = (1 << 0), | |
7020 | }; | |
7021 | ||
36e68442 AN |
7022 | #ifdef CONFIG_DEBUG_INFO_BTF_MODULES |
7023 | struct btf_module { | |
7024 | struct list_head list; | |
7025 | struct module *module; | |
7026 | struct btf *btf; | |
7027 | struct bin_attribute *sysfs_attr; | |
18688de2 | 7028 | int flags; |
36e68442 AN |
7029 | }; |
7030 | ||
7031 | static LIST_HEAD(btf_modules); | |
7032 | static DEFINE_MUTEX(btf_module_mutex); | |
7033 | ||
7034 | static ssize_t | |
7035 | btf_module_read(struct file *file, struct kobject *kobj, | |
7036 | struct bin_attribute *bin_attr, | |
7037 | char *buf, loff_t off, size_t len) | |
7038 | { | |
7039 | const struct btf *btf = bin_attr->private; | |
7040 | ||
7041 | memcpy(buf, btf->data + off, len); | |
7042 | return len; | |
7043 | } | |
7044 | ||
1e89106d AS |
7045 | static void purge_cand_cache(struct btf *btf); |
7046 | ||
36e68442 AN |
7047 | static int btf_module_notify(struct notifier_block *nb, unsigned long op, |
7048 | void *module) | |
7049 | { | |
7050 | struct btf_module *btf_mod, *tmp; | |
7051 | struct module *mod = module; | |
7052 | struct btf *btf; | |
7053 | int err = 0; | |
7054 | ||
7055 | if (mod->btf_data_size == 0 || | |
18688de2 KKD |
7056 | (op != MODULE_STATE_COMING && op != MODULE_STATE_LIVE && |
7057 | op != MODULE_STATE_GOING)) | |
36e68442 AN |
7058 | goto out; |
7059 | ||
7060 | switch (op) { | |
7061 | case MODULE_STATE_COMING: | |
7062 | btf_mod = kzalloc(sizeof(*btf_mod), GFP_KERNEL); | |
7063 | if (!btf_mod) { | |
7064 | err = -ENOMEM; | |
7065 | goto out; | |
7066 | } | |
7067 | btf = btf_parse_module(mod->name, mod->btf_data, mod->btf_data_size); | |
7068 | if (IS_ERR(btf)) { | |
7069 | pr_warn("failed to validate module [%s] BTF: %ld\n", | |
7070 | mod->name, PTR_ERR(btf)); | |
7071 | kfree(btf_mod); | |
5e214f2e CB |
7072 | if (!IS_ENABLED(CONFIG_MODULE_ALLOW_BTF_MISMATCH)) |
7073 | err = PTR_ERR(btf); | |
36e68442 AN |
7074 | goto out; |
7075 | } | |
7076 | err = btf_alloc_id(btf); | |
7077 | if (err) { | |
7078 | btf_free(btf); | |
7079 | kfree(btf_mod); | |
7080 | goto out; | |
7081 | } | |
7082 | ||
1e89106d | 7083 | purge_cand_cache(NULL); |
36e68442 AN |
7084 | mutex_lock(&btf_module_mutex); |
7085 | btf_mod->module = module; | |
7086 | btf_mod->btf = btf; | |
7087 | list_add(&btf_mod->list, &btf_modules); | |
7088 | mutex_unlock(&btf_module_mutex); | |
7089 | ||
7090 | if (IS_ENABLED(CONFIG_SYSFS)) { | |
7091 | struct bin_attribute *attr; | |
7092 | ||
7093 | attr = kzalloc(sizeof(*attr), GFP_KERNEL); | |
7094 | if (!attr) | |
7095 | goto out; | |
7096 | ||
7097 | sysfs_bin_attr_init(attr); | |
7098 | attr->attr.name = btf->name; | |
7099 | attr->attr.mode = 0444; | |
7100 | attr->size = btf->data_size; | |
7101 | attr->private = btf; | |
7102 | attr->read = btf_module_read; | |
7103 | ||
7104 | err = sysfs_create_bin_file(btf_kobj, attr); | |
7105 | if (err) { | |
7106 | pr_warn("failed to register module [%s] BTF in sysfs: %d\n", | |
7107 | mod->name, err); | |
7108 | kfree(attr); | |
7109 | err = 0; | |
7110 | goto out; | |
7111 | } | |
7112 | ||
7113 | btf_mod->sysfs_attr = attr; | |
7114 | } | |
7115 | ||
18688de2 KKD |
7116 | break; |
7117 | case MODULE_STATE_LIVE: | |
7118 | mutex_lock(&btf_module_mutex); | |
7119 | list_for_each_entry_safe(btf_mod, tmp, &btf_modules, list) { | |
7120 | if (btf_mod->module != module) | |
7121 | continue; | |
7122 | ||
7123 | btf_mod->flags |= BTF_MODULE_F_LIVE; | |
7124 | break; | |
7125 | } | |
7126 | mutex_unlock(&btf_module_mutex); | |
36e68442 AN |
7127 | break; |
7128 | case MODULE_STATE_GOING: | |
7129 | mutex_lock(&btf_module_mutex); | |
7130 | list_for_each_entry_safe(btf_mod, tmp, &btf_modules, list) { | |
7131 | if (btf_mod->module != module) | |
7132 | continue; | |
7133 | ||
7134 | list_del(&btf_mod->list); | |
7135 | if (btf_mod->sysfs_attr) | |
7136 | sysfs_remove_bin_file(btf_kobj, btf_mod->sysfs_attr); | |
1e89106d | 7137 | purge_cand_cache(btf_mod->btf); |
36e68442 AN |
7138 | btf_put(btf_mod->btf); |
7139 | kfree(btf_mod->sysfs_attr); | |
7140 | kfree(btf_mod); | |
7141 | break; | |
7142 | } | |
7143 | mutex_unlock(&btf_module_mutex); | |
7144 | break; | |
7145 | } | |
7146 | out: | |
7147 | return notifier_from_errno(err); | |
7148 | } | |
7149 | ||
7150 | static struct notifier_block btf_module_nb = { | |
7151 | .notifier_call = btf_module_notify, | |
7152 | }; | |
7153 | ||
7154 | static int __init btf_module_init(void) | |
7155 | { | |
7156 | register_module_notifier(&btf_module_nb); | |
7157 | return 0; | |
7158 | } | |
7159 | ||
7160 | fs_initcall(btf_module_init); | |
7161 | #endif /* CONFIG_DEBUG_INFO_BTF_MODULES */ | |
541c3bad AN |
7162 | |
7163 | struct module *btf_try_get_module(const struct btf *btf) | |
7164 | { | |
7165 | struct module *res = NULL; | |
7166 | #ifdef CONFIG_DEBUG_INFO_BTF_MODULES | |
7167 | struct btf_module *btf_mod, *tmp; | |
7168 | ||
7169 | mutex_lock(&btf_module_mutex); | |
7170 | list_for_each_entry_safe(btf_mod, tmp, &btf_modules, list) { | |
7171 | if (btf_mod->btf != btf) | |
7172 | continue; | |
7173 | ||
18688de2 KKD |
7174 | /* We must only consider module whose __init routine has |
7175 | * finished, hence we must check for BTF_MODULE_F_LIVE flag, | |
7176 | * which is set from the notifier callback for | |
7177 | * MODULE_STATE_LIVE. | |
7178 | */ | |
7179 | if ((btf_mod->flags & BTF_MODULE_F_LIVE) && try_module_get(btf_mod->module)) | |
541c3bad AN |
7180 | res = btf_mod->module; |
7181 | ||
7182 | break; | |
7183 | } | |
7184 | mutex_unlock(&btf_module_mutex); | |
7185 | #endif | |
7186 | ||
7187 | return res; | |
7188 | } | |
3d78417b | 7189 | |
9492450f KKD |
7190 | /* Returns struct btf corresponding to the struct module. |
7191 | * This function can return NULL or ERR_PTR. | |
dee872e1 KKD |
7192 | */ |
7193 | static struct btf *btf_get_module_btf(const struct module *module) | |
7194 | { | |
dee872e1 KKD |
7195 | #ifdef CONFIG_DEBUG_INFO_BTF_MODULES |
7196 | struct btf_module *btf_mod, *tmp; | |
7197 | #endif | |
9492450f KKD |
7198 | struct btf *btf = NULL; |
7199 | ||
7200 | if (!module) { | |
7201 | btf = bpf_get_btf_vmlinux(); | |
7ada3787 | 7202 | if (!IS_ERR_OR_NULL(btf)) |
9492450f KKD |
7203 | btf_get(btf); |
7204 | return btf; | |
7205 | } | |
dee872e1 | 7206 | |
dee872e1 KKD |
7207 | #ifdef CONFIG_DEBUG_INFO_BTF_MODULES |
7208 | mutex_lock(&btf_module_mutex); | |
7209 | list_for_each_entry_safe(btf_mod, tmp, &btf_modules, list) { | |
7210 | if (btf_mod->module != module) | |
7211 | continue; | |
7212 | ||
7213 | btf_get(btf_mod->btf); | |
7214 | btf = btf_mod->btf; | |
7215 | break; | |
7216 | } | |
7217 | mutex_unlock(&btf_module_mutex); | |
7218 | #endif | |
7219 | ||
7220 | return btf; | |
7221 | } | |
7222 | ||
3d78417b AS |
7223 | BPF_CALL_4(bpf_btf_find_by_name_kind, char *, name, int, name_sz, u32, kind, int, flags) |
7224 | { | |
edc3ec09 KKD |
7225 | struct btf *btf = NULL; |
7226 | int btf_obj_fd = 0; | |
3d78417b AS |
7227 | long ret; |
7228 | ||
7229 | if (flags) | |
7230 | return -EINVAL; | |
7231 | ||
7232 | if (name_sz <= 1 || name[name_sz - 1]) | |
7233 | return -EINVAL; | |
7234 | ||
edc3ec09 KKD |
7235 | ret = bpf_find_btf_id(name, kind, &btf); |
7236 | if (ret > 0 && btf_is_module(btf)) { | |
7237 | btf_obj_fd = __btf_new_fd(btf); | |
7238 | if (btf_obj_fd < 0) { | |
7239 | btf_put(btf); | |
7240 | return btf_obj_fd; | |
3d78417b | 7241 | } |
edc3ec09 | 7242 | return ret | (((u64)btf_obj_fd) << 32); |
3d78417b | 7243 | } |
edc3ec09 KKD |
7244 | if (ret > 0) |
7245 | btf_put(btf); | |
3d78417b AS |
7246 | return ret; |
7247 | } | |
7248 | ||
7249 | const struct bpf_func_proto bpf_btf_find_by_name_kind_proto = { | |
7250 | .func = bpf_btf_find_by_name_kind, | |
7251 | .gpl_only = false, | |
7252 | .ret_type = RET_INTEGER, | |
216e3cd2 | 7253 | .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY, |
3d78417b AS |
7254 | .arg2_type = ARG_CONST_SIZE, |
7255 | .arg3_type = ARG_ANYTHING, | |
7256 | .arg4_type = ARG_ANYTHING, | |
7257 | }; | |
eb529c5b | 7258 | |
d19ddb47 SL |
7259 | BTF_ID_LIST_GLOBAL(btf_tracing_ids, MAX_BTF_TRACING_TYPE) |
7260 | #define BTF_TRACING_TYPE(name, type) BTF_ID(struct, type) | |
7261 | BTF_TRACING_TYPE_xxx | |
7262 | #undef BTF_TRACING_TYPE | |
14f267d9 | 7263 | |
dee872e1 | 7264 | /* Kernel Function (kfunc) BTF ID set registration API */ |
14f267d9 | 7265 | |
a4703e31 KKD |
7266 | static int btf_populate_kfunc_set(struct btf *btf, enum btf_kfunc_hook hook, |
7267 | struct btf_id_set8 *add_set) | |
14f267d9 | 7268 | { |
a4703e31 | 7269 | bool vmlinux_set = !btf_is_module(btf); |
dee872e1 | 7270 | struct btf_kfunc_set_tab *tab; |
a4703e31 | 7271 | struct btf_id_set8 *set; |
dee872e1 KKD |
7272 | u32 set_cnt; |
7273 | int ret; | |
7274 | ||
a4703e31 | 7275 | if (hook >= BTF_KFUNC_HOOK_MAX) { |
dee872e1 KKD |
7276 | ret = -EINVAL; |
7277 | goto end; | |
7278 | } | |
7279 | ||
7280 | if (!add_set->cnt) | |
7281 | return 0; | |
7282 | ||
7283 | tab = btf->kfunc_set_tab; | |
7284 | if (!tab) { | |
7285 | tab = kzalloc(sizeof(*tab), GFP_KERNEL | __GFP_NOWARN); | |
7286 | if (!tab) | |
7287 | return -ENOMEM; | |
7288 | btf->kfunc_set_tab = tab; | |
7289 | } | |
7290 | ||
a4703e31 | 7291 | set = tab->sets[hook]; |
dee872e1 KKD |
7292 | /* Warn when register_btf_kfunc_id_set is called twice for the same hook |
7293 | * for module sets. | |
7294 | */ | |
7295 | if (WARN_ON_ONCE(set && !vmlinux_set)) { | |
7296 | ret = -EINVAL; | |
7297 | goto end; | |
7298 | } | |
7299 | ||
7300 | /* We don't need to allocate, concatenate, and sort module sets, because | |
7301 | * only one is allowed per hook. Hence, we can directly assign the | |
7302 | * pointer and return. | |
7303 | */ | |
7304 | if (!vmlinux_set) { | |
a4703e31 | 7305 | tab->sets[hook] = add_set; |
dee872e1 KKD |
7306 | return 0; |
7307 | } | |
7308 | ||
7309 | /* In case of vmlinux sets, there may be more than one set being | |
7310 | * registered per hook. To create a unified set, we allocate a new set | |
7311 | * and concatenate all individual sets being registered. While each set | |
7312 | * is individually sorted, they may become unsorted when concatenated, | |
7313 | * hence re-sorting the final set again is required to make binary | |
a4703e31 | 7314 | * searching the set using btf_id_set8_contains function work. |
dee872e1 KKD |
7315 | */ |
7316 | set_cnt = set ? set->cnt : 0; | |
7317 | ||
7318 | if (set_cnt > U32_MAX - add_set->cnt) { | |
7319 | ret = -EOVERFLOW; | |
7320 | goto end; | |
7321 | } | |
7322 | ||
7323 | if (set_cnt + add_set->cnt > BTF_KFUNC_SET_MAX_CNT) { | |
7324 | ret = -E2BIG; | |
7325 | goto end; | |
7326 | } | |
7327 | ||
7328 | /* Grow set */ | |
a4703e31 KKD |
7329 | set = krealloc(tab->sets[hook], |
7330 | offsetof(struct btf_id_set8, pairs[set_cnt + add_set->cnt]), | |
dee872e1 KKD |
7331 | GFP_KERNEL | __GFP_NOWARN); |
7332 | if (!set) { | |
7333 | ret = -ENOMEM; | |
7334 | goto end; | |
7335 | } | |
7336 | ||
7337 | /* For newly allocated set, initialize set->cnt to 0 */ | |
a4703e31 | 7338 | if (!tab->sets[hook]) |
dee872e1 | 7339 | set->cnt = 0; |
a4703e31 | 7340 | tab->sets[hook] = set; |
dee872e1 KKD |
7341 | |
7342 | /* Concatenate the two sets */ | |
a4703e31 | 7343 | memcpy(set->pairs + set->cnt, add_set->pairs, add_set->cnt * sizeof(set->pairs[0])); |
dee872e1 KKD |
7344 | set->cnt += add_set->cnt; |
7345 | ||
a4703e31 | 7346 | sort(set->pairs, set->cnt, sizeof(set->pairs[0]), btf_id_cmp_func, NULL); |
dee872e1 KKD |
7347 | |
7348 | return 0; | |
7349 | end: | |
7350 | btf_free_kfunc_set_tab(btf); | |
7351 | return ret; | |
14f267d9 | 7352 | } |
14f267d9 | 7353 | |
a4703e31 | 7354 | static u32 *__btf_kfunc_id_set_contains(const struct btf *btf, |
dee872e1 | 7355 | enum btf_kfunc_hook hook, |
dee872e1 | 7356 | u32 kfunc_btf_id) |
14f267d9 | 7357 | { |
a4703e31 KKD |
7358 | struct btf_id_set8 *set; |
7359 | u32 *id; | |
14f267d9 | 7360 | |
a4703e31 KKD |
7361 | if (hook >= BTF_KFUNC_HOOK_MAX) |
7362 | return NULL; | |
dee872e1 | 7363 | if (!btf->kfunc_set_tab) |
a4703e31 KKD |
7364 | return NULL; |
7365 | set = btf->kfunc_set_tab->sets[hook]; | |
dee872e1 | 7366 | if (!set) |
a4703e31 KKD |
7367 | return NULL; |
7368 | id = btf_id_set8_contains(set, kfunc_btf_id); | |
7369 | if (!id) | |
7370 | return NULL; | |
7371 | /* The flags for BTF ID are located next to it */ | |
7372 | return id + 1; | |
dee872e1 KKD |
7373 | } |
7374 | ||
7375 | static int bpf_prog_type_to_kfunc_hook(enum bpf_prog_type prog_type) | |
7376 | { | |
7377 | switch (prog_type) { | |
7378 | case BPF_PROG_TYPE_XDP: | |
7379 | return BTF_KFUNC_HOOK_XDP; | |
7380 | case BPF_PROG_TYPE_SCHED_CLS: | |
7381 | return BTF_KFUNC_HOOK_TC; | |
7382 | case BPF_PROG_TYPE_STRUCT_OPS: | |
7383 | return BTF_KFUNC_HOOK_STRUCT_OPS; | |
97949767 BT |
7384 | case BPF_PROG_TYPE_TRACING: |
7385 | return BTF_KFUNC_HOOK_TRACING; | |
7386 | case BPF_PROG_TYPE_SYSCALL: | |
7387 | return BTF_KFUNC_HOOK_SYSCALL; | |
dee872e1 KKD |
7388 | default: |
7389 | return BTF_KFUNC_HOOK_MAX; | |
14f267d9 | 7390 | } |
14f267d9 KKD |
7391 | } |
7392 | ||
dee872e1 KKD |
7393 | /* Caution: |
7394 | * Reference to the module (obtained using btf_try_get_module) corresponding to | |
7395 | * the struct btf *MUST* be held when calling this function from verifier | |
7396 | * context. This is usually true as we stash references in prog's kfunc_btf_tab; | |
7397 | * keeping the reference for the duration of the call provides the necessary | |
7398 | * protection for looking up a well-formed btf->kfunc_set_tab. | |
7399 | */ | |
a4703e31 | 7400 | u32 *btf_kfunc_id_set_contains(const struct btf *btf, |
dee872e1 | 7401 | enum bpf_prog_type prog_type, |
a4703e31 | 7402 | u32 kfunc_btf_id) |
dee872e1 KKD |
7403 | { |
7404 | enum btf_kfunc_hook hook; | |
0e32dfc8 | 7405 | |
dee872e1 | 7406 | hook = bpf_prog_type_to_kfunc_hook(prog_type); |
a4703e31 | 7407 | return __btf_kfunc_id_set_contains(btf, hook, kfunc_btf_id); |
dee872e1 | 7408 | } |
d9847eb8 | 7409 | |
dee872e1 KKD |
7410 | /* This function must be invoked only from initcalls/module init functions */ |
7411 | int register_btf_kfunc_id_set(enum bpf_prog_type prog_type, | |
7412 | const struct btf_kfunc_id_set *kset) | |
7413 | { | |
7414 | enum btf_kfunc_hook hook; | |
7415 | struct btf *btf; | |
7416 | int ret; | |
7417 | ||
7418 | btf = btf_get_module_btf(kset->owner); | |
c446fdac SF |
7419 | if (!btf) { |
7420 | if (!kset->owner && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) { | |
7421 | pr_err("missing vmlinux BTF, cannot register kfuncs\n"); | |
7422 | return -ENOENT; | |
7423 | } | |
7424 | if (kset->owner && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES)) { | |
7425 | pr_err("missing module BTF, cannot register kfuncs\n"); | |
7426 | return -ENOENT; | |
7427 | } | |
7428 | return 0; | |
7429 | } | |
7430 | if (IS_ERR(btf)) | |
7431 | return PTR_ERR(btf); | |
dee872e1 KKD |
7432 | |
7433 | hook = bpf_prog_type_to_kfunc_hook(prog_type); | |
a4703e31 | 7434 | ret = btf_populate_kfunc_set(btf, hook, kset->set); |
9492450f | 7435 | btf_put(btf); |
dee872e1 KKD |
7436 | return ret; |
7437 | } | |
7438 | EXPORT_SYMBOL_GPL(register_btf_kfunc_id_set); | |
be315829 | 7439 | |
5ce937d6 KKD |
7440 | s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id) |
7441 | { | |
7442 | struct btf_id_dtor_kfunc_tab *tab = btf->dtor_kfunc_tab; | |
7443 | struct btf_id_dtor_kfunc *dtor; | |
7444 | ||
7445 | if (!tab) | |
7446 | return -ENOENT; | |
7447 | /* Even though the size of tab->dtors[0] is > sizeof(u32), we only need | |
7448 | * to compare the first u32 with btf_id, so we can reuse btf_id_cmp_func. | |
7449 | */ | |
7450 | BUILD_BUG_ON(offsetof(struct btf_id_dtor_kfunc, btf_id) != 0); | |
7451 | dtor = bsearch(&btf_id, tab->dtors, tab->cnt, sizeof(tab->dtors[0]), btf_id_cmp_func); | |
7452 | if (!dtor) | |
7453 | return -ENOENT; | |
7454 | return dtor->kfunc_btf_id; | |
7455 | } | |
7456 | ||
14a324f6 KKD |
7457 | static int btf_check_dtor_kfuncs(struct btf *btf, const struct btf_id_dtor_kfunc *dtors, u32 cnt) |
7458 | { | |
7459 | const struct btf_type *dtor_func, *dtor_func_proto, *t; | |
7460 | const struct btf_param *args; | |
7461 | s32 dtor_btf_id; | |
7462 | u32 nr_args, i; | |
7463 | ||
7464 | for (i = 0; i < cnt; i++) { | |
7465 | dtor_btf_id = dtors[i].kfunc_btf_id; | |
7466 | ||
7467 | dtor_func = btf_type_by_id(btf, dtor_btf_id); | |
7468 | if (!dtor_func || !btf_type_is_func(dtor_func)) | |
7469 | return -EINVAL; | |
7470 | ||
7471 | dtor_func_proto = btf_type_by_id(btf, dtor_func->type); | |
7472 | if (!dtor_func_proto || !btf_type_is_func_proto(dtor_func_proto)) | |
7473 | return -EINVAL; | |
7474 | ||
7475 | /* Make sure the prototype of the destructor kfunc is 'void func(type *)' */ | |
7476 | t = btf_type_by_id(btf, dtor_func_proto->type); | |
7477 | if (!t || !btf_type_is_void(t)) | |
7478 | return -EINVAL; | |
7479 | ||
7480 | nr_args = btf_type_vlen(dtor_func_proto); | |
7481 | if (nr_args != 1) | |
7482 | return -EINVAL; | |
7483 | args = btf_params(dtor_func_proto); | |
7484 | t = btf_type_by_id(btf, args[0].type); | |
7485 | /* Allow any pointer type, as width on targets Linux supports | |
7486 | * will be same for all pointer types (i.e. sizeof(void *)) | |
7487 | */ | |
7488 | if (!t || !btf_type_is_ptr(t)) | |
7489 | return -EINVAL; | |
7490 | } | |
7491 | return 0; | |
7492 | } | |
7493 | ||
5ce937d6 KKD |
7494 | /* This function must be invoked only from initcalls/module init functions */ |
7495 | int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors, u32 add_cnt, | |
7496 | struct module *owner) | |
7497 | { | |
7498 | struct btf_id_dtor_kfunc_tab *tab; | |
7499 | struct btf *btf; | |
7500 | u32 tab_cnt; | |
7501 | int ret; | |
7502 | ||
7503 | btf = btf_get_module_btf(owner); | |
7504 | if (!btf) { | |
7505 | if (!owner && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) { | |
7506 | pr_err("missing vmlinux BTF, cannot register dtor kfuncs\n"); | |
7507 | return -ENOENT; | |
7508 | } | |
7509 | if (owner && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES)) { | |
7510 | pr_err("missing module BTF, cannot register dtor kfuncs\n"); | |
7511 | return -ENOENT; | |
7512 | } | |
7513 | return 0; | |
7514 | } | |
7515 | if (IS_ERR(btf)) | |
7516 | return PTR_ERR(btf); | |
7517 | ||
7518 | if (add_cnt >= BTF_DTOR_KFUNC_MAX_CNT) { | |
7519 | pr_err("cannot register more than %d kfunc destructors\n", BTF_DTOR_KFUNC_MAX_CNT); | |
7520 | ret = -E2BIG; | |
7521 | goto end; | |
7522 | } | |
7523 | ||
14a324f6 KKD |
7524 | /* Ensure that the prototype of dtor kfuncs being registered is sane */ |
7525 | ret = btf_check_dtor_kfuncs(btf, dtors, add_cnt); | |
7526 | if (ret < 0) | |
7527 | goto end; | |
7528 | ||
5ce937d6 KKD |
7529 | tab = btf->dtor_kfunc_tab; |
7530 | /* Only one call allowed for modules */ | |
7531 | if (WARN_ON_ONCE(tab && btf_is_module(btf))) { | |
7532 | ret = -EINVAL; | |
7533 | goto end; | |
7534 | } | |
7535 | ||
7536 | tab_cnt = tab ? tab->cnt : 0; | |
7537 | if (tab_cnt > U32_MAX - add_cnt) { | |
7538 | ret = -EOVERFLOW; | |
7539 | goto end; | |
7540 | } | |
7541 | if (tab_cnt + add_cnt >= BTF_DTOR_KFUNC_MAX_CNT) { | |
7542 | pr_err("cannot register more than %d kfunc destructors\n", BTF_DTOR_KFUNC_MAX_CNT); | |
7543 | ret = -E2BIG; | |
7544 | goto end; | |
7545 | } | |
7546 | ||
7547 | tab = krealloc(btf->dtor_kfunc_tab, | |
7548 | offsetof(struct btf_id_dtor_kfunc_tab, dtors[tab_cnt + add_cnt]), | |
7549 | GFP_KERNEL | __GFP_NOWARN); | |
7550 | if (!tab) { | |
7551 | ret = -ENOMEM; | |
7552 | goto end; | |
7553 | } | |
7554 | ||
7555 | if (!btf->dtor_kfunc_tab) | |
7556 | tab->cnt = 0; | |
7557 | btf->dtor_kfunc_tab = tab; | |
7558 | ||
7559 | memcpy(tab->dtors + tab->cnt, dtors, add_cnt * sizeof(tab->dtors[0])); | |
7560 | tab->cnt += add_cnt; | |
7561 | ||
7562 | sort(tab->dtors, tab->cnt, sizeof(tab->dtors[0]), btf_id_cmp_func, NULL); | |
7563 | ||
7564 | return 0; | |
7565 | end: | |
7566 | btf_free_dtor_kfunc_tab(btf); | |
7567 | btf_put(btf); | |
7568 | return ret; | |
7569 | } | |
7570 | EXPORT_SYMBOL_GPL(register_btf_id_dtor_kfuncs); | |
7571 | ||
e70e13e7 MC |
7572 | #define MAX_TYPES_ARE_COMPAT_DEPTH 2 |
7573 | ||
e70e13e7 MC |
7574 | /* Check local and target types for compatibility. This check is used for |
7575 | * type-based CO-RE relocations and follow slightly different rules than | |
7576 | * field-based relocations. This function assumes that root types were already | |
7577 | * checked for name match. Beyond that initial root-level name check, names | |
7578 | * are completely ignored. Compatibility rules are as follows: | |
6089fb32 | 7579 | * - any two STRUCTs/UNIONs/FWDs/ENUMs/INTs/ENUM64s are considered compatible, but |
e70e13e7 MC |
7580 | * kind should match for local and target types (i.e., STRUCT is not |
7581 | * compatible with UNION); | |
6089fb32 | 7582 | * - for ENUMs/ENUM64s, the size is ignored; |
e70e13e7 MC |
7583 | * - for INT, size and signedness are ignored; |
7584 | * - for ARRAY, dimensionality is ignored, element types are checked for | |
7585 | * compatibility recursively; | |
7586 | * - CONST/VOLATILE/RESTRICT modifiers are ignored; | |
7587 | * - TYPEDEFs/PTRs are compatible if types they pointing to are compatible; | |
7588 | * - FUNC_PROTOs are compatible if they have compatible signature: same | |
7589 | * number of input args and compatible return and argument types. | |
7590 | * These rules are not set in stone and probably will be adjusted as we get | |
7591 | * more experience with using BPF CO-RE relocations. | |
7592 | */ | |
29db4bea AS |
7593 | int bpf_core_types_are_compat(const struct btf *local_btf, __u32 local_id, |
7594 | const struct btf *targ_btf, __u32 targ_id) | |
7595 | { | |
fd75733d | 7596 | return __bpf_core_types_are_compat(local_btf, local_id, targ_btf, targ_id, |
e70e13e7 | 7597 | MAX_TYPES_ARE_COMPAT_DEPTH); |
29db4bea AS |
7598 | } |
7599 | ||
ec6209c8 DM |
7600 | #define MAX_TYPES_MATCH_DEPTH 2 |
7601 | ||
7602 | int bpf_core_types_match(const struct btf *local_btf, u32 local_id, | |
7603 | const struct btf *targ_btf, u32 targ_id) | |
7604 | { | |
7605 | return __bpf_core_types_match(local_btf, local_id, targ_btf, targ_id, false, | |
7606 | MAX_TYPES_MATCH_DEPTH); | |
7607 | } | |
7608 | ||
29db4bea AS |
7609 | static bool bpf_core_is_flavor_sep(const char *s) |
7610 | { | |
7611 | /* check X___Y name pattern, where X and Y are not underscores */ | |
7612 | return s[0] != '_' && /* X */ | |
7613 | s[1] == '_' && s[2] == '_' && s[3] == '_' && /* ___ */ | |
7614 | s[4] != '_'; /* Y */ | |
7615 | } | |
7616 | ||
7617 | size_t bpf_core_essential_name_len(const char *name) | |
7618 | { | |
7619 | size_t n = strlen(name); | |
7620 | int i; | |
7621 | ||
7622 | for (i = n - 5; i >= 0; i--) { | |
7623 | if (bpf_core_is_flavor_sep(name + i)) | |
7624 | return i + 1; | |
7625 | } | |
7626 | return n; | |
7627 | } | |
fbd94c7a | 7628 | |
1e89106d AS |
7629 | struct bpf_cand_cache { |
7630 | const char *name; | |
7631 | u32 name_len; | |
7632 | u16 kind; | |
7633 | u16 cnt; | |
7634 | struct { | |
7635 | const struct btf *btf; | |
7636 | u32 id; | |
7637 | } cands[]; | |
7638 | }; | |
7639 | ||
7640 | static void bpf_free_cands(struct bpf_cand_cache *cands) | |
7641 | { | |
7642 | if (!cands->cnt) | |
7643 | /* empty candidate array was allocated on stack */ | |
7644 | return; | |
7645 | kfree(cands); | |
7646 | } | |
7647 | ||
7648 | static void bpf_free_cands_from_cache(struct bpf_cand_cache *cands) | |
7649 | { | |
7650 | kfree(cands->name); | |
7651 | kfree(cands); | |
7652 | } | |
7653 | ||
7654 | #define VMLINUX_CAND_CACHE_SIZE 31 | |
7655 | static struct bpf_cand_cache *vmlinux_cand_cache[VMLINUX_CAND_CACHE_SIZE]; | |
7656 | ||
7657 | #define MODULE_CAND_CACHE_SIZE 31 | |
7658 | static struct bpf_cand_cache *module_cand_cache[MODULE_CAND_CACHE_SIZE]; | |
7659 | ||
7660 | static DEFINE_MUTEX(cand_cache_mutex); | |
7661 | ||
7662 | static void __print_cand_cache(struct bpf_verifier_log *log, | |
7663 | struct bpf_cand_cache **cache, | |
7664 | int cache_size) | |
7665 | { | |
7666 | struct bpf_cand_cache *cc; | |
7667 | int i, j; | |
7668 | ||
7669 | for (i = 0; i < cache_size; i++) { | |
7670 | cc = cache[i]; | |
7671 | if (!cc) | |
7672 | continue; | |
7673 | bpf_log(log, "[%d]%s(", i, cc->name); | |
7674 | for (j = 0; j < cc->cnt; j++) { | |
7675 | bpf_log(log, "%d", cc->cands[j].id); | |
7676 | if (j < cc->cnt - 1) | |
7677 | bpf_log(log, " "); | |
7678 | } | |
7679 | bpf_log(log, "), "); | |
7680 | } | |
7681 | } | |
7682 | ||
7683 | static void print_cand_cache(struct bpf_verifier_log *log) | |
7684 | { | |
7685 | mutex_lock(&cand_cache_mutex); | |
7686 | bpf_log(log, "vmlinux_cand_cache:"); | |
7687 | __print_cand_cache(log, vmlinux_cand_cache, VMLINUX_CAND_CACHE_SIZE); | |
7688 | bpf_log(log, "\nmodule_cand_cache:"); | |
7689 | __print_cand_cache(log, module_cand_cache, MODULE_CAND_CACHE_SIZE); | |
7690 | bpf_log(log, "\n"); | |
7691 | mutex_unlock(&cand_cache_mutex); | |
7692 | } | |
7693 | ||
7694 | static u32 hash_cands(struct bpf_cand_cache *cands) | |
7695 | { | |
7696 | return jhash(cands->name, cands->name_len, 0); | |
7697 | } | |
7698 | ||
7699 | static struct bpf_cand_cache *check_cand_cache(struct bpf_cand_cache *cands, | |
7700 | struct bpf_cand_cache **cache, | |
7701 | int cache_size) | |
7702 | { | |
7703 | struct bpf_cand_cache *cc = cache[hash_cands(cands) % cache_size]; | |
7704 | ||
7705 | if (cc && cc->name_len == cands->name_len && | |
7706 | !strncmp(cc->name, cands->name, cands->name_len)) | |
7707 | return cc; | |
7708 | return NULL; | |
7709 | } | |
7710 | ||
7711 | static size_t sizeof_cands(int cnt) | |
7712 | { | |
7713 | return offsetof(struct bpf_cand_cache, cands[cnt]); | |
7714 | } | |
7715 | ||
7716 | static struct bpf_cand_cache *populate_cand_cache(struct bpf_cand_cache *cands, | |
7717 | struct bpf_cand_cache **cache, | |
7718 | int cache_size) | |
7719 | { | |
7720 | struct bpf_cand_cache **cc = &cache[hash_cands(cands) % cache_size], *new_cands; | |
7721 | ||
7722 | if (*cc) { | |
7723 | bpf_free_cands_from_cache(*cc); | |
7724 | *cc = NULL; | |
7725 | } | |
4674f210 | 7726 | new_cands = kmemdup(cands, sizeof_cands(cands->cnt), GFP_KERNEL); |
1e89106d AS |
7727 | if (!new_cands) { |
7728 | bpf_free_cands(cands); | |
7729 | return ERR_PTR(-ENOMEM); | |
7730 | } | |
1e89106d AS |
7731 | /* strdup the name, since it will stay in cache. |
7732 | * the cands->name points to strings in prog's BTF and the prog can be unloaded. | |
7733 | */ | |
7734 | new_cands->name = kmemdup_nul(cands->name, cands->name_len, GFP_KERNEL); | |
7735 | bpf_free_cands(cands); | |
7736 | if (!new_cands->name) { | |
7737 | kfree(new_cands); | |
7738 | return ERR_PTR(-ENOMEM); | |
7739 | } | |
7740 | *cc = new_cands; | |
7741 | return new_cands; | |
7742 | } | |
7743 | ||
29f2e5bd | 7744 | #ifdef CONFIG_DEBUG_INFO_BTF_MODULES |
1e89106d AS |
7745 | static void __purge_cand_cache(struct btf *btf, struct bpf_cand_cache **cache, |
7746 | int cache_size) | |
7747 | { | |
7748 | struct bpf_cand_cache *cc; | |
7749 | int i, j; | |
7750 | ||
7751 | for (i = 0; i < cache_size; i++) { | |
7752 | cc = cache[i]; | |
7753 | if (!cc) | |
7754 | continue; | |
7755 | if (!btf) { | |
7756 | /* when new module is loaded purge all of module_cand_cache, | |
7757 | * since new module might have candidates with the name | |
7758 | * that matches cached cands. | |
7759 | */ | |
7760 | bpf_free_cands_from_cache(cc); | |
7761 | cache[i] = NULL; | |
7762 | continue; | |
7763 | } | |
7764 | /* when module is unloaded purge cache entries | |
7765 | * that match module's btf | |
7766 | */ | |
7767 | for (j = 0; j < cc->cnt; j++) | |
7768 | if (cc->cands[j].btf == btf) { | |
7769 | bpf_free_cands_from_cache(cc); | |
7770 | cache[i] = NULL; | |
7771 | break; | |
7772 | } | |
7773 | } | |
7774 | ||
7775 | } | |
7776 | ||
7777 | static void purge_cand_cache(struct btf *btf) | |
7778 | { | |
7779 | mutex_lock(&cand_cache_mutex); | |
7780 | __purge_cand_cache(btf, module_cand_cache, MODULE_CAND_CACHE_SIZE); | |
7781 | mutex_unlock(&cand_cache_mutex); | |
7782 | } | |
29f2e5bd | 7783 | #endif |
1e89106d AS |
7784 | |
7785 | static struct bpf_cand_cache * | |
7786 | bpf_core_add_cands(struct bpf_cand_cache *cands, const struct btf *targ_btf, | |
7787 | int targ_start_id) | |
7788 | { | |
7789 | struct bpf_cand_cache *new_cands; | |
7790 | const struct btf_type *t; | |
7791 | const char *targ_name; | |
7792 | size_t targ_essent_len; | |
7793 | int n, i; | |
7794 | ||
7795 | n = btf_nr_types(targ_btf); | |
7796 | for (i = targ_start_id; i < n; i++) { | |
7797 | t = btf_type_by_id(targ_btf, i); | |
7798 | if (btf_kind(t) != cands->kind) | |
7799 | continue; | |
7800 | ||
7801 | targ_name = btf_name_by_offset(targ_btf, t->name_off); | |
7802 | if (!targ_name) | |
7803 | continue; | |
7804 | ||
7805 | /* the resched point is before strncmp to make sure that search | |
7806 | * for non-existing name will have a chance to schedule(). | |
7807 | */ | |
7808 | cond_resched(); | |
7809 | ||
7810 | if (strncmp(cands->name, targ_name, cands->name_len) != 0) | |
7811 | continue; | |
7812 | ||
7813 | targ_essent_len = bpf_core_essential_name_len(targ_name); | |
7814 | if (targ_essent_len != cands->name_len) | |
7815 | continue; | |
7816 | ||
7817 | /* most of the time there is only one candidate for a given kind+name pair */ | |
7818 | new_cands = kmalloc(sizeof_cands(cands->cnt + 1), GFP_KERNEL); | |
7819 | if (!new_cands) { | |
7820 | bpf_free_cands(cands); | |
7821 | return ERR_PTR(-ENOMEM); | |
7822 | } | |
7823 | ||
7824 | memcpy(new_cands, cands, sizeof_cands(cands->cnt)); | |
7825 | bpf_free_cands(cands); | |
7826 | cands = new_cands; | |
7827 | cands->cands[cands->cnt].btf = targ_btf; | |
7828 | cands->cands[cands->cnt].id = i; | |
7829 | cands->cnt++; | |
7830 | } | |
7831 | return cands; | |
7832 | } | |
7833 | ||
7834 | static struct bpf_cand_cache * | |
7835 | bpf_core_find_cands(struct bpf_core_ctx *ctx, u32 local_type_id) | |
7836 | { | |
7837 | struct bpf_cand_cache *cands, *cc, local_cand = {}; | |
7838 | const struct btf *local_btf = ctx->btf; | |
7839 | const struct btf_type *local_type; | |
7840 | const struct btf *main_btf; | |
7841 | size_t local_essent_len; | |
7842 | struct btf *mod_btf; | |
7843 | const char *name; | |
7844 | int id; | |
7845 | ||
7846 | main_btf = bpf_get_btf_vmlinux(); | |
7847 | if (IS_ERR(main_btf)) | |
f18a4997 | 7848 | return ERR_CAST(main_btf); |
7ada3787 KKD |
7849 | if (!main_btf) |
7850 | return ERR_PTR(-EINVAL); | |
1e89106d AS |
7851 | |
7852 | local_type = btf_type_by_id(local_btf, local_type_id); | |
7853 | if (!local_type) | |
7854 | return ERR_PTR(-EINVAL); | |
7855 | ||
7856 | name = btf_name_by_offset(local_btf, local_type->name_off); | |
7857 | if (str_is_empty(name)) | |
7858 | return ERR_PTR(-EINVAL); | |
7859 | local_essent_len = bpf_core_essential_name_len(name); | |
7860 | ||
7861 | cands = &local_cand; | |
7862 | cands->name = name; | |
7863 | cands->kind = btf_kind(local_type); | |
7864 | cands->name_len = local_essent_len; | |
7865 | ||
7866 | cc = check_cand_cache(cands, vmlinux_cand_cache, VMLINUX_CAND_CACHE_SIZE); | |
7867 | /* cands is a pointer to stack here */ | |
7868 | if (cc) { | |
7869 | if (cc->cnt) | |
7870 | return cc; | |
7871 | goto check_modules; | |
7872 | } | |
7873 | ||
7874 | /* Attempt to find target candidates in vmlinux BTF first */ | |
7875 | cands = bpf_core_add_cands(cands, main_btf, 1); | |
7876 | if (IS_ERR(cands)) | |
f18a4997 | 7877 | return ERR_CAST(cands); |
1e89106d AS |
7878 | |
7879 | /* cands is a pointer to kmalloced memory here if cands->cnt > 0 */ | |
7880 | ||
7881 | /* populate cache even when cands->cnt == 0 */ | |
7882 | cc = populate_cand_cache(cands, vmlinux_cand_cache, VMLINUX_CAND_CACHE_SIZE); | |
7883 | if (IS_ERR(cc)) | |
f18a4997 | 7884 | return ERR_CAST(cc); |
1e89106d AS |
7885 | |
7886 | /* if vmlinux BTF has any candidate, don't go for module BTFs */ | |
7887 | if (cc->cnt) | |
7888 | return cc; | |
7889 | ||
7890 | check_modules: | |
7891 | /* cands is a pointer to stack here and cands->cnt == 0 */ | |
7892 | cc = check_cand_cache(cands, module_cand_cache, MODULE_CAND_CACHE_SIZE); | |
7893 | if (cc) | |
7894 | /* if cache has it return it even if cc->cnt == 0 */ | |
7895 | return cc; | |
7896 | ||
7897 | /* If candidate is not found in vmlinux's BTF then search in module's BTFs */ | |
7898 | spin_lock_bh(&btf_idr_lock); | |
7899 | idr_for_each_entry(&btf_idr, mod_btf, id) { | |
7900 | if (!btf_is_module(mod_btf)) | |
7901 | continue; | |
7902 | /* linear search could be slow hence unlock/lock | |
7903 | * the IDR to avoiding holding it for too long | |
7904 | */ | |
7905 | btf_get(mod_btf); | |
7906 | spin_unlock_bh(&btf_idr_lock); | |
7907 | cands = bpf_core_add_cands(cands, mod_btf, btf_nr_types(main_btf)); | |
7908 | if (IS_ERR(cands)) { | |
7909 | btf_put(mod_btf); | |
f18a4997 | 7910 | return ERR_CAST(cands); |
1e89106d AS |
7911 | } |
7912 | spin_lock_bh(&btf_idr_lock); | |
7913 | btf_put(mod_btf); | |
7914 | } | |
7915 | spin_unlock_bh(&btf_idr_lock); | |
7916 | /* cands is a pointer to kmalloced memory here if cands->cnt > 0 | |
7917 | * or pointer to stack if cands->cnd == 0. | |
7918 | * Copy it into the cache even when cands->cnt == 0 and | |
7919 | * return the result. | |
7920 | */ | |
7921 | return populate_cand_cache(cands, module_cand_cache, MODULE_CAND_CACHE_SIZE); | |
7922 | } | |
7923 | ||
fbd94c7a AS |
7924 | int bpf_core_apply(struct bpf_core_ctx *ctx, const struct bpf_core_relo *relo, |
7925 | int relo_idx, void *insn) | |
7926 | { | |
1e89106d AS |
7927 | bool need_cands = relo->kind != BPF_CORE_TYPE_ID_LOCAL; |
7928 | struct bpf_core_cand_list cands = {}; | |
adb8fa19 | 7929 | struct bpf_core_relo_res targ_res; |
78c1f8d0 | 7930 | struct bpf_core_spec *specs; |
1e89106d AS |
7931 | int err; |
7932 | ||
78c1f8d0 AS |
7933 | /* ~4k of temp memory necessary to convert LLVM spec like "0:1:0:5" |
7934 | * into arrays of btf_ids of struct fields and array indices. | |
7935 | */ | |
7936 | specs = kcalloc(3, sizeof(*specs), GFP_KERNEL); | |
7937 | if (!specs) | |
7938 | return -ENOMEM; | |
7939 | ||
1e89106d AS |
7940 | if (need_cands) { |
7941 | struct bpf_cand_cache *cc; | |
7942 | int i; | |
7943 | ||
7944 | mutex_lock(&cand_cache_mutex); | |
7945 | cc = bpf_core_find_cands(ctx, relo->type_id); | |
7946 | if (IS_ERR(cc)) { | |
7947 | bpf_log(ctx->log, "target candidate search failed for %d\n", | |
7948 | relo->type_id); | |
7949 | err = PTR_ERR(cc); | |
7950 | goto out; | |
7951 | } | |
7952 | if (cc->cnt) { | |
7953 | cands.cands = kcalloc(cc->cnt, sizeof(*cands.cands), GFP_KERNEL); | |
7954 | if (!cands.cands) { | |
7955 | err = -ENOMEM; | |
7956 | goto out; | |
7957 | } | |
7958 | } | |
7959 | for (i = 0; i < cc->cnt; i++) { | |
7960 | bpf_log(ctx->log, | |
7961 | "CO-RE relocating %s %s: found target candidate [%d]\n", | |
7962 | btf_kind_str[cc->kind], cc->name, cc->cands[i].id); | |
7963 | cands.cands[i].btf = cc->cands[i].btf; | |
7964 | cands.cands[i].id = cc->cands[i].id; | |
7965 | } | |
7966 | cands.len = cc->cnt; | |
7967 | /* cand_cache_mutex needs to span the cache lookup and | |
7968 | * copy of btf pointer into bpf_core_cand_list, | |
adb8fa19 | 7969 | * since module can be unloaded while bpf_core_calc_relo_insn |
1e89106d AS |
7970 | * is working with module's btf. |
7971 | */ | |
7972 | } | |
7973 | ||
adb8fa19 MV |
7974 | err = bpf_core_calc_relo_insn((void *)ctx->log, relo, relo_idx, ctx->btf, &cands, specs, |
7975 | &targ_res); | |
7976 | if (err) | |
7977 | goto out; | |
7978 | ||
7979 | err = bpf_core_patch_insn((void *)ctx->log, insn, relo->insn_off / 8, relo, relo_idx, | |
7980 | &targ_res); | |
7981 | ||
1e89106d | 7982 | out: |
78c1f8d0 | 7983 | kfree(specs); |
1e89106d AS |
7984 | if (need_cands) { |
7985 | kfree(cands.cands); | |
7986 | mutex_unlock(&cand_cache_mutex); | |
7987 | if (ctx->log->level & BPF_LOG_LEVEL2) | |
7988 | print_cand_cache(ctx->log); | |
7989 | } | |
7990 | return err; | |
fbd94c7a | 7991 | } |