From patchwork Wed Nov 22 09:42:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13464419 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 07857C61D9D for ; Wed, 22 Nov 2023 09:46:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A35206B05BC; Wed, 22 Nov 2023 04:46:19 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9E5776B05BD; Wed, 22 Nov 2023 04:46:19 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8D4C56B05BE; Wed, 22 Nov 2023 04:46:19 -0500 (EST) 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 7EBCE6B05BC for ; Wed, 22 Nov 2023 04:46:19 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 421FD1A0295 for ; Wed, 22 Nov 2023 09:46:19 +0000 (UTC) X-FDA: 81485109678.28.21BD96A Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf05.hostedemail.com (Postfix) with ESMTP id D9CF9100011 for ; Wed, 22 Nov 2023 09:46:16 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=bO+EDdYB; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf05.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=1700646377; 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=nGowsrdBIlCMrQN6IvwN6mQelvaNZeDEi5rXdHlD8sI=; b=Nqan2a1QOjSpT/4WpwolB9HCYVUBueEkbq3B5FtQAmJKhN0KErS/1sXqOPoqwLjXBUYiuo ORinLyVjZumTXaO4WLpb6s/SJTM5jC6pRXK/TAQqjfBcsJW2y3nqcR0E35OmAA1InmaM/4 mYmUsDPfR/fi1hF2yoc0AMYgkkKTcDQ= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=bO+EDdYB; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf05.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=1700646377; a=rsa-sha256; cv=none; b=R8rcx507EsDx6+QqY6BB/xn4SF6dk7c/6Wmd71W3GKUOijz3f+39FsIOCFwo38zPKmkGB9 1Oslb21DasMeuUHzMb+tYg/jpox2FqSxKpKo8OkN0pUzW/aNPe14cFUOgUviKiqg+ALW5Q x2I3vxwqZZBcYeF0YwOwwInWNqtly6Y= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 58C93CE1E77; Wed, 22 Nov 2023 09:46:14 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id BF243C433CC; Wed, 22 Nov 2023 09:46:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700646374; bh=bFlT5/awvXTof78KkT1d+gL2ROO+ZrM5pYa3JBpSrio=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=bO+EDdYB5VGZElKlGYPJaeuv0B0743DFAhHLmHDfS9M0pJeWV2FZLMZufVNyS02Pn 3CVj+UIACeUO0f8m2Lax3vNWcBPC8/DXe9LIx5KIVvKh55IiqceB7ZxMI+nUQmZ7OC 9NSrb2VB37RIVzuPiya49btFNT5kYwkg9iWQhaTiLYeLBPnfJa9WNUXAxK6tCfuIuG /U7dh9bY2lHlTjRUkVkSUempfu0xOSDHy3z8PJ8jM0rf6MWntWPo6zSBbbaP2v7EzY fu1GvOiR2woyzqDToJtuvf52Q5ny48mcXnET+shaTjYgo1uLZ/+ZJOVvrWLFKxw8uB 8F6iv99DT2QAw== From: Mark Brown Date: Wed, 22 Nov 2023 09:42:36 +0000 Subject: [PATCH v7 26/39] arm64/ptrace: Expose GCS via ptrace and core files MIME-Version: 1.0 Message-Id: <20231122-arm64-gcs-v7-26-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=4259; i=broonie@kernel.org; h=from:subject:message-id; bh=bFlT5/awvXTof78KkT1d+gL2ROO+ZrM5pYa3JBpSrio=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlXc0KraBhWo8Yu9TsgTVu/cTEFTnLR7d01WQ+/ 8pSUITq4fKJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZV3NCgAKCRAk1otyXVSH 0E7dB/9HiqI+MMrMcBA7CHdbJrqryaQT8IGaG8oX639MqYezbjknfbchZggQZNm1U2FGnXbkzku R87sUnYn3bidqmXKt+kRRarvJZZQ8COkGq8aRjZid/yhSg7PXgkZx3llJdei8n5JgyhBt1KVlIv bSltKZCWQZdAXrB8J6PGZJGxfOqwbBkIIQZIEF+IoBgpwOHcag9PjYwiQ5e1xIKqtcYTzkqIUuI 0Ob0Q9V/CR3tpxTzo766EQy7Y7M+yQ3nijDLSrpclfVRXwydQMDiiFr8+psvaol5E43QTIEpwpF YFDWWKN2iQqVHZm3hxklP8V2dgmJ6RHM8XFM5BCIkCQa89Qv X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Rspamd-Queue-Id: D9CF9100011 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: deb567usfzn7qdzdnt77x4p9j75bnot8 X-HE-Tag: 1700646376-381145 X-HE-Meta: U2FsdGVkX1+iqEvhR1lhDqQaNrGSL5UnVb38Z7/AKyJ8fEEjh85NAgyR2jDeB+380OtZt0V5x5MZnuVAcrd0XcA45hr7Nqtgc8r38ZzuX+yKomB95cGKi43QBfgZxp/5+nzFsQ5VAY1f2IlwC2CaG/ijyIe76chnlOolkk49yu1OJPje32WPpCRqhZY7bRnDLMHqmaKvdA32LJvarxQ80EAsOzUtntWIfcUGTLj8Fk+FJZnXJW0k3Nzd5x2F6pQydii+wXXWKMwNpe1saVgq4WvL60HW8AEBL0yT+8XBYcUZYiLMb5LqCjY9bCFnZ2equEV3Ys6nNYuGWD4hDJg3vambI8mJJ+PS/5WQPXp6CyxLZ+LVMSjIVu8fQQ1uE/bg9lzp0i7VR8rYtFg4tNupKpTUsn/i41lsZSDB/RXnDo9mfbZ2mNZJOJhKZ2AAFtwpl82wm2QS0EMF1/et0HF5GEcsTqYsCZDNivqyDnSLB8gZ1XD9uRTG8OCBFtdIkX+3EFeLjJii80tdZYLsQdL/asHuIGlkLojfFzsRFhQaOiWQovSYum1DKK/SQu2A/RWCTvsyoqL2tBsT3oSd1xwJskDsYOQJOFjtRrDG6tGlRzaGEUg4ZlMZySZeE7VQKObtQPotqSTo1E1+upp43ypapUMglxhf2H/svcjaXqkAuKLDdtWePRbIuchLaui+OHShhpY7L2zU5ur6jZDESXnj8y2EuQo0vubJfLfmJcyF2U3RAG0QIF3XntEMF8O6AXxG/0HDOnPXNXM+g/pNPTs9NGDL2zJI+pLMihVLXTEJbLwMtsjYCjOEfUq7bcCcpcxnG0NDkrRuMo2QKi7P0x34f/U4rfSi+unozTb2YkEZWQz4NKsNnyhi6+P8BuaObzZhVkf8Z3iwX/BX+8oquVcGCkY9SXg5p4aFlfCEgBj9bRQIA2TcWmvGt8RINZ7eGoyUKkYYsOjgDgIe+8Db5iA saZuha/F pmp3SLRN3f+KuYyAWLCaGxw5/rdddWd/J+wJLOXqluxZ89XG8TzZz3dFkTfGKt+/Z4PPhcPAgfTqCRRWJV6bYkt+XijW7mfVD9g4DD93o4H1CPEIT68UA4XM2Y8HNA2Q+oRCLQnK9sR0nsDO+IBfVq6wYc0tJ1vGpy4PnIvWEKzt4Ivg9bidQCbwREVLVugEdsTGvBPqpvI1glS5CcvWeQy8Q9rKAA+9q5+043dqtueAL5GNfEX+RmpL/sTC4YkhuyPmWmXUx5UYcwwNSmCGr8kA3B6+tInHncgTsXGbThv/OjMAFSCH7PgQja1kAV0KSimRPqnnhBUyMwPYYb8blGe/AAlJIYYhJ3ywu87DyxkDcUYY= 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. Signed-off-by: Mark Brown --- arch/arm64/include/uapi/asm/ptrace.h | 8 +++++ arch/arm64/kernel/ptrace.c | 59 ++++++++++++++++++++++++++++++++++++ include/uapi/linux/elf.h | 1 + 3 files changed, 68 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 20d7ef82de90..f15b8e33561e 100644 --- a/arch/arm64/kernel/ptrace.c +++ b/arch/arm64/kernel/ptrace.c @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -1409,6 +1410,51 @@ 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; + + /* Do not allow enable via ptrace */ + if ((user_gcs.features_enabled & PR_SHADOW_STACK_ENABLE) && + !!(target->thread.gcs_el0_mode & PR_SHADOW_STACK_ENABLE)) + return -EBUSY; + + 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, @@ -1437,6 +1483,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[] = { @@ -1587,6 +1636,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 9417309b7230..436dfc359f61 100644 --- a/include/uapi/linux/elf.h +++ b/include/uapi/linux/elf.h @@ -440,6 +440,7 @@ typedef struct elf64_shdr { #define NT_ARM_SSVE 0x40b /* ARM Streaming SVE registers */ #define NT_ARM_ZA 0x40c /* ARM SME ZA registers */ #define NT_ARM_ZT 0x40d /* ARM SME ZT registers */ +#define NT_ARM_GCS 0x40e /* 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 */