Linux USB
 help / color / mirror / Atom feed
* [PATCH] usbip: usbip_host: fix invalid device pointer use in do_rebind()
@ 2026-11-27 14:45 Lucas Pascal
  2026-11-27 14:54 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Lucas Pascal @ 2026-11-27 14:45 UTC (permalink / raw)
  To: valentina.manea.m, shuah, i, gregkh
  Cc: linux-usb, linux-kernel, Jeffin Philip, Lucas Pascal, stable,
	syzbot+af76b01c9a0f0ab60fb0

stub_probe() saves the usb_device in the busid table without taking a
reference, and nothing clears it when the device goes away. Writing the
busid to the "rebind" attribute, the last step of "usbip unbind", passes
that pointer to device_attach() to give the device back to its original
driver; so does stub_device_rebind() when the module is removed.

That pointer is invalid in two cases:

- The device was disconnected since it was bound to usbip-host, and
  do_rebind() uses a freed usb_device. This happens when a device stops
  responding while exported: closing its usbip connection makes
  usbip-host reset it, the reset fails and the hub driver disconnects
  it. The following "usbip unbind" oopses:

    BUG: kernel NULL pointer dereference, address: 00000000000000e0
    RIP: 0010:__device_attach+0x3b/0x1e0
    Call Trace:
     device_attach+0x10/0x20
     do_rebind.isra.0+0x3b/0xa0 [usbip_host]
     rebind_store+0x99/0x140 [usbip_host]
     drv_attr_store+0x24/0x50
     sysfs_kf_write+0x72/0x90
     kernfs_fop_write_iter+0x161/0x1f0
     vfs_write+0x29e/0x480
     ksys_write+0x71/0xf0

  do_rebind() holds the parent hub's device lock at that point, which is
  never released: every later operation on that hub blocks (hub_event(),
  usbdev_open(), driver unbinds), and only a reboot recovers.

- The busid was added to match_busid but never bound to usbip-host, and
  the pointer is NULL, as found by syzbot:

    KASAN: null-ptr-deref in range [0x00000000000000f0-0x00000000000000f7]
    RIP: 0010:do_rebind drivers/usb/usbip/stub_main.c:204 [inline]
    RIP: 0010:rebind_store+0x147/0x2d0 drivers/usb/usbip/stub_main.c:264

Look the device up by its name instead, and hold a reference while it is
used, as the driver core's bind, unbind and drivers_probe attributes do.
When there is no such device there is nothing to rebind:
rebind_store() still removes the busid from the table, so that
usbip-host can claim the device again once it comes back.

Fixes: a46034ca57ed ("staging: usbip: trigger driver probing after unbinding from usbip-host")
Cc: stable@vger.kernel.org
Reported-by: syzbot+af76b01c9a0f0ab60fb0@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=af76b01c9a0f0ab60fb0
Signed-off-by: Lucas Pascal <lucas.pascal@pqshield.com>
---

Notes:
    Developed with an AI coding assistant (Claude Code, Claude Opus 5.5),
    which traced the production oops to the pointer saved by stub_probe(),
    wrote the fix, the reproducer and most of this changelog. I reviewed
    and tested the result.
    
    Seen twice on production servers (v7.0 distribution kernels), where the
    oops left the hub lock held and the server needed a SysRq reboot.
    
    Reproduced on v7.4-rc4 (9437ee8b6cf6) with KASAN, in a QEMU VM with a
    g_zero gadget on dummy_hcd, unplugged by unloading g_zero:
    - bind to usbip-host, unbind, unplug, "match_busid del", rebind (the
      "usbip unbind" sequence): slab-use-after-free in do_rebind(), on the
      usb_device freed by usb_disconnect();
    - bind to usbip-host, unplug, rmmod usbip_host: same;
    - "match_busid add", then rebind, never bound: the syzbot
      null-ptr-deref, with and without a device of that name.
    With this patch all three run without KASAN reports, and in the first
    one the device can be bound to usbip-host again once replugged.
    
    Not tested by syzbot: #syz test is disabled for now (syzkaller issue
    8122). The third case above gives the same crash as its report.
    
    Jeffin Philip's series [1] addresses the syzbot report by turning
    "rebind" into a no-op and making the usbip tool write drivers_probe
    instead. This patch only changes the driver: it keeps "rebind" working,
    for the usbip binaries and scripts already in use, and also fixes the
    module removal path. bus_find_device_by_name() only runs on a write to
    "rebind" and at module removal.
    
    Not addressed here: "rebind" of a device still bound to usbip-host
    removes its busid entry, and the following unbind hits the BUG() in
    stub_disconnect() (reproduced, unchanged by this patch).
    
    [1] https://lore.kernel.org/all/20260811160541.29461-1-jeffinphilip14@gmail.com/

 drivers/usb/usbip/stub.h      |  1 -
 drivers/usb/usbip/stub_dev.c  |  1 -
 drivers/usb/usbip/stub_main.c | 32 +++++++++++++++++++++-----------
 3 files changed, 21 insertions(+), 13 deletions(-)

