Linux Serial subsystem development
 help / color / mirror / Atom feed
* [PATCH] tty: vt: protect tty lifetime in keyboard path
@ 2026-11-17  9:12 Haoli Zhang
  2026-11-17  9:23 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Haoli Zhang @ 2026-11-17  9:12 UTC (permalink / raw)
  To: gregkh, jirislaby
  Cc: linux-kernel, linux-serial, syzbot+2932e8970a6398db95c3,
	25031212117, byu, Haoli Zhang

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@syzkaller.appspotmail.com
Closes: https://syzbot.org/bug?extid=2932e8970a6398db95c3
Co-developed-by: Jinning Zhang <25031212117@stu.xidian.edu.cn>
Signed-off-by: Jinning Zhang <25031212117@stu.xidian.edu.cn>
Co-developed-by: Bin Yu <byu@xidian.edu.cn>
Signed-off-by: Bin Yu <byu@xidian.edu.cn>
Signed-off-by: Haoli Zhang <26031212468@stu.xidian.edu.cn>

---
 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)


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] tty: vt: protect tty lifetime in keyboard path
  2026-11-17  9:12 [PATCH] tty: vt: protect tty lifetime in keyboard path Haoli Zhang
@ 2026-11-17  9:23 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-17  9:23 UTC (permalink / raw)
  To: Haoli Zhang; +Cc: linux-serial

> 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@syzkaller.appspotmail.com
> Closes: https://syzbot.org/bug?extid=2932e8970a6398db95c3
> Co-developed-by: Jinning Zhang <25031212117@stu.xidian.edu.cn>
> Signed-off-by: Jinning Zhang <25031212117@stu.xidian.edu.cn>
> Co-developed-by: Bin Yu <byu@xidian.edu.cn>
> Signed-off-by: Bin Yu <byu@xidian.edu.cn>
> Signed-off-by: Haoli Zhang <26031212468@stu.xidian.edu.cn>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117091207.3950936-1-26031212468@stu.xidian.edu.cn?part=1


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-17  9:23 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-17  9:12 [PATCH] tty: vt: protect tty lifetime in keyboard path Haoli Zhang
2026-11-17  9:23 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox