netfilter: fix the race when initializing nf_ct_expect_hash_rnd
Subsystems (3)
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.
Signed-off-by Changli Gao <[email protected]>
--- net/netfilter/nf_conntrack_expect.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-)
net/netfilter/nf_conntrack_expect.c +7 -5
@@ -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),
Am 05.12.2010 17:26, schrieb Changli Gao:
Quoted 25 lines
Since
nf_ct_expect_dst_hash()may be called withoutnf_conntrack_locklocked,nf_ct_expect_hash_rndshould be initialized in the atomic way.Signed-off-by Changli Gao <[email protected]>--- net/netfilter/nf_conntrack_expect.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-)net/netfilter/nf_conntrack_expect.c +0
-3@@ -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.