Skip to contentSkip to conversation

netfilter: fix the race when initializing nf_ct_expect_hash_rnd

PATCH Superseded Changli Gao 2 participants 2 messages Dec 5, 2010 – Dec 15, 2010

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 netfilter: fix the race when initializing nf_ct_expect_hash_rnd

Since nf_ct_expect_dst_hash() may be called without nf_conntrack_lock locked, nf_ct_expect_hash_rnd should be initialized in the atomic way.

---
 net/netfilter/nf_conntrack_expect.c |   12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
net/netfilter/nf_conntrack_expect.c +7 -5
index 46e8966..e2bb3ef 100644
@@ -34,7 +34,6 @@ EXPORT_SYMBOL_GPL(nf_ct_expect_hsize);
 
 static unsigned int nf_ct_expect_hash_rnd __read_mostly;
 unsigned int nf_ct_expect_max __read_mostly;
-static int nf_ct_expect_hash_rnd_initted __read_mostly;
 
 static struct kmem_cache *nf_ct_expect_cachep __read_mostly;
 
@@ -77,10 +76,13 @@ static unsigned int nf_ct_expect_dst_hash(const struct nf_conntrack_tuple *tuple
 {
 	unsigned int hash;
 
-	if (unlikely(!nf_ct_expect_hash_rnd_initted)) {
-		get_random_bytes(&nf_ct_expect_hash_rnd,
-				 sizeof(nf_ct_expect_hash_rnd));
-		nf_ct_expect_hash_rnd_initted = 1;
+	if (unlikely(!nf_ct_expect_hash_rnd)) {
+		unsigned int rand;
+
+		do {
+			get_random_bytes(&rand, sizeof(rand));
+		} while (!rand);
+		cmpxchg(&nf_ct_expect_hash_rnd, 0, rand);
 	}
 
 	hash = jhash2(tuple->dst.u3.all, ARRAY_SIZE(tuple->dst.u3.all),
#1 lore raw

Am 05.12.2010 17:26, schrieb Changli Gao:

Quoted 25 lines

Since nf_ct_expect_dst_hash() may be called without nf_conntrack_lock locked, nf_ct_expect_hash_rnd should be initialized in the atomic way.

---
 net/netfilter/nf_conntrack_expect.c |   12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
net/netfilter/nf_conntrack_expect.c +0 -3
index 46e8966..e2bb3ef 100644
@@ -34,7 +34,6 @@ EXPORT_SYMBOL_GPL(nf_ct_expect_hsize);
 
 static unsigned int nf_ct_expect_hash_rnd __read_mostly;
 unsigned int nf_ct_expect_max __read_mostly;
-static int nf_ct_expect_hash_rnd_initted __read_mostly;
 
 static struct kmem_cache *nf_ct_expect_cachep __read_mostly;
 
@@ -77,10 +76,13 @@ static unsigned int nf_ct_expect_dst_hash(const struct nf_conntrack_tuple *tuple
 {
 	unsigned int hash;
 
-	if (unlikely(!nf_ct_expect_hash_rnd_initted)) {
-		get_random_bytes(&nf_ct_expect_hash_rnd,

net/netfilter/nf_conntrack_expect.c static unsigned int nf_ct_expect_dst_hash(const struct nf_conntrack_tuple *tuple · line 86

Quoted 10 lines
net/netfilter/nf_conntrack_expect.c +7 -2
-				 sizeof(nf_ct_expect_hash_rnd));
-		nf_ct_expect_hash_rnd_initted = 1;
+	if (unlikely(!nf_ct_expect_hash_rnd)) {
+		unsigned int rand;
+
+		do {
+			get_random_bytes(&rand, sizeof(rand));
+		} while (!rand);
+		cmpxchg(&nf_ct_expect_hash_rnd, 0, rand);
 	}

I'd rather just re-use the conntrack hash random value.