From patchwork Mon Aug 7 22:00:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13345066 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 61762C04FDF for ; Mon, 7 Aug 2023 22:01:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B64146B0074; Mon, 7 Aug 2023 18:01:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AEEC98D0003; Mon, 7 Aug 2023 18:01:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9401D8D0001; Mon, 7 Aug 2023 18:01:41 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 7FB076B0074 for ; Mon, 7 Aug 2023 18:01:41 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 307A71207CA for ; Mon, 7 Aug 2023 22:01:41 +0000 (UTC) X-FDA: 81098681202.03.19DA3E5 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf06.hostedemail.com (Postfix) with ESMTP id 338D8180004 for ; Mon, 7 Aug 2023 22:01:38 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=SyRCu2BW; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf06.hostedemail.com: domain of broonie@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=broonie@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1691445699; a=rsa-sha256; cv=none; b=dGfCX81m+ufFvqV+/jqEAHhhhykOJ4/5ZNV3HlrjZI0E905fhX/T15H9QJJ9VVYkNoyLeW /uwps2KkS0XGAkTLhcvzL0lwMQgfNiTFBJkc7oAjEEf3jSRUlsWt25ncIPx6ap62WQtjIg w5I7suDkmCTVhJeXSqd5EgsxwknBPec= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=SyRCu2BW; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf06.hostedemail.com: domain of broonie@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=broonie@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1691445699; 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=Fr3XgA5GF9e8nwXj1QlUMZEria26kzHM2p9KH8H6cJOzIUNAwl1gwFZtQL2OiKf/5cAH8i FIRPuoCDu1TqnL86Nzzu804eSyo0fThjtmFA7JHihu8TWJCiJ7WLXqdBhwYdDvJW3Rm3Kg pYO34LfvhLGmzmVd1Yz3wGPZEAfPUxw= 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 5265962291; Mon, 7 Aug 2023 22:01:38 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 08B87C433D9; Mon, 7 Aug 2023 22:01:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1691445697; bh=AEKjV/BA0T20XbbVS98znUxvUes3P+lFxLNl3dughq0=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=SyRCu2BW6qjr/TKOhDZHI+CIXFJw09DWT9LYLuVY4odwt4KVXZuUtQtcKge0vH1yc NhIjy5UQuv5asrLwQ2E/bayheHLHhu3nQKPeclFm8HdoHYz15GauC5D5Mt6+HcufwO w8Ws+NJ0fdX3DWYEIYqZYcB8EFWgy2L6Pr2mKF8rMpMGF0IKqRz/wzxIrFv3GuR60s dWTTZUgdtgBcylPnAyf0BtVUS19a2vXMm1PhQMt2Q62WTAghgQziRBCzs0HFSlj66J oKFDsNKM58C0NGeGk5NYyAvS1MamrjDewe1Pe07wb1g4kcOtlojlyy4wUOp88xfwu3 D1BIppkgJAr7Q== From: Mark Brown Date: Mon, 07 Aug 2023 23:00:06 +0100 Subject: [PATCH v4 01/36] prctl: arch-agnostic prctl for shadow stack MIME-Version: 1.0 Message-Id: <20230807-arm64-gcs-v4-1-68cfa37f9069@kernel.org> References: <20230807-arm64-gcs-v4-0-68cfa37f9069@kernel.org> In-Reply-To: <20230807-arm64-gcs-v4-0-68cfa37f9069@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/pANAwAKASTWi3JdVIfQAcsmYgBk0WmYCJ09bM4Wf7hxqY5FoHrf7ZbPLgNbywvjDg5K 9v3eFBGJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZNFpmAAKCRAk1otyXVSH0H+1B/ 49rYObgdF8pJ25JkrJQQ9VsWeEnauBaz6z8h/tB4H9GjvBgotjUIUu4IjJLjFteX3yMQwWBijDRuSS 4w+VBd6Aii5fQkBfl2z2D8WxZWyzftiEzzOH64uYZg67Ne3II6aINa8Cd2OVFoUoWb3N0AFOrX8IGA 9jLcxgGOHpHV1C6TC29DoYFyuQpMzniquiwkHi8E7IggvpevruvMayzC5xpReY6C0ux7gZKN6flsQb 78zdOlQWTfdEWt3K0n3hLcxHj0sUr1/nvFuNerm44LHSB0D/xDy4IeYrdtQkoZRrb8KHQwMv5tGmVt kzhrE84nhgvv+3RcGtMHYaj5HUXvgL X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 338D8180004 X-Stat-Signature: 4y7yzbf79burgwydn5buzqtathngu95y X-HE-Tag: 1691445698-143346 X-HE-Meta: U2FsdGVkX19GqoG1O+5gZBudFEM8C+R750y4b7hcwvvKrxUX20LLja3m/uomnFWbHFab8Hd6vHchU2M32iCoObaByAfyNl4elx+HGGvnYXzkL8INu3zSRC7VynknrDcaLYQhiBVPD3pW8NAUWytgZaqgl0WGphuZXwEX/lOF4VVJoyxKKs8El8qvfcuOu4cnoxcZque/oubOd1LsyVpj+hZS7zCeEJzrbF89Alw/V7vnTZfXe5pr/fpbNdRbcDM6LtkH5R93g0dGFZ3qFvFnEfe57z3JdnHv/Ycat+ZQwVStsX4VTpzZ58MEC7rIBWpE+SuqKB710sCl2VWY4ziCM4KSpVLvZEn9ezTGt1H+6Z27DgInOu6YL7tqW7oNP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amicity=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;