]> git.ipfire.org Git - thirdparty/ipset.git/commitdiff
netfilter: ipset: Fix suspicious rcu_dereference_protected()
authorJozsef Kadlecsik <kadlec@netfilter.org>
Mon, 17 Jun 2024 09:18:15 +0000 (11:18 +0200)
committerJozsef Kadlecsik <kadlec@netfilter.org>
Sun, 15 Dec 2024 16:57:48 +0000 (17:57 +0100)
When destroying all sets, we are either in pernet exit phase or
are executing a "destroy all sets command" from userspace. The latter
was taken into account in ip_set_dereference() (nfnetlink mutex is held),
but the former was not. The patch adds the required check to
rcu_dereference_protected() in ip_set_dereference().

Fixes: 4e7aaa6b82d6 ("netfilter: ipset: Fix race between namespace cleanup and gc in the list:set type")
Reported-by: syzbot+b62c37cdd58103293a5a@syzkaller.appspotmail.com
Reported-by: syzbot+cfbe1da5fdfc39efc293@syzkaller.appspotmail.com
Reported-by: kernel test robot <oliver.sang@intel.com>
Closes: https://lore.kernel.org/oe-lkp/202406141556.e0b6f17e-lkp@intel.com
Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
kernel/net/netfilter/ipset/ip_set_core.c

index 9ddb59410ac7f6ff41a8349cc51c51da73596dda..25526bd1053a4210f6388b1a081d1c95c71de4d7 100644 (file)
@@ -54,12 +54,13 @@ MODULE_DESCRIPTION("ip_set: protocol " __stringify(IPSET_PROTOCOL));
 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_IPSET);
 
 /* When the nfnl mutex or ip_set_ref_lock is held: */
-#define ip_set_dereference(p)          \
-       rcu_dereference_protected(p,    \
+#define ip_set_dereference(inst)       \
+       rcu_dereference_protected((inst)->ip_set_list,  \
                lockdep_nfnl_is_held(NFNL_SUBSYS_IPSET) || \
-               lockdep_is_held(&ip_set_ref_lock))
+               lockdep_is_held(&ip_set_ref_lock) || \
+               (inst)->is_deleted)
 #define ip_set(inst, id)               \
-       ip_set_dereference((inst)->ip_set_list)[id]
+       ip_set_dereference(inst)[id]
 #define ip_set_ref_netlink(inst,id)    \
        rcu_dereference_raw((inst)->ip_set_list)[id]
 #define ip_set_dereference_nfnl(p)     \
@@ -1143,7 +1144,7 @@ IPSET_CBFN(ip_set_create, struct net *n, struct sock *ctnl,
                if (!list)
                        goto cleanup;
                /* nfnl mutex is held, both lists are valid */
-               tmp = ip_set_dereference(inst->ip_set_list);
+               tmp = ip_set_dereference(inst);
                memcpy(list, tmp, sizeof(struct ip_set *) * inst->ip_set_max);
                rcu_assign_pointer(inst->ip_set_list, list);
                /* Make sure all current packets have passed through */