From patchwork Wed Nov 22 09:42:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13464396 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 9C4E5C61D9D for ; Wed, 22 Nov 2023 09:43:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 355F16B058E; Wed, 22 Nov 2023 04:43:16 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 305216B058F; Wed, 22 Nov 2023 04:43:16 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 180226B0590; Wed, 22 Nov 2023 04:43:16 -0500 (EST) 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 0881F6B058E for ; Wed, 22 Nov 2023 04:43:16 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id DB64B1A011E for ; Wed, 22 Nov 2023 09:43:15 +0000 (UTC) X-FDA: 81485101950.21.D21C290 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf13.hostedemail.com (Postfix) with ESMTP id 84B0020020 for ; Wed, 22 Nov 2023 09:43:12 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=FleuH5So; spf=pass (imf13.hostedemail.com: domain of broonie@kernel.org designates 145.40.73.55 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=1700646193; 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=x/Cd06F2WL0upBscy50EDRzeuCtnVTQqbaxiU8EnLH8=; b=awLyH74Kzp0NqVuW/h5moZOkiXxUUJ9Kfn9iAGoSwLPwh8veDwxF1vukjsu2/mWRt61nOh jW+Nf2wqgxk470fymNye0VfKBBpiIibZMn3v29RzTlTLcxz5vjYFMqpuwdHUX+6nNvxlHe Ny58Ffgw9cVC7t1f2eF0heEd56sh8Ig= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700646193; a=rsa-sha256; cv=none; b=6KfJhAzItK376V+FOsTuNmL4rHFjPDGXDMIfcBG3YIGp+Hqtepsa5Ca02yRBDh9oKNhNk0 9acKEX5B2Au6YtpDaU7Y9e2xAWMlJcBy2l0jS60SgXjwPE/9Pl372/iUz+w6hex5PA9fY+ DrBt2Sd6yLkOdaikjq9pz978hvCcpbY= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=FleuH5So; spf=pass (imf13.hostedemail.com: domain of broonie@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=broonie@kernel.org; dmarc=pass (policy=none) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 06DC9CE1EC2; Wed, 22 Nov 2023 09:43:08 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 57205C4339A; Wed, 22 Nov 2023 09:43:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700646187; bh=m4jPC43T5RlVTTuFtgUTBLQ8flgL5CIMr0+I83wmp9I=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=FleuH5SorImAIbxUWn9gIWuXUzmHWtYH3b9xowe//GV3/74yObmNXfogVNve7sIMj WMccD4ck5U20K2khawd6OtRpGq5YYPh3DYluwm4oekpP+X5W1afPKhEcncJWsyokZT QuxFrZI6uGQHtD0B0H/NFs2v5M7obOC2y4z5qEvGaQcTlaGHI8gFJy8cINt6HOpNBp K3bLtzIeWHw19VB3+FfJUr8zsIUKPktHiO5WM6O/c+xCF/0Utvl1doa8s1wEEXWxf9 xz2MNNlEFg9Dt/5gSfJ0hfI1ZBoWYrrLiIr0R/772tncZSu8t3aGve5BSKnAjX0/Sx mqfMHDktPFbdw== From: Mark Brown Date: Wed, 22 Nov 2023 09:42:12 +0000 Subject: [PATCH v7 02/39] prctl: arch-agnostic prctl for shadow stack MIME-Version: 1.0 Message-Id: <20231122-arm64-gcs-v7-2-201c483bd775@kernel.org> References: <20231122-arm64-gcs-v7-0-201c483bd775@kernel.org> In-Reply-To: <20231122-arm64-gcs-v7-0-201c483bd775@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 , Florian Weimer , Christian Brauner , Thiago Jung Bauermann , 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-0438c X-Developer-Signature: v=1; a=openpgp-sha256; l=4768; i=broonie@kernel.org; h=from:subject:message-id; bh=m4jPC43T5RlVTTuFtgUTBLQ8flgL5CIMr0+I83wmp9I=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlXcz4jLDgXMldTx3cCVXtLYFoKWIKDnTUBfBl4 r79IyY18TGJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZV3M+AAKCRAk1otyXVSH 0GGQB/4zpkMXgM4OIAIz0Opw5FgAenwHSga7889ghXvOsN574GY+2TGEUJbteagx1wJvOFXTp8j PGhBMiC5YswNbSMKRdHSQt7Hqlm0Xas/t3WyU7ijF9MgF1wtwunYkT8KOhMfqGGx1UD3LbbE9Kt SimxpwSP1rEIK5OLL6wyhewP+ds+FXd2X+Nq1lWtHDhm9wlB0EFBP7seUU1/d9QTejFvXQPFORs VKEkOsozSdF0M0xoLnRAm+22dflTFx4JIBq9YUZrUiN3NAS7GfwF1EPVRfADSMvKB2SudfaQxZh 6EegY7jED3NiJ6kFufmWOYnsKiRWrS9MmOlCBYd/02cPAtSs X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Stat-Signature: idab6npyiotzwhsipy714b6wf5rcdseu X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 84B0020020 X-Rspam-User: X-HE-Tag: 1700646192-534091 X-HE-Meta: U2FsdGVkX1/g6SoKZ4iHetM3Q1LiNBTE5ChivIY7+9LrASu1063QTdCMZCFWmIo3jL5XZWvwq7q0JrDQYMqMHsomwx19KFxiPEpxG9bQVNBFeQot9QLDfnXbxUK6IGNgQMTD2QbZH4uzo10CjQq64lZIyA7q2unb6OTBjzw3ea8v+GfHtDOQ81j9XB+C6tGNf1PZ9iDeP47ZMHVsQoJEIv9cEOaPVx/sEcHSMHqyzn6cDipWNQtP93yNU8QQIDid3bbxhClHIqBTFp90x6dMzJ7S2j26ZiUtWmrdbw7owsipERU2Ks25M4d8q49W5FHMH+wTVLDTUwElLCwR2AHHjStqhDVzAnw5McGMXXyVld8EkoAotgA6BRBCaOQGnzLgJuyEajqYXOBDOWh0JP7cf/2uh2t2tYMY3sC+FFyKmIC0FXLmvLEloLNYR8fPs5FOVT/yXJFzh/Sndf9Wm7ddfPwGLmhMy9pcbv+Iymyp+4PXRvt++hkLvbykfDIOpniCq1wT0Er4gDvKNpBgw3vLDjWzO1swyL1EGiHBycQt2uGNZzb61N7LpIkjx1qt+UAjOwJEWTGiZ9L7Eeg7iM9LGpIhxB/m7C7VNk7XK8SaAHAtK1Rjp20YcXKcY4ZDQceLoaL9X//TZXtYITadi0e0iGf+NoSY2URCmMr1BUZemtHuAIFkQ7WNNFqjMdLHdR9YSwzhySpXYNfqYnoP/5lTzasdiAhKxt6ppYTkZlWPOCWP4YkAP+nMm5i9i7EbPZmoZtW/j1VZoLu7oGzXpEWcA7fyfAaie3v4elXjITmyRSRbpGnic3cdPg3dMATZWKuqOG3EwDhTZ3+HylqtZx0raeiV/wwuCOksAQvJsZq3zk+CKY6UZf2PBsP2HwiqrwyDsX2lwpuwV9BWttWFZFXxJWGczq/BSQB264SQACUC6Y88Ib/JNXWXZW338Sk/uwf64RWFny/y+/hm757/BIi U+3UWQWl BXdnOgH4U/W7OjFbONnYRTM/hvFJRxFFF3sRqDRP0CUEgmS5FJiG7dfIR3C/3Z/KVkYG/Q4psMMBg/rtnJvLb6aLvYxAsn8w3yivzaBuOPrwa1a6e4tL4+cH5HjCPLiU35uFz6jV2EiacfCT0xI/VIXcd2G6kK4xFxkWRZdmzfdBZyavBMtSZIb+s3srjtNCCn0shOoE/siZdXQk7mfaeeHjb/WG44maPctsxzErKVIBlTgKNETu8blL/QaRRbONeB6Qe+GQbDnaJ8eqX4PAvw1R8GMyWKDaZxyDf3SmZS6CGmblpYxRP/b4i1p9dhNn2ejMA X-Bogosity: 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. 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 10462f354614..8b28483b4afa 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -4143,4 +4143,8 @@ static inline bool pfn_is_unaccepted_memory(unsigned long pfn) return range_contains_unaccepted_memory(paddr, paddr + PAGE_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 370ed14b1ae0..3c66ed8f46d8 100644 --- a/include/uapi/linux/prctl.h +++ b/include/uapi/linux/prctl.h @@ -306,4 +306,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 e219fcfa112d..96e8a6b5993a 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -2301,6 +2301,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 @@ -2743,6 +2758,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;