All of lore.kernel.org
 help / color / mirror / Atom feed
From: Sasha Levin <sashal@kernel.org>
To: linux-kernel@vger.kernel.org, stable@vger.kernel.org
Cc: Niels Dossche <dossche.niels@gmail.com>,
	Paolo Abeni <pabeni@redhat.com>, Jakub Kicinski <kuba@kernel.org>,
	Sasha Levin <sashal@kernel.org>,
	davem@davemloft.net, edumazet@google.com,
	yoshfuji@linux-ipv6.org, dsahern@kernel.org,
	netdev@vger.kernel.org
Subject: [PATCH AUTOSEL 4.19 06/38] ipv6: fix locking issues with loops over idev->addr_list
Date: Mon, 30 May 2022 09:48:52 -0400	[thread overview]
Message-ID: <20220530134924.1936816-6-sashal@kernel.org> (raw)
In-Reply-To: <20220530134924.1936816-1-sashal@kernel.org>

From: Niels Dossche <dossche.niels@gmail.com>

[ 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>
---
 include/net/if_inet6.h |  8 ++++++++
 net/ipv6/addrconf.c    | 30 ++++++++++++++++++++++++------
 2 files changed, 32 insertions(+), 6 deletions(-)

diff --git a/include/net/if_inet6.h b/include/net/if_inet6.h
index d7578cf49c3a..3953003b0490 100644
--- a/include/net/if_inet6.h
+++ b/include/net/if_inet6.h
@@ -69,6 +69,14 @@ struct inet6_ifaddr {
 
 	struct hlist_node	addr_lst;
 	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 inet6_ifaddr	*ifpub;
diff --git a/net/ipv6/addrconf.c b/net/ipv6/addrconf.c
index 9d8b791f63ef..a8db1efccbf5 100644
--- a/net/ipv6/addrconf.c
+++ b/net/ipv6/addrconf.c
@@ -739,6 +739,7 @@ static void dev_forward_change(struct inet6_dev *idev)
 {
 	struct net_device *dev;
 	struct inet6_ifaddr *ifa;
+	LIST_HEAD(tmp_addr_list);
 
 	if (!idev)
 		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) {
 		if (ifa->flags&IFA_F_TENTATIVE)
 			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)
 			addrconf_join_anycast(ifa);
 		else
 			addrconf_leave_anycast(ifa);
 	}
+
 	inet6_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF,
 				     NETCONFA_FORWARDING,
 				     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;
 	struct net *net = dev_net(dev);
 	struct inet6_dev *idev;
-	struct inet6_ifaddr *ifa, *tmp;
+	struct inet6_ifaddr *ifa;
+	LIST_HEAD(tmp_addr_list);
 	bool keep_addr = false;
 	int state, i;
 
@@ -3746,16 +3758,23 @@ static int addrconf_ifdown(struct net_device *dev, int how)
 		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;
 		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);
 
 		keep = keep_addr && (ifa->flags & IFA_F_PERMANENT) &&
 			!addr_is_local(&ifa->addr);
 
-		write_unlock_bh(&idev->lock);
 		spin_lock_bh(&ifa->lock);
 
 		if (keep) {
@@ -3786,15 +3805,14 @@ static int addrconf_ifdown(struct net_device *dev, int how)
 			addrconf_leave_solict(ifa->idev, &ifa->addr);
 		}
 
-		write_lock_bh(&idev->lock);
 		if (!keep) {
+			write_lock_bh(&idev->lock);
 			list_del_rcu(&ifa->if_list);
+			write_unlock_bh(&idev->lock);
 			in6_ifa_put(ifa);
 		}
 	}
 
