* [PATCH bpf-next v2 0/2] libbpf: Skip host /proc/kallsyms for typeless ksyms in gen_loader
@ 2026-12-02 21:21 Thiébaud Weksteen
2026-12-02 21:21 ` [PATCH bpf-next v2 1/2] libbpf: Skip reading " Thiébaud Weksteen
2026-12-02 21:21 ` [PATCH bpf-next v2 2/2] selftests/bpf: Test missing strong typeless ksym in light skeleton Thiébaud Weksteen
0 siblings, 2 replies; 4+ messages in thread
From: Thiébaud Weksteen @ 2026-12-02 21:21 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi, Shuah Khan,
Siddharth Nayyar
Cc: Thiébaud Weksteen, bpf, linux-kselftest, Martin KaFai Lau,
Song Liu, Yonghong Song, Jiri Olsa, Emil Tsalapatis,
Ihor Solodrai
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
--
2.56.1.987.g2ec0ab4260-goog
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH bpf-next v2 1/2] libbpf: Skip reading /proc/kallsyms for typeless ksyms in gen_loader
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
2026-12-02 21:56 ` 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
1 sibling, 1 reply; 4+ messages in thread
From: Thiébaud Weksteen @ 2026-12-02 21:21 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi, Shuah Khan,
Siddharth Nayyar
Cc: Thiébaud Weksteen, Alexei Starovoitov, bpf, linux-kselftest,
Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
Emil Tsalapatis, Ihor Solodrai
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
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH bpf-next v2 2/2] selftests/bpf: Test missing strong typeless ksym in light skeleton
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 ` [PATCH bpf-next v2 1/2] libbpf: Skip reading " Thiébaud Weksteen
@ 2026-12-02 21:21 ` Thiébaud Weksteen
1 sibling, 0 replies; 4+ messages in thread
From: Thiébaud Weksteen @ 2026-12-02 21:21 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi, Shuah Khan,
Siddharth Nayyar
Cc: Thiébaud Weksteen, bpf, linux-kselftest, Martin KaFai Lau,
Song Liu, Yonghong Song, Jiri Olsa, Emil Tsalapatis,
Ihor Solodrai
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.
Signed-off-by: Thiébaud Weksteen <tweek@google.com>
---
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
diff --git a/tools/testing/selftests/bpf/Makefile.skel b/tools/testing/selftests/bpf/Makefile.skel
index 334342928d01..1c639b089c99 100644
--- a/tools/testing/selftests/bpf/Makefile.skel
+++ b/tools/testing/selftests/bpf/Makefile.skel
@@ -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
diff --git a/tools/testing/selftests/bpf/prog_tests/ksyms_btf.c b/tools/testing/selftests/bpf/prog_tests/ksyms_btf.c
index 1d7a2f1e0731..1bc3089f10fc 100644
--- a/tools/testing/selftests/bpf/prog_tests/ksyms_btf.c
+++ b/tools/testing/selftests/bpf/prog_tests/ksyms_btf.c
@@ -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);
diff --git a/tools/testing/selftests/bpf/progs/test_ksyms_strong_typeless.c b/tools/testing/selftests/bpf/progs/test_ksyms_strong_typeless.c
new file mode 100644
index 000000000000..e7f20cd34f0d
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/test_ksyms_strong_typeless.c
@@ -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";
--
2.56.1.987.g2ec0ab4260-goog
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH bpf-next v2 1/2] libbpf: Skip reading /proc/kallsyms for typeless ksyms in gen_loader
2026-12-02 21:21 ` [PATCH bpf-next v2 1/2] libbpf: Skip reading " Thiébaud Weksteen
@ 2026-12-02 21:56 ` bot+bpf-ci
0 siblings, 0 replies; 4+ messages in thread
From: bot+bpf-ci @ 2026-12-02 21:56 UTC (permalink / raw)
To: tweek, ast, daniel, andrii, eddyz87, memxor, shuah, sidnayyar
Cc: tweek, alexei.starovoitov, bpf, linux-kselftest, martin.lau, song,
yonghong.song, jolsa, emil, ihor.solodrai, ast, andrii, daniel,
martin.lau, eddyz87, yonghong.song, mason, ihor.solodrai
[-- Attachment #1: Type: text/plain, Size: 2343 bytes --]
> 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.
> diff --git a/tools/lib/bpf/gen_loader.c b/tools/lib/bpf/gen_loader.c
> --- 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);
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:
> 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
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-12-02 21:56 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [PATCH bpf-next v2 1/2] libbpf: Skip reading " Thiébaud Weksteen
2026-12-02 21:56 ` 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
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox