net: hso: fix use-after-free after failed tty open
Subsystems (5)
PATCH 1/1 net: hso: fix use-after-free after failed tty open
hso_serial_open() takes a reference to the serial device only if the first open succeeds. The tty core, however, calls the cleanup() operation after a failed open as well, and hso_serial_cleanup() drops a reference whenever the driver data of the tty is set, which hso_serial_open() does before it tries to start the device.
A first open that fails to start the device therefore drops the reference taken at probe. The serial device is freed while its tty device and its serial_table[] entry are still registered, and the next open or the disconnect uses the freed memory.
Take the reference when the tty is bound to the serial device instead, so that cleanup() always drops a reference that the tty holds.
The failed open has dropped one reference too many since the driver was added. Commit 29bd3bc1194c ("hso: fix crash when device disappears while serial port is open") only moved the final drop from close() to cleanup().
--- drivers/net/usb/hso.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/net/usb/hso.c +5 -3
@@ -1250,7 +1250,11 @@ static int hso_serial_open(struct tty_struct *tty, struct file *filp)hso_dbg (0x1 ,"Opening %d\n" , serial->minor);/* setup */ - tty->driver_data = serial; +if (tty->driver_data != serial) { +/* The tty holds a reference until hso_serial_cleanup(). */ +kref_get (&serial->parent->ref); + tty->driver_data = serial; + }tty_port_tty_set (&serial->port, tty);/* check for port already opened, if not set the termios */
@@ -1265,8 +1269,6 @@ static int hso_serial_open(struct tty_struct *tty, struct file *filp)if (result) {hso_stop_serial_device (serial->parent); serial->port.count--; - }else { -kref_get (&serial->parent->ref); } }else {hso_dbg (0x1 ,"Port was already open\n" );
base-commit: 19cc08c1a6a650f8e54c0461ff198be6448f425d
Signature · 1 line
2.47.3
[email protected]
AI reviewThis is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:
Hi!
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:
- How the issue was discovered, e.g. hit in production, hit during development, syzbot report, manual code inspection, LLM or static analysis tool scan.
- Whether the issue was actually triggered, or is only theoretical (e.g. found by LLM and not proven at runtime). If it was triggered please include the symptoms, like the stack trace or kernel messages.
Please do not repost the series just to address the above. Instead, reply to this email with the missing information, so that reviewers can take it into account.
*IMPORTANT*: If you do repost (for other reasons) - please include the relevant info in the cover letter or commit messages.
Signature · 2 lines
The evaluation is done by an LLM so it may be wrong, if you think that is the case please reply and explain.
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce: - [Critical] net: usb: hso: active serial URBs…
Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [Critical] net: usb: hso: active serial URBs not killed in hso_free_interface() [1]
--
commit 6069581307322cf466dfc112f9e4e7a8b3902bd1
Author: Yannick Müller <[email protected]>
net: hso: fix use-after-free after failed tty open
This commit takes the serial device reference when binding tty->driver_data in hso_serial_open() rather than after hso_start_serial_device() succeeds, so that a failed initial open does not drop the probe reference in hso_serial_cleanup().
Quoted 18 lines
drivers/net/usb/hso.c +5
-1@@ -1250,7 +1250,11 @@ static int hso_serial_open(struct tty_struct *tty, struct file *filp)hso_dbg (0x1 ,"Opening %d\n" , serial->minor);/* setup */ - tty->driver_data = serial; +if (tty->driver_data != serial) { +/* The tty holds a reference until hso_serial_cleanup(). */ +kref_get (&serial->parent->ref); + tty->driver_data = serial; + }tty_port_tty_set (&serial->port, tty);/* check for port already opened, if not set the termios */
drivers/net/usb/hso.c static int hso_serial_open(struct tty_struct *tty, struct file *filp) · line 1274
Quoted 8 lines
drivers/net/usb/hso.c +0
-2@@ -1265,8 +1269,6 @@ static int hso_serial_open(struct tty_struct *tty, struct file *filp)if (result) {hso_stop_serial_device (serial->parent); serial->port.count--; - }else { -kref_get (&serial->parent->ref); } }else {hso_dbg (0x1 ,"Port was already open\n" );
[Severity: Critical]
This problem wasn't introduced by this patch, but can disconnecting an open serial port leave in-flight URBs active when the final reference is dropped in hso_serial_cleanup() [1]?
When an interface is torn down while a serial port is open, hso_free_interface() sets serial->parent->usb_gone = 1 and drops the probe reference without calling hso_stop_serial_device():
drivers/net/usb/hso.c:hso_free_interface() {
...
serial = dev2ser(serial_table[i]);
tty_port_tty_hangup(&serial->port, false);
mutex_lock(&serial->parent->mutex);
serial->parent->usb_gone = 1;
mutex_unlock(&serial->parent->mutex);
cancel_work_sync(&serial_table[i]->async_put_intf);
cancel_work_sync(&serial_table[i]->async_get_intf);
hso_serial_tty_unregister(serial);
kref_put(&serial->parent->ref, hso_serial_ref_free);
...
}When userspace then closes the hung-up TTY, hso_serial_close() sees usb_gone set and skips hso_stop_serial_device():
drivers/net/usb/hso.c:hso_serial_close() {
...
if (serial->port.count <= 0) {
serial->port.count = 0;
tty_port_tty_set(&serial->port, NULL);
if (!usb_gone)
hso_stop_serial_device(serial->parent);
tasklet_kill(&serial->unthrottle_tasklet);
}
...
}hso_serial_cleanup() then drops the final serial->parent->ref, and hso_free_serial_device() and hso_free_shared_int() call usb_free_urb() and kfree() on active URBs and transfer buffers that were never killed via usb_kill_urb(), triggering a WARN_ON in usb_free_urb() and a use-after-free when those URBs complete.
[1] https://sashiko.dev/#/bug/linux-138610c9-94b5-4a04-b8c0-02e4a533d28f
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1