* [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
* Re: [PATCH] USB: serial: mos7720: fix use-after-free in write paths
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
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-04 16:55 UTC (permalink / raw)
To: YuanZhe Liu; +Cc: linux-usb
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] usb: serial: mos7720: failed write URB submit wedges pool slot
permanently
- [Medium] usb: serial: mos7720: post-close write submits live URBs
across suspend/open
- [Medium] USB: serial: mos7720: write URBs freed too early in
mos7720_port_remove()
--
commit a57251408ca39e2c92809e7b23430a70e2f2694b
Author: YuanZhe Liu <25031212351@stu.xidian.edu.cn>
USB: serial: mos7720: fix use-after-free in write paths
This commit moves allocation and freeing of mos7720's write_urb_pool[]
from mos7720_open() and mos7720_close() to mos7720_port_probe() and
mos7720_port_remove().
> diff --git a/drivers/usb/serial/mos7720.c b/drivers/usb/serial/mos7720.c
> index 312fa11cea952..0606de2e9c7a0 100644
> --- a/drivers/usb/serial/mos7720.c
> +++ b/drivers/usb/serial/mos7720.c
[ ... ]
> @@ -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);
[Severity: Medium]
Can a racing echo write still submit live URBs from write_urb_pool[]
after mos7720_close() returns?
mos7720_close() kills write_urb_pool[] with usb_kill_urb() before killing
port->read_urb, and mos7720_write() does not check mos7720_port->open.
A racing flush_to_ldisc echo can therefore submit a bulk-out URB after
serial_port_shutdown() calls usb_autopm_put_interface().
Because moschip7720_2port_driver has no .suspend callback and
usb_serial_port_poison_urbs() only poisons usb-serial core URBs, that URB
can stay in flight across USB autosuspend or collide with usb_clear_halt()
when the port is reopened.
Switching mos7720 to usb-serial core's generic write callbacks would
avoid managing a private write_urb_pool[] altogether.
[ ... ]
> @@ -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;
> }
[Severity: High]
Does reusing write_urb_pool[] across open/close cycles permanently wedge
a pool slot whenever usb_submit_urb() fails?
mos7720 tracks busy slots solely via urb->status == -EINPROGRESS, which
usb_submit_urb() sets before calling the HCD. When a racing
mos7720_write() hits usb_kill_urb() in mos7720_close(), submission fails
with -EPERM without clearing urb->status or calling urb->complete():
drivers/usb/serial/mos7720.c:mos7720_write() {
...
status = usb_submit_urb(urb, GFP_ATOMIC);
if (status) {
dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
"with status = %d\n", __func__, status);
bytes_sent = status;
goto exit;
}
...
}
Previously, reallocating the pool in mos7720_open() reset urb->status to
0. Now a failed submit permanently leaks that 32-byte slot and keeps
mos7720_chars_in_buffer() non-zero, making every later close() stall in
tty_wait_until_sent() for the 30-second closing_wait.
Resetting urb->status on usb_submit_urb() failure in mos7720_write()
would prevent the slot from staying wedged.
> 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);
> }
[Severity: Medium]
Are write_urb_pool[] and their transfer buffers freed too early here if a
line discipline is still tearing down?
tty_port_close() clears port->tty before tty_ldisc_release() runs. If
usb_serial_disconnect() races in between, tty_port_tty_vhangup() skips
hanging up the ldisc and mos7720_port_remove() frees the pool while an
attached ldisc (such as N_GSM0710) still has async TX work running:
CPU0 CPU1 kworker
tty_release()
tty_port_close()
tty_port_tty_set(port, NULL)
usb_serial_disconnect()
tty_port_tty_vhangup()
(port->tty is NULL)
mos7720_port_remove()
usb_kill_urb(urb)
kfree(urb->transfer_buffer)
usb_free_urb(urb)
kfree(mos7720_port)
gsmld_write_task()
gsmld_output()
mos7720_write_room()
<-- use-after-free
tty_ldisc_release()
gsmld_close()
serial_write_room() does not check for disconnect, so mos7720_write_room()
dereferences the freed mos7720_port and URBs even on physical unplug.
On sysfs unbind or module unload, serial_write() can also call
mos7720_write(), writing into a freed transfer_buffer or resubmitting an
URB between usb_kill_urb() and usb_free_urb() because usb_kill_urb() does
not poison the URB.
Note also that the commit message says this matches how usb-serial core
manages its URBs, but usb-serial core poisons its URBs in
usb_serial_disconnect() and defers freeing them until
usb_serial_port_release() after serial_cleanup() drops the final kref.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261204162044.17524-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 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