Received: by 2002:ab2:6d45:0:b0:1fb:d597:ff75 with SMTP id d5csp242853lqr; Wed, 5 Jun 2024 05:02:26 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXmB07YT0vIKtznmnkLyzs/4sWZdNNIFJ7AqX6GsbFs0jVWL2nwd7OyfPu7lxDFVgAHIvrsY4pAs2j+jl5Md8DdNoMb7O/S9G13FDbn8Q== X-Google-Smtp-Source: AGHT+IEIBgfMZ4FOsUuGZT70fMo+y4cvHje11bygFmiiaSjd6m3qt6McbwEQDoJ1C2iRlJgSfXVY X-Received: by 2002:ac8:5f14:0:b0:43a:a8ad:7954 with SMTP id d75a77b69052e-4402b614a89mr26015931cf.28.1717588946297; Wed, 05 Jun 2024 05:02:26 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717588946; cv=pass; d=google.com; s=arc-20160816; b=EhICw2KddxfKTkZ6JoGQmOFp3oNZbdqIfFC+oUCwfcMsTobgA3rqXjsuoZ+I5dwrDG pNCup/jMvFC1/vfM0AOO9VlksDigDv62iUTJYcKsqaJSsHO+NXw64JdbXcbMRqk24FWc xzyKGdsC5VQjrh07ZQz3UsHcLf8mwFyd9p7ylJ6FusCuqkLLf1wT9on6rDbEQs+FToz7 WMKfxwZ+cWWBHnK+zs/rW95ORKSaHatO4jbwR7Q4QBTs1ySJG+8qFfXFo68zX2mp/dMg Dgm9jyd+TsaKh5BokYgDsd6qu5MR+CDRwS6YyjTChb3xbSTUGmhf5PHwIIMMhvjkBI4X ynkA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=m+oOJNtghovq+QOt2F3Q5QUY9PnlobeCr4sAhygikbw=; fh=mfXvw4LDbMHQDy2n3L1LV9BTXaEGThhPoKcVLA7Nkxg=; b=kHBM9RJMT77jzWSSRv44tQPfbC7vzsEnD3FhV/uCnqKU0NNBTDZ6scX0TsPYb4Qd/p 3jhAP2C3hPyx9WkIaCztPvgmB6ZfTPfDpHgGxugbLCfSdqN/JXrF5um5oA4giEW6fMp/ 1znYoEuI7Xt3L7QhF2TKz2FxV3Dx4uB1TDXtuvtbGqyhoDebTvcvSx2RlJrN6C3KJygi me+oxKmyOqA/O/n8gUq6w8aW+7io1njdZoteGcFB2Ua6ThrtgyGuW1mFlSxoj1DTOyIv xs3OmmPoEEXuJuP/oxdEjBnnMaNNdjx7aZETJJ4uFW398CbzS3KbCWbipN1JMIH3iG+k B8og==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=rBYs6BaH; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-202348-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-202348-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id d75a77b69052e-43ff2581023si129928661cf.437.2024.06.05.05.02.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Jun 2024 05:02:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-202348-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=rBYs6BaH; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-202348-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-202348-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 07DA81C2360D for ; Wed, 5 Jun 2024 12:02:26 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 54D3F1A187A; Wed, 5 Jun 2024 11:51:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="rBYs6BaH" 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 7431D1A1860; Wed, 5 Jun 2024 11:51:46 +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=1717588306; cv=none; b=INAwsJiCDaMHEN2Dl46UI4iDvoCHrs+hr08Qrd31ccfte4ACmLKe28Ph6C5vfIwNxN5O9BefVgrqAwr7ix+u7z7Ji6u1XKLOL6roh4ORNLLW+I8Ci8CxqBG/Wg7VQNVEgZt0Q46m1MnMeh1+k4RxPnGR8YHgVymSAJWodtN4v/M= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717588306; c=relaxed/simple; bh=/koGbcjt/brA1y2OEcG0TQp5Myw75w2PN2xhFyfAxqg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=eSAyHCKsJ/ewEsOEWqdpdCApvKNiSDf1XgXuOGmf0SWlK2ghqjXt2F8/UsYla1Hx77fkkHiCiFlv5ra4EG+ehFI+h1spo5swyBB+5FyZJwkBWkh7utBwfyilMc3zuWnBItmtjyLmDvyQdBTd1Qy5j3WmSbzXBqD/C3VOXRuJCtc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=rBYs6BaH; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 264ABC32786; Wed, 5 Jun 2024 11:51:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717588306; bh=/koGbcjt/brA1y2OEcG0TQp5Myw75w2PN2xhFyfAxqg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rBYs6BaHUfKmIq+xNOesILNOuDpU42yoCTJtoou2jMaWyB6uGcafhV6JVpRpVfqZV MZYV/1UpE0heY6MUGNIxtzikCjnnF/OKFAI3TRVNWGgRVr+D83ex8Pu+/it4cB/J1k Vxo5gnWwoRoR9aR2VoGmZGbCSG5kyJl2wczzTjCyW0pT9pxEKn+gzLhhdQ4mpgH7P/ 1TJvprCUebZf9fZsTk8T/r1oM0G7VJGn+XPITU4CUgca7Kwcwy9wN2LnJnWpKVEprN m+z7wPbT5wIv1y0RdiPo9lRwarTKlJ3eADDBMLav88jNyAQSuz2KJTNrKJrVfghP5Q OuRDn9PIdO/jQ== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Linus Torvalds , Tetsuo Handa , Jiri Slaby , Andrew Morton , Daniel Starke , syzbot , Greg Kroah-Hartman , Sasha Levin , linux-serial@vger.kernel.org Subject: [PATCH AUTOSEL 6.8 17/24] tty: add the option to have a tty reject a new ldisc Date: Wed, 5 Jun 2024 07:50:27 -0400 Message-ID: <20240605115101.2962372-17-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240605115101.2962372-1-sashal@kernel.org> References: <20240605115101.2962372-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore X-stable-base: Linux 6.8.12 Content-Transfer-Encoding: 8bit From: Linus Torvalds [ Upstream commit 6bd23e0c2bb6c65d4f5754d1456bc9a4427fc59b ] ... 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 Link: https://lore.kernel.org/r/20240423163339.59780-1-torvalds@linux-foundation.org Signed-off-by: Greg Kroah-Hartman Signed-off-by: Sasha Levin --- 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 3f68e213df1f7..d80e9d4c974b4 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 52e6ca1ba21df..09126aed97bc1 100644 --- a/drivers/tty/vt/vt.c +++ b/drivers/tty/vt/vt.c @@ -3396,6 +3396,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) @@ -3515,6 +3524,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 7372124fbf90b..dd4b31ce6d5d4 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.43.0