Received: by 2002:ab2:6d45:0:b0:1fb:d597:ff75 with SMTP id d5csp250072lqr; Wed, 5 Jun 2024 05:12:51 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWhuWcLm3DoHBuBHogYwEwf1bhkIii3+5SKSoOdQjxP8JQ189278CpVhhmkFUkEPIp85Qi+9j37MUOIye+8qCu7IVdRCm5zP0RlEhrkPw== X-Google-Smtp-Source: AGHT+IHBG7EPCxeyG3wIF13ZoljL2ZqfASG3lcEamtH9/8BCyrSVD9qnOgfNC7OS7svo6On9KMZt X-Received: by 2002:a05:620a:112b:b0:792:bc96:233b with SMTP id af79cd13be357-79523d35853mr216092285a.21.1717589570844; Wed, 05 Jun 2024 05:12:50 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717589570; cv=pass; d=google.com; s=arc-20160816; b=uOkod503iMPlj5Op6rLrihoXLtdXf2xfaJ5hhYXlh5HQykLAk35lmctzdmtWoqxPnZ iviRZ8MvIxVTGlmtMYboKffmdGtwD+9Js7qjgC81rLqmUY+dNUs7kalhZ1f0P6kB2MBP tdokLeQ/9L6Qsdkc0FJ7/658kZPhoYdutPdB4uh/QAQYM4qHk9GoURZFhOOKMXPT+/R6 yxfnuD7X6k022znO9ARtG3OZ3pSt4jQpG6BGGzB5UWIohqAjXaPEb8kUMRaQjmCP+QJx pNz+c4qdXybGlWzpSYYatMN+W91Txaj8KKE2B/FF9GXwDzCoqFidt2aHZ2dQ+u1XBZ0P NqsQ== 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=dQpcI7EcydWr8UiblKcT3PsFgl10tMQBJsfgsCb0VAw=; fh=mfXvw4LDbMHQDy2n3L1LV9BTXaEGThhPoKcVLA7Nkxg=; b=x6CEoYD9z1S1t3n4aKiCN/HfiP9BoipEIgGb/IGOa8hOC1MHDYiKClVlnJkUljEQAO z7TgKieFntl+Ux9x5mvcs+7ngZGnNtj72AiVj8QmkKFdLb82D/osqEme8wv9u9EBFeSo BRHcUoL9tI0nZ7i1YpAkJa/XNMQcDjgfj5jrT24mDb4v6csmSNp6DqHW5kWfYoI3mdvP RtimTUukPr54bquf85ulJLBaLJcj/Ctt89tYi6gMaPoRJdEOY2gYR1P/r3E0um1UK19W m3usjrSck94g4k2gyLltjbvIAv/QEOAmG5fceJvLTi8R/s4NXdMH3THI0skelv7IceCi yo5Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ISY9Cnjp; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-202389-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-202389-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. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id d75a77b69052e-4402fe76fcesi10043541cf.285.2024.06.05.05.12.50 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Jun 2024 05:12:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-202389-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ISY9Cnjp; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-202389-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-202389-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 8E4831C22DEE for ; Wed, 5 Jun 2024 12:12:50 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 032D7196C8E; Wed, 5 Jun 2024 11:54:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="ISY9Cnjp" 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 25DEC1B1408; Wed, 5 Jun 2024 11:54:05 +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=1717588445; cv=none; b=rYEc+O+IykW8qKARWrmPJSHmhy27cdXhEbwTj+YQJNGmTCUSEZu1febPu7TaM/9U6VAhxXniR2Wxq1Wda5up2Nxn5tFT2P8T8uB0tK8vEwZwW0YSlmkr9S8bX7UnfsgYbmTqoWWApmohFRsFL7vAmPUDpw2ofggwGqgWF09aC2I= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717588445; c=relaxed/simple; bh=SJAJNUyZcerE1KcaDG4prT3y1/ZdNWBjS5r3I7/alS0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=mcp83lET6lXLHF9Ja4DbfjmqSB2plm/zjlbBeJIIaZahsWqbZgh4R/KDuwwpBBUqqPhNKIyV8UomBehhbF14MdU9E4t10/2LRR6znNi84J1fkbFiVJu4h02E99FqGabAjSZ4CI8O9mnILtjTlYd2j0IrrW43kfFR64LwLAiPjm8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ISY9Cnjp; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id D2EADC32781; Wed, 5 Jun 2024 11:54:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717588445; bh=SJAJNUyZcerE1KcaDG4prT3y1/ZdNWBjS5r3I7/alS0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ISY9CnjpwpZ25npPWrYS+beD1RuxnmmVDyBgFbUB1UtwU6zidoVdTjOyyMbu5gESO rd4+di5K3iJmgYvlf5B4OconrWgFMzGG3f/g3TTf3JALQcnXgc8az1vS8B/ePq1UDT WyuYAWtg9zev7HGjbOgfOWmDb36SpOVPPyXUBKx+/mjveclYq40bWoJiTffo5QSUop +urtJvoRhWyfHEDjJmWRSpc8IdAy8yKMgQZ8l31MCD4OC8/cMX1opM9J5CrVm8IxLG zZ4kmKDTDJmcwgjL0EBfMFsCXFGdKZC60a7uHZaU1ghWRBbIycjbWlZzKqly+6BuyH 2FFMy1cdweXug== 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.1 11/12] tty: add the option to have a tty reject a new ldisc Date: Wed, 5 Jun 2024 07:53:16 -0400 Message-ID: <20240605115334.2963803-11-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240605115334.2963803-1-sashal@kernel.org> References: <20240605115334.2963803-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.1.92 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 776d8a62f77cc..7ca7731fa78ae 100644 --- a/drivers/tty/tty_ldisc.c +++ b/drivers/tty/tty_ldisc.c @@ -546,6 +546,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 48a9ed7c93c97..e2f9348725ff1 100644 --- a/drivers/tty/vt/vt.c +++ b/drivers/tty/vt/vt.c @@ -3440,6 +3440,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) @@ -3566,6 +3575,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 e00034118c7bc..1df868130adce 100644 --- a/include/linux/tty_driver.h +++ b/include/linux/tty_driver.h @@ -155,6 +155,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 @@ -374,6 +381,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, char ch); -- 2.43.0