From patchwork Sat Feb 3 12:25:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13544151 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 96B4CC4828D for ; Sat, 3 Feb 2024 12:32:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2B2C26B00B3; Sat, 3 Feb 2024 07:32:33 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 2621B6B00B5; Sat, 3 Feb 2024 07:32:33 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0DC346B00B6; Sat, 3 Feb 2024 07:32:33 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id F30316B00B3 for ; Sat, 3 Feb 2024 07:32:32 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id CBD9280694 for ; Sat, 3 Feb 2024 12:32:32 +0000 (UTC) X-FDA: 81750430944.13.2F229D4 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf19.hostedemail.com (Postfix) with ESMTP id 185641A0016 for ; Sat, 3 Feb 2024 12:32:30 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=OVUxytKF; spf=pass (imf19.hostedemail.com: domain of broonie@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=broonie@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706963551; 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=fH3BVW9H6W4BAYCgCayyTdwDQmbksoIMny64AwR46Ds=; b=aKhnaHLIPEOgiF6tmVxNYzjjEDtVCmIzHQnBgxvNhPdoZeiO2jCjiLmVNrLXd0PCnnuBOC o7Bv55niPVewjh0VzvHTKvQpPctHtbKjiE9lKlP/NAffeWlkZUbCTsmVz34nI4t4eJfn4Z xpa/e20r3YJ5WwFxBGEGt64gXE+uIUk= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706963551; a=rsa-sha256; cv=none; b=PgbPvAfxHjn0ZTHvQFEowjsrn1r9lUemK9iZMs0g1mGwq4g2EMBlcBHZjyyY4lc972zw8y EV5/Yde5IczQWfCGpisVzkxA0cfmTBkiB4EBk88ZBFHhzPPn1IQtlJN1roivTEdYsqev/E viRjmSbGyNacabG0PajbvHYtoEp2pbQ= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=OVUxytKF; spf=pass (imf19.hostedemail.com: domain of broonie@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=broonie@kernel.org; dmarc=pass (policy=none) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 5A6F860A6B; Sat, 3 Feb 2024 12:32:30 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CB039C43390; Sat, 3 Feb 2024 12:32:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1706963550; bh=rveKNfu2hCQMG8B9aw4oT/fCpDlPhKgEsrOfPzfCWL8=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=OVUxytKF4cbOXupmS/ihwQNM4dZsOOLDAvJhjCLwCmxiFkSrviJf0QLWifWrWvSeT 5GiqDNlpoCOTk6sp83OU7yOuRv1Y/t3SvCvusmQF0aVQRN+eMFcZnfuQUacGRoMeI2 nO0nKPgYXapl9CDjYIEOgo+AiAp85cSaD6zEKpowjwuxybvVTiana2ffA4ljddX4gU BNW87m/nl2j2MKUtvMH9Bf3mcXrm8pb6O4C5zyqQpBBPxYGd4vOTa3R7BAnbpBxw8K szbAAq6S56depNeBR/NzyG3/Fi2LbmwcXYvc2iN3RSBjv74sdwKBVJKP2SfNb2Tr+3 aESzWRQDBWyAg== From: Mark Brown Date: Sat, 03 Feb 2024 12:25:51 +0000 Subject: [PATCH v8 25/38] arm64/ptrace: Expose GCS via ptrace and core files MIME-Version: 1.0 Message-Id: <20240203-arm64-gcs-v8-25-c9fec77673ef@kernel.org> References: <20240203-arm64-gcs-v8-0-c9fec77673ef@kernel.org> In-Reply-To: <20240203-arm64-gcs-v8-0-c9fec77673ef@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-a684c X-Developer-Signature: v=1; a=openpgp-sha256; l=4258; i=broonie@kernel.org; h=from:subject:message-id; bh=rveKNfu2hCQMG8B9aw4oT/fCpDlPhKgEsrOfPzfCWL8=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlvjDk7EpAnHZap7zSw+wJSg+tMt+cn06om44qNnGU QjiFAzuJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZb4w5AAKCRAk1otyXVSH0IXFB/ 4uczWs8MrgliAEFN68CZRQmFNTKr9rUx0pm8uCNqx/GFBP3N5jkdlltYG89cHTkoVkm5wgk/AGBOpB FLdzgkPBIusbVPEXJopEoC3MfvgtdIPKXWxLBL7fPdFraYUmiPydFX0dg/kEr7Qemu7Xa/IzmTRZ0v gfGDW0QTRWh7kzjR2I9QekcE3p36FvPwTVvBahT6pTmSE4acpvVHyZTB2hCxg+wFSfUlGMQ711e6iG Sz4qoPGyIU/2/lE3fVvtkK1Q+OZB15GxxQQDKxiFpicj7Losb7hCbTByyl4TTb/G73iiuo+0srQ0KO 73A8X+VrHNE4litPcQ+w4wWhPyGYWX X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Rspamd-Queue-Id: 185641A0016 X-Rspam-User: X-Stat-Signature: x6iy6u3c1phknyt8rwr7uy7yihqnopjx X-Rspamd-Server: rspam03 X-HE-Tag: 1706963550-134241 X-HE-Meta: U2FsdGVkX18RBMMdW+WGFV1TPRweKUiMxR69zPsPkUVD117B6eanyYjrKFHNhR4gdA+izx1N+l4sbDBPRWE3bMUsdMPN4+WUOfkDl2k2HnnS1IqT9SfIq0yQW3pPUG2JNr2fBnKCZYnN0u75bNnWvVHv/Ku6C90fMAT3nr8VZLeEQkjMy1KdKM1kV+Ao2mIxidvIxUJOo07k0rLe7HYh+MRDGSR65DX26WryNZpBcRmoZC2HK0pamY1joMn7ZgS866CBT9tIhOx7cLdfRcDtmrfiB6xppRtSfe41DpEXldao90uXDtTXBgYE4lM4AOE7+cgAu5I+O0QplRWF1t3NL4YItTKrIhEZQQO/Ky/W6GqfSE5zOLcCADjwZC/VfwpQpu1pDLpjmsO1heKHdnb/kbIyUUH31aoeAHbK2ewiAFNAmVkmQOk/6+EVJWa+C31nZh8AGhjAT4FHYGIgMDUD3mMgf3OcX2dp1VolfMGPvfkn4sySTmXthkUWm2lSHV6DOEaPF7IcuZBdp9x9PUQ4QchsjtlGwhwtpuSw2ARgpP7ckRHHKwAytQXh2omjCF/oM2yzl74LowozcuAAgtvv5i871ShcB9wx0AmQBN13iIic6gnHcBkqhOpmiQlwE3spZNH6pAEf6qNPfmex2ZBqT8kkc2sOwQdFSv5BpR/vqE6HUmo7Ssnok9LZSDet12kblU4JxodUypt90i9l09TaP2yucXvYExsDv+a1eu03lzMtlBHhPScl9BkxhWI/fsQrqAbXl/4yopNIpRq02mymZa/XjwdSm0Tj4Uj/RzqruZvtmiUfQAm78l/FQqF5rCAU5VcKSSed2hgTzpHZAxEatqokEpjADleYJJ+2a02iNX+i4zMOXAcwuTe/JrKZVtHLOQWPqAzm+k3jJ+eOqJYjaBIMoi2gGThNGnjTJhDwfHzmFCp2z4i3yyU4M1OO+lrH4eBUOyISwIBEaovfFRW tiufbTlr NjNHLLRAaLzMnTeIHM0UM8hEx98iW3oBqdVPLWOFuuyOU4oJ7nJ2Nf/T0fJyY1puuK1nQajPOXepJMgdBQjkjNnoD8iHW6s8FnPOPjyn3W3yoFYHkEKfeRLCL7vTcaX0yK7i4ArWlyxLR7paR4750xZPrY43GZ6HlIUT4+5q9kzsmfhgV5L21TjESnwnI36yse/KIq13GGtQYh1oBtLK8ru5jItYs80MVAx7BGxvc4+y8WgYcBj729RXWUTZWkNIno5ka8HlOckgMFlpYDirivG9PL9aKFMaWaYFnFCi4DnADfJFjm5LsXrNf1ezkFslVLVNtQtUd0d7o5RNI5uL6X7f/g/NLrJIOU2saX4V1sudFjg8= 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 dc6cf0e37194..c8dd489cfca8 100644 --- a/arch/arm64/kernel/ptrace.c +++ b/arch/arm64/kernel/ptrace.c @@ -34,6 +34,7 @@ #include #include #include +#include #include #include #include @@ -1411,6 +1412,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, @@ -1439,6 +1485,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[] = { @@ -1589,6 +1638,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 */