* [PATCH v2] vt: drop console_lock before calling __do_SAK() in vc_SAK()
@ 2026-12-04 5:54 Jiayi Zhang
2026-12-04 6:08 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Jiayi Zhang @ 2026-12-04 5:54 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 is dropped only after the final console_unlock().
Until then it keeps the tty alive and delays con_cleanup(), which drops
the console port's reference, so vt_in_use() stays true and
vt_disallocate() cannot free and reallocate the console before
reset_vc() runs. 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>
---
v2:
- drop the tty reference after the final console_unlock() instead of
before console_lock() is taken again. In v1 the put could release
the last reference while the lock was dropped; con_cleanup() then
dropped the port reference, and the console could be deallocated
and allocated again (INIT_WORK() on SAK_work) before reset_vc()
(Sashiko review)
v1: https://lore.kernel.org/all/20261202181217.4160067-1-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 v1 none of 11, with v2 none of 11.
- 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 v1 none of 9, with v2 none of 10.
No other crash in the runs with v2.
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..ab92b22 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);
+ console_lock();
+ vc = vc_con->d;
+ }
+ if (vc)
+ reset_vc(vc);
+ console_unlock();
+ tty_kref_put(tty);
}
#ifdef CONFIG_COMPAT
--
2.53.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-04 6:08 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 5:54 [PATCH v2] vt: drop console_lock before calling __do_SAK() in vc_SAK() Jiayi Zhang
2026-12-04 6:08 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox