[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-Id: <20250528132816.11433-2-max@enpas.org>
Date: Wed, 28 May 2025 22:28:16 +0900
From: Max Staudt <max@...as.org>
To: Greg Kroah-Hartman <gregkh@...uxfoundation.org>,
Jiri Slaby <jirislaby@...nel.org>
Cc: Johan Hovold <johan@...nel.org>,
linux-serial@...r.kernel.org,
linux-kernel@...r.kernel.org,
Max Staudt <max@...as.org>,
stable@...r.kernel.org
Subject: [PATCH v2 2/2] tty: Fix race against tty_open() in tty_register_device_attr()
Since the chardev is now created before the class device is registered,
an attempt to tty_[k]open() the chardev between these two steps will
lead to tty->dev being assigned NULL by alloc_tty_struct().
alloc_tty_struct() is called via tty_init_dev() when the tty is firstly
opened, and is entered with tty_mutex held, so let's lock the critical
section in tty_register_device_attr() with the same global mutex.
This guarantees that tty->dev can be assigned a sane value.
Fixes: 6a7e6f78c235 ("tty: close race between device register and open")
Signed-off-by: Max Staudt <max@...as.org>
Cc: <stable@...r.kernel.org>
---
drivers/tty/tty_io.c | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
diff --git a/drivers/tty/tty_io.c b/drivers/tty/tty_io.c
index e922b84524d2..94768509e2d2 100644
--- a/drivers/tty/tty_io.c
+++ b/drivers/tty/tty_io.c
@@ -3258,6 +3258,8 @@ struct device *tty_register_device_attr(struct tty_driver *driver,
else
tty_line_name(driver, index, name);
+ mutex_lock(&tty_mutex);
+
if (!(driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)) {
/*
* Free any saved termios data so that the termios state is
@@ -3271,7 +3273,7 @@ struct device *tty_register_device_attr(struct tty_driver *driver,
retval = tty_cdev_add(driver, devt, index, 1);
if (retval)
- return ERR_PTR(retval);
+ goto err_unlock;
cdev_added = true;
}
@@ -3294,6 +3296,8 @@ struct device *tty_register_device_attr(struct tty_driver *driver,
if (retval)
goto err_put;
+ mutex_unlock(&tty_mutex);
+
return dev;
err_put:
@@ -3309,6 +3313,9 @@ struct device *tty_register_device_attr(struct tty_driver *driver,
driver->cdevs[index] = NULL;
}
+err_unlock:
+ mutex_unlock(&tty_mutex);
+
return ERR_PTR(retval);
}
EXPORT_SYMBOL_GPL(tty_register_device_attr);
--
2.39.5
Powered by blists - more mailing lists