Skip to contentSkip to conversation

netfilter: ipt_CLUSTERIP: two more fixes

PATCH In review Florian Westphal 2 participants 4 messages Feb 16, 2018

Subsystems (2)
lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

cover netfilter: ipt_CLUSTERIP: two more fixes

During code audit I found two more bugs in CLUSTERIP, first one is a refcount leak, second is possible use-after free due to kfree() of rcu-protected (and already visible) structure.

I think its time to remove this target, so, if anyone has a good reason to not nuke it in nf-next please let me know soon.

author maintainer
#1 lore raw

PATCH 1/2 netfilter: ipt_CLUSTERIP: put config struct if we can't increment ct refcount

This needs to put() the entry to avoid a resource leak in error path.

---
 net/ipv4/netfilter/ipt_CLUSTERIP.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
net/ipv4/netfilter/ipt_CLUSTERIP.c +6 -2
index 4b02ab39ebc5..4c8cfd352687 100644
@@ -496,12 +496,15 @@ static int clusterip_tg_check(const struct xt_tgchk_param *par)
 				return PTR_ERR(config);
 		}
 	}
-	cipinfo->config = config;
 
 	ret = nf_ct_netns_get(par->net, par->family);
-	if (ret < 0)
+	if (ret < 0) {
 		pr_info("cannot load conntrack support for proto=%u\n",
 			par->family);
+		clusterip_config_entry_put(par->net, config);
+		clusterip_config_put(config);
+		return ret;
+	}
 
 	if (!par->net->xt.clusterip_deprecated_warning) {
 		pr_info("ipt_CLUSTERIP is deprecated and it will removed soon, "
@@ -509,6 +512,7 @@ static int clusterip_tg_check(const struct xt_tgchk_param *par)
 		par->net->xt.clusterip_deprecated_warning = true;
 	}
 
+	cipinfo->config = config;
 	return ret;
 }
 
Signature · 1 line
2.16.1
author maintainer
#2 lore raw

PATCH 2/2 netfilter: ipt_CLUSTERIP: put config instead of freeing it

In reply to Florian Westphal

Once struct is added to per-netns list it becomes visible to other cpus, so we cannot use kfree().

Also delay setting entries refcount to 1 until after everything is initialised so that when we call clusterip_config_put() in this spot entries is still zero.

---
 net/ipv4/netfilter/ipt_CLUSTERIP.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
net/ipv4/netfilter/ipt_CLUSTERIP.c +4 -3
index 4c8cfd352687..8a8ae61cea71 100644
@@ -232,7 +232,6 @@ clusterip_config_init(struct net *net, const struct ipt_clusterip_tgt_info *i,
 	c->hash_mode = i->hash_mode;
 	c->hash_initval = i->hash_initval;
 	refcount_set(&c->refcount, 1);
-	refcount_set(&c->entries, 1);
 
 	spin_lock_bh(&cn->lock);
 	if (__clusterip_config_find(net, ip)) {
@@ -263,8 +262,10 @@ clusterip_config_init(struct net *net, const struct ipt_clusterip_tgt_info *i,
 
 	c->notifier.notifier_call = clusterip_netdev_event;
 	err = register_netdevice_notifier(&c->notifier);
-	if (!err)
+	if (!err) {
+		refcount_set(&c->entries, 1);
 		return c;
+	}
 
 #ifdef CONFIG_PROC_FS
 	proc_remove(c->pde);
@@ -273,7 +274,7 @@ clusterip_config_init(struct net *net, const struct ipt_clusterip_tgt_info *i,
 	spin_lock_bh(&cn->lock);
 	list_del_rcu(&c->list);
 	spin_unlock_bh(&cn->lock);
-	kfree(c);
+	clusterip_config_put(c);
 
 	return ERR_PTR(err);
 }
Signature · 1 line
2.16.1
#3 lore raw
In reply to Florian Westphal
Quoted 6 lines · Florian Westphal

During code audit I found two more bugs in CLUSTERIP, first one is a refcount leak, second is possible use-after free due to kfree() of rcu-protected (and already visible) structure.

I think its time to remove this target, so, if anyone has a good reason to not nuke it in nf-next please let me know soon.

We have the cluster match replacement that has been around for ~10 years, it just needs an extra arptable rules.

So I'm fine with this nuke, actually I remember we talked about deprecating it.