[PATCH] USB: serial: mos7840: fix use-after-free in write path

From: YuanZhe Liu

Date: Fri Dec 04 2026 - 11:22:55 EST


The write URB pool is allocated at open() and freed at close(), but the
TTY line discipline can still invoke the driver write() callback after
close() has returned. tty_release() calls the driver close() before it
cancels 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 mos7840_write() while
reading the transfer buffer of a freed urb, followed by an "urb
submitted while active" warning when the same freed urb was resubmitted.

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 callback 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: 0de9a7024e7a ("USB: overhaul of mos7840 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/mos7840.c | 68 +++++++++++++++++++-----------------
1 file changed, 35 insertions(+), 33 deletions(-)

diff --git a/drivers/usb/serial/mos7840.c b/drivers/usb/serial/mos7840.c
index ef803e74ab4b..f4eda9bc7b23 100644
--- a/drivers/usb/serial/mos7840.c
+++ b/drivers/usb/serial/mos7840.c
@@ -500,30 +500,12 @@ static int mos7840_open(struct tty_struct *tty, struct usb_serial_port *port)
struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
struct usb_serial *serial = port->serial;
int response;
- int j;
- struct urb *urb;
__u16 Data;
int status;

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);
- mos7840_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(urb);
- mos7840_port->write_urb_pool[j] = NULL;
- continue;
- }
- }
-
/*****************************************************************************
* Initialize MCS7840 -- Write Init values to corresponding Registers
*
@@ -699,13 +681,6 @@ static int mos7840_open(struct tty_struct *tty, struct usb_serial_port *port)

return 0;
err:
- for (j = 0; j < NUM_URBS; ++j) {
- urb = mos7840_port->write_urb_pool[j];
- if (!urb)
- continue;
- kfree(urb->transfer_buffer);
- usb_free_urb(urb);
- }
return status;
}

@@ -751,14 +726,6 @@ static void mos7840_close(struct usb_serial_port *port)
for (j = 0; j < NUM_URBS; ++j)
usb_kill_urb(mos7840_port->write_urb_pool[j]);

- /* Freeing Write URBs */
- for (j = 0; j < NUM_URBS; ++j) {
- if (mos7840_port->write_urb_pool[j]) {
- kfree(mos7840_port->write_urb_pool[j]->transfer_buffer);
- usb_free_urb(mos7840_port->write_urb_pool[j]);
- }
- }
-
usb_kill_urb(mos7840_port->read_urb);
mos7840_port->read_urb_busy = false;

@@ -1521,7 +1488,9 @@ static int mos7840_port_probe(struct usb_serial_port *port)
struct usb_serial *serial = port->serial;
unsigned long device_flags = (unsigned long)usb_get_serial_data(serial);
struct moschip_port *mos7840_port;
+ struct urb *urb;
int status;
+ int j;
int pnum;
__u16 Data;

@@ -1542,6 +1511,19 @@ static int mos7840_port_probe(struct usb_serial_port *port)
mos7840_port->port = port;
spin_lock_init(&mos7840_port->pool_lock);

+ /* Initializing the write urb pool */
+ for (j = 0; j < NUM_URBS; ++j) {
+ urb = usb_alloc_urb(0, GFP_KERNEL);
+ if (!urb)
+ goto error;
+ mos7840_port->write_urb_pool[j] = urb;
+
+ urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
+ GFP_KERNEL);
+ if (!urb->transfer_buffer)
+ goto error;
+ }
+
/* minor is not initialised until later by
* usb-serial.c:get_free_serial() and cannot therefore be used
* to index device instances */
@@ -1696,6 +1678,13 @@ static int mos7840_port_probe(struct usb_serial_port *port)

return 0;
error:
+ for (j = 0; j < NUM_URBS; ++j) {
+ urb = mos7840_port->write_urb_pool[j];
+ if (!urb)
+ continue;
+ kfree(urb->transfer_buffer);
+ usb_free_urb(urb);
+ }
kfree(mos7840_port->led_dr);
usb_free_urb(mos7840_port->led_urb);
kfree(mos7840_port);
@@ -1706,6 +1695,19 @@ static int mos7840_port_probe(struct usb_serial_port *port)
static void mos7840_port_remove(struct usb_serial_port *port)
{
struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
+ struct urb *urb;
+ int j;
+
+ for (j = 0; j < NUM_URBS; ++j)
+ usb_kill_urb(mos7840_port->write_urb_pool[j]);
+
+ for (j = 0; j < NUM_URBS; ++j) {
+ urb = mos7840_port->write_urb_pool[j];
+ if (!urb)
+ continue;
+ kfree(urb->transfer_buffer);
+ usb_free_urb(urb);
+ }

if (mos7840_port->has_led) {
/* Turn off LED */
--
2.34.1