Skip to contentSkip to conversation

libbpf: fix signed multiplication overflow in hash_combine

PATCH In review psykose 2 participants 2 messages Jun 16, 2024 – Jun 17, 2024 BPF

lore
Share & tools
lore

psykose

author
#0 lore raw

PATCH 1/1 libbpf: fix signed multiplication overflow in hash_combine

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.

---
 src/btf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/btf.c +1 -1
index 2d0840e..60cd412 100644
@@ -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

Signature · 1 line
2.45.2
reviewer
#1 lore raw
Quoted 8 lines · psykose

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.

This seems against upstream libbpf repo. Could you do a proper patch against bpf-next tree. Please have details how to reproduce the failure.

Please use proper name in your Signed-off-by.

Quoted 19 lines
---
  src/btf.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
src/btf.c +1 -1
index 2d0840e..60cd412 100644
@@ -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