From patchwork Sat Oct 15 04:16:23 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 13007575 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 594F2C433FE for ; Sat, 15 Oct 2022 04:16:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229459AbiJOEQc (ORCPT ); Sat, 15 Oct 2022 00:16:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52016 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229498AbiJOEQb (ORCPT ); Sat, 15 Oct 2022 00:16:31 -0400 Received: from mail-pl1-x631.google.com (mail-pl1-x631.google.com [IPv6:2607:f8b0:4864:20::631]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EF92B6AA04 for ; Fri, 14 Oct 2022 21:16:29 -0700 (PDT) Received: by mail-pl1-x631.google.com with SMTP id 10so6480989pli.0 for ; Fri, 14 Oct 2022 21:16:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=4Zhd+IrU4jdtHIjl5cwKXHkAtq/ao6g5qBaiCZACrNE=; b=eKzl19ovq5OG+t8XnvGfWR1OvcArXyvG9mkw0CKMUwYYVjX691mO+wrpOo+zkwmmTR Jj8vKsc46HvtvWOQyzHPRc3V68Z6fjecVH0KshVGn3l8ArzRAovZJDBl4zHu8GY4jtQ8 A4uLljfGvnTtw1ZJT1XKaP8JOvK7U8GggE9eU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=4Zhd+IrU4jdtHIjl5cwKXHkAtq/ao6g5qBaiCZACrNE=; b=8OJ1sNkhlyUB7fFuJbMI8GXCH7W7IvqNQzKRS/L4nMukEe2c7HBcv9wAto0I1maPgH bxBBwJzGjN4cn1TYL5tIPpMnhOnDEPfmR0IgkWyc6E5o4PJ08VU0L3Y4mNo6eTA2CC+A vCNnReEp7UyxM8lDXZgVvWIMYr4JGpndKVHeJL2hKdsKJ2sMZFsaJmMjqBo66p2i+3qJ hF7rBKvPf9hyse1cNPeVBeEOPIcw+4rGGQNOtbQWJFOJOacm99cFwLS1nrjsmlnISXMn Nz7BehfpdkIToSVhqj0fM5Gop5RN6vxsmTxoLOK5V8FN/doztelZ7mU3mCoZjAr0ArUz GtOA== X-Gm-Message-State: ACrzQf3I6k93JrgqCc86NSmDeQgNhrnoGJWPIUeaB+FNp6s6rQfi0JZJ lUGiI2hgWKvXCQA29uwCEaQx4w== X-Google-Smtp-Source: AMsMyM4RA0/+xXyzB7ntwgC1whlwlVKc36cked5j5EoDP9EUDD80ayXiBpWbUeD0aFFoU+5ZEnRwOg== X-Received: by 2002:a17:902:8205:b0:185:33e:2a0e with SMTP id x5-20020a170902820500b00185033e2a0emr1146421pln.92.1665807389274; Fri, 14 Oct 2022 21:16:29 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id t14-20020a1709027fce00b00176be23bbb3sm2421738plb.172.2022.10.14.21.16.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Oct 2022 21:16:27 -0700 (PDT) From: Kees Cook To: Greg Kroah-Hartman Cc: Kees Cook , Jiri Slaby , Simon Brand , linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [PATCH 1/2] tty: Move sysctl setup into "core" tty logic Date: Fri, 14 Oct 2022 21:16:23 -0700 Message-Id: <20221015041626.1467372-1-keescook@chromium.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221015041352.never.966-kees@kernel.org> References: <20221015041352.never.966-kees@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3538; h=from:subject; bh=UfHWy9+3SRLAJXHVd4eYVy04EbNnVYsHoiv7mjWvzd0=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBjSjQY8BlG6QlYL+LP5sq2Ex0rIi4OkDlCnsJriKUh vYr86t6JAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCY0o0GAAKCRCJcvTf3G3AJnMZD/ 4ywyAmxaAppPe90zfX9UHpj/BtWNCK2vBmpwT5/+vq5qGOnOwB82XyKnY+IpBe76u6jrPBvs48s0mS jm1fuCT/vDpeOkN+HvLC/aNUGuEGf4YYr6AcbvZYi46q2pCajEQKGcOAlUtvXk7mVNjpdb7w9sb/R0 h9Yzxb4YKmMLFVeTA5oMq8BpMVyg/wXcpvCgnYudyz98y0NyMKSrS2JaMy62t4lY8VEBUFypHddd0L 4qqQfD9LSEx7fLR/o4K2zHkJFojDUriV8ZIlmTlNl4qIfU3DRbY1VBMa+Ng2sAzzn93SgPgDXgoFuH US9tSvBqxvUtscehs3TFIsmbuBtzEVA5PhET/4BkqUNfGSEzmc3AmX1bxCbYhukNoXbc/1zfnqjZzc Q+OGWNQg7ml4Zd+FvEupwNNUUISQyvi2roENwKQtf+Ipiu1Po7TE5ZLwbDK2stc98MyD+EHplAU2vk 5hvvx+WMGtlzLPA78hhfA8dmnTrKaOlnKbTQ2ONR5K86b+H52b2XJ3x05Qy2zAkHTVxyoZ6P/ZMj+x 8xtcqSOOz1BzZfXcUSWBlV0Es/LFzEe6w2fcM52x+F/6zNOrvbFL2z6pe+ZnvJ1Q/6vGnKBTI6BB47 3gPxAFKWcW/wnczv+KwY9VpIrrjlcHZPippJkcACFos8zwJUmRc/3iqzbGzw== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 Precedence: bulk List-ID: X-Mailing-List: linux-hardening@vger.kernel.org In preparation for adding another sysctl to the tty subsystem, move the tty setup code into the "core" tty code, which contains tty_init() itself. Cc: Greg Kroah-Hartman Cc: Jiri Slaby Signed-off-by: Kees Cook --- drivers/tty/tty.h | 2 +- drivers/tty/tty_io.c | 34 ++++++++++++++++++++++++++++++++-- drivers/tty/tty_ldisc.c | 38 +------------------------------------- 3 files changed, 34 insertions(+), 40 deletions(-) diff --git a/drivers/tty/tty.h b/drivers/tty/tty.h index f310a8274df1..4938ed3a44a5 100644 --- a/drivers/tty/tty.h +++ b/drivers/tty/tty.h @@ -93,7 +93,7 @@ void tty_ldisc_release(struct tty_struct *tty); int __must_check tty_ldisc_init(struct tty_struct *tty); void tty_ldisc_deinit(struct tty_struct *tty); -void tty_sysctl_init(void); +int tty_ldisc_autoload; /* tty_audit.c */ #ifdef CONFIG_AUDIT diff --git a/drivers/tty/tty_io.c b/drivers/tty/tty_io.c index 82a8855981f7..b397b223eada 100644 --- a/drivers/tty/tty_io.c +++ b/drivers/tty/tty_io.c @@ -3581,13 +3581,44 @@ void console_sysfs_notify(void) sysfs_notify(&consdev->kobj, NULL, "active"); } +static struct ctl_table tty_table[] = { + { + .procname = "ldisc_autoload", + .data = &tty_ldisc_autoload, + .maxlen = sizeof(tty_ldisc_autoload), + .mode = 0644, + .proc_handler = proc_dointvec, + .extra1 = SYSCTL_ZERO, + .extra2 = SYSCTL_ONE, + }, + { } +}; + +static struct ctl_table tty_dir_table[] = { + { + .procname = "tty", + .mode = 0555, + .child = tty_table, + }, + { } +}; + +static struct ctl_table tty_root_table[] = { + { + .procname = "dev", + .mode = 0555, + .child = tty_dir_table, + }, + { } +}; + /* * Ok, now we can initialize the rest of the tty devices and can count * on memory allocations, interrupts etc.. */ int __init tty_init(void) { - tty_sysctl_init(); + register_sysctl_table(tty_root_table); cdev_init(&tty_cdev, &tty_fops); if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) || register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0) @@ -3609,4 +3640,3 @@ int __init tty_init(void) #endif return 0; } - diff --git a/drivers/tty/tty_ldisc.c b/drivers/tty/tty_ldisc.c index 776d8a62f77c..e758f44729e7 100644 --- a/drivers/tty/tty_ldisc.c +++ b/drivers/tty/tty_ldisc.c @@ -117,7 +117,7 @@ static void put_ldops(struct tty_ldisc_ops *ldops) raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags); } -static int tty_ldisc_autoload = IS_BUILTIN(CONFIG_LDISC_AUTOLOAD); +int tty_ldisc_autoload = IS_BUILTIN(CONFIG_LDISC_AUTOLOAD); /** * tty_ldisc_get - take a reference to an ldisc @@ -817,39 +817,3 @@ void tty_ldisc_deinit(struct tty_struct *tty) tty_ldisc_put(tty->ldisc); tty->ldisc = NULL; } - -static struct ctl_table tty_table[] = { - { - .procname = "ldisc_autoload", - .data = &tty_ldisc_autoload, - .maxlen = sizeof(tty_ldisc_autoload), - .mode = 0644, - .proc_handler = proc_dointvec, - .extra1 = SYSCTL_ZERO, - .extra2 = SYSCTL_ONE, - }, - { } -}; - -static struct ctl_table tty_dir_table[] = { - { - .procname = "tty", - .mode = 0555, - .child = tty_table, - }, - { } -}; - -static struct ctl_table tty_root_table[] = { - { - .procname = "dev", - .mode = 0555, - .child = tty_dir_table, - }, - { } -}; - -void tty_sysctl_init(void) -{ - register_sysctl_table(tty_root_table); -} From patchwork Sat Oct 15 04:16:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 13007576 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5CA35C433FE for ; Sat, 15 Oct 2022 04:16:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229736AbiJOEQd (ORCPT ); Sat, 15 Oct 2022 00:16:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52022 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229608AbiJOEQc (ORCPT ); Sat, 15 Oct 2022 00:16:32 -0400 Received: from mail-pf1-x431.google.com (mail-pf1-x431.google.com [IPv6:2607:f8b0:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B1C8E6A492 for ; Fri, 14 Oct 2022 21:16:29 -0700 (PDT) Received: by mail-pf1-x431.google.com with SMTP id f140so6618015pfa.1 for ; Fri, 14 Oct 2022 21:16:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=EZHH+G6x30+/1izQi/oxOuO2T5/3nIdMv8v68SVvGTY=; b=aDPituHhzFtnk38jDFo7U2mVpu1yKW8iKuh2KxLxBsO4z7tBkl8PBsIjhT0hKbeLuE 6YwTdk3W2w/+8qJdhSj5UujeuqHfe062tfhZJXqKK4M0qB9KRafuMUvE5lI9cxRiSY93 4XeydxFTyFhmvzUMp7+gjQdtB8IiCFTTVCOKs= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=EZHH+G6x30+/1izQi/oxOuO2T5/3nIdMv8v68SVvGTY=; b=JY+TjLF5M60U1j/NEhpd9nS5pCxCQcu2O6ddwSRITHfvOpxiL+M/CdVU0GTi5k/76+ YdFdFZOjqe0vJQmX8P64aUip+EvCx/oJd97K7VoojQ/dfuLqnWL/pYditgzQvqaaw6Sc tcZw7nZhUJuXlr0vhsZwR0gnZh7/0WGeG3VMFv4SMgSB6GZSvdDITP5+mBLzDPk+pR69 sQ9ku5bfI5OCFC1wlrrKiSP9s+TcYvv5s1PLE6XlOpgHDkKnwyeTm2VjaMXC06iIqPsb ZIJFOg6eH3WGplYBsgtJj593FMaHNt368QAaxp1A4XbaoNE4sOWbxPJK+P+vH6oey16v g2Qg== X-Gm-Message-State: ACrzQf1gBxU2ZDlrxvDjxGFPyO3Z29YQQxJmhtbZCu1BLaaNnd5UrgC8 h0T/WE1CltxLUr2xw2N3/lUV4g== X-Google-Smtp-Source: AMsMyM5yENby/w6DnsM607zqpycxRY+ZNA+6OUdIr5nL4SLgx8vhfsvGtNrE10++QvyngTUbvRpbsg== X-Received: by 2002:aa7:8895:0:b0:565:e8b7:8494 with SMTP id z21-20020aa78895000000b00565e8b78494mr1273946pfe.82.1665807388938; Fri, 14 Oct 2022 21:16:28 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id g20-20020aa796b4000000b00561beff1e09sm2486377pfk.164.2022.10.14.21.16.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Oct 2022 21:16:27 -0700 (PDT) From: Kees Cook To: Greg Kroah-Hartman Cc: Kees Cook , Jiri Slaby , Simon Brand , linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [PATCH 2/2] tty: Allow TIOCSTI to be disabled Date: Fri, 14 Oct 2022 21:16:24 -0700 Message-Id: <20221015041626.1467372-2-keescook@chromium.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221015041352.never.966-kees@kernel.org> References: <20221015041352.never.966-kees@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3573; h=from:subject; bh=KrYqJjY//fTGi+hxHkKSxhOoF4X4ro3v63esb67QcXE=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBjSjQYOqf4IDT3hGIyRgfciwgCGZXKO9Tqt2gpIa+s ZvVCwMWJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCY0o0GAAKCRCJcvTf3G3AJv3mD/ 9iBDYh+cnm7f+yTDU+sFNRNWREo+3hSR7QgfvS4PSRPQkmMObiiBOPKeOo287WzLuWgMja11TI7UQM MrW9PXxo49EA8GbdeLdlRPY30CM6XnChQYUFNIo/zOMZh4veciVu+NxFsj2A1AT8iC572UfOn2WI+n W5F5Z0b5Y3e2T5Tvm5IJwQ7XwGJcjxVyetgkyc0md+ZfQYjcAlJT6DYzEvLDpi4uwcY0ul7+4P+Hb0 AYy6tANGtC/RfzHoWuZ15r7h9ZYQunBT8mjk0Lf0HaeyxM/83axJNxcQzqRCSFMMwGwJNOXR+w/R8m vlCrPvrk3VTibCp6llt7EjQOuzmzFPCz+jLwuTHPTlu2vO6y0xxicS749Nd4Yib55FIDKhyw2rD/r8 4w9tUYXm3fwKKvcPhjuK2GOEpcW+ekJX1D2joihuBR+60tHrlxjIqqKU3wrVYqFECw6EnPvaAQpR1B qjlivJ0QhKQJYOj4EYw6m0cbDNWUiBms5hC5GJ3Ln5sYGJFk7xBQBw70TNGCK0XzLdtx8BDVMVPvWj /+jqieXkIZlviocmXP46hxFrFGxHPUKGBg7CAuFGFYKJ0X25Y9QEtPattPYf9sJc3oEBXWRiL3T4TU pTbHIcYf9QWiUsx2RaxOz+G7ghXHjRSsWPZZKX/XMta95w50XqPnTSGXJDZQ== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 Precedence: bulk List-ID: X-Mailing-List: linux-hardening@vger.kernel.org TIOCSTI continues its long history of being used in privilege escalation attacks[1]. Prior attempts to provide a mechanism to disable this have devolved into discussions around creating full-blown LSMs to provide arbitrary ioctl filtering, which is hugely over-engineered -- only TIOCSTI is being used this way. 3 years ago OpenBSD entirely removed TIOCSTI[2], Android has had it filtered for longer[3], and the tools that had historically used TIOCSTI either do not need it, are not commonly built with it, or have had its use removed. Provide a simple CONFIG and global sysctl to disable this for the system builders who have wanted this functionality for literally decades now, much like the ldisc_autoload CONFIG and sysctl. [1] https://lore.kernel.org/linux-hardening/Y0m9l52AKmw6Yxi1@hostpad [2] https://undeadly.org/cgi?action=article;sid=20170701132619 [3] https://lore.kernel.org/lkml/CAFJ0LnFGRuEEn1tCLhoki8ZyWrKfktbF+rwwN7WzyC_kBFoQVA@mail.gmail.com/ Cc: Greg Kroah-Hartman Cc: Jiri Slaby Cc: Simon Brand Signed-off-by: Kees Cook --- drivers/tty/Kconfig | 19 +++++++++++++++++++ drivers/tty/tty_io.c | 13 +++++++++++++ 2 files changed, 32 insertions(+) diff --git a/drivers/tty/Kconfig b/drivers/tty/Kconfig index cc30ff93e2e4..d35fc068da74 100644 --- a/drivers/tty/Kconfig +++ b/drivers/tty/Kconfig @@ -149,6 +149,25 @@ config LEGACY_PTY_COUNT When not in use, each legacy PTY occupies 12 bytes on 32-bit architectures and 24 bytes on 64-bit architectures. +config LEGACY_TIOCSTI + bool "Allow legacy TIOCSTI usage" + default y + help + Historically the kernel has allowed TIOCSTI, which will push + characters into a controlling TTY. This continues to be used + as a malicious privilege escalation mechanism, and provides no + meaningful real-world utility any more. Its use is considered + a dangerous legacy operation, and can be disabled on most + systems. + + Say 'Y here only if you have confirmed that your system's + userspace depends on this functionality to continue operating + normally. + + This functionality can be changed at runtime with the + dev.tty.legacy_tiocsti sysctl. This configuration option sets + the default value of the sysctl. + config LDISC_AUTOLOAD bool "Automatically load TTY Line Disciplines" default y diff --git a/drivers/tty/tty_io.c b/drivers/tty/tty_io.c index b397b223eada..29956c5d7778 100644 --- a/drivers/tty/tty_io.c +++ b/drivers/tty/tty_io.c @@ -2275,11 +2275,15 @@ static int tty_fasync(int fd, struct file *filp, int on) * * Called functions take tty_ldiscs_lock * * current->signal->tty check is safe without locks */ +int tty_legacy_tiocsti __read_mostly = IS_BUILTIN(CONFIG_LEGACY_TIOCSTI); static int tiocsti(struct tty_struct *tty, char __user *p) { char ch, mbz = 0; struct tty_ldisc *ld; + if (!tty_legacy_tiocsti) + return -EIO; + if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN)) return -EPERM; if (get_user(ch, p)) @@ -3582,6 +3586,15 @@ void console_sysfs_notify(void) } static struct ctl_table tty_table[] = { + { + .procname = "legacy_tiocsti", + .data = &tty_legacy_tiocsti, + .maxlen = sizeof(tty_legacy_tiocsti), + .mode = 0644, + .proc_handler = proc_dointvec, + .extra1 = SYSCTL_ZERO, + .extra2 = SYSCTL_ONE, + }, { .procname = "ldisc_autoload", .data = &tty_ldisc_autoload,