All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] vt: drop console_lock before calling __do_SAK() in vc_SAK()
@ 2026-12-02 18:12 Jiayi Zhang
  2026-12-02 18:26 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Jiayi Zhang @ 2026-12-02 18:12 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Jiri Slaby
  Cc: Eric W . Biederman, linux-serial, linux-kernel,
	syzbot+52cf91760dcb1dac6376, syzbot+d84934b4184d7f9a1aed,
	syzkaller-bugs

vc_SAK() calls __do_SAK() on the console's tty with console_lock held.
__do_SAK() flushes the tty, and tty_buffer_flush() takes the buffer
lock of the tty. flush_to_ldisc() holds that buffer lock while it
passes input to the line discipline, and n_tty calls con_flush_chars(),
which takes console_lock, after each batch of input:

  vc_SAK()                          flush_to_ldisc()
    console_lock()                    mutex_lock(&buf->lock)
    __do_SAK()                        n_tty_receive_buf_common()
      tty_ldisc_flush()                 con_flush_chars()
        tty_buffer_flush()                console_lock()
          mutex_lock(&buf->lock)

So a SAK on a virtual console that is receiving input can deadlock.
syzbot reports this dependency (console_lock -> &buf->lock) as part of
two cycles, one through termios_rwsem and n_tty's receive path, and one
through the serial port mutex.

Take a reference to the tty with tty_port_tty_get() while holding
console_lock, drop console_lock for __do_SAK(), and take it again for
reset_vc(). Commit 8b6312f4dcc1 ("[PATCH] vt: refactor console SAK
processing") held the lock across __do_SAK() so that nothing else
changes the console during SAK. __do_SAK() does not need it: it
flushes the tty and kills its users, and do_SAK_work() calls it
without the console lock for every other tty. The modes that SAK
resets are reset by reset_vc(), which still runs under the lock after
the processes have been signalled, so a change made in between is
undone. The reference keeps the tty alive and delays con_cleanup(), so
vt_disallocate() cannot free the console meanwhile. It also resolves
the old FIXME: vc_SAK() used vc->port.tty without holding a reference.

Fixes: 42fd552e8647 ("fix serial buffer memory leak")
Reported-by: syzbot+52cf91760dcb1dac6376@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=52cf91760dcb1dac6376
Reported-by: syzbot+d84934b4184d7f9a1aed@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=d84934b4184d7f9a1aed
Signed-off-by: Jiayi Zhang <zhangjiayi1014@gmail.com>
---
Tested in QEMU with the syzbot C reproducers of both reports and their
kernel configs. Both trigger rarely here, so the numbers are small:

- tty_buffer_flush report: without the patch 1 of 4 instances
  reported the deadlock; with the patch none of 11 did.
- n_tty_flush_buffer report, with one tcsetattr() on /dev/ttyS0 added
  to the reproducer (a getty on the serial console does this on the
  syzbot machines; it records the serial half of that cycle): without
  the patch 1 of 5; with the patch none of 9.

An earlier patch for the second report moved the ldisc flush in
tty_buffer_flush() out of buf->lock:
https://lore.kernel.org/all/20260804140251.8197-1-omermetekaya0@gmail.com/
This patch removes the console_lock -> buf->lock dependency in
vc_SAK() instead.

AI tools assisted with the analysis, the code and the changelog.

 drivers/tty/vt/vt_ioctl.c |   20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)

diff --git a/drivers/tty/vt/vt_ioctl.c b/drivers/tty/vt/vt_ioctl.c
index ecefe4a..62a52df 100644
--- a/drivers/tty/vt/vt_ioctl.c
+++ b/drivers/tty/vt/vt_ioctl.c
@@ -986,17 +986,25 @@ void vc_SAK(struct work_struct *work)
 	struct vc_data *vc;
 	struct tty_struct *tty;
 
-	guard(console_lock)();
+	console_lock();
 	vc = vc_con->d;
-	if (!vc)
+	if (!vc) {
+		console_unlock();
 		return;
+	}
 
-	/* FIXME: review tty ref counting */
-	tty = vc->port.tty;
+	tty = tty_port_tty_get(&vc->port);
 	/* SAK should also work in all raw modes and reset them properly. */
-	if (tty)
+	if (tty) {
+		console_unlock();
 		__do_SAK(tty);
-	reset_vc(vc);
+		tty_kref_put(tty);
+		console_lock();
+		vc = vc_con->d;
+	}
+	if (vc)
+		reset_vc(vc);
+	console_unlock();
 }
 
 #ifdef CONFIG_COMPAT
-- 
2.53.0


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

end of thread, other threads:[~2026-12-02 18:26 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 18:12 [PATCH] vt: drop console_lock before calling __do_SAK() in vc_SAK() Jiayi Zhang
2026-12-02 18:26 ` sashiko-bot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.