Skip to contentSkip to conversation

inet: use siphash in exception handling

PATCH Applied Eric Dumazet 3 participants 5 messages Aug 25, 2021 – Aug 26, 2021

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

cover inet: use siphash in exception handling

From: Eric Dumazet <[email protected]>

A group of security researchers brought to our attention the weakness of hash functions used in rt6_exception_hash() and fnhe_hashfun()

I made two distinct patches to help backports, since IPv6 part was added in 4.15

Eric Dumazet (2):
ipv6: use siphash in rt6_exception_hash()
ipv4: use siphash instead of Jenkins in fnhe_hashfun()

 net/ipv4/route.c | 12 ++++++------
 net/ipv6/route.c | 20 ++++++++++++++------
 2 files changed, 20 insertions(+), 12 deletions(-)
Signature · 1 line
2.33.0.rc2.250.ged5fa647cd-goog
#1 lore raw

PATCH 1/2 ipv6: use siphash in rt6_exception_hash()

From: Eric Dumazet <[email protected]>

A group of security researchers brought to our attention the weakness of hash function used in rt6_exception_hash()

Lets use siphash instead of Jenkins Hash, to considerably reduce security risks.

Following patch deals with IPv4.

Fixes 35732d01fe31 ("ipv6: introduce a hash table to store dst cache")
Cc Wei Wang <[email protected]>
Cc Martin KaFai Lau <[email protected]>
---
 net/ipv6/route.c | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)
net/ipv6/route.c +14 -6
index b6ddf23d38330ded88509b8507998ce82a72799b..c5e8ecb96426bda619fe242351e40dcf6ff68bcf 100644
@@ -41,6 +41,7 @@
 #include <linux/nsproxy.h>
 #include <linux/slab.h>
 #include <linux/jhash.h>
+#include <linux/siphash.h>
 #include <net/net_namespace.h>
 #include <net/snmp.h>
 #include <net/ipv6.h>
