From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796148004; b=4anbAeFPDC/WWnTHXNWzIaBfAu7V9OhQ8jQet4x9OVcUosJ43NzVOLOKt6cbPlzB/ujv nTIQdxTocbbV5mgc6x8FT4j1soe5Su5wsGatx4UpZPcYk/0jy8VWoY2qBpBYbdwtJcGmK iQSxYC/O4TEmbOwg9BD1iv2Z7+50T2MyD9kdNbonnsSu/7Gqa7xcvq9hrVaVU6rYDPtiS Nmejcdav70wllfbSQXhBQoOQItj+gg7a8AXo3MrDV/tAmdBtRHiRxkL3TYzHpayLJOlGL THOI3TOwBfV0+6BVpuObjdrZl618mCI7rhw5FI9TURnDzFTQy+y3GNsPqA0R/jRwRJg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796148004; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=iop5Mb52Xhz2YRnrDF8eXBTjmukGAw9RHO+69/nMBpQ=; b=dDWuL95ADgEsFxtCbyg/K2PkN1KfbOzdnKkLR9SqOwy7FJ+/xjAwCXpjUS2UZiaFiJ2T qBek2HaiUx66dIsI9EnInNMbNuKP7Qx8SfqJ6tCzgXdF0KjJxlAXTsu8i2EPOcjJEbwrA afzB5eWTCORAeYv/jwmX6Y/oUg56OpZenc6gE9DuFyP5JfLh2MwG1LKd8dpfR7tlXh0Ll 4wV/dOx1dsijjkh+PcqY9VoLaiMIjXzupTySq3+p8ucx3go32VPcn8PDf2yPpYAwYFekf 8zbX8gGia+yMg7RYCXSP8qof2sdOhSzdyRPksKrXPNHLiPJ2ZxRRcLowRZt0/UGw+bA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=dYgcyIC3; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E9CD2606A322; Tue, 1 Dec 2026 18:00:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=dYgcyIC3 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 222F91F000C1; Tue, 1 Dec 2026 18:00:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796148003; bh=iop5Mb52Xhz2YRnrDF8eXBTjmukGAw9RHO+69/nMBpQ=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=dYgcyIC38JLMLNIxqZL6p8N4klSbzOt8EdiGfrVcOiEmkXDKUgkHSkKf7yfecQXB2 /+yes9FGSZTEXTqIQVAQvl9U4Hppv9R3kL+jM+BoxWFlqAx33OdXrkLO28K5oBP4s8 5uqFEqAiOwTN4qXooleYGm2GVNZmH1gTYtwZK//ZUCRGCCIlyxJMZkgwd1sIaVOfEf 3OGRV0zSkJscrClw7MkIeVmdEhmzplaxt5iwnHDuMrqB4/uk+oPCpDdUKOgQjQcIjb v1dhoJx+cGV3T1uD9g/6/b9zuboS/KpVo39ET2YcNGpJqvMcrcmwdoV1qANFtALCJg Rl3nqgaE/jOfA== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [RFC PATCH v2 6/7] virtio-usb: add host/device role switch support Reply-To: sashiko-reviews@lists.linux.dev To: "Igor Skalkin" Cc: virtualization@lists.linux.dev, linux-usb@vger.kernel.org, "Michael S. Tsirkin" , "Eugenio Perez" In-Reply-To: References: <20260924160907.145405-1-igor.skalkin@oss.qualcomm.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Tue, 01 Dec 2026 18:00:02 +0000 X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list 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 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 *v= dev) > } > } > =20 > + 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 =3D 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 ambi= guous > + * (host|device). > */ > for (i =3D 0; i < vusb->nports; i++) { > + vusb->vports[i].vusb =3D vusb; > + spin_lock_init(&vusb->vports[i].vhc_lock); > + > if (vusb->host_role && !vusb->device_role) { > vusb->vports[i].role =3D VIRTIO_USB_ROLE_HOST; > } else if (vusb->device_role && !vusb->host_role) { > vusb->vports[i].role =3D VIRTIO_USB_ROLE_DEVICE; > } else { > - u32 status, role; > + u32 status, role, supported_role; > =20 > - status =3D virtio_usb_rsw_get_role(vusb, i, &role); > + status =3D virtio_usb_rsw_get_role(vusb, i, &role, > + &supported_role); > if (status !=3D 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 *vd= ev) > goto on_error; > } > vusb->vports[i].role =3D role; > + vusb->vports[i].is_dual_role =3D > + 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 =3D -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 *vd= ev) > } > } > =20 > - 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 =3D 0; i < vusb->nports; i++) { > - if (vusb->vports[i].role !=3D VIRTIO_USB_ROLE_DEVICE) > - continue; > + for (i =3D 0; i < vusb->nports; i++) { > + if (vusb->vports[i].is_dual_role || > + vusb->vports[i].role =3D=3D VIRTIO_USB_ROLE_DEVICE) { > rc =3D 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 =3D vusb->vports[port_id].vudc; driver =3D 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 *v= dev) [ ... ] > - 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/dev= ice.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 virt= io_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 =3D virtio_usb_data_priv(vreq); priv->req.status =3D -ESHUTDOWN; priv->completed_early =3D 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 =3D 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 =3D 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 =3D 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 =3D 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 =3D=3D 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->l= ock, ...). 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 =3D 0; req->status =3D -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 =3D 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 =3D 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 =3D=3D NULL and falls back to returning 0 (OU= T), 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 =3D=3D EP_ENABLED */ spin_unlock_irqrestore(&vudc->lock) virtio_ep_disable() spin_lock_irqsave(&vudc->lock) vep->state =3D 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 =3D 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 virt= queue *vqueue) > { > struct virtio_usb *vusb =3D vqueue->vdev->priv; > =20 > + 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 =3D 0; i < VIRTIO_USB_VQ_HOST_MAX; i++) > vhcd_vp->hcqs[i] =3D &vusb->vqueues[vusb->host_vq_base + i]; > =20 > - /* Install into the port before add_hcd so vhcd_vp->vusb is set */ > - vusb->vports[vp_idx].vhc =3D 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 =3D 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 =3D 0; > + spin_unlock(&vport->vhc_lock); > =20 > /* Add HCDs first so hs/ss are valid before any PORT_CONNECTED event */ > rc =3D virtio_usb_add_hcd(vusb, vhcd_vp); > if (rc) { > + spin_lock(&vusb->vports[vp_idx].vhc_lock); > vusb->vports[vp_idx].vhc =3D 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 =3D vhcd_vp spin_unlock(&vport->vhc_lock) virtio_usb_add_hcd() vhcd_vp->hs =3D 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 =3D vport->vhc spin_unlock(&vport->vhc_lock) virtio_usb_hc_port_event_apply() hcd =3D 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 =3D vusb->vports[vp_idx].vhc; > + struct virtio_usb_hc_vp *vhcd_vp; > + > + spin_lock(&vusb->vports[vp_idx].vhc_lock); > + vhcd_vp =3D vusb->vports[vp_idx].vhc; > + spin_unlock(&vusb->vports[vp_idx].vhc_lock); > =20 > 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 =3D NULL; > vhcd_vp->hs =3D NULL; > =20 > + spin_lock(&vusb->vports[vp_idx].vhc_lock); > vusb->vports[vp_idx].vhc =3D 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; > } > =20 > + /* 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 =3D 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] =3D *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); > =20 > - port =3D &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 =3D vport->vhc spin_unlock(&vport->vhc_lock) virtio_usb_hc_port_event_apply() spin_lock_irqsave(&port->lock) hcd =3D 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_us= b/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 !=3D old_role && role =3D=3D VIRTIO_USB_ROLE_HOST) { > + rc =3D virtio_usb_hc_vp_init(vusb, port_id); > + if (rc) { > + pr_err("%s virtio_usb_hc_vp_init() port=3D%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 !=3D old_role && old_role =3D=3D 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 =3D 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 !=3D old_role) > + pr_info("%s port %u: the new role (%s) is set\n", __func__, > + port_id, > + role =3D=3D 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 ro= le) > +{ > + struct virtio_usb_port *vport =3D usb_role_switch_get_drvdata(sw); > + struct virtio_usb_port_rsw *vport_rsw =3D 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 =3D VIRTIO_USB_ROLE_HOST; > + break; > + case USB_ROLE_DEVICE: > + target =3D 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 =3D virtio_usb_rsw_role_set(vport_rsw->vusb, vport_rsw->port_id, > + target); > + sync_rc =3D > + 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 =3D &vusb->vdev->dev; > + struct virtio_usb_port *vport =3D &vusb->vports[port_id]; > + struct virtio_usb_port_rsw *vport_rsw =3D vport->rsw; > + struct usb_role_switch_desc desc =3D { 0 }; > + > + /* Reuse the devm allocation of an earlier probe of this device. */ > + if (!vport_rsw) { > + vport_rsw =3D devm_kzalloc(dev, sizeof(*vport_rsw), GFP_KERNEL); > + if (!vport_rsw) > + return -ENOMEM; > + mutex_init(&vport_rsw->lock); > + } > + > + vport_rsw->vusb =3D vusb; > + vport_rsw->port_id =3D port_id; > + vport_rsw->sysfs_name =3D kasprintf(GFP_KERNEL, "port%u", port_id); > + > + desc.name =3D vport_rsw->sysfs_name; > + desc.set =3D vusb_set_role_cb; > + desc.get =3D vusb_get_role_cb; > + desc.driver_data =3D vport; > + desc.fwnode =3D NULL; > + desc.allow_userspace_control =3D true; > + > + /* vusb_set_role_cb() can be called as soon as the switch is > + * registered, so vport->rsw must already be set. > + */ > + vport->rsw =3D vport_rsw; > + vport_rsw->sw =3D 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. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/cover.1796144390.gi= t.igor.skalkin@oss.qualcomm.com?part=3D6