linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: gregkh@linuxfoundation.org
To: linux-kernel@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	stable@vger.kernel.org,
	syzbot <syzbot+a93fba6d384346a761e3@syzkaller.appspotmail.com>,
	syzbot <syzbot+bf1a360e305ee719e364@syzkaller.appspotmail.com>,
	syzbot <syzbot+95ce4b142579611ef0a9@syzkaller.appspotmail.com>,
	Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>,
	Shuah Khan <skhan@linuxfoundation.org>
Subject: [PATCH 4.14 64/95] usbip: fix vhci_hcd attach_store() races leading to gpf
Date: Mon, 15 Mar 2021 14:57:34 +0100	[thread overview]
Message-ID: <20210315135742.368685136@linuxfoundation.org> (raw)
In-Reply-To: <20210315135740.245494252@linuxfoundation.org>

From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

From: Shuah Khan <skhan@linuxfoundation.org>

commit 718ad9693e3656120064b715fe931f43a6201e67 upstream.

attach_store() is invoked when user requests import (attach) a device
from usbip host.

Attach and detach are governed by local state and shared state
- Shared state (usbip device status) - Device status is used to manage
  the attach and detach operations on import-able devices.
- Local state (tcp_socket, rx and tx thread task_struct ptrs)
  A valid tcp_socket controls rx and tx thread operations while the
  device is in exported state.
- Device has to be in the right state to be attached and detached.

Attach sequence includes validating the socket and creating receive (rx)
and transmit (tx) threads to talk to the host to get access to the
imported device. rx and tx threads depends on local and shared state to
be correct and in sync.

Detach sequence shuts the socket down and stops the rx and tx threads.
Detach sequence relies on local and shared states to be in sync.

There are races in updating the local and shared status in the current
attach sequence resulting in crashes. These stem from starting rx and
tx threads before local and global state is updated correctly to be in
sync.

1. Doesn't handle kthread_create() error and saves invalid ptr in local
   state that drives rx and tx threads.
2. Updates tcp_socket and sockfd,  starts stub_rx and stub_tx threads
   before updating usbip_device status to VDEV_ST_NOTASSIGNED. This opens
   up a race condition between the threads, port connect, and detach
   handling.

Fix the above problems:
- Stop using kthread_get_run() macro to create/start threads.
- Create threads and get task struct reference.
- Add kthread_create() failure handling and bail out.
- Hold vhci and usbip_device locks to update local and shared states after
  creating rx and tx threads.
- Update usbip_device status to VDEV_ST_NOTASSIGNED.
- Update usbip_device tcp_socket, sockfd, tcp_rx, and tcp_tx
- Start threads after usbip_device (tcp_socket, sockfd, tcp_rx, tcp_tx,
  and status) is complete.

Credit goes to syzbot and Tetsuo Handa for finding and root-causing the
kthread_get_run() improper error handling problem and others. This is
hard problem to find and debug since the races aren't seen in a normal
case. Fuzzing forces the race window to be small enough for the
kthread_get_run() error path bug and starting threads before updating the
local and shared state bug in the attach sequence.
- Update usbip_device tcp_rx and tcp_tx pointers holding vhci and
  usbip_device locks.

Tested with syzbot reproducer:
- https://syzkaller.appspot.com/text?tag=ReproC&x=14801034d00000

Fixes: 9720b4bc76a83807 ("staging/usbip: convert to kthread")
Cc: stable@vger.kernel.org
Reported-by: syzbot <syzbot+a93fba6d384346a761e3@syzkaller.appspotmail.com>
Reported-by: syzbot <syzbot+bf1a360e305ee719e364@syzkaller.appspotmail.com>
Reported-by: syzbot <syzbot+95ce4b142579611ef0a9@syzkaller.appspotmail.com>
Reported-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
Link: https://lore.kernel.org/r/bb434bd5d7a64fbec38b5ecfb838a6baef6eb12b.1615171203.git.skhan@linuxfoundation.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
 drivers/usb/usbip/vhci_sysfs.c |   29 +++++++++++++++++++++++++----
 1 file changed, 25 insertions(+), 4 deletions(-)

--- a/drivers/usb/usbip/vhci_sysfs.c
+++ b/drivers/usb/usbip/vhci_sysfs.c
@@ -326,6 +326,8 @@ static ssize_t store_attach(struct devic
 	struct vhci *vhci;
 	int err;
 	unsigned long flags;
+	struct task_struct *tcp_rx = NULL;
+	struct task_struct *tcp_tx = NULL;
 
 	/*
 	 * @rhport: port number of vhci_hcd
@@ -374,9 +376,24 @@ static ssize_t store_attach(struct devic
 		return -EINVAL;
 	}
 
-	/* now need lock until setting vdev status as used */
+	/* create threads before locking */
+	tcp_rx = kthread_create(vhci_rx_loop, &vdev->ud, "vhci_rx");
+	if (IS_ERR(tcp_rx)) {
+		sockfd_put(socket);
+		return -EINVAL;
+	}
+	tcp_tx = kthread_create(vhci_tx_loop, &vdev->ud, "vhci_tx");
+	if (IS_ERR(tcp_tx)) {
+		kthread_stop(tcp_rx);
+		sockfd_put(socket);
+		return -EINVAL;
+	}
+
+	/* get task structs now */
+	get_task_struct(tcp_rx);
+	get_task_struct(tcp_tx);
 
-	/* begin a lock */
+	/* now begin lock until setting vdev status set */
 	spin_lock_irqsave(&vhci->lock, flags);
 	spin_lock(&vdev->ud.lock);
 
@@ -386,6 +403,8 @@ static ssize_t store_attach(struct devic
 		spin_unlock_irqrestore(&vhci->lock, flags);
 
 		sockfd_put(socket);
+		kthread_stop_put(tcp_rx);
+		kthread_stop_put(tcp_tx);
 
 		dev_err(dev, "port %d already used\n", rhport);
 		/*
@@ -404,14 +423,16 @@ static ssize_t store_attach(struct devic
 	vdev->speed         = speed;
 	vdev->ud.sockfd     = sockfd;
 	vdev->ud.tcp_socket = socket;
+	vdev->ud.tcp_rx     = tcp_rx;
+	vdev->ud.tcp_tx     = tcp_tx;
 	vdev->ud.status     = VDEV_ST_NOTASSIGNED;
 
 	spin_unlock(&vdev->ud.lock);
 	spin_unlock_irqrestore(&vhci->lock, flags);
 	/* end the lock */
 
-	vdev->ud.tcp_rx = kthread_get_run(vhci_rx_loop, &vdev->ud, "vhci_rx");
-	vdev->ud.tcp_tx = kthread_get_run(vhci_tx_loop, &vdev->ud, "vhci_tx");
+	wake_up_process(vdev->ud.tcp_rx);
+	wake_up_process(vdev->ud.tcp_tx);
 
 	rh_port_connect(vdev, speed);
 



  parent reply	other threads:[~2021-03-15 14:24 UTC|newest]

Thread overview: 105+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-15 13:56 [PATCH 4.14 00/95] 4.14.226-rc1 review gregkh
2021-03-15 13:56 ` [PATCH 4.14 01/95] uapi: nfnetlink_cthelper.h: fix userspace compilation error gregkh
2021-03-15 13:56 ` [PATCH 4.14 02/95] ethernet: alx: fix order of calls on resume gregkh
2021-03-15 13:56 ` [PATCH 4.14 03/95] ath9k: fix transmitting to stations in dynamic SMPS mode gregkh
2021-03-15 13:56 ` [PATCH 4.14 04/95] net: Fix gro aggregation for udp encaps with zero csum gregkh
2021-03-15 13:56 ` [PATCH 4.14 05/95] net: Introduce parse_protocol header_ops callback gregkh
2021-03-15 13:56 ` [PATCH 4.14 06/95] net: check if protocol extracted by virtio_net_hdr_set_proto is correct gregkh
2021-03-15 13:56 ` [PATCH 4.14 07/95] net: avoid infinite loop in mpls_gso_segment when mpls_hlen == 0 gregkh
2021-03-15 13:56 ` [PATCH 4.14 08/95] can: skb: can_skb_set_owner(): fix ref counting if socket was closed before setting skb ownership gregkh
2021-03-15 13:56 ` [PATCH 4.14 09/95] can: flexcan: assert FRZ bit in flexcan_chip_freeze() gregkh
2021-03-15 13:56 ` [PATCH 4.14 10/95] can: flexcan: enable RX FIFO after FRZ/HALT valid gregkh
2021-03-15 13:56 ` [PATCH 4.14 11/95] netfilter: x_tables: gpf inside xt_find_revision() gregkh
2021-03-15 13:56 ` [PATCH 4.14 12/95] cifs: return proper error code in statfs(2) gregkh
2021-03-15 13:56 ` [PATCH 4.14 13/95] scripts/recordmcount.{c,pl}: support -ffunction-sections .text.* section names gregkh
2021-03-15 13:56 ` [PATCH 4.14 14/95] Revert "mm, slub: consider rest of partial list if acquire_slab() fails" gregkh
2021-03-15 13:56 ` [PATCH 4.14 15/95] sh_eth: fix TRSCER mask for SH771x gregkh
2021-03-15 13:56 ` [PATCH 4.14 16/95] net/mlx4_en: update moderation when config reset gregkh
2021-03-15 13:56 ` [PATCH 4.14 17/95] net: stmmac: fix incorrect DMA channel intr enable setting of EQoS v4.10 gregkh
2021-03-15 13:56 ` [PATCH 4.14 18/95] net: sched: avoid duplicates in classes dump gregkh
2021-03-15 13:56 ` [PATCH 4.14 19/95] net: usb: qmi_wwan: allow qmimux add/del with master up gregkh
2021-03-15 13:56 ` [PATCH 4.14 20/95] cipso,calipso: resolve a number of problems with the DOI refcounts gregkh
2021-03-15 13:56 ` [PATCH 4.14 21/95] net: lapbether: Remove netif_start_queue / netif_stop_queue gregkh
2021-03-15 13:56 ` [PATCH 4.14 22/95] net: davicom: Fix regulator not turned off on failed probe gregkh
2021-03-15 13:56 ` [PATCH 4.14 23/95] net: davicom: Fix regulator not turned off on driver removal gregkh
2021-03-15 13:56 ` [PATCH 4.14 24/95] net: stmmac: stop each tx channel independently gregkh
2021-03-15 13:56 ` [PATCH 4.14 25/95] perf traceevent: Ensure read cmdlines are null terminated gregkh
2021-03-15 13:56 ` [PATCH 4.14 26/95] s390/cio: return -EFAULT if copy_to_user() fails again gregkh
2021-03-15 13:56 ` [PATCH 4.14 27/95] drm/compat: Clear bounce structures gregkh
2021-03-15 13:56 ` [PATCH 4.14 28/95] drm: meson_drv add shutdown function gregkh
2021-03-15 13:56 ` [PATCH 4.14 29/95] s390/cio: return -EFAULT if copy_to_user() fails gregkh
2021-03-15 13:57 ` [PATCH 4.14 30/95] media: usbtv: Fix deadlock on suspend gregkh
2021-03-15 13:57 ` [PATCH 4.14 31/95] net: phy: fix save wrong speed and duplex problem if autoneg is on gregkh
2021-03-15 13:57 ` [PATCH 4.14 32/95] udf: fix silent AED tagLocation corruption gregkh
2021-03-15 13:57 ` [PATCH 4.14 33/95] mmc: mxs-mmc: Fix a resource leak in an error handling path in mxs_mmc_probe() gregkh
2021-03-15 13:57 ` [PATCH 4.14 34/95] mmc: mediatek: fix race condition between msdc_request_timeout and irq gregkh
2021-03-15 13:57 ` [PATCH 4.14 35/95] powerpc: improve handling of unrecoverable system reset gregkh
2021-03-15 13:57 ` [PATCH 4.14 36/95] powerpc/perf: Record counter overflow always if SAMPLE_IP is unset gregkh
2021-03-15 13:57 ` [PATCH 4.14 37/95] PCI: xgene-msi: Fix race in installing chained irq handler gregkh
2021-03-15 13:57 ` [PATCH 4.14 38/95] PCI: mediatek: Add missing of_node_put() to fix reference leak gregkh
2021-03-15 13:57 ` [PATCH 4.14 39/95] s390/smp: __smp_rescan_cpus() - move cpumask away from stack gregkh
2021-03-15 13:57 ` [PATCH 4.14 40/95] scsi: libiscsi: Fix iscsi_prep_scsi_cmd_pdu() error handling gregkh
2021-03-15 13:57 ` [PATCH 4.14 41/95] ALSA: hda/hdmi: Cancel pending works before suspend gregkh
2021-03-15 13:57 ` [PATCH 4.14 42/95] ALSA: hda: Drop the BATCH workaround for AMD controllers gregkh
2021-03-15 13:57 ` [PATCH 4.14 43/95] ALSA: hda: Avoid spurious unsol event handling during S3/S4 gregkh
2021-03-15 13:57 ` [PATCH 4.14 44/95] ALSA: usb-audio: Fix "cannot get freq eq" errors on Dell AE515 sound bar gregkh
2021-03-15 13:57 ` [PATCH 4.14 45/95] Revert 95ebabde382c ("capabilities: Dont allow writing ambiguous v3 file capabilities") gregkh
2021-03-15 13:57 ` [PATCH 4.14 46/95] s390/dasd: fix hanging DASD driver unbind gregkh
2021-03-15 13:57 ` [PATCH 4.14 47/95] s390/dasd: fix hanging IO request during " gregkh
2021-03-15 13:57 ` [PATCH 4.14 48/95] mmc: core: Fix partition switch time for eMMC gregkh
2021-03-15 13:57 ` [PATCH 4.14 49/95] Goodix Fingerprint device is not a modem gregkh
2021-03-15 13:57 ` [PATCH 4.14 50/95] USB: gadget: u_ether: Fix a configfs return code gregkh
2021-03-15 13:57 ` [PATCH 4.14 51/95] usb: gadget: f_uac2: always increase endpoint max_packet_size by one audio slot gregkh
2021-03-15 13:57 ` [PATCH 4.14 52/95] usb: gadget: f_uac1: stop playback on function disable gregkh
2021-03-15 13:57 ` [PATCH 4.14 53/95] usb: renesas_usbhs: Clear PIPECFG for re-enabling pipe with other EPNUM gregkh
2021-03-15 13:57 ` [PATCH 4.14 54/95] xhci: Improve detection of device initiated wake signal gregkh
2021-03-15 13:57 ` [PATCH 4.14 55/95] usb: xhci: Fix ASMedia ASM1042A and ASM3242 DMA addressing gregkh
2021-03-15 13:57 ` [PATCH 4.14 56/95] USB: serial: io_edgeport: fix memory leak in edge_startup gregkh
2021-03-15 13:57 ` [PATCH 4.14 57/95] USB: serial: ch341: add new Product ID gregkh
2021-03-15 13:57 ` [PATCH 4.14 58/95] USB: serial: cp210x: add ID for Acuity Brands nLight Air Adapter gregkh
2021-03-15 13:57 ` [PATCH 4.14 59/95] USB: serial: cp210x: add some more GE USB IDs gregkh
2021-03-15 13:57 ` [PATCH 4.14 60/95] usbip: fix stub_dev to check for stream socket gregkh
2021-03-15 13:57 ` [PATCH 4.14 61/95] usbip: fix vhci_hcd " gregkh
2021-03-15 13:57 ` [PATCH 4.14 62/95] usbip: fix vudc " gregkh
2021-03-15 13:57 ` [PATCH 4.14 63/95] usbip: fix stub_dev usbip_sockfd_store() races leading to gpf gregkh
2021-03-15 13:57 ` gregkh [this message]
2021-03-15 13:57 ` [PATCH 4.14 65/95] staging: rtl8192u: fix ->ssid overflow in r8192_wx_set_scan() gregkh
2021-03-15 13:57 ` [PATCH 4.14 66/95] staging: rtl8188eu: prevent ->ssid overflow in rtw_wx_set_scan() gregkh
2021-03-15 13:57 ` [PATCH 4.14 67/95] staging: rtl8712: unterminated string leads to read overflow gregkh
2021-03-15 13:57 ` [PATCH 4.14 68/95] staging: rtl8188eu: fix potential memory corruption in rtw_check_beacon_data() gregkh
2021-03-15 13:57 ` [PATCH 4.14 69/95] staging: ks7010: prevent buffer overflow in ks_wlan_set_scan() gregkh
2021-03-15 13:57 ` [PATCH 4.14 70/95] staging: rtl8712: Fix possible buffer overflow in r8712_sitesurvey_cmd gregkh
2021-03-15 13:57 ` [PATCH 4.14 71/95] staging: rtl8192e: Fix possible buffer overflow in _rtl92e_wx_set_scan gregkh
2021-03-15 13:57 ` [PATCH 4.14 72/95] staging: comedi: addi_apci_1032: Fix endian problem for COS sample gregkh
2021-03-15 13:57 ` [PATCH 4.14 73/95] staging: comedi: addi_apci_1500: Fix endian problem for command sample gregkh
2021-03-15 13:57 ` [PATCH 4.14 74/95] staging: comedi: adv_pci1710: Fix endian problem for AI command data gregkh
2021-03-15 13:57 ` [PATCH 4.14 75/95] staging: comedi: das6402: " gregkh
2021-03-15 13:57 ` [PATCH 4.14 76/95] staging: comedi: das800: " gregkh
2021-03-15 13:57 ` [PATCH 4.14 77/95] staging: comedi: dmm32at: " gregkh
2021-03-15 13:57 ` [PATCH 4.14 78/95] staging: comedi: me4000: " gregkh
2021-03-15 13:57 ` [PATCH 4.14 79/95] staging: comedi: pcl711: " gregkh
2021-03-15 13:57 ` [PATCH 4.14 80/95] staging: comedi: pcl818: " gregkh
2021-03-15 13:57 ` [PATCH 4.14 81/95] sh_eth: fix TRSCER mask for R7S72100 gregkh
2021-03-15 13:57 ` [PATCH 4.14 82/95] NFSv4.2: fix return value of _nfs4_get_security_label() gregkh
2021-03-15 13:57 ` [PATCH 4.14 83/95] block: rsxx: fix error return code of rsxx_pci_probe() gregkh
2021-03-15 13:57 ` [PATCH 4.14 84/95] configfs: fix a use-after-free in __configfs_open_file gregkh
2021-03-15 13:57 ` [PATCH 4.14 85/95] stop_machine: mark helpers __always_inline gregkh
2021-03-15 13:57 ` [PATCH 4.14 86/95] include/linux/sched/mm.h: use rcu_dereference in in_vfork() gregkh
2021-03-15 13:57 ` [PATCH 4.14 87/95] prctl: fix PR_SET_MM_AUXV kernel stack leak gregkh
2021-03-15 13:57 ` [PATCH 4.14 88/95] powerpc/64s: Fix instruction encoding for lis in ppc_function_entry() gregkh
2021-03-15 13:57 ` [PATCH 4.14 89/95] binfmt_misc: fix possible deadlock in bm_register_write gregkh
2021-03-15 13:58 ` [PATCH 4.14 90/95] hwmon: (lm90) Fix max6658 sporadic wrong temperature reading gregkh
2021-03-15 13:58 ` [PATCH 4.14 91/95] KVM: arm64: Fix exclusive limit for IPA size gregkh
2021-03-15 13:58 ` [PATCH 4.14 92/95] iio: imu: adis16400: release allocated memory on failure gregkh
2021-03-15 13:58 ` [PATCH 4.14 93/95] xen/events: reset affinity of 2-level event when tearing it down gregkh
2021-03-15 13:58 ` [PATCH 4.14 94/95] xen/events: dont unmask an event channel when an eoi is pending gregkh
2021-03-15 13:58 ` [PATCH 4.14 95/95] xen/events: avoid handling the same event on two cpus at the same time gregkh
2021-03-15 21:30 ` [PATCH 4.14 00/95] 4.14.226-rc1 review Guenter Roeck
2021-03-15 22:57 ` Jason Self
2021-03-16  6:35 ` Samuel Zou
2021-03-17 15:36   ` Greg KH
2021-03-18  5:54     ` Samuel Zou
2021-03-18  6:04       ` Greg KH
2021-03-18  6:09       ` Guenter Roeck
2021-03-16 11:54 ` Naresh Kamboju
2021-03-18 12:00 ` Samuel Zou

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=20210315135742.368685136@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=penguin-kernel@I-love.SAKURA.ne.jp \
    --cc=skhan@linuxfoundation.org \
    --cc=stable@vger.kernel.org \
    --cc=syzbot+95ce4b142579611ef0a9@syzkaller.appspotmail.com \
    --cc=syzbot+a93fba6d384346a761e3@syzkaller.appspotmail.com \
    --cc=syzbot+bf1a360e305ee719e364@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).