From patchwork Thu Aug 1 12:06:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13750381 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 4D9F8C3DA4A for ; Thu, 1 Aug 2024 12:57:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CEC0C6B0099; Thu, 1 Aug 2024 08:57:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C25506B009A; Thu, 1 Aug 2024 08:57:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AC63A6B009B; Thu, 1 Aug 2024 08:57:36 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 8E3646B0099 for ; Thu, 1 Aug 2024 08:57:36 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 1CA271C40BC for ; Thu, 1 Aug 2024 12:57:36 +0000 (UTC) X-FDA: 82403678112.04.4DC0682 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf08.hostedemail.com (Postfix) with ESMTP id 4332A160025 for ; Thu, 1 Aug 2024 12:57:34 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=gn76kpXZ; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf08.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=1722517012; a=rsa-sha256; cv=none; b=cTpelp/w1fWTyBmdjcggjW8wO5jDcKRUcHl0AZlGwe/CclU77s73D/O9niDxpOx3DbApUC +pKv9ugWhI0QpXmIUnKlOzCwDo/eL3TUGXzVcB54+yLE5ndTA7080vVJ46OSanMO5vruth 3NRNbq4GwxK8Nt/A03F7pKoWwBvk5iY= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=gn76kpXZ; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf08.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=1722517012; 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=xE4r1OdMOuQOtvYbXqgXQT6HEL6+DhdOhpFAidzB71o=; b=8Z1X2svs2GkFcqXlNPRB6LAjueZvFhW6orrFtoeMQ0beNLjvW/lAWGcGR47BqhQmIkOLS8 ol0Sbc5oYNQTfI5VyDfNiFtnVrdCEvEUEN0PFjEwTo2ihV3PBIEWqhb1GlaUCDAb7HL4XA Tp/q90gdRctLxjrgHN2zpDWotpEkUEk= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 7EB026288A; Thu, 1 Aug 2024 12:57:33 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CA5D0C4AF0A; Thu, 1 Aug 2024 12:57:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1722517053; bh=OvR6N9yV4HsQXOrwTY0B56Y1UKQlPL/MpYNNiWGIOU8=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=gn76kpXZGuovEJsswE8j2lOsgBW4PR578UDiThd4s4I4YoKJvfCqZ1DbymCf8xbuS X1VOWKfLhwcVY4x/7o6COOGPpHHtRj30f6+8qQT18mHHKmzfTHeh2Xn8DQWch4wQK1 cxBX1MypgoCqxGif2mx/sPN2jyddzQ4Dak828B+c0NQugWavtwya2f4qLq4NQzikc2 ufy8oYQxFvr/igSCWz/RQmXY+eMxi1Oqas371CoVkslDEjBkApbMoyAxOhB9FuJyHV zfRxmc0Z9gmNSXU8mEafsD7OCIHZ1ZKXLsBf9JYdybCT8eKt8/qRU5C5953UD9yNsg FqVwhIyJRtZWQ== From: Mark Brown Date: Thu, 01 Aug 2024 13:06:29 +0100 Subject: [PATCH v10 02/40] prctl: arch-agnostic prctl for shadow stack MIME-Version: 1.0 Message-Id: <20240801-arm64-gcs-v10-2-699e2bd2190b@kernel.org> References: <20240801-arm64-gcs-v10-0-699e2bd2190b@kernel.org> In-Reply-To: <20240801-arm64-gcs-v10-0-699e2bd2190b@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 , 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=4893; i=broonie@kernel.org; h=from:subject:message-id; bh=OvR6N9yV4HsQXOrwTY0B56Y1UKQlPL/MpYNNiWGIOU8=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBmq4YHrCxoJewftWxnfq1JlcPpYr/nO77UmMRV/g7S YVEk79WJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZquGBwAKCRAk1otyXVSH0KqxB/ 9V8lZDkQrrXcsvux9tk7R1eV9YCB9mCgv2Q4nH5xihY0ZzyoSsqiELgMjgt47Ir53+f8lbqNS7cyQB I+xudEB6RcvJF4uBteFTILgVTSp/QZWdfdvlP606m3rXGb2OVV+BTqc8JQTM+aM94uU1xP7QifRd22 iwljaiX/twQduRHonpm2IcsNgOEZIgG9EvS6b2pzPcyQnVma8Y37pKLarPPisLyolug0EvX2jH+Ca2 ZDHi1MtqM5/1pZvEEa17v+w7O01Tzz715NDODwbsM8fWGWjQ+/EYSNM/1DPYHUh3u3Ce8smfZ0m09p 2Grb0JOhTkKuKIk+M6U9e6lTwbhgzk X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Rspamd-Queue-Id: 4332A160025 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: emfidxg3gdoajn35yhuit1xum9eeht4b X-HE-Tag: 1722517054-736562 X-HE-Meta: U2FsdGVkX1+ApfpuV+MqV5VeNYkd6V67stcraWsJ1AEzYd561SoYEYeVS8zUOAD4q24I5s59dRnBn5prHp2Fj/AmUmU26HrwhT/vyBtUZ05iHpRjVdur53GMf6IgFTo6V+3+RfShdaFdYPTfDRgIax89AmiCYV/oth4BSLbKmW2MYif95NoJqXdR3+UxKiFjsTdN2hiwR7kX+OjFs+7UXGnDnA5EF+D8ezlEW7Nl3uixrE32bE/k04udXz1Zy/XsBLqKmeQzZqwW6gFNYGddRYwzHtE+3DjYppCrzeFRQWBNrSKo1q5g3/WVQ0hjocPplpYeb72n0VHdfJsWdAybtDQNvawSzZkjH2bH0aEzPtBC0O/1BzYtsTXByZjws3FB6AeYmIGvJHXjw8az88NQfIWjdY668xMkcQ2xwfu7zTAnqwx1vCT1lfwJI1XKRL3bqFp9+yzq25KIBeYzTxr00lCbfrUbY42IXYEmypmicrBJ7QyBN72e5sVm/QlNEV1Gbf5urwRCzDKiYOn6IgPCD5J66o3FPAI9WDshF5xTkhD6F4MF68splqoC4r/+7m9A/1Iw/cBQvwnQy7tc3Urt2Un0MND25Ey+6lClg1gdLENaUfds61btl/X8w5OBz1vq76AGGFg8Y3FPSlevgXwcDYFyxp4w+NaSdX5BrzFRcH1TNLK0V+nDei5sz4CJuEloQeqBbooioyIPnp6EPWiEdzS7oO7CkT9mhw7+VgwOh5xVFqoQd+kL8XupJ50y4uGR3Oxx8QTybMMeHnrkmqK8u0vTqshvg6OY8eUr3mT5EGBJBTzOtEY8xerI4RquPf+jN/6tgFGGllLE4F2IDV4lSuHXxreXjEtMI0EWmkrBG7cYdtO7OKiXfLc22HqTMMZiaHTTUhM1NJblEvx+bn0+JIXW6D5oyPobdBYmB6ogyLa3joCTXKChiV43KF6iKC7pKrKGWuKgwTuRPX8uexl NGhzrTbS 2tG+gJXjtdlpBT2HWLbWdy/oFuAfoby7b+8gz8/YYLZnjHC8UKGVd4JgEjlxvLCaClFvnlr7hJbJJPvR2WZMiVe+C8EcACijii/l25eO81KSMLtRedOzORLAM8pX3qZLGBSlXzJqW/jIZ+yj9ehA2Gz4Z8OAvL6OP70ZWNgT4aL5dXRzxQj3SMkJOMBu19iTMJ0Ghl0i79gVaNzxqHrL/VRtFeF4aSGOUfzQsCQXzHN2/7nBQnpjdLzksZ48vUq09gljELKmeUH1iG56Z25nAa9v6GMegvmCgNrYeLuERUspJ8k2Kr5zkdYtzYwjPeK3YOe/qHK3OC4vXBC3bdzULBx2yeAsML7PNT2Gn154/Hp7b7+cuKlS+6kBrRpOJfxMN74SR 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. Reviewed-by: Thiago Jung Bauermann Signed-off-by: Mark Brown Reviewed-by: Catalin Marinas --- 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;