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=1796148840; b=z5Dchy/2CJq37ugbt+8B3ian+e4aqF/VHCHxb+NgH1a3LNTDgf0/7Uve/Xo4/LZHWp8U YJ2w+ALEp9k+IRXUqGfAmceAP0n+HuBJS1eCX0vAFKaSHpXhucf1ngb/qiY63x9js1bZG oLcWia2pRcZJZdC5Q8/ker9SKmSQ0ki/VGIqPcS/XrgfToSCVpzqx8lGIS2ne4NtLEJCu qmDqVOArdPFjLPCNjj0zsWVdbAK+VnOOOB9SIMY4pNU9PdhlBPT3einSLtutRjYncEGYo uEcQ/m73Qx8xaDMA68PyWIWwUklsQSdQBiEmdnpJ27CAMeaVtwKTzSc3cab9cXuonHA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796148840; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=q6XgBTVRMhRvcaIMxyW6sJHhTaQdI50wmmzHxg/yLs4=; b=OZ7mUa18L7Sqz82PcDKaxnNwLV84sF7E/1/sa1yqKrr210qrLV2JRfb3VPCEidD9DKo4 Z2RojLdW05p8rd3O3hCiBppWmQWyKgbtQyZ9Q5FWLD8+2EaFMO//samQo4h6Xde04JlKb ymFuyWzpWH4LeN/oOk8iHNnaU906JcN4kmko7j33vqUOTLmwP6LSU3ZRfX3RNxQwUlofv 8jMIQs+61LUMWLUuYDR2J4RS6WnqXiTHZdDEGoIKqPgoEXSZy4G+sVzW8oQ2Ji7EB6PFr 6n6q2sEQWijlqFQRZVvFa8Vveoj1zfklJDg5C55ws2tRPMH5Z2nETg8bnumKr/IgtpQ== 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="X/AsDnV5"; 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 585FB606A320; Tue, 1 Dec 2026 18:14:00 +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=X/AsDnV5 Received: by smtp.kernel.org (Postfix) with ESMTPSA id E6D2E1F000C1; Tue, 1 Dec 2026 18:13:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796148840; bh=q6XgBTVRMhRvcaIMxyW6sJHhTaQdI50wmmzHxg/yLs4=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=X/AsDnV5Sl4y5OYXHWSR7qX3/zjOqxdtj+GK7rjyGu6nYw+Wp2quWqYhnA8BXmaSX 61qkh3SJgl/7gvlfCLDQecA4N/WLQ+1EhepN+gpaKkpRtzR9oxtdEsyFq65lgFR10D Ery9bxv+1Y44q6pzes6zUMuoH3DorUWCoE1El644Ybi+Y90Eld/wa0hxwh5uX+V/Dd R6uzsSPgYw2alX4GH1+QTFqzZ9sPw7WvEibPcWGQBr9AdPzoe+kInatMGtem7EZHvo P/4rT8Hk3mZQbttz/gC9Hf1iHk7F8PqD3BINmxWn4PMYY7W7BiQaxfJM+wchQGSQ6S JT6X+yPxiyXoA== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [RFC PATCH v2 4/7] virtio-usb: add device role (USB Device Controller) 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: <3f0c8f183e321f032bdf30bb06f6837dadb6bc94.1796144390.git.igor.skalkin@oss.qualcomm.com> References: <20260924160907.145405-1-igor.skalkin@oss.qualcomm.com> <3f0c8f183e321f032bdf30bb06f6837dadb6bc94.1796144390.git.igor.skalkin@oss.qualcomm.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Tue, 01 Dec 2026 18:13:59 +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 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 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 *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); [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 *v= dev) > virtio_usb_hc_vp_deinit(vusb, i); > } > =20 > + 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 =3D 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/dev= ice.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 =3D usb_ep_to_virtio_ep(ep); > + unsigned int direction =3D 0; > + > + if (ep->address =3D=3D 0) > + direction =3D vep->setup.bRequestType & USB_DIR_IN ? 1 : 0; > + else if (ep->desc && ep->desc->bEndpointAddress) > + direction =3D usb_endpoint_dir_in(ep->desc) ? 1 : 0; > + > + return direction; > +} [Severity: Medium] Can testing ep->address =3D=3D 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 =3D desc->bEndpointAddress in virtio_ep_enable())? [ ... ] > +static bool virtio_usb_dc_req_queue_empty(struct virtio_usb_ep *vep) > +{ > + struct virtio_usb_dc *vudc =3D vep->vudc; > + unsigned long flags; > + bool empty; > + > + spin_lock_irqsave(&vudc->lock, flags); > + empty =3D 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 =3D 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 =3D virtio_usb_data_response(vreq); > + struct virtio_usb_dc_priv *priv =3D virtio_usb_data_priv(vreq); > + unsigned int status =3D le32_to_cpu(response->status); > + struct usb_request *req =3D &priv->req; > + struct virtio_usb_ep *vep =3D priv->vep; > + struct virtio_usb_dc *vudc =3D vep->vudc; > + struct virtio_usb_data *vreq_iter; > + unsigned int found =3D 0; > + unsigned long flags; > + bool wake_ep0 =3D false; > + bool early; > + > + spin_lock_irqsave(&vudc->lock, flags); > + > + list_for_each_entry(vreq_iter, &vep->req_queue, list) { > + if (vreq_iter =3D=3D vreq) { > + found =3D 1; > + break; > + } > + } > + early =3D 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 =3D true /* vreq remains in flight on dataq */ kfree(vudc->veps) virtio_usb_dc_complete_req(vreq) vep =3D priv->vep vudc =3D 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 !=3D -ECONNRESET && req->status !=3D -ESHUTDOWN) { > + req->status =3D virtio_error_to_usb(status); > + if (!req->status) > + req->actual =3D 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 =3D 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 =3D VIRTIO_USB_EP_ISOCHRONOUS; > + break; > + case USB_ENDPOINT_XFER_BULK: > + transfer_type =3D 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 =3D 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 =3D VIRTIO_USB_EP_INTERRUPT; > + break; > + default: > + transfer_type =3D VIRTIO_USB_EP_CONTROL; > + break; > + } > + } > + request->transfer_type =3D cpu_to_le16(transfer_type); > + > + if (req->length && req->buf) { > + psg_data =3D &sg; > + sg_init_one(psg_data, req->buf, req->length); > + } else if (req->length && req->num_sgs > 0) { > + psg_data =3D req->sg; > + } else if (req->length && req->sg) { [Severity: High] When req->num_sgs > 0, does passing psg_data =3D 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 =3D false; > + priv->submitted =3D false; > + priv->cancel_pending =3D 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 =3D virtio_usb_data_tag_next(vudc->vusb); > + priv->tag =3D tag; > + request->tag =3D cpu_to_le64(tag); > + req->actual =3D 0; > + req->status =3D -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 =3D=3D EP_ENABLED */ spin_unlock_irqrestore(&vudc->lock) virtio_ep_disable() spin_lock_irqsave(&vudc->lock) vep->state =3D 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 =3D EP_ENABLED before dropping vudc->lock inside its pend_queue flush loop, so a concurrent virtio_ep_disable() sees vep->state !=3D 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 =3D 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 =3D virtqueue_add_sgs(queue->vqueue, psgs, nouts, nins, data, GFP_ATOMIC); if (!rc) notify =3D 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 =3D 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 =3D 2; > + > + sg_init_table(psg_data, n_sgs); > + sg_set_buf(&psg_data[0], desc, sizeof(*desc)); > + if (n_sgs =3D=3D 2) > + sg_set_buf(&psg_data[1], comp, sizeof(*comp)); > + hdr =3D virtio_usb_cmd_request(cmd); > + hdr->endpoint =3D cpu_to_le16(endpoint); > + > + rc =3D 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 >=3D 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 =3D > + container_of(work, struct virtio_usb_ep, disable_work); > + struct virtio_usb_dc *vudc =3D 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 =3D 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 =3D 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 =3D 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 =3D usb_ep_to_virtio_ep(ep); > + struct virtio_usb_data *vreq =3D NULL, *vreq_tmp; > + struct virtio_usb_dc *vudc =3D vep->vudc; > + struct virtio_usb_dc_priv *priv =3D 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 =3D=3D 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 =3D EP_ENABLED, and flush pend_queue into the virtqueue. > + > + if (vep->state =3D=3D EP_DISABLED) { > + spin_unlock_irqrestore(&vudc->lock, flags); > + return 0; > + } > + > + vep->state =3D 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 =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); > + } > + > + 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 =3D virtio_usb_data_priv(vreq); > + > + list_del_init(&vreq->list); > + p->req.status =3D -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 =3D 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 =3D 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 =3D usb_ep_to_virtio_ep(ep); > + struct virtio_usb_dev_cmd_set_value *req; > + struct virtio_usb_dc *vudc =3D 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 =3D=3D 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 =3D 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 =3D virtio_usb_dc_cmd_alloc(vudc, VIRTIO_USB_CMD_DEV_EP_SET_HALT, > + GFP_ATOMIC); > + if (!cmd) > + return -ENOMEM; > + > + req =3D virtio_usb_cmd_request(cmd); > + req->value =3D cpu_to_le32(value); > + req->hdr.endpoint =3D cpu_to_le16(endpoint); > + > + rc =3D 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 =3D > + 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 US= B 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 =3D > + container_of(gadget, struct virtio_usb_dc, gadget); > + unsigned int code =3D 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 =3D !!is_on; > + if (is_on =3D=3D vudc->pullup) { > + rc =3D -EALREADY; > + goto on_unlock; > + } > + > + vudc->pullup =3D is_on; > + spin_unlock_irqrestore(&vudc->lock, flags); > + > + cmd =3D virtio_usb_dc_cmd_alloc(vudc, code, GFP_ATOMIC); > + if (!cmd) > + return -ENOMEM; > + > + req =3D virtio_usb_cmd_request(cmd); > + req->value =3D cpu_to_le32(is_on); > + > + rc =3D 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 =3D (is_selfpowered !=3D 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 =3D > + container_of(gadget, struct virtio_usb_dc, gadget); > + unsigned int code =3D VIRTIO_USB_CMD_DEV_GET_FRAME_NUMBER; > + struct virtio_usb_dev_frame_number *resp; > + struct virtio_usb_cmd *cmd; > + int rc; > + > + cmd =3D virtio_usb_dc_cmd_alloc(vudc, code, GFP_ATOMIC); > + if (!cmd) > + return -ENOMEM; > + > + rc =3D virtio_usb_cmd_send_sync(vudc->vusb, NULL, NULL, cmd); > + if (rc) > + return rc; > + > + resp =3D virtio_usb_cmd_response(cmd); > + rc =3D 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 =3D VIRTIO_USB_CMD_DEV_GET_ENDPOINT_COUNT; > + struct virtio_usb_dev_ep_count *ep_count; > + struct virtio_usb_cmd *cmd; > + int rc; > + > + cmd =3D virtio_usb_dc_cmd_alloc(vudc, code, GFP_KERNEL); > + if (!cmd) > + return -ENOMEM; > + > + virtio_usb_cmd_ref(cmd); > + rc =3D virtio_usb_cmd_send_sync(vudc->vusb, NULL, NULL, cmd); > + if (rc) > + goto on_exit; > + > + ep_count =3D virtio_usb_cmd_response(cmd); > + vudc->neps =3D le32_to_cpu(ep_count->count); > + if (!vudc->neps) > + rc =3D -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 >=3D 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 =3D 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 =3D &sg; > + int rc, i; > + > + vudc->veps =3D kcalloc(vudc->neps, sizeof(*vudc->veps) + sizeof(*epinfo= ), > + GFP_KERNEL); > + if (!vudc->veps) > + return -ENOMEM; > + > + epinfo =3D (void *)vudc->veps + (sizeof(*vudc->veps) * vudc->neps); > + > + cmd =3D virtio_usb_dc_cmd_alloc(vudc, code, GFP_KERNEL); > + if (!cmd) { > + rc =3D -ENOMEM; > + goto on_error; > + } > + virtio_usb_cmd_ref(cmd); > + > + sg_init_one(psg_data, epinfo, sizeof(*epinfo) * vudc->neps); > + > + rc =3D 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 =3D 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 =3D platform_device_alloc(GADGET_NAME, vudc->port); > + if (!vudc->pdev) > + return -ENOMEM; > + > + vudc->pdev->dev.parent =3D &vudc->vusb->vdev->dev; > + rc =3D 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." 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 =3D vusb->vports[port_id].vudc; > + driver =3D 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 =3D vudc->driver /* VIRTIO_USB_EVT_DEV_SETUP */ virtio_usb_dc_wait_ep0_idle() gadget_unbind_driver() driver->unbind() virtio_usb_dc_stop() vudc->driver =3D 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 =3D -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 =3D usb_ep_to_virtio_ep(vudc->gadget.ep0); > + setup_evt =3D virtio_usb_event_buf(event); > + rc =3D 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 =3D=3D 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 =3D 0; port_id < vusb->nports; port_id++) { > + struct virtio_usb_dc *vudc =3D 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 =3D 0; i < vudc->neps; i++) { > + list_for_each_entry_safe(vreq, vreq_tmp, > + &vudc->veps[i].req_queue, > + list) { > + priv =3D virtio_usb_data_priv(vreq); > + priv->req.status =3D -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 =3D 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(). --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/cover.1796144390.gi= t.igor.skalkin@oss.qualcomm.com?part=3D4