* [PATCH] tty: port: fix hangup of a reopened tty
@ 2026-11-30 14:14 YuanZhe Liu
2026-11-30 14:37 ` sashiko-bot
0 siblings, 1 reply; 2+ 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 related [flat|nested] 2+ 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 14:37 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-30 14:37 UTC (permalink / raw)
To: YuanZhe Liu; +Cc: linux-serial
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] tty: port: incomplete hangup and lock-drop race in
tty_port_tty_vhangup()
- [Medium] tty: port: tty_port_tty_vhangup() breaks cons_filp
port->count sync
--
commit 2ab4c03c8c59442199d9ab6aed18dfe24377d5b6
Author: YuanZhe Liu <25031212351@stu.xidian.edu.cn>
tty: port: fix hangup of a reopened tty
This commit calls tty_port_hangup() from tty_port_tty_vhangup() if the
port is still initialized after tty_vhangup() returns, covering the case
where a concurrent reopen reactivates the port while TTY_HUPPED is still
set.
> diff --git a/drivers/tty/tty_port.c b/drivers/tty/tty_port.c
> index 4ca4796d13097..756a43e0b323d 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);
[Severity: High]
Does calling tty_port_hangup() after tty_vhangup() returns early on
TTY_HUPPED leave the reopening filp and line discipline un-hung-up, and
open a race window when tty_lock() is dropped between the two calls?
When a hung-up tty is reopened, tty_reopen() reinitializes tty->ldisc,
tty_add_file() attaches the new filp with filp->f_op = &tty_fops, and
tty_port_open() sets TTY_PORT_INITIALIZED before tty_port_block_til_ready()
drops tty_lock(tty) to sleep (before tty_open() clears TTY_HUPPED).
If tty_port_tty_vhangup() runs at that point, __tty_hangup() sees
TTY_HUPPED, drops tty_lock(tty), and returns without setting
filp->f_op = &hung_up_tty_fops or calling tty_ldisc_hangup():
CPU0 CPU1
tty_open()
tty_reopen()
tty_ldisc_reinit()
tty_add_file()
tty_port_open()
/* sets TTY_PORT_INITIALIZED */
tty_port_block_til_ready()
tty_unlock(tty)
tty_port_tty_vhangup()
tty_vhangup()
__tty_hangup()
tty_lock(tty)
/* TTY_HUPPED is set */
tty_unlock(tty)
/* carrier raised / wakes up */
tty_lock(tty)
clear_bit(TTY_HUPPED, &tty->flags)
tty_unlock(tty)
/* returns live fd to userspace */
tty_lock(tty)
tty_port_hangup(port)
tty_unlock(tty)
/* driver frees port data */
tty_ioctl() / tty_write()
/* calls driver on freed port data */ <-- use-after-free
Even if the opener does not win the lock-drop race, skipping
__tty_hangup() leaves tty_hung_up_p(filp) false while tty_port_hangup()
resets port->count to 0. tty_port_block_til_ready() then runs
if (!tty_hung_up_p(filp)) port->count++; and corrupts port->count (or
tty_port_close_start() underflows --port->count to -1 with a warning).
On console ports where port->console is true during
tty_port_tty_vhangup(), tty_port_shutdown_locked() returns early without
clearing TTY_PORT_INITIALIZED, so tty_port_block_til_ready() sees both
tty_hung_up_p(filp) and !tty_port_initialized(port) false and fails to
abort the open. In addition, the reinitialized tty->ldisc is not hung up,
and other hangup paths (such as tty_hangup() and tty_port_tty_hangup())
still drop hangups during a reopen.
Would clearing TTY_HUPPED in tty_reopen() (before dropping tty_lock(tty))
allow __tty_hangup() to perform a complete hangup under a single
tty_lock(tty) hold?
> + if (tty_port_initialized(port))
> + tty_port_hangup(port);
> + tty_unlock(tty);
[Severity: Medium]
Can this unconditional tty_port_hangup() desynchronize port->count when a
redirected console file (such as /dev/console) is open on the tty?
In __tty_hangup(), when a file on tty->tty_files has
write_iter == redirected_tty_write (cons_filp), it leaves cons_filp
un-hung-up and calls tty->ops->close() for the non-console files instead
of tty->ops->hangup() so that tty->count and port->count stay in sync:
drivers/tty/tty_io.c:__tty_hangup() {
...
if (cons_filp) {
if (tty->ops->close)
for (n = 0; n < closecount; n++)
tty->ops->close(tty, cons_filp);
} else if (tty->ops->hangup)
tty->ops->hangup(tty);
...
}
Because TTY_PORT_INITIALIZED remains set after tty_vhangup() in that case,
tty_port_tty_vhangup() will now call tty_port_hangup(port) and reset
port->count to 0 while cons_filp is still open and not marked hung up,
triggering the bad port count warning in tty_port_close_start() when
cons_filp is later closed.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261130141413.10581-1-25031212351@stu.xidian.edu.cn?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-30 14:37 UTC | newest]
Thread overview: 2+ 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 14:37 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox