BPF List
 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
  0 siblings, 1 reply; 3+ 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] 3+ messages in thread

* Re: [PATCH bpf] bpftool: Fix address comparison in kernel_syms_cmp()
  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
  0 siblings, 1 reply; 3+ messages in thread
From: Quentin Monnet @ 2026-12-04 17:55 UTC (permalink / raw)
  To: Arsalan Yavari, bpf
  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

On 04/12/2026 17:02, Arsalan Yavari wrote:
> 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>

Looks correct, thank you.

Acked-by: Quentin Monnet <qmo@kernel.org>

Did you actually hit the issue? On what arch, if you don't mind me asking?

Thanks,
Quentin

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

* Re: [PATCH bpf] bpftool: Fix address comparison in kernel_syms_cmp()
  2026-12-04 17:55 ` Quentin Monnet
@ 2026-12-04 18:36   ` AmirArsalan Yavari Darani
  0 siblings, 0 replies; 3+ messages in thread
From: AmirArsalan Yavari Darani @ 2026-12-04 18:36 UTC (permalink / raw)
  To: Quentin Monnet
  Cc: bpf, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Eduard Zingerman, Kumar Kartikeya Dwivedi, Martin KaFai Lau,
	Song Liu, Yonghong Song, Jiri Olsa, Emil Tsalapatis,
	Ihor Solodrai

On Fri, 4 Dec 2026 at 21:25, Quentin Monnet <qmo@kernel.org> wrote:
> Looks correct, thank you.
>
> Acked-by: Quentin Monnet <qmo@kernel.org>
>
> Did you actually hit the issue? On what arch, if you don't mind me asking?

No, I didn't hit it on a live system. I was looking through
xlated_dumper.c to see how bpftool resolves kallsyms addresses for
'prog dump xlated', noticed the subtraction in kernel_syms_cmp(), and
verified the issue with a small standalone reproducer.

Thanks,
Arsalan

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

end of thread, other threads:[~2026-12-04 18:37 UTC | newest]

Thread overview: 3+ 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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox