From patchwork Tue Aug 22 13:56:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13360852 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id C85DAEE49A3 for ; Tue, 22 Aug 2023 14:03:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2F208940026; Tue, 22 Aug 2023 10:03:20 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2A3C4900011; Tue, 22 Aug 2023 10:03:20 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 11DBB940026; Tue, 22 Aug 2023 10:03:20 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id F3236900011 for ; Tue, 22 Aug 2023 10:03:19 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id C74F5B1C05 for ; Tue, 22 Aug 2023 14:03:19 +0000 (UTC) X-FDA: 81151907718.18.E28BD4D Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf27.hostedemail.com (Postfix) with ESMTP id C84AB40026 for ; Tue, 22 Aug 2023 14:03:16 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=KEoR90I8; spf=pass (imf27.hostedemail.com: domain of broonie@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=broonie@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1692712996; a=rsa-sha256; cv=none; b=uMR929ckfAROjPJ4wGJgBo1Yx5b8u1EyZ9GnVH/Zl26y9df5JDj4LyBXrf7sLYfw7Lw34G Ge040sZotZFc4FUPwjQjy05QPlG259MXm82StiPGOZKYJ9OXqBnaJ6CDKQkokgWvCW9anC 000v/zRhmS0rjyr8HH3Fns3Jb9kClic= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=KEoR90I8; spf=pass (imf27.hostedemail.com: domain of broonie@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=broonie@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1692712996; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Rr2GRmUkRH4qcj8DwtxgJoc5E8ETtGtlZ90s5DNZBXk=; b=iH/vuREhIt46eegy2z7jRCtSqIVTvfGMvlYvNQuy8rTtDYfe4QQPxBzHVzaxQyiHCrPmCK 9tLPnt+U7q7A22L8xm4bif/ZSofcqUNp+1oltCzy0/BA6FjoufPEdvIEkhNzwm34cEwR+P BJ9SFsoGjL9nptSOrNAuoFLWYOWQHeo= Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 033C163A0E; Tue, 22 Aug 2023 14:03:16 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 16940C433C8; Tue, 22 Aug 2023 14:03:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1692712995; bh=AEKjV/BA0T20XbbVS98znUxvUes3P+lFxLNl3dughq0=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=KEoR90I8az6NS+yxqhHblzjRjsUmatkMHuttsjuUZXFLKHRRZlM2BycWQ6wNoViXM VMeDZvQqWeSmHWE0l5G0lq61RCTtTjWLvTmNAXZbznyaXWPrWdXDb4TNjbOy4ObLKy xnJtq3yfzqXhAgqE+WUg8InKCG5bnEIis2wLvMs/X62WQh1UPdXZSyAO+pqlSAZPnD kH4v7ba2uRvGQXt4NkXWpwo6s2LEqBONk29Y/wJ2wcDMFTDyd6f3MYqyq5daOIntP6 OSVfGwsEW6n5t5gZ4M7D5hUu3zRcLX6iSCet0rffyXw3YsR8qKN3Wa+GucmI2BAtVh a3zGn5gp3Z6Kw== From: Mark Brown Date: Tue, 22 Aug 2023 14:56:35 +0100 Subject: [PATCH v5 02/37] prctl: arch-agnostic prctl for shadow stack MIME-Version: 1.0 Message-Id: <20230822-arm64-gcs-v5-2-9ef181dd6324@kernel.org> References: <20230822-arm64-gcs-v5-0-9ef181dd6324@kernel.org> In-Reply-To: <20230822-arm64-gcs-v5-0-9ef181dd6324@kernel.org> To: Catalin Marinas , Will Deacon , Jonathan Corbet , Andrew Morton , Marc Zyngier , Oliver Upton , James Morse , Suzuki K Poulose , Arnd Bergmann , Oleg Nesterov , Eric Biederman , Kees Cook , Shuah Khan , "Rick P. Edgecombe" , Deepak Gupta , Ard Biesheuvel , Szabolcs Nagy Cc: "H.J. Lu" , Paul Walmsley , Palmer Dabbelt , Albert Ou , linux-arm-kernel@lists.infradead.org, linux-doc@vger.kernel.org, kvmarm@lists.linux.dev, linux-fsdevel@vger.kernel.org, linux-arch@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, Mark Brown X-Mailer: b4 0.13-dev-034f2 X-Developer-Signature: v=1; a=openpgp-sha256; l=4712; i=broonie@kernel.org; h=from:subject:message-id; bh=AEKjV/BA0T20XbbVS98znUxvUes3P+lFxLNl3dughq0=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBk5L/x2cXgHFLNnUtT7AI+Jl649aBK5eCftRb+5BtR mT/WTCmJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZOS/8QAKCRAk1otyXVSH0PD6B/ 4wvMvt7dBKa89hiz6f3WEaMlkcy0kjmLQ0B/k0i7MExuJJYSQ89gSZPJltwyznTWd4suVOEXRJFiNp xKSMEphfeBbtpvXkvCJI9rhtwGx6GaC18K/bPSaFUWWjaCkFpOJCqn7EbCox7MzWN3XOLL5RYE86Ly Z9c5ZuiVJx9+2tffiDBFk8HoowhWXMRVS3LDP43k8DrLw98QXIuliyc95y2880kBm+Bga1GFzDNQGh 7+7VTatgg2qlmrr5Ffi8YL2ndXpaYkYRu3XEuCBVTwDLcYdqze7l+wx3eaFoBe5oJVqxzKw0PW0XQj XUEyOkNRHwkKn0Dwde4TRWQQj8OOWv X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: C84AB40026 X-Stat-Signature: 3rrkk445rjcbr4egya4h11bsyedjyy8h X-Rspam-User: X-HE-Tag: 1692712996-576750 X-HE-Meta: U2FsdGVkX1/gE7l+zi1a46edBCPoYcqfbSwocxjmiY42gxlCykcnOKxcxqU+B+K0bF6Be1NkwTNlj8yNNnuotHUw37bgTv7PqkCYh64y90XHWFd1rJyAN0c334qK56TBOqny214ALa68auaYL3EWONg9flb+7Hlqb4cqPed8+z4alFXzuXAiwdeFcTwrqMI7Y5k0cH1YzGWc/WAEQD4MM0CCIhttUpLUKBLsyQNRN4OcCoN+gkbwjtFJzTNAV7TcU8Cd3Ls1H9v6IjCJe5dI/XQ3JiWBrh8OxDAvZW0CylBW5i+7uQTiLiVACVVV0WCNEYZ4nF/BHdkGJIzpHX8TiFuZEMJXwRAj+R3FqUC+3SBm57Hpibe8r7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ity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Three architectures (x86, aarch64, riscv) have announced support for shadow stacks with fairly similar functionality. While x86 is using arch_prctl() to control the functionality neither arm64 nor riscv uses that interface so this patch adds arch-agnostic prctl() support to get and set status of shadow stacks and lock the current configuation to prevent further changes, with support for turning on and off individual subfeatures so applications can limit their exposure to features that they do not need. The features are: - PR_SHADOW_STACK_ENABLE: Tracking and enforcement of shadow stacks, including allocation of a shadow stack if one is not already allocated. - PR_SHADOW_STACK_WRITE: Writes to specific addresses in the shadow stack. - PR_SHADOW_STACK_PUSH: Push additional values onto the shadow stack. These features are expected to be inherited by new threads and cleared on exec(), unknown features should be rejected for enable but accepted for locking (in order to allow for future proofing). This is based on a patch originally written by Deepak Gupta but modified fairly heavily, support for indirect landing pads is removed, additional modes added and the locking interface reworked. The set status prctl() is also reworked to just set flags, if setting/reading the shadow stack pointer is required this could be a separate prctl. Signed-off-by: Mark Brown --- include/linux/mm.h | 4 ++++ include/uapi/linux/prctl.h | 22 ++++++++++++++++++++++ kernel/sys.c | 30 ++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+) diff --git a/include/linux/mm.h b/include/linux/mm.h index 0ead9d8013e8..43fe625b85aa 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -3964,4 +3964,8 @@ static inline void accept_memory(phys_addr_t start, phys_addr_t end) #endif +int arch_get_shadow_stack_status(struct task_struct *t, unsigned long __user *status); +int arch_set_shadow_stack_status(struct task_struct *t, unsigned long status); +int arch_lock_shadow_stack_status(struct task_struct *t, unsigned long status); + #endif /* _LINUX_MM_H */ diff --git a/include/uapi/linux/prctl.h b/include/uapi/linux/prctl.h index 3c36aeade991..0de3d6ee18e0 100644 --- a/include/uapi/linux/prctl.h +++ b/include/uapi/linux/prctl.h @@ -305,4 +305,26 @@ struct prctl_mm_map { # define PR_RISCV_V_VSTATE_CTRL_NEXT_MASK 0xc # define PR_RISCV_V_VSTATE_CTRL_MASK 0x1f +/* + * Get the current shadow stack configuration for the current thread, + * this will be the value configured via PR_SET_SHADOW_STACK_STATUS. + */ +#define PR_GET_SHADOW_STACK_STATUS 71 + +/* + * Set the current shadow stack configuration. Enabling the shadow + * stack will cause a shadow stack to be allocated for the thread. + */ +#define PR_SET_SHADOW_STACK_STATUS 72 +# define PR_SHADOW_STACK_ENABLE (1UL << 0) +# define PR_SHADOW_STACK_WRITE (1UL << 1) +# define PR_SHADOW_STACK_PUSH (1UL << 2) + +/* + * Prevent further changes to the specified shadow stack + * configuration. All bits may be locked via this call, including + * undefined bits. + */ +#define PR_LOCK_SHADOW_STACK_STATUS 73 + #endif /* _LINUX_PRCTL_H */ diff --git a/kernel/sys.c b/kernel/sys.c index 2410e3999ebe..b26423a614a9 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -2302,6 +2302,21 @@ int __weak arch_prctl_spec_ctrl_set(struct task_struct *t, unsigned long which, return -EINVAL; } +int __weak arch_get_shadow_stack_status(struct task_struct *t, unsigned long __user *status) +{ + return -EINVAL; +} + +int __weak arch_set_shadow_stack_status(struct task_struct *t, unsigned long status) +{ + return -EINVAL; +} + +int __weak arch_lock_shadow_stack_status(struct task_struct *t, unsigned long status) +{ + return -EINVAL; +} + #define PR_IO_FLUSHER (PF_MEMALLOC_NOIO | PF_LOCAL_THROTTLE) #ifdef CONFIG_ANON_VMA_NAME @@ -2720,6 +2735,21 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, case PR_RISCV_V_GET_CONTROL: error = RISCV_V_GET_CONTROL(); break; + case PR_GET_SHADOW_STACK_STATUS: + if (arg3 || arg4 || arg5) + return -EINVAL; + error = arch_get_shadow_stack_status(me, (unsigned long __user *) arg2); + break; + case PR_SET_SHADOW_STACK_STATUS: + if (arg3 || arg4 || arg5) + return -EINVAL; + error = arch_set_shadow_stack_status(me, arg2); + break; + case PR_LOCK_SHADOW_STACK_STATUS: + if (arg3 || arg4 || arg5) + return -EINVAL; + error = arch_lock_shadow_stack_status(me, arg2); + break; default: error = -EINVAL; break;