Linux USB
 help / color / mirror / Atom feed
* [PATCH v2] usbip: vhci_hcd: time out unanswered unlink requests
@ 2026-11-27  9:00 Hongyu Xie
  2026-11-27  9:21 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Hongyu Xie @ 2026-11-27  9:00 UTC (permalink / raw)
  To: Shuah Khan, shuah, valentina.manea.m, gregkh
  Cc: i, bgmerrell, linux-usb, linux-kernel,
	syzbot+b6f11035e572f08bc20f, syzbot+a7edecbf389d11a369d4,
	syzbot+bfa320b67fcbe2583c6f

When the connection to the peer is alive, vhci_urb_dequeue() only queues
a CMD_UNLINK PDU and relies on the peer to send RET_UNLINK (or
RET_SUBMIT) before the unlinked URB is given back.  A peer that keeps
the socket open but never answers therefore makes usb_kill_urb() wait
forever.  The waiter is typically the hub work item, which runs with
the shared address0_mutex held, so every later task that needs that
mutex or the device lock chain - a second attach, usbfs open/release/
ioctl, udev - piles up behind it and the whole USB stack wedges.

syzbot reports this shape in three long-lived bugs:
  INFO: task hung in hub_port_init
  INFO: task hung in hub_event
  INFO: task hung in usbdev_release

Give every queued unlink request a deadline of its own and arm a
per-device watchdog; when the watchdog fires, it tears the connection
down through the existing error path (VDEV_EVENT_ERROR_TCP) if any
request is still unanswered past its own deadline.  The teardown
already knows how to give back every pending URB with -ENODEV, and a
RET_UNLINK that arrives late (or never) after that is handled by the
existing "already given back" paths.

The watchdog is cancelled whenever no request is outstanding.  Queueing
a request re-arms it to that request's deadline, so the callback does
not trust the timer's expiry: it re-checks the outstanding requests
against their own deadlines under priv_lock and becomes a no-op when
none has run out.  A peer that answers promptly is therefore unaffected.
Controller removal quiesces the device first - the event handler
stops the per-device rx/tx threads and drains the pending events -
and only then calls timer_shutdown_sync(), which also waits for a
watchdog callback that is already running, so the callback cannot
touch the controller after it is freed.

In a regular setup, any network stall that leaves the TCP connection
established while the peer stops replying (such as a wedged proxy, a
firewall dropping server-to-client traffic, or a hung server) triggers
this whenever a client control transfer times out into usb_kill_urb().
Verified end to end with usbipd exporting g_serial on usbip-vudc and
usbip attach through a TCP relay stopped with SIGSTOP after enumeration:
deauthorizing the device hangs forever in usb_kill_urb() without this
patch, whereas with it the 5s watchdog tears the connection down and
the writer returns cleanly.

A similar fix was proposed as an RFC by the syzbot assisted-fix
pipeline; this version arms a single per-device watchdog instead of a
timer per unlink request.

Fixes: 04679b3489e0 ("Staging: USB/IP: add client driver")
Reported-by: syzbot+b6f11035e572f08bc20f@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?id=58aef18c1ad93d87652f24a0bc7a74b6d98ac710
Reported-by: syzbot+a7edecbf389d11a369d4@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?id=7ff8ae4abed51ff2354c1addb664143ec26ca588
Reported-by: syzbot+bfa320b67fcbe2583c6f@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?id=048da78aba1a3a6139509492b2be505b2d223762
Link: https://lore.kernel.org/all/1d6db53d-07ab-46ad-adc4-d09c95b2c83f@mail.kernel.org/
Signed-off-by: Hongyu Xie <xiehongyu1@kylinos.cn>
---
Changes in v2:
- Document regular-setup reproduction and end-to-end usbipd/vhci test
  verification in the commit message per Shuah Khan's question on v1
  (no code changes).
- Link to v1: https://lore.kernel.org/r/20261006125652.10296-1-xiehongyu1@kylinos.cn

 drivers/usb/usbip/vhci.h     |  8 ++++
 drivers/usb/usbip/vhci_hcd.c | 83 ++++++++++++++++++++++++++++++++++++
 drivers/usb/usbip/vhci_rx.c  |  2 +
 3 files changed, 93 insertions(+)

diff --git a/drivers/usb/usbip/vhci.h b/drivers/usb/usbip/vhci.h
index 5659dce1526e..6222283d9551 100644
--- a/drivers/usb/usbip/vhci.h
+++ b/drivers/usb/usbip/vhci.h
@@ -11,6 +11,7 @@
 #include <linux/list.h>
 #include <linux/spinlock.h>
 #include <linux/sysfs.h>
