linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: linux-kernel@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	stable@vger.kernel.org, Alan Stern <stern@rowland.harvard.edu>,
	Christian Lamparter <chunkeey@gmail.com>,
	Kalle Valo <kvalo@codeaurora.org>,
	Ben Hutchings <ben.hutchings@codethink.co.uk>,
	syzbot+200d4bb11b23d929335f@syzkaller.appspotmail.com
Subject: [PATCH 4.4 08/76] p54usb: Fix race between disconnect and firmware loading
Date: Wed, 22 Jan 2020 10:28:24 +0100	[thread overview]
Message-ID: <20200122092752.433884919@linuxfoundation.org> (raw)
In-Reply-To: <20200122092751.587775548@linuxfoundation.org>

From: Alan Stern <stern@rowland.harvard.edu>

commit 6e41e2257f1094acc37618bf6c856115374c6922 upstream.

The syzbot fuzzer found a bug in the p54 USB wireless driver.  The
issue involves a race between disconnect and the firmware-loader
callback routine, and it has several aspects.

One big problem is that when the firmware can't be loaded, the
callback routine tries to unbind the driver from the USB _device_ (by
calling device_release_driver) instead of from the USB _interface_ to
which it is actually bound (by calling usb_driver_release_interface).

The race involves access to the private data structure.  The driver's
disconnect handler waits for a completion that is signalled by the
firmware-loader callback routine.  As soon as the completion is
signalled, you have to assume that the private data structure may have
been deallocated by the disconnect handler -- even if the firmware was
loaded without errors.  However, the callback routine does access the
private data several times after that point.

Another problem is that, in order to ensure that the USB device
structure hasn't been freed when the callback routine runs, the driver
takes a reference to it.  This isn't good enough any more, because now
that the callback routine calls usb_driver_release_interface, it has
to ensure that the interface structure hasn't been freed.

Finally, the driver takes an unnecessary reference to the USB device
structure in the probe function and drops the reference in the
disconnect handler.  This extra reference doesn't accomplish anything,
because the USB core already guarantees that a device structure won't
be deallocated while a driver is still bound to any of its interfaces.

