mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jiayi Zhang <zhangjiayi1014@gmail.com>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Jiri Slaby <jirislaby@kernel.org>
Cc: Joshua Rogers <linux@joshua.hu>,
	Wentao Guan <guanwentao@uniontech.com>,
	linux-serial@vger.kernel.org, linux-kernel@vger.kernel.org,
	syzbot+098cefc0911c68db5dab@syzkaller.appspotmail.com,
	syzkaller-bugs@googlegroups.com
Subject: [PATCH 1/2] vt: keyboard: take a tty reference in fn_hold()
Date: Wed,  2 Dec 2026 18:05:55 +0000	[thread overview]
Message-ID: <20261202180556.4149260-1-zhangjiayi1014@gmail.com> (raw)

fn_hold(), the handler of the Hold key, reads vc->port.tty without
taking a reference and then calls start_tty() or stop_tty(), which take
tty->flow.lock. When the last file of that console is closed at the
same time, con_shutdown() clears vc->port.tty, the last reference is
dropped and the tty_struct is freed from a work item, so fn_hold() can
use a freed tty_struct.

syzbot reports this on mainline. Its C reproducer binds Scroll Lock to
K_HOLD, makes VT 63 the foreground console and presses Scroll Lock
through uinput while other threads open and close /dev/tty63. On
v7.3-rc4 with the syzbot config it triggered the report in 4 of 4 runs:

  BUG: KASAN: slab-use-after-free in _raw_spin_lock_irqsave+0x3a/0x60
  Read of size 1 at addr ff110000295cc418 by task syz-executor175/5550
  ...
   rt_spin_unlock+0x71/0x200 kernel/locking/spinlock_rt.c:84
   fn_hold+0xaf/0xd0 drivers/tty/vt/keyboard.c:525
   k_spec+0xf0/0x150 drivers/tty/vt/keyboard.c:648
   kbd_keycode drivers/tty/vt/keyboard.c:1574 [inline]
   kbd_event+0xe0c/0x1a60 drivers/tty/vt/keyboard.c:1592
   input_handle_events_default+0x119/0x1b0 drivers/input/input.c:2614
   input_pass_values+0x74e/0x880 drivers/input/input.c:128
   ...
   uinput_write+0xb26/0xff0 drivers/input/misc/uinput.c:682

  Allocated by task 5549:
   alloc_tty_struct+0x96/0x8e0 drivers/tty/tty_io.c:3075
   tty_init_dev.part.0+0x1e/0x500 drivers/tty/tty_io.c:1373
   tty_open+0x963/0xe40 drivers/tty/tty_io.c:2093

  Freed by task 3452:
   kfree+0x2b9/0x580 mm/slub.c:6801
   process_one_work+0xabf/0x1e00 kernel/workqueue.c:3396

  Last potentially related work creation:
   tty_kref_put+0x7e/0x90 drivers/tty/tty_io.c:1527
   tty_open+0x94b/0xe40 drivers/tty/tty_io.c:2093

Commit e25d47a52693 ("vt: stabilize tty reference in kbd_keycode with
tty_port_tty_get") made kbd_keycode() take a reference, but it drops the
reference before the key handler runs, and fn_hold() reads
vc->port.tty again. Take a reference in fn_hold() with
tty_port_tty_get() as well.

This patch alone does not close the report: the next patch is needed
too.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Reported-by: syzbot+098cefc0911c68db5dab@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=098cefc0911c68db5dab
Cc: stable@vger.kernel.org
Signed-off-by: Jiayi Zhang <zhangjiayi1014@gmail.com>
---
Tested in QEMU with the C reproducer and the kernel config of the syzbot
report (PREEMPT_RT, KASAN), on commit efb44d93a620 (the commit of the
report), 5 VMs per job, each running the reproducer for several minutes
or until the first report:
- without the series: 4 of 4 VMs report the use-after-free in
  fn_hold() (one VM did not boot)
- with patch 1 only:  1 of 3 VMs reports the use-after-free in
  tty_port_tty_get() that patch 2 describes
- with both patches:  two jobs; none of the 7 VMs that ran the
  reproducer reports anything (3 VMs did not boot: a drm vblank
  timeout WARNING during boot, with panic_on_warn set)

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

 drivers/tty/vt/keyboard.c |   10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/drivers/tty/vt/keyboard.c b/drivers/tty/vt/keyboard.c
index 51d6d42..b0dc224 100644
--- a/drivers/tty/vt/keyboard.c
+++ b/drivers/tty/vt/keyboard.c
@@ -514,9 +514,13 @@ static void fn_show_ptregs(struct vc_data *vc)
 
 static void fn_hold(struct vc_data *vc)
 {
-	struct tty_struct *tty = vc->port.tty;
+	struct tty_struct *tty;
 
-	if (rep || !tty)
+	if (rep)
+		return;
+
+	tty = tty_port_tty_get(&vc->port);
+	if (!tty)
 		return;
 
 	/*
@@ -528,6 +532,8 @@ static void fn_hold(struct vc_data *vc)
 		start_tty(tty);
 	else
 		stop_tty(tty);
+
+	tty_kref_put(tty);
 }
 
 static void fn_num(struct vc_data *vc)
-- 
2.53.0


             reply	other threads:[~2026-12-02 18:06 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02 18:05 Jiayi Zhang [this message]
2026-12-02 18:05 ` [PATCH 2/2] vt: let the console port hold a reference to its tty Jiayi Zhang
2026-12-03  4:25   ` 关文涛
2026-12-03  4:37     ` 关文涛
2026-12-03  4:23 ` Re:[PATCH 1/2] vt: keyboard: take a tty reference in fn_hold() 关文涛

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261202180556.4149260-1-zhangjiayi1014@gmail.com \
    --to=zhangjiayi1014@gmail.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=guanwentao@uniontech.com \
    --cc=jirislaby@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-serial@vger.kernel.org \
    --cc=linux@joshua.hu \
    --cc=syzbot+098cefc0911c68db5dab@syzkaller.appspotmail.com \
    --cc=syzkaller-bugs@googlegroups.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®