From: sashiko-bot@kernel.org
To: "Igor Skalkin" <igor.skalkin@oss.qualcomm.com>
Cc: virtualization@lists.linux.dev, linux-usb@vger.kernel.org,
"Michael S. Tsirkin" <mst@redhat.com>,
"Eugenio Perez" <eperezma@redhat.com>
Subject: Re: [RFC PATCH v2 6/7] virtio-usb: add host/device role switch support
Date: Tue, 01 Dec 2026 18:00:02 +0000 [thread overview]
Message-ID: <sashiko-outbox-243921@kernel.org> (raw)
In-Reply-To: <c139d48a347eaa45c4b6cadb1a71ec0e4c77463b.1796144390.git.igor.skalkin@oss.qualcomm.com>
Thank you for your contribution! Sashiko AI review found 18 potential
issues to consider:
- [High] usb: virtio: DMA use-after-free from early giveback in
virtio_ep_disable()
- [High] usb: virtio: use-after-free of vudc->veps on
VIRTIO_USB_EVT_DEV_UNBIND
- [High] usb: virtio: sleeping cancel_work_sync() in atomic
virtio_ep_disable()
- [High] usb: virtio: virtio_usb_dc_wait_ep0_idle() ignores ep0
pend_queue
- [High] usb: virtio: vep_disable_work() reads cleared ep->desc for
direction
- [High] usb: virtio: TOCTOU race in virtio_ep_queue() queues on
disabled ep
- [High] usb: virtio: giveback_inflight list corruption in
do_real_queue()
- [High] usb: virtio: cancelling enable_work in virtio_ep_disable()
hangs ep0
- [High] usb: virtio_usb: use-after-free of usb_hcd on role switch
deinit race
- [High] usb: virtio_usb: premature vhc publication in
virtio_usb_hc_vp_init()
- [High] usb: virtio_usb: unbounded devm memory leak across runtime role
switches
- [Medium] usb: virtio: uncaptured error from
virtio_usb_rsw_sysfs_init() in probe
- [Medium] usb: virtio: uninitialized work_structs and vhc_lock used on
probe error
- [Medium] usb: virtio: event virtqueues notified before
virtio_device_ready()
- [Medium] usb: virtio: NULL dereference of vudc in
virtio_usb_dc_event_process()
- [Medium] usb: virtio: missing vusb->started check in
virtio_usb_dc_data_notify_cb()
- [Medium] usb: virtio_usb: unchecked kasprintf() and non-unique name in
rsw_vport_init()
- [Medium] usb: virtio_usb: pending signal aborts ROLE_GET resync in
vusb_set_role_cb()
--
commit 565a75ee81e50e5bcae0b0442367dc31446e8b3f
Author: Igor Skalkin <igor.skalkin@oss.qualcomm.com>
virtio-usb: add host/device role switch support
This commit adds runtime host/device role switching support for dual-role
virtio-usb ports via VIRTIO_USB_F_SWITCH_ROLE and the USB role switch
class, dynamically creating and destroying each port's HCD on role
transitions.
> diff --git a/drivers/usb/virtio_usb/controller.c b/drivers/usb/virtio_usb/controller.c
> index 9f875a127f67b..b0ce6b70c0b1e 100644
> --- a/drivers/usb/virtio_usb/controller.c
> +++ b/drivers/usb/virtio_usb/controller.c
[ ... ]
> @@ -210,20 +224,45 @@ static int virtio_usb_probe(struct virtio_device *vdev)
> }
> }
>
> + if (vusb->device_role) {
> + INIT_WORK(&vusb->vq_dev_event_work, virtio_usb_dc_event_work);
> + INIT_WORK(&vusb->vq_dev_data_rx_work, virtio_usb_dc_data_work);
> +
> + /* Populate the shared device event queue before telling the
> + * backend we are ready, so a DEV_CONNECTED event can never
> + * race ahead of the driver having posted receive buffers.
> + */
> + rc = virtio_usb_dc_event_populate(vusb);
> + if (rc) {
> + dev_err(&vdev->dev,
> + "%s virtio_usb_dc_event_populate() error(%d)\n",
> + __func__, rc);
> + goto on_error;
> + }
> + }
> +
> + virtio_device_ready(vdev);
[Severity: Medium]
Does calling virtio_usb_rsw_init() -> virtio_usb_rsw_event_populate() and
virtio_usb_dc_event_populate() before virtio_device_ready() drop their
virtqueue notifications?
Both functions call virtio_usb_events_populate(), which invokes
virtqueue_notify() immediately. Before virtio_device_ready() sets
VIRTIO_CONFIG_S_DRIVER_OK (and calls __virtio_unbreak_device() when
CONFIG_VIRTIO_HARDEN_NOTIFICATION is enabled), vq->broken is still true, so
virtqueue_notify() returns false without notifying the backend of the
posted event buffers.
> /* Resolve every port's role. With only one role negotiated, every
> * port unambiguously has that role. With both negotiated, query
> - * each port's actual role individually via virtio_usb_rsw_get_role(), since
> - * it is otherwise ambiguous (host|device).
> + * each port's actual role - and, if switch_role is negotiated,
> + * whether the port supports switching - individually via
> + * virtio_usb_rsw_get_role(), since a port's own role is otherwise ambiguous
> + * (host|device).
> */
> for (i = 0; i < vusb->nports; i++) {
> + vusb->vports[i].vusb = vusb;
> + spin_lock_init(&vusb->vports[i].vhc_lock);
> +
> if (vusb->host_role && !vusb->device_role) {
> vusb->vports[i].role = VIRTIO_USB_ROLE_HOST;
> } else if (vusb->device_role && !vusb->host_role) {
> vusb->vports[i].role = VIRTIO_USB_ROLE_DEVICE;
> } else {
> - u32 status, role;
> + u32 status, role, supported_role;
>
> - status = virtio_usb_rsw_get_role(vusb, i, &role);
> + status = virtio_usb_rsw_get_role(vusb, i, &role,
> + &supported_role);
> if (status != VIRTIO_USB_S_OK) {
> dev_err(&vdev->dev, "%s status(%d)\n", __func__,
> status);
> @@ -238,9 +277,27 @@ static int virtio_usb_probe(struct virtio_device *vdev)
> goto on_error;
> }
> vusb->vports[i].role = role;
> + vusb->vports[i].is_dual_role =
> + supported_role &
> + (1UL << VIRTIO_USB_F_SWITCH_ROLE);
> +
> + if (vusb->vports[i].is_dual_role &&
> + !vusb->switch_role) {
> + dev_err(&vdev->dev,
> + "%s port%d switch_role not supported\n",
> + __func__, i);
> + rc = -EIO;
> + goto on_error;
> + }
> }
> }
[Severity: Medium]
Can an early goto on_error in virtio_usb_probe() call cancel_work_sync()
and spin_lock() on uninitialized work_structs and vhc_lock spinlocks?
If probe fails in virtio_usb_find_vqs(), virtio_usb_rsw_init(),
virtio_usb_dc_event_populate(), or partway through the port role loop
above, on_error calls virtio_usb_remove(), which invokes every queue's
stop() callback and calls virtio_usb_hc_vp_deinit() for all nports.
At that point INIT_WORK() has not yet run for vq_host_data_rx_work and
vq_host_evt_work (or vq_dev_event_work and vq_dev_data_rx_work on failures
before device_role setup), and spin_lock_init() has not run for the
remaining ports' vhc_lock, triggering WARN_ON(!work->func) in
__flush_work() and spinlock bad-magic splats in
virtio_usb_hc_dataq_stop_cb() and virtio_usb_hc_vp_deinit().
[ ... ]
> @@ -269,13 +326,9 @@ static int virtio_usb_probe(struct virtio_device *vdev)
> }
> }
>
> - if (vusb->device_role) {
> - INIT_WORK(&vusb->vq_dev_data_rx_work, virtio_usb_dc_data_work);
> - INIT_WORK(&vusb->vq_dev_event_work, virtio_usb_dc_event_work);
> -
> - for (i = 0; i < vusb->nports; i++) {
> - if (vusb->vports[i].role != VIRTIO_USB_ROLE_DEVICE)
> - continue;
> + for (i = 0; i < vusb->nports; i++) {
> + if (vusb->vports[i].is_dual_role ||
> + vusb->vports[i].role == VIRTIO_USB_ROLE_DEVICE) {
> rc = virtio_usb_dc_init(vusb, i);
> if (rc) {
> dev_err(&vdev->dev,
[Severity: Medium]
Can virtio_usb_dc_event_process() dereference a NULL vudc when an event
arrives for a non-dual-role host port?
virtio_usb_dc_init() is only called for device-role or dual-role ports,
leaving vusb->vports[port_id].vudc NULL on host-only ports (and on all
ports between virtio_device_ready() and this loop), but
virtio_usb_dc_event_process() unconditionally dereferences vudc:
drivers/usb/virtio_usb/device.c:virtio_usb_dc_event_process() {
...
vudc = vusb->vports[port_id].vudc;
driver = vudc->driver;
...
}
Also, in virtio_usb_hc_event_populate(), the return value of
virtio_usb_events_alloc() is passed directly to
virtio_usb_events_populate() without a NULL check, which will dereference
events[0] on allocation failure.
> @@ -284,17 +337,27 @@ static int virtio_usb_probe(struct virtio_device *vdev)
[ ... ]
> - virtio_device_ready(vdev);
> + if (vusb->host_role && vusb->device_role)
> + if (virtio_usb_rsw_sysfs_init(vusb))
> + goto on_error;
[Severity: Medium]
Does a failure in virtio_usb_rsw_sysfs_init() cause virtio_usb_probe() to
tear down the device and still return 0?
rc is 0 entering this check, and the return value of
virtio_usb_rsw_sysfs_init() is not assigned to rc before jumping to
on_error. on_error calls virtio_usb_remove(vdev), which frees the
virtqueues via vdev->config->del_vqs(vdev), and then returns 0 to the
driver core, leading to a double teardown and use-after-free when the
device is later unbound.
> diff --git a/drivers/usb/virtio_usb/device.c b/drivers/usb/virtio_usb/device.c
> index af82a0e7e27e6..0d44cb1dce6e4 100644
> --- a/drivers/usb/virtio_usb/device.c
> +++ b/drivers/usb/virtio_usb/device.c
> @@ -1644,6 +1644,8 @@ static void virtio_usb_dc_event_process(struct virtio_usb_event *event)
> break;
> }
> case VIRTIO_USB_EVT_DEV_DISCONNECTED: {
> + pr_info("%s port_id %d VIRTIO_USB_EVT_DEV_DISCONNECTED\n",
> + __func__, port_id);
> if (vudc->driver && vudc->driver->disconnect)
> vudc->driver->disconnect(&vudc->gadget);
> if (vudc->registered)
[Severity: High]
Does virtio_ep_disable() hand in-flight request buffers back to the gadget
driver while their scatterlists are still queued in the data virtqueue?
drivers/usb/virtio_usb/device.c:virtio_ep_disable() {
...
list_for_each_entry_safe(vreq, vreq_tmp, &vep->req_queue, list) {
priv = virtio_usb_data_priv(vreq);
priv->req.status = -ESHUTDOWN;
priv->completed_early = true;
list_del_init(&vreq->list);
list_add_tail(&vreq->list, &giveback_inflight);
}
...
list_for_each_entry_safe(vreq, vreq_tmp, &giveback_inflight, list) {
struct virtio_usb_dc_priv *p = virtio_usb_data_priv(vreq);
list_del_init(&vreq->list);
usb_gadget_giveback_request(&vep->ep, &p->req);
}
}
Once usb_gadget_giveback_request() returns, the function driver regains
ownership of req->buf and req->sg and may free or reuse them while the
virtio backend can still read or write those descriptors before
vep_disable_work() runs, causing a DMA use-after-free.
[Severity: High]
Can virtio_usb_dc_cancel_work() or virtio_usb_dc_complete_req()
dereference vudc->veps after VIRTIO_USB_EVT_DEV_UNBIND frees it?
drivers/usb/virtio_usb/device.c:virtio_usb_dc_event_process() {
...
if (vudc->veps) {
unsigned int i;
flush_workqueue(vudc->ep_cmd_wq);
for (i = 0; i < vudc->neps; i++) {
cancel_work_sync(&vudc->veps[i].enable_work);
cancel_work_sync(&vudc->veps[i].disable_work);
}
kfree(vudc->veps);
vudc->veps = NULL;
}
}
Request cancel_work items are queued on system_percpu_wq via
schedule_work(&priv->cancel_work) rather than on vudc->ep_cmd_wq, and
in-flight requests detached by virtio_ep_disable() with
priv->completed_early = true remain in the data virtqueue until
virtio_usb_dc_complete_req() runs. If either runs after kfree(vudc->veps),
it reads priv->vep and dereferences vep->vudc and vep->req_queue in freed
memory.
[Severity: High]
Can virtio_ep_disable() sleep in atomic context when vep->state is
EP_ENABLING?
drivers/usb/virtio_usb/device.c:virtio_ep_disable() {
...
spin_lock_irqsave(&vudc->lock, flags);
if (vep->state == EP_ENABLING) {
spin_unlock_irqrestore(&vudc->lock, flags);
cancel_work_sync(&vep->enable_work);
spin_lock_irqsave(&vudc->lock, flags);
}
}
usb_ep_disable() is permitted in atomic context, and gadget drivers such
as the composite framework call it while holding spin_lock_irqsave(&cdev->lock, ...).
Dropping vudc->lock leaves the caller's spinlocks held and interrupts
disabled while cancel_work_sync() sleeps.
[Severity: High]
Does virtio_usb_dc_wait_ep0_idle() return early when an EP0 request is
deferred on vep->pend_queue?
When a control request enables non-zero endpoints, virtio_ep_queue() places
the EP0 response on vep->pend_queue while any endpoint is in EP_ENABLING:
drivers/usb/virtio_usb/device.c:virtio_ep_queue() {
...
case EP_ENABLED:
if (!vep->ep_id && virtio_usb_dc_ep_enabling_locked(vudc)) {
virtio_usb_data_ref(vreq);
req->actual = 0;
req->status = -EINPROGRESS;
list_add_tail(&vreq->list, &vep->pend_queue);
spin_unlock_irqrestore(&vudc->lock, flags);
return 0;
}
}
However, virtio_usb_dc_wait_ep0_idle() checks only
virtio_usb_dc_req_queue_empty(), which tests list_empty(&vep->req_queue)
and ignores vep->pend_queue, allowing a subsequent
VIRTIO_USB_EVT_DEV_SETUP event to overwrite vep->setup and call
driver->setup() before the previous control transfer's EP0 request has
completed.
[Severity: High]
Does vep_disable_work() send VIRTIO_USB_EP_DIR_OUT when disabling an IN
endpoint?
drivers/usb/virtio_usb/device.c:vep_disable_work() {
...
endpoint = vep->ep_id |
(usb_ep_dir_in(&vep->ep) ? VIRTIO_USB_EP_DIR_IN :
VIRTIO_USB_EP_DIR_OUT);
}
Standard gadget function drivers (such as f_ecm, f_ncm, f_rndis, and
u_ether) clear ep->desc = NULL immediately after usb_ep_disable() returns.
Because vep_disable_work() runs asynchronously on vudc->ep_cmd_wq,
usb_ep_dir_in() sees ep->desc == NULL and falls back to returning 0 (OUT),
so VIRTIO_USB_CMD_DEV_EP_DISABLE is sent with the wrong direction for IN
endpoints.
[Severity: High]
Can virtio_ep_queue() queue a request onto an endpoint that was disabled
concurrently?
CPU0 CPU1
virtio_ep_queue()
spin_lock_irqsave(&vudc->lock)
/* sees vep->state == EP_ENABLED */
spin_unlock_irqrestore(&vudc->lock)
virtio_ep_disable()
spin_lock_irqsave(&vudc->lock)
vep->state = EP_DISABLED
/* drains pend_queue & req_queue */
spin_unlock_irqrestore(&vudc->lock)
do_real_queue()
spin_lock_irqsave(&vudc->lock)
list_add_tail(&vreq->list,
&vep->req_queue) <-- queued on disabled ep
spin_unlock_irqrestore(&vudc->lock)
virtio_usb_data_send(...)
Because do_real_queue() does not recheck vep->state under vudc->lock, a
request racing with virtio_ep_disable() (from virtio_ep_queue() or
vep_enable_work()) is submitted after usb_ep_disable() has already
completed and returned to the gadget driver.
[Severity: High]
Can an error in do_real_queue() corrupt virtio_ep_disable()'s stack-local
giveback_inflight list?
CPU0 CPU1
do_real_queue()
spin_lock_irqsave(&vudc->lock)
list_add_tail(&vreq->list,
&vep->req_queue)
spin_unlock_irqrestore(&vudc->lock)
virtio_ep_disable()
spin_lock_irqsave(&vudc->lock)
list_del_init(&vreq->list)
list_add_tail(&vreq->list,
&giveback_inflight)
spin_unlock_irqrestore(&vudc->lock)
virtio_usb_data_send() -> fails
spin_lock_irqsave(&vudc->lock)
list_del_init(&vreq->list)
list_for_each_entry_safe(...,
&giveback_inflight, list)
<-- unlocked list corruption
When virtio_usb_data_send() fails after virtio_ep_disable() has moved
vreq->list onto giveback_inflight, do_real_queue() runs
list_del_init(&vreq->list) while virtio_ep_disable() traverses
giveback_inflight without vudc->lock, corrupting giveback_inflight and
returning an error from usb_ep_queue() for a request that
virtio_ep_disable() also gives back.
[Severity: High]
Does cancelling vep->enable_work in virtio_ep_disable() leave deferred EP0
requests stranded in vudc->veps[0].pend_queue?
While any endpoint is in EP_ENABLING, virtio_ep_queue() defers EP0
requests onto vudc->veps[0].pend_queue, relying on vep_enable_work() to
call virtio_usb_dc_flush_ep0_pend(vudc) once enabling finishes. When
virtio_ep_disable() cancels vep->enable_work via cancel_work_sync() and
sets vep->state = EP_DISABLED without calling
virtio_usb_dc_flush_ep0_pend(vudc), nothing flushes
vudc->veps[0].pend_queue, hanging EP0 control transfers.
[ ... ]
> @@ -1968,6 +1973,9 @@ static void virtio_usb_dc_evt_notify_cb(struct virtqueue *vqueue)
> {
> struct virtio_usb *vusb = vqueue->vdev->priv;
>
> + if (!READ_ONCE(vusb->started))
> + return;
> +
> schedule_work(&vusb->vq_dev_event_work);
> }
[Severity: Medium]
Should virtio_usb_dc_data_notify_cb() also check READ_ONCE(vusb->started)
before scheduling vq_dev_data_rx_work?
All of the other queue notification callbacks check vusb->started, but
virtio_usb_dc_data_notify_cb() still unconditionally calls
schedule_work(&vusb->vq_dev_data_rx_work), which allows a virtusb-dev-data
interrupt arriving after cancel_work_sync(&vusb->vq_dev_data_rx_work) in
virtio_usb_dc_dataq_stop_cb() to reschedule the worker during teardown.
Also, in virtio_usb_probe(), virtio_usb_rsw_sysfs_init() registers the role
switches before WRITE_ONCE(vusb->started, true), so a sysfs callback can
run before queue notifications are enabled.
> diff --git a/drivers/usb/virtio_usb/host.c b/drivers/usb/virtio_usb/host.c
> index b072c32ac0ee9..27b45774b1055 100644
> --- a/drivers/usb/virtio_usb/host.c
> +++ b/drivers/usb/virtio_usb/host.c
[ ... ]
> @@ -1151,13 +1156,26 @@ int virtio_usb_hc_vp_init(struct virtio_usb *vusb, unsigned int vp_idx)
> for (i = 0; i < VIRTIO_USB_VQ_HOST_MAX; i++)
> vhcd_vp->hcqs[i] = &vusb->vqueues[vusb->host_vq_base + i];
>
> - /* Install into the port before add_hcd so vhcd_vp->vusb is set */
> - vusb->vports[vp_idx].vhc = vhcd_vp;
> + /* Install into the port before add_hcd so vhcd_vp->vusb is set.
> + * Replay any port event that arrived before this, under the same
> + * lock, so that it is applied before any event that sees vhc.
> + * hs/ss do not exist yet, so the replay only sets port status;
> + * the hub driver reads it when usb_add_hcd() registers the root
> + * hub.
> + */
> + spin_lock(&vport->vhc_lock);
> + vport->vhc = vhcd_vp;
> + for_each_set_bit(i, &vport->pending_evt_mask, VIRTIO_USB_VP_MAX_PORTS)
> + virtio_usb_hc_port_event_apply(vhcd_vp, &vport->pending_evt[i]);
> + vport->pending_evt_mask = 0;
> + spin_unlock(&vport->vhc_lock);
>
> /* Add HCDs first so hs/ss are valid before any PORT_CONNECTED event */
> rc = virtio_usb_add_hcd(vusb, vhcd_vp);
> if (rc) {
> + spin_lock(&vusb->vports[vp_idx].vhc_lock);
> vusb->vports[vp_idx].vhc = NULL;
> + spin_unlock(&vusb->vports[vp_idx].vhc_lock);
> return rc;
> }
[Severity: High]
Can publishing vport->vhc before virtio_usb_add_hcd() cause
virtio_usb_hc_evt_process_one() to use an unregistered or freed usb_hcd?
CPU0 CPU1
virtio_usb_hc_vp_init()
spin_lock(&vport->vhc_lock)
vport->vhc = vhcd_vp
spin_unlock(&vport->vhc_lock)
virtio_usb_add_hcd()
vhcd_vp->hs = usb_create_hcd(...)
usb_add_hcd(vhcd_vp->hs)
...
usb_add_hcd(vhcd_vp->ss) -> fails
virtio_usb_hc_evt_process_one()
spin_lock(&vport->vhc_lock)
vhcd_vp = vport->vhc
spin_unlock(&vport->vhc_lock)
virtio_usb_hc_port_event_apply()
hcd = vhcd_vp->hs
usb_remove_hcd(vhcd_vp->hs)
usb_put_hcd(vhcd_vp->hs)
usb_hcd_poll_rh_status(hcd)
<-- use-after-free
Because vport->vhc is already non-NULL while virtio_usb_add_hcd() runs, any
host port event arriving in that window bypasses pending_evt deferral and
calls virtio_usb_hc_port_event_apply(), which can miss polling root hubs
that are not yet created or dereference vhcd_vp->hs / vhcd_vp->ss after the
error path in virtio_usb_add_hcd() frees them with usb_put_hcd().
> @@ -1173,7 +1191,11 @@ int virtio_usb_hc_vp_init(struct virtio_usb *vusb, unsigned int vp_idx)
> */
> int virtio_usb_hc_vp_deinit(struct virtio_usb *vusb, unsigned int vp_idx)
> {
> - struct virtio_usb_hc_vp *vhcd_vp = vusb->vports[vp_idx].vhc;
> + struct virtio_usb_hc_vp *vhcd_vp;
> +
> + spin_lock(&vusb->vports[vp_idx].vhc_lock);
> + vhcd_vp = vusb->vports[vp_idx].vhc;
> + spin_unlock(&vusb->vports[vp_idx].vhc_lock);
>
> if (!vhcd_vp)
> return 0;
> @@ -1186,7 +1208,9 @@ int virtio_usb_hc_vp_deinit(struct virtio_usb *vusb, unsigned int vp_idx)
> vhcd_vp->ss = NULL;
> vhcd_vp->hs = NULL;
>
> + spin_lock(&vusb->vports[vp_idx].vhc_lock);
> vusb->vports[vp_idx].vhc = NULL;
> + spin_unlock(&vusb->vports[vp_idx].vhc_lock);
> return 0;
> }
[ ... ]
> @@ -1226,16 +1247,55 @@ static void virtio_usb_hc_evt_process_one(struct virtio_usb_event *uevent)
> return;
> }
>
> + /* vhc can be concurrently init/deinit'd by a role switch
> + * (virtio_usb_rsw_event_process() runs on its own workqueue) -
> + * vhc_lock protects only the pointer read itself; the vhcd_vp it
> + * points to remains valid for as long as we hold a reference to
> + * it here, since virtio_usb_hc_vp_deinit() only clears the
> + * vports[vp_idx].vhc pointer under the same lock, it does not
> + * free vhcd_vp itself (devm-managed, freed at device teardown).
> + */
> + spin_lock(&vusb->vports[vp_idx].vhc_lock);
> vhcd_vp = vusb->vports[vp_idx].vhc;
> if (!vhcd_vp) {
> - dev_err_ratelimited(
> - &vusb->vdev->dev,
> - "virtio_usb: PORT event vp_idx %u not host-role, ignoring\n",
> - vp_idx);
> + /* No HCD (yet): on a switch to host role the device can
> + * report the port before the role switch path has created
> + * it. Keep the latest event per port for
> + * virtio_usb_hc_vp_init() to replay.
> + */
> + vusb->vports[vp_idx].pending_evt[port_id] = *evt;
> + __set_bit(port_id, &vusb->vports[vp_idx].pending_evt_mask);
> + spin_unlock(&vusb->vports[vp_idx].vhc_lock);
> + dev_dbg(&vusb->vdev->dev,
> + "PORT event vp_idx %u port %u before HCD, deferred\n",
> + vp_idx, port_id);
> return;
> }
> + spin_unlock(&vusb->vports[vp_idx].vhc_lock);
>
> - port = &vhcd_vp->ports[port_id];
> + virtio_usb_hc_port_event_apply(vhcd_vp, evt);
> +}
[Severity: High]
Can virtio_usb_hc_port_event_apply() dereference a freed struct usb_hcd
when racing with virtio_usb_hc_vp_deinit() during a switch to device role?
CPU0 CPU1
virtio_usb_hc_evt_process_one()
spin_lock(&vport->vhc_lock)
vhcd_vp = vport->vhc
spin_unlock(&vport->vhc_lock)
virtio_usb_hc_port_event_apply()
spin_lock_irqsave(&port->lock)
hcd = vhcd_vp->hs
spin_unlock_irqrestore(&port->lock)
virtio_usb_hc_vp_deinit()
usb_remove_hcd(vhcd_vp->ss)
usb_put_hcd(vhcd_vp->ss)
usb_remove_hcd(vhcd_vp->hs)
usb_put_hcd(vhcd_vp->hs)
usb_hcd_poll_rh_status(hcd) <-- use-after-free
Although vhcd_vp itself is devm-allocated, vhcd_vp->ss and vhcd_vp->hs are
freed by usb_put_hcd() in virtio_usb_hc_vp_deinit() while vport->vhc is
still non-NULL and after virtio_usb_hc_evt_process_one() has already
dropped vhc_lock.
> diff --git a/drivers/usb/virtio_usb/role_switch.c b/drivers/usb/virtio_usb/role_switch.c
> index 50b5d645abf4b..84387d20b96c8 100644
> --- a/drivers/usb/virtio_usb/role_switch.c
> +++ b/drivers/usb/virtio_usb/role_switch.c
[ ... ]
> + if (role != old_role && role == VIRTIO_USB_ROLE_HOST) {
> + rc = virtio_usb_hc_vp_init(vusb, port_id);
> + if (rc) {
> + pr_err("%s virtio_usb_hc_vp_init() port=%u error(%d)\n",
> + __func__, port_id, rc);
> + /* Leave vport->role at its old value: reporting a
> + * host role with no working HCD behind it would be
> + * worse than not switching at all.
> + */
> + return rc;
> + }
> + } else if (role != old_role && old_role == VIRTIO_USB_ROLE_HOST) {
> + virtio_usb_hc_vp_deinit(vusb, port_id);
> + }
[Severity: High]
Does calling virtio_usb_hc_vp_init() on every transition to
VIRTIO_USB_ROLE_HOST leak devm memory across runtime role switches?
virtio_usb_hc_vp_init() allocates struct virtio_usb_hc_vp with
devm_kzalloc(&vusb->vdev->dev, ...), and virtio_usb_add_hcd() allocates the
HCD bus name string with devm_kasprintf(&vusb->vdev->dev, ...). Because
virtio_usb_hc_vp_deinit() simply sets vusb->vports[vp_idx].vhc = NULL
without freeing or reusing those devm allocations, every switch to host
role accumulates another struct virtio_usb_hc_vp and bus name string on
vusb->vdev->dev until the virtio device itself is unbound.
> +
> + WRITE_ONCE(vport->role, role);
> + if (role != old_role)
> + pr_info("%s port %u: the new role (%s) is set\n", __func__,
> + port_id,
> + role == VIRTIO_USB_ROLE_DEVICE ?
> + "VIRTIO_USB_ROLE_DEVICE" :
> + "VIRTIO_USB_ROLE_HOST");
> +
> + return 0;
> +}
> +
> +static int vusb_set_role_cb(struct usb_role_switch *sw, enum usb_role role)
> +{
> + struct virtio_usb_port *vport = usb_role_switch_get_drvdata(sw);
> + struct virtio_usb_port_rsw *vport_rsw = vport->rsw;
> + int rc, sync_rc;
> + u32 target;
> +
> + pr_info("%s set_role to %s\n", __func__, role_switch_role_names[role]);
> +
> + switch (role) {
> + case USB_ROLE_HOST:
> + target = VIRTIO_USB_ROLE_HOST;
> + break;
> + case USB_ROLE_DEVICE:
> + target = VIRTIO_USB_ROLE_DEVICE;
> + break;
> + default:
> + /* A virtio-usb port is always either host or device. */
> + return -EINVAL;
> + }
> +
> + /* The device completes the transition before it answers ROLE_SET,
> + * so do the whole switch here, under the port lock: a concurrent
> + * request or ROLE_CHANGE event waits for it and then sees the new
> + * role. ROLE_SET is sent even if the cached role already matches,
> + * so a role change whose event was lost is picked up here too.
> + *
> + * Resync even if ROLE_SET failed: on a timeout or a signal the
> + * device may still have switched (or be about to), and the cached
> + * role and the HCD must follow what the device actually reports.
> + * Report the ROLE_SET error to the caller in that case.
> + */
> + mutex_lock(&vport_rsw->lock);
> + rc = virtio_usb_rsw_role_set(vport_rsw->vusb, vport_rsw->port_id,
> + target);
> + sync_rc =
> + virtio_usb_rsw_sync_locked(vport_rsw->vusb, vport_rsw->port_id);
> + mutex_unlock(&vport_rsw->lock);
> +
> + return rc ? rc : sync_rc;
> +}
[Severity: Medium]
Does the ROLE_GET resync in virtio_usb_rsw_sync_locked() fail immediately
when virtio_usb_rsw_role_set() was interrupted by a signal?
Both virtio_usb_rsw_role_set() and virtio_usb_rsw_get_role() call
virtio_usb_cmd_send_sync() -> virtio_usb_cmd_send(), which waits with
wait_for_completion_interruptible_timeout(). When a signal interrupts
virtio_usb_rsw_role_set(), TIF_SIGPENDING remains set on current, so the
follow-up wait_for_completion_interruptible_timeout() in
virtio_usb_rsw_get_role() returns -ERESTARTSYS immediately without waiting
for the ROLE_GET reply, leaving vport->role and the port's HCD out of sync
if the device completed the switch.
[ ... ]
> +static int rsw_vport_init(struct virtio_usb *vusb, int port_id)
> +{
> + struct device *dev = &vusb->vdev->dev;
> + struct virtio_usb_port *vport = &vusb->vports[port_id];
> + struct virtio_usb_port_rsw *vport_rsw = vport->rsw;
> + struct usb_role_switch_desc desc = { 0 };
> +
> + /* Reuse the devm allocation of an earlier probe of this device. */
> + if (!vport_rsw) {
> + vport_rsw = devm_kzalloc(dev, sizeof(*vport_rsw), GFP_KERNEL);
> + if (!vport_rsw)
> + return -ENOMEM;
> + mutex_init(&vport_rsw->lock);
> + }
> +
> + vport_rsw->vusb = vusb;
> + vport_rsw->port_id = port_id;
> + vport_rsw->sysfs_name = kasprintf(GFP_KERNEL, "port%u", port_id);
> +
> + desc.name = vport_rsw->sysfs_name;
> + desc.set = vusb_set_role_cb;
> + desc.get = vusb_get_role_cb;
> + desc.driver_data = vport;
> + desc.fwnode = NULL;
> + desc.allow_userspace_control = true;
> +
> + /* vusb_set_role_cb() can be called as soon as the switch is
> + * registered, so vport->rsw must already be set.
> + */
> + vport->rsw = vport_rsw;
> + vport_rsw->sw = usb_role_switch_register(dev, &desc);
[Severity: Medium]
Can rsw_vport_init() fail with -EEXIST across multiple virtio-usb devices
or when kasprintf() returns NULL?
Naming the switch "port%u" without including dev_name(dev) registers
"port0-role-switch" in the global usb_role class, which collides with
-EEXIST when a second dual-role virtio-usb device is probed. In addition,
vport_rsw->sysfs_name is not checked for NULL after kasprintf(), so an
allocation failure causes usb_role_switch_register() to fall back to the
parent device name for the first port and fail with -EEXIST on the second.
Separately, in vusb_set_role_cb(), role_switch_role_names[role] is indexed
before switch (role) validates that role is within bounds.
--
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1796144390.git.igor.skalkin@oss.qualcomm.com?part=6
next prev parent reply other threads:[~2026-12-01 18:00 UTC|newest]
Thread overview: 37+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-24 16:08 [PATCH 0/8] virtio-usb: add dual-role virtio USB driver Igor Skalkin
2026-09-24 16:09 ` [PATCH 1/8] virtio-usb: add protocol header and skeleton dual-role driver Igor Skalkin
2026-09-25 5:14 ` Greg Kroah-Hartman
2026-09-28 14:19 ` Igor Skalkin
2026-09-25 5:21 ` Greg Kroah-Hartman
2026-09-28 14:14 ` Igor Skalkin
2026-09-24 16:09 ` [PATCH 2/8] virtio-usb: add host role (USB Host Controller) support Igor Skalkin
2026-09-25 5:18 ` Greg Kroah-Hartman
2026-09-28 14:01 ` Igor Skalkin
2026-09-24 16:09 ` [PATCH 3/8] virtio-usb: add device role (USB Device " Igor Skalkin
2026-09-24 16:09 ` [PATCH 4/8] virtio-usb: add OTG role query support Igor Skalkin
2026-09-24 16:09 ` [PATCH 5/8] virtio-usb: add USB On-The-Go role-switching support Igor Skalkin
2026-09-24 16:09 ` [PATCH 6/8] virtio-usb: rework endpoint lifecycle to an async split-phase state machine Igor Skalkin
2026-09-24 16:09 ` [PATCH 7/8] virtio-usb: add SuperSpeed device-role support Igor Skalkin
2026-09-24 16:09 ` [PATCH 8/8] virtio-usb: support a guest UDC name prefix from the bind event Igor Skalkin
2026-09-25 5:17 ` [PATCH 0/8] virtio-usb: add dual-role virtio USB driver Greg Kroah-Hartman
2026-09-28 13:55 ` Igor Skalkin
2026-09-28 14:44 ` Greg Kroah-Hartman
2026-09-28 15:56 ` Igor Skalkin
2026-09-28 16:10 ` Greg Kroah-Hartman
2026-09-29 9:47 ` Michael S. Tsirkin
2026-09-29 16:01 ` Greg Kroah-Hartman
2026-09-29 19:02 ` Vasilii Ianikeev
2026-09-29 19:58 ` Vasilii Ianikeev
2026-12-01 17:35 ` [RFC PATCH v2 0/7] " Igor Skalkin
2026-12-01 17:35 ` [RFC PATCH v2 1/7] uapi: virtio_ids: reserve ID for virtio USB device Igor Skalkin
2026-12-01 17:35 ` [RFC PATCH v2 2/7] virtio-usb: add protocol header and skeleton dual-role driver Igor Skalkin
2026-12-01 17:52 ` sashiko-bot
2026-12-01 17:35 ` [RFC PATCH v2 3/7] virtio-usb: add host role (USB Host Controller) support Igor Skalkin
2026-12-01 17:35 ` [RFC PATCH v2 4/7] virtio-usb: add device role (USB Device " Igor Skalkin
2026-12-01 17:35 ` [RFC PATCH v2 5/7] virtio-usb: add per-port role query support Igor Skalkin
2026-12-01 17:54 ` sashiko-bot
2026-12-01 17:35 ` [RFC PATCH v2 6/7] virtio-usb: add host/device role switch support Igor Skalkin
2026-12-01 18:00 ` sashiko-bot [this message]
2026-12-01 17:35 ` [RFC PATCH v2 7/7] virtio-usb: support a guest UDC name prefix from the bind event Igor Skalkin
2026-12-01 17:50 ` sashiko-bot
2026-12-01 17:49 ` [RFC PATCH v2 0/7] virtio-usb: add dual-role virtio USB driver Michael S. Tsirkin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=sashiko-outbox-243921@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=eperezma@redhat.com \
--cc=igor.skalkin@oss.qualcomm.com \
--cc=linux-usb@vger.kernel.org \
--cc=mst@redhat.com \
--cc=sashiko-reviews@lists.linux.dev \
--cc=virtualization@lists.linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox