Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

netfilter: ipset: Fix suspicious rcu_dereference_protected()

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>

authored by

Jozsef Kadlecsik and committed by
Pablo Neira Ayuso
8ecd0627 a8763466

+6 -5
+6 -5
net/netfilter/ipset/ip_set_core.c
··· 53 53 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_IPSET); 54 54 55 55 /* When the nfnl mutex or ip_set_ref_lock is held: */ 56 - #define ip_set_dereference(p) \ 57 - rcu_dereference_protected(p, \ 56 + #define ip_set_dereference(inst) \ 57 + rcu_dereference_protected((inst)->ip_set_list, \ 58 58 lockdep_nfnl_is_held(NFNL_SUBSYS_IPSET) || \ 59 - lockdep_is_held(&ip_set_ref_lock)) 59 + lockdep_is_held(&ip_set_ref_lock) || \ 60 + (inst)->is_deleted) 60 61 #define ip_set(inst, id) \ 61 - ip_set_dereference((inst)->ip_set_list)[id] 62 + ip_set_dereference(inst)[id] 62 63 #define ip_set_ref_netlink(inst,id) \ 63 64 rcu_dereference_raw((inst)->ip_set_list)[id] 64 65 #define ip_set_dereference_nfnl(p) \ ··· 1134 1133 if (!list) 1135 1134 goto cleanup; 1136 1135 /* nfnl mutex is held, both lists are valid */ 1137 - tmp = ip_set_dereference(inst->ip_set_list); 1136 + tmp = ip_set_dereference(inst); 1138 1137 memcpy(list, tmp, sizeof(struct ip_set *) * inst->ip_set_max); 1139 1138 rcu_assign_pointer(inst->ip_set_list, list); 1140 1139 /* Make sure all current packets have passed through */