diff --git a/drivers/usb/usbip/stub.h b/drivers/usb/usbip/stub.h
index d11270560c24..ab5726eeed29 100644
--- a/drivers/usb/usbip/stub.h
+++ b/drivers/usb/usbip/stub.h
@@ -75,7 +75,6 @@ struct bus_id_priv {
 	char status;
 	int interf_count;
 	struct stub_device *sdev;
-	struct usb_device *udev;
 	char shutdown_busid;
 	spinlock_t busid_lock;
 };
diff --git a/drivers/usb/usbip/stub_dev.c b/drivers/usb/usbip/stub_dev.c
index abfa11d6bde7..b03c77d598e8 100644
--- a/drivers/usb/usbip/stub_dev.c
+++ b/drivers/usb/usbip/stub_dev.c
@@ -371,7 +371,6 @@ static int stub_probe(struct usb_device *udev)
 	dev_set_drvdata(&udev->dev, sdev);
 
 	busid_priv->sdev = sdev;
-	busid_priv->udev = udev;
 
 	save_status = busid_priv->status;
 	busid_priv->status = STUB_BUSID_ALLOC;
diff --git a/drivers/usb/usbip/stub_main.c b/drivers/usb/usbip/stub_main.c
index 01452f7bdabb..563c92ab88e7 100644
--- a/drivers/usb/usbip/stub_main.c
+++ b/drivers/usb/usbip/stub_main.c
@@ -196,18 +196,28 @@ static ssize_t match_busid_store(struct device_driver *dev, const char *buf,
 }
 static DRIVER_ATTR_RW(match_busid);
 
-static int do_rebind(char *busid, struct bus_id_priv *busid_priv)
+static int do_rebind(char *busid)
 {
+	struct device *dev;
 	int ret = 0;
 
+	/*
+	 * Look the device up by name: it may have been disconnected and
+	 * freed since it was bound to usbip-host.
+	 */
+	dev = bus_find_device_by_name(stub_driver.driver.bus, NULL, busid);
+	if (!dev)
+		return -ENODEV;
+
 	/* device_attach() callers should hold parent lock for USB */
-	if (busid_priv->udev->dev.parent)
-		device_lock(busid_priv->udev->dev.parent);
-	ret = device_attach(&busid_priv->udev->dev);
-	if (busid_priv->udev->dev.parent)
-		device_unlock(busid_priv->udev->dev.parent);
+	if (dev->parent)
+		device_lock(dev->parent);
+	ret = device_attach(dev);
+	if (dev->parent)
+		device_unlock(dev->parent);
 	if (ret < 0)
-		dev_err(&busid_priv->udev->dev, "rebind failed\n");
+		dev_err(dev, "rebind failed\n");
+	put_device(dev);
 	return ret;
 }
 
@@ -232,8 +242,7 @@ static void stub_device_rebind(void)
 	for (i = 0; i < MAX_BUSID; i++) {
 		if (busid_table[i].name[0] &&
 		    busid_table[i].shutdown_busid) {
-			busid_priv = &(busid_table[i]);
-			do_rebind(busid_table[i].name, busid_priv);
+			do_rebind(busid_table[i].name);
 		}
 	}
 #endif
@@ -261,8 +270,9 @@ static ssize_t rebind_store(struct device_driver *dev, const char *buf,
 	/* release the busid lock */
 	put_busid_priv(bid);
 
-	ret = do_rebind((char *) buf, bid);
-	if (ret < 0)
+	ret = do_rebind((char *) buf);
+	/* a device disconnected meanwhile has nothing to rebind */
+	if (ret < 0 && ret != -ENODEV)
 		return ret;
 
 	/* delete device from busid_table */

base-commit: 9437ee8b6cf607e883b36e44bd755968daaebd48
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] usbip: usbip_host: fix invalid device pointer use in do_rebind()
  2026-11-27 14:45 [PATCH] usbip: usbip_host: fix invalid device pointer use in do_rebind() Lucas Pascal
@ 2026-11-27 14:54 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-27 14:54 UTC (permalink / raw)
  To: Lucas Pascal; +Cc: linux-usb

> stub_probe() saves the usb_device in the busid table without taking a
> reference, and nothing clears it when the device goes away. Writing the
> busid to the "rebind" attribute, the last step of "usbip unbind", passes
> that pointer to device_attach() to give the device back to its original
> driver; so does stub_device_rebind() when the module is removed.
> 
> That pointer is invalid in two cases:
> 
> - The device was disconnected since it was bound to usbip-host, and
>   do_rebind() uses a freed usb_device. This happens when a device stops
>   responding while exported: closing its usbip connection makes
>   usbip-host reset it, the reset fails and the hub driver disconnects
>   it. The following "usbip unbind" oopses:
> 
>     BUG: kernel NULL pointer dereference, address: 00000000000000e0
> [ ... ]
> Fixes: a46034ca57ed ("staging: usbip: trigger driver probing after unbinding from usbip-host")
> Cc: stable@vger.kernel.org
> Reported-by: syzbot+af76b01c9a0f0ab60fb0@syzkaller.appspotmail.com
> Closes: https://syzkaller.appspot.com/bug?extid=af76b01c9a0f0ab60fb0
> Signed-off-by: Lucas Pascal <lucas.pascal@pqshield.com>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261127144551.350348-1-lucas.pascal@pqshield.com?part=1

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-27 14:54 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-27 14:45 [PATCH] usbip: usbip_host: fix invalid device pointer use in do_rebind() Lucas Pascal
2026-11-27 14:54 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox