[PATCH] tty: vt: protect tty lifetime in keyboard path
From: Haoli Zhang
Date: Tue Nov 17 2026 - 04:12:43 EST
The KASAN report reaches stop_tty() through fn_hold(). That handler
reads vc->port.tty and uses it without holding a tty reference. It runs
under kbd_event_lock, while con_shutdown() clears the port association
under console_lock; these locks do not protect the tty lifetime.
Use the tty_port_tty guard in fn_hold() and keep the reference until
start_tty() or stop_tty() returns. Keep kbd_keycode()'s existing tty
reference interval unchanged.
Use tty_port_tty_set() when installing and clearing the association so
that updates use the same port lock as guarded reads and the port holds a
tty reference while the association is active. A keyboard operation
that already obtained a reference can then finish safely after shutdown
clears the association.
Reported-by: syzbot+2932e8970a6398db95c3@xxxxxxxxxxxxxxxxxxxxxxxxx
Closes: https://syzbot.org/bug?extid=2932e8970a6398db95c3
Co-developed-by: Jinning Zhang <25031212117@xxxxxxxxxxxxxxxxx>
Signed-off-by: Jinning Zhang <25031212117@xxxxxxxxxxxxxxxxx>
Co-developed-by: Bin Yu <byu@xxxxxxxxxxxxx>
Signed-off-by: Bin Yu <byu@xxxxxxxxxxxxx>
Signed-off-by: Haoli Zhang <26031212468@xxxxxxxxxxxxxxxxx>
---
drivers/tty/vt/keyboard.c | 34 ++++++++++++++++++++--------------
drivers/tty/vt/vt.c | 4 ++--
2 files changed, 22 insertions(+), 16 deletions(-)
--- a/drivers/tty/vt/keyboard.c
+++ b/drivers/tty/vt/keyboard.c
@@ -509,20 +509,26 @@
static void fn_hold(struct vc_data *vc)
{
- struct tty_struct *tty = vc->port.tty;
-
- if (rep || !tty)
- return;
-
- /*
- * Note: SCROLLOCK will be set (cleared) by stop_tty (start_tty);
- * these routines are also activated by ^S/^Q.
- * (And SCROLLOCK can also be set by the ioctl KDSKBLED.)
- */
- if (tty->flow.stopped)
- start_tty(tty);
- else
- stop_tty(tty);
+ struct tty_struct *tty;
+
+ if (rep)
+ return;
+
+ scoped_guard(tty_port_tty, &vc->port) {
+ tty = scoped_tty();
+ if (!tty)
+ return;
+
+ /*
+ * Note: SCROLLOCK will be set (cleared) by stop_tty (start_tty);
+ * these routines are also activated by ^S/^Q.
+ * (And SCROLLOCK can also be set by the ioctl KDSKBLED.)
+ */
+ if (tty->flow.stopped)
+ start_tty(tty);
+ else
+ stop_tty(tty);
+ }
}
static void fn_num(struct vc_data *vc)
--- a/drivers/tty/vt/vt.c
+++ b/drivers/tty/vt/vt.c
@@ -3756,7 +3756,7 @@
return ret;
tty->driver_data = vc;
- vc->port.tty = tty;
+ tty_port_tty_set(&vc->port, tty);
tty_port_get(&vc->port);
if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
@@ -3789,7 +3789,7 @@
BUG_ON(vc == NULL);
guard(console_lock)();
- vc->port.tty = NULL;
+ tty_port_tty_set(&vc->port, NULL);
}
static void con_cleanup(struct tty_struct *tty)