Skip to contentSkip to conversation

libbpf: Skip host /proc/kallsyms for typeless ksyms in gen_loader

PATCH Applied Thiébaud Weksteen 3 participants 5 messages Dec 2 – Dec 3

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v2 cover libbpf: Skip host /proc/kallsyms for typeless ksyms in gen_loader

When generating a light skeleton / loader program via bpf_object__gen_loader(), libbpf is supposed to defer kernel symbol resolution to the target kernel at load time rather than consulting the build host. Typed ksyms and kfuncs already short-circuit on obj->gen_loader in bpf_object__resolve_ksyms_btf_id(), but bpf_object__resolve_externs() still reads the build host's /proc/kallsyms for typeless ksyms.

This breaks offline loader generation in sandboxed environments, rejects strong typeless ksyms that exist only on the target kernel, and leaks host symbol addresses into the generated loader data blob even though emit_relo_ksym_typeless() overwrites them at load time via bpf_kallsyms_lookup_name().

Patch 1 short-circuits all EXT_KSYM entries in bpf_object__resolve_externs() when obj->gen_loader is set (dropping the now redundant obj->gen_loader check in bpf_object__resolve_ksyms_btf_id()), and makes emit_relo_ksym_typeless() ignore -ENOENT only for weak ksyms so missing strong typeless ksyms still fail at load time on the target kernel.

Patch 2 adds a test_ksyms_strong_typeless light skeleton and ksyms_btf/strong_typeless_lskel selftest verifying that light skeleton generation succeeds for a missing strong typeless ksym and loading it fails with -ENOENT on the target kernel.

Changes in v2:
- Patch 1: Short-circuit all EXT_KSYM entries directly in bpf_object__resolve_externs() (removing the redundant check in bpf_object__resolve_ksyms_btf_id() and avoiding renaming bpf_object__read_kallsyms_file()), per Alexei's suggestion.
- Patch 1: Drop the Fixes: tag.

Thiébaud Weksteen (2):
libbpf: Skip reading /proc/kallsyms for typeless ksyms in gen_loader selftests/bpf: Test missing strong typeless ksym in light skeleton

 tools/lib/bpf/gen_loader.c                    |  6 ++++--
 tools/lib/bpf/libbpf.c                        | 11 ++++-------
 tools/testing/selftests/bpf/Makefile.skel     |  3 ++-
 .../selftests/bpf/prog_tests/ksyms_btf.c      | 19 +++++++++++++++++++
 .../bpf/progs/test_ksyms_strong_typeless.c    | 16 ++++++++++++++++
 5 files changed, 45 insertions(+), 10 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/progs/test_ksyms_strong_typeless.c

base-commit: ac69c149f965b55e85798e516cb66e2bceb63f00

Signature · 1 line
2.56.1.987.g2ec0ab4260-goog
#1 lore raw

PATCH v2 1/2 libbpf: Skip reading /proc/kallsyms for typeless ksyms in gen_loader

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.

---
 tools/lib/bpf/gen_loader.c |  6 ++++--
 tools/lib/bpf/libbpf.c     | 11 ++++-------
 2 files changed, 8 insertions(+), 9 deletions(-)
tools/lib/bpf/gen_loader.c +4 -2
index 749a86c43e7f..3376770e00f4 100644
@@ -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)));
tools/lib/bpf/libbpf.c +4 -7
index e66e848eb463..69f11d7d9a95 100644
@@ -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;
Signature · 1 line
2.56.1.987.g2ec0ab4260-goog
#3 lore raw

PATCH v2 2/2 selftests/bpf: Test missing strong typeless ksym in light skeleton

In reply to Thiébaud Weksteen

Add a test_ksyms_strong_typeless light skeleton and a ksyms_btf/strong_typeless_lskel subtest referencing a nonexistent strong typeless ksym. Light skeleton generation succeeds at build time even though the symbol does not exist on the build host, and loading the generated skeleton fails with -ENOENT on the target kernel.

---
 tools/testing/selftests/bpf/Makefile.skel     |  3 ++-
 .../selftests/bpf/prog_tests/ksyms_btf.c      | 19 +++++++++++++++++++
 .../bpf/progs/test_ksyms_strong_typeless.c    | 16 ++++++++++++++++
 3 files changed, 37 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/bpf/progs/test_ksyms_strong_typeless.c
tools/testing/selftests/bpf/Makefile.skel +2 -1
index 334342928d01..1c639b089c99 100644
@@ -35,7 +35,8 @@ LSKELS := fexit_sleep.c trace_printk.c trace_vprintk.c map_ptr_kern.c 	\
 	core_kern.c core_kern_overflow.c test_ringbuf.c			\
 	test_ringbuf_n.c test_ringbuf_map_key.c test_ringbuf_write.c    \
 	test_ringbuf_overwrite.c callx_rodata.c many_progs.c		\
-	struct_ops_vmlinux.c struct_ops_mod.c
+	struct_ops_vmlinux.c struct_ops_mod.c				\
+	test_ksyms_strong_typeless.c
 
 LSKELS_SIGNED := fentry_test.c fexit_test.c atomics.c
 
tools/testing/selftests/bpf/prog_tests/ksyms_btf.c +19 -0
index 1d7a2f1e0731..1bc3089f10fc 100644
@@ -9,6 +9,7 @@
 #include "test_ksyms_weak.skel.h"
 #include "test_ksyms_weak.lskel.h"
 #include "test_ksyms_btf_write_check.skel.h"
