Skip to contentSkip to conversation

netfilter: fix the hash random initializing race

PATCH Superseded Changli Gao 3 participants 7 messages Aug 20, 2010

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 netfilter: fix the hash random initializing race

nf_conntrack_alloc() isn't called with nf_conntrack_lock locked, so hash random initializing code maybe executed more than once on different CPUs.

---
 net/netfilter/nf_conntrack_core.c |   17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
net/netfilter/nf_conntrack_core.c +11 -6
index df3eedb..7ae7f71 100644
@@ -65,8 +65,7 @@ EXPORT_SYMBOL_GPL(nf_conntrack_max);
 DEFINE_PER_CPU(struct nf_conn, nf_conntrack_untracked);
 EXPORT_PER_CPU_SYMBOL(nf_conntrack_untracked);
 
-static int nf_conntrack_hash_rnd_initted;
-static unsigned int nf_conntrack_hash_rnd;
+static unsigned int nf_conntrack_hash_rnd __read_mostly;
 
 static u_int32_t __hash_conntrack(const struct nf_conntrack_tuple *tuple,
 				  u16 zone, unsigned int size, unsigned int rnd)
@@ -574,10 +573,16 @@ struct nf_conn *nf_conntrack_alloc(struct net *net, u16 zone,
 {
 	struct nf_conn *ct;
 
-	if (unlikely(!nf_conntrack_hash_rnd_initted)) {
-		get_random_bytes(&nf_conntrack_hash_rnd,
-				sizeof(nf_conntrack_hash_rnd));
-		nf_conntrack_hash_rnd_initted = 1;
+	if (unlikely(!nf_conntrack_hash_rnd)) {
+		unsigned int rand;
+
+		/* Why not initialize nf_conntrack_rnd in a "init()" function ?
+		 * Because there isn't enough entropy when system initializing,
+		 * and we initialize it as late as possible. */
+		do {
+			get_random_bytes(&rand, sizeof(rand));
+		} while (!rand);
+		cmpxchg(&nf_conntrack_hash_rnd, 0, rand);
 	}
 
 	/* We don't want any race condition at early drop stage */
#1 lore raw
Quoted 25 lines · Changli Gao

nf_conntrack_alloc() isn't called with nf_conntrack_lock locked, so hash random initializing code maybe executed more than once on different CPUs.

---
 net/netfilter/nf_conntrack_core.c |   17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
net/netfilter/nf_conntrack_core.c +1 -2
index df3eedb..7ae7f71 100644
@@ -65,8 +65,7 @@ EXPORT_SYMBOL_GPL(nf_conntrack_max);
 DEFINE_PER_CPU(struct nf_conn, nf_conntrack_untracked);
 EXPORT_PER_CPU_SYMBOL(nf_conntrack_untracked);
 
-static int nf_conntrack_hash_rnd_initted;
-static unsigned int nf_conntrack_hash_rnd;
+static unsigned int nf_conntrack_hash_rnd __read_mostly;
 
 static u_int32_t __hash_conntrack(const struct nf_conntrack_tuple *tuple,
 				  u16 zone, unsigned int size, unsigned int rnd)
@@ -574,10 +573,16 @@ struct nf_conn *nf_conntrack_alloc(struct net *net, u16 zone,
 {
 	struct nf_conn *ct;
 

net/netfilter/nf_conntrack_core.c struct nf_conn *nf_conntrack_alloc(struct net *net, u16 zone, · line 581

Quoted 10 lines
net/netfilter/nf_conntrack_core.c +6 -4
-	if (unlikely(!nf_conntrack_hash_rnd_initted)) {
-		get_random_bytes(&nf_conntrack_hash_rnd,
-				sizeof(nf_conntrack_hash_rnd));
-		nf_conntrack_hash_rnd_initted = 1;
+	if (unlikely(!nf_conntrack_hash_rnd)) {
+		unsigned int rand;
+
+		/* Why not initialize nf_conntrack_rnd in a "init()" function ?
+		 * Because there isn't enough entropy when system initializing,
+		 * and we initialize it as late as possible. */

This patch is fine but a fine multi line comment is :

	/* Some
	 * fine
	 * comment
	 */

net/netfilter/nf_conntrack_core.c struct nf_conn *nf_conntrack_alloc(struct net *net, u16 zone, · line 588

Quoted 7 lines
net/netfilter/nf_conntrack_core.c +4 -0
+		do {
+			get_random_bytes(&rand, sizeof(rand));
+		} while (!rand);
+		cmpxchg(&nf_conntrack_hash_rnd, 0, rand);
 	}
 
 	/* We don't want any race condition at early drop stage */

--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#2 lore raw
Quoted 8 lines · Eric Dumazet

net/netfilter/nf_conntrack_core.c struct nf_conn *nf_conntrack_alloc(struct net *net, u16 zone, · line 581

Quoted 6 lines
net/netfilter/nf_conntrack_core.c +6 -0
+	if (unlikely(!nf_conntrack_hash_rnd)) {
+		unsigned int rand;
+
+		/* Why not initialize nf_conntrack_rnd in a "init()" function ?
+		 * Because there isn't enough entropy when system initializing,
+		 * and we initialize it as late as possible. */

This patch is fine but a fine multi line comment is :

Quoted 4 lines
	/* Some
	 * fine
	 * comment
	 */

Actually,

	/*
	 * Some
	 */

But that's in CodingStyle already. Always a good read, even if reread.

#3 lore raw
Quoted 14 lines · Jan Engelhardt
Quoted 8 lines · Eric Dumazet

net/netfilter/nf_conntrack_core.c struct nf_conn *nf_conntrack_alloc(struct net *net, u16 zone, · line 581

Quoted 6 lines
net/netfilter/nf_conntrack_core.c +6 -0
+	if (unlikely(!nf_conntrack_hash_rnd)) {
+		unsigned int rand;
+
+		/* Why not initialize nf_conntrack_rnd in a "init()" function ?
+		 * Because there isn't enough entropy when system initializing,
+		 * and we initialize it as late as possible. */

This patch is fine but a fine multi line comment is :

Quoted 4 lines
	/* Some
	 * fine
	 * comment
	 */
Quoted 7 lines

Actually,

	/*
	 * Some
	 */

But that's in CodingStyle already. Always a good read, even if reread.

Sorry, I documented David S. Miller choice, or else I would have mentioned CodingStyle :)

	/* Some
	 * fine
	 * comment
	 */

And it really is better ;)

--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#4 lore raw
Quoted 9 lines · Eric Dumazet
Quoted 5 lines

Actually,

	/*
	 * Some
	 */

But that's in CodingStyle already. Always a good read, even if reread.

Sorry, I documented David S. Miller choice, or else I would have mentioned CodingStyle :)

Quoted 6 lines
	/* Some
	 * fine
	 * comment
	 */

And it really is better ;)

Looks unsymmetric, which is probably the reason CodingStyle was conceived differently in the first place.

#5 lore raw
Quoted 16 lines · Jan Engelhardt
Quoted 14 lines · Eric Dumazet
Quoted 5 lines

Actually,

	/*
	 * Some
	 */

But that's in CodingStyle already. Always a good read, even if reread.

Sorry, I documented David S. Miller choice, or else I would have mentioned CodingStyle :)

	/* Some
	 * fine
	 * comment
	 */
Quoted 4 lines
Quoted 1 line

And it really is better ;)

Looks unsymmetric, which is probably the reason CodingStyle was conceived differently in the first place.

Who said comments must be symmetric ? :)

http://www.spinics.net/lists/netdev/msg134727.html

--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#6 lore raw
Quoted 7 lines · Eric Dumazet
Quoted 6 lines
Quoted 5 lines

/* Some
* fine
* comment
*/
And it really is better

Looks unsymmetric

Who said comments must be symmetric ?

I can live with sub-level maintainers' free interpretation of areas not covered by CodingStyle, but top-level maintainers directly going against the law of Linux code is outrageous! The system is undeniable :)