From patchwork Wed Aug 28 23:27:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13782188 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 D4155C71150 for ; Wed, 28 Aug 2024 23:31:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6A1CC6B00A2; Wed, 28 Aug 2024 19:31:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6514E6B00C3; Wed, 28 Aug 2024 19:31:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4CAA76B00C4; Wed, 28 Aug 2024 19:31:48 -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 28DED6B00A2 for ; Wed, 28 Aug 2024 19:31:48 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id E1305C021B for ; Wed, 28 Aug 2024 23:31:47 +0000 (UTC) X-FDA: 82503253854.27.7BFEF93 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf05.hostedemail.com (Postfix) with ESMTP id EB423100007 for ; Wed, 28 Aug 2024 23:31:45 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=rGArz2A2; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf05.hostedemail.com: domain of broonie@kernel.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=broonie@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724887885; a=rsa-sha256; cv=none; b=PB62a6QEZZQaPN6BHHbt42yKzqMtMV/Mtmad8/CXbaypUktehNwkLLYSR0t0bD0klJBz7Z YUia04mIXgr76ADcxEhBxZTquaf3f3HOvL0oqhX1gdTnLEP3yaeAlaHlsIt6of0UKvBL/p 6HBX0OqTgY5cggVrMvVyKbJ1akLKRWI= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=rGArz2A2; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf05.hostedemail.com: domain of broonie@kernel.org designates 145.40.68.75 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=1724887885; 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=ZXC7qmAPK5+ytFrjeQwm551YGqCp6yCcAp4NbbBoCVQ=; b=0SHnIJJdvtxWahdNENdtyoVlsNEYa+gppF6htWna2TTmPNn7x49VGjFW+v69cP2ixAlG+v rQ+h4mq3jm98NQoy4zqUnxQUVL+c3qI+QDDxg5nEQ36J02+z6h3eyv+7lI23JfhG32G1ph 5LSutyiWRyU6krP/9fZtwwYoMjODTiE= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by ams.source.kernel.org (Postfix) with ESMTP id C2ABFAE408A; Wed, 28 Aug 2024 23:31:38 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id F20DEC4CEC0; Wed, 28 Aug 2024 23:31:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1724887903; bh=jioPgJdKS7IGyoJt/boupHCVco/oNVrYySiKZKN1ka8=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=rGArz2A29GItyz/quN6YxeYuiZ+QrXS2XdZFy/GvHk8zWgO3/tsjI9MVkHdky8RGO BZeTPWsIbDO+8kb9fv2UggzcjMvOSfN3jqAplbyZrbdk3Rf49oEyd325drsqTPvCuT vKAMfaT2xkx+rjE9spVN16a+MIMzn7nItV6kB/8OO8dzgW7eR421KKbCtLJTeBigkn 44G2IFgwZwmonZBqHJ7OIo+qT49txs8SxTh6byC6OUzQ0gxoaZuc5UGhmFAOOLwaYr WLBIlVzOCpIXtAkW5RVWaq3OSWGMnkOWfK5Vw/iyvpueHXdpxPRhpoBky9IfA0tyF0 0vWP46QST9PFA== From: Mark Brown Date: Thu, 29 Aug 2024 00:27:42 +0100 Subject: [PATCH v12 26/39] arm64/ptrace: Expose GCS via ptrace and core files MIME-Version: 1.0 Message-Id: <20240829-arm64-gcs-v12-26-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=4197; i=broonie@kernel.org; h=from:subject:message-id; bh=jioPgJdKS7IGyoJt/boupHCVco/oNVrYySiKZKN1ka8=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBmz7KM2ph/FfEKgpcW0wvMATZEzYmq4o806SdIJLKM p/IvoeaJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZs+yjAAKCRAk1otyXVSH0Ar8B/ 9+RGHdBXSYYgzOnBqq4S+TTWvhRdGSHJ1qWPHSsZCBIfGwsxnpw1puM/g4wOzgH53HXVuA1gdUJDR1 d9HbJ2xmII6Ezt+M6UVY7iUiF16VqyQkLmf1gLXJ+P676ou7AkCmPYPhpVNXXomDhveK8H+e8tDKaW 5xUxpxwd1newUKY3nzxSB6kwot372RIVIEcO/4pAtavzKSRJ9Em4myWUyTRbdq8gIO5YMaLd6oe+2i sqMXPB+enSafWqWkcVmzz/NyGr0R8abgwg9Rgt+9HSIn6exkH9sU/63NuhaF7DtKbnDwdtjBdzUI4F KdClCiWBh4Ayl6wpeGRlKB/R1Cqbrg X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Rspam-User: X-Rspamd-Queue-Id: EB423100007 X-Rspamd-Server: rspam01 X-Stat-Signature: okbrmd95ib3mkwzy453g6jygk5s5ryem X-HE-Tag: 1724887905-113054 X-HE-Meta: U2FsdGVkX1/wHL1cLFVWLPDl+kQWVAEbEr+kxuwZxm7nCJ05GQKDHlTFc/ZYzHpN5fkZey20JFhqUlmm27pJD1Lu86Th9JlV4Tx11Z760EokSSSoZAOUZWfRXB6ehYSF4Y/AZK21cCOo8c0a4iWfntKcAti0U2JFPi4Ux+/+m9IQtiNviHCHE3C40BW5ghSQLLtagxk1xrUFFWsBF0L5SQfknm9IEHu7J8gjgqj9N0tYqXQVYgAWf7mR38cKl4FSlLMxJoGhvA/wmrhIO+0IuJXRVcd9IqWNFYCC5WitasbcPIOt4FT1xbF9Yz92+Z19qgOrP33O4rdK1Plp0DCwy8FvLKho5+3KckdH6oR0waL6hMqL/hHTu2aFaWnZqNCCDn9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xqLx765oDmbWfZ 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: Provide a new register type NT_ARM_GCS reporting the current GCS mode and pointer for EL0. Due to the interactions with allocation and deallocation of Guarded Control Stacks we do not permit any changes to the GCS mode via ptrace, only GCSPR_EL0 may be changed. Reviewed-by: Thiago Jung Bauermann Reviewed-by: Catalin Marinas Signed-off-by: Mark Brown --- arch/arm64/include/uapi/asm/ptrace.h | 8 ++++++ arch/arm64/kernel/ptrace.c | 54 ++++++++++++++++++++++++++++++++++++ include/uapi/linux/elf.h | 1 + 3 files changed, 63 insertions(+) diff --git a/arch/arm64/include/uapi/asm/ptrace.h b/arch/arm64/include/uapi/asm/ptrace.h index 7fa2f7036aa7..0f39ba4f3efd 100644 --- a/arch/arm64/include/uapi/asm/ptrace.h +++ b/arch/arm64/include/uapi/asm/ptrace.h @@ -324,6 +324,14 @@ struct user_za_header { #define ZA_PT_SIZE(vq) \ (ZA_PT_ZA_OFFSET + ZA_PT_ZA_SIZE(vq)) +/* GCS state (NT_ARM_GCS) */ + +struct user_gcs { + __u64 features_enabled; + __u64 features_locked; + __u64 gcspr_el0; +}; + #endif /* __ASSEMBLY__ */ #endif /* _UAPI__ASM_PTRACE_H */ diff --git a/arch/arm64/kernel/ptrace.c b/arch/arm64/kernel/ptrace.c index 0d022599eb61..88f525b0c4fb 100644 --- a/arch/arm64/kernel/ptrace.c +++ b/arch/arm64/kernel/ptrace.c @@ -34,6 +34,7 @@ #include #include #include +#include #include #include #include @@ -1440,6 +1441,46 @@ static int tagged_addr_ctrl_set(struct task_struct *target, const struct } #endif +#ifdef CONFIG_ARM64_GCS +static int gcs_get(struct task_struct *target, + const struct user_regset *regset, + struct membuf to) +{ + struct user_gcs user_gcs; + + if (target == current) + gcs_preserve_current_state(); + + user_gcs.features_enabled = target->thread.gcs_el0_mode; + user_gcs.features_locked = target->thread.gcs_el0_locked; + user_gcs.gcspr_el0 = target->thread.gcspr_el0; + + return membuf_write(&to, &user_gcs, sizeof(user_gcs)); +} + +static int gcs_set(struct task_struct *target, const struct + user_regset *regset, unsigned int pos, + unsigned int count, const void *kbuf, const + void __user *ubuf) +{ + int ret; + struct user_gcs user_gcs; + + ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &user_gcs, 0, -1); + if (ret) + return ret; + + if (user_gcs.features_enabled & ~PR_SHADOW_STACK_SUPPORTED_STATUS_MASK) + return -EINVAL; + + target->thread.gcs_el0_mode = user_gcs.features_enabled; + target->thread.gcs_el0_locked = user_gcs.features_locked; + target->thread.gcspr_el0 = user_gcs.gcspr_el0; + + return 0; +} +#endif + enum aarch64_regset { REGSET_GPR, REGSET_FPR, @@ -1469,6 +1510,9 @@ enum aarch64_regset { #ifdef CONFIG_ARM64_TAGGED_ADDR_ABI REGSET_TAGGED_ADDR_CTRL, #endif +#ifdef CONFIG_ARM64_GCS + REGSET_GCS, +#endif }; static const struct user_regset aarch64_regsets[] = { @@ -1628,6 +1672,16 @@ static const struct user_regset aarch64_regsets[] = { .set = tagged_addr_ctrl_set, }, #endif +#ifdef CONFIG_ARM64_GCS + [REGSET_GCS] = { + .core_note_type = NT_ARM_GCS, + .n = sizeof(struct user_gcs) / sizeof(u64), + .size = sizeof(u64), + .align = sizeof(u64), + .regset_get = gcs_get, + .set = gcs_set, + }, +#endif }; static const struct user_regset_view user_aarch64_view = { diff --git a/include/uapi/linux/elf.h b/include/uapi/linux/elf.h index b54b313bcf07..77d4910bbb9d 100644 --- a/include/uapi/linux/elf.h +++ b/include/uapi/linux/elf.h @@ -441,6 +441,7 @@ typedef struct elf64_shdr { #define NT_ARM_ZA 0x40c /* ARM SME ZA registers */ #define NT_ARM_ZT 0x40d /* ARM SME ZT registers */ #define NT_ARM_FPMR 0x40e /* ARM floating point mode register */ +#define NT_ARM_GCS 0x40f /* ARM GCS state */ #define NT_ARC_V2 0x600 /* ARCv2 accumulator/extra registers */ #define NT_VMCOREDD 0x700 /* Vmcore Device Dump Note */ #define NT_MIPS_DSP 0x800 /* MIPS DSP ASE registers */