The write URB pool is 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 urb. Mark the port closed before the write URBs are killed, and refuse to report room or submit data once the port has been closed, as the other USB serial drivers do. 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 Signed-off-by: Bin Yu Co-developed-by: Cheng Wen Signed-off-by: Cheng Wen Signed-off-by: YuanZhe Liu <25031212351@stu.xidian.edu.cn> --- Changes in v2: - Keep the write URB pool lifetime as it is and only gate the write paths on the port being open, matching the other USB serial drivers. --- drivers/usb/serial/mos7720.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/drivers/usb/serial/mos7720.c b/drivers/usb/serial/mos7720.c index 104f43a8f4d8..dde61131e1f9 100644 --- a/drivers/usb/serial/mos7720.c +++ b/drivers/usb/serial/mos7720.c @@ -954,6 +954,9 @@ static unsigned int mos7720_chars_in_buffer(struct tty_struct *tty) int i; unsigned int chars = 0; + if (!mos7720_port->open) + return 0; + for (i = 0; i < NUM_URBS; ++i) { if (mos7720_port->write_urb_pool[i] && mos7720_port->write_urb_pool[i]->status == -EINPROGRESS) @@ -975,6 +978,8 @@ static void mos7720_close(struct usb_serial_port *port) if (mos7720_port == NULL) return; + mos7720_port->open = 0; + for (j = 0; j < NUM_URBS; ++j) usb_kill_urb(mos7720_port->write_urb_pool[j]); @@ -993,8 +998,6 @@ static void mos7720_close(struct usb_serial_port *port) write_mos_reg(serial, port->port_number, MOS7720_MCR, 0x00); write_mos_reg(serial, port->port_number, MOS7720_IER, 0x00); - - mos7720_port->open = 0; } static int mos7720_break(struct tty_struct *tty, int break_state) @@ -1033,6 +1036,9 @@ static unsigned int mos7720_write_room(struct tty_struct *tty) unsigned int room = 0; int i; + if (!mos7720_port->open) + return 0; + /* FIXME: Locking */ for (i = 0; i < NUM_URBS; ++i) { if (mos7720_port->write_urb_pool[i] && @@ -1063,6 +1069,9 @@ static int mos7720_write(struct tty_struct *tty, struct usb_serial_port *port, if (mos7720_port == NULL) return -ENODEV; + if (!mos7720_port->open) + return 0; + /* try to find a free urb in the list */ urb = NULL; -- 2.34.1