diff mbox series

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

Message ID 20210825231729.401676-2-eric.dumazet@gmail.com (mailing list archive)
State Accepted
Commit 4785305c05b25a242e5314cc821f54ade4c18810
Delegated to: Netdev Maintainers
Headers show
Series inet: use siphash in exception handling | expand

Checks

Context Check Description
netdev/cover_letter success Link
netdev/fixes_present success Link
netdev/patch_count success Link
netdev/tree_selection success Clearly marked for net
netdev/subject_prefix success Link
netdev/cc_maintainers warning 2 maintainers not CCed: dsahern@kernel.org yoshfuji@linux-ipv6.org
netdev/source_inline success Was 0 now: 0
netdev/verify_signedoff success Link
netdev/module_param success Was 0 now: 0
netdev/build_32bit success Errors and warnings before: 1 this patch: 1
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/verify_fixes success Link
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 37 lines checked
netdev/build_allmodconfig_warn success Errors and warnings before: 1 this patch: 1
netdev/header_inline success Link

Commit Message

Eric Dumazet Aug. 25, 2021, 11:17 p.m. UTC
From: Eric Dumazet <edumazet@google.com>

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")
Signed-off-by: Eric Dumazet <edumazet@google.com>
Reported-by: Keyu Man <kman001@ucr.edu>
Cc: Wei Wang <weiwan@google.com>
Cc: Martin KaFai Lau <kafai@fb.com>
---
 net/ipv6/route.c | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)

Comments

Wei Wang Aug. 26, 2021, 12:11 a.m. UTC | #1
On Wed, Aug 25, 2021 at 4:17 PM Eric Dumazet <eric.dumazet@gmail.com> wrote:
>
> From: Eric Dumazet <edumazet@google.com>
>
> 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")
> Signed-off-by: Eric Dumazet <edumazet@google.com>
> Reported-by: Keyu Man <kman001@ucr.edu>
> Cc: Wei Wang <weiwan@google.com>
> Cc: Martin KaFai Lau <kafai@fb.com>
Acked-by: Wei Wang <weiwan@google.com>

Thanks Eric!

> ---
>
>
>
>  net/ipv6/route.c | 20 ++++++++++++++------
>  1 file changed, 14 insertions(+), 6 deletions(-)
>
> diff --git a/net/ipv6/route.c b/net/ipv6/route.c
> index b6ddf23d38330ded88509b8507998ce82a72799b..c5e8ecb96426bda619fe242351e40dcf6ff68bcf 100644
> --- a/net/ipv6/route.c
> +++ b/net/ipv6/route.c
> @@ -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
>
diff mbox series

Patch

diff --git a/net/ipv6/route.c b/net/ipv6/route.c
index b6ddf23d38330ded88509b8507998ce82a72799b..c5e8ecb96426bda619fe242351e40dcf6ff68bcf 100644
--- a/net/ipv6/route.c
+++ b/net/ipv6/route.c
@@ -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