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 4/7] virtio-usb: add device role (USB Device Controller) support
Date: Tue, 01 Dec 2026 18:13:59 +0000 [thread overview]
Message-ID: <sashiko-outbox-243949@kernel.org> (raw)
In-Reply-To: <3f0c8f183e321f032bdf30bb06f6837dadb6bc94.1796144390.git.igor.skalkin@oss.qualcomm.com>
Thank you for your contribution! Sashiko AI review found 28 potential
issues to consider:
- [High] usb: virtio_usb: use-after-free of cmd in
virtio_usb_dc_get_frame()
- [High] usb: virtio_usb: sleeping in atomic context in gadget and ep
callbacks
- [High] usb: virtio_usb: use-after-free of vudc->veps after UNBIND or
deinit
- [High] usb: virtio_usb: premature request giveback in
virtio_ep_disable()
- [High] usb: virtio_usb: NULL dereference of vudc->veps and vudc in dc
paths
- [High] usb: virtio_usb: async ep->desc access and skipped cmd in
vep_disable_work()
- [High] usb: virtio_usb: inverted queue/UDC teardown order in
virtio_usb_remove()
- [High] virtio-usb: virtio_usb_dc_wait_ep0_idle() ignores EP0
pend_queue
- [High] virtio-usb: virtio_ep_disable() strands EP0 pend_queue and
races enable
- [High] virtio-usb: lockless req_queue drain in
virtio_usb_dc_dataq_stop_cb()
- [High] virtio-usb: vep_enable_work() maps 9-byte desc and non-linear
pointers
- [High] virtio-usb: virtio_usb_dc_get_endpoint_info() frees active DMA
buffer
- [High] virtio-usb: do_real_queue() does not bound req->sg to num_sgs
or length
- [High] usb: virtio_usb: race in virtio_ep_disable() with
do_real_queue() and enable
- [High] usb: virtio_usb: list corruption between do_real_queue() and
virtio_ep_disable()
- [High] usb: virtio_usb: race on unlocked vudc->driver in
virtio_usb_dc_event_process()
- [High] usb: virtio_usb: NULL dereference of vudc in
virtio_usb_dc_event_process()
- [High] usb: virtio_usb: unbounded actual_length in
virtio_usb_dc_complete_req()
- [High] usb: virtio_usb: zeroing embedded struct usb_gadget across
re-binds
- [High] usb: virtio_usb: spin_unlock_irq() enables IRQs in
virtio_usb_data_send()
- [Medium] usb: virtio_usb: unset ep->address misclassifies IN endpoints
as OUT
- [Medium] usb: virtio_usb: cancel_work_sync() on uninitialized work on
probe error
- [Medium] virtio-usb: EP0 timeout halt in virtio_usb_dc_event_process()
fails
- [Medium] virtio-usb: do_real_queue() omits stream_id for USB bulk
stream requests
- [Medium] usb: virtio_usb: broken state tracking in set_selfpowered()
and pullup()
- [Medium] usb: virtio_usb: unbounded neps and missing actual_length
handling on completion
- [Medium] usb: virtio_usb: missing req->actual update on error in
complete_req()
- [Medium] usb: virtio_usb: platform_device name collision in
virtio_usb_dc_parent_create()
--
commit 587166946657a56cd17b0220cf5570c4fdd876e6
Author: Aiswarya Cyriac <aiswarya.cyriac@oss.qualcomm.com>
virtio-usb: add device role (USB Device Controller) support
This commit adds USB Device Controller (UDC) role support to the
virtio-usb driver, implementing the usb_gadget_ops and usb_ep_ops
callbacks, asynchronous endpoint state handling, and device event
processing.
> diff --git a/drivers/usb/virtio_usb/controller.c b/drivers/usb/virtio_usb/controller.c
> index eb343f026a462..b463513b2da4e 100644
> --- a/drivers/usb/virtio_usb/controller.c
> +++ b/drivers/usb/virtio_usb/controller.c
[ ... ]
> @@ -180,6 +217,30 @@ 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);
[Severity: Medium]
If virtio_usb_find_vqs() or the host_role initialization loop fails and
jumps to on_error before reaching this block, virtio_usb_remove() calls
vusb->vqueues[i].stop() on all nvqs entries. Because
virtio_usb_dc_dataq_stop_cb() and virtio_usb_dc_evtq_stop_cb()
unconditionally call cancel_work_sync() on vq_dev_data_rx_work and
vq_dev_event_work, won't that trigger WARN_ON(!work->func) in
__flush_work()?
[ ... ]
> @@ -209,6 +270,9 @@ static void virtio_usb_remove(struct virtio_device *vdev)
> virtio_usb_hc_vp_deinit(vusb, i);
> }
>
> + if (vusb->device_role && vusb->vports)
> + virtio_usb_dc_deinit(vusb);
> +
[Severity: High]
Does stopping all virtqueues at the start of virtio_usb_remove() before
calling virtio_usb_dc_deinit() invert the UDC teardown order?
virtio_usb_cmdq_stop_cb() disables callbacks on the device command
virtqueue before virtio_usb_dc_deinit() calls usb_del_gadget_udc().
Furthermore, unlike VIRTIO_USB_EVT_DEV_UNBIND, virtio_usb_dc_deinit()
never clears vudc->registered = 0 before usb_del_gadget_udc(). As a
result, gadget driver unbind callbacks (virtio_usb_dc_pullup() and
vep_disable_work()) submit synchronous commands onto the already-stopped
command virtqueue, blocking for VIRTIO_USB_CMD_TIMEOUT_MS per command on
flush_workqueue(vudc->ep_cmd_wq) and leaking the command allocations.
> diff --git a/drivers/usb/virtio_usb/device.c b/drivers/usb/virtio_usb/device.c
> new file mode 100644
> index 0000000000000..af82a0e7e27e6
> --- /dev/null
> +++ b/drivers/usb/virtio_usb/device.c
> @@ -0,0 +1,2016 @@
[ ... ]
> +static unsigned int usb_ep_dir_in(struct usb_ep *ep)
> +{
> + struct virtio_usb_ep *vep = usb_ep_to_virtio_ep(ep);
> + unsigned int direction = 0;
> +
> + if (ep->address == 0)
> + direction = vep->setup.bRequestType & USB_DIR_IN ? 1 : 0;
> + else if (ep->desc && ep->desc->bEndpointAddress)
> + direction = usb_endpoint_dir_in(ep->desc) ? 1 : 0;
> +
> + return direction;
> +}
[Severity: Medium]
Can testing ep->address == 0 misclassify non-EP0 IN endpoints as OUT?
Neither virtio_usb_dc_get_endpoint_info() nor virtio_ep_enable() sets
ep->address. When a gadget driver enables an endpoint without calling
usb_ep_autoconfig_ss() (for example, raw_gadget or gadgetfs),
ep->address remains 0, so usb_ep_dir_in() reads vep->setup.bRequestType
(which is 0 on non-EP0 endpoints) and treats every IN endpoint as
VIRTIO_USB_EP_DIR_OUT. Should this check !vep->ep_id (and also set
ep->address = desc->bEndpointAddress in virtio_ep_enable())?
[ ... ]
> +static bool virtio_usb_dc_req_queue_empty(struct virtio_usb_ep *vep)
> +{
> + struct virtio_usb_dc *vudc = vep->vudc;
> + unsigned long flags;
> + bool empty;
> +
> + spin_lock_irqsave(&vudc->lock, flags);
> + empty = list_empty(&vep->req_queue);
> + spin_unlock_irqrestore(&vudc->lock, flags);
> +
> + return empty;
> +}
> +
> +static int virtio_usb_dc_wait_ep0_idle(struct virtio_usb_dc *vudc)
> +{
> + struct virtio_usb_ep *vep = usb_ep_to_virtio_ep(vudc->gadget.ep0);
> + long ret;
> +
> + if (virtio_usb_dc_req_queue_empty(vep))
> + return 0;
[Severity: High]
Does virtio_usb_dc_req_queue_empty() miss EP0 requests parked on
vep->pend_queue?
When a control request such as SET_CONFIGURATION enables non-EP0
endpoints, those endpoints enter EP_ENABLING, causing virtio_ep_queue()
to park the EP0 status-stage request on veps[0].pend_queue rather than
veps[0].req_queue. If the next VIRTIO_USB_EVT_DEV_SETUP event arrives
before vep_enable_work() flushes veps[0].pend_queue,
virtio_usb_dc_wait_ep0_idle() sees veps[0].req_queue empty and returns 0
immediately. That overwrites vep->setup (corrupting the direction of
the parked EP0 request) and calls driver->setup(), where composite_setup()
re-queues its shared cdev->req while it is still linked on
veps[0].pend_queue.
> +static void virtio_usb_dc_complete_req(struct virtio_usb_data *vreq)
> +{
> + struct virtio_usb_response *response = virtio_usb_data_response(vreq);
> + struct virtio_usb_dc_priv *priv = virtio_usb_data_priv(vreq);
> + unsigned int status = le32_to_cpu(response->status);
> + struct usb_request *req = &priv->req;
> + struct virtio_usb_ep *vep = priv->vep;
> + struct virtio_usb_dc *vudc = vep->vudc;
> + struct virtio_usb_data *vreq_iter;
> + unsigned int found = 0;
> + unsigned long flags;
> + bool wake_ep0 = false;
> + bool early;
> +
> + spin_lock_irqsave(&vudc->lock, flags);
> +
> + list_for_each_entry(vreq_iter, &vep->req_queue, list) {
> + if (vreq_iter == vreq) {
> + found = 1;
> + break;
> + }
> + }
> + early = priv->completed_early;
> + if (!found) {
> + spin_unlock_irqrestore(&vudc->lock, flags);
> + /* Arrived after ep_disable() already gave back */
> + if (early)
> + virtio_usb_data_unref(vreq);
> + return;
> + }
[Severity: High]
Can virtio_usb_dc_complete_req() and virtio_usb_dc_cancel_work()
dereference priv->vep after VIRTIO_USB_EVT_DEV_UNBIND has freed
vudc->veps?
kworker (event) kworker (data / system_percpu_wq)
virtio_usb_dc_event_process()
/* VIRTIO_USB_EVT_DEV_UNBIND */
usb_del_gadget_udc()
...
virtio_ep_disable()
priv->completed_early = true
/* vreq remains in flight on dataq */
kfree(vudc->veps)
virtio_usb_dc_complete_req(vreq)
vep = priv->vep
vudc = vep->vudc <-- use-after-free
In-flight requests unlinked by virtio_ep_disable() (as well as any
in-flight EP0 requests) remain queued in dataq->vqueue across UNBIND,
and priv->cancel_work is scheduled on system_percpu_wq via
schedule_work() rather than vudc->ep_cmd_wq and is never cancelled on
UNBIND or virtio_usb_dc_deinit(). When either work runs after
kfree(vudc->veps), dereferencing priv->vep and iterating
&vep->req_queue accesses freed slab memory.
> + /* Common case - request still belongs to in-flight queue */
> + if (req->status != -ECONNRESET && req->status != -ESHUTDOWN) {
> + req->status = virtio_error_to_usb(status);
> + if (!req->status)
> + req->actual = le32_to_cpu(response->actual_length);
> + }
[Severity: High]
Should le32_to_cpu(response->actual_length) be validated against
req->length before assigning it to req->actual? If the backend reports
an actual_length larger than req->length, gadget function drivers that
consume req->actual bytes will read past the end of req->buf.
[Severity: Medium]
Also, gating req->actual = le32_to_cpu(response->actual_length) behind
if (!req->status) discards the transferred byte count when a transfer
completes with a non-zero status such as VIRTIO_USB_S_ERR_SHORT_PKT
(-EREMOTEIO when req->short_not_ok is set). Unlike
virtio_usb_hc_complete_urb() in host.c, req->actual remains 0 on short
transfers.
[ ... ]
> +static int do_real_queue(struct virtio_usb_ep *vep, struct usb_request *req)
> +{
[ ... ]
> + if (vep->ep_id && vep->ep.desc) {
> + switch (usb_endpoint_type(vep->ep.desc)) {
> + case USB_ENDPOINT_XFER_ISOC:
> + transfer_type = VIRTIO_USB_EP_ISOCHRONOUS;
> + break;
> + case USB_ENDPOINT_XFER_BULK:
> + transfer_type = VIRTIO_USB_EP_BULK;
> + break;
[Severity: Medium]
virtio_usb_dc_get_endpoint_info() populates vep->ep.max_streams from
epinfo[i].max_streams, but do_real_queue() never sets
request->bulk.stream_id = cpu_to_le32(req->stream_id) for
USB_ENDPOINT_XFER_BULK requests. Won't SuperSpeed bulk stream transfers
(such as UASP in f_tcm) always transmit stream_id 0?
> + case USB_ENDPOINT_XFER_INT:
> + transfer_type = VIRTIO_USB_EP_INTERRUPT;
> + break;
> + default:
> + transfer_type = VIRTIO_USB_EP_CONTROL;
> + break;
> + }
> + }
> + request->transfer_type = cpu_to_le16(transfer_type);
> +
> + if (req->length && req->buf) {
> + psg_data = &sg;
> + sg_init_one(psg_data, req->buf, req->length);
> + } else if (req->length && req->num_sgs > 0) {
> + psg_data = req->sg;
> + } else if (req->length && req->sg) {
[Severity: High]
When req->num_sgs > 0, does passing psg_data = req->sg directly to
virtio_usb_data_send() -> virtqueue_add_sgs() walk past req->num_sgs
entries or req->length bytes?
virtqueue_add_sgs() iterates with sg_next() until it hits SG_END and
ignores both req->num_sgs and req->length. Gadget drivers such as
uvc_video_encode_isoc_sg() initialize an sg_table with more entries than
the active req->num_sgs without moving the SG_END marker, and
ffs_epfile_dmabuf_io() bounds transfers via req->length and
req->num_sgs, so walking until SG_END can dereference unpopulated zeroed
SG entries or transfer data past req->length.
[ ... ]
> + spin_lock_irqsave(&vudc->lock, flags);
> + priv->completed_early = false;
> + priv->submitted = false;
> + priv->cancel_pending = false;
> + /* A new tag for every submission: the request may be queued again
> + * while a cancel for its previous submission is still pending or in
> + * flight, and that cancel must not hit this one.
> + */
> + tag = virtio_usb_data_tag_next(vudc->vusb);
> + priv->tag = tag;
> + request->tag = cpu_to_le64(tag);
> + req->actual = 0;
> + req->status = -EINPROGRESS;
> + list_add_tail(&vreq->list, &vep->req_queue);
> + spin_unlock_irqrestore(&vudc->lock, flags);
[Severity: High]
Can do_real_queue() queue a request onto an endpoint that was disabled
after virtio_ep_queue() or vep_enable_work() dropped vudc->lock?
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 req_queue */
spin_unlock_irqrestore(&vudc->lock)
do_real_queue()
spin_lock_irqsave(&vudc->lock)
list_add_tail(&vreq->list, &vep->req_queue)
spin_unlock_irqrestore(&vudc->lock)
virtio_usb_data_send() <-- sent on disabled endpoint
The same race exists with vep_enable_work(), which sets
vep->state = EP_ENABLED before dropping vudc->lock inside its
pend_queue flush loop, so a concurrent virtio_ep_disable() sees
vep->state != EP_ENABLING, skips cancel_work_sync(&vep->enable_work),
and races with vep_enable_work() calling do_real_queue().
> + /*
> + * virtio_ep_dequeue() can see the request on req_queue and schedule
> + * the cancel work before the request reaches the data virtqueue. A
> + * DEV_CANCEL sent that early would not match anything on the device
> + * side, would be answered with success and dropped, and the request
> + * queued right after would never be cancelled. In that case the
> + * cancel work only records the request and this path reschedules it.
> + *
> + * Hold a reference across the window so the request cannot be
> + * completed and freed while this is being decided.
> + */
> + virtio_usb_data_ref(vreq);
> +
> + rc = virtio_usb_data_send(vudc->vusb, vreq, out_sgs, in_sgs);
> + if (rc) {
> + spin_lock_irqsave(&vudc->lock, flags);
> + list_del_init(&vreq->list);
> + spin_unlock_irqrestore(&vudc->lock, flags);
> +
> + virtio_usb_data_unref(vreq);
> + virtio_usb_data_unref(vreq);
> + return rc;
> + }
[Severity: High]
Does calling virtio_usb_data_send() from virtio_ep_queue() ->
do_real_queue() unconditionally enable hardware interrupts when
usb_ep_queue() is called with IRQs disabled?
drivers/usb/virtio_usb/vq_common.c:virtio_usb_data_send() {
...
spin_lock_irq(&queue->lock);
rc = virtqueue_add_sgs(queue->vqueue, psgs, nouts, nins, data,
GFP_ATOMIC);
if (!rc)
notify = virtqueue_kick_prepare(queue->vqueue);
spin_unlock_irq(&queue->lock);
...
}
Gadget function drivers routinely call usb_ep_queue() while holding a
spin_lock_irqsave() lock; spin_unlock_irq(&queue->lock) re-enables local
interrupts while the caller still holds its IRQ-safe lock.
[Severity: High]
Also, if virtio_usb_data_send() fails while a concurrent
virtio_ep_disable() runs, can list_del_init(&vreq->list) corrupt
virtio_ep_disable()'s unlocked giveback_inflight list?
CPU0 CPU1
do_real_queue()
/* adds vreq to vep->req_queue */
/* drops vudc->lock */
rc = virtio_usb_data_send() /* fails */
virtio_ep_disable()
spin_lock_irqsave(&vudc->lock)
/* moves vreq to giveback_inflight */
spin_unlock_irqrestore(&vudc->lock)
list_for_each_entry_safe(...,
&giveback_inflight, list)
spin_lock_irqsave(&vudc->lock)
list_del_init(&vreq->list) <-- mutates unlocked giveback_inflight
CPU0 then drops both references on vreq and returns an error to the
caller of usb_ep_queue(), while CPU1 also gives back the request.
[ ... ]
> +static void vep_enable_work(struct work_struct *work)
> +{
[ ... ]
> + if (virtio_usb_ep_comp_valid(comp))
> + n_sgs = 2;
> +
> + sg_init_table(psg_data, n_sgs);
> + sg_set_buf(&psg_data[0], desc, sizeof(*desc));
> + if (n_sgs == 2)
> + sg_set_buf(&psg_data[1], comp, sizeof(*comp));
> + hdr = virtio_usb_cmd_request(cmd);
> + hdr->endpoint = cpu_to_le16(endpoint);
> +
> + rc = virtio_usb_cmd_send_sync(vudc->vusb, psg_data, NULL, cmd);
[Severity: High]
Can sg_set_buf(&psg_data[0], desc, sizeof(*desc)) read 2 bytes past the
end of desc and fail on module-static descriptors?
sizeof(*desc) is 9 (USB_DT_ENDPOINT_AUDIO_SIZE), whereas standard USB
endpoint descriptors allocated by usb_copy_descriptors() are 7 bytes
(USB_DT_ENDPOINT_SIZE). Mapping 9 bytes reads 2 bytes out of bounds at
the end of a descriptor block and shifts any following SuperSpeed
companion descriptor in psg_data[1] by 2 bytes. In addition, gadget
drivers such as f_printer and f_uac1 pass static endpoint descriptors in
module data/rodata (vmalloc space when built as modules), which cannot be
passed directly to sg_set_buf(). Should virtio_ep_enable() validate
desc->bLength >= USB_DT_ENDPOINT_SIZE and copy the descriptors into the
kmalloc-allocated command buffer?
[ ... ]
> +static void vep_disable_work(struct work_struct *work)
> +{
> + struct virtio_usb_ep *vep =
> + container_of(work, struct virtio_usb_ep, disable_work);
> + struct virtio_usb_dc *vudc = vep->vudc;
> + struct virtio_usb_cmd *cmd;
> + struct virtio_usb_dev_cmd_hdr *hdr;
> + u16 endpoint;
> +
> + if (!READ_ONCE(vudc->driver) || !vudc->registered)
> + return;
> +
> + endpoint = vep->ep_id |
> + (usb_ep_dir_in(&vep->ep) ? VIRTIO_USB_EP_DIR_IN :
> + VIRTIO_USB_EP_DIR_OUT);
[Severity: High]
Is vep->ep.desc still valid when vep_disable_work() runs asynchronously,
and should vep_disable_work() abort when vudc->driver is NULL?
Per the gadget API, ep->desc is only valid while the endpoint is enabled.
Once virtio_ep_disable() returns, function drivers such as
gether_disconnect() immediately set ep->desc = NULL (causing
usb_ep_dir_in() in vep_disable_work() to return 0 and send
VIRTIO_USB_CMD_DEV_EP_DISABLE for OUT instead of IN), while
raw_ioctl_ep_disable() calls kfree(ep->desc) (causing usb_ep_dir_in() to
dereference freed memory). Moreover, when a gadget driver unbinds while
the UDC stays registered, gadget_unbind_driver() calls
driver->unbind() (which queues vep_disable_work()) and then immediately
calls virtio_usb_dc_stop(), setting vudc->driver = NULL before
vep_disable_work() runs and causing it to skip sending
VIRTIO_USB_CMD_DEV_EP_DISABLE altogether.
[ ... ]
> +static int virtio_ep_disable(struct usb_ep *ep)
> +{
> + struct virtio_usb_ep *vep = usb_ep_to_virtio_ep(ep);
> + struct virtio_usb_data *vreq = NULL, *vreq_tmp;
> + struct virtio_usb_dc *vudc = vep->vudc;
> + struct virtio_usb_dc_priv *priv = NULL;
> + LIST_HEAD(giveback_inflight);
> + LIST_HEAD(giveback_pended);
> + unsigned long flags;
> +
> + if (!ep || ep->caps.type_control) // EP0 not called disable
> + return -EINVAL;
> +
> + spin_lock_irqsave(&vudc->lock, flags);
> +
> + if (vep->state == EP_ENABLING) {
> + /*
> + * cancel_work_sync() can sleep and must not be called while
> + * holding vudc->lock: vep_enable_work() only re-acquires
> + * this same lock after its (possibly blocking) virtio
> + * command completes, so holding the lock here across
> + * cancel_work_sync() would deadlock against it.
> + */
> + spin_unlock_irqrestore(&vudc->lock, flags);
> + cancel_work_sync(&vep->enable_work);
> + spin_lock_irqsave(&vudc->lock, flags);
> + }
[Severity: High]
If cancel_work_sync(&vep->enable_work) cancels an enable work item before
it runs, who flushes veps[0].pend_queue?
While vep->state was EP_ENABLING, virtio_ep_queue() parked any EP0
requests on veps[0].pend_queue, relying on vep_enable_work() to call
virtio_usb_dc_flush_ep0_pend(vudc). When enable_work is cancelled here,
virtio_ep_disable() transitions vep->state to EP_DISABLED without
calling virtio_usb_dc_flush_ep0_pend(vudc), permanently stranding any
pending EP0 control request (and still queuing vep->disable_work for an
endpoint that was never enabled on the backend). Also, while vudc->lock
is dropped for cancel_work_sync() with vep->state still set to
EP_ENABLING, an already-running vep_enable_work() can acquire vudc->lock,
set vep->state = EP_ENABLED, and flush pend_queue into the virtqueue.
> +
> + if (vep->state == EP_DISABLED) {
> + spin_unlock_irqrestore(&vudc->lock, flags);
> + return 0;
> + }
> +
> + vep->state = EP_DISABLED;
> +
> + /*
> + * All pending (waiting for the ENABLING) - put to local list for
> + * giveback(-ESHUTDOWN). These requests are not in the virtqueue,
> + * can be unref now.
> + */
> + list_for_each_entry_safe(vreq, vreq_tmp, &vep->pend_queue, list) {
> + list_del_init(&vreq->list);
> + list_add_tail(&vreq->list, &giveback_pended);
> + }
> +
> + /*
> + * in-flight requests - make early giveback, remove from req_queue,
> + * mark completed_early, but do not unref: unref will be in complete
> + * handler.
> + */
> + 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);
> + }
> +
> + spin_unlock_irqrestore(&vudc->lock, flags);
> +
> + /* pending: just giveback and unref */
> + list_for_each_entry_safe(vreq, vreq_tmp, &giveback_pended, list) {
> + struct virtio_usb_dc_priv *p = virtio_usb_data_priv(vreq);
> +
> + list_del_init(&vreq->list);
> + p->req.status = -ESHUTDOWN;
> + usb_gadget_giveback_request(&vep->ep, &p->req);
> + virtio_usb_data_unref(
> + vreq); /* drop the pend_queue ref taken in virtio_ep_queue() */
> + }
> +
> + /* in-flight: giveback now, unref will be in complete_req */
> + 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);
> + }
[Severity: High]
Is it safe to give back in-flight requests to the gadget driver while
their buffers are still active in dataq->vqueue?
Once usb_gadget_giveback_request() runs, the gadget driver owns req and
req->buf: drivers such as u_ether, f_hid, and f_midi immediately kfree()
req->buf after usb_ep_disable(), leaving the virtio backend reading or
writing freed guest memory until the virtqueue descriptor returns. If
the endpoint is instead re-enabled and the same pre-allocated
usb_request is re-queued before the previous virtqueue entry completes
(as in set_alt() in u_ether or f_uac2), do_real_queue() overwrites the
in-flight vreq header, resets priv->completed_early = false, and adds
vreq to req_queue a second time, causing the first completion to
complete the second submission prematurely and leak a reference.
[ ... ]
> +static int virtio_ep_set_halt(struct usb_ep *ep, int value)
> +{
> + struct virtio_usb_ep *vep = usb_ep_to_virtio_ep(ep);
> + struct virtio_usb_dev_cmd_set_value *req;
> + struct virtio_usb_dc *vudc = vep->vudc;
> + struct virtio_usb_cmd *cmd;
> + unsigned long flags;
> + u16 endpoint;
> + int rc;
> +
> + spin_lock_irqsave(&vudc->lock, flags);
> +
> + /*
> + * EP0 MUST NOT STALL if a control request is still pending.
> + * Composite expects -EAGAIN instead of a forced STALL, otherwise
> + * status stage collapses and the host sees EPROTO/EPIPE.
> + */
> + if (&vep->ep == vudc->gadget.ep0) {
> + if (value && !list_empty(&vep->req_queue)) {
> + spin_unlock_irqrestore(&vudc->lock, flags);
> + return -EAGAIN;
> + }
> + }
> +
> + if (value && ep->desc && usb_ep_dir_in(ep) &&
> + !list_empty(&vep->req_queue)) {
> + spin_unlock_irqrestore(&vudc->lock, flags);
> + return -EAGAIN;
> + }
> + endpoint = vep->ep_id | (usb_ep_dir_in(ep) ? VIRTIO_USB_EP_DIR_IN :
> + VIRTIO_USB_EP_DIR_OUT);
> + spin_unlock_irqrestore(&vudc->lock, flags);
> +
> + cmd = virtio_usb_dc_cmd_alloc(vudc, VIRTIO_USB_CMD_DEV_EP_SET_HALT,
> + GFP_ATOMIC);
> + if (!cmd)
> + return -ENOMEM;
> +
> + req = virtio_usb_cmd_request(cmd);
> + req->value = cpu_to_le32(value);
> + req->hdr.endpoint = cpu_to_le16(endpoint);
> +
> + rc = virtio_usb_cmd_send_sync(vudc->vusb, NULL, NULL, cmd);
> +
> + return rc;
> +}
[Severity: High]
Can virtio_ep_set_halt(), virtio_ep_set_wedge(),
virtio_usb_dc_get_frame(), virtio_usb_dc_pullup(), and
virtio_ep_disable() sleep in atomic context?
Although virtio_ep_set_halt(), virtio_ep_set_wedge(),
virtio_usb_dc_get_frame(), and virtio_usb_dc_pullup() allocate cmd with
GFP_ATOMIC, they all call virtio_usb_cmd_send_sync(), which sleeps in
wait_for_completion_interruptible_timeout(). Similarly,
virtio_ep_disable() drops vudc->lock and calls
cancel_work_sync(&vep->enable_work), which sleeps while callers such as
composite reset_config(), eth_stop(), f_fs, uvcg_video_pump(), gadgetfs,
and raw_gadget hold their own spinlocks with interrupts disabled.
[ ... ]
> +static int virtio_usb_dc_set_selfpowered(struct usb_gadget *gadget,
> + int is_selfpowered)
> +{
> + struct virtio_usb_dc *vudc =
> + container_of(gadget, struct virtio_usb_dc, gadget);
> +
> + atomic_set(&vudc->pending_self_pwr, is_selfpowered ? 1 : 0);
> + queue_work(vudc->dc_cmd_wq, &vudc->dc_cmd_work);
> +
> + return 0;
> +}
> +
> +/**
> + * virtio_usb_dc_pullup() - Software-controlled connect/disconnect to USB host
> + * @gadget: Pointer to the usb gadget structure.
> + * @is_on: flag to start or stop
> + *
> + * Return: 0 on success, -errno on failure.
> + */
> +static int virtio_usb_dc_pullup(struct usb_gadget *gadget, int is_on)
> +{
> + struct virtio_usb_dc *vudc =
> + container_of(gadget, struct virtio_usb_dc, gadget);
> + unsigned int code = VIRTIO_USB_CMD_DEV_PULLUP;
> + struct virtio_usb_dev_cmd_set_value *req;
> + struct virtio_usb_cmd *cmd;
> + unsigned long flags;
> + int rc;
> +
> + spin_lock_irqsave(&vudc->lock, flags);
> + is_on = !!is_on;
> + if (is_on == vudc->pullup) {
> + rc = -EALREADY;
> + goto on_unlock;
> + }
> +
> + vudc->pullup = is_on;
> + spin_unlock_irqrestore(&vudc->lock, flags);
> +
> + cmd = virtio_usb_dc_cmd_alloc(vudc, code, GFP_ATOMIC);
> + if (!cmd)
> + return -ENOMEM;
> +
> + req = virtio_usb_cmd_request(cmd);
> + req->value = cpu_to_le32(is_on);
> +
> + rc = virtio_usb_cmd_send_sync(vudc->vusb, NULL, NULL, cmd);
> +
> + return rc;
[Severity: Medium]
If virtio_usb_dc_cmd_alloc() or virtio_usb_cmd_send_sync() fails in
virtio_usb_dc_pullup(), vudc->pullup is already updated to is_on while
usb_gadget_connect_locked() / usb_gadget_disconnect_locked() in the UDC
core leaves gadget->connected unchanged; won't any subsequent retry fail
with -EALREADY?
Also, virtio_usb_dc_set_selfpowered() never updates
gadget->is_selfpowered = (is_selfpowered != 0) as required by
usb_gadget_set_selfpowered() / usb_gadget_clear_selfpowered(), and
virtio_usb_dc_work() zeroes cur_vbus_ma when self-powered without
restoring the configured VBUS draw when switching back to bus-powered.
[ ... ]
> +static int virtio_usb_dc_get_frame(struct usb_gadget *gadget)
> +{
> + struct virtio_usb_dc *vudc =
> + container_of(gadget, struct virtio_usb_dc, gadget);
> + unsigned int code = VIRTIO_USB_CMD_DEV_GET_FRAME_NUMBER;
> + struct virtio_usb_dev_frame_number *resp;
> + struct virtio_usb_cmd *cmd;
> + int rc;
> +
> + cmd = virtio_usb_dc_cmd_alloc(vudc, code, GFP_ATOMIC);
> + if (!cmd)
> + return -ENOMEM;
> +
> + rc = virtio_usb_cmd_send_sync(vudc->vusb, NULL, NULL, cmd);
> + if (rc)
> + return rc;
> +
> + resp = virtio_usb_cmd_response(cmd);
> + rc = le32_to_cpu(resp->frame_number);
> +
> + return rc;
> +}
[Severity: High]
Does virtio_usb_dc_get_frame() read freed memory when dereferencing cmd
and resp->frame_number?
virtio_usb_cmd_send_sync() drops the initial reference on cmd in
virtio_usb_cmd_send() and frees it before returning unless the caller
takes an extra reference with virtio_usb_cmd_ref(cmd) (as
virtio_usb_dc_get_endpoint_count() does).
[ ... ]
> +static int virtio_usb_dc_get_endpoint_count(struct virtio_usb_dc *vudc)
> +{
> + unsigned int code = VIRTIO_USB_CMD_DEV_GET_ENDPOINT_COUNT;
> + struct virtio_usb_dev_ep_count *ep_count;
> + struct virtio_usb_cmd *cmd;
> + int rc;
> +
> + cmd = virtio_usb_dc_cmd_alloc(vudc, code, GFP_KERNEL);
> + if (!cmd)
> + return -ENOMEM;
> +
> + virtio_usb_cmd_ref(cmd);
> + rc = virtio_usb_cmd_send_sync(vudc->vusb, NULL, NULL, cmd);
> + if (rc)
> + goto on_exit;
> +
> + ep_count = virtio_usb_cmd_response(cmd);
> + vudc->neps = le32_to_cpu(ep_count->count);
> + if (!vudc->neps)
> + rc = -EINVAL;
[Severity: Medium]
Should vudc->neps also be rejected if it exceeds VIRTIO_USB_EP_DIR_IN
(0x80)? Because wire endpoint IDs are formed as
vep->ep_id | VIRTIO_USB_EP_DIR_IN, any ep_id >= 0x80 collides with the
direction bit and turns OUT endpoints into IN endpoints.
> +on_exit:
> + virtio_usb_cmd_unref(cmd);
> + return rc;
> +}
> +
> +static int virtio_usb_dc_get_endpoint_info(struct virtio_usb_dc *vudc)
> +{
> + unsigned int code = VIRTIO_USB_CMD_DEV_GET_ENDPOINT_INFO;
> + struct virtio_usb_dev_ep_info *epinfo;
> + struct virtio_usb_cmd *cmd;
> + struct scatterlist sg;
> + struct scatterlist *psg_data = &sg;
> + int rc, i;
> +
> + vudc->veps = kcalloc(vudc->neps, sizeof(*vudc->veps) + sizeof(*epinfo),
> + GFP_KERNEL);
> + if (!vudc->veps)
> + return -ENOMEM;
> +
> + epinfo = (void *)vudc->veps + (sizeof(*vudc->veps) * vudc->neps);
> +
> + cmd = virtio_usb_dc_cmd_alloc(vudc, code, GFP_KERNEL);
> + if (!cmd) {
> + rc = -ENOMEM;
> + goto on_error;
> + }
> + virtio_usb_cmd_ref(cmd);
> +
> + sg_init_one(psg_data, epinfo, sizeof(*epinfo) * vudc->neps);
> +
> + rc = virtio_usb_cmd_send_sync(vudc->vusb, NULL, psg_data, cmd);
> + if (rc) {
> + virtio_usb_cmd_unref(cmd);
> + goto on_error;
> + }
[ ... ]
> +on_error:
> + kfree(vudc->veps);
> + vudc->veps = NULL;
> + return rc;
> +}
[Severity: High]
If virtio_usb_cmd_send_sync() returns -ETIMEDOUT or -ERESTARTSYS here,
does kfree(vudc->veps) free the device-writable epinfo buffer while
psg_data is still queued in the command virtqueue? If the host backend
subsequently completes the command, it will write into freed slab
memory.
> +static int virtio_usb_dc_parent_create(struct virtio_usb_dc *vudc)
> +{
> + int rc;
> +
> + vudc->pdev = platform_device_alloc(GADGET_NAME, vudc->port);
> + if (!vudc->pdev)
> + return -ENOMEM;
> +
> + vudc->pdev->dev.parent = &vudc->vusb->vdev->dev;
> + rc = platform_device_add(vudc->pdev);
[Severity: Medium]
If a guest has more than one virtio-usb controller instance with a
device-role port at the same 0-based vudc->port index, won't
platform_device_alloc(GADGET_NAME, vudc->port) collide on
"virtio_usb_dc.<port>" in /sys/bus/platform/devices/ and /sys/class/udc/
and fail with -EEXIST? Even with commit "virtio-usb: support a guest
UDC name prefix from the bind event" later in the series, an empty
udc_name_prefix falls back to this same per-controller port index.
[ ... ]
> +static void virtio_usb_dc_event_process(struct virtio_usb_event *event)
> +{
[ ... ]
> + vudc = vusb->vports[port_id].vudc;
> + driver = vudc->driver;
[Severity: High]
Can vudc be NULL here if a device event arrives for a host-role port (or
a port that has not been initialized by virtio_usb_dc_init())?
Dereferencing vudc->driver without checking if (!vudc) first will crash
with a NULL pointer dereference.
[Severity: High]
Also, reading and caching vudc->driver without holding vudc->lock races
with virtio_usb_dc_stop() during gadget driver unbind:
kworker (event) CPU1
virtio_usb_dc_event_process()
driver = vudc->driver
/* VIRTIO_USB_EVT_DEV_SETUP */
virtio_usb_dc_wait_ep0_idle()
gadget_unbind_driver()
driver->unbind()
virtio_usb_dc_stop()
vudc->driver = NULL
driver->setup(...) <-- calls into unbound/unloaded gadget driver
In VIRTIO_USB_EVT_DEV_DISCONNECTED below, vudc->driver is also checked
and then dereferenced unlocked (if (vudc->driver &&
vudc->driver->disconnect)), which can dereference NULL if
virtio_usb_dc_stop() clears vudc->driver in between.
> +
> + switch (le32_to_cpu(evt->code)) {
> + case VIRTIO_USB_EVT_DEV_BIND: {
> + u8 max_speed;
> +
> + if (vudc->registered) {
> + dev_err(&vusb->vdev->dev,
> + "port %d: BIND while still registered, ignoring\n",
> + port_id);
> + rc = -EBUSY;
> + break;
> + }
> +
> + memzero_explicit(&vudc->gadget, sizeof(struct usb_gadget));
[Severity: High]
Is it valid to zero and re-register the embedded vudc->gadget on a
second VIRTIO_USB_EVT_DEV_BIND after VIRTIO_USB_EVT_DEV_UNBIND?
struct usb_gadget embeds a refcounted struct device initialized and
added by usb_add_gadget_udc(). Zeroing vudc->gadget in place with
memzero_explicit() and calling device_initialize() / device_add() again
on the same struct device violates the driver model lifecycle rules and
corrupts kobject state if any reference to gadget.dev is still held.
[ ... ]
> + case VIRTIO_USB_EVT_DEV_SETUP: {
> + if (!driver)
> + break;
> +
> + vep = usb_ep_to_virtio_ep(vudc->gadget.ep0);
> + setup_evt = virtio_usb_event_buf(event);
> + rc = virtio_usb_dc_wait_ep0_idle(vudc);
> + if (rc) {
> + dev_warn(
> + &vusb->vdev->dev,
> + "SETUP while ep0 request is still pending, timing out\n");
> + virtio_ep_set_halt(vudc->gadget.ep0, 1);
> + break;
> + }
[Severity: Medium]
When virtio_usb_dc_wait_ep0_idle() times out here, veps[0].req_queue is
non-empty, but virtio_ep_set_halt() returns -EAGAIN without sending
VIRTIO_USB_CMD_DEV_EP_SET_HALT whenever &vep->ep == vudc->gadget.ep0 &&
value && !list_empty(&vep->req_queue). Doesn't that make the EP0 halt on
timeout a guaranteed no-op?
[ ... ]
> +static void virtio_usb_dc_dataq_stop_cb(struct virtio_usb *vusb,
> + struct virtio_usb_queue *dataq)
> +{
> + int port_id;
> +
> + if (!vusb->device_role)
> + return;
> +
> + virtio_usb_dataq_stop_cb(vusb, dataq);
> + cancel_work_sync(&vusb->vq_dev_data_rx_work);
> +
> + for (port_id = 0; port_id < vusb->nports; port_id++) {
> + struct virtio_usb_dc *vudc = vusb->vports[port_id].vudc;
> + struct virtio_usb_data *vreq, *vreq_tmp;
> + struct virtio_usb_dc_priv *priv;
> + unsigned int i;
> +
> + if (!vudc)
> + continue;
> +
> + for (i = 0; i < vudc->neps; i++) {
> + list_for_each_entry_safe(vreq, vreq_tmp,
> + &vudc->veps[i].req_queue,
> + list) {
> + priv = virtio_usb_data_priv(vreq);
> + priv->req.status = -ESHUTDOWN;
> + list_del_init(&vreq->list);
> + virtio_usb_data_unref(vreq);
> + usb_gadget_giveback_request(&priv->vep->ep,
> + &priv->req);
> + }
> + }
> + }
> +}
[Severity: High]
Can this loop dereference NULL when vudc->veps is NULL but vudc->neps is
non-zero?
Neither VIRTIO_USB_EVT_DEV_UNBIND nor the failure paths in
VIRTIO_USB_EVT_DEV_BIND after virtio_usb_dc_get_endpoint_count() reset
vudc->neps to 0 when freeing vudc->veps and setting vudc->veps = NULL.
When virtio_usb_remove() runs after an UNBIND event or a failed BIND,
virtio_usb_dc_dataq_stop_cb() iterates i < vudc->neps and dereferences
&vudc->veps[i].req_queue on a NULL vudc->veps pointer.
[Severity: High]
In addition, virtio_usb_dc_dataq_stop_cb() runs while the UDC is still
registered and drains vudc->veps[i].req_queue without holding
vudc->lock, racing with concurrent virtio_ep_queue(),
virtio_ep_dequeue(), and virtio_ep_disable() calls. It also calls
virtio_usb_data_unref(vreq) before passing &priv->req (which is embedded
in vreq) to usb_gadget_giveback_request().
--
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1796144390.git.igor.skalkin@oss.qualcomm.com?part=4
next prev parent reply other threads:[~2026-12-01 18:14 UTC|newest]
Thread overview: 49+ 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-24 16:23 ` sashiko-bot
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-24 16:25 ` sashiko-bot
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:28 ` sashiko-bot
2026-09-24 16:09 ` [PATCH 4/8] virtio-usb: add OTG role query support Igor Skalkin
2026-09-24 16:19 ` sashiko-bot
2026-09-24 16:09 ` [PATCH 5/8] virtio-usb: add USB On-The-Go role-switching support Igor Skalkin
2026-09-24 16:24 ` sashiko-bot
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:30 ` sashiko-bot
2026-09-24 16:09 ` [PATCH 7/8] virtio-usb: add SuperSpeed device-role support Igor Skalkin
2026-09-24 16:35 ` sashiko-bot
2026-09-24 16:09 ` [PATCH 8/8] virtio-usb: support a guest UDC name prefix from the bind event Igor Skalkin
2026-09-24 16:35 ` sashiko-bot
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 18:09 ` sashiko-bot
2026-12-01 17:35 ` [RFC PATCH v2 4/7] virtio-usb: add device role (USB Device " Igor Skalkin
2026-12-01 18:13 ` sashiko-bot [this message]
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
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
2026-12-01 18:14 ` sashiko-bot
2026-12-01 20:09 ` Greg Kroah-Hartman
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-243949@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.