2017-11-03 18:28:30 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2008-07-10 04:56:51 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2003-2008 Takahiro Hirofuchi
|
2016-06-13 10:33:40 +08:00
|
|
|
* Copyright (C) 2015-2016 Nobuo Iwata
|
2008-07-10 04:56:51 +08:00
|
|
|
*/
|
|
|
|
|
2011-05-12 13:33:43 +08:00
|
|
|
#include <linux/kthread.h>
|
2012-10-22 03:45:00 +08:00
|
|
|
#include <linux/file.h>
|
2011-05-12 13:33:43 +08:00
|
|
|
#include <linux/net.h>
|
2016-06-13 10:33:40 +08:00
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/slab.h>
|
2011-05-12 13:33:43 +08:00
|
|
|
|
2018-05-19 09:13:42 +08:00
|
|
|
/* Hardening for Spectre-v1 */
|
|
|
|
#include <linux/nospec.h>
|
|
|
|
|
2008-07-10 04:56:51 +08:00
|
|
|
#include "usbip_common.h"
|
|
|
|
#include "vhci.h"
|
|
|
|
|
|
|
|
/* TODO: refine locking ?*/
|
|
|
|
|
2017-06-08 13:04:10 +08:00
|
|
|
/*
|
|
|
|
* output example:
|
2018-01-19 08:25:30 +08:00
|
|
|
* hub port sta spd dev sockfd local_busid
|
|
|
|
* hs 0000 004 000 00000000 000003 1-2.3
|
2017-06-08 13:04:10 +08:00
|
|
|
* ................................................
|
2018-01-19 08:25:30 +08:00
|
|
|
* ss 0008 004 000 00000000 000004 2-3.4
|
2017-06-08 13:04:10 +08:00
|
|
|
* ................................................
|
|
|
|
*
|
2017-12-08 05:16:49 +08:00
|
|
|
* Output includes socket fd instead of socket pointer address to avoid
|
|
|
|
* leaking kernel memory address in:
|
|
|
|
* /sys/devices/platform/vhci_hcd.0/status and in debug output.
|
|
|
|
* The socket pointer address is not used at the moment and it was made
|
|
|
|
* visible as a convenient way to find IP address from socket pointer
|
|
|
|
* address by looking up /proc/net/{tcp,tcp6}. As this opens a security
|
|
|
|
* hole, the change is made to use sockfd instead.
|
|
|
|
*
|
2017-06-08 13:04:10 +08:00
|
|
|
*/
|
|
|
|
static void port_show_vhci(char **out, int hub, int port, struct vhci_device *vdev)
|
|
|
|
{
|
|
|
|
if (hub == HUB_SPEED_HIGH)
|
|
|
|
*out += sprintf(*out, "hs %04u %03u ",
|
|
|
|
port, vdev->ud.status);
|
|
|
|
else /* hub == HUB_SPEED_SUPER */
|
|
|
|
*out += sprintf(*out, "ss %04u %03u ",
|
|
|
|
port, vdev->ud.status);
|
|
|
|
|
|
|
|
if (vdev->ud.status == VDEV_ST_USED) {
|
|
|
|
*out += sprintf(*out, "%03u %08x ",
|
|
|
|
vdev->speed, vdev->devid);
|
2018-01-19 08:25:30 +08:00
|
|
|
*out += sprintf(*out, "%06u %s",
|
2017-12-08 05:16:49 +08:00
|
|
|
vdev->ud.sockfd,
|
2017-06-08 13:04:10 +08:00
|
|
|
dev_name(&vdev->udev->dev));
|
|
|
|
|
|
|
|
} else {
|
|
|
|
*out += sprintf(*out, "000 00000000 ");
|
2018-01-19 08:25:30 +08:00
|
|
|
*out += sprintf(*out, "000000 0-0");
|
2017-06-08 13:04:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
*out += sprintf(*out, "\n");
|
|
|
|
}
|
|
|
|
|
2008-07-10 04:56:51 +08:00
|
|
|
/* Sysfs entry to show port status */
|
2016-06-13 10:33:40 +08:00
|
|
|
static ssize_t status_show_vhci(int pdev_nr, char *out)
|
2008-07-10 04:56:51 +08:00
|
|
|
{
|
2017-06-08 13:04:07 +08:00
|
|
|
struct platform_device *pdev = vhcis[pdev_nr].pdev;
|
2017-06-08 13:04:09 +08:00
|
|
|
struct vhci *vhci;
|
|
|
|
struct usb_hcd *hcd;
|
|
|
|
struct vhci_hcd *vhci_hcd;
|
2008-07-10 04:56:51 +08:00
|
|
|
char *s = out;
|
2017-06-08 13:04:09 +08:00
|
|
|
int i;
|
2016-02-03 01:36:39 +08:00
|
|
|
unsigned long flags;
|
2008-07-10 04:56:51 +08:00
|
|
|
|
2016-06-13 10:33:40 +08:00
|
|
|
if (!pdev || !out) {
|
|
|
|
usbip_dbg_vhci_sysfs("show status error\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-06-08 13:04:09 +08:00
|
|
|
hcd = platform_get_drvdata(pdev);
|
|
|
|
vhci_hcd = hcd_to_vhci_hcd(hcd);
|
|
|
|
vhci = vhci_hcd->vhci;
|
2008-07-10 04:56:51 +08:00
|
|
|
|
2016-06-13 10:33:40 +08:00
|
|
|
spin_lock_irqsave(&vhci->lock, flags);
|
2008-07-10 04:56:51 +08:00
|
|
|
|
2016-06-13 10:33:40 +08:00
|
|
|
for (i = 0; i < VHCI_HC_PORTS; i++) {
|
2017-06-08 13:04:10 +08:00
|
|
|
struct vhci_device *vdev = &vhci->vhci_hcd_hs->vdev[i];
|
2008-07-10 04:56:51 +08:00
|
|
|
|
|
|
|
spin_lock(&vdev->ud.lock);
|
2017-06-08 13:04:10 +08:00
|
|
|
port_show_vhci(&out, HUB_SPEED_HIGH,
|
2017-06-08 13:04:13 +08:00
|
|
|
pdev_nr * VHCI_PORTS + i, vdev);
|
2017-06-08 13:04:10 +08:00
|
|
|
spin_unlock(&vdev->ud.lock);
|
|
|
|
}
|
2008-07-10 04:56:51 +08:00
|
|
|
|
2017-06-08 13:04:10 +08:00
|
|
|
for (i = 0; i < VHCI_HC_PORTS; i++) {
|
|
|
|
struct vhci_device *vdev = &vhci->vhci_hcd_ss->vdev[i];
|
|
|
|
|
|
|
|
spin_lock(&vdev->ud.lock);
|
|
|
|
port_show_vhci(&out, HUB_SPEED_SUPER,
|
2017-06-08 13:04:13 +08:00
|
|
|
pdev_nr * VHCI_PORTS + VHCI_HC_PORTS + i, vdev);
|
2008-07-10 04:56:51 +08:00
|
|
|
spin_unlock(&vdev->ud.lock);
|
|
|
|
}
|
|
|
|
|
2016-06-13 10:33:40 +08:00
|
|
|
spin_unlock_irqrestore(&vhci->lock, flags);
|
|
|
|
|
|
|
|
return out - s;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t status_show_not_ready(int pdev_nr, char *out)
|
|
|
|
{
|
|
|
|
char *s = out;
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < VHCI_HC_PORTS; i++) {
|
2017-06-08 13:04:10 +08:00
|
|
|
out += sprintf(out, "hs %04u %03u ",
|
2017-06-08 13:04:13 +08:00
|
|
|
(pdev_nr * VHCI_PORTS) + i,
|
2017-06-08 13:04:10 +08:00
|
|
|
VDEV_ST_NOTASSIGNED);
|
|
|
|
out += sprintf(out, "000 00000000 0000000000000000 0-0");
|
|
|
|
out += sprintf(out, "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < VHCI_HC_PORTS; i++) {
|
|
|
|
out += sprintf(out, "ss %04u %03u ",
|
2017-06-08 13:04:13 +08:00
|
|
|
(pdev_nr * VHCI_PORTS) + VHCI_HC_PORTS + i,
|
2016-06-13 10:33:40 +08:00
|
|
|
VDEV_ST_NOTASSIGNED);
|
|
|
|
out += sprintf(out, "000 00000000 0000000000000000 0-0");
|
|
|
|
out += sprintf(out, "\n");
|
|
|
|
}
|
|
|
|
return out - s;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int status_name_to_id(const char *name)
|
|
|
|
{
|
|
|
|
char *c;
|
|
|
|
long val;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
c = strchr(name, '.');
|
|
|
|
if (c == NULL)
|
|
|
|
return 0;
|
2008-07-10 04:56:51 +08:00
|
|
|
|
2016-06-13 10:33:40 +08:00
|
|
|
ret = kstrtol(c+1, 10, &val);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t status_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *out)
|
|
|
|
{
|
|
|
|
char *s = out;
|
|
|
|
int pdev_nr;
|
|
|
|
|
|
|
|
out += sprintf(out,
|
2018-01-19 08:25:30 +08:00
|
|
|
"hub port sta spd dev sockfd local_busid\n");
|
2016-06-13 10:33:40 +08:00
|
|
|
|
|
|
|
pdev_nr = status_name_to_id(attr->attr.name);
|
|
|
|
if (pdev_nr < 0)
|
|
|
|
out += status_show_not_ready(pdev_nr, out);
|
|
|
|
else
|
|
|
|
out += status_show_vhci(pdev_nr, out);
|
|
|
|
|
|
|
|
return out - s;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t nports_show(struct device *dev, struct device_attribute *attr,
|
|
|
|
char *out)
|
|
|
|
{
|
|
|
|
char *s = out;
|
|
|
|
|
2017-06-08 13:04:10 +08:00
|
|
|
/*
|
2017-12-08 05:16:49 +08:00
|
|
|
* Half the ports are for SPEED_HIGH and half for SPEED_SUPER,
|
|
|
|
* thus the * 2.
|
2017-06-08 13:04:10 +08:00
|
|
|
*/
|
2017-06-08 13:04:13 +08:00
|
|
|
out += sprintf(out, "%d\n", VHCI_PORTS * vhci_num_controllers);
|
2008-07-10 04:56:51 +08:00
|
|
|
return out - s;
|
|
|
|
}
|
2016-06-13 10:33:40 +08:00
|
|
|
static DEVICE_ATTR_RO(nports);
|
2008-07-10 04:56:51 +08:00
|
|
|
|
|
|
|
/* Sysfs entry to shutdown a virtual connection */
|
2017-06-08 13:04:09 +08:00
|
|
|
static int vhci_port_disconnect(struct vhci_hcd *vhci_hcd, __u32 rhport)
|
2008-07-10 04:56:51 +08:00
|
|
|
{
|
2017-06-08 13:04:09 +08:00
|
|
|
struct vhci_device *vdev = &vhci_hcd->vdev[rhport];
|
|
|
|
struct vhci *vhci = vhci_hcd->vhci;
|
2016-02-03 01:36:39 +08:00
|
|
|
unsigned long flags;
|
2008-07-10 04:56:51 +08:00
|
|
|
|
2009-07-21 14:46:13 +08:00
|
|
|
usbip_dbg_vhci_sysfs("enter\n");
|
2008-07-10 04:56:51 +08:00
|
|
|
|
2021-03-30 09:36:48 +08:00
|
|
|
mutex_lock(&vdev->ud.sysfs_lock);
|
|
|
|
|
2008-07-10 04:56:51 +08:00
|
|
|
/* lock */
|
2016-06-13 10:33:40 +08:00
|
|
|
spin_lock_irqsave(&vhci->lock, flags);
|
2008-07-10 04:56:51 +08:00
|
|
|
spin_lock(&vdev->ud.lock);
|
2016-06-13 10:33:40 +08:00
|
|
|
|
2008-07-10 04:56:51 +08:00
|
|
|
if (vdev->ud.status == VDEV_ST_NULL) {
|
2011-05-20 07:47:32 +08:00
|
|
|
pr_err("not connected %d\n", vdev->ud.status);
|
2008-07-10 04:56:51 +08:00
|
|
|
|
|
|
|
/* unlock */
|
|
|
|
spin_unlock(&vdev->ud.lock);
|
2016-06-13 10:33:40 +08:00
|
|
|
spin_unlock_irqrestore(&vhci->lock, flags);
|
2021-03-30 09:36:48 +08:00
|
|
|
mutex_unlock(&vdev->ud.sysfs_lock);
|
2008-07-10 04:56:51 +08:00
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* unlock */
|
|
|
|
spin_unlock(&vdev->ud.lock);
|
2016-06-13 10:33:40 +08:00
|
|
|
spin_unlock_irqrestore(&vhci->lock, flags);
|
2008-07-10 04:56:51 +08:00
|
|
|
|
|
|
|
usbip_event_add(&vdev->ud, VDEV_EVENT_DOWN);
|
|
|
|
|
2021-03-30 09:36:48 +08:00
|
|
|
mutex_unlock(&vdev->ud.sysfs_lock);
|
|
|
|
|
2008-07-10 04:56:51 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-05-19 09:13:42 +08:00
|
|
|
static int valid_port(__u32 *pdev_nr, __u32 *rhport)
|
2016-06-13 10:33:40 +08:00
|
|
|
{
|
2018-05-19 09:13:42 +08:00
|
|
|
if (*pdev_nr >= vhci_num_controllers) {
|
|
|
|
pr_err("pdev %u\n", *pdev_nr);
|
2016-06-13 10:33:40 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2018-05-19 09:13:42 +08:00
|
|
|
*pdev_nr = array_index_nospec(*pdev_nr, vhci_num_controllers);
|
|
|
|
|
|
|
|
if (*rhport >= VHCI_HC_PORTS) {
|
|
|
|
pr_err("rhport %u\n", *rhport);
|
2016-06-13 10:33:40 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2018-05-19 09:13:42 +08:00
|
|
|
*rhport = array_index_nospec(*rhport, VHCI_HC_PORTS);
|
|
|
|
|
2016-06-13 10:33:40 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-01-23 18:24:07 +08:00
|
|
|
static ssize_t detach_store(struct device *dev, struct device_attribute *attr,
|
2008-07-10 04:56:51 +08:00
|
|
|
const char *buf, size_t count)
|
|
|
|
{
|
2016-06-13 10:33:40 +08:00
|
|
|
__u32 port = 0, pdev_nr = 0, rhport = 0;
|
|
|
|
struct usb_hcd *hcd;
|
2017-06-08 13:04:10 +08:00
|
|
|
struct vhci_hcd *vhci_hcd;
|
2016-06-13 10:33:40 +08:00
|
|
|
int ret;
|
2008-07-10 04:56:51 +08:00
|
|
|
|
2016-06-13 10:33:40 +08:00
|
|
|
if (kstrtoint(buf, 10, &port) < 0)
|
2014-03-07 02:36:34 +08:00
|
|
|
return -EINVAL;
|
2008-07-10 04:56:51 +08:00
|
|
|
|
2016-06-13 10:33:40 +08:00
|
|
|
pdev_nr = port_to_pdev_nr(port);
|
|
|
|
rhport = port_to_rhport(port);
|
|
|
|
|
2018-05-19 09:13:42 +08:00
|
|
|
if (!valid_port(&pdev_nr, &rhport))
|
2008-07-10 04:56:51 +08:00
|
|
|
return -EINVAL;
|
2016-06-13 10:33:40 +08:00
|
|
|
|
2017-06-08 13:04:07 +08:00
|
|
|
hcd = platform_get_drvdata(vhcis[pdev_nr].pdev);
|
2016-06-13 10:33:40 +08:00
|
|
|
if (hcd == NULL) {
|
|
|
|
dev_err(dev, "port is not ready %u\n", port);
|
|
|
|
return -EAGAIN;
|
2008-07-10 04:56:51 +08:00
|
|
|
}
|
|
|
|
|
2017-06-08 13:04:10 +08:00
|
|
|
usbip_dbg_vhci_sysfs("rhport %d\n", rhport);
|
|
|
|
|
|
|
|
if ((port / VHCI_HC_PORTS) % 2)
|
|
|
|
vhci_hcd = hcd_to_vhci_hcd(hcd)->vhci->vhci_hcd_ss;
|
|
|
|
else
|
|
|
|
vhci_hcd = hcd_to_vhci_hcd(hcd)->vhci->vhci_hcd_hs;
|
|
|
|
|
|
|
|
ret = vhci_port_disconnect(vhci_hcd, rhport);
|
2016-06-13 10:33:40 +08:00
|
|
|
if (ret < 0)
|
2008-07-10 04:56:51 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
2009-07-21 14:46:13 +08:00
|
|
|
usbip_dbg_vhci_sysfs("Leave\n");
|
2011-05-06 18:47:52 +08:00
|
|
|
|
2008-07-10 04:56:51 +08:00
|
|
|
return count;
|
|
|
|
}
|
2018-01-23 18:24:07 +08:00
|
|
|
static DEVICE_ATTR_WO(detach);
|
2008-07-10 04:56:51 +08:00
|
|
|
|
2018-05-19 09:13:42 +08:00
|
|
|
static int valid_args(__u32 *pdev_nr, __u32 *rhport,
|
|
|
|
enum usb_device_speed speed)
|
2008-07-10 04:56:51 +08:00
|
|
|
{
|
2016-06-13 10:33:40 +08:00
|
|
|
if (!valid_port(pdev_nr, rhport)) {
|
|
|
|
return 0;
|
2008-07-10 04:56:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (speed) {
|
|
|
|
case USB_SPEED_LOW:
|
|
|
|
case USB_SPEED_FULL:
|
|
|
|
case USB_SPEED_HIGH:
|
2010-01-15 03:08:04 +08:00
|
|
|
case USB_SPEED_WIRELESS:
|
2017-06-08 13:04:11 +08:00
|
|
|
case USB_SPEED_SUPER:
|
2008-07-10 04:56:51 +08:00
|
|
|
break;
|
|
|
|
default:
|
2014-01-23 00:38:14 +08:00
|
|
|
pr_err("Failed attach request for unsupported USB speed: %s\n",
|
|
|
|
usb_speed_string(speed));
|
2016-06-13 10:33:40 +08:00
|
|
|
return 0;
|
2008-07-10 04:56:51 +08:00
|
|
|
}
|
|
|
|
|
2016-06-13 10:33:40 +08:00
|
|
|
return 1;
|
2008-07-10 04:56:51 +08:00
|
|
|
}
|
|
|
|
|
2016-06-13 10:33:40 +08:00
|
|
|
/* Sysfs entry to establish a virtual connection */
|
2008-07-10 04:56:51 +08:00
|
|
|
/*
|
|
|
|
* To start a new USB/IP attachment, a userland program needs to setup a TCP
|
|
|
|
* connection and then write its socket descriptor with remote device
|
|
|
|
* information into this sysfs file.
|
|
|
|
*
|
|
|
|
* A remote device is virtually attached to the root-hub port of @rhport with
|
|
|
|
* @speed. @devid is embedded into a request to specify the remote device in a
|
|
|
|
* server host.
|
|
|
|
*
|
|
|
|
* write() returns 0 on success, else negative errno.
|
|
|
|
*/
|
2018-01-23 18:24:07 +08:00
|
|
|
static ssize_t attach_store(struct device *dev, struct device_attribute *attr,
|
2008-07-10 04:56:51 +08:00
|
|
|
const char *buf, size_t count)
|
|
|
|
{
|
|
|
|
struct socket *socket;
|
|
|
|
int sockfd = 0;
|
2016-06-13 10:33:40 +08:00
|
|
|
__u32 port = 0, pdev_nr = 0, rhport = 0, devid = 0, speed = 0;
|
|
|
|
struct usb_hcd *hcd;
|
2017-06-08 13:04:09 +08:00
|
|
|
struct vhci_hcd *vhci_hcd;
|
2016-06-13 10:33:40 +08:00
|
|
|
struct vhci_device *vdev;
|
2017-06-08 13:04:09 +08:00
|
|
|
struct vhci *vhci;
|
2014-03-06 09:33:08 +08:00
|
|
|
int err;
|
2016-02-03 01:36:39 +08:00
|
|
|
unsigned long flags;
|
usbip: fix vhci_hcd attach_store() races leading to gpf
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>
2021-03-08 11:53:30 +08:00
|
|
|
struct task_struct *tcp_rx = NULL;
|
|
|
|
struct task_struct *tcp_tx = NULL;
|
2008-07-10 04:56:51 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* @rhport: port number of vhci_hcd
|
|
|
|
* @sockfd: socket descriptor of an established TCP connection
|
|
|
|
* @devid: unique device identifier in a remote host
|
|
|
|
* @speed: usb device speed in a remote host
|
|
|
|
*/
|
2016-06-13 10:33:40 +08:00
|
|
|
if (sscanf(buf, "%u %u %u %u", &port, &sockfd, &devid, &speed) != 4)
|
2014-03-07 02:36:34 +08:00
|
|
|
return -EINVAL;
|
2016-06-13 10:33:40 +08:00
|
|
|
pdev_nr = port_to_pdev_nr(port);
|
|
|
|
rhport = port_to_rhport(port);
|
2008-07-10 04:56:51 +08:00
|
|
|
|
2016-06-13 10:33:40 +08:00
|
|
|
usbip_dbg_vhci_sysfs("port(%u) pdev(%d) rhport(%u)\n",
|
|
|
|
port, pdev_nr, rhport);
|
|
|
|
usbip_dbg_vhci_sysfs("sockfd(%u) devid(%u) speed(%u)\n",
|
|
|
|
sockfd, devid, speed);
|
2008-07-10 04:56:51 +08:00
|
|
|
|
|
|
|
/* check received parameters */
|
2018-05-19 09:13:42 +08:00
|
|
|
if (!valid_args(&pdev_nr, &rhport, speed))
|
2008-07-10 04:56:51 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
2017-06-08 13:04:07 +08:00
|
|
|
hcd = platform_get_drvdata(vhcis[pdev_nr].pdev);
|
2016-06-13 10:33:40 +08:00
|
|
|
if (hcd == NULL) {
|
|
|
|
dev_err(dev, "port %d is not ready\n", port);
|
|
|
|
return -EAGAIN;
|
|
|
|
}
|
2017-06-08 13:04:09 +08:00
|
|
|
|
|
|
|
vhci_hcd = hcd_to_vhci_hcd(hcd);
|
|
|
|
vhci = vhci_hcd->vhci;
|
2017-06-08 13:04:10 +08:00
|
|
|
|
|
|
|
if (speed == USB_SPEED_SUPER)
|
|
|
|
vdev = &vhci->vhci_hcd_ss->vdev[rhport];
|
|
|
|
else
|
|
|
|
vdev = &vhci->vhci_hcd_hs->vdev[rhport];
|
2016-06-13 10:33:40 +08:00
|
|
|
|
2021-03-30 09:36:48 +08:00
|
|
|
mutex_lock(&vdev->ud.sysfs_lock);
|
|
|
|
|
2012-10-22 03:45:00 +08:00
|
|
|
/* Extract socket from fd. */
|
2014-03-06 09:33:08 +08:00
|
|
|
socket = sockfd_lookup(sockfd, &err);
|
2021-03-08 11:53:27 +08:00
|
|
|
if (!socket) {
|
|
|
|
dev_err(dev, "failed to lookup sock");
|
2021-03-30 09:36:48 +08:00
|
|
|
err = -EINVAL;
|
|
|
|
goto unlock_mutex;
|
2021-03-08 11:53:27 +08:00
|
|
|
}
|
|
|
|
if (socket->type != SOCK_STREAM) {
|
|
|
|
dev_err(dev, "Expecting SOCK_STREAM - found %d",
|
|
|
|
socket->type);
|
|
|
|
sockfd_put(socket);
|
2021-03-30 09:36:48 +08:00
|
|
|
err = -EINVAL;
|
|
|
|
goto unlock_mutex;
|
2021-03-08 11:53:27 +08:00
|
|
|
}
|
2008-07-10 04:56:51 +08:00
|
|
|
|
usbip: fix vhci_hcd attach_store() races leading to gpf
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>
2021-03-08 11:53:30 +08:00
|
|
|
/* create threads before locking */
|
|
|
|
tcp_rx = kthread_create(vhci_rx_loop, &vdev->ud, "vhci_rx");
|
|
|
|
if (IS_ERR(tcp_rx)) {
|
|
|
|
sockfd_put(socket);
|
2021-03-30 09:36:48 +08:00
|
|
|
err = -EINVAL;
|
|
|
|
goto unlock_mutex;
|
usbip: fix vhci_hcd attach_store() races leading to gpf
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>
2021-03-08 11:53:30 +08:00
|
|
|
}
|
|
|
|
tcp_tx = kthread_create(vhci_tx_loop, &vdev->ud, "vhci_tx");
|
|
|
|
if (IS_ERR(tcp_tx)) {
|
|
|
|
kthread_stop(tcp_rx);
|
|
|
|
sockfd_put(socket);
|
2021-03-30 09:36:48 +08:00
|
|
|
err = -EINVAL;
|
|
|
|
goto unlock_mutex;
|
usbip: fix vhci_hcd attach_store() races leading to gpf
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>
2021-03-08 11:53:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* get task structs now */
|
|
|
|
get_task_struct(tcp_rx);
|
|
|
|
get_task_struct(tcp_tx);
|
2008-07-10 04:56:51 +08:00
|
|
|
|
usbip: fix vhci_hcd attach_store() races leading to gpf
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>
2021-03-08 11:53:30 +08:00
|
|
|
/* now begin lock until setting vdev status set */
|
2016-06-13 10:33:40 +08:00
|
|
|
spin_lock_irqsave(&vhci->lock, flags);
|
2008-07-10 04:56:51 +08:00
|
|
|
spin_lock(&vdev->ud.lock);
|
|
|
|
|
|
|
|
if (vdev->ud.status != VDEV_ST_NULL) {
|
|
|
|
/* end of the lock */
|
|
|
|
spin_unlock(&vdev->ud.lock);
|
2016-06-13 10:33:40 +08:00
|
|
|
spin_unlock_irqrestore(&vhci->lock, flags);
|
2008-07-10 04:56:51 +08:00
|
|
|
|
2014-03-06 09:33:08 +08:00
|
|
|
sockfd_put(socket);
|
usbip: fix vhci_hcd attach_store() races leading to gpf
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>
2021-03-08 11:53:30 +08:00
|
|
|
kthread_stop_put(tcp_rx);
|
|
|
|
kthread_stop_put(tcp_tx);
|
2012-10-22 03:45:00 +08:00
|
|
|
|
2011-05-20 07:47:32 +08:00
|
|
|
dev_err(dev, "port %d already used\n", rhport);
|
2017-08-29 10:13:34 +08:00
|
|
|
/*
|
|
|
|
* Will be retried from userspace
|
|
|
|
* if there's another free port.
|
|
|
|
*/
|
2021-03-30 09:36:48 +08:00
|
|
|
err = -EBUSY;
|
|
|
|
goto unlock_mutex;
|
2008-07-10 04:56:51 +08:00
|
|
|
}
|
|
|
|
|
2016-06-13 10:33:40 +08:00
|
|
|
dev_info(dev, "pdev(%u) rhport(%u) sockfd(%d)\n",
|
|
|
|
pdev_nr, rhport, sockfd);
|
|
|
|
dev_info(dev, "devid(%u) speed(%u) speed_str(%s)\n",
|
|
|
|
devid, speed, usb_speed_string(speed));
|
2008-07-10 04:56:51 +08:00
|
|
|
|
|
|
|
vdev->devid = devid;
|
|
|
|
vdev->speed = speed;
|
2017-12-08 05:16:49 +08:00
|
|
|
vdev->ud.sockfd = sockfd;
|
2008-07-10 04:56:51 +08:00
|
|
|
vdev->ud.tcp_socket = socket;
|
usbip: fix vhci_hcd attach_store() races leading to gpf
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>
2021-03-08 11:53:30 +08:00
|
|
|
vdev->ud.tcp_rx = tcp_rx;
|
|
|
|
vdev->ud.tcp_tx = tcp_tx;
|
2008-07-10 04:56:51 +08:00
|
|
|
vdev->ud.status = VDEV_ST_NOTASSIGNED;
|
2020-11-24 07:50:52 +08:00
|
|
|
usbip_kcov_handle_init(&vdev->ud);
|
2008-07-10 04:56:51 +08:00
|
|
|
|
|
|
|
spin_unlock(&vdev->ud.lock);
|
2016-06-13 10:33:40 +08:00
|
|
|
spin_unlock_irqrestore(&vhci->lock, flags);
|
2008-07-10 04:56:51 +08:00
|
|
|
/* end the lock */
|
|
|
|
|
usbip: fix vhci_hcd attach_store() races leading to gpf
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>
2021-03-08 11:53:30 +08:00
|
|
|
wake_up_process(vdev->ud.tcp_rx);
|
|
|
|
wake_up_process(vdev->ud.tcp_tx);
|
2011-04-19 03:44:10 +08:00
|
|
|
|
2016-06-13 10:33:40 +08:00
|
|
|
rh_port_connect(vdev, speed);
|
2008-07-10 04:56:51 +08:00
|
|
|
|
2021-03-30 09:36:48 +08:00
|
|
|
dev_info(dev, "Device attached\n");
|
|
|
|
|
|
|
|
mutex_unlock(&vdev->ud.sysfs_lock);
|
|
|
|
|
2008-07-10 04:56:51 +08:00
|
|
|
return count;
|
2021-03-30 09:36:48 +08:00
|
|
|
|
|
|
|
unlock_mutex:
|
|
|
|
mutex_unlock(&vdev->ud.sysfs_lock);
|
|
|
|
return err;
|
2008-07-10 04:56:51 +08:00
|
|
|
}
|
2018-01-23 18:24:07 +08:00
|
|
|
static DEVICE_ATTR_WO(attach);
|
2008-07-10 04:56:51 +08:00
|
|
|
|
2016-06-13 10:33:40 +08:00
|
|
|
#define MAX_STATUS_NAME 16
|
|
|
|
|
|
|
|
struct status_attr {
|
|
|
|
struct device_attribute attr;
|
|
|
|
char name[MAX_STATUS_NAME+1];
|
2008-07-10 04:56:51 +08:00
|
|
|
};
|
|
|
|
|
2016-06-13 10:33:40 +08:00
|
|
|
static struct status_attr *status_attrs;
|
|
|
|
|
|
|
|
static void set_status_attr(int id)
|
|
|
|
{
|
|
|
|
struct status_attr *status;
|
|
|
|
|
|
|
|
status = status_attrs + id;
|
|
|
|
if (id == 0)
|
|
|
|
strcpy(status->name, "status");
|
|
|
|
else
|
|
|
|
snprintf(status->name, MAX_STATUS_NAME+1, "status.%d", id);
|
|
|
|
status->attr.attr.name = status->name;
|
|
|
|
status->attr.attr.mode = S_IRUGO;
|
|
|
|
status->attr.show = status_show;
|
2016-12-06 03:56:38 +08:00
|
|
|
sysfs_attr_init(&status->attr.attr);
|
2016-06-13 10:33:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int init_status_attrs(void)
|
|
|
|
{
|
|
|
|
int id;
|
|
|
|
|
|
|
|
status_attrs = kcalloc(vhci_num_controllers, sizeof(struct status_attr),
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (status_attrs == NULL)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
for (id = 0; id < vhci_num_controllers; id++)
|
|
|
|
set_status_attr(id);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void finish_status_attrs(void)
|
|
|
|
{
|
|
|
|
kfree(status_attrs);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct attribute_group vhci_attr_group = {
|
|
|
|
.attrs = NULL,
|
2008-07-10 04:56:51 +08:00
|
|
|
};
|
2016-06-13 10:33:40 +08:00
|
|
|
|
|
|
|
int vhci_init_attr_group(void)
|
|
|
|
{
|
|
|
|
struct attribute **attrs;
|
|
|
|
int ret, i;
|
|
|
|
|
|
|
|
attrs = kcalloc((vhci_num_controllers + 5), sizeof(struct attribute *),
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (attrs == NULL)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
ret = init_status_attrs();
|
|
|
|
if (ret) {
|
|
|
|
kfree(attrs);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
*attrs = &dev_attr_nports.attr;
|
|
|
|
*(attrs + 1) = &dev_attr_detach.attr;
|
|
|
|
*(attrs + 2) = &dev_attr_attach.attr;
|
|
|
|
*(attrs + 3) = &dev_attr_usbip_debug.attr;
|
|
|
|
for (i = 0; i < vhci_num_controllers; i++)
|
|
|
|
*(attrs + i + 4) = &((status_attrs + i)->attr.attr);
|
|
|
|
vhci_attr_group.attrs = attrs;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void vhci_finish_attr_group(void)
|
|
|
|
{
|
|
|
|
finish_status_attrs();
|
|
|
|
kfree(vhci_attr_group.attrs);
|
|
|
|
}
|