BPF List
 help / color / mirror / Atom feed
From: "psykose" <alice@ayaya.dev>
To: <bpf@vger.kernel.org>
Subject: [PATCH] libbpf: fix signed multiplication overflow in hash_combine
Date: Sun, 16 Jun 2024 22:11:05 +0000	[thread overview]
Message-ID: <D21SEVE6F615.2LMUOCTGW8AI7@ayaya.dev> (raw)

when using -fsanitize=undefined (which flags signed overflow which is
UB), a crash can be reproduced when building the linux kernel with BTF
info.

cast to unsigned first to make the overflow not invoke UB semantics- the
result is the same.

Signed-off-by: psykose <alice@ayaya.dev>
---
 src/btf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/btf.c b/src/btf.c
index 2d0840e..60cd412 100644
--- a/src/btf.c
+++ b/src/btf.c
@@ -3317,7 +3317,7 @@ struct btf_dedup {
 
 static long hash_combine(long h, long value)
 {
-	return h * 31 + value;
+	return (long)((unsigned long)h * 31 + (unsigned long)value);
 }
 
 #define for_each_dedup_cand(d, node, hash) \

base-commit: 42065ea6627ff6e1ab4c65e51042a70fbf30ff7c
-- 
2.45.2

             reply	other threads:[~2024-06-16 22:16 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-16 22:11 psykose [this message]
2024-06-17  2:14 ` [PATCH] libbpf: fix signed multiplication overflow in hash_combine Yonghong Song

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=D21SEVE6F615.2LMUOCTGW8AI7@ayaya.dev \
    --to=alice@ayaya.dev \
    --cc=bpf@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox