* [PATCH v2] USB: serial: mos7840: fix use-after-free in write path
@ 2026-12-04 17:20 YuanZhe Liu
2026-12-04 17:51 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: YuanZhe Liu @ 2026-12-04 17: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() 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.
Stop the read URB first and then poison the write URB pool in close(),
unpoisoning it again in open(). A write that still runs after close()
is then rejected by usb_submit_urb() instead of leaving a bulk-out
transfer running on a closed port, which could otherwise race a reopen
or a suspend.
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: 3f5429746d91 ("USB: Moschip 7840 USB-Serial 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>
---
Changes in v2:
- Point Fixes at the initial driver commit, which already allocated the
pool at open and freed it at close.
- Set status to -ENOMEM before the new probe error jumps.
- Kill the read URB first and poison the write pool at close, unpoisoning
it at open, so a post-close write cannot submit a URB or race a reopen.
---
drivers/usb/serial/mos7840.c | 81 +++++++++++++++++++++---------------
1 file changed, 48 insertions(+), 33 deletions(-)
diff --git a/drivers/usb/serial/mos7840.c b/drivers/usb/serial/mos7840.c
index ef803e74ab4b..fa4516775a33 100644
--- a/drivers/usb/serial/mos7840.c
+++ b/drivers/usb/serial/mos7840.c
@@ -193,6 +193,7 @@ struct moschip_port {
struct urb *write_urb_pool[NUM_URBS];
char busy[NUM_URBS];
bool read_urb_busy;
+ bool write_urbs_poisoned;
/* For device(s) with LED indicator */
bool has_led;
@@ -501,27 +502,16 @@ static int mos7840_open(struct tty_struct *tty, struct usb_serial_port *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;
- }
+ if (mos7840_port->write_urbs_poisoned) {
+ for (j = 0; j < NUM_URBS; ++j)
+ usb_unpoison_urb(mos7840_port->write_urb_pool[j]);
+ mos7840_port->write_urbs_poisoned = false;
}
/*****************************************************************************
@@ -699,13 +689,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;
}
@@ -748,20 +731,13 @@ static void mos7840_close(struct usb_serial_port *port)
int j;
__u16 Data;
- 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;
+ for (j = 0; j < NUM_URBS; ++j)
+ usb_poison_urb(mos7840_port->write_urb_pool[j]);
+ mos7840_port->write_urbs_poisoned = true;
+
Data = 0x0;
mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
@@ -1521,7 +1497,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 +1520,23 @@ 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) {
+ status = -ENOMEM;
+ goto error;
+ }
+ mos7840_port->write_urb_pool[j] = urb;
+
+ urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
+ GFP_KERNEL);
+ if (!urb->transfer_buffer) {
+ status = -ENOMEM;
+ 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 +1691,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 +1708,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 v2] USB: serial: mos7840: fix use-after-free in write path
2026-12-04 17:20 [PATCH v2] USB: serial: mos7840: fix use-after-free in write path YuanZhe Liu
@ 2026-12-04 17:51 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-04 17:51 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: mos7840_write() triggers LED URB before
poison check
- [Medium] usb: serial: mos7840: unpoisoning write URBs too early in
mos7840_open()
- [Low] USB: serial: mos7840: commit message misstates usb-serial core
URB lifetime
--
commit fa8687ff7e2ac29a01ae071fbfa4d9d49b895178
Author: YuanZhe Liu <25031212351@stu.xidian.edu.cn>
USB: serial: mos7840: fix use-after-free in write path
This patch moves allocation and freeing of the write URB pool from
mos7840_open() and mos7840_close() to mos7840_port_probe() and
mos7840_port_remove(). It also poisons the write URB pool on close and
unpoisons it on open to reject post-close writes.
> 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.
[Severity: Low]
Does freeing the pool in mos7840_port_remove() really match usb-serial
core's URB lifetime?
usb-serial core poisons port->read_urbs[] and port->write_urbs[] in
usb_serial_disconnect(), but keeps them allocated until
usb_serial_port_release() runs after the last serial->kref is dropped.
mos7840_port_remove() runs earlier during device_del(), so the commit
message overstates the lifetime guarantee.
> diff --git a/drivers/usb/serial/mos7840.c b/drivers/usb/serial/mos7840.c
> index ef803e74ab4bc..fa4516775a332 100644
> --- a/drivers/usb/serial/mos7840.c
> +++ b/drivers/usb/serial/mos7840.c
[ ... ]
> @@ -501,27 +502,16 @@ static int mos7840_open(struct tty_struct *tty, struct usb_serial_port *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;
> - }
> + if (mos7840_port->write_urbs_poisoned) {
> + for (j = 0; j < NUM_URBS; ++j)
> + usb_unpoison_urb(mos7840_port->write_urb_pool[j]);
> + mos7840_port->write_urbs_poisoned = false;
> }
> @@ -699,13 +689,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;
> }
[Severity: Medium]
Can unpoisoning write_urb_pool[] at the start of mos7840_open() allow a
write URB to be submitted during hardware setup or after open fails?
If a port is hung up while another fd keeps tty->count > 0, incoming data
can queue flush_to_ldisc() before mos7840_close() kills read_urb. When a
reopen then calls tty_reopen(), N_TTY is reattached before mos7840_open()
runs, and err: returns without re-poisoning write_urb_pool[]:
CPU0 kworker (flush_to_ldisc)
---- ------------------------
tty_reopen()
tty_ldisc_reinit()
mos7840_open()
usb_unpoison_urb(...)
mos7840_get_reg_sync() fails
goto err; (stays unpoisoned)
usb_autopm_put_interface()
n_tty_receive_buf2()
... -> mos7840_write()
usb_submit_urb() <-- on closed port
Because tty_port_open() leaves tty_port_initialized false when open fails,
mos7840_close() and mos7840_suspend() skip the port, leaving an active
bulk-out URB on a closed, autosuspend-eligible port.
Deferring usb_unpoison_urb() until after all register setup and goto err
exits succeed, just before submitting read_urb, avoids both the setup race
and leaving the pool unpoisoned on failure.
> @@ -748,20 +731,13 @@ static void mos7840_close(struct usb_serial_port *port)
> int j;
> __u16 Data;
>
> - 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;
>
> + for (j = 0; j < NUM_URBS; ++j)
> + usb_poison_urb(mos7840_port->write_urb_pool[j]);
> + mos7840_port->write_urbs_poisoned = true;
> +
> Data = 0x0;
> mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
[Severity: Medium]
Can a post-close mos7840_write() still trigger LED control transfers and
timers on MCS7810 devices before usb_submit_urb() sees the poisoned write
URB?
drivers/usb/serial/mos7840.c:mos7840_write() {
...
if (mos7840_port->has_led)
mos7840_led_activity(port);
/* send it down the pipe */
status = usb_submit_urb(urb, GFP_ATOMIC);
if (status) {
mos7840_port->busy[i] = 0;
dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
"with status = %d\n", __func__, status);
bytes_sent = status;
goto exit;
}
...
}
mos7840_close() poisons write_urb_pool[] but not led_urb or the LED timers.
A post-close write therefore submits led_urb (writing MODEM_CONTROL_REGISTER
in parallel with mos7840_close() clearing it) and arms led_timer1 on a
closed port after runtime PM is dropped.
In addition, every expected -EPERM rejection from the poisoned write URB
will log a spurious dev_err_console() error. Moving mos7840_led_activity()
after the status check and skipping the error log for -EPERM would address
both.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261204172007.5822-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:51 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 17:20 [PATCH v2] USB: serial: mos7840: fix use-after-free in write path YuanZhe Liu
2026-12-04 17:51 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox