1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2 /* Copyright (c) 2019 Facebook */
3
4 #ifdef __KERNEL__
5 #include <linux/bpf.h>
6 #include <linux/btf.h>
7 #include <linux/string.h>
8 #include <linux/bpf_verifier.h>
9 #include "relo_core.h"
10
btf_kind_str(const struct btf_type * t)11 static const char *btf_kind_str(const struct btf_type *t)
12 {
13 return btf_type_str(t);
14 }
15
is_ldimm64_insn(struct bpf_insn * insn)16 static bool is_ldimm64_insn(struct bpf_insn *insn)
17 {
18 return insn->code == (BPF_LD | BPF_IMM | BPF_DW);
19 }
20
21 static const struct btf_type *
skip_mods_and_typedefs(const struct btf * btf,u32 id,u32 * res_id)22 skip_mods_and_typedefs(const struct btf *btf, u32 id, u32 *res_id)
23 {
24 return btf_type_skip_modifiers(btf, id, res_id);
25 }
26
btf__name_by_offset(const struct btf * btf,u32 offset)27 static const char *btf__name_by_offset(const struct btf *btf, u32 offset)
28 {
29 return btf_name_by_offset(btf, offset);
30 }
31
btf__resolve_size(const struct btf * btf,u32 type_id)32 static s64 btf__resolve_size(const struct btf *btf, u32 type_id)
33 {
34 const struct btf_type *t;
35 int size;
36
37 t = btf_type_by_id(btf, type_id);
38 t = btf_resolve_size(btf, t, &size);
39 if (IS_ERR(t))
40 return PTR_ERR(t);
41 return size;
42 }
43
44 enum libbpf_print_level {
45 LIBBPF_WARN,
46 LIBBPF_INFO,
47 LIBBPF_DEBUG,
48 };
49
50 #undef pr_warn
51 #undef pr_info
52 #undef pr_debug
53 #define pr_warn(fmt, log, ...) bpf_log((void *)log, fmt, "", ##__VA_ARGS__)
54 #define pr_info(fmt, log, ...) bpf_log((void *)log, fmt, "", ##__VA_ARGS__)
55 #define pr_debug(fmt, log, ...) bpf_log((void *)log, fmt, "", ##__VA_ARGS__)
56 #define libbpf_print(level, fmt, ...) bpf_log((void *)prog_name, fmt, ##__VA_ARGS__)
57 #else
58 #include <stdio.h>
59 #include <string.h>
60 #include <errno.h>
61 #include <ctype.h>
62 #include <linux/err.h>
63
64 #include "libbpf.h"
65 #include "bpf.h"
66 #include "btf.h"
67 #include "str_error.h"
68 #include "libbpf_internal.h"
69 #endif
70
is_flex_arr(const struct btf * btf,const struct bpf_core_accessor * acc,const struct btf_array * arr)71 static bool is_flex_arr(const struct btf *btf,
72 const struct bpf_core_accessor *acc,
73 const struct btf_array *arr)
74 {
75 const struct btf_type *t;
76
77 /* not a flexible array, if not inside a struct or has non-zero size */
78 if (!acc->name || arr->nelems > 0)
79 return false;
80
81 /* has to be the last member of enclosing struct */
82 t = btf_type_by_id(btf, acc->type_id);
83 return acc->idx == btf_vlen(t) - 1;
84 }
85
core_relo_kind_str(enum bpf_core_relo_kind kind)86 static const char *core_relo_kind_str(enum bpf_core_relo_kind kind)
87 {
88 switch (kind) {
89 case BPF_CORE_FIELD_BYTE_OFFSET: return "byte_off";
90 case BPF_CORE_FIELD_BYTE_SIZE: return "byte_sz";
91 case BPF_CORE_FIELD_EXISTS: return "field_exists";
92 case BPF_CORE_FIELD_SIGNED: return "signed";
93 case BPF_CORE_FIELD_LSHIFT_U64: return "lshift_u64";
94 case BPF_CORE_FIELD_RSHIFT_U64: return "rshift_u64";
95 case BPF_CORE_TYPE_ID_LOCAL: return "local_type_id";
96 case BPF_CORE_TYPE_ID_TARGET: return "target_type_id";
97 case BPF_CORE_TYPE_EXISTS: return "type_exists";
98 case BPF_CORE_TYPE_MATCHES: return "type_matches";
99 case BPF_CORE_TYPE_SIZE: return "type_size";
100 case BPF_CORE_ENUMVAL_EXISTS: return "enumval_exists";
101 case BPF_CORE_ENUMVAL_VALUE: return "enumval_value";
102 default: return "unknown";
103 }
104 }
105
core_relo_is_field_based(enum bpf_core_relo_kind kind)106 static bool core_relo_is_field_based(enum bpf_core_relo_kind kind)
107 {
108 switch (kind) {
109 case BPF_CORE_FIELD_BYTE_OFFSET:
110 case BPF_CORE_FIELD_BYTE_SIZE:
111 case BPF_CORE_FIELD_EXISTS:
112 case BPF_CORE_FIELD_SIGNED:
113 case BPF_CORE_FIELD_LSHIFT_U64:
114 case BPF_CORE_FIELD_RSHIFT_U64:
115 return true;
116 default:
117 return false;
118 }
119 }
120
core_relo_is_type_based(enum bpf_core_relo_kind kind)121 static bool core_relo_is_type_based(enum bpf_core_relo_kind kind)
122 {
123 switch (kind) {
124 case BPF_CORE_TYPE_ID_LOCAL:
125 case BPF_CORE_TYPE_ID_TARGET:
126 case BPF_CORE_TYPE_EXISTS:
127 case BPF_CORE_TYPE_MATCHES:
128 case BPF_CORE_TYPE_SIZE:
129 return true;
130 default:
131 return false;
132 }
133 }
134
core_relo_is_enumval_based(enum bpf_core_relo_kind kind)135 static bool core_relo_is_enumval_based(enum bpf_core_relo_kind kind)
136 {
137 switch (kind) {
138 case BPF_CORE_ENUMVAL_EXISTS:
139 case BPF_CORE_ENUMVAL_VALUE:
140 return true;
141 default:
142 return false;
143 }
144 }
145
__bpf_core_types_are_compat(const struct btf * local_btf,__u32 local_id,const struct btf * targ_btf,__u32 targ_id,int level)146 int __bpf_core_types_are_compat(const struct btf *local_btf, __u32 local_id,
147 const struct btf *targ_btf, __u32 targ_id, int level)
148 {
149 const struct btf_type *local_type, *targ_type;
150 int depth = 32; /* max recursion depth */
151
152 /* caller made sure that names match (ignoring flavor suffix) */
153 local_type = btf_type_by_id(local_btf, local_id);
154 targ_type = btf_type_by_id(targ_btf, targ_id);
155 if (!btf_kind_core_compat(local_type, targ_type))
156 return 0;
157
158 recur:
159 depth--;
160 if (depth < 0)
161 return -EINVAL;
162
163 local_type = skip_mods_and_typedefs(local_btf, local_id, &local_id);
164 targ_type = skip_mods_and_typedefs(targ_btf, targ_id, &targ_id);
165 if (!local_type || !targ_type)
166 return -EINVAL;
167
168 if (!btf_kind_core_compat(local_type, targ_type))
169 return 0;
170
171 switch (btf_kind(local_type)) {
172 case BTF_KIND_UNKN:
173 case BTF_KIND_STRUCT:
174 case BTF_KIND_UNION:
175 case BTF_KIND_ENUM:
176 case BTF_KIND_FWD:
177 case BTF_KIND_ENUM64:
178 return 1;
179 case BTF_KIND_INT:
180 /* just reject deprecated bitfield-like integers; all other
181 * integers are by default compatible between each other
182 */
183 return btf_int_offset(local_type) == 0 && btf_int_offset(targ_type) == 0;
184 case BTF_KIND_PTR:
185 local_id = local_type->type;
186 targ_id = targ_type->type;
187 goto recur;
188 case BTF_KIND_ARRAY:
189 local_id = btf_array(local_type)->type;
190 targ_id = btf_array(targ_type)->type;
191 goto recur;
192 case BTF_KIND_FUNC_PROTO: {
193 struct btf_param *local_p = btf_params(local_type);
194 struct btf_param *targ_p = btf_params(targ_type);
195 __u16 local_vlen = btf_vlen(local_type);
196 __u16 targ_vlen = btf_vlen(targ_type);
197 int i, err;
198
199 if (local_vlen != targ_vlen)
200 return 0;
201
202 for (i = 0; i < local_vlen; i++, local_p++, targ_p++) {
203 if (level <= 0)
204 return -EINVAL;
205
206 skip_mods_and_typedefs(local_btf, local_p->type, &local_id);
207 skip_mods_and_typedefs(targ_btf, targ_p->type, &targ_id);
208 err = __bpf_core_types_are_compat(local_btf, local_id, targ_btf, targ_id,
209 level - 1);
210 if (err <= 0)
211 return err;
212 }
213
214 /* tail recurse for return type check */
215 skip_mods_and_typedefs(local_btf, local_type->type, &local_id);
216 skip_mods_and_typedefs(targ_btf, targ_type->type, &targ_id);
217 goto recur;
218 }
219 default:
220 pr_warn("unexpected kind %s relocated, local [%d], target [%d]\n",
221 btf_kind_str(local_type), local_id, targ_id);
222 return 0;
223 }
224 }
225
226 /*
227 * Turn bpf_core_relo into a low- and high-level spec representation,
228 * validating correctness along the way, as well as calculating resulting
229 * field bit offset, specified by accessor string. Low-level spec captures
230 * every single level of nestedness, including traversing anonymous
231 * struct/union members. High-level one only captures semantically meaningful
232 * "turning points": named fields and array indicies.
233 * E.g., for this case:
234 *
235 * struct sample {
236 * int __unimportant;
237 * struct {
238 * int __1;
239 * int __2;
240 * int a[7];
241 * };
242 * };
243 *
244 * struct sample *s = ...;
245 *
246 * int x = &s->a[3]; // access string = '0:1:2:3'
247 *
248 * Low-level spec has 1:1 mapping with each element of access string (it's
249 * just a parsed access string representation): [0, 1, 2, 3].
250 *
251 * High-level spec will capture only 3 points:
252 * - initial zero-index access by pointer (&s->... is the same as &s[0]...);
253 * - field 'a' access (corresponds to '2' in low-level spec);
254 * - array element #3 access (corresponds to '3' in low-level spec).
255 *
256 * Type-based relocations (TYPE_EXISTS/TYPE_MATCHES/TYPE_SIZE,
257 * TYPE_ID_LOCAL/TYPE_ID_TARGET) don't capture any field information. Their
258 * spec and raw_spec are kept empty.
259 *
260 * Enum value-based relocations (ENUMVAL_EXISTS/ENUMVAL_VALUE) use access
261 * string to specify enumerator's value index that need to be relocated.
262 */
bpf_core_parse_spec(const char * prog_name,const struct btf * btf,const struct bpf_core_relo * relo,struct bpf_core_spec * spec)263 int bpf_core_parse_spec(const char *prog_name, const struct btf *btf,
264 const struct bpf_core_relo *relo,
265 struct bpf_core_spec *spec)
266 {
267 int access_idx, parsed_len, i;
268 struct bpf_core_accessor *acc;
269 const struct btf_type *t;
270 const char *name, *spec_str;
271 __u32 id, name_off;
272 __s64 sz;
273
274 spec_str = btf__name_by_offset(btf, relo->access_str_off);
275 if (str_is_empty(spec_str) || *spec_str == ':')
276 return -EINVAL;
277
278 memset(spec, 0, sizeof(*spec));
279 spec->btf = btf;
280 spec->root_type_id = relo->type_id;
281 spec->relo_kind = relo->kind;
282
283 /* type-based relocations don't have a field access string */
284 if (core_relo_is_type_based(relo->kind)) {
285 if (strcmp(spec_str, "0"))
286 return -EINVAL;
287 return 0;
288 }
289
290 /* parse spec_str="0:1:2:3:4" into array raw_spec=[0, 1, 2, 3, 4] */
291 while (*spec_str) {
292 if (*spec_str == ':')
293 ++spec_str;
294 if (sscanf(spec_str, "%d%n", &access_idx, &parsed_len) != 1)
295 return -EINVAL;
296 if (spec->raw_len == BPF_CORE_SPEC_MAX_LEN)
297 return -E2BIG;
298 spec_str += parsed_len;
299 spec->raw_spec[spec->raw_len++] = access_idx;
300 }
301
302 if (spec->raw_len == 0)
303 return -EINVAL;
304
305 t = skip_mods_and_typedefs(btf, relo->type_id, &id);
306 if (!t)
307 return -EINVAL;
308
309 access_idx = spec->raw_spec[0];
310 acc = &spec->spec[0];
311 acc->type_id = id;
312 acc->idx = access_idx;
313 spec->len++;
314
315 if (core_relo_is_enumval_based(relo->kind)) {
316 if (!btf_is_any_enum(t) || spec->raw_len > 1 || access_idx >= btf_vlen(t))
317 return -EINVAL;
318
319 /* record enumerator name in a first accessor */
320 name_off = btf_is_enum(t) ? btf_enum(t)[access_idx].name_off
321 : btf_enum64(t)[access_idx].name_off;
322 acc->name = btf__name_by_offset(btf, name_off);
323 return 0;
324 }
325
326 if (!core_relo_is_field_based(relo->kind))
327 return -EINVAL;
328
329 sz = btf__resolve_size(btf, id);
330 if (sz < 0)
331 return sz;
332 spec->bit_offset = access_idx * sz * 8;
333
334 for (i = 1; i < spec->raw_len; i++) {
335 t = skip_mods_and_typedefs(btf, id, &id);
336 if (!t)
337 return -EINVAL;
338
339 access_idx = spec->raw_spec[i];
340 acc = &spec->spec[spec->len];
341
342 if (btf_is_composite(t)) {
343 const struct btf_member *m;
344 __u32 bit_offset;
345
346 if (access_idx >= btf_vlen(t))
347 return -EINVAL;
348
349 bit_offset = btf_member_bit_offset(t, access_idx);
350 spec->bit_offset += bit_offset;
351
352 m = btf_members(t) + access_idx;
353 if (m->name_off) {
354 name = btf__name_by_offset(btf, m->name_off);
355 if (str_is_empty(name))
356 return -EINVAL;
357
358 acc->type_id = id;
359 acc->idx = access_idx;
360 acc->name = name;
361 spec->len++;
362 }
363
364 id = m->type;
365 } else if (btf_is_array(t)) {
366 const struct btf_array *a = btf_array(t);
367 bool flex;
368
369 t = skip_mods_and_typedefs(btf, a->type, &id);
370 if (!t)
371 return -EINVAL;
372
373 flex = is_flex_arr(btf, acc - 1, a);
374 if (!flex && access_idx >= a->nelems)
375 return -EINVAL;
376
377 spec->spec[spec->len].type_id = id;
378 spec->spec[spec->len].idx = access_idx;
379 spec->len++;
380
381 sz = btf__resolve_size(btf, id);
382 if (sz < 0)
383 return sz;
384 spec->bit_offset += access_idx * sz * 8;
385 } else {
386 pr_warn("prog '%s': relo for [%u] %s (at idx %d) captures type [%d] of unexpected kind %s\n",
387 prog_name, relo->type_id, spec_str, i, id, btf_kind_str(t));
388 return -EINVAL;
389 }
390 }
391
392 return 0;
393 }
394
395 /* Check two types for compatibility for the purpose of field access
396 * relocation. const/volatile/restrict and typedefs are skipped to ensure we
397 * are relocating semantically compatible entities:
398 * - any two STRUCTs/UNIONs are compatible and can be mixed;
399 * - any two FWDs are compatible, if their names match (modulo flavor suffix);
400 * - any two PTRs are always compatible;
401 * - for ENUMs, names should be the same (ignoring flavor suffix) or at
402 * least one of enums should be anonymous;
403 * - for ENUMs, check sizes, names are ignored;
404 * - for INT, size and signedness are ignored;
405 * - any two FLOATs are always compatible;
406 * - for ARRAY, dimensionality is ignored, element types are checked for
407 * compatibility recursively;
408 * - everything else shouldn't be ever a target of relocation.
409 * These rules are not set in stone and probably will be adjusted as we get
410 * more experience with using BPF CO-RE relocations.
411 */
bpf_core_fields_are_compat(const struct btf * local_btf,__u32 local_id,const struct btf * targ_btf,__u32 targ_id)412 static int bpf_core_fields_are_compat(const struct btf *local_btf,
413 __u32 local_id,
414 const struct btf *targ_btf,
415 __u32 targ_id)
416 {
417 const struct btf_type *local_type, *targ_type;
418
419 recur:
420 local_type = skip_mods_and_typedefs(local_btf, local_id, &local_id);
421 targ_type = skip_mods_and_typedefs(targ_btf, targ_id, &targ_id);
422 if (!local_type || !targ_type)
423 return -EINVAL;
424
425 if (btf_is_composite(local_type) && btf_is_composite(targ_type))
426 return 1;
427 if (!btf_kind_core_compat(local_type, targ_type))
428 return 0;
429
430 switch (btf_kind(local_type)) {
431 case BTF_KIND_PTR:
432 case BTF_KIND_FLOAT:
433 return 1;
434 case BTF_KIND_FWD:
435 case BTF_KIND_ENUM64:
436 case BTF_KIND_ENUM: {
437 const char *local_name, *targ_name;
438 size_t local_len, targ_len;
439
440 local_name = btf__name_by_offset(local_btf,
441 local_type->name_off);
442 targ_name = btf__name_by_offset(targ_btf, targ_type->name_off);
443 local_len = bpf_core_essential_name_len(local_name);
444 targ_len = bpf_core_essential_name_len(targ_name);
445 /* one of them is anonymous or both w/ same flavor-less names */
446 return local_len == 0 || targ_len == 0 ||
447 (local_len == targ_len &&
448 strncmp(local_name, targ_name, local_len) == 0);
449 }
450 case BTF_KIND_INT:
451 /* just reject deprecated bitfield-like integers; all other
452 * integers are by default compatible between each other
453 */
454 return btf_int_offset(local_type) == 0 &&
455 btf_int_offset(targ_type) == 0;
456 case BTF_KIND_ARRAY:
457 local_id = btf_array(local_type)->type;
458 targ_id = btf_array(targ_type)->type;
459 goto recur;
460 default:
461 return 0;
462 }
463 }
464
465 /*
466 * Given single high-level named field accessor in local type, find
467 * corresponding high-level accessor for a target type. Along the way,
468 * maintain low-level spec for target as well. Also keep updating target
469 * bit offset.
470 *
471 * Searching is performed through recursive exhaustive enumeration of all
472 * fields of a struct/union. If there are any anonymous (embedded)
473 * structs/unions, they are recursively searched as well. If field with
474 * desired name is found, check compatibility between local and target types,
475 * before returning result.
476 *
477 * 1 is returned, if field is found.
478 * 0 is returned if no compatible field is found.
479 * <0 is returned on error.
480 */
bpf_core_match_member(const struct btf * local_btf,const struct bpf_core_accessor * local_acc,const struct btf * targ_btf,__u32 targ_id,struct bpf_core_spec * spec,__u32 * next_targ_id)481 static int bpf_core_match_member(const struct btf *local_btf,
482 const struct bpf_core_accessor *local_acc,
483 const struct btf *targ_btf,
484 __u32 targ_id,
485 struct bpf_core_spec *spec,
486 __u32 *next_targ_id)
487 {
488 const struct btf_type *local_type, *targ_type;
489 const struct btf_member *local_member, *m;
490 const char *local_name, *targ_name;
491 __u32 local_id;
492 int i, n, found;
493
494 targ_type = skip_mods_and_typedefs(targ_btf, targ_id, &targ_id);
495 if (!targ_type)
496 return -EINVAL;
497 if (!btf_is_composite(targ_type))
498 return 0;
499
500 local_id = local_acc->type_id;
501 local_type = btf_type_by_id(local_btf, local_id);
502 local_member = btf_members(local_type) + local_acc->idx;
503 local_name = btf__name_by_offset(local_btf, local_member->name_off);
504
505 n = btf_vlen(targ_type);
506 m = btf_members(targ_type);
507 for (i = 0; i < n; i++, m++) {
508 __u32 bit_offset;
509
510 bit_offset = btf_member_bit_offset(targ_type, i);
511
512 /* too deep struct/union/array nesting */
513 if (spec->raw_len == BPF_CORE_SPEC_MAX_LEN)
514 return -E2BIG;
515
516 /* speculate this member will be the good one */
517 spec->bit_offset += bit_offset;
518 spec->raw_spec[spec->raw_len++] = i;
519
520 targ_name = btf__name_by_offset(targ_btf, m->name_off);
521 if (str_is_empty(targ_name)) {
522 /* embedded struct/union, we need to go deeper */
523 found = bpf_core_match_member(local_btf, local_acc,
524 targ_btf, m->type,
525 spec, next_targ_id);
526 if (found) /* either found or error */
527 return found;
528 } else if (strcmp(local_name, targ_name) == 0) {
529 /* matching named field */
530 struct bpf_core_accessor *targ_acc;
531
532 targ_acc = &spec->spec[spec->len++];
533 targ_acc->type_id = targ_id;
534 targ_acc->idx = i;
535 targ_acc->name = targ_name;
536
537 *next_targ_id = m->type;
538 found = bpf_core_fields_are_compat(local_btf,
539 local_member->type,
540 targ_btf, m->type);
541 if (!found)
542 spec->len--; /* pop accessor */
543 return found;
544 }
545 /* member turned out not to be what we looked for */
546 spec->bit_offset -= bit_offset;
547 spec->raw_len--;
548 }
549
550 return 0;
551 }
552
553 /*
554 * Try to match local spec to a target type and, if successful, produce full
555 * target spec (high-level, low-level + bit offset).
556 */
bpf_core_spec_match(struct bpf_core_spec * local_spec,const struct btf * targ_btf,__u32 targ_id,struct bpf_core_spec * targ_spec)557 static int bpf_core_spec_match(struct bpf_core_spec *local_spec,
558 const struct btf *targ_btf, __u32 targ_id,
559 struct bpf_core_spec *targ_spec)
560 {
561 const struct btf_type *targ_type;
562 const struct bpf_core_accessor *local_acc;
563 struct bpf_core_accessor *targ_acc;
564 int i, sz, matched;
565 __u32 name_off;
566
567 memset(targ_spec, 0, sizeof(*targ_spec));
568 targ_spec->btf = targ_btf;
569 targ_spec->root_type_id = targ_id;
570 targ_spec->relo_kind = local_spec->relo_kind;
571
572 if (core_relo_is_type_based(local_spec->relo_kind)) {
573 if (local_spec->relo_kind == BPF_CORE_TYPE_MATCHES)
574 return bpf_core_types_match(local_spec->btf,
575 local_spec->root_type_id,
576 targ_btf, targ_id);
577 else
578 return bpf_core_types_are_compat(local_spec->btf,
579 local_spec->root_type_id,
580 targ_btf, targ_id);
581 }
582
583 local_acc = &local_spec->spec[0];
584 targ_acc = &targ_spec->spec[0];
585
586 if (core_relo_is_enumval_based(local_spec->relo_kind)) {
587 size_t local_essent_len, targ_essent_len;
588 const char *targ_name;
589
590 /* has to resolve to an enum */
591 targ_type = skip_mods_and_typedefs(targ_spec->btf, targ_id, &targ_id);
592 if (!btf_is_any_enum(targ_type))
593 return 0;
594
595 local_essent_len = bpf_core_essential_name_len(local_acc->name);
596
597 for (i = 0; i < btf_vlen(targ_type); i++) {
598 if (btf_is_enum(targ_type))
599 name_off = btf_enum(targ_type)[i].name_off;
600 else
601 name_off = btf_enum64(targ_type)[i].name_off;
602
603 targ_name = btf__name_by_offset(targ_spec->btf, name_off);
604 targ_essent_len = bpf_core_essential_name_len(targ_name);
605 if (targ_essent_len != local_essent_len)
606 continue;
607 if (strncmp(local_acc->name, targ_name, local_essent_len) == 0) {
608 targ_acc->type_id = targ_id;
609 targ_acc->idx = i;
610 targ_acc->name = targ_name;
611 targ_spec->len++;
612 targ_spec->raw_spec[targ_spec->raw_len] = targ_acc->idx;
613 targ_spec->raw_len++;
614 return 1;
615 }
616 }
617 return 0;
618 }
619
620 if (!core_relo_is_field_based(local_spec->relo_kind))
621 return -EINVAL;
622
623 for (i = 0; i < local_spec->len; i++, local_acc++, targ_acc++) {
624 targ_type = skip_mods_and_typedefs(targ_spec->btf, targ_id,
625 &targ_id);
626 if (!targ_type)
627 return -EINVAL;
628
629 if (local_acc->name) {
630 matched = bpf_core_match_member(local_spec->btf,
631 local_acc,
632 targ_btf, targ_id,
633 targ_spec, &targ_id);
634 if (matched <= 0)
635 return matched;
636 } else {
637 /* for i=0, targ_id is already treated as array element
638 * type (because it's the original struct), for others
639 * we should find array element type first
640 */
641 if (i > 0) {
642 const struct btf_array *a;
643 bool flex;
644
645 if (!btf_is_array(targ_type))
646 return 0;
647
648 a = btf_array(targ_type);
649 flex = is_flex_arr(targ_btf, targ_acc - 1, a);
650 if (!flex && local_acc->idx >= a->nelems)
651 return 0;
652 if (!skip_mods_and_typedefs(targ_btf, a->type,
653 &targ_id))
654 return -EINVAL;
655 }
656
657 /* too deep struct/union/array nesting */
658 if (targ_spec->raw_len == BPF_CORE_SPEC_MAX_LEN)
659 return -E2BIG;
660
661 targ_acc->type_id = targ_id;
662 targ_acc->idx = local_acc->idx;
663 targ_acc->name = NULL;
664 targ_spec->len++;
665 targ_spec->raw_spec[targ_spec->raw_len] = targ_acc->idx;
666 targ_spec->raw_len++;
667
668 sz = btf__resolve_size(targ_btf, targ_id);
669 if (sz < 0)
670 return sz;
671 targ_spec->bit_offset += local_acc->idx * sz * 8;
672 }
673 }
674
675 return 1;
676 }
677
bpf_core_calc_field_relo(const char * prog_name,const struct bpf_core_relo * relo,const struct bpf_core_spec * spec,__u64 * val,__u32 * field_sz,__u32 * type_id,bool * validate)678 static int bpf_core_calc_field_relo(const char *prog_name,
679 const struct bpf_core_relo *relo,
680 const struct bpf_core_spec *spec,
681 __u64 *val, __u32 *field_sz, __u32 *type_id,
682 bool *validate)
683 {
684 const struct bpf_core_accessor *acc;
685 const struct btf_type *t;
686 __u32 byte_off, byte_sz, bit_off, bit_sz, field_type_id, elem_id;
687 const struct btf_member *m;
688 const struct btf_type *mt;
689 bool bitfield;
690 __s64 sz;
691
692 *field_sz = 0;
693
694 if (relo->kind == BPF_CORE_FIELD_EXISTS) {
695 *val = spec ? 1 : 0;
696 return 0;
697 }
698
699 if (!spec)
700 return -EUCLEAN; /* request instruction poisoning */
701
702 acc = &spec->spec[spec->len - 1];
703 t = btf_type_by_id(spec->btf, acc->type_id);
704
705 /* a[n] accessor needs special handling */
706 if (!acc->name) {
707 if (relo->kind == BPF_CORE_FIELD_BYTE_OFFSET) {
708 *val = spec->bit_offset / 8;
709 /* remember field size for load/store mem size;
710 * note, for arrays we care about individual element
711 * sizes, not the overall array size
712 */
713 t = skip_mods_and_typedefs(spec->btf, acc->type_id, &elem_id);
714 while (btf_is_array(t))
715 t = skip_mods_and_typedefs(spec->btf, btf_array(t)->type, &elem_id);
716 sz = btf__resolve_size(spec->btf, elem_id);
717 if (sz < 0)
718 return -EINVAL;
719 *field_sz = sz;
720 *type_id = acc->type_id;
721 } else if (relo->kind == BPF_CORE_FIELD_BYTE_SIZE) {
722 sz = btf__resolve_size(spec->btf, acc->type_id);
723 if (sz < 0)
724 return -EINVAL;
725 *val = sz;
726 } else {
727 pr_warn("prog '%s': relo %d at insn #%d can't be applied to array access\n",
728 prog_name, relo->kind, relo->insn_off / 8);
729 return -EINVAL;
730 }
731 if (validate)
732 *validate = true;
733 return 0;
734 }
735
736 m = btf_members(t) + acc->idx;
737 mt = skip_mods_and_typedefs(spec->btf, m->type, &field_type_id);
738 bit_off = spec->bit_offset;
739 bit_sz = btf_member_bitfield_size(t, acc->idx);
740
741 bitfield = bit_sz > 0;
742 if (bitfield) {
743 byte_sz = mt->size;
744 byte_off = bit_off / 8 / byte_sz * byte_sz;
745 /* figure out smallest int size necessary for bitfield load */
746 while (bit_off + bit_sz - byte_off * 8 > byte_sz * 8) {
747 if (byte_sz >= 8) {
748 /* bitfield can't be read with 64-bit read */
749 pr_warn("prog '%s': relo %d at insn #%d can't be satisfied for bitfield\n",
750 prog_name, relo->kind, relo->insn_off / 8);
751 return -E2BIG;
752 }
753 byte_sz *= 2;
754 byte_off = bit_off / 8 / byte_sz * byte_sz;
755 }
756 } else {
757 sz = btf__resolve_size(spec->btf, field_type_id);
758 if (sz < 0)
759 return -EINVAL;
760 byte_sz = sz;
761 byte_off = spec->bit_offset / 8;
762 bit_sz = byte_sz * 8;
763 }
764
765 /* for bitfields, all the relocatable aspects are ambiguous and we
766 * might disagree with compiler, so turn off validation of expected
767 * value, except for signedness
768 */
769 if (validate)
770 *validate = !bitfield;
771
772 switch (relo->kind) {
773 case BPF_CORE_FIELD_BYTE_OFFSET:
774 *val = byte_off;
775 if (!bitfield) {
776 /* remember field size for load/store mem size;
777 * note, for arrays we care about individual element
778 * sizes, not the overall array size
779 */
780 t = skip_mods_and_typedefs(spec->btf, field_type_id, &elem_id);
781 while (btf_is_array(t))
782 t = skip_mods_and_typedefs(spec->btf, btf_array(t)->type, &elem_id);
783 sz = btf__resolve_size(spec->btf, elem_id);
784 if (sz < 0)
785 return -EINVAL;
786 *field_sz = sz;
787 *type_id = field_type_id;
788 }
789 break;
790 case BPF_CORE_FIELD_BYTE_SIZE:
791 *val = byte_sz;
792 break;
793 case BPF_CORE_FIELD_SIGNED:
794 *val = (btf_is_any_enum(mt) && BTF_INFO_KFLAG(mt->info)) ||
795 (btf_is_int(mt) && (btf_int_encoding(mt) & BTF_INT_SIGNED));
796 if (validate)
797 *validate = true; /* signedness is never ambiguous */
798 break;
799 case BPF_CORE_FIELD_LSHIFT_U64:
800 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
801 *val = 64 - (bit_off + bit_sz - byte_off * 8);
802 #else
803 *val = (8 - byte_sz) * 8 + (bit_off - byte_off * 8);
804 #endif
805 break;
806 case BPF_CORE_FIELD_RSHIFT_U64:
807 *val = 64 - bit_sz;
808 if (validate)
809 *validate = true; /* right shift is never ambiguous */
810 break;
811 case BPF_CORE_FIELD_EXISTS:
812 default:
813 return -EOPNOTSUPP;
814 }
815
816 return 0;
817 }
818
bpf_core_calc_type_relo(const struct bpf_core_relo * relo,const struct bpf_core_spec * spec,__u64 * val,bool * validate)819 static int bpf_core_calc_type_relo(const struct bpf_core_relo *relo,
820 const struct bpf_core_spec *spec,
821 __u64 *val, bool *validate)
822 {
823 __s64 sz;
824
825 /* by default, always check expected value in bpf_insn */
826 if (validate)
827 *validate = true;
828
829 /* type-based relos return zero when target type is not found */
830 if (!spec) {
831 *val = 0;
832 return 0;
833 }
834
835 switch (relo->kind) {
836 case BPF_CORE_TYPE_ID_TARGET:
837 *val = spec->root_type_id;
838 /* type ID, embedded in bpf_insn, might change during linking,
839 * so enforcing it is pointless
840 */
841 if (validate)
842 *validate = false;
843 break;
844 case BPF_CORE_TYPE_EXISTS:
845 case BPF_CORE_TYPE_MATCHES:
846 *val = 1;
847 break;
848 case BPF_CORE_TYPE_SIZE:
849 sz = btf__resolve_size(spec->btf, spec->root_type_id);
850 if (sz < 0)
851 return -EINVAL;
852 *val = sz;
853 break;
854 case BPF_CORE_TYPE_ID_LOCAL:
855 /* BPF_CORE_TYPE_ID_LOCAL is handled specially and shouldn't get here */
856 default:
857 return -EOPNOTSUPP;
858 }
859
860 return 0;
861 }
862
bpf_core_calc_enumval_relo(const struct bpf_core_relo * relo,const struct bpf_core_spec * spec,__u64 * val)863 static int bpf_core_calc_enumval_relo(const struct bpf_core_relo *relo,
864 const struct bpf_core_spec *spec,
865 __u64 *val)
866 {
867 const struct btf_type *t;
868
869 switch (relo->kind) {
870 case BPF_CORE_ENUMVAL_EXISTS:
871 *val = spec ? 1 : 0;
872 break;
873 case BPF_CORE_ENUMVAL_VALUE:
874 if (!spec)
875 return -EUCLEAN; /* request instruction poisoning */
876 t = btf_type_by_id(spec->btf, spec->spec[0].type_id);
877 if (btf_is_enum(t))
878 *val = btf_enum(t)[spec->spec[0].idx].val;
879 else
880 *val = btf_enum64_value(btf_enum64(t) + spec->spec[0].idx);
881 break;
882 default:
883 return -EOPNOTSUPP;
884 }
885
886 return 0;
887 }
888
889 /* Calculate original and target relocation values, given local and target
890 * specs and relocation kind. These values are calculated for each candidate.
891 * If there are multiple candidates, resulting values should all be consistent
892 * with each other. Otherwise, libbpf will refuse to proceed due to ambiguity.
893 * If instruction has to be poisoned, *poison will be set to true.
894 */
bpf_core_calc_relo(const char * prog_name,const struct bpf_core_relo * relo,int relo_idx,const struct bpf_core_spec * local_spec,const struct bpf_core_spec * targ_spec,struct bpf_core_relo_res * res)895 static int bpf_core_calc_relo(const char *prog_name,
896 const struct bpf_core_relo *relo,
897 int relo_idx,
898 const struct bpf_core_spec *local_spec,
899 const struct bpf_core_spec *targ_spec,
900 struct bpf_core_relo_res *res)
901 {
902 int err = -EOPNOTSUPP;
903
904 res->orig_val = 0;
905 res->new_val = 0;
906 res->poison = false;
907 res->validate = true;
908 res->fail_memsz_adjust = false;
909 res->orig_sz = res->new_sz = 0;
910 res->orig_type_id = res->new_type_id = 0;
911
912 if (core_relo_is_field_based(relo->kind)) {
913 err = bpf_core_calc_field_relo(prog_name, relo, local_spec,
914 &res->orig_val, &res->orig_sz,
915 &res->orig_type_id, &res->validate);
916 err = err ?: bpf_core_calc_field_relo(prog_name, relo, targ_spec,
917 &res->new_val, &res->new_sz,
918 &res->new_type_id, NULL);
919 if (err)
920 goto done;
921 /* Validate if it's safe to adjust load/store memory size.
922 * Adjustments are performed only if original and new memory
923 * sizes differ.
924 */
925 res->fail_memsz_adjust = false;
926 if (res->orig_sz != res->new_sz) {
927 const struct btf_type *orig_t, *new_t;
928
929 orig_t = btf_type_by_id(local_spec->btf, res->orig_type_id);
930 new_t = btf_type_by_id(targ_spec->btf, res->new_type_id);
931
932 /* There are two use cases in which it's safe to
933 * adjust load/store's mem size:
934 * - reading a 32-bit kernel pointer, while on BPF
935 * size pointers are always 64-bit; in this case
936 * it's safe to "downsize" instruction size due to
937 * pointer being treated as unsigned integer with
938 * zero-extended upper 32-bits;
939 * - reading unsigned integers, again due to
940 * zero-extension is preserving the value correctly.
941 *
942 * In all other cases it's incorrect to attempt to
943 * load/store field because read value will be
944 * incorrect, so we poison relocated instruction.
945 */
946 if (btf_is_ptr(orig_t) && btf_is_ptr(new_t))
947 goto done;
948 if (btf_is_int(orig_t) && btf_is_int(new_t) &&
949 btf_int_encoding(orig_t) != BTF_INT_SIGNED &&
950 btf_int_encoding(new_t) != BTF_INT_SIGNED)
951 goto done;
952
953 /* mark as invalid mem size adjustment, but this will
954 * only be checked for LDX/STX/ST insns
955 */
956 res->fail_memsz_adjust = true;
957 }
958 } else if (core_relo_is_type_based(relo->kind)) {
959 err = bpf_core_calc_type_relo(relo, local_spec, &res->orig_val, &res->validate);
960 err = err ?: bpf_core_calc_type_relo(relo, targ_spec, &res->new_val, NULL);
961 } else if (core_relo_is_enumval_based(relo->kind)) {
962 err = bpf_core_calc_enumval_relo(relo, local_spec, &res->orig_val);
963 err = err ?: bpf_core_calc_enumval_relo(relo, targ_spec, &res->new_val);
964 }
965
966 done:
967 if (err == -EUCLEAN) {
968 /* EUCLEAN is used to signal instruction poisoning request */
969 res->poison = true;
970 err = 0;
971 } else if (err == -EOPNOTSUPP) {
972 /* EOPNOTSUPP means unknown/unsupported relocation */
973 pr_warn("prog '%s': relo #%d: unrecognized CO-RE relocation %s (%d) at insn #%d\n",
974 prog_name, relo_idx, core_relo_kind_str(relo->kind),
975 relo->kind, relo->insn_off / 8);
976 }
977
978 return err;
979 }
980
981 /*
982 * Turn instruction for which CO_RE relocation failed into invalid one with
983 * distinct signature.
984 */
bpf_core_poison_insn(const char * prog_name,int relo_idx,int insn_idx,struct bpf_insn * insn)985 static void bpf_core_poison_insn(const char *prog_name, int relo_idx,
986 int insn_idx, struct bpf_insn *insn)
987 {
988 pr_debug("prog '%s': relo #%d: substituting insn #%d w/ invalid insn\n",
989 prog_name, relo_idx, insn_idx);
990 insn->code = BPF_JMP | BPF_CALL;
991 insn->dst_reg = 0;
992 insn->src_reg = 0;
993 insn->off = 0;
994 /* if this instruction is reachable (not a dead code),
995 * verifier will complain with the following message:
996 * invalid func unknown#195896080
997 */
998 insn->imm = 195896080; /* => 0xbad2310 => "bad relo" */
999 }
1000
insn_bpf_size_to_bytes(struct bpf_insn * insn)1001 static int insn_bpf_size_to_bytes(struct bpf_insn *insn)
1002 {
1003 switch (BPF_SIZE(insn->code)) {
1004 case BPF_DW: return 8;
1005 case BPF_W: return 4;
1006 case BPF_H: return 2;
1007 case BPF_B: return 1;
1008 default: return -1;
1009 }
1010 }
1011
insn_bytes_to_bpf_size(__u32 sz)1012 static int insn_bytes_to_bpf_size(__u32 sz)
1013 {
1014 switch (sz) {
1015 case 8: return BPF_DW;
1016 case 4: return BPF_W;
1017 case 2: return BPF_H;
1018 case 1: return BPF_B;
1019 default: return -1;
1020 }
1021 }
1022
1023 /*
1024 * Patch relocatable BPF instruction.
1025 *
1026 * Patched value is determined by relocation kind and target specification.
1027 * For existence relocations target spec will be NULL if field/type is not found.
1028 * Expected insn->imm value is determined using relocation kind and local
1029 * spec, and is checked before patching instruction. If actual insn->imm value
1030 * is wrong, bail out with error.
1031 *
1032 * Currently supported classes of BPF instruction are:
1033 * 1. rX = <imm> (assignment with immediate operand);
1034 * 2. rX += <imm> (arithmetic operations with immediate operand);
1035 * 3. rX = <imm64> (load with 64-bit immediate value);
1036 * 4. rX = *(T *)(rY + <off>), where T is one of {u8, u16, u32, u64};
1037 * 5. *(T *)(rX + <off>) = rY, where T is one of {u8, u16, u32, u64};
1038 * 6. *(T *)(rX + <off>) = <imm>, where T is one of {u8, u16, u32, u64}.
1039 */
bpf_core_patch_insn(const char * prog_name,struct bpf_insn * insn,int insn_idx,const struct bpf_core_relo * relo,int relo_idx,const struct bpf_core_relo_res * res)1040 int bpf_core_patch_insn(const char *prog_name, struct bpf_insn *insn,
1041 int insn_idx, const struct bpf_core_relo *relo,
1042 int relo_idx, const struct bpf_core_relo_res *res)
1043 {
1044 __u64 orig_val, new_val;
1045 __u8 class;
1046
1047 class = BPF_CLASS(insn->code);
1048
1049 if (res->poison) {
1050 poison:
1051 /* poison second part of ldimm64 to avoid confusing error from
1052 * verifier about "unknown opcode 00"
1053 */
1054 if (is_ldimm64_insn(insn))
1055 bpf_core_poison_insn(prog_name, relo_idx, insn_idx + 1, insn + 1);
1056 bpf_core_poison_insn(prog_name, relo_idx, insn_idx, insn);
1057 return 0;
1058 }
1059
1060 orig_val = res->orig_val;
1061 new_val = res->new_val;
1062
1063 switch (class) {
1064 case BPF_ALU:
1065 case BPF_ALU64:
1066 if (BPF_SRC(insn->code) != BPF_K)
1067 return -EINVAL;
1068 if (res->validate && insn->imm != orig_val) {
1069 pr_warn("prog '%s': relo #%d: unexpected insn #%d (ALU/ALU64) value: got %u, exp %llu -> %llu\n",
1070 prog_name, relo_idx,
1071 insn_idx, insn->imm, (unsigned long long)orig_val,
1072 (unsigned long long)new_val);
1073 return -EINVAL;
1074 }
1075 orig_val = insn->imm;
1076 insn->imm = new_val;
1077 pr_debug("prog '%s': relo #%d: patched insn #%d (ALU/ALU64) imm %llu -> %llu\n",
1078 prog_name, relo_idx, insn_idx,
1079 (unsigned long long)orig_val, (unsigned long long)new_val);
1080 break;
1081 case BPF_LDX:
1082 case BPF_ST:
1083 case BPF_STX:
1084 if (res->validate && insn->off != orig_val) {
1085 pr_warn("prog '%s': relo #%d: unexpected insn #%d (LDX/ST/STX) value: got %u, exp %llu -> %llu\n",
1086 prog_name, relo_idx, insn_idx, insn->off, (unsigned long long)orig_val,
1087 (unsigned long long)new_val);
1088 return -EINVAL;
1089 }
1090 if (new_val > SHRT_MAX) {
1091 pr_warn("prog '%s': relo #%d: insn #%d (LDX/ST/STX) value too big: %llu\n",
1092 prog_name, relo_idx, insn_idx, (unsigned long long)new_val);
1093 return -ERANGE;
1094 }
1095 if (res->fail_memsz_adjust) {
1096 pr_warn("prog '%s': relo #%d: insn #%d (LDX/ST/STX) accesses field incorrectly. "
1097 "Make sure you are accessing pointers, unsigned integers, or fields of matching type and size.\n",
1098 prog_name, relo_idx, insn_idx);
1099 goto poison;
1100 }
1101
1102 orig_val = insn->off;
1103 insn->off = new_val;
1104 pr_debug("prog '%s': relo #%d: patched insn #%d (LDX/ST/STX) off %llu -> %llu\n",
1105 prog_name, relo_idx, insn_idx, (unsigned long long)orig_val,
1106 (unsigned long long)new_val);
1107
1108 if (res->new_sz != res->orig_sz) {
1109 int insn_bytes_sz, insn_bpf_sz;
1110
1111 insn_bytes_sz = insn_bpf_size_to_bytes(insn);
1112 if (insn_bytes_sz != res->orig_sz) {
1113 pr_warn("prog '%s': relo #%d: insn #%d (LDX/ST/STX) unexpected mem size: got %d, exp %u\n",
1114 prog_name, relo_idx, insn_idx, insn_bytes_sz, res->orig_sz);
1115 return -EINVAL;
1116 }
1117
1118 insn_bpf_sz = insn_bytes_to_bpf_size(res->new_sz);
1119 if (insn_bpf_sz < 0) {
1120 pr_warn("prog '%s': relo #%d: insn #%d (LDX/ST/STX) invalid new mem size: %u\n",
1121 prog_name, relo_idx, insn_idx, res->new_sz);
1122 return -EINVAL;
1123 }
1124
1125 insn->code = BPF_MODE(insn->code) | insn_bpf_sz | BPF_CLASS(insn->code);
1126 pr_debug("prog '%s': relo #%d: patched insn #%d (LDX/ST/STX) mem_sz %u -> %u\n",
1127 prog_name, relo_idx, insn_idx, res->orig_sz, res->new_sz);
1128 }
1129 break;
1130 case BPF_LD: {
1131 __u64 imm;
1132
1133 if (!is_ldimm64_insn(insn) ||
1134 insn[0].src_reg != 0 || insn[0].off != 0 ||
1135 insn[1].code != 0 || insn[1].dst_reg != 0 ||
1136 insn[1].src_reg != 0 || insn[1].off != 0) {
1137 pr_warn("prog '%s': relo #%d: insn #%d (LDIMM64) has unexpected form\n",
1138 prog_name, relo_idx, insn_idx);
1139 return -EINVAL;
1140 }
1141
1142 imm = (__u32)insn[0].imm | ((__u64)insn[1].imm << 32);
1143 if (res->validate && imm != orig_val) {
1144 pr_warn("prog '%s': relo #%d: unexpected insn #%d (LDIMM64) value: got %llu, exp %llu -> %llu\n",
1145 prog_name, relo_idx,
1146 insn_idx, (unsigned long long)imm,
1147 (unsigned long long)orig_val, (unsigned long long)new_val);
1148 return -EINVAL;
1149 }
1150
1151 insn[0].imm = new_val;
1152 insn[1].imm = new_val >> 32;
1153 pr_debug("prog '%s': relo #%d: patched insn #%d (LDIMM64) imm64 %llu -> %llu\n",
1154 prog_name, relo_idx, insn_idx,
1155 (unsigned long long)imm, (unsigned long long)new_val);
1156 break;
1157 }
1158 default:
1159 pr_warn("prog '%s': relo #%d: trying to relocate unrecognized insn #%d, code:0x%x, src:0x%x, dst:0x%x, off:0x%x, imm:0x%x\n",
1160 prog_name, relo_idx, insn_idx, insn->code,
1161 insn->src_reg, insn->dst_reg, insn->off, insn->imm);
1162 return -EINVAL;
1163 }
1164
1165 return 0;
1166 }
1167
1168 /* Output spec definition in the format:
1169 * [<type-id>] (<type-name>) + <raw-spec> => <offset>@<spec>,
1170 * where <spec> is a C-syntax view of recorded field access, e.g.: x.a[3].b
1171 */
bpf_core_format_spec(char * buf,size_t buf_sz,const struct bpf_core_spec * spec)1172 int bpf_core_format_spec(char *buf, size_t buf_sz, const struct bpf_core_spec *spec)
1173 {
1174 const struct btf_type *t;
1175 const char *s;
1176 __u32 type_id;
1177 int i, len = 0;
1178
1179 #define append_buf(fmt, args...) \
1180 ({ \
1181 int r; \
1182 r = snprintf(buf, buf_sz, fmt, ##args); \
1183 len += r; \
1184 if (r >= buf_sz) \
1185 r = buf_sz; \
1186 buf += r; \
1187 buf_sz -= r; \
1188 })
1189
1190 type_id = spec->root_type_id;
1191 t = btf_type_by_id(spec->btf, type_id);
1192 s = btf__name_by_offset(spec->btf, t->name_off);
1193
1194 append_buf("<%s> [%u] %s %s",
1195 core_relo_kind_str(spec->relo_kind),
1196 type_id, btf_kind_str(t), str_is_empty(s) ? "<anon>" : s);
1197
1198 if (core_relo_is_type_based(spec->relo_kind))
1199 return len;
1200
1201 if (core_relo_is_enumval_based(spec->relo_kind)) {
1202 t = skip_mods_and_typedefs(spec->btf, type_id, NULL);
1203 if (btf_is_enum(t)) {
1204 const struct btf_enum *e;
1205 const char *fmt_str;
1206
1207 e = btf_enum(t) + spec->raw_spec[0];
1208 s = btf__name_by_offset(spec->btf, e->name_off);
1209 fmt_str = BTF_INFO_KFLAG(t->info) ? "::%s = %d" : "::%s = %u";
1210 append_buf(fmt_str, s, e->val);
1211 } else {
1212 const struct btf_enum64 *e;
1213 const char *fmt_str;
1214
1215 e = btf_enum64(t) + spec->raw_spec[0];
1216 s = btf__name_by_offset(spec->btf, e->name_off);
1217 fmt_str = BTF_INFO_KFLAG(t->info) ? "::%s = %lld" : "::%s = %llu";
1218 append_buf(fmt_str, s, (unsigned long long)btf_enum64_value(e));
1219 }
1220 return len;
1221 }
1222
1223 if (core_relo_is_field_based(spec->relo_kind)) {
1224 for (i = 0; i < spec->len; i++) {
1225 if (spec->spec[i].name)
1226 append_buf(".%s", spec->spec[i].name);
1227 else if (i > 0 || spec->spec[i].idx > 0)
1228 append_buf("[%u]", spec->spec[i].idx);
1229 }
1230
1231 append_buf(" (");
1232 for (i = 0; i < spec->raw_len; i++)
1233 append_buf("%s%d", i == 0 ? "" : ":", spec->raw_spec[i]);
1234
1235 if (spec->bit_offset % 8)
1236 append_buf(" @ offset %u.%u)", spec->bit_offset / 8, spec->bit_offset % 8);
1237 else
1238 append_buf(" @ offset %u)", spec->bit_offset / 8);
1239 return len;
1240 }
1241
1242 return len;
1243 #undef append_buf
1244 }
1245
1246 /*
1247 * Calculate CO-RE relocation target result.
1248 *
1249 * The outline and important points of the algorithm:
1250 * 1. For given local type, find corresponding candidate target types.
1251 * Candidate type is a type with the same "essential" name, ignoring
1252 * everything after last triple underscore (___). E.g., `sample`,
1253 * `sample___flavor_one`, `sample___flavor_another_one`, are all candidates
1254 * for each other. Names with triple underscore are referred to as
1255 * "flavors" and are useful, among other things, to allow to
1256 * specify/support incompatible variations of the same kernel struct, which
1257 * might differ between different kernel versions and/or build
1258 * configurations.
1259 *
1260 * N.B. Struct "flavors" could be generated by bpftool's BTF-to-C
1261 * converter, when deduplicated BTF of a kernel still contains more than
1262 * one different types with the same name. In that case, ___2, ___3, etc
1263 * are appended starting from second name conflict. But start flavors are
1264 * also useful to be defined "locally", in BPF program, to extract same
1265 * data from incompatible changes between different kernel
1266 * versions/configurations. For instance, to handle field renames between
1267 * kernel versions, one can use two flavors of the struct name with the
1268 * same common name and use conditional relocations to extract that field,
1269 * depending on target kernel version.
1270 * 2. For each candidate type, try to match local specification to this
1271 * candidate target type. Matching involves finding corresponding
1272 * high-level spec accessors, meaning that all named fields should match,
1273 * as well as all array accesses should be within the actual bounds. Also,
1274 * types should be compatible (see bpf_core_fields_are_compat for details).
1275 * 3. It is supported and expected that there might be multiple flavors
1276 * matching the spec. As long as all the specs resolve to the same set of
1277 * offsets across all candidates, there is no error. If there is any
1278 * ambiguity, CO-RE relocation will fail. This is necessary to accommodate
1279 * imperfection of BTF deduplication, which can cause slight duplication of
1280 * the same BTF type, if some directly or indirectly referenced (by
1281 * pointer) type gets resolved to different actual types in different
1282 * object files. If such a situation occurs, deduplicated BTF will end up
1283 * with two (or more) structurally identical types, which differ only in
1284 * types they refer to through pointer. This should be OK in most cases and
1285 * is not an error.
1286 * 4. Candidate types search is performed by linearly scanning through all
1287 * types in target BTF. It is anticipated that this is overall more
1288 * efficient memory-wise and not significantly worse (if not better)
1289 * CPU-wise compared to prebuilding a map from all local type names to
1290 * a list of candidate type names. It's also sped up by caching resolved
1291 * list of matching candidates per each local "root" type ID, that has at
1292 * least one bpf_core_relo associated with it. This list is shared
1293 * between multiple relocations for the same type ID and is updated as some
1294 * of the candidates are pruned due to structural incompatibility.
1295 */
bpf_core_calc_relo_insn(const char * prog_name,const struct bpf_core_relo * relo,int relo_idx,const struct btf * local_btf,struct bpf_core_cand_list * cands,struct bpf_core_spec * specs_scratch,struct bpf_core_relo_res * targ_res)1296 int bpf_core_calc_relo_insn(const char *prog_name,
1297 const struct bpf_core_relo *relo,
1298 int relo_idx,
1299 const struct btf *local_btf,
1300 struct bpf_core_cand_list *cands,
1301 struct bpf_core_spec *specs_scratch,
1302 struct bpf_core_relo_res *targ_res)
1303 {
1304 struct bpf_core_spec *local_spec = &specs_scratch[0];
1305 struct bpf_core_spec *cand_spec = &specs_scratch[1];
1306 struct bpf_core_spec *targ_spec = &specs_scratch[2];
1307 struct bpf_core_relo_res cand_res;
1308 const struct btf_type *local_type;
1309 const char *local_name;
1310 __u32 local_id;
1311 char spec_buf[256];
1312 int i, j, err;
1313
1314 local_id = relo->type_id;
1315 local_type = btf_type_by_id(local_btf, local_id);
1316 local_name = btf__name_by_offset(local_btf, local_type->name_off);
1317 if (!local_name)
1318 return -EINVAL;
1319
1320 err = bpf_core_parse_spec(prog_name, local_btf, relo, local_spec);
1321 if (err) {
1322 const char *spec_str;
1323
1324 spec_str = btf__name_by_offset(local_btf, relo->access_str_off);
1325 pr_warn("prog '%s': relo #%d: parsing [%d] %s %s + %s failed: %d\n",
1326 prog_name, relo_idx, local_id, btf_kind_str(local_type),
1327 str_is_empty(local_name) ? "<anon>" : local_name,
1328 spec_str ?: "<?>", err);
1329 return -EINVAL;
1330 }
1331
1332 bpf_core_format_spec(spec_buf, sizeof(spec_buf), local_spec);
1333 pr_debug("prog '%s': relo #%d: %s\n", prog_name, relo_idx, spec_buf);
1334
1335 /* TYPE_ID_LOCAL relo is special and doesn't need candidate search */
1336 if (relo->kind == BPF_CORE_TYPE_ID_LOCAL) {
1337 /* bpf_insn's imm value could get out of sync during linking */
1338 memset(targ_res, 0, sizeof(*targ_res));
1339 targ_res->validate = false;
1340 targ_res->poison = false;
1341 targ_res->orig_val = local_spec->root_type_id;
1342 targ_res->new_val = local_spec->root_type_id;
1343 return 0;
1344 }
1345
1346 /* libbpf doesn't support candidate search for anonymous types */
1347 if (str_is_empty(local_name)) {
1348 pr_warn("prog '%s': relo #%d: <%s> (%d) relocation doesn't support anonymous types\n",
1349 prog_name, relo_idx, core_relo_kind_str(relo->kind), relo->kind);
1350 return -EOPNOTSUPP;
1351 }
1352
1353 for (i = 0, j = 0; i < cands->len; i++) {
1354 err = bpf_core_spec_match(local_spec, cands->cands[i].btf,
1355 cands->cands[i].id, cand_spec);
1356 if (err < 0) {
1357 bpf_core_format_spec(spec_buf, sizeof(spec_buf), cand_spec);
1358 pr_warn("prog '%s': relo #%d: error matching candidate #%d %s: %d\n",
1359 prog_name, relo_idx, i, spec_buf, err);
1360 return err;
1361 }
1362
1363 bpf_core_format_spec(spec_buf, sizeof(spec_buf), cand_spec);
1364 pr_debug("prog '%s': relo #%d: %s candidate #%d %s\n", prog_name,
1365 relo_idx, err == 0 ? "non-matching" : "matching", i, spec_buf);
1366
1367 if (err == 0)
1368 continue;
1369
1370 err = bpf_core_calc_relo(prog_name, relo, relo_idx, local_spec, cand_spec, &cand_res);
1371 if (err)
1372 return err;
1373
1374 if (j == 0) {
1375 *targ_res = cand_res;
1376 *targ_spec = *cand_spec;
1377 } else if (cand_spec->bit_offset != targ_spec->bit_offset) {
1378 /* if there are many field relo candidates, they
1379 * should all resolve to the same bit offset
1380 */
1381 pr_warn("prog '%s': relo #%d: field offset ambiguity: %u != %u\n",
1382 prog_name, relo_idx, cand_spec->bit_offset,
1383 targ_spec->bit_offset);
1384 return -EINVAL;
1385 } else if (cand_res.poison != targ_res->poison ||
1386 cand_res.new_val != targ_res->new_val) {
1387 /* all candidates should result in the same relocation
1388 * decision and value, otherwise it's dangerous to
1389 * proceed due to ambiguity
1390 */
1391 pr_warn("prog '%s': relo #%d: relocation decision ambiguity: %s %llu != %s %llu\n",
1392 prog_name, relo_idx,
1393 cand_res.poison ? "failure" : "success",
1394 (unsigned long long)cand_res.new_val,
1395 targ_res->poison ? "failure" : "success",
1396 (unsigned long long)targ_res->new_val);
1397 return -EINVAL;
1398 }
1399
1400 cands->cands[j++] = cands->cands[i];
1401 }
1402
1403 /*
1404 * For BPF_CORE_FIELD_EXISTS relo or when used BPF program has field
1405 * existence checks or kernel version/config checks, it's expected
1406 * that we might not find any candidates. In this case, if field
1407 * wasn't found in any candidate, the list of candidates shouldn't
1408 * change at all, we'll just handle relocating appropriately,
1409 * depending on relo's kind.
1410 */
1411 if (j > 0)
1412 cands->len = j;
1413
1414 /*
1415 * If no candidates were found, it might be both a programmer error,
1416 * as well as expected case, depending whether instruction w/
1417 * relocation is guarded in some way that makes it unreachable (dead
1418 * code) if relocation can't be resolved. This is handled in
1419 * bpf_core_patch_insn() uniformly by replacing that instruction with
1420 * BPF helper call insn (using invalid helper ID). If that instruction
1421 * is indeed unreachable, then it will be ignored and eliminated by
1422 * verifier. If it was an error, then verifier will complain and point
1423 * to a specific instruction number in its log.
1424 */
1425 if (j == 0) {
1426 pr_debug("prog '%s': relo #%d: no matching targets found\n",
1427 prog_name, relo_idx);
1428
1429 /* calculate single target relo result explicitly */
1430 err = bpf_core_calc_relo(prog_name, relo, relo_idx, local_spec, NULL, targ_res);
1431 if (err)
1432 return err;
1433 }
1434
1435 return 0;
1436 }
1437
bpf_core_names_match(const struct btf * local_btf,size_t local_name_off,const struct btf * targ_btf,size_t targ_name_off)1438 static bool bpf_core_names_match(const struct btf *local_btf, size_t local_name_off,
1439 const struct btf *targ_btf, size_t targ_name_off)
1440 {
1441 const char *local_n, *targ_n;
1442 size_t local_len, targ_len;
1443
1444 local_n = btf__name_by_offset(local_btf, local_name_off);
1445 targ_n = btf__name_by_offset(targ_btf, targ_name_off);
1446
1447 if (str_is_empty(targ_n))
1448 return str_is_empty(local_n);
1449
1450 targ_len = bpf_core_essential_name_len(targ_n);
1451 local_len = bpf_core_essential_name_len(local_n);
1452
1453 return targ_len == local_len && strncmp(local_n, targ_n, local_len) == 0;
1454 }
1455
bpf_core_enums_match(const struct btf * local_btf,const struct btf_type * local_t,const struct btf * targ_btf,const struct btf_type * targ_t)1456 static int bpf_core_enums_match(const struct btf *local_btf, const struct btf_type *local_t,
1457 const struct btf *targ_btf, const struct btf_type *targ_t)
1458 {
1459 __u16 local_vlen = btf_vlen(local_t);
1460 __u16 targ_vlen = btf_vlen(targ_t);
1461 int i, j;
1462
1463 if (local_t->size != targ_t->size)
1464 return 0;
1465
1466 if (local_vlen > targ_vlen)
1467 return 0;
1468
1469 /* iterate over the local enum's variants and make sure each has
1470 * a symbolic name correspondent in the target
1471 */
1472 for (i = 0; i < local_vlen; i++) {
1473 bool matched = false;
1474 __u32 local_n_off, targ_n_off;
1475
1476 local_n_off = btf_is_enum(local_t) ? btf_enum(local_t)[i].name_off :
1477 btf_enum64(local_t)[i].name_off;
1478
1479 for (j = 0; j < targ_vlen; j++) {
1480 targ_n_off = btf_is_enum(targ_t) ? btf_enum(targ_t)[j].name_off :
1481 btf_enum64(targ_t)[j].name_off;
1482
1483 if (bpf_core_names_match(local_btf, local_n_off, targ_btf, targ_n_off)) {
1484 matched = true;
1485 break;
1486 }
1487 }
1488
1489 if (!matched)
1490 return 0;
1491 }
1492 return 1;
1493 }
1494
bpf_core_composites_match(const struct btf * local_btf,const struct btf_type * local_t,const struct btf * targ_btf,const struct btf_type * targ_t,bool behind_ptr,int level)1495 static int bpf_core_composites_match(const struct btf *local_btf, const struct btf_type *local_t,
1496 const struct btf *targ_btf, const struct btf_type *targ_t,
1497 bool behind_ptr, int level)
1498 {
1499 const struct btf_member *local_m = btf_members(local_t);
1500 __u16 local_vlen = btf_vlen(local_t);
1501 __u16 targ_vlen = btf_vlen(targ_t);
1502 int i, j, err;
1503
1504 if (local_vlen > targ_vlen)
1505 return 0;
1506
1507 /* check that all local members have a match in the target */
1508 for (i = 0; i < local_vlen; i++, local_m++) {
1509 const struct btf_member *targ_m = btf_members(targ_t);
1510 bool matched = false;
1511
1512 for (j = 0; j < targ_vlen; j++, targ_m++) {
1513 if (!bpf_core_names_match(local_btf, local_m->name_off,
1514 targ_btf, targ_m->name_off))
1515 continue;
1516
1517 err = __bpf_core_types_match(local_btf, local_m->type, targ_btf,
1518 targ_m->type, behind_ptr, level - 1);
1519 if (err < 0)
1520 return err;
1521 if (err > 0) {
1522 matched = true;
1523 break;
1524 }
1525 }
1526
1527 if (!matched)
1528 return 0;
1529 }
1530 return 1;
1531 }
1532
1533 /* Check that two types "match". This function assumes that root types were
1534 * already checked for name match.
1535 *
1536 * The matching relation is defined as follows:
1537 * - modifiers and typedefs are stripped (and, hence, effectively ignored)
1538 * - generally speaking types need to be of same kind (struct vs. struct, union
1539 * vs. union, etc.)
1540 * - exceptions are struct/union behind a pointer which could also match a
1541 * forward declaration of a struct or union, respectively, and enum vs.
1542 * enum64 (see below)
1543 * Then, depending on type:
1544 * - integers:
1545 * - match if size and signedness match
1546 * - arrays & pointers:
1547 * - target types are recursively matched
1548 * - structs & unions:
1549 * - local members need to exist in target with the same name
1550 * - for each member we recursively check match unless it is already behind a
1551 * pointer, in which case we only check matching names and compatible kind
1552 * - enums:
1553 * - local variants have to have a match in target by symbolic name (but not
1554 * numeric value)
1555 * - size has to match (but enum may match enum64 and vice versa)
1556 * - function pointers:
1557 * - number and position of arguments in local type has to match target
1558 * - for each argument and the return value we recursively check match
1559 */
__bpf_core_types_match(const struct btf * local_btf,__u32 local_id,const struct btf * targ_btf,__u32 targ_id,bool behind_ptr,int level)1560 int __bpf_core_types_match(const struct btf *local_btf, __u32 local_id, const struct btf *targ_btf,
1561 __u32 targ_id, bool behind_ptr, int level)
1562 {
1563 const struct btf_type *local_t, *targ_t;
1564 int depth = 32; /* max recursion depth */
1565 __u16 local_k, targ_k;
1566
1567 if (level <= 0)
1568 return -EINVAL;
1569
1570 recur:
1571 depth--;
1572 if (depth < 0)
1573 return -EINVAL;
1574
1575 local_t = skip_mods_and_typedefs(local_btf, local_id, &local_id);
1576 targ_t = skip_mods_and_typedefs(targ_btf, targ_id, &targ_id);
1577 if (!local_t || !targ_t)
1578 return -EINVAL;
1579
1580 /* While the name check happens after typedefs are skipped, root-level
1581 * typedefs would still be name-matched as that's the contract with
1582 * callers.
1583 */
1584 if (!bpf_core_names_match(local_btf, local_t->name_off, targ_btf, targ_t->name_off))
1585 return 0;
1586
1587 local_k = btf_kind(local_t);
1588 targ_k = btf_kind(targ_t);
1589
1590 switch (local_k) {
1591 case BTF_KIND_UNKN:
1592 return local_k == targ_k;
1593 case BTF_KIND_FWD: {
1594 bool local_f = BTF_INFO_KFLAG(local_t->info);
1595
1596 if (behind_ptr) {
1597 if (local_k == targ_k)
1598 return local_f == BTF_INFO_KFLAG(targ_t->info);
1599
1600 /* for forward declarations kflag dictates whether the
1601 * target is a struct (0) or union (1)
1602 */
1603 return (targ_k == BTF_KIND_STRUCT && !local_f) ||
1604 (targ_k == BTF_KIND_UNION && local_f);
1605 } else {
1606 if (local_k != targ_k)
1607 return 0;
1608
1609 /* match if the forward declaration is for the same kind */
1610 return local_f == BTF_INFO_KFLAG(targ_t->info);
1611 }
1612 }
1613 case BTF_KIND_ENUM:
1614 case BTF_KIND_ENUM64:
1615 if (!btf_is_any_enum(targ_t))
1616 return 0;
1617
1618 return bpf_core_enums_match(local_btf, local_t, targ_btf, targ_t);
1619 case BTF_KIND_STRUCT:
1620 case BTF_KIND_UNION:
1621 if (behind_ptr) {
1622 bool targ_f = BTF_INFO_KFLAG(targ_t->info);
1623
1624 if (local_k == targ_k)
1625 return 1;
1626
1627 if (targ_k != BTF_KIND_FWD)
1628 return 0;
1629
1630 return (local_k == BTF_KIND_UNION) == targ_f;
1631 } else {
1632 if (local_k != targ_k)
1633 return 0;
1634
1635 return bpf_core_composites_match(local_btf, local_t, targ_btf, targ_t,
1636 behind_ptr, level);
1637 }
1638 case BTF_KIND_INT: {
1639 __u8 local_sgn;
1640 __u8 targ_sgn;
1641
1642 if (local_k != targ_k)
1643 return 0;
1644
1645 local_sgn = btf_int_encoding(local_t) & BTF_INT_SIGNED;
1646 targ_sgn = btf_int_encoding(targ_t) & BTF_INT_SIGNED;
1647
1648 return local_t->size == targ_t->size && local_sgn == targ_sgn;
1649 }
1650 case BTF_KIND_PTR:
1651 if (local_k != targ_k)
1652 return 0;
1653
1654 behind_ptr = true;
1655
1656 local_id = local_t->type;
1657 targ_id = targ_t->type;
1658 goto recur;
1659 case BTF_KIND_ARRAY: {
1660 const struct btf_array *local_array = btf_array(local_t);
1661 const struct btf_array *targ_array = btf_array(targ_t);
1662
1663 if (local_k != targ_k)
1664 return 0;
1665
1666 if (local_array->nelems != targ_array->nelems)
1667 return 0;
1668
1669 local_id = local_array->type;
1670 targ_id = targ_array->type;
1671 goto recur;
1672 }
1673 case BTF_KIND_FUNC_PROTO: {
1674 struct btf_param *local_p = btf_params(local_t);
1675 struct btf_param *targ_p = btf_params(targ_t);
1676 __u16 local_vlen = btf_vlen(local_t);
1677 __u16 targ_vlen = btf_vlen(targ_t);
1678 int i, err;
1679
1680 if (local_k != targ_k)
1681 return 0;
1682
1683 if (local_vlen != targ_vlen)
1684 return 0;
1685
1686 for (i = 0; i < local_vlen; i++, local_p++, targ_p++) {
1687 err = __bpf_core_types_match(local_btf, local_p->type, targ_btf,
1688 targ_p->type, behind_ptr, level - 1);
1689 if (err <= 0)
1690 return err;
1691 }
1692
1693 /* tail recurse for return type check */
1694 local_id = local_t->type;
1695 targ_id = targ_t->type;
1696 goto recur;
1697 }
1698 default:
1699 pr_warn("unexpected kind %s relocated, local [%d], target [%d]\n",
1700 btf_kind_str(local_t), local_id, targ_id);
1701 return 0;
1702 }
1703 }
1704