aboutsummaryrefslogtreecommitdiffstats
path: root/net/netfilter
diff options
context:
space:
mode:
authorEric Dumazet <eric.dumazet@gmail.com>2009-07-16 08:03:40 -0400
committerPatrick McHardy <kaber@trash.net>2009-07-16 08:03:40 -0400
commit941297f443f871b8c3372feccf27a8733f6ce9e9 (patch)
treec98b7e30bf14255cfd87a3d8d929f26f35764d21 /net/netfilter
parentaa6a03eb0ae859c1371555ef381de4c96ca1e4e6 (diff)
netfilter: nf_conntrack: nf_conntrack_alloc() fixes
When a slab cache uses SLAB_DESTROY_BY_RCU, we must be careful when allocating objects, since slab allocator could give a freed object still used by lockless readers. In particular, nf_conntrack RCU lookups rely on ct->tuplehash[xxx].hnnode.next being always valid (ie containing a valid 'nulls' value, or a valid pointer to next object in hash chain.) kmem_cache_zalloc() setups object with NULL values, but a NULL value is not valid for ct->tuplehash[xxx].hnnode.next. Fix is to call kmem_cache_alloc() and do the zeroing ourself. As spotted by Patrick, we also need to make sure lookup keys are committed to memory before setting refcount to 1, or a lockless reader could get a reference on the old version of the object. Its key re-check could then pass the barrier. Signed-off-by: Eric Dumazet <eric.dumazet@gmail.com> Signed-off-by: Patrick McHardy <kaber@trash.net>
Diffstat (limited to 'net/netfilter')
-rw-r--r--net/netfilter/nf_conntrack_core.c21
1 files changed, 18 insertions, 3 deletions
diff --git a/net/netfilter/nf_conntrack_core.c b/net/netfilter/nf_conntrack_core.c
index 7508f11c5b39..b5869b9574b0 100644
--- a/net/netfilter/nf_conntrack_core.c
+++ b/net/netfilter/nf_conntrack_core.c
@@ -561,23 +561,38 @@ struct nf_conn *nf_conntrack_alloc(struct net *net,
561 } 561 }
562 } 562 }
563 563
564 ct = kmem_cache_zalloc(nf_conntrack_cachep, gfp); 564 /*
565 * Do not use kmem_cache_zalloc(), as this cache uses
566 * SLAB_DESTROY_BY_RCU.
567 */
568 ct = kmem_cache_alloc(nf_conntrack_cachep, gfp);
565 if (ct == NULL) { 569 if (ct == NULL) {
566 pr_debug("nf_conntrack_alloc: Can't alloc conntrack.\n"); 570 pr_debug("nf_conntrack_alloc: Can't alloc conntrack.\n");
567 atomic_dec(&net->ct.count); 571 atomic_dec(&net->ct.count);
568 return ERR_PTR(-ENOMEM); 572 return ERR_PTR(-ENOMEM);
569 } 573 }
570 574 /*
575 * Let ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode.next
576 * and ct->tuplehash[IP_CT_DIR_REPLY].hnnode.next unchanged.
577 */
578 memset(&ct->tuplehash[IP_CT_DIR_MAX], 0,
579 sizeof(*ct) - offsetof(struct nf_conn, tuplehash[IP_CT_DIR_MAX]));
571 spin_lock_init(&ct->lock); 580 spin_lock_init(&ct->lock);
572 atomic_set(&ct->ct_general.use, 1);
573 ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple = *orig; 581 ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple = *orig;
582 ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode.pprev = NULL;
574 ct->tuplehash[IP_CT_DIR_REPLY].tuple = *repl; 583 ct->tuplehash[IP_CT_DIR_REPLY].tuple = *repl;
584 ct->tuplehash[IP_CT_DIR_REPLY].hnnode.pprev = NULL;
575 /* Don't set timer yet: wait for confirmation */ 585 /* Don't set timer yet: wait for confirmation */
576 setup_timer(&ct->timeout, death_by_timeout, (unsigned long)ct); 586 setup_timer(&ct->timeout, death_by_timeout, (unsigned long)ct);
577#ifdef CONFIG_NET_NS 587#ifdef CONFIG_NET_NS
578 ct->ct_net = net; 588 ct->ct_net = net;
579#endif 589#endif
580 590
591 /*
592 * changes to lookup keys must be done before setting refcnt to 1
593 */
594 smp_wmb();
595 atomic_set(&ct->ct_general.use, 1);
581 return ct; 596 return ct;
582} 597}
583EXPORT_SYMBOL_GPL(nf_conntrack_alloc); 598EXPORT_SYMBOL_GPL(nf_conntrack_alloc);