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 Signed-off-by: Bin Yu Co-developed-by: Cheng Wen Signed-off-by: Cheng Wen 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