public inbox for netdev@vger.kernel.org
 help / color / mirror / Atom feed
From: Eric Dumazet <eric.dumazet@gmail.com>
To: Hannes Frederic Sowa <hannes@stressinduktion.org>
Cc: Tom Herbert <therbert@google.com>,
	davem@davemloft.net, netdev@vger.kernel.org,
	jesse.brandeburg@intel.com
Subject: [PATCH] net: net_secret should not depend on TCP
Date: Tue, 24 Sep 2013 06:19:57 -0700	[thread overview]
Message-ID: <1380028797.3165.65.camel@edumazet-glaptop> (raw)
In-Reply-To: <20130924054532.GA24446@order.stressinduktion.org>

From: Eric Dumazet <edumazet@google.com>

A host might need net_secret[] and never open a single socket. 

Problem added in commit aebda156a570782
("net: defer net_secret[] initialization")

Based on prior patch from Hannes Frederic Sowa.

Reported-by: Hannes Frederic Sowa <hannes@stressinduktion.org>
Signed-off-by: Eric Dumazet <edumazet@google.com>
---
 include/net/secure_seq.h |    1 -
 net/core/secure_seq.c    |   27 ++++++++++++++++++++++++---
 net/ipv4/af_inet.c       |    4 +---
 3 files changed, 25 insertions(+), 7 deletions(-)

diff --git a/include/net/secure_seq.h b/include/net/secure_seq.h
index 6ca975b..c2e542b 100644
--- a/include/net/secure_seq.h
+++ b/include/net/secure_seq.h
@@ -3,7 +3,6 @@
 
 #include <linux/types.h>
 
-extern void net_secret_init(void);
 extern __u32 secure_ip_id(__be32 daddr);
 extern __u32 secure_ipv6_id(const __be32 daddr[4]);
 extern u32 secure_ipv4_port_ephemeral(__be32 saddr, __be32 daddr, __be16 dport);
diff --git a/net/core/secure_seq.c b/net/core/secure_seq.c
index 6a2f13c..3f1ec15 100644
--- a/net/core/secure_seq.c
+++ b/net/core/secure_seq.c
@@ -10,11 +10,24 @@
 
 #include <net/secure_seq.h>
 
-static u32 net_secret[MD5_MESSAGE_BYTES / 4] ____cacheline_aligned;
+#define NET_SECRET_SIZE (MD5_MESSAGE_BYTES / 4)
 
-void net_secret_init(void)
+static u32 net_secret[NET_SECRET_SIZE] ____cacheline_aligned;
+
+static void net_secret_init(void)
 {
-	get_random_bytes(net_secret, sizeof(net_secret));
+	u32 tmp;
+	int i;
+
+	if (likely(net_secret[0]))
+		return;
+
+	for (i = NET_SECRET_SIZE; i > 0;) {
+		do {
+			get_random_bytes(&tmp, sizeof(tmp));
+		} while (!tmp);
+		cmpxchg(&net_secret[--i], 0, tmp);
+	}
 }
 
 #ifdef CONFIG_INET
