* [PATCH] USB: cdc-acm: refuse writes after disconnect
@ 2026-11-18 14:56 William A. Kennington III
2026-11-19 8:24 ` Oliver Neukum
2026-11-20 11:22 ` Johan Hovold
0 siblings, 2 replies; 4+ messages in thread
From: William A. Kennington III @ 2026-11-18 14:56 UTC (permalink / raw)
To: Oliver Neukum, Greg Kroah-Hartman
Cc: Havard Skinnemoen, linux-usb, linux-kernel,
William A. Kennington III
Commit 7fb57a019f94 ("USB: cdc-acm: Fix potential deadlock (lockdep
warning)") stopped clearing acm->dev in acm_disconnect() and replaced
ACM_READY() with acm->disconnected, but left the write path checking
!acm->dev and dropped the disconnect check from acm_tty_write_room().
Because acm_disconnect() frees the write buffers with
usb_free_coherent() and acm_probe() only pins acm->control, a late
acm_tty_write() call dereferences freed memory.
This can happen when a CDC-ACM device disconnects while a file
descriptor remains open with a line discipline such as mctp-serial
attached. Hangup closes the line discipline, but a subsequent reopen of
the hung-up TTY calls tty_reopen() -> tty_ldisc_reinit() from the saved
c_line before acm_tty_open() fails with -ENODEV. Because the original
descriptor keeps tty->count non-zero, the reinitialized mctp-serial
instance stays attached and faults in memcpy() inside acm_tty_write() on
the freed wb->buf when transmitting.
Pin acm->data alongside acm->control until acm_port_destruct() so the
unlocked dev_vdbg() in acm_tty_write() stays valid across disconnect,
check acm->disconnected at the start of the write_lock critical section,
report zero room in acm_tty_write_room() once disconnected, and set
acm->disconnected under write_lock in acm_disconnect() so any concurrent
writer finishes before the URBs are poisoned and the buffers are freed.
Fixes: 7fb57a019f94 ("USB: cdc-acm: Fix potential deadlock (lockdep warning)")
Cc: stable@vger.kernel.org
Signed-off-by: William A. Kennington III <william@wkennington.com>
---
drivers/usb/class/cdc-acm.c | 20 +++++++++++++-------
1 file changed, 13 insertions(+), 7 deletions(-)
diff --git a/drivers/usb/class/cdc-acm.c b/drivers/usb/class/cdc-acm.c
index 666969ffd194..77951cd34846 100644
--- a/drivers/usb/class/cdc-acm.c
+++ b/drivers/usb/class/cdc-acm.c
@@ -762,6 +762,7 @@ static void acm_port_destruct(struct tty_port *port)
if (acm->minor != ACM_MINOR_INVALID)
acm_release_minor(acm);
usb_put_intf(acm->control);
+ usb_put_intf(acm->data);
kfree(acm->country_codes);
kfree(acm);
}
@@ -848,6 +849,11 @@ static ssize_t acm_tty_write(struct tty_struct *tty, const u8 *buf,
dev_vdbg(&acm->data->dev, "%zu bytes from tty layer\n", count);
spin_lock_irqsave(&acm->write_lock, flags);
+ if (acm->disconnected) {
+ spin_unlock_irqrestore(&acm->write_lock, flags);
+ return -ENODEV;
+ }
+
wbn = acm_wb_alloc(acm);
if (wbn < 0) {
spin_unlock_irqrestore(&acm->write_lock, flags);
@@ -855,12 +861,6 @@ static ssize_t acm_tty_write(struct tty_struct *tty, const u8 *buf,
}
wb = &acm->wb[wbn];
- if (!acm->dev) {
- wb->use = false;
- spin_unlock_irqrestore(&acm->write_lock, flags);
- return -ENODEV;
- }
-
count = (count > acm->writesize) ? acm->writesize : count;
dev_vdbg(&acm->data->dev, "writing %zu bytes\n", count);
memcpy(wb->buf, buf, count);
@@ -890,6 +890,9 @@ static ssize_t acm_tty_write(struct tty_struct *tty, const u8 *buf,
static unsigned int acm_tty_write_room(struct tty_struct *tty)
{
struct acm *acm = tty->driver_data;
+
+ if (READ_ONCE(acm->disconnected))
+ return 0;
/*
* Do not let the line discipline to know that we have a reserve,
* or it might get too enthusiastic.
@@ -1400,6 +1403,7 @@ static int acm_probe(struct usb_interface *intf,
acm->data = data_interface;
usb_get_intf(acm->control); /* undone in destruct() */
+ usb_get_intf(acm->data);
minor = acm_alloc_minor(acm);
if (minor < 0) {
@@ -1627,7 +1631,9 @@ static void acm_disconnect(struct usb_interface *intf)
if (!acm)
return;
- acm->disconnected = true;
+ spin_lock_irq(&acm->write_lock);
+ WRITE_ONCE(acm->disconnected, true);
+ spin_unlock_irq(&acm->write_lock);
/*
* there is a circular dependency. acm_softint() can resubmit
* the URBs in error handling so we need to block any
base-commit: f11fe7603df07fbb9fa02b0d385c1696c87a6dab
--
2.56.1.703.gf848ed807c-goog
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] USB: cdc-acm: refuse writes after disconnect
2026-11-18 14:56 [PATCH] USB: cdc-acm: refuse writes after disconnect William A. Kennington III
@ 2026-11-19 8:24 ` Oliver Neukum
2026-11-20 6:55 ` William A. Kennington III
2026-11-20 11:22 ` Johan Hovold
1 sibling, 1 reply; 4+ messages in thread
From: Oliver Neukum @ 2026-11-19 8:24 UTC (permalink / raw)
To: William A. Kennington III, Oliver Neukum, Greg Kroah-Hartman
Cc: Havard Skinnemoen, linux-usb, linux-kernel
On 18.11.26 15:56, William A. Kennington III wrote:
> Pin acm->data alongside acm->control until acm_port_destruct() so the
> unlocked dev_vdbg() in acm_tty_write() stays valid across disconnect,
> check acm->disconnected at the start of the write_lock critical section,
> report zero room in acm_tty_write_room() once disconnected, and set
> acm->disconnected under write_lock in acm_disconnect() so any concurrent
> writer finishes before the URBs are poisoned and the buffers are freed.
Hi,
thank you for the patch. It raises a question, though.
It is not clear to me when you use READ_ONCE/WRITE_ONCE
and when you touch the disconnected flag directly.
Regards
Oliver
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] USB: cdc-acm: refuse writes after disconnect
2026-11-19 8:24 ` Oliver Neukum
@ 2026-11-20 6:55 ` William A. Kennington III
0 siblings, 0 replies; 4+ messages in thread
From: William A. Kennington III @ 2026-11-20 6:55 UTC (permalink / raw)
To: Oliver Neukum
Cc: Greg Kroah-Hartman, Havard Skinnemoen, linux-usb, linux-kernel,
William A. Kennington III
On Thu, Nov 19, 2026 at 09:24:51AM +0100, Oliver Neukum wrote:
> Hi,
>
> thank you for the patch. It raises a question, though.
> It is not clear to me when you use READ_ONCE/WRITE_ONCE
> and when you touch the disconnected flag directly.
I added READ_ONCE() around the unlocked acm->disconnected and then just
added the WRITE_ONCE() for consistency. I guess the rest doesn't use it
so let me just remove both of these.
Thanks,
William
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] USB: cdc-acm: refuse writes after disconnect
2026-11-18 14:56 [PATCH] USB: cdc-acm: refuse writes after disconnect William A. Kennington III
2026-11-19 8:24 ` Oliver Neukum
@ 2026-11-20 11:22 ` Johan Hovold
1 sibling, 0 replies; 4+ messages in thread
From: Johan Hovold @ 2026-11-20 11:22 UTC (permalink / raw)
To: William A. Kennington III
Cc: Oliver Neukum, Greg Kroah-Hartman, Havard Skinnemoen, linux-usb,
linux-kernel
On Wed, Nov 18, 2026 at 02:56:36PM +0000, William A. Kennington III wrote:
> Commit 7fb57a019f94 ("USB: cdc-acm: Fix potential deadlock (lockdep
> warning)") stopped clearing acm->dev in acm_disconnect() and replaced
> ACM_READY() with acm->disconnected, but left the write path checking
> !acm->dev and dropped the disconnect check from acm_tty_write_room().
> Because acm_disconnect() frees the write buffers with
> usb_free_coherent() and acm_probe() only pins acm->control, a late
> acm_tty_write() call dereferences freed memory.
>
> This can happen when a CDC-ACM device disconnects while a file
> descriptor remains open with a line discipline such as mctp-serial
> attached. Hangup closes the line discipline, but a subsequent reopen of
> the hung-up TTY calls tty_reopen() -> tty_ldisc_reinit() from the saved
> c_line before acm_tty_open() fails with -ENODEV. Because the original
> descriptor keeps tty->count non-zero, the reinitialized mctp-serial
> instance stays attached and faults in memcpy() inside acm_tty_write() on
> the freed wb->buf when transmitting.
Thanks for reporting this. I assume this is not something you actually
hit yourself but rather something flagged by an LLM?
> Pin acm->data alongside acm->control until acm_port_destruct() so the
> unlocked dev_vdbg() in acm_tty_write() stays valid across disconnect,
> check acm->disconnected at the start of the write_lock critical section,
> report zero room in acm_tty_write_room() once disconnected, and set
> acm->disconnected under write_lock in acm_disconnect() so any concurrent
> writer finishes before the URBs are poisoned and the buffers are freed.
This isn't the right fix though as the line disciplines should not be
calling back into the driver after a hangup (and this isn't cdc-acm
specific).
I've prepared a fix that restores the ldisc to N_TTY when a tty is
reopened after a hangup instead.
Johan
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-20 11:22 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-18 14:56 [PATCH] USB: cdc-acm: refuse writes after disconnect William A. Kennington III
2026-11-19 8:24 ` Oliver Neukum
2026-11-20 6:55 ` William A. Kennington III
2026-11-20 11:22 ` Johan Hovold
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox