[PATCH v2] selftests/bpf: Fix kallsyms parsing with long Rust symbols

From: Ricardo B. Marlière (SUSE)

Date: Wed Nov 25 2026 - 06:03:43 EST


load_kallsyms_local_common() reads /proc/kallsyms with a 256-byte line
buffer and stops parsing at the first line sscanf() cannot handle. Since
commit b8a94bfb3395 ("kallsyms: increase maximum kernel symbol length to
512"), symbols may be up to 511 characters, and on CONFIG_RUST=y kernels
some mangled Rust symbols (e.g. drm_panic_qr helpers) overflow the buffer.
fgets() splits such a line, the remainder fails to parse and every symbol
after it is silently dropped.

As a result ksym_get_addr() returns 0 for symbols like bpf_fentry_test1 and
bpf_testmod_*, address lookups resolve to the last loaded symbol, and
bpf_get_ksyms() fails with -EINVAL. This breaks bpf_cookie, fill_link_info,
kprobe_multi_test, kprobe_multi_testmod_test, kprobe_multi_bench_attach,
tracing_multi_bench_attach and stacktrace_ips.

Parse the file with fscanf() instead, bounding the symbol name to
KSYM_NAME_LEN and discarding the rest of the line, as kallsyms_find()
already does.

Suggested-by: Alexei Starovoitov <alexei.starovoitov@xxxxxxxxx>
Signed-off-by: Ricardo B. Marlière (SUSE) <ricardo@xxxxxxxxxxxx>
---
Changes in v2:
- Took Alexei's suggestion to drop buf and use fscanf() directly
- Link to v1: https://patch.msgid.link/20261124-selftests-bpf-rust_sym-v1-1-32c450d0c4ee@xxxxxxxxxxxx
---
tools/testing/selftests/bpf/trace_helpers.c | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/tools/testing/selftests/bpf/trace_helpers.c b/tools/testing/selftests/bpf/trace_helpers.c
index 6d7a4b8c4ab8..5f558280856f 100644
--- a/tools/testing/selftests/bpf/trace_helpers.c
+++ b/tools/testing/selftests/bpf/trace_helpers.c
@@ -64,7 +64,7 @@ void free_kallsyms_local(struct ksyms *ksyms)
static struct ksyms *load_kallsyms_local_common(ksym_cmp_t cmp_cb)
{
FILE *f;
- char func[256], buf[256];
+ char func[512];
char symbol;
void *addr;
int ret;
@@ -80,9 +80,7 @@ static struct ksyms *load_kallsyms_local_common(ksym_cmp_t cmp_cb)
return NULL;
}

- while (fgets(buf, sizeof(buf), f)) {
- if (sscanf(buf, "%p %c %s", &addr, &symbol, func) != 3)
- break;
+ while (fscanf(f, "%p %c %511s%*[^\n]\n", &addr, &symbol, func) == 3) {
if (!addr)
continue;


---
base-commit: 81c6c8c608b6075846d3bd5b6f57958300a56eda
change-id: 20261124-selftests-bpf-rust_sym-e9b50bb2a39a

Best regards,
--
Ricardo B. Marlière (SUSE) <ricardo@xxxxxxxxxxxx>