Skip to contentSkip to conversation

ipv4: Switch inet_addr_hash() to less predictable hash.

PATCH Applied Kuniyuki Iwashima 3 participants 3 messages Oct 18, 2024 – Oct 23, 2024

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ipv4: Switch inet_addr_hash() to less predictable hash.

Recently, commit 4a0ec2aa0704 ("ipv6: switch inet6_addr_hash() to less predictable hash") and commit 4daf4dc275f1 ("ipv6: switch inet6_acaddr_hash() to less predictable hash") hardened IPv6 address hash functions.

inet_addr_hash() is also highly predictable, and a malicious use could abuse a specific bucket.

Let's follow the change on IPv4 by using jhash_1word().

---
 include/net/ip.h   | 5 +++++
 net/ipv4/devinet.c | 2 +-
 2 files changed, 6 insertions(+), 1 deletion(-)
include/net/ip.h +5 -0
index 4be0a6a603b2..0e548c1f2a0e 100644
@@ -690,6 +690,11 @@ static inline unsigned int ipv4_addr_hash(__be32 ip)
 	return (__force unsigned int) ip;
 }
 
+static inline u32 __ipv4_addr_hash(const __be32 ip, const u32 initval)
+{
+	return jhash_1word((__force u32)ip, initval);
+}
+
 static inline u32 ipv4_portaddr_hash(const struct net *net,
 				     __be32 saddr,
 				     unsigned int port)
net/ipv4/devinet.c +1 -1
index d81fff93d208..3e5e3b5e78c4 100644
@@ -121,7 +121,7 @@ struct inet_fill_args {
 
 static u32 inet_addr_hash(const struct net *net, __be32 addr)
 {
-	u32 val = (__force u32) addr ^ net_hash_mix(net);
+	u32 val = __ipv4_addr_hash(addr, net_hash_mix(net));
 
 	return hash_32(val, IN4_ADDR_HSIZE_SHIFT);
 }
Signature · 1 line
2.39.5 (Apple Git-154)
Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · Kuniyuki Iwashima

Recently, commit 4a0ec2aa0704 ("ipv6: switch inet6_addr_hash() to less predictable hash") and commit 4daf4dc275f1 ("ipv6: switch inet6_acaddr_hash() to less predictable hash") hardened IPv6 address hash functions.

Quoted 6 lines

inet_addr_hash() is also highly predictable, and a malicious use could abuse a specific bucket.

Let's follow the change on IPv4 by using jhash_1word().

#2 lore raw
In reply to Kuniyuki Iwashima
This patch was applied to netdev/net-next.git (main) by Paolo Abeni <[email protected]>:

Hello:

This patch was applied to netdev/net-next.git (main) by Paolo Abeni <[email protected]>:

Quoted 9 lines · On Thu, 17 Oct 2024 18:41:00 -0700 you wrote:

Recently, commit 4a0ec2aa0704 ("ipv6: switch inet6_addr_hash() to less predictable hash") and commit 4daf4dc275f1 ("ipv6: switch inet6_acaddr_hash() to less predictable hash") hardened IPv6 address hash functions.

inet_addr_hash() is also highly predictable, and a malicious use could abuse a specific bucket.

[...]

Here is the summary with links:
- [v1,net-next] ipv4: Switch inet_addr_hash() to less predictable hash.

    https://git.kernel.org/netdev/net-next/c/c972c1c41d9b

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html