Linux USB
 help / color / mirror / Atom feed
* [PATCH] USB: serial: opticon: fix use-after-free in write completion
@ 2026-11-30 11:46 YuanZhe Liu
  2026-11-30 12:06 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: YuanZhe Liu @ 2026-11-30 11:46 UTC (permalink / raw)
  To: Johan Hovold
  Cc: Greg Kroah-Hartman, linux-usb, linux-kernel, stable, Bin Yu,
	Cheng Wen

The write URBs are anchored in a per-port anchor that is embedded in
the driver's private data, but the anchor is never drained when the
port is removed.

The close callback does kill the anchored URBs, but it runs before the
line discipline is released: tty_release() calls ->close() before
tty_ldisc_release(), and some line disciplines (e.g. ppp) call
tty->ops->write() directly from their transmit path.  A write
submitted after the close callback has drained the anchor will
therefore remain in flight until the port is removed, where the
deferred giveback of the URB accesses the already freed private data:

    BUG: KASAN: slab-use-after-free in usb_anchor_suspend_wakeups
    Write of size 4 at addr ...
    ...
    usb_anchor_suspend_wakeups
    __usb_hcd_giveback_urb
    ...
    Freed by task 11:
    ...
    usb_serial_device_remove
    ...
    kfree

Fix this by killing any anchored URBs in port_remove() before freeing
the private data, as is already done by the garmin_gps driver.

Fixes: e6421583953f ("USB: serial: opticon: stop all I/O on close()")
Cc: stable@vger.kernel.org
Co-developed-by: Bin Yu <byu@xidian.edu.cn>
Signed-off-by: Bin Yu <byu@xidian.edu.cn>
Co-developed-by: Cheng Wen <wencheng@xidian.edu.cn>
Signed-off-by: Cheng Wen <wencheng@xidian.edu.cn>
Signed-off-by: YuanZhe Liu <25031212351@stu.xidian.edu.cn>
---
 drivers/usb/serial/opticon.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/usb/serial/opticon.c b/drivers/usb/serial/opticon.c
index 152dfc901cd8..af53ed13577e 100644
--- a/drivers/usb/serial/opticon.c
+++ b/drivers/usb/serial/opticon.c
@@ -368,6 +368,7 @@ static void opticon_port_remove(struct usb_serial_port *port)
 {
 	struct opticon_private *priv = usb_get_serial_port_data(port);
 
+	usb_kill_anchored_urbs(&priv->anchor);
 	kfree(priv);
 }
 
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] USB: serial: opticon: fix use-after-free in write completion
  2026-11-30 11:46 [PATCH] USB: serial: opticon: fix use-after-free in write completion YuanZhe Liu
@ 2026-11-30 12:06 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-30 12:06 UTC (permalink / raw)
  To: YuanZhe Liu; +Cc: linux-usb

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] usb: serial: opticon: use-after-free of port via work queued in
  port_remove()
--

commit 4e868399fee2ff649a085893481b9d2e4c6c3450
Author: YuanZhe Liu <25031212351@stu.xidian.edu.cn>

USB: serial: opticon: fix use-after-free in write completion

This commit kills anchored write URBs in opticon_port_remove() before
freeing the port's private data to prevent a use-after-free on deferred URB
giveback.

> diff --git a/drivers/usb/serial/opticon.c b/drivers/usb/serial/opticon.c
> index 152dfc901cd8f..af53ed13577e9 100644
> --- a/drivers/usb/serial/opticon.c
> +++ b/drivers/usb/serial/opticon.c
> @@ -368,6 +368,7 @@ static void opticon_port_remove(struct usb_serial_port *port)
>  {
>  	struct opticon_private *priv = usb_get_serial_port_data(port);
>  
> +	usb_kill_anchored_urbs(&priv->anchor);
>  	kfree(priv);
>  }

[Severity: High]
Can killing anchored write URBs in opticon_port_remove() cause a
use-after-free of struct usb_serial_port via port->work?

When usb_kill_anchored_urbs() kills an in-flight write URB here, it
synchronously runs opticon_write_control_callback(), which unconditionally
schedules port->work:

drivers/usb/serial/opticon.c:opticon_write_control_callback() {
	...
	spin_lock_irqsave(&priv->lock, flags);
	--priv->outstanding_urbs;
	priv->outstanding_bytes -= urb->transfer_buffer_length;
	spin_unlock_irqrestore(&priv->lock, flags);

	usb_serial_port_softint(port);
}

In usb_serial_disconnect(), cancel_work_sync(&port->work) runs in the first
port loop before device_del(&port->dev) invokes opticon_port_remove().
Nothing cancels port->work after opticon_port_remove() returns, so when the
tty has already been closed, usb_serial_put() immediately frees port in
usb_serial_port_release() while port->work (usb_serial_port_work) is still
queued:

CPU0                                  CPU1 (system_wq)
usb_serial_disconnect()
  cancel_work_sync(&port->work)
  device_del(&port->dev)
    ...
      opticon_port_remove()
        usb_kill_anchored_urbs()
          ...
            opticon_write_control_callback()
              usb_serial_port_softint(port)
                schedule_work(&port->work)
  usb_serial_put(serial)
    ...
      usb_serial_port_release()
        kfree(port)
                                      usb_serial_port_work()
                                        tty_port_tty_wakeup(&port->port)
                                          <-- use-after-free

Cancelling port->work after usb_kill_anchored_urbs() (or not scheduling
port->work when the URB fails with -ENOENT) would avoid this.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261130114616.31927-1-25031212351@stu.xidian.edu.cn?part=1

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-30 12:06 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 11:46 [PATCH] USB: serial: opticon: fix use-after-free in write completion YuanZhe Liu
2026-11-30 12:06 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox