Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 02a23398 authored by Jozsef Kadlecsik's avatar Jozsef Kadlecsik Committed by Greg Kroah-Hartman
Browse files

netfilter: ipset: Fix race between IPSET_CMD_CREATE and IPSET_CMD_SWAP



[ Upstream commit 7433b6d2afd512d04398c73aa984d1e285be125b ]

Kyle Zeng reported that there is a race between IPSET_CMD_ADD and IPSET_CMD_SWAP
in netfilter/ip_set, which can lead to the invocation of `__ip_set_put` on a
wrong `set`, triggering the `BUG_ON(set->ref == 0);` check in it.

The race is caused by using the wrong reference counter, i.e. the ref counter instead
of ref_netlink.

Fixes: 24e227896bbf ("netfilter: ipset: Add schedule point in call_ad().")
Reported-by: default avatarKyle Zeng <zengyhkyle@gmail.com>
Closes: https://lore.kernel.org/netfilter-devel/ZPZqetxOmH+w%2Fmyc@westworld/#r


Tested-by: default avatarKyle Zeng <zengyhkyle@gmail.com>
Signed-off-by: default avatarJozsef Kadlecsik <kadlec@netfilter.org>
Signed-off-by: default avatarFlorian Westphal <fw@strlen.de>
Signed-off-by: default avatarSasha Levin <sashal@kernel.org>
parent 812da2a0
Loading
Loading
Loading
Loading
+10 −2
Original line number Diff line number Diff line
@@ -530,6 +530,14 @@ __ip_set_put(struct ip_set *set)
/* set->ref can be swapped out by ip_set_swap, netlink events (like dump) need
 * a separate reference counter
 */
static void
__ip_set_get_netlink(struct ip_set *set)
{
	write_lock_bh(&ip_set_ref_lock);
	set->ref_netlink++;
	write_unlock_bh(&ip_set_ref_lock);
}

static inline void
__ip_set_put_netlink(struct ip_set *set)
{
@@ -1529,11 +1537,11 @@ call_ad(struct sock *ctnl, struct sk_buff *skb, struct ip_set *set,

	do {
		if (retried) {
			__ip_set_get(set);
			__ip_set_get_netlink(set);
			nfnl_unlock(NFNL_SUBSYS_IPSET);
			cond_resched();
			nfnl_lock(NFNL_SUBSYS_IPSET);
			__ip_set_put(set);
			__ip_set_put_netlink(set);
		}

		ip_set_lock(set);