Skip to content

Commit 5fc13ad

Browse files
Alexei Starovoitovanakryiko
authored andcommitted
libbpf: Fix relocation of kfunc ksym in ld_imm64 insn.
void *p = kfunc; -> generates ld_imm64 insn. kfunc() -> generates bpf_call insn. libbpf patches bpf_call insn correctly while only btf_id part of ld_imm64 is set in the former case. Which means that pointers to kfuncs in modules are not patched correctly and the verifier rejects load of such programs due to btf_id being out of range. Fix libbpf to patch ld_imm64 for kfunc. Signed-off-by: Alexei Starovoitov <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 58aa2af commit 5fc13ad

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

tools/lib/bpf/libbpf.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7533,6 +7533,12 @@ static int bpf_object__resolve_ksym_func_btf_id(struct bpf_object *obj,
75337533
ext->is_set = true;
75347534
ext->ksym.kernel_btf_id = kfunc_id;
75357535
ext->ksym.btf_fd_idx = mod_btf ? mod_btf->fd_array_idx : 0;
7536+
/* Also set kernel_btf_obj_fd to make sure that bpf_object__relocate_data()
7537+
* populates FD into ld_imm64 insn when it's used to point to kfunc.
7538+
* {kernel_btf_id, btf_fd_idx} -> fixup bpf_call.
7539+
* {kernel_btf_id, kernel_btf_obj_fd} -> fixup ld_imm64.
7540+
*/
7541+
ext->ksym.kernel_btf_obj_fd = mod_btf ? mod_btf->fd : 0;
75367542
pr_debug("extern (func ksym) '%s': resolved to kernel [%d]\n",
75377543
ext->name, kfunc_id);
75387544

0 commit comments

Comments
 (0)