@@ -1484,17 +1485,24 @@ static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket)
 static u32 rt6_exception_hash(const struct in6_addr *dst,
 			      const struct in6_addr *src)
 {
-	static u32 seed __read_mostly;
-	u32 val;
+	static siphash_key_t rt6_exception_key __read_mostly;
+	struct {
+		struct in6_addr dst;
+		struct in6_addr src;
+	} __aligned(SIPHASH_ALIGNMENT) combined = {
+		.dst = *dst,
+	};
+	u64 val;
 
-	net_get_random_once(&seed, sizeof(seed));
-	val = jhash2((const u32 *)dst, sizeof(*dst)/sizeof(u32), seed);
+	net_get_random_once(&rt6_exception_key, sizeof(rt6_exception_key));
 
 #ifdef CONFIG_IPV6_SUBTREES
 	if (src)
-		val = jhash2((const u32 *)src, sizeof(*src)/sizeof(u32), val);
+		combined.src = *src;
 #endif
-	return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT);
+	val = siphash(&combined, sizeof(combined), &rt6_exception_key);
+
+	return hash_64(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT);
 }
 
 /* Helper function to find the cached rt in the hash table
Signature · 1 line
2.33.0.rc2.250.ged5fa647cd-goog
Acked-by on v1 1/2
#2 lore raw

Re: [PATCH net 1/2] ipv6: use siphash in rt6_exception_hash()

Quoted 9 lines · Eric Dumazet

From: Eric Dumazet <[email protected]>

A group of security researchers brought to our attention the weakness of hash function used in rt6_exception_hash()

Lets use siphash instead of Jenkins Hash, to considerably reduce security risks.

Quoted 7 lines

Following patch deals with IPv4.

Fixes 35732d01fe31 ("ipv6: introduce a hash table to store dst cache")
Cc Wei Wang <[email protected]>
Cc Martin KaFai Lau <[email protected]>

Thanks Eric!

Quoted 53 lines

---

 net/ipv6/route.c | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)
net/ipv6/route.c +14 -7
index b6ddf23d38330ded88509b8507998ce82a72799b..c5e8ecb96426bda619fe242351e40dcf6ff68bcf 100644
@@ -41,6 +41,7 @@
 #include <linux/nsproxy.h>
 #include <linux/slab.h>
 #include <linux/jhash.h>
+#include <linux/siphash.h>
 #include <net/net_namespace.h>
 #include <net/snmp.h>
 #include <net/ipv6.h>
@@ -1484,17 +1485,24 @@ static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket)
 static u32 rt6_exception_hash(const struct in6_addr *dst,
                              const struct in6_addr *src)
 {
-       static u32 seed __read_mostly;
-       u32 val;
+       static siphash_key_t rt6_exception_key __read_mostly;
+       struct {
+               struct in6_addr dst;
+               struct in6_addr src;
+       } __aligned(SIPHASH_ALIGNMENT) combined = {
+               .dst = *dst,
+       };
+       u64 val;
 
-       net_get_random_once(&seed, sizeof(seed));
-       val = jhash2((const u32 *)dst, sizeof(*dst)/sizeof(u32), seed);
+       net_get_random_once(&rt6_exception_key, sizeof(rt6_exception_key));
 
 #ifdef CONFIG_IPV6_SUBTREES
        if (src)
-               val = jhash2((const u32 *)src, sizeof(*src)/sizeof(u32), val);
+               combined.src = *src;
 #endif
-       return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT);
+       val = siphash(&combined, sizeof(combined), &rt6_exception_key);
+
+       return hash_64(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT);
 }
 
 /* Helper function to find the cached rt in the hash table
--

2.33.0.rc2.250.ged5fa647cd-goog

#3 lore raw

PATCH 2/2 ipv4: use siphash instead of Jenkins in fnhe_hashfun()

In reply to Eric Dumazet

From: Eric Dumazet <[email protected]>

A group of security researchers brought to our attention the weakness of hash function used in fnhe_hashfun().

Lets use siphash instead of Jenkins Hash, to considerably reduce security risks.

Also remove the inline keyword, this really is distracting.

Fixes d546c621542d ("ipv4: harden fnhe_hashfun()")
Cc Willy Tarreau <[email protected]>
---
 net/ipv4/route.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
net/ipv4/route.c +6 -6
index 99c06944501ab1a8de0960acfdc9f1825b7079b1..a6f20ee3533554b210d27c4ab6637ca7a05b148b 100644
@@ -600,14 +600,14 @@ static struct fib_nh_exception *fnhe_oldest(struct fnhe_hash_bucket *hash)
 	return oldest;
 }
 
-static inline u32 fnhe_hashfun(__be32 daddr)
+static u32 fnhe_hashfun(__be32 daddr)
 {
-	static u32 fnhe_hashrnd __read_mostly;
-	u32 hval;
+	static siphash_key_t fnhe_hash_key __read_mostly;
+	u64 hval;
 
-	net_get_random_once(&fnhe_hashrnd, sizeof(fnhe_hashrnd));
-	hval = jhash_1word((__force u32)daddr, fnhe_hashrnd);
-	return hash_32(hval, FNHE_HASH_SHIFT);
+	net_get_random_once(&fnhe_hash_key, sizeof(fnhe_hash_key));
+	hval = siphash_1u32((__force u32)daddr, &fnhe_hash_key);
+	return hash_64(hval, FNHE_HASH_SHIFT);
 }
 
 static void fill_route_from_fnhe(struct rtable *rt, struct fib_nh_exception *fnhe)
Signature · 1 line
2.33.0.rc2.250.ged5fa647cd-goog
#4 lore raw
In reply to Eric Dumazet
This series was applied to netdev/net.git (refs/heads/master):

Hello:

This series was applied to netdev/net.git (refs/heads/master):

Quoted 10 lines · On Wed, 25 Aug 2021 16:17:27 -0700 you wrote:

From: Eric Dumazet <[email protected]>

A group of security researchers brought to our attention the weakness of hash functions used in rt6_exception_hash() and fnhe_hashfun()

I made two distinct patches to help backports, since IPv6 part was added in 4.15

[...]

Here is the summary with links:
- [net,1/2] ipv6: use siphash in rt6_exception_hash()

    https://git.kernel.org/netdev/net/c/4785305c05b2
  - [net,2/2] ipv4: use siphash instead of Jenkins in fnhe_hashfun()
    https://git.kernel.org/netdev/net/c/6457378fe796

You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html