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> |
f6be98d1 | 22 | #include <linux/bpf.h> |
c0c852dd | 23 | #include <linux/bpf_lsm.h> |
91cc1a99 AS |
24 | #include <linux/skmsg.h> |
25 | #include <linux/perf_event.h> | |
eae2e83e | 26 | #include <linux/bsearch.h> |
36e68442 AN |
27 | #include <linux/kobject.h> |
28 | #include <linux/sysfs.h> | |
41948afc | 29 | #include <linux/overflow.h> |
fd9c663b FW |
30 | |
31 | #include <net/netfilter/nf_bpf_link.h> | |
32 | ||
91cc1a99 | 33 | #include <net/sock.h> |
680ee045 | 34 | #include <net/xdp.h> |
1e89106d | 35 | #include "../tools/lib/bpf/relo_core.h" |
69b693f0 MKL |
36 | |
37 | /* BTF (BPF Type Format) is the meta data format which describes | |
38 | * the data types of BPF program/map. Hence, it basically focus | |
39 | * on the C programming language which the modern BPF is primary | |
40 | * using. | |
41 | * | |
42 | * ELF Section: | |
43 | * ~~~~~~~~~~~ | |
44 | * The BTF data is stored under the ".BTF" ELF section | |
45 | * | |
46 | * struct btf_type: | |
47 | * ~~~~~~~~~~~~~~~ | |
48 | * Each 'struct btf_type' object describes a C data type. | |
49 | * Depending on the type it is describing, a 'struct btf_type' | |
50 | * object may be followed by more data. F.e. | |
51 | * To describe an array, 'struct btf_type' is followed by | |
52 | * 'struct btf_array'. | |
53 | * | |
54 | * 'struct btf_type' and any extra data following it are | |
55 | * 4 bytes aligned. | |
56 | * | |
57 | * Type section: | |
58 | * ~~~~~~~~~~~~~ | |
59 | * The BTF type section contains a list of 'struct btf_type' objects. | |
60 | * Each one describes a C type. Recall from the above section | |
61 | * that a 'struct btf_type' object could be immediately followed by extra | |
8fb33b60 | 62 | * data in order to describe some particular C types. |
69b693f0 MKL |
63 | * |
64 | * type_id: | |
65 | * ~~~~~~~ | |
66 | * Each btf_type object is identified by a type_id. The type_id | |
67 | * is implicitly implied by the location of the btf_type object in | |
68 | * the BTF type section. The first one has type_id 1. The second | |
69 | * one has type_id 2...etc. Hence, an earlier btf_type has | |
70 | * a smaller type_id. | |
71 | * | |
72 | * A btf_type object may refer to another btf_type object by using | |
73 | * type_id (i.e. the "type" in the "struct btf_type"). | |
74 | * | |
75 | * NOTE that we cannot assume any reference-order. | |
76 | * A btf_type object can refer to an earlier btf_type object | |
77 | * but it can also refer to a later btf_type object. | |
78 | * | |
79 | * For example, to describe "const void *". A btf_type | |
80 | * object describing "const" may refer to another btf_type | |
81 | * object describing "void *". This type-reference is done | |
82 | * by specifying type_id: | |
83 | * | |
84 | * [1] CONST (anon) type_id=2 | |
85 | * [2] PTR (anon) type_id=0 | |
86 | * | |
87 | * The above is the btf_verifier debug log: | |
88 | * - Each line started with "[?]" is a btf_type object | |
89 | * - [?] is the type_id of the btf_type object. | |
90 | * - CONST/PTR is the BTF_KIND_XXX | |
91 | * - "(anon)" is the name of the type. It just | |
92 | * happens that CONST and PTR has no name. | |
93 | * - type_id=XXX is the 'u32 type' in btf_type | |
94 | * | |
95 | * NOTE: "void" has type_id 0 | |
96 | * | |
97 | * String section: | |
98 | * ~~~~~~~~~~~~~~ | |
99 | * The BTF string section contains the names used by the type section. | |
100 | * Each string is referred by an "offset" from the beginning of the | |
101 | * string section. | |
102 | * | |
103 | * Each string is '\0' terminated. | |
104 | * | |
105 | * The first character in the string section must be '\0' | |
106 | * which is used to mean 'anonymous'. Some btf_type may not | |
107 | * have a name. | |
108 | */ | |
109 | ||
110 | /* BTF verification: | |
111 | * | |
112 | * To verify BTF data, two passes are needed. | |
113 | * | |
114 | * Pass #1 | |
115 | * ~~~~~~~ | |
116 | * The first pass is to collect all btf_type objects to | |
117 | * an array: "btf->types". | |
118 | * | |
119 | * Depending on the C type that a btf_type is describing, | |
120 | * a btf_type may be followed by extra data. We don't know | |
121 | * how many btf_type is there, and more importantly we don't | |
122 | * know where each btf_type is located in the type section. | |
123 | * | |
124 | * Without knowing the location of each type_id, most verifications | |
125 | * cannot be done. e.g. an earlier btf_type may refer to a later | |
126 | * btf_type (recall the "const void *" above), so we cannot | |
127 | * check this type-reference in the first pass. | |
128 | * | |
129 | * In the first pass, it still does some verifications (e.g. | |
130 | * checking the name is a valid offset to the string section). | |
eb3f595d MKL |
131 | * |
132 | * Pass #2 | |
133 | * ~~~~~~~ | |
134 | * The main focus is to resolve a btf_type that is referring | |
135 | * to another type. | |
136 | * | |
137 | * We have to ensure the referring type: | |
138 | * 1) does exist in the BTF (i.e. in btf->types[]) | |
139 | * 2) does not cause a loop: | |
140 | * struct A { | |
141 | * struct B b; | |
142 | * }; | |
143 | * | |
144 | * struct B { | |
145 | * struct A a; | |
146 | * }; | |
147 | * | |
148 | * btf_type_needs_resolve() decides if a btf_type needs | |
149 | * to be resolved. | |
150 | * | |
151 | * The needs_resolve type implements the "resolve()" ops which | |
152 | * essentially does a DFS and detects backedge. | |
153 | * | |
154 | * During resolve (or DFS), different C types have different | |
155 | * "RESOLVED" conditions. | |
156 | * | |
157 | * When resolving a BTF_KIND_STRUCT, we need to resolve all its | |
158 | * members because a member is always referring to another | |
159 | * type. A struct's member can be treated as "RESOLVED" if | |
160 | * it is referring to a BTF_KIND_PTR. Otherwise, the | |
161 | * following valid C struct would be rejected: | |
162 | * | |
163 | * struct A { | |
164 | * int m; | |
165 | * struct A *a; | |
166 | * }; | |
167 | * | |
168 | * When resolving a BTF_KIND_PTR, it needs to keep resolving if | |
169 | * it is referring to another BTF_KIND_PTR. Otherwise, we cannot | |
170 | * detect a pointer loop, e.g.: | |
171 | * BTF_KIND_CONST -> BTF_KIND_PTR -> BTF_KIND_CONST -> BTF_KIND_PTR + | |
172 | * ^ | | |
173 | * +-----------------------------------------+ | |
174 | * | |
69b693f0 MKL |
175 | */ |
176 | ||
b1e8818c | 177 | #define BITS_PER_U128 (sizeof(u64) * BITS_PER_BYTE * 2) |
69b693f0 MKL |
178 | #define BITS_PER_BYTE_MASK (BITS_PER_BYTE - 1) |
179 | #define BITS_PER_BYTE_MASKED(bits) ((bits) & BITS_PER_BYTE_MASK) | |
180 | #define BITS_ROUNDDOWN_BYTES(bits) ((bits) >> 3) | |
181 | #define BITS_ROUNDUP_BYTES(bits) \ | |
182 | (BITS_ROUNDDOWN_BYTES(bits) + !!BITS_PER_BYTE_MASKED(bits)) | |
183 | ||
b1828f0b | 184 | #define BTF_INFO_MASK 0x9f00ffff |
aea2f7b8 MKL |
185 | #define BTF_INT_MASK 0x0fffffff |
186 | #define BTF_TYPE_ID_VALID(type_id) ((type_id) <= BTF_MAX_TYPE) | |
187 | #define BTF_STR_OFFSET_VALID(name_off) ((name_off) <= BTF_MAX_NAME_OFFSET) | |
188 | ||
69b693f0 MKL |
189 | /* 16MB for 64k structs and each has 16 members and |
190 | * a few MB spaces for the string section. | |
191 | * The hard limit is S32_MAX. | |
192 | */ | |
193 | #define BTF_MAX_SIZE (16 * 1024 * 1024) | |
69b693f0 | 194 | |
eb3f595d MKL |
195 | #define for_each_member_from(i, from, struct_type, member) \ |
196 | for (i = from, member = btf_type_member(struct_type) + from; \ | |
197 | i < btf_type_vlen(struct_type); \ | |
198 | i++, member++) | |
199 | ||
1dc92851 DB |
200 | #define for_each_vsi_from(i, from, struct_type, member) \ |
201 | for (i = from, member = btf_type_var_secinfo(struct_type) + from; \ | |
202 | i < btf_type_vlen(struct_type); \ | |
203 | i++, member++) | |
204 | ||
1b9ed84e QM |
205 | DEFINE_IDR(btf_idr); |
206 | DEFINE_SPINLOCK(btf_idr_lock); | |
78958fca | 207 | |
dee872e1 | 208 | enum btf_kfunc_hook { |
cfe14564 | 209 | BTF_KFUNC_HOOK_COMMON, |
dee872e1 KKD |
210 | BTF_KFUNC_HOOK_XDP, |
211 | BTF_KFUNC_HOOK_TC, | |
212 | BTF_KFUNC_HOOK_STRUCT_OPS, | |
97949767 BT |
213 | BTF_KFUNC_HOOK_TRACING, |
214 | BTF_KFUNC_HOOK_SYSCALL, | |
5b481aca | 215 | BTF_KFUNC_HOOK_FMODRET, |
67666479 | 216 | BTF_KFUNC_HOOK_CGROUP, |
b5964b96 JK |
217 | BTF_KFUNC_HOOK_SCHED_ACT, |
218 | BTF_KFUNC_HOOK_SK_SKB, | |
219 | BTF_KFUNC_HOOK_SOCKET_FILTER, | |
220 | BTF_KFUNC_HOOK_LWT, | |
fd9c663b | 221 | BTF_KFUNC_HOOK_NETFILTER, |
adf46d88 | 222 | BTF_KFUNC_HOOK_KPROBE, |
dee872e1 KKD |
223 | BTF_KFUNC_HOOK_MAX, |
224 | }; | |
225 | ||
226 | enum { | |
f9b34818 | 227 | BTF_KFUNC_SET_MAX_CNT = 256, |
5ce937d6 | 228 | BTF_DTOR_KFUNC_MAX_CNT = 256, |
e924e80e AG |
229 | BTF_KFUNC_FILTER_MAX_CNT = 16, |
230 | }; | |
231 | ||
232 | struct btf_kfunc_hook_filter { | |
233 | btf_kfunc_filter_t filters[BTF_KFUNC_FILTER_MAX_CNT]; | |
234 | u32 nr_filters; | |
dee872e1 KKD |
235 | }; |
236 | ||
237 | struct btf_kfunc_set_tab { | |
a4703e31 | 238 | struct btf_id_set8 *sets[BTF_KFUNC_HOOK_MAX]; |
e924e80e | 239 | struct btf_kfunc_hook_filter hook_filters[BTF_KFUNC_HOOK_MAX]; |
dee872e1 KKD |
240 | }; |
241 | ||
5ce937d6 KKD |
242 | struct btf_id_dtor_kfunc_tab { |
243 | u32 cnt; | |
244 | struct btf_id_dtor_kfunc dtors[]; | |
245 | }; | |
246 | ||
e6199511 KFL |
247 | struct btf_struct_ops_tab { |
248 | u32 cnt; | |
249 | u32 capacity; | |
250 | struct bpf_struct_ops_desc ops[]; | |
251 | }; | |
252 | ||
69b693f0 | 253 | struct btf { |
f80442a4 | 254 | void *data; |
69b693f0 | 255 | struct btf_type **types; |
eb3f595d MKL |
256 | u32 *resolved_ids; |
257 | u32 *resolved_sizes; | |
69b693f0 MKL |
258 | const char *strings; |
259 | void *nohdr_data; | |
f80442a4 | 260 | struct btf_header hdr; |
951bb646 | 261 | u32 nr_types; /* includes VOID for base BTF */ |
69b693f0 MKL |
262 | u32 types_size; |
263 | u32 data_size; | |
f56a653c | 264 | refcount_t refcnt; |
78958fca MKL |
265 | u32 id; |
266 | struct rcu_head rcu; | |
dee872e1 | 267 | struct btf_kfunc_set_tab *kfunc_set_tab; |
5ce937d6 | 268 | struct btf_id_dtor_kfunc_tab *dtor_kfunc_tab; |
8ffa5cc1 | 269 | struct btf_struct_metas *struct_meta_tab; |
e6199511 | 270 | struct btf_struct_ops_tab *struct_ops_tab; |
951bb646 AN |
271 | |
272 | /* split BTF support */ | |
273 | struct btf *base_btf; | |
274 | u32 start_id; /* first type ID in this BTF (0 for base BTF) */ | |
275 | u32 start_str_off; /* first string offset (0 for base BTF) */ | |
53297220 AN |
276 | char name[MODULE_NAME_LEN]; |
277 | bool kernel_btf; | |
8646db23 | 278 | __u32 *base_id_map; /* map from distilled base BTF -> vmlinux BTF ids */ |
69b693f0 MKL |
279 | }; |
280 | ||
eb3f595d MKL |
281 | enum verifier_phase { |
282 | CHECK_META, | |
283 | CHECK_TYPE, | |
284 | }; | |
285 | ||
286 | struct resolve_vertex { | |
287 | const struct btf_type *t; | |
288 | u32 type_id; | |
289 | u16 next_member; | |
290 | }; | |
291 | ||
292 | enum visit_state { | |
293 | NOT_VISITED, | |
294 | VISITED, | |
295 | RESOLVED, | |
296 | }; | |
297 | ||
298 | enum resolve_mode { | |
299 | RESOLVE_TBD, /* To Be Determined */ | |
300 | RESOLVE_PTR, /* Resolving for Pointer */ | |
301 | RESOLVE_STRUCT_OR_ARRAY, /* Resolving for struct/union | |
302 | * or array | |
303 | */ | |
304 | }; | |
305 | ||
306 | #define MAX_RESOLVE_DEPTH 32 | |
307 | ||
f80442a4 MKL |
308 | struct btf_sec_info { |
309 | u32 off; | |
310 | u32 len; | |
311 | }; | |
312 | ||
69b693f0 MKL |
313 | struct btf_verifier_env { |
314 | struct btf *btf; | |
eb3f595d MKL |
315 | u8 *visit_states; |
316 | struct resolve_vertex stack[MAX_RESOLVE_DEPTH]; | |
69b693f0 MKL |
317 | struct bpf_verifier_log log; |
318 | u32 log_type_id; | |
eb3f595d MKL |
319 | u32 top_stack; |
320 | enum verifier_phase phase; | |
321 | enum resolve_mode resolve_mode; | |
69b693f0 MKL |
322 | }; |
323 | ||
324 | static const char * const btf_kind_str[NR_BTF_KINDS] = { | |
325 | [BTF_KIND_UNKN] = "UNKNOWN", | |
326 | [BTF_KIND_INT] = "INT", | |
327 | [BTF_KIND_PTR] = "PTR", | |
328 | [BTF_KIND_ARRAY] = "ARRAY", | |
329 | [BTF_KIND_STRUCT] = "STRUCT", | |
330 | [BTF_KIND_UNION] = "UNION", | |
331 | [BTF_KIND_ENUM] = "ENUM", | |
332 | [BTF_KIND_FWD] = "FWD", | |
333 | [BTF_KIND_TYPEDEF] = "TYPEDEF", | |
334 | [BTF_KIND_VOLATILE] = "VOLATILE", | |
335 | [BTF_KIND_CONST] = "CONST", | |
336 | [BTF_KIND_RESTRICT] = "RESTRICT", | |
2667a262 MKL |
337 | [BTF_KIND_FUNC] = "FUNC", |
338 | [BTF_KIND_FUNC_PROTO] = "FUNC_PROTO", | |
1dc92851 DB |
339 | [BTF_KIND_VAR] = "VAR", |
340 | [BTF_KIND_DATASEC] = "DATASEC", | |
b1828f0b | 341 | [BTF_KIND_FLOAT] = "FLOAT", |
223f903e | 342 | [BTF_KIND_DECL_TAG] = "DECL_TAG", |
8c42d2fa | 343 | [BTF_KIND_TYPE_TAG] = "TYPE_TAG", |
6089fb32 | 344 | [BTF_KIND_ENUM64] = "ENUM64", |
69b693f0 MKL |
345 | }; |
346 | ||
e6ac2450 | 347 | const char *btf_type_str(const struct btf_type *t) |
be8704ff AS |
348 | { |
349 | return btf_kind_str[BTF_INFO_KIND(t->info)]; | |
350 | } | |
351 | ||
31d0bc81 AM |
352 | /* Chunk size we use in safe copy of data to be shown. */ |
353 | #define BTF_SHOW_OBJ_SAFE_SIZE 32 | |
354 | ||
355 | /* | |
356 | * This is the maximum size of a base type value (equivalent to a | |
357 | * 128-bit int); if we are at the end of our safe buffer and have | |
358 | * less than 16 bytes space we can't be assured of being able | |
359 | * to copy the next type safely, so in such cases we will initiate | |
360 | * a new copy. | |
361 | */ | |
362 | #define BTF_SHOW_OBJ_BASE_TYPE_SIZE 16 | |
363 | ||
364 | /* Type name size */ | |
365 | #define BTF_SHOW_NAME_SIZE 80 | |
366 | ||
b613d335 DV |
367 | /* |
368 | * The suffix of a type that indicates it cannot alias another type when | |
369 | * comparing BTF IDs for kfunc invocations. | |
370 | */ | |
371 | #define NOCAST_ALIAS_SUFFIX "___init" | |
372 | ||
31d0bc81 AM |
373 | /* |
374 | * Common data to all BTF show operations. Private show functions can add | |
375 | * their own data to a structure containing a struct btf_show and consult it | |
376 | * in the show callback. See btf_type_show() below. | |
377 | * | |
378 | * One challenge with showing nested data is we want to skip 0-valued | |
379 | * data, but in order to figure out whether a nested object is all zeros | |
380 | * we need to walk through it. As a result, we need to make two passes | |
381 | * when handling structs, unions and arrays; the first path simply looks | |
382 | * for nonzero data, while the second actually does the display. The first | |
383 | * pass is signalled by show->state.depth_check being set, and if we | |
384 | * encounter a non-zero value we set show->state.depth_to_show to | |
385 | * the depth at which we encountered it. When we have completed the | |
386 | * first pass, we will know if anything needs to be displayed if | |
387 | * depth_to_show > depth. See btf_[struct,array]_show() for the | |
388 | * implementation of this. | |
389 | * | |
390 | * Another problem is we want to ensure the data for display is safe to | |
391 | * access. To support this, the anonymous "struct {} obj" tracks the data | |
392 | * object and our safe copy of it. We copy portions of the data needed | |
393 | * to the object "copy" buffer, but because its size is limited to | |
394 | * BTF_SHOW_OBJ_COPY_LEN bytes, multiple copies may be required as we | |
395 | * traverse larger objects for display. | |
396 | * | |
397 | * The various data type show functions all start with a call to | |
398 | * btf_show_start_type() which returns a pointer to the safe copy | |
399 | * of the data needed (or if BTF_SHOW_UNSAFE is specified, to the | |
400 | * raw data itself). btf_show_obj_safe() is responsible for | |
401 | * using copy_from_kernel_nofault() to update the safe data if necessary | |
402 | * as we traverse the object's data. skbuff-like semantics are | |
403 | * used: | |
404 | * | |
405 | * - obj.head points to the start of the toplevel object for display | |
406 | * - obj.size is the size of the toplevel object | |
407 | * - obj.data points to the current point in the original data at | |
408 | * which our safe data starts. obj.data will advance as we copy | |
409 | * portions of the data. | |
410 | * | |
411 | * In most cases a single copy will suffice, but larger data structures | |
412 | * such as "struct task_struct" will require many copies. The logic in | |
413 | * btf_show_obj_safe() handles the logic that determines if a new | |
414 | * copy_from_kernel_nofault() is needed. | |
415 | */ | |
416 | struct btf_show { | |
417 | u64 flags; | |
418 | void *target; /* target of show operation (seq file, buffer) */ | |
2454075f | 419 | __printf(2, 0) void (*showfn)(struct btf_show *show, const char *fmt, va_list args); |
31d0bc81 AM |
420 | const struct btf *btf; |
421 | /* below are used during iteration */ | |
422 | struct { | |
423 | u8 depth; | |
424 | u8 depth_to_show; | |
425 | u8 depth_check; | |
426 | u8 array_member:1, | |
427 | array_terminated:1; | |
428 | u16 array_encoding; | |
429 | u32 type_id; | |
430 | int status; /* non-zero for error */ | |
431 | const struct btf_type *type; | |
432 | const struct btf_member *member; | |
433 | char name[BTF_SHOW_NAME_SIZE]; /* space for member name/type */ | |
434 | } state; | |
435 | struct { | |
436 | u32 size; | |
437 | void *head; | |
438 | void *data; | |
439 | u8 safe[BTF_SHOW_OBJ_SAFE_SIZE]; | |
440 | } obj; | |
441 | }; | |
442 | ||
69b693f0 MKL |
443 | struct btf_kind_operations { |
444 | s32 (*check_meta)(struct btf_verifier_env *env, | |
445 | const struct btf_type *t, | |
446 | u32 meta_left); | |
eb3f595d MKL |
447 | int (*resolve)(struct btf_verifier_env *env, |
448 | const struct resolve_vertex *v); | |
179cde8c MKL |
449 | int (*check_member)(struct btf_verifier_env *env, |
450 | const struct btf_type *struct_type, | |
451 | const struct btf_member *member, | |
452 | const struct btf_type *member_type); | |
9d5f9f70 YS |
453 | int (*check_kflag_member)(struct btf_verifier_env *env, |
454 | const struct btf_type *struct_type, | |
455 | const struct btf_member *member, | |
456 | const struct btf_type *member_type); | |
69b693f0 MKL |
457 | void (*log_details)(struct btf_verifier_env *env, |
458 | const struct btf_type *t); | |
31d0bc81 | 459 | void (*show)(const struct btf *btf, const struct btf_type *t, |
b00b8dae | 460 | u32 type_id, void *data, u8 bits_offsets, |
31d0bc81 | 461 | struct btf_show *show); |
69b693f0 MKL |
462 | }; |
463 | ||
464 | static const struct btf_kind_operations * const kind_ops[NR_BTF_KINDS]; | |
465 | static struct btf_type btf_void; | |
466 | ||
2667a262 MKL |
467 | static int btf_resolve(struct btf_verifier_env *env, |
468 | const struct btf_type *t, u32 type_id); | |
469 | ||
d7e7b42f YS |
470 | static int btf_func_check(struct btf_verifier_env *env, |
471 | const struct btf_type *t); | |
472 | ||
eb3f595d MKL |
473 | static bool btf_type_is_modifier(const struct btf_type *t) |
474 | { | |
475 | /* Some of them is not strictly a C modifier | |
476 | * but they are grouped into the same bucket | |
477 | * for BTF concern: | |
478 | * A type (t) that refers to another | |
479 | * type through t->type AND its size cannot | |
480 | * be determined without following the t->type. | |
481 | * | |
482 | * ptr does not fall into this bucket | |
483 | * because its size is always sizeof(void *). | |
484 | */ | |
485 | switch (BTF_INFO_KIND(t->info)) { | |
486 | case BTF_KIND_TYPEDEF: | |
487 | case BTF_KIND_VOLATILE: | |
488 | case BTF_KIND_CONST: | |
489 | case BTF_KIND_RESTRICT: | |
8c42d2fa | 490 | case BTF_KIND_TYPE_TAG: |
eb3f595d MKL |
491 | return true; |
492 | } | |
493 | ||
494 | return false; | |
495 | } | |
496 | ||
2824ecb7 | 497 | bool btf_type_is_void(const struct btf_type *t) |
eb3f595d | 498 | { |
b47a0bd2 MKL |
499 | return t == &btf_void; |
500 | } | |
501 | ||
e6c2f594 | 502 | static bool btf_type_is_datasec(const struct btf_type *t) |
b47a0bd2 | 503 | { |
e6c2f594 | 504 | return BTF_INFO_KIND(t->info) == BTF_KIND_DATASEC; |
eb3f595d MKL |
505 | } |
506 | ||
e6c2f594 | 507 | static bool btf_type_is_decl_tag(const struct btf_type *t) |
eb3f595d | 508 | { |
e6c2f594 | 509 | return BTF_INFO_KIND(t->info) == BTF_KIND_DECL_TAG; |
eb3f595d MKL |
510 | } |
511 | ||
e6c2f594 | 512 | static bool btf_type_nosize(const struct btf_type *t) |
1dc92851 | 513 | { |
e6c2f594 YS |
514 | return btf_type_is_void(t) || btf_type_is_fwd(t) || |
515 | btf_type_is_func(t) || btf_type_is_func_proto(t) || | |
516 | btf_type_is_decl_tag(t); | |
1dc92851 DB |
517 | } |
518 | ||
e6c2f594 | 519 | static bool btf_type_nosize_or_null(const struct btf_type *t) |
b5ea834d | 520 | { |
e6c2f594 | 521 | return !t || btf_type_nosize(t); |
b5ea834d YS |
522 | } |
523 | ||
223f903e | 524 | static bool btf_type_is_decl_tag_target(const struct btf_type *t) |
b5ea834d YS |
525 | { |
526 | return btf_type_is_func(t) || btf_type_is_struct(t) || | |
bd16dee6 | 527 | btf_type_is_var(t) || btf_type_is_typedef(t); |
b5ea834d YS |
528 | } |
529 | ||
8646db23 AM |
530 | bool btf_is_vmlinux(const struct btf *btf) |
531 | { | |
532 | return btf->kernel_btf && !btf->base_btf; | |
533 | } | |
534 | ||
541c3bad | 535 | u32 btf_nr_types(const struct btf *btf) |
951bb646 AN |
536 | { |
537 | u32 total = 0; | |
538 | ||
539 | while (btf) { | |
540 | total += btf->nr_types; | |
541 | btf = btf->base_btf; | |
542 | } | |
543 | ||
544 | return total; | |
545 | } | |
546 | ||
27ae7997 MKL |
547 | s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind) |
548 | { | |
549 | const struct btf_type *t; | |
550 | const char *tname; | |
951bb646 | 551 | u32 i, total; |
27ae7997 | 552 | |
541c3bad | 553 | total = btf_nr_types(btf); |
951bb646 AN |
554 | for (i = 1; i < total; i++) { |
555 | t = btf_type_by_id(btf, i); | |
27ae7997 MKL |
556 | if (BTF_INFO_KIND(t->info) != kind) |
557 | continue; | |
558 | ||
559 | tname = btf_name_by_offset(btf, t->name_off); | |
560 | if (!strcmp(tname, name)) | |
561 | return i; | |
562 | } | |
563 | ||
564 | return -ENOENT; | |
565 | } | |
566 | ||
b1d1e904 | 567 | s32 bpf_find_btf_id(const char *name, u32 kind, struct btf **btf_p) |
edc3ec09 KKD |
568 | { |
569 | struct btf *btf; | |
570 | s32 ret; | |
571 | int id; | |
572 | ||
573 | btf = bpf_get_btf_vmlinux(); | |
574 | if (IS_ERR(btf)) | |
575 | return PTR_ERR(btf); | |
7ada3787 KKD |
576 | if (!btf) |
577 | return -EINVAL; | |
edc3ec09 KKD |
578 | |
579 | ret = btf_find_by_name_kind(btf, name, kind); | |
580 | /* ret is never zero, since btf_find_by_name_kind returns | |
581 | * positive btf_id or negative error. | |
582 | */ | |
583 | if (ret > 0) { | |
584 | btf_get(btf); | |
585 | *btf_p = btf; | |
586 | return ret; | |
587 | } | |
588 | ||
589 | /* If name is not found in vmlinux's BTF then search in module's BTFs */ | |
590 | spin_lock_bh(&btf_idr_lock); | |
591 | idr_for_each_entry(&btf_idr, btf, id) { | |
592 | if (!btf_is_module(btf)) | |
593 | continue; | |
594 | /* linear search could be slow hence unlock/lock | |
595 | * the IDR to avoiding holding it for too long | |
596 | */ | |
597 | btf_get(btf); | |
598 | spin_unlock_bh(&btf_idr_lock); | |
599 | ret = btf_find_by_name_kind(btf, name, kind); | |
600 | if (ret > 0) { | |
601 | *btf_p = btf; | |
602 | return ret; | |
603 | } | |
edc3ec09 | 604 | btf_put(btf); |
acf1c3d6 | 605 | spin_lock_bh(&btf_idr_lock); |
edc3ec09 KKD |
606 | } |
607 | spin_unlock_bh(&btf_idr_lock); | |
608 | return ret; | |
609 | } | |
4e4136c6 | 610 | EXPORT_SYMBOL_GPL(bpf_find_btf_id); |
edc3ec09 | 611 | |
27ae7997 MKL |
612 | const struct btf_type *btf_type_skip_modifiers(const struct btf *btf, |
613 | u32 id, u32 *res_id) | |
614 | { | |
615 | const struct btf_type *t = btf_type_by_id(btf, id); | |
616 | ||
617 | while (btf_type_is_modifier(t)) { | |
618 | id = t->type; | |
619 | t = btf_type_by_id(btf, t->type); | |
620 | } | |
621 | ||
622 | if (res_id) | |
623 | *res_id = id; | |
624 | ||
625 | return t; | |
626 | } | |
627 | ||
628 | const struct btf_type *btf_type_resolve_ptr(const struct btf *btf, | |
629 | u32 id, u32 *res_id) | |
630 | { | |
631 | const struct btf_type *t; | |
632 | ||
633 | t = btf_type_skip_modifiers(btf, id, NULL); | |
634 | if (!btf_type_is_ptr(t)) | |
635 | return NULL; | |
636 | ||
637 | return btf_type_skip_modifiers(btf, t->type, res_id); | |
638 | } | |
639 | ||
640 | const struct btf_type *btf_type_resolve_func_ptr(const struct btf *btf, | |
641 | u32 id, u32 *res_id) | |
642 | { | |
643 | const struct btf_type *ptype; | |
644 | ||
645 | ptype = btf_type_resolve_ptr(btf, id, res_id); | |
646 | if (ptype && btf_type_is_func_proto(ptype)) | |
647 | return ptype; | |
648 | ||
649 | return NULL; | |
650 | } | |
651 | ||
1dc92851 DB |
652 | /* Types that act only as a source, not sink or intermediate |
653 | * type when resolving. | |
654 | */ | |
655 | static bool btf_type_is_resolve_source_only(const struct btf_type *t) | |
656 | { | |
657 | return btf_type_is_var(t) || | |
223f903e | 658 | btf_type_is_decl_tag(t) || |
1dc92851 DB |
659 | btf_type_is_datasec(t); |
660 | } | |
661 | ||
eb3f595d MKL |
662 | /* What types need to be resolved? |
663 | * | |
664 | * btf_type_is_modifier() is an obvious one. | |
665 | * | |
666 | * btf_type_is_struct() because its member refers to | |
667 | * another type (through member->type). | |
1dc92851 DB |
668 | * |
669 | * btf_type_is_var() because the variable refers to | |
670 | * another type. btf_type_is_datasec() holds multiple | |
671 | * btf_type_is_var() types that need resolving. | |
672 | * | |
eb3f595d MKL |
673 | * btf_type_is_array() because its element (array->type) |
674 | * refers to another type. Array can be thought of a | |
675 | * special case of struct while array just has the same | |
676 | * member-type repeated by array->nelems of times. | |
677 | */ | |
678 | static bool btf_type_needs_resolve(const struct btf_type *t) | |
679 | { | |
680 | return btf_type_is_modifier(t) || | |
1dc92851 DB |
681 | btf_type_is_ptr(t) || |
682 | btf_type_is_struct(t) || | |
683 | btf_type_is_array(t) || | |
684 | btf_type_is_var(t) || | |
d7e7b42f | 685 | btf_type_is_func(t) || |
223f903e | 686 | btf_type_is_decl_tag(t) || |
1dc92851 | 687 | btf_type_is_datasec(t); |
eb3f595d MKL |
688 | } |
689 | ||
690 | /* t->size can be used */ | |
691 | static bool btf_type_has_size(const struct btf_type *t) | |
692 | { | |
693 | switch (BTF_INFO_KIND(t->info)) { | |
694 | case BTF_KIND_INT: | |
695 | case BTF_KIND_STRUCT: | |
696 | case BTF_KIND_UNION: | |
697 | case BTF_KIND_ENUM: | |
1dc92851 | 698 | case BTF_KIND_DATASEC: |
b1828f0b | 699 | case BTF_KIND_FLOAT: |
6089fb32 | 700 | case BTF_KIND_ENUM64: |
eb3f595d MKL |
701 | return true; |
702 | } | |
703 | ||
704 | return false; | |
705 | } | |
706 | ||
69b693f0 MKL |
707 | static const char *btf_int_encoding_str(u8 encoding) |
708 | { | |
709 | if (encoding == 0) | |
710 | return "(none)"; | |
711 | else if (encoding == BTF_INT_SIGNED) | |
712 | return "SIGNED"; | |
713 | else if (encoding == BTF_INT_CHAR) | |
714 | return "CHAR"; | |
715 | else if (encoding == BTF_INT_BOOL) | |
716 | return "BOOL"; | |
69b693f0 MKL |
717 | else |
718 | return "UNKN"; | |
719 | } | |
720 | ||
69b693f0 MKL |
721 | static u32 btf_type_int(const struct btf_type *t) |
722 | { | |
723 | return *(u32 *)(t + 1); | |
724 | } | |
725 | ||
726 | static const struct btf_array *btf_type_array(const struct btf_type *t) | |
727 | { | |
728 | return (const struct btf_array *)(t + 1); | |
729 | } | |
730 | ||
69b693f0 MKL |
731 | static const struct btf_enum *btf_type_enum(const struct btf_type *t) |
732 | { | |
733 | return (const struct btf_enum *)(t + 1); | |
734 | } | |
735 | ||
1dc92851 DB |
736 | static const struct btf_var *btf_type_var(const struct btf_type *t) |
737 | { | |
738 | return (const struct btf_var *)(t + 1); | |
739 | } | |
740 | ||
223f903e | 741 | static const struct btf_decl_tag *btf_type_decl_tag(const struct btf_type *t) |
b5ea834d | 742 | { |
223f903e | 743 | return (const struct btf_decl_tag *)(t + 1); |
b5ea834d YS |
744 | } |
745 | ||
6089fb32 YS |
746 | static const struct btf_enum64 *btf_type_enum64(const struct btf_type *t) |
747 | { | |
748 | return (const struct btf_enum64 *)(t + 1); | |
749 | } | |
750 | ||
69b693f0 MKL |
751 | static const struct btf_kind_operations *btf_type_ops(const struct btf_type *t) |
752 | { | |
753 | return kind_ops[BTF_INFO_KIND(t->info)]; | |
754 | } | |
755 | ||
583c5318 | 756 | static bool btf_name_offset_valid(const struct btf *btf, u32 offset) |
69b693f0 | 757 | { |
951bb646 AN |
758 | if (!BTF_STR_OFFSET_VALID(offset)) |
759 | return false; | |
760 | ||
761 | while (offset < btf->start_str_off) | |
762 | btf = btf->base_btf; | |
763 | ||
764 | offset -= btf->start_str_off; | |
765 | return offset < btf->hdr.str_len; | |
69b693f0 MKL |
766 | } |
767 | ||
9724160b | 768 | static bool __btf_name_char_ok(char c, bool first) |
1dc92851 DB |
769 | { |
770 | if ((first ? !isalpha(c) : | |
771 | !isalnum(c)) && | |
772 | c != '_' && | |
9724160b | 773 | c != '.') |
1dc92851 DB |
774 | return false; |
775 | return true; | |
776 | } | |
777 | ||
8646db23 | 778 | const char *btf_str_by_offset(const struct btf *btf, u32 offset) |
951bb646 AN |
779 | { |
780 | while (offset < btf->start_str_off) | |
781 | btf = btf->base_btf; | |
782 | ||
783 | offset -= btf->start_str_off; | |
784 | if (offset < btf->hdr.str_len) | |
785 | return &btf->strings[offset]; | |
786 | ||
787 | return NULL; | |
788 | } | |
789 | ||
febb6f3e | 790 | static bool btf_name_valid_identifier(const struct btf *btf, u32 offset) |
2667a262 MKL |
791 | { |
792 | /* offset must be valid */ | |
951bb646 | 793 | const char *src = btf_str_by_offset(btf, offset); |
2667a262 MKL |
794 | const char *src_limit; |
795 | ||
9724160b | 796 | if (!__btf_name_char_ok(*src, true)) |
2667a262 MKL |
797 | return false; |
798 | ||
799 | /* set a limit on identifier length */ | |
800 | src_limit = src + KSYM_NAME_LEN; | |
801 | src++; | |
802 | while (*src && src < src_limit) { | |
9724160b | 803 | if (!__btf_name_char_ok(*src, false)) |
2667a262 MKL |
804 | return false; |
805 | src++; | |
806 | } | |
807 | ||
808 | return !*src; | |
809 | } | |
810 | ||
bd70a8fb | 811 | /* Allow any printable character in DATASEC names */ |
1dc92851 DB |
812 | static bool btf_name_valid_section(const struct btf *btf, u32 offset) |
813 | { | |
bd70a8fb EZ |
814 | /* offset must be valid */ |
815 | const char *src = btf_str_by_offset(btf, offset); | |
816 | const char *src_limit; | |
817 | ||
bb6705c3 JP |
818 | if (!*src) |
819 | return false; | |
820 | ||
bd70a8fb EZ |
821 | /* set a limit on identifier length */ |
822 | src_limit = src + KSYM_NAME_LEN; | |
bd70a8fb EZ |
823 | while (*src && src < src_limit) { |
824 | if (!isprint(*src)) | |
825 | return false; | |
826 | src++; | |
827 | } | |
828 | ||
829 | return !*src; | |
1dc92851 DB |
830 | } |
831 | ||
23127b33 | 832 | static const char *__btf_name_by_offset(const struct btf *btf, u32 offset) |
69b693f0 | 833 | { |
951bb646 AN |
834 | const char *name; |
835 | ||
aea2f7b8 | 836 | if (!offset) |
69b693f0 | 837 | return "(anon)"; |
951bb646 AN |
838 | |
839 | name = btf_str_by_offset(btf, offset); | |
840 | return name ?: "(invalid-name-offset)"; | |
69b693f0 MKL |
841 | } |
842 | ||
23127b33 MKL |
843 | const char *btf_name_by_offset(const struct btf *btf, u32 offset) |
844 | { | |
951bb646 | 845 | return btf_str_by_offset(btf, offset); |
23127b33 MKL |
846 | } |
847 | ||
838e9690 | 848 | const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id) |
eb3f595d | 849 | { |
951bb646 AN |
850 | while (type_id < btf->start_id) |
851 | btf = btf->base_btf; | |
eb3f595d | 852 | |
951bb646 AN |
853 | type_id -= btf->start_id; |
854 | if (type_id >= btf->nr_types) | |
855 | return NULL; | |
eb3f595d MKL |
856 | return btf->types[type_id]; |
857 | } | |
84c6ac41 | 858 | EXPORT_SYMBOL_GPL(btf_type_by_id); |
eb3f595d | 859 | |
4ef5f574 MKL |
860 | /* |
861 | * Regular int is not a bit field and it must be either | |
b1e8818c | 862 | * u8/u16/u32/u64 or __int128. |
4ef5f574 MKL |
863 | */ |
864 | static bool btf_type_int_is_regular(const struct btf_type *t) | |
865 | { | |
36fc3c8c | 866 | u8 nr_bits, nr_bytes; |
4ef5f574 MKL |
867 | u32 int_data; |
868 | ||
869 | int_data = btf_type_int(t); | |
870 | nr_bits = BTF_INT_BITS(int_data); | |
871 | nr_bytes = BITS_ROUNDUP_BYTES(nr_bits); | |
872 | if (BITS_PER_BYTE_MASKED(nr_bits) || | |
873 | BTF_INT_OFFSET(int_data) || | |
874 | (nr_bytes != sizeof(u8) && nr_bytes != sizeof(u16) && | |
b1e8818c YS |
875 | nr_bytes != sizeof(u32) && nr_bytes != sizeof(u64) && |
876 | nr_bytes != (2 * sizeof(u64)))) { | |
4ef5f574 MKL |
877 | return false; |
878 | } | |
879 | ||
880 | return true; | |
881 | } | |
882 | ||
9a1126b6 | 883 | /* |
ffa0c1cf YS |
884 | * Check that given struct member is a regular int with expected |
885 | * offset and size. | |
9a1126b6 | 886 | */ |
ffa0c1cf YS |
887 | bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s, |
888 | const struct btf_member *m, | |
889 | u32 expected_offset, u32 expected_size) | |
9a1126b6 | 890 | { |
ffa0c1cf YS |
891 | const struct btf_type *t; |
892 | u32 id, int_data; | |
893 | u8 nr_bits; | |
9a1126b6 | 894 | |
ffa0c1cf YS |
895 | id = m->type; |
896 | t = btf_type_id_size(btf, &id, NULL); | |
897 | if (!t || !btf_type_is_int(t)) | |
9a1126b6 RG |
898 | return false; |
899 | ||
900 | int_data = btf_type_int(t); | |
901 | nr_bits = BTF_INT_BITS(int_data); | |
ffa0c1cf YS |
902 | if (btf_type_kflag(s)) { |
903 | u32 bitfield_size = BTF_MEMBER_BITFIELD_SIZE(m->offset); | |
904 | u32 bit_offset = BTF_MEMBER_BIT_OFFSET(m->offset); | |
905 | ||
906 | /* if kflag set, int should be a regular int and | |
907 | * bit offset should be at byte boundary. | |
908 | */ | |
909 | return !bitfield_size && | |
910 | BITS_ROUNDUP_BYTES(bit_offset) == expected_offset && | |
911 | BITS_ROUNDUP_BYTES(nr_bits) == expected_size; | |
912 | } | |
913 | ||
914 | if (BTF_INT_OFFSET(int_data) || | |
915 | BITS_PER_BYTE_MASKED(m->offset) || | |
916 | BITS_ROUNDUP_BYTES(m->offset) != expected_offset || | |
917 | BITS_PER_BYTE_MASKED(nr_bits) || | |
918 | BITS_ROUNDUP_BYTES(nr_bits) != expected_size) | |
9a1126b6 RG |
919 | return false; |
920 | ||
921 | return true; | |
922 | } | |
923 | ||
31d0bc81 AM |
924 | /* Similar to btf_type_skip_modifiers() but does not skip typedefs. */ |
925 | static const struct btf_type *btf_type_skip_qualifiers(const struct btf *btf, | |
926 | u32 id) | |
927 | { | |
928 | const struct btf_type *t = btf_type_by_id(btf, id); | |
929 | ||
930 | while (btf_type_is_modifier(t) && | |
931 | BTF_INFO_KIND(t->info) != BTF_KIND_TYPEDEF) { | |
31d0bc81 AM |
932 | t = btf_type_by_id(btf, t->type); |
933 | } | |
934 | ||
935 | return t; | |
936 | } | |
937 | ||
938 | #define BTF_SHOW_MAX_ITER 10 | |
939 | ||
940 | #define BTF_KIND_BIT(kind) (1ULL << kind) | |
941 | ||
942 | /* | |
943 | * Populate show->state.name with type name information. | |
944 | * Format of type name is | |
945 | * | |
946 | * [.member_name = ] (type_name) | |
947 | */ | |
948 | static const char *btf_show_name(struct btf_show *show) | |
949 | { | |
950 | /* BTF_MAX_ITER array suffixes "[]" */ | |
951 | const char *array_suffixes = "[][][][][][][][][][]"; | |
952 | const char *array_suffix = &array_suffixes[strlen(array_suffixes)]; | |
953 | /* BTF_MAX_ITER pointer suffixes "*" */ | |
954 | const char *ptr_suffixes = "**********"; | |
955 | const char *ptr_suffix = &ptr_suffixes[strlen(ptr_suffixes)]; | |
956 | const char *name = NULL, *prefix = "", *parens = ""; | |
957 | const struct btf_member *m = show->state.member; | |
73b6eae5 | 958 | const struct btf_type *t; |
31d0bc81 AM |
959 | const struct btf_array *array; |
960 | u32 id = show->state.type_id; | |
961 | const char *member = NULL; | |
962 | bool show_member = false; | |
963 | u64 kinds = 0; | |
964 | int i; | |
965 | ||
966 | show->state.name[0] = '\0'; | |
967 | ||
968 | /* | |
969 | * Don't show type name if we're showing an array member; | |
970 | * in that case we show the array type so don't need to repeat | |
971 | * ourselves for each member. | |
972 | */ | |
973 | if (show->state.array_member) | |
974 | return ""; | |
975 | ||
976 | /* Retrieve member name, if any. */ | |
977 | if (m) { | |
978 | member = btf_name_by_offset(show->btf, m->name_off); | |
979 | show_member = strlen(member) > 0; | |
980 | id = m->type; | |
981 | } | |
982 | ||
983 | /* | |
984 | * Start with type_id, as we have resolved the struct btf_type * | |
985 | * via btf_modifier_show() past the parent typedef to the child | |
986 | * struct, int etc it is defined as. In such cases, the type_id | |
987 | * still represents the starting type while the struct btf_type * | |
988 | * in our show->state points at the resolved type of the typedef. | |
989 | */ | |
990 | t = btf_type_by_id(show->btf, id); | |
991 | if (!t) | |
992 | return ""; | |
993 | ||
994 | /* | |
995 | * The goal here is to build up the right number of pointer and | |
996 | * array suffixes while ensuring the type name for a typedef | |
997 | * is represented. Along the way we accumulate a list of | |
998 | * BTF kinds we have encountered, since these will inform later | |
999 | * display; for example, pointer types will not require an | |
1000 | * opening "{" for struct, we will just display the pointer value. | |
1001 | * | |
1002 | * We also want to accumulate the right number of pointer or array | |
1003 | * indices in the format string while iterating until we get to | |
1004 | * the typedef/pointee/array member target type. | |
1005 | * | |
1006 | * We start by pointing at the end of pointer and array suffix | |
1007 | * strings; as we accumulate pointers and arrays we move the pointer | |
1008 | * or array string backwards so it will show the expected number of | |
1009 | * '*' or '[]' for the type. BTF_SHOW_MAX_ITER of nesting of pointers | |
1010 | * and/or arrays and typedefs are supported as a precaution. | |
1011 | * | |
1012 | * We also want to get typedef name while proceeding to resolve | |
1013 | * type it points to so that we can add parentheses if it is a | |
1014 | * "typedef struct" etc. | |
1015 | */ | |
1016 | for (i = 0; i < BTF_SHOW_MAX_ITER; i++) { | |
1017 | ||
1018 | switch (BTF_INFO_KIND(t->info)) { | |
1019 | case BTF_KIND_TYPEDEF: | |
1020 | if (!name) | |
1021 | name = btf_name_by_offset(show->btf, | |
1022 | t->name_off); | |
1023 | kinds |= BTF_KIND_BIT(BTF_KIND_TYPEDEF); | |
1024 | id = t->type; | |
1025 | break; | |
1026 | case BTF_KIND_ARRAY: | |
1027 | kinds |= BTF_KIND_BIT(BTF_KIND_ARRAY); | |
1028 | parens = "["; | |
1029 | if (!t) | |
1030 | return ""; | |
1031 | array = btf_type_array(t); | |
1032 | if (array_suffix > array_suffixes) | |
1033 | array_suffix -= 2; | |
1034 | id = array->type; | |
1035 | break; | |
1036 | case BTF_KIND_PTR: | |
1037 | kinds |= BTF_KIND_BIT(BTF_KIND_PTR); | |
1038 | if (ptr_suffix > ptr_suffixes) | |
1039 | ptr_suffix -= 1; | |
1040 | id = t->type; | |
1041 | break; | |
1042 | default: | |
1043 | id = 0; | |
1044 | break; | |
1045 | } | |
1046 | if (!id) | |
1047 | break; | |
1048 | t = btf_type_skip_qualifiers(show->btf, id); | |
1049 | } | |
1050 | /* We may not be able to represent this type; bail to be safe */ | |
1051 | if (i == BTF_SHOW_MAX_ITER) | |
1052 | return ""; | |
1053 | ||
1054 | if (!name) | |
1055 | name = btf_name_by_offset(show->btf, t->name_off); | |
1056 | ||
1057 | switch (BTF_INFO_KIND(t->info)) { | |
1058 | case BTF_KIND_STRUCT: | |
1059 | case BTF_KIND_UNION: | |
1060 | prefix = BTF_INFO_KIND(t->info) == BTF_KIND_STRUCT ? | |
1061 | "struct" : "union"; | |
1062 | /* if it's an array of struct/union, parens is already set */ | |
1063 | if (!(kinds & (BTF_KIND_BIT(BTF_KIND_ARRAY)))) | |
1064 | parens = "{"; | |
1065 | break; | |
1066 | case BTF_KIND_ENUM: | |
6089fb32 | 1067 | case BTF_KIND_ENUM64: |
31d0bc81 AM |
1068 | prefix = "enum"; |
1069 | break; | |
1070 | default: | |
1071 | break; | |
1072 | } | |
1073 | ||
1074 | /* pointer does not require parens */ | |
1075 | if (kinds & BTF_KIND_BIT(BTF_KIND_PTR)) | |
1076 | parens = ""; | |
1077 | /* typedef does not require struct/union/enum prefix */ | |
1078 | if (kinds & BTF_KIND_BIT(BTF_KIND_TYPEDEF)) | |
1079 | prefix = ""; | |
1080 | ||
1081 | if (!name) | |
1082 | name = ""; | |
1083 | ||
1084 | /* Even if we don't want type name info, we want parentheses etc */ | |
1085 | if (show->flags & BTF_SHOW_NONAME) | |
1086 | snprintf(show->state.name, sizeof(show->state.name), "%s", | |
1087 | parens); | |
1088 | else | |
1089 | snprintf(show->state.name, sizeof(show->state.name), | |
1090 | "%s%s%s(%s%s%s%s%s%s)%s", | |
1091 | /* first 3 strings comprise ".member = " */ | |
1092 | show_member ? "." : "", | |
1093 | show_member ? member : "", | |
1094 | show_member ? " = " : "", | |
1095 | /* ...next is our prefix (struct, enum, etc) */ | |
1096 | prefix, | |
1097 | strlen(prefix) > 0 && strlen(name) > 0 ? " " : "", | |
1098 | /* ...this is the type name itself */ | |
1099 | name, | |
1100 | /* ...suffixed by the appropriate '*', '[]' suffixes */ | |
1101 | strlen(ptr_suffix) > 0 ? " " : "", ptr_suffix, | |
1102 | array_suffix, parens); | |
1103 | ||
1104 | return show->state.name; | |
1105 | } | |
1106 | ||
1107 | static const char *__btf_show_indent(struct btf_show *show) | |
1108 | { | |
1109 | const char *indents = " "; | |
1110 | const char *indent = &indents[strlen(indents)]; | |
1111 | ||
1112 | if ((indent - show->state.depth) >= indents) | |
1113 | return indent - show->state.depth; | |
1114 | return indents; | |
1115 | } | |
1116 | ||
1117 | static const char *btf_show_indent(struct btf_show *show) | |
1118 | { | |
1119 | return show->flags & BTF_SHOW_COMPACT ? "" : __btf_show_indent(show); | |
1120 | } | |
1121 | ||
1122 | static const char *btf_show_newline(struct btf_show *show) | |
1123 | { | |
1124 | return show->flags & BTF_SHOW_COMPACT ? "" : "\n"; | |
1125 | } | |
1126 | ||
1127 | static const char *btf_show_delim(struct btf_show *show) | |
1128 | { | |
1129 | if (show->state.depth == 0) | |
1130 | return ""; | |
1131 | ||
1132 | if ((show->flags & BTF_SHOW_COMPACT) && show->state.type && | |
1133 | BTF_INFO_KIND(show->state.type->info) == BTF_KIND_UNION) | |
1134 | return "|"; | |
1135 | ||
1136 | return ","; | |
1137 | } | |
1138 | ||
1139 | __printf(2, 3) static void btf_show(struct btf_show *show, const char *fmt, ...) | |
1140 | { | |
1141 | va_list args; | |
1142 | ||
1143 | if (!show->state.depth_check) { | |
1144 | va_start(args, fmt); | |
1145 | show->showfn(show, fmt, args); | |
1146 | va_end(args); | |
1147 | } | |
1148 | } | |
1149 | ||
1150 | /* Macros are used here as btf_show_type_value[s]() prepends and appends | |
1151 | * format specifiers to the format specifier passed in; these do the work of | |
1152 | * adding indentation, delimiters etc while the caller simply has to specify | |
1153 | * the type value(s) in the format specifier + value(s). | |
1154 | */ | |
1155 | #define btf_show_type_value(show, fmt, value) \ | |
1156 | do { \ | |
a2a5580f BD |
1157 | if ((value) != (__typeof__(value))0 || \ |
1158 | (show->flags & BTF_SHOW_ZERO) || \ | |
31d0bc81 AM |
1159 | show->state.depth == 0) { \ |
1160 | btf_show(show, "%s%s" fmt "%s%s", \ | |
1161 | btf_show_indent(show), \ | |
1162 | btf_show_name(show), \ | |
1163 | value, btf_show_delim(show), \ | |
1164 | btf_show_newline(show)); \ | |
1165 | if (show->state.depth > show->state.depth_to_show) \ | |
1166 | show->state.depth_to_show = show->state.depth; \ | |
1167 | } \ | |
1168 | } while (0) | |
1169 | ||
1170 | #define btf_show_type_values(show, fmt, ...) \ | |
1171 | do { \ | |
1172 | btf_show(show, "%s%s" fmt "%s%s", btf_show_indent(show), \ | |
1173 | btf_show_name(show), \ | |
1174 | __VA_ARGS__, btf_show_delim(show), \ | |
1175 | btf_show_newline(show)); \ | |
1176 | if (show->state.depth > show->state.depth_to_show) \ | |
1177 | show->state.depth_to_show = show->state.depth; \ | |
1178 | } while (0) | |
1179 | ||
1180 | /* How much is left to copy to safe buffer after @data? */ | |
1181 | static int btf_show_obj_size_left(struct btf_show *show, void *data) | |
1182 | { | |
1183 | return show->obj.head + show->obj.size - data; | |
1184 | } | |
1185 | ||
1186 | /* Is object pointed to by @data of @size already copied to our safe buffer? */ | |
1187 | static bool btf_show_obj_is_safe(struct btf_show *show, void *data, int size) | |
1188 | { | |
1189 | return data >= show->obj.data && | |
1190 | (data + size) < (show->obj.data + BTF_SHOW_OBJ_SAFE_SIZE); | |
1191 | } | |
1192 | ||
1193 | /* | |
1194 | * If object pointed to by @data of @size falls within our safe buffer, return | |
1195 | * the equivalent pointer to the same safe data. Assumes | |
1196 | * copy_from_kernel_nofault() has already happened and our safe buffer is | |
1197 | * populated. | |
1198 | */ | |
1199 | static void *__btf_show_obj_safe(struct btf_show *show, void *data, int size) | |
1200 | { | |
1201 | if (btf_show_obj_is_safe(show, data, size)) | |
1202 | return show->obj.safe + (data - show->obj.data); | |
1203 | return NULL; | |
1204 | } | |
1205 | ||
1206 | /* | |
1207 | * Return a safe-to-access version of data pointed to by @data. | |
1208 | * We do this by copying the relevant amount of information | |
1209 | * to the struct btf_show obj.safe buffer using copy_from_kernel_nofault(). | |
1210 | * | |
1211 | * If BTF_SHOW_UNSAFE is specified, just return data as-is; no | |
1212 | * safe copy is needed. | |
1213 | * | |
1214 | * Otherwise we need to determine if we have the required amount | |
1215 | * of data (determined by the @data pointer and the size of the | |
1216 | * largest base type we can encounter (represented by | |
1217 | * BTF_SHOW_OBJ_BASE_TYPE_SIZE). Having that much data ensures | |
1218 | * that we will be able to print some of the current object, | |
1219 | * and if more is needed a copy will be triggered. | |
1220 | * Some objects such as structs will not fit into the buffer; | |
1221 | * in such cases additional copies when we iterate over their | |
1222 | * members may be needed. | |
1223 | * | |
1224 | * btf_show_obj_safe() is used to return a safe buffer for | |
1225 | * btf_show_start_type(); this ensures that as we recurse into | |
1226 | * nested types we always have safe data for the given type. | |
1227 | * This approach is somewhat wasteful; it's possible for example | |
1228 | * that when iterating over a large union we'll end up copying the | |
1229 | * same data repeatedly, but the goal is safety not performance. | |
1230 | * We use stack data as opposed to per-CPU buffers because the | |
1231 | * iteration over a type can take some time, and preemption handling | |
1232 | * would greatly complicate use of the safe buffer. | |
1233 | */ | |
1234 | static void *btf_show_obj_safe(struct btf_show *show, | |
1235 | const struct btf_type *t, | |
1236 | void *data) | |
1237 | { | |
1238 | const struct btf_type *rt; | |
1239 | int size_left, size; | |
1240 | void *safe = NULL; | |
1241 | ||
1242 | if (show->flags & BTF_SHOW_UNSAFE) | |
1243 | return data; | |
1244 | ||
1245 | rt = btf_resolve_size(show->btf, t, &size); | |
1246 | if (IS_ERR(rt)) { | |
1247 | show->state.status = PTR_ERR(rt); | |
1248 | return NULL; | |
1249 | } | |
1250 | ||
1251 | /* | |
1252 | * Is this toplevel object? If so, set total object size and | |
1253 | * initialize pointers. Otherwise check if we still fall within | |
1254 | * our safe object data. | |
1255 | */ | |
1256 | if (show->state.depth == 0) { | |
1257 | show->obj.size = size; | |
1258 | show->obj.head = data; | |
1259 | } else { | |
1260 | /* | |
1261 | * If the size of the current object is > our remaining | |
1262 | * safe buffer we _may_ need to do a new copy. However | |
1263 | * consider the case of a nested struct; it's size pushes | |
1264 | * us over the safe buffer limit, but showing any individual | |
1265 | * struct members does not. In such cases, we don't need | |
1266 | * to initiate a fresh copy yet; however we definitely need | |
1267 | * at least BTF_SHOW_OBJ_BASE_TYPE_SIZE bytes left | |
1268 | * in our buffer, regardless of the current object size. | |
1269 | * The logic here is that as we resolve types we will | |
1270 | * hit a base type at some point, and we need to be sure | |
1271 | * the next chunk of data is safely available to display | |
1272 | * that type info safely. We cannot rely on the size of | |
1273 | * the current object here because it may be much larger | |
1274 | * than our current buffer (e.g. task_struct is 8k). | |
1275 | * All we want to do here is ensure that we can print the | |
1276 | * next basic type, which we can if either | |
1277 | * - the current type size is within the safe buffer; or | |
1278 | * - at least BTF_SHOW_OBJ_BASE_TYPE_SIZE bytes are left in | |
1279 | * the safe buffer. | |
1280 | */ | |
1281 | safe = __btf_show_obj_safe(show, data, | |
1282 | min(size, | |
1283 | BTF_SHOW_OBJ_BASE_TYPE_SIZE)); | |
1284 | } | |
1285 | ||
1286 | /* | |
1287 | * We need a new copy to our safe object, either because we haven't | |
8fb33b60 | 1288 | * yet copied and are initializing safe data, or because the data |
31d0bc81 AM |
1289 | * we want falls outside the boundaries of the safe object. |
1290 | */ | |
1291 | if (!safe) { | |
1292 | size_left = btf_show_obj_size_left(show, data); | |
1293 | if (size_left > BTF_SHOW_OBJ_SAFE_SIZE) | |
1294 | size_left = BTF_SHOW_OBJ_SAFE_SIZE; | |
1295 | show->state.status = copy_from_kernel_nofault(show->obj.safe, | |
1296 | data, size_left); | |
1297 | if (!show->state.status) { | |
1298 | show->obj.data = data; | |
1299 | safe = show->obj.safe; | |
1300 | } | |
1301 | } | |
1302 | ||
1303 | return safe; | |
1304 | } | |
1305 | ||
1306 | /* | |
1307 | * Set the type we are starting to show and return a safe data pointer | |
1308 | * to be used for showing the associated data. | |
1309 | */ | |
1310 | static void *btf_show_start_type(struct btf_show *show, | |
1311 | const struct btf_type *t, | |
1312 | u32 type_id, void *data) | |
1313 | { | |
1314 | show->state.type = t; | |
1315 | show->state.type_id = type_id; | |
1316 | show->state.name[0] = '\0'; | |
1317 | ||
1318 | return btf_show_obj_safe(show, t, data); | |
1319 | } | |
1320 | ||
1321 | static void btf_show_end_type(struct btf_show *show) | |
1322 | { | |
1323 | show->state.type = NULL; | |
1324 | show->state.type_id = 0; | |
1325 | show->state.name[0] = '\0'; | |
1326 | } | |
1327 | ||
1328 | static void *btf_show_start_aggr_type(struct btf_show *show, | |
1329 | const struct btf_type *t, | |
1330 | u32 type_id, void *data) | |
1331 | { | |
1332 | void *safe_data = btf_show_start_type(show, t, type_id, data); | |
1333 | ||
1334 | if (!safe_data) | |
1335 | return safe_data; | |
1336 | ||
1337 | btf_show(show, "%s%s%s", btf_show_indent(show), | |
1338 | btf_show_name(show), | |
1339 | btf_show_newline(show)); | |
1340 | show->state.depth++; | |
1341 | return safe_data; | |
1342 | } | |
1343 | ||
1344 | static void btf_show_end_aggr_type(struct btf_show *show, | |
1345 | const char *suffix) | |
1346 | { | |
1347 | show->state.depth--; | |
1348 | btf_show(show, "%s%s%s%s", btf_show_indent(show), suffix, | |
1349 | btf_show_delim(show), btf_show_newline(show)); | |
1350 | btf_show_end_type(show); | |
1351 | } | |
1352 | ||
1353 | static void btf_show_start_member(struct btf_show *show, | |
1354 | const struct btf_member *m) | |
1355 | { | |
1356 | show->state.member = m; | |
1357 | } | |
1358 | ||
1359 | static void btf_show_start_array_member(struct btf_show *show) | |
1360 | { | |
1361 | show->state.array_member = 1; | |
1362 | btf_show_start_member(show, NULL); | |
1363 | } | |
1364 | ||
1365 | static void btf_show_end_member(struct btf_show *show) | |
1366 | { | |
1367 | show->state.member = NULL; | |
1368 | } | |
1369 | ||
1370 | static void btf_show_end_array_member(struct btf_show *show) | |
1371 | { | |
1372 | show->state.array_member = 0; | |
1373 | btf_show_end_member(show); | |
1374 | } | |
1375 | ||
1376 | static void *btf_show_start_array_type(struct btf_show *show, | |
1377 | const struct btf_type *t, | |
1378 | u32 type_id, | |
1379 | u16 array_encoding, | |
1380 | void *data) | |
1381 | { | |
1382 | show->state.array_encoding = array_encoding; | |
1383 | show->state.array_terminated = 0; | |
1384 | return btf_show_start_aggr_type(show, t, type_id, data); | |
1385 | } | |
1386 | ||
1387 | static void btf_show_end_array_type(struct btf_show *show) | |
1388 | { | |
1389 | show->state.array_encoding = 0; | |
1390 | show->state.array_terminated = 0; | |
1391 | btf_show_end_aggr_type(show, "]"); | |
1392 | } | |
1393 | ||
1394 | static void *btf_show_start_struct_type(struct btf_show *show, | |
1395 | const struct btf_type *t, | |
1396 | u32 type_id, | |
1397 | void *data) | |
1398 | { | |
1399 | return btf_show_start_aggr_type(show, t, type_id, data); | |
1400 | } | |
1401 | ||
1402 | static void btf_show_end_struct_type(struct btf_show *show) | |
1403 | { | |
1404 | btf_show_end_aggr_type(show, "}"); | |
1405 | } | |
1406 | ||
69b693f0 MKL |
1407 | __printf(2, 3) static void __btf_verifier_log(struct bpf_verifier_log *log, |
1408 | const char *fmt, ...) | |
1409 | { | |
1410 | va_list args; | |
1411 | ||
1412 | va_start(args, fmt); | |
1413 | bpf_verifier_vlog(log, fmt, args); | |
1414 | va_end(args); | |
1415 | } | |
1416 | ||
1417 | __printf(2, 3) static void btf_verifier_log(struct btf_verifier_env *env, | |
1418 | const char *fmt, ...) | |
1419 | { | |
1420 | struct bpf_verifier_log *log = &env->log; | |
1421 | va_list args; | |
1422 | ||
1423 | if (!bpf_verifier_log_needed(log)) | |
1424 | return; | |
1425 | ||
1426 | va_start(args, fmt); | |
1427 | bpf_verifier_vlog(log, fmt, args); | |
1428 | va_end(args); | |
1429 | } | |
1430 | ||
1431 | __printf(4, 5) static void __btf_verifier_log_type(struct btf_verifier_env *env, | |
1432 | const struct btf_type *t, | |
1433 | bool log_details, | |
1434 | const char *fmt, ...) | |
1435 | { | |
1436 | struct bpf_verifier_log *log = &env->log; | |
69b693f0 MKL |
1437 | struct btf *btf = env->btf; |
1438 | va_list args; | |
1439 | ||
1440 | if (!bpf_verifier_log_needed(log)) | |
1441 | return; | |
1442 | ||
9cb61e50 CB |
1443 | if (log->level == BPF_LOG_KERNEL) { |
1444 | /* btf verifier prints all types it is processing via | |
1445 | * btf_verifier_log_type(..., fmt = NULL). | |
1446 | * Skip those prints for in-kernel BTF verification. | |
1447 | */ | |
1448 | if (!fmt) | |
1449 | return; | |
1450 | ||
1451 | /* Skip logging when loading module BTF with mismatches permitted */ | |
1452 | if (env->btf->base_btf && IS_ENABLED(CONFIG_MODULE_ALLOW_BTF_MISMATCH)) | |
1453 | return; | |
1454 | } | |
8580ac94 | 1455 | |
69b693f0 MKL |
1456 | __btf_verifier_log(log, "[%u] %s %s%s", |
1457 | env->log_type_id, | |
571f9738 | 1458 | btf_type_str(t), |
23127b33 | 1459 | __btf_name_by_offset(btf, t->name_off), |
69b693f0 MKL |
1460 | log_details ? " " : ""); |
1461 | ||
1462 | if (log_details) | |
1463 | btf_type_ops(t)->log_details(env, t); | |
1464 | ||
1465 | if (fmt && *fmt) { | |
1466 | __btf_verifier_log(log, " "); | |
1467 | va_start(args, fmt); | |
1468 | bpf_verifier_vlog(log, fmt, args); | |
1469 | va_end(args); | |
1470 | } | |
1471 | ||
1472 | __btf_verifier_log(log, "\n"); | |
1473 | } | |
1474 | ||
1475 | #define btf_verifier_log_type(env, t, ...) \ | |
1476 | __btf_verifier_log_type((env), (t), true, __VA_ARGS__) | |
1477 | #define btf_verifier_log_basic(env, t, ...) \ | |
1478 | __btf_verifier_log_type((env), (t), false, __VA_ARGS__) | |
1479 | ||
1480 | __printf(4, 5) | |
1481 | static void btf_verifier_log_member(struct btf_verifier_env *env, | |
1482 | const struct btf_type *struct_type, | |
1483 | const struct btf_member *member, | |
1484 | const char *fmt, ...) | |
1485 | { | |
1486 | struct bpf_verifier_log *log = &env->log; | |
1487 | struct btf *btf = env->btf; | |
1488 | va_list args; | |
1489 | ||
1490 | if (!bpf_verifier_log_needed(log)) | |
1491 | return; | |
1492 | ||
9cb61e50 CB |
1493 | if (log->level == BPF_LOG_KERNEL) { |
1494 | if (!fmt) | |
1495 | return; | |
1496 | ||
1497 | /* Skip logging when loading module BTF with mismatches permitted */ | |
1498 | if (env->btf->base_btf && IS_ENABLED(CONFIG_MODULE_ALLOW_BTF_MISMATCH)) | |
1499 | return; | |
1500 | } | |
1501 | ||
eb3f595d MKL |
1502 | /* The CHECK_META phase already did a btf dump. |
1503 | * | |
1504 | * If member is logged again, it must hit an error in | |
1505 | * parsing this member. It is useful to print out which | |
1506 | * struct this member belongs to. | |
1507 | */ | |
1508 | if (env->phase != CHECK_META) | |
1509 | btf_verifier_log_type(env, struct_type, NULL); | |
1510 | ||
9d5f9f70 YS |
1511 | if (btf_type_kflag(struct_type)) |
1512 | __btf_verifier_log(log, | |
1513 | "\t%s type_id=%u bitfield_size=%u bits_offset=%u", | |
1514 | __btf_name_by_offset(btf, member->name_off), | |
1515 | member->type, | |
1516 | BTF_MEMBER_BITFIELD_SIZE(member->offset), | |
1517 | BTF_MEMBER_BIT_OFFSET(member->offset)); | |
1518 | else | |
1519 | __btf_verifier_log(log, "\t%s type_id=%u bits_offset=%u", | |
1520 | __btf_name_by_offset(btf, member->name_off), | |
1521 | member->type, member->offset); | |
69b693f0 MKL |
1522 | |
1523 | if (fmt && *fmt) { | |
1524 | __btf_verifier_log(log, " "); | |
1525 | va_start(args, fmt); | |
1526 | bpf_verifier_vlog(log, fmt, args); | |
1527 | va_end(args); | |
1528 | } | |
1529 | ||
1530 | __btf_verifier_log(log, "\n"); | |
1531 | } | |
1532 | ||
1dc92851 DB |
1533 | __printf(4, 5) |
1534 | static void btf_verifier_log_vsi(struct btf_verifier_env *env, | |
1535 | const struct btf_type *datasec_type, | |
1536 | const struct btf_var_secinfo *vsi, | |
1537 | const char *fmt, ...) | |
1538 | { | |
1539 | struct bpf_verifier_log *log = &env->log; | |
1540 | va_list args; | |
1541 | ||
1542 | if (!bpf_verifier_log_needed(log)) | |
1543 | return; | |
8580ac94 AS |
1544 | if (log->level == BPF_LOG_KERNEL && !fmt) |
1545 | return; | |
1dc92851 DB |
1546 | if (env->phase != CHECK_META) |
1547 | btf_verifier_log_type(env, datasec_type, NULL); | |
1548 | ||
1549 | __btf_verifier_log(log, "\t type_id=%u offset=%u size=%u", | |
1550 | vsi->type, vsi->offset, vsi->size); | |
1551 | if (fmt && *fmt) { | |
1552 | __btf_verifier_log(log, " "); | |
1553 | va_start(args, fmt); | |
1554 | bpf_verifier_vlog(log, fmt, args); | |
1555 | va_end(args); | |
1556 | } | |
1557 | ||
1558 | __btf_verifier_log(log, "\n"); | |
1559 | } | |
1560 | ||
f80442a4 MKL |
1561 | static void btf_verifier_log_hdr(struct btf_verifier_env *env, |
1562 | u32 btf_data_size) | |
69b693f0 MKL |
1563 | { |
1564 | struct bpf_verifier_log *log = &env->log; | |
1565 | const struct btf *btf = env->btf; | |
1566 | const struct btf_header *hdr; | |
1567 | ||
1568 | if (!bpf_verifier_log_needed(log)) | |
1569 | return; | |
1570 | ||
8580ac94 AS |
1571 | if (log->level == BPF_LOG_KERNEL) |
1572 | return; | |
f80442a4 | 1573 | hdr = &btf->hdr; |
69b693f0 MKL |
1574 | __btf_verifier_log(log, "magic: 0x%x\n", hdr->magic); |
1575 | __btf_verifier_log(log, "version: %u\n", hdr->version); | |
1576 | __btf_verifier_log(log, "flags: 0x%x\n", hdr->flags); | |
f80442a4 | 1577 | __btf_verifier_log(log, "hdr_len: %u\n", hdr->hdr_len); |
69b693f0 | 1578 | __btf_verifier_log(log, "type_off: %u\n", hdr->type_off); |
f80442a4 | 1579 | __btf_verifier_log(log, "type_len: %u\n", hdr->type_len); |
69b693f0 MKL |
1580 | __btf_verifier_log(log, "str_off: %u\n", hdr->str_off); |
1581 | __btf_verifier_log(log, "str_len: %u\n", hdr->str_len); | |
f80442a4 | 1582 | __btf_verifier_log(log, "btf_total_size: %u\n", btf_data_size); |
69b693f0 MKL |
1583 | } |
1584 | ||
1585 | static int btf_add_type(struct btf_verifier_env *env, struct btf_type *t) | |
1586 | { | |
1587 | struct btf *btf = env->btf; | |
1588 | ||
951bb646 | 1589 | if (btf->types_size == btf->nr_types) { |
69b693f0 MKL |
1590 | /* Expand 'types' array */ |
1591 | ||
1592 | struct btf_type **new_types; | |
1593 | u32 expand_by, new_size; | |
1594 | ||
951bb646 | 1595 | if (btf->start_id + btf->types_size == BTF_MAX_TYPE) { |
69b693f0 MKL |
1596 | btf_verifier_log(env, "Exceeded max num of types"); |
1597 | return -E2BIG; | |
1598 | } | |
1599 | ||
1600 | expand_by = max_t(u32, btf->types_size >> 2, 16); | |
aea2f7b8 | 1601 | new_size = min_t(u32, BTF_MAX_TYPE, |
69b693f0 MKL |
1602 | btf->types_size + expand_by); |
1603 | ||
778e1cdd | 1604 | new_types = kvcalloc(new_size, sizeof(*new_types), |
69b693f0 MKL |
1605 | GFP_KERNEL | __GFP_NOWARN); |
1606 | if (!new_types) | |
1607 | return -ENOMEM; | |
1608 | ||
951bb646 AN |
1609 | if (btf->nr_types == 0) { |
1610 | if (!btf->base_btf) { | |
1611 | /* lazily init VOID type */ | |
1612 | new_types[0] = &btf_void; | |
1613 | btf->nr_types++; | |
1614 | } | |
1615 | } else { | |
69b693f0 | 1616 | memcpy(new_types, btf->types, |
951bb646 AN |
1617 | sizeof(*btf->types) * btf->nr_types); |
1618 | } | |
69b693f0 MKL |
1619 | |
1620 | kvfree(btf->types); | |
1621 | btf->types = new_types; | |
1622 | btf->types_size = new_size; | |
1623 | } | |
1624 | ||
951bb646 | 1625 | btf->types[btf->nr_types++] = t; |
69b693f0 MKL |
1626 | |
1627 | return 0; | |
1628 | } | |
1629 | ||
78958fca MKL |
1630 | static int btf_alloc_id(struct btf *btf) |
1631 | { | |
1632 | int id; | |
1633 | ||
1634 | idr_preload(GFP_KERNEL); | |
1635 | spin_lock_bh(&btf_idr_lock); | |
1636 | id = idr_alloc_cyclic(&btf_idr, btf, 1, INT_MAX, GFP_ATOMIC); | |
1637 | if (id > 0) | |
1638 | btf->id = id; | |
1639 | spin_unlock_bh(&btf_idr_lock); | |
1640 | idr_preload_end(); | |
1641 | ||
1642 | if (WARN_ON_ONCE(!id)) | |
1643 | return -ENOSPC; | |
1644 | ||
1645 | return id > 0 ? 0 : id; | |
1646 | } | |
1647 | ||
1648 | static void btf_free_id(struct btf *btf) | |
1649 | { | |
1650 | unsigned long flags; | |
1651 | ||
1652 | /* | |
1653 | * In map-in-map, calling map_delete_elem() on outer | |
1654 | * map will call bpf_map_put on the inner map. | |
1655 | * It will then eventually call btf_free_id() | |
1656 | * on the inner map. Some of the map_delete_elem() | |
1657 | * implementation may have irq disabled, so | |
1658 | * we need to use the _irqsave() version instead | |
1659 | * of the _bh() version. | |
1660 | */ | |
1661 | spin_lock_irqsave(&btf_idr_lock, flags); | |
1662 | idr_remove(&btf_idr, btf->id); | |
1663 | spin_unlock_irqrestore(&btf_idr_lock, flags); | |
1664 | } | |
1665 | ||
dee872e1 KKD |
1666 | static void btf_free_kfunc_set_tab(struct btf *btf) |
1667 | { | |
1668 | struct btf_kfunc_set_tab *tab = btf->kfunc_set_tab; | |
a4703e31 | 1669 | int hook; |
dee872e1 KKD |
1670 | |
1671 | if (!tab) | |
1672 | return; | |
a4703e31 KKD |
1673 | for (hook = 0; hook < ARRAY_SIZE(tab->sets); hook++) |
1674 | kfree(tab->sets[hook]); | |
dee872e1 KKD |
1675 | kfree(tab); |
1676 | btf->kfunc_set_tab = NULL; | |
1677 | } | |
1678 | ||
5ce937d6 KKD |
1679 | static void btf_free_dtor_kfunc_tab(struct btf *btf) |
1680 | { | |
1681 | struct btf_id_dtor_kfunc_tab *tab = btf->dtor_kfunc_tab; | |
1682 | ||
1683 | if (!tab) | |
1684 | return; | |
1685 | kfree(tab); | |
1686 | btf->dtor_kfunc_tab = NULL; | |
1687 | } | |
1688 | ||
8ffa5cc1 KKD |
1689 | static void btf_struct_metas_free(struct btf_struct_metas *tab) |
1690 | { | |
1691 | int i; | |
1692 | ||
1693 | if (!tab) | |
1694 | return; | |
cd2a8079 | 1695 | for (i = 0; i < tab->cnt; i++) |
8ffa5cc1 | 1696 | btf_record_free(tab->types[i].record); |
8ffa5cc1 KKD |
1697 | kfree(tab); |
1698 | } | |
1699 | ||
1700 | static void btf_free_struct_meta_tab(struct btf *btf) | |
1701 | { | |
1702 | struct btf_struct_metas *tab = btf->struct_meta_tab; | |
1703 | ||
1704 | btf_struct_metas_free(tab); | |
1705 | btf->struct_meta_tab = NULL; | |
1706 | } | |
1707 | ||
e6199511 KFL |
1708 | static void btf_free_struct_ops_tab(struct btf *btf) |
1709 | { | |
1710 | struct btf_struct_ops_tab *tab = btf->struct_ops_tab; | |
16116035 KFL |
1711 | u32 i; |
1712 | ||
1713 | if (!tab) | |
1714 | return; | |
1715 | ||
1716 | for (i = 0; i < tab->cnt; i++) | |
1717 | bpf_struct_ops_desc_release(&tab->ops[i]); | |
e6199511 KFL |
1718 | |
1719 | kfree(tab); | |
1720 | btf->struct_ops_tab = NULL; | |
1721 | } | |
1722 | ||
69b693f0 MKL |
1723 | static void btf_free(struct btf *btf) |
1724 | { | |
8ffa5cc1 | 1725 | btf_free_struct_meta_tab(btf); |
5ce937d6 | 1726 | btf_free_dtor_kfunc_tab(btf); |
dee872e1 | 1727 | btf_free_kfunc_set_tab(btf); |
e6199511 | 1728 | btf_free_struct_ops_tab(btf); |
69b693f0 | 1729 | kvfree(btf->types); |
eb3f595d MKL |
1730 | kvfree(btf->resolved_sizes); |
1731 | kvfree(btf->resolved_ids); | |
8646db23 AM |
1732 | /* vmlinux does not allocate btf->data, it simply points it at |
1733 | * __start_BTF. | |
1734 | */ | |
1735 | if (!btf_is_vmlinux(btf)) | |
1736 | kvfree(btf->data); | |
1737 | kvfree(btf->base_id_map); | |
69b693f0 MKL |
1738 | kfree(btf); |
1739 | } | |
1740 | ||
78958fca | 1741 | static void btf_free_rcu(struct rcu_head *rcu) |
f56a653c | 1742 | { |
78958fca MKL |
1743 | struct btf *btf = container_of(rcu, struct btf, rcu); |
1744 | ||
1745 | btf_free(btf); | |
f56a653c MKL |
1746 | } |
1747 | ||
3b1f89e7 KFL |
1748 | const char *btf_get_name(const struct btf *btf) |
1749 | { | |
1750 | return btf->name; | |
1751 | } | |
1752 | ||
22dc4a0f AN |
1753 | void btf_get(struct btf *btf) |
1754 | { | |
1755 | refcount_inc(&btf->refcnt); | |
1756 | } | |
1757 | ||
f56a653c MKL |
1758 | void btf_put(struct btf *btf) |
1759 | { | |
78958fca MKL |
1760 | if (btf && refcount_dec_and_test(&btf->refcnt)) { |
1761 | btf_free_id(btf); | |
1762 | call_rcu(&btf->rcu, btf_free_rcu); | |
1763 | } | |
f56a653c MKL |
1764 | } |
1765 | ||
8646db23 AM |
1766 | struct btf *btf_base_btf(const struct btf *btf) |
1767 | { | |
1768 | return btf->base_btf; | |
1769 | } | |
1770 | ||
1771 | const struct btf_header *btf_header(const struct btf *btf) | |
1772 | { | |
1773 | return &btf->hdr; | |
1774 | } | |
1775 | ||
1776 | void btf_set_base_btf(struct btf *btf, const struct btf *base_btf) | |
1777 | { | |
1778 | btf->base_btf = (struct btf *)base_btf; | |
1779 | btf->start_id = btf_nr_types(base_btf); | |
1780 | btf->start_str_off = base_btf->hdr.str_len; | |
1781 | } | |
1782 | ||
eb3f595d MKL |
1783 | static int env_resolve_init(struct btf_verifier_env *env) |
1784 | { | |
1785 | struct btf *btf = env->btf; | |
1786 | u32 nr_types = btf->nr_types; | |
1787 | u32 *resolved_sizes = NULL; | |
1788 | u32 *resolved_ids = NULL; | |
1789 | u8 *visit_states = NULL; | |
1790 | ||
951bb646 | 1791 | resolved_sizes = kvcalloc(nr_types, sizeof(*resolved_sizes), |
eb3f595d MKL |
1792 | GFP_KERNEL | __GFP_NOWARN); |
1793 | if (!resolved_sizes) | |
1794 | goto nomem; | |
1795 | ||
951bb646 | 1796 | resolved_ids = kvcalloc(nr_types, sizeof(*resolved_ids), |
eb3f595d MKL |
1797 | GFP_KERNEL | __GFP_NOWARN); |
1798 | if (!resolved_ids) | |
1799 | goto nomem; | |
1800 | ||
951bb646 | 1801 | visit_states = kvcalloc(nr_types, sizeof(*visit_states), |
eb3f595d MKL |
1802 | GFP_KERNEL | __GFP_NOWARN); |
1803 | if (!visit_states) | |
1804 | goto nomem; | |
1805 | ||
1806 | btf->resolved_sizes = resolved_sizes; | |
1807 | btf->resolved_ids = resolved_ids; | |
1808 | env->visit_states = visit_states; | |
1809 | ||
1810 | return 0; | |
1811 | ||
1812 | nomem: | |
1813 | kvfree(resolved_sizes); | |
1814 | kvfree(resolved_ids); | |
1815 | kvfree(visit_states); | |
1816 | return -ENOMEM; | |
1817 | } | |
1818 | ||
69b693f0 MKL |
1819 | static void btf_verifier_env_free(struct btf_verifier_env *env) |
1820 | { | |
eb3f595d | 1821 | kvfree(env->visit_states); |
69b693f0 MKL |
1822 | kfree(env); |
1823 | } | |
1824 | ||
eb3f595d MKL |
1825 | static bool env_type_is_resolve_sink(const struct btf_verifier_env *env, |
1826 | const struct btf_type *next_type) | |
1827 | { | |
1828 | switch (env->resolve_mode) { | |
1829 | case RESOLVE_TBD: | |
1830 | /* int, enum or void is a sink */ | |
1831 | return !btf_type_needs_resolve(next_type); | |
1832 | case RESOLVE_PTR: | |
2667a262 MKL |
1833 | /* int, enum, void, struct, array, func or func_proto is a sink |
1834 | * for ptr | |
1835 | */ | |
eb3f595d MKL |
1836 | return !btf_type_is_modifier(next_type) && |
1837 | !btf_type_is_ptr(next_type); | |
1838 | case RESOLVE_STRUCT_OR_ARRAY: | |
2667a262 MKL |
1839 | /* int, enum, void, ptr, func or func_proto is a sink |
1840 | * for struct and array | |
1841 | */ | |
eb3f595d MKL |
1842 | return !btf_type_is_modifier(next_type) && |
1843 | !btf_type_is_array(next_type) && | |
1844 | !btf_type_is_struct(next_type); | |
1845 | default: | |
53c8036c | 1846 | BUG(); |
eb3f595d MKL |
1847 | } |
1848 | } | |
1849 | ||
1850 | static bool env_type_is_resolved(const struct btf_verifier_env *env, | |
1851 | u32 type_id) | |
1852 | { | |
951bb646 AN |
1853 | /* base BTF types should be resolved by now */ |
1854 | if (type_id < env->btf->start_id) | |
1855 | return true; | |
1856 | ||
1857 | return env->visit_states[type_id - env->btf->start_id] == RESOLVED; | |
eb3f595d MKL |
1858 | } |
1859 | ||
1860 | static int env_stack_push(struct btf_verifier_env *env, | |
1861 | const struct btf_type *t, u32 type_id) | |
1862 | { | |
951bb646 | 1863 | const struct btf *btf = env->btf; |
eb3f595d MKL |
1864 | struct resolve_vertex *v; |
1865 | ||
1866 | if (env->top_stack == MAX_RESOLVE_DEPTH) | |
1867 | return -E2BIG; | |
1868 | ||
951bb646 AN |
1869 | if (type_id < btf->start_id |
1870 | || env->visit_states[type_id - btf->start_id] != NOT_VISITED) | |
eb3f595d MKL |
1871 | return -EEXIST; |
1872 | ||
951bb646 | 1873 | env->visit_states[type_id - btf->start_id] = VISITED; |
eb3f595d MKL |
1874 | |
1875 | v = &env->stack[env->top_stack++]; | |
1876 | v->t = t; | |
1877 | v->type_id = type_id; | |
1878 | v->next_member = 0; | |
1879 | ||
1880 | if (env->resolve_mode == RESOLVE_TBD) { | |
1881 | if (btf_type_is_ptr(t)) | |
1882 | env->resolve_mode = RESOLVE_PTR; | |
1883 | else if (btf_type_is_struct(t) || btf_type_is_array(t)) | |
1884 | env->resolve_mode = RESOLVE_STRUCT_OR_ARRAY; | |
1885 | } | |
1886 | ||
1887 | return 0; | |
1888 | } | |
1889 | ||
1890 | static void env_stack_set_next_member(struct btf_verifier_env *env, | |
1891 | u16 next_member) | |
1892 | { | |
1893 | env->stack[env->top_stack - 1].next_member = next_member; | |
1894 | } | |
1895 | ||
1896 | static void env_stack_pop_resolved(struct btf_verifier_env *env, | |
1897 | u32 resolved_type_id, | |
1898 | u32 resolved_size) | |
1899 | { | |
1900 | u32 type_id = env->stack[--(env->top_stack)].type_id; | |
1901 | struct btf *btf = env->btf; | |
1902 | ||
951bb646 | 1903 | type_id -= btf->start_id; /* adjust to local type id */ |
eb3f595d MKL |
1904 | btf->resolved_sizes[type_id] = resolved_size; |
1905 | btf->resolved_ids[type_id] = resolved_type_id; | |
1906 | env->visit_states[type_id] = RESOLVED; | |
1907 | } | |
1908 | ||
1909 | static const struct resolve_vertex *env_stack_peak(struct btf_verifier_env *env) | |
1910 | { | |
1911 | return env->top_stack ? &env->stack[env->top_stack - 1] : NULL; | |
1912 | } | |
1913 | ||
7e3617a7 MKL |
1914 | /* Resolve the size of a passed-in "type" |
1915 | * | |
1916 | * type: is an array (e.g. u32 array[x][y]) | |
1917 | * return type: type "u32[x][y]", i.e. BTF_KIND_ARRAY, | |
1918 | * *type_size: (x * y * sizeof(u32)). Hence, *type_size always | |
1919 | * corresponds to the return type. | |
1920 | * *elem_type: u32 | |
69ff3047 | 1921 | * *elem_id: id of u32 |
7e3617a7 MKL |
1922 | * *total_nelems: (x * y). Hence, individual elem size is |
1923 | * (*type_size / *total_nelems) | |
887c31a3 | 1924 | * *type_id: id of type if it's changed within the function, 0 if not |
7e3617a7 MKL |
1925 | * |
1926 | * type: is not an array (e.g. const struct X) | |
1927 | * return type: type "struct X" | |
1928 | * *type_size: sizeof(struct X) | |
1929 | * *elem_type: same as return type ("struct X") | |
69ff3047 | 1930 | * *elem_id: 0 |
7e3617a7 | 1931 | * *total_nelems: 1 |
887c31a3 | 1932 | * *type_id: id of type if it's changed within the function, 0 if not |
7e3617a7 | 1933 | */ |
6298399b JO |
1934 | static const struct btf_type * |
1935 | __btf_resolve_size(const struct btf *btf, const struct btf_type *type, | |
1936 | u32 *type_size, const struct btf_type **elem_type, | |
887c31a3 | 1937 | u32 *elem_id, u32 *total_nelems, u32 *type_id) |
7e3617a7 MKL |
1938 | { |
1939 | const struct btf_type *array_type = NULL; | |
69ff3047 | 1940 | const struct btf_array *array = NULL; |
887c31a3 | 1941 | u32 i, size, nelems = 1, id = 0; |
7e3617a7 MKL |
1942 | |
1943 | for (i = 0; i < MAX_RESOLVE_DEPTH; i++) { | |
1944 | switch (BTF_INFO_KIND(type->info)) { | |
1945 | /* type->size can be used */ | |
1946 | case BTF_KIND_INT: | |
1947 | case BTF_KIND_STRUCT: | |
1948 | case BTF_KIND_UNION: | |
1949 | case BTF_KIND_ENUM: | |
b1828f0b | 1950 | case BTF_KIND_FLOAT: |
6089fb32 | 1951 | case BTF_KIND_ENUM64: |
7e3617a7 MKL |
1952 | size = type->size; |
1953 | goto resolved; | |
1954 | ||
1955 | case BTF_KIND_PTR: | |
1956 | size = sizeof(void *); | |
1957 | goto resolved; | |
1958 | ||
1959 | /* Modifiers */ | |
1960 | case BTF_KIND_TYPEDEF: | |
1961 | case BTF_KIND_VOLATILE: | |
1962 | case BTF_KIND_CONST: | |
1963 | case BTF_KIND_RESTRICT: | |
8c42d2fa | 1964 | case BTF_KIND_TYPE_TAG: |
887c31a3 | 1965 | id = type->type; |
7e3617a7 MKL |
1966 | type = btf_type_by_id(btf, type->type); |
1967 | break; | |
1968 | ||
1969 | case BTF_KIND_ARRAY: | |
1970 | if (!array_type) | |
1971 | array_type = type; | |
1972 | array = btf_type_array(type); | |
1973 | if (nelems && array->nelems > U32_MAX / nelems) | |
1974 | return ERR_PTR(-EINVAL); | |
1975 | nelems *= array->nelems; | |
1976 | type = btf_type_by_id(btf, array->type); | |
1977 | break; | |
1978 | ||
1979 | /* type without size */ | |
1980 | default: | |
1981 | return ERR_PTR(-EINVAL); | |
1982 | } | |
1983 | } | |
1984 | ||
1985 | return ERR_PTR(-EINVAL); | |
1986 | ||
1987 | resolved: | |
1988 | if (nelems && size > U32_MAX / nelems) | |
1989 | return ERR_PTR(-EINVAL); | |
1990 | ||
1991 | *type_size = nelems * size; | |
85d33df3 MKL |
1992 | if (total_nelems) |
1993 | *total_nelems = nelems; | |
1994 | if (elem_type) | |
1995 | *elem_type = type; | |
69ff3047 JO |
1996 | if (elem_id) |
1997 | *elem_id = array ? array->type : 0; | |
887c31a3 JO |
1998 | if (type_id && id) |
1999 | *type_id = id; | |
7e3617a7 MKL |
2000 | |
2001 | return array_type ? : type; | |
2002 | } | |
2003 | ||
6298399b JO |
2004 | const struct btf_type * |
2005 | btf_resolve_size(const struct btf *btf, const struct btf_type *type, | |
2006 | u32 *type_size) | |
2007 | { | |
887c31a3 | 2008 | return __btf_resolve_size(btf, type, type_size, NULL, NULL, NULL, NULL); |
6298399b JO |
2009 | } |
2010 | ||
951bb646 AN |
2011 | static u32 btf_resolved_type_id(const struct btf *btf, u32 type_id) |
2012 | { | |
2013 | while (type_id < btf->start_id) | |
2014 | btf = btf->base_btf; | |
2015 | ||
2016 | return btf->resolved_ids[type_id - btf->start_id]; | |
2017 | } | |
2018 | ||
eb3f595d MKL |
2019 | /* The input param "type_id" must point to a needs_resolve type */ |
2020 | static const struct btf_type *btf_type_id_resolve(const struct btf *btf, | |
2021 | u32 *type_id) | |
2022 | { | |
951bb646 | 2023 | *type_id = btf_resolved_type_id(btf, *type_id); |
eb3f595d MKL |
2024 | return btf_type_by_id(btf, *type_id); |
2025 | } | |
2026 | ||
951bb646 AN |
2027 | static u32 btf_resolved_type_size(const struct btf *btf, u32 type_id) |
2028 | { | |
2029 | while (type_id < btf->start_id) | |
2030 | btf = btf->base_btf; | |
2031 | ||
2032 | return btf->resolved_sizes[type_id - btf->start_id]; | |
2033 | } | |
2034 | ||
eb3f595d MKL |
2035 | const struct btf_type *btf_type_id_size(const struct btf *btf, |
2036 | u32 *type_id, u32 *ret_size) | |
2037 | { | |
2038 | const struct btf_type *size_type; | |
2039 | u32 size_type_id = *type_id; | |
2040 | u32 size = 0; | |
2041 | ||
2042 | size_type = btf_type_by_id(btf, size_type_id); | |
b47a0bd2 | 2043 | if (btf_type_nosize_or_null(size_type)) |
eb3f595d MKL |
2044 | return NULL; |
2045 | ||
2046 | if (btf_type_has_size(size_type)) { | |
2047 | size = size_type->size; | |
2048 | } else if (btf_type_is_array(size_type)) { | |
951bb646 | 2049 | size = btf_resolved_type_size(btf, size_type_id); |
eb3f595d MKL |
2050 | } else if (btf_type_is_ptr(size_type)) { |
2051 | size = sizeof(void *); | |
2052 | } else { | |
1dc92851 DB |
2053 | if (WARN_ON_ONCE(!btf_type_is_modifier(size_type) && |
2054 | !btf_type_is_var(size_type))) | |
eb3f595d MKL |
2055 | return NULL; |
2056 | ||
951bb646 | 2057 | size_type_id = btf_resolved_type_id(btf, size_type_id); |
eb3f595d | 2058 | size_type = btf_type_by_id(btf, size_type_id); |
b47a0bd2 | 2059 | if (btf_type_nosize_or_null(size_type)) |
eb3f595d | 2060 | return NULL; |
1acc5d5c AN |
2061 | else if (btf_type_has_size(size_type)) |
2062 | size = size_type->size; | |
2063 | else if (btf_type_is_array(size_type)) | |
951bb646 | 2064 | size = btf_resolved_type_size(btf, size_type_id); |
1acc5d5c AN |
2065 | else if (btf_type_is_ptr(size_type)) |
2066 | size = sizeof(void *); | |
2067 | else | |
2068 | return NULL; | |
eb3f595d MKL |
2069 | } |
2070 | ||
2071 | *type_id = size_type_id; | |
2072 | if (ret_size) | |
2073 | *ret_size = size; | |
2074 | ||
2075 | return size_type; | |
2076 | } | |
2077 | ||
179cde8c MKL |
2078 | static int btf_df_check_member(struct btf_verifier_env *env, |
2079 | const struct btf_type *struct_type, | |
2080 | const struct btf_member *member, | |
2081 | const struct btf_type *member_type) | |
2082 | { | |
2083 | btf_verifier_log_basic(env, struct_type, | |
2084 | "Unsupported check_member"); | |
2085 | return -EINVAL; | |
2086 | } | |
2087 | ||
9d5f9f70 YS |
2088 | static int btf_df_check_kflag_member(struct btf_verifier_env *env, |
2089 | const struct btf_type *struct_type, | |
2090 | const struct btf_member *member, | |
2091 | const struct btf_type *member_type) | |
2092 | { | |
2093 | btf_verifier_log_basic(env, struct_type, | |
2094 | "Unsupported check_kflag_member"); | |
2095 | return -EINVAL; | |
2096 | } | |
2097 | ||
b1828f0b | 2098 | /* Used for ptr, array struct/union and float type members. |
9d5f9f70 YS |
2099 | * int, enum and modifier types have their specific callback functions. |
2100 | */ | |
2101 | static int btf_generic_check_kflag_member(struct btf_verifier_env *env, | |
2102 | const struct btf_type *struct_type, | |
2103 | const struct btf_member *member, | |
2104 | const struct btf_type *member_type) | |
2105 | { | |
2106 | if (BTF_MEMBER_BITFIELD_SIZE(member->offset)) { | |
2107 | btf_verifier_log_member(env, struct_type, member, | |
2108 | "Invalid member bitfield_size"); | |
2109 | return -EINVAL; | |
2110 | } | |
2111 | ||
2112 | /* bitfield size is 0, so member->offset represents bit offset only. | |
2113 | * It is safe to call non kflag check_member variants. | |
2114 | */ | |
2115 | return btf_type_ops(member_type)->check_member(env, struct_type, | |
2116 | member, | |
2117 | member_type); | |
2118 | } | |
2119 | ||
eb3f595d MKL |
2120 | static int btf_df_resolve(struct btf_verifier_env *env, |
2121 | const struct resolve_vertex *v) | |
2122 | { | |
2123 | btf_verifier_log_basic(env, v->t, "Unsupported resolve"); | |
2124 | return -EINVAL; | |
2125 | } | |
2126 | ||
31d0bc81 AM |
2127 | static void btf_df_show(const struct btf *btf, const struct btf_type *t, |
2128 | u32 type_id, void *data, u8 bits_offsets, | |
2129 | struct btf_show *show) | |
b00b8dae | 2130 | { |
31d0bc81 | 2131 | btf_show(show, "<unsupported kind:%u>", BTF_INFO_KIND(t->info)); |
b00b8dae MKL |
2132 | } |
2133 | ||
179cde8c MKL |
2134 | static int btf_int_check_member(struct btf_verifier_env *env, |
2135 | const struct btf_type *struct_type, | |
2136 | const struct btf_member *member, | |
2137 | const struct btf_type *member_type) | |
2138 | { | |
2139 | u32 int_data = btf_type_int(member_type); | |
2140 | u32 struct_bits_off = member->offset; | |
2141 | u32 struct_size = struct_type->size; | |
2142 | u32 nr_copy_bits; | |
2143 | u32 bytes_offset; | |
2144 | ||
2145 | if (U32_MAX - struct_bits_off < BTF_INT_OFFSET(int_data)) { | |
2146 | btf_verifier_log_member(env, struct_type, member, | |
2147 | "bits_offset exceeds U32_MAX"); | |
2148 | return -EINVAL; | |
2149 | } | |
2150 | ||
2151 | struct_bits_off += BTF_INT_OFFSET(int_data); | |
2152 | bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off); | |
2153 | nr_copy_bits = BTF_INT_BITS(int_data) + | |
2154 | BITS_PER_BYTE_MASKED(struct_bits_off); | |
2155 | ||
b1e8818c | 2156 | if (nr_copy_bits > BITS_PER_U128) { |
179cde8c | 2157 | btf_verifier_log_member(env, struct_type, member, |
b1e8818c | 2158 | "nr_copy_bits exceeds 128"); |
179cde8c MKL |
2159 | return -EINVAL; |
2160 | } | |
2161 | ||
2162 | if (struct_size < bytes_offset || | |
2163 | struct_size - bytes_offset < BITS_ROUNDUP_BYTES(nr_copy_bits)) { | |
2164 | btf_verifier_log_member(env, struct_type, member, | |
2165 | "Member exceeds struct_size"); | |
2166 | return -EINVAL; | |
2167 | } | |
2168 | ||
2169 | return 0; | |
2170 | } | |
2171 | ||
9d5f9f70 YS |
2172 | static int btf_int_check_kflag_member(struct btf_verifier_env *env, |
2173 | const struct btf_type *struct_type, | |
2174 | const struct btf_member *member, | |
2175 | const struct btf_type *member_type) | |
2176 | { | |
2177 | u32 struct_bits_off, nr_bits, nr_int_data_bits, bytes_offset; | |
2178 | u32 int_data = btf_type_int(member_type); | |
2179 | u32 struct_size = struct_type->size; | |
2180 | u32 nr_copy_bits; | |
2181 | ||
2182 | /* a regular int type is required for the kflag int member */ | |
2183 | if (!btf_type_int_is_regular(member_type)) { | |
2184 | btf_verifier_log_member(env, struct_type, member, | |
2185 | "Invalid member base type"); | |
2186 | return -EINVAL; | |
2187 | } | |
2188 | ||
2189 | /* check sanity of bitfield size */ | |
2190 | nr_bits = BTF_MEMBER_BITFIELD_SIZE(member->offset); | |
2191 | struct_bits_off = BTF_MEMBER_BIT_OFFSET(member->offset); | |
2192 | nr_int_data_bits = BTF_INT_BITS(int_data); | |
2193 | if (!nr_bits) { | |
2194 | /* Not a bitfield member, member offset must be at byte | |
2195 | * boundary. | |
2196 | */ | |
2197 | if (BITS_PER_BYTE_MASKED(struct_bits_off)) { | |
2198 | btf_verifier_log_member(env, struct_type, member, | |
2199 | "Invalid member offset"); | |
2200 | return -EINVAL; | |
2201 | } | |
2202 | ||
2203 | nr_bits = nr_int_data_bits; | |
2204 | } else if (nr_bits > nr_int_data_bits) { | |
2205 | btf_verifier_log_member(env, struct_type, member, | |
2206 | "Invalid member bitfield_size"); | |
2207 | return -EINVAL; | |
2208 | } | |
2209 | ||
2210 | bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off); | |
2211 | nr_copy_bits = nr_bits + BITS_PER_BYTE_MASKED(struct_bits_off); | |
b1e8818c | 2212 | if (nr_copy_bits > BITS_PER_U128) { |
9d5f9f70 | 2213 | btf_verifier_log_member(env, struct_type, member, |
b1e8818c | 2214 | "nr_copy_bits exceeds 128"); |
9d5f9f70 YS |
2215 | return -EINVAL; |
2216 | } | |
2217 | ||
2218 | if (struct_size < bytes_offset || | |
2219 | struct_size - bytes_offset < BITS_ROUNDUP_BYTES(nr_copy_bits)) { | |
2220 | btf_verifier_log_member(env, struct_type, member, | |
2221 | "Member exceeds struct_size"); | |
2222 | return -EINVAL; | |
2223 | } | |
2224 | ||
2225 | return 0; | |
2226 | } | |
2227 | ||
69b693f0 MKL |
2228 | static s32 btf_int_check_meta(struct btf_verifier_env *env, |
2229 | const struct btf_type *t, | |
2230 | u32 meta_left) | |
2231 | { | |
2232 | u32 int_data, nr_bits, meta_needed = sizeof(int_data); | |
2233 | u16 encoding; | |
2234 | ||
2235 | if (meta_left < meta_needed) { | |
2236 | btf_verifier_log_basic(env, t, | |
2237 | "meta_left:%u meta_needed:%u", | |
2238 | meta_left, meta_needed); | |
2239 | return -EINVAL; | |
2240 | } | |
2241 | ||
2242 | if (btf_type_vlen(t)) { | |
2243 | btf_verifier_log_type(env, t, "vlen != 0"); | |
2244 | return -EINVAL; | |
2245 | } | |
2246 | ||
9d5f9f70 YS |
2247 | if (btf_type_kflag(t)) { |
2248 | btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); | |
2249 | return -EINVAL; | |
2250 | } | |
2251 | ||
69b693f0 | 2252 | int_data = btf_type_int(t); |
aea2f7b8 MKL |
2253 | if (int_data & ~BTF_INT_MASK) { |
2254 | btf_verifier_log_basic(env, t, "Invalid int_data:%x", | |
2255 | int_data); | |
2256 | return -EINVAL; | |
2257 | } | |
2258 | ||
69b693f0 MKL |
2259 | nr_bits = BTF_INT_BITS(int_data) + BTF_INT_OFFSET(int_data); |
2260 | ||
b1e8818c | 2261 | if (nr_bits > BITS_PER_U128) { |
69b693f0 | 2262 | btf_verifier_log_type(env, t, "nr_bits exceeds %zu", |
b1e8818c | 2263 | BITS_PER_U128); |
69b693f0 MKL |
2264 | return -EINVAL; |
2265 | } | |
2266 | ||
2267 | if (BITS_ROUNDUP_BYTES(nr_bits) > t->size) { | |
2268 | btf_verifier_log_type(env, t, "nr_bits exceeds type_size"); | |
2269 | return -EINVAL; | |
2270 | } | |
2271 | ||
aea2f7b8 MKL |
2272 | /* |
2273 | * Only one of the encoding bits is allowed and it | |
2274 | * should be sufficient for the pretty print purpose (i.e. decoding). | |
2275 | * Multiple bits can be allowed later if it is found | |
2276 | * to be insufficient. | |
2277 | */ | |
69b693f0 MKL |
2278 | encoding = BTF_INT_ENCODING(int_data); |
2279 | if (encoding && | |
2280 | encoding != BTF_INT_SIGNED && | |
2281 | encoding != BTF_INT_CHAR && | |
aea2f7b8 | 2282 | encoding != BTF_INT_BOOL) { |
69b693f0 MKL |
2283 | btf_verifier_log_type(env, t, "Unsupported encoding"); |
2284 | return -ENOTSUPP; | |
2285 | } | |
2286 | ||
2287 | btf_verifier_log_type(env, t, NULL); | |
2288 | ||
2289 | return meta_needed; | |
2290 | } | |
2291 | ||
2292 | static void btf_int_log(struct btf_verifier_env *env, | |
2293 | const struct btf_type *t) | |
2294 | { | |
2295 | int int_data = btf_type_int(t); | |
2296 | ||
2297 | btf_verifier_log(env, | |
2298 | "size=%u bits_offset=%u nr_bits=%u encoding=%s", | |
2299 | t->size, BTF_INT_OFFSET(int_data), | |
2300 | BTF_INT_BITS(int_data), | |
2301 | btf_int_encoding_str(BTF_INT_ENCODING(int_data))); | |
2302 | } | |
2303 | ||
31d0bc81 | 2304 | static void btf_int128_print(struct btf_show *show, void *data) |
b1e8818c YS |
2305 | { |
2306 | /* data points to a __int128 number. | |
2307 | * Suppose | |
2308 | * int128_num = *(__int128 *)data; | |
2309 | * The below formulas shows what upper_num and lower_num represents: | |
2310 | * upper_num = int128_num >> 64; | |
2311 | * lower_num = int128_num & 0xffffffffFFFFFFFFULL; | |
2312 | */ | |
2313 | u64 upper_num, lower_num; | |
2314 | ||
2315 | #ifdef __BIG_ENDIAN_BITFIELD | |
2316 | upper_num = *(u64 *)data; | |
2317 | lower_num = *(u64 *)(data + 8); | |
2318 | #else | |
2319 | upper_num = *(u64 *)(data + 8); | |
2320 | lower_num = *(u64 *)data; | |
2321 | #endif | |
2322 | if (upper_num == 0) | |
31d0bc81 | 2323 | btf_show_type_value(show, "0x%llx", lower_num); |
b1e8818c | 2324 | else |
31d0bc81 AM |
2325 | btf_show_type_values(show, "0x%llx%016llx", upper_num, |
2326 | lower_num); | |
b1e8818c YS |
2327 | } |
2328 | ||
2329 | static void btf_int128_shift(u64 *print_num, u16 left_shift_bits, | |
2330 | u16 right_shift_bits) | |
2331 | { | |
2332 | u64 upper_num, lower_num; | |
2333 | ||
2334 | #ifdef __BIG_ENDIAN_BITFIELD | |
2335 | upper_num = print_num[0]; | |
2336 | lower_num = print_num[1]; | |
2337 | #else | |
2338 | upper_num = print_num[1]; | |
2339 | lower_num = print_num[0]; | |
2340 | #endif | |
2341 | ||
2342 | /* shake out un-needed bits by shift/or operations */ | |
2343 | if (left_shift_bits >= 64) { | |
2344 | upper_num = lower_num << (left_shift_bits - 64); | |
2345 | lower_num = 0; | |
2346 | } else { | |
2347 | upper_num = (upper_num << left_shift_bits) | | |
2348 | (lower_num >> (64 - left_shift_bits)); | |
2349 | lower_num = lower_num << left_shift_bits; | |
2350 | } | |
2351 | ||
2352 | if (right_shift_bits >= 64) { | |
2353 | lower_num = upper_num >> (right_shift_bits - 64); | |
2354 | upper_num = 0; | |
2355 | } else { | |
2356 | lower_num = (lower_num >> right_shift_bits) | | |
2357 | (upper_num << (64 - right_shift_bits)); | |
2358 | upper_num = upper_num >> right_shift_bits; | |
2359 | } | |
2360 | ||
2361 | #ifdef __BIG_ENDIAN_BITFIELD | |
2362 | print_num[0] = upper_num; | |
2363 | print_num[1] = lower_num; | |
2364 | #else | |
2365 | print_num[0] = lower_num; | |
2366 | print_num[1] = upper_num; | |
2367 | #endif | |
2368 | } | |
2369 | ||
31d0bc81 AM |
2370 | static void btf_bitfield_show(void *data, u8 bits_offset, |
2371 | u8 nr_bits, struct btf_show *show) | |
b00b8dae | 2372 | { |
b65f370d | 2373 | u16 left_shift_bits, right_shift_bits; |
36fc3c8c MKL |
2374 | u8 nr_copy_bytes; |
2375 | u8 nr_copy_bits; | |
b1e8818c | 2376 | u64 print_num[2] = {}; |
b00b8dae | 2377 | |
b00b8dae MKL |
2378 | nr_copy_bits = nr_bits + bits_offset; |
2379 | nr_copy_bytes = BITS_ROUNDUP_BYTES(nr_copy_bits); | |
2380 | ||
b1e8818c | 2381 | memcpy(print_num, data, nr_copy_bytes); |
b00b8dae | 2382 | |
b65f370d OK |
2383 | #ifdef __BIG_ENDIAN_BITFIELD |
2384 | left_shift_bits = bits_offset; | |
2385 | #else | |
b1e8818c | 2386 | left_shift_bits = BITS_PER_U128 - nr_copy_bits; |
b65f370d | 2387 | #endif |
b1e8818c | 2388 | right_shift_bits = BITS_PER_U128 - nr_bits; |
b00b8dae | 2389 | |
b1e8818c | 2390 | btf_int128_shift(print_num, left_shift_bits, right_shift_bits); |
31d0bc81 | 2391 | btf_int128_print(show, print_num); |
b00b8dae MKL |
2392 | } |
2393 | ||
9d5f9f70 | 2394 | |
31d0bc81 AM |
2395 | static void btf_int_bits_show(const struct btf *btf, |
2396 | const struct btf_type *t, | |
2397 | void *data, u8 bits_offset, | |
2398 | struct btf_show *show) | |
f97be3ab YS |
2399 | { |
2400 | u32 int_data = btf_type_int(t); | |
2401 | u8 nr_bits = BTF_INT_BITS(int_data); | |
2402 | u8 total_bits_offset; | |
2403 | ||
2404 | /* | |
2405 | * bits_offset is at most 7. | |
b1e8818c | 2406 | * BTF_INT_OFFSET() cannot exceed 128 bits. |
f97be3ab YS |
2407 | */ |
2408 | total_bits_offset = bits_offset + BTF_INT_OFFSET(int_data); | |
17e3ac81 YS |
2409 | data += BITS_ROUNDDOWN_BYTES(total_bits_offset); |
2410 | bits_offset = BITS_PER_BYTE_MASKED(total_bits_offset); | |
31d0bc81 | 2411 | btf_bitfield_show(data, bits_offset, nr_bits, show); |
f97be3ab YS |
2412 | } |
2413 | ||
31d0bc81 AM |
2414 | static void btf_int_show(const struct btf *btf, const struct btf_type *t, |
2415 | u32 type_id, void *data, u8 bits_offset, | |
2416 | struct btf_show *show) | |
b00b8dae MKL |
2417 | { |
2418 | u32 int_data = btf_type_int(t); | |
2419 | u8 encoding = BTF_INT_ENCODING(int_data); | |
2420 | bool sign = encoding & BTF_INT_SIGNED; | |
36fc3c8c | 2421 | u8 nr_bits = BTF_INT_BITS(int_data); |
31d0bc81 AM |
2422 | void *safe_data; |
2423 | ||
2424 | safe_data = btf_show_start_type(show, t, type_id, data); | |
2425 | if (!safe_data) | |
2426 | return; | |
b00b8dae MKL |
2427 | |
2428 | if (bits_offset || BTF_INT_OFFSET(int_data) || | |
2429 | BITS_PER_BYTE_MASKED(nr_bits)) { | |
31d0bc81 AM |
2430 | btf_int_bits_show(btf, t, safe_data, bits_offset, show); |
2431 | goto out; | |
b00b8dae MKL |
2432 | } |
2433 | ||
2434 | switch (nr_bits) { | |
b1e8818c | 2435 | case 128: |
31d0bc81 | 2436 | btf_int128_print(show, safe_data); |
b1e8818c | 2437 | break; |
b00b8dae MKL |
2438 | case 64: |
2439 | if (sign) | |
31d0bc81 | 2440 | btf_show_type_value(show, "%lld", *(s64 *)safe_data); |
b00b8dae | 2441 | else |
31d0bc81 | 2442 | btf_show_type_value(show, "%llu", *(u64 *)safe_data); |
b00b8dae MKL |
2443 | break; |
2444 | case 32: | |
2445 | if (sign) | |
31d0bc81 | 2446 | btf_show_type_value(show, "%d", *(s32 *)safe_data); |
b00b8dae | 2447 | else |
31d0bc81 | 2448 | btf_show_type_value(show, "%u", *(u32 *)safe_data); |
b00b8dae MKL |
2449 | break; |
2450 | case 16: | |
2451 | if (sign) | |
31d0bc81 | 2452 | btf_show_type_value(show, "%d", *(s16 *)safe_data); |
b00b8dae | 2453 | else |
31d0bc81 | 2454 | btf_show_type_value(show, "%u", *(u16 *)safe_data); |
b00b8dae MKL |
2455 | break; |
2456 | case 8: | |
31d0bc81 AM |
2457 | if (show->state.array_encoding == BTF_INT_CHAR) { |
2458 | /* check for null terminator */ | |
2459 | if (show->state.array_terminated) | |
2460 | break; | |
2461 | if (*(char *)data == '\0') { | |
2462 | show->state.array_terminated = 1; | |
2463 | break; | |
2464 | } | |
2465 | if (isprint(*(char *)data)) { | |
2466 | btf_show_type_value(show, "'%c'", | |
2467 | *(char *)safe_data); | |
2468 | break; | |
2469 | } | |
2470 | } | |
b00b8dae | 2471 | if (sign) |
31d0bc81 | 2472 | btf_show_type_value(show, "%d", *(s8 *)safe_data); |
b00b8dae | 2473 | else |
31d0bc81 | 2474 | btf_show_type_value(show, "%u", *(u8 *)safe_data); |
b00b8dae MKL |
2475 | break; |
2476 | default: | |
31d0bc81 AM |
2477 | btf_int_bits_show(btf, t, safe_data, bits_offset, show); |
2478 | break; | |
b00b8dae | 2479 | } |
31d0bc81 AM |
2480 | out: |
2481 | btf_show_end_type(show); | |
b00b8dae MKL |
2482 | } |
2483 | ||
69b693f0 MKL |
2484 | static const struct btf_kind_operations int_ops = { |
2485 | .check_meta = btf_int_check_meta, | |
eb3f595d | 2486 | .resolve = btf_df_resolve, |
179cde8c | 2487 | .check_member = btf_int_check_member, |
9d5f9f70 | 2488 | .check_kflag_member = btf_int_check_kflag_member, |
69b693f0 | 2489 | .log_details = btf_int_log, |
31d0bc81 | 2490 | .show = btf_int_show, |
69b693f0 MKL |
2491 | }; |
2492 | ||
179cde8c MKL |
2493 | static int btf_modifier_check_member(struct btf_verifier_env *env, |
2494 | const struct btf_type *struct_type, | |
2495 | const struct btf_member *member, | |
2496 | const struct btf_type *member_type) | |
2497 | { | |
2498 | const struct btf_type *resolved_type; | |
2499 | u32 resolved_type_id = member->type; | |
2500 | struct btf_member resolved_member; | |
2501 | struct btf *btf = env->btf; | |
2502 | ||
2503 | resolved_type = btf_type_id_size(btf, &resolved_type_id, NULL); | |
2504 | if (!resolved_type) { | |
2505 | btf_verifier_log_member(env, struct_type, member, | |
2506 | "Invalid member"); | |
2507 | return -EINVAL; | |
2508 | } | |
2509 | ||
2510 | resolved_member = *member; | |
2511 | resolved_member.type = resolved_type_id; | |
2512 | ||
2513 | return btf_type_ops(resolved_type)->check_member(env, struct_type, | |
2514 | &resolved_member, | |
2515 | resolved_type); | |
2516 | } | |
2517 | ||
9d5f9f70 YS |
2518 | static int btf_modifier_check_kflag_member(struct btf_verifier_env *env, |
2519 | const struct btf_type *struct_type, | |
2520 | const struct btf_member *member, | |
2521 | const struct btf_type *member_type) | |
2522 | { | |
2523 | const struct btf_type *resolved_type; | |
2524 | u32 resolved_type_id = member->type; | |
2525 | struct btf_member resolved_member; | |
2526 | struct btf *btf = env->btf; | |
2527 | ||
2528 | resolved_type = btf_type_id_size(btf, &resolved_type_id, NULL); | |
2529 | if (!resolved_type) { | |
2530 | btf_verifier_log_member(env, struct_type, member, | |
2531 | "Invalid member"); | |
2532 | return -EINVAL; | |
2533 | } | |
2534 | ||
2535 | resolved_member = *member; | |
2536 | resolved_member.type = resolved_type_id; | |
2537 | ||
2538 | return btf_type_ops(resolved_type)->check_kflag_member(env, struct_type, | |
2539 | &resolved_member, | |
2540 | resolved_type); | |
2541 | } | |
2542 | ||
179cde8c MKL |
2543 | static int btf_ptr_check_member(struct btf_verifier_env *env, |
2544 | const struct btf_type *struct_type, | |
2545 | const struct btf_member *member, | |
2546 | const struct btf_type *member_type) | |
2547 | { | |
2548 | u32 struct_size, struct_bits_off, bytes_offset; | |
2549 | ||
2550 | struct_size = struct_type->size; | |
2551 | struct_bits_off = member->offset; | |
2552 | bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off); | |
2553 | ||
2554 | if (BITS_PER_BYTE_MASKED(struct_bits_off)) { | |
2555 | btf_verifier_log_member(env, struct_type, member, | |
2556 | "Member is not byte aligned"); | |
2557 | return -EINVAL; | |
2558 | } | |
2559 | ||
2560 | if (struct_size - bytes_offset < sizeof(void *)) { | |
2561 | btf_verifier_log_member(env, struct_type, member, | |
2562 | "Member exceeds struct_size"); | |
2563 | return -EINVAL; | |
2564 | } | |
2565 | ||
2566 | return 0; | |
2567 | } | |
2568 | ||
69b693f0 MKL |
2569 | static int btf_ref_type_check_meta(struct btf_verifier_env *env, |
2570 | const struct btf_type *t, | |
2571 | u32 meta_left) | |
2572 | { | |
8c42d2fa YS |
2573 | const char *value; |
2574 | ||
69b693f0 MKL |
2575 | if (btf_type_vlen(t)) { |
2576 | btf_verifier_log_type(env, t, "vlen != 0"); | |
2577 | return -EINVAL; | |
2578 | } | |
2579 | ||
53ee0d66 | 2580 | if (btf_type_kflag(t) && !btf_type_is_type_tag(t)) { |
9d5f9f70 YS |
2581 | btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); |
2582 | return -EINVAL; | |
2583 | } | |
2584 | ||
aea2f7b8 | 2585 | if (!BTF_TYPE_ID_VALID(t->type)) { |
69b693f0 MKL |
2586 | btf_verifier_log_type(env, t, "Invalid type_id"); |
2587 | return -EINVAL; | |
2588 | } | |
2589 | ||
8c42d2fa | 2590 | /* typedef/type_tag type must have a valid name, and other ref types, |
eb04bbb6 YS |
2591 | * volatile, const, restrict, should have a null name. |
2592 | */ | |
2593 | if (BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF) { | |
2594 | if (!t->name_off || | |
2595 | !btf_name_valid_identifier(env->btf, t->name_off)) { | |
2596 | btf_verifier_log_type(env, t, "Invalid name"); | |
2597 | return -EINVAL; | |
2598 | } | |
8c42d2fa YS |
2599 | } else if (BTF_INFO_KIND(t->info) == BTF_KIND_TYPE_TAG) { |
2600 | value = btf_name_by_offset(env->btf, t->name_off); | |
2601 | if (!value || !value[0]) { | |
2602 | btf_verifier_log_type(env, t, "Invalid name"); | |
2603 | return -EINVAL; | |
2604 | } | |
eb04bbb6 YS |
2605 | } else { |
2606 | if (t->name_off) { | |
2607 | btf_verifier_log_type(env, t, "Invalid name"); | |
2608 | return -EINVAL; | |
2609 | } | |
2610 | } | |
2611 | ||
69b693f0 MKL |
2612 | btf_verifier_log_type(env, t, NULL); |
2613 | ||
2614 | return 0; | |
2615 | } | |
2616 | ||
eb3f595d MKL |
2617 | static int btf_modifier_resolve(struct btf_verifier_env *env, |
2618 | const struct resolve_vertex *v) | |
2619 | { | |
2620 | const struct btf_type *t = v->t; | |
2621 | const struct btf_type *next_type; | |
2622 | u32 next_type_id = t->type; | |
2623 | struct btf *btf = env->btf; | |
eb3f595d MKL |
2624 | |
2625 | next_type = btf_type_by_id(btf, next_type_id); | |
1dc92851 | 2626 | if (!next_type || btf_type_is_resolve_source_only(next_type)) { |
eb3f595d MKL |
2627 | btf_verifier_log_type(env, v->t, "Invalid type_id"); |
2628 | return -EINVAL; | |
2629 | } | |
2630 | ||
eb3f595d MKL |
2631 | if (!env_type_is_resolve_sink(env, next_type) && |
2632 | !env_type_is_resolved(env, next_type_id)) | |
2633 | return env_stack_push(env, next_type, next_type_id); | |
2634 | ||
2635 | /* Figure out the resolved next_type_id with size. | |
2636 | * They will be stored in the current modifier's | |
2637 | * resolved_ids and resolved_sizes such that it can | |
2638 | * save us a few type-following when we use it later (e.g. in | |
2639 | * pretty print). | |
2640 | */ | |
1acc5d5c | 2641 | if (!btf_type_id_size(btf, &next_type_id, NULL)) { |
2667a262 MKL |
2642 | if (env_type_is_resolved(env, next_type_id)) |
2643 | next_type = btf_type_id_resolve(btf, &next_type_id); | |
2644 | ||
2645 | /* "typedef void new_void", "const void"...etc */ | |
2646 | if (!btf_type_is_void(next_type) && | |
81f5c6f5 YS |
2647 | !btf_type_is_fwd(next_type) && |
2648 | !btf_type_is_func_proto(next_type)) { | |
2667a262 MKL |
2649 | btf_verifier_log_type(env, v->t, "Invalid type_id"); |
2650 | return -EINVAL; | |
2651 | } | |
eb3f595d MKL |
2652 | } |
2653 | ||
1acc5d5c | 2654 | env_stack_pop_resolved(env, next_type_id, 0); |
eb3f595d MKL |
2655 | |
2656 | return 0; | |
2657 | } | |
2658 | ||
1dc92851 DB |
2659 | static int btf_var_resolve(struct btf_verifier_env *env, |
2660 | const struct resolve_vertex *v) | |
2661 | { | |
2662 | const struct btf_type *next_type; | |
2663 | const struct btf_type *t = v->t; | |
2664 | u32 next_type_id = t->type; | |
2665 | struct btf *btf = env->btf; | |
1dc92851 DB |
2666 | |
2667 | next_type = btf_type_by_id(btf, next_type_id); | |
2668 | if (!next_type || btf_type_is_resolve_source_only(next_type)) { | |
2669 | btf_verifier_log_type(env, v->t, "Invalid type_id"); | |
2670 | return -EINVAL; | |
2671 | } | |
2672 | ||
2673 | if (!env_type_is_resolve_sink(env, next_type) && | |
2674 | !env_type_is_resolved(env, next_type_id)) | |
2675 | return env_stack_push(env, next_type, next_type_id); | |
2676 | ||
2677 | if (btf_type_is_modifier(next_type)) { | |
2678 | const struct btf_type *resolved_type; | |
2679 | u32 resolved_type_id; | |
2680 | ||
2681 | resolved_type_id = next_type_id; | |
2682 | resolved_type = btf_type_id_resolve(btf, &resolved_type_id); | |
2683 | ||
2684 | if (btf_type_is_ptr(resolved_type) && | |
2685 | !env_type_is_resolve_sink(env, resolved_type) && | |
2686 | !env_type_is_resolved(env, resolved_type_id)) | |
2687 | return env_stack_push(env, resolved_type, | |
2688 | resolved_type_id); | |
2689 | } | |
2690 | ||
2691 | /* We must resolve to something concrete at this point, no | |
2692 | * forward types or similar that would resolve to size of | |
2693 | * zero is allowed. | |
2694 | */ | |
1acc5d5c | 2695 | if (!btf_type_id_size(btf, &next_type_id, NULL)) { |
1dc92851 DB |
2696 | btf_verifier_log_type(env, v->t, "Invalid type_id"); |
2697 | return -EINVAL; | |
2698 | } | |
2699 | ||
1acc5d5c | 2700 | env_stack_pop_resolved(env, next_type_id, 0); |
1dc92851 DB |
2701 | |
2702 | return 0; | |
2703 | } | |
2704 | ||
eb3f595d MKL |
2705 | static int btf_ptr_resolve(struct btf_verifier_env *env, |
2706 | const struct resolve_vertex *v) | |
2707 | { | |
2708 | const struct btf_type *next_type; | |
2709 | const struct btf_type *t = v->t; | |
2710 | u32 next_type_id = t->type; | |
2711 | struct btf *btf = env->btf; | |
eb3f595d MKL |
2712 | |
2713 | next_type = btf_type_by_id(btf, next_type_id); | |
1dc92851 | 2714 | if (!next_type || btf_type_is_resolve_source_only(next_type)) { |
eb3f595d MKL |
2715 | btf_verifier_log_type(env, v->t, "Invalid type_id"); |
2716 | return -EINVAL; | |
2717 | } | |
2718 | ||
eb3f595d MKL |
2719 | if (!env_type_is_resolve_sink(env, next_type) && |
2720 | !env_type_is_resolved(env, next_type_id)) | |
2721 | return env_stack_push(env, next_type, next_type_id); | |
2722 | ||
2723 | /* If the modifier was RESOLVED during RESOLVE_STRUCT_OR_ARRAY, | |
2724 | * the modifier may have stopped resolving when it was resolved | |
2725 | * to a ptr (last-resolved-ptr). | |
2726 | * | |
2727 | * We now need to continue from the last-resolved-ptr to | |
2728 | * ensure the last-resolved-ptr will not referring back to | |
c561d110 | 2729 | * the current ptr (t). |
eb3f595d MKL |
2730 | */ |
2731 | if (btf_type_is_modifier(next_type)) { | |
2732 | const struct btf_type *resolved_type; | |
2733 | u32 resolved_type_id; | |
2734 | ||
2735 | resolved_type_id = next_type_id; | |
2736 | resolved_type = btf_type_id_resolve(btf, &resolved_type_id); | |
2737 | ||
2738 | if (btf_type_is_ptr(resolved_type) && | |
2739 | !env_type_is_resolve_sink(env, resolved_type) && | |
2740 | !env_type_is_resolved(env, resolved_type_id)) | |
2741 | return env_stack_push(env, resolved_type, | |
2742 | resolved_type_id); | |
2743 | } | |
2744 | ||
2667a262 MKL |
2745 | if (!btf_type_id_size(btf, &next_type_id, NULL)) { |
2746 | if (env_type_is_resolved(env, next_type_id)) | |
2747 | next_type = btf_type_id_resolve(btf, &next_type_id); | |
2748 | ||
2749 | if (!btf_type_is_void(next_type) && | |
2750 | !btf_type_is_fwd(next_type) && | |
2751 | !btf_type_is_func_proto(next_type)) { | |
2752 | btf_verifier_log_type(env, v->t, "Invalid type_id"); | |
2753 | return -EINVAL; | |
2754 | } | |
eb3f595d MKL |
2755 | } |
2756 | ||
eb3f595d MKL |
2757 | env_stack_pop_resolved(env, next_type_id, 0); |
2758 | ||
2759 | return 0; | |
2760 | } | |
2761 | ||
31d0bc81 AM |
2762 | static void btf_modifier_show(const struct btf *btf, |
2763 | const struct btf_type *t, | |
2764 | u32 type_id, void *data, | |
2765 | u8 bits_offset, struct btf_show *show) | |
b00b8dae | 2766 | { |
85d33df3 MKL |
2767 | if (btf->resolved_ids) |
2768 | t = btf_type_id_resolve(btf, &type_id); | |
2769 | else | |
2770 | t = btf_type_skip_modifiers(btf, type_id, NULL); | |
b00b8dae | 2771 | |
31d0bc81 | 2772 | btf_type_ops(t)->show(btf, t, type_id, data, bits_offset, show); |
b00b8dae MKL |
2773 | } |
2774 | ||
31d0bc81 AM |
2775 | static void btf_var_show(const struct btf *btf, const struct btf_type *t, |
2776 | u32 type_id, void *data, u8 bits_offset, | |
2777 | struct btf_show *show) | |
1dc92851 DB |
2778 | { |
2779 | t = btf_type_id_resolve(btf, &type_id); | |
2780 | ||
31d0bc81 | 2781 | btf_type_ops(t)->show(btf, t, type_id, data, bits_offset, show); |
1dc92851 DB |
2782 | } |
2783 | ||
31d0bc81 AM |
2784 | static void btf_ptr_show(const struct btf *btf, const struct btf_type *t, |
2785 | u32 type_id, void *data, u8 bits_offset, | |
2786 | struct btf_show *show) | |
b00b8dae | 2787 | { |
31d0bc81 AM |
2788 | void *safe_data; |
2789 | ||
2790 | safe_data = btf_show_start_type(show, t, type_id, data); | |
2791 | if (!safe_data) | |
2792 | return; | |
2793 | ||
2794 | /* It is a hashed value unless BTF_SHOW_PTR_RAW is specified */ | |
2795 | if (show->flags & BTF_SHOW_PTR_RAW) | |
2796 | btf_show_type_value(show, "0x%px", *(void **)safe_data); | |
2797 | else | |
2798 | btf_show_type_value(show, "0x%p", *(void **)safe_data); | |
2799 | btf_show_end_type(show); | |
b00b8dae MKL |
2800 | } |
2801 | ||
69b693f0 MKL |
2802 | static void btf_ref_type_log(struct btf_verifier_env *env, |
2803 | const struct btf_type *t) | |
2804 | { | |
2805 | btf_verifier_log(env, "type_id=%u", t->type); | |
2806 | } | |
2807 | ||
7bae563c | 2808 | static const struct btf_kind_operations modifier_ops = { |
69b693f0 | 2809 | .check_meta = btf_ref_type_check_meta, |
eb3f595d | 2810 | .resolve = btf_modifier_resolve, |
179cde8c | 2811 | .check_member = btf_modifier_check_member, |
9d5f9f70 | 2812 | .check_kflag_member = btf_modifier_check_kflag_member, |
69b693f0 | 2813 | .log_details = btf_ref_type_log, |
31d0bc81 | 2814 | .show = btf_modifier_show, |
69b693f0 MKL |
2815 | }; |
2816 | ||
7bae563c | 2817 | static const struct btf_kind_operations ptr_ops = { |
69b693f0 | 2818 | .check_meta = btf_ref_type_check_meta, |
eb3f595d | 2819 | .resolve = btf_ptr_resolve, |
179cde8c | 2820 | .check_member = btf_ptr_check_member, |
9d5f9f70 | 2821 | .check_kflag_member = btf_generic_check_kflag_member, |
69b693f0 | 2822 | .log_details = btf_ref_type_log, |
31d0bc81 | 2823 | .show = btf_ptr_show, |
69b693f0 MKL |
2824 | }; |
2825 | ||
8175383f MKL |
2826 | static s32 btf_fwd_check_meta(struct btf_verifier_env *env, |
2827 | const struct btf_type *t, | |
2828 | u32 meta_left) | |
2829 | { | |
2830 | if (btf_type_vlen(t)) { | |
2831 | btf_verifier_log_type(env, t, "vlen != 0"); | |
2832 | return -EINVAL; | |
2833 | } | |
2834 | ||
2835 | if (t->type) { | |
2836 | btf_verifier_log_type(env, t, "type != 0"); | |
2837 | return -EINVAL; | |
2838 | } | |
2839 | ||
eb04bbb6 YS |
2840 | /* fwd type must have a valid name */ |
2841 | if (!t->name_off || | |
2842 | !btf_name_valid_identifier(env->btf, t->name_off)) { | |
2843 | btf_verifier_log_type(env, t, "Invalid name"); | |
2844 | return -EINVAL; | |
2845 | } | |
2846 | ||
8175383f MKL |
2847 | btf_verifier_log_type(env, t, NULL); |
2848 | ||
2849 | return 0; | |
2850 | } | |
2851 | ||
76c43ae8 YS |
2852 | static void btf_fwd_type_log(struct btf_verifier_env *env, |
2853 | const struct btf_type *t) | |
2854 | { | |
2855 | btf_verifier_log(env, "%s", btf_type_kflag(t) ? "union" : "struct"); | |
2856 | } | |
2857 | ||
7bae563c | 2858 | static const struct btf_kind_operations fwd_ops = { |
8175383f | 2859 | .check_meta = btf_fwd_check_meta, |
eb3f595d | 2860 | .resolve = btf_df_resolve, |
179cde8c | 2861 | .check_member = btf_df_check_member, |
9d5f9f70 | 2862 | .check_kflag_member = btf_df_check_kflag_member, |
76c43ae8 | 2863 | .log_details = btf_fwd_type_log, |
31d0bc81 | 2864 | .show = btf_df_show, |
69b693f0 MKL |
2865 | }; |
2866 | ||
179cde8c MKL |
2867 | static int btf_array_check_member(struct btf_verifier_env *env, |
2868 | const struct btf_type *struct_type, | |
2869 | const struct btf_member *member, | |
2870 | const struct btf_type *member_type) | |
2871 | { | |
2872 | u32 struct_bits_off = member->offset; | |
2873 | u32 struct_size, bytes_offset; | |
2874 | u32 array_type_id, array_size; | |
2875 | struct btf *btf = env->btf; | |
2876 | ||
2877 | if (BITS_PER_BYTE_MASKED(struct_bits_off)) { | |
2878 | btf_verifier_log_member(env, struct_type, member, | |
2879 | "Member is not byte aligned"); | |
2880 | return -EINVAL; | |
2881 | } | |
2882 | ||
2883 | array_type_id = member->type; | |
2884 | btf_type_id_size(btf, &array_type_id, &array_size); | |
2885 | struct_size = struct_type->size; | |
2886 | bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off); | |
2887 | if (struct_size - bytes_offset < array_size) { | |
2888 | btf_verifier_log_member(env, struct_type, member, | |
2889 | "Member exceeds struct_size"); | |
2890 | return -EINVAL; | |
2891 | } | |
2892 | ||
2893 | return 0; | |
2894 | } | |
2895 | ||
69b693f0 MKL |
2896 | static s32 btf_array_check_meta(struct btf_verifier_env *env, |
2897 | const struct btf_type *t, | |
2898 | u32 meta_left) | |
2899 | { | |
2900 | const struct btf_array *array = btf_type_array(t); | |
2901 | u32 meta_needed = sizeof(*array); | |
2902 | ||
2903 | if (meta_left < meta_needed) { | |
2904 | btf_verifier_log_basic(env, t, | |
2905 | "meta_left:%u meta_needed:%u", | |
2906 | meta_left, meta_needed); | |
2907 | return -EINVAL; | |
2908 | } | |
2909 | ||
eb04bbb6 YS |
2910 | /* array type should not have a name */ |
2911 | if (t->name_off) { | |
2912 | btf_verifier_log_type(env, t, "Invalid name"); | |
2913 | return -EINVAL; | |
2914 | } | |
2915 | ||
69b693f0 MKL |
2916 | if (btf_type_vlen(t)) { |
2917 | btf_verifier_log_type(env, t, "vlen != 0"); | |
2918 | return -EINVAL; | |
2919 | } | |
2920 | ||
9d5f9f70 YS |
2921 | if (btf_type_kflag(t)) { |
2922 | btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); | |
2923 | return -EINVAL; | |
2924 | } | |
2925 | ||
b9308ae6 MKL |
2926 | if (t->size) { |
2927 | btf_verifier_log_type(env, t, "size != 0"); | |
2928 | return -EINVAL; | |
2929 | } | |
2930 | ||
4ef5f574 MKL |
2931 | /* Array elem type and index type cannot be in type void, |
2932 | * so !array->type and !array->index_type are not allowed. | |
69b693f0 | 2933 | */ |
aea2f7b8 | 2934 | if (!array->type || !BTF_TYPE_ID_VALID(array->type)) { |
4ef5f574 MKL |
2935 | btf_verifier_log_type(env, t, "Invalid elem"); |
2936 | return -EINVAL; | |
2937 | } | |
2938 | ||
aea2f7b8 | 2939 | if (!array->index_type || !BTF_TYPE_ID_VALID(array->index_type)) { |
4ef5f574 | 2940 | btf_verifier_log_type(env, t, "Invalid index"); |
69b693f0 MKL |
2941 | return -EINVAL; |
2942 | } | |
2943 | ||
2944 | btf_verifier_log_type(env, t, NULL); | |
2945 | ||
2946 | return meta_needed; | |
2947 | } | |
2948 | ||
eb3f595d MKL |
2949 | static int btf_array_resolve(struct btf_verifier_env *env, |
2950 | const struct resolve_vertex *v) | |
2951 | { | |
2952 | const struct btf_array *array = btf_type_array(v->t); | |
4ef5f574 MKL |
2953 | const struct btf_type *elem_type, *index_type; |
2954 | u32 elem_type_id, index_type_id; | |
eb3f595d MKL |
2955 | struct btf *btf = env->btf; |
2956 | u32 elem_size; | |
2957 | ||
4ef5f574 MKL |
2958 | /* Check array->index_type */ |
2959 | index_type_id = array->index_type; | |
2960 | index_type = btf_type_by_id(btf, index_type_id); | |
e4f07120 SF |
2961 | if (btf_type_nosize_or_null(index_type) || |
2962 | btf_type_is_resolve_source_only(index_type)) { | |
4ef5f574 MKL |
2963 | btf_verifier_log_type(env, v->t, "Invalid index"); |
2964 | return -EINVAL; | |
2965 | } | |
2966 | ||
2967 | if (!env_type_is_resolve_sink(env, index_type) && | |
2968 | !env_type_is_resolved(env, index_type_id)) | |
2969 | return env_stack_push(env, index_type, index_type_id); | |
2970 | ||
2971 | index_type = btf_type_id_size(btf, &index_type_id, NULL); | |
2972 | if (!index_type || !btf_type_is_int(index_type) || | |
2973 | !btf_type_int_is_regular(index_type)) { | |
2974 | btf_verifier_log_type(env, v->t, "Invalid index"); | |
2975 | return -EINVAL; | |
2976 | } | |
2977 | ||
2978 | /* Check array->type */ | |
2979 | elem_type_id = array->type; | |
eb3f595d | 2980 | elem_type = btf_type_by_id(btf, elem_type_id); |
e4f07120 SF |
2981 | if (btf_type_nosize_or_null(elem_type) || |
2982 | btf_type_is_resolve_source_only(elem_type)) { | |
eb3f595d MKL |
2983 | btf_verifier_log_type(env, v->t, |
2984 | "Invalid elem"); | |
2985 | return -EINVAL; | |
2986 | } | |
2987 | ||
2988 | if (!env_type_is_resolve_sink(env, elem_type) && | |
2989 | !env_type_is_resolved(env, elem_type_id)) | |
2990 | return env_stack_push(env, elem_type, elem_type_id); | |
2991 | ||
2992 | elem_type = btf_type_id_size(btf, &elem_type_id, &elem_size); | |
2993 | if (!elem_type) { | |
2994 | btf_verifier_log_type(env, v->t, "Invalid elem"); | |
2995 | return -EINVAL; | |
2996 | } | |
2997 | ||
4ef5f574 MKL |
2998 | if (btf_type_is_int(elem_type) && !btf_type_int_is_regular(elem_type)) { |
2999 | btf_verifier_log_type(env, v->t, "Invalid array of int"); | |
3000 | return -EINVAL; | |
eb3f595d MKL |
3001 | } |
3002 | ||
3003 | if (array->nelems && elem_size > U32_MAX / array->nelems) { | |
3004 | btf_verifier_log_type(env, v->t, | |
3005 | "Array size overflows U32_MAX"); | |
3006 | return -EINVAL; | |
3007 | } | |
3008 | ||
3009 | env_stack_pop_resolved(env, elem_type_id, elem_size * array->nelems); | |
3010 | ||
3011 | return 0; | |
3012 | } | |
3013 | ||
69b693f0 MKL |
3014 | static void btf_array_log(struct btf_verifier_env *env, |
3015 | const struct btf_type *t) | |
3016 | { | |
3017 | const struct btf_array *array = btf_type_array(t); | |
3018 | ||
3019 | btf_verifier_log(env, "type_id=%u index_type_id=%u nr_elems=%u", | |
3020 | array->type, array->index_type, array->nelems); | |
3021 | } | |
3022 | ||
31d0bc81 AM |
3023 | static void __btf_array_show(const struct btf *btf, const struct btf_type *t, |
3024 | u32 type_id, void *data, u8 bits_offset, | |
3025 | struct btf_show *show) | |
b00b8dae MKL |
3026 | { |
3027 | const struct btf_array *array = btf_type_array(t); | |
3028 | const struct btf_kind_operations *elem_ops; | |
3029 | const struct btf_type *elem_type; | |
31d0bc81 AM |
3030 | u32 i, elem_size = 0, elem_type_id; |
3031 | u16 encoding = 0; | |
b00b8dae MKL |
3032 | |
3033 | elem_type_id = array->type; | |
31d0bc81 AM |
3034 | elem_type = btf_type_skip_modifiers(btf, elem_type_id, NULL); |
3035 | if (elem_type && btf_type_has_size(elem_type)) | |
3036 | elem_size = elem_type->size; | |
3037 | ||
3038 | if (elem_type && btf_type_is_int(elem_type)) { | |
3039 | u32 int_type = btf_type_int(elem_type); | |
3040 | ||
3041 | encoding = BTF_INT_ENCODING(int_type); | |
3042 | ||
3043 | /* | |
3044 | * BTF_INT_CHAR encoding never seems to be set for | |
3045 | * char arrays, so if size is 1 and element is | |
3046 | * printable as a char, we'll do that. | |
3047 | */ | |
3048 | if (elem_size == 1) | |
3049 | encoding = BTF_INT_CHAR; | |
3050 | } | |
3051 | ||
3052 | if (!btf_show_start_array_type(show, t, type_id, encoding, data)) | |
3053 | return; | |
3054 | ||
3055 | if (!elem_type) | |
3056 | goto out; | |
b00b8dae | 3057 | elem_ops = btf_type_ops(elem_type); |
31d0bc81 | 3058 | |
b00b8dae | 3059 | for (i = 0; i < array->nelems; i++) { |
b00b8dae | 3060 | |
31d0bc81 AM |
3061 | btf_show_start_array_member(show); |
3062 | ||
3063 | elem_ops->show(btf, elem_type, elem_type_id, data, | |
3064 | bits_offset, show); | |
b00b8dae | 3065 | data += elem_size; |
31d0bc81 AM |
3066 | |
3067 | btf_show_end_array_member(show); | |
3068 | ||
3069 | if (show->state.array_terminated) | |
3070 | break; | |
b00b8dae | 3071 | } |
31d0bc81 AM |
3072 | out: |
3073 | btf_show_end_array_type(show); | |
3074 | } | |
3075 | ||
3076 | static void btf_array_show(const struct btf *btf, const struct btf_type *t, | |
3077 | u32 type_id, void *data, u8 bits_offset, | |
3078 | struct btf_show *show) | |
3079 | { | |
3080 | const struct btf_member *m = show->state.member; | |
3081 | ||
3082 | /* | |
3083 | * First check if any members would be shown (are non-zero). | |
3084 | * See comments above "struct btf_show" definition for more | |
3085 | * details on how this works at a high-level. | |
3086 | */ | |
3087 | if (show->state.depth > 0 && !(show->flags & BTF_SHOW_ZERO)) { | |
3088 | if (!show->state.depth_check) { | |
3089 | show->state.depth_check = show->state.depth + 1; | |
3090 | show->state.depth_to_show = 0; | |
3091 | } | |
3092 | __btf_array_show(btf, t, type_id, data, bits_offset, show); | |
3093 | show->state.member = m; | |
3094 | ||
3095 | if (show->state.depth_check != show->state.depth + 1) | |
3096 | return; | |
3097 | show->state.depth_check = 0; | |
3098 | ||
3099 | if (show->state.depth_to_show <= show->state.depth) | |
3100 | return; | |
3101 | /* | |
3102 | * Reaching here indicates we have recursed and found | |
3103 | * non-zero array member(s). | |
3104 | */ | |
3105 | } | |
3106 | __btf_array_show(btf, t, type_id, data, bits_offset, show); | |
b00b8dae MKL |
3107 | } |
3108 | ||
7bae563c | 3109 | static const struct btf_kind_operations array_ops = { |
69b693f0 | 3110 | .check_meta = btf_array_check_meta, |
eb3f595d | 3111 | .resolve = btf_array_resolve, |
179cde8c | 3112 | .check_member = btf_array_check_member, |
9d5f9f70 | 3113 | .check_kflag_member = btf_generic_check_kflag_member, |
69b693f0 | 3114 | .log_details = btf_array_log, |
31d0bc81 | 3115 | .show = btf_array_show, |
69b693f0 MKL |
3116 | }; |
3117 | ||
179cde8c MKL |
3118 | static int btf_struct_check_member(struct btf_verifier_env *env, |
3119 | const struct btf_type *struct_type, | |
3120 | const struct btf_member *member, | |
3121 | const struct btf_type *member_type) | |
3122 | { | |
3123 | u32 struct_bits_off = member->offset; | |
3124 | u32 struct_size, bytes_offset; | |
3125 | ||
3126 | if (BITS_PER_BYTE_MASKED(struct_bits_off)) { | |
3127 | btf_verifier_log_member(env, struct_type, member, | |
3128 | "Member is not byte aligned"); | |
3129 | return -EINVAL; | |
3130 | } | |
3131 | ||
3132 | struct_size = struct_type->size; | |
3133 | bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off); | |
3134 | if (struct_size - bytes_offset < member_type->size) { | |
3135 | btf_verifier_log_member(env, struct_type, member, | |
3136 | "Member exceeds struct_size"); | |
3137 | return -EINVAL; | |
3138 | } | |
3139 | ||
3140 | return 0; | |
3141 | } | |
3142 | ||
69b693f0 MKL |
3143 | static s32 btf_struct_check_meta(struct btf_verifier_env *env, |
3144 | const struct btf_type *t, | |
3145 | u32 meta_left) | |
3146 | { | |
3147 | bool is_union = BTF_INFO_KIND(t->info) == BTF_KIND_UNION; | |
3148 | const struct btf_member *member; | |
6283fa38 | 3149 | u32 meta_needed, last_offset; |
69b693f0 MKL |
3150 | struct btf *btf = env->btf; |
3151 | u32 struct_size = t->size; | |
9d5f9f70 | 3152 | u32 offset; |
69b693f0 MKL |
3153 | u16 i; |
3154 | ||
3155 | meta_needed = btf_type_vlen(t) * sizeof(*member); | |
3156 | if (meta_left < meta_needed) { | |
3157 | btf_verifier_log_basic(env, t, | |
3158 | "meta_left:%u meta_needed:%u", | |
3159 | meta_left, meta_needed); | |
3160 | return -EINVAL; | |
3161 | } | |
3162 | ||
eb04bbb6 YS |
3163 | /* struct type either no name or a valid one */ |
3164 | if (t->name_off && | |
3165 | !btf_name_valid_identifier(env->btf, t->name_off)) { | |
3166 | btf_verifier_log_type(env, t, "Invalid name"); | |
3167 | return -EINVAL; | |
3168 | } | |
3169 | ||
69b693f0 MKL |
3170 | btf_verifier_log_type(env, t, NULL); |
3171 | ||
6283fa38 | 3172 | last_offset = 0; |
69b693f0 | 3173 | for_each_member(i, t, member) { |
fbcf93eb | 3174 | if (!btf_name_offset_valid(btf, member->name_off)) { |
69b693f0 MKL |
3175 | btf_verifier_log_member(env, t, member, |
3176 | "Invalid member name_offset:%u", | |
fbcf93eb | 3177 | member->name_off); |
69b693f0 MKL |
3178 | return -EINVAL; |
3179 | } | |
3180 | ||
eb04bbb6 YS |
3181 | /* struct member either no name or a valid one */ |
3182 | if (member->name_off && | |
3183 | !btf_name_valid_identifier(btf, member->name_off)) { | |
3184 | btf_verifier_log_member(env, t, member, "Invalid name"); | |
3185 | return -EINVAL; | |
3186 | } | |
69b693f0 | 3187 | /* A member cannot be in type void */ |
aea2f7b8 | 3188 | if (!member->type || !BTF_TYPE_ID_VALID(member->type)) { |
69b693f0 MKL |
3189 | btf_verifier_log_member(env, t, member, |
3190 | "Invalid type_id"); | |
3191 | return -EINVAL; | |
3192 | } | |
3193 | ||
8293eb99 | 3194 | offset = __btf_member_bit_offset(t, member); |
9d5f9f70 | 3195 | if (is_union && offset) { |
69b693f0 MKL |
3196 | btf_verifier_log_member(env, t, member, |
3197 | "Invalid member bits_offset"); | |
3198 | return -EINVAL; | |
3199 | } | |
3200 | ||
6283fa38 MKL |
3201 | /* |
3202 | * ">" instead of ">=" because the last member could be | |
3203 | * "char a[0];" | |
3204 | */ | |
9d5f9f70 | 3205 | if (last_offset > offset) { |
6283fa38 MKL |
3206 | btf_verifier_log_member(env, t, member, |
3207 | "Invalid member bits_offset"); | |
3208 | return -EINVAL; | |
3209 | } | |
3210 | ||
9d5f9f70 | 3211 | if (BITS_ROUNDUP_BYTES(offset) > struct_size) { |
69b693f0 | 3212 | btf_verifier_log_member(env, t, member, |
311fe1a8 | 3213 | "Member bits_offset exceeds its struct size"); |
69b693f0 MKL |
3214 | return -EINVAL; |
3215 | } | |
3216 | ||
3217 | btf_verifier_log_member(env, t, member, NULL); | |
9d5f9f70 | 3218 | last_offset = offset; |
69b693f0 MKL |
3219 | } |
3220 | ||
3221 | return meta_needed; | |
3222 | } | |
3223 | ||
eb3f595d MKL |
3224 | static int btf_struct_resolve(struct btf_verifier_env *env, |
3225 | const struct resolve_vertex *v) | |
3226 | { | |
3227 | const struct btf_member *member; | |
179cde8c | 3228 | int err; |
eb3f595d MKL |
3229 | u16 i; |
3230 | ||
3231 | /* Before continue resolving the next_member, | |
3232 | * ensure the last member is indeed resolved to a | |
3233 | * type with size info. | |
3234 | */ | |
3235 | if (v->next_member) { | |
179cde8c | 3236 | const struct btf_type *last_member_type; |
eb3f595d | 3237 | const struct btf_member *last_member; |
a37a3258 | 3238 | u32 last_member_type_id; |
eb3f595d MKL |
3239 | |
3240 | last_member = btf_type_member(v->t) + v->next_member - 1; | |
3241 | last_member_type_id = last_member->type; | |
3242 | if (WARN_ON_ONCE(!env_type_is_resolved(env, | |
3243 | last_member_type_id))) | |
3244 | return -EINVAL; | |
179cde8c MKL |
3245 | |
3246 | last_member_type = btf_type_by_id(env->btf, | |
3247 | last_member_type_id); | |
9d5f9f70 YS |
3248 | if (btf_type_kflag(v->t)) |
3249 | err = btf_type_ops(last_member_type)->check_kflag_member(env, v->t, | |
3250 | last_member, | |
3251 | last_member_type); | |
3252 | else | |
3253 | err = btf_type_ops(last_member_type)->check_member(env, v->t, | |
3254 | last_member, | |
3255 | last_member_type); | |
179cde8c MKL |
3256 | if (err) |
3257 | return err; | |
eb3f595d MKL |
3258 | } |
3259 | ||
3260 | for_each_member_from(i, v->next_member, v->t, member) { | |
3261 | u32 member_type_id = member->type; | |
3262 | const struct btf_type *member_type = btf_type_by_id(env->btf, | |
3263 | member_type_id); | |
3264 | ||
e4f07120 SF |
3265 | if (btf_type_nosize_or_null(member_type) || |
3266 | btf_type_is_resolve_source_only(member_type)) { | |
eb3f595d MKL |
3267 | btf_verifier_log_member(env, v->t, member, |
3268 | "Invalid member"); | |
3269 | return -EINVAL; | |
3270 | } | |
3271 | ||
3272 | if (!env_type_is_resolve_sink(env, member_type) && | |
3273 | !env_type_is_resolved(env, member_type_id)) { | |
3274 | env_stack_set_next_member(env, i + 1); | |
3275 | return env_stack_push(env, member_type, member_type_id); | |
3276 | } | |
179cde8c | 3277 | |
9d5f9f70 YS |
3278 | if (btf_type_kflag(v->t)) |
3279 | err = btf_type_ops(member_type)->check_kflag_member(env, v->t, | |
3280 | member, | |
3281 | member_type); | |
3282 | else | |
3283 | err = btf_type_ops(member_type)->check_member(env, v->t, | |
3284 | member, | |
3285 | member_type); | |
179cde8c MKL |
3286 | if (err) |
3287 | return err; | |
eb3f595d MKL |
3288 | } |
3289 | ||
3290 | env_stack_pop_resolved(env, 0, 0); | |
3291 | ||
3292 | return 0; | |
3293 | } | |
3294 | ||
69b693f0 MKL |
3295 | static void btf_struct_log(struct btf_verifier_env *env, |
3296 | const struct btf_type *t) | |
3297 | { | |
3298 | btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t)); | |
3299 | } | |
3300 | ||
61df10c7 KKD |
3301 | enum { |
3302 | BTF_FIELD_IGNORE = 0, | |
3303 | BTF_FIELD_FOUND = 1, | |
42ba1308 KKD |
3304 | }; |
3305 | ||
3306 | struct btf_field_info { | |
aa3496ac | 3307 | enum btf_field_type type; |
42ba1308 | 3308 | u32 off; |
f0c5941f KKD |
3309 | union { |
3310 | struct { | |
3311 | u32 type_id; | |
3312 | } kptr; | |
3313 | struct { | |
3314 | const char *node_name; | |
3315 | u32 value_btf_id; | |
30465003 | 3316 | } graph_root; |
f0c5941f | 3317 | }; |
42ba1308 KKD |
3318 | }; |
3319 | ||
3320 | static int btf_find_struct(const struct btf *btf, const struct btf_type *t, | |
db559117 KKD |
3321 | u32 off, int sz, enum btf_field_type field_type, |
3322 | struct btf_field_info *info) | |
42ba1308 KKD |
3323 | { |
3324 | if (!__btf_type_is_struct(t)) | |
61df10c7 | 3325 | return BTF_FIELD_IGNORE; |
42ba1308 | 3326 | if (t->size != sz) |
61df10c7 | 3327 | return BTF_FIELD_IGNORE; |
db559117 | 3328 | info->type = field_type; |
42ba1308 | 3329 | info->off = off; |
61df10c7 KKD |
3330 | return BTF_FIELD_FOUND; |
3331 | } | |
3332 | ||
3333 | static int btf_find_kptr(const struct btf *btf, const struct btf_type *t, | |
1cb80d9e | 3334 | u32 off, int sz, struct btf_field_info *info, u32 field_mask) |
61df10c7 | 3335 | { |
aa3496ac | 3336 | enum btf_field_type type; |
53ee0d66 IS |
3337 | const char *tag_value; |
3338 | bool is_type_tag; | |
61df10c7 KKD |
3339 | u32 res_id; |
3340 | ||
23da464d KKD |
3341 | /* Permit modifiers on the pointer itself */ |
3342 | if (btf_type_is_volatile(t)) | |
3343 | t = btf_type_by_id(btf, t->type); | |
61df10c7 KKD |
3344 | /* For PTR, sz is always == 8 */ |
3345 | if (!btf_type_is_ptr(t)) | |
3346 | return BTF_FIELD_IGNORE; | |
3347 | t = btf_type_by_id(btf, t->type); | |
53ee0d66 IS |
3348 | is_type_tag = btf_type_is_type_tag(t) && !btf_type_kflag(t); |
3349 | if (!is_type_tag) | |
61df10c7 KKD |
3350 | return BTF_FIELD_IGNORE; |
3351 | /* Reject extra tags */ | |
3352 | if (btf_type_is_type_tag(btf_type_by_id(btf, t->type))) | |
3353 | return -EINVAL; | |
53ee0d66 IS |
3354 | tag_value = __btf_name_by_offset(btf, t->name_off); |
3355 | if (!strcmp("kptr_untrusted", tag_value)) | |
c0a5a21c | 3356 | type = BPF_KPTR_UNREF; |
53ee0d66 | 3357 | else if (!strcmp("kptr", tag_value)) |
c0a5a21c | 3358 | type = BPF_KPTR_REF; |
53ee0d66 | 3359 | else if (!strcmp("percpu_kptr", tag_value)) |
55db92f4 | 3360 | type = BPF_KPTR_PERCPU; |
53ee0d66 | 3361 | else if (!strcmp("uptr", tag_value)) |
1cb80d9e | 3362 | type = BPF_UPTR; |
c0a5a21c | 3363 | else |
61df10c7 KKD |
3364 | return -EINVAL; |
3365 | ||
1cb80d9e KFL |
3366 | if (!(type & field_mask)) |
3367 | return BTF_FIELD_IGNORE; | |
3368 | ||
61df10c7 KKD |
3369 | /* Get the base type */ |
3370 | t = btf_type_skip_modifiers(btf, t->type, &res_id); | |
3371 | /* Only pointer to struct is allowed */ | |
3372 | if (!__btf_type_is_struct(t)) | |
3373 | return -EINVAL; | |
3374 | ||
c0a5a21c | 3375 | info->type = type; |
db559117 KKD |
3376 | info->off = off; |
3377 | info->kptr.type_id = res_id; | |
61df10c7 | 3378 | return BTF_FIELD_FOUND; |
42ba1308 KKD |
3379 | } |
3380 | ||
522bb2c1 AN |
3381 | int btf_find_next_decl_tag(const struct btf *btf, const struct btf_type *pt, |
3382 | int comp_idx, const char *tag_key, int last_id) | |
f0c5941f | 3383 | { |
522bb2c1 AN |
3384 | int len = strlen(tag_key); |
3385 | int i, n; | |
f0c5941f | 3386 | |
522bb2c1 | 3387 | for (i = last_id + 1, n = btf_nr_types(btf); i < n; i++) { |
f0c5941f | 3388 | const struct btf_type *t = btf_type_by_id(btf, i); |
f0c5941f KKD |
3389 | |
3390 | if (!btf_type_is_decl_tag(t)) | |
3391 | continue; | |
522bb2c1 AN |
3392 | if (pt != btf_type_by_id(btf, t->type)) |
3393 | continue; | |
3394 | if (btf_type_decl_tag(t)->component_idx != comp_idx) | |
f0c5941f KKD |
3395 | continue; |
3396 | if (strncmp(__btf_name_by_offset(btf, t->name_off), tag_key, len)) | |
3397 | continue; | |
522bb2c1 | 3398 | return i; |
f0c5941f | 3399 | } |
522bb2c1 AN |
3400 | return -ENOENT; |
3401 | } | |
3402 | ||
3403 | const char *btf_find_decl_tag_value(const struct btf *btf, const struct btf_type *pt, | |
3404 | int comp_idx, const char *tag_key) | |
3405 | { | |
3406 | const char *value = NULL; | |
3407 | const struct btf_type *t; | |
3408 | int len, id; | |
3409 | ||
3410 | id = btf_find_next_decl_tag(btf, pt, comp_idx, tag_key, 0); | |
3411 | if (id < 0) | |
3412 | return ERR_PTR(id); | |
3413 | ||
3414 | t = btf_type_by_id(btf, id); | |
3415 | len = strlen(tag_key); | |
3416 | value = __btf_name_by_offset(btf, t->name_off) + len; | |
3417 | ||
3418 | /* Prevent duplicate entries for same type */ | |
3419 | id = btf_find_next_decl_tag(btf, pt, comp_idx, tag_key, id); | |
3420 | if (id >= 0) | |
3421 | return ERR_PTR(-EEXIST); | |
3422 | ||
b9ae0c9d | 3423 | return value; |
f0c5941f KKD |
3424 | } |
3425 | ||
9c395c1b DM |
3426 | static int |
3427 | btf_find_graph_root(const struct btf *btf, const struct btf_type *pt, | |
3428 | const struct btf_type *t, int comp_idx, u32 off, | |
3429 | int sz, struct btf_field_info *info, | |
3430 | enum btf_field_type head_type) | |
f0c5941f | 3431 | { |
9c395c1b | 3432 | const char *node_field_name; |
f0c5941f | 3433 | const char *value_type; |
f0c5941f KKD |
3434 | s32 id; |
3435 | ||
3436 | if (!__btf_type_is_struct(t)) | |
3437 | return BTF_FIELD_IGNORE; | |
3438 | if (t->size != sz) | |
3439 | return BTF_FIELD_IGNORE; | |
3440 | value_type = btf_find_decl_tag_value(btf, pt, comp_idx, "contains:"); | |
b9ae0c9d | 3441 | if (IS_ERR(value_type)) |
f0c5941f | 3442 | return -EINVAL; |
9c395c1b DM |
3443 | node_field_name = strstr(value_type, ":"); |
3444 | if (!node_field_name) | |
f0c5941f | 3445 | return -EINVAL; |
9c395c1b | 3446 | value_type = kstrndup(value_type, node_field_name - value_type, GFP_KERNEL | __GFP_NOWARN); |
f0c5941f KKD |
3447 | if (!value_type) |
3448 | return -ENOMEM; | |
3449 | id = btf_find_by_name_kind(btf, value_type, BTF_KIND_STRUCT); | |
3450 | kfree(value_type); | |
3451 | if (id < 0) | |
3452 | return id; | |
9c395c1b DM |
3453 | node_field_name++; |
3454 | if (str_is_empty(node_field_name)) | |
f0c5941f | 3455 | return -EINVAL; |
9c395c1b | 3456 | info->type = head_type; |
f0c5941f | 3457 | info->off = off; |
30465003 | 3458 | info->graph_root.value_btf_id = id; |
9c395c1b | 3459 | info->graph_root.node_name = node_field_name; |
f0c5941f KKD |
3460 | return BTF_FIELD_FOUND; |
3461 | } | |
3462 | ||
9c395c1b DM |
3463 | #define field_mask_test_name(field_type, field_type_str) \ |
3464 | if (field_mask & field_type && !strcmp(name, field_type_str)) { \ | |
3465 | type = field_type; \ | |
3466 | goto end; \ | |
3467 | } | |
3468 | ||
64e8ee81 KFL |
3469 | static int btf_get_field_type(const struct btf *btf, const struct btf_type *var_type, |
3470 | u32 field_mask, u32 *seen_mask, | |
db559117 KKD |
3471 | int *align, int *sz) |
3472 | { | |
3473 | int type = 0; | |
64e8ee81 | 3474 | const char *name = __btf_name_by_offset(btf, var_type->name_off); |
db559117 KKD |
3475 | |
3476 | if (field_mask & BPF_SPIN_LOCK) { | |
3477 | if (!strcmp(name, "bpf_spin_lock")) { | |
3478 | if (*seen_mask & BPF_SPIN_LOCK) | |
3479 | return -E2BIG; | |
3480 | *seen_mask |= BPF_SPIN_LOCK; | |
3481 | type = BPF_SPIN_LOCK; | |
3482 | goto end; | |
3483 | } | |
3484 | } | |
0de20461 KKD |
3485 | if (field_mask & BPF_RES_SPIN_LOCK) { |
3486 | if (!strcmp(name, "bpf_res_spin_lock")) { | |
3487 | if (*seen_mask & BPF_RES_SPIN_LOCK) | |
3488 | return -E2BIG; | |
3489 | *seen_mask |= BPF_RES_SPIN_LOCK; | |
3490 | type = BPF_RES_SPIN_LOCK; | |
3491 | goto end; | |
3492 | } | |
3493 | } | |
db559117 KKD |
3494 | if (field_mask & BPF_TIMER) { |
3495 | if (!strcmp(name, "bpf_timer")) { | |
3496 | if (*seen_mask & BPF_TIMER) | |
3497 | return -E2BIG; | |
3498 | *seen_mask |= BPF_TIMER; | |
3499 | type = BPF_TIMER; | |
3500 | goto end; | |
3501 | } | |
3502 | } | |
d56b63cf BT |
3503 | if (field_mask & BPF_WORKQUEUE) { |
3504 | if (!strcmp(name, "bpf_wq")) { | |
3505 | if (*seen_mask & BPF_WORKQUEUE) | |
3506 | return -E2BIG; | |
3507 | *seen_mask |= BPF_WORKQUEUE; | |
3508 | type = BPF_WORKQUEUE; | |
3509 | goto end; | |
3510 | } | |
3511 | } | |
9c395c1b DM |
3512 | field_mask_test_name(BPF_LIST_HEAD, "bpf_list_head"); |
3513 | field_mask_test_name(BPF_LIST_NODE, "bpf_list_node"); | |
3514 | field_mask_test_name(BPF_RB_ROOT, "bpf_rb_root"); | |
3515 | field_mask_test_name(BPF_RB_NODE, "bpf_rb_node"); | |
d54730b5 | 3516 | field_mask_test_name(BPF_REFCOUNT, "bpf_refcount"); |
9c395c1b | 3517 | |
db559117 | 3518 | /* Only return BPF_KPTR when all other types with matchable names fail */ |
1cb80d9e | 3519 | if (field_mask & (BPF_KPTR | BPF_UPTR) && !__btf_type_is_struct(var_type)) { |
db559117 KKD |
3520 | type = BPF_KPTR_REF; |
3521 | goto end; | |
3522 | } | |
3523 | return 0; | |
3524 | end: | |
3525 | *sz = btf_field_type_size(type); | |
3526 | *align = btf_field_type_align(type); | |
3527 | return type; | |
3528 | } | |
3529 | ||
9c395c1b DM |
3530 | #undef field_mask_test_name |
3531 | ||
64e8ee81 | 3532 | /* Repeat a number of fields for a specified number of times. |
994796c0 | 3533 | * |
64e8ee81 KFL |
3534 | * Copy the fields starting from the first field and repeat them for |
3535 | * repeat_cnt times. The fields are repeated by adding the offset of each | |
3536 | * field with | |
994796c0 KFL |
3537 | * (i + 1) * elem_size |
3538 | * where i is the repeat index and elem_size is the size of an element. | |
3539 | */ | |
797d73ee | 3540 | static int btf_repeat_fields(struct btf_field_info *info, int info_cnt, |
64e8ee81 | 3541 | u32 field_cnt, u32 repeat_cnt, u32 elem_size) |
994796c0 | 3542 | { |
64e8ee81 | 3543 | u32 i, j; |
994796c0 KFL |
3544 | u32 cur; |
3545 | ||
3546 | /* Ensure not repeating fields that should not be repeated. */ | |
64e8ee81 KFL |
3547 | for (i = 0; i < field_cnt; i++) { |
3548 | switch (info[i].type) { | |
3549 | case BPF_KPTR_UNREF: | |
3550 | case BPF_KPTR_REF: | |
3551 | case BPF_KPTR_PERCPU: | |
1cb80d9e | 3552 | case BPF_UPTR: |
64e8ee81 KFL |
3553 | case BPF_LIST_HEAD: |
3554 | case BPF_RB_ROOT: | |
3555 | break; | |
3556 | default: | |
3557 | return -EINVAL; | |
3558 | } | |
994796c0 KFL |
3559 | } |
3560 | ||
797d73ee HT |
3561 | /* The type of struct size or variable size is u32, |
3562 | * so the multiplication will not overflow. | |
3563 | */ | |
3564 | if (field_cnt * (repeat_cnt + 1) > info_cnt) | |
3565 | return -E2BIG; | |
3566 | ||
64e8ee81 | 3567 | cur = field_cnt; |
994796c0 | 3568 | for (i = 0; i < repeat_cnt; i++) { |
64e8ee81 KFL |
3569 | memcpy(&info[cur], &info[0], field_cnt * sizeof(info[0])); |
3570 | for (j = 0; j < field_cnt; j++) | |
3571 | info[cur++].off += (i + 1) * elem_size; | |
994796c0 KFL |
3572 | } |
3573 | ||
3574 | return 0; | |
3575 | } | |
3576 | ||
64e8ee81 KFL |
3577 | static int btf_find_struct_field(const struct btf *btf, |
3578 | const struct btf_type *t, u32 field_mask, | |
f19caf57 KFL |
3579 | struct btf_field_info *info, int info_cnt, |
3580 | u32 level); | |
64e8ee81 KFL |
3581 | |
3582 | /* Find special fields in the struct type of a field. | |
3583 | * | |
3584 | * This function is used to find fields of special types that is not a | |
3585 | * global variable or a direct field of a struct type. It also handles the | |
3586 | * repetition if it is the element type of an array. | |
3587 | */ | |
3588 | static int btf_find_nested_struct(const struct btf *btf, const struct btf_type *t, | |
3589 | u32 off, u32 nelems, | |
3590 | u32 field_mask, struct btf_field_info *info, | |
f19caf57 | 3591 | int info_cnt, u32 level) |
64e8ee81 KFL |
3592 | { |
3593 | int ret, err, i; | |
3594 | ||
f19caf57 KFL |
3595 | level++; |
3596 | if (level >= MAX_RESOLVE_DEPTH) | |
3597 | return -E2BIG; | |
3598 | ||
3599 | ret = btf_find_struct_field(btf, t, field_mask, info, info_cnt, level); | |
64e8ee81 KFL |
3600 | |
3601 | if (ret <= 0) | |
3602 | return ret; | |
3603 | ||
3604 | /* Shift the offsets of the nested struct fields to the offsets | |
3605 | * related to the container. | |
3606 | */ | |
3607 | for (i = 0; i < ret; i++) | |
3608 | info[i].off += off; | |
3609 | ||
3610 | if (nelems > 1) { | |
797d73ee | 3611 | err = btf_repeat_fields(info, info_cnt, ret, nelems - 1, t->size); |
64e8ee81 KFL |
3612 | if (err == 0) |
3613 | ret *= nelems; | |
3614 | else | |
3615 | ret = err; | |
3616 | } | |
3617 | ||
3618 | return ret; | |
3619 | } | |
3620 | ||
a7db0d4f KFL |
3621 | static int btf_find_field_one(const struct btf *btf, |
3622 | const struct btf_type *var, | |
3623 | const struct btf_type *var_type, | |
3624 | int var_idx, | |
3625 | u32 off, u32 expected_size, | |
3626 | u32 field_mask, u32 *seen_mask, | |
f19caf57 KFL |
3627 | struct btf_field_info *info, int info_cnt, |
3628 | u32 level) | |
a7db0d4f KFL |
3629 | { |
3630 | int ret, align, sz, field_type; | |
3631 | struct btf_field_info tmp; | |
994796c0 KFL |
3632 | const struct btf_array *array; |
3633 | u32 i, nelems = 1; | |
3634 | ||
3635 | /* Walk into array types to find the element type and the number of | |
3636 | * elements in the (flattened) array. | |
3637 | */ | |
3638 | for (i = 0; i < MAX_RESOLVE_DEPTH && btf_type_is_array(var_type); i++) { | |
3639 | array = btf_array(var_type); | |
3640 | nelems *= array->nelems; | |
3641 | var_type = btf_type_by_id(btf, array->type); | |
3642 | } | |
3643 | if (i == MAX_RESOLVE_DEPTH) | |
3644 | return -E2BIG; | |
3645 | if (nelems == 0) | |
3646 | return 0; | |
a7db0d4f | 3647 | |
64e8ee81 | 3648 | field_type = btf_get_field_type(btf, var_type, |
a7db0d4f | 3649 | field_mask, seen_mask, &align, &sz); |
64e8ee81 KFL |
3650 | /* Look into variables of struct types */ |
3651 | if (!field_type && __btf_type_is_struct(var_type)) { | |
3652 | sz = var_type->size; | |
3653 | if (expected_size && expected_size != sz * nelems) | |
3654 | return 0; | |
3655 | ret = btf_find_nested_struct(btf, var_type, off, nelems, field_mask, | |
f19caf57 | 3656 | &info[0], info_cnt, level); |
64e8ee81 KFL |
3657 | return ret; |
3658 | } | |
3659 | ||
a7db0d4f KFL |
3660 | if (field_type == 0) |
3661 | return 0; | |
3662 | if (field_type < 0) | |
3663 | return field_type; | |
3664 | ||
994796c0 | 3665 | if (expected_size && expected_size != sz * nelems) |
a7db0d4f KFL |
3666 | return 0; |
3667 | if (off % align) | |
3668 | return 0; | |
3669 | ||
3670 | switch (field_type) { | |
3671 | case BPF_SPIN_LOCK: | |
0de20461 | 3672 | case BPF_RES_SPIN_LOCK: |
a7db0d4f KFL |
3673 | case BPF_TIMER: |
3674 | case BPF_WORKQUEUE: | |
3675 | case BPF_LIST_NODE: | |
3676 | case BPF_RB_NODE: | |
3677 | case BPF_REFCOUNT: | |
3678 | ret = btf_find_struct(btf, var_type, off, sz, field_type, | |
3679 | info_cnt ? &info[0] : &tmp); | |
3680 | if (ret < 0) | |
3681 | return ret; | |
3682 | break; | |
3683 | case BPF_KPTR_UNREF: | |
3684 | case BPF_KPTR_REF: | |
3685 | case BPF_KPTR_PERCPU: | |
1cb80d9e | 3686 | case BPF_UPTR: |
a7db0d4f | 3687 | ret = btf_find_kptr(btf, var_type, off, sz, |
1cb80d9e | 3688 | info_cnt ? &info[0] : &tmp, field_mask); |
a7db0d4f KFL |
3689 | if (ret < 0) |
3690 | return ret; | |
3691 | break; | |
3692 | case BPF_LIST_HEAD: | |
3693 | case BPF_RB_ROOT: | |
3694 | ret = btf_find_graph_root(btf, var, var_type, | |
3695 | var_idx, off, sz, | |
3696 | info_cnt ? &info[0] : &tmp, | |
3697 | field_type); | |
3698 | if (ret < 0) | |
3699 | return ret; | |
3700 | break; | |
3701 | default: | |
3702 | return -EFAULT; | |
3703 | } | |
3704 | ||
3705 | if (ret == BTF_FIELD_IGNORE) | |
3706 | return 0; | |
797d73ee | 3707 | if (!info_cnt) |
a7db0d4f | 3708 | return -E2BIG; |
994796c0 | 3709 | if (nelems > 1) { |
797d73ee | 3710 | ret = btf_repeat_fields(info, info_cnt, 1, nelems - 1, sz); |
994796c0 KFL |
3711 | if (ret < 0) |
3712 | return ret; | |
3713 | } | |
3714 | return nelems; | |
a7db0d4f KFL |
3715 | } |
3716 | ||
db559117 KKD |
3717 | static int btf_find_struct_field(const struct btf *btf, |
3718 | const struct btf_type *t, u32 field_mask, | |
f19caf57 KFL |
3719 | struct btf_field_info *info, int info_cnt, |
3720 | u32 level) | |
d83525ca | 3721 | { |
a7db0d4f | 3722 | int ret, idx = 0; |
d83525ca | 3723 | const struct btf_member *member; |
db559117 | 3724 | u32 i, off, seen_mask = 0; |
d83525ca | 3725 | |
d83525ca AS |
3726 | for_each_member(i, t, member) { |
3727 | const struct btf_type *member_type = btf_type_by_id(btf, | |
3728 | member->type); | |
42ba1308 | 3729 | |
8293eb99 | 3730 | off = __btf_member_bit_offset(t, member); |
d83525ca AS |
3731 | if (off % 8) |
3732 | /* valid C code cannot generate such BTF */ | |
3733 | return -EINVAL; | |
3734 | off /= 8; | |
42ba1308 | 3735 | |
a7db0d4f KFL |
3736 | ret = btf_find_field_one(btf, t, member_type, i, |
3737 | off, 0, | |
3738 | field_mask, &seen_mask, | |
f19caf57 | 3739 | &info[idx], info_cnt - idx, level); |
a7db0d4f KFL |
3740 | if (ret < 0) |
3741 | return ret; | |
3742 | idx += ret; | |
68134668 | 3743 | } |
61df10c7 | 3744 | return idx; |
68134668 AS |
3745 | } |
3746 | ||
3747 | static int btf_find_datasec_var(const struct btf *btf, const struct btf_type *t, | |
db559117 | 3748 | u32 field_mask, struct btf_field_info *info, |
f19caf57 | 3749 | int info_cnt, u32 level) |
68134668 | 3750 | { |
a7db0d4f | 3751 | int ret, idx = 0; |
68134668 | 3752 | const struct btf_var_secinfo *vsi; |
db559117 | 3753 | u32 i, off, seen_mask = 0; |
68134668 AS |
3754 | |
3755 | for_each_vsi(i, t, vsi) { | |
3756 | const struct btf_type *var = btf_type_by_id(btf, vsi->type); | |
3757 | const struct btf_type *var_type = btf_type_by_id(btf, var->type); | |
3758 | ||
db559117 | 3759 | off = vsi->offset; |
a7db0d4f KFL |
3760 | ret = btf_find_field_one(btf, var, var_type, -1, off, vsi->size, |
3761 | field_mask, &seen_mask, | |
f19caf57 KFL |
3762 | &info[idx], info_cnt - idx, |
3763 | level); | |
a7db0d4f KFL |
3764 | if (ret < 0) |
3765 | return ret; | |
3766 | idx += ret; | |
d83525ca | 3767 | } |
61df10c7 | 3768 | return idx; |
d83525ca AS |
3769 | } |
3770 | ||
68134668 | 3771 | static int btf_find_field(const struct btf *btf, const struct btf_type *t, |
db559117 KKD |
3772 | u32 field_mask, struct btf_field_info *info, |
3773 | int info_cnt) | |
68134668 | 3774 | { |
68134668 | 3775 | if (__btf_type_is_struct(t)) |
f19caf57 | 3776 | return btf_find_struct_field(btf, t, field_mask, info, info_cnt, 0); |
68134668 | 3777 | else if (btf_type_is_datasec(t)) |
f19caf57 | 3778 | return btf_find_datasec_var(btf, t, field_mask, info, info_cnt, 0); |
68134668 AS |
3779 | return -EINVAL; |
3780 | } | |
3781 | ||
c5ef5342 | 3782 | /* Callers have to ensure the life cycle of btf if it is program BTF */ |
db559117 KKD |
3783 | static int btf_parse_kptr(const struct btf *btf, struct btf_field *field, |
3784 | struct btf_field_info *info) | |
68134668 | 3785 | { |
db559117 KKD |
3786 | struct module *mod = NULL; |
3787 | const struct btf_type *t; | |
c8e18754 DM |
3788 | /* If a matching btf type is found in kernel or module BTFs, kptr_ref |
3789 | * is that BTF, otherwise it's program BTF | |
3790 | */ | |
3791 | struct btf *kptr_btf; | |
42ba1308 | 3792 | int ret; |
db559117 | 3793 | s32 id; |
42ba1308 | 3794 | |
db559117 KKD |
3795 | /* Find type in map BTF, and use it to look up the matching type |
3796 | * in vmlinux or module BTFs, by name and kind. | |
3797 | */ | |
3798 | t = btf_type_by_id(btf, info->kptr.type_id); | |
3799 | id = bpf_find_btf_id(__btf_name_by_offset(btf, t->name_off), BTF_INFO_KIND(t->info), | |
c8e18754 DM |
3800 | &kptr_btf); |
3801 | if (id == -ENOENT) { | |
3802 | /* btf_parse_kptr should only be called w/ btf = program BTF */ | |
3803 | WARN_ON_ONCE(btf_is_kernel(btf)); | |
3804 | ||
3805 | /* Type exists only in program BTF. Assume that it's a MEM_ALLOC | |
3806 | * kptr allocated via bpf_obj_new | |
3807 | */ | |
9e36a204 | 3808 | field->kptr.dtor = NULL; |
c8e18754 DM |
3809 | id = info->kptr.type_id; |
3810 | kptr_btf = (struct btf *)btf; | |
c8e18754 DM |
3811 | goto found_dtor; |
3812 | } | |
db559117 KKD |
3813 | if (id < 0) |
3814 | return id; | |
3815 | ||
3816 | /* Find and stash the function pointer for the destruction function that | |
3817 | * needs to be eventually invoked from the map free path. | |
3818 | */ | |
3819 | if (info->type == BPF_KPTR_REF) { | |
3820 | const struct btf_type *dtor_func; | |
3821 | const char *dtor_func_name; | |
3822 | unsigned long addr; | |
3823 | s32 dtor_btf_id; | |
3824 | ||
3825 | /* This call also serves as a whitelist of allowed objects that | |
3826 | * can be used as a referenced pointer and be stored in a map at | |
3827 | * the same time. | |
3828 | */ | |
c8e18754 | 3829 | dtor_btf_id = btf_find_dtor_kfunc(kptr_btf, id); |
db559117 KKD |
3830 | if (dtor_btf_id < 0) { |
3831 | ret = dtor_btf_id; | |
3832 | goto end_btf; | |
3833 | } | |
68134668 | 3834 | |
c8e18754 | 3835 | dtor_func = btf_type_by_id(kptr_btf, dtor_btf_id); |
db559117 KKD |
3836 | if (!dtor_func) { |
3837 | ret = -ENOENT; | |
3838 | goto end_btf; | |
3839 | } | |
42ba1308 | 3840 | |
c8e18754 DM |
3841 | if (btf_is_module(kptr_btf)) { |
3842 | mod = btf_try_get_module(kptr_btf); | |
db559117 KKD |
3843 | if (!mod) { |
3844 | ret = -ENXIO; | |
3845 | goto end_btf; | |
3846 | } | |
3847 | } | |
3848 | ||
3849 | /* We already verified dtor_func to be btf_type_is_func | |
3850 | * in register_btf_id_dtor_kfuncs. | |
3851 | */ | |
c8e18754 | 3852 | dtor_func_name = __btf_name_by_offset(kptr_btf, dtor_func->name_off); |
db559117 KKD |
3853 | addr = kallsyms_lookup_name(dtor_func_name); |
3854 | if (!addr) { | |
3855 | ret = -EINVAL; | |
3856 | goto end_mod; | |
3857 | } | |
3858 | field->kptr.dtor = (void *)addr; | |
3859 | } | |
3860 | ||
c8e18754 | 3861 | found_dtor: |
db559117 | 3862 | field->kptr.btf_id = id; |
c8e18754 | 3863 | field->kptr.btf = kptr_btf; |
db559117 KKD |
3864 | field->kptr.module = mod; |
3865 | return 0; | |
3866 | end_mod: | |
3867 | module_put(mod); | |
3868 | end_btf: | |
c8e18754 | 3869 | btf_put(kptr_btf); |
db559117 | 3870 | return ret; |
68134668 AS |
3871 | } |
3872 | ||
9c395c1b DM |
3873 | static int btf_parse_graph_root(const struct btf *btf, |
3874 | struct btf_field *field, | |
3875 | struct btf_field_info *info, | |
3876 | const char *node_type_name, | |
3877 | size_t node_type_align) | |
f0c5941f KKD |
3878 | { |
3879 | const struct btf_type *t, *n = NULL; | |
3880 | const struct btf_member *member; | |
3881 | u32 offset; | |
3882 | int i; | |
3883 | ||
30465003 | 3884 | t = btf_type_by_id(btf, info->graph_root.value_btf_id); |
f0c5941f KKD |
3885 | /* We've already checked that value_btf_id is a struct type. We |
3886 | * just need to figure out the offset of the list_node, and | |
3887 | * verify its type. | |
3888 | */ | |
3889 | for_each_member(i, t, member) { | |
30465003 DM |
3890 | if (strcmp(info->graph_root.node_name, |
3891 | __btf_name_by_offset(btf, member->name_off))) | |
f0c5941f KKD |
3892 | continue; |
3893 | /* Invalid BTF, two members with same name */ | |
3894 | if (n) | |
3895 | return -EINVAL; | |
3896 | n = btf_type_by_id(btf, member->type); | |
3897 | if (!__btf_type_is_struct(n)) | |
3898 | return -EINVAL; | |
9c395c1b | 3899 | if (strcmp(node_type_name, __btf_name_by_offset(btf, n->name_off))) |
f0c5941f KKD |
3900 | return -EINVAL; |
3901 | offset = __btf_member_bit_offset(n, member); | |
3902 | if (offset % 8) | |
3903 | return -EINVAL; | |
3904 | offset /= 8; | |
9c395c1b | 3905 | if (offset % node_type_align) |
f0c5941f KKD |
3906 | return -EINVAL; |
3907 | ||
30465003 DM |
3908 | field->graph_root.btf = (struct btf *)btf; |
3909 | field->graph_root.value_btf_id = info->graph_root.value_btf_id; | |
3910 | field->graph_root.node_offset = offset; | |
f0c5941f KKD |
3911 | } |
3912 | if (!n) | |
3913 | return -ENOENT; | |
3914 | return 0; | |
3915 | } | |
3916 | ||
9c395c1b DM |
3917 | static int btf_parse_list_head(const struct btf *btf, struct btf_field *field, |
3918 | struct btf_field_info *info) | |
3919 | { | |
3920 | return btf_parse_graph_root(btf, field, info, "bpf_list_node", | |
3921 | __alignof__(struct bpf_list_node)); | |
3922 | } | |
3923 | ||
3924 | static int btf_parse_rb_root(const struct btf *btf, struct btf_field *field, | |
3925 | struct btf_field_info *info) | |
3926 | { | |
3927 | return btf_parse_graph_root(btf, field, info, "bpf_rb_node", | |
3928 | __alignof__(struct bpf_rb_node)); | |
3929 | } | |
3930 | ||
cd2a8079 DM |
3931 | static int btf_field_cmp(const void *_a, const void *_b, const void *priv) |
3932 | { | |
3933 | const struct btf_field *a = (const struct btf_field *)_a; | |
3934 | const struct btf_field *b = (const struct btf_field *)_b; | |
3935 | ||
3936 | if (a->offset < b->offset) | |
3937 | return -1; | |
3938 | else if (a->offset > b->offset) | |
3939 | return 1; | |
3940 | return 0; | |
3941 | } | |
3942 | ||
db559117 KKD |
3943 | struct btf_record *btf_parse_fields(const struct btf *btf, const struct btf_type *t, |
3944 | u32 field_mask, u32 value_size) | |
61df10c7 | 3945 | { |
aa3496ac | 3946 | struct btf_field_info info_arr[BTF_FIELDS_MAX]; |
cd2a8079 | 3947 | u32 next_off = 0, field_type_size; |
aa3496ac KKD |
3948 | struct btf_record *rec; |
3949 | int ret, i, cnt; | |
61df10c7 | 3950 | |
db559117 | 3951 | ret = btf_find_field(btf, t, field_mask, info_arr, ARRAY_SIZE(info_arr)); |
61df10c7 KKD |
3952 | if (ret < 0) |
3953 | return ERR_PTR(ret); | |
3954 | if (!ret) | |
3955 | return NULL; | |
3956 | ||
aa3496ac | 3957 | cnt = ret; |
c22dfdd2 KKD |
3958 | /* This needs to be kzalloc to zero out padding and unused fields, see |
3959 | * comment in btf_record_equal. | |
3960 | */ | |
41948afc | 3961 | rec = kzalloc(struct_size(rec, fields, cnt), GFP_KERNEL | __GFP_NOWARN); |
aa3496ac | 3962 | if (!rec) |
61df10c7 | 3963 | return ERR_PTR(-ENOMEM); |
61df10c7 | 3964 | |
db559117 | 3965 | rec->spin_lock_off = -EINVAL; |
0de20461 | 3966 | rec->res_spin_lock_off = -EINVAL; |
db559117 | 3967 | rec->timer_off = -EINVAL; |
d56b63cf | 3968 | rec->wq_off = -EINVAL; |
d54730b5 | 3969 | rec->refcount_off = -EINVAL; |
db559117 | 3970 | for (i = 0; i < cnt; i++) { |
cd2a8079 DM |
3971 | field_type_size = btf_field_type_size(info_arr[i].type); |
3972 | if (info_arr[i].off + field_type_size > value_size) { | |
db559117 KKD |
3973 | WARN_ONCE(1, "verifier bug off %d size %d", info_arr[i].off, value_size); |
3974 | ret = -EFAULT; | |
61df10c7 KKD |
3975 | goto end; |
3976 | } | |
f0c5941f KKD |
3977 | if (info_arr[i].off < next_off) { |
3978 | ret = -EEXIST; | |
3979 | goto end; | |
3980 | } | |
cd2a8079 | 3981 | next_off = info_arr[i].off + field_type_size; |
61df10c7 | 3982 | |
aa3496ac KKD |
3983 | rec->field_mask |= info_arr[i].type; |
3984 | rec->fields[i].offset = info_arr[i].off; | |
3985 | rec->fields[i].type = info_arr[i].type; | |
cd2a8079 | 3986 | rec->fields[i].size = field_type_size; |
db559117 KKD |
3987 | |
3988 | switch (info_arr[i].type) { | |
3989 | case BPF_SPIN_LOCK: | |
3990 | WARN_ON_ONCE(rec->spin_lock_off >= 0); | |
3991 | /* Cache offset for faster lookup at runtime */ | |
3992 | rec->spin_lock_off = rec->fields[i].offset; | |
3993 | break; | |
0de20461 KKD |
3994 | case BPF_RES_SPIN_LOCK: |
3995 | WARN_ON_ONCE(rec->spin_lock_off >= 0); | |
3996 | /* Cache offset for faster lookup at runtime */ | |
3997 | rec->res_spin_lock_off = rec->fields[i].offset; | |
3998 | break; | |
db559117 KKD |
3999 | case BPF_TIMER: |
4000 | WARN_ON_ONCE(rec->timer_off >= 0); | |
4001 | /* Cache offset for faster lookup at runtime */ | |
4002 | rec->timer_off = rec->fields[i].offset; | |
4003 | break; | |
d56b63cf BT |
4004 | case BPF_WORKQUEUE: |
4005 | WARN_ON_ONCE(rec->wq_off >= 0); | |
4006 | /* Cache offset for faster lookup at runtime */ | |
4007 | rec->wq_off = rec->fields[i].offset; | |
4008 | break; | |
d54730b5 DM |
4009 | case BPF_REFCOUNT: |
4010 | WARN_ON_ONCE(rec->refcount_off >= 0); | |
4011 | /* Cache offset for faster lookup at runtime */ | |
4012 | rec->refcount_off = rec->fields[i].offset; | |
4013 | break; | |
db559117 KKD |
4014 | case BPF_KPTR_UNREF: |
4015 | case BPF_KPTR_REF: | |
55db92f4 | 4016 | case BPF_KPTR_PERCPU: |
1cb80d9e | 4017 | case BPF_UPTR: |
db559117 KKD |
4018 | ret = btf_parse_kptr(btf, &rec->fields[i], &info_arr[i]); |
4019 | if (ret < 0) | |
4020 | goto end; | |
4021 | break; | |
f0c5941f KKD |
4022 | case BPF_LIST_HEAD: |
4023 | ret = btf_parse_list_head(btf, &rec->fields[i], &info_arr[i]); | |
4024 | if (ret < 0) | |
4025 | goto end; | |
4026 | break; | |
9c395c1b DM |
4027 | case BPF_RB_ROOT: |
4028 | ret = btf_parse_rb_root(btf, &rec->fields[i], &info_arr[i]); | |
4029 | if (ret < 0) | |
4030 | goto end; | |
4031 | break; | |
8ffa5cc1 | 4032 | case BPF_LIST_NODE: |
9c395c1b | 4033 | case BPF_RB_NODE: |
8ffa5cc1 | 4034 | break; |
db559117 KKD |
4035 | default: |
4036 | ret = -EFAULT; | |
4037 | goto end; | |
4038 | } | |
aa3496ac | 4039 | rec->cnt++; |
61df10c7 | 4040 | } |
f0c5941f | 4041 | |
0de20461 KKD |
4042 | if (rec->spin_lock_off >= 0 && rec->res_spin_lock_off >= 0) { |
4043 | ret = -EINVAL; | |
4044 | goto end; | |
4045 | } | |
4046 | ||
9c395c1b DM |
4047 | /* bpf_{list_head, rb_node} require bpf_spin_lock */ |
4048 | if ((btf_record_has_field(rec, BPF_LIST_HEAD) || | |
0de20461 KKD |
4049 | btf_record_has_field(rec, BPF_RB_ROOT)) && |
4050 | (rec->spin_lock_off < 0 && rec->res_spin_lock_off < 0)) { | |
f0c5941f KKD |
4051 | ret = -EINVAL; |
4052 | goto end; | |
4053 | } | |
4054 | ||
404ad75a DM |
4055 | if (rec->refcount_off < 0 && |
4056 | btf_record_has_field(rec, BPF_LIST_NODE) && | |
a40d3632 DM |
4057 | btf_record_has_field(rec, BPF_RB_NODE)) { |
4058 | ret = -EINVAL; | |
4059 | goto end; | |
4060 | } | |
4061 | ||
cd2a8079 DM |
4062 | sort_r(rec->fields, rec->cnt, sizeof(struct btf_field), btf_field_cmp, |
4063 | NULL, rec); | |
4064 | ||
aa3496ac | 4065 | return rec; |
61df10c7 | 4066 | end: |
aa3496ac | 4067 | btf_record_free(rec); |
61df10c7 KKD |
4068 | return ERR_PTR(ret); |
4069 | } | |
4070 | ||
865ce09a KKD |
4071 | int btf_check_and_fixup_fields(const struct btf *btf, struct btf_record *rec) |
4072 | { | |
4073 | int i; | |
4074 | ||
9c395c1b DM |
4075 | /* There are three types that signify ownership of some other type: |
4076 | * kptr_ref, bpf_list_head, bpf_rb_root. | |
4077 | * kptr_ref only supports storing kernel types, which can't store | |
4078 | * references to program allocated local types. | |
4079 | * | |
4080 | * Hence we only need to ensure that bpf_{list_head,rb_root} ownership | |
4081 | * does not form cycles. | |
865ce09a | 4082 | */ |
1cb80d9e | 4083 | if (IS_ERR_OR_NULL(rec) || !(rec->field_mask & (BPF_GRAPH_ROOT | BPF_UPTR))) |
865ce09a KKD |
4084 | return 0; |
4085 | for (i = 0; i < rec->cnt; i++) { | |
4086 | struct btf_struct_meta *meta; | |
1cb80d9e | 4087 | const struct btf_type *t; |
865ce09a KKD |
4088 | u32 btf_id; |
4089 | ||
1cb80d9e KFL |
4090 | if (rec->fields[i].type == BPF_UPTR) { |
4091 | /* The uptr only supports pinning one page and cannot | |
4092 | * point to a kernel struct | |
4093 | */ | |
4094 | if (btf_is_kernel(rec->fields[i].kptr.btf)) | |
4095 | return -EINVAL; | |
4096 | t = btf_type_by_id(rec->fields[i].kptr.btf, | |
4097 | rec->fields[i].kptr.btf_id); | |
4098 | if (!t->size) | |
4099 | return -EINVAL; | |
4100 | if (t->size > PAGE_SIZE) | |
4101 | return -E2BIG; | |
4102 | continue; | |
4103 | } | |
4104 | ||
790ce3cf | 4105 | if (!(rec->fields[i].type & BPF_GRAPH_ROOT)) |
865ce09a | 4106 | continue; |
30465003 | 4107 | btf_id = rec->fields[i].graph_root.value_btf_id; |
865ce09a KKD |
4108 | meta = btf_find_struct_meta(btf, btf_id); |
4109 | if (!meta) | |
4110 | return -EFAULT; | |
30465003 | 4111 | rec->fields[i].graph_root.value_rec = meta->record; |
865ce09a | 4112 | |
9c395c1b DM |
4113 | /* We need to set value_rec for all root types, but no need |
4114 | * to check ownership cycle for a type unless it's also a | |
4115 | * node type. | |
4116 | */ | |
790ce3cf | 4117 | if (!(rec->field_mask & BPF_GRAPH_NODE)) |
865ce09a KKD |
4118 | continue; |
4119 | ||
4120 | /* We need to ensure ownership acyclicity among all types. The | |
4121 | * proper way to do it would be to topologically sort all BTF | |
4122 | * IDs based on the ownership edges, since there can be multiple | |
9c395c1b DM |
4123 | * bpf_{list_head,rb_node} in a type. Instead, we use the |
4124 | * following resaoning: | |
865ce09a KKD |
4125 | * |
4126 | * - A type can only be owned by another type in user BTF if it | |
9c395c1b | 4127 | * has a bpf_{list,rb}_node. Let's call these node types. |
865ce09a | 4128 | * - A type can only _own_ another type in user BTF if it has a |
9c395c1b | 4129 | * bpf_{list_head,rb_root}. Let's call these root types. |
865ce09a | 4130 | * |
9c395c1b DM |
4131 | * We ensure that if a type is both a root and node, its |
4132 | * element types cannot be root types. | |
865ce09a KKD |
4133 | * |
4134 | * To ensure acyclicity: | |
4135 | * | |
9c395c1b DM |
4136 | * When A is an root type but not a node, its ownership |
4137 | * chain can be: | |
865ce09a KKD |
4138 | * A -> B -> C |
4139 | * Where: | |
9c395c1b DM |
4140 | * - A is an root, e.g. has bpf_rb_root. |
4141 | * - B is both a root and node, e.g. has bpf_rb_node and | |
4142 | * bpf_list_head. | |
4143 | * - C is only an root, e.g. has bpf_list_node | |
865ce09a | 4144 | * |
9c395c1b DM |
4145 | * When A is both a root and node, some other type already |
4146 | * owns it in the BTF domain, hence it can not own | |
4147 | * another root type through any of the ownership edges. | |
865ce09a KKD |
4148 | * A -> B |
4149 | * Where: | |
9c395c1b DM |
4150 | * - A is both an root and node. |
4151 | * - B is only an node. | |
865ce09a | 4152 | */ |
790ce3cf | 4153 | if (meta->record->field_mask & BPF_GRAPH_ROOT) |
865ce09a KKD |
4154 | return -ELOOP; |
4155 | } | |
4156 | return 0; | |
4157 | } | |
4158 | ||
31d0bc81 AM |
4159 | static void __btf_struct_show(const struct btf *btf, const struct btf_type *t, |
4160 | u32 type_id, void *data, u8 bits_offset, | |
4161 | struct btf_show *show) | |
b00b8dae | 4162 | { |
b00b8dae | 4163 | const struct btf_member *member; |
31d0bc81 | 4164 | void *safe_data; |
b00b8dae MKL |
4165 | u32 i; |
4166 | ||
31d0bc81 AM |
4167 | safe_data = btf_show_start_struct_type(show, t, type_id, data); |
4168 | if (!safe_data) | |
4169 | return; | |
4170 | ||
b00b8dae MKL |
4171 | for_each_member(i, t, member) { |
4172 | const struct btf_type *member_type = btf_type_by_id(btf, | |
4173 | member->type); | |
b00b8dae | 4174 | const struct btf_kind_operations *ops; |
9d5f9f70 YS |
4175 | u32 member_offset, bitfield_size; |
4176 | u32 bytes_offset; | |
4177 | u8 bits8_offset; | |
b00b8dae | 4178 | |
31d0bc81 | 4179 | btf_show_start_member(show, member); |
b00b8dae | 4180 | |
8293eb99 AS |
4181 | member_offset = __btf_member_bit_offset(t, member); |
4182 | bitfield_size = __btf_member_bitfield_size(t, member); | |
17e3ac81 YS |
4183 | bytes_offset = BITS_ROUNDDOWN_BYTES(member_offset); |
4184 | bits8_offset = BITS_PER_BYTE_MASKED(member_offset); | |
9d5f9f70 | 4185 | if (bitfield_size) { |
31d0bc81 AM |
4186 | safe_data = btf_show_start_type(show, member_type, |
4187 | member->type, | |
4188 | data + bytes_offset); | |
4189 | if (safe_data) | |
4190 | btf_bitfield_show(safe_data, | |
4191 | bits8_offset, | |
4192 | bitfield_size, show); | |
4193 | btf_show_end_type(show); | |
9d5f9f70 | 4194 | } else { |
9d5f9f70 | 4195 | ops = btf_type_ops(member_type); |
31d0bc81 AM |
4196 | ops->show(btf, member_type, member->type, |
4197 | data + bytes_offset, bits8_offset, show); | |
9d5f9f70 | 4198 | } |
31d0bc81 AM |
4199 | |
4200 | btf_show_end_member(show); | |
b00b8dae | 4201 | } |
31d0bc81 AM |
4202 | |
4203 | btf_show_end_struct_type(show); | |
4204 | } | |
4205 | ||
4206 | static void btf_struct_show(const struct btf *btf, const struct btf_type *t, | |
4207 | u32 type_id, void *data, u8 bits_offset, | |
4208 | struct btf_show *show) | |
4209 | { | |
4210 | const struct btf_member *m = show->state.member; | |
4211 | ||
4212 | /* | |
4213 | * First check if any members would be shown (are non-zero). | |
4214 | * See comments above "struct btf_show" definition for more | |
4215 | * details on how this works at a high-level. | |
4216 | */ | |
4217 | if (show->state.depth > 0 && !(show->flags & BTF_SHOW_ZERO)) { | |
4218 | if (!show->state.depth_check) { | |
4219 | show->state.depth_check = show->state.depth + 1; | |
4220 | show->state.depth_to_show = 0; | |
4221 | } | |
4222 | __btf_struct_show(btf, t, type_id, data, bits_offset, show); | |
4223 | /* Restore saved member data here */ | |
4224 | show->state.member = m; | |
4225 | if (show->state.depth_check != show->state.depth + 1) | |
4226 | return; | |
4227 | show->state.depth_check = 0; | |
4228 | ||
4229 | if (show->state.depth_to_show <= show->state.depth) | |
4230 | return; | |
4231 | /* | |
4232 | * Reaching here indicates we have recursed and found | |
4233 | * non-zero child values. | |
4234 | */ | |
4235 | } | |
4236 | ||
4237 | __btf_struct_show(btf, t, type_id, data, bits_offset, show); | |
b00b8dae MKL |
4238 | } |
4239 | ||
7bae563c | 4240 | static const struct btf_kind_operations struct_ops = { |
69b693f0 | 4241 | .check_meta = btf_struct_check_meta, |
eb3f595d | 4242 | .resolve = btf_struct_resolve, |
179cde8c | 4243 | .check_member = btf_struct_check_member, |
9d5f9f70 | 4244 | .check_kflag_member = btf_generic_check_kflag_member, |
69b693f0 | 4245 | .log_details = btf_struct_log, |
31d0bc81 | 4246 | .show = btf_struct_show, |
69b693f0 MKL |
4247 | }; |
4248 | ||
179cde8c MKL |
4249 | static int btf_enum_check_member(struct btf_verifier_env *env, |
4250 | const struct btf_type *struct_type, | |
4251 | const struct btf_member *member, | |
4252 | const struct btf_type *member_type) | |
4253 | { | |
4254 | u32 struct_bits_off = member->offset; | |
4255 | u32 struct_size, bytes_offset; | |
4256 | ||
4257 | if (BITS_PER_BYTE_MASKED(struct_bits_off)) { | |
4258 | btf_verifier_log_member(env, struct_type, member, | |
4259 | "Member is not byte aligned"); | |
4260 | return -EINVAL; | |
4261 | } | |
4262 | ||
4263 | struct_size = struct_type->size; | |
4264 | bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off); | |
da6c7fae | 4265 | if (struct_size - bytes_offset < member_type->size) { |
179cde8c MKL |
4266 | btf_verifier_log_member(env, struct_type, member, |
4267 | "Member exceeds struct_size"); | |
4268 | return -EINVAL; | |
4269 | } | |
4270 | ||
4271 | return 0; | |
4272 | } | |
4273 | ||
9d5f9f70 YS |
4274 | static int btf_enum_check_kflag_member(struct btf_verifier_env *env, |
4275 | const struct btf_type *struct_type, | |
4276 | const struct btf_member *member, | |
4277 | const struct btf_type *member_type) | |
4278 | { | |
4279 | u32 struct_bits_off, nr_bits, bytes_end, struct_size; | |
4280 | u32 int_bitsize = sizeof(int) * BITS_PER_BYTE; | |
4281 | ||
4282 | struct_bits_off = BTF_MEMBER_BIT_OFFSET(member->offset); | |
4283 | nr_bits = BTF_MEMBER_BITFIELD_SIZE(member->offset); | |
4284 | if (!nr_bits) { | |
4285 | if (BITS_PER_BYTE_MASKED(struct_bits_off)) { | |
4286 | btf_verifier_log_member(env, struct_type, member, | |
4287 | "Member is not byte aligned"); | |
e3439af4 | 4288 | return -EINVAL; |
9d5f9f70 YS |
4289 | } |
4290 | ||
4291 | nr_bits = int_bitsize; | |
4292 | } else if (nr_bits > int_bitsize) { | |
4293 | btf_verifier_log_member(env, struct_type, member, | |
4294 | "Invalid member bitfield_size"); | |
4295 | return -EINVAL; | |
4296 | } | |
4297 | ||
4298 | struct_size = struct_type->size; | |
4299 | bytes_end = BITS_ROUNDUP_BYTES(struct_bits_off + nr_bits); | |
4300 | if (struct_size < bytes_end) { | |
4301 | btf_verifier_log_member(env, struct_type, member, | |
4302 | "Member exceeds struct_size"); | |
4303 | return -EINVAL; | |
4304 | } | |
4305 | ||
4306 | return 0; | |
4307 | } | |
4308 | ||
69b693f0 MKL |
4309 | static s32 btf_enum_check_meta(struct btf_verifier_env *env, |
4310 | const struct btf_type *t, | |
4311 | u32 meta_left) | |
4312 | { | |
4313 | const struct btf_enum *enums = btf_type_enum(t); | |
4314 | struct btf *btf = env->btf; | |
6089fb32 | 4315 | const char *fmt_str; |
69b693f0 MKL |
4316 | u16 i, nr_enums; |
4317 | u32 meta_needed; | |
4318 | ||
4319 | nr_enums = btf_type_vlen(t); | |
4320 | meta_needed = nr_enums * sizeof(*enums); | |
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 | ||
9eea9849 AS |
4329 | if (t->size > 8 || !is_power_of_2(t->size)) { |
4330 | btf_verifier_log_type(env, t, "Unexpected size"); | |
69b693f0 MKL |
4331 | return -EINVAL; |
4332 | } | |
4333 | ||
eb04bbb6 YS |
4334 | /* enum type either no name or a valid one */ |
4335 | if (t->name_off && | |
4336 | !btf_name_valid_identifier(env->btf, t->name_off)) { | |
4337 | btf_verifier_log_type(env, t, "Invalid name"); | |
4338 | return -EINVAL; | |
4339 | } | |
4340 | ||
69b693f0 MKL |
4341 | btf_verifier_log_type(env, t, NULL); |
4342 | ||
4343 | for (i = 0; i < nr_enums; i++) { | |
fbcf93eb | 4344 | if (!btf_name_offset_valid(btf, enums[i].name_off)) { |
69b693f0 | 4345 | btf_verifier_log(env, "\tInvalid name_offset:%u", |
fbcf93eb | 4346 | enums[i].name_off); |
69b693f0 MKL |
4347 | return -EINVAL; |
4348 | } | |
4349 | ||
eb04bbb6 YS |
4350 | /* enum member must have a valid name */ |
4351 | if (!enums[i].name_off || | |
4352 | !btf_name_valid_identifier(btf, enums[i].name_off)) { | |
4353 | btf_verifier_log_type(env, t, "Invalid name"); | |
4354 | return -EINVAL; | |
4355 | } | |
4356 | ||
8580ac94 AS |
4357 | if (env->log.level == BPF_LOG_KERNEL) |
4358 | continue; | |
6089fb32 YS |
4359 | fmt_str = btf_type_kflag(t) ? "\t%s val=%d\n" : "\t%s val=%u\n"; |
4360 | btf_verifier_log(env, fmt_str, | |
23127b33 | 4361 | __btf_name_by_offset(btf, enums[i].name_off), |
69b693f0 MKL |
4362 | enums[i].val); |
4363 | } | |
4364 | ||
4365 | return meta_needed; | |
4366 | } | |
4367 | ||
4368 | static void btf_enum_log(struct btf_verifier_env *env, | |
4369 | const struct btf_type *t) | |
4370 | { | |
4371 | btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t)); | |
4372 | } | |
4373 | ||
31d0bc81 AM |
4374 | static void btf_enum_show(const struct btf *btf, const struct btf_type *t, |
4375 | u32 type_id, void *data, u8 bits_offset, | |
4376 | struct btf_show *show) | |
b00b8dae MKL |
4377 | { |
4378 | const struct btf_enum *enums = btf_type_enum(t); | |
4379 | u32 i, nr_enums = btf_type_vlen(t); | |
31d0bc81 AM |
4380 | void *safe_data; |
4381 | int v; | |
4382 | ||
4383 | safe_data = btf_show_start_type(show, t, type_id, data); | |
4384 | if (!safe_data) | |
4385 | return; | |
4386 | ||
4387 | v = *(int *)safe_data; | |
b00b8dae MKL |
4388 | |
4389 | for (i = 0; i < nr_enums; i++) { | |
31d0bc81 AM |
4390 | if (v != enums[i].val) |
4391 | continue; | |
4392 | ||
4393 | btf_show_type_value(show, "%s", | |
4394 | __btf_name_by_offset(btf, | |
4395 | enums[i].name_off)); | |
4396 | ||
4397 | btf_show_end_type(show); | |
4398 | return; | |
b00b8dae MKL |
4399 | } |
4400 | ||
6089fb32 YS |
4401 | if (btf_type_kflag(t)) |
4402 | btf_show_type_value(show, "%d", v); | |
4403 | else | |
4404 | btf_show_type_value(show, "%u", v); | |
31d0bc81 | 4405 | btf_show_end_type(show); |
b00b8dae MKL |
4406 | } |
4407 | ||
7bae563c | 4408 | static const struct btf_kind_operations enum_ops = { |
69b693f0 | 4409 | .check_meta = btf_enum_check_meta, |
eb3f595d | 4410 | .resolve = btf_df_resolve, |
179cde8c | 4411 | .check_member = btf_enum_check_member, |
9d5f9f70 | 4412 | .check_kflag_member = btf_enum_check_kflag_member, |
69b693f0 | 4413 | .log_details = btf_enum_log, |
31d0bc81 | 4414 | .show = btf_enum_show, |
69b693f0 MKL |
4415 | }; |
4416 | ||
6089fb32 YS |
4417 | static s32 btf_enum64_check_meta(struct btf_verifier_env *env, |
4418 | const struct btf_type *t, | |
4419 | u32 meta_left) | |
4420 | { | |
4421 | const struct btf_enum64 *enums = btf_type_enum64(t); | |
4422 | struct btf *btf = env->btf; | |
4423 | const char *fmt_str; | |
4424 | u16 i, nr_enums; | |
4425 | u32 meta_needed; | |
4426 | ||
4427 | nr_enums = btf_type_vlen(t); | |
4428 | meta_needed = nr_enums * sizeof(*enums); | |
4429 | ||
4430 | if (meta_left < meta_needed) { | |
4431 | btf_verifier_log_basic(env, t, | |
4432 | "meta_left:%u meta_needed:%u", | |
4433 | meta_left, meta_needed); | |
4434 | return -EINVAL; | |
4435 | } | |
4436 | ||
4437 | if (t->size > 8 || !is_power_of_2(t->size)) { | |
4438 | btf_verifier_log_type(env, t, "Unexpected size"); | |
4439 | return -EINVAL; | |
4440 | } | |
4441 | ||
4442 | /* enum type either no name or a valid one */ | |
4443 | if (t->name_off && | |
4444 | !btf_name_valid_identifier(env->btf, t->name_off)) { | |
4445 | btf_verifier_log_type(env, t, "Invalid name"); | |
4446 | return -EINVAL; | |
4447 | } | |
4448 | ||
4449 | btf_verifier_log_type(env, t, NULL); | |
4450 | ||
4451 | for (i = 0; i < nr_enums; i++) { | |
4452 | if (!btf_name_offset_valid(btf, enums[i].name_off)) { | |
4453 | btf_verifier_log(env, "\tInvalid name_offset:%u", | |
4454 | enums[i].name_off); | |
4455 | return -EINVAL; | |
4456 | } | |
4457 | ||
4458 | /* enum member must have a valid name */ | |
4459 | if (!enums[i].name_off || | |
4460 | !btf_name_valid_identifier(btf, enums[i].name_off)) { | |
4461 | btf_verifier_log_type(env, t, "Invalid name"); | |
4462 | return -EINVAL; | |
4463 | } | |
4464 | ||
4465 | if (env->log.level == BPF_LOG_KERNEL) | |
4466 | continue; | |
4467 | ||
4468 | fmt_str = btf_type_kflag(t) ? "\t%s val=%lld\n" : "\t%s val=%llu\n"; | |
4469 | btf_verifier_log(env, fmt_str, | |
4470 | __btf_name_by_offset(btf, enums[i].name_off), | |
4471 | btf_enum64_value(enums + i)); | |
4472 | } | |
4473 | ||
4474 | return meta_needed; | |
4475 | } | |
4476 | ||
4477 | static void btf_enum64_show(const struct btf *btf, const struct btf_type *t, | |
4478 | u32 type_id, void *data, u8 bits_offset, | |
4479 | struct btf_show *show) | |
4480 | { | |
4481 | const struct btf_enum64 *enums = btf_type_enum64(t); | |
4482 | u32 i, nr_enums = btf_type_vlen(t); | |
4483 | void *safe_data; | |
4484 | s64 v; | |
4485 | ||
4486 | safe_data = btf_show_start_type(show, t, type_id, data); | |
4487 | if (!safe_data) | |
4488 | return; | |
4489 | ||
4490 | v = *(u64 *)safe_data; | |
4491 | ||
4492 | for (i = 0; i < nr_enums; i++) { | |
4493 | if (v != btf_enum64_value(enums + i)) | |
4494 | continue; | |
4495 | ||
4496 | btf_show_type_value(show, "%s", | |
4497 | __btf_name_by_offset(btf, | |
4498 | enums[i].name_off)); | |
4499 | ||
4500 | btf_show_end_type(show); | |
4501 | return; | |
4502 | } | |
4503 | ||
4504 | if (btf_type_kflag(t)) | |
4505 | btf_show_type_value(show, "%lld", v); | |
4506 | else | |
4507 | btf_show_type_value(show, "%llu", v); | |
4508 | btf_show_end_type(show); | |
4509 | } | |
4510 | ||
7bae563c | 4511 | static const struct btf_kind_operations enum64_ops = { |
6089fb32 YS |
4512 | .check_meta = btf_enum64_check_meta, |
4513 | .resolve = btf_df_resolve, | |
4514 | .check_member = btf_enum_check_member, | |
4515 | .check_kflag_member = btf_enum_check_kflag_member, | |
4516 | .log_details = btf_enum_log, | |
4517 | .show = btf_enum64_show, | |
4518 | }; | |
4519 | ||
2667a262 MKL |
4520 | static s32 btf_func_proto_check_meta(struct btf_verifier_env *env, |
4521 | const struct btf_type *t, | |
4522 | u32 meta_left) | |
4523 | { | |
4524 | u32 meta_needed = btf_type_vlen(t) * sizeof(struct btf_param); | |
4525 | ||
4526 | if (meta_left < meta_needed) { | |
4527 | btf_verifier_log_basic(env, t, | |
4528 | "meta_left:%u meta_needed:%u", | |
4529 | meta_left, meta_needed); | |
4530 | return -EINVAL; | |
4531 | } | |
4532 | ||
4533 | if (t->name_off) { | |
4534 | btf_verifier_log_type(env, t, "Invalid name"); | |
4535 | return -EINVAL; | |
4536 | } | |
4537 | ||
9d5f9f70 YS |
4538 | if (btf_type_kflag(t)) { |
4539 | btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); | |
4540 | return -EINVAL; | |
4541 | } | |
4542 | ||
2667a262 MKL |
4543 | btf_verifier_log_type(env, t, NULL); |
4544 | ||
4545 | return meta_needed; | |
4546 | } | |
4547 | ||
4548 | static void btf_func_proto_log(struct btf_verifier_env *env, | |
4549 | const struct btf_type *t) | |
4550 | { | |
4551 | const struct btf_param *args = (const struct btf_param *)(t + 1); | |
4552 | u16 nr_args = btf_type_vlen(t), i; | |
4553 | ||
4554 | btf_verifier_log(env, "return=%u args=(", t->type); | |
4555 | if (!nr_args) { | |
4556 | btf_verifier_log(env, "void"); | |
4557 | goto done; | |
4558 | } | |
4559 | ||
4560 | if (nr_args == 1 && !args[0].type) { | |
4561 | /* Only one vararg */ | |
4562 | btf_verifier_log(env, "vararg"); | |
4563 | goto done; | |
4564 | } | |
4565 | ||
4566 | btf_verifier_log(env, "%u %s", args[0].type, | |
23127b33 MKL |
4567 | __btf_name_by_offset(env->btf, |
4568 | args[0].name_off)); | |
2667a262 MKL |
4569 | for (i = 1; i < nr_args - 1; i++) |
4570 | btf_verifier_log(env, ", %u %s", args[i].type, | |
23127b33 MKL |
4571 | __btf_name_by_offset(env->btf, |
4572 | args[i].name_off)); | |
2667a262 MKL |
4573 | |
4574 | if (nr_args > 1) { | |
4575 | const struct btf_param *last_arg = &args[nr_args - 1]; | |
4576 | ||
4577 | if (last_arg->type) | |
4578 | btf_verifier_log(env, ", %u %s", last_arg->type, | |
23127b33 MKL |
4579 | __btf_name_by_offset(env->btf, |
4580 | last_arg->name_off)); | |
2667a262 MKL |
4581 | else |
4582 | btf_verifier_log(env, ", vararg"); | |
4583 | } | |
4584 | ||
4585 | done: | |
4586 | btf_verifier_log(env, ")"); | |
4587 | } | |
4588 | ||
7bae563c | 4589 | static const struct btf_kind_operations func_proto_ops = { |
2667a262 MKL |
4590 | .check_meta = btf_func_proto_check_meta, |
4591 | .resolve = btf_df_resolve, | |
4592 | /* | |
4593 | * BTF_KIND_FUNC_PROTO cannot be directly referred by | |
4594 | * a struct's member. | |
4595 | * | |
8fb33b60 | 4596 | * It should be a function pointer instead. |
2667a262 MKL |
4597 | * (i.e. struct's member -> BTF_KIND_PTR -> BTF_KIND_FUNC_PROTO) |
4598 | * | |
4599 | * Hence, there is no btf_func_check_member(). | |
4600 | */ | |
4601 | .check_member = btf_df_check_member, | |
9d5f9f70 | 4602 | .check_kflag_member = btf_df_check_kflag_member, |
2667a262 | 4603 | .log_details = btf_func_proto_log, |
31d0bc81 | 4604 | .show = btf_df_show, |
2667a262 MKL |
4605 | }; |
4606 | ||
4607 | static s32 btf_func_check_meta(struct btf_verifier_env *env, | |
4608 | const struct btf_type *t, | |
4609 | u32 meta_left) | |
4610 | { | |
4611 | if (!t->name_off || | |
4612 | !btf_name_valid_identifier(env->btf, t->name_off)) { | |
4613 | btf_verifier_log_type(env, t, "Invalid name"); | |
4614 | return -EINVAL; | |
4615 | } | |
4616 | ||
51c39bb1 AS |
4617 | if (btf_type_vlen(t) > BTF_FUNC_GLOBAL) { |
4618 | btf_verifier_log_type(env, t, "Invalid func linkage"); | |
2667a262 MKL |
4619 | return -EINVAL; |
4620 | } | |
4621 | ||
9d5f9f70 YS |
4622 | if (btf_type_kflag(t)) { |
4623 | btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); | |
4624 | return -EINVAL; | |
4625 | } | |
4626 | ||
2667a262 MKL |
4627 | btf_verifier_log_type(env, t, NULL); |
4628 | ||
4629 | return 0; | |
4630 | } | |
4631 | ||
d7e7b42f YS |
4632 | static int btf_func_resolve(struct btf_verifier_env *env, |
4633 | const struct resolve_vertex *v) | |
4634 | { | |
4635 | const struct btf_type *t = v->t; | |
4636 | u32 next_type_id = t->type; | |
4637 | int err; | |
4638 | ||
4639 | err = btf_func_check(env, t); | |
4640 | if (err) | |
4641 | return err; | |
4642 | ||
4643 | env_stack_pop_resolved(env, next_type_id, 0); | |
4644 | return 0; | |
4645 | } | |
4646 | ||
7bae563c | 4647 | static const struct btf_kind_operations func_ops = { |
2667a262 | 4648 | .check_meta = btf_func_check_meta, |
d7e7b42f | 4649 | .resolve = btf_func_resolve, |
2667a262 | 4650 | .check_member = btf_df_check_member, |
9d5f9f70 | 4651 | .check_kflag_member = btf_df_check_kflag_member, |
2667a262 | 4652 | .log_details = btf_ref_type_log, |
31d0bc81 | 4653 | .show = btf_df_show, |
2667a262 MKL |
4654 | }; |
4655 | ||
1dc92851 DB |
4656 | static s32 btf_var_check_meta(struct btf_verifier_env *env, |
4657 | const struct btf_type *t, | |
4658 | u32 meta_left) | |
4659 | { | |
4660 | const struct btf_var *var; | |
4661 | u32 meta_needed = sizeof(*var); | |
4662 | ||
4663 | if (meta_left < meta_needed) { | |
4664 | btf_verifier_log_basic(env, t, | |
4665 | "meta_left:%u meta_needed:%u", | |
4666 | meta_left, meta_needed); | |
4667 | return -EINVAL; | |
4668 | } | |
4669 | ||
4670 | if (btf_type_vlen(t)) { | |
4671 | btf_verifier_log_type(env, t, "vlen != 0"); | |
4672 | return -EINVAL; | |
4673 | } | |
4674 | ||
4675 | if (btf_type_kflag(t)) { | |
4676 | btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); | |
4677 | return -EINVAL; | |
4678 | } | |
4679 | ||
4680 | if (!t->name_off || | |
febb6f3e | 4681 | !btf_name_valid_identifier(env->btf, t->name_off)) { |
1dc92851 DB |
4682 | btf_verifier_log_type(env, t, "Invalid name"); |
4683 | return -EINVAL; | |
4684 | } | |
4685 | ||
4686 | /* A var cannot be in type void */ | |
4687 | if (!t->type || !BTF_TYPE_ID_VALID(t->type)) { | |
4688 | btf_verifier_log_type(env, t, "Invalid type_id"); | |
4689 | return -EINVAL; | |
4690 | } | |
4691 | ||
4692 | var = btf_type_var(t); | |
4693 | if (var->linkage != BTF_VAR_STATIC && | |
4694 | var->linkage != BTF_VAR_GLOBAL_ALLOCATED) { | |
4695 | btf_verifier_log_type(env, t, "Linkage not supported"); | |
4696 | return -EINVAL; | |
4697 | } | |
4698 | ||
4699 | btf_verifier_log_type(env, t, NULL); | |
4700 | ||
4701 | return meta_needed; | |
4702 | } | |
4703 | ||
4704 | static void btf_var_log(struct btf_verifier_env *env, const struct btf_type *t) | |
4705 | { | |
4706 | const struct btf_var *var = btf_type_var(t); | |
4707 | ||
4708 | btf_verifier_log(env, "type_id=%u linkage=%u", t->type, var->linkage); | |
4709 | } | |
4710 | ||
4711 | static const struct btf_kind_operations var_ops = { | |
4712 | .check_meta = btf_var_check_meta, | |
4713 | .resolve = btf_var_resolve, | |
4714 | .check_member = btf_df_check_member, | |
4715 | .check_kflag_member = btf_df_check_kflag_member, | |
4716 | .log_details = btf_var_log, | |
31d0bc81 | 4717 | .show = btf_var_show, |
1dc92851 DB |
4718 | }; |
4719 | ||
4720 | static s32 btf_datasec_check_meta(struct btf_verifier_env *env, | |
4721 | const struct btf_type *t, | |
4722 | u32 meta_left) | |
4723 | { | |
4724 | const struct btf_var_secinfo *vsi; | |
4725 | u64 last_vsi_end_off = 0, sum = 0; | |
4726 | u32 i, meta_needed; | |
4727 | ||
4728 | meta_needed = btf_type_vlen(t) * sizeof(*vsi); | |
4729 | if (meta_left < meta_needed) { | |
4730 | btf_verifier_log_basic(env, t, | |
4731 | "meta_left:%u meta_needed:%u", | |
4732 | meta_left, meta_needed); | |
4733 | return -EINVAL; | |
4734 | } | |
4735 | ||
1dc92851 DB |
4736 | if (!t->size) { |
4737 | btf_verifier_log_type(env, t, "size == 0"); | |
4738 | return -EINVAL; | |
4739 | } | |
4740 | ||
4741 | if (btf_type_kflag(t)) { | |
4742 | btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); | |
4743 | return -EINVAL; | |
4744 | } | |
4745 | ||
4746 | if (!t->name_off || | |
4747 | !btf_name_valid_section(env->btf, t->name_off)) { | |
4748 | btf_verifier_log_type(env, t, "Invalid name"); | |
4749 | return -EINVAL; | |
4750 | } | |
4751 | ||
4752 | btf_verifier_log_type(env, t, NULL); | |
4753 | ||
4754 | for_each_vsi(i, t, vsi) { | |
4755 | /* A var cannot be in type void */ | |
4756 | if (!vsi->type || !BTF_TYPE_ID_VALID(vsi->type)) { | |
4757 | btf_verifier_log_vsi(env, t, vsi, | |
4758 | "Invalid type_id"); | |
4759 | return -EINVAL; | |
4760 | } | |
4761 | ||
4762 | if (vsi->offset < last_vsi_end_off || vsi->offset >= t->size) { | |
4763 | btf_verifier_log_vsi(env, t, vsi, | |
4764 | "Invalid offset"); | |
4765 | return -EINVAL; | |
4766 | } | |
4767 | ||
4768 | if (!vsi->size || vsi->size > t->size) { | |
4769 | btf_verifier_log_vsi(env, t, vsi, | |
4770 | "Invalid size"); | |
4771 | return -EINVAL; | |
4772 | } | |
4773 | ||
4774 | last_vsi_end_off = vsi->offset + vsi->size; | |
4775 | if (last_vsi_end_off > t->size) { | |
4776 | btf_verifier_log_vsi(env, t, vsi, | |
4777 | "Invalid offset+size"); | |
4778 | return -EINVAL; | |
4779 | } | |
4780 | ||
4781 | btf_verifier_log_vsi(env, t, vsi, NULL); | |
4782 | sum += vsi->size; | |
4783 | } | |
4784 | ||
4785 | if (t->size < sum) { | |
4786 | btf_verifier_log_type(env, t, "Invalid btf_info size"); | |
4787 | return -EINVAL; | |
4788 | } | |
4789 | ||
4790 | return meta_needed; | |
4791 | } | |
4792 | ||
4793 | static int btf_datasec_resolve(struct btf_verifier_env *env, | |
4794 | const struct resolve_vertex *v) | |
4795 | { | |
4796 | const struct btf_var_secinfo *vsi; | |
4797 | struct btf *btf = env->btf; | |
4798 | u16 i; | |
4799 | ||
9b459804 | 4800 | env->resolve_mode = RESOLVE_TBD; |
1dc92851 DB |
4801 | for_each_vsi_from(i, v->next_member, v->t, vsi) { |
4802 | u32 var_type_id = vsi->type, type_id, type_size = 0; | |
4803 | const struct btf_type *var_type = btf_type_by_id(env->btf, | |
4804 | var_type_id); | |
4805 | if (!var_type || !btf_type_is_var(var_type)) { | |
4806 | btf_verifier_log_vsi(env, v->t, vsi, | |
4807 | "Not a VAR kind member"); | |
4808 | return -EINVAL; | |
4809 | } | |
4810 | ||
4811 | if (!env_type_is_resolve_sink(env, var_type) && | |
4812 | !env_type_is_resolved(env, var_type_id)) { | |
4813 | env_stack_set_next_member(env, i + 1); | |
4814 | return env_stack_push(env, var_type, var_type_id); | |
4815 | } | |
4816 | ||
4817 | type_id = var_type->type; | |
4818 | if (!btf_type_id_size(btf, &type_id, &type_size)) { | |
4819 | btf_verifier_log_vsi(env, v->t, vsi, "Invalid type"); | |
4820 | return -EINVAL; | |
4821 | } | |
4822 | ||
4823 | if (vsi->size < type_size) { | |
4824 | btf_verifier_log_vsi(env, v->t, vsi, "Invalid size"); | |
4825 | return -EINVAL; | |
4826 | } | |
4827 | } | |
4828 | ||
4829 | env_stack_pop_resolved(env, 0, 0); | |
4830 | return 0; | |
4831 | } | |
4832 | ||
4833 | static void btf_datasec_log(struct btf_verifier_env *env, | |
4834 | const struct btf_type *t) | |
4835 | { | |
4836 | btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t)); | |
4837 | } | |
4838 | ||
31d0bc81 AM |
4839 | static void btf_datasec_show(const struct btf *btf, |
4840 | const struct btf_type *t, u32 type_id, | |
4841 | void *data, u8 bits_offset, | |
4842 | struct btf_show *show) | |
1dc92851 DB |
4843 | { |
4844 | const struct btf_var_secinfo *vsi; | |
4845 | const struct btf_type *var; | |
4846 | u32 i; | |
4847 | ||
31d0bc81 AM |
4848 | if (!btf_show_start_type(show, t, type_id, data)) |
4849 | return; | |
4850 | ||
4851 | btf_show_type_value(show, "section (\"%s\") = {", | |
4852 | __btf_name_by_offset(btf, t->name_off)); | |
1dc92851 DB |
4853 | for_each_vsi(i, t, vsi) { |
4854 | var = btf_type_by_id(btf, vsi->type); | |
4855 | if (i) | |
31d0bc81 AM |
4856 | btf_show(show, ","); |
4857 | btf_type_ops(var)->show(btf, var, vsi->type, | |
4858 | data + vsi->offset, bits_offset, show); | |
1dc92851 | 4859 | } |
31d0bc81 | 4860 | btf_show_end_type(show); |
1dc92851 DB |
4861 | } |
4862 | ||
4863 | static const struct btf_kind_operations datasec_ops = { | |
4864 | .check_meta = btf_datasec_check_meta, | |
4865 | .resolve = btf_datasec_resolve, | |
4866 | .check_member = btf_df_check_member, | |
4867 | .check_kflag_member = btf_df_check_kflag_member, | |
4868 | .log_details = btf_datasec_log, | |
31d0bc81 | 4869 | .show = btf_datasec_show, |
1dc92851 DB |
4870 | }; |
4871 | ||
b1828f0b IL |
4872 | static s32 btf_float_check_meta(struct btf_verifier_env *env, |
4873 | const struct btf_type *t, | |
4874 | u32 meta_left) | |
4875 | { | |
4876 | if (btf_type_vlen(t)) { | |
4877 | btf_verifier_log_type(env, t, "vlen != 0"); | |
4878 | return -EINVAL; | |
4879 | } | |
4880 | ||
4881 | if (btf_type_kflag(t)) { | |
4882 | btf_verifier_log_type(env, t, "Invalid btf_info kind_flag"); | |
4883 | return -EINVAL; | |
4884 | } | |
4885 | ||
4886 | if (t->size != 2 && t->size != 4 && t->size != 8 && t->size != 12 && | |
4887 | t->size != 16) { | |
4888 | btf_verifier_log_type(env, t, "Invalid type_size"); | |
4889 | return -EINVAL; | |
4890 | } | |
4891 | ||
4892 | btf_verifier_log_type(env, t, NULL); | |
4893 | ||
4894 | return 0; | |
4895 | } | |
4896 | ||
4897 | static int btf_float_check_member(struct btf_verifier_env *env, | |
4898 | const struct btf_type *struct_type, | |
4899 | const struct btf_member *member, | |
4900 | const struct btf_type *member_type) | |
4901 | { | |
4902 | u64 start_offset_bytes; | |
4903 | u64 end_offset_bytes; | |
4904 | u64 misalign_bits; | |
4905 | u64 align_bytes; | |
4906 | u64 align_bits; | |
4907 | ||
4908 | /* Different architectures have different alignment requirements, so | |
4909 | * here we check only for the reasonable minimum. This way we ensure | |
4910 | * that types after CO-RE can pass the kernel BTF verifier. | |
4911 | */ | |
4912 | align_bytes = min_t(u64, sizeof(void *), member_type->size); | |
4913 | align_bits = align_bytes * BITS_PER_BYTE; | |
4914 | div64_u64_rem(member->offset, align_bits, &misalign_bits); | |
4915 | if (misalign_bits) { | |
4916 | btf_verifier_log_member(env, struct_type, member, | |
4917 | "Member is not properly aligned"); | |
4918 | return -EINVAL; | |
4919 | } | |
4920 | ||
4921 | start_offset_bytes = member->offset / BITS_PER_BYTE; | |
4922 | end_offset_bytes = start_offset_bytes + member_type->size; | |
4923 | if (end_offset_bytes > struct_type->size) { | |
4924 | btf_verifier_log_member(env, struct_type, member, | |
4925 | "Member exceeds struct_size"); | |
4926 | return -EINVAL; | |
4927 | } | |
4928 | ||
4929 | return 0; | |
4930 | } | |
4931 | ||
4932 | static void btf_float_log(struct btf_verifier_env *env, | |
4933 | const struct btf_type *t) | |
4934 | { | |
4935 | btf_verifier_log(env, "size=%u", t->size); | |
4936 | } | |
4937 | ||
4938 | static const struct btf_kind_operations float_ops = { | |
4939 | .check_meta = btf_float_check_meta, | |
4940 | .resolve = btf_df_resolve, | |
4941 | .check_member = btf_float_check_member, | |
4942 | .check_kflag_member = btf_generic_check_kflag_member, | |
4943 | .log_details = btf_float_log, | |
4944 | .show = btf_df_show, | |
4945 | }; | |
4946 | ||
223f903e | 4947 | static s32 btf_decl_tag_check_meta(struct btf_verifier_env *env, |
b5ea834d YS |
4948 | const struct btf_type *t, |
4949 | u32 meta_left) | |
4950 | { | |
223f903e | 4951 | const struct btf_decl_tag *tag; |
b5ea834d YS |
4952 | u32 meta_needed = sizeof(*tag); |
4953 | s32 component_idx; | |
4954 | const char *value; | |
4955 | ||
4956 | if (meta_left < meta_needed) { | |
4957 | btf_verifier_log_basic(env, t, | |
4958 | "meta_left:%u meta_needed:%u", | |
4959 | meta_left, meta_needed); | |
4960 | return -EINVAL; | |
4961 | } | |
4962 | ||
4963 | value = btf_name_by_offset(env->btf, t->name_off); | |
4964 | if (!value || !value[0]) { | |
4965 | btf_verifier_log_type(env, t, "Invalid value"); | |
4966 | return -EINVAL; | |
4967 | } | |
4968 | ||
4969 | if (btf_type_vlen(t)) { | |
4970 | btf_verifier_log_type(env, t, "vlen != 0"); | |
4971 | return -EINVAL; | |
4972 | } | |
4973 | ||
223f903e | 4974 | component_idx = btf_type_decl_tag(t)->component_idx; |
b5ea834d YS |
4975 | if (component_idx < -1) { |
4976 | btf_verifier_log_type(env, t, "Invalid component_idx"); | |
4977 | return -EINVAL; | |
4978 | } | |
4979 | ||
4980 | btf_verifier_log_type(env, t, NULL); | |
4981 | ||
4982 | return meta_needed; | |
4983 | } | |
4984 | ||
223f903e | 4985 | static int btf_decl_tag_resolve(struct btf_verifier_env *env, |
b5ea834d YS |
4986 | const struct resolve_vertex *v) |
4987 | { | |
4988 | const struct btf_type *next_type; | |
4989 | const struct btf_type *t = v->t; | |
4990 | u32 next_type_id = t->type; | |
4991 | struct btf *btf = env->btf; | |
4992 | s32 component_idx; | |
4993 | u32 vlen; | |
4994 | ||
4995 | next_type = btf_type_by_id(btf, next_type_id); | |
223f903e | 4996 | if (!next_type || !btf_type_is_decl_tag_target(next_type)) { |
b5ea834d YS |
4997 | btf_verifier_log_type(env, v->t, "Invalid type_id"); |
4998 | return -EINVAL; | |
4999 | } | |
5000 | ||
5001 | if (!env_type_is_resolve_sink(env, next_type) && | |
5002 | !env_type_is_resolved(env, next_type_id)) | |
5003 | return env_stack_push(env, next_type, next_type_id); | |
5004 | ||
223f903e | 5005 | component_idx = btf_type_decl_tag(t)->component_idx; |
b5ea834d | 5006 | if (component_idx != -1) { |
bd16dee6 | 5007 | if (btf_type_is_var(next_type) || btf_type_is_typedef(next_type)) { |
b5ea834d YS |
5008 | btf_verifier_log_type(env, v->t, "Invalid component_idx"); |
5009 | return -EINVAL; | |
5010 | } | |
5011 | ||
5012 | if (btf_type_is_struct(next_type)) { | |
5013 | vlen = btf_type_vlen(next_type); | |
5014 | } else { | |
5015 | /* next_type should be a function */ | |
5016 | next_type = btf_type_by_id(btf, next_type->type); | |
5017 | vlen = btf_type_vlen(next_type); | |
5018 | } | |
5019 | ||
5020 | if ((u32)component_idx >= vlen) { | |
5021 | btf_verifier_log_type(env, v->t, "Invalid component_idx"); | |
5022 | return -EINVAL; | |
5023 | } | |
5024 | } | |
5025 | ||
5026 | env_stack_pop_resolved(env, next_type_id, 0); | |
5027 | ||
5028 | return 0; | |
5029 | } | |
5030 | ||
223f903e | 5031 | static void btf_decl_tag_log(struct btf_verifier_env *env, const struct btf_type *t) |
b5ea834d YS |
5032 | { |
5033 | btf_verifier_log(env, "type=%u component_idx=%d", t->type, | |
223f903e | 5034 | btf_type_decl_tag(t)->component_idx); |
b5ea834d YS |
5035 | } |
5036 | ||
223f903e YS |
5037 | static const struct btf_kind_operations decl_tag_ops = { |
5038 | .check_meta = btf_decl_tag_check_meta, | |
5039 | .resolve = btf_decl_tag_resolve, | |
b5ea834d YS |
5040 | .check_member = btf_df_check_member, |
5041 | .check_kflag_member = btf_df_check_kflag_member, | |
223f903e | 5042 | .log_details = btf_decl_tag_log, |
b5ea834d YS |
5043 | .show = btf_df_show, |
5044 | }; | |
5045 | ||
2667a262 MKL |
5046 | static int btf_func_proto_check(struct btf_verifier_env *env, |
5047 | const struct btf_type *t) | |
5048 | { | |
5049 | const struct btf_type *ret_type; | |
5050 | const struct btf_param *args; | |
5051 | const struct btf *btf; | |
5052 | u16 nr_args, i; | |
5053 | int err; | |
5054 | ||
5055 | btf = env->btf; | |
5056 | args = (const struct btf_param *)(t + 1); | |
5057 | nr_args = btf_type_vlen(t); | |
5058 | ||
5059 | /* Check func return type which could be "void" (t->type == 0) */ | |
5060 | if (t->type) { | |
5061 | u32 ret_type_id = t->type; | |
5062 | ||
5063 | ret_type = btf_type_by_id(btf, ret_type_id); | |
5064 | if (!ret_type) { | |
5065 | btf_verifier_log_type(env, t, "Invalid return type"); | |
5066 | return -EINVAL; | |
5067 | } | |
5068 | ||
ea68376c SF |
5069 | if (btf_type_is_resolve_source_only(ret_type)) { |
5070 | btf_verifier_log_type(env, t, "Invalid return type"); | |
5071 | return -EINVAL; | |
5072 | } | |
5073 | ||
2667a262 MKL |
5074 | if (btf_type_needs_resolve(ret_type) && |
5075 | !env_type_is_resolved(env, ret_type_id)) { | |
5076 | err = btf_resolve(env, ret_type, ret_type_id); | |
5077 | if (err) | |
5078 | return err; | |
5079 | } | |
5080 | ||
5081 | /* Ensure the return type is a type that has a size */ | |
5082 | if (!btf_type_id_size(btf, &ret_type_id, NULL)) { | |
5083 | btf_verifier_log_type(env, t, "Invalid return type"); | |
5084 | return -EINVAL; | |
5085 | } | |
5086 | } | |
5087 | ||
5088 | if (!nr_args) | |
5089 | return 0; | |
5090 | ||
5091 | /* Last func arg type_id could be 0 if it is a vararg */ | |
5092 | if (!args[nr_args - 1].type) { | |
5093 | if (args[nr_args - 1].name_off) { | |
5094 | btf_verifier_log_type(env, t, "Invalid arg#%u", | |
5095 | nr_args); | |
5096 | return -EINVAL; | |
5097 | } | |
5098 | nr_args--; | |
5099 | } | |
5100 | ||
2667a262 MKL |
5101 | for (i = 0; i < nr_args; i++) { |
5102 | const struct btf_type *arg_type; | |
5103 | u32 arg_type_id; | |
5104 | ||
5105 | arg_type_id = args[i].type; | |
5106 | arg_type = btf_type_by_id(btf, arg_type_id); | |
5107 | if (!arg_type) { | |
5108 | btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1); | |
5bad3587 | 5109 | return -EINVAL; |
2667a262 MKL |
5110 | } |
5111 | ||
f17472d4 SF |
5112 | if (btf_type_is_resolve_source_only(arg_type)) { |
5113 | btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1); | |
5114 | return -EINVAL; | |
5115 | } | |
5116 | ||
2667a262 MKL |
5117 | if (args[i].name_off && |
5118 | (!btf_name_offset_valid(btf, args[i].name_off) || | |
5119 | !btf_name_valid_identifier(btf, args[i].name_off))) { | |
5120 | btf_verifier_log_type(env, t, | |
5121 | "Invalid arg#%u", i + 1); | |
5bad3587 | 5122 | return -EINVAL; |
2667a262 MKL |
5123 | } |
5124 | ||
5125 | if (btf_type_needs_resolve(arg_type) && | |
5126 | !env_type_is_resolved(env, arg_type_id)) { | |
5127 | err = btf_resolve(env, arg_type, arg_type_id); | |
5128 | if (err) | |
5bad3587 | 5129 | return err; |
2667a262 MKL |
5130 | } |
5131 | ||
5132 | if (!btf_type_id_size(btf, &arg_type_id, NULL)) { | |
5133 | btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1); | |
5bad3587 | 5134 | return -EINVAL; |
2667a262 MKL |
5135 | } |
5136 | } | |
5137 | ||
5bad3587 | 5138 | return 0; |
2667a262 MKL |
5139 | } |
5140 | ||
5141 | static int btf_func_check(struct btf_verifier_env *env, | |
5142 | const struct btf_type *t) | |
5143 | { | |
5144 | const struct btf_type *proto_type; | |
5145 | const struct btf_param *args; | |
5146 | const struct btf *btf; | |
5147 | u16 nr_args, i; | |
5148 | ||
5149 | btf = env->btf; | |
5150 | proto_type = btf_type_by_id(btf, t->type); | |
5151 | ||
5152 | if (!proto_type || !btf_type_is_func_proto(proto_type)) { | |
5153 | btf_verifier_log_type(env, t, "Invalid type_id"); | |
5154 | return -EINVAL; | |
5155 | } | |
5156 | ||
5157 | args = (const struct btf_param *)(proto_type + 1); | |
5158 | nr_args = btf_type_vlen(proto_type); | |
5159 | for (i = 0; i < nr_args; i++) { | |
5160 | if (!args[i].name_off && args[i].type) { | |
5161 | btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1); | |
5162 | return -EINVAL; | |
5163 | } | |
5164 | } | |
5165 | ||
5166 | return 0; | |
5167 | } | |
5168 | ||
69b693f0 MKL |
5169 | static const struct btf_kind_operations * const kind_ops[NR_BTF_KINDS] = { |
5170 | [BTF_KIND_INT] = &int_ops, | |
5171 | [BTF_KIND_PTR] = &ptr_ops, | |
5172 | [BTF_KIND_ARRAY] = &array_ops, | |
5173 | [BTF_KIND_STRUCT] = &struct_ops, | |
5174 | [BTF_KIND_UNION] = &struct_ops, | |
5175 | [BTF_KIND_ENUM] = &enum_ops, | |
5176 | [BTF_KIND_FWD] = &fwd_ops, | |
5177 | [BTF_KIND_TYPEDEF] = &modifier_ops, | |
5178 | [BTF_KIND_VOLATILE] = &modifier_ops, | |
5179 | [BTF_KIND_CONST] = &modifier_ops, | |
5180 | [BTF_KIND_RESTRICT] = &modifier_ops, | |
2667a262 MKL |
5181 | [BTF_KIND_FUNC] = &func_ops, |
5182 | [BTF_KIND_FUNC_PROTO] = &func_proto_ops, | |
1dc92851 DB |
5183 | [BTF_KIND_VAR] = &var_ops, |
5184 | [BTF_KIND_DATASEC] = &datasec_ops, | |
b1828f0b | 5185 | [BTF_KIND_FLOAT] = &float_ops, |
223f903e | 5186 | [BTF_KIND_DECL_TAG] = &decl_tag_ops, |
8c42d2fa | 5187 | [BTF_KIND_TYPE_TAG] = &modifier_ops, |
6089fb32 | 5188 | [BTF_KIND_ENUM64] = &enum64_ops, |
69b693f0 MKL |
5189 | }; |
5190 | ||
5191 | static s32 btf_check_meta(struct btf_verifier_env *env, | |
5192 | const struct btf_type *t, | |
5193 | u32 meta_left) | |
5194 | { | |
5195 | u32 saved_meta_left = meta_left; | |
5196 | s32 var_meta_size; | |
5197 | ||
5198 | if (meta_left < sizeof(*t)) { | |
5199 | btf_verifier_log(env, "[%u] meta_left:%u meta_needed:%zu", | |
5200 | env->log_type_id, meta_left, sizeof(*t)); | |
5201 | return -EINVAL; | |
5202 | } | |
5203 | meta_left -= sizeof(*t); | |
5204 | ||
aea2f7b8 MKL |
5205 | if (t->info & ~BTF_INFO_MASK) { |
5206 | btf_verifier_log(env, "[%u] Invalid btf_info:%x", | |
5207 | env->log_type_id, t->info); | |
5208 | return -EINVAL; | |
5209 | } | |
5210 | ||
69b693f0 MKL |
5211 | if (BTF_INFO_KIND(t->info) > BTF_KIND_MAX || |
5212 | BTF_INFO_KIND(t->info) == BTF_KIND_UNKN) { | |
5213 | btf_verifier_log(env, "[%u] Invalid kind:%u", | |
5214 | env->log_type_id, BTF_INFO_KIND(t->info)); | |
5215 | return -EINVAL; | |
5216 | } | |
5217 | ||
fbcf93eb | 5218 | if (!btf_name_offset_valid(env->btf, t->name_off)) { |
69b693f0 | 5219 | btf_verifier_log(env, "[%u] Invalid name_offset:%u", |
fbcf93eb | 5220 | env->log_type_id, t->name_off); |
69b693f0 MKL |
5221 | return -EINVAL; |
5222 | } | |
5223 | ||
5224 | var_meta_size = btf_type_ops(t)->check_meta(env, t, meta_left); | |
5225 | if (var_meta_size < 0) | |
5226 | return var_meta_size; | |
5227 | ||
5228 | meta_left -= var_meta_size; | |
5229 | ||
5230 | return saved_meta_left - meta_left; | |
5231 | } | |
5232 | ||
5233 | static int btf_check_all_metas(struct btf_verifier_env *env) | |
5234 | { | |
5235 | struct btf *btf = env->btf; | |
5236 | struct btf_header *hdr; | |
5237 | void *cur, *end; | |
5238 | ||
f80442a4 | 5239 | hdr = &btf->hdr; |
69b693f0 | 5240 | cur = btf->nohdr_data + hdr->type_off; |
4b1c5d91 | 5241 | end = cur + hdr->type_len; |
69b693f0 | 5242 | |
951bb646 | 5243 | env->log_type_id = btf->base_btf ? btf->start_id : 1; |
69b693f0 MKL |
5244 | while (cur < end) { |
5245 | struct btf_type *t = cur; | |
5246 | s32 meta_size; | |
5247 | ||
5248 | meta_size = btf_check_meta(env, t, end - cur); | |
5249 | if (meta_size < 0) | |
5250 | return meta_size; | |
5251 | ||
5252 | btf_add_type(env, t); | |
5253 | cur += meta_size; | |
5254 | env->log_type_id++; | |
5255 | } | |
5256 | ||
5257 | return 0; | |
5258 | } | |
5259 | ||
eb3f595d MKL |
5260 | static bool btf_resolve_valid(struct btf_verifier_env *env, |
5261 | const struct btf_type *t, | |
5262 | u32 type_id) | |
5263 | { | |
5264 | struct btf *btf = env->btf; | |
5265 | ||
5266 | if (!env_type_is_resolved(env, type_id)) | |
5267 | return false; | |
5268 | ||
1dc92851 | 5269 | if (btf_type_is_struct(t) || btf_type_is_datasec(t)) |
951bb646 AN |
5270 | return !btf_resolved_type_id(btf, type_id) && |
5271 | !btf_resolved_type_size(btf, type_id); | |
eb3f595d | 5272 | |
d7e7b42f | 5273 | if (btf_type_is_decl_tag(t) || btf_type_is_func(t)) |
b5ea834d YS |
5274 | return btf_resolved_type_id(btf, type_id) && |
5275 | !btf_resolved_type_size(btf, type_id); | |
5276 | ||
1dc92851 DB |
5277 | if (btf_type_is_modifier(t) || btf_type_is_ptr(t) || |
5278 | btf_type_is_var(t)) { | |
eb3f595d | 5279 | t = btf_type_id_resolve(btf, &type_id); |
1dc92851 DB |
5280 | return t && |
5281 | !btf_type_is_modifier(t) && | |
5282 | !btf_type_is_var(t) && | |
5283 | !btf_type_is_datasec(t); | |
eb3f595d MKL |
5284 | } |
5285 | ||
5286 | if (btf_type_is_array(t)) { | |
5287 | const struct btf_array *array = btf_type_array(t); | |
5288 | const struct btf_type *elem_type; | |
5289 | u32 elem_type_id = array->type; | |
5290 | u32 elem_size; | |
5291 | ||
5292 | elem_type = btf_type_id_size(btf, &elem_type_id, &elem_size); | |
5293 | return elem_type && !btf_type_is_modifier(elem_type) && | |
5294 | (array->nelems * elem_size == | |
951bb646 | 5295 | btf_resolved_type_size(btf, type_id)); |
eb3f595d MKL |
5296 | } |
5297 | ||
5298 | return false; | |
5299 | } | |
5300 | ||
2667a262 MKL |
5301 | static int btf_resolve(struct btf_verifier_env *env, |
5302 | const struct btf_type *t, u32 type_id) | |
5303 | { | |
5304 | u32 save_log_type_id = env->log_type_id; | |
5305 | const struct resolve_vertex *v; | |
5306 | int err = 0; | |
5307 | ||
5308 | env->resolve_mode = RESOLVE_TBD; | |
5309 | env_stack_push(env, t, type_id); | |
5310 | while (!err && (v = env_stack_peak(env))) { | |
5311 | env->log_type_id = v->type_id; | |
5312 | err = btf_type_ops(v->t)->resolve(env, v); | |
5313 | } | |
5314 | ||
5315 | env->log_type_id = type_id; | |
5316 | if (err == -E2BIG) { | |
5317 | btf_verifier_log_type(env, t, | |
5318 | "Exceeded max resolving depth:%u", | |
5319 | MAX_RESOLVE_DEPTH); | |
5320 | } else if (err == -EEXIST) { | |
5321 | btf_verifier_log_type(env, t, "Loop detected"); | |
5322 | } | |
5323 | ||
5324 | /* Final sanity check */ | |
5325 | if (!err && !btf_resolve_valid(env, t, type_id)) { | |
5326 | btf_verifier_log_type(env, t, "Invalid resolve state"); | |
5327 | err = -EINVAL; | |
5328 | } | |
5329 | ||
5330 | env->log_type_id = save_log_type_id; | |
5331 | return err; | |
5332 | } | |
5333 | ||
eb3f595d MKL |
5334 | static int btf_check_all_types(struct btf_verifier_env *env) |
5335 | { | |
5336 | struct btf *btf = env->btf; | |
951bb646 AN |
5337 | const struct btf_type *t; |
5338 | u32 type_id, i; | |
eb3f595d MKL |
5339 | int err; |
5340 | ||
5341 | err = env_resolve_init(env); | |
5342 | if (err) | |
5343 | return err; | |
5344 | ||
5345 | env->phase++; | |
951bb646 AN |
5346 | for (i = btf->base_btf ? 0 : 1; i < btf->nr_types; i++) { |
5347 | type_id = btf->start_id + i; | |
5348 | t = btf_type_by_id(btf, type_id); | |
eb3f595d MKL |
5349 | |
5350 | env->log_type_id = type_id; | |
5351 | if (btf_type_needs_resolve(t) && | |
5352 | !env_type_is_resolved(env, type_id)) { | |
5353 | err = btf_resolve(env, t, type_id); | |
5354 | if (err) | |
5355 | return err; | |
5356 | } | |
5357 | ||
2667a262 MKL |
5358 | if (btf_type_is_func_proto(t)) { |
5359 | err = btf_func_proto_check(env, t); | |
5360 | if (err) | |
5361 | return err; | |
5362 | } | |
eb3f595d MKL |
5363 | } |
5364 | ||
5365 | return 0; | |
5366 | } | |
5367 | ||
69b693f0 MKL |
5368 | static int btf_parse_type_sec(struct btf_verifier_env *env) |
5369 | { | |
f80442a4 | 5370 | const struct btf_header *hdr = &env->btf->hdr; |
eb3f595d MKL |
5371 | int err; |
5372 | ||
f80442a4 MKL |
5373 | /* Type section must align to 4 bytes */ |
5374 | if (hdr->type_off & (sizeof(u32) - 1)) { | |
5375 | btf_verifier_log(env, "Unaligned type_off"); | |
5376 | return -EINVAL; | |
5377 | } | |
5378 | ||
951bb646 | 5379 | if (!env->btf->base_btf && !hdr->type_len) { |
f80442a4 MKL |
5380 | btf_verifier_log(env, "No type found"); |
5381 | return -EINVAL; | |
5382 | } | |
5383 | ||
eb3f595d MKL |
5384 | err = btf_check_all_metas(env); |
5385 | if (err) | |
5386 | return err; | |
5387 | ||
5388 | return btf_check_all_types(env); | |
69b693f0 MKL |
5389 | } |
5390 | ||
5391 | static int btf_parse_str_sec(struct btf_verifier_env *env) | |
5392 | { | |
5393 | const struct btf_header *hdr; | |
5394 | struct btf *btf = env->btf; | |
5395 | const char *start, *end; | |
5396 | ||
f80442a4 | 5397 | hdr = &btf->hdr; |
69b693f0 MKL |
5398 | start = btf->nohdr_data + hdr->str_off; |
5399 | end = start + hdr->str_len; | |
5400 | ||
f80442a4 MKL |
5401 | if (end != btf->data + btf->data_size) { |
5402 | btf_verifier_log(env, "String section is not at the end"); | |
5403 | return -EINVAL; | |
5404 | } | |
5405 | ||
951bb646 AN |
5406 | btf->strings = start; |
5407 | ||
5408 | if (btf->base_btf && !hdr->str_len) | |
5409 | return 0; | |
5410 | if (!hdr->str_len || hdr->str_len - 1 > BTF_MAX_NAME_OFFSET || end[-1]) { | |
5411 | btf_verifier_log(env, "Invalid string section"); | |
5412 | return -EINVAL; | |
5413 | } | |
5414 | if (!btf->base_btf && start[0]) { | |
69b693f0 MKL |
5415 | btf_verifier_log(env, "Invalid string section"); |
5416 | return -EINVAL; | |
5417 | } | |
69b693f0 MKL |
5418 | |
5419 | return 0; | |
5420 | } | |
5421 | ||
f80442a4 MKL |
5422 | static const size_t btf_sec_info_offset[] = { |
5423 | offsetof(struct btf_header, type_off), | |
5424 | offsetof(struct btf_header, str_off), | |
5425 | }; | |
5426 | ||
5427 | static int btf_sec_info_cmp(const void *a, const void *b) | |
69b693f0 | 5428 | { |
f80442a4 MKL |
5429 | const struct btf_sec_info *x = a; |
5430 | const struct btf_sec_info *y = b; | |
5431 | ||
5432 | return (int)(x->off - y->off) ? : (int)(x->len - y->len); | |
5433 | } | |
5434 | ||
5435 | static int btf_check_sec_info(struct btf_verifier_env *env, | |
5436 | u32 btf_data_size) | |
5437 | { | |
a2889a4c | 5438 | struct btf_sec_info secs[ARRAY_SIZE(btf_sec_info_offset)]; |
f80442a4 | 5439 | u32 total, expected_total, i; |
69b693f0 | 5440 | const struct btf_header *hdr; |
f80442a4 MKL |
5441 | const struct btf *btf; |
5442 | ||
5443 | btf = env->btf; | |
5444 | hdr = &btf->hdr; | |
5445 | ||
5446 | /* Populate the secs from hdr */ | |
a2889a4c | 5447 | for (i = 0; i < ARRAY_SIZE(btf_sec_info_offset); i++) |
f80442a4 MKL |
5448 | secs[i] = *(struct btf_sec_info *)((void *)hdr + |
5449 | btf_sec_info_offset[i]); | |
5450 | ||
a2889a4c MKL |
5451 | sort(secs, ARRAY_SIZE(btf_sec_info_offset), |
5452 | sizeof(struct btf_sec_info), btf_sec_info_cmp, NULL); | |
f80442a4 MKL |
5453 | |
5454 | /* Check for gaps and overlap among sections */ | |
5455 | total = 0; | |
5456 | expected_total = btf_data_size - hdr->hdr_len; | |
a2889a4c | 5457 | for (i = 0; i < ARRAY_SIZE(btf_sec_info_offset); i++) { |
f80442a4 MKL |
5458 | if (expected_total < secs[i].off) { |
5459 | btf_verifier_log(env, "Invalid section offset"); | |
5460 | return -EINVAL; | |
5461 | } | |
5462 | if (total < secs[i].off) { | |
5463 | /* gap */ | |
5464 | btf_verifier_log(env, "Unsupported section found"); | |
5465 | return -EINVAL; | |
5466 | } | |
5467 | if (total > secs[i].off) { | |
5468 | btf_verifier_log(env, "Section overlap found"); | |
5469 | return -EINVAL; | |
5470 | } | |
5471 | if (expected_total - total < secs[i].len) { | |
5472 | btf_verifier_log(env, | |
5473 | "Total section length too long"); | |
5474 | return -EINVAL; | |
5475 | } | |
5476 | total += secs[i].len; | |
5477 | } | |
5478 | ||
5479 | /* There is data other than hdr and known sections */ | |
5480 | if (expected_total != total) { | |
5481 | btf_verifier_log(env, "Unsupported section found"); | |
5482 | return -EINVAL; | |
5483 | } | |
5484 | ||
5485 | return 0; | |
5486 | } | |
5487 | ||
4a6998af | 5488 | static int btf_parse_hdr(struct btf_verifier_env *env) |
f80442a4 | 5489 | { |
4a6998af | 5490 | u32 hdr_len, hdr_copy, btf_data_size; |
f80442a4 | 5491 | const struct btf_header *hdr; |
f80442a4 | 5492 | struct btf *btf; |
69b693f0 | 5493 | |
f80442a4 | 5494 | btf = env->btf; |
4a6998af | 5495 | btf_data_size = btf->data_size; |
f80442a4 | 5496 | |
583669ab | 5497 | if (btf_data_size < offsetofend(struct btf_header, hdr_len)) { |
f80442a4 MKL |
5498 | btf_verifier_log(env, "hdr_len not found"); |
5499 | return -EINVAL; | |
5500 | } | |
5501 | ||
4a6998af ML |
5502 | hdr = btf->data; |
5503 | hdr_len = hdr->hdr_len; | |
f80442a4 | 5504 | if (btf_data_size < hdr_len) { |
69b693f0 MKL |
5505 | btf_verifier_log(env, "btf_header not found"); |
5506 | return -EINVAL; | |
5507 | } | |
5508 | ||
4a6998af ML |
5509 | /* Ensure the unsupported header fields are zero */ |
5510 | if (hdr_len > sizeof(btf->hdr)) { | |
5511 | u8 *expected_zero = btf->data + sizeof(btf->hdr); | |
5512 | u8 *end = btf->data + hdr_len; | |
5513 | ||
5514 | for (; expected_zero < end; expected_zero++) { | |
5515 | if (*expected_zero) { | |
5516 | btf_verifier_log(env, "Unsupported btf_header"); | |
5517 | return -E2BIG; | |
5518 | } | |
5519 | } | |
f80442a4 MKL |
5520 | } |
5521 | ||
5522 | hdr_copy = min_t(u32, hdr_len, sizeof(btf->hdr)); | |
4a6998af | 5523 | memcpy(&btf->hdr, btf->data, hdr_copy); |
f80442a4 MKL |
5524 | |
5525 | hdr = &btf->hdr; | |
5526 | ||
5527 | btf_verifier_log_hdr(env, btf_data_size); | |
69b693f0 | 5528 | |
69b693f0 MKL |
5529 | if (hdr->magic != BTF_MAGIC) { |
5530 | btf_verifier_log(env, "Invalid magic"); | |
5531 | return -EINVAL; | |
5532 | } | |
5533 | ||
5534 | if (hdr->version != BTF_VERSION) { | |
5535 | btf_verifier_log(env, "Unsupported version"); | |
5536 | return -ENOTSUPP; | |
5537 | } | |
5538 | ||
5539 | if (hdr->flags) { | |
5540 | btf_verifier_log(env, "Unsupported flags"); | |
5541 | return -ENOTSUPP; | |
5542 | } | |
5543 | ||
bcc5e616 | 5544 | if (!btf->base_btf && btf_data_size == hdr->hdr_len) { |
69b693f0 MKL |
5545 | btf_verifier_log(env, "No data"); |
5546 | return -EINVAL; | |
5547 | } | |
5548 | ||
3a74904c | 5549 | return btf_check_sec_info(env, btf_data_size); |
69b693f0 MKL |
5550 | } |
5551 | ||
8ffa5cc1 KKD |
5552 | static const char *alloc_obj_fields[] = { |
5553 | "bpf_spin_lock", | |
5554 | "bpf_list_head", | |
5555 | "bpf_list_node", | |
9c395c1b DM |
5556 | "bpf_rb_root", |
5557 | "bpf_rb_node", | |
d54730b5 | 5558 | "bpf_refcount", |
8ffa5cc1 KKD |
5559 | }; |
5560 | ||
5561 | static struct btf_struct_metas * | |
5562 | btf_parse_struct_metas(struct bpf_verifier_log *log, struct btf *btf) | |
5563 | { | |
8ffa5cc1 | 5564 | struct btf_struct_metas *tab = NULL; |
7a851ecb | 5565 | struct btf_id_set *aof; |
8ffa5cc1 KKD |
5566 | int i, n, id, ret; |
5567 | ||
5568 | BUILD_BUG_ON(offsetof(struct btf_id_set, cnt) != 0); | |
5569 | BUILD_BUG_ON(sizeof(struct btf_id_set) != sizeof(u32)); | |
5570 | ||
7a851ecb DM |
5571 | aof = kmalloc(sizeof(*aof), GFP_KERNEL | __GFP_NOWARN); |
5572 | if (!aof) | |
5573 | return ERR_PTR(-ENOMEM); | |
5574 | aof->cnt = 0; | |
5575 | ||
8ffa5cc1 KKD |
5576 | for (i = 0; i < ARRAY_SIZE(alloc_obj_fields); i++) { |
5577 | /* Try to find whether this special type exists in user BTF, and | |
5578 | * if so remember its ID so we can easily find it among members | |
5579 | * of structs that we iterate in the next loop. | |
5580 | */ | |
7a851ecb DM |
5581 | struct btf_id_set *new_aof; |
5582 | ||
8ffa5cc1 KKD |
5583 | id = btf_find_by_name_kind(btf, alloc_obj_fields[i], BTF_KIND_STRUCT); |
5584 | if (id < 0) | |
5585 | continue; | |
7a851ecb | 5586 | |
41948afc | 5587 | new_aof = krealloc(aof, struct_size(new_aof, ids, aof->cnt + 1), |
7a851ecb DM |
5588 | GFP_KERNEL | __GFP_NOWARN); |
5589 | if (!new_aof) { | |
5590 | ret = -ENOMEM; | |
5591 | goto free_aof; | |
5592 | } | |
5593 | aof = new_aof; | |
5594 | aof->ids[aof->cnt++] = id; | |
8ffa5cc1 KKD |
5595 | } |
5596 | ||
7a851ecb DM |
5597 | n = btf_nr_types(btf); |
5598 | for (i = 1; i < n; i++) { | |
5599 | /* Try to find if there are kptrs in user BTF and remember their ID */ | |
5600 | struct btf_id_set *new_aof; | |
5601 | struct btf_field_info tmp; | |
5602 | const struct btf_type *t; | |
5603 | ||
5604 | t = btf_type_by_id(btf, i); | |
5605 | if (!t) { | |
5606 | ret = -EINVAL; | |
5607 | goto free_aof; | |
5608 | } | |
5609 | ||
1cb80d9e | 5610 | ret = btf_find_kptr(btf, t, 0, 0, &tmp, BPF_KPTR); |
7a851ecb DM |
5611 | if (ret != BTF_FIELD_FOUND) |
5612 | continue; | |
5613 | ||
41948afc | 5614 | new_aof = krealloc(aof, struct_size(new_aof, ids, aof->cnt + 1), |
7a851ecb DM |
5615 | GFP_KERNEL | __GFP_NOWARN); |
5616 | if (!new_aof) { | |
5617 | ret = -ENOMEM; | |
5618 | goto free_aof; | |
5619 | } | |
5620 | aof = new_aof; | |
5621 | aof->ids[aof->cnt++] = i; | |
8ffa5cc1 KKD |
5622 | } |
5623 | ||
986deb29 HT |
5624 | if (!aof->cnt) { |
5625 | kfree(aof); | |
8ffa5cc1 | 5626 | return NULL; |
986deb29 | 5627 | } |
7a851ecb | 5628 | sort(&aof->ids, aof->cnt, sizeof(aof->ids[0]), btf_id_cmp_func, NULL); |
8ffa5cc1 | 5629 | |
8ffa5cc1 KKD |
5630 | for (i = 1; i < n; i++) { |
5631 | struct btf_struct_metas *new_tab; | |
5632 | const struct btf_member *member; | |
8ffa5cc1 KKD |
5633 | struct btf_struct_meta *type; |
5634 | struct btf_record *record; | |
5635 | const struct btf_type *t; | |
5636 | int j, tab_cnt; | |
5637 | ||
5638 | t = btf_type_by_id(btf, i); | |
8ffa5cc1 KKD |
5639 | if (!__btf_type_is_struct(t)) |
5640 | continue; | |
5641 | ||
5642 | cond_resched(); | |
5643 | ||
5644 | for_each_member(j, t, member) { | |
7a851ecb | 5645 | if (btf_id_set_contains(aof, member->type)) |
8ffa5cc1 KKD |
5646 | goto parse; |
5647 | } | |
5648 | continue; | |
5649 | parse: | |
5650 | tab_cnt = tab ? tab->cnt : 0; | |
41948afc | 5651 | new_tab = krealloc(tab, struct_size(new_tab, types, tab_cnt + 1), |
8ffa5cc1 KKD |
5652 | GFP_KERNEL | __GFP_NOWARN); |
5653 | if (!new_tab) { | |
5654 | ret = -ENOMEM; | |
5655 | goto free; | |
5656 | } | |
5657 | if (!tab) | |
5658 | new_tab->cnt = 0; | |
5659 | tab = new_tab; | |
5660 | ||
5661 | type = &tab->types[tab->cnt]; | |
5662 | type->btf_id = i; | |
0de20461 | 5663 | record = btf_parse_fields(btf, t, BPF_SPIN_LOCK | BPF_RES_SPIN_LOCK | BPF_LIST_HEAD | BPF_LIST_NODE | |
7a851ecb DM |
5664 | BPF_RB_ROOT | BPF_RB_NODE | BPF_REFCOUNT | |
5665 | BPF_KPTR, t->size); | |
8ffa5cc1 KKD |
5666 | /* The record cannot be unset, treat it as an error if so */ |
5667 | if (IS_ERR_OR_NULL(record)) { | |
5668 | ret = PTR_ERR_OR_ZERO(record) ?: -EFAULT; | |
5669 | goto free; | |
5670 | } | |
8ffa5cc1 | 5671 | type->record = record; |
8ffa5cc1 KKD |
5672 | tab->cnt++; |
5673 | } | |
7a851ecb | 5674 | kfree(aof); |
8ffa5cc1 KKD |
5675 | return tab; |
5676 | free: | |
5677 | btf_struct_metas_free(tab); | |
7a851ecb DM |
5678 | free_aof: |
5679 | kfree(aof); | |
8ffa5cc1 KKD |
5680 | return ERR_PTR(ret); |
5681 | } | |
5682 | ||
5683 | struct btf_struct_meta *btf_find_struct_meta(const struct btf *btf, u32 btf_id) | |
5684 | { | |
5685 | struct btf_struct_metas *tab; | |
5686 | ||
5687 | BUILD_BUG_ON(offsetof(struct btf_struct_meta, btf_id) != 0); | |
5688 | tab = btf->struct_meta_tab; | |
5689 | if (!tab) | |
5690 | return NULL; | |
5691 | return bsearch(&btf_id, tab->types, tab->cnt, sizeof(tab->types[0]), btf_id_cmp_func); | |
5692 | } | |
5693 | ||
eb596b09 KKD |
5694 | static int btf_check_type_tags(struct btf_verifier_env *env, |
5695 | struct btf *btf, int start_id) | |
5696 | { | |
5697 | int i, n, good_id = start_id - 1; | |
5698 | bool in_tags; | |
5699 | ||
5700 | n = btf_nr_types(btf); | |
5701 | for (i = start_id; i < n; i++) { | |
5702 | const struct btf_type *t; | |
d1a374a1 | 5703 | int chain_limit = 32; |
eb596b09 KKD |
5704 | u32 cur_id = i; |
5705 | ||
5706 | t = btf_type_by_id(btf, i); | |
5707 | if (!t) | |
5708 | return -EINVAL; | |
5709 | if (!btf_type_is_modifier(t)) | |
5710 | continue; | |
5711 | ||
5712 | cond_resched(); | |
5713 | ||
5714 | in_tags = btf_type_is_type_tag(t); | |
5715 | while (btf_type_is_modifier(t)) { | |
d1a374a1 KKD |
5716 | if (!chain_limit--) { |
5717 | btf_verifier_log(env, "Max chain length or cycle detected"); | |
5718 | return -ELOOP; | |
5719 | } | |
eb596b09 KKD |
5720 | if (btf_type_is_type_tag(t)) { |
5721 | if (!in_tags) { | |
5722 | btf_verifier_log(env, "Type tags don't precede modifiers"); | |
5723 | return -EINVAL; | |
5724 | } | |
5725 | } else if (in_tags) { | |
5726 | in_tags = false; | |
5727 | } | |
5728 | if (cur_id <= good_id) | |
5729 | break; | |
5730 | /* Move to next type */ | |
5731 | cur_id = t->type; | |
5732 | t = btf_type_by_id(btf, cur_id); | |
5733 | if (!t) | |
5734 | return -EINVAL; | |
5735 | } | |
5736 | good_id = i; | |
5737 | } | |
5738 | return 0; | |
5739 | } | |
5740 | ||
bdcab414 | 5741 | static int finalize_log(struct bpf_verifier_log *log, bpfptr_t uattr, u32 uattr_size) |
69b693f0 | 5742 | { |
bdcab414 AN |
5743 | u32 log_true_size; |
5744 | int err; | |
5745 | ||
5746 | err = bpf_vlog_finalize(log, &log_true_size); | |
5747 | ||
5748 | if (uattr_size >= offsetofend(union bpf_attr, btf_log_true_size) && | |
5749 | copy_to_bpfptr_offset(uattr, offsetof(union bpf_attr, btf_log_true_size), | |
5750 | &log_true_size, sizeof(log_true_size))) | |
5751 | err = -EFAULT; | |
5752 | ||
5753 | return err; | |
5754 | } | |
5755 | ||
47a71c1f | 5756 | static struct btf *btf_parse(const union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size) |
69b693f0 | 5757 | { |
47a71c1f AN |
5758 | bpfptr_t btf_data = make_bpfptr(attr->btf, uattr.is_kernel); |
5759 | char __user *log_ubuf = u64_to_user_ptr(attr->btf_log_buf); | |
8ffa5cc1 | 5760 | struct btf_struct_metas *struct_meta_tab; |
69b693f0 | 5761 | struct btf_verifier_env *env = NULL; |
69b693f0 MKL |
5762 | struct btf *btf = NULL; |
5763 | u8 *data; | |
bdcab414 | 5764 | int err, ret; |
69b693f0 | 5765 | |
47a71c1f | 5766 | if (attr->btf_size > BTF_MAX_SIZE) |
69b693f0 MKL |
5767 | return ERR_PTR(-E2BIG); |
5768 | ||
5769 | env = kzalloc(sizeof(*env), GFP_KERNEL | __GFP_NOWARN); | |
5770 | if (!env) | |
5771 | return ERR_PTR(-ENOMEM); | |
5772 | ||
bdcab414 AN |
5773 | /* user could have requested verbose verifier output |
5774 | * and supplied buffer to store the verification trace | |
5775 | */ | |
5776 | err = bpf_vlog_init(&env->log, attr->btf_log_level, | |
5777 | log_ubuf, attr->btf_log_size); | |
5778 | if (err) | |
5779 | goto errout_free; | |
69b693f0 MKL |
5780 | |
5781 | btf = kzalloc(sizeof(*btf), GFP_KERNEL | __GFP_NOWARN); | |
5782 | if (!btf) { | |
5783 | err = -ENOMEM; | |
5784 | goto errout; | |
5785 | } | |
f80442a4 MKL |
5786 | env->btf = btf; |
5787 | ||
47a71c1f | 5788 | data = kvmalloc(attr->btf_size, GFP_KERNEL | __GFP_NOWARN); |
69b693f0 MKL |
5789 | if (!data) { |
5790 | err = -ENOMEM; | |
5791 | goto errout; | |
5792 | } | |
5793 | ||
5794 | btf->data = data; | |
47a71c1f | 5795 | btf->data_size = attr->btf_size; |
69b693f0 | 5796 | |
47a71c1f | 5797 | if (copy_from_bpfptr(data, btf_data, attr->btf_size)) { |
69b693f0 MKL |
5798 | err = -EFAULT; |
5799 | goto errout; | |
5800 | } | |
5801 | ||
4a6998af ML |
5802 | err = btf_parse_hdr(env); |
5803 | if (err) | |
5804 | goto errout; | |
5805 | ||
5806 | btf->nohdr_data = btf->data + btf->hdr.hdr_len; | |
5807 | ||
69b693f0 MKL |
5808 | err = btf_parse_str_sec(env); |
5809 | if (err) | |
5810 | goto errout; | |
5811 | ||
5812 | err = btf_parse_type_sec(env); | |
5813 | if (err) | |
5814 | goto errout; | |
5815 | ||
eb596b09 KKD |
5816 | err = btf_check_type_tags(env, btf, 1); |
5817 | if (err) | |
5818 | goto errout; | |
5819 | ||
bdcab414 | 5820 | struct_meta_tab = btf_parse_struct_metas(&env->log, btf); |
8ffa5cc1 KKD |
5821 | if (IS_ERR(struct_meta_tab)) { |
5822 | err = PTR_ERR(struct_meta_tab); | |
5823 | goto errout; | |
5824 | } | |
5825 | btf->struct_meta_tab = struct_meta_tab; | |
5826 | ||
865ce09a KKD |
5827 | if (struct_meta_tab) { |
5828 | int i; | |
5829 | ||
5830 | for (i = 0; i < struct_meta_tab->cnt; i++) { | |
5831 | err = btf_check_and_fixup_fields(btf, struct_meta_tab->types[i].record); | |
5832 | if (err < 0) | |
5833 | goto errout_meta; | |
5834 | } | |
5835 | } | |
5836 | ||
bdcab414 AN |
5837 | err = finalize_log(&env->log, uattr, uattr_size); |
5838 | if (err) | |
5839 | goto errout_free; | |
69b693f0 | 5840 | |
f80442a4 MKL |
5841 | btf_verifier_env_free(env); |
5842 | refcount_set(&btf->refcnt, 1); | |
5843 | return btf; | |
69b693f0 | 5844 | |
8ffa5cc1 KKD |
5845 | errout_meta: |
5846 | btf_free_struct_meta_tab(btf); | |
69b693f0 | 5847 | errout: |
bdcab414 AN |
5848 | /* overwrite err with -ENOSPC or -EFAULT */ |
5849 | ret = finalize_log(&env->log, uattr, uattr_size); | |
5850 | if (ret) | |
5851 | err = ret; | |
5852 | errout_free: | |
69b693f0 MKL |
5853 | btf_verifier_env_free(env); |
5854 | if (btf) | |
5855 | btf_free(btf); | |
5856 | return ERR_PTR(err); | |
5857 | } | |
b00b8dae | 5858 | |
fc5eb4a8 AB |
5859 | extern char __start_BTF[]; |
5860 | extern char __stop_BTF[]; | |
91cc1a99 AS |
5861 | extern struct btf *btf_vmlinux; |
5862 | ||
5863 | #define BPF_MAP_TYPE(_id, _ops) | |
f2e10bff | 5864 | #define BPF_LINK_TYPE(_id, _name) |
91cc1a99 AS |
5865 | static union { |
5866 | struct bpf_ctx_convert { | |
5867 | #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \ | |
5868 | prog_ctx_type _id##_prog; \ | |
5869 | kern_ctx_type _id##_kern; | |
5870 | #include <linux/bpf_types.h> | |
5871 | #undef BPF_PROG_TYPE | |
5872 | } *__t; | |
5873 | /* 't' is written once under lock. Read many times. */ | |
5874 | const struct btf_type *t; | |
5875 | } bpf_ctx_convert; | |
5876 | enum { | |
5877 | #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \ | |
5878 | __ctx_convert##_id, | |
5879 | #include <linux/bpf_types.h> | |
5880 | #undef BPF_PROG_TYPE | |
ce27709b | 5881 | __ctx_convert_unused, /* to avoid empty enum in extreme .config */ |
91cc1a99 AS |
5882 | }; |
5883 | static u8 bpf_ctx_convert_map[] = { | |
5884 | #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \ | |
5885 | [_id] = __ctx_convert##_id, | |
5886 | #include <linux/bpf_types.h> | |
5887 | #undef BPF_PROG_TYPE | |
4c80c7bc | 5888 | 0, /* avoid empty array */ |
91cc1a99 AS |
5889 | }; |
5890 | #undef BPF_MAP_TYPE | |
f2e10bff | 5891 | #undef BPF_LINK_TYPE |
91cc1a99 | 5892 | |
66967a32 | 5893 | static const struct btf_type *find_canonical_prog_ctx_type(enum bpf_prog_type prog_type) |
91cc1a99 AS |
5894 | { |
5895 | const struct btf_type *conv_struct; | |
91cc1a99 | 5896 | const struct btf_member *ctx_type; |
91cc1a99 AS |
5897 | |
5898 | conv_struct = bpf_ctx_convert.t; | |
66967a32 | 5899 | if (!conv_struct) |
91cc1a99 | 5900 | return NULL; |
66967a32 AN |
5901 | /* prog_type is valid bpf program type. No need for bounds check. */ |
5902 | ctx_type = btf_type_member(conv_struct) + bpf_ctx_convert_map[prog_type] * 2; | |
5903 | /* ctx_type is a pointer to prog_ctx_type in vmlinux. | |
5904 | * Like 'struct __sk_buff' | |
5905 | */ | |
5906 | return btf_type_by_id(btf_vmlinux, ctx_type->type); | |
5907 | } | |
5908 | ||
5909 | static int find_kern_ctx_type_id(enum bpf_prog_type prog_type) | |
5910 | { | |
5911 | const struct btf_type *conv_struct; | |
5912 | const struct btf_member *ctx_type; | |
5913 | ||
5914 | conv_struct = bpf_ctx_convert.t; | |
5915 | if (!conv_struct) | |
5916 | return -EFAULT; | |
5917 | /* prog_type is valid bpf program type. No need for bounds check. */ | |
5918 | ctx_type = btf_type_member(conv_struct) + bpf_ctx_convert_map[prog_type] * 2 + 1; | |
5919 | /* ctx_type is a pointer to prog_ctx_type in vmlinux. | |
5920 | * Like 'struct sk_buff' | |
5921 | */ | |
5922 | return ctx_type->type; | |
5923 | } | |
5924 | ||
ec209ad8 DX |
5925 | bool btf_is_projection_of(const char *pname, const char *tname) |
5926 | { | |
5927 | if (strcmp(pname, "__sk_buff") == 0 && strcmp(tname, "sk_buff") == 0) | |
5928 | return true; | |
5929 | if (strcmp(pname, "xdp_md") == 0 && strcmp(tname, "xdp_buff") == 0) | |
5930 | return true; | |
5931 | return false; | |
5932 | } | |
5933 | ||
fb5b86cf AN |
5934 | bool btf_is_prog_ctx_type(struct bpf_verifier_log *log, const struct btf *btf, |
5935 | const struct btf_type *t, enum bpf_prog_type prog_type, | |
5936 | int arg) | |
66967a32 AN |
5937 | { |
5938 | const struct btf_type *ctx_type; | |
5939 | const char *tname, *ctx_tname; | |
5940 | ||
91cc1a99 | 5941 | t = btf_type_by_id(btf, t->type); |
824c58fb AN |
5942 | |
5943 | /* KPROBE programs allow bpf_user_pt_regs_t typedef, which we need to | |
5944 | * check before we skip all the typedef below. | |
5945 | */ | |
5946 | if (prog_type == BPF_PROG_TYPE_KPROBE) { | |
5947 | while (btf_type_is_modifier(t) && !btf_type_is_typedef(t)) | |
5948 | t = btf_type_by_id(btf, t->type); | |
5949 | ||
5950 | if (btf_type_is_typedef(t)) { | |
5951 | tname = btf_name_by_offset(btf, t->name_off); | |
5952 | if (tname && strcmp(tname, "bpf_user_pt_regs_t") == 0) | |
5953 | return true; | |
5954 | } | |
5955 | } | |
5956 | ||
91cc1a99 AS |
5957 | while (btf_type_is_modifier(t)) |
5958 | t = btf_type_by_id(btf, t->type); | |
5959 | if (!btf_type_is_struct(t)) { | |
5960 | /* Only pointer to struct is supported for now. | |
5961 | * That means that BPF_PROG_TYPE_TRACEPOINT with BTF | |
5962 | * is not supported yet. | |
5963 | * BPF_PROG_TYPE_RAW_TRACEPOINT is fine. | |
5964 | */ | |
fb5b86cf | 5965 | return false; |
91cc1a99 AS |
5966 | } |
5967 | tname = btf_name_by_offset(btf, t->name_off); | |
5968 | if (!tname) { | |
51c39bb1 | 5969 | bpf_log(log, "arg#%d struct doesn't have a name\n", arg); |
fb5b86cf | 5970 | return false; |
91cc1a99 | 5971 | } |
66967a32 AN |
5972 | |
5973 | ctx_type = find_canonical_prog_ctx_type(prog_type); | |
5974 | if (!ctx_type) { | |
5975 | bpf_log(log, "btf_vmlinux is malformed\n"); | |
91cc1a99 | 5976 | /* should not happen */ |
fb5b86cf | 5977 | return false; |
66967a32 | 5978 | } |
d384dce2 | 5979 | again: |
66967a32 | 5980 | ctx_tname = btf_name_by_offset(btf_vmlinux, ctx_type->name_off); |
91cc1a99 AS |
5981 | if (!ctx_tname) { |
5982 | /* should not happen */ | |
5983 | bpf_log(log, "Please fix kernel include/linux/bpf_types.h\n"); | |
fb5b86cf | 5984 | return false; |
91cc1a99 | 5985 | } |
879bbe7a AN |
5986 | /* program types without named context types work only with arg:ctx tag */ |
5987 | if (ctx_tname[0] == '\0') | |
5988 | return false; | |
91cc1a99 AS |
5989 | /* only compare that prog's ctx type name is the same as |
5990 | * kernel expects. No need to compare field by field. | |
5991 | * It's ok for bpf prog to do: | |
5992 | * struct __sk_buff {}; | |
5993 | * int socket_filter_bpf_prog(struct __sk_buff *skb) | |
5994 | * { // no fields of skb are ever used } | |
5995 | */ | |
ec209ad8 | 5996 | if (btf_is_projection_of(ctx_tname, tname)) |
fb5b86cf | 5997 | return true; |
d384dce2 AN |
5998 | if (strcmp(ctx_tname, tname)) { |
5999 | /* bpf_user_pt_regs_t is a typedef, so resolve it to | |
6000 | * underlying struct and check name again | |
6001 | */ | |
66967a32 | 6002 | if (!btf_type_is_modifier(ctx_type)) |
fb5b86cf | 6003 | return false; |
66967a32 AN |
6004 | while (btf_type_is_modifier(ctx_type)) |
6005 | ctx_type = btf_type_by_id(btf_vmlinux, ctx_type->type); | |
d384dce2 AN |
6006 | goto again; |
6007 | } | |
fb5b86cf | 6008 | return true; |
91cc1a99 | 6009 | } |
8580ac94 | 6010 | |
0ba97151 AN |
6011 | /* forward declarations for arch-specific underlying types of |
6012 | * bpf_user_pt_regs_t; this avoids the need for arch-specific #ifdef | |
6013 | * compilation guards below for BPF_PROG_TYPE_PERF_EVENT checks, but still | |
6014 | * works correctly with __builtin_types_compatible_p() on respective | |
6015 | * architectures | |
6016 | */ | |
6017 | struct user_regs_struct; | |
6018 | struct user_pt_regs; | |
6019 | ||
6020 | static int btf_validate_prog_ctx_type(struct bpf_verifier_log *log, const struct btf *btf, | |
6021 | const struct btf_type *t, int arg, | |
6022 | enum bpf_prog_type prog_type, | |
6023 | enum bpf_attach_type attach_type) | |
6024 | { | |
6025 | const struct btf_type *ctx_type; | |
6026 | const char *tname, *ctx_tname; | |
6027 | ||
6028 | if (!btf_is_ptr(t)) { | |
6029 | bpf_log(log, "arg#%d type isn't a pointer\n", arg); | |
6030 | return -EINVAL; | |
6031 | } | |
6032 | t = btf_type_by_id(btf, t->type); | |
6033 | ||
6034 | /* KPROBE and PERF_EVENT programs allow bpf_user_pt_regs_t typedef */ | |
6035 | if (prog_type == BPF_PROG_TYPE_KPROBE || prog_type == BPF_PROG_TYPE_PERF_EVENT) { | |
6036 | while (btf_type_is_modifier(t) && !btf_type_is_typedef(t)) | |
6037 | t = btf_type_by_id(btf, t->type); | |
6038 | ||
6039 | if (btf_type_is_typedef(t)) { | |
6040 | tname = btf_name_by_offset(btf, t->name_off); | |
6041 | if (tname && strcmp(tname, "bpf_user_pt_regs_t") == 0) | |
6042 | return 0; | |
6043 | } | |
6044 | } | |
6045 | ||
6046 | /* all other program types don't use typedefs for context type */ | |
6047 | while (btf_type_is_modifier(t)) | |
6048 | t = btf_type_by_id(btf, t->type); | |
6049 | ||
6050 | /* `void *ctx __arg_ctx` is always valid */ | |
6051 | if (btf_type_is_void(t)) | |
6052 | return 0; | |
6053 | ||
6054 | tname = btf_name_by_offset(btf, t->name_off); | |
6055 | if (str_is_empty(tname)) { | |
6056 | bpf_log(log, "arg#%d type doesn't have a name\n", arg); | |
6057 | return -EINVAL; | |
6058 | } | |
6059 | ||
6060 | /* special cases */ | |
6061 | switch (prog_type) { | |
6062 | case BPF_PROG_TYPE_KPROBE: | |
6063 | if (__btf_type_is_struct(t) && strcmp(tname, "pt_regs") == 0) | |
6064 | return 0; | |
6065 | break; | |
6066 | case BPF_PROG_TYPE_PERF_EVENT: | |
6067 | if (__builtin_types_compatible_p(bpf_user_pt_regs_t, struct pt_regs) && | |
6068 | __btf_type_is_struct(t) && strcmp(tname, "pt_regs") == 0) | |
6069 | return 0; | |
6070 | if (__builtin_types_compatible_p(bpf_user_pt_regs_t, struct user_pt_regs) && | |
6071 | __btf_type_is_struct(t) && strcmp(tname, "user_pt_regs") == 0) | |
6072 | return 0; | |
6073 | if (__builtin_types_compatible_p(bpf_user_pt_regs_t, struct user_regs_struct) && | |
6074 | __btf_type_is_struct(t) && strcmp(tname, "user_regs_struct") == 0) | |
6075 | return 0; | |
6076 | break; | |
6077 | case BPF_PROG_TYPE_RAW_TRACEPOINT: | |
6078 | case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE: | |
6079 | /* allow u64* as ctx */ | |
6080 | if (btf_is_int(t) && t->size == 8) | |
6081 | return 0; | |
6082 | break; | |
6083 | case BPF_PROG_TYPE_TRACING: | |
6084 | switch (attach_type) { | |
6085 | case BPF_TRACE_RAW_TP: | |
6086 | /* tp_btf program is TRACING, so need special case here */ | |
6087 | if (__btf_type_is_struct(t) && | |
6088 | strcmp(tname, "bpf_raw_tracepoint_args") == 0) | |
6089 | return 0; | |
6090 | /* allow u64* as ctx */ | |
6091 | if (btf_is_int(t) && t->size == 8) | |
6092 | return 0; | |
6093 | break; | |
6094 | case BPF_TRACE_ITER: | |
6095 | /* allow struct bpf_iter__xxx types only */ | |
6096 | if (__btf_type_is_struct(t) && | |
6097 | strncmp(tname, "bpf_iter__", sizeof("bpf_iter__") - 1) == 0) | |
6098 | return 0; | |
6099 | break; | |
6100 | case BPF_TRACE_FENTRY: | |
6101 | case BPF_TRACE_FEXIT: | |
6102 | case BPF_MODIFY_RETURN: | |
6103 | /* allow u64* as ctx */ | |
6104 | if (btf_is_int(t) && t->size == 8) | |
6105 | return 0; | |
6106 | break; | |
6107 | default: | |
6108 | break; | |
6109 | } | |
6110 | break; | |
6111 | case BPF_PROG_TYPE_LSM: | |
6112 | case BPF_PROG_TYPE_STRUCT_OPS: | |
6113 | /* allow u64* as ctx */ | |
6114 | if (btf_is_int(t) && t->size == 8) | |
6115 | return 0; | |
6116 | break; | |
6117 | case BPF_PROG_TYPE_TRACEPOINT: | |
6118 | case BPF_PROG_TYPE_SYSCALL: | |
6119 | case BPF_PROG_TYPE_EXT: | |
6120 | return 0; /* anything goes */ | |
6121 | default: | |
6122 | break; | |
6123 | } | |
6124 | ||
6125 | ctx_type = find_canonical_prog_ctx_type(prog_type); | |
6126 | if (!ctx_type) { | |
6127 | /* should not happen */ | |
6128 | bpf_log(log, "btf_vmlinux is malformed\n"); | |
6129 | return -EINVAL; | |
6130 | } | |
6131 | ||
6132 | /* resolve typedefs and check that underlying structs are matching as well */ | |
6133 | while (btf_type_is_modifier(ctx_type)) | |
6134 | ctx_type = btf_type_by_id(btf_vmlinux, ctx_type->type); | |
6135 | ||
6136 | /* if program type doesn't have distinctly named struct type for | |
6137 | * context, then __arg_ctx argument can only be `void *`, which we | |
6138 | * already checked above | |
6139 | */ | |
6140 | if (!__btf_type_is_struct(ctx_type)) { | |
6141 | bpf_log(log, "arg#%d should be void pointer\n", arg); | |
6142 | return -EINVAL; | |
6143 | } | |
6144 | ||
6145 | ctx_tname = btf_name_by_offset(btf_vmlinux, ctx_type->name_off); | |
6146 | if (!__btf_type_is_struct(t) || strcmp(ctx_tname, tname) != 0) { | |
6147 | bpf_log(log, "arg#%d should be `struct %s *`\n", arg, ctx_tname); | |
6148 | return -EINVAL; | |
6149 | } | |
6150 | ||
6151 | return 0; | |
6152 | } | |
6153 | ||
5b92a28a AS |
6154 | static int btf_translate_to_vmlinux(struct bpf_verifier_log *log, |
6155 | struct btf *btf, | |
6156 | const struct btf_type *t, | |
51c39bb1 AS |
6157 | enum bpf_prog_type prog_type, |
6158 | int arg) | |
5b92a28a | 6159 | { |
fb5b86cf | 6160 | if (!btf_is_prog_ctx_type(log, btf, t, prog_type, arg)) |
5b92a28a | 6161 | return -ENOENT; |
66967a32 | 6162 | return find_kern_ctx_type_id(prog_type); |
5b92a28a AS |
6163 | } |
6164 | ||
fd264ca0 YS |
6165 | int get_kern_ctx_btf_id(struct bpf_verifier_log *log, enum bpf_prog_type prog_type) |
6166 | { | |
6167 | const struct btf_member *kctx_member; | |
6168 | const struct btf_type *conv_struct; | |
6169 | const struct btf_type *kctx_type; | |
6170 | u32 kctx_type_id; | |
6171 | ||
6172 | conv_struct = bpf_ctx_convert.t; | |
6173 | /* get member for kernel ctx type */ | |
6174 | kctx_member = btf_type_member(conv_struct) + bpf_ctx_convert_map[prog_type] * 2 + 1; | |
6175 | kctx_type_id = kctx_member->type; | |
6176 | kctx_type = btf_type_by_id(btf_vmlinux, kctx_type_id); | |
6177 | if (!btf_type_is_struct(kctx_type)) { | |
6178 | bpf_log(log, "kern ctx type id %u is not a struct\n", kctx_type_id); | |
6179 | return -EINVAL; | |
6180 | } | |
6181 | ||
6182 | return kctx_type_id; | |
6183 | } | |
6184 | ||
49f4e672 JO |
6185 | BTF_ID_LIST(bpf_ctx_convert_btf_id) |
6186 | BTF_ID(struct, bpf_ctx_convert) | |
6187 | ||
8646db23 AM |
6188 | static struct btf *btf_parse_base(struct btf_verifier_env *env, const char *name, |
6189 | void *data, unsigned int data_size) | |
8580ac94 | 6190 | { |
8580ac94 | 6191 | struct btf *btf = NULL; |
49f4e672 | 6192 | int err; |
8580ac94 | 6193 | |
fc5eb4a8 AB |
6194 | if (!IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) |
6195 | return ERR_PTR(-ENOENT); | |
6196 | ||
8580ac94 AS |
6197 | btf = kzalloc(sizeof(*btf), GFP_KERNEL | __GFP_NOWARN); |
6198 | if (!btf) { | |
6199 | err = -ENOMEM; | |
6200 | goto errout; | |
6201 | } | |
6202 | env->btf = btf; | |
6203 | ||
8646db23 AM |
6204 | btf->data = data; |
6205 | btf->data_size = data_size; | |
53297220 | 6206 | btf->kernel_btf = true; |
8646db23 | 6207 | snprintf(btf->name, sizeof(btf->name), "%s", name); |
8580ac94 AS |
6208 | |
6209 | err = btf_parse_hdr(env); | |
6210 | if (err) | |
6211 | goto errout; | |
6212 | ||
6213 | btf->nohdr_data = btf->data + btf->hdr.hdr_len; | |
6214 | ||
6215 | err = btf_parse_str_sec(env); | |
6216 | if (err) | |
6217 | goto errout; | |
6218 | ||
6219 | err = btf_check_all_metas(env); | |
6220 | if (err) | |
6221 | goto errout; | |
6222 | ||
eb596b09 KKD |
6223 | err = btf_check_type_tags(env, btf, 1); |
6224 | if (err) | |
6225 | goto errout; | |
6226 | ||
8580ac94 | 6227 | refcount_set(&btf->refcnt, 1); |
53297220 | 6228 | |
8580ac94 AS |
6229 | return btf; |
6230 | ||
6231 | errout: | |
8580ac94 AS |
6232 | if (btf) { |
6233 | kvfree(btf->types); | |
6234 | kfree(btf); | |
6235 | } | |
6236 | return ERR_PTR(err); | |
6237 | } | |
6238 | ||
8646db23 AM |
6239 | struct btf *btf_parse_vmlinux(void) |
6240 | { | |
6241 | struct btf_verifier_env *env = NULL; | |
6242 | struct bpf_verifier_log *log; | |
6243 | struct btf *btf; | |
6244 | int err; | |
6245 | ||
6246 | env = kzalloc(sizeof(*env), GFP_KERNEL | __GFP_NOWARN); | |
6247 | if (!env) | |
6248 | return ERR_PTR(-ENOMEM); | |
6249 | ||
6250 | log = &env->log; | |
6251 | log->level = BPF_LOG_KERNEL; | |
6252 | btf = btf_parse_base(env, "vmlinux", __start_BTF, __stop_BTF - __start_BTF); | |
6253 | if (IS_ERR(btf)) | |
6254 | goto err_out; | |
6255 | ||
6256 | /* btf_parse_vmlinux() runs under bpf_verifier_lock */ | |
6257 | bpf_ctx_convert.t = btf_type_by_id(btf, bpf_ctx_convert_btf_id[0]); | |
6258 | err = btf_alloc_id(btf); | |
6259 | if (err) { | |
6260 | btf_free(btf); | |
6261 | btf = ERR_PTR(err); | |
6262 | } | |
6263 | err_out: | |
6264 | btf_verifier_env_free(env); | |
6265 | return btf; | |
6266 | } | |
6267 | ||
8646db23 AM |
6268 | /* If .BTF_ids section was created with distilled base BTF, both base and |
6269 | * split BTF ids will need to be mapped to actual base/split ids for | |
6270 | * BTF now that it has been relocated. | |
6271 | */ | |
6272 | static __u32 btf_relocate_id(const struct btf *btf, __u32 id) | |
6273 | { | |
6274 | if (!btf->base_btf || !btf->base_id_map) | |
6275 | return id; | |
6276 | return btf->base_id_map[id]; | |
6277 | } | |
6278 | ||
5a532459 AM |
6279 | #ifdef CONFIG_DEBUG_INFO_BTF_MODULES |
6280 | ||
8646db23 AM |
6281 | static struct btf *btf_parse_module(const char *module_name, const void *data, |
6282 | unsigned int data_size, void *base_data, | |
6283 | unsigned int base_data_size) | |
36e68442 | 6284 | { |
8646db23 | 6285 | struct btf *btf = NULL, *vmlinux_btf, *base_btf = NULL; |
36e68442 AN |
6286 | struct btf_verifier_env *env = NULL; |
6287 | struct bpf_verifier_log *log; | |
8646db23 | 6288 | int err = 0; |
36e68442 | 6289 | |
8646db23 AM |
6290 | vmlinux_btf = bpf_get_btf_vmlinux(); |
6291 | if (IS_ERR(vmlinux_btf)) | |
6292 | return vmlinux_btf; | |
6293 | if (!vmlinux_btf) | |
36e68442 AN |
6294 | return ERR_PTR(-EINVAL); |
6295 | ||
6296 | env = kzalloc(sizeof(*env), GFP_KERNEL | __GFP_NOWARN); | |
6297 | if (!env) | |
6298 | return ERR_PTR(-ENOMEM); | |
6299 | ||
6300 | log = &env->log; | |
6301 | log->level = BPF_LOG_KERNEL; | |
6302 | ||
8646db23 AM |
6303 | if (base_data) { |
6304 | base_btf = btf_parse_base(env, ".BTF.base", base_data, base_data_size); | |
6305 | if (IS_ERR(base_btf)) { | |
6306 | err = PTR_ERR(base_btf); | |
6307 | goto errout; | |
6308 | } | |
6309 | } else { | |
6310 | base_btf = vmlinux_btf; | |
6311 | } | |
6312 | ||
36e68442 AN |
6313 | btf = kzalloc(sizeof(*btf), GFP_KERNEL | __GFP_NOWARN); |
6314 | if (!btf) { | |
6315 | err = -ENOMEM; | |
6316 | goto errout; | |
6317 | } | |
6318 | env->btf = btf; | |
6319 | ||
6320 | btf->base_btf = base_btf; | |
6321 | btf->start_id = base_btf->nr_types; | |
6322 | btf->start_str_off = base_btf->hdr.str_len; | |
6323 | btf->kernel_btf = true; | |
6324 | snprintf(btf->name, sizeof(btf->name), "%s", module_name); | |
6325 | ||
c6d9dafb | 6326 | btf->data = kvmemdup(data, data_size, GFP_KERNEL | __GFP_NOWARN); |
36e68442 AN |
6327 | if (!btf->data) { |
6328 | err = -ENOMEM; | |
6329 | goto errout; | |
6330 | } | |
36e68442 AN |
6331 | btf->data_size = data_size; |
6332 | ||
6333 | err = btf_parse_hdr(env); | |
6334 | if (err) | |
6335 | goto errout; | |
6336 | ||
6337 | btf->nohdr_data = btf->data + btf->hdr.hdr_len; | |
6338 | ||
6339 | err = btf_parse_str_sec(env); | |
6340 | if (err) | |
6341 | goto errout; | |
6342 | ||
6343 | err = btf_check_all_metas(env); | |
6344 | if (err) | |
6345 | goto errout; | |
6346 | ||
eb596b09 KKD |
6347 | err = btf_check_type_tags(env, btf, btf_nr_types(base_btf)); |
6348 | if (err) | |
6349 | goto errout; | |
6350 | ||
8646db23 AM |
6351 | if (base_btf != vmlinux_btf) { |
6352 | err = btf_relocate(btf, vmlinux_btf, &btf->base_id_map); | |
6353 | if (err) | |
6354 | goto errout; | |
6355 | btf_free(base_btf); | |
6356 | base_btf = vmlinux_btf; | |
6357 | } | |
6358 | ||
36e68442 AN |
6359 | btf_verifier_env_free(env); |
6360 | refcount_set(&btf->refcnt, 1); | |
6361 | return btf; | |
6362 | ||
6363 | errout: | |
6364 | btf_verifier_env_free(env); | |
b408473e | 6365 | if (!IS_ERR(base_btf) && base_btf != vmlinux_btf) |
8646db23 | 6366 | btf_free(base_btf); |
36e68442 AN |
6367 | if (btf) { |
6368 | kvfree(btf->data); | |
6369 | kvfree(btf->types); | |
6370 | kfree(btf); | |
6371 | } | |
6372 | return ERR_PTR(err); | |
6373 | } | |
6374 | ||
7112d127 AN |
6375 | #endif /* CONFIG_DEBUG_INFO_BTF_MODULES */ |
6376 | ||
5b92a28a AS |
6377 | struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog) |
6378 | { | |
3aac1ead | 6379 | struct bpf_prog *tgt_prog = prog->aux->dst_prog; |
5b92a28a | 6380 | |
22dc4a0f | 6381 | if (tgt_prog) |
5b92a28a | 6382 | return tgt_prog->aux->btf; |
22dc4a0f AN |
6383 | else |
6384 | return prog->aux->attach_btf; | |
5b92a28a AS |
6385 | } |
6386 | ||
1271a40e | 6387 | static bool is_void_or_int_ptr(struct btf *btf, const struct btf_type *t) |
84ad7a7a | 6388 | { |
91f2dc68 FZ |
6389 | /* skip modifiers */ |
6390 | t = btf_type_skip_modifiers(btf, t->type, NULL); | |
1271a40e | 6391 | return btf_type_is_void(t) || btf_type_is_int(t); |
84ad7a7a JO |
6392 | } |
6393 | ||
a1b669ea AH |
6394 | u32 btf_ctx_arg_idx(struct btf *btf, const struct btf_type *func_proto, |
6395 | int off) | |
720e6a43 YS |
6396 | { |
6397 | const struct btf_param *args; | |
6398 | const struct btf_type *t; | |
6399 | u32 offset = 0, nr_args; | |
6400 | int i; | |
6401 | ||
6402 | if (!func_proto) | |
6403 | return off / 8; | |
6404 | ||
6405 | nr_args = btf_type_vlen(func_proto); | |
6406 | args = (const struct btf_param *)(func_proto + 1); | |
6407 | for (i = 0; i < nr_args; i++) { | |
6408 | t = btf_type_skip_modifiers(btf, args[i].type, NULL); | |
6409 | offset += btf_type_is_ptr(t) ? 8 : roundup(t->size, 8); | |
6410 | if (off < offset) | |
6411 | return i; | |
6412 | } | |
6413 | ||
6414 | t = btf_type_skip_modifiers(btf, func_proto->type, NULL); | |
6415 | offset += btf_type_is_ptr(t) ? 8 : roundup(t->size, 8); | |
6416 | if (off < offset) | |
6417 | return nr_args; | |
6418 | ||
6419 | return nr_args + 1; | |
6420 | } | |
6421 | ||
c6b0337f | 6422 | static bool prog_args_trusted(const struct bpf_prog *prog) |
3f00c523 | 6423 | { |
c6b0337f AS |
6424 | enum bpf_attach_type atype = prog->expected_attach_type; |
6425 | ||
6426 | switch (prog->type) { | |
6427 | case BPF_PROG_TYPE_TRACING: | |
6428 | return atype == BPF_TRACE_RAW_TP || atype == BPF_TRACE_ITER; | |
6429 | case BPF_PROG_TYPE_LSM: | |
c0c852dd | 6430 | return bpf_lsm_is_trusted(prog); |
c6b0337f AS |
6431 | case BPF_PROG_TYPE_STRUCT_OPS: |
6432 | return true; | |
6433 | default: | |
6434 | return false; | |
6435 | } | |
3f00c523 DV |
6436 | } |
6437 | ||
16116035 KFL |
6438 | int btf_ctx_arg_offset(const struct btf *btf, const struct btf_type *func_proto, |
6439 | u32 arg_no) | |
6440 | { | |
6441 | const struct btf_param *args; | |
6442 | const struct btf_type *t; | |
6443 | int off = 0, i; | |
6444 | u32 sz; | |
6445 | ||
6446 | args = btf_params(func_proto); | |
6447 | for (i = 0; i < arg_no; i++) { | |
6448 | t = btf_type_by_id(btf, args[i].type); | |
6449 | t = btf_resolve_size(btf, t, &sz); | |
6450 | if (IS_ERR(t)) | |
6451 | return PTR_ERR(t); | |
6452 | off += roundup(sz, 8); | |
6453 | } | |
6454 | ||
6455 | return off; | |
6456 | } | |
6457 | ||
838a10bd KKD |
6458 | struct bpf_raw_tp_null_args { |
6459 | const char *func; | |
6460 | u64 mask; | |
6461 | }; | |
6462 | ||
6463 | static const struct bpf_raw_tp_null_args raw_tp_null_args[] = { | |
6464 | /* sched */ | |
6465 | { "sched_pi_setprio", 0x10 }, | |
6466 | /* ... from sched_numa_pair_template event class */ | |
6467 | { "sched_stick_numa", 0x100 }, | |
6468 | { "sched_swap_numa", 0x100 }, | |
6469 | /* afs */ | |
6470 | { "afs_make_fs_call", 0x10 }, | |
6471 | { "afs_make_fs_calli", 0x10 }, | |
6472 | { "afs_make_fs_call1", 0x10 }, | |
6473 | { "afs_make_fs_call2", 0x10 }, | |
6474 | { "afs_protocol_error", 0x1 }, | |
6475 | { "afs_flock_ev", 0x10 }, | |
6476 | /* cachefiles */ | |
6477 | { "cachefiles_lookup", 0x1 | 0x200 }, | |
6478 | { "cachefiles_unlink", 0x1 }, | |
6479 | { "cachefiles_rename", 0x1 }, | |
6480 | { "cachefiles_prep_read", 0x1 }, | |
6481 | { "cachefiles_mark_active", 0x1 }, | |
6482 | { "cachefiles_mark_failed", 0x1 }, | |
6483 | { "cachefiles_mark_inactive", 0x1 }, | |
6484 | { "cachefiles_vfs_error", 0x1 }, | |
6485 | { "cachefiles_io_error", 0x1 }, | |
6486 | { "cachefiles_ondemand_open", 0x1 }, | |
6487 | { "cachefiles_ondemand_copen", 0x1 }, | |
6488 | { "cachefiles_ondemand_close", 0x1 }, | |
6489 | { "cachefiles_ondemand_read", 0x1 }, | |
6490 | { "cachefiles_ondemand_cread", 0x1 }, | |
6491 | { "cachefiles_ondemand_fd_write", 0x1 }, | |
6492 | { "cachefiles_ondemand_fd_release", 0x1 }, | |
6493 | /* ext4, from ext4__mballoc event class */ | |
6494 | { "ext4_mballoc_discard", 0x10 }, | |
6495 | { "ext4_mballoc_free", 0x10 }, | |
6496 | /* fib */ | |
6497 | { "fib_table_lookup", 0x100 }, | |
6498 | /* filelock */ | |
6499 | /* ... from filelock_lock event class */ | |
6500 | { "posix_lock_inode", 0x10 }, | |
6501 | { "fcntl_setlk", 0x10 }, | |
6502 | { "locks_remove_posix", 0x10 }, | |
6503 | { "flock_lock_inode", 0x10 }, | |
6504 | /* ... from filelock_lease event class */ | |
6505 | { "break_lease_noblock", 0x10 }, | |
6506 | { "break_lease_block", 0x10 }, | |
6507 | { "break_lease_unblock", 0x10 }, | |
6508 | { "generic_delete_lease", 0x10 }, | |
6509 | { "time_out_leases", 0x10 }, | |
6510 | /* host1x */ | |
6511 | { "host1x_cdma_push_gather", 0x10000 }, | |
6512 | /* huge_memory */ | |
6513 | { "mm_khugepaged_scan_pmd", 0x10 }, | |
6514 | { "mm_collapse_huge_page_isolate", 0x1 }, | |
6515 | { "mm_khugepaged_scan_file", 0x10 }, | |
6516 | { "mm_khugepaged_collapse_file", 0x10 }, | |
6517 | /* kmem */ | |
6518 | { "mm_page_alloc", 0x1 }, | |
6519 | { "mm_page_pcpu_drain", 0x1 }, | |
6520 | /* .. from mm_page event class */ | |
6521 | { "mm_page_alloc_zone_locked", 0x1 }, | |
6522 | /* netfs */ | |
6523 | { "netfs_failure", 0x10 }, | |
6524 | /* power */ | |
6525 | { "device_pm_callback_start", 0x10 }, | |
6526 | /* qdisc */ | |
6527 | { "qdisc_dequeue", 0x1000 }, | |
6528 | /* rxrpc */ | |
6529 | { "rxrpc_recvdata", 0x1 }, | |
6530 | { "rxrpc_resend", 0x10 }, | |
c83e2d97 JO |
6531 | { "rxrpc_tq", 0x10 }, |
6532 | { "rxrpc_client", 0x1 }, | |
5da7e15f KI |
6533 | /* skb */ |
6534 | {"kfree_skb", 0x1000}, | |
838a10bd KKD |
6535 | /* sunrpc */ |
6536 | { "xs_stream_read_data", 0x1 }, | |
6537 | /* ... from xprt_cong_event event class */ | |
6538 | { "xprt_reserve_cong", 0x10 }, | |
6539 | { "xprt_release_cong", 0x10 }, | |
6540 | { "xprt_get_cong", 0x10 }, | |
6541 | { "xprt_put_cong", 0x10 }, | |
6542 | /* tcp */ | |
6543 | { "tcp_send_reset", 0x11 }, | |
0f08335a | 6544 | { "tcp_sendmsg_locked", 0x100 }, |
838a10bd KKD |
6545 | /* tegra_apb_dma */ |
6546 | { "tegra_dma_tx_status", 0x100 }, | |
6547 | /* timer_migration */ | |
6548 | { "tmigr_update_events", 0x1 }, | |
6549 | /* writeback, from writeback_folio_template event class */ | |
6550 | { "writeback_dirty_folio", 0x10 }, | |
6551 | { "folio_wait_writeback", 0x10 }, | |
6552 | /* rdma */ | |
6553 | { "mr_integ_alloc", 0x2000 }, | |
6554 | /* bpf_testmod */ | |
6555 | { "bpf_testmod_test_read", 0x0 }, | |
c83e2d97 JO |
6556 | /* amdgpu */ |
6557 | { "amdgpu_vm_bo_map", 0x1 }, | |
6558 | { "amdgpu_vm_bo_unmap", 0x1 }, | |
6559 | /* netfs */ | |
6560 | { "netfs_folioq", 0x1 }, | |
6561 | /* xfs from xfs_defer_pending_class */ | |
6562 | { "xfs_defer_create_intent", 0x1 }, | |
6563 | { "xfs_defer_cancel_list", 0x1 }, | |
6564 | { "xfs_defer_pending_finish", 0x1 }, | |
6565 | { "xfs_defer_pending_abort", 0x1 }, | |
6566 | { "xfs_defer_relog_intent", 0x1 }, | |
6567 | { "xfs_defer_isolate_paused", 0x1 }, | |
6568 | { "xfs_defer_item_pause", 0x1 }, | |
6569 | { "xfs_defer_item_unpause", 0x1 }, | |
6570 | /* xfs from xfs_defer_pending_item_class */ | |
6571 | { "xfs_defer_add_item", 0x1 }, | |
6572 | { "xfs_defer_cancel_item", 0x1 }, | |
6573 | { "xfs_defer_finish_item", 0x1 }, | |
6574 | /* xfs from xfs_icwalk_class */ | |
6575 | { "xfs_ioc_free_eofblocks", 0x10 }, | |
6576 | { "xfs_blockgc_free_space", 0x10 }, | |
6577 | /* xfs from xfs_btree_cur_class */ | |
6578 | { "xfs_btree_updkeys", 0x100 }, | |
6579 | { "xfs_btree_overlapped_query_range", 0x100 }, | |
6580 | /* xfs from xfs_imap_class*/ | |
6581 | { "xfs_map_blocks_found", 0x10000 }, | |
6582 | { "xfs_map_blocks_alloc", 0x10000 }, | |
6583 | { "xfs_iomap_alloc", 0x1000 }, | |
6584 | { "xfs_iomap_found", 0x1000 }, | |
6585 | /* xfs from xfs_fs_class */ | |
6586 | { "xfs_inodegc_flush", 0x1 }, | |
6587 | { "xfs_inodegc_push", 0x1 }, | |
6588 | { "xfs_inodegc_start", 0x1 }, | |
6589 | { "xfs_inodegc_stop", 0x1 }, | |
6590 | { "xfs_inodegc_queue", 0x1 }, | |
6591 | { "xfs_inodegc_throttle", 0x1 }, | |
6592 | { "xfs_fs_sync_fs", 0x1 }, | |
6593 | { "xfs_blockgc_start", 0x1 }, | |
6594 | { "xfs_blockgc_stop", 0x1 }, | |
6595 | { "xfs_blockgc_worker", 0x1 }, | |
6596 | { "xfs_blockgc_flush_all", 0x1 }, | |
6597 | /* xfs_scrub */ | |
6598 | { "xchk_nlinks_live_update", 0x10 }, | |
6599 | /* xfs_scrub from xchk_metapath_class */ | |
6600 | { "xchk_metapath_lookup", 0x100 }, | |
6601 | /* nfsd */ | |
6602 | { "nfsd_dirent", 0x1 }, | |
6603 | { "nfsd_file_acquire", 0x1001 }, | |
6604 | { "nfsd_file_insert_err", 0x1 }, | |
6605 | { "nfsd_file_cons_err", 0x1 }, | |
6606 | /* nfs4 */ | |
6607 | { "nfs4_setup_sequence", 0x1 }, | |
6608 | { "pnfs_update_layout", 0x10000 }, | |
6609 | { "nfs4_inode_callback_event", 0x200 }, | |
6610 | { "nfs4_inode_stateid_callback_event", 0x200 }, | |
6611 | /* nfs from pnfs_layout_event */ | |
6612 | { "pnfs_mds_fallback_pg_init_read", 0x10000 }, | |
6613 | { "pnfs_mds_fallback_pg_init_write", 0x10000 }, | |
6614 | { "pnfs_mds_fallback_pg_get_mirror_count", 0x10000 }, | |
6615 | { "pnfs_mds_fallback_read_done", 0x10000 }, | |
6616 | { "pnfs_mds_fallback_write_done", 0x10000 }, | |
6617 | { "pnfs_mds_fallback_read_pagelist", 0x10000 }, | |
6618 | { "pnfs_mds_fallback_write_pagelist", 0x10000 }, | |
6619 | /* coda */ | |
6620 | { "coda_dec_pic_run", 0x10 }, | |
6621 | { "coda_dec_pic_done", 0x10 }, | |
6622 | /* cfg80211 */ | |
6623 | { "cfg80211_scan_done", 0x11 }, | |
6624 | { "rdev_set_coalesce", 0x10 }, | |
6625 | { "cfg80211_report_wowlan_wakeup", 0x100 }, | |
6626 | { "cfg80211_inform_bss_frame", 0x100 }, | |
6627 | { "cfg80211_michael_mic_failure", 0x10000 }, | |
6628 | /* cfg80211 from wiphy_work_event */ | |
6629 | { "wiphy_work_queue", 0x10 }, | |
6630 | { "wiphy_work_run", 0x10 }, | |
6631 | { "wiphy_work_cancel", 0x10 }, | |
6632 | { "wiphy_work_flush", 0x10 }, | |
6633 | /* hugetlbfs */ | |
6634 | { "hugetlbfs_alloc_inode", 0x10 }, | |
6635 | /* spufs */ | |
6636 | { "spufs_context", 0x10 }, | |
6637 | /* kvm_hv */ | |
6638 | { "kvm_page_fault_enter", 0x100 }, | |
6639 | /* dpu */ | |
6640 | { "dpu_crtc_setup_mixer", 0x100 }, | |
6641 | /* binder */ | |
6642 | { "binder_transaction", 0x100 }, | |
6643 | /* bcachefs */ | |
6644 | { "btree_path_free", 0x100 }, | |
6645 | /* hfi1_tx */ | |
6646 | { "hfi1_sdma_progress", 0x1000 }, | |
6647 | /* iptfs */ | |
6648 | { "iptfs_ingress_postq_event", 0x1000 }, | |
6649 | /* neigh */ | |
6650 | { "neigh_update", 0x10 }, | |
6651 | /* snd_firewire_lib */ | |
6652 | { "amdtp_packet", 0x100 }, | |
838a10bd KKD |
6653 | }; |
6654 | ||
9e15db66 AS |
6655 | bool btf_ctx_access(int off, int size, enum bpf_access_type type, |
6656 | const struct bpf_prog *prog, | |
6657 | struct bpf_insn_access_aux *info) | |
6658 | { | |
38207291 | 6659 | const struct btf_type *t = prog->aux->attach_func_proto; |
3aac1ead | 6660 | struct bpf_prog *tgt_prog = prog->aux->dst_prog; |
5b92a28a | 6661 | struct btf *btf = bpf_prog_get_target_btf(prog); |
38207291 | 6662 | const char *tname = prog->aux->attach_func_name; |
9e15db66 | 6663 | struct bpf_verifier_log *log = info->log; |
9e15db66 | 6664 | const struct btf_param *args; |
838a10bd | 6665 | bool ptr_err_raw_tp = false; |
c6f1bfe8 | 6666 | const char *tag_value; |
9e15db66 | 6667 | u32 nr_args, arg; |
3c32cc1b | 6668 | int i, ret; |
9e15db66 | 6669 | |
9e15db66 | 6670 | if (off % 8) { |
38207291 | 6671 | bpf_log(log, "func '%s' offset %d is not multiple of 8\n", |
9e15db66 AS |
6672 | tname, off); |
6673 | return false; | |
6674 | } | |
a1b669ea | 6675 | arg = btf_ctx_arg_idx(btf, t, off); |
9e15db66 | 6676 | args = (const struct btf_param *)(t + 1); |
523a4cf4 DB |
6677 | /* if (t == NULL) Fall back to default BPF prog with |
6678 | * MAX_BPF_FUNC_REG_ARGS u64 arguments. | |
6679 | */ | |
6680 | nr_args = t ? btf_type_vlen(t) : MAX_BPF_FUNC_REG_ARGS; | |
38207291 MKL |
6681 | if (prog->aux->attach_btf_trace) { |
6682 | /* skip first 'void *__data' argument in btf_trace_##name typedef */ | |
6683 | args++; | |
6684 | nr_args--; | |
6685 | } | |
fec56f58 | 6686 | |
f50b49a0 KS |
6687 | if (arg > nr_args) { |
6688 | bpf_log(log, "func '%s' doesn't have %d-th argument\n", | |
6689 | tname, arg + 1); | |
6690 | return false; | |
6691 | } | |
6692 | ||
6ba43b76 | 6693 | if (arg == nr_args) { |
f50b49a0 KS |
6694 | switch (prog->expected_attach_type) { |
6695 | case BPF_LSM_MAC: | |
5d99e198 XK |
6696 | /* mark we are accessing the return value */ |
6697 | info->is_retval = true; | |
6698 | fallthrough; | |
6699 | case BPF_LSM_CGROUP: | |
f50b49a0 | 6700 | case BPF_TRACE_FEXIT: |
9e4e01df KS |
6701 | /* When LSM programs are attached to void LSM hooks |
6702 | * they use FEXIT trampolines and when attached to | |
6703 | * int LSM hooks, they use MODIFY_RETURN trampolines. | |
6704 | * | |
6705 | * While the LSM programs are BPF_MODIFY_RETURN-like | |
6706 | * the check: | |
6707 | * | |
6708 | * if (ret_type != 'int') | |
6709 | * return -EINVAL; | |
6710 | * | |
6711 | * is _not_ done here. This is still safe as LSM hooks | |
6712 | * have only void and int return types. | |
6713 | */ | |
6ba43b76 KS |
6714 | if (!t) |
6715 | return true; | |
6716 | t = btf_type_by_id(btf, t->type); | |
f50b49a0 KS |
6717 | break; |
6718 | case BPF_MODIFY_RETURN: | |
6ba43b76 KS |
6719 | /* For now the BPF_MODIFY_RETURN can only be attached to |
6720 | * functions that return an int. | |
6721 | */ | |
6722 | if (!t) | |
6723 | return false; | |
6724 | ||
6725 | t = btf_type_skip_modifiers(btf, t->type, NULL); | |
a9b59159 | 6726 | if (!btf_type_is_small_int(t)) { |
6ba43b76 KS |
6727 | bpf_log(log, |
6728 | "ret type %s not allowed for fmod_ret\n", | |
571f9738 | 6729 | btf_type_str(t)); |
6ba43b76 KS |
6730 | return false; |
6731 | } | |
f50b49a0 KS |
6732 | break; |
6733 | default: | |
6734 | bpf_log(log, "func '%s' doesn't have %d-th argument\n", | |
6735 | tname, arg + 1); | |
6736 | return false; | |
6ba43b76 | 6737 | } |
fec56f58 | 6738 | } else { |
5b92a28a | 6739 | if (!t) |
523a4cf4 | 6740 | /* Default prog with MAX_BPF_FUNC_REG_ARGS args */ |
5b92a28a AS |
6741 | return true; |
6742 | t = btf_type_by_id(btf, args[arg].type); | |
9e15db66 | 6743 | } |
f50b49a0 | 6744 | |
9e15db66 AS |
6745 | /* skip modifiers */ |
6746 | while (btf_type_is_modifier(t)) | |
5b92a28a | 6747 | t = btf_type_by_id(btf, t->type); |
720e6a43 | 6748 | if (btf_type_is_small_int(t) || btf_is_any_enum(t) || __btf_type_is_struct(t)) |
9e15db66 AS |
6749 | /* accessing a scalar */ |
6750 | return true; | |
6751 | if (!btf_type_is_ptr(t)) { | |
6752 | bpf_log(log, | |
38207291 | 6753 | "func '%s' arg%d '%s' has type %s. Only pointer access is allowed\n", |
9e15db66 | 6754 | tname, arg, |
5b92a28a | 6755 | __btf_name_by_offset(btf, t->name_off), |
571f9738 | 6756 | btf_type_str(t)); |
9e15db66 AS |
6757 | return false; |
6758 | } | |
afbf21dc | 6759 | |
659b9ba7 KKD |
6760 | if (size != sizeof(u64)) { |
6761 | bpf_log(log, "func '%s' size %d must be 8\n", | |
6762 | tname, size); | |
6763 | return false; | |
6764 | } | |
6765 | ||
afbf21dc YS |
6766 | /* check for PTR_TO_RDONLY_BUF_OR_NULL or PTR_TO_RDWR_BUF_OR_NULL */ |
6767 | for (i = 0; i < prog->aux->ctx_arg_info_size; i++) { | |
6768 | const struct bpf_ctx_arg_aux *ctx_arg_info = &prog->aux->ctx_arg_info[i]; | |
c25b2ae1 | 6769 | u32 type, flag; |
afbf21dc | 6770 | |
c25b2ae1 HL |
6771 | type = base_type(ctx_arg_info->reg_type); |
6772 | flag = type_flag(ctx_arg_info->reg_type); | |
20b2aff4 | 6773 | if (ctx_arg_info->offset == off && type == PTR_TO_BUF && |
c25b2ae1 | 6774 | (flag & PTR_MAYBE_NULL)) { |
afbf21dc YS |
6775 | info->reg_type = ctx_arg_info->reg_type; |
6776 | return true; | |
6777 | } | |
6778 | } | |
6779 | ||
1271a40e KW |
6780 | /* |
6781 | * If it's a pointer to void, it's the same as scalar from the verifier | |
6782 | * safety POV. Either way, no futher pointer walking is allowed. | |
6783 | */ | |
6784 | if (is_void_or_int_ptr(btf, t)) | |
84ad7a7a JO |
6785 | return true; |
6786 | ||
9e15db66 | 6787 | /* this is a pointer to another type */ |
3c32cc1b YS |
6788 | for (i = 0; i < prog->aux->ctx_arg_info_size; i++) { |
6789 | const struct bpf_ctx_arg_aux *ctx_arg_info = &prog->aux->ctx_arg_info[i]; | |
6790 | ||
6791 | if (ctx_arg_info->offset == off) { | |
d3621642 YS |
6792 | if (!ctx_arg_info->btf_id) { |
6793 | bpf_log(log,"invalid btf_id for context argument offset %u\n", off); | |
6794 | return false; | |
6795 | } | |
6796 | ||
3c32cc1b | 6797 | info->reg_type = ctx_arg_info->reg_type; |
77c0208e | 6798 | info->btf = ctx_arg_info->btf ? : btf_vmlinux; |
951cf368 | 6799 | info->btf_id = ctx_arg_info->btf_id; |
a687df20 | 6800 | info->ref_obj_id = ctx_arg_info->ref_obj_id; |
951cf368 | 6801 | return true; |
3c32cc1b YS |
6802 | } |
6803 | } | |
9e15db66 | 6804 | |
951cf368 | 6805 | info->reg_type = PTR_TO_BTF_ID; |
c6b0337f | 6806 | if (prog_args_trusted(prog)) |
3f00c523 DV |
6807 | info->reg_type |= PTR_TRUSTED; |
6808 | ||
c00d738e | 6809 | if (btf_param_match_suffix(btf, &args[arg], "__nullable")) |
8aeaed21 PL |
6810 | info->reg_type |= PTR_MAYBE_NULL; |
6811 | ||
838a10bd KKD |
6812 | if (prog->expected_attach_type == BPF_TRACE_RAW_TP) { |
6813 | struct btf *btf = prog->aux->attach_btf; | |
6814 | const struct btf_type *t; | |
6815 | const char *tname; | |
6816 | ||
6817 | /* BTF lookups cannot fail, return false on error */ | |
6818 | t = btf_type_by_id(btf, prog->aux->attach_btf_id); | |
6819 | if (!t) | |
6820 | return false; | |
6821 | tname = btf_name_by_offset(btf, t->name_off); | |
6822 | if (!tname) | |
6823 | return false; | |
6824 | /* Checked by bpf_check_attach_target */ | |
6825 | tname += sizeof("btf_trace_") - 1; | |
6826 | for (i = 0; i < ARRAY_SIZE(raw_tp_null_args); i++) { | |
6827 | /* Is this a func with potential NULL args? */ | |
6828 | if (strcmp(tname, raw_tp_null_args[i].func)) | |
6829 | continue; | |
53ebef53 | 6830 | if (raw_tp_null_args[i].mask & (0x1ULL << (arg * 4))) |
838a10bd KKD |
6831 | info->reg_type |= PTR_MAYBE_NULL; |
6832 | /* Is the current arg IS_ERR? */ | |
53ebef53 | 6833 | if (raw_tp_null_args[i].mask & (0x2ULL << (arg * 4))) |
838a10bd KKD |
6834 | ptr_err_raw_tp = true; |
6835 | break; | |
6836 | } | |
6837 | /* If we don't know NULL-ness specification and the tracepoint | |
6838 | * is coming from a loadable module, be conservative and mark | |
6839 | * argument as PTR_MAYBE_NULL. | |
6840 | */ | |
6841 | if (i == ARRAY_SIZE(raw_tp_null_args) && btf_is_module(btf)) | |
6842 | info->reg_type |= PTR_MAYBE_NULL; | |
6843 | } | |
6844 | ||
5b92a28a | 6845 | if (tgt_prog) { |
43bc2874 THJ |
6846 | enum bpf_prog_type tgt_type; |
6847 | ||
6848 | if (tgt_prog->type == BPF_PROG_TYPE_EXT) | |
6849 | tgt_type = tgt_prog->aux->saved_dst_prog_type; | |
6850 | else | |
6851 | tgt_type = tgt_prog->type; | |
6852 | ||
6853 | ret = btf_translate_to_vmlinux(log, btf, t, tgt_type, arg); | |
5b92a28a | 6854 | if (ret > 0) { |
22dc4a0f | 6855 | info->btf = btf_vmlinux; |
5b92a28a AS |
6856 | info->btf_id = ret; |
6857 | return true; | |
6858 | } else { | |
6859 | return false; | |
6860 | } | |
6861 | } | |
275517ff | 6862 | |
22dc4a0f | 6863 | info->btf = btf; |
275517ff | 6864 | info->btf_id = t->type; |
5b92a28a | 6865 | t = btf_type_by_id(btf, t->type); |
c6f1bfe8 | 6866 | |
53ee0d66 | 6867 | if (btf_type_is_type_tag(t) && !btf_type_kflag(t)) { |
c6f1bfe8 YS |
6868 | tag_value = __btf_name_by_offset(btf, t->name_off); |
6869 | if (strcmp(tag_value, "user") == 0) | |
6870 | info->reg_type |= MEM_USER; | |
5844101a HL |
6871 | if (strcmp(tag_value, "percpu") == 0) |
6872 | info->reg_type |= MEM_PERCPU; | |
c6f1bfe8 YS |
6873 | } |
6874 | ||
9e15db66 | 6875 | /* skip modifiers */ |
275517ff MKL |
6876 | while (btf_type_is_modifier(t)) { |
6877 | info->btf_id = t->type; | |
5b92a28a | 6878 | t = btf_type_by_id(btf, t->type); |
275517ff | 6879 | } |
9e15db66 AS |
6880 | if (!btf_type_is_struct(t)) { |
6881 | bpf_log(log, | |
38207291 | 6882 | "func '%s' arg%d type %s is not a struct\n", |
571f9738 | 6883 | tname, arg, btf_type_str(t)); |
9e15db66 AS |
6884 | return false; |
6885 | } | |
38207291 | 6886 | bpf_log(log, "func '%s' arg%d has btf_id %d type %s '%s'\n", |
571f9738 | 6887 | tname, arg, info->btf_id, btf_type_str(t), |
5b92a28a | 6888 | __btf_name_by_offset(btf, t->name_off)); |
838a10bd KKD |
6889 | |
6890 | /* Perform all checks on the validity of type for this argument, but if | |
6891 | * we know it can be IS_ERR at runtime, scrub pointer type and mark as | |
6892 | * scalar. | |
6893 | */ | |
6894 | if (ptr_err_raw_tp) { | |
6895 | bpf_log(log, "marking pointer arg%d as scalar as it may encode error", arg); | |
6896 | info->reg_type = SCALAR_VALUE; | |
6897 | } | |
9e15db66 AS |
6898 | return true; |
6899 | } | |
7c81c249 | 6900 | EXPORT_SYMBOL_GPL(btf_ctx_access); |
9e15db66 | 6901 | |
1c6d28a6 JO |
6902 | enum bpf_struct_walk_result { |
6903 | /* < 0 error */ | |
6904 | WALK_SCALAR = 0, | |
6905 | WALK_PTR, | |
6906 | WALK_STRUCT, | |
6907 | }; | |
6908 | ||
22dc4a0f | 6909 | static int btf_struct_walk(struct bpf_verifier_log *log, const struct btf *btf, |
1c6d28a6 | 6910 | const struct btf_type *t, int off, int size, |
63260df1 AS |
6911 | u32 *next_btf_id, enum bpf_type_flag *flag, |
6912 | const char **field_name) | |
9e15db66 | 6913 | { |
7e3617a7 MKL |
6914 | u32 i, moff, mtrue_end, msize = 0, total_nelems = 0; |
6915 | const struct btf_type *mtype, *elem_type = NULL; | |
9e15db66 | 6916 | const struct btf_member *member; |
c6f1bfe8 | 6917 | const char *tname, *mname, *tag_value; |
1c6d28a6 | 6918 | u32 vlen, elem_id, mid; |
9e15db66 AS |
6919 | |
6920 | again: | |
819d4342 SF |
6921 | if (btf_type_is_modifier(t)) |
6922 | t = btf_type_skip_modifiers(btf, t->type, NULL); | |
22dc4a0f | 6923 | tname = __btf_name_by_offset(btf, t->name_off); |
9e15db66 | 6924 | if (!btf_type_is_struct(t)) { |
275517ff | 6925 | bpf_log(log, "Type '%s' is not a struct\n", tname); |
9e15db66 AS |
6926 | return -EINVAL; |
6927 | } | |
6928 | ||
9c5f8a10 | 6929 | vlen = btf_type_vlen(t); |
7ce4dc3e YS |
6930 | if (BTF_INFO_KIND(t->info) == BTF_KIND_UNION && vlen != 1 && !(*flag & PTR_UNTRUSTED)) |
6931 | /* | |
6932 | * walking unions yields untrusted pointers | |
6933 | * with exception of __bpf_md_ptr and other | |
6934 | * unions with a single member | |
6935 | */ | |
6936 | *flag |= PTR_UNTRUSTED; | |
6937 | ||
976aba00 | 6938 | if (off + size > t->size) { |
9c5f8a10 YS |
6939 | /* If the last element is a variable size array, we may |
6940 | * need to relax the rule. | |
6941 | */ | |
6942 | struct btf_array *array_elem; | |
6943 | ||
6944 | if (vlen == 0) | |
6945 | goto error; | |
6946 | ||
6947 | member = btf_type_member(t) + vlen - 1; | |
22dc4a0f | 6948 | mtype = btf_type_skip_modifiers(btf, member->type, |
9c5f8a10 YS |
6949 | NULL); |
6950 | if (!btf_type_is_array(mtype)) | |
6951 | goto error; | |
6952 | ||
6953 | array_elem = (struct btf_array *)(mtype + 1); | |
6954 | if (array_elem->nelems != 0) | |
6955 | goto error; | |
6956 | ||
8293eb99 | 6957 | moff = __btf_member_bit_offset(t, member) / 8; |
9c5f8a10 YS |
6958 | if (off < moff) |
6959 | goto error; | |
6960 | ||
2569c7b8 | 6961 | /* allow structure and integer */ |
22dc4a0f | 6962 | t = btf_type_skip_modifiers(btf, array_elem->type, |
dafe58fc | 6963 | NULL); |
2569c7b8 FZ |
6964 | |
6965 | if (btf_type_is_int(t)) | |
6966 | return WALK_SCALAR; | |
6967 | ||
dafe58fc | 6968 | if (!btf_type_is_struct(t)) |
9c5f8a10 YS |
6969 | goto error; |
6970 | ||
dafe58fc JO |
6971 | off = (off - moff) % t->size; |
6972 | goto again; | |
9c5f8a10 YS |
6973 | |
6974 | error: | |
976aba00 MKL |
6975 | bpf_log(log, "access beyond struct %s at off %u size %u\n", |
6976 | tname, off, size); | |
6977 | return -EACCES; | |
6978 | } | |
9e15db66 | 6979 | |
976aba00 | 6980 | for_each_member(i, t, member) { |
7e3617a7 | 6981 | /* offset of the field in bytes */ |
8293eb99 | 6982 | moff = __btf_member_bit_offset(t, member) / 8; |
7e3617a7 | 6983 | if (off + size <= moff) |
9e15db66 AS |
6984 | /* won't find anything, field is already too far */ |
6985 | break; | |
976aba00 | 6986 | |
8293eb99 AS |
6987 | if (__btf_member_bitfield_size(t, member)) { |
6988 | u32 end_bit = __btf_member_bit_offset(t, member) + | |
6989 | __btf_member_bitfield_size(t, member); | |
976aba00 MKL |
6990 | |
6991 | /* off <= moff instead of off == moff because clang | |
6992 | * does not generate a BTF member for anonymous | |
6993 | * bitfield like the ":16" here: | |
6994 | * struct { | |
6995 | * int :16; | |
6996 | * int x:8; | |
6997 | * }; | |
6998 | */ | |
6999 | if (off <= moff && | |
7000 | BITS_ROUNDUP_BYTES(end_bit) <= off + size) | |
1c6d28a6 | 7001 | return WALK_SCALAR; |
976aba00 MKL |
7002 | |
7003 | /* off may be accessing a following member | |
7004 | * | |
7005 | * or | |
7006 | * | |
7007 | * Doing partial access at either end of this | |
7008 | * bitfield. Continue on this case also to | |
7009 | * treat it as not accessing this bitfield | |
7010 | * and eventually error out as field not | |
7011 | * found to keep it simple. | |
7012 | * It could be relaxed if there was a legit | |
7013 | * partial access case later. | |
7014 | */ | |
7015 | continue; | |
7016 | } | |
7017 | ||
7e3617a7 MKL |
7018 | /* In case of "off" is pointing to holes of a struct */ |
7019 | if (off < moff) | |
976aba00 | 7020 | break; |
9e15db66 AS |
7021 | |
7022 | /* type of the field */ | |
1c6d28a6 | 7023 | mid = member->type; |
22dc4a0f AN |
7024 | mtype = btf_type_by_id(btf, member->type); |
7025 | mname = __btf_name_by_offset(btf, member->name_off); | |
9e15db66 | 7026 | |
22dc4a0f | 7027 | mtype = __btf_resolve_size(btf, mtype, &msize, |
1c6d28a6 JO |
7028 | &elem_type, &elem_id, &total_nelems, |
7029 | &mid); | |
7e3617a7 | 7030 | if (IS_ERR(mtype)) { |
9e15db66 AS |
7031 | bpf_log(log, "field %s doesn't have size\n", mname); |
7032 | return -EFAULT; | |
7033 | } | |
7e3617a7 MKL |
7034 | |
7035 | mtrue_end = moff + msize; | |
7036 | if (off >= mtrue_end) | |
9e15db66 AS |
7037 | /* no overlap with member, keep iterating */ |
7038 | continue; | |
7e3617a7 MKL |
7039 | |
7040 | if (btf_type_is_array(mtype)) { | |
7041 | u32 elem_idx; | |
7042 | ||
6298399b | 7043 | /* __btf_resolve_size() above helps to |
7e3617a7 MKL |
7044 | * linearize a multi-dimensional array. |
7045 | * | |
7046 | * The logic here is treating an array | |
7047 | * in a struct as the following way: | |
7048 | * | |
7049 | * struct outer { | |
7050 | * struct inner array[2][2]; | |
7051 | * }; | |
7052 | * | |
7053 | * looks like: | |
7054 | * | |
7055 | * struct outer { | |
7056 | * struct inner array_elem0; | |
7057 | * struct inner array_elem1; | |
7058 | * struct inner array_elem2; | |
7059 | * struct inner array_elem3; | |
7060 | * }; | |
7061 | * | |
7062 | * When accessing outer->array[1][0], it moves | |
7063 | * moff to "array_elem2", set mtype to | |
7064 | * "struct inner", and msize also becomes | |
7065 | * sizeof(struct inner). Then most of the | |
7066 | * remaining logic will fall through without | |
7067 | * caring the current member is an array or | |
7068 | * not. | |
7069 | * | |
7070 | * Unlike mtype/msize/moff, mtrue_end does not | |
7071 | * change. The naming difference ("_true") tells | |
7072 | * that it is not always corresponding to | |
7073 | * the current mtype/msize/moff. | |
7074 | * It is the true end of the current | |
7075 | * member (i.e. array in this case). That | |
7076 | * will allow an int array to be accessed like | |
7077 | * a scratch space, | |
7078 | * i.e. allow access beyond the size of | |
7079 | * the array's element as long as it is | |
7080 | * within the mtrue_end boundary. | |
7081 | */ | |
7082 | ||
7083 | /* skip empty array */ | |
7084 | if (moff == mtrue_end) | |
7085 | continue; | |
7086 | ||
7087 | msize /= total_nelems; | |
7088 | elem_idx = (off - moff) / msize; | |
7089 | moff += elem_idx * msize; | |
7090 | mtype = elem_type; | |
1c6d28a6 | 7091 | mid = elem_id; |
7e3617a7 MKL |
7092 | } |
7093 | ||
9e15db66 AS |
7094 | /* the 'off' we're looking for is either equal to start |
7095 | * of this field or inside of this struct | |
7096 | */ | |
7097 | if (btf_type_is_struct(mtype)) { | |
7098 | /* our field must be inside that union or struct */ | |
7099 | t = mtype; | |
7100 | ||
1c6d28a6 JO |
7101 | /* return if the offset matches the member offset */ |
7102 | if (off == moff) { | |
7103 | *next_btf_id = mid; | |
7104 | return WALK_STRUCT; | |
7105 | } | |
7106 | ||
9e15db66 | 7107 | /* adjust offset we're looking for */ |
7e3617a7 | 7108 | off -= moff; |
9e15db66 AS |
7109 | goto again; |
7110 | } | |
9e15db66 AS |
7111 | |
7112 | if (btf_type_is_ptr(mtype)) { | |
c6f1bfe8 YS |
7113 | const struct btf_type *stype, *t; |
7114 | enum bpf_type_flag tmp_flag = 0; | |
257af63d | 7115 | u32 id; |
9e15db66 | 7116 | |
7e3617a7 MKL |
7117 | if (msize != size || off != moff) { |
7118 | bpf_log(log, | |
7119 | "cannot access ptr member %s with moff %u in struct %s with off %u size %u\n", | |
7120 | mname, moff, tname, off, size); | |
7121 | return -EACCES; | |
7122 | } | |
c6f1bfe8 | 7123 | |
5844101a | 7124 | /* check type tag */ |
c6f1bfe8 | 7125 | t = btf_type_by_id(btf, mtype->type); |
53ee0d66 | 7126 | if (btf_type_is_type_tag(t) && !btf_type_kflag(t)) { |
c6f1bfe8 | 7127 | tag_value = __btf_name_by_offset(btf, t->name_off); |
5844101a | 7128 | /* check __user tag */ |
c6f1bfe8 YS |
7129 | if (strcmp(tag_value, "user") == 0) |
7130 | tmp_flag = MEM_USER; | |
5844101a HL |
7131 | /* check __percpu tag */ |
7132 | if (strcmp(tag_value, "percpu") == 0) | |
7133 | tmp_flag = MEM_PERCPU; | |
9bb00b28 YS |
7134 | /* check __rcu tag */ |
7135 | if (strcmp(tag_value, "rcu") == 0) | |
7136 | tmp_flag = MEM_RCU; | |
c6f1bfe8 YS |
7137 | } |
7138 | ||
22dc4a0f | 7139 | stype = btf_type_skip_modifiers(btf, mtype->type, &id); |
9e15db66 | 7140 | if (btf_type_is_struct(stype)) { |
257af63d | 7141 | *next_btf_id = id; |
6fcd486b | 7142 | *flag |= tmp_flag; |
63260df1 AS |
7143 | if (field_name) |
7144 | *field_name = mname; | |
1c6d28a6 | 7145 | return WALK_PTR; |
9e15db66 AS |
7146 | } |
7147 | } | |
7e3617a7 MKL |
7148 | |
7149 | /* Allow more flexible access within an int as long as | |
7150 | * it is within mtrue_end. | |
7151 | * Since mtrue_end could be the end of an array, | |
7152 | * that also allows using an array of int as a scratch | |
7153 | * space. e.g. skb->cb[]. | |
7154 | */ | |
33937607 | 7155 | if (off + size > mtrue_end && !(*flag & PTR_UNTRUSTED)) { |
7e3617a7 MKL |
7156 | bpf_log(log, |
7157 | "access beyond the end of member %s (mend:%u) in struct %s with off %u size %u\n", | |
7158 | mname, mtrue_end, tname, off, size); | |
7159 | return -EACCES; | |
7160 | } | |
7161 | ||
1c6d28a6 | 7162 | return WALK_SCALAR; |
9e15db66 AS |
7163 | } |
7164 | bpf_log(log, "struct %s doesn't have field at offset %d\n", tname, off); | |
7165 | return -EINVAL; | |
7166 | } | |
7167 | ||
6728aea7 KKD |
7168 | int btf_struct_access(struct bpf_verifier_log *log, |
7169 | const struct bpf_reg_state *reg, | |
7170 | int off, int size, enum bpf_access_type atype __maybe_unused, | |
63260df1 AS |
7171 | u32 *next_btf_id, enum bpf_type_flag *flag, |
7172 | const char **field_name) | |
1c6d28a6 | 7173 | { |
6728aea7 | 7174 | const struct btf *btf = reg->btf; |
c6f1bfe8 | 7175 | enum bpf_type_flag tmp_flag = 0; |
6728aea7 KKD |
7176 | const struct btf_type *t; |
7177 | u32 id = reg->btf_id; | |
1c6d28a6 | 7178 | int err; |
1c6d28a6 | 7179 | |
8ffa5cc1 KKD |
7180 | while (type_is_alloc(reg->type)) { |
7181 | struct btf_struct_meta *meta; | |
7182 | struct btf_record *rec; | |
7183 | int i; | |
7184 | ||
7185 | meta = btf_find_struct_meta(btf, id); | |
7186 | if (!meta) | |
7187 | break; | |
7188 | rec = meta->record; | |
7189 | for (i = 0; i < rec->cnt; i++) { | |
7190 | struct btf_field *field = &rec->fields[i]; | |
7191 | u32 offset = field->offset; | |
482f7133 | 7192 | if (off < offset + field->size && offset < off + size) { |
8ffa5cc1 KKD |
7193 | bpf_log(log, |
7194 | "direct access to %s is disallowed\n", | |
7195 | btf_field_type_name(field->type)); | |
7196 | return -EACCES; | |
7197 | } | |
7198 | } | |
7199 | break; | |
7200 | } | |
7201 | ||
6728aea7 | 7202 | t = btf_type_by_id(btf, id); |
1c6d28a6 | 7203 | do { |
63260df1 | 7204 | err = btf_struct_walk(log, btf, t, off, size, &id, &tmp_flag, field_name); |
1c6d28a6 JO |
7205 | |
7206 | switch (err) { | |
7207 | case WALK_PTR: | |
282de143 KKD |
7208 | /* For local types, the destination register cannot |
7209 | * become a pointer again. | |
7210 | */ | |
7211 | if (type_is_alloc(reg->type)) | |
7212 | return SCALAR_VALUE; | |
1c6d28a6 JO |
7213 | /* If we found the pointer or scalar on t+off, |
7214 | * we're done. | |
7215 | */ | |
7216 | *next_btf_id = id; | |
c6f1bfe8 | 7217 | *flag = tmp_flag; |
1c6d28a6 JO |
7218 | return PTR_TO_BTF_ID; |
7219 | case WALK_SCALAR: | |
7220 | return SCALAR_VALUE; | |
7221 | case WALK_STRUCT: | |
7222 | /* We found nested struct, so continue the search | |
7223 | * by diving in it. At this point the offset is | |
7224 | * aligned with the new type, so set it to 0. | |
7225 | */ | |
22dc4a0f | 7226 | t = btf_type_by_id(btf, id); |
1c6d28a6 JO |
7227 | off = 0; |
7228 | break; | |
7229 | default: | |
7230 | /* It's either error or unknown return value.. | |
7231 | * scream and leave. | |
7232 | */ | |
7233 | if (WARN_ONCE(err > 0, "unknown btf_struct_walk return value")) | |
7234 | return -EINVAL; | |
7235 | return err; | |
7236 | } | |
7237 | } while (t); | |
7238 | ||
7239 | return -EINVAL; | |
7240 | } | |
7241 | ||
22dc4a0f AN |
7242 | /* Check that two BTF types, each specified as an BTF object + id, are exactly |
7243 | * the same. Trivial ID check is not enough due to module BTFs, because we can | |
7244 | * end up with two different module BTFs, but IDs point to the common type in | |
7245 | * vmlinux BTF. | |
7246 | */ | |
00b85860 KKD |
7247 | bool btf_types_are_same(const struct btf *btf1, u32 id1, |
7248 | const struct btf *btf2, u32 id2) | |
22dc4a0f AN |
7249 | { |
7250 | if (id1 != id2) | |
7251 | return false; | |
7252 | if (btf1 == btf2) | |
7253 | return true; | |
7254 | return btf_type_by_id(btf1, id1) == btf_type_by_id(btf2, id2); | |
7255 | } | |
7256 | ||
faaf4a79 | 7257 | bool btf_struct_ids_match(struct bpf_verifier_log *log, |
22dc4a0f | 7258 | const struct btf *btf, u32 id, int off, |
2ab3b380 KKD |
7259 | const struct btf *need_btf, u32 need_type_id, |
7260 | bool strict) | |
faaf4a79 JO |
7261 | { |
7262 | const struct btf_type *type; | |
7ce4dc3e | 7263 | enum bpf_type_flag flag = 0; |
faaf4a79 JO |
7264 | int err; |
7265 | ||
7266 | /* Are we already done? */ | |
22dc4a0f | 7267 | if (off == 0 && btf_types_are_same(btf, id, need_btf, need_type_id)) |
faaf4a79 | 7268 | return true; |
2ab3b380 KKD |
7269 | /* In case of strict type match, we do not walk struct, the top level |
7270 | * type match must succeed. When strict is true, off should have already | |
7271 | * been 0. | |
7272 | */ | |
7273 | if (strict) | |
7274 | return false; | |
faaf4a79 | 7275 | again: |
22dc4a0f | 7276 | type = btf_type_by_id(btf, id); |
faaf4a79 JO |
7277 | if (!type) |
7278 | return false; | |
63260df1 | 7279 | err = btf_struct_walk(log, btf, type, off, 1, &id, &flag, NULL); |
faaf4a79 JO |
7280 | if (err != WALK_STRUCT) |
7281 | return false; | |
7282 | ||
7283 | /* We found nested struct object. If it matches | |
7284 | * the requested ID, we're done. Otherwise let's | |
7285 | * continue the search with offset 0 in the new | |
7286 | * type. | |
7287 | */ | |
22dc4a0f | 7288 | if (!btf_types_are_same(btf, id, need_btf, need_type_id)) { |
faaf4a79 JO |
7289 | off = 0; |
7290 | goto again; | |
7291 | } | |
7292 | ||
7293 | return true; | |
7294 | } | |
7295 | ||
fec56f58 | 7296 | static int __get_type_size(struct btf *btf, u32 btf_id, |
a00ed843 | 7297 | const struct btf_type **ret_type) |
fec56f58 AS |
7298 | { |
7299 | const struct btf_type *t; | |
7300 | ||
a00ed843 | 7301 | *ret_type = btf_type_by_id(btf, 0); |
fec56f58 AS |
7302 | if (!btf_id) |
7303 | /* void */ | |
7304 | return 0; | |
7305 | t = btf_type_by_id(btf, btf_id); | |
7306 | while (t && btf_type_is_modifier(t)) | |
7307 | t = btf_type_by_id(btf, t->type); | |
a00ed843 | 7308 | if (!t) |
fec56f58 | 7309 | return -EINVAL; |
a00ed843 | 7310 | *ret_type = t; |
fec56f58 AS |
7311 | if (btf_type_is_ptr(t)) |
7312 | /* kernel size of pointer. Not BPF's size of pointer*/ | |
7313 | return sizeof(void *); | |
720e6a43 | 7314 | if (btf_type_is_int(t) || btf_is_any_enum(t) || __btf_type_is_struct(t)) |
fec56f58 | 7315 | return t->size; |
fec56f58 AS |
7316 | return -EINVAL; |
7317 | } | |
7318 | ||
49f67f39 IL |
7319 | static u8 __get_type_fmodel_flags(const struct btf_type *t) |
7320 | { | |
7321 | u8 flags = 0; | |
7322 | ||
7323 | if (__btf_type_is_struct(t)) | |
7324 | flags |= BTF_FMODEL_STRUCT_ARG; | |
7325 | if (btf_type_is_signed_int(t)) | |
7326 | flags |= BTF_FMODEL_SIGNED_ARG; | |
7327 | ||
7328 | return flags; | |
7329 | } | |
7330 | ||
fec56f58 AS |
7331 | int btf_distill_func_proto(struct bpf_verifier_log *log, |
7332 | struct btf *btf, | |
7333 | const struct btf_type *func, | |
7334 | const char *tname, | |
7335 | struct btf_func_model *m) | |
7336 | { | |
7337 | const struct btf_param *args; | |
7338 | const struct btf_type *t; | |
7339 | u32 i, nargs; | |
7340 | int ret; | |
7341 | ||
5b92a28a AS |
7342 | if (!func) { |
7343 | /* BTF function prototype doesn't match the verifier types. | |
523a4cf4 | 7344 | * Fall back to MAX_BPF_FUNC_REG_ARGS u64 args. |
5b92a28a | 7345 | */ |
720e6a43 | 7346 | for (i = 0; i < MAX_BPF_FUNC_REG_ARGS; i++) { |
5b92a28a | 7347 | m->arg_size[i] = 8; |
720e6a43 YS |
7348 | m->arg_flags[i] = 0; |
7349 | } | |
5b92a28a | 7350 | m->ret_size = 8; |
49f67f39 | 7351 | m->ret_flags = 0; |
523a4cf4 | 7352 | m->nr_args = MAX_BPF_FUNC_REG_ARGS; |
5b92a28a AS |
7353 | return 0; |
7354 | } | |
fec56f58 AS |
7355 | args = (const struct btf_param *)(func + 1); |
7356 | nargs = btf_type_vlen(func); | |
c29a4920 | 7357 | if (nargs > MAX_BPF_FUNC_ARGS) { |
fec56f58 AS |
7358 | bpf_log(log, |
7359 | "The function %s has %d arguments. Too many.\n", | |
7360 | tname, nargs); | |
7361 | return -EINVAL; | |
7362 | } | |
7363 | ret = __get_type_size(btf, func->type, &t); | |
720e6a43 | 7364 | if (ret < 0 || __btf_type_is_struct(t)) { |
fec56f58 AS |
7365 | bpf_log(log, |
7366 | "The function %s return type %s is unsupported.\n", | |
571f9738 | 7367 | tname, btf_type_str(t)); |
fec56f58 AS |
7368 | return -EINVAL; |
7369 | } | |
7370 | m->ret_size = ret; | |
49f67f39 | 7371 | m->ret_flags = __get_type_fmodel_flags(t); |
fec56f58 AS |
7372 | |
7373 | for (i = 0; i < nargs; i++) { | |
31379397 JO |
7374 | if (i == nargs - 1 && args[i].type == 0) { |
7375 | bpf_log(log, | |
7376 | "The function %s with variable args is unsupported.\n", | |
7377 | tname); | |
7378 | return -EINVAL; | |
7379 | } | |
fec56f58 | 7380 | ret = __get_type_size(btf, args[i].type, &t); |
720e6a43 YS |
7381 | |
7382 | /* No support of struct argument size greater than 16 bytes */ | |
7383 | if (ret < 0 || ret > 16) { | |
fec56f58 AS |
7384 | bpf_log(log, |
7385 | "The function %s arg%d type %s is unsupported.\n", | |
571f9738 | 7386 | tname, i, btf_type_str(t)); |
fec56f58 AS |
7387 | return -EINVAL; |
7388 | } | |
31379397 JO |
7389 | if (ret == 0) { |
7390 | bpf_log(log, | |
7391 | "The function %s has malformed void argument.\n", | |
7392 | tname); | |
7393 | return -EINVAL; | |
7394 | } | |
fec56f58 | 7395 | m->arg_size[i] = ret; |
49f67f39 | 7396 | m->arg_flags[i] = __get_type_fmodel_flags(t); |
fec56f58 AS |
7397 | } |
7398 | m->nr_args = nargs; | |
7399 | return 0; | |
7400 | } | |
7401 | ||
be8704ff AS |
7402 | /* Compare BTFs of two functions assuming only scalars and pointers to context. |
7403 | * t1 points to BTF_KIND_FUNC in btf1 | |
7404 | * t2 points to BTF_KIND_FUNC in btf2 | |
7405 | * Returns: | |
7406 | * EINVAL - function prototype mismatch | |
7407 | * EFAULT - verifier bug | |
7408 | * 0 - 99% match. The last 1% is validated by the verifier. | |
7409 | */ | |
2bf0eb9b HY |
7410 | static int btf_check_func_type_match(struct bpf_verifier_log *log, |
7411 | struct btf *btf1, const struct btf_type *t1, | |
7412 | struct btf *btf2, const struct btf_type *t2) | |
be8704ff AS |
7413 | { |
7414 | const struct btf_param *args1, *args2; | |
7415 | const char *fn1, *fn2, *s1, *s2; | |
7416 | u32 nargs1, nargs2, i; | |
7417 | ||
7418 | fn1 = btf_name_by_offset(btf1, t1->name_off); | |
7419 | fn2 = btf_name_by_offset(btf2, t2->name_off); | |
7420 | ||
7421 | if (btf_func_linkage(t1) != BTF_FUNC_GLOBAL) { | |
7422 | bpf_log(log, "%s() is not a global function\n", fn1); | |
7423 | return -EINVAL; | |
7424 | } | |
7425 | if (btf_func_linkage(t2) != BTF_FUNC_GLOBAL) { | |
7426 | bpf_log(log, "%s() is not a global function\n", fn2); | |
7427 | return -EINVAL; | |
7428 | } | |
7429 | ||
7430 | t1 = btf_type_by_id(btf1, t1->type); | |
7431 | if (!t1 || !btf_type_is_func_proto(t1)) | |
7432 | return -EFAULT; | |
7433 | t2 = btf_type_by_id(btf2, t2->type); | |
7434 | if (!t2 || !btf_type_is_func_proto(t2)) | |
7435 | return -EFAULT; | |
7436 | ||
7437 | args1 = (const struct btf_param *)(t1 + 1); | |
7438 | nargs1 = btf_type_vlen(t1); | |
7439 | args2 = (const struct btf_param *)(t2 + 1); | |
7440 | nargs2 = btf_type_vlen(t2); | |
7441 | ||
7442 | if (nargs1 != nargs2) { | |
7443 | bpf_log(log, "%s() has %d args while %s() has %d args\n", | |
7444 | fn1, nargs1, fn2, nargs2); | |
7445 | return -EINVAL; | |
7446 | } | |
7447 | ||
7448 | t1 = btf_type_skip_modifiers(btf1, t1->type, NULL); | |
7449 | t2 = btf_type_skip_modifiers(btf2, t2->type, NULL); | |
7450 | if (t1->info != t2->info) { | |
7451 | bpf_log(log, | |
7452 | "Return type %s of %s() doesn't match type %s of %s()\n", | |
7453 | btf_type_str(t1), fn1, | |
7454 | btf_type_str(t2), fn2); | |
7455 | return -EINVAL; | |
7456 | } | |
7457 | ||
7458 | for (i = 0; i < nargs1; i++) { | |
7459 | t1 = btf_type_skip_modifiers(btf1, args1[i].type, NULL); | |
7460 | t2 = btf_type_skip_modifiers(btf2, args2[i].type, NULL); | |
7461 | ||
7462 | if (t1->info != t2->info) { | |
7463 | bpf_log(log, "arg%d in %s() is %s while %s() has %s\n", | |
7464 | i, fn1, btf_type_str(t1), | |
7465 | fn2, btf_type_str(t2)); | |
7466 | return -EINVAL; | |
7467 | } | |
7468 | if (btf_type_has_size(t1) && t1->size != t2->size) { | |
7469 | bpf_log(log, | |
7470 | "arg%d in %s() has size %d while %s() has %d\n", | |
7471 | i, fn1, t1->size, | |
7472 | fn2, t2->size); | |
7473 | return -EINVAL; | |
7474 | } | |
7475 | ||
7476 | /* global functions are validated with scalars and pointers | |
7477 | * to context only. And only global functions can be replaced. | |
7478 | * Hence type check only those types. | |
7479 | */ | |
6089fb32 | 7480 | if (btf_type_is_int(t1) || btf_is_any_enum(t1)) |
be8704ff AS |
7481 | continue; |
7482 | if (!btf_type_is_ptr(t1)) { | |
7483 | bpf_log(log, | |
7484 | "arg%d in %s() has unrecognized type\n", | |
7485 | i, fn1); | |
7486 | return -EINVAL; | |
7487 | } | |
7488 | t1 = btf_type_skip_modifiers(btf1, t1->type, NULL); | |
7489 | t2 = btf_type_skip_modifiers(btf2, t2->type, NULL); | |
7490 | if (!btf_type_is_struct(t1)) { | |
7491 | bpf_log(log, | |
7492 | "arg%d in %s() is not a pointer to context\n", | |
7493 | i, fn1); | |
7494 | return -EINVAL; | |
7495 | } | |
7496 | if (!btf_type_is_struct(t2)) { | |
7497 | bpf_log(log, | |
7498 | "arg%d in %s() is not a pointer to context\n", | |
7499 | i, fn2); | |
7500 | return -EINVAL; | |
7501 | } | |
7502 | /* This is an optional check to make program writing easier. | |
7503 | * Compare names of structs and report an error to the user. | |
7504 | * btf_prepare_func_args() already checked that t2 struct | |
7505 | * is a context type. btf_prepare_func_args() will check | |
7506 | * later that t1 struct is a context type as well. | |
7507 | */ | |
7508 | s1 = btf_name_by_offset(btf1, t1->name_off); | |
7509 | s2 = btf_name_by_offset(btf2, t2->name_off); | |
7510 | if (strcmp(s1, s2)) { | |
7511 | bpf_log(log, | |
7512 | "arg%d %s(struct %s *) doesn't match %s(struct %s *)\n", | |
7513 | i, fn1, s1, fn2, s2); | |
7514 | return -EINVAL; | |
7515 | } | |
7516 | } | |
7517 | return 0; | |
7518 | } | |
7519 | ||
7520 | /* Compare BTFs of given program with BTF of target program */ | |
efc68158 | 7521 | int btf_check_type_match(struct bpf_verifier_log *log, const struct bpf_prog *prog, |
be8704ff AS |
7522 | struct btf *btf2, const struct btf_type *t2) |
7523 | { | |
7524 | struct btf *btf1 = prog->aux->btf; | |
7525 | const struct btf_type *t1; | |
7526 | u32 btf_id = 0; | |
7527 | ||
7528 | if (!prog->aux->func_info) { | |
efc68158 | 7529 | bpf_log(log, "Program extension requires BTF\n"); |
be8704ff AS |
7530 | return -EINVAL; |
7531 | } | |
7532 | ||
7533 | btf_id = prog->aux->func_info[0].type_id; | |
7534 | if (!btf_id) | |
7535 | return -EFAULT; | |
7536 | ||
7537 | t1 = btf_type_by_id(btf1, btf_id); | |
7538 | if (!t1 || !btf_type_is_func(t1)) | |
7539 | return -EFAULT; | |
7540 | ||
efc68158 | 7541 | return btf_check_func_type_match(log, btf1, t1, btf2, t2); |
be8704ff AS |
7542 | } |
7543 | ||
a64bfe61 AN |
7544 | static bool btf_is_dynptr_ptr(const struct btf *btf, const struct btf_type *t) |
7545 | { | |
7546 | const char *name; | |
7547 | ||
7548 | t = btf_type_by_id(btf, t->type); /* skip PTR */ | |
7549 | ||
7550 | while (btf_type_is_modifier(t)) | |
7551 | t = btf_type_by_id(btf, t->type); | |
7552 | ||
7553 | /* allow either struct or struct forward declaration */ | |
7554 | if (btf_type_is_struct(t) || | |
7555 | (btf_type_is_fwd(t) && btf_type_kflag(t) == 0)) { | |
7556 | name = btf_str_by_offset(btf, t->name_off); | |
7557 | return name && strcmp(name, "bpf_dynptr") == 0; | |
7558 | } | |
7559 | ||
7560 | return false; | |
7561 | } | |
7562 | ||
e2b3c4ff AN |
7563 | struct bpf_cand_cache { |
7564 | const char *name; | |
7565 | u32 name_len; | |
7566 | u16 kind; | |
7567 | u16 cnt; | |
7568 | struct { | |
7569 | const struct btf *btf; | |
7570 | u32 id; | |
7571 | } cands[]; | |
7572 | }; | |
7573 | ||
7574 | static DEFINE_MUTEX(cand_cache_mutex); | |
7575 | ||
7576 | static struct bpf_cand_cache * | |
7577 | bpf_core_find_cands(struct bpf_core_ctx *ctx, u32 local_type_id); | |
7578 | ||
7579 | static int btf_get_ptr_to_btf_id(struct bpf_verifier_log *log, int arg_idx, | |
7580 | const struct btf *btf, const struct btf_type *t) | |
7581 | { | |
7582 | struct bpf_cand_cache *cc; | |
7583 | struct bpf_core_ctx ctx = { | |
7584 | .btf = btf, | |
7585 | .log = log, | |
7586 | }; | |
7587 | u32 kern_type_id, type_id; | |
7588 | int err = 0; | |
7589 | ||
7590 | /* skip PTR and modifiers */ | |
7591 | type_id = t->type; | |
7592 | t = btf_type_by_id(btf, t->type); | |
7593 | while (btf_type_is_modifier(t)) { | |
7594 | type_id = t->type; | |
7595 | t = btf_type_by_id(btf, t->type); | |
7596 | } | |
7597 | ||
7598 | mutex_lock(&cand_cache_mutex); | |
7599 | cc = bpf_core_find_cands(&ctx, type_id); | |
7600 | if (IS_ERR(cc)) { | |
7601 | err = PTR_ERR(cc); | |
7602 | bpf_log(log, "arg#%d reference type('%s %s') candidate matching error: %d\n", | |
7603 | arg_idx, btf_type_str(t), __btf_name_by_offset(btf, t->name_off), | |
7604 | err); | |
7605 | goto cand_cache_unlock; | |
7606 | } | |
7607 | if (cc->cnt != 1) { | |
7608 | bpf_log(log, "arg#%d reference type('%s %s') %s\n", | |
7609 | arg_idx, btf_type_str(t), __btf_name_by_offset(btf, t->name_off), | |
7610 | cc->cnt == 0 ? "has no matches" : "is ambiguous"); | |
7611 | err = cc->cnt == 0 ? -ENOENT : -ESRCH; | |
7612 | goto cand_cache_unlock; | |
7613 | } | |
7614 | if (btf_is_module(cc->cands[0].btf)) { | |
7615 | bpf_log(log, "arg#%d reference type('%s %s') points to kernel module type (unsupported)\n", | |
7616 | arg_idx, btf_type_str(t), __btf_name_by_offset(btf, t->name_off)); | |
7617 | err = -EOPNOTSUPP; | |
7618 | goto cand_cache_unlock; | |
7619 | } | |
7620 | kern_type_id = cc->cands[0].id; | |
7621 | ||
7622 | cand_cache_unlock: | |
7623 | mutex_unlock(&cand_cache_mutex); | |
7624 | if (err) | |
7625 | return err; | |
7626 | ||
7627 | return kern_type_id; | |
7628 | } | |
7629 | ||
54c11ec4 | 7630 | enum btf_arg_tag { |
2edc3de6 AS |
7631 | ARG_TAG_CTX = BIT_ULL(0), |
7632 | ARG_TAG_NONNULL = BIT_ULL(1), | |
7633 | ARG_TAG_TRUSTED = BIT_ULL(2), | |
7634 | ARG_TAG_NULLABLE = BIT_ULL(3), | |
7635 | ARG_TAG_ARENA = BIT_ULL(4), | |
54c11ec4 AN |
7636 | }; |
7637 | ||
4ba1d0f2 AN |
7638 | /* Process BTF of a function to produce high-level expectation of function |
7639 | * arguments (like ARG_PTR_TO_CTX, or ARG_PTR_TO_MEM, etc). This information | |
7640 | * is cached in subprog info for reuse. | |
51c39bb1 AS |
7641 | * Returns: |
7642 | * EFAULT - there is a verifier bug. Abort verification. | |
7643 | * EINVAL - cannot convert BTF. | |
4ba1d0f2 | 7644 | * 0 - Successfully processed BTF and constructed argument expectations. |
51c39bb1 | 7645 | */ |
4ba1d0f2 | 7646 | int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog) |
51c39bb1 | 7647 | { |
e26080d0 | 7648 | bool is_global = subprog_aux(env, subprog)->linkage == BTF_FUNC_GLOBAL; |
4ba1d0f2 | 7649 | struct bpf_subprog_info *sub = subprog_info(env, subprog); |
51c39bb1 AS |
7650 | struct bpf_verifier_log *log = &env->log; |
7651 | struct bpf_prog *prog = env->prog; | |
be8704ff | 7652 | enum bpf_prog_type prog_type = prog->type; |
51c39bb1 AS |
7653 | struct btf *btf = prog->aux->btf; |
7654 | const struct btf_param *args; | |
94e1c70a | 7655 | const struct btf_type *t, *ref_t, *fn_t; |
51c39bb1 AS |
7656 | u32 i, nargs, btf_id; |
7657 | const char *tname; | |
7658 | ||
4ba1d0f2 AN |
7659 | if (sub->args_cached) |
7660 | return 0; | |
7661 | ||
e26080d0 | 7662 | if (!prog->aux->func_info) { |
1cb0f56d | 7663 | verifier_bug(env, "func_info undefined"); |
51c39bb1 AS |
7664 | return -EFAULT; |
7665 | } | |
7666 | ||
7667 | btf_id = prog->aux->func_info[subprog].type_id; | |
7668 | if (!btf_id) { | |
e26080d0 AN |
7669 | if (!is_global) /* not fatal for static funcs */ |
7670 | return -EINVAL; | |
51c39bb1 AS |
7671 | bpf_log(log, "Global functions need valid BTF\n"); |
7672 | return -EFAULT; | |
7673 | } | |
7674 | ||
94e1c70a AN |
7675 | fn_t = btf_type_by_id(btf, btf_id); |
7676 | if (!fn_t || !btf_type_is_func(fn_t)) { | |
51c39bb1 AS |
7677 | /* These checks were already done by the verifier while loading |
7678 | * struct bpf_func_info | |
7679 | */ | |
7680 | bpf_log(log, "BTF of func#%d doesn't point to KIND_FUNC\n", | |
7681 | subprog); | |
7682 | return -EFAULT; | |
7683 | } | |
94e1c70a | 7684 | tname = btf_name_by_offset(btf, fn_t->name_off); |
51c39bb1 | 7685 | |
51c39bb1 | 7686 | if (prog->aux->func_info_aux[subprog].unreliable) { |
1cb0f56d | 7687 | verifier_bug(env, "unreliable BTF for function %s()", tname); |
51c39bb1 AS |
7688 | return -EFAULT; |
7689 | } | |
be8704ff | 7690 | if (prog_type == BPF_PROG_TYPE_EXT) |
3aac1ead | 7691 | prog_type = prog->aux->dst_prog->type; |
51c39bb1 | 7692 | |
94e1c70a | 7693 | t = btf_type_by_id(btf, fn_t->type); |
51c39bb1 AS |
7694 | if (!t || !btf_type_is_func_proto(t)) { |
7695 | bpf_log(log, "Invalid type of function %s()\n", tname); | |
7696 | return -EFAULT; | |
7697 | } | |
7698 | args = (const struct btf_param *)(t + 1); | |
7699 | nargs = btf_type_vlen(t); | |
523a4cf4 | 7700 | if (nargs > MAX_BPF_FUNC_REG_ARGS) { |
1eb98674 AN |
7701 | if (!is_global) |
7702 | return -EINVAL; | |
523a4cf4 DB |
7703 | bpf_log(log, "Global function %s() with %d > %d args. Buggy compiler.\n", |
7704 | tname, nargs, MAX_BPF_FUNC_REG_ARGS); | |
51c39bb1 AS |
7705 | return -EINVAL; |
7706 | } | |
b9ae0c9d | 7707 | /* check that function returns int, exception cb also requires this */ |
51c39bb1 AS |
7708 | t = btf_type_by_id(btf, t->type); |
7709 | while (btf_type_is_modifier(t)) | |
7710 | t = btf_type_by_id(btf, t->type); | |
6089fb32 | 7711 | if (!btf_type_is_int(t) && !btf_is_any_enum(t)) { |
1eb98674 AN |
7712 | if (!is_global) |
7713 | return -EINVAL; | |
51c39bb1 AS |
7714 | bpf_log(log, |
7715 | "Global function %s() doesn't return scalar. Only those are supported.\n", | |
7716 | tname); | |
7717 | return -EINVAL; | |
7718 | } | |
7719 | /* Convert BTF function arguments into verifier types. | |
7720 | * Only PTR_TO_CTX and SCALAR are supported atm. | |
7721 | */ | |
7722 | for (i = 0; i < nargs; i++) { | |
54c11ec4 | 7723 | u32 tags = 0; |
522bb2c1 | 7724 | int id = 0; |
94e1c70a | 7725 | |
94e1c70a AN |
7726 | /* 'arg:<tag>' decl_tag takes precedence over derivation of |
7727 | * register type from BTF type itself | |
7728 | */ | |
522bb2c1 AN |
7729 | while ((id = btf_find_next_decl_tag(btf, fn_t, i, "arg:", id)) > 0) { |
7730 | const struct btf_type *tag_t = btf_type_by_id(btf, id); | |
7731 | const char *tag = __btf_name_by_offset(btf, tag_t->name_off) + 4; | |
7732 | ||
94e1c70a AN |
7733 | /* disallow arg tags in static subprogs */ |
7734 | if (!is_global) { | |
7735 | bpf_log(log, "arg#%d type tag is not supported in static functions\n", i); | |
7736 | return -EOPNOTSUPP; | |
7737 | } | |
54c11ec4 | 7738 | |
94e1c70a | 7739 | if (strcmp(tag, "ctx") == 0) { |
54c11ec4 | 7740 | tags |= ARG_TAG_CTX; |
e2b3c4ff AN |
7741 | } else if (strcmp(tag, "trusted") == 0) { |
7742 | tags |= ARG_TAG_TRUSTED; | |
54c11ec4 AN |
7743 | } else if (strcmp(tag, "nonnull") == 0) { |
7744 | tags |= ARG_TAG_NONNULL; | |
8f2b44cd AN |
7745 | } else if (strcmp(tag, "nullable") == 0) { |
7746 | tags |= ARG_TAG_NULLABLE; | |
2edc3de6 AS |
7747 | } else if (strcmp(tag, "arena") == 0) { |
7748 | tags |= ARG_TAG_ARENA; | |
54c11ec4 AN |
7749 | } else { |
7750 | bpf_log(log, "arg#%d has unsupported set of tags\n", i); | |
7751 | return -EOPNOTSUPP; | |
94e1c70a | 7752 | } |
94e1c70a | 7753 | } |
522bb2c1 AN |
7754 | if (id != -ENOENT) { |
7755 | bpf_log(log, "arg#%d type tag fetching failure: %d\n", i, id); | |
7756 | return id; | |
7757 | } | |
94e1c70a | 7758 | |
54c11ec4 | 7759 | t = btf_type_by_id(btf, args[i].type); |
51c39bb1 AS |
7760 | while (btf_type_is_modifier(t)) |
7761 | t = btf_type_by_id(btf, t->type); | |
54c11ec4 AN |
7762 | if (!btf_type_is_ptr(t)) |
7763 | goto skip_pointer; | |
7764 | ||
fb5b86cf | 7765 | if ((tags & ARG_TAG_CTX) || btf_is_prog_ctx_type(log, btf, t, prog_type, i)) { |
54c11ec4 AN |
7766 | if (tags & ~ARG_TAG_CTX) { |
7767 | bpf_log(log, "arg#%d has invalid combination of tags\n", i); | |
7768 | return -EINVAL; | |
7769 | } | |
add9c58c AN |
7770 | if ((tags & ARG_TAG_CTX) && |
7771 | btf_validate_prog_ctx_type(log, btf, t, i, prog_type, | |
7772 | prog->expected_attach_type)) | |
7773 | return -EINVAL; | |
e26080d0 AN |
7774 | sub->args[i].arg_type = ARG_PTR_TO_CTX; |
7775 | continue; | |
7776 | } | |
54c11ec4 AN |
7777 | if (btf_is_dynptr_ptr(btf, t)) { |
7778 | if (tags) { | |
7779 | bpf_log(log, "arg#%d has invalid combination of tags\n", i); | |
7780 | return -EINVAL; | |
7781 | } | |
a64bfe61 AN |
7782 | sub->args[i].arg_type = ARG_PTR_TO_DYNPTR | MEM_RDONLY; |
7783 | continue; | |
7784 | } | |
e2b3c4ff AN |
7785 | if (tags & ARG_TAG_TRUSTED) { |
7786 | int kern_type_id; | |
7787 | ||
7788 | if (tags & ARG_TAG_NONNULL) { | |
7789 | bpf_log(log, "arg#%d has invalid combination of tags\n", i); | |
7790 | return -EINVAL; | |
7791 | } | |
7792 | ||
7793 | kern_type_id = btf_get_ptr_to_btf_id(log, i, btf, t); | |
7794 | if (kern_type_id < 0) | |
7795 | return kern_type_id; | |
7796 | ||
7797 | sub->args[i].arg_type = ARG_PTR_TO_BTF_ID | PTR_TRUSTED; | |
8f2b44cd AN |
7798 | if (tags & ARG_TAG_NULLABLE) |
7799 | sub->args[i].arg_type |= PTR_MAYBE_NULL; | |
e2b3c4ff AN |
7800 | sub->args[i].btf_id = kern_type_id; |
7801 | continue; | |
7802 | } | |
2edc3de6 AS |
7803 | if (tags & ARG_TAG_ARENA) { |
7804 | if (tags & ~ARG_TAG_ARENA) { | |
7805 | bpf_log(log, "arg#%d arena cannot be combined with any other tags\n", i); | |
7806 | return -EINVAL; | |
7807 | } | |
7808 | sub->args[i].arg_type = ARG_PTR_TO_ARENA; | |
7809 | continue; | |
7810 | } | |
54c11ec4 | 7811 | if (is_global) { /* generic user data pointer */ |
4ba1d0f2 AN |
7812 | u32 mem_size; |
7813 | ||
8f2b44cd AN |
7814 | if (tags & ARG_TAG_NULLABLE) { |
7815 | bpf_log(log, "arg#%d has invalid combination of tags\n", i); | |
7816 | return -EINVAL; | |
7817 | } | |
7818 | ||
e5069b9c | 7819 | t = btf_type_skip_modifiers(btf, t->type, NULL); |
4ba1d0f2 | 7820 | ref_t = btf_resolve_size(btf, t, &mem_size); |
e5069b9c | 7821 | if (IS_ERR(ref_t)) { |
54c11ec4 AN |
7822 | bpf_log(log, "arg#%d reference type('%s %s') size cannot be determined: %ld\n", |
7823 | i, btf_type_str(t), btf_name_by_offset(btf, t->name_off), | |
e5069b9c DB |
7824 | PTR_ERR(ref_t)); |
7825 | return -EINVAL; | |
7826 | } | |
7827 | ||
54c11ec4 AN |
7828 | sub->args[i].arg_type = ARG_PTR_TO_MEM | PTR_MAYBE_NULL; |
7829 | if (tags & ARG_TAG_NONNULL) | |
7830 | sub->args[i].arg_type &= ~PTR_MAYBE_NULL; | |
4ba1d0f2 | 7831 | sub->args[i].mem_size = mem_size; |
51c39bb1 AS |
7832 | continue; |
7833 | } | |
54c11ec4 AN |
7834 | |
7835 | skip_pointer: | |
7836 | if (tags) { | |
7837 | bpf_log(log, "arg#%d has pointer tag, but is not a pointer type\n", i); | |
94e1c70a AN |
7838 | return -EINVAL; |
7839 | } | |
18810ad3 AN |
7840 | if (btf_type_is_int(t) || btf_is_any_enum(t)) { |
7841 | sub->args[i].arg_type = ARG_ANYTHING; | |
7842 | continue; | |
7843 | } | |
1eb98674 AN |
7844 | if (!is_global) |
7845 | return -EINVAL; | |
51c39bb1 | 7846 | bpf_log(log, "Arg#%d type %s in %s() is not supported yet.\n", |
571f9738 | 7847 | i, btf_type_str(t), tname); |
51c39bb1 AS |
7848 | return -EINVAL; |
7849 | } | |
4ba1d0f2 AN |
7850 | |
7851 | sub->arg_cnt = nargs; | |
7852 | sub->args_cached = true; | |
7853 | ||
8c1b6e69 AS |
7854 | return 0; |
7855 | } | |
7856 | ||
31d0bc81 AM |
7857 | static void btf_type_show(const struct btf *btf, u32 type_id, void *obj, |
7858 | struct btf_show *show) | |
7859 | { | |
7860 | const struct btf_type *t = btf_type_by_id(btf, type_id); | |
7861 | ||
7862 | show->btf = btf; | |
7863 | memset(&show->state, 0, sizeof(show->state)); | |
7864 | memset(&show->obj, 0, sizeof(show->obj)); | |
7865 | ||
7866 | btf_type_ops(t)->show(btf, t, type_id, obj, 0, show); | |
7867 | } | |
7868 | ||
b3470da3 AM |
7869 | __printf(2, 0) static void btf_seq_show(struct btf_show *show, const char *fmt, |
7870 | va_list args) | |
31d0bc81 AM |
7871 | { |
7872 | seq_vprintf((struct seq_file *)show->target, fmt, args); | |
7873 | } | |
7874 | ||
eb411377 AM |
7875 | int btf_type_seq_show_flags(const struct btf *btf, u32 type_id, |
7876 | void *obj, struct seq_file *m, u64 flags) | |
31d0bc81 AM |
7877 | { |
7878 | struct btf_show sseq; | |
7879 | ||
7880 | sseq.target = m; | |
7881 | sseq.showfn = btf_seq_show; | |
7882 | sseq.flags = flags; | |
7883 | ||
7884 | btf_type_show(btf, type_id, obj, &sseq); | |
7885 | ||
7886 | return sseq.state.status; | |
7887 | } | |
7888 | ||
b00b8dae MKL |
7889 | void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj, |
7890 | struct seq_file *m) | |
7891 | { | |
31d0bc81 AM |
7892 | (void) btf_type_seq_show_flags(btf, type_id, obj, m, |
7893 | BTF_SHOW_NONAME | BTF_SHOW_COMPACT | | |
7894 | BTF_SHOW_ZERO | BTF_SHOW_UNSAFE); | |
7895 | } | |
7896 | ||
7897 | struct btf_show_snprintf { | |
7898 | struct btf_show show; | |
7899 | int len_left; /* space left in string */ | |
7900 | int len; /* length we would have written */ | |
7901 | }; | |
7902 | ||
b3470da3 AM |
7903 | __printf(2, 0) static void btf_snprintf_show(struct btf_show *show, const char *fmt, |
7904 | va_list args) | |
31d0bc81 AM |
7905 | { |
7906 | struct btf_show_snprintf *ssnprintf = (struct btf_show_snprintf *)show; | |
7907 | int len; | |
7908 | ||
7909 | len = vsnprintf(show->target, ssnprintf->len_left, fmt, args); | |
7910 | ||
7911 | if (len < 0) { | |
7912 | ssnprintf->len_left = 0; | |
7913 | ssnprintf->len = len; | |
58250ae3 | 7914 | } else if (len >= ssnprintf->len_left) { |
31d0bc81 AM |
7915 | /* no space, drive on to get length we would have written */ |
7916 | ssnprintf->len_left = 0; | |
7917 | ssnprintf->len += len; | |
7918 | } else { | |
7919 | ssnprintf->len_left -= len; | |
7920 | ssnprintf->len += len; | |
7921 | show->target += len; | |
7922 | } | |
7923 | } | |
7924 | ||
7925 | int btf_type_snprintf_show(const struct btf *btf, u32 type_id, void *obj, | |
7926 | char *buf, int len, u64 flags) | |
7927 | { | |
7928 | struct btf_show_snprintf ssnprintf; | |
7929 | ||
7930 | ssnprintf.show.target = buf; | |
7931 | ssnprintf.show.flags = flags; | |
7932 | ssnprintf.show.showfn = btf_snprintf_show; | |
7933 | ssnprintf.len_left = len; | |
7934 | ssnprintf.len = 0; | |
7935 | ||
7936 | btf_type_show(btf, type_id, obj, (struct btf_show *)&ssnprintf); | |
7937 | ||
c561d110 | 7938 | /* If we encountered an error, return it. */ |
31d0bc81 AM |
7939 | if (ssnprintf.show.state.status) |
7940 | return ssnprintf.show.state.status; | |
b00b8dae | 7941 | |
31d0bc81 AM |
7942 | /* Otherwise return length we would have written */ |
7943 | return ssnprintf.len; | |
b00b8dae | 7944 | } |
f56a653c | 7945 | |
3481e64b QM |
7946 | #ifdef CONFIG_PROC_FS |
7947 | static void bpf_btf_show_fdinfo(struct seq_file *m, struct file *filp) | |
7948 | { | |
7949 | const struct btf *btf = filp->private_data; | |
7950 | ||
7951 | seq_printf(m, "btf_id:\t%u\n", btf->id); | |
7952 | } | |
7953 | #endif | |
7954 | ||
f56a653c MKL |
7955 | static int btf_release(struct inode *inode, struct file *filp) |
7956 | { | |
7957 | btf_put(filp->private_data); | |
7958 | return 0; | |
7959 | } | |
7960 | ||
60197cfb | 7961 | const struct file_operations btf_fops = { |
3481e64b QM |
7962 | #ifdef CONFIG_PROC_FS |
7963 | .show_fdinfo = bpf_btf_show_fdinfo, | |
7964 | #endif | |
f56a653c MKL |
7965 | .release = btf_release, |
7966 | }; | |
7967 | ||
78958fca MKL |
7968 | static int __btf_new_fd(struct btf *btf) |
7969 | { | |
7970 | return anon_inode_getfd("btf", &btf_fops, btf, O_RDONLY | O_CLOEXEC); | |
7971 | } | |
7972 | ||
47a71c1f | 7973 | int btf_new_fd(const union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size) |
f56a653c MKL |
7974 | { |
7975 | struct btf *btf; | |
78958fca | 7976 | int ret; |
f56a653c | 7977 | |
47a71c1f | 7978 | btf = btf_parse(attr, uattr, uattr_size); |
f56a653c MKL |
7979 | if (IS_ERR(btf)) |
7980 | return PTR_ERR(btf); | |
7981 | ||
78958fca MKL |
7982 | ret = btf_alloc_id(btf); |
7983 | if (ret) { | |
7984 | btf_free(btf); | |
7985 | return ret; | |
7986 | } | |
7987 | ||
7988 | /* | |
7989 | * The BTF ID is published to the userspace. | |
7990 | * All BTF free must go through call_rcu() from | |
7991 | * now on (i.e. free by calling btf_put()). | |
7992 | */ | |
7993 | ||
7994 | ret = __btf_new_fd(btf); | |
7995 | if (ret < 0) | |
f56a653c MKL |
7996 | btf_put(btf); |
7997 | ||
78958fca | 7998 | return ret; |
f56a653c MKL |
7999 | } |
8000 | ||
8001 | struct btf *btf_get_by_fd(int fd) | |
8002 | { | |
8003 | struct btf *btf; | |
eb80ee85 | 8004 | CLASS(fd, f)(fd); |
f56a653c | 8005 | |
4e885fab AP |
8006 | btf = __btf_get_by_fd(f); |
8007 | if (!IS_ERR(btf)) | |
8008 | refcount_inc(&btf->refcnt); | |
f56a653c MKL |
8009 | |
8010 | return btf; | |
8011 | } | |
60197cfb MKL |
8012 | |
8013 | int btf_get_info_by_fd(const struct btf *btf, | |
8014 | const union bpf_attr *attr, | |
8015 | union bpf_attr __user *uattr) | |
8016 | { | |
62dab84c | 8017 | struct bpf_btf_info __user *uinfo; |
5c6f2588 | 8018 | struct bpf_btf_info info; |
62dab84c MKL |
8019 | u32 info_copy, btf_copy; |
8020 | void __user *ubtf; | |
53297220 AN |
8021 | char __user *uname; |
8022 | u32 uinfo_len, uname_len, name_len; | |
8023 | int ret = 0; | |
60197cfb | 8024 | |
62dab84c MKL |
8025 | uinfo = u64_to_user_ptr(attr->info.info); |
8026 | uinfo_len = attr->info.info_len; | |
8027 | ||
8028 | info_copy = min_t(u32, uinfo_len, sizeof(info)); | |
5c6f2588 | 8029 | memset(&info, 0, sizeof(info)); |
62dab84c MKL |
8030 | if (copy_from_user(&info, uinfo, info_copy)) |
8031 | return -EFAULT; | |
8032 | ||
8033 | info.id = btf->id; | |
8034 | ubtf = u64_to_user_ptr(info.btf); | |
8035 | btf_copy = min_t(u32, btf->data_size, info.btf_size); | |
8036 | if (copy_to_user(ubtf, btf->data, btf_copy)) | |
8037 | return -EFAULT; | |
8038 | info.btf_size = btf->data_size; | |
8039 | ||
53297220 AN |
8040 | info.kernel_btf = btf->kernel_btf; |
8041 | ||
8042 | uname = u64_to_user_ptr(info.name); | |
8043 | uname_len = info.name_len; | |
8044 | if (!uname ^ !uname_len) | |
8045 | return -EINVAL; | |
8046 | ||
8047 | name_len = strlen(btf->name); | |
8048 | info.name_len = name_len; | |
8049 | ||
8050 | if (uname) { | |
8051 | if (uname_len >= name_len + 1) { | |
8052 | if (copy_to_user(uname, btf->name, name_len + 1)) | |
8053 | return -EFAULT; | |
8054 | } else { | |
8055 | char zero = '\0'; | |
8056 | ||
8057 | if (copy_to_user(uname, btf->name, uname_len - 1)) | |
8058 | return -EFAULT; | |
8059 | if (put_user(zero, uname + uname_len - 1)) | |
8060 | return -EFAULT; | |
8061 | /* let user-space know about too short buffer */ | |
8062 | ret = -ENOSPC; | |
8063 | } | |
8064 | } | |
8065 | ||
62dab84c MKL |
8066 | if (copy_to_user(uinfo, &info, info_copy) || |
8067 | put_user(info_copy, &uattr->info.info_len)) | |
60197cfb MKL |
8068 | return -EFAULT; |
8069 | ||
53297220 | 8070 | return ret; |
60197cfb | 8071 | } |
78958fca MKL |
8072 | |
8073 | int btf_get_fd_by_id(u32 id) | |
8074 | { | |
8075 | struct btf *btf; | |
8076 | int fd; | |
8077 | ||
8078 | rcu_read_lock(); | |
8079 | btf = idr_find(&btf_idr, id); | |
8080 | if (!btf || !refcount_inc_not_zero(&btf->refcnt)) | |
8081 | btf = ERR_PTR(-ENOENT); | |
8082 | rcu_read_unlock(); | |
8083 | ||
8084 | if (IS_ERR(btf)) | |
8085 | return PTR_ERR(btf); | |
8086 | ||
8087 | fd = __btf_new_fd(btf); | |
8088 | if (fd < 0) | |
8089 | btf_put(btf); | |
8090 | ||
8091 | return fd; | |
8092 | } | |
8093 | ||
22dc4a0f | 8094 | u32 btf_obj_id(const struct btf *btf) |
78958fca MKL |
8095 | { |
8096 | return btf->id; | |
8097 | } | |
eae2e83e | 8098 | |
290248a5 AN |
8099 | bool btf_is_kernel(const struct btf *btf) |
8100 | { | |
8101 | return btf->kernel_btf; | |
8102 | } | |
8103 | ||
541c3bad AN |
8104 | bool btf_is_module(const struct btf *btf) |
8105 | { | |
8106 | return btf->kernel_btf && strcmp(btf->name, "vmlinux") != 0; | |
8107 | } | |
8108 | ||
18688de2 KKD |
8109 | enum { |
8110 | BTF_MODULE_F_LIVE = (1 << 0), | |
8111 | }; | |
8112 | ||
36e68442 AN |
8113 | #ifdef CONFIG_DEBUG_INFO_BTF_MODULES |
8114 | struct btf_module { | |
8115 | struct list_head list; | |
8116 | struct module *module; | |
8117 | struct btf *btf; | |
8118 | struct bin_attribute *sysfs_attr; | |
18688de2 | 8119 | int flags; |
36e68442 AN |
8120 | }; |
8121 | ||
8122 | static LIST_HEAD(btf_modules); | |
8123 | static DEFINE_MUTEX(btf_module_mutex); | |
8124 | ||
1e89106d AS |
8125 | static void purge_cand_cache(struct btf *btf); |
8126 | ||
36e68442 AN |
8127 | static int btf_module_notify(struct notifier_block *nb, unsigned long op, |
8128 | void *module) | |
8129 | { | |
8130 | struct btf_module *btf_mod, *tmp; | |
8131 | struct module *mod = module; | |
8132 | struct btf *btf; | |
8133 | int err = 0; | |
8134 | ||
8135 | if (mod->btf_data_size == 0 || | |
18688de2 KKD |
8136 | (op != MODULE_STATE_COMING && op != MODULE_STATE_LIVE && |
8137 | op != MODULE_STATE_GOING)) | |
36e68442 AN |
8138 | goto out; |
8139 | ||
8140 | switch (op) { | |
8141 | case MODULE_STATE_COMING: | |
8142 | btf_mod = kzalloc(sizeof(*btf_mod), GFP_KERNEL); | |
8143 | if (!btf_mod) { | |
8144 | err = -ENOMEM; | |
8145 | goto out; | |
8146 | } | |
8646db23 AM |
8147 | btf = btf_parse_module(mod->name, mod->btf_data, mod->btf_data_size, |
8148 | mod->btf_base_data, mod->btf_base_data_size); | |
36e68442 | 8149 | if (IS_ERR(btf)) { |
36e68442 | 8150 | kfree(btf_mod); |
9cb61e50 CB |
8151 | if (!IS_ENABLED(CONFIG_MODULE_ALLOW_BTF_MISMATCH)) { |
8152 | pr_warn("failed to validate module [%s] BTF: %ld\n", | |
8153 | mod->name, PTR_ERR(btf)); | |
5e214f2e | 8154 | err = PTR_ERR(btf); |
9cb61e50 CB |
8155 | } else { |
8156 | pr_warn_once("Kernel module BTF mismatch detected, BTF debug info may be unavailable for some modules\n"); | |
8157 | } | |
36e68442 AN |
8158 | goto out; |
8159 | } | |
8160 | err = btf_alloc_id(btf); | |
8161 | if (err) { | |
8162 | btf_free(btf); | |
8163 | kfree(btf_mod); | |
8164 | goto out; | |
8165 | } | |
8166 | ||
1e89106d | 8167 | purge_cand_cache(NULL); |
36e68442 AN |
8168 | mutex_lock(&btf_module_mutex); |
8169 | btf_mod->module = module; | |
8170 | btf_mod->btf = btf; | |
8171 | list_add(&btf_mod->list, &btf_modules); | |
8172 | mutex_unlock(&btf_module_mutex); | |
8173 | ||
8174 | if (IS_ENABLED(CONFIG_SYSFS)) { | |
8175 | struct bin_attribute *attr; | |
8176 | ||
8177 | attr = kzalloc(sizeof(*attr), GFP_KERNEL); | |
8178 | if (!attr) | |
8179 | goto out; | |
8180 | ||
8181 | sysfs_bin_attr_init(attr); | |
8182 | attr->attr.name = btf->name; | |
8183 | attr->attr.mode = 0444; | |
8184 | attr->size = btf->data_size; | |
18032c6b TW |
8185 | attr->private = btf->data; |
8186 | attr->read_new = sysfs_bin_attr_simple_read; | |
36e68442 AN |
8187 | |
8188 | err = sysfs_create_bin_file(btf_kobj, attr); | |
8189 | if (err) { | |
8190 | pr_warn("failed to register module [%s] BTF in sysfs: %d\n", | |
8191 | mod->name, err); | |
8192 | kfree(attr); | |
8193 | err = 0; | |
8194 | goto out; | |
8195 | } | |
8196 | ||
8197 | btf_mod->sysfs_attr = attr; | |
8198 | } | |
8199 | ||
18688de2 KKD |
8200 | break; |
8201 | case MODULE_STATE_LIVE: | |
8202 | mutex_lock(&btf_module_mutex); | |
8203 | list_for_each_entry_safe(btf_mod, tmp, &btf_modules, list) { | |
8204 | if (btf_mod->module != module) | |
8205 | continue; | |
8206 | ||
8207 | btf_mod->flags |= BTF_MODULE_F_LIVE; | |
8208 | break; | |
8209 | } | |
8210 | mutex_unlock(&btf_module_mutex); | |
36e68442 AN |
8211 | break; |
8212 | case MODULE_STATE_GOING: | |
8213 | mutex_lock(&btf_module_mutex); | |
8214 | list_for_each_entry_safe(btf_mod, tmp, &btf_modules, list) { | |
8215 | if (btf_mod->module != module) | |
8216 | continue; | |
8217 | ||
8218 | list_del(&btf_mod->list); | |
8219 | if (btf_mod->sysfs_attr) | |
8220 | sysfs_remove_bin_file(btf_kobj, btf_mod->sysfs_attr); | |
1e89106d | 8221 | purge_cand_cache(btf_mod->btf); |
36e68442 AN |
8222 | btf_put(btf_mod->btf); |
8223 | kfree(btf_mod->sysfs_attr); | |
8224 | kfree(btf_mod); | |
8225 | break; | |
8226 | } | |
8227 | mutex_unlock(&btf_module_mutex); | |
8228 | break; | |
8229 | } | |
8230 | out: | |
8231 | return notifier_from_errno(err); | |
8232 | } | |
8233 | ||
8234 | static struct notifier_block btf_module_nb = { | |
8235 | .notifier_call = btf_module_notify, | |
8236 | }; | |
8237 | ||
8238 | static int __init btf_module_init(void) | |
8239 | { | |
8240 | register_module_notifier(&btf_module_nb); | |
8241 | return 0; | |
8242 | } | |
8243 | ||
8244 | fs_initcall(btf_module_init); | |
8245 | #endif /* CONFIG_DEBUG_INFO_BTF_MODULES */ | |
541c3bad AN |
8246 | |
8247 | struct module *btf_try_get_module(const struct btf *btf) | |
8248 | { | |
8249 | struct module *res = NULL; | |
8250 | #ifdef CONFIG_DEBUG_INFO_BTF_MODULES | |
8251 | struct btf_module *btf_mod, *tmp; | |
8252 | ||
8253 | mutex_lock(&btf_module_mutex); | |
8254 | list_for_each_entry_safe(btf_mod, tmp, &btf_modules, list) { | |
8255 | if (btf_mod->btf != btf) | |
8256 | continue; | |
8257 | ||
18688de2 KKD |
8258 | /* We must only consider module whose __init routine has |
8259 | * finished, hence we must check for BTF_MODULE_F_LIVE flag, | |
8260 | * which is set from the notifier callback for | |
8261 | * MODULE_STATE_LIVE. | |
8262 | */ | |
8263 | if ((btf_mod->flags & BTF_MODULE_F_LIVE) && try_module_get(btf_mod->module)) | |
541c3bad AN |
8264 | res = btf_mod->module; |
8265 | ||
8266 | break; | |
8267 | } | |
8268 | mutex_unlock(&btf_module_mutex); | |
8269 | #endif | |
8270 | ||
8271 | return res; | |
8272 | } | |
3d78417b | 8273 | |
9492450f KKD |
8274 | /* Returns struct btf corresponding to the struct module. |
8275 | * This function can return NULL or ERR_PTR. | |
dee872e1 KKD |
8276 | */ |
8277 | static struct btf *btf_get_module_btf(const struct module *module) | |
8278 | { | |
dee872e1 KKD |
8279 | #ifdef CONFIG_DEBUG_INFO_BTF_MODULES |
8280 | struct btf_module *btf_mod, *tmp; | |
8281 | #endif | |
9492450f KKD |
8282 | struct btf *btf = NULL; |
8283 | ||
8284 | if (!module) { | |
8285 | btf = bpf_get_btf_vmlinux(); | |
7ada3787 | 8286 | if (!IS_ERR_OR_NULL(btf)) |
9492450f KKD |
8287 | btf_get(btf); |
8288 | return btf; | |
8289 | } | |
dee872e1 | 8290 | |
dee872e1 KKD |
8291 | #ifdef CONFIG_DEBUG_INFO_BTF_MODULES |
8292 | mutex_lock(&btf_module_mutex); | |
8293 | list_for_each_entry_safe(btf_mod, tmp, &btf_modules, list) { | |
8294 | if (btf_mod->module != module) | |
8295 | continue; | |
8296 | ||
8297 | btf_get(btf_mod->btf); | |
8298 | btf = btf_mod->btf; | |
8299 | break; | |
8300 | } | |
8301 | mutex_unlock(&btf_module_mutex); | |
8302 | #endif | |
8303 | ||
8304 | return btf; | |
8305 | } | |
8306 | ||
9e60b0e0 GT |
8307 | static int check_btf_kconfigs(const struct module *module, const char *feature) |
8308 | { | |
8309 | if (!module && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) { | |
8310 | pr_err("missing vmlinux BTF, cannot register %s\n", feature); | |
8311 | return -ENOENT; | |
8312 | } | |
8313 | if (module && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES)) | |
8314 | pr_warn("missing module BTF, cannot register %s\n", feature); | |
8315 | return 0; | |
8316 | } | |
8317 | ||
3d78417b AS |
8318 | BPF_CALL_4(bpf_btf_find_by_name_kind, char *, name, int, name_sz, u32, kind, int, flags) |
8319 | { | |
edc3ec09 KKD |
8320 | struct btf *btf = NULL; |
8321 | int btf_obj_fd = 0; | |
3d78417b AS |
8322 | long ret; |
8323 | ||
8324 | if (flags) | |
8325 | return -EINVAL; | |
8326 | ||
8327 | if (name_sz <= 1 || name[name_sz - 1]) | |
8328 | return -EINVAL; | |
8329 | ||
edc3ec09 KKD |
8330 | ret = bpf_find_btf_id(name, kind, &btf); |
8331 | if (ret > 0 && btf_is_module(btf)) { | |
8332 | btf_obj_fd = __btf_new_fd(btf); | |
8333 | if (btf_obj_fd < 0) { | |
8334 | btf_put(btf); | |
8335 | return btf_obj_fd; | |
3d78417b | 8336 | } |
edc3ec09 | 8337 | return ret | (((u64)btf_obj_fd) << 32); |
3d78417b | 8338 | } |
edc3ec09 KKD |
8339 | if (ret > 0) |
8340 | btf_put(btf); | |
3d78417b AS |
8341 | return ret; |
8342 | } | |
8343 | ||
8344 | const struct bpf_func_proto bpf_btf_find_by_name_kind_proto = { | |
8345 | .func = bpf_btf_find_by_name_kind, | |
8346 | .gpl_only = false, | |
8347 | .ret_type = RET_INTEGER, | |
216e3cd2 | 8348 | .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY, |
3d78417b AS |
8349 | .arg2_type = ARG_CONST_SIZE, |
8350 | .arg3_type = ARG_ANYTHING, | |
8351 | .arg4_type = ARG_ANYTHING, | |
8352 | }; | |
eb529c5b | 8353 | |
d19ddb47 SL |
8354 | BTF_ID_LIST_GLOBAL(btf_tracing_ids, MAX_BTF_TRACING_TYPE) |
8355 | #define BTF_TRACING_TYPE(name, type) BTF_ID(struct, type) | |
8356 | BTF_TRACING_TYPE_xxx | |
8357 | #undef BTF_TRACING_TYPE | |
14f267d9 | 8358 | |
496ddd19 AN |
8359 | /* Validate well-formedness of iter argument type. |
8360 | * On success, return positive BTF ID of iter state's STRUCT type. | |
8361 | * On error, negative error is returned. | |
8362 | */ | |
8363 | int btf_check_iter_arg(struct btf *btf, const struct btf_type *func, int arg_idx) | |
8364 | { | |
8365 | const struct btf_param *arg; | |
8366 | const struct btf_type *t; | |
8367 | const char *name; | |
8368 | int btf_id; | |
8369 | ||
8370 | if (btf_type_vlen(func) <= arg_idx) | |
8371 | return -EINVAL; | |
8372 | ||
8373 | arg = &btf_params(func)[arg_idx]; | |
8374 | t = btf_type_skip_modifiers(btf, arg->type, NULL); | |
8375 | if (!t || !btf_type_is_ptr(t)) | |
8376 | return -EINVAL; | |
8377 | t = btf_type_skip_modifiers(btf, t->type, &btf_id); | |
8378 | if (!t || !__btf_type_is_struct(t)) | |
8379 | return -EINVAL; | |
8380 | ||
8381 | name = btf_name_by_offset(btf, t->name_off); | |
8382 | if (!name || strncmp(name, ITER_PREFIX, sizeof(ITER_PREFIX) - 1)) | |
8383 | return -EINVAL; | |
8384 | ||
8385 | return btf_id; | |
8386 | } | |
8387 | ||
215bf496 AN |
8388 | static int btf_check_iter_kfuncs(struct btf *btf, const char *func_name, |
8389 | const struct btf_type *func, u32 func_flags) | |
8390 | { | |
8391 | u32 flags = func_flags & (KF_ITER_NEW | KF_ITER_NEXT | KF_ITER_DESTROY); | |
496ddd19 | 8392 | const char *sfx, *iter_name; |
215bf496 AN |
8393 | const struct btf_type *t; |
8394 | char exp_name[128]; | |
8395 | u32 nr_args; | |
496ddd19 | 8396 | int btf_id; |
215bf496 AN |
8397 | |
8398 | /* exactly one of KF_ITER_{NEW,NEXT,DESTROY} can be set */ | |
8399 | if (!flags || (flags & (flags - 1))) | |
8400 | return -EINVAL; | |
8401 | ||
8402 | /* any BPF iter kfunc should have `struct bpf_iter_<type> *` first arg */ | |
8403 | nr_args = btf_type_vlen(func); | |
8404 | if (nr_args < 1) | |
8405 | return -EINVAL; | |
8406 | ||
496ddd19 AN |
8407 | btf_id = btf_check_iter_arg(btf, func, 0); |
8408 | if (btf_id < 0) | |
8409 | return btf_id; | |
215bf496 AN |
8410 | |
8411 | /* sizeof(struct bpf_iter_<type>) should be a multiple of 8 to | |
8412 | * fit nicely in stack slots | |
8413 | */ | |
496ddd19 | 8414 | t = btf_type_by_id(btf, btf_id); |
215bf496 AN |
8415 | if (t->size == 0 || (t->size % 8)) |
8416 | return -EINVAL; | |
8417 | ||
8418 | /* validate bpf_iter_<type>_{new,next,destroy}(struct bpf_iter_<type> *) | |
8419 | * naming pattern | |
8420 | */ | |
496ddd19 | 8421 | iter_name = btf_name_by_offset(btf, t->name_off) + sizeof(ITER_PREFIX) - 1; |
215bf496 AN |
8422 | if (flags & KF_ITER_NEW) |
8423 | sfx = "new"; | |
8424 | else if (flags & KF_ITER_NEXT) | |
8425 | sfx = "next"; | |
8426 | else /* (flags & KF_ITER_DESTROY) */ | |
8427 | sfx = "destroy"; | |
8428 | ||
8429 | snprintf(exp_name, sizeof(exp_name), "bpf_iter_%s_%s", iter_name, sfx); | |
8430 | if (strcmp(func_name, exp_name)) | |
8431 | return -EINVAL; | |
8432 | ||
8433 | /* only iter constructor should have extra arguments */ | |
8434 | if (!(flags & KF_ITER_NEW) && nr_args != 1) | |
8435 | return -EINVAL; | |
8436 | ||
8437 | if (flags & KF_ITER_NEXT) { | |
8438 | /* bpf_iter_<type>_next() should return pointer */ | |
8439 | t = btf_type_skip_modifiers(btf, func->type, NULL); | |
8440 | if (!t || !btf_type_is_ptr(t)) | |
8441 | return -EINVAL; | |
8442 | } | |
8443 | ||
8444 | if (flags & KF_ITER_DESTROY) { | |
8445 | /* bpf_iter_<type>_destroy() should return void */ | |
8446 | t = btf_type_by_id(btf, func->type); | |
8447 | if (!t || !btf_type_is_void(t)) | |
8448 | return -EINVAL; | |
8449 | } | |
8450 | ||
8451 | return 0; | |
8452 | } | |
8453 | ||
8454 | static int btf_check_kfunc_protos(struct btf *btf, u32 func_id, u32 func_flags) | |
8455 | { | |
8456 | const struct btf_type *func; | |
8457 | const char *func_name; | |
8458 | int err; | |
8459 | ||
8460 | /* any kfunc should be FUNC -> FUNC_PROTO */ | |
8461 | func = btf_type_by_id(btf, func_id); | |
8462 | if (!func || !btf_type_is_func(func)) | |
8463 | return -EINVAL; | |
8464 | ||
8465 | /* sanity check kfunc name */ | |
8466 | func_name = btf_name_by_offset(btf, func->name_off); | |
8467 | if (!func_name || !func_name[0]) | |
8468 | return -EINVAL; | |
8469 | ||
8470 | func = btf_type_by_id(btf, func->type); | |
8471 | if (!func || !btf_type_is_func_proto(func)) | |
8472 | return -EINVAL; | |
8473 | ||
8474 | if (func_flags & (KF_ITER_NEW | KF_ITER_NEXT | KF_ITER_DESTROY)) { | |
8475 | err = btf_check_iter_kfuncs(btf, func_name, func, func_flags); | |
8476 | if (err) | |
8477 | return err; | |
8478 | } | |
8479 | ||
8480 | return 0; | |
8481 | } | |
8482 | ||
dee872e1 | 8483 | /* Kernel Function (kfunc) BTF ID set registration API */ |
14f267d9 | 8484 | |
a4703e31 | 8485 | static int btf_populate_kfunc_set(struct btf *btf, enum btf_kfunc_hook hook, |
e924e80e | 8486 | const struct btf_kfunc_id_set *kset) |
14f267d9 | 8487 | { |
e924e80e AG |
8488 | struct btf_kfunc_hook_filter *hook_filter; |
8489 | struct btf_id_set8 *add_set = kset->set; | |
a4703e31 | 8490 | bool vmlinux_set = !btf_is_module(btf); |
e924e80e | 8491 | bool add_filter = !!kset->filter; |
dee872e1 | 8492 | struct btf_kfunc_set_tab *tab; |
a4703e31 | 8493 | struct btf_id_set8 *set; |
8646db23 | 8494 | u32 set_cnt, i; |
dee872e1 KKD |
8495 | int ret; |
8496 | ||
a4703e31 | 8497 | if (hook >= BTF_KFUNC_HOOK_MAX) { |
dee872e1 KKD |
8498 | ret = -EINVAL; |
8499 | goto end; | |
8500 | } | |
8501 | ||
8502 | if (!add_set->cnt) | |
8503 | return 0; | |
8504 | ||
8505 | tab = btf->kfunc_set_tab; | |
e924e80e AG |
8506 | |
8507 | if (tab && add_filter) { | |
8508 | u32 i; | |
8509 | ||
8510 | hook_filter = &tab->hook_filters[hook]; | |
8511 | for (i = 0; i < hook_filter->nr_filters; i++) { | |
8512 | if (hook_filter->filters[i] == kset->filter) { | |
8513 | add_filter = false; | |
8514 | break; | |
8515 | } | |
8516 | } | |
8517 | ||
8518 | if (add_filter && hook_filter->nr_filters == BTF_KFUNC_FILTER_MAX_CNT) { | |
8519 | ret = -E2BIG; | |
8520 | goto end; | |
8521 | } | |
8522 | } | |
8523 | ||
dee872e1 KKD |
8524 | if (!tab) { |
8525 | tab = kzalloc(sizeof(*tab), GFP_KERNEL | __GFP_NOWARN); | |
8526 | if (!tab) | |
8527 | return -ENOMEM; | |
8528 | btf->kfunc_set_tab = tab; | |
8529 | } | |
8530 | ||
a4703e31 | 8531 | set = tab->sets[hook]; |
dee872e1 KKD |
8532 | /* Warn when register_btf_kfunc_id_set is called twice for the same hook |
8533 | * for module sets. | |
8534 | */ | |
8535 | if (WARN_ON_ONCE(set && !vmlinux_set)) { | |
8536 | ret = -EINVAL; | |
8537 | goto end; | |
8538 | } | |
8539 | ||
dee872e1 KKD |
8540 | /* In case of vmlinux sets, there may be more than one set being |
8541 | * registered per hook. To create a unified set, we allocate a new set | |
8542 | * and concatenate all individual sets being registered. While each set | |
8543 | * is individually sorted, they may become unsorted when concatenated, | |
8544 | * hence re-sorting the final set again is required to make binary | |
a4703e31 | 8545 | * searching the set using btf_id_set8_contains function work. |
8646db23 AM |
8546 | * |
8547 | * For module sets, we need to allocate as we may need to relocate | |
8548 | * BTF ids. | |
dee872e1 KKD |
8549 | */ |
8550 | set_cnt = set ? set->cnt : 0; | |
8551 | ||
8552 | if (set_cnt > U32_MAX - add_set->cnt) { | |
8553 | ret = -EOVERFLOW; | |
8554 | goto end; | |
8555 | } | |
8556 | ||
8557 | if (set_cnt + add_set->cnt > BTF_KFUNC_SET_MAX_CNT) { | |
8558 | ret = -E2BIG; | |
8559 | goto end; | |
8560 | } | |
8561 | ||
8562 | /* Grow set */ | |
a4703e31 | 8563 | set = krealloc(tab->sets[hook], |
41948afc | 8564 | struct_size(set, pairs, set_cnt + add_set->cnt), |
dee872e1 KKD |
8565 | GFP_KERNEL | __GFP_NOWARN); |
8566 | if (!set) { | |
8567 | ret = -ENOMEM; | |
8568 | goto end; | |
8569 | } | |
8570 | ||
8571 | /* For newly allocated set, initialize set->cnt to 0 */ | |
a4703e31 | 8572 | if (!tab->sets[hook]) |
dee872e1 | 8573 | set->cnt = 0; |
a4703e31 | 8574 | tab->sets[hook] = set; |
dee872e1 KKD |
8575 | |
8576 | /* Concatenate the two sets */ | |
a4703e31 | 8577 | memcpy(set->pairs + set->cnt, add_set->pairs, add_set->cnt * sizeof(set->pairs[0])); |
8646db23 AM |
8578 | /* Now that the set is copied, update with relocated BTF ids */ |
8579 | for (i = set->cnt; i < set->cnt + add_set->cnt; i++) | |
8580 | set->pairs[i].id = btf_relocate_id(btf, set->pairs[i].id); | |
8581 | ||
dee872e1 KKD |
8582 | set->cnt += add_set->cnt; |
8583 | ||
a4703e31 | 8584 | sort(set->pairs, set->cnt, sizeof(set->pairs[0]), btf_id_cmp_func, NULL); |
dee872e1 | 8585 | |
e924e80e AG |
8586 | if (add_filter) { |
8587 | hook_filter = &tab->hook_filters[hook]; | |
8588 | hook_filter->filters[hook_filter->nr_filters++] = kset->filter; | |
8589 | } | |
dee872e1 KKD |
8590 | return 0; |
8591 | end: | |
8592 | btf_free_kfunc_set_tab(btf); | |
8593 | return ret; | |
14f267d9 | 8594 | } |
14f267d9 | 8595 | |
a4703e31 | 8596 | static u32 *__btf_kfunc_id_set_contains(const struct btf *btf, |
dee872e1 | 8597 | enum btf_kfunc_hook hook, |
e924e80e AG |
8598 | u32 kfunc_btf_id, |
8599 | const struct bpf_prog *prog) | |
14f267d9 | 8600 | { |
e924e80e | 8601 | struct btf_kfunc_hook_filter *hook_filter; |
a4703e31 | 8602 | struct btf_id_set8 *set; |
e924e80e | 8603 | u32 *id, i; |
14f267d9 | 8604 | |
a4703e31 KKD |
8605 | if (hook >= BTF_KFUNC_HOOK_MAX) |
8606 | return NULL; | |
dee872e1 | 8607 | if (!btf->kfunc_set_tab) |
a4703e31 | 8608 | return NULL; |
e924e80e AG |
8609 | hook_filter = &btf->kfunc_set_tab->hook_filters[hook]; |
8610 | for (i = 0; i < hook_filter->nr_filters; i++) { | |
8611 | if (hook_filter->filters[i](prog, kfunc_btf_id)) | |
8612 | return NULL; | |
8613 | } | |
a4703e31 | 8614 | set = btf->kfunc_set_tab->sets[hook]; |
dee872e1 | 8615 | if (!set) |
a4703e31 KKD |
8616 | return NULL; |
8617 | id = btf_id_set8_contains(set, kfunc_btf_id); | |
8618 | if (!id) | |
8619 | return NULL; | |
8620 | /* The flags for BTF ID are located next to it */ | |
8621 | return id + 1; | |
dee872e1 KKD |
8622 | } |
8623 | ||
8624 | static int bpf_prog_type_to_kfunc_hook(enum bpf_prog_type prog_type) | |
8625 | { | |
8626 | switch (prog_type) { | |
cfe14564 YS |
8627 | case BPF_PROG_TYPE_UNSPEC: |
8628 | return BTF_KFUNC_HOOK_COMMON; | |
dee872e1 KKD |
8629 | case BPF_PROG_TYPE_XDP: |
8630 | return BTF_KFUNC_HOOK_XDP; | |
8631 | case BPF_PROG_TYPE_SCHED_CLS: | |
8632 | return BTF_KFUNC_HOOK_TC; | |
8633 | case BPF_PROG_TYPE_STRUCT_OPS: | |
8634 | return BTF_KFUNC_HOOK_STRUCT_OPS; | |
97949767 | 8635 | case BPF_PROG_TYPE_TRACING: |
bc638d8c JK |
8636 | case BPF_PROG_TYPE_TRACEPOINT: |
8637 | case BPF_PROG_TYPE_PERF_EVENT: | |
d15bf150 | 8638 | case BPF_PROG_TYPE_LSM: |
97949767 BT |
8639 | return BTF_KFUNC_HOOK_TRACING; |
8640 | case BPF_PROG_TYPE_SYSCALL: | |
8641 | return BTF_KFUNC_HOOK_SYSCALL; | |
b5964b96 | 8642 | case BPF_PROG_TYPE_CGROUP_SKB: |
67666479 MC |
8643 | case BPF_PROG_TYPE_CGROUP_SOCK: |
8644 | case BPF_PROG_TYPE_CGROUP_DEVICE: | |
53e380d2 | 8645 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
67666479 MC |
8646 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: |
8647 | case BPF_PROG_TYPE_CGROUP_SYSCTL: | |
59422464 | 8648 | case BPF_PROG_TYPE_SOCK_OPS: |
67666479 | 8649 | return BTF_KFUNC_HOOK_CGROUP; |
b5964b96 JK |
8650 | case BPF_PROG_TYPE_SCHED_ACT: |
8651 | return BTF_KFUNC_HOOK_SCHED_ACT; | |
8652 | case BPF_PROG_TYPE_SK_SKB: | |
8653 | return BTF_KFUNC_HOOK_SK_SKB; | |
8654 | case BPF_PROG_TYPE_SOCKET_FILTER: | |
8655 | return BTF_KFUNC_HOOK_SOCKET_FILTER; | |
8656 | case BPF_PROG_TYPE_LWT_OUT: | |
8657 | case BPF_PROG_TYPE_LWT_IN: | |
8658 | case BPF_PROG_TYPE_LWT_XMIT: | |
8659 | case BPF_PROG_TYPE_LWT_SEG6LOCAL: | |
8660 | return BTF_KFUNC_HOOK_LWT; | |
fd9c663b FW |
8661 | case BPF_PROG_TYPE_NETFILTER: |
8662 | return BTF_KFUNC_HOOK_NETFILTER; | |
adf46d88 JO |
8663 | case BPF_PROG_TYPE_KPROBE: |
8664 | return BTF_KFUNC_HOOK_KPROBE; | |
dee872e1 KKD |
8665 | default: |
8666 | return BTF_KFUNC_HOOK_MAX; | |
14f267d9 | 8667 | } |
14f267d9 KKD |
8668 | } |
8669 | ||
dee872e1 KKD |
8670 | /* Caution: |
8671 | * Reference to the module (obtained using btf_try_get_module) corresponding to | |
8672 | * the struct btf *MUST* be held when calling this function from verifier | |
8673 | * context. This is usually true as we stash references in prog's kfunc_btf_tab; | |
8674 | * keeping the reference for the duration of the call provides the necessary | |
8675 | * protection for looking up a well-formed btf->kfunc_set_tab. | |
8676 | */ | |
a4703e31 | 8677 | u32 *btf_kfunc_id_set_contains(const struct btf *btf, |
e924e80e AG |
8678 | u32 kfunc_btf_id, |
8679 | const struct bpf_prog *prog) | |
dee872e1 | 8680 | { |
e924e80e | 8681 | enum bpf_prog_type prog_type = resolve_prog_type(prog); |
dee872e1 | 8682 | enum btf_kfunc_hook hook; |
cfe14564 YS |
8683 | u32 *kfunc_flags; |
8684 | ||
e924e80e | 8685 | kfunc_flags = __btf_kfunc_id_set_contains(btf, BTF_KFUNC_HOOK_COMMON, kfunc_btf_id, prog); |
cfe14564 YS |
8686 | if (kfunc_flags) |
8687 | return kfunc_flags; | |
0e32dfc8 | 8688 | |
dee872e1 | 8689 | hook = bpf_prog_type_to_kfunc_hook(prog_type); |
e924e80e | 8690 | return __btf_kfunc_id_set_contains(btf, hook, kfunc_btf_id, prog); |
dee872e1 | 8691 | } |
d9847eb8 | 8692 | |
e924e80e AG |
8693 | u32 *btf_kfunc_is_modify_return(const struct btf *btf, u32 kfunc_btf_id, |
8694 | const struct bpf_prog *prog) | |
5b481aca | 8695 | { |
e924e80e | 8696 | return __btf_kfunc_id_set_contains(btf, BTF_KFUNC_HOOK_FMODRET, kfunc_btf_id, prog); |
5b481aca BT |
8697 | } |
8698 | ||
8699 | static int __register_btf_kfunc_id_set(enum btf_kfunc_hook hook, | |
8700 | const struct btf_kfunc_id_set *kset) | |
dee872e1 | 8701 | { |
dee872e1 | 8702 | struct btf *btf; |
215bf496 | 8703 | int ret, i; |
dee872e1 KKD |
8704 | |
8705 | btf = btf_get_module_btf(kset->owner); | |
9e60b0e0 GT |
8706 | if (!btf) |
8707 | return check_btf_kconfigs(kset->owner, "kfunc"); | |
c446fdac SF |
8708 | if (IS_ERR(btf)) |
8709 | return PTR_ERR(btf); | |
dee872e1 | 8710 | |
215bf496 | 8711 | for (i = 0; i < kset->set->cnt; i++) { |
8646db23 | 8712 | ret = btf_check_kfunc_protos(btf, btf_relocate_id(btf, kset->set->pairs[i].id), |
215bf496 AN |
8713 | kset->set->pairs[i].flags); |
8714 | if (ret) | |
8715 | goto err_out; | |
8716 | } | |
8717 | ||
e924e80e AG |
8718 | ret = btf_populate_kfunc_set(btf, hook, kset); |
8719 | ||
215bf496 | 8720 | err_out: |
9492450f | 8721 | btf_put(btf); |
dee872e1 KKD |
8722 | return ret; |
8723 | } | |
5b481aca BT |
8724 | |
8725 | /* This function must be invoked only from initcalls/module init functions */ | |
8726 | int register_btf_kfunc_id_set(enum bpf_prog_type prog_type, | |
8727 | const struct btf_kfunc_id_set *kset) | |
8728 | { | |
8729 | enum btf_kfunc_hook hook; | |
8730 | ||
6f3189f3 DX |
8731 | /* All kfuncs need to be tagged as such in BTF. |
8732 | * WARN() for initcall registrations that do not check errors. | |
8733 | */ | |
8734 | if (!(kset->set->flags & BTF_SET8_KFUNCS)) { | |
8735 | WARN_ON(!kset->owner); | |
8736 | return -EINVAL; | |
8737 | } | |
8738 | ||
5b481aca BT |
8739 | hook = bpf_prog_type_to_kfunc_hook(prog_type); |
8740 | return __register_btf_kfunc_id_set(hook, kset); | |
8741 | } | |
dee872e1 | 8742 | EXPORT_SYMBOL_GPL(register_btf_kfunc_id_set); |
be315829 | 8743 | |
5b481aca BT |
8744 | /* This function must be invoked only from initcalls/module init functions */ |
8745 | int register_btf_fmodret_id_set(const struct btf_kfunc_id_set *kset) | |
8746 | { | |
8747 | return __register_btf_kfunc_id_set(BTF_KFUNC_HOOK_FMODRET, kset); | |
8748 | } | |
8749 | EXPORT_SYMBOL_GPL(register_btf_fmodret_id_set); | |
8750 | ||
5ce937d6 KKD |
8751 | s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id) |
8752 | { | |
8753 | struct btf_id_dtor_kfunc_tab *tab = btf->dtor_kfunc_tab; | |
8754 | struct btf_id_dtor_kfunc *dtor; | |
8755 | ||
8756 | if (!tab) | |
8757 | return -ENOENT; | |
8758 | /* Even though the size of tab->dtors[0] is > sizeof(u32), we only need | |
8759 | * to compare the first u32 with btf_id, so we can reuse btf_id_cmp_func. | |
8760 | */ | |
8761 | BUILD_BUG_ON(offsetof(struct btf_id_dtor_kfunc, btf_id) != 0); | |
8762 | dtor = bsearch(&btf_id, tab->dtors, tab->cnt, sizeof(tab->dtors[0]), btf_id_cmp_func); | |
8763 | if (!dtor) | |
8764 | return -ENOENT; | |
8765 | return dtor->kfunc_btf_id; | |
8766 | } | |
8767 | ||
14a324f6 KKD |
8768 | static int btf_check_dtor_kfuncs(struct btf *btf, const struct btf_id_dtor_kfunc *dtors, u32 cnt) |
8769 | { | |
8770 | const struct btf_type *dtor_func, *dtor_func_proto, *t; | |
8771 | const struct btf_param *args; | |
8772 | s32 dtor_btf_id; | |
8773 | u32 nr_args, i; | |
8774 | ||
8775 | for (i = 0; i < cnt; i++) { | |
8646db23 | 8776 | dtor_btf_id = btf_relocate_id(btf, dtors[i].kfunc_btf_id); |
14a324f6 KKD |
8777 | |
8778 | dtor_func = btf_type_by_id(btf, dtor_btf_id); | |
8779 | if (!dtor_func || !btf_type_is_func(dtor_func)) | |
8780 | return -EINVAL; | |
8781 | ||
8782 | dtor_func_proto = btf_type_by_id(btf, dtor_func->type); | |
8783 | if (!dtor_func_proto || !btf_type_is_func_proto(dtor_func_proto)) | |
8784 | return -EINVAL; | |
8785 | ||
8786 | /* Make sure the prototype of the destructor kfunc is 'void func(type *)' */ | |
8787 | t = btf_type_by_id(btf, dtor_func_proto->type); | |
8788 | if (!t || !btf_type_is_void(t)) | |
8789 | return -EINVAL; | |
8790 | ||
8791 | nr_args = btf_type_vlen(dtor_func_proto); | |
8792 | if (nr_args != 1) | |
8793 | return -EINVAL; | |
8794 | args = btf_params(dtor_func_proto); | |
8795 | t = btf_type_by_id(btf, args[0].type); | |
8796 | /* Allow any pointer type, as width on targets Linux supports | |
8797 | * will be same for all pointer types (i.e. sizeof(void *)) | |
8798 | */ | |
8799 | if (!t || !btf_type_is_ptr(t)) | |
8800 | return -EINVAL; | |
8801 | } | |
8802 | return 0; | |
8803 | } | |
8804 | ||
5ce937d6 KKD |
8805 | /* This function must be invoked only from initcalls/module init functions */ |
8806 | int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors, u32 add_cnt, | |
8807 | struct module *owner) | |
8808 | { | |
8809 | struct btf_id_dtor_kfunc_tab *tab; | |
8810 | struct btf *btf; | |
8646db23 | 8811 | u32 tab_cnt, i; |
5ce937d6 KKD |
8812 | int ret; |
8813 | ||
8814 | btf = btf_get_module_btf(owner); | |
9e60b0e0 GT |
8815 | if (!btf) |
8816 | return check_btf_kconfigs(owner, "dtor kfuncs"); | |
5ce937d6 KKD |
8817 | if (IS_ERR(btf)) |
8818 | return PTR_ERR(btf); | |
8819 | ||
8820 | if (add_cnt >= BTF_DTOR_KFUNC_MAX_CNT) { | |
8821 | pr_err("cannot register more than %d kfunc destructors\n", BTF_DTOR_KFUNC_MAX_CNT); | |
8822 | ret = -E2BIG; | |
8823 | goto end; | |
8824 | } | |
8825 | ||
14a324f6 KKD |
8826 | /* Ensure that the prototype of dtor kfuncs being registered is sane */ |
8827 | ret = btf_check_dtor_kfuncs(btf, dtors, add_cnt); | |
8828 | if (ret < 0) | |
8829 | goto end; | |
8830 | ||
5ce937d6 KKD |
8831 | tab = btf->dtor_kfunc_tab; |
8832 | /* Only one call allowed for modules */ | |
8833 | if (WARN_ON_ONCE(tab && btf_is_module(btf))) { | |
8834 | ret = -EINVAL; | |
8835 | goto end; | |
8836 | } | |
8837 | ||
8838 | tab_cnt = tab ? tab->cnt : 0; | |
8839 | if (tab_cnt > U32_MAX - add_cnt) { | |
8840 | ret = -EOVERFLOW; | |
8841 | goto end; | |
8842 | } | |
8843 | if (tab_cnt + add_cnt >= BTF_DTOR_KFUNC_MAX_CNT) { | |
8844 | pr_err("cannot register more than %d kfunc destructors\n", BTF_DTOR_KFUNC_MAX_CNT); | |
8845 | ret = -E2BIG; | |
8846 | goto end; | |
8847 | } | |
8848 | ||
8849 | tab = krealloc(btf->dtor_kfunc_tab, | |
41948afc | 8850 | struct_size(tab, dtors, tab_cnt + add_cnt), |
5ce937d6 KKD |
8851 | GFP_KERNEL | __GFP_NOWARN); |
8852 | if (!tab) { | |
8853 | ret = -ENOMEM; | |
8854 | goto end; | |
8855 | } | |
8856 | ||
8857 | if (!btf->dtor_kfunc_tab) | |
8858 | tab->cnt = 0; | |
8859 | btf->dtor_kfunc_tab = tab; | |
8860 | ||
8861 | memcpy(tab->dtors + tab->cnt, dtors, add_cnt * sizeof(tab->dtors[0])); | |
8646db23 AM |
8862 | |
8863 | /* remap BTF ids based on BTF relocation (if any) */ | |
8864 | for (i = tab_cnt; i < tab_cnt + add_cnt; i++) { | |
8865 | tab->dtors[i].btf_id = btf_relocate_id(btf, tab->dtors[i].btf_id); | |
8866 | tab->dtors[i].kfunc_btf_id = btf_relocate_id(btf, tab->dtors[i].kfunc_btf_id); | |
8867 | } | |
8868 | ||
5ce937d6 KKD |
8869 | tab->cnt += add_cnt; |
8870 | ||
8871 | sort(tab->dtors, tab->cnt, sizeof(tab->dtors[0]), btf_id_cmp_func, NULL); | |
8872 | ||
5ce937d6 | 8873 | end: |
74bc3a5a JO |
8874 | if (ret) |
8875 | btf_free_dtor_kfunc_tab(btf); | |
5ce937d6 KKD |
8876 | btf_put(btf); |
8877 | return ret; | |
8878 | } | |
8879 | EXPORT_SYMBOL_GPL(register_btf_id_dtor_kfuncs); | |
8880 | ||
e70e13e7 MC |
8881 | #define MAX_TYPES_ARE_COMPAT_DEPTH 2 |
8882 | ||
e70e13e7 MC |
8883 | /* Check local and target types for compatibility. This check is used for |
8884 | * type-based CO-RE relocations and follow slightly different rules than | |
8885 | * field-based relocations. This function assumes that root types were already | |
8886 | * checked for name match. Beyond that initial root-level name check, names | |
8887 | * are completely ignored. Compatibility rules are as follows: | |
6089fb32 | 8888 | * - any two STRUCTs/UNIONs/FWDs/ENUMs/INTs/ENUM64s are considered compatible, but |
e70e13e7 MC |
8889 | * kind should match for local and target types (i.e., STRUCT is not |
8890 | * compatible with UNION); | |
6089fb32 | 8891 | * - for ENUMs/ENUM64s, the size is ignored; |
e70e13e7 MC |
8892 | * - for INT, size and signedness are ignored; |
8893 | * - for ARRAY, dimensionality is ignored, element types are checked for | |
8894 | * compatibility recursively; | |
8895 | * - CONST/VOLATILE/RESTRICT modifiers are ignored; | |
8896 | * - TYPEDEFs/PTRs are compatible if types they pointing to are compatible; | |
8897 | * - FUNC_PROTOs are compatible if they have compatible signature: same | |
8898 | * number of input args and compatible return and argument types. | |
8899 | * These rules are not set in stone and probably will be adjusted as we get | |
8900 | * more experience with using BPF CO-RE relocations. | |
8901 | */ | |
29db4bea AS |
8902 | int bpf_core_types_are_compat(const struct btf *local_btf, __u32 local_id, |
8903 | const struct btf *targ_btf, __u32 targ_id) | |
8904 | { | |
fd75733d | 8905 | return __bpf_core_types_are_compat(local_btf, local_id, targ_btf, targ_id, |
e70e13e7 | 8906 | MAX_TYPES_ARE_COMPAT_DEPTH); |
29db4bea AS |
8907 | } |
8908 | ||
ec6209c8 DM |
8909 | #define MAX_TYPES_MATCH_DEPTH 2 |
8910 | ||
8911 | int bpf_core_types_match(const struct btf *local_btf, u32 local_id, | |
8912 | const struct btf *targ_btf, u32 targ_id) | |
8913 | { | |
8914 | return __bpf_core_types_match(local_btf, local_id, targ_btf, targ_id, false, | |
8915 | MAX_TYPES_MATCH_DEPTH); | |
8916 | } | |
8917 | ||
29db4bea AS |
8918 | static bool bpf_core_is_flavor_sep(const char *s) |
8919 | { | |
8920 | /* check X___Y name pattern, where X and Y are not underscores */ | |
8921 | return s[0] != '_' && /* X */ | |
8922 | s[1] == '_' && s[2] == '_' && s[3] == '_' && /* ___ */ | |
8923 | s[4] != '_'; /* Y */ | |
8924 | } | |
8925 | ||
8926 | size_t bpf_core_essential_name_len(const char *name) | |
8927 | { | |
8928 | size_t n = strlen(name); | |
8929 | int i; | |
8930 | ||
8931 | for (i = n - 5; i >= 0; i--) { | |
8932 | if (bpf_core_is_flavor_sep(name + i)) | |
8933 | return i + 1; | |
8934 | } | |
8935 | return n; | |
8936 | } | |
fbd94c7a | 8937 | |
1e89106d AS |
8938 | static void bpf_free_cands(struct bpf_cand_cache *cands) |
8939 | { | |
8940 | if (!cands->cnt) | |
8941 | /* empty candidate array was allocated on stack */ | |
8942 | return; | |
8943 | kfree(cands); | |
8944 | } | |
8945 | ||
8946 | static void bpf_free_cands_from_cache(struct bpf_cand_cache *cands) | |
8947 | { | |
8948 | kfree(cands->name); | |
8949 | kfree(cands); | |
8950 | } | |
8951 | ||
8952 | #define VMLINUX_CAND_CACHE_SIZE 31 | |
8953 | static struct bpf_cand_cache *vmlinux_cand_cache[VMLINUX_CAND_CACHE_SIZE]; | |
8954 | ||
8955 | #define MODULE_CAND_CACHE_SIZE 31 | |
8956 | static struct bpf_cand_cache *module_cand_cache[MODULE_CAND_CACHE_SIZE]; | |
8957 | ||
1e89106d AS |
8958 | static void __print_cand_cache(struct bpf_verifier_log *log, |
8959 | struct bpf_cand_cache **cache, | |
8960 | int cache_size) | |
8961 | { | |
8962 | struct bpf_cand_cache *cc; | |
8963 | int i, j; | |
8964 | ||
8965 | for (i = 0; i < cache_size; i++) { | |
8966 | cc = cache[i]; | |
8967 | if (!cc) | |
8968 | continue; | |
8969 | bpf_log(log, "[%d]%s(", i, cc->name); | |
8970 | for (j = 0; j < cc->cnt; j++) { | |
8971 | bpf_log(log, "%d", cc->cands[j].id); | |
8972 | if (j < cc->cnt - 1) | |
8973 | bpf_log(log, " "); | |
8974 | } | |
8975 | bpf_log(log, "), "); | |
8976 | } | |
8977 | } | |
8978 | ||
8979 | static void print_cand_cache(struct bpf_verifier_log *log) | |
8980 | { | |
8981 | mutex_lock(&cand_cache_mutex); | |
8982 | bpf_log(log, "vmlinux_cand_cache:"); | |
8983 | __print_cand_cache(log, vmlinux_cand_cache, VMLINUX_CAND_CACHE_SIZE); | |
8984 | bpf_log(log, "\nmodule_cand_cache:"); | |
8985 | __print_cand_cache(log, module_cand_cache, MODULE_CAND_CACHE_SIZE); | |
8986 | bpf_log(log, "\n"); | |
8987 | mutex_unlock(&cand_cache_mutex); | |
8988 | } | |
8989 | ||
8990 | static u32 hash_cands(struct bpf_cand_cache *cands) | |
8991 | { | |
8992 | return jhash(cands->name, cands->name_len, 0); | |
8993 | } | |
8994 | ||
8995 | static struct bpf_cand_cache *check_cand_cache(struct bpf_cand_cache *cands, | |
8996 | struct bpf_cand_cache **cache, | |
8997 | int cache_size) | |
8998 | { | |
8999 | struct bpf_cand_cache *cc = cache[hash_cands(cands) % cache_size]; | |
9000 | ||
9001 | if (cc && cc->name_len == cands->name_len && | |
9002 | !strncmp(cc->name, cands->name, cands->name_len)) | |
9003 | return cc; | |
9004 | return NULL; | |
9005 | } | |
9006 | ||
9007 | static size_t sizeof_cands(int cnt) | |
9008 | { | |
9009 | return offsetof(struct bpf_cand_cache, cands[cnt]); | |
9010 | } | |
9011 | ||
9012 | static struct bpf_cand_cache *populate_cand_cache(struct bpf_cand_cache *cands, | |
9013 | struct bpf_cand_cache **cache, | |
9014 | int cache_size) | |
9015 | { | |
9016 | struct bpf_cand_cache **cc = &cache[hash_cands(cands) % cache_size], *new_cands; | |
9017 | ||
9018 | if (*cc) { | |
9019 | bpf_free_cands_from_cache(*cc); | |
9020 | *cc = NULL; | |
9021 | } | |
4674f210 | 9022 | new_cands = kmemdup(cands, sizeof_cands(cands->cnt), GFP_KERNEL); |
1e89106d AS |
9023 | if (!new_cands) { |
9024 | bpf_free_cands(cands); | |
9025 | return ERR_PTR(-ENOMEM); | |
9026 | } | |
1e89106d AS |
9027 | /* strdup the name, since it will stay in cache. |
9028 | * the cands->name points to strings in prog's BTF and the prog can be unloaded. | |
9029 | */ | |
9030 | new_cands->name = kmemdup_nul(cands->name, cands->name_len, GFP_KERNEL); | |
9031 | bpf_free_cands(cands); | |
9032 | if (!new_cands->name) { | |
9033 | kfree(new_cands); | |
9034 | return ERR_PTR(-ENOMEM); | |
9035 | } | |
9036 | *cc = new_cands; | |
9037 | return new_cands; | |
9038 | } | |
9039 | ||
29f2e5bd | 9040 | #ifdef CONFIG_DEBUG_INFO_BTF_MODULES |
1e89106d AS |
9041 | static void __purge_cand_cache(struct btf *btf, struct bpf_cand_cache **cache, |
9042 | int cache_size) | |
9043 | { | |
9044 | struct bpf_cand_cache *cc; | |
9045 | int i, j; | |
9046 | ||
9047 | for (i = 0; i < cache_size; i++) { | |
9048 | cc = cache[i]; | |
9049 | if (!cc) | |
9050 | continue; | |
9051 | if (!btf) { | |
9052 | /* when new module is loaded purge all of module_cand_cache, | |
9053 | * since new module might have candidates with the name | |
9054 | * that matches cached cands. | |
9055 | */ | |
9056 | bpf_free_cands_from_cache(cc); | |
9057 | cache[i] = NULL; | |
9058 | continue; | |
9059 | } | |
9060 | /* when module is unloaded purge cache entries | |
9061 | * that match module's btf | |
9062 | */ | |
9063 | for (j = 0; j < cc->cnt; j++) | |
9064 | if (cc->cands[j].btf == btf) { | |
9065 | bpf_free_cands_from_cache(cc); | |
9066 | cache[i] = NULL; | |
9067 | break; | |
9068 | } | |
9069 | } | |
9070 | ||
9071 | } | |
9072 | ||
9073 | static void purge_cand_cache(struct btf *btf) | |
9074 | { | |
9075 | mutex_lock(&cand_cache_mutex); | |
9076 | __purge_cand_cache(btf, module_cand_cache, MODULE_CAND_CACHE_SIZE); | |
9077 | mutex_unlock(&cand_cache_mutex); | |
9078 | } | |
29f2e5bd | 9079 | #endif |
1e89106d AS |
9080 | |
9081 | static struct bpf_cand_cache * | |
9082 | bpf_core_add_cands(struct bpf_cand_cache *cands, const struct btf *targ_btf, | |
9083 | int targ_start_id) | |
9084 | { | |
9085 | struct bpf_cand_cache *new_cands; | |
9086 | const struct btf_type *t; | |
9087 | const char *targ_name; | |
9088 | size_t targ_essent_len; | |
9089 | int n, i; | |
9090 | ||
9091 | n = btf_nr_types(targ_btf); | |
9092 | for (i = targ_start_id; i < n; i++) { | |
9093 | t = btf_type_by_id(targ_btf, i); | |
9094 | if (btf_kind(t) != cands->kind) | |
9095 | continue; | |
9096 | ||
9097 | targ_name = btf_name_by_offset(targ_btf, t->name_off); | |
9098 | if (!targ_name) | |
9099 | continue; | |
9100 | ||
9101 | /* the resched point is before strncmp to make sure that search | |
9102 | * for non-existing name will have a chance to schedule(). | |
9103 | */ | |
9104 | cond_resched(); | |
9105 | ||
9106 | if (strncmp(cands->name, targ_name, cands->name_len) != 0) | |
9107 | continue; | |
9108 | ||
9109 | targ_essent_len = bpf_core_essential_name_len(targ_name); | |
9110 | if (targ_essent_len != cands->name_len) | |
9111 | continue; | |
9112 | ||
9113 | /* most of the time there is only one candidate for a given kind+name pair */ | |
9114 | new_cands = kmalloc(sizeof_cands(cands->cnt + 1), GFP_KERNEL); | |
9115 | if (!new_cands) { | |
9116 | bpf_free_cands(cands); | |
9117 | return ERR_PTR(-ENOMEM); | |
9118 | } | |
9119 | ||
9120 | memcpy(new_cands, cands, sizeof_cands(cands->cnt)); | |
9121 | bpf_free_cands(cands); | |
9122 | cands = new_cands; | |
9123 | cands->cands[cands->cnt].btf = targ_btf; | |
9124 | cands->cands[cands->cnt].id = i; | |
9125 | cands->cnt++; | |
9126 | } | |
9127 | return cands; | |
9128 | } | |
9129 | ||
9130 | static struct bpf_cand_cache * | |
9131 | bpf_core_find_cands(struct bpf_core_ctx *ctx, u32 local_type_id) | |
9132 | { | |
9133 | struct bpf_cand_cache *cands, *cc, local_cand = {}; | |
9134 | const struct btf *local_btf = ctx->btf; | |
9135 | const struct btf_type *local_type; | |
9136 | const struct btf *main_btf; | |
9137 | size_t local_essent_len; | |
9138 | struct btf *mod_btf; | |
9139 | const char *name; | |
9140 | int id; | |
9141 | ||
9142 | main_btf = bpf_get_btf_vmlinux(); | |
9143 | if (IS_ERR(main_btf)) | |
f18a4997 | 9144 | return ERR_CAST(main_btf); |
7ada3787 KKD |
9145 | if (!main_btf) |
9146 | return ERR_PTR(-EINVAL); | |
1e89106d AS |
9147 | |
9148 | local_type = btf_type_by_id(local_btf, local_type_id); | |
9149 | if (!local_type) | |
9150 | return ERR_PTR(-EINVAL); | |
9151 | ||
9152 | name = btf_name_by_offset(local_btf, local_type->name_off); | |
9153 | if (str_is_empty(name)) | |
9154 | return ERR_PTR(-EINVAL); | |
9155 | local_essent_len = bpf_core_essential_name_len(name); | |
9156 | ||
9157 | cands = &local_cand; | |
9158 | cands->name = name; | |
9159 | cands->kind = btf_kind(local_type); | |
9160 | cands->name_len = local_essent_len; | |
9161 | ||
9162 | cc = check_cand_cache(cands, vmlinux_cand_cache, VMLINUX_CAND_CACHE_SIZE); | |
9163 | /* cands is a pointer to stack here */ | |
9164 | if (cc) { | |
9165 | if (cc->cnt) | |
9166 | return cc; | |
9167 | goto check_modules; | |
9168 | } | |
9169 | ||
9170 | /* Attempt to find target candidates in vmlinux BTF first */ | |
9171 | cands = bpf_core_add_cands(cands, main_btf, 1); | |
9172 | if (IS_ERR(cands)) | |
f18a4997 | 9173 | return ERR_CAST(cands); |
1e89106d AS |
9174 | |
9175 | /* cands is a pointer to kmalloced memory here if cands->cnt > 0 */ | |
9176 | ||
9177 | /* populate cache even when cands->cnt == 0 */ | |
9178 | cc = populate_cand_cache(cands, vmlinux_cand_cache, VMLINUX_CAND_CACHE_SIZE); | |
9179 | if (IS_ERR(cc)) | |
f18a4997 | 9180 | return ERR_CAST(cc); |
1e89106d AS |
9181 | |
9182 | /* if vmlinux BTF has any candidate, don't go for module BTFs */ | |
9183 | if (cc->cnt) | |
9184 | return cc; | |
9185 | ||
9186 | check_modules: | |
9187 | /* cands is a pointer to stack here and cands->cnt == 0 */ | |
9188 | cc = check_cand_cache(cands, module_cand_cache, MODULE_CAND_CACHE_SIZE); | |
9189 | if (cc) | |
9190 | /* if cache has it return it even if cc->cnt == 0 */ | |
9191 | return cc; | |
9192 | ||
9193 | /* If candidate is not found in vmlinux's BTF then search in module's BTFs */ | |
9194 | spin_lock_bh(&btf_idr_lock); | |
9195 | idr_for_each_entry(&btf_idr, mod_btf, id) { | |
9196 | if (!btf_is_module(mod_btf)) | |
9197 | continue; | |
9198 | /* linear search could be slow hence unlock/lock | |
9199 | * the IDR to avoiding holding it for too long | |
9200 | */ | |
9201 | btf_get(mod_btf); | |
9202 | spin_unlock_bh(&btf_idr_lock); | |
9203 | cands = bpf_core_add_cands(cands, mod_btf, btf_nr_types(main_btf)); | |
acf1c3d6 AS |
9204 | btf_put(mod_btf); |
9205 | if (IS_ERR(cands)) | |
f18a4997 | 9206 | return ERR_CAST(cands); |
1e89106d | 9207 | spin_lock_bh(&btf_idr_lock); |
1e89106d AS |
9208 | } |
9209 | spin_unlock_bh(&btf_idr_lock); | |
9210 | /* cands is a pointer to kmalloced memory here if cands->cnt > 0 | |
9211 | * or pointer to stack if cands->cnd == 0. | |
9212 | * Copy it into the cache even when cands->cnt == 0 and | |
9213 | * return the result. | |
9214 | */ | |
9215 | return populate_cand_cache(cands, module_cand_cache, MODULE_CAND_CACHE_SIZE); | |
9216 | } | |
9217 | ||
fbd94c7a AS |
9218 | int bpf_core_apply(struct bpf_core_ctx *ctx, const struct bpf_core_relo *relo, |
9219 | int relo_idx, void *insn) | |
9220 | { | |
1e89106d AS |
9221 | bool need_cands = relo->kind != BPF_CORE_TYPE_ID_LOCAL; |
9222 | struct bpf_core_cand_list cands = {}; | |
adb8fa19 | 9223 | struct bpf_core_relo_res targ_res; |
78c1f8d0 | 9224 | struct bpf_core_spec *specs; |
3d2786d6 | 9225 | const struct btf_type *type; |
1e89106d AS |
9226 | int err; |
9227 | ||
78c1f8d0 AS |
9228 | /* ~4k of temp memory necessary to convert LLVM spec like "0:1:0:5" |
9229 | * into arrays of btf_ids of struct fields and array indices. | |
9230 | */ | |
9231 | specs = kcalloc(3, sizeof(*specs), GFP_KERNEL); | |
9232 | if (!specs) | |
9233 | return -ENOMEM; | |
9234 | ||
3d2786d6 EZ |
9235 | type = btf_type_by_id(ctx->btf, relo->type_id); |
9236 | if (!type) { | |
9237 | bpf_log(ctx->log, "relo #%u: bad type id %u\n", | |
9238 | relo_idx, relo->type_id); | |
45126b15 | 9239 | kfree(specs); |
3d2786d6 EZ |
9240 | return -EINVAL; |
9241 | } | |
9242 | ||
1e89106d AS |
9243 | if (need_cands) { |
9244 | struct bpf_cand_cache *cc; | |
9245 | int i; | |
9246 | ||
9247 | mutex_lock(&cand_cache_mutex); | |
9248 | cc = bpf_core_find_cands(ctx, relo->type_id); | |
9249 | if (IS_ERR(cc)) { | |
9250 | bpf_log(ctx->log, "target candidate search failed for %d\n", | |
9251 | relo->type_id); | |
9252 | err = PTR_ERR(cc); | |
9253 | goto out; | |
9254 | } | |
9255 | if (cc->cnt) { | |
9256 | cands.cands = kcalloc(cc->cnt, sizeof(*cands.cands), GFP_KERNEL); | |
9257 | if (!cands.cands) { | |
9258 | err = -ENOMEM; | |
9259 | goto out; | |
9260 | } | |
9261 | } | |
9262 | for (i = 0; i < cc->cnt; i++) { | |
9263 | bpf_log(ctx->log, | |
9264 | "CO-RE relocating %s %s: found target candidate [%d]\n", | |
9265 | btf_kind_str[cc->kind], cc->name, cc->cands[i].id); | |
9266 | cands.cands[i].btf = cc->cands[i].btf; | |
9267 | cands.cands[i].id = cc->cands[i].id; | |
9268 | } | |
9269 | cands.len = cc->cnt; | |
9270 | /* cand_cache_mutex needs to span the cache lookup and | |
9271 | * copy of btf pointer into bpf_core_cand_list, | |
adb8fa19 | 9272 | * since module can be unloaded while bpf_core_calc_relo_insn |
1e89106d AS |
9273 | * is working with module's btf. |
9274 | */ | |
9275 | } | |
9276 | ||
adb8fa19 MV |
9277 | err = bpf_core_calc_relo_insn((void *)ctx->log, relo, relo_idx, ctx->btf, &cands, specs, |
9278 | &targ_res); | |
9279 | if (err) | |
9280 | goto out; | |
9281 | ||
9282 | err = bpf_core_patch_insn((void *)ctx->log, insn, relo->insn_off / 8, relo, relo_idx, | |
9283 | &targ_res); | |
9284 | ||
1e89106d | 9285 | out: |
78c1f8d0 | 9286 | kfree(specs); |
1e89106d AS |
9287 | if (need_cands) { |
9288 | kfree(cands.cands); | |
9289 | mutex_unlock(&cand_cache_mutex); | |
9290 | if (ctx->log->level & BPF_LOG_LEVEL2) | |
9291 | print_cand_cache(ctx->log); | |
9292 | } | |
9293 | return err; | |
fbd94c7a | 9294 | } |
57539b1c DV |
9295 | |
9296 | bool btf_nested_type_is_trusted(struct bpf_verifier_log *log, | |
9297 | const struct bpf_reg_state *reg, | |
63260df1 | 9298 | const char *field_name, u32 btf_id, const char *suffix) |
57539b1c DV |
9299 | { |
9300 | struct btf *btf = reg->btf; | |
9301 | const struct btf_type *walk_type, *safe_type; | |
9302 | const char *tname; | |
9303 | char safe_tname[64]; | |
9304 | long ret, safe_id; | |
63260df1 | 9305 | const struct btf_member *member; |
57539b1c | 9306 | u32 i; |
57539b1c DV |
9307 | |
9308 | walk_type = btf_type_by_id(btf, reg->btf_id); | |
9309 | if (!walk_type) | |
9310 | return false; | |
9311 | ||
9312 | tname = btf_name_by_offset(btf, walk_type->name_off); | |
9313 | ||
6fcd486b | 9314 | ret = snprintf(safe_tname, sizeof(safe_tname), "%s%s", tname, suffix); |
a8f12572 | 9315 | if (ret >= sizeof(safe_tname)) |
57539b1c DV |
9316 | return false; |
9317 | ||
9318 | safe_id = btf_find_by_name_kind(btf, safe_tname, BTF_INFO_KIND(walk_type->info)); | |
9319 | if (safe_id < 0) | |
9320 | return false; | |
9321 | ||
9322 | safe_type = btf_type_by_id(btf, safe_id); | |
9323 | if (!safe_type) | |
9324 | return false; | |
9325 | ||
57539b1c DV |
9326 | for_each_member(i, safe_type, member) { |
9327 | const char *m_name = __btf_name_by_offset(btf, member->name_off); | |
63260df1 AS |
9328 | const struct btf_type *mtype = btf_type_by_id(btf, member->type); |
9329 | u32 id; | |
9330 | ||
9331 | if (!btf_type_is_ptr(mtype)) | |
9332 | continue; | |
57539b1c | 9333 | |
63260df1 | 9334 | btf_type_skip_modifiers(btf, mtype->type, &id); |
57539b1c | 9335 | /* If we match on both type and name, the field is considered trusted. */ |
63260df1 | 9336 | if (btf_id == id && !strcmp(field_name, m_name)) |
57539b1c DV |
9337 | return true; |
9338 | } | |
9339 | ||
9340 | return false; | |
9341 | } | |
b613d335 DV |
9342 | |
9343 | bool btf_type_ids_nocast_alias(struct bpf_verifier_log *log, | |
9344 | const struct btf *reg_btf, u32 reg_id, | |
9345 | const struct btf *arg_btf, u32 arg_id) | |
9346 | { | |
9347 | const char *reg_name, *arg_name, *search_needle; | |
9348 | const struct btf_type *reg_type, *arg_type; | |
9349 | int reg_len, arg_len, cmp_len; | |
9350 | size_t pattern_len = sizeof(NOCAST_ALIAS_SUFFIX) - sizeof(char); | |
9351 | ||
9352 | reg_type = btf_type_by_id(reg_btf, reg_id); | |
9353 | if (!reg_type) | |
9354 | return false; | |
9355 | ||
9356 | arg_type = btf_type_by_id(arg_btf, arg_id); | |
9357 | if (!arg_type) | |
9358 | return false; | |
9359 | ||
9360 | reg_name = btf_name_by_offset(reg_btf, reg_type->name_off); | |
9361 | arg_name = btf_name_by_offset(arg_btf, arg_type->name_off); | |
9362 | ||
9363 | reg_len = strlen(reg_name); | |
9364 | arg_len = strlen(arg_name); | |
9365 | ||
9366 | /* Exactly one of the two type names may be suffixed with ___init, so | |
9367 | * if the strings are the same size, they can't possibly be no-cast | |
9368 | * aliases of one another. If you have two of the same type names, e.g. | |
9369 | * they're both nf_conn___init, it would be improper to return true | |
9370 | * because they are _not_ no-cast aliases, they are the same type. | |
9371 | */ | |
9372 | if (reg_len == arg_len) | |
9373 | return false; | |
9374 | ||
9375 | /* Either of the two names must be the other name, suffixed with ___init. */ | |
9376 | if ((reg_len != arg_len + pattern_len) && | |
9377 | (arg_len != reg_len + pattern_len)) | |
9378 | return false; | |
9379 | ||
9380 | if (reg_len < arg_len) { | |
9381 | search_needle = strstr(arg_name, NOCAST_ALIAS_SUFFIX); | |
9382 | cmp_len = reg_len; | |
9383 | } else { | |
9384 | search_needle = strstr(reg_name, NOCAST_ALIAS_SUFFIX); | |
9385 | cmp_len = arg_len; | |
9386 | } | |
9387 | ||
9388 | if (!search_needle) | |
9389 | return false; | |
9390 | ||
9391 | /* ___init suffix must come at the end of the name */ | |
9392 | if (*(search_needle + pattern_len) != '\0') | |
9393 | return false; | |
9394 | ||
9395 | return !strncmp(reg_name, arg_name, cmp_len); | |
9396 | } | |
e6199511 | 9397 | |
f6be98d1 | 9398 | #ifdef CONFIG_BPF_JIT |
e6199511 | 9399 | static int |
f6be98d1 KFL |
9400 | btf_add_struct_ops(struct btf *btf, struct bpf_struct_ops *st_ops, |
9401 | struct bpf_verifier_log *log) | |
e6199511 KFL |
9402 | { |
9403 | struct btf_struct_ops_tab *tab, *new_tab; | |
f6be98d1 | 9404 | int i, err; |
e6199511 KFL |
9405 | |
9406 | tab = btf->struct_ops_tab; | |
9407 | if (!tab) { | |
41948afc | 9408 | tab = kzalloc(struct_size(tab, ops, 4), GFP_KERNEL); |
e6199511 KFL |
9409 | if (!tab) |
9410 | return -ENOMEM; | |
9411 | tab->capacity = 4; | |
9412 | btf->struct_ops_tab = tab; | |
9413 | } | |
9414 | ||
9415 | for (i = 0; i < tab->cnt; i++) | |
9416 | if (tab->ops[i].st_ops == st_ops) | |
9417 | return -EEXIST; | |
9418 | ||
9419 | if (tab->cnt == tab->capacity) { | |
9420 | new_tab = krealloc(tab, | |
41948afc | 9421 | struct_size(tab, ops, tab->capacity * 2), |
e6199511 KFL |
9422 | GFP_KERNEL); |
9423 | if (!new_tab) | |
9424 | return -ENOMEM; | |
9425 | tab = new_tab; | |
9426 | tab->capacity *= 2; | |
9427 | btf->struct_ops_tab = tab; | |
9428 | } | |
9429 | ||
9430 | tab->ops[btf->struct_ops_tab->cnt].st_ops = st_ops; | |
9431 | ||
f6be98d1 KFL |
9432 | err = bpf_struct_ops_desc_init(&tab->ops[btf->struct_ops_tab->cnt], btf, log); |
9433 | if (err) | |
9434 | return err; | |
9435 | ||
e6199511 KFL |
9436 | btf->struct_ops_tab->cnt++; |
9437 | ||
9438 | return 0; | |
9439 | } | |
f6be98d1 KFL |
9440 | |
9441 | const struct bpf_struct_ops_desc * | |
9442 | bpf_struct_ops_find_value(struct btf *btf, u32 value_id) | |
9443 | { | |
9444 | const struct bpf_struct_ops_desc *st_ops_list; | |
9445 | unsigned int i; | |
9446 | u32 cnt; | |
9447 | ||
9448 | if (!value_id) | |
9449 | return NULL; | |
9450 | if (!btf->struct_ops_tab) | |
9451 | return NULL; | |
9452 | ||
9453 | cnt = btf->struct_ops_tab->cnt; | |
9454 | st_ops_list = btf->struct_ops_tab->ops; | |
9455 | for (i = 0; i < cnt; i++) { | |
9456 | if (st_ops_list[i].value_id == value_id) | |
9457 | return &st_ops_list[i]; | |
9458 | } | |
9459 | ||
9460 | return NULL; | |
9461 | } | |
9462 | ||
9463 | const struct bpf_struct_ops_desc * | |
9464 | bpf_struct_ops_find(struct btf *btf, u32 type_id) | |
9465 | { | |
9466 | const struct bpf_struct_ops_desc *st_ops_list; | |
9467 | unsigned int i; | |
9468 | u32 cnt; | |
9469 | ||
9470 | if (!type_id) | |
9471 | return NULL; | |
9472 | if (!btf->struct_ops_tab) | |
9473 | return NULL; | |
9474 | ||
9475 | cnt = btf->struct_ops_tab->cnt; | |
9476 | st_ops_list = btf->struct_ops_tab->ops; | |
9477 | for (i = 0; i < cnt; i++) { | |
9478 | if (st_ops_list[i].type_id == type_id) | |
9479 | return &st_ops_list[i]; | |
9480 | } | |
9481 | ||
9482 | return NULL; | |
9483 | } | |
9484 | ||
9485 | int __register_bpf_struct_ops(struct bpf_struct_ops *st_ops) | |
9486 | { | |
9487 | struct bpf_verifier_log *log; | |
9488 | struct btf *btf; | |
9489 | int err = 0; | |
9490 | ||
9491 | btf = btf_get_module_btf(st_ops->owner); | |
9492 | if (!btf) | |
947e56f8 GT |
9493 | return check_btf_kconfigs(st_ops->owner, "struct_ops"); |
9494 | if (IS_ERR(btf)) | |
9495 | return PTR_ERR(btf); | |
f6be98d1 KFL |
9496 | |
9497 | log = kzalloc(sizeof(*log), GFP_KERNEL | __GFP_NOWARN); | |
9498 | if (!log) { | |
9499 | err = -ENOMEM; | |
9500 | goto errout; | |
9501 | } | |
9502 | ||
9503 | log->level = BPF_LOG_KERNEL; | |
9504 | ||
9505 | err = btf_add_struct_ops(btf, st_ops, log); | |
9506 | ||
9507 | errout: | |
9508 | kfree(log); | |
9509 | btf_put(btf); | |
9510 | ||
9511 | return err; | |
9512 | } | |
9513 | EXPORT_SYMBOL_GPL(__register_bpf_struct_ops); | |
9514 | #endif | |
6115a0ae KFL |
9515 | |
9516 | bool btf_param_match_suffix(const struct btf *btf, | |
9517 | const struct btf_param *arg, | |
9518 | const char *suffix) | |
9519 | { | |
9520 | int suffix_len = strlen(suffix), len; | |
9521 | const char *param_name; | |
9522 | ||
9523 | /* In the future, this can be ported to use BTF tagging */ | |
9524 | param_name = btf_name_by_offset(btf, arg->name_off); | |
9525 | if (str_is_empty(param_name)) | |
9526 | return false; | |
9527 | len = strlen(param_name); | |
9528 | if (len <= suffix_len) | |
9529 | return false; | |
9530 | param_name += len - suffix_len; | |
9531 | return !strncmp(param_name, suffix, suffix_len); | |
9532 | } |