public inbox for linux-serial@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v1] tty: serdev: serdev-ttyport: Fix use-after-free in ttyport_close() due to uninitialized serport->tty
@ 2025-04-30 11:16 Xin Chen
  2025-04-30 11:40 ` Greg Kroah-Hartman
  0 siblings, 1 reply; 13+ messages in thread
From: Xin Chen @ 2025-04-30 11:16 UTC (permalink / raw)
  To: Rob Herring, Greg Kroah-Hartman, Jiri Slaby
  Cc: linux-serial, linux-kernel, liulzhao, quic_chejiang, zaiyongc,
	quic_zijuhu, quic_mohamull, Xin Chen, Panicker Harish

When ttyport_open() fails to initialize a tty device, serport->tty is not
set to NULL, leading to a use-after-free scenario in ttyport_close().

To fix this, initialize serport->tty to NULL upon failure and check its
value before reading.

Call trace1:
release_tty
tty_init_dev
ttyport_open
serdev_device_open
qca_setup[hci_uart]
hci_uart_setup[hci_uart]
hci_dev_open_sync[bluetooth]
hci_dev_do_open[bluetooth]
hci_dev_open[bluetooth]
hci_sock_bind[bluetooth]

Call trace2:
refcount_warn_saturate
tty_lock
ttyport_close
serdev_device_close
hci_uart_close[hci_uart]
hci_dev_open_sync[bluetooth]
hci_dev_do_open[bluetooth]
hci_dev_open[bluetooth]
hci_sock_bind[bluetooth]

Co-developed-by: Panicker Harish <quic_pharish@quicinc.com>
Signed-off-by: Panicker Harish <quic_pharish@quicinc.com>
Signed-off-by: Xin Chen <quic_cxin@quicinc.com>
---
 drivers/tty/serdev/serdev-ttyport.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/drivers/tty/serdev/serdev-ttyport.c b/drivers/tty/serdev/serdev-ttyport.c
index 3d7ae7fa5018..287908f2009b 100644
--- a/drivers/tty/serdev/serdev-ttyport.c
+++ b/drivers/tty/serdev/serdev-ttyport.c
@@ -88,6 +88,10 @@ static void ttyport_write_flush(struct serdev_controller *ctrl)
 {
 	struct serport *serport = serdev_controller_get_drvdata(ctrl);
 	struct tty_struct *tty = serport->tty;
+	if (!tty) {
+		dev_err(&ctrl->dev, "tty is null\n");
+		return;
+	}

 	tty_driver_flush_buffer(tty);
 }
@@ -108,8 +112,10 @@ static int ttyport_open(struct serdev_controller *ctrl)
 	int ret;

 	tty = tty_init_dev(serport->tty_drv, serport->tty_idx);
-	if (IS_ERR(tty))
+	if (IS_ERR(tty)) {
+		serport->tty = NULL;
 		return PTR_ERR(tty);
+	}
 	serport->tty = tty;

 	if (!tty->ops->open || !tty->ops->close) {
@@ -156,6 +162,11 @@ static void ttyport_close(struct serdev_controller *ctrl)

 	clear_bit(SERPORT_ACTIVE, &serport->flags);

+	if (!tty) {
+		dev_err(&ctrl->dev, "tty is null\n");
+		return;
+	}
+
 	tty_lock(tty);
 	if (tty->ops->close)
 		tty->ops->close(tty, NULL);
--
2.34.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2025-06-05  8:13 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-30 11:16 [PATCH v1] tty: serdev: serdev-ttyport: Fix use-after-free in ttyport_close() due to uninitialized serport->tty Xin Chen
2025-04-30 11:40 ` Greg Kroah-Hartman
2025-05-08  9:29   ` Xin Chen
2025-05-08  9:41     ` Greg Kroah-Hartman
2025-05-14  9:14       ` Xin Chen
2025-05-23  2:52         ` Xin Chen
2025-05-29  9:07           ` Greg Kroah-Hartman
2025-05-29  9:41             ` Greg Kroah-Hartman
2025-05-30  8:34               ` Xin Chen
2025-05-31  7:20                 ` Greg Kroah-Hartman
2025-06-05  8:13                   ` Xin Chen
2025-05-30  8:11             ` Xin Chen
2025-05-31  7:16               ` Greg Kroah-Hartman

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox