From patchwork Mon Aug 7 22:00:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13345146 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 71664C001DE for ; Mon, 7 Aug 2023 22:04:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 12ECC94000B; Mon, 7 Aug 2023 18:04:06 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0DD8B8D0001; Mon, 7 Aug 2023 18:04:06 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E733194000B; Mon, 7 Aug 2023 18:04:05 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id D40F88D0001 for ; Mon, 7 Aug 2023 18:04:05 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id A57BAC04E6 for ; Mon, 7 Aug 2023 22:04:05 +0000 (UTC) X-FDA: 81098687250.14.82FF3CA Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf08.hostedemail.com (Postfix) with ESMTP id AEF0C160021 for ; Mon, 7 Aug 2023 22:04:03 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=t8SAodXm; 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=1691445843; 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=7Xh0jTR0r8+ize43vTl6QzYzhaS83l1nMVxt1u8Uym0=; b=cQw/gizYCC7lVLMalwTQL9VDn3zoijeRArX9LI7/qlyqcWFCQ7+YqJ/KOy+BrkL8smp0TI YohsSVsJbRKPUDi8Dd3TULJujRuEpHyoFZVAjIIqpDHYE/Cr2bYPd2Sp0NAtiC6dE7mAEP pmuJFcregzsx3CtMfxdxu2DRGhWrNR8= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=t8SAodXm; 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=1691445843; a=rsa-sha256; cv=none; b=OYZhlN6ATg2VFHo2ONx/fx1pVO9VYb7DV0YRfvgW+4SVe1XxWlz6r/5LqffqdwK1IGKhfx h7XYyaBUjsWyo1rHldDJz0Hs2mqAeD8cE1L7FqT6RsgUUVw9rkZtN76+5ykPPXRhAJ6fiH jmj/qJUk82CLiT0FXa9rwynve+18vuQ= Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id E40A6622B6; Mon, 7 Aug 2023 22:04:02 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9889AC433C9; Mon, 7 Aug 2023 22:03:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1691445842; bh=0QIP38ZumyMQzrMYtc7Z/0L/bXiC+1OudC2zVRODlMU=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=t8SAodXmV30SSAG9TfCx9T0tc1JXTUDa17ANZoNGGQ8XgDtYpu9jzRwEyCbO/Trm5 HUanShH4KzIaVhqJcHgCNdMADk1LvqZrbyuNIf5KqOEVvL+HGwZgVaByJosicjM8XQ iiUrN0aMxoVoCoTynD6gYha5cPt/aqEpAb2TMWLiALtxeKyuZi0gJ1lqNXYR2tiR5a KBXU5KBnq7mzP1GXxJK99+t17miAdwgg9oriwEXiSPYk27mPJ0Qi+MZLYHm47pIqqt Qo5fmQxX42GbxEYST7aAr58Wb9PBT1wIAT0Ryx+nesWif+E4czI4wG8LIt5VbFolyP oaE6I5a1UNOxQ== From: Mark Brown Date: Mon, 07 Aug 2023 23:00:29 +0100 Subject: [PATCH v4 24/36] arm64/ptrace: Expose GCS via ptrace and core files MIME-Version: 1.0 Message-Id: <20230807-arm64-gcs-v4-24-68cfa37f9069@kernel.org> References: <20230807-arm64-gcs-v4-0-68cfa37f9069@kernel.org> In-Reply-To: <20230807-arm64-gcs-v4-0-68cfa37f9069@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 , 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-034f2 X-Developer-Signature: v=1; a=openpgp-sha256; l=4259; i=broonie@kernel.org; h=from:subject:message-id; bh=0QIP38ZumyMQzrMYtc7Z/0L/bXiC+1OudC2zVRODlMU=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBk0WmrLgjCxlg0UGVpy5E7U+JcqO/N4JM5rjw02mao hZnV3zWJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZNFpqwAKCRAk1otyXVSH0PnlB/ oDdyHIOa8icx1bsyF+SJQ9duVfWg7gwTkO/RfjwXSEpwdUEMMlXLDWrwhb12vC5Mm3Bjq6StFpKbgS CKSDQJJIY+xNJXFtxREcls2VA7vIKUexY0482OqGRTM7jtul02zjku/wNRalUHx+SRLWPenORSda2Z fXANEjIjEfPtX7FoGdpDkK877+dKUfzvCK6X5YsWhUpq6J+BBHUuXVw33S4CP5Dwzbx7EoS87RLc05 2ZN5FOuD+uXdItk+ltL89Dncj5XytV3T815hsHegWS9fEyAiKnuP1Ho2zEAqYS0C3+44MfWoe8jiTA 2bPdAXBLpb3l0KkFMhl97WPzxFmUl1 X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Rspamd-Queue-Id: AEF0C160021 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 7zosjg978w73ifgxudyypkk7ix6isffk X-HE-Tag: 1691445843-542828 X-HE-Meta: U2FsdGVkX18IJPovvMwK5rPbasOygKpDk1UOfqJ1IS0eS5MRvzvf30wxcah68lFCzD9ktaSm+BDt27VtmJ4dMRTcXU1sCCBBkJ+j24lzK4JmObUWwT9Y8Wqw9Qd5YHD57JpxEEU6WRrRi49DTEMCd6QJsGiFlaM9sjUjhZrVh1VN4je2TCTc7vRWY6Efi9ij2dl9CEfeZxbzI12OPZWLMr7GHYLkudOP/L6Q5jLfW48Wuww9DhJbPon22YxfZn+wy0Ok4WldcVxZwGMUw0O93dSJOwkYRnrp8U1T8S7sn+K7nCz3PZmGrOjcwUaFnhV8lo+UiRBAAE3HyXJrrjomdybqbmrn3Ilv/ISGji+Ye4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6W4cOQAvZsjFU= 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: 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 d7f4f0d1ae12..c159090bc731 100644 --- a/arch/arm64/kernel/ptrace.c +++ b/arch/arm64/kernel/ptrace.c @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -1390,6 +1391,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, @@ -1418,6 +1464,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[] = { @@ -1568,6 +1617,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 0c8cf359ea5b..00f698a2ab17 100644 --- a/include/uapi/linux/elf.h +++ b/include/uapi/linux/elf.h @@ -438,6 +438,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 */