From: "Thiébaud Weksteen" <tweek@google.com>
To: Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>,
Eduard Zingerman <eddyz87@gmail.com>,
Kumar Kartikeya Dwivedi <memxor@gmail.com>,
Shuah Khan <shuah@kernel.org>,
Siddharth Nayyar <sidnayyar@google.com>
Cc: "Thiébaud Weksteen" <tweek@google.com>,
"Alexei Starovoitov" <alexei.starovoitov@gmail.com>,
bpf@vger.kernel.org, linux-kselftest@vger.kernel.org,
"Martin KaFai Lau" <martin.lau@linux.dev>,
"Song Liu" <song@kernel.org>,
"Yonghong Song" <yonghong.song@linux.dev>,
"Jiri Olsa" <jolsa@kernel.org>,
"Emil Tsalapatis" <emil@etsalapatis.com>,
"Ihor Solodrai" <ihor.solodrai@linux.dev>
Subject: [PATCH bpf-next v2 1/2] libbpf: Skip reading /proc/kallsyms for typeless ksyms in gen_loader
Date: Thu, 3 Dec 2026 08:21:50 +1100 [thread overview]
Message-ID: <20261202212151.1288936-2-tweek@google.com> (raw)
In-Reply-To: <20261202212151.1288936-1-tweek@google.com>
When obj->gen_loader is set, gen_loader resolves typeless ksyms on the
target kernel at load time via bpf_kallsyms_lookup_name() rather than
using addresses resolved on the build host. Typed ksyms and kfuncs
already short-circuit on obj->gen_loader in
bpf_object__resolve_ksyms_btf_id(), avoiding host vmlinux BTF lookups,
but bpf_object__resolve_externs() still sets need_kallsyms = true for
typeless ksyms and calls bpf_object__read_kallsyms_file().
As a result, offline loader generation (e.g. bpftool gen skeleton -L)
opens the build host's /proc/kallsyms, which:
- fails in sandboxed or cross-compilation environments without
/proc/kallsyms access,
- fails with -ESRCH for strong typeless ksyms that exist on the target
kernel but not on the build host, and
- leaks the build host's symbol addresses into the initial ld_imm64
immediates embedded in the generated loader data blob (breaking build
reproducibility), even though emit_relo_ksym_typeless() overwrites
them at load time.
Short-circuit all EXT_KSYM entries in bpf_object__resolve_externs() when
obj->gen_loader is set by setting ext->is_set = true (ext is already
zeroed by bpf_object__collect_externs()), and drop the now redundant
obj->gen_loader check from bpf_object__resolve_ksyms_btf_id(). This
skips both vmlinux BTF and /proc/kallsyms lookups on the build host.
Also fix emit_relo_ksym_typeless() to only ignore -ENOENT from
bpf_kallsyms_lookup_name() when relo->is_weak is set. Previously,
-ENOENT was ignored unconditionally, which silently resolved missing
strong typeless ksyms to 0 on the target kernel (previously masked on
single-host builds by the host /proc/kallsyms check). Enforcing -ENOENT
for non-weak symbols ensures missing strong typeless ksyms fail at load
time on the target kernel now that host /proc/kallsyms is no longer
consulted.
Suggested-by: Alexei Starovoitov <alexei.starovoitov@gmail.com>
Signed-off-by: Thiébaud Weksteen <tweek@google.com>
---
tools/lib/bpf/gen_loader.c | 6 ++++--
tools/lib/bpf/libbpf.c | 11 ++++-------
2 files changed, 8 insertions(+), 9 deletions(-)
diff --git a/tools/lib/bpf/gen_loader.c b/tools/lib/bpf/gen_loader.c
index 749a86c43e7f..3376770e00f4 100644
--- a/tools/lib/bpf/gen_loader.c
+++ b/tools/lib/bpf/gen_loader.c
@@ -817,8 +817,10 @@ static void emit_relo_ksym_typeless(struct bpf_gen *gen,
/* skip typeless ksym_desc in fd closing loop in cleanup_relos */
kdesc->typeless = true;
emit_bpf_kallsyms_lookup_name(gen, relo);
- emit(gen, BPF_JMP_IMM(BPF_JNE, BPF_REG_7, -ENOENT, 1));
- emit(gen, BPF_MOV64_IMM(BPF_REG_7, 0));
+ if (relo->is_weak) {
+ emit(gen, BPF_JMP_IMM(BPF_JNE, BPF_REG_7, -ENOENT, 1));
+ emit(gen, BPF_MOV64_IMM(BPF_REG_7, 0));
+ }
emit_check_err(gen);
/* store lower half of addr into insn[insn_idx].imm */
emit(gen, BPF_STX_MEM(BPF_W, BPF_REG_8, BPF_REG_9, offsetof(struct bpf_insn, imm)));
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index e66e848eb463..69f11d7d9a95 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -9663,12 +9663,6 @@ static int bpf_object__resolve_ksyms_btf_id(struct bpf_object *obj)
if (ext->type != EXT_KSYM || !ext->ksym.type_id)
continue;
- if (obj->gen_loader) {
- ext->is_set = true;
- ext->ksym.kernel_btf_obj_fd = 0;
- ext->ksym.kernel_btf_id = 0;
- continue;
- }
t = btf__type_by_id(obj->btf, ext->btf_id);
if (btf_is_var(t))
err = bpf_object__resolve_ksym_var_btf_id(obj, ext);
@@ -9699,7 +9693,10 @@ static int bpf_object__resolve_externs(struct bpf_object *obj,
ext = &obj->externs[i];
if (ext->type == EXT_KSYM) {
- if (ext->ksym.type_id)
+ if (obj->gen_loader)
+ /* the loader prog resolves it */
+ ext->is_set = true;
+ else if (ext->ksym.type_id)
need_vmlinux_btf = true;
else
need_kallsyms = true;
--
2.56.1.987.g2ec0ab4260-goog
next prev parent reply other threads:[~2026-12-02 21:22 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-02 21:21 [PATCH bpf-next v2 0/2] libbpf: Skip host /proc/kallsyms for typeless ksyms in gen_loader Thiébaud Weksteen
2026-12-02 21:21 ` Thiébaud Weksteen [this message]
2026-12-02 21:56 ` [PATCH bpf-next v2 1/2] libbpf: Skip reading " bot+bpf-ci
2026-12-02 21:21 ` [PATCH bpf-next v2 2/2] selftests/bpf: Test missing strong typeless ksym in light skeleton Thiébaud Weksteen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261202212151.1288936-2-tweek@google.com \
--to=tweek@google.com \
--cc=alexei.starovoitov@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=emil@etsalapatis.com \
--cc=ihor.solodrai@linux.dev \
--cc=jolsa@kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=memxor@gmail.com \
--cc=shuah@kernel.org \
--cc=sidnayyar@google.com \
--cc=song@kernel.org \
--cc=yonghong.song@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox