All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] bpf/btf: fix u32 subtraction overflow in btf_sec_info_cmp
@ 2026-03-18 14:02 ibrahim zein
  2026-03-18 14:11 ` Daniel Borkmann
  0 siblings, 1 reply; 3+ messages in thread
From: ibrahim zein @ 2026-03-18 14:02 UTC (permalink / raw)
  To: bpf; +Cc: ast, daniel, ibrahim zein

The comparison function btf_sec_info_cmp() uses direct subtraction
of u32 values cast to int, which can produce incorrect results due
to integer overflow when values exceed 0x7FFFFFFF.

Replace with explicit comparisons to guarantee correct behavior
across all u32 value ranges.

Reported-by: Ibrahim Zein <zeroxjacks@gmail.com>
Signed-off-by: Ibrahim Zein <zeroxjacks@gmail.com>
---
 btf.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/btf.c b/btf.c
index 4872d2a..5b3e1af 100644
--- a/btf.c
+++ b/btf.c
@@ -5548,7 +5548,11 @@ static int btf_sec_info_cmp(const void *a, const void *b)
 	const struct btf_sec_info *x = a;
 	const struct btf_sec_info *y = b;
 
-	return (int)(x->off - y->off) ? : (int)(x->len - y->len);
+	if (x->off != y->off)
+		return x->off < y->off ? -1 : 1;
+	if (x->len != y->len)
+		return x->len < y->len ? -1 : 1;
+	return 0;
 }
 
 static int btf_check_sec_info(struct btf_verifier_env *env,
-- 
2.51.0


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

* Re: [PATCH] bpf/btf: fix u32 subtraction overflow in btf_sec_info_cmp
  2026-03-18 14:02 [PATCH] bpf/btf: fix u32 subtraction overflow in btf_sec_info_cmp ibrahim zein
@ 2026-03-18 14:11 ` Daniel Borkmann
  0 siblings, 0 replies; 3+ messages in thread
From: Daniel Borkmann @ 2026-03-18 14:11 UTC (permalink / raw)
  To: ibrahim zein, bpf; +Cc: ast

On 3/18/26 3:02 PM, ibrahim zein wrote:
> The comparison function btf_sec_info_cmp() uses direct subtraction
> of u32 values cast to int, which can produce incorrect results due
> to integer overflow when values exceed 0x7FFFFFFF.
> 
> Replace with explicit comparisons to guarantee correct behavior
> across all u32 value ranges.
> 
> Reported-by: Ibrahim Zein <zeroxjacks@gmail.com>
> Signed-off-by: Ibrahim Zein <zeroxjacks@gmail.com>

Ibrahim, you've been asked previously to only send patches with a related
BPF selftest which demonstrate/exercise the problematic behavior that is
being fixed. Recent example of a fix with accompanying BPF selftest:

   https://lore.kernel.org/all/20260304083228.142016-1-tangyazhou@zju.edu.cn/

Thanks,
Daniel

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

* [PATCH] bpf/btf: fix u32 subtraction overflow in btf_sec_info_cmp
@ 2026-03-18 14:15 Ibrahim Zein
  0 siblings, 0 replies; 3+ messages in thread
From: Ibrahim Zein @ 2026-03-18 14:15 UTC (permalink / raw)
  To: bpf; +Cc: ast, daniel, Ibrahim Zein

The comparison function btf_sec_info_cmp() uses direct subtraction
of u32 values cast to int, which can produce incorrect results due
to integer overflow when values exceed 0x7FFFFFFF.

Replace with explicit comparisons to guarantee correct behavior
across all u32 value ranges.

Reported-by: Ibrahim Zein <zeroxjacks@gmail.com>
Signed-off-by: Ibrahim Zein <zeroxjacks@gmail.com>
---
 kernel/bpf/btf.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index 4872d2a..5b3e1af 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -5548,7 +5548,11 @@ static int btf_sec_info_cmp(const void *a, const void *b)
 	const struct btf_sec_info *x = a;
 	const struct btf_sec_info *y = b;
 
-	return (int)(x->off - y->off) ? : (int)(x->len - y->len);
+	if (x->off != y->off)
+		return x->off < y->off ? -1 : 1;
+	if (x->len != y->len)
+		return x->len < y->len ? -1 : 1;
+	return 0;
 }
 
 static int btf_check_sec_info(struct btf_verifier_env *env,
-- 
2.51.0


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

end of thread, other threads:[~2026-03-18 14:15 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-18 14:02 [PATCH] bpf/btf: fix u32 subtraction overflow in btf_sec_info_cmp ibrahim zein
2026-03-18 14:11 ` Daniel Borkmann
  -- strict thread matches above, loose matches on Subject: below --
2026-03-18 14:15 Ibrahim Zein

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.