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

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@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/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


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

end of thread, other threads:[~2026-12-04 16:55 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 16:20 [PATCH] USB: serial: mos7720: fix use-after-free in write paths YuanZhe Liu
2026-12-04 16:55 ` sashiko-bot

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