USB: cdc-acm: refuse writes after disconnect
Subsystems (2)
PATCH 1/1 USB: cdc-acm: refuse writes after disconnect
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.
--- drivers/usb/class/cdc-acm.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-)
drivers/usb/class/cdc-acm.c +13 -7
@@ -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
Signature · 1 line
2.56.1.703.gf848ed807c-goog
[email protected]
AI reviewSashiko has reviewed this patch and found no issues.
Quoted 24 lines
Commit
7fb57a019f94("USB: cdc-acm: Fix potential deadlock (lockdep warning)") stopped clearingacm->devinacm_disconnect()and replacedACM_READY()withacm->disconnected, but left the write path checking !acm->dev and dropped the disconnect check fromacm_tty_write_room(). Becauseacm_disconnect()frees the write buffers withusb_free_coherent()andacm_probe()only pinsacm->control, a lateacm_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 savedc_linebeforeacm_tty_open()fails with-ENODEV. Because the original descriptor keepstty->countnon-zero, the reinitialized mctp-serial instance stays attached and faults inmemcpy()insideacm_tty_write()on the freedwb->bufwhen transmitting.Pin
acm->dataalongsideacm->controluntilacm_port_destruct()so the unlockeddev_vdbg()inacm_tty_write()stays valid across disconnect, checkacm->disconnectedat the start of thewrite_lockcritical section, report zero room inacm_tty_write_room()once disconnected, and setacm->disconnectedunderwrite_lockinacm_disconnect()so any concurrent writer finishes before the URBs are poisoned and the buffers are freed.
Quoted 3 lines
Fixes 7fb57a019f94 ("USB: cdc-acm: Fix potential deadlock (lockdep warning)")Signed-off-by William A. Kennington III <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Oliver Neukum
maintainerQuoted 6 lines · William A. Kennington III
Pin
acm->dataalongsideacm->controluntilacm_port_destruct()so the unlockeddev_vdbg()inacm_tty_write()stays valid across disconnect, checkacm->disconnectedat the start of thewrite_lockcritical section, report zero room inacm_tty_write_room()once disconnected, and setacm->disconnectedunderwrite_lockinacm_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
Quoted 5 lines · Oliver Neukum
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
Quoted 8 lines · William A. Kennington III
Commit
7fb57a019f94("USB: cdc-acm: Fix potential deadlock (lockdep warning)") stopped clearingacm->devinacm_disconnect()and replacedACM_READY()withacm->disconnected, but left the write path checking !acm->dev and dropped the disconnect check fromacm_tty_write_room(). Becauseacm_disconnect()frees the write buffers withusb_free_coherent()andacm_probe()only pinsacm->control, a lateacm_tty_write()call dereferences freed memory.
Quoted 8 lines
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 savedc_linebeforeacm_tty_open()fails with-ENODEV. Because the original descriptor keepstty->countnon-zero, the reinitialized mctp-serial instance stays attached and faults inmemcpy()insideacm_tty_write()on the freedwb->bufwhen transmitting.
Thanks for reporting this. I assume this is not something you actually hit yourself but rather something flagged by an LLM?
Quoted 6 lines
Pin
acm->dataalongsideacm->controluntilacm_port_destruct()so the unlockeddev_vdbg()inacm_tty_write()stays valid across disconnect, checkacm->disconnectedat the start of thewrite_lockcritical section, report zero room inacm_tty_write_room()once disconnected, and setacm->disconnectedunderwrite_lockinacm_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
Greg Kroah-Hartman
maintainerQuoted 28 lines · Johan Hovold
Quoted 8 lines · William A. Kennington III
Commit
7fb57a019f94("USB: cdc-acm: Fix potential deadlock (lockdep warning)") stopped clearingacm->devinacm_disconnect()and replacedACM_READY()withacm->disconnected, but left the write path checking !acm->dev and dropped the disconnect check fromacm_tty_write_room(). Becauseacm_disconnect()frees the write buffers withusb_free_coherent()andacm_probe()only pinsacm->control, a lateacm_tty_write()call dereferences freed memory.Quoted 8 lines
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 savedc_linebeforeacm_tty_open()fails with-ENODEV. Because the original descriptor keepstty->countnon-zero, the reinitialized mctp-serial instance stays attached and faults inmemcpy()insideacm_tty_write()on the freedwb->bufwhen transmitting.Thanks for reporting this. I assume this is not something you actually hit yourself but rather something flagged by an LLM?
Quoted 6 lines
Pin
acm->dataalongsideacm->controluntilacm_port_destruct()so the unlockeddev_vdbg()inacm_tty_write()stays valid across disconnect, checkacm->disconnectedat the start of thewrite_lockcritical section, report zero room inacm_tty_write_room()once disconnected, and setacm->disconnectedunderwrite_lockinacm_disconnect()so any concurrent writer finishes before the URBs are poisoned and the buffers are freed.
Quoted 6 lines
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_TTYwhen a tty is reopened after a hangup instead.
Argh, I grabbed this too soon, I'll go revert it...
Quoted 2 lines · Johan Hovold
Thanks for reporting this. I assume this is not something you actually hit yourself but rather something flagged by an LLM?
We actually hit this twice in a kdump I got from a machine doing some usb reset testing. An LLM was used to squash the bug.
Trimmed dmesg and backtrace from the first vmcore (the second crash three days later hit the same PC/LR at 51s uptime):
[ 57.197371] usb 1-1.4.1: USB disconnect, device number 9 [ 58.287013] usb 1-1.1.1: new high-speed USB device number 15 using npcm7xx-ehci [ 58.599405] usb 1-1.1.1: Product: Zephyr CDC ACM sample [ 58.676523] cdc_acm 1-1.1.1:1.0: ttyACM1: USB ACM device [ 58.797838] cdc_acm 1-1.2.1:1.0: ttyACM2: USB ACM device [ 59.026611] cdc_acm 1-1.4.1:1.0: ttyACM3: USB ACM device [ 59.324421] IPv6: ADDRCONF(NETDEV_CHANGE): mctpserial0: link becomes ready [ 59.378147] Unable to handle kernel paging request at virtual address ffffffc009fd5000 ... [ 59.437892] Internal error: Oops: 0000000096000047 [#1] SMP [ 59.446523] CPU: 1 PID: 217 Comm: kworker/1:2 Kdump: loaded Not tainted 5.15.167-yocto-s-dirty-4ae9335 #1 [ 59.456085] Hardware name: XXX [ 59.462436] Workqueue: events mctp_serial_tx_work [ 59.474127] pc : __memcpy+0x7c/0x240 [ 59.477711] lr : acm_tty_write+0xec/0x1fc
#10 [ffffff8001f03cd0] el1h_64_sync at ffffffc008011250
PC: ffffffc0083e94bc [memmove+124]
LR: ffffffc00859ed50 [acm_tty_write+236] #11 [ffffff8001f03cf0] memmove at ffffffc0083e94b8 #12 [ffffff8001f03d60] mctp_serial_tx_work at ffffffc008559924 #13 [ffffff8001f03d90] process_one_work at ffffffc008059118
In both vmcores, tty ("ttyACM0") had count == 1, ldisc "mctp" attached, acm->disconnected == true, and acm->wb[0].buf == 0xffffffc009fd5000 already unmapped by usb_free_coherent() in acm_disconnect().
Quoted 6 lines
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_TTYwhen a tty is reopened after a hangup instead.
Clearly your patch does it more cleanly :). Thanks for providing a clean patch that looks like it does solve this issue
Thanks,
William
Quoted 6 lines · William A. Kennington III
Quoted 2 lines · Johan Hovold
Thanks for reporting this. I assume this is not something you actually hit yourself but rather something flagged by an LLM?
We actually hit this twice in a kdump I got from a machine doing some usb reset testing. An LLM was used to squash the bug.
Thanks for clarifying.
Johan