Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp4433464rwb; Mon, 31 Jul 2023 06:56:36 -0700 (PDT) X-Google-Smtp-Source: APBJJlEXyYIKGtjBTAuwo391wcmbcR6Ghiev3TOo4A9ZrOeXSoVtbZhSibQztv2PSddag5GY5T+T X-Received: by 2002:a05:6a20:9184:b0:130:9af7:bfa with SMTP id v4-20020a056a20918400b001309af70bfamr10005202pzd.60.1690811795899; Mon, 31 Jul 2023 06:56:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690811795; cv=none; d=google.com; s=arc-20160816; b=v5Poi8zcW1X6liIaODUkw5ftIpL8JhfDrFnPk3m/NlR7Cp5O0gRg+GEw426vUgDykG 3sWMrw6ST/X5/b5GhZAKVmMC+oO6XL6vDP/A5bhiov2hHE+gw86YdO1eonlL8RG/lTJg mNzQACs1K4eR4w5McBNan/DVMR4thVrap6lzeOme55PmHFgtkRnHEMa+aP9m7c4/64XW w5F5wKg83KsgvWpjIY/WeWD0hPBjvlp4+lRSZxRJH3t4jrd0s7kfbzrjJOgTykXsBFN2 9kz0jYeG21KE7p3XtNuFyUfnou2LlrcLBMYjnFyehdH6P/OVfa6lgkeyRN1KaXghMAaO dL1g== 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=Rr2GRmUkRH4qcj8DwtxgJoc5E8ETtGtlZ90s5DNZBXk=; fh=EdAFSmIgUzZTC6WCy4Jg1wBZ0/m6m2d/OjiRUJ3BsTI=; b=QWXfyOI7Oy493CM1cUakVIVT++IJ5x61a0VzsSUD3ceJU8jUfRSLreZ8ILo2amp9UG Jr5on8joWIXW9JxF3tjPmsalDH0Q7iU6HivW94VnqfBNrtTmhpA2ZXvHs6xL/5rj9FHk 7ytQ4MMDV/tAXsnszOxB05E1S21OPjrLOpjcuNt4HWH4MIpav3ZsQNJWz8m7PAa32xWP QUDRsU4BOvk3tr3kTIM6IwTmEBXpRDFu6sxtQpJBw+9MDXZsJ1l9WCOjLggGqp+cwVI0 LYVVHGey8J6c5XDkZtFRWb6wy8E8oNlfmdXEZFot8nuDc2mnwWjCtDJ/aNldTbvEqSKy 37gQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=UNOMtRgb; 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 cl8-20020a056a0032c800b006872dd77e47si2501461pfb.155.2023.07.31.06.56.22; Mon, 31 Jul 2023 06:56:35 -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=UNOMtRgb; 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 S231370AbjGaNvF (ORCPT + 99 others); Mon, 31 Jul 2023 09:51:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42992 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230435AbjGaNu4 (ORCPT ); Mon, 31 Jul 2023 09:50:56 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D31CCD8; Mon, 31 Jul 2023 06:50:54 -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 719DD61158; Mon, 31 Jul 2023 13:50:54 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2E0F5C433D9; Mon, 31 Jul 2023 13:50:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1690811453; bh=AEKjV/BA0T20XbbVS98znUxvUes3P+lFxLNl3dughq0=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=UNOMtRgbah1mpzBWzzzA1VTcOb/1jrRV5/EvQZNOqPfsJGo3zJ16RUrgLCnNQG57q 2sTgV5SvIVLA4JyxIunB1RuU8Aqo2cS21aPekTcbBUdj73VHAocDOWCDk0G+nchj8F EcFFHt+kPZSQuTVs/RBELQ54DfYcQZ8RmwdkhFaKR4Noy0waoEQNa26AeZudwGZjJr 777z+Nkz6cbdBXEqjeKDMgCNm3xZxq7mqB+xRrwGH/DObCjAKerUVOH16ZBc1z8GkN /QgCpGhVReua4PGN4ZuozUfGEhimG5pmf+G668Vkp/bDpmjhxsbzwHtsfgPOjs1ux7 Xk6sBrmLl1OIg== From: Mark Brown Date: Mon, 31 Jul 2023 14:43:10 +0100 Subject: [PATCH v3 01/36] prctl: arch-agnostic prctl for shadow stack MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20230731-arm64-gcs-v3-1-cddf9f980d98@kernel.org> References: <20230731-arm64-gcs-v3-0-cddf9f980d98@kernel.org> In-Reply-To: <20230731-arm64-gcs-v3-0-cddf9f980d98@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-099c9 X-Developer-Signature: v=1; a=openpgp-sha256; l=4712; i=broonie@kernel.org; h=from:subject:message-id; bh=AEKjV/BA0T20XbbVS98znUxvUes3P+lFxLNl3dughq0=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBkx7wUpu1yDaR36IlwDmlUq9r96GHEwjyTLGb9nho8 nyOJGGeJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZMe8FAAKCRAk1otyXVSH0F/fB/ wK6w/cAUGsvzFtYxqT0ScYoTc5n9vxFst0ZF1itibIGCn5WsS+4QfZMrYUoqNii+ZDKslOJH15ZSUG of4V7vA4EXKHlzT+KKEgcI9Z/yDD4mRoNm65Xk0L+o88htn3o/UZ7vVDiFPsvZXHeUjxJBgVoZjQm8 5lPDAz8KtiJ8SDJCBox8Y6Uv9x82YHEqWRqMyeApMk4PAKllk5v6o2az5dcbtI4QVMNdZmyYIRyQ1y 9w6XFwvIEIIBOf099ig6rOdboqXNOg4kQmNnYnj+XCr6rEFM2VOybjNlnAqC22QSjPrbwLYEbWyeqG DMbfZnUushtaAImMpCT4uinZbJi4pv 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,T_SCC_BODY_TEXT_LINE 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 Three architectures (x86, aarch64, riscv) have announced support for shadow stacks with fairly similar functionality. While x86 is using arch_prctl() to control the functionality neither arm64 nor riscv uses that interface so this patch adds arch-agnostic prctl() support to get and set status of shadow stacks and lock the current configuation to prevent further changes, with support for turning on and off individual subfeatures so applications can limit their exposure to features that they do not need. The features are: - PR_SHADOW_STACK_ENABLE: Tracking and enforcement of shadow stacks, including allocation of a shadow stack if one is not already allocated. - PR_SHADOW_STACK_WRITE: Writes to specific addresses in the shadow stack. - PR_SHADOW_STACK_PUSH: Push additional values onto the shadow stack. These features are expected to be inherited by new threads and cleared on exec(), unknown features should be rejected for enable but accepted for locking (in order to allow for future proofing). This is based on a patch originally written by Deepak Gupta but modified fairly heavily, support for indirect landing pads is removed, additional modes added and the locking interface reworked. The set status prctl() is also reworked to just set flags, if setting/reading the shadow stack pointer is required this could be a separate prctl. Signed-off-by: Mark Brown --- include/linux/mm.h | 4 ++++ include/uapi/linux/prctl.h | 22 ++++++++++++++++++++++ kernel/sys.c | 30 ++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+) diff --git a/include/linux/mm.h b/include/linux/mm.h index 0ead9d8013e8..43fe625b85aa 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -3964,4 +3964,8 @@ static inline void accept_memory(phys_addr_t start, phys_addr_t end) #endif +int arch_get_shadow_stack_status(struct task_struct *t, unsigned long __user *status); +int arch_set_shadow_stack_status(struct task_struct *t, unsigned long status); +int arch_lock_shadow_stack_status(struct task_struct *t, unsigned long status); + #endif /* _LINUX_MM_H */ diff --git a/include/uapi/linux/prctl.h b/include/uapi/linux/prctl.h index 3c36aeade991..0de3d6ee18e0 100644 --- a/include/uapi/linux/prctl.h +++ b/include/uapi/linux/prctl.h @@ -305,4 +305,26 @@ struct prctl_mm_map { # define PR_RISCV_V_VSTATE_CTRL_NEXT_MASK 0xc # define PR_RISCV_V_VSTATE_CTRL_MASK 0x1f +/* + * Get the current shadow stack configuration for the current thread, + * this will be the value configured via PR_SET_SHADOW_STACK_STATUS. + */ +#define PR_GET_SHADOW_STACK_STATUS 71 + +/* + * Set the current shadow stack configuration. Enabling the shadow + * stack will cause a shadow stack to be allocated for the thread. + */ +#define PR_SET_SHADOW_STACK_STATUS 72 +# define PR_SHADOW_STACK_ENABLE (1UL << 0) +# define PR_SHADOW_STACK_WRITE (1UL << 1) +# define PR_SHADOW_STACK_PUSH (1UL << 2) + +/* + * Prevent further changes to the specified shadow stack + * configuration. All bits may be locked via this call, including + * undefined bits. + */ +#define PR_LOCK_SHADOW_STACK_STATUS 73 + #endif /* _LINUX_PRCTL_H */ diff --git a/kernel/sys.c b/kernel/sys.c index 2410e3999ebe..b26423a614a9 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -2302,6 +2302,21 @@ int __weak arch_prctl_spec_ctrl_set(struct task_struct *t, unsigned long which, return -EINVAL; } +int __weak arch_get_shadow_stack_status(struct task_struct *t, unsigned long __user *status) +{ + return -EINVAL; +} + +int __weak arch_set_shadow_stack_status(struct task_struct *t, unsigned long status) +{ + return -EINVAL; +} + +int __weak arch_lock_shadow_stack_status(struct task_struct *t, unsigned long status) +{ + return -EINVAL; +} + #define PR_IO_FLUSHER (PF_MEMALLOC_NOIO | PF_LOCAL_THROTTLE) #ifdef CONFIG_ANON_VMA_NAME @@ -2720,6 +2735,21 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, case PR_RISCV_V_GET_CONTROL: error = RISCV_V_GET_CONTROL(); break; + case PR_GET_SHADOW_STACK_STATUS: + if (arg3 || arg4 || arg5) + return -EINVAL; + error = arch_get_shadow_stack_status(me, (unsigned long __user *) arg2); + break; + case PR_SET_SHADOW_STACK_STATUS: + if (arg3 || arg4 || arg5) + return -EINVAL; + error = arch_set_shadow_stack_status(me, arg2); + break; + case PR_LOCK_SHADOW_STACK_STATUS: + if (arg3 || arg4 || arg5) + return -EINVAL; + error = arch_lock_shadow_stack_status(me, arg2); + break; default: error = -EINVAL; break; -- 2.30.2