Received: by 2002:a05:6359:6284:b0:131:369:b2a3 with SMTP id se4csp3517476rwb; Mon, 7 Aug 2023 15:09:47 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEHaD+tCucdBrTCSiBWPZ9iQbxpEivNxu+wBd2l/1lLgcXPM+7yscQvoRnaDAcLhH6SEQWJ X-Received: by 2002:a17:902:e812:b0:1bc:2c79:c6a4 with SMTP id u18-20020a170902e81200b001bc2c79c6a4mr9408182plg.56.1691446187245; Mon, 07 Aug 2023 15:09:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691446187; cv=none; d=google.com; s=arc-20160816; b=gXj+GeYBhEq/Pn0V9WiRf3lVb49PoMU4cuxgXOrJjbieGXzMWs8gHRYcmbs2SOU5e+ UTYGOhyXUo5IlM6zwC6ghkQ4uj9vOF9XkTH6wwCk5ywwIQLN42z2nG0TmEO5M2hxWGJE rmXq7XneULfQGXFW765xrIjBF7RseouUSDmROPU3v1MjW1MWF1bX6lgGsp6FOWC0SL2g LOXEiqTyl33nxreeIRs56pOLpdV11iPuwjWX51KEaWV12vZ5ziQCOw4H9W+zpgOm0z+6 mO+mxNx808MMEqDmru+4dtpfya672x32lgUPSwKms0aFRiMxtFHYWgNbHnkvKdDt06b8 ADzw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:in-reply-to:references:message-id :content-transfer-encoding:mime-version:subject:date:from :dkim-signature; bh=CkR4LcFi/Apf+N3CqhDJhcfTx9Wd0mE1G4u6yDFXxCY=; fh=EdAFSmIgUzZTC6WCy4Jg1wBZ0/m6m2d/OjiRUJ3BsTI=; b=H7TP+tzze10NwYll+nxIqcl/DjOA2Rr9yR9RLELLdZaL9KsHrgsx8ZxwIAZG4Pnb6p P6otOHyphpFoICLmBXylSTrliVn4pdAEkoi/lD6fFASwK/qy1WDiXcNVUwb8puJY4jF3 3HCFvExC1pgx6f8LmTW1Trsk5/TvCTl+GIA75aD7XaodJ/6lhztoLSyLuzu0dYwKNS3r MWi2lqhQrza8BzcHlWI3xEdj9opzwln6m6flJEiJEEXerf4B2tM8+nYSl2PUQpH17i/+ Hu6smAy6l1iFYIKuhJQUrDaXnWds0GtOSzmGNh7oLRx7QjynrswlRf1QySUs5Ks0XkzC n5Vw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=B74Fbqog; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o10-20020a170903008a00b001bb9370760fsi3563284pld.90.2023.08.07.15.09.35; Mon, 07 Aug 2023 15:09:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=B74Fbqog; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231389AbjHGWFq (ORCPT + 99 others); Mon, 7 Aug 2023 18:05:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42132 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229986AbjHGWF0 (ORCPT ); Mon, 7 Aug 2023 18:05:26 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C4155EC; Mon, 7 Aug 2023 15:03:50 -0700 (PDT) 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 4197962290; Mon, 7 Aug 2023 22:03:38 +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 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit 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-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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; +} -- 2.30.2