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 2/2] vt: let the console port hold a reference to its tty
Date: Wed,  2 Dec 2026 18:05:56 +0000	[thread overview]
Message-ID: <20261202180556.4149260-2-zhangjiayi1014@gmail.com> (raw)
In-Reply-To: <20261202180556.4149260-1-zhangjiayi1014@gmail.com>

The keyboard handler runs in atomic context and cannot take the console
lock, so kbd_keycode() and fn_hold() get the tty of the console with
tty_port_tty_get(), which reads vc->port.tty and takes a reference under
vc->port.lock. con_install() and con_shutdown() set and clear
vc->port.tty directly, under the console lock only, and the port holds
no reference to the tty. A reader can therefore read the pointer before
con_shutdown() clears it and take its reference only after
release_tty() has dropped the last one and the tty has been freed.
kbd_keycode() has had this window since commit e25d47a52693 ("vt:
stabilize tty reference in kbd_keycode with tty_port_tty_get") made it
use tty_port_tty_get(). With only the previous patch applied, the
syzbot reproducer triggers it in fn_hold():

  BUG: KASAN: slab-use-after-free in tty_port_tty_get+0x64/0xf0
  Write of size 4 at addr ff11000030529000 by task syz-executor688/5536
  ...
   tty_port_tty_get+0x64/0xf0 drivers/tty/tty_port.c:323
   fn_hold drivers/tty/vt/keyboard.c:517 [inline]
   fn_hold+0x37/0xc0 drivers/tty/vt/keyboard.c:510
   k_spec+0xf0/0x150 drivers/tty/vt/keyboard.c:654
   kbd_keycode drivers/tty/vt/keyboard.c:1580 [inline]
   kbd_event+0xe0c/0x1a60 drivers/tty/vt/keyboard.c:1598
   ...
   uinput_write+0xb26/0xff0 drivers/input/misc/uinput.c:682

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

  Last potentially related work creation:
   release_tty+0x4ce/0x5c0 drivers/tty/tty_io.c:1566
   tty_release_struct+0xb7/0xe0 drivers/tty/tty_io.c:1665
   tty_release+0xe3b/0x1480 drivers/tty/tty_io.c:1825

Use tty_port_tty_set() in con_install() and con_shutdown(), as
tty_port_open() does for other drivers. vc->port.tty is then set and
cleared under vc->port.lock, and the port holds a reference to the tty
from install to shutdown. A reader that finds the tty in vc->port.tty
takes its reference while the port's reference still keeps the tty
alive. The reference is dropped in con_shutdown(), which release_tty()
calls before it drops its own reference.

Wentao Guan proposed the same change to con_install() and
con_shutdown() in April; it received no reply.

Fixes: e25d47a52693 ("vt: stabilize tty reference in kbd_keycode with tty_port_tty_get")
Reported-by: syzbot+098cefc0911c68db5dab@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=098cefc0911c68db5dab
Link: https://lore.kernel.org/all/20260410065546.2106592-1-guanwentao@uniontech.com/
Cc: stable@vger.kernel.org
Signed-off-by: Jiayi Zhang <zhangjiayi1014@gmail.com>
---
Both patches are needed for the syzbot report; the test results are
below the --- line of patch 1/2. With both patches applied, none of the
7 VMs that ran the reproducer reported anything.

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

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

diff --git a/drivers/tty/vt/vt.c b/drivers/tty/vt/vt.c
index 6a6eb8b..7cfb9d7 100644
--- a/drivers/tty/vt/vt.c
+++ b/drivers/tty/vt/vt.c
@@ -3875,7 +3875,7 @@ static int con_install(struct tty_driver *driver, struct tty_struct *tty)
 		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) {
@@ -3908,7 +3908,7 @@ static void con_shutdown(struct tty_struct *tty)
 	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)
-- 
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 [PATCH 1/2] vt: keyboard: take a tty reference in fn_hold() Jiayi Zhang
2026-12-02 18:05 ` Jiayi Zhang [this message]
2026-12-03  4:25   ` Re:[PATCH 2/2] vt: let the console port hold a reference to its tty 关文涛
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-2-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®