From patchwork Wed Nov 22 09:42:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13464416 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 9BDCCC61D9B for ; Wed, 22 Nov 2023 09:45:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3F32F6B05B6; Wed, 22 Nov 2023 04:45:40 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3A3826B05B7; Wed, 22 Nov 2023 04:45:40 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 26C056B05B8; Wed, 22 Nov 2023 04:45:40 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 124166B05B6 for ; Wed, 22 Nov 2023 04:45:40 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id E1C5B80556 for ; Wed, 22 Nov 2023 09:45:39 +0000 (UTC) X-FDA: 81485107998.24.867D020 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf25.hostedemail.com (Postfix) with ESMTP id 127F1A0011 for ; Wed, 22 Nov 2023 09:45:37 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=EpTv0RoK; spf=pass (imf25.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=1700646338; 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=Sfm+zGm8gQVD9hEtJsdOIJdLxVtR9XcyUBct7dLRVTw=; b=pihSzIxYOOSiV1XPlviQ3FPNy5+FCx9waQntfqDbfEFWZUUNxJZAs56eNpBGVMfqevzkXT lFSj41JEcHe3hiJrMpj4995Z7HIlOQAb3zbWtSF4ATHDGmSm4ZoHeHvTQOizBcJLbEyCDC FqYkz0hyPZ9ldsuIV0imerLV1h6OGho= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700646338; a=rsa-sha256; cv=none; b=qrT2tE/bk/Hsyprgoj1FMi2ZC0re1ef0sKXPRcx4fByEK1j9iO2CD6ULvSXLsHmrboH9C4 EIqoBhJDDcFUDVjOSbz8TSeZUj90rISM8F6ay8ZDL+fCQE7Bx9v8pZAIDjRfMjosyaxvod yEblQc5PIabnpXyvc1E1NNfzutOm764= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=EpTv0RoK; spf=pass (imf25.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 3AA0361CCC; Wed, 22 Nov 2023 09:45:37 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D60BAC433C9; Wed, 22 Nov 2023 09:45:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700646336; bh=x72fc+VK4oqJgPWdK4g1pRbvmyi52iEXFShOF15RYHs=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=EpTv0RoKA17/YgCmkJvV8UuVMOQQW4ty21K7jgH8A/zDrtvbdpHZTrkYfvOzn+Jrc PH9eCQwcx73OSxmNFKy6ELZOWGHDJDHp0DJAay+QyWiC3TQTN3uXMeyeM6Ok7/BnDh QqagZTyt/OwGcuBf3c5ln+69zT4wXopP+IhTN341xkPTlZ+qaoT1p+o9IVqjmGFiCh Q2mpgFpunOaHVub8T0um/ENHsTvoE9G7IVDz4lazbW0UPSQxbUJXEMd6NA/7iB/2k6 rs4/3VYRT42yIe5Zv6PcFG1LwXPMzzULY2KwPyMGGSbntuGWCJu60ncwjmLo15AQCa BBo0vEZz1QLog== From: Mark Brown Date: Wed, 22 Nov 2023 09:42:32 +0000 Subject: [PATCH v7 22/39] arm64/gcs: Implement shadow stack prctl() interface MIME-Version: 1.0 Message-Id: <20231122-arm64-gcs-v7-22-201c483bd775@kernel.org> References: <20231122-arm64-gcs-v7-0-201c483bd775@kernel.org> In-Reply-To: <20231122-arm64-gcs-v7-0-201c483bd775@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-0438c X-Developer-Signature: v=1; a=openpgp-sha256; l=5675; i=broonie@kernel.org; h=from:subject:message-id; bh=x72fc+VK4oqJgPWdK4g1pRbvmyi52iEXFShOF15RYHs=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlXc0HgY785xSQh4xaC5GmEKN/hUf0aNGwZWzsN J+sB9i9FraJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZV3NBwAKCRAk1otyXVSH 0AVEB/9YxdfnJWe4XxsXvdgrnhh9yBgTzfPpy2Cfm+j5T5DjzF64dE8vzpwqEryRhKs6H0GDUgS f3Jbjko6vejw/6rhQCpSZ8l5czU1WZGMsl/06vE7gRABuHVq5MwmtZvHXeUsAHT0lRk7eSUotPZ C/ZwzmQLUgbPJovfzpfOAsLW5P0b/EapVNA2oFIdHZtMM//qhB7pyw6q51/cZ90L6yAuUYN7fyS 0pJ8ll7w0v7j6BQ5KvRe+p08dRCuinUdbs2R5crhSIdKxhW15xbYoFkFVL33HR2qasw0LE/AlOu /tAyjhhLeY0t8V+jk4kMFBDWkGOb4SlNjRsZuVJeFVbOOcUy X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Stat-Signature: 1xdrmu4qqrtpkaxgcmktjfki6fziw6gi X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 127F1A0011 X-Rspam-User: X-HE-Tag: 1700646337-384260 X-HE-Meta: U2FsdGVkX188+qEoSGx0G92dB/Z28J8WsGTwZ6QA6k+3ycq2ym2vEWcszWq2Zc4OSpzihqKE7APR9cgOvQB92lP2K0MC4M9m8hyiEo9oKvkHezkcH9i9mNysewNVpjZqao6h8LLRoZ6N9bJXUDGX9JQjEuW9YWvU/w4I2vT5BeYTPG2oLWCzZPPTNChK2gssMa/g6/dougt24R3Rn3HWI+NRPuGrkyjxTG5BJp88qb6XpCFuBZ29Snuv9iVGziI4URgWU1QDF10g0s00M2v3YuoVTRtxp3MPh07QmAkvZwNwlMFZleKu49m63XvyIRQKoYCxh9tGErEyFvVnTvcT7/PQNcYYyx1a4pSDqf25jUJoOuHFLZPTuGjFw5JpyULXb9TMckfoQqsxnAdBV+3nkcUWRDRtvz+Q1KeHOQUCBPn2bifm+OEAQpbdnBk8EMAng5Er5YBexPKDVc1PRVMGaACk3CsclIiP8y+FAX8rvRmEFP7ZRXwThZvvWzWsG0QRrWMRbDQFcGT6UKdXT4/C6Ggt8kXedJG7qmYxqTPIECXEBFZWSvcWptTTXzFsb7t8hBv3EKgPvquqA54Z9ccf1pVaE3hteedOqlqV37QCfXWcXOk53JyqRCX4JHB2a0i31U5LdhAfZeTdZ7VZ6HuKe4tU/RiOgA8O2YfKHoxMv4RhIqJ4rKetCX3lTwFJs3cccyiHaI+oEcFMSLe9H5UN9PQ+V7x9hEQtzoBizqekw6h2IVWFpxWn+xV/yfycKm1LLLbr/AZibJn0AWK4D9CO35V0DSaNQsVEgHP/Xj1JVKo9Pa1tnkVNXIqpM6PZTprmfOfSnqDkI9LeR8r4KLkaEZmcHzV6FrVGncNC1NCWKgOREwS2P0tGtK+BwyKSZ9l8xzijCZvyhbwhoSg2y60OIMkZ54GJEtO2yZHl6tNBOnCSjgiRUjAXImsZTkRth+usJMgRm+2vJg7Aq5jFI3+ oWiog6do 2Wn15Ju/qiFWzkXDzGefktlhgx4aROWcSLxEXTRhcQSOCyxZLUNbBTP0jF2B8vX9Q5UbZCChoJKpWMlsJLkh93SMopSowq6wBADipHHScSVnWDKwwS45xZ3oYS1b7VHmcATxtscsxSQA0D3Ld2Lviw/SwPzGZLgKDrzDtEteiKinPBwl6at4d5U7hap6s2gkzppHYOSieT8JQARqd5UMH3UpM56xfXCYVzRgb/aKru1qPRB1Cibnb1IdTEvwwcCJm6xFAcWHCI/O6q79XTefVm5yAAqN7y190CLkG/+K5b+eR6wGcRIk/FOqmGZQ4ohwQ2FOf 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 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 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 c28681cf9721..029d20ea7878 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 0d4881173b5f..315bd698de86 100644 --- a/arch/arm64/mm/gcs.c +++ b/arch/arm64/mm/gcs.c @@ -113,3 +113,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; +}