From patchwork Sat Oct 15 06:45:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 13007606 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 A8BD9C433FE for ; Sat, 15 Oct 2022 06:45:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229459AbiJOGpY (ORCPT ); Sat, 15 Oct 2022 02:45:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60076 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229537AbiJOGpW (ORCPT ); Sat, 15 Oct 2022 02:45:22 -0400 Received: from mail-pl1-x634.google.com (mail-pl1-x634.google.com [IPv6:2607:f8b0:4864:20::634]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C505832EF9 for ; Fri, 14 Oct 2022 23:45:20 -0700 (PDT) Received: by mail-pl1-x634.google.com with SMTP id 10so6651545pli.0 for ; Fri, 14 Oct 2022 23:45:20 -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=uSLPR2Q4l9Xvh6e71iA4cwlDfkgBH0aV6+SZhDEbGFs=; b=dwxr+rizvsCajPUg1FYGSSAlaOxvHhsLNMiLhL1YhQiocWXTkUyp2RR5ME69D8IwpK GgFoZeP6uOfBwEzXZe6/VQQvns/13YrVySY8yV2K9144ynekR5mP+VhaxxVEDwB9T/BG jWoDHoneUwcvxurOezRn2qKFyAcB3RzTpYq1A= 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=uSLPR2Q4l9Xvh6e71iA4cwlDfkgBH0aV6+SZhDEbGFs=; b=m9+OhuYpKodBqzvsFKzD5udbVNRXLyFrpmU1WXv4FaNTnDvDQgMdiaIjUThdR4iFuJ lbeYCUDB3JRymuEk4KF/FAJppD+dRUEqfzzk/PSQfF4tzzqQofdmc4KSyU8Rtk42luu+ PGRf5PcQHCsE6/fKRTO0bh2D1GRQeX1hgnaeENTAlmVBf0gvcKI3mtm3+TCEbncc42rF 93Ol0PiJHb5lkJsG+8PuPQrLGL7vxwuIrFaZhvZNkq39bdC0FgUAzcRNBeSIP+txMHxW NZ7JDkepqXSeqVbzD2KghkzwsgK85rEYzPHJ6SSLg8CV47zkcN6szQaAw7j5F0zAF8OG CLcw== X-Gm-Message-State: ACrzQf1tjlUT3ozcIDeDyzG/VHR1EPUUj+Rv4WeLDP5/Nw3lfp/WySns JLG8cwOyCdXVgvbIV7qYAaPpXA== X-Google-Smtp-Source: AMsMyM7T2BIK7aEqnP0Sa3vAp9nexgL6yHja3MNMBjS6XTb90jz+bJ+/D75kzBr1GXMRVCXTC/JCKg== X-Received: by 2002:a17:902:ef96:b0:17e:e7f3:31db with SMTP id iz22-20020a170902ef9600b0017ee7f331dbmr1623980plb.127.1665816320247; Fri, 14 Oct 2022 23:45:20 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id r15-20020aa79ecf000000b00535da15a252sm2812330pfq.165.2022.10.14.23.45.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Oct 2022 23:45:19 -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 v2 1/2] tty: Move sysctl setup into "core" tty logic Date: Fri, 14 Oct 2022 23:45:14 -0700 Message-Id: <20221015064517.1554119-1-keescook@chromium.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221015064222.gonna.435-kees@kernel.org> References: <20221015064222.gonna.435-kees@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3545; h=from:subject; bh=oqWbZtlJ6MGMIx/LzLNuza7GPjxoRoljDcfXy+Qysx0=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBjSlb78J/G0sNEqputLzoIxc98iGUESR4bTk2mDnBl 2g3lP7SJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCY0pW+wAKCRCJcvTf3G3AJvpRD/ wNuVfbYOhzxajn6a+L8PoDw0j4FHzHO5FKyd1IsEmu6oovuSJGDx/cw+9fRNF5uCtgTmxH9TQ/Dg2E m37YpYODcnit5gvfp719aBe/hm5wqnybXIUMJTiyNtI/2AuUjL11+3nKuHw/Ox0fjs8B44eCgnFeZF SXFBS6Vh1Jq9ogSDYxA4rGs5Yhs3YvCpRiqxkdYjXF8oYYeIV99SuUH9n/y7rM5QwIhILZLOu/UyHt 2vGmd9SxyDnN0nnkzTzi5UHL5sY4HwkZl+2Rh+Cm0bOczkXarxMGQ/yOILWvglh7DtbiaD89wTkvRd DRssyCyK/K5oL0krUgFsIvpn6h6NAlxb0VS/OiOEF7ZLwUtf0Odn/3qd6qq1HugL6jOFcEA+VcPMaO HbZXuaRp/ptxMKjN6Qnl3x8tj5VaEEc2NHFPFJUZUoW5gB7s4j6USXgR8Fl11ocDMYv2Q91p+afuUG VchRcIVAuvwhFtXW9cXkSaBVcV4GSqTGqB5xXjs1d4dsxRfbCyZUUO4coCJygbtDAqSfUtfjMYMaBq XunqNNriNSl5gxfv+vGfYS6BtBfRfek47qHklGpRA3plBy9wJVm6OQS/rgVac/MbRapzEj5r0H2WJb 0UOpUL+QVDV7tyYzKMDQW1Y+JlNeCJ7nLU35KuofeF8JYSrUHdOWCZjlRQ3Q== 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..b1f22259ed4c 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); +extern 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 06:45:15 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 13007607 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 8F15BC43217 for ; Sat, 15 Oct 2022 06:45:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229537AbiJOGp0 (ORCPT ); Sat, 15 Oct 2022 02:45:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60086 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229619AbiJOGpW (ORCPT ); Sat, 15 Oct 2022 02:45:22 -0400 Received: from mail-pl1-x62c.google.com (mail-pl1-x62c.google.com [IPv6:2607:f8b0:4864:20::62c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 722F233874 for ; Fri, 14 Oct 2022 23:45:21 -0700 (PDT) Received: by mail-pl1-x62c.google.com with SMTP id i6so6563366pli.12 for ; Fri, 14 Oct 2022 23:45:21 -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=pKvoXK/MW48t9MwyWpeLn78cutQ1/AuUErTpoyAp4pw=; b=iRpHiPtMi3dUnwjROUmCYDz3L06j/0iq48p0NvuJkL6l00uZx25ixBf2qej6qcMfr1 CY5gLPdJJY5kWcMVCS9s+f0eWEb3F3sAWht05lihdNmtUyyKL9QKAPCovc2EgznBxbvn SepY6equs/0nk7ruhzNzrOYL/BSUvys3Qgjy8= 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=pKvoXK/MW48t9MwyWpeLn78cutQ1/AuUErTpoyAp4pw=; b=Jgx/KoJfUJEw+KXiku/YAjjQ1SlaUhv8vGxdEKhIsbTtjB7bK90xM//riGv92D/Kxu sPt8vKsetvr/M/hwmtpQs5oEiVd5ZH7XMS/8mA65rjWe2X8vLQApyA5QAhW5kHDkcv7E /uB/HcQeJXb+ufbCLkxXbPU+0lmH6EFJP09JNOTxq+icMm0lxmwDv01y5WUgnYfBKrOZ dppGWhQJED7ZiccppmaMqf5psX7ceDyfIPEn06794fBtZ49H1wqPxy6lwPrJhjctde1l zohwoI+BuNrM1JNAQ35vccdrCfnZW1BRgc23T0FuUN4syTqs4zlJU/gw909GxwV23qBx Z6MA== X-Gm-Message-State: ACrzQf1fJC+NpRDBBCm2Y5I6+guitMW5o7P6nYV/wd/nEdCQASZiDnt8 K34BSex0uAUDQJrFrXvB+md8xKh2jn/HiA== X-Google-Smtp-Source: AMsMyM5gG1n7ZLAMQnXnv8BdATvWenNB8iLpH3NHQvYofvyW1U2/aJiVSjqNGF0eZSp0nD2P81n8GQ== X-Received: by 2002:a17:90a:bb94:b0:209:618f:46ac with SMTP id v20-20020a17090abb9400b00209618f46acmr21913383pjr.240.1665816320971; Fri, 14 Oct 2022 23:45:20 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id ij19-20020a170902ab5300b0017f7628cbddsm2682023plb.30.2022.10.14.23.45.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Oct 2022 23:45:19 -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 v2 2/2] tty: Allow TIOCSTI to be disabled Date: Fri, 14 Oct 2022 23:45:15 -0700 Message-Id: <20221015064517.1554119-2-keescook@chromium.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221015064222.gonna.435-kees@kernel.org> References: <20221015064222.gonna.435-kees@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3580; h=from:subject; bh=kjqVbumXKmpYQu5g+0jngSz3qjLAeN7VzRJkUWWFCAo=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBjSlb7Te+AzPWEJwitcFI9OGMy97KUI9l2gFWD3tbS B0aN6f2JAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCY0pW+wAKCRCJcvTf3G3AJqm+D/ kBsaao5QXORYsXZ6tMoSk1qbF2cqCrh4sImxv9FzAPjhnD1er1Ff438V7Q0KYFh/q83pWtJRotFGBT QU8ZtVKPrtdOlQ+pn7+NbyXorwitwyyC9BCNdrU5ucbAo8xVCWgevYXYTpLrLG9nTOgYphzO2peCIS d7GU50JzErxaXhemK9uQdK2QS8H0XfcUV3rLbcyMD9JSMvI9fIv32/Tn8aU4bXR3GKbFAtByiEll6g b0w59cyuup1kESLx4UNX9EFvJQxap3zx1YZbWIu2YUFUC5wGFSeSmYBdGrrMb7sCeCNFueOtlQncBG MnkneyO334jImK70V/H4Ct2t4iEYSfik+z0HBC1qZCqwyuIKZsP8+e/K6E+e62rgzLL0bjuHlyBPhU cgdRAkIqLCZvFsS2uLklsE+28n+m9xBIDMEJ1cvDAHUvgsT/4k9l69yhubmwERLTUSuA6sVP0T7gXV m1rBbwxEG98C4/UYRH11ToRjUS+EwWYpakcqeR3auZgTfNxekmf+AmNH8QILMrNKNp72dFFywBXcVA J8HCKgLlguJLC/nQfI7CEv181xlDUJlQFiBRT1DPZZ62zEEFSAT2b/4qKJYd2RaWdcpiSuXz4Snnel kVmm2rHyemTv0O1Xl3Bu44zVQWAu2ENBcfOBPWpr3BX6IHT3hht7Zj6o3Q1Q== 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..fa36dac7559e 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 */ +static 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,