@@ -42,6 +55,7 @@ __u32 secure_tcpv6_sequence_number(const __be32 *saddr, const __be32 *daddr,
 	u32 hash[MD5_DIGEST_WORDS];
 	u32 i;
 
+	net_secret_init();
 	memcpy(hash, saddr, 16);
 	for (i = 0; i < 4; i++)
 		secret[i] = net_secret[i] + (__force u32)daddr[i];
@@ -63,6 +77,7 @@ u32 secure_ipv6_port_ephemeral(const __be32 *saddr, const __be32 *daddr,
 	u32 hash[MD5_DIGEST_WORDS];
 	u32 i;
 
+	net_secret_init();
 	memcpy(hash, saddr, 16);
 	for (i = 0; i < 4; i++)
 		secret[i] = net_secret[i] + (__force u32) daddr[i];
@@ -82,6 +97,7 @@ __u32 secure_ip_id(__be32 daddr)
 {
 	u32 hash[MD5_DIGEST_WORDS];
 
+	net_secret_init();
 	hash[0] = (__force __u32) daddr;
 	hash[1] = net_secret[13];
 	hash[2] = net_secret[14];
@@ -96,6 +112,7 @@ __u32 secure_ipv6_id(const __be32 daddr[4])
 {
 	__u32 hash[4];
 
+	net_secret_init();
 	memcpy(hash, daddr, 16);
 	md5_transform(hash, net_secret);
 
@@ -107,6 +124,7 @@ __u32 secure_tcp_sequence_number(__be32 saddr, __be32 daddr,
 {
 	u32 hash[MD5_DIGEST_WORDS];
 
+	net_secret_init();
 	hash[0] = (__force u32)saddr;
 	hash[1] = (__force u32)daddr;
 	hash[2] = ((__force u16)sport << 16) + (__force u16)dport;
@@ -121,6 +139,7 @@ u32 secure_ipv4_port_ephemeral(__be32 saddr, __be32 daddr, __be16 dport)
 {
 	u32 hash[MD5_DIGEST_WORDS];
 
+	net_secret_init();
 	hash[0] = (__force u32)saddr;
 	hash[1] = (__force u32)daddr;
 	hash[2] = (__force u32)dport ^ net_secret[14];
@@ -140,6 +159,7 @@ u64 secure_dccp_sequence_number(__be32 saddr, __be32 daddr,
 	u32 hash[MD5_DIGEST_WORDS];
 	u64 seq;
 
+	net_secret_init();
 	hash[0] = (__force u32)saddr;
 	hash[1] = (__force u32)daddr;
 	hash[2] = ((__force u16)sport << 16) + (__force u16)dport;
@@ -164,6 +184,7 @@ u64 secure_dccpv6_sequence_number(__be32 *saddr, __be32 *daddr,
 	u64 seq;
 	u32 i;
 
+	net_secret_init();
 	memcpy(hash, saddr, 16);
 	for (i = 0; i < 4; i++)
 		secret[i] = net_secret[i] + daddr[i];
diff --git a/net/ipv4/af_inet.c b/net/ipv4/af_inet.c
index 7a1874b..cfeb85c 100644
--- a/net/ipv4/af_inet.c
+++ b/net/ipv4/af_inet.c
@@ -263,10 +263,8 @@ void build_ehash_secret(void)
 		get_random_bytes(&rnd, sizeof(rnd));
 	} while (rnd == 0);
 
-	if (cmpxchg(&inet_ehash_secret, 0, rnd) == 0) {
+	if (cmpxchg(&inet_ehash_secret, 0, rnd) == 0)
 		get_random_bytes(&ipv6_hash_secret, sizeof(ipv6_hash_secret));
-		net_secret_init();
-	}
 }
 EXPORT_SYMBOL(build_ehash_secret);
 

  reply	other threads:[~2013-09-24 13:27 UTC|newest]

Thread overview: 50+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-09-23 22:41 [PATCH 1/2] net: Toeplitz library functions Tom Herbert
2013-09-24  0:03 ` Eric Dumazet
2013-09-24  1:39   ` David Miller
2013-09-24  2:30   ` Hannes Frederic Sowa
2013-09-24  3:35     ` Hannes Frederic Sowa
2013-09-24  5:38       ` Eric Dumazet
2013-09-24  5:45         ` Hannes Frederic Sowa
2013-09-24 13:19           ` Eric Dumazet [this message]
2013-09-24 15:13             ` [PATCH] net: net_secret should not depend on TCP Hannes Frederic Sowa
2013-09-24 15:22               ` Eric Dumazet
2013-09-24 15:28                 ` Hannes Frederic Sowa
2013-09-24 15:46                   ` Eric Dumazet
2013-09-24 23:51             ` Hannes Frederic Sowa
2013-09-28 22:20               ` David Miller
2013-09-25  9:00             ` [PATCH RFC] random: introduce get_random_bytes_busy_wait_initialized Hannes Frederic Sowa
2013-09-25 12:06               ` Eric Dumazet
2013-09-25 13:35                 ` Hannes Frederic Sowa
2013-10-02 15:10               ` Theodore Ts'o
2013-10-02 17:18                 ` Hannes Frederic Sowa
2013-10-02 19:40                   ` Theodore Ts'o
2013-09-24 16:01         ` [PATCH 1/2] net: Toeplitz library functions Hannes Frederic Sowa
2013-09-24 16:14           ` Eric Dumazet
2013-09-24 16:35             ` Tom Herbert
2013-09-24 16:46               ` Eric Dumazet
2013-09-24 17:02                 ` Ben Hutchings
2013-09-24 17:03                 ` Tom Herbert
2013-09-24 17:34                   ` Eric Dumazet
2013-09-24 17:37                     ` Rick Jones
2013-09-24 17:44                       ` Eric Dumazet
2013-09-24 18:02                     ` Tom Herbert
2013-09-24 18:48                       ` David Miller
2013-09-24 19:42                       ` Hannes Frederic Sowa
2013-09-24  8:32 ` David Laight
2013-09-24 12:24   ` Eric Dumazet
2013-09-24 15:22   ` Tom Herbert
2013-09-24 15:29     ` Eric Dumazet
2013-09-24 15:39     ` David Miller
2013-09-24 15:54       ` Tom Herbert
2013-09-24 16:00         ` Hannes Frederic Sowa
2013-09-24 16:10         ` Eric Dumazet
2013-09-24 18:03         ` David Miller
2013-09-24 18:06           ` Tom Herbert
2013-09-24 18:10           ` Ben Hutchings
2013-09-24 18:24             ` Tom Herbert
2013-09-24 19:14               ` Eric Dumazet
2013-09-24 18:49             ` David Miller
2013-09-24 18:48           ` Jesse Brandeburg
2013-09-24 19:04             ` Tom Herbert
2013-09-24 16:38   ` Ben Hutchings
2013-09-24 16:32 ` Ben Hutchings

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1380028797.3165.65.camel@edumazet-glaptop \
    --to=eric.dumazet@gmail.com \
    --cc=davem@davemloft.net \
    --cc=hannes@stressinduktion.org \
    --cc=jesse.brandeburg@intel.com \
    --cc=netdev@vger.kernel.org \
    --cc=therbert@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox