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: Eli Billauer <eli.billauer@gmail.com>,
	Oliver Neukum <oneukum@suse.com>,
	Alan Stern <stern@rowland.harvard.edu>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Sasha Levin <sashal@kernel.org>,
	linux-usb@vger.kernel.org
Subject: [PATCH AUTOSEL 5.4 76/80] usb: core: Solve race condition in anchor cleanup functions
Date: Sun, 18 Oct 2020 15:22:27 -0400	[thread overview]
Message-ID: <20201018192231.4054535-76-sashal@kernel.org> (raw)
In-Reply-To: <20201018192231.4054535-1-sashal@kernel.org>

From: Eli Billauer <eli.billauer@gmail.com>

[ Upstream commit fbc299437c06648afcc7891e6e2e6638dd48d4df ]

usb_kill_anchored_urbs() is commonly used to cancel all URBs on an
anchor just before releasing resources which the URBs rely on. By doing
so, users of this function rely on that no completer callbacks will take
place from any URB on the anchor after it returns.

However if this function is called in parallel with __usb_hcd_giveback_urb
processing a URB on the anchor, the latter may call the completer
callback after usb_kill_anchored_urbs() returns. This can lead to a
kernel panic due to use after release of memory in interrupt context.

The race condition is that __usb_hcd_giveback_urb() first unanchors the URB
and then makes the completer callback. Such URB is hence invisible to
usb_kill_anchored_urbs(), allowing it to return before the completer has
been called, since the anchor's urb_list is empty.

Even worse, if the racing completer callback resubmits the URB, it may
remain in the system long after usb_kill_anchored_urbs() returns.

Hence list_empty(&anchor->urb_list), which is used in the existing
while-loop, doesn't reliably ensure that all URBs of the anchor are gone.

A similar problem exists with usb_poison_anchored_urbs() and
usb_scuttle_anchored_urbs().

This patch adds an external do-while loop, which ensures that all URBs
are indeed handled before these three functions return. This change has
no effect at all unless the race condition occurs, in which case the
loop will busy-wait until the racing completer callback has finished.
This is a rare condition, so the CPU waste of this spinning is
negligible.

The additional do-while loop relies on usb_anchor_check_wakeup(), which
returns true iff the anchor list is empty, and there is no
__usb_hcd_giveback_urb() in the system that is in the middle of the
unanchor-before-complete phase. The @suspend_wakeups member of
struct usb_anchor is used for this purpose, which was introduced to solve
another problem which the same race condition causes, in commit
6ec4147e7bdb ("usb-anchor: Delay usb_wait_anchor_empty_timeout wake up
till completion is done").

The surely_empty variable is necessary, because usb_anchor_check_wakeup()
must be called with the lock held to prevent races. However the spinlock
must be released and reacquired if the outer loop spins with an empty
URB list while waiting for the unanchor-before-complete passage to finish:
The completer callback may very well attempt to take the very same lock.

To summarize, using usb_anchor_check_wakeup() means that the patched
functions can return only when the anchor's list is empty, and there is
no invisible URB being processed. Since the inner while loop finishes on
the empty list condition, the new do-while loop will terminate as well,
except for when the said race condition occurs.

Signed-off-by: Eli Billauer <eli.billauer@gmail.com>
Acked-by: Oliver Neukum <oneukum@suse.com>
Acked-by: Alan Stern <stern@rowland.harvard.edu>
Link: https://lore.kernel.org/r/20200731054650.30644-1-eli.billauer@gmail.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
 drivers/usb/core/urb.c | 89 +++++++++++++++++++++++++-----------------
 1 file changed, 54 insertions(+), 35 deletions(-)

diff --git a/drivers/usb/core/urb.c b/drivers/usb/core/urb.c
index da923ec176122..31ca5abb4c12a 100644
--- a/drivers/usb/core/urb.c
+++ b/drivers/usb/core/urb.c
@@ -772,11 +772,12 @@ void usb_block_urb(struct urb *urb)
 EXPORT_SYMBOL_GPL(usb_block_urb);
 
 /**
- * usb_kill_anchored_urbs - cancel transfer requests en masse
+ * usb_kill_anchored_urbs - kill all URBs associated with an anchor
  * @anchor: anchor the requests are bound to
  *
- * this allows all outstanding URBs to be killed starting
- * from the back of the queue
+ * This kills all outstanding URBs starting from the back of the queue,
+ * with guarantee that no completer callbacks will take place from the
+ * anchor after this function returns.
  *
  * This routine should not be called by a driver after its disconnect
  * method has returned.
@@ -784,20 +785,26 @@ EXPORT_SYMBOL_GPL(usb_block_urb);
 void usb_kill_anchored_urbs(struct usb_anchor *anchor)
 {
 	struct urb *victim;
+	int surely_empty;
 
-	spin_lock_irq(&anchor->lock);
-	while (!list_empty(&anchor->urb_list)) {
-		victim = list_entry(anchor->urb_list.prev, struct urb,
-				    anchor_list);
-		/* we must make sure the URB isn't freed before we kill it*/
-		usb_get_urb(victim);
-		spin_unlock_irq(&anchor->lock);
-		/* this will unanchor the URB */
-		usb_kill_urb(victim);
-		usb_put_urb(victim);
+	do {
 		spin_lock_irq(&anchor->lock);
-	}
-	spin_unlock_irq(&anchor->lock);
+		while (!list_empty(&anchor->urb_list)) {
+			victim = list_entry(anchor->urb_list.prev,
+					    struct urb, anchor_list);
+			/* make sure the URB isn't freed before we kill it */
+			usb_get_urb(victim);
+			spin_unlock_irq(&anchor->lock);
+			/* this will unanchor the URB */
+			usb_kill_urb(victim);
+			usb_put_urb(victim);
+			spin_lock_irq(&anchor->lock);
+		}
+		surely_empty = usb_anchor_check_wakeup(anchor);
+
+		spin_unlock_irq(&anchor->lock);
+		cpu_relax();
+	} while (!surely_empty);
 }
 EXPORT_SYMBOL_GPL(usb_kill_anchored_urbs);
 
@@ -816,21 +823,27 @@ EXPORT_SYMBOL_GPL(usb_kill_anchored_urbs);
 void usb_poison_anchored_urbs(struct usb_anchor *anchor)
 {
 	struct urb *victim;
+	int surely_empty;
 
-	spin_lock_irq(&anchor->lock);
-	anchor->poisoned = 1;
-	while (!list_empty(&anchor->urb_list)) {
-		victim = list_entry(anchor->urb_list.prev, struct urb,
-				    anchor_list);
-		/* we must make sure the URB isn't freed before we kill it*/
-		usb_get_urb(victim);
-		spin_unlock_irq(&anchor->lock);
-		/* this will unanchor the URB */
-		usb_poison_urb(victim);
-		usb_put_urb(victim);
+	do {
 		spin_lock_irq(&anchor->lock);
-	}
-	spin_unlock_irq(&anchor->lock);
+		anchor->poisoned = 1;
+		while (!list_empty(&anchor->urb_list)) {
+			victim = list_entry(anchor->urb_list.prev,
+					    struct urb, anchor_list);
+			/* make sure the URB isn't freed before we kill it */
+			usb_get_urb(victim);
+			spin_unlock_irq(&anchor->lock);
+			/* this will unanchor the URB */
+			usb_poison_urb(victim);
+			usb_put_urb(victim);
+			spin_lock_irq(&anchor->lock);
+		}
+		surely_empty = usb_anchor_check_wakeup(anchor);
+
+		spin_unlock_irq(&anchor->lock);
+		cpu_relax();
+	} while (!surely_empty);
 }
 EXPORT_SYMBOL_GPL(usb_poison_anchored_urbs);
 
@@ -970,14 +983,20 @@ void usb_scuttle_anchored_urbs(struct usb_anchor *anchor)
 {
 	struct urb *victim;
 	unsigned long flags;
+	int surely_empty;
+
+	do {
+		spin_lock_irqsave(&anchor->lock, flags);
+		while (!list_empty(&anchor->urb_list)) {
+			victim = list_entry(anchor->urb_list.prev,
+					    struct urb, anchor_list);
+			__usb_unanchor_urb(victim, anchor);
+		}
+		surely_empty = usb_anchor_check_wakeup(anchor);
 
-	spin_lock_irqsave(&anchor->lock, flags);
-	while (!list_empty(&anchor->urb_list)) {
-		victim = list_entry(anchor->urb_list.prev, struct urb,
-				    anchor_list);
-		__usb_unanchor_urb(victim, anchor);
-	}
-	spin_unlock_irqrestore(&anchor->lock, flags);
+		spin_unlock_irqrestore(&anchor->lock, flags);
+		cpu_relax();
+	} while (!surely_empty);
 }
 
 EXPORT_SYMBOL_GPL(usb_scuttle_anchored_urbs);
-- 
2.25.1


  parent reply	other threads:[~2020-10-18 19:41 UTC|newest]

