A request may be dequeued after its DMA transfer has completed but before the corresponding DMA interrupt is handled. For an IN request whose ZLP is deferred because both endpoint buffers are full, the endpoint interrupt remains enabled to resume ZLP transmission and may fire after the request is dequeued. lpc32xx_ep_dequeue() removes the request from ep->queue and gives it back via done() without disabling the endpoint interrupt or clearing pending DMA interrupt status. When the interrupt handler then runs with an empty queue, list_entry(ep->queue.next, ...) derives an invalid request pointer from the list head; list_entry() does not return NULL. Passing that pointer to done() in udc_handle_eps(), or dereferencing req->dd_desc_ptr in udc_handle_dma_ep(), results in invalid memory accesses. In the x86_64 test configuration, the bogus request's complete callback resolves to NULL, so the giveback path oopses on a NULL function pointer call. Check whether the request queue is empty before touching the request in both handlers. In udc_handle_eps(), do this before sending the deferred ZLP and disable the endpoint interrupt when there is nothing to complete. In udc_handle_dma_ep(), still disable the DMA channel and clear the pending EOT/NDDRT (and SYSERR) interrupt status so the interrupt does not keep firing, without dereferencing the missing request. Both interrupt handlers hold udc->lock while checking ep->queue and obtaining the request and its DMA descriptor. This serializes these accesses against queue updates in lpc32xx_ep_dequeue(). Fixes: 24a28e428351 ("USB: gadget driver for LPC32xx") Cc: stable@vger.kernel.org Reviewed-by: Guanglei Zhu Signed-off-by: Fang Xieyan --- Changes in v2: - Drop the ep_err() print in the DMA empty-queue path and reword the empty-queue comments: this is an expected race with request dequeue, not an error worth logging (Greg KH). - Describe how the issue was found and tested below (Greg KH). v1: https://lore.kernel.org/r/20261109121035.34561-1-fangxy@xiaopeng.com The issue was found during a security review assisted by the Hawkeye agent (LLM-based code analysis) and confirmed by code inspection. No LPC32xx hardware is available; the driver builds on x86_64 with CONFIG_COMPILE_TEST=y. Reproduced in a KASAN-enabled QEMU/KVM guest with a synthetic trigger that calls each handler with an empty queue. The synthetic trigger invokes both handlers with udc->lock held, matching the locking state of their normal IRQ callers. The same trigger also covers non-empty DMA completion, deferred ZLP and the dequeue-then-interrupt sequence: the fixed driver completes the normal paths and returns cleanly from the sequence. The unpatched udc_handle_eps() path crashes in usb_gadget_giveback_request(): BUG: kernel NULL pointer dereference, address: 0000000000000000 #PF: supervisor instruction fetch in kernel mode RIP: 0010:0x0 done+0x1b8/0x390 [lpc32xx_udc] udc_handle_eps+0x236/0x750 [lpc32xx_udc] The udc_handle_dma_ep() path was exercised separately: it dereferences the bogus request's dd_desc_ptr, logs "DMA descriptor did not retire" and "DD not serviced", and reaches the same NULL completion-pointer call in done() (RIP: 0010:0x0; the handler is inlined into the trigger, so its frame appears as lpc32xx_udc_init+0x40a). Both patched handlers return early without accessing the missing request. In the x86_64 guest, KASAN does not flag the accesses themselves because the bogus request pointer and the fields accessed through it remain within the enclosing UDC allocation. Although these accesses use an invalid object pointer, they do not cross a slab allocation boundary. The crash occurs later when usb_gadget_giveback_request() invokes the NULL completion pointer. drivers/usb/gadget/udc/lpc32xx_udc.c | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/drivers/usb/gadget/udc/lpc32xx_udc.c b/drivers/usb/gadget/udc/lpc32xx_udc.c index 044c31869cfb..477f897043b8 100644 --- a/drivers/usb/gadget/udc/lpc32xx_udc.c +++ b/drivers/usb/gadget/udc/lpc32xx_udc.c @@ -1945,6 +1945,13 @@ static void udc_handle_eps(struct lpc32xx_udc *udc, struct lpc32xx_ep *ep) /* Get endpoint status */ epstatus = udc_clearep_getsts(udc, ep->hwep_num); + /* The request may have been dequeued; nothing is left to complete. */ + if (list_empty(&ep->queue)) { + ep->req_pending = 0; + uda_disable_hwepint(udc, ep->hwep_num); + return; + } + /* * This should never happen, but protect against writing to the * buffer when full. @@ -1958,7 +1965,6 @@ static void udc_handle_eps(struct lpc32xx_udc *udc, struct lpc32xx_ep *ep) } else return; - /* If there isn't a request waiting, something went wrong */ req = list_entry(ep->queue.next, struct lpc32xx_request, queue); done(ep, req, 0); @@ -1985,6 +1991,23 @@ static void udc_handle_dma_ep(struct lpc32xx_udc *udc, struct lpc32xx_ep *ep) ep->totalints++; #endif + /* The request may have been dequeued; still clear the pending interrupts. */ + if (list_empty(&ep->queue)) { + u32 mask = 1U << ep->hwep_num; + + ep->req_pending = 0; + + /* Disable DMA and clear pending interrupts */ + udc_ep_dma_disable(udc, ep->hwep_num); + writel(mask, USBD_EOTINTCLR(udc->udp_baseaddr)); + writel(mask, USBD_NDDRTINTCLR(udc->udp_baseaddr)); + + if (readl(USBD_SYSERRTINTST(udc->udp_baseaddr)) & mask) + writel(mask, USBD_SYSERRTINTCLR(udc->udp_baseaddr)); + + return; + } + req = list_entry(ep->queue.next, struct lpc32xx_request, queue); dd = req->dd_desc_ptr; -- 2.50.1