To fix these problems, this patch makes the following changes:

	Call usb_driver_release_interface() rather than
	device_release_driver().

	Don't signal the completion until after the important
	information has been copied out of the private data structure,
	and don't refer to the private data at all thereafter.

	Lock udev (the interface's parent) before unbinding the driver
	instead of locking udev->parent.

	During the firmware loading process, take a reference to the
	USB interface instead of the USB device.

	Don't take an unnecessary reference to the device during probe
	(and then don't drop it during disconnect).

Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Reported-and-tested-by: syzbot+200d4bb11b23d929335f@syzkaller.appspotmail.com
Acked-by: Christian Lamparter <chunkeey@gmail.com>
Signed-off-by: Kalle Valo <kvalo@codeaurora.org>
[bwh: Backported to 4.4: adjust filename]
Signed-off-by: Ben Hutchings <ben.hutchings@codethink.co.uk>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
 drivers/net/wireless/p54/p54usb.c |   43 +++++++++++++++-----------------------
 1 file changed, 18 insertions(+), 25 deletions(-)

--- a/drivers/net/wireless/p54/p54usb.c
+++ b/drivers/net/wireless/p54/p54usb.c
@@ -33,6 +33,8 @@ MODULE_ALIAS("prism54usb");
 MODULE_FIRMWARE("isl3886usb");
 MODULE_FIRMWARE("isl3887usb");
 
+static struct usb_driver p54u_driver;
+
 /*
  * Note:
  *
@@ -921,9 +923,9 @@ static void p54u_load_firmware_cb(const
 {
 	struct p54u_priv *priv = context;
 	struct usb_device *udev = priv->udev;
+	struct usb_interface *intf = priv->intf;
 	int err;
 
-	complete(&priv->fw_wait_load);
 	if (firmware) {
 		priv->fw = firmware;
 		err = p54u_start_ops(priv);
@@ -932,26 +934,22 @@ static void p54u_load_firmware_cb(const
 		dev_err(&udev->dev, "Firmware not found.\n");
 	}
 
-	if (err) {
-		struct device *parent = priv->udev->dev.parent;
-
-		dev_err(&udev->dev, "failed to initialize device (%d)\n", err);
-
-		if (parent)
-			device_lock(parent);
+	complete(&priv->fw_wait_load);
+	/*
+	 * At this point p54u_disconnect may have already freed
+	 * the "priv" context. Do not use it anymore!
+	 */
+	priv = NULL;
 
-		device_release_driver(&udev->dev);
-		/*
-		 * At this point p54u_disconnect has already freed
-		 * the "priv" context. Do not use it anymore!
-		 */
-		priv = NULL;
+	if (err) {
+		dev_err(&intf->dev, "failed to initialize device (%d)\n", err);
 
-		if (parent)
-			device_unlock(parent);
+		usb_lock_device(udev);
+		usb_driver_release_interface(&p54u_driver, intf);
+		usb_unlock_device(udev);
 	}
 
-	usb_put_dev(udev);
+	usb_put_intf(intf);
 }
 
 static int p54u_load_firmware(struct ieee80211_hw *dev,
@@ -972,14 +970,14 @@ static int p54u_load_firmware(struct iee
 	dev_info(&priv->udev->dev, "Loading firmware file %s\n",
 	       p54u_fwlist[i].fw);
 
-	usb_get_dev(udev);
+	usb_get_intf(intf);
 	err = request_firmware_nowait(THIS_MODULE, 1, p54u_fwlist[i].fw,
 				      device, GFP_KERNEL, priv,
 				      p54u_load_firmware_cb);
 	if (err) {
 		dev_err(&priv->udev->dev, "(p54usb) cannot load firmware %s "
 					  "(%d)!\n", p54u_fwlist[i].fw, err);
-		usb_put_dev(udev);
+		usb_put_intf(intf);
 	}
 
 	return err;
@@ -1011,8 +1009,6 @@ static int p54u_probe(struct usb_interfa
 	skb_queue_head_init(&priv->rx_queue);
 	init_usb_anchor(&priv->submitted);
 
-	usb_get_dev(udev);
-
 	/* really lazy and simple way of figuring out if we're a 3887 */
 	/* TODO: should just stick the identification in the device table */
 	i = intf->altsetting->desc.bNumEndpoints;
@@ -1053,10 +1049,8 @@ static int p54u_probe(struct usb_interfa
 		priv->upload_fw = p54u_upload_firmware_net2280;
 	}
 	err = p54u_load_firmware(dev, intf);
-	if (err) {
-		usb_put_dev(udev);
+	if (err)
 		p54_free_common(dev);
-	}
 	return err;
 }
 
@@ -1072,7 +1066,6 @@ static void p54u_disconnect(struct usb_i
 	wait_for_completion(&priv->fw_wait_load);
 	p54_unregister_common(dev);
 
-	usb_put_dev(interface_to_usbdev(intf));
 	release_firmware(priv->fw);
 	p54_free_common(dev);
 }



  parent reply	other threads:[~2020-01-22  9:57 UTC|newest]

Thread overview: 81+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-22  9:28 [PATCH 4.4 00/76] 4.4.211-stable review Greg Kroah-Hartman
2020-01-22  9:28 ` [PATCH 4.4 01/76] hidraw: Return EPOLLOUT from hidraw_poll Greg Kroah-Hartman
2020-01-22  9:28 ` [PATCH 4.4 02/76] HID: hidraw: Fix returning " Greg Kroah-Hartman
2020-01-22  9:28 ` [PATCH 4.4 03/76] HID: hidraw, uhid: Always report EPOLLOUT Greg Kroah-Hartman
2020-01-22  9:28 ` [PATCH 4.4 04/76] rsi: add fix for crash during assertions Greg Kroah-Hartman
2020-01-22  9:28 ` [PATCH 4.4 05/76] cfg80211/mac80211: make ieee80211_send_layer2_update a public function Greg Kroah-Hartman
2020-01-22  9:28 ` [PATCH 4.4 06/76] mac80211: Do not send Layer 2 Update frame before authorization Greg Kroah-Hartman
2020-01-22  9:28 ` [PATCH 4.4 07/76] media: usb:zr364xx:Fix KASAN:null-ptr-deref Read in zr364xx_vidioc_querycap Greg Kroah-Hartman
2020-01-22  9:28 ` Greg Kroah-Hartman [this message]
2020-01-22  9:28 ` [PATCH 4.4 09/76] ALSA: line6: Fix write on zero-sized buffer Greg Kroah-Hartman
2020-01-22  9:28 ` [PATCH 4.4 10/76] ALSA: line6: Fix memory leak at line6_init_pcm() error path Greg Kroah-Hartman
2020-01-22  9:28 ` [PATCH 4.4 11/76] mm/page_alloc.c: calculate available memory in a separate function Greg Kroah-Hartman
2020-01-22  9:28 ` [PATCH 4.4 12/76] xen: let alloc_xenballooned_pages() fail if not enough memory free Greg Kroah-Hartman
2020-01-22  9:28 ` [PATCH 4.4 13/76] wimax: i2400: fix memory leak Greg Kroah-Hartman
2020-01-22  9:28 ` [PATCH 4.4 14/76] wimax: i2400: Fix memory leak in i2400m_op_rfkill_sw_toggle Greg Kroah-Hartman
2020-01-22  9:28 ` [PATCH 4.4 15/76] ext4: fix use-after-free race with debug_want_extra_isize Greg Kroah-Hartman
2020-01-22  9:28 ` [PATCH 4.4 16/76] ext4: add more paranoia checking in ext4_expand_extra_isize handling Greg Kroah-Hartman
2020-01-22  9:28 ` [PATCH 4.4 17/76] dccp: Fix memleak in __feat_register_sp Greg Kroah-Hartman
2020-01-22  9:28 ` [PATCH 4.4 18/76] rtc: mt6397: fix alarm register overwrite Greg Kroah-Hartman
2020-01-22  9:28 ` [PATCH 4.4 19/76] iommu: Remove device link to group on failure Greg Kroah-Hartman
2020-01-22  9:28 ` [PATCH 4.4 20/76] gpio: Fix error message on out-of-range GPIO in lookup table Greg Kroah-Hartman
2020-01-22  9:28 ` [PATCH 4.4 21/76] hsr: reset network header when supervision frame is created Greg Kroah-Hartman
2020-01-22  9:28 ` [PATCH 4.4 22/76] cifs: Adjust indentation in smb2_open_file Greg Kroah-Hartman
2020-01-22  9:28 ` [PATCH 4.4 23/76] RDMA/srpt: Report the SCSI residual to the initiator Greg Kroah-Hartman
2020-01-22  9:28 ` [PATCH 4.4 24/76] scsi: enclosure: Fix stale device oops with hot replug Greg Kroah-Hartman
2020-01-22  9:28 ` [PATCH 4.4 25/76] scsi: sd: Clear sdkp->protection_type if disk is reformatted without PI Greg Kroah-Hartman
2020-01-22  9:28 ` [PATCH 4.4 26/76] platform/x86: asus-wmi: Fix keyboard brightness cannot be set to 0 Greg Kroah-Hartman
2020-01-22  9:28 ` [PATCH 4.4 27/76] iio: imu: adis16480: assign bias value only if operation succeeded Greg Kroah-Hartman
2020-01-22  9:28 ` [PATCH 4.4 28/76] mei: fix modalias documentation Greg Kroah-Hartman
2020-01-22  9:28 ` [PATCH 4.4 29/76] clk: samsung: exynos5420: Preserve CPU clocks configuration during suspend/resume Greg Kroah-Hartman
2020-01-22  9:28 ` [PATCH 4.4 30/76] compat_ioctl: handle SIOCOUTQNSD Greg Kroah-Hartman
2020-01-22  9:28 ` [PATCH 4.4 31/76] tty: serial: imx: use the sg count from dma_map_sg Greg Kroah-Hartman
2020-01-22  9:28 ` [PATCH 4.4 32/76] tty: serial: pch_uart: correct usage of dma_unmap_sg Greg Kroah-Hartman
2020-01-22  9:28 ` [PATCH 4.4 33/76] media: exynos4-is: Fix recursive locking in isp_video_release() Greg Kroah-Hartman
2020-01-22  9:28 ` [PATCH 4.4 34/76] spi: atmel: fix handling of cs_change set on non-last xfer Greg Kroah-Hartman
2020-01-22  9:28 ` [PATCH 4.4 35/76] rtlwifi: Remove unnecessary NULL check in rtl_regd_init Greg Kroah-Hartman
2020-01-22  9:28 ` [PATCH 4.4 36/76] rtc: msm6242: Fix reading of 10-hour digit Greg Kroah-Hartman
2020-01-22  9:28 ` [PATCH 4.4 37/76] rseq/selftests: Turn off timeout setting Greg Kroah-Hartman
2020-01-22  9:28 ` [PATCH 4.4 38/76] hexagon: work around compiler crash Greg Kroah-Hartman
2020-01-22  9:28 ` [PATCH 4.4 39/76] ocfs2: call journal flush to mark journal as empty after journal recovery when mount Greg Kroah-Hartman
2020-01-22  9:28 ` [PATCH 4.4 40/76] ALSA: seq: Fix racy access for queue timer in proc read Greg Kroah-Hartman
2020-01-22  9:28 ` [PATCH 4.4 41/76] Fix built-in early-load Intel microcode alignment Greg Kroah-Hartman
2020-01-22  9:28 ` [PATCH 4.4 42/76] block: fix an integer overflow in logical block size Greg Kroah-Hartman
2020-01-22  9:28 ` [PATCH 4.4 43/76] USB: serial: simple: Add Motorola Solutions TETRA MTP3xxx and MTP85xx Greg Kroah-Hartman
2020-01-22  9:29 ` [PATCH 4.4 44/76] USB: serial: opticon: fix control-message timeouts Greg Kroah-Hartman
2020-01-22  9:29 ` [PATCH 4.4 45/76] USB: serial: suppress driver bind attributes Greg Kroah-Hartman
2020-01-22  9:29 ` [PATCH 4.4 46/76] USB: serial: ch341: handle unbound port at reset_resume Greg Kroah-Hartman
2020-01-22  9:29 ` [PATCH 4.4 47/76] USB: serial: io_edgeport: add missing active-port sanity check Greg Kroah-Hartman
2020-01-22  9:29 ` [PATCH 4.4 48/76] USB: serial: quatech2: handle unbound ports Greg Kroah-Hartman
2020-01-22  9:29 ` [PATCH 4.4 49/76] scsi: mptfusion: Fix double fetch bug in ioctl Greg Kroah-Hartman
2020-01-22  9:29 ` [PATCH 4.4 50/76] usb: core: hub: Improved device recognition on remote wakeup Greg Kroah-Hartman
2020-01-22  9:29 ` [PATCH 4.4 51/76] x86/efistub: Disable paging at mixed mode entry Greg Kroah-Hartman
2020-01-22  9:29 ` [PATCH 4.4 52/76] mm/page-writeback.c: avoid potential division by zero in wb_min_max_ratio() Greg Kroah-Hartman
2020-01-22  9:29 ` [PATCH 4.4 53/76] net: stmmac: 16KB buffer must be 16 byte aligned Greg Kroah-Hartman
2020-01-22  9:29 ` [PATCH 4.4 54/76] net: stmmac: Enable 16KB buffer size Greg Kroah-Hartman
2020-01-22  9:29 ` [PATCH 4.4 55/76] USB: serial: io_edgeport: use irqsave() in USBs complete callback Greg Kroah-Hartman
2020-01-22  9:29 ` [PATCH 4.4 56/76] USB: serial: io_edgeport: handle unbound ports on URB completion Greg Kroah-Hartman
2020-01-22  9:29 ` [PATCH 4.4 57/76] USB: serial: keyspan: handle unbound ports Greg Kroah-Hartman
2020-01-22  9:29 ` [PATCH 4.4 58/76] scsi: fnic: use kernels %pM format option to print MAC Greg Kroah-Hartman
2020-01-22  9:29 ` [PATCH 4.4 59/76] scsi: fnic: fix invalid stack access Greg Kroah-Hartman
2020-01-22  9:29 ` [PATCH 4.4 60/76] arm64: dts: agilex/stratix10: fix pmu interrupt numbers Greg Kroah-Hartman
2020-01-22  9:29 ` [PATCH 4.4 61/76] netfilter: fix a use-after-free in mtype_destroy() Greg Kroah-Hartman
2020-01-22  9:29 ` [PATCH 4.4 62/76] batman-adv: Fix DAT candidate selection on little endian systems Greg Kroah-Hartman
2020-01-22  9:29 ` [PATCH 4.4 63/76] macvlan: use skb_reset_mac_header() in macvlan_queue_xmit() Greg Kroah-Hartman
2020-01-22  9:29 ` [PATCH 4.4 64/76] r8152: add missing endpoint sanity check Greg Kroah-Hartman
2020-01-22  9:29 ` [PATCH 4.4 65/76] tcp: fix marked lost packets not being retransmitted Greg Kroah-Hartman
2020-01-22  9:29 ` [PATCH 4.4 66/76] net: usb: lan78xx: limit size of local TSO packets Greg Kroah-Hartman
2020-01-22  9:29 ` [PATCH 4.4 67/76] xen/blkfront: Adjust indentation in xlvbd_alloc_gendisk Greg Kroah-Hartman
2020-01-22  9:29 ` [PATCH 4.4 68/76] cw1200: Fix a signedness bug in cw1200_load_firmware() Greg Kroah-Hartman
2020-01-22  9:29 ` [PATCH 4.4 69/76] cfg80211: check for set_wiphy_params Greg Kroah-Hartman
2020-01-22  9:29 ` [PATCH 4.4 70/76] scsi: esas2r: unlock on error in esas2r_nvram_read_direct() Greg Kroah-Hartman
2020-01-22  9:29 ` [PATCH 4.4 71/76] scsi: qla4xxx: fix double free bug Greg Kroah-Hartman
2020-01-22  9:29 ` [PATCH 4.4 72/76] scsi: bnx2i: fix potential use after free Greg Kroah-Hartman
2020-01-22  9:29 ` [PATCH 4.4 73/76] scsi: target: core: Fix a pr_debug() argument Greg Kroah-Hartman
2020-01-22  9:29 ` [PATCH 4.4 74/76] scsi: core: scsi_trace: Use get_unaligned_be*() Greg Kroah-Hartman
2020-01-22  9:29 ` [PATCH 4.4 75/76] perf probe: Fix wrong address verification Greg Kroah-Hartman
2020-01-22  9:29 ` [PATCH 4.4 76/76] regulator: ab8500: Remove SYSCLKREQ from enum ab8505_regulator_id Greg Kroah-Hartman
2020-01-22 14:57 ` [PATCH 4.4 00/76] 4.4.211-stable review Jon Hunter
2020-01-22 18:59 ` Guenter Roeck
2020-01-22 20:21 ` Naresh Kamboju
2020-01-22 20:52 ` shuah

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=20200122092752.433884919@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=ben.hutchings@codethink.co.uk \
    --cc=chunkeey@gmail.com \
    --cc=kvalo@codeaurora.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=stern@rowland.harvard.edu \
    --cc=syzbot+200d4bb11b23d929335f@syzkaller.appspotmail.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).