Light skeletons rely on the kernel to apply CO-RE relocations, which only knows how to find candidates by type name, so the function arguments structs supported by libbpf (__bpf_func_args) find no candidate: all arguments appear to be missing. Pass the program being loaded to bpf_core_apply() and, for relocations against a function arguments struct, use the program's attach target as the (single) candidate, as built by bpf_core_func_args_cand() in the shared relo_core.c. prog->aux->attach_btf and attach_btf_id are already set when bpf_check_core_relo() runs, and the program holds a reference to attach_btf, so no candidate cache or locking is needed. As the matching is shared with libbpf, parameter type changes are handled the same way as when libbpf applies the relocations: members of a different kind than their parameter, and pointers to a type with a different kind or name than the parameter's (other than void *), don't match and so appear as missing arguments; integer size changes are handled as for struct fields. Likewise, on big-endian targets, loads of signed arguments smaller than 8 bytes that aren't sign-extending are poisoned, as the verifier only allows loads of whole context slots. Unlike in libbpf, all subprograms belong to the program being loaded, so relocations in subprograms work too. Signed-off-by: Alan Maguire Assisted-by: LLM --- include/linux/bpf.h | 2 ++ kernel/bpf/btf.c | 38 ++++++++++++++++++++++++++++++++++++++ kernel/bpf/check_btf.c | 1 + 3 files changed, 41 insertions(+) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 971e584c6efc..f814f4e8fd81 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -3445,6 +3445,8 @@ int bpf_get_kfunc_addr(const struct bpf_prog *prog, u32 func_id, struct bpf_core_ctx { struct bpf_verifier_log *log; const struct btf *btf; + /* program being loaded, if any, for function argument relocations */ + const struct bpf_prog *prog; }; bool btf_nested_type_is_trusted(struct bpf_verifier_log *log, diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index 2f28b418a7fc..e37a77dec8b8 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -10697,11 +10697,39 @@ bpf_core_find_cands(struct bpf_core_ctx *ctx, u32 local_type_id) return populate_cand_cache(cands, module_cand_cache, MODULE_CAND_CACHE_SIZE); } +/* Function argument structs (see __bpf_func_args in libbpf's bpf_tracing.h) + * are matched against the parameters of the program's attach target. + */ +static int bpf_core_func_args_cand_prog(struct bpf_core_ctx *ctx, int relo_idx, + struct bpf_core_cand *cand) +{ + const struct bpf_prog *prog = ctx->prog; + int err; + + if ((prog->type != BPF_PROG_TYPE_TRACING && prog->type != BPF_PROG_TYPE_LSM) || + !prog->aux->attach_btf || prog->aux->attach_btf_id == 0 || + prog->aux->dst_prog) { + bpf_log(ctx->log, + "relo #%u: function argument relocations need a kernel attach target\n", + relo_idx); + return -EOPNOTSUPP; + } + + err = bpf_core_func_args_cand(prog->aux->attach_btf, prog->aux->attach_btf_id, + prog->expected_attach_type, cand); + if (err) + bpf_log(ctx->log, + "relo #%u: can't relocate function arguments against attach target [%u]: %d\n", + relo_idx, prog->aux->attach_btf_id, err); + return err; +} + int bpf_core_apply(struct bpf_core_ctx *ctx, const struct bpf_core_relo *relo, int relo_idx, void *insn) { bool need_cands = relo->kind != BPF_CORE_TYPE_ID_LOCAL; struct bpf_core_cand_list cands = {}; + struct bpf_core_cand func_args_cand; struct bpf_core_relo_res targ_res; struct bpf_core_spec *specs; const struct btf_type *type; @@ -10722,6 +10750,16 @@ int bpf_core_apply(struct bpf_core_ctx *ctx, const struct bpf_core_relo *relo, return -EINVAL; } + if (need_cands && ctx->prog && bpf_core_is_func_args_type(ctx->btf, relo->type_id)) { + /* no locking needed, prog holds a reference to attach_btf */ + need_cands = false; + err = bpf_core_func_args_cand_prog(ctx, relo_idx, &func_args_cand); + if (err) + goto out; + cands.cands = &func_args_cand; + cands.len = 1; + } + if (need_cands) { struct bpf_cand_cache *cc; int i; diff --git a/kernel/bpf/check_btf.c b/kernel/bpf/check_btf.c index 79e079e907c9..e302feea9ca9 100644 --- a/kernel/bpf/check_btf.c +++ b/kernel/bpf/check_btf.c @@ -352,6 +352,7 @@ int bpf_check_core_relo(struct bpf_verifier_env *env, struct bpf_core_ctx ctx = { .log = &env->log, .btf = btf, + .prog = prog, }; bpfptr_t u_core_relo; int err; -- 2.56.0