Thread overview: 95+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-18 19:21 [PATCH AUTOSEL 5.4 01/80] md/bitmap: fix memory leak of temporary bitmap Sasha Levin
2020-10-18 19:21 ` [PATCH AUTOSEL 5.4 02/80] block: ratelimit handle_bad_sector() message Sasha Levin
2020-10-18 19:21 ` [PATCH AUTOSEL 5.4 03/80] crypto: ccp - fix error handling Sasha Levin
2020-10-18 19:21 ` [PATCH AUTOSEL 5.4 04/80] x86/asm: Replace __force_order with a memory clobber Sasha Levin
2020-10-18 19:21 ` [PATCH AUTOSEL 5.4 05/80] x86/mce: Add Skylake quirk for patrol scrub reported errors Sasha Levin
2020-10-18 19:21 ` [PATCH AUTOSEL 5.4 06/80] media: firewire: fix memory leak Sasha Levin
2020-10-18 19:21 ` [PATCH AUTOSEL 5.4 07/80] media: ati_remote: sanity check for both endpoints Sasha Levin
2020-10-18 19:21 ` [PATCH AUTOSEL 5.4 08/80] media: st-delta: Fix reference count leak in delta_run_work Sasha Levin
2020-10-18 19:21 ` [PATCH AUTOSEL 5.4 09/80] media: sti: Fix reference count leaks Sasha Levin
2020-10-18 19:21 ` [PATCH AUTOSEL 5.4 10/80] media: exynos4-is: Fix several reference count leaks due to pm_runtime_get_sync Sasha Levin
2020-10-18 19:21   ` Sasha Levin
2020-10-18 19:21 ` [PATCH AUTOSEL 5.4 11/80] media: exynos4-is: Fix a reference count leak " Sasha Levin
2020-10-18 19:21   ` Sasha Levin
2020-10-18 19:21 ` [PATCH AUTOSEL 5.4 12/80] media: exynos4-is: Fix a reference count leak Sasha Levin
2020-10-18 19:21   ` Sasha Levin
2020-10-18 19:21 ` [PATCH AUTOSEL 5.4 13/80] media: vsp1: Fix runtime PM imbalance on error Sasha Levin
2020-10-18 19:21 ` [PATCH AUTOSEL 5.4 14/80] media: platform: s3c-camif: " Sasha Levin
2020-10-18 19:21 ` [PATCH AUTOSEL 5.4 15/80] media: platform: sti: hva: " Sasha Levin
2020-10-18 19:21 ` [PATCH AUTOSEL 5.4 16/80] media: bdisp: " Sasha Levin
2020-10-18 19:21 ` [PATCH AUTOSEL 5.4 17/80] media: media/pci: prevent memory leak in bttv_probe Sasha Levin
2020-10-18 19:21 ` [PATCH AUTOSEL 5.4 18/80] x86/mce: Make mce_rdmsrl() panic on an inaccessible MSR Sasha Levin
2020-10-18 19:21 ` [PATCH AUTOSEL 5.4 19/80] media: rcar_drif: Fix fwnode reference leak when parsing DT Sasha Levin
2020-10-18 19:21 ` [PATCH AUTOSEL 5.4 20/80] media: uvcvideo: Ensure all probed info is returned to v4l2 Sasha Levin
2020-10-18 19:21 ` [PATCH AUTOSEL 5.4 21/80] seccomp: kill process instead of thread for unknown actions Sasha Levin
2020-10-18 19:21 ` [PATCH AUTOSEL 5.4 22/80] mmc: sdio: Check for CISTPL_VERS_1 buffer size Sasha Levin
2020-10-18 19:21 ` [PATCH AUTOSEL 5.4 23/80] media: saa7134: avoid a shift overflow Sasha Levin
2020-10-18 19:21 ` [PATCH AUTOSEL 5.4 24/80] media: venus: fixes for list corruption Sasha Levin
2020-10-18 19:21 ` [PATCH AUTOSEL 5.4 25/80] fs: dlm: fix configfs memory leak Sasha Levin
2020-10-18 19:21   ` [Cluster-devel] " Sasha Levin
2020-10-18 19:21 ` [PATCH AUTOSEL 5.4 26/80] media: venus: core: Fix runtime PM imbalance in venus_probe Sasha Levin
2020-10-18 19:21 ` [PATCH AUTOSEL 5.4 27/80] ipv6/icmp: l3mdev: Perform icmp error route lookup on source device routing table (v2) Sasha Levin
2020-10-18 19:21 ` [PATCH AUTOSEL 5.4 28/80] ntfs: add check for mft record size in superblock Sasha Levin
2020-10-18 19:21 ` [PATCH AUTOSEL 5.4 29/80] ip_gre: set dev->hard_header_len and dev->needed_headroom properly Sasha Levin
2020-10-18 19:21 ` [PATCH AUTOSEL 5.4 30/80] mac80211: handle lack of sband->bitrates in rates Sasha Levin
2020-10-18 19:21 ` [PATCH AUTOSEL 5.4 31/80] PM: hibernate: remove the bogus call to get_gendisk() in software_resume() Sasha Levin
2020-10-18 19:21 ` [PATCH AUTOSEL 5.4 32/80] scsi: mvumi: Fix error return in mvumi_io_attach() Sasha Levin
2020-10-18 19:21 ` [PATCH AUTOSEL 5.4 33/80] scsi: target: core: Add CONTROL field for trace events Sasha Levin
2020-10-18 19:21 ` [PATCH AUTOSEL 5.4 34/80] mic: vop: copy data to kernel space then write to io memory Sasha Levin
2020-10-18 19:21 ` [PATCH AUTOSEL 5.4 35/80] misc: vop: add round_up(x,4) for vring_size to avoid kernel panic Sasha Levin
2020-10-18 19:21 ` [PATCH AUTOSEL 5.4 36/80] usb: dwc3: Add splitdisable quirk for Hisilicon Kirin Soc Sasha Levin
2020-10-18 19:21 ` [PATCH AUTOSEL 5.4 37/80] usb: gadget: function: printer: fix use-after-free in __lock_acquire Sasha Levin
2020-10-18 19:21 ` [PATCH AUTOSEL 5.4 38/80] udf: Limit sparing table size Sasha Levin
2020-10-18 19:21 ` [PATCH AUTOSEL 5.4 39/80] udf: Avoid accessing uninitialized data on failed inode read Sasha Levin
2020-10-18 19:21 ` [PATCH AUTOSEL 5.4 40/80] rtw88: increse the size of rx buffer size Sasha Levin
2020-10-18 19:21 ` [PATCH AUTOSEL 5.4 41/80] USB: cdc-acm: handle broken union descriptors Sasha Levin
2020-10-18 19:21 ` [PATCH AUTOSEL 5.4 42/80] usb: dwc3: simple: add support for Hikey 970 Sasha Levin
2020-10-18 19:21 ` [PATCH AUTOSEL 5.4 43/80] can: flexcan: flexcan_chip_stop(): add error handling and propagate error value Sasha Levin
2020-10-18 19:21 ` [PATCH AUTOSEL 5.4 44/80] ath9k: hif_usb: fix race condition between usb_get_urb() and usb_kill_anchored_urbs() Sasha Levin
2020-10-18 19:21 ` [PATCH AUTOSEL 5.4 45/80] drm/panfrost: add amlogic reset quirk callback Sasha Levin
2020-10-18 19:21   ` Sasha Levin
2020-10-18 19:21 ` [PATCH AUTOSEL 5.4 46/80] bpf: Limit caller's stack depth 256 for subprogs with tailcalls Sasha Levin
2020-10-18 19:21 ` [PATCH AUTOSEL 5.4 47/80] misc: rtsx: Fix memory leak in rtsx_pci_probe Sasha Levin
2020-10-18 19:21 ` [PATCH AUTOSEL 5.4 48/80] reiserfs: only call unlock_new_inode() if I_NEW Sasha Levin
2020-10-18 19:22 ` [PATCH AUTOSEL 5.4 49/80] opp: Prevent memory leak in dev_pm_opp_attach_genpd() Sasha Levin
2020-10-18 19:22 ` [PATCH AUTOSEL 5.4 50/80] xfs: make sure the rt allocator doesn't run off the end Sasha Levin
2020-10-18 19:22 ` [PATCH AUTOSEL 5.4 51/80] usb: ohci: Default to per-port over-current protection Sasha Levin
2020-10-18 19:22 ` [PATCH AUTOSEL 5.4 52/80] Bluetooth: Only mark socket zapped after unlocking Sasha Levin
2020-10-18 19:22 ` [PATCH AUTOSEL 5.4 53/80] drm/msm/a6xx: fix a potential overflow issue Sasha Levin
2020-10-18 19:22   ` Sasha Levin
2020-10-18 19:22 ` [PATCH AUTOSEL 5.4 54/80] iomap: fix WARN_ON_ONCE() from unprivileged users Sasha Levin
2020-10-18 19:22 ` [PATCH AUTOSEL 5.4 55/80] scsi: ibmvfc: Fix error return in ibmvfc_probe() Sasha Levin
2020-10-18 19:22   ` Sasha Levin
2020-10-18 19:22 ` [PATCH AUTOSEL 5.4 56/80] scsi: qla2xxx: Warn if done() or free() are called on an already freed srb Sasha Levin
2020-10-18 19:22 ` [PATCH AUTOSEL 5.4 57/80] selftests/bpf: Fix test_sysctl_loop{1, 2} failure due to clang change Sasha Levin
2020-10-18 19:22 ` [PATCH AUTOSEL 5.4 58/80] brcmsmac: fix memory leak in wlc_phy_attach_lcnphy Sasha Levin
2020-10-18 19:22 ` [PATCH AUTOSEL 5.4 59/80] rtl8xxxu: prevent potential memory leak Sasha Levin
2020-10-18 19:22 ` [PATCH AUTOSEL 5.4 60/80] Fix use after free in get_capset_info callback Sasha Levin
2020-10-18 19:22   ` Sasha Levin
2020-10-18 19:22   ` Sasha Levin
2020-10-18 19:22 ` [PATCH AUTOSEL 5.4 61/80] HID: ite: Add USB id match for Acer One S1003 keyboard dock Sasha Levin
2020-10-18 19:22 ` [PATCH AUTOSEL 5.4 62/80] scsi: qedf: Return SUCCESS if stale rport is encountered Sasha Levin
2020-10-18 19:22 ` [PATCH AUTOSEL 5.4 63/80] scsi: qedi: Protect active command list to avoid list corruption Sasha Levin
2020-10-18 19:22 ` [PATCH AUTOSEL 5.4 64/80] scsi: qedi: Fix list_del corruption while removing active I/O Sasha Levin
2020-10-18 19:22 ` [PATCH AUTOSEL 5.4 65/80] fbmem: add margin check to fb_check_caps() Sasha Levin
2020-10-18 19:22   ` Sasha Levin
2020-10-18 19:22 ` [PATCH AUTOSEL 5.4 66/80] tty: ipwireless: fix error handling Sasha Levin
2020-10-18 19:22 ` [PATCH AUTOSEL 5.4 67/80] drm/amd/display: Fix a list corruption Sasha Levin
2020-10-18 19:22   ` Sasha Levin
2020-10-18 19:22   ` Sasha Levin
2020-10-18 19:22 ` [PATCH AUTOSEL 5.4 68/80] Bluetooth: btusb: Fix memleak in btusb_mtk_submit_wmt_recv_urb Sasha Levin
2020-10-18 19:22 ` [PATCH AUTOSEL 5.4 69/80] ipvs: Fix uninit-value in do_ip_vs_set_ctl() Sasha Levin
2020-10-18 19:22 ` [PATCH AUTOSEL 5.4 70/80] reiserfs: Fix memory leak in reiserfs_parse_options() Sasha Levin
2020-10-18 19:22 ` [PATCH AUTOSEL 5.4 71/80] mwifiex: don't call del_timer_sync() on uninitialized timer Sasha Levin
2020-10-18 19:22 ` [PATCH AUTOSEL 5.4 72/80] ALSA: hda/ca0132 - Add AE-7 microphone selection commands Sasha Levin
2020-10-18 19:22   ` Sasha Levin
2020-10-18 19:22 ` [PATCH AUTOSEL 5.4 73/80] ALSA: hda/ca0132 - Add new quirk ID for SoundBlaster AE-7 Sasha Levin
2020-10-18 19:22   ` Sasha Levin
2020-10-18 19:22 ` [PATCH AUTOSEL 5.4 74/80] scsi: smartpqi: Avoid crashing kernel for controller issues Sasha Levin
2020-10-18 19:22 ` [PATCH AUTOSEL 5.4 75/80] brcm80211: fix possible memleak in brcmf_proto_msgbuf_attach Sasha Levin
2020-10-18 19:22 ` Sasha Levin [this message]
2020-10-18 19:22 ` [PATCH AUTOSEL 5.4 77/80] scsi: ufs: ufs-qcom: Fix race conditions caused by ufs_qcom_testbus_config() Sasha Levin
2020-10-18 19:22 ` [PATCH AUTOSEL 5.4 78/80] dmaengine: dw: Add DMA-channels mask cell support Sasha Levin
2020-10-18 19:22 ` [PATCH AUTOSEL 5.4 79/80] dmaengine: dw: Activate FIFO-mode for memory peripherals only Sasha Levin
2020-10-18 19:22 ` [PATCH AUTOSEL 5.4 80/80] ath10k: check idx validity in __ath10k_htt_rx_ring_fill_n() Sasha Levin
2020-10-18 19:22   ` 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=20201018192231.4054535-76-sashal@kernel.org \
    --to=sashal@kernel.org \
    --cc=eli.billauer@gmail.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=oneukum@suse.com \
    --cc=stable@vger.kernel.org \
    --cc=stern@rowland.harvard.edu \
    /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.