From patchwork Wed Aug 28 23:27:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13782165 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 09580C7114C for ; Wed, 28 Aug 2024 23:29:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9AB266B0096; Wed, 28 Aug 2024 19:29:00 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 95B3D6B0098; Wed, 28 Aug 2024 19:29:00 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 823506B0099; Wed, 28 Aug 2024 19:29:00 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 655236B0096 for ; Wed, 28 Aug 2024 19:29:00 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 153CE1601BC for ; Wed, 28 Aug 2024 23:29:00 +0000 (UTC) X-FDA: 82503246840.18.43434AD Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf10.hostedemail.com (Postfix) with ESMTP id AD3CDC0014 for ; Wed, 28 Aug 2024 23:28:57 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=CyUah56F; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf10.hostedemail.com: domain of broonie@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=broonie@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724887718; a=rsa-sha256; cv=none; b=VfTT8oOtqhKCWhFbmiEzdz26FFkZjDRKA8hnn5PxOjR9MngD+HlfBJqF/dv6i8TzZDTkiW 216Mm95O5+uAXAkG9yaXRpXVMLWcsiESEcT2mBlOlTVdfRUVrDUrgqcAvv9llV6DVpg2aV 4idyA0ynk40BqAG6Szk0ctaRskWhBO8= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=CyUah56F; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf10.hostedemail.com: domain of broonie@kernel.org designates 145.40.73.55 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=1724887718; 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=EJN8RNnYmCJzR88SVnfnXKM0lDyz44SjLAMsegCd2Y8=; b=ixLN6G3vJqB0ODoT0QLejpZ/7NxZ70DKd1nIUbHJEnKfZKYXa6YLjWnWlwwGDBZd3Ewth9 xkihyZRbE2VwxbRiZKWH171NQ4kurng0NWt0DXmrImARh6raUwp3kNOwB53bKjIh5QEtJx X4SmFHjv71IcfMzgUwkFawRS1PFqgoc= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 962B7CE1708; Wed, 28 Aug 2024 23:28:54 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0AE6FC4CEC7; Wed, 28 Aug 2024 23:28:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1724887733; bh=RTcnwKCnbai8FTs65e4TvcOIdbpBUk2hXTCc6OzveBw=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=CyUah56F/ucjbAcFwoG9D6pKWXQ+1tmJEEDcJN+9ryqPoOgJTPSLtYBid3RB5nVhA YgRXyYYGH4YY5HdCar6JjmO9SGQzp4qvWwOdniQ4qlLpE5v0TOpQ9pwEBAUb+Tt4W+ NQb74WNVQ/WH0mXhUZTsi4ejE7R3HgCFXk55WwGPXzN8QQeeamJwNC++kVLz+hlD0T 2SDIKVh1wTgL4kdUPjkJyUOIyzpbkY5DQQtPLRxKLevTbdRgPY5iemORfnAiXdMHpX MsWpmJr+RFu34tC4Mlu6XyEez3UKn42y53mWLw3MXV3rXjGfRkXEIkrqPO2o1Q1raS S0RC4G3IKnGUA== From: Mark Brown Date: Thu, 29 Aug 2024 00:27:19 +0100 Subject: [PATCH v12 03/39] prctl: arch-agnostic prctl for shadow stack MIME-Version: 1.0 Message-Id: <20240829-arm64-gcs-v12-3-42fec947436a@kernel.org> References: <20240829-arm64-gcs-v12-0-42fec947436a@kernel.org> In-Reply-To: <20240829-arm64-gcs-v12-0-42fec947436a@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 , Shuah Khan , "Rick P. Edgecombe" , Deepak Gupta , Ard Biesheuvel , Szabolcs Nagy , Kees Cook Cc: "H.J. Lu" , Paul Walmsley , Palmer Dabbelt , Albert Ou , Florian Weimer , Christian Brauner , Thiago Jung Bauermann , Ross Burton , Yury Khrustalev , Wilco Dijkstra , 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.15-dev-37811 X-Developer-Signature: v=1; a=openpgp-sha256; l=4949; i=broonie@kernel.org; h=from:subject:message-id; bh=RTcnwKCnbai8FTs65e4TvcOIdbpBUk2hXTCc6OzveBw=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBmz7J60KI1NAx9s5uebbxOtg5o2u/+i3ZAA5EAZi4W ygltim2JATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZs+yegAKCRAk1otyXVSH0Ej7B/ 4i7V5o8NwcAlzF0dC+dC7r9ly6FsY2jaJX4HBmux5vDqMKE2ssgjL7PW/vLOoPBS6mpzseOWpwEsuQ efZzoPx50er7uqNMG9uH8TNmUHnq2qCpg1JGGzo/wDFM1rZqH43CnJQ5HxHnbQTAwtbo6PJUqJ48ib n9sXJezhzy/kcM9bSLndvUruiEaLAArgR3BggKgdHazVd5/y6+yeRzzxHM7z7MvhfbUFNo4YmqUuuj amvYgdk7+YXJCuHhIrgqI9qoC03307nJQNKADYDPz6WoNiKrU3PDbGav7T1tGnsCkKrI0jdd3Jftbk xjTF6ie+KPWrJmU4xk9fZ7BVYxKeW3 X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Rspam-User: X-Rspamd-Queue-Id: AD3CDC0014 X-Rspamd-Server: rspam01 X-Stat-Signature: y6qc1rwmccectqx9qg8cin3hxmgdiuxt X-HE-Tag: 1724887737-554189 X-HE-Meta: U2FsdGVkX1+ZjV59kF3Bu3tMcMDNfGM4UcdwjAn/5LQU1VvoOUG29D3/fH4DkZeWytZGJX7V006EOpxujxkR/dPEnpl5yLUEAU2s4JShDA+MUBb0X7zQbzkWKCo9ncVBQ1lOTyy5Q7V623iT+84VyW5fYWXR3f6nPriTF83cHQujvrJPjTLrMB1R09nD1YzqaLsv2NXIiGIYGYYROb4w/gJ4lnyGMKe1xivc3uqKylrLCYxFaPei8JYVkz2sz4HHMPHR/VrxPuuMyz9nir83kSBnQynatr2M/e43br19HRsKnaZe45goqPiXMZi05HK8VEiWKGp1CB4RfOggBoVrCwrsgIl5bwlrQpxFZhD/ON15U8YZMwnJh/7tAaG9A4yLVG+J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: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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. Reviewed-by: Thiago Jung Bauermann Reviewed-by: Catalin Marinas Signed-off-by: Mark Brown Acked-by: Yury Khrustalev --- 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 3357625c1db3..96faf26b6083 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -4201,4 +4201,8 @@ void vma_pgtable_walk_end(struct vm_area_struct *vma); int reserve_mem_find_by_name(const char *name, phys_addr_t *start, phys_addr_t *size); +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 35791791a879..557a3d2ac1d4 100644 --- a/include/uapi/linux/prctl.h +++ b/include/uapi/linux/prctl.h @@ -328,4 +328,26 @@ struct prctl_mm_map { # define PR_PPC_DEXCR_CTRL_CLEAR_ONEXEC 0x10 /* Clear the aspect on exec */ # define PR_PPC_DEXCR_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 74 + +/* + * 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 75 +# 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 76 + #endif /* _LINUX_PRCTL_H */ diff --git a/kernel/sys.c b/kernel/sys.c index 3a2df1bd9f64..7e0c10e867cf 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -2324,6 +2324,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 @@ -2782,6 +2797,21 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, case PR_RISCV_SET_ICACHE_FLUSH_CTX: error = RISCV_SET_ICACHE_FLUSH_CTX(arg2, arg3); 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;