+#include <linux/timer.h>
 #include <linux/types.h>
 #include <linux/usb.h>
 #include <linux/usb/hcd.h>
@@ -44,6 +45,9 @@ struct vhci_device {
 	struct list_head unlink_tx;
 	struct list_head unlink_rx;
 
+	/* fires when the peer leaves an unlink request unanswered */
+	struct timer_list unlink_timer;
+
 	/* vhci_tx thread sleeps for this queue */
 	wait_queue_head_t waitq_tx;
 };
@@ -65,6 +69,9 @@ struct vhci_unlink {
 
 	/* seqnum of the unlink target */
 	unsigned long unlink_seqnum;
+
+	/* deadline enforced by the per-device unlink watchdog */
+	unsigned long expires;
 };
 
 enum hub_speed {
@@ -124,6 +131,7 @@ extern struct attribute_group vhci_attr_group;
 
 /* vhci_hcd.c */
 void rh_port_connect(struct vhci_device *vdev, enum usb_device_speed speed);
+void vhci_stop_unlink_timer(struct vhci_device *vdev);
 
 /* vhci_sysfs.c */
 int vhci_init_attr_group(void);
diff --git a/drivers/usb/usbip/vhci_hcd.c b/drivers/usb/usbip/vhci_hcd.c
index 39e8faf4c18c..220051339445 100644
--- a/drivers/usb/usbip/vhci_hcd.c
+++ b/drivers/usb/usbip/vhci_hcd.c
@@ -19,6 +19,15 @@
 #define DRIVER_AUTHOR "Takahiro Hirofuchi"
 #define DRIVER_DESC "USB/IP 'Virtual' Host Controller (VHCI) Driver"
 
+/*
+ * How long to wait for RET_UNLINK after sending CMD_UNLINK.  A peer that
+ * cannot answer an unlink request within a control-transfer timeout
+ * (USB_CTRL_GET_TIMEOUT) worth of time is considered stuck, and the
+ * connection is torn down so that URBs waiting for their unlink reply can
+ * be given back instead of hanging their owners forever.
+ */
+#define VHCI_UNLINK_TIMEOUT_MS	5000
+
 /*
  * TODO
  *	- update root hub emulation
@@ -846,6 +855,67 @@ static int vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flag
 	return ret;
 }
 
+/*
+ * Is any queued unlink request past its own deadline?  Each request
+ * carries the time by which the peer must have answered it, and the
+ * timer may have been re-armed by a newer request before that, so the
+ * callback decides by walking the lists rather than by the timer's own
+ * expiry.
+ */
+static bool vhci_unlink_expired(struct vhci_device *vdev, unsigned long now)
+{
+	struct vhci_unlink *unlink;
+
+	list_for_each_entry(unlink, &vdev->unlink_tx, list)
+		if (time_after_eq(now, unlink->expires))
+			return true;
+	list_for_each_entry(unlink, &vdev->unlink_rx, list)
+		if (time_after_eq(now, unlink->expires))
+			return true;
+
+	return false;
+}
+
+static void vhci_unlink_timeout(struct timer_list *t)
+{
+	struct vhci_device *vdev = timer_container_of(vdev, t, unlink_timer);
+	unsigned long flags;
+	bool expired;
+
+	spin_lock_irqsave(&vdev->priv_lock, flags);
+	expired = vhci_unlink_expired(vdev, jiffies);
+	spin_unlock_irqrestore(&vdev->priv_lock, flags);
+
+	if (!expired)
+		return;
+
+	dev_info(hcd_dev(vhci_hcd_to_hcd(vdev_to_vhci_hcd(vdev))),
+		 "peer did not reply to an unlink request within %d msecs\n",
+		 VHCI_UNLINK_TIMEOUT_MS);
+
+	/*
+	 * Reuses the teardown path taken on other peer failures, which
+	 * gives back all URBs waiting for their unlink reply.
+	 */
+	usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_TCP);
+}
+
+/*
+ * Stop the unlink watchdog once every queued unlink request is answered
+ * or given back.  Safe from any context: a concurrent watchdog expiry
+ * re-checks the per-request deadlines under vdev->priv_lock and becomes
+ * a no-op.
+ */
+void vhci_stop_unlink_timer(struct vhci_device *vdev)
+{
+	unsigned long flags;
+
+	spin_lock_irqsave(&vdev->priv_lock, flags);
+	if (list_empty(&vdev->unlink_tx) && list_empty(&vdev->unlink_rx))
+		timer_delete(&vdev->unlink_timer);
+	spin_unlock_irqrestore(&vdev->priv_lock, flags);
+}
+
 /*
  * vhci_rx gives back the urb after receiving the reply of the urb.  If an
  * unlink pdu is sent or not, vhci_rx receives a normal return pdu and gives
@@ -968,6 +1038,14 @@ static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
 		/* send cmd_unlink and try to cancel the pending URB in the
 		 * peer */
 		list_add_tail(&unlink->list, &vdev->unlink_tx);
