From patchwork Thu Aug 22 01:15:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13776846 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 2C7C8C5321E for ; Sun, 25 Aug 2024 17:31:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9BFC28D0002; Sun, 25 Aug 2024 13:31:50 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7B5738D0010; Sun, 25 Aug 2024 13:31:50 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5B8BF8D000D; Sun, 25 Aug 2024 13:31:50 -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 2F4AB8D0002 for ; Sun, 25 Aug 2024 13:31:50 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id BDFB21607CA for ; Sun, 25 Aug 2024 17:31:49 +0000 (UTC) X-FDA: 82491460338.08.C7D46BD Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf22.hostedemail.com (Postfix) with ESMTP id 189F8C000F for ; Sun, 25 Aug 2024 17:31:47 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=LLU9yqwS; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf22.hostedemail.com: domain of broonie@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=broonie@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724607039; a=rsa-sha256; cv=none; b=U1Q5D51IfgOYP8J9zYAlvcJAqBGQdAb2CBHiRy1ZEScl8ZegWucT4Fj3CzoFsZieg16zMQ GiHPrZvl/FGe4e+Fk/jnF9FyucA7FswVyCa/SZV3ShGpRCd8gDJK2XuoX2WmQn0f4TmT0c I7rnfbs2rv+cw8lGY/BfPi/Gxzz62g4= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=LLU9yqwS; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf22.hostedemail.com: domain of broonie@kernel.org designates 147.75.193.91 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=1724607039; 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=fFyzuu3M8hkoX2V7L6cBYhq1e9tFwhDoMHpjdj4snUcTtwFUodyQe/MAzl0DYx1xyiywn+ J4pE4vzYDAerOaHOl6i0Ls/T94SJ7nF/2swyc6TOyAXoVJvEKoQiLrmSSdySatVEgnEY8E thjTKH4QuEwVQ+3wwVKDIukVNbGgXBo= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id E5395A420FC; Thu, 22 Aug 2024 01:16:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 103F8C32782; Thu, 22 Aug 2024 01:16:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1724289401; bh=RTcnwKCnbai8FTs65e4TvcOIdbpBUk2hXTCc6OzveBw=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=LLU9yqwSxch6MFrP8tnNZceidPgjpIEkIMwk/qXQYo7ntV/JhjWtqDRRPHUFgsGXE Hx+iYD7VBlv3+PxLGRE0XpLyeL/RdWks0gTPfhMfFhJYl0iyQ9JAtjmFOHljB/gVK6 NEW7DH81A08HGAQjBV1XiWO88s3X0eYRXXTD7tALpVFk6UGaJKvosZ0kuFjudDFNOt HTU+j/EsZFLm9xhS1YdAuHFbBawqaPCRnyeISngLKYBirNpAZFV3J28GVjvIMjV1V8 PIcK+TTw3HJbs6tmIrlWidfskUIZzMLpB0oYX/ZLK5pPgjnEepKKHpXdt2booN1GgH fPqYWZ0ZI7mYw== From: Mark Brown Date: Thu, 22 Aug 2024 02:15:06 +0100 Subject: [PATCH v11 03/39] prctl: arch-agnostic prctl for shadow stack MIME-Version: 1.0 Message-Id: <20240822-arm64-gcs-v11-3-41b81947ecb5@kernel.org> References: <20240822-arm64-gcs-v11-0-41b81947ecb5@kernel.org> In-Reply-To: <20240822-arm64-gcs-v11-0-41b81947ecb5@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/pANAwAKASTWi3JdVIfQAcsmYgBmxpEmmIVD2Zm2u/VWnYf7Sg6kYVu6incBd6yxRihZ i7+VTG2JATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZsaRJgAKCRAk1otyXVSH0AUrB/ 42gjPonmMebgIO3VegHoGWbnRry0JQ57FqxHabGM8rSB+f4ltZd0tz1GUs0DsyOlbt4cKPSc+25dfK 6OyG2PBA2LRETtAIfrK7LLrHa5zfqeT6oyeyvBwTli6h2m31x7qwR1khcW5blFTclPDQzyZHKeucHw tUT2K7ZrXpPcyvun8pMaXE7OJe6my6s7tmkUm8Gxm5UgMCPnBi86GQ/Bj1p5I8qqk6D+xgemQ0kHMh xVyneaZ5gqZ5weRYPn+FfkosN/1pwgPcw7iU3xL/IXjWVh7phjKKxDXVGwaGgJmv/W4fW1jwbDZjij p0L3VXPisl+ZdGtK1bQ7sWz1WZ+Qo9 X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 189F8C000F X-Stat-Signature: qpzp1retqefbwxi3q81rnqpn9tfbb319 X-Rspam-User: X-HE-Tag: 1724607107-904445 X-HE-Meta: U2FsdGVkX19MMkccCRcNIrs0LWeFTlZY4MtSR5XMCPxqYcSdBT4zpNCFHEf8cKy6sW2YOpXuWvpsa2Bb0JDRvOJeMNPwWzNMNQb9bul+ZYagidrY/8qwSPiornNl488fnGcM9giSCxVDvur5X8nNUBir0QZrBuRJySnwyPGhEA/sDHA+xK6gPe9smwnHbsbkntJ+v1ihS0qWmQ8OGJK3I21QIsQbPcNaaOTZA5kCgRXtUZUhTLxWtj61Kmb6mvqEUP1Gt4MhsoxLTVn81RIE68f9pP0rK7BFz1j/f/C9ikWLaI/P+lyDS7lzgvifXooZtWNWP+ZQsVy1lalwVNQShzF55hE8AH2jk+rE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K0BogSIesI/Gk6ESG+vTt8zLXwbkenqFPr/Epyk= 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 Reviewed-by: Catalin Marinas 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 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;