[PATCH] USB: serial: mos7720: fix use-after-free in write paths

From: YuanZhe Liu

Date: Fri Dec 04 2026 - 11:21:05 EST


The write URB pool is allocated at open() and freed at close(), but the
TTY line discipline can still invoke the driver write_room() and write()
callbacks after close() has returned. tty_release() calls the driver
close() before it flushes the flip-buffer work, so a port that is still
processing received data can flush pending echo characters to the driver
and dereference the freed URBs.

A KASAN slab-use-after-free was observed in mos7720_write_room() while
reading the status of a freed urb, followed by an "urb submitted while
active" warning when mos7720_write() tried to resubmit the same freed
urb.

Give the write URB pool the same lifetime as the port instead: allocate
it at port probe and free it at port removal, after the URBs have been
killed. This matches how usb-serial core manages its own read and write
URBs and makes the write callbacks safe against a racing close().

The issue was found by exercising the driver with a virtual USB device
during automated testing. The change has only been compile tested; no
physical device was available for runtime testing.

Fixes: 0f64478cbc7a ("USB: add USB serial mos7720 driver")
Cc: stable@xxxxxxxxxxxxxxx
Co-developed-by: Bin Yu <byu@xxxxxxxxxxxxx>
Signed-off-by: Bin Yu <byu@xxxxxxxxxxxxx>
Co-developed-by: Cheng Wen <wencheng@xxxxxxxxxxxxx>
Signed-off-by: Cheng Wen <wencheng@xxxxxxxxxxxxx>
Signed-off-by: YuanZhe Liu <25031212351@xxxxxxxxxxxxxxxxx>
---
drivers/usb/serial/mos7720.c | 70 ++++++++++++++++++++----------------
1 file changed, 39 insertions(+), 31 deletions(-)

diff --git a/drivers/usb/serial/mos7720.c b/drivers/usb/serial/mos7720.c
index 104f43a8f4d8..985a0ac51f98 100644
--- a/drivers/usb/serial/mos7720.c
+++ b/drivers/usb/serial/mos7720.c
@@ -843,13 +843,10 @@ static int mos77xx_calc_num_ports(struct usb_serial *serial,
static int mos7720_open(struct tty_struct *tty, struct usb_serial_port *port)
{
struct usb_serial *serial;
- struct urb *urb;
struct moschip_port *mos7720_port;
int response;
int port_number;
__u8 data;
- int allocated_urbs = 0;
- int j;

serial = port->serial;

@@ -860,26 +857,6 @@ static int mos7720_open(struct tty_struct *tty, struct usb_serial_port *port)
usb_clear_halt(serial->dev, port->write_urb->pipe);
usb_clear_halt(serial->dev, port->read_urb->pipe);

- /* Initialising the write urb pool */
- for (j = 0; j < NUM_URBS; ++j) {
- urb = usb_alloc_urb(0, GFP_KERNEL);
- mos7720_port->write_urb_pool[j] = urb;
- if (!urb)
- continue;
-
- urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
- GFP_KERNEL);
- if (!urb->transfer_buffer) {
- usb_free_urb(mos7720_port->write_urb_pool[j]);
- mos7720_port->write_urb_pool[j] = NULL;
- continue;
- }
- allocated_urbs++;
- }
-
- if (!allocated_urbs)
- return -ENOMEM;
-
/* Initialize MCS7720 -- Write Init values to corresponding Registers
*
* Register Index
@@ -978,14 +955,6 @@ static void mos7720_close(struct usb_serial_port *port)
for (j = 0; j < NUM_URBS; ++j)
usb_kill_urb(mos7720_port->write_urb_pool[j]);

- /* Freeing Write URBs */
- for (j = 0; j < NUM_URBS; ++j) {
- if (mos7720_port->write_urb_pool[j]) {
- kfree(mos7720_port->write_urb_pool[j]->transfer_buffer);
- usb_free_urb(mos7720_port->write_urb_pool[j]);
- }
- }
-
/* While closing port, shutdown all bulk read, write *
* and interrupt read if they exists, otherwise nop */
usb_kill_urb(port->write_urb);
@@ -1703,6 +1672,8 @@ static void mos7720_release(struct usb_serial *serial)
static int mos7720_port_probe(struct usb_serial_port *port)
{
struct moschip_port *mos7720_port;
+ struct urb *urb;
+ int i;

mos7720_port = kzalloc_obj(*mos7720_port);
if (!mos7720_port)
@@ -1710,16 +1681,53 @@ static int mos7720_port_probe(struct usb_serial_port *port)

mos7720_port->port = port;

+ for (i = 0; i < NUM_URBS; ++i) {
+ urb = usb_alloc_urb(0, GFP_KERNEL);
+ if (!urb)
+ goto err_free;
+
+ urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
+ GFP_KERNEL);
+ if (!urb->transfer_buffer) {
+ usb_free_urb(urb);
+ goto err_free;
+ }
+
+ mos7720_port->write_urb_pool[i] = urb;
+ }
+
usb_set_serial_port_data(port, mos7720_port);

return 0;
+
+err_free:
+ while (i-- > 0) {
+ urb = mos7720_port->write_urb_pool[i];
+ kfree(urb->transfer_buffer);
+ usb_free_urb(urb);
+ }
+ kfree(mos7720_port);
+
+ return -ENOMEM;
}

static void mos7720_port_remove(struct usb_serial_port *port)
{
struct moschip_port *mos7720_port;
+ struct urb *urb;
+ int i;

mos7720_port = usb_get_serial_port_data(port);
+
+ for (i = 0; i < NUM_URBS; ++i) {
+ urb = mos7720_port->write_urb_pool[i];
+ if (!urb)
+ continue;
+ usb_kill_urb(urb);
+ kfree(urb->transfer_buffer);
+ usb_free_urb(urb);
+ }
+
kfree(mos7720_port);
}

--
2.34.1