ipv6: fix locking issues with loops over idev->addr_list

[ Upstream commit 51454ea42c1ab4e0c2828bb0d4d53957976980de ]

idev->addr_list needs to be protected by idev->lock. However, it is not
always possible to do so while iterating and performing actions on
inet6_ifaddr instances. For example, multiple functions (like
addrconf_{join,leave}_anycast) eventually call down to other functions
that acquire the idev->lock. The current code temporarily unlocked the
idev->lock during the loops, which can cause race conditions. Moving the
locks up is also not an appropriate solution as the ordering of lock
acquisition will be inconsistent with for example mc_lock.

This solution adds an additional field to inet6_ifaddr that is used
to temporarily add the instances to a temporary list while holding
idev->lock. The temporary list can then be traversed without holding
idev->lock. This change was done in two places. In addrconf_ifdown, the
list_for_each_entry_safe variant of the list loop is also no longer
necessary as there is no deletion within that specific loop.

Suggested-by: Paolo Abeni <pabeni@redhat.com>
Signed-off-by: Niels Dossche <dossche.niels@gmail.com>
Acked-by: Paolo Abeni <pabeni@redhat.com>
Link: https://lore.kernel.org/r/20220403231523.45843-1-dossche.niels@gmail.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
This commit is contained in:
Niels Dossche 2022-04-04 01:15:24 +02:00 committed by Greg Kroah-Hartman
parent 167affc117
commit 459524eaca
2 changed files with 32 additions and 6 deletions

View file

@ -69,6 +69,14 @@ struct inet6_ifaddr {
struct hlist_node addr_lst; struct hlist_node addr_lst;
struct list_head if_list; struct list_head if_list;
/*
* Used to safely traverse idev->addr_list in process context
* if the idev->lock needed to protect idev->addr_list cannot be held.
* In that case, add the items to this list temporarily and iterate
* without holding idev->lock.
* See addrconf_ifdown and dev_forward_change.
*/
struct list_head if_list_aux;
struct list_head tmp_list; struct list_head tmp_list;
struct inet6_ifaddr *ifpub; struct inet6_ifaddr *ifpub;

View file

@ -739,6 +739,7 @@ static void dev_forward_change(struct inet6_dev *idev)
{ {
struct net_device *dev; struct net_device *dev;
struct inet6_ifaddr *ifa; struct inet6_ifaddr *ifa;
LIST_HEAD(tmp_addr_list);
if (!idev) if (!idev)
return; return;
@ -757,14 +758,24 @@ static void dev_forward_change(struct inet6_dev *idev)
} }
} }
read_lock_bh(&idev->lock);
list_for_each_entry(ifa, &idev->addr_list, if_list) { list_for_each_entry(ifa, &idev->addr_list, if_list) {
if (ifa->flags&IFA_F_TENTATIVE) if (ifa->flags&IFA_F_TENTATIVE)
continue; continue;
list_add_tail(&ifa->if_list_aux, &tmp_addr_list);
}
read_unlock_bh(&idev->lock);
while (!list_empty(&tmp_addr_list)) {
ifa = list_first_entry(&tmp_addr_list,
struct inet6_ifaddr, if_list_aux);
list_del(&ifa->if_list_aux);
if (idev->cnf.forwarding) if (idev->cnf.forwarding)
addrconf_join_anycast(ifa); addrconf_join_anycast(ifa);
else else
addrconf_leave_anycast(ifa); addrconf_leave_anycast(ifa);
} }
inet6_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF, inet6_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF,
NETCONFA_FORWARDING, NETCONFA_FORWARDING,
dev->ifindex, &idev->cnf); dev->ifindex, &idev->cnf);
@ -3658,7 +3669,8 @@ static int addrconf_ifdown(struct net_device *dev, int how)
unsigned long event = how ? NETDEV_UNREGISTER : NETDEV_DOWN; unsigned long event = how ? NETDEV_UNREGISTER : NETDEV_DOWN;
struct net *net = dev_net(dev); struct net *net = dev_net(dev);
struct inet6_dev *idev; struct inet6_dev *idev;
struct inet6_ifaddr *ifa, *tmp; struct inet6_ifaddr *ifa;
LIST_HEAD(tmp_addr_list);
bool keep_addr = false; bool keep_addr = false;
int state, i; int state, i;
@ -3746,16 +3758,23 @@ restart:
write_lock_bh(&idev->lock); write_lock_bh(&idev->lock);
} }
list_for_each_entry_safe(ifa, tmp, &idev->addr_list, if_list) { list_for_each_entry(ifa, &idev->addr_list, if_list)
list_add_tail(&ifa->if_list_aux, &tmp_addr_list);
write_unlock_bh(&idev->lock);
while (!list_empty(&tmp_addr_list)) {
struct fib6_info *rt = NULL; struct fib6_info *rt = NULL;
bool keep; bool keep;
ifa = list_first_entry(&tmp_addr_list,
struct inet6_ifaddr, if_list_aux);
list_del(&ifa->if_list_aux);
addrconf_del_dad_work(ifa); addrconf_del_dad_work(ifa);
keep = keep_addr && (ifa->flags & IFA_F_PERMANENT) && keep = keep_addr && (ifa->flags & IFA_F_PERMANENT) &&
!addr_is_local(&ifa->addr); !addr_is_local(&ifa->addr);
write_unlock_bh(&idev->lock);
spin_lock_bh(&ifa->lock); spin_lock_bh(&ifa->lock);
if (keep) { if (keep) {
@ -3786,15 +3805,14 @@ restart:
addrconf_leave_solict(ifa->idev, &ifa->addr); addrconf_leave_solict(ifa->idev, &ifa->addr);
} }
write_lock_bh(&idev->lock);
if (!keep) { if (!keep) {
write_lock_bh(&idev->lock);
list_del_rcu(&ifa->if_list); list_del_rcu(&ifa->if_list);
write_unlock_bh(&idev->lock);
in6_ifa_put(ifa); in6_ifa_put(ifa);
} }
} }
write_unlock_bh(&idev->lock);
/* Step 5: Discard anycast and multicast list */ /* Step 5: Discard anycast and multicast list */
if (how) { if (how) {
ipv6_ac_destroy_dev(idev); ipv6_ac_destroy_dev(idev);