BPF List
 help / color / mirror / Atom feed
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>,
	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 1/2] libbpf: Skip reading /proc/kallsyms for typeless ksyms in gen_loader
Date: Wed,  2 Dec 2026 11:32:33 +1100	[thread overview]
Message-ID: <20261202003234.2630157-2-tweek@google.com> (raw)
In-Reply-To: <20261202003234.2630157-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.

Rename bpf_object__read_kallsyms_file() to
bpf_object__resolve_ksyms_kallsyms() and short-circuit on
obj->gen_loader there instead of reading /proc/kallsyms, marking
typeless ksyms as resolved with address 0 (matching
bpf_object__resolve_ksyms_btf_id()).

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.

Fixes: c24941cd3766 ("libbpf: Add typeless ksym support to gen_loader")
Signed-off-by: Thiébaud Weksteen <tweek@google.com>
---
 tools/lib/bpf/gen_loader.c |  6 ++++--
 tools/lib/bpf/libbpf.c     | 20 +++++++++++++++++---
 2 files changed, 21 insertions(+), 5 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..7db88f2442f6 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -9488,9 +9488,23 @@ static int kallsyms_cb(unsigned long long sym_addr, char sym_type,
 	return 0;
 }
 
-static int bpf_object__read_kallsyms_file(struct bpf_object *obj)
+static int bpf_object__resolve_ksyms_kallsyms(struct bpf_object *obj)
 {
-	return libbpf_kallsyms_parse(kallsyms_cb, obj);
+	struct extern_desc *ext;
+	int i;
+
+	if (!obj->gen_loader)
+		return libbpf_kallsyms_parse(kallsyms_cb, obj);
+
+	for (i = 0; i < obj->nr_extern; i++) {
+		ext = &obj->externs[i];
+		if (ext->type != EXT_KSYM || ext->ksym.type_id)
+			continue;
+
+		ext->is_set = true;
+		ext->ksym.addr = 0;
+	}
+	return 0;
 }
 
 static int find_ksym_btf_id(struct bpf_object *obj, const char *ksym_name,
@@ -9766,7 +9780,7 @@ static int bpf_object__resolve_externs(struct bpf_object *obj,
 			return -EINVAL;
 	}
 	if (need_kallsyms) {
-		err = bpf_object__read_kallsyms_file(obj);
+		err = bpf_object__resolve_ksyms_kallsyms(obj);
 		if (err)
 			return -EINVAL;
 	}
-- 
2.56.1.987.g2ec0ab4260-goog


  reply	other threads:[~2026-12-02  0:32 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02  0:32 [PATCH bpf-next 0/2] libbpf: Skip host /proc/kallsyms for typeless ksyms in gen_loader Thiébaud Weksteen
2026-12-02  0:32 ` Thiébaud Weksteen [this message]
2026-12-02  0:32 ` [PATCH bpf-next 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=20261202003234.2630157-2-tweek@google.com \
    --to=tweek@google.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