From patchwork Mon Aug 7 22:00:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13345142 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 CB096C001DF for ; Mon, 7 Aug 2023 22:03:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6D274940007; Mon, 7 Aug 2023 18:03:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 65B8B8D0001; Mon, 7 Aug 2023 18:03:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4D58B940007; Mon, 7 Aug 2023 18:03:41 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 3AC1F8D0001 for ; Mon, 7 Aug 2023 18:03:41 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id A1D13C0128 for ; Mon, 7 Aug 2023 22:03:40 +0000 (UTC) X-FDA: 81098686200.11.F59E12C Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf11.hostedemail.com (Postfix) with ESMTP id A479E40010 for ; Mon, 7 Aug 2023 22:03:38 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=B74Fbqog; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf11.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=1691445818; 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=CkR4LcFi/Apf+N3CqhDJhcfTx9Wd0mE1G4u6yDFXxCY=; b=pdJBJiot0V3/E3KlSisi+11j+h1u3xL/ZOoJZ/wMExqKZZ1GJKPB+7nGtJoNzqQ31C1+xl O9+sn7WcW9ZnODs3i2f3nFm3086vvjgwLLPQ2GKq5T9C5uEe22Jg0xYBaV2mEwe6kFW9C6 s3pq2lC/Gwa0qGhecC4JRKCQfz9NzuQ= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=B74Fbqog; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf11.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=1691445818; a=rsa-sha256; cv=none; b=6qc/yOtMTlGsmHJWHJssOvOYtzEwpqh0wbX8p0kKU4REf84jNEBnlOcSvfYf/5GYB2H5gR XWqVaiWk5tLON0726hBo+tPyPz4zEFdQi82YW3xF8kw5oGevcmR62op75+mitJJMjaoJ56 Fivb8NycdEmz95xQ6p7VQi9RsQOF8u4= 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 D34D7622AB; Mon, 7 Aug 2023 22:03:37 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E9200C433C7; Mon, 7 Aug 2023 22:03:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1691445817; bh=3y05YgLEE7escOOoGXKTI0JE3+x1TY+XK9aZf0p3Mn4=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=B74Fbqog9SobYYSRdQGRfsY0wS0zXngCFdVXqm4tNtT96B/U0FlBg9DK00lvnbed4 6MFUvy31cmp9rTx7ucCxClFDnQG3b/dNWwZWVTlfDZGYEu6CEPNupSDodgDBHW6yjH rLPVdcHQhWT68Io4Tw/j99fRJrc7Du3rzi/Uzbhva6tKG92qedL1zRbSEWAaqvFfpw 2uCOYuESzXe+oMNgLmgsfNE2ESBJHNN/1JyGTK2ZOVTm8rmeCGB5DVZVBjUyRjEfxS jw3mVIbciC9EQFrZvLDANSaaV6g0C937rv5lGqhFBvE8POZUQg6EN51clJFUoPUw+9 DOXw+ITTF0FoQ== From: Mark Brown Date: Mon, 07 Aug 2023 23:00:25 +0100 Subject: [PATCH v4 20/36] arm64/gcs: Implement shadow stack prctl() interface MIME-Version: 1.0 Message-Id: <20230807-arm64-gcs-v4-20-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=5673; i=broonie@kernel.org; h=from:subject:message-id; bh=3y05YgLEE7escOOoGXKTI0JE3+x1TY+XK9aZf0p3Mn4=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBk0WmnszHr3M8G3eeRDGQExFVNxevdHjgqpcegTJ+o fbEKDnWJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZNFppwAKCRAk1otyXVSH0PKHB/ 9nhGymqffd5PBWSptq58Yzne1KmThxdqYjk59Ov1VQmxowgD+eZM/iacta2nJ9Jz9gZvrvOAC/cfhh sIjuAw7vjrvLUhADoB5b+s25w4AC/XMygrUkkz5mUZUdL603vdoc26fLxQlnzQLae4bSJQHLwrWzcb 6/zQs4tBYxfcbl/AhZSL53XpDNd9KPzS1TxknerkKpNEzAoyCQ0BP6xLkjvofHahe4YPUNH4UDanBn ELW5J3IKC/W+AFyUTzgV568BdFzf5rgcWZbc/Slh3JAA6F1auYSy7FGN1KWakKiBMcllsYsVSheiv6 GRPh560WsnD6ZZYFeX+ZKY9dqCgJgQ X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Rspamd-Queue-Id: A479E40010 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: j4z7yhg4ou5ef3mhyxb8bezzzz1gmnnm X-HE-Tag: 1691445818-654059 X-HE-Meta: U2FsdGVkX1/dnvNHmVTNZlwkrbCWaLDL0UdVB/3FS06tG93FgpfjLyi7rh4oB9OM1tsEsfCrIwBRFpoOZ2yYU80AQwtpqtQ69+T2RrQ4Iay8xQ03y00wNrV0NmtGDe8/+E0jCHQIbmO6xZtflX8+rCjev+6++reQqHqdw+cQtYSuX9KrKMhCLjgLGQd5gwHfynyTnoT/7yjSubCzvHRzuLx5E9lc6AETqYNarVHXhBO7v6tWm2mF3c9fLCUY8Gip/FTkagbG83sxi9CkALqtR98qcOZn67jZI+gTeQzaXrHz3/nHEsdUoSBi3MXg1+Eqp1gokqOU8qNmBb5vAmB6dr7/53qRXnmod3XLfCD4UGZ++vTy8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version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Implement the architecture neutral prtctl() interface for setting the shadow stack status, this supports setting and reading the current GCS configuration for the current thread. Userspace can enable basic GCS functionality and additionally also support for GCS pushes and arbatrary GCS stores. It is expected that this prctl() will be called very early in application startup, for example by the dynamic linker, and not subsequently adjusted during normal operation. Users should carefully note that after enabling GCS for a thread GCS will become active with no call stack so it is not normally possible to return from the function that invoked the prctl(). State is stored per thread, enabling GCS for a thread causes a GCS to be allocated for that thread. Userspace may lock the current GCS configuration by specifying PR_SHADOW_STACK_ENABLE_LOCK, this prevents any further changes to the GCS configuration via any means. If GCS is not being enabled then all flags other than _LOCK are ignored, it is not possible to enable stores or pops without enabling GCS. When disabling the GCS we do not free the allocated stack, this allows for inspection of the GCS after disabling as part of fault reporting. Since it is not an expected use case and since it presents some complications in determining what to do with previously initialsed data on the GCS attempts to reenable GCS after this are rejected. This can be revisted if a use case arises. Signed-off-by: Mark Brown --- arch/arm64/include/asm/gcs.h | 22 ++++++++++ arch/arm64/include/asm/processor.h | 1 + arch/arm64/mm/gcs.c | 82 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+) diff --git a/arch/arm64/include/asm/gcs.h b/arch/arm64/include/asm/gcs.h index 4371a2f99b4a..c150e76869a1 100644 --- a/arch/arm64/include/asm/gcs.h +++ b/arch/arm64/include/asm/gcs.h @@ -48,6 +48,9 @@ static inline u64 gcsss2(void) return Xt; } +#define PR_SHADOW_STACK_SUPPORTED_STATUS_MASK \ + (PR_SHADOW_STACK_ENABLE | PR_SHADOW_STACK_WRITE | PR_SHADOW_STACK_PUSH) + #ifdef CONFIG_ARM64_GCS static inline bool task_gcs_el0_enabled(struct task_struct *task) @@ -61,6 +64,20 @@ void gcs_preserve_current_state(void); unsigned long gcs_alloc_thread_stack(struct task_struct *tsk, unsigned long clone_flags, size_t size); +static inline int gcs_check_locked(struct task_struct *task, + unsigned long new_val) +{ + unsigned long cur_val = task->thread.gcs_el0_mode; + + cur_val &= task->thread.gcs_el0_locked; + new_val &= task->thread.gcs_el0_locked; + + if (cur_val != new_val) + return -EBUSY; + + return 0; +} + #else static inline bool task_gcs_el0_enabled(struct task_struct *task) @@ -76,6 +93,11 @@ static inline unsigned long gcs_alloc_thread_stack(struct task_struct *tsk, { return -ENOTSUPP; } +static inline int gcs_check_locked(struct task_struct *task, + unsigned long new_val) +{ + return 0; +} #endif diff --git a/arch/arm64/include/asm/processor.h b/arch/arm64/include/asm/processor.h index f1551228a143..e4255749844a 100644 --- a/arch/arm64/include/asm/processor.h +++ b/arch/arm64/include/asm/processor.h @@ -181,6 +181,7 @@ struct thread_struct { u64 tpidr2_el0; #ifdef CONFIG_ARM64_GCS unsigned int gcs_el0_mode; + unsigned int gcs_el0_locked; u64 gcspr_el0; u64 gcs_base; u64 gcs_size; diff --git a/arch/arm64/mm/gcs.c b/arch/arm64/mm/gcs.c index 1e059c37088d..64c9f9a85925 100644 --- a/arch/arm64/mm/gcs.c +++ b/arch/arm64/mm/gcs.c @@ -93,3 +93,85 @@ void gcs_free(struct task_struct *task) task->thread.gcs_base = 0; task->thread.gcs_size = 0; } + +int arch_set_shadow_stack_status(struct task_struct *task, unsigned long arg) +{ + unsigned long gcs, size; + int ret; + + if (!system_supports_gcs()) + return -EINVAL; + + if (is_compat_thread(task_thread_info(task))) + return -EINVAL; + + /* Reject unknown flags */ + if (arg & ~PR_SHADOW_STACK_SUPPORTED_STATUS_MASK) + return -EINVAL; + + ret = gcs_check_locked(task, arg); + if (ret != 0) + return ret; + + /* If we are enabling GCS then make sure we have a stack */ + if (arg & PR_SHADOW_STACK_ENABLE) { + if (!task_gcs_el0_enabled(task)) { + /* Do not allow GCS to be reenabled */ + if (task->thread.gcs_base) + return -EINVAL; + + if (task != current) + return -EBUSY; + + size = gcs_size(0); + gcs = alloc_gcs(task->thread.gcspr_el0, size, + 0, 0); + if (!gcs) + return -ENOMEM; + + task->thread.gcspr_el0 = gcs + size - sizeof(u64); + task->thread.gcs_base = gcs; + task->thread.gcs_size = size; + if (task == current) + write_sysreg_s(task->thread.gcspr_el0, + SYS_GCSPR_EL0); + + } + } + + task->thread.gcs_el0_mode = arg; + if (task == current) + gcs_set_el0_mode(task); + + return 0; +} + +int arch_get_shadow_stack_status(struct task_struct *task, + unsigned long __user *arg) +{ + if (!system_supports_gcs()) + return -EINVAL; + + if (is_compat_thread(task_thread_info(task))) + return -EINVAL; + + return put_user(task->thread.gcs_el0_mode, arg); +} + +int arch_lock_shadow_stack_status(struct task_struct *task, + unsigned long arg) +{ + if (!system_supports_gcs()) + return -EINVAL; + + if (is_compat_thread(task_thread_info(task))) + return -EINVAL; + + /* + * We support locking unknown bits so applications can prevent + * any changes in a future proof manner. + */ + task->thread.gcs_el0_locked |= arg; + + return 0; +}