+
+		/* if the peer does not answer this request within the
+		 * timeout, tear the connection down
+		 */
+		unlink->expires =
+			jiffies + msecs_to_jiffies(VHCI_UNLINK_TIMEOUT_MS);
+		mod_timer(&vdev->unlink_timer, unlink->expires);
+
 		wake_up(&vdev->waitq_tx);
 
 		spin_unlock(&vdev->priv_lock);
@@ -1029,6 +1107,8 @@ static void vhci_device_unlink_cleanup(struct vhci_device *vdev)
 
 	/* give back URB of unanswered unlink request */
 	vhci_cleanup_unlink_list(vdev, &vdev->unlink_rx);
+
+	vhci_stop_unlink_timer(vdev);
 }
 
 /*
@@ -1143,6 +1223,8 @@ static void vhci_device_init(struct vhci_device *vdev)
 
 	init_waitqueue_head(&vdev->waitq_tx);
 
+	timer_setup(&vdev->unlink_timer, vhci_unlink_timeout, 0);
+
 	vdev->ud.eh_ops.shutdown = vhci_shutdown_connection;
 	vdev->ud.eh_ops.reset = vhci_device_reset;
 	vdev->ud.eh_ops.unusable = vhci_device_unusable;
@@ -1269,6 +1351,7 @@ static void vhci_stop(struct usb_hcd *hcd)
 
 		usbip_event_add(&vdev->ud, VDEV_EVENT_REMOVED);
 		usbip_stop_eh(&vdev->ud);
+		timer_shutdown_sync(&vdev->unlink_timer);
 	}
 }
 
diff --git a/drivers/usb/usbip/vhci_rx.c b/drivers/usb/usbip/vhci_rx.c
index a678e7c89837..32b1550839d2 100644
--- a/drivers/usb/usbip/vhci_rx.c
+++ b/drivers/usb/usbip/vhci_rx.c
@@ -179,6 +179,8 @@ static void vhci_recv_ret_unlink(struct vhci_device *vdev,
 	}
 
 	kfree(unlink);
+
+	vhci_stop_unlink_timer(vdev);
 }
 
 static int vhci_priv_tx_empty(struct vhci_device *vdev)

base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3
-- 
2.39.5 (Apple Git-154)


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

* Re: [PATCH v2] usbip: vhci_hcd: time out unanswered unlink requests
  2026-11-27  9:00 [PATCH v2] usbip: vhci_hcd: time out unanswered unlink requests Hongyu Xie
@ 2026-11-27  9:21 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-27  9:21 UTC (permalink / raw)
  To: Hongyu Xie; +Cc: linux-usb

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] usbip: vhci_hcd: mod_timer() in vhci_urb_dequeue() starves
  unlink watchdog
- [High] usbip: vhci_hcd: vhci_recv_ret_submit() leaves unlink watchdog
  armed
- [Medium] usbip: vhci_hcd: timer_delete() in
  vhci_device_unlink_cleanup() races timeout
- [Medium] usbip: vhci_hcd: UAF on vdev->ud due to late
  timer_shutdown_sync() in vhci_stop()
--

commit 6905f74cb6251ec6155915442dc359b42470939a
Author: Hongyu Xie <xiehongyu1@kylinos.cn>

usbip: vhci_hcd: time out unanswered unlink requests

This commit adds a per-device watchdog timer that tears down the USB/IP
connection if any queued unlink request remains unanswered past its 5-second
deadline.

Link: https://lore.kernel.org/all/1d6db53d-07ab-46ad-adc4-d09c95b2c83f@mail.kernel.org/

> diff --git a/drivers/usb/usbip/vhci_hcd.c b/drivers/usb/usbip/vhci_hcd.c
> index 39e8faf4c18cd..2200513394451 100644
> --- a/drivers/usb/usbip/vhci_hcd.c
> +++ b/drivers/usb/usbip/vhci_hcd.c

[ ... ]

> @@ -846,6 +855,67 @@ static int vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flag
>  	return ret;
>  }
>  
> +/*
> + * Is any queued unlink request past its own deadline?  Each request
> + * carries the time by which the peer must have answered it, and the
> + * timer may have been re-armed by a newer request before that, so the
> + * callback decides by walking the lists rather than by the timer's own
> + * expiry.
> + */
> +static bool vhci_unlink_expired(struct vhci_device *vdev, unsigned long now)
> +{
> +	struct vhci_unlink *unlink;
> +
> +	list_for_each_entry(unlink, &vdev->unlink_tx, list)
> +		if (time_after_eq(now, unlink->expires))
> +			return true;
> +	list_for_each_entry(unlink, &vdev->unlink_rx, list)
> +		if (time_after_eq(now, unlink->expires))
> +			return true;
> +
> +	return false;
> +}

