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 v2] serial: core: fix use-after-free after driver module unload
Date: Thu, 12 Nov 2026 15:22:09 +0100 [thread overview]
Message-ID: <20261112142209.454331-1-johan@kernel.org> (raw)
Deregistering a uart driver frees the driver uart state including the
tty ports themselves.
Restore the uart driver module as owner of the tty driver 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().
Note that a few drivers deregister their uart drivers also when a
(platform) device is manually unbound through sysfs (but if you use that
interface, you get to keep the pieces).
Fixes: 2f16669d322e ("TTY: remove re-assignments to tty_driver members")
Cc: stable@vger.kernel.org # 3.4
Cc: Jiri Slaby <jirislaby@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/
Sashiko noted that this is actually a regression in 3.4.
Johan
Changes in v2:
- pin the driver module by restoring the tty driver owner field
- mention that some drivers also deregister their uart drivers on
manual unbind of a (platform) device through sysfs
drivers/tty/serial/serial_core.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c
index 2e53d194ecfe..5492bffd3456 100644
--- a/drivers/tty/serial/serial_core.c
+++ b/drivers/tty/serial/serial_core.c
@@ -2813,6 +2813,7 @@ int uart_register_driver(struct uart_driver *drv)
drv->tty_driver = normal;
+ normal->owner = drv->owner;
normal->driver_name = drv->driver_name;
normal->name = drv->dev_name;
normal->major = drv->major;
--
2.56.1
reply other threads:[~2026-11-12 14:23 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20261112142209.454331-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
all inboxes | Powered by JetHome®