* [PATCH] USB: serial: mos7840: fix use-after-free in write path
@ 2026-12-04 16:22 YuanZhe Liu
2026-12-04 17:02 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: YuanZhe Liu @ 2026-12-04 16:22 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() 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@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/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
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] USB: serial: mos7840: fix use-after-free in write path
2026-12-04 16:22 [PATCH] USB: serial: mos7840: fix use-after-free in write path YuanZhe Liu
@ 2026-12-04 17:02 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-04 17:02 UTC (permalink / raw)
To: YuanZhe Liu; +Cc: linux-usb
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] USB: serial: mos7840: uninitialized status return in
mos7840_port_probe()
- [Medium] usb: serial: mos7840: mos7840_close() leaves post-close
writes unblocked
- [Low] USB: serial: mos7840: inaccurate Fixes tag and URB lifetime
claim in commit log
--
commit c490b6baa39966d1389b0f8fab083c3a14fe5814
Author: YuanZhe Liu <25031212351@stu.xidian.edu.cn>
> USB: serial: mos7840: fix use-after-free in write path
>
> 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@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>
[Severity: Low]
Should the Fixes tag point to 3f5429746d91 ("USB: Moschip 7840 USB-Serial
Driver") instead of 0de9a7024e7a ("USB: overhaul of mos7840 driver")? The
initial driver commit already allocated write_urb_pool[] in mos7840_open()
and freed it in mos7840_close() without clearing the pointers.
Also, usb-serial core poisons its read and write URBs in
usb_serial_disconnect() and frees them in usb_serial_port_release(), not
during port_remove().
> diff --git a/drivers/usb/serial/mos7840.c b/drivers/usb/serial/mos7840.c
> index ef803e74ab4bc..f4eda9bc7b236 100644
> --- a/drivers/usb/serial/mos7840.c
> +++ b/drivers/usb/serial/mos7840.c
[ ... ]
> @@ -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;
>
[Severity: Medium]
Can a racing post-close mos7840_write() still submit write URBs and trigger
LED activity after mos7840_close() returns?
CPU0 CPU1
tty_port_close_start()
...
mos7840_bulk_in_callback()
tty_flip_buffer_push()
mos7840_close()
usb_kill_urb(write_urb_pool[])
usb_kill_urb(read_urb)
serial_port_shutdown()
usb_autopm_put_interface()
flush_to_ldisc()
...
mos7840_write()
usb_submit_urb(urb) <-- post-close
Because mos7840_close() kills write_urb_pool[] before read_urb and does not
poison the pool, a queued ldisc write can still submit a bulk-out URB and
arm led_urb/led_timer1 after the runtime PM reference is dropped.
If the port is reopened while that write URB is still queued, mos7840_open()
calls usb_clear_halt() on the busy bulk-out endpoint, triggering an EHCI
warning or xHCI reset refusal and leaving host and device data toggles out
of sync. On suspend, mos7840_suspend() skips closed ports and never poisons
write_urb_pool[] or stops the LED timers and URB, leaving them active on a
suspended device.
Killing read_urb first and poisoning write_urb_pool[] in mos7840_close()
(unpoisoning it in mos7840_open()) would prevent post-close submissions.
[ ... ]
> @@ -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 */
[Severity: Medium]
Does mos7840_port_probe() return an uninitialized status if usb_alloc_urb()
or kmalloc() fails here?
Both error branches jump to error before status is assigned, so the function
frees mos7840_port and returns uninitialized status. If status happens to
be zero (for example under CONFIG_INIT_STACK_ALL_ZERO=y),
usb_serial_device_probe() proceeds to register the tty device with NULL port
driver data, causing a NULL pointer dereference on subsequent open or
removal. Setting status = -ENOMEM before jumping to error would fix this.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261204162226.17678-1-25031212351@stu.xidian.edu.cn?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-04 17:02 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:22 [PATCH] USB: serial: mos7840: fix use-after-free in write path YuanZhe Liu
2026-12-04 17:02 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox