* [PATCH v3] usbip: vhci_hcd: time out unanswered unlink requests
@ 2026-11-27 14:43 Hongyu Xie
2026-11-27 14:59 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Hongyu Xie @ 2026-11-27 14:43 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
active 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.
Queueing an unlink arms the watchdog with timer_reduce() so newer
requests cannot push the expiry past an older pending deadline. When a
request is answered via RET_UNLINK, or its target URB completes first
via RET_SUBMIT, the watchdog is re-armed to the earliest remaining
active deadline or cancelled when none remains; if the callback fires
early, it likewise re-arms to the next pending deadline. Connection
cleanup stops the timer with timer_delete_sync() before port reset so an
in-flight callback cannot queue a stale teardown against a later
attachment, and controller removal calls timer_shutdown_sync() before
posting VDEV_EVENT_REMOVED so no callback can queue work after
usbip_stop_eh() returns.
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 v3:
- Arm the watchdog with timer_reduce() in vhci_urb_dequeue() and re-arm
it to the earliest remaining active deadline when requests complete or
when the callback wakes early, preventing newer unlinks from starving
an older deadline.
- Mark matching queued unlinks completed when their target URB finishes
via RET_SUBMIT (and skip queueing CMD_UNLINK if vhci_rx already claimed
urb->hcpriv) so a lazy server RET_UNLINK cannot trigger a spurious
timeout on an idle connection.
- Use timer_delete_sync() in vhci_device_unlink_cleanup() so an in-flight
timeout callback cannot post VDEV_EVENT_ERROR_TCP after port reset.
- Move timer_shutdown_sync() before VDEV_EVENT_REMOVED in vhci_stop() so
a racing timeout callback cannot queue event work after usbip_stop_eh().
- Link to v2: https://lore.kernel.org/r/179577001740.23588.17598940424505809386@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 | 12 +++
drivers/usb/usbip/vhci_hcd.c | 138 +++++++++++++++++++++++++++++++++++
drivers/usb/usbip/vhci_rx.c | 4 +
3 files changed, 154 insertions(+)
diff --git a/drivers/usb/usbip/vhci.h b/drivers/usb/usbip/vhci.h
index 5659dce1526e..af5d6504eb83 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,12 @@ struct vhci_unlink {
/* seqnum of the unlink target */
unsigned long unlink_seqnum;
+
+ /* deadline enforced by the per-device unlink watchdog */
+ unsigned long expires;
+
+ /* target URB completed via RET_SUBMIT; no longer watched */
+ bool completed;
};
enum hub_speed {
@@ -124,6 +134,8 @@ 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_rearm_unlink_timer_locked(struct vhci_device *vdev);
+void vhci_complete_unlink_locked(struct vhci_device *vdev, __u32 seqnum);
/* 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..18a0d42ba390 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,105 @@ static int vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flag
return ret;
}
+/*
+ * Find the earliest deadline among queued unlink requests whose target URB
+ * has not completed yet. Requests whose target URB already came back via
+ * RET_SUBMIT have nobody waiting on them and are ignored.
+ *
+ * Caller must hold vdev->priv_lock.
+ */
+static bool vhci_unlink_next_deadline(struct vhci_device *vdev,
+ unsigned long *expires)
+{
+ struct vhci_unlink *unlink;
+ bool found = false;
+
+ lockdep_assert_held(&vdev->priv_lock);
+
+ list_for_each_entry(unlink, &vdev->unlink_rx, list) {
+ if (unlink->completed)
+ continue;
+ if (!found || time_before(unlink->expires, *expires)) {
+ *expires = unlink->expires;
+ found = true;
+ }
+ }
+ list_for_each_entry(unlink, &vdev->unlink_tx, list) {
+ if (unlink->completed)
+ continue;
+ if (!found || time_before(unlink->expires, *expires)) {
+ *expires = unlink->expires;
+ found = true;
+ }
+ }
+
+ return found;
+}
+
+void vhci_rearm_unlink_timer_locked(struct vhci_device *vdev)
+{
+ unsigned long expires = 0;
+
+ if (vhci_unlink_next_deadline(vdev, &expires))
+ mod_timer(&vdev->unlink_timer, expires);
+ else
+ timer_delete(&vdev->unlink_timer);
+}
+
+void vhci_complete_unlink_locked(struct vhci_device *vdev, __u32 seqnum)
+{
+ struct vhci_unlink *unlink;
+ bool changed = false;
+
+ lockdep_assert_held(&vdev->priv_lock);
+
+ list_for_each_entry(unlink, &vdev->unlink_rx, list) {
+ if (unlink->unlink_seqnum == seqnum && !unlink->completed) {
+ unlink->completed = true;
+ changed = true;
+ }
+ }
+ list_for_each_entry(unlink, &vdev->unlink_tx, list) {
+ if (unlink->unlink_seqnum == seqnum && !unlink->completed) {
+ unlink->completed = true;
+ changed = true;
+ }
+ }
+
+ if (changed)
+ vhci_rearm_unlink_timer_locked(vdev);
+}
+
+static void vhci_unlink_timeout(struct timer_list *t)
+{
+ struct vhci_device *vdev = timer_container_of(vdev, t, unlink_timer);
+ unsigned long expires = 0;
+ unsigned long flags;
+ bool expired = false;
+
+ spin_lock_irqsave(&vdev->priv_lock, flags);
+ if (vhci_unlink_next_deadline(vdev, &expires)) {
+ if (time_after_eq(jiffies, expires))
+ expired = true;
+ else
+ mod_timer(&vdev->unlink_timer, expires);
+ }
+ 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);
+}
+
/*
* 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
@@ -949,6 +1057,15 @@ static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
struct vhci_unlink *unlink;
spin_lock(&vdev->priv_lock);
+ if (!urb->hcpriv) {
+ /*
+ * URB was just picked up by vhci_rx and will be
+ * given back shortly; no CMD_UNLINK needed.
+ */
+ spin_unlock(&vdev->priv_lock);
+ spin_unlock_irqrestore(&vhci->lock, flags);
+ return 0;
+ }
/* setup CMD_UNLINK pdu */
unlink = kzalloc_obj(struct vhci_unlink, GFP_ATOMIC);
@@ -968,6 +1085,17 @@ 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);
+
+ /*
+ * Tear the connection down if the peer leaves this request
+ * unanswered past its deadline; timer_reduce() keeps any
+ * earlier pending deadline armed so newer unlinks cannot
+ * starve it.
+ */
+ unlink->expires =
+ jiffies + msecs_to_jiffies(VHCI_UNLINK_TIMEOUT_MS);
+ timer_reduce(&vdev->unlink_timer, unlink->expires);
+
wake_up(&vdev->waitq_tx);
spin_unlock(&vdev->priv_lock);
@@ -1029,6 +1157,13 @@ 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);
+
+ /*
+ * Wait for any running watchdog callback before vhci_device_reset()
+ * clears the port so a racing timeout cannot queue a stale teardown
+ * event against a later attachment.
+ */
+ timer_delete_sync(&vdev->unlink_timer);
}
/*
@@ -1143,6 +1278,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;
@@ -1267,6 +1404,7 @@ static void vhci_stop(struct usb_hcd *hcd)
for (rhport = 0; rhport < VHCI_HC_PORTS; rhport++) {
struct vhci_device *vdev = &vhci_hcd->vdev[rhport];
+ timer_shutdown_sync(&vdev->unlink_timer);
usbip_event_add(&vdev->ud, VDEV_EVENT_REMOVED);
usbip_stop_eh(&vdev->ud);
}
diff --git a/drivers/usb/usbip/vhci_rx.c b/drivers/usb/usbip/vhci_rx.c
index a678e7c89837..c71645499a7b 100644
--- a/drivers/usb/usbip/vhci_rx.c
+++ b/drivers/usb/usbip/vhci_rx.c
@@ -63,6 +63,8 @@ static void vhci_recv_ret_submit(struct vhci_device *vdev,
spin_lock_irqsave(&vdev->priv_lock, flags);
urb = pickup_urb_and_free_priv(vdev, pdu->base.seqnum);
+ if (urb)
+ vhci_complete_unlink_locked(vdev, pdu->base.seqnum);
spin_unlock_irqrestore(&vdev->priv_lock, flags);
if (!urb) {
@@ -123,6 +125,8 @@ static struct vhci_unlink *dequeue_pending_unlink(struct vhci_device *vdev,
usbip_dbg_vhci_rx("found pending unlink, %lu\n",
unlink->seqnum);
list_del(&unlink->list);
+ if (!unlink->completed)
+ vhci_rearm_unlink_timer_locked(vdev);
spin_unlock_irqrestore(&vdev->priv_lock, flags);
return unlink;
base-commit: 9437ee8b6cf607e883b36e44bd755968daaebd48
--
2.32.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH v3] usbip: vhci_hcd: time out unanswered unlink requests
2026-11-27 14:43 [PATCH v3] usbip: vhci_hcd: time out unanswered unlink requests Hongyu Xie
@ 2026-11-27 14:59 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-27 14:59 UTC (permalink / raw)
To: Hongyu Xie; +Cc: linux-usb
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] usbip: vhci_hcd: use-after-free of priv in vhci_urb_dequeue()
- [High] usbip: vhci_rx: premature unlink watchdog disarm in
vhci_recv_ret_submit()
--
commit 4a96b2f220c57ad22b859e73a1c3d66273ce70e1
Author: Hongyu Xie <xiehongyu1@kylinos.cn>
usbip: vhci_hcd: time out unanswered unlink requests
This commit adds per-request deadlines and a per-device watchdog timer for
queued CMD_UNLINK requests in vhci_hcd so unanswered unlinks tear down the
connection instead of hanging callers in usb_kill_urb().
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..18a0d42ba3904 100644
> --- a/drivers/usb/usbip/vhci_hcd.c
> +++ b/drivers/usb/usbip/vhci_hcd.c
[ ... ]
> @@ -949,6 +1057,15 @@ static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
> struct vhci_unlink *unlink;
>
> spin_lock(&vdev->priv_lock);
> + if (!urb->hcpriv) {
> + /*
> + * URB was just picked up by vhci_rx and will be
> + * given back shortly; no CMD_UNLINK needed.
> + */
> + spin_unlock(&vdev->priv_lock);
> + spin_unlock_irqrestore(&vhci->lock, flags);
> + return 0;
> + }
[Severity: High]
Can vhci_urb_dequeue() already have dereferenced a freed priv before
reaching this !urb->hcpriv check?
pickup_urb_and_free_priv() runs under vdev->priv_lock without holding
vhci->lock, whereas vhci_urb_dequeue() reads urb->hcpriv and dereferences
priv->vdev under vhci->lock before taking vdev->priv_lock:
CPU0 CPU1 (vhci_rx)
vhci_urb_dequeue()
spin_lock_irqsave(&vhci->lock, ...)
priv = urb->hcpriv;
vhci_recv_ret_submit()
spin_lock_irqsave(&vdev->priv_lock, ...)
pickup_urb_and_free_priv()
list_del(&priv->list);
kfree(priv);
urb->hcpriv = NULL;
spin_unlock_irqrestore(&vdev->priv_lock, ...)
vdev = priv->vdev; <-- use-after-free
if (!vdev->ud.tcp_socket)
...
spin_lock(&vdev->priv_lock);
if (!urb->hcpriv)
In the !vdev->ud.tcp_socket branch above this block, urb->hcpriv is not
rechecked under vdev->priv_lock at all, so the same race also performs a
second list_del(&priv->list) and kfree(priv). Deriving vdev from
urb->dev->portnum (as vhci_urb_enqueue() does) and holding vdev->priv_lock
before reading urb->hcpriv would avoid touching a freed priv.
[ ... ]
> diff --git a/drivers/usb/usbip/vhci_rx.c b/drivers/usb/usbip/vhci_rx.c
> index a678e7c898375..c71645499a7b5 100644
> --- a/drivers/usb/usbip/vhci_rx.c
> +++ b/drivers/usb/usbip/vhci_rx.c
> @@ -63,6 +63,8 @@ static void vhci_recv_ret_submit(struct vhci_device *vdev,
>
> spin_lock_irqsave(&vdev->priv_lock, flags);
> urb = pickup_urb_and_free_priv(vdev, pdu->base.seqnum);
> + if (urb)
> + vhci_complete_unlink_locked(vdev, pdu->base.seqnum);
> spin_unlock_irqrestore(&vdev->priv_lock, flags);
>
> if (!urb) {
[Severity: High]
Does calling vhci_complete_unlink_locked() here disarm vdev->unlink_timer
before the RET_SUBMIT payload has actually been received from the socket?
After releasing vdev->priv_lock, vhci_recv_ret_submit() still calls
usbip_recv_xbuff() and usbip_recv_iso(), which perform blocking
MSG_WAITALL reads on ud->tcp_socket before usb_hcd_giveback_urb() runs:
drivers/usb/usbip/vhci_rx.c:vhci_recv_ret_submit() {
...
/* recv transfer buffer */
if (usbip_recv_xbuff(ud, urb) < 0) {
urb->status = -EPROTO;
goto error;
}
/* recv iso_packet_descriptor */
if (usbip_recv_iso(ud, urb) < 0) {
urb->status = -EPROTO;
goto error;
}
...
}
If the peer stalls after sending the 48-byte RET_SUBMIT header without
sending the transfer buffer or ISO descriptors, vhci_rx blocks forever in
sock_recvmsg(). Because pickup_urb_and_free_priv() has already cleared
urb->hcpriv and vhci_complete_unlink_locked() has already cancelled the
watchdog for any earlier unlink, both pre-existing unlinks and any later
vhci_urb_dequeue() call from usb_kill_urb() are left with no active
watchdog timer, and usb_kill_urb() still hangs indefinitely.
--
Sashiko AI review · https://sashiko.dev/#/patchset/179579060266.47199.14552707473292683159@kylinos.cn?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-27 14:59 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:43 [PATCH v3] usbip: vhci_hcd: time out unanswered unlink requests Hongyu Xie
2026-11-27 14:59 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox