Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

tty: add the option to have a tty reject a new ldisc

... and use it to limit the virtual terminals to just N_TTY. They are
kind of special, and in particular, the "con_write()" routine violates
the "writes cannot sleep" rule that some ldiscs rely on.

This avoids the

BUG: sleeping function called from invalid context at kernel/printk/printk.c:2659

when N_GSM has been attached to a virtual console, and gsmld_write()
calls con_write() while holding a spinlock, and con_write() then tries
to get the console lock.

Tested-by: Tetsuo Handa <penguin-kernel@i-love.sakura.ne.jp>
Cc: Jiri Slaby <jirislaby@kernel.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Daniel Starke <daniel.starke@siemens.com>
Reported-by: syzbot <syzbot+dbac96d8e73b61aa559c@syzkaller.appspotmail.com>
Closes: https://syzkaller.appspot.com/bug?extid=dbac96d8e73b61aa559c
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Link: https://lore.kernel.org/r/20240423163339.59780-1-torvalds@linux-foundation.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Linus Torvalds and committed by
Greg Kroah-Hartman
6bd23e0c a47cf07f

+24
+6
drivers/tty/tty_ldisc.c
··· 545 545 goto out; 546 546 } 547 547 548 + if (tty->ops->ldisc_ok) { 549 + retval = tty->ops->ldisc_ok(tty, disc); 550 + if (retval) 551 + goto out; 552 + } 553 + 548 554 old_ldisc = tty->ldisc; 549 555 550 556 /* Shutdown the old discipline. */
+10
drivers/tty/vt/vt.c
··· 3576 3576 tty_port_put(&vc->port); 3577 3577 } 3578 3578 3579 + /* 3580 + * We can't deal with anything but the N_TTY ldisc, 3581 + * because we can sleep in our write() routine. 3582 + */ 3583 + static int con_ldisc_ok(struct tty_struct *tty, int ldisc) 3584 + { 3585 + return ldisc == N_TTY ? 0 : -EINVAL; 3586 + } 3587 + 3579 3588 static int default_color = 7; /* white */ 3580 3589 static int default_italic_color = 2; // green (ASCII) 3581 3590 static int default_underline_color = 3; // cyan (ASCII) ··· 3704 3695 .resize = vt_resize, 3705 3696 .shutdown = con_shutdown, 3706 3697 .cleanup = con_cleanup, 3698 + .ldisc_ok = con_ldisc_ok, 3707 3699 }; 3708 3700 3709 3701 static struct cdev vc0_cdev;
+8
include/linux/tty_driver.h
··· 154 154 * 155 155 * Optional. Called under the @tty->termios_rwsem. May sleep. 156 156 * 157 + * @ldisc_ok: ``int ()(struct tty_struct *tty, int ldisc)`` 158 + * 159 + * This routine allows the @tty driver to decide if it can deal 160 + * with a particular @ldisc. 161 + * 162 + * Optional. Called under the @tty->ldisc_sem and @tty->termios_rwsem. 163 + * 157 164 * @set_ldisc: ``void ()(struct tty_struct *tty)`` 158 165 * 159 166 * This routine allows the @tty driver to be notified when the device's ··· 379 372 void (*hangup)(struct tty_struct *tty); 380 373 int (*break_ctl)(struct tty_struct *tty, int state); 381 374 void (*flush_buffer)(struct tty_struct *tty); 375 + int (*ldisc_ok)(struct tty_struct *tty, int ldisc); 382 376 void (*set_ldisc)(struct tty_struct *tty); 383 377 void (*wait_until_sent)(struct tty_struct *tty, int timeout); 384 378 void (*send_xchar)(struct tty_struct *tty, u8 ch);