[Severity: High]
Can vhci_unlink_expired() trigger a spurious VDEV_EVENT_ERROR_TCP teardown
when an unlinked URB has already completed normally via RET_SUBMIT?

If the target URB completes on the peer before CMD_UNLINK takes effect,
vhci_recv_ret_submit() calls pickup_urb_and_free_priv() and gives back the
URB, satisfying the unlink caller, but leaves the vhci_unlink entry on
vdev->unlink_tx or vdev->unlink_rx and vdev->unlink_timer armed.

When paired with the in-tree usbip-host driver, stub_recv_cmd_unlink() sees
that the URB is no longer in sdev->priv_init and queues a dummy RET_UNLINK
without waking sdev->tx_waitq:

drivers/usb/usbip/stub_rx.c:stub_recv_cmd_unlink() {
	...
	stub_enqueue_ret_unlink(sdev, pdu->base.seqnum, 0);

	spin_unlock_irqrestore(&sdev->priv_lock, flags);

	return 0;
}

If no other URB completes on the device within 5 seconds to wake
stub_tx_loop(), RET_UNLINK stays queued on the server and
vhci_unlink_timeout() tears down a healthy connection even though the
unlinked URB was already given back.

> +
> +static void vhci_unlink_timeout(struct timer_list *t)
> +{
> +	struct vhci_device *vdev = timer_container_of(vdev, t, unlink_timer);
> +	unsigned long flags;
> +	bool expired;
> +
> +	spin_lock_irqsave(&vdev->priv_lock, flags);
> +	expired = vhci_unlink_expired(vdev, jiffies);
> +	spin_unlock_irqrestore(&vdev->priv_lock, flags);
> +
> +	if (!expired)
> +		return;
> +
> +	dev_info(hcd_dev(vhci_hcd_to_hcd(vdev_to_vhci_hcd(vdev))),
> +		 "peer did not reply to an unlink request within %d msecs\n",
> +		 VHCI_UNLINK_TIMEOUT_MS);
> +
> +	/*
> +	 * Reuses the teardown path taken on other peer failures, which
> +	 * gives back all URBs waiting for their unlink reply.
> +	 */
> +	usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_TCP);
> +}
> +
> +/*
> + * Stop the unlink watchdog once every queued unlink request is answered
> + * or given back.  Safe from any context: a concurrent watchdog expiry
> + * re-checks the per-request deadlines under vdev->priv_lock and becomes
> + * a no-op.
> + */
> +void vhci_stop_unlink_timer(struct vhci_device *vdev)
> +{
> +	unsigned long flags;
> +
> +	spin_lock_irqsave(&vdev->priv_lock, flags);
> +	if (list_empty(&vdev->unlink_tx) && list_empty(&vdev->unlink_rx))
> +		timer_delete(&vdev->unlink_timer);
> +	spin_unlock_irqrestore(&vdev->priv_lock, flags);
> +}
> +
>  /*
>   * vhci_rx gives back the urb after receiving the reply of the urb.  If an
>   * unlink pdu is sent or not, vhci_rx receives a normal return pdu and gives
> @@ -968,6 +1038,14 @@ static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
>  		/* send cmd_unlink and try to cancel the pending URB in the
>  		 * peer */
>  		list_add_tail(&unlink->list, &vdev->unlink_tx);
> +
> +		/* if the peer does not answer this request within the
> +		 * timeout, tear the connection down
> +		 */
> +		unlink->expires =
> +			jiffies + msecs_to_jiffies(VHCI_UNLINK_TIMEOUT_MS);
> +		mod_timer(&vdev->unlink_timer, unlink->expires);
> +
>  		wake_up(&vdev->waitq_tx);
>  
>  		spin_unlock(&vdev->priv_lock);

[Severity: High]
Does calling mod_timer(&vdev->unlink_timer, unlink->expires) unconditionally
on every unlink overwrite the earlier deadline of an older pending request?

