Received: by 2002:ab2:6203:0:b0:1f5:f2ab:c469 with SMTP id o3csp3073111lqt; Tue, 23 Apr 2024 09:33:57 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCXSvTQjk8UqUPWyRGRQ891UGpiyzxsNRhFE2KZxCKlhvQWVMTKX+3hLjsVKdX40iTr3wh2vOV5Vx5i5q9x0Dy3i1WaTRk0ErDRLgyfb8g== X-Google-Smtp-Source: AGHT+IFHVs+Cj9F7QrFgjZ+tC4SxSZzOKawynLcGuAbgjF9CX/MQWTJGTlcyP83HDNdZGkyuZIp4 X-Received: by 2002:ac2:4d93:0:b0:515:beef:837f with SMTP id g19-20020ac24d93000000b00515beef837fmr55849lfe.29.1713890037185; Tue, 23 Apr 2024 09:33:57 -0700 (PDT) Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id f6-20020a170906560600b00a51df3e9f97si7313548ejq.183.2024.04.23.09.33.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Apr 2024 09:33:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-155579-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linux-foundation.org header.s=korg header.b=LB7VZPvF; arc=fail (body hash mismatch); spf=pass (google.com: domain of linux-kernel+bounces-155579-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-155579-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id E16301F23337 for ; Tue, 23 Apr 2024 16:33:56 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4E93B13B78A; Tue, 23 Apr 2024 16:33:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.b="LB7VZPvF" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 24E01160 for ; Tue, 23 Apr 2024 16:33:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713890030; cv=none; b=bNSpeoDdDPBpfs6TkmP7L2/DjImj8M1wlh9k9PtSHiDcMRPXbsEjrI/KEYcp7TJtjykQjl4aD1ZsrytOjR8xDdbrbV6+ULEzmxs0Ox1kt6Nl4rQg3OiQpxtFPxCLrH3lU8bvJpFBEQqgOjLuCzhHURCrmtRbBZxj2acTvLYNPVs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713890030; c=relaxed/simple; bh=fSRRhDtcU4bIDZySRttGghupn5BALmJBJEdE58Pk9Kw=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=GRToujdMYFEwZR8pDmn1VLtuF+3bXo2Z1A+OMr/Aoky2KUw9UFB7Av0PQ26hC7/pxPp5srrHasnRdg7cmMpS9Y747K4ES6kBm/I243F43BZrXQ+LqMpQ0Ax7wH2xaIOqPnz8qGbi1/wMWX3u05O81sCSJqjwPje+/32wxoEATqU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.b=LB7VZPvF; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id B3263C116B1; Tue, 23 Apr 2024 16:33:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1713890029; bh=fSRRhDtcU4bIDZySRttGghupn5BALmJBJEdE58Pk9Kw=; h=From:To:Cc:Subject:Date:From; b=LB7VZPvF2Qe10FM/AjH8w1o+g6p2LIK5uOI86+N0dsDU8RChv8VNgKkcdeoNcw0oR pv7T6BEosRCfwUG6waejq7vtrGWcLdyDm5sCQTCfEH1jTXFDdUrZWDtxaGsHOrgbyR NP9hU+8c6OadzUR2I4kSWRUSX5btbXQJfJDo8NUw= From: Linus Torvalds To: Greg Kroah-Hartman Cc: linux-kernel@vger.kernel.org, Linus Torvalds , Tetsuo Handa , Jiri Slaby , Andrew Morton , Daniel Starke , syzbot Subject: [PATCH] tty: add the option to have a tty reject a new ldisc Date: Tue, 23 Apr 2024 09:33:39 -0700 Message-ID: <20240423163339.59780-1-torvalds@linux-foundation.org> X-Mailer: git-send-email 2.44.0.330.g4d18c88175 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit .. 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 Cc: Jiri Slaby Cc: Andrew Morton Cc: Daniel Starke Reported-by: syzbot Closes: https://syzkaller.appspot.com/bug?extid=dbac96d8e73b61aa559c Signed-off-by: Linus Torvalds --- drivers/tty/tty_ldisc.c | 6 ++++++ drivers/tty/vt/vt.c | 10 ++++++++++ include/linux/tty_driver.h | 8 ++++++++ 3 files changed, 24 insertions(+) diff --git a/drivers/tty/tty_ldisc.c b/drivers/tty/tty_ldisc.c index 3f68e213df1f..d80e9d4c974b 100644 --- a/drivers/tty/tty_ldisc.c +++ b/drivers/tty/tty_ldisc.c @@ -545,6 +545,12 @@ int tty_set_ldisc(struct tty_struct *tty, int disc) goto out; } + if (tty->ops->ldisc_ok) { + retval = tty->ops->ldisc_ok(tty, disc); + if (retval) + goto out; + } + old_ldisc = tty->ldisc; /* Shutdown the old discipline. */ diff --git a/drivers/tty/vt/vt.c b/drivers/tty/vt/vt.c index 9b5b98dfc8b4..cd87e3d1291e 100644 --- a/drivers/tty/vt/vt.c +++ b/drivers/tty/vt/vt.c @@ -3576,6 +3576,15 @@ static void con_cleanup(struct tty_struct *tty) tty_port_put(&vc->port); } +/* + * We can't deal with anything but the N_TTY ldisc, + * because we can sleep in our write() routine. + */ +static int con_ldisc_ok(struct tty_struct *tty, int ldisc) +{ + return ldisc == N_TTY ? 0 : -EINVAL; +} + static int default_color = 7; /* white */ static int default_italic_color = 2; // green (ASCII) static int default_underline_color = 3; // cyan (ASCII) @@ -3695,6 +3704,7 @@ static const struct tty_operations con_ops = { .resize = vt_resize, .shutdown = con_shutdown, .cleanup = con_cleanup, + .ldisc_ok = con_ldisc_ok, }; static struct cdev vc0_cdev; diff --git a/include/linux/tty_driver.h b/include/linux/tty_driver.h index 7372124fbf90..dd4b31ce6d5d 100644 --- a/include/linux/tty_driver.h +++ b/include/linux/tty_driver.h @@ -154,6 +154,13 @@ struct serial_struct; * * Optional. Called under the @tty->termios_rwsem. May sleep. * + * @ldisc_ok: ``int ()(struct tty_struct *tty, int ldisc)`` + * + * This routine allows the @tty driver to decide if it can deal + * with a particular @ldisc. + * + * Optional. Called under the @tty->ldisc_sem and @tty->termios_rwsem. + * * @set_ldisc: ``void ()(struct tty_struct *tty)`` * * This routine allows the @tty driver to be notified when the device's @@ -372,6 +379,7 @@ struct tty_operations { void (*hangup)(struct tty_struct *tty); int (*break_ctl)(struct tty_struct *tty, int state); void (*flush_buffer)(struct tty_struct *tty); + int (*ldisc_ok)(struct tty_struct *tty, int ldisc); void (*set_ldisc)(struct tty_struct *tty); void (*wait_until_sent)(struct tty_struct *tty, int timeout); void (*send_xchar)(struct tty_struct *tty, u8 ch); -- 2.44.0.330.g4d18c88175