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=1796166758; b=3q0oM6hD0NBLxBjfPfwhiCJUzuTGVL15c+MqicfUmsJzbUCqRrz9ChMl7y0DfeCdZSPs BgVGOXSbvZQ0U+FgrLoaERstOPxXjCSLSZsrA/vvAcGY61ptir9IBrawWZdBiIyhxmLJz QteoXfe0iZTDvIEyrkrOHlFX5v9+3vz0fXzOmXw+dFGJbpGtU8VXF1rWS7kYkxijhrj7/ JEI6gbRrBzmSDx/MKe+04wyQDVMeRRoCflRYdEKJtAIUF5u/8FO0tHqiCa7PM0zoV+Pnh NSpoUJJj0xVaLlaoTHGhN4vsHj+Md2YLTat1/CGThna5lujw6L7a7KKvFcHs+xTqNtg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796166758; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=VLwLq1EXBGezNuDOKfbZO8yHgeZ1175RkuFJvYt+IF4=; b=lOoWr0VpES1dVseR/Kt9O4toE0jGy5GDUSOcUqO/7p+/KzPj/lB+iy2jSym0AbZelYQG FVaHOCYVzsmURlIrRDDlql3DQGikA8wkTKYscmL43vbx8fIKmA1iGL4rcEH8XJuaNV54Q QtUDlDX6wrbHaSNpAJ0n5nBEei3dKwUEEs4aFsTPpjCnSfmblkOZewyo+cF28eP45xO22 K1kI+TcNG1PzVw3JJjf2qne/lF03I4pMN9v4B0rGCwACghXF+WinoEss9QRPWsvdCb26M iXEgCxlc6gagfyHIyvMK1E2dwzDeP8ERExegtRRJ7HmTJEoZB6oFaoSy8ixaogPvz8A== 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="H3/4Ty5B"; 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 AAD81606A331 for ; Tue, 1 Dec 2026 23:12:38 +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=H3/4Ty5B Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4E2301F0089B; Tue, 1 Dec 2026 23:12:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796166758; bh=VLwLq1EXBGezNuDOKfbZO8yHgeZ1175RkuFJvYt+IF4=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=H3/4Ty5BSMUOkJvUAjhLYS80nK1RfV7WFwBRgEcDKZw7xiTYn/dQRKwv9QzjTu6dh vAsYVfAikh2a0wsnuQNfNrZuutT/mk8uNU9+kZ9vUlE7TsuWgP1Up9bRhC721VVvqy xaELAYNYsC8NanM8BwC/qoY6XV9Nu9hKhwbTOULmQV6GlmGLl/e9XAsSDoYcxXGW21 IEM6OGXaohtBF0LFVS+cfnz4L38ZZ5MHLK7UGAc+m3XMN+ZFSFABFFRnvfu4Jg9LBr sucHkF2WS7peuPrTzUfOQA+FWlyQ/hrVBCBV1r7dB0axybMWZDME4WrWhgU53yMTxD g1OSk+ZwIU7wg== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v4] usb: gadget: raw_gadget: fix ep0 request leak on unbind Reply-To: sashiko-reviews@lists.linux.dev To: hanzhijian1991@gmail.com Cc: linux-usb@vger.kernel.org In-Reply-To: <20261201173001.3141592-1-hanzhijian1991@gmail.com> References: <20261124012837.2071714-1-hanzhijian1991@gmail.com> <20261201173001.3141592-1-hanzhijian1991@gmail.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Tue, 01 Dec 2026 23:12:38 +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 2 potential issues to consider: - [High] usb: gadget: raw_gadget: NULL deref and UAF on dev->req in raw_process_ep0_io() - [Low] usb: gadget: raw_gadget: reply notes in changelog and missing Fixes trailer -- commit 4ca9a08e6a4e33547f611152d2201e23e273cbdf Author: Zhijian Han > usb: gadget: raw_gadget: fix ep0 request leak on unbind >=20 > You're right, thanks. I re-checked the syzbot console log: it never > prints "failed to queue connect event", so the reproducer does not hit > the raw_queue_event() failure path my previous patch targeted. That > path is also effectively unreachable: USB_RAW_EVENT_CONNECT is the > first event ever queued for a freshly bound device, so > raw_event_queue_add() can only fail there under OOM. >=20 > The actual leak: the reproducer unbinds and rebinds the dummy_udc > driver through sysfs. When the UDC is bound again, gadget_bind() runs > a second time and overwrites dev->req, leaking the request allocated > by the first bind =E2=80=94 gadget_unbind() never releases it. >=20 > This patch releases dev->req in gadget_unbind(), while the gadget and > its ep0 are still valid. (A similar fix was previously tested on > syzbot for the same bug report; #syz test is currently disabled > upstream, so I could not attach a fresh result.) >=20 > gadget_bind() allocates an ep0 request and stores it in dev->req, but > gadget_unbind() never releases it. The request is only freed by > dev_free(), which runs when the last reference to the raw_gadget device > is dropped. When the UDC is unbound and then bound again (e.g. via > sysfs), gadget_bind() is called a second time and overwrites dev->req > with a newly allocated request, leaking the old one. >=20 > syzbot reproduces this by unbinding/rebinding the dummy_udc driver > through sysfs while running a raw-gadget instance: each re-bind leaks > a 128-byte dummy_hcd request. >=20 > Free the request in gadget_unbind() instead, while the gadget and its > ep0 are still valid. Dequeue the request first if an ep0 transfer is > still in flight. >=20 > Reported-by: syzbot+d83b3c49738aea97c0e5@syzkaller.appspotmail.com > Closes: https://syzkaller.appspot.com/bug?extid=3Dd83b3c49738aea97c0e5 > Signed-off-by: Zhijian Han [Severity: Low] The first three paragraphs of the commit message look like conversational reply notes that belong below the "---" tearline rather than in the changelog. Also, should this include a Fixes tag? Fixes: f2c2e717642c ("usb: gadget: add raw-gadget interface") > diff --git a/drivers/usb/gadget/legacy/raw_gadget.c b/drivers/usb/gadget/= legacy/raw_gadget.c > index 4febf8dac7ca5..aedc7a305cc41 100644 > --- a/drivers/usb/gadget/legacy/raw_gadget.c > +++ b/drivers/usb/gadget/legacy/raw_gadget.c > @@ -328,8 +328,30 @@ static int gadget_bind(struct usb_gadget *gadget, > static void gadget_unbind(struct usb_gadget *gadget) > { > struct raw_dev *dev =3D get_gadget_data(gadget); > + struct usb_request *req; > + unsigned long flags; > + bool queued; > =20 > set_gadget_data(gadget, NULL); > + > + spin_lock_irqsave(&dev->lock, flags); > + req =3D dev->req; > + dev->req =3D NULL; > + queued =3D dev->ep0_urb_queued; > + spin_unlock_irqrestore(&dev->lock, flags); > + > + /* > + * Free the ep0 request allocated in gadget_bind(). If the UDC is > + * re-bound to this driver, a new gadget_bind() call would otherwise > + * overwrite dev->req and leak the previous request. Do it here, > + * while the gadget and its ep0 are still valid. > + */ > + if (req) { > + if (queued) > + usb_ep_dequeue(gadget->ep0, req); > + usb_ep_free_request(gadget->ep0, req); > + } [Severity: High] Can clearing and freeing dev->req in gadget_unbind() cause a NULL pointer dereference or use-after-free in raw_process_ep0_io()? gadget_unbind() sets dev->req =3D NULL without clearing dev->gadget or changing dev->state, and raw_process_ep0_io() checks !dev->gadget under dev->lock without checking !dev->req: drivers/usb/gadget/legacy/raw_gadget.c:raw_process_ep0_io() { ... if (!dev->gadget) { dev_dbg(dev->dev, "fail, gadget is not bound\n"); ret =3D -EBUSY; goto out_unlock; } ... dev->req->buf =3D data; dev->req->length =3D io->length; dev->req->zero =3D usb_raw_io_flags_zero(io->flags); dev->ep0_urb_queued =3D true; spin_unlock_irqrestore(&dev->lock, flags); ret =3D usb_ep_queue(dev->gadget->ep0, dev->req, GFP_KERNEL); ... } If a SETUP packet set ep0_in_pending or ep0_out_pending before unbind, a subsequent USB_RAW_IOCTL_EP0_WRITE or USB_RAW_IOCTL_EP0_READ call passes all checks in raw_process_ep0_io() and dereferences dev->req while it is NULL. In addition, raw_process_ep0_io() drops dev->lock with ep0_urb_queued =3D true before calling usb_ep_queue() and later calls usb_ep_dequeue() outside dev->lock when interrupted: CPU0 CPU1 raw_process_ep0_io() spin_lock_irqsave(&dev->lock, ...) dev->ep0_urb_queued =3D true; spin_unlock_irqrestore(&dev->lock, ...) gadget_unbind() spin_lock_irqsave(&dev->lock, ...) req =3D dev->req; dev->req =3D NULL; queued =3D dev->ep0_urb_queued; spin_unlock_irqrestore(&dev->lock, = ...) usb_ep_dequeue(gadget->ep0, req); usb_ep_free_request(gadget->ep0, re= q); usb_ep_queue(..., dev->req, ...) <-- NULL or freed request Because req has not been queued to ep0 yet when gadget_unbind() runs, usb_ep_dequeue() returns -EINVAL and usb_ep_free_request() frees req while CPU0 is about to queue it (or while CPU0 is waiting in wait_for_completion_interruptible() and then calls usb_ep_dequeue() with dev->req). --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261201173001.3141= 592-1-hanzhijian1991@gmail.com?part=3D1