From patchwork Mon Oct 9 12:09:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13413527 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 5DAA7E95A91 for ; Mon, 9 Oct 2023 12:13:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EB21C8D0064; Mon, 9 Oct 2023 08:13:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E13E78D0031; Mon, 9 Oct 2023 08:13:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C8E178D0064; Mon, 9 Oct 2023 08:13:44 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id B1ADD8D0031 for ; Mon, 9 Oct 2023 08:13:44 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 8856580202 for ; Mon, 9 Oct 2023 12:13:44 +0000 (UTC) X-FDA: 81325813968.17.04FADD6 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf09.hostedemail.com (Postfix) with ESMTP id 87A84140002 for ; Mon, 9 Oct 2023 12:13:42 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Dn+JTB1Q; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf09.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=1696853622; 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=0mPDfNpkuIpdkA4GglwZicDksv4LLc26Nc+ZwAqEc6E=; b=LTNO6E37a9ZCOchzGXib0ejtfh5CDjcZ20Euvia1My7WSInaTvuT3cXZcWhxiQX/LwWTZA ZsdHwSiZe40lbJRBF138W77kMaUnq6aEq1zKZxk0jvapR1AJvBQuZEwUDIJbrfrf9GtVXN kNWdb8WpuM5kxrDKP/ewQRB3hEPjYuU= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Dn+JTB1Q; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf09.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=1696853622; a=rsa-sha256; cv=none; b=rDxPsDGnxDEq3lLBUSToXkfkV33PQbdZL0iV08pg3hbrBj2A47RG6dcn6pqapMGr2eNY1Q m171+hRqyxNqxwgnesagLvouJ/6eNCyxTR0qf5KMS+9enJThdT9aJRRbV5Ac+GZzYmDSx1 s3wlMSgRLwgEbDWXWyumF0XnCq/H7bQ= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by ams.source.kernel.org (Postfix) with ESMTP id F3F43B81181; Mon, 9 Oct 2023 12:13:40 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id DC4BAC433C7; Mon, 9 Oct 2023 12:13:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1696853620; bh=1i+GSTpTbO5P/qld4yaRpURtIaffrsBagKFpXCXGmzg=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=Dn+JTB1Q6MBUWHdRdmX1zVJKlbRP82QRAInihBBBPCSvLKASb+DMl1YxFOajCSY/E tkJxqrl67Ke/aipeuAJYv1qEFEMNYX7QYKbLKpb0ekjLWD0oOMx6jarm8pyL68fEUN p6daZNVLAQmNitNSRtPqqKZBwI3Cx32AKFrcA066xT5vh6vQbSnKH9mHyKO961wa2Y f/ZBWZ/PuexVcNk05aIiD+VBQU23+uVHhBiPf+qdhMkqcU2njj48ROrM9XrLpKlskH UMCymKGUkrccZ6jkiKcq3ikK9H0NGmKi0heiEtZJWUtToYJdcn0frKtXAfF9drE77V QRp65+26zQUwg== From: Mark Brown Date: Mon, 09 Oct 2023 13:09:00 +0100 Subject: [PATCH v6 26/38] arm64/ptrace: Expose GCS via ptrace and core files MIME-Version: 1.0 Message-Id: <20231009-arm64-gcs-v6-26-78e55deaa4dd@kernel.org> References: <20231009-arm64-gcs-v6-0-78e55deaa4dd@kernel.org> In-Reply-To: <20231009-arm64-gcs-v6-0-78e55deaa4dd@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 , 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=1i+GSTpTbO5P/qld4yaRpURtIaffrsBagKFpXCXGmzg=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlI+2l6LKdpST7ySLYv5LWeHkMNkrcQaOJdWoxoBqf QQUckdaJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZSPtpQAKCRAk1otyXVSH0JQsB/ 9QjqXMz883xP7630YeP/WsHDXXMqbUL1LZMm+9OQba6q0kMurV9IiHrfNL+X3/jURiix8fFKkuQG84 ZA7LYts4GHIaQ8SMmj5KuMNKNxXZyoz5b316NaTGLgUedhn8Q70Z0H7fLxagK1H7+ccLrJaKkbiETa yToRZqV1yatPowYRk614+CUA2/jURY6M5bWe3e8dfKBBrcnYqNPHInwX5Kx16mnEYJT+XPvXjUmUx+ kgvsAPwoaaZzoSfBLT+LuPy+oago2pJK7XJbrRfHheuH3Gzl0uEtdO4TOtiyc9Sb0096fLm/blVzBJ hSKrRrdwGBwx4X5JPWvIF5vyVrxGgN X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Rspam-User: X-Stat-Signature: ody4h43ur5hwhq1y95ypo9hkknzxdj8a X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 87A84140002 X-HE-Tag: 1696853622-558814 X-HE-Meta: U2FsdGVkX18l4ml/ZECzygQBKjVHfpfjHtN2q/blxf+H2pg6gaWZP+KD8iW1Yj87WFuESkJLVxvdyZzBmkj+l5AqHr1OKdm2Kl+ixO36Kmv4n0zSdhSx8IBEUjK62sCOxppgnHr/ttPXE3Fr8IxnMYC0+lihIMXOQKYf8hOP93jTPQzehZrcAHuj5gFEVa86pKCQTtx6z4cTdEF2khM2aIZ5LyEW91CVf/EO5GjHN0jftVkbIR5TjaKtuZzy6X/vzQ4fDMb0Q29Kt14FnfoYGdxzet3r43nVFE1Z7YQRAe9Lm8faCjVGhXvomoX6cUQ/aSZZmbeUl0Omo3FfhWsEUyAJ4AjXzXcPp0tjnVAKf8orT1j6S+N+CCeb9GxZMBRckRYsjz0FQV+zjwGsYoUSHDWf5DucsDlK05QfyHiWPTl/urNssPwJD/FUBcHsw2vsP977NzkCc8YOJNZqWn8FzFcxwcxECVZx98um8vo9A4H/JScbEOZYlSPDZJg5xbAAJBef0c0LjJnBk1EsjZjW2x93+yk1MElH0XaHoQksiYMlprpVnzgKMwC1cJrrQqzK7PKj9tDjHLeYMUBJ8XlkWxflFEBIh9pdmv4pJSSUsSJY8PZaS8cuydNcMbro+AzCiLLlxlAsk8rrvisZNNLij+UGbzN6pPmhrf/PZSsNF1L5WqnMSXKU9/mWuDxsr6suYb9KMK6draLbpYs+V8q7NPw09EMI3uDs7nB7SGN440CikxlLicrWEWqB2/mXe/dxMMatbaKsUbCoGNdPfesPnkLHrEBigI8zhTzljZejQ6t0rfdjkdzFYGnAwsZGvqxgFCknyGMdy3pcUjpXbLw9P+Z9xrYvm4q38FhceKnuV5HMsoQPaLSspp7A0I6v8Zw7ocpTf9hBif+CM7Lt5O6A3ijI1z+mKYW9NBJo/sFA5UdgKtSyFkIXBAXieLOYmnXXyh3DAgwE0AWOlIT32J2 OqxrY8sT AENoyDCjKhnotr+81JTABn9Z6q8/J+4/VfkvMWvi+ZDmwjRo8bNRfR9WPUKXQRDzofxgRMnQZzFFsaqvYWNapSmw8a+ZtxhW03IifAYlkutYGGSxPTGVJYUoAjijdrHRX4glsQAmmZdaNtR0VPxEukwtBJA+9srGQ5sGpBAEs2KrSa/B9SmuF0ZfFsq502Wj4n0kGeUJ7FCefCpMcS25n5eqC7FnvAABW4nB4lYJOB23CVezMax+6edtl7bSugt9lxbqup+F1scVlT4Oj1Wriotd16t0QnBY6tT4YU9OZkcVzOKdbD7fF3htTgyFAJNUoc7gXzcp6EA58UTZYV7yCVtRFk8XLWVCnPlZdvRytxJT15yIUXOqY4//MGQ== 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 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 9b731976ce2f..fe854a53099e 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 */