From patchwork Tue Nov 14 20:05:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 743898 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DDFE635EE7; Tue, 14 Nov 2023 20:06:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="dAMoMn4n" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 83A1EC433CA; Tue, 14 Nov 2023 20:06:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1699992383; bh=lxd13UVpNwg8UFzzUvQutHFWJR+9mMSrYBkm92AXfbg=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=dAMoMn4nNDlizf2/5z9VG8WyGtBPtE6lm9Abat7J8MCuT7mYLcNqy+5PWwFXhN3Cn ahAQfAKNrKm5TsjIWPt3gYWO45IuogBzP7Ym4g9AIOz6V7OOvgAaBf5o1eCdmp32zs EWhnikRE5T3P4zZmSzb+Swd91p4M6EJUH4oX4gHKfDmo9eo6fmX4tCPW4To3c5p1WN EOyWpwDOQt+8dMYMaP2VxYSlGvGUk/PUc/2Ojup99XCsww/AP8uCSMT//OFXnw19xm AFByJVckql0Abr2C86rW5OBvrMEmnMCxmymjLXFJe6JXc0XPf02LhD8NVmoi1IlWmO Q+fgsK5R52yIQ== From: Mark Brown Date: Tue, 14 Nov 2023 20:05:54 +0000 Subject: [PATCH RFC RFT v2 1/5] mm: Introduce ARCH_HAS_USER_SHADOW_STACK Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20231114-clone3-shadow-stack-v2-1-b613f8681155@kernel.org> References: <20231114-clone3-shadow-stack-v2-0-b613f8681155@kernel.org> In-Reply-To: <20231114-clone3-shadow-stack-v2-0-b613f8681155@kernel.org> To: "Rick P. Edgecombe" , Deepak Gupta , Szabolcs Nagy , "H.J. Lu" , Florian Weimer , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Peter Zijlstra , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Daniel Bristot de Oliveira , Valentin Schneider , Christian Brauner , Shuah Khan Cc: linux-kernel@vger.kernel.org, Catalin Marinas , Will Deacon , Kees Cook , jannh@google.com, bsegall@google.com, linux-kselftest@vger.kernel.org, linux-api@vger.kernel.org, Mark Brown , David Hildenbrand X-Mailer: b4 0.13-dev-0438c X-Developer-Signature: v=1; a=openpgp-sha256; l=2302; i=broonie@kernel.org; h=from:subject:message-id; bh=lxd13UVpNwg8UFzzUvQutHFWJR+9mMSrYBkm92AXfbg=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlU9MtYr4nXcSuu//z+nY9j0Y0mgC9AkOYwlDq5caW zt8MhVuJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZVPTLQAKCRAk1otyXVSH0Aj8B/ 9la80H2PcjfOgN4C5wr529/loi/SZ84kdt4QTtOSvLd0X53U2CTQjQ4ZMpe8YL0gDJUIH5ccPxGSpW 15Vih60XT79JDpTwH4t2ba0uaUbllDr7eKm6xpS54oW1PTQA+/p7MhJPk27S0A3c6kNfbgFkwEjG14 8+Ol0aF8Dq11mH5Tbe9FuAEC5oyoW7sFz/XCZRFBLMI1AXqiHhVRGRaFHb7o02dLvclx1djz0/1N+1 u5QI35aEncQdw1Uly9SYNODV3yxxabFLuM2K76XGfELwUs3Jtjcv0Nmpp5yPj+bKihx/ppD6C0ElIr cCjUSwb35ozMMV089NLdvJA5S0iap3 X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB Since multiple architectures have support for shadow stacks and we need to select support for this feature in several places in the generic code provide a generic config option that the architectures can select. Suggested-by: David Hildenbrand Signed-off-by: Mark Brown Acked-by: David Hildenbrand --- arch/x86/Kconfig | 1 + fs/proc/task_mmu.c | 2 +- include/linux/mm.h | 2 +- mm/Kconfig | 6 ++++++ 4 files changed, 9 insertions(+), 2 deletions(-) diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 3762f41bb092..14b7703a9a2b 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -1952,6 +1952,7 @@ config X86_USER_SHADOW_STACK depends on AS_WRUSS depends on X86_64 select ARCH_USES_HIGH_VMA_FLAGS + select ARCH_HAS_USER_SHADOW_STACK select X86_CET help Shadow stack protection is a hardware feature that detects function diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c index ef2eb12906da..f0a904aeee8e 100644 --- a/fs/proc/task_mmu.c +++ b/fs/proc/task_mmu.c @@ -699,7 +699,7 @@ static void show_smap_vma_flags(struct seq_file *m, struct vm_area_struct *vma) #ifdef CONFIG_HAVE_ARCH_USERFAULTFD_MINOR [ilog2(VM_UFFD_MINOR)] = "ui", #endif /* CONFIG_HAVE_ARCH_USERFAULTFD_MINOR */ -#ifdef CONFIG_X86_USER_SHADOW_STACK +#ifdef CONFIG_ARCH_HAS_USER_SHADOW_STACK [ilog2(VM_SHADOW_STACK)] = "ss", #endif }; diff --git a/include/linux/mm.h b/include/linux/mm.h index 418d26608ece..10462f354614 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -341,7 +341,7 @@ extern unsigned int kobjsize(const void *objp); #endif #endif /* CONFIG_ARCH_HAS_PKEYS */ -#ifdef CONFIG_X86_USER_SHADOW_STACK +#ifdef CONFIG_ARCH_HAS_USER_SHADOW_STACK /* * VM_SHADOW_STACK should not be set with VM_SHARED because of lack of * support core mm. diff --git a/mm/Kconfig b/mm/Kconfig index 89971a894b60..b8638da636e1 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -1270,6 +1270,12 @@ config LOCK_MM_AND_FIND_VMA bool depends on !STACK_GROWSUP +config ARCH_HAS_USER_SHADOW_STACK + bool + help + The architecture has hardware support for userspace shadow call + stacks (eg, x86 CET, arm64 GCS, RISC-V Zisslpcfi). + source "mm/damon/Kconfig" endmenu From patchwork Tue Nov 14 20:05:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 744307 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E842A35EE7; Tue, 14 Nov 2023 20:06:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="X6ueccfi" Received: by smtp.kernel.org (Postfix) with ESMTPSA id CE786C433C8; Tue, 14 Nov 2023 20:06:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1699992389; bh=rEnFppQxs2VRtsQrM4GtrIYWCx4gRYkNGbc4gkefOew=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=X6ueccfi7VjhhSaPqe6F8CadZN+x042CjlAISPE8RWfJM9uyQ/lf/GC/si68Yr5ea gAUxJfHaBM/tUbGU7YpAbK//Q+VGeI6tMCUsGuto9+3crIcqohIUnRmrOzFt0ECjNE 1hz9CANB1tnXQ7/fq5QemjL7X8YOUv0/Et1L8bFtEyKq26BBgmotO5eKsqBVn26bzn Xlf7+5DCr0lK2e7OtoAfzRotC6zqeOpbgh7lCcXS3TFpR3nQjG3p7b+x2ojf9FWx+I pN4sT9pE3Jdz/8cUHY5k7X9kXeawmH0cL/5as0VBjYCu16DYOvD2cCIJbPrm75KNb9 luwtSmUKv8jyg== From: Mark Brown Date: Tue, 14 Nov 2023 20:05:55 +0000 Subject: [PATCH RFC RFT v2 2/5] fork: Add shadow stack support to clone3() Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20231114-clone3-shadow-stack-v2-2-b613f8681155@kernel.org> References: <20231114-clone3-shadow-stack-v2-0-b613f8681155@kernel.org> In-Reply-To: <20231114-clone3-shadow-stack-v2-0-b613f8681155@kernel.org> To: "Rick P. Edgecombe" , Deepak Gupta , Szabolcs Nagy , "H.J. Lu" , Florian Weimer , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Peter Zijlstra , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Daniel Bristot de Oliveira , Valentin Schneider , Christian Brauner , Shuah Khan Cc: linux-kernel@vger.kernel.org, Catalin Marinas , Will Deacon , Kees Cook , jannh@google.com, bsegall@google.com, linux-kselftest@vger.kernel.org, linux-api@vger.kernel.org, Mark Brown X-Mailer: b4 0.13-dev-0438c X-Developer-Signature: v=1; a=openpgp-sha256; l=9325; i=broonie@kernel.org; h=from:subject:message-id; bh=rEnFppQxs2VRtsQrM4GtrIYWCx4gRYkNGbc4gkefOew=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlU9Mu+37t29M3Co3bC5Tcq+Z5IGymkBXb3k0s52CF uC5qirWJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZVPTLgAKCRAk1otyXVSH0BvsB/ 93NTo9+zfkzEA/mPe6KuM6rZ80LGcs2bxhCjT+Lzx37QDoGLO4N6goSliomWPcWUcwmu2sXx/1pE8W OBKKwLWhp7EAtzvaOmQVrGRrkYA08QD8TLvNMcTtc3AIHjMOKnLMURZeZ2Va4UKEYsIMxeWfVkMzjw 2JEm97fp0Be6OnKz6kQRGjcjsko6c58bamnQ10gfgpLjcUTFfyNY3Kxg/jJEQ4dYxjwZjZ5nJWbuJF rELXt33h+v2faarI3747fHXzjTjVcGEpbqwB30M8Lo6FHddVMrf5cGeZqCZKgoualkugjMbUs6sG4J VXFC1Q4BNkDUY2IyLE7ItuvI3japnA X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB Unlike with the normal stack there is no API for configuring the the shadow stack for a new thread, instead the kernel will dynamically allocate a new shadow stack with the same size as the normal stack. This appears to be due to the shadow stack series having been in development since before the more extensible clone3() was added rather than anything more deliberate. Add parameters to clone3() specifying the address and size of a shadow stack for the newly created process, we validate that the range specified is accessible to userspace but do not validate that it has been mapped appropriately for use as a shadow stack (normally via map_shadow_stack()). If the shadow stack is specified in this way then the caller is responsible for freeing the memory as with the main stack. If no shadow stack is specified then the existing implicit allocation and freeing behaviour is maintained. If the architecture does not support shadow stacks the shadow stack parameters must be zero, architectures that do support the feature are expected to have the same requirement on individual systems that lack shadow stack support. Update the existing x86 implementation to pay attention to the newly added arguments, in order to maintain compatibility we use the existing behaviour if no shadow stack is specified. Minimal validation is done of the supplied parameters, detailed enforcement is left to when the thread is executed. Since we are now using four fields from the kernel_clone_args we pass that into the shadow stack code rather than individual fields. Signed-off-by: Mark Brown --- arch/x86/include/asm/shstk.h | 11 +++++++---- arch/x86/kernel/process.c | 2 +- arch/x86/kernel/shstk.c | 30 +++++++++++++++++++++++++----- include/linux/sched/task.h | 2 ++ include/uapi/linux/sched.h | 4 ++++ kernel/fork.c | 24 ++++++++++++++++++++++-- 6 files changed, 61 insertions(+), 12 deletions(-) diff --git a/arch/x86/include/asm/shstk.h b/arch/x86/include/asm/shstk.h index 42fee8959df7..8be7b0a909c3 100644 --- a/arch/x86/include/asm/shstk.h +++ b/arch/x86/include/asm/shstk.h @@ -6,6 +6,7 @@ #include struct task_struct; +struct kernel_clone_args; struct ksignal; #ifdef CONFIG_X86_USER_SHADOW_STACK @@ -16,8 +17,8 @@ struct thread_shstk { long shstk_prctl(struct task_struct *task, int option, unsigned long arg2); void reset_thread_features(void); -unsigned long shstk_alloc_thread_stack(struct task_struct *p, unsigned long clone_flags, - unsigned long stack_size); +unsigned long shstk_alloc_thread_stack(struct task_struct *p, + const struct kernel_clone_args *args); void shstk_free(struct task_struct *p); int setup_signal_shadow_stack(struct ksignal *ksig); int restore_signal_shadow_stack(void); @@ -26,8 +27,10 @@ static inline long shstk_prctl(struct task_struct *task, int option, unsigned long arg2) { return -EINVAL; } static inline void reset_thread_features(void) {} static inline unsigned long shstk_alloc_thread_stack(struct task_struct *p, - unsigned long clone_flags, - unsigned long stack_size) { return 0; } + const struct kernel_clone_args *args) +{ + return 0; +} static inline void shstk_free(struct task_struct *p) {} static inline int setup_signal_shadow_stack(struct ksignal *ksig) { return 0; } static inline int restore_signal_shadow_stack(void) { return 0; } diff --git a/arch/x86/kernel/process.c b/arch/x86/kernel/process.c index b6f4e8399fca..a9ca80ea5056 100644 --- a/arch/x86/kernel/process.c +++ b/arch/x86/kernel/process.c @@ -207,7 +207,7 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) * is disabled, new_ssp will remain 0, and fpu_clone() will know not to * update it. */ - new_ssp = shstk_alloc_thread_stack(p, clone_flags, args->stack_size); + new_ssp = shstk_alloc_thread_stack(p, args); if (IS_ERR_VALUE(new_ssp)) return PTR_ERR((void *)new_ssp); diff --git a/arch/x86/kernel/shstk.c b/arch/x86/kernel/shstk.c index 59e15dd8d0f8..7ffe90010587 100644 --- a/arch/x86/kernel/shstk.c +++ b/arch/x86/kernel/shstk.c @@ -191,18 +191,38 @@ void reset_thread_features(void) current->thread.features_locked = 0; } -unsigned long shstk_alloc_thread_stack(struct task_struct *tsk, unsigned long clone_flags, - unsigned long stack_size) +unsigned long shstk_alloc_thread_stack(struct task_struct *tsk, + const struct kernel_clone_args *args) { struct thread_shstk *shstk = &tsk->thread.shstk; + unsigned long clone_flags = args->flags; unsigned long addr, size; /* * If shadow stack is not enabled on the new thread, skip any - * switch to a new shadow stack. + * implicit switch to a new shadow stack and reject attempts to + * explciitly specify one. */ - if (!features_enabled(ARCH_SHSTK_SHSTK)) + if (!features_enabled(ARCH_SHSTK_SHSTK)) { + if (args->shadow_stack) + return (unsigned long)ERR_PTR(-EINVAL); + return 0; + } + + /* + * If the user specified a shadow stack then do some basic + * validation and use it. The caller is responsible for + * freeing the shadow stack. + */ + if (args->shadow_stack_size) { + size = args->shadow_stack_size; + + if (size < 8) + return (unsigned long)ERR_PTR(-EINVAL); + } else { + size = args->stack_size; + } /* * For CLONE_VFORK the child will share the parents shadow stack. @@ -222,7 +242,7 @@ unsigned long shstk_alloc_thread_stack(struct task_struct *tsk, unsigned long cl if (!(clone_flags & CLONE_VM)) return 0; - size = adjust_shstk_size(stack_size); + size = adjust_shstk_size(size); addr = alloc_shstk(0, size, 0, false); if (IS_ERR_VALUE(addr)) return addr; diff --git a/include/linux/sched/task.h b/include/linux/sched/task.h index a23af225c898..94e7cf62be51 100644 --- a/include/linux/sched/task.h +++ b/include/linux/sched/task.h @@ -41,6 +41,8 @@ struct kernel_clone_args { void *fn_arg; struct cgroup *cgrp; struct css_set *cset; + unsigned long shadow_stack; + unsigned long shadow_stack_size; }; /* diff --git a/include/uapi/linux/sched.h b/include/uapi/linux/sched.h index 3bac0a8ceab2..a998b6d0c897 100644 --- a/include/uapi/linux/sched.h +++ b/include/uapi/linux/sched.h @@ -84,6 +84,8 @@ * kernel's limit of nested PID namespaces. * @cgroup: If CLONE_INTO_CGROUP is specified set this to * a file descriptor for the cgroup. + * @shadow_stack_size: Specify the size of the shadow stack to allocate + * for the child process. * * The structure is versioned by size and thus extensible. * New struct members must go at the end of the struct and @@ -101,12 +103,14 @@ struct clone_args { __aligned_u64 set_tid; __aligned_u64 set_tid_size; __aligned_u64 cgroup; + __aligned_u64 shadow_stack_size; }; #endif #define CLONE_ARGS_SIZE_VER0 64 /* sizeof first published struct */ #define CLONE_ARGS_SIZE_VER1 80 /* sizeof second published struct */ #define CLONE_ARGS_SIZE_VER2 88 /* sizeof third published struct */ +#define CLONE_ARGS_SIZE_VER3 96 /* sizeof fourth published struct */ /* * Scheduling policies diff --git a/kernel/fork.c b/kernel/fork.c index 10917c3e1f03..b0df69c8185e 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -3067,7 +3067,9 @@ noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs, CLONE_ARGS_SIZE_VER1); BUILD_BUG_ON(offsetofend(struct clone_args, cgroup) != CLONE_ARGS_SIZE_VER2); - BUILD_BUG_ON(sizeof(struct clone_args) != CLONE_ARGS_SIZE_VER2); + BUILD_BUG_ON(offsetofend(struct clone_args, shadow_stack_size) != + CLONE_ARGS_SIZE_VER3); + BUILD_BUG_ON(sizeof(struct clone_args) != CLONE_ARGS_SIZE_VER3); if (unlikely(usize > PAGE_SIZE)) return -E2BIG; @@ -3110,6 +3112,7 @@ noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs, .tls = args.tls, .set_tid_size = args.set_tid_size, .cgroup = args.cgroup, + .shadow_stack_size = args.shadow_stack_size, }; if (args.set_tid && @@ -3150,6 +3153,23 @@ static inline bool clone3_stack_valid(struct kernel_clone_args *kargs) return true; } +/** + * clone3_shadow_stack_valid - check and prepare shadow stack + * @kargs: kernel clone args + * + * Verify that shadow stacks are only enabled if supported. + */ +static inline bool clone3_shadow_stack_valid(struct kernel_clone_args *kargs) +{ +#ifdef CONFIG_ARCH_HAS_USER_SHADOW_STACK + /* The architecture must check support on the specific machine */ + return true; +#else + /* The architecture does not support shadow stacks */ + return !kargs->shadow_stack_size; +#endif +} + static bool clone3_args_valid(struct kernel_clone_args *kargs) { /* Verify that no unknown flags are passed along. */ @@ -3172,7 +3192,7 @@ static bool clone3_args_valid(struct kernel_clone_args *kargs) kargs->exit_signal) return false; - if (!clone3_stack_valid(kargs)) + if (!clone3_stack_valid(kargs) || !clone3_shadow_stack_valid(kargs)) return false; return true; From patchwork Tue Nov 14 20:05:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 743897 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EE4CE35EE3; Tue, 14 Nov 2023 20:06:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="fmtF2Nn2" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E44F9C43391; Tue, 14 Nov 2023 20:06:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1699992395; bh=bhar0tJdOqw54YK6iRyaAGbYhafIqhucm+rmgUHLm6s=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=fmtF2Nn2icZenkA4IwM+7xAO2Mn4euR5sCHSu3HmQbat7uOeLig7MeBwFov6UeP8u zxnmcQ2nO7NZQL0HtupovPoE+MdMjDsiumDeycNpVSqCPyRb7ZaeJrHABMtLJxJKEE LxyossiFnrf6j4esaAU2BnCMmVBeLfhYeD2ndZPArT0+HHYEZrGTWWEAS5+TbsrIkD gqf5bboWviGlOm+BbR/ebP7tw+X4Qi7wMyglJSb2wO2tODGfb3pegZngxBbk8YFxsJ CNcidBL8FwWuBGrSLUOZ1myKQhCXnuGvcmv+DRcOiEBhZXsZ9a+qzpS7D58rJgqsCY HdNWAUNx1ntcQ== From: Mark Brown Date: Tue, 14 Nov 2023 20:05:56 +0000 Subject: [PATCH RFC RFT v2 3/5] selftests/clone3: Factor more of main loop into test_clone3() Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20231114-clone3-shadow-stack-v2-3-b613f8681155@kernel.org> References: <20231114-clone3-shadow-stack-v2-0-b613f8681155@kernel.org> In-Reply-To: <20231114-clone3-shadow-stack-v2-0-b613f8681155@kernel.org> To: "Rick P. Edgecombe" , Deepak Gupta , Szabolcs Nagy , "H.J. Lu" , Florian Weimer , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Peter Zijlstra , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Daniel Bristot de Oliveira , Valentin Schneider , Christian Brauner , Shuah Khan Cc: linux-kernel@vger.kernel.org, Catalin Marinas , Will Deacon , Kees Cook , jannh@google.com, bsegall@google.com, linux-kselftest@vger.kernel.org, linux-api@vger.kernel.org, Mark Brown X-Mailer: b4 0.13-dev-0438c X-Developer-Signature: v=1; a=openpgp-sha256; l=3743; i=broonie@kernel.org; h=from:subject:message-id; bh=bhar0tJdOqw54YK6iRyaAGbYhafIqhucm+rmgUHLm6s=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlU9MvLneP/EjxCvhd9QaLV4BeZIYgqAl9ChiofdJO 7ezfchaJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZVPTLwAKCRAk1otyXVSH0NxdB/ 0d90SFw/2LxBAhS98HfWwNERaQ1TiuMJtU0fTpByUEFvr7RIaRd61faQvp1vufxtUiW0bQRx4gxLlS 48jyoM8cpS/wHrgAPaQ0spHNXRMLHW1VQu7vJkF4PmYv9Wi+nR7MKKtrg01jCJ+v0xuGxOTH84C+8c K4+VJ4epUx49J6gCY7KWcATGpxy5fK3OARD/wPgP7sj85FsvsZ+4c/yGN9mjgUoKsUPsmnGnEdbEJW wm4oL1Z1sfxEvouh4FrHCLl9WhtlM2n+II2L9LczPSbZe/pMzsLVjHp9fnhNieof3w9HZ2Nb8Rn3yT WuonufBs5Hr7GuOBbUlwq7RpnsUOKn X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB In order to make it easier to add more configuration for the tests and more support for runtime detection of when tests can be run pass the structure describing the tests into test_clone3() rather than picking the arguments out of it and have that function do all the per-test work. No functional change. Signed-off-by: Mark Brown --- tools/testing/selftests/clone3/clone3.c | 77 ++++++++++++++++----------------- 1 file changed, 37 insertions(+), 40 deletions(-) diff --git a/tools/testing/selftests/clone3/clone3.c b/tools/testing/selftests/clone3/clone3.c index 3c9bf0cd82a8..1108bd8e36d6 100644 --- a/tools/testing/selftests/clone3/clone3.c +++ b/tools/testing/selftests/clone3/clone3.c @@ -30,6 +30,19 @@ enum test_mode { CLONE3_ARGS_INVAL_EXIT_SIGNAL_NSIG, }; +typedef bool (*filter_function)(void); +typedef size_t (*size_function)(void); + +struct test { + const char *name; + uint64_t flags; + size_t size; + size_function size_function; + int expected; + enum test_mode test_mode; + filter_function filter; +}; + static int call_clone3(uint64_t flags, size_t size, enum test_mode test_mode) { struct __clone_args args = { @@ -104,30 +117,40 @@ static int call_clone3(uint64_t flags, size_t size, enum test_mode test_mode) return 0; } -static bool test_clone3(uint64_t flags, size_t size, int expected, - enum test_mode test_mode) +static void test_clone3(const struct test *test) { + size_t size; int ret; + if (test->filter && test->filter()) { + ksft_test_result_skip("%s\n", test->name); + return; + } + + if (test->size_function) + size = test->size_function(); + else + size = test->size; + + ksft_print_msg("Running test '%s'\n", test->name); + ksft_print_msg( "[%d] Trying clone3() with flags %#" PRIx64 " (size %zu)\n", - getpid(), flags, size); - ret = call_clone3(flags, size, test_mode); + getpid(), test->flags, size); + ret = call_clone3(test->flags, size, test->test_mode); ksft_print_msg("[%d] clone3() with flags says: %d expected %d\n", - getpid(), ret, expected); - if (ret != expected) { + getpid(), ret, test->expected); + if (ret != test->expected) { ksft_print_msg( "[%d] Result (%d) is different than expected (%d)\n", - getpid(), ret, expected); - return false; + getpid(), ret, test->expected); + ksft_test_result_fail("%s\n", test->name); + return; } - return true; + ksft_test_result_pass("%s\n", test->name); } -typedef bool (*filter_function)(void); -typedef size_t (*size_function)(void); - static bool not_root(void) { if (getuid() != 0) { @@ -155,16 +178,6 @@ static size_t page_size_plus_8(void) return getpagesize() + 8; } -struct test { - const char *name; - uint64_t flags; - size_t size; - size_function size_function; - int expected; - enum test_mode test_mode; - filter_function filter; -}; - static const struct test tests[] = { { .name = "simple clone3()", @@ -314,24 +327,8 @@ int main(int argc, char *argv[]) ksft_set_plan(ARRAY_SIZE(tests)); test_clone3_supported(); - for (i = 0; i < ARRAY_SIZE(tests); i++) { - if (tests[i].filter && tests[i].filter()) { - ksft_test_result_skip("%s\n", tests[i].name); - continue; - } - - if (tests[i].size_function) - size = tests[i].size_function(); - else - size = tests[i].size; - - ksft_print_msg("Running test '%s'\n", tests[i].name); - - ksft_test_result(test_clone3(tests[i].flags, size, - tests[i].expected, - tests[i].test_mode), - "%s\n", tests[i].name); - } + for (i = 0; i < ARRAY_SIZE(tests); i++) + test_clone3(&tests[i]); ksft_finished(); } From patchwork Tue Nov 14 20:05:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 744306 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CC13A35EE9; Tue, 14 Nov 2023 20:06:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Uxh8I9pc" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0514EC433C7; Tue, 14 Nov 2023 20:06:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1699992401; bh=ZmJrvmANp1kG9kAUuq4ZkamJ4qQyGfBTGbaFEhsSApg=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=Uxh8I9pcemFU+yGRtS7Vuo8DeS1M2BfZvhrOG5xhsC6Vl9YyuKMKsy6cEUNGdNfiv YmA3bhEelh6LltyPaU9bt3Zc3Lg3i0sjQu9Zwd7nzaVlur7sQZ70zDACZyeTab0nm2 dYQCYAY7bxQ6e77mhy4d2sMZjrl3x4mjjvQgv6fqT1RWKnja7bFeIBoZBnfkz79M8t pahN+VpPRyQrkpJSMfwLojoddj2xVk0MDgpJlE5I3DF5TQ9lHSxgt94f8gOmTvIjLR XTiOU+i0gIO0xfAHPC7G4I7x3lpMx865OJ2rAlENPzHA48GKK10BLFPoG2+w92sY3P zU/cCwv7UzeQg== From: Mark Brown Date: Tue, 14 Nov 2023 20:05:57 +0000 Subject: [PATCH RFC RFT v2 4/5] selftests/clone3: Allow tests to flag if -E2BIG is a valid error code Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20231114-clone3-shadow-stack-v2-4-b613f8681155@kernel.org> References: <20231114-clone3-shadow-stack-v2-0-b613f8681155@kernel.org> In-Reply-To: <20231114-clone3-shadow-stack-v2-0-b613f8681155@kernel.org> To: "Rick P. Edgecombe" , Deepak Gupta , Szabolcs Nagy , "H.J. Lu" , Florian Weimer , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Peter Zijlstra , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Daniel Bristot de Oliveira , Valentin Schneider , Christian Brauner , Shuah Khan Cc: linux-kernel@vger.kernel.org, Catalin Marinas , Will Deacon , Kees Cook , jannh@google.com, bsegall@google.com, linux-kselftest@vger.kernel.org, linux-api@vger.kernel.org, Mark Brown X-Mailer: b4 0.13-dev-0438c X-Developer-Signature: v=1; a=openpgp-sha256; l=1887; i=broonie@kernel.org; h=from:subject:message-id; bh=ZmJrvmANp1kG9kAUuq4ZkamJ4qQyGfBTGbaFEhsSApg=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlU9MvjU45KxHk6MXI07uEtQfGL2f1juaDoVQx1uQt jstsNW6JATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZVPTLwAKCRAk1otyXVSH0Cn2B/ 9NNiANJ4S5XcTNlqQbxd9WuKQLoSseYwhfB+36m1g1HI6ZznOzMdea9h1Mw5JQ4ab4pNSnRgZSJ+uX 7ViidqykpVO08D3UL5GDOfiTf+5qsshyDTU822KPe4CV40HJlVyaS/hHqiT5b1V7f0j/X501W+iEBi N2c9NmMSgFTMPwbfDm078r34xcxrNsRq2PnhO7RsS4aC461EufZRU4KhF3FqI+tGBgEdGW4gAlVBOD AaCdWmgGOc3CuiDQK33lkd06u7bHZAGM1J96SbZAmWcAbXHizTMc5jCafHbgNWM+Kkdbb+m0oMStL7 bRS0J5f9pVElfhbFo0IcbtqUrqkZJD X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB The clone_args structure is extensible, with the syscall passing in the length of the structure. Inside the kernel we use copy_struct_from_user() to read the struct but this has the unfortunate side effect of silently accepting some overrun in the structure size providing the extra data is all zeros. This means that we can't discover the clone3() features that the running kernel supports by simply probing with various struct sizes. We need to check this for the benefit of test systems which run newer kselftests on old kernels. Add a flag which can be set on a test to indicate that clone3() may return -E2BIG due to the use of newer struct versions. Currently no tests need this but it will become an issue for testing clone3() support for shadow stacks, the support for shadow stacks is already present on x86. Signed-off-by: Mark Brown --- tools/testing/selftests/clone3/clone3.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tools/testing/selftests/clone3/clone3.c b/tools/testing/selftests/clone3/clone3.c index 1108bd8e36d6..6adbfd14c841 100644 --- a/tools/testing/selftests/clone3/clone3.c +++ b/tools/testing/selftests/clone3/clone3.c @@ -39,6 +39,7 @@ struct test { size_t size; size_function size_function; int expected; + bool e2big_valid; enum test_mode test_mode; filter_function filter; }; @@ -141,6 +142,11 @@ static void test_clone3(const struct test *test) ksft_print_msg("[%d] clone3() with flags says: %d expected %d\n", getpid(), ret, test->expected); if (ret != test->expected) { + if (test->e2big_valid && ret == -E2BIG) { + ksft_print_msg("Test reported -E2BIG\n"); + ksft_test_result_skip("%s\n", test->name); + return; + } ksft_print_msg( "[%d] Result (%d) is different than expected (%d)\n", getpid(), ret, test->expected); From patchwork Tue Nov 14 20:05:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 743896 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E264135EEA; Tue, 14 Nov 2023 20:06:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="COcQPXbg" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1DABDC433CA; Tue, 14 Nov 2023 20:06:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1699992407; bh=E/vO68ZFg6z/6OFrFC7xPTTrAHCnKC1HTh6WooalFG0=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=COcQPXbgQRtRh6McXlLmwmXao6NZB3DoRessUOEzmGfXy4Q/yfYUiqe2WOLh/IYGF FMBnikyOgbZ4guVGrh4thejR7lpM9/J7elIeMvqn0TvaqPkguJ+s3Aq37l4LuqxRFC v4p5MmzIlI1cXY3d+ExzvWO6rv1/BAv2iZM/SxWfFH+irRYyhcVt5iA5XOIAtofqZs ff55nzUXFCkjKqaW3ENaYA+/zyjpH1diIaH8FSe0+DzEzNzf12qZRNKc9wraAurRpB QTq0/Y8N6T3asG2FNY6JVFVGgPdEAYk1sGStNyZsJR1xaOjloUaEo2iNq1d878F5q1 KPBFNtezCOfpA== From: Mark Brown Date: Tue, 14 Nov 2023 20:05:58 +0000 Subject: [PATCH RFC RFT v2 5/5] kselftest/clone3: Test shadow stack support Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20231114-clone3-shadow-stack-v2-5-b613f8681155@kernel.org> References: <20231114-clone3-shadow-stack-v2-0-b613f8681155@kernel.org> In-Reply-To: <20231114-clone3-shadow-stack-v2-0-b613f8681155@kernel.org> To: "Rick P. Edgecombe" , Deepak Gupta , Szabolcs Nagy , "H.J. Lu" , Florian Weimer , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Peter Zijlstra , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Daniel Bristot de Oliveira , Valentin Schneider , Christian Brauner , Shuah Khan Cc: linux-kernel@vger.kernel.org, Catalin Marinas , Will Deacon , Kees Cook , jannh@google.com, bsegall@google.com, linux-kselftest@vger.kernel.org, linux-api@vger.kernel.org, Mark Brown X-Mailer: b4 0.13-dev-0438c X-Developer-Signature: v=1; a=openpgp-sha256; l=4655; i=broonie@kernel.org; h=from:subject:message-id; bh=E/vO68ZFg6z/6OFrFC7xPTTrAHCnKC1HTh6WooalFG0=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlU9Mw/E0ZsNXT9pAEZkYqPqXgi0HGwFTiejBMba7Z 6cP6PmaJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZVPTMAAKCRAk1otyXVSH0LQGB/ 4oDg/SgcY9WyortMyVY5pGTeVD+7/87/hOdY7hfIp+F1K7NuGnb1VAshA9GDrQrRrVDJR33MztYPws ERtdrqnCijOhMif91Jz0Mb6PPxhuPwdQM2wrWk3v7RpgcOyxMN0RaDlVwz4cfhfT8lM1btzDn+R8gP lv1fW6naRVaEBk1u+XByiBjDcNuC2gN6PnxA2uviZWaJEfRitQhRtzZLzcS7JF7G/LDzK9DX985Qcp XKNXyEOsQRryZb01iDaU7uZElG619LThNkspj0Ssw4LBsC1LbumQi+5yiwNW1+WR1Ms0PvU5OPq8JD Gq2E+E1TMJzFmDyydw6e59q55JmVey X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB Add basic test coverage for specifying the shadow stack for a newly created thread via clone3(), including coverage of the newly extended argument structure. We detect support for shadow stacks on the running system by attempting to allocate a shadow stack page during initialisation using map_shadow_stack(). Signed-off-by: Mark Brown --- tools/testing/selftests/clone3/clone3.c | 68 +++++++++++++++++++++++ tools/testing/selftests/clone3/clone3_selftests.h | 7 +++ 2 files changed, 75 insertions(+) diff --git a/tools/testing/selftests/clone3/clone3.c b/tools/testing/selftests/clone3/clone3.c index 6adbfd14c841..10e0487c402a 100644 --- a/tools/testing/selftests/clone3/clone3.c +++ b/tools/testing/selftests/clone3/clone3.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -21,6 +22,9 @@ #include "../kselftest.h" #include "clone3_selftests.h" +static bool shadow_stack_supported; +static size_t max_supported_args_size; + enum test_mode { CLONE3_ARGS_NO_TEST, CLONE3_ARGS_ALL_0, @@ -28,6 +32,7 @@ enum test_mode { CLONE3_ARGS_INVAL_EXIT_SIGNAL_NEG, CLONE3_ARGS_INVAL_EXIT_SIGNAL_CSIG, CLONE3_ARGS_INVAL_EXIT_SIGNAL_NSIG, + CLONE3_ARGS_SHADOW_STACK, }; typedef bool (*filter_function)(void); @@ -44,6 +49,27 @@ struct test { filter_function filter; }; +#ifndef __NR_map_shadow_stack +#define __NR_map_shadow_stack 453 +#endif + +static void test_shadow_stack_supported(void) +{ + long shadow_stack; + + shadow_stack = syscall(__NR_map_shadow_stack, 0, getpagesize(), 0); + if (shadow_stack == -1) { + ksft_print_msg("map_shadow_stack() not supported\n"); + } else if ((void *)shadow_stack == MAP_FAILED) { + ksft_print_msg("Failed to map shadow stack\n"); + } else { + ksft_print_msg("Shadow stack supportd\n"); + shadow_stack_supported = true; + + munmap((void *)shadow_stack, getpagesize()); + } +} + static int call_clone3(uint64_t flags, size_t size, enum test_mode test_mode) { struct __clone_args args = { @@ -89,6 +115,9 @@ static int call_clone3(uint64_t flags, size_t size, enum test_mode test_mode) case CLONE3_ARGS_INVAL_EXIT_SIGNAL_NSIG: args.exit_signal = 0x00000000000000f0ULL; break; + case CLONE3_ARGS_SHADOW_STACK: + args.shadow_stack_size = getpagesize(); + break; } memcpy(&args_ext.args, &args, sizeof(struct __clone_args)); @@ -179,6 +208,26 @@ static bool no_timenamespace(void) return true; } +static bool have_shadow_stack(void) +{ + if (shadow_stack_supported) { + ksft_print_msg("Shadow stack supported\n"); + return true; + } + + return false; +} + +static bool no_shadow_stack(void) +{ + if (!shadow_stack_supported) { + ksft_print_msg("Shadow stack not supported\n"); + return true; + } + + return false; +} + static size_t page_size_plus_8(void) { return getpagesize() + 8; @@ -322,6 +371,24 @@ static const struct test tests[] = { .expected = -EINVAL, .test_mode = CLONE3_ARGS_NO_TEST, }, + { + .name = "Shadow stack on system with shadow stack", + .flags = 0, + .size = 0, + .expected = 0, + .e2big_valid = true, + .test_mode = CLONE3_ARGS_SHADOW_STACK, + .filter = no_shadow_stack, + }, + { + .name = "Shadow stack on system without shadow stack", + .flags = 0, + .size = 0, + .expected = -EINVAL, + .e2big_valid = true, + .test_mode = CLONE3_ARGS_SHADOW_STACK, + .filter = have_shadow_stack, + }, }; int main(int argc, char *argv[]) @@ -332,6 +399,7 @@ int main(int argc, char *argv[]) ksft_print_header(); ksft_set_plan(ARRAY_SIZE(tests)); test_clone3_supported(); + test_shadow_stack_supported(); for (i = 0; i < ARRAY_SIZE(tests); i++) test_clone3(&tests[i]); diff --git a/tools/testing/selftests/clone3/clone3_selftests.h b/tools/testing/selftests/clone3/clone3_selftests.h index 3d2663fe50ba..2e06127091f5 100644 --- a/tools/testing/selftests/clone3/clone3_selftests.h +++ b/tools/testing/selftests/clone3/clone3_selftests.h @@ -31,6 +31,13 @@ struct __clone_args { __aligned_u64 set_tid; __aligned_u64 set_tid_size; __aligned_u64 cgroup; +#ifndef CLONE_ARGS_SIZE_VER2 +#define CLONE_ARGS_SIZE_VER2 88 /* sizeof third published struct */ +#endif + __aligned_u64 shadow_stack_size; +#ifndef CLONE_ARGS_SIZE_VER3 +#define CLONE_ARGS_SIZE_VER3 96 /* sizeof fourth published struct */ +#endif }; static pid_t sys_clone3(struct __clone_args *args, size_t size)