Skip to contentSkip to conversation

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

PATCH Superseded Thiébaud Weksteen 2 participants 4 messages Dec 2

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

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 renames bpf_object__read_kallsyms_file() to bpf_object__resolve_ksyms_kallsyms(), skips reading /proc/kallsyms when obj->gen_loader is set, 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.

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                        | 20 ++++++++++++++++---
 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, 58 insertions(+), 6 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 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.

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")
---
 tools/lib/bpf/gen_loader.c |  6 ++++--
 tools/lib/bpf/libbpf.c     | 20 +++++++++++++++++---
 2 files changed, 21 insertions(+), 5 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 +17 -3
index e66e848eb463..7db88f2442f6 100644
@@ -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;
 	}
Signature · 1 line
2.56.1.987.g2ec0ab4260-goog
#2 lore raw

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

Quoted 8 lines · Thiébaud Weksteen

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.

Yep. That's a real issue. Thank you for working on it.

Quoted 1 line
Fixes c24941cd3766 ("libbpf: Add typeless ksym support to gen_loader")

The tag invites a backport. Pls drop it.

Quoted 10 lines
(quoted diff) +7 -2
-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);
+

Review

Quoted 10 lines
(quoted diff) +9 -0
+	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;
 }

No need for another loop and the rename. bpf_object__resolve_externs() walks all ksyms already. bpf_object__collect_externs() zeroes ext, so is_set is enough. Set it once for typed and typeless ksyms.

In place of patch 1... pls double check my logic.

tools/lib/bpf/gen_loader.c +4 -2
index 749a86c..3376770 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 e66e848..69f11d7 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;

pw-bot: cr

#3 lore raw

PATCH 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