From patchwork Sat Feb 3 12:25:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13544060 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 9209BC4828D for ; Sat, 3 Feb 2024 12:27:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 238B56B0072; Sat, 3 Feb 2024 07:27:38 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1C17D6B0074; Sat, 3 Feb 2024 07:27:38 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 03B996B0075; Sat, 3 Feb 2024 07:27:37 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id E34506B0072 for ; Sat, 3 Feb 2024 07:27:37 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 916EE40695 for ; Sat, 3 Feb 2024 12:27:37 +0000 (UTC) X-FDA: 81750418554.12.0220D69 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf15.hostedemail.com (Postfix) with ESMTP id 7351CA0007 for ; Sat, 3 Feb 2024 12:27:33 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=DPmH+nfN; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf15.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=1706963255; 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=20DYYnKVZbxaBj1AtacImvdZu0oj7mYGOXLaFbxTP4Q=; b=xdFMSrzGk3cd8rWvXaFOO4bwp+sfamGL5wpTkno3YnnebscVLmNPUQ1x7jwmK4TY2fOg2g joM0BsLsK/9zpdW//tKVeZelndhtQkZ7jDcuHYhEB2iKBUOEZ7frYQovY5dhxuRJ5yMw8X FJDXZR3Hw8sXUpTOCW7rrcV4Nzd8EuI= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=DPmH+nfN; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf15.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=1706963255; a=rsa-sha256; cv=none; b=QCOSC0dBiZ0KQiOZhIR+Of/G15Unwj/8vhi5m9Ro5PnFkX+KJUMeig4dTMGfdSXdhOn3oe E2Tv8gGSZTAlt4iWwz7/McoxT+mD0bMnB6UTzHpj7XVKTlD0OowGgQLnO3X5Zlf/GZZSnP X1HsyqI9yemb7lJ69UdGID39DQYu+bU= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id E1CCFCE1EFB; Sat, 3 Feb 2024 12:27:29 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 35388C43609; Sat, 3 Feb 2024 12:27:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1706963248; bh=3IJ5SeAASdHtIIStcvs359UoBpZ4hMPDZ8zRhgL+KJQ=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=DPmH+nfNwutz8I2cy45COQPzbwAEfPjI6ffHVfjCNllrgl6vp33v6POzMgRpiUz1H R6TyvxMdEhSqVGHpIeiEr0p+nf/z4YAOXLc817SR8gY0hxjRWonN4ZYWB1mELfcD+u J7wYEJW9thoiDrQF7mUZhwtuBsqwnml271Qg555AgCbsSA+Nx3F6RaqEpoowp7DVyT JEBlCPvrwO3xFaqY3QrwHh/DKN1ZVngK60F7Ml/DEtqx6WVIyWbFL2sNR7OFKWGnAX rthP6rYhkhBxKkG8iwQoOZdxqrQDiD6u4jT4CVAYdvIIt6mGqI3GifpPZr4J/thkmP IBUzzRC/4Qz0Q== From: Mark Brown Date: Sat, 03 Feb 2024 12:25:28 +0000 Subject: [PATCH v8 02/38] prctl: arch-agnostic prctl for shadow stack MIME-Version: 1.0 Message-Id: <20240203-arm64-gcs-v8-2-c9fec77673ef@kernel.org> References: <20240203-arm64-gcs-v8-0-c9fec77673ef@kernel.org> In-Reply-To: <20240203-arm64-gcs-v8-0-c9fec77673ef@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-a684c X-Developer-Signature: v=1; a=openpgp-sha256; l=4768; i=broonie@kernel.org; h=from:subject:message-id; bh=3IJ5SeAASdHtIIStcvs359UoBpZ4hMPDZ8zRhgL+KJQ=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlvjDS1VpII3RNTbS21ZqAUQaox+Eucg15vDi3Rm0d /5PJGGWJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZb4w0gAKCRAk1otyXVSH0HTNB/ 9zYcH0WIf7htiNJ645c2X8FzMF1bRnXmpIa3HQrk1F0SgbCOhNYcpXWoBqfZBZpYKq+oSSh4xxf9L8 HBVGI/kvF8TwcIyTGk4Qv5eb07yqP+ld18BNiEn5N6adaeLXpm0CQfVz9kiSZJeuoM0uTM6ijzD0H/ ibNLhTdz2oJ8tP5z1WmYMA4uTUBS+Ox3HKYsVRRGjlWNGcpyHCWagK7vzKAwsdUOLmfePgoE3OLKAv IIb8m6ZlcL7kGrSIDv+Z64fInWKFktfQjQrcnKkyt+MFXDI+X/4ArV4mww497KBHQuEsk0c7q1cALZ ISEhWbo8N3Sl5Sj2gdItzpIbuFjN2Q X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Rspamd-Queue-Id: 7351CA0007 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: hiieroxqfg3sghkpogw3aocpaow3osfc X-HE-Tag: 1706963253-923081 X-HE-Meta: U2FsdGVkX1+RPyGVQ2ywbQUiwE7DdZEDePhC6iU5P34TYb2S8oZFiMWJtSZKiPOYk/2O7eP3bwUuuU1ooCVw+TjCnziiAEMRhYQyOaWhlaFhdTP/jcFq2kS1DjNFcPlxgOvTt20K4Wam3pqkREgQofqELcMZ0i6nJM6f78GNeZX0fDdO/AOhC6U7qKBqjXhT/QMZ18N2tbrOWELGnr3TxIuTvL8WAHwgcNoqBwbcCQdnilCwhgISs61NQiIkzib0tJ+5lfLo1Uflwtj1j1b/+tPubSjr2I6yhd+/pukZEKEbw1LFPPR0yAL6+4apuEDhrd6qMJ4bDxFlgy7TE0IAWgBh4LeR2gqbP9UhFPFY/QVN0NW60lL0dgj4rEdTUbXz8/n7sRCou0aU6Ut/cS+MKShW1KKHAwAdKCWmtmwrofYP9RLWL8ZLzXzuyGGQr2sCqzPH/qhQCDRVHArhIMKQIorAi9+sh+SI3944UrukaknV4hwuLO/kliX3bkm3i4/TF89Pfh+9gkMGalPn6uS/4a1sqDLipbVPpCNQzCIbvkL33QaYnZOu+VUI5hTaS45HVWlN0soMuk69BZyXYNMKy46Zy8y0UCyU5VhbjZ2DsMWepQkOjpCXTPEI4J9BnZ0ch75PkmeJq3VqOqEQVycz7qasvh87qfBMgpDy6VYqZWFU28S2kcHJzzyUE9APq2wfEKhoO4v3g1k5bzzq/N7uFnK4eimDydkUHhLQeOVaZ3Pc3vXIn8X58kZq42SDiveN+9F8YBsFRmGHGoqGu/9kJVj3WgaJPkFwq5wLA0igE4z6FiAPuehM0AyHjIzbuQoO/wvK7sSTgyk1W9qcSc++wWTAg7LnYQNhEqZKgIEvq+PDwzMk4Wttw/rpDREVaja7kveyhf83vMouXJbeya2N5OczsrITpC4m/vuEdXqJZ9RYSOwUTVPb3L6hfs2Ut2pCiQRZPdl9YGvARtXp+oE vgRuS8+T QOWWs+8xAB4Ep5iAy/IcufCksg7yL1hqvKSFSfrE9VMrNZfJuCldIxQX+U6LLyea7Ya2L1pwpOrE9AyF0wsyvhLh7S9JTeQg9fnyPCeFNSbgtZem4oiFghWyHgjkFav9yCe2L4Pk0oa/TchsuleIcvpO7WkBOGxIQXbw5mrq67OO8k+a4+Cs7H6ePjeZutdoM1L1I+8CPqTtMI/IqSmmP+d/0JwIThnEgLyMwhVJzYbtRLaSAlEeAra6GtGoORAKEpF4w50uvYLy2WsAyXr/ZSbo9G2KZHg3V5j7M+qBlyneoY8D4osdoWSEjPwL3OZ4us58r 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 c0a782eda803..0b1139c5df60 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -4182,4 +4182,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;