All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH bpf] bpftool: Fix address comparison in kernel_syms_cmp()
@ 2026-12-04 17:02 Arsalan Yavari
  2026-12-04 17:55 ` Quentin Monnet
  2026-12-05  3:50 ` patchwork-bot+netdevbpf
  0 siblings, 2 replies; 4+ messages in thread
From: Arsalan Yavari @ 2026-12-04 17:02 UTC (permalink / raw)
  To: bpf, Quentin Monnet
  Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Eduard Zingerman, Kumar Kartikeya Dwivedi, Martin KaFai Lau,
	Song Liu, Yonghong Song, Jiri Olsa, Emil Tsalapatis,
	Ihor Solodrai, Arsalan Yavari

kernel_syms_cmp() subtracts two unsigned long symbol addresses and
returns the result as an int. On 64-bit systems this truncates the
difference to 32 bits, and on 32-bit systems unsigned differences
above INT_MAX become negative.

When /proc/kallsyms contains addresses at least 2 GiB apart, such as
low per-CPU symbol offsets alongside kernel text and module addresses,
the comparator can return the wrong sign or 0 for distinct addresses.
This breaks the qsort() ordering of dd->sym_mapping and causes
bsearch() in kernel_syms_search() to miss symbols or match the wrong
entry.

Compare the addresses directly instead of subtracting them, matching
cmp_addr_cookie() in link.c.

Fixes: 7105e828c087 ("bpf: allow for correlation of maps and helpers in dump")
Signed-off-by: Arsalan Yavari <arya48.yavari79@gmail.com>
---
 tools/bpf/bpftool/xlated_dumper.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/tools/bpf/bpftool/xlated_dumper.c b/tools/bpf/bpftool/xlated_dumper.c
index 5579173a6..d527fdfdf 100644
--- a/tools/bpf/bpftool/xlated_dumper.c
+++ b/tools/bpf/bpftool/xlated_dumper.c
@@ -19,8 +19,11 @@
 
 static int kernel_syms_cmp(const void *sym_a, const void *sym_b)
 {
-	return ((struct kernel_sym *)sym_a)->address -
-	       ((struct kernel_sym *)sym_b)->address;
+	const struct kernel_sym *a = sym_a, *b = sym_b;
+
+	if (a->address == b->address)
+		return 0;
+	return a->address < b->address ? -1 : 1;
 }
 
 void kernel_syms_load(struct dump_data *dd)
-- 
2.54.0 (Apple Git-157)


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-12-05  3:50 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 17:02 [PATCH bpf] bpftool: Fix address comparison in kernel_syms_cmp() Arsalan Yavari
2026-12-04 17:55 ` Quentin Monnet
2026-12-04 18:36   ` AmirArsalan Yavari Darani
2026-12-05  3:50 ` patchwork-bot+netdevbpf

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.