[PATCH v2] tty: vt: protect tty lifetime in keyboard path

From: Haoli Zhang

Date: Wed Nov 18 2026 - 21:42:29 EST


I apologize for the v1 patch. It included a NULL check inside the
conditional tty_port_tty guard, even though the guard skips its body when
no tty is associated. That check was redundant and made the guard usage
unclear. I have removed it in this version.

fn_hold() reads vc->port.tty and calls start_tty() or stop_tty() without
holding a tty reference. kbd_event_lock serializes keyboard event
processing, but it does not protect the tty lifetime against
con_shutdown() clearing the port association.

Use the tty_port_tty guard in fn_hold() to obtain a reference and keep it
until start_tty() or stop_tty() returns. Use tty_port_tty_set() when
installing and clearing the association, so tty lookup and association
updates are serialized by the port lock. fn_hold() keeps the tty
reference until the start/stop operation completes.

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>
---
Changes in v2:
- Remove the redundant NULL check inside the conditional tty_port_tty guard.

drivers/tty/vt/keyboard.c | 26 ++++++++++++++------------
drivers/tty/vt/vt.c | 4 ++--
2 files changed, 16 insertions(+), 14 deletions(-)

diff --git a/drivers/tty/vt/keyboard.c b/drivers/tty/vt/keyboard.c
--- a/drivers/tty/vt/keyboard.c
+++ b/drivers/tty/vt/keyboard.c
@@ -510,17 +510,19 @@
static void fn_hold(struct vc_data *vc)
{
- struct tty_struct *tty = vc->port.tty;
-
- if (rep || !tty)
+ if (rep)
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);
+ scoped_guard(tty_port_tty, &vc->port) {
+ struct tty_struct *tty = scoped_tty();
+
+ /*
+ * 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)
diff --git a/drivers/tty/vt/vt.c b/drivers/tty/vt/vt.c
--- a/drivers/tty/vt/vt.c
+++ b/drivers/tty/vt/vt.c
@@ -3759,7 +3759,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) {
@@ -3792,7 +3792,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)