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

* Re: [PATCH] vt: drop console_lock before calling __do_SAK() in vc_SAK()
  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
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-02 18:26 UTC (permalink / raw)
  To: Jiayi Zhang; +Cc: linux-serial

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] vt: premature tty_kref_put() in vc_SAK() before re-acquiring
  console_lock
--

commit ae876e3ff5feb53c222ade62935a9819ac41e34c
Author: Jiayi Zhang <zhangjiayi1014@gmail.com>

vt: drop console_lock before calling __do_SAK() in vc_SAK()

This commit drops console_lock around __do_SAK() in vc_SAK() while holding
a tty reference to avoid a lock ordering deadlock between console_lock and
the tty buffer lock.

> diff --git a/drivers/tty/vt/vt_ioctl.c b/drivers/tty/vt/vt_ioctl.c
> index ecefe4a666f26..62a52df1c2c15 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();
>  }

[Severity: Medium]
Can calling tty_kref_put(tty) before re-acquiring console_lock() allow the
console to be freed and reallocated before reset_vc() runs?

If the process killed by __do_SAK() closes the tty before tty_kref_put(),
dropping the reference outside console_lock() can trigger con_cleanup()
and drop vc->port.kref to 1 while unlocked:

kworker (vc_SAK)                  other tasks
__do_SAK(tty)
  /* last user exits, closes tty */
tty_kref_put(tty)
  release_one_tty()
    con_cleanup()
      tty_port_put(&vc->port)
                                  vt_disallocate()
                                    kfree(vc)
                                  vt_setactivate()
                                    vc_allocate()
                                      INIT_WORK(&vc_con->SAK_work, ...)
                                    /* configures new vc_con->d */
console_lock()
vc = vc_con->d
reset_vc(vc)                      <-- clobbers newly allocated vc

In addition to reset_vc() overwriting the new console's vt_mode and
vt_pid, vc_allocate() calls INIT_WORK() on vc_con->SAK_work while vc_SAK()
is still running, which corrupts the work item if fn_SAK() re-queued it.

Putting the tty reference after console_unlock() at the end of vc_SAK()
keeps vc->port.kref elevated across the entire unlocked window so
vt_disallocate() still sees the console as in use.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261202181217.4160067-1-zhangjiayi1014@gmail.com?part=1

^ permalink raw reply	[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.