* [PATCH] tty: port: fix hangup of a reopened tty
@ 2026-11-30 14:14 YuanZhe Liu
2026-11-30 15:37 ` Greg Kroah-Hartman
0 siblings, 1 reply; 3+ messages in thread
From: YuanZhe Liu @ 2026-11-30 14:14 UTC (permalink / raw)
To: Greg Kroah-Hartman, Jiri Slaby
Cc: Johan Hovold, linux-kernel, linux-serial, stable, Bin Yu, Cheng Wen
Drivers rely on tty_port_tty_vhangup() to synchronously hang up their
port before releasing port resources at deregistration (e.g. on physical
disconnect).
tty_vhangup() does nothing if the tty has already been hung up. If the
tty is reopened while still being referenced by another process, the new
open may reactivate the port, but TTY_HUPPED is only cleared once the
open has completed. A hangup request during that window is therefore
dropped and the port remains initialised while the driver tears down its
port data.
A blocking open then ends up calling into the driver (e.g. dtr_rts() or
carrier_raised()) with the port data already gone.
Make sure the port is shut down after tty_vhangup() also when the tty has
already been hung up.
This was found while analysing a NULL-pointer dereference in
pl2303_dtr_rts() on disconnect.
Fixes: b48820de8c1a ("tty: port: fix tty_port_tty_vhangup() race")
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/tty/tty_port.c | 21 +++++++++++++++++----
1 file changed, 17 insertions(+), 4 deletions(-)
diff --git a/drivers/tty/tty_port.c b/drivers/tty/tty_port.c
index 4ca4796d1309..756a43e0b323 100644
--- a/drivers/tty/tty_port.c
+++ b/drivers/tty/tty_port.c
@@ -438,10 +438,23 @@ void tty_port_tty_vhangup(struct tty_port *port)
tty = tty_port_tty_get(port);
mutex_unlock(&port->mutex);
- if (tty) {
- tty_vhangup(tty);
- tty_kref_put(tty);
- }
+ if (!tty)
+ return;
+
+ tty_vhangup(tty);
+
+ /*
+ * tty_vhangup() does nothing if the tty has already been hung up.
+ * A concurrent reopen may however have reactivated the port while
+ * TTY_HUPPED was still set (it is only cleared once the open has
+ * completed), so make sure the port is shut down here as well.
+ */
+ tty_lock(tty);
+ if (tty_port_initialized(port))
+ tty_port_hangup(port);
+ tty_unlock(tty);
+
+ tty_kref_put(tty);
}
EXPORT_SYMBOL_GPL(tty_port_tty_vhangup);
--
2.34.1
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] tty: port: fix hangup of a reopened tty
2026-11-30 14:14 [PATCH] tty: port: fix hangup of a reopened tty YuanZhe Liu
@ 2026-11-30 15:37 ` Greg Kroah-Hartman
2026-11-30 16:27 ` YuanZhe Liu
0 siblings, 1 reply; 3+ messages in thread
From: Greg Kroah-Hartman @ 2026-11-30 15:37 UTC (permalink / raw)
To: YuanZhe Liu
Cc: Jiri Slaby, Johan Hovold, linux-kernel, linux-serial, stable,
Bin Yu, Cheng Wen
On Mon, Nov 30, 2026 at 10:14:13PM +0800, YuanZhe Liu wrote:
> Drivers rely on tty_port_tty_vhangup() to synchronously hang up their
> port before releasing port resources at deregistration (e.g. on physical
> disconnect).
>
> tty_vhangup() does nothing if the tty has already been hung up. If the
> tty is reopened while still being referenced by another process, the new
> open may reactivate the port, but TTY_HUPPED is only cleared once the
> open has completed. A hangup request during that window is therefore
> dropped and the port remains initialised while the driver tears down its
> port data.
>
> A blocking open then ends up calling into the driver (e.g. dtr_rts() or
> carrier_raised()) with the port data already gone.
>
> Make sure the port is shut down after tty_vhangup() also when the tty has
> already been hung up.
>
> This was found while analysing a NULL-pointer dereference in
> pl2303_dtr_rts() on disconnect.
>
> Fixes: b48820de8c1a ("tty: port: fix tty_port_tty_vhangup() race")
> 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/tty/tty_port.c | 21 +++++++++++++++++----
> 1 file changed, 17 insertions(+), 4 deletions(-)
How was this found and tested? And are you sure this isn't fixed in
newer kernel releases? Johan did some work on the hangup path here, so
verifying if that fixed this or not would be good.
Also note that the pl2303 driver might have different issues than all
tty drivers due to them being on the usb bus...
thanks,
greg k-h
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] tty: port: fix hangup of a reopened tty
2026-11-30 15:37 ` Greg Kroah-Hartman
@ 2026-11-30 16:27 ` YuanZhe Liu
0 siblings, 0 replies; 3+ messages in thread
From: YuanZhe Liu @ 2026-11-30 16:27 UTC (permalink / raw)
To: Greg Kroah-Hartman
Cc: Jiri Slaby, Johan Hovold, linux-kernel, linux-serial, stable,
Bin Yu, Cheng Wen
On Mon, Nov 30, 2026 at 04:37:43PM +0100, Greg Kroah-Hartman wrote:
> How was this found and tested? And are you sure this isn't fixed in
> newer kernel releases? Johan did some work on the hangup path here, so
> verifying if that fixed this or not would be good.
>
> Also note that the pl2303 driver might have different issues than all
> tty drivers due to them being on the usb bus...
It was found by syzkaller with a simulated PL2303 USB device. The
crash is a KASAN null-ptr-deref in pl2303_dtr_rts() when the device is
disconnected while a blocking open is still sleeping in
tty_port_block_til_ready() (kernel 7.3.6):
BUG: KASAN: null-ptr-deref in _raw_spin_lock_irqsave+0x64/0xc0
Write of size 4 at addr 0000000000000000
Call Trace:
pl2303_dtr_rts+0x43/0x140 drivers/usb/serial/pl2303.c:1064
serial_port_dtr_rts+0x83/0xb0 drivers/usb/serial/usb-serial.c:822
tty_port_raise_dtr_rts
tty_port_block_til_ready+0x660/0x9a0 drivers/tty/tty_port.c:569
tty_port_open+0x190/0x200 drivers/tty/tty_port.c:806
tty_open+0x37d/0x12f0 drivers/tty/tty_io.c:2112
usb_get_serial_port_data() returns NULL because the port device has
already been unbound; dev_set_drvdata() is cleared by
device_unbind_cleanup() after device_del().
The sequence is: the tty was hung up earlier (e.g. a session leader
exiting) while another process still holds a reference; a blocking
reopen then reuses the tty through tty_reopen() and reactivates the
port, but TTY_HUPPED is only cleared once the open has returned.
disconnect's tty_vhangup() sees TTY_HUPPED and returns without hanging
up the port, so the port stays initialised while device_del() frees the
driver data. When the blocked open wakes up it re-raises DTR/RTS and
dereferences the NULL private data.
Testing so far: compile-tested only on x86_64; I do not have a runtime
reproducer for the fix.
I checked v7.3.6 (where it was reported), v7.3.7 and tty-next as of
7.4-rc5: the relevant code is unchanged there. Johan's b48820de8c1a
serialises tty_port_tty_vhangup() against tty_port_hangup(), but it does
not cover the case above where TTY_HUPPED is already set from an earlier
hangup and a reopen reactivates the port.
A v2 crossed with your reply:
https://lore.kernel.org/r/20261130161204.5003-1-25031212351@stu.xidian.edu.cn
sashiko-bot found problems in v1: forcing the hangup from
tty_port_tty_vhangup() leaves the reopening filp and line discipline
un-hung-up, and can desynchronise port->count for console ports. Rather
than leave a known-broken patch in your queue, I sent v2 to supersede it;
it clears TTY_HUPPED in tty_reopen() instead, so a racing hangup runs to
completion under a single tty lock hold, and it no longer touches
tty_port_tty_vhangup().
You are right that the crash is in a USB serial driver, but the port data
is freed by usb_serial_disconnect() only after tty_port_tty_vhangup()
returns; the missed hangup is what lets the blocked open call back into
the driver afterwards. If you would prefer this to be handled in
usb-serial instead, please let me know.
thanks,
YuanZhe
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-30 16:27 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-30 14:14 [PATCH] tty: port: fix hangup of a reopened tty YuanZhe Liu
2026-11-30 15:37 ` Greg Kroah-Hartman
2026-11-30 16:27 ` YuanZhe Liu
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®