-	write_unlock_bh(&idev->lock);
-
 	/* Step 5: Discard anycast and multicast list */
 	if (how) {
 		ipv6_ac_destroy_dev(idev);
-- 
2.35.1


  parent reply	other threads:[~2022-05-30 14:38 UTC|newest]

Thread overview: 66+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-30 13:48 [PATCH AUTOSEL 4.19 01/38] drm/virtio: fix NULL pointer dereference in virtio_gpu_conn_get_modes Sasha Levin
2022-05-30 13:48 ` Sasha Levin
2022-05-30 13:48 ` Sasha Levin
2022-05-30 13:48 ` [PATCH AUTOSEL 4.19 02/38] mwifiex: add mutex lock for call in mwifiex_dfs_chan_sw_work_queue Sasha Levin
2022-05-30 13:48 ` [PATCH AUTOSEL 4.19 03/38] b43legacy: Fix assigning negative value to unsigned variable Sasha Levin
2022-05-30 13:48   ` Sasha Levin
2022-05-30 13:48 ` [PATCH AUTOSEL 4.19 04/38] b43: " Sasha Levin
2022-05-30 13:48   ` Sasha Levin
2022-05-30 13:48 ` [PATCH AUTOSEL 4.19 05/38] ipw2x00: Fix potential NULL dereference in libipw_xmit() Sasha Levin
2022-05-30 13:48 ` Sasha Levin [this message]
2022-05-30 13:48 ` [PATCH AUTOSEL 4.19 07/38] fbcon: Consistently protect deferred_takeover with console_lock() Sasha Levin
2022-05-30 13:48   ` Sasha Levin
2022-05-30 13:48 ` [PATCH AUTOSEL 4.19 08/38] ACPICA: Avoid cache flush inside virtual machines Sasha Levin
2022-05-30 13:48 ` [PATCH AUTOSEL 4.19 09/38] ALSA: jack: Access input_dev under mutex Sasha Levin
2022-05-30 13:48   ` Sasha Levin
2022-05-30 13:48 ` [PATCH AUTOSEL 4.19 10/38] drm/amd/pm: fix double free in si_parse_power_table() Sasha Levin
2022-05-30 13:48   ` Sasha Levin
2022-05-30 13:48   ` Sasha Levin
2022-05-30 13:48 ` [PATCH AUTOSEL 4.19 11/38] ath9k: fix QCA9561 PA bias level Sasha Levin
2022-05-30 13:48 ` [PATCH AUTOSEL 4.19 12/38] media: venus: hfi: avoid null dereference in deinit Sasha Levin
2022-05-30 13:48 ` [PATCH AUTOSEL 4.19 13/38] media: pci: cx23885: Fix the error handling in cx23885_initdev() Sasha Levin
2022-05-30 13:49 ` [PATCH AUTOSEL 4.19 14/38] media: cx25821: Fix the warning when removing the module Sasha Levin
2022-05-30 13:49 ` [PATCH AUTOSEL 4.19 15/38] md/bitmap: don't set sb values if can't pass sanity check Sasha Levin
2022-05-30 13:49 ` [PATCH AUTOSEL 4.19 16/38] drm/sun4i: Add support for D1 TCONs Sasha Levin
2022-05-30 13:49   ` Sasha Levin
2022-05-30 14:41   ` Samuel Holland
2022-05-30 14:41     ` Samuel Holland
2022-05-30 14:41     ` Samuel Holland
2022-06-05 13:28     ` Sasha Levin
2022-06-05 13:28       ` Sasha Levin
2022-06-05 13:28       ` Sasha Levin
2022-05-30 13:49 ` [PATCH AUTOSEL 4.19 17/38] scsi: megaraid: Fix error check return value of register_chrdev() Sasha Levin
2022-05-30 13:49 ` [PATCH AUTOSEL 4.19 18/38] drm/plane: Move range check for format_count earlier Sasha Levin
2022-05-30 13:49   ` Sasha Levin
2022-05-30 13:49 ` [PATCH AUTOSEL 4.19 19/38] drm/amd/pm: fix the compile warning Sasha Levin
2022-05-30 13:49   ` Sasha Levin
2022-05-30 13:49   ` Sasha Levin
2022-05-30 13:49 ` [PATCH AUTOSEL 4.19 20/38] ipv6: Don't send rs packets to the interface of ARPHRD_TUNNEL Sasha Levin
2022-05-30 13:49 ` [PATCH AUTOSEL 4.19 21/38] ASoC: dapm: Don't fold register value changes into notifications Sasha Levin
2022-05-30 13:49   ` Sasha Levin
2022-05-30 13:49 ` [PATCH AUTOSEL 4.19 22/38] mlxsw: spectrum_dcb: Do not warn about priority changes Sasha Levin
2022-05-30 13:49 ` [PATCH AUTOSEL 4.19 23/38] ASoC: tscs454: Add endianness flag in snd_soc_component_driver Sasha Levin
2022-05-30 13:49   ` Sasha Levin
2022-05-30 13:49 ` [PATCH AUTOSEL 4.19 24/38] net: remove two BUG() from skb_checksum_help() Sasha Levin
2022-05-30 13:49 ` [PATCH AUTOSEL 4.19 25/38] s390/preempt: disable __preempt_count_add() optimization for PROFILE_ALL_BRANCHES Sasha Levin
2022-05-30 13:49 ` [PATCH AUTOSEL 4.19 26/38] dma-debug: change allocation mode from GFP_NOWAIT to GFP_ATIOMIC Sasha Levin
2022-05-30 13:49   ` Sasha Levin
2022-05-30 13:49 ` [PATCH AUTOSEL 4.19 27/38] ipmi:ssif: Check for NULL msg when handling events and messages Sasha Levin
2022-05-30 13:49 ` [PATCH AUTOSEL 4.19 28/38] rtlwifi: Use pr_warn instead of WARN_ONCE Sasha Levin
2022-05-30 13:49 ` [PATCH AUTOSEL 4.19 29/38] media: cec-adap.c: fix is_configuring state Sasha Levin
2022-05-30 13:49 ` [PATCH AUTOSEL 4.19 30/38] openrisc: start CPU timer early in boot Sasha Levin
2022-05-30 13:49   ` Sasha Levin
2022-05-30 13:49 ` [PATCH AUTOSEL 4.19 31/38] nvme-pci: fix a NULL pointer dereference in nvme_alloc_admin_tags Sasha Levin
2022-05-30 13:49 ` [PATCH AUTOSEL 4.19 32/38] ASoC: rt5645: Fix errorenous cleanup order Sasha Levin
2022-05-30 13:49   ` Sasha Levin
2022-05-30 13:49 ` [PATCH AUTOSEL 4.19 33/38] net: phy: micrel: Allow probing without .driver_data Sasha Levin
2022-05-30 13:49 ` [PATCH AUTOSEL 4.19 34/38] media: exynos4-is: Fix compile warning Sasha Levin
2022-05-30 13:49   ` Sasha Levin
2022-05-30 13:49 ` [PATCH AUTOSEL 4.19 35/38] ARM: 9201/1: spectre-bhb: rely on linker to emit cross-section literal loads Sasha Levin
2022-05-30 13:49   ` Sasha Levin
2022-05-30 13:52   ` Ard Biesheuvel
2022-05-30 13:52     ` Ard Biesheuvel
2022-05-30 13:49 ` [PATCH AUTOSEL 4.19 36/38] hwmon: Make chip parameter for with_info API mandatory Sasha Levin
2022-05-30 14:29   ` Guenter Roeck
2022-05-30 13:49 ` [PATCH AUTOSEL 4.19 37/38] rxrpc: Return an error to sendmsg if call failed Sasha Levin
2022-05-30 13:49 ` [PATCH AUTOSEL 4.19 38/38] eth: tg3: silence the GCC 12 array-bounds warning Sasha Levin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220530134924.1936816-6-sashal@kernel.org \
    --to=sashal@kernel.org \
    --cc=davem@davemloft.net \
    --cc=dossche.niels@gmail.com \
    --cc=dsahern@kernel.org \
    --cc=edumazet@google.com \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=stable@vger.kernel.org \
    --cc=yoshfuji@linux-ipv6.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.