1 From foo@baz Mon Apr 15 07:47:06 CEST 2019
2 From: Eric Dumazet <edumazet@google.com>
3 Date: Wed, 27 Mar 2019 08:21:30 -0700
4 Subject: netns: provide pure entropy for net_hash_mix()
6 From: Eric Dumazet <edumazet@google.com>
8 [ Upstream commit 355b98553789b646ed97ad801a619ff898471b92 ]
10 net_hash_mix() currently uses kernel address of a struct net,
11 and is used in many places that could be used to reveal this
12 address to a patient attacker, thus defeating KASLR, for
13 the typical case (initial net namespace, &init_net is
14 not dynamically allocated)
16 I believe the original implementation tried to avoid spending
17 too many cycles in this function, but security comes first.
19 Also provide entropy regardless of CONFIG_NET_NS.
21 Fixes: 0b4419162aa6 ("netns: introduce the net_hash_mix "salt" for hashes")
22 Signed-off-by: Eric Dumazet <edumazet@google.com>
23 Reported-by: Amit Klein <aksecurity@gmail.com>
24 Reported-by: Benny Pinkas <benny@pinkas.net>
25 Cc: Pavel Emelyanov <xemul@openvz.org>
26 Signed-off-by: David S. Miller <davem@davemloft.net>
27 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
29 include/net/net_namespace.h | 2 ++
30 include/net/netns/hash.h | 15 ++-------------
31 net/core/net_namespace.c | 1 +
32 3 files changed, 5 insertions(+), 13 deletions(-)
34 --- a/include/net/net_namespace.h
35 +++ b/include/net/net_namespace.h
36 @@ -54,6 +54,8 @@ struct net {
38 spinlock_t rules_mod_lock;
42 struct list_head list; /* list of network namespaces */
43 struct list_head cleanup_list; /* namespaces on death row */
44 struct list_head exit_list; /* Use only net_mutex */
45 --- a/include/net/netns/hash.h
46 +++ b/include/net/netns/hash.h
48 #ifndef __NET_NS_HASH_H__
49 #define __NET_NS_HASH_H__
51 -#include <asm/cache.h>
54 +#include <net/net_namespace.h>
56 static inline unsigned int net_hash_mix(struct net *net)
60 - * shift this right to eliminate bits, that are
64 - return (unsigned)(((unsigned long)net) >> L1_CACHE_SHIFT);
68 + return net->hash_mix;
71 --- a/net/core/net_namespace.c
72 +++ b/net/core/net_namespace.c
73 @@ -156,6 +156,7 @@ static __net_init int setup_net(struct n
75 atomic_set(&net->count, 1);
76 atomic_set(&net->passive, 1);
77 + get_random_bytes(&net->hash_mix, sizeof(u32));
78 net->dev_base_seq = 1;
79 net->user_ns = user_ns;