From: Johan Hovold <johan@kernel.org>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Jiri Slaby <jirislaby@kernel.org>
Cc: linux-serial@vger.kernel.org, linux-kernel@vger.kernel.org,
Johan Hovold <johan@kernel.org>,
stable@vger.kernel.org
Subject: [PATCH] serial core: fix use-after-free after driver module unload
Date: Thu, 12 Nov 2026 11:05:41 +0100 [thread overview]
Message-ID: <20261112100541.348390-1-johan@kernel.org> (raw)
Deregistering a uart driver frees the driver uart state including the
tty ports themselves.
Take a reference to the uart driver module at install() so that the
uart state and resources like the tty driver name do not go away while a
tty is still open.
This specifically avoids use-after-free when accessing the tty port and
crashes when accessing the tty driver name (e.g. in the tty printk
macros) when a tty is closed after its driver module has been unloaded.
It also makes sure that the uart state is still valid when waking up any
waiters after releasing the final uart port reference in
uart_port_deref().
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: stable@vger.kernel.org
Signed-off-by: Johan Hovold <johan@kernel.org>
---
I hit this when investigating a deregistration race that can lead to a
deadlock:
https://lore.kernel.org/lkml/avSAyV6QE0Id3rAk@hovoldconsulting.com/
Johan
drivers/tty/serial/serial_core.c | 18 +++++++++++++++++-
1 file changed, 17 insertions(+), 1 deletion(-)
diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c
index 2e53d194ecfe..289f767cc068 100644
--- a/drivers/tty/serial/serial_core.c
+++ b/drivers/tty/serial/serial_core.c
@@ -1930,10 +1930,25 @@ static int uart_install(struct tty_driver *driver, struct tty_struct *tty)
{
struct uart_driver *drv = driver->driver_state;
struct uart_state *state = drv->state + tty->index;
+ int ret;
+
+ if (!try_module_get(drv->owner))
+ return -ENODEV;
tty->driver_data = state;
- return tty_standard_install(driver, tty);
+ ret = tty_standard_install(driver, tty);
+ if (ret)
+ module_put(drv->owner);
+
+ return ret;
+}
+
+static void uart_cleanup(struct tty_struct *tty)
+{
+ struct uart_driver *drv = tty->driver->driver_state;
+
+ module_put(drv->owner);
}
/*
@@ -2733,6 +2748,7 @@ static void uart_poll_put_char(struct tty_driver *driver, int line, char ch)
static const struct tty_operations uart_ops = {
.install = uart_install,
+ .cleanup = uart_cleanup,
.open = uart_open,
.close = uart_close,
.write = uart_write,
--
2.56.1
next reply other threads:[~2026-11-12 10:07 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-12 10:05 Johan Hovold [this message]
2026-11-12 10:12 ` [PATCH] serial core: fix use-after-free after driver module unload Johan Hovold
2026-11-12 10:28 ` sashiko-bot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261112100541.348390-1-johan@kernel.org \
--to=johan@kernel.org \
--cc=gregkh@linuxfoundation.org \
--cc=jirislaby@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-serial@vger.kernel.org \
--cc=stable@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox