From patchwork Thu Aug 1 12:06:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13750401 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 11002C3DA4A for ; Thu, 1 Aug 2024 12:59:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 969FB6B00B4; Thu, 1 Aug 2024 08:59:49 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8F2A46B00B7; Thu, 1 Aug 2024 08:59:49 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 746326B00B8; Thu, 1 Aug 2024 08:59:49 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 50BB86B00B4 for ; Thu, 1 Aug 2024 08:59:49 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 1198680D56 for ; Thu, 1 Aug 2024 12:59:49 +0000 (UTC) X-FDA: 82403683698.27.AFE0413 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf17.hostedemail.com (Postfix) with ESMTP id 9F30C40021 for ; Thu, 1 Aug 2024 12:59:46 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=GUkmhbKE; spf=pass (imf17.hostedemail.com: domain of broonie@kernel.org designates 145.40.73.55 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=1722517142; 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=e+GtNoZTL+PLww23zV5G42yIi6YXa+1nPigCcryYOuE=; b=Uv5/sGxpDIp7yMlD6ycyumlOgyiQqMRG4+Mhu30SimK9nlx90QfRWuOGGcEA1bgbjE4g/K MDiahV5Q1AJpdOtcRE6mA2kSuEk47NaWazX5aTR765/kx89uyrLFT0ORn9WMLvK4T0Q1MR ALZDWtLVgScLDUAtlk2rqrrWsI1G7qA= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=GUkmhbKE; spf=pass (imf17.hostedemail.com: domain of broonie@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=broonie@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722517142; a=rsa-sha256; cv=none; b=U7Wc0Wz2t0g+UaQPLzphTgB/8SX0GEljHxhshSL4kr5m8YDQ3RBAPPbXuxbzNcmZxsDJEp d3FufexGfim1hTw9jCNXgM8FG4CmC8J+apA1mJUGjcw6NnqXkbZoo6SnMwWcK9QCJ4AYE9 /vo1+OoHV1ylC9CVr7UiXfnDM9yCiw8= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 12B4BCE1849; Thu, 1 Aug 2024 12:59:44 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id F3DA1C32786; Thu, 1 Aug 2024 12:59:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1722517183; bh=l/cgslNqipFgR1QPpcC58vps94W/JCkBuifrxenYdK8=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=GUkmhbKE+Q+QxkXEevPPCfiJRXd5FNakIcikzhEN2r+Y3716TgIqNQqi3/13TrKdN HIBgZQ/1SxhfeUku1mjS3dI9gJd5Y0/ivPkfcKPfKg5jlCt2uVtcovZSQiwB4bUAY1 60yd94kZUoDzqiPkoVcVrpqLeaIFFQp0zmet05AAhv8xve7WNP/maR57YBUIkEFAM0 ReBIG2n0DHxR5fn2goMGp20tPmqUycZyTj7dOsbsp4ZLaKI5uA18FfBlXS1JAaMxBP evqz3VGXwik6kkVPFN517NfkDbi0mWCXCoD94vwEkIkjYY2LMkGf9buq6RTQzNFkwa bloRRVzHt8Imw== From: Mark Brown Date: Thu, 01 Aug 2024 13:06:48 +0100 Subject: [PATCH v10 21/40] arm64/gcs: Implement shadow stack prctl() interface MIME-Version: 1.0 Message-Id: <20240801-arm64-gcs-v10-21-699e2bd2190b@kernel.org> References: <20240801-arm64-gcs-v10-0-699e2bd2190b@kernel.org> In-Reply-To: <20240801-arm64-gcs-v10-0-699e2bd2190b@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 , Shuah Khan , "Rick P. Edgecombe" , Deepak Gupta , Ard Biesheuvel , Szabolcs Nagy , Kees Cook Cc: "H.J. Lu" , Paul Walmsley , Palmer Dabbelt , Albert Ou , Florian Weimer , Christian Brauner , Thiago Jung Bauermann , Ross Burton , 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.15-dev-37811 X-Developer-Signature: v=1; a=openpgp-sha256; l=5708; i=broonie@kernel.org; h=from:subject:message-id; bh=l/cgslNqipFgR1QPpcC58vps94W/JCkBuifrxenYdK8=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBmq4YW+701C0PZ1auv1x0ZYEkwavziBHutsB26kIic off1qfiJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZquGFgAKCRAk1otyXVSH0KvmB/ wOu13MLk5RjXsF3gjaYR24ss/GiRkPXfkTMTPQb7ytIqHtjun7ntid2AAFcjdLng+3y+Ozw3Wfi0NK Tg4zsf7/hUv0Sr6rdDVaQWSz2RUqS5W4ARSC0sSjtOjTQx5ZRdj1o9kS2BXEqi0aJp/qLl+FHfQTnV 1G64p0RRP7baD+jgOSoWwFEU599l1245JyBmReBWlqt0Wo2i0ULYXqzzKKgapzB6wfLNmPCm680IX6 zf6mHYGwq63zPkix9zp3xpQHZ/bDHZ7NHyXJOzRJulz8dVKlrANaTJ1/QXaTfiFX6RJlG9LPOfNiBL 0Y1WZQwObI0/++tANtghpDMRAyN5dX X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Stat-Signature: 5uy8swjoqrnbe3s3eqcfdyukbntumhhn X-Rspam-User: X-Rspamd-Queue-Id: 9F30C40021 X-Rspamd-Server: rspam02 X-HE-Tag: 1722517186-486118 X-HE-Meta: U2FsdGVkX185fWJsO9MSYdK5EDb8Uo3bE301OLlK/0s+QWbqZr1aMTHeR8aYST9OGZyp9YwKlnnCzbW3hLjnIKqYXQ7Q9bjdQK6Cvxb/Ry6Lb8QpBL3Gh+BLr/gSmcw9Zcqz0gslg7O6oMh5wlkPS0oo7CluwCguNO2IhsbsBsgowIHvqiKWaet5uUIuecsG7ck+4F1EdvzfTrsjrnOyAR7w8jPyyfhbKugxkT1R9PMYCRZmAaTWI1maCQKpMz2QIi+wIRe3CxDu2bUFiPnpMYkAk8erLg6lB2d4/UpldB6Wc3pjrPn8nOjxkVyOzdErLunRA6FR0QjlZb22Y501feflARq8MMEivT7TTmxyDPaM98xQ1CuRDS3BKXgiF79Rz3P/IUn65ENujA0oGCa2fWsV0CvXtBIgVDgUmH3PMCxhszRumtefY7XYa8ZaJMcioLnTivAsKdx4rquadjekwER659vcVbLpGKHSpoTZ6RTlH+usnBTWYlANa9rCT1x1o80ICBjZSbp6byLuVZy9GmAfTgqEseXudMJssyMPbgZxWxNnMWjOOCb6/Bbjtd0XzK8iKOtw0WDU0Dp4PP7VD4FM1WzDP0fuqVxaNbFejOQ93z5rOmOPNOXfXUKyUWWaSaFLoVy6ABMKL+1HaV6axRLYqU9xDMkkJ7yid2TXhFdsISeZBOi7WcVvlklB+aV7jl/zC71G+Es9aRDlF99VC4YIFcdkHKigaK9gbRNQrYLFXQ/bhpUaU/a3WK212iDCinxrDQUCDCFT2s/i14Xx+oXBhLq9nwf4v4X40C8L0PX6Rvckx52oZalsrfszuqR7iiCpubG6lfjx/6xko+a2e4WiIRvUEj0sSDRP371dtjrYvwqy/3XLgYm9+E7vmZwG+K7Traox12Lg55EfB2ApRnNTZ7OygiGeuWMksWuR3CvNtt9YjPAT/A1FHmRq+oYNHmmM6OEKU5gabHBgce0 oGNinqeU Hs2CpQorjQx5iLofLeH+0i2kLqyaxNKIoY6K11MQul2SdZzQqynQzWUTZTCI6WJQupnnu4LHNMy/utNISrC0tMRyemGeGkcbm5ladGZXtNCqwangYuG/AuGfmHkE4oQkM90TVfAVABJXoeblKT5A414jZlIYEaRmX4jJQq7URpDdl3zLoaWOs+EFVewF86B91vfIBqgO7LUUO5LHJeWxK+5GsgJb9I3E+L8TFKgD8/MTMfHdfjR0qjHEkkEdIoulAnl59fLJu0lu8I0+LRMMN6uAr0RdhhT6uwG10LYbb522tDjtH5TqGAJv1ZpjImwBPN0ONe1YCFEyu7n8EZ80N3C1FGWmoviCqNwzir0PxU4s75n1Y6fpfDKfhQaCK3YIOtylP 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: 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 arbitrary 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. Reviewed-by: Thiago Jung Bauermann Signed-off-by: Mark Brown Reviewed-by: Catalin Marinas --- arch/arm64/include/asm/gcs.h | 22 +++++++++++ arch/arm64/include/asm/processor.h | 1 + arch/arm64/mm/gcs.c | 81 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+) diff --git a/arch/arm64/include/asm/gcs.h b/arch/arm64/include/asm/gcs.h index c1f274fdb9c0..48c97e63e56a 100644 --- a/arch/arm64/include/asm/gcs.h +++ b/arch/arm64/include/asm/gcs.h @@ -50,6 +50,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) @@ -63,6 +66,20 @@ void gcs_preserve_current_state(void); unsigned long gcs_alloc_thread_stack(struct task_struct *tsk, const struct kernel_clone_args *args); +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) @@ -78,6 +95,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 c55e3600604a..58eb48cd539f 100644 --- a/arch/arm64/include/asm/processor.h +++ b/arch/arm64/include/asm/processor.h @@ -186,6 +186,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 b71f6b408513..0d39829f862e 100644 --- a/arch/arm64/mm/gcs.c +++ b/arch/arm64/mm/gcs.c @@ -179,3 +179,84 @@ 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(0, size); + 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; +}