If an older unlink request goes unanswered while subsequent URBs on the same
device are unlinked at intervals shorter than 5 seconds, mod_timer()
continually pushes vdev->unlink_timer out to the newest request's deadline,
starving vhci_unlink_timeout() so the caller waiting in usb_kill_urb() still
hangs forever.

In addition, if an earlier request is answered after a newer one has already
pushed the timer forward, or if vhci_unlink_timeout() fires before the
remaining requests reach their deadlines, neither vhci_unlink_timeout() nor
vhci_stop_unlink_timer() re-arms vdev->unlink_timer to the earliest
remaining unlink->expires.  Arming the timer only when it is not already
pending (or to the earliest pending deadline) and re-arming it to the next
earliest deadline when requests remain would avoid both problems.

> @@ -1029,6 +1107,8 @@ static void vhci_device_unlink_cleanup(struct vhci_device *vdev)
>  
>  	/* give back URB of unanswered unlink request */
>  	vhci_cleanup_unlink_list(vdev, &vdev->unlink_rx);
> +
> +	vhci_stop_unlink_timer(vdev);
>  }

[Severity: Medium]
Can an in-flight vhci_unlink_timeout() callback race with
vhci_device_unlink_cleanup() and queue a stale VDEV_EVENT_ERROR_TCP after
connection reset has finished?

CPU0                                    CPU1
vhci_unlink_timeout()
  spin_lock_irqsave(&vdev->priv_lock)
  expired = vhci_unlink_expired(...)
  /* expired == true */
  spin_unlock_irqrestore(...)
                                        event_handler()
                                          vhci_shutdown_connection()
                                            vhci_device_unlink_cleanup()
                                              vhci_cleanup_unlink_list(...)
                                              vhci_stop_unlink_timer()
                                                timer_delete(...)
                                          vhci_device_reset()
  usbip_event_add(&vdev->ud,
                  VDEV_EVENT_ERROR_TCP) <-- spurious shutdown/reset

Because vhci_stop_unlink_timer() uses timer_delete() rather than
timer_delete_sync(), it does not wait for a callback that has already dropped
vdev->priv_lock on another CPU.  Once event_handler() finishes resetting the
port, the delayed usbip_event_add() triggers a second shutdown and reset on
vdev->ud, which can tear down a new connection attached to the same port in
the meantime.  Since vhci_device_unlink_cleanup() runs in process context,
it could call timer_delete_sync(&vdev->unlink_timer).

[ ... ]

> @@ -1269,6 +1351,7 @@ static void vhci_stop(struct usb_hcd *hcd)
>  
>  		usbip_event_add(&vdev->ud, VDEV_EVENT_REMOVED);
>  		usbip_stop_eh(&vdev->ud);
> +		timer_shutdown_sync(&vdev->unlink_timer);
>  	}
>  }

[Severity: Medium]
Can calling timer_shutdown_sync() after usbip_stop_eh() allow an in-flight
vhci_unlink_timeout() to queue usbip_work after the event handler has
already stopped, causing a use-after-free on vdev->ud?

CPU0                                    CPU1
vhci_unlink_timeout()
  usbip_event_add(..., ERROR_TCP)
    if (ud->event & USBIP_EH_BYE)
      /* false */
                                        vhci_stop()
                                          usbip_event_add(..., REMOVED)
                                          usbip_stop_eh(&vdev->ud)
                                            /* event_handler() processes
                                             * REMOVED, sets USBIP_EH_BYE,
                                             * wakes eh_waitq */
    set_event(ud, event)
    spin_lock_irqsave(&event_lock)
    list_add_tail(&ue->node, &event_list)
    queue_work(usbip_queue, &usbip_work)
    spin_unlock_irqrestore(&event_lock)
                                          timer_shutdown_sync(...)
                                        usb_put_hcd(hcd) /* frees vdev */
                                        usbip_work -> event_handler()
                                          ud->event <-- use-after-free

In usbip_event_add(), ud->event & USBIP_EH_BYE is checked locklessly before
acquiring event_lock and is not rechecked under event_lock before queueing
usbip_work.  Calling timer_shutdown_sync(&vdev->unlink_timer) before
usbip_event_add(&vdev->ud, VDEV_EVENT_REMOVED) ensures no timer callback can
race with controller removal.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/179577001740.23588.17598940424505809386@kylinos.cn?part=1

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

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

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-27  9:00 [PATCH v2] usbip: vhci_hcd: time out unanswered unlink requests Hongyu Xie
2026-11-27  9:21 ` sashiko-bot

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