+#include "test_ksyms_strong_typeless.lskel.h"
 
 static int duration;
 
@@ -138,6 +139,21 @@ static void test_weak_syms_lskel(void)
 	test_ksyms_weak_lskel__destroy(skel);
 }
 
+static void test_strong_typeless_lskel(void)
+{
+	struct test_ksyms_strong_typeless_lskel *skel;
+	int err;
+
+	skel = test_ksyms_strong_typeless_lskel__open();
+	if (!ASSERT_OK_PTR(skel, "test_ksyms_strong_typeless_lskel__open"))
+		return;
+
+	err = test_ksyms_strong_typeless_lskel__load(skel);
+	ASSERT_EQ(err, -ENOENT, "strong_missing_ksym_load");
+
+	test_ksyms_strong_typeless_lskel__destroy(skel);
+}
+
 static void test_write_check(bool test_handler1)
 {
 	struct test_ksyms_btf_write_check *skel;
@@ -183,6 +199,9 @@ void test_ksyms_btf(void)
 	if (test__start_subtest("weak_ksyms_lskel"))
 		test_weak_syms_lskel();
 
+	if (test__start_subtest("strong_typeless_lskel"))
+		test_strong_typeless_lskel();
+
 	if (test__start_subtest("write_check1"))
 		test_write_check(true);
 
tools/testing/selftests/bpf/progs/test_ksyms_strong_typeless.c new file +16 -0
new file mode 100644
index 000000000000..e7f20cd34f0d
@@ -0,0 +1,16 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Google */
+
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+
+/* Strong typeless ksym that does not exist in the kernel. */
+extern const void bpf_nonexistent_strong_ksym __ksym;
+
+SEC("raw_tp/sys_enter")
+int handler(const void *ctx)
+{
+	return (int)(__u64)&bpf_nonexistent_strong_ksym;
+}
+
+char _license[] SEC("license") = "GPL";
Signature · 1 line
2.56.1.987.g2ec0ab4260-goog
#2 lore raw

Re: [PATCH bpf-next v2 1/2] libbpf: Skip reading /proc/kallsyms for typeless ksyms in gen_loader

In reply to Thiébaud Weksteen
TL;DR: Possible regression in gen_loader: a strong typeless ksym that exists at address 0 (e.g. __per_cpu_start on pre-6.15 x86-64) may now fail light skeleton load with -ENOENT, unlike regular…
Quoted 1 line

libbpf: Skip reading /proc/kallsyms for typeless ksyms in gen_loader

TL;DR: Possible regression in gen_loader: a strong typeless ksym that exists at address 0 (e.g. __per_cpu_start on pre-6.15 x86-64) may now fail light skeleton load with -ENOENT, unlike regular libbpf.

Quoted 3 lines
tools/lib/bpf/gen_loader.c +0 -0

[ ... ]

tools/lib/bpf/gen_loader.c static void emit_relo_ksym_typeless(struct bpf_gen *gen, · line 824

Quoted 11 lines
tools/lib/bpf/gen_loader.c +4 -2
@@ -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);

Can this reject a strong typeless ksym that exists but has address 0?

bpf_kallsyms_lookup_name() returns -ENOENT in that case too:

    *res = kallsyms_lookup_name(name);
    return *res ? 0 : -ENOENT;

On x86-64 SMP kernels before v6.15 the percpu section is linked at address 0, so kallsyms_lookup_name("__per_cpu_start") returns 0. The selftest progs/test_ksyms.c declares:

    extern const void __per_cpu_start __ksym;

Before this patch, the BPF_JNE/BPF_MOV64_IMM pair mapped -ENOENT to R7 = 0 and the loader stored the correct address 0. With this patch the pair is emitted only for weak relos, so a strong relo reaches emit_check_err() with R7 = -ENOENT and the whole light skeleton load fails.

Regular libbpf still resolves the same extern from /proc/kallsyms to 0 and loads, so the two load paths now disagree for zero-address symbols. Several LTS x86-64 kernels (6.1, 6.6, 6.12) are still zero-based.

The commit message says:

Quoted 2 lines

Enforcing -ENOENT for non-weak symbols ensures missing strong typeless ksyms fail at load time on the target kernel

Is it accurate that -ENOENT always means a missing symbol? Should the commit message at least mention this limitation?

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/46976797079

#4 lore raw
In reply to Thiébaud Weksteen
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Hello:

This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Quoted 9 lines · On Thu, 3 Dec 2026 08:21:49 +1100 you wrote:

When generating a light skeleton / loader program via bpf_object__gen_loader(), libbpf is supposed to defer kernel symbol resolution to the target kernel at load time rather than consulting the build host. Typed ksyms and kfuncs already short-circuit on obj->gen_loader in bpf_object__resolve_ksyms_btf_id(), but bpf_object__resolve_externs() still reads the build host's /proc/kallsyms for typeless ksyms.

[...]

Here is the summary with links:
- [bpf-next,v2,1/2] libbpf: Skip reading /proc/kallsyms for typeless ksyms in gen_loader

    https://git.kernel.org/bpf/bpf-next/c/27e9c99f6fef
  - [bpf-next,v2,2/2] selftests/bpf: Test missing strong typeless ksym in light skeleton
    https://git.kernel.org/bpf/bpf-next/c/e4e3d66b5e5d

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html